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