Skip to contentSkip to conversation

nfsd:refactor nfsd4_create_file()

PATCH In review NeilBrown 2 participants 12 messages Jul 2

Subsystems (3)
lore
Share & tools
lore

NeilBrown

author
#0 lore raw

cover nfsd:refactor nfsd4_create_file()

nfs4_create_file() duplicates knowledge about opening a file which exists in the VFS, mostly in lookup_open(). It does use dentry_create() which shares some code, but there is more code that could be shared.

The nfsd code doesn't get some details quite right, particularly patch 05 shows this.

I hope to introduce a new VFS interface which encapulates more of what nfsd needs and shares more code with lookup_open(). I particularly want this as it will simplify some changes to locking rules that I am working on.

This series re-arranges the nfsd code to get it ready for switching to the new interface. Once that interface lands we can then switch over fairly easily. Hopefully the series helps clarity even before that happens.

The series is against nfsd-testing.

Thanks,
NeilBrown.

[PATCH 01/10] nfsd: replace fh_fill_both_attrs() with [PATCH 02/10] nfsd: move fh_want_write() after preamble in [PATCH 03/10] nfsd: move more nfs-specific code into preamble of [PATCH 04/10] nfsd: remove subtlety from nfsd4_create_file() [PATCH 05/10] nfsd: in nfsd4_create_file() let VFS report if file was [PATCH 06/10] nfsd: nfsd4_create_file(): remove NFSD_MAY_CREATE [PATCH 07/10] nfsd: reduce range of directory lock in [PATCH 08/10] nfsd: open-code nfsd4_vfs_create() into [PATCH 09/10] nfsd: move some code out of the d_really_is_negative() [PATCH 10/10] nfsd: reduce want-write range in nfsd4_create_file(

author
#1 lore raw

PATCH 1/10 nfsd: replace fh_fill_both_attrs() with fh_fill_post_noop()

From: NeilBrown <[email protected]>

fh_fill_both_attrs() is only needed for open/create and is used in the case when the target already existed so no creating happens.

As part of refactoring this code it is changed to call fh_fill_pre_attrs() once early on (so errors only need to be caught in one place) and then to use a new fh_fill_post_noop() when it is determined that no creation happened.

fh_fill_pre_attrs() now stores the attrs (which it had to get all of anyway)_ in ->fh_post_attr. fh_fill_post_noop() simply marks them as valid. fh_fill_post_attrs() replaces them.

---
 fs/nfsd/nfs4proc.c | 20 +++++++++-----------
 fs/nfsd/nfsfh.c    | 40 +++++++++++-----------------------------
 fs/nfsd/nfsfh.h    | 13 ++++++++++++-
 3 files changed, 32 insertions(+), 41 deletions(-)
fs/nfsd/nfs4proc.c +9 -11
index 669896be08b6..06de0cfd8e90 100644
@@ -327,9 +327,7 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 		/* NFSv4 protocol requires change attributes even though
 		 * no change happened.
 		 */
-		status = fh_fill_both_attrs(fhp);
-		if (status != nfs_ok)
-			goto out;
+		fh_fill_post_noop(fhp);
 
 		status = fh_compose(resfhp, fhp->fh_export, child, fhp);
 		if (status != nfs_ok)
@@ -376,9 +374,6 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 	if (!IS_POSIXACL(inode))
 		iap->ia_mode &= ~current_umask();
 
-	status = fh_fill_pre_attrs(fhp);
-	if (status != nfs_ok)
-		goto out;
 	status = nfsd4_vfs_create(fhp, &child, open);
 	if (status != nfs_ok)
 		goto out;
@@ -464,6 +459,9 @@ do_open_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, stru
 	fh_init(*resfh, NFS4_FHSIZE);
 	open->op_truncate = false;
 
+	status = fh_fill_pre_attrs(current_fh);
+	if (status)
+		goto out;
 	if (open->op_create) {
 		/* FIXME: check session persistence and pnfs flags.
 		 * The nfsv4.1 spec requires the following semantics:
@@ -495,11 +493,11 @@ do_open_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, stru
 	} else {
 		status = nfsd_lookup(rqstp, current_fh,
 				     open->op_fname, open->op_fnamelen, *resfh);
-		if (status == nfs_ok)
-			/* NFSv4 protocol requires change attributes even though
-			 * no change happened.
-			 */
-			status = fh_fill_both_attrs(current_fh);
+		/*
+		 * NFSv4 protocol requires change attributes even though
+		 * no change happened.
+		 */
+		fh_fill_post_noop(current_fh);
 	}
 	if (status)
 		goto out;
fs/nfsd/nfsfh.c +11 -29
index 8b1a95e1d058..58de7b7c4238 100644
@@ -784,26 +784,31 @@ __be32 fh_getattr(const struct svc_fh *fhp, struct kstat *stat)
  * fh_fill_pre_attrs - Fill in pre-op attributes
  * @fhp: file handle to be updated
  *
+ * Post-op attrs are filled and pre-op attrs are copied
+ * from there.  The post-op attrs can later be replaced by
+ * fh_fill_post_attrs() or activated by fh_fill_post_noop().
+ *
+ * Returns: error from vfs_getattr() which must be checked.
  */
 __be32 __must_check fh_fill_pre_attrs(struct svc_fh *fhp)
 {
 	bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
-	struct kstat stat;
 	__be32 err;
 
 	if (fhp->fh_no_wcc || fhp->fh_pre_saved)
 		return nfs_ok;
 
-	err = fh_getattr(fhp, &stat);
+	err = fh_getattr(fhp, &fhp->fh_post_attr);
 	if (err)
 		return err;
 
 	if (v4)
-		fhp->fh_pre_change = nfsd4_change_attribute(&stat);
+		fhp->fh_pre_change = fhp->fh_post_change =
+			nfsd4_change_attribute(&fhp->fh_post_attr);
 
-	fhp->fh_pre_mtime = stat.mtime;
-	fhp->fh_pre_ctime = stat.ctime;
-	fhp->fh_pre_size  = stat.size;
+	fhp->fh_pre_mtime = fhp->fh_post_attr.mtime;
+	fhp->fh_pre_ctime = fhp->fh_post_attr.ctime;
+	fhp->fh_pre_size  = fhp->fh_post_attr.size;
 	fhp->fh_pre_saved = true;
 	return nfs_ok;
 }
@@ -835,29 +840,6 @@ __be32 fh_fill_post_attrs(struct svc_fh *fhp)
 	return nfs_ok;
 }
 
