1 From d6f30a383c330905761aaadde5894efe6572ae29 Mon Sep 17 00:00:00 2001
2 From: Sasha Levin <sashal@kernel.org>
3 Date: Tue, 26 Jul 2022 16:45:30 +1000
4 Subject: NFSD: use (un)lock_inode instead of fh_(un)lock for file operations
6 From: NeilBrown <neilb@suse.de>
8 [ Upstream commit bb4d53d66e4b8c8b8e5634802262e53851a2d2db ]
10 When locking a file to access ACLs and xattrs etc, use explicit locking
11 with inode_lock() instead of fh_lock(). This means that the calls to
12 fh_fill_pre/post_attr() are also explicit which improves readability and
13 allows us to place them only where they are needed. Only the xattr
14 calls need pre/post information.
16 When locking a file we don't need I_MUTEX_PARENT as the file is not a
17 parent of anything, so we can use inode_lock() directly rather than the
18 inode_lock_nested() call that fh_lock() uses.
20 Reviewed-by: Jeff Layton <jlayton@kernel.org>
21 Signed-off-by: NeilBrown <neilb@suse.de>
22 Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
24 fs/nfsd/nfs2acl.c | 6 +++---
25 fs/nfsd/nfs3acl.c | 4 ++--
26 fs/nfsd/nfs4state.c | 9 +++++----
27 fs/nfsd/vfs.c | 34 ++++++++++++++++++++--------------
28 4 files changed, 30 insertions(+), 23 deletions(-)
30 diff --git a/fs/nfsd/nfs2acl.c b/fs/nfsd/nfs2acl.c
31 index efcd429b0f28e..87f224cd30a85 100644
32 --- a/fs/nfsd/nfs2acl.c
33 +++ b/fs/nfsd/nfs2acl.c
34 @@ -111,7 +111,7 @@ static __be32 nfsacld_proc_setacl(struct svc_rqst *rqstp)
41 error = set_posix_acl(&init_user_ns, inode, ACL_TYPE_ACCESS,
43 @@ -122,7 +122,7 @@ static __be32 nfsacld_proc_setacl(struct svc_rqst *rqstp)
48 + inode_unlock(inode);
52 @@ -136,7 +136,7 @@ static __be32 nfsacld_proc_setacl(struct svc_rqst *rqstp)
57 + inode_unlock(inode);
60 resp->status = nfserrno(error);
61 diff --git a/fs/nfsd/nfs3acl.c b/fs/nfsd/nfs3acl.c
62 index 35b2ebda14dac..9446c67436649 100644
63 --- a/fs/nfsd/nfs3acl.c
64 +++ b/fs/nfsd/nfs3acl.c
65 @@ -101,7 +101,7 @@ static __be32 nfsd3_proc_setacl(struct svc_rqst *rqstp)
72 error = set_posix_acl(&init_user_ns, inode, ACL_TYPE_ACCESS,
74 @@ -111,7 +111,7 @@ static __be32 nfsd3_proc_setacl(struct svc_rqst *rqstp)
79 + inode_unlock(inode);
82 resp->status = nfserrno(error);
83 diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
84 index f66fb39714893..66cf8217ebe57 100644
85 --- a/fs/nfsd/nfs4state.c
86 +++ b/fs/nfsd/nfs4state.c
87 @@ -7415,21 +7415,22 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
88 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
91 + struct inode *inode;
94 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
97 - fh_lock(fhp); /* to block new leases till after test_lock: */
98 - err = nfserrno(nfsd_open_break_lease(fhp->fh_dentry->d_inode,
100 + inode = fhp->fh_dentry->d_inode;
101 + inode_lock(inode); /* to block new leases till after test_lock: */
102 + err = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
105 lock->fl_file = nf->nf_file;
106 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
107 lock->fl_file = NULL;
110 + inode_unlock(inode);
114 diff --git a/fs/nfsd/vfs.c b/fs/nfsd/vfs.c
115 index 7de76b37a9bc2..73a153be6a5ad 100644
118 @@ -417,7 +417,7 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
126 * RFC5661, Section 18.30.4:
127 @@ -465,7 +465,7 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
128 attr->na_aclerr = set_posix_acl(&init_user_ns,
129 inode, ACL_TYPE_DEFAULT,
132 + inode_unlock(inode);
134 put_write_access(inode);
136 @@ -2156,13 +2156,16 @@ nfsd_listxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char **bufp,
141 - * Removexattr and setxattr need to call fh_lock to both lock the inode
142 - * and set the change attribute. Since the top-level vfs_removexattr
143 - * and vfs_setxattr calls already do their own inode_lock calls, call
144 - * the _locked variant. Pass in a NULL pointer for delegated_inode,
145 - * and let the client deal with NFS4ERR_DELAY (same as with e.g.
146 - * setattr and remove).
148 + * nfsd_removexattr - Remove an extended attribute
149 + * @rqstp: RPC transaction being executed
150 + * @fhp: NFS filehandle of object with xattr to remove
151 + * @name: name of xattr to remove (NUL-terminate)
153 + * Pass in a NULL pointer for delegated_inode, and let the client deal
154 + * with NFS4ERR_DELAY (same as with e.g. setattr and remove).
156 + * Returns nfs_ok on success, or an nfsstat in network byte order.
159 nfsd_removexattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name)
160 @@ -2178,12 +2181,14 @@ nfsd_removexattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name)
162 return nfserrno(ret);
165 + inode_lock(fhp->fh_dentry->d_inode);
166 + fh_fill_pre_attrs(fhp);
168 ret = __vfs_removexattr_locked(&init_user_ns, fhp->fh_dentry,
172 + fh_fill_post_attrs(fhp);
173 + inode_unlock(fhp->fh_dentry->d_inode);
176 return nfsd_xattr_errno(ret);
177 @@ -2203,12 +2208,13 @@ nfsd_setxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name,
178 ret = fh_want_write(fhp);
180 return nfserrno(ret);
182 + inode_lock(fhp->fh_dentry->d_inode);
183 + fh_fill_pre_attrs(fhp);
185 ret = __vfs_setxattr_locked(&init_user_ns, fhp->fh_dentry, name, buf,
189 + fh_fill_post_attrs(fhp);
190 + inode_unlock(fhp->fh_dentry->d_inode);
193 return nfsd_xattr_errno(ret);