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