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