]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
NFSD: reduce locking in nfsd_lookup()
authorNeilBrown <neilb@suse.de>
Tue, 26 Jul 2022 06:45:30 +0000 (16:45 +1000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 10 Apr 2024 14:19:15 +0000 (16:19 +0200)
[ Upstream commit 19d008b46941b8c668402170522e0f7a9258409c ]

nfsd_lookup() takes an exclusive lock on the parent inode, but no
callers want the lock and it may not be needed at all if the
result is in the dcache.

Change nfsd_lookup_dentry() to not take the lock, and call
lookup_one_len_locked() which takes lock only if needed.

nfsd4_open() currently expects the lock to still be held, but that isn't
necessary as nfsd_validate_delegated_dentry() provides required
guarantees without the lock.

NOTE: NFSv4 requires directory changeinfo for OPEN even when a create
  wasn't requested and no change happened.  Now that nfsd_lookup()
  doesn't use fh_lock(), we need to explicitly fill the attributes
  when no create happens.  A new fh_fill_both_attrs() is provided
  for that task.

Signed-off-by: NeilBrown <neilb@suse.de>
Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
fs/nfsd/nfs4proc.c
fs/nfsd/nfs4state.c
fs/nfsd/nfsfh.c
fs/nfsd/nfsfh.h
fs/nfsd/vfs.c

index 26cd2479e30cf16b84eb5e031665fdb1c507135a..b6df56fb6755de853b4f198f4bec4b641c9fbab1 100644 (file)
@@ -302,6 +302,11 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
        if (d_really_is_positive(child)) {
                status = nfs_ok;
 
+               /* NFSv4 protocol requires change attributes even though
+                * no change happened.
+                */
+               fh_fill_both_attrs(fhp);
+
                switch (open->op_createmode) {
                case NFS4_CREATE_UNCHECKED:
                        if (!d_is_reg(child))
@@ -417,15 +422,15 @@ do_open_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, stru
                if (nfsd4_create_is_exclusive(open->op_createmode) && status == 0)
                        open->op_bmval[1] |= (FATTR4_WORD1_TIME_ACCESS |
                                                FATTR4_WORD1_TIME_MODIFY);
-       } else
-               /*
-                * Note this may exit with the parent still locked.
-                * We will hold the lock until nfsd4_open's final
-                * lookup, to prevent renames or unlinks until we've had
-                * a chance to an acquire a delegation if appropriate.
-                */
+       } else {
                status = nfsd_lookup(rqstp, current_fh,
                                     open->op_fname, open->op_fnamelen, *resfh);
+               if (!status)
+                       /* NFSv4 protocol requires change attributes even though
+                        * no change happened.
+                        */
+                       fh_fill_both_attrs(current_fh);
+       }
        if (status)
                goto out;
        status = nfsd_check_obj_isreg(*resfh);
@@ -1043,7 +1048,6 @@ nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
                                    &exp, &dentry);
        if (err)
                return err;
-       fh_unlock(&cstate->current_fh);
        if (d_really_is_negative(dentry)) {
                exp_put(exp);
                err = nfserr_noent;
index a299aeaa0de070b17d79e7bbbab83ad789c3f988..f66fb397148935c0615cd50d94110bffce26bbf4 100644 (file)
@@ -5321,9 +5321,6 @@ nfsd4_verify_deleg_dentry(struct nfsd4_open *open, struct nfs4_file *fp,
        struct dentry *child;
        __be32 err;
 
-       /* parent may already be locked, and it may get unlocked by
-        * this call, but that is safe.
-        */
        err = nfsd_lookup_dentry(open->op_rqstp, parent,
                                 open->op_fname, open->op_fnamelen,
                                 &exp, &child);
index 5e2ed4b2a925c233fadd8d782a6752482b1d1fee..cd2946a88d7275a8d3fbd0d4aedade78cdb28894 100644 (file)
@@ -672,6 +672,25 @@ void fh_fill_post_attrs(struct svc_fh *fhp)
                        nfsd4_change_attribute(&fhp->fh_post_attr, inode);
 }
 
+/**
+ * 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.
+ */
+void fh_fill_both_attrs(struct svc_fh *fhp)
+{
+       fh_fill_post_attrs(fhp);
+       if (!fhp->fh_post_saved)
+               return;
+       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;
+}
+
 /*
  * Release a file handle.
  */
index fb9d358a267e5f7bd1af4b39267624fd2b7baa48..28a4f9a94e2c8fdb1822578a4c2878a9a9e7f48b 100644 (file)
@@ -322,7 +322,7 @@ static inline u64 nfsd4_change_attribute(struct kstat *stat,
 
 extern void fh_fill_pre_attrs(struct svc_fh *fhp);
 extern void fh_fill_post_attrs(struct svc_fh *fhp);
-
+extern void fh_fill_both_attrs(struct svc_fh *fhp);
 
 /*
  * Lock a file handle/inode
index ac716ced1fd5fd567297a08077fb16805939f7a6..c07fe50d6bdfb9821c6cdb1f5412ab230a3d67c1 100644 (file)
@@ -198,27 +198,13 @@ nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
                                goto out_nfserr;
                }
        } else {
-               /*
-                * In the nfsd4_open() case, this may be held across
-                * subsequent open and delegation acquisition which may
-                * need to take the child's i_mutex:
-                */
-               fh_lock_nested(fhp, I_MUTEX_PARENT);
-               dentry = lookup_one_len(name, dparent, len);
+               dentry = lookup_one_len_unlocked(name, dparent, len);
                host_err = PTR_ERR(dentry);
                if (IS_ERR(dentry))
                        goto out_nfserr;
                if (nfsd_mountpoint(dentry, exp)) {
-                       /*
-                        * We don't need the i_mutex after all.  It's
-                        * still possible we could open this (regular
-                        * files can be mountpoints too), but the
-                        * i_mutex is just there to prevent renames of
-                        * something that we might be about to delegate,
-                        * and a mountpoint won't be renamed:
-                        */
-                       fh_unlock(fhp);
-                       if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
+                       host_err = nfsd_cross_mnt(rqstp, &dentry, &exp);
+                       if (host_err) {
                                dput(dentry);
                                goto out_nfserr;
                        }
@@ -233,7 +219,15 @@ out_nfserr:
        return nfserrno(host_err);
 }
 
-/*
+/**
+ * nfsd_lookup - look up a single path component for nfsd
+ *
+ * @rqstp:   the request context
+ * @fhp:     the file handle of the directory
+ * @name:    the component name, or %NULL to look up parent
+ * @len:     length of name to examine
+ * @resfh:   pointer to pre-initialised filehandle to hold result.
+ *
  * Look up one component of a pathname.
  * N.B. After this call _both_ fhp and resfh need an fh_put
  *
@@ -243,11 +237,11 @@ out_nfserr:
  * returned. Otherwise the covered directory is returned.
  * NOTE: this mountpoint crossing is not supported properly by all
  *   clients and is explicitly disallowed for NFSv3
- *      NeilBrown <neilb@cse.unsw.edu.au>
+ *
  */
 __be32
 nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
-                               unsigned int len, struct svc_fh *resfh)
+           unsigned int len, struct svc_fh *resfh)
 {
        struct svc_export       *exp;
        struct dentry           *dentry;