Commit 2b8aa78c authored by Boris Burkov's avatar Boris Burkov Committed by David Sterba

btrfs: qgroup: fix qgroup id collision across mounts

If we delete subvolumes whose ID is the largest in the filesystem, then
unmount and mount again, then btrfs_init_root_free_objectid on the
tree_root will select a subvolid smaller than that one and thus allow
reusing it.

If we are also using qgroups (and particularly squotas) it is possible
to delete the subvol without deleting the qgroup. In that case, we will
be able to create a new subvol whose id already has a level 0 qgroup.
This will result in re-using that qgroup which would then lead to
incorrect accounting.

Fixes: 6ed05643 ("btrfs: create qgroup earlier in snapshot creation")
CC: stable@vger.kernel.org # 6.7+
Reviewed-by: default avatarQu Wenruo <wqu@suse.com>
Signed-off-by: default avatarBoris Burkov <boris@bur.io>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 1fa7603d
...@@ -468,6 +468,7 @@ int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info) ...@@ -468,6 +468,7 @@ int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
} }
if (!qgroup) { if (!qgroup) {
struct btrfs_qgroup *prealloc; struct btrfs_qgroup *prealloc;
struct btrfs_root *tree_root = fs_info->tree_root;
prealloc = kzalloc(sizeof(*prealloc), GFP_KERNEL); prealloc = kzalloc(sizeof(*prealloc), GFP_KERNEL);
if (!prealloc) { if (!prealloc) {
...@@ -475,6 +476,25 @@ int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info) ...@@ -475,6 +476,25 @@ int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
goto out; goto out;
} }
qgroup = add_qgroup_rb(fs_info, prealloc, found_key.offset); qgroup = add_qgroup_rb(fs_info, prealloc, found_key.offset);
/*
* If a qgroup exists for a subvolume ID, it is possible
* that subvolume has been deleted, in which case
* re-using that ID would lead to incorrect accounting.
*
* Ensure that we skip any such subvol ids.
*
* We don't need to lock because this is only called
* during mount before we start doing things like creating
* subvolumes.
*/
if (is_fstree(qgroup->qgroupid) &&
qgroup->qgroupid > tree_root->free_objectid)
/*
* Don't need to check against BTRFS_LAST_FREE_OBJECTID,
* as it will get checked on the next call to
* btrfs_get_free_objectid.
*/
tree_root->free_objectid = qgroup->qgroupid + 1;
} }
ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup); ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
if (ret < 0) if (ret < 0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment