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