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