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