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