Skip to contentSkip to conversation

btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_truncate_inode_items()

PATCH In review Yangtao Li 3 participants 6 messages Apr 11, 2025 – Apr 14, 2025 Btrfs file system

lore
Share & tools
lore

Yangtao Li

author
#0 lore raw

PATCH 1/1 btrfs: use BTRFS_PATH_AUTO_FREE in btrfs_truncate_inode_items()

All cleanup paths lead to btrfs_path_free so we can define path with the automatic free callback.

And David Sterba point out that:
We may still find cases worth converting, the typical pattern is btrfs_path_alloc() somewhere near top of the function and btrfs_free_path() called right before a return.

So let's convert it.

---
 fs/btrfs/inode-item.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/btrfs/inode-item.c +1 -2
index 3530de0618c8..c9d37f6bb099 100644
@@ -456,7 +456,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
 			       struct btrfs_truncate_control *control)
 {
 	struct btrfs_fs_info *fs_info = root->fs_info;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct extent_buffer *leaf;
 	struct btrfs_file_extent_item *fi;
 	struct btrfs_key key;
@@ -743,6 +743,5 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
 	if (!ret && control->last_size > new_size)
 		control->last_size = new_size;
 
-	btrfs_free_path(path);
 	return ret;
 }
Signature · 1 line
2.39.0
#1 lore raw
Quoted 9 lines · Yangtao Li

All cleanup paths lead to btrfs_path_free so we can define path with the automatic free callback.

And David Sterba point out that:
We may still find cases worth converting, the typical pattern is btrfs_path_alloc() somewhere near top of the function and btrfs_free_path() called right before a return.

Quoted 6 lines

So let's convert it.

---
 fs/btrfs/inode-item.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

And what about the other functions in that file? We could even get rid of two allocations passing the path from ..._inode_ref() to ..._inode_extref().

Quoted 24 lines
fs/btrfs/inode-item.c +1 -3
index 3530de0618c8..c9d37f6bb099 100644
@@ -456,7 +456,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
                               struct btrfs_truncate_control *control)
 {
        struct btrfs_fs_info *fs_info = root->fs_info;
-       struct btrfs_path *path;
+       BTRFS_PATH_AUTO_FREE(path);
        struct extent_buffer *leaf;
        struct btrfs_file_extent_item *fi;
        struct btrfs_key key;
@@ -743,6 +743,5 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
        if (!ret && control->last_size > new_size)
                control->last_size = new_size;
 
-       btrfs_free_path(path);
        return ret;
 }
--

2.39.0

#2 lore raw
Quoted 12 lines · Daniel Vacek
Quoted 11 lines · Yangtao Li

All cleanup paths lead to btrfs_path_free so we can define path with the automatic free callback.

And David Sterba point out that:
We may still find cases worth converting, the typical pattern is btrfs_path_alloc() somewhere near top of the function and btrfs_free_path() called right before a return.

So let's convert it.

Quoted 8 lines
Quoted 4 lines
---
 fs/btrfs/inode-item.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

And what about the other functions in that file? We could even get rid of two allocations passing the path from ..._inode_ref() to ..._inode_extref().

If you mean to pass the path object from btrfs_del_inode_ref() to btrfs_del_inode_extref() yeah this looks like a good optimization and reducing the allocations (and potential failures).

The other cases in the "..._inode_ref" is btrfs_insert_inode_ref() -> btrfs_insert_inode_extref().

author
#3 lore raw
In reply to Daniel Vacek

Hi Daniel,

Quoted 1 line

And what about the other functions in that file? We could even get rid of two allocations passing the path from ..._inode_ref() to ..._inode_extref().

I made the following changes, is this what you meant? I will do the rest if that's ok.

Thx,
Yangtao

fs/btrfs/inode-item.c +7 -12
index 3530de0618c8..e082d7e27c29 100644
@@ -105,11 +105,11 @@ btrfs_lookup_inode_extref(struct btrfs_trans_handle *trans,
 
 static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
                                  struct btrfs_root *root,
+                                 struct btrfs_path *path,
                                  const struct fscrypt_str *name,
                                  u64 inode_objectid, u64 ref_objectid,
                                  u64 *index)
 {
-       struct btrfs_path *path;
        struct btrfs_key key;
        struct btrfs_inode_extref *extref;
        struct extent_buffer *leaf;
@@ -131,7 +131,7 @@ static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
        if (ret > 0)
                ret = -ENOENT;
        if (ret < 0)
-               goto out;
+               return ret;
 
        /*
         * Sanity check - did we find the right item for this name?
@@ -142,8 +142,7 @@ static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
                                                ref_objectid, name);
        if (!extref) {
                btrfs_abort_transaction(trans, -ENOENT);
-               ret = -ENOENT;
-               goto out;
+               return -ENOENT;
        }
 
        leaf = path->nodes[0];
@@ -156,8 +155,7 @@ static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
                 * Common case only one ref in the item, remove the
                 * whole item.
                 */
-               ret = btrfs_del_item(trans, root, path);
-               goto out;
+               return btrfs_del_item(trans, root, path);
        }
 
        ptr = (unsigned long)extref;
