xref: /linux/fs/nfsd/nfs4proc.c (revision 0d885f7e7978f81f66e63ce325a2d9de4b41e654)
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 	trace_nfsd_vfs_getattr(rqstp, &cstate->current_fh);
880 
881 	status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
882 	if (status)
883 		return status;
884 
885 	if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
886 		return nfserr_inval;
887 
888 	getattr->ga_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
889 	getattr->ga_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
890 	getattr->ga_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
891 
892 	getattr->ga_fhp = &cstate->current_fh;
893 	return nfs_ok;
894 }
895 
896 static __be32
897 nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
898 	   union nfsd4_op_u *u)
899 {
900 	struct nfsd4_link *link = &u->link;
901 	__be32 status;
902 
903 	status = nfsd_link(rqstp, &cstate->current_fh,
904 			   link->li_name, link->li_namelen, &cstate->save_fh);
905 	if (!status)
906 		set_change_info(&link->li_cinfo, &cstate->current_fh);
907 	return status;
908 }
909 
910 static __be32 nfsd4_do_lookupp(struct svc_rqst *rqstp, struct svc_fh *fh)
911 {
912 	struct svc_fh tmp_fh;
913 	__be32 ret;
914 
915 	fh_init(&tmp_fh, NFS4_FHSIZE);
916 	ret = exp_pseudoroot(rqstp, &tmp_fh);
917 	if (ret)
918 		return ret;
919 	if (tmp_fh.fh_dentry == fh->fh_dentry) {
920 		fh_put(&tmp_fh);
921 		return nfserr_noent;
922 	}
923 	fh_put(&tmp_fh);
924 	return nfsd_lookup(rqstp, fh, "..", 2, fh);
925 }
926 
927 static __be32
928 nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
929 	      union nfsd4_op_u *u)
930 {
931 	return nfsd4_do_lookupp(rqstp, &cstate->current_fh);
932 }
933 
934 static __be32
935 nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
936 	     union nfsd4_op_u *u)
937 {
938 	return nfsd_lookup(rqstp, &cstate->current_fh,
939 			   u->lookup.lo_name, u->lookup.lo_len,
940 			   &cstate->current_fh);
941 }
942 
943 static __be32
944 nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
945 	   union nfsd4_op_u *u)
946 {
947 	struct nfsd4_read *read = &u->read;
948 	__be32 status;
949 
950 	read->rd_nf = NULL;
951 
952 	trace_nfsd_read_start(rqstp, &cstate->current_fh,
953 			      read->rd_offset, read->rd_length);
954 
955 	read->rd_length = min_t(u32, read->rd_length, svc_max_payload(rqstp));
956 	if (read->rd_offset > (u64)OFFSET_MAX)
957 		read->rd_offset = (u64)OFFSET_MAX;
958 	if (read->rd_offset + read->rd_length > (u64)OFFSET_MAX)
959 		read->rd_length = (u64)OFFSET_MAX - read->rd_offset;
960 
961 	/*
962 	 * If we do a zero copy read, then a client will see read data
963 	 * that reflects the state of the file *after* performing the
964 	 * following compound.
965 	 *
966 	 * To ensure proper ordering, we therefore turn off zero copy if
967 	 * the client wants us to do more in this compound:
968 	 */
969 	if (!nfsd4_last_compound_op(rqstp)) {
970 		struct nfsd4_compoundargs *argp = rqstp->rq_argp;
971 
972 		argp->splice_ok = false;
973 	}
974 
975 	/* check stateid */
976 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
977 					&read->rd_stateid, RD_STATE,
978 					&read->rd_nf, NULL);
979 
980 	read->rd_rqstp = rqstp;
981 	read->rd_fhp = &cstate->current_fh;
982 	return status;
983 }
984 
985 
986 static void
987 nfsd4_read_release(union nfsd4_op_u *u)
988 {
989 	if (u->read.rd_nf)
990 		nfsd_file_put(u->read.rd_nf);
991 	trace_nfsd_read_done(u->read.rd_rqstp, u->read.rd_fhp,
992 			     u->read.rd_offset, u->read.rd_length);
993 }
994 
995 static __be32
996 nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
997 	      union nfsd4_op_u *u)
998 {
999 	struct nfsd4_readdir *readdir = &u->readdir;
1000 	u64 cookie = readdir->rd_cookie;
1001 	static const nfs4_verifier zeroverf;
1002 
1003 	trace_nfsd_vfs_readdir(rqstp, &cstate->current_fh,
1004 			       readdir->rd_maxcount, readdir->rd_cookie);
1005 
1006 	/* no need to check permission - this will be done in nfsd_readdir() */
1007 
1008 	if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
1009 		return nfserr_inval;
1010 
1011 	readdir->rd_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
1012 	readdir->rd_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
1013 	readdir->rd_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
1014 
1015 	if ((cookie == 1) || (cookie == 2) ||
1016 	    (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
1017 		return nfserr_bad_cookie;
1018 
1019 	readdir->rd_rqstp = rqstp;
1020 	readdir->rd_fhp = &cstate->current_fh;
1021 	return nfs_ok;
1022 }
1023 
1024 static __be32
1025 nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1026 	       union nfsd4_op_u *u)
1027 {
1028 	u->readlink.rl_rqstp = rqstp;
1029 	u->readlink.rl_fhp = &cstate->current_fh;
1030 	return nfs_ok;
1031 }
1032 
1033 static __be32
1034 nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1035 	     union nfsd4_op_u *u)
1036 {
1037 	struct nfsd4_remove *remove = &u->remove;
1038 	__be32 status;
1039 
1040 	if (opens_in_grace(SVC_NET(rqstp)))
1041 		return nfserr_grace;
1042 	status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
1043 			     remove->rm_name, remove->rm_namelen);
1044 	if (!status)
1045 		set_change_info(&remove->rm_cinfo, &cstate->current_fh);
1046 	return status;
1047 }
1048 
1049 static __be32
1050 nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1051 	     union nfsd4_op_u *u)
1052 {
1053 	struct nfsd4_rename *rename = &u->rename;
1054 	__be32 status;
1055 
1056 	if (opens_in_grace(SVC_NET(rqstp)))
1057 		return nfserr_grace;
1058 	status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname,
1059 			     rename->rn_snamelen, &cstate->current_fh,
1060 			     rename->rn_tname, rename->rn_tnamelen);
1061 	if (status)
1062 		return status;
1063 	set_change_info(&rename->rn_sinfo, &cstate->save_fh);
1064 	set_change_info(&rename->rn_tinfo, &cstate->current_fh);
1065 	return nfs_ok;
1066 }
1067 
1068 static __be32
1069 nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1070 	      union nfsd4_op_u *u)
1071 {
1072 	struct nfsd4_secinfo *secinfo = &u->secinfo;
1073 	struct svc_export *exp;
1074 	struct dentry *dentry;
1075 	__be32 err;
1076 
1077 	err = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_EXEC);
1078 	if (err)
1079 		return err;
1080 	err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
1081 				    secinfo->si_name, secinfo->si_namelen,
1082 				    &exp, &dentry);
1083 	if (err)
1084 		return err;
1085 	if (d_really_is_negative(dentry)) {
1086 		exp_put(exp);
1087 		err = nfserr_noent;
1088 	} else
1089 		secinfo->si_exp = exp;
1090 	dput(dentry);
1091 	if (cstate->minorversion)
1092 		/* See rfc 5661 section 2.6.3.1.1.8 */
1093 		fh_put(&cstate->current_fh);
1094 	return err;
1095 }
1096 
1097 static __be32
1098 nfsd4_secinfo_no_name(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1099 		union nfsd4_op_u *u)
1100 {
1101 	__be32 err;
1102 
1103 	switch (u->secinfo_no_name.sin_style) {
1104 	case NFS4_SECINFO_STYLE4_CURRENT_FH:
1105 		break;
1106 	case NFS4_SECINFO_STYLE4_PARENT:
1107 		err = nfsd4_do_lookupp(rqstp, &cstate->current_fh);
1108 		if (err)
1109 			return err;
1110 		break;
1111 	default:
1112 		return nfserr_inval;
1113 	}
1114 
1115 	u->secinfo_no_name.sin_exp = exp_get(cstate->current_fh.fh_export);
1116 	fh_put(&cstate->current_fh);
1117 	return nfs_ok;
1118 }
1119 
1120 static void
1121 nfsd4_secinfo_release(union nfsd4_op_u *u)
1122 {
1123 	if (u->secinfo.si_exp)
1124 		exp_put(u->secinfo.si_exp);
1125 }
1126 
1127 static void
1128 nfsd4_secinfo_no_name_release(union nfsd4_op_u *u)
1129 {
1130 	if (u->secinfo_no_name.sin_exp)
1131 		exp_put(u->secinfo_no_name.sin_exp);
1132 }
1133 
1134 static __be32
1135 nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1136 	      union nfsd4_op_u *u)
1137 {
1138 	struct nfsd4_setattr *setattr = &u->setattr;
1139 	struct nfsd_attrs attrs = {
1140 		.na_iattr	= &setattr->sa_iattr,
1141 		.na_seclabel	= &setattr->sa_label,
1142 	};
1143 	bool save_no_wcc, deleg_attrs;
1144 	struct nfs4_stid *st = NULL;
1145 	struct inode *inode;
1146 	__be32 status = nfs_ok;
1147 	int err;
1148 
1149 	deleg_attrs = setattr->sa_bmval[2] & (FATTR4_WORD2_TIME_DELEG_ACCESS |
1150 					      FATTR4_WORD2_TIME_DELEG_MODIFY);
1151 
1152 	if (deleg_attrs || (setattr->sa_iattr.ia_valid & ATTR_SIZE)) {
1153 		int flags = WR_STATE;
1154 
1155 		if (setattr->sa_bmval[2] & FATTR4_WORD2_TIME_DELEG_ACCESS)
1156 			flags |= RD_STATE;
1157 
1158 		status = nfs4_preprocess_stateid_op(rqstp, cstate,
1159 				&cstate->current_fh, &setattr->sa_stateid,
1160 				flags, NULL, &st);
1161 		if (status)
1162 			return status;
1163 	}
1164 
1165 	if (deleg_attrs) {
1166 		status = nfserr_bad_stateid;
1167 		if (st->sc_type & SC_TYPE_DELEG) {
1168 			struct nfs4_delegation *dp = delegstateid(st);
1169 
1170 			/* Only for *_ATTRS_DELEG flavors */
1171 			if (deleg_attrs_deleg(dp->dl_type))
1172 				status = nfs_ok;
1173 		}
1174 	}
1175 	if (st)
1176 		nfs4_put_stid(st);
1177 	if (status)
1178 		return status;
1179 
1180 	err = fh_want_write(&cstate->current_fh);
1181 	if (err)
1182 		return nfserrno(err);
1183 	status = nfs_ok;
1184 
1185 	status = check_attr_support(rqstp, cstate, setattr->sa_bmval,
1186 				    nfsd_attrmask);
1187 	if (status)
1188 		goto out;
1189 
1190 	inode = cstate->current_fh.fh_dentry->d_inode;
1191 	status = nfsd4_acl_to_attr(S_ISDIR(inode->i_mode) ? NF4DIR : NF4REG,
1192 				   setattr->sa_acl, &attrs);
1193 
1194 	if (status)
1195 		goto out;
1196 	save_no_wcc = cstate->current_fh.fh_no_wcc;
1197 	cstate->current_fh.fh_no_wcc = true;
1198 	status = nfsd_setattr(rqstp, &cstate->current_fh, &attrs, NULL);
1199 	cstate->current_fh.fh_no_wcc = save_no_wcc;
1200 	if (!status)
1201 		status = nfserrno(attrs.na_labelerr);
1202 	if (!status)
1203 		status = nfserrno(attrs.na_aclerr);
1204 out:
1205 	nfsd_attrs_free(&attrs);
1206 	fh_drop_write(&cstate->current_fh);
1207 	return status;
1208 }
1209 
1210 static __be32
1211 nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1212 	    union nfsd4_op_u *u)
1213 {
1214 	struct nfsd4_write *write = &u->write;
1215 	stateid_t *stateid = &write->wr_stateid;
1216 	struct nfsd_file *nf = NULL;
1217 	__be32 status = nfs_ok;
1218 	unsigned long cnt;
1219 	int nvecs;
1220 
1221 	if (write->wr_offset > (u64)OFFSET_MAX ||
1222 	    write->wr_offset + write->wr_buflen > (u64)OFFSET_MAX)
1223 		return nfserr_fbig;
1224 
1225 	cnt = write->wr_buflen;
1226 	trace_nfsd_write_start(rqstp, &cstate->current_fh,
1227 			       write->wr_offset, cnt);
1228 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1229 						stateid, WR_STATE, &nf, NULL);
1230 	if (status)
1231 		return status;
1232 
1233 	write->wr_how_written = write->wr_stable_how;
1234 
1235 	nvecs = svc_fill_write_vector(rqstp, &write->wr_payload);
1236 
1237 	status = nfsd_vfs_write(rqstp, &cstate->current_fh, nf,
1238 				write->wr_offset, rqstp->rq_vec, nvecs, &cnt,
1239 				write->wr_how_written,
1240 				(__be32 *)write->wr_verifier.data);
1241 	nfsd_file_put(nf);
1242 
1243 	write->wr_bytes_written = cnt;
1244 	trace_nfsd_write_done(rqstp, &cstate->current_fh,
1245 			      write->wr_offset, cnt);
1246 	return status;
1247 }
1248 
1249 static __be32
1250 nfsd4_verify_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1251 		  stateid_t *src_stateid, struct nfsd_file **src,
1252 		  stateid_t *dst_stateid, struct nfsd_file **dst)
1253 {
1254 	__be32 status;
1255 
1256 	if (!cstate->save_fh.fh_dentry)
1257 		return nfserr_nofilehandle;
1258 
1259 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->save_fh,
1260 					    src_stateid, RD_STATE, src, NULL);
1261 	if (status)
1262 		goto out;
1263 
1264 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1265 					    dst_stateid, WR_STATE, dst, NULL);
1266 	if (status)
1267 		goto out_put_src;
1268 
1269 	/* fix up for NFS-specific error code */
1270 	if (!S_ISREG(file_inode((*src)->nf_file)->i_mode) ||
1271 	    !S_ISREG(file_inode((*dst)->nf_file)->i_mode)) {
1272 		status = nfserr_wrong_type;
1273 		goto out_put_dst;
1274 	}
1275 
1276 out:
1277 	return status;
1278 out_put_dst:
1279 	nfsd_file_put(*dst);
1280 	*dst = NULL;
1281 out_put_src:
1282 	nfsd_file_put(*src);
1283 	*src = NULL;
1284 	goto out;
1285 }
1286 
1287 static __be32
1288 nfsd4_clone(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1289 		union nfsd4_op_u *u)
1290 {
1291 	struct nfsd4_clone *clone = &u->clone;
1292 	struct nfsd_file *src, *dst;
1293 	__be32 status;
1294 
1295 	status = nfsd4_verify_copy(rqstp, cstate, &clone->cl_src_stateid, &src,
1296 				   &clone->cl_dst_stateid, &dst);
1297 	if (status)
1298 		goto out;
1299 
1300 	status = nfsd4_clone_file_range(rqstp, src, clone->cl_src_pos,
1301 			dst, clone->cl_dst_pos, clone->cl_count,
1302 			EX_ISSYNC(cstate->current_fh.fh_export));
1303 
1304 	nfsd_file_put(dst);
1305 	nfsd_file_put(src);
1306 out:
1307 	return status;
1308 }
1309 
1310 /**
1311  * nfsd4_has_active_async_copies - Check for ongoing copy operations
1312  * @clp: Client to be checked
1313  *
1314  * NFSD maintains state for async COPY operations after they complete,
1315  * and this state remains in the nfs4_client's async_copies list.
1316  * Ongoing copies should block the destruction of the nfs4_client, but
1317  * completed copies should not.
1318  *
1319  * Return values:
1320  *   %true: At least one active async COPY is ongoing
1321  *   %false: No active async COPY operations were found
1322  */
1323 bool nfsd4_has_active_async_copies(struct nfs4_client *clp)
1324 {
1325 	struct nfsd4_copy *copy;
1326 	bool result = false;
1327 
1328 	spin_lock(&clp->async_lock);
1329 	list_for_each_entry(copy, &clp->async_copies, copies) {
1330 		if (!test_bit(NFSD4_COPY_F_COMPLETED, &copy->cp_flags) &&
1331 		    !test_bit(NFSD4_COPY_F_STOPPED, &copy->cp_flags)) {
1332 			result = true;
1333 			break;
1334 		}
1335 	}
1336 	spin_unlock(&clp->async_lock);
1337 	return result;
1338 }
1339 
1340 /**
1341  * nfsd4_async_copy_reaper - Purge completed copies
1342  * @nn: Network namespace with possible active copy information
1343  */
1344 void nfsd4_async_copy_reaper(struct nfsd_net *nn)
1345 {
1346 	struct nfs4_client *clp;
1347 	struct nfsd4_copy *copy;
1348 	LIST_HEAD(reaplist);
1349 
1350 	spin_lock(&nn->client_lock);
1351 	list_for_each_entry(clp, &nn->client_lru, cl_lru) {
1352 		struct list_head *pos, *next;
1353 
1354 		spin_lock(&clp->async_lock);
1355 		list_for_each_safe(pos, next, &clp->async_copies) {
1356 			copy = list_entry(pos, struct nfsd4_copy, copies);
1357 			if (test_bit(NFSD4_COPY_F_OFFLOAD_DONE, &copy->cp_flags)) {
1358 				if (--copy->cp_ttl) {
1359 					list_del_init(&copy->copies);
1360 					list_add(&copy->copies, &reaplist);
1361 				}
1362 			}
1363 		}
1364 		spin_unlock(&clp->async_lock);
1365 	}
1366 	spin_unlock(&nn->client_lock);
1367 
1368 	while (!list_empty(&reaplist)) {
1369 		copy = list_first_entry(&reaplist, struct nfsd4_copy, copies);
1370 		list_del_init(&copy->copies);
1371 		cleanup_async_copy(copy);
1372 	}
1373 }
1374 
1375 static void nfs4_put_copy(struct nfsd4_copy *copy)
1376 {
1377 	if (!refcount_dec_and_test(&copy->refcount))
1378 		return;
1379 	kfree(copy->cp_src);
1380 	kfree(copy);
1381 }
1382 
1383 static void nfsd4_stop_copy(struct nfsd4_copy *copy)
1384 {
1385 	trace_nfsd_copy_async_cancel(copy);
1386 	if (!test_and_set_bit(NFSD4_COPY_F_STOPPED, &copy->cp_flags)) {
1387 		kthread_stop(copy->copy_task);
1388 		copy->nfserr = nfs_ok;
1389 		set_bit(NFSD4_COPY_F_COMPLETED, &copy->cp_flags);
1390 	}
1391 	nfs4_put_copy(copy);
1392 }
1393 
1394 static struct nfsd4_copy *nfsd4_unhash_copy(struct nfs4_client *clp)
1395 {
1396 	struct nfsd4_copy *copy = NULL;
1397 
1398 	spin_lock(&clp->async_lock);
1399 	if (!list_empty(&clp->async_copies)) {
1400 		copy = list_first_entry(&clp->async_copies, struct nfsd4_copy,
1401 					copies);
1402 		refcount_inc(&copy->refcount);
1403 		copy->cp_clp = NULL;
1404 		if (!list_empty(&copy->copies))
1405 			list_del_init(&copy->copies);
1406 	}
1407 	spin_unlock(&clp->async_lock);
1408 	return copy;
1409 }
1410 
1411 void nfsd4_shutdown_copy(struct nfs4_client *clp)
1412 {
1413 	struct nfsd4_copy *copy;
1414 
1415 	while ((copy = nfsd4_unhash_copy(clp)) != NULL)
1416 		nfsd4_stop_copy(copy);
1417 }
1418 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
1419 
1420 extern struct file *nfs42_ssc_open(struct vfsmount *ss_mnt,
1421 				   struct nfs_fh *src_fh,
1422 				   nfs4_stateid *stateid);
1423 extern void nfs42_ssc_close(struct file *filep);
1424 
1425 extern void nfs_sb_deactive(struct super_block *sb);
1426 
1427 #define NFSD42_INTERSSC_MOUNTOPS "vers=4.2,addr=%s,sec=sys"
1428 
1429 /*
1430  * setup a work entry in the ssc delayed unmount list.
1431  */
1432 static __be32 nfsd4_ssc_setup_dul(struct nfsd_net *nn, char *ipaddr,
1433 				  struct nfsd4_ssc_umount_item **nsui,
1434 				  struct svc_rqst *rqstp)
1435 {
1436 	struct nfsd4_ssc_umount_item *ni = NULL;
1437 	struct nfsd4_ssc_umount_item *work = NULL;
1438 	struct nfsd4_ssc_umount_item *tmp;
1439 	DEFINE_WAIT(wait);
1440 	__be32 status = 0;
1441 
1442 	*nsui = NULL;
1443 	work = kzalloc(sizeof(*work), GFP_KERNEL);
1444 try_again:
1445 	spin_lock(&nn->nfsd_ssc_lock);
1446 	list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
1447 		if (strncmp(ni->nsui_ipaddr, ipaddr, sizeof(ni->nsui_ipaddr)))
1448 			continue;
1449 		/* found a match */
1450 		if (ni->nsui_busy) {
1451 			/*  wait - and try again */
1452 			prepare_to_wait(&nn->nfsd_ssc_waitq, &wait, TASK_IDLE);
1453 			spin_unlock(&nn->nfsd_ssc_lock);
1454 
1455 			/* allow 20secs for mount/unmount for now - revisit */
1456 			if (svc_thread_should_stop(rqstp) ||
1457 					(schedule_timeout(20*HZ) == 0)) {
1458 				finish_wait(&nn->nfsd_ssc_waitq, &wait);
1459 				kfree(work);
1460 				return nfserr_eagain;
1461 			}
1462 			finish_wait(&nn->nfsd_ssc_waitq, &wait);
1463 			goto try_again;
1464 		}
1465 		*nsui = ni;
1466 		refcount_inc(&ni->nsui_refcnt);
1467 		spin_unlock(&nn->nfsd_ssc_lock);
1468 		kfree(work);
1469 
1470 		/* return vfsmount in (*nsui)->nsui_vfsmount */
1471 		return 0;
1472 	}
1473 	if (work) {
1474 		strscpy(work->nsui_ipaddr, ipaddr, sizeof(work->nsui_ipaddr) - 1);
1475 		refcount_set(&work->nsui_refcnt, 2);
1476 		work->nsui_busy = true;
1477 		list_add_tail(&work->nsui_list, &nn->nfsd_ssc_mount_list);
1478 		*nsui = work;
1479 	} else
1480 		status = nfserr_resource;
1481 	spin_unlock(&nn->nfsd_ssc_lock);
1482 	return status;
1483 }
1484 
1485 static void nfsd4_ssc_update_dul(struct nfsd_net *nn,
1486 				 struct nfsd4_ssc_umount_item *nsui,
1487 				 struct vfsmount *ss_mnt)
1488 {
1489 	spin_lock(&nn->nfsd_ssc_lock);
1490 	nsui->nsui_vfsmount = ss_mnt;
1491 	nsui->nsui_busy = false;
1492 	wake_up_all(&nn->nfsd_ssc_waitq);
1493 	spin_unlock(&nn->nfsd_ssc_lock);
1494 }
1495 
1496 static void nfsd4_ssc_cancel_dul(struct nfsd_net *nn,
1497 				 struct nfsd4_ssc_umount_item *nsui)
1498 {
1499 	spin_lock(&nn->nfsd_ssc_lock);
1500 	list_del(&nsui->nsui_list);
1501 	wake_up_all(&nn->nfsd_ssc_waitq);
1502 	spin_unlock(&nn->nfsd_ssc_lock);
1503 	kfree(nsui);
1504 }
1505 
1506 /*
1507  * Support one copy source server for now.
1508  */
1509 static __be32
1510 nfsd4_interssc_connect(struct nl4_server *nss, struct svc_rqst *rqstp,
1511 		       struct nfsd4_ssc_umount_item **nsui)
1512 {
1513 	struct file_system_type *type;
1514 	struct vfsmount *ss_mnt;
1515 	struct nfs42_netaddr *naddr;
1516 	struct sockaddr_storage tmp_addr;
1517 	size_t tmp_addrlen, match_netid_len = 3;
1518 	char *startsep = "", *endsep = "", *match_netid = "tcp";
1519 	char *ipaddr, *dev_name, *raw_data;
1520 	int len, raw_len;
1521 	__be32 status = nfserr_inval;
1522 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1523 
1524 	naddr = &nss->u.nl4_addr;
1525 	tmp_addrlen = rpc_uaddr2sockaddr(SVC_NET(rqstp), naddr->addr,
1526 					 naddr->addr_len,
1527 					 (struct sockaddr *)&tmp_addr,
1528 					 sizeof(tmp_addr));
1529 	*nsui = NULL;
1530 	if (tmp_addrlen == 0)
1531 		goto out_err;
1532 
1533 	if (tmp_addr.ss_family == AF_INET6) {
1534 		startsep = "[";
1535 		endsep = "]";
1536 		match_netid = "tcp6";
1537 		match_netid_len = 4;
1538 	}
1539 
1540 	if (naddr->netid_len != match_netid_len ||
1541 		strncmp(naddr->netid, match_netid, naddr->netid_len))
1542 		goto out_err;
1543 
1544 	/* Construct the raw data for the vfs_kern_mount call */
1545 	len = RPC_MAX_ADDRBUFLEN + 1;
1546 	ipaddr = kzalloc(len, GFP_KERNEL);
1547 	if (!ipaddr)
1548 		goto out_err;
1549 
1550 	rpc_ntop((struct sockaddr *)&tmp_addr, ipaddr, len);
1551 
1552 	/* 2 for ipv6 endsep and startsep. 3 for ":/" and trailing '/0'*/
1553 
1554 	raw_len = strlen(NFSD42_INTERSSC_MOUNTOPS) + strlen(ipaddr);
1555 	raw_data = kzalloc(raw_len, GFP_KERNEL);
1556 	if (!raw_data)
1557 		goto out_free_ipaddr;
1558 
1559 	snprintf(raw_data, raw_len, NFSD42_INTERSSC_MOUNTOPS, ipaddr);
1560 
1561 	status = nfserr_nodev;
1562 	type = get_fs_type("nfs");
1563 	if (!type)
1564 		goto out_free_rawdata;
1565 
1566 	/* Set the server:<export> for the vfs_kern_mount call */
1567 	dev_name = kzalloc(len + 5, GFP_KERNEL);
1568 	if (!dev_name)
1569 		goto out_free_rawdata;
1570 	snprintf(dev_name, len + 5, "%s%s%s:/", startsep, ipaddr, endsep);
1571 
1572 	status = nfsd4_ssc_setup_dul(nn, ipaddr, nsui, rqstp);
1573 	if (status)
1574 		goto out_free_devname;
1575 	if ((*nsui)->nsui_vfsmount)
1576 		goto out_done;
1577 
1578 	/* Use an 'internal' mount: SB_KERNMOUNT -> MNT_INTERNAL */
1579 	ss_mnt = vfs_kern_mount(type, SB_KERNMOUNT, dev_name, raw_data);
1580 	module_put(type->owner);
1581 	if (IS_ERR(ss_mnt)) {
1582 		status = nfserr_nodev;
1583 		nfsd4_ssc_cancel_dul(nn, *nsui);
1584 		goto out_free_devname;
1585 	}
1586 	nfsd4_ssc_update_dul(nn, *nsui, ss_mnt);
1587 out_done:
1588 	status = 0;
1589 
1590 out_free_devname:
1591 	kfree(dev_name);
1592 out_free_rawdata:
1593 	kfree(raw_data);
1594 out_free_ipaddr:
1595 	kfree(ipaddr);
1596 out_err:
1597 	return status;
1598 }
1599 
1600 /*
1601  * Verify COPY destination stateid.
1602  *
1603  * Connect to the source server with NFSv4.1.
1604  * Create the source struct file for nfsd_copy_range.
1605  * Called with COPY cstate:
1606  *    SAVED_FH: source filehandle
1607  *    CURRENT_FH: destination filehandle
1608  */
1609 static __be32
1610 nfsd4_setup_inter_ssc(struct svc_rqst *rqstp,
1611 		      struct nfsd4_compound_state *cstate,
1612 		      struct nfsd4_copy *copy)
1613 {
1614 	struct svc_fh *s_fh = NULL;
1615 	stateid_t *s_stid = &copy->cp_src_stateid;
1616 	__be32 status = nfserr_inval;
1617 
1618 	/* Verify the destination stateid and set dst struct file*/
1619 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1620 					    &copy->cp_dst_stateid,
1621 					    WR_STATE, &copy->nf_dst, NULL);
1622 	if (status)
1623 		goto out;
1624 
1625 	status = nfsd4_interssc_connect(copy->cp_src, rqstp, &copy->ss_nsui);
1626 	if (status)
1627 		goto out;
1628 
1629 	s_fh = &cstate->save_fh;
1630 
1631 	copy->c_fh.size = s_fh->fh_handle.fh_size;
1632 	memcpy(copy->c_fh.data, &s_fh->fh_handle.fh_raw, copy->c_fh.size);
1633 	copy->stateid.seqid = cpu_to_be32(s_stid->si_generation);
1634 	memcpy(copy->stateid.other, (void *)&s_stid->si_opaque,
1635 	       sizeof(stateid_opaque_t));
1636 
1637 	status = 0;
1638 out:
1639 	return status;
1640 }
1641 
1642 static void
1643 nfsd4_cleanup_inter_ssc(struct nfsd4_ssc_umount_item *nsui, struct file *filp,
1644 			struct nfsd_file *dst)
1645 {
1646 	struct nfsd_net *nn = net_generic(dst->nf_net, nfsd_net_id);
1647 	long timeout = msecs_to_jiffies(nfsd4_ssc_umount_timeout);
1648 
1649 	nfs42_ssc_close(filp);
1650 	fput(filp);
1651 
1652 	spin_lock(&nn->nfsd_ssc_lock);
1653 	list_del(&nsui->nsui_list);
1654 	/*
1655 	 * vfsmount can be shared by multiple exports,
1656 	 * decrement refcnt. If the count drops to 1 it
1657 	 * will be unmounted when nsui_expire expires.
1658 	 */
1659 	refcount_dec(&nsui->nsui_refcnt);
1660 	nsui->nsui_expire = jiffies + timeout;
1661 	list_add_tail(&nsui->nsui_list, &nn->nfsd_ssc_mount_list);
1662 	spin_unlock(&nn->nfsd_ssc_lock);
1663 }
1664 
1665 #else /* CONFIG_NFSD_V4_2_INTER_SSC */
1666 
1667 static __be32
1668 nfsd4_setup_inter_ssc(struct svc_rqst *rqstp,
1669 		      struct nfsd4_compound_state *cstate,
1670 		      struct nfsd4_copy *copy)
1671 {
1672 	return nfserr_inval;
1673 }
1674 
1675 static void
1676 nfsd4_cleanup_inter_ssc(struct nfsd4_ssc_umount_item *nsui, struct file *filp,
1677 			struct nfsd_file *dst)
1678 {
1679 }
1680 
1681 static struct file *nfs42_ssc_open(struct vfsmount *ss_mnt,
1682 				   struct nfs_fh *src_fh,
1683 				   nfs4_stateid *stateid)
1684 {
1685 	return NULL;
1686 }
1687 #endif /* CONFIG_NFSD_V4_2_INTER_SSC */
1688 
1689 static __be32
1690 nfsd4_setup_intra_ssc(struct svc_rqst *rqstp,
1691 		      struct nfsd4_compound_state *cstate,
1692 		      struct nfsd4_copy *copy)
1693 {
1694 	return nfsd4_verify_copy(rqstp, cstate, &copy->cp_src_stateid,
1695 				 &copy->nf_src, &copy->cp_dst_stateid,
1696 				 &copy->nf_dst);
1697 }
1698 
1699 static void nfsd4_cb_offload_release(struct nfsd4_callback *cb)
1700 {
1701 	struct nfsd4_cb_offload *cbo =
1702 		container_of(cb, struct nfsd4_cb_offload, co_cb);
1703 	struct nfsd4_copy *copy =
1704 		container_of(cbo, struct nfsd4_copy, cp_cb_offload);
1705 
1706 	set_bit(NFSD4_COPY_F_OFFLOAD_DONE, &copy->cp_flags);
1707 }
1708 
1709 static int nfsd4_cb_offload_done(struct nfsd4_callback *cb,
1710 				 struct rpc_task *task)
1711 {
1712 	struct nfsd4_cb_offload *cbo =
1713 		container_of(cb, struct nfsd4_cb_offload, co_cb);
1714 
1715 	trace_nfsd_cb_offload_done(&cbo->co_res.cb_stateid, task);
1716 	switch (task->tk_status) {
1717 	case -NFS4ERR_DELAY:
1718 		if (cbo->co_retries--) {
1719 			rpc_delay(task, HZ / 5);
1720 			return 0;
1721 		}
1722 	}
1723 	nfsd41_cb_destroy_referring_call_list(cb);
1724 	return 1;
1725 }
1726 
1727 static const struct nfsd4_callback_ops nfsd4_cb_offload_ops = {
1728 	.release = nfsd4_cb_offload_release,
1729 	.done = nfsd4_cb_offload_done,
1730 	.opcode = OP_CB_OFFLOAD,
1731 };
1732 
1733 static void nfsd4_init_copy_res(struct nfsd4_copy *copy, bool sync)
1734 {
1735 	copy->cp_res.wr_stable_how =
1736 		test_bit(NFSD4_COPY_F_COMMITTED, &copy->cp_flags) ?
1737 			NFS_FILE_SYNC : NFS_UNSTABLE;
1738 	nfsd4_copy_set_sync(copy, sync);
1739 }
1740 
1741 static ssize_t _nfsd_copy_file_range(struct nfsd4_copy *copy,
1742 				     struct file *dst,
1743 				     struct file *src)
1744 {
1745 	errseq_t since;
1746 	ssize_t bytes_copied = 0;
1747 	u64 bytes_total = copy->cp_count;
1748 	u64 src_pos = copy->cp_src_pos;
1749 	u64 dst_pos = copy->cp_dst_pos;
1750 	int status;
1751 	loff_t end;
1752 
1753 	/* See RFC 7862 p.67: */
1754 	if (bytes_total == 0)
1755 		bytes_total = ULLONG_MAX;
1756 	do {
1757 		/* Only async copies can be stopped here */
1758 		if (kthread_should_stop())
1759 			break;
1760 		bytes_copied = nfsd_copy_file_range(src, src_pos, dst, dst_pos,
1761 						    bytes_total);
1762 		if (bytes_copied <= 0)
1763 			break;
1764 		bytes_total -= bytes_copied;
1765 		copy->cp_res.wr_bytes_written += bytes_copied;
1766 		src_pos += bytes_copied;
1767 		dst_pos += bytes_copied;
1768 	} while (bytes_total > 0 && nfsd4_copy_is_async(copy));
1769 	/* for a non-zero asynchronous copy do a commit of data */
1770 	if (nfsd4_copy_is_async(copy) && copy->cp_res.wr_bytes_written > 0) {
1771 		since = READ_ONCE(dst->f_wb_err);
1772 		end = copy->cp_dst_pos + copy->cp_res.wr_bytes_written - 1;
1773 		status = vfs_fsync_range(dst, copy->cp_dst_pos, end, 0);
1774 		if (!status)
1775 			status = filemap_check_wb_err(dst->f_mapping, since);
1776 		if (!status)
1777 			set_bit(NFSD4_COPY_F_COMMITTED, &copy->cp_flags);
1778 	}
1779 	return bytes_copied;
1780 }
1781 
1782 static __be32 nfsd4_do_copy(struct nfsd4_copy *copy,
1783 			    struct file *src, struct file *dst,
1784 			    bool sync)
1785 {
1786 	__be32 status;
1787 	ssize_t bytes;
1788 
1789 	bytes = _nfsd_copy_file_range(copy, dst, src);
1790 
1791 	/* for async copy, we ignore the error, client can always retry
1792 	 * to get the error
1793 	 */
1794 	if (bytes < 0 && !copy->cp_res.wr_bytes_written)
1795 		status = nfserrno(bytes);
1796 	else {
1797 		nfsd4_init_copy_res(copy, sync);
1798 		status = nfs_ok;
1799 	}
1800 	return status;
1801 }
1802 
1803 static void dup_copy_fields(struct nfsd4_copy *src, struct nfsd4_copy *dst)
1804 {
1805 	dst->cp_src_pos = src->cp_src_pos;
1806 	dst->cp_dst_pos = src->cp_dst_pos;
1807 	dst->cp_count = src->cp_count;
1808 	dst->cp_flags = src->cp_flags;
1809 	memcpy(&dst->cp_res, &src->cp_res, sizeof(src->cp_res));
1810 	memcpy(&dst->fh, &src->fh, sizeof(src->fh));
1811 	dst->cp_clp = src->cp_clp;
1812 	dst->nf_dst = nfsd_file_get(src->nf_dst);
1813 	/* for inter, nf_src doesn't exist yet */
1814 	if (!nfsd4_ssc_is_inter(src))
1815 		dst->nf_src = nfsd_file_get(src->nf_src);
1816 
1817 	memcpy(&dst->cp_stateid, &src->cp_stateid, sizeof(src->cp_stateid));
1818 	memcpy(dst->cp_src, src->cp_src, sizeof(struct nl4_server));
1819 	memcpy(&dst->stateid, &src->stateid, sizeof(src->stateid));
1820 	memcpy(&dst->c_fh, &src->c_fh, sizeof(src->c_fh));
1821 	dst->ss_nsui = src->ss_nsui;
1822 }
1823 
1824 static void release_copy_files(struct nfsd4_copy *copy)
1825 {
1826 	if (copy->nf_src)
1827 		nfsd_file_put(copy->nf_src);
1828 	if (copy->nf_dst)
1829 		nfsd_file_put(copy->nf_dst);
1830 }
1831 
1832 static void cleanup_async_copy(struct nfsd4_copy *copy)
1833 {
1834 	nfs4_free_copy_state(copy);
1835 	release_copy_files(copy);
1836 	if (copy->cp_clp) {
1837 		spin_lock(&copy->cp_clp->async_lock);
1838 		if (!list_empty(&copy->copies))
1839 			list_del_init(&copy->copies);
1840 		spin_unlock(&copy->cp_clp->async_lock);
1841 	}
1842 	nfs4_put_copy(copy);
1843 }
1844 
1845 static void nfsd4_send_cb_offload(struct nfsd4_copy *copy)
1846 {
1847 	struct nfsd4_cb_offload *cbo = &copy->cp_cb_offload;
1848 
1849 	memcpy(&cbo->co_res, &copy->cp_res, sizeof(copy->cp_res));
1850 	memcpy(&cbo->co_fh, &copy->fh, sizeof(copy->fh));
1851 	cbo->co_nfserr = copy->nfserr;
1852 	cbo->co_retries = 5;
1853 
1854 	nfsd4_init_cb(&cbo->co_cb, copy->cp_clp, &nfsd4_cb_offload_ops,
1855 		      NFSPROC4_CLNT_CB_OFFLOAD);
1856 	nfsd41_cb_referring_call(&cbo->co_cb, &cbo->co_referring_sessionid,
1857 				 cbo->co_referring_slotid,
1858 				 cbo->co_referring_seqno);
1859 	trace_nfsd_cb_offload(copy->cp_clp, &cbo->co_res.cb_stateid,
1860 			      &cbo->co_fh, copy->cp_count, copy->nfserr);
1861 	nfsd4_try_run_cb(&cbo->co_cb);
1862 }
1863 
1864 /**
1865  * nfsd4_do_async_copy - kthread function for background server-side COPY
1866  * @data: arguments for COPY operation
1867  *
1868  * Return values:
1869  *   %0: Copy operation is done.
1870  */
1871 static int nfsd4_do_async_copy(void *data)
1872 {
1873 	struct nfsd4_copy *copy = (struct nfsd4_copy *)data;
1874 
1875 	trace_nfsd_copy_async(copy);
1876 	if (nfsd4_ssc_is_inter(copy)) {
1877 		struct file *filp;
1878 
1879 		filp = nfs42_ssc_open(copy->ss_nsui->nsui_vfsmount,
1880 				      &copy->c_fh, &copy->stateid);
1881 		if (IS_ERR(filp)) {
1882 			switch (PTR_ERR(filp)) {
1883 			case -EBADF:
1884 				copy->nfserr = nfserr_wrong_type;
1885 				break;
1886 			default:
1887 				copy->nfserr = nfserr_offload_denied;
1888 			}
1889 			/* ss_mnt will be unmounted by the laundromat */
1890 			goto do_callback;
1891 		}
1892 		copy->nfserr = nfsd4_do_copy(copy, filp, copy->nf_dst->nf_file,
1893 					     false);
1894 		nfsd4_cleanup_inter_ssc(copy->ss_nsui, filp, copy->nf_dst);
1895 	} else {
1896 		copy->nfserr = nfsd4_do_copy(copy, copy->nf_src->nf_file,
1897 					     copy->nf_dst->nf_file, false);
1898 	}
1899 
1900 do_callback:
1901 	/* The kthread exits forthwith. Ensure that a subsequent
1902 	 * OFFLOAD_CANCEL won't try to kill it again. */
1903 	set_bit(NFSD4_COPY_F_STOPPED, &copy->cp_flags);
1904 
1905 	set_bit(NFSD4_COPY_F_COMPLETED, &copy->cp_flags);
1906 	trace_nfsd_copy_async_done(copy);
1907 	nfsd4_send_cb_offload(copy);
1908 	atomic_dec(&copy->cp_nn->pending_async_copies);
1909 	return 0;
1910 }
1911 
1912 static __be32
1913 nfsd4_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1914 		union nfsd4_op_u *u)
1915 {
1916 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1917 	struct nfsd4_copy *async_copy = NULL;
1918 	struct nfsd4_copy *copy = &u->copy;
1919 	struct nfsd42_write_res *result;
1920 	__be32 status;
1921 
1922 	/*
1923 	 * Currently, async COPY is not reliable. Force all COPY
1924 	 * requests to be synchronous to avoid client application
1925 	 * hangs waiting for COPY completion.
1926 	 */
1927 	nfsd4_copy_set_sync(copy, true);
1928 
1929 	result = &copy->cp_res;
1930 	nfsd_copy_write_verifier((__be32 *)&result->wr_verifier.data, nn);
1931 
1932 	copy->cp_clp = cstate->clp;
1933 	if (nfsd4_ssc_is_inter(copy)) {
1934 		trace_nfsd_copy_inter(copy);
1935 		if (!inter_copy_offload_enable || nfsd4_copy_is_sync(copy)) {
1936 			status = nfserr_notsupp;
1937 			goto out;
1938 		}
1939 		status = nfsd4_setup_inter_ssc(rqstp, cstate, copy);
1940 		if (status) {
1941 			trace_nfsd_copy_done(copy, status);
1942 			return nfserr_offload_denied;
1943 		}
1944 	} else {
1945 		trace_nfsd_copy_intra(copy);
1946 		status = nfsd4_setup_intra_ssc(rqstp, cstate, copy);
1947 		if (status) {
1948 			trace_nfsd_copy_done(copy, status);
1949 			return status;
1950 		}
1951 	}
1952 
1953 	memcpy(&copy->fh, &cstate->current_fh.fh_handle,
1954 		sizeof(struct knfsd_fh));
1955 	if (nfsd4_copy_is_async(copy)) {
1956 		async_copy = kzalloc(sizeof(struct nfsd4_copy), GFP_KERNEL);
1957 		if (!async_copy)
1958 			goto out_err;
1959 		async_copy->cp_nn = nn;
1960 		INIT_LIST_HEAD(&async_copy->copies);
1961 		refcount_set(&async_copy->refcount, 1);
1962 		async_copy->cp_ttl = NFSD_COPY_INITIAL_TTL;
1963 		/* Arbitrary cap on number of pending async copy operations */
1964 		if (atomic_inc_return(&nn->pending_async_copies) >
1965 				(int)rqstp->rq_pool->sp_nrthreads)
1966 			goto out_dec_async_copy_err;
1967 		async_copy->cp_src = kmalloc(sizeof(*async_copy->cp_src), GFP_KERNEL);
1968 		if (!async_copy->cp_src)
1969 			goto out_dec_async_copy_err;
1970 		if (!nfs4_init_copy_state(nn, copy))
1971 			goto out_dec_async_copy_err;
1972 		memcpy(&result->cb_stateid, &copy->cp_stateid.cs_stid,
1973 			sizeof(result->cb_stateid));
1974 		dup_copy_fields(copy, async_copy);
1975 		memcpy(async_copy->cp_cb_offload.co_referring_sessionid.data,
1976 		       cstate->session->se_sessionid.data,
1977 		       NFS4_MAX_SESSIONID_LEN);
1978 		async_copy->cp_cb_offload.co_referring_slotid = cstate->slot->sl_index;
1979 		async_copy->cp_cb_offload.co_referring_seqno = cstate->slot->sl_seqid;
1980 		async_copy->copy_task = kthread_create(nfsd4_do_async_copy,
1981 				async_copy, "%s", "copy thread");
1982 		if (IS_ERR(async_copy->copy_task))
1983 			goto out_dec_async_copy_err;
1984 		spin_lock(&async_copy->cp_clp->async_lock);
1985 		list_add(&async_copy->copies,
1986 				&async_copy->cp_clp->async_copies);
1987 		spin_unlock(&async_copy->cp_clp->async_lock);
1988 		wake_up_process(async_copy->copy_task);
1989 		status = nfs_ok;
1990 	} else {
1991 		status = nfsd4_do_copy(copy, copy->nf_src->nf_file,
1992 				       copy->nf_dst->nf_file, true);
1993 	}
1994 out:
1995 	trace_nfsd_copy_done(copy, status);
1996 	release_copy_files(copy);
1997 	return status;
1998 out_dec_async_copy_err:
1999 	if (async_copy)
2000 		atomic_dec(&nn->pending_async_copies);
2001 out_err:
2002 	if (nfsd4_ssc_is_inter(copy)) {
2003 		/*
2004 		 * Source's vfsmount of inter-copy will be unmounted
2005 		 * by the laundromat. Use copy instead of async_copy
2006 		 * since async_copy->ss_nsui might not be set yet.
2007 		 */
2008 		refcount_dec(&copy->ss_nsui->nsui_refcnt);
2009 	}
2010 	if (async_copy)
2011 		cleanup_async_copy(async_copy);
2012 	status = nfserr_jukebox;
2013 	goto out;
2014 }
2015 
2016 static struct nfsd4_copy *
2017 find_async_copy_locked(struct nfs4_client *clp, stateid_t *stateid)
2018 {
2019 	struct nfsd4_copy *copy;
2020 
2021 	lockdep_assert_held(&clp->async_lock);
2022 
2023 	list_for_each_entry(copy, &clp->async_copies, copies) {
2024 		if (memcmp(&copy->cp_stateid.cs_stid, stateid, NFS4_STATEID_SIZE))
2025 			continue;
2026 		return copy;
2027 	}
2028 	return NULL;
2029 }
2030 
2031 static struct nfsd4_copy *
2032 find_async_copy(struct nfs4_client *clp, stateid_t *stateid)
2033 {
2034 	struct nfsd4_copy *copy;
2035 
2036 	spin_lock(&clp->async_lock);
2037 	copy = find_async_copy_locked(clp, stateid);
2038 	if (copy)
2039 		refcount_inc(&copy->refcount);
2040 	spin_unlock(&clp->async_lock);
2041 	return copy;
2042 }
2043 
2044 static __be32
2045 nfsd4_offload_cancel(struct svc_rqst *rqstp,
2046 		     struct nfsd4_compound_state *cstate,
2047 		     union nfsd4_op_u *u)
2048 {
2049 	struct nfsd4_offload_status *os = &u->offload_status;
2050 	struct nfsd4_copy *copy;
2051 	struct nfs4_client *clp = cstate->clp;
2052 
2053 	copy = find_async_copy(clp, &os->stateid);
2054 	if (!copy) {
2055 		struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2056 
2057 		return manage_cpntf_state(nn, &os->stateid, clp, NULL);
2058 	} else
2059 		nfsd4_stop_copy(copy);
2060 
2061 	return nfs_ok;
2062 }
2063 
2064 static __be32
2065 nfsd4_copy_notify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2066 		  union nfsd4_op_u *u)
2067 {
2068 	struct nfsd4_copy_notify *cn = &u->copy_notify;
2069 	__be32 status;
2070 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2071 	struct nfs4_stid *stid = NULL;
2072 	struct nfs4_cpntf_state *cps;
2073 	struct nfs4_client *clp = cstate->clp;
2074 
2075 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
2076 					&cn->cpn_src_stateid, RD_STATE, NULL,
2077 					&stid);
2078 	if (status)
2079 		return status;
2080 	if (!stid)
2081 		return nfserr_bad_stateid;
2082 
2083 	cn->cpn_lease_time.tv_sec = nn->nfsd4_lease;
2084 	cn->cpn_lease_time.tv_nsec = 0;
2085 
2086 	status = nfserrno(-ENOMEM);
2087 	cps = nfs4_alloc_init_cpntf_state(nn, stid);
2088 	if (!cps)
2089 		goto out;
2090 	memcpy(&cn->cpn_cnr_stateid, &cps->cp_stateid.cs_stid, sizeof(stateid_t));
2091 	memcpy(&cps->cp_p_stateid, &stid->sc_stateid, sizeof(stateid_t));
2092 	memcpy(&cps->cp_p_clid, &clp->cl_clientid, sizeof(clientid_t));
2093 
2094 	/* For now, only return one server address in cpn_src, the
2095 	 * address used by the client to connect to this server.
2096 	 */
2097 	cn->cpn_src->nl4_type = NL4_NETADDR;
2098 	status = nfsd4_set_netaddr((struct sockaddr *)&rqstp->rq_daddr,
2099 				 &cn->cpn_src->u.nl4_addr);
2100 	WARN_ON_ONCE(status);
2101 	if (status) {
2102 		nfs4_put_cpntf_state(nn, cps);
2103 		goto out;
2104 	}
2105 out:
2106 	nfs4_put_stid(stid);
2107 	return status;
2108 }
2109 
2110 static __be32
2111 nfsd4_fallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2112 		struct nfsd4_fallocate *fallocate, int flags)
2113 {
2114 	__be32 status;
2115 	struct nfsd_file *nf;
2116 
2117 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
2118 					    &fallocate->falloc_stateid,
2119 					    WR_STATE, &nf, NULL);
2120 	if (status != nfs_ok)
2121 		return status;
2122 
2123 	status = nfsd4_vfs_fallocate(rqstp, &cstate->current_fh, nf->nf_file,
2124 				     fallocate->falloc_offset,
2125 				     fallocate->falloc_length,
2126 				     flags);
2127 	nfsd_file_put(nf);
2128 	return status;
2129 }
2130 
2131 static __be32
2132 nfsd4_offload_status(struct svc_rqst *rqstp,
2133 		     struct nfsd4_compound_state *cstate,
2134 		     union nfsd4_op_u *u)
2135 {
2136 	struct nfsd4_offload_status *os = &u->offload_status;
2137 	__be32 status = nfs_ok;
2138 	struct nfsd4_copy *copy;
2139 	struct nfs4_client *clp = cstate->clp;
2140 
2141 	os->completed = false;
2142 	spin_lock(&clp->async_lock);
2143 	copy = find_async_copy_locked(clp, &os->stateid);
2144 	if (copy) {
2145 		os->count = copy->cp_res.wr_bytes_written;
2146 		if (test_bit(NFSD4_COPY_F_COMPLETED, &copy->cp_flags)) {
2147 			os->completed = true;
2148 			os->status = copy->nfserr;
2149 		}
2150 	} else
2151 		status = nfserr_bad_stateid;
2152 	spin_unlock(&clp->async_lock);
2153 
2154 	return status;
2155 }
2156 
2157 static __be32
2158 nfsd4_allocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2159 	       union nfsd4_op_u *u)
2160 {
2161 	return nfsd4_fallocate(rqstp, cstate, &u->allocate, 0);
2162 }
2163 
2164 static __be32
2165 nfsd4_deallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2166 		 union nfsd4_op_u *u)
2167 {
2168 	return nfsd4_fallocate(rqstp, cstate, &u->deallocate,
2169 			       FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
2170 }
2171 
2172 static __be32
2173 nfsd4_seek(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2174 	   union nfsd4_op_u *u)
2175 {
2176 	struct nfsd4_seek *seek = &u->seek;
2177 	int whence;
2178 	__be32 status;
2179 	struct nfsd_file *nf;
2180 
2181 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
2182 					    &seek->seek_stateid,
2183 					    RD_STATE, &nf, NULL);
2184 	if (status)
2185 		return status;
2186 
2187 	switch (seek->seek_whence) {
2188 	case NFS4_CONTENT_DATA:
2189 		whence = SEEK_DATA;
2190 		break;
2191 	case NFS4_CONTENT_HOLE:
2192 		whence = SEEK_HOLE;
2193 		break;
2194 	default:
2195 		status = nfserr_union_notsupp;
2196 		goto out;
2197 	}
2198 
2199 	/*
2200 	 * Note:  This call does change file->f_pos, but nothing in NFSD
2201 	 *        should ever file->f_pos.
2202 	 */
2203 	seek->seek_pos = vfs_llseek(nf->nf_file, seek->seek_offset, whence);
2204 	if (seek->seek_pos < 0)
2205 		status = nfserrno(seek->seek_pos);
2206 	else if (seek->seek_pos >= i_size_read(file_inode(nf->nf_file)))
2207 		seek->seek_eof = true;
2208 
2209 out:
2210 	nfsd_file_put(nf);
2211 	return status;
2212 }
2213 
2214 /* This routine never returns NFS_OK!  If there are no other errors, it
2215  * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
2216  * attributes matched.  VERIFY is implemented by mapping NFSERR_SAME
2217  * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
2218  */
2219 static __be32
2220 _nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2221 	     struct nfsd4_verify *verify)
2222 {
2223 	__be32 *buf, *p;
2224 	int count;
2225 	__be32 status;
2226 
2227 	status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
2228 	if (status)
2229 		return status;
2230 
2231 	status = check_attr_support(rqstp, cstate, verify->ve_bmval, NULL);
2232 	if (status)
2233 		return status;
2234 
2235 	if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
2236 	    || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
2237 		return nfserr_inval;
2238 	if (verify->ve_attrlen & 3)
2239 		return nfserr_inval;
2240 
2241 	/* count in words:
2242 	 *   bitmap_len(1) + bitmap(2) + attr_len(1) = 4
2243 	 */
2244 	count = 4 + (verify->ve_attrlen >> 2);
2245 	buf = kmalloc(count << 2, GFP_KERNEL);
2246 	if (!buf)
2247 		return nfserr_jukebox;
2248 
2249 	p = buf;
2250 	status = nfsd4_encode_fattr_to_buf(&p, count, &cstate->current_fh,
2251 				    cstate->current_fh.fh_export,
2252 				    cstate->current_fh.fh_dentry,
2253 				    verify->ve_bmval,
2254 				    rqstp, 0);
2255 	/*
2256 	 * If nfsd4_encode_fattr() ran out of space, assume that's because
2257 	 * the attributes are longer (hence different) than those given:
2258 	 */
2259 	if (status == nfserr_resource)
2260 		status = nfserr_not_same;
2261 	if (status)
2262 		goto out_kfree;
2263 
2264 	/* skip bitmap */
2265 	p = buf + 1 + ntohl(buf[0]);
2266 	status = nfserr_not_same;
2267 	if (ntohl(*p++) != verify->ve_attrlen)
2268 		goto out_kfree;
2269 	if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
2270 		status = nfserr_same;
2271 
2272 out_kfree:
2273 	kfree(buf);
2274 	return status;
2275 }
2276 
2277 static __be32
2278 nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2279 	      union nfsd4_op_u *u)
2280 {
2281 	__be32 status;
2282 
2283 	status = _nfsd4_verify(rqstp, cstate, &u->verify);
2284 	return status == nfserr_not_same ? nfs_ok : status;
2285 }
2286 
2287 static __be32
2288 nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2289 	     union nfsd4_op_u *u)
2290 {
2291 	__be32 status;
2292 
2293 	status = _nfsd4_verify(rqstp, cstate, &u->nverify);
2294 	return status == nfserr_same ? nfs_ok : status;
2295 }
2296 
2297 static __be32
2298 nfsd4_get_dir_delegation(struct svc_rqst *rqstp,
2299 			 struct nfsd4_compound_state *cstate,
2300 			 union nfsd4_op_u *u)
2301 {
2302 	struct nfsd4_get_dir_delegation *gdd = &u->get_dir_delegation;
2303 
2304 	/*
2305 	 * RFC 8881, section 18.39.3 says:
2306 	 *
2307 	 * "The server may refuse to grant the delegation. In that case, the
2308 	 *  server will return NFS4ERR_DIRDELEG_UNAVAIL."
2309 	 *
2310 	 * This is sub-optimal, since it means that the server would need to
2311 	 * abort compound processing just because the delegation wasn't
2312 	 * available. RFC8881bis should change this to allow the server to
2313 	 * return NFS4_OK with a non-fatal status of GDD4_UNAVAIL in this
2314 	 * situation.
2315 	 */
2316 	gdd->gddrnf_status = GDD4_UNAVAIL;
2317 	return nfs_ok;
2318 }
2319 
2320 #ifdef CONFIG_NFSD_PNFS
2321 static const struct nfsd4_layout_ops *
2322 nfsd4_layout_verify(struct svc_export *exp, unsigned int layout_type)
2323 {
2324 	if (!exp->ex_layout_types) {
2325 		dprintk("%s: export does not support pNFS\n", __func__);
2326 		return NULL;
2327 	}
2328 
2329 	if (layout_type >= LAYOUT_TYPE_MAX ||
2330 	    !(exp->ex_layout_types & (1 << layout_type))) {
2331 		dprintk("%s: layout type %d not supported\n",
2332 			__func__, layout_type);
2333 		return NULL;
2334 	}
2335 
2336 	return nfsd4_layout_ops[layout_type];
2337 }
2338 
2339 static __be32
2340 nfsd4_getdeviceinfo(struct svc_rqst *rqstp,
2341 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
2342 {
2343 	struct nfsd4_getdeviceinfo *gdp = &u->getdeviceinfo;
2344 	const struct nfsd4_layout_ops *ops;
2345 	struct nfsd4_deviceid_map *map;
2346 	struct svc_export *exp;
2347 	__be32 nfserr;
2348 
2349 	dprintk("%s: layout_type %u dev_id [0x%llx:0x%x] maxcnt %u\n",
2350 	       __func__,
2351 	       gdp->gd_layout_type,
2352 	       gdp->gd_devid.fsid_idx, gdp->gd_devid.generation,
2353 	       gdp->gd_maxcount);
2354 
2355 	map = nfsd4_find_devid_map(gdp->gd_devid.fsid_idx);
2356 	if (!map) {
2357 		dprintk("%s: couldn't find device ID to export mapping!\n",
2358 			__func__);
2359 		return nfserr_noent;
2360 	}
2361 
2362 	exp = rqst_exp_find(&rqstp->rq_chandle, SVC_NET(rqstp),
2363 			    rqstp->rq_client, rqstp->rq_gssclient,
2364 			    map->fsid_type, map->fsid);
2365 	if (IS_ERR(exp)) {
2366 		dprintk("%s: could not find device id\n", __func__);
2367 		return nfserr_noent;
2368 	}
2369 
2370 	nfserr = nfserr_layoutunavailable;
2371 	ops = nfsd4_layout_verify(exp, gdp->gd_layout_type);
2372 	if (!ops)
2373 		goto out;
2374 
2375 	nfserr = nfs_ok;
2376 	if (gdp->gd_maxcount != 0) {
2377 		nfserr = ops->proc_getdeviceinfo(exp->ex_path.mnt->mnt_sb,
2378 				rqstp, cstate->clp, gdp);
2379 	}
2380 
2381 	gdp->gd_notify_types &= ops->notify_types;
2382 out:
2383 	exp_put(exp);
2384 	return nfserr;
2385 }
2386 
2387 static void
2388 nfsd4_getdeviceinfo_release(union nfsd4_op_u *u)
2389 {
2390 	kfree(u->getdeviceinfo.gd_device);
2391 }
2392 
2393 static __be32
2394 nfsd4_layoutget(struct svc_rqst *rqstp,
2395 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
2396 {
2397 	struct nfsd4_layoutget *lgp = &u->layoutget;
2398 	struct svc_fh *current_fh = &cstate->current_fh;
2399 	const struct nfsd4_layout_ops *ops;
2400 	struct nfs4_layout_stateid *ls;
2401 	__be32 nfserr;
2402 	int accmode = NFSD_MAY_READ_IF_EXEC | NFSD_MAY_OWNER_OVERRIDE;
2403 
2404 	switch (lgp->lg_seg.iomode) {
2405 	case IOMODE_READ:
2406 		accmode |= NFSD_MAY_READ;
2407 		break;
2408 	case IOMODE_RW:
2409 		accmode |= NFSD_MAY_READ | NFSD_MAY_WRITE;
2410 		break;
2411 	default:
2412 		dprintk("%s: invalid iomode %d\n",
2413 			__func__, lgp->lg_seg.iomode);
2414 		nfserr = nfserr_badiomode;
2415 		goto out;
2416 	}
2417 
2418 	nfserr = fh_verify(rqstp, current_fh, 0, accmode);
2419 	if (nfserr)
2420 		goto out;
2421 
2422 	nfserr = nfserr_layoutunavailable;
2423 	ops = nfsd4_layout_verify(current_fh->fh_export, lgp->lg_layout_type);
2424 	if (!ops)
2425 		goto out;
2426 
2427 	/*
2428 	 * Verify minlength and range as per RFC5661:
2429 	 *  o  If loga_length is less than loga_minlength,
2430 	 *     the metadata server MUST return NFS4ERR_INVAL.
2431 	 *  o  If the sum of loga_offset and loga_minlength exceeds
2432 	 *     NFS4_UINT64_MAX, and loga_minlength is not
2433 	 *     NFS4_UINT64_MAX, the error NFS4ERR_INVAL MUST result.
2434 	 *  o  If the sum of loga_offset and loga_length exceeds
2435 	 *     NFS4_UINT64_MAX, and loga_length is not NFS4_UINT64_MAX,
2436 	 *     the error NFS4ERR_INVAL MUST result.
2437 	 */
2438 	nfserr = nfserr_inval;
2439 	if (lgp->lg_seg.length < lgp->lg_minlength ||
2440 	    (lgp->lg_minlength != NFS4_MAX_UINT64 &&
2441 	     lgp->lg_minlength > NFS4_MAX_UINT64 - lgp->lg_seg.offset) ||
2442 	    (lgp->lg_seg.length != NFS4_MAX_UINT64 &&
2443 	     lgp->lg_seg.length > NFS4_MAX_UINT64 - lgp->lg_seg.offset))
2444 		goto out;
2445 	if (lgp->lg_seg.length == 0)
2446 		goto out;
2447 
2448 	nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lgp->lg_sid,
2449 						true, lgp->lg_layout_type, &ls);
2450 	if (nfserr) {
2451 		trace_nfsd_layout_get_lookup_fail(&lgp->lg_sid);
2452 		goto out;
2453 	}
2454 
2455 	nfserr = nfserr_recallconflict;
2456 	if (atomic_read(&ls->ls_stid.sc_file->fi_lo_recalls))
2457 		goto out_put_stid;
2458 
2459 	nfserr = ops->proc_layoutget(d_inode(current_fh->fh_dentry),
2460 				     current_fh, lgp);
2461 	if (nfserr)
2462 		goto out_put_stid;
2463 
2464 	nfserr = nfsd4_insert_layout(lgp, ls);
2465 
2466 out_put_stid:
2467 	mutex_unlock(&ls->ls_mutex);
2468 	nfs4_put_stid(&ls->ls_stid);
2469 out:
2470 	return nfserr;
2471 }
2472 
2473 static void
2474 nfsd4_layoutget_release(union nfsd4_op_u *u)
2475 {
2476 	kfree(u->layoutget.lg_content);
2477 }
2478 
2479 static __be32
2480 nfsd4_layoutcommit(struct svc_rqst *rqstp,
2481 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
2482 {
2483 	struct nfsd4_layoutcommit *lcp = &u->layoutcommit;
2484 	const struct nfsd4_layout_seg *seg = &lcp->lc_seg;
2485 	struct svc_fh *current_fh = &cstate->current_fh;
2486 	const struct nfsd4_layout_ops *ops;
2487 	loff_t new_size = lcp->lc_last_wr + 1;
2488 	struct inode *inode;
2489 	struct nfs4_layout_stateid *ls;
2490 	__be32 nfserr;
2491 
2492 	nfserr = fh_verify(rqstp, current_fh, 0,
2493 			   NFSD_MAY_WRITE | NFSD_MAY_OWNER_OVERRIDE);
2494 	if (nfserr)
2495 		goto out;
2496 
2497 	nfserr = nfserr_layoutunavailable;
2498 	ops = nfsd4_layout_verify(current_fh->fh_export, lcp->lc_layout_type);
2499 	if (!ops)
2500 		goto out;
2501 	inode = d_inode(current_fh->fh_dentry);
2502 
2503 	nfserr = nfserr_inval;
2504 	if (new_size <= seg->offset) {
2505 		dprintk("pnfsd: last write before layout segment\n");
2506 		goto out;
2507 	}
2508 	if (new_size > seg->offset + seg->length) {
2509 		dprintk("pnfsd: last write beyond layout segment\n");
2510 		goto out;
2511 	}
2512 	if (!lcp->lc_newoffset && new_size > i_size_read(inode)) {
2513 		dprintk("pnfsd: layoutcommit beyond EOF\n");
2514 		goto out;
2515 	}
2516 
2517 	nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lcp->lc_sid,
2518 						false, lcp->lc_layout_type,
2519 						&ls);
2520 	if (nfserr) {
2521 		trace_nfsd_layout_commit_lookup_fail(&lcp->lc_sid);
2522 		/* fixup error code as per RFC5661 */
2523 		if (nfserr == nfserr_bad_stateid)
2524 			nfserr = nfserr_badlayout;
2525 		goto out;
2526 	}
2527 
2528 	/* LAYOUTCOMMIT does not require any serialization */
2529 	mutex_unlock(&ls->ls_mutex);
2530 
2531 	if (new_size > i_size_read(inode)) {
2532 		lcp->lc_size_chg = true;
2533 		lcp->lc_newsize = new_size;
2534 	} else {
2535 		lcp->lc_size_chg = false;
2536 	}
2537 
2538 	nfserr = ops->proc_layoutcommit(inode, lcp);
2539 	nfs4_put_stid(&ls->ls_stid);
2540 out:
2541 	return nfserr;
2542 }
2543 
2544 static __be32
2545 nfsd4_layoutreturn(struct svc_rqst *rqstp,
2546 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
2547 {
2548 	struct nfsd4_layoutreturn *lrp = &u->layoutreturn;
2549 	struct svc_fh *current_fh = &cstate->current_fh;
2550 	__be32 nfserr;
2551 
2552 	nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_NOP);
2553 	if (nfserr)
2554 		goto out;
2555 
2556 	nfserr = nfserr_layoutunavailable;
2557 	if (!nfsd4_layout_verify(current_fh->fh_export, lrp->lr_layout_type))
2558 		goto out;
2559 
2560 	switch (lrp->lr_seg.iomode) {
2561 	case IOMODE_READ:
2562 	case IOMODE_RW:
2563 	case IOMODE_ANY:
2564 		break;
2565 	default:
2566 		dprintk("%s: invalid iomode %d\n", __func__,
2567 			lrp->lr_seg.iomode);
2568 		nfserr = nfserr_inval;
2569 		goto out;
2570 	}
2571 
2572 	switch (lrp->lr_return_type) {
2573 	case RETURN_FILE:
2574 		nfserr = nfsd4_return_file_layouts(rqstp, cstate, lrp);
2575 		break;
2576 	case RETURN_FSID:
2577 	case RETURN_ALL:
2578 		nfserr = nfsd4_return_client_layouts(rqstp, cstate, lrp);
2579 		break;
2580 	default:
2581 		dprintk("%s: invalid return_type %d\n", __func__,
2582 			lrp->lr_return_type);
2583 		nfserr = nfserr_inval;
2584 		break;
2585 	}
2586 out:
2587 	return nfserr;
2588 }
2589 #endif /* CONFIG_NFSD_PNFS */
2590 
2591 static __be32
2592 nfsd4_getxattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2593 	       union nfsd4_op_u *u)
2594 {
2595 	struct nfsd4_getxattr *getxattr = &u->getxattr;
2596 
2597 	return nfsd_getxattr(rqstp, &cstate->current_fh,
2598 			     getxattr->getxa_name, &getxattr->getxa_buf,
2599 			     &getxattr->getxa_len);
2600 }
2601 
2602 static __be32
2603 nfsd4_setxattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2604 	   union nfsd4_op_u *u)
2605 {
2606 	struct nfsd4_setxattr *setxattr = &u->setxattr;
2607 	__be32 ret;
2608 
2609 	if (opens_in_grace(SVC_NET(rqstp)))
2610 		return nfserr_grace;
2611 
2612 	ret = nfsd_setxattr(rqstp, &cstate->current_fh, setxattr->setxa_name,
2613 			    setxattr->setxa_buf, setxattr->setxa_len,
2614 			    setxattr->setxa_flags);
2615 
2616 	if (!ret)
2617 		set_change_info(&setxattr->setxa_cinfo, &cstate->current_fh);
2618 
2619 	return ret;
2620 }
2621 
2622 static __be32
2623 nfsd4_listxattrs(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2624 	   union nfsd4_op_u *u)
2625 {
2626 	/*
2627 	 * Get the entire list, then copy out only the user attributes
2628 	 * in the encode function.
2629 	 */
2630 	return nfsd_listxattr(rqstp, &cstate->current_fh,
2631 			     &u->listxattrs.lsxa_buf, &u->listxattrs.lsxa_len);
2632 }
2633 
2634 static __be32
2635 nfsd4_removexattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2636 	   union nfsd4_op_u *u)
2637 {
2638 	struct nfsd4_removexattr *removexattr = &u->removexattr;
2639 	__be32 ret;
2640 
2641 	if (opens_in_grace(SVC_NET(rqstp)))
2642 		return nfserr_grace;
2643 
2644 	ret = nfsd_removexattr(rqstp, &cstate->current_fh,
2645 	    removexattr->rmxa_name);
2646 
2647 	if (!ret)
2648 		set_change_info(&removexattr->rmxa_cinfo, &cstate->current_fh);
2649 
2650 	return ret;
2651 }
2652 
2653 /*
2654  * NULL call.
2655  */
2656 static __be32
2657 nfsd4_proc_null(struct svc_rqst *rqstp)
2658 {
2659 	return rpc_success;
2660 }
2661 
2662 static inline void nfsd4_increment_op_stats(struct nfsd_net *nn, u32 opnum)
2663 {
2664 	if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
2665 		percpu_counter_inc(&nn->counter[NFSD_STATS_NFS4_OP(opnum)]);
2666 }
2667 
2668 static const struct nfsd4_operation nfsd4_ops[];
2669 
2670 static const char *nfsd4_op_name(unsigned opnum);
2671 
2672 /*
2673  * Enforce NFSv4.1 COMPOUND ordering rules:
2674  *
2675  * Also note, enforced elsewhere:
2676  *	- SEQUENCE other than as first op results in
2677  *	  NFS4ERR_SEQUENCE_POS. (Enforced in nfsd4_sequence().)
2678  *	- BIND_CONN_TO_SESSION must be the only op in its compound.
2679  *	  (Enforced in nfsd4_bind_conn_to_session().)
2680  *	- DESTROY_SESSION must be the final operation in a compound, if
2681  *	  sessionid's in SEQUENCE and DESTROY_SESSION are the same.
2682  *	  (Enforced in nfsd4_destroy_session().)
2683  */
2684 static __be32 nfs41_check_op_ordering(struct nfsd4_compoundargs *args)
2685 {
2686 	struct nfsd4_op *first_op = &args->ops[0];
2687 
2688 	/* These ordering requirements don't apply to NFSv4.0: */
2689 	if (args->minorversion == 0)
2690 		return nfs_ok;
2691 	/* This is weird, but OK, not our problem: */
2692 	if (args->opcnt == 0)
2693 		return nfs_ok;
2694 	if (first_op->status == nfserr_op_illegal)
2695 		return nfs_ok;
2696 	if (!(nfsd4_ops[first_op->opnum].op_flags & ALLOWED_AS_FIRST_OP))
2697 		return nfserr_op_not_in_session;
2698 	if (first_op->opnum == OP_SEQUENCE)
2699 		return nfs_ok;
2700 	/*
2701 	 * So first_op is something allowed outside a session, like
2702 	 * EXCHANGE_ID; but then it has to be the only op in the
2703 	 * compound:
2704 	 */
2705 	if (args->opcnt != 1)
2706 		return nfserr_not_only_op;
2707 	return nfs_ok;
2708 }
2709 
2710 const struct nfsd4_operation *OPDESC(struct nfsd4_op *op)
2711 {
2712 	return &nfsd4_ops[op->opnum];
2713 }
2714 
2715 bool nfsd4_cache_this_op(struct nfsd4_op *op)
2716 {
2717 	if (op->opnum == OP_ILLEGAL)
2718 		return false;
2719 	return OPDESC(op)->op_flags & OP_CACHEME;
2720 }
2721 
2722 static bool need_wrongsec_check(struct svc_rqst *rqstp)
2723 {
2724 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
2725 	struct nfsd4_compoundargs *argp = rqstp->rq_argp;
2726 	struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
2727 	struct nfsd4_op *next = &argp->ops[resp->opcnt];
2728 	const struct nfsd4_operation *thisd = OPDESC(this);
2729 	const struct nfsd4_operation *nextd;
2730 
2731 	/*
2732 	 * Most ops check wronsec on our own; only the putfh-like ops
2733 	 * have special rules.
2734 	 */
2735 	if (!(thisd->op_flags & OP_IS_PUTFH_LIKE))
2736 		return false;
2737 	/*
2738 	 * rfc 5661 2.6.3.1.1.6: don't bother erroring out a
2739 	 * put-filehandle operation if we're not going to use the
2740 	 * result:
2741 	 */
2742 	if (argp->opcnt == resp->opcnt)
2743 		return false;
2744 	if (next->opnum == OP_ILLEGAL)
2745 		return false;
2746 	nextd = OPDESC(next);
2747 	/*
2748 	 * Rest of 2.6.3.1.1: certain operations will return WRONGSEC
2749 	 * errors themselves as necessary; others should check for them
2750 	 * now:
2751 	 */
2752 	return !(nextd->op_flags & OP_HANDLES_WRONGSEC);
2753 }
2754 
2755 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
2756 static void
2757 check_if_stalefh_allowed(struct nfsd4_compoundargs *args)
2758 {
2759 	struct nfsd4_op	*op, *current_op = NULL, *saved_op = NULL;
2760 	struct nfsd4_copy *copy;
2761 	struct nfsd4_putfh *putfh;
2762 	int i;
2763 
2764 	/* traverse all operation and if it's a COPY compound, mark the
2765 	 * source filehandle to skip verification
2766 	 */
2767 	for (i = 0; i < args->opcnt; i++) {
2768 		op = &args->ops[i];
2769 		if (op->opnum == OP_PUTFH)
2770 			current_op = op;
2771 		else if (op->opnum == OP_SAVEFH)
2772 			saved_op = current_op;
2773 		else if (op->opnum == OP_RESTOREFH)
2774 			current_op = saved_op;
2775 		else if (op->opnum == OP_COPY) {
2776 			copy = (struct nfsd4_copy *)&op->u;
2777 			if (!saved_op) {
2778 				op->status = nfserr_nofilehandle;
2779 				return;
2780 			}
2781 			putfh = (struct nfsd4_putfh *)&saved_op->u;
2782 			if (nfsd4_ssc_is_inter(copy))
2783 				putfh->no_verify = true;
2784 		}
2785 	}
2786 }
2787 #else
2788 static void
2789 check_if_stalefh_allowed(struct nfsd4_compoundargs *args)
2790 {
2791 }
2792 #endif
2793 
2794 /*
2795  * COMPOUND call.
2796  */
2797 static __be32
2798 nfsd4_proc_compound(struct svc_rqst *rqstp)
2799 {
2800 	struct nfsd4_compoundargs *args = rqstp->rq_argp;
2801 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
2802 	struct nfsd4_op	*op;
2803 	struct nfsd4_compound_state *cstate = &resp->cstate;
2804 	struct svc_fh *current_fh = &cstate->current_fh;
2805 	struct svc_fh *save_fh = &cstate->save_fh;
2806 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2807 	__be32		status;
2808 
2809 	resp->xdr = &rqstp->rq_res_stream;
2810 	resp->statusp = resp->xdr->p;
2811 
2812 	/* reserve space for: NFS status code */
2813 	xdr_reserve_space(resp->xdr, XDR_UNIT);
2814 
2815 	/* reserve space for: taglen, tag, and opcnt */
2816 	xdr_reserve_space(resp->xdr, XDR_UNIT * 2 + args->taglen);
2817 	resp->taglen = args->taglen;
2818 	resp->tag = args->tag;
2819 	resp->rqstp = rqstp;
2820 	cstate->minorversion = args->minorversion;
2821 	fh_init(current_fh, NFS4_FHSIZE);
2822 	fh_init(save_fh, NFS4_FHSIZE);
2823 	/*
2824 	 * Don't use the deferral mechanism for NFSv4; compounds make it
2825 	 * too hard to avoid non-idempotency problems.
2826 	 */
2827 	clear_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
2828 
2829 	/*
2830 	 * According to RFC3010, this takes precedence over all other errors.
2831 	 */
2832 	status = nfserr_minor_vers_mismatch;
2833 	if (nfsd_minorversion(nn, args->minorversion, NFSD_TEST) <= 0)
2834 		goto out;
2835 
2836 	status = nfs41_check_op_ordering(args);
2837 	if (status) {
2838 		op = &args->ops[0];
2839 		op->status = status;
2840 		resp->opcnt = 1;
2841 		goto encode_op;
2842 	}
2843 	check_if_stalefh_allowed(args);
2844 
2845 	rqstp->rq_lease_breaker = (void **)&cstate->clp;
2846 
2847 	trace_nfsd_compound(rqstp, args->tag, args->taglen, args->client_opcnt);
2848 	while (!status && resp->opcnt < args->opcnt) {
2849 		op = &args->ops[resp->opcnt++];
2850 
2851 		if (unlikely(resp->opcnt == NFSD_MAX_OPS_PER_COMPOUND)) {
2852 			/* If there are still more operations to process,
2853 			 * stop here and report NFS4ERR_RESOURCE. */
2854 			if (cstate->minorversion == 0 &&
2855 			    args->client_opcnt > resp->opcnt) {
2856 				op->status = nfserr_resource;
2857 				goto encode_op;
2858 			}
2859 		}
2860 
2861 		/*
2862 		 * The XDR decode routines may have pre-set op->status;
2863 		 * for example, if there is a miscellaneous XDR error
2864 		 * it will be set to nfserr_bad_xdr.
2865 		 */
2866 		if (op->status) {
2867 			if (op->opnum == OP_OPEN)
2868 				op->status = nfsd4_open_omfg(rqstp, cstate, op);
2869 			goto encode_op;
2870 		}
2871 		if (!current_fh->fh_dentry &&
2872 				!HAS_FH_FLAG(current_fh, NFSD4_FH_FOREIGN)) {
2873 			if (!(op->opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
2874 				op->status = nfserr_nofilehandle;
2875 				goto encode_op;
2876 			}
2877 		} else if (current_fh->fh_export &&
2878 			   current_fh->fh_export->ex_fslocs.migrated &&
2879 			  !(op->opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
2880 			op->status = nfserr_moved;
2881 			goto encode_op;
2882 		}
2883 
2884 		fh_clear_pre_post_attrs(current_fh);
2885 
2886 		/* If op is non-idempotent */
2887 		if (op->opdesc->op_flags & OP_MODIFIES_SOMETHING) {
2888 			/*
2889 			 * Don't execute this op if we couldn't encode a
2890 			 * successful reply:
2891 			 */
2892 			u32 plen = op->opdesc->op_rsize_bop(rqstp, op);
2893 			/*
2894 			 * Plus if there's another operation, make sure
2895 			 * we'll have space to at least encode an error:
2896 			 */
2897 			if (resp->opcnt < args->opcnt)
2898 				plen += COMPOUND_ERR_SLACK_SPACE;
2899 			op->status = nfsd4_check_resp_size(resp, plen);
2900 		}
2901 
2902 		if (op->status)
2903 			goto encode_op;
2904 
2905 		if (op->opdesc->op_get_currentstateid)
2906 			op->opdesc->op_get_currentstateid(cstate, &op->u);
2907 		op->status = op->opdesc->op_func(rqstp, cstate, &op->u);
2908 		trace_nfsd_compound_op_err(rqstp, op->opnum, op->status);
2909 
2910 		/* Only from SEQUENCE */
2911 		if (cstate->status == nfserr_replay_cache) {
2912 			dprintk("%s NFS4.1 replay from cache\n", __func__);
2913 			status = op->status;
2914 			goto out;
2915 		}
2916 		if (!op->status) {
2917 			if (op->opdesc->op_set_currentstateid)
2918 				op->opdesc->op_set_currentstateid(cstate, &op->u);
2919 
2920 			if (op->opdesc->op_flags & OP_CLEAR_STATEID)
2921 				clear_current_stateid(cstate);
2922 
2923 			if (current_fh->fh_export &&
2924 					need_wrongsec_check(rqstp))
2925 				op->status = check_nfsd_access(current_fh->fh_export, rqstp, false);
2926 		}
2927 encode_op:
2928 		if (op->status == nfserr_replay_me) {
2929 			op->replay = &cstate->replay_owner->so_replay;
2930 			nfsd4_encode_replay(resp->xdr, op);
2931 			status = op->status = op->replay->rp_status;
2932 		} else {
2933 			nfsd4_encode_operation(resp, op);
2934 			status = op->status;
2935 		}
2936 
2937 		trace_nfsd_compound_status(args->client_opcnt, resp->opcnt,
2938 					   status, nfsd4_op_name(op->opnum));
2939 
2940 		nfsd4_cstate_clear_replay(cstate);
2941 		nfsd4_increment_op_stats(nn, op->opnum);
2942 	}
2943 
2944 	fh_put(current_fh);
2945 	fh_put(save_fh);
2946 	BUG_ON(cstate->replay_owner);
2947 out:
2948 	cstate->status = status;
2949 	/* Reset deferral mechanism for RPC deferrals */
2950 	set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
2951 	return rpc_success;
2952 }
2953 
2954 #define op_encode_hdr_size		(2)
2955 #define op_encode_stateid_maxsz		(XDR_QUADLEN(NFS4_STATEID_SIZE))
2956 #define op_encode_verifier_maxsz	(XDR_QUADLEN(NFS4_VERIFIER_SIZE))
2957 #define op_encode_change_info_maxsz	(5)
2958 #define nfs4_fattr_bitmap_maxsz		(4)
2959 
2960 /* We'll fall back on returning no lockowner if run out of space: */
2961 #define op_encode_lockowner_maxsz	(0)
2962 #define op_encode_lock_denied_maxsz	(8 + op_encode_lockowner_maxsz)
2963 
2964 #define nfs4_owner_maxsz		(1 + XDR_QUADLEN(IDMAP_NAMESZ))
2965 
2966 #define op_encode_ace_maxsz		(3 + nfs4_owner_maxsz)
2967 #define op_encode_delegation_maxsz	(1 + op_encode_stateid_maxsz + 1 + \
2968 					 op_encode_ace_maxsz)
2969 
2970 #define op_encode_channel_attrs_maxsz	(6 + 1 + 1)
2971 
2972 /*
2973  * The _rsize() helpers are invoked by the NFSv4 COMPOUND decoder, which
2974  * is called before sunrpc sets rq_res.buflen. Thus we have to compute
2975  * the maximum payload size here, based on transport limits and the size
2976  * of the remaining space in the rq_pages array.
2977  */
2978 static u32 nfsd4_max_payload(const struct svc_rqst *rqstp)
2979 {
2980 	u32 buflen;
2981 
2982 	buflen = (rqstp->rq_page_end - rqstp->rq_next_page) * PAGE_SIZE;
2983 	buflen -= rqstp->rq_auth_slack;
2984 	buflen -= rqstp->rq_res.head[0].iov_len;
2985 	return min_t(u32, buflen, svc_max_payload(rqstp));
2986 }
2987 
2988 static u32 nfsd4_only_status_rsize(const struct svc_rqst *rqstp,
2989 				   const struct nfsd4_op *op)
2990 {
2991 	return (op_encode_hdr_size) * sizeof(__be32);
2992 }
2993 
2994 static u32 nfsd4_status_stateid_rsize(const struct svc_rqst *rqstp,
2995 				      const struct nfsd4_op *op)
2996 {
2997 	return (op_encode_hdr_size + op_encode_stateid_maxsz)* sizeof(__be32);
2998 }
2999 
3000 static u32 nfsd4_access_rsize(const struct svc_rqst *rqstp,
3001 			      const struct nfsd4_op *op)
3002 {
3003 	/* ac_supported, ac_resp_access */
3004 	return (op_encode_hdr_size + 2)* sizeof(__be32);
3005 }
3006 
3007 static u32 nfsd4_commit_rsize(const struct svc_rqst *rqstp,
3008 			      const struct nfsd4_op *op)
3009 {
3010 	return (op_encode_hdr_size + op_encode_verifier_maxsz) * sizeof(__be32);
3011 }
3012 
3013 static u32 nfsd4_create_rsize(const struct svc_rqst *rqstp,
3014 			      const struct nfsd4_op *op)
3015 {
3016 	return (op_encode_hdr_size + op_encode_change_info_maxsz
3017 		+ nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
3018 }
3019 
3020 /*
3021  * Note since this is an idempotent operation we won't insist on failing
3022  * the op prematurely if the estimate is too large.  We may turn off splice
3023  * reads unnecessarily.
3024  */
3025 static u32 nfsd4_getattr_rsize(const struct svc_rqst *rqstp,
3026 			       const struct nfsd4_op *op)
3027 {
3028 	const u32 *bmap = op->u.getattr.ga_bmval;
3029 	u32 bmap0 = bmap[0], bmap1 = bmap[1], bmap2 = bmap[2];
3030 	u32 ret = 0;
3031 
3032 	if (bmap0 & FATTR4_WORD0_ACL)
3033 		return nfsd4_max_payload(rqstp);
3034 	if (bmap0 & FATTR4_WORD0_FS_LOCATIONS)
3035 		return nfsd4_max_payload(rqstp);
3036 
3037 	if (bmap1 & FATTR4_WORD1_OWNER) {
3038 		ret += IDMAP_NAMESZ + 4;
3039 		bmap1 &= ~FATTR4_WORD1_OWNER;
3040 	}
3041 	if (bmap1 & FATTR4_WORD1_OWNER_GROUP) {
3042 		ret += IDMAP_NAMESZ + 4;
3043 		bmap1 &= ~FATTR4_WORD1_OWNER_GROUP;
3044 	}
3045 	if (bmap0 & FATTR4_WORD0_FILEHANDLE) {
3046 		ret += NFS4_FHSIZE + 4;
3047 		bmap0 &= ~FATTR4_WORD0_FILEHANDLE;
3048 	}
3049 	if (bmap2 & FATTR4_WORD2_SECURITY_LABEL) {
3050 		ret += NFS4_MAXLABELLEN + 12;
3051 		bmap2 &= ~FATTR4_WORD2_SECURITY_LABEL;
3052 	}
3053 	/*
3054 	 * Largest of remaining attributes are 16 bytes (e.g.,
3055 	 * supported_attributes)
3056 	 */
3057 	ret += 16 * (hweight32(bmap0) + hweight32(bmap1) + hweight32(bmap2));
3058 	/* bitmask, length */
3059 	ret += 20;
3060 	return ret;
3061 }
3062 
3063 static u32 nfsd4_getfh_rsize(const struct svc_rqst *rqstp,
3064 			     const struct nfsd4_op *op)
3065 {
3066 	return (op_encode_hdr_size + 1) * sizeof(__be32) + NFS4_FHSIZE;
3067 }
3068 
3069 static u32 nfsd4_link_rsize(const struct svc_rqst *rqstp,
3070 			    const struct nfsd4_op *op)
3071 {
3072 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
3073 		* sizeof(__be32);
3074 }
3075 
3076 static u32 nfsd4_lock_rsize(const struct svc_rqst *rqstp,
3077 			    const struct nfsd4_op *op)
3078 {
3079 	return (op_encode_hdr_size + op_encode_lock_denied_maxsz)
3080 		* sizeof(__be32);
3081 }
3082 
3083 static u32 nfsd4_open_rsize(const struct svc_rqst *rqstp,
3084 			    const struct nfsd4_op *op)
3085 {
3086 	return (op_encode_hdr_size + op_encode_stateid_maxsz
3087 		+ op_encode_change_info_maxsz + 1
3088 		+ nfs4_fattr_bitmap_maxsz
3089 		+ op_encode_delegation_maxsz) * sizeof(__be32);
3090 }
3091 
3092 static u32 nfsd4_read_rsize(const struct svc_rqst *rqstp,
3093 			    const struct nfsd4_op *op)
3094 {
3095 	u32 rlen = min(op->u.read.rd_length, nfsd4_max_payload(rqstp));
3096 
3097 	return (op_encode_hdr_size + 2 + XDR_QUADLEN(rlen)) * sizeof(__be32);
3098 }
3099 
3100 static u32 nfsd4_read_plus_rsize(const struct svc_rqst *rqstp,
3101 				 const struct nfsd4_op *op)
3102 {
3103 	u32 rlen = min(op->u.read.rd_length, nfsd4_max_payload(rqstp));
3104 	/*
3105 	 * If we detect that the file changed during hole encoding, then we
3106 	 * recover by encoding the remaining reply as data. This means we need
3107 	 * to set aside enough room to encode two data segments.
3108 	 */
3109 	u32 seg_len = 2 * (1 + 2 + 1);
3110 
3111 	return (op_encode_hdr_size + 2 + seg_len + XDR_QUADLEN(rlen)) * sizeof(__be32);
3112 }
3113 
3114 static u32 nfsd4_readdir_rsize(const struct svc_rqst *rqstp,
3115 			       const struct nfsd4_op *op)
3116 {
3117 	u32 rlen = min(op->u.readdir.rd_maxcount, nfsd4_max_payload(rqstp));
3118 
3119 	return (op_encode_hdr_size + op_encode_verifier_maxsz +
3120 		XDR_QUADLEN(rlen)) * sizeof(__be32);
3121 }
3122 
3123 static u32 nfsd4_readlink_rsize(const struct svc_rqst *rqstp,
3124 				const struct nfsd4_op *op)
3125 {
3126 	return (op_encode_hdr_size + 1) * sizeof(__be32) + PAGE_SIZE;
3127 }
3128 
3129 static u32 nfsd4_remove_rsize(const struct svc_rqst *rqstp,
3130 			      const struct nfsd4_op *op)
3131 {
3132 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
3133 		* sizeof(__be32);
3134 }
3135 
3136 static u32 nfsd4_rename_rsize(const struct svc_rqst *rqstp,
3137 			      const struct nfsd4_op *op)
3138 {
3139 	return (op_encode_hdr_size + op_encode_change_info_maxsz
3140 		+ op_encode_change_info_maxsz) * sizeof(__be32);
3141 }
3142 
3143 static u32 nfsd4_sequence_rsize(const struct svc_rqst *rqstp,
3144 				const struct nfsd4_op *op)
3145 {
3146 	return (op_encode_hdr_size
3147 		+ XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 5) * sizeof(__be32);
3148 }
3149 
3150 static u32 nfsd4_test_stateid_rsize(const struct svc_rqst *rqstp,
3151 				    const struct nfsd4_op *op)
3152 {
3153 	return (op_encode_hdr_size + 1 + op->u.test_stateid.ts_num_ids)
3154 		* sizeof(__be32);
3155 }
3156 
3157 static u32 nfsd4_setattr_rsize(const struct svc_rqst *rqstp,
3158 			       const struct nfsd4_op *op)
3159 {
3160 	return (op_encode_hdr_size + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
3161 }
3162 
3163 static u32 nfsd4_secinfo_rsize(const struct svc_rqst *rqstp,
3164 			       const struct nfsd4_op *op)
3165 {
3166 	return (op_encode_hdr_size + RPC_AUTH_MAXFLAVOR *
3167 		(4 + XDR_QUADLEN(GSS_OID_MAX_LEN))) * sizeof(__be32);
3168 }
3169 
3170 static u32 nfsd4_setclientid_rsize(const struct svc_rqst *rqstp,
3171 				   const struct nfsd4_op *op)
3172 {
3173 	return (op_encode_hdr_size + 2 + XDR_QUADLEN(NFS4_VERIFIER_SIZE)) *
3174 								sizeof(__be32);
3175 }
3176 
3177 static u32 nfsd4_write_rsize(const struct svc_rqst *rqstp,
3178 			     const struct nfsd4_op *op)
3179 {
3180 	return (op_encode_hdr_size + 2 + op_encode_verifier_maxsz) * sizeof(__be32);
3181 }
3182 
3183 static u32 nfsd4_exchange_id_rsize(const struct svc_rqst *rqstp,
3184 				   const struct nfsd4_op *op)
3185 {
3186 	return (op_encode_hdr_size + 2 + 1 + /* eir_clientid, eir_sequenceid */\
3187 		1 + 1 + /* eir_flags, spr_how */\
3188 		4 + /* spo_must_enforce & _allow with bitmap */\
3189 		2 + /*eir_server_owner.so_minor_id */\
3190 		/* eir_server_owner.so_major_id<> */\
3191 		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
3192 		/* eir_server_scope<> */\
3193 		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
3194 		1 + /* eir_server_impl_id array length */\
3195 		0 /* ignored eir_server_impl_id contents */) * sizeof(__be32);
3196 }
3197 
3198 static u32 nfsd4_bind_conn_to_session_rsize(const struct svc_rqst *rqstp,
3199 					    const struct nfsd4_op *op)
3200 {
3201 	return (op_encode_hdr_size + \
3202 		XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* bctsr_sessid */\
3203 		2 /* bctsr_dir, use_conn_in_rdma_mode */) * sizeof(__be32);
3204 }
3205 
3206 static u32 nfsd4_create_session_rsize(const struct svc_rqst *rqstp,
3207 				      const struct nfsd4_op *op)
3208 {
3209 	return (op_encode_hdr_size + \
3210 		XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* sessionid */\
3211 		2 + /* csr_sequence, csr_flags */\
3212 		op_encode_channel_attrs_maxsz + \
3213 		op_encode_channel_attrs_maxsz) * sizeof(__be32);
3214 }
3215 
3216 static u32 nfsd4_copy_rsize(const struct svc_rqst *rqstp,
3217 			    const struct nfsd4_op *op)
3218 {
3219 	return (op_encode_hdr_size +
3220 		1 /* wr_callback */ +
3221 		op_encode_stateid_maxsz /* wr_callback */ +
3222 		2 /* wr_count */ +
3223 		1 /* wr_committed */ +
3224 		op_encode_verifier_maxsz +
3225 		1 /* cr_consecutive */ +
3226 		1 /* cr_synchronous */) * sizeof(__be32);
3227 }
3228 
3229 static u32 nfsd4_offload_status_rsize(const struct svc_rqst *rqstp,
3230 				      const struct nfsd4_op *op)
3231 {
3232 	return (op_encode_hdr_size +
3233 		2 /* osr_count */ +
3234 		1 /* osr_complete<1> optional 0 for now */) * sizeof(__be32);
3235 }
3236 
3237 static u32 nfsd4_copy_notify_rsize(const struct svc_rqst *rqstp,
3238 				   const struct nfsd4_op *op)
3239 {
3240 	return (op_encode_hdr_size +
3241 		3 /* cnr_lease_time */ +
3242 		1 /* We support one cnr_source_server */ +
3243 		1 /* cnr_stateid seq */ +
3244 		op_encode_stateid_maxsz /* cnr_stateid */ +
3245 		1 /* num cnr_source_server*/ +
3246 		1 /* nl4_type */ +
3247 		1 /* nl4 size */ +
3248 		XDR_QUADLEN(NFS4_OPAQUE_LIMIT) /*nl4_loc + nl4_loc_sz */)
3249 		* sizeof(__be32);
3250 }
3251 
3252 static u32 nfsd4_get_dir_delegation_rsize(const struct svc_rqst *rqstp,
3253 					  const struct nfsd4_op *op)
3254 {
3255 	return (op_encode_hdr_size +
3256 		1 /* gddr_status */ +
3257 		op_encode_verifier_maxsz +
3258 		op_encode_stateid_maxsz +
3259 		2 /* gddr_notification */ +
3260 		2 /* gddr_child_attributes */ +
3261 		2 /* gddr_dir_attributes */);
3262 }
3263 
3264 #ifdef CONFIG_NFSD_PNFS
3265 static u32 nfsd4_getdeviceinfo_rsize(const struct svc_rqst *rqstp,
3266 				     const struct nfsd4_op *op)
3267 {
3268 	u32 rlen = min(op->u.getdeviceinfo.gd_maxcount, nfsd4_max_payload(rqstp));
3269 
3270 	return (op_encode_hdr_size +
3271 		1 /* gd_layout_type*/ +
3272 		XDR_QUADLEN(rlen) +
3273 		2 /* gd_notify_types */) * sizeof(__be32);
3274 }
3275 
3276 /*
3277  * At this stage we don't really know what layout driver will handle the request,
3278  * so we need to define an arbitrary upper bound here.
3279  */
3280 #define MAX_LAYOUT_SIZE		128
3281 static u32 nfsd4_layoutget_rsize(const struct svc_rqst *rqstp,
3282 				 const struct nfsd4_op *op)
3283 {
3284 	return (op_encode_hdr_size +
3285 		1 /* logr_return_on_close */ +
3286 		op_encode_stateid_maxsz +
3287 		1 /* nr of layouts */ +
3288 		MAX_LAYOUT_SIZE) * sizeof(__be32);
3289 }
3290 
3291 static u32 nfsd4_layoutcommit_rsize(const struct svc_rqst *rqstp,
3292 				    const struct nfsd4_op *op)
3293 {
3294 	return (op_encode_hdr_size +
3295 		1 /* locr_newsize */ +
3296 		2 /* ns_size */) * sizeof(__be32);
3297 }
3298 
3299 static u32 nfsd4_layoutreturn_rsize(const struct svc_rqst *rqstp,
3300 				    const struct nfsd4_op *op)
3301 {
3302 	return (op_encode_hdr_size +
3303 		1 /* lrs_stateid */ +
3304 		op_encode_stateid_maxsz) * sizeof(__be32);
3305 }
3306 #endif /* CONFIG_NFSD_PNFS */
3307 
3308 
3309 static u32 nfsd4_seek_rsize(const struct svc_rqst *rqstp,
3310 			    const struct nfsd4_op *op)
3311 {
3312 	return (op_encode_hdr_size + 3) * sizeof(__be32);
3313 }
3314 
3315 static u32 nfsd4_getxattr_rsize(const struct svc_rqst *rqstp,
3316 				const struct nfsd4_op *op)
3317 {
3318 	u32 rlen = min_t(u32, XATTR_SIZE_MAX, nfsd4_max_payload(rqstp));
3319 
3320 	return (op_encode_hdr_size + 1 + XDR_QUADLEN(rlen)) * sizeof(__be32);
3321 }
3322 
3323 static u32 nfsd4_setxattr_rsize(const struct svc_rqst *rqstp,
3324 				const struct nfsd4_op *op)
3325 {
3326 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
3327 		* sizeof(__be32);
3328 }
3329 static u32 nfsd4_listxattrs_rsize(const struct svc_rqst *rqstp,
3330 				  const struct nfsd4_op *op)
3331 {
3332 	u32 rlen = min(op->u.listxattrs.lsxa_maxcount, nfsd4_max_payload(rqstp));
3333 
3334 	return (op_encode_hdr_size + 4 + XDR_QUADLEN(rlen)) * sizeof(__be32);
3335 }
3336 
3337 static u32 nfsd4_removexattr_rsize(const struct svc_rqst *rqstp,
3338 				   const struct nfsd4_op *op)
3339 {
3340 	return (op_encode_hdr_size + op_encode_change_info_maxsz)
3341 		* sizeof(__be32);
3342 }
3343 
3344 
3345 static const struct nfsd4_operation nfsd4_ops[] = {
3346 	[OP_ACCESS] = {
3347 		.op_func = nfsd4_access,
3348 		.op_name = "OP_ACCESS",
3349 		.op_rsize_bop = nfsd4_access_rsize,
3350 	},
3351 	[OP_CLOSE] = {
3352 		.op_func = nfsd4_close,
3353 		.op_flags = OP_MODIFIES_SOMETHING,
3354 		.op_name = "OP_CLOSE",
3355 		.op_rsize_bop = nfsd4_status_stateid_rsize,
3356 		.op_get_currentstateid = nfsd4_get_closestateid,
3357 		.op_set_currentstateid = nfsd4_set_closestateid,
3358 	},
3359 	[OP_COMMIT] = {
3360 		.op_func = nfsd4_commit,
3361 		.op_flags = OP_MODIFIES_SOMETHING,
3362 		.op_name = "OP_COMMIT",
3363 		.op_rsize_bop = nfsd4_commit_rsize,
3364 	},
3365 	[OP_CREATE] = {
3366 		.op_func = nfsd4_create,
3367 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME | OP_CLEAR_STATEID,
3368 		.op_name = "OP_CREATE",
3369 		.op_rsize_bop = nfsd4_create_rsize,
3370 	},
3371 	[OP_DELEGRETURN] = {
3372 		.op_func = nfsd4_delegreturn,
3373 		.op_flags = OP_MODIFIES_SOMETHING,
3374 		.op_name = "OP_DELEGRETURN",
3375 		.op_rsize_bop = nfsd4_only_status_rsize,
3376 		.op_get_currentstateid = nfsd4_get_delegreturnstateid,
3377 	},
3378 	[OP_GETATTR] = {
3379 		.op_func = nfsd4_getattr,
3380 		.op_flags = ALLOWED_ON_ABSENT_FS,
3381 		.op_rsize_bop = nfsd4_getattr_rsize,
3382 		.op_name = "OP_GETATTR",
3383 	},
3384 	[OP_GETFH] = {
3385 		.op_func = nfsd4_getfh,
3386 		.op_name = "OP_GETFH",
3387 		.op_rsize_bop = nfsd4_getfh_rsize,
3388 	},
3389 	[OP_LINK] = {
3390 		.op_func = nfsd4_link,
3391 		.op_flags = ALLOWED_ON_ABSENT_FS | OP_MODIFIES_SOMETHING
3392 				| OP_CACHEME,
3393 		.op_name = "OP_LINK",
3394 		.op_rsize_bop = nfsd4_link_rsize,
3395 	},
3396 	[OP_LOCK] = {
3397 		.op_func = nfsd4_lock,
3398 		.op_release = nfsd4_lock_release,
3399 		.op_flags = OP_MODIFIES_SOMETHING |
3400 				OP_NONTRIVIAL_ERROR_ENCODE,
3401 		.op_name = "OP_LOCK",
3402 		.op_rsize_bop = nfsd4_lock_rsize,
3403 		.op_set_currentstateid = nfsd4_set_lockstateid,
3404 	},
3405 	[OP_LOCKT] = {
3406 		.op_func = nfsd4_lockt,
3407 		.op_release = nfsd4_lockt_release,
3408 		.op_flags = OP_NONTRIVIAL_ERROR_ENCODE,
3409 		.op_name = "OP_LOCKT",
3410 		.op_rsize_bop = nfsd4_lock_rsize,
3411 	},
3412 	[OP_LOCKU] = {
3413 		.op_func = nfsd4_locku,
3414 		.op_flags = OP_MODIFIES_SOMETHING,
3415 		.op_name = "OP_LOCKU",
3416 		.op_rsize_bop = nfsd4_status_stateid_rsize,
3417 		.op_get_currentstateid = nfsd4_get_lockustateid,
3418 	},
3419 	[OP_LOOKUP] = {
3420 		.op_func = nfsd4_lookup,
3421 		.op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
3422 		.op_name = "OP_LOOKUP",
3423 		.op_rsize_bop = nfsd4_only_status_rsize,
3424 	},
3425 	[OP_LOOKUPP] = {
3426 		.op_func = nfsd4_lookupp,
3427 		.op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
3428 		.op_name = "OP_LOOKUPP",
3429 		.op_rsize_bop = nfsd4_only_status_rsize,
3430 	},
3431 	[OP_NVERIFY] = {
3432 		.op_func = nfsd4_nverify,
3433 		.op_name = "OP_NVERIFY",
3434 		.op_rsize_bop = nfsd4_only_status_rsize,
3435 	},
3436 	[OP_OPEN] = {
3437 		.op_func = nfsd4_open,
3438 		.op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
3439 		.op_name = "OP_OPEN",
3440 		.op_rsize_bop = nfsd4_open_rsize,
3441 		.op_set_currentstateid = nfsd4_set_openstateid,
3442 	},
3443 	[OP_OPEN_CONFIRM] = {
3444 		.op_func = nfsd4_open_confirm,
3445 		.op_flags = OP_MODIFIES_SOMETHING,
3446 		.op_name = "OP_OPEN_CONFIRM",
3447 		.op_rsize_bop = nfsd4_status_stateid_rsize,
3448 	},
3449 	[OP_OPEN_DOWNGRADE] = {
3450 		.op_func = nfsd4_open_downgrade,
3451 		.op_flags = OP_MODIFIES_SOMETHING,
3452 		.op_name = "OP_OPEN_DOWNGRADE",
3453 		.op_rsize_bop = nfsd4_status_stateid_rsize,
3454 		.op_get_currentstateid = nfsd4_get_opendowngradestateid,
3455 		.op_set_currentstateid = nfsd4_set_opendowngradestateid,
3456 	},
3457 	[OP_PUTFH] = {
3458 		.op_func = nfsd4_putfh,
3459 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3460 				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
3461 		.op_name = "OP_PUTFH",
3462 		.op_rsize_bop = nfsd4_only_status_rsize,
3463 	},
3464 	[OP_PUTPUBFH] = {
3465 		.op_func = nfsd4_putrootfh,
3466 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3467 				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
3468 		.op_name = "OP_PUTPUBFH",
3469 		.op_rsize_bop = nfsd4_only_status_rsize,
3470 	},
3471 	[OP_PUTROOTFH] = {
3472 		.op_func = nfsd4_putrootfh,
3473 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3474 				| OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
3475 		.op_name = "OP_PUTROOTFH",
3476 		.op_rsize_bop = nfsd4_only_status_rsize,
3477 	},
3478 	[OP_READ] = {
3479 		.op_func = nfsd4_read,
3480 		.op_release = nfsd4_read_release,
3481 		.op_name = "OP_READ",
3482 		.op_rsize_bop = nfsd4_read_rsize,
3483 		.op_get_currentstateid = nfsd4_get_readstateid,
3484 	},
3485 	[OP_READDIR] = {
3486 		.op_func = nfsd4_readdir,
3487 		.op_name = "OP_READDIR",
3488 		.op_rsize_bop = nfsd4_readdir_rsize,
3489 	},
3490 	[OP_READLINK] = {
3491 		.op_func = nfsd4_readlink,
3492 		.op_name = "OP_READLINK",
3493 		.op_rsize_bop = nfsd4_readlink_rsize,
3494 	},
3495 	[OP_REMOVE] = {
3496 		.op_func = nfsd4_remove,
3497 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3498 		.op_name = "OP_REMOVE",
3499 		.op_rsize_bop = nfsd4_remove_rsize,
3500 	},
3501 	[OP_RENAME] = {
3502 		.op_func = nfsd4_rename,
3503 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3504 		.op_name = "OP_RENAME",
3505 		.op_rsize_bop = nfsd4_rename_rsize,
3506 	},
3507 	[OP_RENEW] = {
3508 		.op_func = nfsd4_renew,
3509 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3510 				| OP_MODIFIES_SOMETHING,
3511 		.op_name = "OP_RENEW",
3512 		.op_rsize_bop = nfsd4_only_status_rsize,
3513 
3514 	},
3515 	[OP_RESTOREFH] = {
3516 		.op_func = nfsd4_restorefh,
3517 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3518 				| OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING,
3519 		.op_name = "OP_RESTOREFH",
3520 		.op_rsize_bop = nfsd4_only_status_rsize,
3521 	},
3522 	[OP_SAVEFH] = {
3523 		.op_func = nfsd4_savefh,
3524 		.op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
3525 		.op_name = "OP_SAVEFH",
3526 		.op_rsize_bop = nfsd4_only_status_rsize,
3527 	},
3528 	[OP_SECINFO] = {
3529 		.op_func = nfsd4_secinfo,
3530 		.op_release = nfsd4_secinfo_release,
3531 		.op_flags = OP_HANDLES_WRONGSEC,
3532 		.op_name = "OP_SECINFO",
3533 		.op_rsize_bop = nfsd4_secinfo_rsize,
3534 	},
3535 	[OP_SETATTR] = {
3536 		.op_func = nfsd4_setattr,
3537 		.op_name = "OP_SETATTR",
3538 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME
3539 				| OP_NONTRIVIAL_ERROR_ENCODE,
3540 		.op_rsize_bop = nfsd4_setattr_rsize,
3541 		.op_get_currentstateid = nfsd4_get_setattrstateid,
3542 	},
3543 	[OP_SETCLIENTID] = {
3544 		.op_func = nfsd4_setclientid,
3545 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3546 				| OP_MODIFIES_SOMETHING | OP_CACHEME
3547 				| OP_NONTRIVIAL_ERROR_ENCODE,
3548 		.op_name = "OP_SETCLIENTID",
3549 		.op_rsize_bop = nfsd4_setclientid_rsize,
3550 	},
3551 	[OP_SETCLIENTID_CONFIRM] = {
3552 		.op_func = nfsd4_setclientid_confirm,
3553 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3554 				| OP_MODIFIES_SOMETHING | OP_CACHEME,
3555 		.op_name = "OP_SETCLIENTID_CONFIRM",
3556 		.op_rsize_bop = nfsd4_only_status_rsize,
3557 	},
3558 	[OP_VERIFY] = {
3559 		.op_func = nfsd4_verify,
3560 		.op_name = "OP_VERIFY",
3561 		.op_rsize_bop = nfsd4_only_status_rsize,
3562 	},
3563 	[OP_WRITE] = {
3564 		.op_func = nfsd4_write,
3565 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3566 		.op_name = "OP_WRITE",
3567 		.op_rsize_bop = nfsd4_write_rsize,
3568 		.op_get_currentstateid = nfsd4_get_writestateid,
3569 	},
3570 	[OP_RELEASE_LOCKOWNER] = {
3571 		.op_func = nfsd4_release_lockowner,
3572 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3573 				| OP_MODIFIES_SOMETHING,
3574 		.op_name = "OP_RELEASE_LOCKOWNER",
3575 		.op_rsize_bop = nfsd4_only_status_rsize,
3576 	},
3577 
3578 	/* NFSv4.1 operations */
3579 	[OP_EXCHANGE_ID] = {
3580 		.op_func = nfsd4_exchange_id,
3581 		.op_release = nfsd4_exchange_id_release,
3582 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3583 				| OP_MODIFIES_SOMETHING,
3584 		.op_name = "OP_EXCHANGE_ID",
3585 		.op_rsize_bop = nfsd4_exchange_id_rsize,
3586 	},
3587 	[OP_BACKCHANNEL_CTL] = {
3588 		.op_func = nfsd4_backchannel_ctl,
3589 		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
3590 		.op_name = "OP_BACKCHANNEL_CTL",
3591 		.op_rsize_bop = nfsd4_only_status_rsize,
3592 	},
3593 	[OP_BIND_CONN_TO_SESSION] = {
3594 		.op_func = nfsd4_bind_conn_to_session,
3595 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3596 				| OP_MODIFIES_SOMETHING,
3597 		.op_name = "OP_BIND_CONN_TO_SESSION",
3598 		.op_rsize_bop = nfsd4_bind_conn_to_session_rsize,
3599 	},
3600 	[OP_CREATE_SESSION] = {
3601 		.op_func = nfsd4_create_session,
3602 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3603 				| OP_MODIFIES_SOMETHING,
3604 		.op_name = "OP_CREATE_SESSION",
3605 		.op_rsize_bop = nfsd4_create_session_rsize,
3606 	},
3607 	[OP_DESTROY_SESSION] = {
3608 		.op_func = nfsd4_destroy_session,
3609 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3610 				| OP_MODIFIES_SOMETHING,
3611 		.op_name = "OP_DESTROY_SESSION",
3612 		.op_rsize_bop = nfsd4_only_status_rsize,
3613 	},
3614 	[OP_SEQUENCE] = {
3615 		.op_func = nfsd4_sequence,
3616 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
3617 		.op_name = "OP_SEQUENCE",
3618 		.op_rsize_bop = nfsd4_sequence_rsize,
3619 	},
3620 	[OP_DESTROY_CLIENTID] = {
3621 		.op_func = nfsd4_destroy_clientid,
3622 		.op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3623 				| OP_MODIFIES_SOMETHING,
3624 		.op_name = "OP_DESTROY_CLIENTID",
3625 		.op_rsize_bop = nfsd4_only_status_rsize,
3626 	},
3627 	[OP_RECLAIM_COMPLETE] = {
3628 		.op_func = nfsd4_reclaim_complete,
3629 		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
3630 		.op_name = "OP_RECLAIM_COMPLETE",
3631 		.op_rsize_bop = nfsd4_only_status_rsize,
3632 	},
3633 	[OP_SECINFO_NO_NAME] = {
3634 		.op_func = nfsd4_secinfo_no_name,
3635 		.op_release = nfsd4_secinfo_no_name_release,
3636 		.op_flags = OP_HANDLES_WRONGSEC,
3637 		.op_name = "OP_SECINFO_NO_NAME",
3638 		.op_rsize_bop = nfsd4_secinfo_rsize,
3639 	},
3640 	[OP_TEST_STATEID] = {
3641 		.op_func = nfsd4_test_stateid,
3642 		.op_flags = ALLOWED_WITHOUT_FH,
3643 		.op_name = "OP_TEST_STATEID",
3644 		.op_rsize_bop = nfsd4_test_stateid_rsize,
3645 	},
3646 	[OP_FREE_STATEID] = {
3647 		.op_func = nfsd4_free_stateid,
3648 		.op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
3649 		.op_name = "OP_FREE_STATEID",
3650 		.op_get_currentstateid = nfsd4_get_freestateid,
3651 		.op_rsize_bop = nfsd4_only_status_rsize,
3652 	},
3653 	[OP_GET_DIR_DELEGATION] = {
3654 		.op_func = nfsd4_get_dir_delegation,
3655 		.op_flags = OP_MODIFIES_SOMETHING,
3656 		.op_name = "OP_GET_DIR_DELEGATION",
3657 		.op_rsize_bop = nfsd4_get_dir_delegation_rsize,
3658 	},
3659 #ifdef CONFIG_NFSD_PNFS
3660 	[OP_GETDEVICEINFO] = {
3661 		.op_func = nfsd4_getdeviceinfo,
3662 		.op_release = nfsd4_getdeviceinfo_release,
3663 		.op_flags = ALLOWED_WITHOUT_FH,
3664 		.op_name = "OP_GETDEVICEINFO",
3665 		.op_rsize_bop = nfsd4_getdeviceinfo_rsize,
3666 	},
3667 	[OP_LAYOUTGET] = {
3668 		.op_func = nfsd4_layoutget,
3669 		.op_release = nfsd4_layoutget_release,
3670 		.op_flags = OP_MODIFIES_SOMETHING,
3671 		.op_name = "OP_LAYOUTGET",
3672 		.op_rsize_bop = nfsd4_layoutget_rsize,
3673 	},
3674 	[OP_LAYOUTCOMMIT] = {
3675 		.op_func = nfsd4_layoutcommit,
3676 		.op_flags = OP_MODIFIES_SOMETHING,
3677 		.op_name = "OP_LAYOUTCOMMIT",
3678 		.op_rsize_bop = nfsd4_layoutcommit_rsize,
3679 	},
3680 	[OP_LAYOUTRETURN] = {
3681 		.op_func = nfsd4_layoutreturn,
3682 		.op_flags = OP_MODIFIES_SOMETHING,
3683 		.op_name = "OP_LAYOUTRETURN",
3684 		.op_rsize_bop = nfsd4_layoutreturn_rsize,
3685 	},
3686 #endif /* CONFIG_NFSD_PNFS */
3687 
3688 	/* NFSv4.2 operations */
3689 	[OP_ALLOCATE] = {
3690 		.op_func = nfsd4_allocate,
3691 		.op_flags = OP_MODIFIES_SOMETHING,
3692 		.op_name = "OP_ALLOCATE",
3693 		.op_rsize_bop = nfsd4_only_status_rsize,
3694 	},
3695 	[OP_DEALLOCATE] = {
3696 		.op_func = nfsd4_deallocate,
3697 		.op_flags = OP_MODIFIES_SOMETHING,
3698 		.op_name = "OP_DEALLOCATE",
3699 		.op_rsize_bop = nfsd4_only_status_rsize,
3700 	},
3701 	[OP_CLONE] = {
3702 		.op_func = nfsd4_clone,
3703 		.op_flags = OP_MODIFIES_SOMETHING,
3704 		.op_name = "OP_CLONE",
3705 		.op_rsize_bop = nfsd4_only_status_rsize,
3706 	},
3707 	[OP_COPY] = {
3708 		.op_func = nfsd4_copy,
3709 		.op_flags = OP_MODIFIES_SOMETHING,
3710 		.op_name = "OP_COPY",
3711 		.op_rsize_bop = nfsd4_copy_rsize,
3712 	},
3713 	[OP_READ_PLUS] = {
3714 		.op_func = nfsd4_read,
3715 		.op_release = nfsd4_read_release,
3716 		.op_name = "OP_READ_PLUS",
3717 		.op_rsize_bop = nfsd4_read_plus_rsize,
3718 		.op_get_currentstateid = nfsd4_get_readstateid,
3719 	},
3720 	[OP_SEEK] = {
3721 		.op_func = nfsd4_seek,
3722 		.op_name = "OP_SEEK",
3723 		.op_rsize_bop = nfsd4_seek_rsize,
3724 	},
3725 	[OP_OFFLOAD_STATUS] = {
3726 		.op_func = nfsd4_offload_status,
3727 		.op_name = "OP_OFFLOAD_STATUS",
3728 		.op_rsize_bop = nfsd4_offload_status_rsize,
3729 	},
3730 	[OP_OFFLOAD_CANCEL] = {
3731 		.op_func = nfsd4_offload_cancel,
3732 		.op_flags = OP_MODIFIES_SOMETHING,
3733 		.op_name = "OP_OFFLOAD_CANCEL",
3734 		.op_rsize_bop = nfsd4_only_status_rsize,
3735 	},
3736 	[OP_COPY_NOTIFY] = {
3737 		.op_func = nfsd4_copy_notify,
3738 		.op_flags = OP_MODIFIES_SOMETHING,
3739 		.op_name = "OP_COPY_NOTIFY",
3740 		.op_rsize_bop = nfsd4_copy_notify_rsize,
3741 	},
3742 	[OP_GETXATTR] = {
3743 		.op_func = nfsd4_getxattr,
3744 		.op_name = "OP_GETXATTR",
3745 		.op_rsize_bop = nfsd4_getxattr_rsize,
3746 	},
3747 	[OP_SETXATTR] = {
3748 		.op_func = nfsd4_setxattr,
3749 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3750 		.op_name = "OP_SETXATTR",
3751 		.op_rsize_bop = nfsd4_setxattr_rsize,
3752 	},
3753 	[OP_LISTXATTRS] = {
3754 		.op_func = nfsd4_listxattrs,
3755 		.op_name = "OP_LISTXATTRS",
3756 		.op_rsize_bop = nfsd4_listxattrs_rsize,
3757 	},
3758 	[OP_REMOVEXATTR] = {
3759 		.op_func = nfsd4_removexattr,
3760 		.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3761 		.op_name = "OP_REMOVEXATTR",
3762 		.op_rsize_bop = nfsd4_removexattr_rsize,
3763 	},
3764 };
3765 
3766 /**
3767  * nfsd4_spo_must_allow - Determine if the compound op contains an
3768  * operation that is allowed to be sent with machine credentials
3769  *
3770  * @rqstp: a pointer to the struct svc_rqst
3771  *
3772  * Checks to see if the compound contains a spo_must_allow op
3773  * and confirms that it was sent with the proper machine creds.
3774  */
3775 
3776 bool nfsd4_spo_must_allow(struct svc_rqst *rqstp)
3777 {
3778 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
3779 	struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3780 	struct nfsd4_op *this;
3781 	struct nfsd4_compound_state *cstate = &resp->cstate;
3782 	struct nfs4_op_map *allow = &cstate->clp->cl_spo_must_allow;
3783 	u32 opiter;
3784 
3785 	if (rqstp->rq_procinfo != &nfsd_version4.vs_proc[NFSPROC4_COMPOUND] ||
3786 	    cstate->minorversion == 0)
3787 		return false;
3788 
3789 	if (cstate->spo_must_allowed)
3790 		return true;
3791 
3792 	opiter = resp->opcnt;
3793 	while (opiter < argp->opcnt) {
3794 		this = &argp->ops[opiter++];
3795 		if (test_bit(this->opnum, allow->u.longs) &&
3796 			cstate->clp->cl_mach_cred &&
3797 			nfsd4_mach_creds_match(cstate->clp, rqstp)) {
3798 			cstate->spo_must_allowed = true;
3799 			return true;
3800 		}
3801 	}
3802 	cstate->spo_must_allowed = false;
3803 	return false;
3804 }
3805 
3806 int nfsd4_max_reply(struct svc_rqst *rqstp, struct nfsd4_op *op)
3807 {
3808 	if (op->opnum == OP_ILLEGAL || op->status == nfserr_notsupp)
3809 		return op_encode_hdr_size * sizeof(__be32);
3810 
3811 	BUG_ON(OPDESC(op)->op_rsize_bop == NULL);
3812 	return OPDESC(op)->op_rsize_bop(rqstp, op);
3813 }
3814 
3815 void warn_on_nonidempotent_op(struct nfsd4_op *op)
3816 {
3817 	if (OPDESC(op)->op_flags & OP_MODIFIES_SOMETHING) {
3818 		pr_err("unable to encode reply to nonidempotent op %u (%s)\n",
3819 			op->opnum, nfsd4_op_name(op->opnum));
3820 		WARN_ON_ONCE(1);
3821 	}
3822 }
3823 
3824 static const char *nfsd4_op_name(unsigned opnum)
3825 {
3826 	if (opnum < ARRAY_SIZE(nfsd4_ops))
3827 		return nfsd4_ops[opnum].op_name;
3828 	return "unknown_operation";
3829 }
3830 
3831 static const struct svc_procedure nfsd_procedures4[2] = {
3832 	[NFSPROC4_NULL] = {
3833 		.pc_func = nfsd4_proc_null,
3834 		.pc_decode = nfssvc_decode_voidarg,
3835 		.pc_encode = nfssvc_encode_voidres,
3836 		.pc_argsize = sizeof(struct nfsd_voidargs),
3837 		.pc_argzero = sizeof(struct nfsd_voidargs),
3838 		.pc_ressize = sizeof(struct nfsd_voidres),
3839 		.pc_cachetype = RC_NOCACHE,
3840 		.pc_xdrressize = 1,
3841 		.pc_name = "NULL",
3842 	},
3843 	[NFSPROC4_COMPOUND] = {
3844 		.pc_func = nfsd4_proc_compound,
3845 		.pc_decode = nfs4svc_decode_compoundargs,
3846 		.pc_encode = nfs4svc_encode_compoundres,
3847 		.pc_argsize = sizeof(struct nfsd4_compoundargs),
3848 		.pc_argzero = offsetof(struct nfsd4_compoundargs, iops),
3849 		.pc_ressize = sizeof(struct nfsd4_compoundres),
3850 		.pc_release = nfsd4_release_compoundargs,
3851 		.pc_cachetype = RC_NOCACHE,
3852 		.pc_xdrressize = NFSD_BUFSIZE/4,
3853 		.pc_name = "COMPOUND",
3854 	},
3855 };
3856 
3857 static DEFINE_PER_CPU_ALIGNED(unsigned long,
3858 			      nfsd_count4[ARRAY_SIZE(nfsd_procedures4)]);
3859 const struct svc_version nfsd_version4 = {
3860 	.vs_vers		= 4,
3861 	.vs_nproc		= ARRAY_SIZE(nfsd_procedures4),
3862 	.vs_proc		= nfsd_procedures4,
3863 	.vs_count		= nfsd_count4,
3864 	.vs_dispatch		= nfsd_dispatch,
3865 	.vs_xdrsize		= NFS4_SVC_XDRSIZE,
3866 	.vs_rpcb_optnl		= true,
3867 	.vs_need_cong_ctrl	= true,
3868 };
3869