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