nfsd4: teach encoders to handle reserve_space failures
[firefly-linux-kernel-4.4.55.git] / fs / nfsd / nfs4proc.c
index 9c23696bf36bbfe0a8e1882e13a888738818d02a..6a5b701961a4d2651649d89278bae404594de2f9 100644 (file)
@@ -786,7 +786,6 @@ nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
        if (!nfsd4_last_compound_op(rqstp))
                rqstp->rq_splice_ok = false;
 
-       nfs4_lock_state();
        /* check stateid */
        if ((status = nfs4_preprocess_stateid_op(SVC_NET(rqstp),
                                                 cstate, &read->rd_stateid,
@@ -794,11 +793,8 @@ nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
                dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
                goto out;
        }
-       if (read->rd_filp)
-               get_file(read->rd_filp);
        status = nfs_ok;
 out:
-       nfs4_unlock_state();
        read->rd_rqstp = rqstp;
        read->rd_fhp = &cstate->current_fh;
        return status;
@@ -937,10 +933,8 @@ nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
        int err;
 
        if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
-               nfs4_lock_state();
                status = nfs4_preprocess_stateid_op(SVC_NET(rqstp), cstate,
                        &setattr->sa_stateid, WR_STATE, NULL);
-               nfs4_unlock_state();
                if (status) {
                        dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
                        return status;
@@ -1006,17 +1000,12 @@ nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
        if (write->wr_offset >= OFFSET_MAX)
                return nfserr_inval;
 
-       nfs4_lock_state();
        status = nfs4_preprocess_stateid_op(SVC_NET(rqstp),
                                        cstate, stateid, WR_STATE, &filp);
        if (status) {
-               nfs4_unlock_state();
                dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
                return status;
        }
-       if (filp)
-               get_file(filp);
-       nfs4_unlock_state();
 
        cnt = write->wr_buflen;
        write->wr_how_written = write->wr_stable_how;
@@ -1072,10 +1061,10 @@ _nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
                return nfserr_jukebox;
 
        p = buf;
-       status = nfsd4_encode_fattr(&cstate->current_fh,
+       status = nfsd4_encode_fattr_to_buf(&p, count, &cstate->current_fh,
                                    cstate->current_fh.fh_export,
-                                   cstate->current_fh.fh_dentry, &p,
-                                   count, verify->ve_bmval,
+                                   cstate->current_fh.fh_dentry,
+                                   verify->ve_bmval,
                                    rqstp, 0);
        /*
         * If nfsd4_encode_fattr() ran out of space, assume that's because
@@ -1182,9 +1171,7 @@ struct nfsd4_operation {
 
 static struct nfsd4_operation nfsd4_ops[];
 
-#ifdef NFSD_DEBUG
 static const char *nfsd4_op_name(unsigned opnum);
-#endif
 
 /*
  * Enforce NFSv4.1 COMPOUND ordering rules:
@@ -1226,6 +1213,8 @@ static inline struct nfsd4_operation *OPDESC(struct nfsd4_op *op)
 
 bool nfsd4_cache_this_op(struct nfsd4_op *op)
 {
+       if (op->opnum == OP_ILLEGAL)
+               return false;
        return OPDESC(op)->op_flags & OP_CACHEME;
 }
 
@@ -1262,6 +1251,21 @@ static bool need_wrongsec_check(struct svc_rqst *rqstp)
        return !(nextd->op_flags & OP_HANDLES_WRONGSEC);
 }
 
+static void svcxdr_init_encode(struct svc_rqst *rqstp,
+                              struct nfsd4_compoundres *resp)
+{
+       struct xdr_stream *xdr = &resp->xdr;
+       struct xdr_buf *buf = &rqstp->rq_res;
+       struct kvec *head = buf->head;
+
+       xdr->buf = buf;
+       xdr->iov = head;
+       xdr->p   = head->iov_base + head->iov_len;
+       xdr->end = head->iov_base + PAGE_SIZE - 2 * RPC_MAX_AUTH_SIZE;
+       /* Tail and page_len should be zero at this point: */
+       buf->len = buf->head[0].iov_len;
+}
+
 /*
  * COMPOUND call.
  */
@@ -1279,13 +1283,10 @@ nfsd4_proc_compound(struct svc_rqst *rqstp,
        u32             plen = 0;
        __be32          status;
 
-       resp->xbuf = &rqstp->rq_res;
-       resp->p = rqstp->rq_res.head[0].iov_base +
-                                               rqstp->rq_res.head[0].iov_len;
-       resp->tagp = resp->p;
+       svcxdr_init_encode(rqstp, resp);
+       resp->tagp = resp->xdr.p;
        /* reserve space for: taglen, tag, and opcnt */
-       resp->p += 2 + XDR_QUADLEN(args->taglen);
-       resp->end = rqstp->rq_res.head[0].iov_base + PAGE_SIZE;
+       xdr_reserve_space(&resp->xdr, 8 + args->taglen);
        resp->taglen = args->taglen;
        resp->tag = args->tag;
        resp->opcnt = 0;
@@ -1337,7 +1338,7 @@ nfsd4_proc_compound(struct svc_rqst *rqstp,
                 * failed response to the next operation.  If we don't
                 * have enough room, fail with ERR_RESOURCE.
                 */
-               slack_bytes = (char *)resp->end - (char *)resp->p;
+               slack_bytes = (char *)resp->xdr.end - (char *)resp->xdr.p;
                if (slack_bytes < COMPOUND_SLACK_SPACE
                                + COMPOUND_ERR_SLACK_SPACE) {
                        BUG_ON(slack_bytes < COMPOUND_ERR_SLACK_SPACE);
@@ -1358,6 +1359,8 @@ nfsd4_proc_compound(struct svc_rqst *rqstp,
                        goto encode_op;
                }
 
+               fh_clear_wcc(current_fh);
+
                /* If op is non-idempotent */
                if (opdesc->op_flags & OP_MODIFIES_SOMETHING) {
                        plen = opdesc->op_rsize_bop(rqstp, op);
@@ -1397,7 +1400,7 @@ encode_op:
                }
                if (op->status == nfserr_replay_me) {
                        op->replay = &cstate->replay_owner->so_replay;
-                       nfsd4_encode_replay(resp, op);
+                       nfsd4_encode_replay(&resp->xdr, op);
                        status = op->status = op->replay->rp_status;
                } else {
                        nfsd4_encode_operation(resp, op);
@@ -1498,7 +1501,7 @@ static inline u32 nfsd4_read_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
        if (rlen > maxcount)
                rlen = maxcount;
 
-       return (op_encode_hdr_size + 2) * sizeof(__be32) + rlen;
+       return (op_encode_hdr_size + 2 + XDR_QUADLEN(rlen)) * sizeof(__be32);
 }
 
 static inline u32 nfsd4_readdir_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
@@ -1537,7 +1540,7 @@ static inline u32 nfsd4_setclientid_rsize(struct svc_rqst *rqstp, struct nfsd4_o
 
 static inline u32 nfsd4_write_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
 {
-       return (op_encode_hdr_size + op_encode_verifier_maxsz) * sizeof(__be32);
+       return (op_encode_hdr_size + 2 + op_encode_verifier_maxsz) * sizeof(__be32);
 }
 
 static inline u32 nfsd4_exchange_id_rsize(struct svc_rqst *rqstp, struct nfsd4_op *op)
@@ -1674,37 +1677,32 @@ static struct nfsd4_operation nfsd4_ops[] = {
        [OP_PUTFH] = {
                .op_func = (nfsd4op_func)nfsd4_putfh,
                .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
-                               | OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING
-                               | OP_CLEAR_STATEID,
+                               | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
                .op_name = "OP_PUTFH",
                .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
        },
        [OP_PUTPUBFH] = {
                .op_func = (nfsd4op_func)nfsd4_putrootfh,
                .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
-                               | OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING
-                               | OP_CLEAR_STATEID,
+                               | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
                .op_name = "OP_PUTPUBFH",
                .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
        },
        [OP_PUTROOTFH] = {
                .op_func = (nfsd4op_func)nfsd4_putrootfh,
                .op_flags = ALLOWED_WITHOUT_FH | ALLOWED_ON_ABSENT_FS
-                               | OP_IS_PUTFH_LIKE | OP_MODIFIES_SOMETHING
-                               | OP_CLEAR_STATEID,
+                               | OP_IS_PUTFH_LIKE | OP_CLEAR_STATEID,
                .op_name = "OP_PUTROOTFH",
                .op_rsize_bop = (nfsd4op_rsize)nfsd4_only_status_rsize,
        },
        [OP_READ] = {
                .op_func = (nfsd4op_func)nfsd4_read,
-               .op_flags = OP_MODIFIES_SOMETHING,
                .op_name = "OP_READ",
                .op_rsize_bop = (nfsd4op_rsize)nfsd4_read_rsize,
                .op_get_currentstateid = (stateid_getter)nfsd4_get_readstateid,
        },
        [OP_READDIR] = {
                .op_func = (nfsd4op_func)nfsd4_readdir,
-               .op_flags = OP_MODIFIES_SOMETHING,
                .op_name = "OP_READDIR",
                .op_rsize_bop = (nfsd4op_rsize)nfsd4_readdir_rsize,
        },
@@ -1862,14 +1860,21 @@ static struct nfsd4_operation nfsd4_ops[] = {
        },
 };
 
-#ifdef NFSD_DEBUG
+void warn_on_nonidempotent_op(struct nfsd4_op *op)
+{
+       if (OPDESC(op)->op_flags & OP_MODIFIES_SOMETHING) {
+               pr_err("unable to encode reply to nonidempotent op %d (%s)\n",
+                       op->opnum, nfsd4_op_name(op->opnum));
+               WARN_ON_ONCE(1);
+       }
+}
+
 static const char *nfsd4_op_name(unsigned opnum)
 {
        if (opnum < ARRAY_SIZE(nfsd4_ops))
                return nfsd4_ops[opnum].op_name;
        return "unknown_operation";
 }
-#endif
 
 #define nfsd4_voidres                  nfsd4_voidargs
 struct nfsd4_voidargs { int dummy; };