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>