Skip to contentSkip to conversation

Struct btrfs_path auto cleaning conversions

PATCH In review David Sterba 3 participants 34 messages Feb 26, 2025 – Feb 27, 2025 Btrfs file system

lore
Share & tools
lore

David Sterba

author maintainer
#0 lore raw

cover Struct btrfs_path auto cleaning conversions

We have the scoped freeing for struct btrfs_path but it's not used as much as it could. This patchset converts the easy cases and it's also a preview if we really want to do that. It makes understanding the exit paths a bit less obvious, but so far I think it's manageable.

The path is used in many functions and following a few simple patterns, with the macro BTRFS_PATH_AUTO_FREE quite visible among the declarations, so it's nothing hard to be aware of that when reading the code.

The conversion has been done on half of the files, so if somebody wants to continue, feel free. I've skipped functions with more complicated branching where the auto freeing would make it worse.

David Sterba (27):
btrfs: use BTRFS_PATH_AUTO_FREE in sample_block_group_extent_item()
btrfs: use BTRFS_PATH_AUTO_FREE in insert_dev_extent()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_setup_space_cache()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_start_dirty_block_groups()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_write_dirty_block_groups()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_insert_item()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_commit_inode_delayed_items()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_init_dev_replace()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_run_dev_replace()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_check_dir_item_collision()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_read_tree_root()
btrfs: use BTRFS_PATH_AUTO_FREE in load_global_roots()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_init_root_free_objectid()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_get_name()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_lookup_data_extent()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_lookup_extent_info()
btrfs: use BTRFS_PATH_AUTO_FREE in __btrfs_inc_extent_ref()
btrfs: use BTRFS_PATH_AUTO_FREE in run_delayed_extent_op()
btrfs: use BTRFS_PATH_AUTO_FREE in check_ref_exists()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_drop_subtree()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_insert_hole_extent()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_lookup_bio_sums()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_del_csums()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_remove_free_space_inode()
btrfs: use BTRFS_PATH_AUTO_FREE in populate_free_space_tree()
btrfs: use BTRFS_PATH_AUTO_FREE in clear_free_space_tree()
btrfs: use BTRFS_PATH_AUTO_FREE in load_free_space_tree()

 fs/btrfs/block-group.c      | 20 ++++++----------
 fs/btrfs/ctree.c            |  3 +--
 fs/btrfs/delayed-inode.c    |  3 +--
 fs/btrfs/dev-replace.c      | 32 ++++++++++---------------
 fs/btrfs/dir-item.c         | 24 +++++++------------
 fs/btrfs/disk-io.c          | 29 ++++++++++-------------
 fs/btrfs/export.c           | 10 +++-----
 fs/btrfs/extent-tree.c      | 47 ++++++++++++++-----------------------
 fs/btrfs/file-item.c        | 17 +++++---------
 fs/btrfs/free-space-cache.c | 13 ++++------
 fs/btrfs/free-space-tree.c  | 45 ++++++++++++++---------------------
 11 files changed, 91 insertions(+), 152 deletions(-)
Signature · 1 line
2.47.1
author maintainer
#1 lore raw

PATCH 1/27 btrfs: use BTRFS_PATH_AUTO_FREE in sample_block_group_extent_item()

This is the trivial pattern for path auto free, initialize at the beginning and free at the end.

---
 fs/btrfs/block-group.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/btrfs/block-group.c +1 -2
index 18f58674a16c..cd20bf9289bd 100644
@@ -584,7 +584,7 @@ static int sample_block_group_extent_item(struct btrfs_caching_control *caching_
 	struct btrfs_root *extent_root;
 	u64 search_offset;
 	u64 search_end = block_group->start + block_group->length;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_key search_key;
 	int ret = 0;
 
@@ -626,7 +626,6 @@ static int sample_block_group_extent_item(struct btrfs_caching_control *caching_
 
 	lockdep_assert_held(&caching_ctl->mutex);
 	lockdep_assert_held_read(&fs_info->commit_root_sem);
-	btrfs_free_path(path);
 	return ret;
 }
 
Signature · 1 line
2.47.1
author maintainer
#2 lore raw

PATCH 2/27 btrfs: use BTRFS_PATH_AUTO_FREE in insert_dev_extent()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end with simple goto -> return conversions.

---
 fs/btrfs/block-group.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
fs/btrfs/block-group.c +3 -5
index cd20bf9289bd..9c689e7982c2 100644
@@ -2640,7 +2640,7 @@ static int insert_dev_extent(struct btrfs_trans_handle *trans,
 {
 	struct btrfs_fs_info *fs_info = device->fs_info;
 	struct btrfs_root *root = fs_info->dev_root;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_dev_extent *extent;
 	struct extent_buffer *leaf;
 	struct btrfs_key key;
@@ -2657,7 +2657,7 @@ static int insert_dev_extent(struct btrfs_trans_handle *trans,
 	key.offset = start;
 	ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*extent));
 	if (ret)
-		goto out;
+		return ret;
 
 	leaf = path->nodes[0];
 	extent = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
@@ -2665,10 +2665,8 @@ static int insert_dev_extent(struct btrfs_trans_handle *trans,
 	btrfs_set_dev_extent_chunk_objectid(leaf, extent,
 					    BTRFS_FIRST_CHUNK_TREE_OBJECTID);
 	btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
-
 	btrfs_set_dev_extent_length(leaf, extent, num_bytes);
-out:
-	btrfs_free_path(path);
+
 	return ret;
 }
 
Signature · 1 line
2.47.1
author maintainer
#3 lore raw

PATCH 3/27 btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_setup_space_cache()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end.

---
 fs/btrfs/block-group.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/btrfs/block-group.c +1 -2
index 9c689e7982c2..d99e5ed307d5 100644
@@ -3303,7 +3303,7 @@ int btrfs_setup_space_cache(struct btrfs_trans_handle *trans)
 	struct btrfs_fs_info *fs_info = trans->fs_info;
 	struct btrfs_block_group *cache, *tmp;
 	struct btrfs_transaction *cur_trans = trans->transaction;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 
 	if (list_empty(&cur_trans->dirty_bgs) ||
 	    !btrfs_test_opt(fs_info, SPACE_CACHE))
@@ -3320,7 +3320,6 @@ int btrfs_setup_space_cache(struct btrfs_trans_handle *trans)
 			cache_save_setup(cache, trans, path);
 	}
 
-	btrfs_free_path(path);
 	return 0;
 }
 
