btrfs: inode creation cleanups and fixes
v3 cover btrfs: inode creation cleanups and fixes
From: Omar Sandoval <[email protected]>
This series contains minor updates of the final four patches of my previous series [1].
Based on misc-next with the previous version of "btrfs: allocate inode outside of btrfs_new_inode()" removed.
Changes since v2:
- Mentioned reason for removal of btrfs_lookup_dentry() call in create_subvol() in commit message of patch 1.
- Made btrfs_init_inode_security() also take btrfs_new_inode_args in patch 2.
Thanks!
1: https://lore.kernel.org/linux-btrfs/[email protected]/
Omar Sandoval (4):
btrfs: allocate inode outside of btrfs_new_inode()
btrfs: factor out common part of btrfs_{mknod,create,mkdir}()
btrfs: reserve correct number of items for inode creation
btrfs: move common inode creation code into btrfs_create_new_inode()
fs/btrfs/acl.c | 36 +-- fs/btrfs/ctree.h | 37 ++- fs/btrfs/inode.c | 800 +++++++++++++++++++++++------------------------ fs/btrfs/ioctl.c | 140 +++++---- fs/btrfs/props.c | 40 +-- fs/btrfs/props.h | 4 - 6 files changed, 489 insertions(+), 568 deletions(-)
Signature · 1 line
2.35.1
PATCH v3 2/4 btrfs: factor out common part of btrfs_{mknod,create,mkdir}()
From: Omar Sandoval <[email protected]>
btrfs_{mknod,create,mkdir}() are now identical other than the inode initialization and some inconsequential function call order differences. Factor out the common code to reduce code duplication.
--- fs/btrfs/inode.c | 152 ++++++++++------------------------------------- 1 file changed, 33 insertions(+), 119 deletions(-)
fs/btrfs/inode.c +33 -119
@@ -6346,82 +6346,15 @@ int btrfs_add_link(struct btrfs_trans_handle *trans,return ret; } -static int btrfs_mknod (struct user_namespace *mnt_userns,struct inode *dir, -struct dentry *dentry,umode_t mode,dev_t rdev) +static int btrfs_create_common (struct inode *dir,struct dentry *dentry, +struct inode *inode) {struct btrfs_fs_info *fs_info =btrfs_sb (dir->i_sb); -struct btrfs_trans_handle *trans;struct btrfs_root *root =BTRFS_I (dir)->root; -struct inode *inode; +struct btrfs_trans_handle *trans;int err;u64 index =0 ; - inode =new_inode (dir->i_sb); -if (!inode) -return -ENOMEM ; -inode_init_owner (mnt_userns, inode, dir, mode); - inode->i_op = &btrfs_special_inode_operations; -init_special_inode (inode, inode->i_mode, rdev); - -/* -* 2 for inode item and ref -* 2 for dir items -* 1 for xattr if selinux is on -*/ - trans =btrfs_start_transaction (root,5 ); -if (IS_ERR (trans)) { -iput (inode); -return PTR_ERR (trans); - } - - err =btrfs_new_inode (trans, root, inode, dir, dentry->d_name.name, - dentry->d_name.len, &index); -if (err) { -iput (inode); - inode =NULL ; - goto out_unlock; - } - - err =btrfs_init_inode_security (trans, inode, dir, &dentry->d_name); -if (err) - goto out_unlock; - - err =btrfs_add_link (trans,BTRFS_I (dir),BTRFS_I (inode), - dentry->d_name.name, dentry->d_name.len,0 , index); -if (err) - goto out_unlock; - -btrfs_update_inode (trans, root,BTRFS_I (inode)); -d_instantiate_new (dentry, inode); - -out_unlock: -btrfs_end_transaction (trans); -btrfs_btree_balance_dirty (fs_info); -if (err && inode) { -inode_dec_link_count (inode); -discard_new_inode (inode); - } -return err; -} - -static int btrfs_create (struct user_namespace *mnt_userns,struct inode *dir, -struct dentry *dentry,umode_t mode,bool excl) -{ -struct btrfs_fs_info *fs_info =btrfs_sb (dir->i_sb); -struct btrfs_trans_handle *trans; -struct btrfs_root *root =BTRFS_I (dir)->root; -struct inode *inode; -int err; -u64 index =0 ; - - inode =new_inode (dir->i_sb); -if (!inode) -return -ENOMEM ; -inode_init_owner (mnt_userns, inode, dir, mode); - inode->i_fop = &btrfs_file_operations; - inode->i_op = &btrfs_file_inode_operations; - inode->i_mapping->a_ops = &btrfs_aops; -/* * 2 for inode item and ref * 2 for dir items
@@ -6466,6 +6399,35 @@ static int btrfs_create(struct user_namespace *mnt_userns, struct inode *dir,return err; } +static int btrfs_mknod (struct user_namespace *mnt_userns,struct inode *dir, +struct dentry *dentry,umode_t mode,dev_t rdev) +{ +struct inode *inode; + + inode =new_inode (dir->i_sb); +if (!inode) +return -ENOMEM ; +inode_init_owner (mnt_userns, inode, dir, mode); + inode->i_op = &btrfs_special_inode_operations; +init_special_inode (inode, inode->i_mode, rdev); +return btrfs_create_common (dir, dentry, inode); +} + +static int btrfs_create (struct user_namespace *mnt_userns,struct inode *dir, +struct dentry *dentry,umode_t mode,bool excl) +{ +struct inode *inode; + + inode =new_inode (dir->i_sb); +if (!inode) +return -ENOMEM ; +inode_init_owner (mnt_userns, inode, dir, mode); + inode->i_fop = &btrfs_file_operations; + inode->i_op = &btrfs_file_inode_operations; + inode->i_mapping->a_ops = &btrfs_aops; +return btrfs_create_common (dir, dentry, inode); +} +static int btrfs_link (struct dentry *old_dentry,struct inode *dir,struct dentry *dentry) {
@@ -6547,12 +6509,7 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,static int btrfs_mkdir (struct user_namespace *mnt_userns,struct inode *dir,struct dentry *dentry,umode_t mode) { -struct btrfs_fs_info *fs_info =btrfs_sb (dir->i_sb);struct inode *inode; -struct btrfs_trans_handle *trans; -struct btrfs_root *root =BTRFS_I (dir)->root; -int err; -u64 index =0 ; inode =new_inode (dir->i_sb);if (!inode)
@@ -6560,50 +6517,7 @@ static int btrfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,inode_init_owner (mnt_userns, inode, dir,S_IFDIR | mode); inode->i_op = &btrfs_dir_inode_operations; inode->i_fop = &btrfs_dir_file_operations; - -/* -* 2 items for inode and ref -* 2 items for dir items -* 1 for xattr if selinux is on -*/ - trans =btrfs_start_transaction (root,5 ); -if (IS_ERR (trans)) { -iput (inode); -return PTR_ERR (trans); - } - - err =btrfs_new_inode (trans, root, inode, dir, dentry->d_name.name, - dentry->d_name.len, &index); -if (err) { -iput (inode); - inode =NULL ; - goto out_fail; - } - - err =btrfs_init_inode_security (trans, inode, dir, &dentry->d_name); -if (err) - goto out_fail; - - err =btrfs_update_inode (trans, root,BTRFS_I (inode)); -if (err) - goto out_fail; - - err =btrfs_add_link (trans,BTRFS_I (dir),BTRFS_I (inode), - dentry->d_name.name, - dentry->d_name.len,0 , index); -if (err) - goto out_fail; - -d_instantiate_new (dentry, inode); - -out_fail: -btrfs_end_transaction (trans); -if (err && inode) { -inode_dec_link_count (inode); -discard_new_inode (inode); - } -btrfs_btree_balance_dirty (fs_info); -return err; +return btrfs_create_common (dir, dentry, inode); } static noinline int uncompress_inline(struct btrfs_path *path,
Signature · 1 line
2.35.1
PATCH v3 1/4 btrfs: allocate inode outside of btrfs_new_inode()
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
Instead of calling new_inode() and inode_init_owner() inside of btrfs_new_inode(), do it in the callers. This allows us to pass in just the inode instead of the mnt_userns and mode and removes the need for memalloc_nofs_{save,restores}() since we do it before starting a transaction. In create_subvol(), it also means we no longer have to look up the inode again to instantiate it. This also paves the way for some more cleanups in later patches.
This also removes the comments about Smack checking i_op, which are no longer true since commit 5d6c31910bc0 ("xattr: Add __vfs_{get,set,remove}xattr helpers"). Now it checks inode->i_opflags & IOP_XATTR, which is set based on sb->s_xattr.
--- fs/btrfs/ctree.h | 5 +- fs/btrfs/inode.c | 284 +++++++++++++++++++++++++---------------------- fs/btrfs/ioctl.c | 22 ++-- 3 files changed, 167 insertions(+), 144 deletions(-)
fs/btrfs/ctree.h +3 -2
@@ -3254,10 +3254,11 @@ int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,int btrfs_set_extent_delalloc (struct btrfs_inode *inode,u64 start,u64 end,unsigned int extra_bits,struct extent_state **cached_state); +struct inode *btrfs_new_subvol_inode (struct user_namespace *mnt_userns, +struct inode *dir);int btrfs_create_subvol_root (struct btrfs_trans_handle *trans, -struct btrfs_root *new_root,struct btrfs_root *parent_root, -struct user_namespace *mnt_userns); +struct inode *inode);void btrfs_set_delalloc_extent (struct inode *inode,struct extent_state *state,unsigned *bits); void btrfs_clear_delalloc_extent(struct inode *inode,
fs/btrfs/inode.c +150 -134
519 lines not on this page · raw message
fs/btrfs/ioctl.c +14 -8
@@ -587,6 +587,12 @@ static noinline int create_subvol(struct user_namespace *mnt_userns,if (ret <0 ) goto out_root_item; + inode =btrfs_new_subvol_inode (mnt_userns, dir); +if (!inode) { + ret = -ENOMEM ; + goto out_anon_dev; + } +btrfs_init_block_rsv (&block_rsv,BTRFS_BLOCK_RSV_TEMP );/* * The same as the snapshot creation, please see the comment
@@ -594,13 +600,13 @@ static noinline int create_subvol(struct user_namespace *mnt_userns,*/ ret =btrfs_subvolume_reserve_metadata (root, &block_rsv,8 , false);if (ret) - goto out_anon_dev; + goto out_inode; trans =btrfs_start_transaction (root,0 );if (IS_ERR (trans)) { ret =PTR_ERR (trans);btrfs_subvolume_release_metadata (root, &block_rsv); - goto out_anon_dev; + goto out_inode; } trans->block_rsv = &block_rsv; trans->bytes_reserved = block_rsv.size;
@@ -683,16 +689,16 @@ static noinline int create_subvol(struct user_namespace *mnt_userns, }/* anon_dev is owned by new_root now. */ anon_dev =0 ; +BTRFS_I (inode)->root = new_root; +/* ... and new_root is owned by inode now. */ ret =btrfs_record_root_in_trans (trans, new_root);if (ret) { -btrfs_put_root (new_root);btrfs_abort_transaction (trans, ret); goto out; } - ret =btrfs_create_subvol_root (trans, new_root, root, mnt_userns); -btrfs_put_root (new_root); + ret =btrfs_create_subvol_root (trans, root, inode);if (ret) {/* We potentially lose an unused inode item here */ btrfs_abort_transaction (trans, ret);
@@ -745,11 +751,11 @@ static noinline int create_subvol(struct user_namespace *mnt_userns, ret =btrfs_commit_transaction (trans);if (!ret) { - inode =btrfs_lookup_dentry (dir, dentry); -if (IS_ERR (inode)) -return PTR_ERR (inode);d_instantiate (dentry, inode); + inode =NULL ; } +out_inode: +iput (inode); out_anon_dev:if (anon_dev)free_anon_bdev (anon_dev);
Signature · 1 line
2.35.1
PATCH v3 3/4 btrfs: reserve correct number of items for inode creation
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
The various inode creation code paths do not account for the compression property, POSIX ACLs, or the parent inode item when starting a transaction. Fix it by refactoring all of these code paths to use a new function, btrfs_new_inode_prepare(), which computes the correct number of items. To do so, it needs to know whether POSIX ACLs will be created, so move the ACL creation into that function. To reduce the number of arguments that need to be passed around for inode creation, define struct btrfs_new_inode_args containing all of the relevant information.
btrfs_new_inode_prepare() will also be a good place to set up the fscrypt context and encrypted filename in the future.
--- fs/btrfs/acl.c | 36 +------ fs/btrfs/ctree.h | 34 +++++-- fs/btrfs/inode.c | 250 ++++++++++++++++++++++++++++++++++------------- fs/btrfs/ioctl.c | 83 +++++++++++----- 4 files changed, 270 insertions(+), 133 deletions(-)
fs/btrfs/acl.c +2 -34
@@ -55,8 +55,8 @@ struct posix_acl *btrfs_get_acl(struct inode *inode, int type, bool rcu)return acl; } -static int __btrfs_set_acl (struct btrfs_trans_handle *trans, -struct inode *inode,struct posix_acl *acl,int type) +int __btrfs_set_acl (struct btrfs_trans_handle *trans,struct inode *inode, +struct posix_acl *acl,int type) {int ret, size =0 ;const char *name;
@@ -127,35 +127,3 @@ int btrfs_set_acl(struct user_namespace *mnt_userns, struct inode *inode, inode->i_mode = old_mode;return ret; } - -int btrfs_init_acl (struct btrfs_trans_handle *trans, -struct inode *inode,struct inode *dir) -{ -struct posix_acl *default_acl, *acl; -int ret =0 ; - -/* this happens with subvols */ -if (!dir) -return 0 ; - - ret =posix_acl_create (dir, &inode->i_mode, &default_acl, &acl); -if (ret) -return ret; - -if (default_acl) { - ret =__btrfs_set_acl (trans, inode, default_acl, -ACL_TYPE_DEFAULT ); -posix_acl_release (default_acl); - } - -if (acl) { -if (!ret) - ret =__btrfs_set_acl (trans, inode, acl, -ACL_TYPE_ACCESS ); -posix_acl_release (acl); - } - -if (!default_acl && !acl) -cache_no_acl (inode); -return ret; -}
fs/btrfs/ctree.h +28 -6
@@ -3254,11 +3254,32 @@ int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,int btrfs_set_extent_delalloc (struct btrfs_inode *inode,u64 start,u64 end,unsigned int extra_bits,struct extent_state **cached_state); +struct btrfs_new_inode_args { +/* Input */ +struct inode *dir; +struct dentry *dentry; +struct inode *inode; +bool orphan; +bool subvol; + +/* +* Output from btrfs_new_inode_prepare(), input to +* btrfs_create_new_inode(). +*/ +struct posix_acl *default_acl; +struct posix_acl *acl; +}; +int btrfs_new_inode_prepare (struct btrfs_new_inode_args *args, +unsigned int *trans_num_items); +int btrfs_create_new_inode (struct btrfs_trans_handle *trans, +struct btrfs_new_inode_args *args, +u64 *index); +void btrfs_new_inode_args_destroy (struct btrfs_new_inode_args *args);struct inode *btrfs_new_subvol_inode (struct user_namespace *mnt_userns,struct inode *dir);int btrfs_create_subvol_root (struct btrfs_trans_handle *trans,struct btrfs_root *parent_root, -struct inode *inode); +struct btrfs_new_inode_args *args);void btrfs_set_delalloc_extent (struct inode *inode,struct extent_state *state,unsigned *bits); void btrfs_clear_delalloc_extent(struct inode *inode,
@@ -3816,15 +3837,16 @@ static inline int __btrfs_fs_compat_ro(struct btrfs_fs_info *fs_info, u64 flag)struct posix_acl *btrfs_get_acl (struct inode *inode,int type,bool rcu);int btrfs_set_acl (struct user_namespace *mnt_userns,struct inode *inode,struct posix_acl *acl,int type); -int btrfs_init_acl (struct btrfs_trans_handle *trans, -struct inode *inode,struct inode *dir); +int __btrfs_set_acl (struct btrfs_trans_handle *trans,struct inode *inode, +struct posix_acl *acl,int type);#else #define btrfs_get_acl NULL#define btrfs_set_acl NULL -static inline int btrfs_init_acl (struct btrfs_trans_handle *trans, -struct inode *inode,struct inode *dir) +static inline int __btrfs_set_acl (struct btrfs_trans_handle *trans, +struct inode *inode,struct posix_acl *acl, +int type) { -return 0 ; +return -EOPNOTSUPP ; }#endif
fs/btrfs/inode.c +182 -68
453 lines not on this page · raw message
fs/btrfs/ioctl.c +58 -25
145 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH v3 4/4 btrfs: move common inode creation code into btrfs_create_new_inode()
In reply to Omar SandovalFrom: Omar Sandoval <[email protected]>
All of our inode creation code paths duplicate the calls to btrfs_init_inode_security() and btrfs_add_link(). Subvolume creation additionally duplicates property inheritance and the call to btrfs_set_inode_index(). Fix this by moving the common code into btrfs_create_new_inode(). This accomplishes a few things at once:
1. It reduces code duplication.
2. It allows us to set up the inode completely before inserting the inode item, removing calls to btrfs_update_inode().
3. It fixes a leak of an inode on disk in some error cases. For example, in btrfs_create(), if btrfs_new_inode() succeeds, then we have inserted an inode item and its inode ref. However, if something after that fails (e.g., btrfs_init_inode_security()), then we end the transaction and then decrement the link count on the inode. If the transaction is committed and the system crashes before the failed inode is deleted, then we leak that inode on disk. Instead, this refactoring aborts the transaction when we can't recover more gracefully.
4. It exposes various ways that subvolume creation diverges from mkdir in terms of inheriting flags, properties, permissions, and POSIX ACLs, a lot of which appears to be accidental. This patch explicitly does _not_ change the existing non-standard behavior, but it makes those differences more clear in the code and documents them so that we can discuss whether they should be changed.
--- fs/btrfs/ctree.h | 6 +- fs/btrfs/inode.c | 404 +++++++++++++++++++---------------------------- fs/btrfs/ioctl.c | 59 ++----- fs/btrfs/props.c | 40 +---- fs/btrfs/props.h | 4 - 5 files changed, 180 insertions(+), 333 deletions(-)
fs/btrfs/ctree.h +1 -5
@@ -3272,14 +3272,10 @@ struct btrfs_new_inode_args {int btrfs_new_inode_prepare (struct btrfs_new_inode_args *args,unsigned int *trans_num_items);int btrfs_create_new_inode (struct btrfs_trans_handle *trans, -struct btrfs_new_inode_args *args, -u64 *index); +struct btrfs_new_inode_args *args);void btrfs_new_inode_args_destroy (struct btrfs_new_inode_args *args);struct inode *btrfs_new_subvol_inode (struct user_namespace *mnt_userns,struct inode *dir); -int btrfs_create_subvol_root (struct btrfs_trans_handle *trans, -struct btrfs_root *parent_root, -struct btrfs_new_inode_args *args);void btrfs_set_delalloc_extent (struct inode *inode,struct extent_state *state,unsigned *bits); void btrfs_clear_delalloc_extent(struct inode *inode,
fs/btrfs/inode.c +165 -239
619 lines not on this page · raw message
fs/btrfs/ioctl.c +12 -47
92 lines not on this page · raw message
fs/btrfs/props.c +2 -38
@@ -334,9 +334,8 @@ static struct prop_handler prop_handlers[] = { }, }; -static int inherit_props (struct btrfs_trans_handle *trans, -struct inode *inode, -struct inode *parent) +int btrfs_inode_inherit_props (struct btrfs_trans_handle *trans, +struct inode *inode,struct inode *parent) {struct btrfs_root *root =BTRFS_I (inode)->root;struct btrfs_fs_info *fs_info = root->fs_info;
@@ -408,41 +407,6 @@ static int inherit_props(struct btrfs_trans_handle *trans,return 0 ; } -int btrfs_inode_inherit_props (struct btrfs_trans_handle *trans, -struct inode *inode, -struct inode *dir) -{ -if (!dir) -return 0 ; - -return inherit_props (trans, inode, dir); -} - -int btrfs_subvol_inherit_props (struct btrfs_trans_handle *trans, -struct btrfs_root *root, -struct btrfs_root *parent_root) -{ -struct super_block *sb = root->fs_info->sb; -struct inode *parent_inode, *child_inode; -int ret; - - parent_inode =btrfs_iget (sb,BTRFS_FIRST_FREE_OBJECTID , parent_root); -if (IS_ERR (parent_inode)) -return PTR_ERR (parent_inode); - - child_inode =btrfs_iget (sb,BTRFS_FIRST_FREE_OBJECTID , root); -if (IS_ERR (child_inode)) { -iput (parent_inode); -return PTR_ERR (child_inode); - } - - ret =inherit_props (trans, child_inode, parent_inode); -iput (child_inode); -iput (parent_inode); - -return ret; -} -void __init btrfs_props_init (void ) {int i;
fs/btrfs/props.h +0 -4
@@ -21,8 +21,4 @@ int btrfs_inode_inherit_props(struct btrfs_trans_handle *trans,struct inode *inode, struct inode *dir); -int btrfs_subvol_inherit_props (struct btrfs_trans_handle *trans, -struct btrfs_root *root, -struct btrfs_root *parent_root); -#endif
Signature · 1 line
2.35.1