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