Signature · 1 line
2.47.1
author maintainer
#4 lore raw

PATCH 4/27 btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_start_dirty_block_groups()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end.

---
 fs/btrfs/block-group.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/btrfs/block-group.c +1 -2
index d99e5ed307d5..95b14e3351b5 100644
@@ -3342,7 +3342,7 @@ int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
 	struct btrfs_transaction *cur_trans = trans->transaction;
 	int ret = 0;
 	int should_put;
-	struct btrfs_path *path = NULL;
+	BTRFS_PATH_AUTO_FREE(path);
 	LIST_HEAD(dirty);
 	struct list_head *io = &cur_trans->io_bgs;
 	int loops = 0;
@@ -3497,7 +3497,6 @@ int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans)
 		btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
 	}
 
-	btrfs_free_path(path);
 	return ret;
 }
 
Signature · 1 line
2.47.1
author maintainer
#5 lore raw

PATCH 5/27 btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_write_dirty_block_groups()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end.

---
 fs/btrfs/block-group.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/btrfs/block-group.c +1 -2
index 95b14e3351b5..50398aa2fb89 100644
@@ -3507,7 +3507,7 @@ int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans)
 	struct btrfs_transaction *cur_trans = trans->transaction;
 	int ret = 0;
 	int should_put;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct list_head *io = &cur_trans->io_bgs;
 
 	path = btrfs_alloc_path();
@@ -3619,7 +3619,6 @@ int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans)
 		btrfs_put_block_group(cache);
 	}
 
-	btrfs_free_path(path);
 	return ret;
 }
 
Signature · 1 line
2.47.1
author maintainer
#6 lore raw

PATCH 6/27 btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_insert_item()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end.

---
 fs/btrfs/ctree.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/btrfs/ctree.c +1 -2
index 3dc5a35dd19b..4d02227e9498 100644
@@ -4306,7 +4306,7 @@ int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
 		      u32 data_size)
 {
 	int ret = 0;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct extent_buffer *leaf;
 	unsigned long ptr;
 
@@ -4320,7 +4320,6 @@ int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
 		write_extent_buffer(leaf, data, ptr, data_size);
 		btrfs_mark_buffer_dirty(trans, leaf);
 	}
-	btrfs_free_path(path);
 	return ret;
 }
 
Signature · 1 line
2.47.1
author maintainer
#7 lore raw

PATCH 7/27 btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_commit_inode_delayed_items()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end.

---
 fs/btrfs/delayed-inode.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/btrfs/delayed-inode.c +1 -2
index 1a65f209339b..3f1551d8a5c6 100644
@@ -1211,7 +1211,7 @@ int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
 				     struct btrfs_inode *inode)
 {
 	struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_block_rsv *block_rsv;
 	int ret;
 
@@ -1238,7 +1238,6 @@ int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
 	ret = __btrfs_commit_inode_delayed_items(trans, path, delayed_node);
 
 	btrfs_release_delayed_node(delayed_node);
-	btrfs_free_path(path);
 	trans->block_rsv = block_rsv;
 
 	return ret;
Signature · 1 line
2.47.1
author maintainer
#8 lore raw

PATCH 8/27 btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_init_dev_replace()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end with simple goto -> return conversions.

---
 fs/btrfs/dev-replace.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
fs/btrfs/dev-replace.c +5 -10
index f86fbea0b3de..147c5494adf9 100644
@@ -76,7 +76,7 @@ int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
 	struct extent_buffer *eb;
 	int slot;
 	int ret = 0;
-	struct btrfs_path *path = NULL;
+	BTRFS_PATH_AUTO_FREE(path);
 	int item_size;
 	struct btrfs_dev_replace_item *ptr;
 	u64 src_devid;
@@ -85,10 +85,8 @@ int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
 		return 0;
 
 	path = btrfs_alloc_path();
-	if (!path) {
-		ret = -ENOMEM;
-		goto out;
-	}
+	if (!path)
+		return -ENOMEM;
 
 	key.objectid = 0;
 	key.type = BTRFS_DEV_REPLACE_KEY;
@@ -103,8 +101,7 @@ int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
 		if (btrfs_find_device(fs_info->fs_devices, &args)) {
 			btrfs_err(fs_info,
 			"found replace target device without a valid replace item");
-			ret = -EUCLEAN;
-			goto out;
+			return -EUCLEAN;
 		}
 		ret = 0;
 		dev_replace->replace_state =
@@ -123,7 +120,7 @@ int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
 		dev_replace->tgtdev = NULL;
 		dev_replace->is_valid = 0;
 		dev_replace->item_needs_writeback = 0;
-		goto out;
+		return ret;
 	}
 	slot = path->slots[0];
 	eb = path->nodes[0];
