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