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