-/**
- * fh_fill_both_attrs - Fill pre-op and post-op attributes
- * @fhp: file handle to be updated
- *
- * This is used when the directory wasn't changed, but wcc attributes
- * are needed anyway.
- */
-__be32 __must_check fh_fill_both_attrs(struct svc_fh *fhp)
-{
-	__be32 err;
-
-	err = fh_fill_post_attrs(fhp);
-	if (err)
-		return err;
-
-	fhp->fh_pre_change = fhp->fh_post_change;
-	fhp->fh_pre_mtime = fhp->fh_post_attr.mtime;
-	fhp->fh_pre_ctime = fhp->fh_post_attr.ctime;
-	fhp->fh_pre_size = fhp->fh_post_attr.size;
-	fhp->fh_pre_saved = true;
-	return nfs_ok;
-}
-
 /*
  * Release a file handle.
  */
fs/nfsd/nfsfh.h +12 -1
index cdeb5eea65a8..e8b271a92024 100644
@@ -338,5 +338,16 @@ static inline void fh_clear_pre_post_attrs(struct svc_fh *fhp)
 u64 nfsd4_change_attribute(const struct kstat *stat);
 __be32 __must_check fh_fill_pre_attrs(struct svc_fh *fhp);
 __be32 fh_fill_post_attrs(struct svc_fh *fhp);
-__be32 __must_check fh_fill_both_attrs(struct svc_fh *fhp);
+
+/**
+ * fh_fill_post_noop - Copy pre attrs to post attrs
+ * @fhp: file handle to be updated
+ *
+ * This is used when the directory wasn't changed, but wcc attributes
+ * are needed anyway.
+ */
+static inline void fh_fill_post_noop(struct svc_fh *fhp)
+{
+	fhp->fh_post_saved = true;
+}
 #endif /* _LINUX_NFSD_NFSFH_H */
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
author
#2 lore raw

