]> git.hungrycats.org Git - linux/commitdiff
btrfs: pre-allocate delayed dir index before btree modification
authorJeff Layton <jlayton@kernel.org>
Tue, 25 Aug 2026 16:04:18 +0000 (12:04 -0400)
committerDavid Sterba <dsterba@suse.com>
Mon, 14 Sep 2026 11:23:37 +0000 (13:23 +0200)
Move the delayed dir index allocation in btrfs_insert_dir_item() before
the insert_with_overflow() call that modifies the btree. Previously, the
allocations happened after the DIR_ITEM was already inserted, meaning an
ENOMEM failure left the btree in a partially-modified state that could
only be resolved by aborting the transaction.

Add an optional caller-provided btrfs_dir_index_prealloc parameter to
btrfs_insert_dir_item(). When non-NULL, ownership of the prealloc
transfers to btrfs_insert_dir_item(). When NULL, it allocates internally.
All existing callers pass NULL to preserve the current behavior.

Since ownership transfers, btrfs_insert_dir_item() must free the prealloc
on every path that does not commit it. Route all such exits (including
the early path allocation failure) through a common out_free_prealloc
label, rather than keying cleanup on need_delayed_index.

Remove the btrfs_insert_delayed_dir_index() wrapper, as there are no
more callers.

Assisted-by: LLM
Suggested-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Jeff Layton <jlayton@kernel.org>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/delayed-inode.c
fs/btrfs/delayed-inode.h
fs/btrfs/dir-item.c
fs/btrfs/dir-item.h
fs/btrfs/inode.c
fs/btrfs/transaction.c

index 21a2d123df14f18fb1f80c74cac0ff229546c717..1f20148c1fd44e199ab7df7d57fcfb3dbcc7dd38 100644 (file)
@@ -687,7 +687,7 @@ static int btrfs_insert_delayed_item(struct btrfs_trans_handle *trans,
        /*
         * For delayed items to insert, we track reserved metadata bytes based
         * on the number of leaves that we will use.
-        * See btrfs_insert_delayed_dir_index() and
+        * See btrfs_insert_delayed_dir_index_prealloc() and
         * btrfs_delayed_item_reserve_metadata()).
         */
        ASSERT(first_item->bytes_reserved == 0);
@@ -1629,23 +1629,6 @@ release_node:
        return ret;
 }
 
-/* Return 0, -ENOMEM or -EEXIST (index number collision, unexpected). */
-int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
-                                  const char *name, int name_len,
-                                  struct btrfs_inode *dir,
-                                  const struct btrfs_disk_key *disk_key, u8 flags,
-                                  u64 index)
-{
-       struct btrfs_dir_index_prealloc *prealloc;
-
-       prealloc = btrfs_prealloc_delayed_dir_index(dir, name, name_len);
-       if (IS_ERR(prealloc))
-               return PTR_ERR(prealloc);
-
-       return btrfs_insert_delayed_dir_index_prealloc(trans, dir, prealloc,
-                                                      disk_key, flags, index);
-}
-
 static bool btrfs_delete_delayed_insertion_item(struct btrfs_delayed_node *node,
                                                u64 index)
 {
@@ -1661,7 +1644,7 @@ static bool btrfs_delete_delayed_insertion_item(struct btrfs_delayed_node *node,
        /*
         * For delayed items to insert, we track reserved metadata bytes based
         * on the number of leaves that we will use.
-        * See btrfs_insert_delayed_dir_index() and
+        * See btrfs_insert_delayed_dir_index_prealloc() and
         * btrfs_delayed_item_reserve_metadata()).
         */
        ASSERT(item->bytes_reserved == 0);
index 6d12a145489f2b4f77b85a6e08ce67aae5197c6c..57ba96cfaf9cb38dadf5f21293022ca3f70eaded 100644 (file)
@@ -115,11 +115,6 @@ struct btrfs_delayed_item {
 };
 
 void btrfs_init_delayed_root(struct btrfs_delayed_root *delayed_root);
-int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
-                                  const char *name, int name_len,
-                                  struct btrfs_inode *dir,
-                                  const struct btrfs_disk_key *disk_key, u8 flags,
-                                  u64 index);
 
 struct btrfs_dir_index_prealloc {
        struct btrfs_delayed_node *node;
index 84f1c64423d32847cb4ee85d992c565af14aa995..3a90736915af6f085e9d76f6f615941a5d53f790 100644 (file)
@@ -106,8 +106,11 @@ int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
  * Will return 0 or -ENOMEM
  */
 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans,
-                         const struct fscrypt_str *name, struct btrfs_inode *dir,
-                         const struct btrfs_key *location, u8 type, u64 index)
+                         const struct fscrypt_str *name,
+                         struct btrfs_inode *dir,
+                         const struct btrfs_key *location, u8 type,
+                         u64 index,
+                         struct btrfs_dir_index_prealloc *prealloc)
 {
        int ret = 0;
        int ret2 = 0;
@@ -119,17 +122,27 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans,
        struct btrfs_key key;
        struct btrfs_disk_key disk_key;
        u32 data_size;
+       const bool need_delayed_index = (root != root->fs_info->tree_root);
 
        key.objectid = btrfs_ino(dir);
        key.type = BTRFS_DIR_ITEM_KEY;
        key.offset = btrfs_name_hash(name->name, name->len);
 
        path = btrfs_alloc_path();
-       if (!path)
-               return -ENOMEM;
+       if (!path) {
+               ret = -ENOMEM;
+               goto out_free_prealloc;
+       }
 
        btrfs_cpu_key_to_disk(&disk_key, location);
 
+       /* Pre-allocate the delayed dir index before modifying the btree. */
+       if (need_delayed_index && !prealloc) {
+               prealloc = btrfs_prealloc_delayed_dir_index(dir, name->name, name->len);
+               if (IS_ERR(prealloc))
+                       return PTR_ERR(prealloc);
+       }
+
        data_size = sizeof(*dir_item) + name->len;
        dir_item = insert_with_overflow(trans, root, path, &key, data_size,
                                        name->name, name->len);
@@ -137,7 +150,7 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans,
                ret = PTR_ERR(dir_item);
                if (ret == -EEXIST)
                        goto second_insert;
-               goto out_free;
+               goto out_free_prealloc;
        }
 
        if (IS_ENCRYPTED(&dir->vfs_inode))
