Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  *  Server-side procedures for NFSv4.
0003  *
0004  *  Copyright (c) 2002 The Regents of the University of Michigan.
0005  *  All rights reserved.
0006  *
0007  *  Kendrick Smith <kmsmith@umich.edu>
0008  *  Andy Adamson   <andros@umich.edu>
0009  *
0010  *  Redistribution and use in source and binary forms, with or without
0011  *  modification, are permitted provided that the following conditions
0012  *  are met:
0013  *
0014  *  1. Redistributions of source code must retain the above copyright
0015  *     notice, this list of conditions and the following disclaimer.
0016  *  2. Redistributions in binary form must reproduce the above copyright
0017  *     notice, this list of conditions and the following disclaimer in the
0018  *     documentation and/or other materials provided with the distribution.
0019  *  3. Neither the name of the University nor the names of its
0020  *     contributors may be used to endorse or promote products derived
0021  *     from this software without specific prior written permission.
0022  *
0023  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
0024  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
0025  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
0026  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
0027  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0028  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0029  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
0030  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0031  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0032  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0033  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0034  */
0035 #include <linux/fs_struct.h>
0036 #include <linux/file.h>
0037 #include <linux/falloc.h>
0038 #include <linux/slab.h>
0039 #include <linux/kthread.h>
0040 #include <linux/namei.h>
0041 
0042 #include <linux/sunrpc/addr.h>
0043 #include <linux/nfs_ssc.h>
0044 
0045 #include "idmap.h"
0046 #include "cache.h"
0047 #include "xdr4.h"
0048 #include "vfs.h"
0049 #include "current_stateid.h"
0050 #include "netns.h"
0051 #include "acl.h"
0052 #include "pnfs.h"
0053 #include "trace.h"
0054 
0055 static bool inter_copy_offload_enable;
0056 module_param(inter_copy_offload_enable, bool, 0644);
0057 MODULE_PARM_DESC(inter_copy_offload_enable,
0058          "Enable inter server to server copy offload. Default: false");
0059 
0060 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
0061 static int nfsd4_ssc_umount_timeout = 900000;       /* default to 15 mins */
0062 module_param(nfsd4_ssc_umount_timeout, int, 0644);
0063 MODULE_PARM_DESC(nfsd4_ssc_umount_timeout,
0064         "idle msecs before unmount export from source server");
0065 #endif
0066 
0067 #define NFSDDBG_FACILITY        NFSDDBG_PROC
0068 
0069 static u32 nfsd_attrmask[] = {
0070     NFSD_WRITEABLE_ATTRS_WORD0,
0071     NFSD_WRITEABLE_ATTRS_WORD1,
0072     NFSD_WRITEABLE_ATTRS_WORD2
0073 };
0074 
0075 static u32 nfsd41_ex_attrmask[] = {
0076     NFSD_SUPPATTR_EXCLCREAT_WORD0,
0077     NFSD_SUPPATTR_EXCLCREAT_WORD1,
0078     NFSD_SUPPATTR_EXCLCREAT_WORD2
0079 };
0080 
0081 static __be32
0082 check_attr_support(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
0083            u32 *bmval, u32 *writable)
0084 {
0085     struct dentry *dentry = cstate->current_fh.fh_dentry;
0086     struct svc_export *exp = cstate->current_fh.fh_export;
0087 
0088     if (!nfsd_attrs_supported(cstate->minorversion, bmval))
0089         return nfserr_attrnotsupp;
0090     if ((bmval[0] & FATTR4_WORD0_ACL) && !IS_POSIXACL(d_inode(dentry)))
0091         return nfserr_attrnotsupp;
0092     if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) &&
0093             !(exp->ex_flags & NFSEXP_SECURITY_LABEL))
0094         return nfserr_attrnotsupp;
0095     if (writable && !bmval_is_subset(bmval, writable))
0096         return nfserr_inval;
0097     if (writable && (bmval[2] & FATTR4_WORD2_MODE_UMASK) &&
0098             (bmval[1] & FATTR4_WORD1_MODE))
0099         return nfserr_inval;
0100     return nfs_ok;
0101 }
0102 
0103 static __be32
0104 nfsd4_check_open_attributes(struct svc_rqst *rqstp,
0105     struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
0106 {
0107     __be32 status = nfs_ok;
0108 
0109     if (open->op_create == NFS4_OPEN_CREATE) {
0110         if (open->op_createmode == NFS4_CREATE_UNCHECKED
0111             || open->op_createmode == NFS4_CREATE_GUARDED)
0112             status = check_attr_support(rqstp, cstate,
0113                     open->op_bmval, nfsd_attrmask);
0114         else if (open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1)
0115             status = check_attr_support(rqstp, cstate,
0116                     open->op_bmval, nfsd41_ex_attrmask);
0117     }
0118 
0119     return status;
0120 }
0121 
0122 static int
0123 is_create_with_attrs(struct nfsd4_open *open)
0124 {
0125     return open->op_create == NFS4_OPEN_CREATE
0126         && (open->op_createmode == NFS4_CREATE_UNCHECKED
0127             || open->op_createmode == NFS4_CREATE_GUARDED
0128             || open->op_createmode == NFS4_CREATE_EXCLUSIVE4_1);
0129 }
0130 
0131 static inline void
0132 fh_dup2(struct svc_fh *dst, struct svc_fh *src)
0133 {
0134     fh_put(dst);
0135     dget(src->fh_dentry);
0136     if (src->fh_export)
0137         exp_get(src->fh_export);
0138     *dst = *src;
0139 }
0140 
0141 static __be32
0142 do_open_permission(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open, int accmode)
0143 {
0144     __be32 status;
0145 
0146     if (open->op_truncate &&
0147         !(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
0148         return nfserr_inval;
0149 
0150     accmode |= NFSD_MAY_READ_IF_EXEC;
0151 
0152     if (open->op_share_access & NFS4_SHARE_ACCESS_READ)
0153         accmode |= NFSD_MAY_READ;
0154     if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
0155         accmode |= (NFSD_MAY_WRITE | NFSD_MAY_TRUNC);
0156     if (open->op_share_deny & NFS4_SHARE_DENY_READ)
0157         accmode |= NFSD_MAY_WRITE;
0158 
0159     status = fh_verify(rqstp, current_fh, S_IFREG, accmode);
0160 
0161     return status;
0162 }
0163 
0164 static __be32 nfsd_check_obj_isreg(struct svc_fh *fh)
0165 {
0166     umode_t mode = d_inode(fh->fh_dentry)->i_mode;
0167 
0168     if (S_ISREG(mode))
0169         return nfs_ok;
0170     if (S_ISDIR(mode))
0171         return nfserr_isdir;
0172     /*
0173      * Using err_symlink as our catch-all case may look odd; but
0174      * there's no other obvious error for this case in 4.0, and we
0175      * happen to know that it will cause the linux v4 client to do
0176      * the right thing on attempts to open something other than a
0177      * regular file.
0178      */
0179     return nfserr_symlink;
0180 }
0181 
0182 static void nfsd4_set_open_owner_reply_cache(struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh *resfh)
0183 {
0184     if (nfsd4_has_session(cstate))
0185         return;
0186     fh_copy_shallow(&open->op_openowner->oo_owner.so_replay.rp_openfh,
0187             &resfh->fh_handle);
0188 }
0189 
0190 static inline bool nfsd4_create_is_exclusive(int createmode)
0191 {
0192     return createmode == NFS4_CREATE_EXCLUSIVE ||
0193         createmode == NFS4_CREATE_EXCLUSIVE4_1;
0194 }
0195 
0196 static __be32
0197 nfsd4_vfs_create(struct svc_fh *fhp, struct dentry *child,
0198          struct nfsd4_open *open)
0199 {
0200     struct file *filp;
0201     struct path path;
0202     int oflags;
0203 
0204     oflags = O_CREAT | O_LARGEFILE;
0205     switch (open->op_share_access & NFS4_SHARE_ACCESS_BOTH) {
0206     case NFS4_SHARE_ACCESS_WRITE:
0207         oflags |= O_WRONLY;
0208         break;
0209     case NFS4_SHARE_ACCESS_BOTH:
0210         oflags |= O_RDWR;
0211         break;
0212     default:
0213         oflags |= O_RDONLY;
0214     }
0215 
0216     path.mnt = fhp->fh_export->ex_path.mnt;
0217     path.dentry = child;
0218     filp = dentry_create(&path, oflags, open->op_iattr.ia_mode,
0219                  current_cred());
0220     if (IS_ERR(filp))
0221         return nfserrno(PTR_ERR(filp));
0222 
0223     open->op_filp = filp;
0224     return nfs_ok;
0225 }
0226 
0227 /*
0228  * Implement NFSv4's unchecked, guarded, and exclusive create
0229  * semantics for regular files. Open state for this new file is
0230  * subsequently fabricated in nfsd4_process_open2().
0231  *
0232  * Upon return, caller must release @fhp and @resfhp.
0233  */
0234 static __be32
0235 nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
0236           struct svc_fh *resfhp, struct nfsd4_open *open)
0237 {
0238     struct iattr *iap = &open->op_iattr;
0239     struct nfsd_attrs attrs = {
0240         .na_iattr   = iap,
0241         .na_seclabel    = &open->op_label,
0242     };
0243     struct dentry *parent, *child;
0244     __u32 v_mtime, v_atime;
0245     struct inode *inode;
0246     __be32 status;
0247     int host_err;
0248 
0249     if (isdotent(open->op_fname, open->op_fnamelen))
0250         return nfserr_exist;
0251     if (!(iap->ia_valid & ATTR_MODE))
0252         iap->ia_mode = 0;
0253 
0254     status = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
0255     if (status != nfs_ok)
0256         return status;
0257     parent = fhp->fh_dentry;
0258     inode = d_inode(parent);
0259 
0260     host_err = fh_want_write(fhp);
0261     if (host_err)
0262         return nfserrno(host_err);
0263 
0264     if (is_create_with_attrs(open))
0265         nfsd4_acl_to_attr(NF4REG, open->op_acl, &attrs);
0266 
0267     inode_lock_nested(inode, I_MUTEX_PARENT);
0268 
0269     child = lookup_one_len(open->op_fname, parent, open->op_fnamelen);
0270     if (IS_ERR(child)) {
0271         status = nfserrno(PTR_ERR(child));
0272         goto out;
0273     }
0274 
0275     if (d_really_is_negative(child)) {
0276         status = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
0277         if (status != nfs_ok)
0278             goto out;
0279     }
0280 
0281     status = fh_compose(resfhp, fhp->fh_export, child, fhp);
0282     if (status != nfs_ok)
0283         goto out;
0284 
0285     v_mtime = 0;
0286     v_atime = 0;
0287     if (nfsd4_create_is_exclusive(open->op_createmode)) {
0288         u32 *verifier = (u32 *)open->op_verf.data;
0289 
0290         /*
0291          * Solaris 7 gets confused (bugid 4218508) if these have
0292          * the high bit set, as do xfs filesystems without the
0293          * "bigtime" feature. So just clear the high bits. If this
0294          * is ever changed to use different attrs for storing the
0295          * verifier, then do_open_lookup() will also need to be
0296          * fixed accordingly.
0297          */
0298         v_mtime = verifier[0] & 0x7fffffff;
0299         v_atime = verifier[1] & 0x7fffffff;
0300     }
0301 
0302     if (d_really_is_positive(child)) {
0303         status = nfs_ok;
0304 
0305         /* NFSv4 protocol requires change attributes even though
0306          * no change happened.
0307          */
0308         fh_fill_both_attrs(fhp);
0309 
0310         switch (open->op_createmode) {
0311         case NFS4_CREATE_UNCHECKED:
0312             if (!d_is_reg(child))
0313                 break;
0314 
0315             /*
0316              * In NFSv4, we don't want to truncate the file
0317              * now. This would be wrong if the OPEN fails for
0318              * some other reason. Furthermore, if the size is
0319              * nonzero, we should ignore it according to spec!
0320              */
0321             open->op_truncate = (iap->ia_valid & ATTR_SIZE) &&
0322                         !iap->ia_size;
0323             break;
0324         case NFS4_CREATE_GUARDED:
0325             status = nfserr_exist;
0326             break;
0327         case NFS4_CREATE_EXCLUSIVE:
0328             if (d_inode(child)->i_mtime.tv_sec == v_mtime &&
0329                 d_inode(child)->i_atime.tv_sec == v_atime &&
0330                 d_inode(child)->i_size == 0) {
0331                 open->op_created = true;
0332                 break;      /* subtle */
0333             }
0334             status = nfserr_exist;
0335             break;
0336         case NFS4_CREATE_EXCLUSIVE4_1:
0337             if (d_inode(child)->i_mtime.tv_sec == v_mtime &&
0338                 d_inode(child)->i_atime.tv_sec == v_atime &&
0339                 d_inode(child)->i_size == 0) {
0340                 open->op_created = true;
0341                 goto set_attr;  /* subtle */
0342             }
0343             status = nfserr_exist;
0344         }
0345         goto out;
0346     }
0347 
0348     if (!IS_POSIXACL(inode))
0349         iap->ia_mode &= ~current_umask();
0350 
0351     fh_fill_pre_attrs(fhp);
0352     status = nfsd4_vfs_create(fhp, child, open);
0353     if (status != nfs_ok)
0354         goto out;
0355     open->op_created = true;
0356     fh_fill_post_attrs(fhp);
0357 
0358     /* A newly created file already has a file size of zero. */
0359     if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
0360         iap->ia_valid &= ~ATTR_SIZE;
0361     if (nfsd4_create_is_exclusive(open->op_createmode)) {
0362         iap->ia_valid = ATTR_MTIME | ATTR_ATIME |
0363                 ATTR_MTIME_SET|ATTR_ATIME_SET;
0364         iap->ia_mtime.tv_sec = v_mtime;
0365         iap->ia_atime.tv_sec = v_atime;
0366         iap->ia_mtime.tv_nsec = 0;
0367         iap->ia_atime.tv_nsec = 0;
0368     }
0369 
0370 set_attr:
0371     status = nfsd_create_setattr(rqstp, fhp, resfhp, &attrs);
0372 
0373     if (attrs.na_labelerr)
0374         open->op_bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
0375     if (attrs.na_aclerr)
0376         open->op_bmval[0] &= ~FATTR4_WORD0_ACL;
0377 out:
0378     inode_unlock(inode);
0379     nfsd_attrs_free(&attrs);
0380     if (child && !IS_ERR(child))
0381         dput(child);
0382     fh_drop_write(fhp);
0383     return status;
0384 }
0385 
0386 static __be32
0387 do_open_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open, struct svc_fh **resfh)
0388 {
0389     struct svc_fh *current_fh = &cstate->current_fh;
0390     int accmode;
0391     __be32 status;
0392 
0393     *resfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
0394     if (!*resfh)
0395         return nfserr_jukebox;
0396     fh_init(*resfh, NFS4_FHSIZE);
0397     open->op_truncate = false;
0398 
0399     if (open->op_create) {
0400         /* FIXME: check session persistence and pnfs flags.
0401          * The nfsv4.1 spec requires the following semantics:
0402          *
0403          * Persistent   | pNFS   | Server REQUIRED | Client Allowed
0404          * Reply Cache  | server |                 |
0405          * -------------+--------+-----------------+--------------------
0406          * no           | no     | EXCLUSIVE4_1    | EXCLUSIVE4_1
0407          *              |        |                 | (SHOULD)
0408          *              |        | and EXCLUSIVE4  | or EXCLUSIVE4
0409          *              |        |                 | (SHOULD NOT)
0410          * no           | yes    | EXCLUSIVE4_1    | EXCLUSIVE4_1
0411          * yes          | no     | GUARDED4        | GUARDED4
0412          * yes          | yes    | GUARDED4        | GUARDED4
0413          */
0414 
0415         current->fs->umask = open->op_umask;
0416         status = nfsd4_create_file(rqstp, current_fh, *resfh, open);
0417         current->fs->umask = 0;
0418 
0419         /*
0420          * Following rfc 3530 14.2.16, and rfc 5661 18.16.4
0421          * use the returned bitmask to indicate which attributes
0422          * we used to store the verifier:
0423          */
0424         if (nfsd4_create_is_exclusive(open->op_createmode) && status == 0)
0425             open->op_bmval[1] |= (FATTR4_WORD1_TIME_ACCESS |
0426                         FATTR4_WORD1_TIME_MODIFY);
0427     } else {
0428         status = nfsd_lookup(rqstp, current_fh,
0429                      open->op_fname, open->op_fnamelen, *resfh);
0430         if (!status)
0431             /* NFSv4 protocol requires change attributes even though
0432              * no change happened.
0433              */
0434             fh_fill_both_attrs(current_fh);
0435     }
0436     if (status)
0437         goto out;
0438     status = nfsd_check_obj_isreg(*resfh);
0439     if (status)
0440         goto out;
0441 
0442     nfsd4_set_open_owner_reply_cache(cstate, open, *resfh);
0443     accmode = NFSD_MAY_NOP;
0444     if (open->op_created ||
0445             open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
0446         accmode |= NFSD_MAY_OWNER_OVERRIDE;
0447     status = do_open_permission(rqstp, *resfh, open, accmode);
0448     set_change_info(&open->op_cinfo, current_fh);
0449 out:
0450     return status;
0451 }
0452 
0453 static __be32
0454 do_open_fhandle(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
0455 {
0456     struct svc_fh *current_fh = &cstate->current_fh;
0457     __be32 status;
0458     int accmode = 0;
0459 
0460     /* We don't know the target directory, and therefore can not
0461     * set the change info
0462     */
0463 
0464     memset(&open->op_cinfo, 0, sizeof(struct nfsd4_change_info));
0465 
0466     nfsd4_set_open_owner_reply_cache(cstate, open, current_fh);
0467 
0468     open->op_truncate = (open->op_iattr.ia_valid & ATTR_SIZE) &&
0469         (open->op_iattr.ia_size == 0);
0470     /*
0471      * In the delegation case, the client is telling us about an
0472      * open that it *already* performed locally, some time ago.  We
0473      * should let it succeed now if possible.
0474      *
0475      * In the case of a CLAIM_FH open, on the other hand, the client
0476      * may be counting on us to enforce permissions (the Linux 4.1
0477      * client uses this for normal opens, for example).
0478      */
0479     if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH)
0480         accmode = NFSD_MAY_OWNER_OVERRIDE;
0481 
0482     status = do_open_permission(rqstp, current_fh, open, accmode);
0483 
0484     return status;
0485 }
0486 
0487 static void
0488 copy_clientid(clientid_t *clid, struct nfsd4_session *session)
0489 {
0490     struct nfsd4_sessionid *sid =
0491             (struct nfsd4_sessionid *)session->se_sessionid.data;
0492 
0493     clid->cl_boot = sid->clientid.cl_boot;
0494     clid->cl_id = sid->clientid.cl_id;
0495 }
0496 
0497 static __be32
0498 nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
0499        union nfsd4_op_u *u)
0500 {
0501     struct nfsd4_open *open = &u->open;
0502     __be32 status;
0503     struct svc_fh *resfh = NULL;
0504     struct net *net = SVC_NET(rqstp);
0505     struct nfsd_net *nn = net_generic(net, nfsd_net_id);
0506     bool reclaim = false;
0507 
0508     dprintk("NFSD: nfsd4_open filename %.*s op_openowner %p\n",
0509         (int)open->op_fnamelen, open->op_fname,
0510         open->op_openowner);
0511 
0512     open->op_filp = NULL;
0513     open->op_rqstp = rqstp;
0514 
0515     /* This check required by spec. */
0516     if (open->op_create && open->op_claim_type != NFS4_OPEN_CLAIM_NULL)
0517         return nfserr_inval;
0518 
0519     open->op_created = false;
0520     /*
0521      * RFC5661 18.51.3
0522      * Before RECLAIM_COMPLETE done, server should deny new lock
0523      */
0524     if (nfsd4_has_session(cstate) &&
0525         !test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags) &&
0526         open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
0527         return nfserr_grace;
0528 
0529     if (nfsd4_has_session(cstate))
0530         copy_clientid(&open->op_clientid, cstate->session);
0531 
0532     /* check seqid for replay. set nfs4_owner */
0533     status = nfsd4_process_open1(cstate, open, nn);
0534     if (status == nfserr_replay_me) {
0535         struct nfs4_replay *rp = &open->op_openowner->oo_owner.so_replay;
0536         fh_put(&cstate->current_fh);
0537         fh_copy_shallow(&cstate->current_fh.fh_handle,
0538                 &rp->rp_openfh);
0539         status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
0540         if (status)
0541             dprintk("nfsd4_open: replay failed"
0542                 " restoring previous filehandle\n");
0543         else
0544             status = nfserr_replay_me;
0545     }
0546     if (status)
0547         goto out;
0548     if (open->op_xdr_error) {
0549         status = open->op_xdr_error;
0550         goto out;
0551     }
0552 
0553     status = nfsd4_check_open_attributes(rqstp, cstate, open);
0554     if (status)
0555         goto out;
0556 
0557     /* Openowner is now set, so sequence id will get bumped.  Now we need
0558      * these checks before we do any creates: */
0559     status = nfserr_grace;
0560     if (opens_in_grace(net) && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS)
0561         goto out;
0562     status = nfserr_no_grace;
0563     if (!opens_in_grace(net) && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
0564         goto out;
0565 
0566     switch (open->op_claim_type) {
0567     case NFS4_OPEN_CLAIM_DELEGATE_CUR:
0568     case NFS4_OPEN_CLAIM_NULL:
0569         status = do_open_lookup(rqstp, cstate, open, &resfh);
0570         if (status)
0571             goto out;
0572         break;
0573     case NFS4_OPEN_CLAIM_PREVIOUS:
0574         status = nfs4_check_open_reclaim(cstate->clp);
0575         if (status)
0576             goto out;
0577         open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
0578         reclaim = true;
0579         fallthrough;
0580     case NFS4_OPEN_CLAIM_FH:
0581     case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
0582         status = do_open_fhandle(rqstp, cstate, open);
0583         if (status)
0584             goto out;
0585         resfh = &cstate->current_fh;
0586         break;
0587     case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
0588     case NFS4_OPEN_CLAIM_DELEGATE_PREV:
0589         status = nfserr_notsupp;
0590         goto out;
0591     default:
0592         status = nfserr_inval;
0593         goto out;
0594     }
0595 
0596     status = nfsd4_process_open2(rqstp, resfh, open);
0597     if (status && open->op_created)
0598         pr_warn("nfsd4_process_open2 failed to open newly-created file: status=%u\n",
0599             be32_to_cpu(status));
0600     if (reclaim && !status)
0601         nn->somebody_reclaimed = true;
0602 out:
0603     if (open->op_filp) {
0604         fput(open->op_filp);
0605         open->op_filp = NULL;
0606     }
0607     if (resfh && resfh != &cstate->current_fh) {
0608         fh_dup2(&cstate->current_fh, resfh);
0609         fh_put(resfh);
0610         kfree(resfh);
0611     }
0612     nfsd4_cleanup_open_state(cstate, open);
0613     nfsd4_bump_seqid(cstate, status);
0614     return status;
0615 }
0616 
0617 /*
0618  * OPEN is the only seqid-mutating operation whose decoding can fail
0619  * with a seqid-mutating error (specifically, decoding of user names in
0620  * the attributes).  Therefore we have to do some processing to look up
0621  * the stateowner so that we can bump the seqid.
0622  */
0623 static __be32 nfsd4_open_omfg(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_op *op)
0624 {
0625     struct nfsd4_open *open = &op->u.open;
0626 
0627     if (!seqid_mutating_err(ntohl(op->status)))
0628         return op->status;
0629     if (nfsd4_has_session(cstate))
0630         return op->status;
0631     open->op_xdr_error = op->status;
0632     return nfsd4_open(rqstp, cstate, &op->u);
0633 }
0634 
0635 /*
0636  * filehandle-manipulating ops.
0637  */
0638 static __be32
0639 nfsd4_getfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
0640         union nfsd4_op_u *u)
0641 {
0642     u->getfh = &cstate->current_fh;
0643     return nfs_ok;
0644 }
0645 
0646 static __be32
0647 nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
0648         union nfsd4_op_u *u)
0649 {
0650     struct nfsd4_putfh *putfh = &u->putfh;
0651     __be32 ret;
0652 
0653     fh_put(&cstate->current_fh);
0654     cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
0655     memcpy(&cstate->current_fh.fh_handle.fh_raw, putfh->pf_fhval,
0656            putfh->pf_fhlen);
0657     ret = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_BYPASS_GSS);
0658 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
0659     if (ret == nfserr_stale && putfh->no_verify) {
0660         SET_FH_FLAG(&cstate->current_fh, NFSD4_FH_FOREIGN);
0661         ret = 0;
0662     }
0663 #endif
0664     return ret;
0665 }
0666 
0667 static __be32
0668 nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
0669         union nfsd4_op_u *u)
0670 {
0671     __be32 status;
0672 
0673     fh_put(&cstate->current_fh);
0674     status = exp_pseudoroot(rqstp, &cstate->current_fh);
0675     return status;
0676 }
0677 
0678 static __be32
0679 nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
0680         union nfsd4_op_u *u)
0681 {
0682     if (!cstate->save_fh.fh_dentry)
0683         return nfserr_restorefh;
0684 
0685     fh_dup2(&cstate->current_fh, &cstate->save_fh);
0686     if (HAS_CSTATE_FLAG(cstate, SAVED_STATE_ID_FLAG)) {
0687         memcpy(&cstate->current_stateid, &cstate->save_stateid, sizeof(stateid_t));
0688         SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
0689     }
0690     return nfs_ok;
0691 }
0692 
0693 static __be32
0694 nfsd4_savefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
0695          union nfsd4_op_u *u)
0696 {
0697     fh_dup2(&cstate->save_fh, &cstate->current_fh);
0698     if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG)) {
0699         memcpy(&cstate->save_stateid, &cstate->current_stateid, sizeof(stateid_t));
0700         SET_CSTATE_FLAG(cstate, SAVED_STATE_ID_FLAG);
0701     }
0702     return nfs_ok;
0703 }
0704 
0705 /*
0706  * misc nfsv4 ops
0707  */
0708 static __be32
0709 nfsd4_access(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
0710          union nfsd4_op_u *u)
0711 {
0712     struct nfsd4_access *access = &u->access;
0713     u32 access_full;
0714 
0715     access_full = NFS3_ACCESS_FULL;
0716     if (cstate->minorversion >= 2)
0717         access_full |= NFS4_ACCESS_XALIST | NFS4_ACCESS_XAREAD |
0718                    NFS4_ACCESS_XAWRITE;
0719 
0720     if (access->ac_req_access & ~access_full)
0721         return nfserr_inval;
0722 
0723     access->ac_resp_access = access->ac_req_access;
0724     return nfsd_access(rqstp, &cstate->current_fh, &access->ac_resp_access,
0725                &access->ac_supported);
0726 }
0727 
0728 static void gen_boot_verifier(nfs4_verifier *verifier, struct net *net)
0729 {
0730     __be32 *verf = (__be32 *)verifier->data;
0731 
0732     BUILD_BUG_ON(2*sizeof(*verf) != sizeof(verifier->data));
0733 
0734     nfsd_copy_write_verifier(verf, net_generic(net, nfsd_net_id));
0735 }
0736 
0737 static __be32
0738 nfsd4_commit(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
0739          union nfsd4_op_u *u)
0740 {
0741     struct nfsd4_commit *commit = &u->commit;
0742 
0743     return nfsd_commit(rqstp, &cstate->current_fh, commit->co_offset,
0744                  commit->co_count,
0745                  (__be32 *)commit->co_verf.data);
0746 }
0747 
0748 static __be32
0749 nfsd4_create(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
0750          union nfsd4_op_u *u)
0751 {
0752     struct nfsd4_create *create = &u->create;
0753     struct nfsd_attrs attrs = {
0754         .na_iattr   = &create->cr_iattr,
0755         .na_seclabel    = &create->cr_label,
0756     };
0757     struct svc_fh resfh;
0758     __be32 status;
0759     dev_t rdev;
0760 
0761     fh_init(&resfh, NFS4_FHSIZE);
0762 
0763     status = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_NOP);
0764     if (status)
0765         return status;
0766 
0767     status = check_attr_support(rqstp, cstate, create->cr_bmval,
0768                     nfsd_attrmask);
0769     if (status)
0770         return status;
0771 
0772     status = nfsd4_acl_to_attr(create->cr_type, create->cr_acl, &attrs);
0773     current->fs->umask = create->cr_umask;
0774     switch (create->cr_type) {
0775     case NF4LNK:
0776         status = nfsd_symlink(rqstp, &cstate->current_fh,
0777                       create->cr_name, create->cr_namelen,
0778                       create->cr_data, &attrs, &resfh);
0779         break;
0780 
0781     case NF4BLK:
0782         status = nfserr_inval;
0783         rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
0784         if (MAJOR(rdev) != create->cr_specdata1 ||
0785             MINOR(rdev) != create->cr_specdata2)
0786             goto out_umask;
0787         status = nfsd_create(rqstp, &cstate->current_fh,
0788                      create->cr_name, create->cr_namelen,
0789                      &attrs, S_IFBLK, rdev, &resfh);
0790         break;
0791 
0792     case NF4CHR:
0793         status = nfserr_inval;
0794         rdev = MKDEV(create->cr_specdata1, create->cr_specdata2);
0795         if (MAJOR(rdev) != create->cr_specdata1 ||
0796             MINOR(rdev) != create->cr_specdata2)
0797             goto out_umask;
0798         status = nfsd_create(rqstp, &cstate->current_fh,
0799                      create->cr_name, create->cr_namelen,
0800                      &attrs, S_IFCHR, rdev, &resfh);
0801         break;
0802 
0803     case NF4SOCK:
0804         status = nfsd_create(rqstp, &cstate->current_fh,
0805                      create->cr_name, create->cr_namelen,
0806                      &attrs, S_IFSOCK, 0, &resfh);
0807         break;
0808 
0809     case NF4FIFO:
0810         status = nfsd_create(rqstp, &cstate->current_fh,
0811                      create->cr_name, create->cr_namelen,
0812                      &attrs, S_IFIFO, 0, &resfh);
0813         break;
0814 
0815     case NF4DIR:
0816         create->cr_iattr.ia_valid &= ~ATTR_SIZE;
0817         status = nfsd_create(rqstp, &cstate->current_fh,
0818                      create->cr_name, create->cr_namelen,
0819                      &attrs, S_IFDIR, 0, &resfh);
0820         break;
0821 
0822     default:
0823         status = nfserr_badtype;
0824     }
0825 
0826     if (status)
0827         goto out;
0828 
0829     if (attrs.na_labelerr)
0830         create->cr_bmval[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
0831     if (attrs.na_aclerr)
0832         create->cr_bmval[0] &= ~FATTR4_WORD0_ACL;
0833     set_change_info(&create->cr_cinfo, &cstate->current_fh);
0834     fh_dup2(&cstate->current_fh, &resfh);
0835 out:
0836     fh_put(&resfh);
0837 out_umask:
0838     current->fs->umask = 0;
0839     nfsd_attrs_free(&attrs);
0840     return status;
0841 }
0842 
0843 static __be32
0844 nfsd4_getattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
0845           union nfsd4_op_u *u)
0846 {
0847     struct nfsd4_getattr *getattr = &u->getattr;
0848     __be32 status;
0849 
0850     status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
0851     if (status)
0852         return status;
0853 
0854     if (getattr->ga_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
0855         return nfserr_inval;
0856 
0857     getattr->ga_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
0858     getattr->ga_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
0859     getattr->ga_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
0860 
0861     getattr->ga_fhp = &cstate->current_fh;
0862     return nfs_ok;
0863 }
0864 
0865 static __be32
0866 nfsd4_link(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
0867        union nfsd4_op_u *u)
0868 {
0869     struct nfsd4_link *link = &u->link;
0870     __be32 status;
0871 
0872     status = nfsd_link(rqstp, &cstate->current_fh,
0873                link->li_name, link->li_namelen, &cstate->save_fh);
0874     if (!status)
0875         set_change_info(&link->li_cinfo, &cstate->current_fh);
0876     return status;
0877 }
0878 
0879 static __be32 nfsd4_do_lookupp(struct svc_rqst *rqstp, struct svc_fh *fh)
0880 {
0881     struct svc_fh tmp_fh;
0882     __be32 ret;
0883 
0884     fh_init(&tmp_fh, NFS4_FHSIZE);
0885     ret = exp_pseudoroot(rqstp, &tmp_fh);
0886     if (ret)
0887         return ret;
0888     if (tmp_fh.fh_dentry == fh->fh_dentry) {
0889         fh_put(&tmp_fh);
0890         return nfserr_noent;
0891     }
0892     fh_put(&tmp_fh);
0893     return nfsd_lookup(rqstp, fh, "..", 2, fh);
0894 }
0895 
0896 static __be32
0897 nfsd4_lookupp(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
0898           union nfsd4_op_u *u)
0899 {
0900     return nfsd4_do_lookupp(rqstp, &cstate->current_fh);
0901 }
0902 
0903 static __be32
0904 nfsd4_lookup(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
0905          union nfsd4_op_u *u)
0906 {
0907     return nfsd_lookup(rqstp, &cstate->current_fh,
0908                u->lookup.lo_name, u->lookup.lo_len,
0909                &cstate->current_fh);
0910 }
0911 
0912 static __be32
0913 nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
0914        union nfsd4_op_u *u)
0915 {
0916     struct nfsd4_read *read = &u->read;
0917     __be32 status;
0918 
0919     read->rd_nf = NULL;
0920 
0921     trace_nfsd_read_start(rqstp, &cstate->current_fh,
0922                   read->rd_offset, read->rd_length);
0923 
0924     read->rd_length = min_t(u32, read->rd_length, svc_max_payload(rqstp));
0925     if (read->rd_offset > (u64)OFFSET_MAX)
0926         read->rd_offset = (u64)OFFSET_MAX;
0927     if (read->rd_offset + read->rd_length > (u64)OFFSET_MAX)
0928         read->rd_length = (u64)OFFSET_MAX - read->rd_offset;
0929 
0930     /*
0931      * If we do a zero copy read, then a client will see read data
0932      * that reflects the state of the file *after* performing the
0933      * following compound.
0934      *
0935      * To ensure proper ordering, we therefore turn off zero copy if
0936      * the client wants us to do more in this compound:
0937      */
0938     if (!nfsd4_last_compound_op(rqstp))
0939         __clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
0940 
0941     /* check stateid */
0942     status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
0943                     &read->rd_stateid, RD_STATE,
0944                     &read->rd_nf, NULL);
0945     if (status) {
0946         dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
0947         goto out;
0948     }
0949     status = nfs_ok;
0950 out:
0951     read->rd_rqstp = rqstp;
0952     read->rd_fhp = &cstate->current_fh;
0953     return status;
0954 }
0955 
0956 
0957 static void
0958 nfsd4_read_release(union nfsd4_op_u *u)
0959 {
0960     if (u->read.rd_nf)
0961         nfsd_file_put(u->read.rd_nf);
0962     trace_nfsd_read_done(u->read.rd_rqstp, u->read.rd_fhp,
0963                  u->read.rd_offset, u->read.rd_length);
0964 }
0965 
0966 static __be32
0967 nfsd4_readdir(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
0968           union nfsd4_op_u *u)
0969 {
0970     struct nfsd4_readdir *readdir = &u->readdir;
0971     u64 cookie = readdir->rd_cookie;
0972     static const nfs4_verifier zeroverf;
0973 
0974     /* no need to check permission - this will be done in nfsd_readdir() */
0975 
0976     if (readdir->rd_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1)
0977         return nfserr_inval;
0978 
0979     readdir->rd_bmval[0] &= nfsd_suppattrs[cstate->minorversion][0];
0980     readdir->rd_bmval[1] &= nfsd_suppattrs[cstate->minorversion][1];
0981     readdir->rd_bmval[2] &= nfsd_suppattrs[cstate->minorversion][2];
0982 
0983     if ((cookie == 1) || (cookie == 2) ||
0984         (cookie == 0 && memcmp(readdir->rd_verf.data, zeroverf.data, NFS4_VERIFIER_SIZE)))
0985         return nfserr_bad_cookie;
0986 
0987     readdir->rd_rqstp = rqstp;
0988     readdir->rd_fhp = &cstate->current_fh;
0989     return nfs_ok;
0990 }
0991 
0992 static __be32
0993 nfsd4_readlink(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
0994            union nfsd4_op_u *u)
0995 {
0996     u->readlink.rl_rqstp = rqstp;
0997     u->readlink.rl_fhp = &cstate->current_fh;
0998     return nfs_ok;
0999 }
1000 
1001 static __be32
1002 nfsd4_remove(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1003          union nfsd4_op_u *u)
1004 {
1005     struct nfsd4_remove *remove = &u->remove;
1006     __be32 status;
1007 
1008     if (opens_in_grace(SVC_NET(rqstp)))
1009         return nfserr_grace;
1010     status = nfsd_unlink(rqstp, &cstate->current_fh, 0,
1011                  remove->rm_name, remove->rm_namelen);
1012     if (!status)
1013         set_change_info(&remove->rm_cinfo, &cstate->current_fh);
1014     return status;
1015 }
1016 
1017 static __be32
1018 nfsd4_rename(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1019          union nfsd4_op_u *u)
1020 {
1021     struct nfsd4_rename *rename = &u->rename;
1022     __be32 status;
1023 
1024     if (opens_in_grace(SVC_NET(rqstp)))
1025         return nfserr_grace;
1026     status = nfsd_rename(rqstp, &cstate->save_fh, rename->rn_sname,
1027                  rename->rn_snamelen, &cstate->current_fh,
1028                  rename->rn_tname, rename->rn_tnamelen);
1029     if (status)
1030         return status;
1031     set_change_info(&rename->rn_sinfo, &cstate->current_fh);
1032     set_change_info(&rename->rn_tinfo, &cstate->save_fh);
1033     return nfs_ok;
1034 }
1035 
1036 static __be32
1037 nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1038           union nfsd4_op_u *u)
1039 {
1040     struct nfsd4_secinfo *secinfo = &u->secinfo;
1041     struct svc_export *exp;
1042     struct dentry *dentry;
1043     __be32 err;
1044 
1045     err = fh_verify(rqstp, &cstate->current_fh, S_IFDIR, NFSD_MAY_EXEC);
1046     if (err)
1047         return err;
1048     err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
1049                     secinfo->si_name, secinfo->si_namelen,
1050                     &exp, &dentry);
1051     if (err)
1052         return err;
1053     if (d_really_is_negative(dentry)) {
1054         exp_put(exp);
1055         err = nfserr_noent;
1056     } else
1057         secinfo->si_exp = exp;
1058     dput(dentry);
1059     if (cstate->minorversion)
1060         /* See rfc 5661 section 2.6.3.1.1.8 */
1061         fh_put(&cstate->current_fh);
1062     return err;
1063 }
1064 
1065 static __be32
1066 nfsd4_secinfo_no_name(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1067         union nfsd4_op_u *u)
1068 {
1069     __be32 err;
1070 
1071     switch (u->secinfo_no_name.sin_style) {
1072     case NFS4_SECINFO_STYLE4_CURRENT_FH:
1073         break;
1074     case NFS4_SECINFO_STYLE4_PARENT:
1075         err = nfsd4_do_lookupp(rqstp, &cstate->current_fh);
1076         if (err)
1077             return err;
1078         break;
1079     default:
1080         return nfserr_inval;
1081     }
1082 
1083     u->secinfo_no_name.sin_exp = exp_get(cstate->current_fh.fh_export);
1084     fh_put(&cstate->current_fh);
1085     return nfs_ok;
1086 }
1087 
1088 static void
1089 nfsd4_secinfo_release(union nfsd4_op_u *u)
1090 {
1091     if (u->secinfo.si_exp)
1092         exp_put(u->secinfo.si_exp);
1093 }
1094 
1095 static void
1096 nfsd4_secinfo_no_name_release(union nfsd4_op_u *u)
1097 {
1098     if (u->secinfo_no_name.sin_exp)
1099         exp_put(u->secinfo_no_name.sin_exp);
1100 }
1101 
1102 static __be32
1103 nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1104           union nfsd4_op_u *u)
1105 {
1106     struct nfsd4_setattr *setattr = &u->setattr;
1107     struct nfsd_attrs attrs = {
1108         .na_iattr   = &setattr->sa_iattr,
1109         .na_seclabel    = &setattr->sa_label,
1110     };
1111     struct inode *inode;
1112     __be32 status = nfs_ok;
1113     int err;
1114 
1115     if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
1116         status = nfs4_preprocess_stateid_op(rqstp, cstate,
1117                 &cstate->current_fh, &setattr->sa_stateid,
1118                 WR_STATE, NULL, NULL);
1119         if (status) {
1120             dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
1121             return status;
1122         }
1123     }
1124     err = fh_want_write(&cstate->current_fh);
1125     if (err)
1126         return nfserrno(err);
1127     status = nfs_ok;
1128 
1129     status = check_attr_support(rqstp, cstate, setattr->sa_bmval,
1130                     nfsd_attrmask);
1131     if (status)
1132         goto out;
1133 
1134     inode = cstate->current_fh.fh_dentry->d_inode;
1135     status = nfsd4_acl_to_attr(S_ISDIR(inode->i_mode) ? NF4DIR : NF4REG,
1136                    setattr->sa_acl, &attrs);
1137 
1138     if (status)
1139         goto out;
1140     status = nfsd_setattr(rqstp, &cstate->current_fh, &attrs,
1141                 0, (time64_t)0);
1142     if (!status)
1143         status = nfserrno(attrs.na_labelerr);
1144 out:
1145     nfsd_attrs_free(&attrs);
1146     fh_drop_write(&cstate->current_fh);
1147     return status;
1148 }
1149 
1150 static __be32
1151 nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1152         union nfsd4_op_u *u)
1153 {
1154     struct nfsd4_write *write = &u->write;
1155     stateid_t *stateid = &write->wr_stateid;
1156     struct nfsd_file *nf = NULL;
1157     __be32 status = nfs_ok;
1158     unsigned long cnt;
1159     int nvecs;
1160 
1161     if (write->wr_offset > (u64)OFFSET_MAX ||
1162         write->wr_offset + write->wr_buflen > (u64)OFFSET_MAX)
1163         return nfserr_fbig;
1164 
1165     cnt = write->wr_buflen;
1166     trace_nfsd_write_start(rqstp, &cstate->current_fh,
1167                    write->wr_offset, cnt);
1168     status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1169                         stateid, WR_STATE, &nf, NULL);
1170     if (status) {
1171         dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
1172         return status;
1173     }
1174 
1175     write->wr_how_written = write->wr_stable_how;
1176 
1177     nvecs = svc_fill_write_vector(rqstp, &write->wr_payload);
1178     WARN_ON_ONCE(nvecs > ARRAY_SIZE(rqstp->rq_vec));
1179 
1180     status = nfsd_vfs_write(rqstp, &cstate->current_fh, nf,
1181                 write->wr_offset, rqstp->rq_vec, nvecs, &cnt,
1182                 write->wr_how_written,
1183                 (__be32 *)write->wr_verifier.data);
1184     nfsd_file_put(nf);
1185 
1186     write->wr_bytes_written = cnt;
1187     trace_nfsd_write_done(rqstp, &cstate->current_fh,
1188                   write->wr_offset, cnt);
1189     return status;
1190 }
1191 
1192 static __be32
1193 nfsd4_verify_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1194           stateid_t *src_stateid, struct nfsd_file **src,
1195           stateid_t *dst_stateid, struct nfsd_file **dst)
1196 {
1197     __be32 status;
1198 
1199     if (!cstate->save_fh.fh_dentry)
1200         return nfserr_nofilehandle;
1201 
1202     status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->save_fh,
1203                         src_stateid, RD_STATE, src, NULL);
1204     if (status) {
1205         dprintk("NFSD: %s: couldn't process src stateid!\n", __func__);
1206         goto out;
1207     }
1208 
1209     status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1210                         dst_stateid, WR_STATE, dst, NULL);
1211     if (status) {
1212         dprintk("NFSD: %s: couldn't process dst stateid!\n", __func__);
1213         goto out_put_src;
1214     }
1215 
1216     /* fix up for NFS-specific error code */
1217     if (!S_ISREG(file_inode((*src)->nf_file)->i_mode) ||
1218         !S_ISREG(file_inode((*dst)->nf_file)->i_mode)) {
1219         status = nfserr_wrong_type;
1220         goto out_put_dst;
1221     }
1222 
1223 out:
1224     return status;
1225 out_put_dst:
1226     nfsd_file_put(*dst);
1227 out_put_src:
1228     nfsd_file_put(*src);
1229     goto out;
1230 }
1231 
1232 static __be32
1233 nfsd4_clone(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1234         union nfsd4_op_u *u)
1235 {
1236     struct nfsd4_clone *clone = &u->clone;
1237     struct nfsd_file *src, *dst;
1238     __be32 status;
1239 
1240     status = nfsd4_verify_copy(rqstp, cstate, &clone->cl_src_stateid, &src,
1241                    &clone->cl_dst_stateid, &dst);
1242     if (status)
1243         goto out;
1244 
1245     status = nfsd4_clone_file_range(rqstp, src, clone->cl_src_pos,
1246             dst, clone->cl_dst_pos, clone->cl_count,
1247             EX_ISSYNC(cstate->current_fh.fh_export));
1248 
1249     nfsd_file_put(dst);
1250     nfsd_file_put(src);
1251 out:
1252     return status;
1253 }
1254 
1255 static void nfs4_put_copy(struct nfsd4_copy *copy)
1256 {
1257     if (!refcount_dec_and_test(&copy->refcount))
1258         return;
1259     kfree(copy->cp_src);
1260     kfree(copy);
1261 }
1262 
1263 static void nfsd4_stop_copy(struct nfsd4_copy *copy)
1264 {
1265     if (!test_and_set_bit(NFSD4_COPY_F_STOPPED, &copy->cp_flags))
1266         kthread_stop(copy->copy_task);
1267     nfs4_put_copy(copy);
1268 }
1269 
1270 static struct nfsd4_copy *nfsd4_get_copy(struct nfs4_client *clp)
1271 {
1272     struct nfsd4_copy *copy = NULL;
1273 
1274     spin_lock(&clp->async_lock);
1275     if (!list_empty(&clp->async_copies)) {
1276         copy = list_first_entry(&clp->async_copies, struct nfsd4_copy,
1277                     copies);
1278         refcount_inc(&copy->refcount);
1279     }
1280     spin_unlock(&clp->async_lock);
1281     return copy;
1282 }
1283 
1284 void nfsd4_shutdown_copy(struct nfs4_client *clp)
1285 {
1286     struct nfsd4_copy *copy;
1287 
1288     while ((copy = nfsd4_get_copy(clp)) != NULL)
1289         nfsd4_stop_copy(copy);
1290 }
1291 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
1292 
1293 extern struct file *nfs42_ssc_open(struct vfsmount *ss_mnt,
1294                    struct nfs_fh *src_fh,
1295                    nfs4_stateid *stateid);
1296 extern void nfs42_ssc_close(struct file *filep);
1297 
1298 extern void nfs_sb_deactive(struct super_block *sb);
1299 
1300 #define NFSD42_INTERSSC_MOUNTOPS "vers=4.2,addr=%s,sec=sys"
1301 
1302 /*
1303  * setup a work entry in the ssc delayed unmount list.
1304  */
1305 static __be32 nfsd4_ssc_setup_dul(struct nfsd_net *nn, char *ipaddr,
1306         struct nfsd4_ssc_umount_item **retwork, struct vfsmount **ss_mnt)
1307 {
1308     struct nfsd4_ssc_umount_item *ni = NULL;
1309     struct nfsd4_ssc_umount_item *work = NULL;
1310     struct nfsd4_ssc_umount_item *tmp;
1311     DEFINE_WAIT(wait);
1312 
1313     *ss_mnt = NULL;
1314     *retwork = NULL;
1315     work = kzalloc(sizeof(*work), GFP_KERNEL);
1316 try_again:
1317     spin_lock(&nn->nfsd_ssc_lock);
1318     list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
1319         if (strncmp(ni->nsui_ipaddr, ipaddr, sizeof(ni->nsui_ipaddr)))
1320             continue;
1321         /* found a match */
1322         if (ni->nsui_busy) {
1323             /*  wait - and try again */
1324             prepare_to_wait(&nn->nfsd_ssc_waitq, &wait,
1325                 TASK_INTERRUPTIBLE);
1326             spin_unlock(&nn->nfsd_ssc_lock);
1327 
1328             /* allow 20secs for mount/unmount for now - revisit */
1329             if (signal_pending(current) ||
1330                     (schedule_timeout(20*HZ) == 0)) {
1331                 kfree(work);
1332                 return nfserr_eagain;
1333             }
1334             finish_wait(&nn->nfsd_ssc_waitq, &wait);
1335             goto try_again;
1336         }
1337         *ss_mnt = ni->nsui_vfsmount;
1338         refcount_inc(&ni->nsui_refcnt);
1339         spin_unlock(&nn->nfsd_ssc_lock);
1340         kfree(work);
1341 
1342         /* return vfsmount in ss_mnt */
1343         return 0;
1344     }
1345     if (work) {
1346         strlcpy(work->nsui_ipaddr, ipaddr, sizeof(work->nsui_ipaddr) - 1);
1347         refcount_set(&work->nsui_refcnt, 2);
1348         work->nsui_busy = true;
1349         list_add_tail(&work->nsui_list, &nn->nfsd_ssc_mount_list);
1350         *retwork = work;
1351     }
1352     spin_unlock(&nn->nfsd_ssc_lock);
1353     return 0;
1354 }
1355 
1356 static void nfsd4_ssc_update_dul_work(struct nfsd_net *nn,
1357         struct nfsd4_ssc_umount_item *work, struct vfsmount *ss_mnt)
1358 {
1359     /* set nsui_vfsmount, clear busy flag and wakeup waiters */
1360     spin_lock(&nn->nfsd_ssc_lock);
1361     work->nsui_vfsmount = ss_mnt;
1362     work->nsui_busy = false;
1363     wake_up_all(&nn->nfsd_ssc_waitq);
1364     spin_unlock(&nn->nfsd_ssc_lock);
1365 }
1366 
1367 static void nfsd4_ssc_cancel_dul_work(struct nfsd_net *nn,
1368         struct nfsd4_ssc_umount_item *work)
1369 {
1370     spin_lock(&nn->nfsd_ssc_lock);
1371     list_del(&work->nsui_list);
1372     wake_up_all(&nn->nfsd_ssc_waitq);
1373     spin_unlock(&nn->nfsd_ssc_lock);
1374     kfree(work);
1375 }
1376 
1377 /*
1378  * Support one copy source server for now.
1379  */
1380 static __be32
1381 nfsd4_interssc_connect(struct nl4_server *nss, struct svc_rqst *rqstp,
1382                struct vfsmount **mount)
1383 {
1384     struct file_system_type *type;
1385     struct vfsmount *ss_mnt;
1386     struct nfs42_netaddr *naddr;
1387     struct sockaddr_storage tmp_addr;
1388     size_t tmp_addrlen, match_netid_len = 3;
1389     char *startsep = "", *endsep = "", *match_netid = "tcp";
1390     char *ipaddr, *dev_name, *raw_data;
1391     int len, raw_len;
1392     __be32 status = nfserr_inval;
1393     struct nfsd4_ssc_umount_item *work = NULL;
1394     struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1395 
1396     naddr = &nss->u.nl4_addr;
1397     tmp_addrlen = rpc_uaddr2sockaddr(SVC_NET(rqstp), naddr->addr,
1398                      naddr->addr_len,
1399                      (struct sockaddr *)&tmp_addr,
1400                      sizeof(tmp_addr));
1401     if (tmp_addrlen == 0)
1402         goto out_err;
1403 
1404     if (tmp_addr.ss_family == AF_INET6) {
1405         startsep = "[";
1406         endsep = "]";
1407         match_netid = "tcp6";
1408         match_netid_len = 4;
1409     }
1410 
1411     if (naddr->netid_len != match_netid_len ||
1412         strncmp(naddr->netid, match_netid, naddr->netid_len))
1413         goto out_err;
1414 
1415     /* Construct the raw data for the vfs_kern_mount call */
1416     len = RPC_MAX_ADDRBUFLEN + 1;
1417     ipaddr = kzalloc(len, GFP_KERNEL);
1418     if (!ipaddr)
1419         goto out_err;
1420 
1421     rpc_ntop((struct sockaddr *)&tmp_addr, ipaddr, len);
1422 
1423     /* 2 for ipv6 endsep and startsep. 3 for ":/" and trailing '/0'*/
1424 
1425     raw_len = strlen(NFSD42_INTERSSC_MOUNTOPS) + strlen(ipaddr);
1426     raw_data = kzalloc(raw_len, GFP_KERNEL);
1427     if (!raw_data)
1428         goto out_free_ipaddr;
1429 
1430     snprintf(raw_data, raw_len, NFSD42_INTERSSC_MOUNTOPS, ipaddr);
1431 
1432     status = nfserr_nodev;
1433     type = get_fs_type("nfs");
1434     if (!type)
1435         goto out_free_rawdata;
1436 
1437     /* Set the server:<export> for the vfs_kern_mount call */
1438     dev_name = kzalloc(len + 5, GFP_KERNEL);
1439     if (!dev_name)
1440         goto out_free_rawdata;
1441     snprintf(dev_name, len + 5, "%s%s%s:/", startsep, ipaddr, endsep);
1442 
1443     status = nfsd4_ssc_setup_dul(nn, ipaddr, &work, &ss_mnt);
1444     if (status)
1445         goto out_free_devname;
1446     if (ss_mnt)
1447         goto out_done;
1448 
1449     /* Use an 'internal' mount: SB_KERNMOUNT -> MNT_INTERNAL */
1450     ss_mnt = vfs_kern_mount(type, SB_KERNMOUNT, dev_name, raw_data);
1451     module_put(type->owner);
1452     if (IS_ERR(ss_mnt)) {
1453         status = nfserr_nodev;
1454         if (work)
1455             nfsd4_ssc_cancel_dul_work(nn, work);
1456         goto out_free_devname;
1457     }
1458     if (work)
1459         nfsd4_ssc_update_dul_work(nn, work, ss_mnt);
1460 out_done:
1461     status = 0;
1462     *mount = ss_mnt;
1463 
1464 out_free_devname:
1465     kfree(dev_name);
1466 out_free_rawdata:
1467     kfree(raw_data);
1468 out_free_ipaddr:
1469     kfree(ipaddr);
1470 out_err:
1471     return status;
1472 }
1473 
1474 static void
1475 nfsd4_interssc_disconnect(struct vfsmount *ss_mnt)
1476 {
1477     nfs_do_sb_deactive(ss_mnt->mnt_sb);
1478     mntput(ss_mnt);
1479 }
1480 
1481 /*
1482  * Verify COPY destination stateid.
1483  *
1484  * Connect to the source server with NFSv4.1.
1485  * Create the source struct file for nfsd_copy_range.
1486  * Called with COPY cstate:
1487  *    SAVED_FH: source filehandle
1488  *    CURRENT_FH: destination filehandle
1489  */
1490 static __be32
1491 nfsd4_setup_inter_ssc(struct svc_rqst *rqstp,
1492               struct nfsd4_compound_state *cstate,
1493               struct nfsd4_copy *copy, struct vfsmount **mount)
1494 {
1495     struct svc_fh *s_fh = NULL;
1496     stateid_t *s_stid = &copy->cp_src_stateid;
1497     __be32 status = nfserr_inval;
1498 
1499     /* Verify the destination stateid and set dst struct file*/
1500     status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1501                         &copy->cp_dst_stateid,
1502                         WR_STATE, &copy->nf_dst, NULL);
1503     if (status)
1504         goto out;
1505 
1506     status = nfsd4_interssc_connect(copy->cp_src, rqstp, mount);
1507     if (status)
1508         goto out;
1509 
1510     s_fh = &cstate->save_fh;
1511 
1512     copy->c_fh.size = s_fh->fh_handle.fh_size;
1513     memcpy(copy->c_fh.data, &s_fh->fh_handle.fh_raw, copy->c_fh.size);
1514     copy->stateid.seqid = cpu_to_be32(s_stid->si_generation);
1515     memcpy(copy->stateid.other, (void *)&s_stid->si_opaque,
1516            sizeof(stateid_opaque_t));
1517 
1518     status = 0;
1519 out:
1520     return status;
1521 }
1522 
1523 static void
1524 nfsd4_cleanup_inter_ssc(struct vfsmount *ss_mnt, struct file *filp,
1525             struct nfsd_file *dst)
1526 {
1527     bool found = false;
1528     long timeout;
1529     struct nfsd4_ssc_umount_item *tmp;
1530     struct nfsd4_ssc_umount_item *ni = NULL;
1531     struct nfsd_net *nn = net_generic(dst->nf_net, nfsd_net_id);
1532 
1533     nfs42_ssc_close(filp);
1534     nfsd_file_put(dst);
1535     fput(filp);
1536 
1537     if (!nn) {
1538         mntput(ss_mnt);
1539         return;
1540     }
1541     spin_lock(&nn->nfsd_ssc_lock);
1542     timeout = msecs_to_jiffies(nfsd4_ssc_umount_timeout);
1543     list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
1544         if (ni->nsui_vfsmount->mnt_sb == ss_mnt->mnt_sb) {
1545             list_del(&ni->nsui_list);
1546             /*
1547              * vfsmount can be shared by multiple exports,
1548              * decrement refcnt. If the count drops to 1 it
1549              * will be unmounted when nsui_expire expires.
1550              */
1551             refcount_dec(&ni->nsui_refcnt);
1552             ni->nsui_expire = jiffies + timeout;
1553             list_add_tail(&ni->nsui_list, &nn->nfsd_ssc_mount_list);
1554             found = true;
1555             break;
1556         }
1557     }
1558     spin_unlock(&nn->nfsd_ssc_lock);
1559     if (!found) {
1560         mntput(ss_mnt);
1561         return;
1562     }
1563 }
1564 
1565 #else /* CONFIG_NFSD_V4_2_INTER_SSC */
1566 
1567 static __be32
1568 nfsd4_setup_inter_ssc(struct svc_rqst *rqstp,
1569               struct nfsd4_compound_state *cstate,
1570               struct nfsd4_copy *copy,
1571               struct vfsmount **mount)
1572 {
1573     *mount = NULL;
1574     return nfserr_inval;
1575 }
1576 
1577 static void
1578 nfsd4_cleanup_inter_ssc(struct vfsmount *ss_mnt, struct file *filp,
1579             struct nfsd_file *dst)
1580 {
1581 }
1582 
1583 static void
1584 nfsd4_interssc_disconnect(struct vfsmount *ss_mnt)
1585 {
1586 }
1587 
1588 static struct file *nfs42_ssc_open(struct vfsmount *ss_mnt,
1589                    struct nfs_fh *src_fh,
1590                    nfs4_stateid *stateid)
1591 {
1592     return NULL;
1593 }
1594 #endif /* CONFIG_NFSD_V4_2_INTER_SSC */
1595 
1596 static __be32
1597 nfsd4_setup_intra_ssc(struct svc_rqst *rqstp,
1598               struct nfsd4_compound_state *cstate,
1599               struct nfsd4_copy *copy)
1600 {
1601     return nfsd4_verify_copy(rqstp, cstate, &copy->cp_src_stateid,
1602                  &copy->nf_src, &copy->cp_dst_stateid,
1603                  &copy->nf_dst);
1604 }
1605 
1606 static void
1607 nfsd4_cleanup_intra_ssc(struct nfsd_file *src, struct nfsd_file *dst)
1608 {
1609     nfsd_file_put(src);
1610     nfsd_file_put(dst);
1611 }
1612 
1613 static void nfsd4_cb_offload_release(struct nfsd4_callback *cb)
1614 {
1615     struct nfsd4_cb_offload *cbo =
1616         container_of(cb, struct nfsd4_cb_offload, co_cb);
1617 
1618     kfree(cbo);
1619 }
1620 
1621 static int nfsd4_cb_offload_done(struct nfsd4_callback *cb,
1622                  struct rpc_task *task)
1623 {
1624     return 1;
1625 }
1626 
1627 static const struct nfsd4_callback_ops nfsd4_cb_offload_ops = {
1628     .release = nfsd4_cb_offload_release,
1629     .done = nfsd4_cb_offload_done
1630 };
1631 
1632 static void nfsd4_init_copy_res(struct nfsd4_copy *copy, bool sync)
1633 {
1634     copy->cp_res.wr_stable_how =
1635         test_bit(NFSD4_COPY_F_COMMITTED, &copy->cp_flags) ?
1636             NFS_FILE_SYNC : NFS_UNSTABLE;
1637     nfsd4_copy_set_sync(copy, sync);
1638     gen_boot_verifier(&copy->cp_res.wr_verifier, copy->cp_clp->net);
1639 }
1640 
1641 static ssize_t _nfsd_copy_file_range(struct nfsd4_copy *copy,
1642                      struct file *dst,
1643                      struct file *src)
1644 {
1645     errseq_t since;
1646     ssize_t bytes_copied = 0;
1647     u64 bytes_total = copy->cp_count;
1648     u64 src_pos = copy->cp_src_pos;
1649     u64 dst_pos = copy->cp_dst_pos;
1650     int status;
1651 
1652     /* See RFC 7862 p.67: */
1653     if (bytes_total == 0)
1654         bytes_total = ULLONG_MAX;
1655     do {
1656         if (kthread_should_stop())
1657             break;
1658         bytes_copied = nfsd_copy_file_range(src, src_pos, dst, dst_pos,
1659                             bytes_total);
1660         if (bytes_copied <= 0)
1661             break;
1662         bytes_total -= bytes_copied;
1663         copy->cp_res.wr_bytes_written += bytes_copied;
1664         src_pos += bytes_copied;
1665         dst_pos += bytes_copied;
1666     } while (bytes_total > 0 && nfsd4_copy_is_async(copy));
1667     /* for a non-zero asynchronous copy do a commit of data */
1668     if (nfsd4_copy_is_async(copy) && copy->cp_res.wr_bytes_written > 0) {
1669         since = READ_ONCE(dst->f_wb_err);
1670         status = vfs_fsync_range(dst, copy->cp_dst_pos,
1671                      copy->cp_res.wr_bytes_written, 0);
1672         if (!status)
1673             status = filemap_check_wb_err(dst->f_mapping, since);
1674         if (!status)
1675             set_bit(NFSD4_COPY_F_COMMITTED, &copy->cp_flags);
1676     }
1677     return bytes_copied;
1678 }
1679 
1680 static __be32 nfsd4_do_copy(struct nfsd4_copy *copy,
1681                 struct file *src, struct file *dst,
1682                 bool sync)
1683 {
1684     __be32 status;
1685     ssize_t bytes;
1686 
1687     bytes = _nfsd_copy_file_range(copy, dst, src);
1688 
1689     /* for async copy, we ignore the error, client can always retry
1690      * to get the error
1691      */
1692     if (bytes < 0 && !copy->cp_res.wr_bytes_written)
1693         status = nfserrno(bytes);
1694     else {
1695         nfsd4_init_copy_res(copy, sync);
1696         status = nfs_ok;
1697     }
1698     return status;
1699 }
1700 
1701 static void dup_copy_fields(struct nfsd4_copy *src, struct nfsd4_copy *dst)
1702 {
1703     dst->cp_src_pos = src->cp_src_pos;
1704     dst->cp_dst_pos = src->cp_dst_pos;
1705     dst->cp_count = src->cp_count;
1706     dst->cp_flags = src->cp_flags;
1707     memcpy(&dst->cp_res, &src->cp_res, sizeof(src->cp_res));
1708     memcpy(&dst->fh, &src->fh, sizeof(src->fh));
1709     dst->cp_clp = src->cp_clp;
1710     dst->nf_dst = nfsd_file_get(src->nf_dst);
1711     /* for inter, nf_src doesn't exist yet */
1712     if (!nfsd4_ssc_is_inter(src))
1713         dst->nf_src = nfsd_file_get(src->nf_src);
1714 
1715     memcpy(&dst->cp_stateid, &src->cp_stateid, sizeof(src->cp_stateid));
1716     memcpy(dst->cp_src, src->cp_src, sizeof(struct nl4_server));
1717     memcpy(&dst->stateid, &src->stateid, sizeof(src->stateid));
1718     memcpy(&dst->c_fh, &src->c_fh, sizeof(src->c_fh));
1719     dst->ss_mnt = src->ss_mnt;
1720 }
1721 
1722 static void cleanup_async_copy(struct nfsd4_copy *copy)
1723 {
1724     nfs4_free_copy_state(copy);
1725     nfsd_file_put(copy->nf_dst);
1726     if (!nfsd4_ssc_is_inter(copy))
1727         nfsd_file_put(copy->nf_src);
1728     spin_lock(&copy->cp_clp->async_lock);
1729     list_del(&copy->copies);
1730     spin_unlock(&copy->cp_clp->async_lock);
1731     nfs4_put_copy(copy);
1732 }
1733 
1734 static void nfsd4_send_cb_offload(struct nfsd4_copy *copy, __be32 nfserr)
1735 {
1736     struct nfsd4_cb_offload *cbo;
1737 
1738     cbo = kzalloc(sizeof(*cbo), GFP_KERNEL);
1739     if (!cbo)
1740         return;
1741 
1742     memcpy(&cbo->co_res, &copy->cp_res, sizeof(copy->cp_res));
1743     memcpy(&cbo->co_fh, &copy->fh, sizeof(copy->fh));
1744     cbo->co_nfserr = nfserr;
1745 
1746     nfsd4_init_cb(&cbo->co_cb, copy->cp_clp, &nfsd4_cb_offload_ops,
1747               NFSPROC4_CLNT_CB_OFFLOAD);
1748     trace_nfsd_cb_offload(copy->cp_clp, &cbo->co_res.cb_stateid,
1749                   &cbo->co_fh, copy->cp_count, nfserr);
1750     nfsd4_run_cb(&cbo->co_cb);
1751 }
1752 
1753 /**
1754  * nfsd4_do_async_copy - kthread function for background server-side COPY
1755  * @data: arguments for COPY operation
1756  *
1757  * Return values:
1758  *   %0: Copy operation is done.
1759  */
1760 static int nfsd4_do_async_copy(void *data)
1761 {
1762     struct nfsd4_copy *copy = (struct nfsd4_copy *)data;
1763     __be32 nfserr;
1764 
1765     if (nfsd4_ssc_is_inter(copy)) {
1766         struct file *filp;
1767 
1768         filp = nfs42_ssc_open(copy->ss_mnt, &copy->c_fh,
1769                       &copy->stateid);
1770         if (IS_ERR(filp)) {
1771             nfserr = nfserr_offload_denied;
1772             nfsd4_interssc_disconnect(copy->ss_mnt);
1773             goto do_callback;
1774         }
1775         nfserr = nfsd4_do_copy(copy, filp, copy->nf_dst->nf_file,
1776                        false);
1777         nfsd4_cleanup_inter_ssc(copy->ss_mnt, filp, copy->nf_dst);
1778     } else {
1779         nfserr = nfsd4_do_copy(copy, copy->nf_src->nf_file,
1780                        copy->nf_dst->nf_file, false);
1781         nfsd4_cleanup_intra_ssc(copy->nf_src, copy->nf_dst);
1782     }
1783 
1784 do_callback:
1785     nfsd4_send_cb_offload(copy, nfserr);
1786     cleanup_async_copy(copy);
1787     return 0;
1788 }
1789 
1790 static __be32
1791 nfsd4_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1792         union nfsd4_op_u *u)
1793 {
1794     struct nfsd4_copy *copy = &u->copy;
1795     __be32 status;
1796     struct nfsd4_copy *async_copy = NULL;
1797 
1798     if (nfsd4_ssc_is_inter(copy)) {
1799         if (!inter_copy_offload_enable || nfsd4_copy_is_sync(copy)) {
1800             status = nfserr_notsupp;
1801             goto out;
1802         }
1803         status = nfsd4_setup_inter_ssc(rqstp, cstate, copy,
1804                 &copy->ss_mnt);
1805         if (status)
1806             return nfserr_offload_denied;
1807     } else {
1808         status = nfsd4_setup_intra_ssc(rqstp, cstate, copy);
1809         if (status)
1810             return status;
1811     }
1812 
1813     copy->cp_clp = cstate->clp;
1814     memcpy(&copy->fh, &cstate->current_fh.fh_handle,
1815         sizeof(struct knfsd_fh));
1816     if (nfsd4_copy_is_async(copy)) {
1817         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1818 
1819         status = nfserrno(-ENOMEM);
1820         async_copy = kzalloc(sizeof(struct nfsd4_copy), GFP_KERNEL);
1821         if (!async_copy)
1822             goto out_err;
1823         async_copy->cp_src = kmalloc(sizeof(*async_copy->cp_src), GFP_KERNEL);
1824         if (!async_copy->cp_src)
1825             goto out_err;
1826         if (!nfs4_init_copy_state(nn, copy))
1827             goto out_err;
1828         refcount_set(&async_copy->refcount, 1);
1829         memcpy(&copy->cp_res.cb_stateid, &copy->cp_stateid.stid,
1830             sizeof(copy->cp_res.cb_stateid));
1831         dup_copy_fields(copy, async_copy);
1832         async_copy->copy_task = kthread_create(nfsd4_do_async_copy,
1833                 async_copy, "%s", "copy thread");
1834         if (IS_ERR(async_copy->copy_task))
1835             goto out_err;
1836         spin_lock(&async_copy->cp_clp->async_lock);
1837         list_add(&async_copy->copies,
1838                 &async_copy->cp_clp->async_copies);
1839         spin_unlock(&async_copy->cp_clp->async_lock);
1840         wake_up_process(async_copy->copy_task);
1841         status = nfs_ok;
1842     } else {
1843         status = nfsd4_do_copy(copy, copy->nf_src->nf_file,
1844                        copy->nf_dst->nf_file, true);
1845         nfsd4_cleanup_intra_ssc(copy->nf_src, copy->nf_dst);
1846     }
1847 out:
1848     return status;
1849 out_err:
1850     if (async_copy)
1851         cleanup_async_copy(async_copy);
1852     status = nfserrno(-ENOMEM);
1853     if (nfsd4_ssc_is_inter(copy))
1854         nfsd4_interssc_disconnect(copy->ss_mnt);
1855     goto out;
1856 }
1857 
1858 struct nfsd4_copy *
1859 find_async_copy(struct nfs4_client *clp, stateid_t *stateid)
1860 {
1861     struct nfsd4_copy *copy;
1862 
1863     spin_lock(&clp->async_lock);
1864     list_for_each_entry(copy, &clp->async_copies, copies) {
1865         if (memcmp(&copy->cp_stateid.stid, stateid, NFS4_STATEID_SIZE))
1866             continue;
1867         refcount_inc(&copy->refcount);
1868         spin_unlock(&clp->async_lock);
1869         return copy;
1870     }
1871     spin_unlock(&clp->async_lock);
1872     return NULL;
1873 }
1874 
1875 static __be32
1876 nfsd4_offload_cancel(struct svc_rqst *rqstp,
1877              struct nfsd4_compound_state *cstate,
1878              union nfsd4_op_u *u)
1879 {
1880     struct nfsd4_offload_status *os = &u->offload_status;
1881     struct nfsd4_copy *copy;
1882     struct nfs4_client *clp = cstate->clp;
1883 
1884     copy = find_async_copy(clp, &os->stateid);
1885     if (!copy) {
1886         struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1887 
1888         return manage_cpntf_state(nn, &os->stateid, clp, NULL);
1889     } else
1890         nfsd4_stop_copy(copy);
1891 
1892     return nfs_ok;
1893 }
1894 
1895 static __be32
1896 nfsd4_copy_notify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1897           union nfsd4_op_u *u)
1898 {
1899     struct nfsd4_copy_notify *cn = &u->copy_notify;
1900     __be32 status;
1901     struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1902     struct nfs4_stid *stid;
1903     struct nfs4_cpntf_state *cps;
1904     struct nfs4_client *clp = cstate->clp;
1905 
1906     status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1907                     &cn->cpn_src_stateid, RD_STATE, NULL,
1908                     &stid);
1909     if (status)
1910         return status;
1911 
1912     cn->cpn_sec = nn->nfsd4_lease;
1913     cn->cpn_nsec = 0;
1914 
1915     status = nfserrno(-ENOMEM);
1916     cps = nfs4_alloc_init_cpntf_state(nn, stid);
1917     if (!cps)
1918         goto out;
1919     memcpy(&cn->cpn_cnr_stateid, &cps->cp_stateid.stid, sizeof(stateid_t));
1920     memcpy(&cps->cp_p_stateid, &stid->sc_stateid, sizeof(stateid_t));
1921     memcpy(&cps->cp_p_clid, &clp->cl_clientid, sizeof(clientid_t));
1922 
1923     /* For now, only return one server address in cpn_src, the
1924      * address used by the client to connect to this server.
1925      */
1926     cn->cpn_src->nl4_type = NL4_NETADDR;
1927     status = nfsd4_set_netaddr((struct sockaddr *)&rqstp->rq_daddr,
1928                  &cn->cpn_src->u.nl4_addr);
1929     WARN_ON_ONCE(status);
1930     if (status) {
1931         nfs4_put_cpntf_state(nn, cps);
1932         goto out;
1933     }
1934 out:
1935     nfs4_put_stid(stid);
1936     return status;
1937 }
1938 
1939 static __be32
1940 nfsd4_fallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1941         struct nfsd4_fallocate *fallocate, int flags)
1942 {
1943     __be32 status;
1944     struct nfsd_file *nf;
1945 
1946     status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
1947                         &fallocate->falloc_stateid,
1948                         WR_STATE, &nf, NULL);
1949     if (status != nfs_ok) {
1950         dprintk("NFSD: nfsd4_fallocate: couldn't process stateid!\n");
1951         return status;
1952     }
1953 
1954     status = nfsd4_vfs_fallocate(rqstp, &cstate->current_fh, nf->nf_file,
1955                      fallocate->falloc_offset,
1956                      fallocate->falloc_length,
1957                      flags);
1958     nfsd_file_put(nf);
1959     return status;
1960 }
1961 static __be32
1962 nfsd4_offload_status(struct svc_rqst *rqstp,
1963              struct nfsd4_compound_state *cstate,
1964              union nfsd4_op_u *u)
1965 {
1966     struct nfsd4_offload_status *os = &u->offload_status;
1967     __be32 status = 0;
1968     struct nfsd4_copy *copy;
1969     struct nfs4_client *clp = cstate->clp;
1970 
1971     copy = find_async_copy(clp, &os->stateid);
1972     if (copy) {
1973         os->count = copy->cp_res.wr_bytes_written;
1974         nfs4_put_copy(copy);
1975     } else
1976         status = nfserr_bad_stateid;
1977 
1978     return status;
1979 }
1980 
1981 static __be32
1982 nfsd4_allocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1983            union nfsd4_op_u *u)
1984 {
1985     return nfsd4_fallocate(rqstp, cstate, &u->allocate, 0);
1986 }
1987 
1988 static __be32
1989 nfsd4_deallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1990          union nfsd4_op_u *u)
1991 {
1992     return nfsd4_fallocate(rqstp, cstate, &u->deallocate,
1993                    FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
1994 }
1995 
1996 static __be32
1997 nfsd4_seek(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1998        union nfsd4_op_u *u)
1999 {
2000     struct nfsd4_seek *seek = &u->seek;
2001     int whence;
2002     __be32 status;
2003     struct nfsd_file *nf;
2004 
2005     status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
2006                         &seek->seek_stateid,
2007                         RD_STATE, &nf, NULL);
2008     if (status) {
2009         dprintk("NFSD: nfsd4_seek: couldn't process stateid!\n");
2010         return status;
2011     }
2012 
2013     switch (seek->seek_whence) {
2014     case NFS4_CONTENT_DATA:
2015         whence = SEEK_DATA;
2016         break;
2017     case NFS4_CONTENT_HOLE:
2018         whence = SEEK_HOLE;
2019         break;
2020     default:
2021         status = nfserr_union_notsupp;
2022         goto out;
2023     }
2024 
2025     /*
2026      * Note:  This call does change file->f_pos, but nothing in NFSD
2027      *        should ever file->f_pos.
2028      */
2029     seek->seek_pos = vfs_llseek(nf->nf_file, seek->seek_offset, whence);
2030     if (seek->seek_pos < 0)
2031         status = nfserrno(seek->seek_pos);
2032     else if (seek->seek_pos >= i_size_read(file_inode(nf->nf_file)))
2033         seek->seek_eof = true;
2034 
2035 out:
2036     nfsd_file_put(nf);
2037     return status;
2038 }
2039 
2040 /* This routine never returns NFS_OK!  If there are no other errors, it
2041  * will return NFSERR_SAME or NFSERR_NOT_SAME depending on whether the
2042  * attributes matched.  VERIFY is implemented by mapping NFSERR_SAME
2043  * to NFS_OK after the call; NVERIFY by mapping NFSERR_NOT_SAME to NFS_OK.
2044  */
2045 static __be32
2046 _nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2047          struct nfsd4_verify *verify)
2048 {
2049     __be32 *buf, *p;
2050     int count;
2051     __be32 status;
2052 
2053     status = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_NOP);
2054     if (status)
2055         return status;
2056 
2057     status = check_attr_support(rqstp, cstate, verify->ve_bmval, NULL);
2058     if (status)
2059         return status;
2060 
2061     if ((verify->ve_bmval[0] & FATTR4_WORD0_RDATTR_ERROR)
2062         || (verify->ve_bmval[1] & NFSD_WRITEONLY_ATTRS_WORD1))
2063         return nfserr_inval;
2064     if (verify->ve_attrlen & 3)
2065         return nfserr_inval;
2066 
2067     /* count in words:
2068      *   bitmap_len(1) + bitmap(2) + attr_len(1) = 4
2069      */
2070     count = 4 + (verify->ve_attrlen >> 2);
2071     buf = kmalloc(count << 2, GFP_KERNEL);
2072     if (!buf)
2073         return nfserr_jukebox;
2074 
2075     p = buf;
2076     status = nfsd4_encode_fattr_to_buf(&p, count, &cstate->current_fh,
2077                     cstate->current_fh.fh_export,
2078                     cstate->current_fh.fh_dentry,
2079                     verify->ve_bmval,
2080                     rqstp, 0);
2081     /*
2082      * If nfsd4_encode_fattr() ran out of space, assume that's because
2083      * the attributes are longer (hence different) than those given:
2084      */
2085     if (status == nfserr_resource)
2086         status = nfserr_not_same;
2087     if (status)
2088         goto out_kfree;
2089 
2090     /* skip bitmap */
2091     p = buf + 1 + ntohl(buf[0]);
2092     status = nfserr_not_same;
2093     if (ntohl(*p++) != verify->ve_attrlen)
2094         goto out_kfree;
2095     if (!memcmp(p, verify->ve_attrval, verify->ve_attrlen))
2096         status = nfserr_same;
2097 
2098 out_kfree:
2099     kfree(buf);
2100     return status;
2101 }
2102 
2103 static __be32
2104 nfsd4_nverify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2105           union nfsd4_op_u *u)
2106 {
2107     __be32 status;
2108 
2109     status = _nfsd4_verify(rqstp, cstate, &u->verify);
2110     return status == nfserr_not_same ? nfs_ok : status;
2111 }
2112 
2113 static __be32
2114 nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2115          union nfsd4_op_u *u)
2116 {
2117     __be32 status;
2118 
2119     status = _nfsd4_verify(rqstp, cstate, &u->nverify);
2120     return status == nfserr_same ? nfs_ok : status;
2121 }
2122 
2123 #ifdef CONFIG_NFSD_PNFS
2124 static const struct nfsd4_layout_ops *
2125 nfsd4_layout_verify(struct svc_export *exp, unsigned int layout_type)
2126 {
2127     if (!exp->ex_layout_types) {
2128         dprintk("%s: export does not support pNFS\n", __func__);
2129         return NULL;
2130     }
2131 
2132     if (layout_type >= LAYOUT_TYPE_MAX ||
2133         !(exp->ex_layout_types & (1 << layout_type))) {
2134         dprintk("%s: layout type %d not supported\n",
2135             __func__, layout_type);
2136         return NULL;
2137     }
2138 
2139     return nfsd4_layout_ops[layout_type];
2140 }
2141 
2142 static __be32
2143 nfsd4_getdeviceinfo(struct svc_rqst *rqstp,
2144         struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
2145 {
2146     struct nfsd4_getdeviceinfo *gdp = &u->getdeviceinfo;
2147     const struct nfsd4_layout_ops *ops;
2148     struct nfsd4_deviceid_map *map;
2149     struct svc_export *exp;
2150     __be32 nfserr;
2151 
2152     dprintk("%s: layout_type %u dev_id [0x%llx:0x%x] maxcnt %u\n",
2153            __func__,
2154            gdp->gd_layout_type,
2155            gdp->gd_devid.fsid_idx, gdp->gd_devid.generation,
2156            gdp->gd_maxcount);
2157 
2158     map = nfsd4_find_devid_map(gdp->gd_devid.fsid_idx);
2159     if (!map) {
2160         dprintk("%s: couldn't find device ID to export mapping!\n",
2161             __func__);
2162         return nfserr_noent;
2163     }
2164 
2165     exp = rqst_exp_find(rqstp, map->fsid_type, map->fsid);
2166     if (IS_ERR(exp)) {
2167         dprintk("%s: could not find device id\n", __func__);
2168         return nfserr_noent;
2169     }
2170 
2171     nfserr = nfserr_layoutunavailable;
2172     ops = nfsd4_layout_verify(exp, gdp->gd_layout_type);
2173     if (!ops)
2174         goto out;
2175 
2176     nfserr = nfs_ok;
2177     if (gdp->gd_maxcount != 0) {
2178         nfserr = ops->proc_getdeviceinfo(exp->ex_path.mnt->mnt_sb,
2179                 rqstp, cstate->clp, gdp);
2180     }
2181 
2182     gdp->gd_notify_types &= ops->notify_types;
2183 out:
2184     exp_put(exp);
2185     return nfserr;
2186 }
2187 
2188 static void
2189 nfsd4_getdeviceinfo_release(union nfsd4_op_u *u)
2190 {
2191     kfree(u->getdeviceinfo.gd_device);
2192 }
2193 
2194 static __be32
2195 nfsd4_layoutget(struct svc_rqst *rqstp,
2196         struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
2197 {
2198     struct nfsd4_layoutget *lgp = &u->layoutget;
2199     struct svc_fh *current_fh = &cstate->current_fh;
2200     const struct nfsd4_layout_ops *ops;
2201     struct nfs4_layout_stateid *ls;
2202     __be32 nfserr;
2203     int accmode = NFSD_MAY_READ_IF_EXEC;
2204 
2205     switch (lgp->lg_seg.iomode) {
2206     case IOMODE_READ:
2207         accmode |= NFSD_MAY_READ;
2208         break;
2209     case IOMODE_RW:
2210         accmode |= NFSD_MAY_READ | NFSD_MAY_WRITE;
2211         break;
2212     default:
2213         dprintk("%s: invalid iomode %d\n",
2214             __func__, lgp->lg_seg.iomode);
2215         nfserr = nfserr_badiomode;
2216         goto out;
2217     }
2218 
2219     nfserr = fh_verify(rqstp, current_fh, 0, accmode);
2220     if (nfserr)
2221         goto out;
2222 
2223     nfserr = nfserr_layoutunavailable;
2224     ops = nfsd4_layout_verify(current_fh->fh_export, lgp->lg_layout_type);
2225     if (!ops)
2226         goto out;
2227 
2228     /*
2229      * Verify minlength and range as per RFC5661:
2230      *  o  If loga_length is less than loga_minlength,
2231      *     the metadata server MUST return NFS4ERR_INVAL.
2232      *  o  If the sum of loga_offset and loga_minlength exceeds
2233      *     NFS4_UINT64_MAX, and loga_minlength is not
2234      *     NFS4_UINT64_MAX, the error NFS4ERR_INVAL MUST result.
2235      *  o  If the sum of loga_offset and loga_length exceeds
2236      *     NFS4_UINT64_MAX, and loga_length is not NFS4_UINT64_MAX,
2237      *     the error NFS4ERR_INVAL MUST result.
2238      */
2239     nfserr = nfserr_inval;
2240     if (lgp->lg_seg.length < lgp->lg_minlength ||
2241         (lgp->lg_minlength != NFS4_MAX_UINT64 &&
2242          lgp->lg_minlength > NFS4_MAX_UINT64 - lgp->lg_seg.offset) ||
2243         (lgp->lg_seg.length != NFS4_MAX_UINT64 &&
2244          lgp->lg_seg.length > NFS4_MAX_UINT64 - lgp->lg_seg.offset))
2245         goto out;
2246     if (lgp->lg_seg.length == 0)
2247         goto out;
2248 
2249     nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lgp->lg_sid,
2250                         true, lgp->lg_layout_type, &ls);
2251     if (nfserr) {
2252         trace_nfsd_layout_get_lookup_fail(&lgp->lg_sid);
2253         goto out;
2254     }
2255 
2256     nfserr = nfserr_recallconflict;
2257     if (atomic_read(&ls->ls_stid.sc_file->fi_lo_recalls))
2258         goto out_put_stid;
2259 
2260     nfserr = ops->proc_layoutget(d_inode(current_fh->fh_dentry),
2261                      current_fh, lgp);
2262     if (nfserr)
2263         goto out_put_stid;
2264 
2265     nfserr = nfsd4_insert_layout(lgp, ls);
2266 
2267 out_put_stid:
2268     mutex_unlock(&ls->ls_mutex);
2269     nfs4_put_stid(&ls->ls_stid);
2270 out:
2271     return nfserr;
2272 }
2273 
2274 static void
2275 nfsd4_layoutget_release(union nfsd4_op_u *u)
2276 {
2277     kfree(u->layoutget.lg_content);
2278 }
2279 
2280 static __be32
2281 nfsd4_layoutcommit(struct svc_rqst *rqstp,
2282         struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
2283 {
2284     struct nfsd4_layoutcommit *lcp = &u->layoutcommit;
2285     const struct nfsd4_layout_seg *seg = &lcp->lc_seg;
2286     struct svc_fh *current_fh = &cstate->current_fh;
2287     const struct nfsd4_layout_ops *ops;
2288     loff_t new_size = lcp->lc_last_wr + 1;
2289     struct inode *inode;
2290     struct nfs4_layout_stateid *ls;
2291     __be32 nfserr;
2292 
2293     nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_WRITE);
2294     if (nfserr)
2295         goto out;
2296 
2297     nfserr = nfserr_layoutunavailable;
2298     ops = nfsd4_layout_verify(current_fh->fh_export, lcp->lc_layout_type);
2299     if (!ops)
2300         goto out;
2301     inode = d_inode(current_fh->fh_dentry);
2302 
2303     nfserr = nfserr_inval;
2304     if (new_size <= seg->offset) {
2305         dprintk("pnfsd: last write before layout segment\n");
2306         goto out;
2307     }
2308     if (new_size > seg->offset + seg->length) {
2309         dprintk("pnfsd: last write beyond layout segment\n");
2310         goto out;
2311     }
2312     if (!lcp->lc_newoffset && new_size > i_size_read(inode)) {
2313         dprintk("pnfsd: layoutcommit beyond EOF\n");
2314         goto out;
2315     }
2316 
2317     nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lcp->lc_sid,
2318                         false, lcp->lc_layout_type,
2319                         &ls);
2320     if (nfserr) {
2321         trace_nfsd_layout_commit_lookup_fail(&lcp->lc_sid);
2322         /* fixup error code as per RFC5661 */
2323         if (nfserr == nfserr_bad_stateid)
2324             nfserr = nfserr_badlayout;
2325         goto out;
2326     }
2327 
2328     /* LAYOUTCOMMIT does not require any serialization */
2329     mutex_unlock(&ls->ls_mutex);
2330 
2331     if (new_size > i_size_read(inode)) {
2332         lcp->lc_size_chg = 1;
2333         lcp->lc_newsize = new_size;
2334     } else {
2335         lcp->lc_size_chg = 0;
2336     }
2337 
2338     nfserr = ops->proc_layoutcommit(inode, lcp);
2339     nfs4_put_stid(&ls->ls_stid);
2340 out:
2341     return nfserr;
2342 }
2343 
2344 static __be32
2345 nfsd4_layoutreturn(struct svc_rqst *rqstp,
2346         struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
2347 {
2348     struct nfsd4_layoutreturn *lrp = &u->layoutreturn;
2349     struct svc_fh *current_fh = &cstate->current_fh;
2350     __be32 nfserr;
2351 
2352     nfserr = fh_verify(rqstp, current_fh, 0, NFSD_MAY_NOP);
2353     if (nfserr)
2354         goto out;
2355 
2356     nfserr = nfserr_layoutunavailable;
2357     if (!nfsd4_layout_verify(current_fh->fh_export, lrp->lr_layout_type))
2358         goto out;
2359 
2360     switch (lrp->lr_seg.iomode) {
2361     case IOMODE_READ:
2362     case IOMODE_RW:
2363     case IOMODE_ANY:
2364         break;
2365     default:
2366         dprintk("%s: invalid iomode %d\n", __func__,
2367             lrp->lr_seg.iomode);
2368         nfserr = nfserr_inval;
2369         goto out;
2370     }
2371 
2372     switch (lrp->lr_return_type) {
2373     case RETURN_FILE:
2374         nfserr = nfsd4_return_file_layouts(rqstp, cstate, lrp);
2375         break;
2376     case RETURN_FSID:
2377     case RETURN_ALL:
2378         nfserr = nfsd4_return_client_layouts(rqstp, cstate, lrp);
2379         break;
2380     default:
2381         dprintk("%s: invalid return_type %d\n", __func__,
2382             lrp->lr_return_type);
2383         nfserr = nfserr_inval;
2384         break;
2385     }
2386 out:
2387     return nfserr;
2388 }
2389 #endif /* CONFIG_NFSD_PNFS */
2390 
2391 static __be32
2392 nfsd4_getxattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2393            union nfsd4_op_u *u)
2394 {
2395     struct nfsd4_getxattr *getxattr = &u->getxattr;
2396 
2397     return nfsd_getxattr(rqstp, &cstate->current_fh,
2398                  getxattr->getxa_name, &getxattr->getxa_buf,
2399                  &getxattr->getxa_len);
2400 }
2401 
2402 static __be32
2403 nfsd4_setxattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2404        union nfsd4_op_u *u)
2405 {
2406     struct nfsd4_setxattr *setxattr = &u->setxattr;
2407     __be32 ret;
2408 
2409     if (opens_in_grace(SVC_NET(rqstp)))
2410         return nfserr_grace;
2411 
2412     ret = nfsd_setxattr(rqstp, &cstate->current_fh, setxattr->setxa_name,
2413                 setxattr->setxa_buf, setxattr->setxa_len,
2414                 setxattr->setxa_flags);
2415 
2416     if (!ret)
2417         set_change_info(&setxattr->setxa_cinfo, &cstate->current_fh);
2418 
2419     return ret;
2420 }
2421 
2422 static __be32
2423 nfsd4_listxattrs(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2424        union nfsd4_op_u *u)
2425 {
2426     /*
2427      * Get the entire list, then copy out only the user attributes
2428      * in the encode function.
2429      */
2430     return nfsd_listxattr(rqstp, &cstate->current_fh,
2431                  &u->listxattrs.lsxa_buf, &u->listxattrs.lsxa_len);
2432 }
2433 
2434 static __be32
2435 nfsd4_removexattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2436        union nfsd4_op_u *u)
2437 {
2438     struct nfsd4_removexattr *removexattr = &u->removexattr;
2439     __be32 ret;
2440 
2441     if (opens_in_grace(SVC_NET(rqstp)))
2442         return nfserr_grace;
2443 
2444     ret = nfsd_removexattr(rqstp, &cstate->current_fh,
2445         removexattr->rmxa_name);
2446 
2447     if (!ret)
2448         set_change_info(&removexattr->rmxa_cinfo, &cstate->current_fh);
2449 
2450     return ret;
2451 }
2452 
2453 /*
2454  * NULL call.
2455  */
2456 static __be32
2457 nfsd4_proc_null(struct svc_rqst *rqstp)
2458 {
2459     return rpc_success;
2460 }
2461 
2462 static inline void nfsd4_increment_op_stats(u32 opnum)
2463 {
2464     if (opnum >= FIRST_NFS4_OP && opnum <= LAST_NFS4_OP)
2465         percpu_counter_inc(&nfsdstats.counter[NFSD_STATS_NFS4_OP(opnum)]);
2466 }
2467 
2468 static const struct nfsd4_operation nfsd4_ops[];
2469 
2470 static const char *nfsd4_op_name(unsigned opnum);
2471 
2472 /*
2473  * Enforce NFSv4.1 COMPOUND ordering rules:
2474  *
2475  * Also note, enforced elsewhere:
2476  *  - SEQUENCE other than as first op results in
2477  *    NFS4ERR_SEQUENCE_POS. (Enforced in nfsd4_sequence().)
2478  *  - BIND_CONN_TO_SESSION must be the only op in its compound.
2479  *    (Enforced in nfsd4_bind_conn_to_session().)
2480  *  - DESTROY_SESSION must be the final operation in a compound, if
2481  *    sessionid's in SEQUENCE and DESTROY_SESSION are the same.
2482  *    (Enforced in nfsd4_destroy_session().)
2483  */
2484 static __be32 nfs41_check_op_ordering(struct nfsd4_compoundargs *args)
2485 {
2486     struct nfsd4_op *first_op = &args->ops[0];
2487 
2488     /* These ordering requirements don't apply to NFSv4.0: */
2489     if (args->minorversion == 0)
2490         return nfs_ok;
2491     /* This is weird, but OK, not our problem: */
2492     if (args->opcnt == 0)
2493         return nfs_ok;
2494     if (first_op->status == nfserr_op_illegal)
2495         return nfs_ok;
2496     if (!(nfsd4_ops[first_op->opnum].op_flags & ALLOWED_AS_FIRST_OP))
2497         return nfserr_op_not_in_session;
2498     if (first_op->opnum == OP_SEQUENCE)
2499         return nfs_ok;
2500     /*
2501      * So first_op is something allowed outside a session, like
2502      * EXCHANGE_ID; but then it has to be the only op in the
2503      * compound:
2504      */
2505     if (args->opcnt != 1)
2506         return nfserr_not_only_op;
2507     return nfs_ok;
2508 }
2509 
2510 const struct nfsd4_operation *OPDESC(struct nfsd4_op *op)
2511 {
2512     return &nfsd4_ops[op->opnum];
2513 }
2514 
2515 bool nfsd4_cache_this_op(struct nfsd4_op *op)
2516 {
2517     if (op->opnum == OP_ILLEGAL)
2518         return false;
2519     return OPDESC(op)->op_flags & OP_CACHEME;
2520 }
2521 
2522 static bool need_wrongsec_check(struct svc_rqst *rqstp)
2523 {
2524     struct nfsd4_compoundres *resp = rqstp->rq_resp;
2525     struct nfsd4_compoundargs *argp = rqstp->rq_argp;
2526     struct nfsd4_op *this = &argp->ops[resp->opcnt - 1];
2527     struct nfsd4_op *next = &argp->ops[resp->opcnt];
2528     const struct nfsd4_operation *thisd = OPDESC(this);
2529     const struct nfsd4_operation *nextd;
2530 
2531     /*
2532      * Most ops check wronsec on our own; only the putfh-like ops
2533      * have special rules.
2534      */
2535     if (!(thisd->op_flags & OP_IS_PUTFH_LIKE))
2536         return false;
2537     /*
2538      * rfc 5661 2.6.3.1.1.6: don't bother erroring out a
2539      * put-filehandle operation if we're not going to use the
2540      * result:
2541      */
2542     if (argp->opcnt == resp->opcnt)
2543         return false;
2544     if (next->opnum == OP_ILLEGAL)
2545         return false;
2546     nextd = OPDESC(next);
2547     /*
2548      * Rest of 2.6.3.1.1: certain operations will return WRONGSEC
2549      * errors themselves as necessary; others should check for them
2550      * now:
2551      */
2552     return !(nextd->op_flags & OP_HANDLES_WRONGSEC);
2553 }
2554 
2555 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
2556 static void
2557 check_if_stalefh_allowed(struct nfsd4_compoundargs *args)
2558 {
2559     struct nfsd4_op *op, *current_op = NULL, *saved_op = NULL;
2560     struct nfsd4_copy *copy;
2561     struct nfsd4_putfh *putfh;
2562     int i;
2563 
2564     /* traverse all operation and if it's a COPY compound, mark the
2565      * source filehandle to skip verification
2566      */
2567     for (i = 0; i < args->opcnt; i++) {
2568         op = &args->ops[i];
2569         if (op->opnum == OP_PUTFH)
2570             current_op = op;
2571         else if (op->opnum == OP_SAVEFH)
2572             saved_op = current_op;
2573         else if (op->opnum == OP_RESTOREFH)
2574             current_op = saved_op;
2575         else if (op->opnum == OP_COPY) {
2576             copy = (struct nfsd4_copy *)&op->u;
2577             if (!saved_op) {
2578                 op->status = nfserr_nofilehandle;
2579                 return;
2580             }
2581             putfh = (struct nfsd4_putfh *)&saved_op->u;
2582             if (nfsd4_ssc_is_inter(copy))
2583                 putfh->no_verify = true;
2584         }
2585     }
2586 }
2587 #else
2588 static void
2589 check_if_stalefh_allowed(struct nfsd4_compoundargs *args)
2590 {
2591 }
2592 #endif
2593 
2594 /*
2595  * COMPOUND call.
2596  */
2597 static __be32
2598 nfsd4_proc_compound(struct svc_rqst *rqstp)
2599 {
2600     struct nfsd4_compoundargs *args = rqstp->rq_argp;
2601     struct nfsd4_compoundres *resp = rqstp->rq_resp;
2602     struct nfsd4_op *op;
2603     struct nfsd4_compound_state *cstate = &resp->cstate;
2604     struct svc_fh *current_fh = &cstate->current_fh;
2605     struct svc_fh *save_fh = &cstate->save_fh;
2606     struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2607     __be32      status;
2608 
2609     resp->xdr = &rqstp->rq_res_stream;
2610     resp->statusp = resp->xdr->p;
2611 
2612     /* reserve space for: NFS status code */
2613     xdr_reserve_space(resp->xdr, XDR_UNIT);
2614 
2615     /* reserve space for: taglen, tag, and opcnt */
2616     xdr_reserve_space(resp->xdr, XDR_UNIT * 2 + args->taglen);
2617     resp->taglen = args->taglen;
2618     resp->tag = args->tag;
2619     resp->rqstp = rqstp;
2620     cstate->minorversion = args->minorversion;
2621     fh_init(current_fh, NFS4_FHSIZE);
2622     fh_init(save_fh, NFS4_FHSIZE);
2623 
2624     /*
2625      * Don't use the deferral mechanism for NFSv4; compounds make it
2626      * too hard to avoid non-idempotency problems.
2627      */
2628     __clear_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
2629 
2630     /*
2631      * According to RFC3010, this takes precedence over all other errors.
2632      */
2633     status = nfserr_minor_vers_mismatch;
2634     if (nfsd_minorversion(nn, args->minorversion, NFSD_TEST) <= 0)
2635         goto out;
2636     status = nfserr_resource;
2637     if (args->opcnt > NFSD_MAX_OPS_PER_COMPOUND)
2638         goto out;
2639 
2640     status = nfs41_check_op_ordering(args);
2641     if (status) {
2642         op = &args->ops[0];
2643         op->status = status;
2644         resp->opcnt = 1;
2645         goto encode_op;
2646     }
2647     check_if_stalefh_allowed(args);
2648 
2649     rqstp->rq_lease_breaker = (void **)&cstate->clp;
2650 
2651     trace_nfsd_compound(rqstp, args->opcnt);
2652     while (!status && resp->opcnt < args->opcnt) {
2653         op = &args->ops[resp->opcnt++];
2654 
2655         /*
2656          * The XDR decode routines may have pre-set op->status;
2657          * for example, if there is a miscellaneous XDR error
2658          * it will be set to nfserr_bad_xdr.
2659          */
2660         if (op->status) {
2661             if (op->opnum == OP_OPEN)
2662                 op->status = nfsd4_open_omfg(rqstp, cstate, op);
2663             goto encode_op;
2664         }
2665         if (!current_fh->fh_dentry &&
2666                 !HAS_FH_FLAG(current_fh, NFSD4_FH_FOREIGN)) {
2667             if (!(op->opdesc->op_flags & ALLOWED_WITHOUT_FH)) {
2668                 op->status = nfserr_nofilehandle;
2669                 goto encode_op;
2670             }
2671         } else if (current_fh->fh_export &&
2672                current_fh->fh_export->ex_fslocs.migrated &&
2673               !(op->opdesc->op_flags & ALLOWED_ON_ABSENT_FS)) {
2674             op->status = nfserr_moved;
2675             goto encode_op;
2676         }
2677 
2678         fh_clear_pre_post_attrs(current_fh);
2679 
2680         /* If op is non-idempotent */
2681         if (op->opdesc->op_flags & OP_MODIFIES_SOMETHING) {
2682             /*
2683              * Don't execute this op if we couldn't encode a
2684              * successful reply:
2685              */
2686             u32 plen = op->opdesc->op_rsize_bop(rqstp, op);
2687             /*
2688              * Plus if there's another operation, make sure
2689              * we'll have space to at least encode an error:
2690              */
2691             if (resp->opcnt < args->opcnt)
2692                 plen += COMPOUND_ERR_SLACK_SPACE;
2693             op->status = nfsd4_check_resp_size(resp, plen);
2694         }
2695 
2696         if (op->status)
2697             goto encode_op;
2698 
2699         if (op->opdesc->op_get_currentstateid)
2700             op->opdesc->op_get_currentstateid(cstate, &op->u);
2701         op->status = op->opdesc->op_func(rqstp, cstate, &op->u);
2702 
2703         /* Only from SEQUENCE */
2704         if (cstate->status == nfserr_replay_cache) {
2705             dprintk("%s NFS4.1 replay from cache\n", __func__);
2706             status = op->status;
2707             goto out;
2708         }
2709         if (!op->status) {
2710             if (op->opdesc->op_set_currentstateid)
2711                 op->opdesc->op_set_currentstateid(cstate, &op->u);
2712 
2713             if (op->opdesc->op_flags & OP_CLEAR_STATEID)
2714                 clear_current_stateid(cstate);
2715 
2716             if (current_fh->fh_export &&
2717                     need_wrongsec_check(rqstp))
2718                 op->status = check_nfsd_access(current_fh->fh_export, rqstp);
2719         }
2720 encode_op:
2721         if (op->status == nfserr_replay_me) {
2722             op->replay = &cstate->replay_owner->so_replay;
2723             nfsd4_encode_replay(resp->xdr, op);
2724             status = op->status = op->replay->rp_status;
2725         } else {
2726             nfsd4_encode_operation(resp, op);
2727             status = op->status;
2728         }
2729 
2730         trace_nfsd_compound_status(args->opcnt, resp->opcnt, status,
2731                        nfsd4_op_name(op->opnum));
2732 
2733         nfsd4_cstate_clear_replay(cstate);
2734         nfsd4_increment_op_stats(op->opnum);
2735     }
2736 
2737     fh_put(current_fh);
2738     fh_put(save_fh);
2739     BUG_ON(cstate->replay_owner);
2740 out:
2741     cstate->status = status;
2742     /* Reset deferral mechanism for RPC deferrals */
2743     __set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
2744     return rpc_success;
2745 }
2746 
2747 #define op_encode_hdr_size      (2)
2748 #define op_encode_stateid_maxsz     (XDR_QUADLEN(NFS4_STATEID_SIZE))
2749 #define op_encode_verifier_maxsz    (XDR_QUADLEN(NFS4_VERIFIER_SIZE))
2750 #define op_encode_change_info_maxsz (5)
2751 #define nfs4_fattr_bitmap_maxsz     (4)
2752 
2753 /* We'll fall back on returning no lockowner if run out of space: */
2754 #define op_encode_lockowner_maxsz   (0)
2755 #define op_encode_lock_denied_maxsz (8 + op_encode_lockowner_maxsz)
2756 
2757 #define nfs4_owner_maxsz        (1 + XDR_QUADLEN(IDMAP_NAMESZ))
2758 
2759 #define op_encode_ace_maxsz     (3 + nfs4_owner_maxsz)
2760 #define op_encode_delegation_maxsz  (1 + op_encode_stateid_maxsz + 1 + \
2761                      op_encode_ace_maxsz)
2762 
2763 #define op_encode_channel_attrs_maxsz   (6 + 1 + 1)
2764 
2765 static inline u32 nfsd4_only_status_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2766 {
2767     return (op_encode_hdr_size) * sizeof(__be32);
2768 }
2769 
2770 static inline u32 nfsd4_status_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2771 {
2772     return (op_encode_hdr_size + op_encode_stateid_maxsz)* sizeof(__be32);
2773 }
2774 
2775 static inline u32 nfsd4_access_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2776 {
2777     /* ac_supported, ac_resp_access */
2778     return (op_encode_hdr_size + 2)* sizeof(__be32);
2779 }
2780 
2781 static inline u32 nfsd4_commit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2782 {
2783     return (op_encode_hdr_size + op_encode_verifier_maxsz) * sizeof(__be32);
2784 }
2785 
2786 static inline u32 nfsd4_create_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2787 {
2788     return (op_encode_hdr_size + op_encode_change_info_maxsz
2789         + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
2790 }
2791 
2792 /*
2793  * Note since this is an idempotent operation we won't insist on failing
2794  * the op prematurely if the estimate is too large.  We may turn off splice
2795  * reads unnecessarily.
2796  */
2797 static inline u32 nfsd4_getattr_rsize(struct svc_rqst *rqstp,
2798                       struct nfsd4_op *op)
2799 {
2800     u32 *bmap = op->u.getattr.ga_bmval;
2801     u32 bmap0 = bmap[0], bmap1 = bmap[1], bmap2 = bmap[2];
2802     u32 ret = 0;
2803 
2804     if (bmap0 & FATTR4_WORD0_ACL)
2805         return svc_max_payload(rqstp);
2806     if (bmap0 & FATTR4_WORD0_FS_LOCATIONS)
2807         return svc_max_payload(rqstp);
2808 
2809     if (bmap1 & FATTR4_WORD1_OWNER) {
2810         ret += IDMAP_NAMESZ + 4;
2811         bmap1 &= ~FATTR4_WORD1_OWNER;
2812     }
2813     if (bmap1 & FATTR4_WORD1_OWNER_GROUP) {
2814         ret += IDMAP_NAMESZ + 4;
2815         bmap1 &= ~FATTR4_WORD1_OWNER_GROUP;
2816     }
2817     if (bmap0 & FATTR4_WORD0_FILEHANDLE) {
2818         ret += NFS4_FHSIZE + 4;
2819         bmap0 &= ~FATTR4_WORD0_FILEHANDLE;
2820     }
2821     if (bmap2 & FATTR4_WORD2_SECURITY_LABEL) {
2822         ret += NFS4_MAXLABELLEN + 12;
2823         bmap2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2824     }
2825     /*
2826      * Largest of remaining attributes are 16 bytes (e.g.,
2827      * supported_attributes)
2828      */
2829     ret += 16 * (hweight32(bmap0) + hweight32(bmap1) + hweight32(bmap2));
2830     /* bitmask, length */
2831     ret += 20;
2832     return ret;
2833 }
2834 
2835 static inline u32 nfsd4_getfh_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2836 {
2837     return (op_encode_hdr_size + 1) * sizeof(__be32) + NFS4_FHSIZE;
2838 }
2839 
2840 static inline u32 nfsd4_link_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2841 {
2842     return (op_encode_hdr_size + op_encode_change_info_maxsz)
2843         * sizeof(__be32);
2844 }
2845 
2846 static inline u32 nfsd4_lock_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2847 {
2848     return (op_encode_hdr_size + op_encode_lock_denied_maxsz)
2849         * sizeof(__be32);
2850 }
2851 
2852 static inline u32 nfsd4_open_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2853 {
2854     return (op_encode_hdr_size + op_encode_stateid_maxsz
2855         + op_encode_change_info_maxsz + 1
2856         + nfs4_fattr_bitmap_maxsz
2857         + op_encode_delegation_maxsz) * sizeof(__be32);
2858 }
2859 
2860 static inline u32 nfsd4_read_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2861 {
2862     u32 maxcount = 0, rlen = 0;
2863 
2864     maxcount = svc_max_payload(rqstp);
2865     rlen = min(op->u.read.rd_length, maxcount);
2866 
2867     return (op_encode_hdr_size + 2 + XDR_QUADLEN(rlen)) * sizeof(__be32);
2868 }
2869 
2870 static inline u32 nfsd4_read_plus_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2871 {
2872     u32 maxcount = svc_max_payload(rqstp);
2873     u32 rlen = min(op->u.read.rd_length, maxcount);
2874     /*
2875      * If we detect that the file changed during hole encoding, then we
2876      * recover by encoding the remaining reply as data. This means we need
2877      * to set aside enough room to encode two data segments.
2878      */
2879     u32 seg_len = 2 * (1 + 2 + 1);
2880 
2881     return (op_encode_hdr_size + 2 + seg_len + XDR_QUADLEN(rlen)) * sizeof(__be32);
2882 }
2883 
2884 static inline u32 nfsd4_readdir_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2885 {
2886     u32 maxcount = 0, rlen = 0;
2887 
2888     maxcount = svc_max_payload(rqstp);
2889     rlen = min(op->u.readdir.rd_maxcount, maxcount);
2890 
2891     return (op_encode_hdr_size + op_encode_verifier_maxsz +
2892         XDR_QUADLEN(rlen)) * sizeof(__be32);
2893 }
2894 
2895 static inline u32 nfsd4_readlink_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2896 {
2897     return (op_encode_hdr_size + 1) * sizeof(__be32) + PAGE_SIZE;
2898 }
2899 
2900 static inline u32 nfsd4_remove_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2901 {
2902     return (op_encode_hdr_size + op_encode_change_info_maxsz)
2903         * sizeof(__be32);
2904 }
2905 
2906 static inline u32 nfsd4_rename_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2907 {
2908     return (op_encode_hdr_size + op_encode_change_info_maxsz
2909         + op_encode_change_info_maxsz) * sizeof(__be32);
2910 }
2911 
2912 static inline u32 nfsd4_sequence_rsize(struct svc_rqst *rqstp,
2913                        struct nfsd4_op *op)
2914 {
2915     return (op_encode_hdr_size
2916         + XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 5) * sizeof(__be32);
2917 }
2918 
2919 static inline u32 nfsd4_test_stateid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2920 {
2921     return (op_encode_hdr_size + 1 + op->u.test_stateid.ts_num_ids)
2922         * sizeof(__be32);
2923 }
2924 
2925 static inline u32 nfsd4_setattr_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2926 {
2927     return (op_encode_hdr_size + nfs4_fattr_bitmap_maxsz) * sizeof(__be32);
2928 }
2929 
2930 static inline u32 nfsd4_secinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2931 {
2932     return (op_encode_hdr_size + RPC_AUTH_MAXFLAVOR *
2933         (4 + XDR_QUADLEN(GSS_OID_MAX_LEN))) * sizeof(__be32);
2934 }
2935 
2936 static inline u32 nfsd4_setclientid_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2937 {
2938     return (op_encode_hdr_size + 2 + XDR_QUADLEN(NFS4_VERIFIER_SIZE)) *
2939                                 sizeof(__be32);
2940 }
2941 
2942 static inline u32 nfsd4_write_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2943 {
2944     return (op_encode_hdr_size + 2 + op_encode_verifier_maxsz) * sizeof(__be32);
2945 }
2946 
2947 static inline u32 nfsd4_exchange_id_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2948 {
2949     return (op_encode_hdr_size + 2 + 1 + /* eir_clientid, eir_sequenceid */\
2950         1 + 1 + /* eir_flags, spr_how */\
2951         4 + /* spo_must_enforce & _allow with bitmap */\
2952         2 + /*eir_server_owner.so_minor_id */\
2953         /* eir_server_owner.so_major_id<> */\
2954         XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
2955         /* eir_server_scope<> */\
2956         XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 +\
2957         1 + /* eir_server_impl_id array length */\
2958         0 /* ignored eir_server_impl_id contents */) * sizeof(__be32);
2959 }
2960 
2961 static inline u32 nfsd4_bind_conn_to_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2962 {
2963     return (op_encode_hdr_size + \
2964         XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* bctsr_sessid */\
2965         2 /* bctsr_dir, use_conn_in_rdma_mode */) * sizeof(__be32);
2966 }
2967 
2968 static inline u32 nfsd4_create_session_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2969 {
2970     return (op_encode_hdr_size + \
2971         XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + /* sessionid */\
2972         2 + /* csr_sequence, csr_flags */\
2973         op_encode_channel_attrs_maxsz + \
2974         op_encode_channel_attrs_maxsz) * sizeof(__be32);
2975 }
2976 
2977 static inline u32 nfsd4_copy_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
2978 {
2979     return (op_encode_hdr_size +
2980         1 /* wr_callback */ +
2981         op_encode_stateid_maxsz /* wr_callback */ +
2982         2 /* wr_count */ +
2983         1 /* wr_committed */ +
2984         op_encode_verifier_maxsz +
2985         1 /* cr_consecutive */ +
2986         1 /* cr_synchronous */) * sizeof(__be32);
2987 }
2988 
2989 static inline u32 nfsd4_offload_status_rsize(struct svc_rqst *rqstp,
2990                          struct nfsd4_op *op)
2991 {
2992     return (op_encode_hdr_size +
2993         2 /* osr_count */ +
2994         1 /* osr_complete<1> optional 0 for now */) * sizeof(__be32);
2995 }
2996 
2997 static inline u32 nfsd4_copy_notify_rsize(struct svc_rqst *rqstp,
2998                     struct nfsd4_op *op)
2999 {
3000     return (op_encode_hdr_size +
3001         3 /* cnr_lease_time */ +
3002         1 /* We support one cnr_source_server */ +
3003         1 /* cnr_stateid seq */ +
3004         op_encode_stateid_maxsz /* cnr_stateid */ +
3005         1 /* num cnr_source_server*/ +
3006         1 /* nl4_type */ +
3007         1 /* nl4 size */ +
3008         XDR_QUADLEN(NFS4_OPAQUE_LIMIT) /*nl4_loc + nl4_loc_sz */)
3009         * sizeof(__be32);
3010 }
3011 
3012 #ifdef CONFIG_NFSD_PNFS
3013 static inline u32 nfsd4_getdeviceinfo_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
3014 {
3015     u32 maxcount = 0, rlen = 0;
3016 
3017     maxcount = svc_max_payload(rqstp);
3018     rlen = min(op->u.getdeviceinfo.gd_maxcount, maxcount);
3019 
3020     return (op_encode_hdr_size +
3021         1 /* gd_layout_type*/ +
3022         XDR_QUADLEN(rlen) +
3023         2 /* gd_notify_types */) * sizeof(__be32);
3024 }
3025 
3026 /*
3027  * At this stage we don't really know what layout driver will handle the request,
3028  * so we need to define an arbitrary upper bound here.
3029  */
3030 #define MAX_LAYOUT_SIZE     128
3031 static inline u32 nfsd4_layoutget_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
3032 {
3033     return (op_encode_hdr_size +
3034         1 /* logr_return_on_close */ +
3035         op_encode_stateid_maxsz +
3036         1 /* nr of layouts */ +
3037         MAX_LAYOUT_SIZE) * sizeof(__be32);
3038 }
3039 
3040 static inline u32 nfsd4_layoutcommit_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
3041 {
3042     return (op_encode_hdr_size +
3043         1 /* locr_newsize */ +
3044         2 /* ns_size */) * sizeof(__be32);
3045 }
3046 
3047 static inline u32 nfsd4_layoutreturn_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
3048 {
3049     return (op_encode_hdr_size +
3050         1 /* lrs_stateid */ +
3051         op_encode_stateid_maxsz) * sizeof(__be32);
3052 }
3053 #endif /* CONFIG_NFSD_PNFS */
3054 
3055 
3056 static inline u32 nfsd4_seek_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
3057 {
3058     return (op_encode_hdr_size + 3) * sizeof(__be32);
3059 }
3060 
3061 static inline u32 nfsd4_getxattr_rsize(struct svc_rqst *rqstp,
3062                        struct nfsd4_op *op)
3063 {
3064     u32 maxcount, rlen;
3065 
3066     maxcount = svc_max_payload(rqstp);
3067     rlen = min_t(u32, XATTR_SIZE_MAX, maxcount);
3068 
3069     return (op_encode_hdr_size + 1 + XDR_QUADLEN(rlen)) * sizeof(__be32);
3070 }
3071 
3072 static inline u32 nfsd4_setxattr_rsize(struct svc_rqst *rqstp,
3073                        struct nfsd4_op *op)
3074 {
3075     return (op_encode_hdr_size + op_encode_change_info_maxsz)
3076         * sizeof(__be32);
3077 }
3078 static inline u32 nfsd4_listxattrs_rsize(struct svc_rqst *rqstp,
3079                      struct nfsd4_op *op)
3080 {
3081     u32 maxcount, rlen;
3082 
3083     maxcount = svc_max_payload(rqstp);
3084     rlen = min(op->u.listxattrs.lsxa_maxcount, maxcount);
3085 
3086     return (op_encode_hdr_size + 4 + XDR_QUADLEN(rlen)) * sizeof(__be32);
3087 }
3088 
3089 static inline u32 nfsd4_removexattr_rsize(struct svc_rqst *rqstp,
3090                       struct nfsd4_op *op)
3091 {
3092     return (op_encode_hdr_size + op_encode_change_info_maxsz)
3093         * sizeof(__be32);
3094 }
3095 
3096 
3097 static const struct nfsd4_operation nfsd4_ops[] = {
3098     [OP_ACCESS] = {
3099         .op_func = nfsd4_access,
3100         .op_name = "OP_ACCESS",
3101         .op_rsize_bop = nfsd4_access_rsize,
3102     },
3103     [OP_CLOSE] = {
3104         .op_func = nfsd4_close,
3105         .op_flags = OP_MODIFIES_SOMETHING,
3106         .op_name = "OP_CLOSE",
3107         .op_rsize_bop = nfsd4_status_stateid_rsize,
3108         .op_get_currentstateid = nfsd4_get_closestateid,
3109         .op_set_currentstateid = nfsd4_set_closestateid,
3110     },
3111     [OP_COMMIT] = {
3112         .op_func = nfsd4_commit,
3113         .op_flags = OP_MODIFIES_SOMETHING,
3114         .op_name = "OP_COMMIT",
3115         .op_rsize_bop = nfsd4_commit_rsize,
3116     },
3117     [OP_CREATE] = {
3118         .op_func = nfsd4_create,
3119         .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME | OP_CLEAR_STATEID,
3120         .op_name = "OP_CREATE",
3121         .op_rsize_bop = nfsd4_create_rsize,
3122     },
3123     [OP_DELEGRETURN] = {
3124         .op_func = nfsd4_delegreturn,
3125         .op_flags = OP_MODIFIES_SOMETHING,
3126         .op_name = "OP_DELEGRETURN",
3127         .op_rsize_bop = nfsd4_only_status_rsize,
3128         .op_get_currentstateid = nfsd4_get_delegreturnstateid,
3129     },
3130     [OP_GETATTR] = {
3131         .op_func = nfsd4_getattr,
3132         .op_flags = ALLOWED_ON_ABSENT_FS,
3133         .op_rsize_bop = nfsd4_getattr_rsize,
3134         .op_name = "OP_GETATTR",
3135     },
3136     [OP_GETFH] = {
3137         .op_func = nfsd4_getfh,
3138         .op_name = "OP_GETFH",
3139         .op_rsize_bop = nfsd4_getfh_rsize,
3140     },
3141     [OP_LINK] = {
3142         .op_func = nfsd4_link,
3143         .op_flags = ALLOWED_ON_ABSENT_FS | OP_MODIFIES_SOMETHING
3144                 | OP_CACHEME,
3145         .op_name = "OP_LINK",
3146         .op_rsize_bop = nfsd4_link_rsize,
3147     },
3148     [OP_LOCK] = {
3149         .op_func = nfsd4_lock,
3150         .op_flags = OP_MODIFIES_SOMETHING |
3151                 OP_NONTRIVIAL_ERROR_ENCODE,
3152         .op_name = "OP_LOCK",
3153         .op_rsize_bop = nfsd4_lock_rsize,
3154         .op_set_currentstateid = nfsd4_set_lockstateid,
3155     },
3156     [OP_LOCKT] = {
3157         .op_func = nfsd4_lockt,
3158         .op_flags = OP_NONTRIVIAL_ERROR_ENCODE,
3159         .op_name = "OP_LOCKT",
3160         .op_rsize_bop = nfsd4_lock_rsize,
3161     },
3162     [OP_LOCKU] = {
3163         .op_func = nfsd4_locku,
3164         .op_flags = OP_MODIFIES_SOMETHING,
3165         .op_name = "OP_LOCKU",
3166         .op_rsize_bop = nfsd4_status_stateid_rsize,
3167         .op_get_currentstateid = nfsd4_get_lockustateid,
3168     },
3169     [OP_LOOKUP] = {
3170         .op_func = nfsd4_lookup,
3171         .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
3172         .op_name = "OP_LOOKUP",
3173         .op_rsize_bop = nfsd4_only_status_rsize,
3174     },
3175     [OP_LOOKUPP] = {
3176         .op_func = nfsd4_lookupp,
3177         .op_flags = OP_HANDLES_WRONGSEC | OP_CLEAR_STATEID,
3178         .op_name = "OP_LOOKUPP",
3179         .op_rsize_bop = nfsd4_only_status_rsize,
3180     },
3181     [OP_NVERIFY] = {
3182         .op_func = nfsd4_nverify,
3183         .op_name = "OP_NVERIFY",
3184         .op_rsize_bop = nfsd4_only_status_rsize,
3185     },
3186     [OP_OPEN] = {
3187         .op_func = nfsd4_open,
3188         .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
3189         .op_name = "OP_OPEN",
3190         .op_rsize_bop = nfsd4_open_rsize,
3191         .op_set_currentstateid = nfsd4_set_openstateid,
3192     },
3193     [OP_OPEN_CONFIRM] = {
3194         .op_func = nfsd4_open_confirm,
3195         .op_flags = OP_MODIFIES_SOMETHING,
3196         .op_name = "OP_OPEN_CONFIRM",
3197         .op_rsize_bop = nfsd4_status_stateid_rsize,
3198     },
3199     [OP_OPEN_DOWNGRADE] = {
3200         .op_func = nfsd4_open_downgrade,
3201         .op_flags = OP_MODIFIES_SOMETHING,
3202         .op_name = "OP_OPEN_DOWNGRADE",
3203         .op_rsize_bop = nfsd4_status_stateid_rsize,
3204         .op_get_currentstateid = nfsd4_get_opendowngradestateid,
3205         .op_set_currentstateid = nfsd4_set_opendowngradestateid,
3206     },
3207     [OP_PUTFH] = {
3208         .op_func = nfsd4_putfh,
3209         .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3210                 | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
3211         .op_name = "OP_PUTFH",
3212         .op_rsize_bop = nfsd4_only_status_rsize,
3213     },
3214     [OP_PUTPUBFH] = {
3215         .op_func = nfsd4_putrootfh,
3216         .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3217                 | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
3218         .op_name = "OP_PUTPUBFH",
3219         .op_rsize_bop = nfsd4_only_status_rsize,
3220     },
3221     [OP_PUTROOTFH] = {
3222         .op_func = nfsd4_putrootfh,
3223         .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3224                 | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
3225         .op_name = "OP_PUTROOTFH",
3226         .op_rsize_bop = nfsd4_only_status_rsize,
3227     },
3228     [OP_READ] = {
3229         .op_func = nfsd4_read,
3230         .op_release = nfsd4_read_release,
3231         .op_name = "OP_READ",
3232         .op_rsize_bop = nfsd4_read_rsize,
3233         .op_get_currentstateid = nfsd4_get_readstateid,
3234     },
3235     [OP_READDIR] = {
3236         .op_func = nfsd4_readdir,
3237         .op_name = "OP_READDIR",
3238         .op_rsize_bop = nfsd4_readdir_rsize,
3239     },
3240     [OP_READLINK] = {
3241         .op_func = nfsd4_readlink,
3242         .op_name = "OP_READLINK",
3243         .op_rsize_bop = nfsd4_readlink_rsize,
3244     },
3245     [OP_REMOVE] = {
3246         .op_func = nfsd4_remove,
3247         .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3248         .op_name = "OP_REMOVE",
3249         .op_rsize_bop = nfsd4_remove_rsize,
3250     },
3251     [OP_RENAME] = {
3252         .op_func = nfsd4_rename,
3253         .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3254         .op_name = "OP_RENAME",
3255         .op_rsize_bop = nfsd4_rename_rsize,
3256     },
3257     [OP_RENEW] = {
3258         .op_func = nfsd4_renew,
3259         .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3260                 | OP_MODIFIES_SOMETHING,
3261         .op_name = "OP_RENEW",
3262         .op_rsize_bop = nfsd4_only_status_rsize,
3263 
3264     },
3265     [OP_RESTOREFH] = {
3266         .op_func = nfsd4_restorefh,
3267         .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3268                 | OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING,
3269         .op_name = "OP_RESTOREFH",
3270         .op_rsize_bop = nfsd4_only_status_rsize,
3271     },
3272     [OP_SAVEFH] = {
3273         .op_func = nfsd4_savefh,
3274         .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
3275         .op_name = "OP_SAVEFH",
3276         .op_rsize_bop = nfsd4_only_status_rsize,
3277     },
3278     [OP_SECINFO] = {
3279         .op_func = nfsd4_secinfo,
3280         .op_release = nfsd4_secinfo_release,
3281         .op_flags = OP_HANDLES_WRONGSEC,
3282         .op_name = "OP_SECINFO",
3283         .op_rsize_bop = nfsd4_secinfo_rsize,
3284     },
3285     [OP_SETATTR] = {
3286         .op_func = nfsd4_setattr,
3287         .op_name = "OP_SETATTR",
3288         .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME
3289                 | OP_NONTRIVIAL_ERROR_ENCODE,
3290         .op_rsize_bop = nfsd4_setattr_rsize,
3291         .op_get_currentstateid = nfsd4_get_setattrstateid,
3292     },
3293     [OP_SETCLIENTID] = {
3294         .op_func = nfsd4_setclientid,
3295         .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3296                 | OP_MODIFIES_SOMETHING | OP_CACHEME
3297                 | OP_NONTRIVIAL_ERROR_ENCODE,
3298         .op_name = "OP_SETCLIENTID",
3299         .op_rsize_bop = nfsd4_setclientid_rsize,
3300     },
3301     [OP_SETCLIENTID_CONFIRM] = {
3302         .op_func = nfsd4_setclientid_confirm,
3303         .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3304                 | OP_MODIFIES_SOMETHING | OP_CACHEME,
3305         .op_name = "OP_SETCLIENTID_CONFIRM",
3306         .op_rsize_bop = nfsd4_only_status_rsize,
3307     },
3308     [OP_VERIFY] = {
3309         .op_func = nfsd4_verify,
3310         .op_name = "OP_VERIFY",
3311         .op_rsize_bop = nfsd4_only_status_rsize,
3312     },
3313     [OP_WRITE] = {
3314         .op_func = nfsd4_write,
3315         .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3316         .op_name = "OP_WRITE",
3317         .op_rsize_bop = nfsd4_write_rsize,
3318         .op_get_currentstateid = nfsd4_get_writestateid,
3319     },
3320     [OP_RELEASE_LOCKOWNER] = {
3321         .op_func = nfsd4_release_lockowner,
3322         .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
3323                 | OP_MODIFIES_SOMETHING,
3324         .op_name = "OP_RELEASE_LOCKOWNER",
3325         .op_rsize_bop = nfsd4_only_status_rsize,
3326     },
3327 
3328     /* NFSv4.1 operations */
3329     [OP_EXCHANGE_ID] = {
3330         .op_func = nfsd4_exchange_id,
3331         .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3332                 | OP_MODIFIES_SOMETHING,
3333         .op_name = "OP_EXCHANGE_ID",
3334         .op_rsize_bop = nfsd4_exchange_id_rsize,
3335     },
3336     [OP_BACKCHANNEL_CTL] = {
3337         .op_func = nfsd4_backchannel_ctl,
3338         .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
3339         .op_name = "OP_BACKCHANNEL_CTL",
3340         .op_rsize_bop = nfsd4_only_status_rsize,
3341     },
3342     [OP_BIND_CONN_TO_SESSION] = {
3343         .op_func = nfsd4_bind_conn_to_session,
3344         .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3345                 | OP_MODIFIES_SOMETHING,
3346         .op_name = "OP_BIND_CONN_TO_SESSION",
3347         .op_rsize_bop = nfsd4_bind_conn_to_session_rsize,
3348     },
3349     [OP_CREATE_SESSION] = {
3350         .op_func = nfsd4_create_session,
3351         .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3352                 | OP_MODIFIES_SOMETHING,
3353         .op_name = "OP_CREATE_SESSION",
3354         .op_rsize_bop = nfsd4_create_session_rsize,
3355     },
3356     [OP_DESTROY_SESSION] = {
3357         .op_func = nfsd4_destroy_session,
3358         .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3359                 | OP_MODIFIES_SOMETHING,
3360         .op_name = "OP_DESTROY_SESSION",
3361         .op_rsize_bop = nfsd4_only_status_rsize,
3362     },
3363     [OP_SEQUENCE] = {
3364         .op_func = nfsd4_sequence,
3365         .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP,
3366         .op_name = "OP_SEQUENCE",
3367         .op_rsize_bop = nfsd4_sequence_rsize,
3368     },
3369     [OP_DESTROY_CLIENTID] = {
3370         .op_func = nfsd4_destroy_clientid,
3371         .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_AS_FIRST_OP
3372                 | OP_MODIFIES_SOMETHING,
3373         .op_name = "OP_DESTROY_CLIENTID",
3374         .op_rsize_bop = nfsd4_only_status_rsize,
3375     },
3376     [OP_RECLAIM_COMPLETE] = {
3377         .op_func = nfsd4_reclaim_complete,
3378         .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
3379         .op_name = "OP_RECLAIM_COMPLETE",
3380         .op_rsize_bop = nfsd4_only_status_rsize,
3381     },
3382     [OP_SECINFO_NO_NAME] = {
3383         .op_func = nfsd4_secinfo_no_name,
3384         .op_release = nfsd4_secinfo_no_name_release,
3385         .op_flags = OP_HANDLES_WRONGSEC,
3386         .op_name = "OP_SECINFO_NO_NAME",
3387         .op_rsize_bop = nfsd4_secinfo_rsize,
3388     },
3389     [OP_TEST_STATEID] = {
3390         .op_func = nfsd4_test_stateid,
3391         .op_flags = ALLOWED_WITHOUT_FH,
3392         .op_name = "OP_TEST_STATEID",
3393         .op_rsize_bop = nfsd4_test_stateid_rsize,
3394     },
3395     [OP_FREE_STATEID] = {
3396         .op_func = nfsd4_free_stateid,
3397         .op_flags = ALLOWED_WITHOUT_FH | OP_MODIFIES_SOMETHING,
3398         .op_name = "OP_FREE_STATEID",
3399         .op_get_currentstateid = nfsd4_get_freestateid,
3400         .op_rsize_bop = nfsd4_only_status_rsize,
3401     },
3402 #ifdef CONFIG_NFSD_PNFS
3403     [OP_GETDEVICEINFO] = {
3404         .op_func = nfsd4_getdeviceinfo,
3405         .op_release = nfsd4_getdeviceinfo_release,
3406         .op_flags = ALLOWED_WITHOUT_FH,
3407         .op_name = "OP_GETDEVICEINFO",
3408         .op_rsize_bop = nfsd4_getdeviceinfo_rsize,
3409     },
3410     [OP_LAYOUTGET] = {
3411         .op_func = nfsd4_layoutget,
3412         .op_release = nfsd4_layoutget_release,
3413         .op_flags = OP_MODIFIES_SOMETHING,
3414         .op_name = "OP_LAYOUTGET",
3415         .op_rsize_bop = nfsd4_layoutget_rsize,
3416     },
3417     [OP_LAYOUTCOMMIT] = {
3418         .op_func = nfsd4_layoutcommit,
3419         .op_flags = OP_MODIFIES_SOMETHING,
3420         .op_name = "OP_LAYOUTCOMMIT",
3421         .op_rsize_bop = nfsd4_layoutcommit_rsize,
3422     },
3423     [OP_LAYOUTRETURN] = {
3424         .op_func = nfsd4_layoutreturn,
3425         .op_flags = OP_MODIFIES_SOMETHING,
3426         .op_name = "OP_LAYOUTRETURN",
3427         .op_rsize_bop = nfsd4_layoutreturn_rsize,
3428     },
3429 #endif /* CONFIG_NFSD_PNFS */
3430 
3431     /* NFSv4.2 operations */
3432     [OP_ALLOCATE] = {
3433         .op_func = nfsd4_allocate,
3434         .op_flags = OP_MODIFIES_SOMETHING,
3435         .op_name = "OP_ALLOCATE",
3436         .op_rsize_bop = nfsd4_only_status_rsize,
3437     },
3438     [OP_DEALLOCATE] = {
3439         .op_func = nfsd4_deallocate,
3440         .op_flags = OP_MODIFIES_SOMETHING,
3441         .op_name = "OP_DEALLOCATE",
3442         .op_rsize_bop = nfsd4_only_status_rsize,
3443     },
3444     [OP_CLONE] = {
3445         .op_func = nfsd4_clone,
3446         .op_flags = OP_MODIFIES_SOMETHING,
3447         .op_name = "OP_CLONE",
3448         .op_rsize_bop = nfsd4_only_status_rsize,
3449     },
3450     [OP_COPY] = {
3451         .op_func = nfsd4_copy,
3452         .op_flags = OP_MODIFIES_SOMETHING,
3453         .op_name = "OP_COPY",
3454         .op_rsize_bop = nfsd4_copy_rsize,
3455     },
3456     [OP_READ_PLUS] = {
3457         .op_func = nfsd4_read,
3458         .op_release = nfsd4_read_release,
3459         .op_name = "OP_READ_PLUS",
3460         .op_rsize_bop = nfsd4_read_plus_rsize,
3461         .op_get_currentstateid = nfsd4_get_readstateid,
3462     },
3463     [OP_SEEK] = {
3464         .op_func = nfsd4_seek,
3465         .op_name = "OP_SEEK",
3466         .op_rsize_bop = nfsd4_seek_rsize,
3467     },
3468     [OP_OFFLOAD_STATUS] = {
3469         .op_func = nfsd4_offload_status,
3470         .op_name = "OP_OFFLOAD_STATUS",
3471         .op_rsize_bop = nfsd4_offload_status_rsize,
3472     },
3473     [OP_OFFLOAD_CANCEL] = {
3474         .op_func = nfsd4_offload_cancel,
3475         .op_flags = OP_MODIFIES_SOMETHING,
3476         .op_name = "OP_OFFLOAD_CANCEL",
3477         .op_rsize_bop = nfsd4_only_status_rsize,
3478     },
3479     [OP_COPY_NOTIFY] = {
3480         .op_func = nfsd4_copy_notify,
3481         .op_flags = OP_MODIFIES_SOMETHING,
3482         .op_name = "OP_COPY_NOTIFY",
3483         .op_rsize_bop = nfsd4_copy_notify_rsize,
3484     },
3485     [OP_GETXATTR] = {
3486         .op_func = nfsd4_getxattr,
3487         .op_name = "OP_GETXATTR",
3488         .op_rsize_bop = nfsd4_getxattr_rsize,
3489     },
3490     [OP_SETXATTR] = {
3491         .op_func = nfsd4_setxattr,
3492         .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3493         .op_name = "OP_SETXATTR",
3494         .op_rsize_bop = nfsd4_setxattr_rsize,
3495     },
3496     [OP_LISTXATTRS] = {
3497         .op_func = nfsd4_listxattrs,
3498         .op_name = "OP_LISTXATTRS",
3499         .op_rsize_bop = nfsd4_listxattrs_rsize,
3500     },
3501     [OP_REMOVEXATTR] = {
3502         .op_func = nfsd4_removexattr,
3503         .op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
3504         .op_name = "OP_REMOVEXATTR",
3505         .op_rsize_bop = nfsd4_removexattr_rsize,
3506     },
3507 };
3508 
3509 /**
3510  * nfsd4_spo_must_allow - Determine if the compound op contains an
3511  * operation that is allowed to be sent with machine credentials
3512  *
3513  * @rqstp: a pointer to the struct svc_rqst
3514  *
3515  * Checks to see if the compound contains a spo_must_allow op
3516  * and confirms that it was sent with the proper machine creds.
3517  */
3518 
3519 bool nfsd4_spo_must_allow(struct svc_rqst *rqstp)
3520 {
3521     struct nfsd4_compoundres *resp = rqstp->rq_resp;
3522     struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3523     struct nfsd4_op *this;
3524     struct nfsd4_compound_state *cstate = &resp->cstate;
3525     struct nfs4_op_map *allow = &cstate->clp->cl_spo_must_allow;
3526     u32 opiter;
3527 
3528     if (!cstate->minorversion)
3529         return false;
3530 
3531     if (cstate->spo_must_allowed)
3532         return true;
3533 
3534     opiter = resp->opcnt;
3535     while (opiter < argp->opcnt) {
3536         this = &argp->ops[opiter++];
3537         if (test_bit(this->opnum, allow->u.longs) &&
3538             cstate->clp->cl_mach_cred &&
3539             nfsd4_mach_creds_match(cstate->clp, rqstp)) {
3540             cstate->spo_must_allowed = true;
3541             return true;
3542         }
3543     }
3544     cstate->spo_must_allowed = false;
3545     return false;
3546 }
3547 
3548 int nfsd4_max_reply(struct svc_rqst *rqstp, struct nfsd4_op *op)
3549 {
3550     if (op->opnum == OP_ILLEGAL || op->status == nfserr_notsupp)
3551         return op_encode_hdr_size * sizeof(__be32);
3552 
3553     BUG_ON(OPDESC(op)->op_rsize_bop == NULL);
3554     return OPDESC(op)->op_rsize_bop(rqstp, op);
3555 }
3556 
3557 void warn_on_nonidempotent_op(struct nfsd4_op *op)
3558 {
3559     if (OPDESC(op)->op_flags & OP_MODIFIES_SOMETHING) {
3560         pr_err("unable to encode reply to nonidempotent op %u (%s)\n",
3561             op->opnum, nfsd4_op_name(op->opnum));
3562         WARN_ON_ONCE(1);
3563     }
3564 }
3565 
3566 static const char *nfsd4_op_name(unsigned opnum)
3567 {
3568     if (opnum < ARRAY_SIZE(nfsd4_ops))
3569         return nfsd4_ops[opnum].op_name;
3570     return "unknown_operation";
3571 }
3572 
3573 static const struct svc_procedure nfsd_procedures4[2] = {
3574     [NFSPROC4_NULL] = {
3575         .pc_func = nfsd4_proc_null,
3576         .pc_decode = nfssvc_decode_voidarg,
3577         .pc_encode = nfssvc_encode_voidres,
3578         .pc_argsize = sizeof(struct nfsd_voidargs),
3579         .pc_ressize = sizeof(struct nfsd_voidres),
3580         .pc_cachetype = RC_NOCACHE,
3581         .pc_xdrressize = 1,
3582         .pc_name = "NULL",
3583     },
3584     [NFSPROC4_COMPOUND] = {
3585         .pc_func = nfsd4_proc_compound,
3586         .pc_decode = nfs4svc_decode_compoundargs,
3587         .pc_encode = nfs4svc_encode_compoundres,
3588         .pc_argsize = sizeof(struct nfsd4_compoundargs),
3589         .pc_ressize = sizeof(struct nfsd4_compoundres),
3590         .pc_release = nfsd4_release_compoundargs,
3591         .pc_cachetype = RC_NOCACHE,
3592         .pc_xdrressize = NFSD_BUFSIZE/4,
3593         .pc_name = "COMPOUND",
3594     },
3595 };
3596 
3597 static unsigned int nfsd_count3[ARRAY_SIZE(nfsd_procedures4)];
3598 const struct svc_version nfsd_version4 = {
3599     .vs_vers        = 4,
3600     .vs_nproc       = 2,
3601     .vs_proc        = nfsd_procedures4,
3602     .vs_count       = nfsd_count3,
3603     .vs_dispatch        = nfsd_dispatch,
3604     .vs_xdrsize     = NFS4_SVC_XDRSIZE,
3605     .vs_rpcb_optnl      = true,
3606     .vs_need_cong_ctrl  = true,
3607 };