@@ -226,8 +223,6 @@ int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
 		break;
 	}
 
-out:
-	btrfs_free_path(path);
 	return ret;
 }
 
Signature · 1 line
2.47.1
author maintainer
#9 lore raw

PATCH 9/27 btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_run_dev_replace()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end with simple goto -> return conversions.

---
 fs/btrfs/dev-replace.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
fs/btrfs/dev-replace.c +7 -10
index 147c5494adf9..1a82e88ec5c1 100644
@@ -341,7 +341,7 @@ int btrfs_run_dev_replace(struct btrfs_trans_handle *trans)
 	struct btrfs_fs_info *fs_info = trans->fs_info;
 	int ret;
 	struct btrfs_root *dev_root = fs_info->dev_root;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_key key;
 	struct extent_buffer *eb;
 	struct btrfs_dev_replace_item *ptr;
@@ -360,16 +360,15 @@ int btrfs_run_dev_replace(struct btrfs_trans_handle *trans)
 	key.offset = 0;
 
 	path = btrfs_alloc_path();
-	if (!path) {
-		ret = -ENOMEM;
-		goto out;
-	}
+	if (!path)
+		return -ENOMEM;
+
 	ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
 	if (ret < 0) {
 		btrfs_warn(fs_info,
 			   "error %d while searching for dev_replace item!",
 			   ret);
-		goto out;
+		return ret;
 	}
 
 	if (ret == 0 &&
@@ -390,7 +389,7 @@ int btrfs_run_dev_replace(struct btrfs_trans_handle *trans)
 			btrfs_warn(fs_info,
 				   "delete too small dev_replace item failed %d!",
 				   ret);
-			goto out;
+			return ret;
 		}
 		ret = 1;
 	}
@@ -403,7 +402,7 @@ int btrfs_run_dev_replace(struct btrfs_trans_handle *trans)
 		if (ret < 0) {
 			btrfs_warn(fs_info,
 				   "insert dev_replace item failed %d!", ret);
-			goto out;
+			return ret;
 		}
 	}
 
@@ -435,8 +434,6 @@ int btrfs_run_dev_replace(struct btrfs_trans_handle *trans)
 		dev_replace->cursor_right);
 	dev_replace->item_needs_writeback = 0;
 	up_write(&dev_replace->rwsem);
-out:
-	btrfs_free_path(path);
 
 	return ret;
 }
Signature · 1 line
2.47.1
author maintainer
#10 lore raw

PATCH 10/27 btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_check_dir_item_collision()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end with simple goto -> return conversions.

---
 fs/btrfs/dir-item.c | 24 +++++++++---------------
 1 file changed, 9 insertions(+), 15 deletions(-)
fs/btrfs/dir-item.c +9 -15
index ccf91de29f80..b29cc31a7c4a 100644
@@ -236,7 +236,7 @@ int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
 	int data_size;
 	struct extent_buffer *leaf;
 	int slot;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 
 	path = btrfs_alloc_path();
 	if (!path)
@@ -251,20 +251,17 @@ int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
 	if (IS_ERR(di)) {
 		ret = PTR_ERR(di);
 		/* Nothing found, we're safe */
-		if (ret == -ENOENT) {
-			ret = 0;
-			goto out;
-		}
+		if (ret == -ENOENT)
+			return 0;
 
 		if (ret < 0)
-			goto out;
+			return ret;
 	}
 
 	/* we found an item, look for our name in the item */
 	if (di) {
 		/* our exact name was found */
-		ret = -EEXIST;
-		goto out;
+		return -EEXIST;
 	}
 
 	/* See if there is room in the item to insert this name. */
@@ -273,14 +270,11 @@ int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
 	slot = path->slots[0];
 	if (data_size + btrfs_item_size(leaf, slot) +
 	    sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
-		ret = -EOVERFLOW;
-	} else {
-		/* plenty of insertion room */
-		ret = 0;
+		return -EOVERFLOW;
 	}