@@ -154,21 +167,21 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans,
        write_extent_buffer(leaf, name->name, name_ptr, name->len);
 
 second_insert:
-       /* FIXME, use some real flag for selecting the extra index */
-       if (root == root->fs_info->tree_root) {
+       if (!need_delayed_index) {
                ret = 0;
-               goto out_free;
+               goto out_free_prealloc;
        }
        btrfs_release_path(path);
 
-       ret2 = btrfs_insert_delayed_dir_index(trans, name->name, name->len, dir,
-                                             &disk_key, type, index);
-out_free:
+       ret2 = btrfs_insert_delayed_dir_index_prealloc(trans, dir, prealloc,
+                                                      &disk_key, type, index);
        if (ret)
                return ret;
-       if (ret2)
-               return ret2;
-       return 0;
+       return ret2;
+
+out_free_prealloc:
+       btrfs_free_delayed_dir_index_prealloc(trans, prealloc);
+       return ret;
 }
 
 static struct btrfs_dir_item *btrfs_lookup_match_dir(
index e52174a8baf92c042b567ed6888cf24f487ad876..8d22976a0a77127b0700b33becde8b18911acba1 100644 (file)
@@ -13,12 +13,14 @@ struct btrfs_path;
 struct btrfs_inode;
 struct btrfs_root;
 struct btrfs_trans_handle;
+struct btrfs_dir_index_prealloc;
 
 int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir_ino,
                          const struct fscrypt_str *name);
 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans,
                          const struct fscrypt_str *name, struct btrfs_inode *dir,
-                         const struct btrfs_key *location, u8 type, u64 index);
+                         const struct btrfs_key *location, u8 type, u64 index,
+                         struct btrfs_dir_index_prealloc *prealloc);
 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
                                             struct btrfs_root *root,
                                             struct btrfs_path *path, u64 dir,
@@ -53,5 +55,4 @@ static inline u64 btrfs_name_hash(const char *name, int len)
 {
        return crc32c((u32)~1, name, len);
 }
-
 #endif
index 2967a307d8f066369db2d2a4ff15030c8f6a95ab..babc291751e96fd9eb34c28ece2d2073d40aaeec 100644 (file)
@@ -6889,7 +6889,7 @@ int btrfs_add_link(struct btrfs_trans_handle *trans,
                return ret;
 
        ret = btrfs_insert_dir_item(trans, name, parent_inode, &key,
-                                   btrfs_inode_type(inode), index);
+                                   btrfs_inode_type(inode), index, NULL);
        if (ret == -EEXIST || ret == -EOVERFLOW)
                goto fail_dir_item;
        else if (unlikely(ret)) {
index 6802b94ed76ff4873c0b78d6bb0c81dfd0b21f56..ca114235bbe1e17c82fcf8b4a1fe07b84e2aa15b 100644 (file)
@@ -1890,8 +1890,7 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
                goto fail;
 
        ret = btrfs_insert_dir_item(trans, &fname.disk_name,
-                                   parent_inode, &key, BTRFS_FT_DIR,
-                                   index);
+                                   parent_inode, &key, BTRFS_FT_DIR, index, NULL);
        if (unlikely(ret)) {
                btrfs_abort_transaction(trans, ret);
                goto fail;