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