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