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