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