xref: /linux/fs/nfs/nfs4proc.c (revision a6a0f04e7d28378c181f76d32e4f965aa6a8b0a5)
1 /*
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>
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #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>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
56 #include <linux/freezer.h>
57 #include <linux/iversion.h>
58 
59 #include "nfs4_fs.h"
60 #include "delegation.h"
61 #include "internal.h"
62 #include "iostat.h"
63 #include "callback.h"
64 #include "pnfs.h"
65 #include "netns.h"
66 #include "sysfs.h"
67 #include "nfs4idmap.h"
68 #include "nfs4session.h"
69 #include "fscache.h"
70 #include "nfs42.h"
71 
72 #include "nfs4trace.h"
73 
74 #define NFSDBG_FACILITY		NFSDBG_PROC
75 
76 #define NFS4_BITMASK_SZ		3
77 
78 #define NFS4_POLL_RETRY_MIN	(HZ/10)
79 #define NFS4_POLL_RETRY_MAX	(15*HZ)
80 
81 /* file attributes which can be mapped to nfs attributes */
82 #define NFS4_VALID_ATTRS (ATTR_MODE \
83 	| ATTR_UID \
84 	| ATTR_GID \
85 	| ATTR_SIZE \
86 	| ATTR_ATIME \
87 	| ATTR_MTIME \
88 	| ATTR_CTIME \
89 	| ATTR_ATIME_SET \
90 	| ATTR_MTIME_SET)
91 
92 struct nfs4_opendata;
93 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
94 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
95 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
96 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
97 			      struct nfs_fattr *fattr, struct inode *inode);
98 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
99 			    struct nfs_fattr *fattr, struct iattr *sattr,
100 			    struct nfs_open_context *ctx, struct nfs4_label *ilabel);
101 #ifdef CONFIG_NFS_V4_1
102 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
103 		const struct cred *cred,
104 		struct nfs4_slot *slot,
105 		bool is_privileged);
106 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
107 		const struct cred *);
108 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
109 		const struct cred *, bool);
110 #endif
111 
112 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
113 static inline struct nfs4_label *
114 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
115 	struct iattr *sattr, struct nfs4_label *label)
116 {
117 	int err;
118 
119 	if (label == NULL)
120 		return NULL;
121 
122 	if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
123 		return NULL;
124 
125 	label->lfs = 0;
126 	label->pi = 0;
127 	label->len = 0;
128 	label->label = NULL;
129 
130 	err = security_dentry_init_security(dentry, sattr->ia_mode,
131 				&dentry->d_name, NULL,
132 				(void **)&label->label, &label->len);
133 	if (err == 0)
134 		return label;
135 
136 	return NULL;
137 }
138 static inline void
139 nfs4_label_release_security(struct nfs4_label *label)
140 {
141 	if (label)
142 		security_release_secctx(label->label, label->len);
143 }
144 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
145 {
146 	if (label)
147 		return server->attr_bitmask;
148 
149 	return server->attr_bitmask_nl;
150 }
151 #else
152 static inline struct nfs4_label *
153 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
154 	struct iattr *sattr, struct nfs4_label *l)
155 { return NULL; }
156 static inline void
157 nfs4_label_release_security(struct nfs4_label *label)
158 { return; }
159 static inline u32 *
160 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
161 { return server->attr_bitmask; }
162 #endif
163 
164 /* Prevent leaks of NFSv4 errors into userland */
165 static int nfs4_map_errors(int err)
166 {
167 	if (err >= -1000)
168 		return err;
169 	switch (err) {
170 	case -NFS4ERR_RESOURCE:
171 	case -NFS4ERR_LAYOUTTRYLATER:
172 	case -NFS4ERR_RECALLCONFLICT:
173 	case -NFS4ERR_RETURNCONFLICT:
174 		return -EREMOTEIO;
175 	case -NFS4ERR_WRONGSEC:
176 	case -NFS4ERR_WRONG_CRED:
177 		return -EPERM;
178 	case -NFS4ERR_BADOWNER:
179 	case -NFS4ERR_BADNAME:
180 		return -EINVAL;
181 	case -NFS4ERR_SHARE_DENIED:
182 		return -EACCES;
183 	case -NFS4ERR_MINOR_VERS_MISMATCH:
184 		return -EPROTONOSUPPORT;
185 	case -NFS4ERR_FILE_OPEN:
186 		return -EBUSY;
187 	case -NFS4ERR_NOT_SAME:
188 		return -ENOTSYNC;
189 	default:
190 		dprintk("%s could not handle NFSv4 error %d\n",
191 				__func__, -err);
192 		break;
193 	}
194 	return -EIO;
195 }
196 
197 /*
198  * This is our standard bitmap for GETATTR requests.
199  */
200 const u32 nfs4_fattr_bitmap[3] = {
201 	FATTR4_WORD0_TYPE
202 	| FATTR4_WORD0_CHANGE
203 	| FATTR4_WORD0_SIZE
204 	| FATTR4_WORD0_FSID
205 	| FATTR4_WORD0_FILEID,
206 	FATTR4_WORD1_MODE
207 	| FATTR4_WORD1_NUMLINKS
208 	| FATTR4_WORD1_OWNER
209 	| FATTR4_WORD1_OWNER_GROUP
210 	| FATTR4_WORD1_RAWDEV
211 	| FATTR4_WORD1_SPACE_USED
212 	| FATTR4_WORD1_TIME_ACCESS
213 	| FATTR4_WORD1_TIME_METADATA
214 	| FATTR4_WORD1_TIME_MODIFY
215 	| FATTR4_WORD1_MOUNTED_ON_FILEID,
216 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
217 	FATTR4_WORD2_SECURITY_LABEL
218 #endif
219 };
220 
221 static const u32 nfs4_pnfs_open_bitmap[3] = {
222 	FATTR4_WORD0_TYPE
223 	| FATTR4_WORD0_CHANGE
224 	| FATTR4_WORD0_SIZE
225 	| FATTR4_WORD0_FSID
226 	| FATTR4_WORD0_FILEID,
227 	FATTR4_WORD1_MODE
228 	| FATTR4_WORD1_NUMLINKS
229 	| FATTR4_WORD1_OWNER
230 	| FATTR4_WORD1_OWNER_GROUP
231 	| FATTR4_WORD1_RAWDEV
232 	| FATTR4_WORD1_SPACE_USED
233 	| FATTR4_WORD1_TIME_ACCESS
234 	| FATTR4_WORD1_TIME_METADATA
235 	| FATTR4_WORD1_TIME_MODIFY,
236 	FATTR4_WORD2_MDSTHRESHOLD
237 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
238 	| FATTR4_WORD2_SECURITY_LABEL
239 #endif
240 };
241 
242 static const u32 nfs4_open_noattr_bitmap[3] = {
243 	FATTR4_WORD0_TYPE
244 	| FATTR4_WORD0_FILEID,
245 };
246 
247 const u32 nfs4_statfs_bitmap[3] = {
248 	FATTR4_WORD0_FILES_AVAIL
249 	| FATTR4_WORD0_FILES_FREE
250 	| FATTR4_WORD0_FILES_TOTAL,
251 	FATTR4_WORD1_SPACE_AVAIL
252 	| FATTR4_WORD1_SPACE_FREE
253 	| FATTR4_WORD1_SPACE_TOTAL
254 };
255 
256 const u32 nfs4_pathconf_bitmap[3] = {
257 	FATTR4_WORD0_MAXLINK
258 	| FATTR4_WORD0_MAXNAME,
259 	0
260 };
261 
262 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
263 			| FATTR4_WORD0_MAXREAD
264 			| FATTR4_WORD0_MAXWRITE
265 			| FATTR4_WORD0_LEASE_TIME,
266 			FATTR4_WORD1_TIME_DELTA
267 			| FATTR4_WORD1_FS_LAYOUT_TYPES,
268 			FATTR4_WORD2_LAYOUT_BLKSIZE
269 			| FATTR4_WORD2_CLONE_BLKSIZE
270 			| FATTR4_WORD2_CHANGE_ATTR_TYPE
271 			| FATTR4_WORD2_XATTR_SUPPORT
272 };
273 
274 const u32 nfs4_fs_locations_bitmap[3] = {
275 	FATTR4_WORD0_CHANGE
276 	| FATTR4_WORD0_SIZE
277 	| FATTR4_WORD0_FSID
278 	| FATTR4_WORD0_FILEID
279 	| FATTR4_WORD0_FS_LOCATIONS,
280 	FATTR4_WORD1_OWNER
281 	| FATTR4_WORD1_OWNER_GROUP
282 	| FATTR4_WORD1_RAWDEV
283 	| FATTR4_WORD1_SPACE_USED
284 	| FATTR4_WORD1_TIME_ACCESS
285 	| FATTR4_WORD1_TIME_METADATA
286 	| FATTR4_WORD1_TIME_MODIFY
287 	| FATTR4_WORD1_MOUNTED_ON_FILEID,
288 };
289 
290 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
291 				    struct inode *inode, unsigned long flags)
292 {
293 	unsigned long cache_validity;
294 
295 	memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
296 	if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
297 		return;
298 
299 	cache_validity = READ_ONCE(NFS_I(inode)->cache_validity) | flags;
300 
301 	/* Remove the attributes over which we have full control */
302 	dst[1] &= ~FATTR4_WORD1_RAWDEV;
303 	if (!(cache_validity & NFS_INO_INVALID_SIZE))
304 		dst[0] &= ~FATTR4_WORD0_SIZE;
305 
306 	if (!(cache_validity & NFS_INO_INVALID_CHANGE))
307 		dst[0] &= ~FATTR4_WORD0_CHANGE;
308 
309 	if (!(cache_validity & NFS_INO_INVALID_MODE))
310 		dst[1] &= ~FATTR4_WORD1_MODE;
311 	if (!(cache_validity & NFS_INO_INVALID_OTHER))
312 		dst[1] &= ~(FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP);
313 }
314 
315 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
316 		struct nfs4_readdir_arg *readdir)
317 {
318 	unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
319 	__be32 *start, *p;
320 
321 	if (cookie > 2) {
322 		readdir->cookie = cookie;
323 		memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
324 		return;
325 	}
326 
327 	readdir->cookie = 0;
328 	memset(&readdir->verifier, 0, sizeof(readdir->verifier));
329 	if (cookie == 2)
330 		return;
331 
332 	/*
333 	 * NFSv4 servers do not return entries for '.' and '..'
334 	 * Therefore, we fake these entries here.  We let '.'
335 	 * have cookie 0 and '..' have cookie 1.  Note that
336 	 * when talking to the server, we always send cookie 0
337 	 * instead of 1 or 2.
338 	 */
339 	start = p = kmap_atomic(*readdir->pages);
340 
341 	if (cookie == 0) {
342 		*p++ = xdr_one;                                  /* next */
343 		*p++ = xdr_zero;                   /* cookie, first word */
344 		*p++ = xdr_one;                   /* cookie, second word */
345 		*p++ = xdr_one;                             /* entry len */
346 		memcpy(p, ".\0\0\0", 4);                        /* entry */
347 		p++;
348 		*p++ = xdr_one;                         /* bitmap length */
349 		*p++ = htonl(attrs);                           /* bitmap */
350 		*p++ = htonl(12);             /* attribute buffer length */
351 		*p++ = htonl(NF4DIR);
352 		p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
353 	}
354 
355 	*p++ = xdr_one;                                  /* next */
356 	*p++ = xdr_zero;                   /* cookie, first word */
357 	*p++ = xdr_two;                   /* cookie, second word */
358 	*p++ = xdr_two;                             /* entry len */
359 	memcpy(p, "..\0\0", 4);                         /* entry */
360 	p++;
361 	*p++ = xdr_one;                         /* bitmap length */
362 	*p++ = htonl(attrs);                           /* bitmap */
363 	*p++ = htonl(12);             /* attribute buffer length */
364 	*p++ = htonl(NF4DIR);
365 	p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
366 
367 	readdir->pgbase = (char *)p - (char *)start;
368 	readdir->count -= readdir->pgbase;
369 	kunmap_atomic(start);
370 }
371 
372 static void nfs4_fattr_set_prechange(struct nfs_fattr *fattr, u64 version)
373 {
374 	if (!(fattr->valid & NFS_ATTR_FATTR_PRECHANGE)) {
375 		fattr->pre_change_attr = version;
376 		fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
377 	}
378 }
379 
380 static void nfs4_test_and_free_stateid(struct nfs_server *server,
381 		nfs4_stateid *stateid,
382 		const struct cred *cred)
383 {
384 	const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
385 
386 	ops->test_and_free_expired(server, stateid, cred);
387 }
388 
389 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
390 		nfs4_stateid *stateid,
391 		const struct cred *cred)
392 {
393 	stateid->type = NFS4_REVOKED_STATEID_TYPE;
394 	nfs4_test_and_free_stateid(server, stateid, cred);
395 }
396 
397 static void nfs4_free_revoked_stateid(struct nfs_server *server,
398 		const nfs4_stateid *stateid,
399 		const struct cred *cred)
400 {
401 	nfs4_stateid tmp;
402 
403 	nfs4_stateid_copy(&tmp, stateid);
404 	__nfs4_free_revoked_stateid(server, &tmp, cred);
405 }
406 
407 static long nfs4_update_delay(long *timeout)
408 {
409 	long ret;
410 	if (!timeout)
411 		return NFS4_POLL_RETRY_MAX;
412 	if (*timeout <= 0)
413 		*timeout = NFS4_POLL_RETRY_MIN;
414 	if (*timeout > NFS4_POLL_RETRY_MAX)
415 		*timeout = NFS4_POLL_RETRY_MAX;
416 	ret = *timeout;
417 	*timeout <<= 1;
418 	return ret;
419 }
420 
421 static int nfs4_delay_killable(long *timeout)
422 {
423 	might_sleep();
424 
425 	__set_current_state(TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
426 	schedule_timeout(nfs4_update_delay(timeout));
427 	if (!__fatal_signal_pending(current))
428 		return 0;
429 	return -EINTR;
430 }
431 
432 static int nfs4_delay_interruptible(long *timeout)
433 {
434 	might_sleep();
435 
436 	__set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE_UNSAFE);
437 	schedule_timeout(nfs4_update_delay(timeout));
438 	if (!signal_pending(current))
439 		return 0;
440 	return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
441 }
442 
443 static int nfs4_delay(long *timeout, bool interruptible)
444 {
445 	if (interruptible)
446 		return nfs4_delay_interruptible(timeout);
447 	return nfs4_delay_killable(timeout);
448 }
449 
450 static const nfs4_stateid *
451 nfs4_recoverable_stateid(const nfs4_stateid *stateid)
452 {
453 	if (!stateid)
454 		return NULL;
455 	switch (stateid->type) {
456 	case NFS4_OPEN_STATEID_TYPE:
457 	case NFS4_LOCK_STATEID_TYPE:
458 	case NFS4_DELEGATION_STATEID_TYPE:
459 		return stateid;
460 	default:
461 		break;
462 	}
463 	return NULL;
464 }
465 
466 /* This is the error handling routine for processes that are allowed
467  * to sleep.
468  */
469 static int nfs4_do_handle_exception(struct nfs_server *server,
470 		int errorcode, struct nfs4_exception *exception)
471 {
472 	struct nfs_client *clp = server->nfs_client;
473 	struct nfs4_state *state = exception->state;
474 	const nfs4_stateid *stateid;
475 	struct inode *inode = exception->inode;
476 	int ret = errorcode;
477 
478 	exception->delay = 0;
479 	exception->recovering = 0;
480 	exception->retry = 0;
481 
482 	stateid = nfs4_recoverable_stateid(exception->stateid);
483 	if (stateid == NULL && state != NULL)
484 		stateid = nfs4_recoverable_stateid(&state->stateid);
485 
486 	switch(errorcode) {
487 		case 0:
488 			return 0;
489 		case -NFS4ERR_BADHANDLE:
490 		case -ESTALE:
491 			if (inode != NULL && S_ISREG(inode->i_mode))
492 				pnfs_destroy_layout(NFS_I(inode));
493 			break;
494 		case -NFS4ERR_DELEG_REVOKED:
495 		case -NFS4ERR_ADMIN_REVOKED:
496 		case -NFS4ERR_EXPIRED:
497 		case -NFS4ERR_BAD_STATEID:
498 		case -NFS4ERR_PARTNER_NO_AUTH:
499 			if (inode != NULL && stateid != NULL) {
500 				nfs_inode_find_state_and_recover(inode,
501 						stateid);
502 				goto wait_on_recovery;
503 			}
504 			fallthrough;
505 		case -NFS4ERR_OPENMODE:
506 			if (inode) {
507 				int err;
508 
509 				err = nfs_async_inode_return_delegation(inode,
510 						stateid);
511 				if (err == 0)
512 					goto wait_on_recovery;
513 				if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
514 					exception->retry = 1;
515 					break;
516 				}
517 			}
518 			if (state == NULL)
519 				break;
520 			ret = nfs4_schedule_stateid_recovery(server, state);
521 			if (ret < 0)
522 				break;
523 			goto wait_on_recovery;
524 		case -NFS4ERR_STALE_STATEID:
525 		case -NFS4ERR_STALE_CLIENTID:
526 			nfs4_schedule_lease_recovery(clp);
527 			goto wait_on_recovery;
528 		case -NFS4ERR_MOVED:
529 			ret = nfs4_schedule_migration_recovery(server);
530 			if (ret < 0)
531 				break;
532 			goto wait_on_recovery;
533 		case -NFS4ERR_LEASE_MOVED:
534 			nfs4_schedule_lease_moved_recovery(clp);
535 			goto wait_on_recovery;
536 #if defined(CONFIG_NFS_V4_1)
537 		case -NFS4ERR_BADSESSION:
538 		case -NFS4ERR_BADSLOT:
539 		case -NFS4ERR_BAD_HIGH_SLOT:
540 		case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
541 		case -NFS4ERR_DEADSESSION:
542 		case -NFS4ERR_SEQ_FALSE_RETRY:
543 		case -NFS4ERR_SEQ_MISORDERED:
544 			/* Handled in nfs41_sequence_process() */
545 			goto wait_on_recovery;
546 #endif /* defined(CONFIG_NFS_V4_1) */
547 		case -NFS4ERR_FILE_OPEN:
548 			if (exception->timeout > HZ) {
549 				/* We have retried a decent amount, time to
550 				 * fail
551 				 */
552 				ret = -EBUSY;
553 				break;
554 			}
555 			fallthrough;
556 		case -NFS4ERR_DELAY:
557 			nfs_inc_server_stats(server, NFSIOS_DELAY);
558 			fallthrough;
559 		case -NFS4ERR_GRACE:
560 		case -NFS4ERR_LAYOUTTRYLATER:
561 		case -NFS4ERR_RECALLCONFLICT:
562 		case -NFS4ERR_RETURNCONFLICT:
563 			exception->delay = 1;
564 			return 0;
565 
566 		case -NFS4ERR_RETRY_UNCACHED_REP:
567 		case -NFS4ERR_OLD_STATEID:
568 			exception->retry = 1;
569 			break;
570 		case -NFS4ERR_BADOWNER:
571 			/* The following works around a Linux server bug! */
572 		case -NFS4ERR_BADNAME:
573 			if (server->caps & NFS_CAP_UIDGID_NOMAP) {
574 				server->caps &= ~NFS_CAP_UIDGID_NOMAP;
575 				exception->retry = 1;
576 				printk(KERN_WARNING "NFS: v4 server %s "
577 						"does not accept raw "
578 						"uid/gids. "
579 						"Reenabling the idmapper.\n",
580 						server->nfs_client->cl_hostname);
581 			}
582 	}
583 	/* We failed to handle the error */
584 	return nfs4_map_errors(ret);
585 wait_on_recovery:
586 	exception->recovering = 1;
587 	return 0;
588 }
589 
590 /*
591  * Track the number of NFS4ERR_DELAY related retransmissions and return
592  * EAGAIN if the 'softerr' mount option is set, and we've exceeded the limit
593  * set by 'nfs_delay_retrans'.
594  */
595 static int nfs4_exception_should_retrans(const struct nfs_server *server,
596 					 struct nfs4_exception *exception)
597 {
598 	if (server->flags & NFS_MOUNT_SOFTERR && nfs_delay_retrans >= 0) {
599 		if (exception->retrans++ >= (unsigned short)nfs_delay_retrans)
600 			return -EAGAIN;
601 	}
602 	return 0;
603 }
604 
605 /* This is the error handling routine for processes that are allowed
606  * to sleep.
607  */
608 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
609 {
610 	struct nfs_client *clp = server->nfs_client;
611 	int ret;
612 
613 	ret = nfs4_do_handle_exception(server, errorcode, exception);
614 	if (exception->delay) {
615 		int ret2 = nfs4_exception_should_retrans(server, exception);
616 		if (ret2 < 0) {
617 			exception->retry = 0;
618 			return ret2;
619 		}
620 		ret = nfs4_delay(&exception->timeout,
621 				exception->interruptible);
622 		goto out_retry;
623 	}
624 	if (exception->recovering) {
625 		if (exception->task_is_privileged)
626 			return -EDEADLOCK;
627 		ret = nfs4_wait_clnt_recover(clp);
628 		if (test_bit(NFS_MIG_FAILED, &server->mig_status))
629 			return -EIO;
630 		goto out_retry;
631 	}
632 	return ret;
633 out_retry:
634 	if (ret == 0)
635 		exception->retry = 1;
636 	return ret;
637 }
638 
639 static int
640 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
641 		int errorcode, struct nfs4_exception *exception)
642 {
643 	struct nfs_client *clp = server->nfs_client;
644 	int ret;
645 
646 	ret = nfs4_do_handle_exception(server, errorcode, exception);
647 	if (exception->delay) {
648 		int ret2 = nfs4_exception_should_retrans(server, exception);
649 		if (ret2 < 0) {
650 			exception->retry = 0;
651 			return ret2;
652 		}
653 		rpc_delay(task, nfs4_update_delay(&exception->timeout));
654 		goto out_retry;
655 	}
656 	if (exception->recovering) {
657 		if (exception->task_is_privileged)
658 			return -EDEADLOCK;
659 		rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
660 		if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
661 			rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
662 		goto out_retry;
663 	}
664 	if (test_bit(NFS_MIG_FAILED, &server->mig_status))
665 		ret = -EIO;
666 	return ret;
667 out_retry:
668 	if (ret == 0) {
669 		exception->retry = 1;
670 		/*
671 		 * For NFS4ERR_MOVED, the client transport will need to
672 		 * be recomputed after migration recovery has completed.
673 		 */
674 		if (errorcode == -NFS4ERR_MOVED)
675 			rpc_task_release_transport(task);
676 	}
677 	return ret;
678 }
679 
680 int
681 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
682 			struct nfs4_state *state, long *timeout)
683 {
684 	struct nfs4_exception exception = {
685 		.state = state,
686 	};
687 
688 	if (task->tk_status >= 0)
689 		return 0;
690 	if (timeout)
691 		exception.timeout = *timeout;
692 	task->tk_status = nfs4_async_handle_exception(task, server,
693 			task->tk_status,
694 			&exception);
695 	if (exception.delay && timeout)
696 		*timeout = exception.timeout;
697 	if (exception.retry)
698 		return -EAGAIN;
699 	return 0;
700 }
701 
702 /*
703  * Return 'true' if 'clp' is using an rpc_client that is integrity protected
704  * or 'false' otherwise.
705  */
706 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
707 {
708 	rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
709 	return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
710 }
711 
712 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
713 {
714 	spin_lock(&clp->cl_lock);
715 	if (time_before(clp->cl_last_renewal,timestamp))
716 		clp->cl_last_renewal = timestamp;
717 	spin_unlock(&clp->cl_lock);
718 }
719 
720 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
721 {
722 	struct nfs_client *clp = server->nfs_client;
723 
724 	if (!nfs4_has_session(clp))
725 		do_renew_lease(clp, timestamp);
726 }
727 
728 struct nfs4_call_sync_data {
729 	const struct nfs_server *seq_server;
730 	struct nfs4_sequence_args *seq_args;
731 	struct nfs4_sequence_res *seq_res;
732 };
733 
734 void nfs4_init_sequence(struct nfs4_sequence_args *args,
735 			struct nfs4_sequence_res *res, int cache_reply,
736 			int privileged)
737 {
738 	args->sa_slot = NULL;
739 	args->sa_cache_this = cache_reply;
740 	args->sa_privileged = privileged;
741 
742 	res->sr_slot = NULL;
743 }
744 
745 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
746 {
747 	struct nfs4_slot *slot = res->sr_slot;
748 	struct nfs4_slot_table *tbl;
749 
750 	tbl = slot->table;
751 	spin_lock(&tbl->slot_tbl_lock);
752 	if (!nfs41_wake_and_assign_slot(tbl, slot))
753 		nfs4_free_slot(tbl, slot);
754 	spin_unlock(&tbl->slot_tbl_lock);
755 
756 	res->sr_slot = NULL;
757 }
758 
759 static int nfs40_sequence_done(struct rpc_task *task,
760 			       struct nfs4_sequence_res *res)
761 {
762 	if (res->sr_slot != NULL)
763 		nfs40_sequence_free_slot(res);
764 	return 1;
765 }
766 
767 #if defined(CONFIG_NFS_V4_1)
768 
769 static void nfs41_release_slot(struct nfs4_slot *slot)
770 {
771 	struct nfs4_session *session;
772 	struct nfs4_slot_table *tbl;
773 	bool send_new_highest_used_slotid = false;
774 
775 	if (!slot)
776 		return;
777 	tbl = slot->table;
778 	session = tbl->session;
779 
780 	/* Bump the slot sequence number */
781 	if (slot->seq_done)
782 		slot->seq_nr++;
783 	slot->seq_done = 0;
784 
785 	spin_lock(&tbl->slot_tbl_lock);
786 	/* Be nice to the server: try to ensure that the last transmitted
787 	 * value for highest_user_slotid <= target_highest_slotid
788 	 */
789 	if (tbl->highest_used_slotid > tbl->target_highest_slotid)
790 		send_new_highest_used_slotid = true;
791 
792 	if (nfs41_wake_and_assign_slot(tbl, slot)) {
793 		send_new_highest_used_slotid = false;
794 		goto out_unlock;
795 	}
796 	nfs4_free_slot(tbl, slot);
797 
798 	if (tbl->highest_used_slotid != NFS4_NO_SLOT)
799 		send_new_highest_used_slotid = false;
800 out_unlock:
801 	spin_unlock(&tbl->slot_tbl_lock);
802 	if (send_new_highest_used_slotid)
803 		nfs41_notify_server(session->clp);
804 	if (waitqueue_active(&tbl->slot_waitq))
805 		wake_up_all(&tbl->slot_waitq);
806 }
807 
808 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
809 {
810 	nfs41_release_slot(res->sr_slot);
811 	res->sr_slot = NULL;
812 }
813 
814 static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
815 		u32 seqnr)
816 {
817 	if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
818 		slot->seq_nr_highest_sent = seqnr;
819 }
820 static void nfs4_slot_sequence_acked(struct nfs4_slot *slot, u32 seqnr)
821 {
822 	nfs4_slot_sequence_record_sent(slot, seqnr);
823 	slot->seq_nr_last_acked = seqnr;
824 }
825 
826 static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
827 				struct nfs4_slot *slot)
828 {
829 	struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
830 	if (!IS_ERR(task))
831 		rpc_put_task_async(task);
832 }
833 
834 static int nfs41_sequence_process(struct rpc_task *task,
835 		struct nfs4_sequence_res *res)
836 {
837 	struct nfs4_session *session;
838 	struct nfs4_slot *slot = res->sr_slot;
839 	struct nfs_client *clp;
840 	int status;
841 	int ret = 1;
842 
843 	if (slot == NULL)
844 		goto out_noaction;
845 	/* don't increment the sequence number if the task wasn't sent */
846 	if (!RPC_WAS_SENT(task) || slot->seq_done)
847 		goto out;
848 
849 	session = slot->table->session;
850 	clp = session->clp;
851 
852 	trace_nfs4_sequence_done(session, res);
853 
854 	status = res->sr_status;
855 	if (task->tk_status == -NFS4ERR_DEADSESSION)
856 		status = -NFS4ERR_DEADSESSION;
857 
858 	/* Check the SEQUENCE operation status */
859 	switch (status) {
860 	case 0:
861 		/* Mark this sequence number as having been acked */
862 		nfs4_slot_sequence_acked(slot, slot->seq_nr);
863 		/* Update the slot's sequence and clientid lease timer */
864 		slot->seq_done = 1;
865 		do_renew_lease(clp, res->sr_timestamp);
866 		/* Check sequence flags */
867 		nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
868 				!!slot->privileged);
869 		nfs41_update_target_slotid(slot->table, slot, res);
870 		break;
871 	case 1:
872 		/*
873 		 * sr_status remains 1 if an RPC level error occurred.
874 		 * The server may or may not have processed the sequence
875 		 * operation..
876 		 */
877 		nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
878 		slot->seq_done = 1;
879 		goto out;
880 	case -NFS4ERR_DELAY:
881 		/* The server detected a resend of the RPC call and
882 		 * returned NFS4ERR_DELAY as per Section 2.10.6.2
883 		 * of RFC5661.
884 		 */
885 		dprintk("%s: slot=%u seq=%u: Operation in progress\n",
886 			__func__,
887 			slot->slot_nr,
888 			slot->seq_nr);
889 		goto out_retry;
890 	case -NFS4ERR_RETRY_UNCACHED_REP:
891 	case -NFS4ERR_SEQ_FALSE_RETRY:
892 		/*
893 		 * The server thinks we tried to replay a request.
894 		 * Retry the call after bumping the sequence ID.
895 		 */
896 		nfs4_slot_sequence_acked(slot, slot->seq_nr);
897 		goto retry_new_seq;
898 	case -NFS4ERR_BADSLOT:
899 		/*
900 		 * The slot id we used was probably retired. Try again
901 		 * using a different slot id.
902 		 */
903 		if (slot->slot_nr < slot->table->target_highest_slotid)
904 			goto session_recover;
905 		goto retry_nowait;
906 	case -NFS4ERR_SEQ_MISORDERED:
907 		nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
908 		/*
909 		 * Were one or more calls using this slot interrupted?
910 		 * If the server never received the request, then our
911 		 * transmitted slot sequence number may be too high. However,
912 		 * if the server did receive the request then it might
913 		 * accidentally give us a reply with a mismatched operation.
914 		 * We can sort this out by sending a lone sequence operation
915 		 * to the server on the same slot.
916 		 */
917 		if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
918 			slot->seq_nr--;
919 			if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
920 				nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
921 				res->sr_slot = NULL;
922 			}
923 			goto retry_nowait;
924 		}
925 		/*
926 		 * RFC5661:
927 		 * A retry might be sent while the original request is
928 		 * still in progress on the replier. The replier SHOULD
929 		 * deal with the issue by returning NFS4ERR_DELAY as the
930 		 * reply to SEQUENCE or CB_SEQUENCE operation, but
931 		 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
932 		 *
933 		 * Restart the search after a delay.
934 		 */
935 		slot->seq_nr = slot->seq_nr_highest_sent;
936 		goto out_retry;
937 	case -NFS4ERR_BADSESSION:
938 	case -NFS4ERR_DEADSESSION:
939 	case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
940 		goto session_recover;
941 	default:
942 		/* Just update the slot sequence no. */
943 		slot->seq_done = 1;
944 	}
945 out:
946 	/* The session may be reset by one of the error handlers. */
947 	dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
948 out_noaction:
949 	return ret;
950 session_recover:
951 	set_bit(NFS4_SLOT_TBL_DRAINING, &session->fc_slot_table.slot_tbl_state);
952 	nfs4_schedule_session_recovery(session, status);
953 	dprintk("%s ERROR: %d Reset session\n", __func__, status);
954 	nfs41_sequence_free_slot(res);
955 	goto out;
956 retry_new_seq:
957 	++slot->seq_nr;
958 retry_nowait:
959 	if (rpc_restart_call_prepare(task)) {
960 		nfs41_sequence_free_slot(res);
961 		task->tk_status = 0;
962 		ret = 0;
963 	}
964 	goto out;
965 out_retry:
966 	if (!rpc_restart_call(task))
967 		goto out;
968 	rpc_delay(task, NFS4_POLL_RETRY_MAX);
969 	return 0;
970 }
971 
972 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
973 {
974 	if (!nfs41_sequence_process(task, res))
975 		return 0;
976 	if (res->sr_slot != NULL)
977 		nfs41_sequence_free_slot(res);
978 	return 1;
979 
980 }
981 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
982 
983 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
984 {
985 	if (res->sr_slot == NULL)
986 		return 1;
987 	if (res->sr_slot->table->session != NULL)
988 		return nfs41_sequence_process(task, res);
989 	return nfs40_sequence_done(task, res);
990 }
991 
992 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
993 {
994 	if (res->sr_slot != NULL) {
995 		if (res->sr_slot->table->session != NULL)
996 			nfs41_sequence_free_slot(res);
997 		else
998 			nfs40_sequence_free_slot(res);
999 	}
1000 }
1001 
1002 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
1003 {
1004 	if (res->sr_slot == NULL)
1005 		return 1;
1006 	if (!res->sr_slot->table->session)
1007 		return nfs40_sequence_done(task, res);
1008 	return nfs41_sequence_done(task, res);
1009 }
1010 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1011 
1012 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
1013 {
1014 	struct nfs4_call_sync_data *data = calldata;
1015 
1016 	dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
1017 
1018 	nfs4_setup_sequence(data->seq_server->nfs_client,
1019 			    data->seq_args, data->seq_res, task);
1020 }
1021 
1022 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
1023 {
1024 	struct nfs4_call_sync_data *data = calldata;
1025 
1026 	nfs41_sequence_done(task, data->seq_res);
1027 }
1028 
1029 static const struct rpc_call_ops nfs41_call_sync_ops = {
1030 	.rpc_call_prepare = nfs41_call_sync_prepare,
1031 	.rpc_call_done = nfs41_call_sync_done,
1032 };
1033 
1034 #else	/* !CONFIG_NFS_V4_1 */
1035 
1036 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1037 {
1038 	return nfs40_sequence_done(task, res);
1039 }
1040 
1041 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1042 {
1043 	if (res->sr_slot != NULL)
1044 		nfs40_sequence_free_slot(res);
1045 }
1046 
1047 int nfs4_sequence_done(struct rpc_task *task,
1048 		       struct nfs4_sequence_res *res)
1049 {
1050 	return nfs40_sequence_done(task, res);
1051 }
1052 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1053 
1054 #endif	/* !CONFIG_NFS_V4_1 */
1055 
1056 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1057 {
1058 	res->sr_timestamp = jiffies;
1059 	res->sr_status_flags = 0;
1060 	res->sr_status = 1;
1061 }
1062 
1063 static
1064 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1065 		struct nfs4_sequence_res *res,
1066 		struct nfs4_slot *slot)
1067 {
1068 	if (!slot)
1069 		return;
1070 	slot->privileged = args->sa_privileged ? 1 : 0;
1071 	args->sa_slot = slot;
1072 
1073 	res->sr_slot = slot;
1074 }
1075 
1076 int nfs4_setup_sequence(struct nfs_client *client,
1077 			struct nfs4_sequence_args *args,
1078 			struct nfs4_sequence_res *res,
1079 			struct rpc_task *task)
1080 {
1081 	struct nfs4_session *session = nfs4_get_session(client);
1082 	struct nfs4_slot_table *tbl  = client->cl_slot_tbl;
1083 	struct nfs4_slot *slot;
1084 
1085 	/* slot already allocated? */
1086 	if (res->sr_slot != NULL)
1087 		goto out_start;
1088 
1089 	if (session)
1090 		tbl = &session->fc_slot_table;
1091 
1092 	spin_lock(&tbl->slot_tbl_lock);
1093 	/* The state manager will wait until the slot table is empty */
1094 	if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1095 		goto out_sleep;
1096 
1097 	slot = nfs4_alloc_slot(tbl);
1098 	if (IS_ERR(slot)) {
1099 		if (slot == ERR_PTR(-ENOMEM))
1100 			goto out_sleep_timeout;
1101 		goto out_sleep;
1102 	}
1103 	spin_unlock(&tbl->slot_tbl_lock);
1104 
1105 	nfs4_sequence_attach_slot(args, res, slot);
1106 
1107 	trace_nfs4_setup_sequence(session, args);
1108 out_start:
1109 	nfs41_sequence_res_init(res);
1110 	rpc_call_start(task);
1111 	return 0;
1112 out_sleep_timeout:
1113 	/* Try again in 1/4 second */
1114 	if (args->sa_privileged)
1115 		rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1116 				jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1117 	else
1118 		rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1119 				NULL, jiffies + (HZ >> 2));
1120 	spin_unlock(&tbl->slot_tbl_lock);
1121 	return -EAGAIN;
1122 out_sleep:
1123 	if (args->sa_privileged)
1124 		rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1125 				RPC_PRIORITY_PRIVILEGED);
1126 	else
1127 		rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1128 	spin_unlock(&tbl->slot_tbl_lock);
1129 	return -EAGAIN;
1130 }
1131 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1132 
1133 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1134 {
1135 	struct nfs4_call_sync_data *data = calldata;
1136 	nfs4_setup_sequence(data->seq_server->nfs_client,
1137 				data->seq_args, data->seq_res, task);
1138 }
1139 
1140 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1141 {
1142 	struct nfs4_call_sync_data *data = calldata;
1143 	nfs4_sequence_done(task, data->seq_res);
1144 }
1145 
1146 static const struct rpc_call_ops nfs40_call_sync_ops = {
1147 	.rpc_call_prepare = nfs40_call_sync_prepare,
1148 	.rpc_call_done = nfs40_call_sync_done,
1149 };
1150 
1151 static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1152 {
1153 	int ret;
1154 	struct rpc_task *task;
1155 
1156 	task = rpc_run_task(task_setup);
1157 	if (IS_ERR(task))
1158 		return PTR_ERR(task);
1159 
1160 	ret = task->tk_status;
1161 	rpc_put_task(task);
1162 	return ret;
1163 }
1164 
1165 static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1166 			     struct nfs_server *server,
1167 			     struct rpc_message *msg,
1168 			     struct nfs4_sequence_args *args,
1169 			     struct nfs4_sequence_res *res,
1170 			     unsigned short task_flags)
1171 {
1172 	struct nfs_client *clp = server->nfs_client;
1173 	struct nfs4_call_sync_data data = {
1174 		.seq_server = server,
1175 		.seq_args = args,
1176 		.seq_res = res,
1177 	};
1178 	struct rpc_task_setup task_setup = {
1179 		.rpc_client = clnt,
1180 		.rpc_message = msg,
1181 		.callback_ops = clp->cl_mvops->call_sync_ops,
1182 		.callback_data = &data,
1183 		.flags = task_flags,
1184 	};
1185 
1186 	return nfs4_call_sync_custom(&task_setup);
1187 }
1188 
1189 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1190 				   struct nfs_server *server,
1191 				   struct rpc_message *msg,
1192 				   struct nfs4_sequence_args *args,
1193 				   struct nfs4_sequence_res *res)
1194 {
1195 	unsigned short task_flags = 0;
1196 
1197 	if (server->caps & NFS_CAP_MOVEABLE)
1198 		task_flags = RPC_TASK_MOVEABLE;
1199 	return nfs4_do_call_sync(clnt, server, msg, args, res, task_flags);
1200 }
1201 
1202 
1203 int nfs4_call_sync(struct rpc_clnt *clnt,
1204 		   struct nfs_server *server,
1205 		   struct rpc_message *msg,
1206 		   struct nfs4_sequence_args *args,
1207 		   struct nfs4_sequence_res *res,
1208 		   int cache_reply)
1209 {
1210 	nfs4_init_sequence(args, res, cache_reply, 0);
1211 	return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1212 }
1213 
1214 static void
1215 nfs4_inc_nlink_locked(struct inode *inode)
1216 {
1217 	nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1218 					     NFS_INO_INVALID_CTIME |
1219 					     NFS_INO_INVALID_NLINK);
1220 	inc_nlink(inode);
1221 }
1222 
1223 static void
1224 nfs4_inc_nlink(struct inode *inode)
1225 {
1226 	spin_lock(&inode->i_lock);
1227 	nfs4_inc_nlink_locked(inode);
1228 	spin_unlock(&inode->i_lock);
1229 }
1230 
1231 static void
1232 nfs4_dec_nlink_locked(struct inode *inode)
1233 {
1234 	nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1235 					     NFS_INO_INVALID_CTIME |
1236 					     NFS_INO_INVALID_NLINK);
1237 	drop_nlink(inode);
1238 }
1239 
1240 static void
1241 nfs4_update_changeattr_locked(struct inode *inode,
1242 		struct nfs4_change_info *cinfo,
1243 		unsigned long timestamp, unsigned long cache_validity)
1244 {
1245 	struct nfs_inode *nfsi = NFS_I(inode);
1246 	u64 change_attr = inode_peek_iversion_raw(inode);
1247 
1248 	cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
1249 	if (S_ISDIR(inode->i_mode))
1250 		cache_validity |= NFS_INO_INVALID_DATA;
1251 
1252 	switch (NFS_SERVER(inode)->change_attr_type) {
1253 	case NFS4_CHANGE_TYPE_IS_UNDEFINED:
1254 		if (cinfo->after == change_attr)
1255 			goto out;
1256 		break;
1257 	default:
1258 		if ((s64)(change_attr - cinfo->after) >= 0)
1259 			goto out;
1260 	}
1261 
1262 	inode_set_iversion_raw(inode, cinfo->after);
1263 	if (!cinfo->atomic || cinfo->before != change_attr) {
1264 		if (S_ISDIR(inode->i_mode))
1265 			nfs_force_lookup_revalidate(inode);
1266 
1267 		if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1268 			cache_validity |=
1269 				NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL |
1270 				NFS_INO_INVALID_SIZE | NFS_INO_INVALID_OTHER |
1271 				NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_NLINK |
1272 				NFS_INO_INVALID_MODE | NFS_INO_INVALID_XATTR;
1273 		nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1274 	}
1275 	nfsi->attrtimeo_timestamp = jiffies;
1276 	nfsi->read_cache_jiffies = timestamp;
1277 	nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1278 	nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1279 out:
1280 	nfs_set_cache_invalid(inode, cache_validity);
1281 }
1282 
1283 void
1284 nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1285 		unsigned long timestamp, unsigned long cache_validity)
1286 {
1287 	spin_lock(&dir->i_lock);
1288 	nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1289 	spin_unlock(&dir->i_lock);
1290 }
1291 
1292 struct nfs4_open_createattrs {
1293 	struct nfs4_label *label;
1294 	struct iattr *sattr;
1295 	const __u32 verf[2];
1296 };
1297 
1298 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1299 		int err, struct nfs4_exception *exception)
1300 {
1301 	if (err != -EINVAL)
1302 		return false;
1303 	if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1304 		return false;
1305 	server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1306 	exception->retry = 1;
1307 	return true;
1308 }
1309 
1310 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1311 {
1312 	 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1313 }
1314 
1315 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1316 {
1317 	fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1318 
1319 	return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1320 }
1321 
1322 static u32
1323 nfs4_map_atomic_open_share(struct nfs_server *server,
1324 		fmode_t fmode, int openflags)
1325 {
1326 	u32 res = 0;
1327 
1328 	switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1329 	case FMODE_READ:
1330 		res = NFS4_SHARE_ACCESS_READ;
1331 		break;
1332 	case FMODE_WRITE:
1333 		res = NFS4_SHARE_ACCESS_WRITE;
1334 		break;
1335 	case FMODE_READ|FMODE_WRITE:
1336 		res = NFS4_SHARE_ACCESS_BOTH;
1337 	}
1338 	if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1339 		goto out;
1340 	/* Want no delegation if we're using O_DIRECT */
1341 	if (openflags & O_DIRECT)
1342 		res |= NFS4_SHARE_WANT_NO_DELEG;
1343 out:
1344 	return res;
1345 }
1346 
1347 static enum open_claim_type4
1348 nfs4_map_atomic_open_claim(struct nfs_server *server,
1349 		enum open_claim_type4 claim)
1350 {
1351 	if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1352 		return claim;
1353 	switch (claim) {
1354 	default:
1355 		return claim;
1356 	case NFS4_OPEN_CLAIM_FH:
1357 		return NFS4_OPEN_CLAIM_NULL;
1358 	case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1359 		return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1360 	case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1361 		return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1362 	}
1363 }
1364 
1365 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1366 {
1367 	p->o_res.f_attr = &p->f_attr;
1368 	p->o_res.seqid = p->o_arg.seqid;
1369 	p->c_res.seqid = p->c_arg.seqid;
1370 	p->o_res.server = p->o_arg.server;
1371 	p->o_res.access_request = p->o_arg.access;
1372 	nfs_fattr_init(&p->f_attr);
1373 	nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1374 }
1375 
1376 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1377 		struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1378 		const struct nfs4_open_createattrs *c,
1379 		enum open_claim_type4 claim,
1380 		gfp_t gfp_mask)
1381 {
1382 	struct dentry *parent = dget_parent(dentry);
1383 	struct inode *dir = d_inode(parent);
1384 	struct nfs_server *server = NFS_SERVER(dir);
1385 	struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1386 	struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1387 	struct nfs4_opendata *p;
1388 
1389 	p = kzalloc(sizeof(*p), gfp_mask);
1390 	if (p == NULL)
1391 		goto err;
1392 
1393 	p->f_attr.label = nfs4_label_alloc(server, gfp_mask);
1394 	if (IS_ERR(p->f_attr.label))
1395 		goto err_free_p;
1396 
1397 	p->a_label = nfs4_label_alloc(server, gfp_mask);
1398 	if (IS_ERR(p->a_label))
1399 		goto err_free_f;
1400 
1401 	alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1402 	p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1403 	if (IS_ERR(p->o_arg.seqid))
1404 		goto err_free_label;
1405 	nfs_sb_active(dentry->d_sb);
1406 	p->dentry = dget(dentry);
1407 	p->dir = parent;
1408 	p->owner = sp;
1409 	atomic_inc(&sp->so_count);
1410 	p->o_arg.open_flags = flags;
1411 	p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1412 	p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1413 	p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1414 			fmode, flags);
1415 	if (flags & O_CREAT) {
1416 		p->o_arg.umask = current_umask();
1417 		p->o_arg.label = nfs4_label_copy(p->a_label, label);
1418 		if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1419 			p->o_arg.u.attrs = &p->attrs;
1420 			memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1421 
1422 			memcpy(p->o_arg.u.verifier.data, c->verf,
1423 					sizeof(p->o_arg.u.verifier.data));
1424 		}
1425 	}
1426 	/* ask server to check for all possible rights as results
1427 	 * are cached */
1428 	switch (p->o_arg.claim) {
1429 	default:
1430 		break;
1431 	case NFS4_OPEN_CLAIM_NULL:
1432 	case NFS4_OPEN_CLAIM_FH:
1433 		p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1434 				  NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE |
1435 				  NFS4_ACCESS_EXECUTE |
1436 				  nfs_access_xattr_mask(server);
1437 	}
1438 	p->o_arg.clientid = server->nfs_client->cl_clientid;
1439 	p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1440 	p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1441 	p->o_arg.name = &dentry->d_name;
1442 	p->o_arg.server = server;
1443 	p->o_arg.bitmask = nfs4_bitmask(server, label);
1444 	p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1445 	switch (p->o_arg.claim) {
1446 	case NFS4_OPEN_CLAIM_NULL:
1447 	case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1448 	case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1449 		p->o_arg.fh = NFS_FH(dir);
1450 		break;
1451 	case NFS4_OPEN_CLAIM_PREVIOUS:
1452 	case NFS4_OPEN_CLAIM_FH:
1453 	case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1454 	case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1455 		p->o_arg.fh = NFS_FH(d_inode(dentry));
1456 	}
1457 	p->c_arg.fh = &p->o_res.fh;
1458 	p->c_arg.stateid = &p->o_res.stateid;
1459 	p->c_arg.seqid = p->o_arg.seqid;
1460 	nfs4_init_opendata_res(p);
1461 	kref_init(&p->kref);
1462 	return p;
1463 
1464 err_free_label:
1465 	nfs4_label_free(p->a_label);
1466 err_free_f:
1467 	nfs4_label_free(p->f_attr.label);
1468 err_free_p:
1469 	kfree(p);
1470 err:
1471 	dput(parent);
1472 	return NULL;
1473 }
1474 
1475 static void nfs4_opendata_free(struct kref *kref)
1476 {
1477 	struct nfs4_opendata *p = container_of(kref,
1478 			struct nfs4_opendata, kref);
1479 	struct super_block *sb = p->dentry->d_sb;
1480 
1481 	nfs4_lgopen_release(p->lgp);
1482 	nfs_free_seqid(p->o_arg.seqid);
1483 	nfs4_sequence_free_slot(&p->o_res.seq_res);
1484 	if (p->state != NULL)
1485 		nfs4_put_open_state(p->state);
1486 	nfs4_put_state_owner(p->owner);
1487 
1488 	nfs4_label_free(p->a_label);
1489 	nfs4_label_free(p->f_attr.label);
1490 
1491 	dput(p->dir);
1492 	dput(p->dentry);
1493 	nfs_sb_deactive(sb);
1494 	nfs_fattr_free_names(&p->f_attr);
1495 	kfree(p->f_attr.mdsthreshold);
1496 	kfree(p);
1497 }
1498 
1499 static void nfs4_opendata_put(struct nfs4_opendata *p)
1500 {
1501 	if (p != NULL)
1502 		kref_put(&p->kref, nfs4_opendata_free);
1503 }
1504 
1505 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1506 		fmode_t fmode)
1507 {
1508 	switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1509 	case FMODE_READ|FMODE_WRITE:
1510 		return state->n_rdwr != 0;
1511 	case FMODE_WRITE:
1512 		return state->n_wronly != 0;
1513 	case FMODE_READ:
1514 		return state->n_rdonly != 0;
1515 	}
1516 	WARN_ON_ONCE(1);
1517 	return false;
1518 }
1519 
1520 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1521 		int open_mode, enum open_claim_type4 claim)
1522 {
1523 	int ret = 0;
1524 
1525 	if (open_mode & (O_EXCL|O_TRUNC))
1526 		goto out;
1527 	switch (claim) {
1528 	case NFS4_OPEN_CLAIM_NULL:
1529 	case NFS4_OPEN_CLAIM_FH:
1530 		goto out;
1531 	default:
1532 		break;
1533 	}
1534 	switch (mode & (FMODE_READ|FMODE_WRITE)) {
1535 		case FMODE_READ:
1536 			ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1537 				&& state->n_rdonly != 0;
1538 			break;
1539 		case FMODE_WRITE:
1540 			ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1541 				&& state->n_wronly != 0;
1542 			break;
1543 		case FMODE_READ|FMODE_WRITE:
1544 			ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1545 				&& state->n_rdwr != 0;
1546 	}
1547 out:
1548 	return ret;
1549 }
1550 
1551 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1552 		enum open_claim_type4 claim)
1553 {
1554 	if (delegation == NULL)
1555 		return 0;
1556 	if ((delegation->type & fmode) != fmode)
1557 		return 0;
1558 	switch (claim) {
1559 	case NFS4_OPEN_CLAIM_NULL:
1560 	case NFS4_OPEN_CLAIM_FH:
1561 		break;
1562 	case NFS4_OPEN_CLAIM_PREVIOUS:
1563 		if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1564 			break;
1565 		fallthrough;
1566 	default:
1567 		return 0;
1568 	}
1569 	nfs_mark_delegation_referenced(delegation);
1570 	return 1;
1571 }
1572 
1573 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1574 {
1575 	switch (fmode) {
1576 		case FMODE_WRITE:
1577 			state->n_wronly++;
1578 			break;
1579 		case FMODE_READ:
1580 			state->n_rdonly++;
1581 			break;
1582 		case FMODE_READ|FMODE_WRITE:
1583 			state->n_rdwr++;
1584 	}
1585 	nfs4_state_set_mode_locked(state, state->state | fmode);
1586 }
1587 
1588 #ifdef CONFIG_NFS_V4_1
1589 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1590 {
1591 	if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1592 		return true;
1593 	if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1594 		return true;
1595 	if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1596 		return true;
1597 	return false;
1598 }
1599 #endif /* CONFIG_NFS_V4_1 */
1600 
1601 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1602 {
1603 	if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1604 		wake_up_all(&state->waitq);
1605 }
1606 
1607 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1608 {
1609 	struct nfs_client *clp = state->owner->so_server->nfs_client;
1610 	bool need_recover = false;
1611 
1612 	if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1613 		need_recover = true;
1614 	if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1615 		need_recover = true;
1616 	if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1617 		need_recover = true;
1618 	if (need_recover)
1619 		nfs4_state_mark_reclaim_nograce(clp, state);
1620 }
1621 
1622 /*
1623  * Check for whether or not the caller may update the open stateid
1624  * to the value passed in by stateid.
1625  *
1626  * Note: This function relies heavily on the server implementing
1627  * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1628  * correctly.
1629  * i.e. The stateid seqids have to be initialised to 1, and
1630  * are then incremented on every state transition.
1631  */
1632 static bool nfs_stateid_is_sequential(struct nfs4_state *state,
1633 		const nfs4_stateid *stateid)
1634 {
1635 	if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1636 		/* The common case - we're updating to a new sequence number */
1637 		if (nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1638 			if (nfs4_stateid_is_next(&state->open_stateid, stateid))
1639 				return true;
1640 			return false;
1641 		}
1642 		/* The server returned a new stateid */
1643 	}
1644 	/* This is the first OPEN in this generation */
1645 	if (stateid->seqid == cpu_to_be32(1))
1646 		return true;
1647 	return false;
1648 }
1649 
1650 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1651 {
1652 	if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1653 		return;
1654 	if (state->n_wronly)
1655 		set_bit(NFS_O_WRONLY_STATE, &state->flags);
1656 	if (state->n_rdonly)
1657 		set_bit(NFS_O_RDONLY_STATE, &state->flags);
1658 	if (state->n_rdwr)
1659 		set_bit(NFS_O_RDWR_STATE, &state->flags);
1660 	set_bit(NFS_OPEN_STATE, &state->flags);
1661 }
1662 
1663 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1664 		nfs4_stateid *stateid, fmode_t fmode)
1665 {
1666 	clear_bit(NFS_O_RDWR_STATE, &state->flags);
1667 	switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1668 	case FMODE_WRITE:
1669 		clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1670 		break;
1671 	case FMODE_READ:
1672 		clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1673 		break;
1674 	case 0:
1675 		clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1676 		clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1677 		clear_bit(NFS_OPEN_STATE, &state->flags);
1678 	}
1679 	if (stateid == NULL)
1680 		return;
1681 	/* Handle OPEN+OPEN_DOWNGRADE races */
1682 	if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1683 	    !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1684 		nfs_resync_open_stateid_locked(state);
1685 		goto out;
1686 	}
1687 	if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1688 		nfs4_stateid_copy(&state->stateid, stateid);
1689 	nfs4_stateid_copy(&state->open_stateid, stateid);
1690 	trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1691 out:
1692 	nfs_state_log_update_open_stateid(state);
1693 }
1694 
1695 static void nfs_clear_open_stateid(struct nfs4_state *state,
1696 	nfs4_stateid *arg_stateid,
1697 	nfs4_stateid *stateid, fmode_t fmode)
1698 {
1699 	write_seqlock(&state->seqlock);
1700 	/* Ignore, if the CLOSE argment doesn't match the current stateid */
1701 	if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1702 		nfs_clear_open_stateid_locked(state, stateid, fmode);
1703 	write_sequnlock(&state->seqlock);
1704 	if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1705 		nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1706 }
1707 
1708 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1709 		const nfs4_stateid *stateid, nfs4_stateid *freeme)
1710 	__must_hold(&state->owner->so_lock)
1711 	__must_hold(&state->seqlock)
1712 	__must_hold(RCU)
1713 
1714 {
1715 	DEFINE_WAIT(wait);
1716 	int status = 0;
1717 	for (;;) {
1718 
1719 		if (nfs_stateid_is_sequential(state, stateid))
1720 			break;
1721 
1722 		if (status)
1723 			break;
1724 		/* Rely on seqids for serialisation with NFSv4.0 */
1725 		if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1726 			break;
1727 
1728 		set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1729 		prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1730 		/*
1731 		 * Ensure we process the state changes in the same order
1732 		 * in which the server processed them by delaying the
1733 		 * update of the stateid until we are in sequence.
1734 		 */
1735 		write_sequnlock(&state->seqlock);
1736 		spin_unlock(&state->owner->so_lock);
1737 		rcu_read_unlock();
1738 		trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1739 
1740 		if (!fatal_signal_pending(current)) {
1741 			if (schedule_timeout(5*HZ) == 0)
1742 				status = -EAGAIN;
1743 			else
1744 				status = 0;
1745 		} else
1746 			status = -EINTR;
1747 		finish_wait(&state->waitq, &wait);
1748 		rcu_read_lock();
1749 		spin_lock(&state->owner->so_lock);
1750 		write_seqlock(&state->seqlock);
1751 	}
1752 
1753 	if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1754 	    !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1755 		nfs4_stateid_copy(freeme, &state->open_stateid);
1756 		nfs_test_and_clear_all_open_stateid(state);
1757 	}
1758 
1759 	if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1760 		nfs4_stateid_copy(&state->stateid, stateid);
1761 	nfs4_stateid_copy(&state->open_stateid, stateid);
1762 	trace_nfs4_open_stateid_update(state->inode, stateid, status);
1763 	nfs_state_log_update_open_stateid(state);
1764 }
1765 
1766 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1767 		const nfs4_stateid *open_stateid,
1768 		fmode_t fmode,
1769 		nfs4_stateid *freeme)
1770 {
1771 	/*
1772 	 * Protect the call to nfs4_state_set_mode_locked and
1773 	 * serialise the stateid update
1774 	 */
1775 	write_seqlock(&state->seqlock);
1776 	nfs_set_open_stateid_locked(state, open_stateid, freeme);
1777 	switch (fmode) {
1778 	case FMODE_READ:
1779 		set_bit(NFS_O_RDONLY_STATE, &state->flags);
1780 		break;
1781 	case FMODE_WRITE:
1782 		set_bit(NFS_O_WRONLY_STATE, &state->flags);
1783 		break;
1784 	case FMODE_READ|FMODE_WRITE:
1785 		set_bit(NFS_O_RDWR_STATE, &state->flags);
1786 	}
1787 	set_bit(NFS_OPEN_STATE, &state->flags);
1788 	write_sequnlock(&state->seqlock);
1789 }
1790 
1791 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1792 {
1793 	clear_bit(NFS_O_RDWR_STATE, &state->flags);
1794 	clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1795 	clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1796 	clear_bit(NFS_OPEN_STATE, &state->flags);
1797 }
1798 
1799 static void nfs_state_set_delegation(struct nfs4_state *state,
1800 		const nfs4_stateid *deleg_stateid,
1801 		fmode_t fmode)
1802 {
1803 	/*
1804 	 * Protect the call to nfs4_state_set_mode_locked and
1805 	 * serialise the stateid update
1806 	 */
1807 	write_seqlock(&state->seqlock);
1808 	nfs4_stateid_copy(&state->stateid, deleg_stateid);
1809 	set_bit(NFS_DELEGATED_STATE, &state->flags);
1810 	write_sequnlock(&state->seqlock);
1811 }
1812 
1813 static void nfs_state_clear_delegation(struct nfs4_state *state)
1814 {
1815 	write_seqlock(&state->seqlock);
1816 	nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1817 	clear_bit(NFS_DELEGATED_STATE, &state->flags);
1818 	write_sequnlock(&state->seqlock);
1819 }
1820 
1821 int update_open_stateid(struct nfs4_state *state,
1822 		const nfs4_stateid *open_stateid,
1823 		const nfs4_stateid *delegation,
1824 		fmode_t fmode)
1825 {
1826 	struct nfs_server *server = NFS_SERVER(state->inode);
1827 	struct nfs_client *clp = server->nfs_client;
1828 	struct nfs_inode *nfsi = NFS_I(state->inode);
1829 	struct nfs_delegation *deleg_cur;
1830 	nfs4_stateid freeme = { };
1831 	int ret = 0;
1832 
1833 	fmode &= (FMODE_READ|FMODE_WRITE);
1834 
1835 	rcu_read_lock();
1836 	spin_lock(&state->owner->so_lock);
1837 	if (open_stateid != NULL) {
1838 		nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1839 		ret = 1;
1840 	}
1841 
1842 	deleg_cur = nfs4_get_valid_delegation(state->inode);
1843 	if (deleg_cur == NULL)
1844 		goto no_delegation;
1845 
1846 	spin_lock(&deleg_cur->lock);
1847 	if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1848 	   test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1849 	    (deleg_cur->type & fmode) != fmode)
1850 		goto no_delegation_unlock;
1851 
1852 	if (delegation == NULL)
1853 		delegation = &deleg_cur->stateid;
1854 	else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1855 		goto no_delegation_unlock;
1856 
1857 	nfs_mark_delegation_referenced(deleg_cur);
1858 	nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1859 	ret = 1;
1860 no_delegation_unlock:
1861 	spin_unlock(&deleg_cur->lock);
1862 no_delegation:
1863 	if (ret)
1864 		update_open_stateflags(state, fmode);
1865 	spin_unlock(&state->owner->so_lock);
1866 	rcu_read_unlock();
1867 
1868 	if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1869 		nfs4_schedule_state_manager(clp);
1870 	if (freeme.type != 0)
1871 		nfs4_test_and_free_stateid(server, &freeme,
1872 				state->owner->so_cred);
1873 
1874 	return ret;
1875 }
1876 
1877 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1878 		const nfs4_stateid *stateid)
1879 {
1880 	struct nfs4_state *state = lsp->ls_state;
1881 	bool ret = false;
1882 
1883 	spin_lock(&state->state_lock);
1884 	if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1885 		goto out_noupdate;
1886 	if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1887 		goto out_noupdate;
1888 	nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1889 	ret = true;
1890 out_noupdate:
1891 	spin_unlock(&state->state_lock);
1892 	return ret;
1893 }
1894 
1895 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1896 {
1897 	struct nfs_delegation *delegation;
1898 
1899 	fmode &= FMODE_READ|FMODE_WRITE;
1900 	rcu_read_lock();
1901 	delegation = nfs4_get_valid_delegation(inode);
1902 	if (delegation == NULL || (delegation->type & fmode) == fmode) {
1903 		rcu_read_unlock();
1904 		return;
1905 	}
1906 	rcu_read_unlock();
1907 	nfs4_inode_return_delegation(inode);
1908 }
1909 
1910 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1911 {
1912 	struct nfs4_state *state = opendata->state;
1913 	struct nfs_delegation *delegation;
1914 	int open_mode = opendata->o_arg.open_flags;
1915 	fmode_t fmode = opendata->o_arg.fmode;
1916 	enum open_claim_type4 claim = opendata->o_arg.claim;
1917 	nfs4_stateid stateid;
1918 	int ret = -EAGAIN;
1919 
1920 	for (;;) {
1921 		spin_lock(&state->owner->so_lock);
1922 		if (can_open_cached(state, fmode, open_mode, claim)) {
1923 			update_open_stateflags(state, fmode);
1924 			spin_unlock(&state->owner->so_lock);
1925 			goto out_return_state;
1926 		}
1927 		spin_unlock(&state->owner->so_lock);
1928 		rcu_read_lock();
1929 		delegation = nfs4_get_valid_delegation(state->inode);
1930 		if (!can_open_delegated(delegation, fmode, claim)) {
1931 			rcu_read_unlock();
1932 			break;
1933 		}
1934 		/* Save the delegation */
1935 		nfs4_stateid_copy(&stateid, &delegation->stateid);
1936 		rcu_read_unlock();
1937 		nfs_release_seqid(opendata->o_arg.seqid);
1938 		if (!opendata->is_recover) {
1939 			ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1940 			if (ret != 0)
1941 				goto out;
1942 		}
1943 		ret = -EAGAIN;
1944 
1945 		/* Try to update the stateid using the delegation */
1946 		if (update_open_stateid(state, NULL, &stateid, fmode))
1947 			goto out_return_state;
1948 	}
1949 out:
1950 	return ERR_PTR(ret);
1951 out_return_state:
1952 	refcount_inc(&state->count);
1953 	return state;
1954 }
1955 
1956 static void
1957 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1958 {
1959 	struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1960 	struct nfs_delegation *delegation;
1961 	int delegation_flags = 0;
1962 
1963 	rcu_read_lock();
1964 	delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1965 	if (delegation)
1966 		delegation_flags = delegation->flags;
1967 	rcu_read_unlock();
1968 	switch (data->o_arg.claim) {
1969 	default:
1970 		break;
1971 	case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1972 	case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1973 		pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1974 				   "returning a delegation for "
1975 				   "OPEN(CLAIM_DELEGATE_CUR)\n",
1976 				   clp->cl_hostname);
1977 		return;
1978 	}
1979 	if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1980 		nfs_inode_set_delegation(state->inode,
1981 				data->owner->so_cred,
1982 				data->o_res.delegation_type,
1983 				&data->o_res.delegation,
1984 				data->o_res.pagemod_limit);
1985 	else
1986 		nfs_inode_reclaim_delegation(state->inode,
1987 				data->owner->so_cred,
1988 				data->o_res.delegation_type,
1989 				&data->o_res.delegation,
1990 				data->o_res.pagemod_limit);
1991 
1992 	if (data->o_res.do_recall)
1993 		nfs_async_inode_return_delegation(state->inode,
1994 						  &data->o_res.delegation);
1995 }
1996 
1997 /*
1998  * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1999  * and update the nfs4_state.
2000  */
2001 static struct nfs4_state *
2002 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
2003 {
2004 	struct inode *inode = data->state->inode;
2005 	struct nfs4_state *state = data->state;
2006 	int ret;
2007 
2008 	if (!data->rpc_done) {
2009 		if (data->rpc_status)
2010 			return ERR_PTR(data->rpc_status);
2011 		return nfs4_try_open_cached(data);
2012 	}
2013 
2014 	ret = nfs_refresh_inode(inode, &data->f_attr);
2015 	if (ret)
2016 		return ERR_PTR(ret);
2017 
2018 	if (data->o_res.delegation_type != 0)
2019 		nfs4_opendata_check_deleg(data, state);
2020 
2021 	if (!update_open_stateid(state, &data->o_res.stateid,
2022 				NULL, data->o_arg.fmode))
2023 		return ERR_PTR(-EAGAIN);
2024 	refcount_inc(&state->count);
2025 
2026 	return state;
2027 }
2028 
2029 static struct inode *
2030 nfs4_opendata_get_inode(struct nfs4_opendata *data)
2031 {
2032 	struct inode *inode;
2033 
2034 	switch (data->o_arg.claim) {
2035 	case NFS4_OPEN_CLAIM_NULL:
2036 	case NFS4_OPEN_CLAIM_DELEGATE_CUR:
2037 	case NFS4_OPEN_CLAIM_DELEGATE_PREV:
2038 		if (!(data->f_attr.valid & NFS_ATTR_FATTR))
2039 			return ERR_PTR(-EAGAIN);
2040 		inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
2041 				&data->f_attr);
2042 		break;
2043 	default:
2044 		inode = d_inode(data->dentry);
2045 		ihold(inode);
2046 		nfs_refresh_inode(inode, &data->f_attr);
2047 	}
2048 	return inode;
2049 }
2050 
2051 static struct nfs4_state *
2052 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2053 {
2054 	struct nfs4_state *state;
2055 	struct inode *inode;
2056 
2057 	inode = nfs4_opendata_get_inode(data);
2058 	if (IS_ERR(inode))
2059 		return ERR_CAST(inode);
2060 	if (data->state != NULL && data->state->inode == inode) {
2061 		state = data->state;
2062 		refcount_inc(&state->count);
2063 	} else
2064 		state = nfs4_get_open_state(inode, data->owner);
2065 	iput(inode);
2066 	if (state == NULL)
2067 		state = ERR_PTR(-ENOMEM);
2068 	return state;
2069 }
2070 
2071 static struct nfs4_state *
2072 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2073 {
2074 	struct nfs4_state *state;
2075 
2076 	if (!data->rpc_done) {
2077 		state = nfs4_try_open_cached(data);
2078 		trace_nfs4_cached_open(data->state);
2079 		goto out;
2080 	}
2081 
2082 	state = nfs4_opendata_find_nfs4_state(data);
2083 	if (IS_ERR(state))
2084 		goto out;
2085 
2086 	if (data->o_res.delegation_type != 0)
2087 		nfs4_opendata_check_deleg(data, state);
2088 	if (!update_open_stateid(state, &data->o_res.stateid,
2089 				NULL, data->o_arg.fmode)) {
2090 		nfs4_put_open_state(state);
2091 		state = ERR_PTR(-EAGAIN);
2092 	}
2093 out:
2094 	nfs_release_seqid(data->o_arg.seqid);
2095 	return state;
2096 }
2097 
2098 static struct nfs4_state *
2099 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2100 {
2101 	struct nfs4_state *ret;
2102 
2103 	if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2104 		ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2105 	else
2106 		ret = _nfs4_opendata_to_nfs4_state(data);
2107 	nfs4_sequence_free_slot(&data->o_res.seq_res);
2108 	return ret;
2109 }
2110 
2111 static struct nfs_open_context *
2112 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2113 {
2114 	struct nfs_inode *nfsi = NFS_I(state->inode);
2115 	struct nfs_open_context *ctx;
2116 
2117 	rcu_read_lock();
2118 	list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2119 		if (ctx->state != state)
2120 			continue;
2121 		if ((ctx->mode & mode) != mode)
2122 			continue;
2123 		if (!get_nfs_open_context(ctx))
2124 			continue;
2125 		rcu_read_unlock();
2126 		return ctx;
2127 	}
2128 	rcu_read_unlock();
2129 	return ERR_PTR(-ENOENT);
2130 }
2131 
2132 static struct nfs_open_context *
2133 nfs4_state_find_open_context(struct nfs4_state *state)
2134 {
2135 	struct nfs_open_context *ctx;
2136 
2137 	ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2138 	if (!IS_ERR(ctx))
2139 		return ctx;
2140 	ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2141 	if (!IS_ERR(ctx))
2142 		return ctx;
2143 	return nfs4_state_find_open_context_mode(state, FMODE_READ);
2144 }
2145 
2146 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2147 		struct nfs4_state *state, enum open_claim_type4 claim)
2148 {
2149 	struct nfs4_opendata *opendata;
2150 
2151 	opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2152 			NULL, claim, GFP_NOFS);
2153 	if (opendata == NULL)
2154 		return ERR_PTR(-ENOMEM);
2155 	opendata->state = state;
2156 	refcount_inc(&state->count);
2157 	return opendata;
2158 }
2159 
2160 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2161 				    fmode_t fmode)
2162 {
2163 	struct nfs4_state *newstate;
2164 	struct nfs_server *server = NFS_SB(opendata->dentry->d_sb);
2165 	int openflags = opendata->o_arg.open_flags;
2166 	int ret;
2167 
2168 	if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2169 		return 0;
2170 	opendata->o_arg.fmode = fmode;
2171 	opendata->o_arg.share_access =
2172 		nfs4_map_atomic_open_share(server, fmode, openflags);
2173 	memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2174 	memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2175 	nfs4_init_opendata_res(opendata);
2176 	ret = _nfs4_recover_proc_open(opendata);
2177 	if (ret != 0)
2178 		return ret;
2179 	newstate = nfs4_opendata_to_nfs4_state(opendata);
2180 	if (IS_ERR(newstate))
2181 		return PTR_ERR(newstate);
2182 	if (newstate != opendata->state)
2183 		ret = -ESTALE;
2184 	nfs4_close_state(newstate, fmode);
2185 	return ret;
2186 }
2187 
2188 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2189 {
2190 	int ret;
2191 
2192 	/* memory barrier prior to reading state->n_* */
2193 	smp_rmb();
2194 	ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2195 	if (ret != 0)
2196 		return ret;
2197 	ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2198 	if (ret != 0)
2199 		return ret;
2200 	ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2201 	if (ret != 0)
2202 		return ret;
2203 	/*
2204 	 * We may have performed cached opens for all three recoveries.
2205 	 * Check if we need to update the current stateid.
2206 	 */
2207 	if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2208 	    !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2209 		write_seqlock(&state->seqlock);
2210 		if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2211 			nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2212 		write_sequnlock(&state->seqlock);
2213 	}
2214 	return 0;
2215 }
2216 
2217 /*
2218  * OPEN_RECLAIM:
2219  * 	reclaim state on the server after a reboot.
2220  */
2221 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2222 {
2223 	struct nfs_delegation *delegation;
2224 	struct nfs4_opendata *opendata;
2225 	fmode_t delegation_type = 0;
2226 	int status;
2227 
2228 	opendata = nfs4_open_recoverdata_alloc(ctx, state,
2229 			NFS4_OPEN_CLAIM_PREVIOUS);
2230 	if (IS_ERR(opendata))
2231 		return PTR_ERR(opendata);
2232 	rcu_read_lock();
2233 	delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2234 	if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2235 		delegation_type = delegation->type;
2236 	rcu_read_unlock();
2237 	opendata->o_arg.u.delegation_type = delegation_type;
2238 	status = nfs4_open_recover(opendata, state);
2239 	nfs4_opendata_put(opendata);
2240 	return status;
2241 }
2242 
2243 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2244 {
2245 	struct nfs_server *server = NFS_SERVER(state->inode);
2246 	struct nfs4_exception exception = { };
2247 	int err;
2248 	do {
2249 		err = _nfs4_do_open_reclaim(ctx, state);
2250 		trace_nfs4_open_reclaim(ctx, 0, err);
2251 		if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2252 			continue;
2253 		if (err != -NFS4ERR_DELAY)
2254 			break;
2255 		nfs4_handle_exception(server, err, &exception);
2256 	} while (exception.retry);
2257 	return err;
2258 }
2259 
2260 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2261 {
2262 	struct nfs_open_context *ctx;
2263 	int ret;
2264 
2265 	ctx = nfs4_state_find_open_context(state);
2266 	if (IS_ERR(ctx))
2267 		return -EAGAIN;
2268 	clear_bit(NFS_DELEGATED_STATE, &state->flags);
2269 	nfs_state_clear_open_state_flags(state);
2270 	ret = nfs4_do_open_reclaim(ctx, state);
2271 	put_nfs_open_context(ctx);
2272 	return ret;
2273 }
2274 
2275 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
2276 {
2277 	switch (err) {
2278 		default:
2279 			printk(KERN_ERR "NFS: %s: unhandled error "
2280 					"%d.\n", __func__, err);
2281 			fallthrough;
2282 		case 0:
2283 		case -ENOENT:
2284 		case -EAGAIN:
2285 		case -ESTALE:
2286 		case -ETIMEDOUT:
2287 			break;
2288 		case -NFS4ERR_BADSESSION:
2289 		case -NFS4ERR_BADSLOT:
2290 		case -NFS4ERR_BAD_HIGH_SLOT:
2291 		case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2292 		case -NFS4ERR_DEADSESSION:
2293 			return -EAGAIN;
2294 		case -NFS4ERR_STALE_CLIENTID:
2295 		case -NFS4ERR_STALE_STATEID:
2296 			/* Don't recall a delegation if it was lost */
2297 			nfs4_schedule_lease_recovery(server->nfs_client);
2298 			return -EAGAIN;
2299 		case -NFS4ERR_MOVED:
2300 			nfs4_schedule_migration_recovery(server);
2301 			return -EAGAIN;
2302 		case -NFS4ERR_LEASE_MOVED:
2303 			nfs4_schedule_lease_moved_recovery(server->nfs_client);
2304 			return -EAGAIN;
2305 		case -NFS4ERR_DELEG_REVOKED:
2306 		case -NFS4ERR_ADMIN_REVOKED:
2307 		case -NFS4ERR_EXPIRED:
2308 		case -NFS4ERR_BAD_STATEID:
2309 		case -NFS4ERR_OPENMODE:
2310 			nfs_inode_find_state_and_recover(state->inode,
2311 					stateid);
2312 			nfs4_schedule_stateid_recovery(server, state);
2313 			return -EAGAIN;
2314 		case -NFS4ERR_DELAY:
2315 		case -NFS4ERR_GRACE:
2316 			ssleep(1);
2317 			return -EAGAIN;
2318 		case -ENOMEM:
2319 		case -NFS4ERR_DENIED:
2320 			if (fl) {
2321 				struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2322 				if (lsp)
2323 					set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2324 			}
2325 			return 0;
2326 	}
2327 	return err;
2328 }
2329 
2330 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2331 		struct nfs4_state *state, const nfs4_stateid *stateid)
2332 {
2333 	struct nfs_server *server = NFS_SERVER(state->inode);
2334 	struct nfs4_opendata *opendata;
2335 	int err = 0;
2336 
2337 	opendata = nfs4_open_recoverdata_alloc(ctx, state,
2338 			NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2339 	if (IS_ERR(opendata))
2340 		return PTR_ERR(opendata);
2341 	nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2342 	if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2343 		err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2344 		if (err)
2345 			goto out;
2346 	}
2347 	if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2348 		err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2349 		if (err)
2350 			goto out;
2351 	}
2352 	if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2353 		err = nfs4_open_recover_helper(opendata, FMODE_READ);
2354 		if (err)
2355 			goto out;
2356 	}
2357 	nfs_state_clear_delegation(state);
2358 out:
2359 	nfs4_opendata_put(opendata);
2360 	return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2361 }
2362 
2363 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2364 {
2365 	struct nfs4_opendata *data = calldata;
2366 
2367 	nfs4_setup_sequence(data->o_arg.server->nfs_client,
2368 			   &data->c_arg.seq_args, &data->c_res.seq_res, task);
2369 }
2370 
2371 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2372 {
2373 	struct nfs4_opendata *data = calldata;
2374 
2375 	nfs40_sequence_done(task, &data->c_res.seq_res);
2376 
2377 	data->rpc_status = task->tk_status;
2378 	if (data->rpc_status == 0) {
2379 		nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2380 		nfs_confirm_seqid(&data->owner->so_seqid, 0);
2381 		renew_lease(data->o_res.server, data->timestamp);
2382 		data->rpc_done = true;
2383 	}
2384 }
2385 
2386 static void nfs4_open_confirm_release(void *calldata)
2387 {
2388 	struct nfs4_opendata *data = calldata;
2389 	struct nfs4_state *state = NULL;
2390 
2391 	/* If this request hasn't been cancelled, do nothing */
2392 	if (!data->cancelled)
2393 		goto out_free;
2394 	/* In case of error, no cleanup! */
2395 	if (!data->rpc_done)
2396 		goto out_free;
2397 	state = nfs4_opendata_to_nfs4_state(data);
2398 	if (!IS_ERR(state))
2399 		nfs4_close_state(state, data->o_arg.fmode);
2400 out_free:
2401 	nfs4_opendata_put(data);
2402 }
2403 
2404 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2405 	.rpc_call_prepare = nfs4_open_confirm_prepare,
2406 	.rpc_call_done = nfs4_open_confirm_done,
2407 	.rpc_release = nfs4_open_confirm_release,
2408 };
2409 
2410 /*
2411  * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2412  */
2413 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2414 {
2415 	struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2416 	struct rpc_task *task;
2417 	struct  rpc_message msg = {
2418 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2419 		.rpc_argp = &data->c_arg,
2420 		.rpc_resp = &data->c_res,
2421 		.rpc_cred = data->owner->so_cred,
2422 	};
2423 	struct rpc_task_setup task_setup_data = {
2424 		.rpc_client = server->client,
2425 		.rpc_message = &msg,
2426 		.callback_ops = &nfs4_open_confirm_ops,
2427 		.callback_data = data,
2428 		.workqueue = nfsiod_workqueue,
2429 		.flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2430 	};
2431 	int status;
2432 
2433 	nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2434 				data->is_recover);
2435 	kref_get(&data->kref);
2436 	data->rpc_done = false;
2437 	data->rpc_status = 0;
2438 	data->timestamp = jiffies;
2439 	task = rpc_run_task(&task_setup_data);
2440 	if (IS_ERR(task))
2441 		return PTR_ERR(task);
2442 	status = rpc_wait_for_completion_task(task);
2443 	if (status != 0) {
2444 		data->cancelled = true;
2445 		smp_wmb();
2446 	} else
2447 		status = data->rpc_status;
2448 	rpc_put_task(task);
2449 	return status;
2450 }
2451 
2452 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2453 {
2454 	struct nfs4_opendata *data = calldata;
2455 	struct nfs4_state_owner *sp = data->owner;
2456 	struct nfs_client *clp = sp->so_server->nfs_client;
2457 	enum open_claim_type4 claim = data->o_arg.claim;
2458 
2459 	if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2460 		goto out_wait;
2461 	/*
2462 	 * Check if we still need to send an OPEN call, or if we can use
2463 	 * a delegation instead.
2464 	 */
2465 	if (data->state != NULL) {
2466 		struct nfs_delegation *delegation;
2467 
2468 		if (can_open_cached(data->state, data->o_arg.fmode,
2469 					data->o_arg.open_flags, claim))
2470 			goto out_no_action;
2471 		rcu_read_lock();
2472 		delegation = nfs4_get_valid_delegation(data->state->inode);
2473 		if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2474 			goto unlock_no_action;
2475 		rcu_read_unlock();
2476 	}
2477 	/* Update client id. */
2478 	data->o_arg.clientid = clp->cl_clientid;
2479 	switch (claim) {
2480 	default:
2481 		break;
2482 	case NFS4_OPEN_CLAIM_PREVIOUS:
2483 	case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2484 	case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2485 		data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2486 		fallthrough;
2487 	case NFS4_OPEN_CLAIM_FH:
2488 		task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2489 	}
2490 	data->timestamp = jiffies;
2491 	if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2492 				&data->o_arg.seq_args,
2493 				&data->o_res.seq_res,
2494 				task) != 0)
2495 		nfs_release_seqid(data->o_arg.seqid);
2496 
2497 	/* Set the create mode (note dependency on the session type) */
2498 	data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2499 	if (data->o_arg.open_flags & O_EXCL) {
2500 		data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2501 		if (clp->cl_mvops->minor_version == 0) {
2502 			data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2503 			/* don't put an ACCESS op in OPEN compound if O_EXCL,
2504 			 * because ACCESS will return permission denied for
2505 			 * all bits until close */
2506 			data->o_res.access_request = data->o_arg.access = 0;
2507 		} else if (nfs4_has_persistent_session(clp))
2508 			data->o_arg.createmode = NFS4_CREATE_GUARDED;
2509 	}
2510 	return;
2511 unlock_no_action:
2512 	trace_nfs4_cached_open(data->state);
2513 	rcu_read_unlock();
2514 out_no_action:
2515 	task->tk_action = NULL;
2516 out_wait:
2517 	nfs4_sequence_done(task, &data->o_res.seq_res);
2518 }
2519 
2520 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2521 {
2522 	struct nfs4_opendata *data = calldata;
2523 
2524 	data->rpc_status = task->tk_status;
2525 
2526 	if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2527 		return;
2528 
2529 	if (task->tk_status == 0) {
2530 		if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2531 			switch (data->o_res.f_attr->mode & S_IFMT) {
2532 			case S_IFREG:
2533 				break;
2534 			case S_IFLNK:
2535 				data->rpc_status = -ELOOP;
2536 				break;
2537 			case S_IFDIR:
2538 				data->rpc_status = -EISDIR;
2539 				break;
2540 			default:
2541 				data->rpc_status = -ENOTDIR;
2542 			}
2543 		}
2544 		renew_lease(data->o_res.server, data->timestamp);
2545 		if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2546 			nfs_confirm_seqid(&data->owner->so_seqid, 0);
2547 	}
2548 	data->rpc_done = true;
2549 }
2550 
2551 static void nfs4_open_release(void *calldata)
2552 {
2553 	struct nfs4_opendata *data = calldata;
2554 	struct nfs4_state *state = NULL;
2555 
2556 	/* If this request hasn't been cancelled, do nothing */
2557 	if (!data->cancelled)
2558 		goto out_free;
2559 	/* In case of error, no cleanup! */
2560 	if (data->rpc_status != 0 || !data->rpc_done)
2561 		goto out_free;
2562 	/* In case we need an open_confirm, no cleanup! */
2563 	if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2564 		goto out_free;
2565 	state = nfs4_opendata_to_nfs4_state(data);
2566 	if (!IS_ERR(state))
2567 		nfs4_close_state(state, data->o_arg.fmode);
2568 out_free:
2569 	nfs4_opendata_put(data);
2570 }
2571 
2572 static const struct rpc_call_ops nfs4_open_ops = {
2573 	.rpc_call_prepare = nfs4_open_prepare,
2574 	.rpc_call_done = nfs4_open_done,
2575 	.rpc_release = nfs4_open_release,
2576 };
2577 
2578 static int nfs4_run_open_task(struct nfs4_opendata *data,
2579 			      struct nfs_open_context *ctx)
2580 {
2581 	struct inode *dir = d_inode(data->dir);
2582 	struct nfs_server *server = NFS_SERVER(dir);
2583 	struct nfs_openargs *o_arg = &data->o_arg;
2584 	struct nfs_openres *o_res = &data->o_res;
2585 	struct rpc_task *task;
2586 	struct rpc_message msg = {
2587 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2588 		.rpc_argp = o_arg,
2589 		.rpc_resp = o_res,
2590 		.rpc_cred = data->owner->so_cred,
2591 	};
2592 	struct rpc_task_setup task_setup_data = {
2593 		.rpc_client = server->client,
2594 		.rpc_message = &msg,
2595 		.callback_ops = &nfs4_open_ops,
2596 		.callback_data = data,
2597 		.workqueue = nfsiod_workqueue,
2598 		.flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2599 	};
2600 	int status;
2601 
2602 	if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
2603 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
2604 
2605 	kref_get(&data->kref);
2606 	data->rpc_done = false;
2607 	data->rpc_status = 0;
2608 	data->cancelled = false;
2609 	data->is_recover = false;
2610 	if (!ctx) {
2611 		nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2612 		data->is_recover = true;
2613 		task_setup_data.flags |= RPC_TASK_TIMEOUT;
2614 	} else {
2615 		nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2616 		pnfs_lgopen_prepare(data, ctx);
2617 	}
2618 	task = rpc_run_task(&task_setup_data);
2619 	if (IS_ERR(task))
2620 		return PTR_ERR(task);
2621 	status = rpc_wait_for_completion_task(task);
2622 	if (status != 0) {
2623 		data->cancelled = true;
2624 		smp_wmb();
2625 	} else
2626 		status = data->rpc_status;
2627 	rpc_put_task(task);
2628 
2629 	return status;
2630 }
2631 
2632 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2633 {
2634 	struct inode *dir = d_inode(data->dir);
2635 	struct nfs_openres *o_res = &data->o_res;
2636 	int status;
2637 
2638 	status = nfs4_run_open_task(data, NULL);
2639 	if (status != 0 || !data->rpc_done)
2640 		return status;
2641 
2642 	nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2643 
2644 	if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2645 		status = _nfs4_proc_open_confirm(data);
2646 
2647 	return status;
2648 }
2649 
2650 /*
2651  * Additional permission checks in order to distinguish between an
2652  * open for read, and an open for execute. This works around the
2653  * fact that NFSv4 OPEN treats read and execute permissions as being
2654  * the same.
2655  * Note that in the non-execute case, we want to turn off permission
2656  * checking if we just created a new file (POSIX open() semantics).
2657  */
2658 static int nfs4_opendata_access(const struct cred *cred,
2659 				struct nfs4_opendata *opendata,
2660 				struct nfs4_state *state, fmode_t fmode)
2661 {
2662 	struct nfs_access_entry cache;
2663 	u32 mask, flags;
2664 
2665 	/* access call failed or for some reason the server doesn't
2666 	 * support any access modes -- defer access call until later */
2667 	if (opendata->o_res.access_supported == 0)
2668 		return 0;
2669 
2670 	mask = 0;
2671 	if (fmode & FMODE_EXEC) {
2672 		/* ONLY check for exec rights */
2673 		if (S_ISDIR(state->inode->i_mode))
2674 			mask = NFS4_ACCESS_LOOKUP;
2675 		else
2676 			mask = NFS4_ACCESS_EXECUTE;
2677 	} else if ((fmode & FMODE_READ) && !opendata->file_created)
2678 		mask = NFS4_ACCESS_READ;
2679 
2680 	nfs_access_set_mask(&cache, opendata->o_res.access_result);
2681 	nfs_access_add_cache(state->inode, &cache, cred);
2682 
2683 	flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2684 	if ((mask & ~cache.mask & flags) == 0)
2685 		return 0;
2686 
2687 	return -EACCES;
2688 }
2689 
2690 /*
2691  * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2692  */
2693 static int _nfs4_proc_open(struct nfs4_opendata *data,
2694 			   struct nfs_open_context *ctx)
2695 {
2696 	struct inode *dir = d_inode(data->dir);
2697 	struct nfs_server *server = NFS_SERVER(dir);
2698 	struct nfs_openargs *o_arg = &data->o_arg;
2699 	struct nfs_openres *o_res = &data->o_res;
2700 	int status;
2701 
2702 	status = nfs4_run_open_task(data, ctx);
2703 	if (!data->rpc_done)
2704 		return status;
2705 	if (status != 0) {
2706 		if (status == -NFS4ERR_BADNAME &&
2707 				!(o_arg->open_flags & O_CREAT))
2708 			return -ENOENT;
2709 		return status;
2710 	}
2711 
2712 	nfs_fattr_map_and_free_names(server, &data->f_attr);
2713 
2714 	if (o_arg->open_flags & O_CREAT) {
2715 		if (o_arg->open_flags & O_EXCL)
2716 			data->file_created = true;
2717 		else if (o_res->cinfo.before != o_res->cinfo.after)
2718 			data->file_created = true;
2719 		if (data->file_created ||
2720 		    inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2721 			nfs4_update_changeattr(dir, &o_res->cinfo,
2722 					o_res->f_attr->time_start,
2723 					NFS_INO_INVALID_DATA);
2724 	}
2725 	if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2726 		server->caps &= ~NFS_CAP_POSIX_LOCK;
2727 	if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2728 		status = _nfs4_proc_open_confirm(data);
2729 		if (status != 0)
2730 			return status;
2731 	}
2732 	if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2733 		struct nfs_fh *fh = &o_res->fh;
2734 
2735 		nfs4_sequence_free_slot(&o_res->seq_res);
2736 		if (o_arg->claim == NFS4_OPEN_CLAIM_FH)
2737 			fh = NFS_FH(d_inode(data->dentry));
2738 		nfs4_proc_getattr(server, fh, o_res->f_attr, NULL);
2739 	}
2740 	return 0;
2741 }
2742 
2743 /*
2744  * OPEN_EXPIRED:
2745  * 	reclaim state on the server after a network partition.
2746  * 	Assumes caller holds the appropriate lock
2747  */
2748 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2749 {
2750 	struct nfs4_opendata *opendata;
2751 	int ret;
2752 
2753 	opendata = nfs4_open_recoverdata_alloc(ctx, state, NFS4_OPEN_CLAIM_FH);
2754 	if (IS_ERR(opendata))
2755 		return PTR_ERR(opendata);
2756 	/*
2757 	 * We're not recovering a delegation, so ask for no delegation.
2758 	 * Otherwise the recovery thread could deadlock with an outstanding
2759 	 * delegation return.
2760 	 */
2761 	opendata->o_arg.open_flags = O_DIRECT;
2762 	ret = nfs4_open_recover(opendata, state);
2763 	if (ret == -ESTALE)
2764 		d_drop(ctx->dentry);
2765 	nfs4_opendata_put(opendata);
2766 	return ret;
2767 }
2768 
2769 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2770 {
2771 	struct nfs_server *server = NFS_SERVER(state->inode);
2772 	struct nfs4_exception exception = { };
2773 	int err;
2774 
2775 	do {
2776 		err = _nfs4_open_expired(ctx, state);
2777 		trace_nfs4_open_expired(ctx, 0, err);
2778 		if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2779 			continue;
2780 		switch (err) {
2781 		default:
2782 			goto out;
2783 		case -NFS4ERR_GRACE:
2784 		case -NFS4ERR_DELAY:
2785 			nfs4_handle_exception(server, err, &exception);
2786 			err = 0;
2787 		}
2788 	} while (exception.retry);
2789 out:
2790 	return err;
2791 }
2792 
2793 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2794 {
2795 	struct nfs_open_context *ctx;
2796 	int ret;
2797 
2798 	ctx = nfs4_state_find_open_context(state);
2799 	if (IS_ERR(ctx))
2800 		return -EAGAIN;
2801 	ret = nfs4_do_open_expired(ctx, state);
2802 	put_nfs_open_context(ctx);
2803 	return ret;
2804 }
2805 
2806 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2807 		const nfs4_stateid *stateid)
2808 {
2809 	nfs_remove_bad_delegation(state->inode, stateid);
2810 	nfs_state_clear_delegation(state);
2811 }
2812 
2813 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2814 {
2815 	if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2816 		nfs_finish_clear_delegation_stateid(state, NULL);
2817 }
2818 
2819 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2820 {
2821 	/* NFSv4.0 doesn't allow for delegation recovery on open expire */
2822 	nfs40_clear_delegation_stateid(state);
2823 	nfs_state_clear_open_state_flags(state);
2824 	return nfs4_open_expired(sp, state);
2825 }
2826 
2827 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2828 		nfs4_stateid *stateid,
2829 		const struct cred *cred)
2830 {
2831 	return -NFS4ERR_BAD_STATEID;
2832 }
2833 
2834 #if defined(CONFIG_NFS_V4_1)
2835 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2836 		nfs4_stateid *stateid,
2837 		const struct cred *cred)
2838 {
2839 	int status;
2840 
2841 	switch (stateid->type) {
2842 	default:
2843 		break;
2844 	case NFS4_INVALID_STATEID_TYPE:
2845 	case NFS4_SPECIAL_STATEID_TYPE:
2846 		return -NFS4ERR_BAD_STATEID;
2847 	case NFS4_REVOKED_STATEID_TYPE:
2848 		goto out_free;
2849 	}
2850 
2851 	status = nfs41_test_stateid(server, stateid, cred);
2852 	switch (status) {
2853 	case -NFS4ERR_EXPIRED:
2854 	case -NFS4ERR_ADMIN_REVOKED:
2855 	case -NFS4ERR_DELEG_REVOKED:
2856 		break;
2857 	default:
2858 		return status;
2859 	}
2860 out_free:
2861 	/* Ack the revoked state to the server */
2862 	nfs41_free_stateid(server, stateid, cred, true);
2863 	return -NFS4ERR_EXPIRED;
2864 }
2865 
2866 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2867 {
2868 	struct nfs_server *server = NFS_SERVER(state->inode);
2869 	nfs4_stateid stateid;
2870 	struct nfs_delegation *delegation;
2871 	const struct cred *cred = NULL;
2872 	int status, ret = NFS_OK;
2873 
2874 	/* Get the delegation credential for use by test/free_stateid */
2875 	rcu_read_lock();
2876 	delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2877 	if (delegation == NULL) {
2878 		rcu_read_unlock();
2879 		nfs_state_clear_delegation(state);
2880 		return NFS_OK;
2881 	}
2882 
2883 	spin_lock(&delegation->lock);
2884 	nfs4_stateid_copy(&stateid, &delegation->stateid);
2885 
2886 	if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2887 				&delegation->flags)) {
2888 		spin_unlock(&delegation->lock);
2889 		rcu_read_unlock();
2890 		return NFS_OK;
2891 	}
2892 
2893 	if (delegation->cred)
2894 		cred = get_cred(delegation->cred);
2895 	spin_unlock(&delegation->lock);
2896 	rcu_read_unlock();
2897 	status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2898 	trace_nfs4_test_delegation_stateid(state, NULL, status);
2899 	if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2900 		nfs_finish_clear_delegation_stateid(state, &stateid);
2901 	else
2902 		ret = status;
2903 
2904 	put_cred(cred);
2905 	return ret;
2906 }
2907 
2908 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2909 {
2910 	nfs4_stateid tmp;
2911 
2912 	if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2913 	    nfs4_copy_delegation_stateid(state->inode, state->state,
2914 				&tmp, NULL) &&
2915 	    nfs4_stateid_match_other(&state->stateid, &tmp))
2916 		nfs_state_set_delegation(state, &tmp, state->state);
2917 	else
2918 		nfs_state_clear_delegation(state);
2919 }
2920 
2921 /**
2922  * nfs41_check_expired_locks - possibly free a lock stateid
2923  *
2924  * @state: NFSv4 state for an inode
2925  *
2926  * Returns NFS_OK if recovery for this stateid is now finished.
2927  * Otherwise a negative NFS4ERR value is returned.
2928  */
2929 static int nfs41_check_expired_locks(struct nfs4_state *state)
2930 {
2931 	int status, ret = NFS_OK;
2932 	struct nfs4_lock_state *lsp, *prev = NULL;
2933 	struct nfs_server *server = NFS_SERVER(state->inode);
2934 
2935 	if (!test_bit(LK_STATE_IN_USE, &state->flags))
2936 		goto out;
2937 
2938 	spin_lock(&state->state_lock);
2939 	list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2940 		if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2941 			const struct cred *cred = lsp->ls_state->owner->so_cred;
2942 
2943 			refcount_inc(&lsp->ls_count);
2944 			spin_unlock(&state->state_lock);
2945 
2946 			nfs4_put_lock_state(prev);
2947 			prev = lsp;
2948 
2949 			status = nfs41_test_and_free_expired_stateid(server,
2950 					&lsp->ls_stateid,
2951 					cred);
2952 			trace_nfs4_test_lock_stateid(state, lsp, status);
2953 			if (status == -NFS4ERR_EXPIRED ||
2954 			    status == -NFS4ERR_BAD_STATEID) {
2955 				clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2956 				lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2957 				if (!recover_lost_locks)
2958 					set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2959 			} else if (status != NFS_OK) {
2960 				ret = status;
2961 				nfs4_put_lock_state(prev);
2962 				goto out;
2963 			}
2964 			spin_lock(&state->state_lock);
2965 		}
2966 	}
2967 	spin_unlock(&state->state_lock);
2968 	nfs4_put_lock_state(prev);
2969 out:
2970 	return ret;
2971 }
2972 
2973 /**
2974  * nfs41_check_open_stateid - possibly free an open stateid
2975  *
2976  * @state: NFSv4 state for an inode
2977  *
2978  * Returns NFS_OK if recovery for this stateid is now finished.
2979  * Otherwise a negative NFS4ERR value is returned.
2980  */
2981 static int nfs41_check_open_stateid(struct nfs4_state *state)
2982 {
2983 	struct nfs_server *server = NFS_SERVER(state->inode);
2984 	nfs4_stateid *stateid = &state->open_stateid;
2985 	const struct cred *cred = state->owner->so_cred;
2986 	int status;
2987 
2988 	if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2989 		return -NFS4ERR_BAD_STATEID;
2990 	status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2991 	trace_nfs4_test_open_stateid(state, NULL, status);
2992 	if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2993 		nfs_state_clear_open_state_flags(state);
2994 		stateid->type = NFS4_INVALID_STATEID_TYPE;
2995 		return status;
2996 	}
2997 	if (nfs_open_stateid_recover_openmode(state))
2998 		return -NFS4ERR_OPENMODE;
2999 	return NFS_OK;
3000 }
3001 
3002 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
3003 {
3004 	int status;
3005 
3006 	status = nfs41_check_delegation_stateid(state);
3007 	if (status != NFS_OK)
3008 		return status;
3009 	nfs41_delegation_recover_stateid(state);
3010 
3011 	status = nfs41_check_expired_locks(state);
3012 	if (status != NFS_OK)
3013 		return status;
3014 	status = nfs41_check_open_stateid(state);
3015 	if (status != NFS_OK)
3016 		status = nfs4_open_expired(sp, state);
3017 	return status;
3018 }
3019 #endif
3020 
3021 /*
3022  * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
3023  * fields corresponding to attributes that were used to store the verifier.
3024  * Make sure we clobber those fields in the later setattr call
3025  */
3026 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
3027 				struct iattr *sattr, struct nfs4_label **label)
3028 {
3029 	const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
3030 	__u32 attrset[3];
3031 	unsigned ret;
3032 	unsigned i;
3033 
3034 	for (i = 0; i < ARRAY_SIZE(attrset); i++) {
3035 		attrset[i] = opendata->o_res.attrset[i];
3036 		if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
3037 			attrset[i] &= ~bitmask[i];
3038 	}
3039 
3040 	ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3041 		sattr->ia_valid : 0;
3042 
3043 	if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3044 		if (sattr->ia_valid & ATTR_ATIME_SET)
3045 			ret |= ATTR_ATIME_SET;
3046 		else
3047 			ret |= ATTR_ATIME;
3048 	}
3049 
3050 	if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3051 		if (sattr->ia_valid & ATTR_MTIME_SET)
3052 			ret |= ATTR_MTIME_SET;
3053 		else
3054 			ret |= ATTR_MTIME;
3055 	}
3056 
3057 	if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
3058 		*label = NULL;
3059 	return ret;
3060 }
3061 
3062 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
3063 		struct nfs_open_context *ctx)
3064 {
3065 	struct nfs4_state_owner *sp = opendata->owner;
3066 	struct nfs_server *server = sp->so_server;
3067 	struct dentry *dentry;
3068 	struct nfs4_state *state;
3069 	fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3070 	struct inode *dir = d_inode(opendata->dir);
3071 	unsigned long dir_verifier;
3072 	int ret;
3073 
3074 	dir_verifier = nfs_save_change_attribute(dir);
3075 
3076 	ret = _nfs4_proc_open(opendata, ctx);
3077 	if (ret != 0)
3078 		goto out;
3079 
3080 	state = _nfs4_opendata_to_nfs4_state(opendata);
3081 	ret = PTR_ERR(state);
3082 	if (IS_ERR(state))
3083 		goto out;
3084 	ctx->state = state;
3085 	if (server->caps & NFS_CAP_POSIX_LOCK)
3086 		set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3087 	if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3088 		set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3089 	if (opendata->o_res.rflags & NFS4_OPEN_RESULT_PRESERVE_UNLINKED)
3090 		set_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(state->inode)->flags);
3091 
3092 	dentry = opendata->dentry;
3093 	if (d_really_is_negative(dentry)) {
3094 		struct dentry *alias;
3095 		d_drop(dentry);
3096 		alias = d_exact_alias(dentry, state->inode);
3097 		if (!alias)
3098 			alias = d_splice_alias(igrab(state->inode), dentry);
3099 		/* d_splice_alias() can't fail here - it's a non-directory */
3100 		if (alias) {
3101 			dput(ctx->dentry);
3102 			ctx->dentry = dentry = alias;
3103 		}
3104 	}
3105 
3106 	switch(opendata->o_arg.claim) {
3107 	default:
3108 		break;
3109 	case NFS4_OPEN_CLAIM_NULL:
3110 	case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3111 	case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3112 		if (!opendata->rpc_done)
3113 			break;
3114 		if (opendata->o_res.delegation_type != 0)
3115 			dir_verifier = nfs_save_change_attribute(dir);
3116 		nfs_set_verifier(dentry, dir_verifier);
3117 	}
3118 
3119 	/* Parse layoutget results before we check for access */
3120 	pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3121 
3122 	ret = nfs4_opendata_access(sp->so_cred, opendata, state, acc_mode);
3123 	if (ret != 0)
3124 		goto out;
3125 
3126 	if (d_inode(dentry) == state->inode)
3127 		nfs_inode_attach_open_context(ctx);
3128 
3129 out:
3130 	if (!opendata->cancelled) {
3131 		if (opendata->lgp) {
3132 			nfs4_lgopen_release(opendata->lgp);
3133 			opendata->lgp = NULL;
3134 		}
3135 		nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3136 	}
3137 	return ret;
3138 }
3139 
3140 /*
3141  * Returns a referenced nfs4_state
3142  */
3143 static int _nfs4_do_open(struct inode *dir,
3144 			struct nfs_open_context *ctx,
3145 			int flags,
3146 			const struct nfs4_open_createattrs *c,
3147 			int *opened)
3148 {
3149 	struct nfs4_state_owner  *sp;
3150 	struct nfs4_state     *state = NULL;
3151 	struct nfs_server       *server = NFS_SERVER(dir);
3152 	struct nfs4_opendata *opendata;
3153 	struct dentry *dentry = ctx->dentry;
3154 	const struct cred *cred = ctx->cred;
3155 	struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3156 	fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3157 	enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3158 	struct iattr *sattr = c->sattr;
3159 	struct nfs4_label *label = c->label;
3160 	int status;
3161 
3162 	/* Protect against reboot recovery conflicts */
3163 	status = -ENOMEM;
3164 	sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3165 	if (sp == NULL) {
3166 		dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3167 		goto out_err;
3168 	}
3169 	status = nfs4_client_recover_expired_lease(server->nfs_client);
3170 	if (status != 0)
3171 		goto err_put_state_owner;
3172 	if (d_really_is_positive(dentry))
3173 		nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3174 	status = -ENOMEM;
3175 	if (d_really_is_positive(dentry))
3176 		claim = NFS4_OPEN_CLAIM_FH;
3177 	opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3178 			c, claim, GFP_KERNEL);
3179 	if (opendata == NULL)
3180 		goto err_put_state_owner;
3181 
3182 	if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3183 		if (!opendata->f_attr.mdsthreshold) {
3184 			opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3185 			if (!opendata->f_attr.mdsthreshold)
3186 				goto err_opendata_put;
3187 		}
3188 		opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3189 	}
3190 	if (d_really_is_positive(dentry))
3191 		opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3192 
3193 	status = _nfs4_open_and_get_state(opendata, ctx);
3194 	if (status != 0)
3195 		goto err_opendata_put;
3196 	state = ctx->state;
3197 
3198 	if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3199 	    (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3200 		unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3201 		/*
3202 		 * send create attributes which was not set by open
3203 		 * with an extra setattr.
3204 		 */
3205 		if (attrs || label) {
3206 			unsigned ia_old = sattr->ia_valid;
3207 
3208 			sattr->ia_valid = attrs;
3209 			nfs_fattr_init(opendata->o_res.f_attr);
3210 			status = nfs4_do_setattr(state->inode, cred,
3211 					opendata->o_res.f_attr, sattr,
3212 					ctx, label);
3213 			if (status == 0) {
3214 				nfs_setattr_update_inode(state->inode, sattr,
3215 						opendata->o_res.f_attr);
3216 				nfs_setsecurity(state->inode, opendata->o_res.f_attr);
3217 			}
3218 			sattr->ia_valid = ia_old;
3219 		}
3220 	}
3221 	if (opened && opendata->file_created)
3222 		*opened = 1;
3223 
3224 	if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3225 		*ctx_th = opendata->f_attr.mdsthreshold;
3226 		opendata->f_attr.mdsthreshold = NULL;
3227 	}
3228 
3229 	nfs4_opendata_put(opendata);
3230 	nfs4_put_state_owner(sp);
3231 	return 0;
3232 err_opendata_put:
3233 	nfs4_opendata_put(opendata);
3234 err_put_state_owner:
3235 	nfs4_put_state_owner(sp);
3236 out_err:
3237 	return status;
3238 }
3239 
3240 
3241 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3242 					struct nfs_open_context *ctx,
3243 					int flags,
3244 					struct iattr *sattr,
3245 					struct nfs4_label *label,
3246 					int *opened)
3247 {
3248 	struct nfs_server *server = NFS_SERVER(dir);
3249 	struct nfs4_exception exception = {
3250 		.interruptible = true,
3251 	};
3252 	struct nfs4_state *res;
3253 	struct nfs4_open_createattrs c = {
3254 		.label = label,
3255 		.sattr = sattr,
3256 		.verf = {
3257 			[0] = (__u32)jiffies,
3258 			[1] = (__u32)current->pid,
3259 		},
3260 	};
3261 	int status;
3262 
3263 	do {
3264 		status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3265 		res = ctx->state;
3266 		trace_nfs4_open_file(ctx, flags, status);
3267 		if (status == 0)
3268 			break;
3269 		/* NOTE: BAD_SEQID means the server and client disagree about the
3270 		 * book-keeping w.r.t. state-changing operations
3271 		 * (OPEN/CLOSE/LOCK/LOCKU...)
3272 		 * It is actually a sign of a bug on the client or on the server.
3273 		 *
3274 		 * If we receive a BAD_SEQID error in the particular case of
3275 		 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3276 		 * have unhashed the old state_owner for us, and that we can
3277 		 * therefore safely retry using a new one. We should still warn
3278 		 * the user though...
3279 		 */
3280 		if (status == -NFS4ERR_BAD_SEQID) {
3281 			pr_warn_ratelimited("NFS: v4 server %s "
3282 					" returned a bad sequence-id error!\n",
3283 					NFS_SERVER(dir)->nfs_client->cl_hostname);
3284 			exception.retry = 1;
3285 			continue;
3286 		}
3287 		/*
3288 		 * BAD_STATEID on OPEN means that the server cancelled our
3289 		 * state before it received the OPEN_CONFIRM.
3290 		 * Recover by retrying the request as per the discussion
3291 		 * on Page 181 of RFC3530.
3292 		 */
3293 		if (status == -NFS4ERR_BAD_STATEID) {
3294 			exception.retry = 1;
3295 			continue;
3296 		}
3297 		if (status == -NFS4ERR_EXPIRED) {
3298 			nfs4_schedule_lease_recovery(server->nfs_client);
3299 			exception.retry = 1;
3300 			continue;
3301 		}
3302 		if (status == -EAGAIN) {
3303 			/* We must have found a delegation */
3304 			exception.retry = 1;
3305 			continue;
3306 		}
3307 		if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3308 			continue;
3309 		res = ERR_PTR(nfs4_handle_exception(server,
3310 					status, &exception));
3311 	} while (exception.retry);
3312 	return res;
3313 }
3314 
3315 static int _nfs4_do_setattr(struct inode *inode,
3316 			    struct nfs_setattrargs *arg,
3317 			    struct nfs_setattrres *res,
3318 			    const struct cred *cred,
3319 			    struct nfs_open_context *ctx)
3320 {
3321 	struct nfs_server *server = NFS_SERVER(inode);
3322 	struct rpc_message msg = {
3323 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3324 		.rpc_argp	= arg,
3325 		.rpc_resp	= res,
3326 		.rpc_cred	= cred,
3327 	};
3328 	const struct cred *delegation_cred = NULL;
3329 	unsigned long timestamp = jiffies;
3330 	bool truncate;
3331 	int status;
3332 
3333 	nfs_fattr_init(res->fattr);
3334 
3335 	/* Servers should only apply open mode checks for file size changes */
3336 	truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3337 	if (!truncate) {
3338 		nfs4_inode_make_writeable(inode);
3339 		goto zero_stateid;
3340 	}
3341 
3342 	if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3343 		/* Use that stateid */
3344 	} else if (ctx != NULL && ctx->state) {
3345 		struct nfs_lock_context *l_ctx;
3346 		if (!nfs4_valid_open_stateid(ctx->state))
3347 			return -EBADF;
3348 		l_ctx = nfs_get_lock_context(ctx);
3349 		if (IS_ERR(l_ctx))
3350 			return PTR_ERR(l_ctx);
3351 		status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3352 						&arg->stateid, &delegation_cred);
3353 		nfs_put_lock_context(l_ctx);
3354 		if (status == -EIO)
3355 			return -EBADF;
3356 		else if (status == -EAGAIN)
3357 			goto zero_stateid;
3358 	} else {
3359 zero_stateid:
3360 		nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3361 	}
3362 	if (delegation_cred)
3363 		msg.rpc_cred = delegation_cred;
3364 
3365 	status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3366 
3367 	put_cred(delegation_cred);
3368 	if (status == 0 && ctx != NULL)
3369 		renew_lease(server, timestamp);
3370 	trace_nfs4_setattr(inode, &arg->stateid, status);
3371 	return status;
3372 }
3373 
3374 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3375 			   struct nfs_fattr *fattr, struct iattr *sattr,
3376 			   struct nfs_open_context *ctx, struct nfs4_label *ilabel)
3377 {
3378 	struct nfs_server *server = NFS_SERVER(inode);
3379 	__u32 bitmask[NFS4_BITMASK_SZ];
3380 	struct nfs4_state *state = ctx ? ctx->state : NULL;
3381 	struct nfs_setattrargs	arg = {
3382 		.fh		= NFS_FH(inode),
3383 		.iap		= sattr,
3384 		.server		= server,
3385 		.bitmask = bitmask,
3386 		.label		= ilabel,
3387 	};
3388 	struct nfs_setattrres  res = {
3389 		.fattr		= fattr,
3390 		.server		= server,
3391 	};
3392 	struct nfs4_exception exception = {
3393 		.state = state,
3394 		.inode = inode,
3395 		.stateid = &arg.stateid,
3396 	};
3397 	unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
3398 	int err;
3399 
3400 	if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3401 		adjust_flags |= NFS_INO_INVALID_MODE;
3402 	if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
3403 		adjust_flags |= NFS_INO_INVALID_OTHER;
3404 
3405 	do {
3406 		nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label),
3407 					inode, adjust_flags);
3408 
3409 		err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3410 		switch (err) {
3411 		case -NFS4ERR_OPENMODE:
3412 			if (!(sattr->ia_valid & ATTR_SIZE)) {
3413 				pr_warn_once("NFSv4: server %s is incorrectly "
3414 						"applying open mode checks to "
3415 						"a SETATTR that is not "
3416 						"changing file size.\n",
3417 						server->nfs_client->cl_hostname);
3418 			}
3419 			if (state && !(state->state & FMODE_WRITE)) {
3420 				err = -EBADF;
3421 				if (sattr->ia_valid & ATTR_OPEN)
3422 					err = -EACCES;
3423 				goto out;
3424 			}
3425 		}
3426 		err = nfs4_handle_exception(server, err, &exception);
3427 	} while (exception.retry);
3428 out:
3429 	return err;
3430 }
3431 
3432 static bool
3433 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3434 {
3435 	if (inode == NULL || !nfs_have_layout(inode))
3436 		return false;
3437 
3438 	return pnfs_wait_on_layoutreturn(inode, task);
3439 }
3440 
3441 /*
3442  * Update the seqid of an open stateid
3443  */
3444 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3445 		struct nfs4_state *state)
3446 {
3447 	__be32 seqid_open;
3448 	u32 dst_seqid;
3449 	int seq;
3450 
3451 	for (;;) {
3452 		if (!nfs4_valid_open_stateid(state))
3453 			break;
3454 		seq = read_seqbegin(&state->seqlock);
3455 		if (!nfs4_state_match_open_stateid_other(state, dst)) {
3456 			nfs4_stateid_copy(dst, &state->open_stateid);
3457 			if (read_seqretry(&state->seqlock, seq))
3458 				continue;
3459 			break;
3460 		}
3461 		seqid_open = state->open_stateid.seqid;
3462 		if (read_seqretry(&state->seqlock, seq))
3463 			continue;
3464 
3465 		dst_seqid = be32_to_cpu(dst->seqid);
3466 		if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3467 			dst->seqid = seqid_open;
3468 		break;
3469 	}
3470 }
3471 
3472 /*
3473  * Update the seqid of an open stateid after receiving
3474  * NFS4ERR_OLD_STATEID
3475  */
3476 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3477 		struct nfs4_state *state)
3478 {
3479 	__be32 seqid_open;
3480 	u32 dst_seqid;
3481 	bool ret;
3482 	int seq, status = -EAGAIN;
3483 	DEFINE_WAIT(wait);
3484 
3485 	for (;;) {
3486 		ret = false;
3487 		if (!nfs4_valid_open_stateid(state))
3488 			break;
3489 		seq = read_seqbegin(&state->seqlock);
3490 		if (!nfs4_state_match_open_stateid_other(state, dst)) {
3491 			if (read_seqretry(&state->seqlock, seq))
3492 				continue;
3493 			break;
3494 		}
3495 
3496 		write_seqlock(&state->seqlock);
3497 		seqid_open = state->open_stateid.seqid;
3498 
3499 		dst_seqid = be32_to_cpu(dst->seqid);
3500 
3501 		/* Did another OPEN bump the state's seqid?  try again: */
3502 		if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3503 			dst->seqid = seqid_open;
3504 			write_sequnlock(&state->seqlock);
3505 			ret = true;
3506 			break;
3507 		}
3508 
3509 		/* server says we're behind but we haven't seen the update yet */
3510 		set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3511 		prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3512 		write_sequnlock(&state->seqlock);
3513 		trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3514 
3515 		if (fatal_signal_pending(current))
3516 			status = -EINTR;
3517 		else
3518 			if (schedule_timeout(5*HZ) != 0)
3519 				status = 0;
3520 
3521 		finish_wait(&state->waitq, &wait);
3522 
3523 		if (!status)
3524 			continue;
3525 		if (status == -EINTR)
3526 			break;
3527 
3528 		/* we slept the whole 5 seconds, we must have lost a seqid */
3529 		dst->seqid = cpu_to_be32(dst_seqid + 1);
3530 		ret = true;
3531 		break;
3532 	}
3533 
3534 	return ret;
3535 }
3536 
3537 struct nfs4_closedata {
3538 	struct inode *inode;
3539 	struct nfs4_state *state;
3540 	struct nfs_closeargs arg;
3541 	struct nfs_closeres res;
3542 	struct {
3543 		struct nfs4_layoutreturn_args arg;
3544 		struct nfs4_layoutreturn_res res;
3545 		struct nfs4_xdr_opaque_data ld_private;
3546 		u32 roc_barrier;
3547 		bool roc;
3548 	} lr;
3549 	struct nfs_fattr fattr;
3550 	unsigned long timestamp;
3551 };
3552 
3553 static void nfs4_free_closedata(void *data)
3554 {
3555 	struct nfs4_closedata *calldata = data;
3556 	struct nfs4_state_owner *sp = calldata->state->owner;
3557 	struct super_block *sb = calldata->state->inode->i_sb;
3558 
3559 	if (calldata->lr.roc)
3560 		pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3561 				calldata->res.lr_ret);
3562 	nfs4_put_open_state(calldata->state);
3563 	nfs_free_seqid(calldata->arg.seqid);
3564 	nfs4_put_state_owner(sp);
3565 	nfs_sb_deactive(sb);
3566 	kfree(calldata);
3567 }
3568 
3569 static void nfs4_close_done(struct rpc_task *task, void *data)
3570 {
3571 	struct nfs4_closedata *calldata = data;
3572 	struct nfs4_state *state = calldata->state;
3573 	struct nfs_server *server = NFS_SERVER(calldata->inode);
3574 	nfs4_stateid *res_stateid = NULL;
3575 	struct nfs4_exception exception = {
3576 		.state = state,
3577 		.inode = calldata->inode,
3578 		.stateid = &calldata->arg.stateid,
3579 	};
3580 
3581 	if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3582 		return;
3583 	trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3584 
3585 	/* Handle Layoutreturn errors */
3586 	if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3587 			  &calldata->res.lr_ret) == -EAGAIN)
3588 		goto out_restart;
3589 
3590 	/* hmm. we are done with the inode, and in the process of freeing
3591 	 * the state_owner. we keep this around to process errors
3592 	 */
3593 	switch (task->tk_status) {
3594 		case 0:
3595 			res_stateid = &calldata->res.stateid;
3596 			renew_lease(server, calldata->timestamp);
3597 			break;
3598 		case -NFS4ERR_ACCESS:
3599 			if (calldata->arg.bitmask != NULL) {
3600 				calldata->arg.bitmask = NULL;
3601 				calldata->res.fattr = NULL;
3602 				goto out_restart;
3603 
3604 			}
3605 			break;
3606 		case -NFS4ERR_OLD_STATEID:
3607 			/* Did we race with OPEN? */
3608 			if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3609 						state))
3610 				goto out_restart;
3611 			goto out_release;
3612 		case -NFS4ERR_ADMIN_REVOKED:
3613 		case -NFS4ERR_STALE_STATEID:
3614 		case -NFS4ERR_EXPIRED:
3615 			nfs4_free_revoked_stateid(server,
3616 					&calldata->arg.stateid,
3617 					task->tk_msg.rpc_cred);
3618 			fallthrough;
3619 		case -NFS4ERR_BAD_STATEID:
3620 			if (calldata->arg.fmode == 0)
3621 				break;
3622 			fallthrough;
3623 		default:
3624 			task->tk_status = nfs4_async_handle_exception(task,
3625 					server, task->tk_status, &exception);
3626 			if (exception.retry)
3627 				goto out_restart;
3628 	}
3629 	nfs_clear_open_stateid(state, &calldata->arg.stateid,
3630 			res_stateid, calldata->arg.fmode);
3631 out_release:
3632 	task->tk_status = 0;
3633 	nfs_release_seqid(calldata->arg.seqid);
3634 	nfs_refresh_inode(calldata->inode, &calldata->fattr);
3635 	dprintk("%s: ret = %d\n", __func__, task->tk_status);
3636 	return;
3637 out_restart:
3638 	task->tk_status = 0;
3639 	rpc_restart_call_prepare(task);
3640 	goto out_release;
3641 }
3642 
3643 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3644 {
3645 	struct nfs4_closedata *calldata = data;
3646 	struct nfs4_state *state = calldata->state;
3647 	struct inode *inode = calldata->inode;
3648 	struct nfs_server *server = NFS_SERVER(inode);
3649 	struct pnfs_layout_hdr *lo;
3650 	bool is_rdonly, is_wronly, is_rdwr;
3651 	int call_close = 0;
3652 
3653 	if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3654 		goto out_wait;
3655 
3656 	task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3657 	spin_lock(&state->owner->so_lock);
3658 	is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3659 	is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3660 	is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3661 	/* Calculate the change in open mode */
3662 	calldata->arg.fmode = 0;
3663 	if (state->n_rdwr == 0) {
3664 		if (state->n_rdonly == 0)
3665 			call_close |= is_rdonly;
3666 		else if (is_rdonly)
3667 			calldata->arg.fmode |= FMODE_READ;
3668 		if (state->n_wronly == 0)
3669 			call_close |= is_wronly;
3670 		else if (is_wronly)
3671 			calldata->arg.fmode |= FMODE_WRITE;
3672 		if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3673 			call_close |= is_rdwr;
3674 	} else if (is_rdwr)
3675 		calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3676 
3677 	nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3678 	if (!nfs4_valid_open_stateid(state))
3679 		call_close = 0;
3680 	spin_unlock(&state->owner->so_lock);
3681 
3682 	if (!call_close) {
3683 		/* Note: exit _without_ calling nfs4_close_done */
3684 		goto out_no_action;
3685 	}
3686 
3687 	if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3688 		nfs_release_seqid(calldata->arg.seqid);
3689 		goto out_wait;
3690 	}
3691 
3692 	lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3693 	if (lo && !pnfs_layout_is_valid(lo)) {
3694 		calldata->arg.lr_args = NULL;
3695 		calldata->res.lr_res = NULL;
3696 	}
3697 
3698 	if (calldata->arg.fmode == 0)
3699 		task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3700 
3701 	if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3702 		/* Close-to-open cache consistency revalidation */
3703 		if (!nfs4_have_delegation(inode, FMODE_READ)) {
3704 			nfs4_bitmask_set(calldata->arg.bitmask_store,
3705 					 server->cache_consistency_bitmask,
3706 					 inode, 0);
3707 			calldata->arg.bitmask = calldata->arg.bitmask_store;
3708 		} else
3709 			calldata->arg.bitmask = NULL;
3710 	}
3711 
3712 	calldata->arg.share_access =
3713 		nfs4_map_atomic_open_share(NFS_SERVER(inode),
3714 				calldata->arg.fmode, 0);
3715 
3716 	if (calldata->res.fattr == NULL)
3717 		calldata->arg.bitmask = NULL;
3718 	else if (calldata->arg.bitmask == NULL)
3719 		calldata->res.fattr = NULL;
3720 	calldata->timestamp = jiffies;
3721 	if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3722 				&calldata->arg.seq_args,
3723 				&calldata->res.seq_res,
3724 				task) != 0)
3725 		nfs_release_seqid(calldata->arg.seqid);
3726 	return;
3727 out_no_action:
3728 	task->tk_action = NULL;
3729 out_wait:
3730 	nfs4_sequence_done(task, &calldata->res.seq_res);
3731 }
3732 
3733 static const struct rpc_call_ops nfs4_close_ops = {
3734 	.rpc_call_prepare = nfs4_close_prepare,
3735 	.rpc_call_done = nfs4_close_done,
3736 	.rpc_release = nfs4_free_closedata,
3737 };
3738 
3739 /*
3740  * It is possible for data to be read/written from a mem-mapped file
3741  * after the sys_close call (which hits the vfs layer as a flush).
3742  * This means that we can't safely call nfsv4 close on a file until
3743  * the inode is cleared. This in turn means that we are not good
3744  * NFSv4 citizens - we do not indicate to the server to update the file's
3745  * share state even when we are done with one of the three share
3746  * stateid's in the inode.
3747  *
3748  * NOTE: Caller must be holding the sp->so_owner semaphore!
3749  */
3750 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3751 {
3752 	struct nfs_server *server = NFS_SERVER(state->inode);
3753 	struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3754 	struct nfs4_closedata *calldata;
3755 	struct nfs4_state_owner *sp = state->owner;
3756 	struct rpc_task *task;
3757 	struct rpc_message msg = {
3758 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3759 		.rpc_cred = state->owner->so_cred,
3760 	};
3761 	struct rpc_task_setup task_setup_data = {
3762 		.rpc_client = server->client,
3763 		.rpc_message = &msg,
3764 		.callback_ops = &nfs4_close_ops,
3765 		.workqueue = nfsiod_workqueue,
3766 		.flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3767 	};
3768 	int status = -ENOMEM;
3769 
3770 	if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
3771 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
3772 
3773 	nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3774 		&task_setup_data.rpc_client, &msg);
3775 
3776 	calldata = kzalloc(sizeof(*calldata), gfp_mask);
3777 	if (calldata == NULL)
3778 		goto out;
3779 	nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3780 	calldata->inode = state->inode;
3781 	calldata->state = state;
3782 	calldata->arg.fh = NFS_FH(state->inode);
3783 	if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3784 		goto out_free_calldata;
3785 	/* Serialization for the sequence id */
3786 	alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3787 	calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3788 	if (IS_ERR(calldata->arg.seqid))
3789 		goto out_free_calldata;
3790 	nfs_fattr_init(&calldata->fattr);
3791 	calldata->arg.fmode = 0;
3792 	calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3793 	calldata->res.fattr = &calldata->fattr;
3794 	calldata->res.seqid = calldata->arg.seqid;
3795 	calldata->res.server = server;
3796 	calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3797 	calldata->lr.roc = pnfs_roc(state->inode,
3798 			&calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3799 	if (calldata->lr.roc) {
3800 		calldata->arg.lr_args = &calldata->lr.arg;
3801 		calldata->res.lr_res = &calldata->lr.res;
3802 	}
3803 	nfs_sb_active(calldata->inode->i_sb);
3804 
3805 	msg.rpc_argp = &calldata->arg;
3806 	msg.rpc_resp = &calldata->res;
3807 	task_setup_data.callback_data = calldata;
3808 	task = rpc_run_task(&task_setup_data);
3809 	if (IS_ERR(task))
3810 		return PTR_ERR(task);
3811 	status = 0;
3812 	if (wait)
3813 		status = rpc_wait_for_completion_task(task);
3814 	rpc_put_task(task);
3815 	return status;
3816 out_free_calldata:
3817 	kfree(calldata);
3818 out:
3819 	nfs4_put_open_state(state);
3820 	nfs4_put_state_owner(sp);
3821 	return status;
3822 }
3823 
3824 static struct inode *
3825 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3826 		int open_flags, struct iattr *attr, int *opened)
3827 {
3828 	struct nfs4_state *state;
3829 	struct nfs4_label l, *label;
3830 
3831 	label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3832 
3833 	/* Protect against concurrent sillydeletes */
3834 	state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3835 
3836 	nfs4_label_release_security(label);
3837 
3838 	if (IS_ERR(state))
3839 		return ERR_CAST(state);
3840 	return state->inode;
3841 }
3842 
3843 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3844 {
3845 	if (ctx->state == NULL)
3846 		return;
3847 	if (is_sync)
3848 		nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3849 	else
3850 		nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3851 }
3852 
3853 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3854 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3855 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
3856 
3857 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3858 {
3859 	u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3860 	struct nfs4_server_caps_arg args = {
3861 		.fhandle = fhandle,
3862 		.bitmask = bitmask,
3863 	};
3864 	struct nfs4_server_caps_res res = {};
3865 	struct rpc_message msg = {
3866 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3867 		.rpc_argp = &args,
3868 		.rpc_resp = &res,
3869 	};
3870 	int status;
3871 	int i;
3872 
3873 	bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3874 		     FATTR4_WORD0_FH_EXPIRE_TYPE |
3875 		     FATTR4_WORD0_LINK_SUPPORT |
3876 		     FATTR4_WORD0_SYMLINK_SUPPORT |
3877 		     FATTR4_WORD0_ACLSUPPORT |
3878 		     FATTR4_WORD0_CASE_INSENSITIVE |
3879 		     FATTR4_WORD0_CASE_PRESERVING;
3880 	if (minorversion)
3881 		bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3882 
3883 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3884 	if (status == 0) {
3885 		/* Sanity check the server answers */
3886 		switch (minorversion) {
3887 		case 0:
3888 			res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3889 			res.attr_bitmask[2] = 0;
3890 			break;
3891 		case 1:
3892 			res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3893 			break;
3894 		case 2:
3895 			res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3896 		}
3897 		memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3898 		server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS |
3899 				  NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL);
3900 		server->fattr_valid = NFS_ATTR_FATTR_V4;
3901 		if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3902 				res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3903 			server->caps |= NFS_CAP_ACLS;
3904 		if (res.has_links != 0)
3905 			server->caps |= NFS_CAP_HARDLINKS;
3906 		if (res.has_symlinks != 0)
3907 			server->caps |= NFS_CAP_SYMLINKS;
3908 		if (res.case_insensitive)
3909 			server->caps |= NFS_CAP_CASE_INSENSITIVE;
3910 		if (res.case_preserving)
3911 			server->caps |= NFS_CAP_CASE_PRESERVING;
3912 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3913 		if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3914 			server->caps |= NFS_CAP_SECURITY_LABEL;
3915 #endif
3916 		if (res.attr_bitmask[0] & FATTR4_WORD0_FS_LOCATIONS)
3917 			server->caps |= NFS_CAP_FS_LOCATIONS;
3918 		if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
3919 			server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
3920 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
3921 			server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
3922 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
3923 			server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
3924 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
3925 			server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
3926 				NFS_ATTR_FATTR_OWNER_NAME);
3927 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
3928 			server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
3929 				NFS_ATTR_FATTR_GROUP_NAME);
3930 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
3931 			server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
3932 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
3933 			server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
3934 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
3935 			server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
3936 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
3937 			server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
3938 		memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3939 				sizeof(server->attr_bitmask));
3940 		server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3941 
3942 		memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3943 		server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3944 		server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3945 		server->cache_consistency_bitmask[2] = 0;
3946 
3947 		/* Avoid a regression due to buggy server */
3948 		for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3949 			res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3950 		memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3951 			sizeof(server->exclcreat_bitmask));
3952 
3953 		server->acl_bitmask = res.acl_bitmask;
3954 		server->fh_expire_type = res.fh_expire_type;
3955 	}
3956 
3957 	return status;
3958 }
3959 
3960 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3961 {
3962 	struct nfs4_exception exception = {
3963 		.interruptible = true,
3964 	};
3965 	int err;
3966 
3967 	nfs4_server_set_init_caps(server);
3968 	do {
3969 		err = nfs4_handle_exception(server,
3970 				_nfs4_server_capabilities(server, fhandle),
3971 				&exception);
3972 	} while (exception.retry);
3973 	return err;
3974 }
3975 
3976 static void test_fs_location_for_trunking(struct nfs4_fs_location *location,
3977 					  struct nfs_client *clp,
3978 					  struct nfs_server *server)
3979 {
3980 	int i;
3981 
3982 	for (i = 0; i < location->nservers; i++) {
3983 		struct nfs4_string *srv_loc = &location->servers[i];
3984 		struct sockaddr_storage addr;
3985 		size_t addrlen;
3986 		struct xprt_create xprt_args = {
3987 			.ident = 0,
3988 			.net = clp->cl_net,
3989 		};
3990 		struct nfs4_add_xprt_data xprtdata = {
3991 			.clp = clp,
3992 		};
3993 		struct rpc_add_xprt_test rpcdata = {
3994 			.add_xprt_test = clp->cl_mvops->session_trunk,
3995 			.data = &xprtdata,
3996 		};
3997 		char *servername = NULL;
3998 
3999 		if (!srv_loc->len)
4000 			continue;
4001 
4002 		addrlen = nfs_parse_server_name(srv_loc->data, srv_loc->len,
4003 						&addr, sizeof(addr),
4004 						clp->cl_net, server->port);
4005 		if (!addrlen)
4006 			return;
4007 		xprt_args.dstaddr = (struct sockaddr *)&addr;
4008 		xprt_args.addrlen = addrlen;
4009 		servername = kmalloc(srv_loc->len + 1, GFP_KERNEL);
4010 		if (!servername)
4011 			return;
4012 		memcpy(servername, srv_loc->data, srv_loc->len);
4013 		servername[srv_loc->len] = '\0';
4014 		xprt_args.servername = servername;
4015 
4016 		xprtdata.cred = nfs4_get_clid_cred(clp);
4017 		rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
4018 				  rpc_clnt_setup_test_and_add_xprt,
4019 				  &rpcdata);
4020 		if (xprtdata.cred)
4021 			put_cred(xprtdata.cred);
4022 		kfree(servername);
4023 	}
4024 }
4025 
4026 static bool _is_same_nfs4_pathname(struct nfs4_pathname *path1,
4027 				   struct nfs4_pathname *path2)
4028 {
4029 	int i;
4030 
4031 	if (path1->ncomponents != path2->ncomponents)
4032 		return false;
4033 	for (i = 0; i < path1->ncomponents; i++) {
4034 		if (path1->components[i].len != path2->components[i].len)
4035 			return false;
4036 		if (memcmp(path1->components[i].data, path2->components[i].data,
4037 				path1->components[i].len))
4038 			return false;
4039 	}
4040 	return true;
4041 }
4042 
4043 static int _nfs4_discover_trunking(struct nfs_server *server,
4044 				   struct nfs_fh *fhandle)
4045 {
4046 	struct nfs4_fs_locations *locations = NULL;
4047 	struct page *page;
4048 	const struct cred *cred;
4049 	struct nfs_client *clp = server->nfs_client;
4050 	const struct nfs4_state_maintenance_ops *ops =
4051 		clp->cl_mvops->state_renewal_ops;
4052 	int status = -ENOMEM, i;
4053 
4054 	cred = ops->get_state_renewal_cred(clp);
4055 	if (cred == NULL) {
4056 		cred = nfs4_get_clid_cred(clp);
4057 		if (cred == NULL)
4058 			return -ENOKEY;
4059 	}
4060 
4061 	page = alloc_page(GFP_KERNEL);
4062 	if (!page)
4063 		goto out_put_cred;
4064 	locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4065 	if (!locations)
4066 		goto out_free;
4067 	locations->fattr = nfs_alloc_fattr();
4068 	if (!locations->fattr)
4069 		goto out_free_2;
4070 
4071 	status = nfs4_proc_get_locations(server, fhandle, locations, page,
4072 					 cred);
4073 	if (status)
4074 		goto out_free_3;
4075 
4076 	for (i = 0; i < locations->nlocations; i++) {
4077 		if (!_is_same_nfs4_pathname(&locations->fs_path,
4078 					&locations->locations[i].rootpath))
4079 			continue;
4080 		test_fs_location_for_trunking(&locations->locations[i], clp,
4081 					      server);
4082 	}
4083 out_free_3:
4084 	kfree(locations->fattr);
4085 out_free_2:
4086 	kfree(locations);
4087 out_free:
4088 	__free_page(page);
4089 out_put_cred:
4090 	put_cred(cred);
4091 	return status;
4092 }
4093 
4094 static int nfs4_discover_trunking(struct nfs_server *server,
4095 				  struct nfs_fh *fhandle)
4096 {
4097 	struct nfs4_exception exception = {
4098 		.interruptible = true,
4099 	};
4100 	struct nfs_client *clp = server->nfs_client;
4101 	int err = 0;
4102 
4103 	if (!nfs4_has_session(clp))
4104 		goto out;
4105 	do {
4106 		err = nfs4_handle_exception(server,
4107 				_nfs4_discover_trunking(server, fhandle),
4108 				&exception);
4109 	} while (exception.retry);
4110 out:
4111 	return err;
4112 }
4113 
4114 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4115 		struct nfs_fsinfo *info)
4116 {
4117 	u32 bitmask[3];
4118 	struct nfs4_lookup_root_arg args = {
4119 		.bitmask = bitmask,
4120 	};
4121 	struct nfs4_lookup_res res = {
4122 		.server = server,
4123 		.fattr = info->fattr,
4124 		.fh = fhandle,
4125 	};
4126 	struct rpc_message msg = {
4127 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
4128 		.rpc_argp = &args,
4129 		.rpc_resp = &res,
4130 	};
4131 
4132 	bitmask[0] = nfs4_fattr_bitmap[0];
4133 	bitmask[1] = nfs4_fattr_bitmap[1];
4134 	/*
4135 	 * Process the label in the upcoming getfattr
4136 	 */
4137 	bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
4138 
4139 	nfs_fattr_init(info->fattr);
4140 	return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4141 }
4142 
4143 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4144 		struct nfs_fsinfo *info)
4145 {
4146 	struct nfs4_exception exception = {
4147 		.interruptible = true,
4148 	};
4149 	int err;
4150 	do {
4151 		err = _nfs4_lookup_root(server, fhandle, info);
4152 		trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
4153 		switch (err) {
4154 		case 0:
4155 		case -NFS4ERR_WRONGSEC:
4156 			goto out;
4157 		default:
4158 			err = nfs4_handle_exception(server, err, &exception);
4159 		}
4160 	} while (exception.retry);
4161 out:
4162 	return err;
4163 }
4164 
4165 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4166 				struct nfs_fsinfo *info, rpc_authflavor_t flavor)
4167 {
4168 	struct rpc_auth_create_args auth_args = {
4169 		.pseudoflavor = flavor,
4170 	};
4171 	struct rpc_auth *auth;
4172 
4173 	auth = rpcauth_create(&auth_args, server->client);
4174 	if (IS_ERR(auth))
4175 		return -EACCES;
4176 	return nfs4_lookup_root(server, fhandle, info);
4177 }
4178 
4179 /*
4180  * Retry pseudoroot lookup with various security flavors.  We do this when:
4181  *
4182  *   NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4183  *   NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4184  *
4185  * Returns zero on success, or a negative NFS4ERR value, or a
4186  * negative errno value.
4187  */
4188 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4189 			      struct nfs_fsinfo *info)
4190 {
4191 	/* Per 3530bis 15.33.5 */
4192 	static const rpc_authflavor_t flav_array[] = {
4193 		RPC_AUTH_GSS_KRB5P,
4194 		RPC_AUTH_GSS_KRB5I,
4195 		RPC_AUTH_GSS_KRB5,
4196 		RPC_AUTH_UNIX,			/* courtesy */
4197 		RPC_AUTH_NULL,
4198 	};
4199 	int status = -EPERM;
4200 	size_t i;
4201 
4202 	if (server->auth_info.flavor_len > 0) {
4203 		/* try each flavor specified by user */
4204 		for (i = 0; i < server->auth_info.flavor_len; i++) {
4205 			status = nfs4_lookup_root_sec(server, fhandle, info,
4206 						server->auth_info.flavors[i]);
4207 			if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4208 				continue;
4209 			break;
4210 		}
4211 	} else {
4212 		/* no flavors specified by user, try default list */
4213 		for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4214 			status = nfs4_lookup_root_sec(server, fhandle, info,
4215 						      flav_array[i]);
4216 			if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4217 				continue;
4218 			break;
4219 		}
4220 	}
4221 
4222 	/*
4223 	 * -EACCES could mean that the user doesn't have correct permissions
4224 	 * to access the mount.  It could also mean that we tried to mount
4225 	 * with a gss auth flavor, but rpc.gssd isn't running.  Either way,
4226 	 * existing mount programs don't handle -EACCES very well so it should
4227 	 * be mapped to -EPERM instead.
4228 	 */
4229 	if (status == -EACCES)
4230 		status = -EPERM;
4231 	return status;
4232 }
4233 
4234 /**
4235  * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4236  * @server: initialized nfs_server handle
4237  * @fhandle: we fill in the pseudo-fs root file handle
4238  * @info: we fill in an FSINFO struct
4239  * @auth_probe: probe the auth flavours
4240  *
4241  * Returns zero on success, or a negative errno.
4242  */
4243 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4244 			 struct nfs_fsinfo *info,
4245 			 bool auth_probe)
4246 {
4247 	int status = 0;
4248 
4249 	if (!auth_probe)
4250 		status = nfs4_lookup_root(server, fhandle, info);
4251 
4252 	if (auth_probe || status == NFS4ERR_WRONGSEC)
4253 		status = server->nfs_client->cl_mvops->find_root_sec(server,
4254 				fhandle, info);
4255 
4256 	if (status == 0)
4257 		status = nfs4_server_capabilities(server, fhandle);
4258 	if (status == 0)
4259 		status = nfs4_do_fsinfo(server, fhandle, info);
4260 
4261 	return nfs4_map_errors(status);
4262 }
4263 
4264 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4265 			      struct nfs_fsinfo *info)
4266 {
4267 	int error;
4268 	struct nfs_fattr *fattr = info->fattr;
4269 
4270 	error = nfs4_server_capabilities(server, mntfh);
4271 	if (error < 0) {
4272 		dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4273 		return error;
4274 	}
4275 
4276 	error = nfs4_proc_getattr(server, mntfh, fattr, NULL);
4277 	if (error < 0) {
4278 		dprintk("nfs4_get_root: getattr error = %d\n", -error);
4279 		goto out;
4280 	}
4281 
4282 	if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4283 	    !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4284 		memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4285 
4286 out:
4287 	return error;
4288 }
4289 
4290 /*
4291  * Get locations and (maybe) other attributes of a referral.
4292  * Note that we'll actually follow the referral later when
4293  * we detect fsid mismatch in inode revalidation
4294  */
4295 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4296 			     const struct qstr *name, struct nfs_fattr *fattr,
4297 			     struct nfs_fh *fhandle)
4298 {
4299 	int status = -ENOMEM;
4300 	struct page *page = NULL;
4301 	struct nfs4_fs_locations *locations = NULL;
4302 
4303 	page = alloc_page(GFP_KERNEL);
4304 	if (page == NULL)
4305 		goto out;
4306 	locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4307 	if (locations == NULL)
4308 		goto out;
4309 
4310 	locations->fattr = fattr;
4311 
4312 	status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4313 	if (status != 0)
4314 		goto out;
4315 
4316 	/*
4317 	 * If the fsid didn't change, this is a migration event, not a
4318 	 * referral.  Cause us to drop into the exception handler, which
4319 	 * will kick off migration recovery.
4320 	 */
4321 	if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &fattr->fsid)) {
4322 		dprintk("%s: server did not return a different fsid for"
4323 			" a referral at %s\n", __func__, name->name);
4324 		status = -NFS4ERR_MOVED;
4325 		goto out;
4326 	}
4327 	/* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4328 	nfs_fixup_referral_attributes(fattr);
4329 	memset(fhandle, 0, sizeof(struct nfs_fh));
4330 out:
4331 	if (page)
4332 		__free_page(page);
4333 	kfree(locations);
4334 	return status;
4335 }
4336 
4337 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4338 				struct nfs_fattr *fattr, struct inode *inode)
4339 {
4340 	__u32 bitmask[NFS4_BITMASK_SZ];
4341 	struct nfs4_getattr_arg args = {
4342 		.fh = fhandle,
4343 		.bitmask = bitmask,
4344 	};
4345 	struct nfs4_getattr_res res = {
4346 		.fattr = fattr,
4347 		.server = server,
4348 	};
4349 	struct rpc_message msg = {
4350 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4351 		.rpc_argp = &args,
4352 		.rpc_resp = &res,
4353 	};
4354 	unsigned short task_flags = 0;
4355 
4356 	if (nfs4_has_session(server->nfs_client))
4357 		task_flags = RPC_TASK_MOVEABLE;
4358 
4359 	/* Is this is an attribute revalidation, subject to softreval? */
4360 	if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4361 		task_flags |= RPC_TASK_TIMEOUT;
4362 
4363 	nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label), inode, 0);
4364 	nfs_fattr_init(fattr);
4365 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4366 	return nfs4_do_call_sync(server->client, server, &msg,
4367 			&args.seq_args, &res.seq_res, task_flags);
4368 }
4369 
4370 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4371 				struct nfs_fattr *fattr, struct inode *inode)
4372 {
4373 	struct nfs4_exception exception = {
4374 		.interruptible = true,
4375 	};
4376 	int err;
4377 	do {
4378 		err = _nfs4_proc_getattr(server, fhandle, fattr, inode);
4379 		trace_nfs4_getattr(server, fhandle, fattr, err);
4380 		err = nfs4_handle_exception(server, err,
4381 				&exception);
4382 	} while (exception.retry);
4383 	return err;
4384 }
4385 
4386 /*
4387  * The file is not closed if it is opened due to the a request to change
4388  * the size of the file. The open call will not be needed once the
4389  * VFS layer lookup-intents are implemented.
4390  *
4391  * Close is called when the inode is destroyed.
4392  * If we haven't opened the file for O_WRONLY, we
4393  * need to in the size_change case to obtain a stateid.
4394  *
4395  * Got race?
4396  * Because OPEN is always done by name in nfsv4, it is
4397  * possible that we opened a different file by the same
4398  * name.  We can recognize this race condition, but we
4399  * can't do anything about it besides returning an error.
4400  *
4401  * This will be fixed with VFS changes (lookup-intent).
4402  */
4403 static int
4404 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4405 		  struct iattr *sattr)
4406 {
4407 	struct inode *inode = d_inode(dentry);
4408 	const struct cred *cred = NULL;
4409 	struct nfs_open_context *ctx = NULL;
4410 	int status;
4411 
4412 	if (pnfs_ld_layoutret_on_setattr(inode) &&
4413 	    sattr->ia_valid & ATTR_SIZE &&
4414 	    sattr->ia_size < i_size_read(inode))
4415 		pnfs_commit_and_return_layout(inode);
4416 
4417 	nfs_fattr_init(fattr);
4418 
4419 	/* Deal with open(O_TRUNC) */
4420 	if (sattr->ia_valid & ATTR_OPEN)
4421 		sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4422 
4423 	/* Optimization: if the end result is no change, don't RPC */
4424 	if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4425 		return 0;
4426 
4427 	/* Search for an existing open(O_WRITE) file */
4428 	if (sattr->ia_valid & ATTR_FILE) {
4429 
4430 		ctx = nfs_file_open_context(sattr->ia_file);
4431 		if (ctx)
4432 			cred = ctx->cred;
4433 	}
4434 
4435 	/* Return any delegations if we're going to change ACLs */
4436 	if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4437 		nfs4_inode_make_writeable(inode);
4438 
4439 	status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL);
4440 	if (status == 0) {
4441 		nfs_setattr_update_inode(inode, sattr, fattr);
4442 		nfs_setsecurity(inode, fattr);
4443 	}
4444 	return status;
4445 }
4446 
4447 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4448 		struct dentry *dentry, struct nfs_fh *fhandle,
4449 		struct nfs_fattr *fattr)
4450 {
4451 	struct nfs_server *server = NFS_SERVER(dir);
4452 	int		       status;
4453 	struct nfs4_lookup_arg args = {
4454 		.bitmask = server->attr_bitmask,
4455 		.dir_fh = NFS_FH(dir),
4456 		.name = &dentry->d_name,
4457 	};
4458 	struct nfs4_lookup_res res = {
4459 		.server = server,
4460 		.fattr = fattr,
4461 		.fh = fhandle,
4462 	};
4463 	struct rpc_message msg = {
4464 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4465 		.rpc_argp = &args,
4466 		.rpc_resp = &res,
4467 	};
4468 	unsigned short task_flags = 0;
4469 
4470 	if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
4471 		task_flags = RPC_TASK_MOVEABLE;
4472 
4473 	/* Is this is an attribute revalidation, subject to softreval? */
4474 	if (nfs_lookup_is_soft_revalidate(dentry))
4475 		task_flags |= RPC_TASK_TIMEOUT;
4476 
4477 	args.bitmask = nfs4_bitmask(server, fattr->label);
4478 
4479 	nfs_fattr_init(fattr);
4480 
4481 	dprintk("NFS call  lookup %pd2\n", dentry);
4482 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4483 	status = nfs4_do_call_sync(clnt, server, &msg,
4484 			&args.seq_args, &res.seq_res, task_flags);
4485 	dprintk("NFS reply lookup: %d\n", status);
4486 	return status;
4487 }
4488 
4489 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4490 {
4491 	fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4492 		NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4493 	fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4494 	fattr->nlink = 2;
4495 }
4496 
4497 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4498 				   struct dentry *dentry, struct nfs_fh *fhandle,
4499 				   struct nfs_fattr *fattr)
4500 {
4501 	struct nfs4_exception exception = {
4502 		.interruptible = true,
4503 	};
4504 	struct rpc_clnt *client = *clnt;
4505 	const struct qstr *name = &dentry->d_name;
4506 	int err;
4507 	do {
4508 		err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr);
4509 		trace_nfs4_lookup(dir, name, err);
4510 		switch (err) {
4511 		case -NFS4ERR_BADNAME:
4512 			err = -ENOENT;
4513 			goto out;
4514 		case -NFS4ERR_MOVED:
4515 			err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4516 			if (err == -NFS4ERR_MOVED)
4517 				err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4518 			goto out;
4519 		case -NFS4ERR_WRONGSEC:
4520 			err = -EPERM;
4521 			if (client != *clnt)
4522 				goto out;
4523 			client = nfs4_negotiate_security(client, dir, name);
4524 			if (IS_ERR(client))
4525 				return PTR_ERR(client);
4526 
4527 			exception.retry = 1;
4528 			break;
4529 		default:
4530 			err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4531 		}
4532 	} while (exception.retry);
4533 
4534 out:
4535 	if (err == 0)
4536 		*clnt = client;
4537 	else if (client != *clnt)
4538 		rpc_shutdown_client(client);
4539 
4540 	return err;
4541 }
4542 
4543 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4544 			    struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4545 {
4546 	int status;
4547 	struct rpc_clnt *client = NFS_CLIENT(dir);
4548 
4549 	status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4550 	if (client != NFS_CLIENT(dir)) {
4551 		rpc_shutdown_client(client);
4552 		nfs_fixup_secinfo_attributes(fattr);
4553 	}
4554 	return status;
4555 }
4556 
4557 struct rpc_clnt *
4558 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4559 			    struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4560 {
4561 	struct rpc_clnt *client = NFS_CLIENT(dir);
4562 	int status;
4563 
4564 	status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4565 	if (status < 0)
4566 		return ERR_PTR(status);
4567 	return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4568 }
4569 
4570 static int _nfs4_proc_lookupp(struct inode *inode,
4571 		struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4572 {
4573 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
4574 	struct nfs_server *server = NFS_SERVER(inode);
4575 	int		       status;
4576 	struct nfs4_lookupp_arg args = {
4577 		.bitmask = server->attr_bitmask,
4578 		.fh = NFS_FH(inode),
4579 	};
4580 	struct nfs4_lookupp_res res = {
4581 		.server = server,
4582 		.fattr = fattr,
4583 		.fh = fhandle,
4584 	};
4585 	struct rpc_message msg = {
4586 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4587 		.rpc_argp = &args,
4588 		.rpc_resp = &res,
4589 	};
4590 	unsigned short task_flags = 0;
4591 
4592 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4593 		task_flags |= RPC_TASK_TIMEOUT;
4594 
4595 	args.bitmask = nfs4_bitmask(server, fattr->label);
4596 
4597 	nfs_fattr_init(fattr);
4598 
4599 	dprintk("NFS call  lookupp ino=0x%lx\n", inode->i_ino);
4600 	status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4601 				&res.seq_res, task_flags);
4602 	dprintk("NFS reply lookupp: %d\n", status);
4603 	return status;
4604 }
4605 
4606 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4607 			     struct nfs_fattr *fattr)
4608 {
4609 	struct nfs4_exception exception = {
4610 		.interruptible = true,
4611 	};
4612 	int err;
4613 	do {
4614 		err = _nfs4_proc_lookupp(inode, fhandle, fattr);
4615 		trace_nfs4_lookupp(inode, err);
4616 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
4617 				&exception);
4618 	} while (exception.retry);
4619 	return err;
4620 }
4621 
4622 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4623 			     const struct cred *cred)
4624 {
4625 	struct nfs_server *server = NFS_SERVER(inode);
4626 	struct nfs4_accessargs args = {
4627 		.fh = NFS_FH(inode),
4628 		.access = entry->mask,
4629 	};
4630 	struct nfs4_accessres res = {
4631 		.server = server,
4632 	};
4633 	struct rpc_message msg = {
4634 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4635 		.rpc_argp = &args,
4636 		.rpc_resp = &res,
4637 		.rpc_cred = cred,
4638 	};
4639 	int status = 0;
4640 
4641 	if (!nfs4_have_delegation(inode, FMODE_READ)) {
4642 		res.fattr = nfs_alloc_fattr();
4643 		if (res.fattr == NULL)
4644 			return -ENOMEM;
4645 		args.bitmask = server->cache_consistency_bitmask;
4646 	}
4647 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4648 	if (!status) {
4649 		nfs_access_set_mask(entry, res.access);
4650 		if (res.fattr)
4651 			nfs_refresh_inode(inode, res.fattr);
4652 	}
4653 	nfs_free_fattr(res.fattr);
4654 	return status;
4655 }
4656 
4657 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4658 			    const struct cred *cred)
4659 {
4660 	struct nfs4_exception exception = {
4661 		.interruptible = true,
4662 	};
4663 	int err;
4664 	do {
4665 		err = _nfs4_proc_access(inode, entry, cred);
4666 		trace_nfs4_access(inode, err);
4667 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
4668 				&exception);
4669 	} while (exception.retry);
4670 	return err;
4671 }
4672 
4673 /*
4674  * TODO: For the time being, we don't try to get any attributes
4675  * along with any of the zero-copy operations READ, READDIR,
4676  * READLINK, WRITE.
4677  *
4678  * In the case of the first three, we want to put the GETATTR
4679  * after the read-type operation -- this is because it is hard
4680  * to predict the length of a GETATTR response in v4, and thus
4681  * align the READ data correctly.  This means that the GETATTR
4682  * may end up partially falling into the page cache, and we should
4683  * shift it into the 'tail' of the xdr_buf before processing.
4684  * To do this efficiently, we need to know the total length
4685  * of data received, which doesn't seem to be available outside
4686  * of the RPC layer.
4687  *
4688  * In the case of WRITE, we also want to put the GETATTR after
4689  * the operation -- in this case because we want to make sure
4690  * we get the post-operation mtime and size.
4691  *
4692  * Both of these changes to the XDR layer would in fact be quite
4693  * minor, but I decided to leave them for a subsequent patch.
4694  */
4695 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4696 		unsigned int pgbase, unsigned int pglen)
4697 {
4698 	struct nfs4_readlink args = {
4699 		.fh       = NFS_FH(inode),
4700 		.pgbase	  = pgbase,
4701 		.pglen    = pglen,
4702 		.pages    = &page,
4703 	};
4704 	struct nfs4_readlink_res res;
4705 	struct rpc_message msg = {
4706 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4707 		.rpc_argp = &args,
4708 		.rpc_resp = &res,
4709 	};
4710 
4711 	return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4712 }
4713 
4714 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4715 		unsigned int pgbase, unsigned int pglen)
4716 {
4717 	struct nfs4_exception exception = {
4718 		.interruptible = true,
4719 	};
4720 	int err;
4721 	do {
4722 		err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4723 		trace_nfs4_readlink(inode, err);
4724 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
4725 				&exception);
4726 	} while (exception.retry);
4727 	return err;
4728 }
4729 
4730 /*
4731  * This is just for mknod.  open(O_CREAT) will always do ->open_context().
4732  */
4733 static int
4734 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4735 		 int flags)
4736 {
4737 	struct nfs_server *server = NFS_SERVER(dir);
4738 	struct nfs4_label l, *ilabel;
4739 	struct nfs_open_context *ctx;
4740 	struct nfs4_state *state;
4741 	int status = 0;
4742 
4743 	ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4744 	if (IS_ERR(ctx))
4745 		return PTR_ERR(ctx);
4746 
4747 	ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4748 
4749 	if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4750 		sattr->ia_mode &= ~current_umask();
4751 	state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4752 	if (IS_ERR(state)) {
4753 		status = PTR_ERR(state);
4754 		goto out;
4755 	}
4756 out:
4757 	nfs4_label_release_security(ilabel);
4758 	put_nfs_open_context(ctx);
4759 	return status;
4760 }
4761 
4762 static int
4763 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4764 {
4765 	struct nfs_server *server = NFS_SERVER(dir);
4766 	struct nfs_removeargs args = {
4767 		.fh = NFS_FH(dir),
4768 		.name = *name,
4769 	};
4770 	struct nfs_removeres res = {
4771 		.server = server,
4772 	};
4773 	struct rpc_message msg = {
4774 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4775 		.rpc_argp = &args,
4776 		.rpc_resp = &res,
4777 	};
4778 	unsigned long timestamp = jiffies;
4779 	int status;
4780 
4781 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4782 	if (status == 0) {
4783 		spin_lock(&dir->i_lock);
4784 		/* Removing a directory decrements nlink in the parent */
4785 		if (ftype == NF4DIR && dir->i_nlink > 2)
4786 			nfs4_dec_nlink_locked(dir);
4787 		nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4788 					      NFS_INO_INVALID_DATA);
4789 		spin_unlock(&dir->i_lock);
4790 	}
4791 	return status;
4792 }
4793 
4794 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4795 {
4796 	struct nfs4_exception exception = {
4797 		.interruptible = true,
4798 	};
4799 	struct inode *inode = d_inode(dentry);
4800 	int err;
4801 
4802 	if (inode) {
4803 		if (inode->i_nlink == 1)
4804 			nfs4_inode_return_delegation(inode);
4805 		else
4806 			nfs4_inode_make_writeable(inode);
4807 	}
4808 	do {
4809 		err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4810 		trace_nfs4_remove(dir, &dentry->d_name, err);
4811 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
4812 				&exception);
4813 	} while (exception.retry);
4814 	return err;
4815 }
4816 
4817 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4818 {
4819 	struct nfs4_exception exception = {
4820 		.interruptible = true,
4821 	};
4822 	int err;
4823 
4824 	do {
4825 		err = _nfs4_proc_remove(dir, name, NF4DIR);
4826 		trace_nfs4_remove(dir, name, err);
4827 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
4828 				&exception);
4829 	} while (exception.retry);
4830 	return err;
4831 }
4832 
4833 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4834 		struct dentry *dentry,
4835 		struct inode *inode)
4836 {
4837 	struct nfs_removeargs *args = msg->rpc_argp;
4838 	struct nfs_removeres *res = msg->rpc_resp;
4839 
4840 	res->server = NFS_SB(dentry->d_sb);
4841 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4842 	nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4843 
4844 	nfs_fattr_init(res->dir_attr);
4845 
4846 	if (inode) {
4847 		nfs4_inode_return_delegation(inode);
4848 		nfs_d_prune_case_insensitive_aliases(inode);
4849 	}
4850 }
4851 
4852 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4853 {
4854 	nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4855 			&data->args.seq_args,
4856 			&data->res.seq_res,
4857 			task);
4858 }
4859 
4860 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4861 {
4862 	struct nfs_unlinkdata *data = task->tk_calldata;
4863 	struct nfs_removeres *res = &data->res;
4864 
4865 	if (!nfs4_sequence_done(task, &res->seq_res))
4866 		return 0;
4867 	if (nfs4_async_handle_error(task, res->server, NULL,
4868 				    &data->timeout) == -EAGAIN)
4869 		return 0;
4870 	if (task->tk_status == 0)
4871 		nfs4_update_changeattr(dir, &res->cinfo,
4872 				res->dir_attr->time_start,
4873 				NFS_INO_INVALID_DATA);
4874 	return 1;
4875 }
4876 
4877 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4878 		struct dentry *old_dentry,
4879 		struct dentry *new_dentry)
4880 {
4881 	struct nfs_renameargs *arg = msg->rpc_argp;
4882 	struct nfs_renameres *res = msg->rpc_resp;
4883 	struct inode *old_inode = d_inode(old_dentry);
4884 	struct inode *new_inode = d_inode(new_dentry);
4885 
4886 	if (old_inode)
4887 		nfs4_inode_make_writeable(old_inode);
4888 	if (new_inode)
4889 		nfs4_inode_return_delegation(new_inode);
4890 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4891 	res->server = NFS_SB(old_dentry->d_sb);
4892 	nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4893 }
4894 
4895 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4896 {
4897 	nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4898 			&data->args.seq_args,
4899 			&data->res.seq_res,
4900 			task);
4901 }
4902 
4903 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4904 				 struct inode *new_dir)
4905 {
4906 	struct nfs_renamedata *data = task->tk_calldata;
4907 	struct nfs_renameres *res = &data->res;
4908 
4909 	if (!nfs4_sequence_done(task, &res->seq_res))
4910 		return 0;
4911 	if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4912 		return 0;
4913 
4914 	if (task->tk_status == 0) {
4915 		nfs_d_prune_case_insensitive_aliases(d_inode(data->old_dentry));
4916 		if (new_dir != old_dir) {
4917 			/* Note: If we moved a directory, nlink will change */
4918 			nfs4_update_changeattr(old_dir, &res->old_cinfo,
4919 					res->old_fattr->time_start,
4920 					NFS_INO_INVALID_NLINK |
4921 					    NFS_INO_INVALID_DATA);
4922 			nfs4_update_changeattr(new_dir, &res->new_cinfo,
4923 					res->new_fattr->time_start,
4924 					NFS_INO_INVALID_NLINK |
4925 					    NFS_INO_INVALID_DATA);
4926 		} else
4927 			nfs4_update_changeattr(old_dir, &res->old_cinfo,
4928 					res->old_fattr->time_start,
4929 					NFS_INO_INVALID_DATA);
4930 	}
4931 	return 1;
4932 }
4933 
4934 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4935 {
4936 	struct nfs_server *server = NFS_SERVER(inode);
4937 	__u32 bitmask[NFS4_BITMASK_SZ];
4938 	struct nfs4_link_arg arg = {
4939 		.fh     = NFS_FH(inode),
4940 		.dir_fh = NFS_FH(dir),
4941 		.name   = name,
4942 		.bitmask = bitmask,
4943 	};
4944 	struct nfs4_link_res res = {
4945 		.server = server,
4946 	};
4947 	struct rpc_message msg = {
4948 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4949 		.rpc_argp = &arg,
4950 		.rpc_resp = &res,
4951 	};
4952 	int status = -ENOMEM;
4953 
4954 	res.fattr = nfs_alloc_fattr_with_label(server);
4955 	if (res.fattr == NULL)
4956 		goto out;
4957 
4958 	nfs4_inode_make_writeable(inode);
4959 	nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.fattr->label), inode,
4960 				NFS_INO_INVALID_CHANGE);
4961 	status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4962 	if (!status) {
4963 		nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4964 				       NFS_INO_INVALID_DATA);
4965 		nfs4_inc_nlink(inode);
4966 		status = nfs_post_op_update_inode(inode, res.fattr);
4967 		if (!status)
4968 			nfs_setsecurity(inode, res.fattr);
4969 	}
4970 
4971 out:
4972 	nfs_free_fattr(res.fattr);
4973 	return status;
4974 }
4975 
4976 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4977 {
4978 	struct nfs4_exception exception = {
4979 		.interruptible = true,
4980 	};
4981 	int err;
4982 	do {
4983 		err = nfs4_handle_exception(NFS_SERVER(inode),
4984 				_nfs4_proc_link(inode, dir, name),
4985 				&exception);
4986 	} while (exception.retry);
4987 	return err;
4988 }
4989 
4990 struct nfs4_createdata {
4991 	struct rpc_message msg;
4992 	struct nfs4_create_arg arg;
4993 	struct nfs4_create_res res;
4994 	struct nfs_fh fh;
4995 	struct nfs_fattr fattr;
4996 };
4997 
4998 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4999 		const struct qstr *name, struct iattr *sattr, u32 ftype)
5000 {
5001 	struct nfs4_createdata *data;
5002 
5003 	data = kzalloc(sizeof(*data), GFP_KERNEL);
5004 	if (data != NULL) {
5005 		struct nfs_server *server = NFS_SERVER(dir);
5006 
5007 		data->fattr.label = nfs4_label_alloc(server, GFP_KERNEL);
5008 		if (IS_ERR(data->fattr.label))
5009 			goto out_free;
5010 
5011 		data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
5012 		data->msg.rpc_argp = &data->arg;
5013 		data->msg.rpc_resp = &data->res;
5014 		data->arg.dir_fh = NFS_FH(dir);
5015 		data->arg.server = server;
5016 		data->arg.name = name;
5017 		data->arg.attrs = sattr;
5018 		data->arg.ftype = ftype;
5019 		data->arg.bitmask = nfs4_bitmask(server, data->fattr.label);
5020 		data->arg.umask = current_umask();
5021 		data->res.server = server;
5022 		data->res.fh = &data->fh;
5023 		data->res.fattr = &data->fattr;
5024 		nfs_fattr_init(data->res.fattr);
5025 	}
5026 	return data;
5027 out_free:
5028 	kfree(data);
5029 	return NULL;
5030 }
5031 
5032 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
5033 {
5034 	int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
5035 				    &data->arg.seq_args, &data->res.seq_res, 1);
5036 	if (status == 0) {
5037 		spin_lock(&dir->i_lock);
5038 		/* Creating a directory bumps nlink in the parent */
5039 		if (data->arg.ftype == NF4DIR)
5040 			nfs4_inc_nlink_locked(dir);
5041 		nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
5042 					      data->res.fattr->time_start,
5043 					      NFS_INO_INVALID_DATA);
5044 		spin_unlock(&dir->i_lock);
5045 		status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
5046 	}
5047 	return status;
5048 }
5049 
5050 static void nfs4_free_createdata(struct nfs4_createdata *data)
5051 {
5052 	nfs4_label_free(data->fattr.label);
5053 	kfree(data);
5054 }
5055 
5056 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5057 		struct folio *folio, unsigned int len, struct iattr *sattr,
5058 		struct nfs4_label *label)
5059 {
5060 	struct page *page = &folio->page;
5061 	struct nfs4_createdata *data;
5062 	int status = -ENAMETOOLONG;
5063 
5064 	if (len > NFS4_MAXPATHLEN)
5065 		goto out;
5066 
5067 	status = -ENOMEM;
5068 	data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
5069 	if (data == NULL)
5070 		goto out;
5071 
5072 	data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
5073 	data->arg.u.symlink.pages = &page;
5074 	data->arg.u.symlink.len = len;
5075 	data->arg.label = label;
5076 
5077 	status = nfs4_do_create(dir, dentry, data);
5078 
5079 	nfs4_free_createdata(data);
5080 out:
5081 	return status;
5082 }
5083 
5084 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5085 		struct folio *folio, unsigned int len, struct iattr *sattr)
5086 {
5087 	struct nfs4_exception exception = {
5088 		.interruptible = true,
5089 	};
5090 	struct nfs4_label l, *label;
5091 	int err;
5092 
5093 	label = nfs4_label_init_security(dir, dentry, sattr, &l);
5094 
5095 	do {
5096 		err = _nfs4_proc_symlink(dir, dentry, folio, len, sattr, label);
5097 		trace_nfs4_symlink(dir, &dentry->d_name, err);
5098 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
5099 				&exception);
5100 	} while (exception.retry);
5101 
5102 	nfs4_label_release_security(label);
5103 	return err;
5104 }
5105 
5106 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5107 		struct iattr *sattr, struct nfs4_label *label)
5108 {
5109 	struct nfs4_createdata *data;
5110 	int status = -ENOMEM;
5111 
5112 	data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
5113 	if (data == NULL)
5114 		goto out;
5115 
5116 	data->arg.label = label;
5117 	status = nfs4_do_create(dir, dentry, data);
5118 
5119 	nfs4_free_createdata(data);
5120 out:
5121 	return status;
5122 }
5123 
5124 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5125 		struct iattr *sattr)
5126 {
5127 	struct nfs_server *server = NFS_SERVER(dir);
5128 	struct nfs4_exception exception = {
5129 		.interruptible = true,
5130 	};
5131 	struct nfs4_label l, *label;
5132 	int err;
5133 
5134 	label = nfs4_label_init_security(dir, dentry, sattr, &l);
5135 
5136 	if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5137 		sattr->ia_mode &= ~current_umask();
5138 	do {
5139 		err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
5140 		trace_nfs4_mkdir(dir, &dentry->d_name, err);
5141 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
5142 				&exception);
5143 	} while (exception.retry);
5144 	nfs4_label_release_security(label);
5145 
5146 	return err;
5147 }
5148 
5149 static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
5150 			      struct nfs_readdir_res *nr_res)
5151 {
5152 	struct inode		*dir = d_inode(nr_arg->dentry);
5153 	struct nfs_server	*server = NFS_SERVER(dir);
5154 	struct nfs4_readdir_arg args = {
5155 		.fh = NFS_FH(dir),
5156 		.pages = nr_arg->pages,
5157 		.pgbase = 0,
5158 		.count = nr_arg->page_len,
5159 		.plus = nr_arg->plus,
5160 	};
5161 	struct nfs4_readdir_res res;
5162 	struct rpc_message msg = {
5163 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5164 		.rpc_argp = &args,
5165 		.rpc_resp = &res,
5166 		.rpc_cred = nr_arg->cred,
5167 	};
5168 	int			status;
5169 
5170 	dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
5171 		nr_arg->dentry, (unsigned long long)nr_arg->cookie);
5172 	if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5173 		args.bitmask = server->attr_bitmask_nl;
5174 	else
5175 		args.bitmask = server->attr_bitmask;
5176 
5177 	nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
5178 	res.pgbase = args.pgbase;
5179 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5180 			&res.seq_res, 0);
5181 	if (status >= 0) {
5182 		memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
5183 		status += args.pgbase;
5184 	}
5185 
5186 	nfs_invalidate_atime(dir);
5187 
5188 	dprintk("%s: returns %d\n", __func__, status);
5189 	return status;
5190 }
5191 
5192 static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5193 			     struct nfs_readdir_res *res)
5194 {
5195 	struct nfs4_exception exception = {
5196 		.interruptible = true,
5197 	};
5198 	int err;
5199 	do {
5200 		err = _nfs4_proc_readdir(arg, res);
5201 		trace_nfs4_readdir(d_inode(arg->dentry), err);
5202 		err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5203 					    err, &exception);
5204 	} while (exception.retry);
5205 	return err;
5206 }
5207 
5208 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5209 		struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5210 {
5211 	struct nfs4_createdata *data;
5212 	int mode = sattr->ia_mode;
5213 	int status = -ENOMEM;
5214 
5215 	data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5216 	if (data == NULL)
5217 		goto out;
5218 
5219 	if (S_ISFIFO(mode))
5220 		data->arg.ftype = NF4FIFO;
5221 	else if (S_ISBLK(mode)) {
5222 		data->arg.ftype = NF4BLK;
5223 		data->arg.u.device.specdata1 = MAJOR(rdev);
5224 		data->arg.u.device.specdata2 = MINOR(rdev);
5225 	}
5226 	else if (S_ISCHR(mode)) {
5227 		data->arg.ftype = NF4CHR;
5228 		data->arg.u.device.specdata1 = MAJOR(rdev);
5229 		data->arg.u.device.specdata2 = MINOR(rdev);
5230 	} else if (!S_ISSOCK(mode)) {
5231 		status = -EINVAL;
5232 		goto out_free;
5233 	}
5234 
5235 	data->arg.label = label;
5236 	status = nfs4_do_create(dir, dentry, data);
5237 out_free:
5238 	nfs4_free_createdata(data);
5239 out:
5240 	return status;
5241 }
5242 
5243 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5244 		struct iattr *sattr, dev_t rdev)
5245 {
5246 	struct nfs_server *server = NFS_SERVER(dir);
5247 	struct nfs4_exception exception = {
5248 		.interruptible = true,
5249 	};
5250 	struct nfs4_label l, *label;
5251 	int err;
5252 
5253 	label = nfs4_label_init_security(dir, dentry, sattr, &l);
5254 
5255 	if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5256 		sattr->ia_mode &= ~current_umask();
5257 	do {
5258 		err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5259 		trace_nfs4_mknod(dir, &dentry->d_name, err);
5260 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
5261 				&exception);
5262 	} while (exception.retry);
5263 
5264 	nfs4_label_release_security(label);
5265 
5266 	return err;
5267 }
5268 
5269 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5270 		 struct nfs_fsstat *fsstat)
5271 {
5272 	struct nfs4_statfs_arg args = {
5273 		.fh = fhandle,
5274 		.bitmask = server->attr_bitmask,
5275 	};
5276 	struct nfs4_statfs_res res = {
5277 		.fsstat = fsstat,
5278 	};
5279 	struct rpc_message msg = {
5280 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5281 		.rpc_argp = &args,
5282 		.rpc_resp = &res,
5283 	};
5284 
5285 	nfs_fattr_init(fsstat->fattr);
5286 	return  nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5287 }
5288 
5289 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5290 {
5291 	struct nfs4_exception exception = {
5292 		.interruptible = true,
5293 	};
5294 	int err;
5295 	do {
5296 		err = nfs4_handle_exception(server,
5297 				_nfs4_proc_statfs(server, fhandle, fsstat),
5298 				&exception);
5299 	} while (exception.retry);
5300 	return err;
5301 }
5302 
5303 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5304 		struct nfs_fsinfo *fsinfo)
5305 {
5306 	struct nfs4_fsinfo_arg args = {
5307 		.fh = fhandle,
5308 		.bitmask = server->attr_bitmask,
5309 	};
5310 	struct nfs4_fsinfo_res res = {
5311 		.fsinfo = fsinfo,
5312 	};
5313 	struct rpc_message msg = {
5314 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5315 		.rpc_argp = &args,
5316 		.rpc_resp = &res,
5317 	};
5318 
5319 	return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5320 }
5321 
5322 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5323 {
5324 	struct nfs4_exception exception = {
5325 		.interruptible = true,
5326 	};
5327 	int err;
5328 
5329 	do {
5330 		err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5331 		trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5332 		if (err == 0) {
5333 			nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5334 			break;
5335 		}
5336 		err = nfs4_handle_exception(server, err, &exception);
5337 	} while (exception.retry);
5338 	return err;
5339 }
5340 
5341 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5342 {
5343 	int error;
5344 
5345 	nfs_fattr_init(fsinfo->fattr);
5346 	error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5347 	if (error == 0) {
5348 		/* block layout checks this! */
5349 		server->pnfs_blksize = fsinfo->blksize;
5350 		set_pnfs_layoutdriver(server, fhandle, fsinfo);
5351 	}
5352 
5353 	return error;
5354 }
5355 
5356 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5357 		struct nfs_pathconf *pathconf)
5358 {
5359 	struct nfs4_pathconf_arg args = {
5360 		.fh = fhandle,
5361 		.bitmask = server->attr_bitmask,
5362 	};
5363 	struct nfs4_pathconf_res res = {
5364 		.pathconf = pathconf,
5365 	};
5366 	struct rpc_message msg = {
5367 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5368 		.rpc_argp = &args,
5369 		.rpc_resp = &res,
5370 	};
5371 
5372 	/* None of the pathconf attributes are mandatory to implement */
5373 	if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5374 		memset(pathconf, 0, sizeof(*pathconf));
5375 		return 0;
5376 	}
5377 
5378 	nfs_fattr_init(pathconf->fattr);
5379 	return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5380 }
5381 
5382 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5383 		struct nfs_pathconf *pathconf)
5384 {
5385 	struct nfs4_exception exception = {
5386 		.interruptible = true,
5387 	};
5388 	int err;
5389 
5390 	do {
5391 		err = nfs4_handle_exception(server,
5392 				_nfs4_proc_pathconf(server, fhandle, pathconf),
5393 				&exception);
5394 	} while (exception.retry);
5395 	return err;
5396 }
5397 
5398 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5399 		const struct nfs_open_context *ctx,
5400 		const struct nfs_lock_context *l_ctx,
5401 		fmode_t fmode)
5402 {
5403 	return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5404 }
5405 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5406 
5407 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5408 		const struct nfs_open_context *ctx,
5409 		const struct nfs_lock_context *l_ctx,
5410 		fmode_t fmode)
5411 {
5412 	nfs4_stateid _current_stateid;
5413 
5414 	/* If the current stateid represents a lost lock, then exit */
5415 	if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5416 		return true;
5417 	return nfs4_stateid_match(stateid, &_current_stateid);
5418 }
5419 
5420 static bool nfs4_error_stateid_expired(int err)
5421 {
5422 	switch (err) {
5423 	case -NFS4ERR_DELEG_REVOKED:
5424 	case -NFS4ERR_ADMIN_REVOKED:
5425 	case -NFS4ERR_BAD_STATEID:
5426 	case -NFS4ERR_STALE_STATEID:
5427 	case -NFS4ERR_OLD_STATEID:
5428 	case -NFS4ERR_OPENMODE:
5429 	case -NFS4ERR_EXPIRED:
5430 		return true;
5431 	}
5432 	return false;
5433 }
5434 
5435 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5436 {
5437 	struct nfs_server *server = NFS_SERVER(hdr->inode);
5438 
5439 	trace_nfs4_read(hdr, task->tk_status);
5440 	if (task->tk_status < 0) {
5441 		struct nfs4_exception exception = {
5442 			.inode = hdr->inode,
5443 			.state = hdr->args.context->state,
5444 			.stateid = &hdr->args.stateid,
5445 		};
5446 		task->tk_status = nfs4_async_handle_exception(task,
5447 				server, task->tk_status, &exception);
5448 		if (exception.retry) {
5449 			rpc_restart_call_prepare(task);
5450 			return -EAGAIN;
5451 		}
5452 	}
5453 
5454 	if (task->tk_status > 0)
5455 		renew_lease(server, hdr->timestamp);
5456 	return 0;
5457 }
5458 
5459 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5460 		struct nfs_pgio_args *args)
5461 {
5462 
5463 	if (!nfs4_error_stateid_expired(task->tk_status) ||
5464 		nfs4_stateid_is_current(&args->stateid,
5465 				args->context,
5466 				args->lock_context,
5467 				FMODE_READ))
5468 		return false;
5469 	rpc_restart_call_prepare(task);
5470 	return true;
5471 }
5472 
5473 static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5474 					 struct nfs_pgio_header *hdr)
5475 {
5476 	struct nfs_server *server = NFS_SERVER(hdr->inode);
5477 	struct rpc_message *msg = &task->tk_msg;
5478 
5479 	if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5480 	    task->tk_status == -ENOTSUPP) {
5481 		server->caps &= ~NFS_CAP_READ_PLUS;
5482 		msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5483 		rpc_restart_call_prepare(task);
5484 		return true;
5485 	}
5486 	return false;
5487 }
5488 
5489 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5490 {
5491 	if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5492 		return -EAGAIN;
5493 	if (nfs4_read_stateid_changed(task, &hdr->args))
5494 		return -EAGAIN;
5495 	if (nfs4_read_plus_not_supported(task, hdr))
5496 		return -EAGAIN;
5497 	if (task->tk_status > 0)
5498 		nfs_invalidate_atime(hdr->inode);
5499 	return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5500 				    nfs4_read_done_cb(task, hdr);
5501 }
5502 
5503 #if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
5504 static bool nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5505 				    struct rpc_message *msg)
5506 {
5507 	/* Note: We don't use READ_PLUS with pNFS yet */
5508 	if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp) {
5509 		msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5510 		return nfs_read_alloc_scratch(hdr, READ_PLUS_SCRATCH_SIZE);
5511 	}
5512 	return false;
5513 }
5514 #else
5515 static bool nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5516 				    struct rpc_message *msg)
5517 {
5518 	return false;
5519 }
5520 #endif /* CONFIG_NFS_V4_2 */
5521 
5522 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5523 				 struct rpc_message *msg)
5524 {
5525 	hdr->timestamp   = jiffies;
5526 	if (!hdr->pgio_done_cb)
5527 		hdr->pgio_done_cb = nfs4_read_done_cb;
5528 	if (!nfs42_read_plus_support(hdr, msg))
5529 		msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5530 	nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5531 }
5532 
5533 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5534 				      struct nfs_pgio_header *hdr)
5535 {
5536 	if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5537 			&hdr->args.seq_args,
5538 			&hdr->res.seq_res,
5539 			task))
5540 		return 0;
5541 	if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5542 				hdr->args.lock_context,
5543 				hdr->rw_mode) == -EIO)
5544 		return -EIO;
5545 	if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5546 		return -EIO;
5547 	return 0;
5548 }
5549 
5550 static int nfs4_write_done_cb(struct rpc_task *task,
5551 			      struct nfs_pgio_header *hdr)
5552 {
5553 	struct inode *inode = hdr->inode;
5554 
5555 	trace_nfs4_write(hdr, task->tk_status);
5556 	if (task->tk_status < 0) {
5557 		struct nfs4_exception exception = {
5558 			.inode = hdr->inode,
5559 			.state = hdr->args.context->state,
5560 			.stateid = &hdr->args.stateid,
5561 		};
5562 		task->tk_status = nfs4_async_handle_exception(task,
5563 				NFS_SERVER(inode), task->tk_status,
5564 				&exception);
5565 		if (exception.retry) {
5566 			rpc_restart_call_prepare(task);
5567 			return -EAGAIN;
5568 		}
5569 	}
5570 	if (task->tk_status >= 0) {
5571 		renew_lease(NFS_SERVER(inode), hdr->timestamp);
5572 		nfs_writeback_update_inode(hdr);
5573 	}
5574 	return 0;
5575 }
5576 
5577 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5578 		struct nfs_pgio_args *args)
5579 {
5580 
5581 	if (!nfs4_error_stateid_expired(task->tk_status) ||
5582 		nfs4_stateid_is_current(&args->stateid,
5583 				args->context,
5584 				args->lock_context,
5585 				FMODE_WRITE))
5586 		return false;
5587 	rpc_restart_call_prepare(task);
5588 	return true;
5589 }
5590 
5591 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5592 {
5593 	if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5594 		return -EAGAIN;
5595 	if (nfs4_write_stateid_changed(task, &hdr->args))
5596 		return -EAGAIN;
5597 	return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5598 		nfs4_write_done_cb(task, hdr);
5599 }
5600 
5601 static
5602 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5603 {
5604 	/* Don't request attributes for pNFS or O_DIRECT writes */
5605 	if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5606 		return false;
5607 	/* Otherwise, request attributes if and only if we don't hold
5608 	 * a delegation
5609 	 */
5610 	return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5611 }
5612 
5613 void nfs4_bitmask_set(__u32 bitmask[], const __u32 src[],
5614 		      struct inode *inode, unsigned long cache_validity)
5615 {
5616 	struct nfs_server *server = NFS_SERVER(inode);
5617 	unsigned int i;
5618 
5619 	memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
5620 	cache_validity |= READ_ONCE(NFS_I(inode)->cache_validity);
5621 
5622 	if (cache_validity & NFS_INO_INVALID_CHANGE)
5623 		bitmask[0] |= FATTR4_WORD0_CHANGE;
5624 	if (cache_validity & NFS_INO_INVALID_ATIME)
5625 		bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5626 	if (cache_validity & NFS_INO_INVALID_MODE)
5627 		bitmask[1] |= FATTR4_WORD1_MODE;
5628 	if (cache_validity & NFS_INO_INVALID_OTHER)
5629 		bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
5630 	if (cache_validity & NFS_INO_INVALID_NLINK)
5631 		bitmask[1] |= FATTR4_WORD1_NUMLINKS;
5632 	if (cache_validity & NFS_INO_INVALID_CTIME)
5633 		bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
5634 	if (cache_validity & NFS_INO_INVALID_MTIME)
5635 		bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5636 	if (cache_validity & NFS_INO_INVALID_BLOCKS)
5637 		bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5638 
5639 	if (cache_validity & NFS_INO_INVALID_SIZE)
5640 		bitmask[0] |= FATTR4_WORD0_SIZE;
5641 
5642 	for (i = 0; i < NFS4_BITMASK_SZ; i++)
5643 		bitmask[i] &= server->attr_bitmask[i];
5644 }
5645 
5646 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5647 				  struct rpc_message *msg,
5648 				  struct rpc_clnt **clnt)
5649 {
5650 	struct nfs_server *server = NFS_SERVER(hdr->inode);
5651 
5652 	if (!nfs4_write_need_cache_consistency_data(hdr)) {
5653 		hdr->args.bitmask = NULL;
5654 		hdr->res.fattr = NULL;
5655 	} else {
5656 		nfs4_bitmask_set(hdr->args.bitmask_store,
5657 				 server->cache_consistency_bitmask,
5658 				 hdr->inode, NFS_INO_INVALID_BLOCKS);
5659 		hdr->args.bitmask = hdr->args.bitmask_store;
5660 	}
5661 
5662 	if (!hdr->pgio_done_cb)
5663 		hdr->pgio_done_cb = nfs4_write_done_cb;
5664 	hdr->res.server = server;
5665 	hdr->timestamp   = jiffies;
5666 
5667 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5668 	nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5669 	nfs4_state_protect_write(hdr->ds_clp ? hdr->ds_clp : server->nfs_client, clnt, msg, hdr);
5670 }
5671 
5672 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5673 {
5674 	nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5675 			&data->args.seq_args,
5676 			&data->res.seq_res,
5677 			task);
5678 }
5679 
5680 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5681 {
5682 	struct inode *inode = data->inode;
5683 
5684 	trace_nfs4_commit(data, task->tk_status);
5685 	if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5686 				    NULL, NULL) == -EAGAIN) {
5687 		rpc_restart_call_prepare(task);
5688 		return -EAGAIN;
5689 	}
5690 	return 0;
5691 }
5692 
5693 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5694 {
5695 	if (!nfs4_sequence_done(task, &data->res.seq_res))
5696 		return -EAGAIN;
5697 	return data->commit_done_cb(task, data);
5698 }
5699 
5700 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5701 				   struct rpc_clnt **clnt)
5702 {
5703 	struct nfs_server *server = NFS_SERVER(data->inode);
5704 
5705 	if (data->commit_done_cb == NULL)
5706 		data->commit_done_cb = nfs4_commit_done_cb;
5707 	data->res.server = server;
5708 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5709 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5710 	nfs4_state_protect(data->ds_clp ? data->ds_clp : server->nfs_client,
5711 			NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5712 }
5713 
5714 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5715 				struct nfs_commitres *res)
5716 {
5717 	struct inode *dst_inode = file_inode(dst);
5718 	struct nfs_server *server = NFS_SERVER(dst_inode);
5719 	struct rpc_message msg = {
5720 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5721 		.rpc_argp = args,
5722 		.rpc_resp = res,
5723 	};
5724 
5725 	args->fh = NFS_FH(dst_inode);
5726 	return nfs4_call_sync(server->client, server, &msg,
5727 			&args->seq_args, &res->seq_res, 1);
5728 }
5729 
5730 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5731 {
5732 	struct nfs_commitargs args = {
5733 		.offset = offset,
5734 		.count = count,
5735 	};
5736 	struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5737 	struct nfs4_exception exception = { };
5738 	int status;
5739 
5740 	do {
5741 		status = _nfs4_proc_commit(dst, &args, res);
5742 		status = nfs4_handle_exception(dst_server, status, &exception);
5743 	} while (exception.retry);
5744 
5745 	return status;
5746 }
5747 
5748 struct nfs4_renewdata {
5749 	struct nfs_client	*client;
5750 	unsigned long		timestamp;
5751 };
5752 
5753 /*
5754  * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5755  * standalone procedure for queueing an asynchronous RENEW.
5756  */
5757 static void nfs4_renew_release(void *calldata)
5758 {
5759 	struct nfs4_renewdata *data = calldata;
5760 	struct nfs_client *clp = data->client;
5761 
5762 	if (refcount_read(&clp->cl_count) > 1)
5763 		nfs4_schedule_state_renewal(clp);
5764 	nfs_put_client(clp);
5765 	kfree(data);
5766 }
5767 
5768 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5769 {
5770 	struct nfs4_renewdata *data = calldata;
5771 	struct nfs_client *clp = data->client;
5772 	unsigned long timestamp = data->timestamp;
5773 
5774 	trace_nfs4_renew_async(clp, task->tk_status);
5775 	switch (task->tk_status) {
5776 	case 0:
5777 		break;
5778 	case -NFS4ERR_LEASE_MOVED:
5779 		nfs4_schedule_lease_moved_recovery(clp);
5780 		break;
5781 	default:
5782 		/* Unless we're shutting down, schedule state recovery! */
5783 		if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5784 			return;
5785 		if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5786 			nfs4_schedule_lease_recovery(clp);
5787 			return;
5788 		}
5789 		nfs4_schedule_path_down_recovery(clp);
5790 	}
5791 	do_renew_lease(clp, timestamp);
5792 }
5793 
5794 static const struct rpc_call_ops nfs4_renew_ops = {
5795 	.rpc_call_done = nfs4_renew_done,
5796 	.rpc_release = nfs4_renew_release,
5797 };
5798 
5799 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5800 {
5801 	struct rpc_message msg = {
5802 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5803 		.rpc_argp	= clp,
5804 		.rpc_cred	= cred,
5805 	};
5806 	struct nfs4_renewdata *data;
5807 
5808 	if (renew_flags == 0)
5809 		return 0;
5810 	if (!refcount_inc_not_zero(&clp->cl_count))
5811 		return -EIO;
5812 	data = kmalloc(sizeof(*data), GFP_NOFS);
5813 	if (data == NULL) {
5814 		nfs_put_client(clp);
5815 		return -ENOMEM;
5816 	}
5817 	data->client = clp;
5818 	data->timestamp = jiffies;
5819 	return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5820 			&nfs4_renew_ops, data);
5821 }
5822 
5823 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5824 {
5825 	struct rpc_message msg = {
5826 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5827 		.rpc_argp	= clp,
5828 		.rpc_cred	= cred,
5829 	};
5830 	unsigned long now = jiffies;
5831 	int status;
5832 
5833 	status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5834 	if (status < 0)
5835 		return status;
5836 	do_renew_lease(clp, now);
5837 	return 0;
5838 }
5839 
5840 static bool nfs4_server_supports_acls(const struct nfs_server *server,
5841 				      enum nfs4_acl_type type)
5842 {
5843 	switch (type) {
5844 	default:
5845 		return server->attr_bitmask[0] & FATTR4_WORD0_ACL;
5846 	case NFS4ACL_DACL:
5847 		return server->attr_bitmask[1] & FATTR4_WORD1_DACL;
5848 	case NFS4ACL_SACL:
5849 		return server->attr_bitmask[1] & FATTR4_WORD1_SACL;
5850 	}
5851 }
5852 
5853 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5854  * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5855  * the stack.
5856  */
5857 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5858 
5859 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5860 		struct page **pages)
5861 {
5862 	struct page *newpage, **spages;
5863 	int rc = 0;
5864 	size_t len;
5865 	spages = pages;
5866 
5867 	do {
5868 		len = min_t(size_t, PAGE_SIZE, buflen);
5869 		newpage = alloc_page(GFP_KERNEL);
5870 
5871 		if (newpage == NULL)
5872 			goto unwind;
5873 		memcpy(page_address(newpage), buf, len);
5874 		buf += len;
5875 		buflen -= len;
5876 		*pages++ = newpage;
5877 		rc++;
5878 	} while (buflen != 0);
5879 
5880 	return rc;
5881 
5882 unwind:
5883 	for(; rc > 0; rc--)
5884 		__free_page(spages[rc-1]);
5885 	return -ENOMEM;
5886 }
5887 
5888 struct nfs4_cached_acl {
5889 	enum nfs4_acl_type type;
5890 	int cached;
5891 	size_t len;
5892 	char data[];
5893 };
5894 
5895 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5896 {
5897 	struct nfs_inode *nfsi = NFS_I(inode);
5898 
5899 	spin_lock(&inode->i_lock);
5900 	kfree(nfsi->nfs4_acl);
5901 	nfsi->nfs4_acl = acl;
5902 	spin_unlock(&inode->i_lock);
5903 }
5904 
5905 static void nfs4_zap_acl_attr(struct inode *inode)
5906 {
5907 	nfs4_set_cached_acl(inode, NULL);
5908 }
5909 
5910 static ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf,
5911 				    size_t buflen, enum nfs4_acl_type type)
5912 {
5913 	struct nfs_inode *nfsi = NFS_I(inode);
5914 	struct nfs4_cached_acl *acl;
5915 	int ret = -ENOENT;
5916 
5917 	spin_lock(&inode->i_lock);
5918 	acl = nfsi->nfs4_acl;
5919 	if (acl == NULL)
5920 		goto out;
5921 	if (acl->type != type)
5922 		goto out;
5923 	if (buf == NULL) /* user is just asking for length */
5924 		goto out_len;
5925 	if (acl->cached == 0)
5926 		goto out;
5927 	ret = -ERANGE; /* see getxattr(2) man page */
5928 	if (acl->len > buflen)
5929 		goto out;
5930 	memcpy(buf, acl->data, acl->len);
5931 out_len:
5932 	ret = acl->len;
5933 out:
5934 	spin_unlock(&inode->i_lock);
5935 	return ret;
5936 }
5937 
5938 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages,
5939 				  size_t pgbase, size_t acl_len,
5940 				  enum nfs4_acl_type type)
5941 {
5942 	struct nfs4_cached_acl *acl;
5943 	size_t buflen = sizeof(*acl) + acl_len;
5944 
5945 	if (buflen <= PAGE_SIZE) {
5946 		acl = kmalloc(buflen, GFP_KERNEL);
5947 		if (acl == NULL)
5948 			goto out;
5949 		acl->cached = 1;
5950 		_copy_from_pages(acl->data, pages, pgbase, acl_len);
5951 	} else {
5952 		acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5953 		if (acl == NULL)
5954 			goto out;
5955 		acl->cached = 0;
5956 	}
5957 	acl->type = type;
5958 	acl->len = acl_len;
5959 out:
5960 	nfs4_set_cached_acl(inode, acl);
5961 }
5962 
5963 /*
5964  * The getxattr API returns the required buffer length when called with a
5965  * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5966  * the required buf.  On a NULL buf, we send a page of data to the server
5967  * guessing that the ACL request can be serviced by a page. If so, we cache
5968  * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5969  * the cache. If not so, we throw away the page, and cache the required
5970  * length. The next getxattr call will then produce another round trip to
5971  * the server, this time with the input buf of the required size.
5972  */
5973 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf,
5974 				       size_t buflen, enum nfs4_acl_type type)
5975 {
5976 	struct page **pages;
5977 	struct nfs_getaclargs args = {
5978 		.fh = NFS_FH(inode),
5979 		.acl_type = type,
5980 		.acl_len = buflen,
5981 	};
5982 	struct nfs_getaclres res = {
5983 		.acl_type = type,
5984 		.acl_len = buflen,
5985 	};
5986 	struct rpc_message msg = {
5987 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5988 		.rpc_argp = &args,
5989 		.rpc_resp = &res,
5990 	};
5991 	unsigned int npages;
5992 	int ret = -ENOMEM, i;
5993 	struct nfs_server *server = NFS_SERVER(inode);
5994 
5995 	if (buflen == 0)
5996 		buflen = server->rsize;
5997 
5998 	npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5999 	pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
6000 	if (!pages)
6001 		return -ENOMEM;
6002 
6003 	args.acl_pages = pages;
6004 
6005 	for (i = 0; i < npages; i++) {
6006 		pages[i] = alloc_page(GFP_KERNEL);
6007 		if (!pages[i])
6008 			goto out_free;
6009 	}
6010 
6011 	/* for decoding across pages */
6012 	res.acl_scratch = alloc_page(GFP_KERNEL);
6013 	if (!res.acl_scratch)
6014 		goto out_free;
6015 
6016 	args.acl_len = npages * PAGE_SIZE;
6017 
6018 	dprintk("%s  buf %p buflen %zu npages %d args.acl_len %zu\n",
6019 		__func__, buf, buflen, npages, args.acl_len);
6020 	ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
6021 			     &msg, &args.seq_args, &res.seq_res, 0);
6022 	if (ret)
6023 		goto out_free;
6024 
6025 	/* Handle the case where the passed-in buffer is too short */
6026 	if (res.acl_flags & NFS4_ACL_TRUNC) {
6027 		/* Did the user only issue a request for the acl length? */
6028 		if (buf == NULL)
6029 			goto out_ok;
6030 		ret = -ERANGE;
6031 		goto out_free;
6032 	}
6033 	nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len,
6034 			      type);
6035 	if (buf) {
6036 		if (res.acl_len > buflen) {
6037 			ret = -ERANGE;
6038 			goto out_free;
6039 		}
6040 		_copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
6041 	}
6042 out_ok:
6043 	ret = res.acl_len;
6044 out_free:
6045 	while (--i >= 0)
6046 		__free_page(pages[i]);
6047 	if (res.acl_scratch)
6048 		__free_page(res.acl_scratch);
6049 	kfree(pages);
6050 	return ret;
6051 }
6052 
6053 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf,
6054 				     size_t buflen, enum nfs4_acl_type type)
6055 {
6056 	struct nfs4_exception exception = {
6057 		.interruptible = true,
6058 	};
6059 	ssize_t ret;
6060 	do {
6061 		ret = __nfs4_get_acl_uncached(inode, buf, buflen, type);
6062 		trace_nfs4_get_acl(inode, ret);
6063 		if (ret >= 0)
6064 			break;
6065 		ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
6066 	} while (exception.retry);
6067 	return ret;
6068 }
6069 
6070 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen,
6071 				 enum nfs4_acl_type type)
6072 {
6073 	struct nfs_server *server = NFS_SERVER(inode);
6074 	int ret;
6075 
6076 	if (!nfs4_server_supports_acls(server, type))
6077 		return -EOPNOTSUPP;
6078 	ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
6079 	if (ret < 0)
6080 		return ret;
6081 	if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
6082 		nfs_zap_acl_cache(inode);
6083 	ret = nfs4_read_cached_acl(inode, buf, buflen, type);
6084 	if (ret != -ENOENT)
6085 		/* -ENOENT is returned if there is no ACL or if there is an ACL
6086 		 * but no cached acl data, just the acl length */
6087 		return ret;
6088 	return nfs4_get_acl_uncached(inode, buf, buflen, type);
6089 }
6090 
6091 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf,
6092 			       size_t buflen, enum nfs4_acl_type type)
6093 {
6094 	struct nfs_server *server = NFS_SERVER(inode);
6095 	struct page *pages[NFS4ACL_MAXPAGES];
6096 	struct nfs_setaclargs arg = {
6097 		.fh = NFS_FH(inode),
6098 		.acl_type = type,
6099 		.acl_len = buflen,
6100 		.acl_pages = pages,
6101 	};
6102 	struct nfs_setaclres res;
6103 	struct rpc_message msg = {
6104 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_SETACL],
6105 		.rpc_argp	= &arg,
6106 		.rpc_resp	= &res,
6107 	};
6108 	unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
6109 	int ret, i;
6110 
6111 	/* You can't remove system.nfs4_acl: */
6112 	if (buflen == 0)
6113 		return -EINVAL;
6114 	if (!nfs4_server_supports_acls(server, type))
6115 		return -EOPNOTSUPP;
6116 	if (npages > ARRAY_SIZE(pages))
6117 		return -ERANGE;
6118 	i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
6119 	if (i < 0)
6120 		return i;
6121 	nfs4_inode_make_writeable(inode);
6122 	ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6123 
6124 	/*
6125 	 * Free each page after tx, so the only ref left is
6126 	 * held by the network stack
6127 	 */
6128 	for (; i > 0; i--)
6129 		put_page(pages[i-1]);
6130 
6131 	/*
6132 	 * Acl update can result in inode attribute update.
6133 	 * so mark the attribute cache invalid.
6134 	 */
6135 	spin_lock(&inode->i_lock);
6136 	nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
6137 					     NFS_INO_INVALID_CTIME |
6138 					     NFS_INO_REVAL_FORCED);
6139 	spin_unlock(&inode->i_lock);
6140 	nfs_access_zap_cache(inode);
6141 	nfs_zap_acl_cache(inode);
6142 	return ret;
6143 }
6144 
6145 static int nfs4_proc_set_acl(struct inode *inode, const void *buf,
6146 			     size_t buflen, enum nfs4_acl_type type)
6147 {
6148 	struct nfs4_exception exception = { };
6149 	int err;
6150 	do {
6151 		err = __nfs4_proc_set_acl(inode, buf, buflen, type);
6152 		trace_nfs4_set_acl(inode, err);
6153 		if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
6154 			/*
6155 			 * no need to retry since the kernel
6156 			 * isn't involved in encoding the ACEs.
6157 			 */
6158 			err = -EINVAL;
6159 			break;
6160 		}
6161 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
6162 				&exception);
6163 	} while (exception.retry);
6164 	return err;
6165 }
6166 
6167 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6168 static int _nfs4_get_security_label(struct inode *inode, void *buf,
6169 					size_t buflen)
6170 {
6171 	struct nfs_server *server = NFS_SERVER(inode);
6172 	struct nfs4_label label = {0, 0, buflen, buf};
6173 
6174 	u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6175 	struct nfs_fattr fattr = {
6176 		.label = &label,
6177 	};
6178 	struct nfs4_getattr_arg arg = {
6179 		.fh		= NFS_FH(inode),
6180 		.bitmask	= bitmask,
6181 	};
6182 	struct nfs4_getattr_res res = {
6183 		.fattr		= &fattr,
6184 		.server		= server,
6185 	};
6186 	struct rpc_message msg = {
6187 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
6188 		.rpc_argp	= &arg,
6189 		.rpc_resp	= &res,
6190 	};
6191 	int ret;
6192 
6193 	nfs_fattr_init(&fattr);
6194 
6195 	ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
6196 	if (ret)
6197 		return ret;
6198 	if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6199 		return -ENOENT;
6200 	return label.len;
6201 }
6202 
6203 static int nfs4_get_security_label(struct inode *inode, void *buf,
6204 					size_t buflen)
6205 {
6206 	struct nfs4_exception exception = {
6207 		.interruptible = true,
6208 	};
6209 	int err;
6210 
6211 	if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6212 		return -EOPNOTSUPP;
6213 
6214 	do {
6215 		err = _nfs4_get_security_label(inode, buf, buflen);
6216 		trace_nfs4_get_security_label(inode, err);
6217 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
6218 				&exception);
6219 	} while (exception.retry);
6220 	return err;
6221 }
6222 
6223 static int _nfs4_do_set_security_label(struct inode *inode,
6224 		struct nfs4_label *ilabel,
6225 		struct nfs_fattr *fattr)
6226 {
6227 
6228 	struct iattr sattr = {0};
6229 	struct nfs_server *server = NFS_SERVER(inode);
6230 	const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6231 	struct nfs_setattrargs arg = {
6232 		.fh		= NFS_FH(inode),
6233 		.iap		= &sattr,
6234 		.server		= server,
6235 		.bitmask	= bitmask,
6236 		.label		= ilabel,
6237 	};
6238 	struct nfs_setattrres res = {
6239 		.fattr		= fattr,
6240 		.server		= server,
6241 	};
6242 	struct rpc_message msg = {
6243 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6244 		.rpc_argp	= &arg,
6245 		.rpc_resp	= &res,
6246 	};
6247 	int status;
6248 
6249 	nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6250 
6251 	status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6252 	if (status)
6253 		dprintk("%s failed: %d\n", __func__, status);
6254 
6255 	return status;
6256 }
6257 
6258 static int nfs4_do_set_security_label(struct inode *inode,
6259 		struct nfs4_label *ilabel,
6260 		struct nfs_fattr *fattr)
6261 {
6262 	struct nfs4_exception exception = { };
6263 	int err;
6264 
6265 	do {
6266 		err = _nfs4_do_set_security_label(inode, ilabel, fattr);
6267 		trace_nfs4_set_security_label(inode, err);
6268 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
6269 				&exception);
6270 	} while (exception.retry);
6271 	return err;
6272 }
6273 
6274 static int
6275 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6276 {
6277 	struct nfs4_label ilabel = {0, 0, buflen, (char *)buf };
6278 	struct nfs_fattr *fattr;
6279 	int status;
6280 
6281 	if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6282 		return -EOPNOTSUPP;
6283 
6284 	fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
6285 	if (fattr == NULL)
6286 		return -ENOMEM;
6287 
6288 	status = nfs4_do_set_security_label(inode, &ilabel, fattr);
6289 	if (status == 0)
6290 		nfs_setsecurity(inode, fattr);
6291 
6292 	nfs_free_fattr(fattr);
6293 	return status;
6294 }
6295 #endif	/* CONFIG_NFS_V4_SECURITY_LABEL */
6296 
6297 
6298 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6299 				    nfs4_verifier *bootverf)
6300 {
6301 	__be32 verf[2];
6302 
6303 	if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6304 		/* An impossible timestamp guarantees this value
6305 		 * will never match a generated boot time. */
6306 		verf[0] = cpu_to_be32(U32_MAX);
6307 		verf[1] = cpu_to_be32(U32_MAX);
6308 	} else {
6309 		struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6310 		u64 ns = ktime_to_ns(nn->boot_time);
6311 
6312 		verf[0] = cpu_to_be32(ns >> 32);
6313 		verf[1] = cpu_to_be32(ns);
6314 	}
6315 	memcpy(bootverf->data, verf, sizeof(bootverf->data));
6316 }
6317 
6318 static size_t
6319 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6320 {
6321 	struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6322 	struct nfs_netns_client *nn_clp = nn->nfs_client;
6323 	const char *id;
6324 
6325 	buf[0] = '\0';
6326 
6327 	if (nn_clp) {
6328 		rcu_read_lock();
6329 		id = rcu_dereference(nn_clp->identifier);
6330 		if (id)
6331 			strscpy(buf, id, buflen);
6332 		rcu_read_unlock();
6333 	}
6334 
6335 	if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6336 		strscpy(buf, nfs4_client_id_uniquifier, buflen);
6337 
6338 	return strlen(buf);
6339 }
6340 
6341 static int
6342 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6343 {
6344 	char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6345 	size_t buflen;
6346 	size_t len;
6347 	char *str;
6348 
6349 	if (clp->cl_owner_id != NULL)
6350 		return 0;
6351 
6352 	rcu_read_lock();
6353 	len = 14 +
6354 		strlen(clp->cl_rpcclient->cl_nodename) +
6355 		1 +
6356 		strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6357 		1;
6358 	rcu_read_unlock();
6359 
6360 	buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6361 	if (buflen)
6362 		len += buflen + 1;
6363 
6364 	if (len > NFS4_OPAQUE_LIMIT + 1)
6365 		return -EINVAL;
6366 
6367 	/*
6368 	 * Since this string is allocated at mount time, and held until the
6369 	 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6370 	 * about a memory-reclaim deadlock.
6371 	 */
6372 	str = kmalloc(len, GFP_KERNEL);
6373 	if (!str)
6374 		return -ENOMEM;
6375 
6376 	rcu_read_lock();
6377 	if (buflen)
6378 		scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6379 			  clp->cl_rpcclient->cl_nodename, buf,
6380 			  rpc_peeraddr2str(clp->cl_rpcclient,
6381 					   RPC_DISPLAY_ADDR));
6382 	else
6383 		scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6384 			  clp->cl_rpcclient->cl_nodename,
6385 			  rpc_peeraddr2str(clp->cl_rpcclient,
6386 					   RPC_DISPLAY_ADDR));
6387 	rcu_read_unlock();
6388 
6389 	clp->cl_owner_id = str;
6390 	return 0;
6391 }
6392 
6393 static int
6394 nfs4_init_uniform_client_string(struct nfs_client *clp)
6395 {
6396 	char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6397 	size_t buflen;
6398 	size_t len;
6399 	char *str;
6400 
6401 	if (clp->cl_owner_id != NULL)
6402 		return 0;
6403 
6404 	len = 10 + 10 + 1 + 10 + 1 +
6405 		strlen(clp->cl_rpcclient->cl_nodename) + 1;
6406 
6407 	buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6408 	if (buflen)
6409 		len += buflen + 1;
6410 
6411 	if (len > NFS4_OPAQUE_LIMIT + 1)
6412 		return -EINVAL;
6413 
6414 	/*
6415 	 * Since this string is allocated at mount time, and held until the
6416 	 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6417 	 * about a memory-reclaim deadlock.
6418 	 */
6419 	str = kmalloc(len, GFP_KERNEL);
6420 	if (!str)
6421 		return -ENOMEM;
6422 
6423 	if (buflen)
6424 		scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6425 			  clp->rpc_ops->version, clp->cl_minorversion,
6426 			  buf, clp->cl_rpcclient->cl_nodename);
6427 	else
6428 		scnprintf(str, len, "Linux NFSv%u.%u %s",
6429 			  clp->rpc_ops->version, clp->cl_minorversion,
6430 			  clp->cl_rpcclient->cl_nodename);
6431 	clp->cl_owner_id = str;
6432 	return 0;
6433 }
6434 
6435 /*
6436  * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6437  * services.  Advertise one based on the address family of the
6438  * clientaddr.
6439  */
6440 static unsigned int
6441 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6442 {
6443 	if (strchr(clp->cl_ipaddr, ':') != NULL)
6444 		return scnprintf(buf, len, "tcp6");
6445 	else
6446 		return scnprintf(buf, len, "tcp");
6447 }
6448 
6449 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6450 {
6451 	struct nfs4_setclientid *sc = calldata;
6452 
6453 	if (task->tk_status == 0)
6454 		sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6455 }
6456 
6457 static const struct rpc_call_ops nfs4_setclientid_ops = {
6458 	.rpc_call_done = nfs4_setclientid_done,
6459 };
6460 
6461 /**
6462  * nfs4_proc_setclientid - Negotiate client ID
6463  * @clp: state data structure
6464  * @program: RPC program for NFSv4 callback service
6465  * @port: IP port number for NFS4 callback service
6466  * @cred: credential to use for this call
6467  * @res: where to place the result
6468  *
6469  * Returns zero, a negative errno, or a negative NFS4ERR status code.
6470  */
6471 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6472 		unsigned short port, const struct cred *cred,
6473 		struct nfs4_setclientid_res *res)
6474 {
6475 	nfs4_verifier sc_verifier;
6476 	struct nfs4_setclientid setclientid = {
6477 		.sc_verifier = &sc_verifier,
6478 		.sc_prog = program,
6479 		.sc_clnt = clp,
6480 	};
6481 	struct rpc_message msg = {
6482 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6483 		.rpc_argp = &setclientid,
6484 		.rpc_resp = res,
6485 		.rpc_cred = cred,
6486 	};
6487 	struct rpc_task_setup task_setup_data = {
6488 		.rpc_client = clp->cl_rpcclient,
6489 		.rpc_message = &msg,
6490 		.callback_ops = &nfs4_setclientid_ops,
6491 		.callback_data = &setclientid,
6492 		.flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6493 	};
6494 	unsigned long now = jiffies;
6495 	int status;
6496 
6497 	/* nfs_client_id4 */
6498 	nfs4_init_boot_verifier(clp, &sc_verifier);
6499 
6500 	if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6501 		status = nfs4_init_uniform_client_string(clp);
6502 	else
6503 		status = nfs4_init_nonuniform_client_string(clp);
6504 
6505 	if (status)
6506 		goto out;
6507 
6508 	/* cb_client4 */
6509 	setclientid.sc_netid_len =
6510 				nfs4_init_callback_netid(clp,
6511 						setclientid.sc_netid,
6512 						sizeof(setclientid.sc_netid));
6513 	setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6514 				sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6515 				clp->cl_ipaddr, port >> 8, port & 255);
6516 
6517 	dprintk("NFS call  setclientid auth=%s, '%s'\n",
6518 		clp->cl_rpcclient->cl_auth->au_ops->au_name,
6519 		clp->cl_owner_id);
6520 
6521 	status = nfs4_call_sync_custom(&task_setup_data);
6522 	if (setclientid.sc_cred) {
6523 		kfree(clp->cl_acceptor);
6524 		clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6525 		put_rpccred(setclientid.sc_cred);
6526 	}
6527 
6528 	if (status == 0)
6529 		do_renew_lease(clp, now);
6530 out:
6531 	trace_nfs4_setclientid(clp, status);
6532 	dprintk("NFS reply setclientid: %d\n", status);
6533 	return status;
6534 }
6535 
6536 /**
6537  * nfs4_proc_setclientid_confirm - Confirm client ID
6538  * @clp: state data structure
6539  * @arg: result of a previous SETCLIENTID
6540  * @cred: credential to use for this call
6541  *
6542  * Returns zero, a negative errno, or a negative NFS4ERR status code.
6543  */
6544 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6545 		struct nfs4_setclientid_res *arg,
6546 		const struct cred *cred)
6547 {
6548 	struct rpc_message msg = {
6549 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6550 		.rpc_argp = arg,
6551 		.rpc_cred = cred,
6552 	};
6553 	int status;
6554 
6555 	dprintk("NFS call  setclientid_confirm auth=%s, (client ID %llx)\n",
6556 		clp->cl_rpcclient->cl_auth->au_ops->au_name,
6557 		clp->cl_clientid);
6558 	status = rpc_call_sync(clp->cl_rpcclient, &msg,
6559 			       RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6560 	trace_nfs4_setclientid_confirm(clp, status);
6561 	dprintk("NFS reply setclientid_confirm: %d\n", status);
6562 	return status;
6563 }
6564 
6565 struct nfs4_delegreturndata {
6566 	struct nfs4_delegreturnargs args;
6567 	struct nfs4_delegreturnres res;
6568 	struct nfs_fh fh;
6569 	nfs4_stateid stateid;
6570 	unsigned long timestamp;
6571 	struct {
6572 		struct nfs4_layoutreturn_args arg;
6573 		struct nfs4_layoutreturn_res res;
6574 		struct nfs4_xdr_opaque_data ld_private;
6575 		u32 roc_barrier;
6576 		bool roc;
6577 	} lr;
6578 	struct nfs_fattr fattr;
6579 	int rpc_status;
6580 	struct inode *inode;
6581 };
6582 
6583 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6584 {
6585 	struct nfs4_delegreturndata *data = calldata;
6586 	struct nfs4_exception exception = {
6587 		.inode = data->inode,
6588 		.stateid = &data->stateid,
6589 		.task_is_privileged = data->args.seq_args.sa_privileged,
6590 	};
6591 
6592 	if (!nfs4_sequence_done(task, &data->res.seq_res))
6593 		return;
6594 
6595 	trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6596 
6597 	/* Handle Layoutreturn errors */
6598 	if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6599 			  &data->res.lr_ret) == -EAGAIN)
6600 		goto out_restart;
6601 
6602 	switch (task->tk_status) {
6603 	case 0:
6604 		renew_lease(data->res.server, data->timestamp);
6605 		break;
6606 	case -NFS4ERR_ADMIN_REVOKED:
6607 	case -NFS4ERR_DELEG_REVOKED:
6608 	case -NFS4ERR_EXPIRED:
6609 		nfs4_free_revoked_stateid(data->res.server,
6610 				data->args.stateid,
6611 				task->tk_msg.rpc_cred);
6612 		fallthrough;
6613 	case -NFS4ERR_BAD_STATEID:
6614 	case -NFS4ERR_STALE_STATEID:
6615 	case -ETIMEDOUT:
6616 		task->tk_status = 0;
6617 		break;
6618 	case -NFS4ERR_OLD_STATEID:
6619 		if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6620 			nfs4_stateid_seqid_inc(&data->stateid);
6621 		if (data->args.bitmask) {
6622 			data->args.bitmask = NULL;
6623 			data->res.fattr = NULL;
6624 		}
6625 		goto out_restart;
6626 	case -NFS4ERR_ACCESS:
6627 		if (data->args.bitmask) {
6628 			data->args.bitmask = NULL;
6629 			data->res.fattr = NULL;
6630 			goto out_restart;
6631 		}
6632 		fallthrough;
6633 	default:
6634 		task->tk_status = nfs4_async_handle_exception(task,
6635 				data->res.server, task->tk_status,
6636 				&exception);
6637 		if (exception.retry)
6638 			goto out_restart;
6639 	}
6640 	nfs_delegation_mark_returned(data->inode, data->args.stateid);
6641 	data->rpc_status = task->tk_status;
6642 	return;
6643 out_restart:
6644 	task->tk_status = 0;
6645 	rpc_restart_call_prepare(task);
6646 }
6647 
6648 static void nfs4_delegreturn_release(void *calldata)
6649 {
6650 	struct nfs4_delegreturndata *data = calldata;
6651 	struct inode *inode = data->inode;
6652 
6653 	if (data->lr.roc)
6654 		pnfs_roc_release(&data->lr.arg, &data->lr.res,
6655 				 data->res.lr_ret);
6656 	if (inode) {
6657 		nfs4_fattr_set_prechange(&data->fattr,
6658 					 inode_peek_iversion_raw(inode));
6659 		nfs_refresh_inode(inode, &data->fattr);
6660 		nfs_iput_and_deactive(inode);
6661 	}
6662 	kfree(calldata);
6663 }
6664 
6665 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6666 {
6667 	struct nfs4_delegreturndata *d_data;
6668 	struct pnfs_layout_hdr *lo;
6669 
6670 	d_data = data;
6671 
6672 	if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6673 		nfs4_sequence_done(task, &d_data->res.seq_res);
6674 		return;
6675 	}
6676 
6677 	lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6678 	if (lo && !pnfs_layout_is_valid(lo)) {
6679 		d_data->args.lr_args = NULL;
6680 		d_data->res.lr_res = NULL;
6681 	}
6682 
6683 	nfs4_setup_sequence(d_data->res.server->nfs_client,
6684 			&d_data->args.seq_args,
6685 			&d_data->res.seq_res,
6686 			task);
6687 }
6688 
6689 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6690 	.rpc_call_prepare = nfs4_delegreturn_prepare,
6691 	.rpc_call_done = nfs4_delegreturn_done,
6692 	.rpc_release = nfs4_delegreturn_release,
6693 };
6694 
6695 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6696 {
6697 	struct nfs4_delegreturndata *data;
6698 	struct nfs_server *server = NFS_SERVER(inode);
6699 	struct rpc_task *task;
6700 	struct rpc_message msg = {
6701 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6702 		.rpc_cred = cred,
6703 	};
6704 	struct rpc_task_setup task_setup_data = {
6705 		.rpc_client = server->client,
6706 		.rpc_message = &msg,
6707 		.callback_ops = &nfs4_delegreturn_ops,
6708 		.flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
6709 	};
6710 	int status = 0;
6711 
6712 	if (nfs_server_capable(inode, NFS_CAP_MOVEABLE))
6713 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
6714 
6715 	data = kzalloc(sizeof(*data), GFP_KERNEL);
6716 	if (data == NULL)
6717 		return -ENOMEM;
6718 
6719 	nfs4_state_protect(server->nfs_client,
6720 			NFS_SP4_MACH_CRED_CLEANUP,
6721 			&task_setup_data.rpc_client, &msg);
6722 
6723 	data->args.fhandle = &data->fh;
6724 	data->args.stateid = &data->stateid;
6725 	nfs4_bitmask_set(data->args.bitmask_store,
6726 			 server->cache_consistency_bitmask, inode, 0);
6727 	data->args.bitmask = data->args.bitmask_store;
6728 	nfs_copy_fh(&data->fh, NFS_FH(inode));
6729 	nfs4_stateid_copy(&data->stateid, stateid);
6730 	data->res.fattr = &data->fattr;
6731 	data->res.server = server;
6732 	data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6733 	data->lr.arg.ld_private = &data->lr.ld_private;
6734 	nfs_fattr_init(data->res.fattr);
6735 	data->timestamp = jiffies;
6736 	data->rpc_status = 0;
6737 	data->inode = nfs_igrab_and_active(inode);
6738 	if (data->inode || issync) {
6739 		data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6740 					cred);
6741 		if (data->lr.roc) {
6742 			data->args.lr_args = &data->lr.arg;
6743 			data->res.lr_res = &data->lr.res;
6744 		}
6745 	}
6746 
6747 	if (!data->inode)
6748 		nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6749 				   1);
6750 	else
6751 		nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6752 				   0);
6753 	task_setup_data.callback_data = data;
6754 	msg.rpc_argp = &data->args;
6755 	msg.rpc_resp = &data->res;
6756 	task = rpc_run_task(&task_setup_data);
6757 	if (IS_ERR(task))
6758 		return PTR_ERR(task);
6759 	if (!issync)
6760 		goto out;
6761 	status = rpc_wait_for_completion_task(task);
6762 	if (status != 0)
6763 		goto out;
6764 	status = data->rpc_status;
6765 out:
6766 	rpc_put_task(task);
6767 	return status;
6768 }
6769 
6770 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6771 {
6772 	struct nfs_server *server = NFS_SERVER(inode);
6773 	struct nfs4_exception exception = { };
6774 	int err;
6775 	do {
6776 		err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6777 		trace_nfs4_delegreturn(inode, stateid, err);
6778 		switch (err) {
6779 			case -NFS4ERR_STALE_STATEID:
6780 			case -NFS4ERR_EXPIRED:
6781 			case 0:
6782 				return 0;
6783 		}
6784 		err = nfs4_handle_exception(server, err, &exception);
6785 	} while (exception.retry);
6786 	return err;
6787 }
6788 
6789 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6790 {
6791 	struct inode *inode = state->inode;
6792 	struct nfs_server *server = NFS_SERVER(inode);
6793 	struct nfs_client *clp = server->nfs_client;
6794 	struct nfs_lockt_args arg = {
6795 		.fh = NFS_FH(inode),
6796 		.fl = request,
6797 	};
6798 	struct nfs_lockt_res res = {
6799 		.denied = request,
6800 	};
6801 	struct rpc_message msg = {
6802 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6803 		.rpc_argp	= &arg,
6804 		.rpc_resp	= &res,
6805 		.rpc_cred	= state->owner->so_cred,
6806 	};
6807 	struct nfs4_lock_state *lsp;
6808 	int status;
6809 
6810 	arg.lock_owner.clientid = clp->cl_clientid;
6811 	status = nfs4_set_lock_state(state, request);
6812 	if (status != 0)
6813 		goto out;
6814 	lsp = request->fl_u.nfs4_fl.owner;
6815 	arg.lock_owner.id = lsp->ls_seqid.owner_id;
6816 	arg.lock_owner.s_dev = server->s_dev;
6817 	status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6818 	switch (status) {
6819 		case 0:
6820 			request->c.flc_type = F_UNLCK;
6821 			break;
6822 		case -NFS4ERR_DENIED:
6823 			status = 0;
6824 	}
6825 	request->fl_ops->fl_release_private(request);
6826 	request->fl_ops = NULL;
6827 out:
6828 	return status;
6829 }
6830 
6831 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6832 {
6833 	struct nfs4_exception exception = {
6834 		.interruptible = true,
6835 	};
6836 	int err;
6837 
6838 	do {
6839 		err = _nfs4_proc_getlk(state, cmd, request);
6840 		trace_nfs4_get_lock(request, state, cmd, err);
6841 		err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6842 				&exception);
6843 	} while (exception.retry);
6844 	return err;
6845 }
6846 
6847 /*
6848  * Update the seqid of a lock stateid after receiving
6849  * NFS4ERR_OLD_STATEID
6850  */
6851 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6852 		struct nfs4_lock_state *lsp)
6853 {
6854 	struct nfs4_state *state = lsp->ls_state;
6855 	bool ret = false;
6856 
6857 	spin_lock(&state->state_lock);
6858 	if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6859 		goto out;
6860 	if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6861 		nfs4_stateid_seqid_inc(dst);
6862 	else
6863 		dst->seqid = lsp->ls_stateid.seqid;
6864 	ret = true;
6865 out:
6866 	spin_unlock(&state->state_lock);
6867 	return ret;
6868 }
6869 
6870 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6871 		struct nfs4_lock_state *lsp)
6872 {
6873 	struct nfs4_state *state = lsp->ls_state;
6874 	bool ret;
6875 
6876 	spin_lock(&state->state_lock);
6877 	ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6878 	nfs4_stateid_copy(dst, &lsp->ls_stateid);
6879 	spin_unlock(&state->state_lock);
6880 	return ret;
6881 }
6882 
6883 struct nfs4_unlockdata {
6884 	struct nfs_locku_args arg;
6885 	struct nfs_locku_res res;
6886 	struct nfs4_lock_state *lsp;
6887 	struct nfs_open_context *ctx;
6888 	struct nfs_lock_context *l_ctx;
6889 	struct file_lock fl;
6890 	struct nfs_server *server;
6891 	unsigned long timestamp;
6892 };
6893 
6894 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6895 		struct nfs_open_context *ctx,
6896 		struct nfs4_lock_state *lsp,
6897 		struct nfs_seqid *seqid)
6898 {
6899 	struct nfs4_unlockdata *p;
6900 	struct nfs4_state *state = lsp->ls_state;
6901 	struct inode *inode = state->inode;
6902 
6903 	p = kzalloc(sizeof(*p), GFP_KERNEL);
6904 	if (p == NULL)
6905 		return NULL;
6906 	p->arg.fh = NFS_FH(inode);
6907 	p->arg.fl = &p->fl;
6908 	p->arg.seqid = seqid;
6909 	p->res.seqid = seqid;
6910 	p->lsp = lsp;
6911 	/* Ensure we don't close file until we're done freeing locks! */
6912 	p->ctx = get_nfs_open_context(ctx);
6913 	p->l_ctx = nfs_get_lock_context(ctx);
6914 	locks_init_lock(&p->fl);
6915 	locks_copy_lock(&p->fl, fl);
6916 	p->server = NFS_SERVER(inode);
6917 	spin_lock(&state->state_lock);
6918 	nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6919 	spin_unlock(&state->state_lock);
6920 	return p;
6921 }
6922 
6923 static void nfs4_locku_release_calldata(void *data)
6924 {
6925 	struct nfs4_unlockdata *calldata = data;
6926 	nfs_free_seqid(calldata->arg.seqid);
6927 	nfs4_put_lock_state(calldata->lsp);
6928 	nfs_put_lock_context(calldata->l_ctx);
6929 	put_nfs_open_context(calldata->ctx);
6930 	kfree(calldata);
6931 }
6932 
6933 static void nfs4_locku_done(struct rpc_task *task, void *data)
6934 {
6935 	struct nfs4_unlockdata *calldata = data;
6936 	struct nfs4_exception exception = {
6937 		.inode = calldata->lsp->ls_state->inode,
6938 		.stateid = &calldata->arg.stateid,
6939 	};
6940 
6941 	if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6942 		return;
6943 	switch (task->tk_status) {
6944 		case 0:
6945 			renew_lease(calldata->server, calldata->timestamp);
6946 			locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6947 			if (nfs4_update_lock_stateid(calldata->lsp,
6948 					&calldata->res.stateid))
6949 				break;
6950 			fallthrough;
6951 		case -NFS4ERR_ADMIN_REVOKED:
6952 		case -NFS4ERR_EXPIRED:
6953 			nfs4_free_revoked_stateid(calldata->server,
6954 					&calldata->arg.stateid,
6955 					task->tk_msg.rpc_cred);
6956 			fallthrough;
6957 		case -NFS4ERR_BAD_STATEID:
6958 		case -NFS4ERR_STALE_STATEID:
6959 			if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6960 						calldata->lsp))
6961 				rpc_restart_call_prepare(task);
6962 			break;
6963 		case -NFS4ERR_OLD_STATEID:
6964 			if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6965 						calldata->lsp))
6966 				rpc_restart_call_prepare(task);
6967 			break;
6968 		default:
6969 			task->tk_status = nfs4_async_handle_exception(task,
6970 					calldata->server, task->tk_status,
6971 					&exception);
6972 			if (exception.retry)
6973 				rpc_restart_call_prepare(task);
6974 	}
6975 	nfs_release_seqid(calldata->arg.seqid);
6976 }
6977 
6978 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6979 {
6980 	struct nfs4_unlockdata *calldata = data;
6981 
6982 	if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6983 		nfs_async_iocounter_wait(task, calldata->l_ctx))
6984 		return;
6985 
6986 	if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6987 		goto out_wait;
6988 	if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6989 		/* Note: exit _without_ running nfs4_locku_done */
6990 		goto out_no_action;
6991 	}
6992 	calldata->timestamp = jiffies;
6993 	if (nfs4_setup_sequence(calldata->server->nfs_client,
6994 				&calldata->arg.seq_args,
6995 				&calldata->res.seq_res,
6996 				task) != 0)
6997 		nfs_release_seqid(calldata->arg.seqid);
6998 	return;
6999 out_no_action:
7000 	task->tk_action = NULL;
7001 out_wait:
7002 	nfs4_sequence_done(task, &calldata->res.seq_res);
7003 }
7004 
7005 static const struct rpc_call_ops nfs4_locku_ops = {
7006 	.rpc_call_prepare = nfs4_locku_prepare,
7007 	.rpc_call_done = nfs4_locku_done,
7008 	.rpc_release = nfs4_locku_release_calldata,
7009 };
7010 
7011 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
7012 		struct nfs_open_context *ctx,
7013 		struct nfs4_lock_state *lsp,
7014 		struct nfs_seqid *seqid)
7015 {
7016 	struct nfs4_unlockdata *data;
7017 	struct rpc_message msg = {
7018 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
7019 		.rpc_cred = ctx->cred,
7020 	};
7021 	struct rpc_task_setup task_setup_data = {
7022 		.rpc_client = NFS_CLIENT(lsp->ls_state->inode),
7023 		.rpc_message = &msg,
7024 		.callback_ops = &nfs4_locku_ops,
7025 		.workqueue = nfsiod_workqueue,
7026 		.flags = RPC_TASK_ASYNC,
7027 	};
7028 
7029 	if (nfs_server_capable(lsp->ls_state->inode, NFS_CAP_MOVEABLE))
7030 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
7031 
7032 	nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
7033 		NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
7034 
7035 	/* Ensure this is an unlock - when canceling a lock, the
7036 	 * canceled lock is passed in, and it won't be an unlock.
7037 	 */
7038 	fl->c.flc_type = F_UNLCK;
7039 	if (fl->c.flc_flags & FL_CLOSE)
7040 		set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
7041 
7042 	data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
7043 	if (data == NULL) {
7044 		nfs_free_seqid(seqid);
7045 		return ERR_PTR(-ENOMEM);
7046 	}
7047 
7048 	nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
7049 	msg.rpc_argp = &data->arg;
7050 	msg.rpc_resp = &data->res;
7051 	task_setup_data.callback_data = data;
7052 	return rpc_run_task(&task_setup_data);
7053 }
7054 
7055 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
7056 {
7057 	struct inode *inode = state->inode;
7058 	struct nfs4_state_owner *sp = state->owner;
7059 	struct nfs_inode *nfsi = NFS_I(inode);
7060 	struct nfs_seqid *seqid;
7061 	struct nfs4_lock_state *lsp;
7062 	struct rpc_task *task;
7063 	struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7064 	int status = 0;
7065 	unsigned char saved_flags = request->c.flc_flags;
7066 
7067 	status = nfs4_set_lock_state(state, request);
7068 	/* Unlock _before_ we do the RPC call */
7069 	request->c.flc_flags |= FL_EXISTS;
7070 	/* Exclude nfs_delegation_claim_locks() */
7071 	mutex_lock(&sp->so_delegreturn_mutex);
7072 	/* Exclude nfs4_reclaim_open_stateid() - note nesting! */
7073 	down_read(&nfsi->rwsem);
7074 	if (locks_lock_inode_wait(inode, request) == -ENOENT) {
7075 		up_read(&nfsi->rwsem);
7076 		mutex_unlock(&sp->so_delegreturn_mutex);
7077 		goto out;
7078 	}
7079 	lsp = request->fl_u.nfs4_fl.owner;
7080 	set_bit(NFS_LOCK_UNLOCKING, &lsp->ls_flags);
7081 	up_read(&nfsi->rwsem);
7082 	mutex_unlock(&sp->so_delegreturn_mutex);
7083 	if (status != 0)
7084 		goto out;
7085 	/* Is this a delegated lock? */
7086 	if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
7087 		goto out;
7088 	alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
7089 	seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
7090 	status = -ENOMEM;
7091 	if (IS_ERR(seqid))
7092 		goto out;
7093 	task = nfs4_do_unlck(request,
7094 			     nfs_file_open_context(request->c.flc_file),
7095 			     lsp, seqid);
7096 	status = PTR_ERR(task);
7097 	if (IS_ERR(task))
7098 		goto out;
7099 	status = rpc_wait_for_completion_task(task);
7100 	rpc_put_task(task);
7101 out:
7102 	request->c.flc_flags = saved_flags;
7103 	trace_nfs4_unlock(request, state, F_SETLK, status);
7104 	return status;
7105 }
7106 
7107 struct nfs4_lockdata {
7108 	struct nfs_lock_args arg;
7109 	struct nfs_lock_res res;
7110 	struct nfs4_lock_state *lsp;
7111 	struct nfs_open_context *ctx;
7112 	struct file_lock fl;
7113 	unsigned long timestamp;
7114 	int rpc_status;
7115 	int cancelled;
7116 	struct nfs_server *server;
7117 };
7118 
7119 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
7120 		struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
7121 		gfp_t gfp_mask)
7122 {
7123 	struct nfs4_lockdata *p;
7124 	struct inode *inode = lsp->ls_state->inode;
7125 	struct nfs_server *server = NFS_SERVER(inode);
7126 	struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7127 
7128 	p = kzalloc(sizeof(*p), gfp_mask);
7129 	if (p == NULL)
7130 		return NULL;
7131 
7132 	p->arg.fh = NFS_FH(inode);
7133 	p->arg.fl = &p->fl;
7134 	p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
7135 	if (IS_ERR(p->arg.open_seqid))
7136 		goto out_free;
7137 	alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
7138 	p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
7139 	if (IS_ERR(p->arg.lock_seqid))
7140 		goto out_free_seqid;
7141 	p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
7142 	p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
7143 	p->arg.lock_owner.s_dev = server->s_dev;
7144 	p->res.lock_seqid = p->arg.lock_seqid;
7145 	p->lsp = lsp;
7146 	p->server = server;
7147 	p->ctx = get_nfs_open_context(ctx);
7148 	locks_init_lock(&p->fl);
7149 	locks_copy_lock(&p->fl, fl);
7150 	return p;
7151 out_free_seqid:
7152 	nfs_free_seqid(p->arg.open_seqid);
7153 out_free:
7154 	kfree(p);
7155 	return NULL;
7156 }
7157 
7158 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
7159 {
7160 	struct nfs4_lockdata *data = calldata;
7161 	struct nfs4_state *state = data->lsp->ls_state;
7162 
7163 	if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
7164 		goto out_wait;
7165 	/* Do we need to do an open_to_lock_owner? */
7166 	if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
7167 		if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
7168 			goto out_release_lock_seqid;
7169 		}
7170 		nfs4_stateid_copy(&data->arg.open_stateid,
7171 				&state->open_stateid);
7172 		data->arg.new_lock_owner = 1;
7173 		data->res.open_seqid = data->arg.open_seqid;
7174 	} else {
7175 		data->arg.new_lock_owner = 0;
7176 		nfs4_stateid_copy(&data->arg.lock_stateid,
7177 				&data->lsp->ls_stateid);
7178 	}
7179 	if (!nfs4_valid_open_stateid(state)) {
7180 		data->rpc_status = -EBADF;
7181 		task->tk_action = NULL;
7182 		goto out_release_open_seqid;
7183 	}
7184 	data->timestamp = jiffies;
7185 	if (nfs4_setup_sequence(data->server->nfs_client,
7186 				&data->arg.seq_args,
7187 				&data->res.seq_res,
7188 				task) == 0)
7189 		return;
7190 out_release_open_seqid:
7191 	nfs_release_seqid(data->arg.open_seqid);
7192 out_release_lock_seqid:
7193 	nfs_release_seqid(data->arg.lock_seqid);
7194 out_wait:
7195 	nfs4_sequence_done(task, &data->res.seq_res);
7196 	dprintk("%s: ret = %d\n", __func__, data->rpc_status);
7197 }
7198 
7199 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7200 {
7201 	struct nfs4_lockdata *data = calldata;
7202 	struct nfs4_lock_state *lsp = data->lsp;
7203 
7204 	if (!nfs4_sequence_done(task, &data->res.seq_res))
7205 		return;
7206 
7207 	data->rpc_status = task->tk_status;
7208 	switch (task->tk_status) {
7209 	case 0:
7210 		renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
7211 				data->timestamp);
7212 		if (data->arg.new_lock && !data->cancelled) {
7213 			data->fl.c.flc_flags &= ~(FL_SLEEP | FL_ACCESS);
7214 			if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
7215 				goto out_restart;
7216 		}
7217 		if (data->arg.new_lock_owner != 0) {
7218 			nfs_confirm_seqid(&lsp->ls_seqid, 0);
7219 			nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7220 			set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
7221 		} else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7222 			goto out_restart;
7223 		break;
7224 	case -NFS4ERR_OLD_STATEID:
7225 		if (data->arg.new_lock_owner != 0 &&
7226 			nfs4_refresh_open_old_stateid(&data->arg.open_stateid,
7227 					lsp->ls_state))
7228 			goto out_restart;
7229 		if (nfs4_refresh_lock_old_stateid(&data->arg.lock_stateid, lsp))
7230 			goto out_restart;
7231 		fallthrough;
7232 	case -NFS4ERR_BAD_STATEID:
7233 	case -NFS4ERR_STALE_STATEID:
7234 	case -NFS4ERR_EXPIRED:
7235 		if (data->arg.new_lock_owner != 0) {
7236 			if (!nfs4_stateid_match(&data->arg.open_stateid,
7237 						&lsp->ls_state->open_stateid))
7238 				goto out_restart;
7239 		} else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7240 						&lsp->ls_stateid))
7241 				goto out_restart;
7242 	}
7243 out_done:
7244 	dprintk("%s: ret = %d!\n", __func__, data->rpc_status);
7245 	return;
7246 out_restart:
7247 	if (!data->cancelled)
7248 		rpc_restart_call_prepare(task);
7249 	goto out_done;
7250 }
7251 
7252 static void nfs4_lock_release(void *calldata)
7253 {
7254 	struct nfs4_lockdata *data = calldata;
7255 
7256 	nfs_free_seqid(data->arg.open_seqid);
7257 	if (data->cancelled && data->rpc_status == 0) {
7258 		struct rpc_task *task;
7259 		task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7260 				data->arg.lock_seqid);
7261 		if (!IS_ERR(task))
7262 			rpc_put_task_async(task);
7263 		dprintk("%s: cancelling lock!\n", __func__);
7264 	} else
7265 		nfs_free_seqid(data->arg.lock_seqid);
7266 	nfs4_put_lock_state(data->lsp);
7267 	put_nfs_open_context(data->ctx);
7268 	kfree(data);
7269 }
7270 
7271 static const struct rpc_call_ops nfs4_lock_ops = {
7272 	.rpc_call_prepare = nfs4_lock_prepare,
7273 	.rpc_call_done = nfs4_lock_done,
7274 	.rpc_release = nfs4_lock_release,
7275 };
7276 
7277 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7278 {
7279 	switch (error) {
7280 	case -NFS4ERR_ADMIN_REVOKED:
7281 	case -NFS4ERR_EXPIRED:
7282 	case -NFS4ERR_BAD_STATEID:
7283 		lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7284 		if (new_lock_owner != 0 ||
7285 		   test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7286 			nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7287 		break;
7288 	case -NFS4ERR_STALE_STATEID:
7289 		lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7290 		nfs4_schedule_lease_recovery(server->nfs_client);
7291 	}
7292 }
7293 
7294 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7295 {
7296 	struct nfs4_lockdata *data;
7297 	struct rpc_task *task;
7298 	struct rpc_message msg = {
7299 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7300 		.rpc_cred = state->owner->so_cred,
7301 	};
7302 	struct rpc_task_setup task_setup_data = {
7303 		.rpc_client = NFS_CLIENT(state->inode),
7304 		.rpc_message = &msg,
7305 		.callback_ops = &nfs4_lock_ops,
7306 		.workqueue = nfsiod_workqueue,
7307 		.flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7308 	};
7309 	int ret;
7310 
7311 	if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
7312 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
7313 
7314 	data = nfs4_alloc_lockdata(fl,
7315 				   nfs_file_open_context(fl->c.flc_file),
7316 				   fl->fl_u.nfs4_fl.owner, GFP_KERNEL);
7317 	if (data == NULL)
7318 		return -ENOMEM;
7319 	if (IS_SETLKW(cmd))
7320 		data->arg.block = 1;
7321 	nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7322 				recovery_type > NFS_LOCK_NEW);
7323 	msg.rpc_argp = &data->arg;
7324 	msg.rpc_resp = &data->res;
7325 	task_setup_data.callback_data = data;
7326 	if (recovery_type > NFS_LOCK_NEW) {
7327 		if (recovery_type == NFS_LOCK_RECLAIM)
7328 			data->arg.reclaim = NFS_LOCK_RECLAIM;
7329 	} else
7330 		data->arg.new_lock = 1;
7331 	task = rpc_run_task(&task_setup_data);
7332 	if (IS_ERR(task))
7333 		return PTR_ERR(task);
7334 	ret = rpc_wait_for_completion_task(task);
7335 	if (ret == 0) {
7336 		ret = data->rpc_status;
7337 		if (ret)
7338 			nfs4_handle_setlk_error(data->server, data->lsp,
7339 					data->arg.new_lock_owner, ret);
7340 	} else
7341 		data->cancelled = true;
7342 	trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7343 	rpc_put_task(task);
7344 	dprintk("%s: ret = %d\n", __func__, ret);
7345 	return ret;
7346 }
7347 
7348 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7349 {
7350 	struct nfs_server *server = NFS_SERVER(state->inode);
7351 	struct nfs4_exception exception = {
7352 		.inode = state->inode,
7353 	};
7354 	int err;
7355 
7356 	do {
7357 		/* Cache the lock if possible... */
7358 		if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7359 			return 0;
7360 		err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7361 		if (err != -NFS4ERR_DELAY)
7362 			break;
7363 		nfs4_handle_exception(server, err, &exception);
7364 	} while (exception.retry);
7365 	return err;
7366 }
7367 
7368 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7369 {
7370 	struct nfs_server *server = NFS_SERVER(state->inode);
7371 	struct nfs4_exception exception = {
7372 		.inode = state->inode,
7373 	};
7374 	int err;
7375 
7376 	err = nfs4_set_lock_state(state, request);
7377 	if (err != 0)
7378 		return err;
7379 	if (!recover_lost_locks) {
7380 		set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7381 		return 0;
7382 	}
7383 	do {
7384 		if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7385 			return 0;
7386 		err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7387 		switch (err) {
7388 		default:
7389 			goto out;
7390 		case -NFS4ERR_GRACE:
7391 		case -NFS4ERR_DELAY:
7392 			nfs4_handle_exception(server, err, &exception);
7393 			err = 0;
7394 		}
7395 	} while (exception.retry);
7396 out:
7397 	return err;
7398 }
7399 
7400 #if defined(CONFIG_NFS_V4_1)
7401 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7402 {
7403 	struct nfs4_lock_state *lsp;
7404 	int status;
7405 
7406 	status = nfs4_set_lock_state(state, request);
7407 	if (status != 0)
7408 		return status;
7409 	lsp = request->fl_u.nfs4_fl.owner;
7410 	if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7411 	    test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7412 		return 0;
7413 	return nfs4_lock_expired(state, request);
7414 }
7415 #endif
7416 
7417 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7418 {
7419 	struct nfs_inode *nfsi = NFS_I(state->inode);
7420 	struct nfs4_state_owner *sp = state->owner;
7421 	unsigned char flags = request->c.flc_flags;
7422 	int status;
7423 
7424 	request->c.flc_flags |= FL_ACCESS;
7425 	status = locks_lock_inode_wait(state->inode, request);
7426 	if (status < 0)
7427 		goto out;
7428 	mutex_lock(&sp->so_delegreturn_mutex);
7429 	down_read(&nfsi->rwsem);
7430 	if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7431 		/* Yes: cache locks! */
7432 		/* ...but avoid races with delegation recall... */
7433 		request->c.flc_flags = flags & ~FL_SLEEP;
7434 		status = locks_lock_inode_wait(state->inode, request);
7435 		up_read(&nfsi->rwsem);
7436 		mutex_unlock(&sp->so_delegreturn_mutex);
7437 		goto out;
7438 	}
7439 	up_read(&nfsi->rwsem);
7440 	mutex_unlock(&sp->so_delegreturn_mutex);
7441 	status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7442 out:
7443 	request->c.flc_flags = flags;
7444 	return status;
7445 }
7446 
7447 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7448 {
7449 	struct nfs4_exception exception = {
7450 		.state = state,
7451 		.inode = state->inode,
7452 		.interruptible = true,
7453 	};
7454 	int err;
7455 
7456 	do {
7457 		err = _nfs4_proc_setlk(state, cmd, request);
7458 		if (err == -NFS4ERR_DENIED)
7459 			err = -EAGAIN;
7460 		err = nfs4_handle_exception(NFS_SERVER(state->inode),
7461 				err, &exception);
7462 	} while (exception.retry);
7463 	return err;
7464 }
7465 
7466 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7467 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7468 
7469 static int
7470 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7471 			struct file_lock *request)
7472 {
7473 	int		status = -ERESTARTSYS;
7474 	unsigned long	timeout = NFS4_LOCK_MINTIMEOUT;
7475 
7476 	while(!signalled()) {
7477 		status = nfs4_proc_setlk(state, cmd, request);
7478 		if ((status != -EAGAIN) || IS_SETLK(cmd))
7479 			break;
7480 		__set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
7481 		schedule_timeout(timeout);
7482 		timeout *= 2;
7483 		timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7484 		status = -ERESTARTSYS;
7485 	}
7486 	return status;
7487 }
7488 
7489 #ifdef CONFIG_NFS_V4_1
7490 struct nfs4_lock_waiter {
7491 	struct inode		*inode;
7492 	struct nfs_lowner	owner;
7493 	wait_queue_entry_t	wait;
7494 };
7495 
7496 static int
7497 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7498 {
7499 	struct nfs4_lock_waiter	*waiter	=
7500 		container_of(wait, struct nfs4_lock_waiter, wait);
7501 
7502 	/* NULL key means to wake up everyone */
7503 	if (key) {
7504 		struct cb_notify_lock_args	*cbnl = key;
7505 		struct nfs_lowner		*lowner = &cbnl->cbnl_owner,
7506 						*wowner = &waiter->owner;
7507 
7508 		/* Only wake if the callback was for the same owner. */
7509 		if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7510 			return 0;
7511 
7512 		/* Make sure it's for the right inode */
7513 		if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7514 			return 0;
7515 	}
7516 
7517 	return woken_wake_function(wait, mode, flags, key);
7518 }
7519 
7520 static int
7521 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7522 {
7523 	struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7524 	struct nfs_server *server = NFS_SERVER(state->inode);
7525 	struct nfs_client *clp = server->nfs_client;
7526 	wait_queue_head_t *q = &clp->cl_lock_waitq;
7527 	struct nfs4_lock_waiter waiter = {
7528 		.inode = state->inode,
7529 		.owner = { .clientid = clp->cl_clientid,
7530 			   .id = lsp->ls_seqid.owner_id,
7531 			   .s_dev = server->s_dev },
7532 	};
7533 	int status;
7534 
7535 	/* Don't bother with waitqueue if we don't expect a callback */
7536 	if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7537 		return nfs4_retry_setlk_simple(state, cmd, request);
7538 
7539 	init_wait(&waiter.wait);
7540 	waiter.wait.func = nfs4_wake_lock_waiter;
7541 	add_wait_queue(q, &waiter.wait);
7542 
7543 	do {
7544 		status = nfs4_proc_setlk(state, cmd, request);
7545 		if (status != -EAGAIN || IS_SETLK(cmd))
7546 			break;
7547 
7548 		status = -ERESTARTSYS;
7549 		wait_woken(&waiter.wait, TASK_INTERRUPTIBLE|TASK_FREEZABLE,
7550 			   NFS4_LOCK_MAXTIMEOUT);
7551 	} while (!signalled());
7552 
7553 	remove_wait_queue(q, &waiter.wait);
7554 
7555 	return status;
7556 }
7557 #else /* !CONFIG_NFS_V4_1 */
7558 static inline int
7559 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7560 {
7561 	return nfs4_retry_setlk_simple(state, cmd, request);
7562 }
7563 #endif
7564 
7565 static int
7566 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7567 {
7568 	struct nfs_open_context *ctx;
7569 	struct nfs4_state *state;
7570 	int status;
7571 
7572 	/* verify open state */
7573 	ctx = nfs_file_open_context(filp);
7574 	state = ctx->state;
7575 
7576 	if (IS_GETLK(cmd)) {
7577 		if (state != NULL)
7578 			return nfs4_proc_getlk(state, F_GETLK, request);
7579 		return 0;
7580 	}
7581 
7582 	if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7583 		return -EINVAL;
7584 
7585 	if (lock_is_unlock(request)) {
7586 		if (state != NULL)
7587 			return nfs4_proc_unlck(state, cmd, request);
7588 		return 0;
7589 	}
7590 
7591 	if (state == NULL)
7592 		return -ENOLCK;
7593 
7594 	if ((request->c.flc_flags & FL_POSIX) &&
7595 	    !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7596 		return -ENOLCK;
7597 
7598 	/*
7599 	 * Don't rely on the VFS having checked the file open mode,
7600 	 * since it won't do this for flock() locks.
7601 	 */
7602 	switch (request->c.flc_type) {
7603 	case F_RDLCK:
7604 		if (!(filp->f_mode & FMODE_READ))
7605 			return -EBADF;
7606 		break;
7607 	case F_WRLCK:
7608 		if (!(filp->f_mode & FMODE_WRITE))
7609 			return -EBADF;
7610 	}
7611 
7612 	status = nfs4_set_lock_state(state, request);
7613 	if (status != 0)
7614 		return status;
7615 
7616 	return nfs4_retry_setlk(state, cmd, request);
7617 }
7618 
7619 static int nfs4_delete_lease(struct file *file, void **priv)
7620 {
7621 	return generic_setlease(file, F_UNLCK, NULL, priv);
7622 }
7623 
7624 static int nfs4_add_lease(struct file *file, int arg, struct file_lease **lease,
7625 			  void **priv)
7626 {
7627 	struct inode *inode = file_inode(file);
7628 	fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE;
7629 	int ret;
7630 
7631 	/* No delegation, no lease */
7632 	if (!nfs4_have_delegation(inode, type))
7633 		return -EAGAIN;
7634 	ret = generic_setlease(file, arg, lease, priv);
7635 	if (ret || nfs4_have_delegation(inode, type))
7636 		return ret;
7637 	/* We raced with a delegation return */
7638 	nfs4_delete_lease(file, priv);
7639 	return -EAGAIN;
7640 }
7641 
7642 int nfs4_proc_setlease(struct file *file, int arg, struct file_lease **lease,
7643 		       void **priv)
7644 {
7645 	switch (arg) {
7646 	case F_RDLCK:
7647 	case F_WRLCK:
7648 		return nfs4_add_lease(file, arg, lease, priv);
7649 	case F_UNLCK:
7650 		return nfs4_delete_lease(file, priv);
7651 	default:
7652 		return -EINVAL;
7653 	}
7654 }
7655 
7656 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7657 {
7658 	struct nfs_server *server = NFS_SERVER(state->inode);
7659 	int err;
7660 
7661 	err = nfs4_set_lock_state(state, fl);
7662 	if (err != 0)
7663 		return err;
7664 	do {
7665 		err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7666 		if (err != -NFS4ERR_DELAY)
7667 			break;
7668 		ssleep(1);
7669 	} while (err == -NFS4ERR_DELAY);
7670 	return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7671 }
7672 
7673 struct nfs_release_lockowner_data {
7674 	struct nfs4_lock_state *lsp;
7675 	struct nfs_server *server;
7676 	struct nfs_release_lockowner_args args;
7677 	struct nfs_release_lockowner_res res;
7678 	unsigned long timestamp;
7679 };
7680 
7681 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7682 {
7683 	struct nfs_release_lockowner_data *data = calldata;
7684 	struct nfs_server *server = data->server;
7685 	nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7686 			   &data->res.seq_res, task);
7687 	data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7688 	data->timestamp = jiffies;
7689 }
7690 
7691 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7692 {
7693 	struct nfs_release_lockowner_data *data = calldata;
7694 	struct nfs_server *server = data->server;
7695 
7696 	nfs40_sequence_done(task, &data->res.seq_res);
7697 
7698 	switch (task->tk_status) {
7699 	case 0:
7700 		renew_lease(server, data->timestamp);
7701 		break;
7702 	case -NFS4ERR_STALE_CLIENTID:
7703 	case -NFS4ERR_EXPIRED:
7704 		nfs4_schedule_lease_recovery(server->nfs_client);
7705 		break;
7706 	case -NFS4ERR_LEASE_MOVED:
7707 	case -NFS4ERR_DELAY:
7708 		if (nfs4_async_handle_error(task, server,
7709 					    NULL, NULL) == -EAGAIN)
7710 			rpc_restart_call_prepare(task);
7711 	}
7712 }
7713 
7714 static void nfs4_release_lockowner_release(void *calldata)
7715 {
7716 	struct nfs_release_lockowner_data *data = calldata;
7717 	nfs4_free_lock_state(data->server, data->lsp);
7718 	kfree(calldata);
7719 }
7720 
7721 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7722 	.rpc_call_prepare = nfs4_release_lockowner_prepare,
7723 	.rpc_call_done = nfs4_release_lockowner_done,
7724 	.rpc_release = nfs4_release_lockowner_release,
7725 };
7726 
7727 static void
7728 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7729 {
7730 	struct nfs_release_lockowner_data *data;
7731 	struct rpc_message msg = {
7732 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7733 	};
7734 
7735 	if (server->nfs_client->cl_mvops->minor_version != 0)
7736 		return;
7737 
7738 	data = kmalloc(sizeof(*data), GFP_KERNEL);
7739 	if (!data)
7740 		return;
7741 	data->lsp = lsp;
7742 	data->server = server;
7743 	data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7744 	data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7745 	data->args.lock_owner.s_dev = server->s_dev;
7746 
7747 	msg.rpc_argp = &data->args;
7748 	msg.rpc_resp = &data->res;
7749 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7750 	rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7751 }
7752 
7753 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7754 
7755 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7756 				   struct mnt_idmap *idmap,
7757 				   struct dentry *unused, struct inode *inode,
7758 				   const char *key, const void *buf,
7759 				   size_t buflen, int flags)
7760 {
7761 	return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_ACL);
7762 }
7763 
7764 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7765 				   struct dentry *unused, struct inode *inode,
7766 				   const char *key, void *buf, size_t buflen)
7767 {
7768 	return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_ACL);
7769 }
7770 
7771 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7772 {
7773 	return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_ACL);
7774 }
7775 
7776 #if defined(CONFIG_NFS_V4_1)
7777 #define XATTR_NAME_NFSV4_DACL "system.nfs4_dacl"
7778 
7779 static int nfs4_xattr_set_nfs4_dacl(const struct xattr_handler *handler,
7780 				    struct mnt_idmap *idmap,
7781 				    struct dentry *unused, struct inode *inode,
7782 				    const char *key, const void *buf,
7783 				    size_t buflen, int flags)
7784 {
7785 	return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_DACL);
7786 }
7787 
7788 static int nfs4_xattr_get_nfs4_dacl(const struct xattr_handler *handler,
7789 				    struct dentry *unused, struct inode *inode,
7790 				    const char *key, void *buf, size_t buflen)
7791 {
7792 	return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_DACL);
7793 }
7794 
7795 static bool nfs4_xattr_list_nfs4_dacl(struct dentry *dentry)
7796 {
7797 	return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_DACL);
7798 }
7799 
7800 #define XATTR_NAME_NFSV4_SACL "system.nfs4_sacl"
7801 
7802 static int nfs4_xattr_set_nfs4_sacl(const struct xattr_handler *handler,
7803 				    struct mnt_idmap *idmap,
7804 				    struct dentry *unused, struct inode *inode,
7805 				    const char *key, const void *buf,
7806 				    size_t buflen, int flags)
7807 {
7808 	return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_SACL);
7809 }
7810 
7811 static int nfs4_xattr_get_nfs4_sacl(const struct xattr_handler *handler,
7812 				    struct dentry *unused, struct inode *inode,
7813 				    const char *key, void *buf, size_t buflen)
7814 {
7815 	return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_SACL);
7816 }
7817 
7818 static bool nfs4_xattr_list_nfs4_sacl(struct dentry *dentry)
7819 {
7820 	return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_SACL);
7821 }
7822 
7823 #endif
7824 
7825 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7826 
7827 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7828 				     struct mnt_idmap *idmap,
7829 				     struct dentry *unused, struct inode *inode,
7830 				     const char *key, const void *buf,
7831 				     size_t buflen, int flags)
7832 {
7833 	if (security_ismaclabel(key))
7834 		return nfs4_set_security_label(inode, buf, buflen);
7835 
7836 	return -EOPNOTSUPP;
7837 }
7838 
7839 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7840 				     struct dentry *unused, struct inode *inode,
7841 				     const char *key, void *buf, size_t buflen)
7842 {
7843 	if (security_ismaclabel(key))
7844 		return nfs4_get_security_label(inode, buf, buflen);
7845 	return -EOPNOTSUPP;
7846 }
7847 
7848 static ssize_t
7849 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7850 {
7851 	int len = 0;
7852 
7853 	if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7854 		len = security_inode_listsecurity(inode, list, list_len);
7855 		if (len >= 0 && list_len && len > list_len)
7856 			return -ERANGE;
7857 	}
7858 	return len;
7859 }
7860 
7861 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7862 	.prefix = XATTR_SECURITY_PREFIX,
7863 	.get	= nfs4_xattr_get_nfs4_label,
7864 	.set	= nfs4_xattr_set_nfs4_label,
7865 };
7866 
7867 #else
7868 
7869 static ssize_t
7870 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7871 {
7872 	return 0;
7873 }
7874 
7875 #endif
7876 
7877 #ifdef CONFIG_NFS_V4_2
7878 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7879 				    struct mnt_idmap *idmap,
7880 				    struct dentry *unused, struct inode *inode,
7881 				    const char *key, const void *buf,
7882 				    size_t buflen, int flags)
7883 {
7884 	u32 mask;
7885 	int ret;
7886 
7887 	if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7888 		return -EOPNOTSUPP;
7889 
7890 	/*
7891 	 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7892 	 * flags right now. Handling of xattr operations use the normal
7893 	 * file read/write permissions.
7894 	 *
7895 	 * Just in case the server has other ideas (which RFC 8276 allows),
7896 	 * do a cached access check for the XA* flags to possibly avoid
7897 	 * doing an RPC and getting EACCES back.
7898 	 */
7899 	if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7900 		if (!(mask & NFS_ACCESS_XAWRITE))
7901 			return -EACCES;
7902 	}
7903 
7904 	if (buf == NULL) {
7905 		ret = nfs42_proc_removexattr(inode, key);
7906 		if (!ret)
7907 			nfs4_xattr_cache_remove(inode, key);
7908 	} else {
7909 		ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7910 		if (!ret)
7911 			nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7912 	}
7913 
7914 	return ret;
7915 }
7916 
7917 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7918 				    struct dentry *unused, struct inode *inode,
7919 				    const char *key, void *buf, size_t buflen)
7920 {
7921 	u32 mask;
7922 	ssize_t ret;
7923 
7924 	if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7925 		return -EOPNOTSUPP;
7926 
7927 	if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7928 		if (!(mask & NFS_ACCESS_XAREAD))
7929 			return -EACCES;
7930 	}
7931 
7932 	ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7933 	if (ret)
7934 		return ret;
7935 
7936 	ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7937 	if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7938 		return ret;
7939 
7940 	ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7941 
7942 	return ret;
7943 }
7944 
7945 static ssize_t
7946 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7947 {
7948 	u64 cookie;
7949 	bool eof;
7950 	ssize_t ret, size;
7951 	char *buf;
7952 	size_t buflen;
7953 	u32 mask;
7954 
7955 	if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7956 		return 0;
7957 
7958 	if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7959 		if (!(mask & NFS_ACCESS_XALIST))
7960 			return 0;
7961 	}
7962 
7963 	ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7964 	if (ret)
7965 		return ret;
7966 
7967 	ret = nfs4_xattr_cache_list(inode, list, list_len);
7968 	if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7969 		return ret;
7970 
7971 	cookie = 0;
7972 	eof = false;
7973 	buflen = list_len ? list_len : XATTR_LIST_MAX;
7974 	buf = list_len ? list : NULL;
7975 	size = 0;
7976 
7977 	while (!eof) {
7978 		ret = nfs42_proc_listxattrs(inode, buf, buflen,
7979 		    &cookie, &eof);
7980 		if (ret < 0)
7981 			return ret;
7982 
7983 		if (list_len) {
7984 			buf += ret;
7985 			buflen -= ret;
7986 		}
7987 		size += ret;
7988 	}
7989 
7990 	if (list_len)
7991 		nfs4_xattr_cache_set_list(inode, list, size);
7992 
7993 	return size;
7994 }
7995 
7996 #else
7997 
7998 static ssize_t
7999 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
8000 {
8001 	return 0;
8002 }
8003 #endif /* CONFIG_NFS_V4_2 */
8004 
8005 /*
8006  * nfs_fhget will use either the mounted_on_fileid or the fileid
8007  */
8008 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
8009 {
8010 	if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
8011 	       (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
8012 	      (fattr->valid & NFS_ATTR_FATTR_FSID) &&
8013 	      (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
8014 		return;
8015 
8016 	fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
8017 		NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
8018 	fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
8019 	fattr->nlink = 2;
8020 }
8021 
8022 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
8023 				   const struct qstr *name,
8024 				   struct nfs4_fs_locations *fs_locations,
8025 				   struct page *page)
8026 {
8027 	struct nfs_server *server = NFS_SERVER(dir);
8028 	u32 bitmask[3];
8029 	struct nfs4_fs_locations_arg args = {
8030 		.dir_fh = NFS_FH(dir),
8031 		.name = name,
8032 		.page = page,
8033 		.bitmask = bitmask,
8034 	};
8035 	struct nfs4_fs_locations_res res = {
8036 		.fs_locations = fs_locations,
8037 	};
8038 	struct rpc_message msg = {
8039 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8040 		.rpc_argp = &args,
8041 		.rpc_resp = &res,
8042 	};
8043 	int status;
8044 
8045 	dprintk("%s: start\n", __func__);
8046 
8047 	bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
8048 	bitmask[1] = nfs4_fattr_bitmap[1];
8049 
8050 	/* Ask for the fileid of the absent filesystem if mounted_on_fileid
8051 	 * is not supported */
8052 	if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
8053 		bitmask[0] &= ~FATTR4_WORD0_FILEID;
8054 	else
8055 		bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
8056 
8057 	nfs_fattr_init(fs_locations->fattr);
8058 	fs_locations->server = server;
8059 	fs_locations->nlocations = 0;
8060 	status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
8061 	dprintk("%s: returned status = %d\n", __func__, status);
8062 	return status;
8063 }
8064 
8065 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
8066 			   const struct qstr *name,
8067 			   struct nfs4_fs_locations *fs_locations,
8068 			   struct page *page)
8069 {
8070 	struct nfs4_exception exception = {
8071 		.interruptible = true,
8072 	};
8073 	int err;
8074 	do {
8075 		err = _nfs4_proc_fs_locations(client, dir, name,
8076 				fs_locations, page);
8077 		trace_nfs4_get_fs_locations(dir, name, err);
8078 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
8079 				&exception);
8080 	} while (exception.retry);
8081 	return err;
8082 }
8083 
8084 /*
8085  * This operation also signals the server that this client is
8086  * performing migration recovery.  The server can stop returning
8087  * NFS4ERR_LEASE_MOVED to this client.  A RENEW operation is
8088  * appended to this compound to identify the client ID which is
8089  * performing recovery.
8090  */
8091 static int _nfs40_proc_get_locations(struct nfs_server *server,
8092 				     struct nfs_fh *fhandle,
8093 				     struct nfs4_fs_locations *locations,
8094 				     struct page *page, const struct cred *cred)
8095 {
8096 	struct rpc_clnt *clnt = server->client;
8097 	u32 bitmask[2] = {
8098 		[0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8099 	};
8100 	struct nfs4_fs_locations_arg args = {
8101 		.clientid	= server->nfs_client->cl_clientid,
8102 		.fh		= fhandle,
8103 		.page		= page,
8104 		.bitmask	= bitmask,
8105 		.migration	= 1,		/* skip LOOKUP */
8106 		.renew		= 1,		/* append RENEW */
8107 	};
8108 	struct nfs4_fs_locations_res res = {
8109 		.fs_locations	= locations,
8110 		.migration	= 1,
8111 		.renew		= 1,
8112 	};
8113 	struct rpc_message msg = {
8114 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8115 		.rpc_argp	= &args,
8116 		.rpc_resp	= &res,
8117 		.rpc_cred	= cred,
8118 	};
8119 	unsigned long now = jiffies;
8120 	int status;
8121 
8122 	nfs_fattr_init(locations->fattr);
8123 	locations->server = server;
8124 	locations->nlocations = 0;
8125 
8126 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8127 	status = nfs4_call_sync_sequence(clnt, server, &msg,
8128 					&args.seq_args, &res.seq_res);
8129 	if (status)
8130 		return status;
8131 
8132 	renew_lease(server, now);
8133 	return 0;
8134 }
8135 
8136 #ifdef CONFIG_NFS_V4_1
8137 
8138 /*
8139  * This operation also signals the server that this client is
8140  * performing migration recovery.  The server can stop asserting
8141  * SEQ4_STATUS_LEASE_MOVED for this client.  The client ID
8142  * performing this operation is identified in the SEQUENCE
8143  * operation in this compound.
8144  *
8145  * When the client supports GETATTR(fs_locations_info), it can
8146  * be plumbed in here.
8147  */
8148 static int _nfs41_proc_get_locations(struct nfs_server *server,
8149 				     struct nfs_fh *fhandle,
8150 				     struct nfs4_fs_locations *locations,
8151 				     struct page *page, const struct cred *cred)
8152 {
8153 	struct rpc_clnt *clnt = server->client;
8154 	u32 bitmask[2] = {
8155 		[0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8156 	};
8157 	struct nfs4_fs_locations_arg args = {
8158 		.fh		= fhandle,
8159 		.page		= page,
8160 		.bitmask	= bitmask,
8161 		.migration	= 1,		/* skip LOOKUP */
8162 	};
8163 	struct nfs4_fs_locations_res res = {
8164 		.fs_locations	= locations,
8165 		.migration	= 1,
8166 	};
8167 	struct rpc_message msg = {
8168 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8169 		.rpc_argp	= &args,
8170 		.rpc_resp	= &res,
8171 		.rpc_cred	= cred,
8172 	};
8173 	struct nfs4_call_sync_data data = {
8174 		.seq_server = server,
8175 		.seq_args = &args.seq_args,
8176 		.seq_res = &res.seq_res,
8177 	};
8178 	struct rpc_task_setup task_setup_data = {
8179 		.rpc_client = clnt,
8180 		.rpc_message = &msg,
8181 		.callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
8182 		.callback_data = &data,
8183 		.flags = RPC_TASK_NO_ROUND_ROBIN,
8184 	};
8185 	int status;
8186 
8187 	nfs_fattr_init(locations->fattr);
8188 	locations->server = server;
8189 	locations->nlocations = 0;
8190 
8191 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8192 	status = nfs4_call_sync_custom(&task_setup_data);
8193 	if (status == NFS4_OK &&
8194 	    res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8195 		status = -NFS4ERR_LEASE_MOVED;
8196 	return status;
8197 }
8198 
8199 #endif	/* CONFIG_NFS_V4_1 */
8200 
8201 /**
8202  * nfs4_proc_get_locations - discover locations for a migrated FSID
8203  * @server: pointer to nfs_server to process
8204  * @fhandle: pointer to the kernel NFS client file handle
8205  * @locations: result of query
8206  * @page: buffer
8207  * @cred: credential to use for this operation
8208  *
8209  * Returns NFS4_OK on success, a negative NFS4ERR status code if the
8210  * operation failed, or a negative errno if a local error occurred.
8211  *
8212  * On success, "locations" is filled in, but if the server has
8213  * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
8214  * asserted.
8215  *
8216  * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
8217  * from this client that require migration recovery.
8218  */
8219 int nfs4_proc_get_locations(struct nfs_server *server,
8220 			    struct nfs_fh *fhandle,
8221 			    struct nfs4_fs_locations *locations,
8222 			    struct page *page, const struct cred *cred)
8223 {
8224 	struct nfs_client *clp = server->nfs_client;
8225 	const struct nfs4_mig_recovery_ops *ops =
8226 					clp->cl_mvops->mig_recovery_ops;
8227 	struct nfs4_exception exception = {
8228 		.interruptible = true,
8229 	};
8230 	int status;
8231 
8232 	dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8233 		(unsigned long long)server->fsid.major,
8234 		(unsigned long long)server->fsid.minor,
8235 		clp->cl_hostname);
8236 	nfs_display_fhandle(fhandle, __func__);
8237 
8238 	do {
8239 		status = ops->get_locations(server, fhandle, locations, page,
8240 					    cred);
8241 		if (status != -NFS4ERR_DELAY)
8242 			break;
8243 		nfs4_handle_exception(server, status, &exception);
8244 	} while (exception.retry);
8245 	return status;
8246 }
8247 
8248 /*
8249  * This operation also signals the server that this client is
8250  * performing "lease moved" recovery.  The server can stop
8251  * returning NFS4ERR_LEASE_MOVED to this client.  A RENEW operation
8252  * is appended to this compound to identify the client ID which is
8253  * performing recovery.
8254  */
8255 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
8256 {
8257 	struct nfs_server *server = NFS_SERVER(inode);
8258 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
8259 	struct rpc_clnt *clnt = server->client;
8260 	struct nfs4_fsid_present_arg args = {
8261 		.fh		= NFS_FH(inode),
8262 		.clientid	= clp->cl_clientid,
8263 		.renew		= 1,		/* append RENEW */
8264 	};
8265 	struct nfs4_fsid_present_res res = {
8266 		.renew		= 1,
8267 	};
8268 	struct rpc_message msg = {
8269 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8270 		.rpc_argp	= &args,
8271 		.rpc_resp	= &res,
8272 		.rpc_cred	= cred,
8273 	};
8274 	unsigned long now = jiffies;
8275 	int status;
8276 
8277 	res.fh = nfs_alloc_fhandle();
8278 	if (res.fh == NULL)
8279 		return -ENOMEM;
8280 
8281 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8282 	status = nfs4_call_sync_sequence(clnt, server, &msg,
8283 						&args.seq_args, &res.seq_res);
8284 	nfs_free_fhandle(res.fh);
8285 	if (status)
8286 		return status;
8287 
8288 	do_renew_lease(clp, now);
8289 	return 0;
8290 }
8291 
8292 #ifdef CONFIG_NFS_V4_1
8293 
8294 /*
8295  * This operation also signals the server that this client is
8296  * performing "lease moved" recovery.  The server can stop asserting
8297  * SEQ4_STATUS_LEASE_MOVED for this client.  The client ID performing
8298  * this operation is identified in the SEQUENCE operation in this
8299  * compound.
8300  */
8301 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
8302 {
8303 	struct nfs_server *server = NFS_SERVER(inode);
8304 	struct rpc_clnt *clnt = server->client;
8305 	struct nfs4_fsid_present_arg args = {
8306 		.fh		= NFS_FH(inode),
8307 	};
8308 	struct nfs4_fsid_present_res res = {
8309 	};
8310 	struct rpc_message msg = {
8311 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8312 		.rpc_argp	= &args,
8313 		.rpc_resp	= &res,
8314 		.rpc_cred	= cred,
8315 	};
8316 	int status;
8317 
8318 	res.fh = nfs_alloc_fhandle();
8319 	if (res.fh == NULL)
8320 		return -ENOMEM;
8321 
8322 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8323 	status = nfs4_call_sync_sequence(clnt, server, &msg,
8324 						&args.seq_args, &res.seq_res);
8325 	nfs_free_fhandle(res.fh);
8326 	if (status == NFS4_OK &&
8327 	    res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8328 		status = -NFS4ERR_LEASE_MOVED;
8329 	return status;
8330 }
8331 
8332 #endif	/* CONFIG_NFS_V4_1 */
8333 
8334 /**
8335  * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8336  * @inode: inode on FSID to check
8337  * @cred: credential to use for this operation
8338  *
8339  * Server indicates whether the FSID is present, moved, or not
8340  * recognized.  This operation is necessary to clear a LEASE_MOVED
8341  * condition for this client ID.
8342  *
8343  * Returns NFS4_OK if the FSID is present on this server,
8344  * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8345  *  NFS4ERR code if some error occurred on the server, or a
8346  *  negative errno if a local failure occurred.
8347  */
8348 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
8349 {
8350 	struct nfs_server *server = NFS_SERVER(inode);
8351 	struct nfs_client *clp = server->nfs_client;
8352 	const struct nfs4_mig_recovery_ops *ops =
8353 					clp->cl_mvops->mig_recovery_ops;
8354 	struct nfs4_exception exception = {
8355 		.interruptible = true,
8356 	};
8357 	int status;
8358 
8359 	dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8360 		(unsigned long long)server->fsid.major,
8361 		(unsigned long long)server->fsid.minor,
8362 		clp->cl_hostname);
8363 	nfs_display_fhandle(NFS_FH(inode), __func__);
8364 
8365 	do {
8366 		status = ops->fsid_present(inode, cred);
8367 		if (status != -NFS4ERR_DELAY)
8368 			break;
8369 		nfs4_handle_exception(server, status, &exception);
8370 	} while (exception.retry);
8371 	return status;
8372 }
8373 
8374 /*
8375  * If 'use_integrity' is true and the state managment nfs_client
8376  * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8377  * and the machine credential as per RFC3530bis and RFC5661 Security
8378  * Considerations sections. Otherwise, just use the user cred with the
8379  * filesystem's rpc_client.
8380  */
8381 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8382 {
8383 	int status;
8384 	struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8385 	struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
8386 	struct nfs4_secinfo_arg args = {
8387 		.dir_fh = NFS_FH(dir),
8388 		.name   = name,
8389 	};
8390 	struct nfs4_secinfo_res res = {
8391 		.flavors     = flavors,
8392 	};
8393 	struct rpc_message msg = {
8394 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8395 		.rpc_argp = &args,
8396 		.rpc_resp = &res,
8397 	};
8398 	struct nfs4_call_sync_data data = {
8399 		.seq_server = NFS_SERVER(dir),
8400 		.seq_args = &args.seq_args,
8401 		.seq_res = &res.seq_res,
8402 	};
8403 	struct rpc_task_setup task_setup = {
8404 		.rpc_client = clnt,
8405 		.rpc_message = &msg,
8406 		.callback_ops = clp->cl_mvops->call_sync_ops,
8407 		.callback_data = &data,
8408 		.flags = RPC_TASK_NO_ROUND_ROBIN,
8409 	};
8410 	const struct cred *cred = NULL;
8411 
8412 	if (use_integrity) {
8413 		clnt = clp->cl_rpcclient;
8414 		task_setup.rpc_client = clnt;
8415 
8416 		cred = nfs4_get_clid_cred(clp);
8417 		msg.rpc_cred = cred;
8418 	}
8419 
8420 	dprintk("NFS call  secinfo %s\n", name->name);
8421 
8422 	nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8423 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8424 	status = nfs4_call_sync_custom(&task_setup);
8425 
8426 	dprintk("NFS reply  secinfo: %d\n", status);
8427 
8428 	put_cred(cred);
8429 	return status;
8430 }
8431 
8432 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8433 		      struct nfs4_secinfo_flavors *flavors)
8434 {
8435 	struct nfs4_exception exception = {
8436 		.interruptible = true,
8437 	};
8438 	int err;
8439 	do {
8440 		err = -NFS4ERR_WRONGSEC;
8441 
8442 		/* try to use integrity protection with machine cred */
8443 		if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8444 			err = _nfs4_proc_secinfo(dir, name, flavors, true);
8445 
8446 		/*
8447 		 * if unable to use integrity protection, or SECINFO with
8448 		 * integrity protection returns NFS4ERR_WRONGSEC (which is
8449 		 * disallowed by spec, but exists in deployed servers) use
8450 		 * the current filesystem's rpc_client and the user cred.
8451 		 */
8452 		if (err == -NFS4ERR_WRONGSEC)
8453 			err = _nfs4_proc_secinfo(dir, name, flavors, false);
8454 
8455 		trace_nfs4_secinfo(dir, name, err);
8456 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
8457 				&exception);
8458 	} while (exception.retry);
8459 	return err;
8460 }
8461 
8462 #ifdef CONFIG_NFS_V4_1
8463 /*
8464  * Check the exchange flags returned by the server for invalid flags, having
8465  * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8466  * DS flags set.
8467  */
8468 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
8469 {
8470 	if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8471 		goto out_inval;
8472 	else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
8473 		goto out_inval;
8474 	if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8475 	    (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8476 		goto out_inval;
8477 	if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8478 		goto out_inval;
8479 	return NFS_OK;
8480 out_inval:
8481 	return -NFS4ERR_INVAL;
8482 }
8483 
8484 static bool
8485 nfs41_same_server_scope(struct nfs41_server_scope *a,
8486 			struct nfs41_server_scope *b)
8487 {
8488 	if (a->server_scope_sz != b->server_scope_sz)
8489 		return false;
8490 	return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8491 }
8492 
8493 static void
8494 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8495 {
8496 	struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8497 	struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8498 	struct nfs_client *clp = args->client;
8499 
8500 	switch (task->tk_status) {
8501 	case -NFS4ERR_BADSESSION:
8502 	case -NFS4ERR_DEADSESSION:
8503 		nfs4_schedule_session_recovery(clp->cl_session,
8504 				task->tk_status);
8505 		return;
8506 	}
8507 	if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8508 			res->dir != NFS4_CDFS4_BOTH) {
8509 		rpc_task_close_connection(task);
8510 		if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8511 			rpc_restart_call(task);
8512 	}
8513 }
8514 
8515 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8516 	.rpc_call_done =  nfs4_bind_one_conn_to_session_done,
8517 };
8518 
8519 /*
8520  * nfs4_proc_bind_one_conn_to_session()
8521  *
8522  * The 4.1 client currently uses the same TCP connection for the
8523  * fore and backchannel.
8524  */
8525 static
8526 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8527 		struct rpc_xprt *xprt,
8528 		struct nfs_client *clp,
8529 		const struct cred *cred)
8530 {
8531 	int status;
8532 	struct nfs41_bind_conn_to_session_args args = {
8533 		.client = clp,
8534 		.dir = NFS4_CDFC4_FORE_OR_BOTH,
8535 		.retries = 0,
8536 	};
8537 	struct nfs41_bind_conn_to_session_res res;
8538 	struct rpc_message msg = {
8539 		.rpc_proc =
8540 			&nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8541 		.rpc_argp = &args,
8542 		.rpc_resp = &res,
8543 		.rpc_cred = cred,
8544 	};
8545 	struct rpc_task_setup task_setup_data = {
8546 		.rpc_client = clnt,
8547 		.rpc_xprt = xprt,
8548 		.callback_ops = &nfs4_bind_one_conn_to_session_ops,
8549 		.rpc_message = &msg,
8550 		.flags = RPC_TASK_TIMEOUT,
8551 	};
8552 	struct rpc_task *task;
8553 
8554 	nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8555 	if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8556 		args.dir = NFS4_CDFC4_FORE;
8557 
8558 	/* Do not set the backchannel flag unless this is clnt->cl_xprt */
8559 	if (xprt != rcu_access_pointer(clnt->cl_xprt))
8560 		args.dir = NFS4_CDFC4_FORE;
8561 
8562 	task = rpc_run_task(&task_setup_data);
8563 	if (!IS_ERR(task)) {
8564 		status = task->tk_status;
8565 		rpc_put_task(task);
8566 	} else
8567 		status = PTR_ERR(task);
8568 	trace_nfs4_bind_conn_to_session(clp, status);
8569 	if (status == 0) {
8570 		if (memcmp(res.sessionid.data,
8571 		    clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8572 			dprintk("NFS: %s: Session ID mismatch\n", __func__);
8573 			return -EIO;
8574 		}
8575 		if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8576 			dprintk("NFS: %s: Unexpected direction from server\n",
8577 				__func__);
8578 			return -EIO;
8579 		}
8580 		if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8581 			dprintk("NFS: %s: Server returned RDMA mode = true\n",
8582 				__func__);
8583 			return -EIO;
8584 		}
8585 	}
8586 
8587 	return status;
8588 }
8589 
8590 struct rpc_bind_conn_calldata {
8591 	struct nfs_client *clp;
8592 	const struct cred *cred;
8593 };
8594 
8595 static int
8596 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8597 		struct rpc_xprt *xprt,
8598 		void *calldata)
8599 {
8600 	struct rpc_bind_conn_calldata *p = calldata;
8601 
8602 	return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8603 }
8604 
8605 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8606 {
8607 	struct rpc_bind_conn_calldata data = {
8608 		.clp = clp,
8609 		.cred = cred,
8610 	};
8611 	return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8612 			nfs4_proc_bind_conn_to_session_callback, &data);
8613 }
8614 
8615 /*
8616  * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8617  * and operations we'd like to see to enable certain features in the allow map
8618  */
8619 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8620 	.how = SP4_MACH_CRED,
8621 	.enforce.u.words = {
8622 		[1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8623 		      1 << (OP_EXCHANGE_ID - 32) |
8624 		      1 << (OP_CREATE_SESSION - 32) |
8625 		      1 << (OP_DESTROY_SESSION - 32) |
8626 		      1 << (OP_DESTROY_CLIENTID - 32)
8627 	},
8628 	.allow.u.words = {
8629 		[0] = 1 << (OP_CLOSE) |
8630 		      1 << (OP_OPEN_DOWNGRADE) |
8631 		      1 << (OP_LOCKU) |
8632 		      1 << (OP_DELEGRETURN) |
8633 		      1 << (OP_COMMIT),
8634 		[1] = 1 << (OP_SECINFO - 32) |
8635 		      1 << (OP_SECINFO_NO_NAME - 32) |
8636 		      1 << (OP_LAYOUTRETURN - 32) |
8637 		      1 << (OP_TEST_STATEID - 32) |
8638 		      1 << (OP_FREE_STATEID - 32) |
8639 		      1 << (OP_WRITE - 32)
8640 	}
8641 };
8642 
8643 /*
8644  * Select the state protection mode for client `clp' given the server results
8645  * from exchange_id in `sp'.
8646  *
8647  * Returns 0 on success, negative errno otherwise.
8648  */
8649 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8650 				 struct nfs41_state_protection *sp)
8651 {
8652 	static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8653 		[1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8654 		      1 << (OP_EXCHANGE_ID - 32) |
8655 		      1 << (OP_CREATE_SESSION - 32) |
8656 		      1 << (OP_DESTROY_SESSION - 32) |
8657 		      1 << (OP_DESTROY_CLIENTID - 32)
8658 	};
8659 	unsigned long flags = 0;
8660 	unsigned int i;
8661 	int ret = 0;
8662 
8663 	if (sp->how == SP4_MACH_CRED) {
8664 		/* Print state protect result */
8665 		dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8666 		for (i = 0; i <= LAST_NFS4_OP; i++) {
8667 			if (test_bit(i, sp->enforce.u.longs))
8668 				dfprintk(MOUNT, "  enforce op %d\n", i);
8669 			if (test_bit(i, sp->allow.u.longs))
8670 				dfprintk(MOUNT, "  allow op %d\n", i);
8671 		}
8672 
8673 		/* make sure nothing is on enforce list that isn't supported */
8674 		for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8675 			if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8676 				dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8677 				ret = -EINVAL;
8678 				goto out;
8679 			}
8680 		}
8681 
8682 		/*
8683 		 * Minimal mode - state operations are allowed to use machine
8684 		 * credential.  Note this already happens by default, so the
8685 		 * client doesn't have to do anything more than the negotiation.
8686 		 *
8687 		 * NOTE: we don't care if EXCHANGE_ID is in the list -
8688 		 *       we're already using the machine cred for exchange_id
8689 		 *       and will never use a different cred.
8690 		 */
8691 		if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8692 		    test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8693 		    test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8694 		    test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8695 			dfprintk(MOUNT, "sp4_mach_cred:\n");
8696 			dfprintk(MOUNT, "  minimal mode enabled\n");
8697 			__set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8698 		} else {
8699 			dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8700 			ret = -EINVAL;
8701 			goto out;
8702 		}
8703 
8704 		if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8705 		    test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8706 		    test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8707 		    test_bit(OP_LOCKU, sp->allow.u.longs)) {
8708 			dfprintk(MOUNT, "  cleanup mode enabled\n");
8709 			__set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8710 		}
8711 
8712 		if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8713 			dfprintk(MOUNT, "  pnfs cleanup mode enabled\n");
8714 			__set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8715 		}
8716 
8717 		if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8718 		    test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8719 			dfprintk(MOUNT, "  secinfo mode enabled\n");
8720 			__set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8721 		}
8722 
8723 		if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8724 		    test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8725 			dfprintk(MOUNT, "  stateid mode enabled\n");
8726 			__set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8727 		}
8728 
8729 		if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8730 			dfprintk(MOUNT, "  write mode enabled\n");
8731 			__set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8732 		}
8733 
8734 		if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8735 			dfprintk(MOUNT, "  commit mode enabled\n");
8736 			__set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8737 		}
8738 	}
8739 out:
8740 	clp->cl_sp4_flags = flags;
8741 	return ret;
8742 }
8743 
8744 struct nfs41_exchange_id_data {
8745 	struct nfs41_exchange_id_res res;
8746 	struct nfs41_exchange_id_args args;
8747 };
8748 
8749 static void nfs4_exchange_id_release(void *data)
8750 {
8751 	struct nfs41_exchange_id_data *cdata =
8752 					(struct nfs41_exchange_id_data *)data;
8753 
8754 	nfs_put_client(cdata->args.client);
8755 	kfree(cdata->res.impl_id);
8756 	kfree(cdata->res.server_scope);
8757 	kfree(cdata->res.server_owner);
8758 	kfree(cdata);
8759 }
8760 
8761 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8762 	.rpc_release = nfs4_exchange_id_release,
8763 };
8764 
8765 /*
8766  * _nfs4_proc_exchange_id()
8767  *
8768  * Wrapper for EXCHANGE_ID operation.
8769  */
8770 static struct rpc_task *
8771 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8772 			u32 sp4_how, struct rpc_xprt *xprt)
8773 {
8774 	struct rpc_message msg = {
8775 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8776 		.rpc_cred = cred,
8777 	};
8778 	struct rpc_task_setup task_setup_data = {
8779 		.rpc_client = clp->cl_rpcclient,
8780 		.callback_ops = &nfs4_exchange_id_call_ops,
8781 		.rpc_message = &msg,
8782 		.flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8783 	};
8784 	struct nfs41_exchange_id_data *calldata;
8785 	int status;
8786 
8787 	if (!refcount_inc_not_zero(&clp->cl_count))
8788 		return ERR_PTR(-EIO);
8789 
8790 	status = -ENOMEM;
8791 	calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8792 	if (!calldata)
8793 		goto out;
8794 
8795 	nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8796 
8797 	status = nfs4_init_uniform_client_string(clp);
8798 	if (status)
8799 		goto out_calldata;
8800 
8801 	calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8802 						GFP_NOFS);
8803 	status = -ENOMEM;
8804 	if (unlikely(calldata->res.server_owner == NULL))
8805 		goto out_calldata;
8806 
8807 	calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8808 					GFP_NOFS);
8809 	if (unlikely(calldata->res.server_scope == NULL))
8810 		goto out_server_owner;
8811 
8812 	calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8813 	if (unlikely(calldata->res.impl_id == NULL))
8814 		goto out_server_scope;
8815 
8816 	switch (sp4_how) {
8817 	case SP4_NONE:
8818 		calldata->args.state_protect.how = SP4_NONE;
8819 		break;
8820 
8821 	case SP4_MACH_CRED:
8822 		calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8823 		break;
8824 
8825 	default:
8826 		/* unsupported! */
8827 		WARN_ON_ONCE(1);
8828 		status = -EINVAL;
8829 		goto out_impl_id;
8830 	}
8831 	if (xprt) {
8832 		task_setup_data.rpc_xprt = xprt;
8833 		task_setup_data.flags |= RPC_TASK_SOFTCONN;
8834 		memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8835 				sizeof(calldata->args.verifier.data));
8836 	}
8837 	calldata->args.client = clp;
8838 	calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8839 	EXCHGID4_FLAG_BIND_PRINC_STATEID;
8840 #ifdef CONFIG_NFS_V4_1_MIGRATION
8841 	calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8842 #endif
8843 	if (test_bit(NFS_CS_DS, &clp->cl_flags))
8844 		calldata->args.flags |= EXCHGID4_FLAG_USE_PNFS_DS;
8845 	msg.rpc_argp = &calldata->args;
8846 	msg.rpc_resp = &calldata->res;
8847 	task_setup_data.callback_data = calldata;
8848 
8849 	return rpc_run_task(&task_setup_data);
8850 
8851 out_impl_id:
8852 	kfree(calldata->res.impl_id);
8853 out_server_scope:
8854 	kfree(calldata->res.server_scope);
8855 out_server_owner:
8856 	kfree(calldata->res.server_owner);
8857 out_calldata:
8858 	kfree(calldata);
8859 out:
8860 	nfs_put_client(clp);
8861 	return ERR_PTR(status);
8862 }
8863 
8864 /*
8865  * _nfs4_proc_exchange_id()
8866  *
8867  * Wrapper for EXCHANGE_ID operation.
8868  */
8869 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8870 			u32 sp4_how)
8871 {
8872 	struct rpc_task *task;
8873 	struct nfs41_exchange_id_args *argp;
8874 	struct nfs41_exchange_id_res *resp;
8875 	unsigned long now = jiffies;
8876 	int status;
8877 
8878 	task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8879 	if (IS_ERR(task))
8880 		return PTR_ERR(task);
8881 
8882 	argp = task->tk_msg.rpc_argp;
8883 	resp = task->tk_msg.rpc_resp;
8884 	status = task->tk_status;
8885 	if (status  != 0)
8886 		goto out;
8887 
8888 	status = nfs4_check_cl_exchange_flags(resp->flags,
8889 			clp->cl_mvops->minor_version);
8890 	if (status  != 0)
8891 		goto out;
8892 
8893 	status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8894 	if (status != 0)
8895 		goto out;
8896 
8897 	do_renew_lease(clp, now);
8898 
8899 	clp->cl_clientid = resp->clientid;
8900 	clp->cl_exchange_flags = resp->flags;
8901 	clp->cl_seqid = resp->seqid;
8902 	/* Client ID is not confirmed */
8903 	if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8904 		clear_bit(NFS4_SESSION_ESTABLISHED,
8905 			  &clp->cl_session->session_state);
8906 
8907 	if (clp->cl_serverscope != NULL &&
8908 	    !nfs41_same_server_scope(clp->cl_serverscope,
8909 				resp->server_scope)) {
8910 		dprintk("%s: server_scope mismatch detected\n",
8911 			__func__);
8912 		set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8913 	}
8914 
8915 	swap(clp->cl_serverowner, resp->server_owner);
8916 	swap(clp->cl_serverscope, resp->server_scope);
8917 	swap(clp->cl_implid, resp->impl_id);
8918 
8919 	/* Save the EXCHANGE_ID verifier session trunk tests */
8920 	memcpy(clp->cl_confirm.data, argp->verifier.data,
8921 	       sizeof(clp->cl_confirm.data));
8922 out:
8923 	trace_nfs4_exchange_id(clp, status);
8924 	rpc_put_task(task);
8925 	return status;
8926 }
8927 
8928 /*
8929  * nfs4_proc_exchange_id()
8930  *
8931  * Returns zero, a negative errno, or a negative NFS4ERR status code.
8932  *
8933  * Since the clientid has expired, all compounds using sessions
8934  * associated with the stale clientid will be returning
8935  * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8936  * be in some phase of session reset.
8937  *
8938  * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8939  */
8940 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8941 {
8942 	rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8943 	int status;
8944 
8945 	/* try SP4_MACH_CRED if krb5i/p	*/
8946 	if (authflavor == RPC_AUTH_GSS_KRB5I ||
8947 	    authflavor == RPC_AUTH_GSS_KRB5P) {
8948 		status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8949 		if (!status)
8950 			return 0;
8951 	}
8952 
8953 	/* try SP4_NONE */
8954 	return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8955 }
8956 
8957 /**
8958  * nfs4_test_session_trunk
8959  *
8960  * This is an add_xprt_test() test function called from
8961  * rpc_clnt_setup_test_and_add_xprt.
8962  *
8963  * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8964  * and is dereferrenced in nfs4_exchange_id_release
8965  *
8966  * Upon success, add the new transport to the rpc_clnt
8967  *
8968  * @clnt: struct rpc_clnt to get new transport
8969  * @xprt: the rpc_xprt to test
8970  * @data: call data for _nfs4_proc_exchange_id.
8971  */
8972 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8973 			    void *data)
8974 {
8975 	struct nfs4_add_xprt_data *adata = data;
8976 	struct rpc_task *task;
8977 	int status;
8978 
8979 	u32 sp4_how;
8980 
8981 	dprintk("--> %s try %s\n", __func__,
8982 		xprt->address_strings[RPC_DISPLAY_ADDR]);
8983 
8984 	sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8985 
8986 try_again:
8987 	/* Test connection for session trunking. Async exchange_id call */
8988 	task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8989 	if (IS_ERR(task))
8990 		return;
8991 
8992 	status = task->tk_status;
8993 	if (status == 0) {
8994 		status = nfs4_detect_session_trunking(adata->clp,
8995 				task->tk_msg.rpc_resp, xprt);
8996 		trace_nfs4_trunked_exchange_id(adata->clp,
8997 			xprt->address_strings[RPC_DISPLAY_ADDR], status);
8998 	}
8999 	if (status == 0)
9000 		rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
9001 	else if (status != -NFS4ERR_DELAY && rpc_clnt_xprt_switch_has_addr(clnt,
9002 				(struct sockaddr *)&xprt->addr))
9003 		rpc_clnt_xprt_switch_remove_xprt(clnt, xprt);
9004 
9005 	rpc_put_task(task);
9006 	if (status == -NFS4ERR_DELAY) {
9007 		ssleep(1);
9008 		goto try_again;
9009 	}
9010 }
9011 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
9012 
9013 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
9014 		const struct cred *cred)
9015 {
9016 	struct rpc_message msg = {
9017 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
9018 		.rpc_argp = clp,
9019 		.rpc_cred = cred,
9020 	};
9021 	int status;
9022 
9023 	status = rpc_call_sync(clp->cl_rpcclient, &msg,
9024 			       RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9025 	trace_nfs4_destroy_clientid(clp, status);
9026 	if (status)
9027 		dprintk("NFS: Got error %d from the server %s on "
9028 			"DESTROY_CLIENTID.", status, clp->cl_hostname);
9029 	return status;
9030 }
9031 
9032 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
9033 		const struct cred *cred)
9034 {
9035 	unsigned int loop;
9036 	int ret;
9037 
9038 	for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
9039 		ret = _nfs4_proc_destroy_clientid(clp, cred);
9040 		switch (ret) {
9041 		case -NFS4ERR_DELAY:
9042 		case -NFS4ERR_CLIENTID_BUSY:
9043 			ssleep(1);
9044 			break;
9045 		default:
9046 			return ret;
9047 		}
9048 	}
9049 	return 0;
9050 }
9051 
9052 int nfs4_destroy_clientid(struct nfs_client *clp)
9053 {
9054 	const struct cred *cred;
9055 	int ret = 0;
9056 
9057 	if (clp->cl_mvops->minor_version < 1)
9058 		goto out;
9059 	if (clp->cl_exchange_flags == 0)
9060 		goto out;
9061 	if (clp->cl_preserve_clid)
9062 		goto out;
9063 	cred = nfs4_get_clid_cred(clp);
9064 	ret = nfs4_proc_destroy_clientid(clp, cred);
9065 	put_cred(cred);
9066 	switch (ret) {
9067 	case 0:
9068 	case -NFS4ERR_STALE_CLIENTID:
9069 		clp->cl_exchange_flags = 0;
9070 	}
9071 out:
9072 	return ret;
9073 }
9074 
9075 #endif /* CONFIG_NFS_V4_1 */
9076 
9077 struct nfs4_get_lease_time_data {
9078 	struct nfs4_get_lease_time_args *args;
9079 	struct nfs4_get_lease_time_res *res;
9080 	struct nfs_client *clp;
9081 };
9082 
9083 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
9084 					void *calldata)
9085 {
9086 	struct nfs4_get_lease_time_data *data =
9087 			(struct nfs4_get_lease_time_data *)calldata;
9088 
9089 	/* just setup sequence, do not trigger session recovery
9090 	   since we're invoked within one */
9091 	nfs4_setup_sequence(data->clp,
9092 			&data->args->la_seq_args,
9093 			&data->res->lr_seq_res,
9094 			task);
9095 }
9096 
9097 /*
9098  * Called from nfs4_state_manager thread for session setup, so don't recover
9099  * from sequence operation or clientid errors.
9100  */
9101 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
9102 {
9103 	struct nfs4_get_lease_time_data *data =
9104 			(struct nfs4_get_lease_time_data *)calldata;
9105 
9106 	if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
9107 		return;
9108 	switch (task->tk_status) {
9109 	case -NFS4ERR_DELAY:
9110 	case -NFS4ERR_GRACE:
9111 		rpc_delay(task, NFS4_POLL_RETRY_MIN);
9112 		task->tk_status = 0;
9113 		fallthrough;
9114 	case -NFS4ERR_RETRY_UNCACHED_REP:
9115 		rpc_restart_call_prepare(task);
9116 		return;
9117 	}
9118 }
9119 
9120 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
9121 	.rpc_call_prepare = nfs4_get_lease_time_prepare,
9122 	.rpc_call_done = nfs4_get_lease_time_done,
9123 };
9124 
9125 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
9126 {
9127 	struct nfs4_get_lease_time_args args;
9128 	struct nfs4_get_lease_time_res res = {
9129 		.lr_fsinfo = fsinfo,
9130 	};
9131 	struct nfs4_get_lease_time_data data = {
9132 		.args = &args,
9133 		.res = &res,
9134 		.clp = clp,
9135 	};
9136 	struct rpc_message msg = {
9137 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
9138 		.rpc_argp = &args,
9139 		.rpc_resp = &res,
9140 	};
9141 	struct rpc_task_setup task_setup = {
9142 		.rpc_client = clp->cl_rpcclient,
9143 		.rpc_message = &msg,
9144 		.callback_ops = &nfs4_get_lease_time_ops,
9145 		.callback_data = &data,
9146 		.flags = RPC_TASK_TIMEOUT,
9147 	};
9148 
9149 	nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
9150 	return nfs4_call_sync_custom(&task_setup);
9151 }
9152 
9153 #ifdef CONFIG_NFS_V4_1
9154 
9155 /*
9156  * Initialize the values to be used by the client in CREATE_SESSION
9157  * If nfs4_init_session set the fore channel request and response sizes,
9158  * use them.
9159  *
9160  * Set the back channel max_resp_sz_cached to zero to force the client to
9161  * always set csa_cachethis to FALSE because the current implementation
9162  * of the back channel DRC only supports caching the CB_SEQUENCE operation.
9163  */
9164 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
9165 				    struct rpc_clnt *clnt)
9166 {
9167 	unsigned int max_rqst_sz, max_resp_sz;
9168 	unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
9169 	unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
9170 
9171 	max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
9172 	max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
9173 
9174 	/* Fore channel attributes */
9175 	args->fc_attrs.max_rqst_sz = max_rqst_sz;
9176 	args->fc_attrs.max_resp_sz = max_resp_sz;
9177 	args->fc_attrs.max_ops = NFS4_MAX_OPS;
9178 	args->fc_attrs.max_reqs = max_session_slots;
9179 
9180 	dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
9181 		"max_ops=%u max_reqs=%u\n",
9182 		__func__,
9183 		args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
9184 		args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
9185 
9186 	/* Back channel attributes */
9187 	args->bc_attrs.max_rqst_sz = max_bc_payload;
9188 	args->bc_attrs.max_resp_sz = max_bc_payload;
9189 	args->bc_attrs.max_resp_sz_cached = 0;
9190 	args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
9191 	args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
9192 	if (args->bc_attrs.max_reqs > max_bc_slots)
9193 		args->bc_attrs.max_reqs = max_bc_slots;
9194 
9195 	dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
9196 		"max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
9197 		__func__,
9198 		args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
9199 		args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
9200 		args->bc_attrs.max_reqs);
9201 }
9202 
9203 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
9204 		struct nfs41_create_session_res *res)
9205 {
9206 	struct nfs4_channel_attrs *sent = &args->fc_attrs;
9207 	struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
9208 
9209 	if (rcvd->max_resp_sz > sent->max_resp_sz)
9210 		return -EINVAL;
9211 	/*
9212 	 * Our requested max_ops is the minimum we need; we're not
9213 	 * prepared to break up compounds into smaller pieces than that.
9214 	 * So, no point even trying to continue if the server won't
9215 	 * cooperate:
9216 	 */
9217 	if (rcvd->max_ops < sent->max_ops)
9218 		return -EINVAL;
9219 	if (rcvd->max_reqs == 0)
9220 		return -EINVAL;
9221 	if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
9222 		rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
9223 	return 0;
9224 }
9225 
9226 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
9227 		struct nfs41_create_session_res *res)
9228 {
9229 	struct nfs4_channel_attrs *sent = &args->bc_attrs;
9230 	struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
9231 
9232 	if (!(res->flags & SESSION4_BACK_CHAN))
9233 		goto out;
9234 	if (rcvd->max_rqst_sz > sent->max_rqst_sz)
9235 		return -EINVAL;
9236 	if (rcvd->max_resp_sz < sent->max_resp_sz)
9237 		return -EINVAL;
9238 	if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
9239 		return -EINVAL;
9240 	if (rcvd->max_ops > sent->max_ops)
9241 		return -EINVAL;
9242 	if (rcvd->max_reqs > sent->max_reqs)
9243 		return -EINVAL;
9244 out:
9245 	return 0;
9246 }
9247 
9248 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
9249 				     struct nfs41_create_session_res *res)
9250 {
9251 	int ret;
9252 
9253 	ret = nfs4_verify_fore_channel_attrs(args, res);
9254 	if (ret)
9255 		return ret;
9256 	return nfs4_verify_back_channel_attrs(args, res);
9257 }
9258 
9259 static void nfs4_update_session(struct nfs4_session *session,
9260 		struct nfs41_create_session_res *res)
9261 {
9262 	nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
9263 	/* Mark client id and session as being confirmed */
9264 	session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
9265 	set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
9266 	session->flags = res->flags;
9267 	memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
9268 	if (res->flags & SESSION4_BACK_CHAN)
9269 		memcpy(&session->bc_attrs, &res->bc_attrs,
9270 				sizeof(session->bc_attrs));
9271 }
9272 
9273 static int _nfs4_proc_create_session(struct nfs_client *clp,
9274 		const struct cred *cred)
9275 {
9276 	struct nfs4_session *session = clp->cl_session;
9277 	struct nfs41_create_session_args args = {
9278 		.client = clp,
9279 		.clientid = clp->cl_clientid,
9280 		.seqid = clp->cl_seqid,
9281 		.cb_program = NFS4_CALLBACK,
9282 	};
9283 	struct nfs41_create_session_res res;
9284 
9285 	struct rpc_message msg = {
9286 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
9287 		.rpc_argp = &args,
9288 		.rpc_resp = &res,
9289 		.rpc_cred = cred,
9290 	};
9291 	int status;
9292 
9293 	nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
9294 	args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
9295 
9296 	status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9297 			       RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9298 	trace_nfs4_create_session(clp, status);
9299 
9300 	switch (status) {
9301 	case -NFS4ERR_STALE_CLIENTID:
9302 	case -NFS4ERR_DELAY:
9303 	case -ETIMEDOUT:
9304 	case -EACCES:
9305 	case -EAGAIN:
9306 		goto out;
9307 	}
9308 
9309 	clp->cl_seqid++;
9310 	if (!status) {
9311 		/* Verify the session's negotiated channel_attrs values */
9312 		status = nfs4_verify_channel_attrs(&args, &res);
9313 		/* Increment the clientid slot sequence id */
9314 		if (status)
9315 			goto out;
9316 		nfs4_update_session(session, &res);
9317 	}
9318 out:
9319 	return status;
9320 }
9321 
9322 /*
9323  * Issues a CREATE_SESSION operation to the server.
9324  * It is the responsibility of the caller to verify the session is
9325  * expired before calling this routine.
9326  */
9327 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
9328 {
9329 	int status;
9330 	unsigned *ptr;
9331 	struct nfs4_session *session = clp->cl_session;
9332 	struct nfs4_add_xprt_data xprtdata = {
9333 		.clp = clp,
9334 	};
9335 	struct rpc_add_xprt_test rpcdata = {
9336 		.add_xprt_test = clp->cl_mvops->session_trunk,
9337 		.data = &xprtdata,
9338 	};
9339 
9340 	dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9341 
9342 	status = _nfs4_proc_create_session(clp, cred);
9343 	if (status)
9344 		goto out;
9345 
9346 	/* Init or reset the session slot tables */
9347 	status = nfs4_setup_session_slot_tables(session);
9348 	dprintk("slot table setup returned %d\n", status);
9349 	if (status)
9350 		goto out;
9351 
9352 	ptr = (unsigned *)&session->sess_id.data[0];
9353 	dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9354 		clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
9355 	rpc_clnt_probe_trunked_xprts(clp->cl_rpcclient, &rpcdata);
9356 out:
9357 	return status;
9358 }
9359 
9360 /*
9361  * Issue the over-the-wire RPC DESTROY_SESSION.
9362  * The caller must serialize access to this routine.
9363  */
9364 int nfs4_proc_destroy_session(struct nfs4_session *session,
9365 		const struct cred *cred)
9366 {
9367 	struct rpc_message msg = {
9368 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9369 		.rpc_argp = session,
9370 		.rpc_cred = cred,
9371 	};
9372 	int status = 0;
9373 
9374 	/* session is still being setup */
9375 	if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9376 		return 0;
9377 
9378 	status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9379 			       RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9380 	trace_nfs4_destroy_session(session->clp, status);
9381 
9382 	if (status)
9383 		dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
9384 			"Session has been destroyed regardless...\n", status);
9385 	rpc_clnt_manage_trunked_xprts(session->clp->cl_rpcclient);
9386 	return status;
9387 }
9388 
9389 /*
9390  * Renew the cl_session lease.
9391  */
9392 struct nfs4_sequence_data {
9393 	struct nfs_client *clp;
9394 	struct nfs4_sequence_args args;
9395 	struct nfs4_sequence_res res;
9396 };
9397 
9398 static void nfs41_sequence_release(void *data)
9399 {
9400 	struct nfs4_sequence_data *calldata = data;
9401 	struct nfs_client *clp = calldata->clp;
9402 
9403 	if (refcount_read(&clp->cl_count) > 1)
9404 		nfs4_schedule_state_renewal(clp);
9405 	nfs_put_client(clp);
9406 	kfree(calldata);
9407 }
9408 
9409 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9410 {
9411 	switch(task->tk_status) {
9412 	case -NFS4ERR_DELAY:
9413 		rpc_delay(task, NFS4_POLL_RETRY_MAX);
9414 		return -EAGAIN;
9415 	default:
9416 		nfs4_schedule_lease_recovery(clp);
9417 	}
9418 	return 0;
9419 }
9420 
9421 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
9422 {
9423 	struct nfs4_sequence_data *calldata = data;
9424 	struct nfs_client *clp = calldata->clp;
9425 
9426 	if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9427 		return;
9428 
9429 	trace_nfs4_sequence(clp, task->tk_status);
9430 	if (task->tk_status < 0 && !task->tk_client->cl_shutdown) {
9431 		dprintk("%s ERROR %d\n", __func__, task->tk_status);
9432 		if (refcount_read(&clp->cl_count) == 1)
9433 			return;
9434 
9435 		if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9436 			rpc_restart_call_prepare(task);
9437 			return;
9438 		}
9439 	}
9440 	dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
9441 }
9442 
9443 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9444 {
9445 	struct nfs4_sequence_data *calldata = data;
9446 	struct nfs_client *clp = calldata->clp;
9447 	struct nfs4_sequence_args *args;
9448 	struct nfs4_sequence_res *res;
9449 
9450 	args = task->tk_msg.rpc_argp;
9451 	res = task->tk_msg.rpc_resp;
9452 
9453 	nfs4_setup_sequence(clp, args, res, task);
9454 }
9455 
9456 static const struct rpc_call_ops nfs41_sequence_ops = {
9457 	.rpc_call_done = nfs41_sequence_call_done,
9458 	.rpc_call_prepare = nfs41_sequence_prepare,
9459 	.rpc_release = nfs41_sequence_release,
9460 };
9461 
9462 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9463 		const struct cred *cred,
9464 		struct nfs4_slot *slot,
9465 		bool is_privileged)
9466 {
9467 	struct nfs4_sequence_data *calldata;
9468 	struct rpc_message msg = {
9469 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9470 		.rpc_cred = cred,
9471 	};
9472 	struct rpc_task_setup task_setup_data = {
9473 		.rpc_client = clp->cl_rpcclient,
9474 		.rpc_message = &msg,
9475 		.callback_ops = &nfs41_sequence_ops,
9476 		.flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
9477 	};
9478 	struct rpc_task *ret;
9479 
9480 	ret = ERR_PTR(-EIO);
9481 	if (!refcount_inc_not_zero(&clp->cl_count))
9482 		goto out_err;
9483 
9484 	ret = ERR_PTR(-ENOMEM);
9485 	calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
9486 	if (calldata == NULL)
9487 		goto out_put_clp;
9488 	nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9489 	nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9490 	msg.rpc_argp = &calldata->args;
9491 	msg.rpc_resp = &calldata->res;
9492 	calldata->clp = clp;
9493 	task_setup_data.callback_data = calldata;
9494 
9495 	ret = rpc_run_task(&task_setup_data);
9496 	if (IS_ERR(ret))
9497 		goto out_err;
9498 	return ret;
9499 out_put_clp:
9500 	nfs_put_client(clp);
9501 out_err:
9502 	nfs41_release_slot(slot);
9503 	return ret;
9504 }
9505 
9506 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9507 {
9508 	struct rpc_task *task;
9509 	int ret = 0;
9510 
9511 	if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9512 		return -EAGAIN;
9513 	task = _nfs41_proc_sequence(clp, cred, NULL, false);
9514 	if (IS_ERR(task))
9515 		ret = PTR_ERR(task);
9516 	else
9517 		rpc_put_task_async(task);
9518 	dprintk("<-- %s status=%d\n", __func__, ret);
9519 	return ret;
9520 }
9521 
9522 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9523 {
9524 	struct rpc_task *task;
9525 	int ret;
9526 
9527 	task = _nfs41_proc_sequence(clp, cred, NULL, true);
9528 	if (IS_ERR(task)) {
9529 		ret = PTR_ERR(task);
9530 		goto out;
9531 	}
9532 	ret = rpc_wait_for_completion_task(task);
9533 	if (!ret)
9534 		ret = task->tk_status;
9535 	rpc_put_task(task);
9536 out:
9537 	dprintk("<-- %s status=%d\n", __func__, ret);
9538 	return ret;
9539 }
9540 
9541 struct nfs4_reclaim_complete_data {
9542 	struct nfs_client *clp;
9543 	struct nfs41_reclaim_complete_args arg;
9544 	struct nfs41_reclaim_complete_res res;
9545 };
9546 
9547 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9548 {
9549 	struct nfs4_reclaim_complete_data *calldata = data;
9550 
9551 	nfs4_setup_sequence(calldata->clp,
9552 			&calldata->arg.seq_args,
9553 			&calldata->res.seq_res,
9554 			task);
9555 }
9556 
9557 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9558 {
9559 	switch(task->tk_status) {
9560 	case 0:
9561 		wake_up_all(&clp->cl_lock_waitq);
9562 		fallthrough;
9563 	case -NFS4ERR_COMPLETE_ALREADY:
9564 	case -NFS4ERR_WRONG_CRED: /* What to do here? */
9565 		break;
9566 	case -NFS4ERR_DELAY:
9567 		rpc_delay(task, NFS4_POLL_RETRY_MAX);
9568 		fallthrough;
9569 	case -NFS4ERR_RETRY_UNCACHED_REP:
9570 	case -EACCES:
9571 		dprintk("%s: failed to reclaim complete error %d for server %s, retrying\n",
9572 			__func__, task->tk_status, clp->cl_hostname);
9573 		return -EAGAIN;
9574 	case -NFS4ERR_BADSESSION:
9575 	case -NFS4ERR_DEADSESSION:
9576 	case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9577 		break;
9578 	default:
9579 		nfs4_schedule_lease_recovery(clp);
9580 	}
9581 	return 0;
9582 }
9583 
9584 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9585 {
9586 	struct nfs4_reclaim_complete_data *calldata = data;
9587 	struct nfs_client *clp = calldata->clp;
9588 	struct nfs4_sequence_res *res = &calldata->res.seq_res;
9589 
9590 	if (!nfs41_sequence_done(task, res))
9591 		return;
9592 
9593 	trace_nfs4_reclaim_complete(clp, task->tk_status);
9594 	if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9595 		rpc_restart_call_prepare(task);
9596 		return;
9597 	}
9598 }
9599 
9600 static void nfs4_free_reclaim_complete_data(void *data)
9601 {
9602 	struct nfs4_reclaim_complete_data *calldata = data;
9603 
9604 	kfree(calldata);
9605 }
9606 
9607 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9608 	.rpc_call_prepare = nfs4_reclaim_complete_prepare,
9609 	.rpc_call_done = nfs4_reclaim_complete_done,
9610 	.rpc_release = nfs4_free_reclaim_complete_data,
9611 };
9612 
9613 /*
9614  * Issue a global reclaim complete.
9615  */
9616 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9617 		const struct cred *cred)
9618 {
9619 	struct nfs4_reclaim_complete_data *calldata;
9620 	struct rpc_message msg = {
9621 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9622 		.rpc_cred = cred,
9623 	};
9624 	struct rpc_task_setup task_setup_data = {
9625 		.rpc_client = clp->cl_rpcclient,
9626 		.rpc_message = &msg,
9627 		.callback_ops = &nfs4_reclaim_complete_call_ops,
9628 		.flags = RPC_TASK_NO_ROUND_ROBIN,
9629 	};
9630 	int status = -ENOMEM;
9631 
9632 	calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9633 	if (calldata == NULL)
9634 		goto out;
9635 	calldata->clp = clp;
9636 	calldata->arg.one_fs = 0;
9637 
9638 	nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9639 	msg.rpc_argp = &calldata->arg;
9640 	msg.rpc_resp = &calldata->res;
9641 	task_setup_data.callback_data = calldata;
9642 	status = nfs4_call_sync_custom(&task_setup_data);
9643 out:
9644 	dprintk("<-- %s status=%d\n", __func__, status);
9645 	return status;
9646 }
9647 
9648 static void
9649 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9650 {
9651 	struct nfs4_layoutget *lgp = calldata;
9652 	struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9653 
9654 	nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9655 				&lgp->res.seq_res, task);
9656 }
9657 
9658 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9659 {
9660 	struct nfs4_layoutget *lgp = calldata;
9661 
9662 	nfs41_sequence_process(task, &lgp->res.seq_res);
9663 }
9664 
9665 static int
9666 nfs4_layoutget_handle_exception(struct rpc_task *task,
9667 		struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9668 {
9669 	struct inode *inode = lgp->args.inode;
9670 	struct nfs_server *server = NFS_SERVER(inode);
9671 	struct pnfs_layout_hdr *lo = lgp->lo;
9672 	int nfs4err = task->tk_status;
9673 	int err, status = 0;
9674 	LIST_HEAD(head);
9675 
9676 	dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9677 
9678 	nfs4_sequence_free_slot(&lgp->res.seq_res);
9679 
9680 	exception->state = NULL;
9681 	exception->stateid = NULL;
9682 
9683 	switch (nfs4err) {
9684 	case 0:
9685 		goto out;
9686 
9687 	/*
9688 	 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9689 	 * on the file. set tk_status to -ENODATA to tell upper layer to
9690 	 * retry go inband.
9691 	 */
9692 	case -NFS4ERR_LAYOUTUNAVAILABLE:
9693 		status = -ENODATA;
9694 		goto out;
9695 	/*
9696 	 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9697 	 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9698 	 */
9699 	case -NFS4ERR_BADLAYOUT:
9700 		status = -EOVERFLOW;
9701 		goto out;
9702 	/*
9703 	 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9704 	 * (or clients) writing to the same RAID stripe except when
9705 	 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9706 	 *
9707 	 * Treat it like we would RECALLCONFLICT -- we retry for a little
9708 	 * while, and then eventually give up.
9709 	 */
9710 	case -NFS4ERR_LAYOUTTRYLATER:
9711 		if (lgp->args.minlength == 0) {
9712 			status = -EOVERFLOW;
9713 			goto out;
9714 		}
9715 		status = -EBUSY;
9716 		break;
9717 	case -NFS4ERR_RECALLCONFLICT:
9718 	case -NFS4ERR_RETURNCONFLICT:
9719 		status = -ERECALLCONFLICT;
9720 		break;
9721 	case -NFS4ERR_DELEG_REVOKED:
9722 	case -NFS4ERR_ADMIN_REVOKED:
9723 	case -NFS4ERR_EXPIRED:
9724 	case -NFS4ERR_BAD_STATEID:
9725 		exception->timeout = 0;
9726 		spin_lock(&inode->i_lock);
9727 		/* If the open stateid was bad, then recover it. */
9728 		if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9729 		    !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9730 			spin_unlock(&inode->i_lock);
9731 			exception->state = lgp->args.ctx->state;
9732 			exception->stateid = &lgp->args.stateid;
9733 			break;
9734 		}
9735 
9736 		/*
9737 		 * Mark the bad layout state as invalid, then retry
9738 		 */
9739 		pnfs_mark_layout_stateid_invalid(lo, &head);
9740 		spin_unlock(&inode->i_lock);
9741 		nfs_commit_inode(inode, 0);
9742 		pnfs_free_lseg_list(&head);
9743 		status = -EAGAIN;
9744 		goto out;
9745 	}
9746 
9747 	err = nfs4_handle_exception(server, nfs4err, exception);
9748 	if (!status) {
9749 		if (exception->retry)
9750 			status = -EAGAIN;
9751 		else
9752 			status = err;
9753 	}
9754 out:
9755 	return status;
9756 }
9757 
9758 size_t max_response_pages(struct nfs_server *server)
9759 {
9760 	u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9761 	return nfs_page_array_len(0, max_resp_sz);
9762 }
9763 
9764 static void nfs4_layoutget_release(void *calldata)
9765 {
9766 	struct nfs4_layoutget *lgp = calldata;
9767 
9768 	nfs4_sequence_free_slot(&lgp->res.seq_res);
9769 	pnfs_layoutget_free(lgp);
9770 }
9771 
9772 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9773 	.rpc_call_prepare = nfs4_layoutget_prepare,
9774 	.rpc_call_done = nfs4_layoutget_done,
9775 	.rpc_release = nfs4_layoutget_release,
9776 };
9777 
9778 struct pnfs_layout_segment *
9779 nfs4_proc_layoutget(struct nfs4_layoutget *lgp,
9780 		    struct nfs4_exception *exception)
9781 {
9782 	struct inode *inode = lgp->args.inode;
9783 	struct nfs_server *server = NFS_SERVER(inode);
9784 	struct rpc_task *task;
9785 	struct rpc_message msg = {
9786 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9787 		.rpc_argp = &lgp->args,
9788 		.rpc_resp = &lgp->res,
9789 		.rpc_cred = lgp->cred,
9790 	};
9791 	struct rpc_task_setup task_setup_data = {
9792 		.rpc_client = server->client,
9793 		.rpc_message = &msg,
9794 		.callback_ops = &nfs4_layoutget_call_ops,
9795 		.callback_data = lgp,
9796 		.flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF |
9797 			 RPC_TASK_MOVEABLE,
9798 	};
9799 	struct pnfs_layout_segment *lseg = NULL;
9800 	int status = 0;
9801 
9802 	nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9803 	exception->retry = 0;
9804 
9805 	task = rpc_run_task(&task_setup_data);
9806 	if (IS_ERR(task))
9807 		return ERR_CAST(task);
9808 
9809 	status = rpc_wait_for_completion_task(task);
9810 	if (status != 0)
9811 		goto out;
9812 
9813 	if (task->tk_status < 0) {
9814 		exception->retry = 1;
9815 		status = nfs4_layoutget_handle_exception(task, lgp, exception);
9816 	} else if (lgp->res.layoutp->len == 0) {
9817 		exception->retry = 1;
9818 		status = -EAGAIN;
9819 		nfs4_update_delay(&exception->timeout);
9820 	} else
9821 		lseg = pnfs_layout_process(lgp);
9822 out:
9823 	trace_nfs4_layoutget(lgp->args.ctx,
9824 			&lgp->args.range,
9825 			&lgp->res.range,
9826 			&lgp->res.stateid,
9827 			status);
9828 
9829 	rpc_put_task(task);
9830 	dprintk("<-- %s status=%d\n", __func__, status);
9831 	if (status)
9832 		return ERR_PTR(status);
9833 	return lseg;
9834 }
9835 
9836 static void
9837 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9838 {
9839 	struct nfs4_layoutreturn *lrp = calldata;
9840 
9841 	nfs4_setup_sequence(lrp->clp,
9842 			&lrp->args.seq_args,
9843 			&lrp->res.seq_res,
9844 			task);
9845 	if (!pnfs_layout_is_valid(lrp->args.layout))
9846 		rpc_exit(task, 0);
9847 }
9848 
9849 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9850 {
9851 	struct nfs4_layoutreturn *lrp = calldata;
9852 	struct nfs_server *server;
9853 
9854 	if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9855 		return;
9856 
9857 	/*
9858 	 * Was there an RPC level error? Assume the call succeeded,
9859 	 * and that we need to release the layout
9860 	 */
9861 	if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9862 		lrp->res.lrs_present = 0;
9863 		return;
9864 	}
9865 
9866 	server = NFS_SERVER(lrp->args.inode);
9867 	switch (task->tk_status) {
9868 	case -NFS4ERR_OLD_STATEID:
9869 		if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9870 					&lrp->args.range,
9871 					lrp->args.inode))
9872 			goto out_restart;
9873 		fallthrough;
9874 	default:
9875 		task->tk_status = 0;
9876 		fallthrough;
9877 	case 0:
9878 		break;
9879 	case -NFS4ERR_DELAY:
9880 		if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9881 			break;
9882 		goto out_restart;
9883 	}
9884 	return;
9885 out_restart:
9886 	task->tk_status = 0;
9887 	nfs4_sequence_free_slot(&lrp->res.seq_res);
9888 	rpc_restart_call_prepare(task);
9889 }
9890 
9891 static void nfs4_layoutreturn_release(void *calldata)
9892 {
9893 	struct nfs4_layoutreturn *lrp = calldata;
9894 	struct pnfs_layout_hdr *lo = lrp->args.layout;
9895 
9896 	pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9897 			lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9898 	nfs4_sequence_free_slot(&lrp->res.seq_res);
9899 	if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9900 		lrp->ld_private.ops->free(&lrp->ld_private);
9901 	pnfs_put_layout_hdr(lrp->args.layout);
9902 	nfs_iput_and_deactive(lrp->inode);
9903 	put_cred(lrp->cred);
9904 	kfree(calldata);
9905 }
9906 
9907 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9908 	.rpc_call_prepare = nfs4_layoutreturn_prepare,
9909 	.rpc_call_done = nfs4_layoutreturn_done,
9910 	.rpc_release = nfs4_layoutreturn_release,
9911 };
9912 
9913 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9914 {
9915 	struct rpc_task *task;
9916 	struct rpc_message msg = {
9917 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9918 		.rpc_argp = &lrp->args,
9919 		.rpc_resp = &lrp->res,
9920 		.rpc_cred = lrp->cred,
9921 	};
9922 	struct rpc_task_setup task_setup_data = {
9923 		.rpc_client = NFS_SERVER(lrp->args.inode)->client,
9924 		.rpc_message = &msg,
9925 		.callback_ops = &nfs4_layoutreturn_call_ops,
9926 		.callback_data = lrp,
9927 		.flags = RPC_TASK_MOVEABLE,
9928 	};
9929 	int status = 0;
9930 
9931 	nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9932 			NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9933 			&task_setup_data.rpc_client, &msg);
9934 
9935 	lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9936 	if (!sync) {
9937 		if (!lrp->inode) {
9938 			nfs4_layoutreturn_release(lrp);
9939 			return -EAGAIN;
9940 		}
9941 		task_setup_data.flags |= RPC_TASK_ASYNC;
9942 	}
9943 	if (!lrp->inode)
9944 		nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9945 				   1);
9946 	else
9947 		nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9948 				   0);
9949 	task = rpc_run_task(&task_setup_data);
9950 	if (IS_ERR(task))
9951 		return PTR_ERR(task);
9952 	if (sync)
9953 		status = task->tk_status;
9954 	trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9955 	dprintk("<-- %s status=%d\n", __func__, status);
9956 	rpc_put_task(task);
9957 	return status;
9958 }
9959 
9960 static int
9961 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9962 		struct pnfs_device *pdev,
9963 		const struct cred *cred)
9964 {
9965 	struct nfs4_getdeviceinfo_args args = {
9966 		.pdev = pdev,
9967 		.notify_types = NOTIFY_DEVICEID4_CHANGE |
9968 			NOTIFY_DEVICEID4_DELETE,
9969 	};
9970 	struct nfs4_getdeviceinfo_res res = {
9971 		.pdev = pdev,
9972 	};
9973 	struct rpc_message msg = {
9974 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9975 		.rpc_argp = &args,
9976 		.rpc_resp = &res,
9977 		.rpc_cred = cred,
9978 	};
9979 	int status;
9980 
9981 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9982 	if (res.notification & ~args.notify_types)
9983 		dprintk("%s: unsupported notification\n", __func__);
9984 	if (res.notification != args.notify_types)
9985 		pdev->nocache = 1;
9986 
9987 	trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9988 
9989 	dprintk("<-- %s status=%d\n", __func__, status);
9990 
9991 	return status;
9992 }
9993 
9994 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9995 		struct pnfs_device *pdev,
9996 		const struct cred *cred)
9997 {
9998 	struct nfs4_exception exception = { };
9999 	int err;
10000 
10001 	do {
10002 		err = nfs4_handle_exception(server,
10003 					_nfs4_proc_getdeviceinfo(server, pdev, cred),
10004 					&exception);
10005 	} while (exception.retry);
10006 	return err;
10007 }
10008 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
10009 
10010 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
10011 {
10012 	struct nfs4_layoutcommit_data *data = calldata;
10013 	struct nfs_server *server = NFS_SERVER(data->args.inode);
10014 
10015 	nfs4_setup_sequence(server->nfs_client,
10016 			&data->args.seq_args,
10017 			&data->res.seq_res,
10018 			task);
10019 }
10020 
10021 static void
10022 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
10023 {
10024 	struct nfs4_layoutcommit_data *data = calldata;
10025 	struct nfs_server *server = NFS_SERVER(data->args.inode);
10026 
10027 	if (!nfs41_sequence_done(task, &data->res.seq_res))
10028 		return;
10029 
10030 	switch (task->tk_status) { /* Just ignore these failures */
10031 	case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
10032 	case -NFS4ERR_BADIOMODE:     /* no IOMODE_RW layout for range */
10033 	case -NFS4ERR_BADLAYOUT:     /* no layout */
10034 	case -NFS4ERR_GRACE:	    /* loca_recalim always false */
10035 		task->tk_status = 0;
10036 		break;
10037 	case 0:
10038 		break;
10039 	default:
10040 		if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
10041 			rpc_restart_call_prepare(task);
10042 			return;
10043 		}
10044 	}
10045 }
10046 
10047 static void nfs4_layoutcommit_release(void *calldata)
10048 {
10049 	struct nfs4_layoutcommit_data *data = calldata;
10050 
10051 	pnfs_cleanup_layoutcommit(data);
10052 	nfs_post_op_update_inode_force_wcc(data->args.inode,
10053 					   data->res.fattr);
10054 	put_cred(data->cred);
10055 	nfs_iput_and_deactive(data->inode);
10056 	kfree(data);
10057 }
10058 
10059 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
10060 	.rpc_call_prepare = nfs4_layoutcommit_prepare,
10061 	.rpc_call_done = nfs4_layoutcommit_done,
10062 	.rpc_release = nfs4_layoutcommit_release,
10063 };
10064 
10065 int
10066 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
10067 {
10068 	struct rpc_message msg = {
10069 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
10070 		.rpc_argp = &data->args,
10071 		.rpc_resp = &data->res,
10072 		.rpc_cred = data->cred,
10073 	};
10074 	struct rpc_task_setup task_setup_data = {
10075 		.task = &data->task,
10076 		.rpc_client = NFS_CLIENT(data->args.inode),
10077 		.rpc_message = &msg,
10078 		.callback_ops = &nfs4_layoutcommit_ops,
10079 		.callback_data = data,
10080 		.flags = RPC_TASK_MOVEABLE,
10081 	};
10082 	struct rpc_task *task;
10083 	int status = 0;
10084 
10085 	dprintk("NFS: initiating layoutcommit call. sync %d "
10086 		"lbw: %llu inode %lu\n", sync,
10087 		data->args.lastbytewritten,
10088 		data->args.inode->i_ino);
10089 
10090 	if (!sync) {
10091 		data->inode = nfs_igrab_and_active(data->args.inode);
10092 		if (data->inode == NULL) {
10093 			nfs4_layoutcommit_release(data);
10094 			return -EAGAIN;
10095 		}
10096 		task_setup_data.flags = RPC_TASK_ASYNC;
10097 	}
10098 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
10099 	task = rpc_run_task(&task_setup_data);
10100 	if (IS_ERR(task))
10101 		return PTR_ERR(task);
10102 	if (sync)
10103 		status = task->tk_status;
10104 	trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
10105 	dprintk("%s: status %d\n", __func__, status);
10106 	rpc_put_task(task);
10107 	return status;
10108 }
10109 
10110 /*
10111  * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
10112  * possible) as per RFC3530bis and RFC5661 Security Considerations sections
10113  */
10114 static int
10115 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10116 		    struct nfs_fsinfo *info,
10117 		    struct nfs4_secinfo_flavors *flavors, bool use_integrity)
10118 {
10119 	struct nfs41_secinfo_no_name_args args = {
10120 		.style = SECINFO_STYLE_CURRENT_FH,
10121 	};
10122 	struct nfs4_secinfo_res res = {
10123 		.flavors = flavors,
10124 	};
10125 	struct rpc_message msg = {
10126 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
10127 		.rpc_argp = &args,
10128 		.rpc_resp = &res,
10129 	};
10130 	struct nfs4_call_sync_data data = {
10131 		.seq_server = server,
10132 		.seq_args = &args.seq_args,
10133 		.seq_res = &res.seq_res,
10134 	};
10135 	struct rpc_task_setup task_setup = {
10136 		.rpc_client = server->client,
10137 		.rpc_message = &msg,
10138 		.callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
10139 		.callback_data = &data,
10140 		.flags = RPC_TASK_NO_ROUND_ROBIN,
10141 	};
10142 	const struct cred *cred = NULL;
10143 	int status;
10144 
10145 	if (use_integrity) {
10146 		task_setup.rpc_client = server->nfs_client->cl_rpcclient;
10147 
10148 		cred = nfs4_get_clid_cred(server->nfs_client);
10149 		msg.rpc_cred = cred;
10150 	}
10151 
10152 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
10153 	status = nfs4_call_sync_custom(&task_setup);
10154 	dprintk("<-- %s status=%d\n", __func__, status);
10155 
10156 	put_cred(cred);
10157 
10158 	return status;
10159 }
10160 
10161 static int
10162 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10163 			   struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
10164 {
10165 	struct nfs4_exception exception = {
10166 		.interruptible = true,
10167 	};
10168 	int err;
10169 	do {
10170 		/* first try using integrity protection */
10171 		err = -NFS4ERR_WRONGSEC;
10172 
10173 		/* try to use integrity protection with machine cred */
10174 		if (_nfs4_is_integrity_protected(server->nfs_client))
10175 			err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10176 							  flavors, true);
10177 
10178 		/*
10179 		 * if unable to use integrity protection, or SECINFO with
10180 		 * integrity protection returns NFS4ERR_WRONGSEC (which is
10181 		 * disallowed by spec, but exists in deployed servers) use
10182 		 * the current filesystem's rpc_client and the user cred.
10183 		 */
10184 		if (err == -NFS4ERR_WRONGSEC)
10185 			err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10186 							  flavors, false);
10187 
10188 		switch (err) {
10189 		case 0:
10190 		case -NFS4ERR_WRONGSEC:
10191 		case -ENOTSUPP:
10192 			goto out;
10193 		default:
10194 			err = nfs4_handle_exception(server, err, &exception);
10195 		}
10196 	} while (exception.retry);
10197 out:
10198 	return err;
10199 }
10200 
10201 static int
10202 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
10203 		    struct nfs_fsinfo *info)
10204 {
10205 	int err;
10206 	struct page *page;
10207 	rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
10208 	struct nfs4_secinfo_flavors *flavors;
10209 	struct nfs4_secinfo4 *secinfo;
10210 	int i;
10211 
10212 	page = alloc_page(GFP_KERNEL);
10213 	if (!page) {
10214 		err = -ENOMEM;
10215 		goto out;
10216 	}
10217 
10218 	flavors = page_address(page);
10219 	err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
10220 
10221 	/*
10222 	 * Fall back on "guess and check" method if
10223 	 * the server doesn't support SECINFO_NO_NAME
10224 	 */
10225 	if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
10226 		err = nfs4_find_root_sec(server, fhandle, info);
10227 		goto out_freepage;
10228 	}
10229 	if (err)
10230 		goto out_freepage;
10231 
10232 	for (i = 0; i < flavors->num_flavors; i++) {
10233 		secinfo = &flavors->flavors[i];
10234 
10235 		switch (secinfo->flavor) {
10236 		case RPC_AUTH_NULL:
10237 		case RPC_AUTH_UNIX:
10238 		case RPC_AUTH_GSS:
10239 			flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
10240 					&secinfo->flavor_info);
10241 			break;
10242 		default:
10243 			flavor = RPC_AUTH_MAXFLAVOR;
10244 			break;
10245 		}
10246 
10247 		if (!nfs_auth_info_match(&server->auth_info, flavor))
10248 			flavor = RPC_AUTH_MAXFLAVOR;
10249 
10250 		if (flavor != RPC_AUTH_MAXFLAVOR) {
10251 			err = nfs4_lookup_root_sec(server, fhandle,
10252 						   info, flavor);
10253 			if (!err)
10254 				break;
10255 		}
10256 	}
10257 
10258 	if (flavor == RPC_AUTH_MAXFLAVOR)
10259 		err = -EPERM;
10260 
10261 out_freepage:
10262 	put_page(page);
10263 	if (err == -EACCES)
10264 		return -EPERM;
10265 out:
10266 	return err;
10267 }
10268 
10269 static int _nfs41_test_stateid(struct nfs_server *server,
10270 		nfs4_stateid *stateid,
10271 		const struct cred *cred)
10272 {
10273 	int status;
10274 	struct nfs41_test_stateid_args args = {
10275 		.stateid = stateid,
10276 	};
10277 	struct nfs41_test_stateid_res res;
10278 	struct rpc_message msg = {
10279 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
10280 		.rpc_argp = &args,
10281 		.rpc_resp = &res,
10282 		.rpc_cred = cred,
10283 	};
10284 	struct rpc_clnt *rpc_client = server->client;
10285 
10286 	nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10287 		&rpc_client, &msg);
10288 
10289 	dprintk("NFS call  test_stateid %p\n", stateid);
10290 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
10291 	status = nfs4_call_sync_sequence(rpc_client, server, &msg,
10292 			&args.seq_args, &res.seq_res);
10293 	if (status != NFS_OK) {
10294 		dprintk("NFS reply test_stateid: failed, %d\n", status);
10295 		return status;
10296 	}
10297 	dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
10298 	return -res.status;
10299 }
10300 
10301 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10302 		int err, struct nfs4_exception *exception)
10303 {
10304 	exception->retry = 0;
10305 	switch(err) {
10306 	case -NFS4ERR_DELAY:
10307 	case -NFS4ERR_RETRY_UNCACHED_REP:
10308 		nfs4_handle_exception(server, err, exception);
10309 		break;
10310 	case -NFS4ERR_BADSESSION:
10311 	case -NFS4ERR_BADSLOT:
10312 	case -NFS4ERR_BAD_HIGH_SLOT:
10313 	case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10314 	case -NFS4ERR_DEADSESSION:
10315 		nfs4_do_handle_exception(server, err, exception);
10316 	}
10317 }
10318 
10319 /**
10320  * nfs41_test_stateid - perform a TEST_STATEID operation
10321  *
10322  * @server: server / transport on which to perform the operation
10323  * @stateid: state ID to test
10324  * @cred: credential
10325  *
10326  * Returns NFS_OK if the server recognizes that "stateid" is valid.
10327  * Otherwise a negative NFS4ERR value is returned if the operation
10328  * failed or the state ID is not currently valid.
10329  */
10330 static int nfs41_test_stateid(struct nfs_server *server,
10331 		nfs4_stateid *stateid,
10332 		const struct cred *cred)
10333 {
10334 	struct nfs4_exception exception = {
10335 		.interruptible = true,
10336 	};
10337 	int err;
10338 	do {
10339 		err = _nfs41_test_stateid(server, stateid, cred);
10340 		nfs4_handle_delay_or_session_error(server, err, &exception);
10341 	} while (exception.retry);
10342 	return err;
10343 }
10344 
10345 struct nfs_free_stateid_data {
10346 	struct nfs_server *server;
10347 	struct nfs41_free_stateid_args args;
10348 	struct nfs41_free_stateid_res res;
10349 };
10350 
10351 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10352 {
10353 	struct nfs_free_stateid_data *data = calldata;
10354 	nfs4_setup_sequence(data->server->nfs_client,
10355 			&data->args.seq_args,
10356 			&data->res.seq_res,
10357 			task);
10358 }
10359 
10360 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10361 {
10362 	struct nfs_free_stateid_data *data = calldata;
10363 
10364 	nfs41_sequence_done(task, &data->res.seq_res);
10365 
10366 	switch (task->tk_status) {
10367 	case -NFS4ERR_DELAY:
10368 		if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
10369 			rpc_restart_call_prepare(task);
10370 	}
10371 }
10372 
10373 static void nfs41_free_stateid_release(void *calldata)
10374 {
10375 	struct nfs_free_stateid_data *data = calldata;
10376 	struct nfs_client *clp = data->server->nfs_client;
10377 
10378 	nfs_put_client(clp);
10379 	kfree(calldata);
10380 }
10381 
10382 static const struct rpc_call_ops nfs41_free_stateid_ops = {
10383 	.rpc_call_prepare = nfs41_free_stateid_prepare,
10384 	.rpc_call_done = nfs41_free_stateid_done,
10385 	.rpc_release = nfs41_free_stateid_release,
10386 };
10387 
10388 /**
10389  * nfs41_free_stateid - perform a FREE_STATEID operation
10390  *
10391  * @server: server / transport on which to perform the operation
10392  * @stateid: state ID to release
10393  * @cred: credential
10394  * @privileged: set to true if this call needs to be privileged
10395  *
10396  * Note: this function is always asynchronous.
10397  */
10398 static int nfs41_free_stateid(struct nfs_server *server,
10399 		const nfs4_stateid *stateid,
10400 		const struct cred *cred,
10401 		bool privileged)
10402 {
10403 	struct rpc_message msg = {
10404 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
10405 		.rpc_cred = cred,
10406 	};
10407 	struct rpc_task_setup task_setup = {
10408 		.rpc_client = server->client,
10409 		.rpc_message = &msg,
10410 		.callback_ops = &nfs41_free_stateid_ops,
10411 		.flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE,
10412 	};
10413 	struct nfs_free_stateid_data *data;
10414 	struct rpc_task *task;
10415 	struct nfs_client *clp = server->nfs_client;
10416 
10417 	if (!refcount_inc_not_zero(&clp->cl_count))
10418 		return -EIO;
10419 
10420 	nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10421 		&task_setup.rpc_client, &msg);
10422 
10423 	dprintk("NFS call  free_stateid %p\n", stateid);
10424 	data = kmalloc(sizeof(*data), GFP_KERNEL);
10425 	if (!data)
10426 		return -ENOMEM;
10427 	data->server = server;
10428 	nfs4_stateid_copy(&data->args.stateid, stateid);
10429 
10430 	task_setup.callback_data = data;
10431 
10432 	msg.rpc_argp = &data->args;
10433 	msg.rpc_resp = &data->res;
10434 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
10435 	task = rpc_run_task(&task_setup);
10436 	if (IS_ERR(task))
10437 		return PTR_ERR(task);
10438 	rpc_put_task(task);
10439 	return 0;
10440 }
10441 
10442 static void
10443 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
10444 {
10445 	const struct cred *cred = lsp->ls_state->owner->so_cred;
10446 
10447 	nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10448 	nfs4_free_lock_state(server, lsp);
10449 }
10450 
10451 static bool nfs41_match_stateid(const nfs4_stateid *s1,
10452 		const nfs4_stateid *s2)
10453 {
10454 	if (s1->type != s2->type)
10455 		return false;
10456 
10457 	if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10458 		return false;
10459 
10460 	if (s1->seqid == s2->seqid)
10461 		return true;
10462 
10463 	return s1->seqid == 0 || s2->seqid == 0;
10464 }
10465 
10466 #endif /* CONFIG_NFS_V4_1 */
10467 
10468 static bool nfs4_match_stateid(const nfs4_stateid *s1,
10469 		const nfs4_stateid *s2)
10470 {
10471 	return nfs4_stateid_match(s1, s2);
10472 }
10473 
10474 
10475 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10476 	.owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10477 	.state_flag_bit	= NFS_STATE_RECLAIM_REBOOT,
10478 	.recover_open	= nfs4_open_reclaim,
10479 	.recover_lock	= nfs4_lock_reclaim,
10480 	.establish_clid = nfs4_init_clientid,
10481 	.detect_trunking = nfs40_discover_server_trunking,
10482 };
10483 
10484 #if defined(CONFIG_NFS_V4_1)
10485 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10486 	.owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10487 	.state_flag_bit	= NFS_STATE_RECLAIM_REBOOT,
10488 	.recover_open	= nfs4_open_reclaim,
10489 	.recover_lock	= nfs4_lock_reclaim,
10490 	.establish_clid = nfs41_init_clientid,
10491 	.reclaim_complete = nfs41_proc_reclaim_complete,
10492 	.detect_trunking = nfs41_discover_server_trunking,
10493 };
10494 #endif /* CONFIG_NFS_V4_1 */
10495 
10496 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10497 	.owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10498 	.state_flag_bit	= NFS_STATE_RECLAIM_NOGRACE,
10499 	.recover_open	= nfs40_open_expired,
10500 	.recover_lock	= nfs4_lock_expired,
10501 	.establish_clid = nfs4_init_clientid,
10502 };
10503 
10504 #if defined(CONFIG_NFS_V4_1)
10505 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10506 	.owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10507 	.state_flag_bit	= NFS_STATE_RECLAIM_NOGRACE,
10508 	.recover_open	= nfs41_open_expired,
10509 	.recover_lock	= nfs41_lock_expired,
10510 	.establish_clid = nfs41_init_clientid,
10511 };
10512 #endif /* CONFIG_NFS_V4_1 */
10513 
10514 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10515 	.sched_state_renewal = nfs4_proc_async_renew,
10516 	.get_state_renewal_cred = nfs4_get_renew_cred,
10517 	.renew_lease = nfs4_proc_renew,
10518 };
10519 
10520 #if defined(CONFIG_NFS_V4_1)
10521 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10522 	.sched_state_renewal = nfs41_proc_async_sequence,
10523 	.get_state_renewal_cred = nfs4_get_machine_cred,
10524 	.renew_lease = nfs4_proc_sequence,
10525 };
10526 #endif
10527 
10528 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10529 	.get_locations = _nfs40_proc_get_locations,
10530 	.fsid_present = _nfs40_proc_fsid_present,
10531 };
10532 
10533 #if defined(CONFIG_NFS_V4_1)
10534 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10535 	.get_locations = _nfs41_proc_get_locations,
10536 	.fsid_present = _nfs41_proc_fsid_present,
10537 };
10538 #endif	/* CONFIG_NFS_V4_1 */
10539 
10540 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10541 	.minor_version = 0,
10542 	.init_caps = NFS_CAP_READDIRPLUS
10543 		| NFS_CAP_ATOMIC_OPEN
10544 		| NFS_CAP_POSIX_LOCK,
10545 	.init_client = nfs40_init_client,
10546 	.shutdown_client = nfs40_shutdown_client,
10547 	.match_stateid = nfs4_match_stateid,
10548 	.find_root_sec = nfs4_find_root_sec,
10549 	.free_lock_state = nfs4_release_lockowner,
10550 	.test_and_free_expired = nfs40_test_and_free_expired_stateid,
10551 	.alloc_seqid = nfs_alloc_seqid,
10552 	.call_sync_ops = &nfs40_call_sync_ops,
10553 	.reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10554 	.nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10555 	.state_renewal_ops = &nfs40_state_renewal_ops,
10556 	.mig_recovery_ops = &nfs40_mig_recovery_ops,
10557 };
10558 
10559 #if defined(CONFIG_NFS_V4_1)
10560 static struct nfs_seqid *
10561 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10562 {
10563 	return NULL;
10564 }
10565 
10566 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10567 	.minor_version = 1,
10568 	.init_caps = NFS_CAP_READDIRPLUS
10569 		| NFS_CAP_ATOMIC_OPEN
10570 		| NFS_CAP_POSIX_LOCK
10571 		| NFS_CAP_STATEID_NFSV41
10572 		| NFS_CAP_ATOMIC_OPEN_V1
10573 		| NFS_CAP_LGOPEN
10574 		| NFS_CAP_MOVEABLE,
10575 	.init_client = nfs41_init_client,
10576 	.shutdown_client = nfs41_shutdown_client,
10577 	.match_stateid = nfs41_match_stateid,
10578 	.find_root_sec = nfs41_find_root_sec,
10579 	.free_lock_state = nfs41_free_lock_state,
10580 	.test_and_free_expired = nfs41_test_and_free_expired_stateid,
10581 	.alloc_seqid = nfs_alloc_no_seqid,
10582 	.session_trunk = nfs4_test_session_trunk,
10583 	.call_sync_ops = &nfs41_call_sync_ops,
10584 	.reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10585 	.nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10586 	.state_renewal_ops = &nfs41_state_renewal_ops,
10587 	.mig_recovery_ops = &nfs41_mig_recovery_ops,
10588 };
10589 #endif
10590 
10591 #if defined(CONFIG_NFS_V4_2)
10592 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10593 	.minor_version = 2,
10594 	.init_caps = NFS_CAP_READDIRPLUS
10595 		| NFS_CAP_ATOMIC_OPEN
10596 		| NFS_CAP_POSIX_LOCK
10597 		| NFS_CAP_STATEID_NFSV41
10598 		| NFS_CAP_ATOMIC_OPEN_V1
10599 		| NFS_CAP_LGOPEN
10600 		| NFS_CAP_ALLOCATE
10601 		| NFS_CAP_COPY
10602 		| NFS_CAP_OFFLOAD_CANCEL
10603 		| NFS_CAP_COPY_NOTIFY
10604 		| NFS_CAP_DEALLOCATE
10605 		| NFS_CAP_SEEK
10606 		| NFS_CAP_LAYOUTSTATS
10607 		| NFS_CAP_CLONE
10608 		| NFS_CAP_LAYOUTERROR
10609 		| NFS_CAP_READ_PLUS
10610 		| NFS_CAP_MOVEABLE,
10611 	.init_client = nfs41_init_client,
10612 	.shutdown_client = nfs41_shutdown_client,
10613 	.match_stateid = nfs41_match_stateid,
10614 	.find_root_sec = nfs41_find_root_sec,
10615 	.free_lock_state = nfs41_free_lock_state,
10616 	.call_sync_ops = &nfs41_call_sync_ops,
10617 	.test_and_free_expired = nfs41_test_and_free_expired_stateid,
10618 	.alloc_seqid = nfs_alloc_no_seqid,
10619 	.session_trunk = nfs4_test_session_trunk,
10620 	.reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10621 	.nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10622 	.state_renewal_ops = &nfs41_state_renewal_ops,
10623 	.mig_recovery_ops = &nfs41_mig_recovery_ops,
10624 };
10625 #endif
10626 
10627 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10628 	[0] = &nfs_v4_0_minor_ops,
10629 #if defined(CONFIG_NFS_V4_1)
10630 	[1] = &nfs_v4_1_minor_ops,
10631 #endif
10632 #if defined(CONFIG_NFS_V4_2)
10633 	[2] = &nfs_v4_2_minor_ops,
10634 #endif
10635 };
10636 
10637 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10638 {
10639 	ssize_t error, error2, error3;
10640 	size_t left = size;
10641 
10642 	error = generic_listxattr(dentry, list, left);
10643 	if (error < 0)
10644 		return error;
10645 	if (list) {
10646 		list += error;
10647 		left -= error;
10648 	}
10649 
10650 	error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, left);
10651 	if (error2 < 0)
10652 		return error2;
10653 
10654 	if (list) {
10655 		list += error2;
10656 		left -= error2;
10657 	}
10658 
10659 	error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, left);
10660 	if (error3 < 0)
10661 		return error3;
10662 
10663 	error += error2 + error3;
10664 	if (size && error > size)
10665 		return -ERANGE;
10666 	return error;
10667 }
10668 
10669 static void nfs4_enable_swap(struct inode *inode)
10670 {
10671 	/* The state manager thread must always be running.
10672 	 * It will notice the client is a swapper, and stay put.
10673 	 */
10674 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10675 
10676 	nfs4_schedule_state_manager(clp);
10677 }
10678 
10679 static void nfs4_disable_swap(struct inode *inode)
10680 {
10681 	/* The state manager thread will now exit once it is
10682 	 * woken.
10683 	 */
10684 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10685 
10686 	set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
10687 	clear_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state);
10688 	wake_up_var(&clp->cl_state);
10689 }
10690 
10691 static const struct inode_operations nfs4_dir_inode_operations = {
10692 	.create		= nfs_create,
10693 	.lookup		= nfs_lookup,
10694 	.atomic_open	= nfs_atomic_open,
10695 	.link		= nfs_link,
10696 	.unlink		= nfs_unlink,
10697 	.symlink	= nfs_symlink,
10698 	.mkdir		= nfs_mkdir,
10699 	.rmdir		= nfs_rmdir,
10700 	.mknod		= nfs_mknod,
10701 	.rename		= nfs_rename,
10702 	.permission	= nfs_permission,
10703 	.getattr	= nfs_getattr,
10704 	.setattr	= nfs_setattr,
10705 	.listxattr	= nfs4_listxattr,
10706 };
10707 
10708 static const struct inode_operations nfs4_file_inode_operations = {
10709 	.permission	= nfs_permission,
10710 	.getattr	= nfs_getattr,
10711 	.setattr	= nfs_setattr,
10712 	.listxattr	= nfs4_listxattr,
10713 };
10714 
10715 const struct nfs_rpc_ops nfs_v4_clientops = {
10716 	.version	= 4,			/* protocol version */
10717 	.dentry_ops	= &nfs4_dentry_operations,
10718 	.dir_inode_ops	= &nfs4_dir_inode_operations,
10719 	.file_inode_ops	= &nfs4_file_inode_operations,
10720 	.file_ops	= &nfs4_file_operations,
10721 	.getroot	= nfs4_proc_get_root,
10722 	.submount	= nfs4_submount,
10723 	.try_get_tree	= nfs4_try_get_tree,
10724 	.getattr	= nfs4_proc_getattr,
10725 	.setattr	= nfs4_proc_setattr,
10726 	.lookup		= nfs4_proc_lookup,
10727 	.lookupp	= nfs4_proc_lookupp,
10728 	.access		= nfs4_proc_access,
10729 	.readlink	= nfs4_proc_readlink,
10730 	.create		= nfs4_proc_create,
10731 	.remove		= nfs4_proc_remove,
10732 	.unlink_setup	= nfs4_proc_unlink_setup,
10733 	.unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10734 	.unlink_done	= nfs4_proc_unlink_done,
10735 	.rename_setup	= nfs4_proc_rename_setup,
10736 	.rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10737 	.rename_done	= nfs4_proc_rename_done,
10738 	.link		= nfs4_proc_link,
10739 	.symlink	= nfs4_proc_symlink,
10740 	.mkdir		= nfs4_proc_mkdir,
10741 	.rmdir		= nfs4_proc_rmdir,
10742 	.readdir	= nfs4_proc_readdir,
10743 	.mknod		= nfs4_proc_mknod,
10744 	.statfs		= nfs4_proc_statfs,
10745 	.fsinfo		= nfs4_proc_fsinfo,
10746 	.pathconf	= nfs4_proc_pathconf,
10747 	.set_capabilities = nfs4_server_capabilities,
10748 	.decode_dirent	= nfs4_decode_dirent,
10749 	.pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10750 	.read_setup	= nfs4_proc_read_setup,
10751 	.read_done	= nfs4_read_done,
10752 	.write_setup	= nfs4_proc_write_setup,
10753 	.write_done	= nfs4_write_done,
10754 	.commit_setup	= nfs4_proc_commit_setup,
10755 	.commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10756 	.commit_done	= nfs4_commit_done,
10757 	.lock		= nfs4_proc_lock,
10758 	.clear_acl_cache = nfs4_zap_acl_attr,
10759 	.close_context  = nfs4_close_context,
10760 	.open_context	= nfs4_atomic_open,
10761 	.have_delegation = nfs4_have_delegation,
10762 	.alloc_client	= nfs4_alloc_client,
10763 	.init_client	= nfs4_init_client,
10764 	.free_client	= nfs4_free_client,
10765 	.create_server	= nfs4_create_server,
10766 	.clone_server	= nfs_clone_server,
10767 	.discover_trunking = nfs4_discover_trunking,
10768 	.enable_swap	= nfs4_enable_swap,
10769 	.disable_swap	= nfs4_disable_swap,
10770 };
10771 
10772 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10773 	.name	= XATTR_NAME_NFSV4_ACL,
10774 	.list	= nfs4_xattr_list_nfs4_acl,
10775 	.get	= nfs4_xattr_get_nfs4_acl,
10776 	.set	= nfs4_xattr_set_nfs4_acl,
10777 };
10778 
10779 #if defined(CONFIG_NFS_V4_1)
10780 static const struct xattr_handler nfs4_xattr_nfs4_dacl_handler = {
10781 	.name	= XATTR_NAME_NFSV4_DACL,
10782 	.list	= nfs4_xattr_list_nfs4_dacl,
10783 	.get	= nfs4_xattr_get_nfs4_dacl,
10784 	.set	= nfs4_xattr_set_nfs4_dacl,
10785 };
10786 
10787 static const struct xattr_handler nfs4_xattr_nfs4_sacl_handler = {
10788 	.name	= XATTR_NAME_NFSV4_SACL,
10789 	.list	= nfs4_xattr_list_nfs4_sacl,
10790 	.get	= nfs4_xattr_get_nfs4_sacl,
10791 	.set	= nfs4_xattr_set_nfs4_sacl,
10792 };
10793 #endif
10794 
10795 #ifdef CONFIG_NFS_V4_2
10796 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10797 	.prefix	= XATTR_USER_PREFIX,
10798 	.get	= nfs4_xattr_get_nfs4_user,
10799 	.set	= nfs4_xattr_set_nfs4_user,
10800 };
10801 #endif
10802 
10803 const struct xattr_handler * const nfs4_xattr_handlers[] = {
10804 	&nfs4_xattr_nfs4_acl_handler,
10805 #if defined(CONFIG_NFS_V4_1)
10806 	&nfs4_xattr_nfs4_dacl_handler,
10807 	&nfs4_xattr_nfs4_sacl_handler,
10808 #endif
10809 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10810 	&nfs4_xattr_nfs4_label_handler,
10811 #endif
10812 #ifdef CONFIG_NFS_V4_2
10813 	&nfs4_xattr_nfs4_user_handler,
10814 #endif
10815 	NULL
10816 };
10817