nfsd4: cl_count is unused
[safe/jmp/linux-2.6] / fs / nfs / callback_xdr.c
index 91f6f74..db30c0b 100644 (file)
                                2 + 2 + 3 + 3)
 #define CB_OP_RECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
 
+#if defined(CONFIG_NFS_V4_1)
+#define CB_OP_SEQUENCE_RES_MAXSZ       (CB_OP_HDR_RES_MAXSZ + \
+                                       4 + 1 + 3)
+#define CB_OP_RECALLANY_RES_MAXSZ      (CB_OP_HDR_RES_MAXSZ)
+#define CB_OP_RECALLSLOT_RES_MAXSZ     (CB_OP_HDR_RES_MAXSZ)
+#endif /* CONFIG_NFS_V4_1 */
+
 #define NFSDBG_FACILITY NFSDBG_CALLBACK
 
+/* Internal error code */
+#define NFS4ERR_RESOURCE_HDR   11050
+
 typedef __be32 (*callback_process_op_t)(void *, void *);
 typedef __be32 (*callback_decode_arg_t)(struct svc_rqst *, struct xdr_stream *, void *);
 typedef __be32 (*callback_encode_res_t)(struct svc_rqst *, struct xdr_stream *, void *);
@@ -147,14 +157,15 @@ static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound
        if (unlikely(p == NULL))
                return htonl(NFS4ERR_RESOURCE);
        hdr->minorversion = ntohl(*p++);
-       /* Check minor version is zero. */
-       if (hdr->minorversion != 0) {
+       /* Check minor version is zero or one. */
+       if (hdr->minorversion <= 1) {
+               p++;    /* skip callback_ident */
+       } else {
                printk(KERN_WARNING "%s: NFSv4 server callback with "
                        "illegal minor version %u!\n",
                        __func__, hdr->minorversion);
                return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
        }
-       hdr->callback_ident = ntohl(*p++);
        hdr->nops = ntohl(*p);
        dprintk("%s: minorversion %d nops %d\n", __func__,
                hdr->minorversion, hdr->nops);
@@ -166,7 +177,7 @@ static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op)
        __be32 *p;
        p = read_buf(xdr, 4);
        if (unlikely(p == NULL))
-               return htonl(NFS4ERR_RESOURCE);
+               return htonl(NFS4ERR_RESOURCE_HDR);
        *op = ntohl(*p);
        return 0;
 }
@@ -206,6 +217,155 @@ out:
        return status;
 }
 
