From: Jeff Layton <jlayton@kernel.org>
To: Chris Mason <clm@fb.com>, David Sterba <dsterba@suse.com>
Cc: linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org,
kernel-team@fb.com, Jeff Layton <jlayton@kernel.org>
Subject: [PATCH 4/4] btrfs: pre-allocate delayed dir index for non-overwrite rename
Date: Fri, 17 Jul 2026 12:52:39 -0400 [thread overview]
Message-ID: <20260717-btrfs-enomem-v1-4-cdc9c0e265d0@kernel.org> (raw)
In-Reply-To: <20260717-btrfs-enomem-v1-0-cdc9c0e265d0@kernel.org>
For rename() without an overwrite target, pre-allocate the delayed
dir index before any btree modifications so that ENOMEM can be returned
before the source is unlinked from the old directory.
Add a prealloc parameter to btrfs_add_link() that allows callers to
pass pre-allocated delayed dir index resources. When provided,
btrfs_add_link() takes ownership: it either passes the prealloc to
btrfs_insert_dir_item() (which commits or frees it), or frees it
on early error. All existing callers pass NULL to preserve the current
behavior.
In btrfs_rename(), when new_inode is NULL (no overwrite), call
btrfs_prealloc_delayed_dir_index() before the first btree modification
and pass the result through to btrfs_add_link(). If the prealloc fails,
-ENOMEM is returned before any btree state has changed.
For overwrite rename (new_inode != NULL), the transaction still aborts
on ENOMEM since earlier unlink operations have already made irreversible
btree modifications.
Assisted-by: LLM
Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
fs/btrfs/btrfs_inode.h | 4 +++-
fs/btrfs/inode.c | 43 +++++++++++++++++++++++++++++++++++--------
fs/btrfs/tree-log.c | 4 ++--
3 files changed, 40 insertions(+), 11 deletions(-)
diff --git a/fs/btrfs/btrfs_inode.h b/fs/btrfs/btrfs_inode.h
index d5d81f9546c3..176648f6e7b6 100644
--- a/fs/btrfs/btrfs_inode.h
+++ b/fs/btrfs/btrfs_inode.h
@@ -523,9 +523,11 @@ int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index);
int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
struct btrfs_inode *dir, struct btrfs_inode *inode,
const struct fscrypt_str *name);
+struct btrfs_dir_index_prealloc;
int btrfs_add_link(struct btrfs_trans_handle *trans,
struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
- const struct fscrypt_str *name, bool add_backref, u64 index);
+ const struct fscrypt_str *name, bool add_backref, u64 index,
+ struct btrfs_dir_index_prealloc *prealloc);
int btrfs_delete_subvolume(struct btrfs_inode *dir, struct dentry *dentry);
int btrfs_truncate_block(struct btrfs_inode *inode, u64 offset, u64 start, u64 end);
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 4d9947ae08f7..9a717d21f713 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -6675,7 +6675,7 @@ int btrfs_create_new_inode(struct btrfs_trans_handle *trans,
}
} else {
ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
- false, BTRFS_I(inode)->dir_index);
+ false, BTRFS_I(inode)->dir_index, NULL);
if (ret == -ENOMEM) {
/*
* The ENOMEM came before the DIR_ITEM was inserted,
@@ -6720,7 +6720,8 @@ int btrfs_create_new_inode(struct btrfs_trans_handle *trans,
*/
int btrfs_add_link(struct btrfs_trans_handle *trans,
struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
- const struct fscrypt_str *name, bool add_backref, u64 index)
+ const struct fscrypt_str *name, bool add_backref, u64 index,
+ struct btrfs_dir_index_prealloc *prealloc)
{
int ret = 0;
struct btrfs_key key;
@@ -6746,11 +6747,14 @@ int btrfs_add_link(struct btrfs_trans_handle *trans,
}
/* Nothing to clean up yet */
- if (ret)
+ if (ret) {
+ if (prealloc)
+ btrfs_free_delayed_dir_index_prealloc(trans, prealloc);
return ret;
+ }
ret = btrfs_insert_dir_item(trans, name, parent_inode, &key,
- btrfs_inode_type(inode), index, NULL);
+ btrfs_inode_type(inode), index, prealloc);
if (ret == -EEXIST || ret == -EOVERFLOW || ret == -ENOMEM)
goto fail_dir_item;
else if (unlikely(ret)) {
@@ -6904,7 +6908,7 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
inode_set_ctime_current(inode);
ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
- &fname.disk_name, true, index);
+ &fname.disk_name, true, index, NULL);
if (ret)
goto fail;
@@ -8311,14 +8315,14 @@ static int btrfs_rename_exchange(struct inode *old_dir,
}
ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
- new_name, false, old_idx);
+ new_name, false, old_idx, NULL);
if (unlikely(ret)) {
btrfs_abort_transaction(trans, ret);
goto out_fail;
}
ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
- old_name, false, new_idx);
+ old_name, false, new_idx, NULL);
if (unlikely(ret)) {
btrfs_abort_transaction(trans, ret);
goto out_fail;
@@ -8391,6 +8395,7 @@ static int btrfs_rename(struct mnt_idmap *idmap,
struct inode *new_inode = d_inode(new_dentry);
struct inode *old_inode = d_inode(old_dentry);
struct btrfs_rename_ctx rename_ctx;
+ struct btrfs_dir_index_prealloc prealloc = { };
u64 index = 0;
int ret;
int ret2;
@@ -8514,6 +8519,24 @@ static int btrfs_rename(struct mnt_idmap *idmap,
if (ret)
goto out_fail;
+ /*
+ * When not overwriting an existing entry, pre-allocate the delayed
+ * dir index now so that ENOMEM is returned before any btree
+ * modifications. For the overwrite case, too many btree changes
+ * have already happened by the time btrfs_add_link() is called.
+ */
+ if (!new_inode) {
+ ret = btrfs_prealloc_delayed_dir_index(
+ BTRFS_I(new_dir),
+ new_fname.disk_name.name,
+ new_fname.disk_name.len,
+ &prealloc);
+ if (ret)
+ goto out_fail;
+ memcpy(prealloc.item->data + sizeof(struct btrfs_dir_item),
+ new_fname.disk_name.name, new_fname.disk_name.len);
+ }
+
BTRFS_I(old_inode)->dir_index = 0ULL;
if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
/* force full log commit if subvolume involved. */
@@ -8609,7 +8632,9 @@ static int btrfs_rename(struct mnt_idmap *idmap,
}
ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
- &new_fname.disk_name, false, index);
+ &new_fname.disk_name, false, index,
+ prealloc.item ? &prealloc : NULL);
+ prealloc.item = NULL;
if (unlikely(ret)) {
btrfs_abort_transaction(trans, ret);
goto out_fail;
@@ -8634,6 +8659,8 @@ static int btrfs_rename(struct mnt_idmap *idmap,
}
}
out_fail:
+ if (prealloc.item)
+ btrfs_free_delayed_dir_index_prealloc(trans, &prealloc);
if (logs_pinned) {
btrfs_end_log_trans(root);
btrfs_end_log_trans(dest);
diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index 875e4ddc68ea..86a924d7cd43 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -1700,7 +1700,7 @@ static noinline int add_inode_ref(struct walk_control *wc)
}
/* insert our name */
- ret = btrfs_add_link(trans, dir, inode, &name, false, ref_index);
+ ret = btrfs_add_link(trans, dir, inode, &name, false, ref_index, NULL);
if (ret) {
btrfs_abort_log_replay(wc, ret,
"failed to add link for inode %llu in dir %llu ref_index %llu name %.*s root %llu",
@@ -2048,7 +2048,7 @@ static noinline int insert_one_name(struct btrfs_trans_handle *trans,
return PTR_ERR(dir);
}
- ret = btrfs_add_link(trans, dir, inode, name, true, index);
+ ret = btrfs_add_link(trans, dir, inode, name, true, index, NULL);
/* FIXME, put inode into FIXUP list */
--
2.55.0
prev parent reply other threads:[~2026-07-17 16:53 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-17 16:52 [PATCH 0/4] btrfs: handle -ENOMEM errors in some synchronous dirops without aborting Jeff Layton
2026-07-17 16:52 ` [PATCH 1/4] btrfs: split btrfs_insert_delayed_dir_index() into prealloc and commit phases Jeff Layton
2026-07-17 16:52 ` [PATCH 2/4] btrfs: pre-allocate delayed dir index before btree modification Jeff Layton
2026-07-17 16:52 ` [PATCH 3/4] btrfs: handle ENOMEM from btrfs_insert_dir_item() without aborting Jeff Layton
2026-07-17 20:18 ` Boris Burkov
2026-07-17 22:40 ` Jeff Layton
2026-07-17 23:04 ` Boris Burkov
2026-07-17 23:55 ` Jeff Layton
2026-07-18 0:13 ` Boris Burkov
2026-07-18 0:19 ` Qu Wenruo
2026-07-17 16:52 ` Jeff Layton [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260717-btrfs-enomem-v1-4-cdc9c0e265d0@kernel.org \
--to=jlayton@kernel.org \
--cc=clm@fb.com \
--cc=dsterba@suse.com \
--cc=kernel-team@fb.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox