xref: /openbmc/linux/fs/nfsd/nfs4proc.c (revision 24bab491220faa446d945624086d838af41d616c)
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  */
357e06b7f9SNeilBrown #include <linux/file.h>
365a0e3ad6STejun Heo #include <linux/slab.h>
371da177e4SLinus Torvalds 
3858e7b33aSMi Jinlong #include "idmap.h"
399a74af21SBoaz Harrosh #include "cache.h"
409a74af21SBoaz Harrosh #include "xdr4.h"
410a3adadeSJ. Bruce Fields #include "vfs.h"
428b70484cSTigran Mkrtchyan #include "current_stateid.h"
433320fef1SStanislav Kinsbursky #include "netns.h"
444ac7249eSChristoph Hellwig #include "acl.h"
451da177e4SLinus Torvalds 
4618032ca0SDavid Quigley #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
4718032ca0SDavid Quigley #include <linux/security.h>
4818032ca0SDavid Quigley 
4918032ca0SDavid Quigley static inline void
5018032ca0SDavid Quigley nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
5118032ca0SDavid Quigley {
5218032ca0SDavid Quigley 	struct inode *inode = resfh->fh_dentry->d_inode;
5318032ca0SDavid Quigley 	int status;
5418032ca0SDavid Quigley 
5518032ca0SDavid Quigley 	mutex_lock(&inode->i_mutex);
5618032ca0SDavid Quigley 	status = security_inode_setsecctx(resfh->fh_dentry,
5718032ca0SDavid Quigley 		label->data, label->len);
5818032ca0SDavid Quigley 	mutex_unlock(&inode->i_mutex);
5918032ca0SDavid Quigley 
6018032ca0SDavid Quigley 	if (status)
6118032ca0SDavid Quigley 		/*
6218032ca0SDavid Quigley 		 * XXX: We should really fail the whole open, but we may
6318032ca0SDavid Quigley 		 * already have created a new file, so it may be too
6418032ca0SDavid Quigley 		 * late.  For now this seems the least of evils:
6518032ca0SDavid Quigley 		 */
6618032ca0SDavid Quigley 		bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
6718032ca0SDavid Quigley 
6818032ca0SDavid Quigley 	return;
6918032ca0SDavid Quigley }
7018032ca0SDavid Quigley #else
7118032ca0SDavid Quigley static inline void
7218032ca0SDavid Quigley nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
7318032ca0SDavid Quigley { }
7418032ca0SDavid Quigley #endif
7518032ca0SDavid Quigley 
761da177e4SLinus Torvalds #define NFSDDBG_FACILITY		NFSDDBG_PROC
771da177e4SLinus Torvalds 
783c8e0316SYu Zhiguo static u32 nfsd_attrmask[] = {
793c8e0316SYu Zhiguo 	NFSD_WRITEABLE_ATTRS_WORD0,
803c8e0316SYu Zhiguo 	NFSD_WRITEABLE_ATTRS_WORD1,
813c8e0316SYu Zhiguo 	NFSD_WRITEABLE_ATTRS_WORD2
823c8e0316SYu Zhiguo };
833c8e0316SYu Zhiguo 
843c8e0316SYu Zhiguo static u32 nfsd41_ex_attrmask[] = {
853c8e0316SYu Zhiguo 	NFSD_SUPPATTR_EXCLCREAT_WORD0,
863c8e0316SYu Zhiguo 	NFSD_SUPPATTR_EXCLCREAT_WORD1,
873c8e0316SYu Zhiguo 	NFSD_SUPPATTR_EXCLCREAT_WORD2
883c8e0316SYu Zhiguo };
893c8e0316SYu Zhiguo 
903c8e0316SYu Zhiguo static __be32
913c8e0316SYu Zhiguo check_attr_support(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
923c8e0316SYu Zhiguo 		   u32 *bmval, u32 *writable)
933c8e0316SYu Zhiguo {
943c8e0316SYu Zhiguo 	struct dentry *dentry = cstate->current_fh.fh_dentry;
953c8e0316SYu Zhiguo 
963c8e0316SYu Zhiguo 	/*
973c8e0316SYu Zhiguo 	 * Check about attributes are supported by the NFSv4 server or not.
983c8e0316SYu Zhiguo 	 * According to spec, unsupported attributes return ERR_ATTRNOTSUPP.
993c8e0316SYu Zhiguo 	 */
1003c8e0316SYu Zhiguo 	if ((bmval[0] & ~nfsd_suppattrs0(cstate->minorversion)) ||
1013c8e0316SYu Zhiguo 	    (bmval[1] & ~nfsd_suppattrs1(cstate->minorversion)) ||
1023c8e0316SYu Zhiguo 	    (bmval[2] & ~nfsd_suppattrs2(cstate->minorversion)))
1033c8e0316SYu Zhiguo 		return nfserr_attrnotsupp;
1043c8e0316SYu Zhiguo 
1053c8e0316SYu Zhiguo 	/*
106a06b1261STrond Myklebust 	 * Check FATTR4_WORD0_ACL can be supported
1073c8e0316SYu Zhiguo 	 * in current environment or not.
1083c8e0316SYu Zhiguo 	 */
1093c8e0316SYu Zhiguo 	if (bmval[0] & FATTR4_WORD0_ACL) {
1103c8e0316SYu Zhiguo 		if (!IS_POSIXACL(dentry->d_inode))
1113c8e0316SYu Zhiguo 			return nfserr_attrnotsupp;
1123c8e0316SYu Zhiguo 	}
1133c8e0316SYu Zhiguo 
1143c8e0316SYu Zhiguo 	/*
1153c8e0316SYu Zhiguo 	 * According to spec, read-only attributes return ERR_INVAL.
1163c8e0316SYu Zhiguo 	 */
1173c8e0316SYu Zhiguo 	if (writable) {
1183c8e0316SYu Zhiguo 		if ((bmval[0] & ~writable[0]) || (bmval[1] & ~writable[1]) ||
1193c8e0316SYu Zhiguo 		    (bmval[2] & ~writable[2]))
1203c8e0316SYu Zhiguo 			return nfserr_inval;
1213c8e0316SYu Zhiguo 	}
1223c8e0316SYu Zhiguo 
1233c8e0316SYu Zhiguo 	return nfs_ok;
1243c8e0316SYu Zhiguo }
1253c8e0316SYu Zhiguo 
1263c8e0316SYu Zhiguo static __be32
1273c8e0316SYu Zhiguo nfsd4_check_open_attributes(struct svc_rqst *rqstp,
1283c8e0316SYu Zhiguo 	struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
1293c8e0316SYu Zhiguo {
1303c8e0316SYu Zhiguo 	__be32 status = nfs_ok;
1313c8e0316SYu Zhiguo 
1323c8e0316SYu Zhiguo 	if (open->op_create == NFS4_OPEN_CREATE) {
1333c8e0316SYu Zhiguo 		if (open->op_createmode == NFS4_CREATE_UNCHECKED
1343c8e0316SYu Zhiguo 		    || open->op_createmode == NFS4_CREATE_GUARDED)
1353c8e0316SYu Zhiguo 			status = check_attr_support(rqstp, cstate,
1363c8e0316SYu Zhiguo 					open->op_bmval, nfsd_attrmask);
1373c8e0316SYu Zhiguo 		else if (open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1)
1383c8e0316SYu Zhiguo 			status = check_attr_support(rqstp, cstate,
1393c8e0316SYu Zhiguo 					open->op_bmval, nfsd41_ex_attrmask);
1403c8e0316SYu Zhiguo 	}
1413c8e0316SYu Zhiguo 
1423c8e0316SYu Zhiguo 	return status;
1433c8e0316SYu Zhiguo }
1443c8e0316SYu Zhiguo 
1459208faf2SYu Zhiguo static int
1469208faf2SYu Zhiguo is_create_with_attrs(struct nfsd4_open *open)
1479208faf2SYu Zhiguo {
1489208faf2SYu Zhiguo 	return open->op_create == NFS4_OPEN_CREATE
1499208faf2SYu Zhiguo 		&& (open->op_createmode == NFS4_CREATE_UNCHECKED
1509208faf2SYu Zhiguo 		    || open->op_createmode == NFS4_CREATE_GUARDED
1519208faf2SYu Zhiguo 		    || open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1);
1529208faf2SYu Zhiguo }
1539208faf2SYu Zhiguo 
1549208faf2SYu Zhiguo /*
1559208faf2SYu Zhiguo  * if error occurs when setting the acl, just clear the acl bit
1569208faf2SYu Zhiguo  * in the returned attr bitmap.
1579208faf2SYu Zhiguo  */
1589208faf2SYu Zhiguo static void
1599208faf2SYu Zhiguo do_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
1609208faf2SYu Zhiguo 		struct nfs4_acl *acl, u32 *bmval)
1619208faf2SYu Zhiguo {
1629208faf2SYu Zhiguo 	__be32 status;
1639208faf2SYu Zhiguo 
1649208faf2SYu Zhiguo 	status = nfsd4_set_nfs4_acl(rqstp, fhp, acl);
1659208faf2SYu Zhiguo 	if (status)
1669208faf2SYu Zhiguo 		/*
1679208faf2SYu Zhiguo 		 * We should probably fail the whole open at this point,
1689208faf2SYu Zhiguo 		 * but we've already created the file, so it's too late;
1699208faf2SYu Zhiguo 		 * So this seems the least of evils:
1709208faf2SYu Zhiguo 		 */
1719208faf2SYu Zhiguo 		bmval[0] &= ~FATTR4_WORD0_ACL;
1729208faf2SYu Zhiguo }
1739208faf2SYu Zhiguo 
1741da177e4SLinus Torvalds static inline void
1751da177e4SLinus Torvalds fh_dup2(struct svc_fh *dst, struct svc_fh *src)
1761da177e4SLinus Torvalds {
1771da177e4SLinus Torvalds 	fh_put(dst);
1781da177e4SLinus Torvalds 	dget(src->fh_dentry);
1791da177e4SLinus Torvalds 	if (src->fh_export)
180bf18f163SKinglong Mee 		exp_get(src->fh_export);
1811da177e4SLinus Torvalds 	*dst = *src;
1821da177e4SLinus Torvalds }
1831da177e4SLinus Torvalds 
184b37ad28bSAl Viro static __be32
185dc730e17SJ. Bruce Fields do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, int accmode)
1861da177e4SLinus Torvalds {
187b37ad28bSAl Viro 	__be32 status;
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	if (open->op_truncate &&
1901da177e4SLinus Torvalds 		!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
1911da177e4SLinus Torvalds 		return nfserr_inval;
1921da177e4SLinus Torvalds 
193a043226bSJ. Bruce Fields 	accmode |= NFSD_MAY_READ_IF_EXEC;
194a043226bSJ. Bruce Fields 
1951da177e4SLinus Torvalds 	if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
1968837abcaSMiklos Szeredi 		accmode |= NFSD_MAY_READ;
1979801d8a3SJ. Bruce Fields 	if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1988837abcaSMiklos Szeredi 		accmode |= (NFSD_MAY_WRITE | NFSD_MAY_TRUNC);
19957ecb34fSJ. Bruce Fields 	if (open->op_share_deny & NFS4_SHARE_DENY_READ)
2008837abcaSMiklos Szeredi 		accmode |= NFSD_MAY_WRITE;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	status = fh_verify(rqstp, current_fh, S_IFREG, accmode);
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	return status;
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds 
207aadab6c6SJ. Bruce Fields static __be32 nfsd_check_obj_isreg(struct svc_fh *fh)
208aadab6c6SJ. Bruce Fields {
209aadab6c6SJ. Bruce Fields 	umode_t mode = fh->fh_dentry->d_inode->i_mode;
210aadab6c6SJ. Bruce Fields 
211aadab6c6SJ. Bruce Fields 	if (S_ISREG(mode))
212aadab6c6SJ. Bruce Fields 		return nfs_ok;
213aadab6c6SJ. Bruce Fields 	if (S_ISDIR(mode))
214aadab6c6SJ. Bruce Fields 		return nfserr_isdir;
215aadab6c6SJ. Bruce Fields 	/*
216aadab6c6SJ. Bruce Fields 	 * Using err_symlink as our catch-all case may look odd; but
217aadab6c6SJ. Bruce Fields 	 * there's no other obvious error for this case in 4.0, and we
218aadab6c6SJ. Bruce Fields 	 * happen to know that it will cause the linux v4 client to do
219aadab6c6SJ. Bruce Fields 	 * the right thing on attempts to open something other than a
220aadab6c6SJ. Bruce Fields 	 * regular file.
221aadab6c6SJ. Bruce Fields 	 */
222aadab6c6SJ. Bruce Fields 	return nfserr_symlink;
223aadab6c6SJ. Bruce Fields }
224aadab6c6SJ. Bruce Fields 
225bbc9c36cSJ. Bruce Fields static void nfsd4_set_open_owner_reply_cache(struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh *resfh)
2261da177e4SLinus Torvalds {
227bbc9c36cSJ. Bruce Fields 	if (nfsd4_has_session(cstate))
228bbc9c36cSJ. Bruce Fields 		return;
229bbc9c36cSJ. Bruce Fields 	fh_copy_shallow(&open->op_openowner->oo_owner.so_replay.rp_openfh,
230bbc9c36cSJ. Bruce Fields 			&resfh->fh_handle);
231bbc9c36cSJ. Bruce Fields }
232bbc9c36cSJ. Bruce Fields 
233bbc9c36cSJ. Bruce Fields static __be32
234c0e6bee4SJ. Bruce Fields do_open_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh **resfh)
235bbc9c36cSJ. Bruce Fields {
236bbc9c36cSJ. Bruce Fields 	struct svc_fh *current_fh = &cstate->current_fh;
2377007c90fSNeil Brown 	int accmode;
238b37ad28bSAl Viro 	__be32 status;
2391da177e4SLinus Torvalds 
240c0e6bee4SJ. Bruce Fields 	*resfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
241c0e6bee4SJ. Bruce Fields 	if (!*resfh)
24259deeb9eSJ. Bruce Fields 		return nfserr_jukebox;
243c0e6bee4SJ. Bruce Fields 	fh_init(*resfh, NFS4_FHSIZE);
2441da177e4SLinus Torvalds 	open->op_truncate = 0;
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 	if (open->op_create) {
24779fb54abSBenny Halevy 		/* FIXME: check session persistence and pnfs flags.
24879fb54abSBenny Halevy 		 * The nfsv4.1 spec requires the following semantics:
24979fb54abSBenny Halevy 		 *
25079fb54abSBenny Halevy 		 * Persistent   | pNFS   | Server REQUIRED | Client Allowed
25179fb54abSBenny Halevy 		 * Reply Cache  | server |                 |
25279fb54abSBenny Halevy 		 * -------------+--------+-----------------+--------------------
25379fb54abSBenny Halevy 		 * no           | no     | EXCLUSIVE4_1    | EXCLUSIVE4_1
25479fb54abSBenny Halevy 		 *              |        |                 | (SHOULD)
25579fb54abSBenny Halevy 		 *              |        | and EXCLUSIVE4  | or EXCLUSIVE4
25679fb54abSBenny Halevy 		 *              |        |                 | (SHOULD NOT)
25779fb54abSBenny Halevy 		 * no           | yes    | EXCLUSIVE4_1    | EXCLUSIVE4_1
25879fb54abSBenny Halevy 		 * yes          | no     | GUARDED4        | GUARDED4
25979fb54abSBenny Halevy 		 * yes          | yes    | GUARDED4        | GUARDED4
26079fb54abSBenny Halevy 		 */
26179fb54abSBenny Halevy 
2621da177e4SLinus Torvalds 		/*
2631da177e4SLinus Torvalds 		 * Note: create modes (UNCHECKED,GUARDED...) are the same
264ac6721a1SMi Jinlong 		 * in NFSv4 as in v3 except EXCLUSIVE4_1.
2651da177e4SLinus Torvalds 		 */
266ac6721a1SMi Jinlong 		status = do_nfsd_create(rqstp, current_fh, open->op_fname.data,
2671da177e4SLinus Torvalds 					open->op_fname.len, &open->op_iattr,
268c0e6bee4SJ. Bruce Fields 					*resfh, open->op_createmode,
269749997e5SJeff Layton 					(u32 *)open->op_verf.data,
270856121b2SJ. Bruce Fields 					&open->op_truncate, &open->op_created);
271749997e5SJeff Layton 
27218032ca0SDavid Quigley 		if (!status && open->op_label.len)
273c0e6bee4SJ. Bruce Fields 			nfsd4_security_inode_setsecctx(*resfh, &open->op_label, open->op_bmval);
27418032ca0SDavid Quigley 
27599f88726SJ. Bruce Fields 		/*
27699f88726SJ. Bruce Fields 		 * Following rfc 3530 14.2.16, use the returned bitmask
27799f88726SJ. Bruce Fields 		 * to indicate which attributes we used to store the
27899f88726SJ. Bruce Fields 		 * verifier:
279749997e5SJeff Layton 		 */
280749997e5SJeff Layton 		if (open->op_createmode == NFS4_CREATE_EXCLUSIVE && status == 0)
28199f88726SJ. Bruce Fields 			open->op_bmval[1] = (FATTR4_WORD1_TIME_ACCESS |
282749997e5SJeff Layton 							FATTR4_WORD1_TIME_MODIFY);
2834335723eSJ. Bruce Fields 	} else
2844335723eSJ. Bruce Fields 		/*
2854335723eSJ. Bruce Fields 		 * Note this may exit with the parent still locked.
2864335723eSJ. Bruce Fields 		 * We will hold the lock until nfsd4_open's final
2874335723eSJ. Bruce Fields 		 * lookup, to prevent renames or unlinks until we've had
2884335723eSJ. Bruce Fields 		 * a chance to an acquire a delegation if appropriate.
2894335723eSJ. Bruce Fields 		 */
2901da177e4SLinus Torvalds 		status = nfsd_lookup(rqstp, current_fh,
291c0e6bee4SJ. Bruce Fields 				     open->op_fname.data, open->op_fname.len, *resfh);
292aadab6c6SJ. Bruce Fields 	if (status)
293aadab6c6SJ. Bruce Fields 		goto out;
294c0e6bee4SJ. Bruce Fields 	status = nfsd_check_obj_isreg(*resfh);
295af85852dSJ. Bruce Fields 	if (status)
296af85852dSJ. Bruce Fields 		goto out;
2971da177e4SLinus Torvalds 
2989208faf2SYu Zhiguo 	if (is_create_with_attrs(open) && open->op_acl != NULL)
299c0e6bee4SJ. Bruce Fields 		do_set_nfs4_acl(rqstp, *resfh, open->op_acl, open->op_bmval);
3009208faf2SYu Zhiguo 
301c0e6bee4SJ. Bruce Fields 	nfsd4_set_open_owner_reply_cache(cstate, open, *resfh);
3027007c90fSNeil Brown 	accmode = NFSD_MAY_NOP;
30389f6c336SJ. Bruce Fields 	if (open->op_created ||
30489f6c336SJ. Bruce Fields 			open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
3057007c90fSNeil Brown 		accmode |= NFSD_MAY_OWNER_OVERRIDE;
306c0e6bee4SJ. Bruce Fields 	status = do_open_permission(rqstp, *resfh, open, accmode);
30741fd1e42SJ. Bruce Fields 	set_change_info(&open->op_cinfo, current_fh);
308af85852dSJ. Bruce Fields out:
3091da177e4SLinus Torvalds 	return status;
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
312b37ad28bSAl Viro static __be32
313bbc9c36cSJ. Bruce Fields do_open_fhandle(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
3141da177e4SLinus Torvalds {
315bbc9c36cSJ. Bruce Fields 	struct svc_fh *current_fh = &cstate->current_fh;
316b37ad28bSAl Viro 	__be32 status;
3179f415eb2SJ. Bruce Fields 	int accmode = 0;
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	/* We don't know the target directory, and therefore can not
3201da177e4SLinus Torvalds 	* set the change info
3211da177e4SLinus Torvalds 	*/
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 	memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
3241da177e4SLinus Torvalds 
325bbc9c36cSJ. Bruce Fields 	nfsd4_set_open_owner_reply_cache(cstate, open, current_fh);
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds 	open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
3281da177e4SLinus Torvalds 		(open->op_iattr.ia_size == 0);
3299f415eb2SJ. Bruce Fields 	/*
3309f415eb2SJ. Bruce Fields 	 * In the delegation case, the client is telling us about an
3319f415eb2SJ. Bruce Fields 	 * open that it *already* performed locally, some time ago.  We
3329f415eb2SJ. Bruce Fields 	 * should let it succeed now if possible.
3339f415eb2SJ. Bruce Fields 	 *
3349f415eb2SJ. Bruce Fields 	 * In the case of a CLAIM_FH open, on the other hand, the client
3359f415eb2SJ. Bruce Fields 	 * may be counting on us to enforce permissions (the Linux 4.1
3369f415eb2SJ. Bruce Fields 	 * client uses this for normal opens, for example).
3379f415eb2SJ. Bruce Fields 	 */
3389f415eb2SJ. Bruce Fields 	if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH)
3399f415eb2SJ. Bruce Fields 		accmode = NFSD_MAY_OWNER_OVERRIDE;
3401da177e4SLinus Torvalds 
3419f415eb2SJ. Bruce Fields 	status = do_open_permission(rqstp, current_fh, open, accmode);
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	return status;
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds 
34660adfc50SAndy Adamson static void
34760adfc50SAndy Adamson copy_clientid(clientid_t *clid, struct nfsd4_session *session)
34860adfc50SAndy Adamson {
34960adfc50SAndy Adamson 	struct nfsd4_sessionid *sid =
35060adfc50SAndy Adamson 			(struct nfsd4_sessionid *)session->se_sessionid.data;
35160adfc50SAndy Adamson 
35260adfc50SAndy Adamson 	clid->cl_boot = sid->clientid.cl_boot;
35360adfc50SAndy Adamson 	clid->cl_id = sid->clientid.cl_id;
35460adfc50SAndy Adamson }
3551da177e4SLinus Torvalds 
3567191155bSJ.Bruce Fields static __be32
357ca364317SJ.Bruce Fields nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
358a4f1706aSJ.Bruce Fields 	   struct nfsd4_open *open)
3591da177e4SLinus Torvalds {
360b37ad28bSAl Viro 	__be32 status;
361c0e6bee4SJ. Bruce Fields 	struct svc_fh *resfh = NULL;
3626668958fSAndy Adamson 	struct nfsd4_compoundres *resp;
3633320fef1SStanislav Kinsbursky 	struct net *net = SVC_NET(rqstp);
3643320fef1SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3656668958fSAndy Adamson 
366fe0750e5SJ. Bruce Fields 	dprintk("NFSD: nfsd4_open filename %.*s op_openowner %p\n",
3671da177e4SLinus Torvalds 		(int)open->op_fname.len, open->op_fname.data,
368fe0750e5SJ. Bruce Fields 		open->op_openowner);
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	/* This check required by spec. */
3711da177e4SLinus Torvalds 	if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
3721da177e4SLinus Torvalds 		return nfserr_inval;
3731da177e4SLinus Torvalds 
374856121b2SJ. Bruce Fields 	open->op_created = 0;
375ab1350b2SMi Jinlong 	/*
376ab1350b2SMi Jinlong 	 * RFC5661 18.51.3
377ab1350b2SMi Jinlong 	 * Before RECLAIM_COMPLETE done, server should deny new lock
378ab1350b2SMi Jinlong 	 */
379ab1350b2SMi Jinlong 	if (nfsd4_has_session(cstate) &&
380a52d726bSJeff Layton 	    !test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
381a52d726bSJeff Layton 		      &cstate->session->se_client->cl_flags) &&
382ab1350b2SMi Jinlong 	    open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
383ab1350b2SMi Jinlong 		return nfserr_grace;
384ab1350b2SMi Jinlong 
38560adfc50SAndy Adamson 	if (nfsd4_has_session(cstate))
38660adfc50SAndy Adamson 		copy_clientid(&open->op_clientid, cstate->session);
38760adfc50SAndy Adamson 
3881da177e4SLinus Torvalds 	/* check seqid for replay. set nfs4_owner */
3896668958fSAndy Adamson 	resp = rqstp->rq_resp;
3903320fef1SStanislav Kinsbursky 	status = nfsd4_process_open1(&resp->cstate, open, nn);
391a90b061cSAl Viro 	if (status == nfserr_replay_me) {
392fe0750e5SJ. Bruce Fields 		struct nfs4_replay *rp = &open->op_openowner->oo_owner.so_replay;
393ca364317SJ.Bruce Fields 		fh_put(&cstate->current_fh);
394a4773c08SJ. Bruce Fields 		fh_copy_shallow(&cstate->current_fh.fh_handle,
395a4773c08SJ. Bruce Fields 				&rp->rp_openfh);
3968837abcaSMiklos Szeredi 		status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
3971da177e4SLinus Torvalds 		if (status)
3981da177e4SLinus Torvalds 			dprintk("nfsd4_open: replay failed"
3991da177e4SLinus Torvalds 				" restoring previous filehandle\n");
4001da177e4SLinus Torvalds 		else
401a90b061cSAl Viro 			status = nfserr_replay_me;
4021da177e4SLinus Torvalds 	}
4031da177e4SLinus Torvalds 	if (status)
4041da177e4SLinus Torvalds 		goto out;
4059d313b17SJ. Bruce Fields 	if (open->op_xdr_error) {
4069d313b17SJ. Bruce Fields 		status = open->op_xdr_error;
4079d313b17SJ. Bruce Fields 		goto out;
4089d313b17SJ. Bruce Fields 	}
409fb553c0fSJ. Bruce Fields 
4103c8e0316SYu Zhiguo 	status = nfsd4_check_open_attributes(rqstp, cstate, open);
4113c8e0316SYu Zhiguo 	if (status)
4123c8e0316SYu Zhiguo 		goto out;
4133c8e0316SYu Zhiguo 
414fb553c0fSJ. Bruce Fields 	/* Openowner is now set, so sequence id will get bumped.  Now we need
415fb553c0fSJ. Bruce Fields 	 * these checks before we do any creates: */
416cbd0d51aSJ. Bruce Fields 	status = nfserr_grace;
4173320fef1SStanislav Kinsbursky 	if (locks_in_grace(net) && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
418cbd0d51aSJ. Bruce Fields 		goto out;
419cbd0d51aSJ. Bruce Fields 	status = nfserr_no_grace;
4203320fef1SStanislav Kinsbursky 	if (!locks_in_grace(net) && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
421cbd0d51aSJ. Bruce Fields 		goto out;
422fb553c0fSJ. Bruce Fields 
4231da177e4SLinus Torvalds 	switch (open->op_claim_type) {
4240dd3c192SNeilBrown 		case NFS4_OPEN_CLAIM_DELEGATE_CUR:
4251da177e4SLinus Torvalds 		case NFS4_OPEN_CLAIM_NULL:
426c0e6bee4SJ. Bruce Fields 			status = do_open_lookup(rqstp, cstate, open, &resfh);
4271da177e4SLinus Torvalds 			if (status)
4281da177e4SLinus Torvalds 				goto out;
4291da177e4SLinus Torvalds 			break;
4301da177e4SLinus Torvalds 		case NFS4_OPEN_CLAIM_PREVIOUS:
4313320fef1SStanislav Kinsbursky 			status = nfs4_check_open_reclaim(&open->op_clientid,
4320fe492dbSTrond Myklebust 							 cstate, nn);
4330cf99b91SMi Jinlong 			if (status)
4340cf99b91SMi Jinlong 				goto out;
435ba5378b6SJeff Layton 			open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4368b289b2cSJ. Bruce Fields 		case NFS4_OPEN_CLAIM_FH:
4378b289b2cSJ. Bruce Fields 		case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
438bbc9c36cSJ. Bruce Fields 			status = do_open_fhandle(rqstp, cstate, open);
4391da177e4SLinus Torvalds 			if (status)
4401da177e4SLinus Torvalds 				goto out;
441c0e6bee4SJ. Bruce Fields 			resfh = &cstate->current_fh;
4421da177e4SLinus Torvalds 			break;
4438b289b2cSJ. Bruce Fields 		case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
4441da177e4SLinus Torvalds              	case NFS4_OPEN_CLAIM_DELEGATE_PREV:
4452fdada03SJ. Bruce Fields 			dprintk("NFSD: unsupported OPEN claim type %d\n",
4461da177e4SLinus Torvalds 				open->op_claim_type);
4471da177e4SLinus Torvalds 			status = nfserr_notsupp;
4481da177e4SLinus Torvalds 			goto out;
4491da177e4SLinus Torvalds 		default:
4502fdada03SJ. Bruce Fields 			dprintk("NFSD: Invalid OPEN claim type %d\n",
4511da177e4SLinus Torvalds 				open->op_claim_type);
4521da177e4SLinus Torvalds 			status = nfserr_inval;
4531da177e4SLinus Torvalds 			goto out;
4541da177e4SLinus Torvalds 	}
4551da177e4SLinus Torvalds 	/*
4561da177e4SLinus Torvalds 	 * nfsd4_process_open2() does the actual opening of the file.  If
4571da177e4SLinus Torvalds 	 * successful, it (1) truncates the file if open->op_truncate was
4581da177e4SLinus Torvalds 	 * set, (2) sets open->op_stateid, (3) sets open->op_delegation.
4591da177e4SLinus Torvalds 	 */
460c0e6bee4SJ. Bruce Fields 	status = nfsd4_process_open2(rqstp, resfh, open);
461b3fbfe0eSJeff Layton 	WARN(status && open->op_created,
462b3fbfe0eSJeff Layton 	     "nfsd4_process_open2 failed to open newly-created file! status=%u\n",
463b3fbfe0eSJeff Layton 	     be32_to_cpu(status));
4641da177e4SLinus Torvalds out:
465c0e6bee4SJ. Bruce Fields 	if (resfh && resfh != &cstate->current_fh) {
466c0e6bee4SJ. Bruce Fields 		fh_dup2(&cstate->current_fh, resfh);
467c0e6bee4SJ. Bruce Fields 		fh_put(resfh);
468c0e6bee4SJ. Bruce Fields 		kfree(resfh);
469c0e6bee4SJ. Bruce Fields 	}
47058fb12e6SJeff Layton 	nfsd4_cleanup_open_state(cstate, open, status);
4719411b1d4SJ. Bruce Fields 	nfsd4_bump_seqid(cstate, status);
4721da177e4SLinus Torvalds 	return status;
4731da177e4SLinus Torvalds }
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds /*
4769d313b17SJ. Bruce Fields  * OPEN is the only seqid-mutating operation whose decoding can fail
4779d313b17SJ. Bruce Fields  * with a seqid-mutating error (specifically, decoding of user names in
4789d313b17SJ. Bruce Fields  * the attributes).  Therefore we have to do some processing to look up
4799d313b17SJ. Bruce Fields  * the stateowner so that we can bump the seqid.
4809d313b17SJ. Bruce Fields  */
4819d313b17SJ. Bruce Fields static __be32 nfsd4_open_omfg(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_op *op)
4829d313b17SJ. Bruce Fields {
4839d313b17SJ. Bruce Fields 	struct nfsd4_open *open = (struct nfsd4_open *)&op->u;
4849d313b17SJ. Bruce Fields 
4859d313b17SJ. Bruce Fields 	if (!seqid_mutating_err(ntohl(op->status)))
4869d313b17SJ. Bruce Fields 		return op->status;
4879d313b17SJ. Bruce Fields 	if (nfsd4_has_session(cstate))
4889d313b17SJ. Bruce Fields 		return op->status;
4899d313b17SJ. Bruce Fields 	open->op_xdr_error = op->status;
4909d313b17SJ. Bruce Fields 	return nfsd4_open(rqstp, cstate, open);
4919d313b17SJ. Bruce Fields }
4929d313b17SJ. Bruce Fields 
4939d313b17SJ. Bruce Fields /*
4941da177e4SLinus Torvalds  * filehandle-manipulating ops.
4951da177e4SLinus Torvalds  */
4967191155bSJ.Bruce Fields static __be32
497b591480bSJ.Bruce Fields nfsd4_getfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
498b591480bSJ.Bruce Fields 	    struct svc_fh **getfh)
4991da177e4SLinus Torvalds {
500ca364317SJ.Bruce Fields 	if (!cstate->current_fh.fh_dentry)
5011da177e4SLinus Torvalds 		return nfserr_nofilehandle;
5021da177e4SLinus Torvalds 
503ca364317SJ.Bruce Fields 	*getfh = &cstate->current_fh;
5041da177e4SLinus Torvalds 	return nfs_ok;
5051da177e4SLinus Torvalds }
5061da177e4SLinus Torvalds 
5077191155bSJ.Bruce Fields static __be32
508ca364317SJ.Bruce Fields nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
509ca364317SJ.Bruce Fields 	    struct nfsd4_putfh *putfh)
5101da177e4SLinus Torvalds {
511ca364317SJ.Bruce Fields 	fh_put(&cstate->current_fh);
512ca364317SJ.Bruce Fields 	cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
513ca364317SJ.Bruce Fields 	memcpy(&cstate->current_fh.fh_handle.fh_base, putfh->pf_fhval,
514ca364317SJ.Bruce Fields 	       putfh->pf_fhlen);
51568d93184SJ. Bruce Fields 	return fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_BYPASS_GSS);
5161da177e4SLinus Torvalds }
5171da177e4SLinus Torvalds 
5187191155bSJ.Bruce Fields static __be32
519b591480bSJ.Bruce Fields nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
520b591480bSJ.Bruce Fields 		void *arg)
5211da177e4SLinus Torvalds {
522b37ad28bSAl Viro 	__be32 status;
5231da177e4SLinus Torvalds 
524ca364317SJ.Bruce Fields 	fh_put(&cstate->current_fh);
525df547efbSJ. Bruce Fields 	status = exp_pseudoroot(rqstp, &cstate->current_fh);
5261da177e4SLinus Torvalds 	return status;
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds 
5297191155bSJ.Bruce Fields static __be32
530b591480bSJ.Bruce Fields nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
531b591480bSJ.Bruce Fields 		void *arg)
5321da177e4SLinus Torvalds {
533ca364317SJ.Bruce Fields 	if (!cstate->save_fh.fh_dentry)
5341da177e4SLinus Torvalds 		return nfserr_restorefh;
5351da177e4SLinus Torvalds 
536ca364317SJ.Bruce Fields 	fh_dup2(&cstate->current_fh, &cstate->save_fh);
53737c593c5STigran Mkrtchyan 	if (HAS_STATE_ID(cstate, SAVED_STATE_ID_FLAG)) {
53837c593c5STigran Mkrtchyan 		memcpy(&cstate->current_stateid, &cstate->save_stateid, sizeof(stateid_t));
53937c593c5STigran Mkrtchyan 		SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
54037c593c5STigran Mkrtchyan 	}
5411da177e4SLinus Torvalds 	return nfs_ok;
5421da177e4SLinus Torvalds }
5431da177e4SLinus Torvalds 
5447191155bSJ.Bruce Fields static __be32
545b591480bSJ.Bruce Fields nfsd4_savefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
546b591480bSJ.Bruce Fields 	     void *arg)
5471da177e4SLinus Torvalds {
548ca364317SJ.Bruce Fields 	if (!cstate->current_fh.fh_dentry)
5491da177e4SLinus Torvalds 		return nfserr_nofilehandle;
5501da177e4SLinus Torvalds 
551ca364317SJ.Bruce Fields 	fh_dup2(&cstate->save_fh, &cstate->current_fh);
55237c593c5STigran Mkrtchyan 	if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG)) {
55337c593c5STigran Mkrtchyan 		memcpy(&cstate->save_stateid, &cstate->current_stateid, sizeof(stateid_t));
55437c593c5STigran Mkrtchyan 		SET_STATE_ID(cstate, SAVED_STATE_ID_FLAG);
55537c593c5STigran Mkrtchyan 	}
5561da177e4SLinus Torvalds 	return nfs_ok;
5571da177e4SLinus Torvalds }
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds /*
5601da177e4SLinus Torvalds  * misc nfsv4 ops
5611da177e4SLinus Torvalds  */
5627191155bSJ.Bruce Fields static __be32
563ca364317SJ.Bruce Fields nfsd4_access(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
564ca364317SJ.Bruce Fields 	     struct nfsd4_access *access)
5651da177e4SLinus Torvalds {
5661da177e4SLinus Torvalds 	if (access->ac_req_access & ~NFS3_ACCESS_FULL)
5671da177e4SLinus Torvalds 		return nfserr_inval;
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 	access->ac_resp_access = access->ac_req_access;
570ca364317SJ.Bruce Fields 	return nfsd_access(rqstp, &cstate->current_fh, &access->ac_resp_access,
571ca364317SJ.Bruce Fields 			   &access->ac_supported);
5721da177e4SLinus Torvalds }
5731da177e4SLinus Torvalds 
574b9c0ef85SStanislav Kinsbursky static void gen_boot_verifier(nfs4_verifier *verifier, struct net *net)
575ab4684d1SChuck Lever {
576ab4684d1SChuck Lever 	__be32 verf[2];
577b9c0ef85SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
578ab4684d1SChuck Lever 
579f419992cSJeff Layton 	/*
580f419992cSJeff Layton 	 * This is opaque to client, so no need to byte-swap. Use
581f419992cSJeff Layton 	 * __force to keep sparse happy
582f419992cSJeff Layton 	 */
583f419992cSJeff Layton 	verf[0] = (__force __be32)nn->nfssvc_boot.tv_sec;
584f419992cSJeff Layton 	verf[1] = (__force __be32)nn->nfssvc_boot.tv_usec;
585ab4684d1SChuck Lever 	memcpy(verifier->data, verf, sizeof(verifier->data));
586ab4684d1SChuck Lever }
587ab4684d1SChuck Lever 
5887191155bSJ.Bruce Fields static __be32
589ca364317SJ.Bruce Fields nfsd4_commit(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
590ca364317SJ.Bruce Fields 	     struct nfsd4_commit *commit)
5911da177e4SLinus Torvalds {
592b9c0ef85SStanislav Kinsbursky 	gen_boot_verifier(&commit->co_verf, SVC_NET(rqstp));
59375c096f7SJ. Bruce Fields 	return nfsd_commit(rqstp, &cstate->current_fh, commit->co_offset,
594ca364317SJ.Bruce Fields 			     commit->co_count);
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds 
597b37ad28bSAl Viro static __be32
598ca364317SJ.Bruce Fields nfsd4_create(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
599ca364317SJ.Bruce Fields 	     struct nfsd4_create *create)
6001da177e4SLinus Torvalds {
6011da177e4SLinus Torvalds 	struct svc_fh resfh;
602b37ad28bSAl Viro 	__be32 status;
6031da177e4SLinus Torvalds 	dev_t rdev;
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	fh_init(&resfh, NFS4_FHSIZE);
6061da177e4SLinus Torvalds 
6078837abcaSMiklos Szeredi 	status = fh_verify(rqstp, &cstate->current_fh, S_IFDIR,
6088837abcaSMiklos Szeredi 			   NFSD_MAY_CREATE);
6091da177e4SLinus Torvalds 	if (status)
6101da177e4SLinus Torvalds 		return status;
6111da177e4SLinus Torvalds 
6123c8e0316SYu Zhiguo 	status = check_attr_support(rqstp, cstate, create->cr_bmval,
6133c8e0316SYu Zhiguo 				    nfsd_attrmask);
6143c8e0316SYu Zhiguo 	if (status)
6153c8e0316SYu Zhiguo 		return status;
6163c8e0316SYu Zhiguo 
6171da177e4SLinus Torvalds 	switch (create->cr_type) {
6181da177e4SLinus Torvalds 	case NF4LNK:
619ca364317SJ.Bruce Fields 		status = nfsd_symlink(rqstp, &cstate->current_fh,
620ca364317SJ.Bruce Fields 				      create->cr_name, create->cr_namelen,
6211e444f5bSKinglong Mee 				      create->cr_data, &resfh);
6221da177e4SLinus Torvalds 		break;
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	case NF4BLK:
6251da177e4SLinus Torvalds 		rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
6261da177e4SLinus Torvalds 		if (MAJOR(rdev) != create->cr_specdata1 ||
6271da177e4SLinus Torvalds 		    MINOR(rdev) != create->cr_specdata2)
6281da177e4SLinus Torvalds 			return nfserr_inval;
629ca364317SJ.Bruce Fields 		status = nfsd_create(rqstp, &cstate->current_fh,
630ca364317SJ.Bruce Fields 				     create->cr_name, create->cr_namelen,
631ca364317SJ.Bruce Fields 				     &create->cr_iattr, S_IFBLK, rdev, &resfh);
6321da177e4SLinus Torvalds 		break;
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	case NF4CHR:
6351da177e4SLinus Torvalds 		rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
6361da177e4SLinus Torvalds 		if (MAJOR(rdev) != create->cr_specdata1 ||
6371da177e4SLinus Torvalds 		    MINOR(rdev) != create->cr_specdata2)
6381da177e4SLinus Torvalds 			return nfserr_inval;
639ca364317SJ.Bruce Fields 		status = nfsd_create(rqstp, &cstate->current_fh,
640ca364317SJ.Bruce Fields 				     create->cr_name, create->cr_namelen,
641ca364317SJ.Bruce Fields 				     &create->cr_iattr,S_IFCHR, rdev, &resfh);
6421da177e4SLinus Torvalds 		break;
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	case NF4SOCK:
645ca364317SJ.Bruce Fields 		status = nfsd_create(rqstp, &cstate->current_fh,
646ca364317SJ.Bruce Fields 				     create->cr_name, create->cr_namelen,
647ca364317SJ.Bruce Fields 				     &create->cr_iattr, S_IFSOCK, 0, &resfh);
6481da177e4SLinus Torvalds 		break;
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	case NF4FIFO:
651ca364317SJ.Bruce Fields 		status = nfsd_create(rqstp, &cstate->current_fh,
652ca364317SJ.Bruce Fields 				     create->cr_name, create->cr_namelen,
653ca364317SJ.Bruce Fields 				     &create->cr_iattr, S_IFIFO, 0, &resfh);
6541da177e4SLinus Torvalds 		break;
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	case NF4DIR:
6571da177e4SLinus Torvalds 		create->cr_iattr.ia_valid &= ~ATTR_SIZE;
658ca364317SJ.Bruce Fields 		status = nfsd_create(rqstp, &cstate->current_fh,
659ca364317SJ.Bruce Fields 				     create->cr_name, create->cr_namelen,
660ca364317SJ.Bruce Fields 				     &create->cr_iattr, S_IFDIR, 0, &resfh);
6611da177e4SLinus Torvalds 		break;
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 	default:
6641da177e4SLinus Torvalds 		status = nfserr_badtype;
6651da177e4SLinus Torvalds 	}
6661da177e4SLinus Torvalds 
6679208faf2SYu Zhiguo 	if (status)
6689208faf2SYu Zhiguo 		goto out;
6699208faf2SYu Zhiguo 
67018032ca0SDavid Quigley 	if (create->cr_label.len)
67118032ca0SDavid Quigley 		nfsd4_security_inode_setsecctx(&resfh, &create->cr_label, create->cr_bmval);
67218032ca0SDavid Quigley 
6739208faf2SYu Zhiguo 	if (create->cr_acl != NULL)
6749208faf2SYu Zhiguo 		do_set_nfs4_acl(rqstp, &resfh, create->cr_acl,
6759208faf2SYu Zhiguo 				create->cr_bmval);
6769208faf2SYu Zhiguo 
677ca364317SJ.Bruce Fields 	fh_unlock(&cstate->current_fh);
678ca364317SJ.Bruce Fields 	set_change_info(&create->cr_cinfo, &cstate->current_fh);
679ca364317SJ.Bruce Fields 	fh_dup2(&cstate->current_fh, &resfh);
6809208faf2SYu Zhiguo out:
6811da177e4SLinus Torvalds 	fh_put(&resfh);
6821da177e4SLinus Torvalds 	return status;
6831da177e4SLinus Torvalds }
6841da177e4SLinus Torvalds 
6857191155bSJ.Bruce Fields static __be32
686ca364317SJ.Bruce Fields nfsd4_getattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
687ca364317SJ.Bruce Fields 	      struct nfsd4_getattr *getattr)
6881da177e4SLinus Torvalds {
689b37ad28bSAl Viro 	__be32 status;
6901da177e4SLinus Torvalds 
6918837abcaSMiklos Szeredi 	status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
6921da177e4SLinus Torvalds 	if (status)
6931da177e4SLinus Torvalds 		return status;
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
6961da177e4SLinus Torvalds 		return nfserr_inval;
6971da177e4SLinus Torvalds 
6987e705706SAndy Adamson 	getattr->ga_bmval[0] &= nfsd_suppattrs0(cstate->minorversion);
6997e705706SAndy Adamson 	getattr->ga_bmval[1] &= nfsd_suppattrs1(cstate->minorversion);
7007e705706SAndy Adamson 	getattr->ga_bmval[2] &= nfsd_suppattrs2(cstate->minorversion);
7011da177e4SLinus Torvalds 
702ca364317SJ.Bruce Fields 	getattr->ga_fhp = &cstate->current_fh;
7031da177e4SLinus Torvalds 	return nfs_ok;
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds 
7067191155bSJ.Bruce Fields static __be32
707ca364317SJ.Bruce Fields nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
708ca364317SJ.Bruce Fields 	   struct nfsd4_link *link)
7091da177e4SLinus Torvalds {
710b37ad28bSAl Viro 	__be32 status = nfserr_nofilehandle;
7111da177e4SLinus Torvalds 
712ca364317SJ.Bruce Fields 	if (!cstate->save_fh.fh_dentry)
7131da177e4SLinus Torvalds 		return status;
714ca364317SJ.Bruce Fields 	status = nfsd_link(rqstp, &cstate->current_fh,
715ca364317SJ.Bruce Fields 			   link->li_name, link->li_namelen, &cstate->save_fh);
7161da177e4SLinus Torvalds 	if (!status)
717ca364317SJ.Bruce Fields 		set_change_info(&link->li_cinfo, &cstate->current_fh);
7181da177e4SLinus Torvalds 	return status;
7191da177e4SLinus Torvalds }
7201da177e4SLinus Torvalds 
7210ff7ab46SJ. Bruce Fields static __be32 nfsd4_do_lookupp(struct svc_rqst *rqstp, struct svc_fh *fh)
7221da177e4SLinus Torvalds {
7231da177e4SLinus Torvalds 	struct svc_fh tmp_fh;
724b37ad28bSAl Viro 	__be32 ret;
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	fh_init(&tmp_fh, NFS4_FHSIZE);
727df547efbSJ. Bruce Fields 	ret = exp_pseudoroot(rqstp, &tmp_fh);
728df547efbSJ. Bruce Fields 	if (ret)
7291da177e4SLinus Torvalds 		return ret;
7300ff7ab46SJ. Bruce Fields 	if (tmp_fh.fh_dentry == fh->fh_dentry) {
7311da177e4SLinus Torvalds 		fh_put(&tmp_fh);
7321da177e4SLinus Torvalds 		return nfserr_noent;
7331da177e4SLinus Torvalds 	}
7341da177e4SLinus Torvalds 	fh_put(&tmp_fh);
7350ff7ab46SJ. Bruce Fields 	return nfsd_lookup(rqstp, fh, "..", 2, fh);
7360ff7ab46SJ. Bruce Fields }
7370ff7ab46SJ. Bruce Fields 
7380ff7ab46SJ. Bruce Fields static __be32
7390ff7ab46SJ. Bruce Fields nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7400ff7ab46SJ. Bruce Fields 	      void *arg)
7410ff7ab46SJ. Bruce Fields {
7420ff7ab46SJ. Bruce Fields 	return nfsd4_do_lookupp(rqstp, &cstate->current_fh);
7431da177e4SLinus Torvalds }
7441da177e4SLinus Torvalds 
7457191155bSJ.Bruce Fields static __be32
746ca364317SJ.Bruce Fields nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
747ca364317SJ.Bruce Fields 	     struct nfsd4_lookup *lookup)
7481da177e4SLinus Torvalds {
749ca364317SJ.Bruce Fields 	return nfsd_lookup(rqstp, &cstate->current_fh,
750ca364317SJ.Bruce Fields 			   lookup->lo_name, lookup->lo_len,
751ca364317SJ.Bruce Fields 			   &cstate->current_fh);
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds 
7547191155bSJ.Bruce Fields static __be32
755ca364317SJ.Bruce Fields nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
756ca364317SJ.Bruce Fields 	   struct nfsd4_read *read)
7571da177e4SLinus Torvalds {
758b37ad28bSAl Viro 	__be32 status;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	/* no need to check permission - this will be done in nfsd_read() */
7611da177e4SLinus Torvalds 
7627e06b7f9SNeilBrown 	read->rd_filp = NULL;
7631da177e4SLinus Torvalds 	if (read->rd_offset >= OFFSET_MAX)
7641da177e4SLinus Torvalds 		return nfserr_inval;
7651da177e4SLinus Torvalds 
7669b3234b9SJ. Bruce Fields 	/*
7679b3234b9SJ. Bruce Fields 	 * If we do a zero copy read, then a client will see read data
7689b3234b9SJ. Bruce Fields 	 * that reflects the state of the file *after* performing the
7699b3234b9SJ. Bruce Fields 	 * following compound.
7709b3234b9SJ. Bruce Fields 	 *
7719b3234b9SJ. Bruce Fields 	 * To ensure proper ordering, we therefore turn off zero copy if
7729b3234b9SJ. Bruce Fields 	 * the client wants us to do more in this compound:
7739b3234b9SJ. Bruce Fields 	 */
7749b3234b9SJ. Bruce Fields 	if (!nfsd4_last_compound_op(rqstp))
7759b3234b9SJ. Bruce Fields 		rqstp->rq_splice_ok = false;
7769b3234b9SJ. Bruce Fields 
7771da177e4SLinus Torvalds 	/* check stateid */
7785ccb0066SStanislav Kinsbursky 	if ((status = nfs4_preprocess_stateid_op(SVC_NET(rqstp),
7795ccb0066SStanislav Kinsbursky 						 cstate, &read->rd_stateid,
7806150ef0dSJ. Bruce Fields 						 RD_STATE, &read->rd_filp))) {
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 
7917191155bSJ.Bruce Fields static __be32
792ca364317SJ.Bruce Fields nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
793ca364317SJ.Bruce Fields 	      struct nfsd4_readdir *readdir)
7941da177e4SLinus Torvalds {
7951da177e4SLinus Torvalds 	u64 cookie = readdir->rd_cookie;
7961da177e4SLinus Torvalds 	static const nfs4_verifier zeroverf;
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 	/* no need to check permission - this will be done in nfsd_readdir() */
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 	if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
8011da177e4SLinus Torvalds 		return nfserr_inval;
8021da177e4SLinus Torvalds 
8037e705706SAndy Adamson 	readdir->rd_bmval[0] &= nfsd_suppattrs0(cstate->minorversion);
8047e705706SAndy Adamson 	readdir->rd_bmval[1] &= nfsd_suppattrs1(cstate->minorversion);
8057e705706SAndy Adamson 	readdir->rd_bmval[2] &= nfsd_suppattrs2(cstate->minorversion);
8061da177e4SLinus Torvalds 
807832023bfSBernd Schubert 	if ((cookie == 1) || (cookie == 2) ||
8081da177e4SLinus Torvalds 	    (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
8091da177e4SLinus Torvalds 		return nfserr_bad_cookie;
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 	readdir->rd_rqstp = rqstp;
812ca364317SJ.Bruce Fields 	readdir->rd_fhp = &cstate->current_fh;
8131da177e4SLinus Torvalds 	return nfs_ok;
8141da177e4SLinus Torvalds }
8151da177e4SLinus Torvalds 
8167191155bSJ.Bruce Fields static __be32
817ca364317SJ.Bruce Fields nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
818ca364317SJ.Bruce Fields 	       struct nfsd4_readlink *readlink)
8191da177e4SLinus Torvalds {
8201da177e4SLinus Torvalds 	readlink->rl_rqstp = rqstp;
821ca364317SJ.Bruce Fields 	readlink->rl_fhp = &cstate->current_fh;
8221da177e4SLinus Torvalds 	return nfs_ok;
8231da177e4SLinus Torvalds }
8241da177e4SLinus Torvalds 
8257191155bSJ.Bruce Fields static __be32
826ca364317SJ.Bruce Fields nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
827ca364317SJ.Bruce Fields 	     struct nfsd4_remove *remove)
8281da177e4SLinus Torvalds {
829b37ad28bSAl Viro 	__be32 status;
8301da177e4SLinus Torvalds 
8315ccb0066SStanislav Kinsbursky 	if (locks_in_grace(SVC_NET(rqstp)))
832c815afc7SNeilBrown 		return nfserr_grace;
833ca364317SJ.Bruce Fields 	status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
834ca364317SJ.Bruce Fields 			     remove->rm_name, remove->rm_namelen);
8351da177e4SLinus Torvalds 	if (!status) {
836ca364317SJ.Bruce Fields 		fh_unlock(&cstate->current_fh);
837ca364317SJ.Bruce Fields 		set_change_info(&remove->rm_cinfo, &cstate->current_fh);
8381da177e4SLinus Torvalds 	}
8391da177e4SLinus Torvalds 	return status;
8401da177e4SLinus Torvalds }
8411da177e4SLinus Torvalds 
8427191155bSJ.Bruce Fields static __be32
843ca364317SJ.Bruce Fields nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
844ca364317SJ.Bruce Fields 	     struct nfsd4_rename *rename)
8451da177e4SLinus Torvalds {
846b37ad28bSAl Viro 	__be32 status = nfserr_nofilehandle;
8471da177e4SLinus Torvalds 
848ca364317SJ.Bruce Fields 	if (!cstate->save_fh.fh_dentry)
8491da177e4SLinus Torvalds 		return status;
8505ccb0066SStanislav Kinsbursky 	if (locks_in_grace(SVC_NET(rqstp)) &&
8515ccb0066SStanislav Kinsbursky 		!(cstate->save_fh.fh_export->ex_flags & NFSEXP_NOSUBTREECHECK))
852c815afc7SNeilBrown 		return nfserr_grace;
853ca364317SJ.Bruce Fields 	status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname,
854ca364317SJ.Bruce Fields 			     rename->rn_snamelen, &cstate->current_fh,
8551da177e4SLinus Torvalds 			     rename->rn_tname, rename->rn_tnamelen);
8562a6cf944SJ. Bruce Fields 	if (status)
8572a6cf944SJ. Bruce Fields 		return status;
858ca364317SJ.Bruce Fields 	set_change_info(&rename->rn_sinfo, &cstate->current_fh);
859ca364317SJ.Bruce Fields 	set_change_info(&rename->rn_tinfo, &cstate->save_fh);
8602a6cf944SJ. Bruce Fields 	return nfs_ok;
8611da177e4SLinus Torvalds }
8621da177e4SLinus Torvalds 
8637191155bSJ.Bruce Fields static __be32
864dcb488a3SAndy Adamson nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
865dcb488a3SAndy Adamson 	      struct nfsd4_secinfo *secinfo)
866dcb488a3SAndy Adamson {
867dcb488a3SAndy Adamson 	struct svc_fh resfh;
868dcb488a3SAndy Adamson 	struct svc_export *exp;
869dcb488a3SAndy Adamson 	struct dentry *dentry;
870dcb488a3SAndy Adamson 	__be32 err;
871dcb488a3SAndy Adamson 
872dcb488a3SAndy Adamson 	fh_init(&resfh, NFS4_FHSIZE);
87329a78a3eSJ. Bruce Fields 	err = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_EXEC);
87429a78a3eSJ. Bruce Fields 	if (err)
87529a78a3eSJ. Bruce Fields 		return err;
876dcb488a3SAndy Adamson 	err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
877dcb488a3SAndy Adamson 				    secinfo->si_name, secinfo->si_namelen,
878dcb488a3SAndy Adamson 				    &exp, &dentry);
879dcb488a3SAndy Adamson 	if (err)
880dcb488a3SAndy Adamson 		return err;
881dcb488a3SAndy Adamson 	if (dentry->d_inode == NULL) {
882dcb488a3SAndy Adamson 		exp_put(exp);
883dcb488a3SAndy Adamson 		err = nfserr_noent;
884dcb488a3SAndy Adamson 	} else
885dcb488a3SAndy Adamson 		secinfo->si_exp = exp;
886dcb488a3SAndy Adamson 	dput(dentry);
88756560b9aSJ. Bruce Fields 	if (cstate->minorversion)
88856560b9aSJ. Bruce Fields 		/* See rfc 5661 section 2.6.3.1.1.8 */
88956560b9aSJ. Bruce Fields 		fh_put(&cstate->current_fh);
890dcb488a3SAndy Adamson 	return err;
891dcb488a3SAndy Adamson }
892dcb488a3SAndy Adamson 
893dcb488a3SAndy Adamson static __be32
89404f4ad16SJ. Bruce Fields nfsd4_secinfo_no_name(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
89504f4ad16SJ. Bruce Fields 	      struct nfsd4_secinfo_no_name *sin)
89604f4ad16SJ. Bruce Fields {
89704f4ad16SJ. Bruce Fields 	__be32 err;
89804f4ad16SJ. Bruce Fields 
89904f4ad16SJ. Bruce Fields 	switch (sin->sin_style) {
90004f4ad16SJ. Bruce Fields 	case NFS4_SECINFO_STYLE4_CURRENT_FH:
90104f4ad16SJ. Bruce Fields 		break;
90204f4ad16SJ. Bruce Fields 	case NFS4_SECINFO_STYLE4_PARENT:
90304f4ad16SJ. Bruce Fields 		err = nfsd4_do_lookupp(rqstp, &cstate->current_fh);
90404f4ad16SJ. Bruce Fields 		if (err)
90504f4ad16SJ. Bruce Fields 			return err;
90604f4ad16SJ. Bruce Fields 		break;
90704f4ad16SJ. Bruce Fields 	default:
90804f4ad16SJ. Bruce Fields 		return nfserr_inval;
90904f4ad16SJ. Bruce Fields 	}
910bf18f163SKinglong Mee 
911bf18f163SKinglong Mee 	sin->sin_exp = exp_get(cstate->current_fh.fh_export);
91204f4ad16SJ. Bruce Fields 	fh_put(&cstate->current_fh);
91304f4ad16SJ. Bruce Fields 	return nfs_ok;
91404f4ad16SJ. Bruce Fields }
91504f4ad16SJ. Bruce Fields 
91604f4ad16SJ. Bruce Fields static __be32
917ca364317SJ.Bruce Fields nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
918ca364317SJ.Bruce Fields 	      struct nfsd4_setattr *setattr)
9191da177e4SLinus Torvalds {
920b37ad28bSAl Viro 	__be32 status = nfs_ok;
92196f6f985SAl Viro 	int err;
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds 	if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
9245ccb0066SStanislav Kinsbursky 		status = nfs4_preprocess_stateid_op(SVC_NET(rqstp), cstate,
9256150ef0dSJ. Bruce Fields 			&setattr->sa_stateid, WR_STATE, NULL);
926375c5547SJ. Bruce Fields 		if (status) {
9273e3b4800SGreg Banks 			dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
928375c5547SJ. Bruce Fields 			return status;
929375c5547SJ. Bruce Fields 		}
9301da177e4SLinus Torvalds 	}
93196f6f985SAl Viro 	err = fh_want_write(&cstate->current_fh);
93296f6f985SAl Viro 	if (err)
93396f6f985SAl Viro 		return nfserrno(err);
9341da177e4SLinus Torvalds 	status = nfs_ok;
9353c8e0316SYu Zhiguo 
9363c8e0316SYu Zhiguo 	status = check_attr_support(rqstp, cstate, setattr->sa_bmval,
9373c8e0316SYu Zhiguo 				    nfsd_attrmask);
9383c8e0316SYu Zhiguo 	if (status)
9393c8e0316SYu Zhiguo 		goto out;
9403c8e0316SYu Zhiguo 
9411da177e4SLinus Torvalds 	if (setattr->sa_acl != NULL)
942ca364317SJ.Bruce Fields 		status = nfsd4_set_nfs4_acl(rqstp, &cstate->current_fh,
943ca364317SJ.Bruce Fields 					    setattr->sa_acl);
9441da177e4SLinus Torvalds 	if (status)
94518f335afSDave Hansen 		goto out;
94618032ca0SDavid Quigley 	if (setattr->sa_label.len)
94718032ca0SDavid Quigley 		status = nfsd4_set_nfs4_label(rqstp, &cstate->current_fh,
94818032ca0SDavid Quigley 				&setattr->sa_label);
94918032ca0SDavid Quigley 	if (status)
95018032ca0SDavid Quigley 		goto out;
951ca364317SJ.Bruce Fields 	status = nfsd_setattr(rqstp, &cstate->current_fh, &setattr->sa_iattr,
9521da177e4SLinus Torvalds 				0, (time_t)0);
95318f335afSDave Hansen out:
954bad0dcffSAl Viro 	fh_drop_write(&cstate->current_fh);
9551da177e4SLinus Torvalds 	return status;
9561da177e4SLinus Torvalds }
9571da177e4SLinus Torvalds 
958ffe1137bSJ. Bruce Fields static int fill_in_write_vector(struct kvec *vec, struct nfsd4_write *write)
959ffe1137bSJ. Bruce Fields {
960ffe1137bSJ. Bruce Fields         int i = 1;
961ffe1137bSJ. Bruce Fields         int buflen = write->wr_buflen;
962ffe1137bSJ. Bruce Fields 
963ffe1137bSJ. Bruce Fields         vec[0].iov_base = write->wr_head.iov_base;
964ffe1137bSJ. Bruce Fields         vec[0].iov_len = min_t(int, buflen, write->wr_head.iov_len);
965ffe1137bSJ. Bruce Fields         buflen -= vec[0].iov_len;
966ffe1137bSJ. Bruce Fields 
967ffe1137bSJ. Bruce Fields         while (buflen) {
968ffe1137bSJ. Bruce Fields                 vec[i].iov_base = page_address(write->wr_pagelist[i - 1]);
969ffe1137bSJ. Bruce Fields                 vec[i].iov_len = min_t(int, PAGE_SIZE, buflen);
970ffe1137bSJ. Bruce Fields                 buflen -= vec[i].iov_len;
971ffe1137bSJ. Bruce Fields                 i++;
972ffe1137bSJ. Bruce Fields         }
973ffe1137bSJ. Bruce Fields         return i;
974ffe1137bSJ. Bruce Fields }
975ffe1137bSJ. Bruce Fields 
9767191155bSJ.Bruce Fields static __be32
977ca364317SJ.Bruce Fields nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
978ca364317SJ.Bruce Fields 	    struct nfsd4_write *write)
9791da177e4SLinus Torvalds {
9801da177e4SLinus Torvalds 	stateid_t *stateid = &write->wr_stateid;
9811da177e4SLinus Torvalds 	struct file *filp = NULL;
982b37ad28bSAl Viro 	__be32 status = nfs_ok;
98331dec253SDavid Shaw 	unsigned long cnt;
984ffe1137bSJ. Bruce Fields 	int nvecs;
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds 	/* no need to check permission - this will be done in nfsd_write() */
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 	if (write->wr_offset >= OFFSET_MAX)
9891da177e4SLinus Torvalds 		return nfserr_inval;
9901da177e4SLinus Torvalds 
9915ccb0066SStanislav Kinsbursky 	status = nfs4_preprocess_stateid_op(SVC_NET(rqstp),
9925ccb0066SStanislav Kinsbursky 					cstate, stateid, WR_STATE, &filp);
993375c5547SJ. Bruce Fields 	if (status) {
994375c5547SJ. Bruce Fields 		dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
995375c5547SJ. Bruce Fields 		return status;
996375c5547SJ. Bruce Fields 	}
997375c5547SJ. Bruce Fields 
99831dec253SDavid Shaw 	cnt = write->wr_buflen;
9991da177e4SLinus Torvalds 	write->wr_how_written = write->wr_stable_how;
1000b9c0ef85SStanislav Kinsbursky 	gen_boot_verifier(&write->wr_verifier, SVC_NET(rqstp));
10011da177e4SLinus Torvalds 
1002ffe1137bSJ. Bruce Fields 	nvecs = fill_in_write_vector(rqstp->rq_vec, write);
1003ffe1137bSJ. Bruce Fields 	WARN_ON_ONCE(nvecs > ARRAY_SIZE(rqstp->rq_vec));
1004ffe1137bSJ. Bruce Fields 
1005ca364317SJ.Bruce Fields 	status =  nfsd_write(rqstp, &cstate->current_fh, filp,
1006ffe1137bSJ. Bruce Fields 			     write->wr_offset, rqstp->rq_vec, nvecs,
100731dec253SDavid Shaw 			     &cnt, &write->wr_how_written);
10087e06b7f9SNeilBrown 	if (filp)
10097e06b7f9SNeilBrown 		fput(filp);
10101da177e4SLinus Torvalds 
101131dec253SDavid Shaw 	write->wr_bytes_written = cnt;
101231dec253SDavid Shaw 
10131da177e4SLinus Torvalds 	return status;
10141da177e4SLinus Torvalds }
10151da177e4SLinus Torvalds 
1016*24bab491SAnna Schumaker static __be32
1017*24bab491SAnna Schumaker nfsd4_seek(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1018*24bab491SAnna Schumaker 		struct nfsd4_seek *seek)
1019*24bab491SAnna Schumaker {
1020*24bab491SAnna Schumaker 	int whence;
1021*24bab491SAnna Schumaker 	__be32 status;
1022*24bab491SAnna Schumaker 	struct file *file;
1023*24bab491SAnna Schumaker 
1024*24bab491SAnna Schumaker 	status = nfs4_preprocess_stateid_op(SVC_NET(rqstp), cstate,
1025*24bab491SAnna Schumaker 					    &seek->seek_stateid,
1026*24bab491SAnna Schumaker 					    RD_STATE, &file);
1027*24bab491SAnna Schumaker 	if (status) {
1028*24bab491SAnna Schumaker 		dprintk("NFSD: nfsd4_seek: couldn't process stateid!\n");
1029*24bab491SAnna Schumaker 		return status;
1030*24bab491SAnna Schumaker 	}
1031*24bab491SAnna Schumaker 
1032*24bab491SAnna Schumaker 	switch (seek->seek_whence) {
1033*24bab491SAnna Schumaker 	case NFS4_CONTENT_DATA:
1034*24bab491SAnna Schumaker 		whence = SEEK_DATA;
1035*24bab491SAnna Schumaker 		break;
1036*24bab491SAnna Schumaker 	case NFS4_CONTENT_HOLE:
1037*24bab491SAnna Schumaker 		whence = SEEK_HOLE;
1038*24bab491SAnna Schumaker 		break;
1039*24bab491SAnna Schumaker 	default:
1040*24bab491SAnna Schumaker 		status = nfserr_union_notsupp;
1041*24bab491SAnna Schumaker 		goto out;
1042*24bab491SAnna Schumaker 	}
1043*24bab491SAnna Schumaker 
1044*24bab491SAnna Schumaker 	/*
1045*24bab491SAnna Schumaker 	 * Note:  This call does change file->f_pos, but nothing in NFSD
1046*24bab491SAnna Schumaker 	 *        should ever file->f_pos.
1047*24bab491SAnna Schumaker 	 */
1048*24bab491SAnna Schumaker 	seek->seek_pos = vfs_llseek(file, seek->seek_offset, whence);
1049*24bab491SAnna Schumaker 	if (seek->seek_pos < 0)
1050*24bab491SAnna Schumaker 		status = nfserrno(seek->seek_pos);
1051*24bab491SAnna Schumaker 	else if (seek->seek_pos >= i_size_read(file_inode(file)))
1052*24bab491SAnna Schumaker 		seek->seek_eof = true;
1053*24bab491SAnna Schumaker 
1054*24bab491SAnna Schumaker out:
1055*24bab491SAnna Schumaker 	fput(file);
1056*24bab491SAnna Schumaker 	return status;
1057*24bab491SAnna Schumaker }
1058*24bab491SAnna Schumaker 
10591da177e4SLinus Torvalds /* This routine never returns NFS_OK!  If there are no other errors, it
10601da177e4SLinus Torvalds  * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
10611da177e4SLinus Torvalds  * attributes matched.  VERIFY is implemented by mapping NFSERR_SAME
10621da177e4SLinus Torvalds  * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
10631da177e4SLinus Torvalds  */
1064b37ad28bSAl Viro static __be32
1065c954e2a5SJ.Bruce Fields _nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1066ca364317SJ.Bruce Fields 	     struct nfsd4_verify *verify)
10671da177e4SLinus Torvalds {
10682ebbc012SAl Viro 	__be32 *buf, *p;
10691da177e4SLinus Torvalds 	int count;
1070b37ad28bSAl Viro 	__be32 status;
10711da177e4SLinus Torvalds 
10728837abcaSMiklos Szeredi 	status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
10731da177e4SLinus Torvalds 	if (status)
10741da177e4SLinus Torvalds 		return status;
10751da177e4SLinus Torvalds 
10763c8e0316SYu Zhiguo 	status = check_attr_support(rqstp, cstate, verify->ve_bmval, NULL);
10773c8e0316SYu Zhiguo 	if (status)
10783c8e0316SYu Zhiguo 		return status;
10793c8e0316SYu Zhiguo 
10801da177e4SLinus Torvalds 	if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
10811da177e4SLinus Torvalds 	    || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
10821da177e4SLinus Torvalds 		return nfserr_inval;
10831da177e4SLinus Torvalds 	if (verify->ve_attrlen & 3)
10841da177e4SLinus Torvalds 		return nfserr_inval;
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 	/* count in words:
10871da177e4SLinus Torvalds 	 *   bitmap_len(1) + bitmap(2) + attr_len(1) = 4
10881da177e4SLinus Torvalds 	 */
10891da177e4SLinus Torvalds 	count = 4 + (verify->ve_attrlen >> 2);
10901da177e4SLinus Torvalds 	buf = kmalloc(count << 2, GFP_KERNEL);
10911da177e4SLinus Torvalds 	if (!buf)
10923e772463SJ. Bruce Fields 		return nfserr_jukebox;
10931da177e4SLinus Torvalds 
109484822d0bSJ. Bruce Fields 	p = buf;
1095d5184658SJ. Bruce Fields 	status = nfsd4_encode_fattr_to_buf(&p, count, &cstate->current_fh,
1096ca364317SJ.Bruce Fields 				    cstate->current_fh.fh_export,
1097d5184658SJ. Bruce Fields 				    cstate->current_fh.fh_dentry,
1098d5184658SJ. Bruce Fields 				    verify->ve_bmval,
1099406a7ea9SFrank Filz 				    rqstp, 0);
110041ae6e71SJ. Bruce Fields 	/*
110141ae6e71SJ. Bruce Fields 	 * If nfsd4_encode_fattr() ran out of space, assume that's because
110241ae6e71SJ. Bruce Fields 	 * the attributes are longer (hence different) than those given:
110341ae6e71SJ. Bruce Fields 	 */
110484822d0bSJ. Bruce Fields 	if (status == nfserr_resource)
11051da177e4SLinus Torvalds 		status = nfserr_not_same;
11061da177e4SLinus Torvalds 	if (status)
11071da177e4SLinus Torvalds 		goto out_kfree;
11081da177e4SLinus Torvalds 
110995ec28cdSBenny Halevy 	/* skip bitmap */
111095ec28cdSBenny Halevy 	p = buf + 1 + ntohl(buf[0]);
11111da177e4SLinus Torvalds 	status = nfserr_not_same;
11121da177e4SLinus Torvalds 	if (ntohl(*p++) != verify->ve_attrlen)
11131da177e4SLinus Torvalds 		goto out_kfree;
11141da177e4SLinus Torvalds 	if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
11151da177e4SLinus Torvalds 		status = nfserr_same;
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds out_kfree:
11181da177e4SLinus Torvalds 	kfree(buf);
11191da177e4SLinus Torvalds 	return status;
11201da177e4SLinus Torvalds }
11211da177e4SLinus Torvalds 
1122c954e2a5SJ.Bruce Fields static __be32
1123c954e2a5SJ.Bruce Fields nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1124c954e2a5SJ.Bruce Fields 	      struct nfsd4_verify *verify)
1125c954e2a5SJ.Bruce Fields {
1126c954e2a5SJ.Bruce Fields 	__be32 status;
1127c954e2a5SJ.Bruce Fields 
1128c954e2a5SJ.Bruce Fields 	status = _nfsd4_verify(rqstp, cstate, verify);
1129c954e2a5SJ.Bruce Fields 	return status == nfserr_not_same ? nfs_ok : status;
1130c954e2a5SJ.Bruce Fields }
1131c954e2a5SJ.Bruce Fields 
1132c954e2a5SJ.Bruce Fields static __be32
1133c954e2a5SJ.Bruce Fields nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1134c954e2a5SJ.Bruce Fields 	     struct nfsd4_verify *verify)
1135c954e2a5SJ.Bruce Fields {
1136c954e2a5SJ.Bruce Fields 	__be32 status;
1137c954e2a5SJ.Bruce Fields 
1138c954e2a5SJ.Bruce Fields 	status = _nfsd4_verify(rqstp, cstate, verify);
1139c954e2a5SJ.Bruce Fields 	return status == nfserr_same ? nfs_ok : status;
1140c954e2a5SJ.Bruce Fields }
1141c954e2a5SJ.Bruce Fields 
11421da177e4SLinus Torvalds /*
11431da177e4SLinus Torvalds  * NULL call.
11441da177e4SLinus Torvalds  */
11457111c66eSAl Viro static __be32
11461da177e4SLinus Torvalds nfsd4_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
11471da177e4SLinus Torvalds {
11481da177e4SLinus Torvalds 	return nfs_ok;
11491da177e4SLinus Torvalds }
11501da177e4SLinus Torvalds 
1151e2b20950SShankar Anand static inline void nfsd4_increment_op_stats(u32 opnum)
1152e2b20950SShankar Anand {
1153e2b20950SShankar Anand 	if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
1154e2b20950SShankar Anand 		nfsdstats.nfs4_opcount[opnum]++;
1155e2b20950SShankar Anand }
1156e2b20950SShankar Anand 
1157b591480bSJ.Bruce Fields typedef __be32(*nfsd4op_func)(struct svc_rqst *, struct nfsd4_compound_state *,
1158b591480bSJ.Bruce Fields 			      void *);
115958e7b33aSMi Jinlong typedef u32(*nfsd4op_rsize)(struct svc_rqst *, struct nfsd4_op *op);
11608b70484cSTigran Mkrtchyan typedef void(*stateid_setter)(struct nfsd4_compound_state *, void *);
11618b70484cSTigran Mkrtchyan typedef void(*stateid_getter)(struct nfsd4_compound_state *, void *);
116258e7b33aSMi Jinlong 
1163f9bb94c4SAndy Adamson enum nfsd4_op_flags {
1164f9bb94c4SAndy Adamson 	ALLOWED_WITHOUT_FH = 1 << 0,	/* No current filehandle required */
11655ece3cafSMi Jinlong 	ALLOWED_ON_ABSENT_FS = 1 << 1,	/* ops processed on absent fs */
11665ece3cafSMi Jinlong 	ALLOWED_AS_FIRST_OP = 1 << 2,	/* ops reqired first in compound */
116768d93184SJ. Bruce Fields 	/* For rfc 5661 section 2.6.3.1.1: */
116868d93184SJ. Bruce Fields 	OP_HANDLES_WRONGSEC = 1 << 3,
116968d93184SJ. Bruce Fields 	OP_IS_PUTFH_LIKE = 1 << 4,
11701091006cSJ. Bruce Fields 	/*
1171c856694eSJ. Bruce Fields 	 * These are the ops whose result size we estimate before
1172c856694eSJ. Bruce Fields 	 * encoding, to avoid performing an op then not being able to
1173c856694eSJ. Bruce Fields 	 * respond or cache a response.  This includes writes and setattrs
1174c856694eSJ. Bruce Fields 	 * as well as the operations usually called "nonidempotent":
1175c856694eSJ. Bruce Fields 	 */
1176c856694eSJ. Bruce Fields 	OP_MODIFIES_SOMETHING = 1 << 5,
1177c856694eSJ. Bruce Fields 	/*
1178c856694eSJ. Bruce Fields 	 * Cache compounds containing these ops in the xid-based drc:
11791091006cSJ. Bruce Fields 	 * We use the DRC for compounds containing non-idempotent
11801091006cSJ. Bruce Fields 	 * operations, *except* those that are 4.1-specific (since
11811091006cSJ. Bruce Fields 	 * sessions provide their own EOS), and except for stateful
11821091006cSJ. Bruce Fields 	 * operations other than setclientid and setclientid_confirm
11831091006cSJ. Bruce Fields 	 * (since sequence numbers provide EOS for open, lock, etc in
11841091006cSJ. Bruce Fields 	 * the v4.0 case).
11851091006cSJ. Bruce Fields 	 */
1186c856694eSJ. Bruce Fields 	OP_CACHEME = 1 << 6,
11878b70484cSTigran Mkrtchyan 	/*
11888b70484cSTigran Mkrtchyan 	 * These are ops which clear current state id.
11898b70484cSTigran Mkrtchyan 	 */
11908b70484cSTigran Mkrtchyan 	OP_CLEAR_STATEID = 1 << 7,
1191c856694eSJ. Bruce Fields };
1192c856694eSJ. Bruce Fields 
1193c856694eSJ. Bruce Fields struct nfsd4_operation {
1194c856694eSJ. Bruce Fields 	nfsd4op_func op_func;
1195c856694eSJ. Bruce Fields 	u32 op_flags;
1196c856694eSJ. Bruce Fields 	char *op_name;
119758e7b33aSMi Jinlong 	/* Try to get response size before operation */
119858e7b33aSMi Jinlong 	nfsd4op_rsize op_rsize_bop;
119924ff99c6SBryan Schumaker 	stateid_getter op_get_currentstateid;
120024ff99c6SBryan Schumaker 	stateid_setter op_set_currentstateid;
1201b591480bSJ.Bruce Fields };
1202b591480bSJ.Bruce Fields 
1203b591480bSJ.Bruce Fields static struct nfsd4_operation nfsd4_ops[];
1204b591480bSJ.Bruce Fields 
1205f1c7f79bSAdrian Bunk static const char *nfsd4_op_name(unsigned opnum);
1206b001a1b6SBenny Halevy 
12071da177e4SLinus Torvalds /*
120857716355SJ. Bruce Fields  * Enforce NFSv4.1 COMPOUND ordering rules:
1209f9bb94c4SAndy Adamson  *
121057716355SJ. Bruce Fields  * Also note, enforced elsewhere:
121157716355SJ. Bruce Fields  *	- SEQUENCE other than as first op results in
121257716355SJ. Bruce Fields  *	  NFS4ERR_SEQUENCE_POS. (Enforced in nfsd4_sequence().)
12131d1bc8f2SJ. Bruce Fields  *	- BIND_CONN_TO_SESSION must be the only op in its compound.
12141d1bc8f2SJ. Bruce Fields  *	  (Enforced in nfsd4_bind_conn_to_session().)
121557716355SJ. Bruce Fields  *	- DESTROY_SESSION must be the final operation in a compound, if
121657716355SJ. Bruce Fields  *	  sessionid's in SEQUENCE and DESTROY_SESSION are the same.
121757716355SJ. Bruce Fields  *	  (Enforced in nfsd4_destroy_session().)
1218f9bb94c4SAndy Adamson  */
121957716355SJ. Bruce Fields static __be32 nfs41_check_op_ordering(struct nfsd4_compoundargs *args)
1220f9bb94c4SAndy Adamson {
1221f9bb94c4SAndy Adamson 	struct nfsd4_op *op = &args->ops[0];
122257716355SJ. Bruce Fields 
122357716355SJ. Bruce Fields 	/* These ordering requirements don't apply to NFSv4.0: */
122457716355SJ. Bruce Fields 	if (args->minorversion == 0)
122557716355SJ. Bruce Fields 		return nfs_ok;
122657716355SJ. Bruce Fields 	/* This is weird, but OK, not our problem: */
122757716355SJ. Bruce Fields 	if (args->opcnt == 0)
122857716355SJ. Bruce Fields 		return nfs_ok;
122957716355SJ. Bruce Fields 	if (op->status == nfserr_op_illegal)
123057716355SJ. Bruce Fields 		return nfs_ok;
123157716355SJ. Bruce Fields 	if (!(nfsd4_ops[op->opnum].op_flags & ALLOWED_AS_FIRST_OP))
123257716355SJ. Bruce Fields 		return nfserr_op_not_in_session;
123357716355SJ. Bruce Fields 	if (op->opnum == OP_SEQUENCE)
123457716355SJ. Bruce Fields 		return nfs_ok;
123557716355SJ. Bruce Fields 	if (args->opcnt != 1)
123657716355SJ. Bruce Fields 		return nfserr_not_only_op;
123757716355SJ. Bruce Fields 	return nfs_ok;
1238f9bb94c4SAndy Adamson }
1239f9bb94c4SAndy Adamson 
124022b03214SJ. Bruce Fields static inline struct nfsd4_operation *OPDESC(struct nfsd4_op *op)
124122b03214SJ. Bruce Fields {
124222b03214SJ. Bruce Fields 	return &nfsd4_ops[op->opnum];
124322b03214SJ. Bruce Fields }
124422b03214SJ. Bruce Fields 
12451091006cSJ. Bruce Fields bool nfsd4_cache_this_op(struct nfsd4_op *op)
12461091006cSJ. Bruce Fields {
1247e372ba60SJ. Bruce Fields 	if (op->opnum == OP_ILLEGAL)
1248e372ba60SJ. Bruce Fields 		return false;
1249c856694eSJ. Bruce Fields 	return OPDESC(op)->op_flags & OP_CACHEME;
12501091006cSJ. Bruce Fields }
12511091006cSJ. Bruce Fields 
125268d93184SJ. Bruce Fields static bool need_wrongsec_check(struct svc_rqst *rqstp)
125368d93184SJ. Bruce Fields {
125468d93184SJ. Bruce Fields 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
125568d93184SJ. Bruce Fields 	struct nfsd4_compoundargs *argp = rqstp->rq_argp;
125668d93184SJ. Bruce Fields 	struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
125768d93184SJ. Bruce Fields 	struct nfsd4_op *next = &argp->ops[resp->opcnt];
125868d93184SJ. Bruce Fields 	struct nfsd4_operation *thisd;
125968d93184SJ. Bruce Fields 	struct nfsd4_operation *nextd;
126068d93184SJ. Bruce Fields 
126168d93184SJ. Bruce Fields 	thisd = OPDESC(this);
126268d93184SJ. Bruce Fields 	/*
126368d93184SJ. Bruce Fields 	 * Most ops check wronsec on our own; only the putfh-like ops
126468d93184SJ. Bruce Fields 	 * have special rules.
126568d93184SJ. Bruce Fields 	 */
126668d93184SJ. Bruce Fields 	if (!(thisd->op_flags & OP_IS_PUTFH_LIKE))
126768d93184SJ. Bruce Fields 		return false;
126868d93184SJ. Bruce Fields 	/*
126968d93184SJ. Bruce Fields 	 * rfc 5661 2.6.3.1.1.6: don't bother erroring out a
127068d93184SJ. Bruce Fields 	 * put-filehandle operation if we're not going to use the
127168d93184SJ. Bruce Fields 	 * result:
127268d93184SJ. Bruce Fields 	 */
127368d93184SJ. Bruce Fields 	if (argp->opcnt == resp->opcnt)
127468d93184SJ. Bruce Fields 		return false;
127568d93184SJ. Bruce Fields 
127668d93184SJ. Bruce Fields 	nextd = OPDESC(next);
127768d93184SJ. Bruce Fields 	/*
127868d93184SJ. Bruce Fields 	 * Rest of 2.6.3.1.1: certain operations will return WRONGSEC
127968d93184SJ. Bruce Fields 	 * errors themselves as necessary; others should check for them
128068d93184SJ. Bruce Fields 	 * now:
128168d93184SJ. Bruce Fields 	 */
128268d93184SJ. Bruce Fields 	return !(nextd->op_flags & OP_HANDLES_WRONGSEC);
128368d93184SJ. Bruce Fields }
128468d93184SJ. Bruce Fields 
12852d124dfaSJ. Bruce Fields static void svcxdr_init_encode(struct svc_rqst *rqstp,
12862d124dfaSJ. Bruce Fields 			       struct nfsd4_compoundres *resp)
12872d124dfaSJ. Bruce Fields {
12882d124dfaSJ. Bruce Fields 	struct xdr_stream *xdr = &resp->xdr;
12892d124dfaSJ. Bruce Fields 	struct xdr_buf *buf = &rqstp->rq_res;
12902d124dfaSJ. Bruce Fields 	struct kvec *head = buf->head;
12912d124dfaSJ. Bruce Fields 
12922d124dfaSJ. Bruce Fields 	xdr->buf = buf;
1293ddd1ea56SJ. Bruce Fields 	xdr->iov = head;
12942d124dfaSJ. Bruce Fields 	xdr->p   = head->iov_base + head->iov_len;
1295a5cddc88SJ. Bruce Fields 	xdr->end = head->iov_base + PAGE_SIZE - rqstp->rq_auth_slack;
12966ac90391SJ. Bruce Fields 	/* Tail and page_len should be zero at this point: */
12976ac90391SJ. Bruce Fields 	buf->len = buf->head[0].iov_len;
12982825a7f9SJ. Bruce Fields 	xdr->scratch.iov_len = 0;
129905638dc7SJ. Bruce Fields 	xdr->page_ptr = buf->pages - 1;
13002825a7f9SJ. Bruce Fields 	buf->buflen = PAGE_SIZE * (1 + rqstp->rq_page_end - buf->pages)
1301a5cddc88SJ. Bruce Fields 		- rqstp->rq_auth_slack;
13022d124dfaSJ. Bruce Fields }
13032d124dfaSJ. Bruce Fields 
1304f9bb94c4SAndy Adamson /*
13051da177e4SLinus Torvalds  * COMPOUND call.
13061da177e4SLinus Torvalds  */
13077111c66eSAl Viro static __be32
13081da177e4SLinus Torvalds nfsd4_proc_compound(struct svc_rqst *rqstp,
13091da177e4SLinus Torvalds 		    struct nfsd4_compoundargs *args,
13101da177e4SLinus Torvalds 		    struct nfsd4_compoundres *resp)
13111da177e4SLinus Torvalds {
13121da177e4SLinus Torvalds 	struct nfsd4_op	*op;
1313b591480bSJ.Bruce Fields 	struct nfsd4_operation *opdesc;
1314e354d571SAndy Adamson 	struct nfsd4_compound_state *cstate = &resp->cstate;
13154daeed25SKinglong Mee 	struct svc_fh *current_fh = &cstate->current_fh;
13164daeed25SKinglong Mee 	struct svc_fh *save_fh = &cstate->save_fh;
1317b37ad28bSAl Viro 	__be32		status;
13181da177e4SLinus Torvalds 
13192d124dfaSJ. Bruce Fields 	svcxdr_init_encode(rqstp, resp);
13204aea24b2SJ. Bruce Fields 	resp->tagp = resp->xdr.p;
13211da177e4SLinus Torvalds 	/* reserve space for: taglen, tag, and opcnt */
1322d3f627c8SJ. Bruce Fields 	xdr_reserve_space(&resp->xdr, 8 + args->taglen);
13231da177e4SLinus Torvalds 	resp->taglen = args->taglen;
13241da177e4SLinus Torvalds 	resp->tag = args->tag;
13251da177e4SLinus Torvalds 	resp->rqstp = rqstp;
13264daeed25SKinglong Mee 	cstate->minorversion = args->minorversion;
13274daeed25SKinglong Mee 	fh_init(current_fh, NFS4_FHSIZE);
13284daeed25SKinglong Mee 	fh_init(save_fh, NFS4_FHSIZE);
13298ff30fa4SNeilBrown 	/*
13308ff30fa4SNeilBrown 	 * Don't use the deferral mechanism for NFSv4; compounds make it
13318ff30fa4SNeilBrown 	 * too hard to avoid non-idempotency problems.
13328ff30fa4SNeilBrown 	 */
1333f15a5cf9SKinglong Mee 	rqstp->rq_usedeferral = false;
13341da177e4SLinus Torvalds 
13351da177e4SLinus Torvalds 	/*
13361da177e4SLinus Torvalds 	 * According to RFC3010, this takes precedence over all other errors.
13371da177e4SLinus Torvalds 	 */
13381da177e4SLinus Torvalds 	status = nfserr_minor_vers_mismatch;
133935f7a14fSJ. Bruce Fields 	if (nfsd_minorversion(args->minorversion, NFSD_TEST) <= 0)
13401da177e4SLinus Torvalds 		goto out;
13411da177e4SLinus Torvalds 
134257716355SJ. Bruce Fields 	status = nfs41_check_op_ordering(args);
134357716355SJ. Bruce Fields 	if (status) {
1344f9bb94c4SAndy Adamson 		op = &args->ops[0];
134557716355SJ. Bruce Fields 		op->status = status;
1346f9bb94c4SAndy Adamson 		goto encode_op;
1347f9bb94c4SAndy Adamson 	}
1348f9bb94c4SAndy Adamson 
13491da177e4SLinus Torvalds 	while (!status && resp->opcnt < args->opcnt) {
13501da177e4SLinus Torvalds 		op = &args->ops[resp->opcnt++];
13511da177e4SLinus Torvalds 
1352b001a1b6SBenny Halevy 		dprintk("nfsv4 compound op #%d/%d: %d (%s)\n",
1353b001a1b6SBenny Halevy 			resp->opcnt, args->opcnt, op->opnum,
1354b001a1b6SBenny Halevy 			nfsd4_op_name(op->opnum));
13551da177e4SLinus Torvalds 		/*
13561da177e4SLinus Torvalds 		 * The XDR decode routines may have pre-set op->status;
13571da177e4SLinus Torvalds 		 * for example, if there is a miscellaneous XDR error
13581da177e4SLinus Torvalds 		 * it will be set to nfserr_bad_xdr.
13591da177e4SLinus Torvalds 		 */
13609d313b17SJ. Bruce Fields 		if (op->status) {
13619d313b17SJ. Bruce Fields 			if (op->opnum == OP_OPEN)
13629d313b17SJ. Bruce Fields 				op->status = nfsd4_open_omfg(rqstp, cstate, op);
13631da177e4SLinus Torvalds 			goto encode_op;
13649d313b17SJ. Bruce Fields 		}
13651da177e4SLinus Torvalds 
136622b03214SJ. Bruce Fields 		opdesc = OPDESC(op);
1367b591480bSJ.Bruce Fields 
13684daeed25SKinglong Mee 		if (!current_fh->fh_dentry) {
136927d630ecSJ.Bruce Fields 			if (!(opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
13701da177e4SLinus Torvalds 				op->status = nfserr_nofilehandle;
13711da177e4SLinus Torvalds 				goto encode_op;
13721da177e4SLinus Torvalds 			}
13734daeed25SKinglong Mee 		} else if (current_fh->fh_export->ex_fslocs.migrated &&
1374eeac294eSJ.Bruce Fields 			  !(opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
137542ca0993SJ.Bruce Fields 			op->status = nfserr_moved;
137642ca0993SJ.Bruce Fields 			goto encode_op;
137742ca0993SJ.Bruce Fields 		}
1378b591480bSJ.Bruce Fields 
13792336745eSKinglong Mee 		fh_clear_wcc(current_fh);
13802336745eSKinglong Mee 
138158e7b33aSMi Jinlong 		/* If op is non-idempotent */
138258e7b33aSMi Jinlong 		if (opdesc->op_flags & OP_MODIFIES_SOMETHING) {
13834c69d585SJ. Bruce Fields 			/*
1384a8095f7eSJ. Bruce Fields 			 * Don't execute this op if we couldn't encode a
1385a8095f7eSJ. Bruce Fields 			 * succesful reply:
1386a8095f7eSJ. Bruce Fields 			 */
1387a8095f7eSJ. Bruce Fields 			u32 plen = opdesc->op_rsize_bop(rqstp, op);
1388a8095f7eSJ. Bruce Fields 			/*
1389a8095f7eSJ. Bruce Fields 			 * Plus if there's another operation, make sure
13904c69d585SJ. Bruce Fields 			 * we'll have space to at least encode an error:
13914c69d585SJ. Bruce Fields 			 */
13924c69d585SJ. Bruce Fields 			if (resp->opcnt < args->opcnt)
13934c69d585SJ. Bruce Fields 				plen += COMPOUND_ERR_SLACK_SPACE;
139458e7b33aSMi Jinlong 			op->status = nfsd4_check_resp_size(resp, plen);
139558e7b33aSMi Jinlong 		}
139658e7b33aSMi Jinlong 
139758e7b33aSMi Jinlong 		if (op->status)
139858e7b33aSMi Jinlong 			goto encode_op;
139958e7b33aSMi Jinlong 
14008b70484cSTigran Mkrtchyan 		if (opdesc->op_get_currentstateid)
14018b70484cSTigran Mkrtchyan 			opdesc->op_get_currentstateid(cstate, &op->u);
1402b591480bSJ.Bruce Fields 		op->status = opdesc->op_func(rqstp, cstate, &op->u);
14031da177e4SLinus Torvalds 
14048b70484cSTigran Mkrtchyan 		if (!op->status) {
14058b70484cSTigran Mkrtchyan 			if (opdesc->op_set_currentstateid)
14068b70484cSTigran Mkrtchyan 				opdesc->op_set_currentstateid(cstate, &op->u);
14078b70484cSTigran Mkrtchyan 
14088b70484cSTigran Mkrtchyan 			if (opdesc->op_flags & OP_CLEAR_STATEID)
140937c593c5STigran Mkrtchyan 				clear_current_stateid(cstate);
14108b70484cSTigran Mkrtchyan 
14118b70484cSTigran Mkrtchyan 			if (need_wrongsec_check(rqstp))
14124daeed25SKinglong Mee 				op->status = check_nfsd_access(current_fh->fh_export, rqstp);
14138b70484cSTigran Mkrtchyan 		}
141468d93184SJ. Bruce Fields 
14151da177e4SLinus Torvalds encode_op:
141649557cc7SAndy Adamson 		/* Only from SEQUENCE */
14174daeed25SKinglong Mee 		if (cstate->status == nfserr_replay_cache) {
1418da3846a2SAndy Adamson 			dprintk("%s NFS4.1 replay from cache\n", __func__);
1419da3846a2SAndy Adamson 			status = op->status;
1420da3846a2SAndy Adamson 			goto out;
1421da3846a2SAndy Adamson 		}
1422a90b061cSAl Viro 		if (op->status == nfserr_replay_me) {
1423a4f1706aSJ.Bruce Fields 			op->replay = &cstate->replay_owner->so_replay;
1424d0a381ddSJ. Bruce Fields 			nfsd4_encode_replay(&resp->xdr, op);
14251da177e4SLinus Torvalds 			status = op->status = op->replay->rp_status;
14261da177e4SLinus Torvalds 		} else {
14271da177e4SLinus Torvalds 			nfsd4_encode_operation(resp, op);
14281da177e4SLinus Torvalds 			status = op->status;
14291da177e4SLinus Torvalds 		}
14300407717dSBenny Halevy 
14310407717dSBenny Halevy 		dprintk("nfsv4 compound op %p opcnt %d #%d: %d: status %d\n",
14320407717dSBenny Halevy 			args->ops, args->opcnt, resp->opcnt, op->opnum,
14330407717dSBenny Halevy 			be32_to_cpu(status));
14340407717dSBenny Halevy 
143558fb12e6SJeff Layton 		nfsd4_cstate_clear_replay(cstate);
14367e06b7f9SNeilBrown 		/* XXX Ugh, we need to get rid of this kind of special case: */
14377e06b7f9SNeilBrown 		if (op->opnum == OP_READ && op->u.read.rd_filp)
14387e06b7f9SNeilBrown 			fput(op->u.read.rd_filp);
1439e2b20950SShankar Anand 
1440e2b20950SShankar Anand 		nfsd4_increment_op_stats(op->opnum);
14411da177e4SLinus Torvalds 	}
14421da177e4SLinus Torvalds 
14434daeed25SKinglong Mee 	cstate->status = status;
14444daeed25SKinglong Mee 	fh_put(current_fh);
14454daeed25SKinglong Mee 	fh_put(save_fh);
14464daeed25SKinglong Mee 	BUG_ON(cstate->replay_owner);
14471da177e4SLinus Torvalds out:
14482f425878SAndy Adamson 	/* Reset deferral mechanism for RPC deferrals */
1449f15a5cf9SKinglong Mee 	rqstp->rq_usedeferral = true;
14503b12cd98SJ. Bruce Fields 	dprintk("nfsv4 compound returned %d\n", ntohl(status));
14511da177e4SLinus Torvalds 	return status;
14521da177e4SLinus Torvalds }
14531da177e4SLinus Torvalds 
145458e7b33aSMi Jinlong #define op_encode_hdr_size		(2)
145558e7b33aSMi Jinlong #define op_encode_stateid_maxsz		(XDR_QUADLEN(NFS4_STATEID_SIZE))
145658e7b33aSMi Jinlong #define op_encode_verifier_maxsz	(XDR_QUADLEN(NFS4_VERIFIER_SIZE))
145758e7b33aSMi Jinlong #define op_encode_change_info_maxsz	(5)
145858e7b33aSMi Jinlong #define nfs4_fattr_bitmap_maxsz		(4)
145958e7b33aSMi Jinlong 
14608c7424cfSJ. Bruce Fields /* We'll fall back on returning no lockowner if run out of space: */
14618c7424cfSJ. Bruce Fields #define op_encode_lockowner_maxsz	(0)
146258e7b33aSMi Jinlong #define op_encode_lock_denied_maxsz	(8 + op_encode_lockowner_maxsz)
146358e7b33aSMi Jinlong 
146458e7b33aSMi Jinlong #define nfs4_owner_maxsz		(1 + XDR_QUADLEN(IDMAP_NAMESZ))
146558e7b33aSMi Jinlong 
146658e7b33aSMi Jinlong #define op_encode_ace_maxsz		(3 + nfs4_owner_maxsz)
146758e7b33aSMi Jinlong #define op_encode_delegation_maxsz	(1 + op_encode_stateid_maxsz + 1 + \
146858e7b33aSMi Jinlong 					 op_encode_ace_maxsz)
146958e7b33aSMi Jinlong 
147058e7b33aSMi Jinlong #define op_encode_channel_attrs_maxsz	(6 + 1 + 1)
147158e7b33aSMi Jinlong 
147258e7b33aSMi Jinlong static inline u32 nfsd4_only_status_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
147358e7b33aSMi Jinlong {
147458e7b33aSMi Jinlong 	return (op_encode_hdr_size) * sizeof(__be32);
147558e7b33aSMi Jinlong }
147658e7b33aSMi Jinlong 
147758e7b33aSMi Jinlong static inline u32 nfsd4_status_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
147858e7b33aSMi Jinlong {
147958e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_stateid_maxsz)* sizeof(__be32);
148058e7b33aSMi Jinlong }
148158e7b33aSMi Jinlong 
148258e7b33aSMi Jinlong static inline u32 nfsd4_commit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
148358e7b33aSMi Jinlong {
148458e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_verifier_maxsz) * sizeof(__be32);
148558e7b33aSMi Jinlong }
148658e7b33aSMi Jinlong 
148758e7b33aSMi Jinlong static inline u32 nfsd4_create_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
148858e7b33aSMi Jinlong {
148958e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_change_info_maxsz
149058e7b33aSMi Jinlong 		+ nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
149158e7b33aSMi Jinlong }
149258e7b33aSMi Jinlong 
1493b86cef60SJ. Bruce Fields /*
1494b86cef60SJ. Bruce Fields  * Note since this is an idempotent operation we won't insist on failing
1495b86cef60SJ. Bruce Fields  * the op prematurely if the estimate is too large.  We may turn off splice
1496b86cef60SJ. Bruce Fields  * reads unnecessarily.
1497b86cef60SJ. Bruce Fields  */
1498b86cef60SJ. Bruce Fields static inline u32 nfsd4_getattr_rsize(struct svc_rqst *rqstp,
1499b86cef60SJ. Bruce Fields 				      struct nfsd4_op *op)
1500b86cef60SJ. Bruce Fields {
1501b86cef60SJ. Bruce Fields 	u32 *bmap = op->u.getattr.ga_bmval;
1502b86cef60SJ. Bruce Fields 	u32 bmap0 = bmap[0], bmap1 = bmap[1], bmap2 = bmap[2];
1503b86cef60SJ. Bruce Fields 	u32 ret = 0;
1504b86cef60SJ. Bruce Fields 
1505b86cef60SJ. Bruce Fields 	if (bmap0 & FATTR4_WORD0_ACL)
1506b86cef60SJ. Bruce Fields 		return svc_max_payload(rqstp);
1507b86cef60SJ. Bruce Fields 	if (bmap0 & FATTR4_WORD0_FS_LOCATIONS)
1508b86cef60SJ. Bruce Fields 		return svc_max_payload(rqstp);
1509b86cef60SJ. Bruce Fields 
1510b86cef60SJ. Bruce Fields 	if (bmap1 & FATTR4_WORD1_OWNER) {
1511b86cef60SJ. Bruce Fields 		ret += IDMAP_NAMESZ + 4;
1512b86cef60SJ. Bruce Fields 		bmap1 &= ~FATTR4_WORD1_OWNER;
1513b86cef60SJ. Bruce Fields 	}
1514b86cef60SJ. Bruce Fields 	if (bmap1 & FATTR4_WORD1_OWNER_GROUP) {
1515b86cef60SJ. Bruce Fields 		ret += IDMAP_NAMESZ + 4;
1516b86cef60SJ. Bruce Fields 		bmap1 &= ~FATTR4_WORD1_OWNER_GROUP;
1517b86cef60SJ. Bruce Fields 	}
1518b86cef60SJ. Bruce Fields 	if (bmap0 & FATTR4_WORD0_FILEHANDLE) {
1519b86cef60SJ. Bruce Fields 		ret += NFS4_FHSIZE + 4;
1520b86cef60SJ. Bruce Fields 		bmap0 &= ~FATTR4_WORD0_FILEHANDLE;
1521b86cef60SJ. Bruce Fields 	}
1522b86cef60SJ. Bruce Fields 	if (bmap2 & FATTR4_WORD2_SECURITY_LABEL) {
1523b86cef60SJ. Bruce Fields 		ret += NFSD4_MAX_SEC_LABEL_LEN + 12;
1524b86cef60SJ. Bruce Fields 		bmap2 &= ~FATTR4_WORD2_SECURITY_LABEL;
1525b86cef60SJ. Bruce Fields 	}
1526b86cef60SJ. Bruce Fields 	/*
1527b86cef60SJ. Bruce Fields 	 * Largest of remaining attributes are 16 bytes (e.g.,
1528b86cef60SJ. Bruce Fields 	 * supported_attributes)
1529b86cef60SJ. Bruce Fields 	 */
1530b86cef60SJ. Bruce Fields 	ret += 16 * (hweight32(bmap0) + hweight32(bmap1) + hweight32(bmap2));
1531b86cef60SJ. Bruce Fields 	/* bitmask, length */
1532b86cef60SJ. Bruce Fields 	ret += 20;
1533b86cef60SJ. Bruce Fields 	return ret;
1534b86cef60SJ. Bruce Fields }
1535b86cef60SJ. Bruce Fields 
153658e7b33aSMi Jinlong static inline u32 nfsd4_link_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
153758e7b33aSMi Jinlong {
153858e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
153958e7b33aSMi Jinlong 		* sizeof(__be32);
154058e7b33aSMi Jinlong }
154158e7b33aSMi Jinlong 
154258e7b33aSMi Jinlong static inline u32 nfsd4_lock_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
154358e7b33aSMi Jinlong {
154458e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_lock_denied_maxsz)
154558e7b33aSMi Jinlong 		* sizeof(__be32);
154658e7b33aSMi Jinlong }
154758e7b33aSMi Jinlong 
154858e7b33aSMi Jinlong static inline u32 nfsd4_open_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
154958e7b33aSMi Jinlong {
155058e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_stateid_maxsz
155158e7b33aSMi Jinlong 		+ op_encode_change_info_maxsz + 1
155258e7b33aSMi Jinlong 		+ nfs4_fattr_bitmap_maxsz
155358e7b33aSMi Jinlong 		+ op_encode_delegation_maxsz) * sizeof(__be32);
155458e7b33aSMi Jinlong }
155558e7b33aSMi Jinlong 
155658e7b33aSMi Jinlong static inline u32 nfsd4_read_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
155758e7b33aSMi Jinlong {
155858e7b33aSMi Jinlong 	u32 maxcount = 0, rlen = 0;
155958e7b33aSMi Jinlong 
156058e7b33aSMi Jinlong 	maxcount = svc_max_payload(rqstp);
15613c7aa15dSKinglong Mee 	rlen = min(op->u.read.rd_length, maxcount);
156258e7b33aSMi Jinlong 
1563622f560eSJ. Bruce Fields 	return (op_encode_hdr_size + 2 + XDR_QUADLEN(rlen)) * sizeof(__be32);
156458e7b33aSMi Jinlong }
156558e7b33aSMi Jinlong 
156658e7b33aSMi Jinlong static inline u32 nfsd4_readdir_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
156758e7b33aSMi Jinlong {
15683c7aa15dSKinglong Mee 	u32 maxcount = 0, rlen = 0;
156958e7b33aSMi Jinlong 
15703c7aa15dSKinglong Mee 	maxcount = svc_max_payload(rqstp);
15713c7aa15dSKinglong Mee 	rlen = min(op->u.readdir.rd_maxcount, maxcount);
157258e7b33aSMi Jinlong 
1573561f0ed4SJ. Bruce Fields 	return (op_encode_hdr_size + op_encode_verifier_maxsz +
1574561f0ed4SJ. Bruce Fields 		XDR_QUADLEN(rlen)) * sizeof(__be32);
157558e7b33aSMi Jinlong }
157658e7b33aSMi Jinlong 
157758e7b33aSMi Jinlong static inline u32 nfsd4_remove_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
157858e7b33aSMi Jinlong {
157958e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
158058e7b33aSMi Jinlong 		* sizeof(__be32);
158158e7b33aSMi Jinlong }
158258e7b33aSMi Jinlong 
158358e7b33aSMi Jinlong static inline u32 nfsd4_rename_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
158458e7b33aSMi Jinlong {
158558e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_change_info_maxsz
158658e7b33aSMi Jinlong 		+ op_encode_change_info_maxsz) * sizeof(__be32);
158758e7b33aSMi Jinlong }
158858e7b33aSMi Jinlong 
1589ccae70a9SJ. Bruce Fields static inline u32 nfsd4_sequence_rsize(struct svc_rqst *rqstp,
1590ccae70a9SJ. Bruce Fields 				       struct nfsd4_op *op)
1591ccae70a9SJ. Bruce Fields {
1592ccae70a9SJ. Bruce Fields 	return NFS4_MAX_SESSIONID_LEN + 20;
1593ccae70a9SJ. Bruce Fields }
1594ccae70a9SJ. Bruce Fields 
159558e7b33aSMi Jinlong static inline u32 nfsd4_setattr_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
159658e7b33aSMi Jinlong {
159758e7b33aSMi Jinlong 	return (op_encode_hdr_size + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
159858e7b33aSMi Jinlong }
159958e7b33aSMi Jinlong 
160058e7b33aSMi Jinlong static inline u32 nfsd4_setclientid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
160158e7b33aSMi Jinlong {
1602480efaeeSJ. Bruce Fields 	return (op_encode_hdr_size + 2 + XDR_QUADLEN(NFS4_VERIFIER_SIZE)) *
1603480efaeeSJ. Bruce Fields 								sizeof(__be32);
160458e7b33aSMi Jinlong }
160558e7b33aSMi Jinlong 
160658e7b33aSMi Jinlong static inline u32 nfsd4_write_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
160758e7b33aSMi Jinlong {
1608f34e432bSJ. Bruce Fields 	return (op_encode_hdr_size + 2 + op_encode_verifier_maxsz) * sizeof(__be32);
160958e7b33aSMi Jinlong }
161058e7b33aSMi Jinlong 
161158e7b33aSMi Jinlong static inline u32 nfsd4_exchange_id_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
161258e7b33aSMi Jinlong {
161358e7b33aSMi Jinlong 	return (op_encode_hdr_size + 2 + 1 + /* eir_clientid, eir_sequenceid */\
1614a8bb84bcSKinglong Mee 		1 + 1 + /* eir_flags, spr_how */\
1615a8bb84bcSKinglong Mee 		4 + /* spo_must_enforce & _allow with bitmap */\
161658e7b33aSMi Jinlong 		2 + /*eir_server_owner.so_minor_id */\
161758e7b33aSMi Jinlong 		/* eir_server_owner.so_major_id<> */\
161858e7b33aSMi Jinlong 		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
161958e7b33aSMi Jinlong 		/* eir_server_scope<> */\
162058e7b33aSMi Jinlong 		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
162158e7b33aSMi Jinlong 		1 + /* eir_server_impl_id array length */\
162258e7b33aSMi Jinlong 		0 /* ignored eir_server_impl_id contents */) * sizeof(__be32);
162358e7b33aSMi Jinlong }
162458e7b33aSMi Jinlong 
162558e7b33aSMi Jinlong static inline u32 nfsd4_bind_conn_to_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
162658e7b33aSMi Jinlong {
162758e7b33aSMi Jinlong 	return (op_encode_hdr_size + \
162858e7b33aSMi Jinlong 		XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* bctsr_sessid */\
162958e7b33aSMi Jinlong 		2 /* bctsr_dir, use_conn_in_rdma_mode */) * sizeof(__be32);
163058e7b33aSMi Jinlong }
163158e7b33aSMi Jinlong 
163258e7b33aSMi Jinlong static inline u32 nfsd4_create_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
163358e7b33aSMi Jinlong {
163458e7b33aSMi Jinlong 	return (op_encode_hdr_size + \
163558e7b33aSMi Jinlong 		XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* sessionid */\
163658e7b33aSMi Jinlong 		2 + /* csr_sequence, csr_flags */\
163758e7b33aSMi Jinlong 		op_encode_channel_attrs_maxsz + \
163858e7b33aSMi Jinlong 		op_encode_channel_attrs_maxsz) * sizeof(__be32);
163958e7b33aSMi Jinlong }
164058e7b33aSMi Jinlong 
164120766016SBenny Halevy static struct nfsd4_operation nfsd4_ops[] = {
1642b591480bSJ.Bruce Fields 	[OP_ACCESS] = {
1643b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_access,
1644b001a1b6SBenny Halevy 		.op_name = "OP_ACCESS",
1645b591480bSJ.Bruce Fields 	},
1646b591480bSJ.Bruce Fields 	[OP_CLOSE] = {
1647b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_close,
164858e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
1649b001a1b6SBenny Halevy 		.op_name = "OP_CLOSE",
165058e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_status_stateid_rsize,
16518b70484cSTigran Mkrtchyan 		.op_get_currentstateid = (stateid_getter)nfsd4_get_closestateid,
16528b70484cSTigran Mkrtchyan 		.op_set_currentstateid = (stateid_setter)nfsd4_set_closestateid,
1653b591480bSJ.Bruce Fields 	},
1654b591480bSJ.Bruce Fields 	[OP_COMMIT] = {
1655b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_commit,
165658e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
1657b001a1b6SBenny Halevy 		.op_name = "OP_COMMIT",
165858e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_commit_rsize,
1659b591480bSJ.Bruce Fields 	},
1660b591480bSJ.Bruce Fields 	[OP_CREATE] = {
1661b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_create,
1662d1471053STigran Mkrtchyan 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME | OP_CLEAR_STATEID,
1663b001a1b6SBenny Halevy 		.op_name = "OP_CREATE",
166458e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_create_rsize,
1665b591480bSJ.Bruce Fields 	},
1666b591480bSJ.Bruce Fields 	[OP_DELEGRETURN] = {
1667b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_delegreturn,
166858e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
1669b001a1b6SBenny Halevy 		.op_name = "OP_DELEGRETURN",
167058e7b33aSMi Jinlong 		.op_rsize_bop = nfsd4_only_status_rsize,
16719428fe1aSTigran Mkrtchyan 		.op_get_currentstateid = (stateid_getter)nfsd4_get_delegreturnstateid,
1672b591480bSJ.Bruce Fields 	},
1673b591480bSJ.Bruce Fields 	[OP_GETATTR] = {
1674b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_getattr,
1675eeac294eSJ.Bruce Fields 		.op_flags = ALLOWED_ON_ABSENT_FS,
1676b86cef60SJ. Bruce Fields 		.op_rsize_bop = nfsd4_getattr_rsize,
1677b001a1b6SBenny Halevy 		.op_name = "OP_GETATTR",
1678b591480bSJ.Bruce Fields 	},
1679b591480bSJ.Bruce Fields 	[OP_GETFH] = {
1680b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_getfh,
1681b001a1b6SBenny Halevy 		.op_name = "OP_GETFH",
1682b591480bSJ.Bruce Fields 	},
1683b591480bSJ.Bruce Fields 	[OP_LINK] = {
1684b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_link,
1685c856694eSJ. Bruce Fields 		.op_flags = ALLOWED_ON_ABSENT_FS | OP_MODIFIES_SOMETHING
1686c856694eSJ. Bruce Fields 				| OP_CACHEME,
1687b001a1b6SBenny Halevy 		.op_name = "OP_LINK",
168858e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_link_rsize,
1689b591480bSJ.Bruce Fields 	},
1690b591480bSJ.Bruce Fields 	[OP_LOCK] = {
1691b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_lock,
169258e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
1693b001a1b6SBenny Halevy 		.op_name = "OP_LOCK",
169458e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_lock_rsize,
169562cd4a59STigran Mkrtchyan 		.op_set_currentstateid = (stateid_setter)nfsd4_set_lockstateid,
1696b591480bSJ.Bruce Fields 	},
1697b591480bSJ.Bruce Fields 	[OP_LOCKT] = {
1698b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_lockt,
1699b001a1b6SBenny Halevy 		.op_name = "OP_LOCKT",
1700b591480bSJ.Bruce Fields 	},
1701b591480bSJ.Bruce Fields 	[OP_LOCKU] = {
1702b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_locku,
170358e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
1704b001a1b6SBenny Halevy 		.op_name = "OP_LOCKU",
170558e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_status_stateid_rsize,
170662cd4a59STigran Mkrtchyan 		.op_get_currentstateid = (stateid_getter)nfsd4_get_lockustateid,
1707b591480bSJ.Bruce Fields 	},
1708b591480bSJ.Bruce Fields 	[OP_LOOKUP] = {
1709b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_lookup,
1710d1471053STigran Mkrtchyan 		.op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
1711b001a1b6SBenny Halevy 		.op_name = "OP_LOOKUP",
1712b591480bSJ.Bruce Fields 	},
1713b591480bSJ.Bruce Fields 	[OP_LOOKUPP] = {
1714b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_lookupp,
1715d1471053STigran Mkrtchyan 		.op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
1716b001a1b6SBenny Halevy 		.op_name = "OP_LOOKUPP",
1717b591480bSJ.Bruce Fields 	},
1718b591480bSJ.Bruce Fields 	[OP_NVERIFY] = {
1719b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_nverify,
1720b001a1b6SBenny Halevy 		.op_name = "OP_NVERIFY",
1721b591480bSJ.Bruce Fields 	},
1722b591480bSJ.Bruce Fields 	[OP_OPEN] = {
1723b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_open,
172458e7b33aSMi Jinlong 		.op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
1725b001a1b6SBenny Halevy 		.op_name = "OP_OPEN",
172658e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_open_rsize,
17278b70484cSTigran Mkrtchyan 		.op_set_currentstateid = (stateid_setter)nfsd4_set_openstateid,
1728b591480bSJ.Bruce Fields 	},
1729b591480bSJ.Bruce Fields 	[OP_OPEN_CONFIRM] = {
1730b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_open_confirm,
173158e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
1732b001a1b6SBenny Halevy 		.op_name = "OP_OPEN_CONFIRM",
173358e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_status_stateid_rsize,
1734b591480bSJ.Bruce Fields 	},
1735b591480bSJ.Bruce Fields 	[OP_OPEN_DOWNGRADE] = {
1736b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_open_downgrade,
173758e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
1738b001a1b6SBenny Halevy 		.op_name = "OP_OPEN_DOWNGRADE",
173958e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_status_stateid_rsize,
17409428fe1aSTigran Mkrtchyan 		.op_get_currentstateid = (stateid_getter)nfsd4_get_opendowngradestateid,
17419428fe1aSTigran Mkrtchyan 		.op_set_currentstateid = (stateid_setter)nfsd4_set_opendowngradestateid,
1742b591480bSJ.Bruce Fields 	},
1743b591480bSJ.Bruce Fields 	[OP_PUTFH] = {
1744b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_putfh,
174568d93184SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
17465b648699SJ. Bruce Fields 				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
1747b001a1b6SBenny Halevy 		.op_name = "OP_PUTFH",
174858e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
1749b591480bSJ.Bruce Fields 	},
1750eeac294eSJ.Bruce Fields 	[OP_PUTPUBFH] = {
1751a1c8c4d1SJ. Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_putrootfh,
175268d93184SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
17535b648699SJ. Bruce Fields 				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
1754b001a1b6SBenny Halevy 		.op_name = "OP_PUTPUBFH",
175558e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
1756eeac294eSJ.Bruce Fields 	},
1757b591480bSJ.Bruce Fields 	[OP_PUTROOTFH] = {
1758b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_putrootfh,
175968d93184SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
17605b648699SJ. Bruce Fields 				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
1761b001a1b6SBenny Halevy 		.op_name = "OP_PUTROOTFH",
176258e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
1763b591480bSJ.Bruce Fields 	},
1764b591480bSJ.Bruce Fields 	[OP_READ] = {
1765b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_read,
1766b001a1b6SBenny Halevy 		.op_name = "OP_READ",
176758e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_read_rsize,
176830813e27STigran Mkrtchyan 		.op_get_currentstateid = (stateid_getter)nfsd4_get_readstateid,
1769b591480bSJ.Bruce Fields 	},
1770b591480bSJ.Bruce Fields 	[OP_READDIR] = {
1771b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_readdir,
1772b001a1b6SBenny Halevy 		.op_name = "OP_READDIR",
177358e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_readdir_rsize,
1774b591480bSJ.Bruce Fields 	},
1775b591480bSJ.Bruce Fields 	[OP_READLINK] = {
1776b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_readlink,
1777b001a1b6SBenny Halevy 		.op_name = "OP_READLINK",
1778b591480bSJ.Bruce Fields 	},
1779b591480bSJ.Bruce Fields 	[OP_REMOVE] = {
1780b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_remove,
1781c856694eSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
1782b001a1b6SBenny Halevy 		.op_name = "OP_REMOVE",
178358e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_remove_rsize,
1784b591480bSJ.Bruce Fields 	},
1785b591480bSJ.Bruce Fields 	[OP_RENAME] = {
1786b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_rename,
1787c856694eSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
178858e7b33aSMi Jinlong 		.op_name = "OP_RENAME",
178958e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_rename_rsize,
1790b591480bSJ.Bruce Fields 	},
1791b591480bSJ.Bruce Fields 	[OP_RENEW] = {
1792b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_renew,
179358e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
179458e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
1795b001a1b6SBenny Halevy 		.op_name = "OP_RENEW",
179658e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
179758e7b33aSMi Jinlong 
1798b591480bSJ.Bruce Fields 	},
1799b591480bSJ.Bruce Fields 	[OP_RESTOREFH] = {
1800b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_restorefh,
180168d93184SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
180258e7b33aSMi Jinlong 				| OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING,
1803b001a1b6SBenny Halevy 		.op_name = "OP_RESTOREFH",
180458e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
1805b591480bSJ.Bruce Fields 	},
1806b591480bSJ.Bruce Fields 	[OP_SAVEFH] = {
1807b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_savefh,
180858e7b33aSMi Jinlong 		.op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
1809b001a1b6SBenny Halevy 		.op_name = "OP_SAVEFH",
181058e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
1811b591480bSJ.Bruce Fields 	},
1812dcb488a3SAndy Adamson 	[OP_SECINFO] = {
1813dcb488a3SAndy Adamson 		.op_func = (nfsd4op_func)nfsd4_secinfo,
181468d93184SJ. Bruce Fields 		.op_flags = OP_HANDLES_WRONGSEC,
1815b001a1b6SBenny Halevy 		.op_name = "OP_SECINFO",
1816dcb488a3SAndy Adamson 	},
1817b591480bSJ.Bruce Fields 	[OP_SETATTR] = {
1818b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_setattr,
1819b001a1b6SBenny Halevy 		.op_name = "OP_SETATTR",
1820c856694eSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
182158e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_setattr_rsize,
18221e97b519STigran Mkrtchyan 		.op_get_currentstateid = (stateid_getter)nfsd4_get_setattrstateid,
1823b591480bSJ.Bruce Fields 	},
1824b591480bSJ.Bruce Fields 	[OP_SETCLIENTID] = {
1825b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_setclientid,
182658e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
1827c856694eSJ. Bruce Fields 				| OP_MODIFIES_SOMETHING | OP_CACHEME,
1828b001a1b6SBenny Halevy 		.op_name = "OP_SETCLIENTID",
182958e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_setclientid_rsize,
1830b591480bSJ.Bruce Fields 	},
1831b591480bSJ.Bruce Fields 	[OP_SETCLIENTID_CONFIRM] = {
1832b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_setclientid_confirm,
183358e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
1834c856694eSJ. Bruce Fields 				| OP_MODIFIES_SOMETHING | OP_CACHEME,
1835b001a1b6SBenny Halevy 		.op_name = "OP_SETCLIENTID_CONFIRM",
183658e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
1837b591480bSJ.Bruce Fields 	},
1838b591480bSJ.Bruce Fields 	[OP_VERIFY] = {
1839b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_verify,
1840b001a1b6SBenny Halevy 		.op_name = "OP_VERIFY",
1841b591480bSJ.Bruce Fields 	},
1842b591480bSJ.Bruce Fields 	[OP_WRITE] = {
1843b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_write,
1844c856694eSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
1845b001a1b6SBenny Halevy 		.op_name = "OP_WRITE",
184658e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_write_rsize,
184730813e27STigran Mkrtchyan 		.op_get_currentstateid = (stateid_getter)nfsd4_get_writestateid,
1848b591480bSJ.Bruce Fields 	},
1849b591480bSJ.Bruce Fields 	[OP_RELEASE_LOCKOWNER] = {
1850b591480bSJ.Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_release_lockowner,
185158e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
185258e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
1853b001a1b6SBenny Halevy 		.op_name = "OP_RELEASE_LOCKOWNER",
185458e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
1855b591480bSJ.Bruce Fields 	},
1856069b6ad4SAndy Adamson 
1857069b6ad4SAndy Adamson 	/* NFSv4.1 operations */
1858069b6ad4SAndy Adamson 	[OP_EXCHANGE_ID] = {
1859069b6ad4SAndy Adamson 		.op_func = (nfsd4op_func)nfsd4_exchange_id,
186058e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
186158e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
1862069b6ad4SAndy Adamson 		.op_name = "OP_EXCHANGE_ID",
186358e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_exchange_id_rsize,
1864069b6ad4SAndy Adamson 	},
1865cb73a9f4SJ. Bruce Fields 	[OP_BACKCHANNEL_CTL] = {
1866cb73a9f4SJ. Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_backchannel_ctl,
1867cb73a9f4SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
1868cb73a9f4SJ. Bruce Fields 		.op_name = "OP_BACKCHANNEL_CTL",
1869cb73a9f4SJ. Bruce Fields 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
1870cb73a9f4SJ. Bruce Fields 	},
18711d1bc8f2SJ. Bruce Fields 	[OP_BIND_CONN_TO_SESSION] = {
18721d1bc8f2SJ. Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_bind_conn_to_session,
187358e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
187458e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
18751d1bc8f2SJ. Bruce Fields 		.op_name = "OP_BIND_CONN_TO_SESSION",
187658e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_bind_conn_to_session_rsize,
18771d1bc8f2SJ. Bruce Fields 	},
1878069b6ad4SAndy Adamson 	[OP_CREATE_SESSION] = {
1879069b6ad4SAndy Adamson 		.op_func = (nfsd4op_func)nfsd4_create_session,
188058e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
188158e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
1882069b6ad4SAndy Adamson 		.op_name = "OP_CREATE_SESSION",
188358e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_create_session_rsize,
1884069b6ad4SAndy Adamson 	},
1885069b6ad4SAndy Adamson 	[OP_DESTROY_SESSION] = {
1886069b6ad4SAndy Adamson 		.op_func = (nfsd4op_func)nfsd4_destroy_session,
188758e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
188858e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
1889069b6ad4SAndy Adamson 		.op_name = "OP_DESTROY_SESSION",
189058e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
1891069b6ad4SAndy Adamson 	},
1892069b6ad4SAndy Adamson 	[OP_SEQUENCE] = {
1893069b6ad4SAndy Adamson 		.op_func = (nfsd4op_func)nfsd4_sequence,
1894f9bb94c4SAndy Adamson 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
1895069b6ad4SAndy Adamson 		.op_name = "OP_SEQUENCE",
1896069b6ad4SAndy Adamson 	},
1897094b5d74SBenny Halevy 	[OP_DESTROY_CLIENTID] = {
1898345c2842SMi Jinlong 		.op_func = (nfsd4op_func)nfsd4_destroy_clientid,
189958e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
190058e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
1901094b5d74SBenny Halevy 		.op_name = "OP_DESTROY_CLIENTID",
190258e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
1903094b5d74SBenny Halevy 	},
19044dc6ec00SJ. Bruce Fields 	[OP_RECLAIM_COMPLETE] = {
19054dc6ec00SJ. Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_reclaim_complete,
190658e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
19074dc6ec00SJ. Bruce Fields 		.op_name = "OP_RECLAIM_COMPLETE",
190858e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
19094dc6ec00SJ. Bruce Fields 	},
191004f4ad16SJ. Bruce Fields 	[OP_SECINFO_NO_NAME] = {
191104f4ad16SJ. Bruce Fields 		.op_func = (nfsd4op_func)nfsd4_secinfo_no_name,
191268d93184SJ. Bruce Fields 		.op_flags = OP_HANDLES_WRONGSEC,
191304f4ad16SJ. Bruce Fields 		.op_name = "OP_SECINFO_NO_NAME",
191404f4ad16SJ. Bruce Fields 	},
191517456804SBryan Schumaker 	[OP_TEST_STATEID] = {
191617456804SBryan Schumaker 		.op_func = (nfsd4op_func)nfsd4_test_stateid,
191717456804SBryan Schumaker 		.op_flags = ALLOWED_WITHOUT_FH,
191817456804SBryan Schumaker 		.op_name = "OP_TEST_STATEID",
191917456804SBryan Schumaker 	},
1920e1ca12dfSBryan Schumaker 	[OP_FREE_STATEID] = {
1921e1ca12dfSBryan Schumaker 		.op_func = (nfsd4op_func)nfsd4_free_stateid,
192258e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
1923e1ca12dfSBryan Schumaker 		.op_name = "OP_FREE_STATEID",
1924a1dc6955SJ. Bruce Fields 		.op_get_currentstateid = (stateid_getter)nfsd4_get_freestateid,
192558e7b33aSMi Jinlong 		.op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
1926e1ca12dfSBryan Schumaker 	},
1927*24bab491SAnna Schumaker 
1928*24bab491SAnna Schumaker 	/* NFSv4.2 operations */
1929*24bab491SAnna Schumaker 	[OP_SEEK] = {
1930*24bab491SAnna Schumaker 		.op_func = (nfsd4op_func)nfsd4_seek,
1931*24bab491SAnna Schumaker 		.op_name = "OP_SEEK",
1932*24bab491SAnna Schumaker 	},
1933b591480bSJ.Bruce Fields };
1934b591480bSJ.Bruce Fields 
19354f0cefbfSJ. Bruce Fields int nfsd4_max_reply(struct svc_rqst *rqstp, struct nfsd4_op *op)
19364f0cefbfSJ. Bruce Fields {
19374f0cefbfSJ. Bruce Fields 	struct nfsd4_operation *opdesc;
19384f0cefbfSJ. Bruce Fields 	nfsd4op_rsize estimator;
19394f0cefbfSJ. Bruce Fields 
19404f0cefbfSJ. Bruce Fields 	if (op->opnum == OP_ILLEGAL)
19414f0cefbfSJ. Bruce Fields 		return op_encode_hdr_size * sizeof(__be32);
19424f0cefbfSJ. Bruce Fields 	opdesc = OPDESC(op);
19434f0cefbfSJ. Bruce Fields 	estimator = opdesc->op_rsize_bop;
19444f0cefbfSJ. Bruce Fields 	return estimator ? estimator(rqstp, op) : PAGE_SIZE;
19454f0cefbfSJ. Bruce Fields }
19464f0cefbfSJ. Bruce Fields 
194707d1f802SJ. Bruce Fields void warn_on_nonidempotent_op(struct nfsd4_op *op)
194807d1f802SJ. Bruce Fields {
194907d1f802SJ. Bruce Fields 	if (OPDESC(op)->op_flags & OP_MODIFIES_SOMETHING) {
195007d1f802SJ. Bruce Fields 		pr_err("unable to encode reply to nonidempotent op %d (%s)\n",
195107d1f802SJ. Bruce Fields 			op->opnum, nfsd4_op_name(op->opnum));
195207d1f802SJ. Bruce Fields 		WARN_ON_ONCE(1);
195307d1f802SJ. Bruce Fields 	}
195407d1f802SJ. Bruce Fields }
195507d1f802SJ. Bruce Fields 
1956f1c7f79bSAdrian Bunk static const char *nfsd4_op_name(unsigned opnum)
1957b001a1b6SBenny Halevy {
1958b001a1b6SBenny Halevy 	if (opnum < ARRAY_SIZE(nfsd4_ops))
1959b001a1b6SBenny Halevy 		return nfsd4_ops[opnum].op_name;
1960b001a1b6SBenny Halevy 	return "unknown_operation";
1961b001a1b6SBenny Halevy }
1962b001a1b6SBenny Halevy 
19631da177e4SLinus Torvalds #define nfsd4_voidres			nfsd4_voidargs
19641da177e4SLinus Torvalds struct nfsd4_voidargs { int dummy; };
19651da177e4SLinus Torvalds 
19661da177e4SLinus Torvalds static struct svc_procedure		nfsd_procedures4[2] = {
19670a93a47fSYu Zhiguo 	[NFSPROC4_NULL] = {
19680a93a47fSYu Zhiguo 		.pc_func = (svc_procfunc) nfsd4_proc_null,
19690a93a47fSYu Zhiguo 		.pc_encode = (kxdrproc_t) nfs4svc_encode_voidres,
19700a93a47fSYu Zhiguo 		.pc_argsize = sizeof(struct nfsd4_voidargs),
19710a93a47fSYu Zhiguo 		.pc_ressize = sizeof(struct nfsd4_voidres),
19720a93a47fSYu Zhiguo 		.pc_cachetype = RC_NOCACHE,
19730a93a47fSYu Zhiguo 		.pc_xdrressize = 1,
19740a93a47fSYu Zhiguo 	},
19750a93a47fSYu Zhiguo 	[NFSPROC4_COMPOUND] = {
19760a93a47fSYu Zhiguo 		.pc_func = (svc_procfunc) nfsd4_proc_compound,
19770a93a47fSYu Zhiguo 		.pc_decode = (kxdrproc_t) nfs4svc_decode_compoundargs,
19780a93a47fSYu Zhiguo 		.pc_encode = (kxdrproc_t) nfs4svc_encode_compoundres,
19790a93a47fSYu Zhiguo 		.pc_argsize = sizeof(struct nfsd4_compoundargs),
19800a93a47fSYu Zhiguo 		.pc_ressize = sizeof(struct nfsd4_compoundres),
19813e98abffSJ. Bruce Fields 		.pc_release = nfsd4_release_compoundargs,
19820a93a47fSYu Zhiguo 		.pc_cachetype = RC_NOCACHE,
19830a93a47fSYu Zhiguo 		.pc_xdrressize = NFSD_BUFSIZE/4,
19840a93a47fSYu Zhiguo 	},
19851da177e4SLinus Torvalds };
19861da177e4SLinus Torvalds 
19871da177e4SLinus Torvalds struct svc_version	nfsd_version4 = {
19881da177e4SLinus Torvalds 		.vs_vers	= 4,
19891da177e4SLinus Torvalds 		.vs_nproc	= 2,
19901da177e4SLinus Torvalds 		.vs_proc	= nfsd_procedures4,
19911da177e4SLinus Torvalds 		.vs_dispatch	= nfsd_dispatch,
19921da177e4SLinus Torvalds 		.vs_xdrsize	= NFS4_SVC_XDRSIZE,
19937e55b59bSKinglong Mee 		.vs_rpcb_optnl	= 1,
19941da177e4SLinus Torvalds };
19951da177e4SLinus Torvalds 
19961da177e4SLinus Torvalds /*
19971da177e4SLinus Torvalds  * Local variables:
19981da177e4SLinus Torvalds  *  c-basic-offset: 8
19991da177e4SLinus Torvalds  * End:
20001da177e4SLinus Torvalds  */
2001