xref: /openbmc/linux/fs/nfs/nfs4proc.c (revision 367e5927)
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 	if (!opendata->cancelled)
2937 		nfs4_sequence_free_slot(&opendata->o_res.seq_res);
2938 	return ret;
2939 }
2940 
2941 /*
2942  * Returns a referenced nfs4_state
2943  */
2944 static int _nfs4_do_open(struct inode *dir,
2945 			struct nfs_open_context *ctx,
2946 			int flags,
2947 			const struct nfs4_open_createattrs *c,
2948 			int *opened)
2949 {
2950 	struct nfs4_state_owner  *sp;
2951 	struct nfs4_state     *state = NULL;
2952 	struct nfs_server       *server = NFS_SERVER(dir);
2953 	struct nfs4_opendata *opendata;
2954 	struct dentry *dentry = ctx->dentry;
2955 	const struct cred *cred = ctx->cred;
2956 	struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2957 	fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2958 	enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2959 	struct iattr *sattr = c->sattr;
2960 	struct nfs4_label *label = c->label;
2961 	struct nfs4_label *olabel = NULL;
2962 	int status;
2963 
2964 	/* Protect against reboot recovery conflicts */
2965 	status = -ENOMEM;
2966 	sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2967 	if (sp == NULL) {
2968 		dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2969 		goto out_err;
2970 	}
2971 	status = nfs4_client_recover_expired_lease(server->nfs_client);
2972 	if (status != 0)
2973 		goto err_put_state_owner;
2974 	if (d_really_is_positive(dentry))
2975 		nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2976 	status = -ENOMEM;
2977 	if (d_really_is_positive(dentry))
2978 		claim = NFS4_OPEN_CLAIM_FH;
2979 	opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2980 			c, claim, GFP_KERNEL);
2981 	if (opendata == NULL)
2982 		goto err_put_state_owner;
2983 
2984 	if (label) {
2985 		olabel = nfs4_label_alloc(server, GFP_KERNEL);
2986 		if (IS_ERR(olabel)) {
2987 			status = PTR_ERR(olabel);
2988 			goto err_opendata_put;
2989 		}
2990 	}
2991 
2992 	if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2993 		if (!opendata->f_attr.mdsthreshold) {
2994 			opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2995 			if (!opendata->f_attr.mdsthreshold)
2996 				goto err_free_label;
2997 		}
2998 		opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2999 	}
3000 	if (d_really_is_positive(dentry))
3001 		opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3002 
3003 	status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
3004 	if (status != 0)
3005 		goto err_free_label;
3006 	state = ctx->state;
3007 
3008 	if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3009 	    (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3010 		unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3011 		/*
3012 		 * send create attributes which was not set by open
3013 		 * with an extra setattr.
3014 		 */
3015 		if (attrs || label) {
3016 			unsigned ia_old = sattr->ia_valid;
3017 
3018 			sattr->ia_valid = attrs;
3019 			nfs_fattr_init(opendata->o_res.f_attr);
3020 			status = nfs4_do_setattr(state->inode, cred,
3021 					opendata->o_res.f_attr, sattr,
3022 					ctx, label, olabel);
3023 			if (status == 0) {
3024 				nfs_setattr_update_inode(state->inode, sattr,
3025 						opendata->o_res.f_attr);
3026 				nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3027 			}
3028 			sattr->ia_valid = ia_old;
3029 		}
3030 	}
3031 	if (opened && opendata->file_created)
3032 		*opened = 1;
3033 
3034 	if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3035 		*ctx_th = opendata->f_attr.mdsthreshold;
3036 		opendata->f_attr.mdsthreshold = NULL;
3037 	}
3038 
3039 	nfs4_label_free(olabel);
3040 
3041 	nfs4_opendata_put(opendata);
3042 	nfs4_put_state_owner(sp);
3043 	return 0;
3044 err_free_label:
3045 	nfs4_label_free(olabel);
3046 err_opendata_put:
3047 	nfs4_opendata_put(opendata);
3048 err_put_state_owner:
3049 	nfs4_put_state_owner(sp);
3050 out_err:
3051 	return status;
3052 }
3053 
3054 
3055 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3056 					struct nfs_open_context *ctx,
3057 					int flags,
3058 					struct iattr *sattr,
3059 					struct nfs4_label *label,
3060 					int *opened)
3061 {
3062 	struct nfs_server *server = NFS_SERVER(dir);
3063 	struct nfs4_exception exception = { };
3064 	struct nfs4_state *res;
3065 	struct nfs4_open_createattrs c = {
3066 		.label = label,
3067 		.sattr = sattr,
3068 		.verf = {
3069 			[0] = (__u32)jiffies,
3070 			[1] = (__u32)current->pid,
3071 		},
3072 	};
3073 	int status;
3074 
3075 	do {
3076 		status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3077 		res = ctx->state;
3078 		trace_nfs4_open_file(ctx, flags, status);
3079 		if (status == 0)
3080 			break;
3081 		/* NOTE: BAD_SEQID means the server and client disagree about the
3082 		 * book-keeping w.r.t. state-changing operations
3083 		 * (OPEN/CLOSE/LOCK/LOCKU...)
3084 		 * It is actually a sign of a bug on the client or on the server.
3085 		 *
3086 		 * If we receive a BAD_SEQID error in the particular case of
3087 		 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3088 		 * have unhashed the old state_owner for us, and that we can
3089 		 * therefore safely retry using a new one. We should still warn
3090 		 * the user though...
3091 		 */
3092 		if (status == -NFS4ERR_BAD_SEQID) {
3093 			pr_warn_ratelimited("NFS: v4 server %s "
3094 					" returned a bad sequence-id error!\n",
3095 					NFS_SERVER(dir)->nfs_client->cl_hostname);
3096 			exception.retry = 1;
3097 			continue;
3098 		}
3099 		/*
3100 		 * BAD_STATEID on OPEN means that the server cancelled our
3101 		 * state before it received the OPEN_CONFIRM.
3102 		 * Recover by retrying the request as per the discussion
3103 		 * on Page 181 of RFC3530.
3104 		 */
3105 		if (status == -NFS4ERR_BAD_STATEID) {
3106 			exception.retry = 1;
3107 			continue;
3108 		}
3109 		if (status == -EAGAIN) {
3110 			/* We must have found a delegation */
3111 			exception.retry = 1;
3112 			continue;
3113 		}
3114 		if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3115 			continue;
3116 		res = ERR_PTR(nfs4_handle_exception(server,
3117 					status, &exception));
3118 	} while (exception.retry);
3119 	return res;
3120 }
3121 
3122 static int _nfs4_do_setattr(struct inode *inode,
3123 			    struct nfs_setattrargs *arg,
3124 			    struct nfs_setattrres *res,
3125 			    const struct cred *cred,
3126 			    struct nfs_open_context *ctx)
3127 {
3128 	struct nfs_server *server = NFS_SERVER(inode);
3129 	struct rpc_message msg = {
3130 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3131 		.rpc_argp	= arg,
3132 		.rpc_resp	= res,
3133 		.rpc_cred	= cred,
3134 	};
3135 	const struct cred *delegation_cred = NULL;
3136 	unsigned long timestamp = jiffies;
3137 	bool truncate;
3138 	int status;
3139 
3140 	nfs_fattr_init(res->fattr);
3141 
3142 	/* Servers should only apply open mode checks for file size changes */
3143 	truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3144 	if (!truncate)
3145 		goto zero_stateid;
3146 
3147 	if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3148 		/* Use that stateid */
3149 	} else if (ctx != NULL) {
3150 		struct nfs_lock_context *l_ctx;
3151 		if (!nfs4_valid_open_stateid(ctx->state))
3152 			return -EBADF;
3153 		l_ctx = nfs_get_lock_context(ctx);
3154 		if (IS_ERR(l_ctx))
3155 			return PTR_ERR(l_ctx);
3156 		status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3157 						&arg->stateid, &delegation_cred);
3158 		nfs_put_lock_context(l_ctx);
3159 		if (status == -EIO)
3160 			return -EBADF;
3161 	} else {
3162 zero_stateid:
3163 		nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3164 	}
3165 	if (delegation_cred)
3166 		msg.rpc_cred = delegation_cred;
3167 
3168 	status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3169 
3170 	put_cred(delegation_cred);
3171 	if (status == 0 && ctx != NULL)
3172 		renew_lease(server, timestamp);
3173 	trace_nfs4_setattr(inode, &arg->stateid, status);
3174 	return status;
3175 }
3176 
3177 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3178 			   struct nfs_fattr *fattr, struct iattr *sattr,
3179 			   struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3180 			   struct nfs4_label *olabel)
3181 {
3182 	struct nfs_server *server = NFS_SERVER(inode);
3183 	__u32 bitmask[NFS4_BITMASK_SZ];
3184 	struct nfs4_state *state = ctx ? ctx->state : NULL;
3185 	struct nfs_setattrargs	arg = {
3186 		.fh		= NFS_FH(inode),
3187 		.iap		= sattr,
3188 		.server		= server,
3189 		.bitmask = bitmask,
3190 		.label		= ilabel,
3191 	};
3192 	struct nfs_setattrres  res = {
3193 		.fattr		= fattr,
3194 		.label		= olabel,
3195 		.server		= server,
3196 	};
3197 	struct nfs4_exception exception = {
3198 		.state = state,
3199 		.inode = inode,
3200 		.stateid = &arg.stateid,
3201 	};
3202 	int err;
3203 
3204 	do {
3205 		nfs4_bitmap_copy_adjust_setattr(bitmask,
3206 				nfs4_bitmask(server, olabel),
3207 				inode);
3208 
3209 		err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3210 		switch (err) {
3211 		case -NFS4ERR_OPENMODE:
3212 			if (!(sattr->ia_valid & ATTR_SIZE)) {
3213 				pr_warn_once("NFSv4: server %s is incorrectly "
3214 						"applying open mode checks to "
3215 						"a SETATTR that is not "
3216 						"changing file size.\n",
3217 						server->nfs_client->cl_hostname);
3218 			}
3219 			if (state && !(state->state & FMODE_WRITE)) {
3220 				err = -EBADF;
3221 				if (sattr->ia_valid & ATTR_OPEN)
3222 					err = -EACCES;
3223 				goto out;
3224 			}
3225 		}
3226 		err = nfs4_handle_exception(server, err, &exception);
3227 	} while (exception.retry);
3228 out:
3229 	return err;
3230 }
3231 
3232 static bool
3233 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3234 {
3235 	if (inode == NULL || !nfs_have_layout(inode))
3236 		return false;
3237 
3238 	return pnfs_wait_on_layoutreturn(inode, task);
3239 }
3240 
3241 struct nfs4_closedata {
3242 	struct inode *inode;
3243 	struct nfs4_state *state;
3244 	struct nfs_closeargs arg;
3245 	struct nfs_closeres res;
3246 	struct {
3247 		struct nfs4_layoutreturn_args arg;
3248 		struct nfs4_layoutreturn_res res;
3249 		struct nfs4_xdr_opaque_data ld_private;
3250 		u32 roc_barrier;
3251 		bool roc;
3252 	} lr;
3253 	struct nfs_fattr fattr;
3254 	unsigned long timestamp;
3255 };
3256 
3257 static void nfs4_free_closedata(void *data)
3258 {
3259 	struct nfs4_closedata *calldata = data;
3260 	struct nfs4_state_owner *sp = calldata->state->owner;
3261 	struct super_block *sb = calldata->state->inode->i_sb;
3262 
3263 	if (calldata->lr.roc)
3264 		pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3265 				calldata->res.lr_ret);
3266 	nfs4_put_open_state(calldata->state);
3267 	nfs_free_seqid(calldata->arg.seqid);
3268 	nfs4_put_state_owner(sp);
3269 	nfs_sb_deactive(sb);
3270 	kfree(calldata);
3271 }
3272 
3273 static void nfs4_close_done(struct rpc_task *task, void *data)
3274 {
3275 	struct nfs4_closedata *calldata = data;
3276 	struct nfs4_state *state = calldata->state;
3277 	struct nfs_server *server = NFS_SERVER(calldata->inode);
3278 	nfs4_stateid *res_stateid = NULL;
3279 	struct nfs4_exception exception = {
3280 		.state = state,
3281 		.inode = calldata->inode,
3282 		.stateid = &calldata->arg.stateid,
3283 	};
3284 
3285 	dprintk("%s: begin!\n", __func__);
3286 	if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3287 		return;
3288 	trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3289 
3290 	/* Handle Layoutreturn errors */
3291 	if (calldata->arg.lr_args && task->tk_status != 0) {
3292 		switch (calldata->res.lr_ret) {
3293 		default:
3294 			calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3295 			break;
3296 		case 0:
3297 			calldata->arg.lr_args = NULL;
3298 			calldata->res.lr_res = NULL;
3299 			break;
3300 		case -NFS4ERR_OLD_STATEID:
3301 			if (nfs4_layoutreturn_refresh_stateid(&calldata->arg.lr_args->stateid,
3302 						&calldata->arg.lr_args->range,
3303 						calldata->inode))
3304 				goto lr_restart;
3305 			/* Fallthrough */
3306 		case -NFS4ERR_ADMIN_REVOKED:
3307 		case -NFS4ERR_DELEG_REVOKED:
3308 		case -NFS4ERR_EXPIRED:
3309 		case -NFS4ERR_BAD_STATEID:
3310 		case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3311 		case -NFS4ERR_WRONG_CRED:
3312 			calldata->arg.lr_args = NULL;
3313 			calldata->res.lr_res = NULL;
3314 			goto lr_restart;
3315 		}
3316 	}
3317 
3318 	/* hmm. we are done with the inode, and in the process of freeing
3319 	 * the state_owner. we keep this around to process errors
3320 	 */
3321 	switch (task->tk_status) {
3322 		case 0:
3323 			res_stateid = &calldata->res.stateid;
3324 			renew_lease(server, calldata->timestamp);
3325 			break;
3326 		case -NFS4ERR_ACCESS:
3327 			if (calldata->arg.bitmask != NULL) {
3328 				calldata->arg.bitmask = NULL;
3329 				calldata->res.fattr = NULL;
3330 				goto out_restart;
3331 
3332 			}
3333 			break;
3334 		case -NFS4ERR_OLD_STATEID:
3335 			/* Did we race with OPEN? */
3336 			if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
3337 						state))
3338 				goto out_restart;
3339 			goto out_release;
3340 		case -NFS4ERR_ADMIN_REVOKED:
3341 		case -NFS4ERR_STALE_STATEID:
3342 		case -NFS4ERR_EXPIRED:
3343 			nfs4_free_revoked_stateid(server,
3344 					&calldata->arg.stateid,
3345 					task->tk_msg.rpc_cred);
3346 			/* Fallthrough */
3347 		case -NFS4ERR_BAD_STATEID:
3348 			break;
3349 		default:
3350 			task->tk_status = nfs4_async_handle_exception(task,
3351 					server, task->tk_status, &exception);
3352 			if (exception.retry)
3353 				goto out_restart;
3354 	}
3355 	nfs_clear_open_stateid(state, &calldata->arg.stateid,
3356 			res_stateid, calldata->arg.fmode);
3357 out_release:
3358 	task->tk_status = 0;
3359 	nfs_release_seqid(calldata->arg.seqid);
3360 	nfs_refresh_inode(calldata->inode, &calldata->fattr);
3361 	dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3362 	return;
3363 lr_restart:
3364 	calldata->res.lr_ret = 0;
3365 out_restart:
3366 	task->tk_status = 0;
3367 	rpc_restart_call_prepare(task);
3368 	goto out_release;
3369 }
3370 
3371 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3372 {
3373 	struct nfs4_closedata *calldata = data;
3374 	struct nfs4_state *state = calldata->state;
3375 	struct inode *inode = calldata->inode;
3376 	struct pnfs_layout_hdr *lo;
3377 	bool is_rdonly, is_wronly, is_rdwr;
3378 	int call_close = 0;
3379 
3380 	dprintk("%s: begin!\n", __func__);
3381 	if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3382 		goto out_wait;
3383 
3384 	task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3385 	spin_lock(&state->owner->so_lock);
3386 	is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3387 	is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3388 	is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3389 	/* Calculate the change in open mode */
3390 	calldata->arg.fmode = 0;
3391 	if (state->n_rdwr == 0) {
3392 		if (state->n_rdonly == 0)
3393 			call_close |= is_rdonly;
3394 		else if (is_rdonly)
3395 			calldata->arg.fmode |= FMODE_READ;
3396 		if (state->n_wronly == 0)
3397 			call_close |= is_wronly;
3398 		else if (is_wronly)
3399 			calldata->arg.fmode |= FMODE_WRITE;
3400 		if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3401 			call_close |= is_rdwr;
3402 	} else if (is_rdwr)
3403 		calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3404 
3405 	if (!nfs4_valid_open_stateid(state) ||
3406 	    !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
3407 		call_close = 0;
3408 	spin_unlock(&state->owner->so_lock);
3409 
3410 	if (!call_close) {
3411 		/* Note: exit _without_ calling nfs4_close_done */
3412 		goto out_no_action;
3413 	}
3414 
3415 	if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3416 		nfs_release_seqid(calldata->arg.seqid);
3417 		goto out_wait;
3418 	}
3419 
3420 	lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3421 	if (lo && !pnfs_layout_is_valid(lo)) {
3422 		calldata->arg.lr_args = NULL;
3423 		calldata->res.lr_res = NULL;
3424 	}
3425 
3426 	if (calldata->arg.fmode == 0)
3427 		task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3428 
3429 	if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3430 		/* Close-to-open cache consistency revalidation */
3431 		if (!nfs4_have_delegation(inode, FMODE_READ))
3432 			calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3433 		else
3434 			calldata->arg.bitmask = NULL;
3435 	}
3436 
3437 	calldata->arg.share_access =
3438 		nfs4_map_atomic_open_share(NFS_SERVER(inode),
3439 				calldata->arg.fmode, 0);
3440 
3441 	if (calldata->res.fattr == NULL)
3442 		calldata->arg.bitmask = NULL;
3443 	else if (calldata->arg.bitmask == NULL)
3444 		calldata->res.fattr = NULL;
3445 	calldata->timestamp = jiffies;
3446 	if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3447 				&calldata->arg.seq_args,
3448 				&calldata->res.seq_res,
3449 				task) != 0)
3450 		nfs_release_seqid(calldata->arg.seqid);
3451 	dprintk("%s: done!\n", __func__);
3452 	return;
3453 out_no_action:
3454 	task->tk_action = NULL;
3455 out_wait:
3456 	nfs4_sequence_done(task, &calldata->res.seq_res);
3457 }
3458 
3459 static const struct rpc_call_ops nfs4_close_ops = {
3460 	.rpc_call_prepare = nfs4_close_prepare,
3461 	.rpc_call_done = nfs4_close_done,
3462 	.rpc_release = nfs4_free_closedata,
3463 };
3464 
3465 /*
3466  * It is possible for data to be read/written from a mem-mapped file
3467  * after the sys_close call (which hits the vfs layer as a flush).
3468  * This means that we can't safely call nfsv4 close on a file until
3469  * the inode is cleared. This in turn means that we are not good
3470  * NFSv4 citizens - we do not indicate to the server to update the file's
3471  * share state even when we are done with one of the three share
3472  * stateid's in the inode.
3473  *
3474  * NOTE: Caller must be holding the sp->so_owner semaphore!
3475  */
3476 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3477 {
3478 	struct nfs_server *server = NFS_SERVER(state->inode);
3479 	struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3480 	struct nfs4_closedata *calldata;
3481 	struct nfs4_state_owner *sp = state->owner;
3482 	struct rpc_task *task;
3483 	struct rpc_message msg = {
3484 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3485 		.rpc_cred = state->owner->so_cred,
3486 	};
3487 	struct rpc_task_setup task_setup_data = {
3488 		.rpc_client = server->client,
3489 		.rpc_message = &msg,
3490 		.callback_ops = &nfs4_close_ops,
3491 		.workqueue = nfsiod_workqueue,
3492 		.flags = RPC_TASK_ASYNC,
3493 	};
3494 	int status = -ENOMEM;
3495 
3496 	nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3497 		&task_setup_data.rpc_client, &msg);
3498 
3499 	calldata = kzalloc(sizeof(*calldata), gfp_mask);
3500 	if (calldata == NULL)
3501 		goto out;
3502 	nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3503 	calldata->inode = state->inode;
3504 	calldata->state = state;
3505 	calldata->arg.fh = NFS_FH(state->inode);
3506 	if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3507 		goto out_free_calldata;
3508 	/* Serialization for the sequence id */
3509 	alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3510 	calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3511 	if (IS_ERR(calldata->arg.seqid))
3512 		goto out_free_calldata;
3513 	nfs_fattr_init(&calldata->fattr);
3514 	calldata->arg.fmode = 0;
3515 	calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3516 	calldata->res.fattr = &calldata->fattr;
3517 	calldata->res.seqid = calldata->arg.seqid;
3518 	calldata->res.server = server;
3519 	calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3520 	calldata->lr.roc = pnfs_roc(state->inode,
3521 			&calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3522 	if (calldata->lr.roc) {
3523 		calldata->arg.lr_args = &calldata->lr.arg;
3524 		calldata->res.lr_res = &calldata->lr.res;
3525 	}
3526 	nfs_sb_active(calldata->inode->i_sb);
3527 
3528 	msg.rpc_argp = &calldata->arg;
3529 	msg.rpc_resp = &calldata->res;
3530 	task_setup_data.callback_data = calldata;
3531 	task = rpc_run_task(&task_setup_data);
3532 	if (IS_ERR(task))
3533 		return PTR_ERR(task);
3534 	status = 0;
3535 	if (wait)
3536 		status = rpc_wait_for_completion_task(task);
3537 	rpc_put_task(task);
3538 	return status;
3539 out_free_calldata:
3540 	kfree(calldata);
3541 out:
3542 	nfs4_put_open_state(state);
3543 	nfs4_put_state_owner(sp);
3544 	return status;
3545 }
3546 
3547 static struct inode *
3548 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3549 		int open_flags, struct iattr *attr, int *opened)
3550 {
3551 	struct nfs4_state *state;
3552 	struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3553 
3554 	label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3555 
3556 	/* Protect against concurrent sillydeletes */
3557 	state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3558 
3559 	nfs4_label_release_security(label);
3560 
3561 	if (IS_ERR(state))
3562 		return ERR_CAST(state);
3563 	return state->inode;
3564 }
3565 
3566 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3567 {
3568 	if (ctx->state == NULL)
3569 		return;
3570 	if (is_sync)
3571 		nfs4_close_sync(ctx->state, ctx->mode);
3572 	else
3573 		nfs4_close_state(ctx->state, ctx->mode);
3574 }
3575 
3576 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3577 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3578 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3579 
3580 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3581 {
3582 	u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3583 	struct nfs4_server_caps_arg args = {
3584 		.fhandle = fhandle,
3585 		.bitmask = bitmask,
3586 	};
3587 	struct nfs4_server_caps_res res = {};
3588 	struct rpc_message msg = {
3589 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3590 		.rpc_argp = &args,
3591 		.rpc_resp = &res,
3592 	};
3593 	int status;
3594 	int i;
3595 
3596 	bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3597 		     FATTR4_WORD0_FH_EXPIRE_TYPE |
3598 		     FATTR4_WORD0_LINK_SUPPORT |
3599 		     FATTR4_WORD0_SYMLINK_SUPPORT |
3600 		     FATTR4_WORD0_ACLSUPPORT;
3601 	if (minorversion)
3602 		bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3603 
3604 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3605 	if (status == 0) {
3606 		/* Sanity check the server answers */
3607 		switch (minorversion) {
3608 		case 0:
3609 			res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3610 			res.attr_bitmask[2] = 0;
3611 			break;
3612 		case 1:
3613 			res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3614 			break;
3615 		case 2:
3616 			res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3617 		}
3618 		memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3619 		server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3620 				NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3621 				NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3622 				NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3623 				NFS_CAP_CTIME|NFS_CAP_MTIME|
3624 				NFS_CAP_SECURITY_LABEL);
3625 		if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3626 				res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3627 			server->caps |= NFS_CAP_ACLS;
3628 		if (res.has_links != 0)
3629 			server->caps |= NFS_CAP_HARDLINKS;
3630 		if (res.has_symlinks != 0)
3631 			server->caps |= NFS_CAP_SYMLINKS;
3632 		if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3633 			server->caps |= NFS_CAP_FILEID;
3634 		if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3635 			server->caps |= NFS_CAP_MODE;
3636 		if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3637 			server->caps |= NFS_CAP_NLINK;
3638 		if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3639 			server->caps |= NFS_CAP_OWNER;
3640 		if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3641 			server->caps |= NFS_CAP_OWNER_GROUP;
3642 		if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3643 			server->caps |= NFS_CAP_ATIME;
3644 		if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3645 			server->caps |= NFS_CAP_CTIME;
3646 		if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3647 			server->caps |= NFS_CAP_MTIME;
3648 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3649 		if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3650 			server->caps |= NFS_CAP_SECURITY_LABEL;
3651 #endif
3652 		memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3653 				sizeof(server->attr_bitmask));
3654 		server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3655 
3656 		memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3657 		server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3658 		server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3659 		server->cache_consistency_bitmask[2] = 0;
3660 
3661 		/* Avoid a regression due to buggy server */
3662 		for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3663 			res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3664 		memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3665 			sizeof(server->exclcreat_bitmask));
3666 
3667 		server->acl_bitmask = res.acl_bitmask;
3668 		server->fh_expire_type = res.fh_expire_type;
3669 	}
3670 
3671 	return status;
3672 }
3673 
3674 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3675 {
3676 	struct nfs4_exception exception = { };
3677 	int err;
3678 	do {
3679 		err = nfs4_handle_exception(server,
3680 				_nfs4_server_capabilities(server, fhandle),
3681 				&exception);
3682 	} while (exception.retry);
3683 	return err;
3684 }
3685 
3686 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3687 		struct nfs_fsinfo *info)
3688 {
3689 	u32 bitmask[3];
3690 	struct nfs4_lookup_root_arg args = {
3691 		.bitmask = bitmask,
3692 	};
3693 	struct nfs4_lookup_res res = {
3694 		.server = server,
3695 		.fattr = info->fattr,
3696 		.fh = fhandle,
3697 	};
3698 	struct rpc_message msg = {
3699 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3700 		.rpc_argp = &args,
3701 		.rpc_resp = &res,
3702 	};
3703 
3704 	bitmask[0] = nfs4_fattr_bitmap[0];
3705 	bitmask[1] = nfs4_fattr_bitmap[1];
3706 	/*
3707 	 * Process the label in the upcoming getfattr
3708 	 */
3709 	bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3710 
3711 	nfs_fattr_init(info->fattr);
3712 	return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3713 }
3714 
3715 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3716 		struct nfs_fsinfo *info)
3717 {
3718 	struct nfs4_exception exception = { };
3719 	int err;
3720 	do {
3721 		err = _nfs4_lookup_root(server, fhandle, info);
3722 		trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3723 		switch (err) {
3724 		case 0:
3725 		case -NFS4ERR_WRONGSEC:
3726 			goto out;
3727 		default:
3728 			err = nfs4_handle_exception(server, err, &exception);
3729 		}
3730 	} while (exception.retry);
3731 out:
3732 	return err;
3733 }
3734 
3735 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3736 				struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3737 {
3738 	struct rpc_auth_create_args auth_args = {
3739 		.pseudoflavor = flavor,
3740 	};
3741 	struct rpc_auth *auth;
3742 
3743 	auth = rpcauth_create(&auth_args, server->client);
3744 	if (IS_ERR(auth))
3745 		return -EACCES;
3746 	return nfs4_lookup_root(server, fhandle, info);
3747 }
3748 
3749 /*
3750  * Retry pseudoroot lookup with various security flavors.  We do this when:
3751  *
3752  *   NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3753  *   NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3754  *
3755  * Returns zero on success, or a negative NFS4ERR value, or a
3756  * negative errno value.
3757  */
3758 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3759 			      struct nfs_fsinfo *info)
3760 {
3761 	/* Per 3530bis 15.33.5 */
3762 	static const rpc_authflavor_t flav_array[] = {
3763 		RPC_AUTH_GSS_KRB5P,
3764 		RPC_AUTH_GSS_KRB5I,
3765 		RPC_AUTH_GSS_KRB5,
3766 		RPC_AUTH_UNIX,			/* courtesy */
3767 		RPC_AUTH_NULL,
3768 	};
3769 	int status = -EPERM;
3770 	size_t i;
3771 
3772 	if (server->auth_info.flavor_len > 0) {
3773 		/* try each flavor specified by user */
3774 		for (i = 0; i < server->auth_info.flavor_len; i++) {
3775 			status = nfs4_lookup_root_sec(server, fhandle, info,
3776 						server->auth_info.flavors[i]);
3777 			if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3778 				continue;
3779 			break;
3780 		}
3781 	} else {
3782 		/* no flavors specified by user, try default list */
3783 		for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3784 			status = nfs4_lookup_root_sec(server, fhandle, info,
3785 						      flav_array[i]);
3786 			if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3787 				continue;
3788 			break;
3789 		}
3790 	}
3791 
3792 	/*
3793 	 * -EACCES could mean that the user doesn't have correct permissions
3794 	 * to access the mount.  It could also mean that we tried to mount
3795 	 * with a gss auth flavor, but rpc.gssd isn't running.  Either way,
3796 	 * existing mount programs don't handle -EACCES very well so it should
3797 	 * be mapped to -EPERM instead.
3798 	 */
3799 	if (status == -EACCES)
3800 		status = -EPERM;
3801 	return status;
3802 }
3803 
3804 /**
3805  * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3806  * @server: initialized nfs_server handle
3807  * @fhandle: we fill in the pseudo-fs root file handle
3808  * @info: we fill in an FSINFO struct
3809  * @auth_probe: probe the auth flavours
3810  *
3811  * Returns zero on success, or a negative errno.
3812  */
3813 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3814 			 struct nfs_fsinfo *info,
3815 			 bool auth_probe)
3816 {
3817 	int status = 0;
3818 
3819 	if (!auth_probe)
3820 		status = nfs4_lookup_root(server, fhandle, info);
3821 
3822 	if (auth_probe || status == NFS4ERR_WRONGSEC)
3823 		status = server->nfs_client->cl_mvops->find_root_sec(server,
3824 				fhandle, info);
3825 
3826 	if (status == 0)
3827 		status = nfs4_server_capabilities(server, fhandle);
3828 	if (status == 0)
3829 		status = nfs4_do_fsinfo(server, fhandle, info);
3830 
3831 	return nfs4_map_errors(status);
3832 }
3833 
3834 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3835 			      struct nfs_fsinfo *info)
3836 {
3837 	int error;
3838 	struct nfs_fattr *fattr = info->fattr;
3839 	struct nfs4_label *label = NULL;
3840 
3841 	error = nfs4_server_capabilities(server, mntfh);
3842 	if (error < 0) {
3843 		dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3844 		return error;
3845 	}
3846 
3847 	label = nfs4_label_alloc(server, GFP_KERNEL);
3848 	if (IS_ERR(label))
3849 		return PTR_ERR(label);
3850 
3851 	error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
3852 	if (error < 0) {
3853 		dprintk("nfs4_get_root: getattr error = %d\n", -error);
3854 		goto err_free_label;
3855 	}
3856 
3857 	if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3858 	    !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3859 		memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3860 
3861 err_free_label:
3862 	nfs4_label_free(label);
3863 
3864 	return error;
3865 }
3866 
3867 /*
3868  * Get locations and (maybe) other attributes of a referral.
3869  * Note that we'll actually follow the referral later when
3870  * we detect fsid mismatch in inode revalidation
3871  */
3872 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3873 			     const struct qstr *name, struct nfs_fattr *fattr,
3874 			     struct nfs_fh *fhandle)
3875 {
3876 	int status = -ENOMEM;
3877 	struct page *page = NULL;
3878 	struct nfs4_fs_locations *locations = NULL;
3879 
3880 	page = alloc_page(GFP_KERNEL);
3881 	if (page == NULL)
3882 		goto out;
3883 	locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3884 	if (locations == NULL)
3885 		goto out;
3886 
3887 	status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3888 	if (status != 0)
3889 		goto out;
3890 
3891 	/*
3892 	 * If the fsid didn't change, this is a migration event, not a
3893 	 * referral.  Cause us to drop into the exception handler, which
3894 	 * will kick off migration recovery.
3895 	 */
3896 	if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3897 		dprintk("%s: server did not return a different fsid for"
3898 			" a referral at %s\n", __func__, name->name);
3899 		status = -NFS4ERR_MOVED;
3900 		goto out;
3901 	}
3902 	/* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3903 	nfs_fixup_referral_attributes(&locations->fattr);
3904 
3905 	/* replace the lookup nfs_fattr with the locations nfs_fattr */
3906 	memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3907 	memset(fhandle, 0, sizeof(struct nfs_fh));
3908 out:
3909 	if (page)
3910 		__free_page(page);
3911 	kfree(locations);
3912 	return status;
3913 }
3914 
3915 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3916 				struct nfs_fattr *fattr, struct nfs4_label *label,
3917 				struct inode *inode)
3918 {
3919 	__u32 bitmask[NFS4_BITMASK_SZ];
3920 	struct nfs4_getattr_arg args = {
3921 		.fh = fhandle,
3922 		.bitmask = bitmask,
3923 	};
3924 	struct nfs4_getattr_res res = {
3925 		.fattr = fattr,
3926 		.label = label,
3927 		.server = server,
3928 	};
3929 	struct rpc_message msg = {
3930 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3931 		.rpc_argp = &args,
3932 		.rpc_resp = &res,
3933 	};
3934 
3935 	nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
3936 
3937 	nfs_fattr_init(fattr);
3938 	return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3939 }
3940 
3941 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3942 				struct nfs_fattr *fattr, struct nfs4_label *label,
3943 				struct inode *inode)
3944 {
3945 	struct nfs4_exception exception = { };
3946 	int err;
3947 	do {
3948 		err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
3949 		trace_nfs4_getattr(server, fhandle, fattr, err);
3950 		err = nfs4_handle_exception(server, err,
3951 				&exception);
3952 	} while (exception.retry);
3953 	return err;
3954 }
3955 
3956 /*
3957  * The file is not closed if it is opened due to the a request to change
3958  * the size of the file. The open call will not be needed once the
3959  * VFS layer lookup-intents are implemented.
3960  *
3961  * Close is called when the inode is destroyed.
3962  * If we haven't opened the file for O_WRONLY, we
3963  * need to in the size_change case to obtain a stateid.
3964  *
3965  * Got race?
3966  * Because OPEN is always done by name in nfsv4, it is
3967  * possible that we opened a different file by the same
3968  * name.  We can recognize this race condition, but we
3969  * can't do anything about it besides returning an error.
3970  *
3971  * This will be fixed with VFS changes (lookup-intent).
3972  */
3973 static int
3974 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3975 		  struct iattr *sattr)
3976 {
3977 	struct inode *inode = d_inode(dentry);
3978 	const struct cred *cred = NULL;
3979 	struct nfs_open_context *ctx = NULL;
3980 	struct nfs4_label *label = NULL;
3981 	int status;
3982 
3983 	if (pnfs_ld_layoutret_on_setattr(inode) &&
3984 	    sattr->ia_valid & ATTR_SIZE &&
3985 	    sattr->ia_size < i_size_read(inode))
3986 		pnfs_commit_and_return_layout(inode);
3987 
3988 	nfs_fattr_init(fattr);
3989 
3990 	/* Deal with open(O_TRUNC) */
3991 	if (sattr->ia_valid & ATTR_OPEN)
3992 		sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3993 
3994 	/* Optimization: if the end result is no change, don't RPC */
3995 	if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3996 		return 0;
3997 
3998 	/* Search for an existing open(O_WRITE) file */
3999 	if (sattr->ia_valid & ATTR_FILE) {
4000 
4001 		ctx = nfs_file_open_context(sattr->ia_file);
4002 		if (ctx)
4003 			cred = ctx->cred;
4004 	}
4005 
4006 	label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4007 	if (IS_ERR(label))
4008 		return PTR_ERR(label);
4009 
4010 	/* Return any delegations if we're going to change ACLs */
4011 	if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4012 		nfs4_inode_make_writeable(inode);
4013 
4014 	status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4015 	if (status == 0) {
4016 		nfs_setattr_update_inode(inode, sattr, fattr);
4017 		nfs_setsecurity(inode, fattr, label);
4018 	}
4019 	nfs4_label_free(label);
4020 	return status;
4021 }
4022 
4023 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4024 		const struct qstr *name, struct nfs_fh *fhandle,
4025 		struct nfs_fattr *fattr, struct nfs4_label *label)
4026 {
4027 	struct nfs_server *server = NFS_SERVER(dir);
4028 	int		       status;
4029 	struct nfs4_lookup_arg args = {
4030 		.bitmask = server->attr_bitmask,
4031 		.dir_fh = NFS_FH(dir),
4032 		.name = name,
4033 	};
4034 	struct nfs4_lookup_res res = {
4035 		.server = server,
4036 		.fattr = fattr,
4037 		.label = label,
4038 		.fh = fhandle,
4039 	};
4040 	struct rpc_message msg = {
4041 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4042 		.rpc_argp = &args,
4043 		.rpc_resp = &res,
4044 	};
4045 
4046 	args.bitmask = nfs4_bitmask(server, label);
4047 
4048 	nfs_fattr_init(fattr);
4049 
4050 	dprintk("NFS call  lookup %s\n", name->name);
4051 	status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
4052 	dprintk("NFS reply lookup: %d\n", status);
4053 	return status;
4054 }
4055 
4056 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4057 {
4058 	fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4059 		NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4060 	fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4061 	fattr->nlink = 2;
4062 }
4063 
4064 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4065 				   const struct qstr *name, struct nfs_fh *fhandle,
4066 				   struct nfs_fattr *fattr, struct nfs4_label *label)
4067 {
4068 	struct nfs4_exception exception = { };
4069 	struct rpc_clnt *client = *clnt;
4070 	int err;
4071 	do {
4072 		err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
4073 		trace_nfs4_lookup(dir, name, err);
4074 		switch (err) {
4075 		case -NFS4ERR_BADNAME:
4076 			err = -ENOENT;
4077 			goto out;
4078 		case -NFS4ERR_MOVED:
4079 			err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4080 			if (err == -NFS4ERR_MOVED)
4081 				err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4082 			goto out;
4083 		case -NFS4ERR_WRONGSEC:
4084 			err = -EPERM;
4085 			if (client != *clnt)
4086 				goto out;
4087 			client = nfs4_negotiate_security(client, dir, name);
4088 			if (IS_ERR(client))
4089 				return PTR_ERR(client);
4090 
4091 			exception.retry = 1;
4092 			break;
4093 		default:
4094 			err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4095 		}
4096 	} while (exception.retry);
4097 
4098 out:
4099 	if (err == 0)
4100 		*clnt = client;
4101 	else if (client != *clnt)
4102 		rpc_shutdown_client(client);
4103 
4104 	return err;
4105 }
4106 
4107 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
4108 			    struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4109 			    struct nfs4_label *label)
4110 {
4111 	int status;
4112 	struct rpc_clnt *client = NFS_CLIENT(dir);
4113 
4114 	status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
4115 	if (client != NFS_CLIENT(dir)) {
4116 		rpc_shutdown_client(client);
4117 		nfs_fixup_secinfo_attributes(fattr);
4118 	}
4119 	return status;
4120 }
4121 
4122 struct rpc_clnt *
4123 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
4124 			    struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4125 {
4126 	struct rpc_clnt *client = NFS_CLIENT(dir);
4127 	int status;
4128 
4129 	status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
4130 	if (status < 0)
4131 		return ERR_PTR(status);
4132 	return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4133 }
4134 
4135 static int _nfs4_proc_lookupp(struct inode *inode,
4136 		struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4137 		struct nfs4_label *label)
4138 {
4139 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
4140 	struct nfs_server *server = NFS_SERVER(inode);
4141 	int		       status;
4142 	struct nfs4_lookupp_arg args = {
4143 		.bitmask = server->attr_bitmask,
4144 		.fh = NFS_FH(inode),
4145 	};
4146 	struct nfs4_lookupp_res res = {
4147 		.server = server,
4148 		.fattr = fattr,
4149 		.label = label,
4150 		.fh = fhandle,
4151 	};
4152 	struct rpc_message msg = {
4153 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4154 		.rpc_argp = &args,
4155 		.rpc_resp = &res,
4156 	};
4157 
4158 	args.bitmask = nfs4_bitmask(server, label);
4159 
4160 	nfs_fattr_init(fattr);
4161 
4162 	dprintk("NFS call  lookupp ino=0x%lx\n", inode->i_ino);
4163 	status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4164 				&res.seq_res, 0);
4165 	dprintk("NFS reply lookupp: %d\n", status);
4166 	return status;
4167 }
4168 
4169 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4170 			     struct nfs_fattr *fattr, struct nfs4_label *label)
4171 {
4172 	struct nfs4_exception exception = { };
4173 	int err;
4174 	do {
4175 		err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4176 		trace_nfs4_lookupp(inode, err);
4177 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
4178 				&exception);
4179 	} while (exception.retry);
4180 	return err;
4181 }
4182 
4183 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4184 {
4185 	struct nfs_server *server = NFS_SERVER(inode);
4186 	struct nfs4_accessargs args = {
4187 		.fh = NFS_FH(inode),
4188 		.access = entry->mask,
4189 	};
4190 	struct nfs4_accessres res = {
4191 		.server = server,
4192 	};
4193 	struct rpc_message msg = {
4194 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4195 		.rpc_argp = &args,
4196 		.rpc_resp = &res,
4197 		.rpc_cred = entry->cred,
4198 	};
4199 	int status = 0;
4200 
4201 	if (!nfs4_have_delegation(inode, FMODE_READ)) {
4202 		res.fattr = nfs_alloc_fattr();
4203 		if (res.fattr == NULL)
4204 			return -ENOMEM;
4205 		args.bitmask = server->cache_consistency_bitmask;
4206 	}
4207 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4208 	if (!status) {
4209 		nfs_access_set_mask(entry, res.access);
4210 		if (res.fattr)
4211 			nfs_refresh_inode(inode, res.fattr);
4212 	}
4213 	nfs_free_fattr(res.fattr);
4214 	return status;
4215 }
4216 
4217 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4218 {
4219 	struct nfs4_exception exception = { };
4220 	int err;
4221 	do {
4222 		err = _nfs4_proc_access(inode, entry);
4223 		trace_nfs4_access(inode, err);
4224 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
4225 				&exception);
4226 	} while (exception.retry);
4227 	return err;
4228 }
4229 
4230 /*
4231  * TODO: For the time being, we don't try to get any attributes
4232  * along with any of the zero-copy operations READ, READDIR,
4233  * READLINK, WRITE.
4234  *
4235  * In the case of the first three, we want to put the GETATTR
4236  * after the read-type operation -- this is because it is hard
4237  * to predict the length of a GETATTR response in v4, and thus
4238  * align the READ data correctly.  This means that the GETATTR
4239  * may end up partially falling into the page cache, and we should
4240  * shift it into the 'tail' of the xdr_buf before processing.
4241  * To do this efficiently, we need to know the total length
4242  * of data received, which doesn't seem to be available outside
4243  * of the RPC layer.
4244  *
4245  * In the case of WRITE, we also want to put the GETATTR after
4246  * the operation -- in this case because we want to make sure
4247  * we get the post-operation mtime and size.
4248  *
4249  * Both of these changes to the XDR layer would in fact be quite
4250  * minor, but I decided to leave them for a subsequent patch.
4251  */
4252 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4253 		unsigned int pgbase, unsigned int pglen)
4254 {
4255 	struct nfs4_readlink args = {
4256 		.fh       = NFS_FH(inode),
4257 		.pgbase	  = pgbase,
4258 		.pglen    = pglen,
4259 		.pages    = &page,
4260 	};
4261 	struct nfs4_readlink_res res;
4262 	struct rpc_message msg = {
4263 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4264 		.rpc_argp = &args,
4265 		.rpc_resp = &res,
4266 	};
4267 
4268 	return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4269 }
4270 
4271 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4272 		unsigned int pgbase, unsigned int pglen)
4273 {
4274 	struct nfs4_exception exception = { };
4275 	int err;
4276 	do {
4277 		err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4278 		trace_nfs4_readlink(inode, err);
4279 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
4280 				&exception);
4281 	} while (exception.retry);
4282 	return err;
4283 }
4284 
4285 /*
4286  * This is just for mknod.  open(O_CREAT) will always do ->open_context().
4287  */
4288 static int
4289 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4290 		 int flags)
4291 {
4292 	struct nfs_server *server = NFS_SERVER(dir);
4293 	struct nfs4_label l, *ilabel = NULL;
4294 	struct nfs_open_context *ctx;
4295 	struct nfs4_state *state;
4296 	int status = 0;
4297 
4298 	ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4299 	if (IS_ERR(ctx))
4300 		return PTR_ERR(ctx);
4301 
4302 	ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4303 
4304 	if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4305 		sattr->ia_mode &= ~current_umask();
4306 	state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4307 	if (IS_ERR(state)) {
4308 		status = PTR_ERR(state);
4309 		goto out;
4310 	}
4311 out:
4312 	nfs4_label_release_security(ilabel);
4313 	put_nfs_open_context(ctx);
4314 	return status;
4315 }
4316 
4317 static int
4318 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4319 {
4320 	struct nfs_server *server = NFS_SERVER(dir);
4321 	struct nfs_removeargs args = {
4322 		.fh = NFS_FH(dir),
4323 		.name = *name,
4324 	};
4325 	struct nfs_removeres res = {
4326 		.server = server,
4327 	};
4328 	struct rpc_message msg = {
4329 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4330 		.rpc_argp = &args,
4331 		.rpc_resp = &res,
4332 	};
4333 	unsigned long timestamp = jiffies;
4334 	int status;
4335 
4336 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4337 	if (status == 0) {
4338 		spin_lock(&dir->i_lock);
4339 		update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
4340 		/* Removing a directory decrements nlink in the parent */
4341 		if (ftype == NF4DIR && dir->i_nlink > 2)
4342 			nfs4_dec_nlink_locked(dir);
4343 		spin_unlock(&dir->i_lock);
4344 	}
4345 	return status;
4346 }
4347 
4348 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4349 {
4350 	struct nfs4_exception exception = { };
4351 	struct inode *inode = d_inode(dentry);
4352 	int err;
4353 
4354 	if (inode) {
4355 		if (inode->i_nlink == 1)
4356 			nfs4_inode_return_delegation(inode);
4357 		else
4358 			nfs4_inode_make_writeable(inode);
4359 	}
4360 	do {
4361 		err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4362 		trace_nfs4_remove(dir, &dentry->d_name, err);
4363 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
4364 				&exception);
4365 	} while (exception.retry);
4366 	return err;
4367 }
4368 
4369 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4370 {
4371 	struct nfs4_exception exception = { };
4372 	int err;
4373 
4374 	do {
4375 		err = _nfs4_proc_remove(dir, name, NF4DIR);
4376 		trace_nfs4_remove(dir, name, err);
4377 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
4378 				&exception);
4379 	} while (exception.retry);
4380 	return err;
4381 }
4382 
4383 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4384 		struct dentry *dentry,
4385 		struct inode *inode)
4386 {
4387 	struct nfs_removeargs *args = msg->rpc_argp;
4388 	struct nfs_removeres *res = msg->rpc_resp;
4389 
4390 	res->server = NFS_SB(dentry->d_sb);
4391 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4392 	nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4393 
4394 	nfs_fattr_init(res->dir_attr);
4395 
4396 	if (inode)
4397 		nfs4_inode_return_delegation(inode);
4398 }
4399 
4400 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4401 {
4402 	nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4403 			&data->args.seq_args,
4404 			&data->res.seq_res,
4405 			task);
4406 }
4407 
4408 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4409 {
4410 	struct nfs_unlinkdata *data = task->tk_calldata;
4411 	struct nfs_removeres *res = &data->res;
4412 
4413 	if (!nfs4_sequence_done(task, &res->seq_res))
4414 		return 0;
4415 	if (nfs4_async_handle_error(task, res->server, NULL,
4416 				    &data->timeout) == -EAGAIN)
4417 		return 0;
4418 	if (task->tk_status == 0)
4419 		update_changeattr(dir, &res->cinfo,
4420 				res->dir_attr->time_start, 0);
4421 	return 1;
4422 }
4423 
4424 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4425 		struct dentry *old_dentry,
4426 		struct dentry *new_dentry)
4427 {
4428 	struct nfs_renameargs *arg = msg->rpc_argp;
4429 	struct nfs_renameres *res = msg->rpc_resp;
4430 	struct inode *old_inode = d_inode(old_dentry);
4431 	struct inode *new_inode = d_inode(new_dentry);
4432 
4433 	if (old_inode)
4434 		nfs4_inode_make_writeable(old_inode);
4435 	if (new_inode)
4436 		nfs4_inode_return_delegation(new_inode);
4437 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4438 	res->server = NFS_SB(old_dentry->d_sb);
4439 	nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4440 }
4441 
4442 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4443 {
4444 	nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4445 			&data->args.seq_args,
4446 			&data->res.seq_res,
4447 			task);
4448 }
4449 
4450 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4451 				 struct inode *new_dir)
4452 {
4453 	struct nfs_renamedata *data = task->tk_calldata;
4454 	struct nfs_renameres *res = &data->res;
4455 
4456 	if (!nfs4_sequence_done(task, &res->seq_res))
4457 		return 0;
4458 	if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4459 		return 0;
4460 
4461 	if (task->tk_status == 0) {
4462 		if (new_dir != old_dir) {
4463 			/* Note: If we moved a directory, nlink will change */
4464 			update_changeattr(old_dir, &res->old_cinfo,
4465 					res->old_fattr->time_start,
4466 					NFS_INO_INVALID_OTHER);
4467 			update_changeattr(new_dir, &res->new_cinfo,
4468 					res->new_fattr->time_start,
4469 					NFS_INO_INVALID_OTHER);
4470 		} else
4471 			update_changeattr(old_dir, &res->old_cinfo,
4472 					res->old_fattr->time_start,
4473 					0);
4474 	}
4475 	return 1;
4476 }
4477 
4478 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4479 {
4480 	struct nfs_server *server = NFS_SERVER(inode);
4481 	__u32 bitmask[NFS4_BITMASK_SZ];
4482 	struct nfs4_link_arg arg = {
4483 		.fh     = NFS_FH(inode),
4484 		.dir_fh = NFS_FH(dir),
4485 		.name   = name,
4486 		.bitmask = bitmask,
4487 	};
4488 	struct nfs4_link_res res = {
4489 		.server = server,
4490 		.label = NULL,
4491 	};
4492 	struct rpc_message msg = {
4493 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4494 		.rpc_argp = &arg,
4495 		.rpc_resp = &res,
4496 	};
4497 	int status = -ENOMEM;
4498 
4499 	res.fattr = nfs_alloc_fattr();
4500 	if (res.fattr == NULL)
4501 		goto out;
4502 
4503 	res.label = nfs4_label_alloc(server, GFP_KERNEL);
4504 	if (IS_ERR(res.label)) {
4505 		status = PTR_ERR(res.label);
4506 		goto out;
4507 	}
4508 
4509 	nfs4_inode_make_writeable(inode);
4510 	nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
4511 
4512 	status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4513 	if (!status) {
4514 		update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
4515 		status = nfs_post_op_update_inode(inode, res.fattr);
4516 		if (!status)
4517 			nfs_setsecurity(inode, res.fattr, res.label);
4518 	}
4519 
4520 
4521 	nfs4_label_free(res.label);
4522 
4523 out:
4524 	nfs_free_fattr(res.fattr);
4525 	return status;
4526 }
4527 
4528 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4529 {
4530 	struct nfs4_exception exception = { };
4531 	int err;
4532 	do {
4533 		err = nfs4_handle_exception(NFS_SERVER(inode),
4534 				_nfs4_proc_link(inode, dir, name),
4535 				&exception);
4536 	} while (exception.retry);
4537 	return err;
4538 }
4539 
4540 struct nfs4_createdata {
4541 	struct rpc_message msg;
4542 	struct nfs4_create_arg arg;
4543 	struct nfs4_create_res res;
4544 	struct nfs_fh fh;
4545 	struct nfs_fattr fattr;
4546 	struct nfs4_label *label;
4547 };
4548 
4549 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4550 		const struct qstr *name, struct iattr *sattr, u32 ftype)
4551 {
4552 	struct nfs4_createdata *data;
4553 
4554 	data = kzalloc(sizeof(*data), GFP_KERNEL);
4555 	if (data != NULL) {
4556 		struct nfs_server *server = NFS_SERVER(dir);
4557 
4558 		data->label = nfs4_label_alloc(server, GFP_KERNEL);
4559 		if (IS_ERR(data->label))
4560 			goto out_free;
4561 
4562 		data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4563 		data->msg.rpc_argp = &data->arg;
4564 		data->msg.rpc_resp = &data->res;
4565 		data->arg.dir_fh = NFS_FH(dir);
4566 		data->arg.server = server;
4567 		data->arg.name = name;
4568 		data->arg.attrs = sattr;
4569 		data->arg.ftype = ftype;
4570 		data->arg.bitmask = nfs4_bitmask(server, data->label);
4571 		data->arg.umask = current_umask();
4572 		data->res.server = server;
4573 		data->res.fh = &data->fh;
4574 		data->res.fattr = &data->fattr;
4575 		data->res.label = data->label;
4576 		nfs_fattr_init(data->res.fattr);
4577 	}
4578 	return data;
4579 out_free:
4580 	kfree(data);
4581 	return NULL;
4582 }
4583 
4584 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4585 {
4586 	int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4587 				    &data->arg.seq_args, &data->res.seq_res, 1);
4588 	if (status == 0) {
4589 		spin_lock(&dir->i_lock);
4590 		update_changeattr_locked(dir, &data->res.dir_cinfo,
4591 				data->res.fattr->time_start, 0);
4592 		/* Creating a directory bumps nlink in the parent */
4593 		if (data->arg.ftype == NF4DIR)
4594 			nfs4_inc_nlink_locked(dir);
4595 		spin_unlock(&dir->i_lock);
4596 		status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4597 	}
4598 	return status;
4599 }
4600 
4601 static void nfs4_free_createdata(struct nfs4_createdata *data)
4602 {
4603 	nfs4_label_free(data->label);
4604 	kfree(data);
4605 }
4606 
4607 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4608 		struct page *page, unsigned int len, struct iattr *sattr,
4609 		struct nfs4_label *label)
4610 {
4611 	struct nfs4_createdata *data;
4612 	int status = -ENAMETOOLONG;
4613 
4614 	if (len > NFS4_MAXPATHLEN)
4615 		goto out;
4616 
4617 	status = -ENOMEM;
4618 	data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4619 	if (data == NULL)
4620 		goto out;
4621 
4622 	data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4623 	data->arg.u.symlink.pages = &page;
4624 	data->arg.u.symlink.len = len;
4625 	data->arg.label = label;
4626 
4627 	status = nfs4_do_create(dir, dentry, data);
4628 
4629 	nfs4_free_createdata(data);
4630 out:
4631 	return status;
4632 }
4633 
4634 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4635 		struct page *page, unsigned int len, struct iattr *sattr)
4636 {
4637 	struct nfs4_exception exception = { };
4638 	struct nfs4_label l, *label = NULL;
4639 	int err;
4640 
4641 	label = nfs4_label_init_security(dir, dentry, sattr, &l);
4642 
4643 	do {
4644 		err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4645 		trace_nfs4_symlink(dir, &dentry->d_name, err);
4646 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
4647 				&exception);
4648 	} while (exception.retry);
4649 
4650 	nfs4_label_release_security(label);
4651 	return err;
4652 }
4653 
4654 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4655 		struct iattr *sattr, struct nfs4_label *label)
4656 {
4657 	struct nfs4_createdata *data;
4658 	int status = -ENOMEM;
4659 
4660 	data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4661 	if (data == NULL)
4662 		goto out;
4663 
4664 	data->arg.label = label;
4665 	status = nfs4_do_create(dir, dentry, data);
4666 
4667 	nfs4_free_createdata(data);
4668 out:
4669 	return status;
4670 }
4671 
4672 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4673 		struct iattr *sattr)
4674 {
4675 	struct nfs_server *server = NFS_SERVER(dir);
4676 	struct nfs4_exception exception = { };
4677 	struct nfs4_label l, *label = NULL;
4678 	int err;
4679 
4680 	label = nfs4_label_init_security(dir, dentry, sattr, &l);
4681 
4682 	if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4683 		sattr->ia_mode &= ~current_umask();
4684 	do {
4685 		err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4686 		trace_nfs4_mkdir(dir, &dentry->d_name, err);
4687 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
4688 				&exception);
4689 	} while (exception.retry);
4690 	nfs4_label_release_security(label);
4691 
4692 	return err;
4693 }
4694 
4695 static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4696 		u64 cookie, struct page **pages, unsigned int count, bool plus)
4697 {
4698 	struct inode		*dir = d_inode(dentry);
4699 	struct nfs4_readdir_arg args = {
4700 		.fh = NFS_FH(dir),
4701 		.pages = pages,
4702 		.pgbase = 0,
4703 		.count = count,
4704 		.bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4705 		.plus = plus,
4706 	};
4707 	struct nfs4_readdir_res res;
4708 	struct rpc_message msg = {
4709 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4710 		.rpc_argp = &args,
4711 		.rpc_resp = &res,
4712 		.rpc_cred = cred,
4713 	};
4714 	int			status;
4715 
4716 	dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4717 			dentry,
4718 			(unsigned long long)cookie);
4719 	nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4720 	res.pgbase = args.pgbase;
4721 	status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4722 	if (status >= 0) {
4723 		memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4724 		status += args.pgbase;
4725 	}
4726 
4727 	nfs_invalidate_atime(dir);
4728 
4729 	dprintk("%s: returns %d\n", __func__, status);
4730 	return status;
4731 }
4732 
4733 static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4734 		u64 cookie, struct page **pages, unsigned int count, bool plus)
4735 {
4736 	struct nfs4_exception exception = { };
4737 	int err;
4738 	do {
4739 		err = _nfs4_proc_readdir(dentry, cred, cookie,
4740 				pages, count, plus);
4741 		trace_nfs4_readdir(d_inode(dentry), err);
4742 		err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4743 				&exception);
4744 	} while (exception.retry);
4745 	return err;
4746 }
4747 
4748 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4749 		struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4750 {
4751 	struct nfs4_createdata *data;
4752 	int mode = sattr->ia_mode;
4753 	int status = -ENOMEM;
4754 
4755 	data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4756 	if (data == NULL)
4757 		goto out;
4758 
4759 	if (S_ISFIFO(mode))
4760 		data->arg.ftype = NF4FIFO;
4761 	else if (S_ISBLK(mode)) {
4762 		data->arg.ftype = NF4BLK;
4763 		data->arg.u.device.specdata1 = MAJOR(rdev);
4764 		data->arg.u.device.specdata2 = MINOR(rdev);
4765 	}
4766 	else if (S_ISCHR(mode)) {
4767 		data->arg.ftype = NF4CHR;
4768 		data->arg.u.device.specdata1 = MAJOR(rdev);
4769 		data->arg.u.device.specdata2 = MINOR(rdev);
4770 	} else if (!S_ISSOCK(mode)) {
4771 		status = -EINVAL;
4772 		goto out_free;
4773 	}
4774 
4775 	data->arg.label = label;
4776 	status = nfs4_do_create(dir, dentry, data);
4777 out_free:
4778 	nfs4_free_createdata(data);
4779 out:
4780 	return status;
4781 }
4782 
4783 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4784 		struct iattr *sattr, dev_t rdev)
4785 {
4786 	struct nfs_server *server = NFS_SERVER(dir);
4787 	struct nfs4_exception exception = { };
4788 	struct nfs4_label l, *label = NULL;
4789 	int err;
4790 
4791 	label = nfs4_label_init_security(dir, dentry, sattr, &l);
4792 
4793 	if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4794 		sattr->ia_mode &= ~current_umask();
4795 	do {
4796 		err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4797 		trace_nfs4_mknod(dir, &dentry->d_name, err);
4798 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
4799 				&exception);
4800 	} while (exception.retry);
4801 
4802 	nfs4_label_release_security(label);
4803 
4804 	return err;
4805 }
4806 
4807 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4808 		 struct nfs_fsstat *fsstat)
4809 {
4810 	struct nfs4_statfs_arg args = {
4811 		.fh = fhandle,
4812 		.bitmask = server->attr_bitmask,
4813 	};
4814 	struct nfs4_statfs_res res = {
4815 		.fsstat = fsstat,
4816 	};
4817 	struct rpc_message msg = {
4818 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4819 		.rpc_argp = &args,
4820 		.rpc_resp = &res,
4821 	};
4822 
4823 	nfs_fattr_init(fsstat->fattr);
4824 	return  nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4825 }
4826 
4827 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4828 {
4829 	struct nfs4_exception exception = { };
4830 	int err;
4831 	do {
4832 		err = nfs4_handle_exception(server,
4833 				_nfs4_proc_statfs(server, fhandle, fsstat),
4834 				&exception);
4835 	} while (exception.retry);
4836 	return err;
4837 }
4838 
4839 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4840 		struct nfs_fsinfo *fsinfo)
4841 {
4842 	struct nfs4_fsinfo_arg args = {
4843 		.fh = fhandle,
4844 		.bitmask = server->attr_bitmask,
4845 	};
4846 	struct nfs4_fsinfo_res res = {
4847 		.fsinfo = fsinfo,
4848 	};
4849 	struct rpc_message msg = {
4850 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4851 		.rpc_argp = &args,
4852 		.rpc_resp = &res,
4853 	};
4854 
4855 	return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4856 }
4857 
4858 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4859 {
4860 	struct nfs4_exception exception = { };
4861 	unsigned long now = jiffies;
4862 	int err;
4863 
4864 	do {
4865 		err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4866 		trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4867 		if (err == 0) {
4868 			nfs4_set_lease_period(server->nfs_client,
4869 					fsinfo->lease_time * HZ,
4870 					now);
4871 			break;
4872 		}
4873 		err = nfs4_handle_exception(server, err, &exception);
4874 	} while (exception.retry);
4875 	return err;
4876 }
4877 
4878 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4879 {
4880 	int error;
4881 
4882 	nfs_fattr_init(fsinfo->fattr);
4883 	error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4884 	if (error == 0) {
4885 		/* block layout checks this! */
4886 		server->pnfs_blksize = fsinfo->blksize;
4887 		set_pnfs_layoutdriver(server, fhandle, fsinfo);
4888 	}
4889 
4890 	return error;
4891 }
4892 
4893 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4894 		struct nfs_pathconf *pathconf)
4895 {
4896 	struct nfs4_pathconf_arg args = {
4897 		.fh = fhandle,
4898 		.bitmask = server->attr_bitmask,
4899 	};
4900 	struct nfs4_pathconf_res res = {
4901 		.pathconf = pathconf,
4902 	};
4903 	struct rpc_message msg = {
4904 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4905 		.rpc_argp = &args,
4906 		.rpc_resp = &res,
4907 	};
4908 
4909 	/* None of the pathconf attributes are mandatory to implement */
4910 	if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4911 		memset(pathconf, 0, sizeof(*pathconf));
4912 		return 0;
4913 	}
4914 
4915 	nfs_fattr_init(pathconf->fattr);
4916 	return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4917 }
4918 
4919 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4920 		struct nfs_pathconf *pathconf)
4921 {
4922 	struct nfs4_exception exception = { };
4923 	int err;
4924 
4925 	do {
4926 		err = nfs4_handle_exception(server,
4927 				_nfs4_proc_pathconf(server, fhandle, pathconf),
4928 				&exception);
4929 	} while (exception.retry);
4930 	return err;
4931 }
4932 
4933 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4934 		const struct nfs_open_context *ctx,
4935 		const struct nfs_lock_context *l_ctx,
4936 		fmode_t fmode)
4937 {
4938 	return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
4939 }
4940 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4941 
4942 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4943 		const struct nfs_open_context *ctx,
4944 		const struct nfs_lock_context *l_ctx,
4945 		fmode_t fmode)
4946 {
4947 	nfs4_stateid current_stateid;
4948 
4949 	/* If the current stateid represents a lost lock, then exit */
4950 	if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4951 		return true;
4952 	return nfs4_stateid_match(stateid, &current_stateid);
4953 }
4954 
4955 static bool nfs4_error_stateid_expired(int err)
4956 {
4957 	switch (err) {
4958 	case -NFS4ERR_DELEG_REVOKED:
4959 	case -NFS4ERR_ADMIN_REVOKED:
4960 	case -NFS4ERR_BAD_STATEID:
4961 	case -NFS4ERR_STALE_STATEID:
4962 	case -NFS4ERR_OLD_STATEID:
4963 	case -NFS4ERR_OPENMODE:
4964 	case -NFS4ERR_EXPIRED:
4965 		return true;
4966 	}
4967 	return false;
4968 }
4969 
4970 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4971 {
4972 	struct nfs_server *server = NFS_SERVER(hdr->inode);
4973 
4974 	trace_nfs4_read(hdr, task->tk_status);
4975 	if (task->tk_status < 0) {
4976 		struct nfs4_exception exception = {
4977 			.inode = hdr->inode,
4978 			.state = hdr->args.context->state,
4979 			.stateid = &hdr->args.stateid,
4980 		};
4981 		task->tk_status = nfs4_async_handle_exception(task,
4982 				server, task->tk_status, &exception);
4983 		if (exception.retry) {
4984 			rpc_restart_call_prepare(task);
4985 			return -EAGAIN;
4986 		}
4987 	}
4988 
4989 	if (task->tk_status > 0)
4990 		renew_lease(server, hdr->timestamp);
4991 	return 0;
4992 }
4993 
4994 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4995 		struct nfs_pgio_args *args)
4996 {
4997 
4998 	if (!nfs4_error_stateid_expired(task->tk_status) ||
4999 		nfs4_stateid_is_current(&args->stateid,
5000 				args->context,
5001 				args->lock_context,
5002 				FMODE_READ))
5003 		return false;
5004 	rpc_restart_call_prepare(task);
5005 	return true;
5006 }
5007 
5008 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5009 {
5010 
5011 	dprintk("--> %s\n", __func__);
5012 
5013 	if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5014 		return -EAGAIN;
5015 	if (nfs4_read_stateid_changed(task, &hdr->args))
5016 		return -EAGAIN;
5017 	if (task->tk_status > 0)
5018 		nfs_invalidate_atime(hdr->inode);
5019 	return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5020 				    nfs4_read_done_cb(task, hdr);
5021 }
5022 
5023 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5024 				 struct rpc_message *msg)
5025 {
5026 	hdr->timestamp   = jiffies;
5027 	if (!hdr->pgio_done_cb)
5028 		hdr->pgio_done_cb = nfs4_read_done_cb;
5029 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5030 	nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5031 }
5032 
5033 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5034 				      struct nfs_pgio_header *hdr)
5035 {
5036 	if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5037 			&hdr->args.seq_args,
5038 			&hdr->res.seq_res,
5039 			task))
5040 		return 0;
5041 	if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5042 				hdr->args.lock_context,
5043 				hdr->rw_mode) == -EIO)
5044 		return -EIO;
5045 	if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5046 		return -EIO;
5047 	return 0;
5048 }
5049 
5050 static int nfs4_write_done_cb(struct rpc_task *task,
5051 			      struct nfs_pgio_header *hdr)
5052 {
5053 	struct inode *inode = hdr->inode;
5054 
5055 	trace_nfs4_write(hdr, task->tk_status);
5056 	if (task->tk_status < 0) {
5057 		struct nfs4_exception exception = {
5058 			.inode = hdr->inode,
5059 			.state = hdr->args.context->state,
5060 			.stateid = &hdr->args.stateid,
5061 		};
5062 		task->tk_status = nfs4_async_handle_exception(task,
5063 				NFS_SERVER(inode), task->tk_status,
5064 				&exception);
5065 		if (exception.retry) {
5066 			rpc_restart_call_prepare(task);
5067 			return -EAGAIN;
5068 		}
5069 	}
5070 	if (task->tk_status >= 0) {
5071 		renew_lease(NFS_SERVER(inode), hdr->timestamp);
5072 		nfs_writeback_update_inode(hdr);
5073 	}
5074 	return 0;
5075 }
5076 
5077 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5078 		struct nfs_pgio_args *args)
5079 {
5080 
5081 	if (!nfs4_error_stateid_expired(task->tk_status) ||
5082 		nfs4_stateid_is_current(&args->stateid,
5083 				args->context,
5084 				args->lock_context,
5085 				FMODE_WRITE))
5086 		return false;
5087 	rpc_restart_call_prepare(task);
5088 	return true;
5089 }
5090 
5091 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5092 {
5093 	if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5094 		return -EAGAIN;
5095 	if (nfs4_write_stateid_changed(task, &hdr->args))
5096 		return -EAGAIN;
5097 	return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5098 		nfs4_write_done_cb(task, hdr);
5099 }
5100 
5101 static
5102 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5103 {
5104 	/* Don't request attributes for pNFS or O_DIRECT writes */
5105 	if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5106 		return false;
5107 	/* Otherwise, request attributes if and only if we don't hold
5108 	 * a delegation
5109 	 */
5110 	return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5111 }
5112 
5113 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5114 				  struct rpc_message *msg,
5115 				  struct rpc_clnt **clnt)
5116 {
5117 	struct nfs_server *server = NFS_SERVER(hdr->inode);
5118 
5119 	if (!nfs4_write_need_cache_consistency_data(hdr)) {
5120 		hdr->args.bitmask = NULL;
5121 		hdr->res.fattr = NULL;
5122 	} else
5123 		hdr->args.bitmask = server->cache_consistency_bitmask;
5124 
5125 	if (!hdr->pgio_done_cb)
5126 		hdr->pgio_done_cb = nfs4_write_done_cb;
5127 	hdr->res.server = server;
5128 	hdr->timestamp   = jiffies;
5129 
5130 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5131 	nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
5132 	nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5133 }
5134 
5135 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5136 {
5137 	nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5138 			&data->args.seq_args,
5139 			&data->res.seq_res,
5140 			task);
5141 }
5142 
5143 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5144 {
5145 	struct inode *inode = data->inode;
5146 
5147 	trace_nfs4_commit(data, task->tk_status);
5148 	if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5149 				    NULL, NULL) == -EAGAIN) {
5150 		rpc_restart_call_prepare(task);
5151 		return -EAGAIN;
5152 	}
5153 	return 0;
5154 }
5155 
5156 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5157 {
5158 	if (!nfs4_sequence_done(task, &data->res.seq_res))
5159 		return -EAGAIN;
5160 	return data->commit_done_cb(task, data);
5161 }
5162 
5163 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5164 				   struct rpc_clnt **clnt)
5165 {
5166 	struct nfs_server *server = NFS_SERVER(data->inode);
5167 
5168 	if (data->commit_done_cb == NULL)
5169 		data->commit_done_cb = nfs4_commit_done_cb;
5170 	data->res.server = server;
5171 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5172 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5173 	nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5174 }
5175 
5176 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5177 				struct nfs_commitres *res)
5178 {
5179 	struct inode *dst_inode = file_inode(dst);
5180 	struct nfs_server *server = NFS_SERVER(dst_inode);
5181 	struct rpc_message msg = {
5182 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5183 		.rpc_argp = args,
5184 		.rpc_resp = res,
5185 	};
5186 
5187 	args->fh = NFS_FH(dst_inode);
5188 	return nfs4_call_sync(server->client, server, &msg,
5189 			&args->seq_args, &res->seq_res, 1);
5190 }
5191 
5192 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5193 {
5194 	struct nfs_commitargs args = {
5195 		.offset = offset,
5196 		.count = count,
5197 	};
5198 	struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5199 	struct nfs4_exception exception = { };
5200 	int status;
5201 
5202 	do {
5203 		status = _nfs4_proc_commit(dst, &args, res);
5204 		status = nfs4_handle_exception(dst_server, status, &exception);
5205 	} while (exception.retry);
5206 
5207 	return status;
5208 }
5209 
5210 struct nfs4_renewdata {
5211 	struct nfs_client	*client;
5212 	unsigned long		timestamp;
5213 };
5214 
5215 /*
5216  * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5217  * standalone procedure for queueing an asynchronous RENEW.
5218  */
5219 static void nfs4_renew_release(void *calldata)
5220 {
5221 	struct nfs4_renewdata *data = calldata;
5222 	struct nfs_client *clp = data->client;
5223 
5224 	if (refcount_read(&clp->cl_count) > 1)
5225 		nfs4_schedule_state_renewal(clp);
5226 	nfs_put_client(clp);
5227 	kfree(data);
5228 }
5229 
5230 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5231 {
5232 	struct nfs4_renewdata *data = calldata;
5233 	struct nfs_client *clp = data->client;
5234 	unsigned long timestamp = data->timestamp;
5235 
5236 	trace_nfs4_renew_async(clp, task->tk_status);
5237 	switch (task->tk_status) {
5238 	case 0:
5239 		break;
5240 	case -NFS4ERR_LEASE_MOVED:
5241 		nfs4_schedule_lease_moved_recovery(clp);
5242 		break;
5243 	default:
5244 		/* Unless we're shutting down, schedule state recovery! */
5245 		if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5246 			return;
5247 		if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5248 			nfs4_schedule_lease_recovery(clp);
5249 			return;
5250 		}
5251 		nfs4_schedule_path_down_recovery(clp);
5252 	}
5253 	do_renew_lease(clp, timestamp);
5254 }
5255 
5256 static const struct rpc_call_ops nfs4_renew_ops = {
5257 	.rpc_call_done = nfs4_renew_done,
5258 	.rpc_release = nfs4_renew_release,
5259 };
5260 
5261 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5262 {
5263 	struct rpc_message msg = {
5264 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5265 		.rpc_argp	= clp,
5266 		.rpc_cred	= cred,
5267 	};
5268 	struct nfs4_renewdata *data;
5269 
5270 	if (renew_flags == 0)
5271 		return 0;
5272 	if (!refcount_inc_not_zero(&clp->cl_count))
5273 		return -EIO;
5274 	data = kmalloc(sizeof(*data), GFP_NOFS);
5275 	if (data == NULL) {
5276 		nfs_put_client(clp);
5277 		return -ENOMEM;
5278 	}
5279 	data->client = clp;
5280 	data->timestamp = jiffies;
5281 	return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5282 			&nfs4_renew_ops, data);
5283 }
5284 
5285 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5286 {
5287 	struct rpc_message msg = {
5288 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5289 		.rpc_argp	= clp,
5290 		.rpc_cred	= cred,
5291 	};
5292 	unsigned long now = jiffies;
5293 	int status;
5294 
5295 	status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5296 	if (status < 0)
5297 		return status;
5298 	do_renew_lease(clp, now);
5299 	return 0;
5300 }
5301 
5302 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5303 {
5304 	return server->caps & NFS_CAP_ACLS;
5305 }
5306 
5307 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5308  * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5309  * the stack.
5310  */
5311 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5312 
5313 static int buf_to_pages_noslab(const void *buf, size_t buflen,
5314 		struct page **pages)
5315 {
5316 	struct page *newpage, **spages;
5317 	int rc = 0;
5318 	size_t len;
5319 	spages = pages;
5320 
5321 	do {
5322 		len = min_t(size_t, PAGE_SIZE, buflen);
5323 		newpage = alloc_page(GFP_KERNEL);
5324 
5325 		if (newpage == NULL)
5326 			goto unwind;
5327 		memcpy(page_address(newpage), buf, len);
5328 		buf += len;
5329 		buflen -= len;
5330 		*pages++ = newpage;
5331 		rc++;
5332 	} while (buflen != 0);
5333 
5334 	return rc;
5335 
5336 unwind:
5337 	for(; rc > 0; rc--)
5338 		__free_page(spages[rc-1]);
5339 	return -ENOMEM;
5340 }
5341 
5342 struct nfs4_cached_acl {
5343 	int cached;
5344 	size_t len;
5345 	char data[0];
5346 };
5347 
5348 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5349 {
5350 	struct nfs_inode *nfsi = NFS_I(inode);
5351 
5352 	spin_lock(&inode->i_lock);
5353 	kfree(nfsi->nfs4_acl);
5354 	nfsi->nfs4_acl = acl;
5355 	spin_unlock(&inode->i_lock);
5356 }
5357 
5358 static void nfs4_zap_acl_attr(struct inode *inode)
5359 {
5360 	nfs4_set_cached_acl(inode, NULL);
5361 }
5362 
5363 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5364 {
5365 	struct nfs_inode *nfsi = NFS_I(inode);
5366 	struct nfs4_cached_acl *acl;
5367 	int ret = -ENOENT;
5368 
5369 	spin_lock(&inode->i_lock);
5370 	acl = nfsi->nfs4_acl;
5371 	if (acl == NULL)
5372 		goto out;
5373 	if (buf == NULL) /* user is just asking for length */
5374 		goto out_len;
5375 	if (acl->cached == 0)
5376 		goto out;
5377 	ret = -ERANGE; /* see getxattr(2) man page */
5378 	if (acl->len > buflen)
5379 		goto out;
5380 	memcpy(buf, acl->data, acl->len);
5381 out_len:
5382 	ret = acl->len;
5383 out:
5384 	spin_unlock(&inode->i_lock);
5385 	return ret;
5386 }
5387 
5388 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5389 {
5390 	struct nfs4_cached_acl *acl;
5391 	size_t buflen = sizeof(*acl) + acl_len;
5392 
5393 	if (buflen <= PAGE_SIZE) {
5394 		acl = kmalloc(buflen, GFP_KERNEL);
5395 		if (acl == NULL)
5396 			goto out;
5397 		acl->cached = 1;
5398 		_copy_from_pages(acl->data, pages, pgbase, acl_len);
5399 	} else {
5400 		acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5401 		if (acl == NULL)
5402 			goto out;
5403 		acl->cached = 0;
5404 	}
5405 	acl->len = acl_len;
5406 out:
5407 	nfs4_set_cached_acl(inode, acl);
5408 }
5409 
5410 /*
5411  * The getxattr API returns the required buffer length when called with a
5412  * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5413  * the required buf.  On a NULL buf, we send a page of data to the server
5414  * guessing that the ACL request can be serviced by a page. If so, we cache
5415  * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5416  * the cache. If not so, we throw away the page, and cache the required
5417  * length. The next getxattr call will then produce another round trip to
5418  * the server, this time with the input buf of the required size.
5419  */
5420 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5421 {
5422 	struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
5423 	struct nfs_getaclargs args = {
5424 		.fh = NFS_FH(inode),
5425 		.acl_pages = pages,
5426 		.acl_len = buflen,
5427 	};
5428 	struct nfs_getaclres res = {
5429 		.acl_len = buflen,
5430 	};
5431 	struct rpc_message msg = {
5432 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5433 		.rpc_argp = &args,
5434 		.rpc_resp = &res,
5435 	};
5436 	unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5437 	int ret = -ENOMEM, i;
5438 
5439 	if (npages > ARRAY_SIZE(pages))
5440 		return -ERANGE;
5441 
5442 	for (i = 0; i < npages; i++) {
5443 		pages[i] = alloc_page(GFP_KERNEL);
5444 		if (!pages[i])
5445 			goto out_free;
5446 	}
5447 
5448 	/* for decoding across pages */
5449 	res.acl_scratch = alloc_page(GFP_KERNEL);
5450 	if (!res.acl_scratch)
5451 		goto out_free;
5452 
5453 	args.acl_len = npages * PAGE_SIZE;
5454 
5455 	dprintk("%s  buf %p buflen %zu npages %d args.acl_len %zu\n",
5456 		__func__, buf, buflen, npages, args.acl_len);
5457 	ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5458 			     &msg, &args.seq_args, &res.seq_res, 0);
5459 	if (ret)
5460 		goto out_free;
5461 
5462 	/* Handle the case where the passed-in buffer is too short */
5463 	if (res.acl_flags & NFS4_ACL_TRUNC) {
5464 		/* Did the user only issue a request for the acl length? */
5465 		if (buf == NULL)
5466 			goto out_ok;
5467 		ret = -ERANGE;
5468 		goto out_free;
5469 	}
5470 	nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5471 	if (buf) {
5472 		if (res.acl_len > buflen) {
5473 			ret = -ERANGE;
5474 			goto out_free;
5475 		}
5476 		_copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5477 	}
5478 out_ok:
5479 	ret = res.acl_len;
5480 out_free:
5481 	for (i = 0; i < npages; i++)
5482 		if (pages[i])
5483 			__free_page(pages[i]);
5484 	if (res.acl_scratch)
5485 		__free_page(res.acl_scratch);
5486 	return ret;
5487 }
5488 
5489 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5490 {
5491 	struct nfs4_exception exception = { };
5492 	ssize_t ret;
5493 	do {
5494 		ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5495 		trace_nfs4_get_acl(inode, ret);
5496 		if (ret >= 0)
5497 			break;
5498 		ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5499 	} while (exception.retry);
5500 	return ret;
5501 }
5502 
5503 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5504 {
5505 	struct nfs_server *server = NFS_SERVER(inode);
5506 	int ret;
5507 
5508 	if (!nfs4_server_supports_acls(server))
5509 		return -EOPNOTSUPP;
5510 	ret = nfs_revalidate_inode(server, inode);
5511 	if (ret < 0)
5512 		return ret;
5513 	if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5514 		nfs_zap_acl_cache(inode);
5515 	ret = nfs4_read_cached_acl(inode, buf, buflen);
5516 	if (ret != -ENOENT)
5517 		/* -ENOENT is returned if there is no ACL or if there is an ACL
5518 		 * but no cached acl data, just the acl length */
5519 		return ret;
5520 	return nfs4_get_acl_uncached(inode, buf, buflen);
5521 }
5522 
5523 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5524 {
5525 	struct nfs_server *server = NFS_SERVER(inode);
5526 	struct page *pages[NFS4ACL_MAXPAGES];
5527 	struct nfs_setaclargs arg = {
5528 		.fh		= NFS_FH(inode),
5529 		.acl_pages	= pages,
5530 		.acl_len	= buflen,
5531 	};
5532 	struct nfs_setaclres res;
5533 	struct rpc_message msg = {
5534 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5535 		.rpc_argp	= &arg,
5536 		.rpc_resp	= &res,
5537 	};
5538 	unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5539 	int ret, i;
5540 
5541 	if (!nfs4_server_supports_acls(server))
5542 		return -EOPNOTSUPP;
5543 	if (npages > ARRAY_SIZE(pages))
5544 		return -ERANGE;
5545 	i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5546 	if (i < 0)
5547 		return i;
5548 	nfs4_inode_make_writeable(inode);
5549 	ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5550 
5551 	/*
5552 	 * Free each page after tx, so the only ref left is
5553 	 * held by the network stack
5554 	 */
5555 	for (; i > 0; i--)
5556 		put_page(pages[i-1]);
5557 
5558 	/*
5559 	 * Acl update can result in inode attribute update.
5560 	 * so mark the attribute cache invalid.
5561 	 */
5562 	spin_lock(&inode->i_lock);
5563 	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5564 		| NFS_INO_INVALID_CTIME
5565 		| NFS_INO_REVAL_FORCED;
5566 	spin_unlock(&inode->i_lock);
5567 	nfs_access_zap_cache(inode);
5568 	nfs_zap_acl_cache(inode);
5569 	return ret;
5570 }
5571 
5572 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5573 {
5574 	struct nfs4_exception exception = { };
5575 	int err;
5576 	do {
5577 		err = __nfs4_proc_set_acl(inode, buf, buflen);
5578 		trace_nfs4_set_acl(inode, err);
5579 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
5580 				&exception);
5581 	} while (exception.retry);
5582 	return err;
5583 }
5584 
5585 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5586 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5587 					size_t buflen)
5588 {
5589 	struct nfs_server *server = NFS_SERVER(inode);
5590 	struct nfs_fattr fattr;
5591 	struct nfs4_label label = {0, 0, buflen, buf};
5592 
5593 	u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5594 	struct nfs4_getattr_arg arg = {
5595 		.fh		= NFS_FH(inode),
5596 		.bitmask	= bitmask,
5597 	};
5598 	struct nfs4_getattr_res res = {
5599 		.fattr		= &fattr,
5600 		.label		= &label,
5601 		.server		= server,
5602 	};
5603 	struct rpc_message msg = {
5604 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5605 		.rpc_argp	= &arg,
5606 		.rpc_resp	= &res,
5607 	};
5608 	int ret;
5609 
5610 	nfs_fattr_init(&fattr);
5611 
5612 	ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5613 	if (ret)
5614 		return ret;
5615 	if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5616 		return -ENOENT;
5617 	if (buflen < label.len)
5618 		return -ERANGE;
5619 	return 0;
5620 }
5621 
5622 static int nfs4_get_security_label(struct inode *inode, void *buf,
5623 					size_t buflen)
5624 {
5625 	struct nfs4_exception exception = { };
5626 	int err;
5627 
5628 	if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5629 		return -EOPNOTSUPP;
5630 
5631 	do {
5632 		err = _nfs4_get_security_label(inode, buf, buflen);
5633 		trace_nfs4_get_security_label(inode, err);
5634 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
5635 				&exception);
5636 	} while (exception.retry);
5637 	return err;
5638 }
5639 
5640 static int _nfs4_do_set_security_label(struct inode *inode,
5641 		struct nfs4_label *ilabel,
5642 		struct nfs_fattr *fattr,
5643 		struct nfs4_label *olabel)
5644 {
5645 
5646 	struct iattr sattr = {0};
5647 	struct nfs_server *server = NFS_SERVER(inode);
5648 	const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5649 	struct nfs_setattrargs arg = {
5650 		.fh		= NFS_FH(inode),
5651 		.iap		= &sattr,
5652 		.server		= server,
5653 		.bitmask	= bitmask,
5654 		.label		= ilabel,
5655 	};
5656 	struct nfs_setattrres res = {
5657 		.fattr		= fattr,
5658 		.label		= olabel,
5659 		.server		= server,
5660 	};
5661 	struct rpc_message msg = {
5662 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5663 		.rpc_argp	= &arg,
5664 		.rpc_resp	= &res,
5665 	};
5666 	int status;
5667 
5668 	nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5669 
5670 	status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5671 	if (status)
5672 		dprintk("%s failed: %d\n", __func__, status);
5673 
5674 	return status;
5675 }
5676 
5677 static int nfs4_do_set_security_label(struct inode *inode,
5678 		struct nfs4_label *ilabel,
5679 		struct nfs_fattr *fattr,
5680 		struct nfs4_label *olabel)
5681 {
5682 	struct nfs4_exception exception = { };
5683 	int err;
5684 
5685 	do {
5686 		err = _nfs4_do_set_security_label(inode, ilabel,
5687 				fattr, olabel);
5688 		trace_nfs4_set_security_label(inode, err);
5689 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
5690 				&exception);
5691 	} while (exception.retry);
5692 	return err;
5693 }
5694 
5695 static int
5696 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5697 {
5698 	struct nfs4_label ilabel, *olabel = NULL;
5699 	struct nfs_fattr fattr;
5700 	int status;
5701 
5702 	if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5703 		return -EOPNOTSUPP;
5704 
5705 	nfs_fattr_init(&fattr);
5706 
5707 	ilabel.pi = 0;
5708 	ilabel.lfs = 0;
5709 	ilabel.label = (char *)buf;
5710 	ilabel.len = buflen;
5711 
5712 	olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5713 	if (IS_ERR(olabel)) {
5714 		status = -PTR_ERR(olabel);
5715 		goto out;
5716 	}
5717 
5718 	status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5719 	if (status == 0)
5720 		nfs_setsecurity(inode, &fattr, olabel);
5721 
5722 	nfs4_label_free(olabel);
5723 out:
5724 	return status;
5725 }
5726 #endif	/* CONFIG_NFS_V4_SECURITY_LABEL */
5727 
5728 
5729 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5730 				    nfs4_verifier *bootverf)
5731 {
5732 	__be32 verf[2];
5733 
5734 	if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5735 		/* An impossible timestamp guarantees this value
5736 		 * will never match a generated boot time. */
5737 		verf[0] = cpu_to_be32(U32_MAX);
5738 		verf[1] = cpu_to_be32(U32_MAX);
5739 	} else {
5740 		struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5741 		u64 ns = ktime_to_ns(nn->boot_time);
5742 
5743 		verf[0] = cpu_to_be32(ns >> 32);
5744 		verf[1] = cpu_to_be32(ns);
5745 	}
5746 	memcpy(bootverf->data, verf, sizeof(bootverf->data));
5747 }
5748 
5749 static int
5750 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5751 {
5752 	size_t len;
5753 	char *str;
5754 
5755 	if (clp->cl_owner_id != NULL)
5756 		return 0;
5757 
5758 	rcu_read_lock();
5759 	len = 14 +
5760 		strlen(clp->cl_rpcclient->cl_nodename) +
5761 		1 +
5762 		strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5763 		1;
5764 	rcu_read_unlock();
5765 	if (nfs4_client_id_uniquifier[0] != '\0')
5766 		len += strlen(nfs4_client_id_uniquifier) + 1;
5767 	if (len > NFS4_OPAQUE_LIMIT + 1)
5768 		return -EINVAL;
5769 
5770 	/*
5771 	 * Since this string is allocated at mount time, and held until the
5772 	 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5773 	 * about a memory-reclaim deadlock.
5774 	 */
5775 	str = kmalloc(len, GFP_KERNEL);
5776 	if (!str)
5777 		return -ENOMEM;
5778 
5779 	rcu_read_lock();
5780 	if (nfs4_client_id_uniquifier[0] != '\0')
5781 		scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
5782 			  clp->cl_rpcclient->cl_nodename,
5783 			  nfs4_client_id_uniquifier,
5784 			  rpc_peeraddr2str(clp->cl_rpcclient,
5785 					   RPC_DISPLAY_ADDR));
5786 	else
5787 		scnprintf(str, len, "Linux NFSv4.0 %s/%s",
5788 			  clp->cl_rpcclient->cl_nodename,
5789 			  rpc_peeraddr2str(clp->cl_rpcclient,
5790 					   RPC_DISPLAY_ADDR));
5791 	rcu_read_unlock();
5792 
5793 	clp->cl_owner_id = str;
5794 	return 0;
5795 }
5796 
5797 static int
5798 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5799 {
5800 	size_t len;
5801 	char *str;
5802 
5803 	len = 10 + 10 + 1 + 10 + 1 +
5804 		strlen(nfs4_client_id_uniquifier) + 1 +
5805 		strlen(clp->cl_rpcclient->cl_nodename) + 1;
5806 
5807 	if (len > NFS4_OPAQUE_LIMIT + 1)
5808 		return -EINVAL;
5809 
5810 	/*
5811 	 * Since this string is allocated at mount time, and held until the
5812 	 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5813 	 * about a memory-reclaim deadlock.
5814 	 */
5815 	str = kmalloc(len, GFP_KERNEL);
5816 	if (!str)
5817 		return -ENOMEM;
5818 
5819 	scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5820 			clp->rpc_ops->version, clp->cl_minorversion,
5821 			nfs4_client_id_uniquifier,
5822 			clp->cl_rpcclient->cl_nodename);
5823 	clp->cl_owner_id = str;
5824 	return 0;
5825 }
5826 
5827 static int
5828 nfs4_init_uniform_client_string(struct nfs_client *clp)
5829 {
5830 	size_t len;
5831 	char *str;
5832 
5833 	if (clp->cl_owner_id != NULL)
5834 		return 0;
5835 
5836 	if (nfs4_client_id_uniquifier[0] != '\0')
5837 		return nfs4_init_uniquifier_client_string(clp);
5838 
5839 	len = 10 + 10 + 1 + 10 + 1 +
5840 		strlen(clp->cl_rpcclient->cl_nodename) + 1;
5841 
5842 	if (len > NFS4_OPAQUE_LIMIT + 1)
5843 		return -EINVAL;
5844 
5845 	/*
5846 	 * Since this string is allocated at mount time, and held until the
5847 	 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5848 	 * about a memory-reclaim deadlock.
5849 	 */
5850 	str = kmalloc(len, GFP_KERNEL);
5851 	if (!str)
5852 		return -ENOMEM;
5853 
5854 	scnprintf(str, len, "Linux NFSv%u.%u %s",
5855 			clp->rpc_ops->version, clp->cl_minorversion,
5856 			clp->cl_rpcclient->cl_nodename);
5857 	clp->cl_owner_id = str;
5858 	return 0;
5859 }
5860 
5861 /*
5862  * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5863  * services.  Advertise one based on the address family of the
5864  * clientaddr.
5865  */
5866 static unsigned int
5867 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5868 {
5869 	if (strchr(clp->cl_ipaddr, ':') != NULL)
5870 		return scnprintf(buf, len, "tcp6");
5871 	else
5872 		return scnprintf(buf, len, "tcp");
5873 }
5874 
5875 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5876 {
5877 	struct nfs4_setclientid *sc = calldata;
5878 
5879 	if (task->tk_status == 0)
5880 		sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5881 }
5882 
5883 static const struct rpc_call_ops nfs4_setclientid_ops = {
5884 	.rpc_call_done = nfs4_setclientid_done,
5885 };
5886 
5887 /**
5888  * nfs4_proc_setclientid - Negotiate client ID
5889  * @clp: state data structure
5890  * @program: RPC program for NFSv4 callback service
5891  * @port: IP port number for NFS4 callback service
5892  * @cred: credential to use for this call
5893  * @res: where to place the result
5894  *
5895  * Returns zero, a negative errno, or a negative NFS4ERR status code.
5896  */
5897 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5898 		unsigned short port, const struct cred *cred,
5899 		struct nfs4_setclientid_res *res)
5900 {
5901 	nfs4_verifier sc_verifier;
5902 	struct nfs4_setclientid setclientid = {
5903 		.sc_verifier = &sc_verifier,
5904 		.sc_prog = program,
5905 		.sc_clnt = clp,
5906 	};
5907 	struct rpc_message msg = {
5908 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5909 		.rpc_argp = &setclientid,
5910 		.rpc_resp = res,
5911 		.rpc_cred = cred,
5912 	};
5913 	struct rpc_task *task;
5914 	struct rpc_task_setup task_setup_data = {
5915 		.rpc_client = clp->cl_rpcclient,
5916 		.rpc_message = &msg,
5917 		.callback_ops = &nfs4_setclientid_ops,
5918 		.callback_data = &setclientid,
5919 		.flags = RPC_TASK_TIMEOUT,
5920 	};
5921 	int status;
5922 
5923 	/* nfs_client_id4 */
5924 	nfs4_init_boot_verifier(clp, &sc_verifier);
5925 
5926 	if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5927 		status = nfs4_init_uniform_client_string(clp);
5928 	else
5929 		status = nfs4_init_nonuniform_client_string(clp);
5930 
5931 	if (status)
5932 		goto out;
5933 
5934 	/* cb_client4 */
5935 	setclientid.sc_netid_len =
5936 				nfs4_init_callback_netid(clp,
5937 						setclientid.sc_netid,
5938 						sizeof(setclientid.sc_netid));
5939 	setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5940 				sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5941 				clp->cl_ipaddr, port >> 8, port & 255);
5942 
5943 	dprintk("NFS call  setclientid auth=%s, '%s'\n",
5944 		clp->cl_rpcclient->cl_auth->au_ops->au_name,
5945 		clp->cl_owner_id);
5946 	task = rpc_run_task(&task_setup_data);
5947 	if (IS_ERR(task)) {
5948 		status = PTR_ERR(task);
5949 		goto out;
5950 	}
5951 	status = task->tk_status;
5952 	if (setclientid.sc_cred) {
5953 		clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5954 		put_rpccred(setclientid.sc_cred);
5955 	}
5956 	rpc_put_task(task);
5957 out:
5958 	trace_nfs4_setclientid(clp, status);
5959 	dprintk("NFS reply setclientid: %d\n", status);
5960 	return status;
5961 }
5962 
5963 /**
5964  * nfs4_proc_setclientid_confirm - Confirm client ID
5965  * @clp: state data structure
5966  * @arg: result of a previous SETCLIENTID
5967  * @cred: credential to use for this call
5968  *
5969  * Returns zero, a negative errno, or a negative NFS4ERR status code.
5970  */
5971 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5972 		struct nfs4_setclientid_res *arg,
5973 		const struct cred *cred)
5974 {
5975 	struct rpc_message msg = {
5976 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5977 		.rpc_argp = arg,
5978 		.rpc_cred = cred,
5979 	};
5980 	int status;
5981 
5982 	dprintk("NFS call  setclientid_confirm auth=%s, (client ID %llx)\n",
5983 		clp->cl_rpcclient->cl_auth->au_ops->au_name,
5984 		clp->cl_clientid);
5985 	status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5986 	trace_nfs4_setclientid_confirm(clp, status);
5987 	dprintk("NFS reply setclientid_confirm: %d\n", status);
5988 	return status;
5989 }
5990 
5991 struct nfs4_delegreturndata {
5992 	struct nfs4_delegreturnargs args;
5993 	struct nfs4_delegreturnres res;
5994 	struct nfs_fh fh;
5995 	nfs4_stateid stateid;
5996 	unsigned long timestamp;
5997 	struct {
5998 		struct nfs4_layoutreturn_args arg;
5999 		struct nfs4_layoutreturn_res res;
6000 		struct nfs4_xdr_opaque_data ld_private;
6001 		u32 roc_barrier;
6002 		bool roc;
6003 	} lr;
6004 	struct nfs_fattr fattr;
6005 	int rpc_status;
6006 	struct inode *inode;
6007 };
6008 
6009 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6010 {
6011 	struct nfs4_delegreturndata *data = calldata;
6012 	struct nfs4_exception exception = {
6013 		.inode = data->inode,
6014 		.stateid = &data->stateid,
6015 	};
6016 
6017 	if (!nfs4_sequence_done(task, &data->res.seq_res))
6018 		return;
6019 
6020 	trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6021 
6022 	/* Handle Layoutreturn errors */
6023 	if (data->args.lr_args && task->tk_status != 0) {
6024 		switch(data->res.lr_ret) {
6025 		default:
6026 			data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6027 			break;
6028 		case 0:
6029 			data->args.lr_args = NULL;
6030 			data->res.lr_res = NULL;
6031 			break;
6032 		case -NFS4ERR_OLD_STATEID:
6033 			if (nfs4_layoutreturn_refresh_stateid(&data->args.lr_args->stateid,
6034 						&data->args.lr_args->range,
6035 						data->inode))
6036 				goto lr_restart;
6037 			/* Fallthrough */
6038 		case -NFS4ERR_ADMIN_REVOKED:
6039 		case -NFS4ERR_DELEG_REVOKED:
6040 		case -NFS4ERR_EXPIRED:
6041 		case -NFS4ERR_BAD_STATEID:
6042 		case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
6043 		case -NFS4ERR_WRONG_CRED:
6044 			data->args.lr_args = NULL;
6045 			data->res.lr_res = NULL;
6046 			goto lr_restart;
6047 		}
6048 	}
6049 
6050 	switch (task->tk_status) {
6051 	case 0:
6052 		renew_lease(data->res.server, data->timestamp);
6053 		break;
6054 	case -NFS4ERR_ADMIN_REVOKED:
6055 	case -NFS4ERR_DELEG_REVOKED:
6056 	case -NFS4ERR_EXPIRED:
6057 		nfs4_free_revoked_stateid(data->res.server,
6058 				data->args.stateid,
6059 				task->tk_msg.rpc_cred);
6060 		/* Fallthrough */
6061 	case -NFS4ERR_BAD_STATEID:
6062 	case -NFS4ERR_STALE_STATEID:
6063 		task->tk_status = 0;
6064 		break;
6065 	case -NFS4ERR_OLD_STATEID:
6066 		if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6067 			goto out_restart;
6068 		task->tk_status = 0;
6069 		break;
6070 	case -NFS4ERR_ACCESS:
6071 		if (data->args.bitmask) {
6072 			data->args.bitmask = NULL;
6073 			data->res.fattr = NULL;
6074 			goto out_restart;
6075 		}
6076 		/* Fallthrough */
6077 	default:
6078 		task->tk_status = nfs4_async_handle_exception(task,
6079 				data->res.server, task->tk_status,
6080 				&exception);
6081 		if (exception.retry)
6082 			goto out_restart;
6083 	}
6084 	data->rpc_status = task->tk_status;
6085 	return;
6086 lr_restart:
6087 	data->res.lr_ret = 0;
6088 out_restart:
6089 	task->tk_status = 0;
6090 	rpc_restart_call_prepare(task);
6091 }
6092 
6093 static void nfs4_delegreturn_release(void *calldata)
6094 {
6095 	struct nfs4_delegreturndata *data = calldata;
6096 	struct inode *inode = data->inode;
6097 
6098 	if (inode) {
6099 		if (data->lr.roc)
6100 			pnfs_roc_release(&data->lr.arg, &data->lr.res,
6101 					data->res.lr_ret);
6102 		nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6103 		nfs_iput_and_deactive(inode);
6104 	}
6105 	kfree(calldata);
6106 }
6107 
6108 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6109 {
6110 	struct nfs4_delegreturndata *d_data;
6111 	struct pnfs_layout_hdr *lo;
6112 
6113 	d_data = (struct nfs4_delegreturndata *)data;
6114 
6115 	if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
6116 		return;
6117 
6118 	lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6119 	if (lo && !pnfs_layout_is_valid(lo)) {
6120 		d_data->args.lr_args = NULL;
6121 		d_data->res.lr_res = NULL;
6122 	}
6123 
6124 	nfs4_setup_sequence(d_data->res.server->nfs_client,
6125 			&d_data->args.seq_args,
6126 			&d_data->res.seq_res,
6127 			task);
6128 }
6129 
6130 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6131 	.rpc_call_prepare = nfs4_delegreturn_prepare,
6132 	.rpc_call_done = nfs4_delegreturn_done,
6133 	.rpc_release = nfs4_delegreturn_release,
6134 };
6135 
6136 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6137 {
6138 	struct nfs4_delegreturndata *data;
6139 	struct nfs_server *server = NFS_SERVER(inode);
6140 	struct rpc_task *task;
6141 	struct rpc_message msg = {
6142 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6143 		.rpc_cred = cred,
6144 	};
6145 	struct rpc_task_setup task_setup_data = {
6146 		.rpc_client = server->client,
6147 		.rpc_message = &msg,
6148 		.callback_ops = &nfs4_delegreturn_ops,
6149 		.flags = RPC_TASK_ASYNC,
6150 	};
6151 	int status = 0;
6152 
6153 	data = kzalloc(sizeof(*data), GFP_NOFS);
6154 	if (data == NULL)
6155 		return -ENOMEM;
6156 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
6157 
6158 	nfs4_state_protect(server->nfs_client,
6159 			NFS_SP4_MACH_CRED_CLEANUP,
6160 			&task_setup_data.rpc_client, &msg);
6161 
6162 	data->args.fhandle = &data->fh;
6163 	data->args.stateid = &data->stateid;
6164 	data->args.bitmask = server->cache_consistency_bitmask;
6165 	nfs_copy_fh(&data->fh, NFS_FH(inode));
6166 	nfs4_stateid_copy(&data->stateid, stateid);
6167 	data->res.fattr = &data->fattr;
6168 	data->res.server = server;
6169 	data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6170 	data->lr.arg.ld_private = &data->lr.ld_private;
6171 	nfs_fattr_init(data->res.fattr);
6172 	data->timestamp = jiffies;
6173 	data->rpc_status = 0;
6174 	data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
6175 	data->inode = nfs_igrab_and_active(inode);
6176 	if (data->inode) {
6177 		if (data->lr.roc) {
6178 			data->args.lr_args = &data->lr.arg;
6179 			data->res.lr_res = &data->lr.res;
6180 		}
6181 	} else if (data->lr.roc) {
6182 		pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6183 		data->lr.roc = false;
6184 	}
6185 
6186 	task_setup_data.callback_data = data;
6187 	msg.rpc_argp = &data->args;
6188 	msg.rpc_resp = &data->res;
6189 	task = rpc_run_task(&task_setup_data);
6190 	if (IS_ERR(task))
6191 		return PTR_ERR(task);
6192 	if (!issync)
6193 		goto out;
6194 	status = rpc_wait_for_completion_task(task);
6195 	if (status != 0)
6196 		goto out;
6197 	status = data->rpc_status;
6198 out:
6199 	rpc_put_task(task);
6200 	return status;
6201 }
6202 
6203 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6204 {
6205 	struct nfs_server *server = NFS_SERVER(inode);
6206 	struct nfs4_exception exception = { };
6207 	int err;
6208 	do {
6209 		err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6210 		trace_nfs4_delegreturn(inode, stateid, err);
6211 		switch (err) {
6212 			case -NFS4ERR_STALE_STATEID:
6213 			case -NFS4ERR_EXPIRED:
6214 			case 0:
6215 				return 0;
6216 		}
6217 		err = nfs4_handle_exception(server, err, &exception);
6218 	} while (exception.retry);
6219 	return err;
6220 }
6221 
6222 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6223 {
6224 	struct inode *inode = state->inode;
6225 	struct nfs_server *server = NFS_SERVER(inode);
6226 	struct nfs_client *clp = server->nfs_client;
6227 	struct nfs_lockt_args arg = {
6228 		.fh = NFS_FH(inode),
6229 		.fl = request,
6230 	};
6231 	struct nfs_lockt_res res = {
6232 		.denied = request,
6233 	};
6234 	struct rpc_message msg = {
6235 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6236 		.rpc_argp	= &arg,
6237 		.rpc_resp	= &res,
6238 		.rpc_cred	= state->owner->so_cred,
6239 	};
6240 	struct nfs4_lock_state *lsp;
6241 	int status;
6242 
6243 	arg.lock_owner.clientid = clp->cl_clientid;
6244 	status = nfs4_set_lock_state(state, request);
6245 	if (status != 0)
6246 		goto out;
6247 	lsp = request->fl_u.nfs4_fl.owner;
6248 	arg.lock_owner.id = lsp->ls_seqid.owner_id;
6249 	arg.lock_owner.s_dev = server->s_dev;
6250 	status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6251 	switch (status) {
6252 		case 0:
6253 			request->fl_type = F_UNLCK;
6254 			break;
6255 		case -NFS4ERR_DENIED:
6256 			status = 0;
6257 	}
6258 	request->fl_ops->fl_release_private(request);
6259 	request->fl_ops = NULL;
6260 out:
6261 	return status;
6262 }
6263 
6264 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6265 {
6266 	struct nfs4_exception exception = { };
6267 	int err;
6268 
6269 	do {
6270 		err = _nfs4_proc_getlk(state, cmd, request);
6271 		trace_nfs4_get_lock(request, state, cmd, err);
6272 		err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6273 				&exception);
6274 	} while (exception.retry);
6275 	return err;
6276 }
6277 
6278 struct nfs4_unlockdata {
6279 	struct nfs_locku_args arg;
6280 	struct nfs_locku_res res;
6281 	struct nfs4_lock_state *lsp;
6282 	struct nfs_open_context *ctx;
6283 	struct nfs_lock_context *l_ctx;
6284 	struct file_lock fl;
6285 	struct nfs_server *server;
6286 	unsigned long timestamp;
6287 };
6288 
6289 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6290 		struct nfs_open_context *ctx,
6291 		struct nfs4_lock_state *lsp,
6292 		struct nfs_seqid *seqid)
6293 {
6294 	struct nfs4_unlockdata *p;
6295 	struct inode *inode = lsp->ls_state->inode;
6296 
6297 	p = kzalloc(sizeof(*p), GFP_NOFS);
6298 	if (p == NULL)
6299 		return NULL;
6300 	p->arg.fh = NFS_FH(inode);
6301 	p->arg.fl = &p->fl;
6302 	p->arg.seqid = seqid;
6303 	p->res.seqid = seqid;
6304 	p->lsp = lsp;
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 	p->ctx = get_nfs_open_context(ctx);
6530 	locks_init_lock(&p->fl);
6531 	locks_copy_lock(&p->fl, fl);
6532 	return p;
6533 out_free_seqid:
6534 	nfs_free_seqid(p->arg.open_seqid);
6535 out_free:
6536 	kfree(p);
6537 	return NULL;
6538 }
6539 
6540 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6541 {
6542 	struct nfs4_lockdata *data = calldata;
6543 	struct nfs4_state *state = data->lsp->ls_state;
6544 
6545 	dprintk("%s: begin!\n", __func__);
6546 	if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6547 		goto out_wait;
6548 	/* Do we need to do an open_to_lock_owner? */
6549 	if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6550 		if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6551 			goto out_release_lock_seqid;
6552 		}
6553 		nfs4_stateid_copy(&data->arg.open_stateid,
6554 				&state->open_stateid);
6555 		data->arg.new_lock_owner = 1;
6556 		data->res.open_seqid = data->arg.open_seqid;
6557 	} else {
6558 		data->arg.new_lock_owner = 0;
6559 		nfs4_stateid_copy(&data->arg.lock_stateid,
6560 				&data->lsp->ls_stateid);
6561 	}
6562 	if (!nfs4_valid_open_stateid(state)) {
6563 		data->rpc_status = -EBADF;
6564 		task->tk_action = NULL;
6565 		goto out_release_open_seqid;
6566 	}
6567 	data->timestamp = jiffies;
6568 	if (nfs4_setup_sequence(data->server->nfs_client,
6569 				&data->arg.seq_args,
6570 				&data->res.seq_res,
6571 				task) == 0)
6572 		return;
6573 out_release_open_seqid:
6574 	nfs_release_seqid(data->arg.open_seqid);
6575 out_release_lock_seqid:
6576 	nfs_release_seqid(data->arg.lock_seqid);
6577 out_wait:
6578 	nfs4_sequence_done(task, &data->res.seq_res);
6579 	dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6580 }
6581 
6582 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6583 {
6584 	struct nfs4_lockdata *data = calldata;
6585 	struct nfs4_lock_state *lsp = data->lsp;
6586 
6587 	dprintk("%s: begin!\n", __func__);
6588 
6589 	if (!nfs4_sequence_done(task, &data->res.seq_res))
6590 		return;
6591 
6592 	data->rpc_status = task->tk_status;
6593 	switch (task->tk_status) {
6594 	case 0:
6595 		renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6596 				data->timestamp);
6597 		if (data->arg.new_lock && !data->cancelled) {
6598 			data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6599 			if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
6600 				goto out_restart;
6601 		}
6602 		if (data->arg.new_lock_owner != 0) {
6603 			nfs_confirm_seqid(&lsp->ls_seqid, 0);
6604 			nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6605 			set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6606 		} else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6607 			goto out_restart;
6608 		break;
6609 	case -NFS4ERR_BAD_STATEID:
6610 	case -NFS4ERR_OLD_STATEID:
6611 	case -NFS4ERR_STALE_STATEID:
6612 	case -NFS4ERR_EXPIRED:
6613 		if (data->arg.new_lock_owner != 0) {
6614 			if (!nfs4_stateid_match(&data->arg.open_stateid,
6615 						&lsp->ls_state->open_stateid))
6616 				goto out_restart;
6617 		} else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6618 						&lsp->ls_stateid))
6619 				goto out_restart;
6620 	}
6621 out_done:
6622 	dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6623 	return;
6624 out_restart:
6625 	if (!data->cancelled)
6626 		rpc_restart_call_prepare(task);
6627 	goto out_done;
6628 }
6629 
6630 static void nfs4_lock_release(void *calldata)
6631 {
6632 	struct nfs4_lockdata *data = calldata;
6633 
6634 	dprintk("%s: begin!\n", __func__);
6635 	nfs_free_seqid(data->arg.open_seqid);
6636 	if (data->cancelled && data->rpc_status == 0) {
6637 		struct rpc_task *task;
6638 		task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6639 				data->arg.lock_seqid);
6640 		if (!IS_ERR(task))
6641 			rpc_put_task_async(task);
6642 		dprintk("%s: cancelling lock!\n", __func__);
6643 	} else
6644 		nfs_free_seqid(data->arg.lock_seqid);
6645 	nfs4_put_lock_state(data->lsp);
6646 	put_nfs_open_context(data->ctx);
6647 	kfree(data);
6648 	dprintk("%s: done!\n", __func__);
6649 }
6650 
6651 static const struct rpc_call_ops nfs4_lock_ops = {
6652 	.rpc_call_prepare = nfs4_lock_prepare,
6653 	.rpc_call_done = nfs4_lock_done,
6654 	.rpc_release = nfs4_lock_release,
6655 };
6656 
6657 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6658 {
6659 	switch (error) {
6660 	case -NFS4ERR_ADMIN_REVOKED:
6661 	case -NFS4ERR_EXPIRED:
6662 	case -NFS4ERR_BAD_STATEID:
6663 		lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6664 		if (new_lock_owner != 0 ||
6665 		   test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6666 			nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6667 		break;
6668 	case -NFS4ERR_STALE_STATEID:
6669 		lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6670 		nfs4_schedule_lease_recovery(server->nfs_client);
6671 	};
6672 }
6673 
6674 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6675 {
6676 	struct nfs4_lockdata *data;
6677 	struct rpc_task *task;
6678 	struct rpc_message msg = {
6679 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6680 		.rpc_cred = state->owner->so_cred,
6681 	};
6682 	struct rpc_task_setup task_setup_data = {
6683 		.rpc_client = NFS_CLIENT(state->inode),
6684 		.rpc_message = &msg,
6685 		.callback_ops = &nfs4_lock_ops,
6686 		.workqueue = nfsiod_workqueue,
6687 		.flags = RPC_TASK_ASYNC,
6688 	};
6689 	int ret;
6690 
6691 	dprintk("%s: begin!\n", __func__);
6692 	data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6693 			fl->fl_u.nfs4_fl.owner,
6694 			recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6695 	if (data == NULL)
6696 		return -ENOMEM;
6697 	if (IS_SETLKW(cmd))
6698 		data->arg.block = 1;
6699 	nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6700 				recovery_type > NFS_LOCK_NEW);
6701 	msg.rpc_argp = &data->arg;
6702 	msg.rpc_resp = &data->res;
6703 	task_setup_data.callback_data = data;
6704 	if (recovery_type > NFS_LOCK_NEW) {
6705 		if (recovery_type == NFS_LOCK_RECLAIM)
6706 			data->arg.reclaim = NFS_LOCK_RECLAIM;
6707 	} else
6708 		data->arg.new_lock = 1;
6709 	task = rpc_run_task(&task_setup_data);
6710 	if (IS_ERR(task))
6711 		return PTR_ERR(task);
6712 	ret = rpc_wait_for_completion_task(task);
6713 	if (ret == 0) {
6714 		ret = data->rpc_status;
6715 		if (ret)
6716 			nfs4_handle_setlk_error(data->server, data->lsp,
6717 					data->arg.new_lock_owner, ret);
6718 	} else
6719 		data->cancelled = true;
6720 	rpc_put_task(task);
6721 	dprintk("%s: done, ret = %d!\n", __func__, ret);
6722 	trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6723 	return ret;
6724 }
6725 
6726 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6727 {
6728 	struct nfs_server *server = NFS_SERVER(state->inode);
6729 	struct nfs4_exception exception = {
6730 		.inode = state->inode,
6731 	};
6732 	int err;
6733 
6734 	do {
6735 		/* Cache the lock if possible... */
6736 		if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6737 			return 0;
6738 		err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6739 		if (err != -NFS4ERR_DELAY)
6740 			break;
6741 		nfs4_handle_exception(server, err, &exception);
6742 	} while (exception.retry);
6743 	return err;
6744 }
6745 
6746 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6747 {
6748 	struct nfs_server *server = NFS_SERVER(state->inode);
6749 	struct nfs4_exception exception = {
6750 		.inode = state->inode,
6751 	};
6752 	int err;
6753 
6754 	err = nfs4_set_lock_state(state, request);
6755 	if (err != 0)
6756 		return err;
6757 	if (!recover_lost_locks) {
6758 		set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6759 		return 0;
6760 	}
6761 	do {
6762 		if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6763 			return 0;
6764 		err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6765 		switch (err) {
6766 		default:
6767 			goto out;
6768 		case -NFS4ERR_GRACE:
6769 		case -NFS4ERR_DELAY:
6770 			nfs4_handle_exception(server, err, &exception);
6771 			err = 0;
6772 		}
6773 	} while (exception.retry);
6774 out:
6775 	return err;
6776 }
6777 
6778 #if defined(CONFIG_NFS_V4_1)
6779 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6780 {
6781 	struct nfs4_lock_state *lsp;
6782 	int status;
6783 
6784 	status = nfs4_set_lock_state(state, request);
6785 	if (status != 0)
6786 		return status;
6787 	lsp = request->fl_u.nfs4_fl.owner;
6788 	if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6789 	    test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6790 		return 0;
6791 	return nfs4_lock_expired(state, request);
6792 }
6793 #endif
6794 
6795 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6796 {
6797 	struct nfs_inode *nfsi = NFS_I(state->inode);
6798 	struct nfs4_state_owner *sp = state->owner;
6799 	unsigned char fl_flags = request->fl_flags;
6800 	int status;
6801 
6802 	request->fl_flags |= FL_ACCESS;
6803 	status = locks_lock_inode_wait(state->inode, request);
6804 	if (status < 0)
6805 		goto out;
6806 	mutex_lock(&sp->so_delegreturn_mutex);
6807 	down_read(&nfsi->rwsem);
6808 	if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6809 		/* Yes: cache locks! */
6810 		/* ...but avoid races with delegation recall... */
6811 		request->fl_flags = fl_flags & ~FL_SLEEP;
6812 		status = locks_lock_inode_wait(state->inode, request);
6813 		up_read(&nfsi->rwsem);
6814 		mutex_unlock(&sp->so_delegreturn_mutex);
6815 		goto out;
6816 	}
6817 	up_read(&nfsi->rwsem);
6818 	mutex_unlock(&sp->so_delegreturn_mutex);
6819 	status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6820 out:
6821 	request->fl_flags = fl_flags;
6822 	return status;
6823 }
6824 
6825 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6826 {
6827 	struct nfs4_exception exception = {
6828 		.state = state,
6829 		.inode = state->inode,
6830 	};
6831 	int err;
6832 
6833 	do {
6834 		err = _nfs4_proc_setlk(state, cmd, request);
6835 		if (err == -NFS4ERR_DENIED)
6836 			err = -EAGAIN;
6837 		err = nfs4_handle_exception(NFS_SERVER(state->inode),
6838 				err, &exception);
6839 	} while (exception.retry);
6840 	return err;
6841 }
6842 
6843 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6844 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6845 
6846 static int
6847 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6848 			struct file_lock *request)
6849 {
6850 	int		status = -ERESTARTSYS;
6851 	unsigned long	timeout = NFS4_LOCK_MINTIMEOUT;
6852 
6853 	while(!signalled()) {
6854 		status = nfs4_proc_setlk(state, cmd, request);
6855 		if ((status != -EAGAIN) || IS_SETLK(cmd))
6856 			break;
6857 		freezable_schedule_timeout_interruptible(timeout);
6858 		timeout *= 2;
6859 		timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6860 		status = -ERESTARTSYS;
6861 	}
6862 	return status;
6863 }
6864 
6865 #ifdef CONFIG_NFS_V4_1
6866 struct nfs4_lock_waiter {
6867 	struct task_struct	*task;
6868 	struct inode		*inode;
6869 	struct nfs_lowner	*owner;
6870 	bool			notified;
6871 };
6872 
6873 static int
6874 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
6875 {
6876 	int ret;
6877 	struct nfs4_lock_waiter	*waiter	= wait->private;
6878 
6879 	/* NULL key means to wake up everyone */
6880 	if (key) {
6881 		struct cb_notify_lock_args	*cbnl = key;
6882 		struct nfs_lowner		*lowner = &cbnl->cbnl_owner,
6883 						*wowner = waiter->owner;
6884 
6885 		/* Only wake if the callback was for the same owner. */
6886 		if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6887 			return 0;
6888 
6889 		/* Make sure it's for the right inode */
6890 		if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6891 			return 0;
6892 
6893 		waiter->notified = true;
6894 	}
6895 
6896 	/* override "private" so we can use default_wake_function */
6897 	wait->private = waiter->task;
6898 	ret = autoremove_wake_function(wait, mode, flags, key);
6899 	wait->private = waiter;
6900 	return ret;
6901 }
6902 
6903 static int
6904 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6905 {
6906 	int status = -ERESTARTSYS;
6907 	unsigned long flags;
6908 	struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6909 	struct nfs_server *server = NFS_SERVER(state->inode);
6910 	struct nfs_client *clp = server->nfs_client;
6911 	wait_queue_head_t *q = &clp->cl_lock_waitq;
6912 	struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6913 				    .id = lsp->ls_seqid.owner_id,
6914 				    .s_dev = server->s_dev };
6915 	struct nfs4_lock_waiter waiter = { .task  = current,
6916 					   .inode = state->inode,
6917 					   .owner = &owner,
6918 					   .notified = false };
6919 	wait_queue_entry_t wait;
6920 
6921 	/* Don't bother with waitqueue if we don't expect a callback */
6922 	if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6923 		return nfs4_retry_setlk_simple(state, cmd, request);
6924 
6925 	init_wait(&wait);
6926 	wait.private = &waiter;
6927 	wait.func = nfs4_wake_lock_waiter;
6928 	add_wait_queue(q, &wait);
6929 
6930 	while(!signalled()) {
6931 		waiter.notified = false;
6932 		status = nfs4_proc_setlk(state, cmd, request);
6933 		if ((status != -EAGAIN) || IS_SETLK(cmd))
6934 			break;
6935 
6936 		status = -ERESTARTSYS;
6937 		spin_lock_irqsave(&q->lock, flags);
6938 		if (waiter.notified) {
6939 			spin_unlock_irqrestore(&q->lock, flags);
6940 			continue;
6941 		}
6942 		set_current_state(TASK_INTERRUPTIBLE);
6943 		spin_unlock_irqrestore(&q->lock, flags);
6944 
6945 		freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
6946 	}
6947 
6948 	finish_wait(q, &wait);
6949 	return status;
6950 }
6951 #else /* !CONFIG_NFS_V4_1 */
6952 static inline int
6953 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6954 {
6955 	return nfs4_retry_setlk_simple(state, cmd, request);
6956 }
6957 #endif
6958 
6959 static int
6960 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6961 {
6962 	struct nfs_open_context *ctx;
6963 	struct nfs4_state *state;
6964 	int status;
6965 
6966 	/* verify open state */
6967 	ctx = nfs_file_open_context(filp);
6968 	state = ctx->state;
6969 
6970 	if (IS_GETLK(cmd)) {
6971 		if (state != NULL)
6972 			return nfs4_proc_getlk(state, F_GETLK, request);
6973 		return 0;
6974 	}
6975 
6976 	if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6977 		return -EINVAL;
6978 
6979 	if (request->fl_type == F_UNLCK) {
6980 		if (state != NULL)
6981 			return nfs4_proc_unlck(state, cmd, request);
6982 		return 0;
6983 	}
6984 
6985 	if (state == NULL)
6986 		return -ENOLCK;
6987 
6988 	if ((request->fl_flags & FL_POSIX) &&
6989 	    !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6990 		return -ENOLCK;
6991 
6992 	/*
6993 	 * Don't rely on the VFS having checked the file open mode,
6994 	 * since it won't do this for flock() locks.
6995 	 */
6996 	switch (request->fl_type) {
6997 	case F_RDLCK:
6998 		if (!(filp->f_mode & FMODE_READ))
6999 			return -EBADF;
7000 		break;
7001 	case F_WRLCK:
7002 		if (!(filp->f_mode & FMODE_WRITE))
7003 			return -EBADF;
7004 	}
7005 
7006 	status = nfs4_set_lock_state(state, request);
7007 	if (status != 0)
7008 		return status;
7009 
7010 	return nfs4_retry_setlk(state, cmd, request);
7011 }
7012 
7013 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7014 {
7015 	struct nfs_server *server = NFS_SERVER(state->inode);
7016 	int err;
7017 
7018 	err = nfs4_set_lock_state(state, fl);
7019 	if (err != 0)
7020 		return err;
7021 	err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7022 	return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7023 }
7024 
7025 struct nfs_release_lockowner_data {
7026 	struct nfs4_lock_state *lsp;
7027 	struct nfs_server *server;
7028 	struct nfs_release_lockowner_args args;
7029 	struct nfs_release_lockowner_res res;
7030 	unsigned long timestamp;
7031 };
7032 
7033 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7034 {
7035 	struct nfs_release_lockowner_data *data = calldata;
7036 	struct nfs_server *server = data->server;
7037 	nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7038 			   &data->res.seq_res, task);
7039 	data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7040 	data->timestamp = jiffies;
7041 }
7042 
7043 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7044 {
7045 	struct nfs_release_lockowner_data *data = calldata;
7046 	struct nfs_server *server = data->server;
7047 
7048 	nfs40_sequence_done(task, &data->res.seq_res);
7049 
7050 	switch (task->tk_status) {
7051 	case 0:
7052 		renew_lease(server, data->timestamp);
7053 		break;
7054 	case -NFS4ERR_STALE_CLIENTID:
7055 	case -NFS4ERR_EXPIRED:
7056 		nfs4_schedule_lease_recovery(server->nfs_client);
7057 		break;
7058 	case -NFS4ERR_LEASE_MOVED:
7059 	case -NFS4ERR_DELAY:
7060 		if (nfs4_async_handle_error(task, server,
7061 					    NULL, NULL) == -EAGAIN)
7062 			rpc_restart_call_prepare(task);
7063 	}
7064 }
7065 
7066 static void nfs4_release_lockowner_release(void *calldata)
7067 {
7068 	struct nfs_release_lockowner_data *data = calldata;
7069 	nfs4_free_lock_state(data->server, data->lsp);
7070 	kfree(calldata);
7071 }
7072 
7073 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7074 	.rpc_call_prepare = nfs4_release_lockowner_prepare,
7075 	.rpc_call_done = nfs4_release_lockowner_done,
7076 	.rpc_release = nfs4_release_lockowner_release,
7077 };
7078 
7079 static void
7080 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7081 {
7082 	struct nfs_release_lockowner_data *data;
7083 	struct rpc_message msg = {
7084 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7085 	};
7086 
7087 	if (server->nfs_client->cl_mvops->minor_version != 0)
7088 		return;
7089 
7090 	data = kmalloc(sizeof(*data), GFP_NOFS);
7091 	if (!data)
7092 		return;
7093 	data->lsp = lsp;
7094 	data->server = server;
7095 	data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7096 	data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7097 	data->args.lock_owner.s_dev = server->s_dev;
7098 
7099 	msg.rpc_argp = &data->args;
7100 	msg.rpc_resp = &data->res;
7101 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7102 	rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7103 }
7104 
7105 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7106 
7107 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7108 				   struct dentry *unused, struct inode *inode,
7109 				   const char *key, const void *buf,
7110 				   size_t buflen, int flags)
7111 {
7112 	return nfs4_proc_set_acl(inode, buf, buflen);
7113 }
7114 
7115 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7116 				   struct dentry *unused, struct inode *inode,
7117 				   const char *key, void *buf, size_t buflen)
7118 {
7119 	return nfs4_proc_get_acl(inode, buf, buflen);
7120 }
7121 
7122 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7123 {
7124 	return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7125 }
7126 
7127 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7128 
7129 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7130 				     struct dentry *unused, struct inode *inode,
7131 				     const char *key, const void *buf,
7132 				     size_t buflen, int flags)
7133 {
7134 	if (security_ismaclabel(key))
7135 		return nfs4_set_security_label(inode, buf, buflen);
7136 
7137 	return -EOPNOTSUPP;
7138 }
7139 
7140 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7141 				     struct dentry *unused, struct inode *inode,
7142 				     const char *key, void *buf, size_t buflen)
7143 {
7144 	if (security_ismaclabel(key))
7145 		return nfs4_get_security_label(inode, buf, buflen);
7146 	return -EOPNOTSUPP;
7147 }
7148 
7149 static ssize_t
7150 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7151 {
7152 	int len = 0;
7153 
7154 	if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7155 		len = security_inode_listsecurity(inode, list, list_len);
7156 		if (list_len && len > list_len)
7157 			return -ERANGE;
7158 	}
7159 	return len;
7160 }
7161 
7162 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7163 	.prefix = XATTR_SECURITY_PREFIX,
7164 	.get	= nfs4_xattr_get_nfs4_label,
7165 	.set	= nfs4_xattr_set_nfs4_label,
7166 };
7167 
7168 #else
7169 
7170 static ssize_t
7171 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7172 {
7173 	return 0;
7174 }
7175 
7176 #endif
7177 
7178 /*
7179  * nfs_fhget will use either the mounted_on_fileid or the fileid
7180  */
7181 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7182 {
7183 	if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7184 	       (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7185 	      (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7186 	      (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7187 		return;
7188 
7189 	fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7190 		NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7191 	fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7192 	fattr->nlink = 2;
7193 }
7194 
7195 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7196 				   const struct qstr *name,
7197 				   struct nfs4_fs_locations *fs_locations,
7198 				   struct page *page)
7199 {
7200 	struct nfs_server *server = NFS_SERVER(dir);
7201 	u32 bitmask[3];
7202 	struct nfs4_fs_locations_arg args = {
7203 		.dir_fh = NFS_FH(dir),
7204 		.name = name,
7205 		.page = page,
7206 		.bitmask = bitmask,
7207 	};
7208 	struct nfs4_fs_locations_res res = {
7209 		.fs_locations = fs_locations,
7210 	};
7211 	struct rpc_message msg = {
7212 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7213 		.rpc_argp = &args,
7214 		.rpc_resp = &res,
7215 	};
7216 	int status;
7217 
7218 	dprintk("%s: start\n", __func__);
7219 
7220 	bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7221 	bitmask[1] = nfs4_fattr_bitmap[1];
7222 
7223 	/* Ask for the fileid of the absent filesystem if mounted_on_fileid
7224 	 * is not supported */
7225 	if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7226 		bitmask[0] &= ~FATTR4_WORD0_FILEID;
7227 	else
7228 		bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7229 
7230 	nfs_fattr_init(&fs_locations->fattr);
7231 	fs_locations->server = server;
7232 	fs_locations->nlocations = 0;
7233 	status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7234 	dprintk("%s: returned status = %d\n", __func__, status);
7235 	return status;
7236 }
7237 
7238 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7239 			   const struct qstr *name,
7240 			   struct nfs4_fs_locations *fs_locations,
7241 			   struct page *page)
7242 {
7243 	struct nfs4_exception exception = { };
7244 	int err;
7245 	do {
7246 		err = _nfs4_proc_fs_locations(client, dir, name,
7247 				fs_locations, page);
7248 		trace_nfs4_get_fs_locations(dir, name, err);
7249 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
7250 				&exception);
7251 	} while (exception.retry);
7252 	return err;
7253 }
7254 
7255 /*
7256  * This operation also signals the server that this client is
7257  * performing migration recovery.  The server can stop returning
7258  * NFS4ERR_LEASE_MOVED to this client.  A RENEW operation is
7259  * appended to this compound to identify the client ID which is
7260  * performing recovery.
7261  */
7262 static int _nfs40_proc_get_locations(struct inode *inode,
7263 				     struct nfs4_fs_locations *locations,
7264 				     struct page *page, const struct cred *cred)
7265 {
7266 	struct nfs_server *server = NFS_SERVER(inode);
7267 	struct rpc_clnt *clnt = server->client;
7268 	u32 bitmask[2] = {
7269 		[0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7270 	};
7271 	struct nfs4_fs_locations_arg args = {
7272 		.clientid	= server->nfs_client->cl_clientid,
7273 		.fh		= NFS_FH(inode),
7274 		.page		= page,
7275 		.bitmask	= bitmask,
7276 		.migration	= 1,		/* skip LOOKUP */
7277 		.renew		= 1,		/* append RENEW */
7278 	};
7279 	struct nfs4_fs_locations_res res = {
7280 		.fs_locations	= locations,
7281 		.migration	= 1,
7282 		.renew		= 1,
7283 	};
7284 	struct rpc_message msg = {
7285 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7286 		.rpc_argp	= &args,
7287 		.rpc_resp	= &res,
7288 		.rpc_cred	= cred,
7289 	};
7290 	unsigned long now = jiffies;
7291 	int status;
7292 
7293 	nfs_fattr_init(&locations->fattr);
7294 	locations->server = server;
7295 	locations->nlocations = 0;
7296 
7297 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7298 	status = nfs4_call_sync_sequence(clnt, server, &msg,
7299 					&args.seq_args, &res.seq_res);
7300 	if (status)
7301 		return status;
7302 
7303 	renew_lease(server, now);
7304 	return 0;
7305 }
7306 
7307 #ifdef CONFIG_NFS_V4_1
7308 
7309 /*
7310  * This operation also signals the server that this client is
7311  * performing migration recovery.  The server can stop asserting
7312  * SEQ4_STATUS_LEASE_MOVED for this client.  The client ID
7313  * performing this operation is identified in the SEQUENCE
7314  * operation in this compound.
7315  *
7316  * When the client supports GETATTR(fs_locations_info), it can
7317  * be plumbed in here.
7318  */
7319 static int _nfs41_proc_get_locations(struct inode *inode,
7320 				     struct nfs4_fs_locations *locations,
7321 				     struct page *page, const struct cred *cred)
7322 {
7323 	struct nfs_server *server = NFS_SERVER(inode);
7324 	struct rpc_clnt *clnt = server->client;
7325 	u32 bitmask[2] = {
7326 		[0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7327 	};
7328 	struct nfs4_fs_locations_arg args = {
7329 		.fh		= NFS_FH(inode),
7330 		.page		= page,
7331 		.bitmask	= bitmask,
7332 		.migration	= 1,		/* skip LOOKUP */
7333 	};
7334 	struct nfs4_fs_locations_res res = {
7335 		.fs_locations	= locations,
7336 		.migration	= 1,
7337 	};
7338 	struct rpc_message msg = {
7339 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7340 		.rpc_argp	= &args,
7341 		.rpc_resp	= &res,
7342 		.rpc_cred	= cred,
7343 	};
7344 	int status;
7345 
7346 	nfs_fattr_init(&locations->fattr);
7347 	locations->server = server;
7348 	locations->nlocations = 0;
7349 
7350 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7351 	status = nfs4_call_sync_sequence(clnt, server, &msg,
7352 					&args.seq_args, &res.seq_res);
7353 	if (status == NFS4_OK &&
7354 	    res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7355 		status = -NFS4ERR_LEASE_MOVED;
7356 	return status;
7357 }
7358 
7359 #endif	/* CONFIG_NFS_V4_1 */
7360 
7361 /**
7362  * nfs4_proc_get_locations - discover locations for a migrated FSID
7363  * @inode: inode on FSID that is migrating
7364  * @locations: result of query
7365  * @page: buffer
7366  * @cred: credential to use for this operation
7367  *
7368  * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7369  * operation failed, or a negative errno if a local error occurred.
7370  *
7371  * On success, "locations" is filled in, but if the server has
7372  * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7373  * asserted.
7374  *
7375  * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7376  * from this client that require migration recovery.
7377  */
7378 int nfs4_proc_get_locations(struct inode *inode,
7379 			    struct nfs4_fs_locations *locations,
7380 			    struct page *page, const struct cred *cred)
7381 {
7382 	struct nfs_server *server = NFS_SERVER(inode);
7383 	struct nfs_client *clp = server->nfs_client;
7384 	const struct nfs4_mig_recovery_ops *ops =
7385 					clp->cl_mvops->mig_recovery_ops;
7386 	struct nfs4_exception exception = { };
7387 	int status;
7388 
7389 	dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7390 		(unsigned long long)server->fsid.major,
7391 		(unsigned long long)server->fsid.minor,
7392 		clp->cl_hostname);
7393 	nfs_display_fhandle(NFS_FH(inode), __func__);
7394 
7395 	do {
7396 		status = ops->get_locations(inode, locations, page, cred);
7397 		if (status != -NFS4ERR_DELAY)
7398 			break;
7399 		nfs4_handle_exception(server, status, &exception);
7400 	} while (exception.retry);
7401 	return status;
7402 }
7403 
7404 /*
7405  * This operation also signals the server that this client is
7406  * performing "lease moved" recovery.  The server can stop
7407  * returning NFS4ERR_LEASE_MOVED to this client.  A RENEW operation
7408  * is appended to this compound to identify the client ID which is
7409  * performing recovery.
7410  */
7411 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
7412 {
7413 	struct nfs_server *server = NFS_SERVER(inode);
7414 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7415 	struct rpc_clnt *clnt = server->client;
7416 	struct nfs4_fsid_present_arg args = {
7417 		.fh		= NFS_FH(inode),
7418 		.clientid	= clp->cl_clientid,
7419 		.renew		= 1,		/* append RENEW */
7420 	};
7421 	struct nfs4_fsid_present_res res = {
7422 		.renew		= 1,
7423 	};
7424 	struct rpc_message msg = {
7425 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7426 		.rpc_argp	= &args,
7427 		.rpc_resp	= &res,
7428 		.rpc_cred	= cred,
7429 	};
7430 	unsigned long now = jiffies;
7431 	int status;
7432 
7433 	res.fh = nfs_alloc_fhandle();
7434 	if (res.fh == NULL)
7435 		return -ENOMEM;
7436 
7437 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7438 	status = nfs4_call_sync_sequence(clnt, server, &msg,
7439 						&args.seq_args, &res.seq_res);
7440 	nfs_free_fhandle(res.fh);
7441 	if (status)
7442 		return status;
7443 
7444 	do_renew_lease(clp, now);
7445 	return 0;
7446 }
7447 
7448 #ifdef CONFIG_NFS_V4_1
7449 
7450 /*
7451  * This operation also signals the server that this client is
7452  * performing "lease moved" recovery.  The server can stop asserting
7453  * SEQ4_STATUS_LEASE_MOVED for this client.  The client ID performing
7454  * this operation is identified in the SEQUENCE operation in this
7455  * compound.
7456  */
7457 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
7458 {
7459 	struct nfs_server *server = NFS_SERVER(inode);
7460 	struct rpc_clnt *clnt = server->client;
7461 	struct nfs4_fsid_present_arg args = {
7462 		.fh		= NFS_FH(inode),
7463 	};
7464 	struct nfs4_fsid_present_res res = {
7465 	};
7466 	struct rpc_message msg = {
7467 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7468 		.rpc_argp	= &args,
7469 		.rpc_resp	= &res,
7470 		.rpc_cred	= cred,
7471 	};
7472 	int status;
7473 
7474 	res.fh = nfs_alloc_fhandle();
7475 	if (res.fh == NULL)
7476 		return -ENOMEM;
7477 
7478 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7479 	status = nfs4_call_sync_sequence(clnt, server, &msg,
7480 						&args.seq_args, &res.seq_res);
7481 	nfs_free_fhandle(res.fh);
7482 	if (status == NFS4_OK &&
7483 	    res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7484 		status = -NFS4ERR_LEASE_MOVED;
7485 	return status;
7486 }
7487 
7488 #endif	/* CONFIG_NFS_V4_1 */
7489 
7490 /**
7491  * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7492  * @inode: inode on FSID to check
7493  * @cred: credential to use for this operation
7494  *
7495  * Server indicates whether the FSID is present, moved, or not
7496  * recognized.  This operation is necessary to clear a LEASE_MOVED
7497  * condition for this client ID.
7498  *
7499  * Returns NFS4_OK if the FSID is present on this server,
7500  * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7501  *  NFS4ERR code if some error occurred on the server, or a
7502  *  negative errno if a local failure occurred.
7503  */
7504 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
7505 {
7506 	struct nfs_server *server = NFS_SERVER(inode);
7507 	struct nfs_client *clp = server->nfs_client;
7508 	const struct nfs4_mig_recovery_ops *ops =
7509 					clp->cl_mvops->mig_recovery_ops;
7510 	struct nfs4_exception exception = { };
7511 	int status;
7512 
7513 	dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7514 		(unsigned long long)server->fsid.major,
7515 		(unsigned long long)server->fsid.minor,
7516 		clp->cl_hostname);
7517 	nfs_display_fhandle(NFS_FH(inode), __func__);
7518 
7519 	do {
7520 		status = ops->fsid_present(inode, cred);
7521 		if (status != -NFS4ERR_DELAY)
7522 			break;
7523 		nfs4_handle_exception(server, status, &exception);
7524 	} while (exception.retry);
7525 	return status;
7526 }
7527 
7528 /*
7529  * If 'use_integrity' is true and the state managment nfs_client
7530  * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7531  * and the machine credential as per RFC3530bis and RFC5661 Security
7532  * Considerations sections. Otherwise, just use the user cred with the
7533  * filesystem's rpc_client.
7534  */
7535 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7536 {
7537 	int status;
7538 	struct nfs4_secinfo_arg args = {
7539 		.dir_fh = NFS_FH(dir),
7540 		.name   = name,
7541 	};
7542 	struct nfs4_secinfo_res res = {
7543 		.flavors     = flavors,
7544 	};
7545 	struct rpc_message msg = {
7546 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7547 		.rpc_argp = &args,
7548 		.rpc_resp = &res,
7549 	};
7550 	struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7551 	const struct cred *cred = NULL;
7552 
7553 	if (use_integrity) {
7554 		clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7555 		cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7556 		msg.rpc_cred = cred;
7557 	}
7558 
7559 	dprintk("NFS call  secinfo %s\n", name->name);
7560 
7561 	nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7562 		NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7563 
7564 	status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7565 				&res.seq_res, 0);
7566 	dprintk("NFS reply  secinfo: %d\n", status);
7567 
7568 	put_cred(cred);
7569 
7570 	return status;
7571 }
7572 
7573 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7574 		      struct nfs4_secinfo_flavors *flavors)
7575 {
7576 	struct nfs4_exception exception = { };
7577 	int err;
7578 	do {
7579 		err = -NFS4ERR_WRONGSEC;
7580 
7581 		/* try to use integrity protection with machine cred */
7582 		if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7583 			err = _nfs4_proc_secinfo(dir, name, flavors, true);
7584 
7585 		/*
7586 		 * if unable to use integrity protection, or SECINFO with
7587 		 * integrity protection returns NFS4ERR_WRONGSEC (which is
7588 		 * disallowed by spec, but exists in deployed servers) use
7589 		 * the current filesystem's rpc_client and the user cred.
7590 		 */
7591 		if (err == -NFS4ERR_WRONGSEC)
7592 			err = _nfs4_proc_secinfo(dir, name, flavors, false);
7593 
7594 		trace_nfs4_secinfo(dir, name, err);
7595 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
7596 				&exception);
7597 	} while (exception.retry);
7598 	return err;
7599 }
7600 
7601 #ifdef CONFIG_NFS_V4_1
7602 /*
7603  * Check the exchange flags returned by the server for invalid flags, having
7604  * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7605  * DS flags set.
7606  */
7607 static int nfs4_check_cl_exchange_flags(u32 flags)
7608 {
7609 	if (flags & ~EXCHGID4_FLAG_MASK_R)
7610 		goto out_inval;
7611 	if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7612 	    (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7613 		goto out_inval;
7614 	if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7615 		goto out_inval;
7616 	return NFS_OK;
7617 out_inval:
7618 	return -NFS4ERR_INVAL;
7619 }
7620 
7621 static bool
7622 nfs41_same_server_scope(struct nfs41_server_scope *a,
7623 			struct nfs41_server_scope *b)
7624 {
7625 	if (a->server_scope_sz != b->server_scope_sz)
7626 		return false;
7627 	return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7628 }
7629 
7630 static void
7631 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7632 {
7633 }
7634 
7635 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7636 	.rpc_call_done =  &nfs4_bind_one_conn_to_session_done,
7637 };
7638 
7639 /*
7640  * nfs4_proc_bind_one_conn_to_session()
7641  *
7642  * The 4.1 client currently uses the same TCP connection for the
7643  * fore and backchannel.
7644  */
7645 static
7646 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7647 		struct rpc_xprt *xprt,
7648 		struct nfs_client *clp,
7649 		const struct cred *cred)
7650 {
7651 	int status;
7652 	struct nfs41_bind_conn_to_session_args args = {
7653 		.client = clp,
7654 		.dir = NFS4_CDFC4_FORE_OR_BOTH,
7655 	};
7656 	struct nfs41_bind_conn_to_session_res res;
7657 	struct rpc_message msg = {
7658 		.rpc_proc =
7659 			&nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7660 		.rpc_argp = &args,
7661 		.rpc_resp = &res,
7662 		.rpc_cred = cred,
7663 	};
7664 	struct rpc_task_setup task_setup_data = {
7665 		.rpc_client = clnt,
7666 		.rpc_xprt = xprt,
7667 		.callback_ops = &nfs4_bind_one_conn_to_session_ops,
7668 		.rpc_message = &msg,
7669 		.flags = RPC_TASK_TIMEOUT,
7670 	};
7671 	struct rpc_task *task;
7672 
7673 	nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7674 	if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7675 		args.dir = NFS4_CDFC4_FORE;
7676 
7677 	/* Do not set the backchannel flag unless this is clnt->cl_xprt */
7678 	if (xprt != rcu_access_pointer(clnt->cl_xprt))
7679 		args.dir = NFS4_CDFC4_FORE;
7680 
7681 	task = rpc_run_task(&task_setup_data);
7682 	if (!IS_ERR(task)) {
7683 		status = task->tk_status;
7684 		rpc_put_task(task);
7685 	} else
7686 		status = PTR_ERR(task);
7687 	trace_nfs4_bind_conn_to_session(clp, status);
7688 	if (status == 0) {
7689 		if (memcmp(res.sessionid.data,
7690 		    clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7691 			dprintk("NFS: %s: Session ID mismatch\n", __func__);
7692 			return -EIO;
7693 		}
7694 		if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7695 			dprintk("NFS: %s: Unexpected direction from server\n",
7696 				__func__);
7697 			return -EIO;
7698 		}
7699 		if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7700 			dprintk("NFS: %s: Server returned RDMA mode = true\n",
7701 				__func__);
7702 			return -EIO;
7703 		}
7704 	}
7705 
7706 	return status;
7707 }
7708 
7709 struct rpc_bind_conn_calldata {
7710 	struct nfs_client *clp;
7711 	const struct cred *cred;
7712 };
7713 
7714 static int
7715 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7716 		struct rpc_xprt *xprt,
7717 		void *calldata)
7718 {
7719 	struct rpc_bind_conn_calldata *p = calldata;
7720 
7721 	return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7722 }
7723 
7724 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
7725 {
7726 	struct rpc_bind_conn_calldata data = {
7727 		.clp = clp,
7728 		.cred = cred,
7729 	};
7730 	return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7731 			nfs4_proc_bind_conn_to_session_callback, &data);
7732 }
7733 
7734 /*
7735  * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7736  * and operations we'd like to see to enable certain features in the allow map
7737  */
7738 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7739 	.how = SP4_MACH_CRED,
7740 	.enforce.u.words = {
7741 		[1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7742 		      1 << (OP_EXCHANGE_ID - 32) |
7743 		      1 << (OP_CREATE_SESSION - 32) |
7744 		      1 << (OP_DESTROY_SESSION - 32) |
7745 		      1 << (OP_DESTROY_CLIENTID - 32)
7746 	},
7747 	.allow.u.words = {
7748 		[0] = 1 << (OP_CLOSE) |
7749 		      1 << (OP_OPEN_DOWNGRADE) |
7750 		      1 << (OP_LOCKU) |
7751 		      1 << (OP_DELEGRETURN) |
7752 		      1 << (OP_COMMIT),
7753 		[1] = 1 << (OP_SECINFO - 32) |
7754 		      1 << (OP_SECINFO_NO_NAME - 32) |
7755 		      1 << (OP_LAYOUTRETURN - 32) |
7756 		      1 << (OP_TEST_STATEID - 32) |
7757 		      1 << (OP_FREE_STATEID - 32) |
7758 		      1 << (OP_WRITE - 32)
7759 	}
7760 };
7761 
7762 /*
7763  * Select the state protection mode for client `clp' given the server results
7764  * from exchange_id in `sp'.
7765  *
7766  * Returns 0 on success, negative errno otherwise.
7767  */
7768 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7769 				 struct nfs41_state_protection *sp)
7770 {
7771 	static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7772 		[1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7773 		      1 << (OP_EXCHANGE_ID - 32) |
7774 		      1 << (OP_CREATE_SESSION - 32) |
7775 		      1 << (OP_DESTROY_SESSION - 32) |
7776 		      1 << (OP_DESTROY_CLIENTID - 32)
7777 	};
7778 	unsigned long flags = 0;
7779 	unsigned int i;
7780 	int ret = 0;
7781 
7782 	if (sp->how == SP4_MACH_CRED) {
7783 		/* Print state protect result */
7784 		dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7785 		for (i = 0; i <= LAST_NFS4_OP; i++) {
7786 			if (test_bit(i, sp->enforce.u.longs))
7787 				dfprintk(MOUNT, "  enforce op %d\n", i);
7788 			if (test_bit(i, sp->allow.u.longs))
7789 				dfprintk(MOUNT, "  allow op %d\n", i);
7790 		}
7791 
7792 		/* make sure nothing is on enforce list that isn't supported */
7793 		for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7794 			if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7795 				dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7796 				ret = -EINVAL;
7797 				goto out;
7798 			}
7799 		}
7800 
7801 		/*
7802 		 * Minimal mode - state operations are allowed to use machine
7803 		 * credential.  Note this already happens by default, so the
7804 		 * client doesn't have to do anything more than the negotiation.
7805 		 *
7806 		 * NOTE: we don't care if EXCHANGE_ID is in the list -
7807 		 *       we're already using the machine cred for exchange_id
7808 		 *       and will never use a different cred.
7809 		 */
7810 		if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7811 		    test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7812 		    test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7813 		    test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7814 			dfprintk(MOUNT, "sp4_mach_cred:\n");
7815 			dfprintk(MOUNT, "  minimal mode enabled\n");
7816 			__set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
7817 		} else {
7818 			dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7819 			ret = -EINVAL;
7820 			goto out;
7821 		}
7822 
7823 		if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7824 		    test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7825 		    test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7826 		    test_bit(OP_LOCKU, sp->allow.u.longs)) {
7827 			dfprintk(MOUNT, "  cleanup mode enabled\n");
7828 			__set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
7829 		}
7830 
7831 		if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7832 			dfprintk(MOUNT, "  pnfs cleanup mode enabled\n");
7833 			__set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
7834 		}
7835 
7836 		if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7837 		    test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7838 			dfprintk(MOUNT, "  secinfo mode enabled\n");
7839 			__set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
7840 		}
7841 
7842 		if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7843 		    test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7844 			dfprintk(MOUNT, "  stateid mode enabled\n");
7845 			__set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
7846 		}
7847 
7848 		if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7849 			dfprintk(MOUNT, "  write mode enabled\n");
7850 			__set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
7851 		}
7852 
7853 		if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7854 			dfprintk(MOUNT, "  commit mode enabled\n");
7855 			__set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
7856 		}
7857 	}
7858 out:
7859 	clp->cl_sp4_flags = flags;
7860 	return ret;
7861 }
7862 
7863 struct nfs41_exchange_id_data {
7864 	struct nfs41_exchange_id_res res;
7865 	struct nfs41_exchange_id_args args;
7866 };
7867 
7868 static void nfs4_exchange_id_release(void *data)
7869 {
7870 	struct nfs41_exchange_id_data *cdata =
7871 					(struct nfs41_exchange_id_data *)data;
7872 
7873 	nfs_put_client(cdata->args.client);
7874 	kfree(cdata->res.impl_id);
7875 	kfree(cdata->res.server_scope);
7876 	kfree(cdata->res.server_owner);
7877 	kfree(cdata);
7878 }
7879 
7880 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7881 	.rpc_release = nfs4_exchange_id_release,
7882 };
7883 
7884 /*
7885  * _nfs4_proc_exchange_id()
7886  *
7887  * Wrapper for EXCHANGE_ID operation.
7888  */
7889 static struct rpc_task *
7890 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
7891 			u32 sp4_how, struct rpc_xprt *xprt)
7892 {
7893 	struct rpc_message msg = {
7894 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7895 		.rpc_cred = cred,
7896 	};
7897 	struct rpc_task_setup task_setup_data = {
7898 		.rpc_client = clp->cl_rpcclient,
7899 		.callback_ops = &nfs4_exchange_id_call_ops,
7900 		.rpc_message = &msg,
7901 		.flags = RPC_TASK_TIMEOUT,
7902 	};
7903 	struct nfs41_exchange_id_data *calldata;
7904 	int status;
7905 
7906 	if (!refcount_inc_not_zero(&clp->cl_count))
7907 		return ERR_PTR(-EIO);
7908 
7909 	status = -ENOMEM;
7910 	calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7911 	if (!calldata)
7912 		goto out;
7913 
7914 	nfs4_init_boot_verifier(clp, &calldata->args.verifier);
7915 
7916 	status = nfs4_init_uniform_client_string(clp);
7917 	if (status)
7918 		goto out_calldata;
7919 
7920 	calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7921 						GFP_NOFS);
7922 	status = -ENOMEM;
7923 	if (unlikely(calldata->res.server_owner == NULL))
7924 		goto out_calldata;
7925 
7926 	calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7927 					GFP_NOFS);
7928 	if (unlikely(calldata->res.server_scope == NULL))
7929 		goto out_server_owner;
7930 
7931 	calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7932 	if (unlikely(calldata->res.impl_id == NULL))
7933 		goto out_server_scope;
7934 
7935 	switch (sp4_how) {
7936 	case SP4_NONE:
7937 		calldata->args.state_protect.how = SP4_NONE;
7938 		break;
7939 
7940 	case SP4_MACH_CRED:
7941 		calldata->args.state_protect = nfs4_sp4_mach_cred_request;
7942 		break;
7943 
7944 	default:
7945 		/* unsupported! */
7946 		WARN_ON_ONCE(1);
7947 		status = -EINVAL;
7948 		goto out_impl_id;
7949 	}
7950 	if (xprt) {
7951 		task_setup_data.rpc_xprt = xprt;
7952 		task_setup_data.flags |= RPC_TASK_SOFTCONN;
7953 		memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7954 				sizeof(calldata->args.verifier.data));
7955 	}
7956 	calldata->args.client = clp;
7957 	calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7958 	EXCHGID4_FLAG_BIND_PRINC_STATEID;
7959 #ifdef CONFIG_NFS_V4_1_MIGRATION
7960 	calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
7961 #endif
7962 	msg.rpc_argp = &calldata->args;
7963 	msg.rpc_resp = &calldata->res;
7964 	task_setup_data.callback_data = calldata;
7965 
7966 	return rpc_run_task(&task_setup_data);
7967 
7968 out_impl_id:
7969 	kfree(calldata->res.impl_id);
7970 out_server_scope:
7971 	kfree(calldata->res.server_scope);
7972 out_server_owner:
7973 	kfree(calldata->res.server_owner);
7974 out_calldata:
7975 	kfree(calldata);
7976 out:
7977 	nfs_put_client(clp);
7978 	return ERR_PTR(status);
7979 }
7980 
7981 /*
7982  * _nfs4_proc_exchange_id()
7983  *
7984  * Wrapper for EXCHANGE_ID operation.
7985  */
7986 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
7987 			u32 sp4_how)
7988 {
7989 	struct rpc_task *task;
7990 	struct nfs41_exchange_id_args *argp;
7991 	struct nfs41_exchange_id_res *resp;
7992 	int status;
7993 
7994 	task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7995 	if (IS_ERR(task))
7996 		return PTR_ERR(task);
7997 
7998 	argp = task->tk_msg.rpc_argp;
7999 	resp = task->tk_msg.rpc_resp;
8000 	status = task->tk_status;
8001 	if (status  != 0)
8002 		goto out;
8003 
8004 	status = nfs4_check_cl_exchange_flags(resp->flags);
8005 	if (status  != 0)
8006 		goto out;
8007 
8008 	status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8009 	if (status != 0)
8010 		goto out;
8011 
8012 	clp->cl_clientid = resp->clientid;
8013 	clp->cl_exchange_flags = resp->flags;
8014 	clp->cl_seqid = resp->seqid;
8015 	/* Client ID is not confirmed */
8016 	if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8017 		clear_bit(NFS4_SESSION_ESTABLISHED,
8018 			  &clp->cl_session->session_state);
8019 
8020 	if (clp->cl_serverscope != NULL &&
8021 	    !nfs41_same_server_scope(clp->cl_serverscope,
8022 				resp->server_scope)) {
8023 		dprintk("%s: server_scope mismatch detected\n",
8024 			__func__);
8025 		set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8026 	}
8027 
8028 	swap(clp->cl_serverowner, resp->server_owner);
8029 	swap(clp->cl_serverscope, resp->server_scope);
8030 	swap(clp->cl_implid, resp->impl_id);
8031 
8032 	/* Save the EXCHANGE_ID verifier session trunk tests */
8033 	memcpy(clp->cl_confirm.data, argp->verifier.data,
8034 	       sizeof(clp->cl_confirm.data));
8035 out:
8036 	trace_nfs4_exchange_id(clp, status);
8037 	rpc_put_task(task);
8038 	return status;
8039 }
8040 
8041 /*
8042  * nfs4_proc_exchange_id()
8043  *
8044  * Returns zero, a negative errno, or a negative NFS4ERR status code.
8045  *
8046  * Since the clientid has expired, all compounds using sessions
8047  * associated with the stale clientid will be returning
8048  * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8049  * be in some phase of session reset.
8050  *
8051  * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8052  */
8053 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8054 {
8055 	rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8056 	int status;
8057 
8058 	/* try SP4_MACH_CRED if krb5i/p	*/
8059 	if (authflavor == RPC_AUTH_GSS_KRB5I ||
8060 	    authflavor == RPC_AUTH_GSS_KRB5P) {
8061 		status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8062 		if (!status)
8063 			return 0;
8064 	}
8065 
8066 	/* try SP4_NONE */
8067 	return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8068 }
8069 
8070 /**
8071  * nfs4_test_session_trunk
8072  *
8073  * This is an add_xprt_test() test function called from
8074  * rpc_clnt_setup_test_and_add_xprt.
8075  *
8076  * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8077  * and is dereferrenced in nfs4_exchange_id_release
8078  *
8079  * Upon success, add the new transport to the rpc_clnt
8080  *
8081  * @clnt: struct rpc_clnt to get new transport
8082  * @xprt: the rpc_xprt to test
8083  * @data: call data for _nfs4_proc_exchange_id.
8084  */
8085 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8086 			    void *data)
8087 {
8088 	struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8089 	struct rpc_task *task;
8090 	int status;
8091 
8092 	u32 sp4_how;
8093 
8094 	dprintk("--> %s try %s\n", __func__,
8095 		xprt->address_strings[RPC_DISPLAY_ADDR]);
8096 
8097 	sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8098 
8099 	/* Test connection for session trunking. Async exchange_id call */
8100 	task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8101 	if (IS_ERR(task))
8102 		return;
8103 
8104 	status = task->tk_status;
8105 	if (status == 0)
8106 		status = nfs4_detect_session_trunking(adata->clp,
8107 				task->tk_msg.rpc_resp, xprt);
8108 
8109 	if (status == 0)
8110 		rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8111 
8112 	rpc_put_task(task);
8113 }
8114 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8115 
8116 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8117 		const struct cred *cred)
8118 {
8119 	struct rpc_message msg = {
8120 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8121 		.rpc_argp = clp,
8122 		.rpc_cred = cred,
8123 	};
8124 	int status;
8125 
8126 	status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8127 	trace_nfs4_destroy_clientid(clp, status);
8128 	if (status)
8129 		dprintk("NFS: Got error %d from the server %s on "
8130 			"DESTROY_CLIENTID.", status, clp->cl_hostname);
8131 	return status;
8132 }
8133 
8134 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8135 		const struct cred *cred)
8136 {
8137 	unsigned int loop;
8138 	int ret;
8139 
8140 	for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8141 		ret = _nfs4_proc_destroy_clientid(clp, cred);
8142 		switch (ret) {
8143 		case -NFS4ERR_DELAY:
8144 		case -NFS4ERR_CLIENTID_BUSY:
8145 			ssleep(1);
8146 			break;
8147 		default:
8148 			return ret;
8149 		}
8150 	}
8151 	return 0;
8152 }
8153 
8154 int nfs4_destroy_clientid(struct nfs_client *clp)
8155 {
8156 	const struct cred *cred;
8157 	int ret = 0;
8158 
8159 	if (clp->cl_mvops->minor_version < 1)
8160 		goto out;
8161 	if (clp->cl_exchange_flags == 0)
8162 		goto out;
8163 	if (clp->cl_preserve_clid)
8164 		goto out;
8165 	cred = nfs4_get_clid_cred(clp);
8166 	ret = nfs4_proc_destroy_clientid(clp, cred);
8167 	put_cred(cred);
8168 	switch (ret) {
8169 	case 0:
8170 	case -NFS4ERR_STALE_CLIENTID:
8171 		clp->cl_exchange_flags = 0;
8172 	}
8173 out:
8174 	return ret;
8175 }
8176 
8177 struct nfs4_get_lease_time_data {
8178 	struct nfs4_get_lease_time_args *args;
8179 	struct nfs4_get_lease_time_res *res;
8180 	struct nfs_client *clp;
8181 };
8182 
8183 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8184 					void *calldata)
8185 {
8186 	struct nfs4_get_lease_time_data *data =
8187 			(struct nfs4_get_lease_time_data *)calldata;
8188 
8189 	dprintk("--> %s\n", __func__);
8190 	/* just setup sequence, do not trigger session recovery
8191 	   since we're invoked within one */
8192 	nfs4_setup_sequence(data->clp,
8193 			&data->args->la_seq_args,
8194 			&data->res->lr_seq_res,
8195 			task);
8196 	dprintk("<-- %s\n", __func__);
8197 }
8198 
8199 /*
8200  * Called from nfs4_state_manager thread for session setup, so don't recover
8201  * from sequence operation or clientid errors.
8202  */
8203 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8204 {
8205 	struct nfs4_get_lease_time_data *data =
8206 			(struct nfs4_get_lease_time_data *)calldata;
8207 
8208 	dprintk("--> %s\n", __func__);
8209 	if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
8210 		return;
8211 	switch (task->tk_status) {
8212 	case -NFS4ERR_DELAY:
8213 	case -NFS4ERR_GRACE:
8214 		dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8215 		rpc_delay(task, NFS4_POLL_RETRY_MIN);
8216 		task->tk_status = 0;
8217 		/* fall through */
8218 	case -NFS4ERR_RETRY_UNCACHED_REP:
8219 		rpc_restart_call_prepare(task);
8220 		return;
8221 	}
8222 	dprintk("<-- %s\n", __func__);
8223 }
8224 
8225 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8226 	.rpc_call_prepare = nfs4_get_lease_time_prepare,
8227 	.rpc_call_done = nfs4_get_lease_time_done,
8228 };
8229 
8230 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8231 {
8232 	struct rpc_task *task;
8233 	struct nfs4_get_lease_time_args args;
8234 	struct nfs4_get_lease_time_res res = {
8235 		.lr_fsinfo = fsinfo,
8236 	};
8237 	struct nfs4_get_lease_time_data data = {
8238 		.args = &args,
8239 		.res = &res,
8240 		.clp = clp,
8241 	};
8242 	struct rpc_message msg = {
8243 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8244 		.rpc_argp = &args,
8245 		.rpc_resp = &res,
8246 	};
8247 	struct rpc_task_setup task_setup = {
8248 		.rpc_client = clp->cl_rpcclient,
8249 		.rpc_message = &msg,
8250 		.callback_ops = &nfs4_get_lease_time_ops,
8251 		.callback_data = &data,
8252 		.flags = RPC_TASK_TIMEOUT,
8253 	};
8254 	int status;
8255 
8256 	nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8257 	task = rpc_run_task(&task_setup);
8258 
8259 	if (IS_ERR(task))
8260 		return PTR_ERR(task);
8261 
8262 	status = task->tk_status;
8263 	rpc_put_task(task);
8264 	return status;
8265 }
8266 
8267 /*
8268  * Initialize the values to be used by the client in CREATE_SESSION
8269  * If nfs4_init_session set the fore channel request and response sizes,
8270  * use them.
8271  *
8272  * Set the back channel max_resp_sz_cached to zero to force the client to
8273  * always set csa_cachethis to FALSE because the current implementation
8274  * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8275  */
8276 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8277 				    struct rpc_clnt *clnt)
8278 {
8279 	unsigned int max_rqst_sz, max_resp_sz;
8280 	unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8281 
8282 	max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8283 	max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8284 
8285 	/* Fore channel attributes */
8286 	args->fc_attrs.max_rqst_sz = max_rqst_sz;
8287 	args->fc_attrs.max_resp_sz = max_resp_sz;
8288 	args->fc_attrs.max_ops = NFS4_MAX_OPS;
8289 	args->fc_attrs.max_reqs = max_session_slots;
8290 
8291 	dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8292 		"max_ops=%u max_reqs=%u\n",
8293 		__func__,
8294 		args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8295 		args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8296 
8297 	/* Back channel attributes */
8298 	args->bc_attrs.max_rqst_sz = max_bc_payload;
8299 	args->bc_attrs.max_resp_sz = max_bc_payload;
8300 	args->bc_attrs.max_resp_sz_cached = 0;
8301 	args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8302 	args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8303 
8304 	dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8305 		"max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8306 		__func__,
8307 		args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8308 		args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8309 		args->bc_attrs.max_reqs);
8310 }
8311 
8312 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8313 		struct nfs41_create_session_res *res)
8314 {
8315 	struct nfs4_channel_attrs *sent = &args->fc_attrs;
8316 	struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8317 
8318 	if (rcvd->max_resp_sz > sent->max_resp_sz)
8319 		return -EINVAL;
8320 	/*
8321 	 * Our requested max_ops is the minimum we need; we're not
8322 	 * prepared to break up compounds into smaller pieces than that.
8323 	 * So, no point even trying to continue if the server won't
8324 	 * cooperate:
8325 	 */
8326 	if (rcvd->max_ops < sent->max_ops)
8327 		return -EINVAL;
8328 	if (rcvd->max_reqs == 0)
8329 		return -EINVAL;
8330 	if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8331 		rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8332 	return 0;
8333 }
8334 
8335 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8336 		struct nfs41_create_session_res *res)
8337 {
8338 	struct nfs4_channel_attrs *sent = &args->bc_attrs;
8339 	struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8340 
8341 	if (!(res->flags & SESSION4_BACK_CHAN))
8342 		goto out;
8343 	if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8344 		return -EINVAL;
8345 	if (rcvd->max_resp_sz < sent->max_resp_sz)
8346 		return -EINVAL;
8347 	if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8348 		return -EINVAL;
8349 	if (rcvd->max_ops > sent->max_ops)
8350 		return -EINVAL;
8351 	if (rcvd->max_reqs > sent->max_reqs)
8352 		return -EINVAL;
8353 out:
8354 	return 0;
8355 }
8356 
8357 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8358 				     struct nfs41_create_session_res *res)
8359 {
8360 	int ret;
8361 
8362 	ret = nfs4_verify_fore_channel_attrs(args, res);
8363 	if (ret)
8364 		return ret;
8365 	return nfs4_verify_back_channel_attrs(args, res);
8366 }
8367 
8368 static void nfs4_update_session(struct nfs4_session *session,
8369 		struct nfs41_create_session_res *res)
8370 {
8371 	nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8372 	/* Mark client id and session as being confirmed */
8373 	session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8374 	set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8375 	session->flags = res->flags;
8376 	memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8377 	if (res->flags & SESSION4_BACK_CHAN)
8378 		memcpy(&session->bc_attrs, &res->bc_attrs,
8379 				sizeof(session->bc_attrs));
8380 }
8381 
8382 static int _nfs4_proc_create_session(struct nfs_client *clp,
8383 		const struct cred *cred)
8384 {
8385 	struct nfs4_session *session = clp->cl_session;
8386 	struct nfs41_create_session_args args = {
8387 		.client = clp,
8388 		.clientid = clp->cl_clientid,
8389 		.seqid = clp->cl_seqid,
8390 		.cb_program = NFS4_CALLBACK,
8391 	};
8392 	struct nfs41_create_session_res res;
8393 
8394 	struct rpc_message msg = {
8395 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8396 		.rpc_argp = &args,
8397 		.rpc_resp = &res,
8398 		.rpc_cred = cred,
8399 	};
8400 	int status;
8401 
8402 	nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8403 	args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8404 
8405 	status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8406 	trace_nfs4_create_session(clp, status);
8407 
8408 	switch (status) {
8409 	case -NFS4ERR_STALE_CLIENTID:
8410 	case -NFS4ERR_DELAY:
8411 	case -ETIMEDOUT:
8412 	case -EACCES:
8413 	case -EAGAIN:
8414 		goto out;
8415 	};
8416 
8417 	clp->cl_seqid++;
8418 	if (!status) {
8419 		/* Verify the session's negotiated channel_attrs values */
8420 		status = nfs4_verify_channel_attrs(&args, &res);
8421 		/* Increment the clientid slot sequence id */
8422 		if (status)
8423 			goto out;
8424 		nfs4_update_session(session, &res);
8425 	}
8426 out:
8427 	return status;
8428 }
8429 
8430 /*
8431  * Issues a CREATE_SESSION operation to the server.
8432  * It is the responsibility of the caller to verify the session is
8433  * expired before calling this routine.
8434  */
8435 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
8436 {
8437 	int status;
8438 	unsigned *ptr;
8439 	struct nfs4_session *session = clp->cl_session;
8440 
8441 	dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8442 
8443 	status = _nfs4_proc_create_session(clp, cred);
8444 	if (status)
8445 		goto out;
8446 
8447 	/* Init or reset the session slot tables */
8448 	status = nfs4_setup_session_slot_tables(session);
8449 	dprintk("slot table setup returned %d\n", status);
8450 	if (status)
8451 		goto out;
8452 
8453 	ptr = (unsigned *)&session->sess_id.data[0];
8454 	dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8455 		clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8456 out:
8457 	dprintk("<-- %s\n", __func__);
8458 	return status;
8459 }
8460 
8461 /*
8462  * Issue the over-the-wire RPC DESTROY_SESSION.
8463  * The caller must serialize access to this routine.
8464  */
8465 int nfs4_proc_destroy_session(struct nfs4_session *session,
8466 		const struct cred *cred)
8467 {
8468 	struct rpc_message msg = {
8469 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8470 		.rpc_argp = session,
8471 		.rpc_cred = cred,
8472 	};
8473 	int status = 0;
8474 
8475 	dprintk("--> nfs4_proc_destroy_session\n");
8476 
8477 	/* session is still being setup */
8478 	if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8479 		return 0;
8480 
8481 	status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8482 	trace_nfs4_destroy_session(session->clp, status);
8483 
8484 	if (status)
8485 		dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8486 			"Session has been destroyed regardless...\n", status);
8487 
8488 	dprintk("<-- nfs4_proc_destroy_session\n");
8489 	return status;
8490 }
8491 
8492 /*
8493  * Renew the cl_session lease.
8494  */
8495 struct nfs4_sequence_data {
8496 	struct nfs_client *clp;
8497 	struct nfs4_sequence_args args;
8498 	struct nfs4_sequence_res res;
8499 };
8500 
8501 static void nfs41_sequence_release(void *data)
8502 {
8503 	struct nfs4_sequence_data *calldata = data;
8504 	struct nfs_client *clp = calldata->clp;
8505 
8506 	if (refcount_read(&clp->cl_count) > 1)
8507 		nfs4_schedule_state_renewal(clp);
8508 	nfs_put_client(clp);
8509 	kfree(calldata);
8510 }
8511 
8512 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8513 {
8514 	switch(task->tk_status) {
8515 	case -NFS4ERR_DELAY:
8516 		rpc_delay(task, NFS4_POLL_RETRY_MAX);
8517 		return -EAGAIN;
8518 	default:
8519 		nfs4_schedule_lease_recovery(clp);
8520 	}
8521 	return 0;
8522 }
8523 
8524 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8525 {
8526 	struct nfs4_sequence_data *calldata = data;
8527 	struct nfs_client *clp = calldata->clp;
8528 
8529 	if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8530 		return;
8531 
8532 	trace_nfs4_sequence(clp, task->tk_status);
8533 	if (task->tk_status < 0) {
8534 		dprintk("%s ERROR %d\n", __func__, task->tk_status);
8535 		if (refcount_read(&clp->cl_count) == 1)
8536 			goto out;
8537 
8538 		if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8539 			rpc_restart_call_prepare(task);
8540 			return;
8541 		}
8542 	}
8543 	dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8544 out:
8545 	dprintk("<-- %s\n", __func__);
8546 }
8547 
8548 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8549 {
8550 	struct nfs4_sequence_data *calldata = data;
8551 	struct nfs_client *clp = calldata->clp;
8552 	struct nfs4_sequence_args *args;
8553 	struct nfs4_sequence_res *res;
8554 
8555 	args = task->tk_msg.rpc_argp;
8556 	res = task->tk_msg.rpc_resp;
8557 
8558 	nfs4_setup_sequence(clp, args, res, task);
8559 }
8560 
8561 static const struct rpc_call_ops nfs41_sequence_ops = {
8562 	.rpc_call_done = nfs41_sequence_call_done,
8563 	.rpc_call_prepare = nfs41_sequence_prepare,
8564 	.rpc_release = nfs41_sequence_release,
8565 };
8566 
8567 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8568 		const struct cred *cred,
8569 		struct nfs4_slot *slot,
8570 		bool is_privileged)
8571 {
8572 	struct nfs4_sequence_data *calldata;
8573 	struct rpc_message msg = {
8574 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8575 		.rpc_cred = cred,
8576 	};
8577 	struct rpc_task_setup task_setup_data = {
8578 		.rpc_client = clp->cl_rpcclient,
8579 		.rpc_message = &msg,
8580 		.callback_ops = &nfs41_sequence_ops,
8581 		.flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8582 	};
8583 	struct rpc_task *ret;
8584 
8585 	ret = ERR_PTR(-EIO);
8586 	if (!refcount_inc_not_zero(&clp->cl_count))
8587 		goto out_err;
8588 
8589 	ret = ERR_PTR(-ENOMEM);
8590 	calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8591 	if (calldata == NULL)
8592 		goto out_put_clp;
8593 	nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
8594 	nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
8595 	msg.rpc_argp = &calldata->args;
8596 	msg.rpc_resp = &calldata->res;
8597 	calldata->clp = clp;
8598 	task_setup_data.callback_data = calldata;
8599 
8600 	ret = rpc_run_task(&task_setup_data);
8601 	if (IS_ERR(ret))
8602 		goto out_err;
8603 	return ret;
8604 out_put_clp:
8605 	nfs_put_client(clp);
8606 out_err:
8607 	nfs41_release_slot(slot);
8608 	return ret;
8609 }
8610 
8611 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
8612 {
8613 	struct rpc_task *task;
8614 	int ret = 0;
8615 
8616 	if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8617 		return -EAGAIN;
8618 	task = _nfs41_proc_sequence(clp, cred, NULL, false);
8619 	if (IS_ERR(task))
8620 		ret = PTR_ERR(task);
8621 	else
8622 		rpc_put_task_async(task);
8623 	dprintk("<-- %s status=%d\n", __func__, ret);
8624 	return ret;
8625 }
8626 
8627 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
8628 {
8629 	struct rpc_task *task;
8630 	int ret;
8631 
8632 	task = _nfs41_proc_sequence(clp, cred, NULL, true);
8633 	if (IS_ERR(task)) {
8634 		ret = PTR_ERR(task);
8635 		goto out;
8636 	}
8637 	ret = rpc_wait_for_completion_task(task);
8638 	if (!ret)
8639 		ret = task->tk_status;
8640 	rpc_put_task(task);
8641 out:
8642 	dprintk("<-- %s status=%d\n", __func__, ret);
8643 	return ret;
8644 }
8645 
8646 struct nfs4_reclaim_complete_data {
8647 	struct nfs_client *clp;
8648 	struct nfs41_reclaim_complete_args arg;
8649 	struct nfs41_reclaim_complete_res res;
8650 };
8651 
8652 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8653 {
8654 	struct nfs4_reclaim_complete_data *calldata = data;
8655 
8656 	nfs4_setup_sequence(calldata->clp,
8657 			&calldata->arg.seq_args,
8658 			&calldata->res.seq_res,
8659 			task);
8660 }
8661 
8662 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8663 {
8664 	switch(task->tk_status) {
8665 	case 0:
8666 		wake_up_all(&clp->cl_lock_waitq);
8667 		/* Fallthrough */
8668 	case -NFS4ERR_COMPLETE_ALREADY:
8669 	case -NFS4ERR_WRONG_CRED: /* What to do here? */
8670 		break;
8671 	case -NFS4ERR_DELAY:
8672 		rpc_delay(task, NFS4_POLL_RETRY_MAX);
8673 		/* fall through */
8674 	case -NFS4ERR_RETRY_UNCACHED_REP:
8675 		return -EAGAIN;
8676 	case -NFS4ERR_BADSESSION:
8677 	case -NFS4ERR_DEADSESSION:
8678 	case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8679 		nfs4_schedule_session_recovery(clp->cl_session,
8680 				task->tk_status);
8681 		break;
8682 	default:
8683 		nfs4_schedule_lease_recovery(clp);
8684 	}
8685 	return 0;
8686 }
8687 
8688 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8689 {
8690 	struct nfs4_reclaim_complete_data *calldata = data;
8691 	struct nfs_client *clp = calldata->clp;
8692 	struct nfs4_sequence_res *res = &calldata->res.seq_res;
8693 
8694 	dprintk("--> %s\n", __func__);
8695 	if (!nfs41_sequence_done(task, res))
8696 		return;
8697 
8698 	trace_nfs4_reclaim_complete(clp, task->tk_status);
8699 	if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8700 		rpc_restart_call_prepare(task);
8701 		return;
8702 	}
8703 	dprintk("<-- %s\n", __func__);
8704 }
8705 
8706 static void nfs4_free_reclaim_complete_data(void *data)
8707 {
8708 	struct nfs4_reclaim_complete_data *calldata = data;
8709 
8710 	kfree(calldata);
8711 }
8712 
8713 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8714 	.rpc_call_prepare = nfs4_reclaim_complete_prepare,
8715 	.rpc_call_done = nfs4_reclaim_complete_done,
8716 	.rpc_release = nfs4_free_reclaim_complete_data,
8717 };
8718 
8719 /*
8720  * Issue a global reclaim complete.
8721  */
8722 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8723 		const struct cred *cred)
8724 {
8725 	struct nfs4_reclaim_complete_data *calldata;
8726 	struct rpc_task *task;
8727 	struct rpc_message msg = {
8728 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8729 		.rpc_cred = cred,
8730 	};
8731 	struct rpc_task_setup task_setup_data = {
8732 		.rpc_client = clp->cl_rpcclient,
8733 		.rpc_message = &msg,
8734 		.callback_ops = &nfs4_reclaim_complete_call_ops,
8735 		.flags = RPC_TASK_ASYNC,
8736 	};
8737 	int status = -ENOMEM;
8738 
8739 	dprintk("--> %s\n", __func__);
8740 	calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8741 	if (calldata == NULL)
8742 		goto out;
8743 	calldata->clp = clp;
8744 	calldata->arg.one_fs = 0;
8745 
8746 	nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
8747 	msg.rpc_argp = &calldata->arg;
8748 	msg.rpc_resp = &calldata->res;
8749 	task_setup_data.callback_data = calldata;
8750 	task = rpc_run_task(&task_setup_data);
8751 	if (IS_ERR(task)) {
8752 		status = PTR_ERR(task);
8753 		goto out;
8754 	}
8755 	status = rpc_wait_for_completion_task(task);
8756 	if (status == 0)
8757 		status = task->tk_status;
8758 	rpc_put_task(task);
8759 out:
8760 	dprintk("<-- %s status=%d\n", __func__, status);
8761 	return status;
8762 }
8763 
8764 static void
8765 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8766 {
8767 	struct nfs4_layoutget *lgp = calldata;
8768 	struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8769 
8770 	dprintk("--> %s\n", __func__);
8771 	nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
8772 				&lgp->res.seq_res, task);
8773 	dprintk("<-- %s\n", __func__);
8774 }
8775 
8776 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8777 {
8778 	struct nfs4_layoutget *lgp = calldata;
8779 
8780 	dprintk("--> %s\n", __func__);
8781 	nfs41_sequence_process(task, &lgp->res.seq_res);
8782 	dprintk("<-- %s\n", __func__);
8783 }
8784 
8785 static int
8786 nfs4_layoutget_handle_exception(struct rpc_task *task,
8787 		struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8788 {
8789 	struct inode *inode = lgp->args.inode;
8790 	struct nfs_server *server = NFS_SERVER(inode);
8791 	struct pnfs_layout_hdr *lo;
8792 	int nfs4err = task->tk_status;
8793 	int err, status = 0;
8794 	LIST_HEAD(head);
8795 
8796 	dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8797 
8798 	nfs4_sequence_free_slot(&lgp->res.seq_res);
8799 
8800 	switch (nfs4err) {
8801 	case 0:
8802 		goto out;
8803 
8804 	/*
8805 	 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8806 	 * on the file. set tk_status to -ENODATA to tell upper layer to
8807 	 * retry go inband.
8808 	 */
8809 	case -NFS4ERR_LAYOUTUNAVAILABLE:
8810 		status = -ENODATA;
8811 		goto out;
8812 	/*
8813 	 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8814 	 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8815 	 */
8816 	case -NFS4ERR_BADLAYOUT:
8817 		status = -EOVERFLOW;
8818 		goto out;
8819 	/*
8820 	 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8821 	 * (or clients) writing to the same RAID stripe except when
8822 	 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8823 	 *
8824 	 * Treat it like we would RECALLCONFLICT -- we retry for a little
8825 	 * while, and then eventually give up.
8826 	 */
8827 	case -NFS4ERR_LAYOUTTRYLATER:
8828 		if (lgp->args.minlength == 0) {
8829 			status = -EOVERFLOW;
8830 			goto out;
8831 		}
8832 		status = -EBUSY;
8833 		break;
8834 	case -NFS4ERR_RECALLCONFLICT:
8835 		status = -ERECALLCONFLICT;
8836 		break;
8837 	case -NFS4ERR_DELEG_REVOKED:
8838 	case -NFS4ERR_ADMIN_REVOKED:
8839 	case -NFS4ERR_EXPIRED:
8840 	case -NFS4ERR_BAD_STATEID:
8841 		exception->timeout = 0;
8842 		spin_lock(&inode->i_lock);
8843 		lo = NFS_I(inode)->layout;
8844 		/* If the open stateid was bad, then recover it. */
8845 		if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8846 		    !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
8847 			spin_unlock(&inode->i_lock);
8848 			exception->state = lgp->args.ctx->state;
8849 			exception->stateid = &lgp->args.stateid;
8850 			break;
8851 		}
8852 
8853 		/*
8854 		 * Mark the bad layout state as invalid, then retry
8855 		 */
8856 		pnfs_mark_layout_stateid_invalid(lo, &head);
8857 		spin_unlock(&inode->i_lock);
8858 		nfs_commit_inode(inode, 0);
8859 		pnfs_free_lseg_list(&head);
8860 		status = -EAGAIN;
8861 		goto out;
8862 	}
8863 
8864 	err = nfs4_handle_exception(server, nfs4err, exception);
8865 	if (!status) {
8866 		if (exception->retry)
8867 			status = -EAGAIN;
8868 		else
8869 			status = err;
8870 	}
8871 out:
8872 	dprintk("<-- %s\n", __func__);
8873 	return status;
8874 }
8875 
8876 size_t max_response_pages(struct nfs_server *server)
8877 {
8878 	u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8879 	return nfs_page_array_len(0, max_resp_sz);
8880 }
8881 
8882 static void nfs4_layoutget_release(void *calldata)
8883 {
8884 	struct nfs4_layoutget *lgp = calldata;
8885 
8886 	dprintk("--> %s\n", __func__);
8887 	nfs4_sequence_free_slot(&lgp->res.seq_res);
8888 	pnfs_layoutget_free(lgp);
8889 	dprintk("<-- %s\n", __func__);
8890 }
8891 
8892 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8893 	.rpc_call_prepare = nfs4_layoutget_prepare,
8894 	.rpc_call_done = nfs4_layoutget_done,
8895 	.rpc_release = nfs4_layoutget_release,
8896 };
8897 
8898 struct pnfs_layout_segment *
8899 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
8900 {
8901 	struct inode *inode = lgp->args.inode;
8902 	struct nfs_server *server = NFS_SERVER(inode);
8903 	struct rpc_task *task;
8904 	struct rpc_message msg = {
8905 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8906 		.rpc_argp = &lgp->args,
8907 		.rpc_resp = &lgp->res,
8908 		.rpc_cred = lgp->cred,
8909 	};
8910 	struct rpc_task_setup task_setup_data = {
8911 		.rpc_client = server->client,
8912 		.rpc_message = &msg,
8913 		.callback_ops = &nfs4_layoutget_call_ops,
8914 		.callback_data = lgp,
8915 		.flags = RPC_TASK_ASYNC,
8916 	};
8917 	struct pnfs_layout_segment *lseg = NULL;
8918 	struct nfs4_exception exception = {
8919 		.inode = inode,
8920 		.timeout = *timeout,
8921 	};
8922 	int status = 0;
8923 
8924 	dprintk("--> %s\n", __func__);
8925 
8926 	/* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8927 	pnfs_get_layout_hdr(NFS_I(inode)->layout);
8928 
8929 	nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
8930 
8931 	task = rpc_run_task(&task_setup_data);
8932 	if (IS_ERR(task))
8933 		return ERR_CAST(task);
8934 	status = rpc_wait_for_completion_task(task);
8935 	if (status != 0)
8936 		goto out;
8937 
8938 	if (task->tk_status < 0) {
8939 		status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8940 		*timeout = exception.timeout;
8941 	} else if (lgp->res.layoutp->len == 0) {
8942 		status = -EAGAIN;
8943 		*timeout = nfs4_update_delay(&exception.timeout);
8944 	} else
8945 		lseg = pnfs_layout_process(lgp);
8946 out:
8947 	trace_nfs4_layoutget(lgp->args.ctx,
8948 			&lgp->args.range,
8949 			&lgp->res.range,
8950 			&lgp->res.stateid,
8951 			status);
8952 
8953 	rpc_put_task(task);
8954 	dprintk("<-- %s status=%d\n", __func__, status);
8955 	if (status)
8956 		return ERR_PTR(status);
8957 	return lseg;
8958 }
8959 
8960 static void
8961 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8962 {
8963 	struct nfs4_layoutreturn *lrp = calldata;
8964 
8965 	dprintk("--> %s\n", __func__);
8966 	nfs4_setup_sequence(lrp->clp,
8967 			&lrp->args.seq_args,
8968 			&lrp->res.seq_res,
8969 			task);
8970 	if (!pnfs_layout_is_valid(lrp->args.layout))
8971 		rpc_exit(task, 0);
8972 }
8973 
8974 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8975 {
8976 	struct nfs4_layoutreturn *lrp = calldata;
8977 	struct nfs_server *server;
8978 
8979 	dprintk("--> %s\n", __func__);
8980 
8981 	if (!nfs41_sequence_process(task, &lrp->res.seq_res))
8982 		return;
8983 
8984 	server = NFS_SERVER(lrp->args.inode);
8985 	switch (task->tk_status) {
8986 	case -NFS4ERR_OLD_STATEID:
8987 		if (nfs4_layoutreturn_refresh_stateid(&lrp->args.stateid,
8988 					&lrp->args.range,
8989 					lrp->args.inode))
8990 			goto out_restart;
8991 		/* Fallthrough */
8992 	default:
8993 		task->tk_status = 0;
8994 		/* Fallthrough */
8995 	case 0:
8996 		break;
8997 	case -NFS4ERR_DELAY:
8998 		if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
8999 			break;
9000 		goto out_restart;
9001 	}
9002 	dprintk("<-- %s\n", __func__);
9003 	return;
9004 out_restart:
9005 	task->tk_status = 0;
9006 	nfs4_sequence_free_slot(&lrp->res.seq_res);
9007 	rpc_restart_call_prepare(task);
9008 }
9009 
9010 static void nfs4_layoutreturn_release(void *calldata)
9011 {
9012 	struct nfs4_layoutreturn *lrp = calldata;
9013 	struct pnfs_layout_hdr *lo = lrp->args.layout;
9014 
9015 	dprintk("--> %s\n", __func__);
9016 	pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9017 			lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9018 	nfs4_sequence_free_slot(&lrp->res.seq_res);
9019 	if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9020 		lrp->ld_private.ops->free(&lrp->ld_private);
9021 	pnfs_put_layout_hdr(lrp->args.layout);
9022 	nfs_iput_and_deactive(lrp->inode);
9023 	kfree(calldata);
9024 	dprintk("<-- %s\n", __func__);
9025 }
9026 
9027 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9028 	.rpc_call_prepare = nfs4_layoutreturn_prepare,
9029 	.rpc_call_done = nfs4_layoutreturn_done,
9030 	.rpc_release = nfs4_layoutreturn_release,
9031 };
9032 
9033 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9034 {
9035 	struct rpc_task *task;
9036 	struct rpc_message msg = {
9037 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9038 		.rpc_argp = &lrp->args,
9039 		.rpc_resp = &lrp->res,
9040 		.rpc_cred = lrp->cred,
9041 	};
9042 	struct rpc_task_setup task_setup_data = {
9043 		.rpc_client = NFS_SERVER(lrp->args.inode)->client,
9044 		.rpc_message = &msg,
9045 		.callback_ops = &nfs4_layoutreturn_call_ops,
9046 		.callback_data = lrp,
9047 	};
9048 	int status = 0;
9049 
9050 	nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9051 			NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9052 			&task_setup_data.rpc_client, &msg);
9053 
9054 	dprintk("--> %s\n", __func__);
9055 	if (!sync) {
9056 		lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9057 		if (!lrp->inode) {
9058 			nfs4_layoutreturn_release(lrp);
9059 			return -EAGAIN;
9060 		}
9061 		task_setup_data.flags |= RPC_TASK_ASYNC;
9062 	}
9063 	nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
9064 	task = rpc_run_task(&task_setup_data);
9065 	if (IS_ERR(task))
9066 		return PTR_ERR(task);
9067 	if (sync)
9068 		status = task->tk_status;
9069 	trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9070 	dprintk("<-- %s status=%d\n", __func__, status);
9071 	rpc_put_task(task);
9072 	return status;
9073 }
9074 
9075 static int
9076 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9077 		struct pnfs_device *pdev,
9078 		const struct cred *cred)
9079 {
9080 	struct nfs4_getdeviceinfo_args args = {
9081 		.pdev = pdev,
9082 		.notify_types = NOTIFY_DEVICEID4_CHANGE |
9083 			NOTIFY_DEVICEID4_DELETE,
9084 	};
9085 	struct nfs4_getdeviceinfo_res res = {
9086 		.pdev = pdev,
9087 	};
9088 	struct rpc_message msg = {
9089 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9090 		.rpc_argp = &args,
9091 		.rpc_resp = &res,
9092 		.rpc_cred = cred,
9093 	};
9094 	int status;
9095 
9096 	dprintk("--> %s\n", __func__);
9097 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9098 	if (res.notification & ~args.notify_types)
9099 		dprintk("%s: unsupported notification\n", __func__);
9100 	if (res.notification != args.notify_types)
9101 		pdev->nocache = 1;
9102 
9103 	dprintk("<-- %s status=%d\n", __func__, status);
9104 
9105 	return status;
9106 }
9107 
9108 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9109 		struct pnfs_device *pdev,
9110 		const struct cred *cred)
9111 {
9112 	struct nfs4_exception exception = { };
9113 	int err;
9114 
9115 	do {
9116 		err = nfs4_handle_exception(server,
9117 					_nfs4_proc_getdeviceinfo(server, pdev, cred),
9118 					&exception);
9119 	} while (exception.retry);
9120 	return err;
9121 }
9122 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9123 
9124 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9125 {
9126 	struct nfs4_layoutcommit_data *data = calldata;
9127 	struct nfs_server *server = NFS_SERVER(data->args.inode);
9128 
9129 	nfs4_setup_sequence(server->nfs_client,
9130 			&data->args.seq_args,
9131 			&data->res.seq_res,
9132 			task);
9133 }
9134 
9135 static void
9136 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9137 {
9138 	struct nfs4_layoutcommit_data *data = calldata;
9139 	struct nfs_server *server = NFS_SERVER(data->args.inode);
9140 
9141 	if (!nfs41_sequence_done(task, &data->res.seq_res))
9142 		return;
9143 
9144 	switch (task->tk_status) { /* Just ignore these failures */
9145 	case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9146 	case -NFS4ERR_BADIOMODE:     /* no IOMODE_RW layout for range */
9147 	case -NFS4ERR_BADLAYOUT:     /* no layout */
9148 	case -NFS4ERR_GRACE:	    /* loca_recalim always false */
9149 		task->tk_status = 0;
9150 	case 0:
9151 		break;
9152 	default:
9153 		if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9154 			rpc_restart_call_prepare(task);
9155 			return;
9156 		}
9157 	}
9158 }
9159 
9160 static void nfs4_layoutcommit_release(void *calldata)
9161 {
9162 	struct nfs4_layoutcommit_data *data = calldata;
9163 
9164 	pnfs_cleanup_layoutcommit(data);
9165 	nfs_post_op_update_inode_force_wcc(data->args.inode,
9166 					   data->res.fattr);
9167 	put_cred(data->cred);
9168 	nfs_iput_and_deactive(data->inode);
9169 	kfree(data);
9170 }
9171 
9172 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9173 	.rpc_call_prepare = nfs4_layoutcommit_prepare,
9174 	.rpc_call_done = nfs4_layoutcommit_done,
9175 	.rpc_release = nfs4_layoutcommit_release,
9176 };
9177 
9178 int
9179 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9180 {
9181 	struct rpc_message msg = {
9182 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9183 		.rpc_argp = &data->args,
9184 		.rpc_resp = &data->res,
9185 		.rpc_cred = data->cred,
9186 	};
9187 	struct rpc_task_setup task_setup_data = {
9188 		.task = &data->task,
9189 		.rpc_client = NFS_CLIENT(data->args.inode),
9190 		.rpc_message = &msg,
9191 		.callback_ops = &nfs4_layoutcommit_ops,
9192 		.callback_data = data,
9193 	};
9194 	struct rpc_task *task;
9195 	int status = 0;
9196 
9197 	dprintk("NFS: initiating layoutcommit call. sync %d "
9198 		"lbw: %llu inode %lu\n", sync,
9199 		data->args.lastbytewritten,
9200 		data->args.inode->i_ino);
9201 
9202 	if (!sync) {
9203 		data->inode = nfs_igrab_and_active(data->args.inode);
9204 		if (data->inode == NULL) {
9205 			nfs4_layoutcommit_release(data);
9206 			return -EAGAIN;
9207 		}
9208 		task_setup_data.flags = RPC_TASK_ASYNC;
9209 	}
9210 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9211 	task = rpc_run_task(&task_setup_data);
9212 	if (IS_ERR(task))
9213 		return PTR_ERR(task);
9214 	if (sync)
9215 		status = task->tk_status;
9216 	trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9217 	dprintk("%s: status %d\n", __func__, status);
9218 	rpc_put_task(task);
9219 	return status;
9220 }
9221 
9222 /*
9223  * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9224  * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9225  */
9226 static int
9227 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9228 		    struct nfs_fsinfo *info,
9229 		    struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9230 {
9231 	struct nfs41_secinfo_no_name_args args = {
9232 		.style = SECINFO_STYLE_CURRENT_FH,
9233 	};
9234 	struct nfs4_secinfo_res res = {
9235 		.flavors = flavors,
9236 	};
9237 	struct rpc_message msg = {
9238 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9239 		.rpc_argp = &args,
9240 		.rpc_resp = &res,
9241 	};
9242 	struct rpc_clnt *clnt = server->client;
9243 	const struct cred *cred = NULL;
9244 	int status;
9245 
9246 	if (use_integrity) {
9247 		clnt = server->nfs_client->cl_rpcclient;
9248 		cred = nfs4_get_clid_cred(server->nfs_client);
9249 		msg.rpc_cred = cred;
9250 	}
9251 
9252 	dprintk("--> %s\n", __func__);
9253 	status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9254 				&res.seq_res, 0);
9255 	dprintk("<-- %s status=%d\n", __func__, status);
9256 
9257 	put_cred(cred);
9258 
9259 	return status;
9260 }
9261 
9262 static int
9263 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9264 			   struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9265 {
9266 	struct nfs4_exception exception = { };
9267 	int err;
9268 	do {
9269 		/* first try using integrity protection */
9270 		err = -NFS4ERR_WRONGSEC;
9271 
9272 		/* try to use integrity protection with machine cred */
9273 		if (_nfs4_is_integrity_protected(server->nfs_client))
9274 			err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9275 							  flavors, true);
9276 
9277 		/*
9278 		 * if unable to use integrity protection, or SECINFO with
9279 		 * integrity protection returns NFS4ERR_WRONGSEC (which is
9280 		 * disallowed by spec, but exists in deployed servers) use
9281 		 * the current filesystem's rpc_client and the user cred.
9282 		 */
9283 		if (err == -NFS4ERR_WRONGSEC)
9284 			err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9285 							  flavors, false);
9286 
9287 		switch (err) {
9288 		case 0:
9289 		case -NFS4ERR_WRONGSEC:
9290 		case -ENOTSUPP:
9291 			goto out;
9292 		default:
9293 			err = nfs4_handle_exception(server, err, &exception);
9294 		}
9295 	} while (exception.retry);
9296 out:
9297 	return err;
9298 }
9299 
9300 static int
9301 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9302 		    struct nfs_fsinfo *info)
9303 {
9304 	int err;
9305 	struct page *page;
9306 	rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9307 	struct nfs4_secinfo_flavors *flavors;
9308 	struct nfs4_secinfo4 *secinfo;
9309 	int i;
9310 
9311 	page = alloc_page(GFP_KERNEL);
9312 	if (!page) {
9313 		err = -ENOMEM;
9314 		goto out;
9315 	}
9316 
9317 	flavors = page_address(page);
9318 	err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9319 
9320 	/*
9321 	 * Fall back on "guess and check" method if
9322 	 * the server doesn't support SECINFO_NO_NAME
9323 	 */
9324 	if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9325 		err = nfs4_find_root_sec(server, fhandle, info);
9326 		goto out_freepage;
9327 	}
9328 	if (err)
9329 		goto out_freepage;
9330 
9331 	for (i = 0; i < flavors->num_flavors; i++) {
9332 		secinfo = &flavors->flavors[i];
9333 
9334 		switch (secinfo->flavor) {
9335 		case RPC_AUTH_NULL:
9336 		case RPC_AUTH_UNIX:
9337 		case RPC_AUTH_GSS:
9338 			flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9339 					&secinfo->flavor_info);
9340 			break;
9341 		default:
9342 			flavor = RPC_AUTH_MAXFLAVOR;
9343 			break;
9344 		}
9345 
9346 		if (!nfs_auth_info_match(&server->auth_info, flavor))
9347 			flavor = RPC_AUTH_MAXFLAVOR;
9348 
9349 		if (flavor != RPC_AUTH_MAXFLAVOR) {
9350 			err = nfs4_lookup_root_sec(server, fhandle,
9351 						   info, flavor);
9352 			if (!err)
9353 				break;
9354 		}
9355 	}
9356 
9357 	if (flavor == RPC_AUTH_MAXFLAVOR)
9358 		err = -EPERM;
9359 
9360 out_freepage:
9361 	put_page(page);
9362 	if (err == -EACCES)
9363 		return -EPERM;
9364 out:
9365 	return err;
9366 }
9367 
9368 static int _nfs41_test_stateid(struct nfs_server *server,
9369 		nfs4_stateid *stateid,
9370 		const struct cred *cred)
9371 {
9372 	int status;
9373 	struct nfs41_test_stateid_args args = {
9374 		.stateid = stateid,
9375 	};
9376 	struct nfs41_test_stateid_res res;
9377 	struct rpc_message msg = {
9378 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9379 		.rpc_argp = &args,
9380 		.rpc_resp = &res,
9381 		.rpc_cred = cred,
9382 	};
9383 	struct rpc_clnt *rpc_client = server->client;
9384 
9385 	nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9386 		&rpc_client, &msg);
9387 
9388 	dprintk("NFS call  test_stateid %p\n", stateid);
9389 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
9390 	status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9391 			&args.seq_args, &res.seq_res);
9392 	if (status != NFS_OK) {
9393 		dprintk("NFS reply test_stateid: failed, %d\n", status);
9394 		return status;
9395 	}
9396 	dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9397 	return -res.status;
9398 }
9399 
9400 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9401 		int err, struct nfs4_exception *exception)
9402 {
9403 	exception->retry = 0;
9404 	switch(err) {
9405 	case -NFS4ERR_DELAY:
9406 	case -NFS4ERR_RETRY_UNCACHED_REP:
9407 		nfs4_handle_exception(server, err, exception);
9408 		break;
9409 	case -NFS4ERR_BADSESSION:
9410 	case -NFS4ERR_BADSLOT:
9411 	case -NFS4ERR_BAD_HIGH_SLOT:
9412 	case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9413 	case -NFS4ERR_DEADSESSION:
9414 		nfs4_do_handle_exception(server, err, exception);
9415 	}
9416 }
9417 
9418 /**
9419  * nfs41_test_stateid - perform a TEST_STATEID operation
9420  *
9421  * @server: server / transport on which to perform the operation
9422  * @stateid: state ID to test
9423  * @cred: credential
9424  *
9425  * Returns NFS_OK if the server recognizes that "stateid" is valid.
9426  * Otherwise a negative NFS4ERR value is returned if the operation
9427  * failed or the state ID is not currently valid.
9428  */
9429 static int nfs41_test_stateid(struct nfs_server *server,
9430 		nfs4_stateid *stateid,
9431 		const struct cred *cred)
9432 {
9433 	struct nfs4_exception exception = { };
9434 	int err;
9435 	do {
9436 		err = _nfs41_test_stateid(server, stateid, cred);
9437 		nfs4_handle_delay_or_session_error(server, err, &exception);
9438 	} while (exception.retry);
9439 	return err;
9440 }
9441 
9442 struct nfs_free_stateid_data {
9443 	struct nfs_server *server;
9444 	struct nfs41_free_stateid_args args;
9445 	struct nfs41_free_stateid_res res;
9446 };
9447 
9448 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9449 {
9450 	struct nfs_free_stateid_data *data = calldata;
9451 	nfs4_setup_sequence(data->server->nfs_client,
9452 			&data->args.seq_args,
9453 			&data->res.seq_res,
9454 			task);
9455 }
9456 
9457 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9458 {
9459 	struct nfs_free_stateid_data *data = calldata;
9460 
9461 	nfs41_sequence_done(task, &data->res.seq_res);
9462 
9463 	switch (task->tk_status) {
9464 	case -NFS4ERR_DELAY:
9465 		if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9466 			rpc_restart_call_prepare(task);
9467 	}
9468 }
9469 
9470 static void nfs41_free_stateid_release(void *calldata)
9471 {
9472 	kfree(calldata);
9473 }
9474 
9475 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9476 	.rpc_call_prepare = nfs41_free_stateid_prepare,
9477 	.rpc_call_done = nfs41_free_stateid_done,
9478 	.rpc_release = nfs41_free_stateid_release,
9479 };
9480 
9481 /**
9482  * nfs41_free_stateid - perform a FREE_STATEID operation
9483  *
9484  * @server: server / transport on which to perform the operation
9485  * @stateid: state ID to release
9486  * @cred: credential
9487  * @privileged: set to true if this call needs to be privileged
9488  *
9489  * Note: this function is always asynchronous.
9490  */
9491 static int nfs41_free_stateid(struct nfs_server *server,
9492 		const nfs4_stateid *stateid,
9493 		const struct cred *cred,
9494 		bool privileged)
9495 {
9496 	struct rpc_message msg = {
9497 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9498 		.rpc_cred = cred,
9499 	};
9500 	struct rpc_task_setup task_setup = {
9501 		.rpc_client = server->client,
9502 		.rpc_message = &msg,
9503 		.callback_ops = &nfs41_free_stateid_ops,
9504 		.flags = RPC_TASK_ASYNC,
9505 	};
9506 	struct nfs_free_stateid_data *data;
9507 	struct rpc_task *task;
9508 
9509 	nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9510 		&task_setup.rpc_client, &msg);
9511 
9512 	dprintk("NFS call  free_stateid %p\n", stateid);
9513 	data = kmalloc(sizeof(*data), GFP_NOFS);
9514 	if (!data)
9515 		return -ENOMEM;
9516 	data->server = server;
9517 	nfs4_stateid_copy(&data->args.stateid, stateid);
9518 
9519 	task_setup.callback_data = data;
9520 
9521 	msg.rpc_argp = &data->args;
9522 	msg.rpc_resp = &data->res;
9523 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
9524 	task = rpc_run_task(&task_setup);
9525 	if (IS_ERR(task))
9526 		return PTR_ERR(task);
9527 	rpc_put_task(task);
9528 	return 0;
9529 }
9530 
9531 static void
9532 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9533 {
9534 	const struct cred *cred = lsp->ls_state->owner->so_cred;
9535 
9536 	nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9537 	nfs4_free_lock_state(server, lsp);
9538 }
9539 
9540 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9541 		const nfs4_stateid *s2)
9542 {
9543 	if (s1->type != s2->type)
9544 		return false;
9545 
9546 	if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9547 		return false;
9548 
9549 	if (s1->seqid == s2->seqid)
9550 		return true;
9551 
9552 	return s1->seqid == 0 || s2->seqid == 0;
9553 }
9554 
9555 #endif /* CONFIG_NFS_V4_1 */
9556 
9557 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9558 		const nfs4_stateid *s2)
9559 {
9560 	return nfs4_stateid_match(s1, s2);
9561 }
9562 
9563 
9564 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9565 	.owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9566 	.state_flag_bit	= NFS_STATE_RECLAIM_REBOOT,
9567 	.recover_open	= nfs4_open_reclaim,
9568 	.recover_lock	= nfs4_lock_reclaim,
9569 	.establish_clid = nfs4_init_clientid,
9570 	.detect_trunking = nfs40_discover_server_trunking,
9571 };
9572 
9573 #if defined(CONFIG_NFS_V4_1)
9574 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9575 	.owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9576 	.state_flag_bit	= NFS_STATE_RECLAIM_REBOOT,
9577 	.recover_open	= nfs4_open_reclaim,
9578 	.recover_lock	= nfs4_lock_reclaim,
9579 	.establish_clid = nfs41_init_clientid,
9580 	.reclaim_complete = nfs41_proc_reclaim_complete,
9581 	.detect_trunking = nfs41_discover_server_trunking,
9582 };
9583 #endif /* CONFIG_NFS_V4_1 */
9584 
9585 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9586 	.owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9587 	.state_flag_bit	= NFS_STATE_RECLAIM_NOGRACE,
9588 	.recover_open	= nfs40_open_expired,
9589 	.recover_lock	= nfs4_lock_expired,
9590 	.establish_clid = nfs4_init_clientid,
9591 };
9592 
9593 #if defined(CONFIG_NFS_V4_1)
9594 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9595 	.owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9596 	.state_flag_bit	= NFS_STATE_RECLAIM_NOGRACE,
9597 	.recover_open	= nfs41_open_expired,
9598 	.recover_lock	= nfs41_lock_expired,
9599 	.establish_clid = nfs41_init_clientid,
9600 };
9601 #endif /* CONFIG_NFS_V4_1 */
9602 
9603 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9604 	.sched_state_renewal = nfs4_proc_async_renew,
9605 	.get_state_renewal_cred = nfs4_get_renew_cred,
9606 	.renew_lease = nfs4_proc_renew,
9607 };
9608 
9609 #if defined(CONFIG_NFS_V4_1)
9610 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9611 	.sched_state_renewal = nfs41_proc_async_sequence,
9612 	.get_state_renewal_cred = nfs4_get_machine_cred,
9613 	.renew_lease = nfs4_proc_sequence,
9614 };
9615 #endif
9616 
9617 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9618 	.get_locations = _nfs40_proc_get_locations,
9619 	.fsid_present = _nfs40_proc_fsid_present,
9620 };
9621 
9622 #if defined(CONFIG_NFS_V4_1)
9623 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9624 	.get_locations = _nfs41_proc_get_locations,
9625 	.fsid_present = _nfs41_proc_fsid_present,
9626 };
9627 #endif	/* CONFIG_NFS_V4_1 */
9628 
9629 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9630 	.minor_version = 0,
9631 	.init_caps = NFS_CAP_READDIRPLUS
9632 		| NFS_CAP_ATOMIC_OPEN
9633 		| NFS_CAP_POSIX_LOCK,
9634 	.init_client = nfs40_init_client,
9635 	.shutdown_client = nfs40_shutdown_client,
9636 	.match_stateid = nfs4_match_stateid,
9637 	.find_root_sec = nfs4_find_root_sec,
9638 	.free_lock_state = nfs4_release_lockowner,
9639 	.test_and_free_expired = nfs40_test_and_free_expired_stateid,
9640 	.alloc_seqid = nfs_alloc_seqid,
9641 	.call_sync_ops = &nfs40_call_sync_ops,
9642 	.reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9643 	.nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9644 	.state_renewal_ops = &nfs40_state_renewal_ops,
9645 	.mig_recovery_ops = &nfs40_mig_recovery_ops,
9646 };
9647 
9648 #if defined(CONFIG_NFS_V4_1)
9649 static struct nfs_seqid *
9650 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9651 {
9652 	return NULL;
9653 }
9654 
9655 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9656 	.minor_version = 1,
9657 	.init_caps = NFS_CAP_READDIRPLUS
9658 		| NFS_CAP_ATOMIC_OPEN
9659 		| NFS_CAP_POSIX_LOCK
9660 		| NFS_CAP_STATEID_NFSV41
9661 		| NFS_CAP_ATOMIC_OPEN_V1
9662 		| NFS_CAP_LGOPEN,
9663 	.init_client = nfs41_init_client,
9664 	.shutdown_client = nfs41_shutdown_client,
9665 	.match_stateid = nfs41_match_stateid,
9666 	.find_root_sec = nfs41_find_root_sec,
9667 	.free_lock_state = nfs41_free_lock_state,
9668 	.test_and_free_expired = nfs41_test_and_free_expired_stateid,
9669 	.alloc_seqid = nfs_alloc_no_seqid,
9670 	.session_trunk = nfs4_test_session_trunk,
9671 	.call_sync_ops = &nfs41_call_sync_ops,
9672 	.reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9673 	.nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9674 	.state_renewal_ops = &nfs41_state_renewal_ops,
9675 	.mig_recovery_ops = &nfs41_mig_recovery_ops,
9676 };
9677 #endif
9678 
9679 #if defined(CONFIG_NFS_V4_2)
9680 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9681 	.minor_version = 2,
9682 	.init_caps = NFS_CAP_READDIRPLUS
9683 		| NFS_CAP_ATOMIC_OPEN
9684 		| NFS_CAP_POSIX_LOCK
9685 		| NFS_CAP_STATEID_NFSV41
9686 		| NFS_CAP_ATOMIC_OPEN_V1
9687 		| NFS_CAP_LGOPEN
9688 		| NFS_CAP_ALLOCATE
9689 		| NFS_CAP_COPY
9690 		| NFS_CAP_OFFLOAD_CANCEL
9691 		| NFS_CAP_DEALLOCATE
9692 		| NFS_CAP_SEEK
9693 		| NFS_CAP_LAYOUTSTATS
9694 		| NFS_CAP_CLONE
9695 		| NFS_CAP_LAYOUTERROR,
9696 	.init_client = nfs41_init_client,
9697 	.shutdown_client = nfs41_shutdown_client,
9698 	.match_stateid = nfs41_match_stateid,
9699 	.find_root_sec = nfs41_find_root_sec,
9700 	.free_lock_state = nfs41_free_lock_state,
9701 	.call_sync_ops = &nfs41_call_sync_ops,
9702 	.test_and_free_expired = nfs41_test_and_free_expired_stateid,
9703 	.alloc_seqid = nfs_alloc_no_seqid,
9704 	.session_trunk = nfs4_test_session_trunk,
9705 	.reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9706 	.nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9707 	.state_renewal_ops = &nfs41_state_renewal_ops,
9708 	.mig_recovery_ops = &nfs41_mig_recovery_ops,
9709 };
9710 #endif
9711 
9712 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9713 	[0] = &nfs_v4_0_minor_ops,
9714 #if defined(CONFIG_NFS_V4_1)
9715 	[1] = &nfs_v4_1_minor_ops,
9716 #endif
9717 #if defined(CONFIG_NFS_V4_2)
9718 	[2] = &nfs_v4_2_minor_ops,
9719 #endif
9720 };
9721 
9722 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9723 {
9724 	ssize_t error, error2;
9725 
9726 	error = generic_listxattr(dentry, list, size);
9727 	if (error < 0)
9728 		return error;
9729 	if (list) {
9730 		list += error;
9731 		size -= error;
9732 	}
9733 
9734 	error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9735 	if (error2 < 0)
9736 		return error2;
9737 	return error + error2;
9738 }
9739 
9740 static const struct inode_operations nfs4_dir_inode_operations = {
9741 	.create		= nfs_create,
9742 	.lookup		= nfs_lookup,
9743 	.atomic_open	= nfs_atomic_open,
9744 	.link		= nfs_link,
9745 	.unlink		= nfs_unlink,
9746 	.symlink	= nfs_symlink,
9747 	.mkdir		= nfs_mkdir,
9748 	.rmdir		= nfs_rmdir,
9749 	.mknod		= nfs_mknod,
9750 	.rename		= nfs_rename,
9751 	.permission	= nfs_permission,
9752 	.getattr	= nfs_getattr,
9753 	.setattr	= nfs_setattr,
9754 	.listxattr	= nfs4_listxattr,
9755 };
9756 
9757 static const struct inode_operations nfs4_file_inode_operations = {
9758 	.permission	= nfs_permission,
9759 	.getattr	= nfs_getattr,
9760 	.setattr	= nfs_setattr,
9761 	.listxattr	= nfs4_listxattr,
9762 };
9763 
9764 const struct nfs_rpc_ops nfs_v4_clientops = {
9765 	.version	= 4,			/* protocol version */
9766 	.dentry_ops	= &nfs4_dentry_operations,
9767 	.dir_inode_ops	= &nfs4_dir_inode_operations,
9768 	.file_inode_ops	= &nfs4_file_inode_operations,
9769 	.file_ops	= &nfs4_file_operations,
9770 	.getroot	= nfs4_proc_get_root,
9771 	.submount	= nfs4_submount,
9772 	.try_mount	= nfs4_try_mount,
9773 	.getattr	= nfs4_proc_getattr,
9774 	.setattr	= nfs4_proc_setattr,
9775 	.lookup		= nfs4_proc_lookup,
9776 	.lookupp	= nfs4_proc_lookupp,
9777 	.access		= nfs4_proc_access,
9778 	.readlink	= nfs4_proc_readlink,
9779 	.create		= nfs4_proc_create,
9780 	.remove		= nfs4_proc_remove,
9781 	.unlink_setup	= nfs4_proc_unlink_setup,
9782 	.unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9783 	.unlink_done	= nfs4_proc_unlink_done,
9784 	.rename_setup	= nfs4_proc_rename_setup,
9785 	.rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9786 	.rename_done	= nfs4_proc_rename_done,
9787 	.link		= nfs4_proc_link,
9788 	.symlink	= nfs4_proc_symlink,
9789 	.mkdir		= nfs4_proc_mkdir,
9790 	.rmdir		= nfs4_proc_rmdir,
9791 	.readdir	= nfs4_proc_readdir,
9792 	.mknod		= nfs4_proc_mknod,
9793 	.statfs		= nfs4_proc_statfs,
9794 	.fsinfo		= nfs4_proc_fsinfo,
9795 	.pathconf	= nfs4_proc_pathconf,
9796 	.set_capabilities = nfs4_server_capabilities,
9797 	.decode_dirent	= nfs4_decode_dirent,
9798 	.pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9799 	.read_setup	= nfs4_proc_read_setup,
9800 	.read_done	= nfs4_read_done,
9801 	.write_setup	= nfs4_proc_write_setup,
9802 	.write_done	= nfs4_write_done,
9803 	.commit_setup	= nfs4_proc_commit_setup,
9804 	.commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9805 	.commit_done	= nfs4_commit_done,
9806 	.lock		= nfs4_proc_lock,
9807 	.clear_acl_cache = nfs4_zap_acl_attr,
9808 	.close_context  = nfs4_close_context,
9809 	.open_context	= nfs4_atomic_open,
9810 	.have_delegation = nfs4_have_delegation,
9811 	.alloc_client	= nfs4_alloc_client,
9812 	.init_client	= nfs4_init_client,
9813 	.free_client	= nfs4_free_client,
9814 	.create_server	= nfs4_create_server,
9815 	.clone_server	= nfs_clone_server,
9816 };
9817 
9818 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9819 	.name	= XATTR_NAME_NFSV4_ACL,
9820 	.list	= nfs4_xattr_list_nfs4_acl,
9821 	.get	= nfs4_xattr_get_nfs4_acl,
9822 	.set	= nfs4_xattr_set_nfs4_acl,
9823 };
9824 
9825 const struct xattr_handler *nfs4_xattr_handlers[] = {
9826 	&nfs4_xattr_nfs4_acl_handler,
9827 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9828 	&nfs4_xattr_nfs4_label_handler,
9829 #endif
9830 	NULL
9831 };
9832 
9833 /*
9834  * Local variables:
9835  *  c-basic-offset: 8
9836  * End:
9837  */
9838