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