xref: /openbmc/linux/fs/nfsd/nfs4proc.c (revision 51911868fc62f6b1bc460ea2d8bddece6c72e467)
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>
401da177e4SLinus Torvalds 
4158e7b33aSMi Jinlong #include "idmap.h"
429a74af21SBoaz Harrosh #include "cache.h"
439a74af21SBoaz Harrosh #include "xdr4.h"
440a3adadeSJ. Bruce Fields #include "vfs.h"
458b70484cSTigran Mkrtchyan #include "current_stateid.h"
463320fef1SStanislav Kinsbursky #include "netns.h"
474ac7249eSChristoph Hellwig #include "acl.h"
489cf514ccSChristoph Hellwig #include "pnfs.h"
4931ef83dcSChristoph Hellwig #include "trace.h"
501da177e4SLinus Torvalds 
5118032ca0SDavid Quigley #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
5218032ca0SDavid Quigley #include <linux/security.h>
5318032ca0SDavid Quigley 
5418032ca0SDavid Quigley static inline void
5518032ca0SDavid Quigley nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
5618032ca0SDavid Quigley {
572b0143b5SDavid Howells 	struct inode *inode = d_inode(resfh->fh_dentry);
5818032ca0SDavid Quigley 	int status;
5918032ca0SDavid Quigley 
605955102cSAl Viro 	inode_lock(inode);
6118032ca0SDavid Quigley 	status = security_inode_setsecctx(resfh->fh_dentry,
6218032ca0SDavid Quigley 		label->data, label->len);
635955102cSAl Viro 	inode_unlock(inode);
6418032ca0SDavid Quigley 
6518032ca0SDavid Quigley 	if (status)
6618032ca0SDavid Quigley 		/*
6718032ca0SDavid Quigley 		 * XXX: We should really fail the whole open, but we may
6818032ca0SDavid Quigley 		 * already have created a new file, so it may be too
6918032ca0SDavid Quigley 		 * late.  For now this seems the least of evils:
7018032ca0SDavid Quigley 		 */
7118032ca0SDavid Quigley 		bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
7218032ca0SDavid Quigley 
7318032ca0SDavid Quigley 	return;
7418032ca0SDavid Quigley }
7518032ca0SDavid Quigley #else
7618032ca0SDavid Quigley static inline void
7718032ca0SDavid Quigley nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
7818032ca0SDavid Quigley { }
7918032ca0SDavid Quigley #endif
8018032ca0SDavid Quigley 
811da177e4SLinus Torvalds #define NFSDDBG_FACILITY		NFSDDBG_PROC
821da177e4SLinus Torvalds 
833c8e0316SYu Zhiguo static u32 nfsd_attrmask[] = {
843c8e0316SYu Zhiguo 	NFSD_WRITEABLE_ATTRS_WORD0,
853c8e0316SYu Zhiguo 	NFSD_WRITEABLE_ATTRS_WORD1,
863c8e0316SYu Zhiguo 	NFSD_WRITEABLE_ATTRS_WORD2
873c8e0316SYu Zhiguo };
883c8e0316SYu Zhiguo 
893c8e0316SYu Zhiguo static u32 nfsd41_ex_attrmask[] = {
903c8e0316SYu Zhiguo 	NFSD_SUPPATTR_EXCLCREAT_WORD0,
913c8e0316SYu Zhiguo 	NFSD_SUPPATTR_EXCLCREAT_WORD1,
923c8e0316SYu Zhiguo 	NFSD_SUPPATTR_EXCLCREAT_WORD2
933c8e0316SYu Zhiguo };
943c8e0316SYu Zhiguo 
953c8e0316SYu Zhiguo static __be32
963c8e0316SYu Zhiguo check_attr_support(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
973c8e0316SYu Zhiguo 		   u32 *bmval, u32 *writable)
983c8e0316SYu Zhiguo {
993c8e0316SYu Zhiguo 	struct dentry *dentry = cstate->current_fh.fh_dentry;
10032ddd944SJ. Bruce Fields 	struct svc_export *exp = cstate->current_fh.fh_export;
1013c8e0316SYu Zhiguo 
102916d2d84SJ. Bruce Fields 	if (!nfsd_attrs_supported(cstate->minorversion, bmval))
1033c8e0316SYu Zhiguo 		return nfserr_attrnotsupp;
104916d2d84SJ. Bruce Fields 	if ((bmval[0] & FATTR4_WORD0_ACL) && !IS_POSIXACL(d_inode(dentry)))
1053c8e0316SYu Zhiguo 		return nfserr_attrnotsupp;
10632ddd944SJ. Bruce Fields 	if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) &&
10732ddd944SJ. Bruce Fields 			!(exp->ex_flags & NFSEXP_SECURITY_LABEL))
10832ddd944SJ. Bruce Fields 		return nfserr_attrnotsupp;
109916d2d84SJ. Bruce Fields 	if (writable && !bmval_is_subset(bmval, writable))
1103c8e0316SYu Zhiguo 		return nfserr_inval;
11147057abdSAndreas Gruenbacher 	if (writable && (bmval[2] & FATTR4_WORD2_MODE_UMASK) &&
11247057abdSAndreas Gruenbacher 			(bmval[1] & FATTR4_WORD1_MODE))
11347057abdSAndreas Gruenbacher 		return nfserr_inval;
1143c8e0316SYu Zhiguo 	return nfs_ok;
1153c8e0316SYu Zhiguo }
1163c8e0316SYu Zhiguo 
1173c8e0316SYu Zhiguo static __be32
1183c8e0316SYu Zhiguo nfsd4_check_open_attributes(struct svc_rqst *rqstp,
1193c8e0316SYu Zhiguo 	struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
1203c8e0316SYu Zhiguo {
1213c8e0316SYu Zhiguo 	__be32 status = nfs_ok;
1223c8e0316SYu Zhiguo 
1233c8e0316SYu Zhiguo 	if (open->op_create == NFS4_OPEN_CREATE) {
1243c8e0316SYu Zhiguo 		if (open->op_createmode == NFS4_CREATE_UNCHECKED
1253c8e0316SYu Zhiguo 		    || open->op_createmode == NFS4_CREATE_GUARDED)
1263c8e0316SYu Zhiguo 			status = check_attr_support(rqstp, cstate,
1273c8e0316SYu Zhiguo 					open->op_bmval, nfsd_attrmask);
1283c8e0316SYu Zhiguo 		else if (open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1)
1293c8e0316SYu Zhiguo 			status = check_attr_support(rqstp, cstate,
1303c8e0316SYu Zhiguo 					open->op_bmval, nfsd41_ex_attrmask);
1313c8e0316SYu Zhiguo 	}
1323c8e0316SYu Zhiguo 
1333c8e0316SYu Zhiguo 	return status;
1343c8e0316SYu Zhiguo }
1353c8e0316SYu Zhiguo 
1369208faf2SYu Zhiguo static int
1379208faf2SYu Zhiguo is_create_with_attrs(struct nfsd4_open *open)
1389208faf2SYu Zhiguo {
1399208faf2SYu Zhiguo 	return open->op_create == NFS4_OPEN_CREATE
1409208faf2SYu Zhiguo 		&& (open->op_createmode == NFS4_CREATE_UNCHECKED
1419208faf2SYu Zhiguo 		    || open->op_createmode == NFS4_CREATE_GUARDED
1429208faf2SYu Zhiguo 		    || open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1);
1439208faf2SYu Zhiguo }
1449208faf2SYu Zhiguo 
1459208faf2SYu Zhiguo /*
1469208faf2SYu Zhiguo  * if error occurs when setting the acl, just clear the acl bit
1479208faf2SYu Zhiguo  * in the returned attr bitmap.
1489208faf2SYu Zhiguo  */
1499208faf2SYu Zhiguo static void
1509208faf2SYu Zhiguo do_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
1519208faf2SYu Zhiguo 		struct nfs4_acl *acl, u32 *bmval)
1529208faf2SYu Zhiguo {
1539208faf2SYu Zhiguo 	__be32 status;
1549208faf2SYu Zhiguo 
1559208faf2SYu Zhiguo 	status = nfsd4_set_nfs4_acl(rqstp, fhp, acl);
1569208faf2SYu Zhiguo 	if (status)
1579208faf2SYu Zhiguo 		/*
1589208faf2SYu Zhiguo 		 * We should probably fail the whole open at this point,
1599208faf2SYu Zhiguo 		 * but we've already created the file, so it's too late;
1609208faf2SYu Zhiguo 		 * So this seems the least of evils:
1619208faf2SYu Zhiguo 		 */
1629208faf2SYu Zhiguo 		bmval[0] &= ~FATTR4_WORD0_ACL;
1639208faf2SYu Zhiguo }
1649208faf2SYu Zhiguo 
1651da177e4SLinus Torvalds static inline void
1661da177e4SLinus Torvalds fh_dup2(struct svc_fh *dst, struct svc_fh *src)
1671da177e4SLinus Torvalds {
1681da177e4SLinus Torvalds 	fh_put(dst);
1691da177e4SLinus Torvalds 	dget(src->fh_dentry);
1701da177e4SLinus Torvalds 	if (src->fh_export)
171bf18f163SKinglong Mee 		exp_get(src->fh_export);
1721da177e4SLinus Torvalds 	*dst = *src;
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds 
175b37ad28bSAl Viro static __be32
176dc730e17SJ. Bruce Fields do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, int accmode)
1771da177e4SLinus Torvalds {
178b37ad28bSAl Viro 	__be32 status;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	if (open->op_truncate &&
1811da177e4SLinus Torvalds 		!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
1821da177e4SLinus Torvalds 		return nfserr_inval;
1831da177e4SLinus Torvalds 
184a043226bSJ. Bruce Fields 	accmode |= NFSD_MAY_READ_IF_EXEC;
185a043226bSJ. Bruce Fields 
1861da177e4SLinus Torvalds 	if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
1878837abcaSMiklos Szeredi 		accmode |= NFSD_MAY_READ;
1889801d8a3SJ. Bruce Fields 	if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1898837abcaSMiklos Szeredi 		accmode |= (NFSD_MAY_WRITE | NFSD_MAY_TRUNC);
19057ecb34fSJ. Bruce Fields 	if (open->op_share_deny & NFS4_SHARE_DENY_READ)
1918837abcaSMiklos Szeredi 		accmode |= NFSD_MAY_WRITE;
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	status = fh_verify(rqstp, current_fh, S_IFREG, accmode);
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	return status;
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
198aadab6c6SJ. Bruce Fields static __be32 nfsd_check_obj_isreg(struct svc_fh *fh)
199aadab6c6SJ. Bruce Fields {
2002b0143b5SDavid Howells 	umode_t mode = d_inode(fh->fh_dentry)->i_mode;
201aadab6c6SJ. Bruce Fields 
202aadab6c6SJ. Bruce Fields 	if (S_ISREG(mode))
203aadab6c6SJ. Bruce Fields 		return nfs_ok;
204aadab6c6SJ. Bruce Fields 	if (S_ISDIR(mode))
205aadab6c6SJ. Bruce Fields 		return nfserr_isdir;
206aadab6c6SJ. Bruce Fields 	/*
207aadab6c6SJ. Bruce Fields 	 * Using err_symlink as our catch-all case may look odd; but
208aadab6c6SJ. Bruce Fields 	 * there's no other obvious error for this case in 4.0, and we
209aadab6c6SJ. Bruce Fields 	 * happen to know that it will cause the linux v4 client to do
210aadab6c6SJ. Bruce Fields 	 * the right thing on attempts to open something other than a
211aadab6c6SJ. Bruce Fields 	 * regular file.
212aadab6c6SJ. Bruce Fields 	 */
213aadab6c6SJ. Bruce Fields 	return nfserr_symlink;
214aadab6c6SJ. Bruce Fields }
215aadab6c6SJ. Bruce Fields 
216bbc9c36cSJ. Bruce Fields static void nfsd4_set_open_owner_reply_cache(struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh *resfh)
2171da177e4SLinus Torvalds {
218bbc9c36cSJ. Bruce Fields 	if (nfsd4_has_session(cstate))
219bbc9c36cSJ. Bruce Fields 		return;
220bbc9c36cSJ. Bruce Fields 	fh_copy_shallow(&open->op_openowner->oo_owner.so_replay.rp_openfh,
221bbc9c36cSJ. Bruce Fields 			&resfh->fh_handle);
222bbc9c36cSJ. Bruce Fields }
223bbc9c36cSJ. Bruce Fields 
224bbc9c36cSJ. Bruce Fields static __be32
225c0e6bee4SJ. Bruce Fields do_open_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh **resfh)
226bbc9c36cSJ. Bruce Fields {
227bbc9c36cSJ. Bruce Fields 	struct svc_fh *current_fh = &cstate->current_fh;
2287007c90fSNeil Brown 	int accmode;
229b37ad28bSAl Viro 	__be32 status;
2301da177e4SLinus Torvalds 
231c0e6bee4SJ. Bruce Fields 	*resfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
232c0e6bee4SJ. Bruce Fields 	if (!*resfh)
23359deeb9eSJ. Bruce Fields 		return nfserr_jukebox;
234c0e6bee4SJ. Bruce Fields 	fh_init(*resfh, NFS4_FHSIZE);
2351da177e4SLinus Torvalds 	open->op_truncate = 0;
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	if (open->op_create) {
23879fb54abSBenny Halevy 		/* FIXME: check session persistence and pnfs flags.
23979fb54abSBenny Halevy 		 * The nfsv4.1 spec requires the following semantics:
24079fb54abSBenny Halevy 		 *
24179fb54abSBenny Halevy 		 * Persistent   | pNFS   | Server REQUIRED | Client Allowed
24279fb54abSBenny Halevy 		 * Reply Cache  | server |                 |
24379fb54abSBenny Halevy 		 * -------------+--------+-----------------+--------------------
24479fb54abSBenny Halevy 		 * no           | no     | EXCLUSIVE4_1    | EXCLUSIVE4_1
24579fb54abSBenny Halevy 		 *              |        |                 | (SHOULD)
24679fb54abSBenny Halevy 		 *              |        | and EXCLUSIVE4  | or EXCLUSIVE4
24779fb54abSBenny Halevy 		 *              |        |                 | (SHOULD NOT)
24879fb54abSBenny Halevy 		 * no           | yes    | EXCLUSIVE4_1    | EXCLUSIVE4_1
24979fb54abSBenny Halevy 		 * yes          | no     | GUARDED4        | GUARDED4
25079fb54abSBenny Halevy 		 * yes          | yes    | GUARDED4        | GUARDED4
25179fb54abSBenny Halevy 		 */
25279fb54abSBenny Halevy 
2531da177e4SLinus Torvalds 		/*
2541da177e4SLinus Torvalds 		 * Note: create modes (UNCHECKED,GUARDED...) are the same
255ac6721a1SMi Jinlong 		 * in NFSv4 as in v3 except EXCLUSIVE4_1.
2561da177e4SLinus Torvalds 		 */
257880a3a53SJ. Bruce Fields 		current->fs->umask = open->op_umask;
258ac6721a1SMi Jinlong 		status = do_nfsd_create(rqstp, current_fh, open->op_fname.data,
2591da177e4SLinus Torvalds 					open->op_fname.len, &open->op_iattr,
260c0e6bee4SJ. Bruce Fields 					*resfh, open->op_createmode,
261749997e5SJeff Layton 					(u32 *)open->op_verf.data,
262856121b2SJ. Bruce Fields 					&open->op_truncate, &open->op_created);
263880a3a53SJ. Bruce Fields 		current->fs->umask = 0;
264749997e5SJeff Layton 
26518032ca0SDavid Quigley 		if (!status && open->op_label.len)
266c0e6bee4SJ. Bruce Fields 			nfsd4_security_inode_setsecctx(*resfh, &open->op_label, open->op_bmval);
26718032ca0SDavid Quigley 
26899f88726SJ. Bruce Fields 		/*
269ead8fb8cSKinglong Mee 		 * Following rfc 3530 14.2.16, and rfc 5661 18.16.4
270ead8fb8cSKinglong Mee 		 * use the returned bitmask to indicate which attributes
271ead8fb8cSKinglong Mee 		 * we used to store the verifier:
272749997e5SJeff Layton 		 */
273ead8fb8cSKinglong Mee 		if (nfsd_create_is_exclusive(open->op_createmode) && status == 0)
274ead8fb8cSKinglong Mee 			open->op_bmval[1] |= (FATTR4_WORD1_TIME_ACCESS |
275749997e5SJeff Layton 						FATTR4_WORD1_TIME_MODIFY);
2764335723eSJ. Bruce Fields 	} else
2774335723eSJ. Bruce Fields 		/*
2784335723eSJ. Bruce Fields 		 * Note this may exit with the parent still locked.
2794335723eSJ. Bruce Fields 		 * We will hold the lock until nfsd4_open's final
2804335723eSJ. Bruce Fields 		 * lookup, to prevent renames or unlinks until we've had
2814335723eSJ. Bruce Fields 		 * a chance to an acquire a delegation if appropriate.
2824335723eSJ. Bruce Fields 		 */
2831da177e4SLinus Torvalds 		status = nfsd_lookup(rqstp, current_fh,
284c0e6bee4SJ. Bruce Fields 				     open->op_fname.data, open->op_fname.len, *resfh);
285aadab6c6SJ. Bruce Fields 	if (status)
286aadab6c6SJ. Bruce Fields 		goto out;
287c0e6bee4SJ. Bruce Fields 	status = nfsd_check_obj_isreg(*resfh);
288af85852dSJ. Bruce Fields 	if (status)
289af85852dSJ. Bruce Fields 		goto out;
2901da177e4SLinus Torvalds 
2919208faf2SYu Zhiguo 	if (is_create_with_attrs(open) && open->op_acl != NULL)
292c0e6bee4SJ. Bruce Fields 		do_set_nfs4_acl(rqstp, *resfh, open->op_acl, open->op_bmval);
2939208faf2SYu Zhiguo 
294c0e6bee4SJ. Bruce Fields 	nfsd4_set_open_owner_reply_cache(cstate, open, *resfh);
2957007c90fSNeil Brown 	accmode = NFSD_MAY_NOP;
29689f6c336SJ. Bruce Fields 	if (open->op_created ||
29789f6c336SJ. Bruce Fields 			open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2987007c90fSNeil Brown 		accmode |= NFSD_MAY_OWNER_OVERRIDE;
299c0e6bee4SJ. Bruce Fields 	status = do_open_permission(rqstp, *resfh, open, accmode);
30041fd1e42SJ. Bruce Fields 	set_change_info(&open->op_cinfo, current_fh);
301af85852dSJ. Bruce Fields out:
3021da177e4SLinus Torvalds 	return status;
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds 
305b37ad28bSAl Viro static __be32
306bbc9c36cSJ. Bruce Fields do_open_fhandle(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
3071da177e4SLinus Torvalds {
308bbc9c36cSJ. Bruce Fields 	struct svc_fh *current_fh = &cstate->current_fh;
309b37ad28bSAl Viro 	__be32 status;
3109f415eb2SJ. Bruce Fields 	int accmode = 0;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	/* We don't know the target directory, and therefore can not
3131da177e4SLinus Torvalds 	* set the change info
3141da177e4SLinus Torvalds 	*/
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 	memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
3171da177e4SLinus Torvalds 
318bbc9c36cSJ. Bruce Fields 	nfsd4_set_open_owner_reply_cache(cstate, open, current_fh);
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
3211da177e4SLinus Torvalds 		(open->op_iattr.ia_size == 0);
3229f415eb2SJ. Bruce Fields 	/*
3239f415eb2SJ. Bruce Fields 	 * In the delegation case, the client is telling us about an
3249f415eb2SJ. Bruce Fields 	 * open that it *already* performed locally, some time ago.  We
3259f415eb2SJ. Bruce Fields 	 * should let it succeed now if possible.
3269f415eb2SJ. Bruce Fields 	 *
3279f415eb2SJ. Bruce Fields 	 * In the case of a CLAIM_FH open, on the other hand, the client
3289f415eb2SJ. Bruce Fields 	 * may be counting on us to enforce permissions (the Linux 4.1
3299f415eb2SJ. Bruce Fields 	 * client uses this for normal opens, for example).
3309f415eb2SJ. Bruce Fields 	 */
3319f415eb2SJ. Bruce Fields 	if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH)
3329f415eb2SJ. Bruce Fields 		accmode = NFSD_MAY_OWNER_OVERRIDE;
3331da177e4SLinus Torvalds 
3349f415eb2SJ. Bruce Fields 	status = do_open_permission(rqstp, current_fh, open, accmode);
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	return status;
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds 
33960adfc50SAndy Adamson static void
34060adfc50SAndy Adamson copy_clientid(clientid_t *clid, struct nfsd4_session *session)
34160adfc50SAndy Adamson {
34260adfc50SAndy Adamson 	struct nfsd4_sessionid *sid =
34360adfc50SAndy Adamson 			(struct nfsd4_sessionid *)session->se_sessionid.data;
34460adfc50SAndy Adamson 
34560adfc50SAndy Adamson 	clid->cl_boot = sid->clientid.cl_boot;
34660adfc50SAndy Adamson 	clid->cl_id = sid->clientid.cl_id;
34760adfc50SAndy Adamson }
3481da177e4SLinus Torvalds 
3497191155bSJ.Bruce Fields static __be32
350ca364317SJ.Bruce Fields nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
351eb69853dSChristoph Hellwig 	   union nfsd4_op_u *u)
3521da177e4SLinus Torvalds {
353eb69853dSChristoph Hellwig 	struct nfsd4_open *open = &u->open;
354b37ad28bSAl Viro 	__be32 status;
355c0e6bee4SJ. Bruce Fields 	struct svc_fh *resfh = NULL;
3563320fef1SStanislav Kinsbursky 	struct net *net = SVC_NET(rqstp);
3573320fef1SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
35803f318caSJ. Bruce Fields 	bool reclaim = false;
3596668958fSAndy Adamson 
360fe0750e5SJ. Bruce Fields 	dprintk("NFSD: nfsd4_open filename %.*s op_openowner %p\n",
3611da177e4SLinus Torvalds 		(int)open->op_fname.len, open->op_fname.data,
362fe0750e5SJ. Bruce Fields 		open->op_openowner);
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	/* This check required by spec. */
3651da177e4SLinus Torvalds 	if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
3661da177e4SLinus Torvalds 		return nfserr_inval;
3671da177e4SLinus Torvalds 
368856121b2SJ. Bruce Fields 	open->op_created = 0;
369ab1350b2SMi Jinlong 	/*
370ab1350b2SMi Jinlong 	 * RFC5661 18.51.3
371ab1350b2SMi Jinlong 	 * Before RECLAIM_COMPLETE done, server should deny new lock
372ab1350b2SMi Jinlong 	 */
373ab1350b2SMi Jinlong 	if (nfsd4_has_session(cstate) &&
374a52d726bSJeff Layton 	    !test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
375a52d726bSJeff Layton 		      &cstate->session->se_client->cl_flags) &&
376ab1350b2SMi Jinlong 	    open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
377ab1350b2SMi Jinlong 		return nfserr_grace;
378ab1350b2SMi Jinlong 
37960adfc50SAndy Adamson 	if (nfsd4_has_session(cstate))
38060adfc50SAndy Adamson 		copy_clientid(&open->op_clientid, cstate->session);
38160adfc50SAndy Adamson 
3821da177e4SLinus Torvalds 	/* check seqid for replay. set nfs4_owner */
3836cd22668SKinglong Mee 	status = nfsd4_process_open1(cstate, open, nn);
384a90b061cSAl Viro 	if (status == nfserr_replay_me) {
385fe0750e5SJ. Bruce Fields 		struct nfs4_replay *rp = &open->op_openowner->oo_owner.so_replay;
386ca364317SJ.Bruce Fields 		fh_put(&cstate->current_fh);
387a4773c08SJ. Bruce Fields 		fh_copy_shallow(&cstate->current_fh.fh_handle,
388a4773c08SJ. Bruce Fields 				&rp->rp_openfh);
3898837abcaSMiklos Szeredi 		status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
3901da177e4SLinus Torvalds 		if (status)
3911da177e4SLinus Torvalds 			dprintk("nfsd4_open: replay failed"
3921da177e4SLinus Torvalds 				" restoring previous filehandle\n");
3931da177e4SLinus Torvalds 		else
394a90b061cSAl Viro 			status = nfserr_replay_me;
3951da177e4SLinus Torvalds 	}
3961da177e4SLinus Torvalds 	if (status)
3971da177e4SLinus Torvalds 		goto out;
3989d313b17SJ. Bruce Fields 	if (open->op_xdr_error) {
3999d313b17SJ. Bruce Fields 		status = open->op_xdr_error;
4009d313b17SJ. Bruce Fields 		goto out;
4019d313b17SJ. Bruce Fields 	}
402fb553c0fSJ. Bruce Fields 
4033c8e0316SYu Zhiguo 	status = nfsd4_check_open_attributes(rqstp, cstate, open);
4043c8e0316SYu Zhiguo 	if (status)
4053c8e0316SYu Zhiguo 		goto out;
4063c8e0316SYu Zhiguo 
407fb553c0fSJ. Bruce Fields 	/* Openowner is now set, so sequence id will get bumped.  Now we need
408fb553c0fSJ. Bruce Fields 	 * these checks before we do any creates: */
409cbd0d51aSJ. Bruce Fields 	status = nfserr_grace;
410c87fb4a3SJ. Bruce Fields 	if (opens_in_grace(net) && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
411cbd0d51aSJ. Bruce Fields 		goto out;
412cbd0d51aSJ. Bruce Fields 	status = nfserr_no_grace;
413c87fb4a3SJ. Bruce Fields 	if (!opens_in_grace(net) && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
414cbd0d51aSJ. Bruce Fields 		goto out;
415fb553c0fSJ. Bruce Fields 
4161da177e4SLinus Torvalds 	switch (open->op_claim_type) {
4170dd3c192SNeilBrown 		case NFS4_OPEN_CLAIM_DELEGATE_CUR:
4181da177e4SLinus Torvalds 		case NFS4_OPEN_CLAIM_NULL:
419c0e6bee4SJ. Bruce Fields 			status = do_open_lookup(rqstp, cstate, open, &resfh);
4201da177e4SLinus Torvalds 			if (status)
4211da177e4SLinus Torvalds 				goto out;
4221da177e4SLinus Torvalds 			break;
4231da177e4SLinus Torvalds 		case NFS4_OPEN_CLAIM_PREVIOUS:
4243320fef1SStanislav Kinsbursky 			status = nfs4_check_open_reclaim(&open->op_clientid,
4250fe492dbSTrond Myklebust 							 cstate, nn);
4260cf99b91SMi Jinlong 			if (status)
4270cf99b91SMi Jinlong 				goto out;
428ba5378b6SJeff Layton 			open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
42903f318caSJ. Bruce Fields 			reclaim = true;
4300a4c9265SGustavo A. R. Silva 			/* fall through */
4318b289b2cSJ. Bruce Fields 		case NFS4_OPEN_CLAIM_FH:
4328b289b2cSJ. Bruce Fields 		case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
433bbc9c36cSJ. Bruce Fields 			status = do_open_fhandle(rqstp, cstate, open);
4341da177e4SLinus Torvalds 			if (status)
4351da177e4SLinus Torvalds 				goto out;
436c0e6bee4SJ. Bruce Fields 			resfh = &cstate->current_fh;
4371da177e4SLinus Torvalds 			break;
4388b289b2cSJ. Bruce Fields 		case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
4391da177e4SLinus Torvalds              	case NFS4_OPEN_CLAIM_DELEGATE_PREV:
4402fdada03SJ. Bruce Fields 			dprintk("NFSD: unsupported OPEN claim type %d\n",
4411da177e4SLinus Torvalds 				open->op_claim_type);
4421da177e4SLinus Torvalds 			status = nfserr_notsupp;
4431da177e4SLinus Torvalds 			goto out;
4441da177e4SLinus Torvalds 		default:
4452fdada03SJ. Bruce Fields 			dprintk("NFSD: Invalid OPEN claim type %d\n",
4461da177e4SLinus Torvalds 				open->op_claim_type);
4471da177e4SLinus Torvalds 			status = nfserr_inval;
4481da177e4SLinus Torvalds 			goto out;
4491da177e4SLinus Torvalds 	}
4501da177e4SLinus Torvalds 	/*
4511da177e4SLinus Torvalds 	 * nfsd4_process_open2() does the actual opening of the file.  If
4521da177e4SLinus Torvalds 	 * successful, it (1) truncates the file if open->op_truncate was
4531da177e4SLinus Torvalds 	 * set, (2) sets open->op_stateid, (3) sets open->op_delegation.
4541da177e4SLinus Torvalds 	 */
455c0e6bee4SJ. Bruce Fields 	status = nfsd4_process_open2(rqstp, resfh, open);
456b3fbfe0eSJeff Layton 	WARN(status && open->op_created,
457b3fbfe0eSJeff Layton 	     "nfsd4_process_open2 failed to open newly-created file! status=%u\n",
458b3fbfe0eSJeff Layton 	     be32_to_cpu(status));
45903f318caSJ. Bruce Fields 	if (reclaim && !status)
46003f318caSJ. Bruce Fields 		nn->somebody_reclaimed = true;
4611da177e4SLinus Torvalds out:
462c0e6bee4SJ. Bruce Fields 	if (resfh && resfh != &cstate->current_fh) {
463c0e6bee4SJ. Bruce Fields 		fh_dup2(&cstate->current_fh, resfh);
464c0e6bee4SJ. Bruce Fields 		fh_put(resfh);
465c0e6bee4SJ. Bruce Fields 		kfree(resfh);
466c0e6bee4SJ. Bruce Fields 	}
46742297899SJeff Layton 	nfsd4_cleanup_open_state(cstate, open);
4689411b1d4SJ. Bruce Fields 	nfsd4_bump_seqid(cstate, status);
4691da177e4SLinus Torvalds 	return status;
4701da177e4SLinus Torvalds }
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds /*
4739d313b17SJ. Bruce Fields  * OPEN is the only seqid-mutating operation whose decoding can fail
4749d313b17SJ. Bruce Fields  * with a seqid-mutating error (specifically, decoding of user names in
4759d313b17SJ. Bruce Fields  * the attributes).  Therefore we have to do some processing to look up
4769d313b17SJ. Bruce Fields  * the stateowner so that we can bump the seqid.
4779d313b17SJ. Bruce Fields  */
4789d313b17SJ. Bruce Fields static __be32 nfsd4_open_omfg(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_op *op)
4799d313b17SJ. Bruce Fields {
480eb69853dSChristoph Hellwig 	struct nfsd4_open *open = &op->u.open;
4819d313b17SJ. Bruce Fields 
4829d313b17SJ. Bruce Fields 	if (!seqid_mutating_err(ntohl(op->status)))
4839d313b17SJ. Bruce Fields 		return op->status;
4849d313b17SJ. Bruce Fields 	if (nfsd4_has_session(cstate))
4859d313b17SJ. Bruce Fields 		return op->status;
4869d313b17SJ. Bruce Fields 	open->op_xdr_error = op->status;
487eb69853dSChristoph Hellwig 	return nfsd4_open(rqstp, cstate, &op->u);
4889d313b17SJ. Bruce Fields }
4899d313b17SJ. Bruce Fields 
4909d313b17SJ. Bruce Fields /*
4911da177e4SLinus Torvalds  * filehandle-manipulating ops.
4921da177e4SLinus Torvalds  */
4937191155bSJ.Bruce Fields static __be32
494b591480bSJ.Bruce Fields nfsd4_getfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
495eb69853dSChristoph Hellwig 	    union nfsd4_op_u *u)
4961da177e4SLinus Torvalds {
497eb69853dSChristoph Hellwig 	u->getfh = &cstate->current_fh;
4981da177e4SLinus Torvalds 	return nfs_ok;
4991da177e4SLinus Torvalds }
5001da177e4SLinus Torvalds 
5017191155bSJ.Bruce Fields static __be32
502ca364317SJ.Bruce Fields nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
503eb69853dSChristoph Hellwig 	    union nfsd4_op_u *u)
5041da177e4SLinus Torvalds {
505eb69853dSChristoph Hellwig 	struct nfsd4_putfh *putfh = &u->putfh;
506eb69853dSChristoph Hellwig 
507ca364317SJ.Bruce Fields 	fh_put(&cstate->current_fh);
508ca364317SJ.Bruce Fields 	cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
509ca364317SJ.Bruce Fields 	memcpy(&cstate->current_fh.fh_handle.fh_base, putfh->pf_fhval,
510ca364317SJ.Bruce Fields 	       putfh->pf_fhlen);
51168d93184SJ. Bruce Fields 	return fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_BYPASS_GSS);
5121da177e4SLinus Torvalds }
5131da177e4SLinus Torvalds 
5147191155bSJ.Bruce Fields static __be32
515b591480bSJ.Bruce Fields nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
516eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
5171da177e4SLinus Torvalds {
518b37ad28bSAl Viro 	__be32 status;
5191da177e4SLinus Torvalds 
520ca364317SJ.Bruce Fields 	fh_put(&cstate->current_fh);
521df547efbSJ. Bruce Fields 	status = exp_pseudoroot(rqstp, &cstate->current_fh);
5221da177e4SLinus Torvalds 	return status;
5231da177e4SLinus Torvalds }
5241da177e4SLinus Torvalds 
5257191155bSJ.Bruce Fields static __be32
526b591480bSJ.Bruce Fields nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
527eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
5281da177e4SLinus Torvalds {
529ca364317SJ.Bruce Fields 	if (!cstate->save_fh.fh_dentry)
5301da177e4SLinus Torvalds 		return nfserr_restorefh;
5311da177e4SLinus Torvalds 
532ca364317SJ.Bruce Fields 	fh_dup2(&cstate->current_fh, &cstate->save_fh);
53337c593c5STigran Mkrtchyan 	if (HAS_STATE_ID(cstate, SAVED_STATE_ID_FLAG)) {
53437c593c5STigran Mkrtchyan 		memcpy(&cstate->current_stateid, &cstate->save_stateid, sizeof(stateid_t));
53537c593c5STigran Mkrtchyan 		SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
53637c593c5STigran Mkrtchyan 	}
5371da177e4SLinus Torvalds 	return nfs_ok;
5381da177e4SLinus Torvalds }
5391da177e4SLinus Torvalds 
5407191155bSJ.Bruce Fields static __be32
541b591480bSJ.Bruce Fields nfsd4_savefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
542eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
5431da177e4SLinus Torvalds {
544ca364317SJ.Bruce Fields 	fh_dup2(&cstate->save_fh, &cstate->current_fh);
54537c593c5STigran Mkrtchyan 	if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG)) {
54637c593c5STigran Mkrtchyan 		memcpy(&cstate->save_stateid, &cstate->current_stateid, sizeof(stateid_t));
54737c593c5STigran Mkrtchyan 		SET_STATE_ID(cstate, SAVED_STATE_ID_FLAG);
54837c593c5STigran Mkrtchyan 	}
5491da177e4SLinus Torvalds 	return nfs_ok;
5501da177e4SLinus Torvalds }
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds /*
5531da177e4SLinus Torvalds  * misc nfsv4 ops
5541da177e4SLinus Torvalds  */
5557191155bSJ.Bruce Fields static __be32
556ca364317SJ.Bruce Fields nfsd4_access(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
557eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
5581da177e4SLinus Torvalds {
559eb69853dSChristoph Hellwig 	struct nfsd4_access *access = &u->access;
560eb69853dSChristoph Hellwig 
5611da177e4SLinus Torvalds 	if (access->ac_req_access & ~NFS3_ACCESS_FULL)
5621da177e4SLinus Torvalds 		return nfserr_inval;
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	access->ac_resp_access = access->ac_req_access;
565ca364317SJ.Bruce Fields 	return nfsd_access(rqstp, &cstate->current_fh, &access->ac_resp_access,
566ca364317SJ.Bruce Fields 			   &access->ac_supported);
5671da177e4SLinus Torvalds }
5681da177e4SLinus Torvalds 
569b9c0ef85SStanislav Kinsbursky static void gen_boot_verifier(nfs4_verifier *verifier, struct net *net)
570ab4684d1SChuck Lever {
57127c438f5STrond Myklebust 	__be32 *verf = (__be32 *)verifier->data;
572ab4684d1SChuck Lever 
57327c438f5STrond Myklebust 	BUILD_BUG_ON(2*sizeof(*verf) != sizeof(verifier->data));
57427c438f5STrond Myklebust 
57527c438f5STrond Myklebust 	nfsd_copy_boot_verifier(verf, net_generic(net, nfsd_net_id));
576ab4684d1SChuck Lever }
577ab4684d1SChuck Lever 
5787191155bSJ.Bruce Fields static __be32
579ca364317SJ.Bruce Fields nfsd4_commit(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
580eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
5811da177e4SLinus Torvalds {
582eb69853dSChristoph Hellwig 	struct nfsd4_commit *commit = &u->commit;
583eb69853dSChristoph Hellwig 
584b9c0ef85SStanislav Kinsbursky 	gen_boot_verifier(&commit->co_verf, SVC_NET(rqstp));
58575c096f7SJ. Bruce Fields 	return nfsd_commit(rqstp, &cstate->current_fh, commit->co_offset,
586ca364317SJ.Bruce Fields 			     commit->co_count);
5871da177e4SLinus Torvalds }
5881da177e4SLinus Torvalds 
589b37ad28bSAl Viro static __be32
590ca364317SJ.Bruce Fields nfsd4_create(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
591eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
5921da177e4SLinus Torvalds {
593eb69853dSChristoph Hellwig 	struct nfsd4_create *create = &u->create;
5941da177e4SLinus Torvalds 	struct svc_fh resfh;
595b37ad28bSAl Viro 	__be32 status;
5961da177e4SLinus Torvalds 	dev_t rdev;
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	fh_init(&resfh, NFS4_FHSIZE);
5991da177e4SLinus Torvalds 
600fa08139dSJ. Bruce Fields 	status = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_NOP);
6011da177e4SLinus Torvalds 	if (status)
6021da177e4SLinus Torvalds 		return status;
6031da177e4SLinus Torvalds 
6043c8e0316SYu Zhiguo 	status = check_attr_support(rqstp, cstate, create->cr_bmval,
6053c8e0316SYu Zhiguo 				    nfsd_attrmask);
6063c8e0316SYu Zhiguo 	if (status)
6073c8e0316SYu Zhiguo 		return status;
6083c8e0316SYu Zhiguo 
609880a3a53SJ. Bruce Fields 	current->fs->umask = create->cr_umask;
6101da177e4SLinus Torvalds 	switch (create->cr_type) {
6111da177e4SLinus Torvalds 	case NF4LNK:
612ca364317SJ.Bruce Fields 		status = nfsd_symlink(rqstp, &cstate->current_fh,
613ca364317SJ.Bruce Fields 				      create->cr_name, create->cr_namelen,
6141e444f5bSKinglong Mee 				      create->cr_data, &resfh);
6151da177e4SLinus Torvalds 		break;
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	case NF4BLK:
618880a3a53SJ. Bruce Fields 		status = nfserr_inval;
6191da177e4SLinus Torvalds 		rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
6201da177e4SLinus Torvalds 		if (MAJOR(rdev) != create->cr_specdata1 ||
6211da177e4SLinus Torvalds 		    MINOR(rdev) != create->cr_specdata2)
622880a3a53SJ. Bruce Fields 			goto out_umask;
623ca364317SJ.Bruce Fields 		status = nfsd_create(rqstp, &cstate->current_fh,
624ca364317SJ.Bruce Fields 				     create->cr_name, create->cr_namelen,
625ca364317SJ.Bruce Fields 				     &create->cr_iattr, S_IFBLK, rdev, &resfh);
6261da177e4SLinus Torvalds 		break;
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	case NF4CHR:
629880a3a53SJ. Bruce Fields 		status = nfserr_inval;
6301da177e4SLinus Torvalds 		rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
6311da177e4SLinus Torvalds 		if (MAJOR(rdev) != create->cr_specdata1 ||
6321da177e4SLinus Torvalds 		    MINOR(rdev) != create->cr_specdata2)
633880a3a53SJ. Bruce Fields 			goto out_umask;
634ca364317SJ.Bruce Fields 		status = nfsd_create(rqstp, &cstate->current_fh,
635ca364317SJ.Bruce Fields 				     create->cr_name, create->cr_namelen,
636ca364317SJ.Bruce Fields 				     &create->cr_iattr,S_IFCHR, rdev, &resfh);
6371da177e4SLinus Torvalds 		break;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	case NF4SOCK:
640ca364317SJ.Bruce Fields 		status = nfsd_create(rqstp, &cstate->current_fh,
641ca364317SJ.Bruce Fields 				     create->cr_name, create->cr_namelen,
642ca364317SJ.Bruce Fields 				     &create->cr_iattr, S_IFSOCK, 0, &resfh);
6431da177e4SLinus Torvalds 		break;
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	case NF4FIFO:
646ca364317SJ.Bruce Fields 		status = nfsd_create(rqstp, &cstate->current_fh,
647ca364317SJ.Bruce Fields 				     create->cr_name, create->cr_namelen,
648ca364317SJ.Bruce Fields 				     &create->cr_iattr, S_IFIFO, 0, &resfh);
6491da177e4SLinus Torvalds 		break;
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	case NF4DIR:
6521da177e4SLinus Torvalds 		create->cr_iattr.ia_valid &= ~ATTR_SIZE;
653ca364317SJ.Bruce Fields 		status = nfsd_create(rqstp, &cstate->current_fh,
654ca364317SJ.Bruce Fields 				     create->cr_name, create->cr_namelen,
655ca364317SJ.Bruce Fields 				     &create->cr_iattr, S_IFDIR, 0, &resfh);
6561da177e4SLinus Torvalds 		break;
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	default:
6591da177e4SLinus Torvalds 		status = nfserr_badtype;
6601da177e4SLinus Torvalds 	}
6611da177e4SLinus Torvalds 
6629208faf2SYu Zhiguo 	if (status)
6639208faf2SYu Zhiguo 		goto out;
6649208faf2SYu Zhiguo 
66518032ca0SDavid Quigley 	if (create->cr_label.len)
66618032ca0SDavid Quigley 		nfsd4_security_inode_setsecctx(&resfh, &create->cr_label, create->cr_bmval);
66718032ca0SDavid Quigley 
6689208faf2SYu Zhiguo 	if (create->cr_acl != NULL)
6699208faf2SYu Zhiguo 		do_set_nfs4_acl(rqstp, &resfh, create->cr_acl,
6709208faf2SYu Zhiguo 				create->cr_bmval);
6719208faf2SYu Zhiguo 
672ca364317SJ.Bruce Fields 	fh_unlock(&cstate->current_fh);
673ca364317SJ.Bruce Fields 	set_change_info(&create->cr_cinfo, &cstate->current_fh);
674ca364317SJ.Bruce Fields 	fh_dup2(&cstate->current_fh, &resfh);
6759208faf2SYu Zhiguo out:
6761da177e4SLinus Torvalds 	fh_put(&resfh);
677880a3a53SJ. Bruce Fields out_umask:
678880a3a53SJ. Bruce Fields 	current->fs->umask = 0;
6791da177e4SLinus Torvalds 	return status;
6801da177e4SLinus Torvalds }
6811da177e4SLinus Torvalds 
6827191155bSJ.Bruce Fields static __be32
683ca364317SJ.Bruce Fields nfsd4_getattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
684eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
6851da177e4SLinus Torvalds {
686eb69853dSChristoph Hellwig 	struct nfsd4_getattr *getattr = &u->getattr;
687b37ad28bSAl Viro 	__be32 status;
6881da177e4SLinus Torvalds 
6898837abcaSMiklos Szeredi 	status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
6901da177e4SLinus Torvalds 	if (status)
6911da177e4SLinus Torvalds 		return status;
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 	if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
6941da177e4SLinus Torvalds 		return nfserr_inval;
6951da177e4SLinus Torvalds 
696916d2d84SJ. Bruce Fields 	getattr->ga_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
697916d2d84SJ. Bruce Fields 	getattr->ga_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
698916d2d84SJ. Bruce Fields 	getattr->ga_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
6991da177e4SLinus Torvalds 
700ca364317SJ.Bruce Fields 	getattr->ga_fhp = &cstate->current_fh;
7011da177e4SLinus Torvalds 	return nfs_ok;
7021da177e4SLinus Torvalds }
7031da177e4SLinus Torvalds 
7047191155bSJ.Bruce Fields static __be32
705ca364317SJ.Bruce Fields nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
706eb69853dSChristoph Hellwig 	   union nfsd4_op_u *u)
7071da177e4SLinus Torvalds {
708eb69853dSChristoph Hellwig 	struct nfsd4_link *link = &u->link;
70995424460SJ. Bruce Fields 	__be32 status;
7101da177e4SLinus Torvalds 
711ca364317SJ.Bruce Fields 	status = nfsd_link(rqstp, &cstate->current_fh,
712ca364317SJ.Bruce Fields 			   link->li_name, link->li_namelen, &cstate->save_fh);
7131da177e4SLinus Torvalds 	if (!status)
714ca364317SJ.Bruce Fields 		set_change_info(&link->li_cinfo, &cstate->current_fh);
7151da177e4SLinus Torvalds 	return status;
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
7180ff7ab46SJ. Bruce Fields static __be32 nfsd4_do_lookupp(struct svc_rqst *rqstp, struct svc_fh *fh)
7191da177e4SLinus Torvalds {
7201da177e4SLinus Torvalds 	struct svc_fh tmp_fh;
721b37ad28bSAl Viro 	__be32 ret;
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	fh_init(&tmp_fh, NFS4_FHSIZE);
724df547efbSJ. Bruce Fields 	ret = exp_pseudoroot(rqstp, &tmp_fh);
725df547efbSJ. Bruce Fields 	if (ret)
7261da177e4SLinus Torvalds 		return ret;
7270ff7ab46SJ. Bruce Fields 	if (tmp_fh.fh_dentry == fh->fh_dentry) {
7281da177e4SLinus Torvalds 		fh_put(&tmp_fh);
7291da177e4SLinus Torvalds 		return nfserr_noent;
7301da177e4SLinus Torvalds 	}
7311da177e4SLinus Torvalds 	fh_put(&tmp_fh);
7320ff7ab46SJ. Bruce Fields 	return nfsd_lookup(rqstp, fh, "..", 2, fh);
7330ff7ab46SJ. Bruce Fields }
7340ff7ab46SJ. Bruce Fields 
7350ff7ab46SJ. Bruce Fields static __be32
7360ff7ab46SJ. Bruce Fields nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
737eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
7380ff7ab46SJ. Bruce Fields {
7390ff7ab46SJ. Bruce Fields 	return nfsd4_do_lookupp(rqstp, &cstate->current_fh);
7401da177e4SLinus Torvalds }
7411da177e4SLinus Torvalds 
7427191155bSJ.Bruce Fields static __be32
743ca364317SJ.Bruce Fields nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
744eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
7451da177e4SLinus Torvalds {
746ca364317SJ.Bruce Fields 	return nfsd_lookup(rqstp, &cstate->current_fh,
747eb69853dSChristoph Hellwig 			   u->lookup.lo_name, u->lookup.lo_len,
748ca364317SJ.Bruce Fields 			   &cstate->current_fh);
7491da177e4SLinus Torvalds }
7501da177e4SLinus Torvalds 
7517191155bSJ.Bruce Fields static __be32
752ca364317SJ.Bruce Fields nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
753eb69853dSChristoph Hellwig 	   union nfsd4_op_u *u)
7541da177e4SLinus Torvalds {
755eb69853dSChristoph Hellwig 	struct nfsd4_read *read = &u->read;
756b37ad28bSAl Viro 	__be32 status;
7571da177e4SLinus Torvalds 
7585c4583b2SJeff Layton 	read->rd_nf = NULL;
7591da177e4SLinus Torvalds 	if (read->rd_offset >= OFFSET_MAX)
7601da177e4SLinus Torvalds 		return nfserr_inval;
7611da177e4SLinus Torvalds 
76287c5942eSChuck Lever 	trace_nfsd_read_start(rqstp, &cstate->current_fh,
76387c5942eSChuck Lever 			      read->rd_offset, read->rd_length);
76487c5942eSChuck Lever 
7659b3234b9SJ. Bruce Fields 	/*
7669b3234b9SJ. Bruce Fields 	 * If we do a zero copy read, then a client will see read data
7679b3234b9SJ. Bruce Fields 	 * that reflects the state of the file *after* performing the
7689b3234b9SJ. Bruce Fields 	 * following compound.
7699b3234b9SJ. Bruce Fields 	 *
7709b3234b9SJ. Bruce Fields 	 * To ensure proper ordering, we therefore turn off zero copy if
7719b3234b9SJ. Bruce Fields 	 * the client wants us to do more in this compound:
7729b3234b9SJ. Bruce Fields 	 */
7739b3234b9SJ. Bruce Fields 	if (!nfsd4_last_compound_op(rqstp))
774779fb0f3SJeff Layton 		clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
7759b3234b9SJ. Bruce Fields 
7761da177e4SLinus Torvalds 	/* check stateid */
777aa0d6aedSAnna Schumaker 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
778aa0d6aedSAnna Schumaker 					&read->rd_stateid, RD_STATE,
7795c4583b2SJeff Layton 					&read->rd_nf);
780af90f707SChristoph Hellwig 	if (status) {
7811da177e4SLinus Torvalds 		dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
7821da177e4SLinus Torvalds 		goto out;
7831da177e4SLinus Torvalds 	}
7841da177e4SLinus Torvalds 	status = nfs_ok;
7851da177e4SLinus Torvalds out:
7861da177e4SLinus Torvalds 	read->rd_rqstp = rqstp;
787ca364317SJ.Bruce Fields 	read->rd_fhp = &cstate->current_fh;
7881da177e4SLinus Torvalds 	return status;
7891da177e4SLinus Torvalds }
7901da177e4SLinus Torvalds 
79134b1744cSJ. Bruce Fields 
79234b1744cSJ. Bruce Fields static void
79334b1744cSJ. Bruce Fields nfsd4_read_release(union nfsd4_op_u *u)
79434b1744cSJ. Bruce Fields {
7955c4583b2SJeff Layton 	if (u->read.rd_nf)
7965c4583b2SJeff Layton 		nfsd_file_put(u->read.rd_nf);
79787c5942eSChuck Lever 	trace_nfsd_read_done(u->read.rd_rqstp, u->read.rd_fhp,
79887c5942eSChuck Lever 			     u->read.rd_offset, u->read.rd_length);
79934b1744cSJ. Bruce Fields }
80034b1744cSJ. Bruce Fields 
8017191155bSJ.Bruce Fields static __be32
802ca364317SJ.Bruce Fields nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
803eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
8041da177e4SLinus Torvalds {
805eb69853dSChristoph Hellwig 	struct nfsd4_readdir *readdir = &u->readdir;
8061da177e4SLinus Torvalds 	u64 cookie = readdir->rd_cookie;
8071da177e4SLinus Torvalds 	static const nfs4_verifier zeroverf;
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	/* no need to check permission - this will be done in nfsd_readdir() */
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 	if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
8121da177e4SLinus Torvalds 		return nfserr_inval;
8131da177e4SLinus Torvalds 
814916d2d84SJ. Bruce Fields 	readdir->rd_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
815916d2d84SJ. Bruce Fields 	readdir->rd_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
816916d2d84SJ. Bruce Fields 	readdir->rd_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
8171da177e4SLinus Torvalds 
818832023bfSBernd Schubert 	if ((cookie == 1) || (cookie == 2) ||
8191da177e4SLinus Torvalds 	    (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
8201da177e4SLinus Torvalds 		return nfserr_bad_cookie;
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds 	readdir->rd_rqstp = rqstp;
823ca364317SJ.Bruce Fields 	readdir->rd_fhp = &cstate->current_fh;
8241da177e4SLinus Torvalds 	return nfs_ok;
8251da177e4SLinus Torvalds }
8261da177e4SLinus Torvalds 
8277191155bSJ.Bruce Fields static __be32
828ca364317SJ.Bruce Fields nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
829eb69853dSChristoph Hellwig 	       union nfsd4_op_u *u)
8301da177e4SLinus Torvalds {
831eb69853dSChristoph Hellwig 	u->readlink.rl_rqstp = rqstp;
832eb69853dSChristoph Hellwig 	u->readlink.rl_fhp = &cstate->current_fh;
8331da177e4SLinus Torvalds 	return nfs_ok;
8341da177e4SLinus Torvalds }
8351da177e4SLinus Torvalds 
8367191155bSJ.Bruce Fields static __be32
837ca364317SJ.Bruce Fields nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
838eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
8391da177e4SLinus Torvalds {
840eb69853dSChristoph Hellwig 	struct nfsd4_remove *remove = &u->remove;
841b37ad28bSAl Viro 	__be32 status;
8421da177e4SLinus Torvalds 
843c87fb4a3SJ. Bruce Fields 	if (opens_in_grace(SVC_NET(rqstp)))
844c815afc7SNeilBrown 		return nfserr_grace;
845ca364317SJ.Bruce Fields 	status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
846ca364317SJ.Bruce Fields 			     remove->rm_name, remove->rm_namelen);
8471da177e4SLinus Torvalds 	if (!status) {
848ca364317SJ.Bruce Fields 		fh_unlock(&cstate->current_fh);
849ca364317SJ.Bruce Fields 		set_change_info(&remove->rm_cinfo, &cstate->current_fh);
8501da177e4SLinus Torvalds 	}
8511da177e4SLinus Torvalds 	return status;
8521da177e4SLinus Torvalds }
8531da177e4SLinus Torvalds 
8547191155bSJ.Bruce Fields static __be32
855ca364317SJ.Bruce Fields nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
856eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
8571da177e4SLinus Torvalds {
858eb69853dSChristoph Hellwig 	struct nfsd4_rename *rename = &u->rename;
85995424460SJ. Bruce Fields 	__be32 status;
8601da177e4SLinus Torvalds 
861f8f71d00SJ. Bruce Fields 	if (opens_in_grace(SVC_NET(rqstp)))
862c815afc7SNeilBrown 		return nfserr_grace;
863ca364317SJ.Bruce Fields 	status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname,
864ca364317SJ.Bruce Fields 			     rename->rn_snamelen, &cstate->current_fh,
8651da177e4SLinus Torvalds 			     rename->rn_tname, rename->rn_tnamelen);
8662a6cf944SJ. Bruce Fields 	if (status)
8672a6cf944SJ. Bruce Fields 		return status;
868ca364317SJ.Bruce Fields 	set_change_info(&rename->rn_sinfo, &cstate->current_fh);
869ca364317SJ.Bruce Fields 	set_change_info(&rename->rn_tinfo, &cstate->save_fh);
8702a6cf944SJ. Bruce Fields 	return nfs_ok;
8711da177e4SLinus Torvalds }
8721da177e4SLinus Torvalds 
8737191155bSJ.Bruce Fields static __be32
874dcb488a3SAndy Adamson nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
875eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
876dcb488a3SAndy Adamson {
877eb69853dSChristoph Hellwig 	struct nfsd4_secinfo *secinfo = &u->secinfo;
878dcb488a3SAndy Adamson 	struct svc_export *exp;
879dcb488a3SAndy Adamson 	struct dentry *dentry;
880dcb488a3SAndy Adamson 	__be32 err;
881dcb488a3SAndy Adamson 
88229a78a3eSJ. Bruce Fields 	err = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_EXEC);
88329a78a3eSJ. Bruce Fields 	if (err)
88429a78a3eSJ. Bruce Fields 		return err;
885dcb488a3SAndy Adamson 	err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
886dcb488a3SAndy Adamson 				    secinfo->si_name, secinfo->si_namelen,
887dcb488a3SAndy Adamson 				    &exp, &dentry);
888dcb488a3SAndy Adamson 	if (err)
889dcb488a3SAndy Adamson 		return err;
8902f6fc056SJ. Bruce Fields 	fh_unlock(&cstate->current_fh);
8912b0143b5SDavid Howells 	if (d_really_is_negative(dentry)) {
892dcb488a3SAndy Adamson 		exp_put(exp);
893dcb488a3SAndy Adamson 		err = nfserr_noent;
894dcb488a3SAndy Adamson 	} else
895dcb488a3SAndy Adamson 		secinfo->si_exp = exp;
896dcb488a3SAndy Adamson 	dput(dentry);
89756560b9aSJ. Bruce Fields 	if (cstate->minorversion)
89856560b9aSJ. Bruce Fields 		/* See rfc 5661 section 2.6.3.1.1.8 */
89956560b9aSJ. Bruce Fields 		fh_put(&cstate->current_fh);
900dcb488a3SAndy Adamson 	return err;
901dcb488a3SAndy Adamson }
902dcb488a3SAndy Adamson 
903dcb488a3SAndy Adamson static __be32
90404f4ad16SJ. Bruce Fields nfsd4_secinfo_no_name(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
905eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
90604f4ad16SJ. Bruce Fields {
90704f4ad16SJ. Bruce Fields 	__be32 err;
90804f4ad16SJ. Bruce Fields 
909eb69853dSChristoph Hellwig 	switch (u->secinfo_no_name.sin_style) {
91004f4ad16SJ. Bruce Fields 	case NFS4_SECINFO_STYLE4_CURRENT_FH:
91104f4ad16SJ. Bruce Fields 		break;
91204f4ad16SJ. Bruce Fields 	case NFS4_SECINFO_STYLE4_PARENT:
91304f4ad16SJ. Bruce Fields 		err = nfsd4_do_lookupp(rqstp, &cstate->current_fh);
91404f4ad16SJ. Bruce Fields 		if (err)
91504f4ad16SJ. Bruce Fields 			return err;
91604f4ad16SJ. Bruce Fields 		break;
91704f4ad16SJ. Bruce Fields 	default:
91804f4ad16SJ. Bruce Fields 		return nfserr_inval;
91904f4ad16SJ. Bruce Fields 	}
920bf18f163SKinglong Mee 
921eb69853dSChristoph Hellwig 	u->secinfo_no_name.sin_exp = exp_get(cstate->current_fh.fh_export);
92204f4ad16SJ. Bruce Fields 	fh_put(&cstate->current_fh);
92304f4ad16SJ. Bruce Fields 	return nfs_ok;
92404f4ad16SJ. Bruce Fields }
92504f4ad16SJ. Bruce Fields 
92634b1744cSJ. Bruce Fields static void
92734b1744cSJ. Bruce Fields nfsd4_secinfo_release(union nfsd4_op_u *u)
92834b1744cSJ. Bruce Fields {
92934b1744cSJ. Bruce Fields 	if (u->secinfo.si_exp)
93034b1744cSJ. Bruce Fields 		exp_put(u->secinfo.si_exp);
93134b1744cSJ. Bruce Fields }
93234b1744cSJ. Bruce Fields 
933ec572b9eSEryu Guan static void
934ec572b9eSEryu Guan nfsd4_secinfo_no_name_release(union nfsd4_op_u *u)
935ec572b9eSEryu Guan {
936ec572b9eSEryu Guan 	if (u->secinfo_no_name.sin_exp)
937ec572b9eSEryu Guan 		exp_put(u->secinfo_no_name.sin_exp);
938ec572b9eSEryu Guan }
939ec572b9eSEryu Guan 
94004f4ad16SJ. Bruce Fields static __be32
941ca364317SJ.Bruce Fields nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
942eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
9431da177e4SLinus Torvalds {
944eb69853dSChristoph Hellwig 	struct nfsd4_setattr *setattr = &u->setattr;
945b37ad28bSAl Viro 	__be32 status = nfs_ok;
94696f6f985SAl Viro 	int err;
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds 	if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
949af90f707SChristoph Hellwig 		status = nfs4_preprocess_stateid_op(rqstp, cstate,
950aa0d6aedSAnna Schumaker 				&cstate->current_fh, &setattr->sa_stateid,
9515c4583b2SJeff Layton 				WR_STATE, NULL);
952375c5547SJ. Bruce Fields 		if (status) {
9533e3b4800SGreg Banks 			dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
954375c5547SJ. Bruce Fields 			return status;
955375c5547SJ. Bruce Fields 		}
9561da177e4SLinus Torvalds 	}
95796f6f985SAl Viro 	err = fh_want_write(&cstate->current_fh);
95896f6f985SAl Viro 	if (err)
95996f6f985SAl Viro 		return nfserrno(err);
9601da177e4SLinus Torvalds 	status = nfs_ok;
9613c8e0316SYu Zhiguo 
9623c8e0316SYu Zhiguo 	status = check_attr_support(rqstp, cstate, setattr->sa_bmval,
9633c8e0316SYu Zhiguo 				    nfsd_attrmask);
9643c8e0316SYu Zhiguo 	if (status)
9653c8e0316SYu Zhiguo 		goto out;
9663c8e0316SYu Zhiguo 
9671da177e4SLinus Torvalds 	if (setattr->sa_acl != NULL)
968ca364317SJ.Bruce Fields 		status = nfsd4_set_nfs4_acl(rqstp, &cstate->current_fh,
969ca364317SJ.Bruce Fields 					    setattr->sa_acl);
9701da177e4SLinus Torvalds 	if (status)
97118f335afSDave Hansen 		goto out;
97218032ca0SDavid Quigley 	if (setattr->sa_label.len)
97318032ca0SDavid Quigley 		status = nfsd4_set_nfs4_label(rqstp, &cstate->current_fh,
97418032ca0SDavid Quigley 				&setattr->sa_label);
97518032ca0SDavid Quigley 	if (status)
97618032ca0SDavid Quigley 		goto out;
977ca364317SJ.Bruce Fields 	status = nfsd_setattr(rqstp, &cstate->current_fh, &setattr->sa_iattr,
9781da177e4SLinus Torvalds 				0, (time_t)0);
97918f335afSDave Hansen out:
980bad0dcffSAl Viro 	fh_drop_write(&cstate->current_fh);
9811da177e4SLinus Torvalds 	return status;
9821da177e4SLinus Torvalds }
9831da177e4SLinus Torvalds 
9847191155bSJ.Bruce Fields static __be32
985ca364317SJ.Bruce Fields nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
986eb69853dSChristoph Hellwig 	    union nfsd4_op_u *u)
9871da177e4SLinus Torvalds {
988eb69853dSChristoph Hellwig 	struct nfsd4_write *write = &u->write;
9891da177e4SLinus Torvalds 	stateid_t *stateid = &write->wr_stateid;
9905c4583b2SJeff Layton 	struct nfsd_file *nf = NULL;
991b37ad28bSAl Viro 	__be32 status = nfs_ok;
99231dec253SDavid Shaw 	unsigned long cnt;
993ffe1137bSJ. Bruce Fields 	int nvecs;
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	if (write->wr_offset >= OFFSET_MAX)
9961da177e4SLinus Torvalds 		return nfserr_inval;
9971da177e4SLinus Torvalds 
998d890be15SChuck Lever 	cnt = write->wr_buflen;
999d890be15SChuck Lever 	trace_nfsd_write_start(rqstp, &cstate->current_fh,
1000d890be15SChuck Lever 			       write->wr_offset, cnt);
1001aa0d6aedSAnna Schumaker 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
10025c4583b2SJeff Layton 						stateid, WR_STATE, &nf);
1003375c5547SJ. Bruce Fields 	if (status) {
1004375c5547SJ. Bruce Fields 		dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
1005375c5547SJ. Bruce Fields 		return status;
1006375c5547SJ. Bruce Fields 	}
1007375c5547SJ. Bruce Fields 
10081da177e4SLinus Torvalds 	write->wr_how_written = write->wr_stable_how;
1009b9c0ef85SStanislav Kinsbursky 	gen_boot_verifier(&write->wr_verifier, SVC_NET(rqstp));
10101da177e4SLinus Torvalds 
10113fd9557aSChuck Lever 	nvecs = svc_fill_write_vector(rqstp, write->wr_pagelist,
10123fd9557aSChuck Lever 				      &write->wr_head, write->wr_buflen);
1013ffe1137bSJ. Bruce Fields 	WARN_ON_ONCE(nvecs > ARRAY_SIZE(rqstp->rq_vec));
1014ffe1137bSJ. Bruce Fields 
10155c4583b2SJeff Layton 	status = nfsd_vfs_write(rqstp, &cstate->current_fh, nf->nf_file,
1016af90f707SChristoph Hellwig 				write->wr_offset, rqstp->rq_vec, nvecs, &cnt,
101754bbb7d2SKinglong Mee 				write->wr_how_written);
10185c4583b2SJeff Layton 	nfsd_file_put(nf);
10191da177e4SLinus Torvalds 
102031dec253SDavid Shaw 	write->wr_bytes_written = cnt;
1021d890be15SChuck Lever 	trace_nfsd_write_done(rqstp, &cstate->current_fh,
1022d890be15SChuck Lever 			      write->wr_offset, cnt);
10231da177e4SLinus Torvalds 	return status;
10241da177e4SLinus Torvalds }
10251da177e4SLinus Torvalds 
102624bab491SAnna Schumaker static __be32
102729ae7f9dSAnna Schumaker nfsd4_verify_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
10285c4583b2SJeff Layton 		  stateid_t *src_stateid, struct nfsd_file **src,
10295c4583b2SJeff Layton 		  stateid_t *dst_stateid, struct nfsd_file **dst)
1030ffa0160aSChristoph Hellwig {
1031ffa0160aSChristoph Hellwig 	__be32 status;
1032ffa0160aSChristoph Hellwig 
103301310bb7SScott Mayhew 	if (!cstate->save_fh.fh_dentry)
103401310bb7SScott Mayhew 		return nfserr_nofilehandle;
103501310bb7SScott Mayhew 
1036ffa0160aSChristoph Hellwig 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->save_fh,
10375c4583b2SJeff Layton 					    src_stateid, RD_STATE, src);
1038ffa0160aSChristoph Hellwig 	if (status) {
1039ffa0160aSChristoph Hellwig 		dprintk("NFSD: %s: couldn't process src stateid!\n", __func__);
1040ffa0160aSChristoph Hellwig 		goto out;
1041ffa0160aSChristoph Hellwig 	}
1042ffa0160aSChristoph Hellwig 
1043ffa0160aSChristoph Hellwig 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
10445c4583b2SJeff Layton 					    dst_stateid, WR_STATE, dst);
1045ffa0160aSChristoph Hellwig 	if (status) {
1046ffa0160aSChristoph Hellwig 		dprintk("NFSD: %s: couldn't process dst stateid!\n", __func__);
1047ffa0160aSChristoph Hellwig 		goto out_put_src;
1048ffa0160aSChristoph Hellwig 	}
1049ffa0160aSChristoph Hellwig 
1050ffa0160aSChristoph Hellwig 	/* fix up for NFS-specific error code */
10515c4583b2SJeff Layton 	if (!S_ISREG(file_inode((*src)->nf_file)->i_mode) ||
10525c4583b2SJeff Layton 	    !S_ISREG(file_inode((*dst)->nf_file)->i_mode)) {
1053ffa0160aSChristoph Hellwig 		status = nfserr_wrong_type;
1054ffa0160aSChristoph Hellwig 		goto out_put_dst;
1055ffa0160aSChristoph Hellwig 	}
1056ffa0160aSChristoph Hellwig 
105729ae7f9dSAnna Schumaker out:
105829ae7f9dSAnna Schumaker 	return status;
105929ae7f9dSAnna Schumaker out_put_dst:
10605c4583b2SJeff Layton 	nfsd_file_put(*dst);
106129ae7f9dSAnna Schumaker out_put_src:
10625c4583b2SJeff Layton 	nfsd_file_put(*src);
106329ae7f9dSAnna Schumaker 	goto out;
106429ae7f9dSAnna Schumaker }
106529ae7f9dSAnna Schumaker 
106629ae7f9dSAnna Schumaker static __be32
106729ae7f9dSAnna Schumaker nfsd4_clone(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1068eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
106929ae7f9dSAnna Schumaker {
1070eb69853dSChristoph Hellwig 	struct nfsd4_clone *clone = &u->clone;
10715c4583b2SJeff Layton 	struct nfsd_file *src, *dst;
107229ae7f9dSAnna Schumaker 	__be32 status;
107329ae7f9dSAnna Schumaker 
107429ae7f9dSAnna Schumaker 	status = nfsd4_verify_copy(rqstp, cstate, &clone->cl_src_stateid, &src,
107529ae7f9dSAnna Schumaker 				   &clone->cl_dst_stateid, &dst);
107629ae7f9dSAnna Schumaker 	if (status)
107729ae7f9dSAnna Schumaker 		goto out;
107829ae7f9dSAnna Schumaker 
10795c4583b2SJeff Layton 	status = nfsd4_clone_file_range(src->nf_file, clone->cl_src_pos,
1080a25e3726STrond Myklebust 			dst->nf_file, clone->cl_dst_pos, clone->cl_count,
1081a25e3726STrond Myklebust 			EX_ISSYNC(cstate->current_fh.fh_export));
1082ffa0160aSChristoph Hellwig 
10835c4583b2SJeff Layton 	nfsd_file_put(dst);
10845c4583b2SJeff Layton 	nfsd_file_put(src);
1085ffa0160aSChristoph Hellwig out:
1086ffa0160aSChristoph Hellwig 	return status;
1087ffa0160aSChristoph Hellwig }
1088ffa0160aSChristoph Hellwig 
1089e0639dc5SOlga Kornievskaia void nfs4_put_copy(struct nfsd4_copy *copy)
1090e0639dc5SOlga Kornievskaia {
1091e0639dc5SOlga Kornievskaia 	if (!refcount_dec_and_test(&copy->refcount))
1092e0639dc5SOlga Kornievskaia 		return;
1093e0639dc5SOlga Kornievskaia 	kfree(copy);
1094e0639dc5SOlga Kornievskaia }
1095e0639dc5SOlga Kornievskaia 
1096e0639dc5SOlga Kornievskaia static bool
1097e0639dc5SOlga Kornievskaia check_and_set_stop_copy(struct nfsd4_copy *copy)
1098e0639dc5SOlga Kornievskaia {
1099e0639dc5SOlga Kornievskaia 	bool value;
1100e0639dc5SOlga Kornievskaia 
1101e0639dc5SOlga Kornievskaia 	spin_lock(&copy->cp_clp->async_lock);
1102e0639dc5SOlga Kornievskaia 	value = copy->stopped;
1103e0639dc5SOlga Kornievskaia 	if (!copy->stopped)
1104e0639dc5SOlga Kornievskaia 		copy->stopped = true;
1105e0639dc5SOlga Kornievskaia 	spin_unlock(&copy->cp_clp->async_lock);
1106e0639dc5SOlga Kornievskaia 	return value;
1107e0639dc5SOlga Kornievskaia }
1108e0639dc5SOlga Kornievskaia 
1109e0639dc5SOlga Kornievskaia static void nfsd4_stop_copy(struct nfsd4_copy *copy)
1110e0639dc5SOlga Kornievskaia {
1111e0639dc5SOlga Kornievskaia 	/* only 1 thread should stop the copy */
1112e0639dc5SOlga Kornievskaia 	if (!check_and_set_stop_copy(copy))
1113e0639dc5SOlga Kornievskaia 		kthread_stop(copy->copy_task);
1114e0639dc5SOlga Kornievskaia 	nfs4_put_copy(copy);
1115e0639dc5SOlga Kornievskaia }
1116e0639dc5SOlga Kornievskaia 
1117e0639dc5SOlga Kornievskaia static struct nfsd4_copy *nfsd4_get_copy(struct nfs4_client *clp)
1118e0639dc5SOlga Kornievskaia {
1119e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy = NULL;
1120e0639dc5SOlga Kornievskaia 
1121e0639dc5SOlga Kornievskaia 	spin_lock(&clp->async_lock);
1122e0639dc5SOlga Kornievskaia 	if (!list_empty(&clp->async_copies)) {
1123e0639dc5SOlga Kornievskaia 		copy = list_first_entry(&clp->async_copies, struct nfsd4_copy,
1124e0639dc5SOlga Kornievskaia 					copies);
1125e0639dc5SOlga Kornievskaia 		refcount_inc(&copy->refcount);
1126e0639dc5SOlga Kornievskaia 	}
1127e0639dc5SOlga Kornievskaia 	spin_unlock(&clp->async_lock);
1128e0639dc5SOlga Kornievskaia 	return copy;
1129e0639dc5SOlga Kornievskaia }
1130e0639dc5SOlga Kornievskaia 
1131e0639dc5SOlga Kornievskaia void nfsd4_shutdown_copy(struct nfs4_client *clp)
1132e0639dc5SOlga Kornievskaia {
1133e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy;
1134e0639dc5SOlga Kornievskaia 
1135e0639dc5SOlga Kornievskaia 	while ((copy = nfsd4_get_copy(clp)) != NULL)
1136e0639dc5SOlga Kornievskaia 		nfsd4_stop_copy(copy);
1137e0639dc5SOlga Kornievskaia }
1138e0639dc5SOlga Kornievskaia 
1139e0639dc5SOlga Kornievskaia static void nfsd4_cb_offload_release(struct nfsd4_callback *cb)
1140e0639dc5SOlga Kornievskaia {
1141e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy = container_of(cb, struct nfsd4_copy, cp_cb);
1142e0639dc5SOlga Kornievskaia 
1143e0639dc5SOlga Kornievskaia 	nfs4_put_copy(copy);
1144e0639dc5SOlga Kornievskaia }
1145e0639dc5SOlga Kornievskaia 
1146e0639dc5SOlga Kornievskaia static int nfsd4_cb_offload_done(struct nfsd4_callback *cb,
1147e0639dc5SOlga Kornievskaia 				 struct rpc_task *task)
1148e0639dc5SOlga Kornievskaia {
1149e0639dc5SOlga Kornievskaia 	return 1;
1150e0639dc5SOlga Kornievskaia }
1151e0639dc5SOlga Kornievskaia 
1152e0639dc5SOlga Kornievskaia static const struct nfsd4_callback_ops nfsd4_cb_offload_ops = {
1153e0639dc5SOlga Kornievskaia 	.release = nfsd4_cb_offload_release,
1154e0639dc5SOlga Kornievskaia 	.done = nfsd4_cb_offload_done
1155e0639dc5SOlga Kornievskaia };
1156e0639dc5SOlga Kornievskaia 
1157e0639dc5SOlga Kornievskaia static void nfsd4_init_copy_res(struct nfsd4_copy *copy, bool sync)
1158e0639dc5SOlga Kornievskaia {
1159e0639dc5SOlga Kornievskaia 	copy->cp_res.wr_stable_how = NFS_UNSTABLE;
1160e0639dc5SOlga Kornievskaia 	copy->cp_synchronous = sync;
1161e0639dc5SOlga Kornievskaia 	gen_boot_verifier(&copy->cp_res.wr_verifier, copy->cp_clp->net);
1162e0639dc5SOlga Kornievskaia }
1163e0639dc5SOlga Kornievskaia 
1164e0639dc5SOlga Kornievskaia static ssize_t _nfsd_copy_file_range(struct nfsd4_copy *copy)
1165e0639dc5SOlga Kornievskaia {
1166e0639dc5SOlga Kornievskaia 	ssize_t bytes_copied = 0;
1167e0639dc5SOlga Kornievskaia 	size_t bytes_total = copy->cp_count;
1168e0639dc5SOlga Kornievskaia 	u64 src_pos = copy->cp_src_pos;
1169e0639dc5SOlga Kornievskaia 	u64 dst_pos = copy->cp_dst_pos;
1170e0639dc5SOlga Kornievskaia 
1171e0639dc5SOlga Kornievskaia 	do {
1172e0639dc5SOlga Kornievskaia 		if (kthread_should_stop())
1173e0639dc5SOlga Kornievskaia 			break;
11745c4583b2SJeff Layton 		bytes_copied = nfsd_copy_file_range(copy->nf_src->nf_file,
11755c4583b2SJeff Layton 				src_pos, copy->nf_dst->nf_file, dst_pos,
11765c4583b2SJeff Layton 				bytes_total);
1177e0639dc5SOlga Kornievskaia 		if (bytes_copied <= 0)
1178e0639dc5SOlga Kornievskaia 			break;
1179e0639dc5SOlga Kornievskaia 		bytes_total -= bytes_copied;
1180e0639dc5SOlga Kornievskaia 		copy->cp_res.wr_bytes_written += bytes_copied;
1181e0639dc5SOlga Kornievskaia 		src_pos += bytes_copied;
1182e0639dc5SOlga Kornievskaia 		dst_pos += bytes_copied;
1183e0639dc5SOlga Kornievskaia 	} while (bytes_total > 0 && !copy->cp_synchronous);
1184e0639dc5SOlga Kornievskaia 	return bytes_copied;
1185e0639dc5SOlga Kornievskaia }
1186e0639dc5SOlga Kornievskaia 
1187e0639dc5SOlga Kornievskaia static __be32 nfsd4_do_copy(struct nfsd4_copy *copy, bool sync)
1188e0639dc5SOlga Kornievskaia {
1189e0639dc5SOlga Kornievskaia 	__be32 status;
1190e0639dc5SOlga Kornievskaia 	ssize_t bytes;
1191e0639dc5SOlga Kornievskaia 
1192e0639dc5SOlga Kornievskaia 	bytes = _nfsd_copy_file_range(copy);
1193e0639dc5SOlga Kornievskaia 	/* for async copy, we ignore the error, client can always retry
1194e0639dc5SOlga Kornievskaia 	 * to get the error
1195e0639dc5SOlga Kornievskaia 	 */
1196e0639dc5SOlga Kornievskaia 	if (bytes < 0 && !copy->cp_res.wr_bytes_written)
1197e0639dc5SOlga Kornievskaia 		status = nfserrno(bytes);
1198e0639dc5SOlga Kornievskaia 	else {
1199e0639dc5SOlga Kornievskaia 		nfsd4_init_copy_res(copy, sync);
1200e0639dc5SOlga Kornievskaia 		status = nfs_ok;
1201e0639dc5SOlga Kornievskaia 	}
1202e0639dc5SOlga Kornievskaia 
12035c4583b2SJeff Layton 	nfsd_file_put(copy->nf_src);
12045c4583b2SJeff Layton 	nfsd_file_put(copy->nf_dst);
1205e0639dc5SOlga Kornievskaia 	return status;
1206e0639dc5SOlga Kornievskaia }
1207e0639dc5SOlga Kornievskaia 
1208e0639dc5SOlga Kornievskaia static void dup_copy_fields(struct nfsd4_copy *src, struct nfsd4_copy *dst)
1209e0639dc5SOlga Kornievskaia {
1210e0639dc5SOlga Kornievskaia 	dst->cp_src_pos = src->cp_src_pos;
1211e0639dc5SOlga Kornievskaia 	dst->cp_dst_pos = src->cp_dst_pos;
1212e0639dc5SOlga Kornievskaia 	dst->cp_count = src->cp_count;
1213e0639dc5SOlga Kornievskaia 	dst->cp_synchronous = src->cp_synchronous;
1214e0639dc5SOlga Kornievskaia 	memcpy(&dst->cp_res, &src->cp_res, sizeof(src->cp_res));
1215e0639dc5SOlga Kornievskaia 	memcpy(&dst->fh, &src->fh, sizeof(src->fh));
1216e0639dc5SOlga Kornievskaia 	dst->cp_clp = src->cp_clp;
12175c4583b2SJeff Layton 	dst->nf_dst = nfsd_file_get(src->nf_dst);
12185c4583b2SJeff Layton 	dst->nf_src = nfsd_file_get(src->nf_src);
1219e0639dc5SOlga Kornievskaia 	memcpy(&dst->cp_stateid, &src->cp_stateid, sizeof(src->cp_stateid));
1220e0639dc5SOlga Kornievskaia }
1221e0639dc5SOlga Kornievskaia 
1222e0639dc5SOlga Kornievskaia static void cleanup_async_copy(struct nfsd4_copy *copy)
1223e0639dc5SOlga Kornievskaia {
1224e0639dc5SOlga Kornievskaia 	nfs4_free_cp_state(copy);
12255c4583b2SJeff Layton 	nfsd_file_put(copy->nf_dst);
12265c4583b2SJeff Layton 	nfsd_file_put(copy->nf_src);
1227e0639dc5SOlga Kornievskaia 	spin_lock(&copy->cp_clp->async_lock);
1228e0639dc5SOlga Kornievskaia 	list_del(&copy->copies);
1229e0639dc5SOlga Kornievskaia 	spin_unlock(&copy->cp_clp->async_lock);
1230e0639dc5SOlga Kornievskaia 	nfs4_put_copy(copy);
1231e0639dc5SOlga Kornievskaia }
1232e0639dc5SOlga Kornievskaia 
1233e0639dc5SOlga Kornievskaia static int nfsd4_do_async_copy(void *data)
1234e0639dc5SOlga Kornievskaia {
1235e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy = (struct nfsd4_copy *)data;
1236e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *cb_copy;
1237e0639dc5SOlga Kornievskaia 
1238e0639dc5SOlga Kornievskaia 	copy->nfserr = nfsd4_do_copy(copy, 0);
1239e0639dc5SOlga Kornievskaia 	cb_copy = kzalloc(sizeof(struct nfsd4_copy), GFP_KERNEL);
1240e0639dc5SOlga Kornievskaia 	if (!cb_copy)
1241e0639dc5SOlga Kornievskaia 		goto out;
1242e0639dc5SOlga Kornievskaia 	memcpy(&cb_copy->cp_res, &copy->cp_res, sizeof(copy->cp_res));
1243e0639dc5SOlga Kornievskaia 	cb_copy->cp_clp = copy->cp_clp;
1244e0639dc5SOlga Kornievskaia 	cb_copy->nfserr = copy->nfserr;
1245e0639dc5SOlga Kornievskaia 	memcpy(&cb_copy->fh, &copy->fh, sizeof(copy->fh));
1246e0639dc5SOlga Kornievskaia 	nfsd4_init_cb(&cb_copy->cp_cb, cb_copy->cp_clp,
1247e0639dc5SOlga Kornievskaia 			&nfsd4_cb_offload_ops, NFSPROC4_CLNT_CB_OFFLOAD);
1248e0639dc5SOlga Kornievskaia 	nfsd4_run_cb(&cb_copy->cp_cb);
1249e0639dc5SOlga Kornievskaia out:
1250e0639dc5SOlga Kornievskaia 	cleanup_async_copy(copy);
1251e0639dc5SOlga Kornievskaia 	return 0;
1252e0639dc5SOlga Kornievskaia }
1253e0639dc5SOlga Kornievskaia 
1254ffa0160aSChristoph Hellwig static __be32
125529ae7f9dSAnna Schumaker nfsd4_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1256eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
125729ae7f9dSAnna Schumaker {
1258eb69853dSChristoph Hellwig 	struct nfsd4_copy *copy = &u->copy;
125929ae7f9dSAnna Schumaker 	__be32 status;
1260e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *async_copy = NULL;
126129ae7f9dSAnna Schumaker 
1262e0639dc5SOlga Kornievskaia 	status = nfsd4_verify_copy(rqstp, cstate, &copy->cp_src_stateid,
12635c4583b2SJeff Layton 				   &copy->nf_src, &copy->cp_dst_stateid,
12645c4583b2SJeff Layton 				   &copy->nf_dst);
126529ae7f9dSAnna Schumaker 	if (status)
126629ae7f9dSAnna Schumaker 		goto out;
126729ae7f9dSAnna Schumaker 
1268e0639dc5SOlga Kornievskaia 	copy->cp_clp = cstate->clp;
1269e0639dc5SOlga Kornievskaia 	memcpy(&copy->fh, &cstate->current_fh.fh_handle,
1270e0639dc5SOlga Kornievskaia 		sizeof(struct knfsd_fh));
1271e0639dc5SOlga Kornievskaia 	if (!copy->cp_synchronous) {
1272e0639dc5SOlga Kornievskaia 		struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
127329ae7f9dSAnna Schumaker 
1274e0639dc5SOlga Kornievskaia 		status = nfserrno(-ENOMEM);
1275e0639dc5SOlga Kornievskaia 		async_copy = kzalloc(sizeof(struct nfsd4_copy), GFP_KERNEL);
1276e0639dc5SOlga Kornievskaia 		if (!async_copy)
1277e0639dc5SOlga Kornievskaia 			goto out;
1278e0639dc5SOlga Kornievskaia 		if (!nfs4_init_cp_state(nn, copy)) {
1279e0639dc5SOlga Kornievskaia 			kfree(async_copy);
1280e0639dc5SOlga Kornievskaia 			goto out;
128129ae7f9dSAnna Schumaker 		}
1282e0639dc5SOlga Kornievskaia 		refcount_set(&async_copy->refcount, 1);
1283e0639dc5SOlga Kornievskaia 		memcpy(&copy->cp_res.cb_stateid, &copy->cp_stateid,
1284e0639dc5SOlga Kornievskaia 			sizeof(copy->cp_stateid));
1285e0639dc5SOlga Kornievskaia 		dup_copy_fields(copy, async_copy);
1286e0639dc5SOlga Kornievskaia 		async_copy->copy_task = kthread_create(nfsd4_do_async_copy,
1287e0639dc5SOlga Kornievskaia 				async_copy, "%s", "copy thread");
1288e0639dc5SOlga Kornievskaia 		if (IS_ERR(async_copy->copy_task))
1289e0639dc5SOlga Kornievskaia 			goto out_err;
1290e0639dc5SOlga Kornievskaia 		spin_lock(&async_copy->cp_clp->async_lock);
1291e0639dc5SOlga Kornievskaia 		list_add(&async_copy->copies,
1292e0639dc5SOlga Kornievskaia 				&async_copy->cp_clp->async_copies);
1293e0639dc5SOlga Kornievskaia 		spin_unlock(&async_copy->cp_clp->async_lock);
1294e0639dc5SOlga Kornievskaia 		wake_up_process(async_copy->copy_task);
1295e0639dc5SOlga Kornievskaia 		status = nfs_ok;
1296e0639dc5SOlga Kornievskaia 	} else
1297e0639dc5SOlga Kornievskaia 		status = nfsd4_do_copy(copy, 1);
129829ae7f9dSAnna Schumaker out:
129929ae7f9dSAnna Schumaker 	return status;
1300e0639dc5SOlga Kornievskaia out_err:
130118f428d4SOlga Kornievskaia 	if (async_copy)
1302e0639dc5SOlga Kornievskaia 		cleanup_async_copy(async_copy);
1303e0639dc5SOlga Kornievskaia 	goto out;
1304e0639dc5SOlga Kornievskaia }
1305e0639dc5SOlga Kornievskaia 
1306e0639dc5SOlga Kornievskaia struct nfsd4_copy *
1307e0639dc5SOlga Kornievskaia find_async_copy(struct nfs4_client *clp, stateid_t *stateid)
1308e0639dc5SOlga Kornievskaia {
1309e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy;
1310e0639dc5SOlga Kornievskaia 
1311e0639dc5SOlga Kornievskaia 	spin_lock(&clp->async_lock);
1312e0639dc5SOlga Kornievskaia 	list_for_each_entry(copy, &clp->async_copies, copies) {
1313e0639dc5SOlga Kornievskaia 		if (memcmp(&copy->cp_stateid, stateid, NFS4_STATEID_SIZE))
1314e0639dc5SOlga Kornievskaia 			continue;
1315e0639dc5SOlga Kornievskaia 		refcount_inc(&copy->refcount);
1316e0639dc5SOlga Kornievskaia 		spin_unlock(&clp->async_lock);
1317e0639dc5SOlga Kornievskaia 		return copy;
1318e0639dc5SOlga Kornievskaia 	}
1319e0639dc5SOlga Kornievskaia 	spin_unlock(&clp->async_lock);
1320e0639dc5SOlga Kornievskaia 	return NULL;
132129ae7f9dSAnna Schumaker }
132229ae7f9dSAnna Schumaker 
132329ae7f9dSAnna Schumaker static __be32
1324885e2bf3SOlga Kornievskaia nfsd4_offload_cancel(struct svc_rqst *rqstp,
1325885e2bf3SOlga Kornievskaia 		     struct nfsd4_compound_state *cstate,
1326885e2bf3SOlga Kornievskaia 		     union nfsd4_op_u *u)
1327885e2bf3SOlga Kornievskaia {
1328e0639dc5SOlga Kornievskaia 	struct nfsd4_offload_status *os = &u->offload_status;
1329e0639dc5SOlga Kornievskaia 	__be32 status = 0;
1330e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy;
1331e0639dc5SOlga Kornievskaia 	struct nfs4_client *clp = cstate->clp;
1332e0639dc5SOlga Kornievskaia 
1333e0639dc5SOlga Kornievskaia 	copy = find_async_copy(clp, &os->stateid);
1334e0639dc5SOlga Kornievskaia 	if (copy)
1335e0639dc5SOlga Kornievskaia 		nfsd4_stop_copy(copy);
1336e0639dc5SOlga Kornievskaia 	else
1337e0639dc5SOlga Kornievskaia 		status = nfserr_bad_stateid;
1338e0639dc5SOlga Kornievskaia 
1339e0639dc5SOlga Kornievskaia 	return status;
1340885e2bf3SOlga Kornievskaia }
1341885e2bf3SOlga Kornievskaia 
1342885e2bf3SOlga Kornievskaia static __be32
1343*51911868SOlga Kornievskaia nfsd4_copy_notify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1344*51911868SOlga Kornievskaia 		  union nfsd4_op_u *u)
1345*51911868SOlga Kornievskaia {
1346*51911868SOlga Kornievskaia 	return nfserr_notsupp;
1347*51911868SOlga Kornievskaia }
1348*51911868SOlga Kornievskaia 
1349*51911868SOlga Kornievskaia static __be32
135095d871f0SAnna Schumaker nfsd4_fallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
135195d871f0SAnna Schumaker 		struct nfsd4_fallocate *fallocate, int flags)
135295d871f0SAnna Schumaker {
13530d4d6720SJ. Bruce Fields 	__be32 status;
13545c4583b2SJeff Layton 	struct nfsd_file *nf;
135595d871f0SAnna Schumaker 
1356aa0d6aedSAnna Schumaker 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
135795d871f0SAnna Schumaker 					    &fallocate->falloc_stateid,
13585c4583b2SJeff Layton 					    WR_STATE, &nf);
135995d871f0SAnna Schumaker 	if (status != nfs_ok) {
136095d871f0SAnna Schumaker 		dprintk("NFSD: nfsd4_fallocate: couldn't process stateid!\n");
136195d871f0SAnna Schumaker 		return status;
136295d871f0SAnna Schumaker 	}
136395d871f0SAnna Schumaker 
13645c4583b2SJeff Layton 	status = nfsd4_vfs_fallocate(rqstp, &cstate->current_fh, nf->nf_file,
136595d871f0SAnna Schumaker 				     fallocate->falloc_offset,
136695d871f0SAnna Schumaker 				     fallocate->falloc_length,
136795d871f0SAnna Schumaker 				     flags);
13685c4583b2SJeff Layton 	nfsd_file_put(nf);
136995d871f0SAnna Schumaker 	return status;
137095d871f0SAnna Schumaker }
13716308bc98SOlga Kornievskaia static __be32
13726308bc98SOlga Kornievskaia nfsd4_offload_status(struct svc_rqst *rqstp,
13736308bc98SOlga Kornievskaia 		     struct nfsd4_compound_state *cstate,
13746308bc98SOlga Kornievskaia 		     union nfsd4_op_u *u)
13756308bc98SOlga Kornievskaia {
1376e0639dc5SOlga Kornievskaia 	struct nfsd4_offload_status *os = &u->offload_status;
1377e0639dc5SOlga Kornievskaia 	__be32 status = 0;
1378e0639dc5SOlga Kornievskaia 	struct nfsd4_copy *copy;
1379e0639dc5SOlga Kornievskaia 	struct nfs4_client *clp = cstate->clp;
1380e0639dc5SOlga Kornievskaia 
1381e0639dc5SOlga Kornievskaia 	copy = find_async_copy(clp, &os->stateid);
1382e0639dc5SOlga Kornievskaia 	if (copy) {
1383e0639dc5SOlga Kornievskaia 		os->count = copy->cp_res.wr_bytes_written;
1384e0639dc5SOlga Kornievskaia 		nfs4_put_copy(copy);
1385e0639dc5SOlga Kornievskaia 	} else
1386e0639dc5SOlga Kornievskaia 		status = nfserr_bad_stateid;
1387e0639dc5SOlga Kornievskaia 
1388e0639dc5SOlga Kornievskaia 	return status;
13896308bc98SOlga Kornievskaia }
139095d871f0SAnna Schumaker 
139195d871f0SAnna Schumaker static __be32
139295d871f0SAnna Schumaker nfsd4_allocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1393eb69853dSChristoph Hellwig 	       union nfsd4_op_u *u)
139495d871f0SAnna Schumaker {
1395eb69853dSChristoph Hellwig 	return nfsd4_fallocate(rqstp, cstate, &u->allocate, 0);
139695d871f0SAnna Schumaker }
139795d871f0SAnna Schumaker 
139895d871f0SAnna Schumaker static __be32
1399b0cb9085SAnna Schumaker nfsd4_deallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1400eb69853dSChristoph Hellwig 		 union nfsd4_op_u *u)
1401b0cb9085SAnna Schumaker {
1402eb69853dSChristoph Hellwig 	return nfsd4_fallocate(rqstp, cstate, &u->deallocate,
1403b0cb9085SAnna Schumaker 			       FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
1404b0cb9085SAnna Schumaker }
1405b0cb9085SAnna Schumaker 
1406b0cb9085SAnna Schumaker static __be32
140724bab491SAnna Schumaker nfsd4_seek(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1408eb69853dSChristoph Hellwig 	   union nfsd4_op_u *u)
140924bab491SAnna Schumaker {
1410eb69853dSChristoph Hellwig 	struct nfsd4_seek *seek = &u->seek;
141124bab491SAnna Schumaker 	int whence;
141224bab491SAnna Schumaker 	__be32 status;
14135c4583b2SJeff Layton 	struct nfsd_file *nf;
141424bab491SAnna Schumaker 
1415aa0d6aedSAnna Schumaker 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
141624bab491SAnna Schumaker 					    &seek->seek_stateid,
14175c4583b2SJeff Layton 					    RD_STATE, &nf);
141824bab491SAnna Schumaker 	if (status) {
141924bab491SAnna Schumaker 		dprintk("NFSD: nfsd4_seek: couldn't process stateid!\n");
142024bab491SAnna Schumaker 		return status;
142124bab491SAnna Schumaker 	}
142224bab491SAnna Schumaker 
142324bab491SAnna Schumaker 	switch (seek->seek_whence) {
142424bab491SAnna Schumaker 	case NFS4_CONTENT_DATA:
142524bab491SAnna Schumaker 		whence = SEEK_DATA;
142624bab491SAnna Schumaker 		break;
142724bab491SAnna Schumaker 	case NFS4_CONTENT_HOLE:
142824bab491SAnna Schumaker 		whence = SEEK_HOLE;
142924bab491SAnna Schumaker 		break;
143024bab491SAnna Schumaker 	default:
143124bab491SAnna Schumaker 		status = nfserr_union_notsupp;
143224bab491SAnna Schumaker 		goto out;
143324bab491SAnna Schumaker 	}
143424bab491SAnna Schumaker 
143524bab491SAnna Schumaker 	/*
143624bab491SAnna Schumaker 	 * Note:  This call does change file->f_pos, but nothing in NFSD
143724bab491SAnna Schumaker 	 *        should ever file->f_pos.
143824bab491SAnna Schumaker 	 */
14395c4583b2SJeff Layton 	seek->seek_pos = vfs_llseek(nf->nf_file, seek->seek_offset, whence);
144024bab491SAnna Schumaker 	if (seek->seek_pos < 0)
144124bab491SAnna Schumaker 		status = nfserrno(seek->seek_pos);
14425c4583b2SJeff Layton 	else if (seek->seek_pos >= i_size_read(file_inode(nf->nf_file)))
144324bab491SAnna Schumaker 		seek->seek_eof = true;
144424bab491SAnna Schumaker 
144524bab491SAnna Schumaker out:
14465c4583b2SJeff Layton 	nfsd_file_put(nf);
144724bab491SAnna Schumaker 	return status;
144824bab491SAnna Schumaker }
144924bab491SAnna Schumaker 
14501da177e4SLinus Torvalds /* This routine never returns NFS_OK!  If there are no other errors, it
14511da177e4SLinus Torvalds  * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
14521da177e4SLinus Torvalds  * attributes matched.  VERIFY is implemented by mapping NFSERR_SAME
14531da177e4SLinus Torvalds  * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
14541da177e4SLinus Torvalds  */
1455b37ad28bSAl Viro static __be32
1456c954e2a5SJ.Bruce Fields _nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1457ca364317SJ.Bruce Fields 	     struct nfsd4_verify *verify)
14581da177e4SLinus Torvalds {
14592ebbc012SAl Viro 	__be32 *buf, *p;
14601da177e4SLinus Torvalds 	int count;
1461b37ad28bSAl Viro 	__be32 status;
14621da177e4SLinus Torvalds 
14638837abcaSMiklos Szeredi 	status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
14641da177e4SLinus Torvalds 	if (status)
14651da177e4SLinus Torvalds 		return status;
14661da177e4SLinus Torvalds 
14673c8e0316SYu Zhiguo 	status = check_attr_support(rqstp, cstate, verify->ve_bmval, NULL);
14683c8e0316SYu Zhiguo 	if (status)
14693c8e0316SYu Zhiguo 		return status;
14703c8e0316SYu Zhiguo 
14711da177e4SLinus Torvalds 	if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
14721da177e4SLinus Torvalds 	    || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
14731da177e4SLinus Torvalds 		return nfserr_inval;
14741da177e4SLinus Torvalds 	if (verify->ve_attrlen & 3)
14751da177e4SLinus Torvalds 		return nfserr_inval;
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 	/* count in words:
14781da177e4SLinus Torvalds 	 *   bitmap_len(1) + bitmap(2) + attr_len(1) = 4
14791da177e4SLinus Torvalds 	 */
14801da177e4SLinus Torvalds 	count = 4 + (verify->ve_attrlen >> 2);
14811da177e4SLinus Torvalds 	buf = kmalloc(count << 2, GFP_KERNEL);
14821da177e4SLinus Torvalds 	if (!buf)
14833e772463SJ. Bruce Fields 		return nfserr_jukebox;
14841da177e4SLinus Torvalds 
148584822d0bSJ. Bruce Fields 	p = buf;
1486d5184658SJ. Bruce Fields 	status = nfsd4_encode_fattr_to_buf(&p, count, &cstate->current_fh,
1487ca364317SJ.Bruce Fields 				    cstate->current_fh.fh_export,
1488d5184658SJ. Bruce Fields 				    cstate->current_fh.fh_dentry,
1489d5184658SJ. Bruce Fields 				    verify->ve_bmval,
1490406a7ea9SFrank Filz 				    rqstp, 0);
149141ae6e71SJ. Bruce Fields 	/*
149241ae6e71SJ. Bruce Fields 	 * If nfsd4_encode_fattr() ran out of space, assume that's because
149341ae6e71SJ. Bruce Fields 	 * the attributes are longer (hence different) than those given:
149441ae6e71SJ. Bruce Fields 	 */
149584822d0bSJ. Bruce Fields 	if (status == nfserr_resource)
14961da177e4SLinus Torvalds 		status = nfserr_not_same;
14971da177e4SLinus Torvalds 	if (status)
14981da177e4SLinus Torvalds 		goto out_kfree;
14991da177e4SLinus Torvalds 
150095ec28cdSBenny Halevy 	/* skip bitmap */
150195ec28cdSBenny Halevy 	p = buf + 1 + ntohl(buf[0]);
15021da177e4SLinus Torvalds 	status = nfserr_not_same;
15031da177e4SLinus Torvalds 	if (ntohl(*p++) != verify->ve_attrlen)
15041da177e4SLinus Torvalds 		goto out_kfree;
15051da177e4SLinus Torvalds 	if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
15061da177e4SLinus Torvalds 		status = nfserr_same;
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds out_kfree:
15091da177e4SLinus Torvalds 	kfree(buf);
15101da177e4SLinus Torvalds 	return status;
15111da177e4SLinus Torvalds }
15121da177e4SLinus Torvalds 
1513c954e2a5SJ.Bruce Fields static __be32
1514c954e2a5SJ.Bruce Fields nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1515eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
1516c954e2a5SJ.Bruce Fields {
1517c954e2a5SJ.Bruce Fields 	__be32 status;
1518c954e2a5SJ.Bruce Fields 
1519eb69853dSChristoph Hellwig 	status = _nfsd4_verify(rqstp, cstate, &u->verify);
1520c954e2a5SJ.Bruce Fields 	return status == nfserr_not_same ? nfs_ok : status;
1521c954e2a5SJ.Bruce Fields }
1522c954e2a5SJ.Bruce Fields 
1523c954e2a5SJ.Bruce Fields static __be32
1524c954e2a5SJ.Bruce Fields nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1525eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
1526c954e2a5SJ.Bruce Fields {
1527c954e2a5SJ.Bruce Fields 	__be32 status;
1528c954e2a5SJ.Bruce Fields 
1529eb69853dSChristoph Hellwig 	status = _nfsd4_verify(rqstp, cstate, &u->nverify);
1530c954e2a5SJ.Bruce Fields 	return status == nfserr_same ? nfs_ok : status;
1531c954e2a5SJ.Bruce Fields }
1532c954e2a5SJ.Bruce Fields 
15339cf514ccSChristoph Hellwig #ifdef CONFIG_NFSD_PNFS
15349cf514ccSChristoph Hellwig static const struct nfsd4_layout_ops *
15359cf514ccSChristoph Hellwig nfsd4_layout_verify(struct svc_export *exp, unsigned int layout_type)
15369cf514ccSChristoph Hellwig {
15378a4c3926SJeff Layton 	if (!exp->ex_layout_types) {
15389cf514ccSChristoph Hellwig 		dprintk("%s: export does not support pNFS\n", __func__);
15399cf514ccSChristoph Hellwig 		return NULL;
15409cf514ccSChristoph Hellwig 	}
15419cf514ccSChristoph Hellwig 
1542b550a32eSAri Kauppi 	if (layout_type >= LAYOUT_TYPE_MAX ||
1543b550a32eSAri Kauppi 	    !(exp->ex_layout_types & (1 << layout_type))) {
15449cf514ccSChristoph Hellwig 		dprintk("%s: layout type %d not supported\n",
15459cf514ccSChristoph Hellwig 			__func__, layout_type);
15469cf514ccSChristoph Hellwig 		return NULL;
15479cf514ccSChristoph Hellwig 	}
15489cf514ccSChristoph Hellwig 
15499cf514ccSChristoph Hellwig 	return nfsd4_layout_ops[layout_type];
15509cf514ccSChristoph Hellwig }
15519cf514ccSChristoph Hellwig 
15529cf514ccSChristoph Hellwig static __be32
15539cf514ccSChristoph Hellwig nfsd4_getdeviceinfo(struct svc_rqst *rqstp,
1554eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
15559cf514ccSChristoph Hellwig {
1556eb69853dSChristoph Hellwig 	struct nfsd4_getdeviceinfo *gdp = &u->getdeviceinfo;
15579cf514ccSChristoph Hellwig 	const struct nfsd4_layout_ops *ops;
15589cf514ccSChristoph Hellwig 	struct nfsd4_deviceid_map *map;
15599cf514ccSChristoph Hellwig 	struct svc_export *exp;
15609cf514ccSChristoph Hellwig 	__be32 nfserr;
15619cf514ccSChristoph Hellwig 
15629cf514ccSChristoph Hellwig 	dprintk("%s: layout_type %u dev_id [0x%llx:0x%x] maxcnt %u\n",
15639cf514ccSChristoph Hellwig 	       __func__,
15649cf514ccSChristoph Hellwig 	       gdp->gd_layout_type,
15659cf514ccSChristoph Hellwig 	       gdp->gd_devid.fsid_idx, gdp->gd_devid.generation,
15669cf514ccSChristoph Hellwig 	       gdp->gd_maxcount);
15679cf514ccSChristoph Hellwig 
15689cf514ccSChristoph Hellwig 	map = nfsd4_find_devid_map(gdp->gd_devid.fsid_idx);
15699cf514ccSChristoph Hellwig 	if (!map) {
15709cf514ccSChristoph Hellwig 		dprintk("%s: couldn't find device ID to export mapping!\n",
15719cf514ccSChristoph Hellwig 			__func__);
15729cf514ccSChristoph Hellwig 		return nfserr_noent;
15739cf514ccSChristoph Hellwig 	}
15749cf514ccSChristoph Hellwig 
15759cf514ccSChristoph Hellwig 	exp = rqst_exp_find(rqstp, map->fsid_type, map->fsid);
15769cf514ccSChristoph Hellwig 	if (IS_ERR(exp)) {
15779cf514ccSChristoph Hellwig 		dprintk("%s: could not find device id\n", __func__);
15789cf514ccSChristoph Hellwig 		return nfserr_noent;
15799cf514ccSChristoph Hellwig 	}
15809cf514ccSChristoph Hellwig 
15819cf514ccSChristoph Hellwig 	nfserr = nfserr_layoutunavailable;
15829cf514ccSChristoph Hellwig 	ops = nfsd4_layout_verify(exp, gdp->gd_layout_type);
15839cf514ccSChristoph Hellwig 	if (!ops)
15849cf514ccSChristoph Hellwig 		goto out;
15859cf514ccSChristoph Hellwig 
15869cf514ccSChristoph Hellwig 	nfserr = nfs_ok;
1587f99d4fbdSChristoph Hellwig 	if (gdp->gd_maxcount != 0) {
1588f99d4fbdSChristoph Hellwig 		nfserr = ops->proc_getdeviceinfo(exp->ex_path.mnt->mnt_sb,
1589d7c920d1STom Haynes 				rqstp, cstate->session->se_client, gdp);
1590f99d4fbdSChristoph Hellwig 	}
15919cf514ccSChristoph Hellwig 
15929cf514ccSChristoph Hellwig 	gdp->gd_notify_types &= ops->notify_types;
15939cf514ccSChristoph Hellwig out:
1594a1420384SKinglong Mee 	exp_put(exp);
15959cf514ccSChristoph Hellwig 	return nfserr;
15969cf514ccSChristoph Hellwig }
15979cf514ccSChristoph Hellwig 
159834b1744cSJ. Bruce Fields static void
159934b1744cSJ. Bruce Fields nfsd4_getdeviceinfo_release(union nfsd4_op_u *u)
160034b1744cSJ. Bruce Fields {
160134b1744cSJ. Bruce Fields 	kfree(u->getdeviceinfo.gd_device);
160234b1744cSJ. Bruce Fields }
160334b1744cSJ. Bruce Fields 
16049cf514ccSChristoph Hellwig static __be32
16059cf514ccSChristoph Hellwig nfsd4_layoutget(struct svc_rqst *rqstp,
1606eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
16079cf514ccSChristoph Hellwig {
1608eb69853dSChristoph Hellwig 	struct nfsd4_layoutget *lgp = &u->layoutget;
16099cf514ccSChristoph Hellwig 	struct svc_fh *current_fh = &cstate->current_fh;
16109cf514ccSChristoph Hellwig 	const struct nfsd4_layout_ops *ops;
16119cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls;
16129cf514ccSChristoph Hellwig 	__be32 nfserr;
161366282ec1SBenjamin Coddington 	int accmode = NFSD_MAY_READ_IF_EXEC;
16149cf514ccSChristoph Hellwig 
16159cf514ccSChristoph Hellwig 	switch (lgp->lg_seg.iomode) {
16169cf514ccSChristoph Hellwig 	case IOMODE_READ:
161766282ec1SBenjamin Coddington 		accmode |= NFSD_MAY_READ;
16189cf514ccSChristoph Hellwig 		break;
16199cf514ccSChristoph Hellwig 	case IOMODE_RW:
162066282ec1SBenjamin Coddington 		accmode |= NFSD_MAY_READ | NFSD_MAY_WRITE;
16219cf514ccSChristoph Hellwig 		break;
16229cf514ccSChristoph Hellwig 	default:
16239cf514ccSChristoph Hellwig 		dprintk("%s: invalid iomode %d\n",
16249cf514ccSChristoph Hellwig 			__func__, lgp->lg_seg.iomode);
16259cf514ccSChristoph Hellwig 		nfserr = nfserr_badiomode;
16269cf514ccSChristoph Hellwig 		goto out;
16279cf514ccSChristoph Hellwig 	}
16289cf514ccSChristoph Hellwig 
16299cf514ccSChristoph Hellwig 	nfserr = fh_verify(rqstp, current_fh, 0, accmode);
16309cf514ccSChristoph Hellwig 	if (nfserr)
16319cf514ccSChristoph Hellwig 		goto out;
16329cf514ccSChristoph Hellwig 
16339cf514ccSChristoph Hellwig 	nfserr = nfserr_layoutunavailable;
16349cf514ccSChristoph Hellwig 	ops = nfsd4_layout_verify(current_fh->fh_export, lgp->lg_layout_type);
16359cf514ccSChristoph Hellwig 	if (!ops)
16369cf514ccSChristoph Hellwig 		goto out;
16379cf514ccSChristoph Hellwig 
16389cf514ccSChristoph Hellwig 	/*
16399cf514ccSChristoph Hellwig 	 * Verify minlength and range as per RFC5661:
16409cf514ccSChristoph Hellwig 	 *  o  If loga_length is less than loga_minlength,
16419cf514ccSChristoph Hellwig 	 *     the metadata server MUST return NFS4ERR_INVAL.
16429cf514ccSChristoph Hellwig 	 *  o  If the sum of loga_offset and loga_minlength exceeds
16439cf514ccSChristoph Hellwig 	 *     NFS4_UINT64_MAX, and loga_minlength is not
16449cf514ccSChristoph Hellwig 	 *     NFS4_UINT64_MAX, the error NFS4ERR_INVAL MUST result.
16459cf514ccSChristoph Hellwig 	 *  o  If the sum of loga_offset and loga_length exceeds
16469cf514ccSChristoph Hellwig 	 *     NFS4_UINT64_MAX, and loga_length is not NFS4_UINT64_MAX,
16479cf514ccSChristoph Hellwig 	 *     the error NFS4ERR_INVAL MUST result.
16489cf514ccSChristoph Hellwig 	 */
16499cf514ccSChristoph Hellwig 	nfserr = nfserr_inval;
16509cf514ccSChristoph Hellwig 	if (lgp->lg_seg.length < lgp->lg_minlength ||
16519cf514ccSChristoph Hellwig 	    (lgp->lg_minlength != NFS4_MAX_UINT64 &&
16529cf514ccSChristoph Hellwig 	     lgp->lg_minlength > NFS4_MAX_UINT64 - lgp->lg_seg.offset) ||
16539cf514ccSChristoph Hellwig 	    (lgp->lg_seg.length != NFS4_MAX_UINT64 &&
16549cf514ccSChristoph Hellwig 	     lgp->lg_seg.length > NFS4_MAX_UINT64 - lgp->lg_seg.offset))
16559cf514ccSChristoph Hellwig 		goto out;
16569cf514ccSChristoph Hellwig 	if (lgp->lg_seg.length == 0)
16579cf514ccSChristoph Hellwig 		goto out;
16589cf514ccSChristoph Hellwig 
16599cf514ccSChristoph Hellwig 	nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lgp->lg_sid,
16609cf514ccSChristoph Hellwig 						true, lgp->lg_layout_type, &ls);
166131ef83dcSChristoph Hellwig 	if (nfserr) {
1662f394b62bSChuck Lever 		trace_nfsd_layout_get_lookup_fail(&lgp->lg_sid);
16639cf514ccSChristoph Hellwig 		goto out;
166431ef83dcSChristoph Hellwig 	}
16659cf514ccSChristoph Hellwig 
1666c5c707f9SChristoph Hellwig 	nfserr = nfserr_recallconflict;
1667c5c707f9SChristoph Hellwig 	if (atomic_read(&ls->ls_stid.sc_file->fi_lo_recalls))
1668c5c707f9SChristoph Hellwig 		goto out_put_stid;
1669c5c707f9SChristoph Hellwig 
16702b0143b5SDavid Howells 	nfserr = ops->proc_layoutget(d_inode(current_fh->fh_dentry),
16719cf514ccSChristoph Hellwig 				     current_fh, lgp);
16729cf514ccSChristoph Hellwig 	if (nfserr)
16739cf514ccSChristoph Hellwig 		goto out_put_stid;
16749cf514ccSChristoph Hellwig 
16759cf514ccSChristoph Hellwig 	nfserr = nfsd4_insert_layout(lgp, ls);
16769cf514ccSChristoph Hellwig 
16779cf514ccSChristoph Hellwig out_put_stid:
1678cc8a5532SJeff Layton 	mutex_unlock(&ls->ls_mutex);
16799cf514ccSChristoph Hellwig 	nfs4_put_stid(&ls->ls_stid);
16809cf514ccSChristoph Hellwig out:
16819cf514ccSChristoph Hellwig 	return nfserr;
16829cf514ccSChristoph Hellwig }
16839cf514ccSChristoph Hellwig 
168434b1744cSJ. Bruce Fields static void
168534b1744cSJ. Bruce Fields nfsd4_layoutget_release(union nfsd4_op_u *u)
168634b1744cSJ. Bruce Fields {
168734b1744cSJ. Bruce Fields 	kfree(u->layoutget.lg_content);
168834b1744cSJ. Bruce Fields }
168934b1744cSJ. Bruce Fields 
16909cf514ccSChristoph Hellwig static __be32
16919cf514ccSChristoph Hellwig nfsd4_layoutcommit(struct svc_rqst *rqstp,
1692eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
16939cf514ccSChristoph Hellwig {
1694eb69853dSChristoph Hellwig 	struct nfsd4_layoutcommit *lcp = &u->layoutcommit;
16959cf514ccSChristoph Hellwig 	const struct nfsd4_layout_seg *seg = &lcp->lc_seg;
16969cf514ccSChristoph Hellwig 	struct svc_fh *current_fh = &cstate->current_fh;
16979cf514ccSChristoph Hellwig 	const struct nfsd4_layout_ops *ops;
16989cf514ccSChristoph Hellwig 	loff_t new_size = lcp->lc_last_wr + 1;
16999cf514ccSChristoph Hellwig 	struct inode *inode;
17009cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls;
17019cf514ccSChristoph Hellwig 	__be32 nfserr;
17029cf514ccSChristoph Hellwig 
17039cf514ccSChristoph Hellwig 	nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_WRITE);
17049cf514ccSChristoph Hellwig 	if (nfserr)
17059cf514ccSChristoph Hellwig 		goto out;
17069cf514ccSChristoph Hellwig 
17079cf514ccSChristoph Hellwig 	nfserr = nfserr_layoutunavailable;
17089cf514ccSChristoph Hellwig 	ops = nfsd4_layout_verify(current_fh->fh_export, lcp->lc_layout_type);
17099cf514ccSChristoph Hellwig 	if (!ops)
17109cf514ccSChristoph Hellwig 		goto out;
17112b0143b5SDavid Howells 	inode = d_inode(current_fh->fh_dentry);
17129cf514ccSChristoph Hellwig 
17139cf514ccSChristoph Hellwig 	nfserr = nfserr_inval;
17149cf514ccSChristoph Hellwig 	if (new_size <= seg->offset) {
17159cf514ccSChristoph Hellwig 		dprintk("pnfsd: last write before layout segment\n");
17169cf514ccSChristoph Hellwig 		goto out;
17179cf514ccSChristoph Hellwig 	}
17189cf514ccSChristoph Hellwig 	if (new_size > seg->offset + seg->length) {
17199cf514ccSChristoph Hellwig 		dprintk("pnfsd: last write beyond layout segment\n");
17209cf514ccSChristoph Hellwig 		goto out;
17219cf514ccSChristoph Hellwig 	}
17229cf514ccSChristoph Hellwig 	if (!lcp->lc_newoffset && new_size > i_size_read(inode)) {
17239cf514ccSChristoph Hellwig 		dprintk("pnfsd: layoutcommit beyond EOF\n");
17249cf514ccSChristoph Hellwig 		goto out;
17259cf514ccSChristoph Hellwig 	}
17269cf514ccSChristoph Hellwig 
17279cf514ccSChristoph Hellwig 	nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lcp->lc_sid,
17289cf514ccSChristoph Hellwig 						false, lcp->lc_layout_type,
17299cf514ccSChristoph Hellwig 						&ls);
17309cf514ccSChristoph Hellwig 	if (nfserr) {
1731f394b62bSChuck Lever 		trace_nfsd_layout_commit_lookup_fail(&lcp->lc_sid);
17329cf514ccSChristoph Hellwig 		/* fixup error code as per RFC5661 */
17339cf514ccSChristoph Hellwig 		if (nfserr == nfserr_bad_stateid)
17349cf514ccSChristoph Hellwig 			nfserr = nfserr_badlayout;
17359cf514ccSChristoph Hellwig 		goto out;
17369cf514ccSChristoph Hellwig 	}
17379cf514ccSChristoph Hellwig 
1738cc8a5532SJeff Layton 	/* LAYOUTCOMMIT does not require any serialization */
1739cc8a5532SJeff Layton 	mutex_unlock(&ls->ls_mutex);
1740cc8a5532SJeff Layton 
17419cf514ccSChristoph Hellwig 	if (new_size > i_size_read(inode)) {
17429cf514ccSChristoph Hellwig 		lcp->lc_size_chg = 1;
17439cf514ccSChristoph Hellwig 		lcp->lc_newsize = new_size;
17449cf514ccSChristoph Hellwig 	} else {
17459cf514ccSChristoph Hellwig 		lcp->lc_size_chg = 0;
17469cf514ccSChristoph Hellwig 	}
17479cf514ccSChristoph Hellwig 
1748d8398fc1SKinglong Mee 	nfserr = ops->proc_layoutcommit(inode, lcp);
17499cf514ccSChristoph Hellwig 	nfs4_put_stid(&ls->ls_stid);
17509cf514ccSChristoph Hellwig out:
17519cf514ccSChristoph Hellwig 	return nfserr;
17529cf514ccSChristoph Hellwig }
17539cf514ccSChristoph Hellwig 
17549cf514ccSChristoph Hellwig static __be32
17559cf514ccSChristoph Hellwig nfsd4_layoutreturn(struct svc_rqst *rqstp,
1756eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
17579cf514ccSChristoph Hellwig {
1758eb69853dSChristoph Hellwig 	struct nfsd4_layoutreturn *lrp = &u->layoutreturn;
17599cf514ccSChristoph Hellwig 	struct svc_fh *current_fh = &cstate->current_fh;
17609cf514ccSChristoph Hellwig 	__be32 nfserr;
17619cf514ccSChristoph Hellwig 
17629cf514ccSChristoph Hellwig 	nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_NOP);
17639cf514ccSChristoph Hellwig 	if (nfserr)
17649cf514ccSChristoph Hellwig 		goto out;
17659cf514ccSChristoph Hellwig 
17669cf514ccSChristoph Hellwig 	nfserr = nfserr_layoutunavailable;
17679cf514ccSChristoph Hellwig 	if (!nfsd4_layout_verify(current_fh->fh_export, lrp->lr_layout_type))
17689cf514ccSChristoph Hellwig 		goto out;
17699cf514ccSChristoph Hellwig 
17709cf514ccSChristoph Hellwig 	switch (lrp->lr_seg.iomode) {
17719cf514ccSChristoph Hellwig 	case IOMODE_READ:
17729cf514ccSChristoph Hellwig 	case IOMODE_RW:
17739cf514ccSChristoph Hellwig 	case IOMODE_ANY:
17749cf514ccSChristoph Hellwig 		break;
17759cf514ccSChristoph Hellwig 	default:
17769cf514ccSChristoph Hellwig 		dprintk("%s: invalid iomode %d\n", __func__,
17779cf514ccSChristoph Hellwig 			lrp->lr_seg.iomode);
17789cf514ccSChristoph Hellwig 		nfserr = nfserr_inval;
17799cf514ccSChristoph Hellwig 		goto out;
17809cf514ccSChristoph Hellwig 	}
17819cf514ccSChristoph Hellwig 
17829cf514ccSChristoph Hellwig 	switch (lrp->lr_return_type) {
17839cf514ccSChristoph Hellwig 	case RETURN_FILE:
17849cf514ccSChristoph Hellwig 		nfserr = nfsd4_return_file_layouts(rqstp, cstate, lrp);
17859cf514ccSChristoph Hellwig 		break;
17869cf514ccSChristoph Hellwig 	case RETURN_FSID:
17879cf514ccSChristoph Hellwig 	case RETURN_ALL:
17889cf514ccSChristoph Hellwig 		nfserr = nfsd4_return_client_layouts(rqstp, cstate, lrp);
17899cf514ccSChristoph Hellwig 		break;
17909cf514ccSChristoph Hellwig 	default:
17919cf514ccSChristoph Hellwig 		dprintk("%s: invalid return_type %d\n", __func__,
17929cf514ccSChristoph Hellwig 			lrp->lr_return_type);
17939cf514ccSChristoph Hellwig 		nfserr = nfserr_inval;
17949cf514ccSChristoph Hellwig 		break;
17959cf514ccSChristoph Hellwig 	}
17969cf514ccSChristoph Hellwig out:
17979cf514ccSChristoph Hellwig 	return nfserr;
17989cf514ccSChristoph Hellwig }
17999cf514ccSChristoph Hellwig #endif /* CONFIG_NFSD_PNFS */
18009cf514ccSChristoph Hellwig 
18011da177e4SLinus Torvalds /*
18021da177e4SLinus Torvalds  * NULL call.
18031da177e4SLinus Torvalds  */
18047111c66eSAl Viro static __be32
1805a6beb732SChristoph Hellwig nfsd4_proc_null(struct svc_rqst *rqstp)
18061da177e4SLinus Torvalds {
18071da177e4SLinus Torvalds 	return nfs_ok;
18081da177e4SLinus Torvalds }
18091da177e4SLinus Torvalds 
1810e2b20950SShankar Anand static inline void nfsd4_increment_op_stats(u32 opnum)
1811e2b20950SShankar Anand {
1812e2b20950SShankar Anand 	if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
1813e2b20950SShankar Anand 		nfsdstats.nfs4_opcount[opnum]++;
1814e2b20950SShankar Anand }
1815e2b20950SShankar Anand 
1816bb2a8b0cSChristoph Hellwig static const struct nfsd4_operation nfsd4_ops[];
1817b591480bSJ.Bruce Fields 
1818f1c7f79bSAdrian Bunk static const char *nfsd4_op_name(unsigned opnum);
1819b001a1b6SBenny Halevy 
18201da177e4SLinus Torvalds /*
182157716355SJ. Bruce Fields  * Enforce NFSv4.1 COMPOUND ordering rules:
1822f9bb94c4SAndy Adamson  *
182357716355SJ. Bruce Fields  * Also note, enforced elsewhere:
182457716355SJ. Bruce Fields  *	- SEQUENCE other than as first op results in
182557716355SJ. Bruce Fields  *	  NFS4ERR_SEQUENCE_POS. (Enforced in nfsd4_sequence().)
18261d1bc8f2SJ. Bruce Fields  *	- BIND_CONN_TO_SESSION must be the only op in its compound.
18271d1bc8f2SJ. Bruce Fields  *	  (Enforced in nfsd4_bind_conn_to_session().)
182857716355SJ. Bruce Fields  *	- DESTROY_SESSION must be the final operation in a compound, if
182957716355SJ. Bruce Fields  *	  sessionid's in SEQUENCE and DESTROY_SESSION are the same.
183057716355SJ. Bruce Fields  *	  (Enforced in nfsd4_destroy_session().)
1831f9bb94c4SAndy Adamson  */
183257716355SJ. Bruce Fields static __be32 nfs41_check_op_ordering(struct nfsd4_compoundargs *args)
1833f9bb94c4SAndy Adamson {
18347a04cfdaSJ. Bruce Fields 	struct nfsd4_op *first_op = &args->ops[0];
183557716355SJ. Bruce Fields 
183657716355SJ. Bruce Fields 	/* These ordering requirements don't apply to NFSv4.0: */
183757716355SJ. Bruce Fields 	if (args->minorversion == 0)
183857716355SJ. Bruce Fields 		return nfs_ok;
183957716355SJ. Bruce Fields 	/* This is weird, but OK, not our problem: */
184057716355SJ. Bruce Fields 	if (args->opcnt == 0)
184157716355SJ. Bruce Fields 		return nfs_ok;
18427a04cfdaSJ. Bruce Fields 	if (first_op->status == nfserr_op_illegal)
184357716355SJ. Bruce Fields 		return nfs_ok;
18447a04cfdaSJ. Bruce Fields 	if (!(nfsd4_ops[first_op->opnum].op_flags & ALLOWED_AS_FIRST_OP))
184557716355SJ. Bruce Fields 		return nfserr_op_not_in_session;
18467a04cfdaSJ. Bruce Fields 	if (first_op->opnum == OP_SEQUENCE)
184757716355SJ. Bruce Fields 		return nfs_ok;
18487a04cfdaSJ. Bruce Fields 	/*
18497a04cfdaSJ. Bruce Fields 	 * So first_op is something allowed outside a session, like
18507a04cfdaSJ. Bruce Fields 	 * EXCHANGE_ID; but then it has to be the only op in the
18517a04cfdaSJ. Bruce Fields 	 * compound:
18527a04cfdaSJ. Bruce Fields 	 */
185357716355SJ. Bruce Fields 	if (args->opcnt != 1)
185457716355SJ. Bruce Fields 		return nfserr_not_only_op;
185557716355SJ. Bruce Fields 	return nfs_ok;
1856f9bb94c4SAndy Adamson }
1857f9bb94c4SAndy Adamson 
1858f4f9ef4aSJ. Bruce Fields const struct nfsd4_operation *OPDESC(struct nfsd4_op *op)
185922b03214SJ. Bruce Fields {
186022b03214SJ. Bruce Fields 	return &nfsd4_ops[op->opnum];
186122b03214SJ. Bruce Fields }
186222b03214SJ. Bruce Fields 
18631091006cSJ. Bruce Fields bool nfsd4_cache_this_op(struct nfsd4_op *op)
18641091006cSJ. Bruce Fields {
1865e372ba60SJ. Bruce Fields 	if (op->opnum == OP_ILLEGAL)
1866e372ba60SJ. Bruce Fields 		return false;
1867c856694eSJ. Bruce Fields 	return OPDESC(op)->op_flags & OP_CACHEME;
18681091006cSJ. Bruce Fields }
18691091006cSJ. Bruce Fields 
187068d93184SJ. Bruce Fields static bool need_wrongsec_check(struct svc_rqst *rqstp)
187168d93184SJ. Bruce Fields {
187268d93184SJ. Bruce Fields 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
187368d93184SJ. Bruce Fields 	struct nfsd4_compoundargs *argp = rqstp->rq_argp;
187468d93184SJ. Bruce Fields 	struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
187568d93184SJ. Bruce Fields 	struct nfsd4_op *next = &argp->ops[resp->opcnt];
1876bb2a8b0cSChristoph Hellwig 	const struct nfsd4_operation *thisd = OPDESC(this);
1877bb2a8b0cSChristoph Hellwig 	const struct nfsd4_operation *nextd;
187868d93184SJ. Bruce Fields 
187968d93184SJ. Bruce Fields 	/*
188068d93184SJ. Bruce Fields 	 * Most ops check wronsec on our own; only the putfh-like ops
188168d93184SJ. Bruce Fields 	 * have special rules.
188268d93184SJ. Bruce Fields 	 */
188368d93184SJ. Bruce Fields 	if (!(thisd->op_flags & OP_IS_PUTFH_LIKE))
188468d93184SJ. Bruce Fields 		return false;
188568d93184SJ. Bruce Fields 	/*
188668d93184SJ. Bruce Fields 	 * rfc 5661 2.6.3.1.1.6: don't bother erroring out a
188768d93184SJ. Bruce Fields 	 * put-filehandle operation if we're not going to use the
188868d93184SJ. Bruce Fields 	 * result:
188968d93184SJ. Bruce Fields 	 */
189068d93184SJ. Bruce Fields 	if (argp->opcnt == resp->opcnt)
189168d93184SJ. Bruce Fields 		return false;
189251904b08SJ. Bruce Fields 	if (next->opnum == OP_ILLEGAL)
189351904b08SJ. Bruce Fields 		return false;
189468d93184SJ. Bruce Fields 	nextd = OPDESC(next);
189568d93184SJ. Bruce Fields 	/*
189668d93184SJ. Bruce Fields 	 * Rest of 2.6.3.1.1: certain operations will return WRONGSEC
189768d93184SJ. Bruce Fields 	 * errors themselves as necessary; others should check for them
189868d93184SJ. Bruce Fields 	 * now:
189968d93184SJ. Bruce Fields 	 */
190068d93184SJ. Bruce Fields 	return !(nextd->op_flags & OP_HANDLES_WRONGSEC);
190168d93184SJ. Bruce Fields }
190268d93184SJ. Bruce Fields 
19032d124dfaSJ. Bruce Fields static void svcxdr_init_encode(struct svc_rqst *rqstp,
19042d124dfaSJ. Bruce Fields 			       struct nfsd4_compoundres *resp)
19052d124dfaSJ. Bruce Fields {
19062d124dfaSJ. Bruce Fields 	struct xdr_stream *xdr = &resp->xdr;
19072d124dfaSJ. Bruce Fields 	struct xdr_buf *buf = &rqstp->rq_res;
19082d124dfaSJ. Bruce Fields 	struct kvec *head = buf->head;
19092d124dfaSJ. Bruce Fields 
19102d124dfaSJ. Bruce Fields 	xdr->buf = buf;
1911ddd1ea56SJ. Bruce Fields 	xdr->iov = head;
19122d124dfaSJ. Bruce Fields 	xdr->p   = head->iov_base + head->iov_len;
1913a5cddc88SJ. Bruce Fields 	xdr->end = head->iov_base + PAGE_SIZE - rqstp->rq_auth_slack;
19146ac90391SJ. Bruce Fields 	/* Tail and page_len should be zero at this point: */
19156ac90391SJ. Bruce Fields 	buf->len = buf->head[0].iov_len;
19162825a7f9SJ. Bruce Fields 	xdr->scratch.iov_len = 0;
191705638dc7SJ. Bruce Fields 	xdr->page_ptr = buf->pages - 1;
19182825a7f9SJ. Bruce Fields 	buf->buflen = PAGE_SIZE * (1 + rqstp->rq_page_end - buf->pages)
1919a5cddc88SJ. Bruce Fields 		- rqstp->rq_auth_slack;
19202d124dfaSJ. Bruce Fields }
19212d124dfaSJ. Bruce Fields 
1922f9bb94c4SAndy Adamson /*
19231da177e4SLinus Torvalds  * COMPOUND call.
19241da177e4SLinus Torvalds  */
19257111c66eSAl Viro static __be32
1926a6beb732SChristoph Hellwig nfsd4_proc_compound(struct svc_rqst *rqstp)
19271da177e4SLinus Torvalds {
1928a6beb732SChristoph Hellwig 	struct nfsd4_compoundargs *args = rqstp->rq_argp;
1929a6beb732SChristoph Hellwig 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
19301da177e4SLinus Torvalds 	struct nfsd4_op	*op;
1931e354d571SAndy Adamson 	struct nfsd4_compound_state *cstate = &resp->cstate;
19324daeed25SKinglong Mee 	struct svc_fh *current_fh = &cstate->current_fh;
19334daeed25SKinglong Mee 	struct svc_fh *save_fh = &cstate->save_fh;
1934e333f3bbSTrond Myklebust 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1935b37ad28bSAl Viro 	__be32		status;
19361da177e4SLinus Torvalds 
19372d124dfaSJ. Bruce Fields 	svcxdr_init_encode(rqstp, resp);
19384aea24b2SJ. Bruce Fields 	resp->tagp = resp->xdr.p;
19391da177e4SLinus Torvalds 	/* reserve space for: taglen, tag, and opcnt */
1940d3f627c8SJ. Bruce Fields 	xdr_reserve_space(&resp->xdr, 8 + args->taglen);
19411da177e4SLinus Torvalds 	resp->taglen = args->taglen;
19421da177e4SLinus Torvalds 	resp->tag = args->tag;
19431da177e4SLinus Torvalds 	resp->rqstp = rqstp;
19444daeed25SKinglong Mee 	cstate->minorversion = args->minorversion;
19454daeed25SKinglong Mee 	fh_init(current_fh, NFS4_FHSIZE);
19464daeed25SKinglong Mee 	fh_init(save_fh, NFS4_FHSIZE);
19478ff30fa4SNeilBrown 	/*
19488ff30fa4SNeilBrown 	 * Don't use the deferral mechanism for NFSv4; compounds make it
19498ff30fa4SNeilBrown 	 * too hard to avoid non-idempotency problems.
19508ff30fa4SNeilBrown 	 */
195130660e04SJeff Layton 	clear_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
19521da177e4SLinus Torvalds 
19531da177e4SLinus Torvalds 	/*
19541da177e4SLinus Torvalds 	 * According to RFC3010, this takes precedence over all other errors.
19551da177e4SLinus Torvalds 	 */
19561da177e4SLinus Torvalds 	status = nfserr_minor_vers_mismatch;
1957e333f3bbSTrond Myklebust 	if (nfsd_minorversion(nn, args->minorversion, NFSD_TEST) <= 0)
19581da177e4SLinus Torvalds 		goto out;
19590078117cSJ. Bruce Fields 	status = nfserr_resource;
19600078117cSJ. Bruce Fields 	if (args->opcnt > NFSD_MAX_OPS_PER_COMPOUND)
19610078117cSJ. Bruce Fields 		goto out;
19621da177e4SLinus Torvalds 
196357716355SJ. Bruce Fields 	status = nfs41_check_op_ordering(args);
196457716355SJ. Bruce Fields 	if (status) {
1965f9bb94c4SAndy Adamson 		op = &args->ops[0];
196657716355SJ. Bruce Fields 		op->status = status;
19675b7b15aeSJ. Bruce Fields 		resp->opcnt = 1;
1968f9bb94c4SAndy Adamson 		goto encode_op;
1969f9bb94c4SAndy Adamson 	}
1970f9bb94c4SAndy Adamson 
1971fff4080bSChuck Lever 	trace_nfsd_compound(rqstp, args->opcnt);
19721da177e4SLinus Torvalds 	while (!status && resp->opcnt < args->opcnt) {
19731da177e4SLinus Torvalds 		op = &args->ops[resp->opcnt++];
19741da177e4SLinus Torvalds 
19751da177e4SLinus Torvalds 		/*
19761da177e4SLinus Torvalds 		 * The XDR decode routines may have pre-set op->status;
19771da177e4SLinus Torvalds 		 * for example, if there is a miscellaneous XDR error
19781da177e4SLinus Torvalds 		 * it will be set to nfserr_bad_xdr.
19791da177e4SLinus Torvalds 		 */
19809d313b17SJ. Bruce Fields 		if (op->status) {
19819d313b17SJ. Bruce Fields 			if (op->opnum == OP_OPEN)
19829d313b17SJ. Bruce Fields 				op->status = nfsd4_open_omfg(rqstp, cstate, op);
19831da177e4SLinus Torvalds 			goto encode_op;
19849d313b17SJ. Bruce Fields 		}
19851da177e4SLinus Torvalds 
19864daeed25SKinglong Mee 		if (!current_fh->fh_dentry) {
1987f4f9ef4aSJ. Bruce Fields 			if (!(op->opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
19881da177e4SLinus Torvalds 				op->status = nfserr_nofilehandle;
19891da177e4SLinus Torvalds 				goto encode_op;
19901da177e4SLinus Torvalds 			}
19914daeed25SKinglong Mee 		} else if (current_fh->fh_export->ex_fslocs.migrated &&
1992f4f9ef4aSJ. Bruce Fields 			  !(op->opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
199342ca0993SJ.Bruce Fields 			op->status = nfserr_moved;
199442ca0993SJ.Bruce Fields 			goto encode_op;
199542ca0993SJ.Bruce Fields 		}
1996b591480bSJ.Bruce Fields 
19972336745eSKinglong Mee 		fh_clear_wcc(current_fh);
19982336745eSKinglong Mee 
199958e7b33aSMi Jinlong 		/* If op is non-idempotent */
2000f4f9ef4aSJ. Bruce Fields 		if (op->opdesc->op_flags & OP_MODIFIES_SOMETHING) {
20014c69d585SJ. Bruce Fields 			/*
2002a8095f7eSJ. Bruce Fields 			 * Don't execute this op if we couldn't encode a
2003a8095f7eSJ. Bruce Fields 			 * succesful reply:
2004a8095f7eSJ. Bruce Fields 			 */
2005f4f9ef4aSJ. Bruce Fields 			u32 plen = op->opdesc->op_rsize_bop(rqstp, op);
2006a8095f7eSJ. Bruce Fields 			/*
2007a8095f7eSJ. Bruce Fields 			 * Plus if there's another operation, make sure
20084c69d585SJ. Bruce Fields 			 * we'll have space to at least encode an error:
20094c69d585SJ. Bruce Fields 			 */
20104c69d585SJ. Bruce Fields 			if (resp->opcnt < args->opcnt)
20114c69d585SJ. Bruce Fields 				plen += COMPOUND_ERR_SLACK_SPACE;
201258e7b33aSMi Jinlong 			op->status = nfsd4_check_resp_size(resp, plen);
201358e7b33aSMi Jinlong 		}
201458e7b33aSMi Jinlong 
201558e7b33aSMi Jinlong 		if (op->status)
201658e7b33aSMi Jinlong 			goto encode_op;
201758e7b33aSMi Jinlong 
2018f4f9ef4aSJ. Bruce Fields 		if (op->opdesc->op_get_currentstateid)
2019f4f9ef4aSJ. Bruce Fields 			op->opdesc->op_get_currentstateid(cstate, &op->u);
2020f4f9ef4aSJ. Bruce Fields 		op->status = op->opdesc->op_func(rqstp, cstate, &op->u);
20211da177e4SLinus Torvalds 
20229a307403SJ. Bruce Fields 		/* Only from SEQUENCE */
20239a307403SJ. Bruce Fields 		if (cstate->status == nfserr_replay_cache) {
20249a307403SJ. Bruce Fields 			dprintk("%s NFS4.1 replay from cache\n", __func__);
20259a307403SJ. Bruce Fields 			status = op->status;
20269a307403SJ. Bruce Fields 			goto out;
20279a307403SJ. Bruce Fields 		}
20288b70484cSTigran Mkrtchyan 		if (!op->status) {
2029f4f9ef4aSJ. Bruce Fields 			if (op->opdesc->op_set_currentstateid)
2030f4f9ef4aSJ. Bruce Fields 				op->opdesc->op_set_currentstateid(cstate, &op->u);
20318b70484cSTigran Mkrtchyan 
2032f4f9ef4aSJ. Bruce Fields 			if (op->opdesc->op_flags & OP_CLEAR_STATEID)
203337c593c5STigran Mkrtchyan 				clear_current_stateid(cstate);
20348b70484cSTigran Mkrtchyan 
20358b70484cSTigran Mkrtchyan 			if (need_wrongsec_check(rqstp))
20364daeed25SKinglong Mee 				op->status = check_nfsd_access(current_fh->fh_export, rqstp);
20378b70484cSTigran Mkrtchyan 		}
20381da177e4SLinus Torvalds encode_op:
2039a90b061cSAl Viro 		if (op->status == nfserr_replay_me) {
2040a4f1706aSJ.Bruce Fields 			op->replay = &cstate->replay_owner->so_replay;
2041d0a381ddSJ. Bruce Fields 			nfsd4_encode_replay(&resp->xdr, op);
20421da177e4SLinus Torvalds 			status = op->status = op->replay->rp_status;
20431da177e4SLinus Torvalds 		} else {
20441da177e4SLinus Torvalds 			nfsd4_encode_operation(resp, op);
20451da177e4SLinus Torvalds 			status = op->status;
20461da177e4SLinus Torvalds 		}
20470407717dSBenny Halevy 
2048fff4080bSChuck Lever 		trace_nfsd_compound_status(args->opcnt, resp->opcnt, status,
2049fff4080bSChuck Lever 					   nfsd4_op_name(op->opnum));
20500407717dSBenny Halevy 
205158fb12e6SJeff Layton 		nfsd4_cstate_clear_replay(cstate);
2052e2b20950SShankar Anand 		nfsd4_increment_op_stats(op->opnum);
20531da177e4SLinus Torvalds 	}
20541da177e4SLinus Torvalds 
20554daeed25SKinglong Mee 	cstate->status = status;
20564daeed25SKinglong Mee 	fh_put(current_fh);
20574daeed25SKinglong Mee 	fh_put(save_fh);
20584daeed25SKinglong Mee 	BUG_ON(cstate->replay_owner);
20591da177e4SLinus Torvalds out:
20602f425878SAndy Adamson 	/* Reset deferral mechanism for RPC deferrals */
206130660e04SJeff Layton 	set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
20623b12cd98SJ. Bruce Fields 	dprintk("nfsv4 compound returned %d\n", ntohl(status));
20631da177e4SLinus Torvalds 	return status;
20641da177e4SLinus Torvalds }
20651da177e4SLinus Torvalds 
206658e7b33aSMi Jinlong #define op_encode_hdr_size		(2)
206758e7b33aSMi Jinlong #define op_encode_stateid_maxsz		(XDR_QUADLEN(NFS4_STATEID_SIZE))
206858e7b33aSMi Jinlong #define op_encode_verifier_maxsz	(XDR_QUADLEN(NFS4_VERIFIER_SIZE))
206958e7b33aSMi Jinlong #define op_encode_change_info_maxsz	(5)
207058e7b33aSMi Jinlong #define nfs4_fattr_bitmap_maxsz		(4)
207158e7b33aSMi Jinlong 
20728c7424cfSJ. Bruce Fields /* We'll fall back on returning no lockowner if run out of space: */
20738c7424cfSJ. Bruce Fields #define op_encode_lockowner_maxsz	(0)
207458e7b33aSMi Jinlong #define op_encode_lock_denied_maxsz	(8 + op_encode_lockowner_maxsz)
207558e7b33aSMi Jinlong 
207658e7b33aSMi Jinlong #define nfs4_owner_maxsz		(1 + XDR_QUADLEN(IDMAP_NAMESZ))
207758e7b33aSMi Jinlong 
207858e7b33aSMi Jinlong #define op_encode_ace_maxsz		(3 + nfs4_owner_maxsz)
207958e7b33aSMi Jinlong #define op_encode_delegation_maxsz	(1 + op_encode_stateid_maxsz + 1 + \
208058e7b33aSMi Jinlong 					 op_encode_ace_maxsz)
208158e7b33aSMi Jinlong 
208258e7b33aSMi Jinlong #define op_encode_channel_attrs_maxsz	(6 + 1 + 1)
208358e7b33aSMi Jinlong 
208458e7b33aSMi Jinlong static inline u32 nfsd4_only_status_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
208558e7b33aSMi Jinlong {
208658e7b33aSMi Jinlong 	return (op_encode_hdr_size) * sizeof(__be32);
208758e7b33aSMi Jinlong }
208858e7b33aSMi Jinlong 
208958e7b33aSMi Jinlong static inline u32 nfsd4_status_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
209058e7b33aSMi Jinlong {
209158e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_stateid_maxsz)* sizeof(__be32);
209258e7b33aSMi Jinlong }
209358e7b33aSMi Jinlong 
20942282cd2cSKinglong Mee static inline u32 nfsd4_access_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
20952282cd2cSKinglong Mee {
20962282cd2cSKinglong Mee 	/* ac_supported, ac_resp_access */
20972282cd2cSKinglong Mee 	return (op_encode_hdr_size + 2)* sizeof(__be32);
20982282cd2cSKinglong Mee }
20992282cd2cSKinglong Mee 
210058e7b33aSMi Jinlong static inline u32 nfsd4_commit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
210158e7b33aSMi Jinlong {
210258e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_verifier_maxsz) * sizeof(__be32);
210358e7b33aSMi Jinlong }
210458e7b33aSMi Jinlong 
210558e7b33aSMi Jinlong static inline u32 nfsd4_create_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
210658e7b33aSMi Jinlong {
210758e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_change_info_maxsz
210858e7b33aSMi Jinlong 		+ nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
210958e7b33aSMi Jinlong }
211058e7b33aSMi Jinlong 
2111b86cef60SJ. Bruce Fields /*
2112b86cef60SJ. Bruce Fields  * Note since this is an idempotent operation we won't insist on failing
2113b86cef60SJ. Bruce Fields  * the op prematurely if the estimate is too large.  We may turn off splice
2114b86cef60SJ. Bruce Fields  * reads unnecessarily.
2115b86cef60SJ. Bruce Fields  */
2116b86cef60SJ. Bruce Fields static inline u32 nfsd4_getattr_rsize(struct svc_rqst *rqstp,
2117b86cef60SJ. Bruce Fields 				      struct nfsd4_op *op)
2118b86cef60SJ. Bruce Fields {
2119b86cef60SJ. Bruce Fields 	u32 *bmap = op->u.getattr.ga_bmval;
2120b86cef60SJ. Bruce Fields 	u32 bmap0 = bmap[0], bmap1 = bmap[1], bmap2 = bmap[2];
2121b86cef60SJ. Bruce Fields 	u32 ret = 0;
2122b86cef60SJ. Bruce Fields 
2123b86cef60SJ. Bruce Fields 	if (bmap0 & FATTR4_WORD0_ACL)
2124b86cef60SJ. Bruce Fields 		return svc_max_payload(rqstp);
2125b86cef60SJ. Bruce Fields 	if (bmap0 & FATTR4_WORD0_FS_LOCATIONS)
2126b86cef60SJ. Bruce Fields 		return svc_max_payload(rqstp);
2127b86cef60SJ. Bruce Fields 
2128b86cef60SJ. Bruce Fields 	if (bmap1 & FATTR4_WORD1_OWNER) {
2129b86cef60SJ. Bruce Fields 		ret += IDMAP_NAMESZ + 4;
2130b86cef60SJ. Bruce Fields 		bmap1 &= ~FATTR4_WORD1_OWNER;
2131b86cef60SJ. Bruce Fields 	}
2132b86cef60SJ. Bruce Fields 	if (bmap1 & FATTR4_WORD1_OWNER_GROUP) {
2133b86cef60SJ. Bruce Fields 		ret += IDMAP_NAMESZ + 4;
2134b86cef60SJ. Bruce Fields 		bmap1 &= ~FATTR4_WORD1_OWNER_GROUP;
2135b86cef60SJ. Bruce Fields 	}
2136b86cef60SJ. Bruce Fields 	if (bmap0 & FATTR4_WORD0_FILEHANDLE) {
2137b86cef60SJ. Bruce Fields 		ret += NFS4_FHSIZE + 4;
2138b86cef60SJ. Bruce Fields 		bmap0 &= ~FATTR4_WORD0_FILEHANDLE;
2139b86cef60SJ. Bruce Fields 	}
2140b86cef60SJ. Bruce Fields 	if (bmap2 & FATTR4_WORD2_SECURITY_LABEL) {
21411ec8c0c4SKinglong Mee 		ret += NFS4_MAXLABELLEN + 12;
2142b86cef60SJ. Bruce Fields 		bmap2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2143b86cef60SJ. Bruce Fields 	}
2144b86cef60SJ. Bruce Fields 	/*
2145b86cef60SJ. Bruce Fields 	 * Largest of remaining attributes are 16 bytes (e.g.,
2146b86cef60SJ. Bruce Fields 	 * supported_attributes)
2147b86cef60SJ. Bruce Fields 	 */
2148b86cef60SJ. Bruce Fields 	ret += 16 * (hweight32(bmap0) + hweight32(bmap1) + hweight32(bmap2));
2149b86cef60SJ. Bruce Fields 	/* bitmask, length */
2150b86cef60SJ. Bruce Fields 	ret += 20;
2151b86cef60SJ. Bruce Fields 	return ret;
2152b86cef60SJ. Bruce Fields }
2153b86cef60SJ. Bruce Fields 
21542282cd2cSKinglong Mee static inline u32 nfsd4_getfh_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
21552282cd2cSKinglong Mee {
21562282cd2cSKinglong Mee 	return (op_encode_hdr_size + 1) * sizeof(__be32) + NFS4_FHSIZE;
21572282cd2cSKinglong Mee }
21582282cd2cSKinglong Mee 
215958e7b33aSMi Jinlong static inline u32 nfsd4_link_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
216058e7b33aSMi Jinlong {
216158e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
216258e7b33aSMi Jinlong 		* sizeof(__be32);
216358e7b33aSMi Jinlong }
216458e7b33aSMi Jinlong 
216558e7b33aSMi Jinlong static inline u32 nfsd4_lock_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
216658e7b33aSMi Jinlong {
216758e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_lock_denied_maxsz)
216858e7b33aSMi Jinlong 		* sizeof(__be32);
216958e7b33aSMi Jinlong }
217058e7b33aSMi Jinlong 
217158e7b33aSMi Jinlong static inline u32 nfsd4_open_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
217258e7b33aSMi Jinlong {
217358e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_stateid_maxsz
217458e7b33aSMi Jinlong 		+ op_encode_change_info_maxsz + 1
217558e7b33aSMi Jinlong 		+ nfs4_fattr_bitmap_maxsz
217658e7b33aSMi Jinlong 		+ op_encode_delegation_maxsz) * sizeof(__be32);
217758e7b33aSMi Jinlong }
217858e7b33aSMi Jinlong 
217958e7b33aSMi Jinlong static inline u32 nfsd4_read_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
218058e7b33aSMi Jinlong {
218158e7b33aSMi Jinlong 	u32 maxcount = 0, rlen = 0;
218258e7b33aSMi Jinlong 
218358e7b33aSMi Jinlong 	maxcount = svc_max_payload(rqstp);
21843c7aa15dSKinglong Mee 	rlen = min(op->u.read.rd_length, maxcount);
218558e7b33aSMi Jinlong 
2186622f560eSJ. Bruce Fields 	return (op_encode_hdr_size + 2 + XDR_QUADLEN(rlen)) * sizeof(__be32);
218758e7b33aSMi Jinlong }
218858e7b33aSMi Jinlong 
218958e7b33aSMi Jinlong static inline u32 nfsd4_readdir_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
219058e7b33aSMi Jinlong {
21913c7aa15dSKinglong Mee 	u32 maxcount = 0, rlen = 0;
219258e7b33aSMi Jinlong 
21933c7aa15dSKinglong Mee 	maxcount = svc_max_payload(rqstp);
21943c7aa15dSKinglong Mee 	rlen = min(op->u.readdir.rd_maxcount, maxcount);
219558e7b33aSMi Jinlong 
2196561f0ed4SJ. Bruce Fields 	return (op_encode_hdr_size + op_encode_verifier_maxsz +
2197561f0ed4SJ. Bruce Fields 		XDR_QUADLEN(rlen)) * sizeof(__be32);
219858e7b33aSMi Jinlong }
219958e7b33aSMi Jinlong 
22002282cd2cSKinglong Mee static inline u32 nfsd4_readlink_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
22012282cd2cSKinglong Mee {
22022282cd2cSKinglong Mee 	return (op_encode_hdr_size + 1) * sizeof(__be32) + PAGE_SIZE;
22032282cd2cSKinglong Mee }
22042282cd2cSKinglong Mee 
220558e7b33aSMi Jinlong static inline u32 nfsd4_remove_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
220658e7b33aSMi Jinlong {
220758e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
220858e7b33aSMi Jinlong 		* sizeof(__be32);
220958e7b33aSMi Jinlong }
221058e7b33aSMi Jinlong 
221158e7b33aSMi Jinlong static inline u32 nfsd4_rename_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
221258e7b33aSMi Jinlong {
221358e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_change_info_maxsz
221458e7b33aSMi Jinlong 		+ op_encode_change_info_maxsz) * sizeof(__be32);
221558e7b33aSMi Jinlong }
221658e7b33aSMi Jinlong 
2217ccae70a9SJ. Bruce Fields static inline u32 nfsd4_sequence_rsize(struct svc_rqst *rqstp,
2218ccae70a9SJ. Bruce Fields 				       struct nfsd4_op *op)
2219ccae70a9SJ. Bruce Fields {
2220d1d84c96SJ. Bruce Fields 	return (op_encode_hdr_size
2221d1d84c96SJ. Bruce Fields 		+ XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 5) * sizeof(__be32);
2222ccae70a9SJ. Bruce Fields }
2223ccae70a9SJ. Bruce Fields 
22242282cd2cSKinglong Mee static inline u32 nfsd4_test_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
22252282cd2cSKinglong Mee {
22262282cd2cSKinglong Mee 	return (op_encode_hdr_size + 1 + op->u.test_stateid.ts_num_ids)
22272282cd2cSKinglong Mee 		* sizeof(__be32);
22282282cd2cSKinglong Mee }
22292282cd2cSKinglong Mee 
223058e7b33aSMi Jinlong static inline u32 nfsd4_setattr_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
223158e7b33aSMi Jinlong {
223258e7b33aSMi Jinlong 	return (op_encode_hdr_size + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
223358e7b33aSMi Jinlong }
223458e7b33aSMi Jinlong 
22352282cd2cSKinglong Mee static inline u32 nfsd4_secinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
22362282cd2cSKinglong Mee {
22372282cd2cSKinglong Mee 	return (op_encode_hdr_size + RPC_AUTH_MAXFLAVOR *
22382282cd2cSKinglong Mee 		(4 + XDR_QUADLEN(GSS_OID_MAX_LEN))) * sizeof(__be32);
22392282cd2cSKinglong Mee }
22402282cd2cSKinglong Mee 
224158e7b33aSMi Jinlong static inline u32 nfsd4_setclientid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
224258e7b33aSMi Jinlong {
2243480efaeeSJ. Bruce Fields 	return (op_encode_hdr_size + 2 + XDR_QUADLEN(NFS4_VERIFIER_SIZE)) *
2244480efaeeSJ. Bruce Fields 								sizeof(__be32);
224558e7b33aSMi Jinlong }
224658e7b33aSMi Jinlong 
224758e7b33aSMi Jinlong static inline u32 nfsd4_write_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
224858e7b33aSMi Jinlong {
2249f34e432bSJ. Bruce Fields 	return (op_encode_hdr_size + 2 + op_encode_verifier_maxsz) * sizeof(__be32);
225058e7b33aSMi Jinlong }
225158e7b33aSMi Jinlong 
225258e7b33aSMi Jinlong static inline u32 nfsd4_exchange_id_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
225358e7b33aSMi Jinlong {
225458e7b33aSMi Jinlong 	return (op_encode_hdr_size + 2 + 1 + /* eir_clientid, eir_sequenceid */\
2255a8bb84bcSKinglong Mee 		1 + 1 + /* eir_flags, spr_how */\
2256a8bb84bcSKinglong Mee 		4 + /* spo_must_enforce & _allow with bitmap */\
225758e7b33aSMi Jinlong 		2 + /*eir_server_owner.so_minor_id */\
225858e7b33aSMi Jinlong 		/* eir_server_owner.so_major_id<> */\
225958e7b33aSMi Jinlong 		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
226058e7b33aSMi Jinlong 		/* eir_server_scope<> */\
226158e7b33aSMi Jinlong 		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
226258e7b33aSMi Jinlong 		1 + /* eir_server_impl_id array length */\
226358e7b33aSMi Jinlong 		0 /* ignored eir_server_impl_id contents */) * sizeof(__be32);
226458e7b33aSMi Jinlong }
226558e7b33aSMi Jinlong 
226658e7b33aSMi Jinlong static inline u32 nfsd4_bind_conn_to_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
226758e7b33aSMi Jinlong {
226858e7b33aSMi Jinlong 	return (op_encode_hdr_size + \
226958e7b33aSMi Jinlong 		XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* bctsr_sessid */\
227058e7b33aSMi Jinlong 		2 /* bctsr_dir, use_conn_in_rdma_mode */) * sizeof(__be32);
227158e7b33aSMi Jinlong }
227258e7b33aSMi Jinlong 
227358e7b33aSMi Jinlong static inline u32 nfsd4_create_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
227458e7b33aSMi Jinlong {
227558e7b33aSMi Jinlong 	return (op_encode_hdr_size + \
227658e7b33aSMi Jinlong 		XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* sessionid */\
227758e7b33aSMi Jinlong 		2 + /* csr_sequence, csr_flags */\
227858e7b33aSMi Jinlong 		op_encode_channel_attrs_maxsz + \
227958e7b33aSMi Jinlong 		op_encode_channel_attrs_maxsz) * sizeof(__be32);
228058e7b33aSMi Jinlong }
228158e7b33aSMi Jinlong 
228229ae7f9dSAnna Schumaker static inline u32 nfsd4_copy_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
228329ae7f9dSAnna Schumaker {
228429ae7f9dSAnna Schumaker 	return (op_encode_hdr_size +
228529ae7f9dSAnna Schumaker 		1 /* wr_callback */ +
228629ae7f9dSAnna Schumaker 		op_encode_stateid_maxsz /* wr_callback */ +
228729ae7f9dSAnna Schumaker 		2 /* wr_count */ +
228829ae7f9dSAnna Schumaker 		1 /* wr_committed */ +
228929ae7f9dSAnna Schumaker 		op_encode_verifier_maxsz +
229029ae7f9dSAnna Schumaker 		1 /* cr_consecutive */ +
229129ae7f9dSAnna Schumaker 		1 /* cr_synchronous */) * sizeof(__be32);
229229ae7f9dSAnna Schumaker }
229329ae7f9dSAnna Schumaker 
22946308bc98SOlga Kornievskaia static inline u32 nfsd4_offload_status_rsize(struct svc_rqst *rqstp,
22956308bc98SOlga Kornievskaia 					     struct nfsd4_op *op)
22966308bc98SOlga Kornievskaia {
22976308bc98SOlga Kornievskaia 	return (op_encode_hdr_size +
22986308bc98SOlga Kornievskaia 		2 /* osr_count */ +
22996308bc98SOlga Kornievskaia 		1 /* osr_complete<1> optional 0 for now */) * sizeof(__be32);
23006308bc98SOlga Kornievskaia }
23016308bc98SOlga Kornievskaia 
2302*51911868SOlga Kornievskaia static inline u32 nfsd4_copy_notify_rsize(struct svc_rqst *rqstp,
2303*51911868SOlga Kornievskaia 					struct nfsd4_op *op)
2304*51911868SOlga Kornievskaia {
2305*51911868SOlga Kornievskaia 	return (op_encode_hdr_size +
2306*51911868SOlga Kornievskaia 		3 /* cnr_lease_time */ +
2307*51911868SOlga Kornievskaia 		1 /* We support one cnr_source_server */ +
2308*51911868SOlga Kornievskaia 		1 /* cnr_stateid seq */ +
2309*51911868SOlga Kornievskaia 		op_encode_stateid_maxsz /* cnr_stateid */ +
2310*51911868SOlga Kornievskaia 		1 /* num cnr_source_server*/ +
2311*51911868SOlga Kornievskaia 		1 /* nl4_type */ +
2312*51911868SOlga Kornievskaia 		1 /* nl4 size */ +
2313*51911868SOlga Kornievskaia 		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) /*nl4_loc + nl4_loc_sz */)
2314*51911868SOlga Kornievskaia 		* sizeof(__be32);
2315*51911868SOlga Kornievskaia }
2316*51911868SOlga Kornievskaia 
23179cf514ccSChristoph Hellwig #ifdef CONFIG_NFSD_PNFS
23182282cd2cSKinglong Mee static inline u32 nfsd4_getdeviceinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
23192282cd2cSKinglong Mee {
23202282cd2cSKinglong Mee 	u32 maxcount = 0, rlen = 0;
23212282cd2cSKinglong Mee 
23222282cd2cSKinglong Mee 	maxcount = svc_max_payload(rqstp);
23232282cd2cSKinglong Mee 	rlen = min(op->u.getdeviceinfo.gd_maxcount, maxcount);
23242282cd2cSKinglong Mee 
23252282cd2cSKinglong Mee 	return (op_encode_hdr_size +
23262282cd2cSKinglong Mee 		1 /* gd_layout_type*/ +
23272282cd2cSKinglong Mee 		XDR_QUADLEN(rlen) +
23282282cd2cSKinglong Mee 		2 /* gd_notify_types */) * sizeof(__be32);
23292282cd2cSKinglong Mee }
23302282cd2cSKinglong Mee 
23319cf514ccSChristoph Hellwig /*
23329cf514ccSChristoph Hellwig  * At this stage we don't really know what layout driver will handle the request,
23339cf514ccSChristoph Hellwig  * so we need to define an arbitrary upper bound here.
23349cf514ccSChristoph Hellwig  */
23359cf514ccSChristoph Hellwig #define MAX_LAYOUT_SIZE		128
23369cf514ccSChristoph Hellwig static inline u32 nfsd4_layoutget_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
23379cf514ccSChristoph Hellwig {
23389cf514ccSChristoph Hellwig 	return (op_encode_hdr_size +
23399cf514ccSChristoph Hellwig 		1 /* logr_return_on_close */ +
23409cf514ccSChristoph Hellwig 		op_encode_stateid_maxsz +
23419cf514ccSChristoph Hellwig 		1 /* nr of layouts */ +
23429cf514ccSChristoph Hellwig 		MAX_LAYOUT_SIZE) * sizeof(__be32);
23439cf514ccSChristoph Hellwig }
23449cf514ccSChristoph Hellwig 
23459cf514ccSChristoph Hellwig static inline u32 nfsd4_layoutcommit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
23469cf514ccSChristoph Hellwig {
23479cf514ccSChristoph Hellwig 	return (op_encode_hdr_size +
23489cf514ccSChristoph Hellwig 		1 /* locr_newsize */ +
23499cf514ccSChristoph Hellwig 		2 /* ns_size */) * sizeof(__be32);
23509cf514ccSChristoph Hellwig }
23519cf514ccSChristoph Hellwig 
23529cf514ccSChristoph Hellwig static inline u32 nfsd4_layoutreturn_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
23539cf514ccSChristoph Hellwig {
23549cf514ccSChristoph Hellwig 	return (op_encode_hdr_size +
23559cf514ccSChristoph Hellwig 		1 /* lrs_stateid */ +
23569cf514ccSChristoph Hellwig 		op_encode_stateid_maxsz) * sizeof(__be32);
23579cf514ccSChristoph Hellwig }
23589cf514ccSChristoph Hellwig #endif /* CONFIG_NFSD_PNFS */
23599cf514ccSChristoph Hellwig 
23602282cd2cSKinglong Mee 
23612282cd2cSKinglong Mee static inline u32 nfsd4_seek_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
23622282cd2cSKinglong Mee {
23632282cd2cSKinglong Mee 	return (op_encode_hdr_size + 3) * sizeof(__be32);
23642282cd2cSKinglong Mee }
23652282cd2cSKinglong Mee 
2366bb2a8b0cSChristoph Hellwig static const struct nfsd4_operation nfsd4_ops[] = {
2367b591480bSJ.Bruce Fields 	[OP_ACCESS] = {
2368eb69853dSChristoph Hellwig 		.op_func = nfsd4_access,
2369b001a1b6SBenny Halevy 		.op_name = "OP_ACCESS",
23701c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_access_rsize,
2371b591480bSJ.Bruce Fields 	},
2372b591480bSJ.Bruce Fields 	[OP_CLOSE] = {
2373eb69853dSChristoph Hellwig 		.op_func = nfsd4_close,
237458e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
2375b001a1b6SBenny Halevy 		.op_name = "OP_CLOSE",
23761c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_status_stateid_rsize,
237757832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_closestateid,
2378b60e9859SChristoph Hellwig 		.op_set_currentstateid = nfsd4_set_closestateid,
2379b591480bSJ.Bruce Fields 	},
2380b591480bSJ.Bruce Fields 	[OP_COMMIT] = {
2381eb69853dSChristoph Hellwig 		.op_func = nfsd4_commit,
238258e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
2383b001a1b6SBenny Halevy 		.op_name = "OP_COMMIT",
23841c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_commit_rsize,
2385b591480bSJ.Bruce Fields 	},
2386b591480bSJ.Bruce Fields 	[OP_CREATE] = {
2387eb69853dSChristoph Hellwig 		.op_func = nfsd4_create,
2388d1471053STigran Mkrtchyan 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME | OP_CLEAR_STATEID,
2389b001a1b6SBenny Halevy 		.op_name = "OP_CREATE",
23901c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_create_rsize,
2391b591480bSJ.Bruce Fields 	},
2392b591480bSJ.Bruce Fields 	[OP_DELEGRETURN] = {
2393eb69853dSChristoph Hellwig 		.op_func = nfsd4_delegreturn,
239458e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
2395b001a1b6SBenny Halevy 		.op_name = "OP_DELEGRETURN",
239658e7b33aSMi Jinlong 		.op_rsize_bop = nfsd4_only_status_rsize,
239757832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_delegreturnstateid,
2398b591480bSJ.Bruce Fields 	},
2399b591480bSJ.Bruce Fields 	[OP_GETATTR] = {
2400eb69853dSChristoph Hellwig 		.op_func = nfsd4_getattr,
2401eeac294eSJ.Bruce Fields 		.op_flags = ALLOWED_ON_ABSENT_FS,
2402b86cef60SJ. Bruce Fields 		.op_rsize_bop = nfsd4_getattr_rsize,
2403b001a1b6SBenny Halevy 		.op_name = "OP_GETATTR",
2404b591480bSJ.Bruce Fields 	},
2405b591480bSJ.Bruce Fields 	[OP_GETFH] = {
2406eb69853dSChristoph Hellwig 		.op_func = nfsd4_getfh,
2407b001a1b6SBenny Halevy 		.op_name = "OP_GETFH",
24081c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_getfh_rsize,
2409b591480bSJ.Bruce Fields 	},
2410b591480bSJ.Bruce Fields 	[OP_LINK] = {
2411eb69853dSChristoph Hellwig 		.op_func = nfsd4_link,
2412c856694eSJ. Bruce Fields 		.op_flags = ALLOWED_ON_ABSENT_FS | OP_MODIFIES_SOMETHING
2413c856694eSJ. Bruce Fields 				| OP_CACHEME,
2414b001a1b6SBenny Halevy 		.op_name = "OP_LINK",
24151c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_link_rsize,
2416b591480bSJ.Bruce Fields 	},
2417b591480bSJ.Bruce Fields 	[OP_LOCK] = {
2418eb69853dSChristoph Hellwig 		.op_func = nfsd4_lock,
2419b7571e4cSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING |
2420b7571e4cSJ. Bruce Fields 				OP_NONTRIVIAL_ERROR_ENCODE,
2421b001a1b6SBenny Halevy 		.op_name = "OP_LOCK",
24221c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_lock_rsize,
2423b60e9859SChristoph Hellwig 		.op_set_currentstateid = nfsd4_set_lockstateid,
2424b591480bSJ.Bruce Fields 	},
2425b591480bSJ.Bruce Fields 	[OP_LOCKT] = {
2426eb69853dSChristoph Hellwig 		.op_func = nfsd4_lockt,
2427b7571e4cSJ. Bruce Fields 		.op_flags = OP_NONTRIVIAL_ERROR_ENCODE,
2428b001a1b6SBenny Halevy 		.op_name = "OP_LOCKT",
24291c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_lock_rsize,
2430b591480bSJ.Bruce Fields 	},
2431b591480bSJ.Bruce Fields 	[OP_LOCKU] = {
2432eb69853dSChristoph Hellwig 		.op_func = nfsd4_locku,
243358e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
2434b001a1b6SBenny Halevy 		.op_name = "OP_LOCKU",
24351c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_status_stateid_rsize,
243657832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_lockustateid,
2437b591480bSJ.Bruce Fields 	},
2438b591480bSJ.Bruce Fields 	[OP_LOOKUP] = {
2439eb69853dSChristoph Hellwig 		.op_func = nfsd4_lookup,
2440d1471053STigran Mkrtchyan 		.op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
2441b001a1b6SBenny Halevy 		.op_name = "OP_LOOKUP",
24421c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2443b591480bSJ.Bruce Fields 	},
2444b591480bSJ.Bruce Fields 	[OP_LOOKUPP] = {
2445eb69853dSChristoph Hellwig 		.op_func = nfsd4_lookupp,
2446d1471053STigran Mkrtchyan 		.op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
2447b001a1b6SBenny Halevy 		.op_name = "OP_LOOKUPP",
24481c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2449b591480bSJ.Bruce Fields 	},
2450b591480bSJ.Bruce Fields 	[OP_NVERIFY] = {
2451eb69853dSChristoph Hellwig 		.op_func = nfsd4_nverify,
2452b001a1b6SBenny Halevy 		.op_name = "OP_NVERIFY",
24531c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2454b591480bSJ.Bruce Fields 	},
2455b591480bSJ.Bruce Fields 	[OP_OPEN] = {
2456eb69853dSChristoph Hellwig 		.op_func = nfsd4_open,
245758e7b33aSMi Jinlong 		.op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
2458b001a1b6SBenny Halevy 		.op_name = "OP_OPEN",
24591c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_open_rsize,
2460b60e9859SChristoph Hellwig 		.op_set_currentstateid = nfsd4_set_openstateid,
2461b591480bSJ.Bruce Fields 	},
2462b591480bSJ.Bruce Fields 	[OP_OPEN_CONFIRM] = {
2463eb69853dSChristoph Hellwig 		.op_func = nfsd4_open_confirm,
246458e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
2465b001a1b6SBenny Halevy 		.op_name = "OP_OPEN_CONFIRM",
24661c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_status_stateid_rsize,
2467b591480bSJ.Bruce Fields 	},
2468b591480bSJ.Bruce Fields 	[OP_OPEN_DOWNGRADE] = {
2469eb69853dSChristoph Hellwig 		.op_func = nfsd4_open_downgrade,
247058e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
2471b001a1b6SBenny Halevy 		.op_name = "OP_OPEN_DOWNGRADE",
24721c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_status_stateid_rsize,
247357832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_opendowngradestateid,
2474b60e9859SChristoph Hellwig 		.op_set_currentstateid = nfsd4_set_opendowngradestateid,
2475b591480bSJ.Bruce Fields 	},
2476b591480bSJ.Bruce Fields 	[OP_PUTFH] = {
2477eb69853dSChristoph Hellwig 		.op_func = nfsd4_putfh,
247868d93184SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
24795b648699SJ. Bruce Fields 				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
2480b001a1b6SBenny Halevy 		.op_name = "OP_PUTFH",
24811c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2482b591480bSJ.Bruce Fields 	},
2483eeac294eSJ.Bruce Fields 	[OP_PUTPUBFH] = {
2484eb69853dSChristoph Hellwig 		.op_func = nfsd4_putrootfh,
248568d93184SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
24865b648699SJ. Bruce Fields 				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
2487b001a1b6SBenny Halevy 		.op_name = "OP_PUTPUBFH",
24881c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2489eeac294eSJ.Bruce Fields 	},
2490b591480bSJ.Bruce Fields 	[OP_PUTROOTFH] = {
2491eb69853dSChristoph Hellwig 		.op_func = nfsd4_putrootfh,
249268d93184SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
24935b648699SJ. Bruce Fields 				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
2494b001a1b6SBenny Halevy 		.op_name = "OP_PUTROOTFH",
24951c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2496b591480bSJ.Bruce Fields 	},
2497b591480bSJ.Bruce Fields 	[OP_READ] = {
2498eb69853dSChristoph Hellwig 		.op_func = nfsd4_read,
249934b1744cSJ. Bruce Fields 		.op_release = nfsd4_read_release,
2500b001a1b6SBenny Halevy 		.op_name = "OP_READ",
25011c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_read_rsize,
250257832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_readstateid,
2503b591480bSJ.Bruce Fields 	},
2504b591480bSJ.Bruce Fields 	[OP_READDIR] = {
2505eb69853dSChristoph Hellwig 		.op_func = nfsd4_readdir,
2506b001a1b6SBenny Halevy 		.op_name = "OP_READDIR",
25071c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_readdir_rsize,
2508b591480bSJ.Bruce Fields 	},
2509b591480bSJ.Bruce Fields 	[OP_READLINK] = {
2510eb69853dSChristoph Hellwig 		.op_func = nfsd4_readlink,
2511b001a1b6SBenny Halevy 		.op_name = "OP_READLINK",
25121c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_readlink_rsize,
2513b591480bSJ.Bruce Fields 	},
2514b591480bSJ.Bruce Fields 	[OP_REMOVE] = {
2515eb69853dSChristoph Hellwig 		.op_func = nfsd4_remove,
2516c856694eSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
2517b001a1b6SBenny Halevy 		.op_name = "OP_REMOVE",
25181c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_remove_rsize,
2519b591480bSJ.Bruce Fields 	},
2520b591480bSJ.Bruce Fields 	[OP_RENAME] = {
2521eb69853dSChristoph Hellwig 		.op_func = nfsd4_rename,
2522c856694eSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
252358e7b33aSMi Jinlong 		.op_name = "OP_RENAME",
25241c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_rename_rsize,
2525b591480bSJ.Bruce Fields 	},
2526b591480bSJ.Bruce Fields 	[OP_RENEW] = {
2527eb69853dSChristoph Hellwig 		.op_func = nfsd4_renew,
252858e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
252958e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
2530b001a1b6SBenny Halevy 		.op_name = "OP_RENEW",
25311c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
253258e7b33aSMi Jinlong 
2533b591480bSJ.Bruce Fields 	},
2534b591480bSJ.Bruce Fields 	[OP_RESTOREFH] = {
2535eb69853dSChristoph Hellwig 		.op_func = nfsd4_restorefh,
253668d93184SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
253758e7b33aSMi Jinlong 				| OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING,
2538b001a1b6SBenny Halevy 		.op_name = "OP_RESTOREFH",
25391c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2540b591480bSJ.Bruce Fields 	},
2541b591480bSJ.Bruce Fields 	[OP_SAVEFH] = {
2542eb69853dSChristoph Hellwig 		.op_func = nfsd4_savefh,
254358e7b33aSMi Jinlong 		.op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
2544b001a1b6SBenny Halevy 		.op_name = "OP_SAVEFH",
25451c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2546b591480bSJ.Bruce Fields 	},
2547dcb488a3SAndy Adamson 	[OP_SECINFO] = {
2548eb69853dSChristoph Hellwig 		.op_func = nfsd4_secinfo,
254934b1744cSJ. Bruce Fields 		.op_release = nfsd4_secinfo_release,
255068d93184SJ. Bruce Fields 		.op_flags = OP_HANDLES_WRONGSEC,
2551b001a1b6SBenny Halevy 		.op_name = "OP_SECINFO",
25521c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_secinfo_rsize,
2553dcb488a3SAndy Adamson 	},
2554b591480bSJ.Bruce Fields 	[OP_SETATTR] = {
2555eb69853dSChristoph Hellwig 		.op_func = nfsd4_setattr,
2556b001a1b6SBenny Halevy 		.op_name = "OP_SETATTR",
2557b7571e4cSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME
2558b7571e4cSJ. Bruce Fields 				| OP_NONTRIVIAL_ERROR_ENCODE,
25591c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_setattr_rsize,
256057832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_setattrstateid,
2561b591480bSJ.Bruce Fields 	},
2562b591480bSJ.Bruce Fields 	[OP_SETCLIENTID] = {
2563eb69853dSChristoph Hellwig 		.op_func = nfsd4_setclientid,
256458e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
2565b7571e4cSJ. Bruce Fields 				| OP_MODIFIES_SOMETHING | OP_CACHEME
2566b7571e4cSJ. Bruce Fields 				| OP_NONTRIVIAL_ERROR_ENCODE,
2567b001a1b6SBenny Halevy 		.op_name = "OP_SETCLIENTID",
25681c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_setclientid_rsize,
2569b591480bSJ.Bruce Fields 	},
2570b591480bSJ.Bruce Fields 	[OP_SETCLIENTID_CONFIRM] = {
2571eb69853dSChristoph Hellwig 		.op_func = nfsd4_setclientid_confirm,
257258e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
2573c856694eSJ. Bruce Fields 				| OP_MODIFIES_SOMETHING | OP_CACHEME,
2574b001a1b6SBenny Halevy 		.op_name = "OP_SETCLIENTID_CONFIRM",
25751c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2576b591480bSJ.Bruce Fields 	},
2577b591480bSJ.Bruce Fields 	[OP_VERIFY] = {
2578eb69853dSChristoph Hellwig 		.op_func = nfsd4_verify,
2579b001a1b6SBenny Halevy 		.op_name = "OP_VERIFY",
25801c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2581b591480bSJ.Bruce Fields 	},
2582b591480bSJ.Bruce Fields 	[OP_WRITE] = {
2583eb69853dSChristoph Hellwig 		.op_func = nfsd4_write,
2584c856694eSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
2585b001a1b6SBenny Halevy 		.op_name = "OP_WRITE",
25861c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_write_rsize,
258757832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_writestateid,
2588b591480bSJ.Bruce Fields 	},
2589b591480bSJ.Bruce Fields 	[OP_RELEASE_LOCKOWNER] = {
2590eb69853dSChristoph Hellwig 		.op_func = nfsd4_release_lockowner,
259158e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
259258e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
2593b001a1b6SBenny Halevy 		.op_name = "OP_RELEASE_LOCKOWNER",
25941c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2595b591480bSJ.Bruce Fields 	},
2596069b6ad4SAndy Adamson 
2597069b6ad4SAndy Adamson 	/* NFSv4.1 operations */
2598069b6ad4SAndy Adamson 	[OP_EXCHANGE_ID] = {
2599eb69853dSChristoph Hellwig 		.op_func = nfsd4_exchange_id,
260058e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
260158e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
2602069b6ad4SAndy Adamson 		.op_name = "OP_EXCHANGE_ID",
26031c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_exchange_id_rsize,
2604069b6ad4SAndy Adamson 	},
2605cb73a9f4SJ. Bruce Fields 	[OP_BACKCHANNEL_CTL] = {
2606eb69853dSChristoph Hellwig 		.op_func = nfsd4_backchannel_ctl,
2607cb73a9f4SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
2608cb73a9f4SJ. Bruce Fields 		.op_name = "OP_BACKCHANNEL_CTL",
26091c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2610cb73a9f4SJ. Bruce Fields 	},
26111d1bc8f2SJ. Bruce Fields 	[OP_BIND_CONN_TO_SESSION] = {
2612eb69853dSChristoph Hellwig 		.op_func = nfsd4_bind_conn_to_session,
261358e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
261458e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
26151d1bc8f2SJ. Bruce Fields 		.op_name = "OP_BIND_CONN_TO_SESSION",
26161c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_bind_conn_to_session_rsize,
26171d1bc8f2SJ. Bruce Fields 	},
2618069b6ad4SAndy Adamson 	[OP_CREATE_SESSION] = {
2619eb69853dSChristoph Hellwig 		.op_func = nfsd4_create_session,
262058e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
262158e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
2622069b6ad4SAndy Adamson 		.op_name = "OP_CREATE_SESSION",
26231c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_create_session_rsize,
2624069b6ad4SAndy Adamson 	},
2625069b6ad4SAndy Adamson 	[OP_DESTROY_SESSION] = {
2626eb69853dSChristoph Hellwig 		.op_func = nfsd4_destroy_session,
262758e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
262858e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
2629069b6ad4SAndy Adamson 		.op_name = "OP_DESTROY_SESSION",
26301c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2631069b6ad4SAndy Adamson 	},
2632069b6ad4SAndy Adamson 	[OP_SEQUENCE] = {
2633eb69853dSChristoph Hellwig 		.op_func = nfsd4_sequence,
2634f9bb94c4SAndy Adamson 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
2635069b6ad4SAndy Adamson 		.op_name = "OP_SEQUENCE",
26361c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_sequence_rsize,
2637069b6ad4SAndy Adamson 	},
2638094b5d74SBenny Halevy 	[OP_DESTROY_CLIENTID] = {
2639eb69853dSChristoph Hellwig 		.op_func = nfsd4_destroy_clientid,
264058e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
264158e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
2642094b5d74SBenny Halevy 		.op_name = "OP_DESTROY_CLIENTID",
26431c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2644094b5d74SBenny Halevy 	},
26454dc6ec00SJ. Bruce Fields 	[OP_RECLAIM_COMPLETE] = {
2646eb69853dSChristoph Hellwig 		.op_func = nfsd4_reclaim_complete,
264758e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
26484dc6ec00SJ. Bruce Fields 		.op_name = "OP_RECLAIM_COMPLETE",
26491c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
26504dc6ec00SJ. Bruce Fields 	},
265104f4ad16SJ. Bruce Fields 	[OP_SECINFO_NO_NAME] = {
2652eb69853dSChristoph Hellwig 		.op_func = nfsd4_secinfo_no_name,
2653ec572b9eSEryu Guan 		.op_release = nfsd4_secinfo_no_name_release,
265468d93184SJ. Bruce Fields 		.op_flags = OP_HANDLES_WRONGSEC,
265504f4ad16SJ. Bruce Fields 		.op_name = "OP_SECINFO_NO_NAME",
26561c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_secinfo_rsize,
265704f4ad16SJ. Bruce Fields 	},
265817456804SBryan Schumaker 	[OP_TEST_STATEID] = {
2659eb69853dSChristoph Hellwig 		.op_func = nfsd4_test_stateid,
266017456804SBryan Schumaker 		.op_flags = ALLOWED_WITHOUT_FH,
266117456804SBryan Schumaker 		.op_name = "OP_TEST_STATEID",
26621c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_test_stateid_rsize,
266317456804SBryan Schumaker 	},
2664e1ca12dfSBryan Schumaker 	[OP_FREE_STATEID] = {
2665eb69853dSChristoph Hellwig 		.op_func = nfsd4_free_stateid,
266658e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
2667e1ca12dfSBryan Schumaker 		.op_name = "OP_FREE_STATEID",
266857832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_freestateid,
26691c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2670e1ca12dfSBryan Schumaker 	},
26719cf514ccSChristoph Hellwig #ifdef CONFIG_NFSD_PNFS
26729cf514ccSChristoph Hellwig 	[OP_GETDEVICEINFO] = {
2673eb69853dSChristoph Hellwig 		.op_func = nfsd4_getdeviceinfo,
267434b1744cSJ. Bruce Fields 		.op_release = nfsd4_getdeviceinfo_release,
26759cf514ccSChristoph Hellwig 		.op_flags = ALLOWED_WITHOUT_FH,
26769cf514ccSChristoph Hellwig 		.op_name = "OP_GETDEVICEINFO",
26771c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_getdeviceinfo_rsize,
26789cf514ccSChristoph Hellwig 	},
26799cf514ccSChristoph Hellwig 	[OP_LAYOUTGET] = {
2680eb69853dSChristoph Hellwig 		.op_func = nfsd4_layoutget,
268134b1744cSJ. Bruce Fields 		.op_release = nfsd4_layoutget_release,
26829cf514ccSChristoph Hellwig 		.op_flags = OP_MODIFIES_SOMETHING,
26839cf514ccSChristoph Hellwig 		.op_name = "OP_LAYOUTGET",
26841c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_layoutget_rsize,
26859cf514ccSChristoph Hellwig 	},
26869cf514ccSChristoph Hellwig 	[OP_LAYOUTCOMMIT] = {
2687eb69853dSChristoph Hellwig 		.op_func = nfsd4_layoutcommit,
26889cf514ccSChristoph Hellwig 		.op_flags = OP_MODIFIES_SOMETHING,
26899cf514ccSChristoph Hellwig 		.op_name = "OP_LAYOUTCOMMIT",
26901c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_layoutcommit_rsize,
26919cf514ccSChristoph Hellwig 	},
26929cf514ccSChristoph Hellwig 	[OP_LAYOUTRETURN] = {
2693eb69853dSChristoph Hellwig 		.op_func = nfsd4_layoutreturn,
26949cf514ccSChristoph Hellwig 		.op_flags = OP_MODIFIES_SOMETHING,
26959cf514ccSChristoph Hellwig 		.op_name = "OP_LAYOUTRETURN",
26961c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_layoutreturn_rsize,
26979cf514ccSChristoph Hellwig 	},
26989cf514ccSChristoph Hellwig #endif /* CONFIG_NFSD_PNFS */
269924bab491SAnna Schumaker 
270024bab491SAnna Schumaker 	/* NFSv4.2 operations */
270195d871f0SAnna Schumaker 	[OP_ALLOCATE] = {
2702eb69853dSChristoph Hellwig 		.op_func = nfsd4_allocate,
270303b31f48SOlga Kornievskaia 		.op_flags = OP_MODIFIES_SOMETHING,
270495d871f0SAnna Schumaker 		.op_name = "OP_ALLOCATE",
27051c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
270695d871f0SAnna Schumaker 	},
2707b0cb9085SAnna Schumaker 	[OP_DEALLOCATE] = {
2708eb69853dSChristoph Hellwig 		.op_func = nfsd4_deallocate,
270903b31f48SOlga Kornievskaia 		.op_flags = OP_MODIFIES_SOMETHING,
2710b0cb9085SAnna Schumaker 		.op_name = "OP_DEALLOCATE",
27111c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2712b0cb9085SAnna Schumaker 	},
2713ffa0160aSChristoph Hellwig 	[OP_CLONE] = {
2714eb69853dSChristoph Hellwig 		.op_func = nfsd4_clone,
271503b31f48SOlga Kornievskaia 		.op_flags = OP_MODIFIES_SOMETHING,
2716ffa0160aSChristoph Hellwig 		.op_name = "OP_CLONE",
27171c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2718ffa0160aSChristoph Hellwig 	},
271929ae7f9dSAnna Schumaker 	[OP_COPY] = {
2720eb69853dSChristoph Hellwig 		.op_func = nfsd4_copy,
272103b31f48SOlga Kornievskaia 		.op_flags = OP_MODIFIES_SOMETHING,
272229ae7f9dSAnna Schumaker 		.op_name = "OP_COPY",
27231c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_copy_rsize,
272429ae7f9dSAnna Schumaker 	},
272524bab491SAnna Schumaker 	[OP_SEEK] = {
2726eb69853dSChristoph Hellwig 		.op_func = nfsd4_seek,
272724bab491SAnna Schumaker 		.op_name = "OP_SEEK",
27281c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_seek_rsize,
272924bab491SAnna Schumaker 	},
27306308bc98SOlga Kornievskaia 	[OP_OFFLOAD_STATUS] = {
27316308bc98SOlga Kornievskaia 		.op_func = nfsd4_offload_status,
27326308bc98SOlga Kornievskaia 		.op_name = "OP_OFFLOAD_STATUS",
27336308bc98SOlga Kornievskaia 		.op_rsize_bop = nfsd4_offload_status_rsize,
27346308bc98SOlga Kornievskaia 	},
2735885e2bf3SOlga Kornievskaia 	[OP_OFFLOAD_CANCEL] = {
2736885e2bf3SOlga Kornievskaia 		.op_func = nfsd4_offload_cancel,
2737885e2bf3SOlga Kornievskaia 		.op_flags = OP_MODIFIES_SOMETHING,
2738885e2bf3SOlga Kornievskaia 		.op_name = "OP_OFFLOAD_CANCEL",
2739885e2bf3SOlga Kornievskaia 		.op_rsize_bop = nfsd4_only_status_rsize,
2740885e2bf3SOlga Kornievskaia 	},
2741*51911868SOlga Kornievskaia 	[OP_COPY_NOTIFY] = {
2742*51911868SOlga Kornievskaia 		.op_func = nfsd4_copy_notify,
2743*51911868SOlga Kornievskaia 		.op_flags = OP_MODIFIES_SOMETHING,
2744*51911868SOlga Kornievskaia 		.op_name = "OP_COPY_NOTIFY",
2745*51911868SOlga Kornievskaia 		.op_rsize_bop = nfsd4_copy_notify_rsize,
2746*51911868SOlga Kornievskaia 	},
2747b591480bSJ.Bruce Fields };
2748b591480bSJ.Bruce Fields 
2749ed941643SAndrew Elble /**
2750ed941643SAndrew Elble  * nfsd4_spo_must_allow - Determine if the compound op contains an
2751ed941643SAndrew Elble  * operation that is allowed to be sent with machine credentials
2752ed941643SAndrew Elble  *
2753ed941643SAndrew Elble  * @rqstp: a pointer to the struct svc_rqst
2754ed941643SAndrew Elble  *
2755ed941643SAndrew Elble  * Checks to see if the compound contains a spo_must_allow op
2756ed941643SAndrew Elble  * and confirms that it was sent with the proper machine creds.
2757ed941643SAndrew Elble  */
2758ed941643SAndrew Elble 
2759ed941643SAndrew Elble bool nfsd4_spo_must_allow(struct svc_rqst *rqstp)
2760ed941643SAndrew Elble {
2761ed941643SAndrew Elble 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
2762ed941643SAndrew Elble 	struct nfsd4_compoundargs *argp = rqstp->rq_argp;
2763ed941643SAndrew Elble 	struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
2764ed941643SAndrew Elble 	struct nfsd4_compound_state *cstate = &resp->cstate;
2765ed941643SAndrew Elble 	struct nfs4_op_map *allow = &cstate->clp->cl_spo_must_allow;
2766ed941643SAndrew Elble 	u32 opiter;
2767ed941643SAndrew Elble 
2768ed941643SAndrew Elble 	if (!cstate->minorversion)
2769ed941643SAndrew Elble 		return false;
2770ed941643SAndrew Elble 
2771ed941643SAndrew Elble 	if (cstate->spo_must_allowed == true)
2772ed941643SAndrew Elble 		return true;
2773ed941643SAndrew Elble 
2774ed941643SAndrew Elble 	opiter = resp->opcnt;
2775ed941643SAndrew Elble 	while (opiter < argp->opcnt) {
2776ed941643SAndrew Elble 		this = &argp->ops[opiter++];
2777ed941643SAndrew Elble 		if (test_bit(this->opnum, allow->u.longs) &&
2778ed941643SAndrew Elble 			cstate->clp->cl_mach_cred &&
2779ed941643SAndrew Elble 			nfsd4_mach_creds_match(cstate->clp, rqstp)) {
2780ed941643SAndrew Elble 			cstate->spo_must_allowed = true;
2781ed941643SAndrew Elble 			return true;
2782ed941643SAndrew Elble 		}
2783ed941643SAndrew Elble 	}
2784ed941643SAndrew Elble 	cstate->spo_must_allowed = false;
2785ed941643SAndrew Elble 	return false;
2786ed941643SAndrew Elble }
2787ed941643SAndrew Elble 
27884f0cefbfSJ. Bruce Fields int nfsd4_max_reply(struct svc_rqst *rqstp, struct nfsd4_op *op)
27894f0cefbfSJ. Bruce Fields {
279005b7278dSOlga Kornievskaia 	if (op->opnum == OP_ILLEGAL || op->status == nfserr_notsupp)
27914f0cefbfSJ. Bruce Fields 		return op_encode_hdr_size * sizeof(__be32);
27922282cd2cSKinglong Mee 
27932282cd2cSKinglong Mee 	BUG_ON(OPDESC(op)->op_rsize_bop == NULL);
27942282cd2cSKinglong Mee 	return OPDESC(op)->op_rsize_bop(rqstp, op);
27954f0cefbfSJ. Bruce Fields }
27964f0cefbfSJ. Bruce Fields 
279707d1f802SJ. Bruce Fields void warn_on_nonidempotent_op(struct nfsd4_op *op)
279807d1f802SJ. Bruce Fields {
279907d1f802SJ. Bruce Fields 	if (OPDESC(op)->op_flags & OP_MODIFIES_SOMETHING) {
280007d1f802SJ. Bruce Fields 		pr_err("unable to encode reply to nonidempotent op %d (%s)\n",
280107d1f802SJ. Bruce Fields 			op->opnum, nfsd4_op_name(op->opnum));
280207d1f802SJ. Bruce Fields 		WARN_ON_ONCE(1);
280307d1f802SJ. Bruce Fields 	}
280407d1f802SJ. Bruce Fields }
280507d1f802SJ. Bruce Fields 
2806f1c7f79bSAdrian Bunk static const char *nfsd4_op_name(unsigned opnum)
2807b001a1b6SBenny Halevy {
2808b001a1b6SBenny Halevy 	if (opnum < ARRAY_SIZE(nfsd4_ops))
2809b001a1b6SBenny Halevy 		return nfsd4_ops[opnum].op_name;
2810b001a1b6SBenny Halevy 	return "unknown_operation";
2811b001a1b6SBenny Halevy }
2812b001a1b6SBenny Halevy 
28131da177e4SLinus Torvalds #define nfsd4_voidres			nfsd4_voidargs
28141da177e4SLinus Torvalds struct nfsd4_voidargs { int dummy; };
28151da177e4SLinus Torvalds 
2816860bda29SChristoph Hellwig static const struct svc_procedure nfsd_procedures4[2] = {
28170a93a47fSYu Zhiguo 	[NFSPROC4_NULL] = {
2818f7235b6bSChristoph Hellwig 		.pc_func = nfsd4_proc_null,
281963f8de37SChristoph Hellwig 		.pc_encode = nfs4svc_encode_voidres,
28200a93a47fSYu Zhiguo 		.pc_argsize = sizeof(struct nfsd4_voidargs),
28210a93a47fSYu Zhiguo 		.pc_ressize = sizeof(struct nfsd4_voidres),
28220a93a47fSYu Zhiguo 		.pc_cachetype = RC_NOCACHE,
28230a93a47fSYu Zhiguo 		.pc_xdrressize = 1,
28240a93a47fSYu Zhiguo 	},
28250a93a47fSYu Zhiguo 	[NFSPROC4_COMPOUND] = {
2826f7235b6bSChristoph Hellwig 		.pc_func = nfsd4_proc_compound,
2827026fec7eSChristoph Hellwig 		.pc_decode = nfs4svc_decode_compoundargs,
282863f8de37SChristoph Hellwig 		.pc_encode = nfs4svc_encode_compoundres,
28290a93a47fSYu Zhiguo 		.pc_argsize = sizeof(struct nfsd4_compoundargs),
28300a93a47fSYu Zhiguo 		.pc_ressize = sizeof(struct nfsd4_compoundres),
28313e98abffSJ. Bruce Fields 		.pc_release = nfsd4_release_compoundargs,
28320a93a47fSYu Zhiguo 		.pc_cachetype = RC_NOCACHE,
28330a93a47fSYu Zhiguo 		.pc_xdrressize = NFSD_BUFSIZE/4,
28340a93a47fSYu Zhiguo 	},
28351da177e4SLinus Torvalds };
28361da177e4SLinus Torvalds 
28377fd38af9SChristoph Hellwig static unsigned int nfsd_count3[ARRAY_SIZE(nfsd_procedures4)];
2838e9679189SChristoph Hellwig const struct svc_version nfsd_version4 = {
28391da177e4SLinus Torvalds 	.vs_vers		= 4,
28401da177e4SLinus Torvalds 	.vs_nproc		= 2,
28411da177e4SLinus Torvalds 	.vs_proc		= nfsd_procedures4,
28427fd38af9SChristoph Hellwig 	.vs_count		= nfsd_count3,
28431da177e4SLinus Torvalds 	.vs_dispatch		= nfsd_dispatch,
28441da177e4SLinus Torvalds 	.vs_xdrsize		= NFS4_SVC_XDRSIZE,
284505a45a2dSJeff Layton 	.vs_rpcb_optnl		= true,
28465283b03eSJeff Layton 	.vs_need_cong_ctrl	= true,
28471da177e4SLinus Torvalds };
28481da177e4SLinus Torvalds 
28491da177e4SLinus Torvalds /*
28501da177e4SLinus Torvalds  * Local variables:
28511da177e4SLinus Torvalds  *  c-basic-offset: 8
28521da177e4SLinus Torvalds  * End:
28531da177e4SLinus Torvalds  */
2854