mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jeff Layton <jlayton@kernel.org>
To: Chris Mason <clm@fb.com>, David Sterba <dsterba@suse.com>
Cc: Qu Wenruo <wqu@suse.com>,
	linux-btrfs@vger.kernel.org,  linux-kernel@vger.kernel.org,
	kernel-team@fb.com,  Jeff Layton <jlayton@kernel.org>
Subject: [PATCH v2 4/5] btrfs: handle ENOMEM from btrfs_insert_dir_item() without aborting
Date: Tue, 04 Aug 2026 11:44:36 -0400	[thread overview]
Message-ID: <20260804-btrfs-enomem-v2-4-4d923170e8c1@kernel.org> (raw)
In-Reply-To: <20260804-btrfs-enomem-v2-0-4d923170e8c1@kernel.org>

Now that btrfs_insert_dir_item() returns -ENOMEM before modifying the
btree (thanks to delayed dir index pre-allocation), callers can handle
ENOMEM gracefully instead of aborting the transaction.

- btrfs_add_link(): add -ENOMEM to the recoverable errors alongside
  -EEXIST and -EOVERFLOW.
- btrfs_create_new_inode(): on -ENOMEM from btrfs_add_link(), clear
  nlink and orphan the newly-created inode instead of aborting.

This turns a filesystem-killing abort into a graceful -ENOMEM return for
create(), mkdir(), mknod(), symlink(), and link() under memory pressure.

Assisted-by: LLM
Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
 fs/btrfs/inode.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 3a2dca093c7d..99e990c592f6 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -6863,7 +6863,17 @@ 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);
-		if (unlikely(ret)) {
+		if (ret == -ENOMEM) {
+			clear_nlink(inode);
+			/*
+			 * btrfs_orphan_add() aborts the transaction itself if it
+			 * fails, so only override ret to -ENOMEM on success.
+			 */
+			ret = btrfs_orphan_add(trans, BTRFS_I(inode));
+			if (!ret)
+				ret = -ENOMEM;
+			goto discard;
+		} else if (unlikely(ret)) {
 			btrfs_abort_transaction(trans, ret);
 			goto discard;
 		}
@@ -6925,7 +6935,7 @@ int btrfs_add_link(struct btrfs_trans_handle *trans,
 
 	ret = btrfs_insert_dir_item(trans, name, parent_inode, &key,
 				    btrfs_inode_type(inode), index, NULL);
-	if (ret == -EEXIST || ret == -EOVERFLOW)
+	if (ret == -EEXIST || ret == -EOVERFLOW || ret == -ENOMEM)
 		goto fail_dir_item;
 	else if (unlikely(ret)) {
 		btrfs_abort_transaction(trans, ret);

-- 
2.55.0


  parent reply	other threads:[~2026-08-04 15:44 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-04 15:44 [PATCH v2 0/5] btrfs: handle -ENOMEM errors in some synchronous dirops " Jeff Layton
2026-08-04 15:44 ` [PATCH v2 1/5] btrfs: use an on-stack path in btrfs_insert_orphan_item() Jeff Layton
2026-08-04 23:18   ` Qu Wenruo
2026-08-04 15:44 ` [PATCH v2 2/5] btrfs: split btrfs_insert_delayed_dir_index() into prealloc and commit phases Jeff Layton
2026-08-04 23:14   ` Qu Wenruo
2026-08-05 11:54     ` Jeff Layton
2026-08-05 22:34       ` Qu Wenruo
2026-08-05 22:43         ` Jeff Layton
2026-08-04 15:44 ` [PATCH v2 3/5] btrfs: pre-allocate delayed dir index before btree modification Jeff Layton
2026-08-04 23:51   ` Qu Wenruo
2026-08-05 12:09     ` Jeff Layton
2026-08-04 15:44 ` Jeff Layton [this message]
2026-08-04 15:44 ` [PATCH v2 5/5] btrfs: pre-allocate delayed dir index for non-overwrite rename Jeff Layton

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=20260804-btrfs-enomem-v2-4-4d923170e8c1@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 \
    --cc=wqu@suse.com \
    /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

all inboxes | Powered by JetHome®