xref: /openbmc/linux/fs/nfsd/nfs4proc.c (revision 20a10c78ac3a8b608dca7c42bd6a982b192f55dd)
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
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
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
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
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
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 
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 
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 
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
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
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
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
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
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
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
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  */
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
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
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
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
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
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
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
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
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
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
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 
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 
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;
127687689df6SChuck Lever 	kfree(copy->cp_src);
1277e0639dc5SOlga Kornievskaia 	kfree(copy);
1278e0639dc5SOlga Kornievskaia }
1279e0639dc5SOlga Kornievskaia 
1280e0639dc5SOlga Kornievskaia static void nfsd4_stop_copy(struct nfsd4_copy *copy)
1281e0639dc5SOlga Kornievskaia {
12821913cdf5SChuck Lever 	if (!test_and_set_bit(NFSD4_COPY_F_STOPPED, &copy->cp_flags))
1283e0639dc5SOlga Kornievskaia 		kthread_stop(copy->copy_task);
1284e0639dc5SOlga Kornievskaia 	nfs4_put_copy(copy);
1285e0639dc5SOlga Kornievskaia }
1286e0639dc5SOlga Kornievskaia 
1287e0639dc5SOlga Kornievskaia static struct nfsd4_copy *nfsd4_get_copy(struct nfs4_client *clp)
1288e0639dc5SOlga Kornievskaia {
1289e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy = NULL;
1290e0639dc5SOlga Kornievskaia 
1291e0639dc5SOlga Kornievskaia 	spin_lock(&clp->async_lock);
1292e0639dc5SOlga Kornievskaia 	if (!list_empty(&clp->async_copies)) {
1293e0639dc5SOlga Kornievskaia 		copy = list_first_entry(&clp->async_copies, struct nfsd4_copy,
1294e0639dc5SOlga Kornievskaia 					copies);
1295e0639dc5SOlga Kornievskaia 		refcount_inc(&copy->refcount);
1296e0639dc5SOlga Kornievskaia 	}
1297e0639dc5SOlga Kornievskaia 	spin_unlock(&clp->async_lock);
1298e0639dc5SOlga Kornievskaia 	return copy;
1299e0639dc5SOlga Kornievskaia }
1300e0639dc5SOlga Kornievskaia 
1301e0639dc5SOlga Kornievskaia void nfsd4_shutdown_copy(struct nfs4_client *clp)
1302e0639dc5SOlga Kornievskaia {
1303e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy;
1304e0639dc5SOlga Kornievskaia 
1305e0639dc5SOlga Kornievskaia 	while ((copy = nfsd4_get_copy(clp)) != NULL)
1306e0639dc5SOlga Kornievskaia 		nfsd4_stop_copy(copy);
1307e0639dc5SOlga Kornievskaia }
1308ce0887acSOlga Kornievskaia #ifdef CONFIG_NFSD_V4_2_INTER_SSC
1309ce0887acSOlga Kornievskaia 
1310ce0887acSOlga Kornievskaia extern struct file *nfs42_ssc_open(struct vfsmount *ss_mnt,
1311ce0887acSOlga Kornievskaia 				   struct nfs_fh *src_fh,
1312ce0887acSOlga Kornievskaia 				   nfs4_stateid *stateid);
1313ce0887acSOlga Kornievskaia extern void nfs42_ssc_close(struct file *filep);
1314ce0887acSOlga Kornievskaia 
1315ce0887acSOlga Kornievskaia extern void nfs_sb_deactive(struct super_block *sb);
1316ce0887acSOlga Kornievskaia 
1317ce0887acSOlga Kornievskaia #define NFSD42_INTERSSC_MOUNTOPS "vers=4.2,addr=%s,sec=sys"
1318ce0887acSOlga Kornievskaia 
1319f2453978SChuck Lever /*
1320f4e44b39SDai Ngo  * setup a work entry in the ssc delayed unmount list.
1321f4e44b39SDai Ngo  */
1322f47dc2d3SDai Ngo static __be32 nfsd4_ssc_setup_dul(struct nfsd_net *nn, char *ipaddr,
1323df24ac7aSDai Ngo 				  struct nfsd4_ssc_umount_item **nsui)
1324f4e44b39SDai Ngo {
13258e70bf27SColin Ian King 	struct nfsd4_ssc_umount_item *ni = NULL;
1326f4e44b39SDai Ngo 	struct nfsd4_ssc_umount_item *work = NULL;
1327f4e44b39SDai Ngo 	struct nfsd4_ssc_umount_item *tmp;
1328f4e44b39SDai Ngo 	DEFINE_WAIT(wait);
1329df24ac7aSDai Ngo 	__be32 status = 0;
1330f4e44b39SDai Ngo 
1331df24ac7aSDai Ngo 	*nsui = NULL;
1332f4e44b39SDai Ngo 	work = kzalloc(sizeof(*work), GFP_KERNEL);
1333f4e44b39SDai Ngo try_again:
1334f4e44b39SDai Ngo 	spin_lock(&nn->nfsd_ssc_lock);
1335f4e44b39SDai Ngo 	list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
1336f4e44b39SDai Ngo 		if (strncmp(ni->nsui_ipaddr, ipaddr, sizeof(ni->nsui_ipaddr)))
1337f4e44b39SDai Ngo 			continue;
1338f4e44b39SDai Ngo 		/* found a match */
1339f4e44b39SDai Ngo 		if (ni->nsui_busy) {
1340f4e44b39SDai Ngo 			/*  wait - and try again */
134139039024SNeilBrown 			prepare_to_wait(&nn->nfsd_ssc_waitq, &wait, TASK_IDLE);
1342f4e44b39SDai Ngo 			spin_unlock(&nn->nfsd_ssc_lock);
1343f4e44b39SDai Ngo 
1344f4e44b39SDai Ngo 			/* allow 20secs for mount/unmount for now - revisit */
134539039024SNeilBrown 			if (kthread_should_stop() ||
1346f4e44b39SDai Ngo 					(schedule_timeout(20*HZ) == 0)) {
1347e6cf91b7SXingyuan Mo 				finish_wait(&nn->nfsd_ssc_waitq, &wait);
1348f4e44b39SDai Ngo 				kfree(work);
1349f4e44b39SDai Ngo 				return nfserr_eagain;
1350f4e44b39SDai Ngo 			}
1351f4e44b39SDai Ngo 			finish_wait(&nn->nfsd_ssc_waitq, &wait);
1352f4e44b39SDai Ngo 			goto try_again;
1353f4e44b39SDai Ngo 		}
1354df24ac7aSDai Ngo 		*nsui = ni;
1355f4e44b39SDai Ngo 		refcount_inc(&ni->nsui_refcnt);
1356f4e44b39SDai Ngo 		spin_unlock(&nn->nfsd_ssc_lock);
1357f4e44b39SDai Ngo 		kfree(work);
1358f4e44b39SDai Ngo 
1359df24ac7aSDai Ngo 		/* return vfsmount in (*nsui)->nsui_vfsmount */
1360f4e44b39SDai Ngo 		return 0;
1361f4e44b39SDai Ngo 	}
1362f4e44b39SDai Ngo 	if (work) {
136372f78ae0SWolfram Sang 		strscpy(work->nsui_ipaddr, ipaddr, sizeof(work->nsui_ipaddr) - 1);
1364f4e44b39SDai Ngo 		refcount_set(&work->nsui_refcnt, 2);
1365f4e44b39SDai Ngo 		work->nsui_busy = true;
1366f4e44b39SDai Ngo 		list_add_tail(&work->nsui_list, &nn->nfsd_ssc_mount_list);
1367df24ac7aSDai Ngo 		*nsui = work;
1368df24ac7aSDai Ngo 	} else
1369df24ac7aSDai Ngo 		status = nfserr_resource;
1370f4e44b39SDai Ngo 	spin_unlock(&nn->nfsd_ssc_lock);
1371df24ac7aSDai Ngo 	return status;
1372f4e44b39SDai Ngo }
1373f4e44b39SDai Ngo 
1374df24ac7aSDai Ngo static void nfsd4_ssc_update_dul(struct nfsd_net *nn,
1375df24ac7aSDai Ngo 				 struct nfsd4_ssc_umount_item *nsui,
1376df24ac7aSDai Ngo 				 struct vfsmount *ss_mnt)
1377f4e44b39SDai Ngo {
1378f4e44b39SDai Ngo 	spin_lock(&nn->nfsd_ssc_lock);
1379df24ac7aSDai Ngo 	nsui->nsui_vfsmount = ss_mnt;
1380df24ac7aSDai Ngo 	nsui->nsui_busy = false;
1381f4e44b39SDai Ngo 	wake_up_all(&nn->nfsd_ssc_waitq);
1382f4e44b39SDai Ngo 	spin_unlock(&nn->nfsd_ssc_lock);
1383f4e44b39SDai Ngo }
1384f4e44b39SDai Ngo 
1385df24ac7aSDai Ngo static void nfsd4_ssc_cancel_dul(struct nfsd_net *nn,
1386df24ac7aSDai Ngo 				 struct nfsd4_ssc_umount_item *nsui)
1387f4e44b39SDai Ngo {
1388f4e44b39SDai Ngo 	spin_lock(&nn->nfsd_ssc_lock);
1389df24ac7aSDai Ngo 	list_del(&nsui->nsui_list);
1390f4e44b39SDai Ngo 	wake_up_all(&nn->nfsd_ssc_waitq);
1391f4e44b39SDai Ngo 	spin_unlock(&nn->nfsd_ssc_lock);
1392df24ac7aSDai Ngo 	kfree(nsui);
1393f4e44b39SDai Ngo }
1394f4e44b39SDai Ngo 
1395f4e44b39SDai Ngo /*
1396ce0887acSOlga Kornievskaia  * Support one copy source server for now.
1397ce0887acSOlga Kornievskaia  */
1398ce0887acSOlga Kornievskaia static __be32
1399ce0887acSOlga Kornievskaia nfsd4_interssc_connect(struct nl4_server *nss, struct svc_rqst *rqstp,
1400df24ac7aSDai Ngo 		       struct nfsd4_ssc_umount_item **nsui)
1401ce0887acSOlga Kornievskaia {
1402ce0887acSOlga Kornievskaia 	struct file_system_type *type;
1403ce0887acSOlga Kornievskaia 	struct vfsmount *ss_mnt;
1404ce0887acSOlga Kornievskaia 	struct nfs42_netaddr *naddr;
1405ce0887acSOlga Kornievskaia 	struct sockaddr_storage tmp_addr;
1406ce0887acSOlga Kornievskaia 	size_t tmp_addrlen, match_netid_len = 3;
1407ce0887acSOlga Kornievskaia 	char *startsep = "", *endsep = "", *match_netid = "tcp";
1408ce0887acSOlga Kornievskaia 	char *ipaddr, *dev_name, *raw_data;
1409b8290ca2SOlga Kornievskaia 	int len, raw_len;
1410b8290ca2SOlga Kornievskaia 	__be32 status = nfserr_inval;
1411f4e44b39SDai Ngo 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1412ce0887acSOlga Kornievskaia 
1413ce0887acSOlga Kornievskaia 	naddr = &nss->u.nl4_addr;
1414ce0887acSOlga Kornievskaia 	tmp_addrlen = rpc_uaddr2sockaddr(SVC_NET(rqstp), naddr->addr,
1415ce0887acSOlga Kornievskaia 					 naddr->addr_len,
1416ce0887acSOlga Kornievskaia 					 (struct sockaddr *)&tmp_addr,
1417ce0887acSOlga Kornievskaia 					 sizeof(tmp_addr));
1418df24ac7aSDai Ngo 	*nsui = NULL;
1419ce0887acSOlga Kornievskaia 	if (tmp_addrlen == 0)
1420ce0887acSOlga Kornievskaia 		goto out_err;
1421ce0887acSOlga Kornievskaia 
1422ce0887acSOlga Kornievskaia 	if (tmp_addr.ss_family == AF_INET6) {
1423ce0887acSOlga Kornievskaia 		startsep = "[";
1424ce0887acSOlga Kornievskaia 		endsep = "]";
1425ce0887acSOlga Kornievskaia 		match_netid = "tcp6";
1426ce0887acSOlga Kornievskaia 		match_netid_len = 4;
1427ce0887acSOlga Kornievskaia 	}
1428ce0887acSOlga Kornievskaia 
1429ce0887acSOlga Kornievskaia 	if (naddr->netid_len != match_netid_len ||
1430ce0887acSOlga Kornievskaia 		strncmp(naddr->netid, match_netid, naddr->netid_len))
1431ce0887acSOlga Kornievskaia 		goto out_err;
1432ce0887acSOlga Kornievskaia 
1433ce0887acSOlga Kornievskaia 	/* Construct the raw data for the vfs_kern_mount call */
1434ce0887acSOlga Kornievskaia 	len = RPC_MAX_ADDRBUFLEN + 1;
1435ce0887acSOlga Kornievskaia 	ipaddr = kzalloc(len, GFP_KERNEL);
1436ce0887acSOlga Kornievskaia 	if (!ipaddr)
1437ce0887acSOlga Kornievskaia 		goto out_err;
1438ce0887acSOlga Kornievskaia 
1439ce0887acSOlga Kornievskaia 	rpc_ntop((struct sockaddr *)&tmp_addr, ipaddr, len);
1440ce0887acSOlga Kornievskaia 
1441ce0887acSOlga Kornievskaia 	/* 2 for ipv6 endsep and startsep. 3 for ":/" and trailing '/0'*/
1442ce0887acSOlga Kornievskaia 
1443ce0887acSOlga Kornievskaia 	raw_len = strlen(NFSD42_INTERSSC_MOUNTOPS) + strlen(ipaddr);
1444ce0887acSOlga Kornievskaia 	raw_data = kzalloc(raw_len, GFP_KERNEL);
1445ce0887acSOlga Kornievskaia 	if (!raw_data)
1446ce0887acSOlga Kornievskaia 		goto out_free_ipaddr;
1447ce0887acSOlga Kornievskaia 
1448ce0887acSOlga Kornievskaia 	snprintf(raw_data, raw_len, NFSD42_INTERSSC_MOUNTOPS, ipaddr);
1449ce0887acSOlga Kornievskaia 
1450b8290ca2SOlga Kornievskaia 	status = nfserr_nodev;
1451ce0887acSOlga Kornievskaia 	type = get_fs_type("nfs");
1452ce0887acSOlga Kornievskaia 	if (!type)
1453ce0887acSOlga Kornievskaia 		goto out_free_rawdata;
1454ce0887acSOlga Kornievskaia 
1455ce0887acSOlga Kornievskaia 	/* Set the server:<export> for the vfs_kern_mount call */
1456ce0887acSOlga Kornievskaia 	dev_name = kzalloc(len + 5, GFP_KERNEL);
1457ce0887acSOlga Kornievskaia 	if (!dev_name)
1458ce0887acSOlga Kornievskaia 		goto out_free_rawdata;
1459ce0887acSOlga Kornievskaia 	snprintf(dev_name, len + 5, "%s%s%s:/", startsep, ipaddr, endsep);
1460ce0887acSOlga Kornievskaia 
1461df24ac7aSDai Ngo 	status = nfsd4_ssc_setup_dul(nn, ipaddr, nsui);
1462f4e44b39SDai Ngo 	if (status)
1463f4e44b39SDai Ngo 		goto out_free_devname;
1464df24ac7aSDai Ngo 	if ((*nsui)->nsui_vfsmount)
1465f4e44b39SDai Ngo 		goto out_done;
1466f4e44b39SDai Ngo 
1467ce0887acSOlga Kornievskaia 	/* Use an 'internal' mount: SB_KERNMOUNT -> MNT_INTERNAL */
1468ce0887acSOlga Kornievskaia 	ss_mnt = vfs_kern_mount(type, SB_KERNMOUNT, dev_name, raw_data);
1469ce0887acSOlga Kornievskaia 	module_put(type->owner);
1470f4e44b39SDai Ngo 	if (IS_ERR(ss_mnt)) {
147154185267SWei Yongjun 		status = nfserr_nodev;
1472df24ac7aSDai Ngo 		nfsd4_ssc_cancel_dul(nn, *nsui);
1473ce0887acSOlga Kornievskaia 		goto out_free_devname;
1474f4e44b39SDai Ngo 	}
1475df24ac7aSDai Ngo 	nfsd4_ssc_update_dul(nn, *nsui, ss_mnt);
1476f4e44b39SDai Ngo out_done:
1477ce0887acSOlga Kornievskaia 	status = 0;
1478ce0887acSOlga Kornievskaia 
1479ce0887acSOlga Kornievskaia out_free_devname:
1480ce0887acSOlga Kornievskaia 	kfree(dev_name);
1481ce0887acSOlga Kornievskaia out_free_rawdata:
1482ce0887acSOlga Kornievskaia 	kfree(raw_data);
1483ce0887acSOlga Kornievskaia out_free_ipaddr:
1484ce0887acSOlga Kornievskaia 	kfree(ipaddr);
1485ce0887acSOlga Kornievskaia out_err:
1486ce0887acSOlga Kornievskaia 	return status;
1487ce0887acSOlga Kornievskaia }
1488ce0887acSOlga Kornievskaia 
1489f2453978SChuck Lever /*
1490ce0887acSOlga Kornievskaia  * Verify COPY destination stateid.
1491f2453978SChuck Lever  *
1492ce0887acSOlga Kornievskaia  * Connect to the source server with NFSv4.1.
1493ce0887acSOlga Kornievskaia  * Create the source struct file for nfsd_copy_range.
1494ce0887acSOlga Kornievskaia  * Called with COPY cstate:
1495ce0887acSOlga Kornievskaia  *    SAVED_FH: source filehandle
1496ce0887acSOlga Kornievskaia  *    CURRENT_FH: destination filehandle
1497ce0887acSOlga Kornievskaia  */
1498ce0887acSOlga Kornievskaia static __be32
1499ce0887acSOlga Kornievskaia nfsd4_setup_inter_ssc(struct svc_rqst *rqstp,
1500ce0887acSOlga Kornievskaia 		      struct nfsd4_compound_state *cstate,
1501df24ac7aSDai Ngo 		      struct nfsd4_copy *copy)
1502ce0887acSOlga Kornievskaia {
1503ce0887acSOlga Kornievskaia 	struct svc_fh *s_fh = NULL;
1504ce0887acSOlga Kornievskaia 	stateid_t *s_stid = &copy->cp_src_stateid;
1505b8290ca2SOlga Kornievskaia 	__be32 status = nfserr_inval;
1506ce0887acSOlga Kornievskaia 
1507ce0887acSOlga Kornievskaia 	/* Verify the destination stateid and set dst struct file*/
1508ce0887acSOlga Kornievskaia 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1509ce0887acSOlga Kornievskaia 					    &copy->cp_dst_stateid,
1510ce0887acSOlga Kornievskaia 					    WR_STATE, &copy->nf_dst, NULL);
1511ce0887acSOlga Kornievskaia 	if (status)
1512ce0887acSOlga Kornievskaia 		goto out;
1513ce0887acSOlga Kornievskaia 
1514df24ac7aSDai Ngo 	status = nfsd4_interssc_connect(copy->cp_src, rqstp, &copy->ss_nsui);
1515ce0887acSOlga Kornievskaia 	if (status)
1516ce0887acSOlga Kornievskaia 		goto out;
1517ce0887acSOlga Kornievskaia 
1518ce0887acSOlga Kornievskaia 	s_fh = &cstate->save_fh;
1519ce0887acSOlga Kornievskaia 
1520ce0887acSOlga Kornievskaia 	copy->c_fh.size = s_fh->fh_handle.fh_size;
1521d8b26071SNeilBrown 	memcpy(copy->c_fh.data, &s_fh->fh_handle.fh_raw, copy->c_fh.size);
15223f9544caSOlga Kornievskaia 	copy->stateid.seqid = cpu_to_be32(s_stid->si_generation);
1523ce0887acSOlga Kornievskaia 	memcpy(copy->stateid.other, (void *)&s_stid->si_opaque,
1524ce0887acSOlga Kornievskaia 	       sizeof(stateid_opaque_t));
1525ce0887acSOlga Kornievskaia 
1526ce0887acSOlga Kornievskaia 	status = 0;
1527ce0887acSOlga Kornievskaia out:
1528ce0887acSOlga Kornievskaia 	return status;
1529ce0887acSOlga Kornievskaia }
1530ce0887acSOlga Kornievskaia 
1531ce0887acSOlga Kornievskaia static void
1532df24ac7aSDai Ngo nfsd4_cleanup_inter_ssc(struct nfsd4_ssc_umount_item *nsui, struct file *filp,
1533ce0887acSOlga Kornievskaia 			struct nfsd_file *dst)
1534ce0887acSOlga Kornievskaia {
1535f4e44b39SDai Ngo 	struct nfsd_net *nn = net_generic(dst->nf_net, nfsd_net_id);
1536df24ac7aSDai Ngo 	long timeout = msecs_to_jiffies(nfsd4_ssc_umount_timeout);
1537f4e44b39SDai Ngo 
153824d796eaSChuck Lever 	nfs42_ssc_close(filp);
153924d796eaSChuck Lever 	fput(filp);
1540f4e44b39SDai Ngo 
1541f4e44b39SDai Ngo 	spin_lock(&nn->nfsd_ssc_lock);
1542df24ac7aSDai Ngo 	list_del(&nsui->nsui_list);
1543f4e44b39SDai Ngo 	/*
1544f4e44b39SDai Ngo 	 * vfsmount can be shared by multiple exports,
1545f4e44b39SDai Ngo 	 * decrement refcnt. If the count drops to 1 it
1546f4e44b39SDai Ngo 	 * will be unmounted when nsui_expire expires.
1547f4e44b39SDai Ngo 	 */
1548df24ac7aSDai Ngo 	refcount_dec(&nsui->nsui_refcnt);
1549df24ac7aSDai Ngo 	nsui->nsui_expire = jiffies + timeout;
1550df24ac7aSDai Ngo 	list_add_tail(&nsui->nsui_list, &nn->nfsd_ssc_mount_list);
1551f4e44b39SDai Ngo 	spin_unlock(&nn->nfsd_ssc_lock);
1552ce0887acSOlga Kornievskaia }
1553ce0887acSOlga Kornievskaia 
1554ce0887acSOlga Kornievskaia #else /* CONFIG_NFSD_V4_2_INTER_SSC */
1555ce0887acSOlga Kornievskaia 
1556ce0887acSOlga Kornievskaia static __be32
1557ce0887acSOlga Kornievskaia nfsd4_setup_inter_ssc(struct svc_rqst *rqstp,
1558ce0887acSOlga Kornievskaia 		      struct nfsd4_compound_state *cstate,
1559df24ac7aSDai Ngo 		      struct nfsd4_copy *copy)
1560ce0887acSOlga Kornievskaia {
1561b8290ca2SOlga Kornievskaia 	return nfserr_inval;
1562ce0887acSOlga Kornievskaia }
1563ce0887acSOlga Kornievskaia 
1564ce0887acSOlga Kornievskaia static void
1565df24ac7aSDai Ngo nfsd4_cleanup_inter_ssc(struct nfsd4_ssc_umount_item *nsui, struct file *filp,
1566ce0887acSOlga Kornievskaia 			struct nfsd_file *dst)
1567ce0887acSOlga Kornievskaia {
1568ce0887acSOlga Kornievskaia }
1569ce0887acSOlga Kornievskaia 
1570ce0887acSOlga Kornievskaia static struct file *nfs42_ssc_open(struct vfsmount *ss_mnt,
1571ce0887acSOlga Kornievskaia 				   struct nfs_fh *src_fh,
1572ce0887acSOlga Kornievskaia 				   nfs4_stateid *stateid)
1573ce0887acSOlga Kornievskaia {
1574ce0887acSOlga Kornievskaia 	return NULL;
1575ce0887acSOlga Kornievskaia }
1576ce0887acSOlga Kornievskaia #endif /* CONFIG_NFSD_V4_2_INTER_SSC */
1577ce0887acSOlga Kornievskaia 
1578ce0887acSOlga Kornievskaia static __be32
1579ce0887acSOlga Kornievskaia nfsd4_setup_intra_ssc(struct svc_rqst *rqstp,
1580ce0887acSOlga Kornievskaia 		      struct nfsd4_compound_state *cstate,
1581ce0887acSOlga Kornievskaia 		      struct nfsd4_copy *copy)
1582ce0887acSOlga Kornievskaia {
1583ce0887acSOlga Kornievskaia 	return nfsd4_verify_copy(rqstp, cstate, &copy->cp_src_stateid,
1584ce0887acSOlga Kornievskaia 				 &copy->nf_src, &copy->cp_dst_stateid,
1585ce0887acSOlga Kornievskaia 				 &copy->nf_dst);
1586ce0887acSOlga Kornievskaia }
1587ce0887acSOlga Kornievskaia 
1588e0639dc5SOlga Kornievskaia static void nfsd4_cb_offload_release(struct nfsd4_callback *cb)
1589e0639dc5SOlga Kornievskaia {
1590a11ada99SChuck Lever 	struct nfsd4_cb_offload *cbo =
1591a11ada99SChuck Lever 		container_of(cb, struct nfsd4_cb_offload, co_cb);
1592e0639dc5SOlga Kornievskaia 
1593a11ada99SChuck Lever 	kfree(cbo);
1594e0639dc5SOlga Kornievskaia }
1595e0639dc5SOlga Kornievskaia 
1596e0639dc5SOlga Kornievskaia static int nfsd4_cb_offload_done(struct nfsd4_callback *cb,
1597e0639dc5SOlga Kornievskaia 				 struct rpc_task *task)
1598e0639dc5SOlga Kornievskaia {
15991035d654SChuck Lever 	struct nfsd4_cb_offload *cbo =
16001035d654SChuck Lever 		container_of(cb, struct nfsd4_cb_offload, co_cb);
16011035d654SChuck Lever 
16021035d654SChuck Lever 	trace_nfsd_cb_offload_done(&cbo->co_res.cb_stateid, task);
1603e0639dc5SOlga Kornievskaia 	return 1;
1604e0639dc5SOlga Kornievskaia }
1605e0639dc5SOlga Kornievskaia 
1606e0639dc5SOlga Kornievskaia static const struct nfsd4_callback_ops nfsd4_cb_offload_ops = {
1607e0639dc5SOlga Kornievskaia 	.release = nfsd4_cb_offload_release,
1608e0639dc5SOlga Kornievskaia 	.done = nfsd4_cb_offload_done
1609e0639dc5SOlga Kornievskaia };
1610e0639dc5SOlga Kornievskaia 
1611e0639dc5SOlga Kornievskaia static void nfsd4_init_copy_res(struct nfsd4_copy *copy, bool sync)
1612e0639dc5SOlga Kornievskaia {
1613eac0b17aSOlga Kornievskaia 	copy->cp_res.wr_stable_how =
16141913cdf5SChuck Lever 		test_bit(NFSD4_COPY_F_COMMITTED, &copy->cp_flags) ?
16151913cdf5SChuck Lever 			NFS_FILE_SYNC : NFS_UNSTABLE;
16161913cdf5SChuck Lever 	nfsd4_copy_set_sync(copy, sync);
1617e0639dc5SOlga Kornievskaia }
1618e0639dc5SOlga Kornievskaia 
16193b7bf593SChuck Lever static ssize_t _nfsd_copy_file_range(struct nfsd4_copy *copy,
16203b7bf593SChuck Lever 				     struct file *dst,
16213b7bf593SChuck Lever 				     struct file *src)
1622e0639dc5SOlga Kornievskaia {
1623555dbf1aSTrond Myklebust 	errseq_t since;
1624e0639dc5SOlga Kornievskaia 	ssize_t bytes_copied = 0;
1625e7a833e9SJ. Bruce Fields 	u64 bytes_total = copy->cp_count;
1626e0639dc5SOlga Kornievskaia 	u64 src_pos = copy->cp_src_pos;
1627e0639dc5SOlga Kornievskaia 	u64 dst_pos = copy->cp_dst_pos;
1628c2f1c4bdSChuck Lever 	int status;
162979a1d88aSBrian Foster 	loff_t end;
1630e0639dc5SOlga Kornievskaia 
1631792a5112SJ. Bruce Fields 	/* See RFC 7862 p.67: */
1632792a5112SJ. Bruce Fields 	if (bytes_total == 0)
1633792a5112SJ. Bruce Fields 		bytes_total = ULLONG_MAX;
1634e0639dc5SOlga Kornievskaia 	do {
1635e0639dc5SOlga Kornievskaia 		if (kthread_should_stop())
1636e0639dc5SOlga Kornievskaia 			break;
1637555dbf1aSTrond Myklebust 		bytes_copied = nfsd_copy_file_range(src, src_pos, dst, dst_pos,
16385c4583b2SJeff Layton 						    bytes_total);
1639e0639dc5SOlga Kornievskaia 		if (bytes_copied <= 0)
1640e0639dc5SOlga Kornievskaia 			break;
1641e0639dc5SOlga Kornievskaia 		bytes_total -= bytes_copied;
1642e0639dc5SOlga Kornievskaia 		copy->cp_res.wr_bytes_written += bytes_copied;
1643e0639dc5SOlga Kornievskaia 		src_pos += bytes_copied;
1644e0639dc5SOlga Kornievskaia 		dst_pos += bytes_copied;
16451913cdf5SChuck Lever 	} while (bytes_total > 0 && nfsd4_copy_is_async(copy));
1646eac0b17aSOlga Kornievskaia 	/* for a non-zero asynchronous copy do a commit of data */
16471913cdf5SChuck Lever 	if (nfsd4_copy_is_async(copy) && copy->cp_res.wr_bytes_written > 0) {
1648555dbf1aSTrond Myklebust 		since = READ_ONCE(dst->f_wb_err);
164979a1d88aSBrian Foster 		end = copy->cp_dst_pos + copy->cp_res.wr_bytes_written - 1;
165079a1d88aSBrian Foster 		status = vfs_fsync_range(dst, copy->cp_dst_pos, end, 0);
1651555dbf1aSTrond Myklebust 		if (!status)
1652555dbf1aSTrond Myklebust 			status = filemap_check_wb_err(dst->f_mapping, since);
1653eac0b17aSOlga Kornievskaia 		if (!status)
16541913cdf5SChuck Lever 			set_bit(NFSD4_COPY_F_COMMITTED, &copy->cp_flags);
1655eac0b17aSOlga Kornievskaia 	}
1656e0639dc5SOlga Kornievskaia 	return bytes_copied;
1657e0639dc5SOlga Kornievskaia }
1658e0639dc5SOlga Kornievskaia 
16593b7bf593SChuck Lever static __be32 nfsd4_do_copy(struct nfsd4_copy *copy,
16603b7bf593SChuck Lever 			    struct file *src, struct file *dst,
16613b7bf593SChuck Lever 			    bool sync)
1662e0639dc5SOlga Kornievskaia {
1663e0639dc5SOlga Kornievskaia 	__be32 status;
1664e0639dc5SOlga Kornievskaia 	ssize_t bytes;
1665e0639dc5SOlga Kornievskaia 
16663b7bf593SChuck Lever 	bytes = _nfsd_copy_file_range(copy, dst, src);
16673b7bf593SChuck Lever 
1668e0639dc5SOlga Kornievskaia 	/* for async copy, we ignore the error, client can always retry
1669e0639dc5SOlga Kornievskaia 	 * to get the error
1670e0639dc5SOlga Kornievskaia 	 */
1671e0639dc5SOlga Kornievskaia 	if (bytes < 0 && !copy->cp_res.wr_bytes_written)
1672e0639dc5SOlga Kornievskaia 		status = nfserrno(bytes);
1673e0639dc5SOlga Kornievskaia 	else {
1674e0639dc5SOlga Kornievskaia 		nfsd4_init_copy_res(copy, sync);
1675e0639dc5SOlga Kornievskaia 		status = nfs_ok;
1676e0639dc5SOlga Kornievskaia 	}
1677e0639dc5SOlga Kornievskaia 	return status;
1678e0639dc5SOlga Kornievskaia }
1679e0639dc5SOlga Kornievskaia 
1680eb162e17SChuck Lever static void dup_copy_fields(struct nfsd4_copy *src, struct nfsd4_copy *dst)
1681e0639dc5SOlga Kornievskaia {
1682e0639dc5SOlga Kornievskaia 	dst->cp_src_pos = src->cp_src_pos;
1683e0639dc5SOlga Kornievskaia 	dst->cp_dst_pos = src->cp_dst_pos;
1684e0639dc5SOlga Kornievskaia 	dst->cp_count = src->cp_count;
16851913cdf5SChuck Lever 	dst->cp_flags = src->cp_flags;
1686e0639dc5SOlga Kornievskaia 	memcpy(&dst->cp_res, &src->cp_res, sizeof(src->cp_res));
1687e0639dc5SOlga Kornievskaia 	memcpy(&dst->fh, &src->fh, sizeof(src->fh));
1688e0639dc5SOlga Kornievskaia 	dst->cp_clp = src->cp_clp;
16895c4583b2SJeff Layton 	dst->nf_dst = nfsd_file_get(src->nf_dst);
16901913cdf5SChuck Lever 	/* for inter, nf_src doesn't exist yet */
16911913cdf5SChuck Lever 	if (!nfsd4_ssc_is_inter(src))
16925c4583b2SJeff Layton 		dst->nf_src = nfsd_file_get(src->nf_src);
1693ce0887acSOlga Kornievskaia 
1694e0639dc5SOlga Kornievskaia 	memcpy(&dst->cp_stateid, &src->cp_stateid, sizeof(src->cp_stateid));
169587689df6SChuck Lever 	memcpy(dst->cp_src, src->cp_src, sizeof(struct nl4_server));
1696ce0887acSOlga Kornievskaia 	memcpy(&dst->stateid, &src->stateid, sizeof(src->stateid));
1697ce0887acSOlga Kornievskaia 	memcpy(&dst->c_fh, &src->c_fh, sizeof(src->c_fh));
1698df24ac7aSDai Ngo 	dst->ss_nsui = src->ss_nsui;
1699e0639dc5SOlga Kornievskaia }
1700e0639dc5SOlga Kornievskaia 
17016ba434cbSJeff Layton static void release_copy_files(struct nfsd4_copy *copy)
17026ba434cbSJeff Layton {
17036ba434cbSJeff Layton 	if (copy->nf_src)
17046ba434cbSJeff Layton 		nfsd_file_put(copy->nf_src);
17056ba434cbSJeff Layton 	if (copy->nf_dst)
17066ba434cbSJeff Layton 		nfsd_file_put(copy->nf_dst);
17076ba434cbSJeff Layton }
17086ba434cbSJeff Layton 
1709e0639dc5SOlga Kornievskaia static void cleanup_async_copy(struct nfsd4_copy *copy)
1710e0639dc5SOlga Kornievskaia {
1711624322f1SOlga Kornievskaia 	nfs4_free_copy_state(copy);
17126ba434cbSJeff Layton 	release_copy_files(copy);
171381e72297SDai Ngo 	if (copy->cp_clp) {
1714e0639dc5SOlga Kornievskaia 		spin_lock(&copy->cp_clp->async_lock);
171581e72297SDai Ngo 		if (!list_empty(&copy->copies))
171681e72297SDai Ngo 			list_del_init(&copy->copies);
1717e0639dc5SOlga Kornievskaia 		spin_unlock(&copy->cp_clp->async_lock);
171881e72297SDai Ngo 	}
1719e0639dc5SOlga Kornievskaia 	nfs4_put_copy(copy);
1720e0639dc5SOlga Kornievskaia }
1721e0639dc5SOlga Kornievskaia 
1722a11ada99SChuck Lever static void nfsd4_send_cb_offload(struct nfsd4_copy *copy, __be32 nfserr)
1723e72f9bc0SChuck Lever {
1724a11ada99SChuck Lever 	struct nfsd4_cb_offload *cbo;
1725e72f9bc0SChuck Lever 
1726a11ada99SChuck Lever 	cbo = kzalloc(sizeof(*cbo), GFP_KERNEL);
1727a11ada99SChuck Lever 	if (!cbo)
1728e72f9bc0SChuck Lever 		return;
1729e72f9bc0SChuck Lever 
1730a11ada99SChuck Lever 	memcpy(&cbo->co_res, &copy->cp_res, sizeof(copy->cp_res));
1731a11ada99SChuck Lever 	memcpy(&cbo->co_fh, &copy->fh, sizeof(copy->fh));
1732a11ada99SChuck Lever 	cbo->co_nfserr = nfserr;
1733e72f9bc0SChuck Lever 
1734a11ada99SChuck Lever 	nfsd4_init_cb(&cbo->co_cb, copy->cp_clp, &nfsd4_cb_offload_ops,
1735a11ada99SChuck Lever 		      NFSPROC4_CLNT_CB_OFFLOAD);
1736a11ada99SChuck Lever 	trace_nfsd_cb_offload(copy->cp_clp, &cbo->co_res.cb_stateid,
1737a11ada99SChuck Lever 			      &cbo->co_fh, copy->cp_count, nfserr);
1738a11ada99SChuck Lever 	nfsd4_run_cb(&cbo->co_cb);
1739e72f9bc0SChuck Lever }
1740e72f9bc0SChuck Lever 
1741ad1e46c9SChuck Lever /**
1742ad1e46c9SChuck Lever  * nfsd4_do_async_copy - kthread function for background server-side COPY
1743ad1e46c9SChuck Lever  * @data: arguments for COPY operation
1744ad1e46c9SChuck Lever  *
1745ad1e46c9SChuck Lever  * Return values:
1746ad1e46c9SChuck Lever  *   %0: Copy operation is done.
1747ad1e46c9SChuck Lever  */
1748e0639dc5SOlga Kornievskaia static int nfsd4_do_async_copy(void *data)
1749e0639dc5SOlga Kornievskaia {
1750e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy = (struct nfsd4_copy *)data;
1751a11ada99SChuck Lever 	__be32 nfserr;
1752e0639dc5SOlga Kornievskaia 
17531913cdf5SChuck Lever 	if (nfsd4_ssc_is_inter(copy)) {
1754ad1e46c9SChuck Lever 		struct file *filp;
1755ad1e46c9SChuck Lever 
1756df24ac7aSDai Ngo 		filp = nfs42_ssc_open(copy->ss_nsui->nsui_vfsmount,
1757df24ac7aSDai Ngo 				      &copy->c_fh, &copy->stateid);
1758ad1e46c9SChuck Lever 		if (IS_ERR(filp)) {
1759754035ffSOlga Kornievskaia 			switch (PTR_ERR(filp)) {
1760754035ffSOlga Kornievskaia 			case -EBADF:
1761754035ffSOlga Kornievskaia 				nfserr = nfserr_wrong_type;
1762754035ffSOlga Kornievskaia 				break;
1763754035ffSOlga Kornievskaia 			default:
1764a11ada99SChuck Lever 				nfserr = nfserr_offload_denied;
1765754035ffSOlga Kornievskaia 			}
176675333d48SDai Ngo 			/* ss_mnt will be unmounted by the laundromat */
1767ce0887acSOlga Kornievskaia 			goto do_callback;
1768ce0887acSOlga Kornievskaia 		}
1769a11ada99SChuck Lever 		nfserr = nfsd4_do_copy(copy, filp, copy->nf_dst->nf_file,
1770a11ada99SChuck Lever 				       false);
1771df24ac7aSDai Ngo 		nfsd4_cleanup_inter_ssc(copy->ss_nsui, filp, copy->nf_dst);
1772478ed7b1SChuck Lever 	} else {
1773a11ada99SChuck Lever 		nfserr = nfsd4_do_copy(copy, copy->nf_src->nf_file,
17743b7bf593SChuck Lever 				       copy->nf_dst->nf_file, false);
1775ce0887acSOlga Kornievskaia 	}
1776ce0887acSOlga Kornievskaia 
1777ce0887acSOlga Kornievskaia do_callback:
1778a11ada99SChuck Lever 	nfsd4_send_cb_offload(copy, nfserr);
1779e0639dc5SOlga Kornievskaia 	cleanup_async_copy(copy);
1780e0639dc5SOlga Kornievskaia 	return 0;
1781e0639dc5SOlga Kornievskaia }
1782e0639dc5SOlga Kornievskaia 
1783ffa0160aSChristoph Hellwig static __be32
178429ae7f9dSAnna Schumaker nfsd4_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1785eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
178629ae7f9dSAnna Schumaker {
1787*20a10c78SChuck Lever 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1788e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *async_copy = NULL;
1789*20a10c78SChuck Lever 	struct nfsd4_copy *copy = &u->copy;
1790*20a10c78SChuck Lever 	struct nfsd42_write_res *result;
1791*20a10c78SChuck Lever 	__be32 status;
1792*20a10c78SChuck Lever 
1793*20a10c78SChuck Lever 	result = &copy->cp_res;
1794*20a10c78SChuck Lever 	nfsd_copy_write_verifier((__be32 *)&result->wr_verifier.data, nn);
179529ae7f9dSAnna Schumaker 
179674115b3eSDai Ngo 	copy->cp_clp = cstate->clp;
17971913cdf5SChuck Lever 	if (nfsd4_ssc_is_inter(copy)) {
17981913cdf5SChuck Lever 		if (!inter_copy_offload_enable || nfsd4_copy_is_sync(copy)) {
1799ce0887acSOlga Kornievskaia 			status = nfserr_notsupp;
180029ae7f9dSAnna Schumaker 			goto out;
1801ce0887acSOlga Kornievskaia 		}
1802df24ac7aSDai Ngo 		status = nfsd4_setup_inter_ssc(rqstp, cstate, copy);
1803ce0887acSOlga Kornievskaia 		if (status)
1804ce0887acSOlga Kornievskaia 			return nfserr_offload_denied;
1805ce0887acSOlga Kornievskaia 	} else {
1806ce0887acSOlga Kornievskaia 		status = nfsd4_setup_intra_ssc(rqstp, cstate, copy);
1807ce0887acSOlga Kornievskaia 		if (status)
1808ce0887acSOlga Kornievskaia 			return status;
1809ce0887acSOlga Kornievskaia 	}
181029ae7f9dSAnna Schumaker 
1811e0639dc5SOlga Kornievskaia 	memcpy(&copy->fh, &cstate->current_fh.fh_handle,
1812e0639dc5SOlga Kornievskaia 		sizeof(struct knfsd_fh));
18131913cdf5SChuck Lever 	if (nfsd4_copy_is_async(copy)) {
1814e0639dc5SOlga Kornievskaia 		status = nfserrno(-ENOMEM);
1815e0639dc5SOlga Kornievskaia 		async_copy = kzalloc(sizeof(struct nfsd4_copy), GFP_KERNEL);
1816e0639dc5SOlga Kornievskaia 		if (!async_copy)
1817ce0887acSOlga Kornievskaia 			goto out_err;
181881e72297SDai Ngo 		INIT_LIST_HEAD(&async_copy->copies);
181981e72297SDai Ngo 		refcount_set(&async_copy->refcount, 1);
182087689df6SChuck Lever 		async_copy->cp_src = kmalloc(sizeof(*async_copy->cp_src), GFP_KERNEL);
182187689df6SChuck Lever 		if (!async_copy->cp_src)
182287689df6SChuck Lever 			goto out_err;
1823ce0887acSOlga Kornievskaia 		if (!nfs4_init_copy_state(nn, copy))
1824ce0887acSOlga Kornievskaia 			goto out_err;
1825*20a10c78SChuck Lever 		memcpy(&result->cb_stateid, &copy->cp_stateid.cs_stid,
1826*20a10c78SChuck Lever 			sizeof(result->cb_stateid));
1827eb162e17SChuck Lever 		dup_copy_fields(copy, async_copy);
1828e0639dc5SOlga Kornievskaia 		async_copy->copy_task = kthread_create(nfsd4_do_async_copy,
1829e0639dc5SOlga Kornievskaia 				async_copy, "%s", "copy thread");
1830e0639dc5SOlga Kornievskaia 		if (IS_ERR(async_copy->copy_task))
1831e0639dc5SOlga Kornievskaia 			goto out_err;
1832e0639dc5SOlga Kornievskaia 		spin_lock(&async_copy->cp_clp->async_lock);
1833e0639dc5SOlga Kornievskaia 		list_add(&async_copy->copies,
1834e0639dc5SOlga Kornievskaia 				&async_copy->cp_clp->async_copies);
1835e0639dc5SOlga Kornievskaia 		spin_unlock(&async_copy->cp_clp->async_lock);
1836e0639dc5SOlga Kornievskaia 		wake_up_process(async_copy->copy_task);
1837e0639dc5SOlga Kornievskaia 		status = nfs_ok;
1838ce0887acSOlga Kornievskaia 	} else {
18393b7bf593SChuck Lever 		status = nfsd4_do_copy(copy, copy->nf_src->nf_file,
18403b7bf593SChuck Lever 				       copy->nf_dst->nf_file, true);
1841ce0887acSOlga Kornievskaia 	}
184229ae7f9dSAnna Schumaker out:
18436ba434cbSJeff Layton 	release_copy_files(copy);
184429ae7f9dSAnna Schumaker 	return status;
1845e0639dc5SOlga Kornievskaia out_err:
184634e8f9ecSDai Ngo 	if (nfsd4_ssc_is_inter(copy)) {
184734e8f9ecSDai Ngo 		/*
184834e8f9ecSDai Ngo 		 * Source's vfsmount of inter-copy will be unmounted
184934e8f9ecSDai Ngo 		 * by the laundromat. Use copy instead of async_copy
185034e8f9ecSDai Ngo 		 * since async_copy->ss_nsui might not be set yet.
185134e8f9ecSDai Ngo 		 */
185234e8f9ecSDai Ngo 		refcount_dec(&copy->ss_nsui->nsui_refcnt);
185334e8f9ecSDai Ngo 	}
185418f428d4SOlga Kornievskaia 	if (async_copy)
1855e0639dc5SOlga Kornievskaia 		cleanup_async_copy(async_copy);
1856ce0887acSOlga Kornievskaia 	status = nfserrno(-ENOMEM);
1857e0639dc5SOlga Kornievskaia 	goto out;
1858e0639dc5SOlga Kornievskaia }
1859e0639dc5SOlga Kornievskaia 
1860fcb53097SJeff Layton static struct nfsd4_copy *
1861fcb53097SJeff Layton find_async_copy_locked(struct nfs4_client *clp, stateid_t *stateid)
1862fcb53097SJeff Layton {
1863fcb53097SJeff Layton 	struct nfsd4_copy *copy;
1864fcb53097SJeff Layton 
1865fcb53097SJeff Layton 	lockdep_assert_held(&clp->async_lock);
1866fcb53097SJeff Layton 
1867fcb53097SJeff Layton 	list_for_each_entry(copy, &clp->async_copies, copies) {
1868fcb53097SJeff Layton 		if (memcmp(&copy->cp_stateid.cs_stid, stateid, NFS4_STATEID_SIZE))
1869fcb53097SJeff Layton 			continue;
1870fcb53097SJeff Layton 		return copy;
1871fcb53097SJeff Layton 	}
1872fcb53097SJeff Layton 	return NULL;
1873fcb53097SJeff Layton }
1874fcb53097SJeff Layton 
1875fcb53097SJeff Layton static struct nfsd4_copy *
1876e0639dc5SOlga Kornievskaia find_async_copy(struct nfs4_client *clp, stateid_t *stateid)
1877e0639dc5SOlga Kornievskaia {
1878e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy;
1879e0639dc5SOlga Kornievskaia 
1880e0639dc5SOlga Kornievskaia 	spin_lock(&clp->async_lock);
1881fcb53097SJeff Layton 	copy = find_async_copy_locked(clp, stateid);
1882fcb53097SJeff Layton 	if (copy)
1883e0639dc5SOlga Kornievskaia 		refcount_inc(&copy->refcount);
1884e0639dc5SOlga Kornievskaia 	spin_unlock(&clp->async_lock);
1885e0639dc5SOlga Kornievskaia 	return copy;
1886e0639dc5SOlga Kornievskaia }
188729ae7f9dSAnna Schumaker 
188829ae7f9dSAnna Schumaker static __be32
1889885e2bf3SOlga Kornievskaia nfsd4_offload_cancel(struct svc_rqst *rqstp,
1890885e2bf3SOlga Kornievskaia 		     struct nfsd4_compound_state *cstate,
1891885e2bf3SOlga Kornievskaia 		     union nfsd4_op_u *u)
1892885e2bf3SOlga Kornievskaia {
1893e0639dc5SOlga Kornievskaia 	struct nfsd4_offload_status *os = &u->offload_status;
1894e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy;
1895e0639dc5SOlga Kornievskaia 	struct nfs4_client *clp = cstate->clp;
1896e0639dc5SOlga Kornievskaia 
1897e0639dc5SOlga Kornievskaia 	copy = find_async_copy(clp, &os->stateid);
1898ce0887acSOlga Kornievskaia 	if (!copy) {
1899ce0887acSOlga Kornievskaia 		struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1900e0639dc5SOlga Kornievskaia 
1901ce0887acSOlga Kornievskaia 		return manage_cpntf_state(nn, &os->stateid, clp, NULL);
1902ce0887acSOlga Kornievskaia 	} else
1903ce0887acSOlga Kornievskaia 		nfsd4_stop_copy(copy);
1904ce0887acSOlga Kornievskaia 
1905ce0887acSOlga Kornievskaia 	return nfs_ok;
1906885e2bf3SOlga Kornievskaia }
1907885e2bf3SOlga Kornievskaia 
1908885e2bf3SOlga Kornievskaia static __be32
190951911868SOlga Kornievskaia nfsd4_copy_notify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
191051911868SOlga Kornievskaia 		  union nfsd4_op_u *u)
191151911868SOlga Kornievskaia {
1912624322f1SOlga Kornievskaia 	struct nfsd4_copy_notify *cn = &u->copy_notify;
1913624322f1SOlga Kornievskaia 	__be32 status;
1914624322f1SOlga Kornievskaia 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1915624322f1SOlga Kornievskaia 	struct nfs4_stid *stid;
1916624322f1SOlga Kornievskaia 	struct nfs4_cpntf_state *cps;
1917624322f1SOlga Kornievskaia 	struct nfs4_client *clp = cstate->clp;
1918624322f1SOlga Kornievskaia 
1919624322f1SOlga Kornievskaia 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1920624322f1SOlga Kornievskaia 					&cn->cpn_src_stateid, RD_STATE, NULL,
1921624322f1SOlga Kornievskaia 					&stid);
1922624322f1SOlga Kornievskaia 	if (status)
1923624322f1SOlga Kornievskaia 		return status;
1924624322f1SOlga Kornievskaia 
1925624322f1SOlga Kornievskaia 	cn->cpn_sec = nn->nfsd4_lease;
1926624322f1SOlga Kornievskaia 	cn->cpn_nsec = 0;
1927624322f1SOlga Kornievskaia 
1928624322f1SOlga Kornievskaia 	status = nfserrno(-ENOMEM);
1929624322f1SOlga Kornievskaia 	cps = nfs4_alloc_init_cpntf_state(nn, stid);
1930624322f1SOlga Kornievskaia 	if (!cps)
1931624322f1SOlga Kornievskaia 		goto out;
1932781fde1aSChuck Lever 	memcpy(&cn->cpn_cnr_stateid, &cps->cp_stateid.cs_stid, sizeof(stateid_t));
1933624322f1SOlga Kornievskaia 	memcpy(&cps->cp_p_stateid, &stid->sc_stateid, sizeof(stateid_t));
1934624322f1SOlga Kornievskaia 	memcpy(&cps->cp_p_clid, &clp->cl_clientid, sizeof(clientid_t));
1935624322f1SOlga Kornievskaia 
1936624322f1SOlga Kornievskaia 	/* For now, only return one server address in cpn_src, the
1937624322f1SOlga Kornievskaia 	 * address used by the client to connect to this server.
1938624322f1SOlga Kornievskaia 	 */
193909426ef2SChuck Lever 	cn->cpn_src->nl4_type = NL4_NETADDR;
1940624322f1SOlga Kornievskaia 	status = nfsd4_set_netaddr((struct sockaddr *)&rqstp->rq_daddr,
194109426ef2SChuck Lever 				 &cn->cpn_src->u.nl4_addr);
1942624322f1SOlga Kornievskaia 	WARN_ON_ONCE(status);
1943624322f1SOlga Kornievskaia 	if (status) {
1944624322f1SOlga Kornievskaia 		nfs4_put_cpntf_state(nn, cps);
1945624322f1SOlga Kornievskaia 		goto out;
1946624322f1SOlga Kornievskaia 	}
1947624322f1SOlga Kornievskaia out:
1948624322f1SOlga Kornievskaia 	nfs4_put_stid(stid);
1949624322f1SOlga Kornievskaia 	return status;
195051911868SOlga Kornievskaia }
195151911868SOlga Kornievskaia 
195251911868SOlga Kornievskaia static __be32
195395d871f0SAnna Schumaker nfsd4_fallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
195495d871f0SAnna Schumaker 		struct nfsd4_fallocate *fallocate, int flags)
195595d871f0SAnna Schumaker {
19560d4d6720SJ. Bruce Fields 	__be32 status;
19575c4583b2SJeff Layton 	struct nfsd_file *nf;
195895d871f0SAnna Schumaker 
1959aa0d6aedSAnna Schumaker 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
196095d871f0SAnna Schumaker 					    &fallocate->falloc_stateid,
1961624322f1SOlga Kornievskaia 					    WR_STATE, &nf, NULL);
1962eeff73f7SChuck Lever 	if (status != nfs_ok)
196395d871f0SAnna Schumaker 		return status;
196495d871f0SAnna Schumaker 
19655c4583b2SJeff Layton 	status = nfsd4_vfs_fallocate(rqstp, &cstate->current_fh, nf->nf_file,
196695d871f0SAnna Schumaker 				     fallocate->falloc_offset,
196795d871f0SAnna Schumaker 				     fallocate->falloc_length,
196895d871f0SAnna Schumaker 				     flags);
19695c4583b2SJeff Layton 	nfsd_file_put(nf);
197095d871f0SAnna Schumaker 	return status;
197195d871f0SAnna Schumaker }
1972fcb53097SJeff Layton 
19736308bc98SOlga Kornievskaia static __be32
19746308bc98SOlga Kornievskaia nfsd4_offload_status(struct svc_rqst *rqstp,
19756308bc98SOlga Kornievskaia 		     struct nfsd4_compound_state *cstate,
19766308bc98SOlga Kornievskaia 		     union nfsd4_op_u *u)
19776308bc98SOlga Kornievskaia {
1978e0639dc5SOlga Kornievskaia 	struct nfsd4_offload_status *os = &u->offload_status;
1979fcb53097SJeff Layton 	__be32 status = nfs_ok;
1980e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy;
1981e0639dc5SOlga Kornievskaia 	struct nfs4_client *clp = cstate->clp;
1982e0639dc5SOlga Kornievskaia 
1983fcb53097SJeff Layton 	spin_lock(&clp->async_lock);
1984fcb53097SJeff Layton 	copy = find_async_copy_locked(clp, &os->stateid);
1985fcb53097SJeff Layton 	if (copy)
1986e0639dc5SOlga Kornievskaia 		os->count = copy->cp_res.wr_bytes_written;
1987fcb53097SJeff Layton 	else
1988e0639dc5SOlga Kornievskaia 		status = nfserr_bad_stateid;
1989fcb53097SJeff Layton 	spin_unlock(&clp->async_lock);
1990e0639dc5SOlga Kornievskaia 
1991e0639dc5SOlga Kornievskaia 	return status;
19926308bc98SOlga Kornievskaia }
199395d871f0SAnna Schumaker 
199495d871f0SAnna Schumaker static __be32
199595d871f0SAnna Schumaker nfsd4_allocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1996eb69853dSChristoph Hellwig 	       union nfsd4_op_u *u)
199795d871f0SAnna Schumaker {
1998eb69853dSChristoph Hellwig 	return nfsd4_fallocate(rqstp, cstate, &u->allocate, 0);
199995d871f0SAnna Schumaker }
200095d871f0SAnna Schumaker 
200195d871f0SAnna Schumaker static __be32
2002b0cb9085SAnna Schumaker nfsd4_deallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2003eb69853dSChristoph Hellwig 		 union nfsd4_op_u *u)
2004b0cb9085SAnna Schumaker {
2005eb69853dSChristoph Hellwig 	return nfsd4_fallocate(rqstp, cstate, &u->deallocate,
2006b0cb9085SAnna Schumaker 			       FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
2007b0cb9085SAnna Schumaker }
2008b0cb9085SAnna Schumaker 
2009b0cb9085SAnna Schumaker static __be32
201024bab491SAnna Schumaker nfsd4_seek(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2011eb69853dSChristoph Hellwig 	   union nfsd4_op_u *u)
201224bab491SAnna Schumaker {
2013eb69853dSChristoph Hellwig 	struct nfsd4_seek *seek = &u->seek;
201424bab491SAnna Schumaker 	int whence;
201524bab491SAnna Schumaker 	__be32 status;
20165c4583b2SJeff Layton 	struct nfsd_file *nf;
201724bab491SAnna Schumaker 
2018aa0d6aedSAnna Schumaker 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
201924bab491SAnna Schumaker 					    &seek->seek_stateid,
2020624322f1SOlga Kornievskaia 					    RD_STATE, &nf, NULL);
2021eeff73f7SChuck Lever 	if (status)
202224bab491SAnna Schumaker 		return status;
202324bab491SAnna Schumaker 
202424bab491SAnna Schumaker 	switch (seek->seek_whence) {
202524bab491SAnna Schumaker 	case NFS4_CONTENT_DATA:
202624bab491SAnna Schumaker 		whence = SEEK_DATA;
202724bab491SAnna Schumaker 		break;
202824bab491SAnna Schumaker 	case NFS4_CONTENT_HOLE:
202924bab491SAnna Schumaker 		whence = SEEK_HOLE;
203024bab491SAnna Schumaker 		break;
203124bab491SAnna Schumaker 	default:
203224bab491SAnna Schumaker 		status = nfserr_union_notsupp;
203324bab491SAnna Schumaker 		goto out;
203424bab491SAnna Schumaker 	}
203524bab491SAnna Schumaker 
203624bab491SAnna Schumaker 	/*
203724bab491SAnna Schumaker 	 * Note:  This call does change file->f_pos, but nothing in NFSD
203824bab491SAnna Schumaker 	 *        should ever file->f_pos.
203924bab491SAnna Schumaker 	 */
20405c4583b2SJeff Layton 	seek->seek_pos = vfs_llseek(nf->nf_file, seek->seek_offset, whence);
204124bab491SAnna Schumaker 	if (seek->seek_pos < 0)
204224bab491SAnna Schumaker 		status = nfserrno(seek->seek_pos);
20435c4583b2SJeff Layton 	else if (seek->seek_pos >= i_size_read(file_inode(nf->nf_file)))
204424bab491SAnna Schumaker 		seek->seek_eof = true;
204524bab491SAnna Schumaker 
204624bab491SAnna Schumaker out:
20475c4583b2SJeff Layton 	nfsd_file_put(nf);
204824bab491SAnna Schumaker 	return status;
204924bab491SAnna Schumaker }
205024bab491SAnna Schumaker 
20511da177e4SLinus Torvalds /* This routine never returns NFS_OK!  If there are no other errors, it
20521da177e4SLinus Torvalds  * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
20531da177e4SLinus Torvalds  * attributes matched.  VERIFY is implemented by mapping NFSERR_SAME
20541da177e4SLinus Torvalds  * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
20551da177e4SLinus Torvalds  */
2056b37ad28bSAl Viro static __be32
2057c954e2a5SJ.Bruce Fields _nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2058ca364317SJ.Bruce Fields 	     struct nfsd4_verify *verify)
20591da177e4SLinus Torvalds {
20602ebbc012SAl Viro 	__be32 *buf, *p;
20611da177e4SLinus Torvalds 	int count;
2062b37ad28bSAl Viro 	__be32 status;
20631da177e4SLinus Torvalds 
20648837abcaSMiklos Szeredi 	status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
20651da177e4SLinus Torvalds 	if (status)
20661da177e4SLinus Torvalds 		return status;
20671da177e4SLinus Torvalds 
20683c8e0316SYu Zhiguo 	status = check_attr_support(rqstp, cstate, verify->ve_bmval, NULL);
20693c8e0316SYu Zhiguo 	if (status)
20703c8e0316SYu Zhiguo 		return status;
20713c8e0316SYu Zhiguo 
20721da177e4SLinus Torvalds 	if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
20731da177e4SLinus Torvalds 	    || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
20741da177e4SLinus Torvalds 		return nfserr_inval;
20751da177e4SLinus Torvalds 	if (verify->ve_attrlen & 3)
20761da177e4SLinus Torvalds 		return nfserr_inval;
20771da177e4SLinus Torvalds 
20781da177e4SLinus Torvalds 	/* count in words:
20791da177e4SLinus Torvalds 	 *   bitmap_len(1) + bitmap(2) + attr_len(1) = 4
20801da177e4SLinus Torvalds 	 */
20811da177e4SLinus Torvalds 	count = 4 + (verify->ve_attrlen >> 2);
20821da177e4SLinus Torvalds 	buf = kmalloc(count << 2, GFP_KERNEL);
20831da177e4SLinus Torvalds 	if (!buf)
20843e772463SJ. Bruce Fields 		return nfserr_jukebox;
20851da177e4SLinus Torvalds 
208684822d0bSJ. Bruce Fields 	p = buf;
2087d5184658SJ. Bruce Fields 	status = nfsd4_encode_fattr_to_buf(&p, count, &cstate->current_fh,
2088ca364317SJ.Bruce Fields 				    cstate->current_fh.fh_export,
2089d5184658SJ. Bruce Fields 				    cstate->current_fh.fh_dentry,
2090d5184658SJ. Bruce Fields 				    verify->ve_bmval,
2091406a7ea9SFrank Filz 				    rqstp, 0);
209241ae6e71SJ. Bruce Fields 	/*
209341ae6e71SJ. Bruce Fields 	 * If nfsd4_encode_fattr() ran out of space, assume that's because
209441ae6e71SJ. Bruce Fields 	 * the attributes are longer (hence different) than those given:
209541ae6e71SJ. Bruce Fields 	 */
209684822d0bSJ. Bruce Fields 	if (status == nfserr_resource)
20971da177e4SLinus Torvalds 		status = nfserr_not_same;
20981da177e4SLinus Torvalds 	if (status)
20991da177e4SLinus Torvalds 		goto out_kfree;
21001da177e4SLinus Torvalds 
210195ec28cdSBenny Halevy 	/* skip bitmap */
210295ec28cdSBenny Halevy 	p = buf + 1 + ntohl(buf[0]);
21031da177e4SLinus Torvalds 	status = nfserr_not_same;
21041da177e4SLinus Torvalds 	if (ntohl(*p++) != verify->ve_attrlen)
21051da177e4SLinus Torvalds 		goto out_kfree;
21061da177e4SLinus Torvalds 	if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
21071da177e4SLinus Torvalds 		status = nfserr_same;
21081da177e4SLinus Torvalds 
21091da177e4SLinus Torvalds out_kfree:
21101da177e4SLinus Torvalds 	kfree(buf);
21111da177e4SLinus Torvalds 	return status;
21121da177e4SLinus Torvalds }
21131da177e4SLinus Torvalds 
2114c954e2a5SJ.Bruce Fields static __be32
2115c954e2a5SJ.Bruce Fields nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2116eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
2117c954e2a5SJ.Bruce Fields {
2118c954e2a5SJ.Bruce Fields 	__be32 status;
2119c954e2a5SJ.Bruce Fields 
2120eb69853dSChristoph Hellwig 	status = _nfsd4_verify(rqstp, cstate, &u->verify);
2121c954e2a5SJ.Bruce Fields 	return status == nfserr_not_same ? nfs_ok : status;
2122c954e2a5SJ.Bruce Fields }
2123c954e2a5SJ.Bruce Fields 
2124c954e2a5SJ.Bruce Fields static __be32
2125c954e2a5SJ.Bruce Fields nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2126eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
2127c954e2a5SJ.Bruce Fields {
2128c954e2a5SJ.Bruce Fields 	__be32 status;
2129c954e2a5SJ.Bruce Fields 
2130eb69853dSChristoph Hellwig 	status = _nfsd4_verify(rqstp, cstate, &u->nverify);
2131c954e2a5SJ.Bruce Fields 	return status == nfserr_same ? nfs_ok : status;
2132c954e2a5SJ.Bruce Fields }
2133c954e2a5SJ.Bruce Fields 
21349cf514ccSChristoph Hellwig #ifdef CONFIG_NFSD_PNFS
21359cf514ccSChristoph Hellwig static const struct nfsd4_layout_ops *
21369cf514ccSChristoph Hellwig nfsd4_layout_verify(struct svc_export *exp, unsigned int layout_type)
21379cf514ccSChristoph Hellwig {
21388a4c3926SJeff Layton 	if (!exp->ex_layout_types) {
21399cf514ccSChristoph Hellwig 		dprintk("%s: export does not support pNFS\n", __func__);
21409cf514ccSChristoph Hellwig 		return NULL;
21419cf514ccSChristoph Hellwig 	}
21429cf514ccSChristoph Hellwig 
2143b550a32eSAri Kauppi 	if (layout_type >= LAYOUT_TYPE_MAX ||
2144b550a32eSAri Kauppi 	    !(exp->ex_layout_types & (1 << layout_type))) {
21459cf514ccSChristoph Hellwig 		dprintk("%s: layout type %d not supported\n",
21469cf514ccSChristoph Hellwig 			__func__, layout_type);
21479cf514ccSChristoph Hellwig 		return NULL;
21489cf514ccSChristoph Hellwig 	}
21499cf514ccSChristoph Hellwig 
21509cf514ccSChristoph Hellwig 	return nfsd4_layout_ops[layout_type];
21519cf514ccSChristoph Hellwig }
21529cf514ccSChristoph Hellwig 
21539cf514ccSChristoph Hellwig static __be32
21549cf514ccSChristoph Hellwig nfsd4_getdeviceinfo(struct svc_rqst *rqstp,
2155eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
21569cf514ccSChristoph Hellwig {
2157eb69853dSChristoph Hellwig 	struct nfsd4_getdeviceinfo *gdp = &u->getdeviceinfo;
21589cf514ccSChristoph Hellwig 	const struct nfsd4_layout_ops *ops;
21599cf514ccSChristoph Hellwig 	struct nfsd4_deviceid_map *map;
21609cf514ccSChristoph Hellwig 	struct svc_export *exp;
21619cf514ccSChristoph Hellwig 	__be32 nfserr;
21629cf514ccSChristoph Hellwig 
21639cf514ccSChristoph Hellwig 	dprintk("%s: layout_type %u dev_id [0x%llx:0x%x] maxcnt %u\n",
21649cf514ccSChristoph Hellwig 	       __func__,
21659cf514ccSChristoph Hellwig 	       gdp->gd_layout_type,
21669cf514ccSChristoph Hellwig 	       gdp->gd_devid.fsid_idx, gdp->gd_devid.generation,
21679cf514ccSChristoph Hellwig 	       gdp->gd_maxcount);
21689cf514ccSChristoph Hellwig 
21699cf514ccSChristoph Hellwig 	map = nfsd4_find_devid_map(gdp->gd_devid.fsid_idx);
21709cf514ccSChristoph Hellwig 	if (!map) {
21719cf514ccSChristoph Hellwig 		dprintk("%s: couldn't find device ID to export mapping!\n",
21729cf514ccSChristoph Hellwig 			__func__);
21739cf514ccSChristoph Hellwig 		return nfserr_noent;
21749cf514ccSChristoph Hellwig 	}
21759cf514ccSChristoph Hellwig 
21769cf514ccSChristoph Hellwig 	exp = rqst_exp_find(rqstp, map->fsid_type, map->fsid);
21779cf514ccSChristoph Hellwig 	if (IS_ERR(exp)) {
21789cf514ccSChristoph Hellwig 		dprintk("%s: could not find device id\n", __func__);
21799cf514ccSChristoph Hellwig 		return nfserr_noent;
21809cf514ccSChristoph Hellwig 	}
21819cf514ccSChristoph Hellwig 
21829cf514ccSChristoph Hellwig 	nfserr = nfserr_layoutunavailable;
21839cf514ccSChristoph Hellwig 	ops = nfsd4_layout_verify(exp, gdp->gd_layout_type);
21849cf514ccSChristoph Hellwig 	if (!ops)
21859cf514ccSChristoph Hellwig 		goto out;
21869cf514ccSChristoph Hellwig 
21879cf514ccSChristoph Hellwig 	nfserr = nfs_ok;
2188f99d4fbdSChristoph Hellwig 	if (gdp->gd_maxcount != 0) {
2189f99d4fbdSChristoph Hellwig 		nfserr = ops->proc_getdeviceinfo(exp->ex_path.mnt->mnt_sb,
2190ec59659bSJ. Bruce Fields 				rqstp, cstate->clp, gdp);
2191f99d4fbdSChristoph Hellwig 	}
21929cf514ccSChristoph Hellwig 
21939cf514ccSChristoph Hellwig 	gdp->gd_notify_types &= ops->notify_types;
21949cf514ccSChristoph Hellwig out:
2195a1420384SKinglong Mee 	exp_put(exp);
21969cf514ccSChristoph Hellwig 	return nfserr;
21979cf514ccSChristoph Hellwig }
21989cf514ccSChristoph Hellwig 
219934b1744cSJ. Bruce Fields static void
220034b1744cSJ. Bruce Fields nfsd4_getdeviceinfo_release(union nfsd4_op_u *u)
220134b1744cSJ. Bruce Fields {
220234b1744cSJ. Bruce Fields 	kfree(u->getdeviceinfo.gd_device);
220334b1744cSJ. Bruce Fields }
220434b1744cSJ. Bruce Fields 
22059cf514ccSChristoph Hellwig static __be32
22069cf514ccSChristoph Hellwig nfsd4_layoutget(struct svc_rqst *rqstp,
2207eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
22089cf514ccSChristoph Hellwig {
2209eb69853dSChristoph Hellwig 	struct nfsd4_layoutget *lgp = &u->layoutget;
22109cf514ccSChristoph Hellwig 	struct svc_fh *current_fh = &cstate->current_fh;
22119cf514ccSChristoph Hellwig 	const struct nfsd4_layout_ops *ops;
22129cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls;
22139cf514ccSChristoph Hellwig 	__be32 nfserr;
2214b047898aSChuck Lever 	int accmode = NFSD_MAY_READ_IF_EXEC | NFSD_MAY_OWNER_OVERRIDE;
22159cf514ccSChristoph Hellwig 
22169cf514ccSChristoph Hellwig 	switch (lgp->lg_seg.iomode) {
22179cf514ccSChristoph Hellwig 	case IOMODE_READ:
221866282ec1SBenjamin Coddington 		accmode |= NFSD_MAY_READ;
22199cf514ccSChristoph Hellwig 		break;
22209cf514ccSChristoph Hellwig 	case IOMODE_RW:
222166282ec1SBenjamin Coddington 		accmode |= NFSD_MAY_READ | NFSD_MAY_WRITE;
22229cf514ccSChristoph Hellwig 		break;
22239cf514ccSChristoph Hellwig 	default:
22249cf514ccSChristoph Hellwig 		dprintk("%s: invalid iomode %d\n",
22259cf514ccSChristoph Hellwig 			__func__, lgp->lg_seg.iomode);
22269cf514ccSChristoph Hellwig 		nfserr = nfserr_badiomode;
22279cf514ccSChristoph Hellwig 		goto out;
22289cf514ccSChristoph Hellwig 	}
22299cf514ccSChristoph Hellwig 
22309cf514ccSChristoph Hellwig 	nfserr = fh_verify(rqstp, current_fh, 0, accmode);
22319cf514ccSChristoph Hellwig 	if (nfserr)
22329cf514ccSChristoph Hellwig 		goto out;
22339cf514ccSChristoph Hellwig 
22349cf514ccSChristoph Hellwig 	nfserr = nfserr_layoutunavailable;
22359cf514ccSChristoph Hellwig 	ops = nfsd4_layout_verify(current_fh->fh_export, lgp->lg_layout_type);
22369cf514ccSChristoph Hellwig 	if (!ops)
22379cf514ccSChristoph Hellwig 		goto out;
22389cf514ccSChristoph Hellwig 
22399cf514ccSChristoph Hellwig 	/*
22409cf514ccSChristoph Hellwig 	 * Verify minlength and range as per RFC5661:
22419cf514ccSChristoph Hellwig 	 *  o  If loga_length is less than loga_minlength,
22429cf514ccSChristoph Hellwig 	 *     the metadata server MUST return NFS4ERR_INVAL.
22439cf514ccSChristoph Hellwig 	 *  o  If the sum of loga_offset and loga_minlength exceeds
22449cf514ccSChristoph Hellwig 	 *     NFS4_UINT64_MAX, and loga_minlength is not
22459cf514ccSChristoph Hellwig 	 *     NFS4_UINT64_MAX, the error NFS4ERR_INVAL MUST result.
22469cf514ccSChristoph Hellwig 	 *  o  If the sum of loga_offset and loga_length exceeds
22479cf514ccSChristoph Hellwig 	 *     NFS4_UINT64_MAX, and loga_length is not NFS4_UINT64_MAX,
22489cf514ccSChristoph Hellwig 	 *     the error NFS4ERR_INVAL MUST result.
22499cf514ccSChristoph Hellwig 	 */
22509cf514ccSChristoph Hellwig 	nfserr = nfserr_inval;
22519cf514ccSChristoph Hellwig 	if (lgp->lg_seg.length < lgp->lg_minlength ||
22529cf514ccSChristoph Hellwig 	    (lgp->lg_minlength != NFS4_MAX_UINT64 &&
22539cf514ccSChristoph Hellwig 	     lgp->lg_minlength > NFS4_MAX_UINT64 - lgp->lg_seg.offset) ||
22549cf514ccSChristoph Hellwig 	    (lgp->lg_seg.length != NFS4_MAX_UINT64 &&
22559cf514ccSChristoph Hellwig 	     lgp->lg_seg.length > NFS4_MAX_UINT64 - lgp->lg_seg.offset))
22569cf514ccSChristoph Hellwig 		goto out;
22579cf514ccSChristoph Hellwig 	if (lgp->lg_seg.length == 0)
22589cf514ccSChristoph Hellwig 		goto out;
22599cf514ccSChristoph Hellwig 
22609cf514ccSChristoph Hellwig 	nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lgp->lg_sid,
22619cf514ccSChristoph Hellwig 						true, lgp->lg_layout_type, &ls);
226231ef83dcSChristoph Hellwig 	if (nfserr) {
2263f394b62bSChuck Lever 		trace_nfsd_layout_get_lookup_fail(&lgp->lg_sid);
22649cf514ccSChristoph Hellwig 		goto out;
226531ef83dcSChristoph Hellwig 	}
22669cf514ccSChristoph Hellwig 
2267c5c707f9SChristoph Hellwig 	nfserr = nfserr_recallconflict;
2268c5c707f9SChristoph Hellwig 	if (atomic_read(&ls->ls_stid.sc_file->fi_lo_recalls))
2269c5c707f9SChristoph Hellwig 		goto out_put_stid;
2270c5c707f9SChristoph Hellwig 
22712b0143b5SDavid Howells 	nfserr = ops->proc_layoutget(d_inode(current_fh->fh_dentry),
22729cf514ccSChristoph Hellwig 				     current_fh, lgp);
22739cf514ccSChristoph Hellwig 	if (nfserr)
22749cf514ccSChristoph Hellwig 		goto out_put_stid;
22759cf514ccSChristoph Hellwig 
22769cf514ccSChristoph Hellwig 	nfserr = nfsd4_insert_layout(lgp, ls);
22779cf514ccSChristoph Hellwig 
22789cf514ccSChristoph Hellwig out_put_stid:
2279cc8a5532SJeff Layton 	mutex_unlock(&ls->ls_mutex);
22809cf514ccSChristoph Hellwig 	nfs4_put_stid(&ls->ls_stid);
22819cf514ccSChristoph Hellwig out:
22829cf514ccSChristoph Hellwig 	return nfserr;
22839cf514ccSChristoph Hellwig }
22849cf514ccSChristoph Hellwig 
228534b1744cSJ. Bruce Fields static void
228634b1744cSJ. Bruce Fields nfsd4_layoutget_release(union nfsd4_op_u *u)
228734b1744cSJ. Bruce Fields {
228834b1744cSJ. Bruce Fields 	kfree(u->layoutget.lg_content);
228934b1744cSJ. Bruce Fields }
229034b1744cSJ. Bruce Fields 
22919cf514ccSChristoph Hellwig static __be32
22929cf514ccSChristoph Hellwig nfsd4_layoutcommit(struct svc_rqst *rqstp,
2293eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
22949cf514ccSChristoph Hellwig {
2295eb69853dSChristoph Hellwig 	struct nfsd4_layoutcommit *lcp = &u->layoutcommit;
22969cf514ccSChristoph Hellwig 	const struct nfsd4_layout_seg *seg = &lcp->lc_seg;
22979cf514ccSChristoph Hellwig 	struct svc_fh *current_fh = &cstate->current_fh;
22989cf514ccSChristoph Hellwig 	const struct nfsd4_layout_ops *ops;
22999cf514ccSChristoph Hellwig 	loff_t new_size = lcp->lc_last_wr + 1;
23009cf514ccSChristoph Hellwig 	struct inode *inode;
23019cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls;
23029cf514ccSChristoph Hellwig 	__be32 nfserr;
23039cf514ccSChristoph Hellwig 
2304b047898aSChuck Lever 	nfserr = fh_verify(rqstp, current_fh, 0,
2305b047898aSChuck Lever 			   NFSD_MAY_WRITE | NFSD_MAY_OWNER_OVERRIDE);
23069cf514ccSChristoph Hellwig 	if (nfserr)
23079cf514ccSChristoph Hellwig 		goto out;
23089cf514ccSChristoph Hellwig 
23099cf514ccSChristoph Hellwig 	nfserr = nfserr_layoutunavailable;
23109cf514ccSChristoph Hellwig 	ops = nfsd4_layout_verify(current_fh->fh_export, lcp->lc_layout_type);
23119cf514ccSChristoph Hellwig 	if (!ops)
23129cf514ccSChristoph Hellwig 		goto out;
23132b0143b5SDavid Howells 	inode = d_inode(current_fh->fh_dentry);
23149cf514ccSChristoph Hellwig 
23159cf514ccSChristoph Hellwig 	nfserr = nfserr_inval;
23169cf514ccSChristoph Hellwig 	if (new_size <= seg->offset) {
23179cf514ccSChristoph Hellwig 		dprintk("pnfsd: last write before layout segment\n");
23189cf514ccSChristoph Hellwig 		goto out;
23199cf514ccSChristoph Hellwig 	}
23209cf514ccSChristoph Hellwig 	if (new_size > seg->offset + seg->length) {
23219cf514ccSChristoph Hellwig 		dprintk("pnfsd: last write beyond layout segment\n");
23229cf514ccSChristoph Hellwig 		goto out;
23239cf514ccSChristoph Hellwig 	}
23249cf514ccSChristoph Hellwig 	if (!lcp->lc_newoffset && new_size > i_size_read(inode)) {
23259cf514ccSChristoph Hellwig 		dprintk("pnfsd: layoutcommit beyond EOF\n");
23269cf514ccSChristoph Hellwig 		goto out;
23279cf514ccSChristoph Hellwig 	}
23289cf514ccSChristoph Hellwig 
23299cf514ccSChristoph Hellwig 	nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lcp->lc_sid,
23309cf514ccSChristoph Hellwig 						false, lcp->lc_layout_type,
23319cf514ccSChristoph Hellwig 						&ls);
23329cf514ccSChristoph Hellwig 	if (nfserr) {
2333f394b62bSChuck Lever 		trace_nfsd_layout_commit_lookup_fail(&lcp->lc_sid);
23349cf514ccSChristoph Hellwig 		/* fixup error code as per RFC5661 */
23359cf514ccSChristoph Hellwig 		if (nfserr == nfserr_bad_stateid)
23369cf514ccSChristoph Hellwig 			nfserr = nfserr_badlayout;
23379cf514ccSChristoph Hellwig 		goto out;
23389cf514ccSChristoph Hellwig 	}
23399cf514ccSChristoph Hellwig 
2340cc8a5532SJeff Layton 	/* LAYOUTCOMMIT does not require any serialization */
2341cc8a5532SJeff Layton 	mutex_unlock(&ls->ls_mutex);
2342cc8a5532SJeff Layton 
23439cf514ccSChristoph Hellwig 	if (new_size > i_size_read(inode)) {
23449cf514ccSChristoph Hellwig 		lcp->lc_size_chg = 1;
23459cf514ccSChristoph Hellwig 		lcp->lc_newsize = new_size;
23469cf514ccSChristoph Hellwig 	} else {
23479cf514ccSChristoph Hellwig 		lcp->lc_size_chg = 0;
23489cf514ccSChristoph Hellwig 	}
23499cf514ccSChristoph Hellwig 
2350d8398fc1SKinglong Mee 	nfserr = ops->proc_layoutcommit(inode, lcp);
23519cf514ccSChristoph Hellwig 	nfs4_put_stid(&ls->ls_stid);
23529cf514ccSChristoph Hellwig out:
23539cf514ccSChristoph Hellwig 	return nfserr;
23549cf514ccSChristoph Hellwig }
23559cf514ccSChristoph Hellwig 
23569cf514ccSChristoph Hellwig static __be32
23579cf514ccSChristoph Hellwig nfsd4_layoutreturn(struct svc_rqst *rqstp,
2358eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
23599cf514ccSChristoph Hellwig {
2360eb69853dSChristoph Hellwig 	struct nfsd4_layoutreturn *lrp = &u->layoutreturn;
23619cf514ccSChristoph Hellwig 	struct svc_fh *current_fh = &cstate->current_fh;
23629cf514ccSChristoph Hellwig 	__be32 nfserr;
23639cf514ccSChristoph Hellwig 
23649cf514ccSChristoph Hellwig 	nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_NOP);
23659cf514ccSChristoph Hellwig 	if (nfserr)
23669cf514ccSChristoph Hellwig 		goto out;
23679cf514ccSChristoph Hellwig 
23689cf514ccSChristoph Hellwig 	nfserr = nfserr_layoutunavailable;
23699cf514ccSChristoph Hellwig 	if (!nfsd4_layout_verify(current_fh->fh_export, lrp->lr_layout_type))
23709cf514ccSChristoph Hellwig 		goto out;
23719cf514ccSChristoph Hellwig 
23729cf514ccSChristoph Hellwig 	switch (lrp->lr_seg.iomode) {
23739cf514ccSChristoph Hellwig 	case IOMODE_READ:
23749cf514ccSChristoph Hellwig 	case IOMODE_RW:
23759cf514ccSChristoph Hellwig 	case IOMODE_ANY:
23769cf514ccSChristoph Hellwig 		break;
23779cf514ccSChristoph Hellwig 	default:
23789cf514ccSChristoph Hellwig 		dprintk("%s: invalid iomode %d\n", __func__,
23799cf514ccSChristoph Hellwig 			lrp->lr_seg.iomode);
23809cf514ccSChristoph Hellwig 		nfserr = nfserr_inval;
23819cf514ccSChristoph Hellwig 		goto out;
23829cf514ccSChristoph Hellwig 	}
23839cf514ccSChristoph Hellwig 
23849cf514ccSChristoph Hellwig 	switch (lrp->lr_return_type) {
23859cf514ccSChristoph Hellwig 	case RETURN_FILE:
23869cf514ccSChristoph Hellwig 		nfserr = nfsd4_return_file_layouts(rqstp, cstate, lrp);
23879cf514ccSChristoph Hellwig 		break;
23889cf514ccSChristoph Hellwig 	case RETURN_FSID:
23899cf514ccSChristoph Hellwig 	case RETURN_ALL:
23909cf514ccSChristoph Hellwig 		nfserr = nfsd4_return_client_layouts(rqstp, cstate, lrp);
23919cf514ccSChristoph Hellwig 		break;
23929cf514ccSChristoph Hellwig 	default:
23939cf514ccSChristoph Hellwig 		dprintk("%s: invalid return_type %d\n", __func__,
23949cf514ccSChristoph Hellwig 			lrp->lr_return_type);
23959cf514ccSChristoph Hellwig 		nfserr = nfserr_inval;
23969cf514ccSChristoph Hellwig 		break;
23979cf514ccSChristoph Hellwig 	}
23989cf514ccSChristoph Hellwig out:
23999cf514ccSChristoph Hellwig 	return nfserr;
24009cf514ccSChristoph Hellwig }
24019cf514ccSChristoph Hellwig #endif /* CONFIG_NFSD_PNFS */
24029cf514ccSChristoph Hellwig 
240323e50fe3SFrank van der Linden static __be32
240423e50fe3SFrank van der Linden nfsd4_getxattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
240523e50fe3SFrank van der Linden 	       union nfsd4_op_u *u)
240623e50fe3SFrank van der Linden {
240723e50fe3SFrank van der Linden 	struct nfsd4_getxattr *getxattr = &u->getxattr;
240823e50fe3SFrank van der Linden 
240923e50fe3SFrank van der Linden 	return nfsd_getxattr(rqstp, &cstate->current_fh,
241023e50fe3SFrank van der Linden 			     getxattr->getxa_name, &getxattr->getxa_buf,
241123e50fe3SFrank van der Linden 			     &getxattr->getxa_len);
241223e50fe3SFrank van der Linden }
241323e50fe3SFrank van der Linden 
241423e50fe3SFrank van der Linden static __be32
241523e50fe3SFrank van der Linden nfsd4_setxattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
241623e50fe3SFrank van der Linden 	   union nfsd4_op_u *u)
241723e50fe3SFrank van der Linden {
241823e50fe3SFrank van der Linden 	struct nfsd4_setxattr *setxattr = &u->setxattr;
241923e50fe3SFrank van der Linden 	__be32 ret;
242023e50fe3SFrank van der Linden 
242123e50fe3SFrank van der Linden 	if (opens_in_grace(SVC_NET(rqstp)))
242223e50fe3SFrank van der Linden 		return nfserr_grace;
242323e50fe3SFrank van der Linden 
242423e50fe3SFrank van der Linden 	ret = nfsd_setxattr(rqstp, &cstate->current_fh, setxattr->setxa_name,
242523e50fe3SFrank van der Linden 			    setxattr->setxa_buf, setxattr->setxa_len,
242623e50fe3SFrank van der Linden 			    setxattr->setxa_flags);
242723e50fe3SFrank van der Linden 
242823e50fe3SFrank van der Linden 	if (!ret)
242923e50fe3SFrank van der Linden 		set_change_info(&setxattr->setxa_cinfo, &cstate->current_fh);
243023e50fe3SFrank van der Linden 
243123e50fe3SFrank van der Linden 	return ret;
243223e50fe3SFrank van der Linden }
243323e50fe3SFrank van der Linden 
243423e50fe3SFrank van der Linden static __be32
243523e50fe3SFrank van der Linden nfsd4_listxattrs(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
243623e50fe3SFrank van der Linden 	   union nfsd4_op_u *u)
243723e50fe3SFrank van der Linden {
243823e50fe3SFrank van der Linden 	/*
243923e50fe3SFrank van der Linden 	 * Get the entire list, then copy out only the user attributes
244023e50fe3SFrank van der Linden 	 * in the encode function.
244123e50fe3SFrank van der Linden 	 */
244223e50fe3SFrank van der Linden 	return nfsd_listxattr(rqstp, &cstate->current_fh,
244323e50fe3SFrank van der Linden 			     &u->listxattrs.lsxa_buf, &u->listxattrs.lsxa_len);
244423e50fe3SFrank van der Linden }
244523e50fe3SFrank van der Linden 
244623e50fe3SFrank van der Linden static __be32
244723e50fe3SFrank van der Linden nfsd4_removexattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
244823e50fe3SFrank van der Linden 	   union nfsd4_op_u *u)
244923e50fe3SFrank van der Linden {
245023e50fe3SFrank van der Linden 	struct nfsd4_removexattr *removexattr = &u->removexattr;
245123e50fe3SFrank van der Linden 	__be32 ret;
245223e50fe3SFrank van der Linden 
245323e50fe3SFrank van der Linden 	if (opens_in_grace(SVC_NET(rqstp)))
245423e50fe3SFrank van der Linden 		return nfserr_grace;
245523e50fe3SFrank van der Linden 
245623e50fe3SFrank van der Linden 	ret = nfsd_removexattr(rqstp, &cstate->current_fh,
245723e50fe3SFrank van der Linden 	    removexattr->rmxa_name);
245823e50fe3SFrank van der Linden 
245923e50fe3SFrank van der Linden 	if (!ret)
246023e50fe3SFrank van der Linden 		set_change_info(&removexattr->rmxa_cinfo, &cstate->current_fh);
246123e50fe3SFrank van der Linden 
246223e50fe3SFrank van der Linden 	return ret;
246323e50fe3SFrank van der Linden }
246423e50fe3SFrank van der Linden 
24651da177e4SLinus Torvalds /*
24661da177e4SLinus Torvalds  * NULL call.
24671da177e4SLinus Torvalds  */
24687111c66eSAl Viro static __be32
2469a6beb732SChristoph Hellwig nfsd4_proc_null(struct svc_rqst *rqstp)
24701da177e4SLinus Torvalds {
2471cc028a10SChuck Lever 	return rpc_success;
24721da177e4SLinus Torvalds }
24731da177e4SLinus Torvalds 
2474b670a598SJosef Bacik static inline void nfsd4_increment_op_stats(struct nfsd_net *nn, u32 opnum)
2475e2b20950SShankar Anand {
2476e2b20950SShankar Anand 	if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
2477b670a598SJosef Bacik 		percpu_counter_inc(&nn->counter[NFSD_STATS_NFS4_OP(opnum)]);
2478e2b20950SShankar Anand }
2479e2b20950SShankar Anand 
2480bb2a8b0cSChristoph Hellwig static const struct nfsd4_operation nfsd4_ops[];
2481b591480bSJ.Bruce Fields 
2482f1c7f79bSAdrian Bunk static const char *nfsd4_op_name(unsigned opnum);
2483b001a1b6SBenny Halevy 
24841da177e4SLinus Torvalds /*
248557716355SJ. Bruce Fields  * Enforce NFSv4.1 COMPOUND ordering rules:
2486f9bb94c4SAndy Adamson  *
248757716355SJ. Bruce Fields  * Also note, enforced elsewhere:
248857716355SJ. Bruce Fields  *	- SEQUENCE other than as first op results in
248957716355SJ. Bruce Fields  *	  NFS4ERR_SEQUENCE_POS. (Enforced in nfsd4_sequence().)
24901d1bc8f2SJ. Bruce Fields  *	- BIND_CONN_TO_SESSION must be the only op in its compound.
24911d1bc8f2SJ. Bruce Fields  *	  (Enforced in nfsd4_bind_conn_to_session().)
249257716355SJ. Bruce Fields  *	- DESTROY_SESSION must be the final operation in a compound, if
249357716355SJ. Bruce Fields  *	  sessionid's in SEQUENCE and DESTROY_SESSION are the same.
249457716355SJ. Bruce Fields  *	  (Enforced in nfsd4_destroy_session().)
2495f9bb94c4SAndy Adamson  */
249657716355SJ. Bruce Fields static __be32 nfs41_check_op_ordering(struct nfsd4_compoundargs *args)
2497f9bb94c4SAndy Adamson {
24987a04cfdaSJ. Bruce Fields 	struct nfsd4_op *first_op = &args->ops[0];
249957716355SJ. Bruce Fields 
250057716355SJ. Bruce Fields 	/* These ordering requirements don't apply to NFSv4.0: */
250157716355SJ. Bruce Fields 	if (args->minorversion == 0)
250257716355SJ. Bruce Fields 		return nfs_ok;
250357716355SJ. Bruce Fields 	/* This is weird, but OK, not our problem: */
250457716355SJ. Bruce Fields 	if (args->opcnt == 0)
250557716355SJ. Bruce Fields 		return nfs_ok;
25067a04cfdaSJ. Bruce Fields 	if (first_op->status == nfserr_op_illegal)
250757716355SJ. Bruce Fields 		return nfs_ok;
25087a04cfdaSJ. Bruce Fields 	if (!(nfsd4_ops[first_op->opnum].op_flags & ALLOWED_AS_FIRST_OP))
250957716355SJ. Bruce Fields 		return nfserr_op_not_in_session;
25107a04cfdaSJ. Bruce Fields 	if (first_op->opnum == OP_SEQUENCE)
251157716355SJ. Bruce Fields 		return nfs_ok;
25127a04cfdaSJ. Bruce Fields 	/*
25137a04cfdaSJ. Bruce Fields 	 * So first_op is something allowed outside a session, like
25147a04cfdaSJ. Bruce Fields 	 * EXCHANGE_ID; but then it has to be the only op in the
25157a04cfdaSJ. Bruce Fields 	 * compound:
25167a04cfdaSJ. Bruce Fields 	 */
251757716355SJ. Bruce Fields 	if (args->opcnt != 1)
251857716355SJ. Bruce Fields 		return nfserr_not_only_op;
251957716355SJ. Bruce Fields 	return nfs_ok;
2520f9bb94c4SAndy Adamson }
2521f9bb94c4SAndy Adamson 
2522f4f9ef4aSJ. Bruce Fields const struct nfsd4_operation *OPDESC(struct nfsd4_op *op)
252322b03214SJ. Bruce Fields {
252422b03214SJ. Bruce Fields 	return &nfsd4_ops[op->opnum];
252522b03214SJ. Bruce Fields }
252622b03214SJ. Bruce Fields 
25271091006cSJ. Bruce Fields bool nfsd4_cache_this_op(struct nfsd4_op *op)
25281091006cSJ. Bruce Fields {
2529e372ba60SJ. Bruce Fields 	if (op->opnum == OP_ILLEGAL)
2530e372ba60SJ. Bruce Fields 		return false;
2531c856694eSJ. Bruce Fields 	return OPDESC(op)->op_flags & OP_CACHEME;
25321091006cSJ. Bruce Fields }
25331091006cSJ. Bruce Fields 
253468d93184SJ. Bruce Fields static bool need_wrongsec_check(struct svc_rqst *rqstp)
253568d93184SJ. Bruce Fields {
253668d93184SJ. Bruce Fields 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
253768d93184SJ. Bruce Fields 	struct nfsd4_compoundargs *argp = rqstp->rq_argp;
253868d93184SJ. Bruce Fields 	struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
253968d93184SJ. Bruce Fields 	struct nfsd4_op *next = &argp->ops[resp->opcnt];
2540bb2a8b0cSChristoph Hellwig 	const struct nfsd4_operation *thisd = OPDESC(this);
2541bb2a8b0cSChristoph Hellwig 	const struct nfsd4_operation *nextd;
254268d93184SJ. Bruce Fields 
254368d93184SJ. Bruce Fields 	/*
254468d93184SJ. Bruce Fields 	 * Most ops check wronsec on our own; only the putfh-like ops
254568d93184SJ. Bruce Fields 	 * have special rules.
254668d93184SJ. Bruce Fields 	 */
254768d93184SJ. Bruce Fields 	if (!(thisd->op_flags & OP_IS_PUTFH_LIKE))
254868d93184SJ. Bruce Fields 		return false;
254968d93184SJ. Bruce Fields 	/*
255068d93184SJ. Bruce Fields 	 * rfc 5661 2.6.3.1.1.6: don't bother erroring out a
255168d93184SJ. Bruce Fields 	 * put-filehandle operation if we're not going to use the
255268d93184SJ. Bruce Fields 	 * result:
255368d93184SJ. Bruce Fields 	 */
255468d93184SJ. Bruce Fields 	if (argp->opcnt == resp->opcnt)
255568d93184SJ. Bruce Fields 		return false;
255651904b08SJ. Bruce Fields 	if (next->opnum == OP_ILLEGAL)
255751904b08SJ. Bruce Fields 		return false;
255868d93184SJ. Bruce Fields 	nextd = OPDESC(next);
255968d93184SJ. Bruce Fields 	/*
256068d93184SJ. Bruce Fields 	 * Rest of 2.6.3.1.1: certain operations will return WRONGSEC
256168d93184SJ. Bruce Fields 	 * errors themselves as necessary; others should check for them
256268d93184SJ. Bruce Fields 	 * now:
256368d93184SJ. Bruce Fields 	 */
256468d93184SJ. Bruce Fields 	return !(nextd->op_flags & OP_HANDLES_WRONGSEC);
256568d93184SJ. Bruce Fields }
256668d93184SJ. Bruce Fields 
2567b9e8638eSOlga Kornievskaia #ifdef CONFIG_NFSD_V4_2_INTER_SSC
2568b9e8638eSOlga Kornievskaia static void
2569b9e8638eSOlga Kornievskaia check_if_stalefh_allowed(struct nfsd4_compoundargs *args)
2570b9e8638eSOlga Kornievskaia {
2571b9e8638eSOlga Kornievskaia 	struct nfsd4_op	*op, *current_op = NULL, *saved_op = NULL;
2572b9e8638eSOlga Kornievskaia 	struct nfsd4_copy *copy;
2573b9e8638eSOlga Kornievskaia 	struct nfsd4_putfh *putfh;
2574b9e8638eSOlga Kornievskaia 	int i;
2575b9e8638eSOlga Kornievskaia 
2576b9e8638eSOlga Kornievskaia 	/* traverse all operation and if it's a COPY compound, mark the
2577b9e8638eSOlga Kornievskaia 	 * source filehandle to skip verification
2578b9e8638eSOlga Kornievskaia 	 */
2579b9e8638eSOlga Kornievskaia 	for (i = 0; i < args->opcnt; i++) {
2580b9e8638eSOlga Kornievskaia 		op = &args->ops[i];
2581b9e8638eSOlga Kornievskaia 		if (op->opnum == OP_PUTFH)
2582b9e8638eSOlga Kornievskaia 			current_op = op;
2583b9e8638eSOlga Kornievskaia 		else if (op->opnum == OP_SAVEFH)
2584b9e8638eSOlga Kornievskaia 			saved_op = current_op;
2585b9e8638eSOlga Kornievskaia 		else if (op->opnum == OP_RESTOREFH)
2586b9e8638eSOlga Kornievskaia 			current_op = saved_op;
2587b9e8638eSOlga Kornievskaia 		else if (op->opnum == OP_COPY) {
2588b9e8638eSOlga Kornievskaia 			copy = (struct nfsd4_copy *)&op->u;
2589b9e8638eSOlga Kornievskaia 			if (!saved_op) {
2590b9e8638eSOlga Kornievskaia 				op->status = nfserr_nofilehandle;
2591b9e8638eSOlga Kornievskaia 				return;
2592b9e8638eSOlga Kornievskaia 			}
2593b9e8638eSOlga Kornievskaia 			putfh = (struct nfsd4_putfh *)&saved_op->u;
25941913cdf5SChuck Lever 			if (nfsd4_ssc_is_inter(copy))
2595b9e8638eSOlga Kornievskaia 				putfh->no_verify = true;
2596b9e8638eSOlga Kornievskaia 		}
2597b9e8638eSOlga Kornievskaia 	}
2598b9e8638eSOlga Kornievskaia }
2599b9e8638eSOlga Kornievskaia #else
2600b9e8638eSOlga Kornievskaia static void
2601b9e8638eSOlga Kornievskaia check_if_stalefh_allowed(struct nfsd4_compoundargs *args)
2602b9e8638eSOlga Kornievskaia {
2603b9e8638eSOlga Kornievskaia }
2604b9e8638eSOlga Kornievskaia #endif
2605b9e8638eSOlga Kornievskaia 
2606f9bb94c4SAndy Adamson /*
26071da177e4SLinus Torvalds  * COMPOUND call.
26081da177e4SLinus Torvalds  */
26097111c66eSAl Viro static __be32
2610a6beb732SChristoph Hellwig nfsd4_proc_compound(struct svc_rqst *rqstp)
26111da177e4SLinus Torvalds {
2612a6beb732SChristoph Hellwig 	struct nfsd4_compoundargs *args = rqstp->rq_argp;
2613a6beb732SChristoph Hellwig 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
26141da177e4SLinus Torvalds 	struct nfsd4_op	*op;
2615e354d571SAndy Adamson 	struct nfsd4_compound_state *cstate = &resp->cstate;
26164daeed25SKinglong Mee 	struct svc_fh *current_fh = &cstate->current_fh;
26174daeed25SKinglong Mee 	struct svc_fh *save_fh = &cstate->save_fh;
2618e333f3bbSTrond Myklebust 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2619b37ad28bSAl Viro 	__be32		status;
26201da177e4SLinus Torvalds 
2621bddfdbcdSChuck Lever 	resp->xdr = &rqstp->rq_res_stream;
26223b0ebb25SChuck Lever 	resp->statusp = resp->xdr->p;
2623bddfdbcdSChuck Lever 
2624bddfdbcdSChuck Lever 	/* reserve space for: NFS status code */
2625bddfdbcdSChuck Lever 	xdr_reserve_space(resp->xdr, XDR_UNIT);
2626bddfdbcdSChuck Lever 
26271da177e4SLinus Torvalds 	/* reserve space for: taglen, tag, and opcnt */
2628bddfdbcdSChuck Lever 	xdr_reserve_space(resp->xdr, XDR_UNIT * 2 + args->taglen);
26291da177e4SLinus Torvalds 	resp->taglen = args->taglen;
26301da177e4SLinus Torvalds 	resp->tag = args->tag;
26311da177e4SLinus Torvalds 	resp->rqstp = rqstp;
26324daeed25SKinglong Mee 	cstate->minorversion = args->minorversion;
26334daeed25SKinglong Mee 	fh_init(current_fh, NFS4_FHSIZE);
26344daeed25SKinglong Mee 	fh_init(save_fh, NFS4_FHSIZE);
26358ff30fa4SNeilBrown 	/*
26368ff30fa4SNeilBrown 	 * Don't use the deferral mechanism for NFSv4; compounds make it
26378ff30fa4SNeilBrown 	 * too hard to avoid non-idempotency problems.
26388ff30fa4SNeilBrown 	 */
26397827c81fSChuck Lever 	clear_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
26401da177e4SLinus Torvalds 
26411da177e4SLinus Torvalds 	/*
26421da177e4SLinus Torvalds 	 * According to RFC3010, this takes precedence over all other errors.
26431da177e4SLinus Torvalds 	 */
26441da177e4SLinus Torvalds 	status = nfserr_minor_vers_mismatch;
2645e333f3bbSTrond Myklebust 	if (nfsd_minorversion(nn, args->minorversion, NFSD_TEST) <= 0)
26461da177e4SLinus Torvalds 		goto out;
26471da177e4SLinus Torvalds 
264857716355SJ. Bruce Fields 	status = nfs41_check_op_ordering(args);
264957716355SJ. Bruce Fields 	if (status) {
2650f9bb94c4SAndy Adamson 		op = &args->ops[0];
265157716355SJ. Bruce Fields 		op->status = status;
26525b7b15aeSJ. Bruce Fields 		resp->opcnt = 1;
2653f9bb94c4SAndy Adamson 		goto encode_op;
2654f9bb94c4SAndy Adamson 	}
2655b9e8638eSOlga Kornievskaia 	check_if_stalefh_allowed(args);
2656f9bb94c4SAndy Adamson 
265728df3d15SJ. Bruce Fields 	rqstp->rq_lease_breaker = (void **)&cstate->clp;
265828df3d15SJ. Bruce Fields 
2659de29cf7eSChuck Lever 	trace_nfsd_compound(rqstp, args->tag, args->taglen, args->client_opcnt);
26601da177e4SLinus Torvalds 	while (!status && resp->opcnt < args->opcnt) {
26611da177e4SLinus Torvalds 		op = &args->ops[resp->opcnt++];
26621da177e4SLinus Torvalds 
26637518a3dcSChuck Lever 		if (unlikely(resp->opcnt == NFSD_MAX_OPS_PER_COMPOUND)) {
26647518a3dcSChuck Lever 			/* If there are still more operations to process,
26657518a3dcSChuck Lever 			 * stop here and report NFS4ERR_RESOURCE. */
26667518a3dcSChuck Lever 			if (cstate->minorversion == 0 &&
26677518a3dcSChuck Lever 			    args->client_opcnt > resp->opcnt) {
26687518a3dcSChuck Lever 				op->status = nfserr_resource;
26697518a3dcSChuck Lever 				goto encode_op;
26707518a3dcSChuck Lever 			}
26717518a3dcSChuck Lever 		}
26727518a3dcSChuck Lever 
26731da177e4SLinus Torvalds 		/*
26741da177e4SLinus Torvalds 		 * The XDR decode routines may have pre-set op->status;
26751da177e4SLinus Torvalds 		 * for example, if there is a miscellaneous XDR error
26761da177e4SLinus Torvalds 		 * it will be set to nfserr_bad_xdr.
26771da177e4SLinus Torvalds 		 */
26789d313b17SJ. Bruce Fields 		if (op->status) {
26799d313b17SJ. Bruce Fields 			if (op->opnum == OP_OPEN)
26809d313b17SJ. Bruce Fields 				op->status = nfsd4_open_omfg(rqstp, cstate, op);
26811da177e4SLinus Torvalds 			goto encode_op;
26829d313b17SJ. Bruce Fields 		}
2683b9e8638eSOlga Kornievskaia 		if (!current_fh->fh_dentry &&
2684b9e8638eSOlga Kornievskaia 				!HAS_FH_FLAG(current_fh, NFSD4_FH_FOREIGN)) {
2685f4f9ef4aSJ. Bruce Fields 			if (!(op->opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
26861da177e4SLinus Torvalds 				op->status = nfserr_nofilehandle;
26871da177e4SLinus Torvalds 				goto encode_op;
26881da177e4SLinus Torvalds 			}
2689b9e8638eSOlga Kornievskaia 		} else if (current_fh->fh_export &&
2690b9e8638eSOlga Kornievskaia 			   current_fh->fh_export->ex_fslocs.migrated &&
2691f4f9ef4aSJ. Bruce Fields 			  !(op->opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
269242ca0993SJ.Bruce Fields 			op->status = nfserr_moved;
269342ca0993SJ.Bruce Fields 			goto encode_op;
269442ca0993SJ.Bruce Fields 		}
2695b591480bSJ.Bruce Fields 
2696fcb5e3faSChuck Lever 		fh_clear_pre_post_attrs(current_fh);
26972336745eSKinglong Mee 
269858e7b33aSMi Jinlong 		/* If op is non-idempotent */
2699f4f9ef4aSJ. Bruce Fields 		if (op->opdesc->op_flags & OP_MODIFIES_SOMETHING) {
27004c69d585SJ. Bruce Fields 			/*
2701a8095f7eSJ. Bruce Fields 			 * Don't execute this op if we couldn't encode a
2702f532c9ffSZhang Jiaming 			 * successful reply:
2703a8095f7eSJ. Bruce Fields 			 */
2704f4f9ef4aSJ. Bruce Fields 			u32 plen = op->opdesc->op_rsize_bop(rqstp, op);
2705a8095f7eSJ. Bruce Fields 			/*
2706a8095f7eSJ. Bruce Fields 			 * Plus if there's another operation, make sure
27074c69d585SJ. Bruce Fields 			 * we'll have space to at least encode an error:
27084c69d585SJ. Bruce Fields 			 */
27094c69d585SJ. Bruce Fields 			if (resp->opcnt < args->opcnt)
27104c69d585SJ. Bruce Fields 				plen += COMPOUND_ERR_SLACK_SPACE;
271158e7b33aSMi Jinlong 			op->status = nfsd4_check_resp_size(resp, plen);
271258e7b33aSMi Jinlong 		}
271358e7b33aSMi Jinlong 
271458e7b33aSMi Jinlong 		if (op->status)
271558e7b33aSMi Jinlong 			goto encode_op;
271658e7b33aSMi Jinlong 
2717f4f9ef4aSJ. Bruce Fields 		if (op->opdesc->op_get_currentstateid)
2718f4f9ef4aSJ. Bruce Fields 			op->opdesc->op_get_currentstateid(cstate, &op->u);
2719f4f9ef4aSJ. Bruce Fields 		op->status = op->opdesc->op_func(rqstp, cstate, &op->u);
27201da177e4SLinus Torvalds 
27219a307403SJ. Bruce Fields 		/* Only from SEQUENCE */
27229a307403SJ. Bruce Fields 		if (cstate->status == nfserr_replay_cache) {
27239a307403SJ. Bruce Fields 			dprintk("%s NFS4.1 replay from cache\n", __func__);
27249a307403SJ. Bruce Fields 			status = op->status;
27259a307403SJ. Bruce Fields 			goto out;
27269a307403SJ. Bruce Fields 		}
27278b70484cSTigran Mkrtchyan 		if (!op->status) {
2728f4f9ef4aSJ. Bruce Fields 			if (op->opdesc->op_set_currentstateid)
2729f4f9ef4aSJ. Bruce Fields 				op->opdesc->op_set_currentstateid(cstate, &op->u);
27308b70484cSTigran Mkrtchyan 
2731f4f9ef4aSJ. Bruce Fields 			if (op->opdesc->op_flags & OP_CLEAR_STATEID)
273237c593c5STigran Mkrtchyan 				clear_current_stateid(cstate);
27338b70484cSTigran Mkrtchyan 
2734d781e3dfSJ. Bruce Fields 			if (current_fh->fh_export &&
2735d781e3dfSJ. Bruce Fields 					need_wrongsec_check(rqstp))
27364daeed25SKinglong Mee 				op->status = check_nfsd_access(current_fh->fh_export, rqstp);
27378b70484cSTigran Mkrtchyan 		}
27381da177e4SLinus Torvalds encode_op:
2739a90b061cSAl Viro 		if (op->status == nfserr_replay_me) {
2740a4f1706aSJ.Bruce Fields 			op->replay = &cstate->replay_owner->so_replay;
2741bddfdbcdSChuck Lever 			nfsd4_encode_replay(resp->xdr, op);
27421da177e4SLinus Torvalds 			status = op->status = op->replay->rp_status;
27431da177e4SLinus Torvalds 		} else {
27441da177e4SLinus Torvalds 			nfsd4_encode_operation(resp, op);
27451da177e4SLinus Torvalds 			status = op->status;
27461da177e4SLinus Torvalds 		}
27470407717dSBenny Halevy 
27487518a3dcSChuck Lever 		trace_nfsd_compound_status(args->client_opcnt, resp->opcnt,
27497518a3dcSChuck Lever 					   status, nfsd4_op_name(op->opnum));
27500407717dSBenny Halevy 
275158fb12e6SJeff Layton 		nfsd4_cstate_clear_replay(cstate);
2752b670a598SJosef Bacik 		nfsd4_increment_op_stats(nn, op->opnum);
27531da177e4SLinus Torvalds 	}
27541da177e4SLinus Torvalds 
27554daeed25SKinglong Mee 	fh_put(current_fh);
27564daeed25SKinglong Mee 	fh_put(save_fh);
27574daeed25SKinglong Mee 	BUG_ON(cstate->replay_owner);
27581da177e4SLinus Torvalds out:
2759cc028a10SChuck Lever 	cstate->status = status;
27602f425878SAndy Adamson 	/* Reset deferral mechanism for RPC deferrals */
27617827c81fSChuck Lever 	set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
2762cc028a10SChuck Lever 	return rpc_success;
27631da177e4SLinus Torvalds }
27641da177e4SLinus Torvalds 
276558e7b33aSMi Jinlong #define op_encode_hdr_size		(2)
276658e7b33aSMi Jinlong #define op_encode_stateid_maxsz		(XDR_QUADLEN(NFS4_STATEID_SIZE))
276758e7b33aSMi Jinlong #define op_encode_verifier_maxsz	(XDR_QUADLEN(NFS4_VERIFIER_SIZE))
276858e7b33aSMi Jinlong #define op_encode_change_info_maxsz	(5)
276958e7b33aSMi Jinlong #define nfs4_fattr_bitmap_maxsz		(4)
277058e7b33aSMi Jinlong 
27718c7424cfSJ. Bruce Fields /* We'll fall back on returning no lockowner if run out of space: */
27728c7424cfSJ. Bruce Fields #define op_encode_lockowner_maxsz	(0)
277358e7b33aSMi Jinlong #define op_encode_lock_denied_maxsz	(8 + op_encode_lockowner_maxsz)
277458e7b33aSMi Jinlong 
277558e7b33aSMi Jinlong #define nfs4_owner_maxsz		(1 + XDR_QUADLEN(IDMAP_NAMESZ))
277658e7b33aSMi Jinlong 
277758e7b33aSMi Jinlong #define op_encode_ace_maxsz		(3 + nfs4_owner_maxsz)
277858e7b33aSMi Jinlong #define op_encode_delegation_maxsz	(1 + op_encode_stateid_maxsz + 1 + \
277958e7b33aSMi Jinlong 					 op_encode_ace_maxsz)
278058e7b33aSMi Jinlong 
278158e7b33aSMi Jinlong #define op_encode_channel_attrs_maxsz	(6 + 1 + 1)
278258e7b33aSMi Jinlong 
278376ce4dceSChuck Lever /*
278476ce4dceSChuck Lever  * The _rsize() helpers are invoked by the NFSv4 COMPOUND decoder, which
278576ce4dceSChuck Lever  * is called before sunrpc sets rq_res.buflen. Thus we have to compute
278676ce4dceSChuck Lever  * the maximum payload size here, based on transport limits and the size
278776ce4dceSChuck Lever  * of the remaining space in the rq_pages array.
278876ce4dceSChuck Lever  */
278976ce4dceSChuck Lever static u32 nfsd4_max_payload(const struct svc_rqst *rqstp)
279076ce4dceSChuck Lever {
279176ce4dceSChuck Lever 	u32 buflen;
279276ce4dceSChuck Lever 
279376ce4dceSChuck Lever 	buflen = (rqstp->rq_page_end - rqstp->rq_next_page) * PAGE_SIZE;
279476ce4dceSChuck Lever 	buflen -= rqstp->rq_auth_slack;
279576ce4dceSChuck Lever 	buflen -= rqstp->rq_res.head[0].iov_len;
279676ce4dceSChuck Lever 	return min_t(u32, buflen, svc_max_payload(rqstp));
279776ce4dceSChuck Lever }
279876ce4dceSChuck Lever 
27996604148cSChuck Lever static u32 nfsd4_only_status_rsize(const struct svc_rqst *rqstp,
28006604148cSChuck Lever 				   const struct nfsd4_op *op)
280158e7b33aSMi Jinlong {
280258e7b33aSMi Jinlong 	return (op_encode_hdr_size) * sizeof(__be32);
280358e7b33aSMi Jinlong }
280458e7b33aSMi Jinlong 
28056604148cSChuck Lever static u32 nfsd4_status_stateid_rsize(const struct svc_rqst *rqstp,
28066604148cSChuck Lever 				      const struct nfsd4_op *op)
280758e7b33aSMi Jinlong {
280858e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_stateid_maxsz)* sizeof(__be32);
280958e7b33aSMi Jinlong }
281058e7b33aSMi Jinlong 
28116604148cSChuck Lever static u32 nfsd4_access_rsize(const struct svc_rqst *rqstp,
28126604148cSChuck Lever 			      const struct nfsd4_op *op)
28132282cd2cSKinglong Mee {
28142282cd2cSKinglong Mee 	/* ac_supported, ac_resp_access */
28152282cd2cSKinglong Mee 	return (op_encode_hdr_size + 2)* sizeof(__be32);
28162282cd2cSKinglong Mee }
28172282cd2cSKinglong Mee 
28186604148cSChuck Lever static u32 nfsd4_commit_rsize(const struct svc_rqst *rqstp,
28196604148cSChuck Lever 			      const struct nfsd4_op *op)
282058e7b33aSMi Jinlong {
282158e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_verifier_maxsz) * sizeof(__be32);
282258e7b33aSMi Jinlong }
282358e7b33aSMi Jinlong 
28246604148cSChuck Lever static u32 nfsd4_create_rsize(const struct svc_rqst *rqstp,
28256604148cSChuck Lever 			      const struct nfsd4_op *op)
282658e7b33aSMi Jinlong {
282758e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_change_info_maxsz
282858e7b33aSMi Jinlong 		+ nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
282958e7b33aSMi Jinlong }
283058e7b33aSMi Jinlong 
2831b86cef60SJ. Bruce Fields /*
2832b86cef60SJ. Bruce Fields  * Note since this is an idempotent operation we won't insist on failing
2833b86cef60SJ. Bruce Fields  * the op prematurely if the estimate is too large.  We may turn off splice
2834b86cef60SJ. Bruce Fields  * reads unnecessarily.
2835b86cef60SJ. Bruce Fields  */
28366604148cSChuck Lever static u32 nfsd4_getattr_rsize(const struct svc_rqst *rqstp,
28376604148cSChuck Lever 			       const struct nfsd4_op *op)
2838b86cef60SJ. Bruce Fields {
28396604148cSChuck Lever 	const u32 *bmap = op->u.getattr.ga_bmval;
2840b86cef60SJ. Bruce Fields 	u32 bmap0 = bmap[0], bmap1 = bmap[1], bmap2 = bmap[2];
2841b86cef60SJ. Bruce Fields 	u32 ret = 0;
2842b86cef60SJ. Bruce Fields 
2843b86cef60SJ. Bruce Fields 	if (bmap0 & FATTR4_WORD0_ACL)
284476ce4dceSChuck Lever 		return nfsd4_max_payload(rqstp);
2845b86cef60SJ. Bruce Fields 	if (bmap0 & FATTR4_WORD0_FS_LOCATIONS)
284676ce4dceSChuck Lever 		return nfsd4_max_payload(rqstp);
2847b86cef60SJ. Bruce Fields 
2848b86cef60SJ. Bruce Fields 	if (bmap1 & FATTR4_WORD1_OWNER) {
2849b86cef60SJ. Bruce Fields 		ret += IDMAP_NAMESZ + 4;
2850b86cef60SJ. Bruce Fields 		bmap1 &= ~FATTR4_WORD1_OWNER;
2851b86cef60SJ. Bruce Fields 	}
2852b86cef60SJ. Bruce Fields 	if (bmap1 & FATTR4_WORD1_OWNER_GROUP) {
2853b86cef60SJ. Bruce Fields 		ret += IDMAP_NAMESZ + 4;
2854b86cef60SJ. Bruce Fields 		bmap1 &= ~FATTR4_WORD1_OWNER_GROUP;
2855b86cef60SJ. Bruce Fields 	}
2856b86cef60SJ. Bruce Fields 	if (bmap0 & FATTR4_WORD0_FILEHANDLE) {
2857b86cef60SJ. Bruce Fields 		ret += NFS4_FHSIZE + 4;
2858b86cef60SJ. Bruce Fields 		bmap0 &= ~FATTR4_WORD0_FILEHANDLE;
2859b86cef60SJ. Bruce Fields 	}
2860b86cef60SJ. Bruce Fields 	if (bmap2 & FATTR4_WORD2_SECURITY_LABEL) {
28611ec8c0c4SKinglong Mee 		ret += NFS4_MAXLABELLEN + 12;
2862b86cef60SJ. Bruce Fields 		bmap2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2863b86cef60SJ. Bruce Fields 	}
2864b86cef60SJ. Bruce Fields 	/*
2865b86cef60SJ. Bruce Fields 	 * Largest of remaining attributes are 16 bytes (e.g.,
2866b86cef60SJ. Bruce Fields 	 * supported_attributes)
2867b86cef60SJ. Bruce Fields 	 */
2868b86cef60SJ. Bruce Fields 	ret += 16 * (hweight32(bmap0) + hweight32(bmap1) + hweight32(bmap2));
2869b86cef60SJ. Bruce Fields 	/* bitmask, length */
2870b86cef60SJ. Bruce Fields 	ret += 20;
2871b86cef60SJ. Bruce Fields 	return ret;
2872b86cef60SJ. Bruce Fields }
2873b86cef60SJ. Bruce Fields 
28746604148cSChuck Lever static u32 nfsd4_getfh_rsize(const struct svc_rqst *rqstp,
28756604148cSChuck Lever 			     const struct nfsd4_op *op)
28762282cd2cSKinglong Mee {
28772282cd2cSKinglong Mee 	return (op_encode_hdr_size + 1) * sizeof(__be32) + NFS4_FHSIZE;
28782282cd2cSKinglong Mee }
28792282cd2cSKinglong Mee 
28806604148cSChuck Lever static u32 nfsd4_link_rsize(const struct svc_rqst *rqstp,
28816604148cSChuck Lever 			    const struct nfsd4_op *op)
288258e7b33aSMi Jinlong {
288358e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
288458e7b33aSMi Jinlong 		* sizeof(__be32);
288558e7b33aSMi Jinlong }
288658e7b33aSMi Jinlong 
28876604148cSChuck Lever static u32 nfsd4_lock_rsize(const struct svc_rqst *rqstp,
28886604148cSChuck Lever 			    const struct nfsd4_op *op)
288958e7b33aSMi Jinlong {
289058e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_lock_denied_maxsz)
289158e7b33aSMi Jinlong 		* sizeof(__be32);
289258e7b33aSMi Jinlong }
289358e7b33aSMi Jinlong 
28946604148cSChuck Lever static u32 nfsd4_open_rsize(const struct svc_rqst *rqstp,
28956604148cSChuck Lever 			    const struct nfsd4_op *op)
289658e7b33aSMi Jinlong {
289758e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_stateid_maxsz
289858e7b33aSMi Jinlong 		+ op_encode_change_info_maxsz + 1
289958e7b33aSMi Jinlong 		+ nfs4_fattr_bitmap_maxsz
290058e7b33aSMi Jinlong 		+ op_encode_delegation_maxsz) * sizeof(__be32);
290158e7b33aSMi Jinlong }
290258e7b33aSMi Jinlong 
29036604148cSChuck Lever static u32 nfsd4_read_rsize(const struct svc_rqst *rqstp,
29046604148cSChuck Lever 			    const struct nfsd4_op *op)
290558e7b33aSMi Jinlong {
290676ce4dceSChuck Lever 	u32 rlen = min(op->u.read.rd_length, nfsd4_max_payload(rqstp));
290758e7b33aSMi Jinlong 
2908622f560eSJ. Bruce Fields 	return (op_encode_hdr_size + 2 + XDR_QUADLEN(rlen)) * sizeof(__be32);
290958e7b33aSMi Jinlong }
291058e7b33aSMi Jinlong 
29116604148cSChuck Lever static u32 nfsd4_read_plus_rsize(const struct svc_rqst *rqstp,
29126604148cSChuck Lever 				 const struct nfsd4_op *op)
2913528b8493SAnna Schumaker {
291476ce4dceSChuck Lever 	u32 rlen = min(op->u.read.rd_length, nfsd4_max_payload(rqstp));
2915528b8493SAnna Schumaker 	/*
2916528b8493SAnna Schumaker 	 * If we detect that the file changed during hole encoding, then we
2917528b8493SAnna Schumaker 	 * recover by encoding the remaining reply as data. This means we need
2918528b8493SAnna Schumaker 	 * to set aside enough room to encode two data segments.
2919528b8493SAnna Schumaker 	 */
2920528b8493SAnna Schumaker 	u32 seg_len = 2 * (1 + 2 + 1);
2921528b8493SAnna Schumaker 
2922528b8493SAnna Schumaker 	return (op_encode_hdr_size + 2 + seg_len + XDR_QUADLEN(rlen)) * sizeof(__be32);
2923528b8493SAnna Schumaker }
2924528b8493SAnna Schumaker 
29256604148cSChuck Lever static u32 nfsd4_readdir_rsize(const struct svc_rqst *rqstp,
29266604148cSChuck Lever 			       const struct nfsd4_op *op)
292758e7b33aSMi Jinlong {
292876ce4dceSChuck Lever 	u32 rlen = min(op->u.readdir.rd_maxcount, nfsd4_max_payload(rqstp));
292958e7b33aSMi Jinlong 
2930561f0ed4SJ. Bruce Fields 	return (op_encode_hdr_size + op_encode_verifier_maxsz +
2931561f0ed4SJ. Bruce Fields 		XDR_QUADLEN(rlen)) * sizeof(__be32);
293258e7b33aSMi Jinlong }
293358e7b33aSMi Jinlong 
29346604148cSChuck Lever static u32 nfsd4_readlink_rsize(const struct svc_rqst *rqstp,
29356604148cSChuck Lever 				const struct nfsd4_op *op)
29362282cd2cSKinglong Mee {
29372282cd2cSKinglong Mee 	return (op_encode_hdr_size + 1) * sizeof(__be32) + PAGE_SIZE;
29382282cd2cSKinglong Mee }
29392282cd2cSKinglong Mee 
29406604148cSChuck Lever static u32 nfsd4_remove_rsize(const struct svc_rqst *rqstp,
29416604148cSChuck Lever 			      const struct nfsd4_op *op)
294258e7b33aSMi Jinlong {
294358e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
294458e7b33aSMi Jinlong 		* sizeof(__be32);
294558e7b33aSMi Jinlong }
294658e7b33aSMi Jinlong 
29476604148cSChuck Lever static u32 nfsd4_rename_rsize(const struct svc_rqst *rqstp,
29486604148cSChuck Lever 			      const struct nfsd4_op *op)
294958e7b33aSMi Jinlong {
295058e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_change_info_maxsz
295158e7b33aSMi Jinlong 		+ op_encode_change_info_maxsz) * sizeof(__be32);
295258e7b33aSMi Jinlong }
295358e7b33aSMi Jinlong 
29546604148cSChuck Lever static u32 nfsd4_sequence_rsize(const struct svc_rqst *rqstp,
29556604148cSChuck Lever 				const struct nfsd4_op *op)
2956ccae70a9SJ. Bruce Fields {
2957d1d84c96SJ. Bruce Fields 	return (op_encode_hdr_size
2958d1d84c96SJ. Bruce Fields 		+ XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 5) * sizeof(__be32);
2959ccae70a9SJ. Bruce Fields }
2960ccae70a9SJ. Bruce Fields 
29616604148cSChuck Lever static u32 nfsd4_test_stateid_rsize(const struct svc_rqst *rqstp,
29626604148cSChuck Lever 				    const struct nfsd4_op *op)
29632282cd2cSKinglong Mee {
29642282cd2cSKinglong Mee 	return (op_encode_hdr_size + 1 + op->u.test_stateid.ts_num_ids)
29652282cd2cSKinglong Mee 		* sizeof(__be32);
29662282cd2cSKinglong Mee }
29672282cd2cSKinglong Mee 
29686604148cSChuck Lever static u32 nfsd4_setattr_rsize(const struct svc_rqst *rqstp,
29696604148cSChuck Lever 			       const struct nfsd4_op *op)
297058e7b33aSMi Jinlong {
297158e7b33aSMi Jinlong 	return (op_encode_hdr_size + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
297258e7b33aSMi Jinlong }
297358e7b33aSMi Jinlong 
29746604148cSChuck Lever static u32 nfsd4_secinfo_rsize(const struct svc_rqst *rqstp,
29756604148cSChuck Lever 			       const struct nfsd4_op *op)
29762282cd2cSKinglong Mee {
29772282cd2cSKinglong Mee 	return (op_encode_hdr_size + RPC_AUTH_MAXFLAVOR *
29782282cd2cSKinglong Mee 		(4 + XDR_QUADLEN(GSS_OID_MAX_LEN))) * sizeof(__be32);
29792282cd2cSKinglong Mee }
29802282cd2cSKinglong Mee 
29816604148cSChuck Lever static u32 nfsd4_setclientid_rsize(const struct svc_rqst *rqstp,
29826604148cSChuck Lever 				   const struct nfsd4_op *op)
298358e7b33aSMi Jinlong {
2984480efaeeSJ. Bruce Fields 	return (op_encode_hdr_size + 2 + XDR_QUADLEN(NFS4_VERIFIER_SIZE)) *
2985480efaeeSJ. Bruce Fields 								sizeof(__be32);
298658e7b33aSMi Jinlong }
298758e7b33aSMi Jinlong 
29886604148cSChuck Lever static u32 nfsd4_write_rsize(const struct svc_rqst *rqstp,
29896604148cSChuck Lever 			     const struct nfsd4_op *op)
299058e7b33aSMi Jinlong {
2991f34e432bSJ. Bruce Fields 	return (op_encode_hdr_size + 2 + op_encode_verifier_maxsz) * sizeof(__be32);
299258e7b33aSMi Jinlong }
299358e7b33aSMi Jinlong 
29946604148cSChuck Lever static u32 nfsd4_exchange_id_rsize(const struct svc_rqst *rqstp,
29956604148cSChuck Lever 				   const struct nfsd4_op *op)
299658e7b33aSMi Jinlong {
299758e7b33aSMi Jinlong 	return (op_encode_hdr_size + 2 + 1 + /* eir_clientid, eir_sequenceid */\
2998a8bb84bcSKinglong Mee 		1 + 1 + /* eir_flags, spr_how */\
2999a8bb84bcSKinglong Mee 		4 + /* spo_must_enforce & _allow with bitmap */\
300058e7b33aSMi Jinlong 		2 + /*eir_server_owner.so_minor_id */\
300158e7b33aSMi Jinlong 		/* eir_server_owner.so_major_id<> */\
300258e7b33aSMi Jinlong 		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
300358e7b33aSMi Jinlong 		/* eir_server_scope<> */\
300458e7b33aSMi Jinlong 		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
300558e7b33aSMi Jinlong 		1 + /* eir_server_impl_id array length */\
300658e7b33aSMi Jinlong 		0 /* ignored eir_server_impl_id contents */) * sizeof(__be32);
300758e7b33aSMi Jinlong }
300858e7b33aSMi Jinlong 
30096604148cSChuck Lever static u32 nfsd4_bind_conn_to_session_rsize(const struct svc_rqst *rqstp,
30106604148cSChuck Lever 					    const struct nfsd4_op *op)
301158e7b33aSMi Jinlong {
301258e7b33aSMi Jinlong 	return (op_encode_hdr_size + \
301358e7b33aSMi Jinlong 		XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* bctsr_sessid */\
301458e7b33aSMi Jinlong 		2 /* bctsr_dir, use_conn_in_rdma_mode */) * sizeof(__be32);
301558e7b33aSMi Jinlong }
301658e7b33aSMi Jinlong 
30176604148cSChuck Lever static u32 nfsd4_create_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) + /* sessionid */\
302258e7b33aSMi Jinlong 		2 + /* csr_sequence, csr_flags */\
302358e7b33aSMi Jinlong 		op_encode_channel_attrs_maxsz + \
302458e7b33aSMi Jinlong 		op_encode_channel_attrs_maxsz) * sizeof(__be32);
302558e7b33aSMi Jinlong }
302658e7b33aSMi Jinlong 
30276604148cSChuck Lever static u32 nfsd4_copy_rsize(const struct svc_rqst *rqstp,
30286604148cSChuck Lever 			    const struct nfsd4_op *op)
302929ae7f9dSAnna Schumaker {
303029ae7f9dSAnna Schumaker 	return (op_encode_hdr_size +
303129ae7f9dSAnna Schumaker 		1 /* wr_callback */ +
303229ae7f9dSAnna Schumaker 		op_encode_stateid_maxsz /* wr_callback */ +
303329ae7f9dSAnna Schumaker 		2 /* wr_count */ +
303429ae7f9dSAnna Schumaker 		1 /* wr_committed */ +
303529ae7f9dSAnna Schumaker 		op_encode_verifier_maxsz +
303629ae7f9dSAnna Schumaker 		1 /* cr_consecutive */ +
303729ae7f9dSAnna Schumaker 		1 /* cr_synchronous */) * sizeof(__be32);
303829ae7f9dSAnna Schumaker }
303929ae7f9dSAnna Schumaker 
30406604148cSChuck Lever static u32 nfsd4_offload_status_rsize(const struct svc_rqst *rqstp,
30416604148cSChuck Lever 				      const struct nfsd4_op *op)
30426308bc98SOlga Kornievskaia {
30436308bc98SOlga Kornievskaia 	return (op_encode_hdr_size +
30446308bc98SOlga Kornievskaia 		2 /* osr_count */ +
30456308bc98SOlga Kornievskaia 		1 /* osr_complete<1> optional 0 for now */) * sizeof(__be32);
30466308bc98SOlga Kornievskaia }
30476308bc98SOlga Kornievskaia 
30486604148cSChuck Lever static u32 nfsd4_copy_notify_rsize(const struct svc_rqst *rqstp,
30496604148cSChuck Lever 				   const struct nfsd4_op *op)
305051911868SOlga Kornievskaia {
305151911868SOlga Kornievskaia 	return (op_encode_hdr_size +
305251911868SOlga Kornievskaia 		3 /* cnr_lease_time */ +
305351911868SOlga Kornievskaia 		1 /* We support one cnr_source_server */ +
305451911868SOlga Kornievskaia 		1 /* cnr_stateid seq */ +
305551911868SOlga Kornievskaia 		op_encode_stateid_maxsz /* cnr_stateid */ +
305651911868SOlga Kornievskaia 		1 /* num cnr_source_server*/ +
305751911868SOlga Kornievskaia 		1 /* nl4_type */ +
305851911868SOlga Kornievskaia 		1 /* nl4 size */ +
305951911868SOlga Kornievskaia 		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) /*nl4_loc + nl4_loc_sz */)
306051911868SOlga Kornievskaia 		* sizeof(__be32);
306151911868SOlga Kornievskaia }
306251911868SOlga Kornievskaia 
30639cf514ccSChristoph Hellwig #ifdef CONFIG_NFSD_PNFS
30646604148cSChuck Lever static u32 nfsd4_getdeviceinfo_rsize(const struct svc_rqst *rqstp,
30656604148cSChuck Lever 				     const struct nfsd4_op *op)
30662282cd2cSKinglong Mee {
306776ce4dceSChuck Lever 	u32 rlen = min(op->u.getdeviceinfo.gd_maxcount, nfsd4_max_payload(rqstp));
30682282cd2cSKinglong Mee 
30692282cd2cSKinglong Mee 	return (op_encode_hdr_size +
30702282cd2cSKinglong Mee 		1 /* gd_layout_type*/ +
30712282cd2cSKinglong Mee 		XDR_QUADLEN(rlen) +
30722282cd2cSKinglong Mee 		2 /* gd_notify_types */) * sizeof(__be32);
30732282cd2cSKinglong Mee }
30742282cd2cSKinglong Mee 
30759cf514ccSChristoph Hellwig /*
30769cf514ccSChristoph Hellwig  * At this stage we don't really know what layout driver will handle the request,
30779cf514ccSChristoph Hellwig  * so we need to define an arbitrary upper bound here.
30789cf514ccSChristoph Hellwig  */
30799cf514ccSChristoph Hellwig #define MAX_LAYOUT_SIZE		128
30806604148cSChuck Lever static u32 nfsd4_layoutget_rsize(const struct svc_rqst *rqstp,
30816604148cSChuck Lever 				 const struct nfsd4_op *op)
30829cf514ccSChristoph Hellwig {
30839cf514ccSChristoph Hellwig 	return (op_encode_hdr_size +
30849cf514ccSChristoph Hellwig 		1 /* logr_return_on_close */ +
30859cf514ccSChristoph Hellwig 		op_encode_stateid_maxsz +
30869cf514ccSChristoph Hellwig 		1 /* nr of layouts */ +
30879cf514ccSChristoph Hellwig 		MAX_LAYOUT_SIZE) * sizeof(__be32);
30889cf514ccSChristoph Hellwig }
30899cf514ccSChristoph Hellwig 
30906604148cSChuck Lever static u32 nfsd4_layoutcommit_rsize(const struct svc_rqst *rqstp,
30916604148cSChuck Lever 				    const struct nfsd4_op *op)
30929cf514ccSChristoph Hellwig {
30939cf514ccSChristoph Hellwig 	return (op_encode_hdr_size +
30949cf514ccSChristoph Hellwig 		1 /* locr_newsize */ +
30959cf514ccSChristoph Hellwig 		2 /* ns_size */) * sizeof(__be32);
30969cf514ccSChristoph Hellwig }
30979cf514ccSChristoph Hellwig 
30986604148cSChuck Lever static u32 nfsd4_layoutreturn_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 /* lrs_stateid */ +
31039cf514ccSChristoph Hellwig 		op_encode_stateid_maxsz) * sizeof(__be32);
31049cf514ccSChristoph Hellwig }
31059cf514ccSChristoph Hellwig #endif /* CONFIG_NFSD_PNFS */
31069cf514ccSChristoph Hellwig 
31072282cd2cSKinglong Mee 
31086604148cSChuck Lever static u32 nfsd4_seek_rsize(const struct svc_rqst *rqstp,
31096604148cSChuck Lever 			    const struct nfsd4_op *op)
31102282cd2cSKinglong Mee {
31112282cd2cSKinglong Mee 	return (op_encode_hdr_size + 3) * sizeof(__be32);
31122282cd2cSKinglong Mee }
31132282cd2cSKinglong Mee 
31146604148cSChuck Lever static u32 nfsd4_getxattr_rsize(const struct svc_rqst *rqstp,
31156604148cSChuck Lever 				const struct nfsd4_op *op)
311623e50fe3SFrank van der Linden {
311776ce4dceSChuck Lever 	u32 rlen = min_t(u32, XATTR_SIZE_MAX, nfsd4_max_payload(rqstp));
311823e50fe3SFrank van der Linden 
311923e50fe3SFrank van der Linden 	return (op_encode_hdr_size + 1 + XDR_QUADLEN(rlen)) * sizeof(__be32);
312023e50fe3SFrank van der Linden }
312123e50fe3SFrank van der Linden 
31226604148cSChuck Lever static u32 nfsd4_setxattr_rsize(const struct svc_rqst *rqstp,
31236604148cSChuck Lever 				const struct nfsd4_op *op)
312423e50fe3SFrank van der Linden {
312523e50fe3SFrank van der Linden 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
312623e50fe3SFrank van der Linden 		* sizeof(__be32);
312723e50fe3SFrank van der Linden }
31286604148cSChuck Lever static u32 nfsd4_listxattrs_rsize(const struct svc_rqst *rqstp,
31296604148cSChuck Lever 				  const struct nfsd4_op *op)
313023e50fe3SFrank van der Linden {
313176ce4dceSChuck Lever 	u32 rlen = min(op->u.listxattrs.lsxa_maxcount, nfsd4_max_payload(rqstp));
313223e50fe3SFrank van der Linden 
313323e50fe3SFrank van der Linden 	return (op_encode_hdr_size + 4 + XDR_QUADLEN(rlen)) * sizeof(__be32);
313423e50fe3SFrank van der Linden }
313523e50fe3SFrank van der Linden 
31366604148cSChuck Lever static u32 nfsd4_removexattr_rsize(const struct svc_rqst *rqstp,
31376604148cSChuck Lever 				   const struct nfsd4_op *op)
313823e50fe3SFrank van der Linden {
313923e50fe3SFrank van der Linden 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
314023e50fe3SFrank van der Linden 		* sizeof(__be32);
314123e50fe3SFrank van der Linden }
314223e50fe3SFrank van der Linden 
314323e50fe3SFrank van der Linden 
3144bb2a8b0cSChristoph Hellwig static const struct nfsd4_operation nfsd4_ops[] = {
3145b591480bSJ.Bruce Fields 	[OP_ACCESS] = {
3146eb69853dSChristoph Hellwig 		.op_func = nfsd4_access,
3147b001a1b6SBenny Halevy 		.op_name = "OP_ACCESS",
31481c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_access_rsize,
3149b591480bSJ.Bruce Fields 	},
3150b591480bSJ.Bruce Fields 	[OP_CLOSE] = {
3151eb69853dSChristoph Hellwig 		.op_func = nfsd4_close,
315258e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
3153b001a1b6SBenny Halevy 		.op_name = "OP_CLOSE",
31541c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_status_stateid_rsize,
315557832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_closestateid,
3156b60e9859SChristoph Hellwig 		.op_set_currentstateid = nfsd4_set_closestateid,
3157b591480bSJ.Bruce Fields 	},
3158b591480bSJ.Bruce Fields 	[OP_COMMIT] = {
3159eb69853dSChristoph Hellwig 		.op_func = nfsd4_commit,
316058e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
3161b001a1b6SBenny Halevy 		.op_name = "OP_COMMIT",
31621c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_commit_rsize,
3163b591480bSJ.Bruce Fields 	},
3164b591480bSJ.Bruce Fields 	[OP_CREATE] = {
3165eb69853dSChristoph Hellwig 		.op_func = nfsd4_create,
3166d1471053STigran Mkrtchyan 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME | OP_CLEAR_STATEID,
3167b001a1b6SBenny Halevy 		.op_name = "OP_CREATE",
31681c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_create_rsize,
3169b591480bSJ.Bruce Fields 	},
3170b591480bSJ.Bruce Fields 	[OP_DELEGRETURN] = {
3171eb69853dSChristoph Hellwig 		.op_func = nfsd4_delegreturn,
317258e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
3173b001a1b6SBenny Halevy 		.op_name = "OP_DELEGRETURN",
317458e7b33aSMi Jinlong 		.op_rsize_bop = nfsd4_only_status_rsize,
317557832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_delegreturnstateid,
3176b591480bSJ.Bruce Fields 	},
3177b591480bSJ.Bruce Fields 	[OP_GETATTR] = {
3178eb69853dSChristoph Hellwig 		.op_func = nfsd4_getattr,
3179eeac294eSJ.Bruce Fields 		.op_flags = ALLOWED_ON_ABSENT_FS,
3180b86cef60SJ. Bruce Fields 		.op_rsize_bop = nfsd4_getattr_rsize,
3181b001a1b6SBenny Halevy 		.op_name = "OP_GETATTR",
3182b591480bSJ.Bruce Fields 	},
3183b591480bSJ.Bruce Fields 	[OP_GETFH] = {
3184eb69853dSChristoph Hellwig 		.op_func = nfsd4_getfh,
3185b001a1b6SBenny Halevy 		.op_name = "OP_GETFH",
31861c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_getfh_rsize,
3187b591480bSJ.Bruce Fields 	},
3188b591480bSJ.Bruce Fields 	[OP_LINK] = {
3189eb69853dSChristoph Hellwig 		.op_func = nfsd4_link,
3190c856694eSJ. Bruce Fields 		.op_flags = ALLOWED_ON_ABSENT_FS | OP_MODIFIES_SOMETHING
3191c856694eSJ. Bruce Fields 				| OP_CACHEME,
3192b001a1b6SBenny Halevy 		.op_name = "OP_LINK",
31931c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_link_rsize,
3194b591480bSJ.Bruce Fields 	},
3195b591480bSJ.Bruce Fields 	[OP_LOCK] = {
3196eb69853dSChristoph Hellwig 		.op_func = nfsd4_lock,
3197b7571e4cSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING |
3198b7571e4cSJ. Bruce Fields 				OP_NONTRIVIAL_ERROR_ENCODE,
3199b001a1b6SBenny Halevy 		.op_name = "OP_LOCK",
32001c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_lock_rsize,
3201b60e9859SChristoph Hellwig 		.op_set_currentstateid = nfsd4_set_lockstateid,
3202b591480bSJ.Bruce Fields 	},
3203b591480bSJ.Bruce Fields 	[OP_LOCKT] = {
3204eb69853dSChristoph Hellwig 		.op_func = nfsd4_lockt,
3205b7571e4cSJ. Bruce Fields 		.op_flags = OP_NONTRIVIAL_ERROR_ENCODE,
3206b001a1b6SBenny Halevy 		.op_name = "OP_LOCKT",
32071c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_lock_rsize,
3208b591480bSJ.Bruce Fields 	},
3209b591480bSJ.Bruce Fields 	[OP_LOCKU] = {
3210eb69853dSChristoph Hellwig 		.op_func = nfsd4_locku,
321158e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
3212b001a1b6SBenny Halevy 		.op_name = "OP_LOCKU",
32131c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_status_stateid_rsize,
321457832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_lockustateid,
3215b591480bSJ.Bruce Fields 	},
3216b591480bSJ.Bruce Fields 	[OP_LOOKUP] = {
3217eb69853dSChristoph Hellwig 		.op_func = nfsd4_lookup,
3218d1471053STigran Mkrtchyan 		.op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
3219b001a1b6SBenny Halevy 		.op_name = "OP_LOOKUP",
32201c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3221b591480bSJ.Bruce Fields 	},
3222b591480bSJ.Bruce Fields 	[OP_LOOKUPP] = {
3223eb69853dSChristoph Hellwig 		.op_func = nfsd4_lookupp,
3224d1471053STigran Mkrtchyan 		.op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
3225b001a1b6SBenny Halevy 		.op_name = "OP_LOOKUPP",
32261c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3227b591480bSJ.Bruce Fields 	},
3228b591480bSJ.Bruce Fields 	[OP_NVERIFY] = {
3229eb69853dSChristoph Hellwig 		.op_func = nfsd4_nverify,
3230b001a1b6SBenny Halevy 		.op_name = "OP_NVERIFY",
32311c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3232b591480bSJ.Bruce Fields 	},
3233b591480bSJ.Bruce Fields 	[OP_OPEN] = {
3234eb69853dSChristoph Hellwig 		.op_func = nfsd4_open,
323558e7b33aSMi Jinlong 		.op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
3236b001a1b6SBenny Halevy 		.op_name = "OP_OPEN",
32371c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_open_rsize,
3238b60e9859SChristoph Hellwig 		.op_set_currentstateid = nfsd4_set_openstateid,
3239b591480bSJ.Bruce Fields 	},
3240b591480bSJ.Bruce Fields 	[OP_OPEN_CONFIRM] = {
3241eb69853dSChristoph Hellwig 		.op_func = nfsd4_open_confirm,
324258e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
3243b001a1b6SBenny Halevy 		.op_name = "OP_OPEN_CONFIRM",
32441c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_status_stateid_rsize,
3245b591480bSJ.Bruce Fields 	},
3246b591480bSJ.Bruce Fields 	[OP_OPEN_DOWNGRADE] = {
3247eb69853dSChristoph Hellwig 		.op_func = nfsd4_open_downgrade,
324858e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
3249b001a1b6SBenny Halevy 		.op_name = "OP_OPEN_DOWNGRADE",
32501c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_status_stateid_rsize,
325157832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_opendowngradestateid,
3252b60e9859SChristoph Hellwig 		.op_set_currentstateid = nfsd4_set_opendowngradestateid,
3253b591480bSJ.Bruce Fields 	},
3254b591480bSJ.Bruce Fields 	[OP_PUTFH] = {
3255eb69853dSChristoph Hellwig 		.op_func = nfsd4_putfh,
325668d93184SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
32575b648699SJ. Bruce Fields 				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
3258b001a1b6SBenny Halevy 		.op_name = "OP_PUTFH",
32591c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3260b591480bSJ.Bruce Fields 	},
3261eeac294eSJ.Bruce Fields 	[OP_PUTPUBFH] = {
3262eb69853dSChristoph Hellwig 		.op_func = nfsd4_putrootfh,
326368d93184SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
32645b648699SJ. Bruce Fields 				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
3265b001a1b6SBenny Halevy 		.op_name = "OP_PUTPUBFH",
32661c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3267eeac294eSJ.Bruce Fields 	},
3268b591480bSJ.Bruce Fields 	[OP_PUTROOTFH] = {
3269eb69853dSChristoph Hellwig 		.op_func = nfsd4_putrootfh,
327068d93184SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
32715b648699SJ. Bruce Fields 				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
3272b001a1b6SBenny Halevy 		.op_name = "OP_PUTROOTFH",
32731c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3274b591480bSJ.Bruce Fields 	},
3275b591480bSJ.Bruce Fields 	[OP_READ] = {
3276eb69853dSChristoph Hellwig 		.op_func = nfsd4_read,
327734b1744cSJ. Bruce Fields 		.op_release = nfsd4_read_release,
3278b001a1b6SBenny Halevy 		.op_name = "OP_READ",
32791c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_read_rsize,
328057832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_readstateid,
3281b591480bSJ.Bruce Fields 	},
3282b591480bSJ.Bruce Fields 	[OP_READDIR] = {
3283eb69853dSChristoph Hellwig 		.op_func = nfsd4_readdir,
3284b001a1b6SBenny Halevy 		.op_name = "OP_READDIR",
32851c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_readdir_rsize,
3286b591480bSJ.Bruce Fields 	},
3287b591480bSJ.Bruce Fields 	[OP_READLINK] = {
3288eb69853dSChristoph Hellwig 		.op_func = nfsd4_readlink,
3289b001a1b6SBenny Halevy 		.op_name = "OP_READLINK",
32901c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_readlink_rsize,
3291b591480bSJ.Bruce Fields 	},
3292b591480bSJ.Bruce Fields 	[OP_REMOVE] = {
3293eb69853dSChristoph Hellwig 		.op_func = nfsd4_remove,
3294c856694eSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3295b001a1b6SBenny Halevy 		.op_name = "OP_REMOVE",
32961c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_remove_rsize,
3297b591480bSJ.Bruce Fields 	},
3298b591480bSJ.Bruce Fields 	[OP_RENAME] = {
3299eb69853dSChristoph Hellwig 		.op_func = nfsd4_rename,
3300c856694eSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
330158e7b33aSMi Jinlong 		.op_name = "OP_RENAME",
33021c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_rename_rsize,
3303b591480bSJ.Bruce Fields 	},
3304b591480bSJ.Bruce Fields 	[OP_RENEW] = {
3305eb69853dSChristoph Hellwig 		.op_func = nfsd4_renew,
330658e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
330758e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
3308b001a1b6SBenny Halevy 		.op_name = "OP_RENEW",
33091c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
331058e7b33aSMi Jinlong 
3311b591480bSJ.Bruce Fields 	},
3312b591480bSJ.Bruce Fields 	[OP_RESTOREFH] = {
3313eb69853dSChristoph Hellwig 		.op_func = nfsd4_restorefh,
331468d93184SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
331558e7b33aSMi Jinlong 				| OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING,
3316b001a1b6SBenny Halevy 		.op_name = "OP_RESTOREFH",
33171c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3318b591480bSJ.Bruce Fields 	},
3319b591480bSJ.Bruce Fields 	[OP_SAVEFH] = {
3320eb69853dSChristoph Hellwig 		.op_func = nfsd4_savefh,
332158e7b33aSMi Jinlong 		.op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
3322b001a1b6SBenny Halevy 		.op_name = "OP_SAVEFH",
33231c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3324b591480bSJ.Bruce Fields 	},
3325dcb488a3SAndy Adamson 	[OP_SECINFO] = {
3326eb69853dSChristoph Hellwig 		.op_func = nfsd4_secinfo,
332734b1744cSJ. Bruce Fields 		.op_release = nfsd4_secinfo_release,
332868d93184SJ. Bruce Fields 		.op_flags = OP_HANDLES_WRONGSEC,
3329b001a1b6SBenny Halevy 		.op_name = "OP_SECINFO",
33301c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_secinfo_rsize,
3331dcb488a3SAndy Adamson 	},
3332b591480bSJ.Bruce Fields 	[OP_SETATTR] = {
3333eb69853dSChristoph Hellwig 		.op_func = nfsd4_setattr,
3334b001a1b6SBenny Halevy 		.op_name = "OP_SETATTR",
3335b7571e4cSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME
3336b7571e4cSJ. Bruce Fields 				| OP_NONTRIVIAL_ERROR_ENCODE,
33371c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_setattr_rsize,
333857832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_setattrstateid,
3339b591480bSJ.Bruce Fields 	},
3340b591480bSJ.Bruce Fields 	[OP_SETCLIENTID] = {
3341eb69853dSChristoph Hellwig 		.op_func = nfsd4_setclientid,
334258e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3343b7571e4cSJ. Bruce Fields 				| OP_MODIFIES_SOMETHING | OP_CACHEME
3344b7571e4cSJ. Bruce Fields 				| OP_NONTRIVIAL_ERROR_ENCODE,
3345b001a1b6SBenny Halevy 		.op_name = "OP_SETCLIENTID",
33461c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_setclientid_rsize,
3347b591480bSJ.Bruce Fields 	},
3348b591480bSJ.Bruce Fields 	[OP_SETCLIENTID_CONFIRM] = {
3349eb69853dSChristoph Hellwig 		.op_func = nfsd4_setclientid_confirm,
335058e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3351c856694eSJ. Bruce Fields 				| OP_MODIFIES_SOMETHING | OP_CACHEME,
3352b001a1b6SBenny Halevy 		.op_name = "OP_SETCLIENTID_CONFIRM",
33531c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3354b591480bSJ.Bruce Fields 	},
3355b591480bSJ.Bruce Fields 	[OP_VERIFY] = {
3356eb69853dSChristoph Hellwig 		.op_func = nfsd4_verify,
3357b001a1b6SBenny Halevy 		.op_name = "OP_VERIFY",
33581c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3359b591480bSJ.Bruce Fields 	},
3360b591480bSJ.Bruce Fields 	[OP_WRITE] = {
3361eb69853dSChristoph Hellwig 		.op_func = nfsd4_write,
3362c856694eSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3363b001a1b6SBenny Halevy 		.op_name = "OP_WRITE",
33641c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_write_rsize,
336557832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_writestateid,
3366b591480bSJ.Bruce Fields 	},
3367b591480bSJ.Bruce Fields 	[OP_RELEASE_LOCKOWNER] = {
3368eb69853dSChristoph Hellwig 		.op_func = nfsd4_release_lockowner,
336958e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
337058e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
3371b001a1b6SBenny Halevy 		.op_name = "OP_RELEASE_LOCKOWNER",
33721c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3373b591480bSJ.Bruce Fields 	},
3374069b6ad4SAndy Adamson 
3375069b6ad4SAndy Adamson 	/* NFSv4.1 operations */
3376069b6ad4SAndy Adamson 	[OP_EXCHANGE_ID] = {
3377eb69853dSChristoph Hellwig 		.op_func = nfsd4_exchange_id,
337858e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
337958e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
3380069b6ad4SAndy Adamson 		.op_name = "OP_EXCHANGE_ID",
33811c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_exchange_id_rsize,
3382069b6ad4SAndy Adamson 	},
3383cb73a9f4SJ. Bruce Fields 	[OP_BACKCHANNEL_CTL] = {
3384eb69853dSChristoph Hellwig 		.op_func = nfsd4_backchannel_ctl,
3385cb73a9f4SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
3386cb73a9f4SJ. Bruce Fields 		.op_name = "OP_BACKCHANNEL_CTL",
33871c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3388cb73a9f4SJ. Bruce Fields 	},
33891d1bc8f2SJ. Bruce Fields 	[OP_BIND_CONN_TO_SESSION] = {
3390eb69853dSChristoph Hellwig 		.op_func = nfsd4_bind_conn_to_session,
339158e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
339258e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
33931d1bc8f2SJ. Bruce Fields 		.op_name = "OP_BIND_CONN_TO_SESSION",
33941c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_bind_conn_to_session_rsize,
33951d1bc8f2SJ. Bruce Fields 	},
3396069b6ad4SAndy Adamson 	[OP_CREATE_SESSION] = {
3397eb69853dSChristoph Hellwig 		.op_func = nfsd4_create_session,
339858e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
339958e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
3400069b6ad4SAndy Adamson 		.op_name = "OP_CREATE_SESSION",
34011c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_create_session_rsize,
3402069b6ad4SAndy Adamson 	},
3403069b6ad4SAndy Adamson 	[OP_DESTROY_SESSION] = {
3404eb69853dSChristoph Hellwig 		.op_func = nfsd4_destroy_session,
340558e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
340658e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
3407069b6ad4SAndy Adamson 		.op_name = "OP_DESTROY_SESSION",
34081c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3409069b6ad4SAndy Adamson 	},
3410069b6ad4SAndy Adamson 	[OP_SEQUENCE] = {
3411eb69853dSChristoph Hellwig 		.op_func = nfsd4_sequence,
3412f9bb94c4SAndy Adamson 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
3413069b6ad4SAndy Adamson 		.op_name = "OP_SEQUENCE",
34141c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_sequence_rsize,
3415069b6ad4SAndy Adamson 	},
3416094b5d74SBenny Halevy 	[OP_DESTROY_CLIENTID] = {
3417eb69853dSChristoph Hellwig 		.op_func = nfsd4_destroy_clientid,
341858e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
341958e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
3420094b5d74SBenny Halevy 		.op_name = "OP_DESTROY_CLIENTID",
34211c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3422094b5d74SBenny Halevy 	},
34234dc6ec00SJ. Bruce Fields 	[OP_RECLAIM_COMPLETE] = {
3424eb69853dSChristoph Hellwig 		.op_func = nfsd4_reclaim_complete,
342558e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
34264dc6ec00SJ. Bruce Fields 		.op_name = "OP_RECLAIM_COMPLETE",
34271c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
34284dc6ec00SJ. Bruce Fields 	},
342904f4ad16SJ. Bruce Fields 	[OP_SECINFO_NO_NAME] = {
3430eb69853dSChristoph Hellwig 		.op_func = nfsd4_secinfo_no_name,
3431ec572b9eSEryu Guan 		.op_release = nfsd4_secinfo_no_name_release,
343268d93184SJ. Bruce Fields 		.op_flags = OP_HANDLES_WRONGSEC,
343304f4ad16SJ. Bruce Fields 		.op_name = "OP_SECINFO_NO_NAME",
34341c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_secinfo_rsize,
343504f4ad16SJ. Bruce Fields 	},
343617456804SBryan Schumaker 	[OP_TEST_STATEID] = {
3437eb69853dSChristoph Hellwig 		.op_func = nfsd4_test_stateid,
343817456804SBryan Schumaker 		.op_flags = ALLOWED_WITHOUT_FH,
343917456804SBryan Schumaker 		.op_name = "OP_TEST_STATEID",
34401c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_test_stateid_rsize,
344117456804SBryan Schumaker 	},
3442e1ca12dfSBryan Schumaker 	[OP_FREE_STATEID] = {
3443eb69853dSChristoph Hellwig 		.op_func = nfsd4_free_stateid,
344458e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
3445e1ca12dfSBryan Schumaker 		.op_name = "OP_FREE_STATEID",
344657832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_freestateid,
34471c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3448e1ca12dfSBryan Schumaker 	},
34499cf514ccSChristoph Hellwig #ifdef CONFIG_NFSD_PNFS
34509cf514ccSChristoph Hellwig 	[OP_GETDEVICEINFO] = {
3451eb69853dSChristoph Hellwig 		.op_func = nfsd4_getdeviceinfo,
345234b1744cSJ. Bruce Fields 		.op_release = nfsd4_getdeviceinfo_release,
34539cf514ccSChristoph Hellwig 		.op_flags = ALLOWED_WITHOUT_FH,
34549cf514ccSChristoph Hellwig 		.op_name = "OP_GETDEVICEINFO",
34551c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_getdeviceinfo_rsize,
34569cf514ccSChristoph Hellwig 	},
34579cf514ccSChristoph Hellwig 	[OP_LAYOUTGET] = {
3458eb69853dSChristoph Hellwig 		.op_func = nfsd4_layoutget,
345934b1744cSJ. Bruce Fields 		.op_release = nfsd4_layoutget_release,
34609cf514ccSChristoph Hellwig 		.op_flags = OP_MODIFIES_SOMETHING,
34619cf514ccSChristoph Hellwig 		.op_name = "OP_LAYOUTGET",
34621c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_layoutget_rsize,
34639cf514ccSChristoph Hellwig 	},
34649cf514ccSChristoph Hellwig 	[OP_LAYOUTCOMMIT] = {
3465eb69853dSChristoph Hellwig 		.op_func = nfsd4_layoutcommit,
34669cf514ccSChristoph Hellwig 		.op_flags = OP_MODIFIES_SOMETHING,
34679cf514ccSChristoph Hellwig 		.op_name = "OP_LAYOUTCOMMIT",
34681c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_layoutcommit_rsize,
34699cf514ccSChristoph Hellwig 	},
34709cf514ccSChristoph Hellwig 	[OP_LAYOUTRETURN] = {
3471eb69853dSChristoph Hellwig 		.op_func = nfsd4_layoutreturn,
34729cf514ccSChristoph Hellwig 		.op_flags = OP_MODIFIES_SOMETHING,
34739cf514ccSChristoph Hellwig 		.op_name = "OP_LAYOUTRETURN",
34741c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_layoutreturn_rsize,
34759cf514ccSChristoph Hellwig 	},
34769cf514ccSChristoph Hellwig #endif /* CONFIG_NFSD_PNFS */
347724bab491SAnna Schumaker 
347824bab491SAnna Schumaker 	/* NFSv4.2 operations */
347995d871f0SAnna Schumaker 	[OP_ALLOCATE] = {
3480eb69853dSChristoph Hellwig 		.op_func = nfsd4_allocate,
348103b31f48SOlga Kornievskaia 		.op_flags = OP_MODIFIES_SOMETHING,
348295d871f0SAnna Schumaker 		.op_name = "OP_ALLOCATE",
34831c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
348495d871f0SAnna Schumaker 	},
3485b0cb9085SAnna Schumaker 	[OP_DEALLOCATE] = {
3486eb69853dSChristoph Hellwig 		.op_func = nfsd4_deallocate,
348703b31f48SOlga Kornievskaia 		.op_flags = OP_MODIFIES_SOMETHING,
3488b0cb9085SAnna Schumaker 		.op_name = "OP_DEALLOCATE",
34891c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3490b0cb9085SAnna Schumaker 	},
3491ffa0160aSChristoph Hellwig 	[OP_CLONE] = {
3492eb69853dSChristoph Hellwig 		.op_func = nfsd4_clone,
349303b31f48SOlga Kornievskaia 		.op_flags = OP_MODIFIES_SOMETHING,
3494ffa0160aSChristoph Hellwig 		.op_name = "OP_CLONE",
34951c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
3496ffa0160aSChristoph Hellwig 	},
349729ae7f9dSAnna Schumaker 	[OP_COPY] = {
3498eb69853dSChristoph Hellwig 		.op_func = nfsd4_copy,
349903b31f48SOlga Kornievskaia 		.op_flags = OP_MODIFIES_SOMETHING,
350029ae7f9dSAnna Schumaker 		.op_name = "OP_COPY",
35011c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_copy_rsize,
350229ae7f9dSAnna Schumaker 	},
3503528b8493SAnna Schumaker 	[OP_READ_PLUS] = {
3504528b8493SAnna Schumaker 		.op_func = nfsd4_read,
3505528b8493SAnna Schumaker 		.op_release = nfsd4_read_release,
3506528b8493SAnna Schumaker 		.op_name = "OP_READ_PLUS",
3507528b8493SAnna Schumaker 		.op_rsize_bop = nfsd4_read_plus_rsize,
3508528b8493SAnna Schumaker 		.op_get_currentstateid = nfsd4_get_readstateid,
3509528b8493SAnna Schumaker 	},
351024bab491SAnna Schumaker 	[OP_SEEK] = {
3511eb69853dSChristoph Hellwig 		.op_func = nfsd4_seek,
351224bab491SAnna Schumaker 		.op_name = "OP_SEEK",
35131c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_seek_rsize,
351424bab491SAnna Schumaker 	},
35156308bc98SOlga Kornievskaia 	[OP_OFFLOAD_STATUS] = {
35166308bc98SOlga Kornievskaia 		.op_func = nfsd4_offload_status,
35176308bc98SOlga Kornievskaia 		.op_name = "OP_OFFLOAD_STATUS",
35186308bc98SOlga Kornievskaia 		.op_rsize_bop = nfsd4_offload_status_rsize,
35196308bc98SOlga Kornievskaia 	},
3520885e2bf3SOlga Kornievskaia 	[OP_OFFLOAD_CANCEL] = {
3521885e2bf3SOlga Kornievskaia 		.op_func = nfsd4_offload_cancel,
3522885e2bf3SOlga Kornievskaia 		.op_flags = OP_MODIFIES_SOMETHING,
3523885e2bf3SOlga Kornievskaia 		.op_name = "OP_OFFLOAD_CANCEL",
3524885e2bf3SOlga Kornievskaia 		.op_rsize_bop = nfsd4_only_status_rsize,
3525885e2bf3SOlga Kornievskaia 	},
352651911868SOlga Kornievskaia 	[OP_COPY_NOTIFY] = {
352751911868SOlga Kornievskaia 		.op_func = nfsd4_copy_notify,
352851911868SOlga Kornievskaia 		.op_flags = OP_MODIFIES_SOMETHING,
352951911868SOlga Kornievskaia 		.op_name = "OP_COPY_NOTIFY",
353051911868SOlga Kornievskaia 		.op_rsize_bop = nfsd4_copy_notify_rsize,
353151911868SOlga Kornievskaia 	},
353223e50fe3SFrank van der Linden 	[OP_GETXATTR] = {
353323e50fe3SFrank van der Linden 		.op_func = nfsd4_getxattr,
353423e50fe3SFrank van der Linden 		.op_name = "OP_GETXATTR",
353523e50fe3SFrank van der Linden 		.op_rsize_bop = nfsd4_getxattr_rsize,
353623e50fe3SFrank van der Linden 	},
353723e50fe3SFrank van der Linden 	[OP_SETXATTR] = {
353823e50fe3SFrank van der Linden 		.op_func = nfsd4_setxattr,
353923e50fe3SFrank van der Linden 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
354023e50fe3SFrank van der Linden 		.op_name = "OP_SETXATTR",
354123e50fe3SFrank van der Linden 		.op_rsize_bop = nfsd4_setxattr_rsize,
354223e50fe3SFrank van der Linden 	},
354323e50fe3SFrank van der Linden 	[OP_LISTXATTRS] = {
354423e50fe3SFrank van der Linden 		.op_func = nfsd4_listxattrs,
354523e50fe3SFrank van der Linden 		.op_name = "OP_LISTXATTRS",
354623e50fe3SFrank van der Linden 		.op_rsize_bop = nfsd4_listxattrs_rsize,
354723e50fe3SFrank van der Linden 	},
354823e50fe3SFrank van der Linden 	[OP_REMOVEXATTR] = {
354923e50fe3SFrank van der Linden 		.op_func = nfsd4_removexattr,
355023e50fe3SFrank van der Linden 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
355123e50fe3SFrank van der Linden 		.op_name = "OP_REMOVEXATTR",
355223e50fe3SFrank van der Linden 		.op_rsize_bop = nfsd4_removexattr_rsize,
355323e50fe3SFrank van der Linden 	},
3554b591480bSJ.Bruce Fields };
3555b591480bSJ.Bruce Fields 
3556ed941643SAndrew Elble /**
3557ed941643SAndrew Elble  * nfsd4_spo_must_allow - Determine if the compound op contains an
3558ed941643SAndrew Elble  * operation that is allowed to be sent with machine credentials
3559ed941643SAndrew Elble  *
3560ed941643SAndrew Elble  * @rqstp: a pointer to the struct svc_rqst
3561ed941643SAndrew Elble  *
3562ed941643SAndrew Elble  * Checks to see if the compound contains a spo_must_allow op
3563ed941643SAndrew Elble  * and confirms that it was sent with the proper machine creds.
3564ed941643SAndrew Elble  */
3565ed941643SAndrew Elble 
3566ed941643SAndrew Elble bool nfsd4_spo_must_allow(struct svc_rqst *rqstp)
3567ed941643SAndrew Elble {
3568ed941643SAndrew Elble 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
3569ed941643SAndrew Elble 	struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3570e34c0ce9SColin Ian King 	struct nfsd4_op *this;
3571ed941643SAndrew Elble 	struct nfsd4_compound_state *cstate = &resp->cstate;
3572ed941643SAndrew Elble 	struct nfs4_op_map *allow = &cstate->clp->cl_spo_must_allow;
3573ed941643SAndrew Elble 	u32 opiter;
3574ed941643SAndrew Elble 
3575ed941643SAndrew Elble 	if (!cstate->minorversion)
3576ed941643SAndrew Elble 		return false;
3577ed941643SAndrew Elble 
357844b49aa6SZheng Bin 	if (cstate->spo_must_allowed)
3579ed941643SAndrew Elble 		return true;
3580ed941643SAndrew Elble 
3581ed941643SAndrew Elble 	opiter = resp->opcnt;
3582ed941643SAndrew Elble 	while (opiter < argp->opcnt) {
3583ed941643SAndrew Elble 		this = &argp->ops[opiter++];
3584ed941643SAndrew Elble 		if (test_bit(this->opnum, allow->u.longs) &&
3585ed941643SAndrew Elble 			cstate->clp->cl_mach_cred &&
3586ed941643SAndrew Elble 			nfsd4_mach_creds_match(cstate->clp, rqstp)) {
3587ed941643SAndrew Elble 			cstate->spo_must_allowed = true;
3588ed941643SAndrew Elble 			return true;
3589ed941643SAndrew Elble 		}
3590ed941643SAndrew Elble 	}
3591ed941643SAndrew Elble 	cstate->spo_must_allowed = false;
3592ed941643SAndrew Elble 	return false;
3593ed941643SAndrew Elble }
3594ed941643SAndrew Elble 
35954f0cefbfSJ. Bruce Fields int nfsd4_max_reply(struct svc_rqst *rqstp, struct nfsd4_op *op)
35964f0cefbfSJ. Bruce Fields {
359705b7278dSOlga Kornievskaia 	if (op->opnum == OP_ILLEGAL || op->status == nfserr_notsupp)
35984f0cefbfSJ. Bruce Fields 		return op_encode_hdr_size * sizeof(__be32);
35992282cd2cSKinglong Mee 
36002282cd2cSKinglong Mee 	BUG_ON(OPDESC(op)->op_rsize_bop == NULL);
36012282cd2cSKinglong Mee 	return OPDESC(op)->op_rsize_bop(rqstp, op);
36024f0cefbfSJ. Bruce Fields }
36034f0cefbfSJ. Bruce Fields 
360407d1f802SJ. Bruce Fields void warn_on_nonidempotent_op(struct nfsd4_op *op)
360507d1f802SJ. Bruce Fields {
360607d1f802SJ. Bruce Fields 	if (OPDESC(op)->op_flags & OP_MODIFIES_SOMETHING) {
36073a237b4aSChuck Lever 		pr_err("unable to encode reply to nonidempotent op %u (%s)\n",
360807d1f802SJ. Bruce Fields 			op->opnum, nfsd4_op_name(op->opnum));
360907d1f802SJ. Bruce Fields 		WARN_ON_ONCE(1);
361007d1f802SJ. Bruce Fields 	}
361107d1f802SJ. Bruce Fields }
361207d1f802SJ. Bruce Fields 
3613f1c7f79bSAdrian Bunk static const char *nfsd4_op_name(unsigned opnum)
3614b001a1b6SBenny Halevy {
3615b001a1b6SBenny Halevy 	if (opnum < ARRAY_SIZE(nfsd4_ops))
3616b001a1b6SBenny Halevy 		return nfsd4_ops[opnum].op_name;
3617b001a1b6SBenny Halevy 	return "unknown_operation";
3618b001a1b6SBenny Halevy }
3619b001a1b6SBenny Halevy 
3620860bda29SChristoph Hellwig static const struct svc_procedure nfsd_procedures4[2] = {
36210a93a47fSYu Zhiguo 	[NFSPROC4_NULL] = {
3622f7235b6bSChristoph Hellwig 		.pc_func = nfsd4_proc_null,
3623788f7183SChuck Lever 		.pc_decode = nfssvc_decode_voidarg,
3624788f7183SChuck Lever 		.pc_encode = nfssvc_encode_voidres,
3625788f7183SChuck Lever 		.pc_argsize = sizeof(struct nfsd_voidargs),
3626103cc1faSChuck Lever 		.pc_argzero = sizeof(struct nfsd_voidargs),
3627788f7183SChuck Lever 		.pc_ressize = sizeof(struct nfsd_voidres),
36280a93a47fSYu Zhiguo 		.pc_cachetype = RC_NOCACHE,
36290a93a47fSYu Zhiguo 		.pc_xdrressize = 1,
36302289e87bSChuck Lever 		.pc_name = "NULL",
36310a93a47fSYu Zhiguo 	},
36320a93a47fSYu Zhiguo 	[NFSPROC4_COMPOUND] = {
3633f7235b6bSChristoph Hellwig 		.pc_func = nfsd4_proc_compound,
3634026fec7eSChristoph Hellwig 		.pc_decode = nfs4svc_decode_compoundargs,
363563f8de37SChristoph Hellwig 		.pc_encode = nfs4svc_encode_compoundres,
36360a93a47fSYu Zhiguo 		.pc_argsize = sizeof(struct nfsd4_compoundargs),
36373fdc5464SChuck Lever 		.pc_argzero = offsetof(struct nfsd4_compoundargs, iops),
36380a93a47fSYu Zhiguo 		.pc_ressize = sizeof(struct nfsd4_compoundres),
36393e98abffSJ. Bruce Fields 		.pc_release = nfsd4_release_compoundargs,
36400a93a47fSYu Zhiguo 		.pc_cachetype = RC_NOCACHE,
36410a93a47fSYu Zhiguo 		.pc_xdrressize = NFSD_BUFSIZE/4,
36422289e87bSChuck Lever 		.pc_name = "COMPOUND",
36430a93a47fSYu Zhiguo 	},
36441da177e4SLinus Torvalds };
36451da177e4SLinus Torvalds 
364665ba3d24SChuck Lever static DEFINE_PER_CPU_ALIGNED(unsigned long,
364765ba3d24SChuck Lever 			      nfsd_count4[ARRAY_SIZE(nfsd_procedures4)]);
3648e9679189SChristoph Hellwig const struct svc_version nfsd_version4 = {
36491da177e4SLinus Torvalds 	.vs_vers		= 4,
365065ba3d24SChuck Lever 	.vs_nproc		= ARRAY_SIZE(nfsd_procedures4),
36511da177e4SLinus Torvalds 	.vs_proc		= nfsd_procedures4,
365265ba3d24SChuck Lever 	.vs_count		= nfsd_count4,
36531da177e4SLinus Torvalds 	.vs_dispatch		= nfsd_dispatch,
36541da177e4SLinus Torvalds 	.vs_xdrsize		= NFS4_SVC_XDRSIZE,
365505a45a2dSJeff Layton 	.vs_rpcb_optnl		= true,
36565283b03eSJeff Layton 	.vs_need_cong_ctrl	= true,
36571da177e4SLinus Torvalds };
3658