xref: /openbmc/linux/fs/nfs/nfs4proc.c (revision 586b4106)
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 	/* If this request hasn't been cancelled, do nothing */
2532 	if (!data->cancelled)
2533 		goto out_free;
2534 	/* In case of error, no cleanup! */
2535 	if (data->rpc_status != 0 || !data->rpc_done)
2536 		goto out_free;
2537 	/* In case we need an open_confirm, no cleanup! */
2538 	if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2539 		goto out_free;
2540 	state = nfs4_opendata_to_nfs4_state(data);
2541 	if (!IS_ERR(state))
2542 		nfs4_close_state(state, data->o_arg.fmode);
2543 out_free:
2544 	nfs4_opendata_put(data);
2545 }
2546 
2547 static const struct rpc_call_ops nfs4_open_ops = {
2548 	.rpc_call_prepare = nfs4_open_prepare,
2549 	.rpc_call_done = nfs4_open_done,
2550 	.rpc_release = nfs4_open_release,
2551 };
2552 
2553 static int nfs4_run_open_task(struct nfs4_opendata *data,
2554 			      struct nfs_open_context *ctx)
2555 {
2556 	struct inode *dir = d_inode(data->dir);
2557 	struct nfs_server *server = NFS_SERVER(dir);
2558 	struct nfs_openargs *o_arg = &data->o_arg;
2559 	struct nfs_openres *o_res = &data->o_res;
2560 	struct rpc_task *task;
2561 	struct rpc_message msg = {
2562 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2563 		.rpc_argp = o_arg,
2564 		.rpc_resp = o_res,
2565 		.rpc_cred = data->owner->so_cred,
2566 	};
2567 	struct rpc_task_setup task_setup_data = {
2568 		.rpc_client = server->client,
2569 		.rpc_message = &msg,
2570 		.callback_ops = &nfs4_open_ops,
2571 		.callback_data = data,
2572 		.workqueue = nfsiod_workqueue,
2573 		.flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2574 	};
2575 	int status;
2576 
2577 	if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
2578 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
2579 
2580 	kref_get(&data->kref);
2581 	data->rpc_done = false;
2582 	data->rpc_status = 0;
2583 	data->cancelled = false;
2584 	data->is_recover = false;
2585 	if (!ctx) {
2586 		nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2587 		data->is_recover = true;
2588 		task_setup_data.flags |= RPC_TASK_TIMEOUT;
2589 	} else {
2590 		nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2591 		pnfs_lgopen_prepare(data, ctx);
2592 	}
2593 	task = rpc_run_task(&task_setup_data);
2594 	if (IS_ERR(task))
2595 		return PTR_ERR(task);
2596 	status = rpc_wait_for_completion_task(task);
2597 	if (status != 0) {
2598 		data->cancelled = true;
2599 		smp_wmb();
2600 	} else
2601 		status = data->rpc_status;
2602 	rpc_put_task(task);
2603 
2604 	return status;
2605 }
2606 
2607 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2608 {
2609 	struct inode *dir = d_inode(data->dir);
2610 	struct nfs_openres *o_res = &data->o_res;
2611 	int status;
2612 
2613 	status = nfs4_run_open_task(data, NULL);
2614 	if (status != 0 || !data->rpc_done)
2615 		return status;
2616 
2617 	nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2618 
2619 	if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2620 		status = _nfs4_proc_open_confirm(data);
2621 
2622 	return status;
2623 }
2624 
2625 /*
2626  * Additional permission checks in order to distinguish between an
2627  * open for read, and an open for execute. This works around the
2628  * fact that NFSv4 OPEN treats read and execute permissions as being
2629  * the same.
2630  * Note that in the non-execute case, we want to turn off permission
2631  * checking if we just created a new file (POSIX open() semantics).
2632  */
2633 static int nfs4_opendata_access(const struct cred *cred,
2634 				struct nfs4_opendata *opendata,
2635 				struct nfs4_state *state, fmode_t fmode)
2636 {
2637 	struct nfs_access_entry cache;
2638 	u32 mask, flags;
2639 
2640 	/* access call failed or for some reason the server doesn't
2641 	 * support any access modes -- defer access call until later */
2642 	if (opendata->o_res.access_supported == 0)
2643 		return 0;
2644 
2645 	mask = 0;
2646 	if (fmode & FMODE_EXEC) {
2647 		/* ONLY check for exec rights */
2648 		if (S_ISDIR(state->inode->i_mode))
2649 			mask = NFS4_ACCESS_LOOKUP;
2650 		else
2651 			mask = NFS4_ACCESS_EXECUTE;
2652 	} else if ((fmode & FMODE_READ) && !opendata->file_created)
2653 		mask = NFS4_ACCESS_READ;
2654 
2655 	nfs_access_set_mask(&cache, opendata->o_res.access_result);
2656 	nfs_access_add_cache(state->inode, &cache, cred);
2657 
2658 	flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2659 	if ((mask & ~cache.mask & flags) == 0)
2660 		return 0;
2661 
2662 	return -EACCES;
2663 }
2664 
2665 /*
2666  * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2667  */
2668 static int _nfs4_proc_open(struct nfs4_opendata *data,
2669 			   struct nfs_open_context *ctx)
2670 {
2671 	struct inode *dir = d_inode(data->dir);
2672 	struct nfs_server *server = NFS_SERVER(dir);
2673 	struct nfs_openargs *o_arg = &data->o_arg;
2674 	struct nfs_openres *o_res = &data->o_res;
2675 	int status;
2676 
2677 	status = nfs4_run_open_task(data, ctx);
2678 	if (!data->rpc_done)
2679 		return status;
2680 	if (status != 0) {
2681 		if (status == -NFS4ERR_BADNAME &&
2682 				!(o_arg->open_flags & O_CREAT))
2683 			return -ENOENT;
2684 		return status;
2685 	}
2686 
2687 	nfs_fattr_map_and_free_names(server, &data->f_attr);
2688 
2689 	if (o_arg->open_flags & O_CREAT) {
2690 		if (o_arg->open_flags & O_EXCL)
2691 			data->file_created = true;
2692 		else if (o_res->cinfo.before != o_res->cinfo.after)
2693 			data->file_created = true;
2694 		if (data->file_created ||
2695 		    inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2696 			nfs4_update_changeattr(dir, &o_res->cinfo,
2697 					o_res->f_attr->time_start,
2698 					NFS_INO_INVALID_DATA);
2699 	}
2700 	if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2701 		server->caps &= ~NFS_CAP_POSIX_LOCK;
2702 	if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2703 		status = _nfs4_proc_open_confirm(data);
2704 		if (status != 0)
2705 			return status;
2706 	}
2707 	if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2708 		struct nfs_fh *fh = &o_res->fh;
2709 
2710 		nfs4_sequence_free_slot(&o_res->seq_res);
2711 		if (o_arg->claim == NFS4_OPEN_CLAIM_FH)
2712 			fh = NFS_FH(d_inode(data->dentry));
2713 		nfs4_proc_getattr(server, fh, o_res->f_attr, NULL);
2714 	}
2715 	return 0;
2716 }
2717 
2718 /*
2719  * OPEN_EXPIRED:
2720  * 	reclaim state on the server after a network partition.
2721  * 	Assumes caller holds the appropriate lock
2722  */
2723 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2724 {
2725 	struct nfs4_opendata *opendata;
2726 	int ret;
2727 
2728 	opendata = nfs4_open_recoverdata_alloc(ctx, state, NFS4_OPEN_CLAIM_FH);
2729 	if (IS_ERR(opendata))
2730 		return PTR_ERR(opendata);
2731 	/*
2732 	 * We're not recovering a delegation, so ask for no delegation.
2733 	 * Otherwise the recovery thread could deadlock with an outstanding
2734 	 * delegation return.
2735 	 */
2736 	opendata->o_arg.open_flags = O_DIRECT;
2737 	ret = nfs4_open_recover(opendata, state);
2738 	if (ret == -ESTALE)
2739 		d_drop(ctx->dentry);
2740 	nfs4_opendata_put(opendata);
2741 	return ret;
2742 }
2743 
2744 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2745 {
2746 	struct nfs_server *server = NFS_SERVER(state->inode);
2747 	struct nfs4_exception exception = { };
2748 	int err;
2749 
2750 	do {
2751 		err = _nfs4_open_expired(ctx, state);
2752 		trace_nfs4_open_expired(ctx, 0, err);
2753 		if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2754 			continue;
2755 		switch (err) {
2756 		default:
2757 			goto out;
2758 		case -NFS4ERR_GRACE:
2759 		case -NFS4ERR_DELAY:
2760 			nfs4_handle_exception(server, err, &exception);
2761 			err = 0;
2762 		}
2763 	} while (exception.retry);
2764 out:
2765 	return err;
2766 }
2767 
2768 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2769 {
2770 	struct nfs_open_context *ctx;
2771 	int ret;
2772 
2773 	ctx = nfs4_state_find_open_context(state);
2774 	if (IS_ERR(ctx))
2775 		return -EAGAIN;
2776 	ret = nfs4_do_open_expired(ctx, state);
2777 	put_nfs_open_context(ctx);
2778 	return ret;
2779 }
2780 
2781 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2782 		const nfs4_stateid *stateid)
2783 {
2784 	nfs_remove_bad_delegation(state->inode, stateid);
2785 	nfs_state_clear_delegation(state);
2786 }
2787 
2788 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2789 {
2790 	if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2791 		nfs_finish_clear_delegation_stateid(state, NULL);
2792 }
2793 
2794 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2795 {
2796 	/* NFSv4.0 doesn't allow for delegation recovery on open expire */
2797 	nfs40_clear_delegation_stateid(state);
2798 	nfs_state_clear_open_state_flags(state);
2799 	return nfs4_open_expired(sp, state);
2800 }
2801 
2802 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2803 		nfs4_stateid *stateid,
2804 		const struct cred *cred)
2805 {
2806 	return -NFS4ERR_BAD_STATEID;
2807 }
2808 
2809 #if defined(CONFIG_NFS_V4_1)
2810 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2811 		nfs4_stateid *stateid,
2812 		const struct cred *cred)
2813 {
2814 	int status;
2815 
2816 	switch (stateid->type) {
2817 	default:
2818 		break;
2819 	case NFS4_INVALID_STATEID_TYPE:
2820 	case NFS4_SPECIAL_STATEID_TYPE:
2821 		return -NFS4ERR_BAD_STATEID;
2822 	case NFS4_REVOKED_STATEID_TYPE:
2823 		goto out_free;
2824 	}
2825 
2826 	status = nfs41_test_stateid(server, stateid, cred);
2827 	switch (status) {
2828 	case -NFS4ERR_EXPIRED:
2829 	case -NFS4ERR_ADMIN_REVOKED:
2830 	case -NFS4ERR_DELEG_REVOKED:
2831 		break;
2832 	default:
2833 		return status;
2834 	}
2835 out_free:
2836 	/* Ack the revoked state to the server */
2837 	nfs41_free_stateid(server, stateid, cred, true);
2838 	return -NFS4ERR_EXPIRED;
2839 }
2840 
2841 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2842 {
2843 	struct nfs_server *server = NFS_SERVER(state->inode);
2844 	nfs4_stateid stateid;
2845 	struct nfs_delegation *delegation;
2846 	const struct cred *cred = NULL;
2847 	int status, ret = NFS_OK;
2848 
2849 	/* Get the delegation credential for use by test/free_stateid */
2850 	rcu_read_lock();
2851 	delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2852 	if (delegation == NULL) {
2853 		rcu_read_unlock();
2854 		nfs_state_clear_delegation(state);
2855 		return NFS_OK;
2856 	}
2857 
2858 	spin_lock(&delegation->lock);
2859 	nfs4_stateid_copy(&stateid, &delegation->stateid);
2860 
2861 	if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2862 				&delegation->flags)) {
2863 		spin_unlock(&delegation->lock);
2864 		rcu_read_unlock();
2865 		return NFS_OK;
2866 	}
2867 
2868 	if (delegation->cred)
2869 		cred = get_cred(delegation->cred);
2870 	spin_unlock(&delegation->lock);
2871 	rcu_read_unlock();
2872 	status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2873 	trace_nfs4_test_delegation_stateid(state, NULL, status);
2874 	if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2875 		nfs_finish_clear_delegation_stateid(state, &stateid);
2876 	else
2877 		ret = status;
2878 
2879 	put_cred(cred);
2880 	return ret;
2881 }
2882 
2883 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2884 {
2885 	nfs4_stateid tmp;
2886 
2887 	if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2888 	    nfs4_copy_delegation_stateid(state->inode, state->state,
2889 				&tmp, NULL) &&
2890 	    nfs4_stateid_match_other(&state->stateid, &tmp))
2891 		nfs_state_set_delegation(state, &tmp, state->state);
2892 	else
2893 		nfs_state_clear_delegation(state);
2894 }
2895 
2896 /**
2897  * nfs41_check_expired_locks - possibly free a lock stateid
2898  *
2899  * @state: NFSv4 state for an inode
2900  *
2901  * Returns NFS_OK if recovery for this stateid is now finished.
2902  * Otherwise a negative NFS4ERR value is returned.
2903  */
2904 static int nfs41_check_expired_locks(struct nfs4_state *state)
2905 {
2906 	int status, ret = NFS_OK;
2907 	struct nfs4_lock_state *lsp, *prev = NULL;
2908 	struct nfs_server *server = NFS_SERVER(state->inode);
2909 
2910 	if (!test_bit(LK_STATE_IN_USE, &state->flags))
2911 		goto out;
2912 
2913 	spin_lock(&state->state_lock);
2914 	list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2915 		if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2916 			const struct cred *cred = lsp->ls_state->owner->so_cred;
2917 
2918 			refcount_inc(&lsp->ls_count);
2919 			spin_unlock(&state->state_lock);
2920 
2921 			nfs4_put_lock_state(prev);
2922 			prev = lsp;
2923 
2924 			status = nfs41_test_and_free_expired_stateid(server,
2925 					&lsp->ls_stateid,
2926 					cred);
2927 			trace_nfs4_test_lock_stateid(state, lsp, status);
2928 			if (status == -NFS4ERR_EXPIRED ||
2929 			    status == -NFS4ERR_BAD_STATEID) {
2930 				clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2931 				lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2932 				if (!recover_lost_locks)
2933 					set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2934 			} else if (status != NFS_OK) {
2935 				ret = status;
2936 				nfs4_put_lock_state(prev);
2937 				goto out;
2938 			}
2939 			spin_lock(&state->state_lock);
2940 		}
2941 	}
2942 	spin_unlock(&state->state_lock);
2943 	nfs4_put_lock_state(prev);
2944 out:
2945 	return ret;
2946 }
2947 
2948 /**
2949  * nfs41_check_open_stateid - possibly free an open stateid
2950  *
2951  * @state: NFSv4 state for an inode
2952  *
2953  * Returns NFS_OK if recovery for this stateid is now finished.
2954  * Otherwise a negative NFS4ERR value is returned.
2955  */
2956 static int nfs41_check_open_stateid(struct nfs4_state *state)
2957 {
2958 	struct nfs_server *server = NFS_SERVER(state->inode);
2959 	nfs4_stateid *stateid = &state->open_stateid;
2960 	const struct cred *cred = state->owner->so_cred;
2961 	int status;
2962 
2963 	if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2964 		return -NFS4ERR_BAD_STATEID;
2965 	status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2966 	trace_nfs4_test_open_stateid(state, NULL, status);
2967 	if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2968 		nfs_state_clear_open_state_flags(state);
2969 		stateid->type = NFS4_INVALID_STATEID_TYPE;
2970 		return status;
2971 	}
2972 	if (nfs_open_stateid_recover_openmode(state))
2973 		return -NFS4ERR_OPENMODE;
2974 	return NFS_OK;
2975 }
2976 
2977 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2978 {
2979 	int status;
2980 
2981 	status = nfs41_check_delegation_stateid(state);
2982 	if (status != NFS_OK)
2983 		return status;
2984 	nfs41_delegation_recover_stateid(state);
2985 
2986 	status = nfs41_check_expired_locks(state);
2987 	if (status != NFS_OK)
2988 		return status;
2989 	status = nfs41_check_open_stateid(state);
2990 	if (status != NFS_OK)
2991 		status = nfs4_open_expired(sp, state);
2992 	return status;
2993 }
2994 #endif
2995 
2996 /*
2997  * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2998  * fields corresponding to attributes that were used to store the verifier.
2999  * Make sure we clobber those fields in the later setattr call
3000  */
3001 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
3002 				struct iattr *sattr, struct nfs4_label **label)
3003 {
3004 	const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
3005 	__u32 attrset[3];
3006 	unsigned ret;
3007 	unsigned i;
3008 
3009 	for (i = 0; i < ARRAY_SIZE(attrset); i++) {
3010 		attrset[i] = opendata->o_res.attrset[i];
3011 		if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
3012 			attrset[i] &= ~bitmask[i];
3013 	}
3014 
3015 	ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3016 		sattr->ia_valid : 0;
3017 
3018 	if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3019 		if (sattr->ia_valid & ATTR_ATIME_SET)
3020 			ret |= ATTR_ATIME_SET;
3021 		else
3022 			ret |= ATTR_ATIME;
3023 	}
3024 
3025 	if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3026 		if (sattr->ia_valid & ATTR_MTIME_SET)
3027 			ret |= ATTR_MTIME_SET;
3028 		else
3029 			ret |= ATTR_MTIME;
3030 	}
3031 
3032 	if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
3033 		*label = NULL;
3034 	return ret;
3035 }
3036 
3037 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
3038 		struct nfs_open_context *ctx)
3039 {
3040 	struct nfs4_state_owner *sp = opendata->owner;
3041 	struct nfs_server *server = sp->so_server;
3042 	struct dentry *dentry;
3043 	struct nfs4_state *state;
3044 	fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3045 	struct inode *dir = d_inode(opendata->dir);
3046 	unsigned long dir_verifier;
3047 	unsigned int seq;
3048 	int ret;
3049 
3050 	seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
3051 	dir_verifier = nfs_save_change_attribute(dir);
3052 
3053 	ret = _nfs4_proc_open(opendata, ctx);
3054 	if (ret != 0)
3055 		goto out;
3056 
3057 	state = _nfs4_opendata_to_nfs4_state(opendata);
3058 	ret = PTR_ERR(state);
3059 	if (IS_ERR(state))
3060 		goto out;
3061 	ctx->state = state;
3062 	if (server->caps & NFS_CAP_POSIX_LOCK)
3063 		set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3064 	if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3065 		set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3066 	if (opendata->o_res.rflags & NFS4_OPEN_RESULT_PRESERVE_UNLINKED)
3067 		set_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(state->inode)->flags);
3068 
3069 	dentry = opendata->dentry;
3070 	if (d_really_is_negative(dentry)) {
3071 		struct dentry *alias;
3072 		d_drop(dentry);
3073 		alias = d_exact_alias(dentry, state->inode);
3074 		if (!alias)
3075 			alias = d_splice_alias(igrab(state->inode), dentry);
3076 		/* d_splice_alias() can't fail here - it's a non-directory */
3077 		if (alias) {
3078 			dput(ctx->dentry);
3079 			ctx->dentry = dentry = alias;
3080 		}
3081 	}
3082 
3083 	switch(opendata->o_arg.claim) {
3084 	default:
3085 		break;
3086 	case NFS4_OPEN_CLAIM_NULL:
3087 	case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3088 	case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3089 		if (!opendata->rpc_done)
3090 			break;
3091 		if (opendata->o_res.delegation_type != 0)
3092 			dir_verifier = nfs_save_change_attribute(dir);
3093 		nfs_set_verifier(dentry, dir_verifier);
3094 	}
3095 
3096 	/* Parse layoutget results before we check for access */
3097 	pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3098 
3099 	ret = nfs4_opendata_access(sp->so_cred, opendata, state, acc_mode);
3100 	if (ret != 0)
3101 		goto out;
3102 
3103 	if (d_inode(dentry) == state->inode) {
3104 		nfs_inode_attach_open_context(ctx);
3105 		if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3106 			nfs4_schedule_stateid_recovery(server, state);
3107 	}
3108 
3109 out:
3110 	if (!opendata->cancelled) {
3111 		if (opendata->lgp) {
3112 			nfs4_lgopen_release(opendata->lgp);
3113 			opendata->lgp = NULL;
3114 		}
3115 		nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3116 	}
3117 	return ret;
3118 }
3119 
3120 /*
3121  * Returns a referenced nfs4_state
3122  */
3123 static int _nfs4_do_open(struct inode *dir,
3124 			struct nfs_open_context *ctx,
3125 			int flags,
3126 			const struct nfs4_open_createattrs *c,
3127 			int *opened)
3128 {
3129 	struct nfs4_state_owner  *sp;
3130 	struct nfs4_state     *state = NULL;
3131 	struct nfs_server       *server = NFS_SERVER(dir);
3132 	struct nfs4_opendata *opendata;
3133 	struct dentry *dentry = ctx->dentry;
3134 	const struct cred *cred = ctx->cred;
3135 	struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3136 	fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3137 	enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3138 	struct iattr *sattr = c->sattr;
3139 	struct nfs4_label *label = c->label;
3140 	int status;
3141 
3142 	/* Protect against reboot recovery conflicts */
3143 	status = -ENOMEM;
3144 	sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3145 	if (sp == NULL) {
3146 		dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3147 		goto out_err;
3148 	}
3149 	status = nfs4_client_recover_expired_lease(server->nfs_client);
3150 	if (status != 0)
3151 		goto err_put_state_owner;
3152 	if (d_really_is_positive(dentry))
3153 		nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3154 	status = -ENOMEM;
3155 	if (d_really_is_positive(dentry))
3156 		claim = NFS4_OPEN_CLAIM_FH;
3157 	opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3158 			c, claim, GFP_KERNEL);
3159 	if (opendata == NULL)
3160 		goto err_put_state_owner;
3161 
3162 	if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3163 		if (!opendata->f_attr.mdsthreshold) {
3164 			opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3165 			if (!opendata->f_attr.mdsthreshold)
3166 				goto err_opendata_put;
3167 		}
3168 		opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3169 	}
3170 	if (d_really_is_positive(dentry))
3171 		opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3172 
3173 	status = _nfs4_open_and_get_state(opendata, ctx);
3174 	if (status != 0)
3175 		goto err_opendata_put;
3176 	state = ctx->state;
3177 
3178 	if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3179 	    (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3180 		unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3181 		/*
3182 		 * send create attributes which was not set by open
3183 		 * with an extra setattr.
3184 		 */
3185 		if (attrs || label) {
3186 			unsigned ia_old = sattr->ia_valid;
3187 
3188 			sattr->ia_valid = attrs;
3189 			nfs_fattr_init(opendata->o_res.f_attr);
3190 			status = nfs4_do_setattr(state->inode, cred,
3191 					opendata->o_res.f_attr, sattr,
3192 					ctx, label);
3193 			if (status == 0) {
3194 				nfs_setattr_update_inode(state->inode, sattr,
3195 						opendata->o_res.f_attr);
3196 				nfs_setsecurity(state->inode, opendata->o_res.f_attr);
3197 			}
3198 			sattr->ia_valid = ia_old;
3199 		}
3200 	}
3201 	if (opened && opendata->file_created)
3202 		*opened = 1;
3203 
3204 	if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3205 		*ctx_th = opendata->f_attr.mdsthreshold;
3206 		opendata->f_attr.mdsthreshold = NULL;
3207 	}
3208 
3209 	nfs4_opendata_put(opendata);
3210 	nfs4_put_state_owner(sp);
3211 	return 0;
3212 err_opendata_put:
3213 	nfs4_opendata_put(opendata);
3214 err_put_state_owner:
3215 	nfs4_put_state_owner(sp);
3216 out_err:
3217 	return status;
3218 }
3219 
3220 
3221 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3222 					struct nfs_open_context *ctx,
3223 					int flags,
3224 					struct iattr *sattr,
3225 					struct nfs4_label *label,
3226 					int *opened)
3227 {
3228 	struct nfs_server *server = NFS_SERVER(dir);
3229 	struct nfs4_exception exception = {
3230 		.interruptible = true,
3231 	};
3232 	struct nfs4_state *res;
3233 	struct nfs4_open_createattrs c = {
3234 		.label = label,
3235 		.sattr = sattr,
3236 		.verf = {
3237 			[0] = (__u32)jiffies,
3238 			[1] = (__u32)current->pid,
3239 		},
3240 	};
3241 	int status;
3242 
3243 	do {
3244 		status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3245 		res = ctx->state;
3246 		trace_nfs4_open_file(ctx, flags, status);
3247 		if (status == 0)
3248 			break;
3249 		/* NOTE: BAD_SEQID means the server and client disagree about the
3250 		 * book-keeping w.r.t. state-changing operations
3251 		 * (OPEN/CLOSE/LOCK/LOCKU...)
3252 		 * It is actually a sign of a bug on the client or on the server.
3253 		 *
3254 		 * If we receive a BAD_SEQID error in the particular case of
3255 		 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3256 		 * have unhashed the old state_owner for us, and that we can
3257 		 * therefore safely retry using a new one. We should still warn
3258 		 * the user though...
3259 		 */
3260 		if (status == -NFS4ERR_BAD_SEQID) {
3261 			pr_warn_ratelimited("NFS: v4 server %s "
3262 					" returned a bad sequence-id error!\n",
3263 					NFS_SERVER(dir)->nfs_client->cl_hostname);
3264 			exception.retry = 1;
3265 			continue;
3266 		}
3267 		/*
3268 		 * BAD_STATEID on OPEN means that the server cancelled our
3269 		 * state before it received the OPEN_CONFIRM.
3270 		 * Recover by retrying the request as per the discussion
3271 		 * on Page 181 of RFC3530.
3272 		 */
3273 		if (status == -NFS4ERR_BAD_STATEID) {
3274 			exception.retry = 1;
3275 			continue;
3276 		}
3277 		if (status == -NFS4ERR_EXPIRED) {
3278 			nfs4_schedule_lease_recovery(server->nfs_client);
3279 			exception.retry = 1;
3280 			continue;
3281 		}
3282 		if (status == -EAGAIN) {
3283 			/* We must have found a delegation */
3284 			exception.retry = 1;
3285 			continue;
3286 		}
3287 		if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3288 			continue;
3289 		res = ERR_PTR(nfs4_handle_exception(server,
3290 					status, &exception));
3291 	} while (exception.retry);
3292 	return res;
3293 }
3294 
3295 static int _nfs4_do_setattr(struct inode *inode,
3296 			    struct nfs_setattrargs *arg,
3297 			    struct nfs_setattrres *res,
3298 			    const struct cred *cred,
3299 			    struct nfs_open_context *ctx)
3300 {
3301 	struct nfs_server *server = NFS_SERVER(inode);
3302 	struct rpc_message msg = {
3303 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3304 		.rpc_argp	= arg,
3305 		.rpc_resp	= res,
3306 		.rpc_cred	= cred,
3307 	};
3308 	const struct cred *delegation_cred = NULL;
3309 	unsigned long timestamp = jiffies;
3310 	bool truncate;
3311 	int status;
3312 
3313 	nfs_fattr_init(res->fattr);
3314 
3315 	/* Servers should only apply open mode checks for file size changes */
3316 	truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3317 	if (!truncate) {
3318 		nfs4_inode_make_writeable(inode);
3319 		goto zero_stateid;
3320 	}
3321 
3322 	if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3323 		/* Use that stateid */
3324 	} else if (ctx != NULL && ctx->state) {
3325 		struct nfs_lock_context *l_ctx;
3326 		if (!nfs4_valid_open_stateid(ctx->state))
3327 			return -EBADF;
3328 		l_ctx = nfs_get_lock_context(ctx);
3329 		if (IS_ERR(l_ctx))
3330 			return PTR_ERR(l_ctx);
3331 		status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3332 						&arg->stateid, &delegation_cred);
3333 		nfs_put_lock_context(l_ctx);
3334 		if (status == -EIO)
3335 			return -EBADF;
3336 		else if (status == -EAGAIN)
3337 			goto zero_stateid;
3338 	} else {
3339 zero_stateid:
3340 		nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3341 	}
3342 	if (delegation_cred)
3343 		msg.rpc_cred = delegation_cred;
3344 
3345 	status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3346 
3347 	put_cred(delegation_cred);
3348 	if (status == 0 && ctx != NULL)
3349 		renew_lease(server, timestamp);
3350 	trace_nfs4_setattr(inode, &arg->stateid, status);
3351 	return status;
3352 }
3353 
3354 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3355 			   struct nfs_fattr *fattr, struct iattr *sattr,
3356 			   struct nfs_open_context *ctx, struct nfs4_label *ilabel)
3357 {
3358 	struct nfs_server *server = NFS_SERVER(inode);
3359 	__u32 bitmask[NFS4_BITMASK_SZ];
3360 	struct nfs4_state *state = ctx ? ctx->state : NULL;
3361 	struct nfs_setattrargs	arg = {
3362 		.fh		= NFS_FH(inode),
3363 		.iap		= sattr,
3364 		.server		= server,
3365 		.bitmask = bitmask,
3366 		.label		= ilabel,
3367 	};
3368 	struct nfs_setattrres  res = {
3369 		.fattr		= fattr,
3370 		.server		= server,
3371 	};
3372 	struct nfs4_exception exception = {
3373 		.state = state,
3374 		.inode = inode,
3375 		.stateid = &arg.stateid,
3376 	};
3377 	unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
3378 	int err;
3379 
3380 	if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3381 		adjust_flags |= NFS_INO_INVALID_MODE;
3382 	if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
3383 		adjust_flags |= NFS_INO_INVALID_OTHER;
3384 
3385 	do {
3386 		nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label),
3387 					inode, adjust_flags);
3388 
3389 		err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3390 		switch (err) {
3391 		case -NFS4ERR_OPENMODE:
3392 			if (!(sattr->ia_valid & ATTR_SIZE)) {
3393 				pr_warn_once("NFSv4: server %s is incorrectly "
3394 						"applying open mode checks to "
3395 						"a SETATTR that is not "
3396 						"changing file size.\n",
3397 						server->nfs_client->cl_hostname);
3398 			}
3399 			if (state && !(state->state & FMODE_WRITE)) {
3400 				err = -EBADF;
3401 				if (sattr->ia_valid & ATTR_OPEN)
3402 					err = -EACCES;
3403 				goto out;
3404 			}
3405 		}
3406 		err = nfs4_handle_exception(server, err, &exception);
3407 	} while (exception.retry);
3408 out:
3409 	return err;
3410 }
3411 
3412 static bool
3413 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3414 {
3415 	if (inode == NULL || !nfs_have_layout(inode))
3416 		return false;
3417 
3418 	return pnfs_wait_on_layoutreturn(inode, task);
3419 }
3420 
3421 /*
3422  * Update the seqid of an open stateid
3423  */
3424 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3425 		struct nfs4_state *state)
3426 {
3427 	__be32 seqid_open;
3428 	u32 dst_seqid;
3429 	int seq;
3430 
3431 	for (;;) {
3432 		if (!nfs4_valid_open_stateid(state))
3433 			break;
3434 		seq = read_seqbegin(&state->seqlock);
3435 		if (!nfs4_state_match_open_stateid_other(state, dst)) {
3436 			nfs4_stateid_copy(dst, &state->open_stateid);
3437 			if (read_seqretry(&state->seqlock, seq))
3438 				continue;
3439 			break;
3440 		}
3441 		seqid_open = state->open_stateid.seqid;
3442 		if (read_seqretry(&state->seqlock, seq))
3443 			continue;
3444 
3445 		dst_seqid = be32_to_cpu(dst->seqid);
3446 		if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3447 			dst->seqid = seqid_open;
3448 		break;
3449 	}
3450 }
3451 
3452 /*
3453  * Update the seqid of an open stateid after receiving
3454  * NFS4ERR_OLD_STATEID
3455  */
3456 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3457 		struct nfs4_state *state)
3458 {
3459 	__be32 seqid_open;
3460 	u32 dst_seqid;
3461 	bool ret;
3462 	int seq, status = -EAGAIN;
3463 	DEFINE_WAIT(wait);
3464 
3465 	for (;;) {
3466 		ret = false;
3467 		if (!nfs4_valid_open_stateid(state))
3468 			break;
3469 		seq = read_seqbegin(&state->seqlock);
3470 		if (!nfs4_state_match_open_stateid_other(state, dst)) {
3471 			if (read_seqretry(&state->seqlock, seq))
3472 				continue;
3473 			break;
3474 		}
3475 
3476 		write_seqlock(&state->seqlock);
3477 		seqid_open = state->open_stateid.seqid;
3478 
3479 		dst_seqid = be32_to_cpu(dst->seqid);
3480 
3481 		/* Did another OPEN bump the state's seqid?  try again: */
3482 		if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3483 			dst->seqid = seqid_open;
3484 			write_sequnlock(&state->seqlock);
3485 			ret = true;
3486 			break;
3487 		}
3488 
3489 		/* server says we're behind but we haven't seen the update yet */
3490 		set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3491 		prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3492 		write_sequnlock(&state->seqlock);
3493 		trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3494 
3495 		if (fatal_signal_pending(current))
3496 			status = -EINTR;
3497 		else
3498 			if (schedule_timeout(5*HZ) != 0)
3499 				status = 0;
3500 
3501 		finish_wait(&state->waitq, &wait);
3502 
3503 		if (!status)
3504 			continue;
3505 		if (status == -EINTR)
3506 			break;
3507 
3508 		/* we slept the whole 5 seconds, we must have lost a seqid */
3509 		dst->seqid = cpu_to_be32(dst_seqid + 1);
3510 		ret = true;
3511 		break;
3512 	}
3513 
3514 	return ret;
3515 }
3516 
3517 struct nfs4_closedata {
3518 	struct inode *inode;
3519 	struct nfs4_state *state;
3520 	struct nfs_closeargs arg;
3521 	struct nfs_closeres res;
3522 	struct {
3523 		struct nfs4_layoutreturn_args arg;
3524 		struct nfs4_layoutreturn_res res;
3525 		struct nfs4_xdr_opaque_data ld_private;
3526 		u32 roc_barrier;
3527 		bool roc;
3528 	} lr;
3529 	struct nfs_fattr fattr;
3530 	unsigned long timestamp;
3531 };
3532 
3533 static void nfs4_free_closedata(void *data)
3534 {
3535 	struct nfs4_closedata *calldata = data;
3536 	struct nfs4_state_owner *sp = calldata->state->owner;
3537 	struct super_block *sb = calldata->state->inode->i_sb;
3538 
3539 	if (calldata->lr.roc)
3540 		pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3541 				calldata->res.lr_ret);
3542 	nfs4_put_open_state(calldata->state);
3543 	nfs_free_seqid(calldata->arg.seqid);
3544 	nfs4_put_state_owner(sp);
3545 	nfs_sb_deactive(sb);
3546 	kfree(calldata);
3547 }
3548 
3549 static void nfs4_close_done(struct rpc_task *task, void *data)
3550 {
3551 	struct nfs4_closedata *calldata = data;
3552 	struct nfs4_state *state = calldata->state;
3553 	struct nfs_server *server = NFS_SERVER(calldata->inode);
3554 	nfs4_stateid *res_stateid = NULL;
3555 	struct nfs4_exception exception = {
3556 		.state = state,
3557 		.inode = calldata->inode,
3558 		.stateid = &calldata->arg.stateid,
3559 	};
3560 
3561 	if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3562 		return;
3563 	trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3564 
3565 	/* Handle Layoutreturn errors */
3566 	if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3567 			  &calldata->res.lr_ret) == -EAGAIN)
3568 		goto out_restart;
3569 
3570 	/* hmm. we are done with the inode, and in the process of freeing
3571 	 * the state_owner. we keep this around to process errors
3572 	 */
3573 	switch (task->tk_status) {
3574 		case 0:
3575 			res_stateid = &calldata->res.stateid;
3576 			renew_lease(server, calldata->timestamp);
3577 			break;
3578 		case -NFS4ERR_ACCESS:
3579 			if (calldata->arg.bitmask != NULL) {
3580 				calldata->arg.bitmask = NULL;
3581 				calldata->res.fattr = NULL;
3582 				goto out_restart;
3583 
3584 			}
3585 			break;
3586 		case -NFS4ERR_OLD_STATEID:
3587 			/* Did we race with OPEN? */
3588 			if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3589 						state))
3590 				goto out_restart;
3591 			goto out_release;
3592 		case -NFS4ERR_ADMIN_REVOKED:
3593 		case -NFS4ERR_STALE_STATEID:
3594 		case -NFS4ERR_EXPIRED:
3595 			nfs4_free_revoked_stateid(server,
3596 					&calldata->arg.stateid,
3597 					task->tk_msg.rpc_cred);
3598 			fallthrough;
3599 		case -NFS4ERR_BAD_STATEID:
3600 			if (calldata->arg.fmode == 0)
3601 				break;
3602 			fallthrough;
3603 		default:
3604 			task->tk_status = nfs4_async_handle_exception(task,
3605 					server, task->tk_status, &exception);
3606 			if (exception.retry)
3607 				goto out_restart;
3608 	}
3609 	nfs_clear_open_stateid(state, &calldata->arg.stateid,
3610 			res_stateid, calldata->arg.fmode);
3611 out_release:
3612 	task->tk_status = 0;
3613 	nfs_release_seqid(calldata->arg.seqid);
3614 	nfs_refresh_inode(calldata->inode, &calldata->fattr);
3615 	dprintk("%s: ret = %d\n", __func__, task->tk_status);
3616 	return;
3617 out_restart:
3618 	task->tk_status = 0;
3619 	rpc_restart_call_prepare(task);
3620 	goto out_release;
3621 }
3622 
3623 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3624 {
3625 	struct nfs4_closedata *calldata = data;
3626 	struct nfs4_state *state = calldata->state;
3627 	struct inode *inode = calldata->inode;
3628 	struct nfs_server *server = NFS_SERVER(inode);
3629 	struct pnfs_layout_hdr *lo;
3630 	bool is_rdonly, is_wronly, is_rdwr;
3631 	int call_close = 0;
3632 
3633 	if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3634 		goto out_wait;
3635 
3636 	task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3637 	spin_lock(&state->owner->so_lock);
3638 	is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3639 	is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3640 	is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3641 	/* Calculate the change in open mode */
3642 	calldata->arg.fmode = 0;
3643 	if (state->n_rdwr == 0) {
3644 		if (state->n_rdonly == 0)
3645 			call_close |= is_rdonly;
3646 		else if (is_rdonly)
3647 			calldata->arg.fmode |= FMODE_READ;
3648 		if (state->n_wronly == 0)
3649 			call_close |= is_wronly;
3650 		else if (is_wronly)
3651 			calldata->arg.fmode |= FMODE_WRITE;
3652 		if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3653 			call_close |= is_rdwr;
3654 	} else if (is_rdwr)
3655 		calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3656 
3657 	nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3658 	if (!nfs4_valid_open_stateid(state))
3659 		call_close = 0;
3660 	spin_unlock(&state->owner->so_lock);
3661 
3662 	if (!call_close) {
3663 		/* Note: exit _without_ calling nfs4_close_done */
3664 		goto out_no_action;
3665 	}
3666 
3667 	if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3668 		nfs_release_seqid(calldata->arg.seqid);
3669 		goto out_wait;
3670 	}
3671 
3672 	lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3673 	if (lo && !pnfs_layout_is_valid(lo)) {
3674 		calldata->arg.lr_args = NULL;
3675 		calldata->res.lr_res = NULL;
3676 	}
3677 
3678 	if (calldata->arg.fmode == 0)
3679 		task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3680 
3681 	if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3682 		/* Close-to-open cache consistency revalidation */
3683 		if (!nfs4_have_delegation(inode, FMODE_READ)) {
3684 			nfs4_bitmask_set(calldata->arg.bitmask_store,
3685 					 server->cache_consistency_bitmask,
3686 					 inode, 0);
3687 			calldata->arg.bitmask = calldata->arg.bitmask_store;
3688 		} else
3689 			calldata->arg.bitmask = NULL;
3690 	}
3691 
3692 	calldata->arg.share_access =
3693 		nfs4_map_atomic_open_share(NFS_SERVER(inode),
3694 				calldata->arg.fmode, 0);
3695 
3696 	if (calldata->res.fattr == NULL)
3697 		calldata->arg.bitmask = NULL;
3698 	else if (calldata->arg.bitmask == NULL)
3699 		calldata->res.fattr = NULL;
3700 	calldata->timestamp = jiffies;
3701 	if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3702 				&calldata->arg.seq_args,
3703 				&calldata->res.seq_res,
3704 				task) != 0)
3705 		nfs_release_seqid(calldata->arg.seqid);
3706 	return;
3707 out_no_action:
3708 	task->tk_action = NULL;
3709 out_wait:
3710 	nfs4_sequence_done(task, &calldata->res.seq_res);
3711 }
3712 
3713 static const struct rpc_call_ops nfs4_close_ops = {
3714 	.rpc_call_prepare = nfs4_close_prepare,
3715 	.rpc_call_done = nfs4_close_done,
3716 	.rpc_release = nfs4_free_closedata,
3717 };
3718 
3719 /*
3720  * It is possible for data to be read/written from a mem-mapped file
3721  * after the sys_close call (which hits the vfs layer as a flush).
3722  * This means that we can't safely call nfsv4 close on a file until
3723  * the inode is cleared. This in turn means that we are not good
3724  * NFSv4 citizens - we do not indicate to the server to update the file's
3725  * share state even when we are done with one of the three share
3726  * stateid's in the inode.
3727  *
3728  * NOTE: Caller must be holding the sp->so_owner semaphore!
3729  */
3730 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3731 {
3732 	struct nfs_server *server = NFS_SERVER(state->inode);
3733 	struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3734 	struct nfs4_closedata *calldata;
3735 	struct nfs4_state_owner *sp = state->owner;
3736 	struct rpc_task *task;
3737 	struct rpc_message msg = {
3738 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3739 		.rpc_cred = state->owner->so_cred,
3740 	};
3741 	struct rpc_task_setup task_setup_data = {
3742 		.rpc_client = server->client,
3743 		.rpc_message = &msg,
3744 		.callback_ops = &nfs4_close_ops,
3745 		.workqueue = nfsiod_workqueue,
3746 		.flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3747 	};
3748 	int status = -ENOMEM;
3749 
3750 	if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
3751 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
3752 
3753 	nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3754 		&task_setup_data.rpc_client, &msg);
3755 
3756 	calldata = kzalloc(sizeof(*calldata), gfp_mask);
3757 	if (calldata == NULL)
3758 		goto out;
3759 	nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3760 	calldata->inode = state->inode;
3761 	calldata->state = state;
3762 	calldata->arg.fh = NFS_FH(state->inode);
3763 	if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3764 		goto out_free_calldata;
3765 	/* Serialization for the sequence id */
3766 	alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3767 	calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3768 	if (IS_ERR(calldata->arg.seqid))
3769 		goto out_free_calldata;
3770 	nfs_fattr_init(&calldata->fattr);
3771 	calldata->arg.fmode = 0;
3772 	calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3773 	calldata->res.fattr = &calldata->fattr;
3774 	calldata->res.seqid = calldata->arg.seqid;
3775 	calldata->res.server = server;
3776 	calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3777 	calldata->lr.roc = pnfs_roc(state->inode,
3778 			&calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3779 	if (calldata->lr.roc) {
3780 		calldata->arg.lr_args = &calldata->lr.arg;
3781 		calldata->res.lr_res = &calldata->lr.res;
3782 	}
3783 	nfs_sb_active(calldata->inode->i_sb);
3784 
3785 	msg.rpc_argp = &calldata->arg;
3786 	msg.rpc_resp = &calldata->res;
3787 	task_setup_data.callback_data = calldata;
3788 	task = rpc_run_task(&task_setup_data);
3789 	if (IS_ERR(task))
3790 		return PTR_ERR(task);
3791 	status = 0;
3792 	if (wait)
3793 		status = rpc_wait_for_completion_task(task);
3794 	rpc_put_task(task);
3795 	return status;
3796 out_free_calldata:
3797 	kfree(calldata);
3798 out:
3799 	nfs4_put_open_state(state);
3800 	nfs4_put_state_owner(sp);
3801 	return status;
3802 }
3803 
3804 static struct inode *
3805 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3806 		int open_flags, struct iattr *attr, int *opened)
3807 {
3808 	struct nfs4_state *state;
3809 	struct nfs4_label l, *label;
3810 
3811 	label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3812 
3813 	/* Protect against concurrent sillydeletes */
3814 	state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3815 
3816 	nfs4_label_release_security(label);
3817 
3818 	if (IS_ERR(state))
3819 		return ERR_CAST(state);
3820 	return state->inode;
3821 }
3822 
3823 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3824 {
3825 	if (ctx->state == NULL)
3826 		return;
3827 	if (is_sync)
3828 		nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3829 	else
3830 		nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3831 }
3832 
3833 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3834 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3835 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
3836 
3837 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3838 {
3839 	u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3840 	struct nfs4_server_caps_arg args = {
3841 		.fhandle = fhandle,
3842 		.bitmask = bitmask,
3843 	};
3844 	struct nfs4_server_caps_res res = {};
3845 	struct rpc_message msg = {
3846 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3847 		.rpc_argp = &args,
3848 		.rpc_resp = &res,
3849 	};
3850 	int status;
3851 	int i;
3852 
3853 	bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3854 		     FATTR4_WORD0_FH_EXPIRE_TYPE |
3855 		     FATTR4_WORD0_LINK_SUPPORT |
3856 		     FATTR4_WORD0_SYMLINK_SUPPORT |
3857 		     FATTR4_WORD0_ACLSUPPORT |
3858 		     FATTR4_WORD0_CASE_INSENSITIVE |
3859 		     FATTR4_WORD0_CASE_PRESERVING;
3860 	if (minorversion)
3861 		bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3862 
3863 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3864 	if (status == 0) {
3865 		/* Sanity check the server answers */
3866 		switch (minorversion) {
3867 		case 0:
3868 			res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3869 			res.attr_bitmask[2] = 0;
3870 			break;
3871 		case 1:
3872 			res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3873 			break;
3874 		case 2:
3875 			res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3876 		}
3877 		memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3878 		server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS |
3879 				  NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL);
3880 		server->fattr_valid = NFS_ATTR_FATTR_V4;
3881 		if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3882 				res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3883 			server->caps |= NFS_CAP_ACLS;
3884 		if (res.has_links != 0)
3885 			server->caps |= NFS_CAP_HARDLINKS;
3886 		if (res.has_symlinks != 0)
3887 			server->caps |= NFS_CAP_SYMLINKS;
3888 		if (res.case_insensitive)
3889 			server->caps |= NFS_CAP_CASE_INSENSITIVE;
3890 		if (res.case_preserving)
3891 			server->caps |= NFS_CAP_CASE_PRESERVING;
3892 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3893 		if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3894 			server->caps |= NFS_CAP_SECURITY_LABEL;
3895 #endif
3896 		if (res.attr_bitmask[0] & FATTR4_WORD0_FS_LOCATIONS)
3897 			server->caps |= NFS_CAP_FS_LOCATIONS;
3898 		if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
3899 			server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
3900 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
3901 			server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
3902 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
3903 			server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
3904 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
3905 			server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
3906 				NFS_ATTR_FATTR_OWNER_NAME);
3907 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
3908 			server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
3909 				NFS_ATTR_FATTR_GROUP_NAME);
3910 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
3911 			server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
3912 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
3913 			server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
3914 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
3915 			server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
3916 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
3917 			server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
3918 		memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3919 				sizeof(server->attr_bitmask));
3920 		server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3921 
3922 		memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3923 		server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3924 		server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3925 		server->cache_consistency_bitmask[2] = 0;
3926 
3927 		/* Avoid a regression due to buggy server */
3928 		for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3929 			res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3930 		memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3931 			sizeof(server->exclcreat_bitmask));
3932 
3933 		server->acl_bitmask = res.acl_bitmask;
3934 		server->fh_expire_type = res.fh_expire_type;
3935 	}
3936 
3937 	return status;
3938 }
3939 
3940 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3941 {
3942 	struct nfs4_exception exception = {
3943 		.interruptible = true,
3944 	};
3945 	int err;
3946 
3947 	nfs4_server_set_init_caps(server);
3948 	do {
3949 		err = nfs4_handle_exception(server,
3950 				_nfs4_server_capabilities(server, fhandle),
3951 				&exception);
3952 	} while (exception.retry);
3953 	return err;
3954 }
3955 
3956 static void test_fs_location_for_trunking(struct nfs4_fs_location *location,
3957 					  struct nfs_client *clp,
3958 					  struct nfs_server *server)
3959 {
3960 	int i;
3961 
3962 	for (i = 0; i < location->nservers; i++) {
3963 		struct nfs4_string *srv_loc = &location->servers[i];
3964 		struct sockaddr_storage addr;
3965 		size_t addrlen;
3966 		struct xprt_create xprt_args = {
3967 			.ident = 0,
3968 			.net = clp->cl_net,
3969 		};
3970 		struct nfs4_add_xprt_data xprtdata = {
3971 			.clp = clp,
3972 		};
3973 		struct rpc_add_xprt_test rpcdata = {
3974 			.add_xprt_test = clp->cl_mvops->session_trunk,
3975 			.data = &xprtdata,
3976 		};
3977 		char *servername = NULL;
3978 
3979 		if (!srv_loc->len)
3980 			continue;
3981 
3982 		addrlen = nfs_parse_server_name(srv_loc->data, srv_loc->len,
3983 						&addr, sizeof(addr),
3984 						clp->cl_net, server->port);
3985 		if (!addrlen)
3986 			return;
3987 		xprt_args.dstaddr = (struct sockaddr *)&addr;
3988 		xprt_args.addrlen = addrlen;
3989 		servername = kmalloc(srv_loc->len + 1, GFP_KERNEL);
3990 		if (!servername)
3991 			return;
3992 		memcpy(servername, srv_loc->data, srv_loc->len);
3993 		servername[srv_loc->len] = '\0';
3994 		xprt_args.servername = servername;
3995 
3996 		xprtdata.cred = nfs4_get_clid_cred(clp);
3997 		rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
3998 				  rpc_clnt_setup_test_and_add_xprt,
3999 				  &rpcdata);
4000 		if (xprtdata.cred)
4001 			put_cred(xprtdata.cred);
4002 		kfree(servername);
4003 	}
4004 }
4005 
4006 static bool _is_same_nfs4_pathname(struct nfs4_pathname *path1,
4007 				   struct nfs4_pathname *path2)
4008 {
4009 	int i;
4010 
4011 	if (path1->ncomponents != path2->ncomponents)
4012 		return false;
4013 	for (i = 0; i < path1->ncomponents; i++) {
4014 		if (path1->components[i].len != path2->components[i].len)
4015 			return false;
4016 		if (memcmp(path1->components[i].data, path2->components[i].data,
4017 				path1->components[i].len))
4018 			return false;
4019 	}
4020 	return true;
4021 }
4022 
4023 static int _nfs4_discover_trunking(struct nfs_server *server,
4024 				   struct nfs_fh *fhandle)
4025 {
4026 	struct nfs4_fs_locations *locations = NULL;
4027 	struct page *page;
4028 	const struct cred *cred;
4029 	struct nfs_client *clp = server->nfs_client;
4030 	const struct nfs4_state_maintenance_ops *ops =
4031 		clp->cl_mvops->state_renewal_ops;
4032 	int status = -ENOMEM, i;
4033 
4034 	cred = ops->get_state_renewal_cred(clp);
4035 	if (cred == NULL) {
4036 		cred = nfs4_get_clid_cred(clp);
4037 		if (cred == NULL)
4038 			return -ENOKEY;
4039 	}
4040 
4041 	page = alloc_page(GFP_KERNEL);
4042 	if (!page)
4043 		goto out_put_cred;
4044 	locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4045 	if (!locations)
4046 		goto out_free;
4047 	locations->fattr = nfs_alloc_fattr();
4048 	if (!locations->fattr)
4049 		goto out_free_2;
4050 
4051 	status = nfs4_proc_get_locations(server, fhandle, locations, page,
4052 					 cred);
4053 	if (status)
4054 		goto out_free_3;
4055 
4056 	for (i = 0; i < locations->nlocations; i++) {
4057 		if (!_is_same_nfs4_pathname(&locations->fs_path,
4058 					&locations->locations[i].rootpath))
4059 			continue;
4060 		test_fs_location_for_trunking(&locations->locations[i], clp,
4061 					      server);
4062 	}
4063 out_free_3:
4064 	kfree(locations->fattr);
4065 out_free_2:
4066 	kfree(locations);
4067 out_free:
4068 	__free_page(page);
4069 out_put_cred:
4070 	put_cred(cred);
4071 	return status;
4072 }
4073 
4074 static int nfs4_discover_trunking(struct nfs_server *server,
4075 				  struct nfs_fh *fhandle)
4076 {
4077 	struct nfs4_exception exception = {
4078 		.interruptible = true,
4079 	};
4080 	struct nfs_client *clp = server->nfs_client;
4081 	int err = 0;
4082 
4083 	if (!nfs4_has_session(clp))
4084 		goto out;
4085 	do {
4086 		err = nfs4_handle_exception(server,
4087 				_nfs4_discover_trunking(server, fhandle),
4088 				&exception);
4089 	} while (exception.retry);
4090 out:
4091 	return err;
4092 }
4093 
4094 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4095 		struct nfs_fsinfo *info)
4096 {
4097 	u32 bitmask[3];
4098 	struct nfs4_lookup_root_arg args = {
4099 		.bitmask = bitmask,
4100 	};
4101 	struct nfs4_lookup_res res = {
4102 		.server = server,
4103 		.fattr = info->fattr,
4104 		.fh = fhandle,
4105 	};
4106 	struct rpc_message msg = {
4107 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
4108 		.rpc_argp = &args,
4109 		.rpc_resp = &res,
4110 	};
4111 
4112 	bitmask[0] = nfs4_fattr_bitmap[0];
4113 	bitmask[1] = nfs4_fattr_bitmap[1];
4114 	/*
4115 	 * Process the label in the upcoming getfattr
4116 	 */
4117 	bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
4118 
4119 	nfs_fattr_init(info->fattr);
4120 	return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4121 }
4122 
4123 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4124 		struct nfs_fsinfo *info)
4125 {
4126 	struct nfs4_exception exception = {
4127 		.interruptible = true,
4128 	};
4129 	int err;
4130 	do {
4131 		err = _nfs4_lookup_root(server, fhandle, info);
4132 		trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
4133 		switch (err) {
4134 		case 0:
4135 		case -NFS4ERR_WRONGSEC:
4136 			goto out;
4137 		default:
4138 			err = nfs4_handle_exception(server, err, &exception);
4139 		}
4140 	} while (exception.retry);
4141 out:
4142 	return err;
4143 }
4144 
4145 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4146 				struct nfs_fsinfo *info, rpc_authflavor_t flavor)
4147 {
4148 	struct rpc_auth_create_args auth_args = {
4149 		.pseudoflavor = flavor,
4150 	};
4151 	struct rpc_auth *auth;
4152 
4153 	auth = rpcauth_create(&auth_args, server->client);
4154 	if (IS_ERR(auth))
4155 		return -EACCES;
4156 	return nfs4_lookup_root(server, fhandle, info);
4157 }
4158 
4159 /*
4160  * Retry pseudoroot lookup with various security flavors.  We do this when:
4161  *
4162  *   NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4163  *   NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4164  *
4165  * Returns zero on success, or a negative NFS4ERR value, or a
4166  * negative errno value.
4167  */
4168 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4169 			      struct nfs_fsinfo *info)
4170 {
4171 	/* Per 3530bis 15.33.5 */
4172 	static const rpc_authflavor_t flav_array[] = {
4173 		RPC_AUTH_GSS_KRB5P,
4174 		RPC_AUTH_GSS_KRB5I,
4175 		RPC_AUTH_GSS_KRB5,
4176 		RPC_AUTH_UNIX,			/* courtesy */
4177 		RPC_AUTH_NULL,
4178 	};
4179 	int status = -EPERM;
4180 	size_t i;
4181 
4182 	if (server->auth_info.flavor_len > 0) {
4183 		/* try each flavor specified by user */
4184 		for (i = 0; i < server->auth_info.flavor_len; i++) {
4185 			status = nfs4_lookup_root_sec(server, fhandle, info,
4186 						server->auth_info.flavors[i]);
4187 			if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4188 				continue;
4189 			break;
4190 		}
4191 	} else {
4192 		/* no flavors specified by user, try default list */
4193 		for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4194 			status = nfs4_lookup_root_sec(server, fhandle, info,
4195 						      flav_array[i]);
4196 			if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4197 				continue;
4198 			break;
4199 		}
4200 	}
4201 
4202 	/*
4203 	 * -EACCES could mean that the user doesn't have correct permissions
4204 	 * to access the mount.  It could also mean that we tried to mount
4205 	 * with a gss auth flavor, but rpc.gssd isn't running.  Either way,
4206 	 * existing mount programs don't handle -EACCES very well so it should
4207 	 * be mapped to -EPERM instead.
4208 	 */
4209 	if (status == -EACCES)
4210 		status = -EPERM;
4211 	return status;
4212 }
4213 
4214 /**
4215  * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4216  * @server: initialized nfs_server handle
4217  * @fhandle: we fill in the pseudo-fs root file handle
4218  * @info: we fill in an FSINFO struct
4219  * @auth_probe: probe the auth flavours
4220  *
4221  * Returns zero on success, or a negative errno.
4222  */
4223 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4224 			 struct nfs_fsinfo *info,
4225 			 bool auth_probe)
4226 {
4227 	int status = 0;
4228 
4229 	if (!auth_probe)
4230 		status = nfs4_lookup_root(server, fhandle, info);
4231 
4232 	if (auth_probe || status == NFS4ERR_WRONGSEC)
4233 		status = server->nfs_client->cl_mvops->find_root_sec(server,
4234 				fhandle, info);
4235 
4236 	if (status == 0)
4237 		status = nfs4_server_capabilities(server, fhandle);
4238 	if (status == 0)
4239 		status = nfs4_do_fsinfo(server, fhandle, info);
4240 
4241 	return nfs4_map_errors(status);
4242 }
4243 
4244 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4245 			      struct nfs_fsinfo *info)
4246 {
4247 	int error;
4248 	struct nfs_fattr *fattr = info->fattr;
4249 
4250 	error = nfs4_server_capabilities(server, mntfh);
4251 	if (error < 0) {
4252 		dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4253 		return error;
4254 	}
4255 
4256 	error = nfs4_proc_getattr(server, mntfh, fattr, NULL);
4257 	if (error < 0) {
4258 		dprintk("nfs4_get_root: getattr error = %d\n", -error);
4259 		goto out;
4260 	}
4261 
4262 	if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4263 	    !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4264 		memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4265 
4266 out:
4267 	return error;
4268 }
4269 
4270 /*
4271  * Get locations and (maybe) other attributes of a referral.
4272  * Note that we'll actually follow the referral later when
4273  * we detect fsid mismatch in inode revalidation
4274  */
4275 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4276 			     const struct qstr *name, struct nfs_fattr *fattr,
4277 			     struct nfs_fh *fhandle)
4278 {
4279 	int status = -ENOMEM;
4280 	struct page *page = NULL;
4281 	struct nfs4_fs_locations *locations = NULL;
4282 
4283 	page = alloc_page(GFP_KERNEL);
4284 	if (page == NULL)
4285 		goto out;
4286 	locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4287 	if (locations == NULL)
4288 		goto out;
4289 
4290 	locations->fattr = fattr;
4291 
4292 	status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4293 	if (status != 0)
4294 		goto out;
4295 
4296 	/*
4297 	 * If the fsid didn't change, this is a migration event, not a
4298 	 * referral.  Cause us to drop into the exception handler, which
4299 	 * will kick off migration recovery.
4300 	 */
4301 	if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &fattr->fsid)) {
4302 		dprintk("%s: server did not return a different fsid for"
4303 			" a referral at %s\n", __func__, name->name);
4304 		status = -NFS4ERR_MOVED;
4305 		goto out;
4306 	}
4307 	/* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4308 	nfs_fixup_referral_attributes(fattr);
4309 	memset(fhandle, 0, sizeof(struct nfs_fh));
4310 out:
4311 	if (page)
4312 		__free_page(page);
4313 	kfree(locations);
4314 	return status;
4315 }
4316 
4317 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4318 				struct nfs_fattr *fattr, struct inode *inode)
4319 {
4320 	__u32 bitmask[NFS4_BITMASK_SZ];
4321 	struct nfs4_getattr_arg args = {
4322 		.fh = fhandle,
4323 		.bitmask = bitmask,
4324 	};
4325 	struct nfs4_getattr_res res = {
4326 		.fattr = fattr,
4327 		.server = server,
4328 	};
4329 	struct rpc_message msg = {
4330 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4331 		.rpc_argp = &args,
4332 		.rpc_resp = &res,
4333 	};
4334 	unsigned short task_flags = 0;
4335 
4336 	if (nfs4_has_session(server->nfs_client))
4337 		task_flags = RPC_TASK_MOVEABLE;
4338 
4339 	/* Is this is an attribute revalidation, subject to softreval? */
4340 	if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4341 		task_flags |= RPC_TASK_TIMEOUT;
4342 
4343 	nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label), inode, 0);
4344 	nfs_fattr_init(fattr);
4345 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4346 	return nfs4_do_call_sync(server->client, server, &msg,
4347 			&args.seq_args, &res.seq_res, task_flags);
4348 }
4349 
4350 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4351 				struct nfs_fattr *fattr, struct inode *inode)
4352 {
4353 	struct nfs4_exception exception = {
4354 		.interruptible = true,
4355 	};
4356 	int err;
4357 	do {
4358 		err = _nfs4_proc_getattr(server, fhandle, fattr, inode);
4359 		trace_nfs4_getattr(server, fhandle, fattr, err);
4360 		err = nfs4_handle_exception(server, err,
4361 				&exception);
4362 	} while (exception.retry);
4363 	return err;
4364 }
4365 
4366 /*
4367  * The file is not closed if it is opened due to the a request to change
4368  * the size of the file. The open call will not be needed once the
4369  * VFS layer lookup-intents are implemented.
4370  *
4371  * Close is called when the inode is destroyed.
4372  * If we haven't opened the file for O_WRONLY, we
4373  * need to in the size_change case to obtain a stateid.
4374  *
4375  * Got race?
4376  * Because OPEN is always done by name in nfsv4, it is
4377  * possible that we opened a different file by the same
4378  * name.  We can recognize this race condition, but we
4379  * can't do anything about it besides returning an error.
4380  *
4381  * This will be fixed with VFS changes (lookup-intent).
4382  */
4383 static int
4384 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4385 		  struct iattr *sattr)
4386 {
4387 	struct inode *inode = d_inode(dentry);
4388 	const struct cred *cred = NULL;
4389 	struct nfs_open_context *ctx = NULL;
4390 	int status;
4391 
4392 	if (pnfs_ld_layoutret_on_setattr(inode) &&
4393 	    sattr->ia_valid & ATTR_SIZE &&
4394 	    sattr->ia_size < i_size_read(inode))
4395 		pnfs_commit_and_return_layout(inode);
4396 
4397 	nfs_fattr_init(fattr);
4398 
4399 	/* Deal with open(O_TRUNC) */
4400 	if (sattr->ia_valid & ATTR_OPEN)
4401 		sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4402 
4403 	/* Optimization: if the end result is no change, don't RPC */
4404 	if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4405 		return 0;
4406 
4407 	/* Search for an existing open(O_WRITE) file */
4408 	if (sattr->ia_valid & ATTR_FILE) {
4409 
4410 		ctx = nfs_file_open_context(sattr->ia_file);
4411 		if (ctx)
4412 			cred = ctx->cred;
4413 	}
4414 
4415 	/* Return any delegations if we're going to change ACLs */
4416 	if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4417 		nfs4_inode_make_writeable(inode);
4418 
4419 	status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL);
4420 	if (status == 0) {
4421 		nfs_setattr_update_inode(inode, sattr, fattr);
4422 		nfs_setsecurity(inode, fattr);
4423 	}
4424 	return status;
4425 }
4426 
4427 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4428 		struct dentry *dentry, struct nfs_fh *fhandle,
4429 		struct nfs_fattr *fattr)
4430 {
4431 	struct nfs_server *server = NFS_SERVER(dir);
4432 	int		       status;
4433 	struct nfs4_lookup_arg args = {
4434 		.bitmask = server->attr_bitmask,
4435 		.dir_fh = NFS_FH(dir),
4436 		.name = &dentry->d_name,
4437 	};
4438 	struct nfs4_lookup_res res = {
4439 		.server = server,
4440 		.fattr = fattr,
4441 		.fh = fhandle,
4442 	};
4443 	struct rpc_message msg = {
4444 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4445 		.rpc_argp = &args,
4446 		.rpc_resp = &res,
4447 	};
4448 	unsigned short task_flags = 0;
4449 
4450 	if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
4451 		task_flags = RPC_TASK_MOVEABLE;
4452 
4453 	/* Is this is an attribute revalidation, subject to softreval? */
4454 	if (nfs_lookup_is_soft_revalidate(dentry))
4455 		task_flags |= RPC_TASK_TIMEOUT;
4456 
4457 	args.bitmask = nfs4_bitmask(server, fattr->label);
4458 
4459 	nfs_fattr_init(fattr);
4460 
4461 	dprintk("NFS call  lookup %pd2\n", dentry);
4462 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4463 	status = nfs4_do_call_sync(clnt, server, &msg,
4464 			&args.seq_args, &res.seq_res, task_flags);
4465 	dprintk("NFS reply lookup: %d\n", status);
4466 	return status;
4467 }
4468 
4469 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4470 {
4471 	fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4472 		NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4473 	fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4474 	fattr->nlink = 2;
4475 }
4476 
4477 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4478 				   struct dentry *dentry, struct nfs_fh *fhandle,
4479 				   struct nfs_fattr *fattr)
4480 {
4481 	struct nfs4_exception exception = {
4482 		.interruptible = true,
4483 	};
4484 	struct rpc_clnt *client = *clnt;
4485 	const struct qstr *name = &dentry->d_name;
4486 	int err;
4487 	do {
4488 		err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr);
4489 		trace_nfs4_lookup(dir, name, err);
4490 		switch (err) {
4491 		case -NFS4ERR_BADNAME:
4492 			err = -ENOENT;
4493 			goto out;
4494 		case -NFS4ERR_MOVED:
4495 			err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4496 			if (err == -NFS4ERR_MOVED)
4497 				err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4498 			goto out;
4499 		case -NFS4ERR_WRONGSEC:
4500 			err = -EPERM;
4501 			if (client != *clnt)
4502 				goto out;
4503 			client = nfs4_negotiate_security(client, dir, name);
4504 			if (IS_ERR(client))
4505 				return PTR_ERR(client);
4506 
4507 			exception.retry = 1;
4508 			break;
4509 		default:
4510 			err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4511 		}
4512 	} while (exception.retry);
4513 
4514 out:
4515 	if (err == 0)
4516 		*clnt = client;
4517 	else if (client != *clnt)
4518 		rpc_shutdown_client(client);
4519 
4520 	return err;
4521 }
4522 
4523 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4524 			    struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4525 {
4526 	int status;
4527 	struct rpc_clnt *client = NFS_CLIENT(dir);
4528 
4529 	status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4530 	if (client != NFS_CLIENT(dir)) {
4531 		rpc_shutdown_client(client);
4532 		nfs_fixup_secinfo_attributes(fattr);
4533 	}
4534 	return status;
4535 }
4536 
4537 struct rpc_clnt *
4538 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4539 			    struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4540 {
4541 	struct rpc_clnt *client = NFS_CLIENT(dir);
4542 	int status;
4543 
4544 	status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4545 	if (status < 0)
4546 		return ERR_PTR(status);
4547 	return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4548 }
4549 
4550 static int _nfs4_proc_lookupp(struct inode *inode,
4551 		struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4552 {
4553 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
4554 	struct nfs_server *server = NFS_SERVER(inode);
4555 	int		       status;
4556 	struct nfs4_lookupp_arg args = {
4557 		.bitmask = server->attr_bitmask,
4558 		.fh = NFS_FH(inode),
4559 	};
4560 	struct nfs4_lookupp_res res = {
4561 		.server = server,
4562 		.fattr = fattr,
4563 		.fh = fhandle,
4564 	};
4565 	struct rpc_message msg = {
4566 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4567 		.rpc_argp = &args,
4568 		.rpc_resp = &res,
4569 	};
4570 	unsigned short task_flags = 0;
4571 
4572 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4573 		task_flags |= RPC_TASK_TIMEOUT;
4574 
4575 	args.bitmask = nfs4_bitmask(server, fattr->label);
4576 
4577 	nfs_fattr_init(fattr);
4578 
4579 	dprintk("NFS call  lookupp ino=0x%lx\n", inode->i_ino);
4580 	status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4581 				&res.seq_res, task_flags);
4582 	dprintk("NFS reply lookupp: %d\n", status);
4583 	return status;
4584 }
4585 
4586 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4587 			     struct nfs_fattr *fattr)
4588 {
4589 	struct nfs4_exception exception = {
4590 		.interruptible = true,
4591 	};
4592 	int err;
4593 	do {
4594 		err = _nfs4_proc_lookupp(inode, fhandle, fattr);
4595 		trace_nfs4_lookupp(inode, err);
4596 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
4597 				&exception);
4598 	} while (exception.retry);
4599 	return err;
4600 }
4601 
4602 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4603 			     const struct cred *cred)
4604 {
4605 	struct nfs_server *server = NFS_SERVER(inode);
4606 	struct nfs4_accessargs args = {
4607 		.fh = NFS_FH(inode),
4608 		.access = entry->mask,
4609 	};
4610 	struct nfs4_accessres res = {
4611 		.server = server,
4612 	};
4613 	struct rpc_message msg = {
4614 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4615 		.rpc_argp = &args,
4616 		.rpc_resp = &res,
4617 		.rpc_cred = cred,
4618 	};
4619 	int status = 0;
4620 
4621 	if (!nfs4_have_delegation(inode, FMODE_READ)) {
4622 		res.fattr = nfs_alloc_fattr();
4623 		if (res.fattr == NULL)
4624 			return -ENOMEM;
4625 		args.bitmask = server->cache_consistency_bitmask;
4626 	}
4627 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4628 	if (!status) {
4629 		nfs_access_set_mask(entry, res.access);
4630 		if (res.fattr)
4631 			nfs_refresh_inode(inode, res.fattr);
4632 	}
4633 	nfs_free_fattr(res.fattr);
4634 	return status;
4635 }
4636 
4637 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4638 			    const struct cred *cred)
4639 {
4640 	struct nfs4_exception exception = {
4641 		.interruptible = true,
4642 	};
4643 	int err;
4644 	do {
4645 		err = _nfs4_proc_access(inode, entry, cred);
4646 		trace_nfs4_access(inode, err);
4647 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
4648 				&exception);
4649 	} while (exception.retry);
4650 	return err;
4651 }
4652 
4653 /*
4654  * TODO: For the time being, we don't try to get any attributes
4655  * along with any of the zero-copy operations READ, READDIR,
4656  * READLINK, WRITE.
4657  *
4658  * In the case of the first three, we want to put the GETATTR
4659  * after the read-type operation -- this is because it is hard
4660  * to predict the length of a GETATTR response in v4, and thus
4661  * align the READ data correctly.  This means that the GETATTR
4662  * may end up partially falling into the page cache, and we should
4663  * shift it into the 'tail' of the xdr_buf before processing.
4664  * To do this efficiently, we need to know the total length
4665  * of data received, which doesn't seem to be available outside
4666  * of the RPC layer.
4667  *
4668  * In the case of WRITE, we also want to put the GETATTR after
4669  * the operation -- in this case because we want to make sure
4670  * we get the post-operation mtime and size.
4671  *
4672  * Both of these changes to the XDR layer would in fact be quite
4673  * minor, but I decided to leave them for a subsequent patch.
4674  */
4675 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4676 		unsigned int pgbase, unsigned int pglen)
4677 {
4678 	struct nfs4_readlink args = {
4679 		.fh       = NFS_FH(inode),
4680 		.pgbase	  = pgbase,
4681 		.pglen    = pglen,
4682 		.pages    = &page,
4683 	};
4684 	struct nfs4_readlink_res res;
4685 	struct rpc_message msg = {
4686 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4687 		.rpc_argp = &args,
4688 		.rpc_resp = &res,
4689 	};
4690 
4691 	return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4692 }
4693 
4694 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4695 		unsigned int pgbase, unsigned int pglen)
4696 {
4697 	struct nfs4_exception exception = {
4698 		.interruptible = true,
4699 	};
4700 	int err;
4701 	do {
4702 		err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4703 		trace_nfs4_readlink(inode, err);
4704 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
4705 				&exception);
4706 	} while (exception.retry);
4707 	return err;
4708 }
4709 
4710 /*
4711  * This is just for mknod.  open(O_CREAT) will always do ->open_context().
4712  */
4713 static int
4714 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4715 		 int flags)
4716 {
4717 	struct nfs_server *server = NFS_SERVER(dir);
4718 	struct nfs4_label l, *ilabel;
4719 	struct nfs_open_context *ctx;
4720 	struct nfs4_state *state;
4721 	int status = 0;
4722 
4723 	ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4724 	if (IS_ERR(ctx))
4725 		return PTR_ERR(ctx);
4726 
4727 	ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4728 
4729 	if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4730 		sattr->ia_mode &= ~current_umask();
4731 	state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4732 	if (IS_ERR(state)) {
4733 		status = PTR_ERR(state);
4734 		goto out;
4735 	}
4736 out:
4737 	nfs4_label_release_security(ilabel);
4738 	put_nfs_open_context(ctx);
4739 	return status;
4740 }
4741 
4742 static int
4743 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4744 {
4745 	struct nfs_server *server = NFS_SERVER(dir);
4746 	struct nfs_removeargs args = {
4747 		.fh = NFS_FH(dir),
4748 		.name = *name,
4749 	};
4750 	struct nfs_removeres res = {
4751 		.server = server,
4752 	};
4753 	struct rpc_message msg = {
4754 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4755 		.rpc_argp = &args,
4756 		.rpc_resp = &res,
4757 	};
4758 	unsigned long timestamp = jiffies;
4759 	int status;
4760 
4761 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4762 	if (status == 0) {
4763 		spin_lock(&dir->i_lock);
4764 		/* Removing a directory decrements nlink in the parent */
4765 		if (ftype == NF4DIR && dir->i_nlink > 2)
4766 			nfs4_dec_nlink_locked(dir);
4767 		nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4768 					      NFS_INO_INVALID_DATA);
4769 		spin_unlock(&dir->i_lock);
4770 	}
4771 	return status;
4772 }
4773 
4774 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4775 {
4776 	struct nfs4_exception exception = {
4777 		.interruptible = true,
4778 	};
4779 	struct inode *inode = d_inode(dentry);
4780 	int err;
4781 
4782 	if (inode) {
4783 		if (inode->i_nlink == 1)
4784 			nfs4_inode_return_delegation(inode);
4785 		else
4786 			nfs4_inode_make_writeable(inode);
4787 	}
4788 	do {
4789 		err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4790 		trace_nfs4_remove(dir, &dentry->d_name, err);
4791 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
4792 				&exception);
4793 	} while (exception.retry);
4794 	return err;
4795 }
4796 
4797 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4798 {
4799 	struct nfs4_exception exception = {
4800 		.interruptible = true,
4801 	};
4802 	int err;
4803 
4804 	do {
4805 		err = _nfs4_proc_remove(dir, name, NF4DIR);
4806 		trace_nfs4_remove(dir, name, err);
4807 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
4808 				&exception);
4809 	} while (exception.retry);
4810 	return err;
4811 }
4812 
4813 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4814 		struct dentry *dentry,
4815 		struct inode *inode)
4816 {
4817 	struct nfs_removeargs *args = msg->rpc_argp;
4818 	struct nfs_removeres *res = msg->rpc_resp;
4819 
4820 	res->server = NFS_SB(dentry->d_sb);
4821 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4822 	nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4823 
4824 	nfs_fattr_init(res->dir_attr);
4825 
4826 	if (inode) {
4827 		nfs4_inode_return_delegation(inode);
4828 		nfs_d_prune_case_insensitive_aliases(inode);
4829 	}
4830 }
4831 
4832 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4833 {
4834 	nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4835 			&data->args.seq_args,
4836 			&data->res.seq_res,
4837 			task);
4838 }
4839 
4840 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4841 {
4842 	struct nfs_unlinkdata *data = task->tk_calldata;
4843 	struct nfs_removeres *res = &data->res;
4844 
4845 	if (!nfs4_sequence_done(task, &res->seq_res))
4846 		return 0;
4847 	if (nfs4_async_handle_error(task, res->server, NULL,
4848 				    &data->timeout) == -EAGAIN)
4849 		return 0;
4850 	if (task->tk_status == 0)
4851 		nfs4_update_changeattr(dir, &res->cinfo,
4852 				res->dir_attr->time_start,
4853 				NFS_INO_INVALID_DATA);
4854 	return 1;
4855 }
4856 
4857 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4858 		struct dentry *old_dentry,
4859 		struct dentry *new_dentry)
4860 {
4861 	struct nfs_renameargs *arg = msg->rpc_argp;
4862 	struct nfs_renameres *res = msg->rpc_resp;
4863 	struct inode *old_inode = d_inode(old_dentry);
4864 	struct inode *new_inode = d_inode(new_dentry);
4865 
4866 	if (old_inode)
4867 		nfs4_inode_make_writeable(old_inode);
4868 	if (new_inode)
4869 		nfs4_inode_return_delegation(new_inode);
4870 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4871 	res->server = NFS_SB(old_dentry->d_sb);
4872 	nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4873 }
4874 
4875 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4876 {
4877 	nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4878 			&data->args.seq_args,
4879 			&data->res.seq_res,
4880 			task);
4881 }
4882 
4883 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4884 				 struct inode *new_dir)
4885 {
4886 	struct nfs_renamedata *data = task->tk_calldata;
4887 	struct nfs_renameres *res = &data->res;
4888 
4889 	if (!nfs4_sequence_done(task, &res->seq_res))
4890 		return 0;
4891 	if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4892 		return 0;
4893 
4894 	if (task->tk_status == 0) {
4895 		nfs_d_prune_case_insensitive_aliases(d_inode(data->old_dentry));
4896 		if (new_dir != old_dir) {
4897 			/* Note: If we moved a directory, nlink will change */
4898 			nfs4_update_changeattr(old_dir, &res->old_cinfo,
4899 					res->old_fattr->time_start,
4900 					NFS_INO_INVALID_NLINK |
4901 					    NFS_INO_INVALID_DATA);
4902 			nfs4_update_changeattr(new_dir, &res->new_cinfo,
4903 					res->new_fattr->time_start,
4904 					NFS_INO_INVALID_NLINK |
4905 					    NFS_INO_INVALID_DATA);
4906 		} else
4907 			nfs4_update_changeattr(old_dir, &res->old_cinfo,
4908 					res->old_fattr->time_start,
4909 					NFS_INO_INVALID_DATA);
4910 	}
4911 	return 1;
4912 }
4913 
4914 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4915 {
4916 	struct nfs_server *server = NFS_SERVER(inode);
4917 	__u32 bitmask[NFS4_BITMASK_SZ];
4918 	struct nfs4_link_arg arg = {
4919 		.fh     = NFS_FH(inode),
4920 		.dir_fh = NFS_FH(dir),
4921 		.name   = name,
4922 		.bitmask = bitmask,
4923 	};
4924 	struct nfs4_link_res res = {
4925 		.server = server,
4926 	};
4927 	struct rpc_message msg = {
4928 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4929 		.rpc_argp = &arg,
4930 		.rpc_resp = &res,
4931 	};
4932 	int status = -ENOMEM;
4933 
4934 	res.fattr = nfs_alloc_fattr_with_label(server);
4935 	if (res.fattr == NULL)
4936 		goto out;
4937 
4938 	nfs4_inode_make_writeable(inode);
4939 	nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.fattr->label), inode,
4940 				NFS_INO_INVALID_CHANGE);
4941 	status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4942 	if (!status) {
4943 		nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4944 				       NFS_INO_INVALID_DATA);
4945 		nfs4_inc_nlink(inode);
4946 		status = nfs_post_op_update_inode(inode, res.fattr);
4947 		if (!status)
4948 			nfs_setsecurity(inode, res.fattr);
4949 	}
4950 
4951 out:
4952 	nfs_free_fattr(res.fattr);
4953 	return status;
4954 }
4955 
4956 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4957 {
4958 	struct nfs4_exception exception = {
4959 		.interruptible = true,
4960 	};
4961 	int err;
4962 	do {
4963 		err = nfs4_handle_exception(NFS_SERVER(inode),
4964 				_nfs4_proc_link(inode, dir, name),
4965 				&exception);
4966 	} while (exception.retry);
4967 	return err;
4968 }
4969 
4970 struct nfs4_createdata {
4971 	struct rpc_message msg;
4972 	struct nfs4_create_arg arg;
4973 	struct nfs4_create_res res;
4974 	struct nfs_fh fh;
4975 	struct nfs_fattr fattr;
4976 };
4977 
4978 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4979 		const struct qstr *name, struct iattr *sattr, u32 ftype)
4980 {
4981 	struct nfs4_createdata *data;
4982 
4983 	data = kzalloc(sizeof(*data), GFP_KERNEL);
4984 	if (data != NULL) {
4985 		struct nfs_server *server = NFS_SERVER(dir);
4986 
4987 		data->fattr.label = nfs4_label_alloc(server, GFP_KERNEL);
4988 		if (IS_ERR(data->fattr.label))
4989 			goto out_free;
4990 
4991 		data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4992 		data->msg.rpc_argp = &data->arg;
4993 		data->msg.rpc_resp = &data->res;
4994 		data->arg.dir_fh = NFS_FH(dir);
4995 		data->arg.server = server;
4996 		data->arg.name = name;
4997 		data->arg.attrs = sattr;
4998 		data->arg.ftype = ftype;
4999 		data->arg.bitmask = nfs4_bitmask(server, data->fattr.label);
5000 		data->arg.umask = current_umask();
5001 		data->res.server = server;
5002 		data->res.fh = &data->fh;
5003 		data->res.fattr = &data->fattr;
5004 		nfs_fattr_init(data->res.fattr);
5005 	}
5006 	return data;
5007 out_free:
5008 	kfree(data);
5009 	return NULL;
5010 }
5011 
5012 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
5013 {
5014 	int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
5015 				    &data->arg.seq_args, &data->res.seq_res, 1);
5016 	if (status == 0) {
5017 		spin_lock(&dir->i_lock);
5018 		/* Creating a directory bumps nlink in the parent */
5019 		if (data->arg.ftype == NF4DIR)
5020 			nfs4_inc_nlink_locked(dir);
5021 		nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
5022 					      data->res.fattr->time_start,
5023 					      NFS_INO_INVALID_DATA);
5024 		spin_unlock(&dir->i_lock);
5025 		status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
5026 	}
5027 	return status;
5028 }
5029 
5030 static void nfs4_free_createdata(struct nfs4_createdata *data)
5031 {
5032 	nfs4_label_free(data->fattr.label);
5033 	kfree(data);
5034 }
5035 
5036 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5037 		struct page *page, unsigned int len, struct iattr *sattr,
5038 		struct nfs4_label *label)
5039 {
5040 	struct nfs4_createdata *data;
5041 	int status = -ENAMETOOLONG;
5042 
5043 	if (len > NFS4_MAXPATHLEN)
5044 		goto out;
5045 
5046 	status = -ENOMEM;
5047 	data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
5048 	if (data == NULL)
5049 		goto out;
5050 
5051 	data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
5052 	data->arg.u.symlink.pages = &page;
5053 	data->arg.u.symlink.len = len;
5054 	data->arg.label = label;
5055 
5056 	status = nfs4_do_create(dir, dentry, data);
5057 
5058 	nfs4_free_createdata(data);
5059 out:
5060 	return status;
5061 }
5062 
5063 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5064 		struct page *page, unsigned int len, struct iattr *sattr)
5065 {
5066 	struct nfs4_exception exception = {
5067 		.interruptible = true,
5068 	};
5069 	struct nfs4_label l, *label;
5070 	int err;
5071 
5072 	label = nfs4_label_init_security(dir, dentry, sattr, &l);
5073 
5074 	do {
5075 		err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
5076 		trace_nfs4_symlink(dir, &dentry->d_name, err);
5077 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
5078 				&exception);
5079 	} while (exception.retry);
5080 
5081 	nfs4_label_release_security(label);
5082 	return err;
5083 }
5084 
5085 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5086 		struct iattr *sattr, struct nfs4_label *label)
5087 {
5088 	struct nfs4_createdata *data;
5089 	int status = -ENOMEM;
5090 
5091 	data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
5092 	if (data == NULL)
5093 		goto out;
5094 
5095 	data->arg.label = label;
5096 	status = nfs4_do_create(dir, dentry, data);
5097 
5098 	nfs4_free_createdata(data);
5099 out:
5100 	return status;
5101 }
5102 
5103 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5104 		struct iattr *sattr)
5105 {
5106 	struct nfs_server *server = NFS_SERVER(dir);
5107 	struct nfs4_exception exception = {
5108 		.interruptible = true,
5109 	};
5110 	struct nfs4_label l, *label;
5111 	int err;
5112 
5113 	label = nfs4_label_init_security(dir, dentry, sattr, &l);
5114 
5115 	if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5116 		sattr->ia_mode &= ~current_umask();
5117 	do {
5118 		err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
5119 		trace_nfs4_mkdir(dir, &dentry->d_name, err);
5120 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
5121 				&exception);
5122 	} while (exception.retry);
5123 	nfs4_label_release_security(label);
5124 
5125 	return err;
5126 }
5127 
5128 static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
5129 			      struct nfs_readdir_res *nr_res)
5130 {
5131 	struct inode		*dir = d_inode(nr_arg->dentry);
5132 	struct nfs_server	*server = NFS_SERVER(dir);
5133 	struct nfs4_readdir_arg args = {
5134 		.fh = NFS_FH(dir),
5135 		.pages = nr_arg->pages,
5136 		.pgbase = 0,
5137 		.count = nr_arg->page_len,
5138 		.plus = nr_arg->plus,
5139 	};
5140 	struct nfs4_readdir_res res;
5141 	struct rpc_message msg = {
5142 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5143 		.rpc_argp = &args,
5144 		.rpc_resp = &res,
5145 		.rpc_cred = nr_arg->cred,
5146 	};
5147 	int			status;
5148 
5149 	dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
5150 		nr_arg->dentry, (unsigned long long)nr_arg->cookie);
5151 	if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5152 		args.bitmask = server->attr_bitmask_nl;
5153 	else
5154 		args.bitmask = server->attr_bitmask;
5155 
5156 	nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
5157 	res.pgbase = args.pgbase;
5158 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5159 			&res.seq_res, 0);
5160 	if (status >= 0) {
5161 		memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
5162 		status += args.pgbase;
5163 	}
5164 
5165 	nfs_invalidate_atime(dir);
5166 
5167 	dprintk("%s: returns %d\n", __func__, status);
5168 	return status;
5169 }
5170 
5171 static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5172 			     struct nfs_readdir_res *res)
5173 {
5174 	struct nfs4_exception exception = {
5175 		.interruptible = true,
5176 	};
5177 	int err;
5178 	do {
5179 		err = _nfs4_proc_readdir(arg, res);
5180 		trace_nfs4_readdir(d_inode(arg->dentry), err);
5181 		err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5182 					    err, &exception);
5183 	} while (exception.retry);
5184 	return err;
5185 }
5186 
5187 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5188 		struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5189 {
5190 	struct nfs4_createdata *data;
5191 	int mode = sattr->ia_mode;
5192 	int status = -ENOMEM;
5193 
5194 	data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5195 	if (data == NULL)
5196 		goto out;
5197 
5198 	if (S_ISFIFO(mode))
5199 		data->arg.ftype = NF4FIFO;
5200 	else if (S_ISBLK(mode)) {
5201 		data->arg.ftype = NF4BLK;
5202 		data->arg.u.device.specdata1 = MAJOR(rdev);
5203 		data->arg.u.device.specdata2 = MINOR(rdev);
5204 	}
5205 	else if (S_ISCHR(mode)) {
5206 		data->arg.ftype = NF4CHR;
5207 		data->arg.u.device.specdata1 = MAJOR(rdev);
5208 		data->arg.u.device.specdata2 = MINOR(rdev);
5209 	} else if (!S_ISSOCK(mode)) {
5210 		status = -EINVAL;
5211 		goto out_free;
5212 	}
5213 
5214 	data->arg.label = label;
5215 	status = nfs4_do_create(dir, dentry, data);
5216 out_free:
5217 	nfs4_free_createdata(data);
5218 out:
5219 	return status;
5220 }
5221 
5222 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5223 		struct iattr *sattr, dev_t rdev)
5224 {
5225 	struct nfs_server *server = NFS_SERVER(dir);
5226 	struct nfs4_exception exception = {
5227 		.interruptible = true,
5228 	};
5229 	struct nfs4_label l, *label;
5230 	int err;
5231 
5232 	label = nfs4_label_init_security(dir, dentry, sattr, &l);
5233 
5234 	if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5235 		sattr->ia_mode &= ~current_umask();
5236 	do {
5237 		err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5238 		trace_nfs4_mknod(dir, &dentry->d_name, err);
5239 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
5240 				&exception);
5241 	} while (exception.retry);
5242 
5243 	nfs4_label_release_security(label);
5244 
5245 	return err;
5246 }
5247 
5248 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5249 		 struct nfs_fsstat *fsstat)
5250 {
5251 	struct nfs4_statfs_arg args = {
5252 		.fh = fhandle,
5253 		.bitmask = server->attr_bitmask,
5254 	};
5255 	struct nfs4_statfs_res res = {
5256 		.fsstat = fsstat,
5257 	};
5258 	struct rpc_message msg = {
5259 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5260 		.rpc_argp = &args,
5261 		.rpc_resp = &res,
5262 	};
5263 
5264 	nfs_fattr_init(fsstat->fattr);
5265 	return  nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5266 }
5267 
5268 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5269 {
5270 	struct nfs4_exception exception = {
5271 		.interruptible = true,
5272 	};
5273 	int err;
5274 	do {
5275 		err = nfs4_handle_exception(server,
5276 				_nfs4_proc_statfs(server, fhandle, fsstat),
5277 				&exception);
5278 	} while (exception.retry);
5279 	return err;
5280 }
5281 
5282 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5283 		struct nfs_fsinfo *fsinfo)
5284 {
5285 	struct nfs4_fsinfo_arg args = {
5286 		.fh = fhandle,
5287 		.bitmask = server->attr_bitmask,
5288 	};
5289 	struct nfs4_fsinfo_res res = {
5290 		.fsinfo = fsinfo,
5291 	};
5292 	struct rpc_message msg = {
5293 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5294 		.rpc_argp = &args,
5295 		.rpc_resp = &res,
5296 	};
5297 
5298 	return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5299 }
5300 
5301 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5302 {
5303 	struct nfs4_exception exception = {
5304 		.interruptible = true,
5305 	};
5306 	int err;
5307 
5308 	do {
5309 		err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5310 		trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5311 		if (err == 0) {
5312 			nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5313 			break;
5314 		}
5315 		err = nfs4_handle_exception(server, err, &exception);
5316 	} while (exception.retry);
5317 	return err;
5318 }
5319 
5320 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5321 {
5322 	int error;
5323 
5324 	nfs_fattr_init(fsinfo->fattr);
5325 	error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5326 	if (error == 0) {
5327 		/* block layout checks this! */
5328 		server->pnfs_blksize = fsinfo->blksize;
5329 		set_pnfs_layoutdriver(server, fhandle, fsinfo);
5330 	}
5331 
5332 	return error;
5333 }
5334 
5335 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5336 		struct nfs_pathconf *pathconf)
5337 {
5338 	struct nfs4_pathconf_arg args = {
5339 		.fh = fhandle,
5340 		.bitmask = server->attr_bitmask,
5341 	};
5342 	struct nfs4_pathconf_res res = {
5343 		.pathconf = pathconf,
5344 	};
5345 	struct rpc_message msg = {
5346 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5347 		.rpc_argp = &args,
5348 		.rpc_resp = &res,
5349 	};
5350 
5351 	/* None of the pathconf attributes are mandatory to implement */
5352 	if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5353 		memset(pathconf, 0, sizeof(*pathconf));
5354 		return 0;
5355 	}
5356 
5357 	nfs_fattr_init(pathconf->fattr);
5358 	return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5359 }
5360 
5361 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5362 		struct nfs_pathconf *pathconf)
5363 {
5364 	struct nfs4_exception exception = {
5365 		.interruptible = true,
5366 	};
5367 	int err;
5368 
5369 	do {
5370 		err = nfs4_handle_exception(server,
5371 				_nfs4_proc_pathconf(server, fhandle, pathconf),
5372 				&exception);
5373 	} while (exception.retry);
5374 	return err;
5375 }
5376 
5377 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5378 		const struct nfs_open_context *ctx,
5379 		const struct nfs_lock_context *l_ctx,
5380 		fmode_t fmode)
5381 {
5382 	return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5383 }
5384 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5385 
5386 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5387 		const struct nfs_open_context *ctx,
5388 		const struct nfs_lock_context *l_ctx,
5389 		fmode_t fmode)
5390 {
5391 	nfs4_stateid _current_stateid;
5392 
5393 	/* If the current stateid represents a lost lock, then exit */
5394 	if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5395 		return true;
5396 	return nfs4_stateid_match(stateid, &_current_stateid);
5397 }
5398 
5399 static bool nfs4_error_stateid_expired(int err)
5400 {
5401 	switch (err) {
5402 	case -NFS4ERR_DELEG_REVOKED:
5403 	case -NFS4ERR_ADMIN_REVOKED:
5404 	case -NFS4ERR_BAD_STATEID:
5405 	case -NFS4ERR_STALE_STATEID:
5406 	case -NFS4ERR_OLD_STATEID:
5407 	case -NFS4ERR_OPENMODE:
5408 	case -NFS4ERR_EXPIRED:
5409 		return true;
5410 	}
5411 	return false;
5412 }
5413 
5414 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5415 {
5416 	struct nfs_server *server = NFS_SERVER(hdr->inode);
5417 
5418 	trace_nfs4_read(hdr, task->tk_status);
5419 	if (task->tk_status < 0) {
5420 		struct nfs4_exception exception = {
5421 			.inode = hdr->inode,
5422 			.state = hdr->args.context->state,
5423 			.stateid = &hdr->args.stateid,
5424 		};
5425 		task->tk_status = nfs4_async_handle_exception(task,
5426 				server, task->tk_status, &exception);
5427 		if (exception.retry) {
5428 			rpc_restart_call_prepare(task);
5429 			return -EAGAIN;
5430 		}
5431 	}
5432 
5433 	if (task->tk_status > 0)
5434 		renew_lease(server, hdr->timestamp);
5435 	return 0;
5436 }
5437 
5438 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5439 		struct nfs_pgio_args *args)
5440 {
5441 
5442 	if (!nfs4_error_stateid_expired(task->tk_status) ||
5443 		nfs4_stateid_is_current(&args->stateid,
5444 				args->context,
5445 				args->lock_context,
5446 				FMODE_READ))
5447 		return false;
5448 	rpc_restart_call_prepare(task);
5449 	return true;
5450 }
5451 
5452 static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5453 					 struct nfs_pgio_header *hdr)
5454 {
5455 	struct nfs_server *server = NFS_SERVER(hdr->inode);
5456 	struct rpc_message *msg = &task->tk_msg;
5457 
5458 	if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5459 	    task->tk_status == -ENOTSUPP) {
5460 		server->caps &= ~NFS_CAP_READ_PLUS;
5461 		msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5462 		rpc_restart_call_prepare(task);
5463 		return true;
5464 	}
5465 	return false;
5466 }
5467 
5468 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5469 {
5470 	if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5471 		return -EAGAIN;
5472 	if (nfs4_read_stateid_changed(task, &hdr->args))
5473 		return -EAGAIN;
5474 	if (nfs4_read_plus_not_supported(task, hdr))
5475 		return -EAGAIN;
5476 	if (task->tk_status > 0)
5477 		nfs_invalidate_atime(hdr->inode);
5478 	return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5479 				    nfs4_read_done_cb(task, hdr);
5480 }
5481 
5482 #if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
5483 static bool nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5484 				    struct rpc_message *msg)
5485 {
5486 	/* Note: We don't use READ_PLUS with pNFS yet */
5487 	if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp) {
5488 		msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5489 		return nfs_read_alloc_scratch(hdr, READ_PLUS_SCRATCH_SIZE);
5490 	}
5491 	return false;
5492 }
5493 #else
5494 static bool nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5495 				    struct rpc_message *msg)
5496 {
5497 	return false;
5498 }
5499 #endif /* CONFIG_NFS_V4_2 */
5500 
5501 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5502 				 struct rpc_message *msg)
5503 {
5504 	hdr->timestamp   = jiffies;
5505 	if (!hdr->pgio_done_cb)
5506 		hdr->pgio_done_cb = nfs4_read_done_cb;
5507 	if (!nfs42_read_plus_support(hdr, msg))
5508 		msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5509 	nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5510 }
5511 
5512 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5513 				      struct nfs_pgio_header *hdr)
5514 {
5515 	if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5516 			&hdr->args.seq_args,
5517 			&hdr->res.seq_res,
5518 			task))
5519 		return 0;
5520 	if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5521 				hdr->args.lock_context,
5522 				hdr->rw_mode) == -EIO)
5523 		return -EIO;
5524 	if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5525 		return -EIO;
5526 	return 0;
5527 }
5528 
5529 static int nfs4_write_done_cb(struct rpc_task *task,
5530 			      struct nfs_pgio_header *hdr)
5531 {
5532 	struct inode *inode = hdr->inode;
5533 
5534 	trace_nfs4_write(hdr, task->tk_status);
5535 	if (task->tk_status < 0) {
5536 		struct nfs4_exception exception = {
5537 			.inode = hdr->inode,
5538 			.state = hdr->args.context->state,
5539 			.stateid = &hdr->args.stateid,
5540 		};
5541 		task->tk_status = nfs4_async_handle_exception(task,
5542 				NFS_SERVER(inode), task->tk_status,
5543 				&exception);
5544 		if (exception.retry) {
5545 			rpc_restart_call_prepare(task);
5546 			return -EAGAIN;
5547 		}
5548 	}
5549 	if (task->tk_status >= 0) {
5550 		renew_lease(NFS_SERVER(inode), hdr->timestamp);
5551 		nfs_writeback_update_inode(hdr);
5552 	}
5553 	return 0;
5554 }
5555 
5556 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5557 		struct nfs_pgio_args *args)
5558 {
5559 
5560 	if (!nfs4_error_stateid_expired(task->tk_status) ||
5561 		nfs4_stateid_is_current(&args->stateid,
5562 				args->context,
5563 				args->lock_context,
5564 				FMODE_WRITE))
5565 		return false;
5566 	rpc_restart_call_prepare(task);
5567 	return true;
5568 }
5569 
5570 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5571 {
5572 	if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5573 		return -EAGAIN;
5574 	if (nfs4_write_stateid_changed(task, &hdr->args))
5575 		return -EAGAIN;
5576 	return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5577 		nfs4_write_done_cb(task, hdr);
5578 }
5579 
5580 static
5581 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5582 {
5583 	/* Don't request attributes for pNFS or O_DIRECT writes */
5584 	if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5585 		return false;
5586 	/* Otherwise, request attributes if and only if we don't hold
5587 	 * a delegation
5588 	 */
5589 	return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5590 }
5591 
5592 void nfs4_bitmask_set(__u32 bitmask[], const __u32 src[],
5593 		      struct inode *inode, unsigned long cache_validity)
5594 {
5595 	struct nfs_server *server = NFS_SERVER(inode);
5596 	unsigned int i;
5597 
5598 	memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
5599 	cache_validity |= READ_ONCE(NFS_I(inode)->cache_validity);
5600 
5601 	if (cache_validity & NFS_INO_INVALID_CHANGE)
5602 		bitmask[0] |= FATTR4_WORD0_CHANGE;
5603 	if (cache_validity & NFS_INO_INVALID_ATIME)
5604 		bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5605 	if (cache_validity & NFS_INO_INVALID_MODE)
5606 		bitmask[1] |= FATTR4_WORD1_MODE;
5607 	if (cache_validity & NFS_INO_INVALID_OTHER)
5608 		bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
5609 	if (cache_validity & NFS_INO_INVALID_NLINK)
5610 		bitmask[1] |= FATTR4_WORD1_NUMLINKS;
5611 	if (cache_validity & NFS_INO_INVALID_CTIME)
5612 		bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
5613 	if (cache_validity & NFS_INO_INVALID_MTIME)
5614 		bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5615 	if (cache_validity & NFS_INO_INVALID_BLOCKS)
5616 		bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5617 
5618 	if (cache_validity & NFS_INO_INVALID_SIZE)
5619 		bitmask[0] |= FATTR4_WORD0_SIZE;
5620 
5621 	for (i = 0; i < NFS4_BITMASK_SZ; i++)
5622 		bitmask[i] &= server->attr_bitmask[i];
5623 }
5624 
5625 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5626 				  struct rpc_message *msg,
5627 				  struct rpc_clnt **clnt)
5628 {
5629 	struct nfs_server *server = NFS_SERVER(hdr->inode);
5630 
5631 	if (!nfs4_write_need_cache_consistency_data(hdr)) {
5632 		hdr->args.bitmask = NULL;
5633 		hdr->res.fattr = NULL;
5634 	} else {
5635 		nfs4_bitmask_set(hdr->args.bitmask_store,
5636 				 server->cache_consistency_bitmask,
5637 				 hdr->inode, NFS_INO_INVALID_BLOCKS);
5638 		hdr->args.bitmask = hdr->args.bitmask_store;
5639 	}
5640 
5641 	if (!hdr->pgio_done_cb)
5642 		hdr->pgio_done_cb = nfs4_write_done_cb;
5643 	hdr->res.server = server;
5644 	hdr->timestamp   = jiffies;
5645 
5646 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5647 	nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5648 	nfs4_state_protect_write(hdr->ds_clp ? hdr->ds_clp : server->nfs_client, clnt, msg, hdr);
5649 }
5650 
5651 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5652 {
5653 	nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5654 			&data->args.seq_args,
5655 			&data->res.seq_res,
5656 			task);
5657 }
5658 
5659 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5660 {
5661 	struct inode *inode = data->inode;
5662 
5663 	trace_nfs4_commit(data, task->tk_status);
5664 	if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5665 				    NULL, NULL) == -EAGAIN) {
5666 		rpc_restart_call_prepare(task);
5667 		return -EAGAIN;
5668 	}
5669 	return 0;
5670 }
5671 
5672 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5673 {
5674 	if (!nfs4_sequence_done(task, &data->res.seq_res))
5675 		return -EAGAIN;
5676 	return data->commit_done_cb(task, data);
5677 }
5678 
5679 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5680 				   struct rpc_clnt **clnt)
5681 {
5682 	struct nfs_server *server = NFS_SERVER(data->inode);
5683 
5684 	if (data->commit_done_cb == NULL)
5685 		data->commit_done_cb = nfs4_commit_done_cb;
5686 	data->res.server = server;
5687 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5688 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5689 	nfs4_state_protect(data->ds_clp ? data->ds_clp : server->nfs_client,
5690 			NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5691 }
5692 
5693 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5694 				struct nfs_commitres *res)
5695 {
5696 	struct inode *dst_inode = file_inode(dst);
5697 	struct nfs_server *server = NFS_SERVER(dst_inode);
5698 	struct rpc_message msg = {
5699 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5700 		.rpc_argp = args,
5701 		.rpc_resp = res,
5702 	};
5703 
5704 	args->fh = NFS_FH(dst_inode);
5705 	return nfs4_call_sync(server->client, server, &msg,
5706 			&args->seq_args, &res->seq_res, 1);
5707 }
5708 
5709 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5710 {
5711 	struct nfs_commitargs args = {
5712 		.offset = offset,
5713 		.count = count,
5714 	};
5715 	struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5716 	struct nfs4_exception exception = { };
5717 	int status;
5718 
5719 	do {
5720 		status = _nfs4_proc_commit(dst, &args, res);
5721 		status = nfs4_handle_exception(dst_server, status, &exception);
5722 	} while (exception.retry);
5723 
5724 	return status;
5725 }
5726 
5727 struct nfs4_renewdata {
5728 	struct nfs_client	*client;
5729 	unsigned long		timestamp;
5730 };
5731 
5732 /*
5733  * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5734  * standalone procedure for queueing an asynchronous RENEW.
5735  */
5736 static void nfs4_renew_release(void *calldata)
5737 {
5738 	struct nfs4_renewdata *data = calldata;
5739 	struct nfs_client *clp = data->client;
5740 
5741 	if (refcount_read(&clp->cl_count) > 1)
5742 		nfs4_schedule_state_renewal(clp);
5743 	nfs_put_client(clp);
5744 	kfree(data);
5745 }
5746 
5747 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5748 {
5749 	struct nfs4_renewdata *data = calldata;
5750 	struct nfs_client *clp = data->client;
5751 	unsigned long timestamp = data->timestamp;
5752 
5753 	trace_nfs4_renew_async(clp, task->tk_status);
5754 	switch (task->tk_status) {
5755 	case 0:
5756 		break;
5757 	case -NFS4ERR_LEASE_MOVED:
5758 		nfs4_schedule_lease_moved_recovery(clp);
5759 		break;
5760 	default:
5761 		/* Unless we're shutting down, schedule state recovery! */
5762 		if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5763 			return;
5764 		if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5765 			nfs4_schedule_lease_recovery(clp);
5766 			return;
5767 		}
5768 		nfs4_schedule_path_down_recovery(clp);
5769 	}
5770 	do_renew_lease(clp, timestamp);
5771 }
5772 
5773 static const struct rpc_call_ops nfs4_renew_ops = {
5774 	.rpc_call_done = nfs4_renew_done,
5775 	.rpc_release = nfs4_renew_release,
5776 };
5777 
5778 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5779 {
5780 	struct rpc_message msg = {
5781 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5782 		.rpc_argp	= clp,
5783 		.rpc_cred	= cred,
5784 	};
5785 	struct nfs4_renewdata *data;
5786 
5787 	if (renew_flags == 0)
5788 		return 0;
5789 	if (!refcount_inc_not_zero(&clp->cl_count))
5790 		return -EIO;
5791 	data = kmalloc(sizeof(*data), GFP_NOFS);
5792 	if (data == NULL) {
5793 		nfs_put_client(clp);
5794 		return -ENOMEM;
5795 	}
5796 	data->client = clp;
5797 	data->timestamp = jiffies;
5798 	return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5799 			&nfs4_renew_ops, data);
5800 }
5801 
5802 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5803 {
5804 	struct rpc_message msg = {
5805 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5806 		.rpc_argp	= clp,
5807 		.rpc_cred	= cred,
5808 	};
5809 	unsigned long now = jiffies;
5810 	int status;
5811 
5812 	status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5813 	if (status < 0)
5814 		return status;
5815 	do_renew_lease(clp, now);
5816 	return 0;
5817 }
5818 
5819 static bool nfs4_server_supports_acls(const struct nfs_server *server,
5820 				      enum nfs4_acl_type type)
5821 {
5822 	switch (type) {
5823 	default:
5824 		return server->attr_bitmask[0] & FATTR4_WORD0_ACL;
5825 	case NFS4ACL_DACL:
5826 		return server->attr_bitmask[1] & FATTR4_WORD1_DACL;
5827 	case NFS4ACL_SACL:
5828 		return server->attr_bitmask[1] & FATTR4_WORD1_SACL;
5829 	}
5830 }
5831 
5832 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5833  * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5834  * the stack.
5835  */
5836 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5837 
5838 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5839 		struct page **pages)
5840 {
5841 	struct page *newpage, **spages;
5842 	int rc = 0;
5843 	size_t len;
5844 	spages = pages;
5845 
5846 	do {
5847 		len = min_t(size_t, PAGE_SIZE, buflen);
5848 		newpage = alloc_page(GFP_KERNEL);
5849 
5850 		if (newpage == NULL)
5851 			goto unwind;
5852 		memcpy(page_address(newpage), buf, len);
5853 		buf += len;
5854 		buflen -= len;
5855 		*pages++ = newpage;
5856 		rc++;
5857 	} while (buflen != 0);
5858 
5859 	return rc;
5860 
5861 unwind:
5862 	for(; rc > 0; rc--)
5863 		__free_page(spages[rc-1]);
5864 	return -ENOMEM;
5865 }
5866 
5867 struct nfs4_cached_acl {
5868 	enum nfs4_acl_type type;
5869 	int cached;
5870 	size_t len;
5871 	char data[];
5872 };
5873 
5874 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5875 {
5876 	struct nfs_inode *nfsi = NFS_I(inode);
5877 
5878 	spin_lock(&inode->i_lock);
5879 	kfree(nfsi->nfs4_acl);
5880 	nfsi->nfs4_acl = acl;
5881 	spin_unlock(&inode->i_lock);
5882 }
5883 
5884 static void nfs4_zap_acl_attr(struct inode *inode)
5885 {
5886 	nfs4_set_cached_acl(inode, NULL);
5887 }
5888 
5889 static ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf,
5890 				    size_t buflen, enum nfs4_acl_type type)
5891 {
5892 	struct nfs_inode *nfsi = NFS_I(inode);
5893 	struct nfs4_cached_acl *acl;
5894 	int ret = -ENOENT;
5895 
5896 	spin_lock(&inode->i_lock);
5897 	acl = nfsi->nfs4_acl;
5898 	if (acl == NULL)
5899 		goto out;
5900 	if (acl->type != type)
5901 		goto out;
5902 	if (buf == NULL) /* user is just asking for length */
5903 		goto out_len;
5904 	if (acl->cached == 0)
5905 		goto out;
5906 	ret = -ERANGE; /* see getxattr(2) man page */
5907 	if (acl->len > buflen)
5908 		goto out;
5909 	memcpy(buf, acl->data, acl->len);
5910 out_len:
5911 	ret = acl->len;
5912 out:
5913 	spin_unlock(&inode->i_lock);
5914 	return ret;
5915 }
5916 
5917 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages,
5918 				  size_t pgbase, size_t acl_len,
5919 				  enum nfs4_acl_type type)
5920 {
5921 	struct nfs4_cached_acl *acl;
5922 	size_t buflen = sizeof(*acl) + acl_len;
5923 
5924 	if (buflen <= PAGE_SIZE) {
5925 		acl = kmalloc(buflen, GFP_KERNEL);
5926 		if (acl == NULL)
5927 			goto out;
5928 		acl->cached = 1;
5929 		_copy_from_pages(acl->data, pages, pgbase, acl_len);
5930 	} else {
5931 		acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5932 		if (acl == NULL)
5933 			goto out;
5934 		acl->cached = 0;
5935 	}
5936 	acl->type = type;
5937 	acl->len = acl_len;
5938 out:
5939 	nfs4_set_cached_acl(inode, acl);
5940 }
5941 
5942 /*
5943  * The getxattr API returns the required buffer length when called with a
5944  * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5945  * the required buf.  On a NULL buf, we send a page of data to the server
5946  * guessing that the ACL request can be serviced by a page. If so, we cache
5947  * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5948  * the cache. If not so, we throw away the page, and cache the required
5949  * length. The next getxattr call will then produce another round trip to
5950  * the server, this time with the input buf of the required size.
5951  */
5952 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf,
5953 				       size_t buflen, enum nfs4_acl_type type)
5954 {
5955 	struct page **pages;
5956 	struct nfs_getaclargs args = {
5957 		.fh = NFS_FH(inode),
5958 		.acl_type = type,
5959 		.acl_len = buflen,
5960 	};
5961 	struct nfs_getaclres res = {
5962 		.acl_type = type,
5963 		.acl_len = buflen,
5964 	};
5965 	struct rpc_message msg = {
5966 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5967 		.rpc_argp = &args,
5968 		.rpc_resp = &res,
5969 	};
5970 	unsigned int npages;
5971 	int ret = -ENOMEM, i;
5972 	struct nfs_server *server = NFS_SERVER(inode);
5973 
5974 	if (buflen == 0)
5975 		buflen = server->rsize;
5976 
5977 	npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5978 	pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
5979 	if (!pages)
5980 		return -ENOMEM;
5981 
5982 	args.acl_pages = pages;
5983 
5984 	for (i = 0; i < npages; i++) {
5985 		pages[i] = alloc_page(GFP_KERNEL);
5986 		if (!pages[i])
5987 			goto out_free;
5988 	}
5989 
5990 	/* for decoding across pages */
5991 	res.acl_scratch = alloc_page(GFP_KERNEL);
5992 	if (!res.acl_scratch)
5993 		goto out_free;
5994 
5995 	args.acl_len = npages * PAGE_SIZE;
5996 
5997 	dprintk("%s  buf %p buflen %zu npages %d args.acl_len %zu\n",
5998 		__func__, buf, buflen, npages, args.acl_len);
5999 	ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
6000 			     &msg, &args.seq_args, &res.seq_res, 0);
6001 	if (ret)
6002 		goto out_free;
6003 
6004 	/* Handle the case where the passed-in buffer is too short */
6005 	if (res.acl_flags & NFS4_ACL_TRUNC) {
6006 		/* Did the user only issue a request for the acl length? */
6007 		if (buf == NULL)
6008 			goto out_ok;
6009 		ret = -ERANGE;
6010 		goto out_free;
6011 	}
6012 	nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len,
6013 			      type);
6014 	if (buf) {
6015 		if (res.acl_len > buflen) {
6016 			ret = -ERANGE;
6017 			goto out_free;
6018 		}
6019 		_copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
6020 	}
6021 out_ok:
6022 	ret = res.acl_len;
6023 out_free:
6024 	while (--i >= 0)
6025 		__free_page(pages[i]);
6026 	if (res.acl_scratch)
6027 		__free_page(res.acl_scratch);
6028 	kfree(pages);
6029 	return ret;
6030 }
6031 
6032 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf,
6033 				     size_t buflen, enum nfs4_acl_type type)
6034 {
6035 	struct nfs4_exception exception = {
6036 		.interruptible = true,
6037 	};
6038 	ssize_t ret;
6039 	do {
6040 		ret = __nfs4_get_acl_uncached(inode, buf, buflen, type);
6041 		trace_nfs4_get_acl(inode, ret);
6042 		if (ret >= 0)
6043 			break;
6044 		ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
6045 	} while (exception.retry);
6046 	return ret;
6047 }
6048 
6049 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen,
6050 				 enum nfs4_acl_type type)
6051 {
6052 	struct nfs_server *server = NFS_SERVER(inode);
6053 	int ret;
6054 
6055 	if (!nfs4_server_supports_acls(server, type))
6056 		return -EOPNOTSUPP;
6057 	ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
6058 	if (ret < 0)
6059 		return ret;
6060 	if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
6061 		nfs_zap_acl_cache(inode);
6062 	ret = nfs4_read_cached_acl(inode, buf, buflen, type);
6063 	if (ret != -ENOENT)
6064 		/* -ENOENT is returned if there is no ACL or if there is an ACL
6065 		 * but no cached acl data, just the acl length */
6066 		return ret;
6067 	return nfs4_get_acl_uncached(inode, buf, buflen, type);
6068 }
6069 
6070 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf,
6071 			       size_t buflen, enum nfs4_acl_type type)
6072 {
6073 	struct nfs_server *server = NFS_SERVER(inode);
6074 	struct page *pages[NFS4ACL_MAXPAGES];
6075 	struct nfs_setaclargs arg = {
6076 		.fh = NFS_FH(inode),
6077 		.acl_type = type,
6078 		.acl_len = buflen,
6079 		.acl_pages = pages,
6080 	};
6081 	struct nfs_setaclres res;
6082 	struct rpc_message msg = {
6083 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_SETACL],
6084 		.rpc_argp	= &arg,
6085 		.rpc_resp	= &res,
6086 	};
6087 	unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
6088 	int ret, i;
6089 
6090 	/* You can't remove system.nfs4_acl: */
6091 	if (buflen == 0)
6092 		return -EINVAL;
6093 	if (!nfs4_server_supports_acls(server, type))
6094 		return -EOPNOTSUPP;
6095 	if (npages > ARRAY_SIZE(pages))
6096 		return -ERANGE;
6097 	i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
6098 	if (i < 0)
6099 		return i;
6100 	nfs4_inode_make_writeable(inode);
6101 	ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6102 
6103 	/*
6104 	 * Free each page after tx, so the only ref left is
6105 	 * held by the network stack
6106 	 */
6107 	for (; i > 0; i--)
6108 		put_page(pages[i-1]);
6109 
6110 	/*
6111 	 * Acl update can result in inode attribute update.
6112 	 * so mark the attribute cache invalid.
6113 	 */
6114 	spin_lock(&inode->i_lock);
6115 	nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
6116 					     NFS_INO_INVALID_CTIME |
6117 					     NFS_INO_REVAL_FORCED);
6118 	spin_unlock(&inode->i_lock);
6119 	nfs_access_zap_cache(inode);
6120 	nfs_zap_acl_cache(inode);
6121 	return ret;
6122 }
6123 
6124 static int nfs4_proc_set_acl(struct inode *inode, const void *buf,
6125 			     size_t buflen, enum nfs4_acl_type type)
6126 {
6127 	struct nfs4_exception exception = { };
6128 	int err;
6129 	do {
6130 		err = __nfs4_proc_set_acl(inode, buf, buflen, type);
6131 		trace_nfs4_set_acl(inode, err);
6132 		if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
6133 			/*
6134 			 * no need to retry since the kernel
6135 			 * isn't involved in encoding the ACEs.
6136 			 */
6137 			err = -EINVAL;
6138 			break;
6139 		}
6140 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
6141 				&exception);
6142 	} while (exception.retry);
6143 	return err;
6144 }
6145 
6146 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6147 static int _nfs4_get_security_label(struct inode *inode, void *buf,
6148 					size_t buflen)
6149 {
6150 	struct nfs_server *server = NFS_SERVER(inode);
6151 	struct nfs4_label label = {0, 0, buflen, buf};
6152 
6153 	u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6154 	struct nfs_fattr fattr = {
6155 		.label = &label,
6156 	};
6157 	struct nfs4_getattr_arg arg = {
6158 		.fh		= NFS_FH(inode),
6159 		.bitmask	= bitmask,
6160 	};
6161 	struct nfs4_getattr_res res = {
6162 		.fattr		= &fattr,
6163 		.server		= server,
6164 	};
6165 	struct rpc_message msg = {
6166 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
6167 		.rpc_argp	= &arg,
6168 		.rpc_resp	= &res,
6169 	};
6170 	int ret;
6171 
6172 	nfs_fattr_init(&fattr);
6173 
6174 	ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
6175 	if (ret)
6176 		return ret;
6177 	if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6178 		return -ENOENT;
6179 	return label.len;
6180 }
6181 
6182 static int nfs4_get_security_label(struct inode *inode, void *buf,
6183 					size_t buflen)
6184 {
6185 	struct nfs4_exception exception = {
6186 		.interruptible = true,
6187 	};
6188 	int err;
6189 
6190 	if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6191 		return -EOPNOTSUPP;
6192 
6193 	do {
6194 		err = _nfs4_get_security_label(inode, buf, buflen);
6195 		trace_nfs4_get_security_label(inode, err);
6196 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
6197 				&exception);
6198 	} while (exception.retry);
6199 	return err;
6200 }
6201 
6202 static int _nfs4_do_set_security_label(struct inode *inode,
6203 		struct nfs4_label *ilabel,
6204 		struct nfs_fattr *fattr)
6205 {
6206 
6207 	struct iattr sattr = {0};
6208 	struct nfs_server *server = NFS_SERVER(inode);
6209 	const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6210 	struct nfs_setattrargs arg = {
6211 		.fh		= NFS_FH(inode),
6212 		.iap		= &sattr,
6213 		.server		= server,
6214 		.bitmask	= bitmask,
6215 		.label		= ilabel,
6216 	};
6217 	struct nfs_setattrres res = {
6218 		.fattr		= fattr,
6219 		.server		= server,
6220 	};
6221 	struct rpc_message msg = {
6222 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6223 		.rpc_argp	= &arg,
6224 		.rpc_resp	= &res,
6225 	};
6226 	int status;
6227 
6228 	nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6229 
6230 	status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6231 	if (status)
6232 		dprintk("%s failed: %d\n", __func__, status);
6233 
6234 	return status;
6235 }
6236 
6237 static int nfs4_do_set_security_label(struct inode *inode,
6238 		struct nfs4_label *ilabel,
6239 		struct nfs_fattr *fattr)
6240 {
6241 	struct nfs4_exception exception = { };
6242 	int err;
6243 
6244 	do {
6245 		err = _nfs4_do_set_security_label(inode, ilabel, fattr);
6246 		trace_nfs4_set_security_label(inode, err);
6247 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
6248 				&exception);
6249 	} while (exception.retry);
6250 	return err;
6251 }
6252 
6253 static int
6254 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6255 {
6256 	struct nfs4_label ilabel = {0, 0, buflen, (char *)buf };
6257 	struct nfs_fattr *fattr;
6258 	int status;
6259 
6260 	if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6261 		return -EOPNOTSUPP;
6262 
6263 	fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
6264 	if (fattr == NULL)
6265 		return -ENOMEM;
6266 
6267 	status = nfs4_do_set_security_label(inode, &ilabel, fattr);
6268 	if (status == 0)
6269 		nfs_setsecurity(inode, fattr);
6270 
6271 	return status;
6272 }
6273 #endif	/* CONFIG_NFS_V4_SECURITY_LABEL */
6274 
6275 
6276 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6277 				    nfs4_verifier *bootverf)
6278 {
6279 	__be32 verf[2];
6280 
6281 	if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6282 		/* An impossible timestamp guarantees this value
6283 		 * will never match a generated boot time. */
6284 		verf[0] = cpu_to_be32(U32_MAX);
6285 		verf[1] = cpu_to_be32(U32_MAX);
6286 	} else {
6287 		struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6288 		u64 ns = ktime_to_ns(nn->boot_time);
6289 
6290 		verf[0] = cpu_to_be32(ns >> 32);
6291 		verf[1] = cpu_to_be32(ns);
6292 	}
6293 	memcpy(bootverf->data, verf, sizeof(bootverf->data));
6294 }
6295 
6296 static size_t
6297 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6298 {
6299 	struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6300 	struct nfs_netns_client *nn_clp = nn->nfs_client;
6301 	const char *id;
6302 
6303 	buf[0] = '\0';
6304 
6305 	if (nn_clp) {
6306 		rcu_read_lock();
6307 		id = rcu_dereference(nn_clp->identifier);
6308 		if (id)
6309 			strscpy(buf, id, buflen);
6310 		rcu_read_unlock();
6311 	}
6312 
6313 	if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6314 		strscpy(buf, nfs4_client_id_uniquifier, buflen);
6315 
6316 	return strlen(buf);
6317 }
6318 
6319 static int
6320 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6321 {
6322 	char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6323 	size_t buflen;
6324 	size_t len;
6325 	char *str;
6326 
6327 	if (clp->cl_owner_id != NULL)
6328 		return 0;
6329 
6330 	rcu_read_lock();
6331 	len = 14 +
6332 		strlen(clp->cl_rpcclient->cl_nodename) +
6333 		1 +
6334 		strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6335 		1;
6336 	rcu_read_unlock();
6337 
6338 	buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6339 	if (buflen)
6340 		len += buflen + 1;
6341 
6342 	if (len > NFS4_OPAQUE_LIMIT + 1)
6343 		return -EINVAL;
6344 
6345 	/*
6346 	 * Since this string is allocated at mount time, and held until the
6347 	 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6348 	 * about a memory-reclaim deadlock.
6349 	 */
6350 	str = kmalloc(len, GFP_KERNEL);
6351 	if (!str)
6352 		return -ENOMEM;
6353 
6354 	rcu_read_lock();
6355 	if (buflen)
6356 		scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6357 			  clp->cl_rpcclient->cl_nodename, buf,
6358 			  rpc_peeraddr2str(clp->cl_rpcclient,
6359 					   RPC_DISPLAY_ADDR));
6360 	else
6361 		scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6362 			  clp->cl_rpcclient->cl_nodename,
6363 			  rpc_peeraddr2str(clp->cl_rpcclient,
6364 					   RPC_DISPLAY_ADDR));
6365 	rcu_read_unlock();
6366 
6367 	clp->cl_owner_id = str;
6368 	return 0;
6369 }
6370 
6371 static int
6372 nfs4_init_uniform_client_string(struct nfs_client *clp)
6373 {
6374 	char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6375 	size_t buflen;
6376 	size_t len;
6377 	char *str;
6378 
6379 	if (clp->cl_owner_id != NULL)
6380 		return 0;
6381 
6382 	len = 10 + 10 + 1 + 10 + 1 +
6383 		strlen(clp->cl_rpcclient->cl_nodename) + 1;
6384 
6385 	buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6386 	if (buflen)
6387 		len += buflen + 1;
6388 
6389 	if (len > NFS4_OPAQUE_LIMIT + 1)
6390 		return -EINVAL;
6391 
6392 	/*
6393 	 * Since this string is allocated at mount time, and held until the
6394 	 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6395 	 * about a memory-reclaim deadlock.
6396 	 */
6397 	str = kmalloc(len, GFP_KERNEL);
6398 	if (!str)
6399 		return -ENOMEM;
6400 
6401 	if (buflen)
6402 		scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6403 			  clp->rpc_ops->version, clp->cl_minorversion,
6404 			  buf, clp->cl_rpcclient->cl_nodename);
6405 	else
6406 		scnprintf(str, len, "Linux NFSv%u.%u %s",
6407 			  clp->rpc_ops->version, clp->cl_minorversion,
6408 			  clp->cl_rpcclient->cl_nodename);
6409 	clp->cl_owner_id = str;
6410 	return 0;
6411 }
6412 
6413 /*
6414  * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6415  * services.  Advertise one based on the address family of the
6416  * clientaddr.
6417  */
6418 static unsigned int
6419 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6420 {
6421 	if (strchr(clp->cl_ipaddr, ':') != NULL)
6422 		return scnprintf(buf, len, "tcp6");
6423 	else
6424 		return scnprintf(buf, len, "tcp");
6425 }
6426 
6427 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6428 {
6429 	struct nfs4_setclientid *sc = calldata;
6430 
6431 	if (task->tk_status == 0)
6432 		sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6433 }
6434 
6435 static const struct rpc_call_ops nfs4_setclientid_ops = {
6436 	.rpc_call_done = nfs4_setclientid_done,
6437 };
6438 
6439 /**
6440  * nfs4_proc_setclientid - Negotiate client ID
6441  * @clp: state data structure
6442  * @program: RPC program for NFSv4 callback service
6443  * @port: IP port number for NFS4 callback service
6444  * @cred: credential to use for this call
6445  * @res: where to place the result
6446  *
6447  * Returns zero, a negative errno, or a negative NFS4ERR status code.
6448  */
6449 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6450 		unsigned short port, const struct cred *cred,
6451 		struct nfs4_setclientid_res *res)
6452 {
6453 	nfs4_verifier sc_verifier;
6454 	struct nfs4_setclientid setclientid = {
6455 		.sc_verifier = &sc_verifier,
6456 		.sc_prog = program,
6457 		.sc_clnt = clp,
6458 	};
6459 	struct rpc_message msg = {
6460 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6461 		.rpc_argp = &setclientid,
6462 		.rpc_resp = res,
6463 		.rpc_cred = cred,
6464 	};
6465 	struct rpc_task_setup task_setup_data = {
6466 		.rpc_client = clp->cl_rpcclient,
6467 		.rpc_message = &msg,
6468 		.callback_ops = &nfs4_setclientid_ops,
6469 		.callback_data = &setclientid,
6470 		.flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6471 	};
6472 	unsigned long now = jiffies;
6473 	int status;
6474 
6475 	/* nfs_client_id4 */
6476 	nfs4_init_boot_verifier(clp, &sc_verifier);
6477 
6478 	if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6479 		status = nfs4_init_uniform_client_string(clp);
6480 	else
6481 		status = nfs4_init_nonuniform_client_string(clp);
6482 
6483 	if (status)
6484 		goto out;
6485 
6486 	/* cb_client4 */
6487 	setclientid.sc_netid_len =
6488 				nfs4_init_callback_netid(clp,
6489 						setclientid.sc_netid,
6490 						sizeof(setclientid.sc_netid));
6491 	setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6492 				sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6493 				clp->cl_ipaddr, port >> 8, port & 255);
6494 
6495 	dprintk("NFS call  setclientid auth=%s, '%s'\n",
6496 		clp->cl_rpcclient->cl_auth->au_ops->au_name,
6497 		clp->cl_owner_id);
6498 
6499 	status = nfs4_call_sync_custom(&task_setup_data);
6500 	if (setclientid.sc_cred) {
6501 		kfree(clp->cl_acceptor);
6502 		clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6503 		put_rpccred(setclientid.sc_cred);
6504 	}
6505 
6506 	if (status == 0)
6507 		do_renew_lease(clp, now);
6508 out:
6509 	trace_nfs4_setclientid(clp, status);
6510 	dprintk("NFS reply setclientid: %d\n", status);
6511 	return status;
6512 }
6513 
6514 /**
6515  * nfs4_proc_setclientid_confirm - Confirm client ID
6516  * @clp: state data structure
6517  * @arg: result of a previous SETCLIENTID
6518  * @cred: credential to use for this call
6519  *
6520  * Returns zero, a negative errno, or a negative NFS4ERR status code.
6521  */
6522 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6523 		struct nfs4_setclientid_res *arg,
6524 		const struct cred *cred)
6525 {
6526 	struct rpc_message msg = {
6527 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6528 		.rpc_argp = arg,
6529 		.rpc_cred = cred,
6530 	};
6531 	int status;
6532 
6533 	dprintk("NFS call  setclientid_confirm auth=%s, (client ID %llx)\n",
6534 		clp->cl_rpcclient->cl_auth->au_ops->au_name,
6535 		clp->cl_clientid);
6536 	status = rpc_call_sync(clp->cl_rpcclient, &msg,
6537 			       RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6538 	trace_nfs4_setclientid_confirm(clp, status);
6539 	dprintk("NFS reply setclientid_confirm: %d\n", status);
6540 	return status;
6541 }
6542 
6543 struct nfs4_delegreturndata {
6544 	struct nfs4_delegreturnargs args;
6545 	struct nfs4_delegreturnres res;
6546 	struct nfs_fh fh;
6547 	nfs4_stateid stateid;
6548 	unsigned long timestamp;
6549 	struct {
6550 		struct nfs4_layoutreturn_args arg;
6551 		struct nfs4_layoutreturn_res res;
6552 		struct nfs4_xdr_opaque_data ld_private;
6553 		u32 roc_barrier;
6554 		bool roc;
6555 	} lr;
6556 	struct nfs_fattr fattr;
6557 	int rpc_status;
6558 	struct inode *inode;
6559 };
6560 
6561 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6562 {
6563 	struct nfs4_delegreturndata *data = calldata;
6564 	struct nfs4_exception exception = {
6565 		.inode = data->inode,
6566 		.stateid = &data->stateid,
6567 		.task_is_privileged = data->args.seq_args.sa_privileged,
6568 	};
6569 
6570 	if (!nfs4_sequence_done(task, &data->res.seq_res))
6571 		return;
6572 
6573 	trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6574 
6575 	/* Handle Layoutreturn errors */
6576 	if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6577 			  &data->res.lr_ret) == -EAGAIN)
6578 		goto out_restart;
6579 
6580 	switch (task->tk_status) {
6581 	case 0:
6582 		renew_lease(data->res.server, data->timestamp);
6583 		break;
6584 	case -NFS4ERR_ADMIN_REVOKED:
6585 	case -NFS4ERR_DELEG_REVOKED:
6586 	case -NFS4ERR_EXPIRED:
6587 		nfs4_free_revoked_stateid(data->res.server,
6588 				data->args.stateid,
6589 				task->tk_msg.rpc_cred);
6590 		fallthrough;
6591 	case -NFS4ERR_BAD_STATEID:
6592 	case -NFS4ERR_STALE_STATEID:
6593 	case -ETIMEDOUT:
6594 		task->tk_status = 0;
6595 		break;
6596 	case -NFS4ERR_OLD_STATEID:
6597 		if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6598 			nfs4_stateid_seqid_inc(&data->stateid);
6599 		if (data->args.bitmask) {
6600 			data->args.bitmask = NULL;
6601 			data->res.fattr = NULL;
6602 		}
6603 		goto out_restart;
6604 	case -NFS4ERR_ACCESS:
6605 		if (data->args.bitmask) {
6606 			data->args.bitmask = NULL;
6607 			data->res.fattr = NULL;
6608 			goto out_restart;
6609 		}
6610 		fallthrough;
6611 	default:
6612 		task->tk_status = nfs4_async_handle_exception(task,
6613 				data->res.server, task->tk_status,
6614 				&exception);
6615 		if (exception.retry)
6616 			goto out_restart;
6617 	}
6618 	nfs_delegation_mark_returned(data->inode, data->args.stateid);
6619 	data->rpc_status = task->tk_status;
6620 	return;
6621 out_restart:
6622 	task->tk_status = 0;
6623 	rpc_restart_call_prepare(task);
6624 }
6625 
6626 static void nfs4_delegreturn_release(void *calldata)
6627 {
6628 	struct nfs4_delegreturndata *data = calldata;
6629 	struct inode *inode = data->inode;
6630 
6631 	if (data->lr.roc)
6632 		pnfs_roc_release(&data->lr.arg, &data->lr.res,
6633 				 data->res.lr_ret);
6634 	if (inode) {
6635 		nfs4_fattr_set_prechange(&data->fattr,
6636 					 inode_peek_iversion_raw(inode));
6637 		nfs_refresh_inode(inode, &data->fattr);
6638 		nfs_iput_and_deactive(inode);
6639 	}
6640 	kfree(calldata);
6641 }
6642 
6643 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6644 {
6645 	struct nfs4_delegreturndata *d_data;
6646 	struct pnfs_layout_hdr *lo;
6647 
6648 	d_data = data;
6649 
6650 	if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6651 		nfs4_sequence_done(task, &d_data->res.seq_res);
6652 		return;
6653 	}
6654 
6655 	lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6656 	if (lo && !pnfs_layout_is_valid(lo)) {
6657 		d_data->args.lr_args = NULL;
6658 		d_data->res.lr_res = NULL;
6659 	}
6660 
6661 	nfs4_setup_sequence(d_data->res.server->nfs_client,
6662 			&d_data->args.seq_args,
6663 			&d_data->res.seq_res,
6664 			task);
6665 }
6666 
6667 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6668 	.rpc_call_prepare = nfs4_delegreturn_prepare,
6669 	.rpc_call_done = nfs4_delegreturn_done,
6670 	.rpc_release = nfs4_delegreturn_release,
6671 };
6672 
6673 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6674 {
6675 	struct nfs4_delegreturndata *data;
6676 	struct nfs_server *server = NFS_SERVER(inode);
6677 	struct rpc_task *task;
6678 	struct rpc_message msg = {
6679 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6680 		.rpc_cred = cred,
6681 	};
6682 	struct rpc_task_setup task_setup_data = {
6683 		.rpc_client = server->client,
6684 		.rpc_message = &msg,
6685 		.callback_ops = &nfs4_delegreturn_ops,
6686 		.flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
6687 	};
6688 	int status = 0;
6689 
6690 	if (nfs_server_capable(inode, NFS_CAP_MOVEABLE))
6691 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
6692 
6693 	data = kzalloc(sizeof(*data), GFP_KERNEL);
6694 	if (data == NULL)
6695 		return -ENOMEM;
6696 
6697 	nfs4_state_protect(server->nfs_client,
6698 			NFS_SP4_MACH_CRED_CLEANUP,
6699 			&task_setup_data.rpc_client, &msg);
6700 
6701 	data->args.fhandle = &data->fh;
6702 	data->args.stateid = &data->stateid;
6703 	nfs4_bitmask_set(data->args.bitmask_store,
6704 			 server->cache_consistency_bitmask, inode, 0);
6705 	data->args.bitmask = data->args.bitmask_store;
6706 	nfs_copy_fh(&data->fh, NFS_FH(inode));
6707 	nfs4_stateid_copy(&data->stateid, stateid);
6708 	data->res.fattr = &data->fattr;
6709 	data->res.server = server;
6710 	data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6711 	data->lr.arg.ld_private = &data->lr.ld_private;
6712 	nfs_fattr_init(data->res.fattr);
6713 	data->timestamp = jiffies;
6714 	data->rpc_status = 0;
6715 	data->inode = nfs_igrab_and_active(inode);
6716 	if (data->inode || issync) {
6717 		data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6718 					cred);
6719 		if (data->lr.roc) {
6720 			data->args.lr_args = &data->lr.arg;
6721 			data->res.lr_res = &data->lr.res;
6722 		}
6723 	}
6724 
6725 	if (!data->inode)
6726 		nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6727 				   1);
6728 	else
6729 		nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6730 				   0);
6731 	task_setup_data.callback_data = data;
6732 	msg.rpc_argp = &data->args;
6733 	msg.rpc_resp = &data->res;
6734 	task = rpc_run_task(&task_setup_data);
6735 	if (IS_ERR(task))
6736 		return PTR_ERR(task);
6737 	if (!issync)
6738 		goto out;
6739 	status = rpc_wait_for_completion_task(task);
6740 	if (status != 0)
6741 		goto out;
6742 	status = data->rpc_status;
6743 out:
6744 	rpc_put_task(task);
6745 	return status;
6746 }
6747 
6748 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6749 {
6750 	struct nfs_server *server = NFS_SERVER(inode);
6751 	struct nfs4_exception exception = { };
6752 	int err;
6753 	do {
6754 		err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6755 		trace_nfs4_delegreturn(inode, stateid, err);
6756 		switch (err) {
6757 			case -NFS4ERR_STALE_STATEID:
6758 			case -NFS4ERR_EXPIRED:
6759 			case 0:
6760 				return 0;
6761 		}
6762 		err = nfs4_handle_exception(server, err, &exception);
6763 	} while (exception.retry);
6764 	return err;
6765 }
6766 
6767 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6768 {
6769 	struct inode *inode = state->inode;
6770 	struct nfs_server *server = NFS_SERVER(inode);
6771 	struct nfs_client *clp = server->nfs_client;
6772 	struct nfs_lockt_args arg = {
6773 		.fh = NFS_FH(inode),
6774 		.fl = request,
6775 	};
6776 	struct nfs_lockt_res res = {
6777 		.denied = request,
6778 	};
6779 	struct rpc_message msg = {
6780 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6781 		.rpc_argp	= &arg,
6782 		.rpc_resp	= &res,
6783 		.rpc_cred	= state->owner->so_cred,
6784 	};
6785 	struct nfs4_lock_state *lsp;
6786 	int status;
6787 
6788 	arg.lock_owner.clientid = clp->cl_clientid;
6789 	status = nfs4_set_lock_state(state, request);
6790 	if (status != 0)
6791 		goto out;
6792 	lsp = request->fl_u.nfs4_fl.owner;
6793 	arg.lock_owner.id = lsp->ls_seqid.owner_id;
6794 	arg.lock_owner.s_dev = server->s_dev;
6795 	status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6796 	switch (status) {
6797 		case 0:
6798 			request->fl_type = F_UNLCK;
6799 			break;
6800 		case -NFS4ERR_DENIED:
6801 			status = 0;
6802 	}
6803 	request->fl_ops->fl_release_private(request);
6804 	request->fl_ops = NULL;
6805 out:
6806 	return status;
6807 }
6808 
6809 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6810 {
6811 	struct nfs4_exception exception = {
6812 		.interruptible = true,
6813 	};
6814 	int err;
6815 
6816 	do {
6817 		err = _nfs4_proc_getlk(state, cmd, request);
6818 		trace_nfs4_get_lock(request, state, cmd, err);
6819 		err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6820 				&exception);
6821 	} while (exception.retry);
6822 	return err;
6823 }
6824 
6825 /*
6826  * Update the seqid of a lock stateid after receiving
6827  * NFS4ERR_OLD_STATEID
6828  */
6829 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6830 		struct nfs4_lock_state *lsp)
6831 {
6832 	struct nfs4_state *state = lsp->ls_state;
6833 	bool ret = false;
6834 
6835 	spin_lock(&state->state_lock);
6836 	if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6837 		goto out;
6838 	if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6839 		nfs4_stateid_seqid_inc(dst);
6840 	else
6841 		dst->seqid = lsp->ls_stateid.seqid;
6842 	ret = true;
6843 out:
6844 	spin_unlock(&state->state_lock);
6845 	return ret;
6846 }
6847 
6848 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6849 		struct nfs4_lock_state *lsp)
6850 {
6851 	struct nfs4_state *state = lsp->ls_state;
6852 	bool ret;
6853 
6854 	spin_lock(&state->state_lock);
6855 	ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6856 	nfs4_stateid_copy(dst, &lsp->ls_stateid);
6857 	spin_unlock(&state->state_lock);
6858 	return ret;
6859 }
6860 
6861 struct nfs4_unlockdata {
6862 	struct nfs_locku_args arg;
6863 	struct nfs_locku_res res;
6864 	struct nfs4_lock_state *lsp;
6865 	struct nfs_open_context *ctx;
6866 	struct nfs_lock_context *l_ctx;
6867 	struct file_lock fl;
6868 	struct nfs_server *server;
6869 	unsigned long timestamp;
6870 };
6871 
6872 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6873 		struct nfs_open_context *ctx,
6874 		struct nfs4_lock_state *lsp,
6875 		struct nfs_seqid *seqid)
6876 {
6877 	struct nfs4_unlockdata *p;
6878 	struct nfs4_state *state = lsp->ls_state;
6879 	struct inode *inode = state->inode;
6880 
6881 	p = kzalloc(sizeof(*p), GFP_KERNEL);
6882 	if (p == NULL)
6883 		return NULL;
6884 	p->arg.fh = NFS_FH(inode);
6885 	p->arg.fl = &p->fl;
6886 	p->arg.seqid = seqid;
6887 	p->res.seqid = seqid;
6888 	p->lsp = lsp;
6889 	/* Ensure we don't close file until we're done freeing locks! */
6890 	p->ctx = get_nfs_open_context(ctx);
6891 	p->l_ctx = nfs_get_lock_context(ctx);
6892 	locks_init_lock(&p->fl);
6893 	locks_copy_lock(&p->fl, fl);
6894 	p->server = NFS_SERVER(inode);
6895 	spin_lock(&state->state_lock);
6896 	nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6897 	spin_unlock(&state->state_lock);
6898 	return p;
6899 }
6900 
6901 static void nfs4_locku_release_calldata(void *data)
6902 {
6903 	struct nfs4_unlockdata *calldata = data;
6904 	nfs_free_seqid(calldata->arg.seqid);
6905 	nfs4_put_lock_state(calldata->lsp);
6906 	nfs_put_lock_context(calldata->l_ctx);
6907 	put_nfs_open_context(calldata->ctx);
6908 	kfree(calldata);
6909 }
6910 
6911 static void nfs4_locku_done(struct rpc_task *task, void *data)
6912 {
6913 	struct nfs4_unlockdata *calldata = data;
6914 	struct nfs4_exception exception = {
6915 		.inode = calldata->lsp->ls_state->inode,
6916 		.stateid = &calldata->arg.stateid,
6917 	};
6918 
6919 	if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6920 		return;
6921 	switch (task->tk_status) {
6922 		case 0:
6923 			renew_lease(calldata->server, calldata->timestamp);
6924 			locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6925 			if (nfs4_update_lock_stateid(calldata->lsp,
6926 					&calldata->res.stateid))
6927 				break;
6928 			fallthrough;
6929 		case -NFS4ERR_ADMIN_REVOKED:
6930 		case -NFS4ERR_EXPIRED:
6931 			nfs4_free_revoked_stateid(calldata->server,
6932 					&calldata->arg.stateid,
6933 					task->tk_msg.rpc_cred);
6934 			fallthrough;
6935 		case -NFS4ERR_BAD_STATEID:
6936 		case -NFS4ERR_STALE_STATEID:
6937 			if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6938 						calldata->lsp))
6939 				rpc_restart_call_prepare(task);
6940 			break;
6941 		case -NFS4ERR_OLD_STATEID:
6942 			if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6943 						calldata->lsp))
6944 				rpc_restart_call_prepare(task);
6945 			break;
6946 		default:
6947 			task->tk_status = nfs4_async_handle_exception(task,
6948 					calldata->server, task->tk_status,
6949 					&exception);
6950 			if (exception.retry)
6951 				rpc_restart_call_prepare(task);
6952 	}
6953 	nfs_release_seqid(calldata->arg.seqid);
6954 }
6955 
6956 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6957 {
6958 	struct nfs4_unlockdata *calldata = data;
6959 
6960 	if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6961 		nfs_async_iocounter_wait(task, calldata->l_ctx))
6962 		return;
6963 
6964 	if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6965 		goto out_wait;
6966 	if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6967 		/* Note: exit _without_ running nfs4_locku_done */
6968 		goto out_no_action;
6969 	}
6970 	calldata->timestamp = jiffies;
6971 	if (nfs4_setup_sequence(calldata->server->nfs_client,
6972 				&calldata->arg.seq_args,
6973 				&calldata->res.seq_res,
6974 				task) != 0)
6975 		nfs_release_seqid(calldata->arg.seqid);
6976 	return;
6977 out_no_action:
6978 	task->tk_action = NULL;
6979 out_wait:
6980 	nfs4_sequence_done(task, &calldata->res.seq_res);
6981 }
6982 
6983 static const struct rpc_call_ops nfs4_locku_ops = {
6984 	.rpc_call_prepare = nfs4_locku_prepare,
6985 	.rpc_call_done = nfs4_locku_done,
6986 	.rpc_release = nfs4_locku_release_calldata,
6987 };
6988 
6989 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6990 		struct nfs_open_context *ctx,
6991 		struct nfs4_lock_state *lsp,
6992 		struct nfs_seqid *seqid)
6993 {
6994 	struct nfs4_unlockdata *data;
6995 	struct rpc_message msg = {
6996 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6997 		.rpc_cred = ctx->cred,
6998 	};
6999 	struct rpc_task_setup task_setup_data = {
7000 		.rpc_client = NFS_CLIENT(lsp->ls_state->inode),
7001 		.rpc_message = &msg,
7002 		.callback_ops = &nfs4_locku_ops,
7003 		.workqueue = nfsiod_workqueue,
7004 		.flags = RPC_TASK_ASYNC,
7005 	};
7006 
7007 	if (nfs_server_capable(lsp->ls_state->inode, NFS_CAP_MOVEABLE))
7008 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
7009 
7010 	nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
7011 		NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
7012 
7013 	/* Ensure this is an unlock - when canceling a lock, the
7014 	 * canceled lock is passed in, and it won't be an unlock.
7015 	 */
7016 	fl->fl_type = F_UNLCK;
7017 	if (fl->fl_flags & FL_CLOSE)
7018 		set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
7019 
7020 	data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
7021 	if (data == NULL) {
7022 		nfs_free_seqid(seqid);
7023 		return ERR_PTR(-ENOMEM);
7024 	}
7025 
7026 	nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
7027 	msg.rpc_argp = &data->arg;
7028 	msg.rpc_resp = &data->res;
7029 	task_setup_data.callback_data = data;
7030 	return rpc_run_task(&task_setup_data);
7031 }
7032 
7033 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
7034 {
7035 	struct inode *inode = state->inode;
7036 	struct nfs4_state_owner *sp = state->owner;
7037 	struct nfs_inode *nfsi = NFS_I(inode);
7038 	struct nfs_seqid *seqid;
7039 	struct nfs4_lock_state *lsp;
7040 	struct rpc_task *task;
7041 	struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7042 	int status = 0;
7043 	unsigned char fl_flags = request->fl_flags;
7044 
7045 	status = nfs4_set_lock_state(state, request);
7046 	/* Unlock _before_ we do the RPC call */
7047 	request->fl_flags |= FL_EXISTS;
7048 	/* Exclude nfs_delegation_claim_locks() */
7049 	mutex_lock(&sp->so_delegreturn_mutex);
7050 	/* Exclude nfs4_reclaim_open_stateid() - note nesting! */
7051 	down_read(&nfsi->rwsem);
7052 	if (locks_lock_inode_wait(inode, request) == -ENOENT) {
7053 		up_read(&nfsi->rwsem);
7054 		mutex_unlock(&sp->so_delegreturn_mutex);
7055 		goto out;
7056 	}
7057 	lsp = request->fl_u.nfs4_fl.owner;
7058 	set_bit(NFS_LOCK_UNLOCKING, &lsp->ls_flags);
7059 	up_read(&nfsi->rwsem);
7060 	mutex_unlock(&sp->so_delegreturn_mutex);
7061 	if (status != 0)
7062 		goto out;
7063 	/* Is this a delegated lock? */
7064 	if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
7065 		goto out;
7066 	alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
7067 	seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
7068 	status = -ENOMEM;
7069 	if (IS_ERR(seqid))
7070 		goto out;
7071 	task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
7072 	status = PTR_ERR(task);
7073 	if (IS_ERR(task))
7074 		goto out;
7075 	status = rpc_wait_for_completion_task(task);
7076 	rpc_put_task(task);
7077 out:
7078 	request->fl_flags = fl_flags;
7079 	trace_nfs4_unlock(request, state, F_SETLK, status);
7080 	return status;
7081 }
7082 
7083 struct nfs4_lockdata {
7084 	struct nfs_lock_args arg;
7085 	struct nfs_lock_res res;
7086 	struct nfs4_lock_state *lsp;
7087 	struct nfs_open_context *ctx;
7088 	struct file_lock fl;
7089 	unsigned long timestamp;
7090 	int rpc_status;
7091 	int cancelled;
7092 	struct nfs_server *server;
7093 };
7094 
7095 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
7096 		struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
7097 		gfp_t gfp_mask)
7098 {
7099 	struct nfs4_lockdata *p;
7100 	struct inode *inode = lsp->ls_state->inode;
7101 	struct nfs_server *server = NFS_SERVER(inode);
7102 	struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7103 
7104 	p = kzalloc(sizeof(*p), gfp_mask);
7105 	if (p == NULL)
7106 		return NULL;
7107 
7108 	p->arg.fh = NFS_FH(inode);
7109 	p->arg.fl = &p->fl;
7110 	p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
7111 	if (IS_ERR(p->arg.open_seqid))
7112 		goto out_free;
7113 	alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
7114 	p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
7115 	if (IS_ERR(p->arg.lock_seqid))
7116 		goto out_free_seqid;
7117 	p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
7118 	p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
7119 	p->arg.lock_owner.s_dev = server->s_dev;
7120 	p->res.lock_seqid = p->arg.lock_seqid;
7121 	p->lsp = lsp;
7122 	p->server = server;
7123 	p->ctx = get_nfs_open_context(ctx);
7124 	locks_init_lock(&p->fl);
7125 	locks_copy_lock(&p->fl, fl);
7126 	return p;
7127 out_free_seqid:
7128 	nfs_free_seqid(p->arg.open_seqid);
7129 out_free:
7130 	kfree(p);
7131 	return NULL;
7132 }
7133 
7134 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
7135 {
7136 	struct nfs4_lockdata *data = calldata;
7137 	struct nfs4_state *state = data->lsp->ls_state;
7138 
7139 	if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
7140 		goto out_wait;
7141 	/* Do we need to do an open_to_lock_owner? */
7142 	if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
7143 		if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
7144 			goto out_release_lock_seqid;
7145 		}
7146 		nfs4_stateid_copy(&data->arg.open_stateid,
7147 				&state->open_stateid);
7148 		data->arg.new_lock_owner = 1;
7149 		data->res.open_seqid = data->arg.open_seqid;
7150 	} else {
7151 		data->arg.new_lock_owner = 0;
7152 		nfs4_stateid_copy(&data->arg.lock_stateid,
7153 				&data->lsp->ls_stateid);
7154 	}
7155 	if (!nfs4_valid_open_stateid(state)) {
7156 		data->rpc_status = -EBADF;
7157 		task->tk_action = NULL;
7158 		goto out_release_open_seqid;
7159 	}
7160 	data->timestamp = jiffies;
7161 	if (nfs4_setup_sequence(data->server->nfs_client,
7162 				&data->arg.seq_args,
7163 				&data->res.seq_res,
7164 				task) == 0)
7165 		return;
7166 out_release_open_seqid:
7167 	nfs_release_seqid(data->arg.open_seqid);
7168 out_release_lock_seqid:
7169 	nfs_release_seqid(data->arg.lock_seqid);
7170 out_wait:
7171 	nfs4_sequence_done(task, &data->res.seq_res);
7172 	dprintk("%s: ret = %d\n", __func__, data->rpc_status);
7173 }
7174 
7175 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7176 {
7177 	struct nfs4_lockdata *data = calldata;
7178 	struct nfs4_lock_state *lsp = data->lsp;
7179 
7180 	if (!nfs4_sequence_done(task, &data->res.seq_res))
7181 		return;
7182 
7183 	data->rpc_status = task->tk_status;
7184 	switch (task->tk_status) {
7185 	case 0:
7186 		renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
7187 				data->timestamp);
7188 		if (data->arg.new_lock && !data->cancelled) {
7189 			data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
7190 			if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
7191 				goto out_restart;
7192 		}
7193 		if (data->arg.new_lock_owner != 0) {
7194 			nfs_confirm_seqid(&lsp->ls_seqid, 0);
7195 			nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7196 			set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
7197 		} else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7198 			goto out_restart;
7199 		break;
7200 	case -NFS4ERR_OLD_STATEID:
7201 		if (data->arg.new_lock_owner != 0 &&
7202 			nfs4_refresh_open_old_stateid(&data->arg.open_stateid,
7203 					lsp->ls_state))
7204 			goto out_restart;
7205 		if (nfs4_refresh_lock_old_stateid(&data->arg.lock_stateid, lsp))
7206 			goto out_restart;
7207 		fallthrough;
7208 	case -NFS4ERR_BAD_STATEID:
7209 	case -NFS4ERR_STALE_STATEID:
7210 	case -NFS4ERR_EXPIRED:
7211 		if (data->arg.new_lock_owner != 0) {
7212 			if (!nfs4_stateid_match(&data->arg.open_stateid,
7213 						&lsp->ls_state->open_stateid))
7214 				goto out_restart;
7215 		} else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7216 						&lsp->ls_stateid))
7217 				goto out_restart;
7218 	}
7219 out_done:
7220 	dprintk("%s: ret = %d!\n", __func__, data->rpc_status);
7221 	return;
7222 out_restart:
7223 	if (!data->cancelled)
7224 		rpc_restart_call_prepare(task);
7225 	goto out_done;
7226 }
7227 
7228 static void nfs4_lock_release(void *calldata)
7229 {
7230 	struct nfs4_lockdata *data = calldata;
7231 
7232 	nfs_free_seqid(data->arg.open_seqid);
7233 	if (data->cancelled && data->rpc_status == 0) {
7234 		struct rpc_task *task;
7235 		task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7236 				data->arg.lock_seqid);
7237 		if (!IS_ERR(task))
7238 			rpc_put_task_async(task);
7239 		dprintk("%s: cancelling lock!\n", __func__);
7240 	} else
7241 		nfs_free_seqid(data->arg.lock_seqid);
7242 	nfs4_put_lock_state(data->lsp);
7243 	put_nfs_open_context(data->ctx);
7244 	kfree(data);
7245 }
7246 
7247 static const struct rpc_call_ops nfs4_lock_ops = {
7248 	.rpc_call_prepare = nfs4_lock_prepare,
7249 	.rpc_call_done = nfs4_lock_done,
7250 	.rpc_release = nfs4_lock_release,
7251 };
7252 
7253 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7254 {
7255 	switch (error) {
7256 	case -NFS4ERR_ADMIN_REVOKED:
7257 	case -NFS4ERR_EXPIRED:
7258 	case -NFS4ERR_BAD_STATEID:
7259 		lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7260 		if (new_lock_owner != 0 ||
7261 		   test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7262 			nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7263 		break;
7264 	case -NFS4ERR_STALE_STATEID:
7265 		lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7266 		nfs4_schedule_lease_recovery(server->nfs_client);
7267 	}
7268 }
7269 
7270 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7271 {
7272 	struct nfs4_lockdata *data;
7273 	struct rpc_task *task;
7274 	struct rpc_message msg = {
7275 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7276 		.rpc_cred = state->owner->so_cred,
7277 	};
7278 	struct rpc_task_setup task_setup_data = {
7279 		.rpc_client = NFS_CLIENT(state->inode),
7280 		.rpc_message = &msg,
7281 		.callback_ops = &nfs4_lock_ops,
7282 		.workqueue = nfsiod_workqueue,
7283 		.flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7284 	};
7285 	int ret;
7286 
7287 	if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
7288 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
7289 
7290 	data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
7291 				   fl->fl_u.nfs4_fl.owner, GFP_KERNEL);
7292 	if (data == NULL)
7293 		return -ENOMEM;
7294 	if (IS_SETLKW(cmd))
7295 		data->arg.block = 1;
7296 	nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7297 				recovery_type > NFS_LOCK_NEW);
7298 	msg.rpc_argp = &data->arg;
7299 	msg.rpc_resp = &data->res;
7300 	task_setup_data.callback_data = data;
7301 	if (recovery_type > NFS_LOCK_NEW) {
7302 		if (recovery_type == NFS_LOCK_RECLAIM)
7303 			data->arg.reclaim = NFS_LOCK_RECLAIM;
7304 	} else
7305 		data->arg.new_lock = 1;
7306 	task = rpc_run_task(&task_setup_data);
7307 	if (IS_ERR(task))
7308 		return PTR_ERR(task);
7309 	ret = rpc_wait_for_completion_task(task);
7310 	if (ret == 0) {
7311 		ret = data->rpc_status;
7312 		if (ret)
7313 			nfs4_handle_setlk_error(data->server, data->lsp,
7314 					data->arg.new_lock_owner, ret);
7315 	} else
7316 		data->cancelled = true;
7317 	trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7318 	rpc_put_task(task);
7319 	dprintk("%s: ret = %d\n", __func__, ret);
7320 	return ret;
7321 }
7322 
7323 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7324 {
7325 	struct nfs_server *server = NFS_SERVER(state->inode);
7326 	struct nfs4_exception exception = {
7327 		.inode = state->inode,
7328 	};
7329 	int err;
7330 
7331 	do {
7332 		/* Cache the lock if possible... */
7333 		if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7334 			return 0;
7335 		err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7336 		if (err != -NFS4ERR_DELAY)
7337 			break;
7338 		nfs4_handle_exception(server, err, &exception);
7339 	} while (exception.retry);
7340 	return err;
7341 }
7342 
7343 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7344 {
7345 	struct nfs_server *server = NFS_SERVER(state->inode);
7346 	struct nfs4_exception exception = {
7347 		.inode = state->inode,
7348 	};
7349 	int err;
7350 
7351 	err = nfs4_set_lock_state(state, request);
7352 	if (err != 0)
7353 		return err;
7354 	if (!recover_lost_locks) {
7355 		set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7356 		return 0;
7357 	}
7358 	do {
7359 		if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7360 			return 0;
7361 		err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7362 		switch (err) {
7363 		default:
7364 			goto out;
7365 		case -NFS4ERR_GRACE:
7366 		case -NFS4ERR_DELAY:
7367 			nfs4_handle_exception(server, err, &exception);
7368 			err = 0;
7369 		}
7370 	} while (exception.retry);
7371 out:
7372 	return err;
7373 }
7374 
7375 #if defined(CONFIG_NFS_V4_1)
7376 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7377 {
7378 	struct nfs4_lock_state *lsp;
7379 	int status;
7380 
7381 	status = nfs4_set_lock_state(state, request);
7382 	if (status != 0)
7383 		return status;
7384 	lsp = request->fl_u.nfs4_fl.owner;
7385 	if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7386 	    test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7387 		return 0;
7388 	return nfs4_lock_expired(state, request);
7389 }
7390 #endif
7391 
7392 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7393 {
7394 	struct nfs_inode *nfsi = NFS_I(state->inode);
7395 	struct nfs4_state_owner *sp = state->owner;
7396 	unsigned char fl_flags = request->fl_flags;
7397 	int status;
7398 
7399 	request->fl_flags |= FL_ACCESS;
7400 	status = locks_lock_inode_wait(state->inode, request);
7401 	if (status < 0)
7402 		goto out;
7403 	mutex_lock(&sp->so_delegreturn_mutex);
7404 	down_read(&nfsi->rwsem);
7405 	if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7406 		/* Yes: cache locks! */
7407 		/* ...but avoid races with delegation recall... */
7408 		request->fl_flags = fl_flags & ~FL_SLEEP;
7409 		status = locks_lock_inode_wait(state->inode, request);
7410 		up_read(&nfsi->rwsem);
7411 		mutex_unlock(&sp->so_delegreturn_mutex);
7412 		goto out;
7413 	}
7414 	up_read(&nfsi->rwsem);
7415 	mutex_unlock(&sp->so_delegreturn_mutex);
7416 	status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7417 out:
7418 	request->fl_flags = fl_flags;
7419 	return status;
7420 }
7421 
7422 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7423 {
7424 	struct nfs4_exception exception = {
7425 		.state = state,
7426 		.inode = state->inode,
7427 		.interruptible = true,
7428 	};
7429 	int err;
7430 
7431 	do {
7432 		err = _nfs4_proc_setlk(state, cmd, request);
7433 		if (err == -NFS4ERR_DENIED)
7434 			err = -EAGAIN;
7435 		err = nfs4_handle_exception(NFS_SERVER(state->inode),
7436 				err, &exception);
7437 	} while (exception.retry);
7438 	return err;
7439 }
7440 
7441 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7442 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7443 
7444 static int
7445 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7446 			struct file_lock *request)
7447 {
7448 	int		status = -ERESTARTSYS;
7449 	unsigned long	timeout = NFS4_LOCK_MINTIMEOUT;
7450 
7451 	while(!signalled()) {
7452 		status = nfs4_proc_setlk(state, cmd, request);
7453 		if ((status != -EAGAIN) || IS_SETLK(cmd))
7454 			break;
7455 		__set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
7456 		schedule_timeout(timeout);
7457 		timeout *= 2;
7458 		timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7459 		status = -ERESTARTSYS;
7460 	}
7461 	return status;
7462 }
7463 
7464 #ifdef CONFIG_NFS_V4_1
7465 struct nfs4_lock_waiter {
7466 	struct inode		*inode;
7467 	struct nfs_lowner	owner;
7468 	wait_queue_entry_t	wait;
7469 };
7470 
7471 static int
7472 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7473 {
7474 	struct nfs4_lock_waiter	*waiter	=
7475 		container_of(wait, struct nfs4_lock_waiter, wait);
7476 
7477 	/* NULL key means to wake up everyone */
7478 	if (key) {
7479 		struct cb_notify_lock_args	*cbnl = key;
7480 		struct nfs_lowner		*lowner = &cbnl->cbnl_owner,
7481 						*wowner = &waiter->owner;
7482 
7483 		/* Only wake if the callback was for the same owner. */
7484 		if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7485 			return 0;
7486 
7487 		/* Make sure it's for the right inode */
7488 		if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7489 			return 0;
7490 	}
7491 
7492 	return woken_wake_function(wait, mode, flags, key);
7493 }
7494 
7495 static int
7496 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7497 {
7498 	struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7499 	struct nfs_server *server = NFS_SERVER(state->inode);
7500 	struct nfs_client *clp = server->nfs_client;
7501 	wait_queue_head_t *q = &clp->cl_lock_waitq;
7502 	struct nfs4_lock_waiter waiter = {
7503 		.inode = state->inode,
7504 		.owner = { .clientid = clp->cl_clientid,
7505 			   .id = lsp->ls_seqid.owner_id,
7506 			   .s_dev = server->s_dev },
7507 	};
7508 	int status;
7509 
7510 	/* Don't bother with waitqueue if we don't expect a callback */
7511 	if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7512 		return nfs4_retry_setlk_simple(state, cmd, request);
7513 
7514 	init_wait(&waiter.wait);
7515 	waiter.wait.func = nfs4_wake_lock_waiter;
7516 	add_wait_queue(q, &waiter.wait);
7517 
7518 	do {
7519 		status = nfs4_proc_setlk(state, cmd, request);
7520 		if (status != -EAGAIN || IS_SETLK(cmd))
7521 			break;
7522 
7523 		status = -ERESTARTSYS;
7524 		wait_woken(&waiter.wait, TASK_INTERRUPTIBLE|TASK_FREEZABLE,
7525 			   NFS4_LOCK_MAXTIMEOUT);
7526 	} while (!signalled());
7527 
7528 	remove_wait_queue(q, &waiter.wait);
7529 
7530 	return status;
7531 }
7532 #else /* !CONFIG_NFS_V4_1 */
7533 static inline int
7534 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7535 {
7536 	return nfs4_retry_setlk_simple(state, cmd, request);
7537 }
7538 #endif
7539 
7540 static int
7541 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7542 {
7543 	struct nfs_open_context *ctx;
7544 	struct nfs4_state *state;
7545 	int status;
7546 
7547 	/* verify open state */
7548 	ctx = nfs_file_open_context(filp);
7549 	state = ctx->state;
7550 
7551 	if (IS_GETLK(cmd)) {
7552 		if (state != NULL)
7553 			return nfs4_proc_getlk(state, F_GETLK, request);
7554 		return 0;
7555 	}
7556 
7557 	if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7558 		return -EINVAL;
7559 
7560 	if (request->fl_type == F_UNLCK) {
7561 		if (state != NULL)
7562 			return nfs4_proc_unlck(state, cmd, request);
7563 		return 0;
7564 	}
7565 
7566 	if (state == NULL)
7567 		return -ENOLCK;
7568 
7569 	if ((request->fl_flags & FL_POSIX) &&
7570 	    !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7571 		return -ENOLCK;
7572 
7573 	/*
7574 	 * Don't rely on the VFS having checked the file open mode,
7575 	 * since it won't do this for flock() locks.
7576 	 */
7577 	switch (request->fl_type) {
7578 	case F_RDLCK:
7579 		if (!(filp->f_mode & FMODE_READ))
7580 			return -EBADF;
7581 		break;
7582 	case F_WRLCK:
7583 		if (!(filp->f_mode & FMODE_WRITE))
7584 			return -EBADF;
7585 	}
7586 
7587 	status = nfs4_set_lock_state(state, request);
7588 	if (status != 0)
7589 		return status;
7590 
7591 	return nfs4_retry_setlk(state, cmd, request);
7592 }
7593 
7594 static int nfs4_delete_lease(struct file *file, void **priv)
7595 {
7596 	return generic_setlease(file, F_UNLCK, NULL, priv);
7597 }
7598 
7599 static int nfs4_add_lease(struct file *file, int arg, struct file_lock **lease,
7600 			  void **priv)
7601 {
7602 	struct inode *inode = file_inode(file);
7603 	fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE;
7604 	int ret;
7605 
7606 	/* No delegation, no lease */
7607 	if (!nfs4_have_delegation(inode, type))
7608 		return -EAGAIN;
7609 	ret = generic_setlease(file, arg, lease, priv);
7610 	if (ret || nfs4_have_delegation(inode, type))
7611 		return ret;
7612 	/* We raced with a delegation return */
7613 	nfs4_delete_lease(file, priv);
7614 	return -EAGAIN;
7615 }
7616 
7617 int nfs4_proc_setlease(struct file *file, int arg, struct file_lock **lease,
7618 		       void **priv)
7619 {
7620 	switch (arg) {
7621 	case F_RDLCK:
7622 	case F_WRLCK:
7623 		return nfs4_add_lease(file, arg, lease, priv);
7624 	case F_UNLCK:
7625 		return nfs4_delete_lease(file, priv);
7626 	default:
7627 		return -EINVAL;
7628 	}
7629 }
7630 
7631 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7632 {
7633 	struct nfs_server *server = NFS_SERVER(state->inode);
7634 	int err;
7635 
7636 	err = nfs4_set_lock_state(state, fl);
7637 	if (err != 0)
7638 		return err;
7639 	do {
7640 		err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7641 		if (err != -NFS4ERR_DELAY)
7642 			break;
7643 		ssleep(1);
7644 	} while (err == -NFS4ERR_DELAY);
7645 	return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7646 }
7647 
7648 struct nfs_release_lockowner_data {
7649 	struct nfs4_lock_state *lsp;
7650 	struct nfs_server *server;
7651 	struct nfs_release_lockowner_args args;
7652 	struct nfs_release_lockowner_res res;
7653 	unsigned long timestamp;
7654 };
7655 
7656 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7657 {
7658 	struct nfs_release_lockowner_data *data = calldata;
7659 	struct nfs_server *server = data->server;
7660 	nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7661 			   &data->res.seq_res, task);
7662 	data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7663 	data->timestamp = jiffies;
7664 }
7665 
7666 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7667 {
7668 	struct nfs_release_lockowner_data *data = calldata;
7669 	struct nfs_server *server = data->server;
7670 
7671 	nfs40_sequence_done(task, &data->res.seq_res);
7672 
7673 	switch (task->tk_status) {
7674 	case 0:
7675 		renew_lease(server, data->timestamp);
7676 		break;
7677 	case -NFS4ERR_STALE_CLIENTID:
7678 	case -NFS4ERR_EXPIRED:
7679 		nfs4_schedule_lease_recovery(server->nfs_client);
7680 		break;
7681 	case -NFS4ERR_LEASE_MOVED:
7682 	case -NFS4ERR_DELAY:
7683 		if (nfs4_async_handle_error(task, server,
7684 					    NULL, NULL) == -EAGAIN)
7685 			rpc_restart_call_prepare(task);
7686 	}
7687 }
7688 
7689 static void nfs4_release_lockowner_release(void *calldata)
7690 {
7691 	struct nfs_release_lockowner_data *data = calldata;
7692 	nfs4_free_lock_state(data->server, data->lsp);
7693 	kfree(calldata);
7694 }
7695 
7696 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7697 	.rpc_call_prepare = nfs4_release_lockowner_prepare,
7698 	.rpc_call_done = nfs4_release_lockowner_done,
7699 	.rpc_release = nfs4_release_lockowner_release,
7700 };
7701 
7702 static void
7703 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7704 {
7705 	struct nfs_release_lockowner_data *data;
7706 	struct rpc_message msg = {
7707 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7708 	};
7709 
7710 	if (server->nfs_client->cl_mvops->minor_version != 0)
7711 		return;
7712 
7713 	data = kmalloc(sizeof(*data), GFP_KERNEL);
7714 	if (!data)
7715 		return;
7716 	data->lsp = lsp;
7717 	data->server = server;
7718 	data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7719 	data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7720 	data->args.lock_owner.s_dev = server->s_dev;
7721 
7722 	msg.rpc_argp = &data->args;
7723 	msg.rpc_resp = &data->res;
7724 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7725 	rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7726 }
7727 
7728 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7729 
7730 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7731 				   struct mnt_idmap *idmap,
7732 				   struct dentry *unused, struct inode *inode,
7733 				   const char *key, const void *buf,
7734 				   size_t buflen, int flags)
7735 {
7736 	return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_ACL);
7737 }
7738 
7739 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7740 				   struct dentry *unused, struct inode *inode,
7741 				   const char *key, void *buf, size_t buflen)
7742 {
7743 	return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_ACL);
7744 }
7745 
7746 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7747 {
7748 	return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_ACL);
7749 }
7750 
7751 #if defined(CONFIG_NFS_V4_1)
7752 #define XATTR_NAME_NFSV4_DACL "system.nfs4_dacl"
7753 
7754 static int nfs4_xattr_set_nfs4_dacl(const struct xattr_handler *handler,
7755 				    struct mnt_idmap *idmap,
7756 				    struct dentry *unused, struct inode *inode,
7757 				    const char *key, const void *buf,
7758 				    size_t buflen, int flags)
7759 {
7760 	return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_DACL);
7761 }
7762 
7763 static int nfs4_xattr_get_nfs4_dacl(const struct xattr_handler *handler,
7764 				    struct dentry *unused, struct inode *inode,
7765 				    const char *key, void *buf, size_t buflen)
7766 {
7767 	return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_DACL);
7768 }
7769 
7770 static bool nfs4_xattr_list_nfs4_dacl(struct dentry *dentry)
7771 {
7772 	return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_DACL);
7773 }
7774 
7775 #define XATTR_NAME_NFSV4_SACL "system.nfs4_sacl"
7776 
7777 static int nfs4_xattr_set_nfs4_sacl(const struct xattr_handler *handler,
7778 				    struct mnt_idmap *idmap,
7779 				    struct dentry *unused, struct inode *inode,
7780 				    const char *key, const void *buf,
7781 				    size_t buflen, int flags)
7782 {
7783 	return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_SACL);
7784 }
7785 
7786 static int nfs4_xattr_get_nfs4_sacl(const struct xattr_handler *handler,
7787 				    struct dentry *unused, struct inode *inode,
7788 				    const char *key, void *buf, size_t buflen)
7789 {
7790 	return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_SACL);
7791 }
7792 
7793 static bool nfs4_xattr_list_nfs4_sacl(struct dentry *dentry)
7794 {
7795 	return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_SACL);
7796 }
7797 
7798 #endif
7799 
7800 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7801 
7802 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7803 				     struct mnt_idmap *idmap,
7804 				     struct dentry *unused, struct inode *inode,
7805 				     const char *key, const void *buf,
7806 				     size_t buflen, int flags)
7807 {
7808 	if (security_ismaclabel(key))
7809 		return nfs4_set_security_label(inode, buf, buflen);
7810 
7811 	return -EOPNOTSUPP;
7812 }
7813 
7814 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7815 				     struct dentry *unused, struct inode *inode,
7816 				     const char *key, void *buf, size_t buflen)
7817 {
7818 	if (security_ismaclabel(key))
7819 		return nfs4_get_security_label(inode, buf, buflen);
7820 	return -EOPNOTSUPP;
7821 }
7822 
7823 static ssize_t
7824 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7825 {
7826 	int len = 0;
7827 
7828 	if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7829 		len = security_inode_listsecurity(inode, list, list_len);
7830 		if (len >= 0 && list_len && len > list_len)
7831 			return -ERANGE;
7832 	}
7833 	return len;
7834 }
7835 
7836 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7837 	.prefix = XATTR_SECURITY_PREFIX,
7838 	.get	= nfs4_xattr_get_nfs4_label,
7839 	.set	= nfs4_xattr_set_nfs4_label,
7840 };
7841 
7842 #else
7843 
7844 static ssize_t
7845 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7846 {
7847 	return 0;
7848 }
7849 
7850 #endif
7851 
7852 #ifdef CONFIG_NFS_V4_2
7853 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7854 				    struct mnt_idmap *idmap,
7855 				    struct dentry *unused, struct inode *inode,
7856 				    const char *key, const void *buf,
7857 				    size_t buflen, int flags)
7858 {
7859 	u32 mask;
7860 	int ret;
7861 
7862 	if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7863 		return -EOPNOTSUPP;
7864 
7865 	/*
7866 	 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7867 	 * flags right now. Handling of xattr operations use the normal
7868 	 * file read/write permissions.
7869 	 *
7870 	 * Just in case the server has other ideas (which RFC 8276 allows),
7871 	 * do a cached access check for the XA* flags to possibly avoid
7872 	 * doing an RPC and getting EACCES back.
7873 	 */
7874 	if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7875 		if (!(mask & NFS_ACCESS_XAWRITE))
7876 			return -EACCES;
7877 	}
7878 
7879 	if (buf == NULL) {
7880 		ret = nfs42_proc_removexattr(inode, key);
7881 		if (!ret)
7882 			nfs4_xattr_cache_remove(inode, key);
7883 	} else {
7884 		ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7885 		if (!ret)
7886 			nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7887 	}
7888 
7889 	return ret;
7890 }
7891 
7892 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7893 				    struct dentry *unused, struct inode *inode,
7894 				    const char *key, void *buf, size_t buflen)
7895 {
7896 	u32 mask;
7897 	ssize_t ret;
7898 
7899 	if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7900 		return -EOPNOTSUPP;
7901 
7902 	if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7903 		if (!(mask & NFS_ACCESS_XAREAD))
7904 			return -EACCES;
7905 	}
7906 
7907 	ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7908 	if (ret)
7909 		return ret;
7910 
7911 	ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7912 	if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7913 		return ret;
7914 
7915 	ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7916 
7917 	return ret;
7918 }
7919 
7920 static ssize_t
7921 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7922 {
7923 	u64 cookie;
7924 	bool eof;
7925 	ssize_t ret, size;
7926 	char *buf;
7927 	size_t buflen;
7928 	u32 mask;
7929 
7930 	if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7931 		return 0;
7932 
7933 	if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7934 		if (!(mask & NFS_ACCESS_XALIST))
7935 			return 0;
7936 	}
7937 
7938 	ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7939 	if (ret)
7940 		return ret;
7941 
7942 	ret = nfs4_xattr_cache_list(inode, list, list_len);
7943 	if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7944 		return ret;
7945 
7946 	cookie = 0;
7947 	eof = false;
7948 	buflen = list_len ? list_len : XATTR_LIST_MAX;
7949 	buf = list_len ? list : NULL;
7950 	size = 0;
7951 
7952 	while (!eof) {
7953 		ret = nfs42_proc_listxattrs(inode, buf, buflen,
7954 		    &cookie, &eof);
7955 		if (ret < 0)
7956 			return ret;
7957 
7958 		if (list_len) {
7959 			buf += ret;
7960 			buflen -= ret;
7961 		}
7962 		size += ret;
7963 	}
7964 
7965 	if (list_len)
7966 		nfs4_xattr_cache_set_list(inode, list, size);
7967 
7968 	return size;
7969 }
7970 
7971 #else
7972 
7973 static ssize_t
7974 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7975 {
7976 	return 0;
7977 }
7978 #endif /* CONFIG_NFS_V4_2 */
7979 
7980 /*
7981  * nfs_fhget will use either the mounted_on_fileid or the fileid
7982  */
7983 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7984 {
7985 	if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7986 	       (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7987 	      (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7988 	      (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7989 		return;
7990 
7991 	fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7992 		NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7993 	fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7994 	fattr->nlink = 2;
7995 }
7996 
7997 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7998 				   const struct qstr *name,
7999 				   struct nfs4_fs_locations *fs_locations,
8000 				   struct page *page)
8001 {
8002 	struct nfs_server *server = NFS_SERVER(dir);
8003 	u32 bitmask[3];
8004 	struct nfs4_fs_locations_arg args = {
8005 		.dir_fh = NFS_FH(dir),
8006 		.name = name,
8007 		.page = page,
8008 		.bitmask = bitmask,
8009 	};
8010 	struct nfs4_fs_locations_res res = {
8011 		.fs_locations = fs_locations,
8012 	};
8013 	struct rpc_message msg = {
8014 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8015 		.rpc_argp = &args,
8016 		.rpc_resp = &res,
8017 	};
8018 	int status;
8019 
8020 	dprintk("%s: start\n", __func__);
8021 
8022 	bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
8023 	bitmask[1] = nfs4_fattr_bitmap[1];
8024 
8025 	/* Ask for the fileid of the absent filesystem if mounted_on_fileid
8026 	 * is not supported */
8027 	if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
8028 		bitmask[0] &= ~FATTR4_WORD0_FILEID;
8029 	else
8030 		bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
8031 
8032 	nfs_fattr_init(fs_locations->fattr);
8033 	fs_locations->server = server;
8034 	fs_locations->nlocations = 0;
8035 	status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
8036 	dprintk("%s: returned status = %d\n", __func__, status);
8037 	return status;
8038 }
8039 
8040 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
8041 			   const struct qstr *name,
8042 			   struct nfs4_fs_locations *fs_locations,
8043 			   struct page *page)
8044 {
8045 	struct nfs4_exception exception = {
8046 		.interruptible = true,
8047 	};
8048 	int err;
8049 	do {
8050 		err = _nfs4_proc_fs_locations(client, dir, name,
8051 				fs_locations, page);
8052 		trace_nfs4_get_fs_locations(dir, name, err);
8053 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
8054 				&exception);
8055 	} while (exception.retry);
8056 	return err;
8057 }
8058 
8059 /*
8060  * This operation also signals the server that this client is
8061  * performing migration recovery.  The server can stop returning
8062  * NFS4ERR_LEASE_MOVED to this client.  A RENEW operation is
8063  * appended to this compound to identify the client ID which is
8064  * performing recovery.
8065  */
8066 static int _nfs40_proc_get_locations(struct nfs_server *server,
8067 				     struct nfs_fh *fhandle,
8068 				     struct nfs4_fs_locations *locations,
8069 				     struct page *page, const struct cred *cred)
8070 {
8071 	struct rpc_clnt *clnt = server->client;
8072 	u32 bitmask[2] = {
8073 		[0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8074 	};
8075 	struct nfs4_fs_locations_arg args = {
8076 		.clientid	= server->nfs_client->cl_clientid,
8077 		.fh		= fhandle,
8078 		.page		= page,
8079 		.bitmask	= bitmask,
8080 		.migration	= 1,		/* skip LOOKUP */
8081 		.renew		= 1,		/* append RENEW */
8082 	};
8083 	struct nfs4_fs_locations_res res = {
8084 		.fs_locations	= locations,
8085 		.migration	= 1,
8086 		.renew		= 1,
8087 	};
8088 	struct rpc_message msg = {
8089 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8090 		.rpc_argp	= &args,
8091 		.rpc_resp	= &res,
8092 		.rpc_cred	= cred,
8093 	};
8094 	unsigned long now = jiffies;
8095 	int status;
8096 
8097 	nfs_fattr_init(locations->fattr);
8098 	locations->server = server;
8099 	locations->nlocations = 0;
8100 
8101 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8102 	status = nfs4_call_sync_sequence(clnt, server, &msg,
8103 					&args.seq_args, &res.seq_res);
8104 	if (status)
8105 		return status;
8106 
8107 	renew_lease(server, now);
8108 	return 0;
8109 }
8110 
8111 #ifdef CONFIG_NFS_V4_1
8112 
8113 /*
8114  * This operation also signals the server that this client is
8115  * performing migration recovery.  The server can stop asserting
8116  * SEQ4_STATUS_LEASE_MOVED for this client.  The client ID
8117  * performing this operation is identified in the SEQUENCE
8118  * operation in this compound.
8119  *
8120  * When the client supports GETATTR(fs_locations_info), it can
8121  * be plumbed in here.
8122  */
8123 static int _nfs41_proc_get_locations(struct nfs_server *server,
8124 				     struct nfs_fh *fhandle,
8125 				     struct nfs4_fs_locations *locations,
8126 				     struct page *page, const struct cred *cred)
8127 {
8128 	struct rpc_clnt *clnt = server->client;
8129 	u32 bitmask[2] = {
8130 		[0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8131 	};
8132 	struct nfs4_fs_locations_arg args = {
8133 		.fh		= fhandle,
8134 		.page		= page,
8135 		.bitmask	= bitmask,
8136 		.migration	= 1,		/* skip LOOKUP */
8137 	};
8138 	struct nfs4_fs_locations_res res = {
8139 		.fs_locations	= locations,
8140 		.migration	= 1,
8141 	};
8142 	struct rpc_message msg = {
8143 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8144 		.rpc_argp	= &args,
8145 		.rpc_resp	= &res,
8146 		.rpc_cred	= cred,
8147 	};
8148 	struct nfs4_call_sync_data data = {
8149 		.seq_server = server,
8150 		.seq_args = &args.seq_args,
8151 		.seq_res = &res.seq_res,
8152 	};
8153 	struct rpc_task_setup task_setup_data = {
8154 		.rpc_client = clnt,
8155 		.rpc_message = &msg,
8156 		.callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
8157 		.callback_data = &data,
8158 		.flags = RPC_TASK_NO_ROUND_ROBIN,
8159 	};
8160 	int status;
8161 
8162 	nfs_fattr_init(locations->fattr);
8163 	locations->server = server;
8164 	locations->nlocations = 0;
8165 
8166 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8167 	status = nfs4_call_sync_custom(&task_setup_data);
8168 	if (status == NFS4_OK &&
8169 	    res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8170 		status = -NFS4ERR_LEASE_MOVED;
8171 	return status;
8172 }
8173 
8174 #endif	/* CONFIG_NFS_V4_1 */
8175 
8176 /**
8177  * nfs4_proc_get_locations - discover locations for a migrated FSID
8178  * @server: pointer to nfs_server to process
8179  * @fhandle: pointer to the kernel NFS client file handle
8180  * @locations: result of query
8181  * @page: buffer
8182  * @cred: credential to use for this operation
8183  *
8184  * Returns NFS4_OK on success, a negative NFS4ERR status code if the
8185  * operation failed, or a negative errno if a local error occurred.
8186  *
8187  * On success, "locations" is filled in, but if the server has
8188  * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
8189  * asserted.
8190  *
8191  * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
8192  * from this client that require migration recovery.
8193  */
8194 int nfs4_proc_get_locations(struct nfs_server *server,
8195 			    struct nfs_fh *fhandle,
8196 			    struct nfs4_fs_locations *locations,
8197 			    struct page *page, const struct cred *cred)
8198 {
8199 	struct nfs_client *clp = server->nfs_client;
8200 	const struct nfs4_mig_recovery_ops *ops =
8201 					clp->cl_mvops->mig_recovery_ops;
8202 	struct nfs4_exception exception = {
8203 		.interruptible = true,
8204 	};
8205 	int status;
8206 
8207 	dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8208 		(unsigned long long)server->fsid.major,
8209 		(unsigned long long)server->fsid.minor,
8210 		clp->cl_hostname);
8211 	nfs_display_fhandle(fhandle, __func__);
8212 
8213 	do {
8214 		status = ops->get_locations(server, fhandle, locations, page,
8215 					    cred);
8216 		if (status != -NFS4ERR_DELAY)
8217 			break;
8218 		nfs4_handle_exception(server, status, &exception);
8219 	} while (exception.retry);
8220 	return status;
8221 }
8222 
8223 /*
8224  * This operation also signals the server that this client is
8225  * performing "lease moved" recovery.  The server can stop
8226  * returning NFS4ERR_LEASE_MOVED to this client.  A RENEW operation
8227  * is appended to this compound to identify the client ID which is
8228  * performing recovery.
8229  */
8230 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
8231 {
8232 	struct nfs_server *server = NFS_SERVER(inode);
8233 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
8234 	struct rpc_clnt *clnt = server->client;
8235 	struct nfs4_fsid_present_arg args = {
8236 		.fh		= NFS_FH(inode),
8237 		.clientid	= clp->cl_clientid,
8238 		.renew		= 1,		/* append RENEW */
8239 	};
8240 	struct nfs4_fsid_present_res res = {
8241 		.renew		= 1,
8242 	};
8243 	struct rpc_message msg = {
8244 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8245 		.rpc_argp	= &args,
8246 		.rpc_resp	= &res,
8247 		.rpc_cred	= cred,
8248 	};
8249 	unsigned long now = jiffies;
8250 	int status;
8251 
8252 	res.fh = nfs_alloc_fhandle();
8253 	if (res.fh == NULL)
8254 		return -ENOMEM;
8255 
8256 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8257 	status = nfs4_call_sync_sequence(clnt, server, &msg,
8258 						&args.seq_args, &res.seq_res);
8259 	nfs_free_fhandle(res.fh);
8260 	if (status)
8261 		return status;
8262 
8263 	do_renew_lease(clp, now);
8264 	return 0;
8265 }
8266 
8267 #ifdef CONFIG_NFS_V4_1
8268 
8269 /*
8270  * This operation also signals the server that this client is
8271  * performing "lease moved" recovery.  The server can stop asserting
8272  * SEQ4_STATUS_LEASE_MOVED for this client.  The client ID performing
8273  * this operation is identified in the SEQUENCE operation in this
8274  * compound.
8275  */
8276 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
8277 {
8278 	struct nfs_server *server = NFS_SERVER(inode);
8279 	struct rpc_clnt *clnt = server->client;
8280 	struct nfs4_fsid_present_arg args = {
8281 		.fh		= NFS_FH(inode),
8282 	};
8283 	struct nfs4_fsid_present_res res = {
8284 	};
8285 	struct rpc_message msg = {
8286 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8287 		.rpc_argp	= &args,
8288 		.rpc_resp	= &res,
8289 		.rpc_cred	= cred,
8290 	};
8291 	int status;
8292 
8293 	res.fh = nfs_alloc_fhandle();
8294 	if (res.fh == NULL)
8295 		return -ENOMEM;
8296 
8297 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8298 	status = nfs4_call_sync_sequence(clnt, server, &msg,
8299 						&args.seq_args, &res.seq_res);
8300 	nfs_free_fhandle(res.fh);
8301 	if (status == NFS4_OK &&
8302 	    res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8303 		status = -NFS4ERR_LEASE_MOVED;
8304 	return status;
8305 }
8306 
8307 #endif	/* CONFIG_NFS_V4_1 */
8308 
8309 /**
8310  * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8311  * @inode: inode on FSID to check
8312  * @cred: credential to use for this operation
8313  *
8314  * Server indicates whether the FSID is present, moved, or not
8315  * recognized.  This operation is necessary to clear a LEASE_MOVED
8316  * condition for this client ID.
8317  *
8318  * Returns NFS4_OK if the FSID is present on this server,
8319  * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8320  *  NFS4ERR code if some error occurred on the server, or a
8321  *  negative errno if a local failure occurred.
8322  */
8323 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
8324 {
8325 	struct nfs_server *server = NFS_SERVER(inode);
8326 	struct nfs_client *clp = server->nfs_client;
8327 	const struct nfs4_mig_recovery_ops *ops =
8328 					clp->cl_mvops->mig_recovery_ops;
8329 	struct nfs4_exception exception = {
8330 		.interruptible = true,
8331 	};
8332 	int status;
8333 
8334 	dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8335 		(unsigned long long)server->fsid.major,
8336 		(unsigned long long)server->fsid.minor,
8337 		clp->cl_hostname);
8338 	nfs_display_fhandle(NFS_FH(inode), __func__);
8339 
8340 	do {
8341 		status = ops->fsid_present(inode, cred);
8342 		if (status != -NFS4ERR_DELAY)
8343 			break;
8344 		nfs4_handle_exception(server, status, &exception);
8345 	} while (exception.retry);
8346 	return status;
8347 }
8348 
8349 /*
8350  * If 'use_integrity' is true and the state managment nfs_client
8351  * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8352  * and the machine credential as per RFC3530bis and RFC5661 Security
8353  * Considerations sections. Otherwise, just use the user cred with the
8354  * filesystem's rpc_client.
8355  */
8356 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8357 {
8358 	int status;
8359 	struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8360 	struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
8361 	struct nfs4_secinfo_arg args = {
8362 		.dir_fh = NFS_FH(dir),
8363 		.name   = name,
8364 	};
8365 	struct nfs4_secinfo_res res = {
8366 		.flavors     = flavors,
8367 	};
8368 	struct rpc_message msg = {
8369 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8370 		.rpc_argp = &args,
8371 		.rpc_resp = &res,
8372 	};
8373 	struct nfs4_call_sync_data data = {
8374 		.seq_server = NFS_SERVER(dir),
8375 		.seq_args = &args.seq_args,
8376 		.seq_res = &res.seq_res,
8377 	};
8378 	struct rpc_task_setup task_setup = {
8379 		.rpc_client = clnt,
8380 		.rpc_message = &msg,
8381 		.callback_ops = clp->cl_mvops->call_sync_ops,
8382 		.callback_data = &data,
8383 		.flags = RPC_TASK_NO_ROUND_ROBIN,
8384 	};
8385 	const struct cred *cred = NULL;
8386 
8387 	if (use_integrity) {
8388 		clnt = clp->cl_rpcclient;
8389 		task_setup.rpc_client = clnt;
8390 
8391 		cred = nfs4_get_clid_cred(clp);
8392 		msg.rpc_cred = cred;
8393 	}
8394 
8395 	dprintk("NFS call  secinfo %s\n", name->name);
8396 
8397 	nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8398 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8399 	status = nfs4_call_sync_custom(&task_setup);
8400 
8401 	dprintk("NFS reply  secinfo: %d\n", status);
8402 
8403 	put_cred(cred);
8404 	return status;
8405 }
8406 
8407 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8408 		      struct nfs4_secinfo_flavors *flavors)
8409 {
8410 	struct nfs4_exception exception = {
8411 		.interruptible = true,
8412 	};
8413 	int err;
8414 	do {
8415 		err = -NFS4ERR_WRONGSEC;
8416 
8417 		/* try to use integrity protection with machine cred */
8418 		if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8419 			err = _nfs4_proc_secinfo(dir, name, flavors, true);
8420 
8421 		/*
8422 		 * if unable to use integrity protection, or SECINFO with
8423 		 * integrity protection returns NFS4ERR_WRONGSEC (which is
8424 		 * disallowed by spec, but exists in deployed servers) use
8425 		 * the current filesystem's rpc_client and the user cred.
8426 		 */
8427 		if (err == -NFS4ERR_WRONGSEC)
8428 			err = _nfs4_proc_secinfo(dir, name, flavors, false);
8429 
8430 		trace_nfs4_secinfo(dir, name, err);
8431 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
8432 				&exception);
8433 	} while (exception.retry);
8434 	return err;
8435 }
8436 
8437 #ifdef CONFIG_NFS_V4_1
8438 /*
8439  * Check the exchange flags returned by the server for invalid flags, having
8440  * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8441  * DS flags set.
8442  */
8443 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
8444 {
8445 	if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8446 		goto out_inval;
8447 	else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
8448 		goto out_inval;
8449 	if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8450 	    (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8451 		goto out_inval;
8452 	if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8453 		goto out_inval;
8454 	return NFS_OK;
8455 out_inval:
8456 	return -NFS4ERR_INVAL;
8457 }
8458 
8459 static bool
8460 nfs41_same_server_scope(struct nfs41_server_scope *a,
8461 			struct nfs41_server_scope *b)
8462 {
8463 	if (a->server_scope_sz != b->server_scope_sz)
8464 		return false;
8465 	return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8466 }
8467 
8468 static void
8469 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8470 {
8471 	struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8472 	struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8473 	struct nfs_client *clp = args->client;
8474 
8475 	switch (task->tk_status) {
8476 	case -NFS4ERR_BADSESSION:
8477 	case -NFS4ERR_DEADSESSION:
8478 		nfs4_schedule_session_recovery(clp->cl_session,
8479 				task->tk_status);
8480 		return;
8481 	}
8482 	if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8483 			res->dir != NFS4_CDFS4_BOTH) {
8484 		rpc_task_close_connection(task);
8485 		if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8486 			rpc_restart_call(task);
8487 	}
8488 }
8489 
8490 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8491 	.rpc_call_done =  nfs4_bind_one_conn_to_session_done,
8492 };
8493 
8494 /*
8495  * nfs4_proc_bind_one_conn_to_session()
8496  *
8497  * The 4.1 client currently uses the same TCP connection for the
8498  * fore and backchannel.
8499  */
8500 static
8501 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8502 		struct rpc_xprt *xprt,
8503 		struct nfs_client *clp,
8504 		const struct cred *cred)
8505 {
8506 	int status;
8507 	struct nfs41_bind_conn_to_session_args args = {
8508 		.client = clp,
8509 		.dir = NFS4_CDFC4_FORE_OR_BOTH,
8510 		.retries = 0,
8511 	};
8512 	struct nfs41_bind_conn_to_session_res res;
8513 	struct rpc_message msg = {
8514 		.rpc_proc =
8515 			&nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8516 		.rpc_argp = &args,
8517 		.rpc_resp = &res,
8518 		.rpc_cred = cred,
8519 	};
8520 	struct rpc_task_setup task_setup_data = {
8521 		.rpc_client = clnt,
8522 		.rpc_xprt = xprt,
8523 		.callback_ops = &nfs4_bind_one_conn_to_session_ops,
8524 		.rpc_message = &msg,
8525 		.flags = RPC_TASK_TIMEOUT,
8526 	};
8527 	struct rpc_task *task;
8528 
8529 	nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8530 	if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8531 		args.dir = NFS4_CDFC4_FORE;
8532 
8533 	/* Do not set the backchannel flag unless this is clnt->cl_xprt */
8534 	if (xprt != rcu_access_pointer(clnt->cl_xprt))
8535 		args.dir = NFS4_CDFC4_FORE;
8536 
8537 	task = rpc_run_task(&task_setup_data);
8538 	if (!IS_ERR(task)) {
8539 		status = task->tk_status;
8540 		rpc_put_task(task);
8541 	} else
8542 		status = PTR_ERR(task);
8543 	trace_nfs4_bind_conn_to_session(clp, status);
8544 	if (status == 0) {
8545 		if (memcmp(res.sessionid.data,
8546 		    clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8547 			dprintk("NFS: %s: Session ID mismatch\n", __func__);
8548 			return -EIO;
8549 		}
8550 		if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8551 			dprintk("NFS: %s: Unexpected direction from server\n",
8552 				__func__);
8553 			return -EIO;
8554 		}
8555 		if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8556 			dprintk("NFS: %s: Server returned RDMA mode = true\n",
8557 				__func__);
8558 			return -EIO;
8559 		}
8560 	}
8561 
8562 	return status;
8563 }
8564 
8565 struct rpc_bind_conn_calldata {
8566 	struct nfs_client *clp;
8567 	const struct cred *cred;
8568 };
8569 
8570 static int
8571 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8572 		struct rpc_xprt *xprt,
8573 		void *calldata)
8574 {
8575 	struct rpc_bind_conn_calldata *p = calldata;
8576 
8577 	return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8578 }
8579 
8580 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8581 {
8582 	struct rpc_bind_conn_calldata data = {
8583 		.clp = clp,
8584 		.cred = cred,
8585 	};
8586 	return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8587 			nfs4_proc_bind_conn_to_session_callback, &data);
8588 }
8589 
8590 /*
8591  * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8592  * and operations we'd like to see to enable certain features in the allow map
8593  */
8594 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8595 	.how = SP4_MACH_CRED,
8596 	.enforce.u.words = {
8597 		[1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8598 		      1 << (OP_EXCHANGE_ID - 32) |
8599 		      1 << (OP_CREATE_SESSION - 32) |
8600 		      1 << (OP_DESTROY_SESSION - 32) |
8601 		      1 << (OP_DESTROY_CLIENTID - 32)
8602 	},
8603 	.allow.u.words = {
8604 		[0] = 1 << (OP_CLOSE) |
8605 		      1 << (OP_OPEN_DOWNGRADE) |
8606 		      1 << (OP_LOCKU) |
8607 		      1 << (OP_DELEGRETURN) |
8608 		      1 << (OP_COMMIT),
8609 		[1] = 1 << (OP_SECINFO - 32) |
8610 		      1 << (OP_SECINFO_NO_NAME - 32) |
8611 		      1 << (OP_LAYOUTRETURN - 32) |
8612 		      1 << (OP_TEST_STATEID - 32) |
8613 		      1 << (OP_FREE_STATEID - 32) |
8614 		      1 << (OP_WRITE - 32)
8615 	}
8616 };
8617 
8618 /*
8619  * Select the state protection mode for client `clp' given the server results
8620  * from exchange_id in `sp'.
8621  *
8622  * Returns 0 on success, negative errno otherwise.
8623  */
8624 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8625 				 struct nfs41_state_protection *sp)
8626 {
8627 	static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8628 		[1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8629 		      1 << (OP_EXCHANGE_ID - 32) |
8630 		      1 << (OP_CREATE_SESSION - 32) |
8631 		      1 << (OP_DESTROY_SESSION - 32) |
8632 		      1 << (OP_DESTROY_CLIENTID - 32)
8633 	};
8634 	unsigned long flags = 0;
8635 	unsigned int i;
8636 	int ret = 0;
8637 
8638 	if (sp->how == SP4_MACH_CRED) {
8639 		/* Print state protect result */
8640 		dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8641 		for (i = 0; i <= LAST_NFS4_OP; i++) {
8642 			if (test_bit(i, sp->enforce.u.longs))
8643 				dfprintk(MOUNT, "  enforce op %d\n", i);
8644 			if (test_bit(i, sp->allow.u.longs))
8645 				dfprintk(MOUNT, "  allow op %d\n", i);
8646 		}
8647 
8648 		/* make sure nothing is on enforce list that isn't supported */
8649 		for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8650 			if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8651 				dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8652 				ret = -EINVAL;
8653 				goto out;
8654 			}
8655 		}
8656 
8657 		/*
8658 		 * Minimal mode - state operations are allowed to use machine
8659 		 * credential.  Note this already happens by default, so the
8660 		 * client doesn't have to do anything more than the negotiation.
8661 		 *
8662 		 * NOTE: we don't care if EXCHANGE_ID is in the list -
8663 		 *       we're already using the machine cred for exchange_id
8664 		 *       and will never use a different cred.
8665 		 */
8666 		if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8667 		    test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8668 		    test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8669 		    test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8670 			dfprintk(MOUNT, "sp4_mach_cred:\n");
8671 			dfprintk(MOUNT, "  minimal mode enabled\n");
8672 			__set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8673 		} else {
8674 			dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8675 			ret = -EINVAL;
8676 			goto out;
8677 		}
8678 
8679 		if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8680 		    test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8681 		    test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8682 		    test_bit(OP_LOCKU, sp->allow.u.longs)) {
8683 			dfprintk(MOUNT, "  cleanup mode enabled\n");
8684 			__set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8685 		}
8686 
8687 		if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8688 			dfprintk(MOUNT, "  pnfs cleanup mode enabled\n");
8689 			__set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8690 		}
8691 
8692 		if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8693 		    test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8694 			dfprintk(MOUNT, "  secinfo mode enabled\n");
8695 			__set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8696 		}
8697 
8698 		if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8699 		    test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8700 			dfprintk(MOUNT, "  stateid mode enabled\n");
8701 			__set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8702 		}
8703 
8704 		if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8705 			dfprintk(MOUNT, "  write mode enabled\n");
8706 			__set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8707 		}
8708 
8709 		if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8710 			dfprintk(MOUNT, "  commit mode enabled\n");
8711 			__set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8712 		}
8713 	}
8714 out:
8715 	clp->cl_sp4_flags = flags;
8716 	return ret;
8717 }
8718 
8719 struct nfs41_exchange_id_data {
8720 	struct nfs41_exchange_id_res res;
8721 	struct nfs41_exchange_id_args args;
8722 };
8723 
8724 static void nfs4_exchange_id_release(void *data)
8725 {
8726 	struct nfs41_exchange_id_data *cdata =
8727 					(struct nfs41_exchange_id_data *)data;
8728 
8729 	nfs_put_client(cdata->args.client);
8730 	kfree(cdata->res.impl_id);
8731 	kfree(cdata->res.server_scope);
8732 	kfree(cdata->res.server_owner);
8733 	kfree(cdata);
8734 }
8735 
8736 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8737 	.rpc_release = nfs4_exchange_id_release,
8738 };
8739 
8740 /*
8741  * _nfs4_proc_exchange_id()
8742  *
8743  * Wrapper for EXCHANGE_ID operation.
8744  */
8745 static struct rpc_task *
8746 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8747 			u32 sp4_how, struct rpc_xprt *xprt)
8748 {
8749 	struct rpc_message msg = {
8750 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8751 		.rpc_cred = cred,
8752 	};
8753 	struct rpc_task_setup task_setup_data = {
8754 		.rpc_client = clp->cl_rpcclient,
8755 		.callback_ops = &nfs4_exchange_id_call_ops,
8756 		.rpc_message = &msg,
8757 		.flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8758 	};
8759 	struct nfs41_exchange_id_data *calldata;
8760 	int status;
8761 
8762 	if (!refcount_inc_not_zero(&clp->cl_count))
8763 		return ERR_PTR(-EIO);
8764 
8765 	status = -ENOMEM;
8766 	calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8767 	if (!calldata)
8768 		goto out;
8769 
8770 	nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8771 
8772 	status = nfs4_init_uniform_client_string(clp);
8773 	if (status)
8774 		goto out_calldata;
8775 
8776 	calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8777 						GFP_NOFS);
8778 	status = -ENOMEM;
8779 	if (unlikely(calldata->res.server_owner == NULL))
8780 		goto out_calldata;
8781 
8782 	calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8783 					GFP_NOFS);
8784 	if (unlikely(calldata->res.server_scope == NULL))
8785 		goto out_server_owner;
8786 
8787 	calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8788 	if (unlikely(calldata->res.impl_id == NULL))
8789 		goto out_server_scope;
8790 
8791 	switch (sp4_how) {
8792 	case SP4_NONE:
8793 		calldata->args.state_protect.how = SP4_NONE;
8794 		break;
8795 
8796 	case SP4_MACH_CRED:
8797 		calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8798 		break;
8799 
8800 	default:
8801 		/* unsupported! */
8802 		WARN_ON_ONCE(1);
8803 		status = -EINVAL;
8804 		goto out_impl_id;
8805 	}
8806 	if (xprt) {
8807 		task_setup_data.rpc_xprt = xprt;
8808 		task_setup_data.flags |= RPC_TASK_SOFTCONN;
8809 		memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8810 				sizeof(calldata->args.verifier.data));
8811 	}
8812 	calldata->args.client = clp;
8813 	calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8814 	EXCHGID4_FLAG_BIND_PRINC_STATEID;
8815 #ifdef CONFIG_NFS_V4_1_MIGRATION
8816 	calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8817 #endif
8818 	if (test_bit(NFS_CS_DS, &clp->cl_flags))
8819 		calldata->args.flags |= EXCHGID4_FLAG_USE_PNFS_DS;
8820 	msg.rpc_argp = &calldata->args;
8821 	msg.rpc_resp = &calldata->res;
8822 	task_setup_data.callback_data = calldata;
8823 
8824 	return rpc_run_task(&task_setup_data);
8825 
8826 out_impl_id:
8827 	kfree(calldata->res.impl_id);
8828 out_server_scope:
8829 	kfree(calldata->res.server_scope);
8830 out_server_owner:
8831 	kfree(calldata->res.server_owner);
8832 out_calldata:
8833 	kfree(calldata);
8834 out:
8835 	nfs_put_client(clp);
8836 	return ERR_PTR(status);
8837 }
8838 
8839 /*
8840  * _nfs4_proc_exchange_id()
8841  *
8842  * Wrapper for EXCHANGE_ID operation.
8843  */
8844 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8845 			u32 sp4_how)
8846 {
8847 	struct rpc_task *task;
8848 	struct nfs41_exchange_id_args *argp;
8849 	struct nfs41_exchange_id_res *resp;
8850 	unsigned long now = jiffies;
8851 	int status;
8852 
8853 	task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8854 	if (IS_ERR(task))
8855 		return PTR_ERR(task);
8856 
8857 	argp = task->tk_msg.rpc_argp;
8858 	resp = task->tk_msg.rpc_resp;
8859 	status = task->tk_status;
8860 	if (status  != 0)
8861 		goto out;
8862 
8863 	status = nfs4_check_cl_exchange_flags(resp->flags,
8864 			clp->cl_mvops->minor_version);
8865 	if (status  != 0)
8866 		goto out;
8867 
8868 	status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8869 	if (status != 0)
8870 		goto out;
8871 
8872 	do_renew_lease(clp, now);
8873 
8874 	clp->cl_clientid = resp->clientid;
8875 	clp->cl_exchange_flags = resp->flags;
8876 	clp->cl_seqid = resp->seqid;
8877 	/* Client ID is not confirmed */
8878 	if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8879 		clear_bit(NFS4_SESSION_ESTABLISHED,
8880 			  &clp->cl_session->session_state);
8881 
8882 	if (clp->cl_serverscope != NULL &&
8883 	    !nfs41_same_server_scope(clp->cl_serverscope,
8884 				resp->server_scope)) {
8885 		dprintk("%s: server_scope mismatch detected\n",
8886 			__func__);
8887 		set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8888 	}
8889 
8890 	swap(clp->cl_serverowner, resp->server_owner);
8891 	swap(clp->cl_serverscope, resp->server_scope);
8892 	swap(clp->cl_implid, resp->impl_id);
8893 
8894 	/* Save the EXCHANGE_ID verifier session trunk tests */
8895 	memcpy(clp->cl_confirm.data, argp->verifier.data,
8896 	       sizeof(clp->cl_confirm.data));
8897 out:
8898 	trace_nfs4_exchange_id(clp, status);
8899 	rpc_put_task(task);
8900 	return status;
8901 }
8902 
8903 /*
8904  * nfs4_proc_exchange_id()
8905  *
8906  * Returns zero, a negative errno, or a negative NFS4ERR status code.
8907  *
8908  * Since the clientid has expired, all compounds using sessions
8909  * associated with the stale clientid will be returning
8910  * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8911  * be in some phase of session reset.
8912  *
8913  * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8914  */
8915 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8916 {
8917 	rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8918 	int status;
8919 
8920 	/* try SP4_MACH_CRED if krb5i/p	*/
8921 	if (authflavor == RPC_AUTH_GSS_KRB5I ||
8922 	    authflavor == RPC_AUTH_GSS_KRB5P) {
8923 		status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8924 		if (!status)
8925 			return 0;
8926 	}
8927 
8928 	/* try SP4_NONE */
8929 	return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8930 }
8931 
8932 /**
8933  * nfs4_test_session_trunk
8934  *
8935  * This is an add_xprt_test() test function called from
8936  * rpc_clnt_setup_test_and_add_xprt.
8937  *
8938  * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8939  * and is dereferrenced in nfs4_exchange_id_release
8940  *
8941  * Upon success, add the new transport to the rpc_clnt
8942  *
8943  * @clnt: struct rpc_clnt to get new transport
8944  * @xprt: the rpc_xprt to test
8945  * @data: call data for _nfs4_proc_exchange_id.
8946  */
8947 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8948 			    void *data)
8949 {
8950 	struct nfs4_add_xprt_data *adata = data;
8951 	struct rpc_task *task;
8952 	int status;
8953 
8954 	u32 sp4_how;
8955 
8956 	dprintk("--> %s try %s\n", __func__,
8957 		xprt->address_strings[RPC_DISPLAY_ADDR]);
8958 
8959 	sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8960 
8961 try_again:
8962 	/* Test connection for session trunking. Async exchange_id call */
8963 	task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8964 	if (IS_ERR(task))
8965 		return;
8966 
8967 	status = task->tk_status;
8968 	if (status == 0)
8969 		status = nfs4_detect_session_trunking(adata->clp,
8970 				task->tk_msg.rpc_resp, xprt);
8971 
8972 	if (status == 0)
8973 		rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8974 	else if (status != -NFS4ERR_DELAY && rpc_clnt_xprt_switch_has_addr(clnt,
8975 				(struct sockaddr *)&xprt->addr))
8976 		rpc_clnt_xprt_switch_remove_xprt(clnt, xprt);
8977 
8978 	rpc_put_task(task);
8979 	if (status == -NFS4ERR_DELAY) {
8980 		ssleep(1);
8981 		goto try_again;
8982 	}
8983 }
8984 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8985 
8986 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8987 		const struct cred *cred)
8988 {
8989 	struct rpc_message msg = {
8990 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8991 		.rpc_argp = clp,
8992 		.rpc_cred = cred,
8993 	};
8994 	int status;
8995 
8996 	status = rpc_call_sync(clp->cl_rpcclient, &msg,
8997 			       RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8998 	trace_nfs4_destroy_clientid(clp, status);
8999 	if (status)
9000 		dprintk("NFS: Got error %d from the server %s on "
9001 			"DESTROY_CLIENTID.", status, clp->cl_hostname);
9002 	return status;
9003 }
9004 
9005 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
9006 		const struct cred *cred)
9007 {
9008 	unsigned int loop;
9009 	int ret;
9010 
9011 	for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
9012 		ret = _nfs4_proc_destroy_clientid(clp, cred);
9013 		switch (ret) {
9014 		case -NFS4ERR_DELAY:
9015 		case -NFS4ERR_CLIENTID_BUSY:
9016 			ssleep(1);
9017 			break;
9018 		default:
9019 			return ret;
9020 		}
9021 	}
9022 	return 0;
9023 }
9024 
9025 int nfs4_destroy_clientid(struct nfs_client *clp)
9026 {
9027 	const struct cred *cred;
9028 	int ret = 0;
9029 
9030 	if (clp->cl_mvops->minor_version < 1)
9031 		goto out;
9032 	if (clp->cl_exchange_flags == 0)
9033 		goto out;
9034 	if (clp->cl_preserve_clid)
9035 		goto out;
9036 	cred = nfs4_get_clid_cred(clp);
9037 	ret = nfs4_proc_destroy_clientid(clp, cred);
9038 	put_cred(cred);
9039 	switch (ret) {
9040 	case 0:
9041 	case -NFS4ERR_STALE_CLIENTID:
9042 		clp->cl_exchange_flags = 0;
9043 	}
9044 out:
9045 	return ret;
9046 }
9047 
9048 #endif /* CONFIG_NFS_V4_1 */
9049 
9050 struct nfs4_get_lease_time_data {
9051 	struct nfs4_get_lease_time_args *args;
9052 	struct nfs4_get_lease_time_res *res;
9053 	struct nfs_client *clp;
9054 };
9055 
9056 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
9057 					void *calldata)
9058 {
9059 	struct nfs4_get_lease_time_data *data =
9060 			(struct nfs4_get_lease_time_data *)calldata;
9061 
9062 	/* just setup sequence, do not trigger session recovery
9063 	   since we're invoked within one */
9064 	nfs4_setup_sequence(data->clp,
9065 			&data->args->la_seq_args,
9066 			&data->res->lr_seq_res,
9067 			task);
9068 }
9069 
9070 /*
9071  * Called from nfs4_state_manager thread for session setup, so don't recover
9072  * from sequence operation or clientid errors.
9073  */
9074 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
9075 {
9076 	struct nfs4_get_lease_time_data *data =
9077 			(struct nfs4_get_lease_time_data *)calldata;
9078 
9079 	if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
9080 		return;
9081 	switch (task->tk_status) {
9082 	case -NFS4ERR_DELAY:
9083 	case -NFS4ERR_GRACE:
9084 		rpc_delay(task, NFS4_POLL_RETRY_MIN);
9085 		task->tk_status = 0;
9086 		fallthrough;
9087 	case -NFS4ERR_RETRY_UNCACHED_REP:
9088 		rpc_restart_call_prepare(task);
9089 		return;
9090 	}
9091 }
9092 
9093 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
9094 	.rpc_call_prepare = nfs4_get_lease_time_prepare,
9095 	.rpc_call_done = nfs4_get_lease_time_done,
9096 };
9097 
9098 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
9099 {
9100 	struct nfs4_get_lease_time_args args;
9101 	struct nfs4_get_lease_time_res res = {
9102 		.lr_fsinfo = fsinfo,
9103 	};
9104 	struct nfs4_get_lease_time_data data = {
9105 		.args = &args,
9106 		.res = &res,
9107 		.clp = clp,
9108 	};
9109 	struct rpc_message msg = {
9110 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
9111 		.rpc_argp = &args,
9112 		.rpc_resp = &res,
9113 	};
9114 	struct rpc_task_setup task_setup = {
9115 		.rpc_client = clp->cl_rpcclient,
9116 		.rpc_message = &msg,
9117 		.callback_ops = &nfs4_get_lease_time_ops,
9118 		.callback_data = &data,
9119 		.flags = RPC_TASK_TIMEOUT,
9120 	};
9121 
9122 	nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
9123 	return nfs4_call_sync_custom(&task_setup);
9124 }
9125 
9126 #ifdef CONFIG_NFS_V4_1
9127 
9128 /*
9129  * Initialize the values to be used by the client in CREATE_SESSION
9130  * If nfs4_init_session set the fore channel request and response sizes,
9131  * use them.
9132  *
9133  * Set the back channel max_resp_sz_cached to zero to force the client to
9134  * always set csa_cachethis to FALSE because the current implementation
9135  * of the back channel DRC only supports caching the CB_SEQUENCE operation.
9136  */
9137 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
9138 				    struct rpc_clnt *clnt)
9139 {
9140 	unsigned int max_rqst_sz, max_resp_sz;
9141 	unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
9142 	unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
9143 
9144 	max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
9145 	max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
9146 
9147 	/* Fore channel attributes */
9148 	args->fc_attrs.max_rqst_sz = max_rqst_sz;
9149 	args->fc_attrs.max_resp_sz = max_resp_sz;
9150 	args->fc_attrs.max_ops = NFS4_MAX_OPS;
9151 	args->fc_attrs.max_reqs = max_session_slots;
9152 
9153 	dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
9154 		"max_ops=%u max_reqs=%u\n",
9155 		__func__,
9156 		args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
9157 		args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
9158 
9159 	/* Back channel attributes */
9160 	args->bc_attrs.max_rqst_sz = max_bc_payload;
9161 	args->bc_attrs.max_resp_sz = max_bc_payload;
9162 	args->bc_attrs.max_resp_sz_cached = 0;
9163 	args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
9164 	args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
9165 	if (args->bc_attrs.max_reqs > max_bc_slots)
9166 		args->bc_attrs.max_reqs = max_bc_slots;
9167 
9168 	dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
9169 		"max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
9170 		__func__,
9171 		args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
9172 		args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
9173 		args->bc_attrs.max_reqs);
9174 }
9175 
9176 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
9177 		struct nfs41_create_session_res *res)
9178 {
9179 	struct nfs4_channel_attrs *sent = &args->fc_attrs;
9180 	struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
9181 
9182 	if (rcvd->max_resp_sz > sent->max_resp_sz)
9183 		return -EINVAL;
9184 	/*
9185 	 * Our requested max_ops is the minimum we need; we're not
9186 	 * prepared to break up compounds into smaller pieces than that.
9187 	 * So, no point even trying to continue if the server won't
9188 	 * cooperate:
9189 	 */
9190 	if (rcvd->max_ops < sent->max_ops)
9191 		return -EINVAL;
9192 	if (rcvd->max_reqs == 0)
9193 		return -EINVAL;
9194 	if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
9195 		rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
9196 	return 0;
9197 }
9198 
9199 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
9200 		struct nfs41_create_session_res *res)
9201 {
9202 	struct nfs4_channel_attrs *sent = &args->bc_attrs;
9203 	struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
9204 
9205 	if (!(res->flags & SESSION4_BACK_CHAN))
9206 		goto out;
9207 	if (rcvd->max_rqst_sz > sent->max_rqst_sz)
9208 		return -EINVAL;
9209 	if (rcvd->max_resp_sz < sent->max_resp_sz)
9210 		return -EINVAL;
9211 	if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
9212 		return -EINVAL;
9213 	if (rcvd->max_ops > sent->max_ops)
9214 		return -EINVAL;
9215 	if (rcvd->max_reqs > sent->max_reqs)
9216 		return -EINVAL;
9217 out:
9218 	return 0;
9219 }
9220 
9221 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
9222 				     struct nfs41_create_session_res *res)
9223 {
9224 	int ret;
9225 
9226 	ret = nfs4_verify_fore_channel_attrs(args, res);
9227 	if (ret)
9228 		return ret;
9229 	return nfs4_verify_back_channel_attrs(args, res);
9230 }
9231 
9232 static void nfs4_update_session(struct nfs4_session *session,
9233 		struct nfs41_create_session_res *res)
9234 {
9235 	nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
9236 	/* Mark client id and session as being confirmed */
9237 	session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
9238 	set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
9239 	session->flags = res->flags;
9240 	memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
9241 	if (res->flags & SESSION4_BACK_CHAN)
9242 		memcpy(&session->bc_attrs, &res->bc_attrs,
9243 				sizeof(session->bc_attrs));
9244 }
9245 
9246 static int _nfs4_proc_create_session(struct nfs_client *clp,
9247 		const struct cred *cred)
9248 {
9249 	struct nfs4_session *session = clp->cl_session;
9250 	struct nfs41_create_session_args args = {
9251 		.client = clp,
9252 		.clientid = clp->cl_clientid,
9253 		.seqid = clp->cl_seqid,
9254 		.cb_program = NFS4_CALLBACK,
9255 	};
9256 	struct nfs41_create_session_res res;
9257 
9258 	struct rpc_message msg = {
9259 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
9260 		.rpc_argp = &args,
9261 		.rpc_resp = &res,
9262 		.rpc_cred = cred,
9263 	};
9264 	int status;
9265 
9266 	nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
9267 	args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
9268 
9269 	status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9270 			       RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9271 	trace_nfs4_create_session(clp, status);
9272 
9273 	switch (status) {
9274 	case -NFS4ERR_STALE_CLIENTID:
9275 	case -NFS4ERR_DELAY:
9276 	case -ETIMEDOUT:
9277 	case -EACCES:
9278 	case -EAGAIN:
9279 		goto out;
9280 	}
9281 
9282 	clp->cl_seqid++;
9283 	if (!status) {
9284 		/* Verify the session's negotiated channel_attrs values */
9285 		status = nfs4_verify_channel_attrs(&args, &res);
9286 		/* Increment the clientid slot sequence id */
9287 		if (status)
9288 			goto out;
9289 		nfs4_update_session(session, &res);
9290 	}
9291 out:
9292 	return status;
9293 }
9294 
9295 /*
9296  * Issues a CREATE_SESSION operation to the server.
9297  * It is the responsibility of the caller to verify the session is
9298  * expired before calling this routine.
9299  */
9300 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
9301 {
9302 	int status;
9303 	unsigned *ptr;
9304 	struct nfs4_session *session = clp->cl_session;
9305 	struct nfs4_add_xprt_data xprtdata = {
9306 		.clp = clp,
9307 	};
9308 	struct rpc_add_xprt_test rpcdata = {
9309 		.add_xprt_test = clp->cl_mvops->session_trunk,
9310 		.data = &xprtdata,
9311 	};
9312 
9313 	dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9314 
9315 	status = _nfs4_proc_create_session(clp, cred);
9316 	if (status)
9317 		goto out;
9318 
9319 	/* Init or reset the session slot tables */
9320 	status = nfs4_setup_session_slot_tables(session);
9321 	dprintk("slot table setup returned %d\n", status);
9322 	if (status)
9323 		goto out;
9324 
9325 	ptr = (unsigned *)&session->sess_id.data[0];
9326 	dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9327 		clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
9328 	rpc_clnt_probe_trunked_xprts(clp->cl_rpcclient, &rpcdata);
9329 out:
9330 	return status;
9331 }
9332 
9333 /*
9334  * Issue the over-the-wire RPC DESTROY_SESSION.
9335  * The caller must serialize access to this routine.
9336  */
9337 int nfs4_proc_destroy_session(struct nfs4_session *session,
9338 		const struct cred *cred)
9339 {
9340 	struct rpc_message msg = {
9341 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9342 		.rpc_argp = session,
9343 		.rpc_cred = cred,
9344 	};
9345 	int status = 0;
9346 
9347 	/* session is still being setup */
9348 	if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9349 		return 0;
9350 
9351 	status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9352 			       RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9353 	trace_nfs4_destroy_session(session->clp, status);
9354 
9355 	if (status)
9356 		dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
9357 			"Session has been destroyed regardless...\n", status);
9358 	rpc_clnt_manage_trunked_xprts(session->clp->cl_rpcclient);
9359 	return status;
9360 }
9361 
9362 /*
9363  * Renew the cl_session lease.
9364  */
9365 struct nfs4_sequence_data {
9366 	struct nfs_client *clp;
9367 	struct nfs4_sequence_args args;
9368 	struct nfs4_sequence_res res;
9369 };
9370 
9371 static void nfs41_sequence_release(void *data)
9372 {
9373 	struct nfs4_sequence_data *calldata = data;
9374 	struct nfs_client *clp = calldata->clp;
9375 
9376 	if (refcount_read(&clp->cl_count) > 1)
9377 		nfs4_schedule_state_renewal(clp);
9378 	nfs_put_client(clp);
9379 	kfree(calldata);
9380 }
9381 
9382 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9383 {
9384 	switch(task->tk_status) {
9385 	case -NFS4ERR_DELAY:
9386 		rpc_delay(task, NFS4_POLL_RETRY_MAX);
9387 		return -EAGAIN;
9388 	default:
9389 		nfs4_schedule_lease_recovery(clp);
9390 	}
9391 	return 0;
9392 }
9393 
9394 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
9395 {
9396 	struct nfs4_sequence_data *calldata = data;
9397 	struct nfs_client *clp = calldata->clp;
9398 
9399 	if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9400 		return;
9401 
9402 	trace_nfs4_sequence(clp, task->tk_status);
9403 	if (task->tk_status < 0 && !task->tk_client->cl_shutdown) {
9404 		dprintk("%s ERROR %d\n", __func__, task->tk_status);
9405 		if (refcount_read(&clp->cl_count) == 1)
9406 			return;
9407 
9408 		if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9409 			rpc_restart_call_prepare(task);
9410 			return;
9411 		}
9412 	}
9413 	dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
9414 }
9415 
9416 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9417 {
9418 	struct nfs4_sequence_data *calldata = data;
9419 	struct nfs_client *clp = calldata->clp;
9420 	struct nfs4_sequence_args *args;
9421 	struct nfs4_sequence_res *res;
9422 
9423 	args = task->tk_msg.rpc_argp;
9424 	res = task->tk_msg.rpc_resp;
9425 
9426 	nfs4_setup_sequence(clp, args, res, task);
9427 }
9428 
9429 static const struct rpc_call_ops nfs41_sequence_ops = {
9430 	.rpc_call_done = nfs41_sequence_call_done,
9431 	.rpc_call_prepare = nfs41_sequence_prepare,
9432 	.rpc_release = nfs41_sequence_release,
9433 };
9434 
9435 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9436 		const struct cred *cred,
9437 		struct nfs4_slot *slot,
9438 		bool is_privileged)
9439 {
9440 	struct nfs4_sequence_data *calldata;
9441 	struct rpc_message msg = {
9442 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9443 		.rpc_cred = cred,
9444 	};
9445 	struct rpc_task_setup task_setup_data = {
9446 		.rpc_client = clp->cl_rpcclient,
9447 		.rpc_message = &msg,
9448 		.callback_ops = &nfs41_sequence_ops,
9449 		.flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
9450 	};
9451 	struct rpc_task *ret;
9452 
9453 	ret = ERR_PTR(-EIO);
9454 	if (!refcount_inc_not_zero(&clp->cl_count))
9455 		goto out_err;
9456 
9457 	ret = ERR_PTR(-ENOMEM);
9458 	calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
9459 	if (calldata == NULL)
9460 		goto out_put_clp;
9461 	nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9462 	nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9463 	msg.rpc_argp = &calldata->args;
9464 	msg.rpc_resp = &calldata->res;
9465 	calldata->clp = clp;
9466 	task_setup_data.callback_data = calldata;
9467 
9468 	ret = rpc_run_task(&task_setup_data);
9469 	if (IS_ERR(ret))
9470 		goto out_err;
9471 	return ret;
9472 out_put_clp:
9473 	nfs_put_client(clp);
9474 out_err:
9475 	nfs41_release_slot(slot);
9476 	return ret;
9477 }
9478 
9479 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9480 {
9481 	struct rpc_task *task;
9482 	int ret = 0;
9483 
9484 	if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9485 		return -EAGAIN;
9486 	task = _nfs41_proc_sequence(clp, cred, NULL, false);
9487 	if (IS_ERR(task))
9488 		ret = PTR_ERR(task);
9489 	else
9490 		rpc_put_task_async(task);
9491 	dprintk("<-- %s status=%d\n", __func__, ret);
9492 	return ret;
9493 }
9494 
9495 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9496 {
9497 	struct rpc_task *task;
9498 	int ret;
9499 
9500 	task = _nfs41_proc_sequence(clp, cred, NULL, true);
9501 	if (IS_ERR(task)) {
9502 		ret = PTR_ERR(task);
9503 		goto out;
9504 	}
9505 	ret = rpc_wait_for_completion_task(task);
9506 	if (!ret)
9507 		ret = task->tk_status;
9508 	rpc_put_task(task);
9509 out:
9510 	dprintk("<-- %s status=%d\n", __func__, ret);
9511 	return ret;
9512 }
9513 
9514 struct nfs4_reclaim_complete_data {
9515 	struct nfs_client *clp;
9516 	struct nfs41_reclaim_complete_args arg;
9517 	struct nfs41_reclaim_complete_res res;
9518 };
9519 
9520 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9521 {
9522 	struct nfs4_reclaim_complete_data *calldata = data;
9523 
9524 	nfs4_setup_sequence(calldata->clp,
9525 			&calldata->arg.seq_args,
9526 			&calldata->res.seq_res,
9527 			task);
9528 }
9529 
9530 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9531 {
9532 	switch(task->tk_status) {
9533 	case 0:
9534 		wake_up_all(&clp->cl_lock_waitq);
9535 		fallthrough;
9536 	case -NFS4ERR_COMPLETE_ALREADY:
9537 	case -NFS4ERR_WRONG_CRED: /* What to do here? */
9538 		break;
9539 	case -NFS4ERR_DELAY:
9540 		rpc_delay(task, NFS4_POLL_RETRY_MAX);
9541 		fallthrough;
9542 	case -NFS4ERR_RETRY_UNCACHED_REP:
9543 	case -EACCES:
9544 		dprintk("%s: failed to reclaim complete error %d for server %s, retrying\n",
9545 			__func__, task->tk_status, clp->cl_hostname);
9546 		return -EAGAIN;
9547 	case -NFS4ERR_BADSESSION:
9548 	case -NFS4ERR_DEADSESSION:
9549 	case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9550 		break;
9551 	default:
9552 		nfs4_schedule_lease_recovery(clp);
9553 	}
9554 	return 0;
9555 }
9556 
9557 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9558 {
9559 	struct nfs4_reclaim_complete_data *calldata = data;
9560 	struct nfs_client *clp = calldata->clp;
9561 	struct nfs4_sequence_res *res = &calldata->res.seq_res;
9562 
9563 	if (!nfs41_sequence_done(task, res))
9564 		return;
9565 
9566 	trace_nfs4_reclaim_complete(clp, task->tk_status);
9567 	if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9568 		rpc_restart_call_prepare(task);
9569 		return;
9570 	}
9571 }
9572 
9573 static void nfs4_free_reclaim_complete_data(void *data)
9574 {
9575 	struct nfs4_reclaim_complete_data *calldata = data;
9576 
9577 	kfree(calldata);
9578 }
9579 
9580 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9581 	.rpc_call_prepare = nfs4_reclaim_complete_prepare,
9582 	.rpc_call_done = nfs4_reclaim_complete_done,
9583 	.rpc_release = nfs4_free_reclaim_complete_data,
9584 };
9585 
9586 /*
9587  * Issue a global reclaim complete.
9588  */
9589 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9590 		const struct cred *cred)
9591 {
9592 	struct nfs4_reclaim_complete_data *calldata;
9593 	struct rpc_message msg = {
9594 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9595 		.rpc_cred = cred,
9596 	};
9597 	struct rpc_task_setup task_setup_data = {
9598 		.rpc_client = clp->cl_rpcclient,
9599 		.rpc_message = &msg,
9600 		.callback_ops = &nfs4_reclaim_complete_call_ops,
9601 		.flags = RPC_TASK_NO_ROUND_ROBIN,
9602 	};
9603 	int status = -ENOMEM;
9604 
9605 	calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9606 	if (calldata == NULL)
9607 		goto out;
9608 	calldata->clp = clp;
9609 	calldata->arg.one_fs = 0;
9610 
9611 	nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9612 	msg.rpc_argp = &calldata->arg;
9613 	msg.rpc_resp = &calldata->res;
9614 	task_setup_data.callback_data = calldata;
9615 	status = nfs4_call_sync_custom(&task_setup_data);
9616 out:
9617 	dprintk("<-- %s status=%d\n", __func__, status);
9618 	return status;
9619 }
9620 
9621 static void
9622 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9623 {
9624 	struct nfs4_layoutget *lgp = calldata;
9625 	struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9626 
9627 	nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9628 				&lgp->res.seq_res, task);
9629 }
9630 
9631 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9632 {
9633 	struct nfs4_layoutget *lgp = calldata;
9634 
9635 	nfs41_sequence_process(task, &lgp->res.seq_res);
9636 }
9637 
9638 static int
9639 nfs4_layoutget_handle_exception(struct rpc_task *task,
9640 		struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9641 {
9642 	struct inode *inode = lgp->args.inode;
9643 	struct nfs_server *server = NFS_SERVER(inode);
9644 	struct pnfs_layout_hdr *lo = lgp->lo;
9645 	int nfs4err = task->tk_status;
9646 	int err, status = 0;
9647 	LIST_HEAD(head);
9648 
9649 	dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9650 
9651 	nfs4_sequence_free_slot(&lgp->res.seq_res);
9652 
9653 	switch (nfs4err) {
9654 	case 0:
9655 		goto out;
9656 
9657 	/*
9658 	 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9659 	 * on the file. set tk_status to -ENODATA to tell upper layer to
9660 	 * retry go inband.
9661 	 */
9662 	case -NFS4ERR_LAYOUTUNAVAILABLE:
9663 		status = -ENODATA;
9664 		goto out;
9665 	/*
9666 	 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9667 	 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9668 	 */
9669 	case -NFS4ERR_BADLAYOUT:
9670 		status = -EOVERFLOW;
9671 		goto out;
9672 	/*
9673 	 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9674 	 * (or clients) writing to the same RAID stripe except when
9675 	 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9676 	 *
9677 	 * Treat it like we would RECALLCONFLICT -- we retry for a little
9678 	 * while, and then eventually give up.
9679 	 */
9680 	case -NFS4ERR_LAYOUTTRYLATER:
9681 		if (lgp->args.minlength == 0) {
9682 			status = -EOVERFLOW;
9683 			goto out;
9684 		}
9685 		status = -EBUSY;
9686 		break;
9687 	case -NFS4ERR_RECALLCONFLICT:
9688 	case -NFS4ERR_RETURNCONFLICT:
9689 		status = -ERECALLCONFLICT;
9690 		break;
9691 	case -NFS4ERR_DELEG_REVOKED:
9692 	case -NFS4ERR_ADMIN_REVOKED:
9693 	case -NFS4ERR_EXPIRED:
9694 	case -NFS4ERR_BAD_STATEID:
9695 		exception->timeout = 0;
9696 		spin_lock(&inode->i_lock);
9697 		/* If the open stateid was bad, then recover it. */
9698 		if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9699 		    !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9700 			spin_unlock(&inode->i_lock);
9701 			exception->state = lgp->args.ctx->state;
9702 			exception->stateid = &lgp->args.stateid;
9703 			break;
9704 		}
9705 
9706 		/*
9707 		 * Mark the bad layout state as invalid, then retry
9708 		 */
9709 		pnfs_mark_layout_stateid_invalid(lo, &head);
9710 		spin_unlock(&inode->i_lock);
9711 		nfs_commit_inode(inode, 0);
9712 		pnfs_free_lseg_list(&head);
9713 		status = -EAGAIN;
9714 		goto out;
9715 	}
9716 
9717 	err = nfs4_handle_exception(server, nfs4err, exception);
9718 	if (!status) {
9719 		if (exception->retry)
9720 			status = -EAGAIN;
9721 		else
9722 			status = err;
9723 	}
9724 out:
9725 	return status;
9726 }
9727 
9728 size_t max_response_pages(struct nfs_server *server)
9729 {
9730 	u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9731 	return nfs_page_array_len(0, max_resp_sz);
9732 }
9733 
9734 static void nfs4_layoutget_release(void *calldata)
9735 {
9736 	struct nfs4_layoutget *lgp = calldata;
9737 
9738 	nfs4_sequence_free_slot(&lgp->res.seq_res);
9739 	pnfs_layoutget_free(lgp);
9740 }
9741 
9742 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9743 	.rpc_call_prepare = nfs4_layoutget_prepare,
9744 	.rpc_call_done = nfs4_layoutget_done,
9745 	.rpc_release = nfs4_layoutget_release,
9746 };
9747 
9748 struct pnfs_layout_segment *
9749 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9750 {
9751 	struct inode *inode = lgp->args.inode;
9752 	struct nfs_server *server = NFS_SERVER(inode);
9753 	struct rpc_task *task;
9754 	struct rpc_message msg = {
9755 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9756 		.rpc_argp = &lgp->args,
9757 		.rpc_resp = &lgp->res,
9758 		.rpc_cred = lgp->cred,
9759 	};
9760 	struct rpc_task_setup task_setup_data = {
9761 		.rpc_client = server->client,
9762 		.rpc_message = &msg,
9763 		.callback_ops = &nfs4_layoutget_call_ops,
9764 		.callback_data = lgp,
9765 		.flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF |
9766 			 RPC_TASK_MOVEABLE,
9767 	};
9768 	struct pnfs_layout_segment *lseg = NULL;
9769 	struct nfs4_exception exception = {
9770 		.inode = inode,
9771 		.timeout = *timeout,
9772 	};
9773 	int status = 0;
9774 
9775 	nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9776 
9777 	task = rpc_run_task(&task_setup_data);
9778 	if (IS_ERR(task))
9779 		return ERR_CAST(task);
9780 
9781 	status = rpc_wait_for_completion_task(task);
9782 	if (status != 0)
9783 		goto out;
9784 
9785 	if (task->tk_status < 0) {
9786 		status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9787 		*timeout = exception.timeout;
9788 	} else if (lgp->res.layoutp->len == 0) {
9789 		status = -EAGAIN;
9790 		*timeout = nfs4_update_delay(&exception.timeout);
9791 	} else
9792 		lseg = pnfs_layout_process(lgp);
9793 out:
9794 	trace_nfs4_layoutget(lgp->args.ctx,
9795 			&lgp->args.range,
9796 			&lgp->res.range,
9797 			&lgp->res.stateid,
9798 			status);
9799 
9800 	rpc_put_task(task);
9801 	dprintk("<-- %s status=%d\n", __func__, status);
9802 	if (status)
9803 		return ERR_PTR(status);
9804 	return lseg;
9805 }
9806 
9807 static void
9808 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9809 {
9810 	struct nfs4_layoutreturn *lrp = calldata;
9811 
9812 	nfs4_setup_sequence(lrp->clp,
9813 			&lrp->args.seq_args,
9814 			&lrp->res.seq_res,
9815 			task);
9816 	if (!pnfs_layout_is_valid(lrp->args.layout))
9817 		rpc_exit(task, 0);
9818 }
9819 
9820 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9821 {
9822 	struct nfs4_layoutreturn *lrp = calldata;
9823 	struct nfs_server *server;
9824 
9825 	if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9826 		return;
9827 
9828 	/*
9829 	 * Was there an RPC level error? Assume the call succeeded,
9830 	 * and that we need to release the layout
9831 	 */
9832 	if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9833 		lrp->res.lrs_present = 0;
9834 		return;
9835 	}
9836 
9837 	server = NFS_SERVER(lrp->args.inode);
9838 	switch (task->tk_status) {
9839 	case -NFS4ERR_OLD_STATEID:
9840 		if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9841 					&lrp->args.range,
9842 					lrp->args.inode))
9843 			goto out_restart;
9844 		fallthrough;
9845 	default:
9846 		task->tk_status = 0;
9847 		fallthrough;
9848 	case 0:
9849 		break;
9850 	case -NFS4ERR_DELAY:
9851 		if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9852 			break;
9853 		goto out_restart;
9854 	}
9855 	return;
9856 out_restart:
9857 	task->tk_status = 0;
9858 	nfs4_sequence_free_slot(&lrp->res.seq_res);
9859 	rpc_restart_call_prepare(task);
9860 }
9861 
9862 static void nfs4_layoutreturn_release(void *calldata)
9863 {
9864 	struct nfs4_layoutreturn *lrp = calldata;
9865 	struct pnfs_layout_hdr *lo = lrp->args.layout;
9866 
9867 	pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9868 			lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9869 	nfs4_sequence_free_slot(&lrp->res.seq_res);
9870 	if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9871 		lrp->ld_private.ops->free(&lrp->ld_private);
9872 	pnfs_put_layout_hdr(lrp->args.layout);
9873 	nfs_iput_and_deactive(lrp->inode);
9874 	put_cred(lrp->cred);
9875 	kfree(calldata);
9876 }
9877 
9878 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9879 	.rpc_call_prepare = nfs4_layoutreturn_prepare,
9880 	.rpc_call_done = nfs4_layoutreturn_done,
9881 	.rpc_release = nfs4_layoutreturn_release,
9882 };
9883 
9884 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9885 {
9886 	struct rpc_task *task;
9887 	struct rpc_message msg = {
9888 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9889 		.rpc_argp = &lrp->args,
9890 		.rpc_resp = &lrp->res,
9891 		.rpc_cred = lrp->cred,
9892 	};
9893 	struct rpc_task_setup task_setup_data = {
9894 		.rpc_client = NFS_SERVER(lrp->args.inode)->client,
9895 		.rpc_message = &msg,
9896 		.callback_ops = &nfs4_layoutreturn_call_ops,
9897 		.callback_data = lrp,
9898 		.flags = RPC_TASK_MOVEABLE,
9899 	};
9900 	int status = 0;
9901 
9902 	nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9903 			NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9904 			&task_setup_data.rpc_client, &msg);
9905 
9906 	lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9907 	if (!sync) {
9908 		if (!lrp->inode) {
9909 			nfs4_layoutreturn_release(lrp);
9910 			return -EAGAIN;
9911 		}
9912 		task_setup_data.flags |= RPC_TASK_ASYNC;
9913 	}
9914 	if (!lrp->inode)
9915 		nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9916 				   1);
9917 	else
9918 		nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9919 				   0);
9920 	task = rpc_run_task(&task_setup_data);
9921 	if (IS_ERR(task))
9922 		return PTR_ERR(task);
9923 	if (sync)
9924 		status = task->tk_status;
9925 	trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9926 	dprintk("<-- %s status=%d\n", __func__, status);
9927 	rpc_put_task(task);
9928 	return status;
9929 }
9930 
9931 static int
9932 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9933 		struct pnfs_device *pdev,
9934 		const struct cred *cred)
9935 {
9936 	struct nfs4_getdeviceinfo_args args = {
9937 		.pdev = pdev,
9938 		.notify_types = NOTIFY_DEVICEID4_CHANGE |
9939 			NOTIFY_DEVICEID4_DELETE,
9940 	};
9941 	struct nfs4_getdeviceinfo_res res = {
9942 		.pdev = pdev,
9943 	};
9944 	struct rpc_message msg = {
9945 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9946 		.rpc_argp = &args,
9947 		.rpc_resp = &res,
9948 		.rpc_cred = cred,
9949 	};
9950 	int status;
9951 
9952 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9953 	if (res.notification & ~args.notify_types)
9954 		dprintk("%s: unsupported notification\n", __func__);
9955 	if (res.notification != args.notify_types)
9956 		pdev->nocache = 1;
9957 
9958 	trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9959 
9960 	dprintk("<-- %s status=%d\n", __func__, status);
9961 
9962 	return status;
9963 }
9964 
9965 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9966 		struct pnfs_device *pdev,
9967 		const struct cred *cred)
9968 {
9969 	struct nfs4_exception exception = { };
9970 	int err;
9971 
9972 	do {
9973 		err = nfs4_handle_exception(server,
9974 					_nfs4_proc_getdeviceinfo(server, pdev, cred),
9975 					&exception);
9976 	} while (exception.retry);
9977 	return err;
9978 }
9979 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9980 
9981 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9982 {
9983 	struct nfs4_layoutcommit_data *data = calldata;
9984 	struct nfs_server *server = NFS_SERVER(data->args.inode);
9985 
9986 	nfs4_setup_sequence(server->nfs_client,
9987 			&data->args.seq_args,
9988 			&data->res.seq_res,
9989 			task);
9990 }
9991 
9992 static void
9993 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9994 {
9995 	struct nfs4_layoutcommit_data *data = calldata;
9996 	struct nfs_server *server = NFS_SERVER(data->args.inode);
9997 
9998 	if (!nfs41_sequence_done(task, &data->res.seq_res))
9999 		return;
10000 
10001 	switch (task->tk_status) { /* Just ignore these failures */
10002 	case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
10003 	case -NFS4ERR_BADIOMODE:     /* no IOMODE_RW layout for range */
10004 	case -NFS4ERR_BADLAYOUT:     /* no layout */
10005 	case -NFS4ERR_GRACE:	    /* loca_recalim always false */
10006 		task->tk_status = 0;
10007 		break;
10008 	case 0:
10009 		break;
10010 	default:
10011 		if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
10012 			rpc_restart_call_prepare(task);
10013 			return;
10014 		}
10015 	}
10016 }
10017 
10018 static void nfs4_layoutcommit_release(void *calldata)
10019 {
10020 	struct nfs4_layoutcommit_data *data = calldata;
10021 
10022 	pnfs_cleanup_layoutcommit(data);
10023 	nfs_post_op_update_inode_force_wcc(data->args.inode,
10024 					   data->res.fattr);
10025 	put_cred(data->cred);
10026 	nfs_iput_and_deactive(data->inode);
10027 	kfree(data);
10028 }
10029 
10030 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
10031 	.rpc_call_prepare = nfs4_layoutcommit_prepare,
10032 	.rpc_call_done = nfs4_layoutcommit_done,
10033 	.rpc_release = nfs4_layoutcommit_release,
10034 };
10035 
10036 int
10037 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
10038 {
10039 	struct rpc_message msg = {
10040 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
10041 		.rpc_argp = &data->args,
10042 		.rpc_resp = &data->res,
10043 		.rpc_cred = data->cred,
10044 	};
10045 	struct rpc_task_setup task_setup_data = {
10046 		.task = &data->task,
10047 		.rpc_client = NFS_CLIENT(data->args.inode),
10048 		.rpc_message = &msg,
10049 		.callback_ops = &nfs4_layoutcommit_ops,
10050 		.callback_data = data,
10051 		.flags = RPC_TASK_MOVEABLE,
10052 	};
10053 	struct rpc_task *task;
10054 	int status = 0;
10055 
10056 	dprintk("NFS: initiating layoutcommit call. sync %d "
10057 		"lbw: %llu inode %lu\n", sync,
10058 		data->args.lastbytewritten,
10059 		data->args.inode->i_ino);
10060 
10061 	if (!sync) {
10062 		data->inode = nfs_igrab_and_active(data->args.inode);
10063 		if (data->inode == NULL) {
10064 			nfs4_layoutcommit_release(data);
10065 			return -EAGAIN;
10066 		}
10067 		task_setup_data.flags = RPC_TASK_ASYNC;
10068 	}
10069 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
10070 	task = rpc_run_task(&task_setup_data);
10071 	if (IS_ERR(task))
10072 		return PTR_ERR(task);
10073 	if (sync)
10074 		status = task->tk_status;
10075 	trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
10076 	dprintk("%s: status %d\n", __func__, status);
10077 	rpc_put_task(task);
10078 	return status;
10079 }
10080 
10081 /*
10082  * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
10083  * possible) as per RFC3530bis and RFC5661 Security Considerations sections
10084  */
10085 static int
10086 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10087 		    struct nfs_fsinfo *info,
10088 		    struct nfs4_secinfo_flavors *flavors, bool use_integrity)
10089 {
10090 	struct nfs41_secinfo_no_name_args args = {
10091 		.style = SECINFO_STYLE_CURRENT_FH,
10092 	};
10093 	struct nfs4_secinfo_res res = {
10094 		.flavors = flavors,
10095 	};
10096 	struct rpc_message msg = {
10097 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
10098 		.rpc_argp = &args,
10099 		.rpc_resp = &res,
10100 	};
10101 	struct nfs4_call_sync_data data = {
10102 		.seq_server = server,
10103 		.seq_args = &args.seq_args,
10104 		.seq_res = &res.seq_res,
10105 	};
10106 	struct rpc_task_setup task_setup = {
10107 		.rpc_client = server->client,
10108 		.rpc_message = &msg,
10109 		.callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
10110 		.callback_data = &data,
10111 		.flags = RPC_TASK_NO_ROUND_ROBIN,
10112 	};
10113 	const struct cred *cred = NULL;
10114 	int status;
10115 
10116 	if (use_integrity) {
10117 		task_setup.rpc_client = server->nfs_client->cl_rpcclient;
10118 
10119 		cred = nfs4_get_clid_cred(server->nfs_client);
10120 		msg.rpc_cred = cred;
10121 	}
10122 
10123 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
10124 	status = nfs4_call_sync_custom(&task_setup);
10125 	dprintk("<-- %s status=%d\n", __func__, status);
10126 
10127 	put_cred(cred);
10128 
10129 	return status;
10130 }
10131 
10132 static int
10133 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10134 			   struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
10135 {
10136 	struct nfs4_exception exception = {
10137 		.interruptible = true,
10138 	};
10139 	int err;
10140 	do {
10141 		/* first try using integrity protection */
10142 		err = -NFS4ERR_WRONGSEC;
10143 
10144 		/* try to use integrity protection with machine cred */
10145 		if (_nfs4_is_integrity_protected(server->nfs_client))
10146 			err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10147 							  flavors, true);
10148 
10149 		/*
10150 		 * if unable to use integrity protection, or SECINFO with
10151 		 * integrity protection returns NFS4ERR_WRONGSEC (which is
10152 		 * disallowed by spec, but exists in deployed servers) use
10153 		 * the current filesystem's rpc_client and the user cred.
10154 		 */
10155 		if (err == -NFS4ERR_WRONGSEC)
10156 			err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10157 							  flavors, false);
10158 
10159 		switch (err) {
10160 		case 0:
10161 		case -NFS4ERR_WRONGSEC:
10162 		case -ENOTSUPP:
10163 			goto out;
10164 		default:
10165 			err = nfs4_handle_exception(server, err, &exception);
10166 		}
10167 	} while (exception.retry);
10168 out:
10169 	return err;
10170 }
10171 
10172 static int
10173 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
10174 		    struct nfs_fsinfo *info)
10175 {
10176 	int err;
10177 	struct page *page;
10178 	rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
10179 	struct nfs4_secinfo_flavors *flavors;
10180 	struct nfs4_secinfo4 *secinfo;
10181 	int i;
10182 
10183 	page = alloc_page(GFP_KERNEL);
10184 	if (!page) {
10185 		err = -ENOMEM;
10186 		goto out;
10187 	}
10188 
10189 	flavors = page_address(page);
10190 	err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
10191 
10192 	/*
10193 	 * Fall back on "guess and check" method if
10194 	 * the server doesn't support SECINFO_NO_NAME
10195 	 */
10196 	if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
10197 		err = nfs4_find_root_sec(server, fhandle, info);
10198 		goto out_freepage;
10199 	}
10200 	if (err)
10201 		goto out_freepage;
10202 
10203 	for (i = 0; i < flavors->num_flavors; i++) {
10204 		secinfo = &flavors->flavors[i];
10205 
10206 		switch (secinfo->flavor) {
10207 		case RPC_AUTH_NULL:
10208 		case RPC_AUTH_UNIX:
10209 		case RPC_AUTH_GSS:
10210 			flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
10211 					&secinfo->flavor_info);
10212 			break;
10213 		default:
10214 			flavor = RPC_AUTH_MAXFLAVOR;
10215 			break;
10216 		}
10217 
10218 		if (!nfs_auth_info_match(&server->auth_info, flavor))
10219 			flavor = RPC_AUTH_MAXFLAVOR;
10220 
10221 		if (flavor != RPC_AUTH_MAXFLAVOR) {
10222 			err = nfs4_lookup_root_sec(server, fhandle,
10223 						   info, flavor);
10224 			if (!err)
10225 				break;
10226 		}
10227 	}
10228 
10229 	if (flavor == RPC_AUTH_MAXFLAVOR)
10230 		err = -EPERM;
10231 
10232 out_freepage:
10233 	put_page(page);
10234 	if (err == -EACCES)
10235 		return -EPERM;
10236 out:
10237 	return err;
10238 }
10239 
10240 static int _nfs41_test_stateid(struct nfs_server *server,
10241 		nfs4_stateid *stateid,
10242 		const struct cred *cred)
10243 {
10244 	int status;
10245 	struct nfs41_test_stateid_args args = {
10246 		.stateid = stateid,
10247 	};
10248 	struct nfs41_test_stateid_res res;
10249 	struct rpc_message msg = {
10250 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
10251 		.rpc_argp = &args,
10252 		.rpc_resp = &res,
10253 		.rpc_cred = cred,
10254 	};
10255 	struct rpc_clnt *rpc_client = server->client;
10256 
10257 	nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10258 		&rpc_client, &msg);
10259 
10260 	dprintk("NFS call  test_stateid %p\n", stateid);
10261 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
10262 	status = nfs4_call_sync_sequence(rpc_client, server, &msg,
10263 			&args.seq_args, &res.seq_res);
10264 	if (status != NFS_OK) {
10265 		dprintk("NFS reply test_stateid: failed, %d\n", status);
10266 		return status;
10267 	}
10268 	dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
10269 	return -res.status;
10270 }
10271 
10272 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10273 		int err, struct nfs4_exception *exception)
10274 {
10275 	exception->retry = 0;
10276 	switch(err) {
10277 	case -NFS4ERR_DELAY:
10278 	case -NFS4ERR_RETRY_UNCACHED_REP:
10279 		nfs4_handle_exception(server, err, exception);
10280 		break;
10281 	case -NFS4ERR_BADSESSION:
10282 	case -NFS4ERR_BADSLOT:
10283 	case -NFS4ERR_BAD_HIGH_SLOT:
10284 	case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10285 	case -NFS4ERR_DEADSESSION:
10286 		nfs4_do_handle_exception(server, err, exception);
10287 	}
10288 }
10289 
10290 /**
10291  * nfs41_test_stateid - perform a TEST_STATEID operation
10292  *
10293  * @server: server / transport on which to perform the operation
10294  * @stateid: state ID to test
10295  * @cred: credential
10296  *
10297  * Returns NFS_OK if the server recognizes that "stateid" is valid.
10298  * Otherwise a negative NFS4ERR value is returned if the operation
10299  * failed or the state ID is not currently valid.
10300  */
10301 static int nfs41_test_stateid(struct nfs_server *server,
10302 		nfs4_stateid *stateid,
10303 		const struct cred *cred)
10304 {
10305 	struct nfs4_exception exception = {
10306 		.interruptible = true,
10307 	};
10308 	int err;
10309 	do {
10310 		err = _nfs41_test_stateid(server, stateid, cred);
10311 		nfs4_handle_delay_or_session_error(server, err, &exception);
10312 	} while (exception.retry);
10313 	return err;
10314 }
10315 
10316 struct nfs_free_stateid_data {
10317 	struct nfs_server *server;
10318 	struct nfs41_free_stateid_args args;
10319 	struct nfs41_free_stateid_res res;
10320 };
10321 
10322 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10323 {
10324 	struct nfs_free_stateid_data *data = calldata;
10325 	nfs4_setup_sequence(data->server->nfs_client,
10326 			&data->args.seq_args,
10327 			&data->res.seq_res,
10328 			task);
10329 }
10330 
10331 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10332 {
10333 	struct nfs_free_stateid_data *data = calldata;
10334 
10335 	nfs41_sequence_done(task, &data->res.seq_res);
10336 
10337 	switch (task->tk_status) {
10338 	case -NFS4ERR_DELAY:
10339 		if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
10340 			rpc_restart_call_prepare(task);
10341 	}
10342 }
10343 
10344 static void nfs41_free_stateid_release(void *calldata)
10345 {
10346 	struct nfs_free_stateid_data *data = calldata;
10347 	struct nfs_client *clp = data->server->nfs_client;
10348 
10349 	nfs_put_client(clp);
10350 	kfree(calldata);
10351 }
10352 
10353 static const struct rpc_call_ops nfs41_free_stateid_ops = {
10354 	.rpc_call_prepare = nfs41_free_stateid_prepare,
10355 	.rpc_call_done = nfs41_free_stateid_done,
10356 	.rpc_release = nfs41_free_stateid_release,
10357 };
10358 
10359 /**
10360  * nfs41_free_stateid - perform a FREE_STATEID operation
10361  *
10362  * @server: server / transport on which to perform the operation
10363  * @stateid: state ID to release
10364  * @cred: credential
10365  * @privileged: set to true if this call needs to be privileged
10366  *
10367  * Note: this function is always asynchronous.
10368  */
10369 static int nfs41_free_stateid(struct nfs_server *server,
10370 		const nfs4_stateid *stateid,
10371 		const struct cred *cred,
10372 		bool privileged)
10373 {
10374 	struct rpc_message msg = {
10375 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
10376 		.rpc_cred = cred,
10377 	};
10378 	struct rpc_task_setup task_setup = {
10379 		.rpc_client = server->client,
10380 		.rpc_message = &msg,
10381 		.callback_ops = &nfs41_free_stateid_ops,
10382 		.flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE,
10383 	};
10384 	struct nfs_free_stateid_data *data;
10385 	struct rpc_task *task;
10386 	struct nfs_client *clp = server->nfs_client;
10387 
10388 	if (!refcount_inc_not_zero(&clp->cl_count))
10389 		return -EIO;
10390 
10391 	nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10392 		&task_setup.rpc_client, &msg);
10393 
10394 	dprintk("NFS call  free_stateid %p\n", stateid);
10395 	data = kmalloc(sizeof(*data), GFP_KERNEL);
10396 	if (!data)
10397 		return -ENOMEM;
10398 	data->server = server;
10399 	nfs4_stateid_copy(&data->args.stateid, stateid);
10400 
10401 	task_setup.callback_data = data;
10402 
10403 	msg.rpc_argp = &data->args;
10404 	msg.rpc_resp = &data->res;
10405 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
10406 	task = rpc_run_task(&task_setup);
10407 	if (IS_ERR(task))
10408 		return PTR_ERR(task);
10409 	rpc_put_task(task);
10410 	return 0;
10411 }
10412 
10413 static void
10414 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
10415 {
10416 	const struct cred *cred = lsp->ls_state->owner->so_cred;
10417 
10418 	nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10419 	nfs4_free_lock_state(server, lsp);
10420 }
10421 
10422 static bool nfs41_match_stateid(const nfs4_stateid *s1,
10423 		const nfs4_stateid *s2)
10424 {
10425 	if (s1->type != s2->type)
10426 		return false;
10427 
10428 	if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10429 		return false;
10430 
10431 	if (s1->seqid == s2->seqid)
10432 		return true;
10433 
10434 	return s1->seqid == 0 || s2->seqid == 0;
10435 }
10436 
10437 #endif /* CONFIG_NFS_V4_1 */
10438 
10439 static bool nfs4_match_stateid(const nfs4_stateid *s1,
10440 		const nfs4_stateid *s2)
10441 {
10442 	return nfs4_stateid_match(s1, s2);
10443 }
10444 
10445 
10446 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10447 	.owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10448 	.state_flag_bit	= NFS_STATE_RECLAIM_REBOOT,
10449 	.recover_open	= nfs4_open_reclaim,
10450 	.recover_lock	= nfs4_lock_reclaim,
10451 	.establish_clid = nfs4_init_clientid,
10452 	.detect_trunking = nfs40_discover_server_trunking,
10453 };
10454 
10455 #if defined(CONFIG_NFS_V4_1)
10456 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10457 	.owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10458 	.state_flag_bit	= NFS_STATE_RECLAIM_REBOOT,
10459 	.recover_open	= nfs4_open_reclaim,
10460 	.recover_lock	= nfs4_lock_reclaim,
10461 	.establish_clid = nfs41_init_clientid,
10462 	.reclaim_complete = nfs41_proc_reclaim_complete,
10463 	.detect_trunking = nfs41_discover_server_trunking,
10464 };
10465 #endif /* CONFIG_NFS_V4_1 */
10466 
10467 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10468 	.owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10469 	.state_flag_bit	= NFS_STATE_RECLAIM_NOGRACE,
10470 	.recover_open	= nfs40_open_expired,
10471 	.recover_lock	= nfs4_lock_expired,
10472 	.establish_clid = nfs4_init_clientid,
10473 };
10474 
10475 #if defined(CONFIG_NFS_V4_1)
10476 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10477 	.owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10478 	.state_flag_bit	= NFS_STATE_RECLAIM_NOGRACE,
10479 	.recover_open	= nfs41_open_expired,
10480 	.recover_lock	= nfs41_lock_expired,
10481 	.establish_clid = nfs41_init_clientid,
10482 };
10483 #endif /* CONFIG_NFS_V4_1 */
10484 
10485 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10486 	.sched_state_renewal = nfs4_proc_async_renew,
10487 	.get_state_renewal_cred = nfs4_get_renew_cred,
10488 	.renew_lease = nfs4_proc_renew,
10489 };
10490 
10491 #if defined(CONFIG_NFS_V4_1)
10492 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10493 	.sched_state_renewal = nfs41_proc_async_sequence,
10494 	.get_state_renewal_cred = nfs4_get_machine_cred,
10495 	.renew_lease = nfs4_proc_sequence,
10496 };
10497 #endif
10498 
10499 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10500 	.get_locations = _nfs40_proc_get_locations,
10501 	.fsid_present = _nfs40_proc_fsid_present,
10502 };
10503 
10504 #if defined(CONFIG_NFS_V4_1)
10505 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10506 	.get_locations = _nfs41_proc_get_locations,
10507 	.fsid_present = _nfs41_proc_fsid_present,
10508 };
10509 #endif	/* CONFIG_NFS_V4_1 */
10510 
10511 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10512 	.minor_version = 0,
10513 	.init_caps = NFS_CAP_READDIRPLUS
10514 		| NFS_CAP_ATOMIC_OPEN
10515 		| NFS_CAP_POSIX_LOCK,
10516 	.init_client = nfs40_init_client,
10517 	.shutdown_client = nfs40_shutdown_client,
10518 	.match_stateid = nfs4_match_stateid,
10519 	.find_root_sec = nfs4_find_root_sec,
10520 	.free_lock_state = nfs4_release_lockowner,
10521 	.test_and_free_expired = nfs40_test_and_free_expired_stateid,
10522 	.alloc_seqid = nfs_alloc_seqid,
10523 	.call_sync_ops = &nfs40_call_sync_ops,
10524 	.reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10525 	.nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10526 	.state_renewal_ops = &nfs40_state_renewal_ops,
10527 	.mig_recovery_ops = &nfs40_mig_recovery_ops,
10528 };
10529 
10530 #if defined(CONFIG_NFS_V4_1)
10531 static struct nfs_seqid *
10532 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10533 {
10534 	return NULL;
10535 }
10536 
10537 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10538 	.minor_version = 1,
10539 	.init_caps = NFS_CAP_READDIRPLUS
10540 		| NFS_CAP_ATOMIC_OPEN
10541 		| NFS_CAP_POSIX_LOCK
10542 		| NFS_CAP_STATEID_NFSV41
10543 		| NFS_CAP_ATOMIC_OPEN_V1
10544 		| NFS_CAP_LGOPEN
10545 		| NFS_CAP_MOVEABLE,
10546 	.init_client = nfs41_init_client,
10547 	.shutdown_client = nfs41_shutdown_client,
10548 	.match_stateid = nfs41_match_stateid,
10549 	.find_root_sec = nfs41_find_root_sec,
10550 	.free_lock_state = nfs41_free_lock_state,
10551 	.test_and_free_expired = nfs41_test_and_free_expired_stateid,
10552 	.alloc_seqid = nfs_alloc_no_seqid,
10553 	.session_trunk = nfs4_test_session_trunk,
10554 	.call_sync_ops = &nfs41_call_sync_ops,
10555 	.reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10556 	.nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10557 	.state_renewal_ops = &nfs41_state_renewal_ops,
10558 	.mig_recovery_ops = &nfs41_mig_recovery_ops,
10559 };
10560 #endif
10561 
10562 #if defined(CONFIG_NFS_V4_2)
10563 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10564 	.minor_version = 2,
10565 	.init_caps = NFS_CAP_READDIRPLUS
10566 		| NFS_CAP_ATOMIC_OPEN
10567 		| NFS_CAP_POSIX_LOCK
10568 		| NFS_CAP_STATEID_NFSV41
10569 		| NFS_CAP_ATOMIC_OPEN_V1
10570 		| NFS_CAP_LGOPEN
10571 		| NFS_CAP_ALLOCATE
10572 		| NFS_CAP_COPY
10573 		| NFS_CAP_OFFLOAD_CANCEL
10574 		| NFS_CAP_COPY_NOTIFY
10575 		| NFS_CAP_DEALLOCATE
10576 		| NFS_CAP_SEEK
10577 		| NFS_CAP_LAYOUTSTATS
10578 		| NFS_CAP_CLONE
10579 		| NFS_CAP_LAYOUTERROR
10580 		| NFS_CAP_READ_PLUS
10581 		| NFS_CAP_MOVEABLE,
10582 	.init_client = nfs41_init_client,
10583 	.shutdown_client = nfs41_shutdown_client,
10584 	.match_stateid = nfs41_match_stateid,
10585 	.find_root_sec = nfs41_find_root_sec,
10586 	.free_lock_state = nfs41_free_lock_state,
10587 	.call_sync_ops = &nfs41_call_sync_ops,
10588 	.test_and_free_expired = nfs41_test_and_free_expired_stateid,
10589 	.alloc_seqid = nfs_alloc_no_seqid,
10590 	.session_trunk = nfs4_test_session_trunk,
10591 	.reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10592 	.nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10593 	.state_renewal_ops = &nfs41_state_renewal_ops,
10594 	.mig_recovery_ops = &nfs41_mig_recovery_ops,
10595 };
10596 #endif
10597 
10598 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10599 	[0] = &nfs_v4_0_minor_ops,
10600 #if defined(CONFIG_NFS_V4_1)
10601 	[1] = &nfs_v4_1_minor_ops,
10602 #endif
10603 #if defined(CONFIG_NFS_V4_2)
10604 	[2] = &nfs_v4_2_minor_ops,
10605 #endif
10606 };
10607 
10608 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10609 {
10610 	ssize_t error, error2, error3;
10611 	size_t left = size;
10612 
10613 	error = generic_listxattr(dentry, list, left);
10614 	if (error < 0)
10615 		return error;
10616 	if (list) {
10617 		list += error;
10618 		left -= error;
10619 	}
10620 
10621 	error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, left);
10622 	if (error2 < 0)
10623 		return error2;
10624 
10625 	if (list) {
10626 		list += error2;
10627 		left -= error2;
10628 	}
10629 
10630 	error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, left);
10631 	if (error3 < 0)
10632 		return error3;
10633 
10634 	error += error2 + error3;
10635 	if (size && error > size)
10636 		return -ERANGE;
10637 	return error;
10638 }
10639 
10640 static void nfs4_enable_swap(struct inode *inode)
10641 {
10642 	/* The state manager thread must always be running.
10643 	 * It will notice the client is a swapper, and stay put.
10644 	 */
10645 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10646 
10647 	nfs4_schedule_state_manager(clp);
10648 }
10649 
10650 static void nfs4_disable_swap(struct inode *inode)
10651 {
10652 	/* The state manager thread will now exit once it is
10653 	 * woken.
10654 	 */
10655 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10656 
10657 	set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
10658 	clear_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state);
10659 	wake_up_var(&clp->cl_state);
10660 }
10661 
10662 static const struct inode_operations nfs4_dir_inode_operations = {
10663 	.create		= nfs_create,
10664 	.lookup		= nfs_lookup,
10665 	.atomic_open	= nfs_atomic_open,
10666 	.link		= nfs_link,
10667 	.unlink		= nfs_unlink,
10668 	.symlink	= nfs_symlink,
10669 	.mkdir		= nfs_mkdir,
10670 	.rmdir		= nfs_rmdir,
10671 	.mknod		= nfs_mknod,
10672 	.rename		= nfs_rename,
10673 	.permission	= nfs_permission,
10674 	.getattr	= nfs_getattr,
10675 	.setattr	= nfs_setattr,
10676 	.listxattr	= nfs4_listxattr,
10677 };
10678 
10679 static const struct inode_operations nfs4_file_inode_operations = {
10680 	.permission	= nfs_permission,
10681 	.getattr	= nfs_getattr,
10682 	.setattr	= nfs_setattr,
10683 	.listxattr	= nfs4_listxattr,
10684 };
10685 
10686 const struct nfs_rpc_ops nfs_v4_clientops = {
10687 	.version	= 4,			/* protocol version */
10688 	.dentry_ops	= &nfs4_dentry_operations,
10689 	.dir_inode_ops	= &nfs4_dir_inode_operations,
10690 	.file_inode_ops	= &nfs4_file_inode_operations,
10691 	.file_ops	= &nfs4_file_operations,
10692 	.getroot	= nfs4_proc_get_root,
10693 	.submount	= nfs4_submount,
10694 	.try_get_tree	= nfs4_try_get_tree,
10695 	.getattr	= nfs4_proc_getattr,
10696 	.setattr	= nfs4_proc_setattr,
10697 	.lookup		= nfs4_proc_lookup,
10698 	.lookupp	= nfs4_proc_lookupp,
10699 	.access		= nfs4_proc_access,
10700 	.readlink	= nfs4_proc_readlink,
10701 	.create		= nfs4_proc_create,
10702 	.remove		= nfs4_proc_remove,
10703 	.unlink_setup	= nfs4_proc_unlink_setup,
10704 	.unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10705 	.unlink_done	= nfs4_proc_unlink_done,
10706 	.rename_setup	= nfs4_proc_rename_setup,
10707 	.rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10708 	.rename_done	= nfs4_proc_rename_done,
10709 	.link		= nfs4_proc_link,
10710 	.symlink	= nfs4_proc_symlink,
10711 	.mkdir		= nfs4_proc_mkdir,
10712 	.rmdir		= nfs4_proc_rmdir,
10713 	.readdir	= nfs4_proc_readdir,
10714 	.mknod		= nfs4_proc_mknod,
10715 	.statfs		= nfs4_proc_statfs,
10716 	.fsinfo		= nfs4_proc_fsinfo,
10717 	.pathconf	= nfs4_proc_pathconf,
10718 	.set_capabilities = nfs4_server_capabilities,
10719 	.decode_dirent	= nfs4_decode_dirent,
10720 	.pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10721 	.read_setup	= nfs4_proc_read_setup,
10722 	.read_done	= nfs4_read_done,
10723 	.write_setup	= nfs4_proc_write_setup,
10724 	.write_done	= nfs4_write_done,
10725 	.commit_setup	= nfs4_proc_commit_setup,
10726 	.commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10727 	.commit_done	= nfs4_commit_done,
10728 	.lock		= nfs4_proc_lock,
10729 	.clear_acl_cache = nfs4_zap_acl_attr,
10730 	.close_context  = nfs4_close_context,
10731 	.open_context	= nfs4_atomic_open,
10732 	.have_delegation = nfs4_have_delegation,
10733 	.alloc_client	= nfs4_alloc_client,
10734 	.init_client	= nfs4_init_client,
10735 	.free_client	= nfs4_free_client,
10736 	.create_server	= nfs4_create_server,
10737 	.clone_server	= nfs_clone_server,
10738 	.discover_trunking = nfs4_discover_trunking,
10739 	.enable_swap	= nfs4_enable_swap,
10740 	.disable_swap	= nfs4_disable_swap,
10741 };
10742 
10743 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10744 	.name	= XATTR_NAME_NFSV4_ACL,
10745 	.list	= nfs4_xattr_list_nfs4_acl,
10746 	.get	= nfs4_xattr_get_nfs4_acl,
10747 	.set	= nfs4_xattr_set_nfs4_acl,
10748 };
10749 
10750 #if defined(CONFIG_NFS_V4_1)
10751 static const struct xattr_handler nfs4_xattr_nfs4_dacl_handler = {
10752 	.name	= XATTR_NAME_NFSV4_DACL,
10753 	.list	= nfs4_xattr_list_nfs4_dacl,
10754 	.get	= nfs4_xattr_get_nfs4_dacl,
10755 	.set	= nfs4_xattr_set_nfs4_dacl,
10756 };
10757 
10758 static const struct xattr_handler nfs4_xattr_nfs4_sacl_handler = {
10759 	.name	= XATTR_NAME_NFSV4_SACL,
10760 	.list	= nfs4_xattr_list_nfs4_sacl,
10761 	.get	= nfs4_xattr_get_nfs4_sacl,
10762 	.set	= nfs4_xattr_set_nfs4_sacl,
10763 };
10764 #endif
10765 
10766 #ifdef CONFIG_NFS_V4_2
10767 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10768 	.prefix	= XATTR_USER_PREFIX,
10769 	.get	= nfs4_xattr_get_nfs4_user,
10770 	.set	= nfs4_xattr_set_nfs4_user,
10771 };
10772 #endif
10773 
10774 const struct xattr_handler *nfs4_xattr_handlers[] = {
10775 	&nfs4_xattr_nfs4_acl_handler,
10776 #if defined(CONFIG_NFS_V4_1)
10777 	&nfs4_xattr_nfs4_dacl_handler,
10778 	&nfs4_xattr_nfs4_sacl_handler,
10779 #endif
10780 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10781 	&nfs4_xattr_nfs4_label_handler,
10782 #endif
10783 #ifdef CONFIG_NFS_V4_2
10784 	&nfs4_xattr_nfs4_user_handler,
10785 #endif
10786 	NULL
10787 };
10788