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