PATCH 2/10 nfsd: move fh_want_write() after preamble in nfsd4_create_file()

In reply to NeilBrown

From: NeilBrown <[email protected]>

As part of separating the nfsd-specific code from the VFS interaction code in nfsd4_create_file(), move fh_want_write() to just before we need it.

Consequently errors in the "if" statement that this code is moved over can now be returned immediately rather than needing to "goto out".

Also restructure that "if" statement to only test is_create_with_attrs() once.

---
 fs/nfsd/nfs4proc.c | 31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)
fs/nfsd/nfs4proc.c +17 -14
index 06de0cfd8e90..9ba1e654f1ec 100644
@@ -270,22 +270,18 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 	parent = fhp->fh_dentry;
 	inode = d_inode(parent);
 
-	host_err = fh_want_write(fhp);
-	if (host_err)
-		return nfserrno(host_err);
-
-	if (open->op_acl) {
+	if (!is_create_with_attrs(open)) {
+		/* No attrs to check */
+	} else if (open->op_acl) {
 		if (open->op_dpacl || open->op_pacl) {
-			status = nfserr_inval;
-			goto out;
+			/* Cannot specify both NFSv4 and Posix ACLs */
+			return nfserr_inval;
 		}
-		if (is_create_with_attrs(open)) {
-			status = nfsd4_acl_to_attr(NF4REG, open->op_acl,
+		status = nfsd4_acl_to_attr(NF4REG, open->op_acl,
 						   &attrs);
-			if (status)
-				goto out;
-		}
-	} else if (is_create_with_attrs(open)) {
+		if (status)
+			return status;
+	} else {
 		/* The dpacl and pacl will get released by nfsd_attrs_free(). */
 		attrs.na_dpacl = open->op_dpacl;
 		attrs.na_pacl = open->op_pacl;
@@ -293,6 +289,12 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 		open->op_pacl = NULL;
 	}
 
+	host_err = fh_want_write(fhp);
+	if (host_err) {
+		status = nfserrno(host_err);
+		goto out_free;
+	}
+
 	child = start_creating(&nop_mnt_idmap, parent,
 			       &QSTR_LEN(open->op_fname, open->op_fnamelen));
 	if (IS_ERR(child)) {
@@ -409,8 +411,9 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 		open->op_bmval[2] &= ~FATTR4_WORD2_POSIX_ACCESS_ACL;
 out:
 	end_creating(child);
-	nfsd_attrs_free(&attrs);
 	fh_drop_write(fhp);
+out_free:
+	nfsd_attrs_free(&attrs);
 	return status;
 }
 
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
author
#3 lore raw

PATCH 3/10 nfsd: move more nfs-specific code into preamble of nfsd4_create_file()

In reply to NeilBrown

From: NeilBrown <[email protected]>

Do NFS-specific prep before interacting with the VFS.

---
 fs/nfsd/nfs4proc.c | 55 +++++++++++++++++++++++-----------------------
 1 file changed, 27 insertions(+), 28 deletions(-)
fs/nfsd/nfs4proc.c +27 -28
index 9ba1e654f1ec..c63c8fe64079 100644
@@ -270,6 +270,9 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 	parent = fhp->fh_dentry;
 	inode = d_inode(parent);
 
+	if (!IS_POSIXACL(inode))
+		iap->ia_mode &= ~current_umask();
+
 	if (!is_create_with_attrs(open)) {
 		/* No attrs to check */
 	} else if (open->op_acl) {
@@ -289,6 +292,30 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 		open->op_pacl = NULL;
 	}
 
+	v_mtime = 0;
+	v_atime = 0;
+	if (nfsd4_create_is_exclusive(open->op_createmode)) {
+		u32 *verifier = (u32 *)open->op_verf.data;
+
+		/*
+		 * Solaris 7 gets confused (bugid 4218508) if these have
+		 * the high bit set, as do xfs filesystems without the
+		 * "bigtime" feature. So just clear the high bits. If this
+		 * is ever changed to use different attrs for storing the
+		 * verifier, then do_open_lookup() will also need to be
+		 * fixed accordingly.
+		 */
+		v_mtime = verifier[0] & 0x7fffffff;
+		v_atime = verifier[1] & 0x7fffffff;
+
+		iap->ia_valid = ATTR_MTIME | ATTR_ATIME |
+				ATTR_MTIME_SET|ATTR_ATIME_SET;
+		iap->ia_mtime.tv_sec = v_mtime;
+		iap->ia_atime.tv_sec = v_atime;
+		iap->ia_mtime.tv_nsec = 0;
+		iap->ia_atime.tv_nsec = 0;
+	}
+
 	host_err = fh_want_write(fhp);
 	if (host_err) {
 		status = nfserrno(host_err);
@@ -308,23 +335,6 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 			goto out;
 	}
 
-	v_mtime = 0;
-	v_atime = 0;
-	if (nfsd4_create_is_exclusive(open->op_createmode)) {
-		u32 *verifier = (u32 *)open->op_verf.data;
-
-		/*
-		 * Solaris 7 gets confused (bugid 4218508) if these have
-		 * the high bit set, as do xfs filesystems without the
-		 * "bigtime" feature. So just clear the high bits. If this
-		 * is ever changed to use different attrs for storing the
-		 * verifier, then do_open_lookup() will also need to be
-		 * fixed accordingly.
-		 */
-		v_mtime = verifier[0] & 0x7fffffff;
-		v_atime = verifier[1] & 0x7fffffff;
-	}
-
 	if (d_really_is_positive(child)) {
 		/* NFSv4 protocol requires change attributes even though
 		 * no change happened.
@@ -373,9 +383,6 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 		goto out;
 	}
 
-	if (!IS_POSIXACL(inode))
-		iap->ia_mode &= ~current_umask();
-
 	status = nfsd4_vfs_create(fhp, &child, open);
 	if (status != nfs_ok)
 		goto out;
@@ -389,14 +396,6 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 	/* 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;
-	if (nfsd4_create_is_exclusive(open->op_createmode)) {
-		iap->ia_valid = ATTR_MTIME | ATTR_ATIME |
-				ATTR_MTIME_SET|ATTR_ATIME_SET;
-		iap->ia_mtime.tv_sec = v_mtime;
-		iap->ia_atime.tv_sec = v_atime;
-		iap->ia_mtime.tv_nsec = 0;
-		iap->ia_atime.tv_nsec = 0;
-	}
 
 set_attr:
 	status = nfsd_create_setattr(rqstp, fhp, resfhp, &attrs);
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
author
#4 lore raw

PATCH 4/10 nfsd: remove subtlety from nfsd4_create_file()

In reply to NeilBrown

From: NeilBrown <[email protected]>

nfsd4_create_file() has a switch with cases for NFS4_CREATE_EXCLUSIVE and NFS4_CREATE_EXCLUSIVE4_1 which are identical except for one line which is marked "subtle" in both cases.

The difference boils down to a "goto". For the EXCLUSIVE case the target is "out:" which is after a setattr call. For EXCLUSIVE4_1 the target is "set_attr:" which is the start of that setattr call.

We already have a function is_create_with_attrs() which determines if the setattr is needed, and differentiates between these two cases. So if we guard the setattr with "is_create_with_attrs()", then we can "goto setattr" for both cases and so unify the two cases in the switch. This, I think, makes the code clearer and less subtle.

---
 fs/nfsd/nfs4proc.c | 33 ++++++++++++++-------------------
 1 file changed, 14 insertions(+), 19 deletions(-)
fs/nfsd/nfs4proc.c +14 -19
index c63c8fe64079..5b7f0314776f 100644
@@ -363,22 +363,15 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 			status = nfserr_exist;
 			break;
 		case NFS4_CREATE_EXCLUSIVE:
-			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;
-				break;		/* subtle */
-			}
-			status = nfserr_exist;
-			break;
 		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;	/* subtle */
+				goto set_attr;
 			}
 			status = nfserr_exist;
+			break;
 		}
 		goto out;
 	}
