Skip to content

Commit 44259c9

Browse files
SaltyKitkatkdave
authored andcommitted
btrfs: more trivial BTRFS_PATH_AUTO_FREE conversions
Convert more of the trivial pattern for the auto freeing of btrfs_path with goto -> return conversions where applicable. Signed-off-by: Sun YangKai <sunk67188@gmail.com> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 7924452 commit 44259c9

File tree

4 files changed

+119
-219
lines changed

4 files changed

+119
-219
lines changed

fs/btrfs/uuid-tree.c

Lines changed: 43 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,26 @@ static int btrfs_uuid_tree_lookup(struct btrfs_root *uuid_root, const u8 *uuid,
2727
u8 type, u64 subid)
2828
{
2929
int ret;
30-
struct btrfs_path *path = NULL;
30+
BTRFS_PATH_AUTO_FREE(path);
3131
struct extent_buffer *eb;
3232
int slot;
3333
u32 item_size;
3434
unsigned long offset;
3535
struct btrfs_key key;
3636

37-
if (WARN_ON_ONCE(!uuid_root)) {
38-
ret = -ENOENT;
39-
goto out;
40-
}
37+
if (WARN_ON_ONCE(!uuid_root))
38+
return -ENOENT;
4139

4240
path = btrfs_alloc_path();
43-
if (!path) {
44-
ret = -ENOMEM;
45-
goto out;
46-
}
41+
if (!path)
42+
return -ENOMEM;
4743

4844
btrfs_uuid_to_key(uuid, type, &key);
4945
ret = btrfs_search_slot(NULL, uuid_root, &key, path, 0, 0);
50-
if (ret < 0) {
51-
goto out;
52-
} else if (ret > 0) {
53-
ret = -ENOENT;
54-
goto out;
55-
}
46+
if (ret < 0)
47+
return ret;
48+
if (ret > 0)
49+
return -ENOENT;
5650

5751
eb = path->nodes[0];
5852
slot = path->slots[0];
@@ -64,7 +58,7 @@ static int btrfs_uuid_tree_lookup(struct btrfs_root *uuid_root, const u8 *uuid,
6458
btrfs_warn(uuid_root->fs_info,
6559
"uuid item with illegal size %lu!",
6660
(unsigned long)item_size);
67-
goto out;
61+
return ret;
6862
}
6963
while (item_size) {
7064
__le64 data;
@@ -78,8 +72,6 @@ static int btrfs_uuid_tree_lookup(struct btrfs_root *uuid_root, const u8 *uuid,
7872
item_size -= sizeof(data);
7973
}
8074

81-
out:
82-
btrfs_free_path(path);
8375
return ret;
8476
}
8577

@@ -89,7 +81,7 @@ int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, const u8 *uuid, u8 typ
8981
struct btrfs_fs_info *fs_info = trans->fs_info;
9082
struct btrfs_root *uuid_root = fs_info->uuid_root;
9183
int ret;
92-
struct btrfs_path *path = NULL;
84+
BTRFS_PATH_AUTO_FREE(path);
9385
struct btrfs_key key;
9486
struct extent_buffer *eb;
9587
int slot;
@@ -100,18 +92,14 @@ int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, const u8 *uuid, u8 typ
10092
if (ret != -ENOENT)
10193
return ret;
10294

103-
if (WARN_ON_ONCE(!uuid_root)) {
104-
ret = -EINVAL;
105-
goto out;
106-
}
95+
if (WARN_ON_ONCE(!uuid_root))
96+
return -EINVAL;
10797

10898
btrfs_uuid_to_key(uuid, type, &key);
10999

110100
path = btrfs_alloc_path();
111-
if (!path) {
112-
ret = -ENOMEM;
113-
goto out;
114-
}
101+
if (!path)
102+
return -ENOMEM;
115103

116104
ret = btrfs_insert_empty_item(trans, uuid_root, path, &key,
117105
sizeof(subid_le));
@@ -134,15 +122,12 @@ int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, const u8 *uuid, u8 typ
134122
btrfs_warn(fs_info,
135123
"insert uuid item failed %d (0x%016llx, 0x%016llx) type %u!",
136124
ret, key.objectid, key.offset, type);
137-
goto out;
125+
return ret;
138126
}
139127

140-
ret = 0;
141128
subid_le = cpu_to_le64(subid_cpu);
142129
write_extent_buffer(eb, &subid_le, offset, sizeof(subid_le));
143-
out:
144-
btrfs_free_path(path);
145-
return ret;
130+
return 0;
146131
}
147132

