btrfs: simplify cow only root list extraction during transaction commit
authorFilipe Manana <fdmanana@suse.com>
Thu, 1 May 2025 11:47:35 +0000 (12:47 +0100)
committerDavid Sterba <dsterba@suse.com>
Thu, 15 May 2025 12:30:54 +0000 (14:30 +0200)
There's no need to keep a local variable to extract the first member of
the list and then do a list_entry() call, we can use list_first_entry()
instead, removing the need for the temporary variable and extracting the
first element in a single step.

Also, there's no need to do a list_del_init() followed by list_add_tail(),
instead we can use list_move_tail(). We are in transaction commit critical
section where we don't need to worry about concurrency and that's why we
don't take any locks and can use list_move_tail() (we do assert early at
commit_cowonly_roots() that we are in the critical section, that the
transaction's state is TRANS_STATE_COMMIT_DOING).

Reviewed-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/transaction.c

index 1212b67a91aac9cf9b61a57aed43129fe025bf73..b96195d6480f14e9ad2ec05217a26e5cf9a7fda5 100644 (file)
@@ -1327,7 +1327,6 @@ static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans)
        struct btrfs_fs_info *fs_info = trans->fs_info;
        struct list_head *dirty_bgs = &trans->transaction->dirty_bgs;
        struct list_head *io_bgs = &trans->transaction->io_bgs;
-       struct list_head *next;
        struct extent_buffer *eb;
        int ret;
 
@@ -1363,13 +1362,13 @@ static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans)
 again:
        while (!list_empty(&fs_info->dirty_cowonly_roots)) {
                struct btrfs_root *root;
-               next = fs_info->dirty_cowonly_roots.next;
-               list_del_init(next);
-               root = list_entry(next, struct btrfs_root, dirty_list);
+
+               root = list_first_entry(&fs_info->dirty_cowonly_roots,
+                                       struct btrfs_root, dirty_list);
                clear_bit(BTRFS_ROOT_DIRTY, &root->state);
+               list_move_tail(&root->dirty_list,
+                              &trans->transaction->switch_commits);
 
-               list_add_tail(&root->dirty_list,
-                             &trans->transaction->switch_commits);
                ret = update_cowonly_root(trans, root);
                if (ret)
                        return ret;