Skip to contentSkip to conversation

btrfs: more trivial BTRFS_PATH_AUTO_FREE conversions

PATCH Superseded Sun YangKai 2 participants 3 messages Aug 19, 2025 – Aug 21, 2025 Btrfs file system

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: more trivial BTRFS_PATH_AUTO_FREE conversions

Trival pattern for the auto freeing with goto -> return convertions if possible. No other function cleanup.

---
 fs/btrfs/raid-stripe-tree.c |  16 +-
 fs/btrfs/ref-verify.c       |   3 +-
 fs/btrfs/reflink.c          |   3 +-
 fs/btrfs/relocation.c       |  66 +++-----
 fs/btrfs/root-tree.c        |  49 +++---
 fs/btrfs/scrub.c            |  11 +-
 fs/btrfs/send.c             | 307 +++++++++++++-----------------------
 fs/btrfs/super.c            |   9 +-
 fs/btrfs/tree-log.c         | 124 +++++----------
 9 files changed, 204 insertions(+), 384 deletions(-)
fs/btrfs/raid-stripe-tree.c +5 -11
index cab0b291088c..231107cafab2 100644
@@ -67,7 +67,7 @@ int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 le
 {
 	struct btrfs_fs_info *fs_info = trans->fs_info;
 	struct btrfs_root *stripe_root = fs_info->stripe_root;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_key key;
 	struct extent_buffer *leaf;
 	u64 found_start;
@@ -260,7 +260,6 @@ int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 le
 		btrfs_release_path(path);
 	}
 
-	btrfs_free_path(path);
 	return ret;
 }
 
