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