xref: /openbmc/linux/fs/nfsd/nfs4proc.c (revision ecc23d0a422a3118fcf6e4f0a46e17a6c2047b02)
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 
671da177e4SLinus Torvalds #define NFSDDBG_FACILITY		NFSDDBG_PROC
681da177e4SLinus Torvalds 
693c8e0316SYu Zhiguo static u32 nfsd_attrmask[] = {
703c8e0316SYu Zhiguo 	NFSD_WRITEABLE_ATTRS_WORD0,
713c8e0316SYu Zhiguo 	NFSD_WRITEABLE_ATTRS_WORD1,
723c8e0316SYu Zhiguo 	NFSD_WRITEABLE_ATTRS_WORD2
733c8e0316SYu Zhiguo };
743c8e0316SYu Zhiguo 
753c8e0316SYu Zhiguo static u32 nfsd41_ex_attrmask[] = {
763c8e0316SYu Zhiguo 	NFSD_SUPPATTR_EXCLCREAT_WORD0,
773c8e0316SYu Zhiguo 	NFSD_SUPPATTR_EXCLCREAT_WORD1,
783c8e0316SYu Zhiguo 	NFSD_SUPPATTR_EXCLCREAT_WORD2
793c8e0316SYu Zhiguo };
803c8e0316SYu Zhiguo 
813c8e0316SYu Zhiguo static __be32
check_attr_support(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,u32 * bmval,u32 * writable)823c8e0316SYu Zhiguo check_attr_support(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
833c8e0316SYu Zhiguo 		   u32 *bmval, u32 *writable)
843c8e0316SYu Zhiguo {
853c8e0316SYu Zhiguo 	struct dentry *dentry = cstate->current_fh.fh_dentry;
8632ddd944SJ. Bruce Fields 	struct svc_export *exp = cstate->current_fh.fh_export;
873c8e0316SYu Zhiguo 
88916d2d84SJ. Bruce Fields 	if (!nfsd_attrs_supported(cstate->minorversion, bmval))
893c8e0316SYu Zhiguo 		return nfserr_attrnotsupp;
90916d2d84SJ. Bruce Fields 	if ((bmval[0] & FATTR4_WORD0_ACL) && !IS_POSIXACL(d_inode(dentry)))
913c8e0316SYu Zhiguo 		return nfserr_attrnotsupp;
9232ddd944SJ. Bruce Fields 	if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) &&
9332ddd944SJ. Bruce Fields 			!(exp->ex_flags & NFSEXP_SECURITY_LABEL))
9432ddd944SJ. Bruce Fields 		return nfserr_attrnotsupp;
95916d2d84SJ. Bruce Fields 	if (writable && !bmval_is_subset(bmval, writable))
963c8e0316SYu Zhiguo 		return nfserr_inval;
9747057abdSAndreas Gruenbacher 	if (writable && (bmval[2] & FATTR4_WORD2_MODE_UMASK) &&
9847057abdSAndreas Gruenbacher 			(bmval[1] & FATTR4_WORD1_MODE))
9947057abdSAndreas Gruenbacher 		return nfserr_inval;
1003c8e0316SYu Zhiguo 	return nfs_ok;
1013c8e0316SYu Zhiguo }
1023c8e0316SYu Zhiguo 
1033c8e0316SYu Zhiguo static __be32
nfsd4_check_open_attributes(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_open * open)1043c8e0316SYu Zhiguo nfsd4_check_open_attributes(struct svc_rqst *rqstp,
1053c8e0316SYu Zhiguo 	struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
1063c8e0316SYu Zhiguo {
1073c8e0316SYu Zhiguo 	__be32 status = nfs_ok;
1083c8e0316SYu Zhiguo 
1093c8e0316SYu Zhiguo 	if (open->op_create == NFS4_OPEN_CREATE) {
1103c8e0316SYu Zhiguo 		if (open->op_createmode == NFS4_CREATE_UNCHECKED
1113c8e0316SYu Zhiguo 		    || open->op_createmode == NFS4_CREATE_GUARDED)
1123c8e0316SYu Zhiguo 			status = check_attr_support(rqstp, cstate,
1133c8e0316SYu Zhiguo 					open->op_bmval, nfsd_attrmask);
1143c8e0316SYu Zhiguo 		else if (open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1)
1153c8e0316SYu Zhiguo 			status = check_attr_support(rqstp, cstate,
1163c8e0316SYu Zhiguo 					open->op_bmval, nfsd41_ex_attrmask);
1173c8e0316SYu Zhiguo 	}
1183c8e0316SYu Zhiguo 
1193c8e0316SYu Zhiguo 	return status;
1203c8e0316SYu Zhiguo }
1213c8e0316SYu Zhiguo 
1229208faf2SYu Zhiguo static int
is_create_with_attrs(struct nfsd4_open * open)1239208faf2SYu Zhiguo is_create_with_attrs(struct nfsd4_open *open)
1249208faf2SYu Zhiguo {
1259208faf2SYu Zhiguo 	return open->op_create == NFS4_OPEN_CREATE
1269208faf2SYu Zhiguo 		&& (open->op_createmode == NFS4_CREATE_UNCHECKED
1279208faf2SYu Zhiguo 		    || open->op_createmode == NFS4_CREATE_GUARDED
1289208faf2SYu Zhiguo 		    || open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1);
1299208faf2SYu Zhiguo }
1309208faf2SYu Zhiguo 
1311da177e4SLinus Torvalds static inline void
fh_dup2(struct svc_fh * dst,struct svc_fh * src)1321da177e4SLinus Torvalds fh_dup2(struct svc_fh *dst, struct svc_fh *src)
1331da177e4SLinus Torvalds {
1341da177e4SLinus Torvalds 	fh_put(dst);
1351da177e4SLinus Torvalds 	dget(src->fh_dentry);
1361da177e4SLinus Torvalds 	if (src->fh_export)
137bf18f163SKinglong Mee 		exp_get(src->fh_export);
1381da177e4SLinus Torvalds 	*dst = *src;
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds 
141b37ad28bSAl Viro static __be32
do_open_permission(struct svc_rqst * rqstp,struct svc_fh * current_fh,struct nfsd4_open * open,int accmode)142dc730e17SJ. Bruce Fields do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, int accmode)
1431da177e4SLinus Torvalds {
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	if (open->op_truncate &&
1461da177e4SLinus Torvalds 		!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
1471da177e4SLinus Torvalds 		return nfserr_inval;
1481da177e4SLinus Torvalds 
149a043226bSJ. Bruce Fields 	accmode |= NFSD_MAY_READ_IF_EXEC;
150a043226bSJ. Bruce Fields 
1511da177e4SLinus Torvalds 	if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
1528837abcaSMiklos Szeredi 		accmode |= NFSD_MAY_READ;
1539801d8a3SJ. Bruce Fields 	if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1548837abcaSMiklos Szeredi 		accmode |= (NFSD_MAY_WRITE | NFSD_MAY_TRUNC);
15557ecb34fSJ. Bruce Fields 	if (open->op_share_deny & NFS4_SHARE_DENY_READ)
1568837abcaSMiklos Szeredi 		accmode |= NFSD_MAY_WRITE;
1571da177e4SLinus Torvalds 
1584ab3442cSJinpeng Cui 	return fh_verify(rqstp, current_fh, S_IFREG, accmode);
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds 
nfsd_check_obj_isreg(struct svc_fh * fh)161aadab6c6SJ. Bruce Fields static __be32 nfsd_check_obj_isreg(struct svc_fh *fh)
162aadab6c6SJ. Bruce Fields {
1632b0143b5SDavid Howells 	umode_t mode = d_inode(fh->fh_dentry)->i_mode;
164aadab6c6SJ. Bruce Fields 
165aadab6c6SJ. Bruce Fields 	if (S_ISREG(mode))
166aadab6c6SJ. Bruce Fields 		return nfs_ok;
167aadab6c6SJ. Bruce Fields 	if (S_ISDIR(mode))
168aadab6c6SJ. Bruce Fields 		return nfserr_isdir;
169aadab6c6SJ. Bruce Fields 	/*
170aadab6c6SJ. Bruce Fields 	 * Using err_symlink as our catch-all case may look odd; but
171aadab6c6SJ. Bruce Fields 	 * there's no other obvious error for this case in 4.0, and we
172aadab6c6SJ. Bruce Fields 	 * happen to know that it will cause the linux v4 client to do
173aadab6c6SJ. Bruce Fields 	 * the right thing on attempts to open something other than a
174aadab6c6SJ. Bruce Fields 	 * regular file.
175aadab6c6SJ. Bruce Fields 	 */
176aadab6c6SJ. Bruce Fields 	return nfserr_symlink;
177aadab6c6SJ. Bruce Fields }
178aadab6c6SJ. Bruce Fields 
nfsd4_set_open_owner_reply_cache(struct nfsd4_compound_state * cstate,struct nfsd4_open * open,struct svc_fh * resfh)179bbc9c36cSJ. Bruce Fields static void nfsd4_set_open_owner_reply_cache(struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh *resfh)
1801da177e4SLinus Torvalds {
181bbc9c36cSJ. Bruce Fields 	if (nfsd4_has_session(cstate))
182bbc9c36cSJ. Bruce Fields 		return;
183bbc9c36cSJ. Bruce Fields 	fh_copy_shallow(&open->op_openowner->oo_owner.so_replay.rp_openfh,
184bbc9c36cSJ. Bruce Fields 			&resfh->fh_handle);
185bbc9c36cSJ. Bruce Fields }
186bbc9c36cSJ. Bruce Fields 
nfsd4_create_is_exclusive(int createmode)187254454a5SChuck Lever static inline bool nfsd4_create_is_exclusive(int createmode)
188254454a5SChuck Lever {
189254454a5SChuck Lever 	return createmode == NFS4_CREATE_EXCLUSIVE ||
190254454a5SChuck Lever 		createmode == NFS4_CREATE_EXCLUSIVE4_1;
191254454a5SChuck Lever }
192254454a5SChuck Lever 
193fb70bf12SChuck Lever static __be32
nfsd4_vfs_create(struct svc_fh * fhp,struct dentry * child,struct nfsd4_open * open)194fb70bf12SChuck Lever nfsd4_vfs_create(struct svc_fh *fhp, struct dentry *child,
195fb70bf12SChuck Lever 		 struct nfsd4_open *open)
196fb70bf12SChuck Lever {
197fb70bf12SChuck Lever 	struct file *filp;
198fb70bf12SChuck Lever 	struct path path;
199fb70bf12SChuck Lever 	int oflags;
200fb70bf12SChuck Lever 
201fb70bf12SChuck Lever 	oflags = O_CREAT | O_LARGEFILE;
202fb70bf12SChuck Lever 	switch (open->op_share_access & NFS4_SHARE_ACCESS_BOTH) {
203fb70bf12SChuck Lever 	case NFS4_SHARE_ACCESS_WRITE:
204fb70bf12SChuck Lever 		oflags |= O_WRONLY;
205fb70bf12SChuck Lever 		break;
206fb70bf12SChuck Lever 	case NFS4_SHARE_ACCESS_BOTH:
207fb70bf12SChuck Lever 		oflags |= O_RDWR;
208fb70bf12SChuck Lever 		break;
209fb70bf12SChuck Lever 	default:
210fb70bf12SChuck Lever 		oflags |= O_RDONLY;
211fb70bf12SChuck Lever 	}
212fb70bf12SChuck Lever 
213fb70bf12SChuck Lever 	path.mnt = fhp->fh_export->ex_path.mnt;
214fb70bf12SChuck Lever 	path.dentry = child;
215fb70bf12SChuck Lever 	filp = dentry_create(&path, oflags, open->op_iattr.ia_mode,
216fb70bf12SChuck Lever 			     current_cred());
217fb70bf12SChuck Lever 	if (IS_ERR(filp))
218fb70bf12SChuck Lever 		return nfserrno(PTR_ERR(filp));
219fb70bf12SChuck Lever 
220fb70bf12SChuck Lever 	open->op_filp = filp;
221fb70bf12SChuck Lever 	return nfs_ok;
222fb70bf12SChuck Lever }
223fb70bf12SChuck Lever 
224254454a5SChuck Lever /*
225254454a5SChuck Lever  * Implement NFSv4's unchecked, guarded, and exclusive create
226254454a5SChuck Lever  * semantics for regular files. Open state for this new file is
227254454a5SChuck Lever  * subsequently fabricated in nfsd4_process_open2().
228254454a5SChuck Lever  *
229254454a5SChuck Lever  * Upon return, caller must release @fhp and @resfhp.
230254454a5SChuck Lever  */
231254454a5SChuck Lever static __be32
nfsd4_create_file(struct svc_rqst * rqstp,struct svc_fh * fhp,struct svc_fh * resfhp,struct nfsd4_open * open)232254454a5SChuck Lever nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
233254454a5SChuck Lever 		  struct svc_fh *resfhp, struct nfsd4_open *open)
234254454a5SChuck Lever {
235254454a5SChuck Lever 	struct iattr *iap = &open->op_iattr;
2367fe2a71dSNeilBrown 	struct nfsd_attrs attrs = {
2377fe2a71dSNeilBrown 		.na_iattr	= iap,
238d6a97d3fSNeilBrown 		.na_seclabel	= &open->op_label,
2397fe2a71dSNeilBrown 	};
240254454a5SChuck Lever 	struct dentry *parent, *child;
241254454a5SChuck Lever 	__u32 v_mtime, v_atime;
242254454a5SChuck Lever 	struct inode *inode;
243254454a5SChuck Lever 	__be32 status;
244254454a5SChuck Lever 	int host_err;
245254454a5SChuck Lever 
246254454a5SChuck Lever 	if (isdotent(open->op_fname, open->op_fnamelen))
247254454a5SChuck Lever 		return nfserr_exist;
248254454a5SChuck Lever 	if (!(iap->ia_valid & ATTR_MODE))
249254454a5SChuck Lever 		iap->ia_mode = 0;
250254454a5SChuck Lever 
251254454a5SChuck Lever 	status = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
252254454a5SChuck Lever 	if (status != nfs_ok)
253254454a5SChuck Lever 		return status;
254254454a5SChuck Lever 	parent = fhp->fh_dentry;
255254454a5SChuck Lever 	inode = d_inode(parent);
256254454a5SChuck Lever 
257254454a5SChuck Lever 	host_err = fh_want_write(fhp);
258254454a5SChuck Lever 	if (host_err)
259254454a5SChuck Lever 		return nfserrno(host_err);
260254454a5SChuck Lever 
261c0cbe707SNeilBrown 	if (is_create_with_attrs(open))
262c0cbe707SNeilBrown 		nfsd4_acl_to_attr(NF4REG, open->op_acl, &attrs);
263c0cbe707SNeilBrown 
264debf16f0SNeilBrown 	inode_lock_nested(inode, I_MUTEX_PARENT);
265254454a5SChuck Lever 
266254454a5SChuck Lever 	child = lookup_one_len(open->op_fname, parent, open->op_fnamelen);
267254454a5SChuck Lever 	if (IS_ERR(child)) {
268254454a5SChuck Lever 		status = nfserrno(PTR_ERR(child));
269254454a5SChuck Lever 		goto out;
270254454a5SChuck Lever 	}
271254454a5SChuck Lever 
272254454a5SChuck Lever 	if (d_really_is_negative(child)) {
273254454a5SChuck Lever 		status = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
274254454a5SChuck Lever 		if (status != nfs_ok)
275254454a5SChuck Lever 			goto out;
276254454a5SChuck Lever 	}
277254454a5SChuck Lever 
278254454a5SChuck Lever 	status = fh_compose(resfhp, fhp->fh_export, child, fhp);
279254454a5SChuck Lever 	if (status != nfs_ok)
280254454a5SChuck Lever 		goto out;
281254454a5SChuck Lever 
282254454a5SChuck Lever 	v_mtime = 0;
283254454a5SChuck Lever 	v_atime = 0;
284254454a5SChuck Lever 	if (nfsd4_create_is_exclusive(open->op_createmode)) {
285254454a5SChuck Lever 		u32 *verifier = (u32 *)open->op_verf.data;
286254454a5SChuck Lever 
287254454a5SChuck Lever 		/*
288254454a5SChuck Lever 		 * Solaris 7 gets confused (bugid 4218508) if these have
289254454a5SChuck Lever 		 * the high bit set, as do xfs filesystems without the
290254454a5SChuck Lever 		 * "bigtime" feature. So just clear the high bits. If this
291254454a5SChuck Lever 		 * is ever changed to use different attrs for storing the
292254454a5SChuck Lever 		 * verifier, then do_open_lookup() will also need to be
293254454a5SChuck Lever 		 * fixed accordingly.
294254454a5SChuck Lever 		 */
295254454a5SChuck Lever 		v_mtime = verifier[0] & 0x7fffffff;
296254454a5SChuck Lever 		v_atime = verifier[1] & 0x7fffffff;
297254454a5SChuck Lever 	}
298254454a5SChuck Lever 
299254454a5SChuck Lever 	if (d_really_is_positive(child)) {
30019d008b4SNeilBrown 		/* NFSv4 protocol requires change attributes even though
30119d008b4SNeilBrown 		 * no change happened.
30219d008b4SNeilBrown 		 */
303a332018aSJeff Layton 		status = fh_fill_both_attrs(fhp);
304a332018aSJeff Layton 		if (status != nfs_ok)
305a332018aSJeff Layton 			goto out;
30619d008b4SNeilBrown 
307254454a5SChuck Lever 		switch (open->op_createmode) {
308254454a5SChuck Lever 		case NFS4_CREATE_UNCHECKED:
309254454a5SChuck Lever 			if (!d_is_reg(child))
310254454a5SChuck Lever 				break;
311254454a5SChuck Lever 
312254454a5SChuck Lever 			/*
313254454a5SChuck Lever 			 * In NFSv4, we don't want to truncate the file
314254454a5SChuck Lever 			 * now. This would be wrong if the OPEN fails for
315254454a5SChuck Lever 			 * some other reason. Furthermore, if the size is
316254454a5SChuck Lever 			 * nonzero, we should ignore it according to spec!
317254454a5SChuck Lever 			 */
318254454a5SChuck Lever 			open->op_truncate = (iap->ia_valid & ATTR_SIZE) &&
319254454a5SChuck Lever 						!iap->ia_size;
320254454a5SChuck Lever 			break;
321254454a5SChuck Lever 		case NFS4_CREATE_GUARDED:
322254454a5SChuck Lever 			status = nfserr_exist;
323254454a5SChuck Lever 			break;
324254454a5SChuck Lever 		case NFS4_CREATE_EXCLUSIVE:
325254454a5SChuck Lever 			if (d_inode(child)->i_mtime.tv_sec == v_mtime &&
326254454a5SChuck Lever 			    d_inode(child)->i_atime.tv_sec == v_atime &&
327254454a5SChuck Lever 			    d_inode(child)->i_size == 0) {
328254454a5SChuck Lever 				open->op_created = true;
329254454a5SChuck Lever 				break;		/* subtle */
330254454a5SChuck Lever 			}
331254454a5SChuck Lever 			status = nfserr_exist;
332254454a5SChuck Lever 			break;
333254454a5SChuck Lever 		case NFS4_CREATE_EXCLUSIVE4_1:
334254454a5SChuck Lever 			if (d_inode(child)->i_mtime.tv_sec == v_mtime &&
335254454a5SChuck Lever 			    d_inode(child)->i_atime.tv_sec == v_atime &&
336254454a5SChuck Lever 			    d_inode(child)->i_size == 0) {
337254454a5SChuck Lever 				open->op_created = true;
338254454a5SChuck Lever 				goto set_attr;	/* subtle */
339254454a5SChuck Lever 			}
340254454a5SChuck Lever 			status = nfserr_exist;
341254454a5SChuck Lever 		}
342254454a5SChuck Lever 		goto out;
343254454a5SChuck Lever 	}
344254454a5SChuck Lever 
345254454a5SChuck Lever 	if (!IS_POSIXACL(inode))
346254454a5SChuck Lever 		iap->ia_mode &= ~current_umask();
347254454a5SChuck Lever 
348a332018aSJeff Layton 	status = fh_fill_pre_attrs(fhp);
349a332018aSJeff Layton 	if (status != nfs_ok)
350a332018aSJeff Layton 		goto out;
351fb70bf12SChuck Lever 	status = nfsd4_vfs_create(fhp, child, open);
352fb70bf12SChuck Lever 	if (status != nfs_ok)
353254454a5SChuck Lever 		goto out;
354254454a5SChuck Lever 	open->op_created = true;
355debf16f0SNeilBrown 	fh_fill_post_attrs(fhp);
356254454a5SChuck Lever 
357254454a5SChuck Lever 	/* A newly created file already has a file size of zero. */
358254454a5SChuck Lever 	if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
359254454a5SChuck Lever 		iap->ia_valid &= ~ATTR_SIZE;
360254454a5SChuck Lever 	if (nfsd4_create_is_exclusive(open->op_createmode)) {
361254454a5SChuck Lever 		iap->ia_valid = ATTR_MTIME | ATTR_ATIME |
362254454a5SChuck Lever 				ATTR_MTIME_SET|ATTR_ATIME_SET;
363254454a5SChuck Lever 		iap->ia_mtime.tv_sec = v_mtime;
364254454a5SChuck Lever 		iap->ia_atime.tv_sec = v_atime;
365254454a5SChuck Lever 		iap->ia_mtime.tv_nsec = 0;
366254454a5SChuck Lever 		iap->ia_atime.tv_nsec = 0;
367254454a5SChuck Lever 	}
368254454a5SChuck Lever 
369254454a5SChuck Lever set_attr:
3707fe2a71dSNeilBrown 	status = nfsd_create_setattr(rqstp, fhp, resfhp, &attrs);
371254454a5SChuck Lever 
372d6a97d3fSNeilBrown 	if (attrs.na_labelerr)
373d6a97d3fSNeilBrown 		open->op_bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
374c0cbe707SNeilBrown 	if (attrs.na_aclerr)
375c0cbe707SNeilBrown 		open->op_bmval[0] &= ~FATTR4_WORD0_ACL;
376254454a5SChuck Lever out:
377debf16f0SNeilBrown 	inode_unlock(inode);
378c0cbe707SNeilBrown 	nfsd_attrs_free(&attrs);
379254454a5SChuck Lever 	if (child && !IS_ERR(child))
380254454a5SChuck Lever 		dput(child);
381254454a5SChuck Lever 	fh_drop_write(fhp);
382254454a5SChuck Lever 	return status;
383254454a5SChuck Lever }
384254454a5SChuck Lever 
38597662607SJeff Layton /**
38697662607SJeff Layton  * set_change_info - set up the change_info4 for a reply
38797662607SJeff Layton  * @cinfo: pointer to nfsd4_change_info to be populated
38897662607SJeff Layton  * @fhp: pointer to svc_fh to use as source
38997662607SJeff Layton  *
39097662607SJeff Layton  * Many operations in NFSv4 require change_info4 in the reply. This function
39197662607SJeff Layton  * populates that from the info that we (should!) have already collected. In
39297662607SJeff Layton  * the event that we didn't get any pre-attrs, just zero out both.
39397662607SJeff Layton  */
39497662607SJeff Layton static void
set_change_info(struct nfsd4_change_info * cinfo,struct svc_fh * fhp)39597662607SJeff Layton set_change_info(struct nfsd4_change_info *cinfo, struct svc_fh *fhp)
39697662607SJeff Layton {
39797662607SJeff Layton 	cinfo->atomic = (u32)(fhp->fh_pre_saved && fhp->fh_post_saved && !fhp->fh_no_atomic_attr);
39897662607SJeff Layton 	cinfo->before_change = fhp->fh_pre_change;
39997662607SJeff Layton 	cinfo->after_change = fhp->fh_post_change;
40097662607SJeff Layton 
40197662607SJeff Layton 	/*
40297662607SJeff Layton 	 * If fetching the pre-change attributes failed, then we should
40397662607SJeff Layton 	 * have already failed the whole operation. We could have still
40497662607SJeff Layton 	 * failed to fetch post-change attributes however.
40597662607SJeff Layton 	 *
40697662607SJeff Layton 	 * If we didn't get post-op attrs, just zero-out the after
40797662607SJeff Layton 	 * field since we don't know what it should be. If the pre_saved
40897662607SJeff Layton 	 * field isn't set for some reason, throw warning and just copy
40997662607SJeff Layton 	 * whatever is in the after field.
41097662607SJeff Layton 	 */
41197662607SJeff Layton 	if (WARN_ON_ONCE(!fhp->fh_pre_saved))
41297662607SJeff Layton 		cinfo->before_change = 0;
41397662607SJeff Layton 	if (!fhp->fh_post_saved)
414f2b7019dSJeff Layton 		cinfo->after_change = cinfo->before_change + 1;
41597662607SJeff Layton }
41697662607SJeff Layton 
417bbc9c36cSJ. Bruce Fields static __be32
do_open_lookup(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_open * open,struct svc_fh ** resfh)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 
45099f88726SJ. Bruce Fields 		/*
451ead8fb8cSKinglong Mee 		 * Following rfc 3530 14.2.16, and rfc 5661 18.16.4
452ead8fb8cSKinglong Mee 		 * use the returned bitmask to indicate which attributes
453ead8fb8cSKinglong Mee 		 * we used to store the verifier:
454749997e5SJeff Layton 		 */
455254454a5SChuck Lever 		if (nfsd4_create_is_exclusive(open->op_createmode) && status == 0)
456ead8fb8cSKinglong Mee 			open->op_bmval[1] |= (FATTR4_WORD1_TIME_ACCESS |
457749997e5SJeff Layton 						FATTR4_WORD1_TIME_MODIFY);
45819d008b4SNeilBrown 	} else {
4591da177e4SLinus Torvalds 		status = nfsd_lookup(rqstp, current_fh,
4601708e50bSChuck Lever 				     open->op_fname, open->op_fnamelen, *resfh);
461a332018aSJeff Layton 		if (status == nfs_ok)
46219d008b4SNeilBrown 			/* NFSv4 protocol requires change attributes even though
46319d008b4SNeilBrown 			 * no change happened.
46419d008b4SNeilBrown 			 */
465a332018aSJeff Layton 			status = fh_fill_both_attrs(current_fh);
46619d008b4SNeilBrown 	}
467aadab6c6SJ. Bruce Fields 	if (status)
468aadab6c6SJ. Bruce Fields 		goto out;
469c0e6bee4SJ. Bruce Fields 	status = nfsd_check_obj_isreg(*resfh);
470af85852dSJ. Bruce Fields 	if (status)
471af85852dSJ. Bruce Fields 		goto out;
4721da177e4SLinus Torvalds 
473c0e6bee4SJ. Bruce Fields 	nfsd4_set_open_owner_reply_cache(cstate, open, *resfh);
4747007c90fSNeil Brown 	accmode = NFSD_MAY_NOP;
47589f6c336SJ. Bruce Fields 	if (open->op_created ||
47689f6c336SJ. Bruce Fields 			open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
4777007c90fSNeil Brown 		accmode |= NFSD_MAY_OWNER_OVERRIDE;
478c0e6bee4SJ. Bruce Fields 	status = do_open_permission(rqstp, *resfh, open, accmode);
47941fd1e42SJ. Bruce Fields 	set_change_info(&open->op_cinfo, current_fh);
480af85852dSJ. Bruce Fields out:
4811da177e4SLinus Torvalds 	return status;
4821da177e4SLinus Torvalds }
4831da177e4SLinus Torvalds 
484b37ad28bSAl Viro static __be32
do_open_fhandle(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_open * open)485bbc9c36cSJ. Bruce Fields do_open_fhandle(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
4861da177e4SLinus Torvalds {
487bbc9c36cSJ. Bruce Fields 	struct svc_fh *current_fh = &cstate->current_fh;
4889f415eb2SJ. Bruce Fields 	int accmode = 0;
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 	/* We don't know the target directory, and therefore can not
4911da177e4SLinus Torvalds 	* set the change info
4921da177e4SLinus Torvalds 	*/
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
4951da177e4SLinus Torvalds 
496bbc9c36cSJ. Bruce Fields 	nfsd4_set_open_owner_reply_cache(cstate, open, current_fh);
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 	open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
4991da177e4SLinus Torvalds 		(open->op_iattr.ia_size == 0);
5009f415eb2SJ. Bruce Fields 	/*
5019f415eb2SJ. Bruce Fields 	 * In the delegation case, the client is telling us about an
5029f415eb2SJ. Bruce Fields 	 * open that it *already* performed locally, some time ago.  We
5039f415eb2SJ. Bruce Fields 	 * should let it succeed now if possible.
5049f415eb2SJ. Bruce Fields 	 *
5059f415eb2SJ. Bruce Fields 	 * In the case of a CLAIM_FH open, on the other hand, the client
5069f415eb2SJ. Bruce Fields 	 * may be counting on us to enforce permissions (the Linux 4.1
5079f415eb2SJ. Bruce Fields 	 * client uses this for normal opens, for example).
5089f415eb2SJ. Bruce Fields 	 */
5099f415eb2SJ. Bruce Fields 	if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH)
5109f415eb2SJ. Bruce Fields 		accmode = NFSD_MAY_OWNER_OVERRIDE;
5111da177e4SLinus Torvalds 
5124ab3442cSJinpeng Cui 	return do_open_permission(rqstp, current_fh, open, accmode);
5131da177e4SLinus Torvalds }
5141da177e4SLinus Torvalds 
51560adfc50SAndy Adamson static void
copy_clientid(clientid_t * clid,struct nfsd4_session * session)51660adfc50SAndy Adamson copy_clientid(clientid_t *clid, struct nfsd4_session *session)
51760adfc50SAndy Adamson {
51860adfc50SAndy Adamson 	struct nfsd4_sessionid *sid =
51960adfc50SAndy Adamson 			(struct nfsd4_sessionid *)session->se_sessionid.data;
52060adfc50SAndy Adamson 
52160adfc50SAndy Adamson 	clid->cl_boot = sid->clientid.cl_boot;
52260adfc50SAndy Adamson 	clid->cl_id = sid->clientid.cl_id;
52360adfc50SAndy Adamson }
5241da177e4SLinus Torvalds 
5257191155bSJ.Bruce Fields static __be32
nfsd4_open(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)526ca364317SJ.Bruce Fields nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
527eb69853dSChristoph Hellwig 	   union nfsd4_op_u *u)
5281da177e4SLinus Torvalds {
529eb69853dSChristoph Hellwig 	struct nfsd4_open *open = &u->open;
530b37ad28bSAl Viro 	__be32 status;
531c0e6bee4SJ. Bruce Fields 	struct svc_fh *resfh = NULL;
5323320fef1SStanislav Kinsbursky 	struct net *net = SVC_NET(rqstp);
5333320fef1SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
53403f318caSJ. Bruce Fields 	bool reclaim = false;
5356668958fSAndy Adamson 
536fe0750e5SJ. Bruce Fields 	dprintk("NFSD: nfsd4_open filename %.*s op_openowner %p\n",
5371708e50bSChuck Lever 		(int)open->op_fnamelen, open->op_fname,
538fe0750e5SJ. Bruce Fields 		open->op_openowner);
5391da177e4SLinus Torvalds 
540fb70bf12SChuck Lever 	open->op_filp = NULL;
541876c553cSJeff Layton 	open->op_rqstp = rqstp;
542fb70bf12SChuck Lever 
5431da177e4SLinus Torvalds 	/* This check required by spec. */
5441da177e4SLinus Torvalds 	if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
5451da177e4SLinus Torvalds 		return nfserr_inval;
5461da177e4SLinus Torvalds 
547500c2481Szhengbin 	open->op_created = false;
548ab1350b2SMi Jinlong 	/*
549ab1350b2SMi Jinlong 	 * RFC5661 18.51.3
550ab1350b2SMi Jinlong 	 * Before RECLAIM_COMPLETE done, server should deny new lock
551ab1350b2SMi Jinlong 	 */
552ab1350b2SMi Jinlong 	if (nfsd4_has_session(cstate) &&
553ec59659bSJ. Bruce Fields 	    !test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags) &&
554ab1350b2SMi Jinlong 	    open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
555ab1350b2SMi Jinlong 		return nfserr_grace;
556ab1350b2SMi Jinlong 
55760adfc50SAndy Adamson 	if (nfsd4_has_session(cstate))
55860adfc50SAndy Adamson 		copy_clientid(&open->op_clientid, cstate->session);
55960adfc50SAndy Adamson 
5601da177e4SLinus Torvalds 	/* check seqid for replay. set nfs4_owner */
5616cd22668SKinglong Mee 	status = nfsd4_process_open1(cstate, open, nn);
562a90b061cSAl Viro 	if (status == nfserr_replay_me) {
563fe0750e5SJ. Bruce Fields 		struct nfs4_replay *rp = &open->op_openowner->oo_owner.so_replay;
564ca364317SJ.Bruce Fields 		fh_put(&cstate->current_fh);
565a4773c08SJ. Bruce Fields 		fh_copy_shallow(&cstate->current_fh.fh_handle,
566a4773c08SJ. Bruce Fields 				&rp->rp_openfh);
5678837abcaSMiklos Szeredi 		status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
5681da177e4SLinus Torvalds 		if (status)
5691da177e4SLinus Torvalds 			dprintk("nfsd4_open: replay failed"
5701da177e4SLinus Torvalds 				" restoring previous filehandle\n");
5711da177e4SLinus Torvalds 		else
572a90b061cSAl Viro 			status = nfserr_replay_me;
5731da177e4SLinus Torvalds 	}
5741da177e4SLinus Torvalds 	if (status)
5751da177e4SLinus Torvalds 		goto out;
5769d313b17SJ. Bruce Fields 	if (open->op_xdr_error) {
5779d313b17SJ. Bruce Fields 		status = open->op_xdr_error;
5789d313b17SJ. Bruce Fields 		goto out;
5799d313b17SJ. Bruce Fields 	}
580fb553c0fSJ. Bruce Fields 
5813c8e0316SYu Zhiguo 	status = nfsd4_check_open_attributes(rqstp, cstate, open);
5823c8e0316SYu Zhiguo 	if (status)
5833c8e0316SYu Zhiguo 		goto out;
5843c8e0316SYu Zhiguo 
585fb553c0fSJ. Bruce Fields 	/* Openowner is now set, so sequence id will get bumped.  Now we need
586fb553c0fSJ. Bruce Fields 	 * these checks before we do any creates: */
587cbd0d51aSJ. Bruce Fields 	status = nfserr_grace;
588c87fb4a3SJ. Bruce Fields 	if (opens_in_grace(net) && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
589cbd0d51aSJ. Bruce Fields 		goto out;
590cbd0d51aSJ. Bruce Fields 	status = nfserr_no_grace;
591c87fb4a3SJ. Bruce Fields 	if (!opens_in_grace(net) && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
592cbd0d51aSJ. Bruce Fields 		goto out;
593fb553c0fSJ. Bruce Fields 
5941da177e4SLinus Torvalds 	switch (open->op_claim_type) {
5950dd3c192SNeilBrown 	case NFS4_OPEN_CLAIM_DELEGATE_CUR:
5961da177e4SLinus Torvalds 	case NFS4_OPEN_CLAIM_NULL:
597c0e6bee4SJ. Bruce Fields 		status = do_open_lookup(rqstp, cstate, open, &resfh);
5981da177e4SLinus Torvalds 		if (status)
5991da177e4SLinus Torvalds 			goto out;
6001da177e4SLinus Torvalds 		break;
6011da177e4SLinus Torvalds 	case NFS4_OPEN_CLAIM_PREVIOUS:
6021722b046SJ. Bruce Fields 		status = nfs4_check_open_reclaim(cstate->clp);
6030cf99b91SMi Jinlong 		if (status)
6040cf99b91SMi Jinlong 			goto out;
605ba5378b6SJeff Layton 		open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
60603f318caSJ. Bruce Fields 		reclaim = true;
607df561f66SGustavo A. R. Silva 		fallthrough;
6088b289b2cSJ. Bruce Fields 	case NFS4_OPEN_CLAIM_FH:
6098b289b2cSJ. Bruce Fields 	case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
610bbc9c36cSJ. Bruce Fields 		status = do_open_fhandle(rqstp, cstate, open);
6111da177e4SLinus Torvalds 		if (status)
6121da177e4SLinus Torvalds 			goto out;
613c0e6bee4SJ. Bruce Fields 		resfh = &cstate->current_fh;
6141da177e4SLinus Torvalds 		break;
6158b289b2cSJ. Bruce Fields 	case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
6161da177e4SLinus Torvalds 	case NFS4_OPEN_CLAIM_DELEGATE_PREV:
6171da177e4SLinus Torvalds 		status = nfserr_notsupp;
6181da177e4SLinus Torvalds 		goto out;
6191da177e4SLinus Torvalds 	default:
6201da177e4SLinus Torvalds 		status = nfserr_inval;
6211da177e4SLinus Torvalds 		goto out;
6221da177e4SLinus Torvalds 	}
6237e2ce0ccSChuck Lever 
624c0e6bee4SJ. Bruce Fields 	status = nfsd4_process_open2(rqstp, resfh, open);
625ca3f9acbSChuck Lever 	if (status && open->op_created)
626ca3f9acbSChuck Lever 		pr_warn("nfsd4_process_open2 failed to open newly-created file: status=%u\n",
627b3fbfe0eSJeff Layton 			be32_to_cpu(status));
62803f318caSJ. Bruce Fields 	if (reclaim && !status)
62903f318caSJ. Bruce Fields 		nn->somebody_reclaimed = true;
6301da177e4SLinus Torvalds out:
631fb70bf12SChuck Lever 	if (open->op_filp) {
632fb70bf12SChuck Lever 		fput(open->op_filp);
633fb70bf12SChuck Lever 		open->op_filp = NULL;
634fb70bf12SChuck Lever 	}
635c0e6bee4SJ. Bruce Fields 	if (resfh && resfh != &cstate->current_fh) {
636c0e6bee4SJ. Bruce Fields 		fh_dup2(&cstate->current_fh, resfh);
637c0e6bee4SJ. Bruce Fields 		fh_put(resfh);
638c0e6bee4SJ. Bruce Fields 		kfree(resfh);
639c0e6bee4SJ. Bruce Fields 	}
64042297899SJeff Layton 	nfsd4_cleanup_open_state(cstate, open);
6419411b1d4SJ. Bruce Fields 	nfsd4_bump_seqid(cstate, status);
6421da177e4SLinus Torvalds 	return status;
6431da177e4SLinus Torvalds }
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds /*
6469d313b17SJ. Bruce Fields  * OPEN is the only seqid-mutating operation whose decoding can fail
6479d313b17SJ. Bruce Fields  * with a seqid-mutating error (specifically, decoding of user names in
6489d313b17SJ. Bruce Fields  * the attributes).  Therefore we have to do some processing to look up
6499d313b17SJ. Bruce Fields  * the stateowner so that we can bump the seqid.
6509d313b17SJ. Bruce Fields  */
nfsd4_open_omfg(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_op * op)6519d313b17SJ. Bruce Fields static __be32 nfsd4_open_omfg(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_op *op)
6529d313b17SJ. Bruce Fields {
653eb69853dSChristoph Hellwig 	struct nfsd4_open *open = &op->u.open;
6549d313b17SJ. Bruce Fields 
6559d313b17SJ. Bruce Fields 	if (!seqid_mutating_err(ntohl(op->status)))
6569d313b17SJ. Bruce Fields 		return op->status;
6579d313b17SJ. Bruce Fields 	if (nfsd4_has_session(cstate))
6589d313b17SJ. Bruce Fields 		return op->status;
6599d313b17SJ. Bruce Fields 	open->op_xdr_error = op->status;
660eb69853dSChristoph Hellwig 	return nfsd4_open(rqstp, cstate, &op->u);
6619d313b17SJ. Bruce Fields }
6629d313b17SJ. Bruce Fields 
6639d313b17SJ. Bruce Fields /*
6641da177e4SLinus Torvalds  * filehandle-manipulating ops.
6651da177e4SLinus Torvalds  */
6667191155bSJ.Bruce Fields static __be32
nfsd4_getfh(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)667b591480bSJ.Bruce Fields nfsd4_getfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
668eb69853dSChristoph Hellwig 	    union nfsd4_op_u *u)
6691da177e4SLinus Torvalds {
670eb69853dSChristoph Hellwig 	u->getfh = &cstate->current_fh;
6711da177e4SLinus Torvalds 	return nfs_ok;
6721da177e4SLinus Torvalds }
6731da177e4SLinus Torvalds 
6747191155bSJ.Bruce Fields static __be32
nfsd4_putfh(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)675ca364317SJ.Bruce Fields nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
676eb69853dSChristoph Hellwig 	    union nfsd4_op_u *u)
6771da177e4SLinus Torvalds {
678eb69853dSChristoph Hellwig 	struct nfsd4_putfh *putfh = &u->putfh;
679b9e8638eSOlga Kornievskaia 	__be32 ret;
680eb69853dSChristoph Hellwig 
681ca364317SJ.Bruce Fields 	fh_put(&cstate->current_fh);
682ca364317SJ.Bruce Fields 	cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
683d8b26071SNeilBrown 	memcpy(&cstate->current_fh.fh_handle.fh_raw, putfh->pf_fhval,
684ca364317SJ.Bruce Fields 	       putfh->pf_fhlen);
685b9e8638eSOlga Kornievskaia 	ret = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_BYPASS_GSS);
686b9e8638eSOlga Kornievskaia #ifdef CONFIG_NFSD_V4_2_INTER_SSC
687b9e8638eSOlga Kornievskaia 	if (ret == nfserr_stale && putfh->no_verify) {
688b9e8638eSOlga Kornievskaia 		SET_FH_FLAG(&cstate->current_fh, NFSD4_FH_FOREIGN);
689b9e8638eSOlga Kornievskaia 		ret = 0;
690b9e8638eSOlga Kornievskaia 	}
691b9e8638eSOlga Kornievskaia #endif
692b9e8638eSOlga Kornievskaia 	return ret;
6931da177e4SLinus Torvalds }
6941da177e4SLinus Torvalds 
6957191155bSJ.Bruce Fields static __be32
nfsd4_putrootfh(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)696b591480bSJ.Bruce Fields nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
697eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
6981da177e4SLinus Torvalds {
699ca364317SJ.Bruce Fields 	fh_put(&cstate->current_fh);
7004ab3442cSJinpeng Cui 
7014ab3442cSJinpeng Cui 	return exp_pseudoroot(rqstp, &cstate->current_fh);
7021da177e4SLinus Torvalds }
7031da177e4SLinus Torvalds 
7047191155bSJ.Bruce Fields static __be32
nfsd4_restorefh(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)705b591480bSJ.Bruce Fields nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
706eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
7071da177e4SLinus Torvalds {
708ca364317SJ.Bruce Fields 	if (!cstate->save_fh.fh_dentry)
7091da177e4SLinus Torvalds 		return nfserr_restorefh;
7101da177e4SLinus Torvalds 
711ca364317SJ.Bruce Fields 	fh_dup2(&cstate->current_fh, &cstate->save_fh);
71251100d2bSOlga Kornievskaia 	if (HAS_CSTATE_FLAG(cstate, SAVED_STATE_ID_FLAG)) {
71337c593c5STigran Mkrtchyan 		memcpy(&cstate->current_stateid, &cstate->save_stateid, sizeof(stateid_t));
71451100d2bSOlga Kornievskaia 		SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
71537c593c5STigran Mkrtchyan 	}
7161da177e4SLinus Torvalds 	return nfs_ok;
7171da177e4SLinus Torvalds }
7181da177e4SLinus Torvalds 
7197191155bSJ.Bruce Fields static __be32
nfsd4_savefh(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)720b591480bSJ.Bruce Fields nfsd4_savefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
721eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
7221da177e4SLinus Torvalds {
723ca364317SJ.Bruce Fields 	fh_dup2(&cstate->save_fh, &cstate->current_fh);
72451100d2bSOlga Kornievskaia 	if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG)) {
72537c593c5STigran Mkrtchyan 		memcpy(&cstate->save_stateid, &cstate->current_stateid, sizeof(stateid_t));
72651100d2bSOlga Kornievskaia 		SET_CSTATE_FLAG(cstate, SAVED_STATE_ID_FLAG);
72737c593c5STigran Mkrtchyan 	}
7281da177e4SLinus Torvalds 	return nfs_ok;
7291da177e4SLinus Torvalds }
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds /*
7321da177e4SLinus Torvalds  * misc nfsv4 ops
7331da177e4SLinus Torvalds  */
7347191155bSJ.Bruce Fields static __be32
nfsd4_access(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)735ca364317SJ.Bruce Fields nfsd4_access(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
736eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
7371da177e4SLinus Torvalds {
738eb69853dSChristoph Hellwig 	struct nfsd4_access *access = &u->access;
739c11d7fd1SFrank van der Linden 	u32 access_full;
740eb69853dSChristoph Hellwig 
741c11d7fd1SFrank van der Linden 	access_full = NFS3_ACCESS_FULL;
742c11d7fd1SFrank van der Linden 	if (cstate->minorversion >= 2)
743c11d7fd1SFrank van der Linden 		access_full |= NFS4_ACCESS_XALIST | NFS4_ACCESS_XAREAD |
744c11d7fd1SFrank van der Linden 			       NFS4_ACCESS_XAWRITE;
745c11d7fd1SFrank van der Linden 
746c11d7fd1SFrank van der Linden 	if (access->ac_req_access & ~access_full)
7471da177e4SLinus Torvalds 		return nfserr_inval;
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 	access->ac_resp_access = access->ac_req_access;
750ca364317SJ.Bruce Fields 	return nfsd_access(rqstp, &cstate->current_fh, &access->ac_resp_access,
751ca364317SJ.Bruce Fields 			   &access->ac_supported);
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds 
7547191155bSJ.Bruce Fields static __be32
nfsd4_commit(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)755ca364317SJ.Bruce Fields nfsd4_commit(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
756eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
7571da177e4SLinus Torvalds {
758eb69853dSChristoph Hellwig 	struct nfsd4_commit *commit = &u->commit;
759c2528490SChuck Lever 	struct nfsd_file *nf;
760c2528490SChuck Lever 	__be32 status;
761eb69853dSChristoph Hellwig 
762c2528490SChuck Lever 	status = nfsd_file_acquire(rqstp, &cstate->current_fh, NFSD_MAY_WRITE |
763c2528490SChuck Lever 				   NFSD_MAY_NOT_BREAK_LEASE, &nf);
764c2528490SChuck Lever 	if (status != nfs_ok)
765c2528490SChuck Lever 		return status;
766c2528490SChuck Lever 
767c2528490SChuck Lever 	status = nfsd_commit(rqstp, &cstate->current_fh, nf, commit->co_offset,
768524ff1afSTrond Myklebust 			     commit->co_count,
769524ff1afSTrond Myklebust 			     (__be32 *)commit->co_verf.data);
770c2528490SChuck Lever 	nfsd_file_put(nf);
771c2528490SChuck Lever 	return status;
7721da177e4SLinus Torvalds }
7731da177e4SLinus Torvalds 
774b37ad28bSAl Viro static __be32
nfsd4_create(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)775ca364317SJ.Bruce Fields nfsd4_create(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
776eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
7771da177e4SLinus Torvalds {
778eb69853dSChristoph Hellwig 	struct nfsd4_create *create = &u->create;
7797fe2a71dSNeilBrown 	struct nfsd_attrs attrs = {
7807fe2a71dSNeilBrown 		.na_iattr	= &create->cr_iattr,
781d6a97d3fSNeilBrown 		.na_seclabel	= &create->cr_label,
7827fe2a71dSNeilBrown 	};
7831da177e4SLinus Torvalds 	struct svc_fh resfh;
784b37ad28bSAl Viro 	__be32 status;
7851da177e4SLinus Torvalds 	dev_t rdev;
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	fh_init(&resfh, NFS4_FHSIZE);
7881da177e4SLinus Torvalds 
789fa08139dSJ. Bruce Fields 	status = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_NOP);
7901da177e4SLinus Torvalds 	if (status)
7911da177e4SLinus Torvalds 		return status;
7921da177e4SLinus Torvalds 
7933c8e0316SYu Zhiguo 	status = check_attr_support(rqstp, cstate, create->cr_bmval,
7943c8e0316SYu Zhiguo 				    nfsd_attrmask);
7953c8e0316SYu Zhiguo 	if (status)
7963c8e0316SYu Zhiguo 		return status;
7973c8e0316SYu Zhiguo 
798c0cbe707SNeilBrown 	status = nfsd4_acl_to_attr(create->cr_type, create->cr_acl, &attrs);
799880a3a53SJ. Bruce Fields 	current->fs->umask = create->cr_umask;
8001da177e4SLinus Torvalds 	switch (create->cr_type) {
8011da177e4SLinus Torvalds 	case NF4LNK:
802ca364317SJ.Bruce Fields 		status = nfsd_symlink(rqstp, &cstate->current_fh,
803ca364317SJ.Bruce Fields 				      create->cr_name, create->cr_namelen,
80493adc1e3SNeilBrown 				      create->cr_data, &attrs, &resfh);
8051da177e4SLinus Torvalds 		break;
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds 	case NF4BLK:
808880a3a53SJ. Bruce Fields 		status = nfserr_inval;
8091da177e4SLinus Torvalds 		rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
8101da177e4SLinus Torvalds 		if (MAJOR(rdev) != create->cr_specdata1 ||
8111da177e4SLinus Torvalds 		    MINOR(rdev) != create->cr_specdata2)
812880a3a53SJ. Bruce Fields 			goto out_umask;
813ca364317SJ.Bruce Fields 		status = nfsd_create(rqstp, &cstate->current_fh,
814ca364317SJ.Bruce Fields 				     create->cr_name, create->cr_namelen,
8157fe2a71dSNeilBrown 				     &attrs, S_IFBLK, rdev, &resfh);
8161da177e4SLinus Torvalds 		break;
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 	case NF4CHR:
819880a3a53SJ. Bruce Fields 		status = nfserr_inval;
8201da177e4SLinus Torvalds 		rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
8211da177e4SLinus Torvalds 		if (MAJOR(rdev) != create->cr_specdata1 ||
8221da177e4SLinus Torvalds 		    MINOR(rdev) != create->cr_specdata2)
823880a3a53SJ. Bruce Fields 			goto out_umask;
824ca364317SJ.Bruce Fields 		status = nfsd_create(rqstp, &cstate->current_fh,
825ca364317SJ.Bruce Fields 				     create->cr_name, create->cr_namelen,
8267fe2a71dSNeilBrown 				     &attrs, S_IFCHR, rdev, &resfh);
8271da177e4SLinus Torvalds 		break;
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds 	case NF4SOCK:
830ca364317SJ.Bruce Fields 		status = nfsd_create(rqstp, &cstate->current_fh,
831ca364317SJ.Bruce Fields 				     create->cr_name, create->cr_namelen,
8327fe2a71dSNeilBrown 				     &attrs, S_IFSOCK, 0, &resfh);
8331da177e4SLinus Torvalds 		break;
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	case NF4FIFO:
836ca364317SJ.Bruce Fields 		status = nfsd_create(rqstp, &cstate->current_fh,
837ca364317SJ.Bruce Fields 				     create->cr_name, create->cr_namelen,
8387fe2a71dSNeilBrown 				     &attrs, S_IFIFO, 0, &resfh);
8391da177e4SLinus Torvalds 		break;
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 	case NF4DIR:
8421da177e4SLinus Torvalds 		create->cr_iattr.ia_valid &= ~ATTR_SIZE;
843ca364317SJ.Bruce Fields 		status = nfsd_create(rqstp, &cstate->current_fh,
844ca364317SJ.Bruce Fields 				     create->cr_name, create->cr_namelen,
8457fe2a71dSNeilBrown 				     &attrs, S_IFDIR, 0, &resfh);
8461da177e4SLinus Torvalds 		break;
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 	default:
8491da177e4SLinus Torvalds 		status = nfserr_badtype;
8501da177e4SLinus Torvalds 	}
8511da177e4SLinus Torvalds 
8529208faf2SYu Zhiguo 	if (status)
8539208faf2SYu Zhiguo 		goto out;
8549208faf2SYu Zhiguo 
855d6a97d3fSNeilBrown 	if (attrs.na_labelerr)
856d6a97d3fSNeilBrown 		create->cr_bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
857c0cbe707SNeilBrown 	if (attrs.na_aclerr)
858c0cbe707SNeilBrown 		create->cr_bmval[0] &= ~FATTR4_WORD0_ACL;
859ca364317SJ.Bruce Fields 	set_change_info(&create->cr_cinfo, &cstate->current_fh);
860ca364317SJ.Bruce Fields 	fh_dup2(&cstate->current_fh, &resfh);
8619208faf2SYu Zhiguo out:
8621da177e4SLinus Torvalds 	fh_put(&resfh);
863880a3a53SJ. Bruce Fields out_umask:
864880a3a53SJ. Bruce Fields 	current->fs->umask = 0;
865c0cbe707SNeilBrown 	nfsd_attrs_free(&attrs);
8661da177e4SLinus Torvalds 	return status;
8671da177e4SLinus Torvalds }
8681da177e4SLinus Torvalds 
8697191155bSJ.Bruce Fields static __be32
nfsd4_getattr(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)870ca364317SJ.Bruce Fields nfsd4_getattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
871eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
8721da177e4SLinus Torvalds {
873eb69853dSChristoph Hellwig 	struct nfsd4_getattr *getattr = &u->getattr;
874b37ad28bSAl Viro 	__be32 status;
8751da177e4SLinus Torvalds 
8768837abcaSMiklos Szeredi 	status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
8771da177e4SLinus Torvalds 	if (status)
8781da177e4SLinus Torvalds 		return status;
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 	if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
8811da177e4SLinus Torvalds 		return nfserr_inval;
8821da177e4SLinus Torvalds 
883916d2d84SJ. Bruce Fields 	getattr->ga_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
884916d2d84SJ. Bruce Fields 	getattr->ga_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
885916d2d84SJ. Bruce Fields 	getattr->ga_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
8861da177e4SLinus Torvalds 
887ca364317SJ.Bruce Fields 	getattr->ga_fhp = &cstate->current_fh;
8881da177e4SLinus Torvalds 	return nfs_ok;
8891da177e4SLinus Torvalds }
8901da177e4SLinus Torvalds 
8917191155bSJ.Bruce Fields static __be32
nfsd4_link(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)892ca364317SJ.Bruce Fields nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
893eb69853dSChristoph Hellwig 	   union nfsd4_op_u *u)
8941da177e4SLinus Torvalds {
895eb69853dSChristoph Hellwig 	struct nfsd4_link *link = &u->link;
89695424460SJ. Bruce Fields 	__be32 status;
8971da177e4SLinus Torvalds 
898ca364317SJ.Bruce Fields 	status = nfsd_link(rqstp, &cstate->current_fh,
899ca364317SJ.Bruce Fields 			   link->li_name, link->li_namelen, &cstate->save_fh);
9001da177e4SLinus Torvalds 	if (!status)
901ca364317SJ.Bruce Fields 		set_change_info(&link->li_cinfo, &cstate->current_fh);
9021da177e4SLinus Torvalds 	return status;
9031da177e4SLinus Torvalds }
9041da177e4SLinus Torvalds 
nfsd4_do_lookupp(struct svc_rqst * rqstp,struct svc_fh * fh)9050ff7ab46SJ. Bruce Fields static __be32 nfsd4_do_lookupp(struct svc_rqst *rqstp, struct svc_fh *fh)
9061da177e4SLinus Torvalds {
9071da177e4SLinus Torvalds 	struct svc_fh tmp_fh;
908b37ad28bSAl Viro 	__be32 ret;
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 	fh_init(&tmp_fh, NFS4_FHSIZE);
911df547efbSJ. Bruce Fields 	ret = exp_pseudoroot(rqstp, &tmp_fh);
912df547efbSJ. Bruce Fields 	if (ret)
9131da177e4SLinus Torvalds 		return ret;
9140ff7ab46SJ. Bruce Fields 	if (tmp_fh.fh_dentry == fh->fh_dentry) {
9151da177e4SLinus Torvalds 		fh_put(&tmp_fh);
9161da177e4SLinus Torvalds 		return nfserr_noent;
9171da177e4SLinus Torvalds 	}
9181da177e4SLinus Torvalds 	fh_put(&tmp_fh);
9190ff7ab46SJ. Bruce Fields 	return nfsd_lookup(rqstp, fh, "..", 2, fh);
9200ff7ab46SJ. Bruce Fields }
9210ff7ab46SJ. Bruce Fields 
9220ff7ab46SJ. Bruce Fields static __be32
nfsd4_lookupp(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9230ff7ab46SJ. Bruce Fields nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
924eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
9250ff7ab46SJ. Bruce Fields {
9260ff7ab46SJ. Bruce Fields 	return nfsd4_do_lookupp(rqstp, &cstate->current_fh);
9271da177e4SLinus Torvalds }
9281da177e4SLinus Torvalds 
9297191155bSJ.Bruce Fields static __be32
nfsd4_lookup(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)930ca364317SJ.Bruce Fields nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
931eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
9321da177e4SLinus Torvalds {
933ca364317SJ.Bruce Fields 	return nfsd_lookup(rqstp, &cstate->current_fh,
934eb69853dSChristoph Hellwig 			   u->lookup.lo_name, u->lookup.lo_len,
935ca364317SJ.Bruce Fields 			   &cstate->current_fh);
9361da177e4SLinus Torvalds }
9371da177e4SLinus Torvalds 
9387191155bSJ.Bruce Fields static __be32
nfsd4_read(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)939ca364317SJ.Bruce Fields nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
940eb69853dSChristoph Hellwig 	   union nfsd4_op_u *u)
9411da177e4SLinus Torvalds {
942eb69853dSChristoph Hellwig 	struct nfsd4_read *read = &u->read;
943b37ad28bSAl Viro 	__be32 status;
9441da177e4SLinus Torvalds 
9455c4583b2SJeff Layton 	read->rd_nf = NULL;
9461da177e4SLinus Torvalds 
94787c5942eSChuck Lever 	trace_nfsd_read_start(rqstp, &cstate->current_fh,
94887c5942eSChuck Lever 			      read->rd_offset, read->rd_length);
94987c5942eSChuck Lever 
9500cb4d23aSChuck Lever 	read->rd_length = min_t(u32, read->rd_length, svc_max_payload(rqstp));
9510cb4d23aSChuck Lever 	if (read->rd_offset > (u64)OFFSET_MAX)
9520cb4d23aSChuck Lever 		read->rd_offset = (u64)OFFSET_MAX;
9530cb4d23aSChuck Lever 	if (read->rd_offset + read->rd_length > (u64)OFFSET_MAX)
9540cb4d23aSChuck Lever 		read->rd_length = (u64)OFFSET_MAX - read->rd_offset;
9550cb4d23aSChuck Lever 
9569b3234b9SJ. Bruce Fields 	/*
9579b3234b9SJ. Bruce Fields 	 * If we do a zero copy read, then a client will see read data
9589b3234b9SJ. Bruce Fields 	 * that reflects the state of the file *after* performing the
9599b3234b9SJ. Bruce Fields 	 * following compound.
9609b3234b9SJ. Bruce Fields 	 *
9619b3234b9SJ. Bruce Fields 	 * To ensure proper ordering, we therefore turn off zero copy if
9629b3234b9SJ. Bruce Fields 	 * the client wants us to do more in this compound:
9639b3234b9SJ. Bruce Fields 	 */
9649b3234b9SJ. Bruce Fields 	if (!nfsd4_last_compound_op(rqstp))
9657827c81fSChuck Lever 		clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
9669b3234b9SJ. Bruce Fields 
9671da177e4SLinus Torvalds 	/* check stateid */
968aa0d6aedSAnna Schumaker 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
969aa0d6aedSAnna Schumaker 					&read->rd_stateid, RD_STATE,
970624322f1SOlga Kornievskaia 					&read->rd_nf, NULL);
971eeff73f7SChuck Lever 
9721da177e4SLinus Torvalds 	read->rd_rqstp = rqstp;
973ca364317SJ.Bruce Fields 	read->rd_fhp = &cstate->current_fh;
9741da177e4SLinus Torvalds 	return status;
9751da177e4SLinus Torvalds }
9761da177e4SLinus Torvalds 
97734b1744cSJ. Bruce Fields 
97834b1744cSJ. Bruce Fields static void
nfsd4_read_release(union nfsd4_op_u * u)97934b1744cSJ. Bruce Fields nfsd4_read_release(union nfsd4_op_u *u)
98034b1744cSJ. Bruce Fields {
9815c4583b2SJeff Layton 	if (u->read.rd_nf)
9825c4583b2SJeff Layton 		nfsd_file_put(u->read.rd_nf);
98387c5942eSChuck Lever 	trace_nfsd_read_done(u->read.rd_rqstp, u->read.rd_fhp,
98487c5942eSChuck Lever 			     u->read.rd_offset, u->read.rd_length);
98534b1744cSJ. Bruce Fields }
98634b1744cSJ. Bruce Fields 
9877191155bSJ.Bruce Fields static __be32
nfsd4_readdir(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)988ca364317SJ.Bruce Fields nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
989eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
9901da177e4SLinus Torvalds {
991eb69853dSChristoph Hellwig 	struct nfsd4_readdir *readdir = &u->readdir;
9921da177e4SLinus Torvalds 	u64 cookie = readdir->rd_cookie;
9931da177e4SLinus Torvalds 	static const nfs4_verifier zeroverf;
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	/* no need to check permission - this will be done in nfsd_readdir() */
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds 	if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
9981da177e4SLinus Torvalds 		return nfserr_inval;
9991da177e4SLinus Torvalds 
1000916d2d84SJ. Bruce Fields 	readdir->rd_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
1001916d2d84SJ. Bruce Fields 	readdir->rd_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
1002916d2d84SJ. Bruce Fields 	readdir->rd_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
10031da177e4SLinus Torvalds 
1004832023bfSBernd Schubert 	if ((cookie == 1) || (cookie == 2) ||
10051da177e4SLinus Torvalds 	    (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
10061da177e4SLinus Torvalds 		return nfserr_bad_cookie;
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 	readdir->rd_rqstp = rqstp;
1009ca364317SJ.Bruce Fields 	readdir->rd_fhp = &cstate->current_fh;
10101da177e4SLinus Torvalds 	return nfs_ok;
10111da177e4SLinus Torvalds }
10121da177e4SLinus Torvalds 
10137191155bSJ.Bruce Fields static __be32
nfsd4_readlink(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)1014ca364317SJ.Bruce Fields nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1015eb69853dSChristoph Hellwig 	       union nfsd4_op_u *u)
10161da177e4SLinus Torvalds {
1017eb69853dSChristoph Hellwig 	u->readlink.rl_rqstp = rqstp;
1018eb69853dSChristoph Hellwig 	u->readlink.rl_fhp = &cstate->current_fh;
10191da177e4SLinus Torvalds 	return nfs_ok;
10201da177e4SLinus Torvalds }
10211da177e4SLinus Torvalds 
10227191155bSJ.Bruce Fields static __be32
nfsd4_remove(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)1023ca364317SJ.Bruce Fields nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1024eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
10251da177e4SLinus Torvalds {
1026eb69853dSChristoph Hellwig 	struct nfsd4_remove *remove = &u->remove;
1027b37ad28bSAl Viro 	__be32 status;
10281da177e4SLinus Torvalds 
1029c87fb4a3SJ. Bruce Fields 	if (opens_in_grace(SVC_NET(rqstp)))
1030c815afc7SNeilBrown 		return nfserr_grace;
1031ca364317SJ.Bruce Fields 	status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
1032ca364317SJ.Bruce Fields 			     remove->rm_name, remove->rm_namelen);
1033b677c0c6SNeilBrown 	if (!status)
1034ca364317SJ.Bruce Fields 		set_change_info(&remove->rm_cinfo, &cstate->current_fh);
10351da177e4SLinus Torvalds 	return status;
10361da177e4SLinus Torvalds }
10371da177e4SLinus Torvalds 
10387191155bSJ.Bruce Fields static __be32
nfsd4_rename(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)1039ca364317SJ.Bruce Fields nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1040eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
10411da177e4SLinus Torvalds {
1042eb69853dSChristoph Hellwig 	struct nfsd4_rename *rename = &u->rename;
104395424460SJ. Bruce Fields 	__be32 status;
10441da177e4SLinus Torvalds 
1045f8f71d00SJ. Bruce Fields 	if (opens_in_grace(SVC_NET(rqstp)))
1046c815afc7SNeilBrown 		return nfserr_grace;
1047ca364317SJ.Bruce Fields 	status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname,
1048ca364317SJ.Bruce Fields 			     rename->rn_snamelen, &cstate->current_fh,
10491da177e4SLinus Torvalds 			     rename->rn_tname, rename->rn_tnamelen);
10502a6cf944SJ. Bruce Fields 	if (status)
10512a6cf944SJ. Bruce Fields 		return status;
1052fdd2630aSJeff Layton 	set_change_info(&rename->rn_sinfo, &cstate->save_fh);
1053fdd2630aSJeff Layton 	set_change_info(&rename->rn_tinfo, &cstate->current_fh);
10542a6cf944SJ. Bruce Fields 	return nfs_ok;
10551da177e4SLinus Torvalds }
10561da177e4SLinus Torvalds 
10577191155bSJ.Bruce Fields static __be32
nfsd4_secinfo(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)1058dcb488a3SAndy Adamson nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1059eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
1060dcb488a3SAndy Adamson {
1061eb69853dSChristoph Hellwig 	struct nfsd4_secinfo *secinfo = &u->secinfo;
1062dcb488a3SAndy Adamson 	struct svc_export *exp;
1063dcb488a3SAndy Adamson 	struct dentry *dentry;
1064dcb488a3SAndy Adamson 	__be32 err;
1065dcb488a3SAndy Adamson 
106629a78a3eSJ. Bruce Fields 	err = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_EXEC);
106729a78a3eSJ. Bruce Fields 	if (err)
106829a78a3eSJ. Bruce Fields 		return err;
1069dcb488a3SAndy Adamson 	err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
1070dcb488a3SAndy Adamson 				    secinfo->si_name, secinfo->si_namelen,
1071dcb488a3SAndy Adamson 				    &exp, &dentry);
1072dcb488a3SAndy Adamson 	if (err)
1073dcb488a3SAndy Adamson 		return err;
10742b0143b5SDavid Howells 	if (d_really_is_negative(dentry)) {
1075dcb488a3SAndy Adamson 		exp_put(exp);
1076dcb488a3SAndy Adamson 		err = nfserr_noent;
1077dcb488a3SAndy Adamson 	} else
1078dcb488a3SAndy Adamson 		secinfo->si_exp = exp;
1079dcb488a3SAndy Adamson 	dput(dentry);
108056560b9aSJ. Bruce Fields 	if (cstate->minorversion)
108156560b9aSJ. Bruce Fields 		/* See rfc 5661 section 2.6.3.1.1.8 */
108256560b9aSJ. Bruce Fields 		fh_put(&cstate->current_fh);
1083dcb488a3SAndy Adamson 	return err;
1084dcb488a3SAndy Adamson }
1085dcb488a3SAndy Adamson 
1086dcb488a3SAndy Adamson static __be32
nfsd4_secinfo_no_name(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)108704f4ad16SJ. Bruce Fields nfsd4_secinfo_no_name(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1088eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
108904f4ad16SJ. Bruce Fields {
109004f4ad16SJ. Bruce Fields 	__be32 err;
109104f4ad16SJ. Bruce Fields 
1092eb69853dSChristoph Hellwig 	switch (u->secinfo_no_name.sin_style) {
109304f4ad16SJ. Bruce Fields 	case NFS4_SECINFO_STYLE4_CURRENT_FH:
109404f4ad16SJ. Bruce Fields 		break;
109504f4ad16SJ. Bruce Fields 	case NFS4_SECINFO_STYLE4_PARENT:
109604f4ad16SJ. Bruce Fields 		err = nfsd4_do_lookupp(rqstp, &cstate->current_fh);
109704f4ad16SJ. Bruce Fields 		if (err)
109804f4ad16SJ. Bruce Fields 			return err;
109904f4ad16SJ. Bruce Fields 		break;
110004f4ad16SJ. Bruce Fields 	default:
110104f4ad16SJ. Bruce Fields 		return nfserr_inval;
110204f4ad16SJ. Bruce Fields 	}
1103bf18f163SKinglong Mee 
1104eb69853dSChristoph Hellwig 	u->secinfo_no_name.sin_exp = exp_get(cstate->current_fh.fh_export);
110504f4ad16SJ. Bruce Fields 	fh_put(&cstate->current_fh);
110604f4ad16SJ. Bruce Fields 	return nfs_ok;
110704f4ad16SJ. Bruce Fields }
110804f4ad16SJ. Bruce Fields 
110934b1744cSJ. Bruce Fields static void
nfsd4_secinfo_release(union nfsd4_op_u * u)111034b1744cSJ. Bruce Fields nfsd4_secinfo_release(union nfsd4_op_u *u)
111134b1744cSJ. Bruce Fields {
111234b1744cSJ. Bruce Fields 	if (u->secinfo.si_exp)
111334b1744cSJ. Bruce Fields 		exp_put(u->secinfo.si_exp);
111434b1744cSJ. Bruce Fields }
111534b1744cSJ. Bruce Fields 
1116ec572b9eSEryu Guan static void
nfsd4_secinfo_no_name_release(union nfsd4_op_u * u)1117ec572b9eSEryu Guan nfsd4_secinfo_no_name_release(union nfsd4_op_u *u)
1118ec572b9eSEryu Guan {
1119ec572b9eSEryu Guan 	if (u->secinfo_no_name.sin_exp)
1120ec572b9eSEryu Guan 		exp_put(u->secinfo_no_name.sin_exp);
1121ec572b9eSEryu Guan }
1122ec572b9eSEryu Guan 
112304f4ad16SJ. Bruce Fields static __be32
nfsd4_setattr(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)1124ca364317SJ.Bruce Fields nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1125eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
11261da177e4SLinus Torvalds {
1127eb69853dSChristoph Hellwig 	struct nfsd4_setattr *setattr = &u->setattr;
11287fe2a71dSNeilBrown 	struct nfsd_attrs attrs = {
11297fe2a71dSNeilBrown 		.na_iattr	= &setattr->sa_iattr,
1130d6a97d3fSNeilBrown 		.na_seclabel	= &setattr->sa_label,
11317fe2a71dSNeilBrown 	};
1132c0cbe707SNeilBrown 	struct inode *inode;
1133b37ad28bSAl Viro 	__be32 status = nfs_ok;
113496f6f985SAl Viro 	int err;
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds 	if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
1137af90f707SChristoph Hellwig 		status = nfs4_preprocess_stateid_op(rqstp, cstate,
1138aa0d6aedSAnna Schumaker 				&cstate->current_fh, &setattr->sa_stateid,
1139624322f1SOlga Kornievskaia 				WR_STATE, NULL, NULL);
1140eeff73f7SChuck Lever 		if (status)
1141375c5547SJ. Bruce Fields 			return status;
1142375c5547SJ. Bruce Fields 	}
114396f6f985SAl Viro 	err = fh_want_write(&cstate->current_fh);
114496f6f985SAl Viro 	if (err)
114596f6f985SAl Viro 		return nfserrno(err);
11461da177e4SLinus Torvalds 	status = nfs_ok;
11473c8e0316SYu Zhiguo 
11483c8e0316SYu Zhiguo 	status = check_attr_support(rqstp, cstate, setattr->sa_bmval,
11493c8e0316SYu Zhiguo 				    nfsd_attrmask);
11503c8e0316SYu Zhiguo 	if (status)
11513c8e0316SYu Zhiguo 		goto out;
11523c8e0316SYu Zhiguo 
1153c0cbe707SNeilBrown 	inode = cstate->current_fh.fh_dentry->d_inode;
1154c0cbe707SNeilBrown 	status = nfsd4_acl_to_attr(S_ISDIR(inode->i_mode) ? NF4DIR : NF4REG,
1155c0cbe707SNeilBrown 				   setattr->sa_acl, &attrs);
1156c0cbe707SNeilBrown 
11571da177e4SLinus Torvalds 	if (status)
115818f335afSDave Hansen 		goto out;
11597fe2a71dSNeilBrown 	status = nfsd_setattr(rqstp, &cstate->current_fh, &attrs,
11602a1aa489SArnd Bergmann 				0, (time64_t)0);
1161d6a97d3fSNeilBrown 	if (!status)
1162d6a97d3fSNeilBrown 		status = nfserrno(attrs.na_labelerr);
116301d53a88SJeff Layton 	if (!status)
116401d53a88SJeff Layton 		status = nfserrno(attrs.na_aclerr);
116518f335afSDave Hansen out:
1166c0cbe707SNeilBrown 	nfsd_attrs_free(&attrs);
1167bad0dcffSAl Viro 	fh_drop_write(&cstate->current_fh);
11681da177e4SLinus Torvalds 	return status;
11691da177e4SLinus Torvalds }
11701da177e4SLinus Torvalds 
11717191155bSJ.Bruce Fields static __be32
nfsd4_write(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)1172ca364317SJ.Bruce Fields nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1173eb69853dSChristoph Hellwig 	    union nfsd4_op_u *u)
11741da177e4SLinus Torvalds {
1175eb69853dSChristoph Hellwig 	struct nfsd4_write *write = &u->write;
11761da177e4SLinus Torvalds 	stateid_t *stateid = &write->wr_stateid;
11775c4583b2SJeff Layton 	struct nfsd_file *nf = NULL;
1178b37ad28bSAl Viro 	__be32 status = nfs_ok;
117931dec253SDavid Shaw 	unsigned long cnt;
1180ffe1137bSJ. Bruce Fields 	int nvecs;
11811da177e4SLinus Torvalds 
11826260d9a5SChuck Lever 	if (write->wr_offset > (u64)OFFSET_MAX ||
11836260d9a5SChuck Lever 	    write->wr_offset + write->wr_buflen > (u64)OFFSET_MAX)
11846260d9a5SChuck Lever 		return nfserr_fbig;
11851da177e4SLinus Torvalds 
1186d890be15SChuck Lever 	cnt = write->wr_buflen;
1187d890be15SChuck Lever 	trace_nfsd_write_start(rqstp, &cstate->current_fh,
1188d890be15SChuck Lever 			       write->wr_offset, cnt);
1189aa0d6aedSAnna Schumaker 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1190624322f1SOlga Kornievskaia 						stateid, WR_STATE, &nf, NULL);
1191eeff73f7SChuck Lever 	if (status)
1192375c5547SJ. Bruce Fields 		return status;
1193375c5547SJ. Bruce Fields 
11941da177e4SLinus Torvalds 	write->wr_how_written = write->wr_stable_how;
11951da177e4SLinus Torvalds 
1196dae9a6caSChuck Lever 	nvecs = svc_fill_write_vector(rqstp, &write->wr_payload);
1197ffe1137bSJ. Bruce Fields 	WARN_ON_ONCE(nvecs > ARRAY_SIZE(rqstp->rq_vec));
1198ffe1137bSJ. Bruce Fields 
119916f8f894STrond Myklebust 	status = nfsd_vfs_write(rqstp, &cstate->current_fh, nf,
1200af90f707SChristoph Hellwig 				write->wr_offset, rqstp->rq_vec, nvecs, &cnt,
120119e0663fSTrond Myklebust 				write->wr_how_written,
120219e0663fSTrond Myklebust 				(__be32 *)write->wr_verifier.data);
12035c4583b2SJeff Layton 	nfsd_file_put(nf);
12041da177e4SLinus Torvalds 
120531dec253SDavid Shaw 	write->wr_bytes_written = cnt;
1206d890be15SChuck Lever 	trace_nfsd_write_done(rqstp, &cstate->current_fh,
1207d890be15SChuck Lever 			      write->wr_offset, cnt);
12081da177e4SLinus Torvalds 	return status;
12091da177e4SLinus Torvalds }
12101da177e4SLinus Torvalds 
121124bab491SAnna Schumaker static __be32
nfsd4_verify_copy(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,stateid_t * src_stateid,struct nfsd_file ** src,stateid_t * dst_stateid,struct nfsd_file ** dst)121229ae7f9dSAnna Schumaker nfsd4_verify_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
12135c4583b2SJeff Layton 		  stateid_t *src_stateid, struct nfsd_file **src,
12145c4583b2SJeff Layton 		  stateid_t *dst_stateid, struct nfsd_file **dst)
1215ffa0160aSChristoph Hellwig {
1216ffa0160aSChristoph Hellwig 	__be32 status;
1217ffa0160aSChristoph Hellwig 
121801310bb7SScott Mayhew 	if (!cstate->save_fh.fh_dentry)
121901310bb7SScott Mayhew 		return nfserr_nofilehandle;
122001310bb7SScott Mayhew 
1221ffa0160aSChristoph Hellwig 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->save_fh,
1222624322f1SOlga Kornievskaia 					    src_stateid, RD_STATE, src, NULL);
1223eeff73f7SChuck Lever 	if (status)
1224ffa0160aSChristoph Hellwig 		goto out;
1225ffa0160aSChristoph Hellwig 
1226ffa0160aSChristoph Hellwig 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1227624322f1SOlga Kornievskaia 					    dst_stateid, WR_STATE, dst, NULL);
1228eeff73f7SChuck Lever 	if (status)
1229ffa0160aSChristoph Hellwig 		goto out_put_src;
1230ffa0160aSChristoph Hellwig 
1231ffa0160aSChristoph Hellwig 	/* fix up for NFS-specific error code */
12325c4583b2SJeff Layton 	if (!S_ISREG(file_inode((*src)->nf_file)->i_mode) ||
12335c4583b2SJeff Layton 	    !S_ISREG(file_inode((*dst)->nf_file)->i_mode)) {
1234ffa0160aSChristoph Hellwig 		status = nfserr_wrong_type;
1235ffa0160aSChristoph Hellwig 		goto out_put_dst;
1236ffa0160aSChristoph Hellwig 	}
1237ffa0160aSChristoph Hellwig 
123829ae7f9dSAnna Schumaker out:
123929ae7f9dSAnna Schumaker 	return status;
124029ae7f9dSAnna Schumaker out_put_dst:
12415c4583b2SJeff Layton 	nfsd_file_put(*dst);
12421f0001d4SJeff Layton 	*dst = NULL;
124329ae7f9dSAnna Schumaker out_put_src:
12445c4583b2SJeff Layton 	nfsd_file_put(*src);
12451f0001d4SJeff Layton 	*src = NULL;
124629ae7f9dSAnna Schumaker 	goto out;
124729ae7f9dSAnna Schumaker }
124829ae7f9dSAnna Schumaker 
124929ae7f9dSAnna Schumaker static __be32
nfsd4_clone(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)125029ae7f9dSAnna Schumaker nfsd4_clone(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1251eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
125229ae7f9dSAnna Schumaker {
1253eb69853dSChristoph Hellwig 	struct nfsd4_clone *clone = &u->clone;
12545c4583b2SJeff Layton 	struct nfsd_file *src, *dst;
125529ae7f9dSAnna Schumaker 	__be32 status;
125629ae7f9dSAnna Schumaker 
125729ae7f9dSAnna Schumaker 	status = nfsd4_verify_copy(rqstp, cstate, &clone->cl_src_stateid, &src,
125829ae7f9dSAnna Schumaker 				   &clone->cl_dst_stateid, &dst);
125929ae7f9dSAnna Schumaker 	if (status)
126029ae7f9dSAnna Schumaker 		goto out;
126129ae7f9dSAnna Schumaker 
1262a2f4c3faSTrond Myklebust 	status = nfsd4_clone_file_range(rqstp, src, clone->cl_src_pos,
1263b66ae6ddSTrond Myklebust 			dst, clone->cl_dst_pos, clone->cl_count,
1264a25e3726STrond Myklebust 			EX_ISSYNC(cstate->current_fh.fh_export));
1265ffa0160aSChristoph Hellwig 
12665c4583b2SJeff Layton 	nfsd_file_put(dst);
12675c4583b2SJeff Layton 	nfsd_file_put(src);
1268ffa0160aSChristoph Hellwig out:
1269ffa0160aSChristoph Hellwig 	return status;
1270ffa0160aSChristoph Hellwig }
1271ffa0160aSChristoph Hellwig 
nfs4_put_copy(struct nfsd4_copy * copy)12728ea6e2c9SChuck Lever static void nfs4_put_copy(struct nfsd4_copy *copy)
1273e0639dc5SOlga Kornievskaia {
1274e0639dc5SOlga Kornievskaia 	if (!refcount_dec_and_test(&copy->refcount))
1275e0639dc5SOlga Kornievskaia 		return;
1276ae267989SChuck Lever 	atomic_dec(&copy->cp_nn->pending_async_copies);
127787689df6SChuck Lever 	kfree(copy->cp_src);
1278e0639dc5SOlga Kornievskaia 	kfree(copy);
1279e0639dc5SOlga Kornievskaia }
1280e0639dc5SOlga Kornievskaia 
nfsd4_stop_copy(struct nfsd4_copy * copy)1281e0639dc5SOlga Kornievskaia static void nfsd4_stop_copy(struct nfsd4_copy *copy)
1282e0639dc5SOlga Kornievskaia {
12831913cdf5SChuck Lever 	if (!test_and_set_bit(NFSD4_COPY_F_STOPPED, &copy->cp_flags))
1284e0639dc5SOlga Kornievskaia 		kthread_stop(copy->copy_task);
1285e0639dc5SOlga Kornievskaia 	nfs4_put_copy(copy);
1286e0639dc5SOlga Kornievskaia }
1287e0639dc5SOlga Kornievskaia 
nfsd4_unhash_copy(struct nfs4_client * clp)1288*1dfc79bfSChuck Lever static struct nfsd4_copy *nfsd4_unhash_copy(struct nfs4_client *clp)
1289e0639dc5SOlga Kornievskaia {
1290e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy = NULL;
1291e0639dc5SOlga Kornievskaia 
1292e0639dc5SOlga Kornievskaia 	spin_lock(&clp->async_lock);
1293e0639dc5SOlga Kornievskaia 	if (!list_empty(&clp->async_copies)) {
1294e0639dc5SOlga Kornievskaia 		copy = list_first_entry(&clp->async_copies, struct nfsd4_copy,
1295e0639dc5SOlga Kornievskaia 					copies);
1296e0639dc5SOlga Kornievskaia 		refcount_inc(&copy->refcount);
1297*1dfc79bfSChuck Lever 		copy->cp_clp = NULL;
1298*1dfc79bfSChuck Lever 		if (!list_empty(&copy->copies))
1299*1dfc79bfSChuck Lever 			list_del_init(&copy->copies);
1300e0639dc5SOlga Kornievskaia 	}
1301e0639dc5SOlga Kornievskaia 	spin_unlock(&clp->async_lock);
1302e0639dc5SOlga Kornievskaia 	return copy;
1303e0639dc5SOlga Kornievskaia }
1304e0639dc5SOlga Kornievskaia 
nfsd4_shutdown_copy(struct nfs4_client * clp)1305e0639dc5SOlga Kornievskaia void nfsd4_shutdown_copy(struct nfs4_client *clp)
1306e0639dc5SOlga Kornievskaia {
1307e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy;
1308e0639dc5SOlga Kornievskaia 
1309*1dfc79bfSChuck Lever 	while ((copy = nfsd4_unhash_copy(clp)) != NULL)
1310e0639dc5SOlga Kornievskaia 		nfsd4_stop_copy(copy);
1311e0639dc5SOlga Kornievskaia }
1312ce0887acSOlga Kornievskaia #ifdef CONFIG_NFSD_V4_2_INTER_SSC
1313ce0887acSOlga Kornievskaia 
1314ce0887acSOlga Kornievskaia extern struct file *nfs42_ssc_open(struct vfsmount *ss_mnt,
1315ce0887acSOlga Kornievskaia 				   struct nfs_fh *src_fh,
1316ce0887acSOlga Kornievskaia 				   nfs4_stateid *stateid);
1317ce0887acSOlga Kornievskaia extern void nfs42_ssc_close(struct file *filep);
1318ce0887acSOlga Kornievskaia 
1319ce0887acSOlga Kornievskaia extern void nfs_sb_deactive(struct super_block *sb);
1320ce0887acSOlga Kornievskaia 
1321ce0887acSOlga Kornievskaia #define NFSD42_INTERSSC_MOUNTOPS "vers=4.2,addr=%s,sec=sys"
1322ce0887acSOlga Kornievskaia 
1323f2453978SChuck Lever /*
1324f4e44b39SDai Ngo  * setup a work entry in the ssc delayed unmount list.
1325f4e44b39SDai Ngo  */
nfsd4_ssc_setup_dul(struct nfsd_net * nn,char * ipaddr,struct nfsd4_ssc_umount_item ** nsui)1326f47dc2d3SDai Ngo static __be32 nfsd4_ssc_setup_dul(struct nfsd_net *nn, char *ipaddr,
1327df24ac7aSDai Ngo 				  struct nfsd4_ssc_umount_item **nsui)
1328f4e44b39SDai Ngo {
13298e70bf27SColin Ian King 	struct nfsd4_ssc_umount_item *ni = NULL;
1330f4e44b39SDai Ngo 	struct nfsd4_ssc_umount_item *work = NULL;
1331f4e44b39SDai Ngo 	struct nfsd4_ssc_umount_item *tmp;
1332f4e44b39SDai Ngo 	DEFINE_WAIT(wait);
1333df24ac7aSDai Ngo 	__be32 status = 0;
1334f4e44b39SDai Ngo 
1335df24ac7aSDai Ngo 	*nsui = NULL;
1336f4e44b39SDai Ngo 	work = kzalloc(sizeof(*work), GFP_KERNEL);
1337f4e44b39SDai Ngo try_again:
1338f4e44b39SDai Ngo 	spin_lock(&nn->nfsd_ssc_lock);
1339f4e44b39SDai Ngo 	list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
1340f4e44b39SDai Ngo 		if (strncmp(ni->nsui_ipaddr, ipaddr, sizeof(ni->nsui_ipaddr)))
1341f4e44b39SDai Ngo 			continue;
1342f4e44b39SDai Ngo 		/* found a match */
1343f4e44b39SDai Ngo 		if (ni->nsui_busy) {
1344f4e44b39SDai Ngo 			/*  wait - and try again */
134539039024SNeilBrown 			prepare_to_wait(&nn->nfsd_ssc_waitq, &wait, TASK_IDLE);
1346f4e44b39SDai Ngo 			spin_unlock(&nn->nfsd_ssc_lock);
1347f4e44b39SDai Ngo 
1348f4e44b39SDai Ngo 			/* allow 20secs for mount/unmount for now - revisit */
134939039024SNeilBrown 			if (kthread_should_stop() ||
1350f4e44b39SDai Ngo 					(schedule_timeout(20*HZ) == 0)) {
1351e6cf91b7SXingyuan Mo 				finish_wait(&nn->nfsd_ssc_waitq, &wait);
1352f4e44b39SDai Ngo 				kfree(work);
1353f4e44b39SDai Ngo 				return nfserr_eagain;
1354f4e44b39SDai Ngo 			}
1355f4e44b39SDai Ngo 			finish_wait(&nn->nfsd_ssc_waitq, &wait);
1356f4e44b39SDai Ngo 			goto try_again;
1357f4e44b39SDai Ngo 		}
1358df24ac7aSDai Ngo 		*nsui = ni;
1359f4e44b39SDai Ngo 		refcount_inc(&ni->nsui_refcnt);
1360f4e44b39SDai Ngo 		spin_unlock(&nn->nfsd_ssc_lock);
1361f4e44b39SDai Ngo 		kfree(work);
1362f4e44b39SDai Ngo 
1363df24ac7aSDai Ngo 		/* return vfsmount in (*nsui)->nsui_vfsmount */
1364f4e44b39SDai Ngo 		return 0;
1365f4e44b39SDai Ngo 	}
1366f4e44b39SDai Ngo 	if (work) {
136772f78ae0SWolfram Sang 		strscpy(work->nsui_ipaddr, ipaddr, sizeof(work->nsui_ipaddr) - 1);
1368f4e44b39SDai Ngo 		refcount_set(&work->nsui_refcnt, 2);
1369f4e44b39SDai Ngo 		work->nsui_busy = true;
1370f4e44b39SDai Ngo 		list_add_tail(&work->nsui_list, &nn->nfsd_ssc_mount_list);
1371df24ac7aSDai Ngo 		*nsui = work;
1372df24ac7aSDai Ngo 	} else
1373df24ac7aSDai Ngo 		status = nfserr_resource;
1374f4e44b39SDai Ngo 	spin_unlock(&nn->nfsd_ssc_lock);
1375df24ac7aSDai Ngo 	return status;
1376f4e44b39SDai Ngo }
1377f4e44b39SDai Ngo 
nfsd4_ssc_update_dul(struct nfsd_net * nn,struct nfsd4_ssc_umount_item * nsui,struct vfsmount * ss_mnt)1378df24ac7aSDai Ngo static void nfsd4_ssc_update_dul(struct nfsd_net *nn,
1379df24ac7aSDai Ngo 				 struct nfsd4_ssc_umount_item *nsui,
1380df24ac7aSDai Ngo 				 struct vfsmount *ss_mnt)
1381f4e44b39SDai Ngo {
1382f4e44b39SDai Ngo 	spin_lock(&nn->nfsd_ssc_lock);
1383df24ac7aSDai Ngo 	nsui->nsui_vfsmount = ss_mnt;
1384df24ac7aSDai Ngo 	nsui->nsui_busy = false;
1385f4e44b39SDai Ngo 	wake_up_all(&nn->nfsd_ssc_waitq);
1386f4e44b39SDai Ngo 	spin_unlock(&nn->nfsd_ssc_lock);
1387f4e44b39SDai Ngo }
1388f4e44b39SDai Ngo 
nfsd4_ssc_cancel_dul(struct nfsd_net * nn,struct nfsd4_ssc_umount_item * nsui)1389df24ac7aSDai Ngo static void nfsd4_ssc_cancel_dul(struct nfsd_net *nn,
1390df24ac7aSDai Ngo 				 struct nfsd4_ssc_umount_item *nsui)
1391f4e44b39SDai Ngo {
1392f4e44b39SDai Ngo 	spin_lock(&nn->nfsd_ssc_lock);
1393df24ac7aSDai Ngo 	list_del(&nsui->nsui_list);
1394f4e44b39SDai Ngo 	wake_up_all(&nn->nfsd_ssc_waitq);
1395f4e44b39SDai Ngo 	spin_unlock(&nn->nfsd_ssc_lock);
1396df24ac7aSDai Ngo 	kfree(nsui);
1397f4e44b39SDai Ngo }
1398f4e44b39SDai Ngo 
1399f4e44b39SDai Ngo /*
1400ce0887acSOlga Kornievskaia  * Support one copy source server for now.
1401ce0887acSOlga Kornievskaia  */
1402ce0887acSOlga Kornievskaia static __be32
nfsd4_interssc_connect(struct nl4_server * nss,struct svc_rqst * rqstp,struct nfsd4_ssc_umount_item ** nsui)1403ce0887acSOlga Kornievskaia nfsd4_interssc_connect(struct nl4_server *nss, struct svc_rqst *rqstp,
1404df24ac7aSDai Ngo 		       struct nfsd4_ssc_umount_item **nsui)
1405ce0887acSOlga Kornievskaia {
1406ce0887acSOlga Kornievskaia 	struct file_system_type *type;
1407ce0887acSOlga Kornievskaia 	struct vfsmount *ss_mnt;
1408ce0887acSOlga Kornievskaia 	struct nfs42_netaddr *naddr;
1409ce0887acSOlga Kornievskaia 	struct sockaddr_storage tmp_addr;
1410ce0887acSOlga Kornievskaia 	size_t tmp_addrlen, match_netid_len = 3;
1411ce0887acSOlga Kornievskaia 	char *startsep = "", *endsep = "", *match_netid = "tcp";
1412ce0887acSOlga Kornievskaia 	char *ipaddr, *dev_name, *raw_data;
1413b8290ca2SOlga Kornievskaia 	int len, raw_len;
1414b8290ca2SOlga Kornievskaia 	__be32 status = nfserr_inval;
1415f4e44b39SDai Ngo 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1416ce0887acSOlga Kornievskaia 
1417ce0887acSOlga Kornievskaia 	naddr = &nss->u.nl4_addr;
1418ce0887acSOlga Kornievskaia 	tmp_addrlen = rpc_uaddr2sockaddr(SVC_NET(rqstp), naddr->addr,
1419ce0887acSOlga Kornievskaia 					 naddr->addr_len,
1420ce0887acSOlga Kornievskaia 					 (struct sockaddr *)&tmp_addr,
1421ce0887acSOlga Kornievskaia 					 sizeof(tmp_addr));
1422df24ac7aSDai Ngo 	*nsui = NULL;
1423ce0887acSOlga Kornievskaia 	if (tmp_addrlen == 0)
1424ce0887acSOlga Kornievskaia 		goto out_err;
1425ce0887acSOlga Kornievskaia 
1426ce0887acSOlga Kornievskaia 	if (tmp_addr.ss_family == AF_INET6) {
1427ce0887acSOlga Kornievskaia 		startsep = "[";
1428ce0887acSOlga Kornievskaia 		endsep = "]";
1429ce0887acSOlga Kornievskaia 		match_netid = "tcp6";
1430ce0887acSOlga Kornievskaia 		match_netid_len = 4;
1431ce0887acSOlga Kornievskaia 	}
1432ce0887acSOlga Kornievskaia 
1433ce0887acSOlga Kornievskaia 	if (naddr->netid_len != match_netid_len ||
1434ce0887acSOlga Kornievskaia 		strncmp(naddr->netid, match_netid, naddr->netid_len))
1435ce0887acSOlga Kornievskaia 		goto out_err;
1436ce0887acSOlga Kornievskaia 
1437ce0887acSOlga Kornievskaia 	/* Construct the raw data for the vfs_kern_mount call */
1438ce0887acSOlga Kornievskaia 	len = RPC_MAX_ADDRBUFLEN + 1;
1439ce0887acSOlga Kornievskaia 	ipaddr = kzalloc(len, GFP_KERNEL);
1440ce0887acSOlga Kornievskaia 	if (!ipaddr)
1441ce0887acSOlga Kornievskaia 		goto out_err;
1442ce0887acSOlga Kornievskaia 
1443ce0887acSOlga Kornievskaia 	rpc_ntop((struct sockaddr *)&tmp_addr, ipaddr, len);
1444ce0887acSOlga Kornievskaia 
1445ce0887acSOlga Kornievskaia 	/* 2 for ipv6 endsep and startsep. 3 for ":/" and trailing '/0'*/
1446ce0887acSOlga Kornievskaia 
1447ce0887acSOlga Kornievskaia 	raw_len = strlen(NFSD42_INTERSSC_MOUNTOPS) + strlen(ipaddr);
1448ce0887acSOlga Kornievskaia 	raw_data = kzalloc(raw_len, GFP_KERNEL);
1449ce0887acSOlga Kornievskaia 	if (!raw_data)
1450ce0887acSOlga Kornievskaia 		goto out_free_ipaddr;
1451ce0887acSOlga Kornievskaia 
1452ce0887acSOlga Kornievskaia 	snprintf(raw_data, raw_len, NFSD42_INTERSSC_MOUNTOPS, ipaddr);
1453ce0887acSOlga Kornievskaia 
1454b8290ca2SOlga Kornievskaia 	status = nfserr_nodev;
1455ce0887acSOlga Kornievskaia 	type = get_fs_type("nfs");
1456ce0887acSOlga Kornievskaia 	if (!type)
1457ce0887acSOlga Kornievskaia 		goto out_free_rawdata;
1458ce0887acSOlga Kornievskaia 
1459ce0887acSOlga Kornievskaia 	/* Set the server:<export> for the vfs_kern_mount call */
1460ce0887acSOlga Kornievskaia 	dev_name = kzalloc(len + 5, GFP_KERNEL);
1461ce0887acSOlga Kornievskaia 	if (!dev_name)
1462ce0887acSOlga Kornievskaia 		goto out_free_rawdata;
1463ce0887acSOlga Kornievskaia 	snprintf(dev_name, len + 5, "%s%s%s:/", startsep, ipaddr, endsep);
1464ce0887acSOlga Kornievskaia 
1465df24ac7aSDai Ngo 	status = nfsd4_ssc_setup_dul(nn, ipaddr, nsui);
1466f4e44b39SDai Ngo 	if (status)
1467f4e44b39SDai Ngo 		goto out_free_devname;
1468df24ac7aSDai Ngo 	if ((*nsui)->nsui_vfsmount)
1469f4e44b39SDai Ngo 		goto out_done;
1470f4e44b39SDai Ngo 
1471ce0887acSOlga Kornievskaia 	/* Use an 'internal' mount: SB_KERNMOUNT -> MNT_INTERNAL */
1472ce0887acSOlga Kornievskaia 	ss_mnt = vfs_kern_mount(type, SB_KERNMOUNT, dev_name, raw_data);
1473ce0887acSOlga Kornievskaia 	module_put(type->owner);
1474f4e44b39SDai Ngo 	if (IS_ERR(ss_mnt)) {
147554185267SWei Yongjun 		status = nfserr_nodev;
1476df24ac7aSDai Ngo 		nfsd4_ssc_cancel_dul(nn, *nsui);
1477ce0887acSOlga Kornievskaia 		goto out_free_devname;
1478f4e44b39SDai Ngo 	}
1479df24ac7aSDai Ngo 	nfsd4_ssc_update_dul(nn, *nsui, ss_mnt);
1480f4e44b39SDai Ngo out_done:
1481ce0887acSOlga Kornievskaia 	status = 0;
1482ce0887acSOlga Kornievskaia 
1483ce0887acSOlga Kornievskaia out_free_devname:
1484ce0887acSOlga Kornievskaia 	kfree(dev_name);
1485ce0887acSOlga Kornievskaia out_free_rawdata:
1486ce0887acSOlga Kornievskaia 	kfree(raw_data);
1487ce0887acSOlga Kornievskaia out_free_ipaddr:
1488ce0887acSOlga Kornievskaia 	kfree(ipaddr);
1489ce0887acSOlga Kornievskaia out_err:
1490ce0887acSOlga Kornievskaia 	return status;
1491ce0887acSOlga Kornievskaia }
1492ce0887acSOlga Kornievskaia 
1493f2453978SChuck Lever /*
1494ce0887acSOlga Kornievskaia  * Verify COPY destination stateid.
1495f2453978SChuck Lever  *
1496ce0887acSOlga Kornievskaia  * Connect to the source server with NFSv4.1.
1497ce0887acSOlga Kornievskaia  * Create the source struct file for nfsd_copy_range.
1498ce0887acSOlga Kornievskaia  * Called with COPY cstate:
1499ce0887acSOlga Kornievskaia  *    SAVED_FH: source filehandle
1500ce0887acSOlga Kornievskaia  *    CURRENT_FH: destination filehandle
1501ce0887acSOlga Kornievskaia  */
1502ce0887acSOlga Kornievskaia static __be32
nfsd4_setup_inter_ssc(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_copy * copy)1503ce0887acSOlga Kornievskaia nfsd4_setup_inter_ssc(struct svc_rqst *rqstp,
1504ce0887acSOlga Kornievskaia 		      struct nfsd4_compound_state *cstate,
1505df24ac7aSDai Ngo 		      struct nfsd4_copy *copy)
1506ce0887acSOlga Kornievskaia {
1507ce0887acSOlga Kornievskaia 	struct svc_fh *s_fh = NULL;
1508ce0887acSOlga Kornievskaia 	stateid_t *s_stid = &copy->cp_src_stateid;
1509b8290ca2SOlga Kornievskaia 	__be32 status = nfserr_inval;
1510ce0887acSOlga Kornievskaia 
1511ce0887acSOlga Kornievskaia 	/* Verify the destination stateid and set dst struct file*/
1512ce0887acSOlga Kornievskaia 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1513ce0887acSOlga Kornievskaia 					    &copy->cp_dst_stateid,
1514ce0887acSOlga Kornievskaia 					    WR_STATE, &copy->nf_dst, NULL);
1515ce0887acSOlga Kornievskaia 	if (status)
1516ce0887acSOlga Kornievskaia 		goto out;
1517ce0887acSOlga Kornievskaia 
1518df24ac7aSDai Ngo 	status = nfsd4_interssc_connect(copy->cp_src, rqstp, &copy->ss_nsui);
1519ce0887acSOlga Kornievskaia 	if (status)
1520ce0887acSOlga Kornievskaia 		goto out;
1521ce0887acSOlga Kornievskaia 
1522ce0887acSOlga Kornievskaia 	s_fh = &cstate->save_fh;
1523ce0887acSOlga Kornievskaia 
1524ce0887acSOlga Kornievskaia 	copy->c_fh.size = s_fh->fh_handle.fh_size;
1525d8b26071SNeilBrown 	memcpy(copy->c_fh.data, &s_fh->fh_handle.fh_raw, copy->c_fh.size);
15263f9544caSOlga Kornievskaia 	copy->stateid.seqid = cpu_to_be32(s_stid->si_generation);
1527ce0887acSOlga Kornievskaia 	memcpy(copy->stateid.other, (void *)&s_stid->si_opaque,
1528ce0887acSOlga Kornievskaia 	       sizeof(stateid_opaque_t));
1529ce0887acSOlga Kornievskaia 
1530ce0887acSOlga Kornievskaia 	status = 0;
1531ce0887acSOlga Kornievskaia out:
1532ce0887acSOlga Kornievskaia 	return status;
1533ce0887acSOlga Kornievskaia }
1534ce0887acSOlga Kornievskaia 
1535ce0887acSOlga Kornievskaia static void
nfsd4_cleanup_inter_ssc(struct nfsd4_ssc_umount_item * nsui,struct file * filp,struct nfsd_file * dst)1536df24ac7aSDai Ngo nfsd4_cleanup_inter_ssc(struct nfsd4_ssc_umount_item *nsui, struct file *filp,
1537ce0887acSOlga Kornievskaia 			struct nfsd_file *dst)
1538ce0887acSOlga Kornievskaia {
1539f4e44b39SDai Ngo 	struct nfsd_net *nn = net_generic(dst->nf_net, nfsd_net_id);
1540df24ac7aSDai Ngo 	long timeout = msecs_to_jiffies(nfsd4_ssc_umount_timeout);
1541f4e44b39SDai Ngo 
154224d796eaSChuck Lever 	nfs42_ssc_close(filp);
154324d796eaSChuck Lever 	fput(filp);
1544f4e44b39SDai Ngo 
1545f4e44b39SDai Ngo 	spin_lock(&nn->nfsd_ssc_lock);
1546df24ac7aSDai Ngo 	list_del(&nsui->nsui_list);
1547f4e44b39SDai Ngo 	/*
1548f4e44b39SDai Ngo 	 * vfsmount can be shared by multiple exports,
1549f4e44b39SDai Ngo 	 * decrement refcnt. If the count drops to 1 it
1550f4e44b39SDai Ngo 	 * will be unmounted when nsui_expire expires.
1551f4e44b39SDai Ngo 	 */
1552df24ac7aSDai Ngo 	refcount_dec(&nsui->nsui_refcnt);
1553df24ac7aSDai Ngo 	nsui->nsui_expire = jiffies + timeout;
1554df24ac7aSDai Ngo 	list_add_tail(&nsui->nsui_list, &nn->nfsd_ssc_mount_list);
1555f4e44b39SDai Ngo 	spin_unlock(&nn->nfsd_ssc_lock);
1556ce0887acSOlga Kornievskaia }
1557ce0887acSOlga Kornievskaia 
1558ce0887acSOlga Kornievskaia #else /* CONFIG_NFSD_V4_2_INTER_SSC */
1559ce0887acSOlga Kornievskaia 
1560ce0887acSOlga Kornievskaia static __be32
nfsd4_setup_inter_ssc(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_copy * copy)1561ce0887acSOlga Kornievskaia nfsd4_setup_inter_ssc(struct svc_rqst *rqstp,
1562ce0887acSOlga Kornievskaia 		      struct nfsd4_compound_state *cstate,
1563df24ac7aSDai Ngo 		      struct nfsd4_copy *copy)
1564ce0887acSOlga Kornievskaia {
1565b8290ca2SOlga Kornievskaia 	return nfserr_inval;
1566ce0887acSOlga Kornievskaia }
1567ce0887acSOlga Kornievskaia 
1568ce0887acSOlga Kornievskaia static void
nfsd4_cleanup_inter_ssc(struct nfsd4_ssc_umount_item * nsui,struct file * filp,struct nfsd_file * dst)1569df24ac7aSDai Ngo nfsd4_cleanup_inter_ssc(struct nfsd4_ssc_umount_item *nsui, struct file *filp,
1570ce0887acSOlga Kornievskaia 			struct nfsd_file *dst)
1571ce0887acSOlga Kornievskaia {
1572ce0887acSOlga Kornievskaia }
1573ce0887acSOlga Kornievskaia 
nfs42_ssc_open(struct vfsmount * ss_mnt,struct nfs_fh * src_fh,nfs4_stateid * stateid)1574ce0887acSOlga Kornievskaia static struct file *nfs42_ssc_open(struct vfsmount *ss_mnt,
1575ce0887acSOlga Kornievskaia 				   struct nfs_fh *src_fh,
1576ce0887acSOlga Kornievskaia 				   nfs4_stateid *stateid)
1577ce0887acSOlga Kornievskaia {
1578ce0887acSOlga Kornievskaia 	return NULL;
1579ce0887acSOlga Kornievskaia }
1580ce0887acSOlga Kornievskaia #endif /* CONFIG_NFSD_V4_2_INTER_SSC */
1581ce0887acSOlga Kornievskaia 
1582ce0887acSOlga Kornievskaia static __be32
nfsd4_setup_intra_ssc(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_copy * copy)1583ce0887acSOlga Kornievskaia nfsd4_setup_intra_ssc(struct svc_rqst *rqstp,
1584ce0887acSOlga Kornievskaia 		      struct nfsd4_compound_state *cstate,
1585ce0887acSOlga Kornievskaia 		      struct nfsd4_copy *copy)
1586ce0887acSOlga Kornievskaia {
1587ce0887acSOlga Kornievskaia 	return nfsd4_verify_copy(rqstp, cstate, &copy->cp_src_stateid,
1588ce0887acSOlga Kornievskaia 				 &copy->nf_src, &copy->cp_dst_stateid,
1589ce0887acSOlga Kornievskaia 				 &copy->nf_dst);
1590ce0887acSOlga Kornievskaia }
1591ce0887acSOlga Kornievskaia 
nfsd4_cb_offload_release(struct nfsd4_callback * cb)1592e0639dc5SOlga Kornievskaia static void nfsd4_cb_offload_release(struct nfsd4_callback *cb)
1593e0639dc5SOlga Kornievskaia {
1594a11ada99SChuck Lever 	struct nfsd4_cb_offload *cbo =
1595a11ada99SChuck Lever 		container_of(cb, struct nfsd4_cb_offload, co_cb);
1596e0639dc5SOlga Kornievskaia 
1597a11ada99SChuck Lever 	kfree(cbo);
1598e0639dc5SOlga Kornievskaia }
1599e0639dc5SOlga Kornievskaia 
nfsd4_cb_offload_done(struct nfsd4_callback * cb,struct rpc_task * task)1600e0639dc5SOlga Kornievskaia static int nfsd4_cb_offload_done(struct nfsd4_callback *cb,
1601e0639dc5SOlga Kornievskaia 				 struct rpc_task *task)
1602e0639dc5SOlga Kornievskaia {
16031035d654SChuck Lever 	struct nfsd4_cb_offload *cbo =
16041035d654SChuck Lever 		container_of(cb, struct nfsd4_cb_offload, co_cb);
16051035d654SChuck Lever 
16061035d654SChuck Lever 	trace_nfsd_cb_offload_done(&cbo->co_res.cb_stateid, task);
1607e0639dc5SOlga Kornievskaia 	return 1;
1608e0639dc5SOlga Kornievskaia }
1609e0639dc5SOlga Kornievskaia 
1610e0639dc5SOlga Kornievskaia static const struct nfsd4_callback_ops nfsd4_cb_offload_ops = {
1611e0639dc5SOlga Kornievskaia 	.release = nfsd4_cb_offload_release,
1612e0639dc5SOlga Kornievskaia 	.done = nfsd4_cb_offload_done
1613e0639dc5SOlga Kornievskaia };
1614e0639dc5SOlga Kornievskaia 
nfsd4_init_copy_res(struct nfsd4_copy * copy,bool sync)1615e0639dc5SOlga Kornievskaia static void nfsd4_init_copy_res(struct nfsd4_copy *copy, bool sync)
1616e0639dc5SOlga Kornievskaia {
1617eac0b17aSOlga Kornievskaia 	copy->cp_res.wr_stable_how =
16181913cdf5SChuck Lever 		test_bit(NFSD4_COPY_F_COMMITTED, &copy->cp_flags) ?
16191913cdf5SChuck Lever 			NFS_FILE_SYNC : NFS_UNSTABLE;
16201913cdf5SChuck Lever 	nfsd4_copy_set_sync(copy, sync);
1621e0639dc5SOlga Kornievskaia }
1622e0639dc5SOlga Kornievskaia 
_nfsd_copy_file_range(struct nfsd4_copy * copy,struct file * dst,struct file * src)16233b7bf593SChuck Lever static ssize_t _nfsd_copy_file_range(struct nfsd4_copy *copy,
16243b7bf593SChuck Lever 				     struct file *dst,
16253b7bf593SChuck Lever 				     struct file *src)
1626e0639dc5SOlga Kornievskaia {
1627555dbf1aSTrond Myklebust 	errseq_t since;
1628e0639dc5SOlga Kornievskaia 	ssize_t bytes_copied = 0;
1629e7a833e9SJ. Bruce Fields 	u64 bytes_total = copy->cp_count;
1630e0639dc5SOlga Kornievskaia 	u64 src_pos = copy->cp_src_pos;
1631e0639dc5SOlga Kornievskaia 	u64 dst_pos = copy->cp_dst_pos;
1632c2f1c4bdSChuck Lever 	int status;
163379a1d88aSBrian Foster 	loff_t end;
1634e0639dc5SOlga Kornievskaia 
1635792a5112SJ. Bruce Fields 	/* See RFC 7862 p.67: */
1636792a5112SJ. Bruce Fields 	if (bytes_total == 0)
1637792a5112SJ. Bruce Fields 		bytes_total = ULLONG_MAX;
1638e0639dc5SOlga Kornievskaia 	do {
1639e0639dc5SOlga Kornievskaia 		if (kthread_should_stop())
1640e0639dc5SOlga Kornievskaia 			break;
1641555dbf1aSTrond Myklebust 		bytes_copied = nfsd_copy_file_range(src, src_pos, dst, dst_pos,
16425c4583b2SJeff Layton 						    bytes_total);
1643e0639dc5SOlga Kornievskaia 		if (bytes_copied <= 0)
1644e0639dc5SOlga Kornievskaia 			break;
1645e0639dc5SOlga Kornievskaia 		bytes_total -= bytes_copied;
1646e0639dc5SOlga Kornievskaia 		copy->cp_res.wr_bytes_written += bytes_copied;
1647e0639dc5SOlga Kornievskaia 		src_pos += bytes_copied;
1648e0639dc5SOlga Kornievskaia 		dst_pos += bytes_copied;
16491913cdf5SChuck Lever 	} while (bytes_total > 0 && nfsd4_copy_is_async(copy));
1650eac0b17aSOlga Kornievskaia 	/* for a non-zero asynchronous copy do a commit of data */
16511913cdf5SChuck Lever 	if (nfsd4_copy_is_async(copy) && copy->cp_res.wr_bytes_written > 0) {
1652555dbf1aSTrond Myklebust 		since = READ_ONCE(dst->f_wb_err);
165379a1d88aSBrian Foster 		end = copy->cp_dst_pos + copy->cp_res.wr_bytes_written - 1;
165479a1d88aSBrian Foster 		status = vfs_fsync_range(dst, copy->cp_dst_pos, end, 0);
1655555dbf1aSTrond Myklebust 		if (!status)
1656555dbf1aSTrond Myklebust 			status = filemap_check_wb_err(dst->f_mapping, since);
1657eac0b17aSOlga Kornievskaia 		if (!status)
16581913cdf5SChuck Lever 			set_bit(NFSD4_COPY_F_COMMITTED, &copy->cp_flags);
1659eac0b17aSOlga Kornievskaia 	}
1660e0639dc5SOlga Kornievskaia 	return bytes_copied;
1661e0639dc5SOlga Kornievskaia }
1662e0639dc5SOlga Kornievskaia 
nfsd4_do_copy(struct nfsd4_copy * copy,struct file * src,struct file * dst,bool sync)16633b7bf593SChuck Lever static __be32 nfsd4_do_copy(struct nfsd4_copy *copy,
16643b7bf593SChuck Lever 			    struct file *src, struct file *dst,
16653b7bf593SChuck Lever 			    bool sync)
1666e0639dc5SOlga Kornievskaia {
1667e0639dc5SOlga Kornievskaia 	__be32 status;
1668e0639dc5SOlga Kornievskaia 	ssize_t bytes;
1669e0639dc5SOlga Kornievskaia 
16703b7bf593SChuck Lever 	bytes = _nfsd_copy_file_range(copy, dst, src);
16713b7bf593SChuck Lever 
1672e0639dc5SOlga Kornievskaia 	/* for async copy, we ignore the error, client can always retry
1673e0639dc5SOlga Kornievskaia 	 * to get the error
1674e0639dc5SOlga Kornievskaia 	 */
1675e0639dc5SOlga Kornievskaia 	if (bytes < 0 && !copy->cp_res.wr_bytes_written)
1676e0639dc5SOlga Kornievskaia 		status = nfserrno(bytes);
1677e0639dc5SOlga Kornievskaia 	else {
1678e0639dc5SOlga Kornievskaia 		nfsd4_init_copy_res(copy, sync);
1679e0639dc5SOlga Kornievskaia 		status = nfs_ok;
1680e0639dc5SOlga Kornievskaia 	}
1681e0639dc5SOlga Kornievskaia 	return status;
1682e0639dc5SOlga Kornievskaia }
1683e0639dc5SOlga Kornievskaia 
dup_copy_fields(struct nfsd4_copy * src,struct nfsd4_copy * dst)1684eb162e17SChuck Lever static void dup_copy_fields(struct nfsd4_copy *src, struct nfsd4_copy *dst)
1685e0639dc5SOlga Kornievskaia {
1686e0639dc5SOlga Kornievskaia 	dst->cp_src_pos = src->cp_src_pos;
1687e0639dc5SOlga Kornievskaia 	dst->cp_dst_pos = src->cp_dst_pos;
1688e0639dc5SOlga Kornievskaia 	dst->cp_count = src->cp_count;
16891913cdf5SChuck Lever 	dst->cp_flags = src->cp_flags;
1690e0639dc5SOlga Kornievskaia 	memcpy(&dst->cp_res, &src->cp_res, sizeof(src->cp_res));
1691e0639dc5SOlga Kornievskaia 	memcpy(&dst->fh, &src->fh, sizeof(src->fh));
1692e0639dc5SOlga Kornievskaia 	dst->cp_clp = src->cp_clp;
16935c4583b2SJeff Layton 	dst->nf_dst = nfsd_file_get(src->nf_dst);
16941913cdf5SChuck Lever 	/* for inter, nf_src doesn't exist yet */
16951913cdf5SChuck Lever 	if (!nfsd4_ssc_is_inter(src))
16965c4583b2SJeff Layton 		dst->nf_src = nfsd_file_get(src->nf_src);
1697ce0887acSOlga Kornievskaia 
1698e0639dc5SOlga Kornievskaia 	memcpy(&dst->cp_stateid, &src->cp_stateid, sizeof(src->cp_stateid));
169987689df6SChuck Lever 	memcpy(dst->cp_src, src->cp_src, sizeof(struct nl4_server));
1700ce0887acSOlga Kornievskaia 	memcpy(&dst->stateid, &src->stateid, sizeof(src->stateid));
1701ce0887acSOlga Kornievskaia 	memcpy(&dst->c_fh, &src->c_fh, sizeof(src->c_fh));
1702df24ac7aSDai Ngo 	dst->ss_nsui = src->ss_nsui;
1703e0639dc5SOlga Kornievskaia }
1704e0639dc5SOlga Kornievskaia 
release_copy_files(struct nfsd4_copy * copy)17056ba434cbSJeff Layton static void release_copy_files(struct nfsd4_copy *copy)
17066ba434cbSJeff Layton {
17076ba434cbSJeff Layton 	if (copy->nf_src)
17086ba434cbSJeff Layton 		nfsd_file_put(copy->nf_src);
17096ba434cbSJeff Layton 	if (copy->nf_dst)
17106ba434cbSJeff Layton 		nfsd_file_put(copy->nf_dst);
17116ba434cbSJeff Layton }
17126ba434cbSJeff Layton 
cleanup_async_copy(struct nfsd4_copy * copy)1713e0639dc5SOlga Kornievskaia static void cleanup_async_copy(struct nfsd4_copy *copy)
1714e0639dc5SOlga Kornievskaia {
1715624322f1SOlga Kornievskaia 	nfs4_free_copy_state(copy);
17166ba434cbSJeff Layton 	release_copy_files(copy);
171781e72297SDai Ngo 	if (copy->cp_clp) {
1718e0639dc5SOlga Kornievskaia 		spin_lock(&copy->cp_clp->async_lock);
171981e72297SDai Ngo 		if (!list_empty(&copy->copies))
172081e72297SDai Ngo 			list_del_init(&copy->copies);
1721e0639dc5SOlga Kornievskaia 		spin_unlock(&copy->cp_clp->async_lock);
172281e72297SDai Ngo 	}
1723e0639dc5SOlga Kornievskaia 	nfs4_put_copy(copy);
1724e0639dc5SOlga Kornievskaia }
1725e0639dc5SOlga Kornievskaia 
nfsd4_send_cb_offload(struct nfsd4_copy * copy,__be32 nfserr)1726a11ada99SChuck Lever static void nfsd4_send_cb_offload(struct nfsd4_copy *copy, __be32 nfserr)
1727e72f9bc0SChuck Lever {
1728a11ada99SChuck Lever 	struct nfsd4_cb_offload *cbo;
1729e72f9bc0SChuck Lever 
1730a11ada99SChuck Lever 	cbo = kzalloc(sizeof(*cbo), GFP_KERNEL);
1731a11ada99SChuck Lever 	if (!cbo)
1732e72f9bc0SChuck Lever 		return;
1733e72f9bc0SChuck Lever 
1734a11ada99SChuck Lever 	memcpy(&cbo->co_res, &copy->cp_res, sizeof(copy->cp_res));
1735a11ada99SChuck Lever 	memcpy(&cbo->co_fh, &copy->fh, sizeof(copy->fh));
1736a11ada99SChuck Lever 	cbo->co_nfserr = nfserr;
1737e72f9bc0SChuck Lever 
1738a11ada99SChuck Lever 	nfsd4_init_cb(&cbo->co_cb, copy->cp_clp, &nfsd4_cb_offload_ops,
1739a11ada99SChuck Lever 		      NFSPROC4_CLNT_CB_OFFLOAD);
1740a11ada99SChuck Lever 	trace_nfsd_cb_offload(copy->cp_clp, &cbo->co_res.cb_stateid,
1741a11ada99SChuck Lever 			      &cbo->co_fh, copy->cp_count, nfserr);
1742a11ada99SChuck Lever 	nfsd4_run_cb(&cbo->co_cb);
1743e72f9bc0SChuck Lever }
1744e72f9bc0SChuck Lever 
1745ad1e46c9SChuck Lever /**
1746ad1e46c9SChuck Lever  * nfsd4_do_async_copy - kthread function for background server-side COPY
1747ad1e46c9SChuck Lever  * @data: arguments for COPY operation
1748ad1e46c9SChuck Lever  *
1749ad1e46c9SChuck Lever  * Return values:
1750ad1e46c9SChuck Lever  *   %0: Copy operation is done.
1751ad1e46c9SChuck Lever  */
nfsd4_do_async_copy(void * data)1752e0639dc5SOlga Kornievskaia static int nfsd4_do_async_copy(void *data)
1753e0639dc5SOlga Kornievskaia {
1754e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy = (struct nfsd4_copy *)data;
1755a11ada99SChuck Lever 	__be32 nfserr;
1756e0639dc5SOlga Kornievskaia 
17571913cdf5SChuck Lever 	if (nfsd4_ssc_is_inter(copy)) {
1758ad1e46c9SChuck Lever 		struct file *filp;
1759ad1e46c9SChuck Lever 
1760df24ac7aSDai Ngo 		filp = nfs42_ssc_open(copy->ss_nsui->nsui_vfsmount,
1761df24ac7aSDai Ngo 				      &copy->c_fh, &copy->stateid);
1762ad1e46c9SChuck Lever 		if (IS_ERR(filp)) {
1763754035ffSOlga Kornievskaia 			switch (PTR_ERR(filp)) {
1764754035ffSOlga Kornievskaia 			case -EBADF:
1765754035ffSOlga Kornievskaia 				nfserr = nfserr_wrong_type;
1766754035ffSOlga Kornievskaia 				break;
1767754035ffSOlga Kornievskaia 			default:
1768a11ada99SChuck Lever 				nfserr = nfserr_offload_denied;
1769754035ffSOlga Kornievskaia 			}
177075333d48SDai Ngo 			/* ss_mnt will be unmounted by the laundromat */
1771ce0887acSOlga Kornievskaia 			goto do_callback;
1772ce0887acSOlga Kornievskaia 		}
1773a11ada99SChuck Lever 		nfserr = nfsd4_do_copy(copy, filp, copy->nf_dst->nf_file,
1774a11ada99SChuck Lever 				       false);
1775df24ac7aSDai Ngo 		nfsd4_cleanup_inter_ssc(copy->ss_nsui, filp, copy->nf_dst);
1776478ed7b1SChuck Lever 	} else {
1777a11ada99SChuck Lever 		nfserr = nfsd4_do_copy(copy, copy->nf_src->nf_file,
17783b7bf593SChuck Lever 				       copy->nf_dst->nf_file, false);
1779ce0887acSOlga Kornievskaia 	}
1780ce0887acSOlga Kornievskaia 
1781ce0887acSOlga Kornievskaia do_callback:
1782a11ada99SChuck Lever 	nfsd4_send_cb_offload(copy, nfserr);
1783e0639dc5SOlga Kornievskaia 	cleanup_async_copy(copy);
1784e0639dc5SOlga Kornievskaia 	return 0;
1785e0639dc5SOlga Kornievskaia }
1786e0639dc5SOlga Kornievskaia 
1787ffa0160aSChristoph Hellwig static __be32
nfsd4_copy(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)178829ae7f9dSAnna Schumaker nfsd4_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1789eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
179029ae7f9dSAnna Schumaker {
179120a10c78SChuck Lever 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1792e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *async_copy = NULL;
179320a10c78SChuck Lever 	struct nfsd4_copy *copy = &u->copy;
179420a10c78SChuck Lever 	struct nfsd42_write_res *result;
179520a10c78SChuck Lever 	__be32 status;
179620a10c78SChuck Lever 
179720a10c78SChuck Lever 	result = &copy->cp_res;
179820a10c78SChuck Lever 	nfsd_copy_write_verifier((__be32 *)&result->wr_verifier.data, nn);
179929ae7f9dSAnna Schumaker 
180074115b3eSDai Ngo 	copy->cp_clp = cstate->clp;
18011913cdf5SChuck Lever 	if (nfsd4_ssc_is_inter(copy)) {
18021913cdf5SChuck Lever 		if (!inter_copy_offload_enable || nfsd4_copy_is_sync(copy)) {
1803ce0887acSOlga Kornievskaia 			status = nfserr_notsupp;
180429ae7f9dSAnna Schumaker 			goto out;
1805ce0887acSOlga Kornievskaia 		}
1806df24ac7aSDai Ngo 		status = nfsd4_setup_inter_ssc(rqstp, cstate, copy);
1807ce0887acSOlga Kornievskaia 		if (status)
1808ce0887acSOlga Kornievskaia 			return nfserr_offload_denied;
1809ce0887acSOlga Kornievskaia 	} else {
1810ce0887acSOlga Kornievskaia 		status = nfsd4_setup_intra_ssc(rqstp, cstate, copy);
1811ce0887acSOlga Kornievskaia 		if (status)
1812ce0887acSOlga Kornievskaia 			return status;
1813ce0887acSOlga Kornievskaia 	}
181429ae7f9dSAnna Schumaker 
1815e0639dc5SOlga Kornievskaia 	memcpy(&copy->fh, &cstate->current_fh.fh_handle,
1816e0639dc5SOlga Kornievskaia 		sizeof(struct knfsd_fh));
18171913cdf5SChuck Lever 	if (nfsd4_copy_is_async(copy)) {
1818e0639dc5SOlga Kornievskaia 		async_copy = kzalloc(sizeof(struct nfsd4_copy), GFP_KERNEL);
1819e0639dc5SOlga Kornievskaia 		if (!async_copy)
1820ce0887acSOlga Kornievskaia 			goto out_err;
1821ae267989SChuck Lever 		async_copy->cp_nn = nn;
1822421f1a2aSChuck Lever 		INIT_LIST_HEAD(&async_copy->copies);
1823421f1a2aSChuck Lever 		refcount_set(&async_copy->refcount, 1);
1824ae267989SChuck Lever 		/* Arbitrary cap on number of pending async copy operations */
1825ae267989SChuck Lever 		if (atomic_inc_return(&nn->pending_async_copies) >
1826fb79d68aSChuck Lever 				(int)rqstp->rq_pool->sp_nrthreads)
1827ae267989SChuck Lever 			goto out_err;
182887689df6SChuck Lever 		async_copy->cp_src = kmalloc(sizeof(*async_copy->cp_src), GFP_KERNEL);
182987689df6SChuck Lever 		if (!async_copy->cp_src)
183087689df6SChuck Lever 			goto out_err;
1831ce0887acSOlga Kornievskaia 		if (!nfs4_init_copy_state(nn, copy))
1832ce0887acSOlga Kornievskaia 			goto out_err;
183320a10c78SChuck Lever 		memcpy(&result->cb_stateid, &copy->cp_stateid.cs_stid,
183420a10c78SChuck Lever 			sizeof(result->cb_stateid));
1835eb162e17SChuck Lever 		dup_copy_fields(copy, async_copy);
1836e0639dc5SOlga Kornievskaia 		async_copy->copy_task = kthread_create(nfsd4_do_async_copy,
1837e0639dc5SOlga Kornievskaia 				async_copy, "%s", "copy thread");
1838e0639dc5SOlga Kornievskaia 		if (IS_ERR(async_copy->copy_task))
1839e0639dc5SOlga Kornievskaia 			goto out_err;
1840e0639dc5SOlga Kornievskaia 		spin_lock(&async_copy->cp_clp->async_lock);
1841e0639dc5SOlga Kornievskaia 		list_add(&async_copy->copies,
1842e0639dc5SOlga Kornievskaia 				&async_copy->cp_clp->async_copies);
1843e0639dc5SOlga Kornievskaia 		spin_unlock(&async_copy->cp_clp->async_lock);
1844e0639dc5SOlga Kornievskaia 		wake_up_process(async_copy->copy_task);
1845e0639dc5SOlga Kornievskaia 		status = nfs_ok;
1846ce0887acSOlga Kornievskaia 	} else {
18473b7bf593SChuck Lever 		status = nfsd4_do_copy(copy, copy->nf_src->nf_file,
18483b7bf593SChuck Lever 				       copy->nf_dst->nf_file, true);
1849ce0887acSOlga Kornievskaia 	}
185029ae7f9dSAnna Schumaker out:
18516ba434cbSJeff Layton 	release_copy_files(copy);
185229ae7f9dSAnna Schumaker 	return status;
1853e0639dc5SOlga Kornievskaia out_err:
185434e8f9ecSDai Ngo 	if (nfsd4_ssc_is_inter(copy)) {
185534e8f9ecSDai Ngo 		/*
185634e8f9ecSDai Ngo 		 * Source's vfsmount of inter-copy will be unmounted
185734e8f9ecSDai Ngo 		 * by the laundromat. Use copy instead of async_copy
185834e8f9ecSDai Ngo 		 * since async_copy->ss_nsui might not be set yet.
185934e8f9ecSDai Ngo 		 */
186034e8f9ecSDai Ngo 		refcount_dec(&copy->ss_nsui->nsui_refcnt);
186134e8f9ecSDai Ngo 	}
186218f428d4SOlga Kornievskaia 	if (async_copy)
1863e0639dc5SOlga Kornievskaia 		cleanup_async_copy(async_copy);
1864ae267989SChuck Lever 	status = nfserr_jukebox;
1865e0639dc5SOlga Kornievskaia 	goto out;
1866e0639dc5SOlga Kornievskaia }
1867e0639dc5SOlga Kornievskaia 
1868fcb53097SJeff Layton static struct nfsd4_copy *
find_async_copy_locked(struct nfs4_client * clp,stateid_t * stateid)1869fcb53097SJeff Layton find_async_copy_locked(struct nfs4_client *clp, stateid_t *stateid)
1870fcb53097SJeff Layton {
1871fcb53097SJeff Layton 	struct nfsd4_copy *copy;
1872fcb53097SJeff Layton 
1873fcb53097SJeff Layton 	lockdep_assert_held(&clp->async_lock);
1874fcb53097SJeff Layton 
1875fcb53097SJeff Layton 	list_for_each_entry(copy, &clp->async_copies, copies) {
1876fcb53097SJeff Layton 		if (memcmp(&copy->cp_stateid.cs_stid, stateid, NFS4_STATEID_SIZE))
1877fcb53097SJeff Layton 			continue;
1878fcb53097SJeff Layton 		return copy;
1879fcb53097SJeff Layton 	}
1880fcb53097SJeff Layton 	return NULL;
1881fcb53097SJeff Layton }
1882fcb53097SJeff Layton 
1883fcb53097SJeff Layton static struct nfsd4_copy *
find_async_copy(struct nfs4_client * clp,stateid_t * stateid)1884e0639dc5SOlga Kornievskaia find_async_copy(struct nfs4_client *clp, stateid_t *stateid)
1885e0639dc5SOlga Kornievskaia {
1886e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy;
1887e0639dc5SOlga Kornievskaia 
1888e0639dc5SOlga Kornievskaia 	spin_lock(&clp->async_lock);
1889fcb53097SJeff Layton 	copy = find_async_copy_locked(clp, stateid);
1890fcb53097SJeff Layton 	if (copy)
1891e0639dc5SOlga Kornievskaia 		refcount_inc(&copy->refcount);
1892e0639dc5SOlga Kornievskaia 	spin_unlock(&clp->async_lock);
1893e0639dc5SOlga Kornievskaia 	return copy;
1894e0639dc5SOlga Kornievskaia }
189529ae7f9dSAnna Schumaker 
189629ae7f9dSAnna Schumaker static __be32
nfsd4_offload_cancel(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)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
nfsd4_copy_notify(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)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;
1940781fde1aSChuck Lever 	memcpy(&cn->cpn_cnr_stateid, &cps->cp_stateid.cs_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
nfsd4_fallocate(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_fallocate * fallocate,int flags)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);
1970eeff73f7SChuck Lever 	if (status != nfs_ok)
197195d871f0SAnna Schumaker 		return status;
197295d871f0SAnna Schumaker 
19735c4583b2SJeff Layton 	status = nfsd4_vfs_fallocate(rqstp, &cstate->current_fh, nf->nf_file,
197495d871f0SAnna Schumaker 				     fallocate->falloc_offset,
197595d871f0SAnna Schumaker 				     fallocate->falloc_length,
197695d871f0SAnna Schumaker 				     flags);
19775c4583b2SJeff Layton 	nfsd_file_put(nf);
197895d871f0SAnna Schumaker 	return status;
197995d871f0SAnna Schumaker }
1980fcb53097SJeff Layton 
19816308bc98SOlga Kornievskaia static __be32
nfsd4_offload_status(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)19826308bc98SOlga Kornievskaia nfsd4_offload_status(struct svc_rqst *rqstp,
19836308bc98SOlga Kornievskaia 		     struct nfsd4_compound_state *cstate,
19846308bc98SOlga Kornievskaia 		     union nfsd4_op_u *u)
19856308bc98SOlga Kornievskaia {
1986e0639dc5SOlga Kornievskaia 	struct nfsd4_offload_status *os = &u->offload_status;
1987fcb53097SJeff Layton 	__be32 status = nfs_ok;
1988e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy;
1989e0639dc5SOlga Kornievskaia 	struct nfs4_client *clp = cstate->clp;
1990e0639dc5SOlga Kornievskaia 
1991fcb53097SJeff Layton 	spin_lock(&clp->async_lock);
1992fcb53097SJeff Layton 	copy = find_async_copy_locked(clp, &os->stateid);
1993fcb53097SJeff Layton 	if (copy)
1994e0639dc5SOlga Kornievskaia 		os->count = copy->cp_res.wr_bytes_written;
1995fcb53097SJeff Layton 	else
1996e0639dc5SOlga Kornievskaia 		status = nfserr_bad_stateid;
1997fcb53097SJeff Layton 	spin_unlock(&clp->async_lock);
1998e0639dc5SOlga Kornievskaia 
1999e0639dc5SOlga Kornievskaia 	return status;
20006308bc98SOlga Kornievskaia }
200195d871f0SAnna Schumaker 
200295d871f0SAnna Schumaker static __be32
nfsd4_allocate(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)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
nfsd4_deallocate(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)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
nfsd4_seek(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)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);
2029eeff73f7SChuck Lever 	if (status)
203024bab491SAnna Schumaker 		return status;
203124bab491SAnna Schumaker 
203224bab491SAnna Schumaker 	switch (seek->seek_whence) {
203324bab491SAnna Schumaker 	case NFS4_CONTENT_DATA:
203424bab491SAnna Schumaker 		whence = SEEK_DATA;
203524bab491SAnna Schumaker 		break;
203624bab491SAnna Schumaker 	case NFS4_CONTENT_HOLE:
203724bab491SAnna Schumaker 		whence = SEEK_HOLE;
203824bab491SAnna Schumaker 		break;
203924bab491SAnna Schumaker 	default:
204024bab491SAnna Schumaker 		status = nfserr_union_notsupp;
204124bab491SAnna Schumaker 		goto out;
204224bab491SAnna Schumaker 	}
204324bab491SAnna Schumaker 
204424bab491SAnna Schumaker 	/*
204524bab491SAnna Schumaker 	 * Note:  This call does change file->f_pos, but nothing in NFSD
204624bab491SAnna Schumaker 	 *        should ever file->f_pos.
204724bab491SAnna Schumaker 	 */
20485c4583b2SJeff Layton 	seek->seek_pos = vfs_llseek(nf->nf_file, seek->seek_offset, whence);
204924bab491SAnna Schumaker 	if (seek->seek_pos < 0)
205024bab491SAnna Schumaker 		status = nfserrno(seek->seek_pos);
20515c4583b2SJeff Layton 	else if (seek->seek_pos >= i_size_read(file_inode(nf->nf_file)))
205224bab491SAnna Schumaker 		seek->seek_eof = true;
205324bab491SAnna Schumaker 
205424bab491SAnna Schumaker out:
20555c4583b2SJeff Layton 	nfsd_file_put(nf);
205624bab491SAnna Schumaker 	return status;
205724bab491SAnna Schumaker }
205824bab491SAnna Schumaker 
20591da177e4SLinus Torvalds /* This routine never returns NFS_OK!  If there are no other errors, it
20601da177e4SLinus Torvalds  * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
20611da177e4SLinus Torvalds  * attributes matched.  VERIFY is implemented by mapping NFSERR_SAME
20621da177e4SLinus Torvalds  * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
20631da177e4SLinus Torvalds  */
2064b37ad28bSAl Viro static __be32
_nfsd4_verify(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_verify * verify)2065c954e2a5SJ.Bruce Fields _nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2066ca364317SJ.Bruce Fields 	     struct nfsd4_verify *verify)
20671da177e4SLinus Torvalds {
20682ebbc012SAl Viro 	__be32 *buf, *p;
20691da177e4SLinus Torvalds 	int count;
2070b37ad28bSAl Viro 	__be32 status;
20711da177e4SLinus Torvalds 
20728837abcaSMiklos Szeredi 	status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
20731da177e4SLinus Torvalds 	if (status)
20741da177e4SLinus Torvalds 		return status;
20751da177e4SLinus Torvalds 
20763c8e0316SYu Zhiguo 	status = check_attr_support(rqstp, cstate, verify->ve_bmval, NULL);
20773c8e0316SYu Zhiguo 	if (status)
20783c8e0316SYu Zhiguo 		return status;
20793c8e0316SYu Zhiguo 
20801da177e4SLinus Torvalds 	if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
20811da177e4SLinus Torvalds 	    || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
20821da177e4SLinus Torvalds 		return nfserr_inval;
20831da177e4SLinus Torvalds 	if (verify->ve_attrlen & 3)
20841da177e4SLinus Torvalds 		return nfserr_inval;
20851da177e4SLinus Torvalds 
20861da177e4SLinus Torvalds 	/* count in words:
20871da177e4SLinus Torvalds 	 *   bitmap_len(1) + bitmap(2) + attr_len(1) = 4
20881da177e4SLinus Torvalds 	 */
20891da177e4SLinus Torvalds 	count = 4 + (verify->ve_attrlen >> 2);
20901da177e4SLinus Torvalds 	buf = kmalloc(count << 2, GFP_KERNEL);
20911da177e4SLinus Torvalds 	if (!buf)
20923e772463SJ. Bruce Fields 		return nfserr_jukebox;
20931da177e4SLinus Torvalds 
209484822d0bSJ. Bruce Fields 	p = buf;
2095d5184658SJ. Bruce Fields 	status = nfsd4_encode_fattr_to_buf(&p, count, &cstate->current_fh,
2096ca364317SJ.Bruce Fields 				    cstate->current_fh.fh_export,
2097d5184658SJ. Bruce Fields 				    cstate->current_fh.fh_dentry,
2098d5184658SJ. Bruce Fields 				    verify->ve_bmval,
2099406a7ea9SFrank Filz 				    rqstp, 0);
210041ae6e71SJ. Bruce Fields 	/*
210141ae6e71SJ. Bruce Fields 	 * If nfsd4_encode_fattr() ran out of space, assume that's because
210241ae6e71SJ. Bruce Fields 	 * the attributes are longer (hence different) than those given:
210341ae6e71SJ. Bruce Fields 	 */
210484822d0bSJ. Bruce Fields 	if (status == nfserr_resource)
21051da177e4SLinus Torvalds 		status = nfserr_not_same;
21061da177e4SLinus Torvalds 	if (status)
21071da177e4SLinus Torvalds 		goto out_kfree;
21081da177e4SLinus Torvalds 
210995ec28cdSBenny Halevy 	/* skip bitmap */
211095ec28cdSBenny Halevy 	p = buf + 1 + ntohl(buf[0]);
21111da177e4SLinus Torvalds 	status = nfserr_not_same;
21121da177e4SLinus Torvalds 	if (ntohl(*p++) != verify->ve_attrlen)
21131da177e4SLinus Torvalds 		goto out_kfree;
21141da177e4SLinus Torvalds 	if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
21151da177e4SLinus Torvalds 		status = nfserr_same;
21161da177e4SLinus Torvalds 
21171da177e4SLinus Torvalds out_kfree:
21181da177e4SLinus Torvalds 	kfree(buf);
21191da177e4SLinus Torvalds 	return status;
21201da177e4SLinus Torvalds }
21211da177e4SLinus Torvalds 
2122c954e2a5SJ.Bruce Fields static __be32
nfsd4_nverify(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)2123c954e2a5SJ.Bruce Fields nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2124eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
2125c954e2a5SJ.Bruce Fields {
2126c954e2a5SJ.Bruce Fields 	__be32 status;
2127c954e2a5SJ.Bruce Fields 
2128eb69853dSChristoph Hellwig 	status = _nfsd4_verify(rqstp, cstate, &u->verify);
2129c954e2a5SJ.Bruce Fields 	return status == nfserr_not_same ? nfs_ok : status;
2130c954e2a5SJ.Bruce Fields }
2131c954e2a5SJ.Bruce Fields 
2132c954e2a5SJ.Bruce Fields static __be32
nfsd4_verify(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)2133c954e2a5SJ.Bruce Fields nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2134eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
2135c954e2a5SJ.Bruce Fields {
2136c954e2a5SJ.Bruce Fields 	__be32 status;
2137c954e2a5SJ.Bruce Fields 
2138eb69853dSChristoph Hellwig 	status = _nfsd4_verify(rqstp, cstate, &u->nverify);
2139c954e2a5SJ.Bruce Fields 	return status == nfserr_same ? nfs_ok : status;
2140c954e2a5SJ.Bruce Fields }
2141c954e2a5SJ.Bruce Fields 
21429cf514ccSChristoph Hellwig #ifdef CONFIG_NFSD_PNFS
21439cf514ccSChristoph Hellwig static const struct nfsd4_layout_ops *
nfsd4_layout_verify(struct svc_export * exp,unsigned int layout_type)21449cf514ccSChristoph Hellwig nfsd4_layout_verify(struct svc_export *exp, unsigned int layout_type)
21459cf514ccSChristoph Hellwig {
21468a4c3926SJeff Layton 	if (!exp->ex_layout_types) {
21479cf514ccSChristoph Hellwig 		dprintk("%s: export does not support pNFS\n", __func__);
21489cf514ccSChristoph Hellwig 		return NULL;
21499cf514ccSChristoph Hellwig 	}
21509cf514ccSChristoph Hellwig 
2151b550a32eSAri Kauppi 	if (layout_type >= LAYOUT_TYPE_MAX ||
2152b550a32eSAri Kauppi 	    !(exp->ex_layout_types & (1 << layout_type))) {
21539cf514ccSChristoph Hellwig 		dprintk("%s: layout type %d not supported\n",
21549cf514ccSChristoph Hellwig 			__func__, layout_type);
21559cf514ccSChristoph Hellwig 		return NULL;
21569cf514ccSChristoph Hellwig 	}
21579cf514ccSChristoph Hellwig 
21589cf514ccSChristoph Hellwig 	return nfsd4_layout_ops[layout_type];
21599cf514ccSChristoph Hellwig }
21609cf514ccSChristoph Hellwig 
21619cf514ccSChristoph Hellwig static __be32
nfsd4_getdeviceinfo(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)21629cf514ccSChristoph Hellwig nfsd4_getdeviceinfo(struct svc_rqst *rqstp,
2163eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
21649cf514ccSChristoph Hellwig {
2165eb69853dSChristoph Hellwig 	struct nfsd4_getdeviceinfo *gdp = &u->getdeviceinfo;
21669cf514ccSChristoph Hellwig 	const struct nfsd4_layout_ops *ops;
21679cf514ccSChristoph Hellwig 	struct nfsd4_deviceid_map *map;
21689cf514ccSChristoph Hellwig 	struct svc_export *exp;
21699cf514ccSChristoph Hellwig 	__be32 nfserr;
21709cf514ccSChristoph Hellwig 
21719cf514ccSChristoph Hellwig 	dprintk("%s: layout_type %u dev_id [0x%llx:0x%x] maxcnt %u\n",
21729cf514ccSChristoph Hellwig 	       __func__,
21739cf514ccSChristoph Hellwig 	       gdp->gd_layout_type,
21749cf514ccSChristoph Hellwig 	       gdp->gd_devid.fsid_idx, gdp->gd_devid.generation,
21759cf514ccSChristoph Hellwig 	       gdp->gd_maxcount);
21769cf514ccSChristoph Hellwig 
21779cf514ccSChristoph Hellwig 	map = nfsd4_find_devid_map(gdp->gd_devid.fsid_idx);
21789cf514ccSChristoph Hellwig 	if (!map) {
21799cf514ccSChristoph Hellwig 		dprintk("%s: couldn't find device ID to export mapping!\n",
21809cf514ccSChristoph Hellwig 			__func__);
21819cf514ccSChristoph Hellwig 		return nfserr_noent;
21829cf514ccSChristoph Hellwig 	}
21839cf514ccSChristoph Hellwig 
21849cf514ccSChristoph Hellwig 	exp = rqst_exp_find(rqstp, map->fsid_type, map->fsid);
21859cf514ccSChristoph Hellwig 	if (IS_ERR(exp)) {
21869cf514ccSChristoph Hellwig 		dprintk("%s: could not find device id\n", __func__);
21879cf514ccSChristoph Hellwig 		return nfserr_noent;
21889cf514ccSChristoph Hellwig 	}
21899cf514ccSChristoph Hellwig 
21909cf514ccSChristoph Hellwig 	nfserr = nfserr_layoutunavailable;
21919cf514ccSChristoph Hellwig 	ops = nfsd4_layout_verify(exp, gdp->gd_layout_type);
21929cf514ccSChristoph Hellwig 	if (!ops)
21939cf514ccSChristoph Hellwig 		goto out;
21949cf514ccSChristoph Hellwig 
21959cf514ccSChristoph Hellwig 	nfserr = nfs_ok;
2196f99d4fbdSChristoph Hellwig 	if (gdp->gd_maxcount != 0) {
2197f99d4fbdSChristoph Hellwig 		nfserr = ops->proc_getdeviceinfo(exp->ex_path.mnt->mnt_sb,
2198ec59659bSJ. Bruce Fields 				rqstp, cstate->clp, gdp);
2199f99d4fbdSChristoph Hellwig 	}
22009cf514ccSChristoph Hellwig 
22019cf514ccSChristoph Hellwig 	gdp->gd_notify_types &= ops->notify_types;
22029cf514ccSChristoph Hellwig out:
2203a1420384SKinglong Mee 	exp_put(exp);
22049cf514ccSChristoph Hellwig 	return nfserr;
22059cf514ccSChristoph Hellwig }
22069cf514ccSChristoph Hellwig 
220734b1744cSJ. Bruce Fields static void
nfsd4_getdeviceinfo_release(union nfsd4_op_u * u)220834b1744cSJ. Bruce Fields nfsd4_getdeviceinfo_release(union nfsd4_op_u *u)
220934b1744cSJ. Bruce Fields {
221034b1744cSJ. Bruce Fields 	kfree(u->getdeviceinfo.gd_device);
221134b1744cSJ. Bruce Fields }
221234b1744cSJ. Bruce Fields 
22139cf514ccSChristoph Hellwig static __be32
nfsd4_layoutget(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)22149cf514ccSChristoph Hellwig nfsd4_layoutget(struct svc_rqst *rqstp,
2215eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
22169cf514ccSChristoph Hellwig {
2217eb69853dSChristoph Hellwig 	struct nfsd4_layoutget *lgp = &u->layoutget;
22189cf514ccSChristoph Hellwig 	struct svc_fh *current_fh = &cstate->current_fh;
22199cf514ccSChristoph Hellwig 	const struct nfsd4_layout_ops *ops;
22209cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls;
22219cf514ccSChristoph Hellwig 	__be32 nfserr;
2222b047898aSChuck Lever 	int accmode = NFSD_MAY_READ_IF_EXEC | NFSD_MAY_OWNER_OVERRIDE;
22239cf514ccSChristoph Hellwig 
22249cf514ccSChristoph Hellwig 	switch (lgp->lg_seg.iomode) {
22259cf514ccSChristoph Hellwig 	case IOMODE_READ:
222666282ec1SBenjamin Coddington 		accmode |= NFSD_MAY_READ;
22279cf514ccSChristoph Hellwig 		break;
22289cf514ccSChristoph Hellwig 	case IOMODE_RW:
222966282ec1SBenjamin Coddington 		accmode |= NFSD_MAY_READ | NFSD_MAY_WRITE;
22309cf514ccSChristoph Hellwig 		break;
22319cf514ccSChristoph Hellwig 	default:
22329cf514ccSChristoph Hellwig 		dprintk("%s: invalid iomode %d\n",
22339cf514ccSChristoph Hellwig 			__func__, lgp->lg_seg.iomode);
22349cf514ccSChristoph Hellwig 		nfserr = nfserr_badiomode;
22359cf514ccSChristoph Hellwig 		goto out;
22369cf514ccSChristoph Hellwig 	}
22379cf514ccSChristoph Hellwig 
22389cf514ccSChristoph Hellwig 	nfserr = fh_verify(rqstp, current_fh, 0, accmode);
22399cf514ccSChristoph Hellwig 	if (nfserr)
22409cf514ccSChristoph Hellwig 		goto out;
22419cf514ccSChristoph Hellwig 
22429cf514ccSChristoph Hellwig 	nfserr = nfserr_layoutunavailable;
22439cf514ccSChristoph Hellwig 	ops = nfsd4_layout_verify(current_fh->fh_export, lgp->lg_layout_type);
22449cf514ccSChristoph Hellwig 	if (!ops)
22459cf514ccSChristoph Hellwig 		goto out;
22469cf514ccSChristoph Hellwig 
22479cf514ccSChristoph Hellwig 	/*
22489cf514ccSChristoph Hellwig 	 * Verify minlength and range as per RFC5661:
22499cf514ccSChristoph Hellwig 	 *  o  If loga_length is less than loga_minlength,
22509cf514ccSChristoph Hellwig 	 *     the metadata server MUST return NFS4ERR_INVAL.
22519cf514ccSChristoph Hellwig 	 *  o  If the sum of loga_offset and loga_minlength exceeds
22529cf514ccSChristoph Hellwig 	 *     NFS4_UINT64_MAX, and loga_minlength is not
22539cf514ccSChristoph Hellwig 	 *     NFS4_UINT64_MAX, the error NFS4ERR_INVAL MUST result.
22549cf514ccSChristoph Hellwig 	 *  o  If the sum of loga_offset and loga_length exceeds
22559cf514ccSChristoph Hellwig 	 *     NFS4_UINT64_MAX, and loga_length is not NFS4_UINT64_MAX,
22569cf514ccSChristoph Hellwig 	 *     the error NFS4ERR_INVAL MUST result.
22579cf514ccSChristoph Hellwig 	 */
22589cf514ccSChristoph Hellwig 	nfserr = nfserr_inval;
22599cf514ccSChristoph Hellwig 	if (lgp->lg_seg.length < lgp->lg_minlength ||
22609cf514ccSChristoph Hellwig 	    (lgp->lg_minlength != NFS4_MAX_UINT64 &&
22619cf514ccSChristoph Hellwig 	     lgp->lg_minlength > NFS4_MAX_UINT64 - lgp->lg_seg.offset) ||
22629cf514ccSChristoph Hellwig 	    (lgp->lg_seg.length != NFS4_MAX_UINT64 &&
22639cf514ccSChristoph Hellwig 	     lgp->lg_seg.length > NFS4_MAX_UINT64 - lgp->lg_seg.offset))
22649cf514ccSChristoph Hellwig 		goto out;
22659cf514ccSChristoph Hellwig 	if (lgp->lg_seg.length == 0)
22669cf514ccSChristoph Hellwig 		goto out;
22679cf514ccSChristoph Hellwig 
22689cf514ccSChristoph Hellwig 	nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lgp->lg_sid,
22699cf514ccSChristoph Hellwig 						true, lgp->lg_layout_type, &ls);
227031ef83dcSChristoph Hellwig 	if (nfserr) {
2271f394b62bSChuck Lever 		trace_nfsd_layout_get_lookup_fail(&lgp->lg_sid);
22729cf514ccSChristoph Hellwig 		goto out;
227331ef83dcSChristoph Hellwig 	}
22749cf514ccSChristoph Hellwig 
2275c5c707f9SChristoph Hellwig 	nfserr = nfserr_recallconflict;
2276c5c707f9SChristoph Hellwig 	if (atomic_read(&ls->ls_stid.sc_file->fi_lo_recalls))
2277c5c707f9SChristoph Hellwig 		goto out_put_stid;
2278c5c707f9SChristoph Hellwig 
22792b0143b5SDavid Howells 	nfserr = ops->proc_layoutget(d_inode(current_fh->fh_dentry),
22809cf514ccSChristoph Hellwig 				     current_fh, lgp);
22819cf514ccSChristoph Hellwig 	if (nfserr)
22829cf514ccSChristoph Hellwig 		goto out_put_stid;
22839cf514ccSChristoph Hellwig 
22849cf514ccSChristoph Hellwig 	nfserr = nfsd4_insert_layout(lgp, ls);
22859cf514ccSChristoph Hellwig 
22869cf514ccSChristoph Hellwig out_put_stid:
2287cc8a5532SJeff Layton 	mutex_unlock(&ls->ls_mutex);
22889cf514ccSChristoph Hellwig 	nfs4_put_stid(&ls->ls_stid);
22899cf514ccSChristoph Hellwig out:
22909cf514ccSChristoph Hellwig 	return nfserr;
22919cf514ccSChristoph Hellwig }
22929cf514ccSChristoph Hellwig 
229334b1744cSJ. Bruce Fields static void
nfsd4_layoutget_release(union nfsd4_op_u * u)229434b1744cSJ. Bruce Fields nfsd4_layoutget_release(union nfsd4_op_u *u)
229534b1744cSJ. Bruce Fields {
229634b1744cSJ. Bruce Fields 	kfree(u->layoutget.lg_content);
229734b1744cSJ. Bruce Fields }
229834b1744cSJ. Bruce Fields 
22999cf514ccSChristoph Hellwig static __be32
nfsd4_layoutcommit(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)23009cf514ccSChristoph Hellwig nfsd4_layoutcommit(struct svc_rqst *rqstp,
2301eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
23029cf514ccSChristoph Hellwig {
2303eb69853dSChristoph Hellwig 	struct nfsd4_layoutcommit *lcp = &u->layoutcommit;
23049cf514ccSChristoph Hellwig 	const struct nfsd4_layout_seg *seg = &lcp->lc_seg;
23059cf514ccSChristoph Hellwig 	struct svc_fh *current_fh = &cstate->current_fh;
23069cf514ccSChristoph Hellwig 	const struct nfsd4_layout_ops *ops;
23079cf514ccSChristoph Hellwig 	loff_t new_size = lcp->lc_last_wr + 1;
23089cf514ccSChristoph Hellwig 	struct inode *inode;
23099cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls;
23109cf514ccSChristoph Hellwig 	__be32 nfserr;
23119cf514ccSChristoph Hellwig 
2312b047898aSChuck Lever 	nfserr = fh_verify(rqstp, current_fh, 0,
2313b047898aSChuck Lever 			   NFSD_MAY_WRITE | NFSD_MAY_OWNER_OVERRIDE);
23149cf514ccSChristoph Hellwig 	if (nfserr)
23159cf514ccSChristoph Hellwig 		goto out;
23169cf514ccSChristoph Hellwig 
23179cf514ccSChristoph Hellwig 	nfserr = nfserr_layoutunavailable;
23189cf514ccSChristoph Hellwig 	ops = nfsd4_layout_verify(current_fh->fh_export, lcp->lc_layout_type);
23199cf514ccSChristoph Hellwig 	if (!ops)
23209cf514ccSChristoph Hellwig 		goto out;
23212b0143b5SDavid Howells 	inode = d_inode(current_fh->fh_dentry);
23229cf514ccSChristoph Hellwig 
23239cf514ccSChristoph Hellwig 	nfserr = nfserr_inval;
23249cf514ccSChristoph Hellwig 	if (new_size <= seg->offset) {
23259cf514ccSChristoph Hellwig 		dprintk("pnfsd: last write before layout segment\n");
23269cf514ccSChristoph Hellwig 		goto out;
23279cf514ccSChristoph Hellwig 	}
23289cf514ccSChristoph Hellwig 	if (new_size > seg->offset + seg->length) {
23299cf514ccSChristoph Hellwig 		dprintk("pnfsd: last write beyond layout segment\n");
23309cf514ccSChristoph Hellwig 		goto out;
23319cf514ccSChristoph Hellwig 	}
23329cf514ccSChristoph Hellwig 	if (!lcp->lc_newoffset && new_size > i_size_read(inode)) {
23339cf514ccSChristoph Hellwig 		dprintk("pnfsd: layoutcommit beyond EOF\n");
23349cf514ccSChristoph Hellwig 		goto out;
23359cf514ccSChristoph Hellwig 	}
23369cf514ccSChristoph Hellwig 
23379cf514ccSChristoph Hellwig 	nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lcp->lc_sid,
23389cf514ccSChristoph Hellwig 						false, lcp->lc_layout_type,
23399cf514ccSChristoph Hellwig 						&ls);
23409cf514ccSChristoph Hellwig 	if (nfserr) {
2341f394b62bSChuck Lever 		trace_nfsd_layout_commit_lookup_fail(&lcp->lc_sid);
23429cf514ccSChristoph Hellwig 		/* fixup error code as per RFC5661 */
23439cf514ccSChristoph Hellwig 		if (nfserr == nfserr_bad_stateid)
23449cf514ccSChristoph Hellwig 			nfserr = nfserr_badlayout;
23459cf514ccSChristoph Hellwig 		goto out;
23469cf514ccSChristoph Hellwig 	}
23479cf514ccSChristoph Hellwig 
2348cc8a5532SJeff Layton 	/* LAYOUTCOMMIT does not require any serialization */
2349cc8a5532SJeff Layton 	mutex_unlock(&ls->ls_mutex);
2350cc8a5532SJeff Layton 
23519cf514ccSChristoph Hellwig 	if (new_size > i_size_read(inode)) {
23529cf514ccSChristoph Hellwig 		lcp->lc_size_chg = 1;
23539cf514ccSChristoph Hellwig 		lcp->lc_newsize = new_size;
23549cf514ccSChristoph Hellwig 	} else {
23559cf514ccSChristoph Hellwig 		lcp->lc_size_chg = 0;
23569cf514ccSChristoph Hellwig 	}
23579cf514ccSChristoph Hellwig 
2358d8398fc1SKinglong Mee 	nfserr = ops->proc_layoutcommit(inode, lcp);
23599cf514ccSChristoph Hellwig 	nfs4_put_stid(&ls->ls_stid);
23609cf514ccSChristoph Hellwig out:
23619cf514ccSChristoph Hellwig 	return nfserr;
23629cf514ccSChristoph Hellwig }
23639cf514ccSChristoph Hellwig 
23649cf514ccSChristoph Hellwig static __be32
nfsd4_layoutreturn(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)23659cf514ccSChristoph Hellwig nfsd4_layoutreturn(struct svc_rqst *rqstp,
2366eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
23679cf514ccSChristoph Hellwig {
2368eb69853dSChristoph Hellwig 	struct nfsd4_layoutreturn *lrp = &u->layoutreturn;
23699cf514ccSChristoph Hellwig 	struct svc_fh *current_fh = &cstate->current_fh;
23709cf514ccSChristoph Hellwig 	__be32 nfserr;
23719cf514ccSChristoph Hellwig 
23729cf514ccSChristoph Hellwig 	nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_NOP);
23739cf514ccSChristoph Hellwig 	if (nfserr)
23749cf514ccSChristoph Hellwig 		goto out;
23759cf514ccSChristoph Hellwig 
23769cf514ccSChristoph Hellwig 	nfserr = nfserr_layoutunavailable;
23779cf514ccSChristoph Hellwig 	if (!nfsd4_layout_verify(current_fh->fh_export, lrp->lr_layout_type))
23789cf514ccSChristoph Hellwig 		goto out;
23799cf514ccSChristoph Hellwig 
23809cf514ccSChristoph Hellwig 	switch (lrp->lr_seg.iomode) {
23819cf514ccSChristoph Hellwig 	case IOMODE_READ:
23829cf514ccSChristoph Hellwig 	case IOMODE_RW:
23839cf514ccSChristoph Hellwig 	case IOMODE_ANY:
23849cf514ccSChristoph Hellwig 		break;
23859cf514ccSChristoph Hellwig 	default:
23869cf514ccSChristoph Hellwig 		dprintk("%s: invalid iomode %d\n", __func__,
23879cf514ccSChristoph Hellwig 			lrp->lr_seg.iomode);
23889cf514ccSChristoph Hellwig 		nfserr = nfserr_inval;
23899cf514ccSChristoph Hellwig 		goto out;
23909cf514ccSChristoph Hellwig 	}
23919cf514ccSChristoph Hellwig 
23929cf514ccSChristoph Hellwig 	switch (lrp->lr_return_type) {
23939cf514ccSChristoph Hellwig 	case RETURN_FILE:
23949cf514ccSChristoph Hellwig 		nfserr = nfsd4_return_file_layouts(rqstp, cstate, lrp);
23959cf514ccSChristoph Hellwig 		break;
23969cf514ccSChristoph Hellwig 	case RETURN_FSID:
23979cf514ccSChristoph Hellwig 	case RETURN_ALL:
23989cf514ccSChristoph Hellwig 		nfserr = nfsd4_return_client_layouts(rqstp, cstate, lrp);
23999cf514ccSChristoph Hellwig 		break;
24009cf514ccSChristoph Hellwig 	default:
24019cf514ccSChristoph Hellwig 		dprintk("%s: invalid return_type %d\n", __func__,
24029cf514ccSChristoph Hellwig 			lrp->lr_return_type);
24039cf514ccSChristoph Hellwig 		nfserr = nfserr_inval;
24049cf514ccSChristoph Hellwig 		break;
24059cf514ccSChristoph Hellwig 	}
24069cf514ccSChristoph Hellwig out:
24079cf514ccSChristoph Hellwig 	return nfserr;
24089cf514ccSChristoph Hellwig }
24099cf514ccSChristoph Hellwig #endif /* CONFIG_NFSD_PNFS */
24109cf514ccSChristoph Hellwig 
241123e50fe3SFrank van der Linden static __be32
nfsd4_getxattr(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)241223e50fe3SFrank van der Linden nfsd4_getxattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
241323e50fe3SFrank van der Linden 	       union nfsd4_op_u *u)
241423e50fe3SFrank van der Linden {
241523e50fe3SFrank van der Linden 	struct nfsd4_getxattr *getxattr = &u->getxattr;
241623e50fe3SFrank van der Linden 
241723e50fe3SFrank van der Linden 	return nfsd_getxattr(rqstp, &cstate->current_fh,
241823e50fe3SFrank van der Linden 			     getxattr->getxa_name, &getxattr->getxa_buf,
241923e50fe3SFrank van der Linden 			     &getxattr->getxa_len);
242023e50fe3SFrank van der Linden }
242123e50fe3SFrank van der Linden 
242223e50fe3SFrank van der Linden static __be32
nfsd4_setxattr(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)242323e50fe3SFrank van der Linden nfsd4_setxattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
242423e50fe3SFrank van der Linden 	   union nfsd4_op_u *u)
242523e50fe3SFrank van der Linden {
242623e50fe3SFrank van der Linden 	struct nfsd4_setxattr *setxattr = &u->setxattr;
242723e50fe3SFrank van der Linden 	__be32 ret;
242823e50fe3SFrank van der Linden 
242923e50fe3SFrank van der Linden 	if (opens_in_grace(SVC_NET(rqstp)))
243023e50fe3SFrank van der Linden 		return nfserr_grace;
243123e50fe3SFrank van der Linden 
243223e50fe3SFrank van der Linden 	ret = nfsd_setxattr(rqstp, &cstate->current_fh, setxattr->setxa_name,
243323e50fe3SFrank van der Linden 			    setxattr->setxa_buf, setxattr->setxa_len,
243423e50fe3SFrank van der Linden 			    setxattr->setxa_flags);
243523e50fe3SFrank van der Linden 
243623e50fe3SFrank van der Linden 	if (!ret)
243723e50fe3SFrank van der Linden 		set_change_info(&setxattr->setxa_cinfo, &cstate->current_fh);
243823e50fe3SFrank van der Linden 
243923e50fe3SFrank van der Linden 	return ret;
244023e50fe3SFrank van der Linden }
244123e50fe3SFrank van der Linden 
244223e50fe3SFrank van der Linden static __be32
nfsd4_listxattrs(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)244323e50fe3SFrank van der Linden nfsd4_listxattrs(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
244423e50fe3SFrank van der Linden 	   union nfsd4_op_u *u)
244523e50fe3SFrank van der Linden {
244623e50fe3SFrank van der Linden 	/*
244723e50fe3SFrank van der Linden 	 * Get the entire list, then copy out only the user attributes
244823e50fe3SFrank van der Linden 	 * in the encode function.
244923e50fe3SFrank van der Linden 	 */
245023e50fe3SFrank van der Linden 	return nfsd_listxattr(rqstp, &cstate->current_fh,
245123e50fe3SFrank van der Linden 			     &u->listxattrs.lsxa_buf, &u->listxattrs.lsxa_len);
245223e50fe3SFrank van der Linden }
245323e50fe3SFrank van der Linden 
245423e50fe3SFrank van der Linden static __be32
nfsd4_removexattr(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)245523e50fe3SFrank van der Linden nfsd4_removexattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
245623e50fe3SFrank van der Linden 	   union nfsd4_op_u *u)
245723e50fe3SFrank van der Linden {
245823e50fe3SFrank van der Linden 	struct nfsd4_removexattr *removexattr = &u->removexattr;
245923e50fe3SFrank van der Linden 	__be32 ret;
246023e50fe3SFrank van der Linden 
246123e50fe3SFrank van der Linden 	if (opens_in_grace(SVC_NET(rqstp)))
246223e50fe3SFrank van der Linden 		return nfserr_grace;
246323e50fe3SFrank van der Linden 
246423e50fe3SFrank van der Linden 	ret = nfsd_removexattr(rqstp, &cstate->current_fh,
246523e50fe3SFrank van der Linden 	    removexattr->rmxa_name);
246623e50fe3SFrank van der Linden 
246723e50fe3SFrank van der Linden 	if (!ret)
246823e50fe3SFrank van der Linden 		set_change_info(&removexattr->rmxa_cinfo, &cstate->current_fh);
246923e50fe3SFrank van der Linden 
247023e50fe3SFrank van der Linden 	return ret;
247123e50fe3SFrank van der Linden }
247223e50fe3SFrank van der Linden 
24731da177e4SLinus Torvalds /*
24741da177e4SLinus Torvalds  * NULL call.
24751da177e4SLinus Torvalds  */
24767111c66eSAl Viro static __be32
nfsd4_proc_null(struct svc_rqst * rqstp)2477a6beb732SChristoph Hellwig nfsd4_proc_null(struct svc_rqst *rqstp)
24781da177e4SLinus Torvalds {
2479cc028a10SChuck Lever 	return rpc_success;
24801da177e4SLinus Torvalds }
24811da177e4SLinus Torvalds 
nfsd4_increment_op_stats(struct nfsd_net * nn,u32 opnum)2482b670a598SJosef Bacik static inline void nfsd4_increment_op_stats(struct nfsd_net *nn, u32 opnum)
2483e2b20950SShankar Anand {
2484e2b20950SShankar Anand 	if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
2485b670a598SJosef Bacik 		percpu_counter_inc(&nn->counter[NFSD_STATS_NFS4_OP(opnum)]);
2486e2b20950SShankar Anand }
2487e2b20950SShankar Anand 
2488bb2a8b0cSChristoph Hellwig static const struct nfsd4_operation nfsd4_ops[];
2489b591480bSJ.Bruce Fields 
2490f1c7f79bSAdrian Bunk static const char *nfsd4_op_name(unsigned opnum);
2491b001a1b6SBenny Halevy 
24921da177e4SLinus Torvalds /*
249357716355SJ. Bruce Fields  * Enforce NFSv4.1 COMPOUND ordering rules:
2494f9bb94c4SAndy Adamson  *
249557716355SJ. Bruce Fields  * Also note, enforced elsewhere:
249657716355SJ. Bruce Fields  *	- SEQUENCE other than as first op results in
249757716355SJ. Bruce Fields  *	  NFS4ERR_SEQUENCE_POS. (Enforced in nfsd4_sequence().)
24981d1bc8f2SJ. Bruce Fields  *	- BIND_CONN_TO_SESSION must be the only op in its compound.
24991d1bc8f2SJ. Bruce Fields  *	  (Enforced in nfsd4_bind_conn_to_session().)
250057716355SJ. Bruce Fields  *	- DESTROY_SESSION must be the final operation in a compound, if
250157716355SJ. Bruce Fields  *	  sessionid's in SEQUENCE and DESTROY_SESSION are the same.
250257716355SJ. Bruce Fields  *	  (Enforced in nfsd4_destroy_session().)
2503f9bb94c4SAndy Adamson  */
nfs41_check_op_ordering(struct nfsd4_compoundargs * args)250457716355SJ. Bruce Fields static __be32 nfs41_check_op_ordering(struct nfsd4_compoundargs *args)
2505f9bb94c4SAndy Adamson {
25067a04cfdaSJ. Bruce Fields 	struct nfsd4_op *first_op = &args->ops[0];
250757716355SJ. Bruce Fields 
250857716355SJ. Bruce Fields 	/* These ordering requirements don't apply to NFSv4.0: */
250957716355SJ. Bruce Fields 	if (args->minorversion == 0)
251057716355SJ. Bruce Fields 		return nfs_ok;
251157716355SJ. Bruce Fields 	/* This is weird, but OK, not our problem: */
251257716355SJ. Bruce Fields 	if (args->opcnt == 0)
251357716355SJ. Bruce Fields 		return nfs_ok;
25147a04cfdaSJ. Bruce Fields 	if (first_op->status == nfserr_op_illegal)
251557716355SJ. Bruce Fields 		return nfs_ok;
25167a04cfdaSJ. Bruce Fields 	if (!(nfsd4_ops[first_op->opnum].op_flags & ALLOWED_AS_FIRST_OP))
251757716355SJ. Bruce Fields 		return nfserr_op_not_in_session;
25187a04cfdaSJ. Bruce Fields 	if (first_op->opnum == OP_SEQUENCE)
251957716355SJ. Bruce Fields 		return nfs_ok;
25207a04cfdaSJ. Bruce Fields 	/*
25217a04cfdaSJ. Bruce Fields 	 * So first_op is something allowed outside a session, like
25227a04cfdaSJ. Bruce Fields 	 * EXCHANGE_ID; but then it has to be the only op in the
25237a04cfdaSJ. Bruce Fields 	 * compound:
25247a04cfdaSJ. Bruce Fields 	 */
252557716355SJ. Bruce Fields 	if (args->opcnt != 1)
252657716355SJ. Bruce Fields 		return nfserr_not_only_op;
252757716355SJ. Bruce Fields 	return nfs_ok;
2528f9bb94c4SAndy Adamson }
2529f9bb94c4SAndy Adamson 
OPDESC(struct nfsd4_op * op)2530f4f9ef4aSJ. Bruce Fields const struct nfsd4_operation *OPDESC(struct nfsd4_op *op)
253122b03214SJ. Bruce Fields {
253222b03214SJ. Bruce Fields 	return &nfsd4_ops[op->opnum];
253322b03214SJ. Bruce Fields }
253422b03214SJ. Bruce Fields 
nfsd4_cache_this_op(struct nfsd4_op * op)25351091006cSJ. Bruce Fields bool nfsd4_cache_this_op(struct nfsd4_op *op)
25361091006cSJ. Bruce Fields {
2537e372ba60SJ. Bruce Fields 	if (op->opnum == OP_ILLEGAL)
2538e372ba60SJ. Bruce Fields 		return false;
2539c856694eSJ. Bruce Fields 	return OPDESC(op)->op_flags & OP_CACHEME;
25401091006cSJ. Bruce Fields }
25411091006cSJ. Bruce Fields 
need_wrongsec_check(struct svc_rqst * rqstp)254268d93184SJ. Bruce Fields static bool need_wrongsec_check(struct svc_rqst *rqstp)
254368d93184SJ. Bruce Fields {
254468d93184SJ. Bruce Fields 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
254568d93184SJ. Bruce Fields 	struct nfsd4_compoundargs *argp = rqstp->rq_argp;
254668d93184SJ. Bruce Fields 	struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
254768d93184SJ. Bruce Fields 	struct nfsd4_op *next = &argp->ops[resp->opcnt];
2548bb2a8b0cSChristoph Hellwig 	const struct nfsd4_operation *thisd = OPDESC(this);
2549bb2a8b0cSChristoph Hellwig 	const struct nfsd4_operation *nextd;
255068d93184SJ. Bruce Fields 
255168d93184SJ. Bruce Fields 	/*
255268d93184SJ. Bruce Fields 	 * Most ops check wronsec on our own; only the putfh-like ops
255368d93184SJ. Bruce Fields 	 * have special rules.
255468d93184SJ. Bruce Fields 	 */
255568d93184SJ. Bruce Fields 	if (!(thisd->op_flags & OP_IS_PUTFH_LIKE))
255668d93184SJ. Bruce Fields 		return false;
255768d93184SJ. Bruce Fields 	/*
255868d93184SJ. Bruce Fields 	 * rfc 5661 2.6.3.1.1.6: don't bother erroring out a
255968d93184SJ. Bruce Fields 	 * put-filehandle operation if we're not going to use the
256068d93184SJ. Bruce Fields 	 * result:
256168d93184SJ. Bruce Fields 	 */
256268d93184SJ. Bruce Fields 	if (argp->opcnt == resp->opcnt)
256368d93184SJ. Bruce Fields 		return false;
256451904b08SJ. Bruce Fields 	if (next->opnum == OP_ILLEGAL)
256551904b08SJ. Bruce Fields 		return false;
256668d93184SJ. Bruce Fields 	nextd = OPDESC(next);
256768d93184SJ. Bruce Fields 	/*
256868d93184SJ. Bruce Fields 	 * Rest of 2.6.3.1.1: certain operations will return WRONGSEC
256968d93184SJ. Bruce Fields 	 * errors themselves as necessary; others should check for them
257068d93184SJ. Bruce Fields 	 * now:
257168d93184SJ. Bruce Fields 	 */
257268d93184SJ. Bruce Fields 	return !(nextd->op_flags & OP_HANDLES_WRONGSEC);
257368d93184SJ. Bruce Fields }
257468d93184SJ. Bruce Fields 
2575b9e8638eSOlga Kornievskaia #ifdef CONFIG_NFSD_V4_2_INTER_SSC
2576b9e8638eSOlga Kornievskaia static void
check_if_stalefh_allowed(struct nfsd4_compoundargs * args)2577b9e8638eSOlga Kornievskaia check_if_stalefh_allowed(struct nfsd4_compoundargs *args)
2578b9e8638eSOlga Kornievskaia {
2579b9e8638eSOlga Kornievskaia 	struct nfsd4_op	*op, *current_op = NULL, *saved_op = NULL;
2580b9e8638eSOlga Kornievskaia 	struct nfsd4_copy *copy;
2581b9e8638eSOlga Kornievskaia 	struct nfsd4_putfh *putfh;
2582b9e8638eSOlga Kornievskaia 	int i;
2583b9e8638eSOlga Kornievskaia 
2584b9e8638eSOlga Kornievskaia 	/* traverse all operation and if it's a COPY compound, mark the
2585b9e8638eSOlga Kornievskaia 	 * source filehandle to skip verification
2586b9e8638eSOlga Kornievskaia 	 */
2587b9e8638eSOlga Kornievskaia 	for (i = 0; i < args->opcnt; i++) {
2588b9e8638eSOlga Kornievskaia 		op = &args->ops[i];
2589b9e8638eSOlga Kornievskaia 		if (op->opnum == OP_PUTFH)
2590b9e8638eSOlga Kornievskaia 			current_op = op;
2591b9e8638eSOlga Kornievskaia 		else if (op->opnum == OP_SAVEFH)
2592b9e8638eSOlga Kornievskaia 			saved_op = current_op;
2593b9e8638eSOlga Kornievskaia 		else if (op->opnum == OP_RESTOREFH)
2594b9e8638eSOlga Kornievskaia 			current_op = saved_op;
2595b9e8638eSOlga Kornievskaia 		else if (op->opnum == OP_COPY) {
2596b9e8638eSOlga Kornievskaia 			copy = (struct nfsd4_copy *)&op->u;
2597b9e8638eSOlga Kornievskaia 			if (!saved_op) {
2598b9e8638eSOlga Kornievskaia 				op->status = nfserr_nofilehandle;
2599b9e8638eSOlga Kornievskaia 				return;
2600b9e8638eSOlga Kornievskaia 			}
2601b9e8638eSOlga Kornievskaia 			putfh = (struct nfsd4_putfh *)&saved_op->u;
26021913cdf5SChuck Lever 			if (nfsd4_ssc_is_inter(copy))
2603b9e8638eSOlga Kornievskaia 				putfh->no_verify = true;
2604b9e8638eSOlga Kornievskaia 		}
2605b9e8638eSOlga Kornievskaia 	}
2606b9e8638eSOlga Kornievskaia }
2607b9e8638eSOlga Kornievskaia #else
2608b9e8638eSOlga Kornievskaia static void
check_if_stalefh_allowed(struct nfsd4_compoundargs * args)2609b9e8638eSOlga Kornievskaia check_if_stalefh_allowed(struct nfsd4_compoundargs *args)
2610b9e8638eSOlga Kornievskaia {
2611b9e8638eSOlga Kornievskaia }
2612b9e8638eSOlga Kornievskaia #endif
2613b9e8638eSOlga Kornievskaia 
2614f9bb94c4SAndy Adamson /*
26151da177e4SLinus Torvalds  * COMPOUND call.
26161da177e4SLinus Torvalds  */
26177111c66eSAl Viro static __be32
nfsd4_proc_compound(struct svc_rqst * rqstp)2618a6beb732SChristoph Hellwig nfsd4_proc_compound(struct svc_rqst *rqstp)
26191da177e4SLinus Torvalds {
2620a6beb732SChristoph Hellwig 	struct nfsd4_compoundargs *args = rqstp->rq_argp;
2621a6beb732SChristoph Hellwig 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
26221da177e4SLinus Torvalds 	struct nfsd4_op	*op;
2623e354d571SAndy Adamson 	struct nfsd4_compound_state *cstate = &resp->cstate;
26244daeed25SKinglong Mee 	struct svc_fh *current_fh = &cstate->current_fh;
26254daeed25SKinglong Mee 	struct svc_fh *save_fh = &cstate->save_fh;
2626e333f3bbSTrond Myklebust 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2627b37ad28bSAl Viro 	__be32		status;
26281da177e4SLinus Torvalds 
2629bddfdbcdSChuck Lever 	resp->xdr = &rqstp->rq_res_stream;
26303b0ebb25SChuck Lever 	resp->statusp = resp->xdr->p;
2631bddfdbcdSChuck Lever 
2632bddfdbcdSChuck Lever 	/* reserve space for: NFS status code */
2633bddfdbcdSChuck Lever 	xdr_reserve_space(resp->xdr, XDR_UNIT);
2634bddfdbcdSChuck Lever 
26351da177e4SLinus Torvalds 	/* reserve space for: taglen, tag, and opcnt */
2636bddfdbcdSChuck Lever 	xdr_reserve_space(resp->xdr, XDR_UNIT * 2 + args->taglen);
26371da177e4SLinus Torvalds 	resp->taglen = args->taglen;
26381da177e4SLinus Torvalds 	resp->tag = args->tag;
26391da177e4SLinus Torvalds 	resp->rqstp = rqstp;
26404daeed25SKinglong Mee 	cstate->minorversion = args->minorversion;
26414daeed25SKinglong Mee 	fh_init(current_fh, NFS4_FHSIZE);
26424daeed25SKinglong Mee 	fh_init(save_fh, NFS4_FHSIZE);
26438ff30fa4SNeilBrown 	/*
26448ff30fa4SNeilBrown 	 * Don't use the deferral mechanism for NFSv4; compounds make it
26458ff30fa4SNeilBrown 	 * too hard to avoid non-idempotency problems.
26468ff30fa4SNeilBrown 	 */
26477827c81fSChuck Lever 	clear_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
26481da177e4SLinus Torvalds 
26491da177e4SLinus Torvalds 	/*
26501da177e4SLinus Torvalds 	 * According to RFC3010, this takes precedence over all other errors.
26511da177e4SLinus Torvalds 	 */
26521da177e4SLinus Torvalds 	status = nfserr_minor_vers_mismatch;
2653e333f3bbSTrond Myklebust 	if (nfsd_minorversion(nn, args->minorversion, NFSD_TEST) <= 0)
26541da177e4SLinus Torvalds 		goto out;
26551da177e4SLinus Torvalds 
265657716355SJ. Bruce Fields 	status = nfs41_check_op_ordering(args);
265757716355SJ. Bruce Fields 	if (status) {
2658f9bb94c4SAndy Adamson 		op = &args->ops[0];
265957716355SJ. Bruce Fields 		op->status = status;
26605b7b15aeSJ. Bruce Fields 		resp->opcnt = 1;
2661f9bb94c4SAndy Adamson 		goto encode_op;
2662f9bb94c4SAndy Adamson 	}
2663b9e8638eSOlga Kornievskaia 	check_if_stalefh_allowed(args);
2664f9bb94c4SAndy Adamson 
266528df3d15SJ. Bruce Fields 	rqstp->rq_lease_breaker = (void **)&cstate->clp;
266628df3d15SJ. Bruce Fields 
2667de29cf7eSChuck Lever 	trace_nfsd_compound(rqstp, args->tag, args->taglen, args->client_opcnt);
26681da177e4SLinus Torvalds 	while (!status && resp->opcnt < args->opcnt) {
26691da177e4SLinus Torvalds 		op = &args->ops[resp->opcnt++];
26701da177e4SLinus Torvalds 
26717518a3dcSChuck Lever 		if (unlikely(resp->opcnt == NFSD_MAX_OPS_PER_COMPOUND)) {
26727518a3dcSChuck Lever 			/* If there are still more operations to process,
26737518a3dcSChuck Lever 			 * stop here and report NFS4ERR_RESOURCE. */
26747518a3dcSChuck Lever 			if (cstate->minorversion == 0 &&
26757518a3dcSChuck Lever 			    args->client_opcnt > resp->opcnt) {
26767518a3dcSChuck Lever 				op->status = nfserr_resource;
26777518a3dcSChuck Lever 				goto encode_op;
26787518a3dcSChuck Lever 			}
26797518a3dcSChuck Lever 		}
26807518a3dcSChuck Lever 
26811da177e4SLinus Torvalds 		/*
26821da177e4SLinus Torvalds 		 * The XDR decode routines may have pre-set op->status;
26831da177e4SLinus Torvalds 		 * for example, if there is a miscellaneous XDR error
26841da177e4SLinus Torvalds 		 * it will be set to nfserr_bad_xdr.
26851da177e4SLinus Torvalds 		 */
26869d313b17SJ. Bruce Fields 		if (op->status) {
26879d313b17SJ. Bruce Fields 			if (op->opnum == OP_OPEN)
26889d313b17SJ. Bruce Fields 				op->status = nfsd4_open_omfg(rqstp, cstate, op);
26891da177e4SLinus Torvalds 			goto encode_op;
26909d313b17SJ. Bruce Fields 		}
2691b9e8638eSOlga Kornievskaia 		if (!current_fh->fh_dentry &&
2692b9e8638eSOlga Kornievskaia 				!HAS_FH_FLAG(current_fh, NFSD4_FH_FOREIGN)) {
2693f4f9ef4aSJ. Bruce Fields 			if (!(op->opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
26941da177e4SLinus Torvalds 				op->status = nfserr_nofilehandle;
26951da177e4SLinus Torvalds 				goto encode_op;
26961da177e4SLinus Torvalds 			}
2697b9e8638eSOlga Kornievskaia 		} else if (current_fh->fh_export &&
2698b9e8638eSOlga Kornievskaia 			   current_fh->fh_export->ex_fslocs.migrated &&
2699f4f9ef4aSJ. Bruce Fields 			  !(op->opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
270042ca0993SJ.Bruce Fields 			op->status = nfserr_moved;
270142ca0993SJ.Bruce Fields 			goto encode_op;
270242ca0993SJ.Bruce Fields 		}
2703b591480bSJ.Bruce Fields 
2704fcb5e3faSChuck Lever 		fh_clear_pre_post_attrs(current_fh);
27052336745eSKinglong Mee 
270658e7b33aSMi Jinlong 		/* If op is non-idempotent */
2707f4f9ef4aSJ. Bruce Fields 		if (op->opdesc->op_flags & OP_MODIFIES_SOMETHING) {
27084c69d585SJ. Bruce Fields 			/*
2709a8095f7eSJ. Bruce Fields 			 * Don't execute this op if we couldn't encode a
2710f532c9ffSZhang Jiaming 			 * successful reply:
2711a8095f7eSJ. Bruce Fields 			 */
2712f4f9ef4aSJ. Bruce Fields 			u32 plen = op->opdesc->op_rsize_bop(rqstp, op);
2713a8095f7eSJ. Bruce Fields 			/*
2714a8095f7eSJ. Bruce Fields 			 * Plus if there's another operation, make sure
27154c69d585SJ. Bruce Fields 			 * we'll have space to at least encode an error:
27164c69d585SJ. Bruce Fields 			 */
27174c69d585SJ. Bruce Fields 			if (resp->opcnt < args->opcnt)
27184c69d585SJ. Bruce Fields 				plen += COMPOUND_ERR_SLACK_SPACE;
271958e7b33aSMi Jinlong 			op->status = nfsd4_check_resp_size(resp, plen);
272058e7b33aSMi Jinlong 		}
272158e7b33aSMi Jinlong 
272258e7b33aSMi Jinlong 		if (op->status)
272358e7b33aSMi Jinlong 			goto encode_op;
272458e7b33aSMi Jinlong 
2725f4f9ef4aSJ. Bruce Fields 		if (op->opdesc->op_get_currentstateid)
2726f4f9ef4aSJ. Bruce Fields 			op->opdesc->op_get_currentstateid(cstate, &op->u);
2727f4f9ef4aSJ. Bruce Fields 		op->status = op->opdesc->op_func(rqstp, cstate, &op->u);
27281da177e4SLinus Torvalds 
27299a307403SJ. Bruce Fields 		/* Only from SEQUENCE */
27309a307403SJ. Bruce Fields 		if (cstate->status == nfserr_replay_cache) {
27319a307403SJ. Bruce Fields 			dprintk("%s NFS4.1 replay from cache\n", __func__);
27329a307403SJ. Bruce Fields 			status = op->status;
27339a307403SJ. Bruce Fields 			goto out;
27349a307403SJ. Bruce Fields 		}
27358b70484cSTigran Mkrtchyan 		if (!op->status) {
2736f4f9ef4aSJ. Bruce Fields 			if (op->opdesc->op_set_currentstateid)
2737f4f9ef4aSJ. Bruce Fields 				op->opdesc->op_set_currentstateid(cstate, &op->u);
27388b70484cSTigran Mkrtchyan 
2739f4f9ef4aSJ. Bruce Fields 			if (op->opdesc->op_flags & OP_CLEAR_STATEID)
274037c593c5STigran Mkrtchyan 				clear_current_stateid(cstate);
27418b70484cSTigran Mkrtchyan 
2742d781e3dfSJ. Bruce Fields 			if (current_fh->fh_export &&
2743d781e3dfSJ. Bruce Fields 					need_wrongsec_check(rqstp))
27444daeed25SKinglong Mee 				op->status = check_nfsd_access(current_fh->fh_export, rqstp);
27458b70484cSTigran Mkrtchyan 		}
27461da177e4SLinus Torvalds encode_op:
2747a90b061cSAl Viro 		if (op->status == nfserr_replay_me) {
2748a4f1706aSJ.Bruce Fields 			op->replay = &cstate->replay_owner->so_replay;
2749bddfdbcdSChuck Lever 			nfsd4_encode_replay(resp->xdr, op);
27501da177e4SLinus Torvalds 			status = op->status = op->replay->rp_status;
27511da177e4SLinus Torvalds 		} else {
27521da177e4SLinus Torvalds 			nfsd4_encode_operation(resp, op);
27531da177e4SLinus Torvalds 			status = op->status;
27541da177e4SLinus Torvalds 		}
27550407717dSBenny Halevy 
27567518a3dcSChuck Lever 		trace_nfsd_compound_status(args->client_opcnt, resp->opcnt,
27577518a3dcSChuck Lever 					   status, nfsd4_op_name(op->opnum));
27580407717dSBenny Halevy 
275958fb12e6SJeff Layton 		nfsd4_cstate_clear_replay(cstate);
2760b670a598SJosef Bacik 		nfsd4_increment_op_stats(nn, op->opnum);
27611da177e4SLinus Torvalds 	}
27621da177e4SLinus Torvalds 
27634daeed25SKinglong Mee 	fh_put(current_fh);
27644daeed25SKinglong Mee 	fh_put(save_fh);
27654daeed25SKinglong Mee 	BUG_ON(cstate->replay_owner);
27661da177e4SLinus Torvalds out:
2767cc028a10SChuck Lever 	cstate->status = status;
27682f425878SAndy Adamson 	/* Reset deferral mechanism for RPC deferrals */
27697827c81fSChuck Lever 	set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
2770cc028a10SChuck Lever 	return rpc_success;
27711da177e4SLinus Torvalds }
27721da177e4SLinus Torvalds 
277358e7b33aSMi Jinlong #define op_encode_hdr_size		(2)
277458e7b33aSMi Jinlong #define op_encode_stateid_maxsz		(XDR_QUADLEN(NFS4_STATEID_SIZE))
277558e7b33aSMi Jinlong #define op_encode_verifier_maxsz	(XDR_QUADLEN(NFS4_VERIFIER_SIZE))
277658e7b33aSMi Jinlong #define op_encode_change_info_maxsz	(5)
277758e7b33aSMi Jinlong #define nfs4_fattr_bitmap_maxsz		(4)
277858e7b33aSMi Jinlong 
27798c7424cfSJ. Bruce Fields /* We'll fall back on returning no lockowner if run out of space: */
27808c7424cfSJ. Bruce Fields #define op_encode_lockowner_maxsz	(0)
278158e7b33aSMi Jinlong #define op_encode_lock_denied_maxsz	(8 + op_encode_lockowner_maxsz)
278258e7b33aSMi Jinlong 
278358e7b33aSMi Jinlong #define nfs4_owner_maxsz		(1 + XDR_QUADLEN(IDMAP_NAMESZ))
278458e7b33aSMi Jinlong 
278558e7b33aSMi Jinlong #define op_encode_ace_maxsz		(3 + nfs4_owner_maxsz)
278658e7b33aSMi Jinlong #define op_encode_delegation_maxsz	(1 + op_encode_stateid_maxsz + 1 + \
278758e7b33aSMi Jinlong 					 op_encode_ace_maxsz)
278858e7b33aSMi Jinlong 
278958e7b33aSMi Jinlong #define op_encode_channel_attrs_maxsz	(6 + 1 + 1)
279058e7b33aSMi Jinlong 
279176ce4dceSChuck Lever /*
279276ce4dceSChuck Lever  * The _rsize() helpers are invoked by the NFSv4 COMPOUND decoder, which
279376ce4dceSChuck Lever  * is called before sunrpc sets rq_res.buflen. Thus we have to compute
279476ce4dceSChuck Lever  * the maximum payload size here, based on transport limits and the size
279576ce4dceSChuck Lever  * of the remaining space in the rq_pages array.
279676ce4dceSChuck Lever  */
nfsd4_max_payload(const struct svc_rqst * rqstp)279776ce4dceSChuck Lever static u32 nfsd4_max_payload(const struct svc_rqst *rqstp)
279876ce4dceSChuck Lever {
279976ce4dceSChuck Lever 	u32 buflen;
280076ce4dceSChuck Lever 
280176ce4dceSChuck Lever 	buflen = (rqstp->rq_page_end - rqstp->rq_next_page) * PAGE_SIZE;
280276ce4dceSChuck Lever 	buflen -= rqstp->rq_auth_slack;
280376ce4dceSChuck Lever 	buflen -= rqstp->rq_res.head[0].iov_len;
280476ce4dceSChuck Lever 	return min_t(u32, buflen, svc_max_payload(rqstp));
280576ce4dceSChuck Lever }
280676ce4dceSChuck Lever 
nfsd4_only_status_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)28076604148cSChuck Lever static u32 nfsd4_only_status_rsize(const struct svc_rqst *rqstp,
28086604148cSChuck Lever 				   const struct nfsd4_op *op)
280958e7b33aSMi Jinlong {
281058e7b33aSMi Jinlong 	return (op_encode_hdr_size) * sizeof(__be32);
281158e7b33aSMi Jinlong }
281258e7b33aSMi Jinlong 
nfsd4_status_stateid_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)28136604148cSChuck Lever static u32 nfsd4_status_stateid_rsize(const struct svc_rqst *rqstp,
28146604148cSChuck Lever 				      const struct nfsd4_op *op)
281558e7b33aSMi Jinlong {
281658e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_stateid_maxsz)* sizeof(__be32);
281758e7b33aSMi Jinlong }
281858e7b33aSMi Jinlong 
nfsd4_access_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)28196604148cSChuck Lever static u32 nfsd4_access_rsize(const struct svc_rqst *rqstp,
28206604148cSChuck Lever 			      const struct nfsd4_op *op)
28212282cd2cSKinglong Mee {
28222282cd2cSKinglong Mee 	/* ac_supported, ac_resp_access */
28232282cd2cSKinglong Mee 	return (op_encode_hdr_size + 2)* sizeof(__be32);
28242282cd2cSKinglong Mee }
28252282cd2cSKinglong Mee 
nfsd4_commit_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)28266604148cSChuck Lever static u32 nfsd4_commit_rsize(const struct svc_rqst *rqstp,
28276604148cSChuck Lever 			      const struct nfsd4_op *op)
282858e7b33aSMi Jinlong {
282958e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_verifier_maxsz) * sizeof(__be32);
283058e7b33aSMi Jinlong }
283158e7b33aSMi Jinlong 
nfsd4_create_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)28326604148cSChuck Lever static u32 nfsd4_create_rsize(const struct svc_rqst *rqstp,
28336604148cSChuck Lever 			      const struct nfsd4_op *op)
283458e7b33aSMi Jinlong {
283558e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_change_info_maxsz
283658e7b33aSMi Jinlong 		+ nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
283758e7b33aSMi Jinlong }
283858e7b33aSMi Jinlong 
2839b86cef60SJ. Bruce Fields /*
2840b86cef60SJ. Bruce Fields  * Note since this is an idempotent operation we won't insist on failing
2841b86cef60SJ. Bruce Fields  * the op prematurely if the estimate is too large.  We may turn off splice
2842b86cef60SJ. Bruce Fields  * reads unnecessarily.
2843b86cef60SJ. Bruce Fields  */
nfsd4_getattr_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)28446604148cSChuck Lever static u32 nfsd4_getattr_rsize(const struct svc_rqst *rqstp,
28456604148cSChuck Lever 			       const struct nfsd4_op *op)
2846b86cef60SJ. Bruce Fields {
28476604148cSChuck Lever 	const u32 *bmap = op->u.getattr.ga_bmval;
2848b86cef60SJ. Bruce Fields 	u32 bmap0 = bmap[0], bmap1 = bmap[1], bmap2 = bmap[2];
2849b86cef60SJ. Bruce Fields 	u32 ret = 0;
2850b86cef60SJ. Bruce Fields 
2851b86cef60SJ. Bruce Fields 	if (bmap0 & FATTR4_WORD0_ACL)
285276ce4dceSChuck Lever 		return nfsd4_max_payload(rqstp);
2853b86cef60SJ. Bruce Fields 	if (bmap0 & FATTR4_WORD0_FS_LOCATIONS)
285476ce4dceSChuck Lever 		return nfsd4_max_payload(rqstp);
2855b86cef60SJ. Bruce Fields 
2856b86cef60SJ. Bruce Fields 	if (bmap1 & FATTR4_WORD1_OWNER) {
2857b86cef60SJ. Bruce Fields 		ret += IDMAP_NAMESZ + 4;
2858b86cef60SJ. Bruce Fields 		bmap1 &= ~FATTR4_WORD1_OWNER;
2859b86cef60SJ. Bruce Fields 	}
2860b86cef60SJ. Bruce Fields 	if (bmap1 & FATTR4_WORD1_OWNER_GROUP) {
2861b86cef60SJ. Bruce Fields 		ret += IDMAP_NAMESZ + 4;
2862b86cef60SJ. Bruce Fields 		bmap1 &= ~FATTR4_WORD1_OWNER_GROUP;
2863b86cef60SJ. Bruce Fields 	}
2864b86cef60SJ. Bruce Fields 	if (bmap0 & FATTR4_WORD0_FILEHANDLE) {
2865b86cef60SJ. Bruce Fields 		ret += NFS4_FHSIZE + 4;
2866b86cef60SJ. Bruce Fields 		bmap0 &= ~FATTR4_WORD0_FILEHANDLE;
2867b86cef60SJ. Bruce Fields 	}
2868b86cef60SJ. Bruce Fields 	if (bmap2 & FATTR4_WORD2_SECURITY_LABEL) {
28691ec8c0c4SKinglong Mee 		ret += NFS4_MAXLABELLEN + 12;
2870b86cef60SJ. Bruce Fields 		bmap2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2871b86cef60SJ. Bruce Fields 	}
2872b86cef60SJ. Bruce Fields 	/*
2873b86cef60SJ. Bruce Fields 	 * Largest of remaining attributes are 16 bytes (e.g.,
2874b86cef60SJ. Bruce Fields 	 * supported_attributes)
2875b86cef60SJ. Bruce Fields 	 */
2876b86cef60SJ. Bruce Fields 	ret += 16 * (hweight32(bmap0) + hweight32(bmap1) + hweight32(bmap2));
2877b86cef60SJ. Bruce Fields 	/* bitmask, length */
2878b86cef60SJ. Bruce Fields 	ret += 20;
2879b86cef60SJ. Bruce Fields 	return ret;
2880b86cef60SJ. Bruce Fields }
2881b86cef60SJ. Bruce Fields 
nfsd4_getfh_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)28826604148cSChuck Lever static u32 nfsd4_getfh_rsize(const struct svc_rqst *rqstp,
28836604148cSChuck Lever 			     const struct nfsd4_op *op)
28842282cd2cSKinglong Mee {
28852282cd2cSKinglong Mee 	return (op_encode_hdr_size + 1) * sizeof(__be32) + NFS4_FHSIZE;
28862282cd2cSKinglong Mee }
28872282cd2cSKinglong Mee 
nfsd4_link_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)28886604148cSChuck Lever static u32 nfsd4_link_rsize(const struct svc_rqst *rqstp,
28896604148cSChuck Lever 			    const struct nfsd4_op *op)
289058e7b33aSMi Jinlong {
289158e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
289258e7b33aSMi Jinlong 		* sizeof(__be32);
289358e7b33aSMi Jinlong }
289458e7b33aSMi Jinlong 
nfsd4_lock_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)28956604148cSChuck Lever static u32 nfsd4_lock_rsize(const struct svc_rqst *rqstp,
28966604148cSChuck Lever 			    const struct nfsd4_op *op)
289758e7b33aSMi Jinlong {
289858e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_lock_denied_maxsz)
289958e7b33aSMi Jinlong 		* sizeof(__be32);
290058e7b33aSMi Jinlong }
290158e7b33aSMi Jinlong 
nfsd4_open_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)29026604148cSChuck Lever static u32 nfsd4_open_rsize(const struct svc_rqst *rqstp,
29036604148cSChuck Lever 			    const struct nfsd4_op *op)
290458e7b33aSMi Jinlong {
290558e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_stateid_maxsz
290658e7b33aSMi Jinlong 		+ op_encode_change_info_maxsz + 1
290758e7b33aSMi Jinlong 		+ nfs4_fattr_bitmap_maxsz
290858e7b33aSMi Jinlong 		+ op_encode_delegation_maxsz) * sizeof(__be32);
290958e7b33aSMi Jinlong }
291058e7b33aSMi Jinlong 
nfsd4_read_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)29116604148cSChuck Lever static u32 nfsd4_read_rsize(const struct svc_rqst *rqstp,
29126604148cSChuck Lever 			    const struct nfsd4_op *op)
291358e7b33aSMi Jinlong {
291476ce4dceSChuck Lever 	u32 rlen = min(op->u.read.rd_length, nfsd4_max_payload(rqstp));
291558e7b33aSMi Jinlong 
2916622f560eSJ. Bruce Fields 	return (op_encode_hdr_size + 2 + XDR_QUADLEN(rlen)) * sizeof(__be32);
291758e7b33aSMi Jinlong }
291858e7b33aSMi Jinlong 
nfsd4_read_plus_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)29196604148cSChuck Lever static u32 nfsd4_read_plus_rsize(const struct svc_rqst *rqstp,
29206604148cSChuck Lever 				 const struct nfsd4_op *op)
2921528b8493SAnna Schumaker {
292276ce4dceSChuck Lever 	u32 rlen = min(op->u.read.rd_length, nfsd4_max_payload(rqstp));
2923528b8493SAnna Schumaker 	/*
2924528b8493SAnna Schumaker 	 * If we detect that the file changed during hole encoding, then we
2925528b8493SAnna Schumaker 	 * recover by encoding the remaining reply as data. This means we need
2926528b8493SAnna Schumaker 	 * to set aside enough room to encode two data segments.
2927528b8493SAnna Schumaker 	 */
2928528b8493SAnna Schumaker 	u32 seg_len = 2 * (1 + 2 + 1);
2929528b8493SAnna Schumaker 
2930528b8493SAnna Schumaker 	return (op_encode_hdr_size + 2 + seg_len + XDR_QUADLEN(rlen)) * sizeof(__be32);
2931528b8493SAnna Schumaker }
2932528b8493SAnna Schumaker 
nfsd4_readdir_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)29336604148cSChuck Lever static u32 nfsd4_readdir_rsize(const struct svc_rqst *rqstp,
29346604148cSChuck Lever 			       const struct nfsd4_op *op)
293558e7b33aSMi Jinlong {
293676ce4dceSChuck Lever 	u32 rlen = min(op->u.readdir.rd_maxcount, nfsd4_max_payload(rqstp));
293758e7b33aSMi Jinlong 
2938561f0ed4SJ. Bruce Fields 	return (op_encode_hdr_size + op_encode_verifier_maxsz +
2939561f0ed4SJ. Bruce Fields 		XDR_QUADLEN(rlen)) * sizeof(__be32);
294058e7b33aSMi Jinlong }
294158e7b33aSMi Jinlong 
nfsd4_readlink_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)29426604148cSChuck Lever static u32 nfsd4_readlink_rsize(const struct svc_rqst *rqstp,
29436604148cSChuck Lever 				const struct nfsd4_op *op)
29442282cd2cSKinglong Mee {
29452282cd2cSKinglong Mee 	return (op_encode_hdr_size + 1) * sizeof(__be32) + PAGE_SIZE;
29462282cd2cSKinglong Mee }
29472282cd2cSKinglong Mee 
nfsd4_remove_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)29486604148cSChuck Lever static u32 nfsd4_remove_rsize(const struct svc_rqst *rqstp,
29496604148cSChuck Lever 			      const struct nfsd4_op *op)
295058e7b33aSMi Jinlong {
295158e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
295258e7b33aSMi Jinlong 		* sizeof(__be32);
295358e7b33aSMi Jinlong }
295458e7b33aSMi Jinlong 
nfsd4_rename_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)29556604148cSChuck Lever static u32 nfsd4_rename_rsize(const struct svc_rqst *rqstp,
29566604148cSChuck Lever 			      const struct nfsd4_op *op)
295758e7b33aSMi Jinlong {
295858e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_change_info_maxsz
295958e7b33aSMi Jinlong 		+ op_encode_change_info_maxsz) * sizeof(__be32);
296058e7b33aSMi Jinlong }
296158e7b33aSMi Jinlong 
nfsd4_sequence_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)29626604148cSChuck Lever static u32 nfsd4_sequence_rsize(const struct svc_rqst *rqstp,
29636604148cSChuck Lever 				const struct nfsd4_op *op)
2964ccae70a9SJ. Bruce Fields {
2965d1d84c96SJ. Bruce Fields 	return (op_encode_hdr_size
2966d1d84c96SJ. Bruce Fields 		+ XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 5) * sizeof(__be32);
2967ccae70a9SJ. Bruce Fields }
2968ccae70a9SJ. Bruce Fields 
nfsd4_test_stateid_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)29696604148cSChuck Lever static u32 nfsd4_test_stateid_rsize(const struct svc_rqst *rqstp,
29706604148cSChuck Lever 				    const struct nfsd4_op *op)
29712282cd2cSKinglong Mee {
29722282cd2cSKinglong Mee 	return (op_encode_hdr_size + 1 + op->u.test_stateid.ts_num_ids)
29732282cd2cSKinglong Mee 		* sizeof(__be32);
29742282cd2cSKinglong Mee }
29752282cd2cSKinglong Mee 
nfsd4_setattr_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)29766604148cSChuck Lever static u32 nfsd4_setattr_rsize(const struct svc_rqst *rqstp,
29776604148cSChuck Lever 			       const struct nfsd4_op *op)
297858e7b33aSMi Jinlong {
297958e7b33aSMi Jinlong 	return (op_encode_hdr_size + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
298058e7b33aSMi Jinlong }
298158e7b33aSMi Jinlong 
nfsd4_secinfo_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)29826604148cSChuck Lever static u32 nfsd4_secinfo_rsize(const struct svc_rqst *rqstp,
29836604148cSChuck Lever 			       const struct nfsd4_op *op)
29842282cd2cSKinglong Mee {
29852282cd2cSKinglong Mee 	return (op_encode_hdr_size + RPC_AUTH_MAXFLAVOR *
29862282cd2cSKinglong Mee 		(4 + XDR_QUADLEN(GSS_OID_MAX_LEN))) * sizeof(__be32);
29872282cd2cSKinglong Mee }
29882282cd2cSKinglong Mee 
nfsd4_setclientid_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)29896604148cSChuck Lever static u32 nfsd4_setclientid_rsize(const struct svc_rqst *rqstp,
29906604148cSChuck Lever 				   const struct nfsd4_op *op)
299158e7b33aSMi Jinlong {
2992480efaeeSJ. Bruce Fields 	return (op_encode_hdr_size + 2 + XDR_QUADLEN(NFS4_VERIFIER_SIZE)) *
2993480efaeeSJ. Bruce Fields 								sizeof(__be32);
299458e7b33aSMi Jinlong }
299558e7b33aSMi Jinlong 
nfsd4_write_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)29966604148cSChuck Lever static u32 nfsd4_write_rsize(const struct svc_rqst *rqstp,
29976604148cSChuck Lever 			     const struct nfsd4_op *op)
299858e7b33aSMi Jinlong {
2999f34e432bSJ. Bruce Fields 	return (op_encode_hdr_size + 2 + op_encode_verifier_maxsz) * sizeof(__be32);
300058e7b33aSMi Jinlong }
300158e7b33aSMi Jinlong 
nfsd4_exchange_id_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)30026604148cSChuck Lever static u32 nfsd4_exchange_id_rsize(const struct svc_rqst *rqstp,
30036604148cSChuck Lever 				   const struct nfsd4_op *op)
300458e7b33aSMi Jinlong {
300558e7b33aSMi Jinlong 	return (op_encode_hdr_size + 2 + 1 + /* eir_clientid, eir_sequenceid */\
3006a8bb84bcSKinglong Mee 		1 + 1 + /* eir_flags, spr_how */\
3007a8bb84bcSKinglong Mee 		4 + /* spo_must_enforce & _allow with bitmap */\
300858e7b33aSMi Jinlong 		2 + /*eir_server_owner.so_minor_id */\
300958e7b33aSMi Jinlong 		/* eir_server_owner.so_major_id<> */\
301058e7b33aSMi Jinlong 		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
301158e7b33aSMi Jinlong 		/* eir_server_scope<> */\
301258e7b33aSMi Jinlong 		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
301358e7b33aSMi Jinlong 		1 + /* eir_server_impl_id array length */\
301458e7b33aSMi Jinlong 		0 /* ignored eir_server_impl_id contents */) * sizeof(__be32);
301558e7b33aSMi Jinlong }
301658e7b33aSMi Jinlong 
nfsd4_bind_conn_to_session_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)30176604148cSChuck Lever static u32 nfsd4_bind_conn_to_session_rsize(const struct svc_rqst *rqstp,
30186604148cSChuck Lever 					    const struct nfsd4_op *op)
301958e7b33aSMi Jinlong {
302058e7b33aSMi Jinlong 	return (op_encode_hdr_size + \
302158e7b33aSMi Jinlong 		XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* bctsr_sessid */\
302258e7b33aSMi Jinlong 		2 /* bctsr_dir, use_conn_in_rdma_mode */) * sizeof(__be32);
302358e7b33aSMi Jinlong }
302458e7b33aSMi Jinlong 
nfsd4_create_session_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)30256604148cSChuck Lever static u32 nfsd4_create_session_rsize(const struct svc_rqst *rqstp,
30266604148cSChuck Lever 				      const struct nfsd4_op *op)
302758e7b33aSMi Jinlong {
302858e7b33aSMi Jinlong 	return (op_encode_hdr_size + \
302958e7b33aSMi Jinlong 		XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* sessionid */\
303058e7b33aSMi Jinlong 		2 + /* csr_sequence, csr_flags */\
303158e7b33aSMi Jinlong 		op_encode_channel_attrs_maxsz + \
303258e7b33aSMi Jinlong 		op_encode_channel_attrs_maxsz) * sizeof(__be32);
303358e7b33aSMi Jinlong }
303458e7b33aSMi Jinlong 
nfsd4_copy_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)30356604148cSChuck Lever static u32 nfsd4_copy_rsize(const struct svc_rqst *rqstp,
30366604148cSChuck Lever 			    const struct nfsd4_op *op)
303729ae7f9dSAnna Schumaker {
303829ae7f9dSAnna Schumaker 	return (op_encode_hdr_size +
303929ae7f9dSAnna Schumaker 		1 /* wr_callback */ +
304029ae7f9dSAnna Schumaker 		op_encode_stateid_maxsz /* wr_callback */ +
304129ae7f9dSAnna Schumaker 		2 /* wr_count */ +
304229ae7f9dSAnna Schumaker 		1 /* wr_committed */ +
304329ae7f9dSAnna Schumaker 		op_encode_verifier_maxsz +
304429ae7f9dSAnna Schumaker 		1 /* cr_consecutive */ +
304529ae7f9dSAnna Schumaker 		1 /* cr_synchronous */) * sizeof(__be32);
304629ae7f9dSAnna Schumaker }
304729ae7f9dSAnna Schumaker 
nfsd4_offload_status_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)30486604148cSChuck Lever static u32 nfsd4_offload_status_rsize(const struct svc_rqst *rqstp,
30496604148cSChuck Lever 				      const struct nfsd4_op *op)
30506308bc98SOlga Kornievskaia {
30516308bc98SOlga Kornievskaia 	return (op_encode_hdr_size +
30526308bc98SOlga Kornievskaia 		2 /* osr_count */ +
30536308bc98SOlga Kornievskaia 		1 /* osr_complete<1> optional 0 for now */) * sizeof(__be32);
30546308bc98SOlga Kornievskaia }
30556308bc98SOlga Kornievskaia 
nfsd4_copy_notify_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)30566604148cSChuck Lever static u32 nfsd4_copy_notify_rsize(const struct svc_rqst *rqstp,
30576604148cSChuck Lever 				   const struct nfsd4_op *op)
305851911868SOlga Kornievskaia {
305951911868SOlga Kornievskaia 	return (op_encode_hdr_size +
306051911868SOlga Kornievskaia 		3 /* cnr_lease_time */ +
306151911868SOlga Kornievskaia 		1 /* We support one cnr_source_server */ +
306251911868SOlga Kornievskaia 		1 /* cnr_stateid seq */ +
306351911868SOlga Kornievskaia 		op_encode_stateid_maxsz /* cnr_stateid */ +
306451911868SOlga Kornievskaia 		1 /* num cnr_source_server*/ +
306551911868SOlga Kornievskaia 		1 /* nl4_type */ +
306651911868SOlga Kornievskaia 		1 /* nl4 size */ +
306751911868SOlga Kornievskaia 		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) /*nl4_loc + nl4_loc_sz */)
306851911868SOlga Kornievskaia 		* sizeof(__be32);
306951911868SOlga Kornievskaia }
307051911868SOlga Kornievskaia 
30719cf514ccSChristoph Hellwig #ifdef CONFIG_NFSD_PNFS
nfsd4_getdeviceinfo_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)30726604148cSChuck Lever static u32 nfsd4_getdeviceinfo_rsize(const struct svc_rqst *rqstp,
30736604148cSChuck Lever 				     const struct nfsd4_op *op)
30742282cd2cSKinglong Mee {
307576ce4dceSChuck Lever 	u32 rlen = min(op->u.getdeviceinfo.gd_maxcount, nfsd4_max_payload(rqstp));
30762282cd2cSKinglong Mee 
30772282cd2cSKinglong Mee 	return (op_encode_hdr_size +
30782282cd2cSKinglong Mee 		1 /* gd_layout_type*/ +
30792282cd2cSKinglong Mee 		XDR_QUADLEN(rlen) +
30802282cd2cSKinglong Mee 		2 /* gd_notify_types */) * sizeof(__be32);
30812282cd2cSKinglong Mee }
30822282cd2cSKinglong Mee 
30839cf514ccSChristoph Hellwig /*
30849cf514ccSChristoph Hellwig  * At this stage we don't really know what layout driver will handle the request,
30859cf514ccSChristoph Hellwig  * so we need to define an arbitrary upper bound here.
30869cf514ccSChristoph Hellwig  */
30879cf514ccSChristoph Hellwig #define MAX_LAYOUT_SIZE		128
nfsd4_layoutget_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)30886604148cSChuck Lever static u32 nfsd4_layoutget_rsize(const struct svc_rqst *rqstp,
30896604148cSChuck Lever 				 const struct nfsd4_op *op)
30909cf514ccSChristoph Hellwig {
30919cf514ccSChristoph Hellwig 	return (op_encode_hdr_size +
30929cf514ccSChristoph Hellwig 		1 /* logr_return_on_close */ +
30939cf514ccSChristoph Hellwig 		op_encode_stateid_maxsz +
30949cf514ccSChristoph Hellwig 		1 /* nr of layouts */ +
30959cf514ccSChristoph Hellwig 		MAX_LAYOUT_SIZE) * sizeof(__be32);
30969cf514ccSChristoph Hellwig }
30979cf514ccSChristoph Hellwig 
nfsd4_layoutcommit_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)30986604148cSChuck Lever static u32 nfsd4_layoutcommit_rsize(const struct svc_rqst *rqstp,
30996604148cSChuck Lever 				    const struct nfsd4_op *op)
31009cf514ccSChristoph Hellwig {
31019cf514ccSChristoph Hellwig 	return (op_encode_hdr_size +
31029cf514ccSChristoph Hellwig 		1 /* locr_newsize */ +
31039cf514ccSChristoph Hellwig 		2 /* ns_size */) * sizeof(__be32);
31049cf514ccSChristoph Hellwig }
31059cf514ccSChristoph Hellwig 
nfsd4_layoutreturn_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)31066604148cSChuck Lever static u32 nfsd4_layoutreturn_rsize(const struct svc_rqst *rqstp,
31076604148cSChuck Lever 				    const struct nfsd4_op *op)
31089cf514ccSChristoph Hellwig {
31099cf514ccSChristoph Hellwig 	return (op_encode_hdr_size +
31109cf514ccSChristoph Hellwig 		1 /* lrs_stateid */ +
31119cf514ccSChristoph Hellwig 		op_encode_stateid_maxsz) * sizeof(__be32);
31129cf514ccSChristoph Hellwig }
31139cf514ccSChristoph Hellwig #endif /* CONFIG_NFSD_PNFS */
31149cf514ccSChristoph Hellwig 
31152282cd2cSKinglong Mee 
nfsd4_seek_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)31166604148cSChuck Lever static u32 nfsd4_seek_rsize(const struct svc_rqst *rqstp,
31176604148cSChuck Lever 			    const struct nfsd4_op *op)
31182282cd2cSKinglong Mee {
31192282cd2cSKinglong Mee 	return (op_encode_hdr_size + 3) * sizeof(__be32);
31202282cd2cSKinglong Mee }
31212282cd2cSKinglong Mee 
nfsd4_getxattr_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)31226604148cSChuck Lever static u32 nfsd4_getxattr_rsize(const struct svc_rqst *rqstp,
31236604148cSChuck Lever 				const struct nfsd4_op *op)
312423e50fe3SFrank van der Linden {
312576ce4dceSChuck Lever 	u32 rlen = min_t(u32, XATTR_SIZE_MAX, nfsd4_max_payload(rqstp));
312623e50fe3SFrank van der Linden 
312723e50fe3SFrank van der Linden 	return (op_encode_hdr_size + 1 + XDR_QUADLEN(rlen)) * sizeof(__be32);
312823e50fe3SFrank van der Linden }
312923e50fe3SFrank van der Linden 
nfsd4_setxattr_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)31306604148cSChuck Lever static u32 nfsd4_setxattr_rsize(const struct svc_rqst *rqstp,
31316604148cSChuck Lever 				const struct nfsd4_op *op)
313223e50fe3SFrank van der Linden {
313323e50fe3SFrank van der Linden 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
313423e50fe3SFrank van der Linden 		* sizeof(__be32);
313523e50fe3SFrank van der Linden }
nfsd4_listxattrs_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)31366604148cSChuck Lever static u32 nfsd4_listxattrs_rsize(const struct svc_rqst *rqstp,
31376604148cSChuck Lever 				  const struct nfsd4_op *op)
313823e50fe3SFrank van der Linden {
313976ce4dceSChuck Lever 	u32 rlen = min(op->u.listxattrs.lsxa_maxcount, nfsd4_max_payload(rqstp));
314023e50fe3SFrank van der Linden 
314123e50fe3SFrank van der Linden 	return (op_encode_hdr_size + 4 + XDR_QUADLEN(rlen)) * sizeof(__be32);
314223e50fe3SFrank van der Linden }
314323e50fe3SFrank van der Linden 
nfsd4_removexattr_rsize(const struct svc_rqst * rqstp,const struct nfsd4_op * op)31446604148cSChuck Lever static u32 nfsd4_removexattr_rsize(const struct svc_rqst *rqstp,
31456604148cSChuck Lever 				   const struct nfsd4_op *op)
314623e50fe3SFrank van der Linden {
314723e50fe3SFrank van der Linden 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
314823e50fe3SFrank van der Linden 		* sizeof(__be32);
314923e50fe3SFrank van der Linden }
315023e50fe3SFrank van der Linden 
315123e50fe3SFrank van der Linden 
3152bb2a8b0cSChristoph Hellwig static const struct nfsd4_operation nfsd4_ops[] = {
3153b591480bSJ.Bruce Fields 	[OP_ACCESS] = {
3154eb69853dSChristoph Hellwig 		.op_func = nfsd4_access,
3155b001a1b6SBenny Halevy 		.op_name = "OP_ACCESS",
31561c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_access_rsize,
3157b591480bSJ.Bruce Fields 	},
3158b591480bSJ.Bruce Fields 	[OP_CLOSE] = {
3159eb69853dSChristoph Hellwig 		.op_func = nfsd4_close,
316058e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
3161b001a1b6SBenny Halevy 		.op_name = "OP_CLOSE",
31621c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_status_stateid_rsize,
316357832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_closestateid,
3164b60e9859SChristoph Hellwig 		.op_set_currentstateid = nfsd4_set_closestateid,
3165b591480bSJ.Bruce Fields 	},
3166b591480bSJ.Bruce Fields 	[OP_COMMIT] = {
3167eb69853dSChristoph Hellwig 		.op_func = nfsd4_commit,
316858e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
3169b001a1b6SBenny Halevy 		.op_name = "OP_COMMIT",
31701c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_commit_rsize,
3171b591480bSJ.Bruce Fields 	},
3172b591480bSJ.Bruce Fields 	[OP_CREATE] = {
3173eb69853dSChristoph Hellwig 		.op_func = nfsd4_create,
3174d1471053STigran Mkrtchyan 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME | OP_CLEAR_STATEID,
3175b001a1b6SBenny Halevy 		.op_name = "OP_CREATE",
31761c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_create_rsize,
3177b591480bSJ.Bruce Fields 	},
3178b591480bSJ.Bruce Fields 	[OP_DELEGRETURN] = {
3179eb69853dSChristoph Hellwig 		.op_func = nfsd4_delegreturn,
318058e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
3181b001a1b6SBenny Halevy 		.op_name = "OP_DELEGRETURN",
318258e7b33aSMi Jinlong 		.op_rsize_bop = nfsd4_only_status_rsize,
318357832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_delegreturnstateid,
3184b591480bSJ.Bruce Fields 	},
3185b591480bSJ.Bruce Fields 	[OP_GETATTR] = {
3186eb69853dSChristoph Hellwig 		.op_func = nfsd4_getattr,
3187eeac294eSJ.Bruce Fields 		.op_flags = ALLOWED_ON_ABSENT_FS,
3188b86cef60SJ. Bruce Fields 		.op_rsize_bop = nfsd4_getattr_rsize,
3189b001a1b6SBenny Halevy 		.op_name = "OP_GETATTR",
3190b591480bSJ.Bruce Fields 	},
3191b591480bSJ.Bruce Fields 	[OP_GETFH] = {
3192eb69853dSChristoph Hellwig 		.op_func = nfsd4_getfh,
3193b001a1b6SBenny Halevy 		.op_name = "OP_GETFH",
31941c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_getfh_rsize,
3195b591480bSJ.Bruce Fields 	},
3196b591480bSJ.Bruce Fields 	[OP_LINK] = {
3197eb69853dSChristoph Hellwig 		.op_func = nfsd4_link,
3198c856694eSJ. Bruce Fields 		.op_flags = ALLOWED_ON_ABSENT_FS | OP_MODIFIES_SOMETHING
3199c856694eSJ. Bruce Fields 				| OP_CACHEME,
3200b001a1b6SBenny Halevy 		.op_name = "OP_LINK",
32011c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_link_rsize,
3202b591480bSJ.Bruce Fields 	},
3203b591480bSJ.Bruce Fields 	[OP_LOCK] = {
3204eb69853dSChristoph Hellwig 		.op_func = nfsd4_lock,
3205b7571e4cSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING |
3206b7571e4cSJ. Bruce Fields 				OP_NONTRIVIAL_ERROR_ENCODE,
3207b001a1b6SBenny Halevy 		.op_name = "OP_LOCK",
32081c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_lock_rsize,
3209b60e9859SChristoph Hellwig 		.op_set_currentstateid = nfsd4_set_lockstateid,
3210b591480bSJ.Bruce Fields 	},
3211b591480bSJ.Bruce Fields 	[OP_LOCKT] = {
3212eb69853dSChristoph Hellwig 		.op_func = nfsd4_lockt,
3213b7571e4cSJ. Bruce Fields 		.op_flags = OP_NONTRIVIAL_ERROR_ENCODE,
3214b001a1b6SBenny Halevy 		.op_name = "OP_LOCKT",
32151c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_lock_rsize,
3216b591480bSJ.Bruce Fields 	},
3217b591480bSJ.Bruce Fields 	[OP_LOCKU] = {
3218eb69853dSChristoph Hellwig 		.op_func = nfsd4_locku,
321958e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
3220b001a1b6SBenny Halevy 		.op_name = "OP_LOCKU",
32211c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_status_stateid_rsize,
322257832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_lockustateid,
3223b591480bSJ.Bruce Fields 	},
3224b591480bSJ.Bruce Fields 	[OP_LOOKUP] = {
3225eb69853dSChristoph Hellwig 		.op_func = nfsd4_lookup,
3226d1471053STigran Mkrtchyan 		.op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
3227b001a1b6SBenny Halevy 		.op_name = "OP_LOOKUP",
32281c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3229b591480bSJ.Bruce Fields 	},
3230b591480bSJ.Bruce Fields 	[OP_LOOKUPP] = {
3231eb69853dSChristoph Hellwig 		.op_func = nfsd4_lookupp,
3232d1471053STigran Mkrtchyan 		.op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
3233b001a1b6SBenny Halevy 		.op_name = "OP_LOOKUPP",
32341c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3235b591480bSJ.Bruce Fields 	},
3236b591480bSJ.Bruce Fields 	[OP_NVERIFY] = {
3237eb69853dSChristoph Hellwig 		.op_func = nfsd4_nverify,
3238b001a1b6SBenny Halevy 		.op_name = "OP_NVERIFY",
32391c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3240b591480bSJ.Bruce Fields 	},
3241b591480bSJ.Bruce Fields 	[OP_OPEN] = {
3242eb69853dSChristoph Hellwig 		.op_func = nfsd4_open,
324358e7b33aSMi Jinlong 		.op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
3244b001a1b6SBenny Halevy 		.op_name = "OP_OPEN",
32451c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_open_rsize,
3246b60e9859SChristoph Hellwig 		.op_set_currentstateid = nfsd4_set_openstateid,
3247b591480bSJ.Bruce Fields 	},
3248b591480bSJ.Bruce Fields 	[OP_OPEN_CONFIRM] = {
3249eb69853dSChristoph Hellwig 		.op_func = nfsd4_open_confirm,
325058e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
3251b001a1b6SBenny Halevy 		.op_name = "OP_OPEN_CONFIRM",
32521c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_status_stateid_rsize,
3253b591480bSJ.Bruce Fields 	},
3254b591480bSJ.Bruce Fields 	[OP_OPEN_DOWNGRADE] = {
3255eb69853dSChristoph Hellwig 		.op_func = nfsd4_open_downgrade,
325658e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
3257b001a1b6SBenny Halevy 		.op_name = "OP_OPEN_DOWNGRADE",
32581c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_status_stateid_rsize,
325957832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_opendowngradestateid,
3260b60e9859SChristoph Hellwig 		.op_set_currentstateid = nfsd4_set_opendowngradestateid,
3261b591480bSJ.Bruce Fields 	},
3262b591480bSJ.Bruce Fields 	[OP_PUTFH] = {
3263eb69853dSChristoph Hellwig 		.op_func = nfsd4_putfh,
326468d93184SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
32655b648699SJ. Bruce Fields 				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
3266b001a1b6SBenny Halevy 		.op_name = "OP_PUTFH",
32671c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3268b591480bSJ.Bruce Fields 	},
3269eeac294eSJ.Bruce Fields 	[OP_PUTPUBFH] = {
3270eb69853dSChristoph Hellwig 		.op_func = nfsd4_putrootfh,
327168d93184SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
32725b648699SJ. Bruce Fields 				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
3273b001a1b6SBenny Halevy 		.op_name = "OP_PUTPUBFH",
32741c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3275eeac294eSJ.Bruce Fields 	},
3276b591480bSJ.Bruce Fields 	[OP_PUTROOTFH] = {
3277eb69853dSChristoph Hellwig 		.op_func = nfsd4_putrootfh,
327868d93184SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
32795b648699SJ. Bruce Fields 				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
3280b001a1b6SBenny Halevy 		.op_name = "OP_PUTROOTFH",
32811c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3282b591480bSJ.Bruce Fields 	},
3283b591480bSJ.Bruce Fields 	[OP_READ] = {
3284eb69853dSChristoph Hellwig 		.op_func = nfsd4_read,
328534b1744cSJ. Bruce Fields 		.op_release = nfsd4_read_release,
3286b001a1b6SBenny Halevy 		.op_name = "OP_READ",
32871c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_read_rsize,
328857832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_readstateid,
3289b591480bSJ.Bruce Fields 	},
3290b591480bSJ.Bruce Fields 	[OP_READDIR] = {
3291eb69853dSChristoph Hellwig 		.op_func = nfsd4_readdir,
3292b001a1b6SBenny Halevy 		.op_name = "OP_READDIR",
32931c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_readdir_rsize,
3294b591480bSJ.Bruce Fields 	},
3295b591480bSJ.Bruce Fields 	[OP_READLINK] = {
3296eb69853dSChristoph Hellwig 		.op_func = nfsd4_readlink,
3297b001a1b6SBenny Halevy 		.op_name = "OP_READLINK",
32981c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_readlink_rsize,
3299b591480bSJ.Bruce Fields 	},
3300b591480bSJ.Bruce Fields 	[OP_REMOVE] = {
3301eb69853dSChristoph Hellwig 		.op_func = nfsd4_remove,
3302c856694eSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3303b001a1b6SBenny Halevy 		.op_name = "OP_REMOVE",
33041c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_remove_rsize,
3305b591480bSJ.Bruce Fields 	},
3306b591480bSJ.Bruce Fields 	[OP_RENAME] = {
3307eb69853dSChristoph Hellwig 		.op_func = nfsd4_rename,
3308c856694eSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
330958e7b33aSMi Jinlong 		.op_name = "OP_RENAME",
33101c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_rename_rsize,
3311b591480bSJ.Bruce Fields 	},
3312b591480bSJ.Bruce Fields 	[OP_RENEW] = {
3313eb69853dSChristoph Hellwig 		.op_func = nfsd4_renew,
331458e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
331558e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
3316b001a1b6SBenny Halevy 		.op_name = "OP_RENEW",
33171c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
331858e7b33aSMi Jinlong 
3319b591480bSJ.Bruce Fields 	},
3320b591480bSJ.Bruce Fields 	[OP_RESTOREFH] = {
3321eb69853dSChristoph Hellwig 		.op_func = nfsd4_restorefh,
332268d93184SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
332358e7b33aSMi Jinlong 				| OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING,
3324b001a1b6SBenny Halevy 		.op_name = "OP_RESTOREFH",
33251c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3326b591480bSJ.Bruce Fields 	},
3327b591480bSJ.Bruce Fields 	[OP_SAVEFH] = {
3328eb69853dSChristoph Hellwig 		.op_func = nfsd4_savefh,
332958e7b33aSMi Jinlong 		.op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
3330b001a1b6SBenny Halevy 		.op_name = "OP_SAVEFH",
33311c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3332b591480bSJ.Bruce Fields 	},
3333dcb488a3SAndy Adamson 	[OP_SECINFO] = {
3334eb69853dSChristoph Hellwig 		.op_func = nfsd4_secinfo,
333534b1744cSJ. Bruce Fields 		.op_release = nfsd4_secinfo_release,
333668d93184SJ. Bruce Fields 		.op_flags = OP_HANDLES_WRONGSEC,
3337b001a1b6SBenny Halevy 		.op_name = "OP_SECINFO",
33381c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_secinfo_rsize,
3339dcb488a3SAndy Adamson 	},
3340b591480bSJ.Bruce Fields 	[OP_SETATTR] = {
3341eb69853dSChristoph Hellwig 		.op_func = nfsd4_setattr,
3342b001a1b6SBenny Halevy 		.op_name = "OP_SETATTR",
3343b7571e4cSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME
3344b7571e4cSJ. Bruce Fields 				| OP_NONTRIVIAL_ERROR_ENCODE,
33451c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_setattr_rsize,
334657832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_setattrstateid,
3347b591480bSJ.Bruce Fields 	},
3348b591480bSJ.Bruce Fields 	[OP_SETCLIENTID] = {
3349eb69853dSChristoph Hellwig 		.op_func = nfsd4_setclientid,
335058e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3351b7571e4cSJ. Bruce Fields 				| OP_MODIFIES_SOMETHING | OP_CACHEME
3352b7571e4cSJ. Bruce Fields 				| OP_NONTRIVIAL_ERROR_ENCODE,
3353b001a1b6SBenny Halevy 		.op_name = "OP_SETCLIENTID",
33541c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_setclientid_rsize,
3355b591480bSJ.Bruce Fields 	},
3356b591480bSJ.Bruce Fields 	[OP_SETCLIENTID_CONFIRM] = {
3357eb69853dSChristoph Hellwig 		.op_func = nfsd4_setclientid_confirm,
335858e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3359c856694eSJ. Bruce Fields 				| OP_MODIFIES_SOMETHING | OP_CACHEME,
3360b001a1b6SBenny Halevy 		.op_name = "OP_SETCLIENTID_CONFIRM",
33611c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3362b591480bSJ.Bruce Fields 	},
3363b591480bSJ.Bruce Fields 	[OP_VERIFY] = {
3364eb69853dSChristoph Hellwig 		.op_func = nfsd4_verify,
3365b001a1b6SBenny Halevy 		.op_name = "OP_VERIFY",
33661c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3367b591480bSJ.Bruce Fields 	},
3368b591480bSJ.Bruce Fields 	[OP_WRITE] = {
3369eb69853dSChristoph Hellwig 		.op_func = nfsd4_write,
3370c856694eSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3371b001a1b6SBenny Halevy 		.op_name = "OP_WRITE",
33721c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_write_rsize,
337357832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_writestateid,
3374b591480bSJ.Bruce Fields 	},
3375b591480bSJ.Bruce Fields 	[OP_RELEASE_LOCKOWNER] = {
3376eb69853dSChristoph Hellwig 		.op_func = nfsd4_release_lockowner,
337758e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
337858e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
3379b001a1b6SBenny Halevy 		.op_name = "OP_RELEASE_LOCKOWNER",
33801c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3381b591480bSJ.Bruce Fields 	},
3382069b6ad4SAndy Adamson 
3383069b6ad4SAndy Adamson 	/* NFSv4.1 operations */
3384069b6ad4SAndy Adamson 	[OP_EXCHANGE_ID] = {
3385eb69853dSChristoph Hellwig 		.op_func = nfsd4_exchange_id,
338658e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
338758e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
3388069b6ad4SAndy Adamson 		.op_name = "OP_EXCHANGE_ID",
33891c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_exchange_id_rsize,
3390069b6ad4SAndy Adamson 	},
3391cb73a9f4SJ. Bruce Fields 	[OP_BACKCHANNEL_CTL] = {
3392eb69853dSChristoph Hellwig 		.op_func = nfsd4_backchannel_ctl,
3393cb73a9f4SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
3394cb73a9f4SJ. Bruce Fields 		.op_name = "OP_BACKCHANNEL_CTL",
33951c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3396cb73a9f4SJ. Bruce Fields 	},
33971d1bc8f2SJ. Bruce Fields 	[OP_BIND_CONN_TO_SESSION] = {
3398eb69853dSChristoph Hellwig 		.op_func = nfsd4_bind_conn_to_session,
339958e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
340058e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
34011d1bc8f2SJ. Bruce Fields 		.op_name = "OP_BIND_CONN_TO_SESSION",
34021c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_bind_conn_to_session_rsize,
34031d1bc8f2SJ. Bruce Fields 	},
3404069b6ad4SAndy Adamson 	[OP_CREATE_SESSION] = {
3405eb69853dSChristoph Hellwig 		.op_func = nfsd4_create_session,
340658e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
340758e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
3408069b6ad4SAndy Adamson 		.op_name = "OP_CREATE_SESSION",
34091c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_create_session_rsize,
3410069b6ad4SAndy Adamson 	},
3411069b6ad4SAndy Adamson 	[OP_DESTROY_SESSION] = {
3412eb69853dSChristoph Hellwig 		.op_func = nfsd4_destroy_session,
341358e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
341458e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
3415069b6ad4SAndy Adamson 		.op_name = "OP_DESTROY_SESSION",
34161c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3417069b6ad4SAndy Adamson 	},
3418069b6ad4SAndy Adamson 	[OP_SEQUENCE] = {
3419eb69853dSChristoph Hellwig 		.op_func = nfsd4_sequence,
3420f9bb94c4SAndy Adamson 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
3421069b6ad4SAndy Adamson 		.op_name = "OP_SEQUENCE",
34221c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_sequence_rsize,
3423069b6ad4SAndy Adamson 	},
3424094b5d74SBenny Halevy 	[OP_DESTROY_CLIENTID] = {
3425eb69853dSChristoph Hellwig 		.op_func = nfsd4_destroy_clientid,
342658e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
342758e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
3428094b5d74SBenny Halevy 		.op_name = "OP_DESTROY_CLIENTID",
34291c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3430094b5d74SBenny Halevy 	},
34314dc6ec00SJ. Bruce Fields 	[OP_RECLAIM_COMPLETE] = {
3432eb69853dSChristoph Hellwig 		.op_func = nfsd4_reclaim_complete,
343358e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
34344dc6ec00SJ. Bruce Fields 		.op_name = "OP_RECLAIM_COMPLETE",
34351c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
34364dc6ec00SJ. Bruce Fields 	},
343704f4ad16SJ. Bruce Fields 	[OP_SECINFO_NO_NAME] = {
3438eb69853dSChristoph Hellwig 		.op_func = nfsd4_secinfo_no_name,
3439ec572b9eSEryu Guan 		.op_release = nfsd4_secinfo_no_name_release,
344068d93184SJ. Bruce Fields 		.op_flags = OP_HANDLES_WRONGSEC,
344104f4ad16SJ. Bruce Fields 		.op_name = "OP_SECINFO_NO_NAME",
34421c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_secinfo_rsize,
344304f4ad16SJ. Bruce Fields 	},
344417456804SBryan Schumaker 	[OP_TEST_STATEID] = {
3445eb69853dSChristoph Hellwig 		.op_func = nfsd4_test_stateid,
344617456804SBryan Schumaker 		.op_flags = ALLOWED_WITHOUT_FH,
344717456804SBryan Schumaker 		.op_name = "OP_TEST_STATEID",
34481c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_test_stateid_rsize,
344917456804SBryan Schumaker 	},
3450e1ca12dfSBryan Schumaker 	[OP_FREE_STATEID] = {
3451eb69853dSChristoph Hellwig 		.op_func = nfsd4_free_stateid,
345258e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
3453e1ca12dfSBryan Schumaker 		.op_name = "OP_FREE_STATEID",
345457832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_freestateid,
34551c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3456e1ca12dfSBryan Schumaker 	},
34579cf514ccSChristoph Hellwig #ifdef CONFIG_NFSD_PNFS
34589cf514ccSChristoph Hellwig 	[OP_GETDEVICEINFO] = {
3459eb69853dSChristoph Hellwig 		.op_func = nfsd4_getdeviceinfo,
346034b1744cSJ. Bruce Fields 		.op_release = nfsd4_getdeviceinfo_release,
34619cf514ccSChristoph Hellwig 		.op_flags = ALLOWED_WITHOUT_FH,
34629cf514ccSChristoph Hellwig 		.op_name = "OP_GETDEVICEINFO",
34631c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_getdeviceinfo_rsize,
34649cf514ccSChristoph Hellwig 	},
34659cf514ccSChristoph Hellwig 	[OP_LAYOUTGET] = {
3466eb69853dSChristoph Hellwig 		.op_func = nfsd4_layoutget,
346734b1744cSJ. Bruce Fields 		.op_release = nfsd4_layoutget_release,
34689cf514ccSChristoph Hellwig 		.op_flags = OP_MODIFIES_SOMETHING,
34699cf514ccSChristoph Hellwig 		.op_name = "OP_LAYOUTGET",
34701c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_layoutget_rsize,
34719cf514ccSChristoph Hellwig 	},
34729cf514ccSChristoph Hellwig 	[OP_LAYOUTCOMMIT] = {
3473eb69853dSChristoph Hellwig 		.op_func = nfsd4_layoutcommit,
34749cf514ccSChristoph Hellwig 		.op_flags = OP_MODIFIES_SOMETHING,
34759cf514ccSChristoph Hellwig 		.op_name = "OP_LAYOUTCOMMIT",
34761c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_layoutcommit_rsize,
34779cf514ccSChristoph Hellwig 	},
34789cf514ccSChristoph Hellwig 	[OP_LAYOUTRETURN] = {
3479eb69853dSChristoph Hellwig 		.op_func = nfsd4_layoutreturn,
34809cf514ccSChristoph Hellwig 		.op_flags = OP_MODIFIES_SOMETHING,
34819cf514ccSChristoph Hellwig 		.op_name = "OP_LAYOUTRETURN",
34821c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_layoutreturn_rsize,
34839cf514ccSChristoph Hellwig 	},
34849cf514ccSChristoph Hellwig #endif /* CONFIG_NFSD_PNFS */
348524bab491SAnna Schumaker 
348624bab491SAnna Schumaker 	/* NFSv4.2 operations */
348795d871f0SAnna Schumaker 	[OP_ALLOCATE] = {
3488eb69853dSChristoph Hellwig 		.op_func = nfsd4_allocate,
348903b31f48SOlga Kornievskaia 		.op_flags = OP_MODIFIES_SOMETHING,
349095d871f0SAnna Schumaker 		.op_name = "OP_ALLOCATE",
34911c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
349295d871f0SAnna Schumaker 	},
3493b0cb9085SAnna Schumaker 	[OP_DEALLOCATE] = {
3494eb69853dSChristoph Hellwig 		.op_func = nfsd4_deallocate,
349503b31f48SOlga Kornievskaia 		.op_flags = OP_MODIFIES_SOMETHING,
3496b0cb9085SAnna Schumaker 		.op_name = "OP_DEALLOCATE",
34971c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3498b0cb9085SAnna Schumaker 	},
3499ffa0160aSChristoph Hellwig 	[OP_CLONE] = {
3500eb69853dSChristoph Hellwig 		.op_func = nfsd4_clone,
350103b31f48SOlga Kornievskaia 		.op_flags = OP_MODIFIES_SOMETHING,
3502ffa0160aSChristoph Hellwig 		.op_name = "OP_CLONE",
35031c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3504ffa0160aSChristoph Hellwig 	},
350529ae7f9dSAnna Schumaker 	[OP_COPY] = {
3506eb69853dSChristoph Hellwig 		.op_func = nfsd4_copy,
350703b31f48SOlga Kornievskaia 		.op_flags = OP_MODIFIES_SOMETHING,
350829ae7f9dSAnna Schumaker 		.op_name = "OP_COPY",
35091c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_copy_rsize,
351029ae7f9dSAnna Schumaker 	},
3511528b8493SAnna Schumaker 	[OP_READ_PLUS] = {
3512528b8493SAnna Schumaker 		.op_func = nfsd4_read,
3513528b8493SAnna Schumaker 		.op_release = nfsd4_read_release,
3514528b8493SAnna Schumaker 		.op_name = "OP_READ_PLUS",
3515528b8493SAnna Schumaker 		.op_rsize_bop = nfsd4_read_plus_rsize,
3516528b8493SAnna Schumaker 		.op_get_currentstateid = nfsd4_get_readstateid,
3517528b8493SAnna Schumaker 	},
351824bab491SAnna Schumaker 	[OP_SEEK] = {
3519eb69853dSChristoph Hellwig 		.op_func = nfsd4_seek,
352024bab491SAnna Schumaker 		.op_name = "OP_SEEK",
35211c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_seek_rsize,
352224bab491SAnna Schumaker 	},
35236308bc98SOlga Kornievskaia 	[OP_OFFLOAD_STATUS] = {
35246308bc98SOlga Kornievskaia 		.op_func = nfsd4_offload_status,
35256308bc98SOlga Kornievskaia 		.op_name = "OP_OFFLOAD_STATUS",
35266308bc98SOlga Kornievskaia 		.op_rsize_bop = nfsd4_offload_status_rsize,
35276308bc98SOlga Kornievskaia 	},
3528885e2bf3SOlga Kornievskaia 	[OP_OFFLOAD_CANCEL] = {
3529885e2bf3SOlga Kornievskaia 		.op_func = nfsd4_offload_cancel,
3530885e2bf3SOlga Kornievskaia 		.op_flags = OP_MODIFIES_SOMETHING,
3531885e2bf3SOlga Kornievskaia 		.op_name = "OP_OFFLOAD_CANCEL",
3532885e2bf3SOlga Kornievskaia 		.op_rsize_bop = nfsd4_only_status_rsize,
3533885e2bf3SOlga Kornievskaia 	},
353451911868SOlga Kornievskaia 	[OP_COPY_NOTIFY] = {
353551911868SOlga Kornievskaia 		.op_func = nfsd4_copy_notify,
353651911868SOlga Kornievskaia 		.op_flags = OP_MODIFIES_SOMETHING,
353751911868SOlga Kornievskaia 		.op_name = "OP_COPY_NOTIFY",
353851911868SOlga Kornievskaia 		.op_rsize_bop = nfsd4_copy_notify_rsize,
353951911868SOlga Kornievskaia 	},
354023e50fe3SFrank van der Linden 	[OP_GETXATTR] = {
354123e50fe3SFrank van der Linden 		.op_func = nfsd4_getxattr,
354223e50fe3SFrank van der Linden 		.op_name = "OP_GETXATTR",
354323e50fe3SFrank van der Linden 		.op_rsize_bop = nfsd4_getxattr_rsize,
354423e50fe3SFrank van der Linden 	},
354523e50fe3SFrank van der Linden 	[OP_SETXATTR] = {
354623e50fe3SFrank van der Linden 		.op_func = nfsd4_setxattr,
354723e50fe3SFrank van der Linden 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
354823e50fe3SFrank van der Linden 		.op_name = "OP_SETXATTR",
354923e50fe3SFrank van der Linden 		.op_rsize_bop = nfsd4_setxattr_rsize,
355023e50fe3SFrank van der Linden 	},
355123e50fe3SFrank van der Linden 	[OP_LISTXATTRS] = {
355223e50fe3SFrank van der Linden 		.op_func = nfsd4_listxattrs,
355323e50fe3SFrank van der Linden 		.op_name = "OP_LISTXATTRS",
355423e50fe3SFrank van der Linden 		.op_rsize_bop = nfsd4_listxattrs_rsize,
355523e50fe3SFrank van der Linden 	},
355623e50fe3SFrank van der Linden 	[OP_REMOVEXATTR] = {
355723e50fe3SFrank van der Linden 		.op_func = nfsd4_removexattr,
355823e50fe3SFrank van der Linden 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
355923e50fe3SFrank van der Linden 		.op_name = "OP_REMOVEXATTR",
356023e50fe3SFrank van der Linden 		.op_rsize_bop = nfsd4_removexattr_rsize,
356123e50fe3SFrank van der Linden 	},
3562b591480bSJ.Bruce Fields };
3563b591480bSJ.Bruce Fields 
3564ed941643SAndrew Elble /**
3565ed941643SAndrew Elble  * nfsd4_spo_must_allow - Determine if the compound op contains an
3566ed941643SAndrew Elble  * operation that is allowed to be sent with machine credentials
3567ed941643SAndrew Elble  *
3568ed941643SAndrew Elble  * @rqstp: a pointer to the struct svc_rqst
3569ed941643SAndrew Elble  *
3570ed941643SAndrew Elble  * Checks to see if the compound contains a spo_must_allow op
3571ed941643SAndrew Elble  * and confirms that it was sent with the proper machine creds.
3572ed941643SAndrew Elble  */
3573ed941643SAndrew Elble 
nfsd4_spo_must_allow(struct svc_rqst * rqstp)3574ed941643SAndrew Elble bool nfsd4_spo_must_allow(struct svc_rqst *rqstp)
3575ed941643SAndrew Elble {
3576ed941643SAndrew Elble 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
3577ed941643SAndrew Elble 	struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3578e34c0ce9SColin Ian King 	struct nfsd4_op *this;
3579ed941643SAndrew Elble 	struct nfsd4_compound_state *cstate = &resp->cstate;
3580ed941643SAndrew Elble 	struct nfs4_op_map *allow = &cstate->clp->cl_spo_must_allow;
3581ed941643SAndrew Elble 	u32 opiter;
3582ed941643SAndrew Elble 
3583ed941643SAndrew Elble 	if (!cstate->minorversion)
3584ed941643SAndrew Elble 		return false;
3585ed941643SAndrew Elble 
358644b49aa6SZheng Bin 	if (cstate->spo_must_allowed)
3587ed941643SAndrew Elble 		return true;
3588ed941643SAndrew Elble 
3589ed941643SAndrew Elble 	opiter = resp->opcnt;
3590ed941643SAndrew Elble 	while (opiter < argp->opcnt) {
3591ed941643SAndrew Elble 		this = &argp->ops[opiter++];
3592ed941643SAndrew Elble 		if (test_bit(this->opnum, allow->u.longs) &&
3593ed941643SAndrew Elble 			cstate->clp->cl_mach_cred &&
3594ed941643SAndrew Elble 			nfsd4_mach_creds_match(cstate->clp, rqstp)) {
3595ed941643SAndrew Elble 			cstate->spo_must_allowed = true;
3596ed941643SAndrew Elble 			return true;
3597ed941643SAndrew Elble 		}
3598ed941643SAndrew Elble 	}
3599ed941643SAndrew Elble 	cstate->spo_must_allowed = false;
3600ed941643SAndrew Elble 	return false;
3601ed941643SAndrew Elble }
3602ed941643SAndrew Elble 
nfsd4_max_reply(struct svc_rqst * rqstp,struct nfsd4_op * op)36034f0cefbfSJ. Bruce Fields int nfsd4_max_reply(struct svc_rqst *rqstp, struct nfsd4_op *op)
36044f0cefbfSJ. Bruce Fields {
360505b7278dSOlga Kornievskaia 	if (op->opnum == OP_ILLEGAL || op->status == nfserr_notsupp)
36064f0cefbfSJ. Bruce Fields 		return op_encode_hdr_size * sizeof(__be32);
36072282cd2cSKinglong Mee 
36082282cd2cSKinglong Mee 	BUG_ON(OPDESC(op)->op_rsize_bop == NULL);
36092282cd2cSKinglong Mee 	return OPDESC(op)->op_rsize_bop(rqstp, op);
36104f0cefbfSJ. Bruce Fields }
36114f0cefbfSJ. Bruce Fields 
warn_on_nonidempotent_op(struct nfsd4_op * op)361207d1f802SJ. Bruce Fields void warn_on_nonidempotent_op(struct nfsd4_op *op)
361307d1f802SJ. Bruce Fields {
361407d1f802SJ. Bruce Fields 	if (OPDESC(op)->op_flags & OP_MODIFIES_SOMETHING) {
36153a237b4aSChuck Lever 		pr_err("unable to encode reply to nonidempotent op %u (%s)\n",
361607d1f802SJ. Bruce Fields 			op->opnum, nfsd4_op_name(op->opnum));
361707d1f802SJ. Bruce Fields 		WARN_ON_ONCE(1);
361807d1f802SJ. Bruce Fields 	}
361907d1f802SJ. Bruce Fields }
362007d1f802SJ. Bruce Fields 
nfsd4_op_name(unsigned opnum)3621f1c7f79bSAdrian Bunk static const char *nfsd4_op_name(unsigned opnum)
3622b001a1b6SBenny Halevy {
3623b001a1b6SBenny Halevy 	if (opnum < ARRAY_SIZE(nfsd4_ops))
3624b001a1b6SBenny Halevy 		return nfsd4_ops[opnum].op_name;
3625b001a1b6SBenny Halevy 	return "unknown_operation";
3626b001a1b6SBenny Halevy }
3627b001a1b6SBenny Halevy 
3628860bda29SChristoph Hellwig static const struct svc_procedure nfsd_procedures4[2] = {
36290a93a47fSYu Zhiguo 	[NFSPROC4_NULL] = {
3630f7235b6bSChristoph Hellwig 		.pc_func = nfsd4_proc_null,
3631788f7183SChuck Lever 		.pc_decode = nfssvc_decode_voidarg,
3632788f7183SChuck Lever 		.pc_encode = nfssvc_encode_voidres,
3633788f7183SChuck Lever 		.pc_argsize = sizeof(struct nfsd_voidargs),
3634103cc1faSChuck Lever 		.pc_argzero = sizeof(struct nfsd_voidargs),
3635788f7183SChuck Lever 		.pc_ressize = sizeof(struct nfsd_voidres),
36360a93a47fSYu Zhiguo 		.pc_cachetype = RC_NOCACHE,
36370a93a47fSYu Zhiguo 		.pc_xdrressize = 1,
36382289e87bSChuck Lever 		.pc_name = "NULL",
36390a93a47fSYu Zhiguo 	},
36400a93a47fSYu Zhiguo 	[NFSPROC4_COMPOUND] = {
3641f7235b6bSChristoph Hellwig 		.pc_func = nfsd4_proc_compound,
3642026fec7eSChristoph Hellwig 		.pc_decode = nfs4svc_decode_compoundargs,
364363f8de37SChristoph Hellwig 		.pc_encode = nfs4svc_encode_compoundres,
36440a93a47fSYu Zhiguo 		.pc_argsize = sizeof(struct nfsd4_compoundargs),
36453fdc5464SChuck Lever 		.pc_argzero = offsetof(struct nfsd4_compoundargs, iops),
36460a93a47fSYu Zhiguo 		.pc_ressize = sizeof(struct nfsd4_compoundres),
36473e98abffSJ. Bruce Fields 		.pc_release = nfsd4_release_compoundargs,
36480a93a47fSYu Zhiguo 		.pc_cachetype = RC_NOCACHE,
36490a93a47fSYu Zhiguo 		.pc_xdrressize = NFSD_BUFSIZE/4,
36502289e87bSChuck Lever 		.pc_name = "COMPOUND",
36510a93a47fSYu Zhiguo 	},
36521da177e4SLinus Torvalds };
36531da177e4SLinus Torvalds 
365465ba3d24SChuck Lever static DEFINE_PER_CPU_ALIGNED(unsigned long,
365565ba3d24SChuck Lever 			      nfsd_count4[ARRAY_SIZE(nfsd_procedures4)]);
3656e9679189SChristoph Hellwig const struct svc_version nfsd_version4 = {
36571da177e4SLinus Torvalds 	.vs_vers		= 4,
365865ba3d24SChuck Lever 	.vs_nproc		= ARRAY_SIZE(nfsd_procedures4),
36591da177e4SLinus Torvalds 	.vs_proc		= nfsd_procedures4,
366065ba3d24SChuck Lever 	.vs_count		= nfsd_count4,
36611da177e4SLinus Torvalds 	.vs_dispatch		= nfsd_dispatch,
36621da177e4SLinus Torvalds 	.vs_xdrsize		= NFS4_SVC_XDRSIZE,
366305a45a2dSJeff Layton 	.vs_rpcb_optnl		= true,
36645283b03eSJeff Layton 	.vs_need_cong_ctrl	= true,
36651da177e4SLinus Torvalds };
3666