Re: [PATCH 1/2] btrfs: get_inode_info(): check NULL info parameter early

Sun YangKai
2025-08-22T12:59:00Z

> On Thu, Aug 21, 2025 at 09:12:23PM +0800, Sun YangKai wrote:
> > Move the NULL pointer check for the @info parameter to the beginning
> > of get_inode_info() function to avoid unnecessary operations when no
> > output is required.
> >
> > This change provides two benefits:
> > 1. Avoids allocating path and performing tree lookups when @info is NULL
> > 2. Simplifies the control flow by eliminating the redundant check
> >
> > before writing output
> >
> > The functional behavior remains unchanged except for the performance
> > improvement when called with NULL info parameter.
>
> Reordering conditional usually leads to behaviour changes and I think it
> does in this case as well.
>
> > Signed-off-by: Sun YangKai <sunk67188@gmail.com>
> > ---
> >
> > fs/btrfs/send.c | 6 +++---
> > 1 file changed, 3 insertions(+), 3 deletions(-)
> >
> > diff --git a/fs/btrfs/send.c b/fs/btrfs/send.c
> > index 7664025a5af4..5010d17878f9 100644
> > --- a/fs/btrfs/send.c
> > +++ b/fs/btrfs/send.c
> > @@ -913,6 +913,9 @@ static int get_inode_info(struct btrfs_root *root, u64
> > ino,>
> > struct btrfs_inode_item *ii;
> > struct btrfs_key key;
> >
> > + if (!info)
> > + return 0;
>
> Moving the check as the first statement will skip anything else. There's
> one call of get_inode_info() with info == NULL, in apply_dir_move(),
> otherwise all callers fill in a stack variable.
>
> > +
> >
> > path = alloc_path_for_send();
> > if (!path)
> >
> > return -ENOMEM;
> >
> > @@ -927,9 +930,6 @@ static int get_inode_info(struct btrfs_root *root, u64
> > ino,>
> > goto out;
> >
> > }
>
> Right before this there's call to btrfs_search_slot() looking up
> INODE_ITEM for 'ino', there are 3 results, and two are still valid for
> the callers. Only if ret == 0 then the 'if (!info)' check is done.
> Otherwise it's either an error (ret < 0) or -ENOENT (when ret == 1).
>
> So get_inode_info() is also has the semantics of that the inode exists,
> with the reordered 'info' check it never happens.

I got it. Sorry. My bad. I'll fix it in patch v3.

>
> > - if (!info)
> > - goto out;
> > -
> >
> > ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
> >
> > struct btrfs_inode_item);
> >
> > info->size = btrfs_inode_size(path->nodes[0], ii);

Thanks,
Sun YangKai