148133
int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, const u8 *uuid, u8 type,
@@ -151,7 +136,7 @@ int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, const u8 *uuid, u8
151136
struct btrfs_fs_info *fs_info = trans->fs_info;
152137
struct btrfs_root *uuid_root = fs_info->uuid_root;
153138
int ret;
154-
struct btrfs_path *path = NULL;
139+
BTRFS_PATH_AUTO_FREE(path);
155140
struct btrfs_key key;
156141
struct extent_buffer *eb;
157142
int slot;
@@ -161,29 +146,23 @@ int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, const u8 *uuid, u8
161146
unsigned long move_src;
162147
unsigned long move_len;
163148

164-
if (WARN_ON_ONCE(!uuid_root)) {
165-
ret = -EINVAL;
166-
goto out;
167-
}
149+
if (WARN_ON_ONCE(!uuid_root))
150+
return -EINVAL;
168151

169152
btrfs_uuid_to_key(uuid, type, &key);
170153

171154
path = btrfs_alloc_path();
172-
if (!path) {
173-
ret = -ENOMEM;
174-
goto out;
175-
}
155+
if (!path)
156+
return -ENOMEM;
176157

177158
ret = btrfs_search_slot(trans, uuid_root, &key, path, -1, 1);
178159
if (ret < 0) {
179160
btrfs_warn(fs_info, "error %d while searching for uuid item!",
180161
ret);
181-
goto out;
182-
}
183-
if (ret > 0) {
184-
ret = -ENOENT;
185-
goto out;
162+
return ret;
186163
}
164+
if (ret > 0)
165+
return -ENOENT;
187166

188167
eb = path->nodes[0];
189168
slot = path->slots[0];
@@ -192,8 +171,7 @@ int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, const u8 *uuid, u8
192171
if (!IS_ALIGNED(item_size, sizeof(u64))) {
193172
btrfs_warn(fs_info, "uuid item with illegal size %lu!",
194173
(unsigned long)item_size);
195-
ret = -ENOENT;
196-
goto out;
174+
return -ENOENT;
197175
}
198176
while (item_size) {
199177
__le64 read_subid;
@@ -205,26 +183,20 @@ int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, const u8 *uuid, u8
205183
item_size -= sizeof(read_subid);
206184
}
207185

208-
if (!item_size) {
209-
ret = -ENOENT;
210-
goto out;
211-
}
186+
if (!item_size)
187+
return -ENOENT;
212188

213189
item_size = btrfs_item_size(eb, slot);
214-
if (item_size == sizeof(subid)) {
215-
ret = btrfs_del_item(trans, uuid_root, path);
216-
goto out;
217-
}
190+
if (item_size == sizeof(subid))
191+
return btrfs_del_item(trans, uuid_root, path);
218192

219193
move_dst = offset;
220194
move_src = offset + sizeof(subid);
221195
move_len = item_size - (move_src - btrfs_item_ptr_offset(eb, slot));
222196
memmove_extent_buffer(eb, move_dst, move_src, move_len);
223197
btrfs_truncate_item(trans, path, item_size - sizeof(subid), 1);
224198

225-
out:
226-
btrfs_free_path(path);
227-
return ret;
199+
return 0;
228200
}
229201

230202
static int btrfs_uuid_iter_rem(struct btrfs_root *uuid_root, u8 *uuid, u8 type,
@@ -293,36 +265,32 @@ int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info)
293265
{
294266
struct btrfs_root *root = fs_info->uuid_root;
295267
struct btrfs_key key;
296-
struct btrfs_path *path;
268+
BTRFS_PATH_AUTO_FREE(path);
297269
int ret = 0;
298270
struct extent_buffer *leaf;
299271
int slot;
300272
u32 item_size;
301273
unsigned long offset;
302274

303275
path = btrfs_alloc_path();
304-
if (!path) {
305-
ret = -ENOMEM;
306-
goto out;
307-
}
276+
if (!path)
277+
return -ENOMEM;
308278

309279
key.objectid = 0;
310280
key.type = 0;
311281
key.offset = 0;
312282

313283
again_search_slot:
314284
ret = btrfs_search_forward(root, &key, path, BTRFS_OLDEST_GENERATION);
315-
if (ret) {
316-
if (ret > 0)
317-
ret = 0;
318-
goto out;
319-
}
285+
if (ret < 0)
286+
return ret;
287+
if (ret > 0)
288+
return 0;
320289

321290
while (1) {
322-
if (btrfs_fs_closing(fs_info)) {
323-
ret = -EINTR;
324-
goto out;
325-
}
291+
if (btrfs_fs_closing(fs_info))
292+
return -EINTR;
293+
326294
cond_resched();
327295
leaf = path->nodes[0];
328296
slot = path->slots[0];
@@ -353,7 +321,7 @@ int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info)
353321
ret = btrfs_check_uuid_tree_entry(fs_info, uuid,
354322
key.type, subid_cpu);
355323
if (ret < 0)
356-
goto out;
324+
return ret;
357325
if (ret > 0) {
358326
btrfs_release_path(path);
359327
ret = btrfs_uuid_iter_rem(root, uuid, key.type,
@@ -369,7 +337,7 @@ int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info)
369337
goto again_search_slot;
370338
}
371339
if (ret < 0 && ret != -ENOENT)
372-
goto out;
340+
return ret;
373341
key.offset++;
374342
goto again_search_slot;
375343
}
@@ -386,8 +354,6 @@ int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info)
386354
break;
387355
}
388356

