Skip to content

Commit 1d16c27

Browse files
Robbie Kokdave
authored andcommitted
btrfs: reduce extent tree lock contention when searching for inline backref
When inserting extent backref, in order to check whether refs other than inline refs are used, we always use path keep locks for tree search, which will increase the lock contention of extent tree. We do not need the parent node every time to determine whether normal refs are used. It is only needed when the extent item is the last item in a leaf. Therefore, we change it to first use keep_locks=0 for search. If the extent item happens to be the last item in the leaf, we then change to keep_locks=1 for the second search to reduce lock contention. Reviewed-by: Filipe Manana <fdmanana@suse.com> Signed-off-by: Robbie Ko <robbieko@synology.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 6e6ecde commit 1d16c27

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

fs/btrfs/extent-tree.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,6 @@ int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
795795
if (insert) {
796796
extra_size = btrfs_extent_inline_ref_size(want);
797797
path->search_for_extension = 1;
798-
path->keep_locks = 1;
799798
} else
800799
extra_size = -1;
801800

@@ -946,6 +945,25 @@ int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
946945
ret = -EAGAIN;
947946
goto out;
948947
}
948+
949+
if (path->slots[0] + 1 < btrfs_header_nritems(path->nodes[0])) {
950+
struct btrfs_key tmp_key;
951+
952+
btrfs_item_key_to_cpu(path->nodes[0], &tmp_key, path->slots[0] + 1);
953+
if (tmp_key.objectid == bytenr &&
954+
tmp_key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
955+
ret = -EAGAIN;
956+
goto out;
957+
}
958+
goto out_no_entry;
959+
}
960+
961+
if (!path->keep_locks) {
962+
btrfs_release_path(path);
963+
path->keep_locks = 1;
964+
goto again;
965+
}
966+
949967
/*
950968
* To add new inline back ref, we have to make sure
951969
* there is no corresponding back ref item.
@@ -959,13 +977,15 @@ int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
959977
goto out;
960978
}
961979
}
980+
out_no_entry:
962981
*ref_ret = (struct btrfs_extent_inline_ref *)ptr;
963982
out:
964-
if (insert) {
983+
if (path->keep_locks) {
965984
path->keep_locks = 0;
966-
path->search_for_extension = 0;
967985
btrfs_unlock_up_safe(path, 1);
968986
}
987+
if (insert)
988+
path->search_for_extension = 0;
969989
return ret;
970990
}
971991

0 commit comments

Comments
 (0)