@@ -168,9 +166,6 @@ static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
 
        btrfs_truncate_item(trans, path, item_size - del_len, 1);
 
-out:
-       btrfs_free_path(path);
-
        return ret;
 }
 
@@ -178,7 +173,7 @@ int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
                        struct btrfs_root *root, const struct fscrypt_str *name,
                        u64 inode_objectid, u64 ref_objectid, u64 *index)
 {
-       struct btrfs_path *path;
+       BTRFS_PATH_AUTO_FREE(path);
        struct btrfs_key key;
        struct btrfs_inode_ref *ref;
        struct extent_buffer *leaf;
@@ -230,7 +225,7 @@ int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
                              item_size - (ptr + sub_item_len - item_start));
        btrfs_truncate_item(trans, path, item_size - sub_item_len, 1);
 out:
-       btrfs_free_path(path);
+       btrfs_release_path(path);
 
        if (search_ext_refs) {
                /*
@@ -238,7 +233,7 @@ int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
                 * name in our ref array. Find and remove the extended
                 * inode ref then.
                 */
-               return btrfs_del_inode_extref(trans, root, name,
+               return btrfs_del_inode_extref(trans, root, path, name,
                                              inode_objectid, ref_objectid, index);
        }
#4 lore raw
Quoted 7 lines · 李扬韬

Hi Daniel,

Quoted 1 line

And what about the other functions in that file? We could even get rid of two allocations passing the path from ..._inode_ref() to ..._inode_extref().

I made the following changes, is this what you meant? I will do the rest if that's ok.

Yeah, quite almost.

Quoted 10 lines

Thx,
Yangtao

fs/btrfs/inode-item.c +0 -0
index 3530de0618c8..e082d7e27c29 100644
@@ -105,11 +105,11 @@ btrfs_lookup_inode_extref(struct btrfs_trans_handle *trans,
 
 static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,

fs/btrfs/inode-item.c btrfs_lookup_inode_extref(struct btrfs_trans_handle *trans, · line 115

Quoted 10 lines
fs/btrfs/inode-item.c +1 -1
                                  struct btrfs_root *root,
+                                 struct btrfs_path *path,
                                  const struct fscrypt_str *name,
                                  u64 inode_objectid, u64 ref_objectid,
                                  u64 *index)
 {
-       struct btrfs_path *path;
        struct btrfs_key key;
        struct btrfs_inode_extref *extref;
        struct extent_buffer *leaf;

I think you missed this:

@@ -123,10 +123,6 @@ static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,

        key.type = BTRFS_INODE_EXTREF_KEY;
        key.offset = btrfs_extref_hash(ref_objectid, name->name, name->len);

- path = btrfs_alloc_path();
- if (!path)
- return -ENOMEM;
-

        ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
        if (ret > 0)
                ret = -ENOENT;

Which was ironically the whole point ;-)

fs/btrfs/inode-item.c static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans, · line 132

Quoted 3 lines
fs/btrfs/inode-item.c +0 -0
@@ -131,7 +131,7 @@ static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
        if (ret > 0)
                ret = -ENOENT;

You can also directly return -ENOENT; here.

Quoted 62 lines
fs/btrfs/inode-item.c +6 -11
        if (ret < 0)
-               goto out;
+               return ret;
 
        /*
         * Sanity check - did we find the right item for this name?
@@ -142,8 +142,7 @@ static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
                                                ref_objectid, name);
        if (!extref) {
                btrfs_abort_transaction(trans, -ENOENT);
-               ret = -ENOENT;
-               goto out;
+               return -ENOENT;
        }
 
        leaf = path->nodes[0];
@@ -156,8 +155,7 @@ static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
                 * Common case only one ref in the item, remove the
                 * whole item.
                 */
-               ret = btrfs_del_item(trans, root, path);
-               goto out;
+               return btrfs_del_item(trans, root, path);
        }
 
        ptr = (unsigned long)extref;
@@ -168,9 +166,6 @@ static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
 
        btrfs_truncate_item(trans, path, item_size - del_len, 1);
 
-out:
-       btrfs_free_path(path);
-
        return ret;
 }
 
