btrfs: Turn fs_roots_radix in btrfs_fs_info into an XArray
PATCH v2 1/1 btrfs: Turn fs_roots_radix in btrfs_fs_info into an XArray
… rename it to simply fs_roots and adjust all usages of this object to use the XArray API, because it is notionally easier to use and understand, as it provides array semantics, and also takes care of locking for us, further simplifying the code.
Also do some refactoring, esp. where the API change requires largely rewriting some functions, anyway.
---
Changes from v1:
- Removed unnecessary enclosing while-loops around XArray iterators (Nikolay)
- Renamed BTRFS_ROOT_IN_RADIX to BTRFS_ROOT_REGISTERED (Nikolay & me)
- Moved variable declaration into the one loop that uses it (Nikolay)
- Removed some unnecessary linebreaks (Nikolay)
--- fs/btrfs/ctree.h | 7 +- fs/btrfs/disk-io.c | 179 +++++++++++++++++------------------ fs/btrfs/extent-tree.c | 2 +- fs/btrfs/inode.c | 13 +-- fs/btrfs/tests/btrfs-tests.c | 2 +- fs/btrfs/transaction.c | 113 ++++++++++------------ 6 files changed, 149 insertions(+), 167 deletions(-)
fs/btrfs/ctree.h +4 -3
@@ -675,8 +675,9 @@ struct btrfs_fs_info {rwlock_t global_root_lock;struct rb_root global_root_tree; -spinlock_t fs_roots_radix_lock; -struct radix_tree_root fs_roots_radix; +/* The XArray that holds all the FS roots */ +spinlock_t fs_roots_lock; +struct xarray fs_roots;/* block group cache stuff */ spinlock_t block_group_cache_lock;
@@ -1120,7 +1121,7 @@ enum {*/ BTRFS_ROOT_SHAREABLE ,BTRFS_ROOT_TRACK_DIRTY , -BTRFS_ROOT_IN_RADIX , +BTRFS_ROOT_REGISTERED ,BTRFS_ROOT_ORPHAN_ITEM_INSERTED ,BTRFS_ROOT_DEFRAG_RUNNING , BTRFS_ROOT_FORCE_COW,
fs/btrfs/disk-io.c +86 -93
278 lines not on this page · raw message
fs/btrfs/extent-tree.c +1 -1
@@ -5818,7 +5818,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref, int for_reloc)btrfs_qgroup_convert_reserved_meta (root,INT_MAX );btrfs_qgroup_free_meta_all_pertrans (root); -if (test_bit (BTRFS_ROOT_IN_RADIX , &root->state)) +if (test_bit (BTRFS_ROOT_REGISTERED , &root->state))btrfs_add_dropped_root (trans, root);else btrfs_put_root (root);
fs/btrfs/inode.c +7 -6
@@ -3494,6 +3494,7 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)u64 last_objectid =0 ;int ret =0 , nr_unlink =0 ; +/* Bail out if the cleanup is already running. */ if (test_and_set_bit (BTRFS_ROOT_ORPHAN_CLEANUP , &root->state))return 0 ;
@@ -3576,17 +3577,17 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)* * btrfs_find_orphan_roots() ran before us, which has * found all deleted roots and loaded them into -* fs_info->fs_roots_radix. So here we can find if an +* fs_info->fs_roots. So here we can find if an * orphan item corresponds to a deleted root by looking -* up the root from that radix tree. +* up the root from that xarray. */ -spin_lock (&fs_info->fs_roots_radix_lock); - dead_root =radix_tree_lookup (&fs_info->fs_roots_radix, - (unsigned long )found_key.objectid); +spin_lock (&fs_info->fs_roots_lock); + dead_root =xa_load (&fs_info->fs_roots, + (unsigned long )found_key.objectid);if (dead_root &&btrfs_root_refs (&dead_root->root_item) ==0 ) is_dead_root =1 ; -spin_unlock (&fs_info->fs_roots_radix_lock); +spin_unlock (&fs_info->fs_roots_lock);if (is_dead_root) {/* prevent this orphan from being found again */
fs/btrfs/tests/btrfs-tests.c +1 -1
@@ -202,7 +202,7 @@ void btrfs_free_dummy_root(struct btrfs_root *root)if (!root)return ;/* Will be freed by btrfs_free_fs_roots */ -if (WARN_ON (test_bit (BTRFS_ROOT_IN_RADIX , &root->state))) +if (WARN_ON (test_bit (BTRFS_ROOT_REGISTERED , &root->state)))return ;btrfs_global_root_delete (root);btrfs_put_root (root);
fs/btrfs/transaction.c +50 -63
149 lines not on this page · raw message
Signature · 1 line
2.36.0
Quoted 10 lines · Gabriel Niebler
… rename it to simply
fs_rootsand adjust all usages of this object to use the XArray API, because it is notionally easier to use and understand, as it provides array semantics, and also takes care of locking for us, further simplifying the code.Also do some refactoring, esp. where the API change requires largely rewriting some functions, anyway.
Signed-off-by Gabriel Niebler <[email protected]>---
LGTM, one nit below but I'd like David to say what he thinks of the style.
Quoted 16 lines
Changes from v1:
- Removed unnecessary enclosing while-loops around XArray iterators (Nikolay)
- RenamedBTRFS_ROOT_IN_RADIXtoBTRFS_ROOT_REGISTERED(Nikolay & me)
- Moved variable declaration into the one loop that uses it (Nikolay)
- Removed some unnecessary linebreaks (Nikolay)--- fs/btrfs/ctree.h | 7 +- fs/btrfs/disk-io.c | 179 +++++++++++++++++------------------ fs/btrfs/extent-tree.c | 2 +- fs/btrfs/inode.c | 13 +-- fs/btrfs/tests/btrfs-tests.c | 2 +- fs/btrfs/transaction.c | 113 ++++++++++------------ 6 files changed, 149 insertions(+), 167 deletions(-)
<snip>
Quoted 45 lines
(quoted diff) +20
-17@@ -4872,31 +4862,34 @@ static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) static void btrfs_drop_all_logs(struct btrfs_fs_info *fs_info) { - struct btrfs_root *gang[8]; - u64 root_objectid = 0; - int ret; - - spin_lock(&fs_info->fs_roots_radix_lock); - while ((ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, - (void **)gang, root_objectid, - ARRAY_SIZE(gang))) != 0) { - int i; + unsigned long index = 0; - for (i = 0; i < ret; i++) - gang[i] = btrfs_grab_root(gang[i]); - spin_unlock(&fs_info->fs_roots_radix_lock); + spin_lock(&fs_info->fs_roots_lock); + while (xa_find(&fs_info->fs_roots, &index, ULONG_MAX, XA_PRESENT)) { + struct btrfs_root *root; + struct btrfs_root *roots[8]; + int grabbed = 0; + + xa_for_each_start(&fs_info->fs_roots, index, root, + index) { + roots[grabbed] = btrfs_grab_root(root); + grabbed++; + if (grabbed >= ARRAY_SIZE(roots)) + break; + } + spin_unlock(&fs_info->fs_roots_lock); - for (i = 0; i < ret; i++) { - if (!gang[i]) + for (int i = 0; i < grabbed; i++) { + if (!roots[i]) continue; - root_objectid = gang[i]->root_key.objectid; - btrfs_free_log(NULL, gang[i]); - btrfs_put_root(gang[i]); + index = roots[i]->root_key.objectid; + btrfs_free_log(NULL, roots[i]); + btrfs_put_root(roots[i]); }
Review static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) · line 4895
Quoted 10 lines
(quoted diff) +3
-3- root_objectid++; - spin_lock(&fs_info->fs_roots_radix_lock); + index++; + spin_lock(&fs_info->fs_roots_lock); } - spin_unlock(&fs_info->fs_roots_radix_lock); + spin_unlock(&fs_info->fs_roots_lock); btrfs_free_log_root_tree(NULL, fs_info); }
nit: The xa_find/xa_for_each_start as used in this loop are really open-coded xa_extract. So the code could be simplified even further:
fs/btrfs/disk-io.c +7 -12
@@ -4875,23 +4875,18 @@ static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info)static void btrfs_drop_all_logs (struct btrfs_fs_info *fs_info) {unsigned long index =0 ; +int grabbed =0 ; +struct btrfs_root *roots[8 ];spin_lock (&fs_info->fs_roots_lock); -while (xa_find (&fs_info->fs_roots, &index,ULONG_MAX ,XA_PRESENT )) { -struct btrfs_root *root; -struct btrfs_root *roots[8 ]; -int i; -int grabbed =0 ; +while ((grabbed =xa_extract (&fs_info->fs_roots, (void **)roots, index, +ULONG_MAX ,8 ,XA_PRESENT ))) { -xa_for_each_start (&fs_info->fs_roots, index, root, index) { - roots[grabbed] =btrfs_grab_root (root); - grabbed++; -if (grabbed >=ARRAY_SIZE (roots)) -break ; - } +for (int i =0 ; i < grabbed; i++) + roots[i] =btrfs_grab_root (roots[i]);spin_unlock (&fs_info->fs_roots_lock); -for (i =0 ; i < grabbed; i++) { +for (int i =0 ; i < grabbed; i++) {if (!roots[i])continue ;
<snip>
Quoted 32 lines · Nikolay Borisov
Review
Quoted 4 lines
(quoted diff) +1
-0+ spin_unlock(&fs_info->fs_roots_lock); btrfs_free_log_root_tree(NULL, fs_info); }nit: The xa_find/xa_for_each_start as used in this loop are really open-coded
xa_extract. So the code could be simplified even further:fs/btrfs/disk-io.c +4
-9@@ -4875,23 +4875,18 @@ static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info)static void btrfs_drop_all_logs (struct btrfs_fs_info *fs_info) {unsigned long index =0 ; +int grabbed =0 ; +struct btrfs_root *roots[8 ];spin_lock (&fs_info->fs_roots_lock); -while (xa_find (&fs_info->fs_roots, &index,ULONG_MAX ,XA_PRESENT )) { -struct btrfs_root *root; -struct btrfs_root *roots[8 ]; -int i; -int grabbed =0 ; +while ((grabbed =xa_extract (&fs_info->fs_roots, (void **)roots, index, +ULONG_MAX ,8 ,XA_PRESENT ))) { -xa_for_each_start (&fs_info->fs_roots, index, root, index) { - roots[grabbed] =btrfs_grab_root (root); - grabbed++; -if (grabbed >=ARRAY_SIZE (roots))
fs/btrfs/disk-io.c static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info) · line 4891
Quoted 10 lines
fs/btrfs/disk-io.c +3
-3-break ; - } +for (int i =0 ; i < grabbed; i++) + roots[i] =btrfs_grab_root (roots[i]);spin_unlock (&fs_info->fs_roots_lock); -for (i =0 ; i < grabbed; i++) { +for (int i =0 ; i < grabbed; i++) {if (!roots[i])continue ;
Folded to the patch, thanks.
Quoted 9 lines · Gabriel Niebler
… rename it to simply
fs_rootsand adjust all usages of this object to use the XArray API, because it is notionally easier to use and understand, as it provides array semantics, and also takes care of locking for us, further simplifying the code.Also do some refactoring, esp. where the API change requires largely rewriting some functions, anyway.
Signed-off-by Gabriel Niebler <[email protected]>
Updated in misc-next, thanks.