+#if defined(CONFIG_NFS_V4_1)
+
+static __be32 decode_sessionid(struct xdr_stream *xdr,
+                                struct nfs4_sessionid *sid)
+{
+       __be32 *p;
+       int len = NFS4_MAX_SESSIONID_LEN;
+
+       p = read_buf(xdr, len);
+       if (unlikely(p == NULL))
+               return htonl(NFS4ERR_RESOURCE);
+
+       memcpy(sid->data, p, len);
+       return 0;
+}
+
+static __be32 decode_rc_list(struct xdr_stream *xdr,
+                              struct referring_call_list *rc_list)
+{
+       __be32 *p;
+       int i;
+       __be32 status;
+
+       status = decode_sessionid(xdr, &rc_list->rcl_sessionid);
+       if (status)
+               goto out;
+
+       status = htonl(NFS4ERR_RESOURCE);
+       p = read_buf(xdr, sizeof(uint32_t));
+       if (unlikely(p == NULL))
+               goto out;
+
+       rc_list->rcl_nrefcalls = ntohl(*p++);
+       if (rc_list->rcl_nrefcalls) {
+               p = read_buf(xdr,
+                            rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t));
+               if (unlikely(p == NULL))
+                       goto out;
+               rc_list->rcl_refcalls = kmalloc(rc_list->rcl_nrefcalls *
+                                               sizeof(*rc_list->rcl_refcalls),
+                                               GFP_KERNEL);
+               if (unlikely(rc_list->rcl_refcalls == NULL))
+                       goto out;
+               for (i = 0; i < rc_list->rcl_nrefcalls; i++) {
+                       rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++);
+                       rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++);
+               }
+       }
+       status = 0;
+
+out:
+       return status;
+}
+
+static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp,
+                                       struct xdr_stream *xdr,
+                                       struct cb_sequenceargs *args)
+{
+       __be32 *p;
+       int i;
+       __be32 status;
+
+       status = decode_sessionid(xdr, &args->csa_sessionid);
+       if (status)
+               goto out;
+
+       status = htonl(NFS4ERR_RESOURCE);
+       p = read_buf(xdr, 5 * sizeof(uint32_t));
+       if (unlikely(p == NULL))
+               goto out;
+
+       args->csa_addr = svc_addr(rqstp);
+       args->csa_sequenceid = ntohl(*p++);
+       args->csa_slotid = ntohl(*p++);
+       args->csa_highestslotid = ntohl(*p++);
+       args->csa_cachethis = ntohl(*p++);
+       args->csa_nrclists = ntohl(*p++);
+       args->csa_rclists = NULL;
+       if (args->csa_nrclists) {
+               args->csa_rclists = kmalloc(args->csa_nrclists *
+                                           sizeof(*args->csa_rclists),
+                                           GFP_KERNEL);
+               if (unlikely(args->csa_rclists == NULL))
+                       goto out;
+
+               for (i = 0; i < args->csa_nrclists; i++) {
+                       status = decode_rc_list(xdr, &args->csa_rclists[i]);
+                       if (status)
+                               goto out_free;
+               }
+       }
+       status = 0;
+
+       dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u slotid %u "
+               "highestslotid %u cachethis %d nrclists %u\n",
+               __func__,
+               ((u32 *)&args->csa_sessionid)[0],
+               ((u32 *)&args->csa_sessionid)[1],
+               ((u32 *)&args->csa_sessionid)[2],
+               ((u32 *)&args->csa_sessionid)[3],
+               args->csa_sequenceid, args->csa_slotid,
+               args->csa_highestslotid, args->csa_cachethis,
+               args->csa_nrclists);
+out:
+       dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
+       return status;
+
+out_free:
+       for (i = 0; i < args->csa_nrclists; i++)
+               kfree(args->csa_rclists[i].rcl_refcalls);
+       kfree(args->csa_rclists);
+       goto out;
+}
+
+static __be32 decode_recallany_args(struct svc_rqst *rqstp,
+                                     struct xdr_stream *xdr,
+                                     struct cb_recallanyargs *args)
+{
+       __be32 *p;
+
+       args->craa_addr = svc_addr(rqstp);
+       p = read_buf(xdr, 4);
+       if (unlikely(p == NULL))
+               return htonl(NFS4ERR_BADXDR);
+       args->craa_objs_to_keep = ntohl(*p++);
+       p = read_buf(xdr, 4);
+       if (unlikely(p == NULL))
+               return htonl(NFS4ERR_BADXDR);
+       args->craa_type_mask = ntohl(*p);
+
+       return 0;
+}
+
+static __be32 decode_recallslot_args(struct svc_rqst *rqstp,
+                                       struct xdr_stream *xdr,
+                                       struct cb_recallslotargs *args)
+{
+       __be32 *p;
+
+       args->crsa_addr = svc_addr(rqstp);
+       p = read_buf(xdr, 4);
+       if (unlikely(p == NULL))
+               return htonl(NFS4ERR_BADXDR);
+       args->crsa_target_max_slots = ntohl(*p++);
+       return 0;
+}
+
+#endif /* CONFIG_NFS_V4_1 */
+
 static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
 {
        __be32 *p;
@@ -323,7 +483,7 @@ static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
        
        p = xdr_reserve_space(xdr, 8);
        if (unlikely(p == NULL))
-               return htonl(NFS4ERR_RESOURCE);
+               return htonl(NFS4ERR_RESOURCE_HDR);
        *p++ = htonl(op);
        *p = res;
        return 0;
@@ -355,43 +515,157 @@ out:
        return status;
 }
 
