Skip to contentSkip to conversation

btrfs: more trivial BTRFS_PATH_AUTO_FREE conversions

PATCH In review Sun YangKai 2 participants 6 messages Sep 30, 2025 – Oct 4, 2025 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: more trivial BTRFS_PATH_AUTO_FREE conversions

Trivial pattern for the auto freeing without goto -> return conversions. No other function cleanup.

Tested with btrfs/auto group.

---
 fs/btrfs/uuid-tree.c | 12 ++++--------
 fs/btrfs/verity.c    |  9 +++------
 fs/btrfs/volumes.c   | 44 ++++++++++++++------------------------------
 fs/btrfs/xattr.c     |  7 ++-----
 4 files changed, 23 insertions(+), 49 deletions(-)
fs/btrfs/uuid-tree.c +4 -8
index 17b5e81123a1..30e5d80adda9 100644
@@ -27,7 +27,7 @@ static int btrfs_uuid_tree_lookup(struct btrfs_root *uuid_root, const u8 *uuid,
 				  u8 type, u64 subid)
 {
 	int ret;
-	struct btrfs_path *path = NULL;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct extent_buffer *eb;
 	int slot;
 	u32 item_size;
@@ -79,7 +79,6 @@ static int btrfs_uuid_tree_lookup(struct btrfs_root *uuid_root, const u8 *uuid,
 	}
 
 out:
-	btrfs_free_path(path);
 	return ret;
 }
 
@@ -89,7 +88,7 @@ int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, const u8 *uuid, u8 typ
 	struct btrfs_fs_info *fs_info = trans->fs_info;
 	struct btrfs_root *uuid_root = fs_info->uuid_root;
 	int ret;
-	struct btrfs_path *path = NULL;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_key key;
 	struct extent_buffer *eb;
 	int slot;
@@ -141,7 +140,6 @@ int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, const u8 *uuid, u8 typ
 	subid_le = cpu_to_le64(subid_cpu);
 	write_extent_buffer(eb, &subid_le, offset, sizeof(subid_le));
 out:
-	btrfs_free_path(path);
 	return ret;
 }
 
@@ -151,7 +149,7 @@ int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, const u8 *uuid, u8
 	struct btrfs_fs_info *fs_info = trans->fs_info;
 	struct btrfs_root *uuid_root = fs_info->uuid_root;
 	int ret;
-	struct btrfs_path *path = NULL;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_key key;
 	struct extent_buffer *eb;
 	int slot;
@@ -223,7 +221,6 @@ int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, const u8 *uuid, u8
 	btrfs_truncate_item(trans, path, item_size - sizeof(subid), 1);
 
 out:
-	btrfs_free_path(path);
 	return ret;
 }
 
@@ -293,7 +290,7 @@ int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info)
 {
 	struct btrfs_root *root = fs_info->uuid_root;
 	struct btrfs_key key;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	int ret = 0;
 	struct extent_buffer *leaf;
 	int slot;
@@ -387,7 +384,6 @@ int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info)
 	}
 
 out:
-	btrfs_free_path(path);
 	return ret;
 }
 
fs/btrfs/verity.c +3 -6
index 4633cbcfcdb9..b1903030faf4 100644
@@ -109,7 +109,7 @@ static int drop_verity_items(struct btrfs_inode *inode, u8 key_type)
 {
 	struct btrfs_trans_handle *trans;
 	struct btrfs_root *root = inode->root;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_key key;
 	int count = 0;
 	int ret;
@@ -170,7 +170,6 @@ static int drop_verity_items(struct btrfs_inode *inode, u8 key_type)
 	ret = count;
 	btrfs_end_transaction(trans);
 out:
-	btrfs_free_path(path);
 	return ret;
 }
 
@@ -217,7 +216,7 @@ static int write_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset,
 			   const char *src, u64 len)
 {
 	struct btrfs_trans_handle *trans;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_root *root = inode->root;
 	struct extent_buffer *leaf;
 	struct btrfs_key key;
@@ -267,7 +266,6 @@ static int write_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset,
 		btrfs_end_transaction(trans);
 	}
 
-	btrfs_free_path(path);
 	return ret;
 }
 
