[PATCH v3 4/9] vfs: don't call ->atomic_open with hashed negative in non-O_CREAT case.

From: NeilBrown

Date: Wed Dec 02 2026 - 22:52:12 EST


From: NeilBrown <neil@xxxxxxxxxx>

If lookup_fast_for_open() finds a hashed-negative dentry and O_CREAT
wasn't requested, it will be returned to open_last_lookups() which will
bypass lookup_open() and no call to nfs_atomic_open() will be attempted.

However if it doesn't find a hashed dentry, but lookup_open() does find
a hashed-negative, then that dentry *will* be passed to ->atomic_open.
This is inconsistent and somewhat problematic.

When O_CREAT was requested (even if it isn't passed to ->atomic_create
due to permission issues), ->atomic_open will have exclusive access
to the dentry.
When the dentry is d_in_lookup(), ->atomic_open again has exclusive
access.
The dentry cannot be positive (that case is handled in lookup_open())
but if it is negative but O_CREAT wasn't requested then ->atomic_open
will not have exclusive access. NFS currently has code to handle this
which should not be necessary.

To provide a more consistent context for ->atomic_open handle the
hashed-negative-without-O_CREAT case in lookup_open().

Signed-off-by: NeilBrown <neil@xxxxxxxxxx>
---
Documentation/filesystems/vfs.rst | 13 +++++--------
fs/namei.c | 4 ++++
2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/Documentation/filesystems/vfs.rst b/Documentation/filesystems/vfs.rst
index c1f3ea7aad67..d1d32540e85f 100644
--- a/Documentation/filesystems/vfs.rst
+++ b/Documentation/filesystems/vfs.rst
@@ -648,14 +648,11 @@ otherwise noted.
needs lookup. Cached positive dentries are still handled by
f_op->open().

- If the dentry provided is negative (not in-lookup) and O_CREAT
- isn't set, then there is no guarantee of exclusive access to the
- dentry - another thread might call ->atomic_open() on the same
- dentry at the same time. If needed, a filesystem can ensure this
- doesn't happen by returning 0 from ->d_revalidate when that is
- called with LOOKUP_OPEN on a negative dentry. This will ensure
- that ->atomic_open() only receives an in-lookup dentry, which
- always ensures exclusive access.
+ It is guaranteed that this method has exclusive access to the
+ dentry. If it is not in-lookup, then O_CREAT was requested
+ and the parent is locked exclusively. This will be the case
+ even if O_CREAT isn't still set in open_flag (it may be cleared
+ if permissions checks show that creation is not permitted).

``tmpfile``
called in the end of O_TMPFILE open(). Optional, equivalent to
diff --git a/fs/namei.c b/fs/namei.c
index 55ade0626f2f..f2c7a69f4fdd 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -4608,6 +4608,10 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
if (dentry->d_inode) {
/* Cached positive dentry: will open in do_open(). */
goto out;
+ } else if (!d_in_lookup(dentry) && !(open_flag & O_CREAT)) {
+ /* Cached negative dentry without O_CREAT */
+ error = -ENOENT;
+ goto out_dput;
}

if (open_flag & O_CREAT)
--
2.50.0.107.gf914562f5916.dirty