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