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