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