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