cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

nfs4proc.c (99772B)


      1/*
      2 *  Server-side procedures for NFSv4.
      3 *
      4 *  Copyright (c) 2002 The Regents of the University of Michigan.
      5 *  All rights reserved.
      6 *
      7 *  Kendrick Smith <kmsmith@umich.edu>
      8 *  Andy Adamson   <andros@umich.edu>
      9 *
     10 *  Redistribution and use in source and binary forms, with or without
     11 *  modification, are permitted provided that the following conditions
     12 *  are met:
     13 *
     14 *  1. Redistributions of source code must retain the above copyright
     15 *     notice, this list of conditions and the following disclaimer.
     16 *  2. Redistributions in binary form must reproduce the above copyright
     17 *     notice, this list of conditions and the following disclaimer in the
     18 *     documentation and/or other materials provided with the distribution.
     19 *  3. Neither the name of the University nor the names of its
     20 *     contributors may be used to endorse or promote products derived
     21 *     from this software without specific prior written permission.
     22 *
     23 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
     24 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     25 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     26 *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27 *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     30 *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     31 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     32 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     33 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34 */
     35#include <linux/fs_struct.h>
     36#include <linux/file.h>
     37#include <linux/falloc.h>
     38#include <linux/slab.h>
     39#include <linux/kthread.h>
     40#include <linux/namei.h>
     41
     42#include <linux/sunrpc/addr.h>
     43#include <linux/nfs_ssc.h>
     44
     45#include "idmap.h"
     46#include "cache.h"
     47#include "xdr4.h"
     48#include "vfs.h"
     49#include "current_stateid.h"
     50#include "netns.h"
     51#include "acl.h"
     52#include "pnfs.h"
     53#include "trace.h"
     54
     55static bool inter_copy_offload_enable;
     56module_param(inter_copy_offload_enable, bool, 0644);
     57MODULE_PARM_DESC(inter_copy_offload_enable,
     58		 "Enable inter server to server copy offload. Default: false");
     59
     60#ifdef CONFIG_NFSD_V4_2_INTER_SSC
     61static int nfsd4_ssc_umount_timeout = 900000;		/* default to 15 mins */
     62module_param(nfsd4_ssc_umount_timeout, int, 0644);
     63MODULE_PARM_DESC(nfsd4_ssc_umount_timeout,
     64		"idle msecs before unmount export from source server");
     65#endif
     66
     67#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
     68#include <linux/security.h>
     69
     70static inline void
     71nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
     72{
     73	struct inode *inode = d_inode(resfh->fh_dentry);
     74	int status;
     75
     76	inode_lock(inode);
     77	status = security_inode_setsecctx(resfh->fh_dentry,
     78		label->data, label->len);
     79	inode_unlock(inode);
     80
     81	if (status)
     82		/*
     83		 * XXX: We should really fail the whole open, but we may
     84		 * already have created a new file, so it may be too
     85		 * late.  For now this seems the least of evils:
     86		 */
     87		bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
     88
     89	return;
     90}
     91#else
     92static inline void
     93nfsd4_security_inode_setsecctx(struct svc_fh *resfh, struct xdr_netobj *label, u32 *bmval)
     94{ }
     95#endif
     96
     97#define NFSDDBG_FACILITY		NFSDDBG_PROC
     98
     99static u32 nfsd_attrmask[] = {
    100	NFSD_WRITEABLE_ATTRS_WORD0,
    101	NFSD_WRITEABLE_ATTRS_WORD1,
    102	NFSD_WRITEABLE_ATTRS_WORD2
    103};
    104
    105static u32 nfsd41_ex_attrmask[] = {
    106	NFSD_SUPPATTR_EXCLCREAT_WORD0,
    107	NFSD_SUPPATTR_EXCLCREAT_WORD1,
    108	NFSD_SUPPATTR_EXCLCREAT_WORD2
    109};
    110
    111static __be32
    112check_attr_support(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
    113		   u32 *bmval, u32 *writable)
    114{
    115	struct dentry *dentry = cstate->current_fh.fh_dentry;
    116	struct svc_export *exp = cstate->current_fh.fh_export;
    117
    118	if (!nfsd_attrs_supported(cstate->minorversion, bmval))
    119		return nfserr_attrnotsupp;
    120	if ((bmval[0] & FATTR4_WORD0_ACL) && !IS_POSIXACL(d_inode(dentry)))
    121		return nfserr_attrnotsupp;
    122	if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) &&
    123			!(exp->ex_flags & NFSEXP_SECURITY_LABEL))
    124		return nfserr_attrnotsupp;
    125	if (writable && !bmval_is_subset(bmval, writable))
    126		return nfserr_inval;
    127	if (writable && (bmval[2] & FATTR4_WORD2_MODE_UMASK) &&
    128			(bmval[1] & FATTR4_WORD1_MODE))
    129		return nfserr_inval;
    130	return nfs_ok;
    131}
    132
    133static __be32
    134nfsd4_check_open_attributes(struct svc_rqst *rqstp,
    135	struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
    136{
    137	__be32 status = nfs_ok;
    138
    139	if (open->op_create == NFS4_OPEN_CREATE) {
    140		if (open->op_createmode == NFS4_CREATE_UNCHECKED
    141		    || open->op_createmode == NFS4_CREATE_GUARDED)
    142			status = check_attr_support(rqstp, cstate,
    143					open->op_bmval, nfsd_attrmask);
    144		else if (open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1)
    145			status = check_attr_support(rqstp, cstate,
    146					open->op_bmval, nfsd41_ex_attrmask);
    147	}
    148
    149	return status;
    150}
    151
    152static int
    153is_create_with_attrs(struct nfsd4_open *open)
    154{
    155	return open->op_create == NFS4_OPEN_CREATE
    156		&& (open->op_createmode == NFS4_CREATE_UNCHECKED
    157		    || open->op_createmode == NFS4_CREATE_GUARDED
    158		    || open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1);
    159}
    160
    161/*
    162 * if error occurs when setting the acl, just clear the acl bit
    163 * in the returned attr bitmap.
    164 */
    165static void
    166do_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
    167		struct nfs4_acl *acl, u32 *bmval)
    168{
    169	__be32 status;
    170
    171	status = nfsd4_set_nfs4_acl(rqstp, fhp, acl);
    172	if (status)
    173		/*
    174		 * We should probably fail the whole open at this point,
    175		 * but we've already created the file, so it's too late;
    176		 * So this seems the least of evils:
    177		 */
    178		bmval[0] &= ~FATTR4_WORD0_ACL;
    179}
    180
    181static inline void
    182fh_dup2(struct svc_fh *dst, struct svc_fh *src)
    183{
    184	fh_put(dst);
    185	dget(src->fh_dentry);
    186	if (src->fh_export)
    187		exp_get(src->fh_export);
    188	*dst = *src;
    189}
    190
    191static __be32
    192do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, int accmode)
    193{
    194	__be32 status;
    195
    196	if (open->op_truncate &&
    197		!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
    198		return nfserr_inval;
    199
    200	accmode |= NFSD_MAY_READ_IF_EXEC;
    201
    202	if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
    203		accmode |= NFSD_MAY_READ;
    204	if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
    205		accmode |= (NFSD_MAY_WRITE | NFSD_MAY_TRUNC);
    206	if (open->op_share_deny & NFS4_SHARE_DENY_READ)
    207		accmode |= NFSD_MAY_WRITE;
    208
    209	status = fh_verify(rqstp, current_fh, S_IFREG, accmode);
    210
    211	return status;
    212}
    213
    214static __be32 nfsd_check_obj_isreg(struct svc_fh *fh)
    215{
    216	umode_t mode = d_inode(fh->fh_dentry)->i_mode;
    217
    218	if (S_ISREG(mode))
    219		return nfs_ok;
    220	if (S_ISDIR(mode))
    221		return nfserr_isdir;
    222	/*
    223	 * Using err_symlink as our catch-all case may look odd; but
    224	 * there's no other obvious error for this case in 4.0, and we
    225	 * happen to know that it will cause the linux v4 client to do
    226	 * the right thing on attempts to open something other than a
    227	 * regular file.
    228	 */
    229	return nfserr_symlink;
    230}
    231
    232static void nfsd4_set_open_owner_reply_cache(struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh *resfh)
    233{
    234	if (nfsd4_has_session(cstate))
    235		return;
    236	fh_copy_shallow(&open->op_openowner->oo_owner.so_replay.rp_openfh,
    237			&resfh->fh_handle);
    238}
    239
    240static inline bool nfsd4_create_is_exclusive(int createmode)
    241{
    242	return createmode == NFS4_CREATE_EXCLUSIVE ||
    243		createmode == NFS4_CREATE_EXCLUSIVE4_1;
    244}
    245
    246static __be32
    247nfsd4_vfs_create(struct svc_fh *fhp, struct dentry *child,
    248		 struct nfsd4_open *open)
    249{
    250	struct file *filp;
    251	struct path path;
    252	int oflags;
    253
    254	oflags = O_CREAT | O_LARGEFILE;
    255	switch (open->op_share_access & NFS4_SHARE_ACCESS_BOTH) {
    256	case NFS4_SHARE_ACCESS_WRITE:
    257		oflags |= O_WRONLY;
    258		break;
    259	case NFS4_SHARE_ACCESS_BOTH:
    260		oflags |= O_RDWR;
    261		break;
    262	default:
    263		oflags |= O_RDONLY;
    264	}
    265
    266	path.mnt = fhp->fh_export->ex_path.mnt;
    267	path.dentry = child;
    268	filp = dentry_create(&path, oflags, open->op_iattr.ia_mode,
    269			     current_cred());
    270	if (IS_ERR(filp))
    271		return nfserrno(PTR_ERR(filp));
    272
    273	open->op_filp = filp;
    274	return nfs_ok;
    275}
    276
    277/*
    278 * Implement NFSv4's unchecked, guarded, and exclusive create
    279 * semantics for regular files. Open state for this new file is
    280 * subsequently fabricated in nfsd4_process_open2().
    281 *
    282 * Upon return, caller must release @fhp and @resfhp.
    283 */
    284static __be32
    285nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
    286		  struct svc_fh *resfhp, struct nfsd4_open *open)
    287{
    288	struct iattr *iap = &open->op_iattr;
    289	struct dentry *parent, *child;
    290	__u32 v_mtime, v_atime;
    291	struct inode *inode;
    292	__be32 status;
    293	int host_err;
    294
    295	if (isdotent(open->op_fname, open->op_fnamelen))
    296		return nfserr_exist;
    297	if (!(iap->ia_valid & ATTR_MODE))
    298		iap->ia_mode = 0;
    299
    300	status = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
    301	if (status != nfs_ok)
    302		return status;
    303	parent = fhp->fh_dentry;
    304	inode = d_inode(parent);
    305
    306	host_err = fh_want_write(fhp);
    307	if (host_err)
    308		return nfserrno(host_err);
    309
    310	fh_lock_nested(fhp, I_MUTEX_PARENT);
    311
    312	child = lookup_one_len(open->op_fname, parent, open->op_fnamelen);
    313	if (IS_ERR(child)) {
    314		status = nfserrno(PTR_ERR(child));
    315		goto out;
    316	}
    317
    318	if (d_really_is_negative(child)) {
    319		status = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
    320		if (status != nfs_ok)
    321			goto out;
    322	}
    323
    324	status = fh_compose(resfhp, fhp->fh_export, child, fhp);
    325	if (status != nfs_ok)
    326		goto out;
    327
    328	v_mtime = 0;
    329	v_atime = 0;
    330	if (nfsd4_create_is_exclusive(open->op_createmode)) {
    331		u32 *verifier = (u32 *)open->op_verf.data;
    332
    333		/*
    334		 * Solaris 7 gets confused (bugid 4218508) if these have
    335		 * the high bit set, as do xfs filesystems without the
    336		 * "bigtime" feature. So just clear the high bits. If this
    337		 * is ever changed to use different attrs for storing the
    338		 * verifier, then do_open_lookup() will also need to be
    339		 * fixed accordingly.
    340		 */
    341		v_mtime = verifier[0] & 0x7fffffff;
    342		v_atime = verifier[1] & 0x7fffffff;
    343	}
    344
    345	if (d_really_is_positive(child)) {
    346		status = nfs_ok;
    347
    348		switch (open->op_createmode) {
    349		case NFS4_CREATE_UNCHECKED:
    350			if (!d_is_reg(child))
    351				break;
    352
    353			/*
    354			 * In NFSv4, we don't want to truncate the file
    355			 * now. This would be wrong if the OPEN fails for
    356			 * some other reason. Furthermore, if the size is
    357			 * nonzero, we should ignore it according to spec!
    358			 */
    359			open->op_truncate = (iap->ia_valid & ATTR_SIZE) &&
    360						!iap->ia_size;
    361			break;
    362		case NFS4_CREATE_GUARDED:
    363			status = nfserr_exist;
    364			break;
    365		case NFS4_CREATE_EXCLUSIVE:
    366			if (d_inode(child)->i_mtime.tv_sec == v_mtime &&
    367			    d_inode(child)->i_atime.tv_sec == v_atime &&
    368			    d_inode(child)->i_size == 0) {
    369				open->op_created = true;
    370				break;		/* subtle */
    371			}
    372			status = nfserr_exist;
    373			break;
    374		case NFS4_CREATE_EXCLUSIVE4_1:
    375			if (d_inode(child)->i_mtime.tv_sec == v_mtime &&
    376			    d_inode(child)->i_atime.tv_sec == v_atime &&
    377			    d_inode(child)->i_size == 0) {
    378				open->op_created = true;
    379				goto set_attr;	/* subtle */
    380			}
    381			status = nfserr_exist;
    382		}
    383		goto out;
    384	}
    385
    386	if (!IS_POSIXACL(inode))
    387		iap->ia_mode &= ~current_umask();
    388
    389	status = nfsd4_vfs_create(fhp, child, open);
    390	if (status != nfs_ok)
    391		goto out;
    392	open->op_created = true;
    393
    394	/* A newly created file already has a file size of zero. */
    395	if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
    396		iap->ia_valid &= ~ATTR_SIZE;
    397	if (nfsd4_create_is_exclusive(open->op_createmode)) {
    398		iap->ia_valid = ATTR_MTIME | ATTR_ATIME |
    399				ATTR_MTIME_SET|ATTR_ATIME_SET;
    400		iap->ia_mtime.tv_sec = v_mtime;
    401		iap->ia_atime.tv_sec = v_atime;
    402		iap->ia_mtime.tv_nsec = 0;
    403		iap->ia_atime.tv_nsec = 0;
    404	}
    405
    406set_attr:
    407	status = nfsd_create_setattr(rqstp, fhp, resfhp, iap);
    408
    409out:
    410	fh_unlock(fhp);
    411	if (child && !IS_ERR(child))
    412		dput(child);
    413	fh_drop_write(fhp);
    414	return status;
    415}
    416
    417static __be32
    418do_open_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh **resfh)
    419{
    420	struct svc_fh *current_fh = &cstate->current_fh;
    421	int accmode;
    422	__be32 status;
    423
    424	*resfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
    425	if (!*resfh)
    426		return nfserr_jukebox;
    427	fh_init(*resfh, NFS4_FHSIZE);
    428	open->op_truncate = false;
    429
    430	if (open->op_create) {
    431		/* FIXME: check session persistence and pnfs flags.
    432		 * The nfsv4.1 spec requires the following semantics:
    433		 *
    434		 * Persistent   | pNFS   | Server REQUIRED | Client Allowed
    435		 * Reply Cache  | server |                 |
    436		 * -------------+--------+-----------------+--------------------
    437		 * no           | no     | EXCLUSIVE4_1    | EXCLUSIVE4_1
    438		 *              |        |                 | (SHOULD)
    439		 *              |        | and EXCLUSIVE4  | or EXCLUSIVE4
    440		 *              |        |                 | (SHOULD NOT)
    441		 * no           | yes    | EXCLUSIVE4_1    | EXCLUSIVE4_1
    442		 * yes          | no     | GUARDED4        | GUARDED4
    443		 * yes          | yes    | GUARDED4        | GUARDED4
    444		 */
    445
    446		current->fs->umask = open->op_umask;
    447		status = nfsd4_create_file(rqstp, current_fh, *resfh, open);
    448		current->fs->umask = 0;
    449
    450		if (!status && open->op_label.len)
    451			nfsd4_security_inode_setsecctx(*resfh, &open->op_label, open->op_bmval);
    452
    453		/*
    454		 * Following rfc 3530 14.2.16, and rfc 5661 18.16.4
    455		 * use the returned bitmask to indicate which attributes
    456		 * we used to store the verifier:
    457		 */
    458		if (nfsd4_create_is_exclusive(open->op_createmode) && status == 0)
    459			open->op_bmval[1] |= (FATTR4_WORD1_TIME_ACCESS |
    460						FATTR4_WORD1_TIME_MODIFY);
    461	} else
    462		/*
    463		 * Note this may exit with the parent still locked.
    464		 * We will hold the lock until nfsd4_open's final
    465		 * lookup, to prevent renames or unlinks until we've had
    466		 * a chance to an acquire a delegation if appropriate.
    467		 */
    468		status = nfsd_lookup(rqstp, current_fh,
    469				     open->op_fname, open->op_fnamelen, *resfh);
    470	if (status)
    471		goto out;
    472	status = nfsd_check_obj_isreg(*resfh);
    473	if (status)
    474		goto out;
    475
    476	if (is_create_with_attrs(open) && open->op_acl != NULL)
    477		do_set_nfs4_acl(rqstp, *resfh, open->op_acl, open->op_bmval);
    478
    479	nfsd4_set_open_owner_reply_cache(cstate, open, *resfh);
    480	accmode = NFSD_MAY_NOP;
    481	if (open->op_created ||
    482			open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
    483		accmode |= NFSD_MAY_OWNER_OVERRIDE;
    484	status = do_open_permission(rqstp, *resfh, open, accmode);
    485	set_change_info(&open->op_cinfo, current_fh);
    486out:
    487	return status;
    488}
    489
    490static __be32
    491do_open_fhandle(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
    492{
    493	struct svc_fh *current_fh = &cstate->current_fh;
    494	__be32 status;
    495	int accmode = 0;
    496
    497	/* We don't know the target directory, and therefore can not
    498	* set the change info
    499	*/
    500
    501	memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
    502
    503	nfsd4_set_open_owner_reply_cache(cstate, open, current_fh);
    504
    505	open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
    506		(open->op_iattr.ia_size == 0);
    507	/*
    508	 * In the delegation case, the client is telling us about an
    509	 * open that it *already* performed locally, some time ago.  We
    510	 * should let it succeed now if possible.
    511	 *
    512	 * In the case of a CLAIM_FH open, on the other hand, the client
    513	 * may be counting on us to enforce permissions (the Linux 4.1
    514	 * client uses this for normal opens, for example).
    515	 */
    516	if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH)
    517		accmode = NFSD_MAY_OWNER_OVERRIDE;
    518
    519	status = do_open_permission(rqstp, current_fh, open, accmode);
    520
    521	return status;
    522}
    523
    524static void
    525copy_clientid(clientid_t *clid, struct nfsd4_session *session)
    526{
    527	struct nfsd4_sessionid *sid =
    528			(struct nfsd4_sessionid *)session->se_sessionid.data;
    529
    530	clid->cl_boot = sid->clientid.cl_boot;
    531	clid->cl_id = sid->clientid.cl_id;
    532}
    533
    534static __be32
    535nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
    536	   union nfsd4_op_u *u)
    537{
    538	struct nfsd4_open *open = &u->open;
    539	__be32 status;
    540	struct svc_fh *resfh = NULL;
    541	struct net *net = SVC_NET(rqstp);
    542	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
    543	bool reclaim = false;
    544
    545	dprintk("NFSD: nfsd4_open filename %.*s op_openowner %p\n",
    546		(int)open->op_fnamelen, open->op_fname,
    547		open->op_openowner);
    548
    549	open->op_filp = NULL;
    550
    551	/* This check required by spec. */
    552	if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
    553		return nfserr_inval;
    554
    555	open->op_created = false;
    556	/*
    557	 * RFC5661 18.51.3
    558	 * Before RECLAIM_COMPLETE done, server should deny new lock
    559	 */
    560	if (nfsd4_has_session(cstate) &&
    561	    !test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags) &&
    562	    open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
    563		return nfserr_grace;
    564
    565	if (nfsd4_has_session(cstate))
    566		copy_clientid(&open->op_clientid, cstate->session);
    567
    568	/* check seqid for replay. set nfs4_owner */
    569	status = nfsd4_process_open1(cstate, open, nn);
    570	if (status == nfserr_replay_me) {
    571		struct nfs4_replay *rp = &open->op_openowner->oo_owner.so_replay;
    572		fh_put(&cstate->current_fh);
    573		fh_copy_shallow(&cstate->current_fh.fh_handle,
    574				&rp->rp_openfh);
    575		status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
    576		if (status)
    577			dprintk("nfsd4_open: replay failed"
    578				" restoring previous filehandle\n");
    579		else
    580			status = nfserr_replay_me;
    581	}
    582	if (status)
    583		goto out;
    584	if (open->op_xdr_error) {
    585		status = open->op_xdr_error;
    586		goto out;
    587	}
    588
    589	status = nfsd4_check_open_attributes(rqstp, cstate, open);
    590	if (status)
    591		goto out;
    592
    593	/* Openowner is now set, so sequence id will get bumped.  Now we need
    594	 * these checks before we do any creates: */
    595	status = nfserr_grace;
    596	if (opens_in_grace(net) && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
    597		goto out;
    598	status = nfserr_no_grace;
    599	if (!opens_in_grace(net) && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
    600		goto out;
    601
    602	switch (open->op_claim_type) {
    603	case NFS4_OPEN_CLAIM_DELEGATE_CUR:
    604	case NFS4_OPEN_CLAIM_NULL:
    605		status = do_open_lookup(rqstp, cstate, open, &resfh);
    606		if (status)
    607			goto out;
    608		break;
    609	case NFS4_OPEN_CLAIM_PREVIOUS:
    610		status = nfs4_check_open_reclaim(cstate->clp);
    611		if (status)
    612			goto out;
    613		open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
    614		reclaim = true;
    615		fallthrough;
    616	case NFS4_OPEN_CLAIM_FH:
    617	case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
    618		status = do_open_fhandle(rqstp, cstate, open);
    619		if (status)
    620			goto out;
    621		resfh = &cstate->current_fh;
    622		break;
    623	case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
    624	case NFS4_OPEN_CLAIM_DELEGATE_PREV:
    625		status = nfserr_notsupp;
    626		goto out;
    627	default:
    628		status = nfserr_inval;
    629		goto out;
    630	}
    631
    632	status = nfsd4_process_open2(rqstp, resfh, open);
    633	WARN(status && open->op_created,
    634	     "nfsd4_process_open2 failed to open newly-created file! status=%u\n",
    635	     be32_to_cpu(status));
    636	if (reclaim && !status)
    637		nn->somebody_reclaimed = true;
    638out:
    639	if (open->op_filp) {
    640		fput(open->op_filp);
    641		open->op_filp = NULL;
    642	}
    643	if (resfh && resfh != &cstate->current_fh) {
    644		fh_dup2(&cstate->current_fh, resfh);
    645		fh_put(resfh);
    646		kfree(resfh);
    647	}
    648	nfsd4_cleanup_open_state(cstate, open);
    649	nfsd4_bump_seqid(cstate, status);
    650	return status;
    651}
    652
    653/*
    654 * OPEN is the only seqid-mutating operation whose decoding can fail
    655 * with a seqid-mutating error (specifically, decoding of user names in
    656 * the attributes).  Therefore we have to do some processing to look up
    657 * the stateowner so that we can bump the seqid.
    658 */
    659static __be32 nfsd4_open_omfg(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_op *op)
    660{
    661	struct nfsd4_open *open = &op->u.open;
    662
    663	if (!seqid_mutating_err(ntohl(op->status)))
    664		return op->status;
    665	if (nfsd4_has_session(cstate))
    666		return op->status;
    667	open->op_xdr_error = op->status;
    668	return nfsd4_open(rqstp, cstate, &op->u);
    669}
    670
    671/*
    672 * filehandle-manipulating ops.
    673 */
    674static __be32
    675nfsd4_getfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
    676	    union nfsd4_op_u *u)
    677{
    678	u->getfh = &cstate->current_fh;
    679	return nfs_ok;
    680}
    681
    682static __be32
    683nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
    684	    union nfsd4_op_u *u)
    685{
    686	struct nfsd4_putfh *putfh = &u->putfh;
    687	__be32 ret;
    688
    689	fh_put(&cstate->current_fh);
    690	cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
    691	memcpy(&cstate->current_fh.fh_handle.fh_raw, putfh->pf_fhval,
    692	       putfh->pf_fhlen);
    693	ret = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_BYPASS_GSS);
    694#ifdef CONFIG_NFSD_V4_2_INTER_SSC
    695	if (ret == nfserr_stale && putfh->no_verify) {
    696		SET_FH_FLAG(&cstate->current_fh, NFSD4_FH_FOREIGN);
    697		ret = 0;
    698	}
    699#endif
    700	return ret;
    701}
    702
    703static __be32
    704nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
    705		union nfsd4_op_u *u)
    706{
    707	__be32 status;
    708
    709	fh_put(&cstate->current_fh);
    710	status = exp_pseudoroot(rqstp, &cstate->current_fh);
    711	return status;
    712}
    713
    714static __be32
    715nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
    716		union nfsd4_op_u *u)
    717{
    718	if (!cstate->save_fh.fh_dentry)
    719		return nfserr_restorefh;
    720
    721	fh_dup2(&cstate->current_fh, &cstate->save_fh);
    722	if (HAS_CSTATE_FLAG(cstate, SAVED_STATE_ID_FLAG)) {
    723		memcpy(&cstate->current_stateid, &cstate->save_stateid, sizeof(stateid_t));
    724		SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
    725	}
    726	return nfs_ok;
    727}
    728
    729static __be32
    730nfsd4_savefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
    731	     union nfsd4_op_u *u)
    732{
    733	fh_dup2(&cstate->save_fh, &cstate->current_fh);
    734	if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG)) {
    735		memcpy(&cstate->save_stateid, &cstate->current_stateid, sizeof(stateid_t));
    736		SET_CSTATE_FLAG(cstate, SAVED_STATE_ID_FLAG);
    737	}
    738	return nfs_ok;
    739}
    740
    741/*
    742 * misc nfsv4 ops
    743 */
    744static __be32
    745nfsd4_access(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
    746	     union nfsd4_op_u *u)
    747{
    748	struct nfsd4_access *access = &u->access;
    749	u32 access_full;
    750
    751	access_full = NFS3_ACCESS_FULL;
    752	if (cstate->minorversion >= 2)
    753		access_full |= NFS4_ACCESS_XALIST | NFS4_ACCESS_XAREAD |
    754			       NFS4_ACCESS_XAWRITE;
    755
    756	if (access->ac_req_access & ~access_full)
    757		return nfserr_inval;
    758
    759	access->ac_resp_access = access->ac_req_access;
    760	return nfsd_access(rqstp, &cstate->current_fh, &access->ac_resp_access,
    761			   &access->ac_supported);
    762}
    763
    764static void gen_boot_verifier(nfs4_verifier *verifier, struct net *net)
    765{
    766	__be32 *verf = (__be32 *)verifier->data;
    767
    768	BUILD_BUG_ON(2*sizeof(*verf) != sizeof(verifier->data));
    769
    770	nfsd_copy_write_verifier(verf, net_generic(net, nfsd_net_id));
    771}
    772
    773static __be32
    774nfsd4_commit(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
    775	     union nfsd4_op_u *u)
    776{
    777	struct nfsd4_commit *commit = &u->commit;
    778
    779	return nfsd_commit(rqstp, &cstate->current_fh, commit->co_offset,
    780			     commit->co_count,
    781			     (__be32 *)commit->co_verf.data);
    782}
    783
    784static __be32
    785nfsd4_create(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
    786	     union nfsd4_op_u *u)
    787{
    788	struct nfsd4_create *create = &u->create;
    789	struct svc_fh resfh;
    790	__be32 status;
    791	dev_t rdev;
    792
    793	fh_init(&resfh, NFS4_FHSIZE);
    794
    795	status = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_NOP);
    796	if (status)
    797		return status;
    798
    799	status = check_attr_support(rqstp, cstate, create->cr_bmval,
    800				    nfsd_attrmask);
    801	if (status)
    802		return status;
    803
    804	current->fs->umask = create->cr_umask;
    805	switch (create->cr_type) {
    806	case NF4LNK:
    807		status = nfsd_symlink(rqstp, &cstate->current_fh,
    808				      create->cr_name, create->cr_namelen,
    809				      create->cr_data, &resfh);
    810		break;
    811
    812	case NF4BLK:
    813		status = nfserr_inval;
    814		rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
    815		if (MAJOR(rdev) != create->cr_specdata1 ||
    816		    MINOR(rdev) != create->cr_specdata2)
    817			goto out_umask;
    818		status = nfsd_create(rqstp, &cstate->current_fh,
    819				     create->cr_name, create->cr_namelen,
    820				     &create->cr_iattr, S_IFBLK, rdev, &resfh);
    821		break;
    822
    823	case NF4CHR:
    824		status = nfserr_inval;
    825		rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
    826		if (MAJOR(rdev) != create->cr_specdata1 ||
    827		    MINOR(rdev) != create->cr_specdata2)
    828			goto out_umask;
    829		status = nfsd_create(rqstp, &cstate->current_fh,
    830				     create->cr_name, create->cr_namelen,
    831				     &create->cr_iattr,S_IFCHR, rdev, &resfh);
    832		break;
    833
    834	case NF4SOCK:
    835		status = nfsd_create(rqstp, &cstate->current_fh,
    836				     create->cr_name, create->cr_namelen,
    837				     &create->cr_iattr, S_IFSOCK, 0, &resfh);
    838		break;
    839
    840	case NF4FIFO:
    841		status = nfsd_create(rqstp, &cstate->current_fh,
    842				     create->cr_name, create->cr_namelen,
    843				     &create->cr_iattr, S_IFIFO, 0, &resfh);
    844		break;
    845
    846	case NF4DIR:
    847		create->cr_iattr.ia_valid &= ~ATTR_SIZE;
    848		status = nfsd_create(rqstp, &cstate->current_fh,
    849				     create->cr_name, create->cr_namelen,
    850				     &create->cr_iattr, S_IFDIR, 0, &resfh);
    851		break;
    852
    853	default:
    854		status = nfserr_badtype;
    855	}
    856
    857	if (status)
    858		goto out;
    859
    860	if (create->cr_label.len)
    861		nfsd4_security_inode_setsecctx(&resfh, &create->cr_label, create->cr_bmval);
    862
    863	if (create->cr_acl != NULL)
    864		do_set_nfs4_acl(rqstp, &resfh, create->cr_acl,
    865				create->cr_bmval);
    866
    867	fh_unlock(&cstate->current_fh);
    868	set_change_info(&create->cr_cinfo, &cstate->current_fh);
    869	fh_dup2(&cstate->current_fh, &resfh);
    870out:
    871	fh_put(&resfh);
    872out_umask:
    873	current->fs->umask = 0;
    874	return status;
    875}
    876
    877static __be32
    878nfsd4_getattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
    879	      union nfsd4_op_u *u)
    880{
    881	struct nfsd4_getattr *getattr = &u->getattr;
    882	__be32 status;
    883
    884	status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
    885	if (status)
    886		return status;
    887
    888	if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
    889		return nfserr_inval;
    890
    891	getattr->ga_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
    892	getattr->ga_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
    893	getattr->ga_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
    894
    895	getattr->ga_fhp = &cstate->current_fh;
    896	return nfs_ok;
    897}
    898
    899static __be32
    900nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
    901	   union nfsd4_op_u *u)
    902{
    903	struct nfsd4_link *link = &u->link;
    904	__be32 status;
    905
    906	status = nfsd_link(rqstp, &cstate->current_fh,
    907			   link->li_name, link->li_namelen, &cstate->save_fh);
    908	if (!status)
    909		set_change_info(&link->li_cinfo, &cstate->current_fh);
    910	return status;
    911}
    912
    913static __be32 nfsd4_do_lookupp(struct svc_rqst *rqstp, struct svc_fh *fh)
    914{
    915	struct svc_fh tmp_fh;
    916	__be32 ret;
    917
    918	fh_init(&tmp_fh, NFS4_FHSIZE);
    919	ret = exp_pseudoroot(rqstp, &tmp_fh);
    920	if (ret)
    921		return ret;
    922	if (tmp_fh.fh_dentry == fh->fh_dentry) {
    923		fh_put(&tmp_fh);
    924		return nfserr_noent;
    925	}
    926	fh_put(&tmp_fh);
    927	return nfsd_lookup(rqstp, fh, "..", 2, fh);
    928}
    929
    930static __be32
    931nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
    932	      union nfsd4_op_u *u)
    933{
    934	return nfsd4_do_lookupp(rqstp, &cstate->current_fh);
    935}
    936
    937static __be32
    938nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
    939	     union nfsd4_op_u *u)
    940{
    941	return nfsd_lookup(rqstp, &cstate->current_fh,
    942			   u->lookup.lo_name, u->lookup.lo_len,
    943			   &cstate->current_fh);
    944}
    945
    946static __be32
    947nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
    948	   union nfsd4_op_u *u)
    949{
    950	struct nfsd4_read *read = &u->read;
    951	__be32 status;
    952
    953	read->rd_nf = NULL;
    954
    955	trace_nfsd_read_start(rqstp, &cstate->current_fh,
    956			      read->rd_offset, read->rd_length);
    957
    958	read->rd_length = min_t(u32, read->rd_length, svc_max_payload(rqstp));
    959	if (read->rd_offset > (u64)OFFSET_MAX)
    960		read->rd_offset = (u64)OFFSET_MAX;
    961	if (read->rd_offset + read->rd_length > (u64)OFFSET_MAX)
    962		read->rd_length = (u64)OFFSET_MAX - read->rd_offset;
    963
    964	/*
    965	 * If we do a zero copy read, then a client will see read data
    966	 * that reflects the state of the file *after* performing the
    967	 * following compound.
    968	 *
    969	 * To ensure proper ordering, we therefore turn off zero copy if
    970	 * the client wants us to do more in this compound:
    971	 */
    972	if (!nfsd4_last_compound_op(rqstp))
    973		__clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
    974
    975	/* check stateid */
    976	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
    977					&read->rd_stateid, RD_STATE,
    978					&read->rd_nf, NULL);
    979	if (status) {
    980		dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
    981		goto out;
    982	}
    983	status = nfs_ok;
    984out:
    985	read->rd_rqstp = rqstp;
    986	read->rd_fhp = &cstate->current_fh;
    987	return status;
    988}
    989
    990
    991static void
    992nfsd4_read_release(union nfsd4_op_u *u)
    993{
    994	if (u->read.rd_nf)
    995		nfsd_file_put(u->read.rd_nf);
    996	trace_nfsd_read_done(u->read.rd_rqstp, u->read.rd_fhp,
    997			     u->read.rd_offset, u->read.rd_length);
    998}
    999
   1000static __be32
   1001nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
   1002	      union nfsd4_op_u *u)
   1003{
   1004	struct nfsd4_readdir *readdir = &u->readdir;
   1005	u64 cookie = readdir->rd_cookie;
   1006	static const nfs4_verifier zeroverf;
   1007
   1008	/* no need to check permission - this will be done in nfsd_readdir() */
   1009
   1010	if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
   1011		return nfserr_inval;
   1012
   1013	readdir->rd_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
   1014	readdir->rd_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
   1015	readdir->rd_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
   1016
   1017	if ((cookie == 1) || (cookie == 2) ||
   1018	    (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
   1019		return nfserr_bad_cookie;
   1020
   1021	readdir->rd_rqstp = rqstp;
   1022	readdir->rd_fhp = &cstate->current_fh;
   1023	return nfs_ok;
   1024}
   1025
   1026static __be32
   1027nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
   1028	       union nfsd4_op_u *u)
   1029{
   1030	u->readlink.rl_rqstp = rqstp;
   1031	u->readlink.rl_fhp = &cstate->current_fh;
   1032	return nfs_ok;
   1033}
   1034
   1035static __be32
   1036nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
   1037	     union nfsd4_op_u *u)
   1038{
   1039	struct nfsd4_remove *remove = &u->remove;
   1040	__be32 status;
   1041
   1042	if (opens_in_grace(SVC_NET(rqstp)))
   1043		return nfserr_grace;
   1044	status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
   1045			     remove->rm_name, remove->rm_namelen);
   1046	if (!status) {
   1047		fh_unlock(&cstate->current_fh);
   1048		set_change_info(&remove->rm_cinfo, &cstate->current_fh);
   1049	}
   1050	return status;
   1051}
   1052
   1053static __be32
   1054nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
   1055	     union nfsd4_op_u *u)
   1056{
   1057	struct nfsd4_rename *rename = &u->rename;
   1058	__be32 status;
   1059
   1060	if (opens_in_grace(SVC_NET(rqstp)))
   1061		return nfserr_grace;
   1062	status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname,
   1063			     rename->rn_snamelen, &cstate->current_fh,
   1064			     rename->rn_tname, rename->rn_tnamelen);
   1065	if (status)
   1066		return status;
   1067	set_change_info(&rename->rn_sinfo, &cstate->current_fh);
   1068	set_change_info(&rename->rn_tinfo, &cstate->save_fh);
   1069	return nfs_ok;
   1070}
   1071
   1072static __be32
   1073nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
   1074	      union nfsd4_op_u *u)
   1075{
   1076	struct nfsd4_secinfo *secinfo = &u->secinfo;
   1077	struct svc_export *exp;
   1078	struct dentry *dentry;
   1079	__be32 err;
   1080
   1081	err = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_EXEC);
   1082	if (err)
   1083		return err;
   1084	err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
   1085				    secinfo->si_name, secinfo->si_namelen,
   1086				    &exp, &dentry);
   1087	if (err)
   1088		return err;
   1089	fh_unlock(&cstate->current_fh);
   1090	if (d_really_is_negative(dentry)) {
   1091		exp_put(exp);
   1092		err = nfserr_noent;
   1093	} else
   1094		secinfo->si_exp = exp;
   1095	dput(dentry);
   1096	if (cstate->minorversion)
   1097		/* See rfc 5661 section 2.6.3.1.1.8 */
   1098		fh_put(&cstate->current_fh);
   1099	return err;
   1100}
   1101
   1102static __be32
   1103nfsd4_secinfo_no_name(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
   1104		union nfsd4_op_u *u)
   1105{
   1106	__be32 err;
   1107
   1108	switch (u->secinfo_no_name.sin_style) {
   1109	case NFS4_SECINFO_STYLE4_CURRENT_FH:
   1110		break;
   1111	case NFS4_SECINFO_STYLE4_PARENT:
   1112		err = nfsd4_do_lookupp(rqstp, &cstate->current_fh);
   1113		if (err)
   1114			return err;
   1115		break;
   1116	default:
   1117		return nfserr_inval;
   1118	}
   1119
   1120	u->secinfo_no_name.sin_exp = exp_get(cstate->current_fh.fh_export);
   1121	fh_put(&cstate->current_fh);
   1122	return nfs_ok;
   1123}
   1124
   1125static void
   1126nfsd4_secinfo_release(union nfsd4_op_u *u)
   1127{
   1128	if (u->secinfo.si_exp)
   1129		exp_put(u->secinfo.si_exp);
   1130}
   1131
   1132static void
   1133nfsd4_secinfo_no_name_release(union nfsd4_op_u *u)
   1134{
   1135	if (u->secinfo_no_name.sin_exp)
   1136		exp_put(u->secinfo_no_name.sin_exp);
   1137}
   1138
   1139static __be32
   1140nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
   1141	      union nfsd4_op_u *u)
   1142{
   1143	struct nfsd4_setattr *setattr = &u->setattr;
   1144	__be32 status = nfs_ok;
   1145	int err;
   1146
   1147	if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
   1148		status = nfs4_preprocess_stateid_op(rqstp, cstate,
   1149				&cstate->current_fh, &setattr->sa_stateid,
   1150				WR_STATE, NULL, NULL);
   1151		if (status) {
   1152			dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
   1153			return status;
   1154		}
   1155	}
   1156	err = fh_want_write(&cstate->current_fh);
   1157	if (err)
   1158		return nfserrno(err);
   1159	status = nfs_ok;
   1160
   1161	status = check_attr_support(rqstp, cstate, setattr->sa_bmval,
   1162				    nfsd_attrmask);
   1163	if (status)
   1164		goto out;
   1165
   1166	if (setattr->sa_acl != NULL)
   1167		status = nfsd4_set_nfs4_acl(rqstp, &cstate->current_fh,
   1168					    setattr->sa_acl);
   1169	if (status)
   1170		goto out;
   1171	if (setattr->sa_label.len)
   1172		status = nfsd4_set_nfs4_label(rqstp, &cstate->current_fh,
   1173				&setattr->sa_label);
   1174	if (status)
   1175		goto out;
   1176	status = nfsd_setattr(rqstp, &cstate->current_fh, &setattr->sa_iattr,
   1177				0, (time64_t)0);
   1178out:
   1179	fh_drop_write(&cstate->current_fh);
   1180	return status;
   1181}
   1182
   1183static __be32
   1184nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
   1185	    union nfsd4_op_u *u)
   1186{
   1187	struct nfsd4_write *write = &u->write;
   1188	stateid_t *stateid = &write->wr_stateid;
   1189	struct nfsd_file *nf = NULL;
   1190	__be32 status = nfs_ok;
   1191	unsigned long cnt;
   1192	int nvecs;
   1193
   1194	if (write->wr_offset > (u64)OFFSET_MAX ||
   1195	    write->wr_offset + write->wr_buflen > (u64)OFFSET_MAX)
   1196		return nfserr_fbig;
   1197
   1198	cnt = write->wr_buflen;
   1199	trace_nfsd_write_start(rqstp, &cstate->current_fh,
   1200			       write->wr_offset, cnt);
   1201	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
   1202						stateid, WR_STATE, &nf, NULL);
   1203	if (status) {
   1204		dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
   1205		return status;
   1206	}
   1207
   1208	write->wr_how_written = write->wr_stable_how;
   1209
   1210	nvecs = svc_fill_write_vector(rqstp, &write->wr_payload);
   1211	WARN_ON_ONCE(nvecs > ARRAY_SIZE(rqstp->rq_vec));
   1212
   1213	status = nfsd_vfs_write(rqstp, &cstate->current_fh, nf,
   1214				write->wr_offset, rqstp->rq_vec, nvecs, &cnt,
   1215				write->wr_how_written,
   1216				(__be32 *)write->wr_verifier.data);
   1217	nfsd_file_put(nf);
   1218
   1219	write->wr_bytes_written = cnt;
   1220	trace_nfsd_write_done(rqstp, &cstate->current_fh,
   1221			      write->wr_offset, cnt);
   1222	return status;
   1223}
   1224
   1225static __be32
   1226nfsd4_verify_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
   1227		  stateid_t *src_stateid, struct nfsd_file **src,
   1228		  stateid_t *dst_stateid, struct nfsd_file **dst)
   1229{
   1230	__be32 status;
   1231
   1232	if (!cstate->save_fh.fh_dentry)
   1233		return nfserr_nofilehandle;
   1234
   1235	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->save_fh,
   1236					    src_stateid, RD_STATE, src, NULL);
   1237	if (status) {
   1238		dprintk("NFSD: %s: couldn't process src stateid!\n", __func__);
   1239		goto out;
   1240	}
   1241
   1242	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
   1243					    dst_stateid, WR_STATE, dst, NULL);
   1244	if (status) {
   1245		dprintk("NFSD: %s: couldn't process dst stateid!\n", __func__);
   1246		goto out_put_src;
   1247	}
   1248
   1249	/* fix up for NFS-specific error code */
   1250	if (!S_ISREG(file_inode((*src)->nf_file)->i_mode) ||
   1251	    !S_ISREG(file_inode((*dst)->nf_file)->i_mode)) {
   1252		status = nfserr_wrong_type;
   1253		goto out_put_dst;
   1254	}
   1255
   1256out:
   1257	return status;
   1258out_put_dst:
   1259	nfsd_file_put(*dst);
   1260out_put_src:
   1261	nfsd_file_put(*src);
   1262	goto out;
   1263}
   1264
   1265static __be32
   1266nfsd4_clone(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
   1267		union nfsd4_op_u *u)
   1268{
   1269	struct nfsd4_clone *clone = &u->clone;
   1270	struct nfsd_file *src, *dst;
   1271	__be32 status;
   1272
   1273	status = nfsd4_verify_copy(rqstp, cstate, &clone->cl_src_stateid, &src,
   1274				   &clone->cl_dst_stateid, &dst);
   1275	if (status)
   1276		goto out;
   1277
   1278	status = nfsd4_clone_file_range(rqstp, src, clone->cl_src_pos,
   1279			dst, clone->cl_dst_pos, clone->cl_count,
   1280			EX_ISSYNC(cstate->current_fh.fh_export));
   1281
   1282	nfsd_file_put(dst);
   1283	nfsd_file_put(src);
   1284out:
   1285	return status;
   1286}
   1287
   1288void nfs4_put_copy(struct nfsd4_copy *copy)
   1289{
   1290	if (!refcount_dec_and_test(&copy->refcount))
   1291		return;
   1292	kfree(copy);
   1293}
   1294
   1295static bool
   1296check_and_set_stop_copy(struct nfsd4_copy *copy)
   1297{
   1298	bool value;
   1299
   1300	spin_lock(&copy->cp_clp->async_lock);
   1301	value = copy->stopped;
   1302	if (!copy->stopped)
   1303		copy->stopped = true;
   1304	spin_unlock(&copy->cp_clp->async_lock);
   1305	return value;
   1306}
   1307
   1308static void nfsd4_stop_copy(struct nfsd4_copy *copy)
   1309{
   1310	/* only 1 thread should stop the copy */
   1311	if (!check_and_set_stop_copy(copy))
   1312		kthread_stop(copy->copy_task);
   1313	nfs4_put_copy(copy);
   1314}
   1315
   1316static struct nfsd4_copy *nfsd4_get_copy(struct nfs4_client *clp)
   1317{
   1318	struct nfsd4_copy *copy = NULL;
   1319
   1320	spin_lock(&clp->async_lock);
   1321	if (!list_empty(&clp->async_copies)) {
   1322		copy = list_first_entry(&clp->async_copies, struct nfsd4_copy,
   1323					copies);
   1324		refcount_inc(&copy->refcount);
   1325	}
   1326	spin_unlock(&clp->async_lock);
   1327	return copy;
   1328}
   1329
   1330void nfsd4_shutdown_copy(struct nfs4_client *clp)
   1331{
   1332	struct nfsd4_copy *copy;
   1333
   1334	while ((copy = nfsd4_get_copy(clp)) != NULL)
   1335		nfsd4_stop_copy(copy);
   1336}
   1337#ifdef CONFIG_NFSD_V4_2_INTER_SSC
   1338
   1339extern struct file *nfs42_ssc_open(struct vfsmount *ss_mnt,
   1340				   struct nfs_fh *src_fh,
   1341				   nfs4_stateid *stateid);
   1342extern void nfs42_ssc_close(struct file *filep);
   1343
   1344extern void nfs_sb_deactive(struct super_block *sb);
   1345
   1346#define NFSD42_INTERSSC_MOUNTOPS "vers=4.2,addr=%s,sec=sys"
   1347
   1348/*
   1349 * setup a work entry in the ssc delayed unmount list.
   1350 */
   1351static __be32 nfsd4_ssc_setup_dul(struct nfsd_net *nn, char *ipaddr,
   1352		struct nfsd4_ssc_umount_item **retwork, struct vfsmount **ss_mnt)
   1353{
   1354	struct nfsd4_ssc_umount_item *ni = NULL;
   1355	struct nfsd4_ssc_umount_item *work = NULL;
   1356	struct nfsd4_ssc_umount_item *tmp;
   1357	DEFINE_WAIT(wait);
   1358
   1359	*ss_mnt = NULL;
   1360	*retwork = NULL;
   1361	work = kzalloc(sizeof(*work), GFP_KERNEL);
   1362try_again:
   1363	spin_lock(&nn->nfsd_ssc_lock);
   1364	list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
   1365		if (strncmp(ni->nsui_ipaddr, ipaddr, sizeof(ni->nsui_ipaddr)))
   1366			continue;
   1367		/* found a match */
   1368		if (ni->nsui_busy) {
   1369			/*  wait - and try again */
   1370			prepare_to_wait(&nn->nfsd_ssc_waitq, &wait,
   1371				TASK_INTERRUPTIBLE);
   1372			spin_unlock(&nn->nfsd_ssc_lock);
   1373
   1374			/* allow 20secs for mount/unmount for now - revisit */
   1375			if (signal_pending(current) ||
   1376					(schedule_timeout(20*HZ) == 0)) {
   1377				kfree(work);
   1378				return nfserr_eagain;
   1379			}
   1380			finish_wait(&nn->nfsd_ssc_waitq, &wait);
   1381			goto try_again;
   1382		}
   1383		*ss_mnt = ni->nsui_vfsmount;
   1384		refcount_inc(&ni->nsui_refcnt);
   1385		spin_unlock(&nn->nfsd_ssc_lock);
   1386		kfree(work);
   1387
   1388		/* return vfsmount in ss_mnt */
   1389		return 0;
   1390	}
   1391	if (work) {
   1392		strncpy(work->nsui_ipaddr, ipaddr, sizeof(work->nsui_ipaddr));
   1393		refcount_set(&work->nsui_refcnt, 2);
   1394		work->nsui_busy = true;
   1395		list_add_tail(&work->nsui_list, &nn->nfsd_ssc_mount_list);
   1396		*retwork = work;
   1397	}
   1398	spin_unlock(&nn->nfsd_ssc_lock);
   1399	return 0;
   1400}
   1401
   1402static void nfsd4_ssc_update_dul_work(struct nfsd_net *nn,
   1403		struct nfsd4_ssc_umount_item *work, struct vfsmount *ss_mnt)
   1404{
   1405	/* set nsui_vfsmount, clear busy flag and wakeup waiters */
   1406	spin_lock(&nn->nfsd_ssc_lock);
   1407	work->nsui_vfsmount = ss_mnt;
   1408	work->nsui_busy = false;
   1409	wake_up_all(&nn->nfsd_ssc_waitq);
   1410	spin_unlock(&nn->nfsd_ssc_lock);
   1411}
   1412
   1413static void nfsd4_ssc_cancel_dul_work(struct nfsd_net *nn,
   1414		struct nfsd4_ssc_umount_item *work)
   1415{
   1416	spin_lock(&nn->nfsd_ssc_lock);
   1417	list_del(&work->nsui_list);
   1418	wake_up_all(&nn->nfsd_ssc_waitq);
   1419	spin_unlock(&nn->nfsd_ssc_lock);
   1420	kfree(work);
   1421}
   1422
   1423/*
   1424 * Support one copy source server for now.
   1425 */
   1426static __be32
   1427nfsd4_interssc_connect(struct nl4_server *nss, struct svc_rqst *rqstp,
   1428		       struct vfsmount **mount)
   1429{
   1430	struct file_system_type *type;
   1431	struct vfsmount *ss_mnt;
   1432	struct nfs42_netaddr *naddr;
   1433	struct sockaddr_storage tmp_addr;
   1434	size_t tmp_addrlen, match_netid_len = 3;
   1435	char *startsep = "", *endsep = "", *match_netid = "tcp";
   1436	char *ipaddr, *dev_name, *raw_data;
   1437	int len, raw_len;
   1438	__be32 status = nfserr_inval;
   1439	struct nfsd4_ssc_umount_item *work = NULL;
   1440	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
   1441
   1442	naddr = &nss->u.nl4_addr;
   1443	tmp_addrlen = rpc_uaddr2sockaddr(SVC_NET(rqstp), naddr->addr,
   1444					 naddr->addr_len,
   1445					 (struct sockaddr *)&tmp_addr,
   1446					 sizeof(tmp_addr));
   1447	if (tmp_addrlen == 0)
   1448		goto out_err;
   1449
   1450	if (tmp_addr.ss_family == AF_INET6) {
   1451		startsep = "[";
   1452		endsep = "]";
   1453		match_netid = "tcp6";
   1454		match_netid_len = 4;
   1455	}
   1456
   1457	if (naddr->netid_len != match_netid_len ||
   1458		strncmp(naddr->netid, match_netid, naddr->netid_len))
   1459		goto out_err;
   1460
   1461	/* Construct the raw data for the vfs_kern_mount call */
   1462	len = RPC_MAX_ADDRBUFLEN + 1;
   1463	ipaddr = kzalloc(len, GFP_KERNEL);
   1464	if (!ipaddr)
   1465		goto out_err;
   1466
   1467	rpc_ntop((struct sockaddr *)&tmp_addr, ipaddr, len);
   1468
   1469	/* 2 for ipv6 endsep and startsep. 3 for ":/" and trailing '/0'*/
   1470
   1471	raw_len = strlen(NFSD42_INTERSSC_MOUNTOPS) + strlen(ipaddr);
   1472	raw_data = kzalloc(raw_len, GFP_KERNEL);
   1473	if (!raw_data)
   1474		goto out_free_ipaddr;
   1475
   1476	snprintf(raw_data, raw_len, NFSD42_INTERSSC_MOUNTOPS, ipaddr);
   1477
   1478	status = nfserr_nodev;
   1479	type = get_fs_type("nfs");
   1480	if (!type)
   1481		goto out_free_rawdata;
   1482
   1483	/* Set the server:<export> for the vfs_kern_mount call */
   1484	dev_name = kzalloc(len + 5, GFP_KERNEL);
   1485	if (!dev_name)
   1486		goto out_free_rawdata;
   1487	snprintf(dev_name, len + 5, "%s%s%s:/", startsep, ipaddr, endsep);
   1488
   1489	status = nfsd4_ssc_setup_dul(nn, ipaddr, &work, &ss_mnt);
   1490	if (status)
   1491		goto out_free_devname;
   1492	if (ss_mnt)
   1493		goto out_done;
   1494
   1495	/* Use an 'internal' mount: SB_KERNMOUNT -> MNT_INTERNAL */
   1496	ss_mnt = vfs_kern_mount(type, SB_KERNMOUNT, dev_name, raw_data);
   1497	module_put(type->owner);
   1498	if (IS_ERR(ss_mnt)) {
   1499		status = nfserr_nodev;
   1500		if (work)
   1501			nfsd4_ssc_cancel_dul_work(nn, work);
   1502		goto out_free_devname;
   1503	}
   1504	if (work)
   1505		nfsd4_ssc_update_dul_work(nn, work, ss_mnt);
   1506out_done:
   1507	status = 0;
   1508	*mount = ss_mnt;
   1509
   1510out_free_devname:
   1511	kfree(dev_name);
   1512out_free_rawdata:
   1513	kfree(raw_data);
   1514out_free_ipaddr:
   1515	kfree(ipaddr);
   1516out_err:
   1517	return status;
   1518}
   1519
   1520static void
   1521nfsd4_interssc_disconnect(struct vfsmount *ss_mnt)
   1522{
   1523	nfs_do_sb_deactive(ss_mnt->mnt_sb);
   1524	mntput(ss_mnt);
   1525}
   1526
   1527/*
   1528 * Verify COPY destination stateid.
   1529 *
   1530 * Connect to the source server with NFSv4.1.
   1531 * Create the source struct file for nfsd_copy_range.
   1532 * Called with COPY cstate:
   1533 *    SAVED_FH: source filehandle
   1534 *    CURRENT_FH: destination filehandle
   1535 */
   1536static __be32
   1537nfsd4_setup_inter_ssc(struct svc_rqst *rqstp,
   1538		      struct nfsd4_compound_state *cstate,
   1539		      struct nfsd4_copy *copy, struct vfsmount **mount)
   1540{
   1541	struct svc_fh *s_fh = NULL;
   1542	stateid_t *s_stid = &copy->cp_src_stateid;
   1543	__be32 status = nfserr_inval;
   1544
   1545	/* Verify the destination stateid and set dst struct file*/
   1546	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
   1547					    &copy->cp_dst_stateid,
   1548					    WR_STATE, &copy->nf_dst, NULL);
   1549	if (status)
   1550		goto out;
   1551
   1552	status = nfsd4_interssc_connect(&copy->cp_src, rqstp, mount);
   1553	if (status)
   1554		goto out;
   1555
   1556	s_fh = &cstate->save_fh;
   1557
   1558	copy->c_fh.size = s_fh->fh_handle.fh_size;
   1559	memcpy(copy->c_fh.data, &s_fh->fh_handle.fh_raw, copy->c_fh.size);
   1560	copy->stateid.seqid = cpu_to_be32(s_stid->si_generation);
   1561	memcpy(copy->stateid.other, (void *)&s_stid->si_opaque,
   1562	       sizeof(stateid_opaque_t));
   1563
   1564	status = 0;
   1565out:
   1566	return status;
   1567}
   1568
   1569static void
   1570nfsd4_cleanup_inter_ssc(struct vfsmount *ss_mnt, struct nfsd_file *src,
   1571			struct nfsd_file *dst)
   1572{
   1573	bool found = false;
   1574	long timeout;
   1575	struct nfsd4_ssc_umount_item *tmp;
   1576	struct nfsd4_ssc_umount_item *ni = NULL;
   1577	struct nfsd_net *nn = net_generic(dst->nf_net, nfsd_net_id);
   1578
   1579	nfs42_ssc_close(src->nf_file);
   1580	nfsd_file_put(dst);
   1581	fput(src->nf_file);
   1582
   1583	if (!nn) {
   1584		mntput(ss_mnt);
   1585		return;
   1586	}
   1587	spin_lock(&nn->nfsd_ssc_lock);
   1588	timeout = msecs_to_jiffies(nfsd4_ssc_umount_timeout);
   1589	list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
   1590		if (ni->nsui_vfsmount->mnt_sb == ss_mnt->mnt_sb) {
   1591			list_del(&ni->nsui_list);
   1592			/*
   1593			 * vfsmount can be shared by multiple exports,
   1594			 * decrement refcnt. If the count drops to 1 it
   1595			 * will be unmounted when nsui_expire expires.
   1596			 */
   1597			refcount_dec(&ni->nsui_refcnt);
   1598			ni->nsui_expire = jiffies + timeout;
   1599			list_add_tail(&ni->nsui_list, &nn->nfsd_ssc_mount_list);
   1600			found = true;
   1601			break;
   1602		}
   1603	}
   1604	spin_unlock(&nn->nfsd_ssc_lock);
   1605	if (!found) {
   1606		mntput(ss_mnt);
   1607		return;
   1608	}
   1609}
   1610
   1611#else /* CONFIG_NFSD_V4_2_INTER_SSC */
   1612
   1613static __be32
   1614nfsd4_setup_inter_ssc(struct svc_rqst *rqstp,
   1615		      struct nfsd4_compound_state *cstate,
   1616		      struct nfsd4_copy *copy,
   1617		      struct vfsmount **mount)
   1618{
   1619	*mount = NULL;
   1620	return nfserr_inval;
   1621}
   1622
   1623static void
   1624nfsd4_cleanup_inter_ssc(struct vfsmount *ss_mnt, struct nfsd_file *src,
   1625			struct nfsd_file *dst)
   1626{
   1627}
   1628
   1629static void
   1630nfsd4_interssc_disconnect(struct vfsmount *ss_mnt)
   1631{
   1632}
   1633
   1634static struct file *nfs42_ssc_open(struct vfsmount *ss_mnt,
   1635				   struct nfs_fh *src_fh,
   1636				   nfs4_stateid *stateid)
   1637{
   1638	return NULL;
   1639}
   1640#endif /* CONFIG_NFSD_V4_2_INTER_SSC */
   1641
   1642static __be32
   1643nfsd4_setup_intra_ssc(struct svc_rqst *rqstp,
   1644		      struct nfsd4_compound_state *cstate,
   1645		      struct nfsd4_copy *copy)
   1646{
   1647	return nfsd4_verify_copy(rqstp, cstate, &copy->cp_src_stateid,
   1648				 &copy->nf_src, &copy->cp_dst_stateid,
   1649				 &copy->nf_dst);
   1650}
   1651
   1652static void
   1653nfsd4_cleanup_intra_ssc(struct nfsd_file *src, struct nfsd_file *dst)
   1654{
   1655	nfsd_file_put(src);
   1656	nfsd_file_put(dst);
   1657}
   1658
   1659static void nfsd4_cb_offload_release(struct nfsd4_callback *cb)
   1660{
   1661	struct nfsd4_copy *copy = container_of(cb, struct nfsd4_copy, cp_cb);
   1662
   1663	nfs4_put_copy(copy);
   1664}
   1665
   1666static int nfsd4_cb_offload_done(struct nfsd4_callback *cb,
   1667				 struct rpc_task *task)
   1668{
   1669	return 1;
   1670}
   1671
   1672static const struct nfsd4_callback_ops nfsd4_cb_offload_ops = {
   1673	.release = nfsd4_cb_offload_release,
   1674	.done = nfsd4_cb_offload_done
   1675};
   1676
   1677static void nfsd4_init_copy_res(struct nfsd4_copy *copy, bool sync)
   1678{
   1679	copy->cp_res.wr_stable_how =
   1680		copy->committed ? NFS_FILE_SYNC : NFS_UNSTABLE;
   1681	copy->cp_synchronous = sync;
   1682	gen_boot_verifier(&copy->cp_res.wr_verifier, copy->cp_clp->net);
   1683}
   1684
   1685static ssize_t _nfsd_copy_file_range(struct nfsd4_copy *copy)
   1686{
   1687	struct file *dst = copy->nf_dst->nf_file;
   1688	struct file *src = copy->nf_src->nf_file;
   1689	errseq_t since;
   1690	ssize_t bytes_copied = 0;
   1691	u64 bytes_total = copy->cp_count;
   1692	u64 src_pos = copy->cp_src_pos;
   1693	u64 dst_pos = copy->cp_dst_pos;
   1694	int status;
   1695
   1696	/* See RFC 7862 p.67: */
   1697	if (bytes_total == 0)
   1698		bytes_total = ULLONG_MAX;
   1699	do {
   1700		if (kthread_should_stop())
   1701			break;
   1702		bytes_copied = nfsd_copy_file_range(src, src_pos, dst, dst_pos,
   1703						    bytes_total);
   1704		if (bytes_copied <= 0)
   1705			break;
   1706		bytes_total -= bytes_copied;
   1707		copy->cp_res.wr_bytes_written += bytes_copied;
   1708		src_pos += bytes_copied;
   1709		dst_pos += bytes_copied;
   1710	} while (bytes_total > 0 && !copy->cp_synchronous);
   1711	/* for a non-zero asynchronous copy do a commit of data */
   1712	if (!copy->cp_synchronous && copy->cp_res.wr_bytes_written > 0) {
   1713		since = READ_ONCE(dst->f_wb_err);
   1714		status = vfs_fsync_range(dst, copy->cp_dst_pos,
   1715					 copy->cp_res.wr_bytes_written, 0);
   1716		if (!status)
   1717			status = filemap_check_wb_err(dst->f_mapping, since);
   1718		if (!status)
   1719			copy->committed = true;
   1720	}
   1721	return bytes_copied;
   1722}
   1723
   1724static __be32 nfsd4_do_copy(struct nfsd4_copy *copy, bool sync)
   1725{
   1726	__be32 status;
   1727	ssize_t bytes;
   1728
   1729	bytes = _nfsd_copy_file_range(copy);
   1730	/* for async copy, we ignore the error, client can always retry
   1731	 * to get the error
   1732	 */
   1733	if (bytes < 0 && !copy->cp_res.wr_bytes_written)
   1734		status = nfserrno(bytes);
   1735	else {
   1736		nfsd4_init_copy_res(copy, sync);
   1737		status = nfs_ok;
   1738	}
   1739
   1740	if (!copy->cp_intra) /* Inter server SSC */
   1741		nfsd4_cleanup_inter_ssc(copy->ss_mnt, copy->nf_src,
   1742					copy->nf_dst);
   1743	else
   1744		nfsd4_cleanup_intra_ssc(copy->nf_src, copy->nf_dst);
   1745
   1746	return status;
   1747}
   1748
   1749static void dup_copy_fields(struct nfsd4_copy *src, struct nfsd4_copy *dst)
   1750{
   1751	dst->cp_src_pos = src->cp_src_pos;
   1752	dst->cp_dst_pos = src->cp_dst_pos;
   1753	dst->cp_count = src->cp_count;
   1754	dst->cp_synchronous = src->cp_synchronous;
   1755	memcpy(&dst->cp_res, &src->cp_res, sizeof(src->cp_res));
   1756	memcpy(&dst->fh, &src->fh, sizeof(src->fh));
   1757	dst->cp_clp = src->cp_clp;
   1758	dst->nf_dst = nfsd_file_get(src->nf_dst);
   1759	dst->cp_intra = src->cp_intra;
   1760	if (src->cp_intra) /* for inter, file_src doesn't exist yet */
   1761		dst->nf_src = nfsd_file_get(src->nf_src);
   1762
   1763	memcpy(&dst->cp_stateid, &src->cp_stateid, sizeof(src->cp_stateid));
   1764	memcpy(&dst->cp_src, &src->cp_src, sizeof(struct nl4_server));
   1765	memcpy(&dst->stateid, &src->stateid, sizeof(src->stateid));
   1766	memcpy(&dst->c_fh, &src->c_fh, sizeof(src->c_fh));
   1767	dst->ss_mnt = src->ss_mnt;
   1768}
   1769
   1770static void cleanup_async_copy(struct nfsd4_copy *copy)
   1771{
   1772	nfs4_free_copy_state(copy);
   1773	nfsd_file_put(copy->nf_dst);
   1774	if (copy->cp_intra)
   1775		nfsd_file_put(copy->nf_src);
   1776	spin_lock(&copy->cp_clp->async_lock);
   1777	list_del(&copy->copies);
   1778	spin_unlock(&copy->cp_clp->async_lock);
   1779	nfs4_put_copy(copy);
   1780}
   1781
   1782static int nfsd4_do_async_copy(void *data)
   1783{
   1784	struct nfsd4_copy *copy = (struct nfsd4_copy *)data;
   1785	struct nfsd4_copy *cb_copy;
   1786
   1787	if (!copy->cp_intra) { /* Inter server SSC */
   1788		copy->nf_src = kzalloc(sizeof(struct nfsd_file), GFP_KERNEL);
   1789		if (!copy->nf_src) {
   1790			copy->nfserr = nfserr_serverfault;
   1791			nfsd4_interssc_disconnect(copy->ss_mnt);
   1792			goto do_callback;
   1793		}
   1794		copy->nf_src->nf_file = nfs42_ssc_open(copy->ss_mnt, &copy->c_fh,
   1795					      &copy->stateid);
   1796		if (IS_ERR(copy->nf_src->nf_file)) {
   1797			copy->nfserr = nfserr_offload_denied;
   1798			nfsd4_interssc_disconnect(copy->ss_mnt);
   1799			goto do_callback;
   1800		}
   1801	}
   1802
   1803	copy->nfserr = nfsd4_do_copy(copy, 0);
   1804do_callback:
   1805	cb_copy = kzalloc(sizeof(struct nfsd4_copy), GFP_KERNEL);
   1806	if (!cb_copy)
   1807		goto out;
   1808	refcount_set(&cb_copy->refcount, 1);
   1809	memcpy(&cb_copy->cp_res, &copy->cp_res, sizeof(copy->cp_res));
   1810	cb_copy->cp_clp = copy->cp_clp;
   1811	cb_copy->nfserr = copy->nfserr;
   1812	memcpy(&cb_copy->fh, &copy->fh, sizeof(copy->fh));
   1813	nfsd4_init_cb(&cb_copy->cp_cb, cb_copy->cp_clp,
   1814			&nfsd4_cb_offload_ops, NFSPROC4_CLNT_CB_OFFLOAD);
   1815	trace_nfsd_cb_offload(copy->cp_clp, &copy->cp_res.cb_stateid,
   1816			      &copy->fh, copy->cp_count, copy->nfserr);
   1817	nfsd4_run_cb(&cb_copy->cp_cb);
   1818out:
   1819	if (!copy->cp_intra)
   1820		kfree(copy->nf_src);
   1821	cleanup_async_copy(copy);
   1822	return 0;
   1823}
   1824
   1825static __be32
   1826nfsd4_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
   1827		union nfsd4_op_u *u)
   1828{
   1829	struct nfsd4_copy *copy = &u->copy;
   1830	__be32 status;
   1831	struct nfsd4_copy *async_copy = NULL;
   1832
   1833	if (!copy->cp_intra) { /* Inter server SSC */
   1834		if (!inter_copy_offload_enable || copy->cp_synchronous) {
   1835			status = nfserr_notsupp;
   1836			goto out;
   1837		}
   1838		status = nfsd4_setup_inter_ssc(rqstp, cstate, copy,
   1839				&copy->ss_mnt);
   1840		if (status)
   1841			return nfserr_offload_denied;
   1842	} else {
   1843		status = nfsd4_setup_intra_ssc(rqstp, cstate, copy);
   1844		if (status)
   1845			return status;
   1846	}
   1847
   1848	copy->cp_clp = cstate->clp;
   1849	memcpy(&copy->fh, &cstate->current_fh.fh_handle,
   1850		sizeof(struct knfsd_fh));
   1851	if (!copy->cp_synchronous) {
   1852		struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
   1853
   1854		status = nfserrno(-ENOMEM);
   1855		async_copy = kzalloc(sizeof(struct nfsd4_copy), GFP_KERNEL);
   1856		if (!async_copy)
   1857			goto out_err;
   1858		if (!nfs4_init_copy_state(nn, copy))
   1859			goto out_err;
   1860		refcount_set(&async_copy->refcount, 1);
   1861		memcpy(&copy->cp_res.cb_stateid, &copy->cp_stateid.stid,
   1862			sizeof(copy->cp_res.cb_stateid));
   1863		dup_copy_fields(copy, async_copy);
   1864		async_copy->copy_task = kthread_create(nfsd4_do_async_copy,
   1865				async_copy, "%s", "copy thread");
   1866		if (IS_ERR(async_copy->copy_task))
   1867			goto out_err;
   1868		spin_lock(&async_copy->cp_clp->async_lock);
   1869		list_add(&async_copy->copies,
   1870				&async_copy->cp_clp->async_copies);
   1871		spin_unlock(&async_copy->cp_clp->async_lock);
   1872		wake_up_process(async_copy->copy_task);
   1873		status = nfs_ok;
   1874	} else {
   1875		status = nfsd4_do_copy(copy, 1);
   1876	}
   1877out:
   1878	return status;
   1879out_err:
   1880	if (async_copy)
   1881		cleanup_async_copy(async_copy);
   1882	status = nfserrno(-ENOMEM);
   1883	if (!copy->cp_intra)
   1884		nfsd4_interssc_disconnect(copy->ss_mnt);
   1885	goto out;
   1886}
   1887
   1888struct nfsd4_copy *
   1889find_async_copy(struct nfs4_client *clp, stateid_t *stateid)
   1890{
   1891	struct nfsd4_copy *copy;
   1892
   1893	spin_lock(&clp->async_lock);
   1894	list_for_each_entry(copy, &clp->async_copies, copies) {
   1895		if (memcmp(&copy->cp_stateid.stid, stateid, NFS4_STATEID_SIZE))
   1896			continue;
   1897		refcount_inc(&copy->refcount);
   1898		spin_unlock(&clp->async_lock);
   1899		return copy;
   1900	}
   1901	spin_unlock(&clp->async_lock);
   1902	return NULL;
   1903}
   1904
   1905static __be32
   1906nfsd4_offload_cancel(struct svc_rqst *rqstp,
   1907		     struct nfsd4_compound_state *cstate,
   1908		     union nfsd4_op_u *u)
   1909{
   1910	struct nfsd4_offload_status *os = &u->offload_status;
   1911	struct nfsd4_copy *copy;
   1912	struct nfs4_client *clp = cstate->clp;
   1913
   1914	copy = find_async_copy(clp, &os->stateid);
   1915	if (!copy) {
   1916		struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
   1917
   1918		return manage_cpntf_state(nn, &os->stateid, clp, NULL);
   1919	} else
   1920		nfsd4_stop_copy(copy);
   1921
   1922	return nfs_ok;
   1923}
   1924
   1925static __be32
   1926nfsd4_copy_notify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
   1927		  union nfsd4_op_u *u)
   1928{
   1929	struct nfsd4_copy_notify *cn = &u->copy_notify;
   1930	__be32 status;
   1931	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
   1932	struct nfs4_stid *stid;
   1933	struct nfs4_cpntf_state *cps;
   1934	struct nfs4_client *clp = cstate->clp;
   1935
   1936	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
   1937					&cn->cpn_src_stateid, RD_STATE, NULL,
   1938					&stid);
   1939	if (status)
   1940		return status;
   1941
   1942	cn->cpn_sec = nn->nfsd4_lease;
   1943	cn->cpn_nsec = 0;
   1944
   1945	status = nfserrno(-ENOMEM);
   1946	cps = nfs4_alloc_init_cpntf_state(nn, stid);
   1947	if (!cps)
   1948		goto out;
   1949	memcpy(&cn->cpn_cnr_stateid, &cps->cp_stateid.stid, sizeof(stateid_t));
   1950	memcpy(&cps->cp_p_stateid, &stid->sc_stateid, sizeof(stateid_t));
   1951	memcpy(&cps->cp_p_clid, &clp->cl_clientid, sizeof(clientid_t));
   1952
   1953	/* For now, only return one server address in cpn_src, the
   1954	 * address used by the client to connect to this server.
   1955	 */
   1956	cn->cpn_src.nl4_type = NL4_NETADDR;
   1957	status = nfsd4_set_netaddr((struct sockaddr *)&rqstp->rq_daddr,
   1958				 &cn->cpn_src.u.nl4_addr);
   1959	WARN_ON_ONCE(status);
   1960	if (status) {
   1961		nfs4_put_cpntf_state(nn, cps);
   1962		goto out;
   1963	}
   1964out:
   1965	nfs4_put_stid(stid);
   1966	return status;
   1967}
   1968
   1969static __be32
   1970nfsd4_fallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
   1971		struct nfsd4_fallocate *fallocate, int flags)
   1972{
   1973	__be32 status;
   1974	struct nfsd_file *nf;
   1975
   1976	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
   1977					    &fallocate->falloc_stateid,
   1978					    WR_STATE, &nf, NULL);
   1979	if (status != nfs_ok) {
   1980		dprintk("NFSD: nfsd4_fallocate: couldn't process stateid!\n");
   1981		return status;
   1982	}
   1983
   1984	status = nfsd4_vfs_fallocate(rqstp, &cstate->current_fh, nf->nf_file,
   1985				     fallocate->falloc_offset,
   1986				     fallocate->falloc_length,
   1987				     flags);
   1988	nfsd_file_put(nf);
   1989	return status;
   1990}
   1991static __be32
   1992nfsd4_offload_status(struct svc_rqst *rqstp,
   1993		     struct nfsd4_compound_state *cstate,
   1994		     union nfsd4_op_u *u)
   1995{
   1996	struct nfsd4_offload_status *os = &u->offload_status;
   1997	__be32 status = 0;
   1998	struct nfsd4_copy *copy;
   1999	struct nfs4_client *clp = cstate->clp;
   2000
   2001	copy = find_async_copy(clp, &os->stateid);
   2002	if (copy) {
   2003		os->count = copy->cp_res.wr_bytes_written;
   2004		nfs4_put_copy(copy);
   2005	} else
   2006		status = nfserr_bad_stateid;
   2007
   2008	return status;
   2009}
   2010
   2011static __be32
   2012nfsd4_allocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
   2013	       union nfsd4_op_u *u)
   2014{
   2015	return nfsd4_fallocate(rqstp, cstate, &u->allocate, 0);
   2016}
   2017
   2018static __be32
   2019nfsd4_deallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
   2020		 union nfsd4_op_u *u)
   2021{
   2022	return nfsd4_fallocate(rqstp, cstate, &u->deallocate,
   2023			       FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
   2024}
   2025
   2026static __be32
   2027nfsd4_seek(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
   2028	   union nfsd4_op_u *u)
   2029{
   2030	struct nfsd4_seek *seek = &u->seek;
   2031	int whence;
   2032	__be32 status;
   2033	struct nfsd_file *nf;
   2034
   2035	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
   2036					    &seek->seek_stateid,
   2037					    RD_STATE, &nf, NULL);
   2038	if (status) {
   2039		dprintk("NFSD: nfsd4_seek: couldn't process stateid!\n");
   2040		return status;
   2041	}
   2042
   2043	switch (seek->seek_whence) {
   2044	case NFS4_CONTENT_DATA:
   2045		whence = SEEK_DATA;
   2046		break;
   2047	case NFS4_CONTENT_HOLE:
   2048		whence = SEEK_HOLE;
   2049		break;
   2050	default:
   2051		status = nfserr_union_notsupp;
   2052		goto out;
   2053	}
   2054
   2055	/*
   2056	 * Note:  This call does change file->f_pos, but nothing in NFSD
   2057	 *        should ever file->f_pos.
   2058	 */
   2059	seek->seek_pos = vfs_llseek(nf->nf_file, seek->seek_offset, whence);
   2060	if (seek->seek_pos < 0)
   2061		status = nfserrno(seek->seek_pos);
   2062	else if (seek->seek_pos >= i_size_read(file_inode(nf->nf_file)))
   2063		seek->seek_eof = true;
   2064
   2065out:
   2066	nfsd_file_put(nf);
   2067	return status;
   2068}
   2069
   2070/* This routine never returns NFS_OK!  If there are no other errors, it
   2071 * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
   2072 * attributes matched.  VERIFY is implemented by mapping NFSERR_SAME
   2073 * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
   2074 */
   2075static __be32
   2076_nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
   2077	     struct nfsd4_verify *verify)
   2078{
   2079	__be32 *buf, *p;
   2080	int count;
   2081	__be32 status;
   2082
   2083	status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
   2084	if (status)
   2085		return status;
   2086
   2087	status = check_attr_support(rqstp, cstate, verify->ve_bmval, NULL);
   2088	if (status)
   2089		return status;
   2090
   2091	if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
   2092	    || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
   2093		return nfserr_inval;
   2094	if (verify->ve_attrlen & 3)
   2095		return nfserr_inval;
   2096
   2097	/* count in words:
   2098	 *   bitmap_len(1) + bitmap(2) + attr_len(1) = 4
   2099	 */
   2100	count = 4 + (verify->ve_attrlen >> 2);
   2101	buf = kmalloc(count << 2, GFP_KERNEL);
   2102	if (!buf)
   2103		return nfserr_jukebox;
   2104
   2105	p = buf;
   2106	status = nfsd4_encode_fattr_to_buf(&p, count, &cstate->current_fh,
   2107				    cstate->current_fh.fh_export,
   2108				    cstate->current_fh.fh_dentry,
   2109				    verify->ve_bmval,
   2110				    rqstp, 0);
   2111	/*
   2112	 * If nfsd4_encode_fattr() ran out of space, assume that's because
   2113	 * the attributes are longer (hence different) than those given:
   2114	 */
   2115	if (status == nfserr_resource)
   2116		status = nfserr_not_same;
   2117	if (status)
   2118		goto out_kfree;
   2119
   2120	/* skip bitmap */
   2121	p = buf + 1 + ntohl(buf[0]);
   2122	status = nfserr_not_same;
   2123	if (ntohl(*p++) != verify->ve_attrlen)
   2124		goto out_kfree;
   2125	if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
   2126		status = nfserr_same;
   2127
   2128out_kfree:
   2129	kfree(buf);
   2130	return status;
   2131}
   2132
   2133static __be32
   2134nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
   2135	      union nfsd4_op_u *u)
   2136{
   2137	__be32 status;
   2138
   2139	status = _nfsd4_verify(rqstp, cstate, &u->verify);
   2140	return status == nfserr_not_same ? nfs_ok : status;
   2141}
   2142
   2143static __be32
   2144nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
   2145	     union nfsd4_op_u *u)
   2146{
   2147	__be32 status;
   2148
   2149	status = _nfsd4_verify(rqstp, cstate, &u->nverify);
   2150	return status == nfserr_same ? nfs_ok : status;
   2151}
   2152
   2153#ifdef CONFIG_NFSD_PNFS
   2154static const struct nfsd4_layout_ops *
   2155nfsd4_layout_verify(struct svc_export *exp, unsigned int layout_type)
   2156{
   2157	if (!exp->ex_layout_types) {
   2158		dprintk("%s: export does not support pNFS\n", __func__);
   2159		return NULL;
   2160	}
   2161
   2162	if (layout_type >= LAYOUT_TYPE_MAX ||
   2163	    !(exp->ex_layout_types & (1 << layout_type))) {
   2164		dprintk("%s: layout type %d not supported\n",
   2165			__func__, layout_type);
   2166		return NULL;
   2167	}
   2168
   2169	return nfsd4_layout_ops[layout_type];
   2170}
   2171
   2172static __be32
   2173nfsd4_getdeviceinfo(struct svc_rqst *rqstp,
   2174		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
   2175{
   2176	struct nfsd4_getdeviceinfo *gdp = &u->getdeviceinfo;
   2177	const struct nfsd4_layout_ops *ops;
   2178	struct nfsd4_deviceid_map *map;
   2179	struct svc_export *exp;
   2180	__be32 nfserr;
   2181
   2182	dprintk("%s: layout_type %u dev_id [0x%llx:0x%x] maxcnt %u\n",
   2183	       __func__,
   2184	       gdp->gd_layout_type,
   2185	       gdp->gd_devid.fsid_idx, gdp->gd_devid.generation,
   2186	       gdp->gd_maxcount);
   2187
   2188	map = nfsd4_find_devid_map(gdp->gd_devid.fsid_idx);
   2189	if (!map) {
   2190		dprintk("%s: couldn't find device ID to export mapping!\n",
   2191			__func__);
   2192		return nfserr_noent;
   2193	}
   2194
   2195	exp = rqst_exp_find(rqstp, map->fsid_type, map->fsid);
   2196	if (IS_ERR(exp)) {
   2197		dprintk("%s: could not find device id\n", __func__);
   2198		return nfserr_noent;
   2199	}
   2200
   2201	nfserr = nfserr_layoutunavailable;
   2202	ops = nfsd4_layout_verify(exp, gdp->gd_layout_type);
   2203	if (!ops)
   2204		goto out;
   2205
   2206	nfserr = nfs_ok;
   2207	if (gdp->gd_maxcount != 0) {
   2208		nfserr = ops->proc_getdeviceinfo(exp->ex_path.mnt->mnt_sb,
   2209				rqstp, cstate->clp, gdp);
   2210	}
   2211
   2212	gdp->gd_notify_types &= ops->notify_types;
   2213out:
   2214	exp_put(exp);
   2215	return nfserr;
   2216}
   2217
   2218static void
   2219nfsd4_getdeviceinfo_release(union nfsd4_op_u *u)
   2220{
   2221	kfree(u->getdeviceinfo.gd_device);
   2222}
   2223
   2224static __be32
   2225nfsd4_layoutget(struct svc_rqst *rqstp,
   2226		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
   2227{
   2228	struct nfsd4_layoutget *lgp = &u->layoutget;
   2229	struct svc_fh *current_fh = &cstate->current_fh;
   2230	const struct nfsd4_layout_ops *ops;
   2231	struct nfs4_layout_stateid *ls;
   2232	__be32 nfserr;
   2233	int accmode = NFSD_MAY_READ_IF_EXEC;
   2234
   2235	switch (lgp->lg_seg.iomode) {
   2236	case IOMODE_READ:
   2237		accmode |= NFSD_MAY_READ;
   2238		break;
   2239	case IOMODE_RW:
   2240		accmode |= NFSD_MAY_READ | NFSD_MAY_WRITE;
   2241		break;
   2242	default:
   2243		dprintk("%s: invalid iomode %d\n",
   2244			__func__, lgp->lg_seg.iomode);
   2245		nfserr = nfserr_badiomode;
   2246		goto out;
   2247	}
   2248
   2249	nfserr = fh_verify(rqstp, current_fh, 0, accmode);
   2250	if (nfserr)
   2251		goto out;
   2252
   2253	nfserr = nfserr_layoutunavailable;
   2254	ops = nfsd4_layout_verify(current_fh->fh_export, lgp->lg_layout_type);
   2255	if (!ops)
   2256		goto out;
   2257
   2258	/*
   2259	 * Verify minlength and range as per RFC5661:
   2260	 *  o  If loga_length is less than loga_minlength,
   2261	 *     the metadata server MUST return NFS4ERR_INVAL.
   2262	 *  o  If the sum of loga_offset and loga_minlength exceeds
   2263	 *     NFS4_UINT64_MAX, and loga_minlength is not
   2264	 *     NFS4_UINT64_MAX, the error NFS4ERR_INVAL MUST result.
   2265	 *  o  If the sum of loga_offset and loga_length exceeds
   2266	 *     NFS4_UINT64_MAX, and loga_length is not NFS4_UINT64_MAX,
   2267	 *     the error NFS4ERR_INVAL MUST result.
   2268	 */
   2269	nfserr = nfserr_inval;
   2270	if (lgp->lg_seg.length < lgp->lg_minlength ||
   2271	    (lgp->lg_minlength != NFS4_MAX_UINT64 &&
   2272	     lgp->lg_minlength > NFS4_MAX_UINT64 - lgp->lg_seg.offset) ||
   2273	    (lgp->lg_seg.length != NFS4_MAX_UINT64 &&
   2274	     lgp->lg_seg.length > NFS4_MAX_UINT64 - lgp->lg_seg.offset))
   2275		goto out;
   2276	if (lgp->lg_seg.length == 0)
   2277		goto out;
   2278
   2279	nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lgp->lg_sid,
   2280						true, lgp->lg_layout_type, &ls);
   2281	if (nfserr) {
   2282		trace_nfsd_layout_get_lookup_fail(&lgp->lg_sid);
   2283		goto out;
   2284	}
   2285
   2286	nfserr = nfserr_recallconflict;
   2287	if (atomic_read(&ls->ls_stid.sc_file->fi_lo_recalls))
   2288		goto out_put_stid;
   2289
   2290	nfserr = ops->proc_layoutget(d_inode(current_fh->fh_dentry),
   2291				     current_fh, lgp);
   2292	if (nfserr)
   2293		goto out_put_stid;
   2294
   2295	nfserr = nfsd4_insert_layout(lgp, ls);
   2296
   2297out_put_stid:
   2298	mutex_unlock(&ls->ls_mutex);
   2299	nfs4_put_stid(&ls->ls_stid);
   2300out:
   2301	return nfserr;
   2302}
   2303
   2304static void
   2305nfsd4_layoutget_release(union nfsd4_op_u *u)
   2306{
   2307	kfree(u->layoutget.lg_content);
   2308}
   2309
   2310static __be32
   2311nfsd4_layoutcommit(struct svc_rqst *rqstp,
   2312		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
   2313{
   2314	struct nfsd4_layoutcommit *lcp = &u->layoutcommit;
   2315	const struct nfsd4_layout_seg *seg = &lcp->lc_seg;
   2316	struct svc_fh *current_fh = &cstate->current_fh;
   2317	const struct nfsd4_layout_ops *ops;
   2318	loff_t new_size = lcp->lc_last_wr + 1;
   2319	struct inode *inode;
   2320	struct nfs4_layout_stateid *ls;
   2321	__be32 nfserr;
   2322
   2323	nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_WRITE);
   2324	if (nfserr)
   2325		goto out;
   2326
   2327	nfserr = nfserr_layoutunavailable;
   2328	ops = nfsd4_layout_verify(current_fh->fh_export, lcp->lc_layout_type);
   2329	if (!ops)
   2330		goto out;
   2331	inode = d_inode(current_fh->fh_dentry);
   2332
   2333	nfserr = nfserr_inval;
   2334	if (new_size <= seg->offset) {
   2335		dprintk("pnfsd: last write before layout segment\n");
   2336		goto out;
   2337	}
   2338	if (new_size > seg->offset + seg->length) {
   2339		dprintk("pnfsd: last write beyond layout segment\n");
   2340		goto out;
   2341	}
   2342	if (!lcp->lc_newoffset && new_size > i_size_read(inode)) {
   2343		dprintk("pnfsd: layoutcommit beyond EOF\n");
   2344		goto out;
   2345	}
   2346
   2347	nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lcp->lc_sid,
   2348						false, lcp->lc_layout_type,
   2349						&ls);
   2350	if (nfserr) {
   2351		trace_nfsd_layout_commit_lookup_fail(&lcp->lc_sid);
   2352		/* fixup error code as per RFC5661 */
   2353		if (nfserr == nfserr_bad_stateid)
   2354			nfserr = nfserr_badlayout;
   2355		goto out;
   2356	}
   2357
   2358	/* LAYOUTCOMMIT does not require any serialization */
   2359	mutex_unlock(&ls->ls_mutex);
   2360
   2361	if (new_size > i_size_read(inode)) {
   2362		lcp->lc_size_chg = 1;
   2363		lcp->lc_newsize = new_size;
   2364	} else {
   2365		lcp->lc_size_chg = 0;
   2366	}
   2367
   2368	nfserr = ops->proc_layoutcommit(inode, lcp);
   2369	nfs4_put_stid(&ls->ls_stid);
   2370out:
   2371	return nfserr;
   2372}
   2373
   2374static __be32
   2375nfsd4_layoutreturn(struct svc_rqst *rqstp,
   2376		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
   2377{
   2378	struct nfsd4_layoutreturn *lrp = &u->layoutreturn;
   2379	struct svc_fh *current_fh = &cstate->current_fh;
   2380	__be32 nfserr;
   2381
   2382	nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_NOP);
   2383	if (nfserr)
   2384		goto out;
   2385
   2386	nfserr = nfserr_layoutunavailable;
   2387	if (!nfsd4_layout_verify(current_fh->fh_export, lrp->lr_layout_type))
   2388		goto out;
   2389
   2390	switch (lrp->lr_seg.iomode) {
   2391	case IOMODE_READ:
   2392	case IOMODE_RW:
   2393	case IOMODE_ANY:
   2394		break;
   2395	default:
   2396		dprintk("%s: invalid iomode %d\n", __func__,
   2397			lrp->lr_seg.iomode);
   2398		nfserr = nfserr_inval;
   2399		goto out;
   2400	}
   2401
   2402	switch (lrp->lr_return_type) {
   2403	case RETURN_FILE:
   2404		nfserr = nfsd4_return_file_layouts(rqstp, cstate, lrp);
   2405		break;
   2406	case RETURN_FSID:
   2407	case RETURN_ALL:
   2408		nfserr = nfsd4_return_client_layouts(rqstp, cstate, lrp);
   2409		break;
   2410	default:
   2411		dprintk("%s: invalid return_type %d\n", __func__,
   2412			lrp->lr_return_type);
   2413		nfserr = nfserr_inval;
   2414		break;
   2415	}
   2416out:
   2417	return nfserr;
   2418}
   2419#endif /* CONFIG_NFSD_PNFS */
   2420
   2421static __be32
   2422nfsd4_getxattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
   2423	       union nfsd4_op_u *u)
   2424{
   2425	struct nfsd4_getxattr *getxattr = &u->getxattr;
   2426
   2427	return nfsd_getxattr(rqstp, &cstate->current_fh,
   2428			     getxattr->getxa_name, &getxattr->getxa_buf,
   2429			     &getxattr->getxa_len);
   2430}
   2431
   2432static __be32
   2433nfsd4_setxattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
   2434	   union nfsd4_op_u *u)
   2435{
   2436	struct nfsd4_setxattr *setxattr = &u->setxattr;
   2437	__be32 ret;
   2438
   2439	if (opens_in_grace(SVC_NET(rqstp)))
   2440		return nfserr_grace;
   2441
   2442	ret = nfsd_setxattr(rqstp, &cstate->current_fh, setxattr->setxa_name,
   2443			    setxattr->setxa_buf, setxattr->setxa_len,
   2444			    setxattr->setxa_flags);
   2445
   2446	if (!ret)
   2447		set_change_info(&setxattr->setxa_cinfo, &cstate->current_fh);
   2448
   2449	return ret;
   2450}
   2451
   2452static __be32
   2453nfsd4_listxattrs(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
   2454	   union nfsd4_op_u *u)
   2455{
   2456	/*
   2457	 * Get the entire list, then copy out only the user attributes
   2458	 * in the encode function.
   2459	 */
   2460	return nfsd_listxattr(rqstp, &cstate->current_fh,
   2461			     &u->listxattrs.lsxa_buf, &u->listxattrs.lsxa_len);
   2462}
   2463
   2464static __be32
   2465nfsd4_removexattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
   2466	   union nfsd4_op_u *u)
   2467{
   2468	struct nfsd4_removexattr *removexattr = &u->removexattr;
   2469	__be32 ret;
   2470
   2471	if (opens_in_grace(SVC_NET(rqstp)))
   2472		return nfserr_grace;
   2473
   2474	ret = nfsd_removexattr(rqstp, &cstate->current_fh,
   2475	    removexattr->rmxa_name);
   2476
   2477	if (!ret)
   2478		set_change_info(&removexattr->rmxa_cinfo, &cstate->current_fh);
   2479
   2480	return ret;
   2481}
   2482
   2483/*
   2484 * NULL call.
   2485 */
   2486static __be32
   2487nfsd4_proc_null(struct svc_rqst *rqstp)
   2488{
   2489	return rpc_success;
   2490}
   2491
   2492static inline void nfsd4_increment_op_stats(u32 opnum)
   2493{
   2494	if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
   2495		percpu_counter_inc(&nfsdstats.counter[NFSD_STATS_NFS4_OP(opnum)]);
   2496}
   2497
   2498static const struct nfsd4_operation nfsd4_ops[];
   2499
   2500static const char *nfsd4_op_name(unsigned opnum);
   2501
   2502/*
   2503 * Enforce NFSv4.1 COMPOUND ordering rules:
   2504 *
   2505 * Also note, enforced elsewhere:
   2506 *	- SEQUENCE other than as first op results in
   2507 *	  NFS4ERR_SEQUENCE_POS. (Enforced in nfsd4_sequence().)
   2508 *	- BIND_CONN_TO_SESSION must be the only op in its compound.
   2509 *	  (Enforced in nfsd4_bind_conn_to_session().)
   2510 *	- DESTROY_SESSION must be the final operation in a compound, if
   2511 *	  sessionid's in SEQUENCE and DESTROY_SESSION are the same.
   2512 *	  (Enforced in nfsd4_destroy_session().)
   2513 */
   2514static __be32 nfs41_check_op_ordering(struct nfsd4_compoundargs *args)
   2515{
   2516	struct nfsd4_op *first_op = &args->ops[0];
   2517
   2518	/* These ordering requirements don't apply to NFSv4.0: */
   2519	if (args->minorversion == 0)
   2520		return nfs_ok;
   2521	/* This is weird, but OK, not our problem: */
   2522	if (args->opcnt == 0)
   2523		return nfs_ok;
   2524	if (first_op->status == nfserr_op_illegal)
   2525		return nfs_ok;
   2526	if (!(nfsd4_ops[first_op->opnum].op_flags & ALLOWED_AS_FIRST_OP))
   2527		return nfserr_op_not_in_session;
   2528	if (first_op->opnum == OP_SEQUENCE)
   2529		return nfs_ok;
   2530	/*
   2531	 * So first_op is something allowed outside a session, like
   2532	 * EXCHANGE_ID; but then it has to be the only op in the
   2533	 * compound:
   2534	 */
   2535	if (args->opcnt != 1)
   2536		return nfserr_not_only_op;
   2537	return nfs_ok;
   2538}
   2539
   2540const struct nfsd4_operation *OPDESC(struct nfsd4_op *op)
   2541{
   2542	return &nfsd4_ops[op->opnum];
   2543}
   2544
   2545bool nfsd4_cache_this_op(struct nfsd4_op *op)
   2546{
   2547	if (op->opnum == OP_ILLEGAL)
   2548		return false;
   2549	return OPDESC(op)->op_flags & OP_CACHEME;
   2550}
   2551
   2552static bool need_wrongsec_check(struct svc_rqst *rqstp)
   2553{
   2554	struct nfsd4_compoundres *resp = rqstp->rq_resp;
   2555	struct nfsd4_compoundargs *argp = rqstp->rq_argp;
   2556	struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
   2557	struct nfsd4_op *next = &argp->ops[resp->opcnt];
   2558	const struct nfsd4_operation *thisd = OPDESC(this);
   2559	const struct nfsd4_operation *nextd;
   2560
   2561	/*
   2562	 * Most ops check wronsec on our own; only the putfh-like ops
   2563	 * have special rules.
   2564	 */
   2565	if (!(thisd->op_flags & OP_IS_PUTFH_LIKE))
   2566		return false;
   2567	/*
   2568	 * rfc 5661 2.6.3.1.1.6: don't bother erroring out a
   2569	 * put-filehandle operation if we're not going to use the
   2570	 * result:
   2571	 */
   2572	if (argp->opcnt == resp->opcnt)
   2573		return false;
   2574	if (next->opnum == OP_ILLEGAL)
   2575		return false;
   2576	nextd = OPDESC(next);
   2577	/*
   2578	 * Rest of 2.6.3.1.1: certain operations will return WRONGSEC
   2579	 * errors themselves as necessary; others should check for them
   2580	 * now:
   2581	 */
   2582	return !(nextd->op_flags & OP_HANDLES_WRONGSEC);
   2583}
   2584
   2585#ifdef CONFIG_NFSD_V4_2_INTER_SSC
   2586static void
   2587check_if_stalefh_allowed(struct nfsd4_compoundargs *args)
   2588{
   2589	struct nfsd4_op	*op, *current_op = NULL, *saved_op = NULL;
   2590	struct nfsd4_copy *copy;
   2591	struct nfsd4_putfh *putfh;
   2592	int i;
   2593
   2594	/* traverse all operation and if it's a COPY compound, mark the
   2595	 * source filehandle to skip verification
   2596	 */
   2597	for (i = 0; i < args->opcnt; i++) {
   2598		op = &args->ops[i];
   2599		if (op->opnum == OP_PUTFH)
   2600			current_op = op;
   2601		else if (op->opnum == OP_SAVEFH)
   2602			saved_op = current_op;
   2603		else if (op->opnum == OP_RESTOREFH)
   2604			current_op = saved_op;
   2605		else if (op->opnum == OP_COPY) {
   2606			copy = (struct nfsd4_copy *)&op->u;
   2607			if (!saved_op) {
   2608				op->status = nfserr_nofilehandle;
   2609				return;
   2610			}
   2611			putfh = (struct nfsd4_putfh *)&saved_op->u;
   2612			if (!copy->cp_intra)
   2613				putfh->no_verify = true;
   2614		}
   2615	}
   2616}
   2617#else
   2618static void
   2619check_if_stalefh_allowed(struct nfsd4_compoundargs *args)
   2620{
   2621}
   2622#endif
   2623
   2624/*
   2625 * COMPOUND call.
   2626 */
   2627static __be32
   2628nfsd4_proc_compound(struct svc_rqst *rqstp)
   2629{
   2630	struct nfsd4_compoundargs *args = rqstp->rq_argp;
   2631	struct nfsd4_compoundres *resp = rqstp->rq_resp;
   2632	struct nfsd4_op	*op;
   2633	struct nfsd4_compound_state *cstate = &resp->cstate;
   2634	struct svc_fh *current_fh = &cstate->current_fh;
   2635	struct svc_fh *save_fh = &cstate->save_fh;
   2636	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
   2637	__be32		status;
   2638
   2639	resp->xdr = &rqstp->rq_res_stream;
   2640	resp->statusp = resp->xdr->p;
   2641
   2642	/* reserve space for: NFS status code */
   2643	xdr_reserve_space(resp->xdr, XDR_UNIT);
   2644
   2645	/* reserve space for: taglen, tag, and opcnt */
   2646	xdr_reserve_space(resp->xdr, XDR_UNIT * 2 + args->taglen);
   2647	resp->taglen = args->taglen;
   2648	resp->tag = args->tag;
   2649	resp->rqstp = rqstp;
   2650	cstate->minorversion = args->minorversion;
   2651	fh_init(current_fh, NFS4_FHSIZE);
   2652	fh_init(save_fh, NFS4_FHSIZE);
   2653
   2654	/*
   2655	 * Don't use the deferral mechanism for NFSv4; compounds make it
   2656	 * too hard to avoid non-idempotency problems.
   2657	 */
   2658	__clear_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
   2659
   2660	/*
   2661	 * According to RFC3010, this takes precedence over all other errors.
   2662	 */
   2663	status = nfserr_minor_vers_mismatch;
   2664	if (nfsd_minorversion(nn, args->minorversion, NFSD_TEST) <= 0)
   2665		goto out;
   2666	status = nfserr_resource;
   2667	if (args->opcnt > NFSD_MAX_OPS_PER_COMPOUND)
   2668		goto out;
   2669
   2670	status = nfs41_check_op_ordering(args);
   2671	if (status) {
   2672		op = &args->ops[0];
   2673		op->status = status;
   2674		resp->opcnt = 1;
   2675		goto encode_op;
   2676	}
   2677	check_if_stalefh_allowed(args);
   2678
   2679	rqstp->rq_lease_breaker = (void **)&cstate->clp;
   2680
   2681	trace_nfsd_compound(rqstp, args->opcnt);
   2682	while (!status && resp->opcnt < args->opcnt) {
   2683		op = &args->ops[resp->opcnt++];
   2684
   2685		/*
   2686		 * The XDR decode routines may have pre-set op->status;
   2687		 * for example, if there is a miscellaneous XDR error
   2688		 * it will be set to nfserr_bad_xdr.
   2689		 */
   2690		if (op->status) {
   2691			if (op->opnum == OP_OPEN)
   2692				op->status = nfsd4_open_omfg(rqstp, cstate, op);
   2693			goto encode_op;
   2694		}
   2695		if (!current_fh->fh_dentry &&
   2696				!HAS_FH_FLAG(current_fh, NFSD4_FH_FOREIGN)) {
   2697			if (!(op->opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
   2698				op->status = nfserr_nofilehandle;
   2699				goto encode_op;
   2700			}
   2701		} else if (current_fh->fh_export &&
   2702			   current_fh->fh_export->ex_fslocs.migrated &&
   2703			  !(op->opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
   2704			op->status = nfserr_moved;
   2705			goto encode_op;
   2706		}
   2707
   2708		fh_clear_pre_post_attrs(current_fh);
   2709
   2710		/* If op is non-idempotent */
   2711		if (op->opdesc->op_flags & OP_MODIFIES_SOMETHING) {
   2712			/*
   2713			 * Don't execute this op if we couldn't encode a
   2714			 * succesful reply:
   2715			 */
   2716			u32 plen = op->opdesc->op_rsize_bop(rqstp, op);
   2717			/*
   2718			 * Plus if there's another operation, make sure
   2719			 * we'll have space to at least encode an error:
   2720			 */
   2721			if (resp->opcnt < args->opcnt)
   2722				plen += COMPOUND_ERR_SLACK_SPACE;
   2723			op->status = nfsd4_check_resp_size(resp, plen);
   2724		}
   2725
   2726		if (op->status)
   2727			goto encode_op;
   2728
   2729		if (op->opdesc->op_get_currentstateid)
   2730			op->opdesc->op_get_currentstateid(cstate, &op->u);
   2731		op->status = op->opdesc->op_func(rqstp, cstate, &op->u);
   2732
   2733		/* Only from SEQUENCE */
   2734		if (cstate->status == nfserr_replay_cache) {
   2735			dprintk("%s NFS4.1 replay from cache\n", __func__);
   2736			status = op->status;
   2737			goto out;
   2738		}
   2739		if (!op->status) {
   2740			if (op->opdesc->op_set_currentstateid)
   2741				op->opdesc->op_set_currentstateid(cstate, &op->u);
   2742
   2743			if (op->opdesc->op_flags & OP_CLEAR_STATEID)
   2744				clear_current_stateid(cstate);
   2745
   2746			if (current_fh->fh_export &&
   2747					need_wrongsec_check(rqstp))
   2748				op->status = check_nfsd_access(current_fh->fh_export, rqstp);
   2749		}
   2750encode_op:
   2751		if (op->status == nfserr_replay_me) {
   2752			op->replay = &cstate->replay_owner->so_replay;
   2753			nfsd4_encode_replay(resp->xdr, op);
   2754			status = op->status = op->replay->rp_status;
   2755		} else {
   2756			nfsd4_encode_operation(resp, op);
   2757			status = op->status;
   2758		}
   2759
   2760		trace_nfsd_compound_status(args->opcnt, resp->opcnt, status,
   2761					   nfsd4_op_name(op->opnum));
   2762
   2763		nfsd4_cstate_clear_replay(cstate);
   2764		nfsd4_increment_op_stats(op->opnum);
   2765	}
   2766
   2767	fh_put(current_fh);
   2768	fh_put(save_fh);
   2769	BUG_ON(cstate->replay_owner);
   2770out:
   2771	cstate->status = status;
   2772	/* Reset deferral mechanism for RPC deferrals */
   2773	__set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
   2774	return rpc_success;
   2775}
   2776
   2777#define op_encode_hdr_size		(2)
   2778#define op_encode_stateid_maxsz		(XDR_QUADLEN(NFS4_STATEID_SIZE))
   2779#define op_encode_verifier_maxsz	(XDR_QUADLEN(NFS4_VERIFIER_SIZE))
   2780#define op_encode_change_info_maxsz	(5)
   2781#define nfs4_fattr_bitmap_maxsz		(4)
   2782
   2783/* We'll fall back on returning no lockowner if run out of space: */
   2784#define op_encode_lockowner_maxsz	(0)
   2785#define op_encode_lock_denied_maxsz	(8 + op_encode_lockowner_maxsz)
   2786
   2787#define nfs4_owner_maxsz		(1 + XDR_QUADLEN(IDMAP_NAMESZ))
   2788
   2789#define op_encode_ace_maxsz		(3 + nfs4_owner_maxsz)
   2790#define op_encode_delegation_maxsz	(1 + op_encode_stateid_maxsz + 1 + \
   2791					 op_encode_ace_maxsz)
   2792
   2793#define op_encode_channel_attrs_maxsz	(6 + 1 + 1)
   2794
   2795static inline u32 nfsd4_only_status_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   2796{
   2797	return (op_encode_hdr_size) * sizeof(__be32);
   2798}
   2799
   2800static inline u32 nfsd4_status_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   2801{
   2802	return (op_encode_hdr_size + op_encode_stateid_maxsz)* sizeof(__be32);
   2803}
   2804
   2805static inline u32 nfsd4_access_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   2806{
   2807	/* ac_supported, ac_resp_access */
   2808	return (op_encode_hdr_size + 2)* sizeof(__be32);
   2809}
   2810
   2811static inline u32 nfsd4_commit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   2812{
   2813	return (op_encode_hdr_size + op_encode_verifier_maxsz) * sizeof(__be32);
   2814}
   2815
   2816static inline u32 nfsd4_create_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   2817{
   2818	return (op_encode_hdr_size + op_encode_change_info_maxsz
   2819		+ nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
   2820}
   2821
   2822/*
   2823 * Note since this is an idempotent operation we won't insist on failing
   2824 * the op prematurely if the estimate is too large.  We may turn off splice
   2825 * reads unnecessarily.
   2826 */
   2827static inline u32 nfsd4_getattr_rsize(struct svc_rqst *rqstp,
   2828				      struct nfsd4_op *op)
   2829{
   2830	u32 *bmap = op->u.getattr.ga_bmval;
   2831	u32 bmap0 = bmap[0], bmap1 = bmap[1], bmap2 = bmap[2];
   2832	u32 ret = 0;
   2833
   2834	if (bmap0 & FATTR4_WORD0_ACL)
   2835		return svc_max_payload(rqstp);
   2836	if (bmap0 & FATTR4_WORD0_FS_LOCATIONS)
   2837		return svc_max_payload(rqstp);
   2838
   2839	if (bmap1 & FATTR4_WORD1_OWNER) {
   2840		ret += IDMAP_NAMESZ + 4;
   2841		bmap1 &= ~FATTR4_WORD1_OWNER;
   2842	}
   2843	if (bmap1 & FATTR4_WORD1_OWNER_GROUP) {
   2844		ret += IDMAP_NAMESZ + 4;
   2845		bmap1 &= ~FATTR4_WORD1_OWNER_GROUP;
   2846	}
   2847	if (bmap0 & FATTR4_WORD0_FILEHANDLE) {
   2848		ret += NFS4_FHSIZE + 4;
   2849		bmap0 &= ~FATTR4_WORD0_FILEHANDLE;
   2850	}
   2851	if (bmap2 & FATTR4_WORD2_SECURITY_LABEL) {
   2852		ret += NFS4_MAXLABELLEN + 12;
   2853		bmap2 &= ~FATTR4_WORD2_SECURITY_LABEL;
   2854	}
   2855	/*
   2856	 * Largest of remaining attributes are 16 bytes (e.g.,
   2857	 * supported_attributes)
   2858	 */
   2859	ret += 16 * (hweight32(bmap0) + hweight32(bmap1) + hweight32(bmap2));
   2860	/* bitmask, length */
   2861	ret += 20;
   2862	return ret;
   2863}
   2864
   2865static inline u32 nfsd4_getfh_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   2866{
   2867	return (op_encode_hdr_size + 1) * sizeof(__be32) + NFS4_FHSIZE;
   2868}
   2869
   2870static inline u32 nfsd4_link_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   2871{
   2872	return (op_encode_hdr_size + op_encode_change_info_maxsz)
   2873		* sizeof(__be32);
   2874}
   2875
   2876static inline u32 nfsd4_lock_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   2877{
   2878	return (op_encode_hdr_size + op_encode_lock_denied_maxsz)
   2879		* sizeof(__be32);
   2880}
   2881
   2882static inline u32 nfsd4_open_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   2883{
   2884	return (op_encode_hdr_size + op_encode_stateid_maxsz
   2885		+ op_encode_change_info_maxsz + 1
   2886		+ nfs4_fattr_bitmap_maxsz
   2887		+ op_encode_delegation_maxsz) * sizeof(__be32);
   2888}
   2889
   2890static inline u32 nfsd4_read_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   2891{
   2892	u32 maxcount = 0, rlen = 0;
   2893
   2894	maxcount = svc_max_payload(rqstp);
   2895	rlen = min(op->u.read.rd_length, maxcount);
   2896
   2897	return (op_encode_hdr_size + 2 + XDR_QUADLEN(rlen)) * sizeof(__be32);
   2898}
   2899
   2900static inline u32 nfsd4_read_plus_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   2901{
   2902	u32 maxcount = svc_max_payload(rqstp);
   2903	u32 rlen = min(op->u.read.rd_length, maxcount);
   2904	/*
   2905	 * If we detect that the file changed during hole encoding, then we
   2906	 * recover by encoding the remaining reply as data. This means we need
   2907	 * to set aside enough room to encode two data segments.
   2908	 */
   2909	u32 seg_len = 2 * (1 + 2 + 1);
   2910
   2911	return (op_encode_hdr_size + 2 + seg_len + XDR_QUADLEN(rlen)) * sizeof(__be32);
   2912}
   2913
   2914static inline u32 nfsd4_readdir_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   2915{
   2916	u32 maxcount = 0, rlen = 0;
   2917
   2918	maxcount = svc_max_payload(rqstp);
   2919	rlen = min(op->u.readdir.rd_maxcount, maxcount);
   2920
   2921	return (op_encode_hdr_size + op_encode_verifier_maxsz +
   2922		XDR_QUADLEN(rlen)) * sizeof(__be32);
   2923}
   2924
   2925static inline u32 nfsd4_readlink_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   2926{
   2927	return (op_encode_hdr_size + 1) * sizeof(__be32) + PAGE_SIZE;
   2928}
   2929
   2930static inline u32 nfsd4_remove_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   2931{
   2932	return (op_encode_hdr_size + op_encode_change_info_maxsz)
   2933		* sizeof(__be32);
   2934}
   2935
   2936static inline u32 nfsd4_rename_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   2937{
   2938	return (op_encode_hdr_size + op_encode_change_info_maxsz
   2939		+ op_encode_change_info_maxsz) * sizeof(__be32);
   2940}
   2941
   2942static inline u32 nfsd4_sequence_rsize(struct svc_rqst *rqstp,
   2943				       struct nfsd4_op *op)
   2944{
   2945	return (op_encode_hdr_size
   2946		+ XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 5) * sizeof(__be32);
   2947}
   2948
   2949static inline u32 nfsd4_test_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   2950{
   2951	return (op_encode_hdr_size + 1 + op->u.test_stateid.ts_num_ids)
   2952		* sizeof(__be32);
   2953}
   2954
   2955static inline u32 nfsd4_setattr_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   2956{
   2957	return (op_encode_hdr_size + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
   2958}
   2959
   2960static inline u32 nfsd4_secinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   2961{
   2962	return (op_encode_hdr_size + RPC_AUTH_MAXFLAVOR *
   2963		(4 + XDR_QUADLEN(GSS_OID_MAX_LEN))) * sizeof(__be32);
   2964}
   2965
   2966static inline u32 nfsd4_setclientid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   2967{
   2968	return (op_encode_hdr_size + 2 + XDR_QUADLEN(NFS4_VERIFIER_SIZE)) *
   2969								sizeof(__be32);
   2970}
   2971
   2972static inline u32 nfsd4_write_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   2973{
   2974	return (op_encode_hdr_size + 2 + op_encode_verifier_maxsz) * sizeof(__be32);
   2975}
   2976
   2977static inline u32 nfsd4_exchange_id_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   2978{
   2979	return (op_encode_hdr_size + 2 + 1 + /* eir_clientid, eir_sequenceid */\
   2980		1 + 1 + /* eir_flags, spr_how */\
   2981		4 + /* spo_must_enforce & _allow with bitmap */\
   2982		2 + /*eir_server_owner.so_minor_id */\
   2983		/* eir_server_owner.so_major_id<> */\
   2984		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
   2985		/* eir_server_scope<> */\
   2986		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
   2987		1 + /* eir_server_impl_id array length */\
   2988		0 /* ignored eir_server_impl_id contents */) * sizeof(__be32);
   2989}
   2990
   2991static inline u32 nfsd4_bind_conn_to_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   2992{
   2993	return (op_encode_hdr_size + \
   2994		XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* bctsr_sessid */\
   2995		2 /* bctsr_dir, use_conn_in_rdma_mode */) * sizeof(__be32);
   2996}
   2997
   2998static inline u32 nfsd4_create_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   2999{
   3000	return (op_encode_hdr_size + \
   3001		XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* sessionid */\
   3002		2 + /* csr_sequence, csr_flags */\
   3003		op_encode_channel_attrs_maxsz + \
   3004		op_encode_channel_attrs_maxsz) * sizeof(__be32);
   3005}
   3006
   3007static inline u32 nfsd4_copy_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   3008{
   3009	return (op_encode_hdr_size +
   3010		1 /* wr_callback */ +
   3011		op_encode_stateid_maxsz /* wr_callback */ +
   3012		2 /* wr_count */ +
   3013		1 /* wr_committed */ +
   3014		op_encode_verifier_maxsz +
   3015		1 /* cr_consecutive */ +
   3016		1 /* cr_synchronous */) * sizeof(__be32);
   3017}
   3018
   3019static inline u32 nfsd4_offload_status_rsize(struct svc_rqst *rqstp,
   3020					     struct nfsd4_op *op)
   3021{
   3022	return (op_encode_hdr_size +
   3023		2 /* osr_count */ +
   3024		1 /* osr_complete<1> optional 0 for now */) * sizeof(__be32);
   3025}
   3026
   3027static inline u32 nfsd4_copy_notify_rsize(struct svc_rqst *rqstp,
   3028					struct nfsd4_op *op)
   3029{
   3030	return (op_encode_hdr_size +
   3031		3 /* cnr_lease_time */ +
   3032		1 /* We support one cnr_source_server */ +
   3033		1 /* cnr_stateid seq */ +
   3034		op_encode_stateid_maxsz /* cnr_stateid */ +
   3035		1 /* num cnr_source_server*/ +
   3036		1 /* nl4_type */ +
   3037		1 /* nl4 size */ +
   3038		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) /*nl4_loc + nl4_loc_sz */)
   3039		* sizeof(__be32);
   3040}
   3041
   3042#ifdef CONFIG_NFSD_PNFS
   3043static inline u32 nfsd4_getdeviceinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   3044{
   3045	u32 maxcount = 0, rlen = 0;
   3046
   3047	maxcount = svc_max_payload(rqstp);
   3048	rlen = min(op->u.getdeviceinfo.gd_maxcount, maxcount);
   3049
   3050	return (op_encode_hdr_size +
   3051		1 /* gd_layout_type*/ +
   3052		XDR_QUADLEN(rlen) +
   3053		2 /* gd_notify_types */) * sizeof(__be32);
   3054}
   3055
   3056/*
   3057 * At this stage we don't really know what layout driver will handle the request,
   3058 * so we need to define an arbitrary upper bound here.
   3059 */
   3060#define MAX_LAYOUT_SIZE		128
   3061static inline u32 nfsd4_layoutget_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   3062{
   3063	return (op_encode_hdr_size +
   3064		1 /* logr_return_on_close */ +
   3065		op_encode_stateid_maxsz +
   3066		1 /* nr of layouts */ +
   3067		MAX_LAYOUT_SIZE) * sizeof(__be32);
   3068}
   3069
   3070static inline u32 nfsd4_layoutcommit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   3071{
   3072	return (op_encode_hdr_size +
   3073		1 /* locr_newsize */ +
   3074		2 /* ns_size */) * sizeof(__be32);
   3075}
   3076
   3077static inline u32 nfsd4_layoutreturn_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   3078{
   3079	return (op_encode_hdr_size +
   3080		1 /* lrs_stateid */ +
   3081		op_encode_stateid_maxsz) * sizeof(__be32);
   3082}
   3083#endif /* CONFIG_NFSD_PNFS */
   3084
   3085
   3086static inline u32 nfsd4_seek_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
   3087{
   3088	return (op_encode_hdr_size + 3) * sizeof(__be32);
   3089}
   3090
   3091static inline u32 nfsd4_getxattr_rsize(struct svc_rqst *rqstp,
   3092				       struct nfsd4_op *op)
   3093{
   3094	u32 maxcount, rlen;
   3095
   3096	maxcount = svc_max_payload(rqstp);
   3097	rlen = min_t(u32, XATTR_SIZE_MAX, maxcount);
   3098
   3099	return (op_encode_hdr_size + 1 + XDR_QUADLEN(rlen)) * sizeof(__be32);
   3100}
   3101
   3102static inline u32 nfsd4_setxattr_rsize(struct svc_rqst *rqstp,
   3103				       struct nfsd4_op *op)
   3104{
   3105	return (op_encode_hdr_size + op_encode_change_info_maxsz)
   3106		* sizeof(__be32);
   3107}
   3108static inline u32 nfsd4_listxattrs_rsize(struct svc_rqst *rqstp,
   3109					 struct nfsd4_op *op)
   3110{
   3111	u32 maxcount, rlen;
   3112
   3113	maxcount = svc_max_payload(rqstp);
   3114	rlen = min(op->u.listxattrs.lsxa_maxcount, maxcount);
   3115
   3116	return (op_encode_hdr_size + 4 + XDR_QUADLEN(rlen)) * sizeof(__be32);
   3117}
   3118
   3119static inline u32 nfsd4_removexattr_rsize(struct svc_rqst *rqstp,
   3120					  struct nfsd4_op *op)
   3121{
   3122	return (op_encode_hdr_size + op_encode_change_info_maxsz)
   3123		* sizeof(__be32);
   3124}
   3125
   3126
   3127static const struct nfsd4_operation nfsd4_ops[] = {
   3128	[OP_ACCESS] = {
   3129		.op_func = nfsd4_access,
   3130		.op_name = "OP_ACCESS",
   3131		.op_rsize_bop = nfsd4_access_rsize,
   3132	},
   3133	[OP_CLOSE] = {
   3134		.op_func = nfsd4_close,
   3135		.op_flags = OP_MODIFIES_SOMETHING,
   3136		.op_name = "OP_CLOSE",
   3137		.op_rsize_bop = nfsd4_status_stateid_rsize,
   3138		.op_get_currentstateid = nfsd4_get_closestateid,
   3139		.op_set_currentstateid = nfsd4_set_closestateid,
   3140	},
   3141	[OP_COMMIT] = {
   3142		.op_func = nfsd4_commit,
   3143		.op_flags = OP_MODIFIES_SOMETHING,
   3144		.op_name = "OP_COMMIT",
   3145		.op_rsize_bop = nfsd4_commit_rsize,
   3146	},
   3147	[OP_CREATE] = {
   3148		.op_func = nfsd4_create,
   3149		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME | OP_CLEAR_STATEID,
   3150		.op_name = "OP_CREATE",
   3151		.op_rsize_bop = nfsd4_create_rsize,
   3152	},
   3153	[OP_DELEGRETURN] = {
   3154		.op_func = nfsd4_delegreturn,
   3155		.op_flags = OP_MODIFIES_SOMETHING,
   3156		.op_name = "OP_DELEGRETURN",
   3157		.op_rsize_bop = nfsd4_only_status_rsize,
   3158		.op_get_currentstateid = nfsd4_get_delegreturnstateid,
   3159	},
   3160	[OP_GETATTR] = {
   3161		.op_func = nfsd4_getattr,
   3162		.op_flags = ALLOWED_ON_ABSENT_FS,
   3163		.op_rsize_bop = nfsd4_getattr_rsize,
   3164		.op_name = "OP_GETATTR",
   3165	},
   3166	[OP_GETFH] = {
   3167		.op_func = nfsd4_getfh,
   3168		.op_name = "OP_GETFH",
   3169		.op_rsize_bop = nfsd4_getfh_rsize,
   3170	},
   3171	[OP_LINK] = {
   3172		.op_func = nfsd4_link,
   3173		.op_flags = ALLOWED_ON_ABSENT_FS | OP_MODIFIES_SOMETHING
   3174				| OP_CACHEME,
   3175		.op_name = "OP_LINK",
   3176		.op_rsize_bop = nfsd4_link_rsize,
   3177	},
   3178	[OP_LOCK] = {
   3179		.op_func = nfsd4_lock,
   3180		.op_flags = OP_MODIFIES_SOMETHING |
   3181				OP_NONTRIVIAL_ERROR_ENCODE,
   3182		.op_name = "OP_LOCK",
   3183		.op_rsize_bop = nfsd4_lock_rsize,
   3184		.op_set_currentstateid = nfsd4_set_lockstateid,
   3185	},
   3186	[OP_LOCKT] = {
   3187		.op_func = nfsd4_lockt,
   3188		.op_flags = OP_NONTRIVIAL_ERROR_ENCODE,
   3189		.op_name = "OP_LOCKT",
   3190		.op_rsize_bop = nfsd4_lock_rsize,
   3191	},
   3192	[OP_LOCKU] = {
   3193		.op_func = nfsd4_locku,
   3194		.op_flags = OP_MODIFIES_SOMETHING,
   3195		.op_name = "OP_LOCKU",
   3196		.op_rsize_bop = nfsd4_status_stateid_rsize,
   3197		.op_get_currentstateid = nfsd4_get_lockustateid,
   3198	},
   3199	[OP_LOOKUP] = {
   3200		.op_func = nfsd4_lookup,
   3201		.op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
   3202		.op_name = "OP_LOOKUP",
   3203		.op_rsize_bop = nfsd4_only_status_rsize,
   3204	},
   3205	[OP_LOOKUPP] = {
   3206		.op_func = nfsd4_lookupp,
   3207		.op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
   3208		.op_name = "OP_LOOKUPP",
   3209		.op_rsize_bop = nfsd4_only_status_rsize,
   3210	},
   3211	[OP_NVERIFY] = {
   3212		.op_func = nfsd4_nverify,
   3213		.op_name = "OP_NVERIFY",
   3214		.op_rsize_bop = nfsd4_only_status_rsize,
   3215	},
   3216	[OP_OPEN] = {
   3217		.op_func = nfsd4_open,
   3218		.op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
   3219		.op_name = "OP_OPEN",
   3220		.op_rsize_bop = nfsd4_open_rsize,
   3221		.op_set_currentstateid = nfsd4_set_openstateid,
   3222	},
   3223	[OP_OPEN_CONFIRM] = {
   3224		.op_func = nfsd4_open_confirm,
   3225		.op_flags = OP_MODIFIES_SOMETHING,
   3226		.op_name = "OP_OPEN_CONFIRM",
   3227		.op_rsize_bop = nfsd4_status_stateid_rsize,
   3228	},
   3229	[OP_OPEN_DOWNGRADE] = {
   3230		.op_func = nfsd4_open_downgrade,
   3231		.op_flags = OP_MODIFIES_SOMETHING,
   3232		.op_name = "OP_OPEN_DOWNGRADE",
   3233		.op_rsize_bop = nfsd4_status_stateid_rsize,
   3234		.op_get_currentstateid = nfsd4_get_opendowngradestateid,
   3235		.op_set_currentstateid = nfsd4_set_opendowngradestateid,
   3236	},
   3237	[OP_PUTFH] = {
   3238		.op_func = nfsd4_putfh,
   3239		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
   3240				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
   3241		.op_name = "OP_PUTFH",
   3242		.op_rsize_bop = nfsd4_only_status_rsize,
   3243	},
   3244	[OP_PUTPUBFH] = {
   3245		.op_func = nfsd4_putrootfh,
   3246		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
   3247				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
   3248		.op_name = "OP_PUTPUBFH",
   3249		.op_rsize_bop = nfsd4_only_status_rsize,
   3250	},
   3251	[OP_PUTROOTFH] = {
   3252		.op_func = nfsd4_putrootfh,
   3253		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
   3254				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
   3255		.op_name = "OP_PUTROOTFH",
   3256		.op_rsize_bop = nfsd4_only_status_rsize,
   3257	},
   3258	[OP_READ] = {
   3259		.op_func = nfsd4_read,
   3260		.op_release = nfsd4_read_release,
   3261		.op_name = "OP_READ",
   3262		.op_rsize_bop = nfsd4_read_rsize,
   3263		.op_get_currentstateid = nfsd4_get_readstateid,
   3264	},
   3265	[OP_READDIR] = {
   3266		.op_func = nfsd4_readdir,
   3267		.op_name = "OP_READDIR",
   3268		.op_rsize_bop = nfsd4_readdir_rsize,
   3269	},
   3270	[OP_READLINK] = {
   3271		.op_func = nfsd4_readlink,
   3272		.op_name = "OP_READLINK",
   3273		.op_rsize_bop = nfsd4_readlink_rsize,
   3274	},
   3275	[OP_REMOVE] = {
   3276		.op_func = nfsd4_remove,
   3277		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
   3278		.op_name = "OP_REMOVE",
   3279		.op_rsize_bop = nfsd4_remove_rsize,
   3280	},
   3281	[OP_RENAME] = {
   3282		.op_func = nfsd4_rename,
   3283		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
   3284		.op_name = "OP_RENAME",
   3285		.op_rsize_bop = nfsd4_rename_rsize,
   3286	},
   3287	[OP_RENEW] = {
   3288		.op_func = nfsd4_renew,
   3289		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
   3290				| OP_MODIFIES_SOMETHING,
   3291		.op_name = "OP_RENEW",
   3292		.op_rsize_bop = nfsd4_only_status_rsize,
   3293
   3294	},
   3295	[OP_RESTOREFH] = {
   3296		.op_func = nfsd4_restorefh,
   3297		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
   3298				| OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING,
   3299		.op_name = "OP_RESTOREFH",
   3300		.op_rsize_bop = nfsd4_only_status_rsize,
   3301	},
   3302	[OP_SAVEFH] = {
   3303		.op_func = nfsd4_savefh,
   3304		.op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
   3305		.op_name = "OP_SAVEFH",
   3306		.op_rsize_bop = nfsd4_only_status_rsize,
   3307	},
   3308	[OP_SECINFO] = {
   3309		.op_func = nfsd4_secinfo,
   3310		.op_release = nfsd4_secinfo_release,
   3311		.op_flags = OP_HANDLES_WRONGSEC,
   3312		.op_name = "OP_SECINFO",
   3313		.op_rsize_bop = nfsd4_secinfo_rsize,
   3314	},
   3315	[OP_SETATTR] = {
   3316		.op_func = nfsd4_setattr,
   3317		.op_name = "OP_SETATTR",
   3318		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME
   3319				| OP_NONTRIVIAL_ERROR_ENCODE,
   3320		.op_rsize_bop = nfsd4_setattr_rsize,
   3321		.op_get_currentstateid = nfsd4_get_setattrstateid,
   3322	},
   3323	[OP_SETCLIENTID] = {
   3324		.op_func = nfsd4_setclientid,
   3325		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
   3326				| OP_MODIFIES_SOMETHING | OP_CACHEME
   3327				| OP_NONTRIVIAL_ERROR_ENCODE,
   3328		.op_name = "OP_SETCLIENTID",
   3329		.op_rsize_bop = nfsd4_setclientid_rsize,
   3330	},
   3331	[OP_SETCLIENTID_CONFIRM] = {
   3332		.op_func = nfsd4_setclientid_confirm,
   3333		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
   3334				| OP_MODIFIES_SOMETHING | OP_CACHEME,
   3335		.op_name = "OP_SETCLIENTID_CONFIRM",
   3336		.op_rsize_bop = nfsd4_only_status_rsize,
   3337	},
   3338	[OP_VERIFY] = {
   3339		.op_func = nfsd4_verify,
   3340		.op_name = "OP_VERIFY",
   3341		.op_rsize_bop = nfsd4_only_status_rsize,
   3342	},
   3343	[OP_WRITE] = {
   3344		.op_func = nfsd4_write,
   3345		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
   3346		.op_name = "OP_WRITE",
   3347		.op_rsize_bop = nfsd4_write_rsize,
   3348		.op_get_currentstateid = nfsd4_get_writestateid,
   3349	},
   3350	[OP_RELEASE_LOCKOWNER] = {
   3351		.op_func = nfsd4_release_lockowner,
   3352		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
   3353				| OP_MODIFIES_SOMETHING,
   3354		.op_name = "OP_RELEASE_LOCKOWNER",
   3355		.op_rsize_bop = nfsd4_only_status_rsize,
   3356	},
   3357
   3358	/* NFSv4.1 operations */
   3359	[OP_EXCHANGE_ID] = {
   3360		.op_func = nfsd4_exchange_id,
   3361		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
   3362				| OP_MODIFIES_SOMETHING,
   3363		.op_name = "OP_EXCHANGE_ID",
   3364		.op_rsize_bop = nfsd4_exchange_id_rsize,
   3365	},
   3366	[OP_BACKCHANNEL_CTL] = {
   3367		.op_func = nfsd4_backchannel_ctl,
   3368		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
   3369		.op_name = "OP_BACKCHANNEL_CTL",
   3370		.op_rsize_bop = nfsd4_only_status_rsize,
   3371	},
   3372	[OP_BIND_CONN_TO_SESSION] = {
   3373		.op_func = nfsd4_bind_conn_to_session,
   3374		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
   3375				| OP_MODIFIES_SOMETHING,
   3376		.op_name = "OP_BIND_CONN_TO_SESSION",
   3377		.op_rsize_bop = nfsd4_bind_conn_to_session_rsize,
   3378	},
   3379	[OP_CREATE_SESSION] = {
   3380		.op_func = nfsd4_create_session,
   3381		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
   3382				| OP_MODIFIES_SOMETHING,
   3383		.op_name = "OP_CREATE_SESSION",
   3384		.op_rsize_bop = nfsd4_create_session_rsize,
   3385	},
   3386	[OP_DESTROY_SESSION] = {
   3387		.op_func = nfsd4_destroy_session,
   3388		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
   3389				| OP_MODIFIES_SOMETHING,
   3390		.op_name = "OP_DESTROY_SESSION",
   3391		.op_rsize_bop = nfsd4_only_status_rsize,
   3392	},
   3393	[OP_SEQUENCE] = {
   3394		.op_func = nfsd4_sequence,
   3395		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
   3396		.op_name = "OP_SEQUENCE",
   3397		.op_rsize_bop = nfsd4_sequence_rsize,
   3398	},
   3399	[OP_DESTROY_CLIENTID] = {
   3400		.op_func = nfsd4_destroy_clientid,
   3401		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
   3402				| OP_MODIFIES_SOMETHING,
   3403		.op_name = "OP_DESTROY_CLIENTID",
   3404		.op_rsize_bop = nfsd4_only_status_rsize,
   3405	},
   3406	[OP_RECLAIM_COMPLETE] = {
   3407		.op_func = nfsd4_reclaim_complete,
   3408		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
   3409		.op_name = "OP_RECLAIM_COMPLETE",
   3410		.op_rsize_bop = nfsd4_only_status_rsize,
   3411	},
   3412	[OP_SECINFO_NO_NAME] = {
   3413		.op_func = nfsd4_secinfo_no_name,
   3414		.op_release = nfsd4_secinfo_no_name_release,
   3415		.op_flags = OP_HANDLES_WRONGSEC,
   3416		.op_name = "OP_SECINFO_NO_NAME",
   3417		.op_rsize_bop = nfsd4_secinfo_rsize,
   3418	},
   3419	[OP_TEST_STATEID] = {
   3420		.op_func = nfsd4_test_stateid,
   3421		.op_flags = ALLOWED_WITHOUT_FH,
   3422		.op_name = "OP_TEST_STATEID",
   3423		.op_rsize_bop = nfsd4_test_stateid_rsize,
   3424	},
   3425	[OP_FREE_STATEID] = {
   3426		.op_func = nfsd4_free_stateid,
   3427		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
   3428		.op_name = "OP_FREE_STATEID",
   3429		.op_get_currentstateid = nfsd4_get_freestateid,
   3430		.op_rsize_bop = nfsd4_only_status_rsize,
   3431	},
   3432#ifdef CONFIG_NFSD_PNFS
   3433	[OP_GETDEVICEINFO] = {
   3434		.op_func = nfsd4_getdeviceinfo,
   3435		.op_release = nfsd4_getdeviceinfo_release,
   3436		.op_flags = ALLOWED_WITHOUT_FH,
   3437		.op_name = "OP_GETDEVICEINFO",
   3438		.op_rsize_bop = nfsd4_getdeviceinfo_rsize,
   3439	},
   3440	[OP_LAYOUTGET] = {
   3441		.op_func = nfsd4_layoutget,
   3442		.op_release = nfsd4_layoutget_release,
   3443		.op_flags = OP_MODIFIES_SOMETHING,
   3444		.op_name = "OP_LAYOUTGET",
   3445		.op_rsize_bop = nfsd4_layoutget_rsize,
   3446	},
   3447	[OP_LAYOUTCOMMIT] = {
   3448		.op_func = nfsd4_layoutcommit,
   3449		.op_flags = OP_MODIFIES_SOMETHING,
   3450		.op_name = "OP_LAYOUTCOMMIT",
   3451		.op_rsize_bop = nfsd4_layoutcommit_rsize,
   3452	},
   3453	[OP_LAYOUTRETURN] = {
   3454		.op_func = nfsd4_layoutreturn,
   3455		.op_flags = OP_MODIFIES_SOMETHING,
   3456		.op_name = "OP_LAYOUTRETURN",
   3457		.op_rsize_bop = nfsd4_layoutreturn_rsize,
   3458	},
   3459#endif /* CONFIG_NFSD_PNFS */
   3460
   3461	/* NFSv4.2 operations */
   3462	[OP_ALLOCATE] = {
   3463		.op_func = nfsd4_allocate,
   3464		.op_flags = OP_MODIFIES_SOMETHING,
   3465		.op_name = "OP_ALLOCATE",
   3466		.op_rsize_bop = nfsd4_only_status_rsize,
   3467	},
   3468	[OP_DEALLOCATE] = {
   3469		.op_func = nfsd4_deallocate,
   3470		.op_flags = OP_MODIFIES_SOMETHING,
   3471		.op_name = "OP_DEALLOCATE",
   3472		.op_rsize_bop = nfsd4_only_status_rsize,
   3473	},
   3474	[OP_CLONE] = {
   3475		.op_func = nfsd4_clone,
   3476		.op_flags = OP_MODIFIES_SOMETHING,
   3477		.op_name = "OP_CLONE",
   3478		.op_rsize_bop = nfsd4_only_status_rsize,
   3479	},
   3480	[OP_COPY] = {
   3481		.op_func = nfsd4_copy,
   3482		.op_flags = OP_MODIFIES_SOMETHING,
   3483		.op_name = "OP_COPY",
   3484		.op_rsize_bop = nfsd4_copy_rsize,
   3485	},
   3486	[OP_READ_PLUS] = {
   3487		.op_func = nfsd4_read,
   3488		.op_release = nfsd4_read_release,
   3489		.op_name = "OP_READ_PLUS",
   3490		.op_rsize_bop = nfsd4_read_plus_rsize,
   3491		.op_get_currentstateid = nfsd4_get_readstateid,
   3492	},
   3493	[OP_SEEK] = {
   3494		.op_func = nfsd4_seek,
   3495		.op_name = "OP_SEEK",
   3496		.op_rsize_bop = nfsd4_seek_rsize,
   3497	},
   3498	[OP_OFFLOAD_STATUS] = {
   3499		.op_func = nfsd4_offload_status,
   3500		.op_name = "OP_OFFLOAD_STATUS",
   3501		.op_rsize_bop = nfsd4_offload_status_rsize,
   3502	},
   3503	[OP_OFFLOAD_CANCEL] = {
   3504		.op_func = nfsd4_offload_cancel,
   3505		.op_flags = OP_MODIFIES_SOMETHING,
   3506		.op_name = "OP_OFFLOAD_CANCEL",
   3507		.op_rsize_bop = nfsd4_only_status_rsize,
   3508	},
   3509	[OP_COPY_NOTIFY] = {
   3510		.op_func = nfsd4_copy_notify,
   3511		.op_flags = OP_MODIFIES_SOMETHING,
   3512		.op_name = "OP_COPY_NOTIFY",
   3513		.op_rsize_bop = nfsd4_copy_notify_rsize,
   3514	},
   3515	[OP_GETXATTR] = {
   3516		.op_func = nfsd4_getxattr,
   3517		.op_name = "OP_GETXATTR",
   3518		.op_rsize_bop = nfsd4_getxattr_rsize,
   3519	},
   3520	[OP_SETXATTR] = {
   3521		.op_func = nfsd4_setxattr,
   3522		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
   3523		.op_name = "OP_SETXATTR",
   3524		.op_rsize_bop = nfsd4_setxattr_rsize,
   3525	},
   3526	[OP_LISTXATTRS] = {
   3527		.op_func = nfsd4_listxattrs,
   3528		.op_name = "OP_LISTXATTRS",
   3529		.op_rsize_bop = nfsd4_listxattrs_rsize,
   3530	},
   3531	[OP_REMOVEXATTR] = {
   3532		.op_func = nfsd4_removexattr,
   3533		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
   3534		.op_name = "OP_REMOVEXATTR",
   3535		.op_rsize_bop = nfsd4_removexattr_rsize,
   3536	},
   3537};
   3538
   3539/**
   3540 * nfsd4_spo_must_allow - Determine if the compound op contains an
   3541 * operation that is allowed to be sent with machine credentials
   3542 *
   3543 * @rqstp: a pointer to the struct svc_rqst
   3544 *
   3545 * Checks to see if the compound contains a spo_must_allow op
   3546 * and confirms that it was sent with the proper machine creds.
   3547 */
   3548
   3549bool nfsd4_spo_must_allow(struct svc_rqst *rqstp)
   3550{
   3551	struct nfsd4_compoundres *resp = rqstp->rq_resp;
   3552	struct nfsd4_compoundargs *argp = rqstp->rq_argp;
   3553	struct nfsd4_op *this;
   3554	struct nfsd4_compound_state *cstate = &resp->cstate;
   3555	struct nfs4_op_map *allow = &cstate->clp->cl_spo_must_allow;
   3556	u32 opiter;
   3557
   3558	if (!cstate->minorversion)
   3559		return false;
   3560
   3561	if (cstate->spo_must_allowed)
   3562		return true;
   3563
   3564	opiter = resp->opcnt;
   3565	while (opiter < argp->opcnt) {
   3566		this = &argp->ops[opiter++];
   3567		if (test_bit(this->opnum, allow->u.longs) &&
   3568			cstate->clp->cl_mach_cred &&
   3569			nfsd4_mach_creds_match(cstate->clp, rqstp)) {
   3570			cstate->spo_must_allowed = true;
   3571			return true;
   3572		}
   3573	}
   3574	cstate->spo_must_allowed = false;
   3575	return false;
   3576}
   3577
   3578int nfsd4_max_reply(struct svc_rqst *rqstp, struct nfsd4_op *op)
   3579{
   3580	if (op->opnum == OP_ILLEGAL || op->status == nfserr_notsupp)
   3581		return op_encode_hdr_size * sizeof(__be32);
   3582
   3583	BUG_ON(OPDESC(op)->op_rsize_bop == NULL);
   3584	return OPDESC(op)->op_rsize_bop(rqstp, op);
   3585}
   3586
   3587void warn_on_nonidempotent_op(struct nfsd4_op *op)
   3588{
   3589	if (OPDESC(op)->op_flags & OP_MODIFIES_SOMETHING) {
   3590		pr_err("unable to encode reply to nonidempotent op %u (%s)\n",
   3591			op->opnum, nfsd4_op_name(op->opnum));
   3592		WARN_ON_ONCE(1);
   3593	}
   3594}
   3595
   3596static const char *nfsd4_op_name(unsigned opnum)
   3597{
   3598	if (opnum < ARRAY_SIZE(nfsd4_ops))
   3599		return nfsd4_ops[opnum].op_name;
   3600	return "unknown_operation";
   3601}
   3602
   3603static const struct svc_procedure nfsd_procedures4[2] = {
   3604	[NFSPROC4_NULL] = {
   3605		.pc_func = nfsd4_proc_null,
   3606		.pc_decode = nfssvc_decode_voidarg,
   3607		.pc_encode = nfssvc_encode_voidres,
   3608		.pc_argsize = sizeof(struct nfsd_voidargs),
   3609		.pc_ressize = sizeof(struct nfsd_voidres),
   3610		.pc_cachetype = RC_NOCACHE,
   3611		.pc_xdrressize = 1,
   3612		.pc_name = "NULL",
   3613	},
   3614	[NFSPROC4_COMPOUND] = {
   3615		.pc_func = nfsd4_proc_compound,
   3616		.pc_decode = nfs4svc_decode_compoundargs,
   3617		.pc_encode = nfs4svc_encode_compoundres,
   3618		.pc_argsize = sizeof(struct nfsd4_compoundargs),
   3619		.pc_ressize = sizeof(struct nfsd4_compoundres),
   3620		.pc_release = nfsd4_release_compoundargs,
   3621		.pc_cachetype = RC_NOCACHE,
   3622		.pc_xdrressize = NFSD_BUFSIZE/4,
   3623		.pc_name = "COMPOUND",
   3624	},
   3625};
   3626
   3627static unsigned int nfsd_count3[ARRAY_SIZE(nfsd_procedures4)];
   3628const struct svc_version nfsd_version4 = {
   3629	.vs_vers		= 4,
   3630	.vs_nproc		= 2,
   3631	.vs_proc		= nfsd_procedures4,
   3632	.vs_count		= nfsd_count3,
   3633	.vs_dispatch		= nfsd_dispatch,
   3634	.vs_xdrsize		= NFS4_SVC_XDRSIZE,
   3635	.vs_rpcb_optnl		= true,
   3636	.vs_need_cong_ctrl	= true,
   3637};