File Index
Files and directories
Sign in
FILE

nfs4proc.c

File preview with syntax highlighting.

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