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