xref: /openbmc/linux/fs/nfs/nfs4proc.c (revision 4a0a1436)
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 				NFS_INO_REVAL_PAGECACHE;
1234 		nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1235 	}
1236 	nfsi->attrtimeo_timestamp = jiffies;
1237 	nfsi->read_cache_jiffies = timestamp;
1238 	nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1239 	nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1240 out:
1241 	nfs_set_cache_invalid(inode, cache_validity);
1242 }
1243 
1244 void
1245 nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1246 		unsigned long timestamp, unsigned long cache_validity)
1247 {
1248 	spin_lock(&dir->i_lock);
1249 	nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1250 	spin_unlock(&dir->i_lock);
1251 }
1252 
1253 struct nfs4_open_createattrs {
1254 	struct nfs4_label *label;
1255 	struct iattr *sattr;
1256 	const __u32 verf[2];
1257 };
1258 
1259 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1260 		int err, struct nfs4_exception *exception)
1261 {
1262 	if (err != -EINVAL)
1263 		return false;
1264 	if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1265 		return false;
1266 	server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1267 	exception->retry = 1;
1268 	return true;
1269 }
1270 
1271 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1272 {
1273 	 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1274 }
1275 
1276 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1277 {
1278 	fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1279 
1280 	return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1281 }
1282 
1283 static u32
1284 nfs4_map_atomic_open_share(struct nfs_server *server,
1285 		fmode_t fmode, int openflags)
1286 {
1287 	u32 res = 0;
1288 
1289 	switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1290 	case FMODE_READ:
1291 		res = NFS4_SHARE_ACCESS_READ;
1292 		break;
1293 	case FMODE_WRITE:
1294 		res = NFS4_SHARE_ACCESS_WRITE;
1295 		break;
1296 	case FMODE_READ|FMODE_WRITE:
1297 		res = NFS4_SHARE_ACCESS_BOTH;
1298 	}
1299 	if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1300 		goto out;
1301 	/* Want no delegation if we're using O_DIRECT */
1302 	if (openflags & O_DIRECT)
1303 		res |= NFS4_SHARE_WANT_NO_DELEG;
1304 out:
1305 	return res;
1306 }
1307 
1308 static enum open_claim_type4
1309 nfs4_map_atomic_open_claim(struct nfs_server *server,
1310 		enum open_claim_type4 claim)
1311 {
1312 	if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1313 		return claim;
1314 	switch (claim) {
1315 	default:
1316 		return claim;
1317 	case NFS4_OPEN_CLAIM_FH:
1318 		return NFS4_OPEN_CLAIM_NULL;
1319 	case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1320 		return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1321 	case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1322 		return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1323 	}
1324 }
1325 
1326 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1327 {
1328 	p->o_res.f_attr = &p->f_attr;
1329 	p->o_res.seqid = p->o_arg.seqid;
1330 	p->c_res.seqid = p->c_arg.seqid;
1331 	p->o_res.server = p->o_arg.server;
1332 	p->o_res.access_request = p->o_arg.access;
1333 	nfs_fattr_init(&p->f_attr);
1334 	nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1335 }
1336 
1337 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1338 		struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1339 		const struct nfs4_open_createattrs *c,
1340 		enum open_claim_type4 claim,
1341 		gfp_t gfp_mask)
1342 {
1343 	struct dentry *parent = dget_parent(dentry);
1344 	struct inode *dir = d_inode(parent);
1345 	struct nfs_server *server = NFS_SERVER(dir);
1346 	struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1347 	struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1348 	struct nfs4_opendata *p;
1349 
1350 	p = kzalloc(sizeof(*p), gfp_mask);
1351 	if (p == NULL)
1352 		goto err;
1353 
1354 	p->f_attr.label = nfs4_label_alloc(server, gfp_mask);
1355 	if (IS_ERR(p->f_attr.label))
1356 		goto err_free_p;
1357 
1358 	p->a_label = nfs4_label_alloc(server, gfp_mask);
1359 	if (IS_ERR(p->a_label))
1360 		goto err_free_f;
1361 
1362 	alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1363 	p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1364 	if (IS_ERR(p->o_arg.seqid))
1365 		goto err_free_label;
1366 	nfs_sb_active(dentry->d_sb);
1367 	p->dentry = dget(dentry);
1368 	p->dir = parent;
1369 	p->owner = sp;
1370 	atomic_inc(&sp->so_count);
1371 	p->o_arg.open_flags = flags;
1372 	p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1373 	p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1374 	p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1375 			fmode, flags);
1376 	if (flags & O_CREAT) {
1377 		p->o_arg.umask = current_umask();
1378 		p->o_arg.label = nfs4_label_copy(p->a_label, label);
1379 		if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1380 			p->o_arg.u.attrs = &p->attrs;
1381 			memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1382 
1383 			memcpy(p->o_arg.u.verifier.data, c->verf,
1384 					sizeof(p->o_arg.u.verifier.data));
1385 		}
1386 	}
1387 	/* ask server to check for all possible rights as results
1388 	 * are cached */
1389 	switch (p->o_arg.claim) {
1390 	default:
1391 		break;
1392 	case NFS4_OPEN_CLAIM_NULL:
1393 	case NFS4_OPEN_CLAIM_FH:
1394 		p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1395 				  NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE |
1396 				  NFS4_ACCESS_EXECUTE;
1397 #ifdef CONFIG_NFS_V4_2
1398 		if (!(server->caps & NFS_CAP_XATTR))
1399 			break;
1400 		p->o_arg.access |= NFS4_ACCESS_XAREAD | NFS4_ACCESS_XAWRITE |
1401 				   NFS4_ACCESS_XALIST;
1402 #endif
1403 	}
1404 	p->o_arg.clientid = server->nfs_client->cl_clientid;
1405 	p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1406 	p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1407 	p->o_arg.name = &dentry->d_name;
1408 	p->o_arg.server = server;
1409 	p->o_arg.bitmask = nfs4_bitmask(server, label);
1410 	p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1411 	switch (p->o_arg.claim) {
1412 	case NFS4_OPEN_CLAIM_NULL:
1413 	case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1414 	case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1415 		p->o_arg.fh = NFS_FH(dir);
1416 		break;
1417 	case NFS4_OPEN_CLAIM_PREVIOUS:
1418 	case NFS4_OPEN_CLAIM_FH:
1419 	case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1420 	case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1421 		p->o_arg.fh = NFS_FH(d_inode(dentry));
1422 	}
1423 	p->c_arg.fh = &p->o_res.fh;
1424 	p->c_arg.stateid = &p->o_res.stateid;
1425 	p->c_arg.seqid = p->o_arg.seqid;
1426 	nfs4_init_opendata_res(p);
1427 	kref_init(&p->kref);
1428 	return p;
1429 
1430 err_free_label:
1431 	nfs4_label_free(p->a_label);
1432 err_free_f:
1433 	nfs4_label_free(p->f_attr.label);
1434 err_free_p:
1435 	kfree(p);
1436 err:
1437 	dput(parent);
1438 	return NULL;
1439 }
1440 
1441 static void nfs4_opendata_free(struct kref *kref)
1442 {
1443 	struct nfs4_opendata *p = container_of(kref,
1444 			struct nfs4_opendata, kref);
1445 	struct super_block *sb = p->dentry->d_sb;
1446 
1447 	nfs4_lgopen_release(p->lgp);
1448 	nfs_free_seqid(p->o_arg.seqid);
1449 	nfs4_sequence_free_slot(&p->o_res.seq_res);
1450 	if (p->state != NULL)
1451 		nfs4_put_open_state(p->state);
1452 	nfs4_put_state_owner(p->owner);
1453 
1454 	nfs4_label_free(p->a_label);
1455 	nfs4_label_free(p->f_attr.label);
1456 
1457 	dput(p->dir);
1458 	dput(p->dentry);
1459 	nfs_sb_deactive(sb);
1460 	nfs_fattr_free_names(&p->f_attr);
1461 	kfree(p->f_attr.mdsthreshold);
1462 	kfree(p);
1463 }
1464 
1465 static void nfs4_opendata_put(struct nfs4_opendata *p)
1466 {
1467 	if (p != NULL)
1468 		kref_put(&p->kref, nfs4_opendata_free);
1469 }
1470 
1471 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1472 		fmode_t fmode)
1473 {
1474 	switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1475 	case FMODE_READ|FMODE_WRITE:
1476 		return state->n_rdwr != 0;
1477 	case FMODE_WRITE:
1478 		return state->n_wronly != 0;
1479 	case FMODE_READ:
1480 		return state->n_rdonly != 0;
1481 	}
1482 	WARN_ON_ONCE(1);
1483 	return false;
1484 }
1485 
1486 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1487 		int open_mode, enum open_claim_type4 claim)
1488 {
1489 	int ret = 0;
1490 
1491 	if (open_mode & (O_EXCL|O_TRUNC))
1492 		goto out;
1493 	switch (claim) {
1494 	case NFS4_OPEN_CLAIM_NULL:
1495 	case NFS4_OPEN_CLAIM_FH:
1496 		goto out;
1497 	default:
1498 		break;
1499 	}
1500 	switch (mode & (FMODE_READ|FMODE_WRITE)) {
1501 		case FMODE_READ:
1502 			ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1503 				&& state->n_rdonly != 0;
1504 			break;
1505 		case FMODE_WRITE:
1506 			ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1507 				&& state->n_wronly != 0;
1508 			break;
1509 		case FMODE_READ|FMODE_WRITE:
1510 			ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1511 				&& state->n_rdwr != 0;
1512 	}
1513 out:
1514 	return ret;
1515 }
1516 
1517 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1518 		enum open_claim_type4 claim)
1519 {
1520 	if (delegation == NULL)
1521 		return 0;
1522 	if ((delegation->type & fmode) != fmode)
1523 		return 0;
1524 	switch (claim) {
1525 	case NFS4_OPEN_CLAIM_NULL:
1526 	case NFS4_OPEN_CLAIM_FH:
1527 		break;
1528 	case NFS4_OPEN_CLAIM_PREVIOUS:
1529 		if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1530 			break;
1531 		fallthrough;
1532 	default:
1533 		return 0;
1534 	}
1535 	nfs_mark_delegation_referenced(delegation);
1536 	return 1;
1537 }
1538 
1539 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1540 {
1541 	switch (fmode) {
1542 		case FMODE_WRITE:
1543 			state->n_wronly++;
1544 			break;
1545 		case FMODE_READ:
1546 			state->n_rdonly++;
1547 			break;
1548 		case FMODE_READ|FMODE_WRITE:
1549 			state->n_rdwr++;
1550 	}
1551 	nfs4_state_set_mode_locked(state, state->state | fmode);
1552 }
1553 
1554 #ifdef CONFIG_NFS_V4_1
1555 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1556 {
1557 	if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1558 		return true;
1559 	if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1560 		return true;
1561 	if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1562 		return true;
1563 	return false;
1564 }
1565 #endif /* CONFIG_NFS_V4_1 */
1566 
1567 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1568 {
1569 	if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1570 		wake_up_all(&state->waitq);
1571 }
1572 
1573 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1574 {
1575 	struct nfs_client *clp = state->owner->so_server->nfs_client;
1576 	bool need_recover = false;
1577 
1578 	if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1579 		need_recover = true;
1580 	if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1581 		need_recover = true;
1582 	if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1583 		need_recover = true;
1584 	if (need_recover)
1585 		nfs4_state_mark_reclaim_nograce(clp, state);
1586 }
1587 
1588 /*
1589  * Check for whether or not the caller may update the open stateid
1590  * to the value passed in by stateid.
1591  *
1592  * Note: This function relies heavily on the server implementing
1593  * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1594  * correctly.
1595  * i.e. The stateid seqids have to be initialised to 1, and
1596  * are then incremented on every state transition.
1597  */
1598 static bool nfs_stateid_is_sequential(struct nfs4_state *state,
1599 		const nfs4_stateid *stateid)
1600 {
1601 	if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1602 		/* The common case - we're updating to a new sequence number */
1603 		if (nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1604 			if (nfs4_stateid_is_next(&state->open_stateid, stateid))
1605 				return true;
1606 			return false;
1607 		}
1608 		/* The server returned a new stateid */
1609 	}
1610 	/* This is the first OPEN in this generation */
1611 	if (stateid->seqid == cpu_to_be32(1))
1612 		return true;
1613 	return false;
1614 }
1615 
1616 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1617 {
1618 	if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1619 		return;
1620 	if (state->n_wronly)
1621 		set_bit(NFS_O_WRONLY_STATE, &state->flags);
1622 	if (state->n_rdonly)
1623 		set_bit(NFS_O_RDONLY_STATE, &state->flags);
1624 	if (state->n_rdwr)
1625 		set_bit(NFS_O_RDWR_STATE, &state->flags);
1626 	set_bit(NFS_OPEN_STATE, &state->flags);
1627 }
1628 
1629 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1630 		nfs4_stateid *stateid, fmode_t fmode)
1631 {
1632 	clear_bit(NFS_O_RDWR_STATE, &state->flags);
1633 	switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1634 	case FMODE_WRITE:
1635 		clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1636 		break;
1637 	case FMODE_READ:
1638 		clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1639 		break;
1640 	case 0:
1641 		clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1642 		clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1643 		clear_bit(NFS_OPEN_STATE, &state->flags);
1644 	}
1645 	if (stateid == NULL)
1646 		return;
1647 	/* Handle OPEN+OPEN_DOWNGRADE races */
1648 	if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1649 	    !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1650 		nfs_resync_open_stateid_locked(state);
1651 		goto out;
1652 	}
1653 	if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1654 		nfs4_stateid_copy(&state->stateid, stateid);
1655 	nfs4_stateid_copy(&state->open_stateid, stateid);
1656 	trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1657 out:
1658 	nfs_state_log_update_open_stateid(state);
1659 }
1660 
1661 static void nfs_clear_open_stateid(struct nfs4_state *state,
1662 	nfs4_stateid *arg_stateid,
1663 	nfs4_stateid *stateid, fmode_t fmode)
1664 {
1665 	write_seqlock(&state->seqlock);
1666 	/* Ignore, if the CLOSE argment doesn't match the current stateid */
1667 	if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1668 		nfs_clear_open_stateid_locked(state, stateid, fmode);
1669 	write_sequnlock(&state->seqlock);
1670 	if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1671 		nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1672 }
1673 
1674 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1675 		const nfs4_stateid *stateid, nfs4_stateid *freeme)
1676 	__must_hold(&state->owner->so_lock)
1677 	__must_hold(&state->seqlock)
1678 	__must_hold(RCU)
1679 
1680 {
1681 	DEFINE_WAIT(wait);
1682 	int status = 0;
1683 	for (;;) {
1684 
1685 		if (nfs_stateid_is_sequential(state, stateid))
1686 			break;
1687 
1688 		if (status)
1689 			break;
1690 		/* Rely on seqids for serialisation with NFSv4.0 */
1691 		if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1692 			break;
1693 
1694 		set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1695 		prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1696 		/*
1697 		 * Ensure we process the state changes in the same order
1698 		 * in which the server processed them by delaying the
1699 		 * update of the stateid until we are in sequence.
1700 		 */
1701 		write_sequnlock(&state->seqlock);
1702 		spin_unlock(&state->owner->so_lock);
1703 		rcu_read_unlock();
1704 		trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1705 
1706 		if (!fatal_signal_pending(current)) {
1707 			if (schedule_timeout(5*HZ) == 0)
1708 				status = -EAGAIN;
1709 			else
1710 				status = 0;
1711 		} else
1712 			status = -EINTR;
1713 		finish_wait(&state->waitq, &wait);
1714 		rcu_read_lock();
1715 		spin_lock(&state->owner->so_lock);
1716 		write_seqlock(&state->seqlock);
1717 	}
1718 
1719 	if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1720 	    !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1721 		nfs4_stateid_copy(freeme, &state->open_stateid);
1722 		nfs_test_and_clear_all_open_stateid(state);
1723 	}
1724 
1725 	if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1726 		nfs4_stateid_copy(&state->stateid, stateid);
1727 	nfs4_stateid_copy(&state->open_stateid, stateid);
1728 	trace_nfs4_open_stateid_update(state->inode, stateid, status);
1729 	nfs_state_log_update_open_stateid(state);
1730 }
1731 
1732 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1733 		const nfs4_stateid *open_stateid,
1734 		fmode_t fmode,
1735 		nfs4_stateid *freeme)
1736 {
1737 	/*
1738 	 * Protect the call to nfs4_state_set_mode_locked and
1739 	 * serialise the stateid update
1740 	 */
1741 	write_seqlock(&state->seqlock);
1742 	nfs_set_open_stateid_locked(state, open_stateid, freeme);
1743 	switch (fmode) {
1744 	case FMODE_READ:
1745 		set_bit(NFS_O_RDONLY_STATE, &state->flags);
1746 		break;
1747 	case FMODE_WRITE:
1748 		set_bit(NFS_O_WRONLY_STATE, &state->flags);
1749 		break;
1750 	case FMODE_READ|FMODE_WRITE:
1751 		set_bit(NFS_O_RDWR_STATE, &state->flags);
1752 	}
1753 	set_bit(NFS_OPEN_STATE, &state->flags);
1754 	write_sequnlock(&state->seqlock);
1755 }
1756 
1757 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1758 {
1759 	clear_bit(NFS_O_RDWR_STATE, &state->flags);
1760 	clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1761 	clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1762 	clear_bit(NFS_OPEN_STATE, &state->flags);
1763 }
1764 
1765 static void nfs_state_set_delegation(struct nfs4_state *state,
1766 		const nfs4_stateid *deleg_stateid,
1767 		fmode_t fmode)
1768 {
1769 	/*
1770 	 * Protect the call to nfs4_state_set_mode_locked and
1771 	 * serialise the stateid update
1772 	 */
1773 	write_seqlock(&state->seqlock);
1774 	nfs4_stateid_copy(&state->stateid, deleg_stateid);
1775 	set_bit(NFS_DELEGATED_STATE, &state->flags);
1776 	write_sequnlock(&state->seqlock);
1777 }
1778 
1779 static void nfs_state_clear_delegation(struct nfs4_state *state)
1780 {
1781 	write_seqlock(&state->seqlock);
1782 	nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1783 	clear_bit(NFS_DELEGATED_STATE, &state->flags);
1784 	write_sequnlock(&state->seqlock);
1785 }
1786 
1787 int update_open_stateid(struct nfs4_state *state,
1788 		const nfs4_stateid *open_stateid,
1789 		const nfs4_stateid *delegation,
1790 		fmode_t fmode)
1791 {
1792 	struct nfs_server *server = NFS_SERVER(state->inode);
1793 	struct nfs_client *clp = server->nfs_client;
1794 	struct nfs_inode *nfsi = NFS_I(state->inode);
1795 	struct nfs_delegation *deleg_cur;
1796 	nfs4_stateid freeme = { };
1797 	int ret = 0;
1798 
1799 	fmode &= (FMODE_READ|FMODE_WRITE);
1800 
1801 	rcu_read_lock();
1802 	spin_lock(&state->owner->so_lock);
1803 	if (open_stateid != NULL) {
1804 		nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1805 		ret = 1;
1806 	}
1807 
1808 	deleg_cur = nfs4_get_valid_delegation(state->inode);
1809 	if (deleg_cur == NULL)
1810 		goto no_delegation;
1811 
1812 	spin_lock(&deleg_cur->lock);
1813 	if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1814 	   test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1815 	    (deleg_cur->type & fmode) != fmode)
1816 		goto no_delegation_unlock;
1817 
1818 	if (delegation == NULL)
1819 		delegation = &deleg_cur->stateid;
1820 	else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1821 		goto no_delegation_unlock;
1822 
1823 	nfs_mark_delegation_referenced(deleg_cur);
1824 	nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1825 	ret = 1;
1826 no_delegation_unlock:
1827 	spin_unlock(&deleg_cur->lock);
1828 no_delegation:
1829 	if (ret)
1830 		update_open_stateflags(state, fmode);
1831 	spin_unlock(&state->owner->so_lock);
1832 	rcu_read_unlock();
1833 
1834 	if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1835 		nfs4_schedule_state_manager(clp);
1836 	if (freeme.type != 0)
1837 		nfs4_test_and_free_stateid(server, &freeme,
1838 				state->owner->so_cred);
1839 
1840 	return ret;
1841 }
1842 
1843 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1844 		const nfs4_stateid *stateid)
1845 {
1846 	struct nfs4_state *state = lsp->ls_state;
1847 	bool ret = false;
1848 
1849 	spin_lock(&state->state_lock);
1850 	if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1851 		goto out_noupdate;
1852 	if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1853 		goto out_noupdate;
1854 	nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1855 	ret = true;
1856 out_noupdate:
1857 	spin_unlock(&state->state_lock);
1858 	return ret;
1859 }
1860 
1861 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1862 {
1863 	struct nfs_delegation *delegation;
1864 
1865 	fmode &= FMODE_READ|FMODE_WRITE;
1866 	rcu_read_lock();
1867 	delegation = nfs4_get_valid_delegation(inode);
1868 	if (delegation == NULL || (delegation->type & fmode) == fmode) {
1869 		rcu_read_unlock();
1870 		return;
1871 	}
1872 	rcu_read_unlock();
1873 	nfs4_inode_return_delegation(inode);
1874 }
1875 
1876 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1877 {
1878 	struct nfs4_state *state = opendata->state;
1879 	struct nfs_delegation *delegation;
1880 	int open_mode = opendata->o_arg.open_flags;
1881 	fmode_t fmode = opendata->o_arg.fmode;
1882 	enum open_claim_type4 claim = opendata->o_arg.claim;
1883 	nfs4_stateid stateid;
1884 	int ret = -EAGAIN;
1885 
1886 	for (;;) {
1887 		spin_lock(&state->owner->so_lock);
1888 		if (can_open_cached(state, fmode, open_mode, claim)) {
1889 			update_open_stateflags(state, fmode);
1890 			spin_unlock(&state->owner->so_lock);
1891 			goto out_return_state;
1892 		}
1893 		spin_unlock(&state->owner->so_lock);
1894 		rcu_read_lock();
1895 		delegation = nfs4_get_valid_delegation(state->inode);
1896 		if (!can_open_delegated(delegation, fmode, claim)) {
1897 			rcu_read_unlock();
1898 			break;
1899 		}
1900 		/* Save the delegation */
1901 		nfs4_stateid_copy(&stateid, &delegation->stateid);
1902 		rcu_read_unlock();
1903 		nfs_release_seqid(opendata->o_arg.seqid);
1904 		if (!opendata->is_recover) {
1905 			ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1906 			if (ret != 0)
1907 				goto out;
1908 		}
1909 		ret = -EAGAIN;
1910 
1911 		/* Try to update the stateid using the delegation */
1912 		if (update_open_stateid(state, NULL, &stateid, fmode))
1913 			goto out_return_state;
1914 	}
1915 out:
1916 	return ERR_PTR(ret);
1917 out_return_state:
1918 	refcount_inc(&state->count);
1919 	return state;
1920 }
1921 
1922 static void
1923 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1924 {
1925 	struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1926 	struct nfs_delegation *delegation;
1927 	int delegation_flags = 0;
1928 
1929 	rcu_read_lock();
1930 	delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1931 	if (delegation)
1932 		delegation_flags = delegation->flags;
1933 	rcu_read_unlock();
1934 	switch (data->o_arg.claim) {
1935 	default:
1936 		break;
1937 	case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1938 	case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1939 		pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1940 				   "returning a delegation for "
1941 				   "OPEN(CLAIM_DELEGATE_CUR)\n",
1942 				   clp->cl_hostname);
1943 		return;
1944 	}
1945 	if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1946 		nfs_inode_set_delegation(state->inode,
1947 				data->owner->so_cred,
1948 				data->o_res.delegation_type,
1949 				&data->o_res.delegation,
1950 				data->o_res.pagemod_limit);
1951 	else
1952 		nfs_inode_reclaim_delegation(state->inode,
1953 				data->owner->so_cred,
1954 				data->o_res.delegation_type,
1955 				&data->o_res.delegation,
1956 				data->o_res.pagemod_limit);
1957 
1958 	if (data->o_res.do_recall)
1959 		nfs_async_inode_return_delegation(state->inode,
1960 						  &data->o_res.delegation);
1961 }
1962 
1963 /*
1964  * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1965  * and update the nfs4_state.
1966  */
1967 static struct nfs4_state *
1968 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1969 {
1970 	struct inode *inode = data->state->inode;
1971 	struct nfs4_state *state = data->state;
1972 	int ret;
1973 
1974 	if (!data->rpc_done) {
1975 		if (data->rpc_status)
1976 			return ERR_PTR(data->rpc_status);
1977 		/* cached opens have already been processed */
1978 		goto update;
1979 	}
1980 
1981 	ret = nfs_refresh_inode(inode, &data->f_attr);
1982 	if (ret)
1983 		return ERR_PTR(ret);
1984 
1985 	if (data->o_res.delegation_type != 0)
1986 		nfs4_opendata_check_deleg(data, state);
1987 update:
1988 	if (!update_open_stateid(state, &data->o_res.stateid,
1989 				NULL, data->o_arg.fmode))
1990 		return ERR_PTR(-EAGAIN);
1991 	refcount_inc(&state->count);
1992 
1993 	return state;
1994 }
1995 
1996 static struct inode *
1997 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1998 {
1999 	struct inode *inode;
2000 
2001 	switch (data->o_arg.claim) {
2002 	case NFS4_OPEN_CLAIM_NULL:
2003 	case NFS4_OPEN_CLAIM_DELEGATE_CUR:
2004 	case NFS4_OPEN_CLAIM_DELEGATE_PREV:
2005 		if (!(data->f_attr.valid & NFS_ATTR_FATTR))
2006 			return ERR_PTR(-EAGAIN);
2007 		inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
2008 				&data->f_attr);
2009 		break;
2010 	default:
2011 		inode = d_inode(data->dentry);
2012 		ihold(inode);
2013 		nfs_refresh_inode(inode, &data->f_attr);
2014 	}
2015 	return inode;
2016 }
2017 
2018 static struct nfs4_state *
2019 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2020 {
2021 	struct nfs4_state *state;
2022 	struct inode *inode;
2023 
2024 	inode = nfs4_opendata_get_inode(data);
2025 	if (IS_ERR(inode))
2026 		return ERR_CAST(inode);
2027 	if (data->state != NULL && data->state->inode == inode) {
2028 		state = data->state;
2029 		refcount_inc(&state->count);
2030 	} else
2031 		state = nfs4_get_open_state(inode, data->owner);
2032 	iput(inode);
2033 	if (state == NULL)
2034 		state = ERR_PTR(-ENOMEM);
2035 	return state;
2036 }
2037 
2038 static struct nfs4_state *
2039 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2040 {
2041 	struct nfs4_state *state;
2042 
2043 	if (!data->rpc_done) {
2044 		state = nfs4_try_open_cached(data);
2045 		trace_nfs4_cached_open(data->state);
2046 		goto out;
2047 	}
2048 
2049 	state = nfs4_opendata_find_nfs4_state(data);
2050 	if (IS_ERR(state))
2051 		goto out;
2052 
2053 	if (data->o_res.delegation_type != 0)
2054 		nfs4_opendata_check_deleg(data, state);
2055 	if (!update_open_stateid(state, &data->o_res.stateid,
2056 				NULL, data->o_arg.fmode)) {
2057 		nfs4_put_open_state(state);
2058 		state = ERR_PTR(-EAGAIN);
2059 	}
2060 out:
2061 	nfs_release_seqid(data->o_arg.seqid);
2062 	return state;
2063 }
2064 
2065 static struct nfs4_state *
2066 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2067 {
2068 	struct nfs4_state *ret;
2069 
2070 	if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2071 		ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2072 	else
2073 		ret = _nfs4_opendata_to_nfs4_state(data);
2074 	nfs4_sequence_free_slot(&data->o_res.seq_res);
2075 	return ret;
2076 }
2077 
2078 static struct nfs_open_context *
2079 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2080 {
2081 	struct nfs_inode *nfsi = NFS_I(state->inode);
2082 	struct nfs_open_context *ctx;
2083 
2084 	rcu_read_lock();
2085 	list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2086 		if (ctx->state != state)
2087 			continue;
2088 		if ((ctx->mode & mode) != mode)
2089 			continue;
2090 		if (!get_nfs_open_context(ctx))
2091 			continue;
2092 		rcu_read_unlock();
2093 		return ctx;
2094 	}
2095 	rcu_read_unlock();
2096 	return ERR_PTR(-ENOENT);
2097 }
2098 
2099 static struct nfs_open_context *
2100 nfs4_state_find_open_context(struct nfs4_state *state)
2101 {
2102 	struct nfs_open_context *ctx;
2103 
2104 	ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2105 	if (!IS_ERR(ctx))
2106 		return ctx;
2107 	ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2108 	if (!IS_ERR(ctx))
2109 		return ctx;
2110 	return nfs4_state_find_open_context_mode(state, FMODE_READ);
2111 }
2112 
2113 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2114 		struct nfs4_state *state, enum open_claim_type4 claim)
2115 {
2116 	struct nfs4_opendata *opendata;
2117 
2118 	opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2119 			NULL, claim, GFP_NOFS);
2120 	if (opendata == NULL)
2121 		return ERR_PTR(-ENOMEM);
2122 	opendata->state = state;
2123 	refcount_inc(&state->count);
2124 	return opendata;
2125 }
2126 
2127 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2128 		fmode_t fmode)
2129 {
2130 	struct nfs4_state *newstate;
2131 	int ret;
2132 
2133 	if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2134 		return 0;
2135 	opendata->o_arg.open_flags = 0;
2136 	opendata->o_arg.fmode = fmode;
2137 	opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2138 			NFS_SB(opendata->dentry->d_sb),
2139 			fmode, 0);
2140 	memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2141 	memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2142 	nfs4_init_opendata_res(opendata);
2143 	ret = _nfs4_recover_proc_open(opendata);
2144 	if (ret != 0)
2145 		return ret;
2146 	newstate = nfs4_opendata_to_nfs4_state(opendata);
2147 	if (IS_ERR(newstate))
2148 		return PTR_ERR(newstate);
2149 	if (newstate != opendata->state)
2150 		ret = -ESTALE;
2151 	nfs4_close_state(newstate, fmode);
2152 	return ret;
2153 }
2154 
2155 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2156 {
2157 	int ret;
2158 
2159 	/* memory barrier prior to reading state->n_* */
2160 	smp_rmb();
2161 	ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2162 	if (ret != 0)
2163 		return ret;
2164 	ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2165 	if (ret != 0)
2166 		return ret;
2167 	ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2168 	if (ret != 0)
2169 		return ret;
2170 	/*
2171 	 * We may have performed cached opens for all three recoveries.
2172 	 * Check if we need to update the current stateid.
2173 	 */
2174 	if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2175 	    !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2176 		write_seqlock(&state->seqlock);
2177 		if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2178 			nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2179 		write_sequnlock(&state->seqlock);
2180 	}
2181 	return 0;
2182 }
2183 
2184 /*
2185  * OPEN_RECLAIM:
2186  * 	reclaim state on the server after a reboot.
2187  */
2188 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2189 {
2190 	struct nfs_delegation *delegation;
2191 	struct nfs4_opendata *opendata;
2192 	fmode_t delegation_type = 0;
2193 	int status;
2194 
2195 	opendata = nfs4_open_recoverdata_alloc(ctx, state,
2196 			NFS4_OPEN_CLAIM_PREVIOUS);
2197 	if (IS_ERR(opendata))
2198 		return PTR_ERR(opendata);
2199 	rcu_read_lock();
2200 	delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2201 	if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2202 		delegation_type = delegation->type;
2203 	rcu_read_unlock();
2204 	opendata->o_arg.u.delegation_type = delegation_type;
2205 	status = nfs4_open_recover(opendata, state);
2206 	nfs4_opendata_put(opendata);
2207 	return status;
2208 }
2209 
2210 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2211 {
2212 	struct nfs_server *server = NFS_SERVER(state->inode);
2213 	struct nfs4_exception exception = { };
2214 	int err;
2215 	do {
2216 		err = _nfs4_do_open_reclaim(ctx, state);
2217 		trace_nfs4_open_reclaim(ctx, 0, err);
2218 		if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2219 			continue;
2220 		if (err != -NFS4ERR_DELAY)
2221 			break;
2222 		nfs4_handle_exception(server, err, &exception);
2223 	} while (exception.retry);
2224 	return err;
2225 }
2226 
2227 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2228 {
2229 	struct nfs_open_context *ctx;
2230 	int ret;
2231 
2232 	ctx = nfs4_state_find_open_context(state);
2233 	if (IS_ERR(ctx))
2234 		return -EAGAIN;
2235 	clear_bit(NFS_DELEGATED_STATE, &state->flags);
2236 	nfs_state_clear_open_state_flags(state);
2237 	ret = nfs4_do_open_reclaim(ctx, state);
2238 	put_nfs_open_context(ctx);
2239 	return ret;
2240 }
2241 
2242 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)
2243 {
2244 	switch (err) {
2245 		default:
2246 			printk(KERN_ERR "NFS: %s: unhandled error "
2247 					"%d.\n", __func__, err);
2248 			fallthrough;
2249 		case 0:
2250 		case -ENOENT:
2251 		case -EAGAIN:
2252 		case -ESTALE:
2253 		case -ETIMEDOUT:
2254 			break;
2255 		case -NFS4ERR_BADSESSION:
2256 		case -NFS4ERR_BADSLOT:
2257 		case -NFS4ERR_BAD_HIGH_SLOT:
2258 		case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2259 		case -NFS4ERR_DEADSESSION:
2260 			return -EAGAIN;
2261 		case -NFS4ERR_STALE_CLIENTID:
2262 		case -NFS4ERR_STALE_STATEID:
2263 			/* Don't recall a delegation if it was lost */
2264 			nfs4_schedule_lease_recovery(server->nfs_client);
2265 			return -EAGAIN;
2266 		case -NFS4ERR_MOVED:
2267 			nfs4_schedule_migration_recovery(server);
2268 			return -EAGAIN;
2269 		case -NFS4ERR_LEASE_MOVED:
2270 			nfs4_schedule_lease_moved_recovery(server->nfs_client);
2271 			return -EAGAIN;
2272 		case -NFS4ERR_DELEG_REVOKED:
2273 		case -NFS4ERR_ADMIN_REVOKED:
2274 		case -NFS4ERR_EXPIRED:
2275 		case -NFS4ERR_BAD_STATEID:
2276 		case -NFS4ERR_OPENMODE:
2277 			nfs_inode_find_state_and_recover(state->inode,
2278 					stateid);
2279 			nfs4_schedule_stateid_recovery(server, state);
2280 			return -EAGAIN;
2281 		case -NFS4ERR_DELAY:
2282 		case -NFS4ERR_GRACE:
2283 			ssleep(1);
2284 			return -EAGAIN;
2285 		case -ENOMEM:
2286 		case -NFS4ERR_DENIED:
2287 			if (fl) {
2288 				struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2289 				if (lsp)
2290 					set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2291 			}
2292 			return 0;
2293 	}
2294 	return err;
2295 }
2296 
2297 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2298 		struct nfs4_state *state, const nfs4_stateid *stateid)
2299 {
2300 	struct nfs_server *server = NFS_SERVER(state->inode);
2301 	struct nfs4_opendata *opendata;
2302 	int err = 0;
2303 
2304 	opendata = nfs4_open_recoverdata_alloc(ctx, state,
2305 			NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2306 	if (IS_ERR(opendata))
2307 		return PTR_ERR(opendata);
2308 	nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2309 	if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2310 		err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2311 		if (err)
2312 			goto out;
2313 	}
2314 	if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2315 		err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2316 		if (err)
2317 			goto out;
2318 	}
2319 	if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2320 		err = nfs4_open_recover_helper(opendata, FMODE_READ);
2321 		if (err)
2322 			goto out;
2323 	}
2324 	nfs_state_clear_delegation(state);
2325 out:
2326 	nfs4_opendata_put(opendata);
2327 	return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2328 }
2329 
2330 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2331 {
2332 	struct nfs4_opendata *data = calldata;
2333 
2334 	nfs4_setup_sequence(data->o_arg.server->nfs_client,
2335 			   &data->c_arg.seq_args, &data->c_res.seq_res, task);
2336 }
2337 
2338 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2339 {
2340 	struct nfs4_opendata *data = calldata;
2341 
2342 	nfs40_sequence_done(task, &data->c_res.seq_res);
2343 
2344 	data->rpc_status = task->tk_status;
2345 	if (data->rpc_status == 0) {
2346 		nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2347 		nfs_confirm_seqid(&data->owner->so_seqid, 0);
2348 		renew_lease(data->o_res.server, data->timestamp);
2349 		data->rpc_done = true;
2350 	}
2351 }
2352 
2353 static void nfs4_open_confirm_release(void *calldata)
2354 {
2355 	struct nfs4_opendata *data = calldata;
2356 	struct nfs4_state *state = NULL;
2357 
2358 	/* If this request hasn't been cancelled, do nothing */
2359 	if (!data->cancelled)
2360 		goto out_free;
2361 	/* In case of error, no cleanup! */
2362 	if (!data->rpc_done)
2363 		goto out_free;
2364 	state = nfs4_opendata_to_nfs4_state(data);
2365 	if (!IS_ERR(state))
2366 		nfs4_close_state(state, data->o_arg.fmode);
2367 out_free:
2368 	nfs4_opendata_put(data);
2369 }
2370 
2371 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2372 	.rpc_call_prepare = nfs4_open_confirm_prepare,
2373 	.rpc_call_done = nfs4_open_confirm_done,
2374 	.rpc_release = nfs4_open_confirm_release,
2375 };
2376 
2377 /*
2378  * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2379  */
2380 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2381 {
2382 	struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2383 	struct rpc_task *task;
2384 	struct  rpc_message msg = {
2385 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2386 		.rpc_argp = &data->c_arg,
2387 		.rpc_resp = &data->c_res,
2388 		.rpc_cred = data->owner->so_cred,
2389 	};
2390 	struct rpc_task_setup task_setup_data = {
2391 		.rpc_client = server->client,
2392 		.rpc_message = &msg,
2393 		.callback_ops = &nfs4_open_confirm_ops,
2394 		.callback_data = data,
2395 		.workqueue = nfsiod_workqueue,
2396 		.flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2397 	};
2398 	int status;
2399 
2400 	nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2401 				data->is_recover);
2402 	kref_get(&data->kref);
2403 	data->rpc_done = false;
2404 	data->rpc_status = 0;
2405 	data->timestamp = jiffies;
2406 	task = rpc_run_task(&task_setup_data);
2407 	if (IS_ERR(task))
2408 		return PTR_ERR(task);
2409 	status = rpc_wait_for_completion_task(task);
2410 	if (status != 0) {
2411 		data->cancelled = true;
2412 		smp_wmb();
2413 	} else
2414 		status = data->rpc_status;
2415 	rpc_put_task(task);
2416 	return status;
2417 }
2418 
2419 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2420 {
2421 	struct nfs4_opendata *data = calldata;
2422 	struct nfs4_state_owner *sp = data->owner;
2423 	struct nfs_client *clp = sp->so_server->nfs_client;
2424 	enum open_claim_type4 claim = data->o_arg.claim;
2425 
2426 	if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2427 		goto out_wait;
2428 	/*
2429 	 * Check if we still need to send an OPEN call, or if we can use
2430 	 * a delegation instead.
2431 	 */
2432 	if (data->state != NULL) {
2433 		struct nfs_delegation *delegation;
2434 
2435 		if (can_open_cached(data->state, data->o_arg.fmode,
2436 					data->o_arg.open_flags, claim))
2437 			goto out_no_action;
2438 		rcu_read_lock();
2439 		delegation = nfs4_get_valid_delegation(data->state->inode);
2440 		if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2441 			goto unlock_no_action;
2442 		rcu_read_unlock();
2443 	}
2444 	/* Update client id. */
2445 	data->o_arg.clientid = clp->cl_clientid;
2446 	switch (claim) {
2447 	default:
2448 		break;
2449 	case NFS4_OPEN_CLAIM_PREVIOUS:
2450 	case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2451 	case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2452 		data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2453 		fallthrough;
2454 	case NFS4_OPEN_CLAIM_FH:
2455 		task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2456 	}
2457 	data->timestamp = jiffies;
2458 	if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2459 				&data->o_arg.seq_args,
2460 				&data->o_res.seq_res,
2461 				task) != 0)
2462 		nfs_release_seqid(data->o_arg.seqid);
2463 
2464 	/* Set the create mode (note dependency on the session type) */
2465 	data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2466 	if (data->o_arg.open_flags & O_EXCL) {
2467 		data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2468 		if (clp->cl_mvops->minor_version == 0) {
2469 			data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2470 			/* don't put an ACCESS op in OPEN compound if O_EXCL,
2471 			 * because ACCESS will return permission denied for
2472 			 * all bits until close */
2473 			data->o_res.access_request = data->o_arg.access = 0;
2474 		} else if (nfs4_has_persistent_session(clp))
2475 			data->o_arg.createmode = NFS4_CREATE_GUARDED;
2476 	}
2477 	return;
2478 unlock_no_action:
2479 	trace_nfs4_cached_open(data->state);
2480 	rcu_read_unlock();
2481 out_no_action:
2482 	task->tk_action = NULL;
2483 out_wait:
2484 	nfs4_sequence_done(task, &data->o_res.seq_res);
2485 }
2486 
2487 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2488 {
2489 	struct nfs4_opendata *data = calldata;
2490 
2491 	data->rpc_status = task->tk_status;
2492 
2493 	if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2494 		return;
2495 
2496 	if (task->tk_status == 0) {
2497 		if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2498 			switch (data->o_res.f_attr->mode & S_IFMT) {
2499 			case S_IFREG:
2500 				break;
2501 			case S_IFLNK:
2502 				data->rpc_status = -ELOOP;
2503 				break;
2504 			case S_IFDIR:
2505 				data->rpc_status = -EISDIR;
2506 				break;
2507 			default:
2508 				data->rpc_status = -ENOTDIR;
2509 			}
2510 		}
2511 		renew_lease(data->o_res.server, data->timestamp);
2512 		if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2513 			nfs_confirm_seqid(&data->owner->so_seqid, 0);
2514 	}
2515 	data->rpc_done = true;
2516 }
2517 
2518 static void nfs4_open_release(void *calldata)
2519 {
2520 	struct nfs4_opendata *data = calldata;
2521 	struct nfs4_state *state = NULL;
2522 
2523 	/* If this request hasn't been cancelled, do nothing */
2524 	if (!data->cancelled)
2525 		goto out_free;
2526 	/* In case of error, no cleanup! */
2527 	if (data->rpc_status != 0 || !data->rpc_done)
2528 		goto out_free;
2529 	/* In case we need an open_confirm, no cleanup! */
2530 	if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2531 		goto out_free;
2532 	state = nfs4_opendata_to_nfs4_state(data);
2533 	if (!IS_ERR(state))
2534 		nfs4_close_state(state, data->o_arg.fmode);
2535 out_free:
2536 	nfs4_opendata_put(data);
2537 }
2538 
2539 static const struct rpc_call_ops nfs4_open_ops = {
2540 	.rpc_call_prepare = nfs4_open_prepare,
2541 	.rpc_call_done = nfs4_open_done,
2542 	.rpc_release = nfs4_open_release,
2543 };
2544 
2545 static int nfs4_run_open_task(struct nfs4_opendata *data,
2546 			      struct nfs_open_context *ctx)
2547 {
2548 	struct inode *dir = d_inode(data->dir);
2549 	struct nfs_server *server = NFS_SERVER(dir);
2550 	struct nfs_openargs *o_arg = &data->o_arg;
2551 	struct nfs_openres *o_res = &data->o_res;
2552 	struct rpc_task *task;
2553 	struct rpc_message msg = {
2554 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2555 		.rpc_argp = o_arg,
2556 		.rpc_resp = o_res,
2557 		.rpc_cred = data->owner->so_cred,
2558 	};
2559 	struct rpc_task_setup task_setup_data = {
2560 		.rpc_client = server->client,
2561 		.rpc_message = &msg,
2562 		.callback_ops = &nfs4_open_ops,
2563 		.callback_data = data,
2564 		.workqueue = nfsiod_workqueue,
2565 		.flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2566 	};
2567 	int status;
2568 
2569 	if (server->nfs_client->cl_minorversion)
2570 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
2571 
2572 	kref_get(&data->kref);
2573 	data->rpc_done = false;
2574 	data->rpc_status = 0;
2575 	data->cancelled = false;
2576 	data->is_recover = false;
2577 	if (!ctx) {
2578 		nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2579 		data->is_recover = true;
2580 		task_setup_data.flags |= RPC_TASK_TIMEOUT;
2581 	} else {
2582 		nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2583 		pnfs_lgopen_prepare(data, ctx);
2584 	}
2585 	task = rpc_run_task(&task_setup_data);
2586 	if (IS_ERR(task))
2587 		return PTR_ERR(task);
2588 	status = rpc_wait_for_completion_task(task);
2589 	if (status != 0) {
2590 		data->cancelled = true;
2591 		smp_wmb();
2592 	} else
2593 		status = data->rpc_status;
2594 	rpc_put_task(task);
2595 
2596 	return status;
2597 }
2598 
2599 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2600 {
2601 	struct inode *dir = d_inode(data->dir);
2602 	struct nfs_openres *o_res = &data->o_res;
2603 	int status;
2604 
2605 	status = nfs4_run_open_task(data, NULL);
2606 	if (status != 0 || !data->rpc_done)
2607 		return status;
2608 
2609 	nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2610 
2611 	if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2612 		status = _nfs4_proc_open_confirm(data);
2613 
2614 	return status;
2615 }
2616 
2617 /*
2618  * Additional permission checks in order to distinguish between an
2619  * open for read, and an open for execute. This works around the
2620  * fact that NFSv4 OPEN treats read and execute permissions as being
2621  * the same.
2622  * Note that in the non-execute case, we want to turn off permission
2623  * checking if we just created a new file (POSIX open() semantics).
2624  */
2625 static int nfs4_opendata_access(const struct cred *cred,
2626 				struct nfs4_opendata *opendata,
2627 				struct nfs4_state *state, fmode_t fmode,
2628 				int openflags)
2629 {
2630 	struct nfs_access_entry cache;
2631 	u32 mask, flags;
2632 
2633 	/* access call failed or for some reason the server doesn't
2634 	 * support any access modes -- defer access call until later */
2635 	if (opendata->o_res.access_supported == 0)
2636 		return 0;
2637 
2638 	mask = 0;
2639 	/*
2640 	 * Use openflags to check for exec, because fmode won't
2641 	 * always have FMODE_EXEC set when file open for exec.
2642 	 */
2643 	if (openflags & __FMODE_EXEC) {
2644 		/* ONLY check for exec rights */
2645 		if (S_ISDIR(state->inode->i_mode))
2646 			mask = NFS4_ACCESS_LOOKUP;
2647 		else
2648 			mask = NFS4_ACCESS_EXECUTE;
2649 	} else if ((fmode & FMODE_READ) && !opendata->file_created)
2650 		mask = NFS4_ACCESS_READ;
2651 
2652 	nfs_access_set_mask(&cache, opendata->o_res.access_result);
2653 	nfs_access_add_cache(state->inode, &cache, cred);
2654 
2655 	flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2656 	if ((mask & ~cache.mask & flags) == 0)
2657 		return 0;
2658 
2659 	return -EACCES;
2660 }
2661 
2662 /*
2663  * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2664  */
2665 static int _nfs4_proc_open(struct nfs4_opendata *data,
2666 			   struct nfs_open_context *ctx)
2667 {
2668 	struct inode *dir = d_inode(data->dir);
2669 	struct nfs_server *server = NFS_SERVER(dir);
2670 	struct nfs_openargs *o_arg = &data->o_arg;
2671 	struct nfs_openres *o_res = &data->o_res;
2672 	int status;
2673 
2674 	status = nfs4_run_open_task(data, ctx);
2675 	if (!data->rpc_done)
2676 		return status;
2677 	if (status != 0) {
2678 		if (status == -NFS4ERR_BADNAME &&
2679 				!(o_arg->open_flags & O_CREAT))
2680 			return -ENOENT;
2681 		return status;
2682 	}
2683 
2684 	nfs_fattr_map_and_free_names(server, &data->f_attr);
2685 
2686 	if (o_arg->open_flags & O_CREAT) {
2687 		if (o_arg->open_flags & O_EXCL)
2688 			data->file_created = true;
2689 		else if (o_res->cinfo.before != o_res->cinfo.after)
2690 			data->file_created = true;
2691 		if (data->file_created ||
2692 		    inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2693 			nfs4_update_changeattr(dir, &o_res->cinfo,
2694 					o_res->f_attr->time_start,
2695 					NFS_INO_INVALID_DATA);
2696 	}
2697 	if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2698 		server->caps &= ~NFS_CAP_POSIX_LOCK;
2699 	if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2700 		status = _nfs4_proc_open_confirm(data);
2701 		if (status != 0)
2702 			return status;
2703 	}
2704 	if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2705 		nfs4_sequence_free_slot(&o_res->seq_res);
2706 		nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, NULL);
2707 	}
2708 	return 0;
2709 }
2710 
2711 /*
2712  * OPEN_EXPIRED:
2713  * 	reclaim state on the server after a network partition.
2714  * 	Assumes caller holds the appropriate lock
2715  */
2716 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2717 {
2718 	struct nfs4_opendata *opendata;
2719 	int ret;
2720 
2721 	opendata = nfs4_open_recoverdata_alloc(ctx, state,
2722 			NFS4_OPEN_CLAIM_FH);
2723 	if (IS_ERR(opendata))
2724 		return PTR_ERR(opendata);
2725 	ret = nfs4_open_recover(opendata, state);
2726 	if (ret == -ESTALE)
2727 		d_drop(ctx->dentry);
2728 	nfs4_opendata_put(opendata);
2729 	return ret;
2730 }
2731 
2732 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2733 {
2734 	struct nfs_server *server = NFS_SERVER(state->inode);
2735 	struct nfs4_exception exception = { };
2736 	int err;
2737 
2738 	do {
2739 		err = _nfs4_open_expired(ctx, state);
2740 		trace_nfs4_open_expired(ctx, 0, err);
2741 		if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2742 			continue;
2743 		switch (err) {
2744 		default:
2745 			goto out;
2746 		case -NFS4ERR_GRACE:
2747 		case -NFS4ERR_DELAY:
2748 			nfs4_handle_exception(server, err, &exception);
2749 			err = 0;
2750 		}
2751 	} while (exception.retry);
2752 out:
2753 	return err;
2754 }
2755 
2756 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2757 {
2758 	struct nfs_open_context *ctx;
2759 	int ret;
2760 
2761 	ctx = nfs4_state_find_open_context(state);
2762 	if (IS_ERR(ctx))
2763 		return -EAGAIN;
2764 	ret = nfs4_do_open_expired(ctx, state);
2765 	put_nfs_open_context(ctx);
2766 	return ret;
2767 }
2768 
2769 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2770 		const nfs4_stateid *stateid)
2771 {
2772 	nfs_remove_bad_delegation(state->inode, stateid);
2773 	nfs_state_clear_delegation(state);
2774 }
2775 
2776 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2777 {
2778 	if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2779 		nfs_finish_clear_delegation_stateid(state, NULL);
2780 }
2781 
2782 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2783 {
2784 	/* NFSv4.0 doesn't allow for delegation recovery on open expire */
2785 	nfs40_clear_delegation_stateid(state);
2786 	nfs_state_clear_open_state_flags(state);
2787 	return nfs4_open_expired(sp, state);
2788 }
2789 
2790 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2791 		nfs4_stateid *stateid,
2792 		const struct cred *cred)
2793 {
2794 	return -NFS4ERR_BAD_STATEID;
2795 }
2796 
2797 #if defined(CONFIG_NFS_V4_1)
2798 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2799 		nfs4_stateid *stateid,
2800 		const struct cred *cred)
2801 {
2802 	int status;
2803 
2804 	switch (stateid->type) {
2805 	default:
2806 		break;
2807 	case NFS4_INVALID_STATEID_TYPE:
2808 	case NFS4_SPECIAL_STATEID_TYPE:
2809 		return -NFS4ERR_BAD_STATEID;
2810 	case NFS4_REVOKED_STATEID_TYPE:
2811 		goto out_free;
2812 	}
2813 
2814 	status = nfs41_test_stateid(server, stateid, cred);
2815 	switch (status) {
2816 	case -NFS4ERR_EXPIRED:
2817 	case -NFS4ERR_ADMIN_REVOKED:
2818 	case -NFS4ERR_DELEG_REVOKED:
2819 		break;
2820 	default:
2821 		return status;
2822 	}
2823 out_free:
2824 	/* Ack the revoked state to the server */
2825 	nfs41_free_stateid(server, stateid, cred, true);
2826 	return -NFS4ERR_EXPIRED;
2827 }
2828 
2829 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2830 {
2831 	struct nfs_server *server = NFS_SERVER(state->inode);
2832 	nfs4_stateid stateid;
2833 	struct nfs_delegation *delegation;
2834 	const struct cred *cred = NULL;
2835 	int status, ret = NFS_OK;
2836 
2837 	/* Get the delegation credential for use by test/free_stateid */
2838 	rcu_read_lock();
2839 	delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2840 	if (delegation == NULL) {
2841 		rcu_read_unlock();
2842 		nfs_state_clear_delegation(state);
2843 		return NFS_OK;
2844 	}
2845 
2846 	spin_lock(&delegation->lock);
2847 	nfs4_stateid_copy(&stateid, &delegation->stateid);
2848 
2849 	if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2850 				&delegation->flags)) {
2851 		spin_unlock(&delegation->lock);
2852 		rcu_read_unlock();
2853 		return NFS_OK;
2854 	}
2855 
2856 	if (delegation->cred)
2857 		cred = get_cred(delegation->cred);
2858 	spin_unlock(&delegation->lock);
2859 	rcu_read_unlock();
2860 	status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2861 	trace_nfs4_test_delegation_stateid(state, NULL, status);
2862 	if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2863 		nfs_finish_clear_delegation_stateid(state, &stateid);
2864 	else
2865 		ret = status;
2866 
2867 	put_cred(cred);
2868 	return ret;
2869 }
2870 
2871 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2872 {
2873 	nfs4_stateid tmp;
2874 
2875 	if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2876 	    nfs4_copy_delegation_stateid(state->inode, state->state,
2877 				&tmp, NULL) &&
2878 	    nfs4_stateid_match_other(&state->stateid, &tmp))
2879 		nfs_state_set_delegation(state, &tmp, state->state);
2880 	else
2881 		nfs_state_clear_delegation(state);
2882 }
2883 
2884 /**
2885  * nfs41_check_expired_locks - possibly free a lock stateid
2886  *
2887  * @state: NFSv4 state for an inode
2888  *
2889  * Returns NFS_OK if recovery for this stateid is now finished.
2890  * Otherwise a negative NFS4ERR value is returned.
2891  */
2892 static int nfs41_check_expired_locks(struct nfs4_state *state)
2893 {
2894 	int status, ret = NFS_OK;
2895 	struct nfs4_lock_state *lsp, *prev = NULL;
2896 	struct nfs_server *server = NFS_SERVER(state->inode);
2897 
2898 	if (!test_bit(LK_STATE_IN_USE, &state->flags))
2899 		goto out;
2900 
2901 	spin_lock(&state->state_lock);
2902 	list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2903 		if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2904 			const struct cred *cred = lsp->ls_state->owner->so_cred;
2905 
2906 			refcount_inc(&lsp->ls_count);
2907 			spin_unlock(&state->state_lock);
2908 
2909 			nfs4_put_lock_state(prev);
2910 			prev = lsp;
2911 
2912 			status = nfs41_test_and_free_expired_stateid(server,
2913 					&lsp->ls_stateid,
2914 					cred);
2915 			trace_nfs4_test_lock_stateid(state, lsp, status);
2916 			if (status == -NFS4ERR_EXPIRED ||
2917 			    status == -NFS4ERR_BAD_STATEID) {
2918 				clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2919 				lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2920 				if (!recover_lost_locks)
2921 					set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2922 			} else if (status != NFS_OK) {
2923 				ret = status;
2924 				nfs4_put_lock_state(prev);
2925 				goto out;
2926 			}
2927 			spin_lock(&state->state_lock);
2928 		}
2929 	}
2930 	spin_unlock(&state->state_lock);
2931 	nfs4_put_lock_state(prev);
2932 out:
2933 	return ret;
2934 }
2935 
2936 /**
2937  * nfs41_check_open_stateid - possibly free an open stateid
2938  *
2939  * @state: NFSv4 state for an inode
2940  *
2941  * Returns NFS_OK if recovery for this stateid is now finished.
2942  * Otherwise a negative NFS4ERR value is returned.
2943  */
2944 static int nfs41_check_open_stateid(struct nfs4_state *state)
2945 {
2946 	struct nfs_server *server = NFS_SERVER(state->inode);
2947 	nfs4_stateid *stateid = &state->open_stateid;
2948 	const struct cred *cred = state->owner->so_cred;
2949 	int status;
2950 
2951 	if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2952 		return -NFS4ERR_BAD_STATEID;
2953 	status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2954 	trace_nfs4_test_open_stateid(state, NULL, status);
2955 	if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2956 		nfs_state_clear_open_state_flags(state);
2957 		stateid->type = NFS4_INVALID_STATEID_TYPE;
2958 		return status;
2959 	}
2960 	if (nfs_open_stateid_recover_openmode(state))
2961 		return -NFS4ERR_OPENMODE;
2962 	return NFS_OK;
2963 }
2964 
2965 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2966 {
2967 	int status;
2968 
2969 	status = nfs41_check_delegation_stateid(state);
2970 	if (status != NFS_OK)
2971 		return status;
2972 	nfs41_delegation_recover_stateid(state);
2973 
2974 	status = nfs41_check_expired_locks(state);
2975 	if (status != NFS_OK)
2976 		return status;
2977 	status = nfs41_check_open_stateid(state);
2978 	if (status != NFS_OK)
2979 		status = nfs4_open_expired(sp, state);
2980 	return status;
2981 }
2982 #endif
2983 
2984 /*
2985  * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2986  * fields corresponding to attributes that were used to store the verifier.
2987  * Make sure we clobber those fields in the later setattr call
2988  */
2989 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2990 				struct iattr *sattr, struct nfs4_label **label)
2991 {
2992 	const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2993 	__u32 attrset[3];
2994 	unsigned ret;
2995 	unsigned i;
2996 
2997 	for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2998 		attrset[i] = opendata->o_res.attrset[i];
2999 		if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
3000 			attrset[i] &= ~bitmask[i];
3001 	}
3002 
3003 	ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3004 		sattr->ia_valid : 0;
3005 
3006 	if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3007 		if (sattr->ia_valid & ATTR_ATIME_SET)
3008 			ret |= ATTR_ATIME_SET;
3009 		else
3010 			ret |= ATTR_ATIME;
3011 	}
3012 
3013 	if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3014 		if (sattr->ia_valid & ATTR_MTIME_SET)
3015 			ret |= ATTR_MTIME_SET;
3016 		else
3017 			ret |= ATTR_MTIME;
3018 	}
3019 
3020 	if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
3021 		*label = NULL;
3022 	return ret;
3023 }
3024 
3025 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
3026 		int flags, struct nfs_open_context *ctx)
3027 {
3028 	struct nfs4_state_owner *sp = opendata->owner;
3029 	struct nfs_server *server = sp->so_server;
3030 	struct dentry *dentry;
3031 	struct nfs4_state *state;
3032 	fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3033 	struct inode *dir = d_inode(opendata->dir);
3034 	unsigned long dir_verifier;
3035 	unsigned int seq;
3036 	int ret;
3037 
3038 	seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
3039 	dir_verifier = nfs_save_change_attribute(dir);
3040 
3041 	ret = _nfs4_proc_open(opendata, ctx);
3042 	if (ret != 0)
3043 		goto out;
3044 
3045 	state = _nfs4_opendata_to_nfs4_state(opendata);
3046 	ret = PTR_ERR(state);
3047 	if (IS_ERR(state))
3048 		goto out;
3049 	ctx->state = state;
3050 	if (server->caps & NFS_CAP_POSIX_LOCK)
3051 		set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3052 	if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3053 		set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3054 
3055 	dentry = opendata->dentry;
3056 	if (d_really_is_negative(dentry)) {
3057 		struct dentry *alias;
3058 		d_drop(dentry);
3059 		alias = d_exact_alias(dentry, state->inode);
3060 		if (!alias)
3061 			alias = d_splice_alias(igrab(state->inode), dentry);
3062 		/* d_splice_alias() can't fail here - it's a non-directory */
3063 		if (alias) {
3064 			dput(ctx->dentry);
3065 			ctx->dentry = dentry = alias;
3066 		}
3067 	}
3068 
3069 	switch(opendata->o_arg.claim) {
3070 	default:
3071 		break;
3072 	case NFS4_OPEN_CLAIM_NULL:
3073 	case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3074 	case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3075 		if (!opendata->rpc_done)
3076 			break;
3077 		if (opendata->o_res.delegation_type != 0)
3078 			dir_verifier = nfs_save_change_attribute(dir);
3079 		nfs_set_verifier(dentry, dir_verifier);
3080 	}
3081 
3082 	/* Parse layoutget results before we check for access */
3083 	pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3084 
3085 	ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3086 			acc_mode, flags);
3087 	if (ret != 0)
3088 		goto out;
3089 
3090 	if (d_inode(dentry) == state->inode) {
3091 		nfs_inode_attach_open_context(ctx);
3092 		if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3093 			nfs4_schedule_stateid_recovery(server, state);
3094 	}
3095 
3096 out:
3097 	if (!opendata->cancelled)
3098 		nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3099 	return ret;
3100 }
3101 
3102 /*
3103  * Returns a referenced nfs4_state
3104  */
3105 static int _nfs4_do_open(struct inode *dir,
3106 			struct nfs_open_context *ctx,
3107 			int flags,
3108 			const struct nfs4_open_createattrs *c,
3109 			int *opened)
3110 {
3111 	struct nfs4_state_owner  *sp;
3112 	struct nfs4_state     *state = NULL;
3113 	struct nfs_server       *server = NFS_SERVER(dir);
3114 	struct nfs4_opendata *opendata;
3115 	struct dentry *dentry = ctx->dentry;
3116 	const struct cred *cred = ctx->cred;
3117 	struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3118 	fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3119 	enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3120 	struct iattr *sattr = c->sattr;
3121 	struct nfs4_label *label = c->label;
3122 	int status;
3123 
3124 	/* Protect against reboot recovery conflicts */
3125 	status = -ENOMEM;
3126 	sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3127 	if (sp == NULL) {
3128 		dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3129 		goto out_err;
3130 	}
3131 	status = nfs4_client_recover_expired_lease(server->nfs_client);
3132 	if (status != 0)
3133 		goto err_put_state_owner;
3134 	if (d_really_is_positive(dentry))
3135 		nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3136 	status = -ENOMEM;
3137 	if (d_really_is_positive(dentry))
3138 		claim = NFS4_OPEN_CLAIM_FH;
3139 	opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3140 			c, claim, GFP_KERNEL);
3141 	if (opendata == NULL)
3142 		goto err_put_state_owner;
3143 
3144 	if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3145 		if (!opendata->f_attr.mdsthreshold) {
3146 			opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3147 			if (!opendata->f_attr.mdsthreshold)
3148 				goto err_opendata_put;
3149 		}
3150 		opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3151 	}
3152 	if (d_really_is_positive(dentry))
3153 		opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3154 
3155 	status = _nfs4_open_and_get_state(opendata, flags, ctx);
3156 	if (status != 0)
3157 		goto err_opendata_put;
3158 	state = ctx->state;
3159 
3160 	if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3161 	    (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3162 		unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3163 		/*
3164 		 * send create attributes which was not set by open
3165 		 * with an extra setattr.
3166 		 */
3167 		if (attrs || label) {
3168 			unsigned ia_old = sattr->ia_valid;
3169 
3170 			sattr->ia_valid = attrs;
3171 			nfs_fattr_init(opendata->o_res.f_attr);
3172 			status = nfs4_do_setattr(state->inode, cred,
3173 					opendata->o_res.f_attr, sattr,
3174 					ctx, label);
3175 			if (status == 0) {
3176 				nfs_setattr_update_inode(state->inode, sattr,
3177 						opendata->o_res.f_attr);
3178 				nfs_setsecurity(state->inode, opendata->o_res.f_attr);
3179 			}
3180 			sattr->ia_valid = ia_old;
3181 		}
3182 	}
3183 	if (opened && opendata->file_created)
3184 		*opened = 1;
3185 
3186 	if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3187 		*ctx_th = opendata->f_attr.mdsthreshold;
3188 		opendata->f_attr.mdsthreshold = NULL;
3189 	}
3190 
3191 	nfs4_opendata_put(opendata);
3192 	nfs4_put_state_owner(sp);
3193 	return 0;
3194 err_opendata_put:
3195 	nfs4_opendata_put(opendata);
3196 err_put_state_owner:
3197 	nfs4_put_state_owner(sp);
3198 out_err:
3199 	return status;
3200 }
3201 
3202 
3203 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3204 					struct nfs_open_context *ctx,
3205 					int flags,
3206 					struct iattr *sattr,
3207 					struct nfs4_label *label,
3208 					int *opened)
3209 {
3210 	struct nfs_server *server = NFS_SERVER(dir);
3211 	struct nfs4_exception exception = {
3212 		.interruptible = true,
3213 	};
3214 	struct nfs4_state *res;
3215 	struct nfs4_open_createattrs c = {
3216 		.label = label,
3217 		.sattr = sattr,
3218 		.verf = {
3219 			[0] = (__u32)jiffies,
3220 			[1] = (__u32)current->pid,
3221 		},
3222 	};
3223 	int status;
3224 
3225 	do {
3226 		status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3227 		res = ctx->state;
3228 		trace_nfs4_open_file(ctx, flags, status);
3229 		if (status == 0)
3230 			break;
3231 		/* NOTE: BAD_SEQID means the server and client disagree about the
3232 		 * book-keeping w.r.t. state-changing operations
3233 		 * (OPEN/CLOSE/LOCK/LOCKU...)
3234 		 * It is actually a sign of a bug on the client or on the server.
3235 		 *
3236 		 * If we receive a BAD_SEQID error in the particular case of
3237 		 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3238 		 * have unhashed the old state_owner for us, and that we can
3239 		 * therefore safely retry using a new one. We should still warn
3240 		 * the user though...
3241 		 */
3242 		if (status == -NFS4ERR_BAD_SEQID) {
3243 			pr_warn_ratelimited("NFS: v4 server %s "
3244 					" returned a bad sequence-id error!\n",
3245 					NFS_SERVER(dir)->nfs_client->cl_hostname);
3246 			exception.retry = 1;
3247 			continue;
3248 		}
3249 		/*
3250 		 * BAD_STATEID on OPEN means that the server cancelled our
3251 		 * state before it received the OPEN_CONFIRM.
3252 		 * Recover by retrying the request as per the discussion
3253 		 * on Page 181 of RFC3530.
3254 		 */
3255 		if (status == -NFS4ERR_BAD_STATEID) {
3256 			exception.retry = 1;
3257 			continue;
3258 		}
3259 		if (status == -NFS4ERR_EXPIRED) {
3260 			nfs4_schedule_lease_recovery(server->nfs_client);
3261 			exception.retry = 1;
3262 			continue;
3263 		}
3264 		if (status == -EAGAIN) {
3265 			/* We must have found a delegation */
3266 			exception.retry = 1;
3267 			continue;
3268 		}
3269 		if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3270 			continue;
3271 		res = ERR_PTR(nfs4_handle_exception(server,
3272 					status, &exception));
3273 	} while (exception.retry);
3274 	return res;
3275 }
3276 
3277 static int _nfs4_do_setattr(struct inode *inode,
3278 			    struct nfs_setattrargs *arg,
3279 			    struct nfs_setattrres *res,
3280 			    const struct cred *cred,
3281 			    struct nfs_open_context *ctx)
3282 {
3283 	struct nfs_server *server = NFS_SERVER(inode);
3284 	struct rpc_message msg = {
3285 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3286 		.rpc_argp	= arg,
3287 		.rpc_resp	= res,
3288 		.rpc_cred	= cred,
3289 	};
3290 	const struct cred *delegation_cred = NULL;
3291 	unsigned long timestamp = jiffies;
3292 	bool truncate;
3293 	int status;
3294 
3295 	nfs_fattr_init(res->fattr);
3296 
3297 	/* Servers should only apply open mode checks for file size changes */
3298 	truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3299 	if (!truncate) {
3300 		nfs4_inode_make_writeable(inode);
3301 		goto zero_stateid;
3302 	}
3303 
3304 	if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3305 		/* Use that stateid */
3306 	} else if (ctx != NULL && ctx->state) {
3307 		struct nfs_lock_context *l_ctx;
3308 		if (!nfs4_valid_open_stateid(ctx->state))
3309 			return -EBADF;
3310 		l_ctx = nfs_get_lock_context(ctx);
3311 		if (IS_ERR(l_ctx))
3312 			return PTR_ERR(l_ctx);
3313 		status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3314 						&arg->stateid, &delegation_cred);
3315 		nfs_put_lock_context(l_ctx);
3316 		if (status == -EIO)
3317 			return -EBADF;
3318 		else if (status == -EAGAIN)
3319 			goto zero_stateid;
3320 	} else {
3321 zero_stateid:
3322 		nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3323 	}
3324 	if (delegation_cred)
3325 		msg.rpc_cred = delegation_cred;
3326 
3327 	status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3328 
3329 	put_cred(delegation_cred);
3330 	if (status == 0 && ctx != NULL)
3331 		renew_lease(server, timestamp);
3332 	trace_nfs4_setattr(inode, &arg->stateid, status);
3333 	return status;
3334 }
3335 
3336 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3337 			   struct nfs_fattr *fattr, struct iattr *sattr,
3338 			   struct nfs_open_context *ctx, struct nfs4_label *ilabel)
3339 {
3340 	struct nfs_server *server = NFS_SERVER(inode);
3341 	__u32 bitmask[NFS4_BITMASK_SZ];
3342 	struct nfs4_state *state = ctx ? ctx->state : NULL;
3343 	struct nfs_setattrargs	arg = {
3344 		.fh		= NFS_FH(inode),
3345 		.iap		= sattr,
3346 		.server		= server,
3347 		.bitmask = bitmask,
3348 		.label		= ilabel,
3349 	};
3350 	struct nfs_setattrres  res = {
3351 		.fattr		= fattr,
3352 		.server		= server,
3353 	};
3354 	struct nfs4_exception exception = {
3355 		.state = state,
3356 		.inode = inode,
3357 		.stateid = &arg.stateid,
3358 	};
3359 	unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
3360 	int err;
3361 
3362 	if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3363 		adjust_flags |= NFS_INO_INVALID_MODE;
3364 	if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
3365 		adjust_flags |= NFS_INO_INVALID_OTHER;
3366 
3367 	do {
3368 		nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label),
3369 					inode, adjust_flags);
3370 
3371 		err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3372 		switch (err) {
3373 		case -NFS4ERR_OPENMODE:
3374 			if (!(sattr->ia_valid & ATTR_SIZE)) {
3375 				pr_warn_once("NFSv4: server %s is incorrectly "
3376 						"applying open mode checks to "
3377 						"a SETATTR that is not "
3378 						"changing file size.\n",
3379 						server->nfs_client->cl_hostname);
3380 			}
3381 			if (state && !(state->state & FMODE_WRITE)) {
3382 				err = -EBADF;
3383 				if (sattr->ia_valid & ATTR_OPEN)
3384 					err = -EACCES;
3385 				goto out;
3386 			}
3387 		}
3388 		err = nfs4_handle_exception(server, err, &exception);
3389 	} while (exception.retry);
3390 out:
3391 	return err;
3392 }
3393 
3394 static bool
3395 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3396 {
3397 	if (inode == NULL || !nfs_have_layout(inode))
3398 		return false;
3399 
3400 	return pnfs_wait_on_layoutreturn(inode, task);
3401 }
3402 
3403 /*
3404  * Update the seqid of an open stateid
3405  */
3406 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3407 		struct nfs4_state *state)
3408 {
3409 	__be32 seqid_open;
3410 	u32 dst_seqid;
3411 	int seq;
3412 
3413 	for (;;) {
3414 		if (!nfs4_valid_open_stateid(state))
3415 			break;
3416 		seq = read_seqbegin(&state->seqlock);
3417 		if (!nfs4_state_match_open_stateid_other(state, dst)) {
3418 			nfs4_stateid_copy(dst, &state->open_stateid);
3419 			if (read_seqretry(&state->seqlock, seq))
3420 				continue;
3421 			break;
3422 		}
3423 		seqid_open = state->open_stateid.seqid;
3424 		if (read_seqretry(&state->seqlock, seq))
3425 			continue;
3426 
3427 		dst_seqid = be32_to_cpu(dst->seqid);
3428 		if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3429 			dst->seqid = seqid_open;
3430 		break;
3431 	}
3432 }
3433 
3434 /*
3435  * Update the seqid of an open stateid after receiving
3436  * NFS4ERR_OLD_STATEID
3437  */
3438 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3439 		struct nfs4_state *state)
3440 {
3441 	__be32 seqid_open;
3442 	u32 dst_seqid;
3443 	bool ret;
3444 	int seq, status = -EAGAIN;
3445 	DEFINE_WAIT(wait);
3446 
3447 	for (;;) {
3448 		ret = false;
3449 		if (!nfs4_valid_open_stateid(state))
3450 			break;
3451 		seq = read_seqbegin(&state->seqlock);
3452 		if (!nfs4_state_match_open_stateid_other(state, dst)) {
3453 			if (read_seqretry(&state->seqlock, seq))
3454 				continue;
3455 			break;
3456 		}
3457 
3458 		write_seqlock(&state->seqlock);
3459 		seqid_open = state->open_stateid.seqid;
3460 
3461 		dst_seqid = be32_to_cpu(dst->seqid);
3462 
3463 		/* Did another OPEN bump the state's seqid?  try again: */
3464 		if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3465 			dst->seqid = seqid_open;
3466 			write_sequnlock(&state->seqlock);
3467 			ret = true;
3468 			break;
3469 		}
3470 
3471 		/* server says we're behind but we haven't seen the update yet */
3472 		set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3473 		prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3474 		write_sequnlock(&state->seqlock);
3475 		trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3476 
3477 		if (fatal_signal_pending(current))
3478 			status = -EINTR;
3479 		else
3480 			if (schedule_timeout(5*HZ) != 0)
3481 				status = 0;
3482 
3483 		finish_wait(&state->waitq, &wait);
3484 
3485 		if (!status)
3486 			continue;
3487 		if (status == -EINTR)
3488 			break;
3489 
3490 		/* we slept the whole 5 seconds, we must have lost a seqid */
3491 		dst->seqid = cpu_to_be32(dst_seqid + 1);
3492 		ret = true;
3493 		break;
3494 	}
3495 
3496 	return ret;
3497 }
3498 
3499 struct nfs4_closedata {
3500 	struct inode *inode;
3501 	struct nfs4_state *state;
3502 	struct nfs_closeargs arg;
3503 	struct nfs_closeres res;
3504 	struct {
3505 		struct nfs4_layoutreturn_args arg;
3506 		struct nfs4_layoutreturn_res res;
3507 		struct nfs4_xdr_opaque_data ld_private;
3508 		u32 roc_barrier;
3509 		bool roc;
3510 	} lr;
3511 	struct nfs_fattr fattr;
3512 	unsigned long timestamp;
3513 };
3514 
3515 static void nfs4_free_closedata(void *data)
3516 {
3517 	struct nfs4_closedata *calldata = data;
3518 	struct nfs4_state_owner *sp = calldata->state->owner;
3519 	struct super_block *sb = calldata->state->inode->i_sb;
3520 
3521 	if (calldata->lr.roc)
3522 		pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3523 				calldata->res.lr_ret);
3524 	nfs4_put_open_state(calldata->state);
3525 	nfs_free_seqid(calldata->arg.seqid);
3526 	nfs4_put_state_owner(sp);
3527 	nfs_sb_deactive(sb);
3528 	kfree(calldata);
3529 }
3530 
3531 static void nfs4_close_done(struct rpc_task *task, void *data)
3532 {
3533 	struct nfs4_closedata *calldata = data;
3534 	struct nfs4_state *state = calldata->state;
3535 	struct nfs_server *server = NFS_SERVER(calldata->inode);
3536 	nfs4_stateid *res_stateid = NULL;
3537 	struct nfs4_exception exception = {
3538 		.state = state,
3539 		.inode = calldata->inode,
3540 		.stateid = &calldata->arg.stateid,
3541 	};
3542 
3543 	if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3544 		return;
3545 	trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3546 
3547 	/* Handle Layoutreturn errors */
3548 	if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3549 			  &calldata->res.lr_ret) == -EAGAIN)
3550 		goto out_restart;
3551 
3552 	/* hmm. we are done with the inode, and in the process of freeing
3553 	 * the state_owner. we keep this around to process errors
3554 	 */
3555 	switch (task->tk_status) {
3556 		case 0:
3557 			res_stateid = &calldata->res.stateid;
3558 			renew_lease(server, calldata->timestamp);
3559 			break;
3560 		case -NFS4ERR_ACCESS:
3561 			if (calldata->arg.bitmask != NULL) {
3562 				calldata->arg.bitmask = NULL;
3563 				calldata->res.fattr = NULL;
3564 				goto out_restart;
3565 
3566 			}
3567 			break;
3568 		case -NFS4ERR_OLD_STATEID:
3569 			/* Did we race with OPEN? */
3570 			if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3571 						state))
3572 				goto out_restart;
3573 			goto out_release;
3574 		case -NFS4ERR_ADMIN_REVOKED:
3575 		case -NFS4ERR_STALE_STATEID:
3576 		case -NFS4ERR_EXPIRED:
3577 			nfs4_free_revoked_stateid(server,
3578 					&calldata->arg.stateid,
3579 					task->tk_msg.rpc_cred);
3580 			fallthrough;
3581 		case -NFS4ERR_BAD_STATEID:
3582 			if (calldata->arg.fmode == 0)
3583 				break;
3584 			fallthrough;
3585 		default:
3586 			task->tk_status = nfs4_async_handle_exception(task,
3587 					server, task->tk_status, &exception);
3588 			if (exception.retry)
3589 				goto out_restart;
3590 	}
3591 	nfs_clear_open_stateid(state, &calldata->arg.stateid,
3592 			res_stateid, calldata->arg.fmode);
3593 out_release:
3594 	task->tk_status = 0;
3595 	nfs_release_seqid(calldata->arg.seqid);
3596 	nfs_refresh_inode(calldata->inode, &calldata->fattr);
3597 	dprintk("%s: ret = %d\n", __func__, task->tk_status);
3598 	return;
3599 out_restart:
3600 	task->tk_status = 0;
3601 	rpc_restart_call_prepare(task);
3602 	goto out_release;
3603 }
3604 
3605 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3606 {
3607 	struct nfs4_closedata *calldata = data;
3608 	struct nfs4_state *state = calldata->state;
3609 	struct inode *inode = calldata->inode;
3610 	struct nfs_server *server = NFS_SERVER(inode);
3611 	struct pnfs_layout_hdr *lo;
3612 	bool is_rdonly, is_wronly, is_rdwr;
3613 	int call_close = 0;
3614 
3615 	if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3616 		goto out_wait;
3617 
3618 	task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3619 	spin_lock(&state->owner->so_lock);
3620 	is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3621 	is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3622 	is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3623 	/* Calculate the change in open mode */
3624 	calldata->arg.fmode = 0;
3625 	if (state->n_rdwr == 0) {
3626 		if (state->n_rdonly == 0)
3627 			call_close |= is_rdonly;
3628 		else if (is_rdonly)
3629 			calldata->arg.fmode |= FMODE_READ;
3630 		if (state->n_wronly == 0)
3631 			call_close |= is_wronly;
3632 		else if (is_wronly)
3633 			calldata->arg.fmode |= FMODE_WRITE;
3634 		if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3635 			call_close |= is_rdwr;
3636 	} else if (is_rdwr)
3637 		calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3638 
3639 	nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3640 	if (!nfs4_valid_open_stateid(state))
3641 		call_close = 0;
3642 	spin_unlock(&state->owner->so_lock);
3643 
3644 	if (!call_close) {
3645 		/* Note: exit _without_ calling nfs4_close_done */
3646 		goto out_no_action;
3647 	}
3648 
3649 	if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3650 		nfs_release_seqid(calldata->arg.seqid);
3651 		goto out_wait;
3652 	}
3653 
3654 	lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3655 	if (lo && !pnfs_layout_is_valid(lo)) {
3656 		calldata->arg.lr_args = NULL;
3657 		calldata->res.lr_res = NULL;
3658 	}
3659 
3660 	if (calldata->arg.fmode == 0)
3661 		task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3662 
3663 	if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3664 		/* Close-to-open cache consistency revalidation */
3665 		if (!nfs4_have_delegation(inode, FMODE_READ)) {
3666 			nfs4_bitmask_set(calldata->arg.bitmask_store,
3667 					 server->cache_consistency_bitmask,
3668 					 inode, 0);
3669 			calldata->arg.bitmask = calldata->arg.bitmask_store;
3670 		} else
3671 			calldata->arg.bitmask = NULL;
3672 	}
3673 
3674 	calldata->arg.share_access =
3675 		nfs4_map_atomic_open_share(NFS_SERVER(inode),
3676 				calldata->arg.fmode, 0);
3677 
3678 	if (calldata->res.fattr == NULL)
3679 		calldata->arg.bitmask = NULL;
3680 	else if (calldata->arg.bitmask == NULL)
3681 		calldata->res.fattr = NULL;
3682 	calldata->timestamp = jiffies;
3683 	if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3684 				&calldata->arg.seq_args,
3685 				&calldata->res.seq_res,
3686 				task) != 0)
3687 		nfs_release_seqid(calldata->arg.seqid);
3688 	return;
3689 out_no_action:
3690 	task->tk_action = NULL;
3691 out_wait:
3692 	nfs4_sequence_done(task, &calldata->res.seq_res);
3693 }
3694 
3695 static const struct rpc_call_ops nfs4_close_ops = {
3696 	.rpc_call_prepare = nfs4_close_prepare,
3697 	.rpc_call_done = nfs4_close_done,
3698 	.rpc_release = nfs4_free_closedata,
3699 };
3700 
3701 /*
3702  * It is possible for data to be read/written from a mem-mapped file
3703  * after the sys_close call (which hits the vfs layer as a flush).
3704  * This means that we can't safely call nfsv4 close on a file until
3705  * the inode is cleared. This in turn means that we are not good
3706  * NFSv4 citizens - we do not indicate to the server to update the file's
3707  * share state even when we are done with one of the three share
3708  * stateid's in the inode.
3709  *
3710  * NOTE: Caller must be holding the sp->so_owner semaphore!
3711  */
3712 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3713 {
3714 	struct nfs_server *server = NFS_SERVER(state->inode);
3715 	struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3716 	struct nfs4_closedata *calldata;
3717 	struct nfs4_state_owner *sp = state->owner;
3718 	struct rpc_task *task;
3719 	struct rpc_message msg = {
3720 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3721 		.rpc_cred = state->owner->so_cred,
3722 	};
3723 	struct rpc_task_setup task_setup_data = {
3724 		.rpc_client = server->client,
3725 		.rpc_message = &msg,
3726 		.callback_ops = &nfs4_close_ops,
3727 		.workqueue = nfsiod_workqueue,
3728 		.flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3729 	};
3730 	int status = -ENOMEM;
3731 
3732 	if (server->nfs_client->cl_minorversion)
3733 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
3734 
3735 	nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3736 		&task_setup_data.rpc_client, &msg);
3737 
3738 	calldata = kzalloc(sizeof(*calldata), gfp_mask);
3739 	if (calldata == NULL)
3740 		goto out;
3741 	nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3742 	calldata->inode = state->inode;
3743 	calldata->state = state;
3744 	calldata->arg.fh = NFS_FH(state->inode);
3745 	if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3746 		goto out_free_calldata;
3747 	/* Serialization for the sequence id */
3748 	alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3749 	calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3750 	if (IS_ERR(calldata->arg.seqid))
3751 		goto out_free_calldata;
3752 	nfs_fattr_init(&calldata->fattr);
3753 	calldata->arg.fmode = 0;
3754 	calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3755 	calldata->res.fattr = &calldata->fattr;
3756 	calldata->res.seqid = calldata->arg.seqid;
3757 	calldata->res.server = server;
3758 	calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3759 	calldata->lr.roc = pnfs_roc(state->inode,
3760 			&calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3761 	if (calldata->lr.roc) {
3762 		calldata->arg.lr_args = &calldata->lr.arg;
3763 		calldata->res.lr_res = &calldata->lr.res;
3764 	}
3765 	nfs_sb_active(calldata->inode->i_sb);
3766 
3767 	msg.rpc_argp = &calldata->arg;
3768 	msg.rpc_resp = &calldata->res;
3769 	task_setup_data.callback_data = calldata;
3770 	task = rpc_run_task(&task_setup_data);
3771 	if (IS_ERR(task))
3772 		return PTR_ERR(task);
3773 	status = 0;
3774 	if (wait)
3775 		status = rpc_wait_for_completion_task(task);
3776 	rpc_put_task(task);
3777 	return status;
3778 out_free_calldata:
3779 	kfree(calldata);
3780 out:
3781 	nfs4_put_open_state(state);
3782 	nfs4_put_state_owner(sp);
3783 	return status;
3784 }
3785 
3786 static struct inode *
3787 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3788 		int open_flags, struct iattr *attr, int *opened)
3789 {
3790 	struct nfs4_state *state;
3791 	struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3792 
3793 	label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3794 
3795 	/* Protect against concurrent sillydeletes */
3796 	state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3797 
3798 	nfs4_label_release_security(label);
3799 
3800 	if (IS_ERR(state))
3801 		return ERR_CAST(state);
3802 	return state->inode;
3803 }
3804 
3805 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3806 {
3807 	if (ctx->state == NULL)
3808 		return;
3809 	if (is_sync)
3810 		nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3811 	else
3812 		nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3813 }
3814 
3815 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3816 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3817 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
3818 
3819 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3820 {
3821 	u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3822 	struct nfs4_server_caps_arg args = {
3823 		.fhandle = fhandle,
3824 		.bitmask = bitmask,
3825 	};
3826 	struct nfs4_server_caps_res res = {};
3827 	struct rpc_message msg = {
3828 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3829 		.rpc_argp = &args,
3830 		.rpc_resp = &res,
3831 	};
3832 	int status;
3833 	int i;
3834 
3835 	bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3836 		     FATTR4_WORD0_FH_EXPIRE_TYPE |
3837 		     FATTR4_WORD0_LINK_SUPPORT |
3838 		     FATTR4_WORD0_SYMLINK_SUPPORT |
3839 		     FATTR4_WORD0_ACLSUPPORT |
3840 		     FATTR4_WORD0_CASE_INSENSITIVE |
3841 		     FATTR4_WORD0_CASE_PRESERVING;
3842 	if (minorversion)
3843 		bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3844 
3845 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3846 	if (status == 0) {
3847 		/* Sanity check the server answers */
3848 		switch (minorversion) {
3849 		case 0:
3850 			res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3851 			res.attr_bitmask[2] = 0;
3852 			break;
3853 		case 1:
3854 			res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3855 			break;
3856 		case 2:
3857 			res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3858 		}
3859 		memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3860 		server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS |
3861 				  NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL);
3862 		server->fattr_valid = NFS_ATTR_FATTR_V4;
3863 		if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3864 				res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3865 			server->caps |= NFS_CAP_ACLS;
3866 		if (res.has_links != 0)
3867 			server->caps |= NFS_CAP_HARDLINKS;
3868 		if (res.has_symlinks != 0)
3869 			server->caps |= NFS_CAP_SYMLINKS;
3870 		if (res.case_insensitive)
3871 			server->caps |= NFS_CAP_CASE_INSENSITIVE;
3872 		if (res.case_preserving)
3873 			server->caps |= NFS_CAP_CASE_PRESERVING;
3874 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3875 		if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3876 			server->caps |= NFS_CAP_SECURITY_LABEL;
3877 #endif
3878 		if (res.attr_bitmask[0] & FATTR4_WORD0_FS_LOCATIONS)
3879 			server->caps |= NFS_CAP_FS_LOCATIONS;
3880 		if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
3881 			server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
3882 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
3883 			server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
3884 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
3885 			server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
3886 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
3887 			server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
3888 				NFS_ATTR_FATTR_OWNER_NAME);
3889 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
3890 			server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
3891 				NFS_ATTR_FATTR_GROUP_NAME);
3892 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
3893 			server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
3894 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
3895 			server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
3896 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
3897 			server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
3898 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
3899 			server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
3900 		memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3901 				sizeof(server->attr_bitmask));
3902 		server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3903 
3904 		memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3905 		server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3906 		server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3907 		server->cache_consistency_bitmask[2] = 0;
3908 
3909 		/* Avoid a regression due to buggy server */
3910 		for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3911 			res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3912 		memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3913 			sizeof(server->exclcreat_bitmask));
3914 
3915 		server->acl_bitmask = res.acl_bitmask;
3916 		server->fh_expire_type = res.fh_expire_type;
3917 	}
3918 
3919 	return status;
3920 }
3921 
3922 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3923 {
3924 	struct nfs4_exception exception = {
3925 		.interruptible = true,
3926 	};
3927 	int err;
3928 
3929 	nfs4_server_set_init_caps(server);
3930 	do {
3931 		err = nfs4_handle_exception(server,
3932 				_nfs4_server_capabilities(server, fhandle),
3933 				&exception);
3934 	} while (exception.retry);
3935 	return err;
3936 }
3937 
3938 static void test_fs_location_for_trunking(struct nfs4_fs_location *location,
3939 					  struct nfs_client *clp,
3940 					  struct nfs_server *server)
3941 {
3942 	int i;
3943 
3944 	for (i = 0; i < location->nservers; i++) {
3945 		struct nfs4_string *srv_loc = &location->servers[i];
3946 		struct sockaddr addr;
3947 		size_t addrlen;
3948 		struct xprt_create xprt_args = {
3949 			.ident = 0,
3950 			.net = clp->cl_net,
3951 		};
3952 		struct nfs4_add_xprt_data xprtdata = {
3953 			.clp = clp,
3954 		};
3955 		struct rpc_add_xprt_test rpcdata = {
3956 			.add_xprt_test = clp->cl_mvops->session_trunk,
3957 			.data = &xprtdata,
3958 		};
3959 		char *servername = NULL;
3960 
3961 		if (!srv_loc->len)
3962 			continue;
3963 
3964 		addrlen = nfs_parse_server_name(srv_loc->data, srv_loc->len,
3965 						&addr, sizeof(addr),
3966 						clp->cl_net, server->port);
3967 		if (!addrlen)
3968 			return;
3969 		xprt_args.dstaddr = &addr;
3970 		xprt_args.addrlen = addrlen;
3971 		servername = kmalloc(srv_loc->len + 1, GFP_KERNEL);
3972 		if (!servername)
3973 			return;
3974 		memcpy(servername, srv_loc->data, srv_loc->len);
3975 		servername[srv_loc->len] = '\0';
3976 		xprt_args.servername = servername;
3977 
3978 		xprtdata.cred = nfs4_get_clid_cred(clp);
3979 		rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
3980 				  rpc_clnt_setup_test_and_add_xprt,
3981 				  &rpcdata);
3982 		if (xprtdata.cred)
3983 			put_cred(xprtdata.cred);
3984 		kfree(servername);
3985 	}
3986 }
3987 
3988 static int _nfs4_discover_trunking(struct nfs_server *server,
3989 				   struct nfs_fh *fhandle)
3990 {
3991 	struct nfs4_fs_locations *locations = NULL;
3992 	struct page *page;
3993 	const struct cred *cred;
3994 	struct nfs_client *clp = server->nfs_client;
3995 	const struct nfs4_state_maintenance_ops *ops =
3996 		clp->cl_mvops->state_renewal_ops;
3997 	int status = -ENOMEM, i;
3998 
3999 	cred = ops->get_state_renewal_cred(clp);
4000 	if (cred == NULL) {
4001 		cred = nfs4_get_clid_cred(clp);
4002 		if (cred == NULL)
4003 			return -ENOKEY;
4004 	}
4005 
4006 	page = alloc_page(GFP_KERNEL);
4007 	locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4008 	if (page == NULL || locations == NULL)
4009 		goto out;
4010 
4011 	status = nfs4_proc_get_locations(server, fhandle, locations, page,
4012 					 cred);
4013 	if (status)
4014 		goto out;
4015 
4016 	for (i = 0; i < locations->nlocations; i++)
4017 		test_fs_location_for_trunking(&locations->locations[i], clp,
4018 					      server);
4019 out:
4020 	if (page)
4021 		__free_page(page);
4022 	kfree(locations);
4023 	return status;
4024 }
4025 
4026 static int nfs4_discover_trunking(struct nfs_server *server,
4027 				  struct nfs_fh *fhandle)
4028 {
4029 	struct nfs4_exception exception = {
4030 		.interruptible = true,
4031 	};
4032 	struct nfs_client *clp = server->nfs_client;
4033 	int err = 0;
4034 
4035 	if (!nfs4_has_session(clp))
4036 		goto out;
4037 	do {
4038 		err = nfs4_handle_exception(server,
4039 				_nfs4_discover_trunking(server, fhandle),
4040 				&exception);
4041 	} while (exception.retry);
4042 out:
4043 	return err;
4044 }
4045 
4046 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4047 		struct nfs_fsinfo *info)
4048 {
4049 	u32 bitmask[3];
4050 	struct nfs4_lookup_root_arg args = {
4051 		.bitmask = bitmask,
4052 	};
4053 	struct nfs4_lookup_res res = {
4054 		.server = server,
4055 		.fattr = info->fattr,
4056 		.fh = fhandle,
4057 	};
4058 	struct rpc_message msg = {
4059 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
4060 		.rpc_argp = &args,
4061 		.rpc_resp = &res,
4062 	};
4063 
4064 	bitmask[0] = nfs4_fattr_bitmap[0];
4065 	bitmask[1] = nfs4_fattr_bitmap[1];
4066 	/*
4067 	 * Process the label in the upcoming getfattr
4068 	 */
4069 	bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
4070 
4071 	nfs_fattr_init(info->fattr);
4072 	return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4073 }
4074 
4075 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4076 		struct nfs_fsinfo *info)
4077 {
4078 	struct nfs4_exception exception = {
4079 		.interruptible = true,
4080 	};
4081 	int err;
4082 	do {
4083 		err = _nfs4_lookup_root(server, fhandle, info);
4084 		trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
4085 		switch (err) {
4086 		case 0:
4087 		case -NFS4ERR_WRONGSEC:
4088 			goto out;
4089 		default:
4090 			err = nfs4_handle_exception(server, err, &exception);
4091 		}
4092 	} while (exception.retry);
4093 out:
4094 	return err;
4095 }
4096 
4097 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4098 				struct nfs_fsinfo *info, rpc_authflavor_t flavor)
4099 {
4100 	struct rpc_auth_create_args auth_args = {
4101 		.pseudoflavor = flavor,
4102 	};
4103 	struct rpc_auth *auth;
4104 
4105 	auth = rpcauth_create(&auth_args, server->client);
4106 	if (IS_ERR(auth))
4107 		return -EACCES;
4108 	return nfs4_lookup_root(server, fhandle, info);
4109 }
4110 
4111 /*
4112  * Retry pseudoroot lookup with various security flavors.  We do this when:
4113  *
4114  *   NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4115  *   NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4116  *
4117  * Returns zero on success, or a negative NFS4ERR value, or a
4118  * negative errno value.
4119  */
4120 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4121 			      struct nfs_fsinfo *info)
4122 {
4123 	/* Per 3530bis 15.33.5 */
4124 	static const rpc_authflavor_t flav_array[] = {
4125 		RPC_AUTH_GSS_KRB5P,
4126 		RPC_AUTH_GSS_KRB5I,
4127 		RPC_AUTH_GSS_KRB5,
4128 		RPC_AUTH_UNIX,			/* courtesy */
4129 		RPC_AUTH_NULL,
4130 	};
4131 	int status = -EPERM;
4132 	size_t i;
4133 
4134 	if (server->auth_info.flavor_len > 0) {
4135 		/* try each flavor specified by user */
4136 		for (i = 0; i < server->auth_info.flavor_len; i++) {
4137 			status = nfs4_lookup_root_sec(server, fhandle, info,
4138 						server->auth_info.flavors[i]);
4139 			if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4140 				continue;
4141 			break;
4142 		}
4143 	} else {
4144 		/* no flavors specified by user, try default list */
4145 		for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4146 			status = nfs4_lookup_root_sec(server, fhandle, info,
4147 						      flav_array[i]);
4148 			if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4149 				continue;
4150 			break;
4151 		}
4152 	}
4153 
4154 	/*
4155 	 * -EACCES could mean that the user doesn't have correct permissions
4156 	 * to access the mount.  It could also mean that we tried to mount
4157 	 * with a gss auth flavor, but rpc.gssd isn't running.  Either way,
4158 	 * existing mount programs don't handle -EACCES very well so it should
4159 	 * be mapped to -EPERM instead.
4160 	 */
4161 	if (status == -EACCES)
4162 		status = -EPERM;
4163 	return status;
4164 }
4165 
4166 /**
4167  * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4168  * @server: initialized nfs_server handle
4169  * @fhandle: we fill in the pseudo-fs root file handle
4170  * @info: we fill in an FSINFO struct
4171  * @auth_probe: probe the auth flavours
4172  *
4173  * Returns zero on success, or a negative errno.
4174  */
4175 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4176 			 struct nfs_fsinfo *info,
4177 			 bool auth_probe)
4178 {
4179 	int status = 0;
4180 
4181 	if (!auth_probe)
4182 		status = nfs4_lookup_root(server, fhandle, info);
4183 
4184 	if (auth_probe || status == NFS4ERR_WRONGSEC)
4185 		status = server->nfs_client->cl_mvops->find_root_sec(server,
4186 				fhandle, info);
4187 
4188 	if (status == 0)
4189 		status = nfs4_server_capabilities(server, fhandle);
4190 	if (status == 0)
4191 		status = nfs4_do_fsinfo(server, fhandle, info);
4192 
4193 	return nfs4_map_errors(status);
4194 }
4195 
4196 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4197 			      struct nfs_fsinfo *info)
4198 {
4199 	int error;
4200 	struct nfs_fattr *fattr = info->fattr;
4201 
4202 	error = nfs4_server_capabilities(server, mntfh);
4203 	if (error < 0) {
4204 		dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4205 		return error;
4206 	}
4207 
4208 	error = nfs4_proc_getattr(server, mntfh, fattr, NULL);
4209 	if (error < 0) {
4210 		dprintk("nfs4_get_root: getattr error = %d\n", -error);
4211 		goto out;
4212 	}
4213 
4214 	if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4215 	    !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4216 		memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4217 
4218 out:
4219 	return error;
4220 }
4221 
4222 /*
4223  * Get locations and (maybe) other attributes of a referral.
4224  * Note that we'll actually follow the referral later when
4225  * we detect fsid mismatch in inode revalidation
4226  */
4227 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4228 			     const struct qstr *name, struct nfs_fattr *fattr,
4229 			     struct nfs_fh *fhandle)
4230 {
4231 	int status = -ENOMEM;
4232 	struct page *page = NULL;
4233 	struct nfs4_fs_locations *locations = NULL;
4234 
4235 	page = alloc_page(GFP_KERNEL);
4236 	if (page == NULL)
4237 		goto out;
4238 	locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4239 	if (locations == NULL)
4240 		goto out;
4241 
4242 	status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4243 	if (status != 0)
4244 		goto out;
4245 
4246 	/*
4247 	 * If the fsid didn't change, this is a migration event, not a
4248 	 * referral.  Cause us to drop into the exception handler, which
4249 	 * will kick off migration recovery.
4250 	 */
4251 	if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
4252 		dprintk("%s: server did not return a different fsid for"
4253 			" a referral at %s\n", __func__, name->name);
4254 		status = -NFS4ERR_MOVED;
4255 		goto out;
4256 	}
4257 	/* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4258 	nfs_fixup_referral_attributes(&locations->fattr);
4259 
4260 	/* replace the lookup nfs_fattr with the locations nfs_fattr */
4261 	memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
4262 	memset(fhandle, 0, sizeof(struct nfs_fh));
4263 out:
4264 	if (page)
4265 		__free_page(page);
4266 	kfree(locations);
4267 	return status;
4268 }
4269 
4270 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4271 				struct nfs_fattr *fattr, struct inode *inode)
4272 {
4273 	__u32 bitmask[NFS4_BITMASK_SZ];
4274 	struct nfs4_getattr_arg args = {
4275 		.fh = fhandle,
4276 		.bitmask = bitmask,
4277 	};
4278 	struct nfs4_getattr_res res = {
4279 		.fattr = fattr,
4280 		.server = server,
4281 	};
4282 	struct rpc_message msg = {
4283 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4284 		.rpc_argp = &args,
4285 		.rpc_resp = &res,
4286 	};
4287 	unsigned short task_flags = 0;
4288 
4289 	if (nfs4_has_session(server->nfs_client))
4290 		task_flags = RPC_TASK_MOVEABLE;
4291 
4292 	/* Is this is an attribute revalidation, subject to softreval? */
4293 	if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4294 		task_flags |= RPC_TASK_TIMEOUT;
4295 
4296 	nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label), inode, 0);
4297 	nfs_fattr_init(fattr);
4298 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4299 	return nfs4_do_call_sync(server->client, server, &msg,
4300 			&args.seq_args, &res.seq_res, task_flags);
4301 }
4302 
4303 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4304 				struct nfs_fattr *fattr, struct inode *inode)
4305 {
4306 	struct nfs4_exception exception = {
4307 		.interruptible = true,
4308 	};
4309 	int err;
4310 	do {
4311 		err = _nfs4_proc_getattr(server, fhandle, fattr, inode);
4312 		trace_nfs4_getattr(server, fhandle, fattr, err);
4313 		err = nfs4_handle_exception(server, err,
4314 				&exception);
4315 	} while (exception.retry);
4316 	return err;
4317 }
4318 
4319 /*
4320  * The file is not closed if it is opened due to the a request to change
4321  * the size of the file. The open call will not be needed once the
4322  * VFS layer lookup-intents are implemented.
4323  *
4324  * Close is called when the inode is destroyed.
4325  * If we haven't opened the file for O_WRONLY, we
4326  * need to in the size_change case to obtain a stateid.
4327  *
4328  * Got race?
4329  * Because OPEN is always done by name in nfsv4, it is
4330  * possible that we opened a different file by the same
4331  * name.  We can recognize this race condition, but we
4332  * can't do anything about it besides returning an error.
4333  *
4334  * This will be fixed with VFS changes (lookup-intent).
4335  */
4336 static int
4337 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4338 		  struct iattr *sattr)
4339 {
4340 	struct inode *inode = d_inode(dentry);
4341 	const struct cred *cred = NULL;
4342 	struct nfs_open_context *ctx = NULL;
4343 	int status;
4344 
4345 	if (pnfs_ld_layoutret_on_setattr(inode) &&
4346 	    sattr->ia_valid & ATTR_SIZE &&
4347 	    sattr->ia_size < i_size_read(inode))
4348 		pnfs_commit_and_return_layout(inode);
4349 
4350 	nfs_fattr_init(fattr);
4351 
4352 	/* Deal with open(O_TRUNC) */
4353 	if (sattr->ia_valid & ATTR_OPEN)
4354 		sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4355 
4356 	/* Optimization: if the end result is no change, don't RPC */
4357 	if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4358 		return 0;
4359 
4360 	/* Search for an existing open(O_WRITE) file */
4361 	if (sattr->ia_valid & ATTR_FILE) {
4362 
4363 		ctx = nfs_file_open_context(sattr->ia_file);
4364 		if (ctx)
4365 			cred = ctx->cred;
4366 	}
4367 
4368 	/* Return any delegations if we're going to change ACLs */
4369 	if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4370 		nfs4_inode_make_writeable(inode);
4371 
4372 	status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL);
4373 	if (status == 0) {
4374 		nfs_setattr_update_inode(inode, sattr, fattr);
4375 		nfs_setsecurity(inode, fattr);
4376 	}
4377 	return status;
4378 }
4379 
4380 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4381 		struct dentry *dentry, struct nfs_fh *fhandle,
4382 		struct nfs_fattr *fattr)
4383 {
4384 	struct nfs_server *server = NFS_SERVER(dir);
4385 	int		       status;
4386 	struct nfs4_lookup_arg args = {
4387 		.bitmask = server->attr_bitmask,
4388 		.dir_fh = NFS_FH(dir),
4389 		.name = &dentry->d_name,
4390 	};
4391 	struct nfs4_lookup_res res = {
4392 		.server = server,
4393 		.fattr = fattr,
4394 		.fh = fhandle,
4395 	};
4396 	struct rpc_message msg = {
4397 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4398 		.rpc_argp = &args,
4399 		.rpc_resp = &res,
4400 	};
4401 	unsigned short task_flags = 0;
4402 
4403 	if (server->nfs_client->cl_minorversion)
4404 		task_flags = RPC_TASK_MOVEABLE;
4405 
4406 	/* Is this is an attribute revalidation, subject to softreval? */
4407 	if (nfs_lookup_is_soft_revalidate(dentry))
4408 		task_flags |= RPC_TASK_TIMEOUT;
4409 
4410 	args.bitmask = nfs4_bitmask(server, fattr->label);
4411 
4412 	nfs_fattr_init(fattr);
4413 
4414 	dprintk("NFS call  lookup %pd2\n", dentry);
4415 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4416 	status = nfs4_do_call_sync(clnt, server, &msg,
4417 			&args.seq_args, &res.seq_res, task_flags);
4418 	dprintk("NFS reply lookup: %d\n", status);
4419 	return status;
4420 }
4421 
4422 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4423 {
4424 	fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4425 		NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4426 	fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4427 	fattr->nlink = 2;
4428 }
4429 
4430 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4431 				   struct dentry *dentry, struct nfs_fh *fhandle,
4432 				   struct nfs_fattr *fattr)
4433 {
4434 	struct nfs4_exception exception = {
4435 		.interruptible = true,
4436 	};
4437 	struct rpc_clnt *client = *clnt;
4438 	const struct qstr *name = &dentry->d_name;
4439 	int err;
4440 	do {
4441 		err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr);
4442 		trace_nfs4_lookup(dir, name, err);
4443 		switch (err) {
4444 		case -NFS4ERR_BADNAME:
4445 			err = -ENOENT;
4446 			goto out;
4447 		case -NFS4ERR_MOVED:
4448 			err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4449 			if (err == -NFS4ERR_MOVED)
4450 				err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4451 			goto out;
4452 		case -NFS4ERR_WRONGSEC:
4453 			err = -EPERM;
4454 			if (client != *clnt)
4455 				goto out;
4456 			client = nfs4_negotiate_security(client, dir, name);
4457 			if (IS_ERR(client))
4458 				return PTR_ERR(client);
4459 
4460 			exception.retry = 1;
4461 			break;
4462 		default:
4463 			err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4464 		}
4465 	} while (exception.retry);
4466 
4467 out:
4468 	if (err == 0)
4469 		*clnt = client;
4470 	else if (client != *clnt)
4471 		rpc_shutdown_client(client);
4472 
4473 	return err;
4474 }
4475 
4476 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4477 			    struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4478 {
4479 	int status;
4480 	struct rpc_clnt *client = NFS_CLIENT(dir);
4481 
4482 	status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4483 	if (client != NFS_CLIENT(dir)) {
4484 		rpc_shutdown_client(client);
4485 		nfs_fixup_secinfo_attributes(fattr);
4486 	}
4487 	return status;
4488 }
4489 
4490 struct rpc_clnt *
4491 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4492 			    struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4493 {
4494 	struct rpc_clnt *client = NFS_CLIENT(dir);
4495 	int status;
4496 
4497 	status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4498 	if (status < 0)
4499 		return ERR_PTR(status);
4500 	return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4501 }
4502 
4503 static int _nfs4_proc_lookupp(struct inode *inode,
4504 		struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4505 {
4506 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
4507 	struct nfs_server *server = NFS_SERVER(inode);
4508 	int		       status;
4509 	struct nfs4_lookupp_arg args = {
4510 		.bitmask = server->attr_bitmask,
4511 		.fh = NFS_FH(inode),
4512 	};
4513 	struct nfs4_lookupp_res res = {
4514 		.server = server,
4515 		.fattr = fattr,
4516 		.fh = fhandle,
4517 	};
4518 	struct rpc_message msg = {
4519 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4520 		.rpc_argp = &args,
4521 		.rpc_resp = &res,
4522 	};
4523 	unsigned short task_flags = 0;
4524 
4525 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4526 		task_flags |= RPC_TASK_TIMEOUT;
4527 
4528 	args.bitmask = nfs4_bitmask(server, fattr->label);
4529 
4530 	nfs_fattr_init(fattr);
4531 
4532 	dprintk("NFS call  lookupp ino=0x%lx\n", inode->i_ino);
4533 	status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4534 				&res.seq_res, task_flags);
4535 	dprintk("NFS reply lookupp: %d\n", status);
4536 	return status;
4537 }
4538 
4539 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4540 			     struct nfs_fattr *fattr)
4541 {
4542 	struct nfs4_exception exception = {
4543 		.interruptible = true,
4544 	};
4545 	int err;
4546 	do {
4547 		err = _nfs4_proc_lookupp(inode, fhandle, fattr);
4548 		trace_nfs4_lookupp(inode, err);
4549 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
4550 				&exception);
4551 	} while (exception.retry);
4552 	return err;
4553 }
4554 
4555 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4556 			     const struct cred *cred)
4557 {
4558 	struct nfs_server *server = NFS_SERVER(inode);
4559 	struct nfs4_accessargs args = {
4560 		.fh = NFS_FH(inode),
4561 		.access = entry->mask,
4562 	};
4563 	struct nfs4_accessres res = {
4564 		.server = server,
4565 	};
4566 	struct rpc_message msg = {
4567 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4568 		.rpc_argp = &args,
4569 		.rpc_resp = &res,
4570 		.rpc_cred = cred,
4571 	};
4572 	int status = 0;
4573 
4574 	if (!nfs4_have_delegation(inode, FMODE_READ)) {
4575 		res.fattr = nfs_alloc_fattr();
4576 		if (res.fattr == NULL)
4577 			return -ENOMEM;
4578 		args.bitmask = server->cache_consistency_bitmask;
4579 	}
4580 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4581 	if (!status) {
4582 		nfs_access_set_mask(entry, res.access);
4583 		if (res.fattr)
4584 			nfs_refresh_inode(inode, res.fattr);
4585 	}
4586 	nfs_free_fattr(res.fattr);
4587 	return status;
4588 }
4589 
4590 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4591 			    const struct cred *cred)
4592 {
4593 	struct nfs4_exception exception = {
4594 		.interruptible = true,
4595 	};
4596 	int err;
4597 	do {
4598 		err = _nfs4_proc_access(inode, entry, cred);
4599 		trace_nfs4_access(inode, err);
4600 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
4601 				&exception);
4602 	} while (exception.retry);
4603 	return err;
4604 }
4605 
4606 /*
4607  * TODO: For the time being, we don't try to get any attributes
4608  * along with any of the zero-copy operations READ, READDIR,
4609  * READLINK, WRITE.
4610  *
4611  * In the case of the first three, we want to put the GETATTR
4612  * after the read-type operation -- this is because it is hard
4613  * to predict the length of a GETATTR response in v4, and thus
4614  * align the READ data correctly.  This means that the GETATTR
4615  * may end up partially falling into the page cache, and we should
4616  * shift it into the 'tail' of the xdr_buf before processing.
4617  * To do this efficiently, we need to know the total length
4618  * of data received, which doesn't seem to be available outside
4619  * of the RPC layer.
4620  *
4621  * In the case of WRITE, we also want to put the GETATTR after
4622  * the operation -- in this case because we want to make sure
4623  * we get the post-operation mtime and size.
4624  *
4625  * Both of these changes to the XDR layer would in fact be quite
4626  * minor, but I decided to leave them for a subsequent patch.
4627  */
4628 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4629 		unsigned int pgbase, unsigned int pglen)
4630 {
4631 	struct nfs4_readlink args = {
4632 		.fh       = NFS_FH(inode),
4633 		.pgbase	  = pgbase,
4634 		.pglen    = pglen,
4635 		.pages    = &page,
4636 	};
4637 	struct nfs4_readlink_res res;
4638 	struct rpc_message msg = {
4639 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4640 		.rpc_argp = &args,
4641 		.rpc_resp = &res,
4642 	};
4643 
4644 	return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4645 }
4646 
4647 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4648 		unsigned int pgbase, unsigned int pglen)
4649 {
4650 	struct nfs4_exception exception = {
4651 		.interruptible = true,
4652 	};
4653 	int err;
4654 	do {
4655 		err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4656 		trace_nfs4_readlink(inode, err);
4657 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
4658 				&exception);
4659 	} while (exception.retry);
4660 	return err;
4661 }
4662 
4663 /*
4664  * This is just for mknod.  open(O_CREAT) will always do ->open_context().
4665  */
4666 static int
4667 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4668 		 int flags)
4669 {
4670 	struct nfs_server *server = NFS_SERVER(dir);
4671 	struct nfs4_label l, *ilabel = NULL;
4672 	struct nfs_open_context *ctx;
4673 	struct nfs4_state *state;
4674 	int status = 0;
4675 
4676 	ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4677 	if (IS_ERR(ctx))
4678 		return PTR_ERR(ctx);
4679 
4680 	ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4681 
4682 	if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4683 		sattr->ia_mode &= ~current_umask();
4684 	state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4685 	if (IS_ERR(state)) {
4686 		status = PTR_ERR(state);
4687 		goto out;
4688 	}
4689 out:
4690 	nfs4_label_release_security(ilabel);
4691 	put_nfs_open_context(ctx);
4692 	return status;
4693 }
4694 
4695 static int
4696 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4697 {
4698 	struct nfs_server *server = NFS_SERVER(dir);
4699 	struct nfs_removeargs args = {
4700 		.fh = NFS_FH(dir),
4701 		.name = *name,
4702 	};
4703 	struct nfs_removeres res = {
4704 		.server = server,
4705 	};
4706 	struct rpc_message msg = {
4707 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4708 		.rpc_argp = &args,
4709 		.rpc_resp = &res,
4710 	};
4711 	unsigned long timestamp = jiffies;
4712 	int status;
4713 
4714 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4715 	if (status == 0) {
4716 		spin_lock(&dir->i_lock);
4717 		/* Removing a directory decrements nlink in the parent */
4718 		if (ftype == NF4DIR && dir->i_nlink > 2)
4719 			nfs4_dec_nlink_locked(dir);
4720 		nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4721 					      NFS_INO_INVALID_DATA);
4722 		spin_unlock(&dir->i_lock);
4723 	}
4724 	return status;
4725 }
4726 
4727 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4728 {
4729 	struct nfs4_exception exception = {
4730 		.interruptible = true,
4731 	};
4732 	struct inode *inode = d_inode(dentry);
4733 	int err;
4734 
4735 	if (inode) {
4736 		if (inode->i_nlink == 1)
4737 			nfs4_inode_return_delegation(inode);
4738 		else
4739 			nfs4_inode_make_writeable(inode);
4740 	}
4741 	do {
4742 		err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4743 		trace_nfs4_remove(dir, &dentry->d_name, err);
4744 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
4745 				&exception);
4746 	} while (exception.retry);
4747 	return err;
4748 }
4749 
4750 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4751 {
4752 	struct nfs4_exception exception = {
4753 		.interruptible = true,
4754 	};
4755 	int err;
4756 
4757 	do {
4758 		err = _nfs4_proc_remove(dir, name, NF4DIR);
4759 		trace_nfs4_remove(dir, name, err);
4760 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
4761 				&exception);
4762 	} while (exception.retry);
4763 	return err;
4764 }
4765 
4766 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4767 		struct dentry *dentry,
4768 		struct inode *inode)
4769 {
4770 	struct nfs_removeargs *args = msg->rpc_argp;
4771 	struct nfs_removeres *res = msg->rpc_resp;
4772 
4773 	res->server = NFS_SB(dentry->d_sb);
4774 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4775 	nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4776 
4777 	nfs_fattr_init(res->dir_attr);
4778 
4779 	if (inode) {
4780 		nfs4_inode_return_delegation(inode);
4781 		nfs_d_prune_case_insensitive_aliases(inode);
4782 	}
4783 }
4784 
4785 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4786 {
4787 	nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4788 			&data->args.seq_args,
4789 			&data->res.seq_res,
4790 			task);
4791 }
4792 
4793 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4794 {
4795 	struct nfs_unlinkdata *data = task->tk_calldata;
4796 	struct nfs_removeres *res = &data->res;
4797 
4798 	if (!nfs4_sequence_done(task, &res->seq_res))
4799 		return 0;
4800 	if (nfs4_async_handle_error(task, res->server, NULL,
4801 				    &data->timeout) == -EAGAIN)
4802 		return 0;
4803 	if (task->tk_status == 0)
4804 		nfs4_update_changeattr(dir, &res->cinfo,
4805 				res->dir_attr->time_start,
4806 				NFS_INO_INVALID_DATA);
4807 	return 1;
4808 }
4809 
4810 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4811 		struct dentry *old_dentry,
4812 		struct dentry *new_dentry)
4813 {
4814 	struct nfs_renameargs *arg = msg->rpc_argp;
4815 	struct nfs_renameres *res = msg->rpc_resp;
4816 	struct inode *old_inode = d_inode(old_dentry);
4817 	struct inode *new_inode = d_inode(new_dentry);
4818 
4819 	if (old_inode)
4820 		nfs4_inode_make_writeable(old_inode);
4821 	if (new_inode)
4822 		nfs4_inode_return_delegation(new_inode);
4823 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4824 	res->server = NFS_SB(old_dentry->d_sb);
4825 	nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4826 }
4827 
4828 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4829 {
4830 	nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4831 			&data->args.seq_args,
4832 			&data->res.seq_res,
4833 			task);
4834 }
4835 
4836 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4837 				 struct inode *new_dir)
4838 {
4839 	struct nfs_renamedata *data = task->tk_calldata;
4840 	struct nfs_renameres *res = &data->res;
4841 
4842 	if (!nfs4_sequence_done(task, &res->seq_res))
4843 		return 0;
4844 	if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4845 		return 0;
4846 
4847 	if (task->tk_status == 0) {
4848 		nfs_d_prune_case_insensitive_aliases(d_inode(data->old_dentry));
4849 		if (new_dir != old_dir) {
4850 			/* Note: If we moved a directory, nlink will change */
4851 			nfs4_update_changeattr(old_dir, &res->old_cinfo,
4852 					res->old_fattr->time_start,
4853 					NFS_INO_INVALID_NLINK |
4854 					    NFS_INO_INVALID_DATA);
4855 			nfs4_update_changeattr(new_dir, &res->new_cinfo,
4856 					res->new_fattr->time_start,
4857 					NFS_INO_INVALID_NLINK |
4858 					    NFS_INO_INVALID_DATA);
4859 		} else
4860 			nfs4_update_changeattr(old_dir, &res->old_cinfo,
4861 					res->old_fattr->time_start,
4862 					NFS_INO_INVALID_DATA);
4863 	}
4864 	return 1;
4865 }
4866 
4867 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4868 {
4869 	struct nfs_server *server = NFS_SERVER(inode);
4870 	__u32 bitmask[NFS4_BITMASK_SZ];
4871 	struct nfs4_link_arg arg = {
4872 		.fh     = NFS_FH(inode),
4873 		.dir_fh = NFS_FH(dir),
4874 		.name   = name,
4875 		.bitmask = bitmask,
4876 	};
4877 	struct nfs4_link_res res = {
4878 		.server = server,
4879 	};
4880 	struct rpc_message msg = {
4881 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4882 		.rpc_argp = &arg,
4883 		.rpc_resp = &res,
4884 	};
4885 	int status = -ENOMEM;
4886 
4887 	res.fattr = nfs_alloc_fattr_with_label(server);
4888 	if (res.fattr == NULL)
4889 		goto out;
4890 
4891 	nfs4_inode_make_writeable(inode);
4892 	nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.fattr->label), inode,
4893 				NFS_INO_INVALID_CHANGE);
4894 	status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4895 	if (!status) {
4896 		nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4897 				       NFS_INO_INVALID_DATA);
4898 		nfs4_inc_nlink(inode);
4899 		status = nfs_post_op_update_inode(inode, res.fattr);
4900 		if (!status)
4901 			nfs_setsecurity(inode, res.fattr);
4902 	}
4903 
4904 out:
4905 	nfs_free_fattr(res.fattr);
4906 	return status;
4907 }
4908 
4909 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4910 {
4911 	struct nfs4_exception exception = {
4912 		.interruptible = true,
4913 	};
4914 	int err;
4915 	do {
4916 		err = nfs4_handle_exception(NFS_SERVER(inode),
4917 				_nfs4_proc_link(inode, dir, name),
4918 				&exception);
4919 	} while (exception.retry);
4920 	return err;
4921 }
4922 
4923 struct nfs4_createdata {
4924 	struct rpc_message msg;
4925 	struct nfs4_create_arg arg;
4926 	struct nfs4_create_res res;
4927 	struct nfs_fh fh;
4928 	struct nfs_fattr fattr;
4929 };
4930 
4931 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4932 		const struct qstr *name, struct iattr *sattr, u32 ftype)
4933 {
4934 	struct nfs4_createdata *data;
4935 
4936 	data = kzalloc(sizeof(*data), GFP_KERNEL);
4937 	if (data != NULL) {
4938 		struct nfs_server *server = NFS_SERVER(dir);
4939 
4940 		data->fattr.label = nfs4_label_alloc(server, GFP_KERNEL);
4941 		if (IS_ERR(data->fattr.label))
4942 			goto out_free;
4943 
4944 		data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4945 		data->msg.rpc_argp = &data->arg;
4946 		data->msg.rpc_resp = &data->res;
4947 		data->arg.dir_fh = NFS_FH(dir);
4948 		data->arg.server = server;
4949 		data->arg.name = name;
4950 		data->arg.attrs = sattr;
4951 		data->arg.ftype = ftype;
4952 		data->arg.bitmask = nfs4_bitmask(server, data->fattr.label);
4953 		data->arg.umask = current_umask();
4954 		data->res.server = server;
4955 		data->res.fh = &data->fh;
4956 		data->res.fattr = &data->fattr;
4957 		nfs_fattr_init(data->res.fattr);
4958 	}
4959 	return data;
4960 out_free:
4961 	kfree(data);
4962 	return NULL;
4963 }
4964 
4965 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4966 {
4967 	int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4968 				    &data->arg.seq_args, &data->res.seq_res, 1);
4969 	if (status == 0) {
4970 		spin_lock(&dir->i_lock);
4971 		/* Creating a directory bumps nlink in the parent */
4972 		if (data->arg.ftype == NF4DIR)
4973 			nfs4_inc_nlink_locked(dir);
4974 		nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4975 					      data->res.fattr->time_start,
4976 					      NFS_INO_INVALID_DATA);
4977 		spin_unlock(&dir->i_lock);
4978 		status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
4979 	}
4980 	return status;
4981 }
4982 
4983 static void nfs4_free_createdata(struct nfs4_createdata *data)
4984 {
4985 	nfs4_label_free(data->fattr.label);
4986 	kfree(data);
4987 }
4988 
4989 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4990 		struct page *page, unsigned int len, struct iattr *sattr,
4991 		struct nfs4_label *label)
4992 {
4993 	struct nfs4_createdata *data;
4994 	int status = -ENAMETOOLONG;
4995 
4996 	if (len > NFS4_MAXPATHLEN)
4997 		goto out;
4998 
4999 	status = -ENOMEM;
5000 	data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
5001 	if (data == NULL)
5002 		goto out;
5003 
5004 	data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
5005 	data->arg.u.symlink.pages = &page;
5006 	data->arg.u.symlink.len = len;
5007 	data->arg.label = label;
5008 
5009 	status = nfs4_do_create(dir, dentry, data);
5010 
5011 	nfs4_free_createdata(data);
5012 out:
5013 	return status;
5014 }
5015 
5016 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5017 		struct page *page, unsigned int len, struct iattr *sattr)
5018 {
5019 	struct nfs4_exception exception = {
5020 		.interruptible = true,
5021 	};
5022 	struct nfs4_label l, *label = NULL;
5023 	int err;
5024 
5025 	label = nfs4_label_init_security(dir, dentry, sattr, &l);
5026 
5027 	do {
5028 		err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
5029 		trace_nfs4_symlink(dir, &dentry->d_name, err);
5030 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
5031 				&exception);
5032 	} while (exception.retry);
5033 
5034 	nfs4_label_release_security(label);
5035 	return err;
5036 }
5037 
5038 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5039 		struct iattr *sattr, struct nfs4_label *label)
5040 {
5041 	struct nfs4_createdata *data;
5042 	int status = -ENOMEM;
5043 
5044 	data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
5045 	if (data == NULL)
5046 		goto out;
5047 
5048 	data->arg.label = label;
5049 	status = nfs4_do_create(dir, dentry, data);
5050 
5051 	nfs4_free_createdata(data);
5052 out:
5053 	return status;
5054 }
5055 
5056 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5057 		struct iattr *sattr)
5058 {
5059 	struct nfs_server *server = NFS_SERVER(dir);
5060 	struct nfs4_exception exception = {
5061 		.interruptible = true,
5062 	};
5063 	struct nfs4_label l, *label = NULL;
5064 	int err;
5065 
5066 	label = nfs4_label_init_security(dir, dentry, sattr, &l);
5067 
5068 	if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5069 		sattr->ia_mode &= ~current_umask();
5070 	do {
5071 		err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
5072 		trace_nfs4_mkdir(dir, &dentry->d_name, err);
5073 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
5074 				&exception);
5075 	} while (exception.retry);
5076 	nfs4_label_release_security(label);
5077 
5078 	return err;
5079 }
5080 
5081 static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
5082 			      struct nfs_readdir_res *nr_res)
5083 {
5084 	struct inode		*dir = d_inode(nr_arg->dentry);
5085 	struct nfs_server	*server = NFS_SERVER(dir);
5086 	struct nfs4_readdir_arg args = {
5087 		.fh = NFS_FH(dir),
5088 		.pages = nr_arg->pages,
5089 		.pgbase = 0,
5090 		.count = nr_arg->page_len,
5091 		.plus = nr_arg->plus,
5092 	};
5093 	struct nfs4_readdir_res res;
5094 	struct rpc_message msg = {
5095 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5096 		.rpc_argp = &args,
5097 		.rpc_resp = &res,
5098 		.rpc_cred = nr_arg->cred,
5099 	};
5100 	int			status;
5101 
5102 	dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
5103 		nr_arg->dentry, (unsigned long long)nr_arg->cookie);
5104 	if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5105 		args.bitmask = server->attr_bitmask_nl;
5106 	else
5107 		args.bitmask = server->attr_bitmask;
5108 
5109 	nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
5110 	res.pgbase = args.pgbase;
5111 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5112 			&res.seq_res, 0);
5113 	if (status >= 0) {
5114 		memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
5115 		status += args.pgbase;
5116 	}
5117 
5118 	nfs_invalidate_atime(dir);
5119 
5120 	dprintk("%s: returns %d\n", __func__, status);
5121 	return status;
5122 }
5123 
5124 static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5125 			     struct nfs_readdir_res *res)
5126 {
5127 	struct nfs4_exception exception = {
5128 		.interruptible = true,
5129 	};
5130 	int err;
5131 	do {
5132 		err = _nfs4_proc_readdir(arg, res);
5133 		trace_nfs4_readdir(d_inode(arg->dentry), err);
5134 		err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5135 					    err, &exception);
5136 	} while (exception.retry);
5137 	return err;
5138 }
5139 
5140 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5141 		struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5142 {
5143 	struct nfs4_createdata *data;
5144 	int mode = sattr->ia_mode;
5145 	int status = -ENOMEM;
5146 
5147 	data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5148 	if (data == NULL)
5149 		goto out;
5150 
5151 	if (S_ISFIFO(mode))
5152 		data->arg.ftype = NF4FIFO;
5153 	else if (S_ISBLK(mode)) {
5154 		data->arg.ftype = NF4BLK;
5155 		data->arg.u.device.specdata1 = MAJOR(rdev);
5156 		data->arg.u.device.specdata2 = MINOR(rdev);
5157 	}
5158 	else if (S_ISCHR(mode)) {
5159 		data->arg.ftype = NF4CHR;
5160 		data->arg.u.device.specdata1 = MAJOR(rdev);
5161 		data->arg.u.device.specdata2 = MINOR(rdev);
5162 	} else if (!S_ISSOCK(mode)) {
5163 		status = -EINVAL;
5164 		goto out_free;
5165 	}
5166 
5167 	data->arg.label = label;
5168 	status = nfs4_do_create(dir, dentry, data);
5169 out_free:
5170 	nfs4_free_createdata(data);
5171 out:
5172 	return status;
5173 }
5174 
5175 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5176 		struct iattr *sattr, dev_t rdev)
5177 {
5178 	struct nfs_server *server = NFS_SERVER(dir);
5179 	struct nfs4_exception exception = {
5180 		.interruptible = true,
5181 	};
5182 	struct nfs4_label l, *label = NULL;
5183 	int err;
5184 
5185 	label = nfs4_label_init_security(dir, dentry, sattr, &l);
5186 
5187 	if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5188 		sattr->ia_mode &= ~current_umask();
5189 	do {
5190 		err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5191 		trace_nfs4_mknod(dir, &dentry->d_name, err);
5192 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
5193 				&exception);
5194 	} while (exception.retry);
5195 
5196 	nfs4_label_release_security(label);
5197 
5198 	return err;
5199 }
5200 
5201 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5202 		 struct nfs_fsstat *fsstat)
5203 {
5204 	struct nfs4_statfs_arg args = {
5205 		.fh = fhandle,
5206 		.bitmask = server->attr_bitmask,
5207 	};
5208 	struct nfs4_statfs_res res = {
5209 		.fsstat = fsstat,
5210 	};
5211 	struct rpc_message msg = {
5212 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5213 		.rpc_argp = &args,
5214 		.rpc_resp = &res,
5215 	};
5216 
5217 	nfs_fattr_init(fsstat->fattr);
5218 	return  nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5219 }
5220 
5221 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5222 {
5223 	struct nfs4_exception exception = {
5224 		.interruptible = true,
5225 	};
5226 	int err;
5227 	do {
5228 		err = nfs4_handle_exception(server,
5229 				_nfs4_proc_statfs(server, fhandle, fsstat),
5230 				&exception);
5231 	} while (exception.retry);
5232 	return err;
5233 }
5234 
5235 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5236 		struct nfs_fsinfo *fsinfo)
5237 {
5238 	struct nfs4_fsinfo_arg args = {
5239 		.fh = fhandle,
5240 		.bitmask = server->attr_bitmask,
5241 	};
5242 	struct nfs4_fsinfo_res res = {
5243 		.fsinfo = fsinfo,
5244 	};
5245 	struct rpc_message msg = {
5246 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5247 		.rpc_argp = &args,
5248 		.rpc_resp = &res,
5249 	};
5250 
5251 	return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5252 }
5253 
5254 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5255 {
5256 	struct nfs4_exception exception = {
5257 		.interruptible = true,
5258 	};
5259 	int err;
5260 
5261 	do {
5262 		err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5263 		trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5264 		if (err == 0) {
5265 			nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5266 			break;
5267 		}
5268 		err = nfs4_handle_exception(server, err, &exception);
5269 	} while (exception.retry);
5270 	return err;
5271 }
5272 
5273 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5274 {
5275 	int error;
5276 
5277 	nfs_fattr_init(fsinfo->fattr);
5278 	error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5279 	if (error == 0) {
5280 		/* block layout checks this! */
5281 		server->pnfs_blksize = fsinfo->blksize;
5282 		set_pnfs_layoutdriver(server, fhandle, fsinfo);
5283 	}
5284 
5285 	return error;
5286 }
5287 
5288 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5289 		struct nfs_pathconf *pathconf)
5290 {
5291 	struct nfs4_pathconf_arg args = {
5292 		.fh = fhandle,
5293 		.bitmask = server->attr_bitmask,
5294 	};
5295 	struct nfs4_pathconf_res res = {
5296 		.pathconf = pathconf,
5297 	};
5298 	struct rpc_message msg = {
5299 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5300 		.rpc_argp = &args,
5301 		.rpc_resp = &res,
5302 	};
5303 
5304 	/* None of the pathconf attributes are mandatory to implement */
5305 	if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5306 		memset(pathconf, 0, sizeof(*pathconf));
5307 		return 0;
5308 	}
5309 
5310 	nfs_fattr_init(pathconf->fattr);
5311 	return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5312 }
5313 
5314 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5315 		struct nfs_pathconf *pathconf)
5316 {
5317 	struct nfs4_exception exception = {
5318 		.interruptible = true,
5319 	};
5320 	int err;
5321 
5322 	do {
5323 		err = nfs4_handle_exception(server,
5324 				_nfs4_proc_pathconf(server, fhandle, pathconf),
5325 				&exception);
5326 	} while (exception.retry);
5327 	return err;
5328 }
5329 
5330 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5331 		const struct nfs_open_context *ctx,
5332 		const struct nfs_lock_context *l_ctx,
5333 		fmode_t fmode)
5334 {
5335 	return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5336 }
5337 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5338 
5339 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5340 		const struct nfs_open_context *ctx,
5341 		const struct nfs_lock_context *l_ctx,
5342 		fmode_t fmode)
5343 {
5344 	nfs4_stateid _current_stateid;
5345 
5346 	/* If the current stateid represents a lost lock, then exit */
5347 	if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5348 		return true;
5349 	return nfs4_stateid_match(stateid, &_current_stateid);
5350 }
5351 
5352 static bool nfs4_error_stateid_expired(int err)
5353 {
5354 	switch (err) {
5355 	case -NFS4ERR_DELEG_REVOKED:
5356 	case -NFS4ERR_ADMIN_REVOKED:
5357 	case -NFS4ERR_BAD_STATEID:
5358 	case -NFS4ERR_STALE_STATEID:
5359 	case -NFS4ERR_OLD_STATEID:
5360 	case -NFS4ERR_OPENMODE:
5361 	case -NFS4ERR_EXPIRED:
5362 		return true;
5363 	}
5364 	return false;
5365 }
5366 
5367 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5368 {
5369 	struct nfs_server *server = NFS_SERVER(hdr->inode);
5370 
5371 	trace_nfs4_read(hdr, task->tk_status);
5372 	if (task->tk_status < 0) {
5373 		struct nfs4_exception exception = {
5374 			.inode = hdr->inode,
5375 			.state = hdr->args.context->state,
5376 			.stateid = &hdr->args.stateid,
5377 		};
5378 		task->tk_status = nfs4_async_handle_exception(task,
5379 				server, task->tk_status, &exception);
5380 		if (exception.retry) {
5381 			rpc_restart_call_prepare(task);
5382 			return -EAGAIN;
5383 		}
5384 	}
5385 
5386 	if (task->tk_status > 0)
5387 		renew_lease(server, hdr->timestamp);
5388 	return 0;
5389 }
5390 
5391 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5392 		struct nfs_pgio_args *args)
5393 {
5394 
5395 	if (!nfs4_error_stateid_expired(task->tk_status) ||
5396 		nfs4_stateid_is_current(&args->stateid,
5397 				args->context,
5398 				args->lock_context,
5399 				FMODE_READ))
5400 		return false;
5401 	rpc_restart_call_prepare(task);
5402 	return true;
5403 }
5404 
5405 static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5406 					 struct nfs_pgio_header *hdr)
5407 {
5408 	struct nfs_server *server = NFS_SERVER(hdr->inode);
5409 	struct rpc_message *msg = &task->tk_msg;
5410 
5411 	if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5412 	    server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5413 		server->caps &= ~NFS_CAP_READ_PLUS;
5414 		msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5415 		rpc_restart_call_prepare(task);
5416 		return true;
5417 	}
5418 	return false;
5419 }
5420 
5421 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5422 {
5423 	if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5424 		return -EAGAIN;
5425 	if (nfs4_read_stateid_changed(task, &hdr->args))
5426 		return -EAGAIN;
5427 	if (nfs4_read_plus_not_supported(task, hdr))
5428 		return -EAGAIN;
5429 	if (task->tk_status > 0)
5430 		nfs_invalidate_atime(hdr->inode);
5431 	return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5432 				    nfs4_read_done_cb(task, hdr);
5433 }
5434 
5435 #if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
5436 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5437 				    struct rpc_message *msg)
5438 {
5439 	/* Note: We don't use READ_PLUS with pNFS yet */
5440 	if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
5441 		msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5442 }
5443 #else
5444 static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5445 				    struct rpc_message *msg)
5446 {
5447 }
5448 #endif /* CONFIG_NFS_V4_2 */
5449 
5450 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5451 				 struct rpc_message *msg)
5452 {
5453 	hdr->timestamp   = jiffies;
5454 	if (!hdr->pgio_done_cb)
5455 		hdr->pgio_done_cb = nfs4_read_done_cb;
5456 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5457 	nfs42_read_plus_support(hdr, msg);
5458 	nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5459 }
5460 
5461 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5462 				      struct nfs_pgio_header *hdr)
5463 {
5464 	if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5465 			&hdr->args.seq_args,
5466 			&hdr->res.seq_res,
5467 			task))
5468 		return 0;
5469 	if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5470 				hdr->args.lock_context,
5471 				hdr->rw_mode) == -EIO)
5472 		return -EIO;
5473 	if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5474 		return -EIO;
5475 	return 0;
5476 }
5477 
5478 static int nfs4_write_done_cb(struct rpc_task *task,
5479 			      struct nfs_pgio_header *hdr)
5480 {
5481 	struct inode *inode = hdr->inode;
5482 
5483 	trace_nfs4_write(hdr, task->tk_status);
5484 	if (task->tk_status < 0) {
5485 		struct nfs4_exception exception = {
5486 			.inode = hdr->inode,
5487 			.state = hdr->args.context->state,
5488 			.stateid = &hdr->args.stateid,
5489 		};
5490 		task->tk_status = nfs4_async_handle_exception(task,
5491 				NFS_SERVER(inode), task->tk_status,
5492 				&exception);
5493 		if (exception.retry) {
5494 			rpc_restart_call_prepare(task);
5495 			return -EAGAIN;
5496 		}
5497 	}
5498 	if (task->tk_status >= 0) {
5499 		renew_lease(NFS_SERVER(inode), hdr->timestamp);
5500 		nfs_writeback_update_inode(hdr);
5501 	}
5502 	return 0;
5503 }
5504 
5505 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5506 		struct nfs_pgio_args *args)
5507 {
5508 
5509 	if (!nfs4_error_stateid_expired(task->tk_status) ||
5510 		nfs4_stateid_is_current(&args->stateid,
5511 				args->context,
5512 				args->lock_context,
5513 				FMODE_WRITE))
5514 		return false;
5515 	rpc_restart_call_prepare(task);
5516 	return true;
5517 }
5518 
5519 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5520 {
5521 	if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5522 		return -EAGAIN;
5523 	if (nfs4_write_stateid_changed(task, &hdr->args))
5524 		return -EAGAIN;
5525 	return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5526 		nfs4_write_done_cb(task, hdr);
5527 }
5528 
5529 static
5530 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5531 {
5532 	/* Don't request attributes for pNFS or O_DIRECT writes */
5533 	if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5534 		return false;
5535 	/* Otherwise, request attributes if and only if we don't hold
5536 	 * a delegation
5537 	 */
5538 	return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5539 }
5540 
5541 void nfs4_bitmask_set(__u32 bitmask[], const __u32 src[],
5542 		      struct inode *inode, unsigned long cache_validity)
5543 {
5544 	struct nfs_server *server = NFS_SERVER(inode);
5545 	unsigned int i;
5546 
5547 	memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
5548 	cache_validity |= READ_ONCE(NFS_I(inode)->cache_validity);
5549 
5550 	if (cache_validity & NFS_INO_INVALID_CHANGE)
5551 		bitmask[0] |= FATTR4_WORD0_CHANGE;
5552 	if (cache_validity & NFS_INO_INVALID_ATIME)
5553 		bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5554 	if (cache_validity & NFS_INO_INVALID_MODE)
5555 		bitmask[1] |= FATTR4_WORD1_MODE;
5556 	if (cache_validity & NFS_INO_INVALID_OTHER)
5557 		bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
5558 	if (cache_validity & NFS_INO_INVALID_NLINK)
5559 		bitmask[1] |= FATTR4_WORD1_NUMLINKS;
5560 	if (cache_validity & NFS_INO_INVALID_CTIME)
5561 		bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
5562 	if (cache_validity & NFS_INO_INVALID_MTIME)
5563 		bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5564 	if (cache_validity & NFS_INO_INVALID_BLOCKS)
5565 		bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5566 
5567 	if (cache_validity & NFS_INO_INVALID_SIZE)
5568 		bitmask[0] |= FATTR4_WORD0_SIZE;
5569 
5570 	for (i = 0; i < NFS4_BITMASK_SZ; i++)
5571 		bitmask[i] &= server->attr_bitmask[i];
5572 }
5573 
5574 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5575 				  struct rpc_message *msg,
5576 				  struct rpc_clnt **clnt)
5577 {
5578 	struct nfs_server *server = NFS_SERVER(hdr->inode);
5579 
5580 	if (!nfs4_write_need_cache_consistency_data(hdr)) {
5581 		hdr->args.bitmask = NULL;
5582 		hdr->res.fattr = NULL;
5583 	} else {
5584 		nfs4_bitmask_set(hdr->args.bitmask_store,
5585 				 server->cache_consistency_bitmask,
5586 				 hdr->inode, NFS_INO_INVALID_BLOCKS);
5587 		hdr->args.bitmask = hdr->args.bitmask_store;
5588 	}
5589 
5590 	if (!hdr->pgio_done_cb)
5591 		hdr->pgio_done_cb = nfs4_write_done_cb;
5592 	hdr->res.server = server;
5593 	hdr->timestamp   = jiffies;
5594 
5595 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5596 	nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5597 	nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5598 }
5599 
5600 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5601 {
5602 	nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5603 			&data->args.seq_args,
5604 			&data->res.seq_res,
5605 			task);
5606 }
5607 
5608 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5609 {
5610 	struct inode *inode = data->inode;
5611 
5612 	trace_nfs4_commit(data, task->tk_status);
5613 	if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5614 				    NULL, NULL) == -EAGAIN) {
5615 		rpc_restart_call_prepare(task);
5616 		return -EAGAIN;
5617 	}
5618 	return 0;
5619 }
5620 
5621 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5622 {
5623 	if (!nfs4_sequence_done(task, &data->res.seq_res))
5624 		return -EAGAIN;
5625 	return data->commit_done_cb(task, data);
5626 }
5627 
5628 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5629 				   struct rpc_clnt **clnt)
5630 {
5631 	struct nfs_server *server = NFS_SERVER(data->inode);
5632 
5633 	if (data->commit_done_cb == NULL)
5634 		data->commit_done_cb = nfs4_commit_done_cb;
5635 	data->res.server = server;
5636 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5637 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5638 	nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5639 }
5640 
5641 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5642 				struct nfs_commitres *res)
5643 {
5644 	struct inode *dst_inode = file_inode(dst);
5645 	struct nfs_server *server = NFS_SERVER(dst_inode);
5646 	struct rpc_message msg = {
5647 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5648 		.rpc_argp = args,
5649 		.rpc_resp = res,
5650 	};
5651 
5652 	args->fh = NFS_FH(dst_inode);
5653 	return nfs4_call_sync(server->client, server, &msg,
5654 			&args->seq_args, &res->seq_res, 1);
5655 }
5656 
5657 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5658 {
5659 	struct nfs_commitargs args = {
5660 		.offset = offset,
5661 		.count = count,
5662 	};
5663 	struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5664 	struct nfs4_exception exception = { };
5665 	int status;
5666 
5667 	do {
5668 		status = _nfs4_proc_commit(dst, &args, res);
5669 		status = nfs4_handle_exception(dst_server, status, &exception);
5670 	} while (exception.retry);
5671 
5672 	return status;
5673 }
5674 
5675 struct nfs4_renewdata {
5676 	struct nfs_client	*client;
5677 	unsigned long		timestamp;
5678 };
5679 
5680 /*
5681  * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5682  * standalone procedure for queueing an asynchronous RENEW.
5683  */
5684 static void nfs4_renew_release(void *calldata)
5685 {
5686 	struct nfs4_renewdata *data = calldata;
5687 	struct nfs_client *clp = data->client;
5688 
5689 	if (refcount_read(&clp->cl_count) > 1)
5690 		nfs4_schedule_state_renewal(clp);
5691 	nfs_put_client(clp);
5692 	kfree(data);
5693 }
5694 
5695 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5696 {
5697 	struct nfs4_renewdata *data = calldata;
5698 	struct nfs_client *clp = data->client;
5699 	unsigned long timestamp = data->timestamp;
5700 
5701 	trace_nfs4_renew_async(clp, task->tk_status);
5702 	switch (task->tk_status) {
5703 	case 0:
5704 		break;
5705 	case -NFS4ERR_LEASE_MOVED:
5706 		nfs4_schedule_lease_moved_recovery(clp);
5707 		break;
5708 	default:
5709 		/* Unless we're shutting down, schedule state recovery! */
5710 		if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5711 			return;
5712 		if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5713 			nfs4_schedule_lease_recovery(clp);
5714 			return;
5715 		}
5716 		nfs4_schedule_path_down_recovery(clp);
5717 	}
5718 	do_renew_lease(clp, timestamp);
5719 }
5720 
5721 static const struct rpc_call_ops nfs4_renew_ops = {
5722 	.rpc_call_done = nfs4_renew_done,
5723 	.rpc_release = nfs4_renew_release,
5724 };
5725 
5726 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5727 {
5728 	struct rpc_message msg = {
5729 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5730 		.rpc_argp	= clp,
5731 		.rpc_cred	= cred,
5732 	};
5733 	struct nfs4_renewdata *data;
5734 
5735 	if (renew_flags == 0)
5736 		return 0;
5737 	if (!refcount_inc_not_zero(&clp->cl_count))
5738 		return -EIO;
5739 	data = kmalloc(sizeof(*data), GFP_NOFS);
5740 	if (data == NULL) {
5741 		nfs_put_client(clp);
5742 		return -ENOMEM;
5743 	}
5744 	data->client = clp;
5745 	data->timestamp = jiffies;
5746 	return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5747 			&nfs4_renew_ops, data);
5748 }
5749 
5750 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5751 {
5752 	struct rpc_message msg = {
5753 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5754 		.rpc_argp	= clp,
5755 		.rpc_cred	= cred,
5756 	};
5757 	unsigned long now = jiffies;
5758 	int status;
5759 
5760 	status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5761 	if (status < 0)
5762 		return status;
5763 	do_renew_lease(clp, now);
5764 	return 0;
5765 }
5766 
5767 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5768 {
5769 	return server->caps & NFS_CAP_ACLS;
5770 }
5771 
5772 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5773  * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5774  * the stack.
5775  */
5776 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5777 
5778 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5779 		struct page **pages)
5780 {
5781 	struct page *newpage, **spages;
5782 	int rc = 0;
5783 	size_t len;
5784 	spages = pages;
5785 
5786 	do {
5787 		len = min_t(size_t, PAGE_SIZE, buflen);
5788 		newpage = alloc_page(GFP_KERNEL);
5789 
5790 		if (newpage == NULL)
5791 			goto unwind;
5792 		memcpy(page_address(newpage), buf, len);
5793 		buf += len;
5794 		buflen -= len;
5795 		*pages++ = newpage;
5796 		rc++;
5797 	} while (buflen != 0);
5798 
5799 	return rc;
5800 
5801 unwind:
5802 	for(; rc > 0; rc--)
5803 		__free_page(spages[rc-1]);
5804 	return -ENOMEM;
5805 }
5806 
5807 struct nfs4_cached_acl {
5808 	int cached;
5809 	size_t len;
5810 	char data[];
5811 };
5812 
5813 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5814 {
5815 	struct nfs_inode *nfsi = NFS_I(inode);
5816 
5817 	spin_lock(&inode->i_lock);
5818 	kfree(nfsi->nfs4_acl);
5819 	nfsi->nfs4_acl = acl;
5820 	spin_unlock(&inode->i_lock);
5821 }
5822 
5823 static void nfs4_zap_acl_attr(struct inode *inode)
5824 {
5825 	nfs4_set_cached_acl(inode, NULL);
5826 }
5827 
5828 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5829 {
5830 	struct nfs_inode *nfsi = NFS_I(inode);
5831 	struct nfs4_cached_acl *acl;
5832 	int ret = -ENOENT;
5833 
5834 	spin_lock(&inode->i_lock);
5835 	acl = nfsi->nfs4_acl;
5836 	if (acl == NULL)
5837 		goto out;
5838 	if (buf == NULL) /* user is just asking for length */
5839 		goto out_len;
5840 	if (acl->cached == 0)
5841 		goto out;
5842 	ret = -ERANGE; /* see getxattr(2) man page */
5843 	if (acl->len > buflen)
5844 		goto out;
5845 	memcpy(buf, acl->data, acl->len);
5846 out_len:
5847 	ret = acl->len;
5848 out:
5849 	spin_unlock(&inode->i_lock);
5850 	return ret;
5851 }
5852 
5853 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5854 {
5855 	struct nfs4_cached_acl *acl;
5856 	size_t buflen = sizeof(*acl) + acl_len;
5857 
5858 	if (buflen <= PAGE_SIZE) {
5859 		acl = kmalloc(buflen, GFP_KERNEL);
5860 		if (acl == NULL)
5861 			goto out;
5862 		acl->cached = 1;
5863 		_copy_from_pages(acl->data, pages, pgbase, acl_len);
5864 	} else {
5865 		acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5866 		if (acl == NULL)
5867 			goto out;
5868 		acl->cached = 0;
5869 	}
5870 	acl->len = acl_len;
5871 out:
5872 	nfs4_set_cached_acl(inode, acl);
5873 }
5874 
5875 /*
5876  * The getxattr API returns the required buffer length when called with a
5877  * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5878  * the required buf.  On a NULL buf, we send a page of data to the server
5879  * guessing that the ACL request can be serviced by a page. If so, we cache
5880  * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5881  * the cache. If not so, we throw away the page, and cache the required
5882  * length. The next getxattr call will then produce another round trip to
5883  * the server, this time with the input buf of the required size.
5884  */
5885 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5886 {
5887 	struct page **pages;
5888 	struct nfs_getaclargs args = {
5889 		.fh = NFS_FH(inode),
5890 		.acl_len = buflen,
5891 	};
5892 	struct nfs_getaclres res = {
5893 		.acl_len = buflen,
5894 	};
5895 	struct rpc_message msg = {
5896 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5897 		.rpc_argp = &args,
5898 		.rpc_resp = &res,
5899 	};
5900 	unsigned int npages;
5901 	int ret = -ENOMEM, i;
5902 	struct nfs_server *server = NFS_SERVER(inode);
5903 
5904 	if (buflen == 0)
5905 		buflen = server->rsize;
5906 
5907 	npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5908 	pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5909 	if (!pages)
5910 		return -ENOMEM;
5911 
5912 	args.acl_pages = pages;
5913 
5914 	for (i = 0; i < npages; i++) {
5915 		pages[i] = alloc_page(GFP_KERNEL);
5916 		if (!pages[i])
5917 			goto out_free;
5918 	}
5919 
5920 	/* for decoding across pages */
5921 	res.acl_scratch = alloc_page(GFP_KERNEL);
5922 	if (!res.acl_scratch)
5923 		goto out_free;
5924 
5925 	args.acl_len = npages * PAGE_SIZE;
5926 
5927 	dprintk("%s  buf %p buflen %zu npages %d args.acl_len %zu\n",
5928 		__func__, buf, buflen, npages, args.acl_len);
5929 	ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5930 			     &msg, &args.seq_args, &res.seq_res, 0);
5931 	if (ret)
5932 		goto out_free;
5933 
5934 	/* Handle the case where the passed-in buffer is too short */
5935 	if (res.acl_flags & NFS4_ACL_TRUNC) {
5936 		/* Did the user only issue a request for the acl length? */
5937 		if (buf == NULL)
5938 			goto out_ok;
5939 		ret = -ERANGE;
5940 		goto out_free;
5941 	}
5942 	nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5943 	if (buf) {
5944 		if (res.acl_len > buflen) {
5945 			ret = -ERANGE;
5946 			goto out_free;
5947 		}
5948 		_copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5949 	}
5950 out_ok:
5951 	ret = res.acl_len;
5952 out_free:
5953 	for (i = 0; i < npages; i++)
5954 		if (pages[i])
5955 			__free_page(pages[i]);
5956 	if (res.acl_scratch)
5957 		__free_page(res.acl_scratch);
5958 	kfree(pages);
5959 	return ret;
5960 }
5961 
5962 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5963 {
5964 	struct nfs4_exception exception = {
5965 		.interruptible = true,
5966 	};
5967 	ssize_t ret;
5968 	do {
5969 		ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5970 		trace_nfs4_get_acl(inode, ret);
5971 		if (ret >= 0)
5972 			break;
5973 		ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5974 	} while (exception.retry);
5975 	return ret;
5976 }
5977 
5978 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5979 {
5980 	struct nfs_server *server = NFS_SERVER(inode);
5981 	int ret;
5982 
5983 	if (!nfs4_server_supports_acls(server))
5984 		return -EOPNOTSUPP;
5985 	ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
5986 	if (ret < 0)
5987 		return ret;
5988 	if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5989 		nfs_zap_acl_cache(inode);
5990 	ret = nfs4_read_cached_acl(inode, buf, buflen);
5991 	if (ret != -ENOENT)
5992 		/* -ENOENT is returned if there is no ACL or if there is an ACL
5993 		 * but no cached acl data, just the acl length */
5994 		return ret;
5995 	return nfs4_get_acl_uncached(inode, buf, buflen);
5996 }
5997 
5998 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5999 {
6000 	struct nfs_server *server = NFS_SERVER(inode);
6001 	struct page *pages[NFS4ACL_MAXPAGES];
6002 	struct nfs_setaclargs arg = {
6003 		.fh		= NFS_FH(inode),
6004 		.acl_pages	= pages,
6005 		.acl_len	= buflen,
6006 	};
6007 	struct nfs_setaclres res;
6008 	struct rpc_message msg = {
6009 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_SETACL],
6010 		.rpc_argp	= &arg,
6011 		.rpc_resp	= &res,
6012 	};
6013 	unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
6014 	int ret, i;
6015 
6016 	/* You can't remove system.nfs4_acl: */
6017 	if (buflen == 0)
6018 		return -EINVAL;
6019 	if (!nfs4_server_supports_acls(server))
6020 		return -EOPNOTSUPP;
6021 	if (npages > ARRAY_SIZE(pages))
6022 		return -ERANGE;
6023 	i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
6024 	if (i < 0)
6025 		return i;
6026 	nfs4_inode_make_writeable(inode);
6027 	ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6028 
6029 	/*
6030 	 * Free each page after tx, so the only ref left is
6031 	 * held by the network stack
6032 	 */
6033 	for (; i > 0; i--)
6034 		put_page(pages[i-1]);
6035 
6036 	/*
6037 	 * Acl update can result in inode attribute update.
6038 	 * so mark the attribute cache invalid.
6039 	 */
6040 	spin_lock(&inode->i_lock);
6041 	nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
6042 					     NFS_INO_INVALID_CTIME |
6043 					     NFS_INO_REVAL_FORCED);
6044 	spin_unlock(&inode->i_lock);
6045 	nfs_access_zap_cache(inode);
6046 	nfs_zap_acl_cache(inode);
6047 	return ret;
6048 }
6049 
6050 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
6051 {
6052 	struct nfs4_exception exception = { };
6053 	int err;
6054 	do {
6055 		err = __nfs4_proc_set_acl(inode, buf, buflen);
6056 		trace_nfs4_set_acl(inode, err);
6057 		if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
6058 			/*
6059 			 * no need to retry since the kernel
6060 			 * isn't involved in encoding the ACEs.
6061 			 */
6062 			err = -EINVAL;
6063 			break;
6064 		}
6065 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
6066 				&exception);
6067 	} while (exception.retry);
6068 	return err;
6069 }
6070 
6071 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6072 static int _nfs4_get_security_label(struct inode *inode, void *buf,
6073 					size_t buflen)
6074 {
6075 	struct nfs_server *server = NFS_SERVER(inode);
6076 	struct nfs4_label label = {0, 0, buflen, buf};
6077 
6078 	u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6079 	struct nfs_fattr fattr = {
6080 		.label = &label,
6081 	};
6082 	struct nfs4_getattr_arg arg = {
6083 		.fh		= NFS_FH(inode),
6084 		.bitmask	= bitmask,
6085 	};
6086 	struct nfs4_getattr_res res = {
6087 		.fattr		= &fattr,
6088 		.server		= server,
6089 	};
6090 	struct rpc_message msg = {
6091 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
6092 		.rpc_argp	= &arg,
6093 		.rpc_resp	= &res,
6094 	};
6095 	int ret;
6096 
6097 	nfs_fattr_init(&fattr);
6098 
6099 	ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
6100 	if (ret)
6101 		return ret;
6102 	if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6103 		return -ENOENT;
6104 	return label.len;
6105 }
6106 
6107 static int nfs4_get_security_label(struct inode *inode, void *buf,
6108 					size_t buflen)
6109 {
6110 	struct nfs4_exception exception = {
6111 		.interruptible = true,
6112 	};
6113 	int err;
6114 
6115 	if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6116 		return -EOPNOTSUPP;
6117 
6118 	do {
6119 		err = _nfs4_get_security_label(inode, buf, buflen);
6120 		trace_nfs4_get_security_label(inode, err);
6121 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
6122 				&exception);
6123 	} while (exception.retry);
6124 	return err;
6125 }
6126 
6127 static int _nfs4_do_set_security_label(struct inode *inode,
6128 		struct nfs4_label *ilabel,
6129 		struct nfs_fattr *fattr)
6130 {
6131 
6132 	struct iattr sattr = {0};
6133 	struct nfs_server *server = NFS_SERVER(inode);
6134 	const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6135 	struct nfs_setattrargs arg = {
6136 		.fh		= NFS_FH(inode),
6137 		.iap		= &sattr,
6138 		.server		= server,
6139 		.bitmask	= bitmask,
6140 		.label		= ilabel,
6141 	};
6142 	struct nfs_setattrres res = {
6143 		.fattr		= fattr,
6144 		.server		= server,
6145 	};
6146 	struct rpc_message msg = {
6147 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6148 		.rpc_argp	= &arg,
6149 		.rpc_resp	= &res,
6150 	};
6151 	int status;
6152 
6153 	nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6154 
6155 	status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6156 	if (status)
6157 		dprintk("%s failed: %d\n", __func__, status);
6158 
6159 	return status;
6160 }
6161 
6162 static int nfs4_do_set_security_label(struct inode *inode,
6163 		struct nfs4_label *ilabel,
6164 		struct nfs_fattr *fattr)
6165 {
6166 	struct nfs4_exception exception = { };
6167 	int err;
6168 
6169 	do {
6170 		err = _nfs4_do_set_security_label(inode, ilabel, fattr);
6171 		trace_nfs4_set_security_label(inode, err);
6172 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
6173 				&exception);
6174 	} while (exception.retry);
6175 	return err;
6176 }
6177 
6178 static int
6179 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6180 {
6181 	struct nfs4_label ilabel = {0, 0, buflen, (char *)buf };
6182 	struct nfs_fattr *fattr;
6183 	int status;
6184 
6185 	if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6186 		return -EOPNOTSUPP;
6187 
6188 	fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
6189 	if (fattr == NULL)
6190 		return -ENOMEM;
6191 
6192 	status = nfs4_do_set_security_label(inode, &ilabel, fattr);
6193 	if (status == 0)
6194 		nfs_setsecurity(inode, fattr);
6195 
6196 	return status;
6197 }
6198 #endif	/* CONFIG_NFS_V4_SECURITY_LABEL */
6199 
6200 
6201 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6202 				    nfs4_verifier *bootverf)
6203 {
6204 	__be32 verf[2];
6205 
6206 	if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6207 		/* An impossible timestamp guarantees this value
6208 		 * will never match a generated boot time. */
6209 		verf[0] = cpu_to_be32(U32_MAX);
6210 		verf[1] = cpu_to_be32(U32_MAX);
6211 	} else {
6212 		struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6213 		u64 ns = ktime_to_ns(nn->boot_time);
6214 
6215 		verf[0] = cpu_to_be32(ns >> 32);
6216 		verf[1] = cpu_to_be32(ns);
6217 	}
6218 	memcpy(bootverf->data, verf, sizeof(bootverf->data));
6219 }
6220 
6221 static size_t
6222 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6223 {
6224 	struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6225 	struct nfs_netns_client *nn_clp = nn->nfs_client;
6226 	const char *id;
6227 
6228 	buf[0] = '\0';
6229 
6230 	if (nn_clp) {
6231 		rcu_read_lock();
6232 		id = rcu_dereference(nn_clp->identifier);
6233 		if (id)
6234 			strscpy(buf, id, buflen);
6235 		rcu_read_unlock();
6236 	}
6237 
6238 	if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6239 		strscpy(buf, nfs4_client_id_uniquifier, buflen);
6240 
6241 	return strlen(buf);
6242 }
6243 
6244 static int
6245 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6246 {
6247 	char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6248 	size_t buflen;
6249 	size_t len;
6250 	char *str;
6251 
6252 	if (clp->cl_owner_id != NULL)
6253 		return 0;
6254 
6255 	rcu_read_lock();
6256 	len = 14 +
6257 		strlen(clp->cl_rpcclient->cl_nodename) +
6258 		1 +
6259 		strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6260 		1;
6261 	rcu_read_unlock();
6262 
6263 	buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6264 	if (buflen)
6265 		len += buflen + 1;
6266 
6267 	if (len > NFS4_OPAQUE_LIMIT + 1)
6268 		return -EINVAL;
6269 
6270 	/*
6271 	 * Since this string is allocated at mount time, and held until the
6272 	 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6273 	 * about a memory-reclaim deadlock.
6274 	 */
6275 	str = kmalloc(len, GFP_KERNEL);
6276 	if (!str)
6277 		return -ENOMEM;
6278 
6279 	rcu_read_lock();
6280 	if (buflen)
6281 		scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6282 			  clp->cl_rpcclient->cl_nodename, buf,
6283 			  rpc_peeraddr2str(clp->cl_rpcclient,
6284 					   RPC_DISPLAY_ADDR));
6285 	else
6286 		scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6287 			  clp->cl_rpcclient->cl_nodename,
6288 			  rpc_peeraddr2str(clp->cl_rpcclient,
6289 					   RPC_DISPLAY_ADDR));
6290 	rcu_read_unlock();
6291 
6292 	clp->cl_owner_id = str;
6293 	return 0;
6294 }
6295 
6296 static int
6297 nfs4_init_uniform_client_string(struct nfs_client *clp)
6298 {
6299 	char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6300 	size_t buflen;
6301 	size_t len;
6302 	char *str;
6303 
6304 	if (clp->cl_owner_id != NULL)
6305 		return 0;
6306 
6307 	len = 10 + 10 + 1 + 10 + 1 +
6308 		strlen(clp->cl_rpcclient->cl_nodename) + 1;
6309 
6310 	buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6311 	if (buflen)
6312 		len += buflen + 1;
6313 
6314 	if (len > NFS4_OPAQUE_LIMIT + 1)
6315 		return -EINVAL;
6316 
6317 	/*
6318 	 * Since this string is allocated at mount time, and held until the
6319 	 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6320 	 * about a memory-reclaim deadlock.
6321 	 */
6322 	str = kmalloc(len, GFP_KERNEL);
6323 	if (!str)
6324 		return -ENOMEM;
6325 
6326 	if (buflen)
6327 		scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6328 			  clp->rpc_ops->version, clp->cl_minorversion,
6329 			  buf, clp->cl_rpcclient->cl_nodename);
6330 	else
6331 		scnprintf(str, len, "Linux NFSv%u.%u %s",
6332 			  clp->rpc_ops->version, clp->cl_minorversion,
6333 			  clp->cl_rpcclient->cl_nodename);
6334 	clp->cl_owner_id = str;
6335 	return 0;
6336 }
6337 
6338 /*
6339  * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6340  * services.  Advertise one based on the address family of the
6341  * clientaddr.
6342  */
6343 static unsigned int
6344 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6345 {
6346 	if (strchr(clp->cl_ipaddr, ':') != NULL)
6347 		return scnprintf(buf, len, "tcp6");
6348 	else
6349 		return scnprintf(buf, len, "tcp");
6350 }
6351 
6352 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6353 {
6354 	struct nfs4_setclientid *sc = calldata;
6355 
6356 	if (task->tk_status == 0)
6357 		sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6358 }
6359 
6360 static const struct rpc_call_ops nfs4_setclientid_ops = {
6361 	.rpc_call_done = nfs4_setclientid_done,
6362 };
6363 
6364 /**
6365  * nfs4_proc_setclientid - Negotiate client ID
6366  * @clp: state data structure
6367  * @program: RPC program for NFSv4 callback service
6368  * @port: IP port number for NFS4 callback service
6369  * @cred: credential to use for this call
6370  * @res: where to place the result
6371  *
6372  * Returns zero, a negative errno, or a negative NFS4ERR status code.
6373  */
6374 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6375 		unsigned short port, const struct cred *cred,
6376 		struct nfs4_setclientid_res *res)
6377 {
6378 	nfs4_verifier sc_verifier;
6379 	struct nfs4_setclientid setclientid = {
6380 		.sc_verifier = &sc_verifier,
6381 		.sc_prog = program,
6382 		.sc_clnt = clp,
6383 	};
6384 	struct rpc_message msg = {
6385 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6386 		.rpc_argp = &setclientid,
6387 		.rpc_resp = res,
6388 		.rpc_cred = cred,
6389 	};
6390 	struct rpc_task_setup task_setup_data = {
6391 		.rpc_client = clp->cl_rpcclient,
6392 		.rpc_message = &msg,
6393 		.callback_ops = &nfs4_setclientid_ops,
6394 		.callback_data = &setclientid,
6395 		.flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6396 	};
6397 	unsigned long now = jiffies;
6398 	int status;
6399 
6400 	/* nfs_client_id4 */
6401 	nfs4_init_boot_verifier(clp, &sc_verifier);
6402 
6403 	if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6404 		status = nfs4_init_uniform_client_string(clp);
6405 	else
6406 		status = nfs4_init_nonuniform_client_string(clp);
6407 
6408 	if (status)
6409 		goto out;
6410 
6411 	/* cb_client4 */
6412 	setclientid.sc_netid_len =
6413 				nfs4_init_callback_netid(clp,
6414 						setclientid.sc_netid,
6415 						sizeof(setclientid.sc_netid));
6416 	setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6417 				sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6418 				clp->cl_ipaddr, port >> 8, port & 255);
6419 
6420 	dprintk("NFS call  setclientid auth=%s, '%s'\n",
6421 		clp->cl_rpcclient->cl_auth->au_ops->au_name,
6422 		clp->cl_owner_id);
6423 
6424 	status = nfs4_call_sync_custom(&task_setup_data);
6425 	if (setclientid.sc_cred) {
6426 		kfree(clp->cl_acceptor);
6427 		clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6428 		put_rpccred(setclientid.sc_cred);
6429 	}
6430 
6431 	if (status == 0)
6432 		do_renew_lease(clp, now);
6433 out:
6434 	trace_nfs4_setclientid(clp, status);
6435 	dprintk("NFS reply setclientid: %d\n", status);
6436 	return status;
6437 }
6438 
6439 /**
6440  * nfs4_proc_setclientid_confirm - Confirm client ID
6441  * @clp: state data structure
6442  * @arg: result of a previous SETCLIENTID
6443  * @cred: credential to use for this call
6444  *
6445  * Returns zero, a negative errno, or a negative NFS4ERR status code.
6446  */
6447 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6448 		struct nfs4_setclientid_res *arg,
6449 		const struct cred *cred)
6450 {
6451 	struct rpc_message msg = {
6452 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6453 		.rpc_argp = arg,
6454 		.rpc_cred = cred,
6455 	};
6456 	int status;
6457 
6458 	dprintk("NFS call  setclientid_confirm auth=%s, (client ID %llx)\n",
6459 		clp->cl_rpcclient->cl_auth->au_ops->au_name,
6460 		clp->cl_clientid);
6461 	status = rpc_call_sync(clp->cl_rpcclient, &msg,
6462 			       RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6463 	trace_nfs4_setclientid_confirm(clp, status);
6464 	dprintk("NFS reply setclientid_confirm: %d\n", status);
6465 	return status;
6466 }
6467 
6468 struct nfs4_delegreturndata {
6469 	struct nfs4_delegreturnargs args;
6470 	struct nfs4_delegreturnres res;
6471 	struct nfs_fh fh;
6472 	nfs4_stateid stateid;
6473 	unsigned long timestamp;
6474 	struct {
6475 		struct nfs4_layoutreturn_args arg;
6476 		struct nfs4_layoutreturn_res res;
6477 		struct nfs4_xdr_opaque_data ld_private;
6478 		u32 roc_barrier;
6479 		bool roc;
6480 	} lr;
6481 	struct nfs_fattr fattr;
6482 	int rpc_status;
6483 	struct inode *inode;
6484 };
6485 
6486 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6487 {
6488 	struct nfs4_delegreturndata *data = calldata;
6489 	struct nfs4_exception exception = {
6490 		.inode = data->inode,
6491 		.stateid = &data->stateid,
6492 		.task_is_privileged = data->args.seq_args.sa_privileged,
6493 	};
6494 
6495 	if (!nfs4_sequence_done(task, &data->res.seq_res))
6496 		return;
6497 
6498 	trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6499 
6500 	/* Handle Layoutreturn errors */
6501 	if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6502 			  &data->res.lr_ret) == -EAGAIN)
6503 		goto out_restart;
6504 
6505 	switch (task->tk_status) {
6506 	case 0:
6507 		renew_lease(data->res.server, data->timestamp);
6508 		break;
6509 	case -NFS4ERR_ADMIN_REVOKED:
6510 	case -NFS4ERR_DELEG_REVOKED:
6511 	case -NFS4ERR_EXPIRED:
6512 		nfs4_free_revoked_stateid(data->res.server,
6513 				data->args.stateid,
6514 				task->tk_msg.rpc_cred);
6515 		fallthrough;
6516 	case -NFS4ERR_BAD_STATEID:
6517 	case -NFS4ERR_STALE_STATEID:
6518 	case -ETIMEDOUT:
6519 		task->tk_status = 0;
6520 		break;
6521 	case -NFS4ERR_OLD_STATEID:
6522 		if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6523 			nfs4_stateid_seqid_inc(&data->stateid);
6524 		if (data->args.bitmask) {
6525 			data->args.bitmask = NULL;
6526 			data->res.fattr = NULL;
6527 		}
6528 		goto out_restart;
6529 	case -NFS4ERR_ACCESS:
6530 		if (data->args.bitmask) {
6531 			data->args.bitmask = NULL;
6532 			data->res.fattr = NULL;
6533 			goto out_restart;
6534 		}
6535 		fallthrough;
6536 	default:
6537 		task->tk_status = nfs4_async_handle_exception(task,
6538 				data->res.server, task->tk_status,
6539 				&exception);
6540 		if (exception.retry)
6541 			goto out_restart;
6542 	}
6543 	nfs_delegation_mark_returned(data->inode, data->args.stateid);
6544 	data->rpc_status = task->tk_status;
6545 	return;
6546 out_restart:
6547 	task->tk_status = 0;
6548 	rpc_restart_call_prepare(task);
6549 }
6550 
6551 static void nfs4_delegreturn_release(void *calldata)
6552 {
6553 	struct nfs4_delegreturndata *data = calldata;
6554 	struct inode *inode = data->inode;
6555 
6556 	if (data->lr.roc)
6557 		pnfs_roc_release(&data->lr.arg, &data->lr.res,
6558 				 data->res.lr_ret);
6559 	if (inode) {
6560 		nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6561 		nfs_iput_and_deactive(inode);
6562 	}
6563 	kfree(calldata);
6564 }
6565 
6566 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6567 {
6568 	struct nfs4_delegreturndata *d_data;
6569 	struct pnfs_layout_hdr *lo;
6570 
6571 	d_data = (struct nfs4_delegreturndata *)data;
6572 
6573 	if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6574 		nfs4_sequence_done(task, &d_data->res.seq_res);
6575 		return;
6576 	}
6577 
6578 	lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6579 	if (lo && !pnfs_layout_is_valid(lo)) {
6580 		d_data->args.lr_args = NULL;
6581 		d_data->res.lr_res = NULL;
6582 	}
6583 
6584 	nfs4_setup_sequence(d_data->res.server->nfs_client,
6585 			&d_data->args.seq_args,
6586 			&d_data->res.seq_res,
6587 			task);
6588 }
6589 
6590 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6591 	.rpc_call_prepare = nfs4_delegreturn_prepare,
6592 	.rpc_call_done = nfs4_delegreturn_done,
6593 	.rpc_release = nfs4_delegreturn_release,
6594 };
6595 
6596 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6597 {
6598 	struct nfs4_delegreturndata *data;
6599 	struct nfs_server *server = NFS_SERVER(inode);
6600 	struct rpc_task *task;
6601 	struct rpc_message msg = {
6602 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6603 		.rpc_cred = cred,
6604 	};
6605 	struct rpc_task_setup task_setup_data = {
6606 		.rpc_client = server->client,
6607 		.rpc_message = &msg,
6608 		.callback_ops = &nfs4_delegreturn_ops,
6609 		.flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
6610 	};
6611 	int status = 0;
6612 
6613 	data = kzalloc(sizeof(*data), GFP_NOFS);
6614 	if (data == NULL)
6615 		return -ENOMEM;
6616 
6617 	nfs4_state_protect(server->nfs_client,
6618 			NFS_SP4_MACH_CRED_CLEANUP,
6619 			&task_setup_data.rpc_client, &msg);
6620 
6621 	data->args.fhandle = &data->fh;
6622 	data->args.stateid = &data->stateid;
6623 	nfs4_bitmask_set(data->args.bitmask_store,
6624 			 server->cache_consistency_bitmask, inode, 0);
6625 	data->args.bitmask = data->args.bitmask_store;
6626 	nfs_copy_fh(&data->fh, NFS_FH(inode));
6627 	nfs4_stateid_copy(&data->stateid, stateid);
6628 	data->res.fattr = &data->fattr;
6629 	data->res.server = server;
6630 	data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6631 	data->lr.arg.ld_private = &data->lr.ld_private;
6632 	nfs_fattr_init(data->res.fattr);
6633 	data->timestamp = jiffies;
6634 	data->rpc_status = 0;
6635 	data->inode = nfs_igrab_and_active(inode);
6636 	if (data->inode || issync) {
6637 		data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6638 					cred);
6639 		if (data->lr.roc) {
6640 			data->args.lr_args = &data->lr.arg;
6641 			data->res.lr_res = &data->lr.res;
6642 		}
6643 	}
6644 
6645 	if (!data->inode)
6646 		nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6647 				   1);
6648 	else
6649 		nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6650 				   0);
6651 	task_setup_data.callback_data = data;
6652 	msg.rpc_argp = &data->args;
6653 	msg.rpc_resp = &data->res;
6654 	task = rpc_run_task(&task_setup_data);
6655 	if (IS_ERR(task))
6656 		return PTR_ERR(task);
6657 	if (!issync)
6658 		goto out;
6659 	status = rpc_wait_for_completion_task(task);
6660 	if (status != 0)
6661 		goto out;
6662 	status = data->rpc_status;
6663 out:
6664 	rpc_put_task(task);
6665 	return status;
6666 }
6667 
6668 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6669 {
6670 	struct nfs_server *server = NFS_SERVER(inode);
6671 	struct nfs4_exception exception = { };
6672 	int err;
6673 	do {
6674 		err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6675 		trace_nfs4_delegreturn(inode, stateid, err);
6676 		switch (err) {
6677 			case -NFS4ERR_STALE_STATEID:
6678 			case -NFS4ERR_EXPIRED:
6679 			case 0:
6680 				return 0;
6681 		}
6682 		err = nfs4_handle_exception(server, err, &exception);
6683 	} while (exception.retry);
6684 	return err;
6685 }
6686 
6687 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6688 {
6689 	struct inode *inode = state->inode;
6690 	struct nfs_server *server = NFS_SERVER(inode);
6691 	struct nfs_client *clp = server->nfs_client;
6692 	struct nfs_lockt_args arg = {
6693 		.fh = NFS_FH(inode),
6694 		.fl = request,
6695 	};
6696 	struct nfs_lockt_res res = {
6697 		.denied = request,
6698 	};
6699 	struct rpc_message msg = {
6700 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6701 		.rpc_argp	= &arg,
6702 		.rpc_resp	= &res,
6703 		.rpc_cred	= state->owner->so_cred,
6704 	};
6705 	struct nfs4_lock_state *lsp;
6706 	int status;
6707 
6708 	arg.lock_owner.clientid = clp->cl_clientid;
6709 	status = nfs4_set_lock_state(state, request);
6710 	if (status != 0)
6711 		goto out;
6712 	lsp = request->fl_u.nfs4_fl.owner;
6713 	arg.lock_owner.id = lsp->ls_seqid.owner_id;
6714 	arg.lock_owner.s_dev = server->s_dev;
6715 	status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6716 	switch (status) {
6717 		case 0:
6718 			request->fl_type = F_UNLCK;
6719 			break;
6720 		case -NFS4ERR_DENIED:
6721 			status = 0;
6722 	}
6723 	request->fl_ops->fl_release_private(request);
6724 	request->fl_ops = NULL;
6725 out:
6726 	return status;
6727 }
6728 
6729 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6730 {
6731 	struct nfs4_exception exception = {
6732 		.interruptible = true,
6733 	};
6734 	int err;
6735 
6736 	do {
6737 		err = _nfs4_proc_getlk(state, cmd, request);
6738 		trace_nfs4_get_lock(request, state, cmd, err);
6739 		err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6740 				&exception);
6741 	} while (exception.retry);
6742 	return err;
6743 }
6744 
6745 /*
6746  * Update the seqid of a lock stateid after receiving
6747  * NFS4ERR_OLD_STATEID
6748  */
6749 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6750 		struct nfs4_lock_state *lsp)
6751 {
6752 	struct nfs4_state *state = lsp->ls_state;
6753 	bool ret = false;
6754 
6755 	spin_lock(&state->state_lock);
6756 	if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6757 		goto out;
6758 	if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6759 		nfs4_stateid_seqid_inc(dst);
6760 	else
6761 		dst->seqid = lsp->ls_stateid.seqid;
6762 	ret = true;
6763 out:
6764 	spin_unlock(&state->state_lock);
6765 	return ret;
6766 }
6767 
6768 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6769 		struct nfs4_lock_state *lsp)
6770 {
6771 	struct nfs4_state *state = lsp->ls_state;
6772 	bool ret;
6773 
6774 	spin_lock(&state->state_lock);
6775 	ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6776 	nfs4_stateid_copy(dst, &lsp->ls_stateid);
6777 	spin_unlock(&state->state_lock);
6778 	return ret;
6779 }
6780 
6781 struct nfs4_unlockdata {
6782 	struct nfs_locku_args arg;
6783 	struct nfs_locku_res res;
6784 	struct nfs4_lock_state *lsp;
6785 	struct nfs_open_context *ctx;
6786 	struct nfs_lock_context *l_ctx;
6787 	struct file_lock fl;
6788 	struct nfs_server *server;
6789 	unsigned long timestamp;
6790 };
6791 
6792 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6793 		struct nfs_open_context *ctx,
6794 		struct nfs4_lock_state *lsp,
6795 		struct nfs_seqid *seqid)
6796 {
6797 	struct nfs4_unlockdata *p;
6798 	struct nfs4_state *state = lsp->ls_state;
6799 	struct inode *inode = state->inode;
6800 
6801 	p = kzalloc(sizeof(*p), GFP_NOFS);
6802 	if (p == NULL)
6803 		return NULL;
6804 	p->arg.fh = NFS_FH(inode);
6805 	p->arg.fl = &p->fl;
6806 	p->arg.seqid = seqid;
6807 	p->res.seqid = seqid;
6808 	p->lsp = lsp;
6809 	/* Ensure we don't close file until we're done freeing locks! */
6810 	p->ctx = get_nfs_open_context(ctx);
6811 	p->l_ctx = nfs_get_lock_context(ctx);
6812 	locks_init_lock(&p->fl);
6813 	locks_copy_lock(&p->fl, fl);
6814 	p->server = NFS_SERVER(inode);
6815 	spin_lock(&state->state_lock);
6816 	nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6817 	spin_unlock(&state->state_lock);
6818 	return p;
6819 }
6820 
6821 static void nfs4_locku_release_calldata(void *data)
6822 {
6823 	struct nfs4_unlockdata *calldata = data;
6824 	nfs_free_seqid(calldata->arg.seqid);
6825 	nfs4_put_lock_state(calldata->lsp);
6826 	nfs_put_lock_context(calldata->l_ctx);
6827 	put_nfs_open_context(calldata->ctx);
6828 	kfree(calldata);
6829 }
6830 
6831 static void nfs4_locku_done(struct rpc_task *task, void *data)
6832 {
6833 	struct nfs4_unlockdata *calldata = data;
6834 	struct nfs4_exception exception = {
6835 		.inode = calldata->lsp->ls_state->inode,
6836 		.stateid = &calldata->arg.stateid,
6837 	};
6838 
6839 	if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6840 		return;
6841 	switch (task->tk_status) {
6842 		case 0:
6843 			renew_lease(calldata->server, calldata->timestamp);
6844 			locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6845 			if (nfs4_update_lock_stateid(calldata->lsp,
6846 					&calldata->res.stateid))
6847 				break;
6848 			fallthrough;
6849 		case -NFS4ERR_ADMIN_REVOKED:
6850 		case -NFS4ERR_EXPIRED:
6851 			nfs4_free_revoked_stateid(calldata->server,
6852 					&calldata->arg.stateid,
6853 					task->tk_msg.rpc_cred);
6854 			fallthrough;
6855 		case -NFS4ERR_BAD_STATEID:
6856 		case -NFS4ERR_STALE_STATEID:
6857 			if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6858 						calldata->lsp))
6859 				rpc_restart_call_prepare(task);
6860 			break;
6861 		case -NFS4ERR_OLD_STATEID:
6862 			if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6863 						calldata->lsp))
6864 				rpc_restart_call_prepare(task);
6865 			break;
6866 		default:
6867 			task->tk_status = nfs4_async_handle_exception(task,
6868 					calldata->server, task->tk_status,
6869 					&exception);
6870 			if (exception.retry)
6871 				rpc_restart_call_prepare(task);
6872 	}
6873 	nfs_release_seqid(calldata->arg.seqid);
6874 }
6875 
6876 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6877 {
6878 	struct nfs4_unlockdata *calldata = data;
6879 
6880 	if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6881 		nfs_async_iocounter_wait(task, calldata->l_ctx))
6882 		return;
6883 
6884 	if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6885 		goto out_wait;
6886 	if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6887 		/* Note: exit _without_ running nfs4_locku_done */
6888 		goto out_no_action;
6889 	}
6890 	calldata->timestamp = jiffies;
6891 	if (nfs4_setup_sequence(calldata->server->nfs_client,
6892 				&calldata->arg.seq_args,
6893 				&calldata->res.seq_res,
6894 				task) != 0)
6895 		nfs_release_seqid(calldata->arg.seqid);
6896 	return;
6897 out_no_action:
6898 	task->tk_action = NULL;
6899 out_wait:
6900 	nfs4_sequence_done(task, &calldata->res.seq_res);
6901 }
6902 
6903 static const struct rpc_call_ops nfs4_locku_ops = {
6904 	.rpc_call_prepare = nfs4_locku_prepare,
6905 	.rpc_call_done = nfs4_locku_done,
6906 	.rpc_release = nfs4_locku_release_calldata,
6907 };
6908 
6909 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6910 		struct nfs_open_context *ctx,
6911 		struct nfs4_lock_state *lsp,
6912 		struct nfs_seqid *seqid)
6913 {
6914 	struct nfs4_unlockdata *data;
6915 	struct rpc_message msg = {
6916 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6917 		.rpc_cred = ctx->cred,
6918 	};
6919 	struct rpc_task_setup task_setup_data = {
6920 		.rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6921 		.rpc_message = &msg,
6922 		.callback_ops = &nfs4_locku_ops,
6923 		.workqueue = nfsiod_workqueue,
6924 		.flags = RPC_TASK_ASYNC,
6925 	};
6926 	struct nfs_client *client =
6927 		NFS_SERVER(lsp->ls_state->inode)->nfs_client;
6928 
6929 	if (client->cl_minorversion)
6930 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
6931 
6932 	nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6933 		NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6934 
6935 	/* Ensure this is an unlock - when canceling a lock, the
6936 	 * canceled lock is passed in, and it won't be an unlock.
6937 	 */
6938 	fl->fl_type = F_UNLCK;
6939 	if (fl->fl_flags & FL_CLOSE)
6940 		set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6941 
6942 	data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6943 	if (data == NULL) {
6944 		nfs_free_seqid(seqid);
6945 		return ERR_PTR(-ENOMEM);
6946 	}
6947 
6948 	nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6949 	msg.rpc_argp = &data->arg;
6950 	msg.rpc_resp = &data->res;
6951 	task_setup_data.callback_data = data;
6952 	return rpc_run_task(&task_setup_data);
6953 }
6954 
6955 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6956 {
6957 	struct inode *inode = state->inode;
6958 	struct nfs4_state_owner *sp = state->owner;
6959 	struct nfs_inode *nfsi = NFS_I(inode);
6960 	struct nfs_seqid *seqid;
6961 	struct nfs4_lock_state *lsp;
6962 	struct rpc_task *task;
6963 	struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6964 	int status = 0;
6965 	unsigned char fl_flags = request->fl_flags;
6966 
6967 	status = nfs4_set_lock_state(state, request);
6968 	/* Unlock _before_ we do the RPC call */
6969 	request->fl_flags |= FL_EXISTS;
6970 	/* Exclude nfs_delegation_claim_locks() */
6971 	mutex_lock(&sp->so_delegreturn_mutex);
6972 	/* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6973 	down_read(&nfsi->rwsem);
6974 	if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6975 		up_read(&nfsi->rwsem);
6976 		mutex_unlock(&sp->so_delegreturn_mutex);
6977 		goto out;
6978 	}
6979 	up_read(&nfsi->rwsem);
6980 	mutex_unlock(&sp->so_delegreturn_mutex);
6981 	if (status != 0)
6982 		goto out;
6983 	/* Is this a delegated lock? */
6984 	lsp = request->fl_u.nfs4_fl.owner;
6985 	if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6986 		goto out;
6987 	alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6988 	seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6989 	status = -ENOMEM;
6990 	if (IS_ERR(seqid))
6991 		goto out;
6992 	task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6993 	status = PTR_ERR(task);
6994 	if (IS_ERR(task))
6995 		goto out;
6996 	status = rpc_wait_for_completion_task(task);
6997 	rpc_put_task(task);
6998 out:
6999 	request->fl_flags = fl_flags;
7000 	trace_nfs4_unlock(request, state, F_SETLK, status);
7001 	return status;
7002 }
7003 
7004 struct nfs4_lockdata {
7005 	struct nfs_lock_args arg;
7006 	struct nfs_lock_res res;
7007 	struct nfs4_lock_state *lsp;
7008 	struct nfs_open_context *ctx;
7009 	struct file_lock fl;
7010 	unsigned long timestamp;
7011 	int rpc_status;
7012 	int cancelled;
7013 	struct nfs_server *server;
7014 };
7015 
7016 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
7017 		struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
7018 		gfp_t gfp_mask)
7019 {
7020 	struct nfs4_lockdata *p;
7021 	struct inode *inode = lsp->ls_state->inode;
7022 	struct nfs_server *server = NFS_SERVER(inode);
7023 	struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7024 
7025 	p = kzalloc(sizeof(*p), gfp_mask);
7026 	if (p == NULL)
7027 		return NULL;
7028 
7029 	p->arg.fh = NFS_FH(inode);
7030 	p->arg.fl = &p->fl;
7031 	p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
7032 	if (IS_ERR(p->arg.open_seqid))
7033 		goto out_free;
7034 	alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
7035 	p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
7036 	if (IS_ERR(p->arg.lock_seqid))
7037 		goto out_free_seqid;
7038 	p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
7039 	p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
7040 	p->arg.lock_owner.s_dev = server->s_dev;
7041 	p->res.lock_seqid = p->arg.lock_seqid;
7042 	p->lsp = lsp;
7043 	p->server = server;
7044 	p->ctx = get_nfs_open_context(ctx);
7045 	locks_init_lock(&p->fl);
7046 	locks_copy_lock(&p->fl, fl);
7047 	return p;
7048 out_free_seqid:
7049 	nfs_free_seqid(p->arg.open_seqid);
7050 out_free:
7051 	kfree(p);
7052 	return NULL;
7053 }
7054 
7055 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
7056 {
7057 	struct nfs4_lockdata *data = calldata;
7058 	struct nfs4_state *state = data->lsp->ls_state;
7059 
7060 	if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
7061 		goto out_wait;
7062 	/* Do we need to do an open_to_lock_owner? */
7063 	if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
7064 		if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
7065 			goto out_release_lock_seqid;
7066 		}
7067 		nfs4_stateid_copy(&data->arg.open_stateid,
7068 				&state->open_stateid);
7069 		data->arg.new_lock_owner = 1;
7070 		data->res.open_seqid = data->arg.open_seqid;
7071 	} else {
7072 		data->arg.new_lock_owner = 0;
7073 		nfs4_stateid_copy(&data->arg.lock_stateid,
7074 				&data->lsp->ls_stateid);
7075 	}
7076 	if (!nfs4_valid_open_stateid(state)) {
7077 		data->rpc_status = -EBADF;
7078 		task->tk_action = NULL;
7079 		goto out_release_open_seqid;
7080 	}
7081 	data->timestamp = jiffies;
7082 	if (nfs4_setup_sequence(data->server->nfs_client,
7083 				&data->arg.seq_args,
7084 				&data->res.seq_res,
7085 				task) == 0)
7086 		return;
7087 out_release_open_seqid:
7088 	nfs_release_seqid(data->arg.open_seqid);
7089 out_release_lock_seqid:
7090 	nfs_release_seqid(data->arg.lock_seqid);
7091 out_wait:
7092 	nfs4_sequence_done(task, &data->res.seq_res);
7093 	dprintk("%s: ret = %d\n", __func__, data->rpc_status);
7094 }
7095 
7096 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7097 {
7098 	struct nfs4_lockdata *data = calldata;
7099 	struct nfs4_lock_state *lsp = data->lsp;
7100 
7101 	if (!nfs4_sequence_done(task, &data->res.seq_res))
7102 		return;
7103 
7104 	data->rpc_status = task->tk_status;
7105 	switch (task->tk_status) {
7106 	case 0:
7107 		renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
7108 				data->timestamp);
7109 		if (data->arg.new_lock && !data->cancelled) {
7110 			data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
7111 			if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
7112 				goto out_restart;
7113 		}
7114 		if (data->arg.new_lock_owner != 0) {
7115 			nfs_confirm_seqid(&lsp->ls_seqid, 0);
7116 			nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7117 			set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
7118 		} else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7119 			goto out_restart;
7120 		break;
7121 	case -NFS4ERR_BAD_STATEID:
7122 	case -NFS4ERR_OLD_STATEID:
7123 	case -NFS4ERR_STALE_STATEID:
7124 	case -NFS4ERR_EXPIRED:
7125 		if (data->arg.new_lock_owner != 0) {
7126 			if (!nfs4_stateid_match(&data->arg.open_stateid,
7127 						&lsp->ls_state->open_stateid))
7128 				goto out_restart;
7129 		} else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7130 						&lsp->ls_stateid))
7131 				goto out_restart;
7132 	}
7133 out_done:
7134 	dprintk("%s: ret = %d!\n", __func__, data->rpc_status);
7135 	return;
7136 out_restart:
7137 	if (!data->cancelled)
7138 		rpc_restart_call_prepare(task);
7139 	goto out_done;
7140 }
7141 
7142 static void nfs4_lock_release(void *calldata)
7143 {
7144 	struct nfs4_lockdata *data = calldata;
7145 
7146 	nfs_free_seqid(data->arg.open_seqid);
7147 	if (data->cancelled && data->rpc_status == 0) {
7148 		struct rpc_task *task;
7149 		task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7150 				data->arg.lock_seqid);
7151 		if (!IS_ERR(task))
7152 			rpc_put_task_async(task);
7153 		dprintk("%s: cancelling lock!\n", __func__);
7154 	} else
7155 		nfs_free_seqid(data->arg.lock_seqid);
7156 	nfs4_put_lock_state(data->lsp);
7157 	put_nfs_open_context(data->ctx);
7158 	kfree(data);
7159 }
7160 
7161 static const struct rpc_call_ops nfs4_lock_ops = {
7162 	.rpc_call_prepare = nfs4_lock_prepare,
7163 	.rpc_call_done = nfs4_lock_done,
7164 	.rpc_release = nfs4_lock_release,
7165 };
7166 
7167 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7168 {
7169 	switch (error) {
7170 	case -NFS4ERR_ADMIN_REVOKED:
7171 	case -NFS4ERR_EXPIRED:
7172 	case -NFS4ERR_BAD_STATEID:
7173 		lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7174 		if (new_lock_owner != 0 ||
7175 		   test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7176 			nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7177 		break;
7178 	case -NFS4ERR_STALE_STATEID:
7179 		lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7180 		nfs4_schedule_lease_recovery(server->nfs_client);
7181 	}
7182 }
7183 
7184 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7185 {
7186 	struct nfs4_lockdata *data;
7187 	struct rpc_task *task;
7188 	struct rpc_message msg = {
7189 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7190 		.rpc_cred = state->owner->so_cred,
7191 	};
7192 	struct rpc_task_setup task_setup_data = {
7193 		.rpc_client = NFS_CLIENT(state->inode),
7194 		.rpc_message = &msg,
7195 		.callback_ops = &nfs4_lock_ops,
7196 		.workqueue = nfsiod_workqueue,
7197 		.flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7198 	};
7199 	int ret;
7200 	struct nfs_client *client = NFS_SERVER(state->inode)->nfs_client;
7201 
7202 	if (client->cl_minorversion)
7203 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
7204 
7205 	data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
7206 			fl->fl_u.nfs4_fl.owner,
7207 			recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
7208 	if (data == NULL)
7209 		return -ENOMEM;
7210 	if (IS_SETLKW(cmd))
7211 		data->arg.block = 1;
7212 	nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7213 				recovery_type > NFS_LOCK_NEW);
7214 	msg.rpc_argp = &data->arg;
7215 	msg.rpc_resp = &data->res;
7216 	task_setup_data.callback_data = data;
7217 	if (recovery_type > NFS_LOCK_NEW) {
7218 		if (recovery_type == NFS_LOCK_RECLAIM)
7219 			data->arg.reclaim = NFS_LOCK_RECLAIM;
7220 	} else
7221 		data->arg.new_lock = 1;
7222 	task = rpc_run_task(&task_setup_data);
7223 	if (IS_ERR(task))
7224 		return PTR_ERR(task);
7225 	ret = rpc_wait_for_completion_task(task);
7226 	if (ret == 0) {
7227 		ret = data->rpc_status;
7228 		if (ret)
7229 			nfs4_handle_setlk_error(data->server, data->lsp,
7230 					data->arg.new_lock_owner, ret);
7231 	} else
7232 		data->cancelled = true;
7233 	trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7234 	rpc_put_task(task);
7235 	dprintk("%s: ret = %d\n", __func__, ret);
7236 	return ret;
7237 }
7238 
7239 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7240 {
7241 	struct nfs_server *server = NFS_SERVER(state->inode);
7242 	struct nfs4_exception exception = {
7243 		.inode = state->inode,
7244 	};
7245 	int err;
7246 
7247 	do {
7248 		/* Cache the lock if possible... */
7249 		if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7250 			return 0;
7251 		err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7252 		if (err != -NFS4ERR_DELAY)
7253 			break;
7254 		nfs4_handle_exception(server, err, &exception);
7255 	} while (exception.retry);
7256 	return err;
7257 }
7258 
7259 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7260 {
7261 	struct nfs_server *server = NFS_SERVER(state->inode);
7262 	struct nfs4_exception exception = {
7263 		.inode = state->inode,
7264 	};
7265 	int err;
7266 
7267 	err = nfs4_set_lock_state(state, request);
7268 	if (err != 0)
7269 		return err;
7270 	if (!recover_lost_locks) {
7271 		set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7272 		return 0;
7273 	}
7274 	do {
7275 		if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7276 			return 0;
7277 		err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7278 		switch (err) {
7279 		default:
7280 			goto out;
7281 		case -NFS4ERR_GRACE:
7282 		case -NFS4ERR_DELAY:
7283 			nfs4_handle_exception(server, err, &exception);
7284 			err = 0;
7285 		}
7286 	} while (exception.retry);
7287 out:
7288 	return err;
7289 }
7290 
7291 #if defined(CONFIG_NFS_V4_1)
7292 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7293 {
7294 	struct nfs4_lock_state *lsp;
7295 	int status;
7296 
7297 	status = nfs4_set_lock_state(state, request);
7298 	if (status != 0)
7299 		return status;
7300 	lsp = request->fl_u.nfs4_fl.owner;
7301 	if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7302 	    test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7303 		return 0;
7304 	return nfs4_lock_expired(state, request);
7305 }
7306 #endif
7307 
7308 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7309 {
7310 	struct nfs_inode *nfsi = NFS_I(state->inode);
7311 	struct nfs4_state_owner *sp = state->owner;
7312 	unsigned char fl_flags = request->fl_flags;
7313 	int status;
7314 
7315 	request->fl_flags |= FL_ACCESS;
7316 	status = locks_lock_inode_wait(state->inode, request);
7317 	if (status < 0)
7318 		goto out;
7319 	mutex_lock(&sp->so_delegreturn_mutex);
7320 	down_read(&nfsi->rwsem);
7321 	if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7322 		/* Yes: cache locks! */
7323 		/* ...but avoid races with delegation recall... */
7324 		request->fl_flags = fl_flags & ~FL_SLEEP;
7325 		status = locks_lock_inode_wait(state->inode, request);
7326 		up_read(&nfsi->rwsem);
7327 		mutex_unlock(&sp->so_delegreturn_mutex);
7328 		goto out;
7329 	}
7330 	up_read(&nfsi->rwsem);
7331 	mutex_unlock(&sp->so_delegreturn_mutex);
7332 	status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7333 out:
7334 	request->fl_flags = fl_flags;
7335 	return status;
7336 }
7337 
7338 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7339 {
7340 	struct nfs4_exception exception = {
7341 		.state = state,
7342 		.inode = state->inode,
7343 		.interruptible = true,
7344 	};
7345 	int err;
7346 
7347 	do {
7348 		err = _nfs4_proc_setlk(state, cmd, request);
7349 		if (err == -NFS4ERR_DENIED)
7350 			err = -EAGAIN;
7351 		err = nfs4_handle_exception(NFS_SERVER(state->inode),
7352 				err, &exception);
7353 	} while (exception.retry);
7354 	return err;
7355 }
7356 
7357 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7358 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7359 
7360 static int
7361 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7362 			struct file_lock *request)
7363 {
7364 	int		status = -ERESTARTSYS;
7365 	unsigned long	timeout = NFS4_LOCK_MINTIMEOUT;
7366 
7367 	while(!signalled()) {
7368 		status = nfs4_proc_setlk(state, cmd, request);
7369 		if ((status != -EAGAIN) || IS_SETLK(cmd))
7370 			break;
7371 		freezable_schedule_timeout_interruptible(timeout);
7372 		timeout *= 2;
7373 		timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7374 		status = -ERESTARTSYS;
7375 	}
7376 	return status;
7377 }
7378 
7379 #ifdef CONFIG_NFS_V4_1
7380 struct nfs4_lock_waiter {
7381 	struct inode		*inode;
7382 	struct nfs_lowner	owner;
7383 	wait_queue_entry_t	wait;
7384 };
7385 
7386 static int
7387 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7388 {
7389 	struct nfs4_lock_waiter	*waiter	=
7390 		container_of(wait, struct nfs4_lock_waiter, wait);
7391 
7392 	/* NULL key means to wake up everyone */
7393 	if (key) {
7394 		struct cb_notify_lock_args	*cbnl = key;
7395 		struct nfs_lowner		*lowner = &cbnl->cbnl_owner,
7396 						*wowner = &waiter->owner;
7397 
7398 		/* Only wake if the callback was for the same owner. */
7399 		if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7400 			return 0;
7401 
7402 		/* Make sure it's for the right inode */
7403 		if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7404 			return 0;
7405 	}
7406 
7407 	return woken_wake_function(wait, mode, flags, key);
7408 }
7409 
7410 static int
7411 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7412 {
7413 	struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7414 	struct nfs_server *server = NFS_SERVER(state->inode);
7415 	struct nfs_client *clp = server->nfs_client;
7416 	wait_queue_head_t *q = &clp->cl_lock_waitq;
7417 	struct nfs4_lock_waiter waiter = {
7418 		.inode = state->inode,
7419 		.owner = { .clientid = clp->cl_clientid,
7420 			   .id = lsp->ls_seqid.owner_id,
7421 			   .s_dev = server->s_dev },
7422 	};
7423 	int status;
7424 
7425 	/* Don't bother with waitqueue if we don't expect a callback */
7426 	if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7427 		return nfs4_retry_setlk_simple(state, cmd, request);
7428 
7429 	init_wait(&waiter.wait);
7430 	waiter.wait.func = nfs4_wake_lock_waiter;
7431 	add_wait_queue(q, &waiter.wait);
7432 
7433 	do {
7434 		status = nfs4_proc_setlk(state, cmd, request);
7435 		if (status != -EAGAIN || IS_SETLK(cmd))
7436 			break;
7437 
7438 		status = -ERESTARTSYS;
7439 		freezer_do_not_count();
7440 		wait_woken(&waiter.wait, TASK_INTERRUPTIBLE,
7441 			   NFS4_LOCK_MAXTIMEOUT);
7442 		freezer_count();
7443 	} while (!signalled());
7444 
7445 	remove_wait_queue(q, &waiter.wait);
7446 
7447 	return status;
7448 }
7449 #else /* !CONFIG_NFS_V4_1 */
7450 static inline int
7451 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7452 {
7453 	return nfs4_retry_setlk_simple(state, cmd, request);
7454 }
7455 #endif
7456 
7457 static int
7458 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7459 {
7460 	struct nfs_open_context *ctx;
7461 	struct nfs4_state *state;
7462 	int status;
7463 
7464 	/* verify open state */
7465 	ctx = nfs_file_open_context(filp);
7466 	state = ctx->state;
7467 
7468 	if (IS_GETLK(cmd)) {
7469 		if (state != NULL)
7470 			return nfs4_proc_getlk(state, F_GETLK, request);
7471 		return 0;
7472 	}
7473 
7474 	if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7475 		return -EINVAL;
7476 
7477 	if (request->fl_type == F_UNLCK) {
7478 		if (state != NULL)
7479 			return nfs4_proc_unlck(state, cmd, request);
7480 		return 0;
7481 	}
7482 
7483 	if (state == NULL)
7484 		return -ENOLCK;
7485 
7486 	if ((request->fl_flags & FL_POSIX) &&
7487 	    !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7488 		return -ENOLCK;
7489 
7490 	/*
7491 	 * Don't rely on the VFS having checked the file open mode,
7492 	 * since it won't do this for flock() locks.
7493 	 */
7494 	switch (request->fl_type) {
7495 	case F_RDLCK:
7496 		if (!(filp->f_mode & FMODE_READ))
7497 			return -EBADF;
7498 		break;
7499 	case F_WRLCK:
7500 		if (!(filp->f_mode & FMODE_WRITE))
7501 			return -EBADF;
7502 	}
7503 
7504 	status = nfs4_set_lock_state(state, request);
7505 	if (status != 0)
7506 		return status;
7507 
7508 	return nfs4_retry_setlk(state, cmd, request);
7509 }
7510 
7511 static int nfs4_delete_lease(struct file *file, void **priv)
7512 {
7513 	return generic_setlease(file, F_UNLCK, NULL, priv);
7514 }
7515 
7516 static int nfs4_add_lease(struct file *file, long arg, struct file_lock **lease,
7517 			  void **priv)
7518 {
7519 	struct inode *inode = file_inode(file);
7520 	fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE;
7521 	int ret;
7522 
7523 	/* No delegation, no lease */
7524 	if (!nfs4_have_delegation(inode, type))
7525 		return -EAGAIN;
7526 	ret = generic_setlease(file, arg, lease, priv);
7527 	if (ret || nfs4_have_delegation(inode, type))
7528 		return ret;
7529 	/* We raced with a delegation return */
7530 	nfs4_delete_lease(file, priv);
7531 	return -EAGAIN;
7532 }
7533 
7534 int nfs4_proc_setlease(struct file *file, long arg, struct file_lock **lease,
7535 		       void **priv)
7536 {
7537 	switch (arg) {
7538 	case F_RDLCK:
7539 	case F_WRLCK:
7540 		return nfs4_add_lease(file, arg, lease, priv);
7541 	case F_UNLCK:
7542 		return nfs4_delete_lease(file, priv);
7543 	default:
7544 		return -EINVAL;
7545 	}
7546 }
7547 
7548 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7549 {
7550 	struct nfs_server *server = NFS_SERVER(state->inode);
7551 	int err;
7552 
7553 	err = nfs4_set_lock_state(state, fl);
7554 	if (err != 0)
7555 		return err;
7556 	do {
7557 		err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7558 		if (err != -NFS4ERR_DELAY)
7559 			break;
7560 		ssleep(1);
7561 	} while (err == -NFS4ERR_DELAY);
7562 	return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7563 }
7564 
7565 struct nfs_release_lockowner_data {
7566 	struct nfs4_lock_state *lsp;
7567 	struct nfs_server *server;
7568 	struct nfs_release_lockowner_args args;
7569 	struct nfs_release_lockowner_res res;
7570 	unsigned long timestamp;
7571 };
7572 
7573 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7574 {
7575 	struct nfs_release_lockowner_data *data = calldata;
7576 	struct nfs_server *server = data->server;
7577 	nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7578 			   &data->res.seq_res, task);
7579 	data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7580 	data->timestamp = jiffies;
7581 }
7582 
7583 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7584 {
7585 	struct nfs_release_lockowner_data *data = calldata;
7586 	struct nfs_server *server = data->server;
7587 
7588 	nfs40_sequence_done(task, &data->res.seq_res);
7589 
7590 	switch (task->tk_status) {
7591 	case 0:
7592 		renew_lease(server, data->timestamp);
7593 		break;
7594 	case -NFS4ERR_STALE_CLIENTID:
7595 	case -NFS4ERR_EXPIRED:
7596 		nfs4_schedule_lease_recovery(server->nfs_client);
7597 		break;
7598 	case -NFS4ERR_LEASE_MOVED:
7599 	case -NFS4ERR_DELAY:
7600 		if (nfs4_async_handle_error(task, server,
7601 					    NULL, NULL) == -EAGAIN)
7602 			rpc_restart_call_prepare(task);
7603 	}
7604 }
7605 
7606 static void nfs4_release_lockowner_release(void *calldata)
7607 {
7608 	struct nfs_release_lockowner_data *data = calldata;
7609 	nfs4_free_lock_state(data->server, data->lsp);
7610 	kfree(calldata);
7611 }
7612 
7613 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7614 	.rpc_call_prepare = nfs4_release_lockowner_prepare,
7615 	.rpc_call_done = nfs4_release_lockowner_done,
7616 	.rpc_release = nfs4_release_lockowner_release,
7617 };
7618 
7619 static void
7620 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7621 {
7622 	struct nfs_release_lockowner_data *data;
7623 	struct rpc_message msg = {
7624 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7625 	};
7626 
7627 	if (server->nfs_client->cl_mvops->minor_version != 0)
7628 		return;
7629 
7630 	data = kmalloc(sizeof(*data), GFP_NOFS);
7631 	if (!data)
7632 		return;
7633 	data->lsp = lsp;
7634 	data->server = server;
7635 	data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7636 	data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7637 	data->args.lock_owner.s_dev = server->s_dev;
7638 
7639 	msg.rpc_argp = &data->args;
7640 	msg.rpc_resp = &data->res;
7641 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7642 	rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7643 }
7644 
7645 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7646 
7647 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7648 				   struct user_namespace *mnt_userns,
7649 				   struct dentry *unused, struct inode *inode,
7650 				   const char *key, const void *buf,
7651 				   size_t buflen, int flags)
7652 {
7653 	return nfs4_proc_set_acl(inode, buf, buflen);
7654 }
7655 
7656 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7657 				   struct dentry *unused, struct inode *inode,
7658 				   const char *key, void *buf, size_t buflen)
7659 {
7660 	return nfs4_proc_get_acl(inode, buf, buflen);
7661 }
7662 
7663 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7664 {
7665 	return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7666 }
7667 
7668 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7669 
7670 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7671 				     struct user_namespace *mnt_userns,
7672 				     struct dentry *unused, struct inode *inode,
7673 				     const char *key, const void *buf,
7674 				     size_t buflen, int flags)
7675 {
7676 	if (security_ismaclabel(key))
7677 		return nfs4_set_security_label(inode, buf, buflen);
7678 
7679 	return -EOPNOTSUPP;
7680 }
7681 
7682 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7683 				     struct dentry *unused, struct inode *inode,
7684 				     const char *key, void *buf, size_t buflen)
7685 {
7686 	if (security_ismaclabel(key))
7687 		return nfs4_get_security_label(inode, buf, buflen);
7688 	return -EOPNOTSUPP;
7689 }
7690 
7691 static ssize_t
7692 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7693 {
7694 	int len = 0;
7695 
7696 	if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7697 		len = security_inode_listsecurity(inode, list, list_len);
7698 		if (len >= 0 && list_len && len > list_len)
7699 			return -ERANGE;
7700 	}
7701 	return len;
7702 }
7703 
7704 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7705 	.prefix = XATTR_SECURITY_PREFIX,
7706 	.get	= nfs4_xattr_get_nfs4_label,
7707 	.set	= nfs4_xattr_set_nfs4_label,
7708 };
7709 
7710 #else
7711 
7712 static ssize_t
7713 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7714 {
7715 	return 0;
7716 }
7717 
7718 #endif
7719 
7720 #ifdef CONFIG_NFS_V4_2
7721 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7722 				    struct user_namespace *mnt_userns,
7723 				    struct dentry *unused, struct inode *inode,
7724 				    const char *key, const void *buf,
7725 				    size_t buflen, int flags)
7726 {
7727 	u32 mask;
7728 	int ret;
7729 
7730 	if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7731 		return -EOPNOTSUPP;
7732 
7733 	/*
7734 	 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7735 	 * flags right now. Handling of xattr operations use the normal
7736 	 * file read/write permissions.
7737 	 *
7738 	 * Just in case the server has other ideas (which RFC 8276 allows),
7739 	 * do a cached access check for the XA* flags to possibly avoid
7740 	 * doing an RPC and getting EACCES back.
7741 	 */
7742 	if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7743 		if (!(mask & NFS_ACCESS_XAWRITE))
7744 			return -EACCES;
7745 	}
7746 
7747 	if (buf == NULL) {
7748 		ret = nfs42_proc_removexattr(inode, key);
7749 		if (!ret)
7750 			nfs4_xattr_cache_remove(inode, key);
7751 	} else {
7752 		ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7753 		if (!ret)
7754 			nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7755 	}
7756 
7757 	return ret;
7758 }
7759 
7760 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7761 				    struct dentry *unused, struct inode *inode,
7762 				    const char *key, void *buf, size_t buflen)
7763 {
7764 	u32 mask;
7765 	ssize_t ret;
7766 
7767 	if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7768 		return -EOPNOTSUPP;
7769 
7770 	if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7771 		if (!(mask & NFS_ACCESS_XAREAD))
7772 			return -EACCES;
7773 	}
7774 
7775 	ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7776 	if (ret)
7777 		return ret;
7778 
7779 	ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7780 	if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7781 		return ret;
7782 
7783 	ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7784 
7785 	return ret;
7786 }
7787 
7788 static ssize_t
7789 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7790 {
7791 	u64 cookie;
7792 	bool eof;
7793 	ssize_t ret, size;
7794 	char *buf;
7795 	size_t buflen;
7796 	u32 mask;
7797 
7798 	if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7799 		return 0;
7800 
7801 	if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7802 		if (!(mask & NFS_ACCESS_XALIST))
7803 			return 0;
7804 	}
7805 
7806 	ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7807 	if (ret)
7808 		return ret;
7809 
7810 	ret = nfs4_xattr_cache_list(inode, list, list_len);
7811 	if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7812 		return ret;
7813 
7814 	cookie = 0;
7815 	eof = false;
7816 	buflen = list_len ? list_len : XATTR_LIST_MAX;
7817 	buf = list_len ? list : NULL;
7818 	size = 0;
7819 
7820 	while (!eof) {
7821 		ret = nfs42_proc_listxattrs(inode, buf, buflen,
7822 		    &cookie, &eof);
7823 		if (ret < 0)
7824 			return ret;
7825 
7826 		if (list_len) {
7827 			buf += ret;
7828 			buflen -= ret;
7829 		}
7830 		size += ret;
7831 	}
7832 
7833 	if (list_len)
7834 		nfs4_xattr_cache_set_list(inode, list, size);
7835 
7836 	return size;
7837 }
7838 
7839 #else
7840 
7841 static ssize_t
7842 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7843 {
7844 	return 0;
7845 }
7846 #endif /* CONFIG_NFS_V4_2 */
7847 
7848 /*
7849  * nfs_fhget will use either the mounted_on_fileid or the fileid
7850  */
7851 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7852 {
7853 	if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7854 	       (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7855 	      (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7856 	      (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7857 		return;
7858 
7859 	fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7860 		NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7861 	fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7862 	fattr->nlink = 2;
7863 }
7864 
7865 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7866 				   const struct qstr *name,
7867 				   struct nfs4_fs_locations *fs_locations,
7868 				   struct page *page)
7869 {
7870 	struct nfs_server *server = NFS_SERVER(dir);
7871 	u32 bitmask[3];
7872 	struct nfs4_fs_locations_arg args = {
7873 		.dir_fh = NFS_FH(dir),
7874 		.name = name,
7875 		.page = page,
7876 		.bitmask = bitmask,
7877 	};
7878 	struct nfs4_fs_locations_res res = {
7879 		.fs_locations = fs_locations,
7880 	};
7881 	struct rpc_message msg = {
7882 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7883 		.rpc_argp = &args,
7884 		.rpc_resp = &res,
7885 	};
7886 	int status;
7887 
7888 	dprintk("%s: start\n", __func__);
7889 
7890 	bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7891 	bitmask[1] = nfs4_fattr_bitmap[1];
7892 
7893 	/* Ask for the fileid of the absent filesystem if mounted_on_fileid
7894 	 * is not supported */
7895 	if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7896 		bitmask[0] &= ~FATTR4_WORD0_FILEID;
7897 	else
7898 		bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7899 
7900 	nfs_fattr_init(&fs_locations->fattr);
7901 	fs_locations->server = server;
7902 	fs_locations->nlocations = 0;
7903 	status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7904 	dprintk("%s: returned status = %d\n", __func__, status);
7905 	return status;
7906 }
7907 
7908 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7909 			   const struct qstr *name,
7910 			   struct nfs4_fs_locations *fs_locations,
7911 			   struct page *page)
7912 {
7913 	struct nfs4_exception exception = {
7914 		.interruptible = true,
7915 	};
7916 	int err;
7917 	do {
7918 		err = _nfs4_proc_fs_locations(client, dir, name,
7919 				fs_locations, page);
7920 		trace_nfs4_get_fs_locations(dir, name, err);
7921 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
7922 				&exception);
7923 	} while (exception.retry);
7924 	return err;
7925 }
7926 
7927 /*
7928  * This operation also signals the server that this client is
7929  * performing migration recovery.  The server can stop returning
7930  * NFS4ERR_LEASE_MOVED to this client.  A RENEW operation is
7931  * appended to this compound to identify the client ID which is
7932  * performing recovery.
7933  */
7934 static int _nfs40_proc_get_locations(struct nfs_server *server,
7935 				     struct nfs_fh *fhandle,
7936 				     struct nfs4_fs_locations *locations,
7937 				     struct page *page, const struct cred *cred)
7938 {
7939 	struct rpc_clnt *clnt = server->client;
7940 	u32 bitmask[2] = {
7941 		[0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7942 	};
7943 	struct nfs4_fs_locations_arg args = {
7944 		.clientid	= server->nfs_client->cl_clientid,
7945 		.fh		= fhandle,
7946 		.page		= page,
7947 		.bitmask	= bitmask,
7948 		.migration	= 1,		/* skip LOOKUP */
7949 		.renew		= 1,		/* append RENEW */
7950 	};
7951 	struct nfs4_fs_locations_res res = {
7952 		.fs_locations	= locations,
7953 		.migration	= 1,
7954 		.renew		= 1,
7955 	};
7956 	struct rpc_message msg = {
7957 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7958 		.rpc_argp	= &args,
7959 		.rpc_resp	= &res,
7960 		.rpc_cred	= cred,
7961 	};
7962 	unsigned long now = jiffies;
7963 	int status;
7964 
7965 	nfs_fattr_init(&locations->fattr);
7966 	locations->server = server;
7967 	locations->nlocations = 0;
7968 
7969 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7970 	status = nfs4_call_sync_sequence(clnt, server, &msg,
7971 					&args.seq_args, &res.seq_res);
7972 	if (status)
7973 		return status;
7974 
7975 	renew_lease(server, now);
7976 	return 0;
7977 }
7978 
7979 #ifdef CONFIG_NFS_V4_1
7980 
7981 /*
7982  * This operation also signals the server that this client is
7983  * performing migration recovery.  The server can stop asserting
7984  * SEQ4_STATUS_LEASE_MOVED for this client.  The client ID
7985  * performing this operation is identified in the SEQUENCE
7986  * operation in this compound.
7987  *
7988  * When the client supports GETATTR(fs_locations_info), it can
7989  * be plumbed in here.
7990  */
7991 static int _nfs41_proc_get_locations(struct nfs_server *server,
7992 				     struct nfs_fh *fhandle,
7993 				     struct nfs4_fs_locations *locations,
7994 				     struct page *page, const struct cred *cred)
7995 {
7996 	struct rpc_clnt *clnt = server->client;
7997 	u32 bitmask[2] = {
7998 		[0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7999 	};
8000 	struct nfs4_fs_locations_arg args = {
8001 		.fh		= fhandle,
8002 		.page		= page,
8003 		.bitmask	= bitmask,
8004 		.migration	= 1,		/* skip LOOKUP */
8005 	};
8006 	struct nfs4_fs_locations_res res = {
8007 		.fs_locations	= locations,
8008 		.migration	= 1,
8009 	};
8010 	struct rpc_message msg = {
8011 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8012 		.rpc_argp	= &args,
8013 		.rpc_resp	= &res,
8014 		.rpc_cred	= cred,
8015 	};
8016 	int status;
8017 
8018 	nfs_fattr_init(&locations->fattr);
8019 	locations->server = server;
8020 	locations->nlocations = 0;
8021 
8022 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8023 	status = nfs4_call_sync_sequence(clnt, server, &msg,
8024 					&args.seq_args, &res.seq_res);
8025 	if (status == NFS4_OK &&
8026 	    res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8027 		status = -NFS4ERR_LEASE_MOVED;
8028 	return status;
8029 }
8030 
8031 #endif	/* CONFIG_NFS_V4_1 */
8032 
8033 /**
8034  * nfs4_proc_get_locations - discover locations for a migrated FSID
8035  * @inode: inode on FSID that is migrating
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