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