@@ -269,7 +268,7 @@ static int update_raid_extent_item(struct btrfs_trans_handle *trans,
 				   struct btrfs_stripe_extent *stripe_extent,
 				   const size_t item_size)
 {
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct extent_buffer *leaf;
 	int ret;
 	int slot;
@@ -288,7 +287,6 @@ static int update_raid_extent_item(struct btrfs_trans_handle *trans,
 
 	write_extent_buffer(leaf, stripe_extent, btrfs_item_ptr_offset(leaf, slot),
 			    item_size);
-	btrfs_free_path(path);
 
 	return ret;
 }
@@ -376,7 +374,7 @@ int btrfs_get_raid_extent_offset(struct btrfs_fs_info *fs_info,
 	struct btrfs_stripe_extent *stripe_extent;
 	struct btrfs_key stripe_key;
 	struct btrfs_key found_key;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct extent_buffer *leaf;
 	const u64 end = logical + *length;
 	int num_stripes;
@@ -402,7 +400,7 @@ int btrfs_get_raid_extent_offset(struct btrfs_fs_info *fs_info,
 
 	ret = btrfs_search_slot(NULL, stripe_root, &stripe_key, path, 0, 0);
 	if (ret < 0)
-		goto free_path;
+		return ret;
 	if (ret) {
 		if (path->slots[0] != 0)
 			path->slots[0]--;
@@ -459,8 +457,7 @@ int btrfs_get_raid_extent_offset(struct btrfs_fs_info *fs_info,
 		trace_btrfs_get_raid_extent_offset(fs_info, logical, *length,
 						   stripe->physical, devid);
 
-		ret = 0;
-		goto free_path;
+		return 0;
 	}
 
 	/* If we're here, we haven't found the requested devid in the stripe. */
@@ -474,8 +471,5 @@ int btrfs_get_raid_extent_offset(struct btrfs_fs_info *fs_info,
 			  logical, logical + *length, stripe->dev->devid,
 			  btrfs_bg_type_to_raid_name(map_type));
 	}
-free_path:
-	btrfs_free_path(path);
-
 	return ret;
 }
fs/btrfs/ref-verify.c +1 -2
index 3871c3a6c743..cba14eb75c0a 100644
@@ -971,7 +971,7 @@ void btrfs_free_ref_tree_range(struct btrfs_fs_info *fs_info, u64 start,
 int btrfs_build_ref_tree(struct btrfs_fs_info *fs_info)
 {
 	struct btrfs_root *extent_root;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct extent_buffer *eb;
 	int tree_block_level = 0;
 	u64 bytenr = 0, num_bytes = 0;
@@ -1014,6 +1014,5 @@ int btrfs_build_ref_tree(struct btrfs_fs_info *fs_info)
 		btrfs_free_ref_cache(fs_info);
 		btrfs_clear_opt(fs_info->mount_opt, REF_VERIFY);
 	}
-	btrfs_free_path(path);
 	return ret;
 }
fs/btrfs/reflink.c +1 -2
index ce25ab7f0e99..98def02ce5cc 100644
@@ -340,7 +340,7 @@ static int btrfs_clone(struct inode *src, struct inode *inode,
 		       const u64 destoff, int no_time_update)
 {
 	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
-	struct btrfs_path *path = NULL;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct extent_buffer *leaf;
 	struct btrfs_trans_handle *trans;
 	char *buf = NULL;
@@ -611,7 +611,6 @@ static int btrfs_clone(struct inode *src, struct inode *inode,
 	}
 
 out:
-	btrfs_free_path(path);
 	kvfree(buf);
 	clear_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &BTRFS_I(inode)->runtime_flags);
 
fs/btrfs/relocation.c +21 -45

209 lines not on this page · raw message

fs/btrfs/root-tree.c +18 -31

166 lines not on this page · raw message

fs/btrfs/scrub.c +3 -8
index 6776e6ab8d10..025a91d267b4 100644
@@ -585,7 +585,7 @@ static void scrub_print_common_warning(const char *errstr, struct btrfs_device *
 				       bool is_super, u64 logical, u64 physical)
 {
 	struct btrfs_fs_info *fs_info = dev->fs_info;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_key found_key;
 	struct extent_buffer *eb;
 	struct btrfs_extent_item *ei;
@@ -612,7 +612,7 @@ static void scrub_print_common_warning(const char *errstr, struct btrfs_device *
 	ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
 				  &flags);
 	if (ret < 0)
-		goto out;
+		return;
 
 	swarn.extent_item_size = found_key.offset;
 
@@ -658,9 +658,6 @@ static void scrub_print_common_warning(const char *errstr, struct btrfs_device *
 
 		iterate_extent_inodes(&ctx, true, scrub_print_warning_inode, &swarn);
 	}
-
-out:
-	btrfs_free_path(path);
 }
 
 static int fill_writer_pointer_gap(struct scrub_ctx *sctx, u64 physical)
@@ -2586,7 +2583,7 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx,
 			   struct btrfs_device *scrub_dev, u64 start, u64 end)
 {
 	struct btrfs_dev_extent *dev_extent = NULL;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_fs_info *fs_info = sctx->fs_info;
 	struct btrfs_root *root = fs_info->dev_root;
 	u64 chunk_offset;
@@ -2872,8 +2869,6 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx,
 		btrfs_release_path(path);
 	}
 
-	btrfs_free_path(path);
-
 	return ret;
 }
 
fs/btrfs/send.c +110 -197

846 lines not on this page · raw message

fs/btrfs/super.c +2 -7
index a262b494a89f..5e6eccd11fbe 100644
@@ -794,7 +794,7 @@ char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
 	struct btrfs_root_ref *root_ref;
 	struct btrfs_inode_ref *inode_ref;
 	struct btrfs_key key;
-	struct btrfs_path *path = NULL;
+	BTRFS_PATH_AUTO_FREE(path);
 	char *name = NULL, *ptr;
 	u64 dirid;
 	int len;
@@ -892,7 +892,6 @@ char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
 		fs_root = NULL;
 	}
 
