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