389-
out:
390-
btrfs_free_path(path);
391357
return ret;
392358
}
393359

fs/btrfs/verity.c

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ static int drop_verity_items(struct btrfs_inode *inode, u8 key_type)
109109
{
110110
struct btrfs_trans_handle *trans;
111111
struct btrfs_root *root = inode->root;
112-
struct btrfs_path *path;
112+
BTRFS_PATH_AUTO_FREE(path);
113113
struct btrfs_key key;
114114
int count = 0;
115115
int ret;
@@ -121,10 +121,8 @@ static int drop_verity_items(struct btrfs_inode *inode, u8 key_type)
121121
while (1) {
122122
/* 1 for the item being dropped */
123123
trans = btrfs_start_transaction(root, 1);
124-
if (IS_ERR(trans)) {
125-
ret = PTR_ERR(trans);
126-
goto out;
127-
}
124+
if (IS_ERR(trans))
125+
return PTR_ERR(trans);
128126

129127
/*
130128
* Walk backwards through all the items until we find one that
@@ -143,7 +141,7 @@ static int drop_verity_items(struct btrfs_inode *inode, u8 key_type)
143141
path->slots[0]--;
144142
} else if (ret < 0) {
145143
btrfs_end_transaction(trans);
146-
goto out;
144+
return ret;
147145
}
148146

149147
btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
@@ -161,17 +159,14 @@ static int drop_verity_items(struct btrfs_inode *inode, u8 key_type)
161159
ret = btrfs_del_items(trans, root, path, path->slots[0], 1);
162160
if (ret) {
163161
btrfs_end_transaction(trans);
164-
goto out;
162+
return ret;
165163
}
166164
count++;
167165
btrfs_release_path(path);
168166
btrfs_end_transaction(trans);
169167
}
170-
ret = count;
171168
btrfs_end_transaction(trans);
172-
out:
173-
btrfs_free_path(path);
174-
return ret;
169+
return count;
175170
}
176171

177172
/*
@@ -217,7 +212,7 @@ static int write_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset,
217212
const char *src, u64 len)
218213
{
219214
struct btrfs_trans_handle *trans;
220-
struct btrfs_path *path;
215+
BTRFS_PATH_AUTO_FREE(path);
221216
struct btrfs_root *root = inode->root;
222217
struct extent_buffer *leaf;
223218
struct btrfs_key key;
@@ -233,10 +228,8 @@ static int write_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset,
233228
while (len > 0) {
234229
/* 1 for the new item being inserted */
235230
trans = btrfs_start_transaction(root, 1);
236-
if (IS_ERR(trans)) {
237-
ret = PTR_ERR(trans);
238-
break;
239-
}
231+
if (IS_ERR(trans))
232+
return PTR_ERR(trans);
240233

241234
key.objectid = btrfs_ino(inode);
242235
key.type = key_type;
@@ -267,7 +260,6 @@ static int write_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset,
267260
btrfs_end_transaction(trans);
268261
}
269262

270-
btrfs_free_path(path);
271263
return ret;
272264
}
273265

@@ -296,7 +288,7 @@ static int write_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset,
296288
static int read_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset,
297289
char *dest, u64 len, struct folio *dest_folio)
298290
{
299-
struct btrfs_path *path;
291+
BTRFS_PATH_AUTO_FREE(path);
300292
struct btrfs_root *root = inode->root;
301293
struct extent_buffer *leaf;
302294
struct btrfs_key key;
@@ -404,7 +396,6 @@ static int read_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset,
404396
}
405397
}
406398
out:
407-
btrfs_free_path(path);
408399
if (!ret)
409400
ret = copied;
410401
return ret;

0 commit comments

Comments
 (0)