Re: [RFC PATCH v6 2/9] fs/namei.c: move create error && negative dentry case in lookup_open up
From: NeilBrown
Date: Sun May 31 2026 - 22:17:25 EST
On Mon, 01 Jun 2026, Jori Koolstra wrote:
> O_CREAT is stripped when create_error is set in lookup_open(), so when
> lookup does not return an inode, the case
>
> if (!dentry->d_inode && (open_flag & O_CREAT))
>
> is always skipped. We can get rid of this cognitive step by handling the
> error case first.
So you are moving an if() statement which can only do anything if
create_error is set (so O_CREAT is clear) before an if statement that
code only do anything if O_CREAT is set (so create_error must be clear)
and either statement changes either create_error or O_CREAT (in
open_flag).
So that must be safe, and I agree that it improves clarity of the code.
Reviewed-by: NeilBrown <neil@xxxxxxxxxx>
Thanks,
NeilBrown
>
> Signed-off-by: Jori Koolstra <jkoolstra@xxxxxxxxx>
> ---
> fs/namei.c | 10 ++++++----
> 1 file changed, 6 insertions(+), 4 deletions(-)
>
> diff --git a/fs/namei.c b/fs/namei.c
> index 137d9fffbe80..4cd132fe3981 100644
> --- a/fs/namei.c
> +++ b/fs/namei.c
> @@ -4499,6 +4499,11 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
> }
> }
>
> + if (unlikely(create_error) && !dentry->d_inode) {
> + error = create_error;
> + goto out_dput;
> + }
> +
> /* Negative dentry, just create the file */
> if (!dentry->d_inode && (open_flag & O_CREAT)) {
> /* but break the directory lease first! */
> @@ -4518,10 +4523,7 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
> if (error)
> goto out_dput;
> }
> - if (unlikely(create_error) && !dentry->d_inode) {
> - error = create_error;
> - goto out_dput;
> - }
> +
> return dentry;
>
> out_dput:
> --
> 2.54.0
>
>