Skip to content

Commit 67d8b3a

Browse files
mssolakdave
authored andcommitted
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>
1 parent ad286e5 commit 67d8b3a

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

fs/btrfs/qgroup.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,14 @@ int btrfs_quota_enable(struct btrfs_fs_info *fs_info,
12631263
btrfs_end_transaction(trans);
12641264
else if (trans)
12651265
ret = btrfs_end_transaction(trans);
1266-
kfree(prealloc);
1266+
1267+
/*
1268+
* At this point we either failed at allocating prealloc, or we
1269+
* succeeded and passed the ownership to it to add_qgroup_rb(). In any
1270+
* case, this needs to be NULL or there is something wrong.
1271+
*/
1272+
ASSERT(prealloc == NULL);
1273+
12671274
return ret;
12681275
}
12691276

@@ -1695,7 +1702,12 @@ int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
16951702
ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
16961703
out:
16971704
mutex_unlock(&fs_info->qgroup_ioctl_lock);
1698-
kfree(prealloc);
1705+
/*
1706+
* At this point we either failed at allocating prealloc, or we
1707+
* succeeded and passed the ownership to it to add_qgroup_rb(). In any
1708+
* case, this needs to be NULL or there is something wrong.
1709+
*/
1710+
ASSERT(prealloc == NULL);
16991711
return ret;
17001712
}
17011713

@@ -3303,7 +3315,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
33033315
struct btrfs_root *quota_root;
33043316
struct btrfs_qgroup *srcgroup;
33053317
struct btrfs_qgroup *dstgroup;
3306-
struct btrfs_qgroup *prealloc;
3318+
struct btrfs_qgroup *prealloc = NULL;
33073319
struct btrfs_qgroup_list **qlist_prealloc = NULL;
33083320
bool free_inherit = false;
33093321
bool need_rescan = false;
@@ -3544,7 +3556,14 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
35443556
}
35453557
if (free_inherit)
35463558
kfree(inherit);
3547-
kfree(prealloc);
3559+
3560+
/*
3561+
* At this point we either failed at allocating prealloc, or we
3562+
* succeeded and passed the ownership to it to add_qgroup_rb(). In any
3563+
* case, this needs to be NULL or there is something wrong.
3564+
*/
3565+
ASSERT(prealloc == NULL);
3566+
35483567
return ret;
35493568
}
35503569

0 commit comments

Comments
 (0)