-	btrfs_free_path(path);
 	if (ptr == name + PATH_MAX - 1) {
 		name[0] = '/';
 		name[1] = '\0';
@@ -903,7 +902,6 @@ char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
 
 err:
 	btrfs_put_root(fs_root);
-	btrfs_free_path(path);
 	kfree(name);
 	return ERR_PTR(ret);
 }
@@ -912,7 +910,7 @@ static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objec
 {
 	struct btrfs_root *root = fs_info->tree_root;
 	struct btrfs_dir_item *di;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_key location;
 	struct fscrypt_str name = FSTR_INIT("default", 7);
 	u64 dir_id;
@@ -929,7 +927,6 @@ static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objec
 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, &name, 0);
 	if (IS_ERR(di)) {
-		btrfs_free_path(path);
 		return PTR_ERR(di);
 	}
 	if (!di) {
@@ -938,13 +935,11 @@ static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objec
 		 * it's always been there, but don't freak out, just try and
 		 * mount the top-level subvolume.
 		 */
-		btrfs_free_path(path);
 		*objectid = BTRFS_FS_TREE_OBJECTID;
 		return 0;
 	}
 
 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
-	btrfs_free_path(path);
 	*objectid = location.objectid;
 	return 0;
 }
fs/btrfs/tree-log.c +43 -81

390 lines not on this page · raw message

Signature · 1 line
2.50.1
build bot
#1 lore raw
BCC: [email protected] CC: [email protected] In-Reply-To: <[email protected]> References: <[email protected]> TO: Sun YangKai…

BCC: [email protected]
CC: [email protected]
In-Reply-To: <[email protected]>
References: <[email protected]>
TO: Sun YangKai <[email protected]>
TO: [email protected]
CC: Sun YangKai <[email protected]>

Hi Sun,

kernel test robot noticed the following build warnings:

