]> git.hungrycats.org Git - linux/commit
btrfs: cleanup dirty_cowonly_roots during btrfs_free_fs_roots()
authorQu Wenruo <wqu@suse.com>
Tue, 13 Oct 2026 05:41:20 +0000 (16:11 +1030)
committerDavid Sterba <dsterba@suse.com>
Fri, 16 Oct 2026 16:50:22 +0000 (18:50 +0200)
commite37fb4ac525bf3e0c9b4e481ac0e9c566b454fb3
tree578426a387909eab8429d92e606a857e2da6f37b
parent4a128ef18b0c907ecf888d4df93df38ad112d333
btrfs: cleanup dirty_cowonly_roots during btrfs_free_fs_roots()

[EXISTING ERROR HANDLING QUIRK]
Sashiko mentioned an existing problem that, if btrfs_abort_transaction()
is triggered during the following call site, some roots will be attached
to dirty_cowonly_roots without being cleaned up:

 btrfs_commit_transaction()
 |- commit_cowonly_roots()
    |- list_move_tail();
    |  One root's dirty_list is moved from dirty_cowonly_roots to
    |  switch_commits.
    |  But there are still other dirty roots in dirty_cowonly_roots.
    |
    |- update_cowonly_root()
    |  |- btrfs_update_root()
    |     |- btrfs_abort_transaction()
    |
    |- return ret;

In that case, those dirty roots linked into dirty_cowonly_roots will
never be removed from that list.
As normally we require a transaction commit to move them, but since the
fs is already marked error, no new transaction can be started.

For roots that are already moved to switch_commits list, they are
cleaned up by btrfs_cleanup_one_transaction().

This is not going to cause memory leak on the btrfs_root structure
itself, as after a transaction is aborted, no one will read
dirty_cowonly_roots.

Furthermore dirty_cowonly_roots only tracks trees that are not
subvolume or log trees. They should not have root->dirty_log_pages.
And qgroup swapped blocks are going to be released during
btrfs_put_root().

Thus no memory leak from those two members either.

But we're still relying on a hidden behavior to keep us from leaking
memory.

[ENHANCEMENT]
Unlink all roots that are still linked to dirty_cowonly_roots during
btrfs_free_fs_roots(), and do one extra ASSERT() to make sure that their
dirty_log_pages are empty.

At that timing there is no other thread thus we're safe to unlink roots
without holding trans_lock.

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/disk-io.c