Skip to content

Commit 88e081b

Browse files
Wang ShilongJosef Bacik
Wang Shilong
authored and
Josef Bacik
committed
Btrfs: cleanup to make the function btrfs_delalloc_reserve_metadata more logic
The original code is a little confusing and not clear, The right way to deal with the kernel code like this: [...] if (ret) goto out; [...] So i move the common clean_up code to the place labeled with out_fail, this will be easier to maintain. Signed-off-by: Wang Shilong <[email protected]> Signed-off-by: Josef Bacik <[email protected]>
1 parent a9870c0 commit 88e081b

File tree

1 file changed

+38
-44
lines changed

1 file changed

+38
-44
lines changed

fs/btrfs/extent-tree.c

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4721,6 +4721,8 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
47214721
enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
47224722
int ret = 0;
47234723
bool delalloc_lock = true;
4724+
u64 to_free = 0;
4725+
unsigned dropped;
47244726

47254727
/* If we are a free space inode we need to not flush since we will be in
47264728
* the middle of a transaction commit. We also don't need the delalloc
@@ -4764,55 +4766,19 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
47644766
csum_bytes = BTRFS_I(inode)->csum_bytes;
47654767
spin_unlock(&BTRFS_I(inode)->lock);
47664768

4767-
if (root->fs_info->quota_enabled)
4769+
if (root->fs_info->quota_enabled) {
47684770
ret = btrfs_qgroup_reserve(root, num_bytes +
47694771
nr_extents * root->leafsize);
4772+
if (ret)
4773+
goto out_fail;
4774+
}
47704775

4771-
/*
4772-
* ret != 0 here means the qgroup reservation failed, we go straight to
4773-
* the shared error handling then.
4774-
*/
4775-
if (ret == 0) {
4776-
ret = reserve_metadata_bytes(root, block_rsv,
4777-
to_reserve, flush);
4778-
if (ret && root->fs_info->quota_enabled) {
4776+
ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
4777+
if (unlikely(ret)) {
4778+
if (root->fs_info->quota_enabled)
47794779
btrfs_qgroup_free(root, num_bytes +
47804780
nr_extents * root->leafsize);
4781-
}
4782-
}
4783-
4784-
if (ret) {
4785-
u64 to_free = 0;
4786-
unsigned dropped;
4787-
4788-
spin_lock(&BTRFS_I(inode)->lock);
4789-
dropped = drop_outstanding_extent(inode);
4790-
/*
4791-
* If the inodes csum_bytes is the same as the original
4792-
* csum_bytes then we know we haven't raced with any free()ers
4793-
* so we can just reduce our inodes csum bytes and carry on.
4794-
* Otherwise we have to do the normal free thing to account for
4795-
* the case that the free side didn't free up its reserve
4796-
* because of this outstanding reservation.
4797-
*/
4798-
if (BTRFS_I(inode)->csum_bytes == csum_bytes)
4799-
calc_csum_metadata_size(inode, num_bytes, 0);
4800-
else
4801-
to_free = calc_csum_metadata_size(inode, num_bytes, 0);
4802-
spin_unlock(&BTRFS_I(inode)->lock);
4803-
if (dropped)
4804-
to_free += btrfs_calc_trans_metadata_size(root, dropped);
4805-
4806-
if (to_free) {
4807-
btrfs_block_rsv_release(root, block_rsv, to_free);
4808-
trace_btrfs_space_reservation(root->fs_info,
4809-
"delalloc",
4810-
btrfs_ino(inode),
4811-
to_free, 0);
4812-
}
4813-
if (delalloc_lock)
4814-
mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
4815-
return ret;
4781+
goto out_fail;
48164782
}
48174783

48184784
spin_lock(&BTRFS_I(inode)->lock);
@@ -4833,6 +4799,34 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
48334799
block_rsv_add_bytes(block_rsv, to_reserve, 1);
48344800

48354801
return 0;
4802+
4803+
out_fail:
4804+
spin_lock(&BTRFS_I(inode)->lock);
4805+
dropped = drop_outstanding_extent(inode);
4806+
/*
4807+
* If the inodes csum_bytes is the same as the original
4808+
* csum_bytes then we know we haven't raced with any free()ers
4809+
* so we can just reduce our inodes csum bytes and carry on.
4810+
* Otherwise we have to do the normal free thing to account for
4811+
* the case that the free side didn't free up its reserve
4812+
* because of this outstanding reservation.
4813+
*/
4814+
if (BTRFS_I(inode)->csum_bytes == csum_bytes)
4815+
calc_csum_metadata_size(inode, num_bytes, 0);
4816+
else
4817+
to_free = calc_csum_metadata_size(inode, num_bytes, 0);
4818+
spin_unlock(&BTRFS_I(inode)->lock);
4819+
if (dropped)
4820+
to_free += btrfs_calc_trans_metadata_size(root, dropped);
4821+
4822+
if (to_free) {
4823+
btrfs_block_rsv_release(root, block_rsv, to_free);
4824+
trace_btrfs_space_reservation(root->fs_info, "delalloc",
4825+
btrfs_ino(inode), to_free, 0);
4826+
}
4827+
if (delalloc_lock)
4828+
mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
4829+
return ret;
48364830
}
48374831

48384832
/**

0 commit comments

Comments
 (0)