From: Jeff Layton <jlayton@kernel.org>
To: Qu Wenruo <quwenruo.btrfs@gmx.com>, Qu Wenruo <wqu@suse.com>,
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
Subject: Re: [PATCH v2 2/5] btrfs: split btrfs_insert_delayed_dir_index() into prealloc and commit phases
Date: Wed, 05 Aug 2026 18:43:21 -0400 [thread overview]
Message-ID: <aa938a429707345bb706d2a8918c657dc4e0d6f3.camel@kernel.org> (raw)
In-Reply-To: <2527e1c9-a8cd-40bf-96a1-95f74359d21e@gmx.com>
On Thu, 2026-08-06 at 08:04 +0930, Qu Wenruo wrote:
>
> 在 2026/8/5 21:24, Jeff Layton 写道:
> > On Wed, 2026-08-05 at 08:44 +0930, 'Qu Wenruo' via Kernel Team wrote:
> > >
> > > 在 2026/8/5 01:14, Jeff Layton 写道:
> > > > Split btrfs_insert_delayed_dir_index() into three functions using a new
> > > > btrfs_dir_index_prealloc struct to bundle the pre-allocated resources:
> > > >
> > > > - btrfs_prealloc_delayed_dir_index(): performs the two GFP_NOFS
> > > > allocations (delayed node + delayed item) that can fail with -ENOMEM.
> > > > - btrfs_insert_delayed_dir_index_prealloc(): populates the item data,
> > > > inserts into the rb-tree, and reserves metadata space. Cannot fail
> > > > with -ENOMEM since all allocations were done in the prealloc step.
> > > > - btrfs_free_delayed_dir_index_prealloc(): frees pre-allocated
> > > > resources when the caller's btree insertion fails.
> > > >
> > > > The original btrfs_insert_delayed_dir_index() is refactored into a thin
> > > > wrapper that calls the prealloc and commit functions.
> > > >
> > > > This split allows callers to move the fallible memory allocations before
> > > > the point of no return (the DIR_ITEM btree insertion), so that -ENOMEM
> > > > can be returned cleanly without aborting the transaction.
> > > >
> > > > Assisted-by: LLM
> > > > Signed-off-by: Jeff Layton <jlayton@kernel.org>
> > > > ---
> > > > fs/btrfs/delayed-inode.c | 111 +++++++++++++++++++++++++++++++++++++----------
> > > > fs/btrfs/delayed-inode.h | 17 ++++++++
> > > > 2 files changed, 104 insertions(+), 24 deletions(-)
> > > >
> > > > diff --git a/fs/btrfs/delayed-inode.c b/fs/btrfs/delayed-inode.c
> > > > index db2ffab0941a..95d2dca80444 100644
> > > > --- a/fs/btrfs/delayed-inode.c
> > > > +++ b/fs/btrfs/delayed-inode.c
> > > > @@ -6,6 +6,7 @@
> > > >
> > > > #include <linux/slab.h>
> > > > #include <linux/iversion.h>
> > > > +#include <linux/error-injection.h>
> > > > #include "ctree.h"
> > > > #include "fs.h"
> > > > #include "messages.h"
> > > > @@ -1469,35 +1470,74 @@ static void btrfs_release_dir_index_item_space(struct btrfs_trans_handle *trans)
> > > > trans->bytes_reserved -= bytes;
> > > > }
> > > >
> > > > -/* Will 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)
> > > > +/*
> > > > + * Pre-allocate a delayed node and delayed item for a dir index insertion.
> > > > + * Call this before modifying the btree so that ENOMEM can be returned
> > > > + * before any on-disk state has changed.
> > > > + *
> > > > + * Returns 0 on success, -ENOMEM on allocation failure.
> > > > + */
> > > > +int btrfs_prealloc_delayed_dir_index(struct btrfs_inode *dir,
> > > > + const char *name, int name_len,
> > > > + struct btrfs_dir_index_prealloc *prealloc)
> > >
> > > We can directly return a btrfs_dir_index_prealloc pointer, which reduce
> > > the parameter list.
> > >
> >
> > ...but then we'd have to allocate it separately. It's currently
> > allocated on the stack by the caller. I think we ought to keep it this
> > way, but I'm willing to be convinced otherwise.
>
> In the end, there are only two callsites using on-stack memory for
> prealloc structure, one in btrfs_rename() and one in
> btrfs_insert_dir_item().
>
> So it would be not that hard to convert to heap memory based solution.
>
>
> One thing I found a little weird is related to the lifespan management
> of btrfs_dir_index_prealloc.
>
> We have several call sites doing things like:
>
> struct btrfs_dir_index_prealloc prealloc = { };
>
> ret = btrfs_prealloc_delayed_dir_index();
>
> ret = btrfs_add_link(..., prealloc.item ? prealloc : NULL);
> prealloc.item = NULL;
>
> Which looks a little weird to me, as we're using item to indicate if the
> structure is properly allocated, not the structure itself.
>
> If we go regular heap memory allocation, it would be more
> straightforward and more common:
>
> struct btrfs_dir_index_prealloc *prealloc = NULL;
>
> prealloc = btrfs_prealloc_delayed_dir_index();
>
> ret = btrfs_add_link(..., prealloc);
> prealloc = NULL;
>
> Feel free to ignore this one, it's mostly preference.
>
It's a good point. It is more natural to be able to just look and say
"NULL pointer means no prealloc".
OTOH, the entire reason we're doing this is to deal with low-memory
situations. Do we want to add another heap allocation into this
codepath?
Either way, I'll think about it.
Thanks again for the review!
--
Jeff Layton <jlayton@kernel.org>
next prev parent reply other threads:[~2026-08-05 22:43 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 without aborting 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 [this message]
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 ` [PATCH v2 4/5] btrfs: handle ENOMEM from btrfs_insert_dir_item() without aborting Jeff Layton
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=aa938a429707345bb706d2a8918c657dc4e0d6f3.camel@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=quwenruo.btrfs@gmx.com \
--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