btrfs: send: make ensure_commit_roots_uptodate() simpler and more efficient
authorFilipe Manana <fdmanana@suse.com>
Tue, 21 May 2024 10:20:54 +0000 (11:20 +0100)
committerDavid Sterba <dsterba@suse.com>
Thu, 11 Jul 2024 13:33:19 +0000 (15:33 +0200)
Before starting a send operation we have to make sure that every root has
its commit root matching the regular root, to that send doesn't find stale
inodes in the commit root (inodes that were deleted in the regular root)
and fails the inode lookups with -ESTALE.

Currently we keep looking for roots used by the send operation and as soon
as we find one we commit the current transaction (or a new one since
btrfs_join_transaction() creates one if there isn't any running or the
running one is in a state >= TRANS_STATE_UNBLOCKED). It's pointless to
keep looking until we don't find any, because after the first transaction
commit all the other roots are updated too, as they were already tagged in
the fs_info->fs_roots_radix radix tree when they were modified in order to
have a commit root different from the regular root.

Currently we are also always passing the main send root into
btrfs_join_transaction(), which despite not having any functional issue,
it is not optimal because in case the root wasn't modified we end up
adding it to fs_info->fs_roots_radix and then update its root item in the
root tree when committing the transaction, causing unnecessary work.

So simplify and make this more efficient by removing the looping and by
passing the first root we found that is modified as the argument to
btrfs_join_transaction().

Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Reviewed-by: Qu Wenruo <wqu@suse.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/send.c

index c69743233be5553eb7b5f34420f94cb34bbf7942..2c46bd1c90d3424586ce4174e16de65bd4b3ee1d 100644 (file)
@@ -7998,32 +7998,29 @@ out:
  */
 static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
 {
-       int i;
-       struct btrfs_trans_handle *trans = NULL;
+       struct btrfs_trans_handle *trans;
+       struct btrfs_root *root = sctx->parent_root;
 
-again:
-       if (sctx->parent_root &&
-           sctx->parent_root->node != sctx->parent_root->commit_root)
+       if (root && root->node != root->commit_root)
                goto commit_trans;
 
-       for (i = 0; i < sctx->clone_roots_cnt; i++)
-               if (sctx->clone_roots[i].root->node !=
-                   sctx->clone_roots[i].root->commit_root)
+       for (int i = 0; i < sctx->clone_roots_cnt; i++) {
+               root = sctx->clone_roots[i].root;
+               if (root->node != root->commit_root)
                        goto commit_trans;
-
-       if (trans)
-               return btrfs_end_transaction(trans);
+       }
 
        return 0;
 
 commit_trans:
-       /* Use any root, all fs roots will get their commit roots updated. */
-       if (!trans) {
-               trans = btrfs_join_transaction(sctx->send_root);
-               if (IS_ERR(trans))
-                       return PTR_ERR(trans);
-               goto again;
-       }
+       /*
+        * Use the first root we found. We could use any but that would cause
+        * an unnecessary update of the root's item in the root tree when
+        * committing the transaction if that root wasn't changed before.
+        */
+       trans = btrfs_join_transaction(root);
+       if (IS_ERR(trans))
+               return PTR_ERR(trans);
 
        return btrfs_commit_transaction(trans);
 }