-out:
-	btrfs_free_path(path);
-	return ret;
+
+	/* Plenty of insertion room. */
+	return 0;
 }
 
 /*
Signature · 1 line
2.47.1
author maintainer
#11 lore raw

PATCH 11/27 btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_read_tree_root()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end.

---
 fs/btrfs/disk-io.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/btrfs/disk-io.c +1 -2
index a799216aa264..ab7cbbf90af3 100644
@@ -1089,13 +1089,12 @@ struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root,
 					const struct btrfs_key *key)
 {
 	struct btrfs_root *root;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 
 	path = btrfs_alloc_path();
 	if (!path)
 		return ERR_PTR(-ENOMEM);
 	root = read_tree_root_path(tree_root, path, key);
-	btrfs_free_path(path);
 
 	return root;
 }
Signature · 1 line
2.47.1
author maintainer
#12 lore raw

PATCH 12/27 btrfs: use BTRFS_PATH_AUTO_FREE in load_global_roots()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end with simple goto -> return conversions.

---
 fs/btrfs/disk-io.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
fs/btrfs/disk-io.c +6 -7
index ab7cbbf90af3..f0e4dd0245df 100644
@@ -2198,8 +2198,8 @@ static int load_global_roots_objectid(struct btrfs_root *tree_root,
 
 static int load_global_roots(struct btrfs_root *tree_root)
 {
-	struct btrfs_path *path;
-	int ret = 0;
+	BTRFS_PATH_AUTO_FREE(path);
+	int ret;
 
 	path = btrfs_alloc_path();
 	if (!path)
@@ -2208,18 +2208,17 @@ static int load_global_roots(struct btrfs_root *tree_root)
 	ret = load_global_roots_objectid(tree_root, path,
 					 BTRFS_EXTENT_TREE_OBJECTID, "extent");
 	if (ret)
-		goto out;
+		return ret;
 	ret = load_global_roots_objectid(tree_root, path,
 					 BTRFS_CSUM_TREE_OBJECTID, "csum");
 	if (ret)
-		goto out;
+		return ret;
 	if (!btrfs_fs_compat_ro(tree_root->fs_info, FREE_SPACE_TREE))
-		goto out;
+		return ret;
 	ret = load_global_roots_objectid(tree_root, path,
 					 BTRFS_FREE_SPACE_TREE_OBJECTID,
 					 "free space");
-out:
-	btrfs_free_path(path);
+
 	return ret;
 }
 
Signature · 1 line
2.47.1
author maintainer
#13 lore raw

PATCH 13/27 btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_init_root_free_objectid()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end with simple goto -> return conversions.

---
 fs/btrfs/disk-io.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
fs/btrfs/disk-io.c +5 -8
index f0e4dd0245df..7ae9d020f12a 100644
@@ -4898,7 +4898,7 @@ static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info)
 
 int btrfs_init_root_free_objectid(struct btrfs_root *root)
 {
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	int ret;
 	struct extent_buffer *l;
 	struct btrfs_key search_key;
@@ -4914,14 +4914,13 @@ int btrfs_init_root_free_objectid(struct btrfs_root *root)
 	search_key.offset = (u64)-1;
 	ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
 	if (ret < 0)
-		goto error;
+		return ret;
 	if (ret == 0) {
 		/*
 		 * Key with offset -1 found, there would have to exist a root
 		 * with such id, but this is out of valid range.
 		 */
-		ret = -EUCLEAN;
-		goto error;
+		return -EUCLEAN;
 	}
 	if (path->slots[0] > 0) {
 		slot = path->slots[0] - 1;
@@ -4932,10 +4931,8 @@ int btrfs_init_root_free_objectid(struct btrfs_root *root)
 	} else {
 		root->free_objectid = BTRFS_FIRST_FREE_OBJECTID;
 	}
-	ret = 0;
-error:
-	btrfs_free_path(path);
-	return ret;
+
+	return 0;
 }
 
 int btrfs_get_free_objectid(struct btrfs_root *root, u64 *objectid)
Signature · 1 line
2.47.1
author maintainer
#14 lore raw

PATCH 14/27 btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_get_name()

In reply to David Sterba

s is the trivial pattern for path auto free, initialize at the beginning and free at the end with some return simplifications.

---
 fs/btrfs/export.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
