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