@@ -296,7 +294,7 @@ static int write_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset,
 static int read_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset,
 			  char *dest, u64 len, struct folio *dest_folio)
 {
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_root *root = inode->root;
 	struct extent_buffer *leaf;
 	struct btrfs_key key;
@@ -404,7 +402,6 @@ static int read_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset,
 		}
 	}
 out:
-	btrfs_free_path(path);
 	if (!ret)
 		ret = copied;
 	return ret;
fs/btrfs/volumes.c +14 -30

212 lines not on this page · raw message

fs/btrfs/xattr.c +2 -5
index 79fb1614bd0c..7fec70dfe06b 100644
@@ -29,7 +29,7 @@ int btrfs_getxattr(const struct inode *inode, const char *name,
 {
 	struct btrfs_dir_item *di;
 	struct btrfs_root *root = BTRFS_I(inode)->root;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct extent_buffer *leaf;
 	int ret = 0;
 	unsigned long data_ptr;
@@ -76,7 +76,6 @@ int btrfs_getxattr(const struct inode *inode, const char *name,
 	ret = btrfs_dir_data_len(leaf, di);
 
 out:
-	btrfs_free_path(path);
 	return ret;
 }
 
@@ -278,7 +277,7 @@ ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
 	struct btrfs_key key;
 	struct inode *inode = d_inode(dentry);
 	struct btrfs_root *root = BTRFS_I(inode)->root;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	int iter_ret = 0;
 	int ret = 0;
 	size_t total_size = 0, size_left = size;
@@ -354,8 +353,6 @@ ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
 	else
 		ret = total_size;
 
-	btrfs_free_path(path);
-
 	return ret;
 }
 
Signature · 1 line
2.51.0
#1 lore raw
Quoted 27 lines · Sun YangKai

Trivial pattern for the auto freeing without goto -> return conversions. No other function cleanup.

Tested with btrfs/auto group.

---
 fs/btrfs/uuid-tree.c | 12 ++++--------
 fs/btrfs/verity.c    |  9 +++------
 fs/btrfs/volumes.c   | 44 ++++++++++++++------------------------------
 fs/btrfs/xattr.c     |  7 ++-----
 4 files changed, 23 insertions(+), 49 deletions(-)
fs/btrfs/uuid-tree.c +1 -1
index 17b5e81123a1..30e5d80adda9 100644
@@ -27,7 +27,7 @@ static int btrfs_uuid_tree_lookup(struct btrfs_root *uuid_root, const u8 *uuid,
 				  u8 type, u64 subid)
 {
 	int ret;
-	struct btrfs_path *path = NULL;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct extent_buffer *eb;
 	int slot;
 	u32 item_size;

fs/btrfs/uuid-tree.c static int btrfs_uuid_tree_lookup(struct btrfs_root *uuid_root, const u8 *uuid, · line 82

Quoted 4 lines
fs/btrfs/uuid-tree.c +0 -1
@@ -79,7 +79,6 @@ static int btrfs_uuid_tree_lookup(struct btrfs_root *uuid_root, const u8 *uuid,
 	}
 
 out:
-	btrfs_free_path(path);

Here and in several other places you did not convert gotos to returns and removed the out: label. What I've seen in other places in the patch the conversions are straightforward and you already did that in other patches. Can you please do it here as well?

The unnecessary gotos are in functions where the patch auto freeing is not done, this could be another cleanup round.

#2 lore raw
Quoted 4 lines

Here and in several other places you did not convert gotos to returns and removed the out: label. What I've seen in other places in the patch the conversions are straightforward and you already did that in other patches. Can you please do it here as well?

OK. I'll send a v2 patch with goto -> return conversions later.

#3 lore raw

PATCH 1/1 btrfs: goto out -> return conversions for previous patch

In reply to David Sterba

Tested with btrfs/auto group. Tests that require $SCRATCH_DEV_POOL, $LOGWRITES_DEV, zoned block devices and dm flakey support are skipped.

---
 fs/btrfs/uuid-tree.c | 102 +++++++++++++++----------------------------
 fs/btrfs/verity.c    |  14 +++---
 fs/btrfs/volumes.c   |  98 +++++++++++++++--------------------------
 fs/btrfs/xattr.c     |  29 ++++--------
 4 files changed, 86 insertions(+), 157 deletions(-)
fs/btrfs/uuid-tree.c +36 -66
index 30e5d80adda9..8dd71df277c1 100644
@@ -34,25 +34,19 @@ static int btrfs_uuid_tree_lookup(struct btrfs_root *uuid_root, const u8 *uuid,
 	unsigned long offset;
 	struct btrfs_key key;
 
-	if (WARN_ON_ONCE(!uuid_root)) {
-		ret = -ENOENT;
-		goto out;
-	}
+	if (WARN_ON_ONCE(!uuid_root))
+		return -ENOENT;
 
 	path = btrfs_alloc_path();
-	if (!path) {
-		ret = -ENOMEM;
-		goto out;
-	}
+	if (!path)
+		return -ENOMEM;
 
 	btrfs_uuid_to_key(uuid, type, &key);
 	ret = btrfs_search_slot(NULL, uuid_root, &key, path, 0, 0);
-	if (ret < 0) {
-		goto out;
-	} else if (ret > 0) {
-		ret = -ENOENT;
-		goto out;
-	}
+	if (ret < 0)
+		return ret;
+	if (ret > 0)
+		return -ENOENT;
 
 	eb = path->nodes[0];
 	slot = path->slots[0];
@@ -64,7 +58,7 @@ static int btrfs_uuid_tree_lookup(struct btrfs_root *uuid_root, const u8 *uuid,
 		btrfs_warn(uuid_root->fs_info,
 			   "uuid item with illegal size %lu!",
 			   (unsigned long)item_size);
-		goto out;
+		return ret;
 	}
 	while (item_size) {
 		__le64 data;
@@ -78,7 +72,6 @@ static int btrfs_uuid_tree_lookup(struct btrfs_root *uuid_root, const u8 *uuid,
 		item_size -= sizeof(data);
 	}
 
-out:
 	return ret;
 }
 
@@ -99,18 +92,14 @@ int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, const u8 *uuid, u8 typ
 	if (ret != -ENOENT)
 		return ret;
 
-	if (WARN_ON_ONCE(!uuid_root)) {
-		ret = -EINVAL;
-		goto out;
-	}
+	if (WARN_ON_ONCE(!uuid_root))
+		return -EINVAL;
 
 	btrfs_uuid_to_key(uuid, type, &key);
 
 	path = btrfs_alloc_path();
-	if (!path) {
-		ret = -ENOMEM;
-		goto out;
-	}
+	if (!path)
+		return -ENOMEM;
 
 	ret = btrfs_insert_empty_item(trans, uuid_root, path, &key,
 				      sizeof(subid_le));
@@ -133,14 +122,12 @@ int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, const u8 *uuid, u8 typ
 		btrfs_warn(fs_info,
 			   "insert uuid item failed %d (0x%016llx, 0x%016llx) type %u!",
 			   ret, key.objectid, key.offset, type);
-		goto out;
+		return ret;
 	}
 
-	ret = 0;
 	subid_le = cpu_to_le64(subid_cpu);
 	write_extent_buffer(eb, &subid_le, offset, sizeof(subid_le));
-out:
-	return ret;
+	return 0;
 }
 
 int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, const u8 *uuid, u8 type,
@@ -159,29 +146,23 @@ int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, const u8 *uuid, u8
 	unsigned long move_src;
 	unsigned long move_len;
 
-	if (WARN_ON_ONCE(!uuid_root)) {
-		ret = -EINVAL;
-		goto out;
-	}
+	if (WARN_ON_ONCE(!uuid_root))
+		return -EINVAL;
 
 	btrfs_uuid_to_key(uuid, type, &key);
 
 	path = btrfs_alloc_path();
-	if (!path) {
-		ret = -ENOMEM;
-		goto out;
-	}
+	if (!path)
+		return -ENOMEM;
 
 	ret = btrfs_search_slot(trans, uuid_root, &key, path, -1, 1);
 	if (ret < 0) {
 		btrfs_warn(fs_info, "error %d while searching for uuid item!",
 			   ret);
-		goto out;
-	}
-	if (ret > 0) {
-		ret = -ENOENT;
-		goto out;
+		return ret;
 	}
+	if (ret > 0)
+		return -ENOENT;
 
 	eb = path->nodes[0];
 	slot = path->slots[0];
@@ -190,8 +171,7 @@ int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, const u8 *uuid, u8
 	if (!IS_ALIGNED(item_size, sizeof(u64))) {
 		btrfs_warn(fs_info, "uuid item with illegal size %lu!",
 			   (unsigned long)item_size);
-		ret = -ENOENT;
-		goto out;
+		return -ENOENT;
 	}
 	while (item_size) {
 		__le64 read_subid;
@@ -203,25 +183,19 @@ int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, const u8 *uuid, u8
 		item_size -= sizeof(read_subid);
 	}
 
-	if (!item_size) {
-		ret = -ENOENT;
-		goto out;
-	}
+	if (!item_size)
+		return -ENOENT;
 
 	item_size = btrfs_item_size(eb, slot);
-	if (item_size == sizeof(subid)) {
-		ret = btrfs_del_item(trans, uuid_root, path);
-		goto out;
-	}
+	if (item_size == sizeof(subid))
+		return btrfs_del_item(trans, uuid_root, path);
 
 	move_dst = offset;
 	move_src = offset + sizeof(subid);
 	move_len = item_size - (move_src - btrfs_item_ptr_offset(eb, slot));
 	memmove_extent_buffer(eb, move_dst, move_src, move_len);
 	btrfs_truncate_item(trans, path, item_size - sizeof(subid), 1);
-
-out:
-	return ret;
+	return 0;
 }
 
 static int btrfs_uuid_iter_rem(struct btrfs_root *uuid_root, u8 *uuid, u8 type,
@@ -298,10 +272,8 @@ int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info)
 	unsigned long offset;
 
 	path = btrfs_alloc_path();
-	if (!path) {
-		ret = -ENOMEM;
-		goto out;
-	}
+	if (!path)
+		return -ENOMEM;
 
 	key.objectid = 0;
 	key.type = 0;
@@ -312,14 +284,13 @@ int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info)
 	if (ret) {
 		if (ret > 0)
 			ret = 0;
-		goto out;
+		return ret;
 	}
 
 	while (1) {
-		if (btrfs_fs_closing(fs_info)) {
-			ret = -EINTR;
-			goto out;
-		}
+		if (btrfs_fs_closing(fs_info))
+			return -EINTR;
+
 		cond_resched();
 		leaf = path->nodes[0];
 		slot = path->slots[0];
@@ -350,7 +321,7 @@ int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info)
 			ret = btrfs_check_uuid_tree_entry(fs_info, uuid,
 							  key.type, subid_cpu);
 			if (ret < 0)