@@ -398,16 +391,18 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 		iap->ia_valid &= ~ATTR_SIZE;
 
 set_attr:
-	status = nfsd_create_setattr(rqstp, fhp, resfhp, &attrs);
-
-	if (attrs.na_labelerr)
-		open->op_bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
-	if (attrs.na_paclerr || attrs.na_dpaclerr)
-		open->op_bmval[0] &= ~FATTR4_WORD0_ACL;
-	if (attrs.na_dpaclerr)
-		open->op_bmval[2] &= ~FATTR4_WORD2_POSIX_DEFAULT_ACL;
-	if (attrs.na_paclerr)
-		open->op_bmval[2] &= ~FATTR4_WORD2_POSIX_ACCESS_ACL;
+	if (is_create_with_attrs(open)) {
+		status = nfsd_create_setattr(rqstp, fhp, resfhp, &attrs);
+
+		if (attrs.na_labelerr)
+			open->op_bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
+		if (attrs.na_paclerr || attrs.na_dpaclerr)
+			open->op_bmval[0] &= ~FATTR4_WORD0_ACL;
+		if (attrs.na_dpaclerr)
+			open->op_bmval[2] &= ~FATTR4_WORD2_POSIX_DEFAULT_ACL;
+		if (attrs.na_paclerr)
+			open->op_bmval[2] &= ~FATTR4_WORD2_POSIX_ACCESS_ACL;
+	}
 out:
 	end_creating(child);
 	fh_drop_write(fhp);
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
author
#5 lore raw

