xref: /openbmc/linux/fs/nfsd/nfs4proc.c (revision eb69853d)
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>
36b0cb9085SAnna Schumaker #include <linux/falloc.h>
375a0e3ad6STejun Heo #include <linux/slab.h>
381da177e4SLinus Torvalds 
3958e7b33aSMi Jinlong #include "idmap.h"
409a74af21SBoaz Harrosh #include "cache.h"
419a74af21SBoaz Harrosh #include "xdr4.h"
420a3adadeSJ. Bruce Fields #include "vfs.h"
438b70484cSTigran Mkrtchyan #include "current_stateid.h"
443320fef1SStanislav Kinsbursky #include "netns.h"
454ac7249eSChristoph Hellwig #include "acl.h"
469cf514ccSChristoph Hellwig #include "pnfs.h"
4731ef83dcSChristoph Hellwig #include "trace.h"
481da177e4SLinus Torvalds 
4918032ca0SDavid Quigley #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
5018032ca0SDavid Quigley #include <linux/security.h>
5118032ca0SDavid Quigley 
5218032ca0SDavid Quigley static inline void
5318032ca0SDavid Quigley nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
5418032ca0SDavid Quigley {
552b0143b5SDavid Howells 	struct inode *inode = d_inode(resfh->fh_dentry);
5618032ca0SDavid Quigley 	int status;
5718032ca0SDavid Quigley 
585955102cSAl Viro 	inode_lock(inode);
5918032ca0SDavid Quigley 	status = security_inode_setsecctx(resfh->fh_dentry,
6018032ca0SDavid Quigley 		label->data, label->len);
615955102cSAl Viro 	inode_unlock(inode);
6218032ca0SDavid Quigley 
6318032ca0SDavid Quigley 	if (status)
6418032ca0SDavid Quigley 		/*
6518032ca0SDavid Quigley 		 * XXX: We should really fail the whole open, but we may
6618032ca0SDavid Quigley 		 * already have created a new file, so it may be too
6718032ca0SDavid Quigley 		 * late.  For now this seems the least of evils:
6818032ca0SDavid Quigley 		 */
6918032ca0SDavid Quigley 		bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
7018032ca0SDavid Quigley 
7118032ca0SDavid Quigley 	return;
7218032ca0SDavid Quigley }
7318032ca0SDavid Quigley #else
7418032ca0SDavid Quigley static inline void
7518032ca0SDavid Quigley nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
7618032ca0SDavid Quigley { }
7718032ca0SDavid Quigley #endif
7818032ca0SDavid Quigley 
791da177e4SLinus Torvalds #define NFSDDBG_FACILITY		NFSDDBG_PROC
801da177e4SLinus Torvalds 
813c8e0316SYu Zhiguo static u32 nfsd_attrmask[] = {
823c8e0316SYu Zhiguo 	NFSD_WRITEABLE_ATTRS_WORD0,
833c8e0316SYu Zhiguo 	NFSD_WRITEABLE_ATTRS_WORD1,
843c8e0316SYu Zhiguo 	NFSD_WRITEABLE_ATTRS_WORD2
853c8e0316SYu Zhiguo };
863c8e0316SYu Zhiguo 
873c8e0316SYu Zhiguo static u32 nfsd41_ex_attrmask[] = {
883c8e0316SYu Zhiguo 	NFSD_SUPPATTR_EXCLCREAT_WORD0,
893c8e0316SYu Zhiguo 	NFSD_SUPPATTR_EXCLCREAT_WORD1,
903c8e0316SYu Zhiguo 	NFSD_SUPPATTR_EXCLCREAT_WORD2
913c8e0316SYu Zhiguo };
923c8e0316SYu Zhiguo 
933c8e0316SYu Zhiguo static __be32
943c8e0316SYu Zhiguo check_attr_support(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
953c8e0316SYu Zhiguo 		   u32 *bmval, u32 *writable)
963c8e0316SYu Zhiguo {
973c8e0316SYu Zhiguo 	struct dentry *dentry = cstate->current_fh.fh_dentry;
9832ddd944SJ. Bruce Fields 	struct svc_export *exp = cstate->current_fh.fh_export;
993c8e0316SYu Zhiguo 
100916d2d84SJ. Bruce Fields 	if (!nfsd_attrs_supported(cstate->minorversion, bmval))
1013c8e0316SYu Zhiguo 		return nfserr_attrnotsupp;
102916d2d84SJ. Bruce Fields 	if ((bmval[0] & FATTR4_WORD0_ACL) && !IS_POSIXACL(d_inode(dentry)))
1033c8e0316SYu Zhiguo 		return nfserr_attrnotsupp;
10432ddd944SJ. Bruce Fields 	if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) &&
10532ddd944SJ. Bruce Fields 			!(exp->ex_flags & NFSEXP_SECURITY_LABEL))
10632ddd944SJ. Bruce Fields 		return nfserr_attrnotsupp;
107916d2d84SJ. Bruce Fields 	if (writable && !bmval_is_subset(bmval, writable))
1083c8e0316SYu Zhiguo 		return nfserr_inval;
10947057abdSAndreas Gruenbacher 	if (writable && (bmval[2] & FATTR4_WORD2_MODE_UMASK) &&
11047057abdSAndreas Gruenbacher 			(bmval[1] & FATTR4_WORD1_MODE))
11147057abdSAndreas Gruenbacher 		return nfserr_inval;
1123c8e0316SYu Zhiguo 	return nfs_ok;
1133c8e0316SYu Zhiguo }
1143c8e0316SYu Zhiguo 
1153c8e0316SYu Zhiguo static __be32
1163c8e0316SYu Zhiguo nfsd4_check_open_attributes(struct svc_rqst *rqstp,
1173c8e0316SYu Zhiguo 	struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
1183c8e0316SYu Zhiguo {
1193c8e0316SYu Zhiguo 	__be32 status = nfs_ok;
1203c8e0316SYu Zhiguo 
1213c8e0316SYu Zhiguo 	if (open->op_create == NFS4_OPEN_CREATE) {
1223c8e0316SYu Zhiguo 		if (open->op_createmode == NFS4_CREATE_UNCHECKED
1233c8e0316SYu Zhiguo 		    || open->op_createmode == NFS4_CREATE_GUARDED)
1243c8e0316SYu Zhiguo 			status = check_attr_support(rqstp, cstate,
1253c8e0316SYu Zhiguo 					open->op_bmval, nfsd_attrmask);
1263c8e0316SYu Zhiguo 		else if (open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1)
1273c8e0316SYu Zhiguo 			status = check_attr_support(rqstp, cstate,
1283c8e0316SYu Zhiguo 					open->op_bmval, nfsd41_ex_attrmask);
1293c8e0316SYu Zhiguo 	}
1303c8e0316SYu Zhiguo 
1313c8e0316SYu Zhiguo 	return status;
1323c8e0316SYu Zhiguo }
1333c8e0316SYu Zhiguo 
1349208faf2SYu Zhiguo static int
1359208faf2SYu Zhiguo is_create_with_attrs(struct nfsd4_open *open)
1369208faf2SYu Zhiguo {
1379208faf2SYu Zhiguo 	return open->op_create == NFS4_OPEN_CREATE
1389208faf2SYu Zhiguo 		&& (open->op_createmode == NFS4_CREATE_UNCHECKED
1399208faf2SYu Zhiguo 		    || open->op_createmode == NFS4_CREATE_GUARDED
1409208faf2SYu Zhiguo 		    || open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1);
1419208faf2SYu Zhiguo }
1429208faf2SYu Zhiguo 
1439208faf2SYu Zhiguo /*
1449208faf2SYu Zhiguo  * if error occurs when setting the acl, just clear the acl bit
1459208faf2SYu Zhiguo  * in the returned attr bitmap.
1469208faf2SYu Zhiguo  */
1479208faf2SYu Zhiguo static void
1489208faf2SYu Zhiguo do_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
1499208faf2SYu Zhiguo 		struct nfs4_acl *acl, u32 *bmval)
1509208faf2SYu Zhiguo {
1519208faf2SYu Zhiguo 	__be32 status;
1529208faf2SYu Zhiguo 
1539208faf2SYu Zhiguo 	status = nfsd4_set_nfs4_acl(rqstp, fhp, acl);
1549208faf2SYu Zhiguo 	if (status)
1559208faf2SYu Zhiguo 		/*
1569208faf2SYu Zhiguo 		 * We should probably fail the whole open at this point,
1579208faf2SYu Zhiguo 		 * but we've already created the file, so it's too late;
1589208faf2SYu Zhiguo 		 * So this seems the least of evils:
1599208faf2SYu Zhiguo 		 */
1609208faf2SYu Zhiguo 		bmval[0] &= ~FATTR4_WORD0_ACL;
1619208faf2SYu Zhiguo }
1629208faf2SYu Zhiguo 
1631da177e4SLinus Torvalds static inline void
1641da177e4SLinus Torvalds fh_dup2(struct svc_fh *dst, struct svc_fh *src)
1651da177e4SLinus Torvalds {
1661da177e4SLinus Torvalds 	fh_put(dst);
1671da177e4SLinus Torvalds 	dget(src->fh_dentry);
1681da177e4SLinus Torvalds 	if (src->fh_export)
169bf18f163SKinglong Mee 		exp_get(src->fh_export);
1701da177e4SLinus Torvalds 	*dst = *src;
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds 
173b37ad28bSAl Viro static __be32
174dc730e17SJ. Bruce Fields do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, int accmode)
1751da177e4SLinus Torvalds {
176b37ad28bSAl Viro 	__be32 status;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	if (open->op_truncate &&
1791da177e4SLinus Torvalds 		!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
1801da177e4SLinus Torvalds 		return nfserr_inval;
1811da177e4SLinus Torvalds 
182a043226bSJ. Bruce Fields 	accmode |= NFSD_MAY_READ_IF_EXEC;
183a043226bSJ. Bruce Fields 
1841da177e4SLinus Torvalds 	if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
1858837abcaSMiklos Szeredi 		accmode |= NFSD_MAY_READ;
1869801d8a3SJ. Bruce Fields 	if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1878837abcaSMiklos Szeredi 		accmode |= (NFSD_MAY_WRITE | NFSD_MAY_TRUNC);
18857ecb34fSJ. Bruce Fields 	if (open->op_share_deny & NFS4_SHARE_DENY_READ)
1898837abcaSMiklos Szeredi 		accmode |= NFSD_MAY_WRITE;
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 	status = fh_verify(rqstp, current_fh, S_IFREG, accmode);
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	return status;
1941da177e4SLinus Torvalds }
1951da177e4SLinus Torvalds 
196aadab6c6SJ. Bruce Fields static __be32 nfsd_check_obj_isreg(struct svc_fh *fh)
197aadab6c6SJ. Bruce Fields {
1982b0143b5SDavid Howells 	umode_t mode = d_inode(fh->fh_dentry)->i_mode;
199aadab6c6SJ. Bruce Fields 
200aadab6c6SJ. Bruce Fields 	if (S_ISREG(mode))
201aadab6c6SJ. Bruce Fields 		return nfs_ok;
202aadab6c6SJ. Bruce Fields 	if (S_ISDIR(mode))
203aadab6c6SJ. Bruce Fields 		return nfserr_isdir;
204aadab6c6SJ. Bruce Fields 	/*
205aadab6c6SJ. Bruce Fields 	 * Using err_symlink as our catch-all case may look odd; but
206aadab6c6SJ. Bruce Fields 	 * there's no other obvious error for this case in 4.0, and we
207aadab6c6SJ. Bruce Fields 	 * happen to know that it will cause the linux v4 client to do
208aadab6c6SJ. Bruce Fields 	 * the right thing on attempts to open something other than a
209aadab6c6SJ. Bruce Fields 	 * regular file.
210aadab6c6SJ. Bruce Fields 	 */
211aadab6c6SJ. Bruce Fields 	return nfserr_symlink;
212aadab6c6SJ. Bruce Fields }
213aadab6c6SJ. Bruce Fields 
214bbc9c36cSJ. Bruce Fields static void nfsd4_set_open_owner_reply_cache(struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh *resfh)
2151da177e4SLinus Torvalds {
216bbc9c36cSJ. Bruce Fields 	if (nfsd4_has_session(cstate))
217bbc9c36cSJ. Bruce Fields 		return;
218bbc9c36cSJ. Bruce Fields 	fh_copy_shallow(&open->op_openowner->oo_owner.so_replay.rp_openfh,
219bbc9c36cSJ. Bruce Fields 			&resfh->fh_handle);
220bbc9c36cSJ. Bruce Fields }
221bbc9c36cSJ. Bruce Fields 
222bbc9c36cSJ. Bruce Fields static __be32
223c0e6bee4SJ. Bruce Fields do_open_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh **resfh)
224bbc9c36cSJ. Bruce Fields {
225bbc9c36cSJ. Bruce Fields 	struct svc_fh *current_fh = &cstate->current_fh;
2267007c90fSNeil Brown 	int accmode;
227b37ad28bSAl Viro 	__be32 status;
2281da177e4SLinus Torvalds 
229c0e6bee4SJ. Bruce Fields 	*resfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
230c0e6bee4SJ. Bruce Fields 	if (!*resfh)
23159deeb9eSJ. Bruce Fields 		return nfserr_jukebox;
232c0e6bee4SJ. Bruce Fields 	fh_init(*resfh, NFS4_FHSIZE);
2331da177e4SLinus Torvalds 	open->op_truncate = 0;
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	if (open->op_create) {
23679fb54abSBenny Halevy 		/* FIXME: check session persistence and pnfs flags.
23779fb54abSBenny Halevy 		 * The nfsv4.1 spec requires the following semantics:
23879fb54abSBenny Halevy 		 *
23979fb54abSBenny Halevy 		 * Persistent   | pNFS   | Server REQUIRED | Client Allowed
24079fb54abSBenny Halevy 		 * Reply Cache  | server |                 |
24179fb54abSBenny Halevy 		 * -------------+--------+-----------------+--------------------
24279fb54abSBenny Halevy 		 * no           | no     | EXCLUSIVE4_1    | EXCLUSIVE4_1
24379fb54abSBenny Halevy 		 *              |        |                 | (SHOULD)
24479fb54abSBenny Halevy 		 *              |        | and EXCLUSIVE4  | or EXCLUSIVE4
24579fb54abSBenny Halevy 		 *              |        |                 | (SHOULD NOT)
24679fb54abSBenny Halevy 		 * no           | yes    | EXCLUSIVE4_1    | EXCLUSIVE4_1
24779fb54abSBenny Halevy 		 * yes          | no     | GUARDED4        | GUARDED4
24879fb54abSBenny Halevy 		 * yes          | yes    | GUARDED4        | GUARDED4
24979fb54abSBenny Halevy 		 */
25079fb54abSBenny Halevy 
2511da177e4SLinus Torvalds 		/*
2521da177e4SLinus Torvalds 		 * Note: create modes (UNCHECKED,GUARDED...) are the same
253ac6721a1SMi Jinlong 		 * in NFSv4 as in v3 except EXCLUSIVE4_1.
2541da177e4SLinus Torvalds 		 */
255ac6721a1SMi Jinlong 		status = do_nfsd_create(rqstp, current_fh, open->op_fname.data,
2561da177e4SLinus Torvalds 					open->op_fname.len, &open->op_iattr,
257c0e6bee4SJ. Bruce Fields 					*resfh, open->op_createmode,
258749997e5SJeff Layton 					(u32 *)open->op_verf.data,
259856121b2SJ. Bruce Fields 					&open->op_truncate, &open->op_created);
260749997e5SJeff Layton 
26118032ca0SDavid Quigley 		if (!status && open->op_label.len)
262c0e6bee4SJ. Bruce Fields 			nfsd4_security_inode_setsecctx(*resfh, &open->op_label, open->op_bmval);
26318032ca0SDavid Quigley 
26499f88726SJ. Bruce Fields 		/*
265ead8fb8cSKinglong Mee 		 * Following rfc 3530 14.2.16, and rfc 5661 18.16.4
266ead8fb8cSKinglong Mee 		 * use the returned bitmask to indicate which attributes
267ead8fb8cSKinglong Mee 		 * we used to store the verifier:
268749997e5SJeff Layton 		 */
269ead8fb8cSKinglong Mee 		if (nfsd_create_is_exclusive(open->op_createmode) && status == 0)
270ead8fb8cSKinglong Mee 			open->op_bmval[1] |= (FATTR4_WORD1_TIME_ACCESS |
271749997e5SJeff Layton 						FATTR4_WORD1_TIME_MODIFY);
2724335723eSJ. Bruce Fields 	} else
2734335723eSJ. Bruce Fields 		/*
2744335723eSJ. Bruce Fields 		 * Note this may exit with the parent still locked.
2754335723eSJ. Bruce Fields 		 * We will hold the lock until nfsd4_open's final
2764335723eSJ. Bruce Fields 		 * lookup, to prevent renames or unlinks until we've had
2774335723eSJ. Bruce Fields 		 * a chance to an acquire a delegation if appropriate.
2784335723eSJ. Bruce Fields 		 */
2791da177e4SLinus Torvalds 		status = nfsd_lookup(rqstp, current_fh,
280c0e6bee4SJ. Bruce Fields 				     open->op_fname.data, open->op_fname.len, *resfh);
281aadab6c6SJ. Bruce Fields 	if (status)
282aadab6c6SJ. Bruce Fields 		goto out;
283c0e6bee4SJ. Bruce Fields 	status = nfsd_check_obj_isreg(*resfh);
284af85852dSJ. Bruce Fields 	if (status)
285af85852dSJ. Bruce Fields 		goto out;
2861da177e4SLinus Torvalds 
2879208faf2SYu Zhiguo 	if (is_create_with_attrs(open) && open->op_acl != NULL)
288c0e6bee4SJ. Bruce Fields 		do_set_nfs4_acl(rqstp, *resfh, open->op_acl, open->op_bmval);
2899208faf2SYu Zhiguo 
290c0e6bee4SJ. Bruce Fields 	nfsd4_set_open_owner_reply_cache(cstate, open, *resfh);
2917007c90fSNeil Brown 	accmode = NFSD_MAY_NOP;
29289f6c336SJ. Bruce Fields 	if (open->op_created ||
29389f6c336SJ. Bruce Fields 			open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2947007c90fSNeil Brown 		accmode |= NFSD_MAY_OWNER_OVERRIDE;
295c0e6bee4SJ. Bruce Fields 	status = do_open_permission(rqstp, *resfh, open, accmode);
29641fd1e42SJ. Bruce Fields 	set_change_info(&open->op_cinfo, current_fh);
297af85852dSJ. Bruce Fields out:
2981da177e4SLinus Torvalds 	return status;
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
301b37ad28bSAl Viro static __be32
302bbc9c36cSJ. Bruce Fields do_open_fhandle(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
3031da177e4SLinus Torvalds {
304bbc9c36cSJ. Bruce Fields 	struct svc_fh *current_fh = &cstate->current_fh;
305b37ad28bSAl Viro 	__be32 status;
3069f415eb2SJ. Bruce Fields 	int accmode = 0;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	/* We don't know the target directory, and therefore can not
3091da177e4SLinus Torvalds 	* set the change info
3101da177e4SLinus Torvalds 	*/
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
3131da177e4SLinus Torvalds 
314bbc9c36cSJ. Bruce Fields 	nfsd4_set_open_owner_reply_cache(cstate, open, current_fh);
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 	open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
3171da177e4SLinus Torvalds 		(open->op_iattr.ia_size == 0);
3189f415eb2SJ. Bruce Fields 	/*
3199f415eb2SJ. Bruce Fields 	 * In the delegation case, the client is telling us about an
3209f415eb2SJ. Bruce Fields 	 * open that it *already* performed locally, some time ago.  We
3219f415eb2SJ. Bruce Fields 	 * should let it succeed now if possible.
3229f415eb2SJ. Bruce Fields 	 *
3239f415eb2SJ. Bruce Fields 	 * In the case of a CLAIM_FH open, on the other hand, the client
3249f415eb2SJ. Bruce Fields 	 * may be counting on us to enforce permissions (the Linux 4.1
3259f415eb2SJ. Bruce Fields 	 * client uses this for normal opens, for example).
3269f415eb2SJ. Bruce Fields 	 */
3279f415eb2SJ. Bruce Fields 	if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH)
3289f415eb2SJ. Bruce Fields 		accmode = NFSD_MAY_OWNER_OVERRIDE;
3291da177e4SLinus Torvalds 
3309f415eb2SJ. Bruce Fields 	status = do_open_permission(rqstp, current_fh, open, accmode);
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	return status;
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds 
33560adfc50SAndy Adamson static void
33660adfc50SAndy Adamson copy_clientid(clientid_t *clid, struct nfsd4_session *session)
33760adfc50SAndy Adamson {
33860adfc50SAndy Adamson 	struct nfsd4_sessionid *sid =
33960adfc50SAndy Adamson 			(struct nfsd4_sessionid *)session->se_sessionid.data;
34060adfc50SAndy Adamson 
34160adfc50SAndy Adamson 	clid->cl_boot = sid->clientid.cl_boot;
34260adfc50SAndy Adamson 	clid->cl_id = sid->clientid.cl_id;
34360adfc50SAndy Adamson }
3441da177e4SLinus Torvalds 
3457191155bSJ.Bruce Fields static __be32
346ca364317SJ.Bruce Fields nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
347eb69853dSChristoph Hellwig 	   union nfsd4_op_u *u)
3481da177e4SLinus Torvalds {
349eb69853dSChristoph Hellwig 	struct nfsd4_open *open = &u->open;
350b37ad28bSAl Viro 	__be32 status;
351c0e6bee4SJ. Bruce Fields 	struct svc_fh *resfh = NULL;
3523320fef1SStanislav Kinsbursky 	struct net *net = SVC_NET(rqstp);
3533320fef1SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3546668958fSAndy Adamson 
355fe0750e5SJ. Bruce Fields 	dprintk("NFSD: nfsd4_open filename %.*s op_openowner %p\n",
3561da177e4SLinus Torvalds 		(int)open->op_fname.len, open->op_fname.data,
357fe0750e5SJ. Bruce Fields 		open->op_openowner);
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	/* This check required by spec. */
3601da177e4SLinus Torvalds 	if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
3611da177e4SLinus Torvalds 		return nfserr_inval;
3621da177e4SLinus Torvalds 
363856121b2SJ. Bruce Fields 	open->op_created = 0;
364ab1350b2SMi Jinlong 	/*
365ab1350b2SMi Jinlong 	 * RFC5661 18.51.3
366ab1350b2SMi Jinlong 	 * Before RECLAIM_COMPLETE done, server should deny new lock
367ab1350b2SMi Jinlong 	 */
368ab1350b2SMi Jinlong 	if (nfsd4_has_session(cstate) &&
369a52d726bSJeff Layton 	    !test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
370a52d726bSJeff Layton 		      &cstate->session->se_client->cl_flags) &&
371ab1350b2SMi Jinlong 	    open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
372ab1350b2SMi Jinlong 		return nfserr_grace;
373ab1350b2SMi Jinlong 
37460adfc50SAndy Adamson 	if (nfsd4_has_session(cstate))
37560adfc50SAndy Adamson 		copy_clientid(&open->op_clientid, cstate->session);
37660adfc50SAndy Adamson 
3771da177e4SLinus Torvalds 	/* check seqid for replay. set nfs4_owner */
3786cd22668SKinglong Mee 	status = nfsd4_process_open1(cstate, open, nn);
379a90b061cSAl Viro 	if (status == nfserr_replay_me) {
380fe0750e5SJ. Bruce Fields 		struct nfs4_replay *rp = &open->op_openowner->oo_owner.so_replay;
381ca364317SJ.Bruce Fields 		fh_put(&cstate->current_fh);
382a4773c08SJ. Bruce Fields 		fh_copy_shallow(&cstate->current_fh.fh_handle,
383a4773c08SJ. Bruce Fields 				&rp->rp_openfh);
3848837abcaSMiklos Szeredi 		status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
3851da177e4SLinus Torvalds 		if (status)
3861da177e4SLinus Torvalds 			dprintk("nfsd4_open: replay failed"
3871da177e4SLinus Torvalds 				" restoring previous filehandle\n");
3881da177e4SLinus Torvalds 		else
389a90b061cSAl Viro 			status = nfserr_replay_me;
3901da177e4SLinus Torvalds 	}
3911da177e4SLinus Torvalds 	if (status)
3921da177e4SLinus Torvalds 		goto out;
3939d313b17SJ. Bruce Fields 	if (open->op_xdr_error) {
3949d313b17SJ. Bruce Fields 		status = open->op_xdr_error;
3959d313b17SJ. Bruce Fields 		goto out;
3969d313b17SJ. Bruce Fields 	}
397fb553c0fSJ. Bruce Fields 
3983c8e0316SYu Zhiguo 	status = nfsd4_check_open_attributes(rqstp, cstate, open);
3993c8e0316SYu Zhiguo 	if (status)
4003c8e0316SYu Zhiguo 		goto out;
4013c8e0316SYu Zhiguo 
402fb553c0fSJ. Bruce Fields 	/* Openowner is now set, so sequence id will get bumped.  Now we need
403fb553c0fSJ. Bruce Fields 	 * these checks before we do any creates: */
404cbd0d51aSJ. Bruce Fields 	status = nfserr_grace;
405c87fb4a3SJ. Bruce Fields 	if (opens_in_grace(net) && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
406cbd0d51aSJ. Bruce Fields 		goto out;
407cbd0d51aSJ. Bruce Fields 	status = nfserr_no_grace;
408c87fb4a3SJ. Bruce Fields 	if (!opens_in_grace(net) && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
409cbd0d51aSJ. Bruce Fields 		goto out;
410fb553c0fSJ. Bruce Fields 
4111da177e4SLinus Torvalds 	switch (open->op_claim_type) {
4120dd3c192SNeilBrown 		case NFS4_OPEN_CLAIM_DELEGATE_CUR:
4131da177e4SLinus Torvalds 		case NFS4_OPEN_CLAIM_NULL:
414c0e6bee4SJ. Bruce Fields 			status = do_open_lookup(rqstp, cstate, open, &resfh);
4151da177e4SLinus Torvalds 			if (status)
4161da177e4SLinus Torvalds 				goto out;
4171da177e4SLinus Torvalds 			break;
4181da177e4SLinus Torvalds 		case NFS4_OPEN_CLAIM_PREVIOUS:
4193320fef1SStanislav Kinsbursky 			status = nfs4_check_open_reclaim(&open->op_clientid,
4200fe492dbSTrond Myklebust 							 cstate, nn);
4210cf99b91SMi Jinlong 			if (status)
4220cf99b91SMi Jinlong 				goto out;
423ba5378b6SJeff Layton 			open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4248b289b2cSJ. Bruce Fields 		case NFS4_OPEN_CLAIM_FH:
4258b289b2cSJ. Bruce Fields 		case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
426bbc9c36cSJ. Bruce Fields 			status = do_open_fhandle(rqstp, cstate, open);
4271da177e4SLinus Torvalds 			if (status)
4281da177e4SLinus Torvalds 				goto out;
429c0e6bee4SJ. Bruce Fields 			resfh = &cstate->current_fh;
4301da177e4SLinus Torvalds 			break;
4318b289b2cSJ. Bruce Fields 		case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
4321da177e4SLinus Torvalds              	case NFS4_OPEN_CLAIM_DELEGATE_PREV:
4332fdada03SJ. Bruce Fields 			dprintk("NFSD: unsupported OPEN claim type %d\n",
4341da177e4SLinus Torvalds 				open->op_claim_type);
4351da177e4SLinus Torvalds 			status = nfserr_notsupp;
4361da177e4SLinus Torvalds 			goto out;
4371da177e4SLinus Torvalds 		default:
4382fdada03SJ. Bruce Fields 			dprintk("NFSD: Invalid OPEN claim type %d\n",
4391da177e4SLinus Torvalds 				open->op_claim_type);
4401da177e4SLinus Torvalds 			status = nfserr_inval;
4411da177e4SLinus Torvalds 			goto out;
4421da177e4SLinus Torvalds 	}
4431da177e4SLinus Torvalds 	/*
4441da177e4SLinus Torvalds 	 * nfsd4_process_open2() does the actual opening of the file.  If
4451da177e4SLinus Torvalds 	 * successful, it (1) truncates the file if open->op_truncate was
4461da177e4SLinus Torvalds 	 * set, (2) sets open->op_stateid, (3) sets open->op_delegation.
4471da177e4SLinus Torvalds 	 */
448c0e6bee4SJ. Bruce Fields 	status = nfsd4_process_open2(rqstp, resfh, open);
449b3fbfe0eSJeff Layton 	WARN(status && open->op_created,
450b3fbfe0eSJeff Layton 	     "nfsd4_process_open2 failed to open newly-created file! status=%u\n",
451b3fbfe0eSJeff Layton 	     be32_to_cpu(status));
4521da177e4SLinus Torvalds out:
453c0e6bee4SJ. Bruce Fields 	if (resfh && resfh != &cstate->current_fh) {
454c0e6bee4SJ. Bruce Fields 		fh_dup2(&cstate->current_fh, resfh);
455c0e6bee4SJ. Bruce Fields 		fh_put(resfh);
456c0e6bee4SJ. Bruce Fields 		kfree(resfh);
457c0e6bee4SJ. Bruce Fields 	}
45842297899SJeff Layton 	nfsd4_cleanup_open_state(cstate, open);
4599411b1d4SJ. Bruce Fields 	nfsd4_bump_seqid(cstate, status);
4601da177e4SLinus Torvalds 	return status;
4611da177e4SLinus Torvalds }
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds /*
4649d313b17SJ. Bruce Fields  * OPEN is the only seqid-mutating operation whose decoding can fail
4659d313b17SJ. Bruce Fields  * with a seqid-mutating error (specifically, decoding of user names in
4669d313b17SJ. Bruce Fields  * the attributes).  Therefore we have to do some processing to look up
4679d313b17SJ. Bruce Fields  * the stateowner so that we can bump the seqid.
4689d313b17SJ. Bruce Fields  */
4699d313b17SJ. Bruce Fields static __be32 nfsd4_open_omfg(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_op *op)
4709d313b17SJ. Bruce Fields {
471eb69853dSChristoph Hellwig 	struct nfsd4_open *open = &op->u.open;
4729d313b17SJ. Bruce Fields 
4739d313b17SJ. Bruce Fields 	if (!seqid_mutating_err(ntohl(op->status)))
4749d313b17SJ. Bruce Fields 		return op->status;
4759d313b17SJ. Bruce Fields 	if (nfsd4_has_session(cstate))
4769d313b17SJ. Bruce Fields 		return op->status;
4779d313b17SJ. Bruce Fields 	open->op_xdr_error = op->status;
478eb69853dSChristoph Hellwig 	return nfsd4_open(rqstp, cstate, &op->u);
4799d313b17SJ. Bruce Fields }
4809d313b17SJ. Bruce Fields 
4819d313b17SJ. Bruce Fields /*
4821da177e4SLinus Torvalds  * filehandle-manipulating ops.
4831da177e4SLinus Torvalds  */
4847191155bSJ.Bruce Fields static __be32
485b591480bSJ.Bruce Fields nfsd4_getfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
486eb69853dSChristoph Hellwig 	    union nfsd4_op_u *u)
4871da177e4SLinus Torvalds {
488ca364317SJ.Bruce Fields 	if (!cstate->current_fh.fh_dentry)
4891da177e4SLinus Torvalds 		return nfserr_nofilehandle;
4901da177e4SLinus Torvalds 
491eb69853dSChristoph Hellwig 	u->getfh = &cstate->current_fh;
4921da177e4SLinus Torvalds 	return nfs_ok;
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds 
4957191155bSJ.Bruce Fields static __be32
496ca364317SJ.Bruce Fields nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
497eb69853dSChristoph Hellwig 	    union nfsd4_op_u *u)
4981da177e4SLinus Torvalds {
499eb69853dSChristoph Hellwig 	struct nfsd4_putfh *putfh = &u->putfh;
500eb69853dSChristoph Hellwig 
501ca364317SJ.Bruce Fields 	fh_put(&cstate->current_fh);
502ca364317SJ.Bruce Fields 	cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
503ca364317SJ.Bruce Fields 	memcpy(&cstate->current_fh.fh_handle.fh_base, putfh->pf_fhval,
504ca364317SJ.Bruce Fields 	       putfh->pf_fhlen);
50568d93184SJ. Bruce Fields 	return fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_BYPASS_GSS);
5061da177e4SLinus Torvalds }
5071da177e4SLinus Torvalds 
5087191155bSJ.Bruce Fields static __be32
509b591480bSJ.Bruce Fields nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
510eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
5111da177e4SLinus Torvalds {
512b37ad28bSAl Viro 	__be32 status;
5131da177e4SLinus Torvalds 
514ca364317SJ.Bruce Fields 	fh_put(&cstate->current_fh);
515df547efbSJ. Bruce Fields 	status = exp_pseudoroot(rqstp, &cstate->current_fh);
5161da177e4SLinus Torvalds 	return status;
5171da177e4SLinus Torvalds }
5181da177e4SLinus Torvalds 
5197191155bSJ.Bruce Fields static __be32
520b591480bSJ.Bruce Fields nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
521eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
5221da177e4SLinus Torvalds {
523ca364317SJ.Bruce Fields 	if (!cstate->save_fh.fh_dentry)
5241da177e4SLinus Torvalds 		return nfserr_restorefh;
5251da177e4SLinus Torvalds 
526ca364317SJ.Bruce Fields 	fh_dup2(&cstate->current_fh, &cstate->save_fh);
52737c593c5STigran Mkrtchyan 	if (HAS_STATE_ID(cstate, SAVED_STATE_ID_FLAG)) {
52837c593c5STigran Mkrtchyan 		memcpy(&cstate->current_stateid, &cstate->save_stateid, sizeof(stateid_t));
52937c593c5STigran Mkrtchyan 		SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
53037c593c5STigran Mkrtchyan 	}
5311da177e4SLinus Torvalds 	return nfs_ok;
5321da177e4SLinus Torvalds }
5331da177e4SLinus Torvalds 
5347191155bSJ.Bruce Fields static __be32
535b591480bSJ.Bruce Fields nfsd4_savefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
536eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
5371da177e4SLinus Torvalds {
538ca364317SJ.Bruce Fields 	if (!cstate->current_fh.fh_dentry)
5391da177e4SLinus Torvalds 		return nfserr_nofilehandle;
5401da177e4SLinus Torvalds 
541ca364317SJ.Bruce Fields 	fh_dup2(&cstate->save_fh, &cstate->current_fh);
54237c593c5STigran Mkrtchyan 	if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG)) {
54337c593c5STigran Mkrtchyan 		memcpy(&cstate->save_stateid, &cstate->current_stateid, sizeof(stateid_t));
54437c593c5STigran Mkrtchyan 		SET_STATE_ID(cstate, SAVED_STATE_ID_FLAG);
54537c593c5STigran Mkrtchyan 	}
5461da177e4SLinus Torvalds 	return nfs_ok;
5471da177e4SLinus Torvalds }
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds /*
5501da177e4SLinus Torvalds  * misc nfsv4 ops
5511da177e4SLinus Torvalds  */
5527191155bSJ.Bruce Fields static __be32
553ca364317SJ.Bruce Fields nfsd4_access(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
554eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
5551da177e4SLinus Torvalds {
556eb69853dSChristoph Hellwig 	struct nfsd4_access *access = &u->access;
557eb69853dSChristoph Hellwig 
5581da177e4SLinus Torvalds 	if (access->ac_req_access & ~NFS3_ACCESS_FULL)
5591da177e4SLinus Torvalds 		return nfserr_inval;
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 	access->ac_resp_access = access->ac_req_access;
562ca364317SJ.Bruce Fields 	return nfsd_access(rqstp, &cstate->current_fh, &access->ac_resp_access,
563ca364317SJ.Bruce Fields 			   &access->ac_supported);
5641da177e4SLinus Torvalds }
5651da177e4SLinus Torvalds 
566b9c0ef85SStanislav Kinsbursky static void gen_boot_verifier(nfs4_verifier *verifier, struct net *net)
567ab4684d1SChuck Lever {
568ab4684d1SChuck Lever 	__be32 verf[2];
569b9c0ef85SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
570ab4684d1SChuck Lever 
571f419992cSJeff Layton 	/*
572f419992cSJeff Layton 	 * This is opaque to client, so no need to byte-swap. Use
573f419992cSJeff Layton 	 * __force to keep sparse happy
574f419992cSJeff Layton 	 */
575f419992cSJeff Layton 	verf[0] = (__force __be32)nn->nfssvc_boot.tv_sec;
576f419992cSJeff Layton 	verf[1] = (__force __be32)nn->nfssvc_boot.tv_usec;
577ab4684d1SChuck Lever 	memcpy(verifier->data, verf, sizeof(verifier->data));
578ab4684d1SChuck Lever }
579ab4684d1SChuck Lever 
5807191155bSJ.Bruce Fields static __be32
581ca364317SJ.Bruce Fields nfsd4_commit(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
582eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
5831da177e4SLinus Torvalds {
584eb69853dSChristoph Hellwig 	struct nfsd4_commit *commit = &u->commit;
585eb69853dSChristoph Hellwig 
586b9c0ef85SStanislav Kinsbursky 	gen_boot_verifier(&commit->co_verf, SVC_NET(rqstp));
58775c096f7SJ. Bruce Fields 	return nfsd_commit(rqstp, &cstate->current_fh, commit->co_offset,
588ca364317SJ.Bruce Fields 			     commit->co_count);
5891da177e4SLinus Torvalds }
5901da177e4SLinus Torvalds 
591b37ad28bSAl Viro static __be32
592ca364317SJ.Bruce Fields nfsd4_create(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
593eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
5941da177e4SLinus Torvalds {
595eb69853dSChristoph Hellwig 	struct nfsd4_create *create = &u->create;
5961da177e4SLinus Torvalds 	struct svc_fh resfh;
597b37ad28bSAl Viro 	__be32 status;
5981da177e4SLinus Torvalds 	dev_t rdev;
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds 	fh_init(&resfh, NFS4_FHSIZE);
6011da177e4SLinus Torvalds 
602fa08139dSJ. Bruce Fields 	status = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_NOP);
6031da177e4SLinus Torvalds 	if (status)
6041da177e4SLinus Torvalds 		return status;
6051da177e4SLinus Torvalds 
6063c8e0316SYu Zhiguo 	status = check_attr_support(rqstp, cstate, create->cr_bmval,
6073c8e0316SYu Zhiguo 				    nfsd_attrmask);
6083c8e0316SYu Zhiguo 	if (status)
6093c8e0316SYu Zhiguo 		return status;
6103c8e0316SYu Zhiguo 
6111da177e4SLinus Torvalds 	switch (create->cr_type) {
6121da177e4SLinus Torvalds 	case NF4LNK:
613ca364317SJ.Bruce Fields 		status = nfsd_symlink(rqstp, &cstate->current_fh,
614ca364317SJ.Bruce Fields 				      create->cr_name, create->cr_namelen,
6151e444f5bSKinglong Mee 				      create->cr_data, &resfh);
6161da177e4SLinus Torvalds 		break;
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 	case NF4BLK:
6191da177e4SLinus Torvalds 		rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
6201da177e4SLinus Torvalds 		if (MAJOR(rdev) != create->cr_specdata1 ||
6211da177e4SLinus Torvalds 		    MINOR(rdev) != create->cr_specdata2)
6221da177e4SLinus Torvalds 			return nfserr_inval;
623ca364317SJ.Bruce Fields 		status = nfsd_create(rqstp, &cstate->current_fh,
624ca364317SJ.Bruce Fields 				     create->cr_name, create->cr_namelen,
625ca364317SJ.Bruce Fields 				     &create->cr_iattr, S_IFBLK, rdev, &resfh);
6261da177e4SLinus Torvalds 		break;
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	case NF4CHR:
6291da177e4SLinus Torvalds 		rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
6301da177e4SLinus Torvalds 		if (MAJOR(rdev) != create->cr_specdata1 ||
6311da177e4SLinus Torvalds 		    MINOR(rdev) != create->cr_specdata2)
6321da177e4SLinus Torvalds 			return nfserr_inval;
633ca364317SJ.Bruce Fields 		status = nfsd_create(rqstp, &cstate->current_fh,
634ca364317SJ.Bruce Fields 				     create->cr_name, create->cr_namelen,
635ca364317SJ.Bruce Fields 				     &create->cr_iattr,S_IFCHR, rdev, &resfh);
6361da177e4SLinus Torvalds 		break;
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	case NF4SOCK:
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_IFSOCK, 0, &resfh);
6421da177e4SLinus Torvalds 		break;
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	case NF4FIFO:
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_IFIFO, 0, &resfh);
6481da177e4SLinus Torvalds 		break;
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	case NF4DIR:
6511da177e4SLinus Torvalds 		create->cr_iattr.ia_valid &= ~ATTR_SIZE;
652ca364317SJ.Bruce Fields 		status = nfsd_create(rqstp, &cstate->current_fh,
653ca364317SJ.Bruce Fields 				     create->cr_name, create->cr_namelen,
654ca364317SJ.Bruce Fields 				     &create->cr_iattr, S_IFDIR, 0, &resfh);
6551da177e4SLinus Torvalds 		break;
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	default:
6581da177e4SLinus Torvalds 		status = nfserr_badtype;
6591da177e4SLinus Torvalds 	}
6601da177e4SLinus Torvalds 
6619208faf2SYu Zhiguo 	if (status)
6629208faf2SYu Zhiguo 		goto out;
6639208faf2SYu Zhiguo 
66418032ca0SDavid Quigley 	if (create->cr_label.len)
66518032ca0SDavid Quigley 		nfsd4_security_inode_setsecctx(&resfh, &create->cr_label, create->cr_bmval);
66618032ca0SDavid Quigley 
6679208faf2SYu Zhiguo 	if (create->cr_acl != NULL)
6689208faf2SYu Zhiguo 		do_set_nfs4_acl(rqstp, &resfh, create->cr_acl,
6699208faf2SYu Zhiguo 				create->cr_bmval);
6709208faf2SYu Zhiguo 
671ca364317SJ.Bruce Fields 	fh_unlock(&cstate->current_fh);
672ca364317SJ.Bruce Fields 	set_change_info(&create->cr_cinfo, &cstate->current_fh);
673ca364317SJ.Bruce Fields 	fh_dup2(&cstate->current_fh, &resfh);
6749208faf2SYu Zhiguo out:
6751da177e4SLinus Torvalds 	fh_put(&resfh);
6761da177e4SLinus Torvalds 	return status;
6771da177e4SLinus Torvalds }
6781da177e4SLinus Torvalds 
6797191155bSJ.Bruce Fields static __be32
680ca364317SJ.Bruce Fields nfsd4_getattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
681eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
6821da177e4SLinus Torvalds {
683eb69853dSChristoph Hellwig 	struct nfsd4_getattr *getattr = &u->getattr;
684b37ad28bSAl Viro 	__be32 status;
6851da177e4SLinus Torvalds 
6868837abcaSMiklos Szeredi 	status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
6871da177e4SLinus Torvalds 	if (status)
6881da177e4SLinus Torvalds 		return status;
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 	if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
6911da177e4SLinus Torvalds 		return nfserr_inval;
6921da177e4SLinus Torvalds 
693916d2d84SJ. Bruce Fields 	getattr->ga_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
694916d2d84SJ. Bruce Fields 	getattr->ga_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
695916d2d84SJ. Bruce Fields 	getattr->ga_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
6961da177e4SLinus Torvalds 
697ca364317SJ.Bruce Fields 	getattr->ga_fhp = &cstate->current_fh;
6981da177e4SLinus Torvalds 	return nfs_ok;
6991da177e4SLinus Torvalds }
7001da177e4SLinus Torvalds 
7017191155bSJ.Bruce Fields static __be32
702ca364317SJ.Bruce Fields nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
703eb69853dSChristoph Hellwig 	   union nfsd4_op_u *u)
7041da177e4SLinus Torvalds {
705eb69853dSChristoph Hellwig 	struct nfsd4_link *link = &u->link;
706b37ad28bSAl Viro 	__be32 status = nfserr_nofilehandle;
7071da177e4SLinus Torvalds 
708ca364317SJ.Bruce Fields 	if (!cstate->save_fh.fh_dentry)
7091da177e4SLinus Torvalds 		return status;
710ca364317SJ.Bruce Fields 	status = nfsd_link(rqstp, &cstate->current_fh,
711ca364317SJ.Bruce Fields 			   link->li_name, link->li_namelen, &cstate->save_fh);
7121da177e4SLinus Torvalds 	if (!status)
713ca364317SJ.Bruce Fields 		set_change_info(&link->li_cinfo, &cstate->current_fh);
7141da177e4SLinus Torvalds 	return status;
7151da177e4SLinus Torvalds }
7161da177e4SLinus Torvalds 
7170ff7ab46SJ. Bruce Fields static __be32 nfsd4_do_lookupp(struct svc_rqst *rqstp, struct svc_fh *fh)
7181da177e4SLinus Torvalds {
7191da177e4SLinus Torvalds 	struct svc_fh tmp_fh;
720b37ad28bSAl Viro 	__be32 ret;
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 	fh_init(&tmp_fh, NFS4_FHSIZE);
723df547efbSJ. Bruce Fields 	ret = exp_pseudoroot(rqstp, &tmp_fh);
724df547efbSJ. Bruce Fields 	if (ret)
7251da177e4SLinus Torvalds 		return ret;
7260ff7ab46SJ. Bruce Fields 	if (tmp_fh.fh_dentry == fh->fh_dentry) {
7271da177e4SLinus Torvalds 		fh_put(&tmp_fh);
7281da177e4SLinus Torvalds 		return nfserr_noent;
7291da177e4SLinus Torvalds 	}
7301da177e4SLinus Torvalds 	fh_put(&tmp_fh);
7310ff7ab46SJ. Bruce Fields 	return nfsd_lookup(rqstp, fh, "..", 2, fh);
7320ff7ab46SJ. Bruce Fields }
7330ff7ab46SJ. Bruce Fields 
7340ff7ab46SJ. Bruce Fields static __be32
7350ff7ab46SJ. Bruce Fields nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
736eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
7370ff7ab46SJ. Bruce Fields {
7380ff7ab46SJ. Bruce Fields 	return nfsd4_do_lookupp(rqstp, &cstate->current_fh);
7391da177e4SLinus Torvalds }
7401da177e4SLinus Torvalds 
7417191155bSJ.Bruce Fields static __be32
742ca364317SJ.Bruce Fields nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
743eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
7441da177e4SLinus Torvalds {
745ca364317SJ.Bruce Fields 	return nfsd_lookup(rqstp, &cstate->current_fh,
746eb69853dSChristoph Hellwig 			   u->lookup.lo_name, u->lookup.lo_len,
747ca364317SJ.Bruce Fields 			   &cstate->current_fh);
7481da177e4SLinus Torvalds }
7491da177e4SLinus Torvalds 
7507191155bSJ.Bruce Fields static __be32
751ca364317SJ.Bruce Fields nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
752eb69853dSChristoph Hellwig 	   union nfsd4_op_u *u)
7531da177e4SLinus Torvalds {
754eb69853dSChristoph Hellwig 	struct nfsd4_read *read = &u->read;
755b37ad28bSAl Viro 	__be32 status;
7561da177e4SLinus Torvalds 
7577e06b7f9SNeilBrown 	read->rd_filp = NULL;
7581da177e4SLinus Torvalds 	if (read->rd_offset >= OFFSET_MAX)
7591da177e4SLinus Torvalds 		return nfserr_inval;
7601da177e4SLinus Torvalds 
7619b3234b9SJ. Bruce Fields 	/*
7629b3234b9SJ. Bruce Fields 	 * If we do a zero copy read, then a client will see read data
7639b3234b9SJ. Bruce Fields 	 * that reflects the state of the file *after* performing the
7649b3234b9SJ. Bruce Fields 	 * following compound.
7659b3234b9SJ. Bruce Fields 	 *
7669b3234b9SJ. Bruce Fields 	 * To ensure proper ordering, we therefore turn off zero copy if
7679b3234b9SJ. Bruce Fields 	 * the client wants us to do more in this compound:
7689b3234b9SJ. Bruce Fields 	 */
7699b3234b9SJ. Bruce Fields 	if (!nfsd4_last_compound_op(rqstp))
770779fb0f3SJeff Layton 		clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
7719b3234b9SJ. Bruce Fields 
7721da177e4SLinus Torvalds 	/* check stateid */
773aa0d6aedSAnna Schumaker 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
774aa0d6aedSAnna Schumaker 					&read->rd_stateid, RD_STATE,
775aa0d6aedSAnna Schumaker 					&read->rd_filp, &read->rd_tmp_file);
776af90f707SChristoph Hellwig 	if (status) {
7771da177e4SLinus Torvalds 		dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
7781da177e4SLinus Torvalds 		goto out;
7791da177e4SLinus Torvalds 	}
7801da177e4SLinus Torvalds 	status = nfs_ok;
7811da177e4SLinus Torvalds out:
7821da177e4SLinus Torvalds 	read->rd_rqstp = rqstp;
783ca364317SJ.Bruce Fields 	read->rd_fhp = &cstate->current_fh;
7841da177e4SLinus Torvalds 	return status;
7851da177e4SLinus Torvalds }
7861da177e4SLinus Torvalds 
7877191155bSJ.Bruce Fields static __be32
788ca364317SJ.Bruce Fields nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
789eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
7901da177e4SLinus Torvalds {
791eb69853dSChristoph Hellwig 	struct nfsd4_readdir *readdir = &u->readdir;
7921da177e4SLinus Torvalds 	u64 cookie = readdir->rd_cookie;
7931da177e4SLinus Torvalds 	static const nfs4_verifier zeroverf;
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 	/* no need to check permission - this will be done in nfsd_readdir() */
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 	if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
7981da177e4SLinus Torvalds 		return nfserr_inval;
7991da177e4SLinus Torvalds 
800916d2d84SJ. Bruce Fields 	readdir->rd_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
801916d2d84SJ. Bruce Fields 	readdir->rd_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
802916d2d84SJ. Bruce Fields 	readdir->rd_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
8031da177e4SLinus Torvalds 
804832023bfSBernd Schubert 	if ((cookie == 1) || (cookie == 2) ||
8051da177e4SLinus Torvalds 	    (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
8061da177e4SLinus Torvalds 		return nfserr_bad_cookie;
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 	readdir->rd_rqstp = rqstp;
809ca364317SJ.Bruce Fields 	readdir->rd_fhp = &cstate->current_fh;
8101da177e4SLinus Torvalds 	return nfs_ok;
8111da177e4SLinus Torvalds }
8121da177e4SLinus Torvalds 
8137191155bSJ.Bruce Fields static __be32
814ca364317SJ.Bruce Fields nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
815eb69853dSChristoph Hellwig 	       union nfsd4_op_u *u)
8161da177e4SLinus Torvalds {
817eb69853dSChristoph Hellwig 	u->readlink.rl_rqstp = rqstp;
818eb69853dSChristoph Hellwig 	u->readlink.rl_fhp = &cstate->current_fh;
8191da177e4SLinus Torvalds 	return nfs_ok;
8201da177e4SLinus Torvalds }
8211da177e4SLinus Torvalds 
8227191155bSJ.Bruce Fields static __be32
823ca364317SJ.Bruce Fields nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
824eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
8251da177e4SLinus Torvalds {
826eb69853dSChristoph Hellwig 	struct nfsd4_remove *remove = &u->remove;
827b37ad28bSAl Viro 	__be32 status;
8281da177e4SLinus Torvalds 
829c87fb4a3SJ. Bruce Fields 	if (opens_in_grace(SVC_NET(rqstp)))
830c815afc7SNeilBrown 		return nfserr_grace;
831ca364317SJ.Bruce Fields 	status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
832ca364317SJ.Bruce Fields 			     remove->rm_name, remove->rm_namelen);
8331da177e4SLinus Torvalds 	if (!status) {
834ca364317SJ.Bruce Fields 		fh_unlock(&cstate->current_fh);
835ca364317SJ.Bruce Fields 		set_change_info(&remove->rm_cinfo, &cstate->current_fh);
8361da177e4SLinus Torvalds 	}
8371da177e4SLinus Torvalds 	return status;
8381da177e4SLinus Torvalds }
8391da177e4SLinus Torvalds 
8407191155bSJ.Bruce Fields static __be32
841ca364317SJ.Bruce Fields nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
842eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
8431da177e4SLinus Torvalds {
844eb69853dSChristoph Hellwig 	struct nfsd4_rename *rename = &u->rename;
845b37ad28bSAl Viro 	__be32 status = nfserr_nofilehandle;
8461da177e4SLinus Torvalds 
847ca364317SJ.Bruce Fields 	if (!cstate->save_fh.fh_dentry)
8481da177e4SLinus Torvalds 		return status;
849c87fb4a3SJ. Bruce Fields 	if (opens_in_grace(SVC_NET(rqstp)) &&
8505ccb0066SStanislav Kinsbursky 		!(cstate->save_fh.fh_export->ex_flags & NFSEXP_NOSUBTREECHECK))
851c815afc7SNeilBrown 		return nfserr_grace;
852ca364317SJ.Bruce Fields 	status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname,
853ca364317SJ.Bruce Fields 			     rename->rn_snamelen, &cstate->current_fh,
8541da177e4SLinus Torvalds 			     rename->rn_tname, rename->rn_tnamelen);
8552a6cf944SJ. Bruce Fields 	if (status)
8562a6cf944SJ. Bruce Fields 		return status;
857ca364317SJ.Bruce Fields 	set_change_info(&rename->rn_sinfo, &cstate->current_fh);
858ca364317SJ.Bruce Fields 	set_change_info(&rename->rn_tinfo, &cstate->save_fh);
8592a6cf944SJ. Bruce Fields 	return nfs_ok;
8601da177e4SLinus Torvalds }
8611da177e4SLinus Torvalds 
8627191155bSJ.Bruce Fields static __be32
863dcb488a3SAndy Adamson nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
864eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
865dcb488a3SAndy Adamson {
866eb69853dSChristoph Hellwig 	struct nfsd4_secinfo *secinfo = &u->secinfo;
867dcb488a3SAndy Adamson 	struct svc_export *exp;
868dcb488a3SAndy Adamson 	struct dentry *dentry;
869dcb488a3SAndy Adamson 	__be32 err;
870dcb488a3SAndy Adamson 
87129a78a3eSJ. Bruce Fields 	err = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_EXEC);
87229a78a3eSJ. Bruce Fields 	if (err)
87329a78a3eSJ. Bruce Fields 		return err;
874dcb488a3SAndy Adamson 	err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
875dcb488a3SAndy Adamson 				    secinfo->si_name, secinfo->si_namelen,
876dcb488a3SAndy Adamson 				    &exp, &dentry);
877dcb488a3SAndy Adamson 	if (err)
878dcb488a3SAndy Adamson 		return err;
8792f6fc056SJ. Bruce Fields 	fh_unlock(&cstate->current_fh);
8802b0143b5SDavid Howells 	if (d_really_is_negative(dentry)) {
881dcb488a3SAndy Adamson 		exp_put(exp);
882dcb488a3SAndy Adamson 		err = nfserr_noent;
883dcb488a3SAndy Adamson 	} else
884dcb488a3SAndy Adamson 		secinfo->si_exp = exp;
885dcb488a3SAndy Adamson 	dput(dentry);
88656560b9aSJ. Bruce Fields 	if (cstate->minorversion)
88756560b9aSJ. Bruce Fields 		/* See rfc 5661 section 2.6.3.1.1.8 */
88856560b9aSJ. Bruce Fields 		fh_put(&cstate->current_fh);
889dcb488a3SAndy Adamson 	return err;
890dcb488a3SAndy Adamson }
891dcb488a3SAndy Adamson 
892dcb488a3SAndy Adamson static __be32
89304f4ad16SJ. Bruce Fields nfsd4_secinfo_no_name(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
894eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
89504f4ad16SJ. Bruce Fields {
89604f4ad16SJ. Bruce Fields 	__be32 err;
89704f4ad16SJ. Bruce Fields 
898eb69853dSChristoph Hellwig 	switch (u->secinfo_no_name.sin_style) {
89904f4ad16SJ. Bruce Fields 	case NFS4_SECINFO_STYLE4_CURRENT_FH:
90004f4ad16SJ. Bruce Fields 		break;
90104f4ad16SJ. Bruce Fields 	case NFS4_SECINFO_STYLE4_PARENT:
90204f4ad16SJ. Bruce Fields 		err = nfsd4_do_lookupp(rqstp, &cstate->current_fh);
90304f4ad16SJ. Bruce Fields 		if (err)
90404f4ad16SJ. Bruce Fields 			return err;
90504f4ad16SJ. Bruce Fields 		break;
90604f4ad16SJ. Bruce Fields 	default:
90704f4ad16SJ. Bruce Fields 		return nfserr_inval;
90804f4ad16SJ. Bruce Fields 	}
909bf18f163SKinglong Mee 
910eb69853dSChristoph Hellwig 	u->secinfo_no_name.sin_exp = exp_get(cstate->current_fh.fh_export);
91104f4ad16SJ. Bruce Fields 	fh_put(&cstate->current_fh);
91204f4ad16SJ. Bruce Fields 	return nfs_ok;
91304f4ad16SJ. Bruce Fields }
91404f4ad16SJ. Bruce Fields 
91504f4ad16SJ. Bruce Fields static __be32
916ca364317SJ.Bruce Fields nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
917eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
9181da177e4SLinus Torvalds {
919eb69853dSChristoph Hellwig 	struct nfsd4_setattr *setattr = &u->setattr;
920b37ad28bSAl Viro 	__be32 status = nfs_ok;
92196f6f985SAl Viro 	int err;
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds 	if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
924af90f707SChristoph Hellwig 		status = nfs4_preprocess_stateid_op(rqstp, cstate,
925aa0d6aedSAnna Schumaker 				&cstate->current_fh, &setattr->sa_stateid,
926aa0d6aedSAnna Schumaker 				WR_STATE, NULL, NULL);
927375c5547SJ. Bruce Fields 		if (status) {
9283e3b4800SGreg Banks 			dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
929375c5547SJ. Bruce Fields 			return status;
930375c5547SJ. Bruce Fields 		}
9311da177e4SLinus Torvalds 	}
93296f6f985SAl Viro 	err = fh_want_write(&cstate->current_fh);
93396f6f985SAl Viro 	if (err)
93496f6f985SAl Viro 		return nfserrno(err);
9351da177e4SLinus Torvalds 	status = nfs_ok;
9363c8e0316SYu Zhiguo 
9373c8e0316SYu Zhiguo 	status = check_attr_support(rqstp, cstate, setattr->sa_bmval,
9383c8e0316SYu Zhiguo 				    nfsd_attrmask);
9393c8e0316SYu Zhiguo 	if (status)
9403c8e0316SYu Zhiguo 		goto out;
9413c8e0316SYu Zhiguo 
9421da177e4SLinus Torvalds 	if (setattr->sa_acl != NULL)
943ca364317SJ.Bruce Fields 		status = nfsd4_set_nfs4_acl(rqstp, &cstate->current_fh,
944ca364317SJ.Bruce Fields 					    setattr->sa_acl);
9451da177e4SLinus Torvalds 	if (status)
94618f335afSDave Hansen 		goto out;
94718032ca0SDavid Quigley 	if (setattr->sa_label.len)
94818032ca0SDavid Quigley 		status = nfsd4_set_nfs4_label(rqstp, &cstate->current_fh,
94918032ca0SDavid Quigley 				&setattr->sa_label);
95018032ca0SDavid Quigley 	if (status)
95118032ca0SDavid Quigley 		goto out;
952ca364317SJ.Bruce Fields 	status = nfsd_setattr(rqstp, &cstate->current_fh, &setattr->sa_iattr,
9531da177e4SLinus Torvalds 				0, (time_t)0);
95418f335afSDave Hansen out:
955bad0dcffSAl Viro 	fh_drop_write(&cstate->current_fh);
9561da177e4SLinus Torvalds 	return status;
9571da177e4SLinus Torvalds }
9581da177e4SLinus Torvalds 
959ffe1137bSJ. Bruce Fields static int fill_in_write_vector(struct kvec *vec, struct nfsd4_write *write)
960ffe1137bSJ. Bruce Fields {
961ffe1137bSJ. Bruce Fields         int i = 1;
962ffe1137bSJ. Bruce Fields         int buflen = write->wr_buflen;
963ffe1137bSJ. Bruce Fields 
964ffe1137bSJ. Bruce Fields         vec[0].iov_base = write->wr_head.iov_base;
965ffe1137bSJ. Bruce Fields         vec[0].iov_len = min_t(int, buflen, write->wr_head.iov_len);
966ffe1137bSJ. Bruce Fields         buflen -= vec[0].iov_len;
967ffe1137bSJ. Bruce Fields 
968ffe1137bSJ. Bruce Fields         while (buflen) {
969ffe1137bSJ. Bruce Fields                 vec[i].iov_base = page_address(write->wr_pagelist[i - 1]);
970ffe1137bSJ. Bruce Fields                 vec[i].iov_len = min_t(int, PAGE_SIZE, buflen);
971ffe1137bSJ. Bruce Fields                 buflen -= vec[i].iov_len;
972ffe1137bSJ. Bruce Fields                 i++;
973ffe1137bSJ. Bruce Fields         }
974ffe1137bSJ. Bruce Fields         return i;
975ffe1137bSJ. Bruce Fields }
976ffe1137bSJ. Bruce Fields 
9777191155bSJ.Bruce Fields static __be32
978ca364317SJ.Bruce Fields nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
979eb69853dSChristoph Hellwig 	    union nfsd4_op_u *u)
9801da177e4SLinus Torvalds {
981eb69853dSChristoph Hellwig 	struct nfsd4_write *write = &u->write;
9821da177e4SLinus Torvalds 	stateid_t *stateid = &write->wr_stateid;
9831da177e4SLinus Torvalds 	struct file *filp = NULL;
984b37ad28bSAl Viro 	__be32 status = nfs_ok;
98531dec253SDavid Shaw 	unsigned long cnt;
986ffe1137bSJ. Bruce Fields 	int nvecs;
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 	if (write->wr_offset >= OFFSET_MAX)
9891da177e4SLinus Torvalds 		return nfserr_inval;
9901da177e4SLinus Torvalds 
991aa0d6aedSAnna Schumaker 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
992aa0d6aedSAnna Schumaker 						stateid, WR_STATE, &filp, NULL);
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 
1005af90f707SChristoph Hellwig 	status = nfsd_vfs_write(rqstp, &cstate->current_fh, filp,
1006af90f707SChristoph Hellwig 				write->wr_offset, rqstp->rq_vec, nvecs, &cnt,
100754bbb7d2SKinglong Mee 				write->wr_how_written);
10087e06b7f9SNeilBrown 	fput(filp);
10091da177e4SLinus Torvalds 
101031dec253SDavid Shaw 	write->wr_bytes_written = cnt;
101131dec253SDavid Shaw 
10121da177e4SLinus Torvalds 	return status;
10131da177e4SLinus Torvalds }
10141da177e4SLinus Torvalds 
101524bab491SAnna Schumaker static __be32
101629ae7f9dSAnna Schumaker nfsd4_verify_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
101729ae7f9dSAnna Schumaker 		  stateid_t *src_stateid, struct file **src,
101829ae7f9dSAnna Schumaker 		  stateid_t *dst_stateid, struct file **dst)
1019ffa0160aSChristoph Hellwig {
1020ffa0160aSChristoph Hellwig 	__be32 status;
1021ffa0160aSChristoph Hellwig 
1022ffa0160aSChristoph Hellwig 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->save_fh,
102329ae7f9dSAnna Schumaker 					    src_stateid, RD_STATE, src, NULL);
1024ffa0160aSChristoph Hellwig 	if (status) {
1025ffa0160aSChristoph Hellwig 		dprintk("NFSD: %s: couldn't process src stateid!\n", __func__);
1026ffa0160aSChristoph Hellwig 		goto out;
1027ffa0160aSChristoph Hellwig 	}
1028ffa0160aSChristoph Hellwig 
1029ffa0160aSChristoph Hellwig 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
103029ae7f9dSAnna Schumaker 					    dst_stateid, WR_STATE, dst, NULL);
1031ffa0160aSChristoph Hellwig 	if (status) {
1032ffa0160aSChristoph Hellwig 		dprintk("NFSD: %s: couldn't process dst stateid!\n", __func__);
1033ffa0160aSChristoph Hellwig 		goto out_put_src;
1034ffa0160aSChristoph Hellwig 	}
1035ffa0160aSChristoph Hellwig 
1036ffa0160aSChristoph Hellwig 	/* fix up for NFS-specific error code */
103729ae7f9dSAnna Schumaker 	if (!S_ISREG(file_inode(*src)->i_mode) ||
103829ae7f9dSAnna Schumaker 	    !S_ISREG(file_inode(*dst)->i_mode)) {
1039ffa0160aSChristoph Hellwig 		status = nfserr_wrong_type;
1040ffa0160aSChristoph Hellwig 		goto out_put_dst;
1041ffa0160aSChristoph Hellwig 	}
1042ffa0160aSChristoph Hellwig 
104329ae7f9dSAnna Schumaker out:
104429ae7f9dSAnna Schumaker 	return status;
104529ae7f9dSAnna Schumaker out_put_dst:
104629ae7f9dSAnna Schumaker 	fput(*dst);
104729ae7f9dSAnna Schumaker out_put_src:
104829ae7f9dSAnna Schumaker 	fput(*src);
104929ae7f9dSAnna Schumaker 	goto out;
105029ae7f9dSAnna Schumaker }
105129ae7f9dSAnna Schumaker 
105229ae7f9dSAnna Schumaker static __be32
105329ae7f9dSAnna Schumaker nfsd4_clone(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1054eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
105529ae7f9dSAnna Schumaker {
1056eb69853dSChristoph Hellwig 	struct nfsd4_clone *clone = &u->clone;
105729ae7f9dSAnna Schumaker 	struct file *src, *dst;
105829ae7f9dSAnna Schumaker 	__be32 status;
105929ae7f9dSAnna Schumaker 
106029ae7f9dSAnna Schumaker 	status = nfsd4_verify_copy(rqstp, cstate, &clone->cl_src_stateid, &src,
106129ae7f9dSAnna Schumaker 				   &clone->cl_dst_stateid, &dst);
106229ae7f9dSAnna Schumaker 	if (status)
106329ae7f9dSAnna Schumaker 		goto out;
106429ae7f9dSAnna Schumaker 
1065ffa0160aSChristoph Hellwig 	status = nfsd4_clone_file_range(src, clone->cl_src_pos,
1066ffa0160aSChristoph Hellwig 			dst, clone->cl_dst_pos, clone->cl_count);
1067ffa0160aSChristoph Hellwig 
1068ffa0160aSChristoph Hellwig 	fput(dst);
1069ffa0160aSChristoph Hellwig 	fput(src);
1070ffa0160aSChristoph Hellwig out:
1071ffa0160aSChristoph Hellwig 	return status;
1072ffa0160aSChristoph Hellwig }
1073ffa0160aSChristoph Hellwig 
1074ffa0160aSChristoph Hellwig static __be32
107529ae7f9dSAnna Schumaker nfsd4_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1076eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
107729ae7f9dSAnna Schumaker {
1078eb69853dSChristoph Hellwig 	struct nfsd4_copy *copy = &u->copy;
107929ae7f9dSAnna Schumaker 	struct file *src, *dst;
108029ae7f9dSAnna Schumaker 	__be32 status;
108129ae7f9dSAnna Schumaker 	ssize_t bytes;
108229ae7f9dSAnna Schumaker 
108329ae7f9dSAnna Schumaker 	status = nfsd4_verify_copy(rqstp, cstate, &copy->cp_src_stateid, &src,
108429ae7f9dSAnna Schumaker 				   &copy->cp_dst_stateid, &dst);
108529ae7f9dSAnna Schumaker 	if (status)
108629ae7f9dSAnna Schumaker 		goto out;
108729ae7f9dSAnna Schumaker 
108829ae7f9dSAnna Schumaker 	bytes = nfsd_copy_file_range(src, copy->cp_src_pos,
108929ae7f9dSAnna Schumaker 			dst, copy->cp_dst_pos, copy->cp_count);
109029ae7f9dSAnna Schumaker 
109129ae7f9dSAnna Schumaker 	if (bytes < 0)
109229ae7f9dSAnna Schumaker 		status = nfserrno(bytes);
109329ae7f9dSAnna Schumaker 	else {
109429ae7f9dSAnna Schumaker 		copy->cp_res.wr_bytes_written = bytes;
109529ae7f9dSAnna Schumaker 		copy->cp_res.wr_stable_how = NFS_UNSTABLE;
109629ae7f9dSAnna Schumaker 		copy->cp_consecutive = 1;
109729ae7f9dSAnna Schumaker 		copy->cp_synchronous = 1;
109829ae7f9dSAnna Schumaker 		gen_boot_verifier(&copy->cp_res.wr_verifier, SVC_NET(rqstp));
109929ae7f9dSAnna Schumaker 		status = nfs_ok;
110029ae7f9dSAnna Schumaker 	}
110129ae7f9dSAnna Schumaker 
110229ae7f9dSAnna Schumaker 	fput(src);
110329ae7f9dSAnna Schumaker 	fput(dst);
110429ae7f9dSAnna Schumaker out:
110529ae7f9dSAnna Schumaker 	return status;
110629ae7f9dSAnna Schumaker }
110729ae7f9dSAnna Schumaker 
110829ae7f9dSAnna Schumaker static __be32
110995d871f0SAnna Schumaker nfsd4_fallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
111095d871f0SAnna Schumaker 		struct nfsd4_fallocate *fallocate, int flags)
111195d871f0SAnna Schumaker {
111295d871f0SAnna Schumaker 	__be32 status = nfserr_notsupp;
111395d871f0SAnna Schumaker 	struct file *file;
111495d871f0SAnna Schumaker 
1115aa0d6aedSAnna Schumaker 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
111695d871f0SAnna Schumaker 					    &fallocate->falloc_stateid,
1117af90f707SChristoph Hellwig 					    WR_STATE, &file, NULL);
111895d871f0SAnna Schumaker 	if (status != nfs_ok) {
111995d871f0SAnna Schumaker 		dprintk("NFSD: nfsd4_fallocate: couldn't process stateid!\n");
112095d871f0SAnna Schumaker 		return status;
112195d871f0SAnna Schumaker 	}
112295d871f0SAnna Schumaker 
112395d871f0SAnna Schumaker 	status = nfsd4_vfs_fallocate(rqstp, &cstate->current_fh, file,
112495d871f0SAnna Schumaker 				     fallocate->falloc_offset,
112595d871f0SAnna Schumaker 				     fallocate->falloc_length,
112695d871f0SAnna Schumaker 				     flags);
112795d871f0SAnna Schumaker 	fput(file);
112895d871f0SAnna Schumaker 	return status;
112995d871f0SAnna Schumaker }
113095d871f0SAnna Schumaker 
113195d871f0SAnna Schumaker static __be32
113295d871f0SAnna Schumaker nfsd4_allocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1133eb69853dSChristoph Hellwig 	       union nfsd4_op_u *u)
113495d871f0SAnna Schumaker {
1135eb69853dSChristoph Hellwig 	return nfsd4_fallocate(rqstp, cstate, &u->allocate, 0);
113695d871f0SAnna Schumaker }
113795d871f0SAnna Schumaker 
113895d871f0SAnna Schumaker static __be32
1139b0cb9085SAnna Schumaker nfsd4_deallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1140eb69853dSChristoph Hellwig 		 union nfsd4_op_u *u)
1141b0cb9085SAnna Schumaker {
1142eb69853dSChristoph Hellwig 	return nfsd4_fallocate(rqstp, cstate, &u->deallocate,
1143b0cb9085SAnna Schumaker 			       FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
1144b0cb9085SAnna Schumaker }
1145b0cb9085SAnna Schumaker 
1146b0cb9085SAnna Schumaker static __be32
114724bab491SAnna Schumaker nfsd4_seek(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1148eb69853dSChristoph Hellwig 	   union nfsd4_op_u *u)
114924bab491SAnna Schumaker {
1150eb69853dSChristoph Hellwig 	struct nfsd4_seek *seek = &u->seek;
115124bab491SAnna Schumaker 	int whence;
115224bab491SAnna Schumaker 	__be32 status;
115324bab491SAnna Schumaker 	struct file *file;
115424bab491SAnna Schumaker 
1155aa0d6aedSAnna Schumaker 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
115624bab491SAnna Schumaker 					    &seek->seek_stateid,
1157af90f707SChristoph Hellwig 					    RD_STATE, &file, NULL);
115824bab491SAnna Schumaker 	if (status) {
115924bab491SAnna Schumaker 		dprintk("NFSD: nfsd4_seek: couldn't process stateid!\n");
116024bab491SAnna Schumaker 		return status;
116124bab491SAnna Schumaker 	}
116224bab491SAnna Schumaker 
116324bab491SAnna Schumaker 	switch (seek->seek_whence) {
116424bab491SAnna Schumaker 	case NFS4_CONTENT_DATA:
116524bab491SAnna Schumaker 		whence = SEEK_DATA;
116624bab491SAnna Schumaker 		break;
116724bab491SAnna Schumaker 	case NFS4_CONTENT_HOLE:
116824bab491SAnna Schumaker 		whence = SEEK_HOLE;
116924bab491SAnna Schumaker 		break;
117024bab491SAnna Schumaker 	default:
117124bab491SAnna Schumaker 		status = nfserr_union_notsupp;
117224bab491SAnna Schumaker 		goto out;
117324bab491SAnna Schumaker 	}
117424bab491SAnna Schumaker 
117524bab491SAnna Schumaker 	/*
117624bab491SAnna Schumaker 	 * Note:  This call does change file->f_pos, but nothing in NFSD
117724bab491SAnna Schumaker 	 *        should ever file->f_pos.
117824bab491SAnna Schumaker 	 */
117924bab491SAnna Schumaker 	seek->seek_pos = vfs_llseek(file, seek->seek_offset, whence);
118024bab491SAnna Schumaker 	if (seek->seek_pos < 0)
118124bab491SAnna Schumaker 		status = nfserrno(seek->seek_pos);
118224bab491SAnna Schumaker 	else if (seek->seek_pos >= i_size_read(file_inode(file)))
118324bab491SAnna Schumaker 		seek->seek_eof = true;
118424bab491SAnna Schumaker 
118524bab491SAnna Schumaker out:
118624bab491SAnna Schumaker 	fput(file);
118724bab491SAnna Schumaker 	return status;
118824bab491SAnna Schumaker }
118924bab491SAnna Schumaker 
11901da177e4SLinus Torvalds /* This routine never returns NFS_OK!  If there are no other errors, it
11911da177e4SLinus Torvalds  * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
11921da177e4SLinus Torvalds  * attributes matched.  VERIFY is implemented by mapping NFSERR_SAME
11931da177e4SLinus Torvalds  * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
11941da177e4SLinus Torvalds  */
1195b37ad28bSAl Viro static __be32
1196c954e2a5SJ.Bruce Fields _nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1197ca364317SJ.Bruce Fields 	     struct nfsd4_verify *verify)
11981da177e4SLinus Torvalds {
11992ebbc012SAl Viro 	__be32 *buf, *p;
12001da177e4SLinus Torvalds 	int count;
1201b37ad28bSAl Viro 	__be32 status;
12021da177e4SLinus Torvalds 
12038837abcaSMiklos Szeredi 	status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
12041da177e4SLinus Torvalds 	if (status)
12051da177e4SLinus Torvalds 		return status;
12061da177e4SLinus Torvalds 
12073c8e0316SYu Zhiguo 	status = check_attr_support(rqstp, cstate, verify->ve_bmval, NULL);
12083c8e0316SYu Zhiguo 	if (status)
12093c8e0316SYu Zhiguo 		return status;
12103c8e0316SYu Zhiguo 
12111da177e4SLinus Torvalds 	if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
12121da177e4SLinus Torvalds 	    || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
12131da177e4SLinus Torvalds 		return nfserr_inval;
12141da177e4SLinus Torvalds 	if (verify->ve_attrlen & 3)
12151da177e4SLinus Torvalds 		return nfserr_inval;
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 	/* count in words:
12181da177e4SLinus Torvalds 	 *   bitmap_len(1) + bitmap(2) + attr_len(1) = 4
12191da177e4SLinus Torvalds 	 */
12201da177e4SLinus Torvalds 	count = 4 + (verify->ve_attrlen >> 2);
12211da177e4SLinus Torvalds 	buf = kmalloc(count << 2, GFP_KERNEL);
12221da177e4SLinus Torvalds 	if (!buf)
12233e772463SJ. Bruce Fields 		return nfserr_jukebox;
12241da177e4SLinus Torvalds 
122584822d0bSJ. Bruce Fields 	p = buf;
1226d5184658SJ. Bruce Fields 	status = nfsd4_encode_fattr_to_buf(&p, count, &cstate->current_fh,
1227ca364317SJ.Bruce Fields 				    cstate->current_fh.fh_export,
1228d5184658SJ. Bruce Fields 				    cstate->current_fh.fh_dentry,
1229d5184658SJ. Bruce Fields 				    verify->ve_bmval,
1230406a7ea9SFrank Filz 				    rqstp, 0);
123141ae6e71SJ. Bruce Fields 	/*
123241ae6e71SJ. Bruce Fields 	 * If nfsd4_encode_fattr() ran out of space, assume that's because
123341ae6e71SJ. Bruce Fields 	 * the attributes are longer (hence different) than those given:
123441ae6e71SJ. Bruce Fields 	 */
123584822d0bSJ. Bruce Fields 	if (status == nfserr_resource)
12361da177e4SLinus Torvalds 		status = nfserr_not_same;
12371da177e4SLinus Torvalds 	if (status)
12381da177e4SLinus Torvalds 		goto out_kfree;
12391da177e4SLinus Torvalds 
124095ec28cdSBenny Halevy 	/* skip bitmap */
124195ec28cdSBenny Halevy 	p = buf + 1 + ntohl(buf[0]);
12421da177e4SLinus Torvalds 	status = nfserr_not_same;
12431da177e4SLinus Torvalds 	if (ntohl(*p++) != verify->ve_attrlen)
12441da177e4SLinus Torvalds 		goto out_kfree;
12451da177e4SLinus Torvalds 	if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
12461da177e4SLinus Torvalds 		status = nfserr_same;
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds out_kfree:
12491da177e4SLinus Torvalds 	kfree(buf);
12501da177e4SLinus Torvalds 	return status;
12511da177e4SLinus Torvalds }
12521da177e4SLinus Torvalds 
1253c954e2a5SJ.Bruce Fields static __be32
1254c954e2a5SJ.Bruce Fields nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1255eb69853dSChristoph Hellwig 	      union nfsd4_op_u *u)
1256c954e2a5SJ.Bruce Fields {
1257c954e2a5SJ.Bruce Fields 	__be32 status;
1258c954e2a5SJ.Bruce Fields 
1259eb69853dSChristoph Hellwig 	status = _nfsd4_verify(rqstp, cstate, &u->verify);
1260c954e2a5SJ.Bruce Fields 	return status == nfserr_not_same ? nfs_ok : status;
1261c954e2a5SJ.Bruce Fields }
1262c954e2a5SJ.Bruce Fields 
1263c954e2a5SJ.Bruce Fields static __be32
1264c954e2a5SJ.Bruce Fields nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1265eb69853dSChristoph Hellwig 	     union nfsd4_op_u *u)
1266c954e2a5SJ.Bruce Fields {
1267c954e2a5SJ.Bruce Fields 	__be32 status;
1268c954e2a5SJ.Bruce Fields 
1269eb69853dSChristoph Hellwig 	status = _nfsd4_verify(rqstp, cstate, &u->nverify);
1270c954e2a5SJ.Bruce Fields 	return status == nfserr_same ? nfs_ok : status;
1271c954e2a5SJ.Bruce Fields }
1272c954e2a5SJ.Bruce Fields 
12739cf514ccSChristoph Hellwig #ifdef CONFIG_NFSD_PNFS
12749cf514ccSChristoph Hellwig static const struct nfsd4_layout_ops *
12759cf514ccSChristoph Hellwig nfsd4_layout_verify(struct svc_export *exp, unsigned int layout_type)
12769cf514ccSChristoph Hellwig {
12778a4c3926SJeff Layton 	if (!exp->ex_layout_types) {
12789cf514ccSChristoph Hellwig 		dprintk("%s: export does not support pNFS\n", __func__);
12799cf514ccSChristoph Hellwig 		return NULL;
12809cf514ccSChristoph Hellwig 	}
12819cf514ccSChristoph Hellwig 
1282b550a32eSAri Kauppi 	if (layout_type >= LAYOUT_TYPE_MAX ||
1283b550a32eSAri Kauppi 	    !(exp->ex_layout_types & (1 << layout_type))) {
12849cf514ccSChristoph Hellwig 		dprintk("%s: layout type %d not supported\n",
12859cf514ccSChristoph Hellwig 			__func__, layout_type);
12869cf514ccSChristoph Hellwig 		return NULL;
12879cf514ccSChristoph Hellwig 	}
12889cf514ccSChristoph Hellwig 
12899cf514ccSChristoph Hellwig 	return nfsd4_layout_ops[layout_type];
12909cf514ccSChristoph Hellwig }
12919cf514ccSChristoph Hellwig 
12929cf514ccSChristoph Hellwig static __be32
12939cf514ccSChristoph Hellwig nfsd4_getdeviceinfo(struct svc_rqst *rqstp,
1294eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
12959cf514ccSChristoph Hellwig {
1296eb69853dSChristoph Hellwig 	struct nfsd4_getdeviceinfo *gdp = &u->getdeviceinfo;
12979cf514ccSChristoph Hellwig 	const struct nfsd4_layout_ops *ops;
12989cf514ccSChristoph Hellwig 	struct nfsd4_deviceid_map *map;
12999cf514ccSChristoph Hellwig 	struct svc_export *exp;
13009cf514ccSChristoph Hellwig 	__be32 nfserr;
13019cf514ccSChristoph Hellwig 
13029cf514ccSChristoph Hellwig 	dprintk("%s: layout_type %u dev_id [0x%llx:0x%x] maxcnt %u\n",
13039cf514ccSChristoph Hellwig 	       __func__,
13049cf514ccSChristoph Hellwig 	       gdp->gd_layout_type,
13059cf514ccSChristoph Hellwig 	       gdp->gd_devid.fsid_idx, gdp->gd_devid.generation,
13069cf514ccSChristoph Hellwig 	       gdp->gd_maxcount);
13079cf514ccSChristoph Hellwig 
13089cf514ccSChristoph Hellwig 	map = nfsd4_find_devid_map(gdp->gd_devid.fsid_idx);
13099cf514ccSChristoph Hellwig 	if (!map) {
13109cf514ccSChristoph Hellwig 		dprintk("%s: couldn't find device ID to export mapping!\n",
13119cf514ccSChristoph Hellwig 			__func__);
13129cf514ccSChristoph Hellwig 		return nfserr_noent;
13139cf514ccSChristoph Hellwig 	}
13149cf514ccSChristoph Hellwig 
13159cf514ccSChristoph Hellwig 	exp = rqst_exp_find(rqstp, map->fsid_type, map->fsid);
13169cf514ccSChristoph Hellwig 	if (IS_ERR(exp)) {
13179cf514ccSChristoph Hellwig 		dprintk("%s: could not find device id\n", __func__);
13189cf514ccSChristoph Hellwig 		return nfserr_noent;
13199cf514ccSChristoph Hellwig 	}
13209cf514ccSChristoph Hellwig 
13219cf514ccSChristoph Hellwig 	nfserr = nfserr_layoutunavailable;
13229cf514ccSChristoph Hellwig 	ops = nfsd4_layout_verify(exp, gdp->gd_layout_type);
13239cf514ccSChristoph Hellwig 	if (!ops)
13249cf514ccSChristoph Hellwig 		goto out;
13259cf514ccSChristoph Hellwig 
13269cf514ccSChristoph Hellwig 	nfserr = nfs_ok;
1327f99d4fbdSChristoph Hellwig 	if (gdp->gd_maxcount != 0) {
1328f99d4fbdSChristoph Hellwig 		nfserr = ops->proc_getdeviceinfo(exp->ex_path.mnt->mnt_sb,
1329d7c920d1STom Haynes 				rqstp, cstate->session->se_client, gdp);
1330f99d4fbdSChristoph Hellwig 	}
13319cf514ccSChristoph Hellwig 
13329cf514ccSChristoph Hellwig 	gdp->gd_notify_types &= ops->notify_types;
13339cf514ccSChristoph Hellwig out:
1334a1420384SKinglong Mee 	exp_put(exp);
13359cf514ccSChristoph Hellwig 	return nfserr;
13369cf514ccSChristoph Hellwig }
13379cf514ccSChristoph Hellwig 
13389cf514ccSChristoph Hellwig static __be32
13399cf514ccSChristoph Hellwig nfsd4_layoutget(struct svc_rqst *rqstp,
1340eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
13419cf514ccSChristoph Hellwig {
1342eb69853dSChristoph Hellwig 	struct nfsd4_layoutget *lgp = &u->layoutget;
13439cf514ccSChristoph Hellwig 	struct svc_fh *current_fh = &cstate->current_fh;
13449cf514ccSChristoph Hellwig 	const struct nfsd4_layout_ops *ops;
13459cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls;
13469cf514ccSChristoph Hellwig 	__be32 nfserr;
13479cf514ccSChristoph Hellwig 	int accmode;
13489cf514ccSChristoph Hellwig 
13499cf514ccSChristoph Hellwig 	switch (lgp->lg_seg.iomode) {
13509cf514ccSChristoph Hellwig 	case IOMODE_READ:
13519cf514ccSChristoph Hellwig 		accmode = NFSD_MAY_READ;
13529cf514ccSChristoph Hellwig 		break;
13539cf514ccSChristoph Hellwig 	case IOMODE_RW:
13549cf514ccSChristoph Hellwig 		accmode = NFSD_MAY_READ | NFSD_MAY_WRITE;
13559cf514ccSChristoph Hellwig 		break;
13569cf514ccSChristoph Hellwig 	default:
13579cf514ccSChristoph Hellwig 		dprintk("%s: invalid iomode %d\n",
13589cf514ccSChristoph Hellwig 			__func__, lgp->lg_seg.iomode);
13599cf514ccSChristoph Hellwig 		nfserr = nfserr_badiomode;
13609cf514ccSChristoph Hellwig 		goto out;
13619cf514ccSChristoph Hellwig 	}
13629cf514ccSChristoph Hellwig 
13639cf514ccSChristoph Hellwig 	nfserr = fh_verify(rqstp, current_fh, 0, accmode);
13649cf514ccSChristoph Hellwig 	if (nfserr)
13659cf514ccSChristoph Hellwig 		goto out;
13669cf514ccSChristoph Hellwig 
13679cf514ccSChristoph Hellwig 	nfserr = nfserr_layoutunavailable;
13689cf514ccSChristoph Hellwig 	ops = nfsd4_layout_verify(current_fh->fh_export, lgp->lg_layout_type);
13699cf514ccSChristoph Hellwig 	if (!ops)
13709cf514ccSChristoph Hellwig 		goto out;
13719cf514ccSChristoph Hellwig 
13729cf514ccSChristoph Hellwig 	/*
13739cf514ccSChristoph Hellwig 	 * Verify minlength and range as per RFC5661:
13749cf514ccSChristoph Hellwig 	 *  o  If loga_length is less than loga_minlength,
13759cf514ccSChristoph Hellwig 	 *     the metadata server MUST return NFS4ERR_INVAL.
13769cf514ccSChristoph Hellwig 	 *  o  If the sum of loga_offset and loga_minlength exceeds
13779cf514ccSChristoph Hellwig 	 *     NFS4_UINT64_MAX, and loga_minlength is not
13789cf514ccSChristoph Hellwig 	 *     NFS4_UINT64_MAX, the error NFS4ERR_INVAL MUST result.
13799cf514ccSChristoph Hellwig 	 *  o  If the sum of loga_offset and loga_length exceeds
13809cf514ccSChristoph Hellwig 	 *     NFS4_UINT64_MAX, and loga_length is not NFS4_UINT64_MAX,
13819cf514ccSChristoph Hellwig 	 *     the error NFS4ERR_INVAL MUST result.
13829cf514ccSChristoph Hellwig 	 */
13839cf514ccSChristoph Hellwig 	nfserr = nfserr_inval;
13849cf514ccSChristoph Hellwig 	if (lgp->lg_seg.length < lgp->lg_minlength ||
13859cf514ccSChristoph Hellwig 	    (lgp->lg_minlength != NFS4_MAX_UINT64 &&
13869cf514ccSChristoph Hellwig 	     lgp->lg_minlength > NFS4_MAX_UINT64 - lgp->lg_seg.offset) ||
13879cf514ccSChristoph Hellwig 	    (lgp->lg_seg.length != NFS4_MAX_UINT64 &&
13889cf514ccSChristoph Hellwig 	     lgp->lg_seg.length > NFS4_MAX_UINT64 - lgp->lg_seg.offset))
13899cf514ccSChristoph Hellwig 		goto out;
13909cf514ccSChristoph Hellwig 	if (lgp->lg_seg.length == 0)
13919cf514ccSChristoph Hellwig 		goto out;
13929cf514ccSChristoph Hellwig 
13939cf514ccSChristoph Hellwig 	nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lgp->lg_sid,
13949cf514ccSChristoph Hellwig 						true, lgp->lg_layout_type, &ls);
139531ef83dcSChristoph Hellwig 	if (nfserr) {
139631ef83dcSChristoph Hellwig 		trace_layout_get_lookup_fail(&lgp->lg_sid);
13979cf514ccSChristoph Hellwig 		goto out;
139831ef83dcSChristoph Hellwig 	}
13999cf514ccSChristoph Hellwig 
1400c5c707f9SChristoph Hellwig 	nfserr = nfserr_recallconflict;
1401c5c707f9SChristoph Hellwig 	if (atomic_read(&ls->ls_stid.sc_file->fi_lo_recalls))
1402c5c707f9SChristoph Hellwig 		goto out_put_stid;
1403c5c707f9SChristoph Hellwig 
14042b0143b5SDavid Howells 	nfserr = ops->proc_layoutget(d_inode(current_fh->fh_dentry),
14059cf514ccSChristoph Hellwig 				     current_fh, lgp);
14069cf514ccSChristoph Hellwig 	if (nfserr)
14079cf514ccSChristoph Hellwig 		goto out_put_stid;
14089cf514ccSChristoph Hellwig 
14099cf514ccSChristoph Hellwig 	nfserr = nfsd4_insert_layout(lgp, ls);
14109cf514ccSChristoph Hellwig 
14119cf514ccSChristoph Hellwig out_put_stid:
1412cc8a5532SJeff Layton 	mutex_unlock(&ls->ls_mutex);
14139cf514ccSChristoph Hellwig 	nfs4_put_stid(&ls->ls_stid);
14149cf514ccSChristoph Hellwig out:
14159cf514ccSChristoph Hellwig 	return nfserr;
14169cf514ccSChristoph Hellwig }
14179cf514ccSChristoph Hellwig 
14189cf514ccSChristoph Hellwig static __be32
14199cf514ccSChristoph Hellwig nfsd4_layoutcommit(struct svc_rqst *rqstp,
1420eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
14219cf514ccSChristoph Hellwig {
1422eb69853dSChristoph Hellwig 	struct nfsd4_layoutcommit *lcp = &u->layoutcommit;
14239cf514ccSChristoph Hellwig 	const struct nfsd4_layout_seg *seg = &lcp->lc_seg;
14249cf514ccSChristoph Hellwig 	struct svc_fh *current_fh = &cstate->current_fh;
14259cf514ccSChristoph Hellwig 	const struct nfsd4_layout_ops *ops;
14269cf514ccSChristoph Hellwig 	loff_t new_size = lcp->lc_last_wr + 1;
14279cf514ccSChristoph Hellwig 	struct inode *inode;
14289cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls;
14299cf514ccSChristoph Hellwig 	__be32 nfserr;
14309cf514ccSChristoph Hellwig 
14319cf514ccSChristoph Hellwig 	nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_WRITE);
14329cf514ccSChristoph Hellwig 	if (nfserr)
14339cf514ccSChristoph Hellwig 		goto out;
14349cf514ccSChristoph Hellwig 
14359cf514ccSChristoph Hellwig 	nfserr = nfserr_layoutunavailable;
14369cf514ccSChristoph Hellwig 	ops = nfsd4_layout_verify(current_fh->fh_export, lcp->lc_layout_type);
14379cf514ccSChristoph Hellwig 	if (!ops)
14389cf514ccSChristoph Hellwig 		goto out;
14392b0143b5SDavid Howells 	inode = d_inode(current_fh->fh_dentry);
14409cf514ccSChristoph Hellwig 
14419cf514ccSChristoph Hellwig 	nfserr = nfserr_inval;
14429cf514ccSChristoph Hellwig 	if (new_size <= seg->offset) {
14439cf514ccSChristoph Hellwig 		dprintk("pnfsd: last write before layout segment\n");
14449cf514ccSChristoph Hellwig 		goto out;
14459cf514ccSChristoph Hellwig 	}
14469cf514ccSChristoph Hellwig 	if (new_size > seg->offset + seg->length) {
14479cf514ccSChristoph Hellwig 		dprintk("pnfsd: last write beyond layout segment\n");
14489cf514ccSChristoph Hellwig 		goto out;
14499cf514ccSChristoph Hellwig 	}
14509cf514ccSChristoph Hellwig 	if (!lcp->lc_newoffset && new_size > i_size_read(inode)) {
14519cf514ccSChristoph Hellwig 		dprintk("pnfsd: layoutcommit beyond EOF\n");
14529cf514ccSChristoph Hellwig 		goto out;
14539cf514ccSChristoph Hellwig 	}
14549cf514ccSChristoph Hellwig 
14559cf514ccSChristoph Hellwig 	nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lcp->lc_sid,
14569cf514ccSChristoph Hellwig 						false, lcp->lc_layout_type,
14579cf514ccSChristoph Hellwig 						&ls);
14589cf514ccSChristoph Hellwig 	if (nfserr) {
145931ef83dcSChristoph Hellwig 		trace_layout_commit_lookup_fail(&lcp->lc_sid);
14609cf514ccSChristoph Hellwig 		/* fixup error code as per RFC5661 */
14619cf514ccSChristoph Hellwig 		if (nfserr == nfserr_bad_stateid)
14629cf514ccSChristoph Hellwig 			nfserr = nfserr_badlayout;
14639cf514ccSChristoph Hellwig 		goto out;
14649cf514ccSChristoph Hellwig 	}
14659cf514ccSChristoph Hellwig 
1466cc8a5532SJeff Layton 	/* LAYOUTCOMMIT does not require any serialization */
1467cc8a5532SJeff Layton 	mutex_unlock(&ls->ls_mutex);
1468cc8a5532SJeff Layton 
14699cf514ccSChristoph Hellwig 	if (new_size > i_size_read(inode)) {
14709cf514ccSChristoph Hellwig 		lcp->lc_size_chg = 1;
14719cf514ccSChristoph Hellwig 		lcp->lc_newsize = new_size;
14729cf514ccSChristoph Hellwig 	} else {
14739cf514ccSChristoph Hellwig 		lcp->lc_size_chg = 0;
14749cf514ccSChristoph Hellwig 	}
14759cf514ccSChristoph Hellwig 
1476d8398fc1SKinglong Mee 	nfserr = ops->proc_layoutcommit(inode, lcp);
14779cf514ccSChristoph Hellwig 	nfs4_put_stid(&ls->ls_stid);
14789cf514ccSChristoph Hellwig out:
14799cf514ccSChristoph Hellwig 	return nfserr;
14809cf514ccSChristoph Hellwig }
14819cf514ccSChristoph Hellwig 
14829cf514ccSChristoph Hellwig static __be32
14839cf514ccSChristoph Hellwig nfsd4_layoutreturn(struct svc_rqst *rqstp,
1484eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
14859cf514ccSChristoph Hellwig {
1486eb69853dSChristoph Hellwig 	struct nfsd4_layoutreturn *lrp = &u->layoutreturn;
14879cf514ccSChristoph Hellwig 	struct svc_fh *current_fh = &cstate->current_fh;
14889cf514ccSChristoph Hellwig 	__be32 nfserr;
14899cf514ccSChristoph Hellwig 
14909cf514ccSChristoph Hellwig 	nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_NOP);
14919cf514ccSChristoph Hellwig 	if (nfserr)
14929cf514ccSChristoph Hellwig 		goto out;
14939cf514ccSChristoph Hellwig 
14949cf514ccSChristoph Hellwig 	nfserr = nfserr_layoutunavailable;
14959cf514ccSChristoph Hellwig 	if (!nfsd4_layout_verify(current_fh->fh_export, lrp->lr_layout_type))
14969cf514ccSChristoph Hellwig 		goto out;
14979cf514ccSChristoph Hellwig 
14989cf514ccSChristoph Hellwig 	switch (lrp->lr_seg.iomode) {
14999cf514ccSChristoph Hellwig 	case IOMODE_READ:
15009cf514ccSChristoph Hellwig 	case IOMODE_RW:
15019cf514ccSChristoph Hellwig 	case IOMODE_ANY:
15029cf514ccSChristoph Hellwig 		break;
15039cf514ccSChristoph Hellwig 	default:
15049cf514ccSChristoph Hellwig 		dprintk("%s: invalid iomode %d\n", __func__,
15059cf514ccSChristoph Hellwig 			lrp->lr_seg.iomode);
15069cf514ccSChristoph Hellwig 		nfserr = nfserr_inval;
15079cf514ccSChristoph Hellwig 		goto out;
15089cf514ccSChristoph Hellwig 	}
15099cf514ccSChristoph Hellwig 
15109cf514ccSChristoph Hellwig 	switch (lrp->lr_return_type) {
15119cf514ccSChristoph Hellwig 	case RETURN_FILE:
15129cf514ccSChristoph Hellwig 		nfserr = nfsd4_return_file_layouts(rqstp, cstate, lrp);
15139cf514ccSChristoph Hellwig 		break;
15149cf514ccSChristoph Hellwig 	case RETURN_FSID:
15159cf514ccSChristoph Hellwig 	case RETURN_ALL:
15169cf514ccSChristoph Hellwig 		nfserr = nfsd4_return_client_layouts(rqstp, cstate, lrp);
15179cf514ccSChristoph Hellwig 		break;
15189cf514ccSChristoph Hellwig 	default:
15199cf514ccSChristoph Hellwig 		dprintk("%s: invalid return_type %d\n", __func__,
15209cf514ccSChristoph Hellwig 			lrp->lr_return_type);
15219cf514ccSChristoph Hellwig 		nfserr = nfserr_inval;
15229cf514ccSChristoph Hellwig 		break;
15239cf514ccSChristoph Hellwig 	}
15249cf514ccSChristoph Hellwig out:
15259cf514ccSChristoph Hellwig 	return nfserr;
15269cf514ccSChristoph Hellwig }
15279cf514ccSChristoph Hellwig #endif /* CONFIG_NFSD_PNFS */
15289cf514ccSChristoph Hellwig 
15291da177e4SLinus Torvalds /*
15301da177e4SLinus Torvalds  * NULL call.
15311da177e4SLinus Torvalds  */
15327111c66eSAl Viro static __be32
1533a6beb732SChristoph Hellwig nfsd4_proc_null(struct svc_rqst *rqstp)
15341da177e4SLinus Torvalds {
15351da177e4SLinus Torvalds 	return nfs_ok;
15361da177e4SLinus Torvalds }
15371da177e4SLinus Torvalds 
1538e2b20950SShankar Anand static inline void nfsd4_increment_op_stats(u32 opnum)
1539e2b20950SShankar Anand {
1540e2b20950SShankar Anand 	if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
1541e2b20950SShankar Anand 		nfsdstats.nfs4_opcount[opnum]++;
1542e2b20950SShankar Anand }
1543e2b20950SShankar Anand 
1544f9bb94c4SAndy Adamson enum nfsd4_op_flags {
1545f9bb94c4SAndy Adamson 	ALLOWED_WITHOUT_FH = 1 << 0,	/* No current filehandle required */
15465ece3cafSMi Jinlong 	ALLOWED_ON_ABSENT_FS = 1 << 1,	/* ops processed on absent fs */
15475ece3cafSMi Jinlong 	ALLOWED_AS_FIRST_OP = 1 << 2,	/* ops reqired first in compound */
154868d93184SJ. Bruce Fields 	/* For rfc 5661 section 2.6.3.1.1: */
154968d93184SJ. Bruce Fields 	OP_HANDLES_WRONGSEC = 1 << 3,
155068d93184SJ. Bruce Fields 	OP_IS_PUTFH_LIKE = 1 << 4,
15511091006cSJ. Bruce Fields 	/*
1552c856694eSJ. Bruce Fields 	 * These are the ops whose result size we estimate before
1553c856694eSJ. Bruce Fields 	 * encoding, to avoid performing an op then not being able to
1554c856694eSJ. Bruce Fields 	 * respond or cache a response.  This includes writes and setattrs
1555c856694eSJ. Bruce Fields 	 * as well as the operations usually called "nonidempotent":
1556c856694eSJ. Bruce Fields 	 */
1557c856694eSJ. Bruce Fields 	OP_MODIFIES_SOMETHING = 1 << 5,
1558c856694eSJ. Bruce Fields 	/*
1559c856694eSJ. Bruce Fields 	 * Cache compounds containing these ops in the xid-based drc:
15601091006cSJ. Bruce Fields 	 * We use the DRC for compounds containing non-idempotent
15611091006cSJ. Bruce Fields 	 * operations, *except* those that are 4.1-specific (since
15621091006cSJ. Bruce Fields 	 * sessions provide their own EOS), and except for stateful
15631091006cSJ. Bruce Fields 	 * operations other than setclientid and setclientid_confirm
15641091006cSJ. Bruce Fields 	 * (since sequence numbers provide EOS for open, lock, etc in
15651091006cSJ. Bruce Fields 	 * the v4.0 case).
15661091006cSJ. Bruce Fields 	 */
1567c856694eSJ. Bruce Fields 	OP_CACHEME = 1 << 6,
15688b70484cSTigran Mkrtchyan 	/*
15698b70484cSTigran Mkrtchyan 	 * These are ops which clear current state id.
15708b70484cSTigran Mkrtchyan 	 */
15718b70484cSTigran Mkrtchyan 	OP_CLEAR_STATEID = 1 << 7,
1572c856694eSJ. Bruce Fields };
1573c856694eSJ. Bruce Fields 
1574c856694eSJ. Bruce Fields struct nfsd4_operation {
1575eb69853dSChristoph Hellwig 	__be32 (*op_func)(struct svc_rqst *, struct nfsd4_compound_state *,
1576eb69853dSChristoph Hellwig 			union nfsd4_op_u *);
1577c856694eSJ. Bruce Fields 	u32 op_flags;
1578c856694eSJ. Bruce Fields 	char *op_name;
157958e7b33aSMi Jinlong 	/* Try to get response size before operation */
15801c122638SChristoph Hellwig 	u32 (*op_rsize_bop)(struct svc_rqst *, struct nfsd4_op *);
158157832e7bSChristoph Hellwig 	void (*op_get_currentstateid)(struct nfsd4_compound_state *,
158257832e7bSChristoph Hellwig 			union nfsd4_op_u *);
1583b60e9859SChristoph Hellwig 	void (*op_set_currentstateid)(struct nfsd4_compound_state *,
1584b60e9859SChristoph Hellwig 			union nfsd4_op_u *);
1585b591480bSJ.Bruce Fields };
1586b591480bSJ.Bruce Fields 
1587b591480bSJ.Bruce Fields static struct nfsd4_operation nfsd4_ops[];
1588b591480bSJ.Bruce Fields 
1589f1c7f79bSAdrian Bunk static const char *nfsd4_op_name(unsigned opnum);
1590b001a1b6SBenny Halevy 
15911da177e4SLinus Torvalds /*
159257716355SJ. Bruce Fields  * Enforce NFSv4.1 COMPOUND ordering rules:
1593f9bb94c4SAndy Adamson  *
159457716355SJ. Bruce Fields  * Also note, enforced elsewhere:
159557716355SJ. Bruce Fields  *	- SEQUENCE other than as first op results in
159657716355SJ. Bruce Fields  *	  NFS4ERR_SEQUENCE_POS. (Enforced in nfsd4_sequence().)
15971d1bc8f2SJ. Bruce Fields  *	- BIND_CONN_TO_SESSION must be the only op in its compound.
15981d1bc8f2SJ. Bruce Fields  *	  (Enforced in nfsd4_bind_conn_to_session().)
159957716355SJ. Bruce Fields  *	- DESTROY_SESSION must be the final operation in a compound, if
160057716355SJ. Bruce Fields  *	  sessionid's in SEQUENCE and DESTROY_SESSION are the same.
160157716355SJ. Bruce Fields  *	  (Enforced in nfsd4_destroy_session().)
1602f9bb94c4SAndy Adamson  */
160357716355SJ. Bruce Fields static __be32 nfs41_check_op_ordering(struct nfsd4_compoundargs *args)
1604f9bb94c4SAndy Adamson {
1605f9bb94c4SAndy Adamson 	struct nfsd4_op *op = &args->ops[0];
160657716355SJ. Bruce Fields 
160757716355SJ. Bruce Fields 	/* These ordering requirements don't apply to NFSv4.0: */
160857716355SJ. Bruce Fields 	if (args->minorversion == 0)
160957716355SJ. Bruce Fields 		return nfs_ok;
161057716355SJ. Bruce Fields 	/* This is weird, but OK, not our problem: */
161157716355SJ. Bruce Fields 	if (args->opcnt == 0)
161257716355SJ. Bruce Fields 		return nfs_ok;
161357716355SJ. Bruce Fields 	if (op->status == nfserr_op_illegal)
161457716355SJ. Bruce Fields 		return nfs_ok;
161557716355SJ. Bruce Fields 	if (!(nfsd4_ops[op->opnum].op_flags & ALLOWED_AS_FIRST_OP))
161657716355SJ. Bruce Fields 		return nfserr_op_not_in_session;
161757716355SJ. Bruce Fields 	if (op->opnum == OP_SEQUENCE)
161857716355SJ. Bruce Fields 		return nfs_ok;
161957716355SJ. Bruce Fields 	if (args->opcnt != 1)
162057716355SJ. Bruce Fields 		return nfserr_not_only_op;
162157716355SJ. Bruce Fields 	return nfs_ok;
1622f9bb94c4SAndy Adamson }
1623f9bb94c4SAndy Adamson 
162422b03214SJ. Bruce Fields static inline struct nfsd4_operation *OPDESC(struct nfsd4_op *op)
162522b03214SJ. Bruce Fields {
162622b03214SJ. Bruce Fields 	return &nfsd4_ops[op->opnum];
162722b03214SJ. Bruce Fields }
162822b03214SJ. Bruce Fields 
16291091006cSJ. Bruce Fields bool nfsd4_cache_this_op(struct nfsd4_op *op)
16301091006cSJ. Bruce Fields {
1631e372ba60SJ. Bruce Fields 	if (op->opnum == OP_ILLEGAL)
1632e372ba60SJ. Bruce Fields 		return false;
1633c856694eSJ. Bruce Fields 	return OPDESC(op)->op_flags & OP_CACHEME;
16341091006cSJ. Bruce Fields }
16351091006cSJ. Bruce Fields 
163668d93184SJ. Bruce Fields static bool need_wrongsec_check(struct svc_rqst *rqstp)
163768d93184SJ. Bruce Fields {
163868d93184SJ. Bruce Fields 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
163968d93184SJ. Bruce Fields 	struct nfsd4_compoundargs *argp = rqstp->rq_argp;
164068d93184SJ. Bruce Fields 	struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
164168d93184SJ. Bruce Fields 	struct nfsd4_op *next = &argp->ops[resp->opcnt];
164268d93184SJ. Bruce Fields 	struct nfsd4_operation *thisd;
164368d93184SJ. Bruce Fields 	struct nfsd4_operation *nextd;
164468d93184SJ. Bruce Fields 
164568d93184SJ. Bruce Fields 	thisd = OPDESC(this);
164668d93184SJ. Bruce Fields 	/*
164768d93184SJ. Bruce Fields 	 * Most ops check wronsec on our own; only the putfh-like ops
164868d93184SJ. Bruce Fields 	 * have special rules.
164968d93184SJ. Bruce Fields 	 */
165068d93184SJ. Bruce Fields 	if (!(thisd->op_flags & OP_IS_PUTFH_LIKE))
165168d93184SJ. Bruce Fields 		return false;
165268d93184SJ. Bruce Fields 	/*
165368d93184SJ. Bruce Fields 	 * rfc 5661 2.6.3.1.1.6: don't bother erroring out a
165468d93184SJ. Bruce Fields 	 * put-filehandle operation if we're not going to use the
165568d93184SJ. Bruce Fields 	 * result:
165668d93184SJ. Bruce Fields 	 */
165768d93184SJ. Bruce Fields 	if (argp->opcnt == resp->opcnt)
165868d93184SJ. Bruce Fields 		return false;
165951904b08SJ. Bruce Fields 	if (next->opnum == OP_ILLEGAL)
166051904b08SJ. Bruce Fields 		return false;
166168d93184SJ. Bruce Fields 	nextd = OPDESC(next);
166268d93184SJ. Bruce Fields 	/*
166368d93184SJ. Bruce Fields 	 * Rest of 2.6.3.1.1: certain operations will return WRONGSEC
166468d93184SJ. Bruce Fields 	 * errors themselves as necessary; others should check for them
166568d93184SJ. Bruce Fields 	 * now:
166668d93184SJ. Bruce Fields 	 */
166768d93184SJ. Bruce Fields 	return !(nextd->op_flags & OP_HANDLES_WRONGSEC);
166868d93184SJ. Bruce Fields }
166968d93184SJ. Bruce Fields 
16702d124dfaSJ. Bruce Fields static void svcxdr_init_encode(struct svc_rqst *rqstp,
16712d124dfaSJ. Bruce Fields 			       struct nfsd4_compoundres *resp)
16722d124dfaSJ. Bruce Fields {
16732d124dfaSJ. Bruce Fields 	struct xdr_stream *xdr = &resp->xdr;
16742d124dfaSJ. Bruce Fields 	struct xdr_buf *buf = &rqstp->rq_res;
16752d124dfaSJ. Bruce Fields 	struct kvec *head = buf->head;
16762d124dfaSJ. Bruce Fields 
16772d124dfaSJ. Bruce Fields 	xdr->buf = buf;
1678ddd1ea56SJ. Bruce Fields 	xdr->iov = head;
16792d124dfaSJ. Bruce Fields 	xdr->p   = head->iov_base + head->iov_len;
1680a5cddc88SJ. Bruce Fields 	xdr->end = head->iov_base + PAGE_SIZE - rqstp->rq_auth_slack;
16816ac90391SJ. Bruce Fields 	/* Tail and page_len should be zero at this point: */
16826ac90391SJ. Bruce Fields 	buf->len = buf->head[0].iov_len;
16832825a7f9SJ. Bruce Fields 	xdr->scratch.iov_len = 0;
168405638dc7SJ. Bruce Fields 	xdr->page_ptr = buf->pages - 1;
16852825a7f9SJ. Bruce Fields 	buf->buflen = PAGE_SIZE * (1 + rqstp->rq_page_end - buf->pages)
1686a5cddc88SJ. Bruce Fields 		- rqstp->rq_auth_slack;
16872d124dfaSJ. Bruce Fields }
16882d124dfaSJ. Bruce Fields 
1689f9bb94c4SAndy Adamson /*
16901da177e4SLinus Torvalds  * COMPOUND call.
16911da177e4SLinus Torvalds  */
16927111c66eSAl Viro static __be32
1693a6beb732SChristoph Hellwig nfsd4_proc_compound(struct svc_rqst *rqstp)
16941da177e4SLinus Torvalds {
1695a6beb732SChristoph Hellwig 	struct nfsd4_compoundargs *args = rqstp->rq_argp;
1696a6beb732SChristoph Hellwig 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
16971da177e4SLinus Torvalds 	struct nfsd4_op	*op;
1698b591480bSJ.Bruce Fields 	struct nfsd4_operation *opdesc;
1699e354d571SAndy Adamson 	struct nfsd4_compound_state *cstate = &resp->cstate;
17004daeed25SKinglong Mee 	struct svc_fh *current_fh = &cstate->current_fh;
17014daeed25SKinglong Mee 	struct svc_fh *save_fh = &cstate->save_fh;
1702b37ad28bSAl Viro 	__be32		status;
17031da177e4SLinus Torvalds 
17042d124dfaSJ. Bruce Fields 	svcxdr_init_encode(rqstp, resp);
17054aea24b2SJ. Bruce Fields 	resp->tagp = resp->xdr.p;
17061da177e4SLinus Torvalds 	/* reserve space for: taglen, tag, and opcnt */
1707d3f627c8SJ. Bruce Fields 	xdr_reserve_space(&resp->xdr, 8 + args->taglen);
17081da177e4SLinus Torvalds 	resp->taglen = args->taglen;
17091da177e4SLinus Torvalds 	resp->tag = args->tag;
17101da177e4SLinus Torvalds 	resp->rqstp = rqstp;
17114daeed25SKinglong Mee 	cstate->minorversion = args->minorversion;
17124daeed25SKinglong Mee 	fh_init(current_fh, NFS4_FHSIZE);
17134daeed25SKinglong Mee 	fh_init(save_fh, NFS4_FHSIZE);
17148ff30fa4SNeilBrown 	/*
17158ff30fa4SNeilBrown 	 * Don't use the deferral mechanism for NFSv4; compounds make it
17168ff30fa4SNeilBrown 	 * too hard to avoid non-idempotency problems.
17178ff30fa4SNeilBrown 	 */
171830660e04SJeff Layton 	clear_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
17191da177e4SLinus Torvalds 
17201da177e4SLinus Torvalds 	/*
17211da177e4SLinus Torvalds 	 * According to RFC3010, this takes precedence over all other errors.
17221da177e4SLinus Torvalds 	 */
17231da177e4SLinus Torvalds 	status = nfserr_minor_vers_mismatch;
172435f7a14fSJ. Bruce Fields 	if (nfsd_minorversion(args->minorversion, NFSD_TEST) <= 0)
17251da177e4SLinus Torvalds 		goto out;
17261da177e4SLinus Torvalds 
172757716355SJ. Bruce Fields 	status = nfs41_check_op_ordering(args);
172857716355SJ. Bruce Fields 	if (status) {
1729f9bb94c4SAndy Adamson 		op = &args->ops[0];
173057716355SJ. Bruce Fields 		op->status = status;
1731f9bb94c4SAndy Adamson 		goto encode_op;
1732f9bb94c4SAndy Adamson 	}
1733f9bb94c4SAndy Adamson 
17341da177e4SLinus Torvalds 	while (!status && resp->opcnt < args->opcnt) {
17351da177e4SLinus Torvalds 		op = &args->ops[resp->opcnt++];
17361da177e4SLinus Torvalds 
1737b001a1b6SBenny Halevy 		dprintk("nfsv4 compound op #%d/%d: %d (%s)\n",
1738b001a1b6SBenny Halevy 			resp->opcnt, args->opcnt, op->opnum,
1739b001a1b6SBenny Halevy 			nfsd4_op_name(op->opnum));
17401da177e4SLinus Torvalds 		/*
17411da177e4SLinus Torvalds 		 * The XDR decode routines may have pre-set op->status;
17421da177e4SLinus Torvalds 		 * for example, if there is a miscellaneous XDR error
17431da177e4SLinus Torvalds 		 * it will be set to nfserr_bad_xdr.
17441da177e4SLinus Torvalds 		 */
17459d313b17SJ. Bruce Fields 		if (op->status) {
17469d313b17SJ. Bruce Fields 			if (op->opnum == OP_OPEN)
17479d313b17SJ. Bruce Fields 				op->status = nfsd4_open_omfg(rqstp, cstate, op);
17481da177e4SLinus Torvalds 			goto encode_op;
17499d313b17SJ. Bruce Fields 		}
17501da177e4SLinus Torvalds 
175122b03214SJ. Bruce Fields 		opdesc = OPDESC(op);
1752b591480bSJ.Bruce Fields 
17534daeed25SKinglong Mee 		if (!current_fh->fh_dentry) {
175427d630ecSJ.Bruce Fields 			if (!(opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
17551da177e4SLinus Torvalds 				op->status = nfserr_nofilehandle;
17561da177e4SLinus Torvalds 				goto encode_op;
17571da177e4SLinus Torvalds 			}
17584daeed25SKinglong Mee 		} else if (current_fh->fh_export->ex_fslocs.migrated &&
1759eeac294eSJ.Bruce Fields 			  !(opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
176042ca0993SJ.Bruce Fields 			op->status = nfserr_moved;
176142ca0993SJ.Bruce Fields 			goto encode_op;
176242ca0993SJ.Bruce Fields 		}
1763b591480bSJ.Bruce Fields 
17642336745eSKinglong Mee 		fh_clear_wcc(current_fh);
17652336745eSKinglong Mee 
176658e7b33aSMi Jinlong 		/* If op is non-idempotent */
176758e7b33aSMi Jinlong 		if (opdesc->op_flags & OP_MODIFIES_SOMETHING) {
17684c69d585SJ. Bruce Fields 			/*
1769a8095f7eSJ. Bruce Fields 			 * Don't execute this op if we couldn't encode a
1770a8095f7eSJ. Bruce Fields 			 * succesful reply:
1771a8095f7eSJ. Bruce Fields 			 */
1772a8095f7eSJ. Bruce Fields 			u32 plen = opdesc->op_rsize_bop(rqstp, op);
1773a8095f7eSJ. Bruce Fields 			/*
1774a8095f7eSJ. Bruce Fields 			 * Plus if there's another operation, make sure
17754c69d585SJ. Bruce Fields 			 * we'll have space to at least encode an error:
17764c69d585SJ. Bruce Fields 			 */
17774c69d585SJ. Bruce Fields 			if (resp->opcnt < args->opcnt)
17784c69d585SJ. Bruce Fields 				plen += COMPOUND_ERR_SLACK_SPACE;
177958e7b33aSMi Jinlong 			op->status = nfsd4_check_resp_size(resp, plen);
178058e7b33aSMi Jinlong 		}
178158e7b33aSMi Jinlong 
178258e7b33aSMi Jinlong 		if (op->status)
178358e7b33aSMi Jinlong 			goto encode_op;
178458e7b33aSMi Jinlong 
17858b70484cSTigran Mkrtchyan 		if (opdesc->op_get_currentstateid)
17868b70484cSTigran Mkrtchyan 			opdesc->op_get_currentstateid(cstate, &op->u);
1787b591480bSJ.Bruce Fields 		op->status = opdesc->op_func(rqstp, cstate, &op->u);
17881da177e4SLinus Torvalds 
17898b70484cSTigran Mkrtchyan 		if (!op->status) {
17908b70484cSTigran Mkrtchyan 			if (opdesc->op_set_currentstateid)
17918b70484cSTigran Mkrtchyan 				opdesc->op_set_currentstateid(cstate, &op->u);
17928b70484cSTigran Mkrtchyan 
17938b70484cSTigran Mkrtchyan 			if (opdesc->op_flags & OP_CLEAR_STATEID)
179437c593c5STigran Mkrtchyan 				clear_current_stateid(cstate);
17958b70484cSTigran Mkrtchyan 
17968b70484cSTigran Mkrtchyan 			if (need_wrongsec_check(rqstp))
17974daeed25SKinglong Mee 				op->status = check_nfsd_access(current_fh->fh_export, rqstp);
17988b70484cSTigran Mkrtchyan 		}
179968d93184SJ. Bruce Fields 
18001da177e4SLinus Torvalds encode_op:
180149557cc7SAndy Adamson 		/* Only from SEQUENCE */
18024daeed25SKinglong Mee 		if (cstate->status == nfserr_replay_cache) {
1803da3846a2SAndy Adamson 			dprintk("%s NFS4.1 replay from cache\n", __func__);
1804da3846a2SAndy Adamson 			status = op->status;
1805da3846a2SAndy Adamson 			goto out;
1806da3846a2SAndy Adamson 		}
1807a90b061cSAl Viro 		if (op->status == nfserr_replay_me) {
1808a4f1706aSJ.Bruce Fields 			op->replay = &cstate->replay_owner->so_replay;
1809d0a381ddSJ. Bruce Fields 			nfsd4_encode_replay(&resp->xdr, op);
18101da177e4SLinus Torvalds 			status = op->status = op->replay->rp_status;
18111da177e4SLinus Torvalds 		} else {
18121da177e4SLinus Torvalds 			nfsd4_encode_operation(resp, op);
18131da177e4SLinus Torvalds 			status = op->status;
18141da177e4SLinus Torvalds 		}
18150407717dSBenny Halevy 
18160407717dSBenny Halevy 		dprintk("nfsv4 compound op %p opcnt %d #%d: %d: status %d\n",
18170407717dSBenny Halevy 			args->ops, args->opcnt, resp->opcnt, op->opnum,
18180407717dSBenny Halevy 			be32_to_cpu(status));
18190407717dSBenny Halevy 
182058fb12e6SJeff Layton 		nfsd4_cstate_clear_replay(cstate);
1821e2b20950SShankar Anand 		nfsd4_increment_op_stats(op->opnum);
18221da177e4SLinus Torvalds 	}
18231da177e4SLinus Torvalds 
18244daeed25SKinglong Mee 	cstate->status = status;
18254daeed25SKinglong Mee 	fh_put(current_fh);
18264daeed25SKinglong Mee 	fh_put(save_fh);
18274daeed25SKinglong Mee 	BUG_ON(cstate->replay_owner);
18281da177e4SLinus Torvalds out:
18292f425878SAndy Adamson 	/* Reset deferral mechanism for RPC deferrals */
183030660e04SJeff Layton 	set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
18313b12cd98SJ. Bruce Fields 	dprintk("nfsv4 compound returned %d\n", ntohl(status));
18321da177e4SLinus Torvalds 	return status;
18331da177e4SLinus Torvalds }
18341da177e4SLinus Torvalds 
183558e7b33aSMi Jinlong #define op_encode_hdr_size		(2)
183658e7b33aSMi Jinlong #define op_encode_stateid_maxsz		(XDR_QUADLEN(NFS4_STATEID_SIZE))
183758e7b33aSMi Jinlong #define op_encode_verifier_maxsz	(XDR_QUADLEN(NFS4_VERIFIER_SIZE))
183858e7b33aSMi Jinlong #define op_encode_change_info_maxsz	(5)
183958e7b33aSMi Jinlong #define nfs4_fattr_bitmap_maxsz		(4)
184058e7b33aSMi Jinlong 
18418c7424cfSJ. Bruce Fields /* We'll fall back on returning no lockowner if run out of space: */
18428c7424cfSJ. Bruce Fields #define op_encode_lockowner_maxsz	(0)
184358e7b33aSMi Jinlong #define op_encode_lock_denied_maxsz	(8 + op_encode_lockowner_maxsz)
184458e7b33aSMi Jinlong 
184558e7b33aSMi Jinlong #define nfs4_owner_maxsz		(1 + XDR_QUADLEN(IDMAP_NAMESZ))
184658e7b33aSMi Jinlong 
184758e7b33aSMi Jinlong #define op_encode_ace_maxsz		(3 + nfs4_owner_maxsz)
184858e7b33aSMi Jinlong #define op_encode_delegation_maxsz	(1 + op_encode_stateid_maxsz + 1 + \
184958e7b33aSMi Jinlong 					 op_encode_ace_maxsz)
185058e7b33aSMi Jinlong 
185158e7b33aSMi Jinlong #define op_encode_channel_attrs_maxsz	(6 + 1 + 1)
185258e7b33aSMi Jinlong 
185358e7b33aSMi Jinlong static inline u32 nfsd4_only_status_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
185458e7b33aSMi Jinlong {
185558e7b33aSMi Jinlong 	return (op_encode_hdr_size) * sizeof(__be32);
185658e7b33aSMi Jinlong }
185758e7b33aSMi Jinlong 
185858e7b33aSMi Jinlong static inline u32 nfsd4_status_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
185958e7b33aSMi Jinlong {
186058e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_stateid_maxsz)* sizeof(__be32);
186158e7b33aSMi Jinlong }
186258e7b33aSMi Jinlong 
18632282cd2cSKinglong Mee static inline u32 nfsd4_access_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
18642282cd2cSKinglong Mee {
18652282cd2cSKinglong Mee 	/* ac_supported, ac_resp_access */
18662282cd2cSKinglong Mee 	return (op_encode_hdr_size + 2)* sizeof(__be32);
18672282cd2cSKinglong Mee }
18682282cd2cSKinglong Mee 
186958e7b33aSMi Jinlong static inline u32 nfsd4_commit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
187058e7b33aSMi Jinlong {
187158e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_verifier_maxsz) * sizeof(__be32);
187258e7b33aSMi Jinlong }
187358e7b33aSMi Jinlong 
187458e7b33aSMi Jinlong static inline u32 nfsd4_create_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
187558e7b33aSMi Jinlong {
187658e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_change_info_maxsz
187758e7b33aSMi Jinlong 		+ nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
187858e7b33aSMi Jinlong }
187958e7b33aSMi Jinlong 
1880b86cef60SJ. Bruce Fields /*
1881b86cef60SJ. Bruce Fields  * Note since this is an idempotent operation we won't insist on failing
1882b86cef60SJ. Bruce Fields  * the op prematurely if the estimate is too large.  We may turn off splice
1883b86cef60SJ. Bruce Fields  * reads unnecessarily.
1884b86cef60SJ. Bruce Fields  */
1885b86cef60SJ. Bruce Fields static inline u32 nfsd4_getattr_rsize(struct svc_rqst *rqstp,
1886b86cef60SJ. Bruce Fields 				      struct nfsd4_op *op)
1887b86cef60SJ. Bruce Fields {
1888b86cef60SJ. Bruce Fields 	u32 *bmap = op->u.getattr.ga_bmval;
1889b86cef60SJ. Bruce Fields 	u32 bmap0 = bmap[0], bmap1 = bmap[1], bmap2 = bmap[2];
1890b86cef60SJ. Bruce Fields 	u32 ret = 0;
1891b86cef60SJ. Bruce Fields 
1892b86cef60SJ. Bruce Fields 	if (bmap0 & FATTR4_WORD0_ACL)
1893b86cef60SJ. Bruce Fields 		return svc_max_payload(rqstp);
1894b86cef60SJ. Bruce Fields 	if (bmap0 & FATTR4_WORD0_FS_LOCATIONS)
1895b86cef60SJ. Bruce Fields 		return svc_max_payload(rqstp);
1896b86cef60SJ. Bruce Fields 
1897b86cef60SJ. Bruce Fields 	if (bmap1 & FATTR4_WORD1_OWNER) {
1898b86cef60SJ. Bruce Fields 		ret += IDMAP_NAMESZ + 4;
1899b86cef60SJ. Bruce Fields 		bmap1 &= ~FATTR4_WORD1_OWNER;
1900b86cef60SJ. Bruce Fields 	}
1901b86cef60SJ. Bruce Fields 	if (bmap1 & FATTR4_WORD1_OWNER_GROUP) {
1902b86cef60SJ. Bruce Fields 		ret += IDMAP_NAMESZ + 4;
1903b86cef60SJ. Bruce Fields 		bmap1 &= ~FATTR4_WORD1_OWNER_GROUP;
1904b86cef60SJ. Bruce Fields 	}
1905b86cef60SJ. Bruce Fields 	if (bmap0 & FATTR4_WORD0_FILEHANDLE) {
1906b86cef60SJ. Bruce Fields 		ret += NFS4_FHSIZE + 4;
1907b86cef60SJ. Bruce Fields 		bmap0 &= ~FATTR4_WORD0_FILEHANDLE;
1908b86cef60SJ. Bruce Fields 	}
1909b86cef60SJ. Bruce Fields 	if (bmap2 & FATTR4_WORD2_SECURITY_LABEL) {
19101ec8c0c4SKinglong Mee 		ret += NFS4_MAXLABELLEN + 12;
1911b86cef60SJ. Bruce Fields 		bmap2 &= ~FATTR4_WORD2_SECURITY_LABEL;
1912b86cef60SJ. Bruce Fields 	}
1913b86cef60SJ. Bruce Fields 	/*
1914b86cef60SJ. Bruce Fields 	 * Largest of remaining attributes are 16 bytes (e.g.,
1915b86cef60SJ. Bruce Fields 	 * supported_attributes)
1916b86cef60SJ. Bruce Fields 	 */
1917b86cef60SJ. Bruce Fields 	ret += 16 * (hweight32(bmap0) + hweight32(bmap1) + hweight32(bmap2));
1918b86cef60SJ. Bruce Fields 	/* bitmask, length */
1919b86cef60SJ. Bruce Fields 	ret += 20;
1920b86cef60SJ. Bruce Fields 	return ret;
1921b86cef60SJ. Bruce Fields }
1922b86cef60SJ. Bruce Fields 
19232282cd2cSKinglong Mee static inline u32 nfsd4_getfh_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
19242282cd2cSKinglong Mee {
19252282cd2cSKinglong Mee 	return (op_encode_hdr_size + 1) * sizeof(__be32) + NFS4_FHSIZE;
19262282cd2cSKinglong Mee }
19272282cd2cSKinglong Mee 
192858e7b33aSMi Jinlong static inline u32 nfsd4_link_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
192958e7b33aSMi Jinlong {
193058e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
193158e7b33aSMi Jinlong 		* sizeof(__be32);
193258e7b33aSMi Jinlong }
193358e7b33aSMi Jinlong 
193458e7b33aSMi Jinlong static inline u32 nfsd4_lock_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
193558e7b33aSMi Jinlong {
193658e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_lock_denied_maxsz)
193758e7b33aSMi Jinlong 		* sizeof(__be32);
193858e7b33aSMi Jinlong }
193958e7b33aSMi Jinlong 
194058e7b33aSMi Jinlong static inline u32 nfsd4_open_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
194158e7b33aSMi Jinlong {
194258e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_stateid_maxsz
194358e7b33aSMi Jinlong 		+ op_encode_change_info_maxsz + 1
194458e7b33aSMi Jinlong 		+ nfs4_fattr_bitmap_maxsz
194558e7b33aSMi Jinlong 		+ op_encode_delegation_maxsz) * sizeof(__be32);
194658e7b33aSMi Jinlong }
194758e7b33aSMi Jinlong 
194858e7b33aSMi Jinlong static inline u32 nfsd4_read_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
194958e7b33aSMi Jinlong {
195058e7b33aSMi Jinlong 	u32 maxcount = 0, rlen = 0;
195158e7b33aSMi Jinlong 
195258e7b33aSMi Jinlong 	maxcount = svc_max_payload(rqstp);
19533c7aa15dSKinglong Mee 	rlen = min(op->u.read.rd_length, maxcount);
195458e7b33aSMi Jinlong 
1955622f560eSJ. Bruce Fields 	return (op_encode_hdr_size + 2 + XDR_QUADLEN(rlen)) * sizeof(__be32);
195658e7b33aSMi Jinlong }
195758e7b33aSMi Jinlong 
195858e7b33aSMi Jinlong static inline u32 nfsd4_readdir_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
195958e7b33aSMi Jinlong {
19603c7aa15dSKinglong Mee 	u32 maxcount = 0, rlen = 0;
196158e7b33aSMi Jinlong 
19623c7aa15dSKinglong Mee 	maxcount = svc_max_payload(rqstp);
19633c7aa15dSKinglong Mee 	rlen = min(op->u.readdir.rd_maxcount, maxcount);
196458e7b33aSMi Jinlong 
1965561f0ed4SJ. Bruce Fields 	return (op_encode_hdr_size + op_encode_verifier_maxsz +
1966561f0ed4SJ. Bruce Fields 		XDR_QUADLEN(rlen)) * sizeof(__be32);
196758e7b33aSMi Jinlong }
196858e7b33aSMi Jinlong 
19692282cd2cSKinglong Mee static inline u32 nfsd4_readlink_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
19702282cd2cSKinglong Mee {
19712282cd2cSKinglong Mee 	return (op_encode_hdr_size + 1) * sizeof(__be32) + PAGE_SIZE;
19722282cd2cSKinglong Mee }
19732282cd2cSKinglong Mee 
197458e7b33aSMi Jinlong static inline u32 nfsd4_remove_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
197558e7b33aSMi Jinlong {
197658e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
197758e7b33aSMi Jinlong 		* sizeof(__be32);
197858e7b33aSMi Jinlong }
197958e7b33aSMi Jinlong 
198058e7b33aSMi Jinlong static inline u32 nfsd4_rename_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
198158e7b33aSMi Jinlong {
198258e7b33aSMi Jinlong 	return (op_encode_hdr_size + op_encode_change_info_maxsz
198358e7b33aSMi Jinlong 		+ op_encode_change_info_maxsz) * sizeof(__be32);
198458e7b33aSMi Jinlong }
198558e7b33aSMi Jinlong 
1986ccae70a9SJ. Bruce Fields static inline u32 nfsd4_sequence_rsize(struct svc_rqst *rqstp,
1987ccae70a9SJ. Bruce Fields 				       struct nfsd4_op *op)
1988ccae70a9SJ. Bruce Fields {
1989d1d84c96SJ. Bruce Fields 	return (op_encode_hdr_size
1990d1d84c96SJ. Bruce Fields 		+ XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 5) * sizeof(__be32);
1991ccae70a9SJ. Bruce Fields }
1992ccae70a9SJ. Bruce Fields 
19932282cd2cSKinglong Mee static inline u32 nfsd4_test_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
19942282cd2cSKinglong Mee {
19952282cd2cSKinglong Mee 	return (op_encode_hdr_size + 1 + op->u.test_stateid.ts_num_ids)
19962282cd2cSKinglong Mee 		* sizeof(__be32);
19972282cd2cSKinglong Mee }
19982282cd2cSKinglong Mee 
199958e7b33aSMi Jinlong static inline u32 nfsd4_setattr_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
200058e7b33aSMi Jinlong {
200158e7b33aSMi Jinlong 	return (op_encode_hdr_size + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
200258e7b33aSMi Jinlong }
200358e7b33aSMi Jinlong 
20042282cd2cSKinglong Mee static inline u32 nfsd4_secinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
20052282cd2cSKinglong Mee {
20062282cd2cSKinglong Mee 	return (op_encode_hdr_size + RPC_AUTH_MAXFLAVOR *
20072282cd2cSKinglong Mee 		(4 + XDR_QUADLEN(GSS_OID_MAX_LEN))) * sizeof(__be32);
20082282cd2cSKinglong Mee }
20092282cd2cSKinglong Mee 
201058e7b33aSMi Jinlong static inline u32 nfsd4_setclientid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
201158e7b33aSMi Jinlong {
2012480efaeeSJ. Bruce Fields 	return (op_encode_hdr_size + 2 + XDR_QUADLEN(NFS4_VERIFIER_SIZE)) *
2013480efaeeSJ. Bruce Fields 								sizeof(__be32);
201458e7b33aSMi Jinlong }
201558e7b33aSMi Jinlong 
201658e7b33aSMi Jinlong static inline u32 nfsd4_write_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
201758e7b33aSMi Jinlong {
2018f34e432bSJ. Bruce Fields 	return (op_encode_hdr_size + 2 + op_encode_verifier_maxsz) * sizeof(__be32);
201958e7b33aSMi Jinlong }
202058e7b33aSMi Jinlong 
202158e7b33aSMi Jinlong static inline u32 nfsd4_exchange_id_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
202258e7b33aSMi Jinlong {
202358e7b33aSMi Jinlong 	return (op_encode_hdr_size + 2 + 1 + /* eir_clientid, eir_sequenceid */\
2024a8bb84bcSKinglong Mee 		1 + 1 + /* eir_flags, spr_how */\
2025a8bb84bcSKinglong Mee 		4 + /* spo_must_enforce & _allow with bitmap */\
202658e7b33aSMi Jinlong 		2 + /*eir_server_owner.so_minor_id */\
202758e7b33aSMi Jinlong 		/* eir_server_owner.so_major_id<> */\
202858e7b33aSMi Jinlong 		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
202958e7b33aSMi Jinlong 		/* eir_server_scope<> */\
203058e7b33aSMi Jinlong 		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
203158e7b33aSMi Jinlong 		1 + /* eir_server_impl_id array length */\
203258e7b33aSMi Jinlong 		0 /* ignored eir_server_impl_id contents */) * sizeof(__be32);
203358e7b33aSMi Jinlong }
203458e7b33aSMi Jinlong 
203558e7b33aSMi Jinlong static inline u32 nfsd4_bind_conn_to_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
203658e7b33aSMi Jinlong {
203758e7b33aSMi Jinlong 	return (op_encode_hdr_size + \
203858e7b33aSMi Jinlong 		XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* bctsr_sessid */\
203958e7b33aSMi Jinlong 		2 /* bctsr_dir, use_conn_in_rdma_mode */) * sizeof(__be32);
204058e7b33aSMi Jinlong }
204158e7b33aSMi Jinlong 
204258e7b33aSMi Jinlong static inline u32 nfsd4_create_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
204358e7b33aSMi Jinlong {
204458e7b33aSMi Jinlong 	return (op_encode_hdr_size + \
204558e7b33aSMi Jinlong 		XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* sessionid */\
204658e7b33aSMi Jinlong 		2 + /* csr_sequence, csr_flags */\
204758e7b33aSMi Jinlong 		op_encode_channel_attrs_maxsz + \
204858e7b33aSMi Jinlong 		op_encode_channel_attrs_maxsz) * sizeof(__be32);
204958e7b33aSMi Jinlong }
205058e7b33aSMi Jinlong 
205129ae7f9dSAnna Schumaker static inline u32 nfsd4_copy_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
205229ae7f9dSAnna Schumaker {
205329ae7f9dSAnna Schumaker 	return (op_encode_hdr_size +
205429ae7f9dSAnna Schumaker 		1 /* wr_callback */ +
205529ae7f9dSAnna Schumaker 		op_encode_stateid_maxsz /* wr_callback */ +
205629ae7f9dSAnna Schumaker 		2 /* wr_count */ +
205729ae7f9dSAnna Schumaker 		1 /* wr_committed */ +
205829ae7f9dSAnna Schumaker 		op_encode_verifier_maxsz +
205929ae7f9dSAnna Schumaker 		1 /* cr_consecutive */ +
206029ae7f9dSAnna Schumaker 		1 /* cr_synchronous */) * sizeof(__be32);
206129ae7f9dSAnna Schumaker }
206229ae7f9dSAnna Schumaker 
20639cf514ccSChristoph Hellwig #ifdef CONFIG_NFSD_PNFS
20642282cd2cSKinglong Mee static inline u32 nfsd4_getdeviceinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
20652282cd2cSKinglong Mee {
20662282cd2cSKinglong Mee 	u32 maxcount = 0, rlen = 0;
20672282cd2cSKinglong Mee 
20682282cd2cSKinglong Mee 	maxcount = svc_max_payload(rqstp);
20692282cd2cSKinglong Mee 	rlen = min(op->u.getdeviceinfo.gd_maxcount, maxcount);
20702282cd2cSKinglong Mee 
20712282cd2cSKinglong Mee 	return (op_encode_hdr_size +
20722282cd2cSKinglong Mee 		1 /* gd_layout_type*/ +
20732282cd2cSKinglong Mee 		XDR_QUADLEN(rlen) +
20742282cd2cSKinglong Mee 		2 /* gd_notify_types */) * sizeof(__be32);
20752282cd2cSKinglong Mee }
20762282cd2cSKinglong Mee 
20779cf514ccSChristoph Hellwig /*
20789cf514ccSChristoph Hellwig  * At this stage we don't really know what layout driver will handle the request,
20799cf514ccSChristoph Hellwig  * so we need to define an arbitrary upper bound here.
20809cf514ccSChristoph Hellwig  */
20819cf514ccSChristoph Hellwig #define MAX_LAYOUT_SIZE		128
20829cf514ccSChristoph Hellwig static inline u32 nfsd4_layoutget_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
20839cf514ccSChristoph Hellwig {
20849cf514ccSChristoph Hellwig 	return (op_encode_hdr_size +
20859cf514ccSChristoph Hellwig 		1 /* logr_return_on_close */ +
20869cf514ccSChristoph Hellwig 		op_encode_stateid_maxsz +
20879cf514ccSChristoph Hellwig 		1 /* nr of layouts */ +
20889cf514ccSChristoph Hellwig 		MAX_LAYOUT_SIZE) * sizeof(__be32);
20899cf514ccSChristoph Hellwig }
20909cf514ccSChristoph Hellwig 
20919cf514ccSChristoph Hellwig static inline u32 nfsd4_layoutcommit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
20929cf514ccSChristoph Hellwig {
20939cf514ccSChristoph Hellwig 	return (op_encode_hdr_size +
20949cf514ccSChristoph Hellwig 		1 /* locr_newsize */ +
20959cf514ccSChristoph Hellwig 		2 /* ns_size */) * sizeof(__be32);
20969cf514ccSChristoph Hellwig }
20979cf514ccSChristoph Hellwig 
20989cf514ccSChristoph Hellwig static inline u32 nfsd4_layoutreturn_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
20999cf514ccSChristoph Hellwig {
21009cf514ccSChristoph Hellwig 	return (op_encode_hdr_size +
21019cf514ccSChristoph Hellwig 		1 /* lrs_stateid */ +
21029cf514ccSChristoph Hellwig 		op_encode_stateid_maxsz) * sizeof(__be32);
21039cf514ccSChristoph Hellwig }
21049cf514ccSChristoph Hellwig #endif /* CONFIG_NFSD_PNFS */
21059cf514ccSChristoph Hellwig 
21062282cd2cSKinglong Mee 
21072282cd2cSKinglong Mee static inline u32 nfsd4_seek_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
21082282cd2cSKinglong Mee {
21092282cd2cSKinglong Mee 	return (op_encode_hdr_size + 3) * sizeof(__be32);
21102282cd2cSKinglong Mee }
21112282cd2cSKinglong Mee 
211220766016SBenny Halevy static struct nfsd4_operation nfsd4_ops[] = {
2113b591480bSJ.Bruce Fields 	[OP_ACCESS] = {
2114eb69853dSChristoph Hellwig 		.op_func = nfsd4_access,
2115b001a1b6SBenny Halevy 		.op_name = "OP_ACCESS",
21161c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_access_rsize,
2117b591480bSJ.Bruce Fields 	},
2118b591480bSJ.Bruce Fields 	[OP_CLOSE] = {
2119eb69853dSChristoph Hellwig 		.op_func = nfsd4_close,
212058e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
2121b001a1b6SBenny Halevy 		.op_name = "OP_CLOSE",
21221c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_status_stateid_rsize,
212357832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_closestateid,
2124b60e9859SChristoph Hellwig 		.op_set_currentstateid = nfsd4_set_closestateid,
2125b591480bSJ.Bruce Fields 	},
2126b591480bSJ.Bruce Fields 	[OP_COMMIT] = {
2127eb69853dSChristoph Hellwig 		.op_func = nfsd4_commit,
212858e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
2129b001a1b6SBenny Halevy 		.op_name = "OP_COMMIT",
21301c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_commit_rsize,
2131b591480bSJ.Bruce Fields 	},
2132b591480bSJ.Bruce Fields 	[OP_CREATE] = {
2133eb69853dSChristoph Hellwig 		.op_func = nfsd4_create,
2134d1471053STigran Mkrtchyan 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME | OP_CLEAR_STATEID,
2135b001a1b6SBenny Halevy 		.op_name = "OP_CREATE",
21361c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_create_rsize,
2137b591480bSJ.Bruce Fields 	},
2138b591480bSJ.Bruce Fields 	[OP_DELEGRETURN] = {
2139eb69853dSChristoph Hellwig 		.op_func = nfsd4_delegreturn,
214058e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
2141b001a1b6SBenny Halevy 		.op_name = "OP_DELEGRETURN",
214258e7b33aSMi Jinlong 		.op_rsize_bop = nfsd4_only_status_rsize,
214357832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_delegreturnstateid,
2144b591480bSJ.Bruce Fields 	},
2145b591480bSJ.Bruce Fields 	[OP_GETATTR] = {
2146eb69853dSChristoph Hellwig 		.op_func = nfsd4_getattr,
2147eeac294eSJ.Bruce Fields 		.op_flags = ALLOWED_ON_ABSENT_FS,
2148b86cef60SJ. Bruce Fields 		.op_rsize_bop = nfsd4_getattr_rsize,
2149b001a1b6SBenny Halevy 		.op_name = "OP_GETATTR",
2150b591480bSJ.Bruce Fields 	},
2151b591480bSJ.Bruce Fields 	[OP_GETFH] = {
2152eb69853dSChristoph Hellwig 		.op_func = nfsd4_getfh,
2153b001a1b6SBenny Halevy 		.op_name = "OP_GETFH",
21541c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_getfh_rsize,
2155b591480bSJ.Bruce Fields 	},
2156b591480bSJ.Bruce Fields 	[OP_LINK] = {
2157eb69853dSChristoph Hellwig 		.op_func = nfsd4_link,
2158c856694eSJ. Bruce Fields 		.op_flags = ALLOWED_ON_ABSENT_FS | OP_MODIFIES_SOMETHING
2159c856694eSJ. Bruce Fields 				| OP_CACHEME,
2160b001a1b6SBenny Halevy 		.op_name = "OP_LINK",
21611c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_link_rsize,
2162b591480bSJ.Bruce Fields 	},
2163b591480bSJ.Bruce Fields 	[OP_LOCK] = {
2164eb69853dSChristoph Hellwig 		.op_func = nfsd4_lock,
216558e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
2166b001a1b6SBenny Halevy 		.op_name = "OP_LOCK",
21671c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_lock_rsize,
2168b60e9859SChristoph Hellwig 		.op_set_currentstateid = nfsd4_set_lockstateid,
2169b591480bSJ.Bruce Fields 	},
2170b591480bSJ.Bruce Fields 	[OP_LOCKT] = {
2171eb69853dSChristoph Hellwig 		.op_func = nfsd4_lockt,
2172b001a1b6SBenny Halevy 		.op_name = "OP_LOCKT",
21731c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_lock_rsize,
2174b591480bSJ.Bruce Fields 	},
2175b591480bSJ.Bruce Fields 	[OP_LOCKU] = {
2176eb69853dSChristoph Hellwig 		.op_func = nfsd4_locku,
217758e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
2178b001a1b6SBenny Halevy 		.op_name = "OP_LOCKU",
21791c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_status_stateid_rsize,
218057832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_lockustateid,
2181b591480bSJ.Bruce Fields 	},
2182b591480bSJ.Bruce Fields 	[OP_LOOKUP] = {
2183eb69853dSChristoph Hellwig 		.op_func = nfsd4_lookup,
2184d1471053STigran Mkrtchyan 		.op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
2185b001a1b6SBenny Halevy 		.op_name = "OP_LOOKUP",
21861c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2187b591480bSJ.Bruce Fields 	},
2188b591480bSJ.Bruce Fields 	[OP_LOOKUPP] = {
2189eb69853dSChristoph Hellwig 		.op_func = nfsd4_lookupp,
2190d1471053STigran Mkrtchyan 		.op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
2191b001a1b6SBenny Halevy 		.op_name = "OP_LOOKUPP",
21921c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2193b591480bSJ.Bruce Fields 	},
2194b591480bSJ.Bruce Fields 	[OP_NVERIFY] = {
2195eb69853dSChristoph Hellwig 		.op_func = nfsd4_nverify,
2196b001a1b6SBenny Halevy 		.op_name = "OP_NVERIFY",
21971c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2198b591480bSJ.Bruce Fields 	},
2199b591480bSJ.Bruce Fields 	[OP_OPEN] = {
2200eb69853dSChristoph Hellwig 		.op_func = nfsd4_open,
220158e7b33aSMi Jinlong 		.op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
2202b001a1b6SBenny Halevy 		.op_name = "OP_OPEN",
22031c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_open_rsize,
2204b60e9859SChristoph Hellwig 		.op_set_currentstateid = nfsd4_set_openstateid,
2205b591480bSJ.Bruce Fields 	},
2206b591480bSJ.Bruce Fields 	[OP_OPEN_CONFIRM] = {
2207eb69853dSChristoph Hellwig 		.op_func = nfsd4_open_confirm,
220858e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
2209b001a1b6SBenny Halevy 		.op_name = "OP_OPEN_CONFIRM",
22101c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_status_stateid_rsize,
2211b591480bSJ.Bruce Fields 	},
2212b591480bSJ.Bruce Fields 	[OP_OPEN_DOWNGRADE] = {
2213eb69853dSChristoph Hellwig 		.op_func = nfsd4_open_downgrade,
221458e7b33aSMi Jinlong 		.op_flags = OP_MODIFIES_SOMETHING,
2215b001a1b6SBenny Halevy 		.op_name = "OP_OPEN_DOWNGRADE",
22161c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_status_stateid_rsize,
221757832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_opendowngradestateid,
2218b60e9859SChristoph Hellwig 		.op_set_currentstateid = nfsd4_set_opendowngradestateid,
2219b591480bSJ.Bruce Fields 	},
2220b591480bSJ.Bruce Fields 	[OP_PUTFH] = {
2221eb69853dSChristoph Hellwig 		.op_func = nfsd4_putfh,
222268d93184SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
22235b648699SJ. Bruce Fields 				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
2224b001a1b6SBenny Halevy 		.op_name = "OP_PUTFH",
22251c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2226b591480bSJ.Bruce Fields 	},
2227eeac294eSJ.Bruce Fields 	[OP_PUTPUBFH] = {
2228eb69853dSChristoph Hellwig 		.op_func = nfsd4_putrootfh,
222968d93184SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
22305b648699SJ. Bruce Fields 				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
2231b001a1b6SBenny Halevy 		.op_name = "OP_PUTPUBFH",
22321c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2233eeac294eSJ.Bruce Fields 	},
2234b591480bSJ.Bruce Fields 	[OP_PUTROOTFH] = {
2235eb69853dSChristoph Hellwig 		.op_func = nfsd4_putrootfh,
223668d93184SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
22375b648699SJ. Bruce Fields 				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
2238b001a1b6SBenny Halevy 		.op_name = "OP_PUTROOTFH",
22391c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2240b591480bSJ.Bruce Fields 	},
2241b591480bSJ.Bruce Fields 	[OP_READ] = {
2242eb69853dSChristoph Hellwig 		.op_func = nfsd4_read,
2243b001a1b6SBenny Halevy 		.op_name = "OP_READ",
22441c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_read_rsize,
224557832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_readstateid,
2246b591480bSJ.Bruce Fields 	},
2247b591480bSJ.Bruce Fields 	[OP_READDIR] = {
2248eb69853dSChristoph Hellwig 		.op_func = nfsd4_readdir,
2249b001a1b6SBenny Halevy 		.op_name = "OP_READDIR",
22501c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_readdir_rsize,
2251b591480bSJ.Bruce Fields 	},
2252b591480bSJ.Bruce Fields 	[OP_READLINK] = {
2253eb69853dSChristoph Hellwig 		.op_func = nfsd4_readlink,
2254b001a1b6SBenny Halevy 		.op_name = "OP_READLINK",
22551c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_readlink_rsize,
2256b591480bSJ.Bruce Fields 	},
2257b591480bSJ.Bruce Fields 	[OP_REMOVE] = {
2258eb69853dSChristoph Hellwig 		.op_func = nfsd4_remove,
2259c856694eSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
2260b001a1b6SBenny Halevy 		.op_name = "OP_REMOVE",
22611c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_remove_rsize,
2262b591480bSJ.Bruce Fields 	},
2263b591480bSJ.Bruce Fields 	[OP_RENAME] = {
2264eb69853dSChristoph Hellwig 		.op_func = nfsd4_rename,
2265c856694eSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
226658e7b33aSMi Jinlong 		.op_name = "OP_RENAME",
22671c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_rename_rsize,
2268b591480bSJ.Bruce Fields 	},
2269b591480bSJ.Bruce Fields 	[OP_RENEW] = {
2270eb69853dSChristoph Hellwig 		.op_func = nfsd4_renew,
227158e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
227258e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
2273b001a1b6SBenny Halevy 		.op_name = "OP_RENEW",
22741c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
227558e7b33aSMi Jinlong 
2276b591480bSJ.Bruce Fields 	},
2277b591480bSJ.Bruce Fields 	[OP_RESTOREFH] = {
2278eb69853dSChristoph Hellwig 		.op_func = nfsd4_restorefh,
227968d93184SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
228058e7b33aSMi Jinlong 				| OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING,
2281b001a1b6SBenny Halevy 		.op_name = "OP_RESTOREFH",
22821c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2283b591480bSJ.Bruce Fields 	},
2284b591480bSJ.Bruce Fields 	[OP_SAVEFH] = {
2285eb69853dSChristoph Hellwig 		.op_func = nfsd4_savefh,
228658e7b33aSMi Jinlong 		.op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
2287b001a1b6SBenny Halevy 		.op_name = "OP_SAVEFH",
22881c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2289b591480bSJ.Bruce Fields 	},
2290dcb488a3SAndy Adamson 	[OP_SECINFO] = {
2291eb69853dSChristoph Hellwig 		.op_func = nfsd4_secinfo,
229268d93184SJ. Bruce Fields 		.op_flags = OP_HANDLES_WRONGSEC,
2293b001a1b6SBenny Halevy 		.op_name = "OP_SECINFO",
22941c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_secinfo_rsize,
2295dcb488a3SAndy Adamson 	},
2296b591480bSJ.Bruce Fields 	[OP_SETATTR] = {
2297eb69853dSChristoph Hellwig 		.op_func = nfsd4_setattr,
2298b001a1b6SBenny Halevy 		.op_name = "OP_SETATTR",
2299c856694eSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
23001c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_setattr_rsize,
230157832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_setattrstateid,
2302b591480bSJ.Bruce Fields 	},
2303b591480bSJ.Bruce Fields 	[OP_SETCLIENTID] = {
2304eb69853dSChristoph Hellwig 		.op_func = nfsd4_setclientid,
230558e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
2306c856694eSJ. Bruce Fields 				| OP_MODIFIES_SOMETHING | OP_CACHEME,
2307b001a1b6SBenny Halevy 		.op_name = "OP_SETCLIENTID",
23081c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_setclientid_rsize,
2309b591480bSJ.Bruce Fields 	},
2310b591480bSJ.Bruce Fields 	[OP_SETCLIENTID_CONFIRM] = {
2311eb69853dSChristoph Hellwig 		.op_func = nfsd4_setclientid_confirm,
231258e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
2313c856694eSJ. Bruce Fields 				| OP_MODIFIES_SOMETHING | OP_CACHEME,
2314b001a1b6SBenny Halevy 		.op_name = "OP_SETCLIENTID_CONFIRM",
23151c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2316b591480bSJ.Bruce Fields 	},
2317b591480bSJ.Bruce Fields 	[OP_VERIFY] = {
2318eb69853dSChristoph Hellwig 		.op_func = nfsd4_verify,
2319b001a1b6SBenny Halevy 		.op_name = "OP_VERIFY",
23201c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2321b591480bSJ.Bruce Fields 	},
2322b591480bSJ.Bruce Fields 	[OP_WRITE] = {
2323eb69853dSChristoph Hellwig 		.op_func = nfsd4_write,
2324c856694eSJ. Bruce Fields 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
2325b001a1b6SBenny Halevy 		.op_name = "OP_WRITE",
23261c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_write_rsize,
232757832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_writestateid,
2328b591480bSJ.Bruce Fields 	},
2329b591480bSJ.Bruce Fields 	[OP_RELEASE_LOCKOWNER] = {
2330eb69853dSChristoph Hellwig 		.op_func = nfsd4_release_lockowner,
233158e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
233258e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
2333b001a1b6SBenny Halevy 		.op_name = "OP_RELEASE_LOCKOWNER",
23341c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2335b591480bSJ.Bruce Fields 	},
2336069b6ad4SAndy Adamson 
2337069b6ad4SAndy Adamson 	/* NFSv4.1 operations */
2338069b6ad4SAndy Adamson 	[OP_EXCHANGE_ID] = {
2339eb69853dSChristoph Hellwig 		.op_func = nfsd4_exchange_id,
234058e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
234158e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
2342069b6ad4SAndy Adamson 		.op_name = "OP_EXCHANGE_ID",
23431c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_exchange_id_rsize,
2344069b6ad4SAndy Adamson 	},
2345cb73a9f4SJ. Bruce Fields 	[OP_BACKCHANNEL_CTL] = {
2346eb69853dSChristoph Hellwig 		.op_func = nfsd4_backchannel_ctl,
2347cb73a9f4SJ. Bruce Fields 		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
2348cb73a9f4SJ. Bruce Fields 		.op_name = "OP_BACKCHANNEL_CTL",
23491c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2350cb73a9f4SJ. Bruce Fields 	},
23511d1bc8f2SJ. Bruce Fields 	[OP_BIND_CONN_TO_SESSION] = {
2352eb69853dSChristoph Hellwig 		.op_func = nfsd4_bind_conn_to_session,
235358e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
235458e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
23551d1bc8f2SJ. Bruce Fields 		.op_name = "OP_BIND_CONN_TO_SESSION",
23561c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_bind_conn_to_session_rsize,
23571d1bc8f2SJ. Bruce Fields 	},
2358069b6ad4SAndy Adamson 	[OP_CREATE_SESSION] = {
2359eb69853dSChristoph Hellwig 		.op_func = nfsd4_create_session,
236058e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
236158e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
2362069b6ad4SAndy Adamson 		.op_name = "OP_CREATE_SESSION",
23631c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_create_session_rsize,
2364069b6ad4SAndy Adamson 	},
2365069b6ad4SAndy Adamson 	[OP_DESTROY_SESSION] = {
2366eb69853dSChristoph Hellwig 		.op_func = nfsd4_destroy_session,
236758e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
236858e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
2369069b6ad4SAndy Adamson 		.op_name = "OP_DESTROY_SESSION",
23701c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2371069b6ad4SAndy Adamson 	},
2372069b6ad4SAndy Adamson 	[OP_SEQUENCE] = {
2373eb69853dSChristoph Hellwig 		.op_func = nfsd4_sequence,
2374f9bb94c4SAndy Adamson 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
2375069b6ad4SAndy Adamson 		.op_name = "OP_SEQUENCE",
23761c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_sequence_rsize,
2377069b6ad4SAndy Adamson 	},
2378094b5d74SBenny Halevy 	[OP_DESTROY_CLIENTID] = {
2379eb69853dSChristoph Hellwig 		.op_func = nfsd4_destroy_clientid,
238058e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
238158e7b33aSMi Jinlong 				| OP_MODIFIES_SOMETHING,
2382094b5d74SBenny Halevy 		.op_name = "OP_DESTROY_CLIENTID",
23831c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2384094b5d74SBenny Halevy 	},
23854dc6ec00SJ. Bruce Fields 	[OP_RECLAIM_COMPLETE] = {
2386eb69853dSChristoph Hellwig 		.op_func = nfsd4_reclaim_complete,
238758e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
23884dc6ec00SJ. Bruce Fields 		.op_name = "OP_RECLAIM_COMPLETE",
23891c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
23904dc6ec00SJ. Bruce Fields 	},
239104f4ad16SJ. Bruce Fields 	[OP_SECINFO_NO_NAME] = {
2392eb69853dSChristoph Hellwig 		.op_func = nfsd4_secinfo_no_name,
239368d93184SJ. Bruce Fields 		.op_flags = OP_HANDLES_WRONGSEC,
239404f4ad16SJ. Bruce Fields 		.op_name = "OP_SECINFO_NO_NAME",
23951c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_secinfo_rsize,
239604f4ad16SJ. Bruce Fields 	},
239717456804SBryan Schumaker 	[OP_TEST_STATEID] = {
2398eb69853dSChristoph Hellwig 		.op_func = nfsd4_test_stateid,
239917456804SBryan Schumaker 		.op_flags = ALLOWED_WITHOUT_FH,
240017456804SBryan Schumaker 		.op_name = "OP_TEST_STATEID",
24011c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_test_stateid_rsize,
240217456804SBryan Schumaker 	},
2403e1ca12dfSBryan Schumaker 	[OP_FREE_STATEID] = {
2404eb69853dSChristoph Hellwig 		.op_func = nfsd4_free_stateid,
240558e7b33aSMi Jinlong 		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
2406e1ca12dfSBryan Schumaker 		.op_name = "OP_FREE_STATEID",
240757832e7bSChristoph Hellwig 		.op_get_currentstateid = nfsd4_get_freestateid,
24081c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2409e1ca12dfSBryan Schumaker 	},
24109cf514ccSChristoph Hellwig #ifdef CONFIG_NFSD_PNFS
24119cf514ccSChristoph Hellwig 	[OP_GETDEVICEINFO] = {
2412eb69853dSChristoph Hellwig 		.op_func = nfsd4_getdeviceinfo,
24139cf514ccSChristoph Hellwig 		.op_flags = ALLOWED_WITHOUT_FH,
24149cf514ccSChristoph Hellwig 		.op_name = "OP_GETDEVICEINFO",
24151c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_getdeviceinfo_rsize,
24169cf514ccSChristoph Hellwig 	},
24179cf514ccSChristoph Hellwig 	[OP_LAYOUTGET] = {
2418eb69853dSChristoph Hellwig 		.op_func = nfsd4_layoutget,
24199cf514ccSChristoph Hellwig 		.op_flags = OP_MODIFIES_SOMETHING,
24209cf514ccSChristoph Hellwig 		.op_name = "OP_LAYOUTGET",
24211c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_layoutget_rsize,
24229cf514ccSChristoph Hellwig 	},
24239cf514ccSChristoph Hellwig 	[OP_LAYOUTCOMMIT] = {
2424eb69853dSChristoph Hellwig 		.op_func = nfsd4_layoutcommit,
24259cf514ccSChristoph Hellwig 		.op_flags = OP_MODIFIES_SOMETHING,
24269cf514ccSChristoph Hellwig 		.op_name = "OP_LAYOUTCOMMIT",
24271c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_layoutcommit_rsize,
24289cf514ccSChristoph Hellwig 	},
24299cf514ccSChristoph Hellwig 	[OP_LAYOUTRETURN] = {
2430eb69853dSChristoph Hellwig 		.op_func = nfsd4_layoutreturn,
24319cf514ccSChristoph Hellwig 		.op_flags = OP_MODIFIES_SOMETHING,
24329cf514ccSChristoph Hellwig 		.op_name = "OP_LAYOUTRETURN",
24331c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_layoutreturn_rsize,
24349cf514ccSChristoph Hellwig 	},
24359cf514ccSChristoph Hellwig #endif /* CONFIG_NFSD_PNFS */
243624bab491SAnna Schumaker 
243724bab491SAnna Schumaker 	/* NFSv4.2 operations */
243895d871f0SAnna Schumaker 	[OP_ALLOCATE] = {
2439eb69853dSChristoph Hellwig 		.op_func = nfsd4_allocate,
244095d871f0SAnna Schumaker 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
244195d871f0SAnna Schumaker 		.op_name = "OP_ALLOCATE",
24421c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
244395d871f0SAnna Schumaker 	},
2444b0cb9085SAnna Schumaker 	[OP_DEALLOCATE] = {
2445eb69853dSChristoph Hellwig 		.op_func = nfsd4_deallocate,
2446b0cb9085SAnna Schumaker 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
2447b0cb9085SAnna Schumaker 		.op_name = "OP_DEALLOCATE",
24481c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2449b0cb9085SAnna Schumaker 	},
2450ffa0160aSChristoph Hellwig 	[OP_CLONE] = {
2451eb69853dSChristoph Hellwig 		.op_func = nfsd4_clone,
2452ffa0160aSChristoph Hellwig 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
2453ffa0160aSChristoph Hellwig 		.op_name = "OP_CLONE",
24541c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_only_status_rsize,
2455ffa0160aSChristoph Hellwig 	},
245629ae7f9dSAnna Schumaker 	[OP_COPY] = {
2457eb69853dSChristoph Hellwig 		.op_func = nfsd4_copy,
245829ae7f9dSAnna Schumaker 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
245929ae7f9dSAnna Schumaker 		.op_name = "OP_COPY",
24601c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_copy_rsize,
246129ae7f9dSAnna Schumaker 	},
246224bab491SAnna Schumaker 	[OP_SEEK] = {
2463eb69853dSChristoph Hellwig 		.op_func = nfsd4_seek,
246424bab491SAnna Schumaker 		.op_name = "OP_SEEK",
24651c122638SChristoph Hellwig 		.op_rsize_bop = nfsd4_seek_rsize,
246624bab491SAnna Schumaker 	},
2467b591480bSJ.Bruce Fields };
2468b591480bSJ.Bruce Fields 
2469ed941643SAndrew Elble /**
2470ed941643SAndrew Elble  * nfsd4_spo_must_allow - Determine if the compound op contains an
2471ed941643SAndrew Elble  * operation that is allowed to be sent with machine credentials
2472ed941643SAndrew Elble  *
2473ed941643SAndrew Elble  * @rqstp: a pointer to the struct svc_rqst
2474ed941643SAndrew Elble  *
2475ed941643SAndrew Elble  * Checks to see if the compound contains a spo_must_allow op
2476ed941643SAndrew Elble  * and confirms that it was sent with the proper machine creds.
2477ed941643SAndrew Elble  */
2478ed941643SAndrew Elble 
2479ed941643SAndrew Elble bool nfsd4_spo_must_allow(struct svc_rqst *rqstp)
2480ed941643SAndrew Elble {
2481ed941643SAndrew Elble 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
2482ed941643SAndrew Elble 	struct nfsd4_compoundargs *argp = rqstp->rq_argp;
2483ed941643SAndrew Elble 	struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
2484ed941643SAndrew Elble 	struct nfsd4_compound_state *cstate = &resp->cstate;
2485ed941643SAndrew Elble 	struct nfs4_op_map *allow = &cstate->clp->cl_spo_must_allow;
2486ed941643SAndrew Elble 	u32 opiter;
2487ed941643SAndrew Elble 
2488ed941643SAndrew Elble 	if (!cstate->minorversion)
2489ed941643SAndrew Elble 		return false;
2490ed941643SAndrew Elble 
2491ed941643SAndrew Elble 	if (cstate->spo_must_allowed == true)
2492ed941643SAndrew Elble 		return true;
2493ed941643SAndrew Elble 
2494ed941643SAndrew Elble 	opiter = resp->opcnt;
2495ed941643SAndrew Elble 	while (opiter < argp->opcnt) {
2496ed941643SAndrew Elble 		this = &argp->ops[opiter++];
2497ed941643SAndrew Elble 		if (test_bit(this->opnum, allow->u.longs) &&
2498ed941643SAndrew Elble 			cstate->clp->cl_mach_cred &&
2499ed941643SAndrew Elble 			nfsd4_mach_creds_match(cstate->clp, rqstp)) {
2500ed941643SAndrew Elble 			cstate->spo_must_allowed = true;
2501ed941643SAndrew Elble 			return true;
2502ed941643SAndrew Elble 		}
2503ed941643SAndrew Elble 	}
2504ed941643SAndrew Elble 	cstate->spo_must_allowed = false;
2505ed941643SAndrew Elble 	return false;
2506ed941643SAndrew Elble }
2507ed941643SAndrew Elble 
25084f0cefbfSJ. Bruce Fields int nfsd4_max_reply(struct svc_rqst *rqstp, struct nfsd4_op *op)
25094f0cefbfSJ. Bruce Fields {
251005b7278dSOlga Kornievskaia 	if (op->opnum == OP_ILLEGAL || op->status == nfserr_notsupp)
25114f0cefbfSJ. Bruce Fields 		return op_encode_hdr_size * sizeof(__be32);
25122282cd2cSKinglong Mee 
25132282cd2cSKinglong Mee 	BUG_ON(OPDESC(op)->op_rsize_bop == NULL);
25142282cd2cSKinglong Mee 	return OPDESC(op)->op_rsize_bop(rqstp, op);
25154f0cefbfSJ. Bruce Fields }
25164f0cefbfSJ. Bruce Fields 
251707d1f802SJ. Bruce Fields void warn_on_nonidempotent_op(struct nfsd4_op *op)
251807d1f802SJ. Bruce Fields {
251907d1f802SJ. Bruce Fields 	if (OPDESC(op)->op_flags & OP_MODIFIES_SOMETHING) {
252007d1f802SJ. Bruce Fields 		pr_err("unable to encode reply to nonidempotent op %d (%s)\n",
252107d1f802SJ. Bruce Fields 			op->opnum, nfsd4_op_name(op->opnum));
252207d1f802SJ. Bruce Fields 		WARN_ON_ONCE(1);
252307d1f802SJ. Bruce Fields 	}
252407d1f802SJ. Bruce Fields }
252507d1f802SJ. Bruce Fields 
2526f1c7f79bSAdrian Bunk static const char *nfsd4_op_name(unsigned opnum)
2527b001a1b6SBenny Halevy {
2528b001a1b6SBenny Halevy 	if (opnum < ARRAY_SIZE(nfsd4_ops))
2529b001a1b6SBenny Halevy 		return nfsd4_ops[opnum].op_name;
2530b001a1b6SBenny Halevy 	return "unknown_operation";
2531b001a1b6SBenny Halevy }
2532b001a1b6SBenny Halevy 
25331da177e4SLinus Torvalds #define nfsd4_voidres			nfsd4_voidargs
25341da177e4SLinus Torvalds struct nfsd4_voidargs { int dummy; };
25351da177e4SLinus Torvalds 
25361da177e4SLinus Torvalds static struct svc_procedure		nfsd_procedures4[2] = {
25370a93a47fSYu Zhiguo 	[NFSPROC4_NULL] = {
2538f7235b6bSChristoph Hellwig 		.pc_func = nfsd4_proc_null,
253963f8de37SChristoph Hellwig 		.pc_encode = nfs4svc_encode_voidres,
25400a93a47fSYu Zhiguo 		.pc_argsize = sizeof(struct nfsd4_voidargs),
25410a93a47fSYu Zhiguo 		.pc_ressize = sizeof(struct nfsd4_voidres),
25420a93a47fSYu Zhiguo 		.pc_cachetype = RC_NOCACHE,
25430a93a47fSYu Zhiguo 		.pc_xdrressize = 1,
25440a93a47fSYu Zhiguo 	},
25450a93a47fSYu Zhiguo 	[NFSPROC4_COMPOUND] = {
2546f7235b6bSChristoph Hellwig 		.pc_func = nfsd4_proc_compound,
2547026fec7eSChristoph Hellwig 		.pc_decode = nfs4svc_decode_compoundargs,
254863f8de37SChristoph Hellwig 		.pc_encode = nfs4svc_encode_compoundres,
25490a93a47fSYu Zhiguo 		.pc_argsize = sizeof(struct nfsd4_compoundargs),
25500a93a47fSYu Zhiguo 		.pc_ressize = sizeof(struct nfsd4_compoundres),
25513e98abffSJ. Bruce Fields 		.pc_release = nfsd4_release_compoundargs,
25520a93a47fSYu Zhiguo 		.pc_cachetype = RC_NOCACHE,
25530a93a47fSYu Zhiguo 		.pc_xdrressize = NFSD_BUFSIZE/4,
25540a93a47fSYu Zhiguo 	},
25551da177e4SLinus Torvalds };
25561da177e4SLinus Torvalds 
25571da177e4SLinus Torvalds struct svc_version	nfsd_version4 = {
25581da177e4SLinus Torvalds 	.vs_vers		= 4,
25591da177e4SLinus Torvalds 	.vs_nproc		= 2,
25601da177e4SLinus Torvalds 	.vs_proc		= nfsd_procedures4,
25611da177e4SLinus Torvalds 	.vs_dispatch		= nfsd_dispatch,
25621da177e4SLinus Torvalds 	.vs_xdrsize		= NFS4_SVC_XDRSIZE,
256305a45a2dSJeff Layton 	.vs_rpcb_optnl		= true,
25645283b03eSJeff Layton 	.vs_need_cong_ctrl	= true,
25651da177e4SLinus Torvalds };
25661da177e4SLinus Torvalds 
25671da177e4SLinus Torvalds /*
25681da177e4SLinus Torvalds  * Local variables:
25691da177e4SLinus Torvalds  *  c-basic-offset: 8
25701da177e4SLinus Torvalds  * End:
25711da177e4SLinus Torvalds  */
2572