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