PATCH 5/10 nfsd: in nfsd4_create_file() let VFS report if file was created.

In reply to NeilBrown

From: NeilBrown <[email protected]>

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 that. 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.

Also move the clearing of ATTR_SIZE for a newly created file down to a point where we know we have actually created the file.

With this rearrangement we now repeat the setattr when an exclusive create is repeated. This should be both rare and harmless, and it simplifies the code.

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.

---
 fs/namei.c         |  2 ++
 fs/nfsd/nfs4proc.c | 63 ++++++++++++++++++----------------------------
 2 files changed, 27 insertions(+), 38 deletions(-)
fs/namei.c +2 -0
index 5cc9f0f466b8..e0a62198fc60 100644
@@ -5073,6 +5073,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);
fs/nfsd/nfs4proc.c +25 -38

88 lines not on this page · raw message

Signature · 1 line
2.50.0.107.gf914562f5916.dirty
author
#6 lore raw

PATCH 6/10 nfsd: nfsd4_create_file(): remove NFSD_MAY_CREATE check.

In reply to NeilBrown

From: NeilBrown <[email protected]>

This check is redundant as dentry_create() called by nfsd4_vfs_create() performs the same check.

---
 fs/nfsd/nfs4proc.c | 4 ----
 1 file changed, 4 deletions(-)
fs/nfsd/nfs4proc.c +0 -4
index cbb549ca6567..4f730ac83cf8 100644
@@ -330,10 +330,6 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 	}
 
 	if (d_really_is_negative(child)) {
-		status = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
-		if (status != nfs_ok)
-			goto out;
-
 		status = nfsd4_vfs_create(fhp, &child, open);
 		if (status != nfs_ok)
 			goto out;
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
author
#7 lore raw

PATCH 7/10 nfsd: reduce range of directory lock in nfsd4_create_file()

In reply to NeilBrown

From: NeilBrown <[email protected]>

We only need to hold the lock taken by start_creating() until the create has been attempted. Holding for longer can serve no purpose.

The lock is currently held across the setattr call. This might be the intent but it serves no purpose. Holding the lock prevents the name from being removed or renamed, but it doesn't prevent a GETATTR or a racing SETATTR or an OPEN.

---
 fs/nfsd/nfs4proc.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/nfsd/nfs4proc.c +3 -3
index 4f730ac83cf8..9dfdeb6dc379 100644
@@ -331,10 +331,11 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 
 	if (d_really_is_negative(child)) {
 		status = nfsd4_vfs_create(fhp, &child, open);
-		if (status != nfs_ok)
-			goto out;
 		open->op_created = open->op_filp->f_mode & FMODE_CREATED;
 	}
+	end_creating(child);
+	if (status != nfs_ok)
+		goto out;
 
 	status = fh_compose(resfhp, fhp->fh_export, child, fhp);
 	if (status != nfs_ok)
@@ -387,7 +388,6 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 			open->op_bmval[2] &= ~FATTR4_WORD2_POSIX_ACCESS_ACL;
 	}
 out:
