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