From 7fdce07ebdcbb94b6464e348734d3548c506146b Mon Sep 17 00:00:00 2001 From: liuzerun Date: Fri, 14 Jul 2023 07:34:18 +0000 Subject: [PATCH 1/4] clean code Signed-off-by: liuzerun --- fs/hmdfs/file_merge.c | 26 ++++++++++++-------------- fs/hmdfs/hmdfs_server.c | 24 ++++++++++++------------ fs/hmdfs/inode_local.c | 4 ++-- fs/hmdfs/inode_remote.c | 9 ++++++--- 4 files changed, 32 insertions(+), 31 deletions(-) diff --git a/fs/hmdfs/file_merge.c b/fs/hmdfs/file_merge.c index 6dea80cf8597..b3f406393e49 100644 --- a/fs/hmdfs/file_merge.c +++ b/fs/hmdfs/file_merge.c @@ -385,7 +385,7 @@ int do_dir_open_merge(struct file *file, const struct cred *cred, if (IS_ERR_OR_NULL(cred)) return ret; - + wait_event(dim->wait_queue, !has_merge_lookup_work(dim)); mutex_lock(&dim->comrade_list_lock); @@ -600,7 +600,7 @@ static int copy_string_from_user(unsigned long pos, unsigned long len, { char *tmp_data; - if (!access_ok((char *)pos, len)) + if (!access_ok(pos, len)) return -EFAULT; tmp_data = kmalloc(len, GFP_KERNEL); @@ -608,31 +608,30 @@ static int copy_string_from_user(unsigned long pos, unsigned long len, return -ENOMEM; *data = tmp_data; - if (copy_from_user(tmp_data, (char __user *)pos, len)){ + if (copy_from_user(tmp_data, (char __user *)pos, len)) return -EFAULT; - } return 0; } -static int hmdfs_get_info_from_user(unsigned long pos, +static int hmdfs_get_info_from_user(unsigned long pos, struct hmdfs_dst_info *hdi, struct hmdfs_user_info *data) { int ret = 0; - if (!access_ok((struct hmdfs_dst_info __user *)pos, + if (!access_ok((struct hmdfs_dst_info __user *)pos, sizeof(struct hmdfs_dst_info))) return -ENOMEM; if (copy_from_user(hdi, (struct hmdfs_dst_info __user *)pos, sizeof(struct hmdfs_dst_info))) return -EFAULT; - + ret = copy_string_from_user(hdi->local_path_pos, hdi->local_path_len, &data->local_path); if (ret != 0) return ret; - ret = copy_string_from_user(hdi->distributed_path_pos, + ret = copy_string_from_user(hdi->distributed_path_pos, hdi->distributed_path_len, &data->distributed_path); if (ret != 0) @@ -646,7 +645,7 @@ static int hmdfs_get_info_from_user(unsigned long pos, return 0; } -static const struct cred *change_cred(struct dentry *dentry, +static const struct cred *change_cred(struct dentry *dentry, const char *bundle_name) { int bid; @@ -654,16 +653,15 @@ static const struct cred *change_cred(struct dentry *dentry, const struct cred *old_cred = NULL; cred = prepare_creds(); - if (!cred) { + if (!cred) return NULL; - } bid = get_bundle_uid(hmdfs_sb(dentry->d_sb), bundle_name); if (bid != 0) { cred->fsuid = KUIDT_INIT(bid); cred->fsgid = KGIDT_INIT(bid); old_cred = override_creds(cred); } - + return old_cred; } @@ -700,7 +698,7 @@ static int create_link_file(struct hmdfs_user_info *data) path_put(&path); return ret; } - + dentry = kern_path_create(AT_FDCWD, data->distributed_path, &path, 0); if (IS_ERR(dentry)) return PTR_ERR(dentry); @@ -726,7 +724,7 @@ static int create_dir(const char *path_value, mode_t mode) if (err && err != -EEXIST) hmdfs_err("vfs_mkdir failed, err = %d", err); done_path_create(&path, dentry); - + return err; } diff --git a/fs/hmdfs/hmdfs_server.c b/fs/hmdfs/hmdfs_server.c index d2dec5c95ccb..2e7a0cbaf250 100644 --- a/fs/hmdfs/hmdfs_server.c +++ b/fs/hmdfs/hmdfs_server.c @@ -98,7 +98,7 @@ struct file *hmdfs_open_link(struct hmdfs_sb_info *sbi, if (IS_ERR(file)) { hmdfs_info("filp_open failed: %ld", PTR_ERR(file)); } else { - hmdfs_info("get file with magic %lu", + hmdfs_info("get file with magic %lu", file->f_inode->i_sb->s_magic); } @@ -190,7 +190,7 @@ void __init hmdfs_server_add_node_evt_cb(void) hmdfs_node_add_evt_cb(server_cb, ARRAY_SIZE(server_cb)); } -static int hmdfs_get_inode_by_name(struct hmdfs_peer *con, const char *filename, +static int hmdfs_get_inode_by_name(struct hmdfs_peer *con, const char *filename, uint64_t *ino) { int ret = 0; @@ -204,7 +204,7 @@ static int hmdfs_get_inode_by_name(struct hmdfs_peer *con, const char *filename, return ret; } - ret = vfs_path_lookup(root_path.dentry, root_path.mnt, filename, 0, + ret = vfs_path_lookup(root_path.dentry, root_path.mnt, filename, 0, &dst_path); if (ret) { path_put(&root_path); @@ -1224,7 +1224,7 @@ void hmdfs_server_rename(struct hmdfs_peer *con, struct hmdfs_head_cmd *cmd, hmdfs_send_err_response(con, cmd, err); } -static int hmdfs_lookup_symlink(struct path *link_path, const char *path_fmt, +static int hmdfs_lookup_symlink(struct path *link_path, const char *path_fmt, ... ) { int ret; @@ -1233,7 +1233,7 @@ static int hmdfs_lookup_symlink(struct path *link_path, const char *path_fmt, if (!path) return -ENOMEM; - + va_start(args, path_fmt); ret = vsnprintf(path, PATH_MAX, path_fmt, args); va_end(args); @@ -1311,11 +1311,11 @@ static int hmdfs_filldir_real(struct dir_context *ctx, const char *name, } else if (d_type == DT_LNK) { struct path link_path; - res = hmdfs_lookup_symlink(&link_path, "%s/%s/%s", + res = hmdfs_lookup_symlink(&link_path, "%s/%s/%s", gc->sbi->local_src, gc->dir, name); if (!res) { - create_dentry(child, d_inode(link_path.dentry), + create_dentry(child, d_inode(link_path.dentry), gc->file, gc->sbi); path_put(&link_path); gc->num++; @@ -1448,17 +1448,17 @@ void hmdfs_server_writepage(struct hmdfs_peer *con, struct hmdfs_head_cmd *cmd, hmdfs_server_check_writeback(hswb); } -static int hmdfs_lookup_linkpath(struct hmdfs_sb_info *sbi, +static int hmdfs_lookup_linkpath(struct hmdfs_sb_info *sbi, const char *path_name, struct path *dst_path) { struct path link_path; int err; - err = hmdfs_lookup_symlink(&link_path, "%s/%s", sbi->local_dst, + err = hmdfs_lookup_symlink(&link_path, "%s/%s", sbi->local_dst, path_name); if (err) return err; - + if (d_inode(link_path.dentry)->i_sb != sbi->sb) { path_put(dst_path); *dst_path = link_path; @@ -1537,7 +1537,7 @@ void hmdfs_server_setattr(struct hmdfs_peer *con, struct hmdfs_head_cmd *cmd, if (S_ISLNK(inode->i_mode)) { err = hmdfs_lookup_linkpath(con->sbi, recv->buf, &dst_path); - if(err == -ENOENT) + if(err == -ENOENT) err = 0; else if (err) goto out_put_dst; @@ -1634,7 +1634,7 @@ void hmdfs_server_getattr(struct hmdfs_peer *con, struct hmdfs_head_cmd *cmd, if (S_ISLNK(inode->i_mode)) { err = hmdfs_lookup_linkpath(con->sbi, recv->buf, &dst_path); - if(err && err != -ENOENT) + if(err && err != -ENOENT) goto out_put_dst; } diff --git a/fs/hmdfs/inode_local.c b/fs/hmdfs/inode_local.c index aca355d33928..55f56d35f4b3 100644 --- a/fs/hmdfs/inode_local.c +++ b/fs/hmdfs/inode_local.c @@ -740,8 +740,8 @@ int hmdfs_rename_local(struct inode *old_dir, struct dentry *old_dentry, return err; } -static const char *hmdfs_get_link_local(struct dentry *dentry, - struct inode *inode, +static const char *hmdfs_get_link_local(struct dentry *dentry, + struct inode *inode, struct delayed_call *done) { const char *link = NULL; diff --git a/fs/hmdfs/inode_remote.c b/fs/hmdfs/inode_remote.c index fb227d41abb6..c42f81cb9a25 100644 --- a/fs/hmdfs/inode_remote.c +++ b/fs/hmdfs/inode_remote.c @@ -371,12 +371,15 @@ struct inode *fill_inode_remote(struct super_block *sb, struct hmdfs_peer *con, inode->i_uid = KUIDT_INIT((uid_t)1000); inode->i_gid = KGIDT_INIT((gid_t)1000); - if (S_ISDIR(mode)) + if (S_ISDIR(mode)) { inode->i_mode = S_IFDIR | S_IRWXU | S_IRWXG | S_IXOTH; - else if (S_ISREG(mode)) + } + else if (S_ISREG(mode)) { inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; - else if (S_ISLNK(mode)) + } + else if (S_ISLNK(mode)) { inode->i_mode = S_IFREG | S_IRWXU | S_IRWXG; + } else { ret = -EIO; goto bad_inode; -- Gitee From fc54a0a84aab866a1d421f7ca195a980495513ff Mon Sep 17 00:00:00 2001 From: liuzerun Date: Mon, 17 Jul 2023 08:53:23 +0000 Subject: [PATCH 2/4] add logs Signed-off-by: liuzerun --- fs/hmdfs/file_merge.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/fs/hmdfs/file_merge.c b/fs/hmdfs/file_merge.c index b3f406393e49..9d74f8e844b1 100644 --- a/fs/hmdfs/file_merge.c +++ b/fs/hmdfs/file_merge.c @@ -600,16 +600,22 @@ static int copy_string_from_user(unsigned long pos, unsigned long len, { char *tmp_data; - if (!access_ok(pos, len)) + if (!access_ok(pos, len)) { + hmdfs_err("User space pointer is not available, %d", EFAULT); return -EFAULT; + } tmp_data = kmalloc(len, GFP_KERNEL); - if (!tmp_data) + if (!tmp_data) { + hmdfs_err("No Memory, %d", ENOMEM); return -ENOMEM; + } *data = tmp_data; - if (copy_from_user(tmp_data, (char __user *)pos, len)) + if (copy_from_user(tmp_data, (char __user *)pos, len)) { + hmdfs_err("Data is not fully copied, %d", EFAULT); return -EFAULT; + } return 0; } @@ -620,11 +626,15 @@ static int hmdfs_get_info_from_user(unsigned long pos, int ret = 0; if (!access_ok((struct hmdfs_dst_info __user *)pos, - sizeof(struct hmdfs_dst_info))) - return -ENOMEM; + sizeof(struct hmdfs_dst_info))) { + hmdfs_err("User space pointer of struct is not available, %d", EFAULT); + return -EFAULT; + } if (copy_from_user(hdi, (struct hmdfs_dst_info __user *)pos, - sizeof(struct hmdfs_dst_info))) + sizeof(struct hmdfs_dst_info))) { + hmdfs_err("Struct is not fully copied, %d", EFAULT); return -EFAULT; + } ret = copy_string_from_user(hdi->local_path_pos, hdi->local_path_len, &data->local_path); @@ -653,8 +663,10 @@ static const struct cred *change_cred(struct dentry *dentry, const struct cred *old_cred = NULL; cred = prepare_creds(); - if (!cred) + if (!cred) { + hmdfs_err("Failed to prepare cred, %d", EACCES); return NULL; + } bid = get_bundle_uid(hmdfs_sb(dentry->d_sb), bundle_name); if (bid != 0) { cred->fsuid = KUIDT_INIT(bid); @@ -700,8 +712,10 @@ static int create_link_file(struct hmdfs_user_info *data) } dentry = kern_path_create(AT_FDCWD, data->distributed_path, &path, 0); - if (IS_ERR(dentry)) + if (IS_ERR(dentry)) { + hmdfs_err("create_link_file failed, %d", PTR_ERR(dentry)); return PTR_ERR(dentry); + } ret = vfs_symlink(path.dentry->d_inode, dentry, data->local_path); done_path_create(&path, dentry); @@ -717,8 +731,10 @@ static int create_dir(const char *path_value, mode_t mode) dentry = kern_path_create(AT_FDCWD, path_value, &path, LOOKUP_DIRECTORY); if(PTR_ERR(dentry) == -EEXIST) return 0; - if (IS_ERR(dentry)) + if (IS_ERR(dentry)) { + hmdfs_err("create_dir failed, %d", PTR_ERR(dentry)); return PTR_ERR(dentry); + } err = vfs_mkdir(d_inode(path.dentry), dentry, mode); if (err && err != -EEXIST) -- Gitee From 20f6e8426dccb777166f36283c3d52853e68085c Mon Sep 17 00:00:00 2001 From: liuzerun Date: Thu, 27 Jul 2023 06:50:11 +0000 Subject: [PATCH 3/4] change %d to %ld Signed-off-by: liuzerun --- fs/hmdfs/file_merge.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fs/hmdfs/file_merge.c b/fs/hmdfs/file_merge.c index 253882d5182e..fa334915a8e2 100644 --- a/fs/hmdfs/file_merge.c +++ b/fs/hmdfs/file_merge.c @@ -714,7 +714,7 @@ static int create_link_file(struct hmdfs_user_info *data) dentry = kern_path_create(AT_FDCWD, data->distributed_path, &path, 0); if (IS_ERR(dentry)) { - hmdfs_err("create_link_file failed, %d", PTR_ERR(dentry)); + hmdfs_err("create_link_file failed, %ld", PTR_ERR(dentry)); return PTR_ERR(dentry); } ret = vfs_symlink(path.dentry->d_inode, dentry, data->local_path); @@ -733,7 +733,7 @@ static int create_dir(const char *path_value, mode_t mode) if(PTR_ERR(dentry) == -EEXIST) return 0; if (IS_ERR(dentry)) { - hmdfs_err("create_dir failed, %d", PTR_ERR(dentry)); + hmdfs_err("create_dir failed, %ld", PTR_ERR(dentry)); return PTR_ERR(dentry); } -- Gitee From a908cdec19bf1d8299bbee048735cd9d8f94c589 Mon Sep 17 00:00:00 2001 From: liuzerun Date: Fri, 11 Aug 2023 10:43:46 +0000 Subject: [PATCH 4/4] change to check_and_fixup Signed-off-by: liuzerun --- fs/hmdfs/inode_merge.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fs/hmdfs/inode_merge.c b/fs/hmdfs/inode_merge.c index 1982f446ff31..c0bebf3a1fc0 100644 --- a/fs/hmdfs/inode_merge.c +++ b/fs/hmdfs/inode_merge.c @@ -850,8 +850,8 @@ int do_mkdir_merge(struct inode *parent_inode, struct dentry *child_dentry, ret = PTR_ERR(child_inode); goto out; } - child_inode->i_uid = parent_inode->i_uid; - child_inode->i_gid = parent_inode->i_gid; + check_and_fixup_ownership_remote(parent_inode, child_inode, + child_dentry); d_add(child_dentry, child_inode); /* nlink should be increased with the joining of children */ @@ -878,8 +878,8 @@ int do_create_merge(struct inode *parent_inode, struct dentry *child_dentry, ret = PTR_ERR(child_inode); goto out; } - child_inode->i_uid = parent_inode->i_uid; - child_inode->i_gid = parent_inode->i_gid; + check_and_fixup_ownership_remote(parent_inode, child_inode, + child_dentry); d_add(child_dentry, child_inode); /* nlink should be increased with the joining of children */ -- Gitee