-				goto out;
+				return ret;
 			if (ret > 0) {
 				btrfs_release_path(path);
 				ret = btrfs_uuid_iter_rem(root, uuid, key.type,
@@ -366,7 +337,7 @@ int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info)
 					goto again_search_slot;
 				}
 				if (ret < 0 && ret != -ENOENT)
-					goto out;
+					return ret;
 				key.offset++;
 				goto again_search_slot;
 			}
@@ -383,7 +354,6 @@ int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info)
 		break;
 	}
 
-out:
 	return ret;
 }
 
fs/btrfs/verity.c +5 -9
index b1903030faf4..5a87bfcbc744 100644
@@ -121,10 +121,8 @@ static int drop_verity_items(struct btrfs_inode *inode, u8 key_type)
 	while (1) {
 		/* 1 for the item being dropped */
 		trans = btrfs_start_transaction(root, 1);
-		if (IS_ERR(trans)) {
-			ret = PTR_ERR(trans);
-			goto out;
-		}
+		if (IS_ERR(trans))
+			return PTR_ERR(trans);
 
 		/*
 		 * 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)
 			path->slots[0]--;
 		} else if (ret < 0) {
 			btrfs_end_transaction(trans);
-			goto out;
+			return ret;
 		}
 
 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
@@ -161,16 +159,14 @@ static int drop_verity_items(struct btrfs_inode *inode, u8 key_type)
 		ret = btrfs_del_items(trans, root, path, path->slots[0], 1);
 		if (ret) {
 			btrfs_end_transaction(trans);
-			goto out;
+			return ret;
 		}
 		count++;
 		btrfs_release_path(path);
 		btrfs_end_transaction(trans);
 	}
-	ret = count;
 	btrfs_end_transaction(trans);
-out:
-	return ret;
+	return count;
 }
 
 /*
fs/btrfs/volumes.c +36 -62

298 lines not on this page · raw message

fs/btrfs/xattr.c +9 -20
index 7fec70dfe06b..3d27eb1e2f74 100644
@@ -31,7 +31,6 @@ int btrfs_getxattr(const struct inode *inode, const char *name,
 	struct btrfs_root *root = BTRFS_I(inode)->root;
 	BTRFS_PATH_AUTO_FREE(path);
 	struct extent_buffer *leaf;
-	int ret = 0;
 	unsigned long data_ptr;
 
 	path = btrfs_alloc_path();
@@ -41,26 +40,19 @@ int btrfs_getxattr(const struct inode *inode, const char *name,
 	/* lookup the xattr by name */
 	di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(BTRFS_I(inode)),
 			name, strlen(name), 0);
