* [PATCH 0/8] vfs: improvements for atomic_open and vfs_lookup_open
@ 2026-12-02 6:05 NeilBrown
2026-12-02 6:05 ` [PATCH 1/8] vfs: revise and expand documentation for atomic_open NeilBrown
` (7 more replies)
0 siblings, 8 replies; 9+ messages in thread
From: NeilBrown @ 2026-12-02 6:05 UTC (permalink / raw)
To: linux-kernel
This is a preview to get sashiko review before posting to reviewers
(Or is there a better way to do that?)
[PATCH 1/8] vfs: revise and expand documentation for atomic_open.
[PATCH 2/8] vfs: only set op.mode when O_CREAT in vfs_lookup_open()
[PATCH 3/8] vfs: don't call ->atomic_open with hashed negative in
[PATCH 4/8] vfs: add some allowed open flags to vfs_lookup_open()
[PATCH 5/8] vfs: O_NONBLOCK|O_CREAT open shouldn't wait for directory
[PATCH 6/8] vfs: don't return -ENODEV from vfs_lookup_open()
[PATCH 7/8] vfs: change vfs_lookup_open() to use do_open(), not
[PATCH 8/8] vfs: add MAY_SERVE_FILE and FMODE_SERVE_FILE
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 1/8] vfs: revise and expand documentation for atomic_open.
2026-12-02 6:05 [PATCH 0/8] vfs: improvements for atomic_open and vfs_lookup_open NeilBrown
@ 2026-12-02 6:05 ` NeilBrown
2026-12-02 6:05 ` [PATCH 2/8] vfs: only set op.mode when O_CREAT in vfs_lookup_open() NeilBrown
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: NeilBrown @ 2026-12-02 6:05 UTC (permalink / raw)
To: linux-kernel
From: NeilBrown <neil@brown.name>
atomic_open is a complex operation which different filesystems implement
quite differently. The available documentation doesn't give clear
guidance on how it should be implemented.
nfsd has a particular need to open only regular files, but to get
precise information about what was found if it wasn't a regular file.
This is slightly different to the syscall calling needs. In particular
it suggests that __O_REGULAR shouldn't always result in -EFTYPE.
In any case that does involve creating open state, using
finish_no_open() is simplest as it reduces the need to check
__O_REGULAR, O_DIRECTORY, O_NOFOLLOW.
So refresh the documentation to give guidance on the choice between
finish_no_open, finish_open, and an error. Efficiency always wins, but
when that isn't an issue, prefer finish_no_open().
Also clarify the required behaviour when __O_REGULAR is given. This
should return -EISDIR if a directory is found as nfsd needs this. If a
symlink is found then __O_REGULAR does NOT apply: O_NOFOLLOW must be
used to decided if it is safe to not return the looked-up dentry.
Assorted improvements thanks to Andreas Gruenbacher <agruenba@redhat.com>
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: NeilBrown <neil@brown.name>
---
Documentation/filesystems/vfs.rst | 68 +++++++++++++++++++++++++------
fs/namei.c | 3 ++
2 files changed, 59 insertions(+), 12 deletions(-)
diff --git a/Documentation/filesystems/vfs.rst b/Documentation/filesystems/vfs.rst
index 9ab182dbbd73..c1f3ea7aad67 100644
--- a/Documentation/filesystems/vfs.rst
+++ b/Documentation/filesystems/vfs.rst
@@ -600,18 +600,62 @@ otherwise noted.
inode.
``atomic_open``
- called on the last component of an open. Using this optional
- method the filesystem can look up, possibly create and open the
- file in one atomic operation. If it wants to leave actual
- opening to the caller (e.g. if the file turned out to be a
- symlink, device, or just something filesystem won't do atomic
- open for), it may signal this by returning finish_no_open(file,
- dentry). This method is only called if the last component is
- negative or needs lookup. Cached positive dentries are still
- handled by f_op->open(). If the file was created, FMODE_CREATED
- flag should be set in file->f_mode. In case of O_EXCL the
- method must only succeed if the file didn't exist and hence
- FMODE_CREATED shall always be set on success.
+ called on the last component of an open. This optional method
+ can look up, create, truncate, and open the file in one atomic
+ operation. This is needed if the filesystem content can change
+ asynchronously and specifically, if a negative dentry is not a
+ guarantee that the object doesn't exist. It is also useful if
+ combinations of revalidate, lookup, create, open, and truncate
+ can be performed more efficiently than with a sequence of
+ individual operations.
+
+ If the object found is not a file or directory, or if
+ lookup/create succeeded without establishing any "open" state,
+ then finish_no_open() should be called to indicate that the
+ dentry is ready to be handled by normal VFS processing.
+ FMODE_CREATED should be set in file->f_mode if the object was
+ created; this will prevent further access permission checks and
+ handling of O_TRUNC and O_EXCL.
+
+ If the lookup/create operation established some open state for a
+ file or directory, the operation should be completed by calling
+ finish_open(). Passing NULL as the "open" function to
+ finish_open() is unlikely to be useful as that assumes that no
+ open state has been established.
+
+ atomic_open() may generate errors related to O_DIRECTORY,
+ __O_REGULAR, O_EXCL, O_NOFOLLOW, but is not required to as the
+ caller will check those against the resulting dentry and
+ generate any error needed, possibly closing the file if it was
+ opened by finish_open(). atomic_open() is encouraged to handle
+ these flags only when doing so is more efficient than not.
+
+ If __O_REGULAR is handled, it should generate -EISDIR if the
+ name is known to be a directory, and -EFTYPE if it is some other
+ non-regular file other than a symbolic link. Handling of
+ symbolic links should be guided by O_NOFOLLOW, not __O_REGULAR:
+ -ELOOP can be returned if O_NOFOLLOW is set; otherwise the symlink
+ should be returned through finish_no_open().
+
+ The focus for atomic_open() is to provide the correct dentry and
+ to set FMODE_CREATED as accurately as possible. If O_EXCL is
+ set, FMODE_CREATED should only be set if this operation
+ certainly created the object. If O_EXCL is not set,
+ FMODE_CREATED should be set if it is possible that this operation
+ created the object.
+
+ This method is only called if the last component is negative or
+ 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.
``tmpfile``
called in the end of O_TMPFILE open(). Optional, equivalent to
diff --git a/fs/namei.c b/fs/namei.c
index c79eecffe4ac..64ee0f7ef421 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -5133,6 +5133,9 @@ static struct file *path_openat(struct nameidata *nd,
error = -EINVAL;
}
fput_close(file);
+ if (error == -EISDIR &&
+ (op->open_flag & __O_REGULAR))
+ error = -EFTYPE;
if (error == -EOPENSTALE) {
if (flags & LOOKUP_RCU)
error = -ECHILD;
base-commit: 84fb6f8a1ba91eee3ffe6aa519686fce851a0e42
--
2.50.0.107.gf914562f5916.dirty
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 2/8] vfs: only set op.mode when O_CREAT in vfs_lookup_open()
2026-12-02 6:05 [PATCH 0/8] vfs: improvements for atomic_open and vfs_lookup_open NeilBrown
2026-12-02 6:05 ` [PATCH 1/8] vfs: revise and expand documentation for atomic_open NeilBrown
@ 2026-12-02 6:05 ` NeilBrown
2026-12-02 6:05 ` [PATCH 3/8] vfs: don't call ->atomic_open with hashed negative in non-O_CREAT case NeilBrown
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: NeilBrown @ 2026-12-02 6:05 UTC (permalink / raw)
To: linux-kernel
From: NeilBrown <neil@brown.name>
build_open_flags() only sets op.mode when O_CREAT (or __O_TMPFILE).
Change vfs_lookup_open() to follow the same pattern for consistency.
Signed-off-by: NeilBrown <neil@brown.name>
---
fs/namei.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/fs/namei.c b/fs/namei.c
index 64ee0f7ef421..bff4dfaf6721 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -4767,9 +4767,9 @@ struct file *vfs_lookup_open(struct path *parent, struct qstr *last,
nd.flags |= LOOKUP_CREATE;
if (open_flag & O_EXCL)
nd.flags |= LOOKUP_EXCL;
+ op.mode = mode;
}
op.open_flag = open_flag;
- op.mode = mode;
dentry = lookup_open(&nd, file, &op);
if (IS_ERR(dentry))
--
2.50.0.107.gf914562f5916.dirty
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 3/8] vfs: don't call ->atomic_open with hashed negative in non-O_CREAT case.
2026-12-02 6:05 [PATCH 0/8] vfs: improvements for atomic_open and vfs_lookup_open NeilBrown
2026-12-02 6:05 ` [PATCH 1/8] vfs: revise and expand documentation for atomic_open NeilBrown
2026-12-02 6:05 ` [PATCH 2/8] vfs: only set op.mode when O_CREAT in vfs_lookup_open() NeilBrown
@ 2026-12-02 6:05 ` NeilBrown
2026-12-02 6:05 ` [PATCH 4/8] vfs: add some allowed open flags to vfs_lookup_open() NeilBrown
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: NeilBrown @ 2026-12-02 6:05 UTC (permalink / raw)
To: linux-kernel
From: NeilBrown <neil@brown.name>
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@brown.name>
---
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 bff4dfaf6721..441560ba755a 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
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 4/8] vfs: add some allowed open flags to vfs_lookup_open()
2026-12-02 6:05 [PATCH 0/8] vfs: improvements for atomic_open and vfs_lookup_open NeilBrown
` (2 preceding siblings ...)
2026-12-02 6:05 ` [PATCH 3/8] vfs: don't call ->atomic_open with hashed negative in non-O_CREAT case NeilBrown
@ 2026-12-02 6:05 ` NeilBrown
2026-12-02 6:05 ` [PATCH 5/8] vfs: O_NONBLOCK|O_CREAT open shouldn't wait for directory delegation NeilBrown
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: NeilBrown @ 2026-12-02 6:05 UTC (permalink / raw)
To: linux-kernel
From: NeilBrown <neil@brown.name>
vfs_lookup_open() must allow:
O_LARGEFILE so that large files can be opened.
O_NONBLOCK so that break_lease() can be asked to return -EWOULDBLOCK.
Also allow O_NOFOLLOW as we don't/can't handle symlinks. In fact we
should enforce O_NOFOLLOW for the same reason we enforce __O_REGULAR.
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: NeilBrown <neil@brown.name>
---
fs/namei.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/fs/namei.c b/fs/namei.c
index 441560ba755a..afd001fe4391 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -4750,11 +4750,12 @@ struct file *vfs_lookup_open(struct path *parent, struct qstr *last,
int error = 0;
WARN_ONCE(mode & ~S_IALLUGO, "mode must only have permission bits");
- WARN_ONCE(open_flag & ~(O_ACCMODE|O_CREAT|O_EXCL|O_TRUNC|__O_REGULAR),
+ WARN_ONCE(open_flag & ~(O_ACCMODE|O_CREAT|O_EXCL|O_TRUNC|__O_REGULAR|
+ O_NONBLOCK|O_LARGEFILE|O_NOFOLLOW),
"open_flag has unsupported flags");
mode |= S_IFREG;
- open_flag |= __O_REGULAR;
+ open_flag |= __O_REGULAR | O_NOFOLLOW;
error = lookup_noperm_common(last, parent->dentry);
if (error)
--
2.50.0.107.gf914562f5916.dirty
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 5/8] vfs: O_NONBLOCK|O_CREAT open shouldn't wait for directory delegation
2026-12-02 6:05 [PATCH 0/8] vfs: improvements for atomic_open and vfs_lookup_open NeilBrown
` (3 preceding siblings ...)
2026-12-02 6:05 ` [PATCH 4/8] vfs: add some allowed open flags to vfs_lookup_open() NeilBrown
@ 2026-12-02 6:05 ` NeilBrown
2026-12-02 6:05 ` [PATCH 6/8] vfs: don't return -ENODEV from vfs_lookup_open() NeilBrown
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: NeilBrown @ 2026-12-02 6:05 UTC (permalink / raw)
To: linux-kernel
From: NeilBrown <neil@brown.name>
An open() of a regular file can block if there is an active lease on that
file, but this can be prevented by opening with O_NONBLOCK.
An open(O_CREAT) of a non-existent file can block if there is an active
delegation on the parent directory but this CANNOT be prevented with
O_NONBLOCK.
If nfsd is to use common VFS code for open/create it needs to be able to
prevent this blocking. It is conceivable that a user-space application
might need this too.
So extend O_NONBLOCK protection to not block on a directory delegation
when creating a file.
Fixes: 134796f43a5e ("vfs: break parent dir delegations in open(..., O_CREAT) codepath")
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: NeilBrown <neil@brown.name>
---
fs/namei.c | 20 ++++++++++++++++----
1 file changed, 16 insertions(+), 4 deletions(-)
diff --git a/fs/namei.c b/fs/namei.c
index afd001fe4391..38b6eb37d08d 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -4671,8 +4671,14 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
}
if (create_dir) {
- struct dentry *res = vfs_mkdir_no_perm(idmap, dir_inode, dentry, mode,
- &delegated_inode);
+ struct dentry *res;
+
+ if (op->open_flag & O_NONBLOCK)
+ res = vfs_mkdir_no_perm(idmap, dir_inode, dentry, mode,
+ NULL);
+ else
+ res = vfs_mkdir_no_perm(idmap, dir_inode, dentry, mode,
+ &delegated_inode);
error = PTR_ERR_OR_ZERO(res);
if (!error) {
dentry = res;
@@ -4684,8 +4690,14 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
error = -EIO;
}
} else {
- error = vfs_create_no_perm(idmap, dir_inode, dentry, mode,
- &delegated_inode);
+\x11 if (op->open_flag & O_NONBLOCK)
+ error = vfs_create_no_perm(idmap, dir_inode, dentry,
+ mode, NULL);
+ else
+ error = vfs_create_no_perm(idmap, dir_inode, dentry,
+ mode, &delegated_inode);
+ if (error)
+ goto out_dput;
}
if (error)
goto out_dput;
--
2.50.0.107.gf914562f5916.dirty
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 6/8] vfs: don't return -ENODEV from vfs_lookup_open()
2026-12-02 6:05 [PATCH 0/8] vfs: improvements for atomic_open and vfs_lookup_open NeilBrown
` (4 preceding siblings ...)
2026-12-02 6:05 ` [PATCH 5/8] vfs: O_NONBLOCK|O_CREAT open shouldn't wait for directory delegation NeilBrown
@ 2026-12-02 6:05 ` NeilBrown
2026-12-02 6:05 ` [PATCH 7/8] vfs: change vfs_lookup_open() to use do_open(), not vfs_open() NeilBrown
2026-12-02 6:05 ` [PATCH 8/8] vfs: add MAY_SERVE_FILE and FMODE_SERVE_FILE NeilBrown
7 siblings, 0 replies; 9+ messages in thread
From: NeilBrown @ 2026-12-02 6:05 UTC (permalink / raw)
To: linux-kernel
From: NeilBrown <neil@brown.name>
-ENODEV is not generally an error meaning "the object is a device"
and nfsd - the only intended caller of vfs_lookup_open() - does not
benefit from knowing it was a device file. So return -EFTYPE in
that case.
Also switch to testing the dentry type rather than dereferencing the
inode to get the type.
-EISDIR is widely used to mean "the object is a directory which is
not what is wanted".
-ELOOP is sometimes used elsewhere to mean "a symlink was found but
cannot be handled".
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: NeilBrown <neil@brown.name>
---
fs/namei.c | 24 +++++-------------------
1 file changed, 5 insertions(+), 19 deletions(-)
diff --git a/fs/namei.c b/fs/namei.c
index 38b6eb37d08d..d7980391a40d 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -4746,9 +4746,7 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
* determine the type of file found from the error.
* -EISDIR : a directory was found
* -ELOOP : a symlink was found
- * -ENODEV : a block or character device special file was found
- * -EFTYPE : any other non-regular file was found, such as FIFO or SOCK.
- * or ->atomic_open responded to __O_REGULAR.
+ * -EFTYPE : any other non-regular file was found, device-special, FIFO or SOCK
*
* Returns: the opened struct file, or an error.
*/
@@ -4796,24 +4794,12 @@ struct file *vfs_lookup_open(struct path *parent, struct qstr *last,
error = -ENOENT;
} else if (!(file->f_mode & FMODE_CREATED) && (open_flag & O_EXCL)) {
error = -EEXIST;
- } else if ((dentry->d_inode->i_mode & S_IFMT) != S_IFREG) {
- switch (dentry->d_inode->i_mode & S_IFMT) {
- case S_IFDIR:
+ } else if (!d_is_reg(dentry)) {
+ error = -EFTYPE;
+ if (d_is_dir(dentry))
error = -EISDIR;
- break;
- case S_IFLNK:
+ if (d_is_symlink(dentry))
error = -ELOOP;
- break;
- case S_IFBLK:
- case S_IFCHR:
- error = -ENODEV;
- break;
- case S_IFIFO:
- case S_IFSOCK:
- default:
- error = -EFTYPE;
- break;
- }
} else if (!(file->f_mode & FMODE_OPENED)) {
nd.path.dentry = dentry;
error = vfs_open(&nd.path, file);
--
2.50.0.107.gf914562f5916.dirty
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 7/8] vfs: change vfs_lookup_open() to use do_open(), not vfs_open()
2026-12-02 6:05 [PATCH 0/8] vfs: improvements for atomic_open and vfs_lookup_open NeilBrown
` (5 preceding siblings ...)
2026-12-02 6:05 ` [PATCH 6/8] vfs: don't return -ENODEV from vfs_lookup_open() NeilBrown
@ 2026-12-02 6:05 ` NeilBrown
2026-12-02 6:05 ` [PATCH 8/8] vfs: add MAY_SERVE_FILE and FMODE_SERVE_FILE NeilBrown
7 siblings, 0 replies; 9+ messages in thread
From: NeilBrown @ 2026-12-02 6:05 UTC (permalink / raw)
To: linux-kernel
From: NeilBrown <neil@brown.name>
The vfs_open() call misses permission checks which should happen before
an open is attempted (atomic_open does separate permission checks).
This can be addressed by using do_open() instead of vfs_open().
Much of the code in do_open() will have no effect as the relevant
LOOKUP_ flags aren't set. Truncation will be done (which nfsd is not
expected to use) along with some audit logs and security hooks. The
important benefit is getting the may_open() check.
So change vfs_lookup_open() to call do_open(), having first set
op.acc_mode to the appropriate combination of MAY_READ and MAY_WRITE to
be checked by may_open().
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: NeilBrown <neil@brown.name>
---
fs/namei.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/fs/namei.c b/fs/namei.c
index d7980391a40d..c685fbe7671c 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -4729,6 +4729,9 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
goto out;
}
+static int do_open(struct nameidata *nd,
+ struct file *file, const struct open_flags *op);
+
/**
* vfs_lookup_open - open and possibly create a regular file
* @parent: directory to contain file
@@ -4785,6 +4788,9 @@ struct file *vfs_lookup_open(struct path *parent, struct qstr *last,
op.mode = mode;
}
op.open_flag = open_flag;
+ op.acc_mode = ACC_MODE(open_flag);;
+ if (open_flag & O_TRUNC)
+ op.acc_mode |= MAY_WRITE;
dentry = lookup_open(&nd, file, &op);
if (IS_ERR(dentry))
@@ -4802,7 +4808,7 @@ struct file *vfs_lookup_open(struct path *parent, struct qstr *last,
error = -ELOOP;
} else if (!(file->f_mode & FMODE_OPENED)) {
nd.path.dentry = dentry;
- error = vfs_open(&nd.path, file);
+ error = do_open(&nd, file, &op);
}
dput(dentry);
--
2.50.0.107.gf914562f5916.dirty
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 8/8] vfs: add MAY_SERVE_FILE and FMODE_SERVE_FILE
2026-12-02 6:05 [PATCH 0/8] vfs: improvements for atomic_open and vfs_lookup_open NeilBrown
` (6 preceding siblings ...)
2026-12-02 6:05 ` [PATCH 7/8] vfs: change vfs_lookup_open() to use do_open(), not vfs_open() NeilBrown
@ 2026-12-02 6:05 ` NeilBrown
7 siblings, 0 replies; 9+ messages in thread
From: NeilBrown @ 2026-12-02 6:05 UTC (permalink / raw)
To: linux-kernel
From: NeilBrown <neil@brown.name>
Most access permissions have security implications, but some are for
convenience.
In particular:
r/w/x access for the owner have no security implication as the owner can
always change these flags.
The different between 'x' and 'r' permission has no security
implications. A user with 'r' could find a way to map the file and
execute the code, a user with 'x' could execute the file under a
debugger and extract the contents.
When serving files over the network, the client can provide the
convenience of imposing these permissions while the server cannot
in general impose them.
A server must allow a client to read the content of an execute-only file
so that it can be loaded into client memory and executed. A server must
allow a client to write to a file that was created without
write-permission - at least as long as it remains open after the O_CREAT
open which gave it that permission. In this case the creating user will
be the owner.
nfsd already manages these permission requirements in most cases.
However the need to use ->atomic_open interferes with the ability of
nfsd to allow extra permissions. To provide complete correctness we
need to instruct ->atomic_open to bypass the convenience permissions.
Also if core VFS code understands these needs we can achieve more code
sharing between nfsd and VFS.
So we introduce MAY_SERVE_FILE which instructs permission() functions,
and generic_permission() in particular, to allow read-access to
execute-only files and to allow all access to an owner.
We also add FMODE_SERVE_FILE which is passed to ->atomic_open so these
functions can handle when needed. No ->atomic_open functions are
changed to make use of it yet.
Setting these in vfs_lookup_open() will mean that nfsd gets the
permission checking that it requires when it is changed to use
vfs_lookup_open().
Signed-off-by: NeilBrown <neil@brown.name>
---
Documentation/filesystems/vfs.rst | 5 +++++
fs/namei.c | 15 +++++++++++++--
include/linux/fs.h | 6 ++++--
3 files changed, 22 insertions(+), 4 deletions(-)
diff --git a/Documentation/filesystems/vfs.rst b/Documentation/filesystems/vfs.rst
index d1d32540e85f..9d6e9e8bfdae 100644
--- a/Documentation/filesystems/vfs.rst
+++ b/Documentation/filesystems/vfs.rst
@@ -644,6 +644,11 @@ otherwise noted.
FMODE_CREATED should be set if it is possible that this operation
created the object.
+ If atomic_open() calls finish_open() then it must have performed
+ all necessary access permission checks. If FMODE_SERVE_FILE
+ is set in file->f_mode, then it should assume MAY_SERVE_FILE if
+ possible.
+
This method is only called if the last component is negative or
needs lookup. Cached positive dentries are still handled by
f_op->open().
diff --git a/fs/namei.c b/fs/namei.c
index c685fbe7671c..fded05d15c08 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -441,6 +441,11 @@ static int acl_permission_check(const struct mnt_idmap *idmap,
unsigned int mode = inode->i_mode;
vfsuid_t vfsuid;
+ if (mask & MAY_SERVE_FILE) {
+ /* 'x' permission grants 'r' permission */
+ mode |= (mode & 0111) << 2;
+ }
+
/*
* Common cheap case: everybody has the requested
* rights, and there are no ACLs to check. No need
@@ -468,7 +473,12 @@ static int acl_permission_check(const struct mnt_idmap *idmap,
if (likely(vfsuid_eq_kuid(vfsuid, current_fsuid()))) {
mask &= 7;
mode >>= 6;
- return (mask & ~mode) ? -EACCES : 0;
+ if (likely((mask & ~mode) == 0))
+ return 0;
+ if (mask & MAY_SERVE_FILE)
+ /* Owner always gets access */
+ return 0;
+ return -EACCES;
}
/* Do we have ACL's? */
@@ -4777,6 +4787,7 @@ struct file *vfs_lookup_open(struct path *parent, struct qstr *last,
file = alloc_empty_file(open_flag, current_cred());
if (IS_ERR(file))
return file;
+ file->f_mode |= FMODE_SERVE_FILE;
nd.path = *parent;
nd.last = *last;
@@ -4788,7 +4799,7 @@ struct file *vfs_lookup_open(struct path *parent, struct qstr *last,
op.mode = mode;
}
op.open_flag = open_flag;
- op.acc_mode = ACC_MODE(open_flag);;
+ op.acc_mode = ACC_MODE(open_flag) | MAY_SERVE_FILE;
if (open_flag & O_TRUNC)
op.acc_mode |= MAY_WRITE;
dentry = lookup_open(&nd, file, &op);
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 79c376d4bc57..4337a77d051f 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -99,6 +99,8 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset,
#define MAY_CHDIR 0x00000040
/* called from RCU mode, don't block */
#define MAY_NOT_BLOCK 0x00000080
+/* Passed by nfsd - owner may have any access, X access gives R */
+#define MAY_SERVE_FILE 0x00000100
/*
* flags in file.f_mode. Note that FMODE_READ and FMODE_WRITE must correspond
@@ -121,8 +123,8 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset,
#define FMODE_WRITE_RESTRICTED ((__force fmode_t)(1 << 6))
/* File supports atomic writes */
#define FMODE_CAN_ATOMIC_WRITE ((__force fmode_t)(1 << 7))
-
-/* FMODE_* bit 8 */
+/* File used to serve file to a client, so MAY_SERVE_FILE applies */
+#define FMODE_SERVE_FILE ((__force fmode_t)(1 << 8))
/* 32bit hashes as llseek() offset (for directories) */
#define FMODE_32BITHASH ((__force fmode_t)(1 << 9))
--
2.50.0.107.gf914562f5916.dirty
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-12-02 6:06 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-02 6:05 [PATCH 0/8] vfs: improvements for atomic_open and vfs_lookup_open NeilBrown
2026-12-02 6:05 ` [PATCH 1/8] vfs: revise and expand documentation for atomic_open NeilBrown
2026-12-02 6:05 ` [PATCH 2/8] vfs: only set op.mode when O_CREAT in vfs_lookup_open() NeilBrown
2026-12-02 6:05 ` [PATCH 3/8] vfs: don't call ->atomic_open with hashed negative in non-O_CREAT case NeilBrown
2026-12-02 6:05 ` [PATCH 4/8] vfs: add some allowed open flags to vfs_lookup_open() NeilBrown
2026-12-02 6:05 ` [PATCH 5/8] vfs: O_NONBLOCK|O_CREAT open shouldn't wait for directory delegation NeilBrown
2026-12-02 6:05 ` [PATCH 6/8] vfs: don't return -ENODEV from vfs_lookup_open() NeilBrown
2026-12-02 6:05 ` [PATCH 7/8] vfs: change vfs_lookup_open() to use do_open(), not vfs_open() NeilBrown
2026-12-02 6:05 ` [PATCH 8/8] vfs: add MAY_SERVE_FILE and FMODE_SERVE_FILE NeilBrown
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®