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