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