blob: f7ef2ca699a5f6268ca78137f995f77d9c3cc963 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Jeff Layton1eb5d982018-01-09 08:21:17 -050057#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Frank van der Linden012a2112020-06-23 22:39:03 +000069#include "nfs42.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Trond Myklebustc6d01c62013-08-09 11:51:26 -040071#include "nfs4trace.h"
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define NFSDBG_FACILITY NFSDBG_PROC
74
Trond Myklebust30846df2018-04-07 13:44:28 -040075#define NFS4_BITMASK_SZ 3
76
Trond Myklebust2066fe82006-09-15 08:30:46 -040077#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#define NFS4_POLL_RETRY_MAX (15*HZ)
79
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020080/* file attributes which can be mapped to nfs attributes */
81#define NFS4_VALID_ATTRS (ATTR_MODE \
82 | ATTR_UID \
83 | ATTR_GID \
84 | ATTR_SIZE \
85 | ATTR_ATIME \
86 | ATTR_MTIME \
87 | ATTR_CTIME \
88 | ATTR_ATIME_SET \
89 | ATTR_MTIME_SET)
90
Trond Myklebustcdd4e682006-01-03 09:55:12 +010091struct nfs4_opendata;
Alexandros Batsakisb2579572009-12-14 21:27:57 -080092static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050094static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Trond Myklebusta841b542018-04-07 13:50:59 -040095static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
NeilBrowna52458b2018-12-03 11:30:31 +110096static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust0ab64e02010-04-16 16:22:51 -040097 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110098 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040099 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400100#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400101static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +1100102 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400103 struct nfs4_slot *slot,
104 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400105static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100106 const struct cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400107static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100108 const struct cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400109#endif
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -0400110static void nfs4_bitmask_adjust(__u32 *bitmask, struct inode *inode,
111 struct nfs_server *server,
112 struct nfs4_label *label);
David Quigleyaa9c2662013-05-22 12:50:44 -0400113
114#ifdef CONFIG_NFS_V4_SECURITY_LABEL
115static inline struct nfs4_label *
116nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
117 struct iattr *sattr, struct nfs4_label *label)
118{
119 int err;
120
121 if (label == NULL)
122 return NULL;
123
124 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
125 return NULL;
126
David Quigleyaa9c2662013-05-22 12:50:44 -0400127 err = security_dentry_init_security(dentry, sattr->ia_mode,
128 &dentry->d_name, (void **)&label->label, &label->len);
129 if (err == 0)
130 return label;
131
132 return NULL;
133}
134static inline void
135nfs4_label_release_security(struct nfs4_label *label)
136{
137 if (label)
138 security_release_secctx(label->label, label->len);
139}
140static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
141{
142 if (label)
143 return server->attr_bitmask;
144
145 return server->attr_bitmask_nl;
146}
147#else
148static inline struct nfs4_label *
149nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
150 struct iattr *sattr, struct nfs4_label *l)
151{ return NULL; }
152static inline void
153nfs4_label_release_security(struct nfs4_label *label)
154{ return; }
155static inline u32 *
156nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
157{ return server->attr_bitmask; }
158#endif
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500161static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Trond Myklebust52567b02009-10-23 14:46:42 -0400163 if (err >= -1000)
164 return err;
165 switch (err) {
166 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500167 case -NFS4ERR_LAYOUTTRYLATER:
168 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400169 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000170 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400171 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000172 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800173 case -NFS4ERR_BADOWNER:
174 case -NFS4ERR_BADNAME:
175 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400176 case -NFS4ERR_SHARE_DENIED:
177 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400178 case -NFS4ERR_MINOR_VERS_MISMATCH:
179 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400180 case -NFS4ERR_FILE_OPEN:
181 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400182 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700184 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400185 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400187 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
190/*
191 * This is our standard bitmap for GETATTR requests.
192 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400193const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 FATTR4_WORD0_TYPE
195 | FATTR4_WORD0_CHANGE
196 | FATTR4_WORD0_SIZE
197 | FATTR4_WORD0_FSID
198 | FATTR4_WORD0_FILEID,
199 FATTR4_WORD1_MODE
200 | FATTR4_WORD1_NUMLINKS
201 | FATTR4_WORD1_OWNER
202 | FATTR4_WORD1_OWNER_GROUP
203 | FATTR4_WORD1_RAWDEV
204 | FATTR4_WORD1_SPACE_USED
205 | FATTR4_WORD1_TIME_ACCESS
206 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400207 | FATTR4_WORD1_TIME_MODIFY
208 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400209#ifdef CONFIG_NFS_V4_SECURITY_LABEL
210 FATTR4_WORD2_SECURITY_LABEL
211#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212};
213
Trond Myklebust1549210f2012-06-05 09:16:47 -0400214static const u32 nfs4_pnfs_open_bitmap[3] = {
215 FATTR4_WORD0_TYPE
216 | FATTR4_WORD0_CHANGE
217 | FATTR4_WORD0_SIZE
218 | FATTR4_WORD0_FSID
219 | FATTR4_WORD0_FILEID,
220 FATTR4_WORD1_MODE
221 | FATTR4_WORD1_NUMLINKS
222 | FATTR4_WORD1_OWNER
223 | FATTR4_WORD1_OWNER_GROUP
224 | FATTR4_WORD1_RAWDEV
225 | FATTR4_WORD1_SPACE_USED
226 | FATTR4_WORD1_TIME_ACCESS
227 | FATTR4_WORD1_TIME_METADATA
228 | FATTR4_WORD1_TIME_MODIFY,
229 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500230#ifdef CONFIG_NFS_V4_SECURITY_LABEL
231 | FATTR4_WORD2_SECURITY_LABEL
232#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400233};
234
Andy Adamsone23008e2012-10-02 21:07:32 -0400235static const u32 nfs4_open_noattr_bitmap[3] = {
236 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400237 | FATTR4_WORD0_FILEID,
238};
239
David Quigleya09df2c2013-05-22 12:50:41 -0400240const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 FATTR4_WORD0_FILES_AVAIL
242 | FATTR4_WORD0_FILES_FREE
243 | FATTR4_WORD0_FILES_TOTAL,
244 FATTR4_WORD1_SPACE_AVAIL
245 | FATTR4_WORD1_SPACE_FREE
246 | FATTR4_WORD1_SPACE_TOTAL
247};
248
David Quigleya09df2c2013-05-22 12:50:41 -0400249const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 FATTR4_WORD0_MAXLINK
251 | FATTR4_WORD0_MAXNAME,
252 0
253};
254
Fred Isamandae100c2011-07-30 20:52:37 -0400255const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 | FATTR4_WORD0_MAXREAD
257 | FATTR4_WORD0_MAXWRITE
258 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700259 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400260 | FATTR4_WORD1_FS_LAYOUT_TYPES,
261 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800262 | FATTR4_WORD2_CLONE_BLKSIZE
Frank van der Lindenb78ef842020-06-23 22:38:55 +0000263 | FATTR4_WORD2_XATTR_SUPPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264};
265
David Quigleya09df2c2013-05-22 12:50:41 -0400266const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500267 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400268 | FATTR4_WORD0_SIZE
269 | FATTR4_WORD0_FSID
270 | FATTR4_WORD0_FILEID
271 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500272 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400273 | FATTR4_WORD1_OWNER_GROUP
274 | FATTR4_WORD1_RAWDEV
275 | FATTR4_WORD1_SPACE_USED
276 | FATTR4_WORD1_TIME_ACCESS
277 | FATTR4_WORD1_TIME_METADATA
278 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400279 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400280};
281
Trond Myklebust30846df2018-04-07 13:44:28 -0400282static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
283 struct inode *inode)
284{
285 unsigned long cache_validity;
286
287 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
288 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
289 return;
290
291 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
292 if (!(cache_validity & NFS_INO_REVAL_FORCED))
293 cache_validity &= ~(NFS_INO_INVALID_CHANGE
294 | NFS_INO_INVALID_SIZE);
295
296 if (!(cache_validity & NFS_INO_INVALID_SIZE))
297 dst[0] &= ~FATTR4_WORD0_SIZE;
298
299 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
300 dst[0] &= ~FATTR4_WORD0_CHANGE;
301}
302
303static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
304 const __u32 *src, struct inode *inode)
305{
306 nfs4_bitmap_copy_adjust(dst, src, inode);
307}
308
Al Virobc4785c2006-10-19 23:28:51 -0700309static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 struct nfs4_readdir_arg *readdir)
311{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400312 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700313 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000316 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
318 return;
319 }
320
321 readdir->cookie = 0;
322 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
323 if (cookie == 2)
324 return;
325
326 /*
327 * NFSv4 servers do not return entries for '.' and '..'
328 * Therefore, we fake these entries here. We let '.'
329 * have cookie 0 and '..' have cookie 1. Note that
330 * when talking to the server, we always send cookie 0
331 * instead of 1 or 2.
332 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800333 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 if (cookie == 0) {
336 *p++ = xdr_one; /* next */
337 *p++ = xdr_zero; /* cookie, first word */
338 *p++ = xdr_one; /* cookie, second word */
339 *p++ = xdr_one; /* entry len */
340 memcpy(p, ".\0\0\0", 4); /* entry */
341 p++;
342 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400343 *p++ = htonl(attrs); /* bitmap */
344 *p++ = htonl(12); /* attribute buffer length */
345 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000346 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348
349 *p++ = xdr_one; /* next */
350 *p++ = xdr_zero; /* cookie, first word */
351 *p++ = xdr_two; /* cookie, second word */
352 *p++ = xdr_two; /* entry len */
353 memcpy(p, "..\0\0", 4); /* entry */
354 p++;
355 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400356 *p++ = htonl(attrs); /* bitmap */
357 *p++ = htonl(12); /* attribute buffer length */
358 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000359 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 readdir->pgbase = (char *)p - (char *)start;
362 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800363 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Trond Myklebust26d36302016-09-22 13:39:05 -0400366static void nfs4_test_and_free_stateid(struct nfs_server *server,
367 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100368 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400369{
370 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
371
372 ops->test_and_free_expired(server, stateid, cred);
373}
374
375static void __nfs4_free_revoked_stateid(struct nfs_server *server,
376 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100377 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400378{
379 stateid->type = NFS4_REVOKED_STATEID_TYPE;
380 nfs4_test_and_free_stateid(server, stateid, cred);
381}
382
383static void nfs4_free_revoked_stateid(struct nfs_server *server,
384 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100385 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400386{
387 nfs4_stateid tmp;
388
389 nfs4_stateid_copy(&tmp, stateid);
390 __nfs4_free_revoked_stateid(server, &tmp, cred);
391}
392
NeilBrown8478eaa2014-09-18 16:09:27 +1000393static long nfs4_update_delay(long *timeout)
394{
395 long ret;
396 if (!timeout)
397 return NFS4_POLL_RETRY_MAX;
398 if (*timeout <= 0)
399 *timeout = NFS4_POLL_RETRY_MIN;
400 if (*timeout > NFS4_POLL_RETRY_MAX)
401 *timeout = NFS4_POLL_RETRY_MAX;
402 ret = *timeout;
403 *timeout <<= 1;
404 return ret;
405}
406
Trond Myklebust0688e642019-04-07 13:59:09 -0400407static int nfs4_delay_killable(long *timeout)
Trond Myklebust65de8722008-12-23 15:21:44 -0500408{
Trond Myklebust65de8722008-12-23 15:21:44 -0500409 might_sleep();
410
NeilBrown8478eaa2014-09-18 16:09:27 +1000411 freezable_schedule_timeout_killable_unsafe(
412 nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400413 if (!__fatal_signal_pending(current))
414 return 0;
415 return -EINTR;
416}
417
418static int nfs4_delay_interruptible(long *timeout)
419{
420 might_sleep();
421
He Zhe59679d92020-07-06 17:52:24 +0800422 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400423 if (!signal_pending(current))
424 return 0;
425 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
426}
427
428static int nfs4_delay(long *timeout, bool interruptible)
429{
430 if (interruptible)
431 return nfs4_delay_interruptible(timeout);
432 return nfs4_delay_killable(timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500433}
434
Trond Myklebust50c80002019-07-11 19:02:18 -0400435static const nfs4_stateid *
436nfs4_recoverable_stateid(const nfs4_stateid *stateid)
437{
438 if (!stateid)
439 return NULL;
440 switch (stateid->type) {
441 case NFS4_OPEN_STATEID_TYPE:
442 case NFS4_LOCK_STATEID_TYPE:
443 case NFS4_DELEGATION_STATEID_TYPE:
444 return stateid;
445 default:
446 break;
447 }
448 return NULL;
449}
450
Trond Myklebust65de8722008-12-23 15:21:44 -0500451/* This is the error handling routine for processes that are allowed
452 * to sleep.
453 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400454static int nfs4_do_handle_exception(struct nfs_server *server,
455 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500456{
457 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500458 struct nfs4_state *state = exception->state;
Trond Myklebust50c80002019-07-11 19:02:18 -0400459 const nfs4_stateid *stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500460 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500461 int ret = errorcode;
462
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400463 exception->delay = 0;
464 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500465 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400466
Trond Myklebust50c80002019-07-11 19:02:18 -0400467 stateid = nfs4_recoverable_stateid(exception->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400468 if (stateid == NULL && state != NULL)
Trond Myklebust50c80002019-07-11 19:02:18 -0400469 stateid = nfs4_recoverable_stateid(&state->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400470
Trond Myklebust65de8722008-12-23 15:21:44 -0500471 switch(errorcode) {
472 case 0:
473 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400474 case -NFS4ERR_BADHANDLE:
475 case -ESTALE:
476 if (inode != NULL && S_ISREG(inode->i_mode))
477 pnfs_destroy_layout(NFS_I(inode));
478 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400479 case -NFS4ERR_DELEG_REVOKED:
480 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400481 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400482 case -NFS4ERR_BAD_STATEID:
Olga Kornievskaiafefa1a82019-06-14 14:22:12 -0400483 case -NFS4ERR_PARTNER_NO_AUTH:
Trond Myklebust272289a2016-09-22 13:39:15 -0400484 if (inode != NULL && stateid != NULL) {
485 nfs_inode_find_state_and_recover(inode,
486 stateid);
487 goto wait_on_recovery;
488 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500489 fallthrough;
Trond Myklebust272289a2016-09-22 13:39:15 -0400490 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400491 if (inode) {
492 int err;
493
494 err = nfs_async_inode_return_delegation(inode,
495 stateid);
496 if (err == 0)
497 goto wait_on_recovery;
498 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
499 exception->retry = 1;
500 break;
501 }
502 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500503 if (state == NULL)
504 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400505 ret = nfs4_schedule_stateid_recovery(server, state);
506 if (ret < 0)
507 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500508 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500509 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500510 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500511 nfs4_schedule_lease_recovery(clp);
512 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400513 case -NFS4ERR_MOVED:
514 ret = nfs4_schedule_migration_recovery(server);
515 if (ret < 0)
516 break;
517 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400518 case -NFS4ERR_LEASE_MOVED:
519 nfs4_schedule_lease_moved_recovery(clp);
520 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500521#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400522 case -NFS4ERR_BADSESSION:
523 case -NFS4ERR_BADSLOT:
524 case -NFS4ERR_BAD_HIGH_SLOT:
525 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
526 case -NFS4ERR_DEADSESSION:
527 case -NFS4ERR_SEQ_FALSE_RETRY:
528 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust5c441542019-11-13 08:34:00 +0100529 /* Handled in nfs41_sequence_process() */
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400530 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500531#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500532 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500533 if (exception->timeout > HZ) {
534 /* We have retried a decent amount, time to
535 * fail
536 */
537 ret = -EBUSY;
538 break;
539 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500540 fallthrough;
Trond Myklebust65de8722008-12-23 15:21:44 -0500541 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400542 nfs_inc_server_stats(server, NFSIOS_DELAY);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500543 fallthrough;
Trond Myklebust2598ed32015-09-20 16:10:18 -0400544 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400545 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400546 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400547 exception->delay = 1;
548 return 0;
549
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400550 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500551 case -NFS4ERR_OLD_STATEID:
552 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800553 break;
554 case -NFS4ERR_BADOWNER:
555 /* The following works around a Linux server bug! */
556 case -NFS4ERR_BADNAME:
557 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
558 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
559 exception->retry = 1;
560 printk(KERN_WARNING "NFS: v4 server %s "
561 "does not accept raw "
562 "uid/gids. "
563 "Reenabling the idmapper.\n",
564 server->nfs_client->cl_hostname);
565 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500566 }
567 /* We failed to handle the error */
568 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500569wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400570 exception->recovering = 1;
571 return 0;
572}
573
574/* This is the error handling routine for processes that are allowed
575 * to sleep.
576 */
577int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
578{
579 struct nfs_client *clp = server->nfs_client;
580 int ret;
581
582 ret = nfs4_do_handle_exception(server, errorcode, exception);
583 if (exception->delay) {
Trond Myklebust0688e642019-04-07 13:59:09 -0400584 ret = nfs4_delay(&exception->timeout,
585 exception->interruptible);
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400586 goto out_retry;
587 }
588 if (exception->recovering) {
589 ret = nfs4_wait_clnt_recover(clp);
590 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
591 return -EIO;
592 goto out_retry;
593 }
594 return ret;
595out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500596 if (ret == 0)
597 exception->retry = 1;
598 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500599}
600
Trond Myklebust037fc982015-09-20 15:51:00 -0400601static int
602nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
603 int errorcode, struct nfs4_exception *exception)
604{
605 struct nfs_client *clp = server->nfs_client;
606 int ret;
607
608 ret = nfs4_do_handle_exception(server, errorcode, exception);
609 if (exception->delay) {
610 rpc_delay(task, nfs4_update_delay(&exception->timeout));
611 goto out_retry;
612 }
613 if (exception->recovering) {
614 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
615 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
616 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
617 goto out_retry;
618 }
619 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
620 ret = -EIO;
621 return ret;
622out_retry:
Bill Baker0f90be12018-06-19 16:24:58 -0500623 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400624 exception->retry = 1;
Bill Baker0f90be12018-06-19 16:24:58 -0500625 /*
626 * For NFS4ERR_MOVED, the client transport will need to
627 * be recomputed after migration recovery has completed.
628 */
629 if (errorcode == -NFS4ERR_MOVED)
630 rpc_task_release_transport(task);
631 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400632 return ret;
633}
634
Olga Kornievskaia0f913a52018-07-09 15:13:33 -0400635int
Trond Myklebust037fc982015-09-20 15:51:00 -0400636nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
637 struct nfs4_state *state, long *timeout)
638{
639 struct nfs4_exception exception = {
640 .state = state,
641 };
642
643 if (task->tk_status >= 0)
644 return 0;
645 if (timeout)
646 exception.timeout = *timeout;
647 task->tk_status = nfs4_async_handle_exception(task, server,
648 task->tk_status,
649 &exception);
650 if (exception.delay && timeout)
651 *timeout = exception.timeout;
652 if (exception.retry)
653 return -EAGAIN;
654 return 0;
655}
656
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400657/*
658 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
659 * or 'false' otherwise.
660 */
661static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
662{
663 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500664 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400665}
Trond Myklebust65de8722008-12-23 15:21:44 -0500666
Trond Myklebust452e9352010-07-31 14:29:06 -0400667static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 spin_lock(&clp->cl_lock);
670 if (time_before(clp->cl_last_renewal,timestamp))
671 clp->cl_last_renewal = timestamp;
672 spin_unlock(&clp->cl_lock);
673}
674
Trond Myklebust452e9352010-07-31 14:29:06 -0400675static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
676{
Trond Myklebustbe824162015-07-05 14:50:46 -0400677 struct nfs_client *clp = server->nfs_client;
678
679 if (!nfs4_has_session(clp))
680 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400681}
682
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400683struct nfs4_call_sync_data {
684 const struct nfs_server *seq_server;
685 struct nfs4_sequence_args *seq_args;
686 struct nfs4_sequence_res *seq_res;
687};
688
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800689void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400690 struct nfs4_sequence_res *res, int cache_reply,
691 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400692{
693 args->sa_slot = NULL;
694 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400695 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400696
697 res->sr_slot = NULL;
698}
699
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400700static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400701{
702 struct nfs4_slot *slot = res->sr_slot;
703 struct nfs4_slot_table *tbl;
704
Chuck Lever3bd23842013-08-09 12:49:19 -0400705 tbl = slot->table;
706 spin_lock(&tbl->slot_tbl_lock);
707 if (!nfs41_wake_and_assign_slot(tbl, slot))
708 nfs4_free_slot(tbl, slot);
709 spin_unlock(&tbl->slot_tbl_lock);
710
711 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400712}
713
714static int nfs40_sequence_done(struct rpc_task *task,
715 struct nfs4_sequence_res *res)
716{
717 if (res->sr_slot != NULL)
718 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400719 return 1;
720}
721
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400722#if defined(CONFIG_NFS_V4_1)
723
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400724static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400725{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500726 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400727 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500728 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400729
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400730 if (!slot)
731 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500732 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500733 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500734
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400735 /* Bump the slot sequence number */
736 if (slot->seq_done)
737 slot->seq_nr++;
738 slot->seq_done = 0;
739
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500740 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500741 /* Be nice to the server: try to ensure that the last transmitted
742 * value for highest_user_slotid <= target_highest_slotid
743 */
744 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
745 send_new_highest_used_slotid = true;
746
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500747 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500748 send_new_highest_used_slotid = false;
749 goto out_unlock;
750 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500751 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500752
753 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
754 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500755out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500756 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500757 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400758 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400759 if (waitqueue_active(&tbl->slot_waitq))
760 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400761}
762
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400763static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
764{
765 nfs41_release_slot(res->sr_slot);
766 res->sr_slot = NULL;
767}
768
Trond Myklebust3453d572018-06-20 17:53:34 -0400769static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
770 u32 seqnr)
771{
772 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
773 slot->seq_nr_highest_sent = seqnr;
774}
775static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
776 u32 seqnr)
777{
778 slot->seq_nr_highest_sent = seqnr;
779 slot->seq_nr_last_acked = seqnr;
780}
781
Anna Schumaker913fadc2020-07-08 10:33:40 -0400782static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
783 struct nfs4_slot *slot)
784{
785 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
786 if (!IS_ERR(task))
787 rpc_put_task_async(task);
788}
789
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400790static int nfs41_sequence_process(struct rpc_task *task,
791 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400792{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500793 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500794 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400795 struct nfs_client *clp;
Trond Myklebust5c441542019-11-13 08:34:00 +0100796 int status;
Trond Myklebust85563072012-12-11 10:31:12 -0500797 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400798
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500799 if (slot == NULL)
800 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400801 /* don't increment the sequence number if the task wasn't sent */
Trond Myklebustc71c46f2019-03-01 11:40:05 -0500802 if (!RPC_WAS_SENT(task) || slot->seq_done)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400803 goto out;
804
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500805 session = slot->table->session;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400806 clp = session->clp;
Trond Myklebust933602e2012-11-16 12:12:38 -0500807
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400808 trace_nfs4_sequence_done(session, res);
Trond Myklebust5c441542019-11-13 08:34:00 +0100809
810 status = res->sr_status;
811 if (task->tk_status == -NFS4ERR_DEADSESSION)
812 status = -NFS4ERR_DEADSESSION;
813
Andy Adamson691daf32009-12-04 15:55:39 -0500814 /* Check the SEQUENCE operation status */
Trond Myklebust5c441542019-11-13 08:34:00 +0100815 switch (status) {
Trond Myklebust14516c32010-07-31 14:29:06 -0400816 case 0:
Trond Myklebust3453d572018-06-20 17:53:34 -0400817 /* Mark this sequence number as having been acked */
818 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400819 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400820 slot->seq_done = 1;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500821 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500822 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400823 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
824 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500825 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400826 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500827 case 1:
828 /*
829 * sr_status remains 1 if an RPC level error occurred.
830 * The server may or may not have processed the sequence
831 * operation..
Trond Myklebustac20d162012-12-15 15:36:07 -0500832 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400833 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
834 slot->seq_done = 1;
Trond Myklebustac20d162012-12-15 15:36:07 -0500835 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400836 case -NFS4ERR_DELAY:
837 /* The server detected a resend of the RPC call and
838 * returned NFS4ERR_DELAY as per Section 2.10.6.2
839 * of RFC5661.
840 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500841 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400842 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500843 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500844 slot->seq_nr);
Trond Myklebust3453d572018-06-20 17:53:34 -0400845 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400846 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400847 case -NFS4ERR_RETRY_UNCACHED_REP:
848 case -NFS4ERR_SEQ_FALSE_RETRY:
849 /*
850 * The server thinks we tried to replay a request.
851 * Retry the call after bumping the sequence ID.
852 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400853 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebustf9312a52018-06-09 19:10:31 -0400854 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500855 case -NFS4ERR_BADSLOT:
856 /*
857 * The slot id we used was probably retired. Try again
858 * using a different slot id.
859 */
Trond Myklebust99589102018-06-09 12:50:50 -0400860 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400861 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500862 goto retry_nowait;
863 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust3453d572018-06-20 17:53:34 -0400864 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
Trond Myklebuste8794442012-12-15 13:56:18 -0500865 /*
Trond Myklebust3453d572018-06-20 17:53:34 -0400866 * Were one or more calls using this slot interrupted?
867 * If the server never received the request, then our
Anna Schumaker913fadc2020-07-08 10:33:40 -0400868 * transmitted slot sequence number may be too high. However,
869 * if the server did receive the request then it might
870 * accidentally give us a reply with a mismatched operation.
871 * We can sort this out by sending a lone sequence operation
872 * to the server on the same slot.
Trond Myklebustac20d162012-12-15 15:36:07 -0500873 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400874 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
875 slot->seq_nr--;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400876 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
877 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
878 res->sr_slot = NULL;
879 }
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500880 goto retry_nowait;
881 }
Trond Myklebust3453d572018-06-20 17:53:34 -0400882 /*
883 * RFC5661:
884 * A retry might be sent while the original request is
885 * still in progress on the replier. The replier SHOULD
886 * deal with the issue by returning NFS4ERR_DELAY as the
887 * reply to SEQUENCE or CB_SEQUENCE operation, but
888 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
889 *
890 * Restart the search after a delay.
891 */
892 slot->seq_nr = slot->seq_nr_highest_sent;
893 goto out_retry;
Trond Myklebust5c441542019-11-13 08:34:00 +0100894 case -NFS4ERR_BADSESSION:
895 case -NFS4ERR_DEADSESSION:
896 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
897 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400898 default:
899 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400900 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400901 }
902out:
903 /* The session may be reset by one of the error handlers. */
904 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500905out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500906 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400907session_recover:
Trond Myklebust5c441542019-11-13 08:34:00 +0100908 nfs4_schedule_session_recovery(session, status);
909 dprintk("%s ERROR: %d Reset session\n", __func__, status);
910 nfs41_sequence_free_slot(res);
911 goto out;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400912retry_new_seq:
913 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500914retry_nowait:
915 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400916 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500917 task->tk_status = 0;
918 ret = 0;
919 }
920 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400921out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400922 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400923 goto out;
924 rpc_delay(task, NFS4_POLL_RETRY_MAX);
925 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400926}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400927
928int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
929{
930 if (!nfs41_sequence_process(task, res))
931 return 0;
932 if (res->sr_slot != NULL)
933 nfs41_sequence_free_slot(res);
934 return 1;
935
936}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500937EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400938
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400939static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
940{
941 if (res->sr_slot == NULL)
942 return 1;
943 if (res->sr_slot->table->session != NULL)
944 return nfs41_sequence_process(task, res);
945 return nfs40_sequence_done(task, res);
946}
947
948static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
949{
950 if (res->sr_slot != NULL) {
951 if (res->sr_slot->table->session != NULL)
952 nfs41_sequence_free_slot(res);
953 else
954 nfs40_sequence_free_slot(res);
955 }
956}
957
Peng Tao2c4b1312014-06-11 05:24:16 +0800958int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400959{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500960 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400961 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400962 if (!res->sr_slot->table->session)
963 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400964 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400965}
Peng Tao2c4b1312014-06-11 05:24:16 +0800966EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400967
Andy Adamsonce5039c2009-04-01 09:22:13 -0400968static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
969{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400970 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400971
Trond Myklebust035168ab2010-06-16 09:52:26 -0400972 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
973
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500974 nfs4_setup_sequence(data->seq_server->nfs_client,
975 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400976}
977
Andy Adamson69ab40c2009-04-01 09:22:19 -0400978static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
979{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400980 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400981
Trond Myklebust14516c32010-07-31 14:29:06 -0400982 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400983}
984
Trond Myklebust17280172012-03-11 13:11:00 -0400985static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400986 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400987 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400988};
989
Chuck Lever3bd23842013-08-09 12:49:19 -0400990#else /* !CONFIG_NFS_V4_1 */
991
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400992static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
993{
994 return nfs40_sequence_done(task, res);
995}
996
997static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
998{
999 if (res->sr_slot != NULL)
1000 nfs40_sequence_free_slot(res);
1001}
1002
Peng Tao2c4b1312014-06-11 05:24:16 +08001003int nfs4_sequence_done(struct rpc_task *task,
1004 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001005{
Chuck Lever3bd23842013-08-09 12:49:19 -04001006 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001007}
Peng Tao2c4b1312014-06-11 05:24:16 +08001008EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001009
1010#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001011
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001012static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1013{
1014 res->sr_timestamp = jiffies;
1015 res->sr_status_flags = 0;
1016 res->sr_status = 1;
1017}
1018
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001019static
1020void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1021 struct nfs4_sequence_res *res,
1022 struct nfs4_slot *slot)
1023{
1024 if (!slot)
1025 return;
1026 slot->privileged = args->sa_privileged ? 1 : 0;
1027 args->sa_slot = slot;
1028
1029 res->sr_slot = slot;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001030}
1031
1032int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001033 struct nfs4_sequence_args *args,
1034 struct nfs4_sequence_res *res,
1035 struct rpc_task *task)
1036{
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001037 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -05001038 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -05001039 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001040
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001041 /* slot already allocated? */
1042 if (res->sr_slot != NULL)
1043 goto out_start;
1044
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001045 if (session)
Anna Schumaker76ee0352017-01-10 16:49:31 -05001046 tbl = &session->fc_slot_table;
Anna Schumaker76ee0352017-01-10 16:49:31 -05001047
Trond Myklebust3453d572018-06-20 17:53:34 -04001048 spin_lock(&tbl->slot_tbl_lock);
1049 /* The state manager will wait until the slot table is empty */
1050 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1051 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -05001052
Trond Myklebust3453d572018-06-20 17:53:34 -04001053 slot = nfs4_alloc_slot(tbl);
1054 if (IS_ERR(slot)) {
Trond Myklebust3453d572018-06-20 17:53:34 -04001055 if (slot == ERR_PTR(-ENOMEM))
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001056 goto out_sleep_timeout;
Trond Myklebust3453d572018-06-20 17:53:34 -04001057 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -05001058 }
Trond Myklebust3453d572018-06-20 17:53:34 -04001059 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001060
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001061 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001062
Anna Schumakerad05cc02017-01-11 13:37:06 -05001063 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001064out_start:
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001065 nfs41_sequence_res_init(res);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001066 rpc_call_start(task);
1067 return 0;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001068out_sleep_timeout:
1069 /* Try again in 1/4 second */
1070 if (args->sa_privileged)
1071 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1072 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1073 else
1074 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1075 NULL, jiffies + (HZ >> 2));
1076 spin_unlock(&tbl->slot_tbl_lock);
1077 return -EAGAIN;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001078out_sleep:
1079 if (args->sa_privileged)
1080 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
Trond Myklebust8357a9b2019-04-07 13:58:48 -04001081 RPC_PRIORITY_PRIVILEGED);
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001082 else
1083 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1084 spin_unlock(&tbl->slot_tbl_lock);
1085 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001086}
1087EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1088
Chuck Lever9915ea72013-08-09 12:48:27 -04001089static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1090{
1091 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001092 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001093 data->seq_args, data->seq_res, task);
1094}
1095
1096static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1097{
1098 struct nfs4_call_sync_data *data = calldata;
1099 nfs4_sequence_done(task, data->seq_res);
1100}
1101
1102static const struct rpc_call_ops nfs40_call_sync_ops = {
1103 .rpc_call_prepare = nfs40_call_sync_prepare,
1104 .rpc_call_done = nfs40_call_sync_done,
1105};
1106
Anna Schumaker48c05852019-08-14 15:27:00 -04001107static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1108{
1109 int ret;
1110 struct rpc_task *task;
1111
1112 task = rpc_run_task(task_setup);
1113 if (IS_ERR(task))
1114 return PTR_ERR(task);
1115
1116 ret = task->tk_status;
1117 rpc_put_task(task);
1118 return ret;
1119}
1120
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001121static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1122 struct nfs_server *server,
1123 struct rpc_message *msg,
1124 struct nfs4_sequence_args *args,
1125 struct nfs4_sequence_res *res,
1126 unsigned short task_flags)
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001127{
Chuck Lever9915ea72013-08-09 12:48:27 -04001128 struct nfs_client *clp = server->nfs_client;
1129 struct nfs4_call_sync_data data = {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001130 .seq_server = server,
Trond Myklebustad389da2007-06-05 12:30:00 -04001131 .seq_args = args,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001132 .seq_res = res,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001133 };
1134 struct rpc_task_setup task_setup = {
Trond Myklebustad389da2007-06-05 12:30:00 -04001135 .rpc_client = clnt,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001136 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001137 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001138 .callback_data = &data,
1139 .flags = task_flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001140 };
1141
Anna Schumaker48c05852019-08-14 15:27:00 -04001142 return nfs4_call_sync_custom(&task_setup);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001143}
1144
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001145static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1146 struct nfs_server *server,
1147 struct rpc_message *msg,
1148 struct nfs4_sequence_args *args,
1149 struct nfs4_sequence_res *res)
1150{
1151 return nfs4_do_call_sync(clnt, server, msg, args, res, 0);
1152}
1153
1154
Bryan Schumaker7c513052011-03-24 17:12:24 +00001155int nfs4_call_sync(struct rpc_clnt *clnt,
1156 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001157 struct rpc_message *msg,
1158 struct nfs4_sequence_args *args,
1159 struct nfs4_sequence_res *res,
1160 int cache_reply)
1161{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001162 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001163 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001164}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Trond Myklebust3c591172018-07-31 15:54:10 -04001166static void
1167nfs4_inc_nlink_locked(struct inode *inode)
1168{
1169 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1170 inc_nlink(inode);
1171}
1172
1173static void
1174nfs4_dec_nlink_locked(struct inode *inode)
1175{
1176 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1177 drop_nlink(inode);
1178}
1179
1180static void
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001181nfs4_update_changeattr_locked(struct inode *inode,
1182 struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001183 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001185 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Trond Myklebust16e14372018-03-20 16:53:31 -04001187 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1188 | NFS_INO_INVALID_MTIME
Trond Myklebust5636ec42018-07-31 15:54:11 -04001189 | cache_validity;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001190
1191 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(inode)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001192 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1193 nfsi->attrtimeo_timestamp = jiffies;
1194 } else {
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001195 if (S_ISDIR(inode->i_mode)) {
1196 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
1197 nfs_force_lookup_revalidate(inode);
1198 } else {
1199 if (!NFS_PROTO(inode)->have_delegation(inode,
1200 FMODE_READ))
1201 nfsi->cache_validity |= NFS_INO_REVAL_PAGECACHE;
1202 }
1203
1204 if (cinfo->before != inode_peek_iversion_raw(inode))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001205 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
Frank van der Linden0f44da52020-06-23 22:39:00 +00001206 NFS_INO_INVALID_ACL |
1207 NFS_INO_INVALID_XATTR;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001208 }
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001209 inode_set_iversion_raw(inode, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001210 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001211 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustc8d07152018-07-31 15:54:12 -04001212 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001213
1214 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
1215 nfs_fscache_invalidate(inode);
Trond Myklebust3c591172018-07-31 15:54:10 -04001216}
1217
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001218void
1219nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001220 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001221{
1222 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001223 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 spin_unlock(&dir->i_lock);
1225}
1226
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001227struct nfs4_open_createattrs {
1228 struct nfs4_label *label;
1229 struct iattr *sattr;
1230 const __u32 verf[2];
1231};
1232
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001233static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1234 int err, struct nfs4_exception *exception)
1235{
1236 if (err != -EINVAL)
1237 return false;
1238 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1239 return false;
1240 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1241 exception->retry = 1;
1242 return true;
1243}
1244
Trond Myklebust1bf85d82019-06-27 06:30:48 -04001245static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1246{
1247 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1248}
1249
1250static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1251{
1252 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1253
1254 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1255}
1256
Trond Myklebust6ae37332015-01-30 14:21:14 -05001257static u32
1258nfs4_map_atomic_open_share(struct nfs_server *server,
1259 fmode_t fmode, int openflags)
1260{
1261 u32 res = 0;
1262
1263 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1264 case FMODE_READ:
1265 res = NFS4_SHARE_ACCESS_READ;
1266 break;
1267 case FMODE_WRITE:
1268 res = NFS4_SHARE_ACCESS_WRITE;
1269 break;
1270 case FMODE_READ|FMODE_WRITE:
1271 res = NFS4_SHARE_ACCESS_BOTH;
1272 }
1273 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1274 goto out;
1275 /* Want no delegation if we're using O_DIRECT */
1276 if (openflags & O_DIRECT)
1277 res |= NFS4_SHARE_WANT_NO_DELEG;
1278out:
1279 return res;
1280}
1281
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001282static enum open_claim_type4
1283nfs4_map_atomic_open_claim(struct nfs_server *server,
1284 enum open_claim_type4 claim)
1285{
1286 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1287 return claim;
1288 switch (claim) {
1289 default:
1290 return claim;
1291 case NFS4_OPEN_CLAIM_FH:
1292 return NFS4_OPEN_CLAIM_NULL;
1293 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1294 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1295 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1296 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1297 }
1298}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001301{
1302 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001303 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001304 p->o_res.seqid = p->o_arg.seqid;
1305 p->c_res.seqid = p->c_arg.seqid;
1306 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001307 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001308 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001309 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001310}
1311
Al Viro82a2c1b2011-06-22 18:30:55 -04001312static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001313 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001314 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001315 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001316 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001317{
Al Viro82a2c1b2011-06-22 18:30:55 -04001318 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001319 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001320 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001321 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001322 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001323 struct nfs4_opendata *p;
1324
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001325 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001326 if (p == NULL)
1327 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001328
1329 p->f_label = nfs4_label_alloc(server, gfp_mask);
1330 if (IS_ERR(p->f_label))
1331 goto err_free_p;
1332
Kinglong Meea49c2692015-07-27 15:31:38 +08001333 p->a_label = nfs4_label_alloc(server, gfp_mask);
1334 if (IS_ERR(p->a_label))
1335 goto err_free_f;
1336
Trond Myklebust63f5f792015-01-23 19:19:25 -05001337 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1338 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001339 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001340 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001341 nfs_sb_active(dentry->d_sb);
1342 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001343 p->dir = parent;
1344 p->owner = sp;
1345 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001346 p->o_arg.open_flags = flags;
1347 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust536585c2016-11-10 15:40:34 -05001348 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001349 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1350 fmode, flags);
Benjamin Coddington90910512019-06-07 06:37:30 -04001351 if (flags & O_CREAT) {
1352 p->o_arg.umask = current_umask();
1353 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1354 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1355 p->o_arg.u.attrs = &p->attrs;
1356 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1357
1358 memcpy(p->o_arg.u.verifier.data, c->verf,
1359 sizeof(p->o_arg.u.verifier.data));
1360 }
1361 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001362 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1363 * will return permission denied for all bits until close */
1364 if (!(flags & O_EXCL)) {
1365 /* ask server to check for all possible rights as results
1366 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001367 switch (p->o_arg.claim) {
1368 default:
1369 break;
1370 case NFS4_OPEN_CLAIM_NULL:
1371 case NFS4_OPEN_CLAIM_FH:
1372 p->o_arg.access = NFS4_ACCESS_READ |
1373 NFS4_ACCESS_MODIFY |
1374 NFS4_ACCESS_EXTEND |
1375 NFS4_ACCESS_EXECUTE;
Frank van der Linden72832a22020-06-23 22:38:58 +00001376#ifdef CONFIG_NFS_V4_2
1377 if (server->caps & NFS_CAP_XATTR)
1378 p->o_arg.access |= NFS4_ACCESS_XAREAD |
1379 NFS4_ACCESS_XAWRITE |
1380 NFS4_ACCESS_XALIST;
1381#endif
Trond Myklebust536585c2016-11-10 15:40:34 -05001382 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001383 }
David Howells7539bba2006-08-22 20:06:09 -04001384 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001385 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1386 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001387 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001388 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001389 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001390 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001391 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001392 case NFS4_OPEN_CLAIM_NULL:
1393 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1394 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1395 p->o_arg.fh = NFS_FH(dir);
1396 break;
1397 case NFS4_OPEN_CLAIM_PREVIOUS:
1398 case NFS4_OPEN_CLAIM_FH:
1399 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1400 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001401 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001402 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001403 p->c_arg.fh = &p->o_res.fh;
1404 p->c_arg.stateid = &p->o_res.stateid;
1405 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001406 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001407 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001408 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001409
1410err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001411 nfs4_label_free(p->a_label);
1412err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001413 nfs4_label_free(p->f_label);
1414err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001415 kfree(p);
1416err:
1417 dput(parent);
1418 return NULL;
1419}
1420
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001421static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001422{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001423 struct nfs4_opendata *p = container_of(kref,
1424 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001425 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001426
Fred Isaman30ae2412016-10-18 13:39:51 -04001427 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001428 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001429 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001430 if (p->state != NULL)
1431 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001432 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001433
Kinglong Meea49c2692015-07-27 15:31:38 +08001434 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001435 nfs4_label_free(p->f_label);
1436
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001437 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001438 dput(p->dentry);
1439 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001440 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001441 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001442 kfree(p);
1443}
1444
1445static void nfs4_opendata_put(struct nfs4_opendata *p)
1446{
1447 if (p != NULL)
1448 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001449}
1450
Trond Myklebust24311f82015-09-20 10:50:17 -04001451static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1452 fmode_t fmode)
1453{
1454 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1455 case FMODE_READ|FMODE_WRITE:
1456 return state->n_rdwr != 0;
1457 case FMODE_WRITE:
1458 return state->n_wronly != 0;
1459 case FMODE_READ:
1460 return state->n_rdonly != 0;
1461 }
1462 WARN_ON_ONCE(1);
1463 return false;
1464}
1465
Trond Myklebustbe189f72018-09-27 17:12:33 -04001466static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1467 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001468{
1469 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001470
Trond Myklebust536e43d2012-01-17 22:04:26 -05001471 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001472 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001473 switch (claim) {
1474 case NFS4_OPEN_CLAIM_NULL:
1475 case NFS4_OPEN_CLAIM_FH:
1476 goto out;
1477 default:
1478 break;
1479 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001480 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001481 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001482 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1483 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001484 break;
1485 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001486 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1487 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001488 break;
1489 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001490 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1491 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001492 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001493out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001494 return ret;
1495}
1496
Trond Myklebust2a606182015-08-19 22:30:00 -05001497static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1498 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001499{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001500 if (delegation == NULL)
1501 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001502 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001503 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001504 switch (claim) {
1505 case NFS4_OPEN_CLAIM_NULL:
1506 case NFS4_OPEN_CLAIM_FH:
1507 break;
1508 case NFS4_OPEN_CLAIM_PREVIOUS:
1509 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1510 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001511 fallthrough;
Trond Myklebust2a606182015-08-19 22:30:00 -05001512 default:
1513 return 0;
1514 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001515 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001516 return 1;
1517}
1518
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001519static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001520{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001521 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001522 case FMODE_WRITE:
1523 state->n_wronly++;
1524 break;
1525 case FMODE_READ:
1526 state->n_rdonly++;
1527 break;
1528 case FMODE_READ|FMODE_WRITE:
1529 state->n_rdwr++;
1530 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001531 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001532}
1533
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001534#ifdef CONFIG_NFS_V4_1
1535static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1536{
1537 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1538 return true;
1539 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1540 return true;
1541 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1542 return true;
1543 return false;
1544}
1545#endif /* CONFIG_NFS_V4_1 */
1546
Trond Myklebustc9399f22017-11-06 15:28:01 -05001547static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1548{
1549 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1550 wake_up_all(&state->waitq);
1551}
1552
Trond Myklebust4f14c192014-02-12 19:15:06 -05001553static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001554{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001555 struct nfs_client *clp = state->owner->so_server->nfs_client;
1556 bool need_recover = false;
1557
1558 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1559 need_recover = true;
1560 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1561 need_recover = true;
1562 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1563 need_recover = true;
1564 if (need_recover)
1565 nfs4_state_mark_reclaim_nograce(clp, state);
1566}
1567
Trond Myklebustc9399f22017-11-06 15:28:01 -05001568/*
1569 * Check for whether or not the caller may update the open stateid
1570 * to the value passed in by stateid.
1571 *
1572 * Note: This function relies heavily on the server implementing
1573 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1574 * correctly.
1575 * i.e. The stateid seqids have to be initialised to 1, and
1576 * are then incremented on every state transition.
1577 */
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001578static bool nfs_stateid_is_sequential(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001579 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001580{
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001581 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1582 /* The common case - we're updating to a new sequence number */
1583 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1584 nfs4_stateid_is_next(&state->open_stateid, stateid)) {
1585 return true;
1586 }
1587 } else {
1588 /* This is the first OPEN in this generation */
Trond Myklebustc9399f22017-11-06 15:28:01 -05001589 if (stateid->seqid == cpu_to_be32(1))
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001590 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001591 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001592 return false;
1593}
1594
Trond Myklebustf95549c2015-01-23 18:06:09 -05001595static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1596{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001597 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1598 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001599 if (state->n_wronly)
1600 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1601 if (state->n_rdonly)
1602 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1603 if (state->n_rdwr)
1604 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001605 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001606}
1607
Trond Myklebust226056c2014-02-11 10:41:07 -05001608static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1609 nfs4_stateid *stateid, fmode_t fmode)
1610{
1611 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1612 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1613 case FMODE_WRITE:
1614 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1615 break;
1616 case FMODE_READ:
1617 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1618 break;
1619 case 0:
1620 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1621 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1622 clear_bit(NFS_OPEN_STATE, &state->flags);
1623 }
1624 if (stateid == NULL)
1625 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001626 /* Handle OPEN+OPEN_DOWNGRADE races */
1627 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1628 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001629 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001630 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001631 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001632 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001633 nfs4_stateid_copy(&state->stateid, stateid);
1634 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001635 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001636out:
1637 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001638}
1639
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001640static void nfs_clear_open_stateid(struct nfs4_state *state,
1641 nfs4_stateid *arg_stateid,
1642 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001643{
1644 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001645 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1646 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1647 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001648 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001649 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1650 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001651}
1652
Trond Myklebust1393d962016-09-22 13:39:13 -04001653static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001654 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste9acf212019-01-22 14:01:16 -05001655 __must_hold(&state->owner->so_lock)
1656 __must_hold(&state->seqlock)
1657 __must_hold(RCU)
1658
Trond Myklebust003707c2007-07-05 18:07:55 -04001659{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001660 DEFINE_WAIT(wait);
1661 int status = 0;
1662 for (;;) {
1663
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001664 if (nfs_stateid_is_sequential(state, stateid))
Trond Myklebust003707c2007-07-05 18:07:55 -04001665 break;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001666
Trond Myklebustc9399f22017-11-06 15:28:01 -05001667 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001668 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001669 /* Rely on seqids for serialisation with NFSv4.0 */
1670 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1671 break;
1672
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001673 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001674 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1675 /*
1676 * Ensure we process the state changes in the same order
1677 * in which the server processed them by delaying the
1678 * update of the stateid until we are in sequence.
1679 */
1680 write_sequnlock(&state->seqlock);
1681 spin_unlock(&state->owner->so_lock);
1682 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001683 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001684
Trond Myklebustc9399f22017-11-06 15:28:01 -05001685 if (!signal_pending(current)) {
1686 if (schedule_timeout(5*HZ) == 0)
1687 status = -EAGAIN;
1688 else
1689 status = 0;
1690 } else
1691 status = -EINTR;
1692 finish_wait(&state->waitq, &wait);
1693 rcu_read_lock();
1694 spin_lock(&state->owner->so_lock);
1695 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001696 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001697
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001698 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1699 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001700 nfs4_stateid_copy(freeme, &state->open_stateid);
1701 nfs_test_and_clear_all_open_stateid(state);
1702 }
1703
Trond Myklebuste999e802014-02-10 18:20:47 -05001704 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1705 nfs4_stateid_copy(&state->stateid, stateid);
1706 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001707 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001708 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001709}
1710
Trond Myklebustc9399f22017-11-06 15:28:01 -05001711static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001712 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001713 fmode_t fmode,
1714 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001716 /*
1717 * Protect the call to nfs4_state_set_mode_locked and
1718 * serialise the stateid update
1719 */
1720 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001721 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1722 switch (fmode) {
1723 case FMODE_READ:
1724 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1725 break;
1726 case FMODE_WRITE:
1727 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1728 break;
1729 case FMODE_READ|FMODE_WRITE:
1730 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001731 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001732 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001733 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001734}
1735
Trond Myklebust27a30cf2019-07-22 18:32:59 +01001736static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1737{
1738 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1739 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1740 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1741 clear_bit(NFS_OPEN_STATE, &state->flags);
1742}
1743
Trond Myklebustc9399f22017-11-06 15:28:01 -05001744static void nfs_state_set_delegation(struct nfs4_state *state,
1745 const nfs4_stateid *deleg_stateid,
1746 fmode_t fmode)
1747{
1748 /*
1749 * Protect the call to nfs4_state_set_mode_locked and
1750 * serialise the stateid update
1751 */
1752 write_seqlock(&state->seqlock);
1753 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1754 set_bit(NFS_DELEGATED_STATE, &state->flags);
1755 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001758static void nfs_state_clear_delegation(struct nfs4_state *state)
1759{
1760 write_seqlock(&state->seqlock);
1761 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1762 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1763 write_sequnlock(&state->seqlock);
1764}
1765
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04001766int update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001767 const nfs4_stateid *open_stateid,
1768 const nfs4_stateid *delegation,
1769 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001770{
Trond Myklebust1393d962016-09-22 13:39:13 -04001771 struct nfs_server *server = NFS_SERVER(state->inode);
1772 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001773 struct nfs_inode *nfsi = NFS_I(state->inode);
1774 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001775 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001776 int ret = 0;
1777
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001778 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001779
1780 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001781 spin_lock(&state->owner->so_lock);
1782 if (open_stateid != NULL) {
1783 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1784 ret = 1;
1785 }
1786
Trond Myklebust333ac782019-10-22 12:12:17 -04001787 deleg_cur = nfs4_get_valid_delegation(state->inode);
Trond Myklebust34310432008-12-23 15:21:38 -05001788 if (deleg_cur == NULL)
1789 goto no_delegation;
1790
1791 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001792 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001793 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001794 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001795 goto no_delegation_unlock;
1796
1797 if (delegation == NULL)
1798 delegation = &deleg_cur->stateid;
Trond Myklebust333ac782019-10-22 12:12:17 -04001799 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001800 goto no_delegation_unlock;
1801
Trond Myklebustb7391f42008-12-23 15:21:52 -05001802 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001803 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001804 ret = 1;
1805no_delegation_unlock:
1806 spin_unlock(&deleg_cur->lock);
1807no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001808 if (ret)
1809 update_open_stateflags(state, fmode);
1810 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001811 rcu_read_unlock();
1812
Trond Myklebust4f14c192014-02-12 19:15:06 -05001813 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001814 nfs4_schedule_state_manager(clp);
1815 if (freeme.type != 0)
1816 nfs4_test_and_free_stateid(server, &freeme,
1817 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001818
1819 return ret;
1820}
1821
Trond Myklebust39071e62015-01-24 15:07:56 -05001822static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1823 const nfs4_stateid *stateid)
1824{
1825 struct nfs4_state *state = lsp->ls_state;
1826 bool ret = false;
1827
1828 spin_lock(&state->state_lock);
1829 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1830 goto out_noupdate;
1831 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1832 goto out_noupdate;
1833 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1834 ret = true;
1835out_noupdate:
1836 spin_unlock(&state->state_lock);
1837 return ret;
1838}
Trond Myklebust34310432008-12-23 15:21:38 -05001839
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001840static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001841{
1842 struct nfs_delegation *delegation;
1843
Trond Myklebustf5086242018-03-20 16:43:13 -04001844 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001845 rcu_read_lock();
Trond Myklebust40e6aa12019-10-27 13:38:45 -04001846 delegation = nfs4_get_valid_delegation(inode);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001847 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001848 rcu_read_unlock();
1849 return;
1850 }
1851 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001852 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001853}
1854
Trond Myklebust6ee41262007-07-08 14:11:36 -04001855static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001856{
1857 struct nfs4_state *state = opendata->state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001858 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001859 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001860 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001861 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001862 nfs4_stateid stateid;
1863 int ret = -EAGAIN;
1864
Trond Myklebustaac00a82007-07-05 19:02:21 -04001865 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001866 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001867 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001868 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001869 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001870 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001871 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001872 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001873 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04001874 delegation = nfs4_get_valid_delegation(state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05001875 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001876 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001877 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001878 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001879 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001880 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001881 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001882 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001883 if (!opendata->is_recover) {
1884 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1885 if (ret != 0)
1886 goto out;
1887 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001888 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001889
1890 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001891 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001892 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001893 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001894out:
1895 return ERR_PTR(ret);
1896out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001897 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001898 return state;
1899}
1900
Andy Adamsone23008e2012-10-02 21:07:32 -04001901static void
1902nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1903{
1904 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1905 struct nfs_delegation *delegation;
1906 int delegation_flags = 0;
1907
1908 rcu_read_lock();
1909 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1910 if (delegation)
1911 delegation_flags = delegation->flags;
1912 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001913 switch (data->o_arg.claim) {
1914 default:
1915 break;
1916 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1917 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001918 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1919 "returning a delegation for "
1920 "OPEN(CLAIM_DELEGATE_CUR)\n",
1921 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001922 return;
1923 }
1924 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001925 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001926 data->owner->so_cred,
1927 data->o_res.delegation_type,
1928 &data->o_res.delegation,
1929 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001930 else
1931 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001932 data->owner->so_cred,
1933 data->o_res.delegation_type,
1934 &data->o_res.delegation,
1935 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001936
1937 if (data->o_res.do_recall)
1938 nfs_async_inode_return_delegation(state->inode,
1939 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001940}
1941
1942/*
1943 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1944 * and update the nfs4_state.
1945 */
1946static struct nfs4_state *
1947_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1948{
1949 struct inode *inode = data->state->inode;
1950 struct nfs4_state *state = data->state;
1951 int ret;
1952
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001953 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001954 if (data->rpc_status)
1955 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001956 /* cached opens have already been processed */
1957 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001958 }
1959
Andy Adamsone23008e2012-10-02 21:07:32 -04001960 ret = nfs_refresh_inode(inode, &data->f_attr);
1961 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001962 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001963
1964 if (data->o_res.delegation_type != 0)
1965 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001966update:
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01001967 if (!update_open_stateid(state, &data->o_res.stateid,
1968 NULL, data->o_arg.fmode))
1969 return ERR_PTR(-EAGAIN);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001970 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001971
1972 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001973}
1974
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001975static struct inode *
1976nfs4_opendata_get_inode(struct nfs4_opendata *data)
1977{
1978 struct inode *inode;
1979
1980 switch (data->o_arg.claim) {
1981 case NFS4_OPEN_CLAIM_NULL:
1982 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1983 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1984 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1985 return ERR_PTR(-EAGAIN);
1986 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1987 &data->f_attr, data->f_label);
1988 break;
1989 default:
1990 inode = d_inode(data->dentry);
1991 ihold(inode);
1992 nfs_refresh_inode(inode, &data->f_attr);
1993 }
1994 return inode;
1995}
1996
Andy Adamsone23008e2012-10-02 21:07:32 -04001997static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001998nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1999{
2000 struct nfs4_state *state;
2001 struct inode *inode;
2002
2003 inode = nfs4_opendata_get_inode(data);
2004 if (IS_ERR(inode))
2005 return ERR_CAST(inode);
2006 if (data->state != NULL && data->state->inode == inode) {
2007 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002008 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04002009 } else
2010 state = nfs4_get_open_state(inode, data->owner);
2011 iput(inode);
2012 if (state == NULL)
2013 state = ERR_PTR(-ENOMEM);
2014 return state;
2015}
2016
Andy Adamsone23008e2012-10-02 21:07:32 -04002017static struct nfs4_state *
2018_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002019{
Trond Myklebust75e8c482017-08-08 10:38:07 -04002020 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002021
Trond Myklebustaac00a82007-07-05 19:02:21 -04002022 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04002023 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002024 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002025 goto out;
2026 }
2027
Trond Myklebust75e8c482017-08-08 10:38:07 -04002028 state = nfs4_opendata_find_nfs4_state(data);
2029 if (IS_ERR(state))
2030 goto out;
2031
Andy Adamsone23008e2012-10-02 21:07:32 -04002032 if (data->o_res.delegation_type != 0)
2033 nfs4_opendata_check_deleg(data, state);
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01002034 if (!update_open_stateid(state, &data->o_res.stateid,
2035 NULL, data->o_arg.fmode)) {
2036 nfs4_put_open_state(state);
2037 state = ERR_PTR(-EAGAIN);
2038 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002039out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08002040 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002041 return state;
2042}
2043
Andy Adamsone23008e2012-10-02 21:07:32 -04002044static struct nfs4_state *
2045nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2046{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002047 struct nfs4_state *ret;
2048
Andy Adamsone23008e2012-10-02 21:07:32 -04002049 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002050 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2051 else
2052 ret = _nfs4_opendata_to_nfs4_state(data);
2053 nfs4_sequence_free_slot(&data->o_res.seq_res);
2054 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04002055}
2056
Trond Myklebust0de43972018-09-02 15:57:01 -04002057static struct nfs_open_context *
2058nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002059{
2060 struct nfs_inode *nfsi = NFS_I(state->inode);
2061 struct nfs_open_context *ctx;
2062
Trond Myklebust0de43972018-09-02 15:57:01 -04002063 rcu_read_lock();
2064 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01002065 if (ctx->state != state)
2066 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04002067 if ((ctx->mode & mode) != mode)
2068 continue;
2069 if (!get_nfs_open_context(ctx))
2070 continue;
2071 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002072 return ctx;
2073 }
Trond Myklebust0de43972018-09-02 15:57:01 -04002074 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002075 return ERR_PTR(-ENOENT);
2076}
2077
Trond Myklebust0de43972018-09-02 15:57:01 -04002078static struct nfs_open_context *
2079nfs4_state_find_open_context(struct nfs4_state *state)
2080{
2081 struct nfs_open_context *ctx;
2082
2083 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2084 if (!IS_ERR(ctx))
2085 return ctx;
2086 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2087 if (!IS_ERR(ctx))
2088 return ctx;
2089 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2090}
2091
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002092static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2093 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002094{
2095 struct nfs4_opendata *opendata;
2096
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002097 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002098 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002099 if (opendata == NULL)
2100 return ERR_PTR(-ENOMEM);
2101 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002102 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002103 return opendata;
2104}
2105
Trond Myklebust24311f82015-09-20 10:50:17 -04002106static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2107 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002108{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002109 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01002110 int ret;
2111
Trond Myklebust24311f82015-09-20 10:50:17 -04002112 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10002113 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002114 opendata->o_arg.open_flags = 0;
2115 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05002116 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2117 NFS_SB(opendata->dentry->d_sb),
2118 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002119 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2120 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2121 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002122 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002123 if (ret != 0)
2124 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002125 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002126 if (IS_ERR(newstate))
2127 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002128 if (newstate != opendata->state)
2129 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002130 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002131 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002132}
2133
2134static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2135{
Trond Myklebust864472e2006-01-03 09:55:15 +01002136 int ret;
2137
2138 /* memory barrier prior to reading state->n_* */
2139 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002140 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2141 if (ret != 0)
2142 return ret;
2143 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2144 if (ret != 0)
2145 return ret;
2146 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2147 if (ret != 0)
2148 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002149 /*
2150 * We may have performed cached opens for all three recoveries.
2151 * Check if we need to update the current stateid.
2152 */
2153 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002154 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002155 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002156 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002157 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002158 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002159 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002160 return 0;
2161}
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163/*
2164 * OPEN_RECLAIM:
2165 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002167static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002169 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002170 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002171 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 int status;
2173
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002174 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2175 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002176 if (IS_ERR(opendata))
2177 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002178 rcu_read_lock();
2179 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002180 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002181 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002182 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002183 opendata->o_arg.u.delegation_type = delegation_type;
2184 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002185 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 return status;
2187}
2188
Trond Myklebust539cd032007-06-05 11:46:42 -04002189static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
2191 struct nfs_server *server = NFS_SERVER(state->inode);
2192 struct nfs4_exception exception = { };
2193 int err;
2194 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002195 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002196 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002197 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2198 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002199 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002200 break;
2201 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 } while (exception.retry);
2203 return err;
2204}
2205
Trond Myklebust864472e2006-01-03 09:55:15 +01002206static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2207{
2208 struct nfs_open_context *ctx;
2209 int ret;
2210
2211 ctx = nfs4_state_find_open_context(state);
2212 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002213 return -EAGAIN;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002214 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2215 nfs_state_clear_open_state_flags(state);
Trond Myklebust539cd032007-06-05 11:46:42 -04002216 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002217 put_nfs_open_context(ctx);
2218 return ret;
2219}
2220
NeilBrowndce26302017-12-13 09:57:09 +11002221static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002223 switch (err) {
2224 default:
2225 printk(KERN_ERR "NFS: %s: unhandled error "
2226 "%d.\n", __func__, err);
2227 case 0:
2228 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002229 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002230 case -ESTALE:
Trond Myklebust67e7b522019-08-07 07:31:27 -04002231 case -ETIMEDOUT:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002232 break;
2233 case -NFS4ERR_BADSESSION:
2234 case -NFS4ERR_BADSLOT:
2235 case -NFS4ERR_BAD_HIGH_SLOT:
2236 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2237 case -NFS4ERR_DEADSESSION:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002238 return -EAGAIN;
2239 case -NFS4ERR_STALE_CLIENTID:
2240 case -NFS4ERR_STALE_STATEID:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002241 /* Don't recall a delegation if it was lost */
2242 nfs4_schedule_lease_recovery(server->nfs_client);
2243 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002244 case -NFS4ERR_MOVED:
2245 nfs4_schedule_migration_recovery(server);
2246 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002247 case -NFS4ERR_LEASE_MOVED:
2248 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2249 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002250 case -NFS4ERR_DELEG_REVOKED:
2251 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002252 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002253 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002254 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002255 nfs_inode_find_state_and_recover(state->inode,
2256 stateid);
2257 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002258 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002259 case -NFS4ERR_DELAY:
2260 case -NFS4ERR_GRACE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002261 ssleep(1);
2262 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002263 case -ENOMEM:
2264 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002265 if (fl) {
2266 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2267 if (lsp)
2268 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2269 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002270 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 return err;
2273}
2274
Trond Myklebust24311f82015-09-20 10:50:17 -04002275int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002276 struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002277{
2278 struct nfs_server *server = NFS_SERVER(state->inode);
2279 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002280 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002281
2282 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2283 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2284 if (IS_ERR(opendata))
2285 return PTR_ERR(opendata);
2286 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002287 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002288 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2289 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002290 goto out;
2291 }
2292 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002293 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2294 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002295 goto out;
Trond Myklebust24311f82015-09-20 10:50:17 -04002296 }
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002297 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2298 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2299 if (err)
2300 goto out;
2301 }
2302 nfs_state_clear_delegation(state);
2303out:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002304 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002305 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002306}
2307
Chuck Leverbe05c862013-08-09 12:49:47 -04002308static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2309{
2310 struct nfs4_opendata *data = calldata;
2311
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002312 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2313 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002314}
2315
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002316static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2317{
2318 struct nfs4_opendata *data = calldata;
2319
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002320 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002321
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002322 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002323 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002324 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002325 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002326 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002327 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002328 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002329}
2330
2331static void nfs4_open_confirm_release(void *calldata)
2332{
2333 struct nfs4_opendata *data = calldata;
2334 struct nfs4_state *state = NULL;
2335
2336 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002337 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002338 goto out_free;
2339 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002340 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002341 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002342 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002343 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002344 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002345out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002346 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002347}
2348
2349static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002350 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002351 .rpc_call_done = nfs4_open_confirm_done,
2352 .rpc_release = nfs4_open_confirm_release,
2353};
2354
2355/*
2356 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2357 */
2358static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2359{
David Howells2b0143b2015-03-17 22:25:59 +00002360 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002361 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002362 struct rpc_message msg = {
2363 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2364 .rpc_argp = &data->c_arg,
2365 .rpc_resp = &data->c_res,
2366 .rpc_cred = data->owner->so_cred,
2367 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002368 struct rpc_task_setup task_setup_data = {
2369 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002370 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002371 .callback_ops = &nfs4_open_confirm_ops,
2372 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002373 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002374 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002375 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 int status;
2377
Anna Schumakerfba83f32018-05-04 16:22:50 -04002378 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2379 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002380 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002381 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002382 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002383 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002384 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002385 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002386 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002387 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002388 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002389 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002390 smp_wmb();
2391 } else
2392 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002393 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 return status;
2395}
2396
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002397static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002399 struct nfs4_opendata *data = calldata;
2400 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002401 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002402 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002403
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002404 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002405 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002406 /*
2407 * Check if we still need to send an OPEN call, or if we can use
2408 * a delegation instead.
2409 */
2410 if (data->state != NULL) {
2411 struct nfs_delegation *delegation;
2412
Trond Myklebustbe189f72018-09-27 17:12:33 -04002413 if (can_open_cached(data->state, data->o_arg.fmode,
2414 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002415 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002416 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04002417 delegation = nfs4_get_valid_delegation(data->state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05002418 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002419 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002420 rcu_read_unlock();
2421 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002422 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002423 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002424 switch (claim) {
2425 default:
2426 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002427 case NFS4_OPEN_CLAIM_PREVIOUS:
2428 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2429 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002430 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002431 fallthrough;
Trond Myklebust8188df12013-04-23 14:31:19 -04002432 case NFS4_OPEN_CLAIM_FH:
2433 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002434 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002435 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002436 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002437 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002438 &data->o_res.seq_res,
2439 task) != 0)
2440 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002441
2442 /* Set the create mode (note dependency on the session type) */
2443 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2444 if (data->o_arg.open_flags & O_EXCL) {
2445 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2446 if (nfs4_has_persistent_session(clp))
2447 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2448 else if (clp->cl_mvops->minor_version > 0)
2449 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2450 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002451 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002452unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002453 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002454 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002455out_no_action:
2456 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002457out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002458 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002459}
2460
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002461static void nfs4_open_done(struct rpc_task *task, void *calldata)
2462{
2463 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002465 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002466
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002467 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002468 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002469
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002470 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002471 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2472 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002473 case S_IFREG:
2474 break;
2475 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002476 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002477 break;
2478 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002479 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002480 break;
2481 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002482 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002483 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002484 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002485 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002486 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2487 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002488 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002489 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002490}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002491
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002492static void nfs4_open_release(void *calldata)
2493{
2494 struct nfs4_opendata *data = calldata;
2495 struct nfs4_state *state = NULL;
2496
2497 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002498 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002499 goto out_free;
2500 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002501 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002502 goto out_free;
2503 /* In case we need an open_confirm, no cleanup! */
2504 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2505 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002506 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002507 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002508 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002509out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002510 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002511}
2512
2513static const struct rpc_call_ops nfs4_open_ops = {
2514 .rpc_call_prepare = nfs4_open_prepare,
2515 .rpc_call_done = nfs4_open_done,
2516 .rpc_release = nfs4_open_release,
2517};
2518
Fred Isaman3b65a302016-09-19 10:06:49 -04002519static int nfs4_run_open_task(struct nfs4_opendata *data,
2520 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002521{
David Howells2b0143b2015-03-17 22:25:59 +00002522 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002523 struct nfs_server *server = NFS_SERVER(dir);
2524 struct nfs_openargs *o_arg = &data->o_arg;
2525 struct nfs_openres *o_res = &data->o_res;
2526 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002527 struct rpc_message msg = {
2528 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2529 .rpc_argp = o_arg,
2530 .rpc_resp = o_res,
2531 .rpc_cred = data->owner->so_cred,
2532 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002533 struct rpc_task_setup task_setup_data = {
2534 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002535 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002536 .callback_ops = &nfs4_open_ops,
2537 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002538 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002539 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002540 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002541 int status;
2542
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002543 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002544 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002545 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002546 data->cancelled = false;
2547 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002548 if (!ctx) {
2549 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002550 data->is_recover = true;
Trond Myklebust67e7b522019-08-07 07:31:27 -04002551 task_setup_data.flags |= RPC_TASK_TIMEOUT;
Fred Isaman2409a972016-10-06 12:11:21 -04002552 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002553 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002554 pnfs_lgopen_prepare(data, ctx);
2555 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002556 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002557 if (IS_ERR(task))
2558 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002559 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002560 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002561 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002562 smp_wmb();
2563 } else
2564 status = data->rpc_status;
2565 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002566
2567 return status;
2568}
2569
2570static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2571{
David Howells2b0143b2015-03-17 22:25:59 +00002572 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002573 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002574 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002575
Fred Isaman3b65a302016-09-19 10:06:49 -04002576 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002577 if (status != 0 || !data->rpc_done)
2578 return status;
2579
Trond Myklebust6926afd2012-01-07 13:22:46 -05002580 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2581
Anna Schumakerd7e98252017-01-11 16:13:29 -05002582 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002583 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002584
2585 return status;
2586}
2587
Trond Myklebustf3792d62014-07-10 08:54:32 -04002588/*
2589 * Additional permission checks in order to distinguish between an
2590 * open for read, and an open for execute. This works around the
2591 * fact that NFSv4 OPEN treats read and execute permissions as being
2592 * the same.
2593 * Note that in the non-execute case, we want to turn off permission
2594 * checking if we just created a new file (POSIX open() semantics).
2595 */
NeilBrowna52458b2018-12-03 11:30:31 +11002596static int nfs4_opendata_access(const struct cred *cred,
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002597 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002598 struct nfs4_state *state, fmode_t fmode,
2599 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002600{
2601 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002602 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002603
2604 /* access call failed or for some reason the server doesn't
2605 * support any access modes -- defer access call until later */
2606 if (opendata->o_res.access_supported == 0)
2607 return 0;
2608
2609 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002610 /*
2611 * Use openflags to check for exec, because fmode won't
2612 * always have FMODE_EXEC set when file open for exec.
2613 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002614 if (openflags & __FMODE_EXEC) {
2615 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002616 if (S_ISDIR(state->inode->i_mode))
2617 mask = NFS4_ACCESS_LOOKUP;
2618 else
2619 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002620 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002621 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002622
2623 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002624 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2625 nfs_access_add_cache(state->inode, &cache);
2626
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002627 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2628 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002629 return 0;
2630
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002631 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002632}
2633
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002634/*
2635 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2636 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002637static int _nfs4_proc_open(struct nfs4_opendata *data,
2638 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002639{
David Howells2b0143b2015-03-17 22:25:59 +00002640 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002641 struct nfs_server *server = NFS_SERVER(dir);
2642 struct nfs_openargs *o_arg = &data->o_arg;
2643 struct nfs_openres *o_res = &data->o_res;
2644 int status;
2645
Fred Isaman3b65a302016-09-19 10:06:49 -04002646 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002647 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002648 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002649 if (status != 0) {
2650 if (status == -NFS4ERR_BADNAME &&
2651 !(o_arg->open_flags & O_CREAT))
2652 return -ENOENT;
2653 return status;
2654 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002655
Trond Myklebust6926afd2012-01-07 13:22:46 -05002656 nfs_fattr_map_and_free_names(server, &data->f_attr);
2657
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002658 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002659 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002660 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002661 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002662 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002663 if (data->file_created ||
2664 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00002665 nfs4_update_changeattr(dir, &o_res->cinfo,
2666 o_res->f_attr->time_start,
2667 NFS_INO_INVALID_DATA);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002668 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002669 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2670 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002672 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002674 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002676 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2677 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002678 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2679 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002680 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002681 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682}
2683
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684/*
2685 * OPEN_EXPIRED:
2686 * reclaim state on the server after a network partition.
2687 * Assumes caller holds the appropriate lock
2688 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002689static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002691 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002692 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002694 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002695 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002696 if (IS_ERR(opendata))
2697 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002698 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002699 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002700 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002701 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002702 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703}
2704
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002705static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002706{
Trond Myklebust539cd032007-06-05 11:46:42 -04002707 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002708 struct nfs4_exception exception = { };
2709 int err;
2710
2711 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002712 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002713 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002714 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2715 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002716 switch (err) {
2717 default:
2718 goto out;
2719 case -NFS4ERR_GRACE:
2720 case -NFS4ERR_DELAY:
2721 nfs4_handle_exception(server, err, &exception);
2722 err = 0;
2723 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002724 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002725out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002726 return err;
2727}
2728
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2730{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002732 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Trond Myklebust864472e2006-01-03 09:55:15 +01002734 ctx = nfs4_state_find_open_context(state);
2735 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002736 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002737 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002738 put_nfs_open_context(ctx);
2739 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740}
2741
Trond Myklebust41020b62016-09-22 13:38:58 -04002742static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2743 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002744{
Trond Myklebust41020b62016-09-22 13:38:58 -04002745 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002746 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002747}
2748
2749static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2750{
2751 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002752 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002753}
2754
2755static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2756{
2757 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2758 nfs40_clear_delegation_stateid(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002759 nfs_state_clear_open_state_flags(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002760 return nfs4_open_expired(sp, state);
2761}
2762
Trond Myklebust45870d62016-09-22 13:38:59 -04002763static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2764 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002765 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002766{
2767 return -NFS4ERR_BAD_STATEID;
2768}
2769
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002770#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002771static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2772 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002773 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002774{
2775 int status;
2776
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002777 switch (stateid->type) {
2778 default:
2779 break;
2780 case NFS4_INVALID_STATEID_TYPE:
2781 case NFS4_SPECIAL_STATEID_TYPE:
2782 return -NFS4ERR_BAD_STATEID;
2783 case NFS4_REVOKED_STATEID_TYPE:
2784 goto out_free;
2785 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002786
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002787 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002788 switch (status) {
2789 case -NFS4ERR_EXPIRED:
2790 case -NFS4ERR_ADMIN_REVOKED:
2791 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002792 break;
2793 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002794 return status;
2795 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002796out_free:
2797 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002798 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002799 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002800}
2801
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002802static int nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002803{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002804 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002805 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002806 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002807 const struct cred *cred = NULL;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002808 int status, ret = NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002809
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002810 /* Get the delegation credential for use by test/free_stateid */
2811 rcu_read_lock();
2812 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002813 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002814 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002815 nfs_state_clear_delegation(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002816 return NFS_OK;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002817 }
2818
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002819 spin_lock(&delegation->lock);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002820 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002821
Trond Myklebust994b15b2018-09-05 14:07:14 -04002822 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2823 &delegation->flags)) {
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002824 spin_unlock(&delegation->lock);
Trond Myklebust994b15b2018-09-05 14:07:14 -04002825 rcu_read_unlock();
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002826 return NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002827 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002828
NeilBrowna52458b2018-12-03 11:30:31 +11002829 if (delegation->cred)
2830 cred = get_cred(delegation->cred);
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002831 spin_unlock(&delegation->lock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002832 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002833 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002834 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002835 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002836 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002837 else
2838 ret = status;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002839
Trond Myklebust8c39a392019-07-19 13:48:44 -04002840 put_cred(cred);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002841 return ret;
2842}
2843
2844static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2845{
2846 nfs4_stateid tmp;
2847
2848 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2849 nfs4_copy_delegation_stateid(state->inode, state->state,
2850 &tmp, NULL) &&
2851 nfs4_stateid_match_other(&state->stateid, &tmp))
2852 nfs_state_set_delegation(state, &tmp, state->state);
2853 else
2854 nfs_state_clear_delegation(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002855}
2856
2857/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002858 * nfs41_check_expired_locks - possibly free a lock stateid
2859 *
2860 * @state: NFSv4 state for an inode
2861 *
2862 * Returns NFS_OK if recovery for this stateid is now finished.
2863 * Otherwise a negative NFS4ERR value is returned.
2864 */
2865static int nfs41_check_expired_locks(struct nfs4_state *state)
2866{
2867 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002868 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002869 struct nfs_server *server = NFS_SERVER(state->inode);
2870
2871 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2872 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002873
2874 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002875 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2876 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002877 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002878
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002879 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002880 spin_unlock(&state->state_lock);
2881
2882 nfs4_put_lock_state(prev);
2883 prev = lsp;
2884
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002885 status = nfs41_test_and_free_expired_stateid(server,
2886 &lsp->ls_stateid,
2887 cred);
2888 trace_nfs4_test_lock_stateid(state, lsp, status);
2889 if (status == -NFS4ERR_EXPIRED ||
2890 status == -NFS4ERR_BAD_STATEID) {
2891 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002892 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002893 if (!recover_lost_locks)
2894 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2895 } else if (status != NFS_OK) {
2896 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002897 nfs4_put_lock_state(prev);
2898 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002899 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002900 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002901 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002902 }
2903 spin_unlock(&state->state_lock);
2904 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002905out:
2906 return ret;
2907}
2908
2909/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002910 * nfs41_check_open_stateid - possibly free an open stateid
2911 *
2912 * @state: NFSv4 state for an inode
2913 *
2914 * Returns NFS_OK if recovery for this stateid is now finished.
2915 * Otherwise a negative NFS4ERR value is returned.
2916 */
2917static int nfs41_check_open_stateid(struct nfs4_state *state)
2918{
2919 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002920 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002921 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002922 int status;
2923
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002924 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002925 return -NFS4ERR_BAD_STATEID;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002926 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002927 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002928 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002929 nfs_state_clear_open_state_flags(state);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002930 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002931 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002932 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002933 if (nfs_open_stateid_recover_openmode(state))
2934 return -NFS4ERR_OPENMODE;
2935 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002936}
2937
2938static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2939{
Chuck Levereb64cf92012-07-11 16:30:05 -04002940 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002941
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002942 status = nfs41_check_delegation_stateid(state);
2943 if (status != NFS_OK)
2944 return status;
2945 nfs41_delegation_recover_stateid(state);
2946
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002947 status = nfs41_check_expired_locks(state);
2948 if (status != NFS_OK)
2949 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002950 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002951 if (status != NFS_OK)
2952 status = nfs4_open_expired(sp, state);
2953 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002954}
2955#endif
2956
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002958 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2959 * fields corresponding to attributes that were used to store the verifier.
2960 * Make sure we clobber those fields in the later setattr call
2961 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002962static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002963 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002964{
Trond Myklebust609339c2018-03-28 16:18:17 -04002965 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2966 __u32 attrset[3];
2967 unsigned ret;
2968 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002969
Trond Myklebust609339c2018-03-28 16:18:17 -04002970 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2971 attrset[i] = opendata->o_res.attrset[i];
2972 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2973 attrset[i] &= ~bitmask[i];
2974 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002975
Trond Myklebust609339c2018-03-28 16:18:17 -04002976 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2977 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002978
Trond Myklebust609339c2018-03-28 16:18:17 -04002979 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2980 if (sattr->ia_valid & ATTR_ATIME_SET)
2981 ret |= ATTR_ATIME_SET;
2982 else
2983 ret |= ATTR_ATIME;
2984 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002985
Trond Myklebust609339c2018-03-28 16:18:17 -04002986 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2987 if (sattr->ia_valid & ATTR_MTIME_SET)
2988 ret |= ATTR_MTIME_SET;
2989 else
2990 ret |= ATTR_MTIME;
2991 }
2992
2993 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002994 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002995 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002996}
2997
Trond Myklebustc21443c2013-02-07 14:26:21 -05002998static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
Trond Myklebust1bf85d82019-06-27 06:30:48 -04002999 int flags, struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003000{
3001 struct nfs4_state_owner *sp = opendata->owner;
3002 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04003003 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003004 struct nfs4_state *state;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003005 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003006 struct inode *dir = d_inode(opendata->dir);
3007 unsigned long dir_verifier;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003008 unsigned int seq;
3009 int ret;
3010
3011 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003012 dir_verifier = nfs_save_change_attribute(dir);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003013
Fred Isaman3b65a302016-09-19 10:06:49 -04003014 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03003015 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003016 goto out;
3017
Trond Myklebustae55e592018-05-22 11:17:16 -04003018 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003019 ret = PTR_ERR(state);
3020 if (IS_ERR(state))
3021 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05003022 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003023 if (server->caps & NFS_CAP_POSIX_LOCK)
3024 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04003025 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3026 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003027
Trond Myklebust275bb302013-05-29 13:11:28 -04003028 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00003029 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05003030 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003031 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003032 alias = d_exact_alias(dentry, state->inode);
3033 if (!alias)
3034 alias = d_splice_alias(igrab(state->inode), dentry);
3035 /* d_splice_alias() can't fail here - it's a non-directory */
3036 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04003037 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003038 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003039 }
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003040 }
3041
3042 switch(opendata->o_arg.claim) {
3043 default:
3044 break;
3045 case NFS4_OPEN_CLAIM_NULL:
3046 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3047 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3048 if (!opendata->rpc_done)
3049 break;
3050 if (opendata->o_res.delegation_type != 0)
3051 dir_verifier = nfs_save_change_attribute(dir);
3052 nfs_set_verifier(dentry, dir_verifier);
Trond Myklebust275bb302013-05-29 13:11:28 -04003053 }
3054
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04003055 /* Parse layoutget results before we check for access */
3056 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3057
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003058 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3059 acc_mode, flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003060 if (ret != 0)
3061 goto out;
3062
David Howells2b0143b2015-03-17 22:25:59 +00003063 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04003064 nfs_inode_attach_open_context(ctx);
3065 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3066 nfs4_schedule_stateid_recovery(server, state);
3067 }
Fred Isaman2409a972016-10-06 12:11:21 -04003068
Trond Myklebustc21443c2013-02-07 14:26:21 -05003069out:
Olga Kornievskaia0cb98ab2019-03-19 12:12:13 -04003070 if (!opendata->cancelled)
3071 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003072 return ret;
3073}
3074
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003075/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01003076 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 */
Andy Adamson82be4172012-05-23 05:02:35 -04003078static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003079 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003080 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003081 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003082 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083{
3084 struct nfs4_state_owner *sp;
3085 struct nfs4_state *state = NULL;
3086 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003087 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04003088 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11003089 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04003090 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003091 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003092 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003093 struct iattr *sattr = c->sattr;
3094 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04003095 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04003096 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
3098 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05003100 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3101 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3103 goto out_err;
3104 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05003105 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01003106 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003107 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00003108 if (d_really_is_positive(dentry))
3109 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01003110 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00003111 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003112 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003113 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3114 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003115 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05003116 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
David Quigley14c43f72013-05-22 12:50:43 -04003118 if (label) {
3119 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3120 if (IS_ERR(olabel)) {
3121 status = PTR_ERR(olabel);
3122 goto err_opendata_put;
3123 }
3124 }
3125
Trond Myklebuste911b812014-03-26 13:24:37 -07003126 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3127 if (!opendata->f_attr.mdsthreshold) {
3128 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3129 if (!opendata->f_attr.mdsthreshold)
3130 goto err_free_label;
3131 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04003132 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04003133 }
David Howells2b0143b2015-03-17 22:25:59 +00003134 if (d_really_is_positive(dentry))
3135 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04003136
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003137 status = _nfs4_open_and_get_state(opendata, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003138 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04003139 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003140 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003141
NeilBrownefcbc042015-07-30 13:00:56 +10003142 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003143 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003144 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003145 /*
3146 * send create attributes which was not set by open
3147 * with an extra setattr.
3148 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003149 if (attrs || label) {
3150 unsigned ia_old = sattr->ia_valid;
3151
3152 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003153 nfs_fattr_init(opendata->o_res.f_attr);
3154 status = nfs4_do_setattr(state->inode, cred,
3155 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003156 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003157 if (status == 0) {
3158 nfs_setattr_update_inode(state->inode, sattr,
3159 opendata->o_res.f_attr);
3160 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3161 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003162 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003163 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003164 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003165 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003166 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003167
Trond Myklebuste911b812014-03-26 13:24:37 -07003168 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003169 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003170 opendata->f_attr.mdsthreshold = NULL;
3171 }
Andy Adamson82be4172012-05-23 05:02:35 -04003172
David Quigley14c43f72013-05-22 12:50:43 -04003173 nfs4_label_free(olabel);
3174
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003175 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003178err_free_label:
3179 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003180err_opendata_put:
3181 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003182err_put_state_owner:
3183 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 return status;
3186}
3187
3188
Andy Adamson82be4172012-05-23 05:02:35 -04003189static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003190 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003191 int flags,
3192 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003193 struct nfs4_label *label,
3194 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003196 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04003197 struct nfs4_exception exception = {
3198 .interruptible = true,
3199 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003201 struct nfs4_open_createattrs c = {
3202 .label = label,
3203 .sattr = sattr,
3204 .verf = {
3205 [0] = (__u32)jiffies,
3206 [1] = (__u32)current->pid,
3207 },
3208 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 int status;
3210
3211 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003212 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003213 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003214 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 if (status == 0)
3216 break;
3217 /* NOTE: BAD_SEQID means the server and client disagree about the
3218 * book-keeping w.r.t. state-changing operations
3219 * (OPEN/CLOSE/LOCK/LOCKU...)
3220 * It is actually a sign of a bug on the client or on the server.
3221 *
3222 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003223 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 * have unhashed the old state_owner for us, and that we can
3225 * therefore safely retry using a new one. We should still warn
3226 * the user though...
3227 */
3228 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003229 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003230 " returned a bad sequence-id error!\n",
3231 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 exception.retry = 1;
3233 continue;
3234 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003235 /*
3236 * BAD_STATEID on OPEN means that the server cancelled our
3237 * state before it received the OPEN_CONFIRM.
3238 * Recover by retrying the request as per the discussion
3239 * on Page 181 of RFC3530.
3240 */
3241 if (status == -NFS4ERR_BAD_STATEID) {
3242 exception.retry = 1;
3243 continue;
3244 }
Robert Milkowski924491f2020-01-28 08:37:47 +00003245 if (status == -NFS4ERR_EXPIRED) {
3246 nfs4_schedule_lease_recovery(server->nfs_client);
3247 exception.retry = 1;
3248 continue;
3249 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003250 if (status == -EAGAIN) {
3251 /* We must have found a delegation */
3252 exception.retry = 1;
3253 continue;
3254 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003255 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3256 continue;
3257 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 status, &exception));
3259 } while (exception.retry);
3260 return res;
3261}
3262
Trond Myklebust8487c472016-06-26 08:44:35 -04003263static int _nfs4_do_setattr(struct inode *inode,
3264 struct nfs_setattrargs *arg,
3265 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003266 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003267 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003268{
3269 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003270 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3272 .rpc_argp = arg,
3273 .rpc_resp = res,
3274 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003275 };
NeilBrowna52458b2018-12-03 11:30:31 +11003276 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003277 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003278 bool truncate;
3279 int status;
3280
3281 nfs_fattr_init(res->fattr);
3282
3283 /* Servers should only apply open mode checks for file size changes */
3284 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Chuck Lever644c9f42020-09-04 17:39:12 -04003285 if (!truncate) {
3286 nfs4_inode_make_writeable(inode);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003287 goto zero_stateid;
Chuck Lever644c9f42020-09-04 17:39:12 -04003288 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003289
Trond Myklebust991eedb2018-04-09 11:15:30 -04003290 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003291 /* Use that stateid */
Trond Myklebust09a54f02019-08-03 10:28:18 -04003292 } else if (ctx != NULL && ctx->state) {
NeilBrown17393472016-10-13 15:26:47 +11003293 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003294 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003295 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003296 l_ctx = nfs_get_lock_context(ctx);
3297 if (IS_ERR(l_ctx))
3298 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003299 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3300 &arg->stateid, &delegation_cred);
3301 nfs_put_lock_context(l_ctx);
3302 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003303 return -EBADF;
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -05003304 else if (status == -EAGAIN)
3305 goto zero_stateid;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003306 } else {
3307zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003308 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003309 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003310 if (delegation_cred)
3311 msg.rpc_cred = delegation_cred;
3312
3313 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3314
NeilBrowna52458b2018-12-03 11:30:31 +11003315 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003316 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003317 renew_lease(server, timestamp);
3318 trace_nfs4_setattr(inode, &arg->stateid, status);
3319 return status;
3320}
3321
NeilBrowna52458b2018-12-03 11:30:31 +11003322static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003323 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003324 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003325 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003327 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003328 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003329 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003330 struct nfs_setattrargs arg = {
3331 .fh = NFS_FH(inode),
3332 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003334 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003335 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003336 };
3337 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003339 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003341 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003342 struct nfs4_exception exception = {
3343 .state = state,
3344 .inode = inode,
3345 .stateid = &arg.stateid,
3346 };
3347 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 do {
Trond Myklebust30846df2018-04-07 13:44:28 -04003350 nfs4_bitmap_copy_adjust_setattr(bitmask,
3351 nfs4_bitmask(server, olabel),
3352 inode);
3353
NeilBrown29b59f92016-10-13 15:26:47 +11003354 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003355 switch (err) {
3356 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003357 if (!(sattr->ia_valid & ATTR_SIZE)) {
3358 pr_warn_once("NFSv4: server %s is incorrectly "
3359 "applying open mode checks to "
3360 "a SETATTR that is not "
3361 "changing file size.\n",
3362 server->nfs_client->cl_hostname);
3363 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003364 if (state && !(state->state & FMODE_WRITE)) {
3365 err = -EBADF;
3366 if (sattr->ia_valid & ATTR_OPEN)
3367 err = -EACCES;
3368 goto out;
3369 }
3370 }
3371 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003373out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 return err;
3375}
3376
Peng Tao500d7012015-09-22 11:35:22 +08003377static bool
3378nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3379{
3380 if (inode == NULL || !nfs_have_layout(inode))
3381 return false;
3382
3383 return pnfs_wait_on_layoutreturn(inode, task);
3384}
3385
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003386/*
3387 * Update the seqid of an open stateid
3388 */
3389static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3390 struct nfs4_state *state)
3391{
3392 __be32 seqid_open;
3393 u32 dst_seqid;
3394 int seq;
3395
3396 for (;;) {
3397 if (!nfs4_valid_open_stateid(state))
3398 break;
3399 seq = read_seqbegin(&state->seqlock);
3400 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3401 nfs4_stateid_copy(dst, &state->open_stateid);
3402 if (read_seqretry(&state->seqlock, seq))
3403 continue;
3404 break;
3405 }
3406 seqid_open = state->open_stateid.seqid;
3407 if (read_seqretry(&state->seqlock, seq))
3408 continue;
3409
3410 dst_seqid = be32_to_cpu(dst->seqid);
3411 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3412 dst->seqid = seqid_open;
3413 break;
3414 }
3415}
3416
3417/*
3418 * Update the seqid of an open stateid after receiving
3419 * NFS4ERR_OLD_STATEID
3420 */
3421static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3422 struct nfs4_state *state)
3423{
3424 __be32 seqid_open;
3425 u32 dst_seqid;
3426 bool ret;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003427 int seq, status = -EAGAIN;
3428 DEFINE_WAIT(wait);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003429
3430 for (;;) {
3431 ret = false;
3432 if (!nfs4_valid_open_stateid(state))
3433 break;
3434 seq = read_seqbegin(&state->seqlock);
3435 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3436 if (read_seqretry(&state->seqlock, seq))
3437 continue;
3438 break;
3439 }
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003440
3441 write_seqlock(&state->seqlock);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003442 seqid_open = state->open_stateid.seqid;
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003443
3444 dst_seqid = be32_to_cpu(dst->seqid);
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003445
3446 /* Did another OPEN bump the state's seqid? try again: */
3447 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003448 dst->seqid = seqid_open;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003449 write_sequnlock(&state->seqlock);
3450 ret = true;
3451 break;
3452 }
3453
3454 /* server says we're behind but we haven't seen the update yet */
3455 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3456 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3457 write_sequnlock(&state->seqlock);
3458 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3459
3460 if (signal_pending(current))
3461 status = -EINTR;
3462 else
3463 if (schedule_timeout(5*HZ) != 0)
3464 status = 0;
3465
3466 finish_wait(&state->waitq, &wait);
3467
3468 if (!status)
3469 continue;
3470 if (status == -EINTR)
3471 break;
3472
3473 /* we slept the whole 5 seconds, we must have lost a seqid */
3474 dst->seqid = cpu_to_be32(dst_seqid + 1);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003475 ret = true;
3476 break;
3477 }
3478
3479 return ret;
3480}
3481
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482struct nfs4_closedata {
3483 struct inode *inode;
3484 struct nfs4_state *state;
3485 struct nfs_closeargs arg;
3486 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003487 struct {
3488 struct nfs4_layoutreturn_args arg;
3489 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003490 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003491 u32 roc_barrier;
3492 bool roc;
3493 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003494 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003495 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496};
3497
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003498static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003499{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003500 struct nfs4_closedata *calldata = data;
3501 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003502 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003503
Trond Myklebustcf805162016-11-15 14:56:07 -05003504 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003505 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3506 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003507 nfs4_put_open_state(calldata->state);
3508 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003509 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003510 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003511 kfree(calldata);
3512}
3513
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003514static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003516 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003519 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003520 struct nfs4_exception exception = {
3521 .state = state,
3522 .inode = calldata->inode,
3523 .stateid = &calldata->arg.stateid,
3524 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525
Chuck Levera3ca5652012-03-01 17:00:40 -05003526 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003527 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3528 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003529 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003530
3531 /* Handle Layoutreturn errors */
Trond Myklebust287a9c52019-09-20 07:23:41 -04003532 if (pnfs_roc_done(task, calldata->inode,
3533 &calldata->arg.lr_args,
3534 &calldata->res.lr_res,
3535 &calldata->res.lr_ret) == -EAGAIN)
3536 goto out_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003537
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003538 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 * the state_owner. we keep this around to process errors
3540 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 switch (task->tk_status) {
3542 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003543 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003544 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003545 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003546 case -NFS4ERR_ACCESS:
3547 if (calldata->arg.bitmask != NULL) {
3548 calldata->arg.bitmask = NULL;
3549 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003550 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003551
3552 }
3553 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003554 case -NFS4ERR_OLD_STATEID:
3555 /* Did we race with OPEN? */
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003556 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003557 state))
3558 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003559 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003560 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003562 case -NFS4ERR_EXPIRED:
3563 nfs4_free_revoked_stateid(server,
3564 &calldata->arg.stateid,
3565 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003566 fallthrough;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003567 case -NFS4ERR_BAD_STATEID:
Trond Myklebuste217e822019-09-20 07:23:46 -04003568 if (calldata->arg.fmode == 0)
3569 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003570 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003572 task->tk_status = nfs4_async_handle_exception(task,
3573 server, task->tk_status, &exception);
3574 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003575 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003577 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3578 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003579out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003580 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003581 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003582 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003583 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003584 return;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003585out_restart:
3586 task->tk_status = 0;
3587 rpc_restart_call_prepare(task);
3588 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589}
3590
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003591static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003593 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003594 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003595 struct inode *inode = calldata->inode;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003596 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003597 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003598 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003599
Chuck Levera3ca5652012-03-01 17:00:40 -05003600 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003601 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003602 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003603
Trond Myklebust88069f72009-12-08 08:33:16 -05003604 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003605 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003606 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3607 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3608 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003609 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003610 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003611 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003612 if (state->n_rdonly == 0)
3613 call_close |= is_rdonly;
3614 else if (is_rdonly)
3615 calldata->arg.fmode |= FMODE_READ;
3616 if (state->n_wronly == 0)
3617 call_close |= is_wronly;
3618 else if (is_wronly)
3619 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003620 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3621 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003622 } else if (is_rdwr)
3623 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3624
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003625 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3626 if (!nfs4_valid_open_stateid(state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003627 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003628 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003629
3630 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003631 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003632 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003633 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003634
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003635 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003636 nfs_release_seqid(calldata->arg.seqid);
3637 goto out_wait;
3638 }
3639
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003640 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3641 if (lo && !pnfs_layout_is_valid(lo)) {
3642 calldata->arg.lr_args = NULL;
3643 calldata->res.lr_res = NULL;
3644 }
3645
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003646 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003647 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003648
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003649 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003650 /* Close-to-open cache consistency revalidation */
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04003651 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003652 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04003653 nfs4_bitmask_adjust(calldata->arg.bitmask, inode, NFS_SERVER(inode), NULL);
3654 } else
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003655 calldata->arg.bitmask = NULL;
3656 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003657
Trond Myklebust6ae37332015-01-30 14:21:14 -05003658 calldata->arg.share_access =
3659 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3660 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003661
Trond Myklebustd8d84982016-12-19 12:14:44 -05003662 if (calldata->res.fattr == NULL)
3663 calldata->arg.bitmask = NULL;
3664 else if (calldata->arg.bitmask == NULL)
3665 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003666 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003667 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003668 &calldata->arg.seq_args,
3669 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003670 task) != 0)
3671 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003672 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003673 return;
3674out_no_action:
3675 task->tk_action = NULL;
3676out_wait:
3677 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678}
3679
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003680static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003681 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003682 .rpc_call_done = nfs4_close_done,
3683 .rpc_release = nfs4_free_closedata,
3684};
3685
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686/*
3687 * It is possible for data to be read/written from a mem-mapped file
3688 * after the sys_close call (which hits the vfs layer as a flush).
3689 * This means that we can't safely call nfsv4 close on a file until
3690 * the inode is cleared. This in turn means that we are not good
3691 * NFSv4 citizens - we do not indicate to the server to update the file's
3692 * share state even when we are done with one of the three share
3693 * stateid's in the inode.
3694 *
3695 * NOTE: Caller must be holding the sp->so_owner semaphore!
3696 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003697int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003699 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003700 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003702 struct nfs4_state_owner *sp = state->owner;
3703 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003704 struct rpc_message msg = {
3705 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3706 .rpc_cred = state->owner->so_cred,
3707 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003708 struct rpc_task_setup task_setup_data = {
3709 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003710 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003711 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003712 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05003713 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003714 };
Trond Myklebust95121352005-10-18 14:20:12 -07003715 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003717 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3718 &task_setup_data.rpc_client, &msg);
3719
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003720 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003722 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003723 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003724 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003726 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003727 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3728 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003730 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3731 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003732 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003733 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003734 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003735 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003736 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003737 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003738 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003739 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003740 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003741 calldata->lr.roc = pnfs_roc(state->inode,
3742 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3743 if (calldata->lr.roc) {
3744 calldata->arg.lr_args = &calldata->lr.arg;
3745 calldata->res.lr_res = &calldata->lr.res;
3746 }
Al Viro643168c2011-06-22 18:20:23 -04003747 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003748
Trond Myklebust1174dd12010-12-21 10:52:24 -05003749 msg.rpc_argp = &calldata->arg;
3750 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003751 task_setup_data.callback_data = calldata;
3752 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003753 if (IS_ERR(task))
3754 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003755 status = 0;
3756 if (wait)
3757 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003758 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003759 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003760out_free_calldata:
3761 kfree(calldata);
3762out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003763 nfs4_put_open_state(state);
3764 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003765 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766}
3767
Trond Myklebust2b484292010-09-17 10:56:51 -04003768static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003769nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3770 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003773 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3774
3775 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
Trond Myklebust565277f2007-10-15 18:17:53 -04003777 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003778 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003779
3780 nfs4_label_release_security(label);
3781
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003782 if (IS_ERR(state))
3783 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003784 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785}
3786
Trond Myklebust1185a552009-12-03 15:54:02 -05003787static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003788{
3789 if (ctx->state == NULL)
3790 return;
3791 if (is_sync)
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003792 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003793 else
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003794 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003795}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796
Trond Myklebustb944dba2013-11-04 15:20:20 -05003797#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3798#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Frank van der Lindenb78ef842020-06-23 22:38:55 +00003799#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003800
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3802{
Kinglong Mee8c612822015-08-26 21:12:58 +08003803 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003804 struct nfs4_server_caps_arg args = {
3805 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003806 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003807 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 struct nfs4_server_caps_res res = {};
3809 struct rpc_message msg = {
3810 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003811 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 .rpc_resp = &res,
3813 };
3814 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003815 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816
Kinglong Mee8c612822015-08-26 21:12:58 +08003817 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3818 FATTR4_WORD0_FH_EXPIRE_TYPE |
3819 FATTR4_WORD0_LINK_SUPPORT |
3820 FATTR4_WORD0_SYMLINK_SUPPORT |
3821 FATTR4_WORD0_ACLSUPPORT;
3822 if (minorversion)
3823 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3824
Bryan Schumaker7c513052011-03-24 17:12:24 +00003825 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003827 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003828 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003829 case 0:
3830 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3831 res.attr_bitmask[2] = 0;
3832 break;
3833 case 1:
3834 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3835 break;
3836 case 2:
3837 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003840 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3841 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3842 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3843 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003844 NFS_CAP_CTIME|NFS_CAP_MTIME|
3845 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003846 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3847 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 server->caps |= NFS_CAP_ACLS;
3849 if (res.has_links != 0)
3850 server->caps |= NFS_CAP_HARDLINKS;
3851 if (res.has_symlinks != 0)
3852 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003853 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3854 server->caps |= NFS_CAP_FILEID;
3855 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3856 server->caps |= NFS_CAP_MODE;
3857 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3858 server->caps |= NFS_CAP_NLINK;
3859 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3860 server->caps |= NFS_CAP_OWNER;
3861 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3862 server->caps |= NFS_CAP_OWNER_GROUP;
3863 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3864 server->caps |= NFS_CAP_ATIME;
3865 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3866 server->caps |= NFS_CAP_CTIME;
3867 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3868 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003869#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3870 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3871 server->caps |= NFS_CAP_SECURITY_LABEL;
3872#endif
3873 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3874 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003875 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003876
Trond Myklebusta65318b2009-03-11 14:10:28 -04003877 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3878 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3879 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003880 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003881
3882 /* Avoid a regression due to buggy server */
3883 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3884 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003885 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3886 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003887
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003889 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003891
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 return status;
3893}
3894
Trond Myklebust55a97592006-06-09 09:34:19 -04003895int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896{
Trond Myklebust0688e642019-04-07 13:59:09 -04003897 struct nfs4_exception exception = {
3898 .interruptible = true,
3899 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 int err;
3901 do {
3902 err = nfs4_handle_exception(server,
3903 _nfs4_server_capabilities(server, fhandle),
3904 &exception);
3905 } while (exception.retry);
3906 return err;
3907}
3908
3909static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3910 struct nfs_fsinfo *info)
3911{
David Quigleyaa9c2662013-05-22 12:50:44 -04003912 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003914 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 };
3916 struct nfs4_lookup_res res = {
3917 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003918 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 .fh = fhandle,
3920 };
3921 struct rpc_message msg = {
3922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3923 .rpc_argp = &args,
3924 .rpc_resp = &res,
3925 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003926
David Quigleyaa9c2662013-05-22 12:50:44 -04003927 bitmask[0] = nfs4_fattr_bitmap[0];
3928 bitmask[1] = nfs4_fattr_bitmap[1];
3929 /*
3930 * Process the label in the upcoming getfattr
3931 */
3932 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3933
Trond Myklebust0e574af2005-10-27 22:12:38 -04003934 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003935 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936}
3937
3938static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3939 struct nfs_fsinfo *info)
3940{
Trond Myklebust0688e642019-04-07 13:59:09 -04003941 struct nfs4_exception exception = {
3942 .interruptible = true,
3943 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 int err;
3945 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003946 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003947 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003948 switch (err) {
3949 case 0:
3950 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003951 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003952 default:
3953 err = nfs4_handle_exception(server, err, &exception);
3954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003956out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 return err;
3958}
3959
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003960static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3961 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3962{
Trond Myklebustc2190662013-08-26 19:23:04 -04003963 struct rpc_auth_create_args auth_args = {
3964 .pseudoflavor = flavor,
3965 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003966 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003967
Trond Myklebustc2190662013-08-26 19:23:04 -04003968 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003969 if (IS_ERR(auth))
3970 return -EACCES;
3971 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003972}
3973
Chuck Lever9a744ba2013-03-16 15:56:02 -04003974/*
3975 * Retry pseudoroot lookup with various security flavors. We do this when:
3976 *
3977 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3978 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3979 *
3980 * Returns zero on success, or a negative NFS4ERR value, or a
3981 * negative errno value.
3982 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003983static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003984 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003986 /* Per 3530bis 15.33.5 */
3987 static const rpc_authflavor_t flav_array[] = {
3988 RPC_AUTH_GSS_KRB5P,
3989 RPC_AUTH_GSS_KRB5I,
3990 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003991 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003992 RPC_AUTH_NULL,
3993 };
3994 int status = -EPERM;
3995 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003997 if (server->auth_info.flavor_len > 0) {
3998 /* try each flavor specified by user */
3999 for (i = 0; i < server->auth_info.flavor_len; i++) {
4000 status = nfs4_lookup_root_sec(server, fhandle, info,
4001 server->auth_info.flavors[i]);
4002 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4003 continue;
4004 break;
4005 }
4006 } else {
4007 /* no flavors specified by user, try default list */
4008 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4009 status = nfs4_lookup_root_sec(server, fhandle, info,
4010 flav_array[i]);
4011 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4012 continue;
4013 break;
4014 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004015 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04004016
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004017 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01004018 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004019 * to access the mount. It could also mean that we tried to mount
4020 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4021 * existing mount programs don't handle -EACCES very well so it should
4022 * be mapped to -EPERM instead.
4023 */
4024 if (status == -EACCES)
4025 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004026 return status;
4027}
4028
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004029/**
4030 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4031 * @server: initialized nfs_server handle
4032 * @fhandle: we fill in the pseudo-fs root file handle
4033 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004034 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004035 *
4036 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004037 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04004038int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004039 struct nfs_fsinfo *info,
4040 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004041{
Andre Przywarac7757072015-04-23 17:17:40 +01004042 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004043
Andre Przywarac7757072015-04-23 17:17:40 +01004044 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004045 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01004046
4047 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04004048 status = server->nfs_client->cl_mvops->find_root_sec(server,
4049 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004050
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 if (status == 0)
4052 status = nfs4_server_capabilities(server, fhandle);
4053 if (status == 0)
4054 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004055
Trond Myklebustc12e87f2006-03-13 21:20:47 -08004056 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057}
4058
Bryan Schumakerbae36242012-05-10 15:07:31 -04004059static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4060 struct nfs_fsinfo *info)
4061{
4062 int error;
4063 struct nfs_fattr *fattr = info->fattr;
Scott Mayhew779df6a2020-03-03 17:58:37 -05004064 struct nfs4_label *label = fattr->label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004065
4066 error = nfs4_server_capabilities(server, mntfh);
4067 if (error < 0) {
4068 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4069 return error;
4070 }
4071
Trond Myklebusta841b542018-04-07 13:50:59 -04004072 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04004073 if (error < 0) {
4074 dprintk("nfs4_get_root: getattr error = %d\n", -error);
Scott Mayhew779df6a2020-03-03 17:58:37 -05004075 goto out;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004076 }
4077
4078 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4079 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4080 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4081
Scott Mayhew779df6a2020-03-03 17:58:37 -05004082out:
Bryan Schumakerbae36242012-05-10 15:07:31 -04004083 return error;
4084}
4085
Manoj Naik6b97fd32006-06-09 09:34:29 -04004086/*
4087 * Get locations and (maybe) other attributes of a referral.
4088 * Note that we'll actually follow the referral later when
4089 * we detect fsid mismatch in inode revalidation
4090 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004091static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4092 const struct qstr *name, struct nfs_fattr *fattr,
4093 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04004094{
4095 int status = -ENOMEM;
4096 struct page *page = NULL;
4097 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004098
4099 page = alloc_page(GFP_KERNEL);
4100 if (page == NULL)
4101 goto out;
4102 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4103 if (locations == NULL)
4104 goto out;
4105
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004106 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004107 if (status != 0)
4108 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04004109
4110 /*
4111 * If the fsid didn't change, this is a migration event, not a
4112 * referral. Cause us to drop into the exception handler, which
4113 * will kick off migration recovery.
4114 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004115 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04004116 dprintk("%s: server did not return a different fsid for"
4117 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04004118 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004119 goto out;
4120 }
Andy Adamson533eb462011-06-13 18:25:56 -04004121 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4122 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004123
Andy Adamson533eb462011-06-13 18:25:56 -04004124 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004125 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04004126 memset(fhandle, 0, sizeof(struct nfs_fh));
4127out:
4128 if (page)
4129 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04004130 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004131 return status;
4132}
4133
David Quigley1775fd32013-05-22 12:50:42 -04004134static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004135 struct nfs_fattr *fattr, struct nfs4_label *label,
4136 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137{
Trond Myklebust771734f2018-04-07 13:54:23 -04004138 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 struct nfs4_getattr_arg args = {
4140 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04004141 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 };
4143 struct nfs4_getattr_res res = {
4144 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04004145 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 .server = server,
4147 };
4148 struct rpc_message msg = {
4149 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4150 .rpc_argp = &args,
4151 .rpc_resp = &res,
4152 };
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004153 unsigned short task_flags = 0;
4154
4155 /* Is this is an attribute revalidation, subject to softreval? */
4156 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4157 task_flags |= RPC_TASK_TIMEOUT;
David Quigleyaa9c2662013-05-22 12:50:44 -04004158
Trond Myklebust771734f2018-04-07 13:54:23 -04004159 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04004160
Trond Myklebust0e574af2005-10-27 22:12:38 -04004161 nfs_fattr_init(fattr);
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004162 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4163 return nfs4_do_call_sync(server->client, server, &msg,
4164 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165}
4166
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04004167int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004168 struct nfs_fattr *fattr, struct nfs4_label *label,
4169 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170{
Trond Myklebust0688e642019-04-07 13:59:09 -04004171 struct nfs4_exception exception = {
4172 .interruptible = true,
4173 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 int err;
4175 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04004176 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004177 trace_nfs4_getattr(server, fhandle, fattr, err);
4178 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 &exception);
4180 } while (exception.retry);
4181 return err;
4182}
4183
4184/*
4185 * The file is not closed if it is opened due to the a request to change
4186 * the size of the file. The open call will not be needed once the
4187 * VFS layer lookup-intents are implemented.
4188 *
4189 * Close is called when the inode is destroyed.
4190 * If we haven't opened the file for O_WRONLY, we
4191 * need to in the size_change case to obtain a stateid.
4192 *
4193 * Got race?
4194 * Because OPEN is always done by name in nfsv4, it is
4195 * possible that we opened a different file by the same
4196 * name. We can recognize this race condition, but we
4197 * can't do anything about it besides returning an error.
4198 *
4199 * This will be fixed with VFS changes (lookup-intent).
4200 */
4201static int
4202nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4203 struct iattr *sattr)
4204{
David Howells2b0143b2015-03-17 22:25:59 +00004205 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11004206 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11004207 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04004208 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 int status;
4210
Peng Tao88ac8152014-09-12 11:04:10 +08004211 if (pnfs_ld_layoutret_on_setattr(inode) &&
4212 sattr->ia_valid & ATTR_SIZE &&
4213 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04004214 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04004215
Trond Myklebust0e574af2005-10-27 22:12:38 -04004216 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217
Andy Adamson26699402012-05-30 16:12:24 -04004218 /* Deal with open(O_TRUNC) */
4219 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03004220 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04004221
4222 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03004223 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04004224 return 0;
4225
Trond Myklebustd5308382005-11-04 15:33:38 -05004226 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004227 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00004228
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004229 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11004230 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11004231 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004232 }
4233
David Quigley14c43f72013-05-22 12:50:43 -04004234 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4235 if (IS_ERR(label))
4236 return PTR_ERR(label);
4237
Trond Myklebust199366f2018-03-20 16:43:18 -04004238 /* Return any delegations if we're going to change ACLs */
4239 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004240 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004241
NeilBrown29b59f92016-10-13 15:26:47 +11004242 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004243 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004244 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004245 nfs_setsecurity(inode, fattr, label);
4246 }
David Quigley14c43f72013-05-22 12:50:43 -04004247 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 return status;
4249}
4250
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004251static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004252 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004253 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04004254{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004255 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004256 int status;
4257 struct nfs4_lookup_arg args = {
4258 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004259 .dir_fh = NFS_FH(dir),
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004260 .name = &dentry->d_name,
David Howells2b3de442006-08-22 20:06:09 -04004261 };
4262 struct nfs4_lookup_res res = {
4263 .server = server,
4264 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004265 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004266 .fh = fhandle,
4267 };
4268 struct rpc_message msg = {
4269 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4270 .rpc_argp = &args,
4271 .rpc_resp = &res,
4272 };
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004273 unsigned short task_flags = 0;
4274
4275 /* Is this is an attribute revalidation, subject to softreval? */
4276 if (nfs_lookup_is_soft_revalidate(dentry))
4277 task_flags |= RPC_TASK_TIMEOUT;
David Howells2b3de442006-08-22 20:06:09 -04004278
David Quigleyaa9c2662013-05-22 12:50:44 -04004279 args.bitmask = nfs4_bitmask(server, label);
4280
David Howells2b3de442006-08-22 20:06:09 -04004281 nfs_fattr_init(fattr);
4282
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004283 dprintk("NFS call lookup %pd2\n", dentry);
4284 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4285 status = nfs4_do_call_sync(clnt, server, &msg,
4286 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 dprintk("NFS reply lookup: %d\n", status);
4288 return status;
4289}
4290
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004291static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004292{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004293 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004294 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004295 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4296 fattr->nlink = 2;
4297}
4298
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004299static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004300 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004301 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004302{
Trond Myklebust0688e642019-04-07 13:59:09 -04004303 struct nfs4_exception exception = {
4304 .interruptible = true,
4305 };
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004306 struct rpc_clnt *client = *clnt;
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004307 const struct qstr *name = &dentry->d_name;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004308 int err;
4309 do {
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004310 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004311 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004312 switch (err) {
4313 case -NFS4ERR_BADNAME:
4314 err = -ENOENT;
4315 goto out;
4316 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004317 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004318 if (err == -NFS4ERR_MOVED)
4319 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004320 goto out;
4321 case -NFS4ERR_WRONGSEC:
4322 err = -EPERM;
4323 if (client != *clnt)
4324 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004325 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004326 if (IS_ERR(client))
4327 return PTR_ERR(client);
4328
4329 exception.retry = 1;
4330 break;
4331 default:
4332 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4333 }
4334 } while (exception.retry);
4335
4336out:
4337 if (err == 0)
4338 *clnt = client;
4339 else if (client != *clnt)
4340 rpc_shutdown_client(client);
4341
4342 return err;
4343}
4344
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004345static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004346 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4347 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004349 int status;
4350 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004351
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004352 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004353 if (client != NFS_CLIENT(dir)) {
4354 rpc_shutdown_client(client);
4355 nfs_fixup_secinfo_attributes(fattr);
4356 }
4357 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358}
4359
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004360struct rpc_clnt *
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004361nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004362 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4363{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004364 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004365 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004366
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004367 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004368 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004369 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004370 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004371}
4372
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004373static int _nfs4_proc_lookupp(struct inode *inode,
4374 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4375 struct nfs4_label *label)
4376{
4377 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4378 struct nfs_server *server = NFS_SERVER(inode);
4379 int status;
4380 struct nfs4_lookupp_arg args = {
4381 .bitmask = server->attr_bitmask,
4382 .fh = NFS_FH(inode),
4383 };
4384 struct nfs4_lookupp_res res = {
4385 .server = server,
4386 .fattr = fattr,
4387 .label = label,
4388 .fh = fhandle,
4389 };
4390 struct rpc_message msg = {
4391 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4392 .rpc_argp = &args,
4393 .rpc_resp = &res,
4394 };
4395
4396 args.bitmask = nfs4_bitmask(server, label);
4397
4398 nfs_fattr_init(fattr);
4399
4400 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4401 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4402 &res.seq_res, 0);
4403 dprintk("NFS reply lookupp: %d\n", status);
4404 return status;
4405}
4406
4407static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4408 struct nfs_fattr *fattr, struct nfs4_label *label)
4409{
Trond Myklebust0688e642019-04-07 13:59:09 -04004410 struct nfs4_exception exception = {
4411 .interruptible = true,
4412 };
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004413 int err;
4414 do {
4415 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4416 trace_nfs4_lookupp(inode, err);
4417 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4418 &exception);
4419 } while (exception.retry);
4420 return err;
4421}
4422
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4424{
Trond Myklebust76b32992007-08-10 17:45:11 -04004425 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 struct nfs4_accessargs args = {
4427 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004428 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004430 struct nfs4_accessres res = {
4431 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004432 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 struct rpc_message msg = {
4434 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4435 .rpc_argp = &args,
4436 .rpc_resp = &res,
4437 .rpc_cred = entry->cred,
4438 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004439 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440
Trond Myklebust7c672652018-06-04 15:00:53 -04004441 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004442 res.fattr = nfs_alloc_fattr();
4443 if (res.fattr == NULL)
4444 return -ENOMEM;
4445 args.bitmask = server->cache_consistency_bitmask;
4446 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004447 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004449 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004450 if (res.fattr)
4451 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004453 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 return status;
4455}
4456
4457static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4458{
Trond Myklebust0688e642019-04-07 13:59:09 -04004459 struct nfs4_exception exception = {
4460 .interruptible = true,
4461 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 int err;
4463 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004464 err = _nfs4_proc_access(inode, entry);
4465 trace_nfs4_access(inode, err);
4466 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 &exception);
4468 } while (exception.retry);
4469 return err;
4470}
4471
4472/*
4473 * TODO: For the time being, we don't try to get any attributes
4474 * along with any of the zero-copy operations READ, READDIR,
4475 * READLINK, WRITE.
4476 *
4477 * In the case of the first three, we want to put the GETATTR
4478 * after the read-type operation -- this is because it is hard
4479 * to predict the length of a GETATTR response in v4, and thus
4480 * align the READ data correctly. This means that the GETATTR
4481 * may end up partially falling into the page cache, and we should
4482 * shift it into the 'tail' of the xdr_buf before processing.
4483 * To do this efficiently, we need to know the total length
4484 * of data received, which doesn't seem to be available outside
4485 * of the RPC layer.
4486 *
4487 * In the case of WRITE, we also want to put the GETATTR after
4488 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004489 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 *
4491 * Both of these changes to the XDR layer would in fact be quite
4492 * minor, but I decided to leave them for a subsequent patch.
4493 */
4494static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4495 unsigned int pgbase, unsigned int pglen)
4496{
4497 struct nfs4_readlink args = {
4498 .fh = NFS_FH(inode),
4499 .pgbase = pgbase,
4500 .pglen = pglen,
4501 .pages = &page,
4502 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004503 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 struct rpc_message msg = {
4505 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4506 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004507 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 };
4509
Bryan Schumaker7c513052011-03-24 17:12:24 +00004510 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511}
4512
4513static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4514 unsigned int pgbase, unsigned int pglen)
4515{
Trond Myklebust0688e642019-04-07 13:59:09 -04004516 struct nfs4_exception exception = {
4517 .interruptible = true,
4518 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 int err;
4520 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004521 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4522 trace_nfs4_readlink(inode, err);
4523 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 &exception);
4525 } while (exception.retry);
4526 return err;
4527}
4528
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004530 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532static int
4533nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004534 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004536 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004537 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004538 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 int status = 0;
4541
NeilBrown532d4de2016-10-13 15:26:47 +11004542 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004543 if (IS_ERR(ctx))
4544 return PTR_ERR(ctx);
4545
David Quigleyaa9c2662013-05-22 12:50:44 -04004546 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4547
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004548 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4549 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004550 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 if (IS_ERR(state)) {
4552 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004553 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004556 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004557 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 return status;
4559}
4560
Trond Myklebust3c591172018-07-31 15:54:10 -04004561static int
4562_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563{
Trond Myklebust16e42952005-10-27 22:12:44 -04004564 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004565 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004567 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004569 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004570 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004571 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004573 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4574 .rpc_argp = &args,
4575 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004577 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004578 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004579
Bryan Schumaker7c513052011-03-24 17:12:24 +00004580 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004581 if (status == 0) {
4582 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004583 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4584 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004585 /* Removing a directory decrements nlink in the parent */
4586 if (ftype == NF4DIR && dir->i_nlink > 2)
4587 nfs4_dec_nlink_locked(dir);
4588 spin_unlock(&dir->i_lock);
4589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 return status;
4591}
4592
Trond Myklebust912678d2018-03-20 16:43:15 -04004593static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4594{
Trond Myklebust0688e642019-04-07 13:59:09 -04004595 struct nfs4_exception exception = {
4596 .interruptible = true,
4597 };
Trond Myklebust912678d2018-03-20 16:43:15 -04004598 struct inode *inode = d_inode(dentry);
4599 int err;
4600
Trond Myklebustc01d3642018-03-20 16:43:20 -04004601 if (inode) {
4602 if (inode->i_nlink == 1)
4603 nfs4_inode_return_delegation(inode);
4604 else
4605 nfs4_inode_make_writeable(inode);
4606 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004607 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004608 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004609 trace_nfs4_remove(dir, &dentry->d_name, err);
4610 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4611 &exception);
4612 } while (exception.retry);
4613 return err;
4614}
4615
4616static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617{
Trond Myklebust0688e642019-04-07 13:59:09 -04004618 struct nfs4_exception exception = {
4619 .interruptible = true,
4620 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004622
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004624 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004625 trace_nfs4_remove(dir, name, err);
4626 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 &exception);
4628 } while (exception.retry);
4629 return err;
4630}
4631
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004632static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4633 struct dentry *dentry,
4634 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004636 struct nfs_removeargs *args = msg->rpc_argp;
4637 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638
Trond Myklebust977fcc22018-03-20 16:43:17 -04004639 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004641 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004642
4643 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004644
4645 if (inode)
4646 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647}
4648
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004649static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4650{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004651 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004652 &data->args.seq_args,
4653 &data->res.seq_res,
4654 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655}
4656
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004657static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004659 struct nfs_unlinkdata *data = task->tk_calldata;
4660 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004661
Trond Myklebust14516c32010-07-31 14:29:06 -04004662 if (!nfs4_sequence_done(task, &res->seq_res))
4663 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004664 if (nfs4_async_handle_error(task, res->server, NULL,
4665 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004666 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004667 if (task->tk_status == 0)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004668 nfs4_update_changeattr(dir, &res->cinfo,
4669 res->dir_attr->time_start,
4670 NFS_INO_INVALID_DATA);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004671 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672}
4673
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004674static void nfs4_proc_rename_setup(struct rpc_message *msg,
4675 struct dentry *old_dentry,
4676 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004677{
Jeff Laytond3d41522010-09-17 17:31:57 -04004678 struct nfs_renameargs *arg = msg->rpc_argp;
4679 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004680 struct inode *old_inode = d_inode(old_dentry);
4681 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004682
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004683 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004684 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004685 if (new_inode)
4686 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004687 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004688 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004689 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004690}
4691
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004692static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4693{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004694 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004695 &data->args.seq_args,
4696 &data->res.seq_res,
4697 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004698}
4699
4700static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4701 struct inode *new_dir)
4702{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004703 struct nfs_renamedata *data = task->tk_calldata;
4704 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004705
4706 if (!nfs4_sequence_done(task, &res->seq_res))
4707 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004708 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004709 return 0;
4710
Trond Myklebustc733c492017-01-11 12:32:26 -05004711 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004712 if (new_dir != old_dir) {
4713 /* Note: If we moved a directory, nlink will change */
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004714 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004715 res->old_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004716 NFS_INO_INVALID_OTHER |
4717 NFS_INO_INVALID_DATA);
4718 nfs4_update_changeattr(new_dir, &res->new_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004719 res->new_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004720 NFS_INO_INVALID_OTHER |
4721 NFS_INO_INVALID_DATA);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004722 } else
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004723 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004724 res->old_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004725 NFS_INO_INVALID_DATA);
Trond Myklebustc733c492017-01-11 12:32:26 -05004726 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004727 return 1;
4728}
4729
Al Virobeffb8f2016-07-20 16:34:42 -04004730static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004732 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004733 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 struct nfs4_link_arg arg = {
4735 .fh = NFS_FH(inode),
4736 .dir_fh = NFS_FH(dir),
4737 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004738 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004740 struct nfs4_link_res res = {
4741 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004742 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004743 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 struct rpc_message msg = {
4745 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4746 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004747 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004749 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750
Trond Myklebust136f2622010-04-16 16:22:49 -04004751 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004752 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004753 goto out;
4754
David Quigley14c43f72013-05-22 12:50:43 -04004755 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4756 if (IS_ERR(res.label)) {
4757 status = PTR_ERR(res.label);
4758 goto out;
4759 }
4760
Trond Myklebustc01d3642018-03-20 16:43:20 -04004761 nfs4_inode_make_writeable(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004762 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004763
Bryan Schumaker7c513052011-03-24 17:12:24 +00004764 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004765 if (!status) {
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004766 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4767 NFS_INO_INVALID_DATA);
David Quigleyaa9c2662013-05-22 12:50:44 -04004768 status = nfs_post_op_update_inode(inode, res.fattr);
4769 if (!status)
4770 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004771 }
David Quigley14c43f72013-05-22 12:50:43 -04004772
4773
4774 nfs4_label_free(res.label);
4775
Trond Myklebust136f2622010-04-16 16:22:49 -04004776out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004777 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 return status;
4779}
4780
Al Virobeffb8f2016-07-20 16:34:42 -04004781static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782{
Trond Myklebust0688e642019-04-07 13:59:09 -04004783 struct nfs4_exception exception = {
4784 .interruptible = true,
4785 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 int err;
4787 do {
4788 err = nfs4_handle_exception(NFS_SERVER(inode),
4789 _nfs4_proc_link(inode, dir, name),
4790 &exception);
4791 } while (exception.retry);
4792 return err;
4793}
4794
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004795struct nfs4_createdata {
4796 struct rpc_message msg;
4797 struct nfs4_create_arg arg;
4798 struct nfs4_create_res res;
4799 struct nfs_fh fh;
4800 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004801 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004802};
4803
4804static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004805 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004806{
4807 struct nfs4_createdata *data;
4808
4809 data = kzalloc(sizeof(*data), GFP_KERNEL);
4810 if (data != NULL) {
4811 struct nfs_server *server = NFS_SERVER(dir);
4812
David Quigley14c43f72013-05-22 12:50:43 -04004813 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4814 if (IS_ERR(data->label))
4815 goto out_free;
4816
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004817 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4818 data->msg.rpc_argp = &data->arg;
4819 data->msg.rpc_resp = &data->res;
4820 data->arg.dir_fh = NFS_FH(dir);
4821 data->arg.server = server;
4822 data->arg.name = name;
4823 data->arg.attrs = sattr;
4824 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004825 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004826 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004827 data->res.server = server;
4828 data->res.fh = &data->fh;
4829 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004830 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004831 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004832 }
4833 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004834out_free:
4835 kfree(data);
4836 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004837}
4838
4839static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4840{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004841 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004842 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004843 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004844 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004845 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4846 data->res.fattr->time_start,
4847 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004848 /* Creating a directory bumps nlink in the parent */
4849 if (data->arg.ftype == NF4DIR)
4850 nfs4_inc_nlink_locked(dir);
4851 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004852 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004853 }
4854 return status;
4855}
4856
4857static void nfs4_free_createdata(struct nfs4_createdata *data)
4858{
David Quigley14c43f72013-05-22 12:50:43 -04004859 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004860 kfree(data);
4861}
4862
Chuck Lever4f390c12006-08-22 20:06:22 -04004863static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004864 struct page *page, unsigned int len, struct iattr *sattr,
4865 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004867 struct nfs4_createdata *data;
4868 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
Chuck Lever94a6d752006-08-22 20:06:23 -04004870 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004871 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004872
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004873 status = -ENOMEM;
4874 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4875 if (data == NULL)
4876 goto out;
4877
4878 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4879 data->arg.u.symlink.pages = &page;
4880 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004881 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004883 status = nfs4_do_create(dir, dentry, data);
4884
4885 nfs4_free_createdata(data);
4886out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 return status;
4888}
4889
Chuck Lever4f390c12006-08-22 20:06:22 -04004890static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004891 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892{
Trond Myklebust0688e642019-04-07 13:59:09 -04004893 struct nfs4_exception exception = {
4894 .interruptible = true,
4895 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004896 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004898
4899 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4900
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004902 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4903 trace_nfs4_symlink(dir, &dentry->d_name, err);
4904 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 &exception);
4906 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004907
4908 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 return err;
4910}
4911
4912static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004913 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004915 struct nfs4_createdata *data;
4916 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004918 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4919 if (data == NULL)
4920 goto out;
4921
David Quigley1775fd32013-05-22 12:50:42 -04004922 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004923 status = nfs4_do_create(dir, dentry, data);
4924
4925 nfs4_free_createdata(data);
4926out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 return status;
4928}
4929
4930static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4931 struct iattr *sattr)
4932{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004933 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004934 struct nfs4_exception exception = {
4935 .interruptible = true,
4936 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004937 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004939
David Quigleyaa9c2662013-05-22 12:50:44 -04004940 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4941
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004942 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4943 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004945 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4946 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4947 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 &exception);
4949 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004950 nfs4_label_release_security(label);
4951
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 return err;
4953}
4954
NeilBrown684f39b2018-12-03 11:30:30 +11004955static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004956 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957{
David Howells2b0143b2015-03-17 22:25:59 +00004958 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 struct nfs4_readdir_arg args = {
4960 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004961 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 .pgbase = 0,
4963 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004964 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004965 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 };
4967 struct nfs4_readdir_res res;
4968 struct rpc_message msg = {
4969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4970 .rpc_argp = &args,
4971 .rpc_resp = &res,
4972 .rpc_cred = cred,
4973 };
4974 int status;
4975
Al Viro6de14722013-09-16 10:53:17 -04004976 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4977 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004978 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004979 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004981 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05004982 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004983 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004984 status += args.pgbase;
4985 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004986
4987 nfs_invalidate_atime(dir);
4988
Harvey Harrison3110ff82008-05-02 13:42:44 -07004989 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 return status;
4991}
4992
NeilBrown684f39b2018-12-03 11:30:30 +11004993static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004994 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995{
Trond Myklebust0688e642019-04-07 13:59:09 -04004996 struct nfs4_exception exception = {
4997 .interruptible = true,
4998 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 int err;
5000 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005001 err = _nfs4_proc_readdir(dentry, cred, cookie,
5002 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00005003 trace_nfs4_readdir(d_inode(dentry), err);
5004 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 &exception);
5006 } while (exception.retry);
5007 return err;
5008}
5009
5010static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04005011 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005013 struct nfs4_createdata *data;
5014 int mode = sattr->ia_mode;
5015 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005017 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5018 if (data == NULL)
5019 goto out;
5020
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005022 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005024 data->arg.ftype = NF4BLK;
5025 data->arg.u.device.specdata1 = MAJOR(rdev);
5026 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 }
5028 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005029 data->arg.ftype = NF4CHR;
5030 data->arg.u.device.specdata1 = MAJOR(rdev);
5031 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005032 } else if (!S_ISSOCK(mode)) {
5033 status = -EINVAL;
5034 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 }
David Quigley1775fd32013-05-22 12:50:42 -04005036
David Quigleyaa9c2662013-05-22 12:50:44 -04005037 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005038 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005039out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005040 nfs4_free_createdata(data);
5041out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 return status;
5043}
5044
5045static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5046 struct iattr *sattr, dev_t rdev)
5047{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005048 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04005049 struct nfs4_exception exception = {
5050 .interruptible = true,
5051 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005052 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00005054
David Quigleyaa9c2662013-05-22 12:50:44 -04005055 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5056
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005057 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5058 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005060 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5061 trace_nfs4_mknod(dir, &dentry->d_name, err);
5062 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 &exception);
5064 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04005065
5066 nfs4_label_release_security(label);
5067
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 return err;
5069}
5070
5071static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5072 struct nfs_fsstat *fsstat)
5073{
5074 struct nfs4_statfs_arg args = {
5075 .fh = fhandle,
5076 .bitmask = server->attr_bitmask,
5077 };
Benny Halevy24ad1482009-04-01 09:21:56 -04005078 struct nfs4_statfs_res res = {
5079 .fsstat = fsstat,
5080 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 struct rpc_message msg = {
5082 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5083 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04005084 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 };
5086
Trond Myklebust0e574af2005-10-27 22:12:38 -04005087 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005088 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089}
5090
5091static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5092{
Trond Myklebust0688e642019-04-07 13:59:09 -04005093 struct nfs4_exception exception = {
5094 .interruptible = true,
5095 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 int err;
5097 do {
5098 err = nfs4_handle_exception(server,
5099 _nfs4_proc_statfs(server, fhandle, fsstat),
5100 &exception);
5101 } while (exception.retry);
5102 return err;
5103}
5104
5105static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5106 struct nfs_fsinfo *fsinfo)
5107{
5108 struct nfs4_fsinfo_arg args = {
5109 .fh = fhandle,
5110 .bitmask = server->attr_bitmask,
5111 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04005112 struct nfs4_fsinfo_res res = {
5113 .fsinfo = fsinfo,
5114 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 struct rpc_message msg = {
5116 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5117 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04005118 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 };
5120
Bryan Schumaker7c513052011-03-24 17:12:24 +00005121 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122}
5123
5124static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5125{
Trond Myklebust0688e642019-04-07 13:59:09 -04005126 struct nfs4_exception exception = {
5127 .interruptible = true,
5128 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 int err;
5130
5131 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04005132 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04005133 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005134 if (err == 0) {
Robert Milkowski7dc29932020-01-30 09:43:25 +00005135 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005136 break;
5137 }
5138 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 } while (exception.retry);
5140 return err;
5141}
5142
5143static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5144{
Bryan Schumakere38eb652012-06-20 15:53:40 -04005145 int error;
5146
Trond Myklebust0e574af2005-10-27 22:12:38 -04005147 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04005148 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005149 if (error == 0) {
5150 /* block layout checks this! */
5151 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04005152 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005153 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04005154
5155 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156}
5157
5158static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5159 struct nfs_pathconf *pathconf)
5160{
5161 struct nfs4_pathconf_arg args = {
5162 .fh = fhandle,
5163 .bitmask = server->attr_bitmask,
5164 };
Benny Halevyd45b2982009-04-01 09:21:58 -04005165 struct nfs4_pathconf_res res = {
5166 .pathconf = pathconf,
5167 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 struct rpc_message msg = {
5169 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5170 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04005171 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 };
5173
5174 /* None of the pathconf attributes are mandatory to implement */
5175 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5176 memset(pathconf, 0, sizeof(*pathconf));
5177 return 0;
5178 }
5179
Trond Myklebust0e574af2005-10-27 22:12:38 -04005180 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005181 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182}
5183
5184static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5185 struct nfs_pathconf *pathconf)
5186{
Trond Myklebust0688e642019-04-07 13:59:09 -04005187 struct nfs4_exception exception = {
5188 .interruptible = true,
5189 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 int err;
5191
5192 do {
5193 err = nfs4_handle_exception(server,
5194 _nfs4_proc_pathconf(server, fhandle, pathconf),
5195 &exception);
5196 } while (exception.retry);
5197 return err;
5198}
5199
Trond Myklebust5521abf2013-03-16 20:54:34 -04005200int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04005201 const struct nfs_open_context *ctx,
5202 const struct nfs_lock_context *l_ctx,
5203 fmode_t fmode)
5204{
NeilBrown17393472016-10-13 15:26:47 +11005205 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04005206}
5207EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5208
Trond Myklebust5521abf2013-03-16 20:54:34 -04005209static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5210 const struct nfs_open_context *ctx,
5211 const struct nfs_lock_context *l_ctx,
5212 fmode_t fmode)
5213{
Ben Dooksd49dd112019-10-16 17:28:21 +01005214 nfs4_stateid _current_stateid;
Trond Myklebust5521abf2013-03-16 20:54:34 -04005215
Trond Myklebuste1253be2014-03-05 08:44:23 -05005216 /* If the current stateid represents a lost lock, then exit */
Ben Dooksd49dd112019-10-16 17:28:21 +01005217 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
Trond Myklebuste1253be2014-03-05 08:44:23 -05005218 return true;
Ben Dooksd49dd112019-10-16 17:28:21 +01005219 return nfs4_stateid_match(stateid, &_current_stateid);
Trond Myklebust5521abf2013-03-16 20:54:34 -04005220}
5221
5222static bool nfs4_error_stateid_expired(int err)
5223{
5224 switch (err) {
5225 case -NFS4ERR_DELEG_REVOKED:
5226 case -NFS4ERR_ADMIN_REVOKED:
5227 case -NFS4ERR_BAD_STATEID:
5228 case -NFS4ERR_STALE_STATEID:
5229 case -NFS4ERR_OLD_STATEID:
5230 case -NFS4ERR_OPENMODE:
5231 case -NFS4ERR_EXPIRED:
5232 return true;
5233 }
5234 return false;
5235}
5236
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005237static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005239 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005241 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005242 if (task->tk_status < 0) {
5243 struct nfs4_exception exception = {
5244 .inode = hdr->inode,
5245 .state = hdr->args.context->state,
5246 .stateid = &hdr->args.stateid,
5247 };
5248 task->tk_status = nfs4_async_handle_exception(task,
5249 server, task->tk_status, &exception);
5250 if (exception.retry) {
5251 rpc_restart_call_prepare(task);
5252 return -EAGAIN;
5253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 }
Trond Myklebust8850df92007-09-28 17:20:07 -04005255
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005257 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05005258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259}
5260
Trond Myklebust5521abf2013-03-16 20:54:34 -04005261static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005262 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005263{
5264
5265 if (!nfs4_error_stateid_expired(task->tk_status) ||
5266 nfs4_stateid_is_current(&args->stateid,
5267 args->context,
5268 args->lock_context,
5269 FMODE_READ))
5270 return false;
5271 rpc_restart_call_prepare(task);
5272 return true;
5273}
5274
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005275static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005276{
5277
5278 dprintk("--> %s\n", __func__);
5279
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005280 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005281 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005282 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005283 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005284 if (task->tk_status > 0)
5285 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005286 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5287 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005288}
5289
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005290static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5291 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005293 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005294 if (!hdr->pgio_done_cb)
5295 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005296 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005297 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298}
5299
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005300static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5301 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005302{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005303 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005304 &hdr->args.seq_args,
5305 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005306 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005307 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005308 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5309 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005310 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005311 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005312 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005313 return -EIO;
5314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315}
5316
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005317static int nfs4_write_done_cb(struct rpc_task *task,
5318 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005320 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005321
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005322 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005323 if (task->tk_status < 0) {
5324 struct nfs4_exception exception = {
5325 .inode = hdr->inode,
5326 .state = hdr->args.context->state,
5327 .stateid = &hdr->args.stateid,
5328 };
5329 task->tk_status = nfs4_async_handle_exception(task,
5330 NFS_SERVER(inode), task->tk_status,
5331 &exception);
5332 if (exception.retry) {
5333 rpc_restart_call_prepare(task);
5334 return -EAGAIN;
5335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005337 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005338 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005339 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005340 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342}
5343
Trond Myklebust5521abf2013-03-16 20:54:34 -04005344static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005345 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005346{
5347
5348 if (!nfs4_error_stateid_expired(task->tk_status) ||
5349 nfs4_stateid_is_current(&args->stateid,
5350 args->context,
5351 args->lock_context,
5352 FMODE_WRITE))
5353 return false;
5354 rpc_restart_call_prepare(task);
5355 return true;
5356}
5357
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005358static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005359{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005360 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005361 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005362 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005363 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005364 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5365 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005366}
5367
Trond Myklebust5a37f852012-04-28 14:55:16 -04005368static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005369bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005370{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005371 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005372 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005373 return false;
5374 /* Otherwise, request attributes if and only if we don't hold
5375 * a delegation
5376 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005377 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005378}
Fred Isamana69aef12011-03-03 15:13:47 +00005379
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005380static void nfs4_bitmask_adjust(__u32 *bitmask, struct inode *inode,
5381 struct nfs_server *server,
5382 struct nfs4_label *label)
5383{
5384
5385 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
5386
5387 if ((cache_validity & NFS_INO_INVALID_DATA) ||
5388 (cache_validity & NFS_INO_REVAL_PAGECACHE) ||
5389 (cache_validity & NFS_INO_REVAL_FORCED) ||
5390 (cache_validity & NFS_INO_INVALID_OTHER))
5391 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
5392
5393 if (cache_validity & NFS_INO_INVALID_ATIME)
5394 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5395 if (cache_validity & NFS_INO_INVALID_ACCESS)
5396 bitmask[0] |= FATTR4_WORD1_MODE | FATTR4_WORD1_OWNER |
5397 FATTR4_WORD1_OWNER_GROUP;
5398 if (cache_validity & NFS_INO_INVALID_ACL)
5399 bitmask[0] |= FATTR4_WORD0_ACL;
5400 if (cache_validity & NFS_INO_INVALID_LABEL)
5401 bitmask[2] |= FATTR4_WORD2_SECURITY_LABEL;
5402 if (cache_validity & NFS_INO_INVALID_CTIME)
5403 bitmask[0] |= FATTR4_WORD0_CHANGE;
5404 if (cache_validity & NFS_INO_INVALID_MTIME)
5405 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5406 if (cache_validity & NFS_INO_INVALID_SIZE)
5407 bitmask[0] |= FATTR4_WORD0_SIZE;
5408 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5409 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5410}
5411
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005412static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005413 struct rpc_message *msg,
5414 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005416 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005417
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005418 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5419 hdr->args.bitmask = NULL;
5420 hdr->res.fattr = NULL;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005421 } else {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005422 hdr->args.bitmask = server->cache_consistency_bitmask;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005423 nfs4_bitmask_adjust(hdr->args.bitmask, hdr->inode, server, NULL);
5424 }
Trond Myklebust5a37f852012-04-28 14:55:16 -04005425
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005426 if (!hdr->pgio_done_cb)
5427 hdr->pgio_done_cb = nfs4_write_done_cb;
5428 hdr->res.server = server;
5429 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005431 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Olga Kornievskaiacd1b6592020-02-12 17:32:12 -05005432 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005433 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434}
5435
Fred Isaman0b7c0152012-04-20 14:47:39 -04005436static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5437{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005438 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005439 &data->args.seq_args,
5440 &data->res.seq_res,
5441 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442}
5443
Fred Isaman0b7c0152012-04-20 14:47:39 -04005444static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005447
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005448 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005449 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5450 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005451 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005452 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455}
5456
Fred Isaman0b7c0152012-04-20 14:47:39 -04005457static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005458{
5459 if (!nfs4_sequence_done(task, &data->res.seq_res))
5460 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005461 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005462}
5463
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005464static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5465 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005467 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005468
Fred Isaman0b7c0152012-04-20 14:47:39 -04005469 if (data->commit_done_cb == NULL)
5470 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005471 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005472 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005473 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005474 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475}
5476
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005477static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5478 struct nfs_commitres *res)
5479{
5480 struct inode *dst_inode = file_inode(dst);
5481 struct nfs_server *server = NFS_SERVER(dst_inode);
5482 struct rpc_message msg = {
5483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5484 .rpc_argp = args,
5485 .rpc_resp = res,
5486 };
5487
5488 args->fh = NFS_FH(dst_inode);
5489 return nfs4_call_sync(server->client, server, &msg,
5490 &args->seq_args, &res->seq_res, 1);
5491}
5492
5493int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5494{
5495 struct nfs_commitargs args = {
5496 .offset = offset,
5497 .count = count,
5498 };
5499 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5500 struct nfs4_exception exception = { };
5501 int status;
5502
5503 do {
5504 status = _nfs4_proc_commit(dst, &args, res);
5505 status = nfs4_handle_exception(dst_server, status, &exception);
5506 } while (exception.retry);
5507
5508 return status;
5509}
5510
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005511struct nfs4_renewdata {
5512 struct nfs_client *client;
5513 unsigned long timestamp;
5514};
5515
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516/*
5517 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5518 * standalone procedure for queueing an asynchronous RENEW.
5519 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005520static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005521{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005522 struct nfs4_renewdata *data = calldata;
5523 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005524
Elena Reshetova212bf412017-10-20 12:53:38 +03005525 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005526 nfs4_schedule_state_renewal(clp);
5527 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005528 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005529}
5530
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005531static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005533 struct nfs4_renewdata *data = calldata;
5534 struct nfs_client *clp = data->client;
5535 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005537 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005538 switch (task->tk_status) {
5539 case 0:
5540 break;
5541 case -NFS4ERR_LEASE_MOVED:
5542 nfs4_schedule_lease_moved_recovery(clp);
5543 break;
5544 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005545 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005546 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5547 return;
5548 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005549 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005550 return;
5551 }
5552 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005554 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555}
5556
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005557static const struct rpc_call_ops nfs4_renew_ops = {
5558 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005559 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005560};
5561
NeilBrowna52458b2018-12-03 11:30:31 +11005562static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563{
5564 struct rpc_message msg = {
5565 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5566 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005567 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005569 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005571 if (renew_flags == 0)
5572 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005573 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005574 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005575 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005576 if (data == NULL) {
5577 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005578 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005579 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005580 data->client = clp;
5581 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005582 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005583 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584}
5585
NeilBrowna52458b2018-12-03 11:30:31 +11005586static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587{
5588 struct rpc_message msg = {
5589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5590 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005591 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 };
5593 unsigned long now = jiffies;
5594 int status;
5595
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005596 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 if (status < 0)
5598 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005599 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 return 0;
5601}
5602
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005603static inline int nfs4_server_supports_acls(struct nfs_server *server)
5604{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005605 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005606}
5607
Trond Myklebust21f498c2012-08-24 10:59:25 -04005608/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5609 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005610 * the stack.
5611 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005612#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005613
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005614int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005615 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005616{
5617 struct page *newpage, **spages;
5618 int rc = 0;
5619 size_t len;
5620 spages = pages;
5621
5622 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005623 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005624 newpage = alloc_page(GFP_KERNEL);
5625
5626 if (newpage == NULL)
5627 goto unwind;
5628 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005629 buf += len;
5630 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005631 *pages++ = newpage;
5632 rc++;
5633 } while (buflen != 0);
5634
5635 return rc;
5636
5637unwind:
5638 for(; rc > 0; rc--)
5639 __free_page(spages[rc-1]);
5640 return -ENOMEM;
5641}
5642
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005643struct nfs4_cached_acl {
5644 int cached;
5645 size_t len;
Gustavo A. R. Silva5601cda2020-03-09 13:24:42 -05005646 char data[];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005647};
5648
5649static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005650{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005651 struct nfs_inode *nfsi = NFS_I(inode);
5652
5653 spin_lock(&inode->i_lock);
5654 kfree(nfsi->nfs4_acl);
5655 nfsi->nfs4_acl = acl;
5656 spin_unlock(&inode->i_lock);
5657}
5658
5659static void nfs4_zap_acl_attr(struct inode *inode)
5660{
5661 nfs4_set_cached_acl(inode, NULL);
5662}
5663
5664static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5665{
5666 struct nfs_inode *nfsi = NFS_I(inode);
5667 struct nfs4_cached_acl *acl;
5668 int ret = -ENOENT;
5669
5670 spin_lock(&inode->i_lock);
5671 acl = nfsi->nfs4_acl;
5672 if (acl == NULL)
5673 goto out;
5674 if (buf == NULL) /* user is just asking for length */
5675 goto out_len;
5676 if (acl->cached == 0)
5677 goto out;
5678 ret = -ERANGE; /* see getxattr(2) man page */
5679 if (acl->len > buflen)
5680 goto out;
5681 memcpy(buf, acl->data, acl->len);
5682out_len:
5683 ret = acl->len;
5684out:
5685 spin_unlock(&inode->i_lock);
5686 return ret;
5687}
5688
Sachin Prabhu5794d212012-04-17 14:36:40 +01005689static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005690{
5691 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005692 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005693
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005694 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005695 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005696 if (acl == NULL)
5697 goto out;
5698 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005699 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005700 } else {
5701 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5702 if (acl == NULL)
5703 goto out;
5704 acl->cached = 0;
5705 }
5706 acl->len = acl_len;
5707out:
5708 nfs4_set_cached_acl(inode, acl);
5709}
5710
Andy Adamsonbf118a32011-12-07 11:55:27 -05005711/*
5712 * The getxattr API returns the required buffer length when called with a
5713 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5714 * the required buf. On a NULL buf, we send a page of data to the server
5715 * guessing that the ACL request can be serviced by a page. If so, we cache
5716 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5717 * the cache. If not so, we throw away the page, and cache the required
5718 * length. The next getxattr call will then produce another round trip to
5719 * the server, this time with the input buf of the required size.
5720 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005721static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005722{
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005723 struct page **pages;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005724 struct nfs_getaclargs args = {
5725 .fh = NFS_FH(inode),
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005726 .acl_len = buflen,
5727 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005728 struct nfs_getaclres res = {
5729 .acl_len = buflen,
5730 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005731 struct rpc_message msg = {
5732 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5733 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005734 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005735 };
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005736 unsigned int npages;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005737 int ret = -ENOMEM, i;
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005738 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005739
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005740 if (buflen == 0)
5741 buflen = server->rsize;
5742
5743 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5744 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5745 if (!pages)
5746 return -ENOMEM;
5747
5748 args.acl_pages = pages;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005749
Andy Adamsonbf118a32011-12-07 11:55:27 -05005750 for (i = 0; i < npages; i++) {
5751 pages[i] = alloc_page(GFP_KERNEL);
5752 if (!pages[i])
5753 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005754 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005755
5756 /* for decoding across pages */
5757 res.acl_scratch = alloc_page(GFP_KERNEL);
5758 if (!res.acl_scratch)
5759 goto out_free;
5760
Andy Adamsonbf118a32011-12-07 11:55:27 -05005761 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005762
Peng Taode040be2012-01-10 22:42:47 +08005763 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005764 __func__, buf, buflen, npages, args.acl_len);
5765 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5766 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005767 if (ret)
5768 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005769
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005770 /* Handle the case where the passed-in buffer is too short */
5771 if (res.acl_flags & NFS4_ACL_TRUNC) {
5772 /* Did the user only issue a request for the acl length? */
5773 if (buf == NULL)
5774 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005775 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005776 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005777 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005778 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005779 if (buf) {
5780 if (res.acl_len > buflen) {
5781 ret = -ERANGE;
5782 goto out_free;
5783 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005784 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005785 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005786out_ok:
5787 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005788out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005789 for (i = 0; i < npages; i++)
5790 if (pages[i])
5791 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005792 if (res.acl_scratch)
5793 __free_page(res.acl_scratch);
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005794 kfree(pages);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005795 return ret;
5796}
5797
Trond Myklebust16b42892006-08-24 12:27:15 -04005798static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5799{
Trond Myklebust0688e642019-04-07 13:59:09 -04005800 struct nfs4_exception exception = {
5801 .interruptible = true,
5802 };
Trond Myklebust16b42892006-08-24 12:27:15 -04005803 ssize_t ret;
5804 do {
5805 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005806 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005807 if (ret >= 0)
5808 break;
5809 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5810 } while (exception.retry);
5811 return ret;
5812}
5813
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005814static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5815{
5816 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005817 int ret;
5818
5819 if (!nfs4_server_supports_acls(server))
5820 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005821 ret = nfs_revalidate_inode(server, inode);
5822 if (ret < 0)
5823 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005824 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5825 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005826 ret = nfs4_read_cached_acl(inode, buf, buflen);
5827 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005828 /* -ENOENT is returned if there is no ACL or if there is an ACL
5829 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005830 return ret;
5831 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005832}
5833
Trond Myklebust16b42892006-08-24 12:27:15 -04005834static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005835{
5836 struct nfs_server *server = NFS_SERVER(inode);
5837 struct page *pages[NFS4ACL_MAXPAGES];
5838 struct nfs_setaclargs arg = {
5839 .fh = NFS_FH(inode),
5840 .acl_pages = pages,
5841 .acl_len = buflen,
5842 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005843 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005844 struct rpc_message msg = {
5845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5846 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005847 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005848 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005849 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005850 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005851
5852 if (!nfs4_server_supports_acls(server))
5853 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005854 if (npages > ARRAY_SIZE(pages))
5855 return -ERANGE;
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005856 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005857 if (i < 0)
5858 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005859 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005860 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005861
5862 /*
5863 * Free each page after tx, so the only ref left is
5864 * held by the network stack
5865 */
5866 for (; i > 0; i--)
5867 put_page(pages[i-1]);
5868
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005869 /*
5870 * Acl update can result in inode attribute update.
5871 * so mark the attribute cache invalid.
5872 */
5873 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005874 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
Trond Myklebust472f7612018-04-08 18:14:43 -04005875 | NFS_INO_INVALID_CTIME
5876 | NFS_INO_REVAL_FORCED;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005877 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005878 nfs_access_zap_cache(inode);
5879 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005880 return ret;
5881}
5882
Trond Myklebust16b42892006-08-24 12:27:15 -04005883static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5884{
5885 struct nfs4_exception exception = { };
5886 int err;
5887 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005888 err = __nfs4_proc_set_acl(inode, buf, buflen);
5889 trace_nfs4_set_acl(inode, err);
5890 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005891 &exception);
5892 } while (exception.retry);
5893 return err;
5894}
5895
David Quigleyaa9c2662013-05-22 12:50:44 -04005896#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5897static int _nfs4_get_security_label(struct inode *inode, void *buf,
5898 size_t buflen)
5899{
5900 struct nfs_server *server = NFS_SERVER(inode);
5901 struct nfs_fattr fattr;
5902 struct nfs4_label label = {0, 0, buflen, buf};
5903
5904 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005905 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005906 .fh = NFS_FH(inode),
5907 .bitmask = bitmask,
5908 };
5909 struct nfs4_getattr_res res = {
5910 .fattr = &fattr,
5911 .label = &label,
5912 .server = server,
5913 };
5914 struct rpc_message msg = {
5915 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005916 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005917 .rpc_resp = &res,
5918 };
5919 int ret;
5920
5921 nfs_fattr_init(&fattr);
5922
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005923 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005924 if (ret)
5925 return ret;
5926 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5927 return -ENOENT;
David Quigleyaa9c2662013-05-22 12:50:44 -04005928 return 0;
5929}
5930
5931static int nfs4_get_security_label(struct inode *inode, void *buf,
5932 size_t buflen)
5933{
Trond Myklebust0688e642019-04-07 13:59:09 -04005934 struct nfs4_exception exception = {
5935 .interruptible = true,
5936 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005937 int err;
5938
5939 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5940 return -EOPNOTSUPP;
5941
5942 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005943 err = _nfs4_get_security_label(inode, buf, buflen);
5944 trace_nfs4_get_security_label(inode, err);
5945 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005946 &exception);
5947 } while (exception.retry);
5948 return err;
5949}
5950
5951static int _nfs4_do_set_security_label(struct inode *inode,
5952 struct nfs4_label *ilabel,
5953 struct nfs_fattr *fattr,
5954 struct nfs4_label *olabel)
5955{
5956
5957 struct iattr sattr = {0};
5958 struct nfs_server *server = NFS_SERVER(inode);
5959 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005960 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005961 .fh = NFS_FH(inode),
5962 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005963 .server = server,
5964 .bitmask = bitmask,
5965 .label = ilabel,
5966 };
5967 struct nfs_setattrres res = {
5968 .fattr = fattr,
5969 .label = olabel,
5970 .server = server,
5971 };
5972 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005973 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5974 .rpc_argp = &arg,
5975 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005976 };
5977 int status;
5978
Jeff Layton12207f62013-11-01 10:49:32 -04005979 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005980
Jeff Layton12207f62013-11-01 10:49:32 -04005981 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005982 if (status)
5983 dprintk("%s failed: %d\n", __func__, status);
5984
5985 return status;
5986}
5987
5988static int nfs4_do_set_security_label(struct inode *inode,
5989 struct nfs4_label *ilabel,
5990 struct nfs_fattr *fattr,
5991 struct nfs4_label *olabel)
5992{
5993 struct nfs4_exception exception = { };
5994 int err;
5995
5996 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005997 err = _nfs4_do_set_security_label(inode, ilabel,
5998 fattr, olabel);
5999 trace_nfs4_set_security_label(inode, err);
6000 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04006001 &exception);
6002 } while (exception.retry);
6003 return err;
6004}
6005
6006static int
Al Viro59301222016-05-27 10:19:30 -04006007nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04006008{
6009 struct nfs4_label ilabel, *olabel = NULL;
6010 struct nfs_fattr fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04006011 int status;
6012
6013 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6014 return -EOPNOTSUPP;
6015
6016 nfs_fattr_init(&fattr);
6017
6018 ilabel.pi = 0;
6019 ilabel.lfs = 0;
6020 ilabel.label = (char *)buf;
6021 ilabel.len = buflen;
6022
David Quigleyaa9c2662013-05-22 12:50:44 -04006023 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
6024 if (IS_ERR(olabel)) {
6025 status = -PTR_ERR(olabel);
6026 goto out;
6027 }
6028
6029 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
6030 if (status == 0)
6031 nfs_setsecurity(inode, &fattr, olabel);
6032
6033 nfs4_label_free(olabel);
6034out:
David Quigleyaa9c2662013-05-22 12:50:44 -04006035 return status;
6036}
6037#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6038
6039
Chuck Leverf0920752012-05-21 22:45:41 -04006040static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6041 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05006042{
6043 __be32 verf[2];
6044
Chuck Lever2c820d92012-05-21 22:45:33 -04006045 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6046 /* An impossible timestamp guarantees this value
6047 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07006048 verf[0] = cpu_to_be32(U32_MAX);
6049 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04006050 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04006051 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07006052 u64 ns = ktime_to_ns(nn->boot_time);
6053
6054 verf[0] = cpu_to_be32(ns >> 32);
6055 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04006056 }
Chuck Levercd937102012-03-02 17:14:31 -05006057 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6058}
6059
Jeff Laytona3192682015-06-09 19:43:59 -04006060static int
6061nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04006062{
Jeff Laytona3192682015-06-09 19:43:59 -04006063 size_t len;
6064 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04006065
Trond Myklebustceb3a162015-01-03 15:16:04 -05006066 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04006067 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08006068
Jeff Laytona3192682015-06-09 19:43:59 -04006069 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04006070 len = 14 +
6071 strlen(clp->cl_rpcclient->cl_nodename) +
6072 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04006073 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04006074 1;
6075 rcu_read_unlock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04006076 if (nfs4_client_id_uniquifier[0] != '\0')
6077 len += strlen(nfs4_client_id_uniquifier) + 1;
Jeff Laytona3192682015-06-09 19:43:59 -04006078 if (len > NFS4_OPAQUE_LIMIT + 1)
6079 return -EINVAL;
6080
6081 /*
6082 * Since this string is allocated at mount time, and held until the
6083 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6084 * about a memory-reclaim deadlock.
6085 */
6086 str = kmalloc(len, GFP_KERNEL);
6087 if (!str)
6088 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006089
Chuck Levere984a552012-09-14 17:24:21 -04006090 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04006091 if (nfs4_client_id_uniquifier[0] != '\0')
Chuck Lever025bb9f2018-06-04 10:53:34 -04006092 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04006093 clp->cl_rpcclient->cl_nodename,
6094 nfs4_client_id_uniquifier,
6095 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006096 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04006097 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04006098 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04006099 clp->cl_rpcclient->cl_nodename,
6100 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006101 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04006102 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04006103
Jeff Laytona3192682015-06-09 19:43:59 -04006104 clp->cl_owner_id = str;
6105 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006106}
6107
Jeff Layton873e3852015-06-09 19:44:00 -04006108static int
6109nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04006110{
Jeff Layton873e3852015-06-09 19:44:00 -04006111 size_t len;
6112 char *str;
6113
6114 len = 10 + 10 + 1 + 10 + 1 +
6115 strlen(nfs4_client_id_uniquifier) + 1 +
6116 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6117
6118 if (len > NFS4_OPAQUE_LIMIT + 1)
6119 return -EINVAL;
6120
6121 /*
6122 * Since this string is allocated at mount time, and held until the
6123 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6124 * about a memory-reclaim deadlock.
6125 */
6126 str = kmalloc(len, GFP_KERNEL);
6127 if (!str)
6128 return -ENOMEM;
6129
Trond Myklebustf2dd4362015-10-08 11:33:17 -04006130 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04006131 clp->rpc_ops->version, clp->cl_minorversion,
6132 nfs4_client_id_uniquifier,
6133 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006134 clp->cl_owner_id = str;
6135 return 0;
6136}
6137
6138static int
6139nfs4_init_uniform_client_string(struct nfs_client *clp)
6140{
Jeff Layton873e3852015-06-09 19:44:00 -04006141 size_t len;
6142 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006143
6144 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04006145 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04006146
6147 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04006148 return nfs4_init_uniquifier_client_string(clp);
6149
6150 len = 10 + 10 + 1 + 10 + 1 +
6151 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6152
6153 if (len > NFS4_OPAQUE_LIMIT + 1)
6154 return -EINVAL;
6155
6156 /*
6157 * Since this string is allocated at mount time, and held until the
6158 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6159 * about a memory-reclaim deadlock.
6160 */
6161 str = kmalloc(len, GFP_KERNEL);
6162 if (!str)
6163 return -ENOMEM;
6164
Trond Myklebustf2dd4362015-10-08 11:33:17 -04006165 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04006166 clp->rpc_ops->version, clp->cl_minorversion,
6167 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006168 clp->cl_owner_id = str;
6169 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006170}
6171
Chuck Lever706cb8d2014-03-12 12:51:47 -04006172/*
6173 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6174 * services. Advertise one based on the address family of the
6175 * clientaddr.
6176 */
6177static unsigned int
6178nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6179{
6180 if (strchr(clp->cl_ipaddr, ':') != NULL)
6181 return scnprintf(buf, len, "tcp6");
6182 else
6183 return scnprintf(buf, len, "tcp");
6184}
6185
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006186static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6187{
6188 struct nfs4_setclientid *sc = calldata;
6189
6190 if (task->tk_status == 0)
6191 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6192}
6193
6194static const struct rpc_call_ops nfs4_setclientid_ops = {
6195 .rpc_call_done = nfs4_setclientid_done,
6196};
6197
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006198/**
6199 * nfs4_proc_setclientid - Negotiate client ID
6200 * @clp: state data structure
6201 * @program: RPC program for NFSv4 callback service
6202 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11006203 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006204 * @res: where to place the result
6205 *
6206 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6207 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006208int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11006209 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006210 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211{
6212 nfs4_verifier sc_verifier;
6213 struct nfs4_setclientid setclientid = {
6214 .sc_verifier = &sc_verifier,
6215 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006216 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 };
6218 struct rpc_message msg = {
6219 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6220 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006221 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006222 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006224 struct rpc_task_setup task_setup_data = {
6225 .rpc_client = clp->cl_rpcclient,
6226 .rpc_message = &msg,
6227 .callback_ops = &nfs4_setclientid_ops,
6228 .callback_data = &setclientid,
NeilBrown5a0c2572019-05-30 10:41:28 +10006229 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006230 };
Robert Milkowski7dc29932020-01-30 09:43:25 +00006231 unsigned long now = jiffies;
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006232 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233
Chuck Leverde734832012-07-11 16:30:50 -04006234 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04006235 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006236
6237 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6238 status = nfs4_init_uniform_client_string(clp);
6239 else
Jeff Laytona3192682015-06-09 19:43:59 -04006240 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04006241
6242 if (status)
6243 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006244
Chuck Leverde734832012-07-11 16:30:50 -04006245 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04006246 setclientid.sc_netid_len =
6247 nfs4_init_callback_netid(clp,
6248 setclientid.sc_netid,
6249 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04006250 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05006251 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 clp->cl_ipaddr, port >> 8, port & 255);
6253
Jeff Layton3a6bb732015-06-09 19:43:57 -04006254 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006255 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006256 clp->cl_owner_id);
Anna Schumakerdae40962019-08-14 15:28:28 -04006257
6258 status = nfs4_call_sync_custom(&task_setup_data);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006259 if (setclientid.sc_cred) {
Chuck Lever1047ec82019-10-04 09:58:54 -04006260 kfree(clp->cl_acceptor);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006261 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6262 put_rpccred(setclientid.sc_cred);
6263 }
Robert Milkowski7dc29932020-01-30 09:43:25 +00006264
6265 if (status == 0)
6266 do_renew_lease(clp, now);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006267out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006268 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006269 dprintk("NFS reply setclientid: %d\n", status);
6270 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271}
6272
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006273/**
6274 * nfs4_proc_setclientid_confirm - Confirm client ID
6275 * @clp: state data structure
Trond Myklebust302fad72019-02-18 13:32:38 -05006276 * @arg: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11006277 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006278 *
6279 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6280 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04006281int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006282 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11006283 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285 struct rpc_message msg = {
6286 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006287 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006288 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290 int status;
6291
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006292 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6293 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6294 clp->cl_clientid);
NeilBrown5a0c2572019-05-30 10:41:28 +10006295 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6296 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006297 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006298 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299 return status;
6300}
6301
Trond Myklebustfe650402006-01-03 09:55:18 +01006302struct nfs4_delegreturndata {
6303 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006304 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01006305 struct nfs_fh fh;
6306 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006307 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006308 struct {
6309 struct nfs4_layoutreturn_args arg;
6310 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006311 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006312 u32 roc_barrier;
6313 bool roc;
6314 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006315 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006316 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006317 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006318};
6319
Trond Myklebustfe650402006-01-03 09:55:18 +01006320static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6321{
6322 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006323 struct nfs4_exception exception = {
6324 .inode = data->inode,
6325 .stateid = &data->stateid,
6326 };
Andy Adamson938e1012009-04-01 09:22:28 -04006327
Trond Myklebust14516c32010-07-31 14:29:06 -04006328 if (!nfs4_sequence_done(task, &data->res.seq_res))
6329 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006330
Trond Myklebustca8acf82013-08-13 10:36:56 -04006331 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006332
6333 /* Handle Layoutreturn errors */
Trond Myklebust287a9c52019-09-20 07:23:41 -04006334 if (pnfs_roc_done(task, data->inode,
6335 &data->args.lr_args,
6336 &data->res.lr_res,
6337 &data->res.lr_ret) == -EAGAIN)
6338 goto out_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006339
Ricardo Labiaga79708862009-12-07 09:23:21 -05006340 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006341 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006342 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006343 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006344 case -NFS4ERR_ADMIN_REVOKED:
6345 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006346 case -NFS4ERR_EXPIRED:
6347 nfs4_free_revoked_stateid(data->res.server,
6348 data->args.stateid,
6349 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006350 fallthrough;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006351 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006352 case -NFS4ERR_STALE_STATEID:
Trond Myklebust244fcd22019-12-20 10:43:37 -05006353 case -ETIMEDOUT:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006354 task->tk_status = 0;
6355 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006356 case -NFS4ERR_OLD_STATEID:
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006357 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6358 nfs4_stateid_seqid_inc(&data->stateid);
Trond Myklebust70d136b2019-10-26 22:37:40 -04006359 if (data->args.bitmask) {
6360 data->args.bitmask = NULL;
6361 data->res.fattr = NULL;
6362 }
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006363 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006364 case -NFS4ERR_ACCESS:
6365 if (data->args.bitmask) {
6366 data->args.bitmask = NULL;
6367 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006368 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006369 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006370 fallthrough;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006371 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006372 task->tk_status = nfs4_async_handle_exception(task,
6373 data->res.server, task->tk_status,
6374 &exception);
6375 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006376 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006377 }
Trond Myklebustd51f91d2019-10-21 14:22:14 -04006378 nfs_delegation_mark_returned(data->inode, data->args.stateid);
Ricardo Labiaga79708862009-12-07 09:23:21 -05006379 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006380 return;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006381out_restart:
6382 task->tk_status = 0;
6383 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006384}
6385
6386static void nfs4_delegreturn_release(void *calldata)
6387{
Peng Tao039b7562014-07-03 13:05:02 +08006388 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006389 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006390
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006391 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05006392 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006393 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6394 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006395 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006396 nfs_iput_and_deactive(inode);
6397 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006398 kfree(calldata);
6399}
6400
Andy Adamson938e1012009-04-01 09:22:28 -04006401static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6402{
6403 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006404 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006405
6406 d_data = (struct nfs4_delegreturndata *)data;
6407
Trond Myklebust5326de92019-11-13 09:39:36 +01006408 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6409 nfs4_sequence_done(task, &d_data->res.seq_res);
Peng Tao500d7012015-09-22 11:35:22 +08006410 return;
Trond Myklebust5326de92019-11-13 09:39:36 +01006411 }
Peng Tao500d7012015-09-22 11:35:22 +08006412
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006413 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6414 if (lo && !pnfs_layout_is_valid(lo)) {
6415 d_data->args.lr_args = NULL;
6416 d_data->res.lr_res = NULL;
6417 }
6418
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006419 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006420 &d_data->args.seq_args,
6421 &d_data->res.seq_res,
6422 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006423}
Andy Adamson938e1012009-04-01 09:22:28 -04006424
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006425static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006426 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006427 .rpc_call_done = nfs4_delegreturn_done,
6428 .rpc_release = nfs4_delegreturn_release,
6429};
6430
NeilBrowna52458b2018-12-03 11:30:31 +11006431static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006432{
6433 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006434 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006435 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006436 struct rpc_message msg = {
6437 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6438 .rpc_cred = cred,
6439 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006440 struct rpc_task_setup task_setup_data = {
6441 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006442 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006443 .callback_ops = &nfs4_delegreturn_ops,
Trond Myklebustf304a802020-05-11 10:42:04 -04006444 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006445 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006446 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006447
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006448 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006449 if (data == NULL)
6450 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006451 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006452
6453 nfs4_state_protect(server->nfs_client,
6454 NFS_SP4_MACH_CRED_CLEANUP,
6455 &task_setup_data.rpc_client, &msg);
6456
Trond Myklebustfe650402006-01-03 09:55:18 +01006457 data->args.fhandle = &data->fh;
6458 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04006459 data->args.bitmask = server->cache_consistency_bitmask;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04006460 nfs4_bitmask_adjust(data->args.bitmask, inode, server, NULL);
Trond Myklebustfe650402006-01-03 09:55:18 +01006461 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006462 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006463 data->res.fattr = &data->fattr;
6464 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006465 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006466 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006467 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006468 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006469 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006470 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006471 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006472 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006473 if (data->lr.roc) {
6474 data->args.lr_args = &data->lr.arg;
6475 data->res.lr_res = &data->lr.res;
6476 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006477 } else if (data->lr.roc) {
6478 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6479 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006480 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006481
Trond Myklebustc970aa82007-07-14 15:39:59 -04006482 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006483 msg.rpc_argp = &data->args;
6484 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006485 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006486 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006487 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006488 if (!issync)
6489 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006490 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006491 if (status != 0)
6492 goto out;
6493 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006494out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006495 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006496 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497}
6498
NeilBrowna52458b2018-12-03 11:30:31 +11006499int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500{
6501 struct nfs_server *server = NFS_SERVER(inode);
6502 struct nfs4_exception exception = { };
6503 int err;
6504 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006505 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006506 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507 switch (err) {
6508 case -NFS4ERR_STALE_STATEID:
6509 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510 case 0:
6511 return 0;
6512 }
6513 err = nfs4_handle_exception(server, err, &exception);
6514 } while (exception.retry);
6515 return err;
6516}
6517
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6519{
6520 struct inode *inode = state->inode;
6521 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006522 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006523 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006525 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006527 struct nfs_lockt_res res = {
6528 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529 };
6530 struct rpc_message msg = {
6531 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006532 .rpc_argp = &arg,
6533 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534 .rpc_cred = state->owner->so_cred,
6535 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536 struct nfs4_lock_state *lsp;
6537 int status;
6538
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006539 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006540 status = nfs4_set_lock_state(state, request);
6541 if (status != 0)
6542 goto out;
6543 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006544 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006545 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006546 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006547 switch (status) {
6548 case 0:
6549 request->fl_type = F_UNLCK;
6550 break;
6551 case -NFS4ERR_DENIED:
6552 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006554 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006555 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006556out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557 return status;
6558}
6559
6560static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6561{
Trond Myklebust0688e642019-04-07 13:59:09 -04006562 struct nfs4_exception exception = {
6563 .interruptible = true,
6564 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565 int err;
6566
6567 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006568 err = _nfs4_proc_getlk(state, cmd, request);
6569 trace_nfs4_get_lock(request, state, cmd, err);
6570 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571 &exception);
6572 } while (exception.retry);
6573 return err;
6574}
6575
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006576/*
6577 * Update the seqid of a lock stateid after receiving
6578 * NFS4ERR_OLD_STATEID
6579 */
6580static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6581 struct nfs4_lock_state *lsp)
6582{
6583 struct nfs4_state *state = lsp->ls_state;
6584 bool ret = false;
6585
6586 spin_lock(&state->state_lock);
6587 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6588 goto out;
6589 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6590 nfs4_stateid_seqid_inc(dst);
6591 else
6592 dst->seqid = lsp->ls_stateid.seqid;
6593 ret = true;
6594out:
6595 spin_unlock(&state->state_lock);
6596 return ret;
6597}
6598
6599static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6600 struct nfs4_lock_state *lsp)
6601{
6602 struct nfs4_state *state = lsp->ls_state;
6603 bool ret;
6604
6605 spin_lock(&state->state_lock);
6606 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6607 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6608 spin_unlock(&state->state_lock);
6609 return ret;
6610}
6611
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006612struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006613 struct nfs_locku_args arg;
6614 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006615 struct nfs4_lock_state *lsp;
6616 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006617 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006618 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006619 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006620 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006621};
6622
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006623static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6624 struct nfs_open_context *ctx,
6625 struct nfs4_lock_state *lsp,
6626 struct nfs_seqid *seqid)
6627{
6628 struct nfs4_unlockdata *p;
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006629 struct nfs4_state *state = lsp->ls_state;
6630 struct inode *inode = state->inode;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006631
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006632 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006633 if (p == NULL)
6634 return NULL;
6635 p->arg.fh = NFS_FH(inode);
6636 p->arg.fl = &p->fl;
6637 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006638 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006639 p->lsp = lsp;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006640 /* Ensure we don't close file until we're done freeing locks! */
6641 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006642 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006643 locks_init_lock(&p->fl);
6644 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006645 p->server = NFS_SERVER(inode);
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006646 spin_lock(&state->state_lock);
6647 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6648 spin_unlock(&state->state_lock);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006649 return p;
6650}
6651
Trond Myklebust06f814a2006-01-03 09:55:07 +01006652static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006653{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006654 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006655 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006656 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006657 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006658 put_nfs_open_context(calldata->ctx);
6659 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006660}
6661
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006662static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006663{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006664 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006665 struct nfs4_exception exception = {
6666 .inode = calldata->lsp->ls_state->inode,
6667 .stateid = &calldata->arg.stateid,
6668 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006669
Trond Myklebust14516c32010-07-31 14:29:06 -04006670 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6671 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006672 switch (task->tk_status) {
6673 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006674 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006675 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006676 if (nfs4_update_lock_stateid(calldata->lsp,
6677 &calldata->res.stateid))
6678 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006679 fallthrough;
Trond Myklebust26d36302016-09-22 13:39:05 -04006680 case -NFS4ERR_ADMIN_REVOKED:
6681 case -NFS4ERR_EXPIRED:
6682 nfs4_free_revoked_stateid(calldata->server,
6683 &calldata->arg.stateid,
6684 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006685 fallthrough;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006686 case -NFS4ERR_BAD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006687 case -NFS4ERR_STALE_STATEID:
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006688 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6689 calldata->lsp))
6690 rpc_restart_call_prepare(task);
6691 break;
6692 case -NFS4ERR_OLD_STATEID:
6693 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6694 calldata->lsp))
Trond Myklebust425c1d42015-01-24 14:57:53 -05006695 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006696 break;
6697 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006698 task->tk_status = nfs4_async_handle_exception(task,
6699 calldata->server, task->tk_status,
6700 &exception);
6701 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006702 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006703 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006704 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006705}
6706
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006707static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006708{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006709 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006711 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6712 nfs_async_iocounter_wait(task, calldata->l_ctx))
6713 return;
6714
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006715 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006716 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04006717 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006718 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006719 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006720 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006721 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006722 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006723 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006724 &calldata->res.seq_res,
6725 task) != 0)
6726 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006727 return;
6728out_no_action:
6729 task->tk_action = NULL;
6730out_wait:
6731 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732}
6733
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006734static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006735 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006736 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006737 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006738};
6739
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006740static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6741 struct nfs_open_context *ctx,
6742 struct nfs4_lock_state *lsp,
6743 struct nfs_seqid *seqid)
6744{
6745 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006746 struct rpc_message msg = {
6747 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6748 .rpc_cred = ctx->cred,
6749 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006750 struct rpc_task_setup task_setup_data = {
6751 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006752 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006753 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006754 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006755 .flags = RPC_TASK_ASYNC,
6756 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006757
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006758 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6759 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6760
Frank Filz137d6ac2007-07-09 15:32:29 -07006761 /* Ensure this is an unlock - when canceling a lock, the
6762 * canceled lock is passed in, and it won't be an unlock.
6763 */
6764 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006765 if (fl->fl_flags & FL_CLOSE)
6766 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006767
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006768 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6769 if (data == NULL) {
6770 nfs_free_seqid(seqid);
6771 return ERR_PTR(-ENOMEM);
6772 }
6773
Anna Schumakerfba83f32018-05-04 16:22:50 -04006774 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006775 msg.rpc_argp = &data->arg;
6776 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006777 task_setup_data.callback_data = data;
6778 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006779}
6780
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6782{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006783 struct inode *inode = state->inode;
6784 struct nfs4_state_owner *sp = state->owner;
6785 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006786 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006787 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006788 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006789 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006790 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006791 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792
Trond Myklebust9b073572006-06-29 16:38:34 -04006793 status = nfs4_set_lock_state(state, request);
6794 /* Unlock _before_ we do the RPC call */
6795 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006796 /* Exclude nfs_delegation_claim_locks() */
6797 mutex_lock(&sp->so_delegreturn_mutex);
6798 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006799 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006800 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006801 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006802 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006803 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006804 }
6805 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006806 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006807 if (status != 0)
6808 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006809 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006810 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006811 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6812 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006813 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6814 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006815 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006816 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006817 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006818 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006819 status = PTR_ERR(task);
6820 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006821 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006822 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006823 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006824out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006825 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006826 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006827 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828}
6829
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006830struct nfs4_lockdata {
6831 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006832 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006833 struct nfs4_lock_state *lsp;
6834 struct nfs_open_context *ctx;
6835 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006836 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006837 int rpc_status;
6838 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006839 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006840};
6841
6842static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006843 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6844 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006845{
6846 struct nfs4_lockdata *p;
6847 struct inode *inode = lsp->ls_state->inode;
6848 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006849 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006850
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006851 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006852 if (p == NULL)
6853 return NULL;
6854
6855 p->arg.fh = NFS_FH(inode);
6856 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006857 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006858 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006859 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006860 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6861 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006862 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006863 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006864 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006865 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006866 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006867 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006868 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006869 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006870 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006871 locks_init_lock(&p->fl);
6872 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006873 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006874out_free_seqid:
6875 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006876out_free:
6877 kfree(p);
6878 return NULL;
6879}
6880
6881static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6882{
6883 struct nfs4_lockdata *data = calldata;
6884 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885
Harvey Harrison3110ff82008-05-02 13:42:44 -07006886 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006887 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006888 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006889 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006890 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006891 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006892 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006893 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006894 nfs4_stateid_copy(&data->arg.open_stateid,
6895 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006896 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006897 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006898 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006899 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006900 nfs4_stateid_copy(&data->arg.lock_stateid,
6901 &data->lsp->ls_stateid);
6902 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006903 if (!nfs4_valid_open_stateid(state)) {
6904 data->rpc_status = -EBADF;
6905 task->tk_action = NULL;
6906 goto out_release_open_seqid;
6907 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006908 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006909 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006910 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006911 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006912 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006913 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006914out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006915 nfs_release_seqid(data->arg.open_seqid);
6916out_release_lock_seqid:
6917 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006918out_wait:
6919 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006920 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006921}
6922
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006923static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6924{
6925 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006926 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006927
Harvey Harrison3110ff82008-05-02 13:42:44 -07006928 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006929
Trond Myklebust14516c32010-07-31 14:29:06 -04006930 if (!nfs4_sequence_done(task, &data->res.seq_res))
6931 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006932
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006933 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006934 switch (task->tk_status) {
6935 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006936 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006937 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006938 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006939 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006940 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006941 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006942 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006943 if (data->arg.new_lock_owner != 0) {
6944 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6945 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6946 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006947 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6948 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006949 break;
6950 case -NFS4ERR_BAD_STATEID:
6951 case -NFS4ERR_OLD_STATEID:
6952 case -NFS4ERR_STALE_STATEID:
6953 case -NFS4ERR_EXPIRED:
6954 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006955 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006956 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006957 goto out_restart;
6958 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006959 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006960 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006961 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006962out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07006963 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006964 return;
6965out_restart:
6966 if (!data->cancelled)
6967 rpc_restart_call_prepare(task);
6968 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006969}
6970
6971static void nfs4_lock_release(void *calldata)
6972{
6973 struct nfs4_lockdata *data = calldata;
6974
Harvey Harrison3110ff82008-05-02 13:42:44 -07006975 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006976 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006977 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006978 struct rpc_task *task;
6979 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6980 data->arg.lock_seqid);
6981 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006982 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006983 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006984 } else
6985 nfs_free_seqid(data->arg.lock_seqid);
6986 nfs4_put_lock_state(data->lsp);
6987 put_nfs_open_context(data->ctx);
6988 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006989 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006990}
6991
6992static const struct rpc_call_ops nfs4_lock_ops = {
6993 .rpc_call_prepare = nfs4_lock_prepare,
6994 .rpc_call_done = nfs4_lock_done,
6995 .rpc_release = nfs4_lock_release,
6996};
6997
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006998static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6999{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007000 switch (error) {
7001 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04007002 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007003 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05007004 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007005 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04007006 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05007007 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05007008 break;
7009 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05007010 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05007011 nfs4_schedule_lease_recovery(server->nfs_client);
zhengbin8b98a532019-12-19 18:34:47 +08007012 }
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007013}
7014
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007015static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007016{
7017 struct nfs4_lockdata *data;
7018 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04007019 struct rpc_message msg = {
7020 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7021 .rpc_cred = state->owner->so_cred,
7022 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04007023 struct rpc_task_setup task_setup_data = {
7024 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04007025 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04007026 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05007027 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05007028 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04007029 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007030 int ret;
7031
Harvey Harrison3110ff82008-05-02 13:42:44 -07007032 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007033 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007034 fl->fl_u.nfs4_fl.owner,
7035 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007036 if (data == NULL)
7037 return -ENOMEM;
7038 if (IS_SETLKW(cmd))
7039 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007040 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7041 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05007042 msg.rpc_argp = &data->arg;
7043 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04007044 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007045 if (recovery_type > NFS_LOCK_NEW) {
7046 if (recovery_type == NFS_LOCK_RECLAIM)
7047 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05007048 } else
7049 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04007050 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05007051 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007052 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05007053 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007054 if (ret == 0) {
7055 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007056 if (ret)
7057 nfs4_handle_setlk_error(data->server, data->lsp,
7058 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007059 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04007060 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05007061 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007062 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05007063 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007064 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065}
7066
7067static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7068{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007069 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007070 struct nfs4_exception exception = {
7071 .inode = state->inode,
7072 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007073 int err;
7074
7075 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007076 /* Cache the lock if possible... */
7077 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7078 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007079 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04007080 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00007081 break;
7082 nfs4_handle_exception(server, err, &exception);
7083 } while (exception.retry);
7084 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085}
7086
7087static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7088{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007089 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007090 struct nfs4_exception exception = {
7091 .inode = state->inode,
7092 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007093 int err;
7094
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05007095 err = nfs4_set_lock_state(state, request);
7096 if (err != 0)
7097 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04007098 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10007099 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7100 return 0;
7101 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007102 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007103 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7104 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007105 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007106 switch (err) {
7107 default:
7108 goto out;
7109 case -NFS4ERR_GRACE:
7110 case -NFS4ERR_DELAY:
7111 nfs4_handle_exception(server, err, &exception);
7112 err = 0;
7113 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007114 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007115out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00007116 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117}
7118
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007119#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007120static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7121{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007122 struct nfs4_lock_state *lsp;
7123 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007124
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007125 status = nfs4_set_lock_state(state, request);
7126 if (status != 0)
7127 return status;
7128 lsp = request->fl_u.nfs4_fl.owner;
7129 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7130 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7131 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05007132 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007133}
7134#endif
7135
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7137{
Trond Myklebust19e03c52008-12-23 15:21:44 -05007138 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04007139 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007140 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007141 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142
Trond Myklebust01c3b862006-06-29 16:38:39 -04007143 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04007144 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007145 if (status < 0)
7146 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04007147 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05007148 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007149 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04007150 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04007151 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05007152 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04007153 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007154 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007155 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007156 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007157 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05007158 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007159 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007160 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007161out:
7162 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163 return status;
7164}
7165
7166static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7167{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007168 struct nfs4_exception exception = {
7169 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04007170 .inode = state->inode,
Trond Myklebust0688e642019-04-07 13:59:09 -04007171 .interruptible = true,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007172 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173 int err;
7174
7175 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07007176 err = _nfs4_proc_setlk(state, cmd, request);
7177 if (err == -NFS4ERR_DENIED)
7178 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07007180 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181 } while (exception.retry);
7182 return err;
7183}
7184
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007185#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7186#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7187
7188static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04007189nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7190 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007191{
7192 int status = -ERESTARTSYS;
7193 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7194
7195 while(!signalled()) {
7196 status = nfs4_proc_setlk(state, cmd, request);
7197 if ((status != -EAGAIN) || IS_SETLK(cmd))
7198 break;
7199 freezable_schedule_timeout_interruptible(timeout);
7200 timeout *= 2;
7201 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7202 status = -ERESTARTSYS;
7203 }
7204 return status;
7205}
7206
Jeff Laytona1d617d82016-09-17 18:17:39 -04007207#ifdef CONFIG_NFS_V4_1
7208struct nfs4_lock_waiter {
7209 struct task_struct *task;
7210 struct inode *inode;
7211 struct nfs_lowner *owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007212};
7213
7214static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02007215nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04007216{
7217 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007218 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007219
Jeff Layton57174592018-03-18 08:37:03 -04007220 /* NULL key means to wake up everyone */
7221 if (key) {
7222 struct cb_notify_lock_args *cbnl = key;
7223 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7224 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007225
Jeff Layton57174592018-03-18 08:37:03 -04007226 /* Only wake if the callback was for the same owner. */
7227 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7228 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007229
Jeff Layton57174592018-03-18 08:37:03 -04007230 /* Make sure it's for the right inode */
7231 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7232 return 0;
Jeff Layton57174592018-03-18 08:37:03 -04007233 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007234
7235 /* override "private" so we can use default_wake_function */
7236 wait->private = waiter->task;
Yihao Wu52b042a2019-05-22 01:57:10 +08007237 ret = woken_wake_function(wait, mode, flags, key);
7238 if (ret)
7239 list_del_init(&wait->entry);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007240 wait->private = waiter;
7241 return ret;
7242}
7243
7244static int
7245nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7246{
7247 int status = -ERESTARTSYS;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007248 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7249 struct nfs_server *server = NFS_SERVER(state->inode);
7250 struct nfs_client *clp = server->nfs_client;
7251 wait_queue_head_t *q = &clp->cl_lock_waitq;
7252 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7253 .id = lsp->ls_seqid.owner_id,
7254 .s_dev = server->s_dev };
7255 struct nfs4_lock_waiter waiter = { .task = current,
7256 .inode = state->inode,
Yihao Wu52b042a2019-05-22 01:57:10 +08007257 .owner = &owner};
Ingo Molnarac6424b2017-06-20 12:06:13 +02007258 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007259
7260 /* Don't bother with waitqueue if we don't expect a callback */
7261 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7262 return nfs4_retry_setlk_simple(state, cmd, request);
7263
7264 init_wait(&wait);
7265 wait.private = &waiter;
7266 wait.func = nfs4_wake_lock_waiter;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007267
7268 while(!signalled()) {
Yihao Wuba851a32019-05-13 14:58:22 +08007269 add_wait_queue(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007270 status = nfs4_proc_setlk(state, cmd, request);
Yihao Wuba851a32019-05-13 14:58:22 +08007271 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7272 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007273 break;
Yihao Wuba851a32019-05-13 14:58:22 +08007274 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007275
7276 status = -ERESTARTSYS;
Yihao Wu52b042a2019-05-22 01:57:10 +08007277 freezer_do_not_count();
7278 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7279 freezer_count();
Yihao Wuba851a32019-05-13 14:58:22 +08007280 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007281 }
7282
Jeff Laytona1d617d82016-09-17 18:17:39 -04007283 return status;
7284}
7285#else /* !CONFIG_NFS_V4_1 */
7286static inline int
7287nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7288{
7289 return nfs4_retry_setlk_simple(state, cmd, request);
7290}
7291#endif
7292
Linus Torvalds1da177e2005-04-16 15:20:36 -07007293static int
7294nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7295{
7296 struct nfs_open_context *ctx;
7297 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007298 int status;
7299
7300 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007301 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007302 state = ctx->state;
7303
Trond Myklebustd9531262009-07-21 19:22:38 -04007304 if (IS_GETLK(cmd)) {
7305 if (state != NULL)
7306 return nfs4_proc_getlk(state, F_GETLK, request);
7307 return 0;
7308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007309
7310 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7311 return -EINVAL;
7312
Trond Myklebustd9531262009-07-21 19:22:38 -04007313 if (request->fl_type == F_UNLCK) {
7314 if (state != NULL)
7315 return nfs4_proc_unlck(state, cmd, request);
7316 return 0;
7317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318
Trond Myklebustd9531262009-07-21 19:22:38 -04007319 if (state == NULL)
7320 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007321
7322 if ((request->fl_flags & FL_POSIX) &&
7323 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7324 return -ENOLCK;
7325
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05007326 /*
7327 * Don't rely on the VFS having checked the file open mode,
7328 * since it won't do this for flock() locks.
7329 */
7330 switch (request->fl_type) {
7331 case F_RDLCK:
7332 if (!(filp->f_mode & FMODE_READ))
7333 return -EBADF;
7334 break;
7335 case F_WRLCK:
7336 if (!(filp->f_mode & FMODE_WRITE))
7337 return -EBADF;
7338 }
7339
Jeff Layton1ea67db2016-09-17 18:17:37 -04007340 status = nfs4_set_lock_state(state, request);
7341 if (status != 0)
7342 return status;
7343
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007344 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007345}
7346
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007347int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007348{
7349 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007350 int err;
7351
7352 err = nfs4_set_lock_state(state, fl);
7353 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007354 return err;
Olga Kornievskaia3d7a9522020-08-20 18:52:43 -04007355 do {
7356 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7357 if (err != -NFS4ERR_DELAY)
7358 break;
7359 ssleep(1);
7360 } while (err == -NFS4ERR_DELAY);
NeilBrowndce26302017-12-13 09:57:09 +11007361 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007362}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007363
Trond Myklebustcf470c32012-03-07 13:49:12 -05007364struct nfs_release_lockowner_data {
7365 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007366 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007367 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007368 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007369 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007370};
7371
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007372static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7373{
7374 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007375 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007376 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7377 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007378 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007379 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007380}
7381
7382static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7383{
7384 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007385 struct nfs_server *server = data->server;
7386
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007387 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007388
7389 switch (task->tk_status) {
7390 case 0:
7391 renew_lease(server, data->timestamp);
7392 break;
7393 case -NFS4ERR_STALE_CLIENTID:
7394 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007395 nfs4_schedule_lease_recovery(server->nfs_client);
7396 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007397 case -NFS4ERR_LEASE_MOVED:
7398 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007399 if (nfs4_async_handle_error(task, server,
7400 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007401 rpc_restart_call_prepare(task);
7402 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007403}
7404
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007405static void nfs4_release_lockowner_release(void *calldata)
7406{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007407 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007408 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007409 kfree(calldata);
7410}
7411
Trond Myklebust17280172012-03-11 13:11:00 -04007412static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007413 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7414 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007415 .rpc_release = nfs4_release_lockowner_release,
7416};
7417
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007418static void
7419nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007420{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007421 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007422 struct rpc_message msg = {
7423 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7424 };
7425
7426 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007427 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007428
Trond Myklebustcf470c32012-03-07 13:49:12 -05007429 data = kmalloc(sizeof(*data), GFP_NOFS);
7430 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007431 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007432 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007433 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007434 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7435 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7436 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007437
Trond Myklebustcf470c32012-03-07 13:49:12 -05007438 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007439 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007440 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007441 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007442}
7443
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007444#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7445
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007446static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007447 struct dentry *unused, struct inode *inode,
7448 const char *key, const void *buf,
7449 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007450{
Al Viro59301222016-05-27 10:19:30 -04007451 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007452}
7453
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007454static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007455 struct dentry *unused, struct inode *inode,
7456 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007457{
Al Virob2968212016-04-10 20:48:24 -04007458 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007459}
7460
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007461static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007462{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007463 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007464}
7465
David Quigleyc9bccef2013-05-22 12:50:45 -04007466#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007467
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007468static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007469 struct dentry *unused, struct inode *inode,
7470 const char *key, const void *buf,
7471 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007472{
7473 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007474 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007475
7476 return -EOPNOTSUPP;
7477}
7478
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007479static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007480 struct dentry *unused, struct inode *inode,
7481 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007482{
7483 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007484 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007485 return -EOPNOTSUPP;
7486}
7487
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007488static ssize_t
7489nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007490{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007491 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007492
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007493 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7494 len = security_inode_listsecurity(inode, list, list_len);
Chengguang Xu82c596e2020-06-17 09:09:39 +08007495 if (len >= 0 && list_len && len > list_len)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007496 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007497 }
7498 return len;
7499}
7500
7501static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7502 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007503 .get = nfs4_xattr_get_nfs4_label,
7504 .set = nfs4_xattr_set_nfs4_label,
7505};
David Quigleyc9bccef2013-05-22 12:50:45 -04007506
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007507#else
7508
7509static ssize_t
7510nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7511{
7512 return 0;
7513}
7514
7515#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007516
Frank van der Linden012a2112020-06-23 22:39:03 +00007517#ifdef CONFIG_NFS_V4_2
7518static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7519 struct dentry *unused, struct inode *inode,
7520 const char *key, const void *buf,
7521 size_t buflen, int flags)
7522{
7523 struct nfs_access_entry cache;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007524 int ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007525
7526 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7527 return -EOPNOTSUPP;
7528
7529 /*
7530 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7531 * flags right now. Handling of xattr operations use the normal
7532 * file read/write permissions.
7533 *
7534 * Just in case the server has other ideas (which RFC 8276 allows),
7535 * do a cached access check for the XA* flags to possibly avoid
7536 * doing an RPC and getting EACCES back.
7537 */
7538 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7539 if (!(cache.mask & NFS_ACCESS_XAWRITE))
7540 return -EACCES;
7541 }
7542
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007543 if (buf == NULL) {
7544 ret = nfs42_proc_removexattr(inode, key);
7545 if (!ret)
7546 nfs4_xattr_cache_remove(inode, key);
7547 } else {
7548 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7549 if (!ret)
7550 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7551 }
7552
7553 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007554}
7555
7556static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7557 struct dentry *unused, struct inode *inode,
7558 const char *key, void *buf, size_t buflen)
7559{
7560 struct nfs_access_entry cache;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007561 ssize_t ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007562
7563 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7564 return -EOPNOTSUPP;
7565
7566 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7567 if (!(cache.mask & NFS_ACCESS_XAREAD))
7568 return -EACCES;
7569 }
7570
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007571 ret = nfs_revalidate_inode(NFS_SERVER(inode), inode);
7572 if (ret)
7573 return ret;
7574
7575 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7576 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7577 return ret;
7578
7579 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7580
7581 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007582}
7583
7584static ssize_t
7585nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7586{
7587 u64 cookie;
7588 bool eof;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007589 ssize_t ret, size;
Frank van der Linden012a2112020-06-23 22:39:03 +00007590 char *buf;
7591 size_t buflen;
7592 struct nfs_access_entry cache;
7593
7594 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7595 return 0;
7596
7597 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7598 if (!(cache.mask & NFS_ACCESS_XALIST))
7599 return 0;
7600 }
7601
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007602 ret = nfs_revalidate_inode(NFS_SERVER(inode), inode);
7603 if (ret)
7604 return ret;
7605
7606 ret = nfs4_xattr_cache_list(inode, list, list_len);
7607 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7608 return ret;
7609
Frank van der Linden012a2112020-06-23 22:39:03 +00007610 cookie = 0;
7611 eof = false;
7612 buflen = list_len ? list_len : XATTR_LIST_MAX;
7613 buf = list_len ? list : NULL;
7614 size = 0;
7615
7616 while (!eof) {
7617 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7618 &cookie, &eof);
7619 if (ret < 0)
7620 return ret;
7621
7622 if (list_len) {
7623 buf += ret;
7624 buflen -= ret;
7625 }
7626 size += ret;
7627 }
7628
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007629 if (list_len)
7630 nfs4_xattr_cache_set_list(inode, list, size);
7631
Frank van der Linden012a2112020-06-23 22:39:03 +00007632 return size;
7633}
7634
7635#else
7636
7637static ssize_t
7638nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7639{
7640 return 0;
7641}
7642#endif /* CONFIG_NFS_V4_2 */
7643
Andy Adamson533eb462011-06-13 18:25:56 -04007644/*
7645 * nfs_fhget will use either the mounted_on_fileid or the fileid
7646 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007647static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7648{
Andy Adamson533eb462011-06-13 18:25:56 -04007649 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7650 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7651 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007652 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007653 return;
7654
7655 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007656 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007657 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7658 fattr->nlink = 2;
7659}
7660
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007661static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7662 const struct qstr *name,
7663 struct nfs4_fs_locations *fs_locations,
7664 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007665{
7666 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007667 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007668 struct nfs4_fs_locations_arg args = {
7669 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007670 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007671 .page = page,
7672 .bitmask = bitmask,
7673 };
Benny Halevy22958462009-04-01 09:22:02 -04007674 struct nfs4_fs_locations_res res = {
7675 .fs_locations = fs_locations,
7676 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007677 struct rpc_message msg = {
7678 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7679 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007680 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007681 };
7682 int status;
7683
Harvey Harrison3110ff82008-05-02 13:42:44 -07007684 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007685
Chuck Leverc05cefc2017-11-05 15:45:22 -05007686 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7687 bitmask[1] = nfs4_fattr_bitmap[1];
7688
Andy Adamson533eb462011-06-13 18:25:56 -04007689 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7690 * is not supported */
7691 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007692 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007693 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007694 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007695
Trond Myklebustc228fd32007-01-13 02:28:11 -05007696 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007697 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007698 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007699 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007700 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007701 return status;
7702}
7703
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007704int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7705 const struct qstr *name,
7706 struct nfs4_fs_locations *fs_locations,
7707 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007708{
Trond Myklebust0688e642019-04-07 13:59:09 -04007709 struct nfs4_exception exception = {
7710 .interruptible = true,
7711 };
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007712 int err;
7713 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007714 err = _nfs4_proc_fs_locations(client, dir, name,
7715 fs_locations, page);
7716 trace_nfs4_get_fs_locations(dir, name, err);
7717 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007718 &exception);
7719 } while (exception.retry);
7720 return err;
7721}
7722
Chuck Leverb03d7352013-10-17 14:12:50 -04007723/*
7724 * This operation also signals the server that this client is
7725 * performing migration recovery. The server can stop returning
7726 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7727 * appended to this compound to identify the client ID which is
7728 * performing recovery.
7729 */
7730static int _nfs40_proc_get_locations(struct inode *inode,
7731 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007732 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007733{
7734 struct nfs_server *server = NFS_SERVER(inode);
7735 struct rpc_clnt *clnt = server->client;
7736 u32 bitmask[2] = {
7737 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7738 };
7739 struct nfs4_fs_locations_arg args = {
7740 .clientid = server->nfs_client->cl_clientid,
7741 .fh = NFS_FH(inode),
7742 .page = page,
7743 .bitmask = bitmask,
7744 .migration = 1, /* skip LOOKUP */
7745 .renew = 1, /* append RENEW */
7746 };
7747 struct nfs4_fs_locations_res res = {
7748 .fs_locations = locations,
7749 .migration = 1,
7750 .renew = 1,
7751 };
7752 struct rpc_message msg = {
7753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7754 .rpc_argp = &args,
7755 .rpc_resp = &res,
7756 .rpc_cred = cred,
7757 };
7758 unsigned long now = jiffies;
7759 int status;
7760
7761 nfs_fattr_init(&locations->fattr);
7762 locations->server = server;
7763 locations->nlocations = 0;
7764
Anna Schumakerfba83f32018-05-04 16:22:50 -04007765 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007766 status = nfs4_call_sync_sequence(clnt, server, &msg,
7767 &args.seq_args, &res.seq_res);
7768 if (status)
7769 return status;
7770
7771 renew_lease(server, now);
7772 return 0;
7773}
7774
7775#ifdef CONFIG_NFS_V4_1
7776
7777/*
7778 * This operation also signals the server that this client is
7779 * performing migration recovery. The server can stop asserting
7780 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7781 * performing this operation is identified in the SEQUENCE
7782 * operation in this compound.
7783 *
7784 * When the client supports GETATTR(fs_locations_info), it can
7785 * be plumbed in here.
7786 */
7787static int _nfs41_proc_get_locations(struct inode *inode,
7788 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007789 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007790{
7791 struct nfs_server *server = NFS_SERVER(inode);
7792 struct rpc_clnt *clnt = server->client;
7793 u32 bitmask[2] = {
7794 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7795 };
7796 struct nfs4_fs_locations_arg args = {
7797 .fh = NFS_FH(inode),
7798 .page = page,
7799 .bitmask = bitmask,
7800 .migration = 1, /* skip LOOKUP */
7801 };
7802 struct nfs4_fs_locations_res res = {
7803 .fs_locations = locations,
7804 .migration = 1,
7805 };
7806 struct rpc_message msg = {
7807 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7808 .rpc_argp = &args,
7809 .rpc_resp = &res,
7810 .rpc_cred = cred,
7811 };
7812 int status;
7813
7814 nfs_fattr_init(&locations->fattr);
7815 locations->server = server;
7816 locations->nlocations = 0;
7817
Anna Schumakerfba83f32018-05-04 16:22:50 -04007818 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007819 status = nfs4_call_sync_sequence(clnt, server, &msg,
7820 &args.seq_args, &res.seq_res);
7821 if (status == NFS4_OK &&
7822 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7823 status = -NFS4ERR_LEASE_MOVED;
7824 return status;
7825}
7826
7827#endif /* CONFIG_NFS_V4_1 */
7828
7829/**
7830 * nfs4_proc_get_locations - discover locations for a migrated FSID
7831 * @inode: inode on FSID that is migrating
7832 * @locations: result of query
7833 * @page: buffer
7834 * @cred: credential to use for this operation
7835 *
7836 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7837 * operation failed, or a negative errno if a local error occurred.
7838 *
7839 * On success, "locations" is filled in, but if the server has
7840 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7841 * asserted.
7842 *
7843 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7844 * from this client that require migration recovery.
7845 */
7846int nfs4_proc_get_locations(struct inode *inode,
7847 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007848 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007849{
7850 struct nfs_server *server = NFS_SERVER(inode);
7851 struct nfs_client *clp = server->nfs_client;
7852 const struct nfs4_mig_recovery_ops *ops =
7853 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007854 struct nfs4_exception exception = {
7855 .interruptible = true,
7856 };
Chuck Leverb03d7352013-10-17 14:12:50 -04007857 int status;
7858
7859 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7860 (unsigned long long)server->fsid.major,
7861 (unsigned long long)server->fsid.minor,
7862 clp->cl_hostname);
7863 nfs_display_fhandle(NFS_FH(inode), __func__);
7864
7865 do {
7866 status = ops->get_locations(inode, locations, page, cred);
7867 if (status != -NFS4ERR_DELAY)
7868 break;
7869 nfs4_handle_exception(server, status, &exception);
7870 } while (exception.retry);
7871 return status;
7872}
7873
Chuck Lever44c99932013-10-17 14:13:30 -04007874/*
7875 * This operation also signals the server that this client is
7876 * performing "lease moved" recovery. The server can stop
7877 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7878 * is appended to this compound to identify the client ID which is
7879 * performing recovery.
7880 */
NeilBrowna52458b2018-12-03 11:30:31 +11007881static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007882{
7883 struct nfs_server *server = NFS_SERVER(inode);
7884 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7885 struct rpc_clnt *clnt = server->client;
7886 struct nfs4_fsid_present_arg args = {
7887 .fh = NFS_FH(inode),
7888 .clientid = clp->cl_clientid,
7889 .renew = 1, /* append RENEW */
7890 };
7891 struct nfs4_fsid_present_res res = {
7892 .renew = 1,
7893 };
7894 struct rpc_message msg = {
7895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7896 .rpc_argp = &args,
7897 .rpc_resp = &res,
7898 .rpc_cred = cred,
7899 };
7900 unsigned long now = jiffies;
7901 int status;
7902
7903 res.fh = nfs_alloc_fhandle();
7904 if (res.fh == NULL)
7905 return -ENOMEM;
7906
Anna Schumakerfba83f32018-05-04 16:22:50 -04007907 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007908 status = nfs4_call_sync_sequence(clnt, server, &msg,
7909 &args.seq_args, &res.seq_res);
7910 nfs_free_fhandle(res.fh);
7911 if (status)
7912 return status;
7913
7914 do_renew_lease(clp, now);
7915 return 0;
7916}
7917
7918#ifdef CONFIG_NFS_V4_1
7919
7920/*
7921 * This operation also signals the server that this client is
7922 * performing "lease moved" recovery. The server can stop asserting
7923 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7924 * this operation is identified in the SEQUENCE operation in this
7925 * compound.
7926 */
NeilBrowna52458b2018-12-03 11:30:31 +11007927static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007928{
7929 struct nfs_server *server = NFS_SERVER(inode);
7930 struct rpc_clnt *clnt = server->client;
7931 struct nfs4_fsid_present_arg args = {
7932 .fh = NFS_FH(inode),
7933 };
7934 struct nfs4_fsid_present_res res = {
7935 };
7936 struct rpc_message msg = {
7937 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7938 .rpc_argp = &args,
7939 .rpc_resp = &res,
7940 .rpc_cred = cred,
7941 };
7942 int status;
7943
7944 res.fh = nfs_alloc_fhandle();
7945 if (res.fh == NULL)
7946 return -ENOMEM;
7947
Anna Schumakerfba83f32018-05-04 16:22:50 -04007948 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007949 status = nfs4_call_sync_sequence(clnt, server, &msg,
7950 &args.seq_args, &res.seq_res);
7951 nfs_free_fhandle(res.fh);
7952 if (status == NFS4_OK &&
7953 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7954 status = -NFS4ERR_LEASE_MOVED;
7955 return status;
7956}
7957
7958#endif /* CONFIG_NFS_V4_1 */
7959
7960/**
7961 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7962 * @inode: inode on FSID to check
7963 * @cred: credential to use for this operation
7964 *
7965 * Server indicates whether the FSID is present, moved, or not
7966 * recognized. This operation is necessary to clear a LEASE_MOVED
7967 * condition for this client ID.
7968 *
7969 * Returns NFS4_OK if the FSID is present on this server,
7970 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7971 * NFS4ERR code if some error occurred on the server, or a
7972 * negative errno if a local failure occurred.
7973 */
NeilBrowna52458b2018-12-03 11:30:31 +11007974int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007975{
7976 struct nfs_server *server = NFS_SERVER(inode);
7977 struct nfs_client *clp = server->nfs_client;
7978 const struct nfs4_mig_recovery_ops *ops =
7979 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007980 struct nfs4_exception exception = {
7981 .interruptible = true,
7982 };
Chuck Lever44c99932013-10-17 14:13:30 -04007983 int status;
7984
7985 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7986 (unsigned long long)server->fsid.major,
7987 (unsigned long long)server->fsid.minor,
7988 clp->cl_hostname);
7989 nfs_display_fhandle(NFS_FH(inode), __func__);
7990
7991 do {
7992 status = ops->fsid_present(inode, cred);
7993 if (status != -NFS4ERR_DELAY)
7994 break;
7995 nfs4_handle_exception(server, status, &exception);
7996 } while (exception.retry);
7997 return status;
7998}
7999
Trond Myklebust302fad72019-02-18 13:32:38 -05008000/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008001 * If 'use_integrity' is true and the state managment nfs_client
8002 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8003 * and the machine credential as per RFC3530bis and RFC5661 Security
8004 * Considerations sections. Otherwise, just use the user cred with the
8005 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04008006 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008007static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008008{
8009 int status;
Anna Schumaker50493362019-08-14 15:30:16 -04008010 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8011 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008012 struct nfs4_secinfo_arg args = {
8013 .dir_fh = NFS_FH(dir),
8014 .name = name,
8015 };
8016 struct nfs4_secinfo_res res = {
8017 .flavors = flavors,
8018 };
8019 struct rpc_message msg = {
8020 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8021 .rpc_argp = &args,
8022 .rpc_resp = &res,
8023 };
Anna Schumaker50493362019-08-14 15:30:16 -04008024 struct nfs4_call_sync_data data = {
8025 .seq_server = NFS_SERVER(dir),
8026 .seq_args = &args.seq_args,
8027 .seq_res = &res.seq_res,
8028 };
8029 struct rpc_task_setup task_setup = {
8030 .rpc_client = clnt,
8031 .rpc_message = &msg,
8032 .callback_ops = clp->cl_mvops->call_sync_ops,
8033 .callback_data = &data,
8034 .flags = RPC_TASK_NO_ROUND_ROBIN,
8035 };
NeilBrowna52458b2018-12-03 11:30:31 +11008036 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008037
8038 if (use_integrity) {
Anna Schumaker50493362019-08-14 15:30:16 -04008039 clnt = clp->cl_rpcclient;
8040 task_setup.rpc_client = clnt;
8041
8042 cred = nfs4_get_clid_cred(clp);
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008043 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008044 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008045
8046 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008047
Anna Schumaker50493362019-08-14 15:30:16 -04008048 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8049 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8050 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008051
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008052 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008053
NeilBrowna52458b2018-12-03 11:30:31 +11008054 put_cred(cred);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008055 return status;
8056}
8057
Bryan Schumaker72de53e2012-04-27 13:27:40 -04008058int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8059 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008060{
Trond Myklebust0688e642019-04-07 13:59:09 -04008061 struct nfs4_exception exception = {
8062 .interruptible = true,
8063 };
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008064 int err;
8065 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008066 err = -NFS4ERR_WRONGSEC;
8067
8068 /* try to use integrity protection with machine cred */
8069 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8070 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8071
8072 /*
8073 * if unable to use integrity protection, or SECINFO with
8074 * integrity protection returns NFS4ERR_WRONGSEC (which is
8075 * disallowed by spec, but exists in deployed servers) use
8076 * the current filesystem's rpc_client and the user cred.
8077 */
8078 if (err == -NFS4ERR_WRONGSEC)
8079 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8080
Trond Myklebust078ea3d2013-08-12 16:45:55 -04008081 trace_nfs4_secinfo(dir, name, err);
8082 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008083 &exception);
8084 } while (exception.retry);
8085 return err;
8086}
8087
Andy Adamson557134a2009-04-01 09:21:53 -04008088#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04008089/*
Andy Adamson357f54d2010-12-14 10:11:57 -05008090 * Check the exchange flags returned by the server for invalid flags, having
8091 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8092 * DS flags set.
8093 */
8094static int nfs4_check_cl_exchange_flags(u32 flags)
8095{
8096 if (flags & ~EXCHGID4_FLAG_MASK_R)
8097 goto out_inval;
8098 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8099 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8100 goto out_inval;
8101 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8102 goto out_inval;
8103 return NFS_OK;
8104out_inval:
8105 return -NFS4ERR_INVAL;
8106}
8107
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008108static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04008109nfs41_same_server_scope(struct nfs41_server_scope *a,
8110 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008111{
Anna Schumaker49ad0142017-01-11 16:51:59 -05008112 if (a->server_scope_sz != b->server_scope_sz)
8113 return false;
8114 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008115}
8116
Andy Adamson02a95de2016-02-05 16:08:37 -05008117static void
8118nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8119{
Trond Myklebust5c441542019-11-13 08:34:00 +01008120 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008121 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
Trond Myklebust5c441542019-11-13 08:34:00 +01008122 struct nfs_client *clp = args->client;
8123
8124 switch (task->tk_status) {
8125 case -NFS4ERR_BADSESSION:
8126 case -NFS4ERR_DEADSESSION:
8127 nfs4_schedule_session_recovery(clp->cl_session,
8128 task->tk_status);
8129 }
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008130 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8131 res->dir != NFS4_CDFS4_BOTH) {
8132 rpc_task_close_connection(task);
8133 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8134 rpc_restart_call(task);
8135 }
Andy Adamson02a95de2016-02-05 16:08:37 -05008136}
8137
8138static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
Olga Kornievskaia1c709b72020-04-26 11:30:00 -04008139 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
Andy Adamson02a95de2016-02-05 16:08:37 -05008140};
8141
Andy Adamson357f54d2010-12-14 10:11:57 -05008142/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008143 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008144 *
8145 * The 4.1 client currently uses the same TCP connection for the
8146 * fore and backchannel.
8147 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008148static
8149int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8150 struct rpc_xprt *xprt,
8151 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008152 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008153{
8154 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08008155 struct nfs41_bind_conn_to_session_args args = {
8156 .client = clp,
8157 .dir = NFS4_CDFC4_FORE_OR_BOTH,
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008158 .retries = 0,
Trond Myklebust71a097c2015-02-18 09:27:18 -08008159 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008160 struct nfs41_bind_conn_to_session_res res;
8161 struct rpc_message msg = {
8162 .rpc_proc =
8163 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08008164 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008165 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04008166 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008167 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008168 struct rpc_task_setup task_setup_data = {
8169 .rpc_client = clnt,
8170 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05008171 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008172 .rpc_message = &msg,
8173 .flags = RPC_TASK_TIMEOUT,
8174 };
8175 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008176
Trond Myklebust71a097c2015-02-18 09:27:18 -08008177 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8178 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8179 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008180
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008181 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8182 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8183 args.dir = NFS4_CDFC4_FORE;
8184
8185 task = rpc_run_task(&task_setup_data);
8186 if (!IS_ERR(task)) {
8187 status = task->tk_status;
8188 rpc_put_task(task);
8189 } else
8190 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008191 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008192 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08008193 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008194 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8195 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008196 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008197 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008198 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008199 dprintk("NFS: %s: Unexpected direction from server\n",
8200 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008201 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008202 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008203 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008204 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8205 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008206 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008207 }
8208 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008209
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008210 return status;
8211}
8212
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008213struct rpc_bind_conn_calldata {
8214 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11008215 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008216};
8217
8218static int
8219nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8220 struct rpc_xprt *xprt,
8221 void *calldata)
8222{
8223 struct rpc_bind_conn_calldata *p = calldata;
8224
8225 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8226}
8227
NeilBrowna52458b2018-12-03 11:30:31 +11008228int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008229{
8230 struct rpc_bind_conn_calldata data = {
8231 .clp = clp,
8232 .cred = cred,
8233 };
8234 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8235 nfs4_proc_bind_conn_to_session_callback, &data);
8236}
8237
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008238/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008239 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8240 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04008241 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008242static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8243 .how = SP4_MACH_CRED,
8244 .enforce.u.words = {
8245 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8246 1 << (OP_EXCHANGE_ID - 32) |
8247 1 << (OP_CREATE_SESSION - 32) |
8248 1 << (OP_DESTROY_SESSION - 32) |
8249 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008250 },
8251 .allow.u.words = {
8252 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008253 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008254 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008255 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008256 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008257 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008258 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008259 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008260 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008261 1 << (OP_FREE_STATEID - 32) |
8262 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008263 }
8264};
8265
8266/*
8267 * Select the state protection mode for client `clp' given the server results
8268 * from exchange_id in `sp'.
8269 *
8270 * Returns 0 on success, negative errno otherwise.
8271 */
8272static int nfs4_sp4_select_mode(struct nfs_client *clp,
8273 struct nfs41_state_protection *sp)
8274{
8275 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8276 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8277 1 << (OP_EXCHANGE_ID - 32) |
8278 1 << (OP_CREATE_SESSION - 32) |
8279 1 << (OP_DESTROY_SESSION - 32) |
8280 1 << (OP_DESTROY_CLIENTID - 32)
8281 };
Trond Myklebust937e3132017-08-01 07:32:50 -04008282 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008283 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04008284 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008285
8286 if (sp->how == SP4_MACH_CRED) {
8287 /* Print state protect result */
8288 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8289 for (i = 0; i <= LAST_NFS4_OP; i++) {
8290 if (test_bit(i, sp->enforce.u.longs))
8291 dfprintk(MOUNT, " enforce op %d\n", i);
8292 if (test_bit(i, sp->allow.u.longs))
8293 dfprintk(MOUNT, " allow op %d\n", i);
8294 }
8295
8296 /* make sure nothing is on enforce list that isn't supported */
8297 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8298 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8299 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008300 ret = -EINVAL;
8301 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008302 }
8303 }
8304
8305 /*
8306 * Minimal mode - state operations are allowed to use machine
8307 * credential. Note this already happens by default, so the
8308 * client doesn't have to do anything more than the negotiation.
8309 *
8310 * NOTE: we don't care if EXCHANGE_ID is in the list -
8311 * we're already using the machine cred for exchange_id
8312 * and will never use a different cred.
8313 */
8314 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8315 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8316 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8317 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8318 dfprintk(MOUNT, "sp4_mach_cred:\n");
8319 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008320 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008321 } else {
8322 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008323 ret = -EINVAL;
8324 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008325 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008326
8327 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05008328 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8329 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008330 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8331 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008332 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008333 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008334
Andrew Elble99ade3c2015-12-02 09:39:51 -05008335 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8336 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008337 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05008338 }
8339
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008340 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8341 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8342 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008343 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008344 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008345
8346 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8347 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8348 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008349 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008350 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008351
8352 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8353 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008354 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008355 }
8356
8357 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8358 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008359 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008360 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008361 }
Trond Myklebust937e3132017-08-01 07:32:50 -04008362out:
8363 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00008364 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008365}
8366
Andy Adamson8d89bd72016-09-09 09:22:18 -04008367struct nfs41_exchange_id_data {
8368 struct nfs41_exchange_id_res res;
8369 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008370};
8371
Andy Adamson8d89bd72016-09-09 09:22:18 -04008372static void nfs4_exchange_id_release(void *data)
8373{
8374 struct nfs41_exchange_id_data *cdata =
8375 (struct nfs41_exchange_id_data *)data;
8376
Olga Kornievskaia63513232017-03-13 10:36:19 -04008377 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008378 kfree(cdata->res.impl_id);
8379 kfree(cdata->res.server_scope);
8380 kfree(cdata->res.server_owner);
8381 kfree(cdata);
8382}
8383
8384static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04008385 .rpc_release = nfs4_exchange_id_release,
8386};
8387
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008388/*
8389 * _nfs4_proc_exchange_id()
8390 *
8391 * Wrapper for EXCHANGE_ID operation.
8392 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008393static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11008394nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04008395 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04008396{
Benny Halevy99fe60d2009-04-01 09:22:29 -04008397 struct rpc_message msg = {
8398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04008399 .rpc_cred = cred,
8400 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04008401 struct rpc_task_setup task_setup_data = {
8402 .rpc_client = clp->cl_rpcclient,
8403 .callback_ops = &nfs4_exchange_id_call_ops,
8404 .rpc_message = &msg,
NeilBrown5a0c2572019-05-30 10:41:28 +10008405 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Andy Adamson8d89bd72016-09-09 09:22:18 -04008406 };
8407 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04008408 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008409
Elena Reshetova212bf412017-10-20 12:53:38 +03008410 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04008411 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008412
Trond Myklebust9c760d12017-07-31 18:38:50 -04008413 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008414 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008415 if (!calldata)
8416 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008417
Trond Myklebustfd405592017-08-01 16:02:47 -04008418 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04008419
8420 status = nfs4_init_uniform_client_string(clp);
8421 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04008422 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04008423
Andy Adamson8d89bd72016-09-09 09:22:18 -04008424 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8425 GFP_NOFS);
8426 status = -ENOMEM;
8427 if (unlikely(calldata->res.server_owner == NULL))
8428 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008429
Andy Adamson8d89bd72016-09-09 09:22:18 -04008430 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04008431 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008432 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04008433 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008434
Andy Adamson8d89bd72016-09-09 09:22:18 -04008435 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8436 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008437 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008438
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008439 switch (sp4_how) {
8440 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008441 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008442 break;
8443
8444 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008445 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008446 break;
8447
8448 default:
8449 /* unsupported! */
8450 WARN_ON_ONCE(1);
8451 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08008452 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008453 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04008454 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04008455 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04008456 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04008457 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8458 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04008459 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04008460 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04008461 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8462 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008463#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04008464 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008465#endif
8466 msg.rpc_argp = &calldata->args;
8467 msg.rpc_resp = &calldata->res;
8468 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008469
Trond Myklebust9c760d12017-07-31 18:38:50 -04008470 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008471
8472out_impl_id:
8473 kfree(calldata->res.impl_id);
8474out_server_scope:
8475 kfree(calldata->res.server_scope);
8476out_server_owner:
8477 kfree(calldata->res.server_owner);
8478out_calldata:
8479 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008480out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04008481 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008482 return ERR_PTR(status);
8483}
8484
8485/*
8486 * _nfs4_proc_exchange_id()
8487 *
8488 * Wrapper for EXCHANGE_ID operation.
8489 */
NeilBrowna52458b2018-12-03 11:30:31 +11008490static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04008491 u32 sp4_how)
8492{
8493 struct rpc_task *task;
8494 struct nfs41_exchange_id_args *argp;
8495 struct nfs41_exchange_id_res *resp;
Robert Milkowski7dc29932020-01-30 09:43:25 +00008496 unsigned long now = jiffies;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008497 int status;
8498
8499 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8500 if (IS_ERR(task))
8501 return PTR_ERR(task);
8502
8503 argp = task->tk_msg.rpc_argp;
8504 resp = task->tk_msg.rpc_resp;
8505 status = task->tk_status;
8506 if (status != 0)
8507 goto out;
8508
8509 status = nfs4_check_cl_exchange_flags(resp->flags);
8510 if (status != 0)
8511 goto out;
8512
8513 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8514 if (status != 0)
8515 goto out;
8516
Robert Milkowski7dc29932020-01-30 09:43:25 +00008517 do_renew_lease(clp, now);
8518
Trond Myklebust9c760d12017-07-31 18:38:50 -04008519 clp->cl_clientid = resp->clientid;
8520 clp->cl_exchange_flags = resp->flags;
8521 clp->cl_seqid = resp->seqid;
8522 /* Client ID is not confirmed */
8523 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8524 clear_bit(NFS4_SESSION_ESTABLISHED,
8525 &clp->cl_session->session_state);
8526
8527 if (clp->cl_serverscope != NULL &&
8528 !nfs41_same_server_scope(clp->cl_serverscope,
8529 resp->server_scope)) {
8530 dprintk("%s: server_scope mismatch detected\n",
8531 __func__);
8532 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8533 }
8534
8535 swap(clp->cl_serverowner, resp->server_owner);
8536 swap(clp->cl_serverscope, resp->server_scope);
8537 swap(clp->cl_implid, resp->impl_id);
8538
8539 /* Save the EXCHANGE_ID verifier session trunk tests */
8540 memcpy(clp->cl_confirm.data, argp->verifier.data,
8541 sizeof(clp->cl_confirm.data));
8542out:
8543 trace_nfs4_exchange_id(clp, status);
8544 rpc_put_task(task);
8545 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008546}
8547
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008548/*
8549 * nfs4_proc_exchange_id()
8550 *
8551 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8552 *
8553 * Since the clientid has expired, all compounds using sessions
8554 * associated with the stale clientid will be returning
8555 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8556 * be in some phase of session reset.
8557 *
8558 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8559 */
NeilBrowna52458b2018-12-03 11:30:31 +11008560int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008561{
8562 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8563 int status;
8564
8565 /* try SP4_MACH_CRED if krb5i/p */
8566 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8567 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008568 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008569 if (!status)
8570 return 0;
8571 }
8572
8573 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008574 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008575}
8576
Andy Adamson04fa2c62016-09-09 09:22:29 -04008577/**
8578 * nfs4_test_session_trunk
8579 *
8580 * This is an add_xprt_test() test function called from
8581 * rpc_clnt_setup_test_and_add_xprt.
8582 *
8583 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8584 * and is dereferrenced in nfs4_exchange_id_release
8585 *
8586 * Upon success, add the new transport to the rpc_clnt
8587 *
8588 * @clnt: struct rpc_clnt to get new transport
8589 * @xprt: the rpc_xprt to test
8590 * @data: call data for _nfs4_proc_exchange_id.
8591 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308592void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008593 void *data)
8594{
8595 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008596 struct rpc_task *task;
8597 int status;
8598
Andy Adamson04fa2c62016-09-09 09:22:29 -04008599 u32 sp4_how;
8600
8601 dprintk("--> %s try %s\n", __func__,
8602 xprt->address_strings[RPC_DISPLAY_ADDR]);
8603
8604 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8605
8606 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008607 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8608 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308609 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008610
8611 status = task->tk_status;
8612 if (status == 0)
8613 status = nfs4_detect_session_trunking(adata->clp,
8614 task->tk_msg.rpc_resp, xprt);
8615
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308616 if (status == 0)
8617 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8618
Trond Myklebust9c760d12017-07-31 18:38:50 -04008619 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008620}
8621EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8622
Trond Myklebust66245532012-05-25 17:18:09 -04008623static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008624 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008625{
8626 struct rpc_message msg = {
8627 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8628 .rpc_argp = clp,
8629 .rpc_cred = cred,
8630 };
8631 int status;
8632
NeilBrown5a0c2572019-05-30 10:41:28 +10008633 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8634 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008635 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008636 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008637 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008638 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8639 return status;
8640}
8641
8642static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008643 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008644{
8645 unsigned int loop;
8646 int ret;
8647
8648 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8649 ret = _nfs4_proc_destroy_clientid(clp, cred);
8650 switch (ret) {
8651 case -NFS4ERR_DELAY:
8652 case -NFS4ERR_CLIENTID_BUSY:
8653 ssleep(1);
8654 break;
8655 default:
8656 return ret;
8657 }
8658 }
8659 return 0;
8660}
8661
8662int nfs4_destroy_clientid(struct nfs_client *clp)
8663{
NeilBrowna52458b2018-12-03 11:30:31 +11008664 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008665 int ret = 0;
8666
8667 if (clp->cl_mvops->minor_version < 1)
8668 goto out;
8669 if (clp->cl_exchange_flags == 0)
8670 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008671 if (clp->cl_preserve_clid)
8672 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008673 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008674 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008675 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008676 switch (ret) {
8677 case 0:
8678 case -NFS4ERR_STALE_CLIENTID:
8679 clp->cl_exchange_flags = 0;
8680 }
8681out:
8682 return ret;
8683}
8684
Donald Buczek0efb01b2019-07-07 21:26:08 +02008685#endif /* CONFIG_NFS_V4_1 */
8686
Andy Adamson2050f0c2009-04-01 09:22:30 -04008687struct nfs4_get_lease_time_data {
8688 struct nfs4_get_lease_time_args *args;
8689 struct nfs4_get_lease_time_res *res;
8690 struct nfs_client *clp;
8691};
8692
8693static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8694 void *calldata)
8695{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008696 struct nfs4_get_lease_time_data *data =
8697 (struct nfs4_get_lease_time_data *)calldata;
8698
8699 dprintk("--> %s\n", __func__);
8700 /* just setup sequence, do not trigger session recovery
8701 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008702 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008703 &data->args->la_seq_args,
8704 &data->res->lr_seq_res,
8705 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008706 dprintk("<-- %s\n", __func__);
8707}
8708
8709/*
8710 * Called from nfs4_state_manager thread for session setup, so don't recover
8711 * from sequence operation or clientid errors.
8712 */
8713static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8714{
8715 struct nfs4_get_lease_time_data *data =
8716 (struct nfs4_get_lease_time_data *)calldata;
8717
8718 dprintk("--> %s\n", __func__);
Donald Buczek0efb01b2019-07-07 21:26:08 +02008719 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04008720 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008721 switch (task->tk_status) {
8722 case -NFS4ERR_DELAY:
8723 case -NFS4ERR_GRACE:
8724 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8725 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8726 task->tk_status = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05008727 fallthrough;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008728 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008729 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008730 return;
8731 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008732 dprintk("<-- %s\n", __func__);
8733}
8734
Trond Myklebust17280172012-03-11 13:11:00 -04008735static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008736 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8737 .rpc_call_done = nfs4_get_lease_time_done,
8738};
8739
8740int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8741{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008742 struct nfs4_get_lease_time_args args;
8743 struct nfs4_get_lease_time_res res = {
8744 .lr_fsinfo = fsinfo,
8745 };
8746 struct nfs4_get_lease_time_data data = {
8747 .args = &args,
8748 .res = &res,
8749 .clp = clp,
8750 };
8751 struct rpc_message msg = {
8752 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8753 .rpc_argp = &args,
8754 .rpc_resp = &res,
8755 };
8756 struct rpc_task_setup task_setup = {
8757 .rpc_client = clp->cl_rpcclient,
8758 .rpc_message = &msg,
8759 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008760 .callback_data = &data,
8761 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008762 };
Andy Adamson2050f0c2009-04-01 09:22:30 -04008763
Anna Schumakerfba83f32018-05-04 16:22:50 -04008764 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Anna Schumakerf836b272019-08-19 10:18:28 -04008765 return nfs4_call_sync_custom(&task_setup);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008766}
8767
Donald Buczek0efb01b2019-07-07 21:26:08 +02008768#ifdef CONFIG_NFS_V4_1
8769
Andy Adamsonfc931582009-04-01 09:22:31 -04008770/*
8771 * Initialize the values to be used by the client in CREATE_SESSION
8772 * If nfs4_init_session set the fore channel request and response sizes,
8773 * use them.
8774 *
8775 * Set the back channel max_resp_sz_cached to zero to force the client to
8776 * always set csa_cachethis to FALSE because the current implementation
8777 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8778 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008779static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8780 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008781{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008782 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008783 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008784 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008785
Andy Adamson18aad3d2013-06-26 12:21:49 -04008786 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8787 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8788
Andy Adamsonfc931582009-04-01 09:22:31 -04008789 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008790 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8791 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008792 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008793 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008794
8795 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008796 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008797 __func__,
8798 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008799 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008800
8801 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008802 args->bc_attrs.max_rqst_sz = max_bc_payload;
8803 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008804 args->bc_attrs.max_resp_sz_cached = 0;
8805 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008806 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008807 if (args->bc_attrs.max_reqs > max_bc_slots)
8808 args->bc_attrs.max_reqs = max_bc_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008809
8810 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8811 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8812 __func__,
8813 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8814 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8815 args->bc_attrs.max_reqs);
8816}
8817
Trond Myklebust79969dd2015-02-18 11:30:18 -08008818static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8819 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008820{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008821 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008822 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008823
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008824 if (rcvd->max_resp_sz > sent->max_resp_sz)
8825 return -EINVAL;
8826 /*
8827 * Our requested max_ops is the minimum we need; we're not
8828 * prepared to break up compounds into smaller pieces than that.
8829 * So, no point even trying to continue if the server won't
8830 * cooperate:
8831 */
8832 if (rcvd->max_ops < sent->max_ops)
8833 return -EINVAL;
8834 if (rcvd->max_reqs == 0)
8835 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008836 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8837 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008838 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008839}
8840
Trond Myklebust79969dd2015-02-18 11:30:18 -08008841static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8842 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008843{
8844 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008845 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008846
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008847 if (!(res->flags & SESSION4_BACK_CHAN))
8848 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008849 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8850 return -EINVAL;
8851 if (rcvd->max_resp_sz < sent->max_resp_sz)
8852 return -EINVAL;
8853 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8854 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008855 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008856 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008857 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008858 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008859out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008860 return 0;
8861}
Andy Adamson8d353012009-04-01 09:22:32 -04008862
Andy Adamson8d353012009-04-01 09:22:32 -04008863static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008864 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008865{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008866 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008867
Trond Myklebust79969dd2015-02-18 11:30:18 -08008868 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008869 if (ret)
8870 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008871 return nfs4_verify_back_channel_attrs(args, res);
8872}
8873
8874static void nfs4_update_session(struct nfs4_session *session,
8875 struct nfs41_create_session_res *res)
8876{
8877 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008878 /* Mark client id and session as being confirmed */
8879 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8880 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008881 session->flags = res->flags;
8882 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008883 if (res->flags & SESSION4_BACK_CHAN)
8884 memcpy(&session->bc_attrs, &res->bc_attrs,
8885 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008886}
8887
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008888static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008889 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008890{
8891 struct nfs4_session *session = clp->cl_session;
8892 struct nfs41_create_session_args args = {
8893 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008894 .clientid = clp->cl_clientid,
8895 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008896 .cb_program = NFS4_CALLBACK,
8897 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008898 struct nfs41_create_session_res res;
8899
Andy Adamsonfc931582009-04-01 09:22:31 -04008900 struct rpc_message msg = {
8901 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8902 .rpc_argp = &args,
8903 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008904 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008905 };
8906 int status;
8907
Chuck Lever6b26cc82016-05-02 14:40:40 -04008908 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008909 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008910
NeilBrown5a0c2572019-05-30 10:41:28 +10008911 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8912 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008913 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008914
Trond Myklebustb519d402016-09-11 14:50:01 -04008915 switch (status) {
8916 case -NFS4ERR_STALE_CLIENTID:
8917 case -NFS4ERR_DELAY:
8918 case -ETIMEDOUT:
8919 case -EACCES:
8920 case -EAGAIN:
8921 goto out;
zhengbin8b98a532019-12-19 18:34:47 +08008922 }
Trond Myklebustb519d402016-09-11 14:50:01 -04008923
8924 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008925 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008926 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008927 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008928 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008929 if (status)
8930 goto out;
8931 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008932 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008933out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008934 return status;
8935}
8936
8937/*
8938 * Issues a CREATE_SESSION operation to the server.
8939 * It is the responsibility of the caller to verify the session is
8940 * expired before calling this routine.
8941 */
NeilBrowna52458b2018-12-03 11:30:31 +11008942int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008943{
8944 int status;
8945 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008946 struct nfs4_session *session = clp->cl_session;
8947
8948 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8949
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008950 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008951 if (status)
8952 goto out;
8953
Andy Adamsonaacd5532011-11-09 13:58:21 -05008954 /* Init or reset the session slot tables */
8955 status = nfs4_setup_session_slot_tables(session);
8956 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008957 if (status)
8958 goto out;
8959
8960 ptr = (unsigned *)&session->sess_id.data[0];
8961 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8962 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008963out:
8964 dprintk("<-- %s\n", __func__);
8965 return status;
8966}
8967
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008968/*
8969 * Issue the over-the-wire RPC DESTROY_SESSION.
8970 * The caller must serialize access to this routine.
8971 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008972int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11008973 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008974{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008975 struct rpc_message msg = {
8976 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8977 .rpc_argp = session,
8978 .rpc_cred = cred,
8979 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008980 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008981
8982 dprintk("--> nfs4_proc_destroy_session\n");
8983
8984 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008985 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8986 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008987
NeilBrown5a0c2572019-05-30 10:41:28 +10008988 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8989 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008990 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008991
8992 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008993 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008994 "Session has been destroyed regardless...\n", status);
8995
8996 dprintk("<-- nfs4_proc_destroy_session\n");
8997 return status;
8998}
8999
Trond Myklebust7b38c362012-05-23 13:23:31 -04009000/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009001 * Renew the cl_session lease.
9002 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009003struct nfs4_sequence_data {
9004 struct nfs_client *clp;
9005 struct nfs4_sequence_args args;
9006 struct nfs4_sequence_res res;
9007};
9008
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009009static void nfs41_sequence_release(void *data)
9010{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009011 struct nfs4_sequence_data *calldata = data;
9012 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009013
Elena Reshetova212bf412017-10-20 12:53:38 +03009014 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08009015 nfs4_schedule_state_renewal(clp);
9016 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009017 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009018}
9019
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009020static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9021{
9022 switch(task->tk_status) {
9023 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009024 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9025 return -EAGAIN;
9026 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009027 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009028 }
9029 return 0;
9030}
9031
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009032static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009033{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009034 struct nfs4_sequence_data *calldata = data;
9035 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009036
Trond Myklebust14516c32010-07-31 14:29:06 -04009037 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9038 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009039
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009040 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009041 if (task->tk_status < 0) {
9042 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03009043 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08009044 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009045
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009046 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9047 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009048 return;
9049 }
9050 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009051 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08009052out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009053 dprintk("<-- %s\n", __func__);
9054}
9055
9056static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9057{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009058 struct nfs4_sequence_data *calldata = data;
9059 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009060 struct nfs4_sequence_args *args;
9061 struct nfs4_sequence_res *res;
9062
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009063 args = task->tk_msg.rpc_argp;
9064 res = task->tk_msg.rpc_resp;
9065
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009066 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009067}
9068
9069static const struct rpc_call_ops nfs41_sequence_ops = {
9070 .rpc_call_done = nfs41_sequence_call_done,
9071 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009072 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009073};
9074
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009075static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009076 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009077 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009078 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009079{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009080 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009081 struct rpc_message msg = {
9082 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9083 .rpc_cred = cred,
9084 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009085 struct rpc_task_setup task_setup_data = {
9086 .rpc_client = clp->cl_rpcclient,
9087 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009088 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04009089 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009090 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009091 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009092
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009093 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03009094 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009095 goto out_err;
9096
9097 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04009098 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009099 if (calldata == NULL)
9100 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009101 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009102 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009103 msg.rpc_argp = &calldata->args;
9104 msg.rpc_resp = &calldata->res;
9105 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009106 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009107
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009108 ret = rpc_run_task(&task_setup_data);
9109 if (IS_ERR(ret))
9110 goto out_err;
9111 return ret;
9112out_put_clp:
9113 nfs_put_client(clp);
9114out_err:
9115 nfs41_release_slot(slot);
9116 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009117}
9118
NeilBrowna52458b2018-12-03 11:30:31 +11009119static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009120{
9121 struct rpc_task *task;
9122 int ret = 0;
9123
Trond Myklebust2f60ea62011-08-24 15:07:37 -04009124 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04009125 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009126 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009127 if (IS_ERR(task))
9128 ret = PTR_ERR(task);
9129 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08009130 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009131 dprintk("<-- %s status=%d\n", __func__, ret);
9132 return ret;
9133}
9134
NeilBrowna52458b2018-12-03 11:30:31 +11009135static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009136{
9137 struct rpc_task *task;
9138 int ret;
9139
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009140 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009141 if (IS_ERR(task)) {
9142 ret = PTR_ERR(task);
9143 goto out;
9144 }
9145 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04009146 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009147 ret = task->tk_status;
9148 rpc_put_task(task);
9149out:
9150 dprintk("<-- %s status=%d\n", __func__, ret);
9151 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009152}
9153
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009154struct nfs4_reclaim_complete_data {
9155 struct nfs_client *clp;
9156 struct nfs41_reclaim_complete_args arg;
9157 struct nfs41_reclaim_complete_res res;
9158};
9159
9160static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9161{
9162 struct nfs4_reclaim_complete_data *calldata = data;
9163
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009164 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009165 &calldata->arg.seq_args,
9166 &calldata->res.seq_res,
9167 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009168}
9169
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009170static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9171{
9172 switch(task->tk_status) {
9173 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04009174 wake_up_all(&clp->cl_lock_waitq);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009175 fallthrough;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009176 case -NFS4ERR_COMPLETE_ALREADY:
9177 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9178 break;
9179 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009180 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009181 fallthrough;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04009182 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009183 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009184 case -NFS4ERR_BADSESSION:
9185 case -NFS4ERR_DEADSESSION:
9186 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009187 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009188 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009189 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009190 }
9191 return 0;
9192}
9193
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009194static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9195{
9196 struct nfs4_reclaim_complete_data *calldata = data;
9197 struct nfs_client *clp = calldata->clp;
9198 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9199
9200 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04009201 if (!nfs41_sequence_done(task, res))
9202 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009203
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009204 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009205 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9206 rpc_restart_call_prepare(task);
9207 return;
9208 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009209 dprintk("<-- %s\n", __func__);
9210}
9211
9212static void nfs4_free_reclaim_complete_data(void *data)
9213{
9214 struct nfs4_reclaim_complete_data *calldata = data;
9215
9216 kfree(calldata);
9217}
9218
9219static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9220 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9221 .rpc_call_done = nfs4_reclaim_complete_done,
9222 .rpc_release = nfs4_free_reclaim_complete_data,
9223};
9224
9225/*
9226 * Issue a global reclaim complete.
9227 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04009228static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009229 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009230{
9231 struct nfs4_reclaim_complete_data *calldata;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009232 struct rpc_message msg = {
9233 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04009234 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009235 };
9236 struct rpc_task_setup task_setup_data = {
9237 .rpc_client = clp->cl_rpcclient,
9238 .rpc_message = &msg,
9239 .callback_ops = &nfs4_reclaim_complete_call_ops,
Anna Schumaker4c952e32019-08-14 15:46:48 -04009240 .flags = RPC_TASK_NO_ROUND_ROBIN,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009241 };
9242 int status = -ENOMEM;
9243
9244 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04009245 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009246 if (calldata == NULL)
9247 goto out;
9248 calldata->clp = clp;
9249 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009250
Anna Schumakerfba83f32018-05-04 16:22:50 -04009251 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009252 msg.rpc_argp = &calldata->arg;
9253 msg.rpc_resp = &calldata->res;
9254 task_setup_data.callback_data = calldata;
Anna Schumaker4c952e32019-08-14 15:46:48 -04009255 status = nfs4_call_sync_custom(&task_setup_data);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009256out:
9257 dprintk("<-- %s status=%d\n", __func__, status);
9258 return status;
9259}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009260
9261static void
9262nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9263{
9264 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00009265 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009266
9267 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009268 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04009269 &lgp->res.seq_res, task);
9270 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009271}
9272
9273static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9274{
9275 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04009276
9277 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009278 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04009279 dprintk("<-- %s\n", __func__);
9280}
9281
9282static int
9283nfs4_layoutget_handle_exception(struct rpc_task *task,
9284 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9285{
Trond Myklebustee314c22012-10-01 17:25:48 -07009286 struct inode *inode = lgp->args.inode;
9287 struct nfs_server *server = NFS_SERVER(inode);
9288 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009289 int nfs4err = task->tk_status;
9290 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009291 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009292
Boaz Harroshed7e5422014-01-22 20:34:54 +02009293 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009294
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009295 nfs4_sequence_free_slot(&lgp->res.seq_res);
9296
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009297 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009298 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07009299 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009300
9301 /*
9302 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9303 * on the file. set tk_status to -ENODATA to tell upper layer to
9304 * retry go inband.
9305 */
9306 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04009307 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009308 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009309 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07009310 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9311 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9312 */
9313 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009314 status = -EOVERFLOW;
9315 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07009316 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02009317 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07009318 * (or clients) writing to the same RAID stripe except when
9319 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04009320 *
9321 * Treat it like we would RECALLCONFLICT -- we retry for a little
9322 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02009323 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009324 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04009325 if (lgp->args.minlength == 0) {
9326 status = -EOVERFLOW;
9327 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009328 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009329 status = -EBUSY;
9330 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04009331 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009332 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009333 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04009334 case -NFS4ERR_DELEG_REVOKED:
9335 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07009336 case -NFS4ERR_EXPIRED:
9337 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04009338 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07009339 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009340 lo = NFS_I(inode)->layout;
9341 /* If the open stateid was bad, then recover it. */
9342 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04009343 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07009344 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04009345 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04009346 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04009347 break;
9348 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009349
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009350 /*
9351 * Mark the bad layout state as invalid, then retry
9352 */
Trond Myklebust668f4552016-07-24 17:08:59 -04009353 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009354 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04009355 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009356 pnfs_free_lseg_list(&head);
9357 status = -EAGAIN;
9358 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009359 }
Jeff Layton183d9e72016-05-17 12:28:47 -04009360
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009361 err = nfs4_handle_exception(server, nfs4err, exception);
9362 if (!status) {
9363 if (exception->retry)
9364 status = -EAGAIN;
9365 else
9366 status = err;
9367 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009368out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009369 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04009370 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009371}
9372
Fred Isamandacb4522016-09-19 17:47:09 -04009373size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03009374{
9375 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9376 return nfs_page_array_len(0, max_resp_sz);
9377}
9378
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009379static void nfs4_layoutget_release(void *calldata)
9380{
9381 struct nfs4_layoutget *lgp = calldata;
9382
9383 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04009384 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04009385 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009386 dprintk("<-- %s\n", __func__);
9387}
9388
9389static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9390 .rpc_call_prepare = nfs4_layoutget_prepare,
9391 .rpc_call_done = nfs4_layoutget_done,
9392 .rpc_release = nfs4_layoutget_release,
9393};
9394
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009395struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04009396nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009397{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009398 struct inode *inode = lgp->args.inode;
9399 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009400 struct rpc_task *task;
9401 struct rpc_message msg = {
9402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9403 .rpc_argp = &lgp->args,
9404 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04009405 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009406 };
9407 struct rpc_task_setup task_setup_data = {
9408 .rpc_client = server->client,
9409 .rpc_message = &msg,
9410 .callback_ops = &nfs4_layoutget_call_ops,
9411 .callback_data = lgp,
Trond Myklebust63ec2b62020-02-07 19:40:14 -05009412 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009413 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009414 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04009415 struct nfs4_exception exception = {
9416 .inode = inode,
9417 .timeout = *timeout,
9418 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009419 int status = 0;
9420
9421 dprintk("--> %s\n", __func__);
9422
Peng Tao4bd5a982014-11-17 11:05:17 +08009423 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9424 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9425
Anna Schumakerfba83f32018-05-04 16:22:50 -04009426 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009427
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009428 task = rpc_run_task(&task_setup_data);
Xiyu Yang6e476662020-04-25 21:04:40 +08009429
Anna Schumaker820bf852017-01-11 15:01:43 -05009430 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009431 if (status != 0)
9432 goto out;
9433
Trond Myklebust18c07782019-02-13 07:55:31 -05009434 if (task->tk_status < 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04009435 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9436 *timeout = exception.timeout;
Trond Myklebust18c07782019-02-13 07:55:31 -05009437 } else if (lgp->res.layoutp->len == 0) {
9438 status = -EAGAIN;
9439 *timeout = nfs4_update_delay(&exception.timeout);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009440 } else
9441 lseg = pnfs_layout_process(lgp);
9442out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009443 trace_nfs4_layoutget(lgp->args.ctx,
9444 &lgp->args.range,
9445 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009446 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009447 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04009448
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009449 rpc_put_task(task);
9450 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009451 if (status)
9452 return ERR_PTR(status);
9453 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009454}
9455
Benny Halevycbe82602011-05-22 19:52:37 +03009456static void
9457nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9458{
9459 struct nfs4_layoutreturn *lrp = calldata;
9460
9461 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009462 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009463 &lrp->args.seq_args,
9464 &lrp->res.seq_res,
9465 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04009466 if (!pnfs_layout_is_valid(lrp->args.layout))
9467 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009468}
9469
9470static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9471{
9472 struct nfs4_layoutreturn *lrp = calldata;
9473 struct nfs_server *server;
9474
9475 dprintk("--> %s\n", __func__);
9476
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009477 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03009478 return;
9479
Trond Myklebust6109bcf2019-09-20 07:23:43 -04009480 /*
9481 * Was there an RPC level error? Assume the call succeeded,
9482 * and that we need to release the layout
9483 */
9484 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9485 lrp->res.lrs_present = 0;
9486 return;
9487 }
9488
Benny Halevycbe82602011-05-22 19:52:37 +03009489 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009490 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05009491 case -NFS4ERR_OLD_STATEID:
Trond Myklebust30cb3ee2019-09-20 07:23:45 -04009492 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04009493 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05009494 lrp->args.inode))
9495 goto out_restart;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009496 fallthrough;
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009497 default:
9498 task->tk_status = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009499 fallthrough;
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009500 case 0:
9501 break;
9502 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009503 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009504 break;
Trond Myklebustff905142017-11-06 15:28:08 -05009505 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03009506 }
Benny Halevycbe82602011-05-22 19:52:37 +03009507 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05009508 return;
9509out_restart:
9510 task->tk_status = 0;
9511 nfs4_sequence_free_slot(&lrp->res.seq_res);
9512 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009513}
9514
9515static void nfs4_layoutreturn_release(void *calldata)
9516{
9517 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009518 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009519
9520 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009521 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009522 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009523 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009524 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9525 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009526 pnfs_put_layout_hdr(lrp->args.layout);
9527 nfs_iput_and_deactive(lrp->inode);
Trond Myklebust44ea8df2020-04-02 15:37:02 -04009528 put_cred(lrp->cred);
Benny Halevycbe82602011-05-22 19:52:37 +03009529 kfree(calldata);
9530 dprintk("<-- %s\n", __func__);
9531}
9532
9533static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9534 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9535 .rpc_call_done = nfs4_layoutreturn_done,
9536 .rpc_release = nfs4_layoutreturn_release,
9537};
9538
Peng Tao6c166052014-11-17 09:30:40 +08009539int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009540{
9541 struct rpc_task *task;
9542 struct rpc_message msg = {
9543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9544 .rpc_argp = &lrp->args,
9545 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009546 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009547 };
9548 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04009549 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009550 .rpc_message = &msg,
9551 .callback_ops = &nfs4_layoutreturn_call_ops,
9552 .callback_data = lrp,
9553 };
Peng Tao6c166052014-11-17 09:30:40 +08009554 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009555
Andrew Elble99ade3c2015-12-02 09:39:51 -05009556 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9557 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9558 &task_setup_data.rpc_client, &msg);
9559
Benny Halevycbe82602011-05-22 19:52:37 +03009560 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009561 if (!sync) {
9562 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9563 if (!lrp->inode) {
9564 nfs4_layoutreturn_release(lrp);
9565 return -EAGAIN;
9566 }
9567 task_setup_data.flags |= RPC_TASK_ASYNC;
9568 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009569 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009570 task = rpc_run_task(&task_setup_data);
9571 if (IS_ERR(task))
9572 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009573 if (sync)
9574 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009575 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009576 dprintk("<-- %s status=%d\n", __func__, status);
9577 rpc_put_task(task);
9578 return status;
9579}
9580
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009581static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009582_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9583 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009584 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009585{
9586 struct nfs4_getdeviceinfo_args args = {
9587 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009588 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9589 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009590 };
9591 struct nfs4_getdeviceinfo_res res = {
9592 .pdev = pdev,
9593 };
9594 struct rpc_message msg = {
9595 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9596 .rpc_argp = &args,
9597 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009598 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009599 };
9600 int status;
9601
9602 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009603 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009604 if (res.notification & ~args.notify_types)
9605 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009606 if (res.notification != args.notify_types)
9607 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009608
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009609 dprintk("<-- %s status=%d\n", __func__, status);
9610
9611 return status;
9612}
9613
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009614int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9615 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009616 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009617{
9618 struct nfs4_exception exception = { };
9619 int err;
9620
9621 do {
9622 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009623 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009624 &exception);
9625 } while (exception.retry);
9626 return err;
9627}
9628EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9629
Andy Adamson863a3c62011-03-23 13:27:54 +00009630static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9631{
9632 struct nfs4_layoutcommit_data *data = calldata;
9633 struct nfs_server *server = NFS_SERVER(data->args.inode);
9634
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009635 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009636 &data->args.seq_args,
9637 &data->res.seq_res,
9638 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009639}
9640
9641static void
9642nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9643{
9644 struct nfs4_layoutcommit_data *data = calldata;
9645 struct nfs_server *server = NFS_SERVER(data->args.inode);
9646
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009647 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009648 return;
9649
9650 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009651 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9652 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9653 case -NFS4ERR_BADLAYOUT: /* no layout */
9654 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009655 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009656 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009657 break;
9658 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009659 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009660 rpc_restart_call_prepare(task);
9661 return;
9662 }
9663 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009664}
9665
9666static void nfs4_layoutcommit_release(void *calldata)
9667{
9668 struct nfs4_layoutcommit_data *data = calldata;
9669
Andy Adamsondb29c082011-07-30 20:52:38 -04009670 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009671 nfs_post_op_update_inode_force_wcc(data->args.inode,
9672 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009673 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009674 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009675 kfree(data);
9676}
9677
9678static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9679 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9680 .rpc_call_done = nfs4_layoutcommit_done,
9681 .rpc_release = nfs4_layoutcommit_release,
9682};
9683
9684int
Andy Adamsonef311532011-03-12 02:58:10 -05009685nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009686{
9687 struct rpc_message msg = {
9688 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9689 .rpc_argp = &data->args,
9690 .rpc_resp = &data->res,
9691 .rpc_cred = data->cred,
9692 };
9693 struct rpc_task_setup task_setup_data = {
9694 .task = &data->task,
9695 .rpc_client = NFS_CLIENT(data->args.inode),
9696 .rpc_message = &msg,
9697 .callback_ops = &nfs4_layoutcommit_ops,
9698 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009699 };
9700 struct rpc_task *task;
9701 int status = 0;
9702
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009703 dprintk("NFS: initiating layoutcommit call. sync %d "
9704 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009705 data->args.lastbytewritten,
9706 data->args.inode->i_ino);
9707
Trond Myklebust472e2592015-02-05 16:50:30 -05009708 if (!sync) {
9709 data->inode = nfs_igrab_and_active(data->args.inode);
9710 if (data->inode == NULL) {
9711 nfs4_layoutcommit_release(data);
9712 return -EAGAIN;
9713 }
9714 task_setup_data.flags = RPC_TASK_ASYNC;
9715 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009716 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009717 task = rpc_run_task(&task_setup_data);
9718 if (IS_ERR(task))
9719 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009720 if (sync)
9721 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009722 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009723 dprintk("%s: status %d\n", __func__, status);
9724 rpc_put_task(task);
9725 return status;
9726}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009727
Trond Myklebust302fad72019-02-18 13:32:38 -05009728/*
Andy Adamson97431202013-08-08 10:57:56 -04009729 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9730 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9731 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009732static int
9733_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009734 struct nfs_fsinfo *info,
9735 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009736{
9737 struct nfs41_secinfo_no_name_args args = {
9738 .style = SECINFO_STYLE_CURRENT_FH,
9739 };
9740 struct nfs4_secinfo_res res = {
9741 .flavors = flavors,
9742 };
9743 struct rpc_message msg = {
9744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9745 .rpc_argp = &args,
9746 .rpc_resp = &res,
9747 };
Anna Schumakercc15e242019-08-14 16:22:31 -04009748 struct nfs4_call_sync_data data = {
9749 .seq_server = server,
9750 .seq_args = &args.seq_args,
9751 .seq_res = &res.seq_res,
9752 };
9753 struct rpc_task_setup task_setup = {
9754 .rpc_client = server->client,
9755 .rpc_message = &msg,
9756 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9757 .callback_data = &data,
9758 .flags = RPC_TASK_NO_ROUND_ROBIN,
9759 };
NeilBrowna52458b2018-12-03 11:30:31 +11009760 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009761 int status;
9762
9763 if (use_integrity) {
Colin Ian King48bb6ec2020-06-10 00:22:57 +01009764 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
Anna Schumakercc15e242019-08-14 16:22:31 -04009765
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009766 cred = nfs4_get_clid_cred(server->nfs_client);
9767 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009768 }
9769
9770 dprintk("--> %s\n", __func__);
Anna Schumakercc15e242019-08-14 16:22:31 -04009771 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9772 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009773 dprintk("<-- %s status=%d\n", __func__, status);
9774
NeilBrowna52458b2018-12-03 11:30:31 +11009775 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009776
9777 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009778}
9779
9780static int
9781nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9782 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9783{
Trond Myklebust0688e642019-04-07 13:59:09 -04009784 struct nfs4_exception exception = {
9785 .interruptible = true,
9786 };
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009787 int err;
9788 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009789 /* first try using integrity protection */
9790 err = -NFS4ERR_WRONGSEC;
9791
9792 /* try to use integrity protection with machine cred */
9793 if (_nfs4_is_integrity_protected(server->nfs_client))
9794 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9795 flavors, true);
9796
9797 /*
9798 * if unable to use integrity protection, or SECINFO with
9799 * integrity protection returns NFS4ERR_WRONGSEC (which is
9800 * disallowed by spec, but exists in deployed servers) use
9801 * the current filesystem's rpc_client and the user cred.
9802 */
9803 if (err == -NFS4ERR_WRONGSEC)
9804 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9805 flavors, false);
9806
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009807 switch (err) {
9808 case 0:
9809 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009810 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009811 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009812 default:
9813 err = nfs4_handle_exception(server, err, &exception);
9814 }
9815 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009816out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009817 return err;
9818}
9819
9820static int
9821nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9822 struct nfs_fsinfo *info)
9823{
9824 int err;
9825 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009826 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009827 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009828 struct nfs4_secinfo4 *secinfo;
9829 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009830
9831 page = alloc_page(GFP_KERNEL);
9832 if (!page) {
9833 err = -ENOMEM;
9834 goto out;
9835 }
9836
9837 flavors = page_address(page);
9838 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9839
9840 /*
9841 * Fall back on "guess and check" method if
9842 * the server doesn't support SECINFO_NO_NAME
9843 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009844 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009845 err = nfs4_find_root_sec(server, fhandle, info);
9846 goto out_freepage;
9847 }
9848 if (err)
9849 goto out_freepage;
9850
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009851 for (i = 0; i < flavors->num_flavors; i++) {
9852 secinfo = &flavors->flavors[i];
9853
9854 switch (secinfo->flavor) {
9855 case RPC_AUTH_NULL:
9856 case RPC_AUTH_UNIX:
9857 case RPC_AUTH_GSS:
9858 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9859 &secinfo->flavor_info);
9860 break;
9861 default:
9862 flavor = RPC_AUTH_MAXFLAVOR;
9863 break;
9864 }
9865
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009866 if (!nfs_auth_info_match(&server->auth_info, flavor))
9867 flavor = RPC_AUTH_MAXFLAVOR;
9868
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009869 if (flavor != RPC_AUTH_MAXFLAVOR) {
9870 err = nfs4_lookup_root_sec(server, fhandle,
9871 info, flavor);
9872 if (!err)
9873 break;
9874 }
9875 }
9876
9877 if (flavor == RPC_AUTH_MAXFLAVOR)
9878 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009879
9880out_freepage:
9881 put_page(page);
9882 if (err == -EACCES)
9883 return -EPERM;
9884out:
9885 return err;
9886}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009887
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009888static int _nfs41_test_stateid(struct nfs_server *server,
9889 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009890 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009891{
9892 int status;
9893 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009894 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009895 };
9896 struct nfs41_test_stateid_res res;
9897 struct rpc_message msg = {
9898 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9899 .rpc_argp = &args,
9900 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009901 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009902 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009903 struct rpc_clnt *rpc_client = server->client;
9904
9905 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9906 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009907
Chuck Lever38527b12012-07-11 16:30:23 -04009908 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009909 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009910 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009911 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009912 if (status != NFS_OK) {
9913 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009914 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009915 }
9916 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009917 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009918}
9919
Trond Myklebust43912bb2016-09-22 13:38:56 -04009920static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9921 int err, struct nfs4_exception *exception)
9922{
9923 exception->retry = 0;
9924 switch(err) {
9925 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009926 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009927 nfs4_handle_exception(server, err, exception);
9928 break;
9929 case -NFS4ERR_BADSESSION:
9930 case -NFS4ERR_BADSLOT:
9931 case -NFS4ERR_BAD_HIGH_SLOT:
9932 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9933 case -NFS4ERR_DEADSESSION:
9934 nfs4_do_handle_exception(server, err, exception);
9935 }
9936}
9937
Chuck Lever38527b12012-07-11 16:30:23 -04009938/**
9939 * nfs41_test_stateid - perform a TEST_STATEID operation
9940 *
9941 * @server: server / transport on which to perform the operation
9942 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009943 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009944 *
9945 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9946 * Otherwise a negative NFS4ERR value is returned if the operation
9947 * failed or the state ID is not currently valid.
9948 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009949static int nfs41_test_stateid(struct nfs_server *server,
9950 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009951 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009952{
Trond Myklebust0688e642019-04-07 13:59:09 -04009953 struct nfs4_exception exception = {
9954 .interruptible = true,
9955 };
Bryan Schumaker7d974792011-06-02 14:59:08 -04009956 int err;
9957 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009958 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009959 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009960 } while (exception.retry);
9961 return err;
9962}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009963
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009964struct nfs_free_stateid_data {
9965 struct nfs_server *server;
9966 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009967 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009968};
9969
9970static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9971{
9972 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009973 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009974 &data->args.seq_args,
9975 &data->res.seq_res,
9976 task);
9977}
9978
9979static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9980{
9981 struct nfs_free_stateid_data *data = calldata;
9982
9983 nfs41_sequence_done(task, &data->res.seq_res);
9984
9985 switch (task->tk_status) {
9986 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009987 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009988 rpc_restart_call_prepare(task);
9989 }
9990}
9991
9992static void nfs41_free_stateid_release(void *calldata)
9993{
9994 kfree(calldata);
9995}
9996
Trond Myklebust17f26b12013-08-21 15:48:42 -04009997static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009998 .rpc_call_prepare = nfs41_free_stateid_prepare,
9999 .rpc_call_done = nfs41_free_stateid_done,
10000 .rpc_release = nfs41_free_stateid_release,
10001};
10002
Anna Schumaker2f261022018-05-15 13:03:39 -040010003/**
10004 * nfs41_free_stateid - perform a FREE_STATEID operation
10005 *
10006 * @server: server / transport on which to perform the operation
10007 * @stateid: state ID to release
10008 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -050010009 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -040010010 *
10011 * Note: this function is always asynchronous.
10012 */
10013static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010014 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010015 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010016 bool privileged)
10017{
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010018 struct rpc_message msg = {
10019 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010020 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010021 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010022 struct rpc_task_setup task_setup = {
10023 .rpc_client = server->client,
10024 .rpc_message = &msg,
10025 .callback_ops = &nfs41_free_stateid_ops,
10026 .flags = RPC_TASK_ASYNC,
10027 };
10028 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -040010029 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010030
Weston Andros Adamson3787d502013-08-13 16:37:36 -040010031 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10032 &task_setup.rpc_client, &msg);
10033
Chuck Lever38527b12012-07-11 16:30:23 -040010034 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010035 data = kmalloc(sizeof(*data), GFP_NOFS);
10036 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -040010037 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010038 data->server = server;
10039 nfs4_stateid_copy(&data->args.stateid, stateid);
10040
10041 task_setup.callback_data = data;
10042
10043 msg.rpc_argp = &data->args;
10044 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -040010045 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -040010046 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010047 if (IS_ERR(task))
10048 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010049 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010050 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010051}
Trond Myklebust36281ca2012-03-04 18:13:56 -050010052
Jeff Laytonf1cdae82014-05-01 06:28:47 -040010053static void
10054nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010055{
NeilBrowna52458b2018-12-03 11:30:31 +110010056 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010057
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010058 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010059 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010060}
10061
Trond Myklebust36281ca2012-03-04 18:13:56 -050010062static bool nfs41_match_stateid(const nfs4_stateid *s1,
10063 const nfs4_stateid *s2)
10064{
Trond Myklebust93b717f2016-05-16 17:42:43 -040010065 if (s1->type != s2->type)
10066 return false;
10067
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010068 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010069 return false;
10070
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010071 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010072 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010073
Anna Schumaker045c5512017-01-11 16:59:48 -050010074 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010075}
10076
Andy Adamson557134a2009-04-01 09:21:53 -040010077#endif /* CONFIG_NFS_V4_1 */
10078
Trond Myklebust36281ca2012-03-04 18:13:56 -050010079static bool nfs4_match_stateid(const nfs4_stateid *s1,
10080 const nfs4_stateid *s2)
10081{
Trond Myklebustf597c532012-03-04 18:13:56 -050010082 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -050010083}
10084
10085
Trond Myklebust17280172012-03-11 13:11:00 -040010086static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010087 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010088 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010089 .recover_open = nfs4_open_reclaim,
10090 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -040010091 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -040010092 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010093};
10094
Andy Adamson591d71c2009-04-01 09:22:47 -040010095#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010096static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010097 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10098 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10099 .recover_open = nfs4_open_reclaim,
10100 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -050010101 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -050010102 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -040010103 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -040010104};
10105#endif /* CONFIG_NFS_V4_1 */
10106
Trond Myklebust17280172012-03-11 13:11:00 -040010107static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010108 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010109 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +030010110 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010111 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -040010112 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010113};
10114
Andy Adamson591d71c2009-04-01 09:22:47 -040010115#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010116static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010117 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10118 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -040010119 .recover_open = nfs41_open_expired,
10120 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -050010121 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -040010122};
10123#endif /* CONFIG_NFS_V4_1 */
10124
Trond Myklebust17280172012-03-11 13:11:00 -040010125static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010126 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +110010127 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010128 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -040010129};
10130
10131#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010132static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010133 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +110010134 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010135 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -040010136};
10137#endif
10138
Chuck Leverec011fe2013-10-17 14:12:39 -040010139static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010140 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010141 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010142};
10143
10144#if defined(CONFIG_NFS_V4_1)
10145static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010146 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010147 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010148};
10149#endif /* CONFIG_NFS_V4_1 */
10150
Trond Myklebust97dc1352010-06-16 09:52:26 -040010151static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10152 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010153 .init_caps = NFS_CAP_READDIRPLUS
10154 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010155 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010156 .init_client = nfs40_init_client,
10157 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010158 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010159 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010160 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -040010161 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010162 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -040010163 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010164 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10165 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10166 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010167 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010168};
10169
10170#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -050010171static struct nfs_seqid *
10172nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10173{
10174 return NULL;
10175}
10176
Trond Myklebust97dc1352010-06-16 09:52:26 -040010177static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10178 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010179 .init_caps = NFS_CAP_READDIRPLUS
10180 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -040010181 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -040010182 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -040010183 | NFS_CAP_ATOMIC_OPEN_V1
10184 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010185 .init_client = nfs41_init_client,
10186 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010187 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010188 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010189 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -040010190 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010191 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010192 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -040010193 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010194 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10195 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10196 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010197 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010198};
10199#endif
10200
Steve Dickson42c2c422013-05-22 12:50:38 -040010201#if defined(CONFIG_NFS_V4_2)
10202static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10203 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -040010204 .init_caps = NFS_CAP_READDIRPLUS
10205 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -040010206 | NFS_CAP_POSIX_LOCK
10207 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -040010208 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -040010209 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -050010210 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -040010211 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -040010212 | NFS_CAP_OFFLOAD_CANCEL
Olga Kornievskaia04915672019-06-04 16:14:30 -040010213 | NFS_CAP_COPY_NOTIFY
Anna Schumaker624bd5b2014-11-25 13:18:16 -050010214 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -040010215 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +080010216 | NFS_CAP_LAYOUTSTATS
Trond Myklebust3eb86092019-02-08 10:31:05 -050010217 | NFS_CAP_CLONE
10218 | NFS_CAP_LAYOUTERROR,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010219 .init_client = nfs41_init_client,
10220 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -040010221 .match_stateid = nfs41_match_stateid,
10222 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -040010223 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -040010224 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -040010225 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010226 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010227 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -040010228 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10229 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10230 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +080010231 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -040010232};
10233#endif
10234
Trond Myklebust97dc1352010-06-16 09:52:26 -040010235const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10236 [0] = &nfs_v4_0_minor_ops,
10237#if defined(CONFIG_NFS_V4_1)
10238 [1] = &nfs_v4_1_minor_ops,
10239#endif
Steve Dickson42c2c422013-05-22 12:50:38 -040010240#if defined(CONFIG_NFS_V4_2)
10241 [2] = &nfs_v4_2_minor_ops,
10242#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -040010243};
10244
Trond Myklebust13997822016-07-24 17:10:52 -040010245static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010246{
Frank van der Linden012a2112020-06-23 22:39:03 +000010247 ssize_t error, error2, error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010248
10249 error = generic_listxattr(dentry, list, size);
10250 if (error < 0)
10251 return error;
10252 if (list) {
10253 list += error;
10254 size -= error;
10255 }
10256
10257 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10258 if (error2 < 0)
10259 return error2;
Frank van der Linden012a2112020-06-23 22:39:03 +000010260
10261 if (list) {
10262 list += error2;
10263 size -= error2;
10264 }
10265
10266 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10267 if (error3 < 0)
10268 return error3;
10269
10270 return error + error2 + error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010271}
10272
Trond Myklebust17f26b12013-08-21 15:48:42 -040010273static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -040010274 .create = nfs_create,
10275 .lookup = nfs_lookup,
10276 .atomic_open = nfs_atomic_open,
10277 .link = nfs_link,
10278 .unlink = nfs_unlink,
10279 .symlink = nfs_symlink,
10280 .mkdir = nfs_mkdir,
10281 .rmdir = nfs_rmdir,
10282 .mknod = nfs_mknod,
10283 .rename = nfs_rename,
10284 .permission = nfs_permission,
10285 .getattr = nfs_getattr,
10286 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010287 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -040010288};
10289
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080010290static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010291 .permission = nfs_permission,
10292 .getattr = nfs_getattr,
10293 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010294 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010295};
10296
David Howells509de812006-08-22 20:06:11 -040010297const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010298 .version = 4, /* protocol version */
10299 .dentry_ops = &nfs4_dentry_operations,
10300 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010301 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -040010302 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010303 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -040010304 .submount = nfs4_submount,
David Howellsf2aedb72019-12-10 07:31:13 -050010305 .try_get_tree = nfs4_try_get_tree,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010306 .getattr = nfs4_proc_getattr,
10307 .setattr = nfs4_proc_setattr,
10308 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -070010309 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010310 .access = nfs4_proc_access,
10311 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010312 .create = nfs4_proc_create,
10313 .remove = nfs4_proc_remove,
10314 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -040010315 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010316 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -040010317 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -040010318 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -040010319 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010320 .link = nfs4_proc_link,
10321 .symlink = nfs4_proc_symlink,
10322 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -040010323 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010324 .readdir = nfs4_proc_readdir,
10325 .mknod = nfs4_proc_mknod,
10326 .statfs = nfs4_proc_statfs,
10327 .fsinfo = nfs4_proc_fsinfo,
10328 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -040010329 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010330 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -040010331 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010332 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -050010333 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010334 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010335 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010336 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -040010337 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010338 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010339 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +000010340 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -040010341 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -040010342 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -040010343 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -040010344 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +000010345 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -040010346 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -040010347 .create_server = nfs4_create_server,
10348 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010349};
10350
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010351static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +010010352 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010353 .list = nfs4_xattr_list_nfs4_acl,
10354 .get = nfs4_xattr_get_nfs4_acl,
10355 .set = nfs4_xattr_set_nfs4_acl,
10356};
10357
Frank van der Linden012a2112020-06-23 22:39:03 +000010358#ifdef CONFIG_NFS_V4_2
10359static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10360 .prefix = XATTR_USER_PREFIX,
10361 .get = nfs4_xattr_get_nfs4_user,
10362 .set = nfs4_xattr_set_nfs4_user,
10363};
10364#endif
10365
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010366const struct xattr_handler *nfs4_xattr_handlers[] = {
10367 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -040010368#ifdef CONFIG_NFS_V4_SECURITY_LABEL
10369 &nfs4_xattr_nfs4_label_handler,
10370#endif
Frank van der Linden012a2112020-06-23 22:39:03 +000010371#ifdef CONFIG_NFS_V4_2
10372 &nfs4_xattr_nfs4_user_handler,
10373#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010374 NULL
10375};
10376
Linus Torvalds1da177e2005-04-16 15:20:36 -070010377/*
10378 * Local variables:
10379 * c-basic-offset: 8
10380 * End:
10381 */