btrfs: add ASSERTs on prealloc in qgroup functions

The prealloc variable in these functions is always initialized to
NULL. Whenever we allocate memory for it, if it fails then NULL is
preserved, otherwise we delegate the ownership of the pointer to
add_qgroup_rb() and set it right after to NULL.

Since in any case the pointer ends up being NULL at the end of its
usage, we can safely remove calls to kfree() for it, while adding an
ASSERT as an extra check.

Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Miquel Sabaté Solà
2025-10-24 12:21:43 +02:00
committed by David Sterba
parent 7ab5d01d58
commit 252877a870

View File

@@ -1263,7 +1263,14 @@ out:
btrfs_end_transaction(trans);
else if (trans)
ret = btrfs_end_transaction(trans);
kfree(prealloc);
/*
* At this point we either failed at allocating prealloc, or we
* succeeded and passed the ownership to it to add_qgroup_rb(). In any
* case, this needs to be NULL or there is something wrong.
*/
ASSERT(prealloc == NULL);
return ret;
}
@@ -1695,7 +1702,12 @@ int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
out:
mutex_unlock(&fs_info->qgroup_ioctl_lock);
kfree(prealloc);
/*
* At this point we either failed at allocating prealloc, or we
* succeeded and passed the ownership to it to add_qgroup_rb(). In any
* case, this needs to be NULL or there is something wrong.
*/
ASSERT(prealloc == NULL);
return ret;
}
@@ -3303,7 +3315,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
struct btrfs_root *quota_root;
struct btrfs_qgroup *srcgroup;
struct btrfs_qgroup *dstgroup;
struct btrfs_qgroup *prealloc;
struct btrfs_qgroup *prealloc = NULL;
struct btrfs_qgroup_list **qlist_prealloc = NULL;
bool free_inherit = false;
bool need_rescan = false;
@@ -3544,7 +3556,14 @@ out:
}
if (free_inherit)
kfree(inherit);
kfree(prealloc);
/*
* At this point we either failed at allocating prealloc, or we
* succeeded and passed the ownership to it to add_qgroup_rb(). In any
* case, this needs to be NULL or there is something wrong.
*/
ASSERT(prealloc == NULL);
return ret;
}