[auto build test WARNING on v6.17-rc2]
[also build test WARNING on linus/master next-20250820] [cannot apply to kdave/for-next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Sun-YangKai/btrfs-more-trivial-BTRFS_PATH_AUTO_FREE-conversions/20250819-114252
base:   v6.17-rc2
patch link:    https://lore.kernel.org/r/20250819033819.19826-1-sunk67188%40gmail.com
patch subject: [PATCH] btrfs: more trivial BTRFS_PATH_AUTO_FREE conversions

:::::: branch date: 2 days ago
:::::: commit date: 2 days ago
config: parisc-randconfig-r071-20250821 (https://download.01.org/0day-ci/archive/20250821/[email protected]/config)
compiler: hppa-linux-gcc (GCC) 9.5.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

smatch warnings:
fs/btrfs/send.c:931 get_inode_info() warn: missing error code? 'ret'

vim +/ret +931 fs/btrfs/send.c

7e93f6dc11d9128 BingJing Chang  2022-08-12  904  
31db9f7c23fbf7e Alexander Block 2012-07-25  905  /*
31db9f7c23fbf7e Alexander Block 2012-07-25  906   * Helper function to retrieve some fields from an inode item.
31db9f7c23fbf7e Alexander Block 2012-07-25  907   */
7e93f6dc11d9128 BingJing Chang  2022-08-12  908  static int get_inode_info(struct btrfs_root *root, u64 ino,
7e93f6dc11d9128 BingJing Chang  2022-08-12  909  			  struct btrfs_inode_info *info)
31db9f7c23fbf7e Alexander Block 2012-07-25  910  {
31db9f7c23fbf7e Alexander Block 2012-07-25  911  	int ret;
c8ce1e5fe626333 Sun YangKai     2025-08-19  912  	BTRFS_PATH_AUTO_FREE(path);
31db9f7c23fbf7e Alexander Block 2012-07-25  913  	struct btrfs_inode_item *ii;
31db9f7c23fbf7e Alexander Block 2012-07-25  914  	struct btrfs_key key;
31db9f7c23fbf7e Alexander Block 2012-07-25  915  
7e93f6dc11d9128 BingJing Chang  2022-08-12  916  	path = alloc_path_for_send();
7e93f6dc11d9128 BingJing Chang  2022-08-12  917  	if (!path)
7e93f6dc11d9128 BingJing Chang  2022-08-12  918  		return -ENOMEM;
7e93f6dc11d9128 BingJing Chang  2022-08-12  919  
31db9f7c23fbf7e Alexander Block 2012-07-25  920  	key.objectid = ino;
31db9f7c23fbf7e Alexander Block 2012-07-25  921  	key.type = BTRFS_INODE_ITEM_KEY;
31db9f7c23fbf7e Alexander Block 2012-07-25  922  	key.offset = 0;
31db9f7c23fbf7e Alexander Block 2012-07-25  923  	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
31db9f7c23fbf7e Alexander Block 2012-07-25  924  	if (ret) {
3f8a18cc53bd1be Josef Bacik     2014-03-28  925  		if (ret > 0)
31db9f7c23fbf7e Alexander Block 2012-07-25  926  			ret = -ENOENT;
c8ce1e5fe626333 Sun YangKai     2025-08-19  927  		return ret;
31db9f7c23fbf7e Alexander Block 2012-07-25  928  	}
31db9f7c23fbf7e Alexander Block 2012-07-25  929  
7e93f6dc11d9128 BingJing Chang  2022-08-12  930  	if (!info)
c8ce1e5fe626333 Sun YangKai     2025-08-19 @931  		return ret;
7e93f6dc11d9128 BingJing Chang  2022-08-12  932  
31db9f7c23fbf7e Alexander Block 2012-07-25  933  	ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
31db9f7c23fbf7e Alexander Block 2012-07-25  934  			struct btrfs_inode_item);
7e93f6dc11d9128 BingJing Chang  2022-08-12  935  	info->size = btrfs_inode_size(path->nodes[0], ii);
7e93f6dc11d9128 BingJing Chang  2022-08-12  936  	info->gen = btrfs_inode_generation(path->nodes[0], ii);
7e93f6dc11d9128 BingJing Chang  2022-08-12  937  	info->mode = btrfs_inode_mode(path->nodes[0], ii);
7e93f6dc11d9128 BingJing Chang  2022-08-12  938  	info->uid = btrfs_inode_uid(path->nodes[0], ii);
7e93f6dc11d9128 BingJing Chang  2022-08-12  939  	info->gid = btrfs_inode_gid(path->nodes[0], ii);
7e93f6dc11d9128 BingJing Chang  2022-08-12  940  	info->rdev = btrfs_inode_rdev(path->nodes[0], ii);
9ed0a72e5b355d4 BingJing Chang  2022-08-12  941  	info->nlink = btrfs_inode_nlink(path->nodes[0], ii);
4824735918a1b7f David Sterba    2022-05-18  942  	/*
4824735918a1b7f David Sterba    2022-05-18  943  	 * Transfer the unchanged u64 value of btrfs_inode_item::flags, that's
4824735918a1b7f David Sterba    2022-05-18  944  	 * otherwise logically split to 32/32 parts.
4824735918a1b7f David Sterba    2022-05-18  945  	 */
7e93f6dc11d9128 BingJing Chang  2022-08-12  946  	info->fileattr = btrfs_inode_flags(path->nodes[0], ii);
31db9f7c23fbf7e Alexander Block 2012-07-25  947  
3f8a18cc53bd1be Josef Bacik     2014-03-28  948  	return ret;
3f8a18cc53bd1be Josef Bacik     2014-03-28  949  }
3f8a18cc53bd1be Josef Bacik     2014-03-28  950  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#2 lore raw
Quoted 23 lines

Hi Sun,

kernel test robot noticed the following build warnings:

https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:
https://github.com/intel-lab-lkp/linux/commits/Sun-YangKai/btrfs-more-trivi
al-BTRFS_PATH_AUTO_FREE-conversions/20250819-114252 base: v6.17-rc2
patch link:
https://lore.kernel.org/r/20250819033819.19826-1-sunk67188%40gmail.com
patch subject: [PATCH] btrfs: more trivial BTRFS_PATH_AUTO_FREE conversions
config: parisc-randconfig-r071-20250821
(https://download.01.org/0day-ci/archive/20250821/202508211534.QmDKCbTm-lkp
@intel.com/config) compiler: hppa-linux-gcc (GCC) 9.5.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/
Quoted 8 lines

smatch warnings:
fs/btrfs/send.c:931 get_inode_info() warn: missing error code? 'ret'

vim +/ret +931 fs/btrfs/send.c

7e93f6dc11d9128 BingJing Chang 2022-08-12 908 static int get_inode_info(struct btrfs_root *root, u64 ino, 7e93f6dc11d9128 BingJing
Chang 2022-08-12 909 struct btrfs_inode_info *info)

31db9f7c23fbf7e

Quoted 9 lines

Alexander Block 2012-07-25 910 {
31db9f7c23fbf7e Alexander Block 2012-07-25 911 int ret;
c8ce1e5fe626333 Sun YangKai 2025-08-19 912

	BTRFS_PATH_AUTO_FREE(path); 31db9f7c23fbf7e Alexander Block 2012-07-25 

913 struct btrfs_inode_item *ii; 31db9f7c23fbf7e Alexander Block
2012-07-25 914 struct btrfs_key key; 31db9f7c23fbf7e Alexander Block
2012-07-25 915
7e93f6dc11d9128 BingJing Chang 2022-08-12 916 path =
alloc_path_for_send(); 7e93f6dc11d9128 BingJing Chang 2022-08-12 917

if

Quoted 16 lines

(!path)

7e93f6dc11d9128 BingJing Chang  2022-08-12  918  		return -ENOMEM;
7e93f6dc11d9128 BingJing Chang  2022-08-12  919
31db9f7c23fbf7e Alexander Block 2012-07-25  920  	key.objectid = ino;
31db9f7c23fbf7e Alexander Block 2012-07-25  921  	key.type =
BTRFS_INODE_ITEM_KEY; 31db9f7c23fbf7e Alexander Block 2012-07-25  922 
	key.offset = 0;
31db9f7c23fbf7e Alexander Block 2012-07-25  923  	ret =
btrfs_search_slot(NULL, root, &key, path, 0, 0); 31db9f7c23fbf7e Alexander
Block 2012-07-25  924  	if (ret) {
3f8a18cc53bd1be Josef Bacik     2014-03-28  925  		if (ret > 0)
31db9f7c23fbf7e Alexander Block 2012-07-25  926  			ret = -ENOENT;
c8ce1e5fe626333 Sun YangKai     2025-08-19  927  		return ret;
31db9f7c23fbf7e Alexander Block 2012-07-25  928  	}
31db9f7c23fbf7e Alexander Block 2012-07-25  929
7e93f6dc11d9128 BingJing Chang  2022-08-12  930  	if (!info)
Quoted 1 line

c8ce1e5fe626333 Sun YangKai 2025-08-19 @931 return ret;

This early return should be done at the beginning of this function instead of after btrfs_search_slot() since info is the pointer to the output. With a NULL output, we should do nothing. I'll change this in patch v2.

Quoted 7 lines

ret is zero, but it should be an error code.

7e93f6dc11d9128 BingJing Chang 2022-08-12 932
31db9f7c23fbf7e Alexander Block 2012-07-25 933 ii =
btrfs_item_ptr(path->nodes[0], path->slots[0], 31db9f7c23fbf7e Alexander
Block 2012-07-25 934 struct btrfs_inode_item);

7e93f6dc11d9128

Quoted 3 lines

BingJing Chang 2022-08-12 935 info->size =
btrfs_inode_size(path->nodes[0], ii); 7e93f6dc11d9128 BingJing Chang
2022-08-12 936 info->gen = btrfs_inode_generation(path->nodes[0], ii);

Thanks,
Sun Yangkai