Btrfs: fix our reservations for updating an inode when completing io V3
PATCH 1/1 Btrfs: fix our reservations for updating an inode when completing io V3
People have been reporting ENOSPC crashes in finish_ordered_io. This is because we try to steal from the delalloc block rsv to satisfy a reservation to update the inode. The problem with this is we don't explicitly save space for updating the inode when doing delalloc. This is kind of a problem and we've gotten away with this because way back when we just stole from the delalloc reserve without any questions, and this worked out fine because generally speaking the leaf had been modified either by the mtime update when we did the original write or because we just updated the leaf when we inserted the file extent item, only on rare occasions had the leaf not actually been modified, and that was still ok because we'd just use a block or two out of the over-reservation that is delalloc.
Then came the delayed inode stuff. This is amazing, except it wants a full reservation for updating the inode since it may do it at some point down the road after we've written the blocks and we have to recow everything again. This worked out because the delayed inode stuff just stole from the global reserve, that is until recently when I changed that because it caused other problems.
So here we are, we're doing everything right and being screwed for it. So take an extra reservation for the inode at delalloc reservation time and carry it through the life of the delalloc reservation. If we need it we can steal it in the delayed inode stuff. If we have already stolen it try and do a normal metadata reservation. If that fails try to steal from the delalloc reservation. If _that_ fails we'll get a WARN_ON() so I can start thinking of a better way to solve this and in the meantime we'll steal from the global reserve.
If we fail all of this, then we'll fall back to just updating the inode instead of delaying it, since we shouldn't use that much space as we've just modified the tree by inserting the file extent item.
With this patch I ran xfstests 13 in a loop for a couple of hours and didn't see any problems. Thanks,
---V2->V3: lxo hit one of my "we should never hit this" BUG()'s with V2. It is possible we could have stolen all of our extra reservations, so add an option to btrfs_update_inode to fall back to the direct update of the inode, so in the endio case we can make sure we update the inode and don't get ENOSPC.
fs/btrfs/btrfs_inode.h | 4 +-- fs/btrfs/ctree.h | 4 +- fs/btrfs/delayed-inode.c | 58 ++++++++++++++++++++++++++++++++++++++++++- fs/btrfs/extent-tree.c | 24 +++++++++++++++--- fs/btrfs/free-space-cache.c | 4 +- fs/btrfs/inode-map.c | 2 +- fs/btrfs/inode.c | 47 +++++++++++++++++++--------------- fs/btrfs/ioctl.c | 6 ++-- fs/btrfs/transaction.c | 2 +- fs/btrfs/tree-log.c | 8 +++--- fs/btrfs/xattr.c | 2 +- 11 files changed, 118 insertions(+), 43 deletions(-)
fs/btrfs/btrfs_inode.h +1 -3
@@ -147,14 +147,12 @@ struct btrfs_inode {* the btrfs file release call will add this inode to the * ordered operations list so that we make sure to flush out any * new data the application may have written before commit. -* -* yes, its silly to have a single bitflag, but we might grow more -* of these. */ unsigned ordered_data_close:1; unsigned orphan_meta_reserved:1; unsigned dummy_inode:1; unsigned in_defrag:1; + unsigned delalloc_meta_reserved:1;/* * always compress this one file
fs/btrfs/ctree.h +2 -2
@@ -2699,8 +2699,8 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, size_t pg_offset, u64 start, u64 end, int create);int btrfs_update_inode (struct btrfs_trans_handle *trans, -struct btrfs_root *root, -struct inode *inode); +struct btrfs_root *root,struct inode *inode, +int fallback);int btrfs_orphan_add (struct btrfs_trans_handle *trans,struct inode *inode);int btrfs_orphan_del (struct btrfs_trans_handle *trans,struct inode *inode);int btrfs_orphan_cleanup (struct btrfs_root *root);
fs/btrfs/delayed-inode.c +57 -1
88 lines not on this page · raw message
fs/btrfs/extent-tree.c +20 -4
@@ -2731,7 +2731,7 @@ again:* time. */ BTRFS_I (inode)->generation =0 ; - ret =btrfs_update_inode (trans, root, inode); + ret =btrfs_update_inode (trans, root, inode,0 );WARN_ON (ret);if (i_size_read (inode) >0 ) {
@@ -4063,23 +4063,30 @@ int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,*/ static unsigned drop_outstanding_extent (struct inode *inode) { +unsigned drop_inode_space =0 ;unsigned dropped_extents =0 ;BUG_ON (!BTRFS_I (inode)->outstanding_extents);BTRFS_I (inode)->outstanding_extents--; +if (BTRFS_I (inode)->outstanding_extents ==0 && +BTRFS_I (inode)->delalloc_meta_reserved) { + drop_inode_space =1 ; +BTRFS_I (inode)->delalloc_meta_reserved =0 ; + } +/* * If we have more or the same amount of outsanding extents than we have * reserved then we need to leave the reserved extents count alone. */ if (BTRFS_I (inode)->outstanding_extents >=BTRFS_I (inode)->reserved_extents) -return 0 ; +return drop_inode_space; dropped_extents =BTRFS_I (inode)->reserved_extents -BTRFS_I (inode)->outstanding_extents;BTRFS_I (inode)->reserved_extents -= dropped_extents; -return dropped_extents; +return dropped_extents + drop_inode_space; }/**
@@ -4165,9 +4172,18 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) nr_extents =BTRFS_I (inode)->outstanding_extents -BTRFS_I (inode)->reserved_extents;BTRFS_I (inode)->reserved_extents += nr_extents; + } - to_reserve =btrfs_calc_trans_metadata_size (root, nr_extents); +/* +* Add an item to reserve for updating the inode when we complete the +* delalloc io. +*/ +if (!BTRFS_I (inode)->delalloc_meta_reserved) { + nr_extents++; +BTRFS_I (inode)->delalloc_meta_reserved =1 ; } + + to_reserve =btrfs_calc_trans_metadata_size (root, nr_extents); to_reserve +=calc_csum_metadata_size (inode, num_bytes,1 );spin_unlock (&BTRFS_I (inode)->lock);
fs/btrfs/free-space-cache.c +2 -2
@@ -234,7 +234,7 @@ int btrfs_truncate_free_space_cache(struct btrfs_root *root,return ret; } - ret =btrfs_update_inode (trans, root, inode); + ret =btrfs_update_inode (trans, root, inode,0 ); trans->block_rsv = rsv;return ret;
@@ -1008,7 +1008,7 @@ out:invalidate_inode_pages2 (inode->i_mapping);BTRFS_I (inode)->generation =0 ; } -btrfs_update_inode (trans, root, inode); +btrfs_update_inode (trans, root, inode,0 );return err; out_nospc:
fs/btrfs/inode-map.c +1 -1
@@ -439,7 +439,7 @@ again: }BTRFS_I (inode)->generation =0 ; - ret =btrfs_update_inode (trans, root, inode); + ret =btrfs_update_inode (trans, root, inode,0 );WARN_ON (ret);if (i_size_read (inode) >0 ) {
fs/btrfs/inode.c +26 -21
188 lines not on this page · raw message
fs/btrfs/ioctl.c +3 -3
@@ -252,7 +252,7 @@ static int btrfs_ioctl_setflags(struct file *file, void __user *arg) trans =btrfs_join_transaction (root);BUG_ON (IS_ERR (trans)); - ret =btrfs_update_inode (trans, root, inode); + ret =btrfs_update_inode (trans, root, inode,0 );BUG_ON (ret);btrfs_update_iflags (inode);
@@ -434,7 +434,7 @@ static noinline int create_subvol(struct btrfs_root *root, goto fail;btrfs_i_size_write (dir, dir->i_size + namelen *2 ); - ret =btrfs_update_inode (trans, root, dir); + ret =btrfs_update_inode (trans, root, dir,0 );BUG_ON (ret); ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
@@ -2488,7 +2488,7 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,if (endoff > inode->i_size)btrfs_i_size_write (inode, endoff); - ret =btrfs_update_inode (trans, root, inode); + ret =btrfs_update_inode (trans, root, inode,0 );BUG_ON (ret);btrfs_end_transaction (trans, root); }
fs/btrfs/transaction.c +1 -1
@@ -915,7 +915,7 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,btrfs_i_size_write (parent_inode, parent_inode->i_size + dentry->d_name.len *2 ); - ret =btrfs_update_inode (trans, parent_root, parent_inode); + ret =btrfs_update_inode (trans, parent_root, parent_inode,0 );BUG_ON (ret);/*
fs/btrfs/tree-log.c +4 -4
@@ -639,7 +639,7 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans, }inode_set_bytes (inode, saved_nbytes); -btrfs_update_inode (trans, root, inode); +btrfs_update_inode (trans, root, inode,0 ); out:if (inode)iput (inode);
@@ -936,7 +936,7 @@ insert: btrfs_inode_ref_index(eb, ref));BUG_ON (ret); -btrfs_update_inode (trans, root, inode); +btrfs_update_inode (trans, root, inode,0 ); out: ref_ptr = (unsigned long )(ref +1 ) + namelen;
@@ -1032,7 +1032,7 @@ static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,btrfs_release_path (path);if (nlink != inode->i_nlink) { inode->i_nlink = nlink; -btrfs_update_inode (trans, root, inode); +btrfs_update_inode (trans, root, inode,0 ); }BTRFS_I (inode)->index_cnt = (u64)-1 ;
@@ -1132,7 +1132,7 @@ static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,btrfs_release_path (path);if (ret ==0 ) {btrfs_inc_nlink (inode); -btrfs_update_inode (trans, root, inode); +btrfs_update_inode (trans, root, inode,0 ); }else if (ret == -EEXIST ) { ret =0 ; }else {
fs/btrfs/xattr.c +1 -1
@@ -197,7 +197,7 @@ int __btrfs_setxattr(struct btrfs_trans_handle *trans, goto out; inode->i_ctime =CURRENT_TIME ; - ret =btrfs_update_inode (trans, root, inode); + ret =btrfs_update_inode (trans, root, inode,0 );BUG_ON (ret); out:btrfs_end_transaction_throttle (trans, root);
Signature · 1 line
1.7.6.4
fs/btrfs/ctree.h struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, · line 2703
Quoted 10 lines · Josef Bacik
fs/btrfs/ctree.h +2
-2@@ -2699,8 +2699,8 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, size_t pg_offset, u64 start, u64 end, int create);int btrfs_update_inode (struct btrfs_trans_handle *trans, -struct btrfs_root *root, -struct inode *inode); +struct btrfs_root *root,struct inode *inode, +int fallback);
Can you please add a helper __btrfs_update_inode which takes the argument, leave btrfs_update_inode as is and add btrfs_update_inode_fallback for the '1' case?
So you don't change every callsite of btrfs_update_inode and just those 4 which need _fallback variant (the rest is 29, quite a lot).
david
Quoted 16 lines · David Sterba
fs/btrfs/ctree.h struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, · line 2703
Quoted 10 lines · Josef Bacik
fs/btrfs/ctree.h +2
-2@@ -2699,8 +2699,8 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, size_t pg_offset, u64 start, u64 end, int create);int btrfs_update_inode (struct btrfs_trans_handle *trans, -struct btrfs_root *root, -struct inode *inode); +struct btrfs_root *root,struct inode *inode, +int fallback);Can you please add a helper
__btrfs_update_inodewhich takes the argument, leavebtrfs_update_inodeas is and addbtrfs_update_inode_fallbackfor the '1' case?
Quoted 2 lines
So you don't change every callsite of
btrfs_update_inodeand just those 4 which need_fallbackvariant (the rest is 29, quite a lot).
I reworked this last night since I already have v2. See my for-linus branch.
-chris