-static __be32 process_op(struct svc_rqst *rqstp,
+#if defined(CONFIG_NFS_V4_1)
+
+static __be32 encode_sessionid(struct xdr_stream *xdr,
+                                const struct nfs4_sessionid *sid)
+{
+       __be32 *p;
+       int len = NFS4_MAX_SESSIONID_LEN;
+
+       p = xdr_reserve_space(xdr, len);
+       if (unlikely(p == NULL))
+               return htonl(NFS4ERR_RESOURCE);
+
+       memcpy(p, sid, len);
+       return 0;
+}
+
+static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp,
+                                      struct xdr_stream *xdr,
+                                      const struct cb_sequenceres *res)
+{
+       __be32 *p;
+       unsigned status = res->csr_status;
+
+       if (unlikely(status != 0))
+               goto out;
+
+       encode_sessionid(xdr, &res->csr_sessionid);
+
+       p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
+       if (unlikely(p == NULL))
+               return htonl(NFS4ERR_RESOURCE);
+
+       *p++ = htonl(res->csr_sequenceid);
+       *p++ = htonl(res->csr_slotid);
+       *p++ = htonl(res->csr_highestslotid);
+       *p++ = htonl(res->csr_target_highestslotid);
+out:
+       dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
+       return status;
+}
+
+static __be32
+preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
+{
+       if (op_nr == OP_CB_SEQUENCE) {
+               if (nop != 0)
+                       return htonl(NFS4ERR_SEQUENCE_POS);
+       } else {
+               if (nop == 0)
+                       return htonl(NFS4ERR_OP_NOT_IN_SESSION);
+       }
+
+       switch (op_nr) {
+       case OP_CB_GETATTR:
+       case OP_CB_RECALL:
+       case OP_CB_SEQUENCE:
+       case OP_CB_RECALL_ANY:
+       case OP_CB_RECALL_SLOT:
+               *op = &callback_ops[op_nr];
+               break;
+
+       case OP_CB_LAYOUTRECALL:
+       case OP_CB_NOTIFY_DEVICEID:
+       case OP_CB_NOTIFY:
+       case OP_CB_PUSH_DELEG:
+       case OP_CB_RECALLABLE_OBJ_AVAIL:
+       case OP_CB_WANTS_CANCELLED:
+       case OP_CB_NOTIFY_LOCK:
+               return htonl(NFS4ERR_NOTSUPP);
+
+       default:
+               return htonl(NFS4ERR_OP_ILLEGAL);
+       }
+
+       return htonl(NFS_OK);
+}
+
+#else /* CONFIG_NFS_V4_1 */
+
+static __be32
+preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
+{
+       return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
+}
+
+#endif /* CONFIG_NFS_V4_1 */
+
+static __be32
+preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
+{
+       switch (op_nr) {
+       case OP_CB_GETATTR:
+       case OP_CB_RECALL:
+               *op = &callback_ops[op_nr];
+               break;
+       default:
+               return htonl(NFS4ERR_OP_ILLEGAL);
+       }
+
+       return htonl(NFS_OK);
+}
+
+static __be32 process_op(uint32_t minorversion, int nop,
+               struct svc_rqst *rqstp,
                struct xdr_stream *xdr_in, void *argp,
-               struct xdr_stream *xdr_out, void *resp)
+               struct xdr_stream *xdr_out, void *resp, int* drc_status)
 {
        struct callback_op *op = &callback_ops[0];
-       unsigned int op_nr = OP_CB_ILLEGAL;
-       __be32 status = 0;
+       unsigned int op_nr;
+       __be32 status;
        long maxlen;
        __be32 res;
 
        dprintk("%s: start\n", __func__);
        status = decode_op_hdr(xdr_in, &op_nr);
-       if (likely(status == 0)) {
-               switch (op_nr) {
-                       case OP_CB_GETATTR:
-                       case OP_CB_RECALL:
-                               op = &callback_ops[op_nr];
-                               break;
-                       default:
-                               op_nr = OP_CB_ILLEGAL;
-                               op = &callback_ops[0];
-                               status = htonl(NFS4ERR_OP_ILLEGAL);
-               }
+       if (unlikely(status))
+               return status;
+
+       dprintk("%s: minorversion=%d nop=%d op_nr=%u\n",
+               __func__, minorversion, nop, op_nr);
+
+       status = minorversion ? preprocess_nfs41_op(nop, op_nr, &op) :
+                               preprocess_nfs4_op(op_nr, &op);
+       if (status == htonl(NFS4ERR_OP_ILLEGAL))
+               op_nr = OP_CB_ILLEGAL;
+       if (status)
+               goto encode_hdr;
+
+       if (*drc_status) {
+               status = *drc_status;
+               goto encode_hdr;
        }
 
        maxlen = xdr_out->end - xdr_out->p;
        if (maxlen > 0 && maxlen < PAGE_SIZE) {
-               if (likely(status == 0 && op->decode_args != NULL))
-                       status = op->decode_args(rqstp, xdr_in, argp);
-               if (likely(status == 0 && op->process_op != NULL))
+               status = op->decode_args(rqstp, xdr_in, argp);
+               if (likely(status == 0))
                        status = op->process_op(argp, resp);
        } else
                status = htonl(NFS4ERR_RESOURCE);
 
+       /* Only set by OP_CB_SEQUENCE processing */
+       if (status == htonl(NFS4ERR_RETRY_UNCACHED_REP)) {
+               *drc_status = status;
+               status = 0;
+       }
+
+encode_hdr:
        res = encode_op_hdr(xdr_out, op_nr, status);
-       if (status == 0)
-               status = res;
+       if (unlikely(res))
+               return res;
        if (op->encode_res != NULL && status == 0)
                status = op->encode_res(rqstp, xdr_out, resp);
        dprintk("%s: done, status = %d\n", __func__, ntohl(status));
@@ -407,7 +681,7 @@ static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *r
        struct cb_compound_hdr_res hdr_res = { NULL };
        struct xdr_stream xdr_in, xdr_out;
        __be32 *p;
-       __be32 status;
+       __be32 status, drc_status = 0;
        unsigned int nops = 0;
 
        dprintk("%s: start\n", __func__);
@@ -427,10 +701,18 @@ static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *r
                return rpc_system_err;
 
        while (status == 0 && nops != hdr_arg.nops) {
-               status = process_op(rqstp, &xdr_in, argp, &xdr_out, resp);
+               status = process_op(hdr_arg.minorversion, nops, rqstp,
+                                   &xdr_in, argp, &xdr_out, resp, &drc_status);
                nops++;
        }
 
+       /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
+       * resource error in cb_compound status without returning op */
+       if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
+               status = htonl(NFS4ERR_RESOURCE);
+               nops--;
+       }
+
        *hdr_res.status = status;
        *hdr_res.nops = htonl(nops);
        dprintk("%s: done, status = %u\n", __func__, ntohl(status));
@@ -454,7 +736,25 @@ static struct callback_op callback_ops[] = {
                .process_op = (callback_process_op_t)nfs4_callback_recall,
                .decode_args = (callback_decode_arg_t)decode_recall_args,
                .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
-       }
+       },
+#if defined(CONFIG_NFS_V4_1)
+       [OP_CB_SEQUENCE] = {
+               .process_op = (callback_process_op_t)nfs4_callback_sequence,
+               .decode_args = (callback_decode_arg_t)decode_cb_sequence_args,
+               .encode_res = (callback_encode_res_t)encode_cb_sequence_res,
+               .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
+       },
+       [OP_CB_RECALL_ANY] = {
+               .process_op = (callback_process_op_t)nfs4_callback_recallany,
+               .decode_args = (callback_decode_arg_t)decode_recallany_args,
+               .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
+       },
+       [OP_CB_RECALL_SLOT] = {
+               .process_op = (callback_process_op_t)nfs4_callback_recallslot,
+               .decode_args = (callback_decode_arg_t)decode_recallslot_args,
+               .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
+       },
+#endif /* CONFIG_NFS_V4_1 */
 };
 
 /*
@@ -484,3 +784,10 @@ struct svc_version nfs4_callback_version1 = {
        .vs_dispatch = NULL,
 };
 
+struct svc_version nfs4_callback_version4 = {
+       .vs_vers = 4,
+       .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
+       .vs_proc = nfs4_callback_procedures1,
+       .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
+       .vs_dispatch = NULL,
+};