Skip to contentSkip to conversation

Btrfs: use bytes_may_use for all ENOSPC reservations

PATCH New Josef Bacik 1 participant 1 message Jul 26, 2011 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs: use bytes_may_use for all ENOSPC reservations

We have been using bytes_reserved for metadata reservations, which is wrong since we use that to keep track of outstanding reservations from the allocator. This resulted in us doing a lot of silly things to make sure we don't allocate a bunch of metadata chunks since we never had a real view of how much space was actually in use by metadata.

There are a lot of fixes in here to make this work, but the biggest things are this

1) When we make a disk reservation we drop bytes_may_use and add to bytes_reserved. We didn't use to do this because delalloc would update bytes_may_use when it cleared delalloc. So now we don't do this, we only will update bytes_may_use if we have an error and have to clear the accounting manually.

2) In should_alloc_chunk() we need to take into account the global_rsv size since that is basically used space. This will make sure we do actually alloc chunks. This logic needs to be reworked anyway, but since this is all very touchy I'll leave that for a different patch.

This passes Arne's enospc test and xfstests as well as my own enospc tests. Hopefully this will get us moving in the right direction. Thanks,

---
 fs/btrfs/ctree.h            |    9 +-
 fs/btrfs/disk-io.c          |    6 +-
 fs/btrfs/extent-tree.c      |  191 +++++++++++++++++++++++++------------------
 fs/btrfs/file.c             |    7 ++-
 fs/btrfs/free-space-cache.c |    9 +--
 fs/btrfs/inode.c            |    6 +-
 6 files changed, 129 insertions(+), 99 deletions(-)
fs/btrfs/ctree.h +5 -4
index ad28922..8b9e1f9 100644
@@ -2198,8 +2198,6 @@ int btrfs_free_extent(struct btrfs_trans_handle *trans,
 		      u64 root_objectid, u64 owner, u64 offset);
 
 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len);
-int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
-				u64 num_bytes, int reserve, int sinfo);
 int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
 				struct btrfs_root *root);
 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
@@ -2269,8 +2267,11 @@ void btrfs_put_block_group_cache(struct btrfs_fs_info *info);
 u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo);
 int btrfs_error_unpin_extent_range(struct btrfs_root *root,
 				   u64 start, u64 end);
-int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
-			       u64 num_bytes, u64 *actual_bytes);
+int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, u64 num_bytes,
+			 u64 *actual_bytes);
+int btrfs_error_discard_extent(struct btrfs_root *root,
+			       struct btrfs_block_group_cache *block_group,
+			       u64 bytenr, u64 num_bytes, u64 *actual_bytes);
 int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
 			    struct btrfs_root *root, u64 type);
 int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range);
fs/btrfs/disk-io.c +3 -3
index 36d19e8..15b3857 100644
@@ -3003,9 +3003,9 @@ static int btrfs_destroy_pinned_extent(struct btrfs_root *root,
 
 		/* opt_discard */
 		if (btrfs_test_opt(root, DISCARD))
-			ret = btrfs_error_discard_extent(root, start,
-							 end + 1 - start,
-							 NULL);
+			ret = btrfs_discard_extent(root, start,
+						   end + 1 - start,
+						   NULL);
 
 		clear_extent_dirty(unpin, start, end, GFP_NOFS);
 		btrfs_error_unpin_extent_range(root, start, end);
fs/btrfs/extent-tree.c +113 -78

343 lines not on this page · raw message

fs/btrfs/file.c +6 -1
index a35e51c..e93cf0a 100644
@@ -1670,7 +1670,12 @@ static long btrfs_fallocate(struct file *file, int mode,
 	unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
 			     &cached_state, GFP_NOFS);
 
-	btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
+	/*
+	 * We failed to allocate some of our space, so free up part of our
+	 * reservation.
+	 */
+	if (cur_offset < alloc_end)
+		btrfs_free_reserved_data_space(inode, alloc_end - cur_offset);
 out:
 	mutex_unlock(&inode->i_mutex);
 	return ret;
fs/btrfs/free-space-cache.c +1 -8
index 6377713..85bbac9 100644
@@ -2462,20 +2462,13 @@ int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
 		spin_unlock(&ctl->tree_lock);
 
 		if (bytes >= minlen) {
-			int update_ret;
-			update_ret = btrfs_update_reserved_bytes(block_group,
-								 bytes, 1, 1);
-
 			ret = btrfs_error_discard_extent(fs_info->extent_root,
+							 block_group,
 							 start,
 							 bytes,
 							 &actually_trimmed);
 
 			btrfs_add_free_space(block_group, start, bytes);
-			if (!update_ret)
-				btrfs_update_reserved_bytes(block_group,
-							    bytes, 0, 1);
-
 			if (ret)
 				break;
 			*trimmed += actually_trimmed;
fs/btrfs/inode.c +1 -5
index 0b858d7..ffd0924 100644
@@ -1388,11 +1388,7 @@ static int btrfs_clear_bit_hook(struct inode *inode,
 		}
 
 		if (*bits & EXTENT_DO_ACCOUNTING)
-			btrfs_delalloc_release_metadata(inode, len);
-
-		if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
-		    && do_list)
-			btrfs_free_reserved_data_space(inode, len);
+			btrfs_delalloc_release_space(inode, len);
 
 		spin_lock(&root->fs_info->delalloc_lock);
 		root->fs_info->delalloc_bytes -= len;
Signature · 1 line
1.7.5.2