btrfs: reuse existing pointers from btrfs_ioctl
cover btrfs: reuse existing pointers from btrfs_ioctl
This is a small cleanup that reuses some of the existing pointers and removes the duplicated dereferencing.
There are other subfunctions that have similar/identical dereferencing as the enclosing btrfs_ioctl, but I'm not sure if changing those is welcomed since it would require changing their arg count. Right now, all of the sub ioctl functions have essentially the same signature.
For example, reusing the pointers for btrfs_ioctl_set_fslabel would require passing in *fs_info and *root, in addition to its current args.
Sahil Kang (1):
btrfs: reuse existing pointers from btrfs_ioctl
fs/btrfs/ioctl.c | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-)
Signature · 1 line
Sahil
PATCH 1/1 btrfs: reuse existing pointers from btrfs_ioctl
btrfs_ioctl already contains pointers to the inode and btrfs_root structs, so we can pass them into the subfunctions instead of the toplevel struct file.
--- fs/btrfs/ioctl.c | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-)
fs/btrfs/ioctl.c +11 -17
@@ -414,10 +414,8 @@ void btrfs_exclop_finish(struct btrfs_fs_info *fs_info)sysfs_notify (&fs_info->fs_devices->fsid_kobj,NULL ,"exclusive_operation" ); } -static int btrfs_ioctl_getversion (struct file *file,int __user *arg) +static int btrfs_ioctl_getversion (struct inode *inode,int __user *arg) { -struct inode *inode =file_inode (file); -return put_user (inode->i_generation, arg); }
@@ -2559,25 +2557,22 @@ static int btrfs_search_path_in_tree_user(struct user_namespace *mnt_userns,return ret; } -static noinline int btrfs_ioctl_ino_lookup(struct file *file, +static noinline int btrfs_ioctl_ino_lookup(struct btrfs_root *root,void __user *argp) {struct btrfs_ioctl_ino_lookup_args *args; -struct inode *inode;int ret =0 ; args =memdup_user (argp,sizeof (*args));if (IS_ERR (args))return PTR_ERR (args); - inode =file_inode (file); -/* * Unprivileged query to obtain the containing subvolume root id. The * path is reset so it's consistent with btrfs_search_path_in_tree. */ if (args->treeid ==0 ) - args->treeid =BTRFS_I (inode)->root->root_key.objectid; + args->treeid = root->root_key.objectid;if (args->objectid ==BTRFS_FIRST_FREE_OBJECTID ) { args->name[0 ] =0 ;
@@ -2589,7 +2584,7 @@ static noinline int btrfs_ioctl_ino_lookup(struct file *file, goto out; } - ret =btrfs_search_path_in_tree (BTRFS_I (inode)->root->fs_info, + ret =btrfs_search_path_in_tree (root->fs_info, args->treeid, args->objectid, args->name);
@@ -2765,7 +2760,8 @@ static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)* Return ROOT_REF information of the subvolume containing this inode * except the subvolume name. */ -static int btrfs_ioctl_get_subvol_rootref (struct file *file,void __user *argp) +static int btrfs_ioctl_get_subvol_rootref (struct btrfs_root *subvol_root, +void __user *argp) {struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;struct btrfs_root_ref *rref;
@@ -2773,7 +2769,6 @@ static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)struct btrfs_path *path;struct btrfs_key key;struct extent_buffer *leaf; -struct inode *inode;u64 objectid;int slot;int ret;
@@ -2789,9 +2784,8 @@ static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)return PTR_ERR (rootrefs); } - inode =file_inode (file); - root =BTRFS_I (inode)->root->fs_info->tree_root; - objectid =BTRFS_I (inode)->root->root_key.objectid; + root = subvol_root->fs_info->tree_root; + objectid = subvol_root->root_key.objectid; key.objectid = objectid; key.type =BTRFS_ROOT_REF_KEY ;
@@ -4873,7 +4867,7 @@ long btrfs_ioctl(struct file *file, unsigned intswitch (cmd) {case FS_IOC_GETVERSION : -return btrfs_ioctl_getversion (file, argp); +return btrfs_ioctl_getversion (inode, argp);case FS_IOC_GETFSLABEL :return btrfs_ioctl_get_fslabel (fs_info, argp);case FS_IOC_SETFSLABEL :
@@ -4921,7 +4915,7 @@ long btrfs_ioctl(struct file *file, unsigned intcase BTRFS_IOC_TREE_SEARCH_V2 :return btrfs_ioctl_tree_search_v2 (file, argp);case BTRFS_IOC_INO_LOOKUP : -return btrfs_ioctl_ino_lookup (file, argp); +return btrfs_ioctl_ino_lookup (root, argp);case BTRFS_IOC_INO_PATHS :return btrfs_ioctl_ino_to_path (root, argp);case BTRFS_IOC_LOGICAL_INO :
@@ -5000,7 +4994,7 @@ long btrfs_ioctl(struct file *file, unsigned intcase BTRFS_IOC_GET_SUBVOL_INFO :return btrfs_ioctl_get_subvol_info (file, argp);case BTRFS_IOC_GET_SUBVOL_ROOTREF : -return btrfs_ioctl_get_subvol_rootref (file, argp); +return btrfs_ioctl_get_subvol_rootref (root, argp);case BTRFS_IOC_INO_LOOKUP_USER :return btrfs_ioctl_ino_lookup_user (file, argp);case FS_IOC_ENABLE_VERITY :
Signature · 1 line
Sahil
Quoted 5 lines · Sahil Kang
btrfs_ioctlalready contains pointers to the inode andbtrfs_rootstructs, so we can pass them into the subfunctions instead of the toplevel struct file.Signed-off-by Sahil Kang <[email protected]>
Just some small nitpicks inlined below.
[...]
fs/btrfs/ioctl.c static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp) · line 2764
Quoted 3 lines
fs/btrfs/ioctl.c +2
-1-static int btrfs_ioctl_get_subvol_rootref (struct file *file,void __user *argp) +static int btrfs_ioctl_get_subvol_rootref (struct btrfs_root *subvol_root, +void __user *argp)
I guess you're using the @subvol_root naming is to avoid the conflicts in the existing @root defined locally.
It may be a better idea to rename @root to @tree_root, or doesn't define it at all.
Since the existing @root is only used in 3 locations. [...]
Quoted 19 lines
fs/btrfs/ioctl.c +2
-2@@ -4873,7 +4867,7 @@ long btrfs_ioctl(struct file *file, unsigned intswitch (cmd) {case FS_IOC_GETVERSION : -return btrfs_ioctl_getversion (file, argp); +return btrfs_ioctl_getversion (inode, argp);case FS_IOC_GETFSLABEL :return btrfs_ioctl_get_fslabel (fs_info, argp);case FS_IOC_SETFSLABEL :@@ -4921,7 +4915,7 @@ long btrfs_ioctl(struct file *file, unsigned intcase BTRFS_IOC_TREE_SEARCH_V2 :return btrfs_ioctl_tree_search_v2 (file, argp);case BTRFS_IOC_INO_LOOKUP : -return btrfs_ioctl_ino_lookup (file, argp); +return btrfs_ioctl_ino_lookup (root, argp);case BTRFS_IOC_INO_PATHS :return btrfs_ioctl_ino_to_path (root, argp);case BTRFS_IOC_LOGICAL_INO :
fs/btrfs/ioctl.c long btrfs_ioctl(struct file *file, unsigned int · line 5000
Quoted 8 lines
fs/btrfs/ioctl.c +1
-1@@ -5000,7 +4994,7 @@ long btrfs_ioctl(struct file *file, unsigned intcase BTRFS_IOC_GET_SUBVOL_INFO :return btrfs_ioctl_get_subvol_info (file, argp);case BTRFS_IOC_GET_SUBVOL_ROOTREF : -return btrfs_ioctl_get_subvol_rootref (file, argp); +return btrfs_ioctl_get_subvol_rootref (root, argp);case BTRFS_IOC_INO_LOOKUP_USER :return btrfs_ioctl_ino_lookup_user (file, argp);case FS_IOC_ENABLE_VERITY :
There are some remaining ioctl sub-routines which you may want to further cleanup, as they don't really rely on @file to do write check (aka, RO operations):
- btrfs_ioctl_subvol_getflags()
- btrfs_ioctl_tree_search()
- btrfs_ioctl_tree_search_v2()
- btrfs_ioctl_send()
- btrfs_ioctl_get_subvol_info()
- btrfs_ioctl_ino_lookup_user()
One suspicious ioctls, they should require write permission, but don't:
- btrfs_ioctl_start_sync()
If there is no running trans, it would bail out without doing anything, but I'm wondering if we should call mnt_want_write_file().
As this means, if we mount a subvolume read-only, but the fs still has some part mounted RW, we can start a sync using the read-only mounted part.
Thanks,
Qu
Quoted 9 lines · Qu Wenruo
One suspicious ioctls, they should require write permission, but don't:
-
btrfs_ioctl_start_sync()
If there is no running trans, it would bail out without doing anything, but I'm wondering if we should callmnt_want_write_file().As this means, if we mount a subvolume read-only, but the fs still has some part mounted RW, we can start a sync using the read-only mounted part.
We can possibly do the mnt_want_write but it should not propagate the errors, because calling sync on a fully read-only filesystem should not error out.
cover btrfs: reuse existing inode from btrfs_ioctl
This is a follow up to misc-next/152adfc to incorporate Qu's review.
Sahil Kang (1):
btrfs: reuse existing inode from btrfs_ioctl
fs/btrfs/ioctl.c | 43 ++++++++++++++++++------------------------- fs/btrfs/send.c | 4 ++-- fs/btrfs/send.h | 2 +- 3 files changed, 21 insertions(+), 28 deletions(-)
Signature · 1 line
Sahil
PATCH 1/1 btrfs: reuse existing inode from btrfs_ioctl
btrfs_ioctl extracts inode from file so we can pass that into the subfunctions.
--- fs/btrfs/ioctl.c | 43 ++++++++++++++++++------------------------- fs/btrfs/send.c | 4 ++-- fs/btrfs/send.h | 2 +- 3 files changed, 21 insertions(+), 28 deletions(-)
fs/btrfs/ioctl.c +18 -25
@@ -1941,10 +1941,9 @@ static noinline int btrfs_ioctl_snap_create_v2(struct file *file,return ret; } -static noinline int btrfs_ioctl_subvol_getflags(struct file *file, +static noinline int btrfs_ioctl_subvol_getflags(struct inode *inode,void __user *arg) { -struct inode *inode =file_inode (file);struct btrfs_fs_info *fs_info =btrfs_sb (inode->i_sb);struct btrfs_root *root =BTRFS_I (inode)->root;int ret =0 ;
@@ -2274,12 +2273,11 @@ static noinline int search_ioctl(struct inode *inode,return ret; } -static noinline int btrfs_ioctl_tree_search(struct file *file, -void __user *argp) +static noinline int btrfs_ioctl_tree_search(struct inode *inode, +void __user *argp) {struct btrfs_ioctl_search_args __user *uargs;struct btrfs_ioctl_search_key sk; -struct inode *inode;int ret;size_t buf_size;
@@ -2293,7 +2291,6 @@ static noinline int btrfs_ioctl_tree_search(struct file *file, buf_size =sizeof (uargs->buf); - inode =file_inode (file); ret =search_ioctl (inode, &sk, &buf_size, uargs->buf);/*
@@ -2308,12 +2305,11 @@ static noinline int btrfs_ioctl_tree_search(struct file *file,return ret; } -static noinline int btrfs_ioctl_tree_search_v2(struct file *file, +static noinline int btrfs_ioctl_tree_search_v2(struct inode *inode,void __user *argp) {struct btrfs_ioctl_search_args_v2 __user *uarg;struct btrfs_ioctl_search_args_v2 args; -struct inode *inode;int ret;size_t buf_size;const size_t buf_limit =SZ_16M ;
@@ -2332,7 +2328,6 @@ static noinline int btrfs_ioctl_tree_search_v2(struct file *file,if (buf_size > buf_limit) buf_size = buf_limit; - inode =file_inode (file); ret =search_ioctl (inode, &args.key, &buf_size, (char __user *)(&uarg->buf[0 ]));if (ret ==0 &©_to_user (&uarg->key, &args.key,sizeof (args.key)))
@@ -2666,7 +2661,7 @@ static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp) }/* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */ -static int btrfs_ioctl_get_subvol_info (struct file *file,void __user *argp) +static int btrfs_ioctl_get_subvol_info (struct inode *inode,void __user *argp) {struct btrfs_ioctl_get_subvol_info_args *subvol_info;struct btrfs_fs_info *fs_info;
@@ -2678,7 +2673,6 @@ static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)struct extent_buffer *leaf;unsigned long item_off;unsigned long item_len; -struct inode *inode;int slot;int ret =0 ;
@@ -2692,7 +2686,6 @@ static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)return -ENOMEM ; } - inode =file_inode (file); fs_info =BTRFS_I (inode)->root->fs_info;/* Get root_item of inode's subvolume */
@@ -2786,12 +2779,11 @@ static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)* Return ROOT_REF information of the subvolume containing this inode * except the subvolume name. */ -static int btrfs_ioctl_get_subvol_rootref (struct btrfs_root *subvol_root, +static int btrfs_ioctl_get_subvol_rootref (struct btrfs_root *root,void __user *argp) {struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;struct btrfs_root_ref *rref; -struct btrfs_root *root;struct btrfs_path *path;struct btrfs_key key;struct extent_buffer *leaf;
@@ -2810,14 +2802,13 @@ static int btrfs_ioctl_get_subvol_rootref(struct btrfs_root *subvol_root,return PTR_ERR (rootrefs); } - root = subvol_root->fs_info->tree_root; - objectid = subvol_root->root_key.objectid; - + objectid = root->root_key.objectid; key.objectid = objectid; key.type =BTRFS_ROOT_REF_KEY ; key.offset = rootrefs->min_treeid; found =0 ; + root = root->fs_info->tree_root; ret =btrfs_search_slot (NULL , root, &key, path,0 ,0 );if (ret <0 ) { goto out;
@@ -4859,7 +4850,9 @@ static int btrfs_ioctl_set_features(struct file *file, void __user *arg)return ret; } -static int _btrfs_ioctl_send (struct file *file,void __user *argp,bool compat) +static int _btrfs_ioctl_send (struct inode *inode, +void __user *argp, +bool compat) {struct btrfs_ioctl_send_args *arg;int ret;
@@ -4889,7 +4882,7 @@ static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)if (IS_ERR (arg))return PTR_ERR (arg); } - ret =btrfs_ioctl_send (file, arg); + ret =btrfs_ioctl_send (inode, arg);kfree (arg);return ret; }
@@ -4924,7 +4917,7 @@ long btrfs_ioctl(struct file *file, unsigned intcase BTRFS_IOC_SNAP_DESTROY_V2 :return btrfs_ioctl_snap_destroy (file, argp, true);case BTRFS_IOC_SUBVOL_GETFLAGS : -return btrfs_ioctl_subvol_getflags (file, argp); +return btrfs_ioctl_subvol_getflags (inode, argp);case BTRFS_IOC_SUBVOL_SETFLAGS :return btrfs_ioctl_subvol_setflags (file, argp);case BTRFS_IOC_DEFAULT_SUBVOL :
@@ -4948,9 +4941,9 @@ long btrfs_ioctl(struct file *file, unsigned intcase BTRFS_IOC_BALANCE :return btrfs_ioctl_balance (file,NULL );case BTRFS_IOC_TREE_SEARCH : -return btrfs_ioctl_tree_search (file, argp); +return btrfs_ioctl_tree_search (inode, argp);case BTRFS_IOC_TREE_SEARCH_V2 : -return btrfs_ioctl_tree_search_v2 (file, argp); +return btrfs_ioctl_tree_search_v2 (inode, argp);case BTRFS_IOC_INO_LOOKUP :return btrfs_ioctl_ino_lookup (root, argp);case BTRFS_IOC_INO_PATHS :
@@ -4999,10 +4992,10 @@ long btrfs_ioctl(struct file *file, unsigned intreturn btrfs_ioctl_set_received_subvol_32 (file, argp);#endif case BTRFS_IOC_SEND : -return _btrfs_ioctl_send (file, argp, false); +return _btrfs_ioctl_send (inode, argp, false);#if defined(CONFIG_64BIT ) && defined(CONFIG_COMPAT )case BTRFS_IOC_SEND_32 : -return _btrfs_ioctl_send (file, argp, true); +return _btrfs_ioctl_send (inode, argp, true);#endif case BTRFS_IOC_GET_DEV_STATS :return btrfs_ioctl_get_dev_stats (fs_info, argp);
@@ -5029,7 +5022,7 @@ long btrfs_ioctl(struct file *file, unsigned intcase BTRFS_IOC_SET_FEATURES :return btrfs_ioctl_set_features (file, argp);case BTRFS_IOC_GET_SUBVOL_INFO : -return btrfs_ioctl_get_subvol_info (file, argp); +return btrfs_ioctl_get_subvol_info (inode, argp);case BTRFS_IOC_GET_SUBVOL_ROOTREF :return btrfs_ioctl_get_subvol_rootref (root, argp);case BTRFS_IOC_INO_LOOKUP_USER :
fs/btrfs/send.c +2 -2
@@ -7473,10 +7473,10 @@ static void dedupe_in_progress_warn(const struct btrfs_root *root) root->root_key.objectid, root->dedupe_in_progress); } -long btrfs_ioctl_send (struct file *mnt_file,struct btrfs_ioctl_send_args *arg) +long btrfs_ioctl_send (struct inode *inode,struct btrfs_ioctl_send_args *arg) {int ret =0 ; -struct btrfs_root *send_root =BTRFS_I (file_inode (mnt_file))->root; +struct btrfs_root *send_root =BTRFS_I (inode)->root;struct btrfs_fs_info *fs_info = send_root->fs_info;struct btrfs_root *clone_root;struct send_ctx *sctx =NULL ;
fs/btrfs/send.h +1 -1
@@ -126,7 +126,7 @@ enum {#define BTRFS_SEND_A_MAX (__BTRFS_SEND_A_MAX - 1)#ifdef __KERNEL__ -long btrfs_ioctl_send (struct file *mnt_file,struct btrfs_ioctl_send_args *arg); +long btrfs_ioctl_send (struct inode *inode,struct btrfs_ioctl_send_args *arg);#endif #endif
Signature · 1 line
Sahil
Re: [PATCH 1/1] btrfs: reuse existing inode from btrfs_ioctl
Quoted 4 lines · Sahil Kang
btrfs_ioctlextracts inode from file so we can pass that into the subfunctions.Signed-off-by Sahil Kang <[email protected]>
Added to devel, thanks.
fs/btrfs/ioctl.c static int btrfs_ioctl_set_features(struct file *file, void __user *arg) · line 4855
Quoted 4 lines
fs/btrfs/ioctl.c +3
-1-static int _btrfs_ioctl_send (struct file *file,void __user *argp,bool compat) +static int _btrfs_ioctl_send (struct inode *inode, +void __user *argp, +bool compat)
Please keep the lines filled and don't split when not necessary. Fixed in the committed patch.
Quoted 5 lines · Sahil Kang
btrfs_ioctlalready contains pointers to the inode andbtrfs_rootstructs, so we can pass them into the subfunctions instead of the toplevel struct file.Signed-off-by Sahil Kang <[email protected]>
Added to misc-next, thanks.
Quoted 2 lines · Sahil Kang
This is a small cleanup that reuses some of the existing pointers and removes the duplicated dereferencing.
Some quick glance indeed shows that we have pretty inconsistent arguments for a lot of ioctls, mostly switching between file/inode.
So the cleanup looks good to me.
But I also find that, there are ioctl sub-routines that requires file as it needs to call mnt_want_write_file().
I know I'm asking for too much, but maybe it's a good idea to separate those ioctl sub-routines into two types:
- Those write ioctls which need @file to do mnt_want_write_file() check So they can use the existing @file argument as usual
- Those read-only ioctls which don't need @file at all
Then pass @inode/@root to those functions.
(Personally speaking I prefer to pass @inode and let sub-routines to
handle the root grabbing)
Anyway, your patch looks like a good initial step.
Will give more detailed review on the patch.
Thanks,
Qu
Quoted 16 lines
There are other subfunctions that have similar/identical dereferencing as the enclosing
btrfs_ioctl, but I'm not sure if changing those is welcomed since it would require changing their arg count. Right now, all of the sub ioctl functions have essentially the same signature.For example, reusing the pointers for
btrfs_ioctl_set_fslabelwould require passing in*fs_infoand *root, in addition to its current args.Sahil Kang (1):
btrfs: reuse existing pointers frombtrfs_ioctlfs/btrfs/ioctl.c | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-)
Quoted 10 lines · Qu Wenruo
Quoted 2 lines · Sahil Kang
This is a small cleanup that reuses some of the existing pointers and removes the duplicated dereferencing.
Some quick glance indeed shows that we have pretty inconsistent arguments for a lot of ioctls, mostly switching between file/inode.
So the cleanup looks good to me.
Agreed.
Quoted 2 lines
But I also find that, there are ioctl sub-routines that requires file as it needs to call
mnt_want_write_file().
I've noticed there are some calls to file_mnt_user_ns that use the file.
Quoted 10 lines
I know I'm asking for too much, but maybe it's a good idea to separate those ioctl sub-routines into two types:
- Those write ioctls which need @file to do
mnt_want_write_file()check So they can use the existing @file argument as usual- Those read-only ioctls which don't need @file at all
Then pass @inode/@root to those functions.
(Personally speaking I prefer to pass @inode and let sub-routines to
handle the root grabbing)
The main switch function btrfs_ioctl extracts all the pointers so I'd suggest to pass only the one that the ioctl callbacks need, ie. not an inode if it's just for the root pointer.