-	if (!di) {
-		ret = -ENODATA;
-		goto out;
-	} else if (IS_ERR(di)) {
-		ret = PTR_ERR(di);
-		goto out;
-	}
+	if (!di)
+		return -ENODATA;
+	if (IS_ERR(di))
+		return PTR_ERR(di);
 
 	leaf = path->nodes[0];
 	/* if size is 0, that means we want the size of the attr */
-	if (!size) {
-		ret = btrfs_dir_data_len(leaf, di);
-		goto out;
-	}
+	if (!size)
+		return btrfs_dir_data_len(leaf, di);
 
 	/* now get the data out of our dir_item */
-	if (btrfs_dir_data_len(leaf, di) > size) {
-		ret = -ERANGE;
-		goto out;
-	}
+	if (btrfs_dir_data_len(leaf, di) > size)
+		return -ERANGE;
 
 	/*
 	 * The way things are packed into the leaf is like this
@@ -73,10 +65,7 @@ int btrfs_getxattr(const struct inode *inode, const char *name,
 				   btrfs_dir_name_len(leaf, di));
 	read_extent_buffer(leaf, buffer, data_ptr,
 			   btrfs_dir_data_len(leaf, di));
-	ret = btrfs_dir_data_len(leaf, di);
-
-out:
-	return ret;
+	return btrfs_dir_data_len(leaf, di);
 }
 
 int btrfs_setxattr(struct btrfs_trans_handle *trans, struct inode *inode,
Signature · 1 line
2.51.0
#4 lore raw

Re: [PATCH] btrfs: goto out -> return conversions for previous patch

Quoted 2 lines · Sun YangKai

Tested with btrfs/auto group. Tests that require $SCRATCH_DEV_POOL, $LOGWRITES_DEV, zoned block devices and dm flakey support are skipped.

Is this a standalone patch or fixup to the path auto freeing? The changelog should describe the change, not what you tested it on. This may matter in some cases but this is better under the "---" line in the patch.

Quoted 3 lines

---

(here)

Quoted 5 lines
 fs/btrfs/uuid-tree.c | 102 +++++++++++++++----------------------------
 fs/btrfs/verity.c    |  14 +++---
 fs/btrfs/volumes.c   |  98 +++++++++++++++--------------------------
 fs/btrfs/xattr.c     |  29 ++++--------
 4 files changed, 86 insertions(+), 157 deletions(-)
#5 lore raw

Re: [PATCH] btrfs: goto out -> return conversions for previous patch

Quoted 8 lines · Sun YangKai
Quoted 2 lines

Tested with btrfs/auto group. Tests that require $SCRATCH_DEV_POOL, $LOGWRITES_DEV, zoned block devices and dm flakey support are skipped.

Is this a standalone patch or fixup to the path auto freeing? The changelog should describe the change, not what you tested it on. This may matter in some cases but this is better under the "---" line in the patch.

Sorry. This is a fixup to the previous path auto freeing patch. I'll resend the patches with corrected message later.

Quoted 11 lines
Quoted 3 lines

---

(here)

Quoted 5 lines
 fs/btrfs/uuid-tree.c | 102 +++++++++++++++----------------------------
 fs/btrfs/verity.c    |  14 +++---
 fs/btrfs/volumes.c   |  98 +++++++++++++++--------------------------
 fs/btrfs/xattr.c     |  29 ++++--------
 4 files changed, 86 insertions(+), 157 deletions(-)