-	end_creating(child);
 	fh_drop_write(fhp);
 out_free:
 	nfsd_attrs_free(&attrs);
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
author
#8 lore raw

PATCH 8/10 nfsd: open-code nfsd4_vfs_create() into nfsd4_create_file()

In reply to NeilBrown

From: NeilBrown <[email protected]>

Having this sub function separate doesn't really add clarity, and merging allows for some refactoring and ultimately using a different VFS interface.

---
 fs/nfsd/nfs4proc.c | 70 +++++++++++++++++++++-------------------------
 1 file changed, 32 insertions(+), 38 deletions(-)
fs/nfsd/nfs4proc.c +32 -38
index 9dfdeb6dc379..6e94f88d79da 100644
@@ -201,41 +201,6 @@ static inline bool nfsd4_create_is_exclusive(int createmode)
 		createmode == NFS4_CREATE_EXCLUSIVE4_1;
 }
 
-static __be32
-nfsd4_vfs_create(struct svc_fh *fhp, struct dentry **child,
-		 struct nfsd4_open *open)
-{
-	struct file *filp;
-	struct path path;
-	int oflags;
-
-	oflags = O_CREAT | O_LARGEFILE;
-	if (nfsd4_create_is_exclusive(open->op_createmode))
-		oflags |= O_EXCL;
-
-	switch (open->op_share_access & NFS4_SHARE_ACCESS_BOTH) {
-	case NFS4_SHARE_ACCESS_WRITE:
-		oflags |= O_WRONLY;
-		break;
-	case NFS4_SHARE_ACCESS_BOTH:
-		oflags |= O_RDWR;
-		break;
-	default:
-		oflags |= O_RDONLY;
-	}
-
-	path.mnt = fhp->fh_export->ex_path.mnt;
-	path.dentry = *child;
-	filp = dentry_create(&path, oflags, open->op_iattr.ia_mode,
-			     current_cred());
-	*child = path.dentry;
-
-	if (IS_ERR(filp))
-		return nfserrno(PTR_ERR(filp));
-
-	open->op_filp = filp;
-	return nfs_ok;
-}
 
 /*
  * Implement NFSv4's unchecked, guarded, and exclusive create
@@ -330,12 +295,41 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 	}
 
 	if (d_really_is_negative(child)) {
-		status = nfsd4_vfs_create(fhp, &child, open);
+		struct file *filp;
+		struct path path;
+		int oflags;
+
+		oflags = O_CREAT | O_LARGEFILE;
+		if (nfsd4_create_is_exclusive(open->op_createmode))
+			oflags |= O_EXCL;
+
+		switch (open->op_share_access & NFS4_SHARE_ACCESS_BOTH) {
+		case NFS4_SHARE_ACCESS_WRITE:
+			oflags |= O_WRONLY;
+			break;
+		case NFS4_SHARE_ACCESS_BOTH:
+			oflags |= O_RDWR;
+			break;
+		default:
+			oflags |= O_RDONLY;
+		}
+
+		path.mnt = fhp->fh_export->ex_path.mnt;
+		path.dentry = child;
+		filp = dentry_create(&path, oflags, open->op_iattr.ia_mode,
+				     current_cred());
+		child = path.dentry;
+
+		if (IS_ERR(filp)) {
+			end_creating(child);
+			status = nfserrno(PTR_ERR(filp));
+			goto out;
+		}
+
+		open->op_filp = filp;
 		open->op_created = open->op_filp->f_mode & FMODE_CREATED;
 	}
 	end_creating(child);
-	if (status != nfs_ok)
-		goto out;
 
 	status = fh_compose(resfhp, fhp->fh_export, child, fhp);
 	if (status != nfs_ok)
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
author
#9 lore raw

PATCH 9/10 nfsd: move some code out of the d_really_is_negative() branch in nfsd4_create_file()

In reply to NeilBrown

From: NeilBrown <[email protected]>

The benefit of this code movement isn't immediately obvious, but it will make it easier to switch to using vfs_lookup_open().

---
 fs/nfsd/nfs4proc.c | 49 ++++++++++++++++++++++------------------------
 1 file changed, 23 insertions(+), 26 deletions(-)
fs/nfsd/nfs4proc.c +23 -26
index 6e94f88d79da..961e0c26e9a2 100644
@@ -218,7 +218,11 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 		.na_iattr	= iap,
 		.na_seclabel	= &open->op_label,
 	};
+	int oflags = O_CREAT | O_LARGEFILE;
 	struct dentry *parent, *child = ERR_PTR(-EINVAL);
+	struct path path = {
+		.mnt = fhp->fh_export->ex_path.mnt,
+	};
 	__u32 v_mtime, v_atime;
 	struct inode *inode;
 	__be32 status;
@@ -279,6 +283,19 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 		iap->ia_atime.tv_sec = v_atime;
 		iap->ia_mtime.tv_nsec = 0;
 		iap->ia_atime.tv_nsec = 0;
+
+		oflags |= O_EXCL;
+	}
+
+	switch (open->op_share_access & NFS4_SHARE_ACCESS_BOTH) {
+	case NFS4_SHARE_ACCESS_WRITE:
+		oflags |= O_WRONLY;
+		break;
+	case NFS4_SHARE_ACCESS_BOTH:
+		oflags |= O_RDWR;
+		break;
+	default:
+		oflags |= O_RDONLY;
 	}
 
 	host_err = fh_want_write(fhp);
@@ -293,40 +310,20 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 		status = nfserrno(PTR_ERR(child));
 		goto out;
 	}
+	path.dentry = child;
 
 	if (d_really_is_negative(child)) {
-		struct file *filp;
-		struct path path;
-		int oflags;
-
-		oflags = O_CREAT | O_LARGEFILE;
-		if (nfsd4_create_is_exclusive(open->op_createmode))
-			oflags |= O_EXCL;
-
-		switch (open->op_share_access & NFS4_SHARE_ACCESS_BOTH) {
-		case NFS4_SHARE_ACCESS_WRITE:
-			oflags |= O_WRONLY;
-			break;
-		case NFS4_SHARE_ACCESS_BOTH:
-			oflags |= O_RDWR;
-			break;
-		default:
-			oflags |= O_RDONLY;
-		}
-
-		path.mnt = fhp->fh_export->ex_path.mnt;
-		path.dentry = child;
-		filp = dentry_create(&path, oflags, open->op_iattr.ia_mode,
-				     current_cred());
+		open->op_filp = dentry_create(&path, oflags, open->op_iattr.ia_mode,
+					      current_cred());
 		child = path.dentry;
 
-		if (IS_ERR(filp)) {
+		if (IS_ERR(open->op_filp)) {
 			end_creating(child);
-			status = nfserrno(PTR_ERR(filp));
+			status = nfserrno(PTR_ERR(open->op_filp));
+			open->op_filp = NULL;
 			goto out;
 		}
 
-		open->op_filp = filp;
 		open->op_created = open->op_filp->f_mode & FMODE_CREATED;
 	}
 	end_creating(child);
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
author
#10 lore raw

PATCH 10/10 nfsd: reduce want-write range in nfsd4_create_file(

In reply to NeilBrown

From: NeilBrown <[email protected]>

nfsd4_create_file() needs write access to the mount for two purposes:

1/ to create/open the file.
2/ to set attributes on the newly created file.

Normally a file being created would be open for write, and once we have an active open we have the write access needed for a setattr. However if a file were created but opened read-only then the setattr wouldn't necessarily have write access to the mount.

Currently this is all handled by holding the write access across the open and the setattr. A subsequent patch will necessarily change how write access is gained for the open. So we reduce the range for the first want_write, and add another one only if setattr is needed on a read-only open.

---
 fs/nfsd/nfs4proc.c | 34 +++++++++++++++++++++++++++-------
 1 file changed, 27 insertions(+), 7 deletions(-)
fs/nfsd/nfs4proc.c +27 -7
index 961e0c26e9a2..08e1213de743 100644
@@ -308,7 +308,7 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 			       &QSTR_LEN(open->op_fname, open->op_fnamelen));
 	if (IS_ERR(child)) {
 		status = nfserrno(PTR_ERR(child));
-		goto out;
+		goto out_write;
 	}
 	path.dentry = child;
 
@@ -321,16 +321,17 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 			end_creating(child);
 			status = nfserrno(PTR_ERR(open->op_filp));
 			open->op_filp = NULL;
-			goto out;
+			goto out_write;
 		}
 
 		open->op_created = open->op_filp->f_mode & FMODE_CREATED;
 	}
 	end_creating(child);
+	fh_drop_write(fhp);
 
 	status = fh_compose(resfhp, fhp->fh_export, child, fhp);
 	if (status != nfs_ok)
-		goto out;
+		goto out_free;
 
 	if (!open->op_created &&
 	    nfsd4_create_is_exclusive(open->op_createmode) &&
@@ -357,7 +358,7 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 					     !iap->ia_size);
 		} else
 			status = nfserr_exist;
-		goto out;
+		goto out_free;
 	}
 	/* file was created */
 	fh_fill_post_attrs(fhp);
