Skip to content

Commit 8a5040f

Browse files
adam900710kdave
authored andcommitted
btrfs: ref-verify: Use btrfs_ref to refactor btrfs_ref_tree_mod()
It's a perfect match for btrfs_ref_tree_mod() to use btrfs_ref, as btrfs_ref describes a metadata/data reference update comprehensively. Now we have one less function use confusing owner/level trick. Signed-off-by: Qu Wenruo <wqu@suse.com> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 7667559 commit 8a5040f

3 files changed

Lines changed: 42 additions & 48 deletions

File tree

‎fs/btrfs/extent-tree.c‎

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2054,9 +2054,6 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
20542054
BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
20552055
root_objectid == BTRFS_TREE_LOG_OBJECTID);
20562056

2057-
btrfs_ref_tree_mod(root, bytenr, num_bytes, parent, root_objectid,
2058-
owner, offset, BTRFS_ADD_DELAYED_REF);
2059-
20602057
btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_REF, bytenr,
20612058
num_bytes, parent);
20622059
generic_ref.real_root = root->root_key.objectid;
@@ -2070,6 +2067,8 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
20702067
&old_ref_mod, &new_ref_mod);
20712068
}
20722069

2070+
btrfs_ref_tree_mod(fs_info, &generic_ref);
2071+
20732072
if (ret == 0 && old_ref_mod < 0 && new_ref_mod >= 0) {
20742073
bool metadata = owner < BTRFS_FIRST_FREE_OBJECTID;
20752074

@@ -7188,10 +7187,7 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
71887187
if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
71897188
int old_ref_mod, new_ref_mod;
71907189

7191-
btrfs_ref_tree_mod(root, buf->start, buf->len, parent,
7192-
root->root_key.objectid,
7193-
btrfs_header_level(buf), 0,
7194-
BTRFS_DROP_DELAYED_REF);
7190+
btrfs_ref_tree_mod(fs_info, &generic_ref);
71957191
ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, NULL,
71967192
&old_ref_mod, &new_ref_mod);
71977193
BUG_ON(ret); /* -ENOMEM */
@@ -7251,11 +7247,6 @@ int btrfs_free_extent(struct btrfs_trans_handle *trans,
72517247
if (btrfs_is_testing(fs_info))
72527248
return 0;
72537249

7254-
if (root_objectid != BTRFS_TREE_LOG_OBJECTID)
7255-
btrfs_ref_tree_mod(root, bytenr, num_bytes, parent,
7256-
root_objectid, owner, offset,
7257-
BTRFS_DROP_DELAYED_REF);
7258-
72597250
btrfs_init_generic_ref(&generic_ref, BTRFS_DROP_DELAYED_REF, bytenr,
72607251
num_bytes, parent);
72617252
generic_ref.real_root = root->root_key.objectid;
@@ -7279,6 +7270,9 @@ int btrfs_free_extent(struct btrfs_trans_handle *trans,
72797270
&old_ref_mod, &new_ref_mod);
72807271
}
72817272

7273+
if (root_objectid != BTRFS_TREE_LOG_OBJECTID)
7274+
btrfs_ref_tree_mod(fs_info, &generic_ref);
7275+
72827276
if (ret == 0 && old_ref_mod >= 0 && new_ref_mod < 0) {
72837277
bool metadata = owner < BTRFS_FIRST_FREE_OBJECTID;
72847278

@@ -8386,13 +8380,10 @@ int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
83868380

83878381
BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
83888382

8389-
btrfs_ref_tree_mod(root, ins->objectid, ins->offset, 0,
8390-
root->root_key.objectid, owner, offset,
8391-
BTRFS_ADD_DELAYED_EXTENT);
8392-
83938383
btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
83948384
ins->objectid, ins->offset, 0);
83958385
btrfs_init_data_ref(&generic_ref, root->root_key.objectid, owner, offset);
8386+
btrfs_ref_tree_mod(root->fs_info, &generic_ref);
83968387
ret = btrfs_add_delayed_data_ref(trans, &generic_ref,
83978388
ram_bytes, NULL, NULL);
83988389
return ret;
@@ -8641,13 +8632,11 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
86418632
extent_op->is_data = false;
86428633
extent_op->level = level;
86438634

