Re: [PATCH v8 07/12] vfs: short-circuit MAY_WRITE access for O_DIRECTORY opens
From: Jori Koolstra
Date: Wed Nov 25 2026 - 05:29:34 EST
> Op 25-11-2026 04:42 EST schreef NeilBrown <neilb@xxxxxxxxxxx>:
>
>
> On Wed, 25 Nov 2026, Jori Koolstra wrote:
> > > Op 25-11-2026 01:21 EST schreef Askar Safin <safinaskar@xxxxxxxxx>:
> > >
> > >
> > > Jori Koolstra <jkoolstra@xxxxxxxxx>:
> > > > Requesting write access on a directory can never succeed. Rather
> > > > than performing a path-walk to determine whether the target is
> > > > actually a directory (-EISDIR) or not (-ENOTDIR), or does not exist
> > > > (-ENOENT), etc., we short-circuit to -ENOTDIR.
> > > >
> > > > Currently O_WRONLY for directories is only blocked in may_open(),
> > > > which happens after we have the inode for the target, so after any
> > > > create via O_CREAT|O_DIRECTORY.
> > > >
> > > > The advantage of short-circuiting is that we don't have to add even more
> > > > logic to lookup_open() to differentiate -EISDIR/-ENOTDIR. Also, for
> > > > filesystems that define ->atomic_open, handling this cannot even be done
> > > > at the VFS level, as we can't know ahead what the result of the lookup
> > > > will be.
> > > >
> > > > Suggested-by: Christian Brauner (Amutable) <brauner@xxxxxxxxxx>
> > > > Reviewed-by: NeilBrown <neil@xxxxxxxxxx>
> > > > Signed-off-by: Jori Koolstra <jkoolstra@xxxxxxxxx>
> > > > ---
> > > > fs/open.c | 9 +++++++++
> > > > 1 file changed, 9 insertions(+)
> > > >
> > > > diff --git a/fs/open.c b/fs/open.c
> > > > index e43f02ff64ac..4334f30bb7ff 100644
> > > > --- a/fs/open.c
> > > > +++ b/fs/open.c
> > > > @@ -1343,6 +1343,15 @@ inline int build_open_flags(const struct open_how *how, struct open_flags *op)
> > > >
> > > > op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
> > > >
> > > > + /*
> > > > + * Requesting write access on a directory can never succeed. Rather
> > > > + * than performing a path-walk to determine whether the target is
> > > > + * actually a directory (-EISDIR) or not (-ENOTDIR), we short-circuit
> > > > + * to -ENOTDIR.
> > > > + */
> > > > + if ((flags & O_DIRECTORY) && !(flags & __O_TMPFILE) && (acc_mode & MAY_WRITE))
> > > > + return -ENOTDIR;
> > > > +
> > >
> > > Please, do -EISDIR here, not -ENOTDIR. You returning error here, **because** user attempts to write to **directory**. EISDIR is natural here, ENOTDIR is not.
> > >
> >
> > That's a fair point, and I have thought about this. The downside is that
> > if we return -EISDIR there, that will also be returned if you open(O_DIRECTORY)
> > what turns out to be a regular file. So you may then get -EISDIR for something not
> > a directory.
> >
> > The semantics of -ENOTDIR here that I had in my mind is something like "the thing
> > you are trying to do can NOT be done _with_ a DIRectory." It made a bit more sense
> > to me (but only a bit.) Really it should be -ENOTWITHDIR or something...
>
> The problem is that "flags" contains an invalid value. The correct
> error for that (according to open.2 man page) is EINVAL.
>
This is best probably. The counter is that I wanted to change errnos as little
as possible. Now you get at least something that could already happen when
you did a normal open(O_DIRECTORY). Were this designed from the start -EINVAL
is obviously what is needed here.
I dunno, not sure what other people think.
Best,
Jori.