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