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