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