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