]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
btrfs: handle free space tree rebuild in multiple transactions
authorQu Wenruo <wqu@suse.com>
Thu, 19 Dec 2024 04:34:08 +0000 (15:04 +1030)
committerDavid Sterba <dsterba@suse.com>
Mon, 13 Jan 2025 13:53:18 +0000 (14:53 +0100)
During free space tree rebuild, we're holding a transaction handle for
the whole rebuild process.

This can lead to blocked task warning, e.g. btrfs-transaction kthread
(which is already created before btrfs_start_pre_rw_mount()) can be
waked up to join and commit the current transaction.

But the free space tree rebuild process may need to go through thousands
block groups, this will block btrfs-transaction kthread for a long time.

Fix the problem by calling btrfs_should_end_transaction() after each
block group, so that we won't hold the transaction handle too long.

And since the free-space-tree rebuild can be split into
multiple transactions, we need to consider the safety when the rebuild
process is interrupted.

Thankfully since we only set the FREE_SPACE_TREE compat_ro flag without
FREE_SPACE_TREE_VALID flag, even if the rebuild is interrupted, on the
next RW mount, we will still go rebuild the free space tree, by deleting
any items we have and re-starting the rebuild from scratch.

Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/free-space-tree.c

index 7ba50e133921a40cf3100da891a6a4850de90338..2400fa5a5be44382ae1a19b0fa130f3f531f2b98 100644 (file)
@@ -1350,6 +1350,12 @@ int btrfs_rebuild_free_space_tree(struct btrfs_fs_info *fs_info)
                        btrfs_end_transaction(trans);
                        return ret;
                }
+               if (btrfs_should_end_transaction(trans)) {
+                       btrfs_end_transaction(trans);
+                       trans = btrfs_start_transaction(free_space_root, 1);
+                       if (IS_ERR(trans))
+                               return PTR_ERR(trans);
+               }
                node = rb_next(node);
        }