fs/btrfs/export.c +3 -7
index 0c0b8db82df6..a91eaf0ca34e 100644
@@ -223,7 +223,7 @@ static int btrfs_get_name(struct dentry *parent, char *name,
 	struct btrfs_inode *dir = BTRFS_I(d_inode(parent));
 	struct btrfs_root *root = dir->root;
 	struct btrfs_fs_info *fs_info = root->fs_info;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_inode_ref *iref;
 	struct btrfs_root_ref *rref;
 	struct extent_buffer *leaf;
@@ -255,15 +255,12 @@ static int btrfs_get_name(struct dentry *parent, char *name,
 
 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
 	if (ret < 0) {
-		btrfs_free_path(path);
 		return ret;
 	} else if (ret > 0) {
-		if (ino == BTRFS_FIRST_FREE_OBJECTID) {
+		if (ino == BTRFS_FIRST_FREE_OBJECTID)
 			path->slots[0]--;
-		} else {
-			btrfs_free_path(path);
+		else
 			return -ENOENT;
-		}
 	}
 	leaf = path->nodes[0];
 
@@ -280,7 +277,6 @@ static int btrfs_get_name(struct dentry *parent, char *name,
 	}
 
 	read_extent_buffer(leaf, name, name_ptr, name_len);
-	btrfs_free_path(path);
 
 	/*
 	 * have to add the null termination to make sure that reconnect_path
Signature · 1 line
2.47.1
#15 lore raw

Re: [PATCH 14/27] btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_get_name()

Quoted 1 line · David Sterba

s is the trivial pattern for path auto free, initialize at the beginning

^~ This

author maintainer
#16 lore raw

PATCH 15/27 btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_lookup_data_extent()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end.

---
 fs/btrfs/extent-tree.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
fs/btrfs/extent-tree.c +2 -5
index 4a6036e7fa83..e62ab4eaa6ff 100644
@@ -70,9 +70,8 @@ static int block_group_bits(struct btrfs_block_group *cache, u64 bits)
 int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len)
 {
 	struct btrfs_root *root = btrfs_extent_root(fs_info, start);
-	int ret;
 	struct btrfs_key key;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 
 	path = btrfs_alloc_path();
 	if (!path)
@@ -81,9 +80,7 @@ int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len)
 	key.objectid = start;
 	key.type = BTRFS_EXTENT_ITEM_KEY;
 	key.offset = len;
-	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
-	btrfs_free_path(path);
-	return ret;
+	return btrfs_search_slot(NULL, root, &key, path, 0, 0);
 }
 
 /*
Signature · 1 line
2.47.1
author maintainer
#17 lore raw

PATCH 16/27 btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_lookup_extent_info()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end with simple goto -> return conversions.

---
 fs/btrfs/extent-tree.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
fs/btrfs/extent-tree.c +5 -6
index e62ab4eaa6ff..f0dbb96651d6 100644
@@ -100,7 +100,7 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
 	struct btrfs_root *extent_root;
 	struct btrfs_delayed_ref_head *head;
 	struct btrfs_delayed_ref_root *delayed_refs;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_key key;
 	u64 num_refs;
 	u64 extent_flags;
@@ -131,7 +131,7 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
 	extent_root = btrfs_extent_root(fs_info, bytenr);
 	ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
 	if (ret < 0)
-		goto out_free;
+		return ret;
 
 	if (ret > 0 && key.type == BTRFS_METADATA_ITEM_KEY) {
 		if (path->slots[0]) {
@@ -156,7 +156,7 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
 			"unexpected extent item size, has %u expect >= %zu",
 				  item_size, sizeof(*ei));
 			btrfs_abort_transaction(trans, ret);
-			goto out_free;
+			return ret;
 		}
 
 		ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
@@ -167,7 +167,7 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
 		"unexpected zero reference count for extent item (%llu %u %llu)",
 				  key.objectid, key.type, key.offset);
 			btrfs_abort_transaction(trans, ret);
-			goto out_free;
+			return ret;
 		}
 		extent_flags = btrfs_extent_flags(leaf, ei);
 		owner = btrfs_get_extent_owner_root(fs_info, leaf, path->slots[0]);
@@ -213,8 +213,7 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
 		*flags = extent_flags;
 	if (owning_root)
 		*owning_root = owner;
-out_free:
-	btrfs_free_path(path);
+
 	return ret;
 }
 
Signature · 1 line
2.47.1
author maintainer
#18 lore raw

PATCH 17/27 btrfs: use BTRFS_PATH_AUTO_FREE in __btrfs_inc_extent_ref()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end with simple goto -> return conversions.

---
 fs/btrfs/extent-tree.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
fs/btrfs/extent-tree.c +3 -4
index f0dbb96651d6..9c1bd8831e83 100644
@@ -1483,7 +1483,7 @@ static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
 				  struct btrfs_delayed_ref_node *node,
 				  struct btrfs_delayed_extent_op *extent_op)
 {
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct extent_buffer *leaf;
 	struct btrfs_extent_item *item;
 	struct btrfs_key key;
@@ -1504,7 +1504,7 @@ static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
 					   node->parent, node->ref_root, owner,
 					   offset, refs_to_add, extent_op);
 	if ((ret < 0 && ret != -EAGAIN) || !ret)
-		goto out;
+		return ret;
 
 	/*
 	 * Ok we had -EAGAIN which means we didn't have space to insert and
@@ -1529,8 +1529,7 @@ static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
 
 	if (ret)
 		btrfs_abort_transaction(trans, ret);
-out:
-	btrfs_free_path(path);
+
 	return ret;
 }
 
Signature · 1 line
2.47.1
author maintainer
#19 lore raw

PATCH 18/27 btrfs: use BTRFS_PATH_AUTO_FREE in run_delayed_extent_op()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end with simple goto -> return conversions.

---
 fs/btrfs/extent-tree.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
fs/btrfs/extent-tree.c +5 -6
index 9c1bd8831e83..f4ddef135851 100644
@@ -1626,7 +1626,7 @@ static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
 	struct btrfs_fs_info *fs_info = trans->fs_info;
 	struct btrfs_root *root;
 	struct btrfs_key key;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_extent_item *ei;
 	struct extent_buffer *leaf;
 	u32 item_size;
@@ -1657,7 +1657,7 @@ static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
 again:
 	ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
 	if (ret < 0) {
-		goto out;
+		return ret;
 	} else if (ret > 0) {
 		if (metadata) {
 			if (path->slots[0] > 0) {
@@ -1683,7 +1683,7 @@ static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
 			btrfs_err(fs_info,
 		  "missing extent item for extent %llu num_bytes %llu level %d",
 				  head->bytenr, head->num_bytes, head->level);
-			goto out;
+			return ret;
 		}
 	}
 
@@ -1696,13 +1696,12 @@ static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
 			  "unexpected extent item size, has %u expect >= %zu",
 			  item_size, sizeof(*ei));
 		btrfs_abort_transaction(trans, ret);
-		goto out;
+		return ret;
 	}
 
 	ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
 	__run_delayed_extent_op(extent_op, leaf, ei);
-out:
-	btrfs_free_path(path);
+
 	return ret;
 }
 
Signature · 1 line
2.47.1
author maintainer
#20 lore raw

PATCH 19/27 btrfs: use BTRFS_PATH_AUTO_FREE in check_ref_exists()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end.

---
 fs/btrfs/extent-tree.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
fs/btrfs/extent-tree.c +1 -3
index f4ddef135851..122d575c016b 100644
@@ -5459,7 +5459,7 @@ static int check_ref_exists(struct btrfs_trans_handle *trans,
 {
 	struct btrfs_delayed_ref_root *delayed_refs;
 	struct btrfs_delayed_ref_head *head;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_extent_inline_ref *iref;
 	int ret;
 	bool exists = false;
@@ -5476,7 +5476,6 @@ static int check_ref_exists(struct btrfs_trans_handle *trans,
 		 * If we get 0 then we found our reference, return 1, else
 		 * return the error if it's not -ENOENT;
 		 */
-		btrfs_free_path(path);
 		return (ret < 0 ) ? ret : 1;
 	}
 
@@ -5511,7 +5510,6 @@ static int check_ref_exists(struct btrfs_trans_handle *trans,
 	mutex_unlock(&head->mutex);
 out:
 	spin_unlock(&delayed_refs->lock);
-	btrfs_free_path(path);
 	return exists ? 1 : 0;
 }
 
Signature · 1 line
2.47.1
author maintainer
#21 lore raw

PATCH 20/27 btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_drop_subtree()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end with simple goto -> return conversions.

---
 fs/btrfs/extent-tree.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
fs/btrfs/extent-tree.c +2 -5
index 122d575c016b..5de1a1293c93 100644
@@ -6277,7 +6277,7 @@ int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
 			struct extent_buffer *parent)
 {
 	struct btrfs_fs_info *fs_info = root->fs_info;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct walk_control *wc;
 	int level;
 	int parent_level;
@@ -6290,10 +6290,8 @@ int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
 		return -ENOMEM;
 
 	wc = kzalloc(sizeof(*wc), GFP_NOFS);
-	if (!wc) {
-		btrfs_free_path(path);
+	if (!wc)
 		return -ENOMEM;
-	}
 
 	btrfs_assert_tree_write_locked(parent);
 	parent_level = btrfs_header_level(parent);
@@ -6330,7 +6328,6 @@ int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
 	}
 
 	kfree(wc);
