Skip to content

Commit ad286e5

Browse files
mssolakdave
authored andcommitted
btrfs: apply the AUTO_K(V)FREE macros throughout the code
Apply the AUTO_KFREE and AUTO_KVFREE macros wherever it makes sense. Since this macro is expected to improve code readability, it has been avoided in places where the lifetime of objects wasn't easy to follow and a cleanup attribute would've made things worse; or when the cleanup section of a function involved many other things and thus there was no readability impact anyways. This change has also not been applied in extremely short functions where readability was clearly not an issue. 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 32e4a9d commit ad286e5

File tree

15 files changed

+106
-186
lines changed

15 files changed

+106
-186
lines changed

fs/btrfs/acl.c

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
#include "ctree.h"
1515
#include "xattr.h"
1616
#include "acl.h"
17+
#include "misc.h"
1718

1819
struct posix_acl *btrfs_get_acl(struct inode *inode, int type, bool rcu)
1920
{
2021
int size;
2122
const char *name;
22-
char *value = NULL;
23+
char AUTO_KFREE(value);
2324
struct posix_acl *acl;
2425

2526
if (rcu)
@@ -49,7 +50,6 @@ struct posix_acl *btrfs_get_acl(struct inode *inode, int type, bool rcu)
4950
acl = NULL;
5051
else
5152
acl = ERR_PTR(size);
52-
kfree(value);
5353

5454
return acl;
5555
}
@@ -59,7 +59,7 @@ int __btrfs_set_acl(struct btrfs_trans_handle *trans, struct inode *inode,
5959
{
6060
int ret, size = 0;
6161
const char *name;
62-
char *value = NULL;
62+
char AUTO_KFREE(value);
6363

6464
switch (type) {
6565
case ACL_TYPE_ACCESS:
@@ -85,28 +85,23 @@ int __btrfs_set_acl(struct btrfs_trans_handle *trans, struct inode *inode,
8585
nofs_flag = memalloc_nofs_save();
8686
value = kmalloc(size, GFP_KERNEL);
8787
memalloc_nofs_restore(nofs_flag);
88-
if (!value) {
89-
ret = -ENOMEM;
90-
goto out;
91-
}
88+
if (!value)
89+
return -ENOMEM;
9290

9391
ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
9492
if (ret < 0)
95-
goto out;
93+
return ret;
9694
}
9795

9896
if (trans)
9997
ret = btrfs_setxattr(trans, inode, name, value, size, 0);
10098
else
10199
ret = btrfs_setxattr_trans(inode, name, value, size, 0);
100+
if (ret < 0)
101+
return ret;
102102

103-
out:
104-
kfree(value);
105-
106-
if (!ret)
107-
set_cached_acl(inode, type, acl);
108-
109-
return ret;
103+
set_cached_acl(inode, type, acl);
104+
return 0;
110105
}
111106

112107
int btrfs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,

fs/btrfs/delayed-inode.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ static int btrfs_insert_delayed_item(struct btrfs_trans_handle *trans,
668668
struct btrfs_key first_key;
669669
const u32 first_data_size = first_item->data_len;
670670
int total_size;
671-
char *ins_data = NULL;
671+
char AUTO_KFREE(ins_data);
672672
int ret;
673673
bool continuous_keys_only = false;
674674

@@ -740,10 +740,8 @@ static int btrfs_insert_delayed_item(struct btrfs_trans_handle *trans,
740740

741741
ins_data = kmalloc_array(batch.nr,
742742
sizeof(u32) + sizeof(struct btrfs_key), GFP_NOFS);
743-
if (!ins_data) {
744-
ret = -ENOMEM;
745-
goto out;
746-
}
743+
if (!ins_data)
744+
return -ENOMEM;
747745
ins_sizes = (u32 *)ins_data;
748746
ins_keys = (struct btrfs_key *)(ins_data + batch.nr * sizeof(u32));
749747
batch.keys = ins_keys;
@@ -759,7 +757,7 @@ static int btrfs_insert_delayed_item(struct btrfs_trans_handle *trans,
759757

760758
ret = btrfs_insert_empty_items(trans, root, path, &batch);
761759
if (ret)
762-
goto out;
760+
return ret;
763761

764762
list_for_each_entry(curr, &item_list, tree_list) {
765763
char *data_ptr;
@@ -814,9 +812,8 @@ static int btrfs_insert_delayed_item(struct btrfs_trans_handle *trans,
814812
list_del(&curr->tree_list);
815813
btrfs_release_delayed_item(curr);
816814
}
817-
out:
818-
kfree(ins_data);
819-
return ret;
815+
816+
return 0;
820817
}
821818

822819
static int btrfs_insert_delayed_items(struct btrfs_trans_handle *trans,

fs/btrfs/extent-tree.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6058,7 +6058,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root, bool update_ref, bool for_reloc
60586058
struct btrfs_trans_handle *trans;
60596059
struct btrfs_root *tree_root = fs_info->tree_root;
60606060
struct btrfs_root_item *root_item = &root->root_item;
6061-
struct walk_control *wc;
6061+
struct walk_control AUTO_KFREE(wc);
60626062
struct btrfs_key key;
60636063
const u64 rootid = btrfs_root_id(root);
60646064
int ret = 0;
@@ -6076,9 +6076,8 @@ int btrfs_drop_snapshot(struct btrfs_root *root, bool update_ref, bool for_reloc
60766076

60776077
wc = kzalloc(sizeof(*wc), GFP_NOFS);
60786078
if (!wc) {
6079-
btrfs_free_path(path);
60806079
ret = -ENOMEM;
6081-
goto out;
6080+
goto out_free;
60826081
}
60836082

60846083
/*
@@ -6288,7 +6287,6 @@ int btrfs_drop_snapshot(struct btrfs_root *root, bool update_ref, bool for_reloc
62886287

62896288
btrfs_end_transaction_throttle(trans);
62906289
out_free:
6291-
kfree(wc);
62926290
btrfs_free_path(path);
62936291
out:
62946292
if (!ret && root_dropped) {
@@ -6331,7 +6329,7 @@ int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
63316329
{
63326330
struct btrfs_fs_info *fs_info = root->fs_info;
63336331
BTRFS_PATH_AUTO_FREE(path);
6334-
struct walk_control *wc;
6332+
struct walk_control AUTO_KFREE(wc);
63356333
int level;
63366334
int parent_level;
63376335
int ret = 0;
@@ -6370,18 +6368,17 @@ int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
63706368
while (1) {
63716369
ret = walk_down_tree(trans, root, path, wc);
63726370
if (ret < 0)
6373-
break;
6371+
return ret;
63746372

63756373
ret = walk_up_tree(trans, root, path, wc, parent_level);
63766374
if (ret) {
6377-
if (ret > 0)
6378-
ret = 0;
6375+
if (ret < 0)
6376+
return ret;
63796377
break;
63806378
}
63816379
}
63826380

6383-
kfree(wc);
6384-
return ret;
6381+
return 0;
63856382
}
63866383

63876384
/*

fs/btrfs/ioctl.c

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ static noinline int create_subvol(struct mnt_idmap *idmap,
503503
struct btrfs_fs_info *fs_info = inode_to_fs_info(dir);
504504
struct btrfs_trans_handle *trans;
505505
struct btrfs_key key;
506-
struct btrfs_root_item *root_item;
506+
struct btrfs_root_item AUTO_KFREE(root_item);
507507
struct btrfs_inode_item *inode_item;
508508
struct extent_buffer *leaf;
509509
struct btrfs_root *root = BTRFS_I(dir)->root;
@@ -527,20 +527,18 @@ static noinline int create_subvol(struct mnt_idmap *idmap,
527527

528528
ret = btrfs_get_free_objectid(fs_info->tree_root, &objectid);
529529
if (ret)
530-
goto out_root_item;
530+
return ret;
531531

532532
/*
533533
* Don't create subvolume whose level is not zero. Or qgroup will be
534534
* screwed up since it assumes subvolume qgroup's level to be 0.
535535
*/
536-
if (btrfs_qgroup_level(objectid)) {
537-
ret = -ENOSPC;
538-
goto out_root_item;
539-
}
536+
if (btrfs_qgroup_level(objectid))
537+
return -ENOSPC;
540538

541539
ret = get_anon_bdev(&anon_dev);
542540
if (ret < 0)
543-
goto out_root_item;
541+
return ret;
544542

545543
new_inode_args.inode = btrfs_new_subvol_inode(idmap, dir);
546544
if (!new_inode_args.inode) {
@@ -692,8 +690,7 @@ static noinline int create_subvol(struct mnt_idmap *idmap,
692690
out_anon_dev:
693691
if (anon_dev)
694692
free_anon_bdev(anon_dev);
695-
out_root_item:
696-
kfree(root_item);
693+
697694
return ret;
698695
}
699696

@@ -2956,7 +2953,7 @@ static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
29562953
struct btrfs_ioctl_space_args space_args = { 0 };
29572954
struct btrfs_ioctl_space_info space;
29582955
struct btrfs_ioctl_space_info *dest;
2959-
struct btrfs_ioctl_space_info *dest_orig;
2956+
struct btrfs_ioctl_space_info AUTO_KFREE(dest_orig);
29602957
struct btrfs_ioctl_space_info __user *user_dest;
29612958
struct btrfs_space_info *info;
29622959
static const u64 types[] = {
@@ -3077,9 +3074,8 @@ static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
30773074
(arg + sizeof(struct btrfs_ioctl_space_args));
30783075

30793076
if (copy_to_user(user_dest, dest_orig, alloc_size))
3080-
ret = -EFAULT;
3077+
return -EFAULT;
30813078

3082-
kfree(dest_orig);
30833079
out:
30843080
if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
30853081
ret = -EFAULT;
@@ -3610,7 +3606,7 @@ static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
36103606
static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
36113607
void __user *arg)
36123608
{
3613-
struct btrfs_ioctl_balance_args *bargs;
3609+
struct btrfs_ioctl_balance_args AUTO_KFREE(bargs);
36143610
int ret = 0;
36153611

36163612
if (!capable(CAP_SYS_ADMIN))
@@ -3632,8 +3628,6 @@ static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
36323628

36333629
if (copy_to_user(arg, bargs, sizeof(*bargs)))
36343630
ret = -EFAULT;
3635-
3636-
kfree(bargs);
36373631
out:
36383632
mutex_unlock(&fs_info->balance_mutex);
36393633
return ret;
@@ -4227,20 +4221,19 @@ static int check_feature_bits(const struct btrfs_fs_info *fs_info,
42274221
u64 safe_set, u64 safe_clear)
42284222
{
42294223
const char *type = btrfs_feature_set_name(set);
4230-
char *names;
4224+
const char AUTO_KFREE(names);
42314225
u64 disallowed, unsupported;
42324226
u64 set_mask = flags & change_mask;
42334227
u64 clear_mask = ~flags & change_mask;
42344228

42354229
unsupported = set_mask & ~supported_flags;
42364230
if (unsupported) {
42374231
names = btrfs_printable_features(set, unsupported);
4238-
if (names) {
4232+
if (names)
42394233
btrfs_warn(fs_info,
42404234
"this kernel does not support the %s feature bit%s",
42414235
names, strchr(names, ',') ? "s" : "");
4242-
kfree(names);
4243-
} else
4236+
else
42444237
btrfs_warn(fs_info,
42454238
"this kernel does not support %s bits 0x%llx",
42464239
type, unsupported);
@@ -4250,12 +4243,11 @@ static int check_feature_bits(const struct btrfs_fs_info *fs_info,
42504243
disallowed = set_mask & ~safe_set;
42514244
if (disallowed) {
42524245
names = btrfs_printable_features(set, disallowed);
4253-
if (names) {
4246+
if (names)
42544247
btrfs_warn(fs_info,
42554248
"can't set the %s feature bit%s while mounted",
42564249
names, strchr(names, ',') ? "s" : "");
4257-
kfree(names);
4258-
} else
4250+
else
42594251
btrfs_warn(fs_info,
42604252
"can't set %s bits 0x%llx while mounted",
42614253
type, disallowed);
@@ -4265,12 +4257,11 @@ static int check_feature_bits(const struct btrfs_fs_info *fs_info,
42654257
disallowed = clear_mask & ~safe_clear;
42664258
if (disallowed) {
42674259
names = btrfs_printable_features(set, disallowed);
4268-
if (names) {
4260+
if (names)
42694261
btrfs_warn(fs_info,
42704262
"can't clear the %s feature bit%s while mounted",
42714263
names, strchr(names, ',') ? "s" : "");
4272-
kfree(names);
4273-
} else
4264+
else
42744265
btrfs_warn(fs_info,
42754266
"can't clear %s bits 0x%llx while mounted",
42764267
type, disallowed);

fs/btrfs/qgroup.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4794,7 +4794,7 @@ int btrfs_qgroup_trace_subtree_after_cow(struct btrfs_trans_handle *trans,
47944794
struct btrfs_fs_info *fs_info = root->fs_info;
47954795
struct btrfs_tree_parent_check check = { 0 };
47964796
struct btrfs_qgroup_swapped_blocks *blocks = &root->swapped_blocks;
4797-
struct btrfs_qgroup_swapped_block *block;
4797+
struct btrfs_qgroup_swapped_block AUTO_KFREE(block);
47984798
struct extent_buffer *reloc_eb = NULL;
47994799
struct rb_node *node;
48004800
bool swapped = false;
@@ -4851,7 +4851,6 @@ int btrfs_qgroup_trace_subtree_after_cow(struct btrfs_trans_handle *trans,
48514851
ret = qgroup_trace_subtree_swap(trans, reloc_eb, subvol_eb,
48524852
block->last_snapshot, block->trace_leaf);
48534853
free_out:
4854-
kfree(block);
48554854
free_extent_buffer(reloc_eb);
48564855
out:
48574856
if (ret < 0) {

fs/btrfs/raid-stripe-tree.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static int btrfs_partially_delete_raid_extent(struct btrfs_trans_handle *trans,
1919
u64 newlen, u64 frontpad)
2020
{
2121
struct btrfs_root *stripe_root = trans->fs_info->stripe_root;
22-
struct btrfs_stripe_extent *extent, *newitem;
22+
struct btrfs_stripe_extent *extent, AUTO_KFREE(newitem);
2323
struct extent_buffer *leaf;
2424
int slot;
2525
size_t item_size;
@@ -53,14 +53,10 @@ static int btrfs_partially_delete_raid_extent(struct btrfs_trans_handle *trans,
5353

5454
ret = btrfs_del_item(trans, stripe_root, path);
5555
if (ret)
56-
goto out;
56+
return ret;
5757

5858
btrfs_release_path(path);
59-
ret = btrfs_insert_item(trans, stripe_root, &newkey, newitem, item_size);
60-
61-
out:
62-
kfree(newitem);
63-
return ret;
59+
return btrfs_insert_item(trans, stripe_root, &newkey, newitem, item_size);
6460
}
6561

6662
int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 length)
@@ -299,7 +295,7 @@ int btrfs_insert_one_raid_extent(struct btrfs_trans_handle *trans,
299295
struct btrfs_key stripe_key;
300296
struct btrfs_root *stripe_root = fs_info->stripe_root;
301297
const int num_stripes = btrfs_bg_type_to_factor(bioc->map_type);
302-
struct btrfs_stripe_extent *stripe_extent;
298+
struct btrfs_stripe_extent AUTO_KFREE(stripe_extent);
303299
const size_t item_size = struct_size(stripe_extent, strides, num_stripes);
304300
int ret;
305301

@@ -336,8 +332,6 @@ int btrfs_insert_one_raid_extent(struct btrfs_trans_handle *trans,
336332
btrfs_abort_transaction(trans, ret);
337333
}
338334

339-
kfree(stripe_extent);
340-
341335
return ret;
342336
}
343337

fs/btrfs/reflink.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ static int btrfs_clone(struct inode *src, struct inode *inode,
343343
BTRFS_PATH_AUTO_FREE(path);
344344
struct extent_buffer *leaf;
345345
struct btrfs_trans_handle *trans;
346-
char *buf = NULL;
346+
char AUTO_KVFREE(buf);
347347
struct btrfs_key key;
348348
u32 nritems;
349349
int slot;
@@ -358,10 +358,8 @@ static int btrfs_clone(struct inode *src, struct inode *inode,
358358
return ret;
359359

360360
path = btrfs_alloc_path();
361-
if (!path) {
362-
kvfree(buf);
361+
if (!path)
363362
return ret;
364-
}
365363

366364
path->reada = READA_FORWARD;
367365
/* Clone data */
@@ -611,7 +609,6 @@ static int btrfs_clone(struct inode *src, struct inode *inode,
611609
}
612610

613611
out:
614-
kvfree(buf);
615612
clear_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &BTRFS_I(inode)->runtime_flags);
616613

617614
return ret;

0 commit comments

Comments
 (0)