Commit ffd4bb2a authored by Qu Wenruo's avatar Qu Wenruo Committed by David Sterba

btrfs: extent-tree: Use btrfs_ref to refactor btrfs_free_extent()

Similar to btrfs_inc_extent_ref(), use btrfs_ref to replace the long
parameter list and the confusing @owner parameter.
Signed-off-by: default avatarQu Wenruo <wqu@suse.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 82fa113f
...@@ -2749,10 +2749,7 @@ int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, ...@@ -2749,10 +2749,7 @@ int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
struct btrfs_fs_info *fs_info, struct btrfs_fs_info *fs_info,
u64 bytenr, u64 num_bytes, u64 flags, u64 bytenr, u64 num_bytes, u64 flags,
int level, int is_data); int level, int is_data);
int btrfs_free_extent(struct btrfs_trans_handle *trans, int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref);
struct btrfs_root *root,
u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
u64 owner, u64 offset);
int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info, int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
u64 start, u64 len, int delalloc); u64 start, u64 len, int delalloc);
......
...@@ -3195,9 +3195,7 @@ static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, ...@@ -3195,9 +3195,7 @@ static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
if (inc) if (inc)
ret = btrfs_inc_extent_ref(trans, &generic_ref); ret = btrfs_inc_extent_ref(trans, &generic_ref);
else else
ret = btrfs_free_extent(trans, root, bytenr, ret = btrfs_free_extent(trans, &generic_ref);
num_bytes, parent, ref_root,
key.objectid, key.offset);
if (ret) if (ret)
goto fail; goto fail;
} else { } else {
...@@ -3211,9 +3209,7 @@ static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, ...@@ -3211,9 +3209,7 @@ static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
if (inc) if (inc)
ret = btrfs_inc_extent_ref(trans, &generic_ref); ret = btrfs_inc_extent_ref(trans, &generic_ref);
else else
ret = btrfs_free_extent(trans, root, bytenr, ret = btrfs_free_extent(trans, &generic_ref);
num_bytes, parent, ref_root,
level - 1, 0);
if (ret) if (ret)
goto fail; goto fail;
} }
...@@ -7238,47 +7234,43 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans, ...@@ -7238,47 +7234,43 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
} }
/* Can return -ENOMEM */ /* Can return -ENOMEM */
int btrfs_free_extent(struct btrfs_trans_handle *trans, int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref)
struct btrfs_root *root,
u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
u64 owner, u64 offset)
{ {
struct btrfs_fs_info *fs_info = root->fs_info; struct btrfs_fs_info *fs_info = trans->fs_info;
struct btrfs_ref generic_ref = { 0 };
int old_ref_mod, new_ref_mod; int old_ref_mod, new_ref_mod;
int ret; int ret;
if (btrfs_is_testing(fs_info)) if (btrfs_is_testing(fs_info))
return 0; return 0;
btrfs_init_generic_ref(&generic_ref, BTRFS_DROP_DELAYED_REF, bytenr,
num_bytes, parent);
generic_ref.real_root = root->root_key.objectid;
/* /*
* tree log blocks never actually go into the extent allocation * tree log blocks never actually go into the extent allocation
* tree, just update pinning info and exit early. * tree, just update pinning info and exit early.
*/ */
if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { if ((ref->type == BTRFS_REF_METADATA &&
WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) ||
(ref->type == BTRFS_REF_DATA &&
ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)) {
/* unlocks the pinned mutex */ /* unlocks the pinned mutex */
btrfs_pin_extent(fs_info, bytenr, num_bytes, 1); btrfs_pin_extent(fs_info, ref->bytenr, ref->len, 1);
old_ref_mod = new_ref_mod = 0; old_ref_mod = new_ref_mod = 0;
ret = 0; ret = 0;
} else if (owner < BTRFS_FIRST_FREE_OBJECTID) { } else if (ref->type == BTRFS_REF_METADATA) {
btrfs_init_tree_ref(&generic_ref, (int)owner, root_objectid); ret = btrfs_add_delayed_tree_ref(trans, ref, NULL,
ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, NULL,
&old_ref_mod, &new_ref_mod); &old_ref_mod, &new_ref_mod);
} else { } else {
btrfs_init_data_ref(&generic_ref, root_objectid, owner, offset); ret = btrfs_add_delayed_data_ref(trans, ref, 0,
ret = btrfs_add_delayed_data_ref(trans, &generic_ref, 0,
&old_ref_mod, &new_ref_mod); &old_ref_mod, &new_ref_mod);
} }
if (root_objectid != BTRFS_TREE_LOG_OBJECTID) if (!((ref->type == BTRFS_REF_METADATA &&
btrfs_ref_tree_mod(fs_info, &generic_ref); ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) ||
(ref->type == BTRFS_REF_DATA &&
ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)))
btrfs_ref_tree_mod(fs_info, ref);
if (ret == 0 && old_ref_mod >= 0 && new_ref_mod < 0) if (ret == 0 && old_ref_mod >= 0 && new_ref_mod < 0)
add_pinned_bytes(fs_info, &generic_ref); add_pinned_bytes(fs_info, ref);
return ret; return ret;
} }
...@@ -8883,6 +8875,7 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans, ...@@ -8883,6 +8875,7 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
u64 parent; u64 parent;
struct btrfs_key key; struct btrfs_key key;
struct btrfs_key first_key; struct btrfs_key first_key;
struct btrfs_ref ref = { 0 };
struct extent_buffer *next; struct extent_buffer *next;
int level = wc->level; int level = wc->level;
int reada = 0; int reada = 0;
...@@ -9055,9 +9048,10 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans, ...@@ -9055,9 +9048,10 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
wc->drop_level = level; wc->drop_level = level;
find_next_key(path, level, &wc->drop_progress); find_next_key(path, level, &wc->drop_progress);
ret = btrfs_free_extent(trans, root, bytenr, fs_info->nodesize, btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
parent, root->root_key.objectid, fs_info->nodesize, parent);
level - 1, 0); btrfs_init_tree_ref(&ref, level - 1, root->root_key.objectid);
ret = btrfs_free_extent(trans, &ref);
if (ret) if (ret)
goto out_unlock; goto out_unlock;
} }
......
...@@ -997,11 +997,14 @@ int __btrfs_drop_extents(struct btrfs_trans_handle *trans, ...@@ -997,11 +997,14 @@ int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
extent_end = ALIGN(extent_end, extent_end = ALIGN(extent_end,
fs_info->sectorsize); fs_info->sectorsize);
} else if (update_refs && disk_bytenr > 0) { } else if (update_refs && disk_bytenr > 0) {
ret = btrfs_free_extent(trans, root, btrfs_init_generic_ref(&ref,
disk_bytenr, num_bytes, 0, BTRFS_DROP_DELAYED_REF,
disk_bytenr, num_bytes, 0);
btrfs_init_data_ref(&ref,
root->root_key.objectid, root->root_key.objectid,
key.objectid, key.offset - key.objectid,
extent_offset); key.offset - extent_offset);
ret = btrfs_free_extent(trans, &ref);
BUG_ON(ret); /* -ENOMEM */ BUG_ON(ret); /* -ENOMEM */
inode_sub_bytes(inode, inode_sub_bytes(inode,
extent_end - key.offset); extent_end - key.offset);
...@@ -1318,6 +1321,9 @@ int btrfs_mark_extent_written(struct btrfs_trans_handle *trans, ...@@ -1318,6 +1321,9 @@ int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
other_start = end; other_start = end;
other_end = 0; other_end = 0;
btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
num_bytes, 0);
btrfs_init_data_ref(&ref, root->root_key.objectid, ino, orig_offset);
if (extent_mergeable(leaf, path->slots[0] + 1, if (extent_mergeable(leaf, path->slots[0] + 1,
ino, bytenr, orig_offset, ino, bytenr, orig_offset,
&other_start, &other_end)) { &other_start, &other_end)) {
...@@ -1328,9 +1334,7 @@ int btrfs_mark_extent_written(struct btrfs_trans_handle *trans, ...@@ -1328,9 +1334,7 @@ int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
extent_end = other_end; extent_end = other_end;
del_slot = path->slots[0] + 1; del_slot = path->slots[0] + 1;
del_nr++; del_nr++;
ret = btrfs_free_extent(trans, root, bytenr, num_bytes, ret = btrfs_free_extent(trans, &ref);
0, root->root_key.objectid,
ino, orig_offset);
if (ret) { if (ret) {
btrfs_abort_transaction(trans, ret); btrfs_abort_transaction(trans, ret);
goto out; goto out;
...@@ -1348,9 +1352,7 @@ int btrfs_mark_extent_written(struct btrfs_trans_handle *trans, ...@@ -1348,9 +1352,7 @@ int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
key.offset = other_start; key.offset = other_start;
del_slot = path->slots[0]; del_slot = path->slots[0];
del_nr++; del_nr++;
ret = btrfs_free_extent(trans, root, bytenr, num_bytes, ret = btrfs_free_extent(trans, &ref);
0, root->root_key.objectid,
ino, orig_offset);
if (ret) { if (ret) {
btrfs_abort_transaction(trans, ret); btrfs_abort_transaction(trans, ret);
goto out; goto out;
......
...@@ -4708,12 +4708,17 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, ...@@ -4708,12 +4708,17 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
if (found_extent && if (found_extent &&
(test_bit(BTRFS_ROOT_REF_COWS, &root->state) || (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
root == fs_info->tree_root)) { root == fs_info->tree_root)) {
struct btrfs_ref ref = { 0 };
btrfs_set_path_blocking(path); btrfs_set_path_blocking(path);
bytes_deleted += extent_num_bytes; bytes_deleted += extent_num_bytes;
ret = btrfs_free_extent(trans, root, extent_start,
extent_num_bytes, 0, btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
btrfs_header_owner(leaf), extent_start, extent_num_bytes, 0);
ino, extent_offset); ref.real_root = root->root_key.objectid;
btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
ino, extent_offset);
ret = btrfs_free_extent(trans, &ref);
if (ret) { if (ret) {
btrfs_abort_transaction(trans, ret); btrfs_abort_transaction(trans, ret);
break; break;
......
...@@ -1716,9 +1716,12 @@ int replace_file_extents(struct btrfs_trans_handle *trans, ...@@ -1716,9 +1716,12 @@ int replace_file_extents(struct btrfs_trans_handle *trans,
break; break;
} }
ret = btrfs_free_extent(trans, root, bytenr, num_bytes, btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
parent, btrfs_header_owner(leaf), num_bytes, parent);
key.objectid, key.offset); ref.real_root = root->root_key.objectid;
btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
key.objectid, key.offset);
ret = btrfs_free_extent(trans, &ref);
if (ret) { if (ret) {
btrfs_abort_transaction(trans, ret); btrfs_abort_transaction(trans, ret);
break; break;
...@@ -1934,14 +1937,18 @@ int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc, ...@@ -1934,14 +1937,18 @@ int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc,
ret = btrfs_inc_extent_ref(trans, &ref); ret = btrfs_inc_extent_ref(trans, &ref);
BUG_ON(ret); BUG_ON(ret);
ret = btrfs_free_extent(trans, src, new_bytenr, blocksize, btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, new_bytenr,
path->nodes[level]->start, blocksize, path->nodes[level]->start);
src->root_key.objectid, level - 1, 0); btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid);
ref.skip_qgroup = true;
ret = btrfs_free_extent(trans, &ref);
BUG_ON(ret); BUG_ON(ret);
ret = btrfs_free_extent(trans, dest, old_bytenr, blocksize, btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, old_bytenr,
0, dest->root_key.objectid, level - 1, blocksize, 0);
0); btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid);
ref.skip_qgroup = true;
ret = btrfs_free_extent(trans, &ref);
BUG_ON(ret); BUG_ON(ret);
btrfs_unlock_up_safe(path, 0); btrfs_unlock_up_safe(path, 0);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment