]> git.hungrycats.org Git - linux/commitdiff
btrfs: always remove quota_root from dirty_cowonly_roots
authorQu Wenruo <wqu@suse.com>
Tue, 13 Oct 2026 05:41:17 +0000 (16:11 +1030)
committerDavid Sterba <dsterba@suse.com>
Fri, 30 Oct 2026 20:36:16 +0000 (21:36 +0100)
[ERROR HANDLING QUIRK]
When btrfs_quota_disable() failed at btrfs_clean_quota_tree() or
btrfs_del_root(), the quota_root->dirty_list is not removed from
fs_info->dirty_cowonly_roots, but the quota_root itself will be freed.

This leaves a dangling pointer in the dirty_cowonly_roots.

This is not a huge problem, as the transaction is aborted thus no one
will read the dirty_cowonly_roots for now.

Furthermore, quota_root should never be logged nor have qgroup swapped
tree blocks, thus it won't cause leaks from extent io tree nor qgroup
swapped blocks.

[ENHANCEMENT]
For the incoming cleanup of dirty_cowonly_roots during
btrfs_free_fs_roots(), any root that is still linked to
dirty_cowonly_roots will be read again, and this existing dangling
pointer is going to cause problem.

To prepare for that cleanup, move the list_del() call into the out
label, so that even if the transaction is aborted, we still unlink
quota_root from dirty_cowonly_roots before freeing it.

Assisted-by: LLM (exposing the bug during review)
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/qgroup.c

index a0651fbfe778205185e889b309746310af489335..baef546fa213e3f9745cedd729939865bc1976ea 100644 (file)
@@ -1440,10 +1440,6 @@ int btrfs_quota_disable(struct btrfs_fs_info *fs_info)
                goto out;
        }
 
-       spin_lock(&fs_info->trans_lock);
-       list_del(&quota_root->dirty_list);
-       spin_unlock(&fs_info->trans_lock);
-
        btrfs_tree_lock(quota_root->node);
        btrfs_clear_buffer_dirty(trans, quota_root->node);
        btrfs_tree_unlock(quota_root->node);
@@ -1454,7 +1450,13 @@ int btrfs_quota_disable(struct btrfs_fs_info *fs_info)
                btrfs_abort_transaction(trans, ret);
 
 out:
-       btrfs_put_root(quota_root);
+       if (quota_root) {
+               spin_lock(&fs_info->trans_lock);
+               list_del(&quota_root->dirty_list);
+               spin_unlock(&fs_info->trans_lock);
+               btrfs_put_root(quota_root);
+       }
+
        mutex_unlock(&fs_info->qgroup_ioctl_lock);
        if (ret && trans)
                btrfs_end_transaction(trans);