8644-
btrfs_ref_tree_mod(root, ins.objectid, ins.offset, parent,
8645-
root_objectid, level, 0,
8646-
BTRFS_ADD_DELAYED_EXTENT);
86478635
btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
86488636
ins.objectid, ins.offset, parent);
86498637
generic_ref.real_root = root->root_key.objectid;
86508638
btrfs_init_tree_ref(&generic_ref, level, root_objectid);
8639+
btrfs_ref_tree_mod(fs_info, &generic_ref);
86518640
ret = btrfs_add_delayed_tree_ref(trans, &generic_ref,
86528641
extent_op, NULL, NULL);
86538642
if (ret)

‎fs/btrfs/ref-verify.c‎

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -670,36 +670,43 @@ static void dump_block_entry(struct btrfs_fs_info *fs_info,
670670

671671
/*
672672
* btrfs_ref_tree_mod: called when we modify a ref for a bytenr
673-
* @root: the root we are making this modification from.
674-
* @bytenr: the bytenr we are modifying.
675-
* @num_bytes: number of bytes.
676-
* @parent: the parent bytenr.
677-
* @ref_root: the original root owner of the bytenr.
678-
* @owner: level in the case of metadata, inode in the case of data.
679-
* @offset: 0 for metadata, file offset for data.
680-
* @action: the action that we are doing, this is the same as the delayed ref
681-
* action.
682673
*
683674
* This will add an action item to the given bytenr and do sanity checks to make
684675
* sure we haven't messed something up. If we are making a new allocation and
685676
* this block entry has history we will delete all previous actions as long as
686677
* our sanity checks pass as they are no longer needed.
687678
*/
688-
int btrfs_ref_tree_mod(struct btrfs_root *root, u64 bytenr, u64 num_bytes,
689-
u64 parent, u64 ref_root, u64 owner, u64 offset,
690-
int action)
679+
int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info,
680+
struct btrfs_ref *generic_ref)
691681
{
692-
struct btrfs_fs_info *fs_info = root->fs_info;
693682
struct ref_entry *ref = NULL, *exist;
694683
struct ref_action *ra = NULL;
695684
struct block_entry *be = NULL;
696685
struct root_entry *re = NULL;
686+
int action = generic_ref->action;
697687
int ret = 0;
698-
bool metadata = owner < BTRFS_FIRST_FREE_OBJECTID;
688+
bool metadata;
689+
u64 bytenr = generic_ref->bytenr;
690+
u64 num_bytes = generic_ref->len;
691+
u64 parent = generic_ref->parent;
692+
u64 ref_root;
693+
u64 owner;
694+
u64 offset;
699695

700-
if (!btrfs_test_opt(root->fs_info, REF_VERIFY))
696+
if (!btrfs_test_opt(fs_info, REF_VERIFY))
701697
return 0;
702698

699+
if (generic_ref->type == BTRFS_REF_METADATA) {
700+
ref_root = generic_ref->tree_ref.root;
701+
owner = generic_ref->tree_ref.level;
702+
offset = 0;
703+
} else {
704+
ref_root = generic_ref->data_ref.ref_root;
705+
owner = generic_ref->data_ref.ino;
706+
offset = generic_ref->data_ref.offset;
707+
}
708+
metadata = owner < BTRFS_FIRST_FREE_OBJECTID;
709+
703710
ref = kzalloc(sizeof(struct ref_entry), GFP_NOFS);
704711
ra = kmalloc(sizeof(struct ref_action), GFP_NOFS);
705712
if (!ra || !ref) {
@@ -732,7 +739,7 @@ int btrfs_ref_tree_mod(struct btrfs_root *root, u64 bytenr, u64 num_bytes,
732739

733740
INIT_LIST_HEAD(&ra->list);
734741
ra->action = action;
735-
ra->root = root->root_key.objectid;
742+
ra->root = generic_ref->real_root;
736743

737744
/*
738745
* This is an allocation, preallocate the block_entry in case we haven't
@@ -745,7 +752,7 @@ int btrfs_ref_tree_mod(struct btrfs_root *root, u64 bytenr, u64 num_bytes,
745752
* is and the new root objectid, so let's not treat the passed
746753
* in root as if it really has a ref for this bytenr.
747754
*/
748-
be = add_block_entry(root->fs_info, bytenr, num_bytes, ref_root);
755+
be = add_block_entry(fs_info, bytenr, num_bytes, ref_root);
749756
if (IS_ERR(be)) {
750757
kfree(ra);
751758
ret = PTR_ERR(be);
@@ -787,13 +794,13 @@ int btrfs_ref_tree_mod(struct btrfs_root *root, u64 bytenr, u64 num_bytes,
787794
* one we want to lookup below when we modify the
788795
* re->num_refs.
789796
*/
790-
ref_root = root->root_key.objectid;
791-
re->root_objectid = root->root_key.objectid;
797+
ref_root = generic_ref->real_root;
798+
re->root_objectid = generic_ref->real_root;
792799
re->num_refs = 0;
793800
}
794801

795-
spin_lock(&root->fs_info->ref_verify_lock);
796-
be = lookup_block_entry(&root->fs_info->block_tree, bytenr);
802+
spin_lock(&fs_info->ref_verify_lock);
803+
be = lookup_block_entry(&fs_info->block_tree, bytenr);
797804
if (!be) {
798805
btrfs_err(fs_info,
799806
"trying to do action %d to bytenr %llu num_bytes %llu but there is no existing entry!",
@@ -862,7 +869,7 @@ int btrfs_ref_tree_mod(struct btrfs_root *root, u64 bytenr, u64 num_bytes,
862869
* didn't think of some other corner case.
863870
*/
864871
btrfs_err(fs_info, "failed to find root %llu for %llu",
865-
root->root_key.objectid, be->bytenr);
872+
generic_ref->real_root, be->bytenr);
866873
dump_block_entry(fs_info, be);
867874
dump_ref_action(fs_info, ra);
868875
kfree(ra);
@@ -881,7 +888,7 @@ int btrfs_ref_tree_mod(struct btrfs_root *root, u64 bytenr, u64 num_bytes,
881888
list_add_tail(&ra->list, &be->actions);
882889
ret = 0;
883890
out_unlock:
884-
spin_unlock(&root->fs_info->ref_verify_lock);
891+
spin_unlock(&fs_info->ref_verify_lock);
885892
out:
886893
if (ret)
887894
btrfs_clear_opt(fs_info->mount_opt, REF_VERIFY);

‎fs/btrfs/ref-verify.h‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
#ifdef CONFIG_BTRFS_FS_REF_VERIFY
1010
int btrfs_build_ref_tree(struct btrfs_fs_info *fs_info);
1111
void btrfs_free_ref_cache(struct btrfs_fs_info *fs_info);
12-
int btrfs_ref_tree_mod(struct btrfs_root *root, u64 bytenr, u64 num_bytes,
13-
u64 parent, u64 ref_root, u64 owner, u64 offset,
14-
int action);
12+
int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info,
13+
struct btrfs_ref *generic_ref);
1514
void btrfs_free_ref_tree_range(struct btrfs_fs_info *fs_info, u64 start,
1615
u64 len);
1716

@@ -30,9 +29,8 @@ static inline void btrfs_free_ref_cache(struct btrfs_fs_info *fs_info)
3029
{
3130
}
3231

33-
static inline int btrfs_ref_tree_mod(struct btrfs_root *root, u64 bytenr,
34-
u64 num_bytes, u64 parent, u64 ref_root,
35-
u64 owner, u64 offset, int action)
32+
static inline int btrfs_ref_tree_mod(struct btrfs_fs_info *fs_info,
33+
struct btrfs_ref *generic_ref)
3634
{
3735
return 0;
3836
}

0 commit comments

Comments
 (0)