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