-	btrfs_free_path(path);
 	return ret;
 }
 
Signature · 1 line
2.47.1
author maintainer
#22 lore raw

PATCH 21/27 btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_insert_hole_extent()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end with simple goto -> return conversions.

---
 fs/btrfs/file-item.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
fs/btrfs/file-item.c +3 -4
index 5083025d28b2..2f5e6cad8f55 100644
@@ -163,7 +163,7 @@ int btrfs_insert_hole_extent(struct btrfs_trans_handle *trans,
 	int ret = 0;
 	struct btrfs_file_extent_item *item;
 	struct btrfs_key file_key;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct extent_buffer *leaf;
 
 	path = btrfs_alloc_path();
@@ -177,7 +177,7 @@ int btrfs_insert_hole_extent(struct btrfs_trans_handle *trans,
 	ret = btrfs_insert_empty_item(trans, root, path, &file_key,
 				      sizeof(*item));
 	if (ret < 0)
-		goto out;
+		return ret;
 	leaf = path->nodes[0];
 	item = btrfs_item_ptr(leaf, path->slots[0],
 			      struct btrfs_file_extent_item);
@@ -191,8 +191,7 @@ int btrfs_insert_hole_extent(struct btrfs_trans_handle *trans,
 	btrfs_set_file_extent_compression(leaf, item, 0);
 	btrfs_set_file_extent_encryption(leaf, item, 0);
 	btrfs_set_file_extent_other_encoding(leaf, item, 0);
-out:
-	btrfs_free_path(path);
+
 	return ret;
 }
 
Signature · 1 line
2.47.1
author maintainer
#23 lore raw

PATCH 22/27 btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_lookup_bio_sums()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end.

---
 fs/btrfs/file-item.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
fs/btrfs/file-item.c +2 -5
index 2f5e6cad8f55..e0475465ba51 100644
@@ -341,7 +341,7 @@ blk_status_t btrfs_lookup_bio_sums(struct btrfs_bio *bbio)
 	struct btrfs_inode *inode = bbio->inode;
 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
 	struct bio *bio = &bbio->bio;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	const u32 sectorsize = fs_info->sectorsize;
 	const u32 csum_size = fs_info->csum_size;
 	u32 orig_len = bio->bi_iter.bi_size;
@@ -373,10 +373,8 @@ blk_status_t btrfs_lookup_bio_sums(struct btrfs_bio *bbio)
 
 	if (nblocks * csum_size > BTRFS_BIO_INLINE_CSUM_SIZE) {
 		bbio->csum = kmalloc_array(nblocks, csum_size, GFP_NOFS);
-		if (!bbio->csum) {
-			btrfs_free_path(path);
+		if (!bbio->csum)
 			return BLK_STS_RESOURCE;
-		}
 	} else {
 		bbio->csum = bbio->csum_inline;
 	}
@@ -444,7 +442,6 @@ blk_status_t btrfs_lookup_bio_sums(struct btrfs_bio *bbio)
 		bio_offset += count * sectorsize;
 	}
 
-	btrfs_free_path(path);
 	return ret;
 }
 
Signature · 1 line
2.47.1
author maintainer
#24 lore raw

PATCH 23/27 btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_del_csums()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end.

---
 fs/btrfs/file-item.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/btrfs/file-item.c +1 -2
index e0475465ba51..344b4db487a0 100644
@@ -871,7 +871,7 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans,
 		    struct btrfs_root *root, u64 bytenr, u64 len)
 {
 	struct btrfs_fs_info *fs_info = trans->fs_info;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_key key;
 	u64 end_byte = bytenr + len;
 	u64 csum_end;
@@ -1007,7 +1007,6 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans,
 		}
 		btrfs_release_path(path);
 	}
-	btrfs_free_path(path);
 	return ret;
 }
 
Signature · 1 line
2.47.1
author maintainer
#25 lore raw

PATCH 24/27 btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_remove_free_space_inode()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end with simple goto -> return conversions.

---
 fs/btrfs/free-space-cache.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