@@ -367,7 +368,24 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 		iap->ia_valid &= ~ATTR_SIZE;
 
 	if (is_create_with_attrs(open)) {
-		status = nfsd_create_setattr(rqstp, fhp, resfhp, &attrs);
+		if (((oflags & O_ACCMODE) == O_RDONLY)) {
+				/*
+				 * We will need write access to set the attrs,
+				 * but a successful open won't have provided
+				 * that.
+				 */
+				int host_err = fh_want_write(fhp);
+				if (host_err) {
+					status = nfserrno(host_err);
+				} else {
+					status = nfsd_create_setattr(rqstp, fhp,
+								     resfhp, &attrs);
+					fh_drop_write(fhp);
+				}
+			} else {
+				status = nfsd_create_setattr(rqstp, fhp,
+							     resfhp, &attrs);
+			}
 
 		if (attrs.na_labelerr)
 			open->op_bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
@@ -378,11 +396,13 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
 		if (attrs.na_paclerr)
 			open->op_bmval[2] &= ~FATTR4_WORD2_POSIX_ACCESS_ACL;
 	}
-out:
-	fh_drop_write(fhp);
 out_free:
 	nfsd_attrs_free(&attrs);
 	return status;
+
+out_write:
+	fh_drop_write(fhp);
+	goto out_free;
 }
 
 /**
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
maintainer
#11 lore raw
In reply to NeilBrown
Quoted 12 lines · NeilBrown

nfs4_create_file() duplicates knowledge about opening a file which exists in the VFS, mostly in lookup_open(). It does use dentry_create() which shares some code, but there is more code that could be shared.

The nfsd code doesn't get some details quite right, particularly patch 05 shows this.

I hope to introduce a new VFS interface which encapulates more of what nfsd needs and shares more code with lookup_open(). I particularly want this as it will simplify some changes to locking rules that I am working on.

Quoted 4 lines

This series re-arranges the nfsd code to get it ready for switching to the new interface. Once that interface lands we can then switch over fairly easily. Hopefully the series helps clarity even before that happens.

A couple of high-level remarks (and full disclosure, I haven't looked closely at these yet):

- Sashiko found something remarkable in 9 of the 10 patches in this
series: https://sashiko.dev/#/patchset/20260702014000.3397240-1-neilb%40ownmail.net

Which I commend to you for study ;-)

- We'll need to decide how to merge these given the other related series you have in flight that targets VFS code that NFSD consumes. Are these two series truly independent?

Signature · 1 line
Chuck Lever