@@ -178,7 +173,7 @@ int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
                        struct btrfs_root *root, const struct fscrypt_str *name,
                        u64 inode_objectid, u64 ref_objectid, u64 *index)
 {
-       struct btrfs_path *path;
+       BTRFS_PATH_AUTO_FREE(path);
        struct btrfs_key key;
        struct btrfs_inode_ref *ref;
        struct extent_buffer *leaf;
@@ -230,7 +225,7 @@ int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
                              item_size - (ptr + sub_item_len - item_start));
        btrfs_truncate_item(trans, path, item_size - sub_item_len, 1);
 out:
-       btrfs_free_path(path);
+       btrfs_release_path(path);
 
        if (search_ext_refs) {
                /*
@@ -238,7 +233,7 @@ int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
                 * name in our ref array. Find and remove the extended
                 * inode ref then.
                 */
-               return btrfs_del_inode_extref(trans, root, name,
+               return btrfs_del_inode_extref(trans, root, path, name,
                                              inode_objectid, ref_objectid, index);
        }
#5 lore raw
In reply to 李扬韬
Quoted 58 lines · 李扬韬

Hi Daniel,

Quoted 1 line

And what about the other functions in that file? We could even get rid of two allocations passing the path from ..._inode_ref() to ..._inode_extref().

I made the following changes, is this what you meant? I will do the rest if that's ok.

Thx,
Yangtao

fs/btrfs/inode-item.c +4 -6
index 3530de0618c8..e082d7e27c29 100644
@@ -105,11 +105,11 @@ btrfs_lookup_inode_extref(struct btrfs_trans_handle *trans,
 
 static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
                                  struct btrfs_root *root,
+                                 struct btrfs_path *path,
                                  const struct fscrypt_str *name,
                                  u64 inode_objectid, u64 ref_objectid,
                                  u64 *index)
 {
-       struct btrfs_path *path;
        struct btrfs_key key;
        struct btrfs_inode_extref *extref;
        struct extent_buffer *leaf;
@@ -131,7 +131,7 @@ static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
        if (ret > 0)
                ret = -ENOENT;
        if (ret < 0)
-               goto out;
+               return ret;
 
        /*
         * Sanity check - did we find the right item for this name?
@@ -142,8 +142,7 @@ static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
                                                ref_objectid, name);
        if (!extref) {
                btrfs_abort_transaction(trans, -ENOENT);
-               ret = -ENOENT;
-               goto out;
+               return -ENOENT;
        }
 
        leaf = path->nodes[0];
@@ -156,8 +155,7 @@ static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
                 * Common case only one ref in the item, remove the
                 * whole item.
                 */
-               ret = btrfs_del_item(trans, root, path);
-               goto out;
+               return btrfs_del_item(trans, root, path);
        }
 
        ptr = (unsigned long)extref;

fs/btrfs/inode-item.c static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans, · line 169

Quoted 7 lines
fs/btrfs/inode-item.c +0 -3
@@ -168,9 +166,6 @@ static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
 
        btrfs_truncate_item(trans, path, item_size - del_len, 1);
 
-out:
-       btrfs_free_path(path);
-
        return ret;

And if I look correctly you can also directly return 0 here.

Quoted 28 lines
fs/btrfs/inode-item.c +3 -3
 }
 
@@ -178,7 +173,7 @@ int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
                        struct btrfs_root *root, const struct fscrypt_str *name,
                        u64 inode_objectid, u64 ref_objectid, u64 *index)
 {
-       struct btrfs_path *path;
+       BTRFS_PATH_AUTO_FREE(path);
        struct btrfs_key key;
        struct btrfs_inode_ref *ref;
        struct extent_buffer *leaf;
@@ -230,7 +225,7 @@ int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
                              item_size - (ptr + sub_item_len - item_start));
        btrfs_truncate_item(trans, path, item_size - sub_item_len, 1);
 out:
-       btrfs_free_path(path);
+       btrfs_release_path(path);
 
        if (search_ext_refs) {
                /*
@@ -238,7 +233,7 @@ int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
                 * name in our ref array. Find and remove the extended
                 * inode ref then.
                 */
-               return btrfs_del_inode_extref(trans, root, name,
+               return btrfs_del_inode_extref(trans, root, path, name,
                                              inode_objectid, ref_objectid, index);
        }