fs/btrfs/free-space-cache.c +5 -8
index 056546bf9abd..3095cce904b5 100644
@@ -244,7 +244,7 @@ int btrfs_remove_free_space_inode(struct btrfs_trans_handle *trans,
 				  struct inode *inode,
 				  struct btrfs_block_group *block_group)
 {
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_key key;
 	int ret = 0;
 
@@ -257,12 +257,12 @@ int btrfs_remove_free_space_inode(struct btrfs_trans_handle *trans,
 	if (IS_ERR(inode)) {
 		if (PTR_ERR(inode) != -ENOENT)
 			ret = PTR_ERR(inode);
-		goto out;
+		return ret;
 	}
 	ret = btrfs_orphan_add(trans, BTRFS_I(inode));
 	if (ret) {
 		btrfs_add_delayed_iput(BTRFS_I(inode));
-		goto out;
+		return ret;
 	}
 	clear_nlink(inode);
 	/* One for the block groups ref */
@@ -285,12 +285,9 @@ int btrfs_remove_free_space_inode(struct btrfs_trans_handle *trans,
 	if (ret) {
 		if (ret > 0)
 			ret = 0;
-		goto out;
+		return ret;
 	}
-	ret = btrfs_del_item(trans, trans->fs_info->tree_root, path);
-out:
-	btrfs_free_path(path);
-	return ret;
+	return btrfs_del_item(trans, trans->fs_info->tree_root, path);
 }
 
 int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
Signature · 1 line
2.47.1
author maintainer
#26 lore raw

PATCH 25/27 btrfs: use BTRFS_PATH_AUTO_FREE in populate_free_space_tree()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end with simple goto -> return conversions. This applies to both path and path2.

---
 fs/btrfs/free-space-tree.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
fs/btrfs/free-space-tree.c +7 -9
index cae540ec15ed..d1ec02d5e1f6 100644
@@ -1062,7 +1062,8 @@ static int populate_free_space_tree(struct btrfs_trans_handle *trans,
 				    struct btrfs_block_group *block_group)
 {
 	struct btrfs_root *extent_root;
-	struct btrfs_path *path, *path2;
+	BTRFS_PATH_AUTO_FREE(path);
+	BTRFS_PATH_AUTO_FREE(path2);
 	struct btrfs_key key;
 	u64 start, end;
 	int ret;
@@ -1070,17 +1071,16 @@ static int populate_free_space_tree(struct btrfs_trans_handle *trans,
 	path = btrfs_alloc_path();
 	if (!path)
 		return -ENOMEM;
-	path->reada = READA_FORWARD;
 
 	path2 = btrfs_alloc_path();
-	if (!path2) {
-		btrfs_free_path(path);
+	if (!path2)
 		return -ENOMEM;
-	}
+
+	path->reada = READA_FORWARD;
 
 	ret = add_new_free_space_info(trans, block_group, path2);
 	if (ret)
-		goto out;
+		return ret;
 
 	mutex_lock(&block_group->free_space_lock);
 
@@ -1146,9 +1146,7 @@ static int populate_free_space_tree(struct btrfs_trans_handle *trans,
 	ret = 0;
 out_locked:
 	mutex_unlock(&block_group->free_space_lock);
-out:
-	btrfs_free_path(path2);
-	btrfs_free_path(path);
+
 	return ret;
 }
 
Signature · 1 line
2.47.1
author maintainer
#27 lore raw

PATCH 26/27 btrfs: use BTRFS_PATH_AUTO_FREE in clear_free_space_tree()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end with simple goto -> return conversions.

---
 fs/btrfs/free-space-tree.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
fs/btrfs/free-space-tree.c +4 -7
index d1ec02d5e1f6..589ff73ed13a 100644
@@ -1215,7 +1215,7 @@ int btrfs_create_free_space_tree(struct btrfs_fs_info *fs_info)
 static int clear_free_space_tree(struct btrfs_trans_handle *trans,
 				 struct btrfs_root *root)
 {
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_key key;
 	int nr;
 	int ret;
@@ -1231,7 +1231,7 @@ static int clear_free_space_tree(struct btrfs_trans_handle *trans,
 	while (1) {
 		ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
 		if (ret < 0)
-			goto out;
+			return ret;
 
 		nr = btrfs_header_nritems(path->nodes[0]);
 		if (!nr)
@@ -1240,15 +1240,12 @@ static int clear_free_space_tree(struct btrfs_trans_handle *trans,
 		path->slots[0] = 0;
 		ret = btrfs_del_items(trans, root, path, 0, nr);
 		if (ret)
-			goto out;
+			return ret;
 
 		btrfs_release_path(path);
 	}
 
-	ret = 0;
-out:
-	btrfs_free_path(path);
-	return ret;
+	return 0;
 }
 
 int btrfs_delete_free_space_tree(struct btrfs_fs_info *fs_info)
Signature · 1 line
2.47.1
author maintainer
#28 lore raw

PATCH 27/27 btrfs: use BTRFS_PATH_AUTO_FREE in load_free_space_tree()

In reply to David Sterba

This is the trivial pattern for path auto free, initialize at the beginning and free at the end with simple goto -> return conversions.

---
 fs/btrfs/free-space-tree.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
fs/btrfs/free-space-tree.c +6 -12
index 589ff73ed13a..39c6b96a4c25 100644
@@ -1633,9 +1633,8 @@ int load_free_space_tree(struct btrfs_caching_control *caching_ctl)
 {
 	struct btrfs_block_group *block_group;
 	struct btrfs_free_space_info *info;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	u32 extent_count, flags;
-	int ret;
 
 	block_group = caching_ctl->block_group;
 
@@ -1652,10 +1651,9 @@ int load_free_space_tree(struct btrfs_caching_control *caching_ctl)
 	path->reada = READA_FORWARD;
 
 	info = search_free_space_info(NULL, block_group, path, 0);
-	if (IS_ERR(info)) {
-		ret = PTR_ERR(info);
-		goto out;
-	}
+	if (IS_ERR(info))
+		return PTR_ERR(info);
+
 	extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
 	flags = btrfs_free_space_flags(path->nodes[0], info);
 
@@ -1665,11 +1663,7 @@ int load_free_space_tree(struct btrfs_caching_control *caching_ctl)
 	 * there.
 	 */
 	if (flags & BTRFS_FREE_SPACE_USING_BITMAPS)
-		ret = load_free_space_bitmaps(caching_ctl, path, extent_count);
+		return load_free_space_bitmaps(caching_ctl, path, extent_count);
 	else
-		ret = load_free_space_extents(caching_ctl, path, extent_count);
-
-out:
-	btrfs_free_path(path);
-	return ret;
+		return load_free_space_extents(caching_ctl, path, extent_count);
 }
Signature · 1 line
2.47.1
#29 lore raw

Re: [PATCH 27/27] btrfs: use BTRFS_PATH_AUTO_FREE in load_free_space_tree()

Quoted 4 lines · David Sterba
fs/btrfs/free-space-tree.c +0 -0
@@ -1665,11 +1663,7 @@ int load_free_space_tree(struct btrfs_caching_control *caching_ctl)
  	 * there.
  	 */
  	if (flags & BTRFS_FREE_SPACE_USING_BITMAPS)

fs/btrfs/free-space-tree.c int load_free_space_tree(struct btrfs_caching_control *caching_ctl) · line 1669

Quoted 10 lines
fs/btrfs/free-space-tree.c +2 -6
-		ret = load_free_space_bitmaps(caching_ctl, path, extent_count);
+		return load_free_space_bitmaps(caching_ctl, path, extent_count);
  	else
-		ret = load_free_space_extents(caching_ctl, path, extent_count);
-
-out:
-	btrfs_free_path(path);
-	return ret;
+		return load_free_space_extents(caching_ctl, path, extent_count);
  }

No need for the else here, i.e.:

	if (flags & BTRFS_FREE_SPACE_USING_BITMAPS)
		return load_free_space_bitmaps(caching_ctl, path, extent_count);

	return load_free_space_extents(caching_ctl, path, extent_count);
Reviewed-by on the v1 series
#30 lore raw
In reply to David Sterba
Quoted 5 lines · David Sterba

We have the scoped freeing for struct btrfs_path but it's not used as much as it could. This patchset converts the easy cases and it's also a preview if we really want to do that. It makes understanding the exit paths a bit less obvious, but so far I think it's manageable.

Quoted 8 lines

The path is used in many functions and following a few simple patterns, with the macro BTRFS_PATH_AUTO_FREE quite visible among the declarations, so it's nothing hard to be aware of that when reading the code.

The conversion has been done on half of the files, so if somebody wants to continue, feel free. I've skipped functions with more complicated branching where the auto freeing would make it worse.

Let's see where this will lead us to.

Anyways, apart from the 2 small nits,

#31 lore raw
In reply to David Sterba
Quoted 34 lines · David Sterba

We have the scoped freeing for struct btrfs_path but it's not used as much as it could. This patchset converts the easy cases and it's also a preview if we really want to do that. It makes understanding the exit paths a bit less obvious, but so far I think it's manageable.

The path is used in many functions and following a few simple patterns, with the macro BTRFS_PATH_AUTO_FREE quite visible among the declarations, so it's nothing hard to be aware of that when reading the code.

The conversion has been done on half of the files, so if somebody wants to continue, feel free. I've skipped functions with more complicated branching where the auto freeing would make it worse.

David Sterba (27):
btrfs: use BTRFS_PATH_AUTO_FREE in sample_block_group_extent_item()
btrfs: use BTRFS_PATH_AUTO_FREE in insert_dev_extent()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_setup_space_cache()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_start_dirty_block_groups()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_write_dirty_block_groups()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_insert_item()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_commit_inode_delayed_items()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_init_dev_replace()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_run_dev_replace()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_check_dir_item_collision()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_read_tree_root()
btrfs: use BTRFS_PATH_AUTO_FREE in load_global_roots()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_init_root_free_objectid()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_get_name()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_lookup_data_extent()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_lookup_extent_info()
btrfs: use BTRFS_PATH_AUTO_FREE in __btrfs_inc_extent_ref()
btrfs: use BTRFS_PATH_AUTO_FREE in run_delayed_extent_op()
btrfs: use BTRFS_PATH_AUTO_FREE in check_ref_exists()

Quoted 8 lines

btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_drop_subtree()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_insert_hole_extent()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_lookup_bio_sums()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_del_csums()
btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_remove_free_space_inode()
btrfs: use BTRFS_PATH_AUTO_FREE in populate_free_space_tree()
btrfs: use BTRFS_PATH_AUTO_FREE in clear_free_space_tree()
btrfs: use BTRFS_PATH_AUTO_FREE in load_free_space_tree()

Daniel noted that the trivial patches are maybe too trivial and should be grouped into fewer patches. I agree after looking at the series now, so I'll rework it

#32 lore raw
Quoted 3 lines · David Sterba

Daniel noted that the trivial patches are maybe too trivial and should be grouped into fewer patches. I agree after looking at the series now, so I'll rework it

Agreed, though I must say it was super nice for reviewing them, one tiny little change at a time.

#33 lore raw
Quoted 7 lines · Johannes Thumshirn
Quoted 3 lines · David Sterba

Daniel noted that the trivial patches are maybe too trivial and should be grouped into fewer patches. I agree after looking at the series now, so I'll rework it

Agreed, though I must say it was super nice for reviewing them, one tiny little change at a time.

Yeah, I think grouping the simple declaration/free removal patches to one would still be easy to review as the scope is always just one function and nothing is needed to be kept in memory. Which roughly cuts the size of the patchset to half.