NFS: Don't ask for readdirplus unless it can help nfs_getattr()
authorTrond Myklebust <trond.myklebust@hammerspace.com>
Thu, 17 Feb 2022 20:46:23 +0000 (15:46 -0500)
committerTrond Myklebust <trond.myklebust@hammerspace.com>
Wed, 2 Mar 2022 13:43:38 +0000 (08:43 -0500)
If attribute caching is turned off, then use of readdirplus is not going
to help stat() performance.
Readdirplus also doesn't help if a file is being written to, since we
will have to flush those writes in order to sync the mtime/ctime.

Signed-off-by: Trond Myklebust <trond.myklebust@hammerspace.com>
fs/nfs/inode.c

index bbf4357ff727c404a8a61ac05ede32c4e69fde5e..e51d86707fcace83aaed301875983a50fea422c2 100644 (file)
@@ -780,26 +780,32 @@ void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr,
 }
 EXPORT_SYMBOL_GPL(nfs_setattr_update_inode);
 
-static void nfs_readdirplus_parent_cache_miss(struct dentry *dentry)
+/*
+ * Don't request help from readdirplus if the file is being written to,
+ * or if attribute caching is turned off
+ */
+static bool nfs_getattr_readdirplus_enable(const struct inode *inode)
 {
-       struct dentry *parent;
+       return nfs_server_capable(inode, NFS_CAP_READDIRPLUS) &&
+              !nfs_have_writebacks(inode) && NFS_MAXATTRTIMEO(inode) > 5 * HZ;
+}
 
-       if (!nfs_server_capable(d_inode(dentry), NFS_CAP_READDIRPLUS))
-               return;
-       parent = dget_parent(dentry);
-       nfs_readdir_record_entry_cache_miss(d_inode(parent));
-       dput(parent);
+static void nfs_readdirplus_parent_cache_miss(struct dentry *dentry)
+{
+       if (!IS_ROOT(dentry)) {
+               struct dentry *parent = dget_parent(dentry);
+               nfs_readdir_record_entry_cache_miss(d_inode(parent));
+               dput(parent);
+       }
 }
 
 static void nfs_readdirplus_parent_cache_hit(struct dentry *dentry)
 {
-       struct dentry *parent;
-
-       if (!nfs_server_capable(d_inode(dentry), NFS_CAP_READDIRPLUS))
-               return;
-       parent = dget_parent(dentry);
-       nfs_readdir_record_entry_cache_hit(d_inode(parent));
-       dput(parent);
+       if (!IS_ROOT(dentry)) {
+               struct dentry *parent = dget_parent(dentry);
+               nfs_readdir_record_entry_cache_hit(d_inode(parent));
+               dput(parent);
+       }
 }
 
 static u32 nfs_get_valid_attrmask(struct inode *inode)
@@ -835,6 +841,7 @@ int nfs_getattr(struct user_namespace *mnt_userns, const struct path *path,
        int err = 0;
        bool force_sync = query_flags & AT_STATX_FORCE_SYNC;
        bool do_update = false;
+       bool readdirplus_enabled = nfs_getattr_readdirplus_enable(inode);
 
        trace_nfs_getattr_enter(inode);
 
@@ -843,7 +850,8 @@ int nfs_getattr(struct user_namespace *mnt_userns, const struct path *path,
                        STATX_INO | STATX_SIZE | STATX_BLOCKS;
 
        if ((query_flags & AT_STATX_DONT_SYNC) && !force_sync) {
-               nfs_readdirplus_parent_cache_hit(path->dentry);
+               if (readdirplus_enabled)
+                       nfs_readdirplus_parent_cache_hit(path->dentry);
                goto out_no_revalidate;
        }
 
@@ -893,15 +901,12 @@ int nfs_getattr(struct user_namespace *mnt_userns, const struct path *path,
                do_update |= cache_validity & NFS_INO_INVALID_BLOCKS;
 
        if (do_update) {
-               /* Update the attribute cache */
-               if (!(server->flags & NFS_MOUNT_NOAC))
+               if (readdirplus_enabled)
                        nfs_readdirplus_parent_cache_miss(path->dentry);
-               else
-                       nfs_readdirplus_parent_cache_hit(path->dentry);
                err = __nfs_revalidate_inode(server, inode);
                if (err)
                        goto out;
-       } else
+       } else if (readdirplus_enabled)
                nfs_readdirplus_parent_cache_hit(path->dentry);
 out_no_revalidate:
        /* Only return attributes that were revalidated. */