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