xref: /openbmc/linux/fs/nfsd/nfs4proc.c (revision 1913cdf56cb5bfbc8170873728d13598cbecda23)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  Server-side procedures for NFSv4.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (c) 2002 The Regents of the University of Michigan.
51da177e4SLinus Torvalds  *  All rights reserved.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  Kendrick Smith <kmsmith@umich.edu>
81da177e4SLinus Torvalds  *  Andy Adamson   <andros@umich.edu>
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *  Redistribution and use in source and binary forms, with or without
111da177e4SLinus Torvalds  *  modification, are permitted provided that the following conditions
121da177e4SLinus Torvalds  *  are met:
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *  1. Redistributions of source code must retain the above copyright
151da177e4SLinus Torvalds  *     notice, this list of conditions and the following disclaimer.
161da177e4SLinus Torvalds  *  2. Redistributions in binary form must reproduce the above copyright
171da177e4SLinus Torvalds  *     notice, this list of conditions and the following disclaimer in the
181da177e4SLinus Torvalds  *     documentation and/or other materials provided with the distribution.
191da177e4SLinus Torvalds  *  3. Neither the name of the University nor the names of its
201da177e4SLinus Torvalds  *     contributors may be used to endorse or promote products derived
211da177e4SLinus Torvalds  *     from this software without specific prior written permission.
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
241da177e4SLinus Torvalds  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
251da177e4SLinus Torvalds  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
261da177e4SLinus Torvalds  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
271da177e4SLinus Torvalds  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
281da177e4SLinus Torvalds  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
291da177e4SLinus Torvalds  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
301da177e4SLinus Torvalds  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
311da177e4SLinus Torvalds  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
321da177e4SLinus Torvalds  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
331da177e4SLinus Torvalds  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
341da177e4SLinus Torvalds  */
35880a3a53SJ. Bruce Fields #include <linux/fs_struct.h>
367e06b7f9SNeilBrown #include <linux/file.h>
37b0cb9085SAnna Schumaker #include <linux/falloc.h>
385a0e3ad6STejun Heo #include <linux/slab.h>
39e0639dc5SOlga Kornievskaia #include <linux/kthread.h>
40254454a5SChuck Lever #include <linux/namei.h>
41254454a5SChuck Lever 
42624322f1SOlga Kornievskaia #include <linux/sunrpc/addr.h>
430cfcd405SDai Ngo #include <linux/nfs_ssc.h>
441da177e4SLinus Torvalds 
4558e7b33aSMi Jinlong #include "idmap.h"
469a74af21SBoaz Harrosh #include "cache.h"
479a74af21SBoaz Harrosh #include "xdr4.h"
480a3adadeSJ. Bruce Fields #include "vfs.h"
498b70484cSTigran Mkrtchyan #include "current_stateid.h"
503320fef1SStanislav Kinsbursky #include "netns.h"
514ac7249eSChristoph Hellwig #include "acl.h"
529cf514ccSChristoph Hellwig #include "pnfs.h"
5331ef83dcSChristoph Hellwig #include "trace.h"
541da177e4SLinus Torvalds 
55d6c9e436SChuck Lever static bool inter_copy_offload_enable;
56d6c9e436SChuck Lever module_param(inter_copy_offload_enable, bool, 0644);
57d6c9e436SChuck Lever MODULE_PARM_DESC(inter_copy_offload_enable,
58d6c9e436SChuck Lever 		 "Enable inter server to server copy offload. Default: false");
59d6c9e436SChuck Lever 
60f4e44b39SDai Ngo #ifdef CONFIG_NFSD_V4_2_INTER_SSC
61f4e44b39SDai Ngo static int nfsd4_ssc_umount_timeout = 900000;		/* default to 15 mins */
62f4e44b39SDai Ngo module_param(nfsd4_ssc_umount_timeout, int, 0644);
63f4e44b39SDai Ngo MODULE_PARM_DESC(nfsd4_ssc_umount_timeout,
64f4e44b39SDai Ngo 		"idle msecs before unmount export from source server");
65f4e44b39SDai Ngo #endif
66f4e44b39SDai Ngo 
6718032ca0SDavid Quigley #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
6818032ca0SDavid Quigley #include <linux/security.h>
6918032ca0SDavid Quigley 
7018032ca0SDavid Quigley static inline void
7118032ca0SDavid Quigley nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
7218032ca0SDavid Quigley {
732b0143b5SDavid Howells 	struct inode *inode = d_inode(resfh->fh_dentry);
7418032ca0SDavid Quigley 	int status;
7518032ca0SDavid Quigley 
765955102cSAl Viro 	inode_lock(inode);
7718032ca0SDavid Quigley 	status = security_inode_setsecctx(resfh->fh_dentry,
7818032ca0SDavid Quigley 		label->data, label->len);
795955102cSAl Viro 	inode_unlock(inode);
8018032ca0SDavid Quigley 
8118032ca0SDavid Quigley 	if (status)
8218032ca0SDavid Quigley 		/*
8318032ca0SDavid Quigley 		 * XXX: We should really fail the whole open, but we may
8418032ca0SDavid Quigley 		 * already have created a new file, so it may be too
8518032ca0SDavid Quigley 		 * late.  For now this seems the least of evils:
8618032ca0SDavid Quigley 		 */
8718032ca0SDavid Quigley 		bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
8818032ca0SDavid Quigley 
8918032ca0SDavid Quigley 	return;
9018032ca0SDavid Quigley }
9118032ca0SDavid Quigley #else
9218032ca0SDavid Quigley static inline void
9318032ca0SDavid Quigley nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
9418032ca0SDavid Quigley { }
9518032ca0SDavid Quigley #endif
9618032ca0SDavid Quigley 
971da177e4SLinus Torvalds #define NFSDDBG_FACILITY		NFSDDBG_PROC
981da177e4SLinus Torvalds 
993c8e0316SYu Zhiguo static u32 nfsd_attrmask[] = {
1003c8e0316SYu Zhiguo 	NFSD_WRITEABLE_ATTRS_WORD0,
1013c8e0316SYu Zhiguo 	NFSD_WRITEABLE_ATTRS_WORD1,
1023c8e0316SYu Zhiguo 	NFSD_WRITEABLE_ATTRS_WORD2
1033c8e0316SYu Zhiguo };
1043c8e0316SYu Zhiguo 
1053c8e0316SYu Zhiguo static u32 nfsd41_ex_attrmask[] = {
1063c8e0316SYu Zhiguo 	NFSD_SUPPATTR_EXCLCREAT_WORD0,
1073c8e0316SYu Zhiguo 	NFSD_SUPPATTR_EXCLCREAT_WORD1,
1083c8e0316SYu Zhiguo 	NFSD_SUPPATTR_EXCLCREAT_WORD2
1093c8e0316SYu Zhiguo };
1103c8e0316SYu Zhiguo 
1113c8e0316SYu Zhiguo static __be32
1123c8e0316SYu Zhiguo check_attr_support(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1133c8e0316SYu Zhiguo 		   u32 *bmval, u32 *writable)
1143c8e0316SYu Zhiguo {
1153c8e0316SYu Zhiguo 	struct dentry *dentry = cstate->current_fh.fh_dentry;
11632ddd944SJ. Bruce Fields 	struct svc_export *exp = cstate->current_fh.fh_export;
1173c8e0316SYu Zhiguo 
118916d2d84SJ. Bruce Fields 	if (!nfsd_attrs_supported(cstate->minorversion, bmval))
1193c8e0316SYu Zhiguo 		return nfserr_attrnotsupp;
120916d2d84SJ. Bruce Fields 	if ((bmval[0] & FATTR4_WORD0_ACL) && !IS_POSIXACL(d_inode(dentry)))
1213c8e0316SYu Zhiguo 		return nfserr_attrnotsupp;
12232ddd944SJ. Bruce Fields 	if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) &&
12332ddd944SJ. Bruce Fields 			!(exp->ex_flags & NFSEXP_SECURITY_LABEL))
12432ddd944SJ. Bruce Fields 		return nfserr_attrnotsupp;
125916d2d84SJ. Bruce Fields 	if (writable && !bmval_is_subset(bmval, writable))
1263c8e0316SYu Zhiguo 		return nfserr_inval;
12747057abdSAndreas Gruenbacher 	if (writable && (bmval[2] & FATTR4_WORD2_MODE_UMASK) &&
12847057abdSAndreas Gruenbacher 			(bmval[1] & FATTR4_WORD1_MODE))
12947057abdSAndreas Gruenbacher 		return nfserr_inval;
1303c8e0316SYu Zhiguo 	return nfs_ok;
1313c8e0316SYu Zhiguo }
1323c8e0316SYu Zhiguo 
1333c8e0316SYu Zhiguo static __be32
1343c8e0316SYu Zhiguo nfsd4_check_open_attributes(struct svc_rqst *rqstp,
1353c8e0316SYu Zhiguo 	struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
1363c8e0316SYu Zhiguo {
1373c8e0316SYu Zhiguo 	__be32 status = nfs_ok;
1383c8e0316SYu Zhiguo 
1393c8e0316SYu Zhiguo 	if (open->op_create == NFS4_OPEN_CREATE) {
1403c8e0316SYu Zhiguo 		if (open->op_createmode == NFS4_CREATE_UNCHECKED
1413c8e0316SYu Zhiguo 		    || open->op_createmode == NFS4_CREATE_GUARDED)
1423c8e0316SYu Zhiguo 			status = check_attr_support(rqstp, cstate,
1433c8e0316SYu Zhiguo 					open->op_bmval, nfsd_attrmask);
1443c8e0316SYu Zhiguo 		else if (open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1)
1453c8e0316SYu Zhiguo 			status = check_attr_support(rqstp, cstate,
1463c8e0316SYu Zhiguo 					open->op_bmval, nfsd41_ex_attrmask);
1473c8e0316SYu Zhiguo 	}
1483c8e0316SYu Zhiguo 
1493c8e0316SYu Zhiguo 	return status;
1503c8e0316SYu Zhiguo }
1513c8e0316SYu Zhiguo 
1529208faf2SYu Zhiguo static int
1539208faf2SYu Zhiguo is_create_with_attrs(struct nfsd4_open *open)
1549208faf2SYu Zhiguo {
1559208faf2SYu Zhiguo 	return open->op_create == NFS4_OPEN_CREATE
1569208faf2SYu Zhiguo 		&& (open->op_createmode == NFS4_CREATE_UNCHECKED
1579208faf2SYu Zhiguo 		    || open->op_createmode == NFS4_CREATE_GUARDED
1589208faf2SYu Zhiguo 		    || open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1);
1599208faf2SYu Zhiguo }
1609208faf2SYu Zhiguo 
1619208faf2SYu Zhiguo /*
1629208faf2SYu Zhiguo  * if error occurs when setting the acl, just clear the acl bit
1639208faf2SYu Zhiguo  * in the returned attr bitmap.
1649208faf2SYu Zhiguo  */
1659208faf2SYu Zhiguo static void
1669208faf2SYu Zhiguo do_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
1679208faf2SYu Zhiguo 		struct nfs4_acl *acl, u32 *bmval)
1689208faf2SYu Zhiguo {
1699208faf2SYu Zhiguo 	__be32 status;
1709208faf2SYu Zhiguo 
1719208faf2SYu Zhiguo 	status = nfsd4_set_nfs4_acl(rqstp, fhp, acl);
1729208faf2SYu Zhiguo 	if (status)
1739208faf2SYu Zhiguo 		/*
1749208faf2SYu Zhiguo 		 * We should probably fail the whole open at this point,
1759208faf2SYu Zhiguo 		 * but we've already created the file, so it's too late;
1769208faf2SYu Zhiguo 		 * So this seems the least of evils:
1779208faf2SYu Zhiguo 		 */
1789208faf2SYu Zhiguo 		bmval[0] &= ~FATTR4_WORD0_ACL;
1799208faf2SYu Zhiguo }
1809208faf2SYu Zhiguo 
1811da177e4SLinus Torvalds static inline void
1821da177e4SLinus Torvalds fh_dup2(struct svc_fh *dst, struct svc_fh *src)
1831da177e4SLinus Torvalds {
1841da177e4SLinus Torvalds 	fh_put(dst);
1851da177e4SLinus Torvalds 	dget(src->fh_dentry);
1861da177e4SLinus Torvalds 	if (src->fh_export)
187bf18f163SKinglong Mee 		exp_get(src->fh_export);
1881da177e4SLinus Torvalds 	*dst = *src;
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
191b37ad28bSAl Viro static __be32
192dc730e17SJ. Bruce Fields do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, int accmode)
1931da177e4SLinus Torvalds {
194b37ad28bSAl Viro 	__be32 status;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	if (open->op_truncate &&
1971da177e4SLinus Torvalds 		!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
1981da177e4SLinus Torvalds 		return nfserr_inval;
1991da177e4SLinus Torvalds 
200a043226bSJ. Bruce Fields 	accmode |= NFSD_MAY_READ_IF_EXEC;
201a043226bSJ. Bruce Fields 
2021da177e4SLinus Torvalds 	if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
2038837abcaSMiklos Szeredi 		accmode |= NFSD_MAY_READ;
2049801d8a3SJ. Bruce Fields 	if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2058837abcaSMiklos Szeredi 		accmode |= (NFSD_MAY_WRITE | NFSD_MAY_TRUNC);
20657ecb34fSJ. Bruce Fields 	if (open->op_share_deny & NFS4_SHARE_DENY_READ)
2078837abcaSMiklos Szeredi 		accmode |= NFSD_MAY_WRITE;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	status = fh_verify(rqstp, current_fh, S_IFREG, accmode);
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	return status;
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds 
214aadab6c6SJ. Bruce Fields static __be32 nfsd_check_obj_isreg(struct svc_fh *fh)
215aadab6c6SJ. Bruce Fields {
2162b0143b5SDavid Howells 	umode_t mode = d_inode(fh->fh_dentry)->i_mode;
217aadab6c6SJ. Bruce Fields 
218aadab6c6SJ. Bruce Fields 	if (S_ISREG(mode))
219aadab6c6SJ. Bruce Fields 		return nfs_ok;
220aadab6c6SJ. Bruce Fields 	if (S_ISDIR(mode))
221aadab6c6SJ. Bruce Fields 		return nfserr_isdir;
222aadab6c6SJ. Bruce Fields 	/*
223aadab6c6SJ. Bruce Fields 	 * Using err_symlink as our catch-all case may look odd; but
224aadab6c6SJ. Bruce Fields 	 * there's no other obvious error for this case in 4.0, and we
225aadab6c6SJ. Bruce Fields 	 * happen to know that it will cause the linux v4 client to do
226aadab6c6SJ. Bruce Fields 	 * the right thing on attempts to open something other than a
227aadab6c6SJ. Bruce Fields 	 * regular file.
228aadab6c6SJ. Bruce Fields 	 */
229aadab6c6SJ. Bruce Fields 	return nfserr_symlink;
230aadab6c6SJ. Bruce Fields }
231aadab6c6SJ. Bruce Fields 
232bbc9c36cSJ. Bruce Fields static void nfsd4_set_open_owner_reply_cache(struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh *resfh)
2331da177e4SLinus Torvalds {
234bbc9c36cSJ. Bruce Fields 	if (nfsd4_has_session(cstate))
235bbc9c36cSJ. Bruce Fields 		return;
236bbc9c36cSJ. Bruce Fields 	fh_copy_shallow(&open->op_openowner->oo_owner.so_replay.rp_openfh,
237bbc9c36cSJ. Bruce Fields 			&resfh->fh_handle);
238bbc9c36cSJ. Bruce Fields }
239bbc9c36cSJ. Bruce Fields 
240254454a5SChuck Lever static inline bool nfsd4_create_is_exclusive(int createmode)
241254454a5SChuck Lever {
242254454a5SChuck Lever 	return createmode == NFS4_CREATE_EXCLUSIVE ||
243254454a5SChuck Lever 		createmode == NFS4_CREATE_EXCLUSIVE4_1;
244254454a5SChuck Lever }
245254454a5SChuck Lever 
246fb70bf12SChuck Lever static __be32
247fb70bf12SChuck Lever nfsd4_vfs_create(struct svc_fh *fhp, struct dentry *child,
248fb70bf12SChuck Lever 		 struct nfsd4_open *open)
249fb70bf12SChuck Lever {
250fb70bf12SChuck Lever 	struct file *filp;
251fb70bf12SChuck Lever 	struct path path;
252fb70bf12SChuck Lever 	int oflags;
253fb70bf12SChuck Lever 
254fb70bf12SChuck Lever 	oflags = O_CREAT | O_LARGEFILE;
255fb70bf12SChuck Lever 	switch (open->op_share_access & NFS4_SHARE_ACCESS_BOTH) {
256fb70bf12SChuck Lever 	case NFS4_SHARE_ACCESS_WRITE:
257fb70bf12SChuck Lever 		oflags |= O_WRONLY;
258fb70bf12SChuck Lever 		break;
259fb70bf12SChuck Lever 	case NFS4_SHARE_ACCESS_BOTH:
260fb70bf12SChuck Lever 		oflags |= O_RDWR;
261fb70bf12SChuck Lever 		break;
262fb70bf12SChuck Lever 	default:
263fb70bf12SChuck Lever 		oflags |= O_RDONLY;
264fb70bf12SChuck Lever 	}
265fb70bf12SChuck Lever 
266fb70bf12SChuck Lever 	path.mnt = fhp->fh_export->ex_path.mnt;
267fb70bf12SChuck Lever 	path.dentry = child;
268fb70bf12SChuck Lever 	filp = dentry_create(&path, oflags, open->op_iattr.ia_mode,
269fb70bf12SChuck Lever 			     current_cred());
270fb70bf12SChuck Lever 	if (IS_ERR(filp))
271fb70bf12SChuck Lever 		return nfserrno(PTR_ERR(filp));
272fb70bf12SChuck Lever 
273fb70bf12SChuck Lever 	open->op_filp = filp;
274fb70bf12SChuck Lever 	return nfs_ok;
275fb70bf12SChuck Lever }
276fb70bf12SChuck Lever 
277254454a5SChuck Lever /*
278254454a5SChuck Lever  * Implement NFSv4's unchecked, guarded, and exclusive create
279254454a5SChuck Lever  * semantics for regular files. Open state for this new file is
280254454a5SChuck Lever  * subsequently fabricated in nfsd4_process_open2().
281254454a5SChuck Lever  *
282254454a5SChuck Lever  * Upon return, caller must release @fhp and @resfhp.
283254454a5SChuck Lever  */
284254454a5SChuck Lever static __be32
285254454a5SChuck Lever nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
286254454a5SChuck Lever 		  struct svc_fh *resfhp, struct nfsd4_open *open)
287254454a5SChuck Lever {
288254454a5SChuck Lever 	struct iattr *iap = &open->op_iattr;
289254454a5SChuck Lever 	struct dentry *parent, *child;
290254454a5SChuck Lever 	__u32 v_mtime, v_atime;
291254454a5SChuck Lever 	struct inode *inode;
292254454a5SChuck Lever 	__be32 status;
293254454a5SChuck Lever 	int host_err;
294254454a5SChuck Lever 
295254454a5SChuck Lever 	if (isdotent(open->op_fname, open->op_fnamelen))
296254454a5SChuck Lever 		return nfserr_exist;
297254454a5SChuck Lever 	if (!(iap->ia_valid & ATTR_MODE))
298254454a5SChuck Lever 		iap->ia_mode = 0;
299254454a5SChuck Lever 
300254454a5SChuck Lever 	status = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
301254454a5SChuck Lever 	if (status != nfs_ok)
302254454a5SChuck Lever 		return status;
303254454a5SChuck Lever 	parent = fhp->fh_dentry;
304254454a5SChuck Lever 	inode = d_inode(parent);
305254454a5SChuck Lever 
306254454a5SChuck Lever 	host_err = fh_want_write(fhp);
307254454a5SChuck Lever 	if (host_err)
308254454a5SChuck Lever 		return nfserrno(host_err);
309254454a5SChuck Lever 
310254454a5SChuck Lever 	fh_lock_nested(fhp, I_MUTEX_PARENT);
311254454a5SChuck Lever 
312254454a5SChuck Lever 	child = lookup_one_len(open->op_fname, parent, open->op_fnamelen);
313254454a5SChuck Lever 	if (IS_ERR(child)) {
314254454a5SChuck Lever 		status = nfserrno(PTR_ERR(child));
315254454a5SChuck Lever 		goto out;
316254454a5SChuck Lever 	}
317254454a5SChuck Lever 
318254454a5SChuck Lever 	if (d_really_is_negative(child)) {
319254454a5SChuck Lever 		status = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
320254454a5SChuck Lever 		if (status != nfs_ok)
321254454a5SChuck Lever 			goto out;
322254454a5SChuck Lever 	}
323254454a5SChuck Lever 
324254454a5SChuck Lever 	status = fh_compose(resfhp, fhp->fh_export, child, fhp);
325254454a5SChuck Lever 	if (status != nfs_ok)
326254454a5SChuck Lever 		goto out;
327254454a5SChuck Lever 
328254454a5SChuck Lever 	v_mtime = 0;
329254454a5SChuck Lever 	v_atime = 0;
330254454a5SChuck Lever 	if (nfsd4_create_is_exclusive(open->op_createmode)) {
331254454a5SChuck Lever 		u32 *verifier = (u32 *)open->op_verf.data;
332254454a5SChuck Lever 
333254454a5SChuck Lever 		/*
334254454a5SChuck Lever 		 * Solaris 7 gets confused (bugid 4218508) if these have
335254454a5SChuck Lever 		 * the high bit set, as do xfs filesystems without the
336254454a5SChuck Lever 		 * "bigtime" feature. So just clear the high bits. If this
337254454a5SChuck Lever 		 * is ever changed to use different attrs for storing the
338254454a5SChuck Lever 		 * verifier, then do_open_lookup() will also need to be
339254454a5SChuck Lever 		 * fixed accordingly.
340254454a5SChuck Lever 		 */
341254454a5SChuck Lever 		v_mtime = verifier[0] & 0x7fffffff;
342254454a5SChuck Lever 		v_atime = verifier[1] & 0x7fffffff;
343254454a5SChuck Lever 	}
344254454a5SChuck Lever 
345254454a5SChuck Lever 	if (d_really_is_positive(child)) {
346254454a5SChuck Lever 		status = nfs_ok;
347254454a5SChuck Lever 
348254454a5SChuck Lever 		switch (open->op_createmode) {
349254454a5SChuck Lever 		case NFS4_CREATE_UNCHECKED:
350254454a5SChuck Lever 			if (!d_is_reg(child))
351254454a5SChuck Lever 				break;
352254454a5SChuck Lever 
353254454a5SChuck Lever 			/*
354254454a5SChuck Lever 			 * In NFSv4, we don't want to truncate the file
355254454a5SChuck Lever 			 * now. This would be wrong if the OPEN fails for
356254454a5SChuck Lever 			 * some other reason. Furthermore, if the size is
357254454a5SChuck Lever 			 * nonzero, we should ignore it according to spec!
358254454a5SChuck Lever 			 */
359254454a5SChuck Lever 			open->op_truncate = (iap->ia_valid & ATTR_SIZE) &&
360254454a5SChuck Lever 						!iap->ia_size;
361254454a5SChuck Lever 			break;
362254454a5SChuck Lever 		case NFS4_CREATE_GUARDED:
363254454a5SChuck Lever 			status = nfserr_exist;
364254454a5SChuck Lever 			break;
365254454a5SChuck Lever 		case NFS4_CREATE_EXCLUSIVE:
366254454a5SChuck Lever 			if (d_inode(child)->i_mtime.tv_sec == v_mtime &&
367254454a5SChuck Lever 			    d_inode(child)->i_atime.tv_sec == v_atime &&
368254454a5SChuck Lever 			    d_inode(child)->i_size == 0) {
369254454a5SChuck Lever 				open->op_created = true;
370254454a5SChuck Lever 				break;		/* subtle */
371254454a5SChuck Lever 			}
372254454a5SChuck Lever 			status = nfserr_exist;
373254454a5SChuck Lever 			break;
374254454a5SChuck Lever 		case NFS4_CREATE_EXCLUSIVE4_1:
375254454a5SChuck Lever 			if (d_inode(child)->i_mtime.tv_sec == v_mtime &&
376254454a5SChuck Lever 			    d_inode(child)->i_atime.tv_sec == v_atime &&
377254454a5SChuck Lever 			    d_inode(child)->i_size == 0) {
378254454a5SChuck Lever 				open->op_created = true;
379254454a5SChuck Lever 				goto set_attr;	/* subtle */
380254454a5SChuck Lever 			}
381254454a5SChuck Lever 			status = nfserr_exist;
382254454a5SChuck Lever 		}
383254454a5SChuck Lever 		goto out;
384254454a5SChuck Lever 	}
385254454a5SChuck Lever 
386254454a5SChuck Lever 	if (!IS_POSIXACL(inode))
387254454a5SChuck Lever 		iap->ia_mode &= ~current_umask();
388254454a5SChuck Lever 
389fb70bf12SChuck Lever 	status = nfsd4_vfs_create(fhp, child, open);
390fb70bf12SChuck Lever 	if (status != nfs_ok)
391254454a5SChuck Lever 		goto out;
392254454a5SChuck Lever 	open->op_created = true;
393254454a5SChuck Lever 
394254454a5SChuck Lever 	/* A newly created file already has a file size of zero. */
395254454a5SChuck Lever 	if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
396254454a5SChuck Lever 		iap->ia_valid &= ~ATTR_SIZE;
397254454a5SChuck Lever 	if (nfsd4_create_is_exclusive(open->op_createmode)) {
398254454a5SChuck Lever 		iap->ia_valid = ATTR_MTIME | ATTR_ATIME |
399254454a5SChuck Lever 				ATTR_MTIME_SET|ATTR_ATIME_SET;
400254454a5SChuck Lever 		iap->ia_mtime.tv_sec = v_mtime;
401254454a5SChuck Lever 		iap->ia_atime.tv_sec = v_atime;
402254454a5SChuck Lever 		iap->ia_mtime.tv_nsec = 0;
403254454a5SChuck Lever 		iap->ia_atime.tv_nsec = 0;
404254454a5SChuck Lever 	}
405254454a5SChuck Lever 
406254454a5SChuck Lever set_attr:
407254454a5SChuck Lever 	status = nfsd_create_setattr(rqstp, fhp, resfhp, iap);
408254454a5SChuck Lever 
409254454a5SChuck Lever out:
410254454a5SChuck Lever 	fh_unlock(fhp);
411254454a5SChuck Lever 	if (child && !IS_ERR(child))
412254454a5SChuck Lever 		dput(child);
413254454a5SChuck Lever 	fh_drop_write(fhp);
414254454a5SChuck Lever 	return status;
415254454a5SChuck Lever }
416254454a5SChuck Lever 
417bbc9c36cSJ. Bruce Fields static __be32
418c0e6bee4SJ. Bruce Fields do_open_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh **resfh)
419bbc9c36cSJ. Bruce Fields {
420bbc9c36cSJ. Bruce Fields 	struct svc_fh *current_fh = &cstate->current_fh;
4217007c90fSNeil Brown 	int accmode;
422b37ad28bSAl Viro 	__be32 status;
4231da177e4SLinus Torvalds 
424c0e6bee4SJ. Bruce Fields 	*resfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
425c0e6bee4SJ. Bruce Fields 	if (!*resfh)
42659deeb9eSJ. Bruce Fields 		return nfserr_jukebox;
427c0e6bee4SJ. Bruce Fields 	fh_init(*resfh, NFS4_FHSIZE);
428500c2481Szhengbin 	open->op_truncate = false;
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	if (open->op_create) {
43179fb54abSBenny Halevy 		/* FIXME: check session persistence and pnfs flags.
43279fb54abSBenny Halevy 		 * The nfsv4.1 spec requires the following semantics:
43379fb54abSBenny Halevy 		 *
43479fb54abSBenny Halevy 		 * Persistent   | pNFS   | Server REQUIRED | Client Allowed
43579fb54abSBenny Halevy 		 * Reply Cache  | server |                 |
43679fb54abSBenny Halevy 		 * -------------+--------+-----------------+--------------------
43779fb54abSBenny Halevy 		 * no           | no     | EXCLUSIVE4_1    | EXCLUSIVE4_1
43879fb54abSBenny Halevy 		 *              |        |                 | (SHOULD)
43979fb54abSBenny Halevy 		 *              |        | and EXCLUSIVE4  | or EXCLUSIVE4
44079fb54abSBenny Halevy 		 *              |        |                 | (SHOULD NOT)
44179fb54abSBenny Halevy 		 * no           | yes    | EXCLUSIVE4_1    | EXCLUSIVE4_1
44279fb54abSBenny Halevy 		 * yes          | no     | GUARDED4        | GUARDED4
44379fb54abSBenny Halevy 		 * yes          | yes    | GUARDED4        | GUARDED4
44479fb54abSBenny Halevy 		 */
44579fb54abSBenny Halevy 
446880a3a53SJ. Bruce Fields 		current->fs->umask = open->op_umask;
447254454a5SChuck Lever 		status = nfsd4_create_file(rqstp, current_fh, *resfh, open);
448880a3a53SJ. Bruce Fields 		current->fs->umask = 0;
449749997e5SJeff Layton 
45018032ca0SDavid Quigley 		if (!status && open->op_label.len)
451c0e6bee4SJ. Bruce Fields 			nfsd4_security_inode_setsecctx(*resfh, &open->op_label, open->op_bmval);
45218032ca0SDavid Quigley 
45399f88726SJ. Bruce Fields 		/*
454ead8fb8cSKinglong Mee 		 * Following rfc 3530 14.2.16, and rfc 5661 18.16.4
455ead8fb8cSKinglong Mee 		 * use the returned bitmask to indicate which attributes
456ead8fb8cSKinglong Mee 		 * we used to store the verifier:
457749997e5SJeff Layton 		 */
458254454a5SChuck Lever 		if (nfsd4_create_is_exclusive(open->op_createmode) && status == 0)
459ead8fb8cSKinglong Mee 			open->op_bmval[1] |= (FATTR4_WORD1_TIME_ACCESS |
460749997e5SJeff Layton 						FATTR4_WORD1_TIME_MODIFY);
4614335723eSJ. Bruce Fields 	} else
4624335723eSJ. Bruce Fields 		/*
4634335723eSJ. Bruce Fields 		 * Note this may exit with the parent still locked.
4644335723eSJ. Bruce Fields 		 * We will hold the lock until nfsd4_open's final
4654335723eSJ. Bruce Fields 		 * lookup, to prevent renames or unlinks until we've had
4664335723eSJ. Bruce Fields 		 * a chance to an acquire a delegation if appropriate.
4674335723eSJ. Bruce Fields 		 */
4681da177e4SLinus Torvalds 		status = nfsd_lookup(rqstp, current_fh,
4691708e50bSChuck Lever 				     open->op_fname, open->op_fnamelen, *resfh);
470aadab6c6SJ. Bruce Fields 	if (status)
471aadab6c6SJ. Bruce Fields 		goto out;
472c0e6bee4SJ. Bruce Fields 	status = nfsd_check_obj_isreg(*resfh);
473af85852dSJ. Bruce Fields 	if (status)
474af85852dSJ. Bruce Fields 		goto out;
4751da177e4SLinus Torvalds 
4769208faf2SYu Zhiguo 	if (is_create_with_attrs(open) && open->op_acl != NULL)
477c0e6bee4SJ. Bruce Fields 		do_set_nfs4_acl(rqstp, *resfh, open->op_acl, open->op_bmval);
4789208faf2SYu Zhiguo 
479c0e6bee4SJ. Bruce Fields 	nfsd4_set_open_owner_reply_cache(cstate, open, *resfh);
4807007c90fSNeil Brown 	accmode = NFSD_MAY_NOP;
48189f6c336SJ. Bruce Fields 	if (open->op_created ||
48289f6c336SJ. Bruce Fields 			open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
4837007c90fSNeil Brown 		accmode |= NFSD_MAY_OWNER_OVERRIDE;
484c0e6bee4SJ. Bruce Fields 	status = do_open_permission(rqstp, *resfh, open, accmode);
48541fd1e42SJ. Bruce Fields 	set_change_info(&open->op_cinfo, current_fh);
486af85852dSJ. Bruce Fields out:
4871da177e4SLinus Torvalds 	return status;
4881da177e4SLinus Torvalds }
4891da177e4SLinus Torvalds 
490b37ad28bSAl Viro static __be32
491bbc9c36cSJ. Bruce Fields do_open_fhandle(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
4921da177e4SLinus Torvalds {
493bbc9c36cSJ. Bruce Fields 	struct svc_fh *current_fh = &cstate->current_fh;
494b37ad28bSAl Viro 	__be32 status;
4959f415eb2SJ. Bruce Fields 	int accmode = 0;
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	/* We don't know the target directory, and therefore can not
4981da177e4SLinus Torvalds 	* set the change info
4991da177e4SLinus Torvalds 	*/
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
5021da177e4SLinus Torvalds 
503bbc9c36cSJ. Bruce Fields 	nfsd4_set_open_owner_reply_cache(cstate, open, current_fh);
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
5061da177e4SLinus Torvalds 		(open->op_iattr.ia_size == 0);
5079f415eb2SJ. Bruce Fields 	/*
5089f415eb2SJ. Bruce Fields 	 * In the delegation case, the client is telling us about an
5099f415eb2SJ. Bruce Fields 	 * open that it *already* performed locally, some time ago.  We
5109f415eb2SJ. Bruce Fields 	 * should let it succeed now if possible.
5119f415eb2SJ. Bruce Fields 	 *
5129f415eb2SJ. Bruce Fields 	 * In the case of a CLAIM_FH open, on the other hand, the client
5139f415eb2SJ. Bruce Fields 	 * may be counting on us to enforce permissions (the Linux 4.1
5149f415eb2SJ. Bruce Fields 	 * client uses this for normal opens, for example).
5159f415eb2SJ. Bruce Fields 	 */
5169f415eb2SJ. Bruce Fields 	if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH)
5179f415eb2SJ. Bruce Fields 		accmode = NFSD_MAY_OWNER_OVERRIDE;
5181da177e4SLinus Torvalds 
5199f415eb2SJ. Bruce Fields 	status = do_open_permission(rqstp, current_fh, open, accmode);
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	return status;
5221da177e4SLinus Torvalds }
5231da177e4SLinus Torvalds 
52460adfc50SAndy Adamson static void
52560adfc50SAndy Adamson copy_clientid(clientid_t *clid, struct nfsd4_session *session)
52660adfc50SAndy Adamson {
52760adfc50SAndy Adamson 	struct nfsd4_sessionid *sid =
52860adfc50SAndy Adamson 			(struct nfsd4_sessionid *)session->se_sessionid.data;
52960adfc50SAndy Adamson 
53060adfc50SAndy Adamson 	clid->cl_boot = sid->clientid.cl_boot;
53160adfc50SAndy Adamson 	clid->cl_id = sid->clientid.cl_id;
53260adfc50SAndy Adamson }
5331da177e4SLinus Torvalds 
5347191155bSJ.Bruce Fields static __be32
535ca364317SJ.Bruce Fields nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
536eb69853dSChristoph Hellwig 	   union nfsd4_op_u *u)
5371da177e4SLinus Torvalds {
538eb69853dSChristoph Hellwig 	struct nfsd4_open *open = &u->open;
539b37ad28bSAl Viro 	__be32 status;
540c0e6bee4SJ. Bruce Fields 	struct svc_fh *resfh = NULL;
5413320fef1SStanislav Kinsbursky 	struct net *net = SVC_NET(rqstp);
5423320fef1SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
54303f318caSJ. Bruce Fields 	bool reclaim = false;
5446668958fSAndy Adamson 
545fe0750e5SJ. Bruce Fields 	dprintk("NFSD: nfsd4_open filename %.*s op_openowner %p\n",
5461708e50bSChuck Lever 		(int)open->op_fnamelen, open->op_fname,
547fe0750e5SJ. Bruce Fields 		open->op_openowner);
5481da177e4SLinus Torvalds 
549fb70bf12SChuck Lever 	open->op_filp = NULL;
550fb70bf12SChuck Lever 
5511da177e4SLinus Torvalds 	/* This check required by spec. */
5521da177e4SLinus Torvalds 	if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
5531da177e4SLinus Torvalds 		return nfserr_inval;
5541da177e4SLinus Torvalds 
555500c2481Szhengbin 	open->op_created = false;
556ab1350b2SMi Jinlong 	/*
557ab1350b2SMi Jinlong 	 * RFC5661 18.51.3
558ab1350b2SMi Jinlong 	 * Before RECLAIM_COMPLETE done, server should deny new lock
559ab1350b2SMi Jinlong 	 */
560ab1350b2SMi Jinlong 	if (nfsd4_has_session(cstate) &&
561ec59659bSJ. Bruce Fields 	    !test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags) &&
562ab1350b2SMi Jinlong 	    open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
563ab1350b2SMi Jinlong 		return nfserr_grace;
564ab1350b2SMi Jinlong 
56560adfc50SAndy Adamson 	if (nfsd4_has_session(cstate))
56660adfc50SAndy Adamson 		copy_clientid(&open->op_clientid, cstate->session);
56760adfc50SAndy Adamson 
5681da177e4SLinus Torvalds 	/* check seqid for replay. set nfs4_owner */
5696cd22668SKinglong Mee 	status = nfsd4_process_open1(cstate, open, nn);
570a90b061cSAl Viro 	if (status == nfserr_replay_me) {
571fe0750e5SJ. Bruce Fields 		struct nfs4_replay *rp = &open->op_openowner->oo_owner.so_replay;
572ca364317SJ.Bruce Fields 		fh_put(&cstate->current_fh);
573a4773c08SJ. Bruce Fields 		fh_copy_shallow(&cstate->current_fh.fh_handle,
574a4773c08SJ. Bruce Fields 				&rp->rp_openfh);
5758837abcaSMiklos Szeredi 		status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
5761da177e4SLinus Torvalds 		if (status)
5771da177e4SLinus Torvalds 			dprintk("nfsd4_open: replay failed"
5781da177e4SLinus Torvalds 				" restoring previous filehandle\n");
5791da177e4SLinus Torvalds 		else
580a90b061cSAl Viro 			status = nfserr_replay_me;
5811da177e4SLinus Torvalds 	}
5821da177e4SLinus Torvalds 	if (status)
5831da177e4SLinus Torvalds 		goto out;
5849d313b17SJ. Bruce Fields 	if (open->op_xdr_error) {
5859d313b17SJ. Bruce Fields 		status = open->op_xdr_error;
5869d313b17SJ. Bruce Fields 		goto out;
5879d313b17SJ. Bruce Fields 	}
588fb553c0fSJ. Bruce Fields 
5893c8e0316SYu Zhiguo 	status = nfsd4_check_open_attributes(rqstp, cstate, open);
5903c8e0316SYu Zhiguo 	if (status)
5913c8e0316SYu Zhiguo 		goto out;
5923c8e0316SYu Zhiguo 
593fb553c0fSJ. Bruce Fields 	/* Openowner is now set, so sequence id will get bumped.  Now we need
594fb553c0fSJ. Bruce Fields 	 * these checks before we do any creates: */
595cbd0d51aSJ. Bruce Fields 	status = nfserr_grace;
596c87fb4a3SJ. Bruce Fields 	if (opens_in_grace(net) && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
597cbd0d51aSJ. Bruce Fields 		goto out;
598cbd0d51aSJ. Bruce Fields 	status = nfserr_no_grace;
599c87fb4a3SJ. Bruce Fields 	if (!opens_in_grace(net) && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
600cbd0d51aSJ. Bruce Fields 		goto out;
601fb553c0fSJ. Bruce Fields 
6021da177e4SLinus Torvalds 	switch (open->op_claim_type) {
6030dd3c192SNeilBrown 	case NFS4_OPEN_CLAIM_DELEGATE_CUR:
6041da177e4SLinus Torvalds 	case NFS4_OPEN_CLAIM_NULL:
605c0e6bee4SJ. Bruce Fields 		status = do_open_lookup(rqstp, cstate, open, &resfh);
6061da177e4SLinus Torvalds 		if (status)
6071da177e4SLinus Torvalds 			goto out;
6081da177e4SLinus Torvalds 		break;
6091da177e4SLinus Torvalds 	case NFS4_OPEN_CLAIM_PREVIOUS:
6101722b046SJ. Bruce Fields 		status = nfs4_check_open_reclaim(cstate->clp);
6110cf99b91SMi Jinlong 		if (status)
6120cf99b91SMi Jinlong 			goto out;
613ba5378b6SJeff Layton 		open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
61403f318caSJ. Bruce Fields 		reclaim = true;
615df561f66SGustavo A. R. Silva 		fallthrough;
6168b289b2cSJ. Bruce Fields 	case NFS4_OPEN_CLAIM_FH:
6178b289b2cSJ. Bruce Fields 	case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
618bbc9c36cSJ. Bruce Fields 		status = do_open_fhandle(rqstp, cstate, open);
6191da177e4SLinus Torvalds 		if (status)
6201da177e4SLinus Torvalds 			goto out;
621c0e6bee4SJ. Bruce Fields 		resfh = &cstate->current_fh;
6221da177e4SLinus Torvalds 		break;
6238b289b2cSJ. Bruce Fields 	case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
6241da177e4SLinus Torvalds 	case NFS4_OPEN_CLAIM_DELEGATE_PREV:
6251da177e4SLinus Torvalds 		status = nfserr_notsupp;
6261da177e4SLinus Torvalds 		goto out;
6271da177e4SLinus Torvalds 	default:
6281da177e4SLinus Torvalds 		status = nfserr_inval;
6291da177e4SLinus Torvalds 		goto out;
6301da177e4SLinus Torvalds 	}
6317e2ce0ccSChuck Lever 
632c0e6bee4SJ. Bruce Fields 	status = nfsd4_process_open2(rqstp, resfh, open);
633ca3f9acbSChuck Lever 	if (status && open->op_created)
634ca3f9acbSChuck Lever 		pr_warn("nfsd4_process_open2 failed to open newly-created file: status=%u\n",
635b3fbfe0eSJeff Layton 			be32_to_cpu(status));
63603f318caSJ. Bruce Fields 	if (reclaim && !status)
63703f318caSJ. Bruce Fields 		nn->somebody_reclaimed = true;
6381da177e4SLinus Torvalds out:
639fb70bf12SChuck Lever 	if (open->op_filp) {
640fb70bf12SChuck Lever 		fput(open->op_filp);
641fb70bf12SChuck Lever 		open->op_filp = NULL;
642fb70bf12SChuck Lever 	}
643c0e6bee4SJ. Bruce Fields 	if (resfh && resfh != &cstate->current_fh) {
644c0e6bee4SJ. Bruce Fields 		fh_dup2(&cstate->current_fh, resfh);
645c0e6bee4SJ. Bruce Fields 		fh_put(resfh);
646c0e6bee4SJ. Bruce Fields 		kfree(resfh);
647c0e6bee4SJ. Bruce Fields 	}
64842297899SJeff Layton 	nfsd4_cleanup_open_state(cstate, open);
6499411b1d4SJ. Bruce Fields 	nfsd4_bump_seqid(cstate, status);
6501da177e4SLinus Torvalds 	return status;
6511da177e4SLinus Torvalds }
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds /*
6549d313b17SJ. Bruce Fields  * OPEN is the only seqid-mutating operation whose decoding can fail
6559d313b17SJ. Bruce Fields  * with a seqid-mutating error (specifically, decoding of user names in
6569d313b17SJ. Bruce Fields  * the attributes).  Therefore we have to do some processing to look up
6579d313b17SJ. Bruce Fields  * the stateowner so that we can bump the seqid.
6589d313b17SJ. Bruce Fields  */
6599d313b17SJ. Bruce Fields static __be32 nfsd4_open_omfg(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_op *op)
6609d313b17SJ. Bruce Fields {
661eb69853dSChristoph Hellwig 	struct nfsd4_open *open = &op->u.open;
6629d313b17SJ. Bruce Fields 
6639d313b17SJ. Bruce Fields 	if (!seqid_mutating_err(ntohl(op->status)))
6649d313b17SJ. Bruce Fields 		return op->status;
6659d313b17SJ. Bruce Fields 	if (nfsd4_has_session(cstate))
6669d313b17SJ. Bruce Fields 		return op->status;
6679d313b17SJ. Bruce Fields 	open->op_xdr_error = op->status;
668eb69853dSChristoph Hellwig 	return nfsd4_open(rqstp, cstate, &op->u);
6699d313b17SJ. Bruce Fields }
6709d313b17SJ. Bruce Fields 
6719d313b17SJ. Bruce Fields /*
6721da177e4SLinus Torvalds  * filehandle-manipulating ops.
6731da177e4SLinus Torvalds  */
6747191155bSJ.Bruce Fields static __be32
675b591480bSJ.Bruce Fields nfsd4_getfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
676eb69853dSChristoph Hellwig 	    union nfsd4_op_u *u)
6771da177e4SLinus Torvalds {
678eb69853dSChristoph Hellwig 	u->getfh = &cstate->current_fh;
6791da177e4SLinus Torvalds 	return nfs_ok;
6801da177e4SLinus Torvalds }
6811da177e4SLinus Torvalds 
6827191155bSJ.Bruce Fields static __be32
683ca364317SJ.Bruce Fields nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
684eb69853dSChristoph Hellwig 	    union nfsd4_op_u *u)
6851da177e4SLinus Torvalds {
686eb69853dSChristoph Hellwig 	struct nfsd4_putfh *putfh = &u->putfh;
687b9e8638eSOlga Kornievskaia 	__be32 ret;
688eb69853dSChristoph Hellwig 
689ca364317SJ.Bruce Fields 	fh_put(&cstate->current_fh);
690ca364317SJ.Bruce Fields 	cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
691d8b26071SNeilBrown 	memcpy(&cstate->current_fh.fh_handle.fh_raw, putfh->pf_fhval,
692ca364317SJ.Bruce Fields 	       putfh->pf_fhlen);
693b9e8638eSOlga Kornievskaia 	ret = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_BYPASS_GSS);
694b9e8638eSOlga Kornievskaia #ifdef CONFIG_NFSD_V4_2_INTER_SSC
695b9e8638eSOlga Kornievskaia 	if (ret == nfserr_stale && putfh->no_verify) {
696b9e8638eSOlga Kornievskaia 		SET_FH_FLAG(&cstate->current_fh, NFSD4_FH_FOREIGN);
697b9e8638eSOlga Kornievskaia 		ret = 0;
698b9e8638eSOlga Kornievskaia 	}
699b9e8638eSOlga Kornievskaia #endif
700b9e8638eSOlga Kornievskaia 	return ret;
7011da177e4SLinus Torvalds }
7021da177e4SLinus Torvalds 
7037191155bSJ.Bruce Fields static __be32
704b591480bSJ.Bruce Fields nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
705eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
7061da177e4SLinus Torvalds {
707b37ad28bSAl Viro 	__be32 status;
7081da177e4SLinus Torvalds 
709ca364317SJ.Bruce Fields 	fh_put(&cstate->current_fh);
710df547efbSJ. Bruce Fields 	status = exp_pseudoroot(rqstp, &cstate->current_fh);
7111da177e4SLinus Torvalds 	return status;
7121da177e4SLinus Torvalds }
7131da177e4SLinus Torvalds 
7147191155bSJ.Bruce Fields static __be32
715b591480bSJ.Bruce Fields nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
716eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
7171da177e4SLinus Torvalds {
718ca364317SJ.Bruce Fields 	if (!cstate->save_fh.fh_dentry)
7191da177e4SLinus Torvalds 		return nfserr_restorefh;
7201da177e4SLinus Torvalds 
721ca364317SJ.Bruce Fields 	fh_dup2(&cstate->current_fh, &cstate->save_fh);
72251100d2bSOlga Kornievskaia 	if (HAS_CSTATE_FLAG(cstate, SAVED_STATE_ID_FLAG)) {
72337c593c5STigran Mkrtchyan 		memcpy(&cstate->current_stateid, &cstate->save_stateid, sizeof(stateid_t));
72451100d2bSOlga Kornievskaia 		SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
72537c593c5STigran Mkrtchyan 	}
7261da177e4SLinus Torvalds 	return nfs_ok;
7271da177e4SLinus Torvalds }
7281da177e4SLinus Torvalds 
7297191155bSJ.Bruce Fields static __be32
730b591480bSJ.Bruce Fields nfsd4_savefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
731eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
7321da177e4SLinus Torvalds {
733ca364317SJ.Bruce Fields 	fh_dup2(&cstate->save_fh, &cstate->current_fh);
73451100d2bSOlga Kornievskaia 	if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG)) {
73537c593c5STigran Mkrtchyan 		memcpy(&cstate->save_stateid, &cstate->current_stateid, sizeof(stateid_t));
73651100d2bSOlga Kornievskaia 		SET_CSTATE_FLAG(cstate, SAVED_STATE_ID_FLAG);
73737c593c5STigran Mkrtchyan 	}
7381da177e4SLinus Torvalds 	return nfs_ok;
7391da177e4SLinus Torvalds }
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds /*
7421da177e4SLinus Torvalds  * misc nfsv4 ops
7431da177e4SLinus Torvalds  */
7447191155bSJ.Bruce Fields static __be32
745ca364317SJ.Bruce Fields nfsd4_access(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
746eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
7471da177e4SLinus Torvalds {
748eb69853dSChristoph Hellwig 	struct nfsd4_access *access = &u->access;
749c11d7fd1SFrank van der Linden 	u32 access_full;
750eb69853dSChristoph Hellwig 
751c11d7fd1SFrank van der Linden 	access_full = NFS3_ACCESS_FULL;
752c11d7fd1SFrank van der Linden 	if (cstate->minorversion >= 2)
753c11d7fd1SFrank van der Linden 		access_full |= NFS4_ACCESS_XALIST | NFS4_ACCESS_XAREAD |
754c11d7fd1SFrank van der Linden 			       NFS4_ACCESS_XAWRITE;
755c11d7fd1SFrank van der Linden 
756c11d7fd1SFrank van der Linden 	if (access->ac_req_access & ~access_full)
7571da177e4SLinus Torvalds 		return nfserr_inval;
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	access->ac_resp_access = access->ac_req_access;
760ca364317SJ.Bruce Fields 	return nfsd_access(rqstp, &cstate->current_fh, &access->ac_resp_access,
761ca364317SJ.Bruce Fields 			   &access->ac_supported);
7621da177e4SLinus Torvalds }
7631da177e4SLinus Torvalds 
764b9c0ef85SStanislav Kinsbursky static void gen_boot_verifier(nfs4_verifier *verifier, struct net *net)
765ab4684d1SChuck Lever {
76627c438f5STrond Myklebust 	__be32 *verf = (__be32 *)verifier->data;
767ab4684d1SChuck Lever 
76827c438f5STrond Myklebust 	BUILD_BUG_ON(2*sizeof(*verf) != sizeof(verifier->data));
76927c438f5STrond Myklebust 
7703988a578SChuck Lever 	nfsd_copy_write_verifier(verf, net_generic(net, nfsd_net_id));
771ab4684d1SChuck Lever }
772ab4684d1SChuck Lever 
7737191155bSJ.Bruce Fields static __be32
774ca364317SJ.Bruce Fields nfsd4_commit(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
775eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
7761da177e4SLinus Torvalds {
777eb69853dSChristoph Hellwig 	struct nfsd4_commit *commit = &u->commit;
778eb69853dSChristoph Hellwig 
77975c096f7SJ. Bruce Fields 	return nfsd_commit(rqstp, &cstate->current_fh, commit->co_offset,
780524ff1afSTrond Myklebust 			     commit->co_count,
781524ff1afSTrond Myklebust 			     (__be32 *)commit->co_verf.data);
7821da177e4SLinus Torvalds }
7831da177e4SLinus Torvalds 
784b37ad28bSAl Viro static __be32
785ca364317SJ.Bruce Fields nfsd4_create(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
786eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
7871da177e4SLinus Torvalds {
788eb69853dSChristoph Hellwig 	struct nfsd4_create *create = &u->create;
7891da177e4SLinus Torvalds 	struct svc_fh resfh;
790b37ad28bSAl Viro 	__be32 status;
7911da177e4SLinus Torvalds 	dev_t rdev;
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 	fh_init(&resfh, NFS4_FHSIZE);
7941da177e4SLinus Torvalds 
795fa08139dSJ. Bruce Fields 	status = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_NOP);
7961da177e4SLinus Torvalds 	if (status)
7971da177e4SLinus Torvalds 		return status;
7981da177e4SLinus Torvalds 
7993c8e0316SYu Zhiguo 	status = check_attr_support(rqstp, cstate, create->cr_bmval,
8003c8e0316SYu Zhiguo 				    nfsd_attrmask);
8013c8e0316SYu Zhiguo 	if (status)
8023c8e0316SYu Zhiguo 		return status;
8033c8e0316SYu Zhiguo 
804880a3a53SJ. Bruce Fields 	current->fs->umask = create->cr_umask;
8051da177e4SLinus Torvalds 	switch (create->cr_type) {
8061da177e4SLinus Torvalds 	case NF4LNK:
807ca364317SJ.Bruce Fields 		status = nfsd_symlink(rqstp, &cstate->current_fh,
808ca364317SJ.Bruce Fields 				      create->cr_name, create->cr_namelen,
8091e444f5bSKinglong Mee 				      create->cr_data, &resfh);
8101da177e4SLinus Torvalds 		break;
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 	case NF4BLK:
813880a3a53SJ. Bruce Fields 		status = nfserr_inval;
8141da177e4SLinus Torvalds 		rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
8151da177e4SLinus Torvalds 		if (MAJOR(rdev) != create->cr_specdata1 ||
8161da177e4SLinus Torvalds 		    MINOR(rdev) != create->cr_specdata2)
817880a3a53SJ. Bruce Fields 			goto out_umask;
818ca364317SJ.Bruce Fields 		status = nfsd_create(rqstp, &cstate->current_fh,
819ca364317SJ.Bruce Fields 				     create->cr_name, create->cr_namelen,
820ca364317SJ.Bruce Fields 				     &create->cr_iattr, S_IFBLK, rdev, &resfh);
8211da177e4SLinus Torvalds 		break;
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	case NF4CHR:
824880a3a53SJ. Bruce Fields 		status = nfserr_inval;
8251da177e4SLinus Torvalds 		rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
8261da177e4SLinus Torvalds 		if (MAJOR(rdev) != create->cr_specdata1 ||
8271da177e4SLinus Torvalds 		    MINOR(rdev) != create->cr_specdata2)
828880a3a53SJ. Bruce Fields 			goto out_umask;
829ca364317SJ.Bruce Fields 		status = nfsd_create(rqstp, &cstate->current_fh,
830ca364317SJ.Bruce Fields 				     create->cr_name, create->cr_namelen,
831ca364317SJ.Bruce Fields 				     &create->cr_iattr, S_IFCHR, rdev, &resfh);
8321da177e4SLinus Torvalds 		break;
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 	case NF4SOCK:
835ca364317SJ.Bruce Fields 		status = nfsd_create(rqstp, &cstate->current_fh,
836ca364317SJ.Bruce Fields 				     create->cr_name, create->cr_namelen,
837ca364317SJ.Bruce Fields 				     &create->cr_iattr, S_IFSOCK, 0, &resfh);
8381da177e4SLinus Torvalds 		break;
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 	case NF4FIFO:
841ca364317SJ.Bruce Fields 		status = nfsd_create(rqstp, &cstate->current_fh,
842ca364317SJ.Bruce Fields 				     create->cr_name, create->cr_namelen,
843ca364317SJ.Bruce Fields 				     &create->cr_iattr, S_IFIFO, 0, &resfh);
8441da177e4SLinus Torvalds 		break;
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds 	case NF4DIR:
8471da177e4SLinus Torvalds 		create->cr_iattr.ia_valid &= ~ATTR_SIZE;
848ca364317SJ.Bruce Fields 		status = nfsd_create(rqstp, &cstate->current_fh,
849ca364317SJ.Bruce Fields 				     create->cr_name, create->cr_namelen,
850ca364317SJ.Bruce Fields 				     &create->cr_iattr, S_IFDIR, 0, &resfh);
8511da177e4SLinus Torvalds 		break;
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 	default:
8541da177e4SLinus Torvalds 		status = nfserr_badtype;
8551da177e4SLinus Torvalds 	}
8561da177e4SLinus Torvalds 
8579208faf2SYu Zhiguo 	if (status)
8589208faf2SYu Zhiguo 		goto out;
8599208faf2SYu Zhiguo 
86018032ca0SDavid Quigley 	if (create->cr_label.len)
86118032ca0SDavid Quigley 		nfsd4_security_inode_setsecctx(&resfh, &create->cr_label, create->cr_bmval);
86218032ca0SDavid Quigley 
8639208faf2SYu Zhiguo 	if (create->cr_acl != NULL)
8649208faf2SYu Zhiguo 		do_set_nfs4_acl(rqstp, &resfh, create->cr_acl,
8659208faf2SYu Zhiguo 				create->cr_bmval);
8669208faf2SYu Zhiguo 
867ca364317SJ.Bruce Fields 	fh_unlock(&cstate->current_fh);
868ca364317SJ.Bruce Fields 	set_change_info(&create->cr_cinfo, &cstate->current_fh);
869ca364317SJ.Bruce Fields 	fh_dup2(&cstate->current_fh, &resfh);
8709208faf2SYu Zhiguo out:
8711da177e4SLinus Torvalds 	fh_put(&resfh);
872880a3a53SJ. Bruce Fields out_umask:
873880a3a53SJ. Bruce Fields 	current->fs->umask = 0;
8741da177e4SLinus Torvalds 	return status;
8751da177e4SLinus Torvalds }
8761da177e4SLinus Torvalds 
8777191155bSJ.Bruce Fields static __be32
878ca364317SJ.Bruce Fields nfsd4_getattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
879eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
8801da177e4SLinus Torvalds {
881eb69853dSChristoph Hellwig 	struct nfsd4_getattr *getattr = &u->getattr;
882b37ad28bSAl Viro 	__be32 status;
8831da177e4SLinus Torvalds 
8848837abcaSMiklos Szeredi 	status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
8851da177e4SLinus Torvalds 	if (status)
8861da177e4SLinus Torvalds 		return status;
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
8891da177e4SLinus Torvalds 		return nfserr_inval;
8901da177e4SLinus Torvalds 
891916d2d84SJ. Bruce Fields 	getattr->ga_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
892916d2d84SJ. Bruce Fields 	getattr->ga_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
893916d2d84SJ. Bruce Fields 	getattr->ga_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
8941da177e4SLinus Torvalds 
895ca364317SJ.Bruce Fields 	getattr->ga_fhp = &cstate->current_fh;
8961da177e4SLinus Torvalds 	return nfs_ok;
8971da177e4SLinus Torvalds }
8981da177e4SLinus Torvalds 
8997191155bSJ.Bruce Fields static __be32
900ca364317SJ.Bruce Fields nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
901eb69853dSChristoph Hellwig 	   union nfsd4_op_u *u)
9021da177e4SLinus Torvalds {
903eb69853dSChristoph Hellwig 	struct nfsd4_link *link = &u->link;
90495424460SJ. Bruce Fields 	__be32 status;
9051da177e4SLinus Torvalds 
906ca364317SJ.Bruce Fields 	status = nfsd_link(rqstp, &cstate->current_fh,
907ca364317SJ.Bruce Fields 			   link->li_name, link->li_namelen, &cstate->save_fh);
9081da177e4SLinus Torvalds 	if (!status)
909ca364317SJ.Bruce Fields 		set_change_info(&link->li_cinfo, &cstate->current_fh);
9101da177e4SLinus Torvalds 	return status;
9111da177e4SLinus Torvalds }
9121da177e4SLinus Torvalds 
9130ff7ab46SJ. Bruce Fields static __be32 nfsd4_do_lookupp(struct svc_rqst *rqstp, struct svc_fh *fh)
9141da177e4SLinus Torvalds {
9151da177e4SLinus Torvalds 	struct svc_fh tmp_fh;
916b37ad28bSAl Viro 	__be32 ret;
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	fh_init(&tmp_fh, NFS4_FHSIZE);
919df547efbSJ. Bruce Fields 	ret = exp_pseudoroot(rqstp, &tmp_fh);
920df547efbSJ. Bruce Fields 	if (ret)
9211da177e4SLinus Torvalds 		return ret;
9220ff7ab46SJ. Bruce Fields 	if (tmp_fh.fh_dentry == fh->fh_dentry) {
9231da177e4SLinus Torvalds 		fh_put(&tmp_fh);
9241da177e4SLinus Torvalds 		return nfserr_noent;
9251da177e4SLinus Torvalds 	}
9261da177e4SLinus Torvalds 	fh_put(&tmp_fh);
9270ff7ab46SJ. Bruce Fields 	return nfsd_lookup(rqstp, fh, "..", 2, fh);
9280ff7ab46SJ. Bruce Fields }
9290ff7ab46SJ. Bruce Fields 
9300ff7ab46SJ. Bruce Fields static __be32
9310ff7ab46SJ. Bruce Fields nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
932eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
9330ff7ab46SJ. Bruce Fields {
9340ff7ab46SJ. Bruce Fields 	return nfsd4_do_lookupp(rqstp, &cstate->current_fh);
9351da177e4SLinus Torvalds }
9361da177e4SLinus Torvalds 
9377191155bSJ.Bruce Fields static __be32
938ca364317SJ.Bruce Fields nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
939eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
9401da177e4SLinus Torvalds {
941ca364317SJ.Bruce Fields 	return nfsd_lookup(rqstp, &cstate->current_fh,
942eb69853dSChristoph Hellwig 			   u->lookup.lo_name, u->lookup.lo_len,
943ca364317SJ.Bruce Fields 			   &cstate->current_fh);
9441da177e4SLinus Torvalds }
9451da177e4SLinus Torvalds 
9467191155bSJ.Bruce Fields static __be32
947ca364317SJ.Bruce Fields nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
948eb69853dSChristoph Hellwig 	   union nfsd4_op_u *u)
9491da177e4SLinus Torvalds {
950eb69853dSChristoph Hellwig 	struct nfsd4_read *read = &u->read;
951b37ad28bSAl Viro 	__be32 status;
9521da177e4SLinus Torvalds 
9535c4583b2SJeff Layton 	read->rd_nf = NULL;
9541da177e4SLinus Torvalds 
95587c5942eSChuck Lever 	trace_nfsd_read_start(rqstp, &cstate->current_fh,
95687c5942eSChuck Lever 			      read->rd_offset, read->rd_length);
95787c5942eSChuck Lever 
9580cb4d23aSChuck Lever 	read->rd_length = min_t(u32, read->rd_length, svc_max_payload(rqstp));
9590cb4d23aSChuck Lever 	if (read->rd_offset > (u64)OFFSET_MAX)
9600cb4d23aSChuck Lever 		read->rd_offset = (u64)OFFSET_MAX;
9610cb4d23aSChuck Lever 	if (read->rd_offset + read->rd_length > (u64)OFFSET_MAX)
9620cb4d23aSChuck Lever 		read->rd_length = (u64)OFFSET_MAX - read->rd_offset;
9630cb4d23aSChuck Lever 
9649b3234b9SJ. Bruce Fields 	/*
9659b3234b9SJ. Bruce Fields 	 * If we do a zero copy read, then a client will see read data
9669b3234b9SJ. Bruce Fields 	 * that reflects the state of the file *after* performing the
9679b3234b9SJ. Bruce Fields 	 * following compound.
9689b3234b9SJ. Bruce Fields 	 *
9699b3234b9SJ. Bruce Fields 	 * To ensure proper ordering, we therefore turn off zero copy if
9709b3234b9SJ. Bruce Fields 	 * the client wants us to do more in this compound:
9719b3234b9SJ. Bruce Fields 	 */
9729b3234b9SJ. Bruce Fields 	if (!nfsd4_last_compound_op(rqstp))
97328df0988SChuck Lever 		__clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
9749b3234b9SJ. Bruce Fields 
9751da177e4SLinus Torvalds 	/* check stateid */
976aa0d6aedSAnna Schumaker 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
977aa0d6aedSAnna Schumaker 					&read->rd_stateid, RD_STATE,
978624322f1SOlga Kornievskaia 					&read->rd_nf, NULL);
979af90f707SChristoph Hellwig 	if (status) {
9801da177e4SLinus Torvalds 		dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
9811da177e4SLinus Torvalds 		goto out;
9821da177e4SLinus Torvalds 	}
9831da177e4SLinus Torvalds 	status = nfs_ok;
9841da177e4SLinus Torvalds out:
9851da177e4SLinus Torvalds 	read->rd_rqstp = rqstp;
986ca364317SJ.Bruce Fields 	read->rd_fhp = &cstate->current_fh;
9871da177e4SLinus Torvalds 	return status;
9881da177e4SLinus Torvalds }
9891da177e4SLinus Torvalds 
99034b1744cSJ. Bruce Fields 
99134b1744cSJ. Bruce Fields static void
99234b1744cSJ. Bruce Fields nfsd4_read_release(union nfsd4_op_u *u)
99334b1744cSJ. Bruce Fields {
9945c4583b2SJeff Layton 	if (u->read.rd_nf)
9955c4583b2SJeff Layton 		nfsd_file_put(u->read.rd_nf);
99687c5942eSChuck Lever 	trace_nfsd_read_done(u->read.rd_rqstp, u->read.rd_fhp,
99787c5942eSChuck Lever 			     u->read.rd_offset, u->read.rd_length);
99834b1744cSJ. Bruce Fields }
99934b1744cSJ. Bruce Fields 
10007191155bSJ.Bruce Fields static __be32
1001ca364317SJ.Bruce Fields nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1002eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
10031da177e4SLinus Torvalds {
1004eb69853dSChristoph Hellwig 	struct nfsd4_readdir *readdir = &u->readdir;
10051da177e4SLinus Torvalds 	u64 cookie = readdir->rd_cookie;
10061da177e4SLinus Torvalds 	static const nfs4_verifier zeroverf;
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 	/* no need to check permission - this will be done in nfsd_readdir() */
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 	if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
10111da177e4SLinus Torvalds 		return nfserr_inval;
10121da177e4SLinus Torvalds 
1013916d2d84SJ. Bruce Fields 	readdir->rd_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
1014916d2d84SJ. Bruce Fields 	readdir->rd_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
1015916d2d84SJ. Bruce Fields 	readdir->rd_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
10161da177e4SLinus Torvalds 
1017832023bfSBernd Schubert 	if ((cookie == 1) || (cookie == 2) ||
10181da177e4SLinus Torvalds 	    (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
10191da177e4SLinus Torvalds 		return nfserr_bad_cookie;
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds 	readdir->rd_rqstp = rqstp;
1022ca364317SJ.Bruce Fields 	readdir->rd_fhp = &cstate->current_fh;
10231da177e4SLinus Torvalds 	return nfs_ok;
10241da177e4SLinus Torvalds }
10251da177e4SLinus Torvalds 
10267191155bSJ.Bruce Fields static __be32
1027ca364317SJ.Bruce Fields nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1028eb69853dSChristoph Hellwig 	       union nfsd4_op_u *u)
10291da177e4SLinus Torvalds {
1030eb69853dSChristoph Hellwig 	u->readlink.rl_rqstp = rqstp;
1031eb69853dSChristoph Hellwig 	u->readlink.rl_fhp = &cstate->current_fh;
10321da177e4SLinus Torvalds 	return nfs_ok;
10331da177e4SLinus Torvalds }
10341da177e4SLinus Torvalds 
10357191155bSJ.Bruce Fields static __be32
1036ca364317SJ.Bruce Fields nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1037eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
10381da177e4SLinus Torvalds {
1039eb69853dSChristoph Hellwig 	struct nfsd4_remove *remove = &u->remove;
1040b37ad28bSAl Viro 	__be32 status;
10411da177e4SLinus Torvalds 
1042c87fb4a3SJ. Bruce Fields 	if (opens_in_grace(SVC_NET(rqstp)))
1043c815afc7SNeilBrown 		return nfserr_grace;
1044ca364317SJ.Bruce Fields 	status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
1045ca364317SJ.Bruce Fields 			     remove->rm_name, remove->rm_namelen);
10461da177e4SLinus Torvalds 	if (!status) {
1047ca364317SJ.Bruce Fields 		fh_unlock(&cstate->current_fh);
1048ca364317SJ.Bruce Fields 		set_change_info(&remove->rm_cinfo, &cstate->current_fh);
10491da177e4SLinus Torvalds 	}
10501da177e4SLinus Torvalds 	return status;
10511da177e4SLinus Torvalds }
10521da177e4SLinus Torvalds 
10537191155bSJ.Bruce Fields static __be32
1054ca364317SJ.Bruce Fields nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1055eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
10561da177e4SLinus Torvalds {
1057eb69853dSChristoph Hellwig 	struct nfsd4_rename *rename = &u->rename;
105895424460SJ. Bruce Fields 	__be32 status;
10591da177e4SLinus Torvalds 
1060f8f71d00SJ. Bruce Fields 	if (opens_in_grace(SVC_NET(rqstp)))
1061c815afc7SNeilBrown 		return nfserr_grace;
1062ca364317SJ.Bruce Fields 	status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname,
1063ca364317SJ.Bruce Fields 			     rename->rn_snamelen, &cstate->current_fh,
10641da177e4SLinus Torvalds 			     rename->rn_tname, rename->rn_tnamelen);
10652a6cf944SJ. Bruce Fields 	if (status)
10662a6cf944SJ. Bruce Fields 		return status;
1067ca364317SJ.Bruce Fields 	set_change_info(&rename->rn_sinfo, &cstate->current_fh);
1068ca364317SJ.Bruce Fields 	set_change_info(&rename->rn_tinfo, &cstate->save_fh);
10692a6cf944SJ. Bruce Fields 	return nfs_ok;
10701da177e4SLinus Torvalds }
10711da177e4SLinus Torvalds 
10727191155bSJ.Bruce Fields static __be32
1073dcb488a3SAndy Adamson nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1074eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
1075dcb488a3SAndy Adamson {
1076eb69853dSChristoph Hellwig 	struct nfsd4_secinfo *secinfo = &u->secinfo;
1077dcb488a3SAndy Adamson 	struct svc_export *exp;
1078dcb488a3SAndy Adamson 	struct dentry *dentry;
1079dcb488a3SAndy Adamson 	__be32 err;
1080dcb488a3SAndy Adamson 
108129a78a3eSJ. Bruce Fields 	err = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_EXEC);
108229a78a3eSJ. Bruce Fields 	if (err)
108329a78a3eSJ. Bruce Fields 		return err;
1084dcb488a3SAndy Adamson 	err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
1085dcb488a3SAndy Adamson 				    secinfo->si_name, secinfo->si_namelen,
1086dcb488a3SAndy Adamson 				    &exp, &dentry);
1087dcb488a3SAndy Adamson 	if (err)
1088dcb488a3SAndy Adamson 		return err;
10892f6fc056SJ. Bruce Fields 	fh_unlock(&cstate->current_fh);
10902b0143b5SDavid Howells 	if (d_really_is_negative(dentry)) {
1091dcb488a3SAndy Adamson 		exp_put(exp);
1092dcb488a3SAndy Adamson 		err = nfserr_noent;
1093dcb488a3SAndy Adamson 	} else
1094dcb488a3SAndy Adamson 		secinfo->si_exp = exp;
1095dcb488a3SAndy Adamson 	dput(dentry);
109656560b9aSJ. Bruce Fields 	if (cstate->minorversion)
109756560b9aSJ. Bruce Fields 		/* See rfc 5661 section 2.6.3.1.1.8 */
109856560b9aSJ. Bruce Fields 		fh_put(&cstate->current_fh);
1099dcb488a3SAndy Adamson 	return err;
1100dcb488a3SAndy Adamson }
1101dcb488a3SAndy Adamson 
1102dcb488a3SAndy Adamson static __be32
110304f4ad16SJ. Bruce Fields nfsd4_secinfo_no_name(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1104eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
110504f4ad16SJ. Bruce Fields {
110604f4ad16SJ. Bruce Fields 	__be32 err;
110704f4ad16SJ. Bruce Fields 
1108eb69853dSChristoph Hellwig 	switch (u->secinfo_no_name.sin_style) {
110904f4ad16SJ. Bruce Fields 	case NFS4_SECINFO_STYLE4_CURRENT_FH:
111004f4ad16SJ. Bruce Fields 		break;
111104f4ad16SJ. Bruce Fields 	case NFS4_SECINFO_STYLE4_PARENT:
111204f4ad16SJ. Bruce Fields 		err = nfsd4_do_lookupp(rqstp, &cstate->current_fh);
111304f4ad16SJ. Bruce Fields 		if (err)
111404f4ad16SJ. Bruce Fields 			return err;
111504f4ad16SJ. Bruce Fields 		break;
111604f4ad16SJ. Bruce Fields 	default:
111704f4ad16SJ. Bruce Fields 		return nfserr_inval;
111804f4ad16SJ. Bruce Fields 	}
1119bf18f163SKinglong Mee 
1120eb69853dSChristoph Hellwig 	u->secinfo_no_name.sin_exp = exp_get(cstate->current_fh.fh_export);
112104f4ad16SJ. Bruce Fields 	fh_put(&cstate->current_fh);
112204f4ad16SJ. Bruce Fields 	return nfs_ok;
112304f4ad16SJ. Bruce Fields }
112404f4ad16SJ. Bruce Fields 
112534b1744cSJ. Bruce Fields static void
112634b1744cSJ. Bruce Fields nfsd4_secinfo_release(union nfsd4_op_u *u)
112734b1744cSJ. Bruce Fields {
112834b1744cSJ. Bruce Fields 	if (u->secinfo.si_exp)
112934b1744cSJ. Bruce Fields 		exp_put(u->secinfo.si_exp);
113034b1744cSJ. Bruce Fields }
113134b1744cSJ. Bruce Fields 
1132ec572b9eSEryu Guan static void
1133ec572b9eSEryu Guan nfsd4_secinfo_no_name_release(union nfsd4_op_u *u)
1134ec572b9eSEryu Guan {
1135ec572b9eSEryu Guan 	if (u->secinfo_no_name.sin_exp)
1136ec572b9eSEryu Guan 		exp_put(u->secinfo_no_name.sin_exp);
1137ec572b9eSEryu Guan }
1138ec572b9eSEryu Guan 
113904f4ad16SJ. Bruce Fields static __be32
1140ca364317SJ.Bruce Fields nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1141eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
11421da177e4SLinus Torvalds {
1143eb69853dSChristoph Hellwig 	struct nfsd4_setattr *setattr = &u->setattr;
1144b37ad28bSAl Viro 	__be32 status = nfs_ok;
114596f6f985SAl Viro 	int err;
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds 	if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
1148af90f707SChristoph Hellwig 		status = nfs4_preprocess_stateid_op(rqstp, cstate,
1149aa0d6aedSAnna Schumaker 				&cstate->current_fh, &setattr->sa_stateid,
1150624322f1SOlga Kornievskaia 				WR_STATE, NULL, NULL);
1151375c5547SJ. Bruce Fields 		if (status) {
11523e3b4800SGreg Banks 			dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
1153375c5547SJ. Bruce Fields 			return status;
1154375c5547SJ. Bruce Fields 		}
11551da177e4SLinus Torvalds 	}
115696f6f985SAl Viro 	err = fh_want_write(&cstate->current_fh);
115796f6f985SAl Viro 	if (err)
115896f6f985SAl Viro 		return nfserrno(err);
11591da177e4SLinus Torvalds 	status = nfs_ok;
11603c8e0316SYu Zhiguo 
11613c8e0316SYu Zhiguo 	status = check_attr_support(rqstp, cstate, setattr->sa_bmval,
11623c8e0316SYu Zhiguo 				    nfsd_attrmask);
11633c8e0316SYu Zhiguo 	if (status)
11643c8e0316SYu Zhiguo 		goto out;
11653c8e0316SYu Zhiguo 
11661da177e4SLinus Torvalds 	if (setattr->sa_acl != NULL)
1167ca364317SJ.Bruce Fields 		status = nfsd4_set_nfs4_acl(rqstp, &cstate->current_fh,
1168ca364317SJ.Bruce Fields 					    setattr->sa_acl);
11691da177e4SLinus Torvalds 	if (status)
117018f335afSDave Hansen 		goto out;
117118032ca0SDavid Quigley 	if (setattr->sa_label.len)
117218032ca0SDavid Quigley 		status = nfsd4_set_nfs4_label(rqstp, &cstate->current_fh,
117318032ca0SDavid Quigley 				&setattr->sa_label);
117418032ca0SDavid Quigley 	if (status)
117518032ca0SDavid Quigley 		goto out;
1176ca364317SJ.Bruce Fields 	status = nfsd_setattr(rqstp, &cstate->current_fh, &setattr->sa_iattr,
11772a1aa489SArnd Bergmann 				0, (time64_t)0);
117818f335afSDave Hansen out:
1179bad0dcffSAl Viro 	fh_drop_write(&cstate->current_fh);
11801da177e4SLinus Torvalds 	return status;
11811da177e4SLinus Torvalds }
11821da177e4SLinus Torvalds 
11837191155bSJ.Bruce Fields static __be32
1184ca364317SJ.Bruce Fields nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1185eb69853dSChristoph Hellwig 	    union nfsd4_op_u *u)
11861da177e4SLinus Torvalds {
1187eb69853dSChristoph Hellwig 	struct nfsd4_write *write = &u->write;
11881da177e4SLinus Torvalds 	stateid_t *stateid = &write->wr_stateid;
11895c4583b2SJeff Layton 	struct nfsd_file *nf = NULL;
1190b37ad28bSAl Viro 	__be32 status = nfs_ok;
119131dec253SDavid Shaw 	unsigned long cnt;
1192ffe1137bSJ. Bruce Fields 	int nvecs;
11931da177e4SLinus Torvalds 
11946260d9a5SChuck Lever 	if (write->wr_offset > (u64)OFFSET_MAX ||
11956260d9a5SChuck Lever 	    write->wr_offset + write->wr_buflen > (u64)OFFSET_MAX)
11966260d9a5SChuck Lever 		return nfserr_fbig;
11971da177e4SLinus Torvalds 
1198d890be15SChuck Lever 	cnt = write->wr_buflen;
1199d890be15SChuck Lever 	trace_nfsd_write_start(rqstp, &cstate->current_fh,
1200d890be15SChuck Lever 			       write->wr_offset, cnt);
1201aa0d6aedSAnna Schumaker 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1202624322f1SOlga Kornievskaia 						stateid, WR_STATE, &nf, NULL);
1203375c5547SJ. Bruce Fields 	if (status) {
1204375c5547SJ. Bruce Fields 		dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
1205375c5547SJ. Bruce Fields 		return status;
1206375c5547SJ. Bruce Fields 	}
1207375c5547SJ. Bruce Fields 
12081da177e4SLinus Torvalds 	write->wr_how_written = write->wr_stable_how;
12091da177e4SLinus Torvalds 
1210dae9a6caSChuck Lever 	nvecs = svc_fill_write_vector(rqstp, &write->wr_payload);
1211ffe1137bSJ. Bruce Fields 	WARN_ON_ONCE(nvecs > ARRAY_SIZE(rqstp->rq_vec));
1212ffe1137bSJ. Bruce Fields 
121316f8f894STrond Myklebust 	status = nfsd_vfs_write(rqstp, &cstate->current_fh, nf,
1214af90f707SChristoph Hellwig 				write->wr_offset, rqstp->rq_vec, nvecs, &cnt,
121519e0663fSTrond Myklebust 				write->wr_how_written,
121619e0663fSTrond Myklebust 				(__be32 *)write->wr_verifier.data);
12175c4583b2SJeff Layton 	nfsd_file_put(nf);
12181da177e4SLinus Torvalds 
121931dec253SDavid Shaw 	write->wr_bytes_written = cnt;
1220d890be15SChuck Lever 	trace_nfsd_write_done(rqstp, &cstate->current_fh,
1221d890be15SChuck Lever 			      write->wr_offset, cnt);
12221da177e4SLinus Torvalds 	return status;
12231da177e4SLinus Torvalds }
12241da177e4SLinus Torvalds 
122524bab491SAnna Schumaker static __be32
122629ae7f9dSAnna Schumaker nfsd4_verify_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
12275c4583b2SJeff Layton 		  stateid_t *src_stateid, struct nfsd_file **src,
12285c4583b2SJeff Layton 		  stateid_t *dst_stateid, struct nfsd_file **dst)
1229ffa0160aSChristoph Hellwig {
1230ffa0160aSChristoph Hellwig 	__be32 status;
1231ffa0160aSChristoph Hellwig 
123201310bb7SScott Mayhew 	if (!cstate->save_fh.fh_dentry)
123301310bb7SScott Mayhew 		return nfserr_nofilehandle;
123401310bb7SScott Mayhew 
1235ffa0160aSChristoph Hellwig 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->save_fh,
1236624322f1SOlga Kornievskaia 					    src_stateid, RD_STATE, src, NULL);
1237ffa0160aSChristoph Hellwig 	if (status) {
1238ffa0160aSChristoph Hellwig 		dprintk("NFSD: %s: couldn't process src stateid!\n", __func__);
1239ffa0160aSChristoph Hellwig 		goto out;
1240ffa0160aSChristoph Hellwig 	}
1241ffa0160aSChristoph Hellwig 
1242ffa0160aSChristoph Hellwig 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1243624322f1SOlga Kornievskaia 					    dst_stateid, WR_STATE, dst, NULL);
1244ffa0160aSChristoph Hellwig 	if (status) {
1245ffa0160aSChristoph Hellwig 		dprintk("NFSD: %s: couldn't process dst stateid!\n", __func__);
1246ffa0160aSChristoph Hellwig 		goto out_put_src;
1247ffa0160aSChristoph Hellwig 	}
1248ffa0160aSChristoph Hellwig 
1249ffa0160aSChristoph Hellwig 	/* fix up for NFS-specific error code */
12505c4583b2SJeff Layton 	if (!S_ISREG(file_inode((*src)->nf_file)->i_mode) ||
12515c4583b2SJeff Layton 	    !S_ISREG(file_inode((*dst)->nf_file)->i_mode)) {
1252ffa0160aSChristoph Hellwig 		status = nfserr_wrong_type;
1253ffa0160aSChristoph Hellwig 		goto out_put_dst;
1254ffa0160aSChristoph Hellwig 	}
1255ffa0160aSChristoph Hellwig 
125629ae7f9dSAnna Schumaker out:
125729ae7f9dSAnna Schumaker 	return status;
125829ae7f9dSAnna Schumaker out_put_dst:
12595c4583b2SJeff Layton 	nfsd_file_put(*dst);
126029ae7f9dSAnna Schumaker out_put_src:
12615c4583b2SJeff Layton 	nfsd_file_put(*src);
126229ae7f9dSAnna Schumaker 	goto out;
126329ae7f9dSAnna Schumaker }
126429ae7f9dSAnna Schumaker 
126529ae7f9dSAnna Schumaker static __be32
126629ae7f9dSAnna Schumaker nfsd4_clone(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1267eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
126829ae7f9dSAnna Schumaker {
1269eb69853dSChristoph Hellwig 	struct nfsd4_clone *clone = &u->clone;
12705c4583b2SJeff Layton 	struct nfsd_file *src, *dst;
127129ae7f9dSAnna Schumaker 	__be32 status;
127229ae7f9dSAnna Schumaker 
127329ae7f9dSAnna Schumaker 	status = nfsd4_verify_copy(rqstp, cstate, &clone->cl_src_stateid, &src,
127429ae7f9dSAnna Schumaker 				   &clone->cl_dst_stateid, &dst);
127529ae7f9dSAnna Schumaker 	if (status)
127629ae7f9dSAnna Schumaker 		goto out;
127729ae7f9dSAnna Schumaker 
1278a2f4c3faSTrond Myklebust 	status = nfsd4_clone_file_range(rqstp, src, clone->cl_src_pos,
1279b66ae6ddSTrond Myklebust 			dst, clone->cl_dst_pos, clone->cl_count,
1280a25e3726STrond Myklebust 			EX_ISSYNC(cstate->current_fh.fh_export));
1281ffa0160aSChristoph Hellwig 
12825c4583b2SJeff Layton 	nfsd_file_put(dst);
12835c4583b2SJeff Layton 	nfsd_file_put(src);
1284ffa0160aSChristoph Hellwig out:
1285ffa0160aSChristoph Hellwig 	return status;
1286ffa0160aSChristoph Hellwig }
1287ffa0160aSChristoph Hellwig 
12888ea6e2c9SChuck Lever static void nfs4_put_copy(struct nfsd4_copy *copy)
1289e0639dc5SOlga Kornievskaia {
1290e0639dc5SOlga Kornievskaia 	if (!refcount_dec_and_test(&copy->refcount))
1291e0639dc5SOlga Kornievskaia 		return;
129287689df6SChuck Lever 	kfree(copy->cp_src);
1293e0639dc5SOlga Kornievskaia 	kfree(copy);
1294e0639dc5SOlga Kornievskaia }
1295e0639dc5SOlga Kornievskaia 
1296e0639dc5SOlga Kornievskaia static void nfsd4_stop_copy(struct nfsd4_copy *copy)
1297e0639dc5SOlga Kornievskaia {
1298*1913cdf5SChuck Lever 	if (!test_and_set_bit(NFSD4_COPY_F_STOPPED, &copy->cp_flags))
1299e0639dc5SOlga Kornievskaia 		kthread_stop(copy->copy_task);
1300e0639dc5SOlga Kornievskaia 	nfs4_put_copy(copy);
1301e0639dc5SOlga Kornievskaia }
1302e0639dc5SOlga Kornievskaia 
1303e0639dc5SOlga Kornievskaia static struct nfsd4_copy *nfsd4_get_copy(struct nfs4_client *clp)
1304e0639dc5SOlga Kornievskaia {
1305e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy = NULL;
1306e0639dc5SOlga Kornievskaia 
1307e0639dc5SOlga Kornievskaia 	spin_lock(&clp->async_lock);
1308e0639dc5SOlga Kornievskaia 	if (!list_empty(&clp->async_copies)) {
1309e0639dc5SOlga Kornievskaia 		copy = list_first_entry(&clp->async_copies, struct nfsd4_copy,
1310e0639dc5SOlga Kornievskaia 					copies);
1311e0639dc5SOlga Kornievskaia 		refcount_inc(&copy->refcount);
1312e0639dc5SOlga Kornievskaia 	}
1313e0639dc5SOlga Kornievskaia 	spin_unlock(&clp->async_lock);
1314e0639dc5SOlga Kornievskaia 	return copy;
1315e0639dc5SOlga Kornievskaia }
1316e0639dc5SOlga Kornievskaia 
1317e0639dc5SOlga Kornievskaia void nfsd4_shutdown_copy(struct nfs4_client *clp)
1318e0639dc5SOlga Kornievskaia {
1319e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy;
1320e0639dc5SOlga Kornievskaia 
1321e0639dc5SOlga Kornievskaia 	while ((copy = nfsd4_get_copy(clp)) != NULL)
1322e0639dc5SOlga Kornievskaia 		nfsd4_stop_copy(copy);
1323e0639dc5SOlga Kornievskaia }
1324ce0887acSOlga Kornievskaia #ifdef CONFIG_NFSD_V4_2_INTER_SSC
1325ce0887acSOlga Kornievskaia 
1326ce0887acSOlga Kornievskaia extern struct file *nfs42_ssc_open(struct vfsmount *ss_mnt,
1327ce0887acSOlga Kornievskaia 				   struct nfs_fh *src_fh,
1328ce0887acSOlga Kornievskaia 				   nfs4_stateid *stateid);
1329ce0887acSOlga Kornievskaia extern void nfs42_ssc_close(struct file *filep);
1330ce0887acSOlga Kornievskaia 
1331ce0887acSOlga Kornievskaia extern void nfs_sb_deactive(struct super_block *sb);
1332ce0887acSOlga Kornievskaia 
1333ce0887acSOlga Kornievskaia #define NFSD42_INTERSSC_MOUNTOPS "vers=4.2,addr=%s,sec=sys"
1334ce0887acSOlga Kornievskaia 
1335f2453978SChuck Lever /*
1336f4e44b39SDai Ngo  * setup a work entry in the ssc delayed unmount list.
1337f4e44b39SDai Ngo  */
1338f47dc2d3SDai Ngo static __be32 nfsd4_ssc_setup_dul(struct nfsd_net *nn, char *ipaddr,
1339f4e44b39SDai Ngo 		struct nfsd4_ssc_umount_item **retwork, struct vfsmount **ss_mnt)
1340f4e44b39SDai Ngo {
13418e70bf27SColin Ian King 	struct nfsd4_ssc_umount_item *ni = NULL;
1342f4e44b39SDai Ngo 	struct nfsd4_ssc_umount_item *work = NULL;
1343f4e44b39SDai Ngo 	struct nfsd4_ssc_umount_item *tmp;
1344f4e44b39SDai Ngo 	DEFINE_WAIT(wait);
1345f4e44b39SDai Ngo 
1346f4e44b39SDai Ngo 	*ss_mnt = NULL;
1347f4e44b39SDai Ngo 	*retwork = NULL;
1348f4e44b39SDai Ngo 	work = kzalloc(sizeof(*work), GFP_KERNEL);
1349f4e44b39SDai Ngo try_again:
1350f4e44b39SDai Ngo 	spin_lock(&nn->nfsd_ssc_lock);
1351f4e44b39SDai Ngo 	list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
1352f4e44b39SDai Ngo 		if (strncmp(ni->nsui_ipaddr, ipaddr, sizeof(ni->nsui_ipaddr)))
1353f4e44b39SDai Ngo 			continue;
1354f4e44b39SDai Ngo 		/* found a match */
1355f4e44b39SDai Ngo 		if (ni->nsui_busy) {
1356f4e44b39SDai Ngo 			/*  wait - and try again */
1357f4e44b39SDai Ngo 			prepare_to_wait(&nn->nfsd_ssc_waitq, &wait,
1358f4e44b39SDai Ngo 				TASK_INTERRUPTIBLE);
1359f4e44b39SDai Ngo 			spin_unlock(&nn->nfsd_ssc_lock);
1360f4e44b39SDai Ngo 
1361f4e44b39SDai Ngo 			/* allow 20secs for mount/unmount for now - revisit */
1362f4e44b39SDai Ngo 			if (signal_pending(current) ||
1363f4e44b39SDai Ngo 					(schedule_timeout(20*HZ) == 0)) {
1364f4e44b39SDai Ngo 				kfree(work);
1365f4e44b39SDai Ngo 				return nfserr_eagain;
1366f4e44b39SDai Ngo 			}
1367f4e44b39SDai Ngo 			finish_wait(&nn->nfsd_ssc_waitq, &wait);
1368f4e44b39SDai Ngo 			goto try_again;
1369f4e44b39SDai Ngo 		}
1370f4e44b39SDai Ngo 		*ss_mnt = ni->nsui_vfsmount;
1371f4e44b39SDai Ngo 		refcount_inc(&ni->nsui_refcnt);
1372f4e44b39SDai Ngo 		spin_unlock(&nn->nfsd_ssc_lock);
1373f4e44b39SDai Ngo 		kfree(work);
1374f4e44b39SDai Ngo 
1375f4e44b39SDai Ngo 		/* return vfsmount in ss_mnt */
1376f4e44b39SDai Ngo 		return 0;
1377f4e44b39SDai Ngo 	}
1378f4e44b39SDai Ngo 	if (work) {
137953048779SChuck Lever 		strlcpy(work->nsui_ipaddr, ipaddr, sizeof(work->nsui_ipaddr) - 1);
1380f4e44b39SDai Ngo 		refcount_set(&work->nsui_refcnt, 2);
1381f4e44b39SDai Ngo 		work->nsui_busy = true;
1382f4e44b39SDai Ngo 		list_add_tail(&work->nsui_list, &nn->nfsd_ssc_mount_list);
1383f4e44b39SDai Ngo 		*retwork = work;
1384f4e44b39SDai Ngo 	}
1385f4e44b39SDai Ngo 	spin_unlock(&nn->nfsd_ssc_lock);
1386f4e44b39SDai Ngo 	return 0;
1387f4e44b39SDai Ngo }
1388f4e44b39SDai Ngo 
1389f4e44b39SDai Ngo static void nfsd4_ssc_update_dul_work(struct nfsd_net *nn,
1390f4e44b39SDai Ngo 		struct nfsd4_ssc_umount_item *work, struct vfsmount *ss_mnt)
1391f4e44b39SDai Ngo {
1392f4e44b39SDai Ngo 	/* set nsui_vfsmount, clear busy flag and wakeup waiters */
1393f4e44b39SDai Ngo 	spin_lock(&nn->nfsd_ssc_lock);
1394f4e44b39SDai Ngo 	work->nsui_vfsmount = ss_mnt;
1395f4e44b39SDai Ngo 	work->nsui_busy = false;
1396f4e44b39SDai Ngo 	wake_up_all(&nn->nfsd_ssc_waitq);
1397f4e44b39SDai Ngo 	spin_unlock(&nn->nfsd_ssc_lock);
1398f4e44b39SDai Ngo }
1399f4e44b39SDai Ngo 
1400f4e44b39SDai Ngo static void nfsd4_ssc_cancel_dul_work(struct nfsd_net *nn,
1401f4e44b39SDai Ngo 		struct nfsd4_ssc_umount_item *work)
1402f4e44b39SDai Ngo {
1403f4e44b39SDai Ngo 	spin_lock(&nn->nfsd_ssc_lock);
1404f4e44b39SDai Ngo 	list_del(&work->nsui_list);
1405f4e44b39SDai Ngo 	wake_up_all(&nn->nfsd_ssc_waitq);
1406f4e44b39SDai Ngo 	spin_unlock(&nn->nfsd_ssc_lock);
1407f4e44b39SDai Ngo 	kfree(work);
1408f4e44b39SDai Ngo }
1409f4e44b39SDai Ngo 
1410f4e44b39SDai Ngo /*
1411ce0887acSOlga Kornievskaia  * Support one copy source server for now.
1412ce0887acSOlga Kornievskaia  */
1413ce0887acSOlga Kornievskaia static __be32
1414ce0887acSOlga Kornievskaia nfsd4_interssc_connect(struct nl4_server *nss, struct svc_rqst *rqstp,
1415ce0887acSOlga Kornievskaia 		       struct vfsmount **mount)
1416ce0887acSOlga Kornievskaia {
1417ce0887acSOlga Kornievskaia 	struct file_system_type *type;
1418ce0887acSOlga Kornievskaia 	struct vfsmount *ss_mnt;
1419ce0887acSOlga Kornievskaia 	struct nfs42_netaddr *naddr;
1420ce0887acSOlga Kornievskaia 	struct sockaddr_storage tmp_addr;
1421ce0887acSOlga Kornievskaia 	size_t tmp_addrlen, match_netid_len = 3;
1422ce0887acSOlga Kornievskaia 	char *startsep = "", *endsep = "", *match_netid = "tcp";
1423ce0887acSOlga Kornievskaia 	char *ipaddr, *dev_name, *raw_data;
1424b8290ca2SOlga Kornievskaia 	int len, raw_len;
1425b8290ca2SOlga Kornievskaia 	__be32 status = nfserr_inval;
1426f4e44b39SDai Ngo 	struct nfsd4_ssc_umount_item *work = NULL;
1427f4e44b39SDai Ngo 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1428ce0887acSOlga Kornievskaia 
1429ce0887acSOlga Kornievskaia 	naddr = &nss->u.nl4_addr;
1430ce0887acSOlga Kornievskaia 	tmp_addrlen = rpc_uaddr2sockaddr(SVC_NET(rqstp), naddr->addr,
1431ce0887acSOlga Kornievskaia 					 naddr->addr_len,
1432ce0887acSOlga Kornievskaia 					 (struct sockaddr *)&tmp_addr,
1433ce0887acSOlga Kornievskaia 					 sizeof(tmp_addr));
1434ce0887acSOlga Kornievskaia 	if (tmp_addrlen == 0)
1435ce0887acSOlga Kornievskaia 		goto out_err;
1436ce0887acSOlga Kornievskaia 
1437ce0887acSOlga Kornievskaia 	if (tmp_addr.ss_family == AF_INET6) {
1438ce0887acSOlga Kornievskaia 		startsep = "[";
1439ce0887acSOlga Kornievskaia 		endsep = "]";
1440ce0887acSOlga Kornievskaia 		match_netid = "tcp6";
1441ce0887acSOlga Kornievskaia 		match_netid_len = 4;
1442ce0887acSOlga Kornievskaia 	}
1443ce0887acSOlga Kornievskaia 
1444ce0887acSOlga Kornievskaia 	if (naddr->netid_len != match_netid_len ||
1445ce0887acSOlga Kornievskaia 		strncmp(naddr->netid, match_netid, naddr->netid_len))
1446ce0887acSOlga Kornievskaia 		goto out_err;
1447ce0887acSOlga Kornievskaia 
1448ce0887acSOlga Kornievskaia 	/* Construct the raw data for the vfs_kern_mount call */
1449ce0887acSOlga Kornievskaia 	len = RPC_MAX_ADDRBUFLEN + 1;
1450ce0887acSOlga Kornievskaia 	ipaddr = kzalloc(len, GFP_KERNEL);
1451ce0887acSOlga Kornievskaia 	if (!ipaddr)
1452ce0887acSOlga Kornievskaia 		goto out_err;
1453ce0887acSOlga Kornievskaia 
1454ce0887acSOlga Kornievskaia 	rpc_ntop((struct sockaddr *)&tmp_addr, ipaddr, len);
1455ce0887acSOlga Kornievskaia 
1456ce0887acSOlga Kornievskaia 	/* 2 for ipv6 endsep and startsep. 3 for ":/" and trailing '/0'*/
1457ce0887acSOlga Kornievskaia 
1458ce0887acSOlga Kornievskaia 	raw_len = strlen(NFSD42_INTERSSC_MOUNTOPS) + strlen(ipaddr);
1459ce0887acSOlga Kornievskaia 	raw_data = kzalloc(raw_len, GFP_KERNEL);
1460ce0887acSOlga Kornievskaia 	if (!raw_data)
1461ce0887acSOlga Kornievskaia 		goto out_free_ipaddr;
1462ce0887acSOlga Kornievskaia 
1463ce0887acSOlga Kornievskaia 	snprintf(raw_data, raw_len, NFSD42_INTERSSC_MOUNTOPS, ipaddr);
1464ce0887acSOlga Kornievskaia 
1465b8290ca2SOlga Kornievskaia 	status = nfserr_nodev;
1466ce0887acSOlga Kornievskaia 	type = get_fs_type("nfs");
1467ce0887acSOlga Kornievskaia 	if (!type)
1468ce0887acSOlga Kornievskaia 		goto out_free_rawdata;
1469ce0887acSOlga Kornievskaia 
1470ce0887acSOlga Kornievskaia 	/* Set the server:<export> for the vfs_kern_mount call */
1471ce0887acSOlga Kornievskaia 	dev_name = kzalloc(len + 5, GFP_KERNEL);
1472ce0887acSOlga Kornievskaia 	if (!dev_name)
1473ce0887acSOlga Kornievskaia 		goto out_free_rawdata;
1474ce0887acSOlga Kornievskaia 	snprintf(dev_name, len + 5, "%s%s%s:/", startsep, ipaddr, endsep);
1475ce0887acSOlga Kornievskaia 
1476f4e44b39SDai Ngo 	status = nfsd4_ssc_setup_dul(nn, ipaddr, &work, &ss_mnt);
1477f4e44b39SDai Ngo 	if (status)
1478f4e44b39SDai Ngo 		goto out_free_devname;
1479f4e44b39SDai Ngo 	if (ss_mnt)
1480f4e44b39SDai Ngo 		goto out_done;
1481f4e44b39SDai Ngo 
1482ce0887acSOlga Kornievskaia 	/* Use an 'internal' mount: SB_KERNMOUNT -> MNT_INTERNAL */
1483ce0887acSOlga Kornievskaia 	ss_mnt = vfs_kern_mount(type, SB_KERNMOUNT, dev_name, raw_data);
1484ce0887acSOlga Kornievskaia 	module_put(type->owner);
1485f4e44b39SDai Ngo 	if (IS_ERR(ss_mnt)) {
148654185267SWei Yongjun 		status = nfserr_nodev;
1487f4e44b39SDai Ngo 		if (work)
1488f4e44b39SDai Ngo 			nfsd4_ssc_cancel_dul_work(nn, work);
1489ce0887acSOlga Kornievskaia 		goto out_free_devname;
1490f4e44b39SDai Ngo 	}
1491f4e44b39SDai Ngo 	if (work)
1492f4e44b39SDai Ngo 		nfsd4_ssc_update_dul_work(nn, work, ss_mnt);
1493f4e44b39SDai Ngo out_done:
1494ce0887acSOlga Kornievskaia 	status = 0;
1495ce0887acSOlga Kornievskaia 	*mount = ss_mnt;
1496ce0887acSOlga Kornievskaia 
1497ce0887acSOlga Kornievskaia out_free_devname:
1498ce0887acSOlga Kornievskaia 	kfree(dev_name);
1499ce0887acSOlga Kornievskaia out_free_rawdata:
1500ce0887acSOlga Kornievskaia 	kfree(raw_data);
1501ce0887acSOlga Kornievskaia out_free_ipaddr:
1502ce0887acSOlga Kornievskaia 	kfree(ipaddr);
1503ce0887acSOlga Kornievskaia out_err:
1504ce0887acSOlga Kornievskaia 	return status;
1505ce0887acSOlga Kornievskaia }
1506ce0887acSOlga Kornievskaia 
1507ce0887acSOlga Kornievskaia static void
1508ce0887acSOlga Kornievskaia nfsd4_interssc_disconnect(struct vfsmount *ss_mnt)
1509ce0887acSOlga Kornievskaia {
15100cfcd405SDai Ngo 	nfs_do_sb_deactive(ss_mnt->mnt_sb);
1511ce0887acSOlga Kornievskaia 	mntput(ss_mnt);
1512ce0887acSOlga Kornievskaia }
1513ce0887acSOlga Kornievskaia 
1514f2453978SChuck Lever /*
1515ce0887acSOlga Kornievskaia  * Verify COPY destination stateid.
1516f2453978SChuck Lever  *
1517ce0887acSOlga Kornievskaia  * Connect to the source server with NFSv4.1.
1518ce0887acSOlga Kornievskaia  * Create the source struct file for nfsd_copy_range.
1519ce0887acSOlga Kornievskaia  * Called with COPY cstate:
1520ce0887acSOlga Kornievskaia  *    SAVED_FH: source filehandle
1521ce0887acSOlga Kornievskaia  *    CURRENT_FH: destination filehandle
1522ce0887acSOlga Kornievskaia  */
1523ce0887acSOlga Kornievskaia static __be32
1524ce0887acSOlga Kornievskaia nfsd4_setup_inter_ssc(struct svc_rqst *rqstp,
1525ce0887acSOlga Kornievskaia 		      struct nfsd4_compound_state *cstate,
1526ce0887acSOlga Kornievskaia 		      struct nfsd4_copy *copy, struct vfsmount **mount)
1527ce0887acSOlga Kornievskaia {
1528ce0887acSOlga Kornievskaia 	struct svc_fh *s_fh = NULL;
1529ce0887acSOlga Kornievskaia 	stateid_t *s_stid = &copy->cp_src_stateid;
1530b8290ca2SOlga Kornievskaia 	__be32 status = nfserr_inval;
1531ce0887acSOlga Kornievskaia 
1532ce0887acSOlga Kornievskaia 	/* Verify the destination stateid and set dst struct file*/
1533ce0887acSOlga Kornievskaia 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1534ce0887acSOlga Kornievskaia 					    &copy->cp_dst_stateid,
1535ce0887acSOlga Kornievskaia 					    WR_STATE, &copy->nf_dst, NULL);
1536ce0887acSOlga Kornievskaia 	if (status)
1537ce0887acSOlga Kornievskaia 		goto out;
1538ce0887acSOlga Kornievskaia 
153987689df6SChuck Lever 	status = nfsd4_interssc_connect(copy->cp_src, rqstp, mount);
1540ce0887acSOlga Kornievskaia 	if (status)
1541ce0887acSOlga Kornievskaia 		goto out;
1542ce0887acSOlga Kornievskaia 
1543ce0887acSOlga Kornievskaia 	s_fh = &cstate->save_fh;
1544ce0887acSOlga Kornievskaia 
1545ce0887acSOlga Kornievskaia 	copy->c_fh.size = s_fh->fh_handle.fh_size;
1546d8b26071SNeilBrown 	memcpy(copy->c_fh.data, &s_fh->fh_handle.fh_raw, copy->c_fh.size);
15473f9544caSOlga Kornievskaia 	copy->stateid.seqid = cpu_to_be32(s_stid->si_generation);
1548ce0887acSOlga Kornievskaia 	memcpy(copy->stateid.other, (void *)&s_stid->si_opaque,
1549ce0887acSOlga Kornievskaia 	       sizeof(stateid_opaque_t));
1550ce0887acSOlga Kornievskaia 
1551ce0887acSOlga Kornievskaia 	status = 0;
1552ce0887acSOlga Kornievskaia out:
1553ce0887acSOlga Kornievskaia 	return status;
1554ce0887acSOlga Kornievskaia }
1555ce0887acSOlga Kornievskaia 
1556ce0887acSOlga Kornievskaia static void
1557ce0887acSOlga Kornievskaia nfsd4_cleanup_inter_ssc(struct vfsmount *ss_mnt, struct nfsd_file *src,
1558ce0887acSOlga Kornievskaia 			struct nfsd_file *dst)
1559ce0887acSOlga Kornievskaia {
1560f4e44b39SDai Ngo 	bool found = false;
1561f4e44b39SDai Ngo 	long timeout;
1562f4e44b39SDai Ngo 	struct nfsd4_ssc_umount_item *tmp;
1563f47dc2d3SDai Ngo 	struct nfsd4_ssc_umount_item *ni = NULL;
1564f4e44b39SDai Ngo 	struct nfsd_net *nn = net_generic(dst->nf_net, nfsd_net_id);
1565f4e44b39SDai Ngo 
1566ce0887acSOlga Kornievskaia 	nfs42_ssc_close(src->nf_file);
1567ce0887acSOlga Kornievskaia 	nfsd_file_put(dst);
1568f4e44b39SDai Ngo 	fput(src->nf_file);
1569f4e44b39SDai Ngo 
1570f4e44b39SDai Ngo 	if (!nn) {
1571ce0887acSOlga Kornievskaia 		mntput(ss_mnt);
1572f4e44b39SDai Ngo 		return;
1573f4e44b39SDai Ngo 	}
1574f4e44b39SDai Ngo 	spin_lock(&nn->nfsd_ssc_lock);
1575f4e44b39SDai Ngo 	timeout = msecs_to_jiffies(nfsd4_ssc_umount_timeout);
1576f4e44b39SDai Ngo 	list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
1577f4e44b39SDai Ngo 		if (ni->nsui_vfsmount->mnt_sb == ss_mnt->mnt_sb) {
1578f4e44b39SDai Ngo 			list_del(&ni->nsui_list);
1579f4e44b39SDai Ngo 			/*
1580f4e44b39SDai Ngo 			 * vfsmount can be shared by multiple exports,
1581f4e44b39SDai Ngo 			 * decrement refcnt. If the count drops to 1 it
1582f4e44b39SDai Ngo 			 * will be unmounted when nsui_expire expires.
1583f4e44b39SDai Ngo 			 */
1584f4e44b39SDai Ngo 			refcount_dec(&ni->nsui_refcnt);
1585f4e44b39SDai Ngo 			ni->nsui_expire = jiffies + timeout;
1586f4e44b39SDai Ngo 			list_add_tail(&ni->nsui_list, &nn->nfsd_ssc_mount_list);
1587f4e44b39SDai Ngo 			found = true;
1588f4e44b39SDai Ngo 			break;
1589f4e44b39SDai Ngo 		}
1590f4e44b39SDai Ngo 	}
1591f4e44b39SDai Ngo 	spin_unlock(&nn->nfsd_ssc_lock);
1592f4e44b39SDai Ngo 	if (!found) {
1593f4e44b39SDai Ngo 		mntput(ss_mnt);
1594f4e44b39SDai Ngo 		return;
1595f4e44b39SDai Ngo 	}
1596ce0887acSOlga Kornievskaia }
1597ce0887acSOlga Kornievskaia 
1598ce0887acSOlga Kornievskaia #else /* CONFIG_NFSD_V4_2_INTER_SSC */
1599ce0887acSOlga Kornievskaia 
1600ce0887acSOlga Kornievskaia static __be32
1601ce0887acSOlga Kornievskaia nfsd4_setup_inter_ssc(struct svc_rqst *rqstp,
1602ce0887acSOlga Kornievskaia 		      struct nfsd4_compound_state *cstate,
1603ce0887acSOlga Kornievskaia 		      struct nfsd4_copy *copy,
1604ce0887acSOlga Kornievskaia 		      struct vfsmount **mount)
1605ce0887acSOlga Kornievskaia {
1606ce0887acSOlga Kornievskaia 	*mount = NULL;
1607b8290ca2SOlga Kornievskaia 	return nfserr_inval;
1608ce0887acSOlga Kornievskaia }
1609ce0887acSOlga Kornievskaia 
1610ce0887acSOlga Kornievskaia static void
1611ce0887acSOlga Kornievskaia nfsd4_cleanup_inter_ssc(struct vfsmount *ss_mnt, struct nfsd_file *src,
1612ce0887acSOlga Kornievskaia 			struct nfsd_file *dst)
1613ce0887acSOlga Kornievskaia {
1614ce0887acSOlga Kornievskaia }
1615ce0887acSOlga Kornievskaia 
1616ce0887acSOlga Kornievskaia static void
1617ce0887acSOlga Kornievskaia nfsd4_interssc_disconnect(struct vfsmount *ss_mnt)
1618ce0887acSOlga Kornievskaia {
1619ce0887acSOlga Kornievskaia }
1620ce0887acSOlga Kornievskaia 
1621ce0887acSOlga Kornievskaia static struct file *nfs42_ssc_open(struct vfsmount *ss_mnt,
1622ce0887acSOlga Kornievskaia 				   struct nfs_fh *src_fh,
1623ce0887acSOlga Kornievskaia 				   nfs4_stateid *stateid)
1624ce0887acSOlga Kornievskaia {
1625ce0887acSOlga Kornievskaia 	return NULL;
1626ce0887acSOlga Kornievskaia }
1627ce0887acSOlga Kornievskaia #endif /* CONFIG_NFSD_V4_2_INTER_SSC */
1628ce0887acSOlga Kornievskaia 
1629ce0887acSOlga Kornievskaia static __be32
1630ce0887acSOlga Kornievskaia nfsd4_setup_intra_ssc(struct svc_rqst *rqstp,
1631ce0887acSOlga Kornievskaia 		      struct nfsd4_compound_state *cstate,
1632ce0887acSOlga Kornievskaia 		      struct nfsd4_copy *copy)
1633ce0887acSOlga Kornievskaia {
1634ce0887acSOlga Kornievskaia 	return nfsd4_verify_copy(rqstp, cstate, &copy->cp_src_stateid,
1635ce0887acSOlga Kornievskaia 				 &copy->nf_src, &copy->cp_dst_stateid,
1636ce0887acSOlga Kornievskaia 				 &copy->nf_dst);
1637ce0887acSOlga Kornievskaia }
1638ce0887acSOlga Kornievskaia 
1639ce0887acSOlga Kornievskaia static void
1640ce0887acSOlga Kornievskaia nfsd4_cleanup_intra_ssc(struct nfsd_file *src, struct nfsd_file *dst)
1641ce0887acSOlga Kornievskaia {
1642ce0887acSOlga Kornievskaia 	nfsd_file_put(src);
1643ce0887acSOlga Kornievskaia 	nfsd_file_put(dst);
1644ce0887acSOlga Kornievskaia }
1645e0639dc5SOlga Kornievskaia 
1646e0639dc5SOlga Kornievskaia static void nfsd4_cb_offload_release(struct nfsd4_callback *cb)
1647e0639dc5SOlga Kornievskaia {
1648e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy = container_of(cb, struct nfsd4_copy, cp_cb);
1649e0639dc5SOlga Kornievskaia 
1650e0639dc5SOlga Kornievskaia 	nfs4_put_copy(copy);
1651e0639dc5SOlga Kornievskaia }
1652e0639dc5SOlga Kornievskaia 
1653e0639dc5SOlga Kornievskaia static int nfsd4_cb_offload_done(struct nfsd4_callback *cb,
1654e0639dc5SOlga Kornievskaia 				 struct rpc_task *task)
1655e0639dc5SOlga Kornievskaia {
1656e0639dc5SOlga Kornievskaia 	return 1;
1657e0639dc5SOlga Kornievskaia }
1658e0639dc5SOlga Kornievskaia 
1659e0639dc5SOlga Kornievskaia static const struct nfsd4_callback_ops nfsd4_cb_offload_ops = {
1660e0639dc5SOlga Kornievskaia 	.release = nfsd4_cb_offload_release,
1661e0639dc5SOlga Kornievskaia 	.done = nfsd4_cb_offload_done
1662e0639dc5SOlga Kornievskaia };
1663e0639dc5SOlga Kornievskaia 
1664e0639dc5SOlga Kornievskaia static void nfsd4_init_copy_res(struct nfsd4_copy *copy, bool sync)
1665e0639dc5SOlga Kornievskaia {
1666eac0b17aSOlga Kornievskaia 	copy->cp_res.wr_stable_how =
1667*1913cdf5SChuck Lever 		test_bit(NFSD4_COPY_F_COMMITTED, &copy->cp_flags) ?
1668*1913cdf5SChuck Lever 			NFS_FILE_SYNC : NFS_UNSTABLE;
1669*1913cdf5SChuck Lever 	nfsd4_copy_set_sync(copy, sync);
1670e0639dc5SOlga Kornievskaia 	gen_boot_verifier(&copy->cp_res.wr_verifier, copy->cp_clp->net);
1671e0639dc5SOlga Kornievskaia }
1672e0639dc5SOlga Kornievskaia 
1673e0639dc5SOlga Kornievskaia static ssize_t _nfsd_copy_file_range(struct nfsd4_copy *copy)
1674e0639dc5SOlga Kornievskaia {
1675555dbf1aSTrond Myklebust 	struct file *dst = copy->nf_dst->nf_file;
1676555dbf1aSTrond Myklebust 	struct file *src = copy->nf_src->nf_file;
1677555dbf1aSTrond Myklebust 	errseq_t since;
1678e0639dc5SOlga Kornievskaia 	ssize_t bytes_copied = 0;
1679e7a833e9SJ. Bruce Fields 	u64 bytes_total = copy->cp_count;
1680e0639dc5SOlga Kornievskaia 	u64 src_pos = copy->cp_src_pos;
1681e0639dc5SOlga Kornievskaia 	u64 dst_pos = copy->cp_dst_pos;
1682c2f1c4bdSChuck Lever 	int status;
1683e0639dc5SOlga Kornievskaia 
1684792a5112SJ. Bruce Fields 	/* See RFC 7862 p.67: */
1685792a5112SJ. Bruce Fields 	if (bytes_total == 0)
1686792a5112SJ. Bruce Fields 		bytes_total = ULLONG_MAX;
1687e0639dc5SOlga Kornievskaia 	do {
1688e0639dc5SOlga Kornievskaia 		if (kthread_should_stop())
1689e0639dc5SOlga Kornievskaia 			break;
1690555dbf1aSTrond Myklebust 		bytes_copied = nfsd_copy_file_range(src, src_pos, dst, dst_pos,
16915c4583b2SJeff Layton 						    bytes_total);
1692e0639dc5SOlga Kornievskaia 		if (bytes_copied <= 0)
1693e0639dc5SOlga Kornievskaia 			break;
1694e0639dc5SOlga Kornievskaia 		bytes_total -= bytes_copied;
1695e0639dc5SOlga Kornievskaia 		copy->cp_res.wr_bytes_written += bytes_copied;
1696e0639dc5SOlga Kornievskaia 		src_pos += bytes_copied;
1697e0639dc5SOlga Kornievskaia 		dst_pos += bytes_copied;
1698*1913cdf5SChuck Lever 	} while (bytes_total > 0 && nfsd4_copy_is_async(copy));
1699eac0b17aSOlga Kornievskaia 	/* for a non-zero asynchronous copy do a commit of data */
1700*1913cdf5SChuck Lever 	if (nfsd4_copy_is_async(copy) && copy->cp_res.wr_bytes_written > 0) {
1701555dbf1aSTrond Myklebust 		since = READ_ONCE(dst->f_wb_err);
1702555dbf1aSTrond Myklebust 		status = vfs_fsync_range(dst, copy->cp_dst_pos,
1703eac0b17aSOlga Kornievskaia 					 copy->cp_res.wr_bytes_written, 0);
1704555dbf1aSTrond Myklebust 		if (!status)
1705555dbf1aSTrond Myklebust 			status = filemap_check_wb_err(dst->f_mapping, since);
1706eac0b17aSOlga Kornievskaia 		if (!status)
1707*1913cdf5SChuck Lever 			set_bit(NFSD4_COPY_F_COMMITTED, &copy->cp_flags);
1708eac0b17aSOlga Kornievskaia 	}
1709e0639dc5SOlga Kornievskaia 	return bytes_copied;
1710e0639dc5SOlga Kornievskaia }
1711e0639dc5SOlga Kornievskaia 
1712e0639dc5SOlga Kornievskaia static __be32 nfsd4_do_copy(struct nfsd4_copy *copy, bool sync)
1713e0639dc5SOlga Kornievskaia {
1714e0639dc5SOlga Kornievskaia 	__be32 status;
1715e0639dc5SOlga Kornievskaia 	ssize_t bytes;
1716e0639dc5SOlga Kornievskaia 
1717e0639dc5SOlga Kornievskaia 	bytes = _nfsd_copy_file_range(copy);
1718e0639dc5SOlga Kornievskaia 	/* for async copy, we ignore the error, client can always retry
1719e0639dc5SOlga Kornievskaia 	 * to get the error
1720e0639dc5SOlga Kornievskaia 	 */
1721e0639dc5SOlga Kornievskaia 	if (bytes < 0 && !copy->cp_res.wr_bytes_written)
1722e0639dc5SOlga Kornievskaia 		status = nfserrno(bytes);
1723e0639dc5SOlga Kornievskaia 	else {
1724e0639dc5SOlga Kornievskaia 		nfsd4_init_copy_res(copy, sync);
1725e0639dc5SOlga Kornievskaia 		status = nfs_ok;
1726e0639dc5SOlga Kornievskaia 	}
1727e0639dc5SOlga Kornievskaia 
1728*1913cdf5SChuck Lever 	if (nfsd4_ssc_is_inter(copy))
1729ce0887acSOlga Kornievskaia 		nfsd4_cleanup_inter_ssc(copy->ss_mnt, copy->nf_src,
1730ce0887acSOlga Kornievskaia 					copy->nf_dst);
1731ce0887acSOlga Kornievskaia 	else
1732ce0887acSOlga Kornievskaia 		nfsd4_cleanup_intra_ssc(copy->nf_src, copy->nf_dst);
1733ce0887acSOlga Kornievskaia 
1734e0639dc5SOlga Kornievskaia 	return status;
1735e0639dc5SOlga Kornievskaia }
1736e0639dc5SOlga Kornievskaia 
1737eb162e17SChuck Lever static void dup_copy_fields(struct nfsd4_copy *src, struct nfsd4_copy *dst)
1738e0639dc5SOlga Kornievskaia {
1739e0639dc5SOlga Kornievskaia 	dst->cp_src_pos = src->cp_src_pos;
1740e0639dc5SOlga Kornievskaia 	dst->cp_dst_pos = src->cp_dst_pos;
1741e0639dc5SOlga Kornievskaia 	dst->cp_count = src->cp_count;
1742*1913cdf5SChuck Lever 	dst->cp_flags = src->cp_flags;
1743e0639dc5SOlga Kornievskaia 	memcpy(&dst->cp_res, &src->cp_res, sizeof(src->cp_res));
1744e0639dc5SOlga Kornievskaia 	memcpy(&dst->fh, &src->fh, sizeof(src->fh));
1745e0639dc5SOlga Kornievskaia 	dst->cp_clp = src->cp_clp;
17465c4583b2SJeff Layton 	dst->nf_dst = nfsd_file_get(src->nf_dst);
1747*1913cdf5SChuck Lever 	/* for inter, nf_src doesn't exist yet */
1748*1913cdf5SChuck Lever 	if (!nfsd4_ssc_is_inter(src))
17495c4583b2SJeff Layton 		dst->nf_src = nfsd_file_get(src->nf_src);
1750ce0887acSOlga Kornievskaia 
1751e0639dc5SOlga Kornievskaia 	memcpy(&dst->cp_stateid, &src->cp_stateid, sizeof(src->cp_stateid));
175287689df6SChuck Lever 	memcpy(dst->cp_src, src->cp_src, sizeof(struct nl4_server));
1753ce0887acSOlga Kornievskaia 	memcpy(&dst->stateid, &src->stateid, sizeof(src->stateid));
1754ce0887acSOlga Kornievskaia 	memcpy(&dst->c_fh, &src->c_fh, sizeof(src->c_fh));
1755ce0887acSOlga Kornievskaia 	dst->ss_mnt = src->ss_mnt;
1756e0639dc5SOlga Kornievskaia }
1757e0639dc5SOlga Kornievskaia 
1758e0639dc5SOlga Kornievskaia static void cleanup_async_copy(struct nfsd4_copy *copy)
1759e0639dc5SOlga Kornievskaia {
1760624322f1SOlga Kornievskaia 	nfs4_free_copy_state(copy);
17615c4583b2SJeff Layton 	nfsd_file_put(copy->nf_dst);
1762*1913cdf5SChuck Lever 	if (!nfsd4_ssc_is_inter(copy))
17635c4583b2SJeff Layton 		nfsd_file_put(copy->nf_src);
1764e0639dc5SOlga Kornievskaia 	spin_lock(&copy->cp_clp->async_lock);
1765e0639dc5SOlga Kornievskaia 	list_del(&copy->copies);
1766e0639dc5SOlga Kornievskaia 	spin_unlock(&copy->cp_clp->async_lock);
1767e0639dc5SOlga Kornievskaia 	nfs4_put_copy(copy);
1768e0639dc5SOlga Kornievskaia }
1769e0639dc5SOlga Kornievskaia 
1770e0639dc5SOlga Kornievskaia static int nfsd4_do_async_copy(void *data)
1771e0639dc5SOlga Kornievskaia {
1772e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy = (struct nfsd4_copy *)data;
1773e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *cb_copy;
1774e0639dc5SOlga Kornievskaia 
1775*1913cdf5SChuck Lever 	if (nfsd4_ssc_is_inter(copy)) {
1776ce0887acSOlga Kornievskaia 		copy->nf_src = kzalloc(sizeof(struct nfsd_file), GFP_KERNEL);
1777ce0887acSOlga Kornievskaia 		if (!copy->nf_src) {
1778ce0887acSOlga Kornievskaia 			copy->nfserr = nfserr_serverfault;
1779ce0887acSOlga Kornievskaia 			nfsd4_interssc_disconnect(copy->ss_mnt);
1780ce0887acSOlga Kornievskaia 			goto do_callback;
1781ce0887acSOlga Kornievskaia 		}
1782ce0887acSOlga Kornievskaia 		copy->nf_src->nf_file = nfs42_ssc_open(copy->ss_mnt, &copy->c_fh,
1783ce0887acSOlga Kornievskaia 					      &copy->stateid);
1784ce0887acSOlga Kornievskaia 		if (IS_ERR(copy->nf_src->nf_file)) {
1785ce0887acSOlga Kornievskaia 			copy->nfserr = nfserr_offload_denied;
1786ce0887acSOlga Kornievskaia 			nfsd4_interssc_disconnect(copy->ss_mnt);
1787ce0887acSOlga Kornievskaia 			goto do_callback;
1788ce0887acSOlga Kornievskaia 		}
1789ce0887acSOlga Kornievskaia 	}
1790ce0887acSOlga Kornievskaia 
1791e0639dc5SOlga Kornievskaia 	copy->nfserr = nfsd4_do_copy(copy, 0);
1792ce0887acSOlga Kornievskaia do_callback:
1793e0639dc5SOlga Kornievskaia 	cb_copy = kzalloc(sizeof(struct nfsd4_copy), GFP_KERNEL);
1794e0639dc5SOlga Kornievskaia 	if (!cb_copy)
1795e0639dc5SOlga Kornievskaia 		goto out;
179649a36132SDai Ngo 	refcount_set(&cb_copy->refcount, 1);
1797e0639dc5SOlga Kornievskaia 	memcpy(&cb_copy->cp_res, &copy->cp_res, sizeof(copy->cp_res));
1798e0639dc5SOlga Kornievskaia 	cb_copy->cp_clp = copy->cp_clp;
1799e0639dc5SOlga Kornievskaia 	cb_copy->nfserr = copy->nfserr;
1800e0639dc5SOlga Kornievskaia 	memcpy(&cb_copy->fh, &copy->fh, sizeof(copy->fh));
1801e0639dc5SOlga Kornievskaia 	nfsd4_init_cb(&cb_copy->cp_cb, cb_copy->cp_clp,
1802e0639dc5SOlga Kornievskaia 			&nfsd4_cb_offload_ops, NFSPROC4_CLNT_CB_OFFLOAD);
180387512386SChuck Lever 	trace_nfsd_cb_offload(copy->cp_clp, &copy->cp_res.cb_stateid,
180487512386SChuck Lever 			      &copy->fh, copy->cp_count, copy->nfserr);
1805e0639dc5SOlga Kornievskaia 	nfsd4_run_cb(&cb_copy->cp_cb);
1806e0639dc5SOlga Kornievskaia out:
1807*1913cdf5SChuck Lever 	if (nfsd4_ssc_is_inter(copy))
1808ce0887acSOlga Kornievskaia 		kfree(copy->nf_src);
1809e0639dc5SOlga Kornievskaia 	cleanup_async_copy(copy);
1810e0639dc5SOlga Kornievskaia 	return 0;
1811e0639dc5SOlga Kornievskaia }
1812e0639dc5SOlga Kornievskaia 
1813ffa0160aSChristoph Hellwig static __be32
181429ae7f9dSAnna Schumaker nfsd4_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1815eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
181629ae7f9dSAnna Schumaker {
1817eb69853dSChristoph Hellwig 	struct nfsd4_copy *copy = &u->copy;
181829ae7f9dSAnna Schumaker 	__be32 status;
1819e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *async_copy = NULL;
182029ae7f9dSAnna Schumaker 
1821*1913cdf5SChuck Lever 	if (nfsd4_ssc_is_inter(copy)) {
1822*1913cdf5SChuck Lever 		if (!inter_copy_offload_enable || nfsd4_copy_is_sync(copy)) {
1823ce0887acSOlga Kornievskaia 			status = nfserr_notsupp;
182429ae7f9dSAnna Schumaker 			goto out;
1825ce0887acSOlga Kornievskaia 		}
1826ce0887acSOlga Kornievskaia 		status = nfsd4_setup_inter_ssc(rqstp, cstate, copy,
1827ce0887acSOlga Kornievskaia 				&copy->ss_mnt);
1828ce0887acSOlga Kornievskaia 		if (status)
1829ce0887acSOlga Kornievskaia 			return nfserr_offload_denied;
1830ce0887acSOlga Kornievskaia 	} else {
1831ce0887acSOlga Kornievskaia 		status = nfsd4_setup_intra_ssc(rqstp, cstate, copy);
1832ce0887acSOlga Kornievskaia 		if (status)
1833ce0887acSOlga Kornievskaia 			return status;
1834ce0887acSOlga Kornievskaia 	}
183529ae7f9dSAnna Schumaker 
1836e0639dc5SOlga Kornievskaia 	copy->cp_clp = cstate->clp;
1837e0639dc5SOlga Kornievskaia 	memcpy(&copy->fh, &cstate->current_fh.fh_handle,
1838e0639dc5SOlga Kornievskaia 		sizeof(struct knfsd_fh));
1839*1913cdf5SChuck Lever 	if (nfsd4_copy_is_async(copy)) {
1840e0639dc5SOlga Kornievskaia 		struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
184129ae7f9dSAnna Schumaker 
1842e0639dc5SOlga Kornievskaia 		status = nfserrno(-ENOMEM);
1843e0639dc5SOlga Kornievskaia 		async_copy = kzalloc(sizeof(struct nfsd4_copy), GFP_KERNEL);
1844e0639dc5SOlga Kornievskaia 		if (!async_copy)
1845ce0887acSOlga Kornievskaia 			goto out_err;
184687689df6SChuck Lever 		async_copy->cp_src = kmalloc(sizeof(*async_copy->cp_src), GFP_KERNEL);
184787689df6SChuck Lever 		if (!async_copy->cp_src)
184887689df6SChuck Lever 			goto out_err;
1849ce0887acSOlga Kornievskaia 		if (!nfs4_init_copy_state(nn, copy))
1850ce0887acSOlga Kornievskaia 			goto out_err;
1851e0639dc5SOlga Kornievskaia 		refcount_set(&async_copy->refcount, 1);
1852e739b120SOlga Kornievskaia 		memcpy(&copy->cp_res.cb_stateid, &copy->cp_stateid.stid,
1853e739b120SOlga Kornievskaia 			sizeof(copy->cp_res.cb_stateid));
1854eb162e17SChuck Lever 		dup_copy_fields(copy, async_copy);
1855e0639dc5SOlga Kornievskaia 		async_copy->copy_task = kthread_create(nfsd4_do_async_copy,
1856e0639dc5SOlga Kornievskaia 				async_copy, "%s", "copy thread");
1857e0639dc5SOlga Kornievskaia 		if (IS_ERR(async_copy->copy_task))
1858e0639dc5SOlga Kornievskaia 			goto out_err;
1859e0639dc5SOlga Kornievskaia 		spin_lock(&async_copy->cp_clp->async_lock);
1860e0639dc5SOlga Kornievskaia 		list_add(&async_copy->copies,
1861e0639dc5SOlga Kornievskaia 				&async_copy->cp_clp->async_copies);
1862e0639dc5SOlga Kornievskaia 		spin_unlock(&async_copy->cp_clp->async_lock);
1863e0639dc5SOlga Kornievskaia 		wake_up_process(async_copy->copy_task);
1864e0639dc5SOlga Kornievskaia 		status = nfs_ok;
1865ce0887acSOlga Kornievskaia 	} else {
1866e0639dc5SOlga Kornievskaia 		status = nfsd4_do_copy(copy, 1);
1867ce0887acSOlga Kornievskaia 	}
186829ae7f9dSAnna Schumaker out:
186929ae7f9dSAnna Schumaker 	return status;
1870e0639dc5SOlga Kornievskaia out_err:
187118f428d4SOlga Kornievskaia 	if (async_copy)
1872e0639dc5SOlga Kornievskaia 		cleanup_async_copy(async_copy);
1873ce0887acSOlga Kornievskaia 	status = nfserrno(-ENOMEM);
1874*1913cdf5SChuck Lever 	if (nfsd4_ssc_is_inter(copy))
1875ce0887acSOlga Kornievskaia 		nfsd4_interssc_disconnect(copy->ss_mnt);
1876e0639dc5SOlga Kornievskaia 	goto out;
1877e0639dc5SOlga Kornievskaia }
1878e0639dc5SOlga Kornievskaia 
1879e0639dc5SOlga Kornievskaia struct nfsd4_copy *
1880e0639dc5SOlga Kornievskaia find_async_copy(struct nfs4_client *clp, stateid_t *stateid)
1881e0639dc5SOlga Kornievskaia {
1882e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy;
1883e0639dc5SOlga Kornievskaia 
1884e0639dc5SOlga Kornievskaia 	spin_lock(&clp->async_lock);
1885e0639dc5SOlga Kornievskaia 	list_for_each_entry(copy, &clp->async_copies, copies) {
1886ce0887acSOlga Kornievskaia 		if (memcmp(&copy->cp_stateid.stid, stateid, NFS4_STATEID_SIZE))
1887e0639dc5SOlga Kornievskaia 			continue;
1888e0639dc5SOlga Kornievskaia 		refcount_inc(&copy->refcount);
1889e0639dc5SOlga Kornievskaia 		spin_unlock(&clp->async_lock);
1890e0639dc5SOlga Kornievskaia 		return copy;
1891e0639dc5SOlga Kornievskaia 	}
1892e0639dc5SOlga Kornievskaia 	spin_unlock(&clp->async_lock);
1893e0639dc5SOlga Kornievskaia 	return NULL;
189429ae7f9dSAnna Schumaker }
189529ae7f9dSAnna Schumaker 
189629ae7f9dSAnna Schumaker static __be32
1897885e2bf3SOlga Kornievskaia nfsd4_offload_cancel(struct svc_rqst *rqstp,
1898885e2bf3SOlga Kornievskaia 		     struct nfsd4_compound_state *cstate,
1899885e2bf3SOlga Kornievskaia 		     union nfsd4_op_u *u)
1900885e2bf3SOlga Kornievskaia {
1901e0639dc5SOlga Kornievskaia 	struct nfsd4_offload_status *os = &u->offload_status;
1902e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy;
1903e0639dc5SOlga Kornievskaia 	struct nfs4_client *clp = cstate->clp;
1904e0639dc5SOlga Kornievskaia 
1905e0639dc5SOlga Kornievskaia 	copy = find_async_copy(clp, &os->stateid);
1906ce0887acSOlga Kornievskaia 	if (!copy) {
1907ce0887acSOlga Kornievskaia 		struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1908e0639dc5SOlga Kornievskaia 
1909ce0887acSOlga Kornievskaia 		return manage_cpntf_state(nn, &os->stateid, clp, NULL);
1910ce0887acSOlga Kornievskaia 	} else
1911ce0887acSOlga Kornievskaia 		nfsd4_stop_copy(copy);
1912ce0887acSOlga Kornievskaia 
1913ce0887acSOlga Kornievskaia 	return nfs_ok;
1914885e2bf3SOlga Kornievskaia }
1915885e2bf3SOlga Kornievskaia 
1916885e2bf3SOlga Kornievskaia static __be32
191751911868SOlga Kornievskaia nfsd4_copy_notify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
191851911868SOlga Kornievskaia 		  union nfsd4_op_u *u)
191951911868SOlga Kornievskaia {
1920624322f1SOlga Kornievskaia 	struct nfsd4_copy_notify *cn = &u->copy_notify;
1921624322f1SOlga Kornievskaia 	__be32 status;
1922624322f1SOlga Kornievskaia 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1923624322f1SOlga Kornievskaia 	struct nfs4_stid *stid;
1924624322f1SOlga Kornievskaia 	struct nfs4_cpntf_state *cps;
1925624322f1SOlga Kornievskaia 	struct nfs4_client *clp = cstate->clp;
1926624322f1SOlga Kornievskaia 
1927624322f1SOlga Kornievskaia 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1928624322f1SOlga Kornievskaia 					&cn->cpn_src_stateid, RD_STATE, NULL,
1929624322f1SOlga Kornievskaia 					&stid);
1930624322f1SOlga Kornievskaia 	if (status)
1931624322f1SOlga Kornievskaia 		return status;
1932624322f1SOlga Kornievskaia 
1933624322f1SOlga Kornievskaia 	cn->cpn_sec = nn->nfsd4_lease;
1934624322f1SOlga Kornievskaia 	cn->cpn_nsec = 0;
1935624322f1SOlga Kornievskaia 
1936624322f1SOlga Kornievskaia 	status = nfserrno(-ENOMEM);
1937624322f1SOlga Kornievskaia 	cps = nfs4_alloc_init_cpntf_state(nn, stid);
1938624322f1SOlga Kornievskaia 	if (!cps)
1939624322f1SOlga Kornievskaia 		goto out;
1940624322f1SOlga Kornievskaia 	memcpy(&cn->cpn_cnr_stateid, &cps->cp_stateid.stid, sizeof(stateid_t));
1941624322f1SOlga Kornievskaia 	memcpy(&cps->cp_p_stateid, &stid->sc_stateid, sizeof(stateid_t));
1942624322f1SOlga Kornievskaia 	memcpy(&cps->cp_p_clid, &clp->cl_clientid, sizeof(clientid_t));
1943624322f1SOlga Kornievskaia 
1944624322f1SOlga Kornievskaia 	/* For now, only return one server address in cpn_src, the
1945624322f1SOlga Kornievskaia 	 * address used by the client to connect to this server.
1946624322f1SOlga Kornievskaia 	 */
194709426ef2SChuck Lever 	cn->cpn_src->nl4_type = NL4_NETADDR;
1948624322f1SOlga Kornievskaia 	status = nfsd4_set_netaddr((struct sockaddr *)&rqstp->rq_daddr,
194909426ef2SChuck Lever 				 &cn->cpn_src->u.nl4_addr);
1950624322f1SOlga Kornievskaia 	WARN_ON_ONCE(status);
1951624322f1SOlga Kornievskaia 	if (status) {
1952624322f1SOlga Kornievskaia 		nfs4_put_cpntf_state(nn, cps);
1953624322f1SOlga Kornievskaia 		goto out;
1954624322f1SOlga Kornievskaia 	}
1955624322f1SOlga Kornievskaia out:
1956624322f1SOlga Kornievskaia 	nfs4_put_stid(stid);
1957624322f1SOlga Kornievskaia 	return status;
195851911868SOlga Kornievskaia }
195951911868SOlga Kornievskaia 
196051911868SOlga Kornievskaia static __be32
196195d871f0SAnna Schumaker nfsd4_fallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
196295d871f0SAnna Schumaker 		struct nfsd4_fallocate *fallocate, int flags)
196395d871f0SAnna Schumaker {
19640d4d6720SJ. Bruce Fields 	__be32 status;
19655c4583b2SJeff Layton 	struct nfsd_file *nf;
196695d871f0SAnna Schumaker 
1967aa0d6aedSAnna Schumaker 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
196895d871f0SAnna Schumaker 					    &fallocate->falloc_stateid,
1969624322f1SOlga Kornievskaia 					    WR_STATE, &nf, NULL);
197095d871f0SAnna Schumaker 	if (status != nfs_ok) {
197195d871f0SAnna Schumaker 		dprintk("NFSD: nfsd4_fallocate: couldn't process stateid!\n");
197295d871f0SAnna Schumaker 		return status;
197395d871f0SAnna Schumaker 	}
197495d871f0SAnna Schumaker 
19755c4583b2SJeff Layton 	status = nfsd4_vfs_fallocate(rqstp, &cstate->current_fh, nf->nf_file,
197695d871f0SAnna Schumaker 				     fallocate->falloc_offset,
197795d871f0SAnna Schumaker 				     fallocate->falloc_length,
197895d871f0SAnna Schumaker 				     flags);
19795c4583b2SJeff Layton 	nfsd_file_put(nf);
198095d871f0SAnna Schumaker 	return status;
198195d871f0SAnna Schumaker }
19826308bc98SOlga Kornievskaia static __be32
19836308bc98SOlga Kornievskaia nfsd4_offload_status(struct svc_rqst *rqstp,
19846308bc98SOlga Kornievskaia 		     struct nfsd4_compound_state *cstate,
19856308bc98SOlga Kornievskaia 		     union nfsd4_op_u *u)
19866308bc98SOlga Kornievskaia {
1987e0639dc5SOlga Kornievskaia 	struct nfsd4_offload_status *os = &u->offload_status;
1988e0639dc5SOlga Kornievskaia 	__be32 status = 0;
1989e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy;
1990e0639dc5SOlga Kornievskaia 	struct nfs4_client *clp = cstate->clp;
1991e0639dc5SOlga Kornievskaia 
1992e0639dc5SOlga Kornievskaia 	copy = find_async_copy(clp, &os->stateid);
1993e0639dc5SOlga Kornievskaia 	if (copy) {
1994e0639dc5SOlga Kornievskaia 		os->count = copy->cp_res.wr_bytes_written;
1995e0639dc5SOlga Kornievskaia 		nfs4_put_copy(copy);
1996e0639dc5SOlga Kornievskaia 	} else
1997e0639dc5SOlga Kornievskaia 		status = nfserr_bad_stateid;
1998e0639dc5SOlga Kornievskaia 
1999e0639dc5SOlga Kornievskaia 	return status;
20006308bc98SOlga Kornievskaia }
200195d871f0SAnna Schumaker 
200295d871f0SAnna Schumaker static __be32
200395d871f0SAnna Schumaker nfsd4_allocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2004eb69853dSChristoph Hellwig 	       union nfsd4_op_u *u)
200595d871f0SAnna Schumaker {
2006eb69853dSChristoph Hellwig 	return nfsd4_fallocate(rqstp, cstate, &u->allocate, 0);
200795d871f0SAnna Schumaker }
200895d871f0SAnna Schumaker 
200995d871f0SAnna Schumaker static __be32
2010b0cb9085SAnna Schumaker nfsd4_deallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2011eb69853dSChristoph Hellwig 		 union nfsd4_op_u *u)
2012b0cb9085SAnna Schumaker {
2013eb69853dSChristoph Hellwig 	return nfsd4_fallocate(rqstp, cstate, &u->deallocate,
2014b0cb9085SAnna Schumaker 			       FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
2015b0cb9085SAnna Schumaker }
2016b0cb9085SAnna Schumaker 
2017b0cb9085SAnna Schumaker static __be32
201824bab491SAnna Schumaker nfsd4_seek(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2019eb69853dSChristoph Hellwig 	   union nfsd4_op_u *u)
202024bab491SAnna Schumaker {
2021eb69853dSChristoph Hellwig 	struct nfsd4_seek *seek = &u->seek;
202224bab491SAnna Schumaker 	int whence;
202324bab491SAnna Schumaker 	__be32 status;
20245c4583b2SJeff Layton 	struct nfsd_file *nf;
202524bab491SAnna Schumaker 
2026aa0d6aedSAnna Schumaker 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
202724bab491SAnna Schumaker 					    &seek->seek_stateid,
2028624322f1SOlga Kornievskaia 					    RD_STATE, &nf, NULL);
202924bab491SAnna Schumaker 	if (status) {
203024bab491SAnna Schumaker 		dprintk("NFSD: nfsd4_seek: couldn't process stateid!\n");
203124bab491SAnna Schumaker 		return status;
203224bab491SAnna Schumaker 	}
203324bab491SAnna Schumaker 
203424bab491SAnna Schumaker 	switch (seek->seek_whence) {
203524bab491SAnna Schumaker 	case NFS4_CONTENT_DATA:
203624bab491SAnna Schumaker 		whence = SEEK_DATA;
203724bab491SAnna Schumaker 		break;
203824bab491SAnna Schumaker 	case NFS4_CONTENT_HOLE:
203924bab491SAnna Schumaker 		whence = SEEK_HOLE;
204024bab491SAnna Schumaker 		break;
204124bab491SAnna Schumaker 	default:
204224bab491SAnna Schumaker 		status = nfserr_union_notsupp;
204324bab491SAnna Schumaker 		goto out;
204424bab491SAnna Schumaker 	}
204524bab491SAnna Schumaker 
204624bab491SAnna Schumaker 	/*
204724bab491SAnna Schumaker 	 * Note:  This call does change file->f_pos, but nothing in NFSD
204824bab491SAnna Schumaker 	 *        should ever file->f_pos.
204924bab491SAnna Schumaker 	 */
20505c4583b2SJeff Layton 	seek->seek_pos = vfs_llseek(nf->nf_file, seek->seek_offset, whence);
205124bab491SAnna Schumaker 	if (seek->seek_pos < 0)
205224bab491SAnna Schumaker 		status = nfserrno(seek->seek_pos);
20535c4583b2SJeff Layton 	else if (seek->seek_pos >= i_size_read(file_inode(nf->nf_file)))
205424bab491SAnna Schumaker 		seek->seek_eof = true;
205524bab491SAnna Schumaker 
205624bab491SAnna Schumaker out:
20575c4583b2SJeff Layton 	nfsd_file_put(nf);
205824bab491SAnna Schumaker 	return status;
205924bab491SAnna Schumaker }
206024bab491SAnna Schumaker 
20611da177e4SLinus Torvalds /* This routine never returns NFS_OK!  If there are no other errors, it
20621da177e4SLinus Torvalds  * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
20631da177e4SLinus Torvalds  * attributes matched.  VERIFY is implemented by mapping NFSERR_SAME
20641da177e4SLinus Torvalds  * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
20651da177e4SLinus Torvalds  */
2066b37ad28bSAl Viro static __be32
2067c954e2a5SJ.Bruce Fields _nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2068ca364317SJ.Bruce Fields 	     struct nfsd4_verify *verify)
20691da177e4SLinus Torvalds {
20702ebbc012SAl Viro 	__be32 *buf, *p;
20711da177e4SLinus Torvalds 	int count;
2072b37ad28bSAl Viro 	__be32 status;
20731da177e4SLinus Torvalds 
20748837abcaSMiklos Szeredi 	status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
20751da177e4SLinus Torvalds 	if (status)
20761da177e4SLinus Torvalds 		return status;
20771da177e4SLinus Torvalds 
20783c8e0316SYu Zhiguo 	status = check_attr_support(rqstp, cstate, verify->ve_bmval, NULL);
20793c8e0316SYu Zhiguo 	if (status)
20803c8e0316SYu Zhiguo 		return status;
20813c8e0316SYu Zhiguo 
20821da177e4SLinus Torvalds 	if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
20831da177e4SLinus Torvalds 	    || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
20841da177e4SLinus Torvalds 		return nfserr_inval;
20851da177e4SLinus Torvalds 	if (verify->ve_attrlen & 3)
20861da177e4SLinus Torvalds 		return nfserr_inval;
20871da177e4SLinus Torvalds 
20881da177e4SLinus Torvalds 	/* count in words:
20891da177e4SLinus Torvalds 	 *   bitmap_len(1) + bitmap(2) + attr_len(1) = 4
20901da177e4SLinus Torvalds 	 */
20911da177e4SLinus Torvalds 	count = 4 + (verify->ve_attrlen >> 2);
20921da177e4SLinus Torvalds 	buf = kmalloc(count << 2, GFP_KERNEL);
20931da177e4SLinus Torvalds 	if (!buf)
20943e772463SJ. Bruce Fields 		return nfserr_jukebox;
20951da177e4SLinus Torvalds 
209684822d0bSJ. Bruce Fields 	p = buf;
2097d5184658SJ. Bruce Fields 	status = nfsd4_encode_fattr_to_buf(&p, count, &cstate->current_fh,
2098ca364317SJ.Bruce Fields 				    cstate->current_fh.fh_export,
2099d5184658SJ. Bruce Fields 				    cstate->current_fh.fh_dentry,
2100d5184658SJ. Bruce Fields 				    verify->ve_bmval,
2101406a7ea9SFrank Filz 				    rqstp, 0);
210241ae6e71SJ. Bruce Fields 	/*
210341ae6e71SJ. Bruce Fields 	 * If nfsd4_encode_fattr() ran out of space, assume that's because
210441ae6e71SJ. Bruce Fields 	 * the attributes are longer (hence different) than those given:
210541ae6e71SJ. Bruce Fields 	 */
210684822d0bSJ. Bruce Fields 	if (status == nfserr_resource)
21071da177e4SLinus Torvalds 		status = nfserr_not_same;
21081da177e4SLinus Torvalds 	if (status)
21091da177e4SLinus Torvalds 		goto out_kfree;
21101da177e4SLinus Torvalds 
211195ec28cdSBenny Halevy 	/* skip bitmap */
211295ec28cdSBenny Halevy 	p = buf + 1 + ntohl(buf[0]);
21131da177e4SLinus Torvalds 	status = nfserr_not_same;
21141da177e4SLinus Torvalds 	if (ntohl(*p++) != verify->ve_attrlen)
21151da177e4SLinus Torvalds 		goto out_kfree;
21161da177e4SLinus Torvalds 	if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
21171da177e4SLinus Torvalds 		status = nfserr_same;
21181da177e4SLinus Torvalds 
21191da177e4SLinus Torvalds out_kfree:
21201da177e4SLinus Torvalds 	kfree(buf);
21211da177e4SLinus Torvalds 	return status;
21221da177e4SLinus Torvalds }
21231da177e4SLinus Torvalds 
2124c954e2a5SJ.Bruce Fields static __be32
2125c954e2a5SJ.Bruce Fields nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2126eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
2127c954e2a5SJ.Bruce Fields {
2128c954e2a5SJ.Bruce Fields 	__be32 status;
2129c954e2a5SJ.Bruce Fields 
2130eb69853dSChristoph Hellwig 	status = _nfsd4_verify(rqstp, cstate, &u->verify);
2131c954e2a5SJ.Bruce Fields 	return status == nfserr_not_same ? nfs_ok : status;
2132c954e2a5SJ.Bruce Fields }
2133c954e2a5SJ.Bruce Fields 
2134c954e2a5SJ.Bruce Fields static __be32
2135c954e2a5SJ.Bruce Fields nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2136eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
2137c954e2a5SJ.Bruce Fields {
2138c954e2a5SJ.Bruce Fields 	__be32 status;
2139c954e2a5SJ.Bruce Fields 
2140eb69853dSChristoph Hellwig 	status = _nfsd4_verify(rqstp, cstate, &u->nverify);
2141c954e2a5SJ.Bruce Fields 	return status == nfserr_same ? nfs_ok : status;
2142c954e2a5SJ.Bruce Fields }
2143c954e2a5SJ.Bruce Fields 
21449cf514ccSChristoph Hellwig #ifdef CONFIG_NFSD_PNFS
21459cf514ccSChristoph Hellwig static const struct nfsd4_layout_ops *
21469cf514ccSChristoph Hellwig nfsd4_layout_verify(struct svc_export *exp, unsigned int layout_type)
21479cf514ccSChristoph Hellwig {
21488a4c3926SJeff Layton 	if (!exp->ex_layout_types) {
21499cf514ccSChristoph Hellwig 		dprintk("%s: export does not support pNFS\n", __func__);
21509cf514ccSChristoph Hellwig 		return NULL;
21519cf514ccSChristoph Hellwig 	}
21529cf514ccSChristoph Hellwig 
2153b550a32eSAri Kauppi 	if (layout_type >= LAYOUT_TYPE_MAX ||
2154b550a32eSAri Kauppi 	    !(exp->ex_layout_types & (1 << layout_type))) {
21559cf514ccSChristoph Hellwig 		dprintk("%s: layout type %d not supported\n",
21569cf514ccSChristoph Hellwig 			__func__, layout_type);
21579cf514ccSChristoph Hellwig 		return NULL;
21589cf514ccSChristoph Hellwig 	}
21599cf514ccSChristoph Hellwig 
21609cf514ccSChristoph Hellwig 	return nfsd4_layout_ops[layout_type];
21619cf514ccSChristoph Hellwig }
21629cf514ccSChristoph Hellwig 
21639cf514ccSChristoph Hellwig static __be32
21649cf514ccSChristoph Hellwig nfsd4_getdeviceinfo(struct svc_rqst *rqstp,
2165eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
21669cf514ccSChristoph Hellwig {
2167eb69853dSChristoph Hellwig 	struct nfsd4_getdeviceinfo *gdp = &u->getdeviceinfo;
21689cf514ccSChristoph Hellwig 	const struct nfsd4_layout_ops *ops;
21699cf514ccSChristoph Hellwig 	struct nfsd4_deviceid_map *map;
21709cf514ccSChristoph Hellwig 	struct svc_export *exp;
21719cf514ccSChristoph Hellwig 	__be32 nfserr;
21729cf514ccSChristoph Hellwig 
21739cf514ccSChristoph Hellwig 	dprintk("%s: layout_type %u dev_id [0x%llx:0x%x] maxcnt %u\n",
21749cf514ccSChristoph Hellwig 	       __func__,
21759cf514ccSChristoph Hellwig 	       gdp->gd_layout_type,
21769cf514ccSChristoph Hellwig 	       gdp->gd_devid.fsid_idx, gdp->gd_devid.generation,
21779cf514ccSChristoph Hellwig 	       gdp->gd_maxcount);
21789cf514ccSChristoph Hellwig 
21799cf514ccSChristoph Hellwig 	map = nfsd4_find_devid_map(gdp->gd_devid.fsid_idx);
21809cf514ccSChristoph Hellwig 	if (!map) {
21819cf514ccSChristoph Hellwig 		dprintk("%s: couldn't find device ID to export mapping!\n",
21829cf514ccSChristoph Hellwig 			__func__);
21839cf514ccSChristoph Hellwig 		return nfserr_noent;
21849cf514ccSChristoph Hellwig 	}
21859cf514ccSChristoph Hellwig 
21869cf514ccSChristoph Hellwig 	exp = rqst_exp_find(rqstp, map->fsid_type, map->fsid);
21879cf514ccSChristoph Hellwig 	if (IS_ERR(exp)) {
21889cf514ccSChristoph Hellwig 		dprintk("%s: could not find device id\n", __func__);
21899cf514ccSChristoph Hellwig 		return nfserr_noent;
21909cf514ccSChristoph Hellwig 	}
21919cf514ccSChristoph Hellwig 
21929cf514ccSChristoph Hellwig 	nfserr = nfserr_layoutunavailable;
21939cf514ccSChristoph Hellwig 	ops = nfsd4_layout_verify(exp, gdp->gd_layout_type);
21949cf514ccSChristoph Hellwig 	if (!ops)
21959cf514ccSChristoph Hellwig 		goto out;
21969cf514ccSChristoph Hellwig 
21979cf514ccSChristoph Hellwig 	nfserr = nfs_ok;
2198f99d4fbdSChristoph Hellwig 	if (gdp->gd_maxcount != 0) {
2199f99d4fbdSChristoph Hellwig 		nfserr = ops->proc_getdeviceinfo(exp->ex_path.mnt->mnt_sb,
2200ec59659bSJ. Bruce Fields 				rqstp, cstate->clp, gdp);
2201f99d4fbdSChristoph Hellwig 	}
22029cf514ccSChristoph Hellwig 
22039cf514ccSChristoph Hellwig 	gdp->gd_notify_types &= ops->notify_types;
22049cf514ccSChristoph Hellwig out:
2205a1420384SKinglong Mee 	exp_put(exp);
22069cf514ccSChristoph Hellwig 	return nfserr;
22079cf514ccSChristoph Hellwig }
22089cf514ccSChristoph Hellwig 
220934b1744cSJ. Bruce Fields static void
221034b1744cSJ. Bruce Fields nfsd4_getdeviceinfo_release(union nfsd4_op_u *u)
221134b1744cSJ. Bruce Fields {
221234b1744cSJ. Bruce Fields 	kfree(u->getdeviceinfo.gd_device);
221334b1744cSJ. Bruce Fields }
221434b1744cSJ. Bruce Fields 
22159cf514ccSChristoph Hellwig static __be32
22169cf514ccSChristoph Hellwig nfsd4_layoutget(struct svc_rqst *rqstp,
2217eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
22189cf514ccSChristoph Hellwig {
2219eb69853dSChristoph Hellwig 	struct nfsd4_layoutget *lgp = &u->layoutget;
22209cf514ccSChristoph Hellwig 	struct svc_fh *current_fh = &cstate->current_fh;
22219cf514ccSChristoph Hellwig 	const struct nfsd4_layout_ops *ops;
22229cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls;
22239cf514ccSChristoph Hellwig 	__be32 nfserr;
222466282ec1SBenjamin Coddington 	int accmode = NFSD_MAY_READ_IF_EXEC;
22259cf514ccSChristoph Hellwig 
22269cf514ccSChristoph Hellwig 	switch (lgp->lg_seg.iomode) {
22279cf514ccSChristoph Hellwig 	case IOMODE_READ:
222866282ec1SBenjamin Coddington 		accmode |= NFSD_MAY_READ;
22299cf514ccSChristoph Hellwig 		break;
22309cf514ccSChristoph Hellwig 	case IOMODE_RW:
223166282ec1SBenjamin Coddington 		accmode |= NFSD_MAY_READ | NFSD_MAY_WRITE;
22329cf514ccSChristoph Hellwig 		break;
22339cf514ccSChristoph Hellwig 	default:
22349cf514ccSChristoph Hellwig 		dprintk("%s: invalid iomode %d\n",
22359cf514ccSChristoph Hellwig 			__func__, lgp->lg_seg.iomode);
22369cf514ccSChristoph Hellwig 		nfserr = nfserr_badiomode;
22379cf514ccSChristoph Hellwig 		goto out;
22389cf514ccSChristoph Hellwig 	}
22399cf514ccSChristoph Hellwig 
22409cf514ccSChristoph Hellwig 	nfserr = fh_verify(rqstp, current_fh, 0, accmode);
22419cf514ccSChristoph Hellwig 	if (nfserr)
22429cf514ccSChristoph Hellwig 		goto out;
22439cf514ccSChristoph Hellwig 
22449cf514ccSChristoph Hellwig 	nfserr = nfserr_layoutunavailable;
22459cf514ccSChristoph Hellwig 	ops = nfsd4_layout_verify(current_fh->fh_export, lgp->lg_layout_type);
22469cf514ccSChristoph Hellwig 	if (!ops)
22479cf514ccSChristoph Hellwig 		goto out;
22489cf514ccSChristoph Hellwig 
22499cf514ccSChristoph Hellwig 	/*
22509cf514ccSChristoph Hellwig 	 * Verify minlength and range as per RFC5661:
22519cf514ccSChristoph Hellwig 	 *  o  If loga_length is less than loga_minlength,
22529cf514ccSChristoph Hellwig 	 *     the metadata server MUST return NFS4ERR_INVAL.
22539cf514ccSChristoph Hellwig 	 *  o  If the sum of loga_offset and loga_minlength exceeds
22549cf514ccSChristoph Hellwig 	 *     NFS4_UINT64_MAX, and loga_minlength is not
22559cf514ccSChristoph Hellwig 	 *     NFS4_UINT64_MAX, the error NFS4ERR_INVAL MUST result.
22569cf514ccSChristoph Hellwig 	 *  o  If the sum of loga_offset and loga_length exceeds
22579cf514ccSChristoph Hellwig 	 *     NFS4_UINT64_MAX, and loga_length is not NFS4_UINT64_MAX,
22589cf514ccSChristoph Hellwig 	 *     the error NFS4ERR_INVAL MUST result.
22599cf514ccSChristoph Hellwig 	 */
22609cf514ccSChristoph Hellwig 	nfserr = nfserr_inval;
22619cf514ccSChristoph Hellwig 	if (lgp->lg_seg.length < lgp->lg_minlength ||
22629cf514ccSChristoph Hellwig 	    (lgp->lg_minlength != NFS4_MAX_UINT64 &&
22639cf514ccSChristoph Hellwig 	     lgp->lg_minlength > NFS4_MAX_UINT64 - lgp->lg_seg.offset) ||
22649cf514ccSChristoph Hellwig 	    (lgp->lg_seg.length != NFS4_MAX_UINT64 &&
22659cf514ccSChristoph Hellwig 	     lgp->lg_seg.length > NFS4_MAX_UINT64 - lgp->lg_seg.offset))
22669cf514ccSChristoph Hellwig 		goto out;
22679cf514ccSChristoph Hellwig 	if (lgp->lg_seg.length == 0)
22689cf514ccSChristoph Hellwig 		goto out;
22699cf514ccSChristoph Hellwig 
22709cf514ccSChristoph Hellwig 	nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lgp->lg_sid,
22719cf514ccSChristoph Hellwig 						true, lgp->lg_layout_type, &ls);
227231ef83dcSChristoph Hellwig 	if (nfserr) {
2273f394b62bSChuck Lever 		trace_nfsd_layout_get_lookup_fail(&lgp->lg_sid);
22749cf514ccSChristoph Hellwig 		goto out;
227531ef83dcSChristoph Hellwig 	}
22769cf514ccSChristoph Hellwig 
2277c5c707f9SChristoph Hellwig 	nfserr = nfserr_recallconflict;
2278c5c707f9SChristoph Hellwig 	if (atomic_read(&ls->ls_stid.sc_file->fi_lo_recalls))
2279c5c707f9SChristoph Hellwig 		goto out_put_stid;
2280c5c707f9SChristoph Hellwig 
22812b0143b5SDavid Howells 	nfserr = ops->proc_layoutget(d_inode(current_fh->fh_dentry),
22829cf514ccSChristoph Hellwig 				     current_fh, lgp);
22839cf514ccSChristoph Hellwig 	if (nfserr)
22849cf514ccSChristoph Hellwig 		goto out_put_stid;
22859cf514ccSChristoph Hellwig 
22869cf514ccSChristoph Hellwig 	nfserr = nfsd4_insert_layout(lgp, ls);
22879cf514ccSChristoph Hellwig 
22889cf514ccSChristoph Hellwig out_put_stid:
2289cc8a5532SJeff Layton 	mutex_unlock(&ls->ls_mutex);
22909cf514ccSChristoph Hellwig 	nfs4_put_stid(&ls->ls_stid);
22919cf514ccSChristoph Hellwig out:
22929cf514ccSChristoph Hellwig 	return nfserr;
22939cf514ccSChristoph Hellwig }
22949cf514ccSChristoph Hellwig 
229534b1744cSJ. Bruce Fields static void
229634b1744cSJ. Bruce Fields nfsd4_layoutget_release(union nfsd4_op_u *u)
229734b1744cSJ. Bruce Fields {
229834b1744cSJ. Bruce Fields 	kfree(u->layoutget.lg_content);
229934b1744cSJ. Bruce Fields }
230034b1744cSJ. Bruce Fields 
23019cf514ccSChristoph Hellwig static __be32
23029cf514ccSChristoph Hellwig nfsd4_layoutcommit(struct svc_rqst *rqstp,
2303eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
23049cf514ccSChristoph Hellwig {
2305eb69853dSChristoph Hellwig 	struct nfsd4_layoutcommit *lcp = &u->layoutcommit;
23069cf514ccSChristoph Hellwig 	const struct nfsd4_layout_seg *seg = &lcp->lc_seg;
23079cf514ccSChristoph Hellwig 	struct svc_fh *current_fh = &cstate->current_fh;
23089cf514ccSChristoph Hellwig 	const struct nfsd4_layout_ops *ops;
23099cf514ccSChristoph Hellwig 	loff_t new_size = lcp->lc_last_wr + 1;
23109cf514ccSChristoph Hellwig 	struct inode *inode;
23119cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls;
23129cf514ccSChristoph Hellwig 	__be32 nfserr;
23139cf514ccSChristoph Hellwig 
23149cf514ccSChristoph Hellwig 	nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_WRITE);
23159cf514ccSChristoph Hellwig 	if (nfserr)
23169cf514ccSChristoph Hellwig 		goto out;
23179cf514ccSChristoph Hellwig 
23189cf514ccSChristoph Hellwig 	nfserr = nfserr_layoutunavailable;
23199cf514ccSChristoph Hellwig 	ops = nfsd4_layout_verify(current_fh->fh_export, lcp->lc_layout_type);
23209cf514ccSChristoph Hellwig 	if (!ops)
23219cf514ccSChristoph Hellwig 		goto out;
23222b0143b5SDavid Howells 	inode = d_inode(current_fh->fh_dentry);
23239cf514ccSChristoph Hellwig 
23249cf514ccSChristoph Hellwig 	nfserr = nfserr_inval;
23259cf514ccSChristoph Hellwig 	if (new_size <= seg->offset) {
23269cf514ccSChristoph Hellwig 		dprintk("pnfsd: last write before layout segment\n");
23279cf514ccSChristoph Hellwig 		goto out;
23289cf514ccSChristoph Hellwig 	}
23299cf514ccSChristoph Hellwig 	if (new_size > seg->offset + seg->length) {
23309cf514ccSChristoph Hellwig 		dprintk("pnfsd: last write beyond layout segment\n");
23319cf514ccSChristoph Hellwig 		goto out;
23329cf514ccSChristoph Hellwig 	}
23339cf514ccSChristoph Hellwig 	if (!lcp->lc_newoffset && new_size > i_size_read(inode)) {
23349cf514ccSChristoph Hellwig 		dprintk("pnfsd: layoutcommit beyond EOF\n");
23359cf514ccSChristoph Hellwig 		goto out;
23369cf514ccSChristoph Hellwig 	}
23379cf514ccSChristoph Hellwig 
23389cf514ccSChristoph Hellwig 	nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lcp->lc_sid,
23399cf514ccSChristoph Hellwig 						false, lcp->lc_layout_type,
23409cf514ccSChristoph Hellwig 						&ls);
23419cf514ccSChristoph Hellwig 	if (nfserr) {
2342f394b62bSChuck Lever 		trace_nfsd_layout_commit_lookup_fail(&lcp->lc_sid);
23439cf514ccSChristoph Hellwig 		/* fixup error code as per RFC5661 */
23449cf514ccSChristoph Hellwig 		if (nfserr == nfserr_bad_stateid)
23459cf514ccSChristoph Hellwig 			nfserr = nfserr_badlayout;
23469cf514ccSChristoph Hellwig 		goto out;
23479cf514ccSChristoph Hellwig 	}
23489cf514ccSChristoph Hellwig 
2349cc8a5532SJeff Layton 	/* LAYOUTCOMMIT does not require any serialization */
2350cc8a5532SJeff Layton 	mutex_unlock(&ls->ls_mutex);
2351cc8a5532SJeff Layton 
23529cf514ccSChristoph Hellwig 	if (new_size > i_size_read(inode)) {
23539cf514ccSChristoph Hellwig 		lcp->lc_size_chg = 1;
23549cf514ccSChristoph Hellwig 		lcp->lc_newsize = new_size;
23559cf514ccSChristoph Hellwig 	} else {
23569cf514ccSChristoph Hellwig 		lcp->lc_size_chg = 0;
23579cf514ccSChristoph Hellwig 	}
23589cf514ccSChristoph Hellwig 
2359d8398fc1SKinglong Mee 	nfserr = ops->proc_layoutcommit(inode, lcp);
23609cf514ccSChristoph Hellwig 	nfs4_put_stid(&ls->ls_stid);
23619cf514ccSChristoph Hellwig out:
23629cf514ccSChristoph Hellwig 	return nfserr;
23639cf514ccSChristoph Hellwig }
23649cf514ccSChristoph Hellwig 
23659cf514ccSChristoph Hellwig static __be32
23669cf514ccSChristoph Hellwig nfsd4_layoutreturn(struct svc_rqst *rqstp,
2367eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
23689cf514ccSChristoph Hellwig {
2369eb69853dSChristoph Hellwig 	struct nfsd4_layoutreturn *lrp = &u->layoutreturn;
23709cf514ccSChristoph Hellwig 	struct svc_fh *current_fh = &cstate->current_fh;
23719cf514ccSChristoph Hellwig 	__be32 nfserr;
23729cf514ccSChristoph Hellwig 
23739cf514ccSChristoph Hellwig 	nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_NOP);
23749cf514ccSChristoph Hellwig 	if (nfserr)
23759cf514ccSChristoph Hellwig 		goto out;
23769cf514ccSChristoph Hellwig 
23779cf514ccSChristoph Hellwig 	nfserr = nfserr_layoutunavailable;
23789cf514ccSChristoph Hellwig 	if (!nfsd4_layout_verify(current_fh->fh_export, lrp->lr_layout_type))
23799cf514ccSChristoph Hellwig 		goto out;
23809cf514ccSChristoph Hellwig 
23819cf514ccSChristoph Hellwig 	switch (lrp->lr_seg.iomode) {
23829cf514ccSChristoph Hellwig 	case IOMODE_READ:
23839cf514ccSChristoph Hellwig 	case IOMODE_RW:
23849cf514ccSChristoph Hellwig 	case IOMODE_ANY:
23859cf514ccSChristoph Hellwig 		break;
23869cf514ccSChristoph Hellwig 	default:
23879cf514ccSChristoph Hellwig 		dprintk("%s: invalid iomode %d\n", __func__,
23889cf514ccSChristoph Hellwig 			lrp->lr_seg.iomode);
23899cf514ccSChristoph Hellwig 		nfserr = nfserr_inval;
23909cf514ccSChristoph Hellwig 		goto out;
23919cf514ccSChristoph Hellwig 	}
23929cf514ccSChristoph Hellwig 
23939cf514ccSChristoph Hellwig 	switch (lrp->lr_return_type) {
23949cf514ccSChristoph Hellwig 	case RETURN_FILE:
23959cf514ccSChristoph Hellwig 		nfserr = nfsd4_return_file_layouts(rqstp, cstate, lrp);
23969cf514ccSChristoph Hellwig 		break;
23979cf514ccSChristoph Hellwig 	case RETURN_FSID:
23989cf514ccSChristoph Hellwig 	case RETURN_ALL:
23999cf514ccSChristoph Hellwig 		nfserr = nfsd4_return_client_layouts(rqstp, cstate, lrp);
24009cf514ccSChristoph Hellwig 		break;
24019cf514ccSChristoph Hellwig 	default:
24029cf514ccSChristoph Hellwig 		dprintk("%s: invalid return_type %d\n", __func__,
24039cf514ccSChristoph Hellwig 			lrp->lr_return_type);
24049cf514ccSChristoph Hellwig 		nfserr = nfserr_inval;
24059cf514ccSChristoph Hellwig 		break;
24069cf514ccSChristoph Hellwig 	}
24079cf514ccSChristoph Hellwig out:
24089cf514ccSChristoph Hellwig 	return nfserr;
24099cf514ccSChristoph Hellwig }
24109cf514ccSChristoph Hellwig #endif /* CONFIG_NFSD_PNFS */
24119cf514ccSChristoph Hellwig 
241223e50fe3SFrank van der Linden static __be32
241323e50fe3SFrank van der Linden nfsd4_getxattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
241423e50fe3SFrank van der Linden 	       union nfsd4_op_u *u)
241523e50fe3SFrank van der Linden {
241623e50fe3SFrank van der Linden 	struct nfsd4_getxattr *getxattr = &u->getxattr;
241723e50fe3SFrank van der Linden 
241823e50fe3SFrank van der Linden 	return nfsd_getxattr(rqstp, &cstate->current_fh,
241923e50fe3SFrank van der Linden 			     getxattr->getxa_name, &getxattr->getxa_buf,
242023e50fe3SFrank van der Linden 			     &getxattr->getxa_len);
242123e50fe3SFrank van der Linden }
242223e50fe3SFrank van der Linden 
242323e50fe3SFrank van der Linden static __be32
242423e50fe3SFrank van der Linden nfsd4_setxattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
242523e50fe3SFrank van der Linden 	   union nfsd4_op_u *u)
242623e50fe3SFrank van der Linden {
242723e50fe3SFrank van der Linden 	struct nfsd4_setxattr *setxattr = &u->setxattr;
242823e50fe3SFrank van der Linden 	__be32 ret;
242923e50fe3SFrank van der Linden 
243023e50fe3SFrank van der Linden 	if (opens_in_grace(SVC_NET(rqstp)))
243123e50fe3SFrank van der Linden 		return nfserr_grace;
243223e50fe3SFrank van der Linden 
243323e50fe3SFrank van der Linden 	ret = nfsd_setxattr(rqstp, &cstate->current_fh, setxattr->setxa_name,
243423e50fe3SFrank van der Linden 			    setxattr->setxa_buf, setxattr->setxa_len,
243523e50fe3SFrank van der Linden 			    setxattr->setxa_flags);
243623e50fe3SFrank van der Linden 
243723e50fe3SFrank van der Linden 	if (!ret)
243823e50fe3SFrank van der Linden 		set_change_info(&setxattr->setxa_cinfo, &cstate->current_fh);
243923e50fe3SFrank van der Linden 
244023e50fe3SFrank van der Linden 	return ret;
244123e50fe3SFrank van der Linden }
244223e50fe3SFrank van der Linden 
244323e50fe3SFrank van der Linden static __be32
244423e50fe3SFrank van der Linden nfsd4_listxattrs(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
244523e50fe3SFrank van der Linden 	   union nfsd4_op_u *u)
244623e50fe3SFrank van der Linden {
244723e50fe3SFrank van der Linden 	/*
244823e50fe3SFrank van der Linden 	 * Get the entire list, then copy out only the user attributes
244923e50fe3SFrank van der Linden 	 * in the encode function.
245023e50fe3SFrank van der Linden 	 */
245123e50fe3SFrank van der Linden 	return nfsd_listxattr(rqstp, &cstate->current_fh,
245223e50fe3SFrank van der Linden 			     &u->listxattrs.lsxa_buf, &u->listxattrs.lsxa_len);
245323e50fe3SFrank van der Linden }
245423e50fe3SFrank van der Linden 
245523e50fe3SFrank van der Linden static __be32
245623e50fe3SFrank van der Linden nfsd4_removexattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
245723e50fe3SFrank van der Linden 	   union nfsd4_op_u *u)
245823e50fe3SFrank van der Linden {
245923e50fe3SFrank van der Linden 	struct nfsd4_removexattr *removexattr = &u->removexattr;
246023e50fe3SFrank van der Linden 	__be32 ret;
246123e50fe3SFrank van der Linden 
246223e50fe3SFrank van der Linden 	if (opens_in_grace(SVC_NET(rqstp)))
246323e50fe3SFrank van der Linden 		return nfserr_grace;
246423e50fe3SFrank van der Linden 
246523e50fe3SFrank van der Linden 	ret = nfsd_removexattr(rqstp, &cstate->current_fh,
246623e50fe3SFrank van der Linden 	    removexattr->rmxa_name);
246723e50fe3SFrank van der Linden 
246823e50fe3SFrank van der Linden 	if (!ret)
246923e50fe3SFrank van der Linden 		set_change_info(&removexattr->rmxa_cinfo, &cstate->current_fh);
247023e50fe3SFrank van der Linden 
247123e50fe3SFrank van der Linden 	return ret;
247223e50fe3SFrank van der Linden }
247323e50fe3SFrank van der Linden 
24741da177e4SLinus Torvalds /*
24751da177e4SLinus Torvalds  * NULL call.
24761da177e4SLinus Torvalds  */
24777111c66eSAl Viro static __be32
2478a6beb732SChristoph Hellwig nfsd4_proc_null(struct svc_rqst *rqstp)
24791da177e4SLinus Torvalds {
2480cc028a10SChuck Lever 	return rpc_success;
24811da177e4SLinus Torvalds }
24821da177e4SLinus Torvalds 
2483e2b20950SShankar Anand static inline void nfsd4_increment_op_stats(u32 opnum)
2484e2b20950SShankar Anand {
2485e2b20950SShankar Anand 	if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
2486e567b98cSAmir Goldstein 		percpu_counter_inc(&nfsdstats.counter[NFSD_STATS_NFS4_OP(opnum)]);
2487e2b20950SShankar Anand }
2488e2b20950SShankar Anand 
2489bb2a8b0cSChristoph Hellwig static const struct nfsd4_operation nfsd4_ops[];
2490b591480bSJ.Bruce Fields 
2491f1c7f79bSAdrian Bunk static const char *nfsd4_op_name(unsigned opnum);
2492b001a1b6SBenny Halevy 
24931da177e4SLinus Torvalds /*
249457716355SJ. Bruce Fields  * Enforce NFSv4.1 COMPOUND ordering rules:
2495f9bb94c4SAndy Adamson  *
249657716355SJ. Bruce Fields  * Also note, enforced elsewhere:
249757716355SJ. Bruce Fields  *	- SEQUENCE other than as first op results in
249857716355SJ. Bruce Fields  *	  NFS4ERR_SEQUENCE_POS. (Enforced in nfsd4_sequence().)
24991d1bc8f2SJ. Bruce Fields  *	- BIND_CONN_TO_SESSION must be the only op in its compound.
25001d1bc8f2SJ. Bruce Fields  *	  (Enforced in nfsd4_bind_conn_to_session().)
250157716355SJ. Bruce Fields  *	- DESTROY_SESSION must be the final operation in a compound, if
250257716355SJ. Bruce Fields  *	  sessionid's in SEQUENCE and DESTROY_SESSION are the same.
250357716355SJ. Bruce Fields  *	  (Enforced in nfsd4_destroy_session().)
2504f9bb94c4SAndy Adamson  */
250557716355SJ. Bruce Fields static __be32 nfs41_check_op_ordering(struct nfsd4_compoundargs *args)
2506f9bb94c4SAndy Adamson {
25077a04cfdaSJ. Bruce Fields 	struct nfsd4_op *first_op = &args->ops[0];
250857716355SJ. Bruce Fields 
250957716355SJ. Bruce Fields 	/* These ordering requirements don't apply to NFSv4.0: */
251057716355SJ. Bruce Fields 	if (args->minorversion == 0)
251157716355SJ. Bruce Fields 		return nfs_ok;
251257716355SJ. Bruce Fields 	/* This is weird, but OK, not our problem: */
251357716355SJ. Bruce Fields 	if (args->opcnt == 0)
251457716355SJ. Bruce Fields 		return nfs_ok;
25157a04cfdaSJ. Bruce Fields 	if (first_op->status == nfserr_op_illegal)
251657716355SJ. Bruce Fields 		return nfs_ok;
25177a04cfdaSJ. Bruce Fields 	if (!(nfsd4_ops[first_op->opnum].op_flags & ALLOWED_AS_FIRST_OP))
251857716355SJ. Bruce Fields 		return nfserr_op_not_in_session;
25197a04cfdaSJ. Bruce Fields 	if (first_op->opnum == OP_SEQUENCE)
252057716355SJ. Bruce Fields 		return nfs_ok;
25217a04cfdaSJ. Bruce Fields 	/*
25227a04cfdaSJ. Bruce Fields 	 * So first_op is something allowed outside a session, like
25237a04cfdaSJ. Bruce Fields 	 * EXCHANGE_ID; but then it has to be the only op in the
25247a04cfdaSJ. Bruce Fields 	 * compound:
25257a04cfdaSJ. Bruce Fields 	 */
252657716355SJ. Bruce Fields 	if (args->opcnt != 1)
252757716355SJ. Bruce Fields 		return nfserr_not_only_op;
252857716355SJ. Bruce Fields 	return nfs_ok;
2529f9bb94c4SAndy Adamson }
2530f9bb94c4SAndy Adamson 
2531f4f9ef4aSJ. Bruce Fields const struct nfsd4_operation *OPDESC(struct nfsd4_op *op)
253222b03214SJ. Bruce Fields {
253322b03214SJ. Bruce Fields 	return &nfsd4_ops[op->opnum];
253422b03214SJ. Bruce Fields }
253522b03214SJ. Bruce Fields 
25361091006cSJ. Bruce Fields bool nfsd4_cache_this_op(struct nfsd4_op *op)
25371091006cSJ. Bruce Fields {
2538e372ba60SJ. Bruce Fields 	if (op->opnum == OP_ILLEGAL)
2539e372ba60SJ. Bruce Fields 		return false;
2540c856694eSJ. Bruce Fields 	return OPDESC(op)->op_flags & OP_CACHEME;
25411091006cSJ. Bruce Fields }
25421091006cSJ. Bruce Fields 
254368d93184SJ. Bruce Fields static bool need_wrongsec_check(struct svc_rqst *rqstp)
254468d93184SJ. Bruce Fields {
254568d93184SJ. Bruce Fields 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
254668d93184SJ. Bruce Fields 	struct nfsd4_compoundargs *argp = rqstp->rq_argp;
254768d93184SJ. Bruce Fields 	struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
254868d93184SJ. Bruce Fields 	struct nfsd4_op *next = &argp->ops[resp->opcnt];
2549bb2a8b0cSChristoph Hellwig 	const struct nfsd4_operation *thisd = OPDESC(this);
2550bb2a8b0cSChristoph Hellwig 	const struct nfsd4_operation *nextd;
255168d93184SJ. Bruce Fields 
255268d93184SJ. Bruce Fields 	/*
255368d93184SJ. Bruce Fields 	 * Most ops check wronsec on our own; only the putfh-like ops
255468d93184SJ. Bruce Fields 	 * have special rules.
255568d93184SJ. Bruce Fields 	 */
255668d93184SJ. Bruce Fields 	if (!(thisd->op_flags & OP_IS_PUTFH_LIKE))
255768d93184SJ. Bruce Fields 		return false;
255868d93184SJ. Bruce Fields 	/*
255968d93184SJ. Bruce Fields 	 * rfc 5661 2.6.3.1.1.6: don't bother erroring out a
256068d93184SJ. Bruce Fields 	 * put-filehandle operation if we're not going to use the
256168d93184SJ. Bruce Fields 	 * result:
256268d93184SJ. Bruce Fields 	 */
256368d93184SJ. Bruce Fields 	if (argp->opcnt == resp->opcnt)
256468d93184SJ. Bruce Fields 		return false;
256551904b08SJ. Bruce Fields 	if (next->opnum == OP_ILLEGAL)
256651904b08SJ. Bruce Fields 		return false;
256768d93184SJ. Bruce Fields 	nextd = OPDESC(next);
256868d93184SJ. Bruce Fields 	/*
256968d93184SJ. Bruce Fields 	 * Rest of 2.6.3.1.1: certain operations will return WRONGSEC
257068d93184SJ. Bruce Fields 	 * errors themselves as necessary; others should check for them
257168d93184SJ. Bruce Fields 	 * now:
257268d93184SJ. Bruce Fields 	 */
257368d93184SJ. Bruce Fields 	return !(nextd->op_flags & OP_HANDLES_WRONGSEC);
257468d93184SJ. Bruce Fields }
257568d93184SJ. Bruce Fields 
2576b9e8638eSOlga Kornievskaia #ifdef CONFIG_NFSD_V4_2_INTER_SSC
2577b9e8638eSOlga Kornievskaia static void
2578b9e8638eSOlga Kornievskaia check_if_stalefh_allowed(struct nfsd4_compoundargs *args)
2579b9e8638eSOlga Kornievskaia {
2580b9e8638eSOlga Kornievskaia 	struct nfsd4_op	*op, *current_op = NULL, *saved_op = NULL;
2581b9e8638eSOlga Kornievskaia 	struct nfsd4_copy *copy;
2582b9e8638eSOlga Kornievskaia 	struct nfsd4_putfh *putfh;
2583b9e8638eSOlga Kornievskaia 	int i;
2584b9e8638eSOlga Kornievskaia 
2585b9e8638eSOlga Kornievskaia 	/* traverse all operation and if it's a COPY compound, mark the
2586b9e8638eSOlga Kornievskaia 	 * source filehandle to skip verification
2587b9e8638eSOlga Kornievskaia 	 */
2588b9e8638eSOlga Kornievskaia 	for (i = 0; i < args->opcnt; i++) {
2589b9e8638eSOlga Kornievskaia 		op = &args->ops[i];
2590b9e8638eSOlga Kornievskaia 		if (op->opnum == OP_PUTFH)
2591b9e8638eSOlga Kornievskaia 			current_op = op;
2592b9e8638eSOlga Kornievskaia 		else if (op->opnum == OP_SAVEFH)
2593b9e8638eSOlga Kornievskaia 			saved_op = current_op;
2594b9e8638eSOlga Kornievskaia 		else if (op->opnum == OP_RESTOREFH)
2595b9e8638eSOlga Kornievskaia 			current_op = saved_op;
2596b9e8638eSOlga Kornievskaia 		else if (op->opnum == OP_COPY) {
2597b9e8638eSOlga Kornievskaia 			copy = (struct nfsd4_copy *)&op->u;
2598b9e8638eSOlga Kornievskaia 			if (!saved_op) {
2599b9e8638eSOlga Kornievskaia 				op->status = nfserr_nofilehandle;
2600b9e8638eSOlga Kornievskaia 				return;
2601b9e8638eSOlga Kornievskaia 			}
2602b9e8638eSOlga Kornievskaia 			putfh = (struct nfsd4_putfh *)&saved_op->u;
2603*1913cdf5SChuck Lever 			if (nfsd4_ssc_is_inter(copy))
2604b9e8638eSOlga Kornievskaia 				putfh->no_verify = true;
2605b9e8638eSOlga Kornievskaia 		}
2606b9e8638eSOlga Kornievskaia 	}
2607b9e8638eSOlga Kornievskaia }
2608b9e8638eSOlga Kornievskaia #else
2609b9e8638eSOlga Kornievskaia static void
2610b9e8638eSOlga Kornievskaia check_if_stalefh_allowed(struct nfsd4_compoundargs *args)
2611b9e8638eSOlga Kornievskaia {
2612b9e8638eSOlga Kornievskaia }
2613b9e8638eSOlga Kornievskaia #endif
2614b9e8638eSOlga Kornievskaia 
2615f9bb94c4SAndy Adamson /*
26161da177e4SLinus Torvalds  * COMPOUND call.
26171da177e4SLinus Torvalds  */
26187111c66eSAl Viro static __be32
2619a6beb732SChristoph Hellwig nfsd4_proc_compound(struct svc_rqst *rqstp)
26201da177e4SLinus Torvalds {
2621a6beb732SChristoph Hellwig 	struct nfsd4_compoundargs *args = rqstp->rq_argp;
2622a6beb732SChristoph Hellwig 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
26231da177e4SLinus Torvalds 	struct nfsd4_op	*op;
2624e354d571SAndy Adamson 	struct nfsd4_compound_state *cstate = &resp->cstate;
26254daeed25SKinglong Mee 	struct svc_fh *current_fh = &cstate->current_fh;
26264daeed25SKinglong Mee 	struct svc_fh *save_fh = &cstate->save_fh;
2627e333f3bbSTrond Myklebust 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2628b37ad28bSAl Viro 	__be32		status;
26291da177e4SLinus Torvalds 
2630bddfdbcdSChuck Lever 	resp->xdr = &rqstp->rq_res_stream;
26313b0ebb25SChuck Lever 	resp->statusp = resp->xdr->p;
2632bddfdbcdSChuck Lever 
2633bddfdbcdSChuck Lever 	/* reserve space for: NFS status code */
2634bddfdbcdSChuck Lever 	xdr_reserve_space(resp->xdr, XDR_UNIT);
2635bddfdbcdSChuck Lever 
26361da177e4SLinus Torvalds 	/* reserve space for: taglen, tag, and opcnt */
2637bddfdbcdSChuck Lever 	xdr_reserve_space(resp->xdr, XDR_UNIT * 2 + args->taglen);
26381da177e4SLinus Torvalds 	resp->taglen = args->taglen;
26391da177e4SLinus Torvalds 	resp->tag = args->tag;
26401da177e4SLinus Torvalds 	resp->rqstp = rqstp;
26414daeed25SKinglong Mee 	cstate->minorversion = args->minorversion;
26424daeed25SKinglong Mee 	fh_init(current_fh, NFS4_FHSIZE);
26434daeed25SKinglong Mee 	fh_init(save_fh, NFS4_FHSIZE);
264428df0988SChuck Lever 
26458ff30fa4SNeilBrown 	/*
26468ff30fa4SNeilBrown 	 * Don't use the deferral mechanism for NFSv4; compounds make it
26478ff30fa4SNeilBrown 	 * too hard to avoid non-idempotency problems.
26488ff30fa4SNeilBrown 	 */
264928df0988SChuck Lever 	__clear_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
26501da177e4SLinus Torvalds 
26511da177e4SLinus Torvalds 	/*
26521da177e4SLinus Torvalds 	 * According to RFC3010, this takes precedence over all other errors.
26531da177e4SLinus Torvalds 	 */
26541da177e4SLinus Torvalds 	status = nfserr_minor_vers_mismatch;
2655e333f3bbSTrond Myklebust 	if (nfsd_minorversion(nn, args->minorversion, NFSD_TEST) <= 0)
26561da177e4SLinus Torvalds 		goto out;
26570078117cSJ. Bruce Fields 	status = nfserr_resource;
26580078117cSJ. Bruce Fields 	if (args->opcnt > NFSD_MAX_OPS_PER_COMPOUND)
26590078117cSJ. Bruce Fields 		goto out;
26601da177e4SLinus Torvalds 
266157716355SJ. Bruce Fields 	status = nfs41_check_op_ordering(args);
266257716355SJ. Bruce Fields 	if (status) {
2663f9bb94c4SAndy Adamson 		op = &args->ops[0];
266457716355SJ. Bruce Fields 		op->status = status;
26655b7b15aeSJ. Bruce Fields 		resp->opcnt = 1;
2666f9bb94c4SAndy Adamson 		goto encode_op;
2667f9bb94c4SAndy Adamson 	}
2668b9e8638eSOlga Kornievskaia 	check_if_stalefh_allowed(args);
2669f9bb94c4SAndy Adamson 
267028df3d15SJ. Bruce Fields 	rqstp->rq_lease_breaker = (void **)&cstate->clp;
267128df3d15SJ. Bruce Fields 
2672fff4080bSChuck Lever 	trace_nfsd_compound(rqstp, args->opcnt);
26731da177e4SLinus Torvalds 	while (!status && resp->opcnt < args->opcnt) {
26741da177e4SLinus Torvalds 		op = &args->ops[resp->opcnt++];
26751da177e4SLinus Torvalds 
26761da177e4SLinus Torvalds 		/*
26771da177e4SLinus Torvalds 		 * The XDR decode routines may have pre-set op->status;
26781da177e4SLinus Torvalds 		 * for example, if there is a miscellaneous XDR error
26791da177e4SLinus Torvalds 		 * it will be set to nfserr_bad_xdr.
26801da177e4SLinus Torvalds 		 */
26819d313b17SJ. Bruce Fields 		if (op->status) {
26829d313b17SJ. Bruce Fields 			if (op->opnum == OP_OPEN)
26839d313b17SJ. Bruce Fields 				op->status = nfsd4_open_omfg(rqstp, cstate, op);
26841da177e4SLinus Torvalds 			goto encode_op;
26859d313b17SJ. Bruce Fields 		}
2686b9e8638eSOlga Kornievskaia 		if (!current_fh->fh_dentry &&
2687b9e8638eSOlga Kornievskaia 				!HAS_FH_FLAG(current_fh, NFSD4_FH_FOREIGN)) {
2688f4f9ef4aSJ. Bruce Fields 			if (!(op->opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
26891da177e4SLinus Torvalds 				op->status = nfserr_nofilehandle;
26901da177e4SLinus Torvalds 				goto encode_op;
26911da177e4SLinus Torvalds 			}
2692b9e8638eSOlga Kornievskaia 		} else if (current_fh->fh_export &&
2693b9e8638eSOlga Kornievskaia 			   current_fh->fh_export->ex_fslocs.migrated &&
2694f4f9ef4aSJ. Bruce Fields 			  !(op->opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
269542ca0993SJ.Bruce Fields 			op->status = nfserr_moved;
269642ca0993SJ.Bruce Fields 			goto encode_op;
269742ca0993SJ.Bruce Fields 		}
2698b591480bSJ.Bruce Fields 
2699fcb5e3faSChuck Lever 		fh_clear_pre_post_attrs(current_fh);
27002336745eSKinglong Mee 
270158e7b33aSMi Jinlong 		/* If op is non-idempotent */
2702f4f9ef4aSJ. Bruce Fields 		if (op->opdesc->op_flags & OP_MODIFIES_SOMETHING) {
27034c69d585SJ. Bruce Fields 			/*
2704a8095f7eSJ. Bruce Fields 			 * Don't execute this op if we couldn't encode a
2705f532c9ffSZhang Jiaming 			 * successful reply:
2706a8095f7eSJ. Bruce Fields 			 */
2707f4f9ef4aSJ. Bruce Fields 			u32 plen = op->opdesc->op_rsize_bop(rqstp, op);
2708a8095f7eSJ. Bruce Fields 			/*
2709a8095f7eSJ. Bruce Fields 			 * Plus if there's another operation, make sure
27104c69d585SJ. Bruce Fields 			 * we'll have space to at least encode an error:
27114c69d585SJ. Bruce Fields 			 */
27124c69d585SJ. Bruce Fields 			if (resp->opcnt < args->opcnt)
27134c69d585SJ. Bruce Fields 				plen += COMPOUND_ERR_SLACK_SPACE;
271458e7b33aSMi Jinlong 			op->status = nfsd4_check_resp_size(resp, plen);
271558e7b33aSMi Jinlong 		}
271658e7b33aSMi Jinlong 
271758e7b33aSMi Jinlong 		if (op->status)
271858e7b33aSMi Jinlong 			goto encode_op;
271958e7b33aSMi Jinlong 
2720f4f9ef4aSJ. Bruce Fields 		if (op->opdesc->op_get_currentstateid)
2721f4f9ef4aSJ. Bruce Fields 			op->opdesc->op_get_currentstateid(cstate, &op->u);
2722f4f9ef4aSJ. Bruce Fields 		op->status = op->opdesc->op_func(rqstp, cstate, &op->u);
27231da177e4SLinus Torvalds 
27249a307403SJ. Bruce Fields 		/* Only from SEQUENCE */
27259a307403SJ. Bruce Fields 		if (cstate->status == nfserr_replay_cache) {
27269a307403SJ. Bruce Fields 			dprintk("%s NFS4.1 replay from cache\n", __func__);
27279a307403SJ. Bruce Fields 			status = op->status;
27289a307403SJ. Bruce Fields 			goto out;
27299a307403SJ. Bruce Fields 		}
27308b70484cSTigran Mkrtchyan 		if (!op->status) {
2731f4f9ef4aSJ. Bruce Fields 			if (op->opdesc->op_set_currentstateid)
2732f4f9ef4aSJ. Bruce Fields 				op->opdesc->op_set_currentstateid(cstate, &op->u);
27338b70484cSTigran Mkrtchyan 
2734f4f9ef4aSJ. Bruce Fields 			if (op->opdesc->op_flags & OP_CLEAR_STATEID)
273537c593c5STigran Mkrtchyan 				clear_current_stateid(cstate);
27368b70484cSTigran Mkrtchyan 
2737d781e3dfSJ. Bruce Fields 			if (current_fh->fh_export &&
2738d781e3dfSJ. Bruce Fields 					need_wrongsec_check(rqstp))
27394daeed25SKinglong Mee 				op->status = check_nfsd_access(current_fh->fh_export, rqstp);
27408b70484cSTigran Mkrtchyan 		}
27411da177e4SLinus Torvalds encode_op:
2742a90b061cSAl Viro 		if (op->status == nfserr_replay_me) {
2743a4f1706aSJ.Bruce Fields 			op->replay = &cstate->replay_owner->so_replay;
2744bddfdbcdSChuck Lever 			nfsd4_encode_replay(resp->xdr, op);
27451da177e4SLinus Torvalds 			status = op->status = op->replay->rp_status;
27461da177e4SLinus Torvalds 		} else {
27471da177e4SLinus Torvalds 			nfsd4_encode_operation(resp, op);
27481da177e4SLinus Torvalds 			status = op->status;
27491da177e4SLinus Torvalds 		}
27500407717dSBenny Halevy 
2751fff4080bSChuck Lever 		trace_nfsd_compound_status(args->opcnt, resp->opcnt, status,
2752fff4080bSChuck Lever 					   nfsd4_op_name(op->opnum));
27530407717dSBenny Halevy 
275458fb12e6SJeff Layton 		nfsd4_cstate_clear_replay(cstate);
2755e2b20950SShankar Anand 		nfsd4_increment_op_stats(op->opnum);
27561da177e4SLinus Torvalds 	}
27571da177e4SLinus Torvalds 
27584daeed25SKinglong Mee 	fh_put(current_fh);
27594daeed25SKinglong Mee 	fh_put(save_fh);
27604daeed25SKinglong Mee 	BUG_ON(cstate->replay_owner);
27611da177e4SLinus Torvalds out:
2762cc028a10SChuck Lever 	cstate->status = status;
27632f425878SAndy Adamson 	/* Reset deferral mechanism for RPC deferrals */
276428df0988SChuck Lever 	__set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
2765cc028a10SChuck Lever 	return rpc_success;
27661da177e4SLinus Torvalds }
27671da177e4SLinus Torvalds 
276858e7b33aSMi Jinlong #define op_encode_hdr_size		(2)
276958e7b33aSMi Jinlong #define op_encode_stateid_maxsz		(XDR_QUADLEN(NFS4_STATEID_SIZE))
277058e7b33aSMi Jinlong #define op_encode_verifier_maxsz	(XDR_QUADLEN(NFS4_VERIFIER_SIZE))
277158e7b33aSMi Jinlong #define op_encode_change_info_maxsz	(5)
277258e7b33aSMi Jinlong #define nfs4_fattr_bitmap_maxsz		(4)
277358e7b33aSMi Jinlong 
27748c7424cfSJ. Bruce Fields /* We'll fall back on returning no lockowner if run out of space: */
27758c7424cfSJ. Bruce Fields #define op_encode_lockowner_maxsz	(0)
277658e7b33aSMi Jinlong #define op_encode_lock_denied_maxsz	(8 + op_encode_lockowner_maxsz)
277758e7b33aSMi Jinlong 
277858e7b33aSMi Jinlong #define nfs4_owner_maxsz		(1 + XDR_QUADLEN(IDMAP_NAMESZ))
277958e7b33aSMi Jinlong 
278058e7b33aSMi Jinlong #define op_encode_ace_maxsz		(3 + nfs4_owner_maxsz)
278158e7b33aSMi Jinlong #define op_encode_delegation_maxsz	(1 + op_encode_stateid_maxsz + 1 + \
278258e7b33aSMi Jinlong 					 op_encode_ace_maxsz)
278358e7b33aSMi Jinlong 
278458e7b33aSMi Jinlong #define op_encode_channel_attrs_maxsz	(6 + 1 + 1)
278558e7b33aSMi Jinlong 
278658e7b33aSMi Jinlong static inline u32 nfsd4_only_status_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
278758e7b33aSMi Jinlong {
278858e7b33aSMi Jinlong 	return (op_encode_hdr_size) * sizeof(__be32);
278958e7b33aSMi Jinlong }
279058e7b33aSMi Jinlong 
279158e7b33aSMi Jinlong static inline u32 nfsd4_status_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
279258e7b33aSMi Jinlong {
279358e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_stateid_maxsz)* sizeof(__be32);
279458e7b33aSMi Jinlong }
279558e7b33aSMi Jinlong 
27962282cd2cSKinglong Mee static inline u32 nfsd4_access_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
27972282cd2cSKinglong Mee {
27982282cd2cSKinglong Mee 	/* ac_supported, ac_resp_access */
27992282cd2cSKinglong Mee 	return (op_encode_hdr_size + 2)* sizeof(__be32);
28002282cd2cSKinglong Mee }
28012282cd2cSKinglong Mee 
280258e7b33aSMi Jinlong static inline u32 nfsd4_commit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
280358e7b33aSMi Jinlong {
280458e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_verifier_maxsz) * sizeof(__be32);
280558e7b33aSMi Jinlong }
280658e7b33aSMi Jinlong 
280758e7b33aSMi Jinlong static inline u32 nfsd4_create_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
280858e7b33aSMi Jinlong {
280958e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_change_info_maxsz
281058e7b33aSMi Jinlong 		+ nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
281158e7b33aSMi Jinlong }
281258e7b33aSMi Jinlong 
2813b86cef60SJ. Bruce Fields /*
2814b86cef60SJ. Bruce Fields  * Note since this is an idempotent operation we won't insist on failing
2815b86cef60SJ. Bruce Fields  * the op prematurely if the estimate is too large.  We may turn off splice
2816b86cef60SJ. Bruce Fields  * reads unnecessarily.
2817b86cef60SJ. Bruce Fields  */
2818b86cef60SJ. Bruce Fields static inline u32 nfsd4_getattr_rsize(struct svc_rqst *rqstp,
2819b86cef60SJ. Bruce Fields 				      struct nfsd4_op *op)
2820b86cef60SJ. Bruce Fields {
2821b86cef60SJ. Bruce Fields 	u32 *bmap = op->u.getattr.ga_bmval;
2822b86cef60SJ. Bruce Fields 	u32 bmap0 = bmap[0], bmap1 = bmap[1], bmap2 = bmap[2];
2823b86cef60SJ. Bruce Fields 	u32 ret = 0;
2824b86cef60SJ. Bruce Fields 
2825b86cef60SJ. Bruce Fields 	if (bmap0 & FATTR4_WORD0_ACL)
2826b86cef60SJ. Bruce Fields 		return svc_max_payload(rqstp);
2827b86cef60SJ. Bruce Fields 	if (bmap0 & FATTR4_WORD0_FS_LOCATIONS)
2828b86cef60SJ. Bruce Fields 		return svc_max_payload(rqstp);
2829b86cef60SJ. Bruce Fields 
2830b86cef60SJ. Bruce Fields 	if (bmap1 & FATTR4_WORD1_OWNER) {
2831b86cef60SJ. Bruce Fields 		ret += IDMAP_NAMESZ + 4;
2832b86cef60SJ. Bruce Fields 		bmap1 &= ~FATTR4_WORD1_OWNER;
2833b86cef60SJ. Bruce Fields 	}
2834b86cef60SJ. Bruce Fields 	if (bmap1 & FATTR4_WORD1_OWNER_GROUP) {
2835b86cef60SJ. Bruce Fields 		ret += IDMAP_NAMESZ + 4;
2836b86cef60SJ. Bruce Fields 		bmap1 &= ~FATTR4_WORD1_OWNER_GROUP;
2837b86cef60SJ. Bruce Fields 	}
2838b86cef60SJ. Bruce Fields 	if (bmap0 & FATTR4_WORD0_FILEHANDLE) {
2839b86cef60SJ. Bruce Fields 		ret += NFS4_FHSIZE + 4;
2840b86cef60SJ. Bruce Fields 		bmap0 &= ~FATTR4_WORD0_FILEHANDLE;
2841b86cef60SJ. Bruce Fields 	}
2842b86cef60SJ. Bruce Fields 	if (bmap2 & FATTR4_WORD2_SECURITY_LABEL) {
28431ec8c0c4SKinglong Mee 		ret += NFS4_MAXLABELLEN + 12;
2844b86cef60SJ. Bruce Fields 		bmap2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2845b86cef60SJ. Bruce Fields 	}
2846b86cef60SJ. Bruce Fields 	/*
2847b86cef60SJ. Bruce Fields 	 * Largest of remaining attributes are 16 bytes (e.g.,
2848b86cef60SJ. Bruce Fields 	 * supported_attributes)
2849b86cef60SJ. Bruce Fields 	 */
2850b86cef60SJ. Bruce Fields 	ret += 16 * (hweight32(bmap0) + hweight32(bmap1) + hweight32(bmap2));
2851b86cef60SJ. Bruce Fields 	/* bitmask, length */
2852b86cef60SJ. Bruce Fields 	ret += 20;
2853b86cef60SJ. Bruce Fields 	return ret;
2854b86cef60SJ. Bruce Fields }
2855b86cef60SJ. Bruce Fields 
28562282cd2cSKinglong Mee static inline u32 nfsd4_getfh_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
28572282cd2cSKinglong Mee {
28582282cd2cSKinglong Mee 	return (op_encode_hdr_size + 1) * sizeof(__be32) + NFS4_FHSIZE;
28592282cd2cSKinglong Mee }
28602282cd2cSKinglong Mee 
286158e7b33aSMi Jinlong static inline u32 nfsd4_link_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
286258e7b33aSMi Jinlong {
286358e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
286458e7b33aSMi Jinlong 		* sizeof(__be32);
286558e7b33aSMi Jinlong }
286658e7b33aSMi Jinlong 
286758e7b33aSMi Jinlong static inline u32 nfsd4_lock_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
286858e7b33aSMi Jinlong {
286958e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_lock_denied_maxsz)
287058e7b33aSMi Jinlong 		* sizeof(__be32);
287158e7b33aSMi Jinlong }
287258e7b33aSMi Jinlong 
287358e7b33aSMi Jinlong static inline u32 nfsd4_open_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
287458e7b33aSMi Jinlong {
287558e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_stateid_maxsz
287658e7b33aSMi Jinlong 		+ op_encode_change_info_maxsz + 1
287758e7b33aSMi Jinlong 		+ nfs4_fattr_bitmap_maxsz
287858e7b33aSMi Jinlong 		+ op_encode_delegation_maxsz) * sizeof(__be32);
287958e7b33aSMi Jinlong }
288058e7b33aSMi Jinlong 
288158e7b33aSMi Jinlong static inline u32 nfsd4_read_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
288258e7b33aSMi Jinlong {
288358e7b33aSMi Jinlong 	u32 maxcount = 0, rlen = 0;
288458e7b33aSMi Jinlong 
288558e7b33aSMi Jinlong 	maxcount = svc_max_payload(rqstp);
28863c7aa15dSKinglong Mee 	rlen = min(op->u.read.rd_length, maxcount);
288758e7b33aSMi Jinlong 
2888622f560eSJ. Bruce Fields 	return (op_encode_hdr_size + 2 + XDR_QUADLEN(rlen)) * sizeof(__be32);
288958e7b33aSMi Jinlong }
289058e7b33aSMi Jinlong 
2891528b8493SAnna Schumaker static inline u32 nfsd4_read_plus_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2892528b8493SAnna Schumaker {
2893528b8493SAnna Schumaker 	u32 maxcount = svc_max_payload(rqstp);
2894528b8493SAnna Schumaker 	u32 rlen = min(op->u.read.rd_length, maxcount);
2895528b8493SAnna Schumaker 	/*
2896528b8493SAnna Schumaker 	 * If we detect that the file changed during hole encoding, then we
2897528b8493SAnna Schumaker 	 * recover by encoding the remaining reply as data. This means we need
2898528b8493SAnna Schumaker 	 * to set aside enough room to encode two data segments.
2899528b8493SAnna Schumaker 	 */
2900528b8493SAnna Schumaker 	u32 seg_len = 2 * (1 + 2 + 1);
2901528b8493SAnna Schumaker 
2902528b8493SAnna Schumaker 	return (op_encode_hdr_size + 2 + seg_len + XDR_QUADLEN(rlen)) * sizeof(__be32);
2903528b8493SAnna Schumaker }
2904528b8493SAnna Schumaker 
290558e7b33aSMi Jinlong static inline u32 nfsd4_readdir_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
290658e7b33aSMi Jinlong {
29073c7aa15dSKinglong Mee 	u32 maxcount = 0, rlen = 0;
290858e7b33aSMi Jinlong 
29093c7aa15dSKinglong Mee 	maxcount = svc_max_payload(rqstp);
29103c7aa15dSKinglong Mee 	rlen = min(op->u.readdir.rd_maxcount, maxcount);
291158e7b33aSMi Jinlong 
2912561f0ed4SJ. Bruce Fields 	return (op_encode_hdr_size + op_encode_verifier_maxsz +
2913561f0ed4SJ. Bruce Fields 		XDR_QUADLEN(rlen)) * sizeof(__be32);
291458e7b33aSMi Jinlong }
291558e7b33aSMi Jinlong 
29162282cd2cSKinglong Mee static inline u32 nfsd4_readlink_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
29172282cd2cSKinglong Mee {
29182282cd2cSKinglong Mee 	return (op_encode_hdr_size + 1) * sizeof(__be32) + PAGE_SIZE;
29192282cd2cSKinglong Mee }
29202282cd2cSKinglong Mee 
292158e7b33aSMi Jinlong static inline u32 nfsd4_remove_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
292258e7b33aSMi Jinlong {
292358e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
292458e7b33aSMi Jinlong 		* sizeof(__be32);
292558e7b33aSMi Jinlong }
292658e7b33aSMi Jinlong 
292758e7b33aSMi Jinlong static inline u32 nfsd4_rename_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
292858e7b33aSMi Jinlong {
292958e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_change_info_maxsz
293058e7b33aSMi Jinlong 		+ op_encode_change_info_maxsz) * sizeof(__be32);
293158e7b33aSMi Jinlong }
293258e7b33aSMi Jinlong 
2933ccae70a9SJ. Bruce Fields static inline u32 nfsd4_sequence_rsize(struct svc_rqst *rqstp,
2934ccae70a9SJ. Bruce Fields 				       struct nfsd4_op *op)
2935ccae70a9SJ. Bruce Fields {
2936d1d84c96SJ. Bruce Fields 	return (op_encode_hdr_size
2937d1d84c96SJ. Bruce Fields 		+ XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 5) * sizeof(__be32);
2938ccae70a9SJ. Bruce Fields }
2939ccae70a9SJ. Bruce Fields 
29402282cd2cSKinglong Mee static inline u32 nfsd4_test_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
29412282cd2cSKinglong Mee {
29422282cd2cSKinglong Mee 	return (op_encode_hdr_size + 1 + op->u.test_stateid.ts_num_ids)
29432282cd2cSKinglong Mee 		* sizeof(__be32);
29442282cd2cSKinglong Mee }
29452282cd2cSKinglong Mee 
294658e7b33aSMi Jinlong static inline u32 nfsd4_setattr_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
294758e7b33aSMi Jinlong {
294858e7b33aSMi Jinlong 	return (op_encode_hdr_size + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
294958e7b33aSMi Jinlong }
295058e7b33aSMi Jinlong 
29512282cd2cSKinglong Mee static inline u32 nfsd4_secinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
29522282cd2cSKinglong Mee {
29532282cd2cSKinglong Mee 	return (op_encode_hdr_size + RPC_AUTH_MAXFLAVOR *
29542282cd2cSKinglong Mee 		(4 + XDR_QUADLEN(GSS_OID_MAX_LEN))) * sizeof(__be32);
29552282cd2cSKinglong Mee }
29562282cd2cSKinglong Mee 
295758e7b33aSMi Jinlong static inline u32 nfsd4_setclientid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
295858e7b33aSMi Jinlong {
2959480efaeeSJ. Bruce Fields 	return (op_encode_hdr_size + 2 + XDR_QUADLEN(NFS4_VERIFIER_SIZE)) *
2960480efaeeSJ. Bruce Fields 								sizeof(__be32);
296158e7b33aSMi Jinlong }
296258e7b33aSMi Jinlong 
296358e7b33aSMi Jinlong static inline u32 nfsd4_write_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
296458e7b33aSMi Jinlong {
2965f34e432bSJ. Bruce Fields 	return (op_encode_hdr_size + 2 + op_encode_verifier_maxsz) * sizeof(__be32);
296658e7b33aSMi Jinlong }
296758e7b33aSMi Jinlong 
296858e7b33aSMi Jinlong static inline u32 nfsd4_exchange_id_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
296958e7b33aSMi Jinlong {
297058e7b33aSMi Jinlong 	return (op_encode_hdr_size + 2 + 1 + /* eir_clientid, eir_sequenceid */\
2971a8bb84bcSKinglong Mee 		1 + 1 + /* eir_flags, spr_how */\
2972a8bb84bcSKinglong Mee 		4 + /* spo_must_enforce & _allow with bitmap */\
297358e7b33aSMi Jinlong 		2 + /*eir_server_owner.so_minor_id */\
297458e7b33aSMi Jinlong 		/* eir_server_owner.so_major_id<> */\
297558e7b33aSMi Jinlong 		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
297658e7b33aSMi Jinlong 		/* eir_server_scope<> */\
297758e7b33aSMi Jinlong 		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
297858e7b33aSMi Jinlong 		1 + /* eir_server_impl_id array length */\
297958e7b33aSMi Jinlong 		0 /* ignored eir_server_impl_id contents */) * sizeof(__be32);
298058e7b33aSMi Jinlong }
298158e7b33aSMi Jinlong 
298258e7b33aSMi Jinlong static inline u32 nfsd4_bind_conn_to_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
298358e7b33aSMi Jinlong {
298458e7b33aSMi Jinlong 	return (op_encode_hdr_size + \
298558e7b33aSMi Jinlong 		XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* bctsr_sessid */\
298658e7b33aSMi Jinlong 		2 /* bctsr_dir, use_conn_in_rdma_mode */) * sizeof(__be32);
298758e7b33aSMi Jinlong }
298858e7b33aSMi Jinlong 
298958e7b33aSMi Jinlong static inline u32 nfsd4_create_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
299058e7b33aSMi Jinlong {
299158e7b33aSMi Jinlong 	return (op_encode_hdr_size + \
299258e7b33aSMi Jinlong 		XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* sessionid */\
299358e7b33aSMi Jinlong 		2 + /* csr_sequence, csr_flags */\
299458e7b33aSMi Jinlong 		op_encode_channel_attrs_maxsz + \
299558e7b33aSMi Jinlong 		op_encode_channel_attrs_maxsz) * sizeof(__be32);
299658e7b33aSMi Jinlong }
299758e7b33aSMi Jinlong 
299829ae7f9dSAnna Schumaker static inline u32 nfsd4_copy_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
299929ae7f9dSAnna Schumaker {
300029ae7f9dSAnna Schumaker 	return (op_encode_hdr_size +
300129ae7f9dSAnna Schumaker 		1 /* wr_callback */ +
300229ae7f9dSAnna Schumaker 		op_encode_stateid_maxsz /* wr_callback */ +
300329ae7f9dSAnna Schumaker 		2 /* wr_count */ +
300429ae7f9dSAnna Schumaker 		1 /* wr_committed */ +
300529ae7f9dSAnna Schumaker 		op_encode_verifier_maxsz +
300629ae7f9dSAnna Schumaker 		1 /* cr_consecutive */ +
300729ae7f9dSAnna Schumaker 		1 /* cr_synchronous */) * sizeof(__be32);
300829ae7f9dSAnna Schumaker }
300929ae7f9dSAnna Schumaker 
30106308bc98SOlga Kornievskaia static inline u32 nfsd4_offload_status_rsize(struct svc_rqst *rqstp,
30116308bc98SOlga Kornievskaia 					     struct nfsd4_op *op)
30126308bc98SOlga Kornievskaia {
30136308bc98SOlga Kornievskaia 	return (op_encode_hdr_size +
30146308bc98SOlga Kornievskaia 		2 /* osr_count */ +
30156308bc98SOlga Kornievskaia 		1 /* osr_complete<1> optional 0 for now */) * sizeof(__be32);
30166308bc98SOlga Kornievskaia }
30176308bc98SOlga Kornievskaia 
301851911868SOlga Kornievskaia static inline u32 nfsd4_copy_notify_rsize(struct svc_rqst *rqstp,
301951911868SOlga Kornievskaia 					struct nfsd4_op *op)
302051911868SOlga Kornievskaia {
302151911868SOlga Kornievskaia 	return (op_encode_hdr_size +
302251911868SOlga Kornievskaia 		3 /* cnr_lease_time */ +
302351911868SOlga Kornievskaia 		1 /* We support one cnr_source_server */ +
302451911868SOlga Kornievskaia 		1 /* cnr_stateid seq */ +
302551911868SOlga Kornievskaia 		op_encode_stateid_maxsz /* cnr_stateid */ +
302651911868SOlga Kornievskaia 		1 /* num cnr_source_server*/ +
302751911868SOlga Kornievskaia 		1 /* nl4_type */ +
302851911868SOlga Kornievskaia 		1 /* nl4 size */ +
302951911868SOlga Kornievskaia 		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) /*nl4_loc + nl4_loc_sz */)
303051911868SOlga Kornievskaia 		* sizeof(__be32);
303151911868SOlga Kornievskaia }
303251911868SOlga Kornievskaia 
30339cf514ccSChristoph Hellwig #ifdef CONFIG_NFSD_PNFS
30342282cd2cSKinglong Mee static inline u32 nfsd4_getdeviceinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
30352282cd2cSKinglong Mee {
30362282cd2cSKinglong Mee 	u32 maxcount = 0, rlen = 0;
30372282cd2cSKinglong Mee 
30382282cd2cSKinglong Mee 	maxcount = svc_max_payload(rqstp);
30392282cd2cSKinglong Mee 	rlen = min(op->u.getdeviceinfo.gd_maxcount, maxcount);
30402282cd2cSKinglong Mee 
30412282cd2cSKinglong Mee 	return (op_encode_hdr_size +
30422282cd2cSKinglong Mee 		1 /* gd_layout_type*/ +
30432282cd2cSKinglong Mee 		XDR_QUADLEN(rlen) +
30442282cd2cSKinglong Mee 		2 /* gd_notify_types */) * sizeof(__be32);
30452282cd2cSKinglong Mee }
30462282cd2cSKinglong Mee 
30479cf514ccSChristoph Hellwig /*
30489cf514ccSChristoph Hellwig  * At this stage we don't really know what layout driver will handle the request,
30499cf514ccSChristoph Hellwig  * so we need to define an arbitrary upper bound here.
30509cf514ccSChristoph Hellwig  */
30519cf514ccSChristoph Hellwig #define MAX_LAYOUT_SIZE		128
30529cf514ccSChristoph Hellwig static inline u32 nfsd4_layoutget_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
30539cf514ccSChristoph Hellwig {
30549cf514ccSChristoph Hellwig 	return (op_encode_hdr_size +
30559cf514ccSChristoph Hellwig 		1 /* logr_return_on_close */ +
30569cf514ccSChristoph Hellwig 		op_encode_stateid_maxsz +
30579cf514ccSChristoph Hellwig 		1 /* nr of layouts */ +
30589cf514ccSChristoph Hellwig 		MAX_LAYOUT_SIZE) * sizeof(__be32);
30599cf514ccSChristoph Hellwig }
30609cf514ccSChristoph Hellwig 
30619cf514ccSChristoph Hellwig static inline u32 nfsd4_layoutcommit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
30629cf514ccSChristoph Hellwig {
30639cf514ccSChristoph Hellwig 	return (op_encode_hdr_size +
30649cf514ccSChristoph Hellwig 		1 /* locr_newsize */ +
30659cf514ccSChristoph Hellwig 		2 /* ns_size */) * sizeof(__be32);
30669cf514ccSChristoph Hellwig }
30679cf514ccSChristoph Hellwig 
30689cf514ccSChristoph Hellwig static inline u32 nfsd4_layoutreturn_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
30699cf514ccSChristoph Hellwig {
30709cf514ccSChristoph Hellwig 	return (op_encode_hdr_size +
30719cf514ccSChristoph Hellwig 		1 /* lrs_stateid */ +
30729cf514ccSChristoph Hellwig 		op_encode_stateid_maxsz) * sizeof(__be32);
30739cf514ccSChristoph Hellwig }
30749cf514ccSChristoph Hellwig #endif /* CONFIG_NFSD_PNFS */
30759cf514ccSChristoph Hellwig 
30762282cd2cSKinglong Mee 
30772282cd2cSKinglong Mee static inline u32 nfsd4_seek_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
30782282cd2cSKinglong Mee {
30792282cd2cSKinglong Mee 	return (op_encode_hdr_size + 3) * sizeof(__be32);
30802282cd2cSKinglong Mee }
30812282cd2cSKinglong Mee 
308223e50fe3SFrank van der Linden static inline u32 nfsd4_getxattr_rsize(struct svc_rqst *rqstp,
308323e50fe3SFrank van der Linden 				       struct nfsd4_op *op)
308423e50fe3SFrank van der Linden {
308523e50fe3SFrank van der Linden 	u32 maxcount, rlen;
308623e50fe3SFrank van der Linden 
308723e50fe3SFrank van der Linden 	maxcount = svc_max_payload(rqstp);
308823e50fe3SFrank van der Linden 	rlen = min_t(u32, XATTR_SIZE_MAX, maxcount);
308923e50fe3SFrank van der Linden 
309023e50fe3SFrank van der Linden 	return (op_encode_hdr_size + 1 + XDR_QUADLEN(rlen)) * sizeof(__be32);
309123e50fe3SFrank van der Linden }
309223e50fe3SFrank van der Linden 
309323e50fe3SFrank van der Linden static inline u32 nfsd4_setxattr_rsize(struct svc_rqst *rqstp,
309423e50fe3SFrank van der Linden 				       struct nfsd4_op *op)
309523e50fe3SFrank van der Linden {
309623e50fe3SFrank van der Linden 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
309723e50fe3SFrank van der Linden 		* sizeof(__be32);
309823e50fe3SFrank van der Linden }
309923e50fe3SFrank van der Linden static inline u32 nfsd4_listxattrs_rsize(struct svc_rqst *rqstp,
310023e50fe3SFrank van der Linden 					 struct nfsd4_op *op)
310123e50fe3SFrank van der Linden {
310223e50fe3SFrank van der Linden 	u32 maxcount, rlen;
310323e50fe3SFrank van der Linden 
310423e50fe3SFrank van der Linden 	maxcount = svc_max_payload(rqstp);
310523e50fe3SFrank van der Linden 	rlen = min(op->u.listxattrs.lsxa_maxcount, maxcount);
310623e50fe3SFrank van der Linden 
310723e50fe3SFrank van der Linden 	return (op_encode_hdr_size + 4 + XDR_QUADLEN(rlen)) * sizeof(__be32);
310823e50fe3SFrank van der Linden }
310923e50fe3SFrank van der Linden 
311023e50fe3SFrank van der Linden static inline u32 nfsd4_removexattr_rsize(struct svc_rqst *rqstp,
311123e50fe3SFrank van der Linden 					  struct nfsd4_op *op)
311223e50fe3SFrank van der Linden {
311323e50fe3SFrank van der Linden 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
311423e50fe3SFrank van der Linden 		* sizeof(__be32);
311523e50fe3SFrank van der Linden }
311623e50fe3SFrank van der Linden 
311723e50fe3SFrank van der Linden 
3118bb2a8b0cSChristoph Hellwig static const struct nfsd4_operation nfsd4_ops[] = {
3119b591480bSJ.Bruce Fields 	[OP_ACCESS] = {
3120eb69853dSChristoph Hellwig 		.op_func = nfsd4_access,
3121b001a1b6SBenny Halevy 		.op_name = "OP_ACCESS",
31221c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_access_rsize,
3123b591480bSJ.Bruce Fields 	},
3124b591480bSJ.Bruce Fields 	[OP_CLOSE] = {
3125eb69853dSChristoph Hellwig 		.op_func = nfsd4_close,
312658e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
3127b001a1b6SBenny Halevy 		.op_name = "OP_CLOSE",
31281c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_status_stateid_rsize,
312957832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_closestateid,
3130b60e9859SChristoph Hellwig 		.op_set_currentstateid = nfsd4_set_closestateid,
3131b591480bSJ.Bruce Fields 	},
3132b591480bSJ.Bruce Fields 	[OP_COMMIT] = {
3133eb69853dSChristoph Hellwig 		.op_func = nfsd4_commit,
313458e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
3135b001a1b6SBenny Halevy 		.op_name = "OP_COMMIT",
31361c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_commit_rsize,
3137b591480bSJ.Bruce Fields 	},
3138b591480bSJ.Bruce Fields 	[OP_CREATE] = {
3139eb69853dSChristoph Hellwig 		.op_func = nfsd4_create,
3140d1471053STigran Mkrtchyan 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME | OP_CLEAR_STATEID,
3141b001a1b6SBenny Halevy 		.op_name = "OP_CREATE",
31421c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_create_rsize,
3143b591480bSJ.Bruce Fields 	},
3144b591480bSJ.Bruce Fields 	[OP_DELEGRETURN] = {
3145eb69853dSChristoph Hellwig 		.op_func = nfsd4_delegreturn,
314658e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
3147b001a1b6SBenny Halevy 		.op_name = "OP_DELEGRETURN",
314858e7b33aSMi Jinlong 		.op_rsize_bop = nfsd4_only_status_rsize,
314957832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_delegreturnstateid,
3150b591480bSJ.Bruce Fields 	},
3151b591480bSJ.Bruce Fields 	[OP_GETATTR] = {
3152eb69853dSChristoph Hellwig 		.op_func = nfsd4_getattr,
3153eeac294eSJ.Bruce Fields 		.op_flags = ALLOWED_ON_ABSENT_FS,
3154b86cef60SJ. Bruce Fields 		.op_rsize_bop = nfsd4_getattr_rsize,
3155b001a1b6SBenny Halevy 		.op_name = "OP_GETATTR",
3156b591480bSJ.Bruce Fields 	},
3157b591480bSJ.Bruce Fields 	[OP_GETFH] = {
3158eb69853dSChristoph Hellwig 		.op_func = nfsd4_getfh,
3159b001a1b6SBenny Halevy 		.op_name = "OP_GETFH",
31601c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_getfh_rsize,
3161b591480bSJ.Bruce Fields 	},
3162b591480bSJ.Bruce Fields 	[OP_LINK] = {
3163eb69853dSChristoph Hellwig 		.op_func = nfsd4_link,
3164c856694eSJ. Bruce Fields 		.op_flags = ALLOWED_ON_ABSENT_FS | OP_MODIFIES_SOMETHING
3165c856694eSJ. Bruce Fields 				| OP_CACHEME,
3166b001a1b6SBenny Halevy 		.op_name = "OP_LINK",
31671c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_link_rsize,
3168b591480bSJ.Bruce Fields 	},
3169b591480bSJ.Bruce Fields 	[OP_LOCK] = {
3170eb69853dSChristoph Hellwig 		.op_func = nfsd4_lock,
3171b7571e4cSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING |
3172b7571e4cSJ. Bruce Fields 				OP_NONTRIVIAL_ERROR_ENCODE,
3173b001a1b6SBenny Halevy 		.op_name = "OP_LOCK",
31741c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_lock_rsize,
3175b60e9859SChristoph Hellwig 		.op_set_currentstateid = nfsd4_set_lockstateid,
3176b591480bSJ.Bruce Fields 	},
3177b591480bSJ.Bruce Fields 	[OP_LOCKT] = {
3178eb69853dSChristoph Hellwig 		.op_func = nfsd4_lockt,
3179b7571e4cSJ. Bruce Fields 		.op_flags = OP_NONTRIVIAL_ERROR_ENCODE,
3180b001a1b6SBenny Halevy 		.op_name = "OP_LOCKT",
31811c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_lock_rsize,
3182b591480bSJ.Bruce Fields 	},
3183b591480bSJ.Bruce Fields 	[OP_LOCKU] = {
3184eb69853dSChristoph Hellwig 		.op_func = nfsd4_locku,
318558e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
3186b001a1b6SBenny Halevy 		.op_name = "OP_LOCKU",
31871c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_status_stateid_rsize,
318857832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_lockustateid,
3189b591480bSJ.Bruce Fields 	},
3190b591480bSJ.Bruce Fields 	[OP_LOOKUP] = {
3191eb69853dSChristoph Hellwig 		.op_func = nfsd4_lookup,
3192d1471053STigran Mkrtchyan 		.op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
3193b001a1b6SBenny Halevy 		.op_name = "OP_LOOKUP",
31941c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3195b591480bSJ.Bruce Fields 	},
3196b591480bSJ.Bruce Fields 	[OP_LOOKUPP] = {
3197eb69853dSChristoph Hellwig 		.op_func = nfsd4_lookupp,
3198d1471053STigran Mkrtchyan 		.op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
3199b001a1b6SBenny Halevy 		.op_name = "OP_LOOKUPP",
32001c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3201b591480bSJ.Bruce Fields 	},
3202b591480bSJ.Bruce Fields 	[OP_NVERIFY] = {
3203eb69853dSChristoph Hellwig 		.op_func = nfsd4_nverify,
3204b001a1b6SBenny Halevy 		.op_name = "OP_NVERIFY",
32051c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3206b591480bSJ.Bruce Fields 	},
3207b591480bSJ.Bruce Fields 	[OP_OPEN] = {
3208eb69853dSChristoph Hellwig 		.op_func = nfsd4_open,
320958e7b33aSMi Jinlong 		.op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
3210b001a1b6SBenny Halevy 		.op_name = "OP_OPEN",
32111c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_open_rsize,
3212b60e9859SChristoph Hellwig 		.op_set_currentstateid = nfsd4_set_openstateid,
3213b591480bSJ.Bruce Fields 	},
3214b591480bSJ.Bruce Fields 	[OP_OPEN_CONFIRM] = {
3215eb69853dSChristoph Hellwig 		.op_func = nfsd4_open_confirm,
321658e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
3217b001a1b6SBenny Halevy 		.op_name = "OP_OPEN_CONFIRM",
32181c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_status_stateid_rsize,
3219b591480bSJ.Bruce Fields 	},
3220b591480bSJ.Bruce Fields 	[OP_OPEN_DOWNGRADE] = {
3221eb69853dSChristoph Hellwig 		.op_func = nfsd4_open_downgrade,
322258e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
3223b001a1b6SBenny Halevy 		.op_name = "OP_OPEN_DOWNGRADE",
32241c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_status_stateid_rsize,
322557832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_opendowngradestateid,
3226b60e9859SChristoph Hellwig 		.op_set_currentstateid = nfsd4_set_opendowngradestateid,
3227b591480bSJ.Bruce Fields 	},
3228b591480bSJ.Bruce Fields 	[OP_PUTFH] = {
3229eb69853dSChristoph Hellwig 		.op_func = nfsd4_putfh,
323068d93184SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
32315b648699SJ. Bruce Fields 				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
3232b001a1b6SBenny Halevy 		.op_name = "OP_PUTFH",
32331c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3234b591480bSJ.Bruce Fields 	},
3235eeac294eSJ.Bruce Fields 	[OP_PUTPUBFH] = {
3236eb69853dSChristoph Hellwig 		.op_func = nfsd4_putrootfh,
323768d93184SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
32385b648699SJ. Bruce Fields 				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
3239b001a1b6SBenny Halevy 		.op_name = "OP_PUTPUBFH",
32401c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3241eeac294eSJ.Bruce Fields 	},
3242b591480bSJ.Bruce Fields 	[OP_PUTROOTFH] = {
3243eb69853dSChristoph Hellwig 		.op_func = nfsd4_putrootfh,
324468d93184SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
32455b648699SJ. Bruce Fields 				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
3246b001a1b6SBenny Halevy 		.op_name = "OP_PUTROOTFH",
32471c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3248b591480bSJ.Bruce Fields 	},
3249b591480bSJ.Bruce Fields 	[OP_READ] = {
3250eb69853dSChristoph Hellwig 		.op_func = nfsd4_read,
325134b1744cSJ. Bruce Fields 		.op_release = nfsd4_read_release,
3252b001a1b6SBenny Halevy 		.op_name = "OP_READ",
32531c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_read_rsize,
325457832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_readstateid,
3255b591480bSJ.Bruce Fields 	},
3256b591480bSJ.Bruce Fields 	[OP_READDIR] = {
3257eb69853dSChristoph Hellwig 		.op_func = nfsd4_readdir,
3258b001a1b6SBenny Halevy 		.op_name = "OP_READDIR",
32591c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_readdir_rsize,
3260b591480bSJ.Bruce Fields 	},
3261b591480bSJ.Bruce Fields 	[OP_READLINK] = {
3262eb69853dSChristoph Hellwig 		.op_func = nfsd4_readlink,
3263b001a1b6SBenny Halevy 		.op_name = "OP_READLINK",
32641c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_readlink_rsize,
3265b591480bSJ.Bruce Fields 	},
3266b591480bSJ.Bruce Fields 	[OP_REMOVE] = {
3267eb69853dSChristoph Hellwig 		.op_func = nfsd4_remove,
3268c856694eSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3269b001a1b6SBenny Halevy 		.op_name = "OP_REMOVE",
32701c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_remove_rsize,
3271b591480bSJ.Bruce Fields 	},
3272b591480bSJ.Bruce Fields 	[OP_RENAME] = {
3273eb69853dSChristoph Hellwig 		.op_func = nfsd4_rename,
3274c856694eSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
327558e7b33aSMi Jinlong 		.op_name = "OP_RENAME",
32761c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_rename_rsize,
3277b591480bSJ.Bruce Fields 	},
3278b591480bSJ.Bruce Fields 	[OP_RENEW] = {
3279eb69853dSChristoph Hellwig 		.op_func = nfsd4_renew,
328058e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
328158e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
3282b001a1b6SBenny Halevy 		.op_name = "OP_RENEW",
32831c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
328458e7b33aSMi Jinlong 
3285b591480bSJ.Bruce Fields 	},
3286b591480bSJ.Bruce Fields 	[OP_RESTOREFH] = {
3287eb69853dSChristoph Hellwig 		.op_func = nfsd4_restorefh,
328868d93184SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
328958e7b33aSMi Jinlong 				| OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING,
3290b001a1b6SBenny Halevy 		.op_name = "OP_RESTOREFH",
32911c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3292b591480bSJ.Bruce Fields 	},
3293b591480bSJ.Bruce Fields 	[OP_SAVEFH] = {
3294eb69853dSChristoph Hellwig 		.op_func = nfsd4_savefh,
329558e7b33aSMi Jinlong 		.op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
3296b001a1b6SBenny Halevy 		.op_name = "OP_SAVEFH",
32971c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3298b591480bSJ.Bruce Fields 	},
3299dcb488a3SAndy Adamson 	[OP_SECINFO] = {
3300eb69853dSChristoph Hellwig 		.op_func = nfsd4_secinfo,
330134b1744cSJ. Bruce Fields 		.op_release = nfsd4_secinfo_release,
330268d93184SJ. Bruce Fields 		.op_flags = OP_HANDLES_WRONGSEC,
3303b001a1b6SBenny Halevy 		.op_name = "OP_SECINFO",
33041c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_secinfo_rsize,
3305dcb488a3SAndy Adamson 	},
3306b591480bSJ.Bruce Fields 	[OP_SETATTR] = {
3307eb69853dSChristoph Hellwig 		.op_func = nfsd4_setattr,
3308b001a1b6SBenny Halevy 		.op_name = "OP_SETATTR",
3309b7571e4cSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME
3310b7571e4cSJ. Bruce Fields 				| OP_NONTRIVIAL_ERROR_ENCODE,
33111c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_setattr_rsize,
331257832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_setattrstateid,
3313b591480bSJ.Bruce Fields 	},
3314b591480bSJ.Bruce Fields 	[OP_SETCLIENTID] = {
3315eb69853dSChristoph Hellwig 		.op_func = nfsd4_setclientid,
331658e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3317b7571e4cSJ. Bruce Fields 				| OP_MODIFIES_SOMETHING | OP_CACHEME
3318b7571e4cSJ. Bruce Fields 				| OP_NONTRIVIAL_ERROR_ENCODE,
3319b001a1b6SBenny Halevy 		.op_name = "OP_SETCLIENTID",
33201c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_setclientid_rsize,
3321b591480bSJ.Bruce Fields 	},
3322b591480bSJ.Bruce Fields 	[OP_SETCLIENTID_CONFIRM] = {
3323eb69853dSChristoph Hellwig 		.op_func = nfsd4_setclientid_confirm,
332458e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3325c856694eSJ. Bruce Fields 				| OP_MODIFIES_SOMETHING | OP_CACHEME,
3326b001a1b6SBenny Halevy 		.op_name = "OP_SETCLIENTID_CONFIRM",
33271c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3328b591480bSJ.Bruce Fields 	},
3329b591480bSJ.Bruce Fields 	[OP_VERIFY] = {
3330eb69853dSChristoph Hellwig 		.op_func = nfsd4_verify,
3331b001a1b6SBenny Halevy 		.op_name = "OP_VERIFY",
33321c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3333b591480bSJ.Bruce Fields 	},
3334b591480bSJ.Bruce Fields 	[OP_WRITE] = {
3335eb69853dSChristoph Hellwig 		.op_func = nfsd4_write,
3336c856694eSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3337b001a1b6SBenny Halevy 		.op_name = "OP_WRITE",
33381c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_write_rsize,
333957832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_writestateid,
3340b591480bSJ.Bruce Fields 	},
3341b591480bSJ.Bruce Fields 	[OP_RELEASE_LOCKOWNER] = {
3342eb69853dSChristoph Hellwig 		.op_func = nfsd4_release_lockowner,
334358e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
334458e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
3345b001a1b6SBenny Halevy 		.op_name = "OP_RELEASE_LOCKOWNER",
33461c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3347b591480bSJ.Bruce Fields 	},
3348069b6ad4SAndy Adamson 
3349069b6ad4SAndy Adamson 	/* NFSv4.1 operations */
3350069b6ad4SAndy Adamson 	[OP_EXCHANGE_ID] = {
3351eb69853dSChristoph Hellwig 		.op_func = nfsd4_exchange_id,
335258e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
335358e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
3354069b6ad4SAndy Adamson 		.op_name = "OP_EXCHANGE_ID",
33551c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_exchange_id_rsize,
3356069b6ad4SAndy Adamson 	},
3357cb73a9f4SJ. Bruce Fields 	[OP_BACKCHANNEL_CTL] = {
3358eb69853dSChristoph Hellwig 		.op_func = nfsd4_backchannel_ctl,
3359cb73a9f4SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
3360cb73a9f4SJ. Bruce Fields 		.op_name = "OP_BACKCHANNEL_CTL",
33611c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3362cb73a9f4SJ. Bruce Fields 	},
33631d1bc8f2SJ. Bruce Fields 	[OP_BIND_CONN_TO_SESSION] = {
3364eb69853dSChristoph Hellwig 		.op_func = nfsd4_bind_conn_to_session,
336558e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
336658e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
33671d1bc8f2SJ. Bruce Fields 		.op_name = "OP_BIND_CONN_TO_SESSION",
33681c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_bind_conn_to_session_rsize,
33691d1bc8f2SJ. Bruce Fields 	},
3370069b6ad4SAndy Adamson 	[OP_CREATE_SESSION] = {
3371eb69853dSChristoph Hellwig 		.op_func = nfsd4_create_session,
337258e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
337358e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
3374069b6ad4SAndy Adamson 		.op_name = "OP_CREATE_SESSION",
33751c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_create_session_rsize,
3376069b6ad4SAndy Adamson 	},
3377069b6ad4SAndy Adamson 	[OP_DESTROY_SESSION] = {
3378eb69853dSChristoph Hellwig 		.op_func = nfsd4_destroy_session,
337958e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
338058e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
3381069b6ad4SAndy Adamson 		.op_name = "OP_DESTROY_SESSION",
33821c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3383069b6ad4SAndy Adamson 	},
3384069b6ad4SAndy Adamson 	[OP_SEQUENCE] = {
3385eb69853dSChristoph Hellwig 		.op_func = nfsd4_sequence,
3386f9bb94c4SAndy Adamson 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
3387069b6ad4SAndy Adamson 		.op_name = "OP_SEQUENCE",
33881c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_sequence_rsize,
3389069b6ad4SAndy Adamson 	},
3390094b5d74SBenny Halevy 	[OP_DESTROY_CLIENTID] = {
3391eb69853dSChristoph Hellwig 		.op_func = nfsd4_destroy_clientid,
339258e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
339358e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
3394094b5d74SBenny Halevy 		.op_name = "OP_DESTROY_CLIENTID",
33951c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3396094b5d74SBenny Halevy 	},
33974dc6ec00SJ. Bruce Fields 	[OP_RECLAIM_COMPLETE] = {
3398eb69853dSChristoph Hellwig 		.op_func = nfsd4_reclaim_complete,
339958e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
34004dc6ec00SJ. Bruce Fields 		.op_name = "OP_RECLAIM_COMPLETE",
34011c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
34024dc6ec00SJ. Bruce Fields 	},
340304f4ad16SJ. Bruce Fields 	[OP_SECINFO_NO_NAME] = {
3404eb69853dSChristoph Hellwig 		.op_func = nfsd4_secinfo_no_name,
3405ec572b9eSEryu Guan 		.op_release = nfsd4_secinfo_no_name_release,
340668d93184SJ. Bruce Fields 		.op_flags = OP_HANDLES_WRONGSEC,
340704f4ad16SJ. Bruce Fields 		.op_name = "OP_SECINFO_NO_NAME",
34081c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_secinfo_rsize,
340904f4ad16SJ. Bruce Fields 	},
341017456804SBryan Schumaker 	[OP_TEST_STATEID] = {
3411eb69853dSChristoph Hellwig 		.op_func = nfsd4_test_stateid,
341217456804SBryan Schumaker 		.op_flags = ALLOWED_WITHOUT_FH,
341317456804SBryan Schumaker 		.op_name = "OP_TEST_STATEID",
34141c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_test_stateid_rsize,
341517456804SBryan Schumaker 	},
3416e1ca12dfSBryan Schumaker 	[OP_FREE_STATEID] = {
3417eb69853dSChristoph Hellwig 		.op_func = nfsd4_free_stateid,
341858e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
3419e1ca12dfSBryan Schumaker 		.op_name = "OP_FREE_STATEID",
342057832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_freestateid,
34211c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3422e1ca12dfSBryan Schumaker 	},
34239cf514ccSChristoph Hellwig #ifdef CONFIG_NFSD_PNFS
34249cf514ccSChristoph Hellwig 	[OP_GETDEVICEINFO] = {
3425eb69853dSChristoph Hellwig 		.op_func = nfsd4_getdeviceinfo,
342634b1744cSJ. Bruce Fields 		.op_release = nfsd4_getdeviceinfo_release,
34279cf514ccSChristoph Hellwig 		.op_flags = ALLOWED_WITHOUT_FH,
34289cf514ccSChristoph Hellwig 		.op_name = "OP_GETDEVICEINFO",
34291c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_getdeviceinfo_rsize,
34309cf514ccSChristoph Hellwig 	},
34319cf514ccSChristoph Hellwig 	[OP_LAYOUTGET] = {
3432eb69853dSChristoph Hellwig 		.op_func = nfsd4_layoutget,
343334b1744cSJ. Bruce Fields 		.op_release = nfsd4_layoutget_release,
34349cf514ccSChristoph Hellwig 		.op_flags = OP_MODIFIES_SOMETHING,
34359cf514ccSChristoph Hellwig 		.op_name = "OP_LAYOUTGET",
34361c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_layoutget_rsize,
34379cf514ccSChristoph Hellwig 	},
34389cf514ccSChristoph Hellwig 	[OP_LAYOUTCOMMIT] = {
3439eb69853dSChristoph Hellwig 		.op_func = nfsd4_layoutcommit,
34409cf514ccSChristoph Hellwig 		.op_flags = OP_MODIFIES_SOMETHING,
34419cf514ccSChristoph Hellwig 		.op_name = "OP_LAYOUTCOMMIT",
34421c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_layoutcommit_rsize,
34439cf514ccSChristoph Hellwig 	},
34449cf514ccSChristoph Hellwig 	[OP_LAYOUTRETURN] = {
3445eb69853dSChristoph Hellwig 		.op_func = nfsd4_layoutreturn,
34469cf514ccSChristoph Hellwig 		.op_flags = OP_MODIFIES_SOMETHING,
34479cf514ccSChristoph Hellwig 		.op_name = "OP_LAYOUTRETURN",
34481c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_layoutreturn_rsize,
34499cf514ccSChristoph Hellwig 	},
34509cf514ccSChristoph Hellwig #endif /* CONFIG_NFSD_PNFS */
345124bab491SAnna Schumaker 
345224bab491SAnna Schumaker 	/* NFSv4.2 operations */
345395d871f0SAnna Schumaker 	[OP_ALLOCATE] = {
3454eb69853dSChristoph Hellwig 		.op_func = nfsd4_allocate,
345503b31f48SOlga Kornievskaia 		.op_flags = OP_MODIFIES_SOMETHING,
345695d871f0SAnna Schumaker 		.op_name = "OP_ALLOCATE",
34571c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
345895d871f0SAnna Schumaker 	},
3459b0cb9085SAnna Schumaker 	[OP_DEALLOCATE] = {
3460eb69853dSChristoph Hellwig 		.op_func = nfsd4_deallocate,
346103b31f48SOlga Kornievskaia 		.op_flags = OP_MODIFIES_SOMETHING,
3462b0cb9085SAnna Schumaker 		.op_name = "OP_DEALLOCATE",
34631c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3464b0cb9085SAnna Schumaker 	},
3465ffa0160aSChristoph Hellwig 	[OP_CLONE] = {
3466eb69853dSChristoph Hellwig 		.op_func = nfsd4_clone,
346703b31f48SOlga Kornievskaia 		.op_flags = OP_MODIFIES_SOMETHING,
3468ffa0160aSChristoph Hellwig 		.op_name = "OP_CLONE",
34691c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3470ffa0160aSChristoph Hellwig 	},
347129ae7f9dSAnna Schumaker 	[OP_COPY] = {
3472eb69853dSChristoph Hellwig 		.op_func = nfsd4_copy,
347303b31f48SOlga Kornievskaia 		.op_flags = OP_MODIFIES_SOMETHING,
347429ae7f9dSAnna Schumaker 		.op_name = "OP_COPY",
34751c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_copy_rsize,
347629ae7f9dSAnna Schumaker 	},
3477528b8493SAnna Schumaker 	[OP_READ_PLUS] = {
3478528b8493SAnna Schumaker 		.op_func = nfsd4_read,
3479528b8493SAnna Schumaker 		.op_release = nfsd4_read_release,
3480528b8493SAnna Schumaker 		.op_name = "OP_READ_PLUS",
3481528b8493SAnna Schumaker 		.op_rsize_bop = nfsd4_read_plus_rsize,
3482528b8493SAnna Schumaker 		.op_get_currentstateid = nfsd4_get_readstateid,
3483528b8493SAnna Schumaker 	},
348424bab491SAnna Schumaker 	[OP_SEEK] = {
3485eb69853dSChristoph Hellwig 		.op_func = nfsd4_seek,
348624bab491SAnna Schumaker 		.op_name = "OP_SEEK",
34871c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_seek_rsize,
348824bab491SAnna Schumaker 	},
34896308bc98SOlga Kornievskaia 	[OP_OFFLOAD_STATUS] = {
34906308bc98SOlga Kornievskaia 		.op_func = nfsd4_offload_status,
34916308bc98SOlga Kornievskaia 		.op_name = "OP_OFFLOAD_STATUS",
34926308bc98SOlga Kornievskaia 		.op_rsize_bop = nfsd4_offload_status_rsize,
34936308bc98SOlga Kornievskaia 	},
3494885e2bf3SOlga Kornievskaia 	[OP_OFFLOAD_CANCEL] = {
3495885e2bf3SOlga Kornievskaia 		.op_func = nfsd4_offload_cancel,
3496885e2bf3SOlga Kornievskaia 		.op_flags = OP_MODIFIES_SOMETHING,
3497885e2bf3SOlga Kornievskaia 		.op_name = "OP_OFFLOAD_CANCEL",
3498885e2bf3SOlga Kornievskaia 		.op_rsize_bop = nfsd4_only_status_rsize,
3499885e2bf3SOlga Kornievskaia 	},
350051911868SOlga Kornievskaia 	[OP_COPY_NOTIFY] = {
350151911868SOlga Kornievskaia 		.op_func = nfsd4_copy_notify,
350251911868SOlga Kornievskaia 		.op_flags = OP_MODIFIES_SOMETHING,
350351911868SOlga Kornievskaia 		.op_name = "OP_COPY_NOTIFY",
350451911868SOlga Kornievskaia 		.op_rsize_bop = nfsd4_copy_notify_rsize,
350551911868SOlga Kornievskaia 	},
350623e50fe3SFrank van der Linden 	[OP_GETXATTR] = {
350723e50fe3SFrank van der Linden 		.op_func = nfsd4_getxattr,
350823e50fe3SFrank van der Linden 		.op_name = "OP_GETXATTR",
350923e50fe3SFrank van der Linden 		.op_rsize_bop = nfsd4_getxattr_rsize,
351023e50fe3SFrank van der Linden 	},
351123e50fe3SFrank van der Linden 	[OP_SETXATTR] = {
351223e50fe3SFrank van der Linden 		.op_func = nfsd4_setxattr,
351323e50fe3SFrank van der Linden 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
351423e50fe3SFrank van der Linden 		.op_name = "OP_SETXATTR",
351523e50fe3SFrank van der Linden 		.op_rsize_bop = nfsd4_setxattr_rsize,
351623e50fe3SFrank van der Linden 	},
351723e50fe3SFrank van der Linden 	[OP_LISTXATTRS] = {
351823e50fe3SFrank van der Linden 		.op_func = nfsd4_listxattrs,
351923e50fe3SFrank van der Linden 		.op_name = "OP_LISTXATTRS",
352023e50fe3SFrank van der Linden 		.op_rsize_bop = nfsd4_listxattrs_rsize,
352123e50fe3SFrank van der Linden 	},
352223e50fe3SFrank van der Linden 	[OP_REMOVEXATTR] = {
352323e50fe3SFrank van der Linden 		.op_func = nfsd4_removexattr,
352423e50fe3SFrank van der Linden 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
352523e50fe3SFrank van der Linden 		.op_name = "OP_REMOVEXATTR",
352623e50fe3SFrank van der Linden 		.op_rsize_bop = nfsd4_removexattr_rsize,
352723e50fe3SFrank van der Linden 	},
3528b591480bSJ.Bruce Fields };
3529b591480bSJ.Bruce Fields 
3530ed941643SAndrew Elble /**
3531ed941643SAndrew Elble  * nfsd4_spo_must_allow - Determine if the compound op contains an
3532ed941643SAndrew Elble  * operation that is allowed to be sent with machine credentials
3533ed941643SAndrew Elble  *
3534ed941643SAndrew Elble  * @rqstp: a pointer to the struct svc_rqst
3535ed941643SAndrew Elble  *
3536ed941643SAndrew Elble  * Checks to see if the compound contains a spo_must_allow op
3537ed941643SAndrew Elble  * and confirms that it was sent with the proper machine creds.
3538ed941643SAndrew Elble  */
3539ed941643SAndrew Elble 
3540ed941643SAndrew Elble bool nfsd4_spo_must_allow(struct svc_rqst *rqstp)
3541ed941643SAndrew Elble {
3542ed941643SAndrew Elble 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
3543ed941643SAndrew Elble 	struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3544e34c0ce9SColin Ian King 	struct nfsd4_op *this;
3545ed941643SAndrew Elble 	struct nfsd4_compound_state *cstate = &resp->cstate;
3546ed941643SAndrew Elble 	struct nfs4_op_map *allow = &cstate->clp->cl_spo_must_allow;
3547ed941643SAndrew Elble 	u32 opiter;
3548ed941643SAndrew Elble 
3549ed941643SAndrew Elble 	if (!cstate->minorversion)
3550ed941643SAndrew Elble 		return false;
3551ed941643SAndrew Elble 
355244b49aa6SZheng Bin 	if (cstate->spo_must_allowed)
3553ed941643SAndrew Elble 		return true;
3554ed941643SAndrew Elble 
3555ed941643SAndrew Elble 	opiter = resp->opcnt;
3556ed941643SAndrew Elble 	while (opiter < argp->opcnt) {
3557ed941643SAndrew Elble 		this = &argp->ops[opiter++];
3558ed941643SAndrew Elble 		if (test_bit(this->opnum, allow->u.longs) &&
3559ed941643SAndrew Elble 			cstate->clp->cl_mach_cred &&
3560ed941643SAndrew Elble 			nfsd4_mach_creds_match(cstate->clp, rqstp)) {
3561ed941643SAndrew Elble 			cstate->spo_must_allowed = true;
3562ed941643SAndrew Elble 			return true;
3563ed941643SAndrew Elble 		}
3564ed941643SAndrew Elble 	}
3565ed941643SAndrew Elble 	cstate->spo_must_allowed = false;
3566ed941643SAndrew Elble 	return false;
3567ed941643SAndrew Elble }
3568ed941643SAndrew Elble 
35694f0cefbfSJ. Bruce Fields int nfsd4_max_reply(struct svc_rqst *rqstp, struct nfsd4_op *op)
35704f0cefbfSJ. Bruce Fields {
357105b7278dSOlga Kornievskaia 	if (op->opnum == OP_ILLEGAL || op->status == nfserr_notsupp)
35724f0cefbfSJ. Bruce Fields 		return op_encode_hdr_size * sizeof(__be32);
35732282cd2cSKinglong Mee 
35742282cd2cSKinglong Mee 	BUG_ON(OPDESC(op)->op_rsize_bop == NULL);
35752282cd2cSKinglong Mee 	return OPDESC(op)->op_rsize_bop(rqstp, op);
35764f0cefbfSJ. Bruce Fields }
35774f0cefbfSJ. Bruce Fields 
357807d1f802SJ. Bruce Fields void warn_on_nonidempotent_op(struct nfsd4_op *op)
357907d1f802SJ. Bruce Fields {
358007d1f802SJ. Bruce Fields 	if (OPDESC(op)->op_flags & OP_MODIFIES_SOMETHING) {
35813a237b4aSChuck Lever 		pr_err("unable to encode reply to nonidempotent op %u (%s)\n",
358207d1f802SJ. Bruce Fields 			op->opnum, nfsd4_op_name(op->opnum));
358307d1f802SJ. Bruce Fields 		WARN_ON_ONCE(1);
358407d1f802SJ. Bruce Fields 	}
358507d1f802SJ. Bruce Fields }
358607d1f802SJ. Bruce Fields 
3587f1c7f79bSAdrian Bunk static const char *nfsd4_op_name(unsigned opnum)
3588b001a1b6SBenny Halevy {
3589b001a1b6SBenny Halevy 	if (opnum < ARRAY_SIZE(nfsd4_ops))
3590b001a1b6SBenny Halevy 		return nfsd4_ops[opnum].op_name;
3591b001a1b6SBenny Halevy 	return "unknown_operation";
3592b001a1b6SBenny Halevy }
3593b001a1b6SBenny Halevy 
3594860bda29SChristoph Hellwig static const struct svc_procedure nfsd_procedures4[2] = {
35950a93a47fSYu Zhiguo 	[NFSPROC4_NULL] = {
3596f7235b6bSChristoph Hellwig 		.pc_func = nfsd4_proc_null,
3597788f7183SChuck Lever 		.pc_decode = nfssvc_decode_voidarg,
3598788f7183SChuck Lever 		.pc_encode = nfssvc_encode_voidres,
3599788f7183SChuck Lever 		.pc_argsize = sizeof(struct nfsd_voidargs),
3600788f7183SChuck Lever 		.pc_ressize = sizeof(struct nfsd_voidres),
36010a93a47fSYu Zhiguo 		.pc_cachetype = RC_NOCACHE,
36020a93a47fSYu Zhiguo 		.pc_xdrressize = 1,
36032289e87bSChuck Lever 		.pc_name = "NULL",
36040a93a47fSYu Zhiguo 	},
36050a93a47fSYu Zhiguo 	[NFSPROC4_COMPOUND] = {
3606f7235b6bSChristoph Hellwig 		.pc_func = nfsd4_proc_compound,
3607026fec7eSChristoph Hellwig 		.pc_decode = nfs4svc_decode_compoundargs,
360863f8de37SChristoph Hellwig 		.pc_encode = nfs4svc_encode_compoundres,
36090a93a47fSYu Zhiguo 		.pc_argsize = sizeof(struct nfsd4_compoundargs),
36100a93a47fSYu Zhiguo 		.pc_ressize = sizeof(struct nfsd4_compoundres),
36113e98abffSJ. Bruce Fields 		.pc_release = nfsd4_release_compoundargs,
36120a93a47fSYu Zhiguo 		.pc_cachetype = RC_NOCACHE,
36130a93a47fSYu Zhiguo 		.pc_xdrressize = NFSD_BUFSIZE/4,
36142289e87bSChuck Lever 		.pc_name = "COMPOUND",
36150a93a47fSYu Zhiguo 	},
36161da177e4SLinus Torvalds };
36171da177e4SLinus Torvalds 
36187fd38af9SChristoph Hellwig static unsigned int nfsd_count3[ARRAY_SIZE(nfsd_procedures4)];
3619e9679189SChristoph Hellwig const struct svc_version nfsd_version4 = {
36201da177e4SLinus Torvalds 	.vs_vers		= 4,
36211da177e4SLinus Torvalds 	.vs_nproc		= 2,
36221da177e4SLinus Torvalds 	.vs_proc		= nfsd_procedures4,
36237fd38af9SChristoph Hellwig 	.vs_count		= nfsd_count3,
36241da177e4SLinus Torvalds 	.vs_dispatch		= nfsd_dispatch,
36251da177e4SLinus Torvalds 	.vs_xdrsize		= NFS4_SVC_XDRSIZE,
362605a45a2dSJeff Layton 	.vs_rpcb_optnl		= true,
36275283b03eSJeff Layton 	.vs_need_cong_ctrl	= true,
36281da177e4SLinus Torvalds };
3629