From: NeilBrown <neilb@ownmail.net>
To: Chuck Lever <chuck.lever@oracle.com>, Jeff Layton <jlayton@kernel.org>
Cc: Olga Kornievskaia <okorniev@redhat.com>,
Dai Ngo <Dai.Ngo@oracle.com>, Tom Talpey <tom@talpey.com>,
linux-nfs@vger.kernel.org
Subject: [PATCH v3 07/17] nfsd: in nfsd4_create_file() let VFS report if file was created.
Date: Mon, 13 Jul 2026 16:15:30 +1000 [thread overview]
Message-ID: <20260713062219.6399-8-neilb@ownmail.net> (raw)
In-Reply-To: <20260713062219.6399-1-neilb@ownmail.net>
From: NeilBrown <neil@brown.name>
nfsd4_create_file() currently assumes that if a lookup failed but then a
create succeeds, then the "create" operation actually created the file.
With atomic_open this may not be the case - some other actor might have
created the file between the lookup and the create.
So we move the call to nfsd4_vfs_create() earlier and set ->op_created
based on the FMODE_CREATED flag that it set. Then use "! ->op_created"
to trigger nfserr_exist handling.
The switch statement is split up into two if() statements.
First we check for the possibility of a successful exclusive
create and set ->op_create to true if appropriate.
Then we check for NFS4_CREATE_UNCHECKED to decide if a
pre-existing file means an error or success.
This allows us to combine the two fh_compose() calls to one place.
A subtle difference here is that we now must only pass O_EXCL to
dentry_create() for NFS4_CREATE_GUARDED. For the EXCLUSIVE create modes
we want a successful open even if the file already exists. We then
check the verifier after the open succeeded to see if it was exclusive.
The above requires changing dentry_create() to reliably set
FMODE_CREATED when the file was actually created. Previously it only
sets this flag when atomic_open is used.
Signed-off-by: NeilBrown <neil@brown.name>
---
fs/namei.c | 2 ++
fs/nfsd/nfs4proc.c | 69 ++++++++++++++++++++--------------------------
2 files changed, 32 insertions(+), 39 deletions(-)
diff --git a/fs/namei.c b/fs/namei.c
index 19ce43c9a6e6..9af1d5bc89fc 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -5077,6 +5077,8 @@ struct file *dentry_create(struct path *path, int flags, umode_t mode,
error = vfs_create(mnt_idmap(path->mnt), path->dentry, mode, NULL);
if (!error)
error = vfs_open(path, file);
+ if (!error)
+ file->f_mode |= FMODE_CREATED;
}
if (unlikely(error))
return ERR_PTR(error);
diff --git a/fs/nfsd/nfs4proc.c b/fs/nfsd/nfs4proc.c
index ec3e31376da4..83ad690a4948 100644
--- a/fs/nfsd/nfs4proc.c
+++ b/fs/nfsd/nfs4proc.c
@@ -210,7 +210,11 @@ nfsd4_vfs_create(struct svc_fh *fhp, struct dentry **child,
int oflags;
oflags = O_CREAT | O_LARGEFILE;
- if (nfsd4_create_is_exclusive(open->op_createmode))
+ /*
+ * For the EXCLUSIVE modes we do our own uniqueness tests
+ * so don't want O_EXCL.
+ */
+ if (open->op_createmode == NFS4_CREATE_GUARDED)
oflags |= O_EXCL;
switch (open->op_share_access & NFS4_SHARE_ACCESS_BOTH) {
@@ -362,22 +366,30 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
status = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
if (status != nfs_ok)
goto out;
- }
- if (d_really_is_positive(child)) {
- /* NFSv4 protocol requires change attributes even though
- * no change happened.
- */
- fh_fill_post_noop(fhp);
-
- status = fh_compose(resfhp, fhp->fh_export, child, fhp);
+ status = nfsd4_vfs_create(fhp, &child, open);
if (status != nfs_ok)
goto out;
+ open->op_created = open->op_filp->f_mode & FMODE_CREATED;
+ }
- switch (open->op_createmode) {
- case NFS4_CREATE_UNCHECKED:
- if (!d_is_reg(child))
- break;
+ status = fh_compose(resfhp, fhp->fh_export, child, fhp);
+ if (status != nfs_ok)
+ goto out;
+
+ if (!open->op_created &&
+ nfsd4_create_is_exclusive(open->op_createmode) &&
+ inode_get_mtime_sec(d_inode(child)) == v_mtime &&
+ inode_get_atime_sec(d_inode(child)) == v_atime &&
+ d_inode(child)->i_size == 0)
+ open->op_created = true;
+
+ if (!open->op_created) {
+ if (open->op_createmode == NFS4_CREATE_UNCHECKED) {
+ /* NFSv4 protocol requires change attributes
+ * even though no change happened.
+ */
+ fh_fill_post_noop(fhp);
/*
* In NFSv4, we don't want to truncate the file
@@ -385,41 +397,20 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
* some other reason. Furthermore, if the size is
* nonzero, we should ignore it according to spec!
*/
- open->op_truncate = (iap->ia_valid & ATTR_SIZE) &&
- !iap->ia_size;
- break;
- case NFS4_CREATE_GUARDED:
- status = nfserr_exist;
- break;
- case NFS4_CREATE_EXCLUSIVE:
- case NFS4_CREATE_EXCLUSIVE4_1:
- if (inode_get_mtime_sec(d_inode(child)) == v_mtime &&
- inode_get_atime_sec(d_inode(child)) == v_atime &&
- d_inode(child)->i_size == 0) {
- open->op_created = true;
- goto set_attr;
- }
+ open->op_truncate = (d_is_reg(child) &&
+ (iap->ia_valid & ATTR_SIZE) &&
+ !iap->ia_size);
+ } else
status = nfserr_exist;
- break;
- }
goto out;
}
-
- status = nfsd4_vfs_create(fhp, &child, open);
- if (status != nfs_ok)
- goto out;
- open->op_created = true;
+ /* file was created */
fh_fill_post_attrs(fhp);
- status = fh_compose(resfhp, fhp->fh_export, child, fhp);
- if (status != nfs_ok)
- goto out;
-
/* A newly created file already has a file size of zero. */
if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
iap->ia_valid &= ~ATTR_SIZE;
-set_attr:
status = nfsd_create_setattr(rqstp, fhp, resfhp, &attrs);
if (attrs.na_labelerr)
--
2.50.0.107.gf914562f5916.dirty
next prev parent reply other threads:[~2026-07-13 6:23 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-13 6:15 [PATCH v3 00/17] nfsd: refactor nfs4_create_file() NeilBrown
2026-07-13 6:15 ` [PATCH v3 01/17] nfsd: honour client-provided attributes for NFS4_CREATE_EXCLUSIVE4_1 NeilBrown
2026-07-13 6:15 ` [PATCH v3 02/17] nfsd: correctly handle CREATE of mounted-on files NeilBrown
2026-07-13 13:38 ` Chuck Lever
2026-07-13 21:46 ` NeilBrown
2026-07-13 6:15 ` [PATCH v3 03/17] nfsd: replace fh_fill_both_attrs() with fh_fill_post_noop() NeilBrown
2026-07-13 6:15 ` [PATCH v3 04/17] nfsd: move fh_want_write() after preamble in nfsd4_create_file() NeilBrown
2026-07-13 6:15 ` [PATCH v3 05/17] nfsd: move more nfs-specific code into preamble of nfsd4_create_file() NeilBrown
2026-07-13 6:15 ` [PATCH v3 06/17] nfsd: remove subtlety from nfsd4_create_file() NeilBrown
2026-07-13 6:15 ` NeilBrown [this message]
2026-07-13 6:15 ` [PATCH v3 08/17] nfsd: nfsd4_create_file(): Move NFSD_MAY_CREATE check earlier NeilBrown
2026-07-13 6:15 ` [PATCH v3 09/17] nfsd: fh_want_write) failure need not be immediately fatal for nfsd4_create_file() NeilBrown
2026-07-13 6:15 ` [PATCH v3 10/17] nfsd: (almost) always open file in nfsd4_create_file() NeilBrown
2026-07-13 6:15 ` [PATCH v3 11/17] nfsd: reduce range of directory lock " NeilBrown
2026-07-13 6:15 ` [PATCH v3 12/17] nfsd: open-code nfsd4_vfs_create() into nfsd4_create_file() NeilBrown
2026-07-13 6:15 ` [PATCH v3 13/17] nfsd: move some code out of the d_really_is_negative() branch in nfsd4_create_file() NeilBrown
2026-07-13 6:15 ` [PATCH v3 14/17] nfsd: reduce want-write range " NeilBrown
2026-07-16 12:29 ` Jeff Layton
2026-07-13 6:15 ` [PATCH v3 15/17] nfsd: move v0 checking out of nfsd_check_obj_isreg() NeilBrown
2026-07-16 12:31 ` Jeff Layton
2026-07-13 6:15 ` [PATCH v3 16/17] nfsd: separate out VFS-specific code from nfsd4_create_file() NeilBrown
2026-07-13 6:15 ` [PATCH v3 17/17] nfsd: use do_lookup_open() for non-creating open requests too NeilBrown
2026-07-16 13:29 ` [PATCH v3 00/17] nfsd: refactor nfs4_create_file() Jeff Layton
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260713062219.6399-8-neilb@ownmail.net \
--to=neilb@ownmail.net \
--cc=Dai.Ngo@oracle.com \
--cc=chuck.lever@oracle.com \
--cc=jlayton@kernel.org \
--cc=linux-nfs@vger.kernel.org \
--cc=neil@brown.name \
--cc=okorniev@redhat.com \
--cc=tom@talpey.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox