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