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