Btrfs: fix ENOSPC accounting when max_extent is not maxed out
PATCH 1/1 Btrfs: fix ENOSPC accounting when max_extent is not maxed out
A user reported a bug a few weeks back where if he set max_extent=1m and then did a dd and then stopped it, we would panic. This is because I miscalculated how many extents would be needed for splits/merges. Turns out I didn't actually take max_extent into account properly, since we only ever add 1 extent for a write, which isn't quite right for the case that say max_extent is 4k and we do 8k writes. That would result in more than 1 extent. So this patch makes us properly figure out how many extents are needed for the amount of data that is being written, and deals with splitting and merging better. I've tested this ad nauseum and it works well. It depends on all of the other patches I've sent recently, including the per-cpu pools patch. Thanks,
--- fs/btrfs/ctree.h | 8 ++++ fs/btrfs/extent-tree.c | 6 +- fs/btrfs/file.c | 5 +- fs/btrfs/inode.c | 99 +++++++++++++++++++++++++++++++--------------- fs/btrfs/ordered-data.c | 13 ++++-- 5 files changed, 90 insertions(+), 41 deletions(-)
fs/btrfs/ctree.h +8 -0
@@ -1965,6 +1965,14 @@ static inline struct dentry *fdentry(struct file *file)return file->f_path.dentry; } +static inline int calculate_extents (struct btrfs_root *root,u64 bytes) +{ +if (bytes <= root->fs_info->max_extent) +return 1 ; +return (int )div64_u64 (bytes + root->fs_info->max_extent -1 , + root->fs_info->max_extent); +} +/* extent-tree.c */ void btrfs_put_block_group (struct btrfs_block_group_cache *cache); int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
fs/btrfs/extent-tree.c +3 -3
@@ -2837,7 +2837,7 @@ int btrfs_unreserve_metadata_for_delalloc(struct btrfs_root *root,spin_unlock (&BTRFS_I (inode)->accounting_lock);return 0 ; } -BTRFS_I (inode)->reserved_extents--; +BTRFS_I (inode)->reserved_extents -= num_items;spin_unlock (&BTRFS_I (inode)->accounting_lock);btrfs_unreserve_metadata_space (root, num_items);
@@ -3059,7 +3059,7 @@ again:if (realloc_bytes >= num_bytes) { pool->total_bytes += realloc_bytes;spin_lock (&BTRFS_I (inode)->accounting_lock); -BTRFS_I (inode)->reserved_extents++; +BTRFS_I (inode)->reserved_extents += num_items;spin_unlock (&BTRFS_I (inode)->accounting_lock);spin_unlock (&pool->lock);return 0 ;
@@ -3074,7 +3074,7 @@ again:*/ if (pool->reserved_bytes + pool->used_bytes <= pool->total_bytes) {spin_lock (&BTRFS_I (inode)->accounting_lock); -BTRFS_I (inode)->reserved_extents++; +BTRFS_I (inode)->reserved_extents += num_items;spin_unlock (&BTRFS_I (inode)->accounting_lock);spin_unlock (&pool->lock);return 0 ;
fs/btrfs/file.c +3 -2
@@ -838,6 +838,7 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,unsigned long first_index;unsigned long last_index;int will_write; +int reserved =calculate_extents (root, count); will_write = ((file->f_flags &O_SYNC ) ||IS_SYNC (inode) || (file->f_flags &O_DIRECT ));
@@ -855,7 +856,7 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,/* do the reserve before the mutex lock in case we have to do some * flushing. We wouldn't deadlock, but this is more polite. */ - err =btrfs_reserve_metadata_for_delalloc (root, inode,1 ); + err =btrfs_reserve_metadata_for_delalloc (root, inode, reserved);if (err) goto out_nolock;
@@ -975,7 +976,7 @@ out:mutex_unlock (&inode->i_mutex);if (ret) err = ret; -btrfs_unreserve_metadata_for_delalloc (root, inode,1 ); +btrfs_unreserve_metadata_for_delalloc (root, inode, reserved); out_nolock:kfree (pages);
fs/btrfs/inode.c +67 -32
182 lines not on this page · raw message
fs/btrfs/ordered-data.c +9 -4
@@ -303,7 +303,9 @@ static int __btrfs_remove_ordered_extent(struct inode *inode, struct btrfs_ordered_extent *entry) {struct btrfs_ordered_inode_tree *tree; +struct btrfs_root *root =BTRFS_I (inode)->root;struct rb_node *node; +int bug =0 ; tree = &BTRFS_I (inode)->ordered_tree; node = &entry->rb_node;
@@ -312,13 +314,16 @@ static int __btrfs_remove_ordered_extent(struct inode *inode,set_bit (BTRFS_ORDERED_COMPLETE , &entry->flags);spin_lock (&BTRFS_I (inode)->accounting_lock); -BUG_ON (!BTRFS_I (inode)->outstanding_extents); -BTRFS_I (inode)->outstanding_extents--; +if (BTRFS_I (inode)->outstanding_extents) +BTRFS_I (inode)->outstanding_extents--; +else + bug =1 ;spin_unlock (&BTRFS_I (inode)->accounting_lock); +BUG_ON (bug);btrfs_unreserve_metadata_for_delalloc (BTRFS_I (inode)->root, inode,1 ); -spin_lock (&BTRFS_I (inode)->root->fs_info->ordered_extent_lock); +spin_lock (&root->fs_info->ordered_extent_lock);list_del_init (&entry->root_extent_list);/*
@@ -330,7 +335,7 @@ static int __btrfs_remove_ordered_extent(struct inode *inode, !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) {list_del_init (&BTRFS_I (inode)->ordered_operations); } -spin_unlock (&BTRFS_I (inode)->root->fs_info->ordered_extent_lock); +spin_unlock (&root->fs_info->ordered_extent_lock);return 0 ; }
Signature · 1 line
1.6.6