hammer2 - Fix nact assertion
[dragonfly.git] / sbin / hammer2 / msg.c
index 4fc4439..a47d811 100644 (file)
 
 #include "hammer2.h"
 
+static int hammer2_state_msgrx(hammer2_iocom_t *iocom, hammer2_msg_t *msg);
+static void hammer2_state_cleanuptx(hammer2_iocom_t *iocom, hammer2_msg_t *msg);
+
+/*
+ * Indexed messages are stored in a red-black tree indexed by their
+ * msgid.  Only persistent messages are indexed.
+ */
+int
+hammer2_state_cmp(hammer2_state_t *state1, hammer2_state_t *state2)
+{
+       if (state1->spanid < state2->spanid)
+               return(-1);
+       if (state1->spanid > state2->spanid)
+               return(1);
+       if (state1->msgid < state2->msgid)
+               return(-1);
+       if (state1->msgid > state2->msgid)
+               return(1);
+       return(0);
+}
+
+RB_GENERATE(hammer2_state_tree, hammer2_state, rbnode, hammer2_state_cmp);
+
 /*
  * Initialize a low-level ioq
  */
@@ -46,36 +69,64 @@ hammer2_ioq_init(hammer2_iocom_t *iocom __unused, hammer2_ioq_t *ioq)
        TAILQ_INIT(&ioq->msgq);
 }
 
+/*
+ * Cleanup queue.
+ *
+ * caller holds iocom->mtx.
+ */
 void
 hammer2_ioq_done(hammer2_iocom_t *iocom __unused, hammer2_ioq_t *ioq)
 {
        hammer2_msg_t *msg;
 
        while ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
-               TAILQ_REMOVE(&ioq->msgq, msg, entry);
-               hammer2_freemsg(msg);
+               assert(0);      /* shouldn't happen */
+               TAILQ_REMOVE(&ioq->msgq, msg, qentry);
+               hammer2_msg_free(msg);
        }
        if ((msg = ioq->msg) != NULL) {
                ioq->msg = NULL;
-               hammer2_freemsg(msg);
+               hammer2_msg_free(msg);
        }
 }
 
 /*
- * Initialize a low-level communications channel
+ * Initialize a low-level communications channel.
+ *
+ * NOTE: The signal_func() is called at least once from the loop and can be
+ *      re-armed via hammer2_iocom_restate().
  */
 void
-hammer2_iocom_init(hammer2_iocom_t *iocom, int sock_fd, int alt_fd)
+hammer2_iocom_init(hammer2_iocom_t *iocom, int sock_fd, int alt_fd,
+                  void (*signal_func)(hammer2_router_t *),
+                  void (*rcvmsg_func)(hammer2_msg_t *),
+                  void (*altmsg_func)(hammer2_iocom_t *))
 {
        bzero(iocom, sizeof(*iocom));
 
+       iocom->router.signal_callback = signal_func;
+       iocom->router.rcvmsg_callback = rcvmsg_func;
+       iocom->router.altmsg_callback = altmsg_func;
+
+       pthread_mutex_init(&iocom->mtx, NULL);
+       RB_INIT(&iocom->router.staterd_tree);
+       RB_INIT(&iocom->router.statewr_tree);
        TAILQ_INIT(&iocom->freeq);
        TAILQ_INIT(&iocom->freeq_aux);
+       TAILQ_INIT(&iocom->addrq);
+       TAILQ_INIT(&iocom->router.txmsgq);
+       iocom->router.iocom = iocom;
        iocom->sock_fd = sock_fd;
        iocom->alt_fd = alt_fd;
-       iocom->flags = HAMMER2_IOCOMF_RREQ | HAMMER2_IOCOMF_WIDLE;
+       iocom->flags = HAMMER2_IOCOMF_RREQ;
+       if (signal_func)
+               iocom->flags |= HAMMER2_IOCOMF_SWORK;
        hammer2_ioq_init(iocom, &iocom->ioq_rx);
        hammer2_ioq_init(iocom, &iocom->ioq_tx);
+       if (pipe(iocom->wakeupfds) < 0)
+               assert(0);
+       fcntl(iocom->wakeupfds[0], F_SETFL, O_NONBLOCK);
+       fcntl(iocom->wakeupfds[1], F_SETFL, O_NONBLOCK);
 
        /*
         * Negotiate session crypto synchronously.  This will mark the
@@ -95,47 +146,126 @@ hammer2_iocom_init(hammer2_iocom_t *iocom, int sock_fd, int alt_fd)
 #endif
 }
 
+/*
+ * May only be called from a callback from iocom_core.
+ *
+ * Adjust state machine functions, set flags to guarantee that both
+ * the recevmsg_func and the sendmsg_func is called at least once.
+ */
+void
+hammer2_router_restate(hammer2_router_t *router,
+                  void (*signal_func)(hammer2_router_t *),
+                  void (*rcvmsg_func)(hammer2_msg_t *msg),
+                  void (*altmsg_func)(hammer2_iocom_t *))
+{
+       router->signal_callback = signal_func;
+       router->rcvmsg_callback = rcvmsg_func;
+       router->altmsg_callback = altmsg_func;
+       if (signal_func)
+               router->iocom->flags |= HAMMER2_IOCOMF_SWORK;
+       else
+               router->iocom->flags &= ~HAMMER2_IOCOMF_SWORK;
+}
+
+void
+hammer2_router_signal(hammer2_router_t *router)
+{
+       if (router->signal_callback)
+               router->iocom->flags |= HAMMER2_IOCOMF_SWORK;
+}
+
+/*
+ * Cleanup a terminating iocom.
+ *
+ * Caller should not hold iocom->mtx.  The iocom has already been disconnected
+ * from all possible references to it.
+ */
 void
 hammer2_iocom_done(hammer2_iocom_t *iocom)
 {
        hammer2_msg_t *msg;
 
-       iocom->sock_fd = -1;
+       if (iocom->sock_fd >= 0) {
+               close(iocom->sock_fd);
+               iocom->sock_fd = -1;
+       }
+       if (iocom->alt_fd >= 0) {
+               close(iocom->alt_fd);
+               iocom->alt_fd = -1;
+       }
        hammer2_ioq_done(iocom, &iocom->ioq_rx);
        hammer2_ioq_done(iocom, &iocom->ioq_tx);
        if ((msg = TAILQ_FIRST(&iocom->freeq)) != NULL) {
-               TAILQ_REMOVE(&iocom->freeq, msg, entry);
+               TAILQ_REMOVE(&iocom->freeq, msg, qentry);
                free(msg);
        }
        if ((msg = TAILQ_FIRST(&iocom->freeq_aux)) != NULL) {
-               TAILQ_REMOVE(&iocom->freeq_aux, msg, entry);
+               TAILQ_REMOVE(&iocom->freeq_aux, msg, qentry);
                free(msg->aux_data);
                msg->aux_data = NULL;
                free(msg);
        }
+       if (iocom->wakeupfds[0] >= 0) {
+               close(iocom->wakeupfds[0]);
+               iocom->wakeupfds[0] = -1;
+       }
+       if (iocom->wakeupfds[1] >= 0) {
+               close(iocom->wakeupfds[1]);
+               iocom->wakeupfds[1] = -1;
+       }
+       pthread_mutex_destroy(&iocom->mtx);
 }
 
 /*
  * Allocate a new one-way message.
  */
 hammer2_msg_t *
-hammer2_allocmsg(hammer2_iocom_t *iocom, uint32_t cmd, int aux_size)
+hammer2_msg_alloc(hammer2_router_t *router, size_t aux_size, uint32_t cmd,
+                 void (*func)(hammer2_msg_t *), void *data)
 {
+       hammer2_state_t *state = NULL;
+       hammer2_iocom_t *iocom = router->iocom;
        hammer2_msg_t *msg;
        int hbytes;
 
+       pthread_mutex_lock(&iocom->mtx);
        if (aux_size) {
                aux_size = (aux_size + HAMMER2_MSG_ALIGNMASK) &
                           ~HAMMER2_MSG_ALIGNMASK;
                if ((msg = TAILQ_FIRST(&iocom->freeq_aux)) != NULL)
-                       TAILQ_REMOVE(&iocom->freeq_aux, msg, entry);
+                       TAILQ_REMOVE(&iocom->freeq_aux, msg, qentry);
        } else {
                if ((msg = TAILQ_FIRST(&iocom->freeq)) != NULL)
-                       TAILQ_REMOVE(&iocom->freeq, msg, entry);
+                       TAILQ_REMOVE(&iocom->freeq, msg, qentry);
+       }
+       if ((cmd & (HAMMER2_MSGF_CREATE | HAMMER2_MSGF_REPLY)) ==
+           HAMMER2_MSGF_CREATE) {
+               /*
+                * Create state when CREATE is set without REPLY.
+                *
+                * NOTE: CREATE in txcmd handled by hammer2_msg_write()
+                * NOTE: DELETE in txcmd handled by hammer2_state_cleanuptx()
+                */
+               state = malloc(sizeof(*state));
+               bzero(state, sizeof(*state));
+               state->iocom = iocom;
+               state->flags = HAMMER2_STATE_DYNAMIC;
+               state->msgid = (uint64_t)(uintptr_t)state;
+               /* XXX set state->spanid from router */
+               state->txcmd = cmd & ~(HAMMER2_MSGF_CREATE |
+                                      HAMMER2_MSGF_DELETE);
+               state->rxcmd = HAMMER2_MSGF_REPLY;
+               state->func = func;
+               state->any.any = data;
+               pthread_mutex_lock(&iocom->mtx);
+               RB_INSERT(hammer2_state_tree, &iocom->router.statewr_tree, state);
+               pthread_mutex_unlock(&iocom->mtx);
+               state->flags |= HAMMER2_STATE_INSERTED;
        }
+       pthread_mutex_unlock(&iocom->mtx);
        if (msg == NULL) {
                msg = malloc(sizeof(*msg));
-               msg->iocom = iocom;
+               bzero(msg, sizeof(*msg));
                msg->aux_data = NULL;
                msg->aux_size = 0;
        }
@@ -150,120 +280,223 @@ hammer2_allocmsg(hammer2_iocom_t *iocom, uint32_t cmd, int aux_size)
                        msg->aux_size = aux_size;
                }
        }
-       msg->flags = 0;
        hbytes = (cmd & HAMMER2_MSGF_SIZE) * HAMMER2_MSG_ALIGN;
        if (hbytes)
                bzero(&msg->any.head, hbytes);
-       msg->any.head.aux_icrc = 0;
+       msg->hdr_size = hbytes;
        msg->any.head.cmd = cmd;
-
+       msg->any.head.aux_descr = 0;
+       msg->any.head.aux_crc = 0;
+       msg->router = router;
+       if (state) {
+               msg->state = state;
+               state->msg = msg;
+               msg->any.head.msgid = state->msgid;
+       }
        return (msg);
 }
 
-/*
- * Allocate a one-way or streaming reply to a message.  The message is
- * not modified.  This function may be used to allocate multiple replies.
- *
- * If cmd is 0 then msg->any.head.cmd is used to formulate the reply command.
- */
-hammer2_msg_t *
-hammer2_allocreply(hammer2_msg_t *msg, uint32_t cmd, int aux_size)
-{
-       hammer2_msg_t *rmsg;
-       hammer2_persist_t *pers;
-
-       assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
-       if (cmd == 0)
-               cmd = msg->any.head.cmd;
-
-       rmsg = hammer2_allocmsg(msg->iocom, cmd, aux_size);
-       rmsg->any.head = msg->any.head;
-       rmsg->any.head.source = msg->any.head.target;
-       rmsg->any.head.target = msg->any.head.source;
-       rmsg->any.head.cmd = (cmd | HAMMER2_MSGF_REPLY) &
-                            ~(HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE);
-       rmsg->any.head.aux_icrc = 0;
-
-       if ((pers = msg->persist) != NULL) {
-               assert(pers->lrep & HAMMER2_MSGF_DELETE);
-               rmsg->any.head.cmd |= pers->lrep & HAMMER2_MSGF_CREATE;
-               pers->lrep &= ~HAMMER2_MSGF_CREATE;
-               /* do not clear DELETE */
-       }
-       return (rmsg);
-}
-
 /*
  * Free a message so it can be reused afresh.
  *
  * NOTE: aux_size can be 0 with a non-NULL aux_data.
  */
+static
 void
-hammer2_freemsg(hammer2_msg_t *msg)
+hammer2_msg_free_locked(hammer2_msg_t *msg)
 {
-       hammer2_iocom_t *iocom = msg->iocom;
+       hammer2_iocom_t *iocom = msg->router->iocom;
 
+       msg->state = NULL;
        if (msg->aux_data)
-               TAILQ_INSERT_TAIL(&iocom->freeq_aux, msg, entry);
+               TAILQ_INSERT_TAIL(&iocom->freeq_aux, msg, qentry);
        else
-               TAILQ_INSERT_TAIL(&iocom->freeq, msg, entry);
+               TAILQ_INSERT_TAIL(&iocom->freeq, msg, qentry);
+}
+
+void
+hammer2_msg_free(hammer2_msg_t *msg)
+{
+       hammer2_iocom_t *iocom = msg->router->iocom;
+
+       pthread_mutex_lock(&iocom->mtx);
+       hammer2_msg_free_locked(msg);
+       pthread_mutex_unlock(&iocom->mtx);
 }
 
 /*
  * I/O core loop for an iocom.
+ *
+ * Thread localized, iocom->mtx not held.
  */
 void
-hammer2_iocom_core(hammer2_iocom_t *iocom,
-                  void (*recvmsg_func)(hammer2_iocom_t *),
-                  void (*sendmsg_func)(hammer2_iocom_t *),
-                  void (*altmsg_func)(hammer2_iocom_t *))
+hammer2_iocom_core(hammer2_iocom_t *iocom)
 {
-       struct pollfd fds[2];
+       struct pollfd fds[3];
+       char dummybuf[256];
+       hammer2_msg_t *msg;
        int timeout;
-
-       iocom->recvmsg_callback = recvmsg_func;
-       iocom->sendmsg_callback = sendmsg_func;
-       iocom->altmsg_callback = altmsg_func;
+       int count;
+       int wi; /* wakeup pipe */
+       int si; /* socket */
+       int ai; /* alt bulk path socket */
 
        while ((iocom->flags & HAMMER2_IOCOMF_EOF) == 0) {
-               timeout = 5000;
-
-               fds[0].fd = iocom->sock_fd;
-               fds[0].events = 0;
-               fds[0].revents = 0;
+               if ((iocom->flags & (HAMMER2_IOCOMF_RWORK |
+                                    HAMMER2_IOCOMF_WWORK |
+                                    HAMMER2_IOCOMF_PWORK |
+                                    HAMMER2_IOCOMF_SWORK |
+                                    HAMMER2_IOCOMF_ARWORK |
+                                    HAMMER2_IOCOMF_AWWORK)) == 0) {
+                       /*
+                        * Only poll if no immediate work is pending.
+                        * Otherwise we are just wasting our time calling
+                        * poll.
+                        */
+                       timeout = 5000;
+
+                       count = 0;
+                       wi = -1;
+                       si = -1;
+                       ai = -1;
+
+                       /*
+                        * Always check the inter-thread pipe, e.g.
+                        * for iocom->txmsgq work.
+                        */
+                       wi = count++;
+                       fds[wi].fd = iocom->wakeupfds[0];
+                       fds[wi].events = POLLIN;
+                       fds[wi].revents = 0;
+
+                       /*
+                        * Check the socket input/output direction as
+                        * requested
+                        */
+                       if (iocom->flags & (HAMMER2_IOCOMF_RREQ |
+                                           HAMMER2_IOCOMF_WREQ)) {
+                               si = count++;
+                               fds[si].fd = iocom->sock_fd;
+                               fds[si].events = 0;
+                               fds[si].revents = 0;
+
+                               if (iocom->flags & HAMMER2_IOCOMF_RREQ)
+                                       fds[si].events |= POLLIN;
+                               if (iocom->flags & HAMMER2_IOCOMF_WREQ)
+                                       fds[si].events |= POLLOUT;
+                       }
 
-               if (iocom->flags & HAMMER2_IOCOMF_RREQ)
-                       fds[0].events |= POLLIN;
-               else
-                       timeout = 0;
-               if ((iocom->flags & HAMMER2_IOCOMF_WIDLE) == 0) {
-                       if (iocom->flags & HAMMER2_IOCOMF_WREQ)
-                               fds[0].events |= POLLOUT;
-                       else
-                               timeout = 0;
-               }
-
-               if (iocom->alt_fd >= 0) {
-                       fds[1].fd = iocom->alt_fd;
-                       fds[1].events |= POLLIN;
-                       fds[1].revents = 0;
-                       poll(fds, 2, timeout);
+                       /*
+                        * Check the alternative fd for work.
+                        */
+                       if (iocom->alt_fd >= 0) {
+                               ai = count++;
+                               fds[ai].fd = iocom->alt_fd;
+                               fds[ai].events = POLLIN;
+                               fds[ai].revents = 0;
+                       }
+                       poll(fds, count, timeout);
+
+                       if (wi >= 0 && (fds[wi].revents & POLLIN))
+                               iocom->flags |= HAMMER2_IOCOMF_PWORK;
+                       if (si >= 0 && (fds[si].revents & POLLIN))
+                               iocom->flags |= HAMMER2_IOCOMF_RWORK;
+                       if (si >= 0 && (fds[si].revents & POLLOUT))
+                               iocom->flags |= HAMMER2_IOCOMF_WWORK;
+                       if (wi >= 0 && (fds[wi].revents & POLLOUT))
+                               iocom->flags |= HAMMER2_IOCOMF_WWORK;
+                       if (ai >= 0 && (fds[ai].revents & POLLIN))
+                               iocom->flags |= HAMMER2_IOCOMF_ARWORK;
                } else {
-                       poll(fds, 1, timeout);
+                       /*
+                        * Always check the pipe
+                        */
+                       iocom->flags |= HAMMER2_IOCOMF_PWORK;
                }
-               if ((fds[0].revents & POLLIN) ||
-                   (iocom->flags & HAMMER2_IOCOMF_RREQ) == 0) {
-                       iocom->recvmsg_callback(iocom);
+
+               if (iocom->flags & HAMMER2_IOCOMF_SWORK) {
+                       iocom->flags &= ~HAMMER2_IOCOMF_SWORK;
+                       iocom->router.signal_callback(&iocom->router);
+               }
+
+               /*
+                * Pending message queues from other threads wake us up
+                * with a write to the wakeupfds[] pipe.  We have to clear
+                * the pipe with a dummy read.
+                */
+               if (iocom->flags & HAMMER2_IOCOMF_PWORK) {
+                       iocom->flags &= ~HAMMER2_IOCOMF_PWORK;
+                       read(iocom->wakeupfds[0], dummybuf, sizeof(dummybuf));
+                       iocom->flags |= HAMMER2_IOCOMF_RWORK;
+                       iocom->flags |= HAMMER2_IOCOMF_WWORK;
+                       if (TAILQ_FIRST(&iocom->router.txmsgq))
+                               hammer2_iocom_flush1(iocom);
                }
-               if ((iocom->flags & HAMMER2_IOCOMF_WIDLE) == 0) {
-                       if ((fds[0].revents & POLLOUT) ||
-                           (iocom->flags & HAMMER2_IOCOMF_WREQ) == 0) {
-                               iocom->sendmsg_callback(iocom);
+
+               /*
+                * Message write sequencing
+                */
+               if (iocom->flags & HAMMER2_IOCOMF_WWORK)
+                       hammer2_iocom_flush1(iocom);
+
+               /*
+                * Message read sequencing.  Run this after the write
+                * sequencing in case the write sequencing allowed another
+                * auto-DELETE to occur on the read side.
+                */
+               if (iocom->flags & HAMMER2_IOCOMF_RWORK) {
+                       while ((iocom->flags & HAMMER2_IOCOMF_EOF) == 0 &&
+                              (msg = hammer2_ioq_read(iocom)) != NULL) {
+                               if (DebugOpt) {
+                                       fprintf(stderr, "receive %s\n",
+                                               hammer2_msg_str(msg));
+                               }
+                               iocom->router.rcvmsg_callback(msg);
+                               hammer2_state_cleanuprx(iocom, msg);
                        }
                }
-               if (iocom->alt_fd >= 0 && (fds[1].revents & POLLIN))
-                       iocom->altmsg_callback(iocom);
+
+               if (iocom->flags & HAMMER2_IOCOMF_ARWORK) {
+                       iocom->flags &= ~HAMMER2_IOCOMF_ARWORK;
+                       iocom->router.altmsg_callback(iocom);
+               }
+       }
+}
+
+/*
+ * Make sure there's enough room in the FIFO to hold the
+ * needed data.
+ *
+ * Assume worst case encrypted form is 2x the size of the
+ * plaintext equivalent.
+ */
+static
+size_t
+hammer2_ioq_makeroom(hammer2_ioq_t *ioq, size_t needed)
+{
+       size_t bytes;
+       size_t nmax;
+
+       bytes = ioq->fifo_cdx - ioq->fifo_beg;
+       nmax = sizeof(ioq->buf) - ioq->fifo_end;
+       if (bytes + nmax / 2 < needed) {
+               if (bytes) {
+                       bcopy(ioq->buf + ioq->fifo_beg,
+                             ioq->buf,
+                             bytes);
+               }
+               ioq->fifo_cdx -= ioq->fifo_beg;
+               ioq->fifo_beg = 0;
+               if (ioq->fifo_cdn < ioq->fifo_end) {
+                       bcopy(ioq->buf + ioq->fifo_cdn,
+                             ioq->buf + ioq->fifo_cdx,
+                             ioq->fifo_end - ioq->fifo_cdn);
+               }
+               ioq->fifo_end -= ioq->fifo_cdn - ioq->fifo_cdx;
+               ioq->fifo_cdn = ioq->fifo_cdx;
+               nmax = sizeof(ioq->buf) - ioq->fifo_end;
        }
+       return(nmax);
 }
 
 /*
@@ -271,7 +504,12 @@ hammer2_iocom_core(hammer2_iocom_t *iocom,
  * Caller should retry on a read-event when NULL is returned.
  *
  * If an error occurs during reception a HAMMER2_LNK_ERROR msg will
- * be returned (and the caller must not call us again after that).
+ * be returned for each open transaction, then the ioq and iocom
+ * will be errored out and a non-transactional HAMMER2_LNK_ERROR
+ * msg will be returned as the final message.  The caller should not call
+ * us again after the final message is returned.
+ *
+ * Thread localized, iocom->mtx not held.
  */
 hammer2_msg_t *
 hammer2_ioq_read(hammer2_iocom_t *iocom)
@@ -279,28 +517,34 @@ hammer2_ioq_read(hammer2_iocom_t *iocom)
        hammer2_ioq_t *ioq = &iocom->ioq_rx;
        hammer2_msg_t *msg;
        hammer2_msg_hdr_t *head;
+       hammer2_state_t *state;
        ssize_t n;
-       int bytes;
-       int flags;
-       int nmax;
-       uint16_t xcrc16;
+       size_t bytes;
+       size_t nmax;
        uint32_t xcrc32;
+       int error;
+
+again:
+       iocom->flags &= ~(HAMMER2_IOCOMF_RREQ | HAMMER2_IOCOMF_RWORK);
 
        /*
         * If a message is already pending we can just remove and
-        * return it.
+        * return it.  Message state has already been processed.
         */
        if ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
-               TAILQ_REMOVE(&ioq->msgq, msg, entry);
-               return(msg);
+               TAILQ_REMOVE(&ioq->msgq, msg, qentry);
+               return (msg);
        }
 
+       if (ioq->error)
+               goto skip;
+
        /*
         * Message read in-progress (msg is NULL at the moment).  We don't
         * allocate a msg until we have its core header.
         */
-       bytes = ioq->fifo_end - ioq->fifo_beg;
        nmax = sizeof(ioq->buf) - ioq->fifo_end;
+       bytes = ioq->fifo_cdx - ioq->fifo_beg;          /* already decrypted */
        msg = ioq->msg;
 
        switch(ioq->state) {
@@ -311,7 +555,8 @@ hammer2_ioq_read(hammer2_iocom_t *iocom)
                 * size is the message alignment it will never straddle
                 * the end of the buffer.
                 */
-               if (bytes < (int)sizeof(msg->any.head)) {
+               nmax = hammer2_ioq_makeroom(ioq, sizeof(msg->any.head));
+               if (bytes < sizeof(msg->any.head)) {
                        n = read(iocom->sock_fd,
                                 ioq->buf + ioq->fifo_end,
                                 nmax);
@@ -329,99 +574,84 @@ hammer2_ioq_read(hammer2_iocom_t *iocom)
                                n = 0;
                                /* fall through */
                        }
-                       ioq->fifo_end += n;
-                       bytes += n;
-                       nmax -= n;
+                       ioq->fifo_end += (size_t)n;
+                       nmax -= (size_t)n;
+               }
+
+               /*
+                * Decrypt data received so far.  Data will be decrypted
+                * in-place but might create gaps in the FIFO.  Partial
+                * blocks are not immediately decrypted.
+                *
+                * WARNING!  The header might be in the wrong endian, we
+                *           do not fix it up until we get the entire
+                *           extended header.
+                */
+               if (iocom->flags & HAMMER2_IOCOMF_CRYPTED) {
+                       hammer2_crypto_decrypt(iocom, ioq);
+               } else {
+                       ioq->fifo_cdx = ioq->fifo_end;
+                       ioq->fifo_cdn = ioq->fifo_end;
                }
+               bytes = ioq->fifo_cdx - ioq->fifo_beg;
 
                /*
                 * Insufficient data accumulated (msg is NULL, caller will
                 * retry on event).
                 */
                assert(msg == NULL);
-               if (bytes < (int)sizeof(msg->any.head))
+               if (bytes < sizeof(msg->any.head))
                        break;
 
-               /*
-                * Calculate the header, decrypt data received so far.
-                * Data will be decrypted in-place.  Partial blocks are
-                * not immediately decrypted.
-                */
-               hammer2_crypto_decrypt(iocom, ioq);
-               flags = 0;
-               head = (void *)(ioq->buf + ioq->fifo_beg);
-
                /*
                 * Check and fixup the core header.  Note that the icrc
                 * has to be calculated before any fixups, but the crc
                 * fields in the msg may have to be swapped like everything
                 * else.
                 */
+               head = (void *)(ioq->buf + ioq->fifo_beg);
                if (head->magic != HAMMER2_MSGHDR_MAGIC &&
                    head->magic != HAMMER2_MSGHDR_MAGIC_REV) {
                        ioq->error = HAMMER2_IOQ_ERROR_SYNC;
                        break;
                }
 
-               xcrc32 = hammer2_icrc32((char *)head + HAMMER2_MSGHDR_CRCOFF,
-                                       HAMMER2_MSGHDR_CRCBYTES);
-               if (head->magic == HAMMER2_MSGHDR_MAGIC_REV) {
-                       hammer2_bswap_head(head);
-                       flags |= HAMMER2_MSGX_BSWAPPED;
-               }
-               xcrc16 = (uint16_t)xcrc32 ^ (uint16_t)(xcrc32 >> 16);
-               if (xcrc16 != head->icrc1) {
-                       ioq->error = HAMMER2_IOQ_ERROR_HCRC;
-                       break;
-               }
-
                /*
                 * Calculate the full header size and aux data size
                 */
-               ioq->hbytes = (head->cmd & HAMMER2_MSGF_SIZE) *
-                             HAMMER2_MSG_ALIGN;
-               ioq->abytes = head->aux_bytes * HAMMER2_MSG_ALIGN;
-               if (ioq->hbytes < (int)sizeof(msg->any.head) ||
-                   ioq->hbytes > (int)sizeof(msg->any) ||
+               if (head->magic == HAMMER2_MSGHDR_MAGIC_REV) {
+                       ioq->hbytes = (bswap32(head->cmd) & HAMMER2_MSGF_SIZE) *
+                                     HAMMER2_MSG_ALIGN;
+                       ioq->abytes = bswap32(head->aux_bytes) *
+                                     HAMMER2_MSG_ALIGN;
+               } else {
+                       ioq->hbytes = (head->cmd & HAMMER2_MSGF_SIZE) *
+                                     HAMMER2_MSG_ALIGN;
+                       ioq->abytes = head->aux_bytes * HAMMER2_MSG_ALIGN;
+               }
+               if (ioq->hbytes < sizeof(msg->any.head) ||
+                   ioq->hbytes > sizeof(msg->any) ||
                    ioq->abytes > HAMMER2_MSGAUX_MAX) {
                        ioq->error = HAMMER2_IOQ_ERROR_FIELD;
                        break;
                }
 
                /*
-                * Finally allocate the message and copy the core header
-                * to the embedded extended header.
-                *
-                * Initialize msg->aux_size to 0 and use it to track
-                * the amount of data copied from the stream.
+                * Allocate the message, the next state will fill it in.
                 */
-               msg = hammer2_allocmsg(iocom, 0, ioq->abytes);
-               msg->aux_size = 0;
-               msg->flags = flags;
+               msg = hammer2_msg_alloc(&iocom->router, ioq->abytes, 0,
+                                       NULL, NULL);
                ioq->msg = msg;
 
-               /*
-                * We are either done or we fall-through
-                */
-               if (ioq->hbytes == sizeof(msg->any.head) && ioq->abytes == 0) {
-                       bcopy(head, &msg->any.head, sizeof(msg->any.head));
-                       ioq->fifo_beg += ioq->hbytes;
-                       break;
-               }
-
                /*
                 * Fall through to the next state.  Make sure that the
                 * extended header does not straddle the end of the buffer.
                 * We still want to issue larger reads into our buffer,
                 * book-keeping is easier if we don't bcopy() yet.
+                *
+                * Make sure there is enough room for bloated encrypt data.
                 */
-               if (bytes + nmax < ioq->hbytes) {
-                       bcopy(ioq->buf + ioq->fifo_beg, ioq->buf, bytes);
-                       ioq->fifo_cdx -= ioq->fifo_beg;
-                       ioq->fifo_beg = 0;
-                       ioq->fifo_end = bytes;
-                       nmax = sizeof(ioq->buf) - ioq->fifo_end;
-               }
+               nmax = hammer2_ioq_makeroom(ioq, ioq->hbytes);
                ioq->state = HAMMER2_MSGQ_STATE_HEADER2;
                /* fall through */
        case HAMMER2_MSGQ_STATE_HEADER2:
@@ -431,7 +661,7 @@ hammer2_ioq_read(hammer2_iocom_t *iocom)
                assert(msg != NULL);
                if (bytes < ioq->hbytes) {
                        n = read(iocom->sock_fd,
-                                msg->any.buf + ioq->fifo_end,
+                                ioq->buf + ioq->fifo_end,
                                 nmax);
                        if (n <= 0) {
                                if (n == 0) {
@@ -447,10 +677,17 @@ hammer2_ioq_read(hammer2_iocom_t *iocom)
                                n = 0;
                                /* fall through */
                        }
-                       ioq->fifo_end += n;
-                       bytes += n;
-                       nmax -= n;
+                       ioq->fifo_end += (size_t)n;
+                       nmax -= (size_t)n;
+               }
+
+               if (iocom->flags & HAMMER2_IOCOMF_CRYPTED) {
+                       hammer2_crypto_decrypt(iocom, ioq);
+               } else {
+                       ioq->fifo_cdx = ioq->fifo_end;
+                       ioq->fifo_cdn = ioq->fifo_end;
                }
+               bytes = ioq->fifo_cdx - ioq->fifo_beg;
 
                /*
                 * Insufficient data accumulated (set msg NULL so caller will
@@ -463,22 +700,31 @@ hammer2_ioq_read(hammer2_iocom_t *iocom)
 
                /*
                 * Calculate the extended header, decrypt data received
-                * so far.
+                * so far.  Handle endian-conversion for the entire extended
+                * header.
                 */
-               hammer2_crypto_decrypt(iocom, ioq);
                head = (void *)(ioq->buf + ioq->fifo_beg);
 
                /*
-                * Check the crc on the extended header
+                * Check the CRC.
                 */
-               if (ioq->hbytes > (int)sizeof(hammer2_msg_hdr_t)) {
-                       xcrc32 = hammer2_icrc32(head + 1,
-                                               ioq->hbytes - sizeof(*head));
-                       xcrc16 = (uint16_t)xcrc32 ^ (uint16_t)(xcrc32 >> 16);
-                       if (head->icrc2 != xcrc16) {
-                               ioq->error = HAMMER2_IOQ_ERROR_XCRC;
-                               break;
-                       }
+               if (head->magic == HAMMER2_MSGHDR_MAGIC_REV)
+                       xcrc32 = bswap32(head->hdr_crc);
+               else
+                       xcrc32 = head->hdr_crc;
+               head->hdr_crc = 0;
+               if (hammer2_icrc32(head, ioq->hbytes) != xcrc32) {
+                       ioq->error = HAMMER2_IOQ_ERROR_XCRC;
+                       fprintf(stderr, "BAD-XCRC(%08x,%08x) %s\n",
+                               xcrc32, hammer2_icrc32(head, ioq->hbytes),
+                               hammer2_msg_str(msg));
+                       assert(0);
+                       break;
+               }
+               head->hdr_crc = xcrc32;
+
+               if (head->magic == HAMMER2_MSGHDR_MAGIC_REV) {
+                       hammer2_bswap_head(head);
                }
 
                /*
@@ -496,31 +742,29 @@ hammer2_ioq_read(hammer2_iocom_t *iocom)
                }
 
                /*
-                * Must adjust nmax and bytes (and the state) when falling
-                * through.
+                * Must adjust bytes (and the state) when falling through.
+                * nmax doesn't change.
                 */
                ioq->fifo_beg += ioq->hbytes;
-               nmax -= ioq->hbytes;
                bytes -= ioq->hbytes;
                ioq->state = HAMMER2_MSGQ_STATE_AUXDATA1;
                /* fall through */
        case HAMMER2_MSGQ_STATE_AUXDATA1:
                /*
                 * Copy the partial or complete payload from remaining
-                * bytes in the FIFO.  We have to fall-through either
+                * bytes in the FIFO in order to optimize the makeroom call
+                * in the AUXDATA2 state.  We have to fall-through either
                 * way so we can check the crc.
+                *
+                * msg->aux_size tracks our aux data.
                 */
-               assert(msg->aux_size == 0);
-               ioq->already = ioq->fifo_cdx - ioq->fifo_beg;
-               if (ioq->already > ioq->abytes)
-                       ioq->already = ioq->abytes;
                if (bytes >= ioq->abytes) {
                        bcopy(ioq->buf + ioq->fifo_beg, msg->aux_data,
                              ioq->abytes);
                        msg->aux_size = ioq->abytes;
                        ioq->fifo_beg += ioq->abytes;
-                       if (ioq->fifo_cdx < ioq->fifo_beg)
-                               ioq->fifo_cdx = ioq->fifo_beg;
+                       assert(ioq->fifo_beg <= ioq->fifo_cdx);
+                       assert(ioq->fifo_cdx <= ioq->fifo_cdn);
                        bytes -= ioq->abytes;
                } else if (bytes) {
                        bcopy(ioq->buf + ioq->fifo_beg, msg->aux_data,
@@ -529,21 +773,29 @@ hammer2_ioq_read(hammer2_iocom_t *iocom)
                        ioq->fifo_beg += bytes;
                        if (ioq->fifo_cdx < ioq->fifo_beg)
                                ioq->fifo_cdx = ioq->fifo_beg;
+                       assert(ioq->fifo_beg <= ioq->fifo_cdx);
+                       assert(ioq->fifo_cdx <= ioq->fifo_cdn);
                        bytes = 0;
+               } else {
+                       msg->aux_size = 0;
                }
                ioq->state = HAMMER2_MSGQ_STATE_AUXDATA2;
                /* fall through */
        case HAMMER2_MSGQ_STATE_AUXDATA2:
                /*
-                * Read the remainder of the payload directly into the
-                * msg->aux_data buffer.
+                * Make sure there is enough room for more data.
                 */
                assert(msg);
+               nmax = hammer2_ioq_makeroom(ioq, ioq->abytes - msg->aux_size);
+
+               /*
+                * Read and decrypt more of the payload.
+                */
                if (msg->aux_size < ioq->abytes) {
                        assert(bytes == 0);
                        n = read(iocom->sock_fd,
-                                msg->aux_data + msg->aux_size,
-                                ioq->abytes - msg->aux_size);
+                                ioq->buf + ioq->fifo_end,
+                                nmax);
                        if (n <= 0) {
                                if (n == 0) {
                                        ioq->error = HAMMER2_IOQ_ERROR_EOF;
@@ -558,7 +810,27 @@ hammer2_ioq_read(hammer2_iocom_t *iocom)
                                n = 0;
                                /* fall through */
                        }
-                       msg->aux_size += n;
+                       ioq->fifo_end += (size_t)n;
+                       nmax -= (size_t)n;
+               }
+
+               if (iocom->flags & HAMMER2_IOCOMF_CRYPTED) {
+                       hammer2_crypto_decrypt(iocom, ioq);
+               } else {
+                       ioq->fifo_cdx = ioq->fifo_end;
+                       ioq->fifo_cdn = ioq->fifo_end;
+               }
+               bytes = ioq->fifo_cdx - ioq->fifo_beg;
+
+               if (bytes > ioq->abytes - msg->aux_size)
+                       bytes = ioq->abytes - msg->aux_size;
+
+               if (bytes) {
+                       bcopy(ioq->buf + ioq->fifo_beg,
+                             msg->aux_data + msg->aux_size,
+                             bytes);
+                       msg->aux_size += bytes;
+                       ioq->fifo_beg += bytes;
                }
 
                /*
@@ -570,18 +842,24 @@ hammer2_ioq_read(hammer2_iocom_t *iocom)
                        break;
                }
                assert(msg->aux_size == ioq->abytes);
-               hammer2_crypto_decrypt_aux(iocom, ioq, msg, ioq->already);
 
                /*
-                * Check aux_icrc, then we are done.
+                * Check aux_crc, then we are done.
                 */
                xcrc32 = hammer2_icrc32(msg->aux_data, msg->aux_size);
-               if (xcrc32 != msg->any.head.aux_icrc) {
+               if (xcrc32 != msg->any.head.aux_crc) {
                        ioq->error = HAMMER2_IOQ_ERROR_ACRC;
                        break;
                }
                break;
        case HAMMER2_MSGQ_STATE_ERROR:
+               /*
+                * Continued calls to drain recorded transactions (returning
+                * a LNK_ERROR for each one), before we return the final
+                * LNK_ERROR.
+                */
+               assert(msg == NULL);
+               break;
        default:
                /*
                 * We don't double-return errors, the caller should not
@@ -603,6 +881,20 @@ hammer2_ioq_read(hammer2_iocom_t *iocom)
                }
        }
 
+       /*
+        * Process transactional state for the message.
+        */
+       if (msg && ioq->error == 0) {
+               error = hammer2_state_msgrx(iocom, msg);
+               if (error) {
+                       if (error == HAMMER2_IOQ_ERROR_EALREADY) {
+                               hammer2_msg_free(msg);
+                               goto again;
+                       }
+                       ioq->error = error;
+               }
+       }
+
        /*
         * Handle error, RREQ, or completion
         *
@@ -610,33 +902,100 @@ hammer2_ioq_read(hammer2_iocom_t *iocom)
         *       to update them when breaking out.
         */
        if (ioq->error) {
+skip:
                /*
-                * An unrecoverable error occured during processing,
-                * return a special error message.  Try to leave the
-                * ioq state alone for post-mortem debugging.
+                * An unrecoverable error causes all active receive
+                * transactions to be terminated with a LNK_ERROR message.
                 *
-                * Link error messages are returned as one-way messages,
-                * so no flags get set.  Source and target is 0 (link-level),
-                * msgid is 0 (link-level).  All we really need to do is
-                * set up magic, cmd, and error.
+                * Once all active transactions are exhausted we set the
+                * iocom ERROR flag and return a non-transactional LNK_ERROR
+                * message, which should cause master processing loops to
+                * terminate.
                 */
                assert(ioq->msg == msg);
-               if (msg == NULL)
-                       msg = hammer2_allocmsg(iocom, 0, 0);
-               else
+               if (msg) {
+                       hammer2_msg_free(msg);
                        ioq->msg = NULL;
-
-               if (msg->aux_data) {
-                       free(msg->aux_data);
-                       msg->aux_data = NULL;
-                       msg->aux_size = 0;
                }
+
+               /*
+                * No more I/O read processing
+                */
+               ioq->state = HAMMER2_MSGQ_STATE_ERROR;
+
+               /*
+                * Simulate a remote LNK_ERROR DELETE msg for any open
+                * transactions, ending with a final non-transactional
+                * LNK_ERROR (that the session can detect) when no
+                * transactions remain.
+                */
+               msg = hammer2_msg_alloc(&iocom->router, 0, 0, NULL, NULL);
                bzero(&msg->any.head, sizeof(msg->any.head));
                msg->any.head.magic = HAMMER2_MSGHDR_MAGIC;
                msg->any.head.cmd = HAMMER2_LNK_ERROR;
                msg->any.head.error = ioq->error;
-               ioq->state = HAMMER2_MSGQ_STATE_ERROR;
-               iocom->flags |= HAMMER2_IOCOMF_EOF;
+
+               pthread_mutex_lock(&iocom->mtx);
+               hammer2_iocom_drain(iocom);
+               if ((state = RB_ROOT(&iocom->router.staterd_tree)) != NULL) {
+                       /*
+                        * Active remote transactions are still present.
+                        * Simulate the other end sending us a DELETE.
+                        */
+                       if (state->rxcmd & HAMMER2_MSGF_DELETE) {
+                               hammer2_msg_free(msg);
+                               msg = NULL;
+                       } else {
+                               /*state->txcmd |= HAMMER2_MSGF_DELETE;*/
+                               msg->state = state;
+                               msg->any.head.spanid = state->spanid;
+                               msg->any.head.msgid = state->msgid;
+                               msg->any.head.cmd |= HAMMER2_MSGF_ABORT |
+                                                    HAMMER2_MSGF_DELETE;
+                       }
+               } else if ((state = RB_ROOT(&iocom->router.statewr_tree)) != NULL) {
+                       /*
+                        * Active local transactions are still present.
+                        * Simulate the other end sending us a DELETE.
+                        */
+                       if (state->rxcmd & HAMMER2_MSGF_DELETE) {
+                               hammer2_msg_free(msg);
+                               msg = NULL;
+                       } else {
+                               msg->state = state;
+                               msg->any.head.spanid = state->spanid;
+                               msg->any.head.msgid = state->msgid;
+                               msg->any.head.cmd |= HAMMER2_MSGF_ABORT |
+                                                    HAMMER2_MSGF_DELETE |
+                                                    HAMMER2_MSGF_REPLY;
+                               if ((state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
+                                       msg->any.head.cmd |=
+                                                    HAMMER2_MSGF_CREATE;
+                               }
+                       }
+               } else {
+                       /*
+                        * No active local or remote transactions remain.
+                        * Generate a final LNK_ERROR and flag EOF.
+                        */
+                       msg->state = NULL;
+                       iocom->flags |= HAMMER2_IOCOMF_EOF;
+                       fprintf(stderr, "EOF ON SOCKET %d\n", iocom->sock_fd);
+               }
+               pthread_mutex_unlock(&iocom->mtx);
+
+               /*
+                * For the iocom error case we want to set RWORK to indicate
+                * that more messages might be pending.
+                *
+                * It is possible to return NULL when there is more work to
+                * do because each message has to be DELETEd in both
+                * directions before we continue on with the next (though
+                * this could be optimized).  The transmit direction will
+                * re-set RWORK.
+                */
+               if (msg)
+                       iocom->flags |= HAMMER2_IOCOMF_RWORK;
        } else if (msg == NULL) {
                /*
                 * Insufficient data received to finish building the message,
@@ -646,27 +1005,26 @@ hammer2_ioq_read(hammer2_iocom_t *iocom)
                 * Leave the FIFO intact.
                 */
                iocom->flags |= HAMMER2_IOCOMF_RREQ;
-#if 0
-               ioq->fifo_cdx = 0;
-               ioq->fifo_beg = 0;
-               ioq->fifo_end = 0;
-#endif
        } else {
                /*
-                * Return msg, clear the FIFO if it is now empty.
-                * Flag RREQ if the caller needs to wait for a read-event
-                * or not.
+                * Return msg.
                 *
                 * The fifo has already been advanced past the message.
                 * Trivially reset the FIFO indices if possible.
+                *
+                * clear the FIFO if it is now empty and set RREQ to wait
+                * for more from the socket.  If the FIFO is not empty set
+                * TWORK to bypass the poll so we loop immediately.
                 */
-               if (ioq->fifo_beg == ioq->fifo_end) {
+               if (ioq->fifo_beg == ioq->fifo_cdx &&
+                   ioq->fifo_cdn == ioq->fifo_end) {
                        iocom->flags |= HAMMER2_IOCOMF_RREQ;
                        ioq->fifo_cdx = 0;
+                       ioq->fifo_cdn = 0;
                        ioq->fifo_beg = 0;
                        ioq->fifo_end = 0;
                } else {
-                       iocom->flags &= ~HAMMER2_IOCOMF_RREQ;
+                       iocom->flags |= HAMMER2_IOCOMF_RWORK;
                }
                ioq->state = HAMMER2_MSGQ_STATE_HEADER1;
                ioq->msg = NULL;
@@ -676,140 +1034,141 @@ hammer2_ioq_read(hammer2_iocom_t *iocom)
 
 /*
  * Calculate the header and data crc's and write a low-level message to
- * the connection.  If aux_icrc is non-zero the aux_data crc is already
+ * the connection.  If aux_crc is non-zero the aux_data crc is already
  * assumed to have been set.
  *
  * A non-NULL msg is added to the queue but not necessarily flushed.
  * Calling this function with msg == NULL will get a flush going.
+ *
+ * Caller must hold iocom->mtx.
  */
 void
-hammer2_ioq_write(hammer2_msg_t *msg)
+hammer2_iocom_flush1(hammer2_iocom_t *iocom)
 {
-       hammer2_iocom_t *iocom = msg->iocom;
        hammer2_ioq_t *ioq = &iocom->ioq_tx;
-       uint16_t xcrc16;
+       hammer2_msg_t *msg;
        uint32_t xcrc32;
        int hbytes;
-
-       assert(msg);
-       if (ioq->error) {
-               TAILQ_INSERT_TAIL(&ioq->msgq, msg, entry);
-               ++ioq->msgcount;
-               hammer2_iocom_drain(iocom);
-               return;
+       hammer2_msg_queue_t tmpq;
+
+       iocom->flags &= ~(HAMMER2_IOCOMF_WREQ | HAMMER2_IOCOMF_WWORK);
+       TAILQ_INIT(&tmpq);
+       pthread_mutex_lock(&iocom->mtx);
+       while ((msg = TAILQ_FIRST(&iocom->router.txmsgq)) != NULL) {
+               TAILQ_REMOVE(&iocom->router.txmsgq, msg, qentry);
+               TAILQ_INSERT_TAIL(&tmpq, msg, qentry);
        }
+       pthread_mutex_unlock(&iocom->mtx);
 
-       /*
-        * Finish populating the msg fields.  The salt ensures that the iv[]
-        * array is ridiculously randomized and we also re-seed our PRNG
-        * every 32768 messages just to be sure.
-        */
-       msg->any.head.magic = HAMMER2_MSGHDR_MAGIC;
-       msg->any.head.salt = (random() << 8) | (ioq->seq & 255);
-       ++ioq->seq;
-       if ((ioq->seq & 32767) == 0)
-               srandomdev();
+       while ((msg = TAILQ_FIRST(&tmpq)) != NULL) {
+               /*
+                * Process terminal connection errors.
+                */
+               TAILQ_REMOVE(&tmpq, msg, qentry);
+               if (ioq->error) {
+                       TAILQ_INSERT_TAIL(&ioq->msgq, msg, qentry);
+                       ++ioq->msgcount;
+                       continue;
+               }
 
-       /*
-        * Calculate aux_icrc if 0, calculate icrc2, and finally
-        * calculate icrc1.
-        */
-       if (msg->aux_size && msg->any.head.aux_icrc == 0) {
+               /*
+                * Finish populating the msg fields.  The salt ensures that
+                * the iv[] array is ridiculously randomized and we also
+                * re-seed our PRNG every 32768 messages just to be sure.
+                */
+               msg->any.head.magic = HAMMER2_MSGHDR_MAGIC;
+               msg->any.head.salt = (random() << 8) | (ioq->seq & 255);
+               ++ioq->seq;
+               if ((ioq->seq & 32767) == 0)
+                       srandomdev();
+
+               /*
+                * Calculate aux_crc if 0, then calculate hdr_crc.
+                */
+               if (msg->aux_size && msg->any.head.aux_crc == 0) {
+                       assert((msg->aux_size & HAMMER2_MSG_ALIGNMASK) == 0);
+                       xcrc32 = hammer2_icrc32(msg->aux_data, msg->aux_size);
+                       msg->any.head.aux_crc = xcrc32;
+               }
+               msg->any.head.aux_bytes = msg->aux_size / HAMMER2_MSG_ALIGN;
                assert((msg->aux_size & HAMMER2_MSG_ALIGNMASK) == 0);
-               xcrc32 = hammer2_icrc32(msg->aux_data, msg->aux_size);
-               msg->any.head.aux_icrc = xcrc32;
-       }
-       msg->any.head.aux_bytes = msg->aux_size / HAMMER2_MSG_ALIGN;
-       assert((msg->aux_size & HAMMER2_MSG_ALIGNMASK) == 0);
 
-       if ((msg->any.head.cmd & HAMMER2_MSGF_SIZE) >
-           sizeof(msg->any.head) / HAMMER2_MSG_ALIGN) {
                hbytes = (msg->any.head.cmd & HAMMER2_MSGF_SIZE) *
-                       HAMMER2_MSG_ALIGN;
-               hbytes -= sizeof(msg->any.head);
-               xcrc32 = hammer2_icrc32(&msg->any.head + 1, hbytes);
-               xcrc16 = (uint16_t)xcrc32 ^ (uint16_t)(xcrc32 >> 16);
-               msg->any.head.icrc2 = xcrc16;
-       } else {
-               msg->any.head.icrc2 = 0;
-       }
-       xcrc32 = hammer2_icrc32(msg->any.buf + HAMMER2_MSGHDR_CRCOFF,
-                               HAMMER2_MSGHDR_CRCBYTES);
-       xcrc16 = (uint16_t)xcrc32 ^ (uint16_t)(xcrc32 >> 16);
-       msg->any.head.icrc1 = xcrc16;
-
-       /*
-        * XXX Encrypt the message
-        */
-
-       /*
-        * Enqueue the message.
-        */
-       TAILQ_INSERT_TAIL(&ioq->msgq, msg, entry);
-       ++ioq->msgcount;
-       iocom->flags &= ~HAMMER2_IOCOMF_WIDLE;
+                        HAMMER2_MSG_ALIGN;
+               msg->any.head.hdr_crc = 0;
+               msg->any.head.hdr_crc = hammer2_icrc32(&msg->any.head, hbytes);
 
-       /*
-        * Flush if we know we can write (WREQ not set) and if
-        * sufficient messages have accumulated.  Otherwise hold
-        * off to avoid piecemeal system calls.
-        */
-       if (iocom->flags & HAMMER2_IOCOMF_WREQ)
-               return;
-       if (ioq->msgcount < HAMMER2_IOQ_MAXIOVEC / 2)
-               return;
-       hammer2_iocom_flush(iocom);
+               /*
+                * Enqueue the message (the flush codes handles stream
+                * encryption).
+                */
+               TAILQ_INSERT_TAIL(&ioq->msgq, msg, qentry);
+               ++ioq->msgcount;
+       }
+       hammer2_iocom_flush2(iocom);
 }
 
+/*
+ * Thread localized, iocom->mtx not held by caller.
+ */
 void
-hammer2_iocom_flush(hammer2_iocom_t *iocom)
+hammer2_iocom_flush2(hammer2_iocom_t *iocom)
 {
        hammer2_ioq_t *ioq = &iocom->ioq_tx;
        hammer2_msg_t *msg;
-       ssize_t nmax;
-       ssize_t nact;
+       ssize_t n;
        struct iovec iov[HAMMER2_IOQ_MAXIOVEC];
-       int hbytes;
-       int abytes;
-       int hoff;
-       int aoff;
-       int n;
+       size_t nact;
+       size_t hbytes;
+       size_t abytes;
+       size_t hoff;
+       size_t aoff;
+       int iovcnt;
+
+       if (ioq->error) {
+               hammer2_iocom_drain(iocom);
+               return;
+       }
 
        /*
         * Pump messages out the connection by building an iovec.
+        *
+        * ioq->hbytes/ioq->abytes tracks how much of the first message
+        * in the queue has been successfully written out, so we can
+        * resume writing.
         */
-       n = 0;
-       nmax = 0;
+       iovcnt = 0;
+       nact = 0;
+       hoff = ioq->hbytes;
+       aoff = ioq->abytes;
 
-       TAILQ_FOREACH(msg, &ioq->msgq, entry) {
-               hoff = 0;
+       TAILQ_FOREACH(msg, &ioq->msgq, qentry) {
                hbytes = (msg->any.head.cmd & HAMMER2_MSGF_SIZE) *
                         HAMMER2_MSG_ALIGN;
-               aoff = 0;
                abytes = msg->aux_size;
-               if (n == 0) {
-                       hoff += ioq->hbytes;
-                       aoff += ioq->abytes;
-               }
-               if (hbytes - hoff > 0) {
-                       iov[n].iov_base = (char *)&msg->any.head + hoff;
-                       iov[n].iov_len = hbytes - hoff;
-                       nmax += hbytes - hoff;
-                       ++n;
-                       if (n == HAMMER2_IOQ_MAXIOVEC)
+               assert(hoff <= hbytes && aoff <= abytes);
+
+               if (hoff < hbytes) {
+                       iov[iovcnt].iov_base = (char *)&msg->any.head + hoff;
+                       iov[iovcnt].iov_len = hbytes - hoff;
+                       nact += hbytes - hoff;
+                       ++iovcnt;
+                       if (iovcnt == HAMMER2_IOQ_MAXIOVEC)
                                break;
                }
-               if (abytes - aoff > 0) {
+               if (aoff < abytes) {
                        assert(msg->aux_data != NULL);
-                       iov[n].iov_base = msg->aux_data + aoff;
-                       iov[n].iov_len = abytes - aoff;
-                       nmax += abytes - aoff;
-                       ++n;
-                       if (n == HAMMER2_IOQ_MAXIOVEC)
+                       iov[iovcnt].iov_base = (char *)msg->aux_data + aoff;
+                       iov[iovcnt].iov_len = abytes - aoff;
+                       nact += abytes - aoff;
+                       ++iovcnt;
+                       if (iovcnt == HAMMER2_IOQ_MAXIOVEC)
                                break;
                }
+               hoff = 0;
+               aoff = 0;
        }
-       if (n == 0)
+       if (iovcnt == 0)
                return;
 
        /*
@@ -817,65 +1176,109 @@ hammer2_iocom_flush(hammer2_iocom_t *iocom)
         * data into the fifo and adjust the iov as necessary.  If
         * encryption is disabled the iov is left alone.
         *
-        * hammer2_crypto_encrypt_wrote()
+        * May return a smaller iov (thus a smaller n), with aggregated
+        * chunks.  May reduce nmax to what fits in the FIFO.
+        *
+        * This function sets nact to the number of original bytes now
+        * encrypted, adding to the FIFO some number of bytes that might
+        * be greater depending on the crypto mechanic.  iov[] is adjusted
+        * to point at the FIFO if necessary.
+        *
+        * NOTE: The return value from the writev() is the post-encrypted
+        *       byte count, not the plaintext count.
         */
-       n = hammer2_crypto_encrypt(iocom, ioq, iov, n);
+       if (iocom->flags & HAMMER2_IOCOMF_CRYPTED) {
+               /*
+                * Make sure the FIFO has a reasonable amount of space
+                * left (if not completely full).
+                */
+               if (ioq->fifo_beg > sizeof(ioq->buf) / 2 &&
+                   sizeof(ioq->buf) - ioq->fifo_end >= HAMMER2_MSG_ALIGN * 2) {
+                       bcopy(ioq->buf + ioq->fifo_beg, ioq->buf,
+                             ioq->fifo_end - ioq->fifo_beg);
+                       ioq->fifo_cdx -= ioq->fifo_beg;
+                       ioq->fifo_cdn -= ioq->fifo_beg;
+                       ioq->fifo_end -= ioq->fifo_beg;
+                       ioq->fifo_beg = 0;
+               }
 
-       /*
-        * Execute the writev() then figure out what happened.
-        */
-       nact = writev(iocom->sock_fd, iov, n);
-       if (nact < 0) {
-               if (errno != EINTR &&
-                   errno != EINPROGRESS &&
-                   errno != EAGAIN) {
-                       ioq->error = HAMMER2_IOQ_ERROR_SOCK;
-                       hammer2_iocom_drain(iocom);
-               } else {
-                       iocom->flags |= HAMMER2_IOCOMF_WREQ;
+               iovcnt = hammer2_crypto_encrypt(iocom, ioq, iov, iovcnt, &nact);
+               n = writev(iocom->sock_fd, iov, iovcnt);
+               if (n > 0) {
+                       ioq->fifo_beg += n;
+                       ioq->fifo_cdn += n;
+                       ioq->fifo_cdx += n;
+                       if (ioq->fifo_beg == ioq->fifo_end) {
+                               ioq->fifo_beg = 0;
+                               ioq->fifo_cdn = 0;
+                               ioq->fifo_cdx = 0;
+                               ioq->fifo_end = 0;
+                       }
                }
-               return;
+       } else {
+               n = writev(iocom->sock_fd, iov, iovcnt);
+               if (n > 0)
+                       nact = n;
+               else
+                       nact = 0;
        }
-       hammer2_crypto_encrypt_wrote(iocom, ioq, nact);
-       if (nact == nmax)
-               iocom->flags &= ~HAMMER2_IOCOMF_WREQ;
-       else
-               iocom->flags |= HAMMER2_IOCOMF_WREQ;
 
+       /*
+        * Clean out the transmit queue based on what we successfully
+        * sent (nact is the plaintext count).  ioq->hbytes/abytes
+        * represents the portion of the first message previously sent.
+        */
        while ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
                hbytes = (msg->any.head.cmd & HAMMER2_MSGF_SIZE) *
                         HAMMER2_MSG_ALIGN;
                abytes = msg->aux_size;
 
-               if (nact < hbytes - ioq->hbytes) {
+               if ((size_t)nact < hbytes - ioq->hbytes) {
                        ioq->hbytes += nact;
+                       nact = 0;
                        break;
                }
                nact -= hbytes - ioq->hbytes;
                ioq->hbytes = hbytes;
-               if (nact < abytes - ioq->abytes) {
+               if ((size_t)nact < abytes - ioq->abytes) {
                        ioq->abytes += nact;
+                       nact = 0;
                        break;
                }
                nact -= abytes - ioq->abytes;
 
-               TAILQ_REMOVE(&ioq->msgq, msg, entry);
+               TAILQ_REMOVE(&ioq->msgq, msg, qentry);
                --ioq->msgcount;
                ioq->hbytes = 0;
                ioq->abytes = 0;
-               if (msg->aux_data)
-                       TAILQ_INSERT_TAIL(&iocom->freeq_aux, msg, entry);
-               else
-                       TAILQ_INSERT_TAIL(&iocom->freeq, msg, entry);
+
+               hammer2_state_cleanuptx(iocom, msg);
        }
-       if (msg == NULL) {
-               iocom->flags |= HAMMER2_IOCOMF_WIDLE;
-               iocom->flags &= ~HAMMER2_IOCOMF_WREQ;
+       assert(nact == 0);
+
+       /*
+        * Process the return value from the write w/regards to blocking.
+        */
+       if (n < 0) {
+               if (errno != EINTR &&
+                   errno != EINPROGRESS &&
+                   errno != EAGAIN) {
+                       /*
+                        * Fatal write error
+                        */
+                       ioq->error = HAMMER2_IOQ_ERROR_SOCK;
+                       hammer2_iocom_drain(iocom);
+               } else {
+                       /*
+                        * Wait for socket buffer space
+                        */
+                       iocom->flags |= HAMMER2_IOCOMF_WREQ;
+               }
+       } else {
+               iocom->flags |= HAMMER2_IOCOMF_WREQ;
        }
        if (ioq->error) {
-               iocom->flags |= HAMMER2_IOCOMF_EOF |
-                               HAMMER2_IOCOMF_WIDLE;
-               iocom->flags &= ~HAMMER2_IOCOMF_WREQ;
+               hammer2_iocom_drain(iocom);
        }
 }
 
@@ -884,6 +1287,8 @@ hammer2_iocom_flush(hammer2_iocom_t *iocom)
  * write events will occur.  We don't kill read msgs because we want
  * the caller to pull off our contrived terminal error msg to detect
  * the connection failure.
+ *
+ * Thread localized, iocom->mtx not held by caller.
  */
 void
 hammer2_iocom_drain(hammer2_iocom_t *iocom)
@@ -891,47 +1296,807 @@ hammer2_iocom_drain(hammer2_iocom_t *iocom)
        hammer2_ioq_t *ioq = &iocom->ioq_tx;
        hammer2_msg_t *msg;
 
+       iocom->flags &= ~(HAMMER2_IOCOMF_WREQ | HAMMER2_IOCOMF_WWORK);
+       ioq->hbytes = 0;
+       ioq->abytes = 0;
+
        while ((msg = TAILQ_FIRST(&ioq->msgq)) != NULL) {
-               TAILQ_REMOVE(&ioq->msgq, msg, entry);
+               TAILQ_REMOVE(&ioq->msgq, msg, qentry);
                --ioq->msgcount;
-               hammer2_freemsg(msg);
+               hammer2_state_cleanuptx(iocom, msg);
+       }
+}
+
+/*
+ * Write a message to an iocom, with additional state processing.
+ */
+void
+hammer2_msg_write(hammer2_msg_t *msg)
+{
+       hammer2_iocom_t *iocom = msg->router->iocom;
+       hammer2_state_t *state;
+       char dummy;
+
+       /*
+        * Handle state processing, create state if necessary.
+        */
+       pthread_mutex_lock(&iocom->mtx);
+       if ((state = msg->state) != NULL) {
+               /*
+                * Existing transaction (could be reply).  It is also
+                * possible for this to be the first reply (CREATE is set),
+                * in which case we populate state->txcmd.
+                *
+                * state->txcmd is adjusted to hold the final message cmd,
+                * and we also be sure to set the CREATE bit here.  We did
+                * not set it in hammer2_msg_alloc() because that would have
+                * not been serialized (state could have gotten ripped out
+                * from under the message prior to it being transmitted).
+                */
+               if ((msg->any.head.cmd & (HAMMER2_MSGF_CREATE |
+                                         HAMMER2_MSGF_REPLY)) ==
+                   HAMMER2_MSGF_CREATE) {
+                       state->txcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
+               }
+               msg->any.head.msgid = state->msgid;
+               msg->any.head.spanid = state->spanid;
+               assert(((state->txcmd ^ msg->any.head.cmd) &
+                       HAMMER2_MSGF_REPLY) == 0);
+               if (msg->any.head.cmd & HAMMER2_MSGF_CREATE)
+                       state->txcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
+       } else {
+               msg->any.head.msgid = 0;
+               /* XXX set spanid by router */
        }
-       iocom->flags |= HAMMER2_IOCOMF_WIDLE;
-       iocom->flags &= ~HAMMER2_IOCOMF_WREQ;
+
+       /*
+        * Queue it for output, wake up the I/O pthread.  Note that the
+        * I/O thread is responsible for generating the CRCs and encryption.
+        */
+       TAILQ_INSERT_TAIL(&iocom->router.txmsgq, msg, qentry);
+       dummy = 0;
+       write(iocom->wakeupfds[1], &dummy, 1);  /* XXX optimize me */
+       pthread_mutex_unlock(&iocom->mtx);
 }
 
 /*
- * This is a shortcut to the normal hammer2_allocreply() mechanic which
- * uses the received message to formulate a final reply and error code.
- * Can be used to issue a final reply for one-way, one-off, or streaming
- * commands.
+ * This is a shortcut to formulate a reply to msg with a simple error code,
+ * It can reply to and terminate a transaction, or it can reply to a one-way
+ * messages.  A HAMMER2_LNK_ERROR command code is utilized to encode
+ * the error code (which can be 0).  Not all transactions are terminated
+ * with HAMMER2_LNK_ERROR status (the low level only cares about the
+ * MSGF_DELETE flag), but most are.
  *
  * Replies to one-way messages are a bit of an oxymoron but the feature
  * is used by the debug (DBG) protocol.
  *
- * The reply contains no data.
+ * The reply contains no extended data.
+ */
+void
+hammer2_msg_reply(hammer2_msg_t *msg, uint32_t error)
+{
+       hammer2_iocom_t *iocom = msg->router->iocom;
+       hammer2_state_t *state = msg->state;
+       hammer2_msg_t *nmsg;
+       uint32_t cmd;
+
+
+       /*
+        * Reply with a simple error code and terminate the transaction.
+        */
+       cmd = HAMMER2_LNK_ERROR;
+
+       /*
+        * Check if our direction has even been initiated yet, set CREATE.
+        *
+        * Check what direction this is (command or reply direction).  Note
+        * that txcmd might not have been initiated yet.
+        *
+        * If our direction has already been closed we just return without
+        * doing anything.
+        */
+       if (state) {
+               if (state->txcmd & HAMMER2_MSGF_DELETE)
+                       return;
+               if (state->txcmd & HAMMER2_MSGF_REPLY)
+                       cmd |= HAMMER2_MSGF_REPLY;
+               cmd |= HAMMER2_MSGF_DELETE;
+       } else {
+               if ((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0)
+                       cmd |= HAMMER2_MSGF_REPLY;
+       }
+
+       /*
+        * Allocate the message and associate it with the existing state.
+        * We cannot pass MSGF_CREATE to msg_alloc() because that may
+        * allocate new state.  We have our state already.
+        */
+       nmsg = hammer2_msg_alloc(&iocom->router, 0, cmd, NULL, NULL);
+       if (state) {
+               if ((state->txcmd & HAMMER2_MSGF_CREATE) == 0)
+                       nmsg->any.head.cmd |= HAMMER2_MSGF_CREATE;
+       }
+       nmsg->any.head.error = error;
+       nmsg->state = state;
+       hammer2_msg_write(nmsg);
+}
+
+/*
+ * Similar to hammer2_msg_reply() but leave the transaction open.  That is,
+ * we are generating a streaming reply or an intermediate acknowledgement
+ * of some sort as part of the higher level protocol, with more to come
+ * later.
+ */
+void
+hammer2_msg_result(hammer2_msg_t *msg, uint32_t error)
+{
+       hammer2_iocom_t *iocom = msg->router->iocom;
+       hammer2_state_t *state = msg->state;
+       hammer2_msg_t *nmsg;
+       uint32_t cmd;
+
+
+       /*
+        * Reply with a simple error code and terminate the transaction.
+        */
+       cmd = HAMMER2_LNK_ERROR;
+
+       /*
+        * Check if our direction has even been initiated yet, set CREATE.
+        *
+        * Check what direction this is (command or reply direction).  Note
+        * that txcmd might not have been initiated yet.
+        *
+        * If our direction has already been closed we just return without
+        * doing anything.
+        */
+       if (state) {
+               if (state->txcmd & HAMMER2_MSGF_DELETE)
+                       return;
+               if (state->txcmd & HAMMER2_MSGF_REPLY)
+                       cmd |= HAMMER2_MSGF_REPLY;
+               /* continuing transaction, do not set MSGF_DELETE */
+       } else {
+               if ((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0)
+                       cmd |= HAMMER2_MSGF_REPLY;
+       }
+
+       nmsg = hammer2_msg_alloc(&iocom->router, 0, cmd, NULL, NULL);
+       if (state) {
+               if ((state->txcmd & HAMMER2_MSGF_CREATE) == 0)
+                       nmsg->any.head.cmd |= HAMMER2_MSGF_CREATE;
+       }
+       nmsg->any.head.error = error;
+       nmsg->state = state;
+       hammer2_msg_write(nmsg);
+}
+
+/*
+ * Terminate a transaction given a state structure by issuing a DELETE.
+ */
+void
+hammer2_state_reply(hammer2_state_t *state, uint32_t error)
+{
+       hammer2_msg_t *nmsg;
+       uint32_t cmd = HAMMER2_LNK_ERROR | HAMMER2_MSGF_DELETE;
+
+       /*
+        * Nothing to do if we already transmitted a delete
+        */
+       if (state->txcmd & HAMMER2_MSGF_DELETE)
+               return;
+
+       /*
+        * Set REPLY if the other end initiated the command.  Otherwise
+        * we are the command direction.
+        */
+       if (state->txcmd & HAMMER2_MSGF_REPLY)
+               cmd |= HAMMER2_MSGF_REPLY;
+
+       nmsg = hammer2_msg_alloc(&state->iocom->router, 0, cmd, NULL, NULL);
+       if (state) {
+               if ((state->txcmd & HAMMER2_MSGF_CREATE) == 0)
+                       nmsg->any.head.cmd |= HAMMER2_MSGF_CREATE;
+       }
+       nmsg->any.head.error = error;
+       nmsg->state = state;
+       hammer2_msg_write(nmsg);
+}
+
+/************************************************************************
+ *                     TRANSACTION STATE HANDLING                      *
+ ************************************************************************
+ *
+ */
+
+/*
+ * Process state tracking for a message after reception, prior to
+ * execution.
+ *
+ * Called with msglk held and the msg dequeued.
+ *
+ * All messages are called with dummy state and return actual state.
+ * (One-off messages often just return the same dummy state).
+ *
+ * May request that caller discard the message by setting *discardp to 1.
+ * The returned state is not used in this case and is allowed to be NULL.
+ *
+ * --
+ *
+ * These routines handle persistent and command/reply message state via the
+ * CREATE and DELETE flags.  The first message in a command or reply sequence
+ * sets CREATE, the last message in a command or reply sequence sets DELETE.
+ *
+ * There can be any number of intermediate messages belonging to the same
+ * sequence sent inbetween the CREATE message and the DELETE message,
+ * which set neither flag.  This represents a streaming command or reply.
+ *
+ * Any command message received with CREATE set expects a reply sequence to
+ * be returned.  Reply sequences work the same as command sequences except the
+ * REPLY bit is also sent.  Both the command side and reply side can
+ * degenerate into a single message with both CREATE and DELETE set.  Note
+ * that one side can be streaming and the other side not, or neither, or both.
+ *
+ * The msgid is unique for the initiator.  That is, two sides sending a new
+ * message can use the same msgid without colliding.
  *
- * (msg) is eaten up by this function.
+ * --
+ *
+ * ABORT sequences work by setting the ABORT flag along with normal message
+ * state.  However, ABORTs can also be sent on half-closed messages, that is
+ * even if the command or reply side has already sent a DELETE, as long as
+ * the message has not been fully closed it can still send an ABORT+DELETE
+ * to terminate the half-closed message state.
+ *
+ * Since ABORT+DELETEs can race we silently discard ABORT's for message
+ * state which has already been fully closed.  REPLY+ABORT+DELETEs can
+ * also race, and in this situation the other side might have already
+ * initiated a new unrelated command with the same message id.  Since
+ * the abort has not set the CREATE flag the situation can be detected
+ * and the message will also be discarded.
+ *
+ * Non-blocking requests can be initiated with ABORT+CREATE[+DELETE].
+ * The ABORT request is essentially integrated into the command instead
+ * of being sent later on.  In this situation the command implementation
+ * detects that CREATE and ABORT are both set (vs ABORT alone) and can
+ * special-case non-blocking operation for the command.
+ *
+ * NOTE!  Messages with ABORT set without CREATE or DELETE are considered
+ *       to be mid-stream aborts for command/reply sequences.  ABORTs on
+ *       one-way messages are not supported.
+ *
+ * NOTE!  If a command sequence does not support aborts the ABORT flag is
+ *       simply ignored.
+ *
+ * --
+ *
+ * One-off messages (no reply expected) are sent with neither CREATE or DELETE
+ * set.  One-off messages cannot be aborted and typically aren't processed
+ * by these routines.  The REPLY bit can be used to distinguish whether a
+ * one-off message is a command or reply.  For example, one-off replies
+ * will typically just contain status updates.
+ */
+static int
+hammer2_state_msgrx(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
+{
+       hammer2_state_t *state;
+       hammer2_state_t dummy;
+       int error;
+
+       /*
+        * Lock RB tree and locate existing persistent state, if any.
+        *
+        * If received msg is a command state is on staterd_tree.
+        * If received msg is a reply state is on statewr_tree.
+        */
+
+       dummy.msgid = msg->any.head.msgid;
+       dummy.spanid = msg->any.head.spanid;
+       pthread_mutex_lock(&iocom->mtx);
+       if (msg->any.head.cmd & HAMMER2_MSGF_REPLY) {
+               state = RB_FIND(hammer2_state_tree,
+                               &iocom->router.statewr_tree, &dummy);
+       } else {
+               state = RB_FIND(hammer2_state_tree,
+                               &iocom->router.staterd_tree, &dummy);
+       }
+       msg->state = state;
+       pthread_mutex_unlock(&iocom->mtx);
+
+       /*
+        * Short-cut one-off or mid-stream messages (state may be NULL).
+        */
+       if ((msg->any.head.cmd & (HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE |
+                                 HAMMER2_MSGF_ABORT)) == 0) {
+               return(0);
+       }
+
+       /*
+        * Switch on CREATE, DELETE, REPLY, and also handle ABORT from
+        * inside the case statements.
+        */
+       switch(msg->any.head.cmd & (HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE |
+                                   HAMMER2_MSGF_REPLY)) {
+       case HAMMER2_MSGF_CREATE:
+       case HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE:
+               /*
+                * New persistant command received.
+                */
+               if (state) {
+                       fprintf(stderr, "duplicate-trans %s\n",
+                               hammer2_msg_str(msg));
+                       error = HAMMER2_IOQ_ERROR_TRANS;
+                       assert(0);
+                       break;
+               }
+               state = malloc(sizeof(*state));
+               bzero(state, sizeof(*state));
+               state->iocom = iocom;
+               state->flags = HAMMER2_STATE_DYNAMIC;
+               state->msg = msg;
+               state->txcmd = HAMMER2_MSGF_REPLY;
+               state->rxcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
+               state->flags |= HAMMER2_STATE_INSERTED;
+               state->msgid = msg->any.head.msgid;
+               state->spanid = msg->any.head.spanid;
+               msg->state = state;
+               pthread_mutex_lock(&iocom->mtx);
+               RB_INSERT(hammer2_state_tree,
+                         &iocom->router.staterd_tree, state);
+               pthread_mutex_unlock(&iocom->mtx);
+               error = 0;
+               if (DebugOpt) {
+                       fprintf(stderr, "create state %p id=%08x on iocom staterd %p\n",
+                               state, (uint32_t)state->msgid, iocom);
+               }
+               break;
+       case HAMMER2_MSGF_DELETE:
+               /*
+                * Persistent state is expected but might not exist if an
+                * ABORT+DELETE races the close.
+                */
+               if (state == NULL) {
+                       if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
+                               error = HAMMER2_IOQ_ERROR_EALREADY;
+                       } else {
+                               fprintf(stderr, "missing-state %s\n",
+                                       hammer2_msg_str(msg));
+                               error = HAMMER2_IOQ_ERROR_TRANS;
+                       assert(0);
+                       }
+                       break;
+               }
+
+               /*
+                * Handle another ABORT+DELETE case if the msgid has already
+                * been reused.
+                */
+               if ((state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
+                       if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
+                               error = HAMMER2_IOQ_ERROR_EALREADY;
+                       } else {
+                               fprintf(stderr, "reused-state %s\n",
+                                       hammer2_msg_str(msg));
+                               error = HAMMER2_IOQ_ERROR_TRANS;
+                       assert(0);
+                       }
+                       break;
+               }
+               error = 0;
+               break;
+       default:
+               /*
+                * Check for mid-stream ABORT command received, otherwise
+                * allow.
+                */
+               if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
+                       if (state == NULL ||
+                           (state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
+                               error = HAMMER2_IOQ_ERROR_EALREADY;
+                               break;
+                       }
+               }
+               error = 0;
+               break;
+       case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_CREATE:
+       case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE:
+               /*
+                * When receiving a reply with CREATE set the original
+                * persistent state message should already exist.
+                */
+               if (state == NULL) {
+                       fprintf(stderr, "no-state(r) %s\n",
+                               hammer2_msg_str(msg));
+                       error = HAMMER2_IOQ_ERROR_TRANS;
+                       assert(0);
+                       break;
+               }
+               assert(((state->rxcmd ^ msg->any.head.cmd) &
+                       HAMMER2_MSGF_REPLY) == 0);
+               state->rxcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
+               error = 0;
+               break;
+       case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_DELETE:
+               /*
+                * Received REPLY+ABORT+DELETE in case where msgid has
+                * already been fully closed, ignore the message.
+                */
+               if (state == NULL) {
+                       if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
+                               error = HAMMER2_IOQ_ERROR_EALREADY;
+                       } else {
+                               fprintf(stderr, "no-state(r,d) %s\n",
+                                       hammer2_msg_str(msg));
+                               error = HAMMER2_IOQ_ERROR_TRANS;
+                       assert(0);
+                       }
+                       break;
+               }
+
+               /*
+                * Received REPLY+ABORT+DELETE in case where msgid has
+                * already been reused for an unrelated message,
+                * ignore the message.
+                */
+               if ((state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
+                       if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
+                               error = HAMMER2_IOQ_ERROR_EALREADY;
+                       } else {
+                               fprintf(stderr, "reused-state(r,d) %s\n",
+                                       hammer2_msg_str(msg));
+                               error = HAMMER2_IOQ_ERROR_TRANS;
+                       assert(0);
+                       }
+                       break;
+               }
+               error = 0;
+               break;
+       case HAMMER2_MSGF_REPLY:
+               /*
+                * Check for mid-stream ABORT reply received to sent command.
+                */
+               if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
+                       if (state == NULL ||
+                           (state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
+                               error = HAMMER2_IOQ_ERROR_EALREADY;
+                               break;
+                       }
+               }
+               error = 0;
+               break;
+       }
+       return (error);
+}
+
+void
+hammer2_state_cleanuprx(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
+{
+       hammer2_state_t *state;
+
+       if ((state = msg->state) == NULL) {
+               /*
+                * Free a non-transactional message, there is no state
+                * to worry about.
+                */
+               hammer2_msg_free(msg);
+       } else if (msg->any.head.cmd & HAMMER2_MSGF_DELETE) {
+               /*
+                * Message terminating transaction, destroy the related
+                * state, the original message, and this message (if it
+                * isn't the original message due to a CREATE|DELETE).
+                */
+               pthread_mutex_lock(&iocom->mtx);
+               state->rxcmd |= HAMMER2_MSGF_DELETE;
+               if (state->txcmd & HAMMER2_MSGF_DELETE) {
+                       if (state->msg == msg)
+                               state->msg = NULL;
+                       assert(state->flags & HAMMER2_STATE_INSERTED);
+                       if (state->rxcmd & HAMMER2_MSGF_REPLY) {
+                               assert(msg->any.head.cmd & HAMMER2_MSGF_REPLY);
+                               RB_REMOVE(hammer2_state_tree,
+                                         &iocom->router.statewr_tree, state);
+                       } else {
+                               assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
+                               RB_REMOVE(hammer2_state_tree,
+                                         &iocom->router.staterd_tree, state);
+                       }
+                       state->flags &= ~HAMMER2_STATE_INSERTED;
+                       hammer2_state_free(state);
+               } else {
+                       ;
+               }
+               pthread_mutex_unlock(&iocom->mtx);
+               hammer2_msg_free(msg);
+       } else if (state->msg != msg) {
+               /*
+                * Message not terminating transaction, leave state intact
+                * and free message if it isn't the CREATE message.
+                */
+               hammer2_msg_free(msg);
+       }
+}
+
+static void
+hammer2_state_cleanuptx(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
+{
+       hammer2_state_t *state;
+
+       if ((state = msg->state) == NULL) {
+               hammer2_msg_free(msg);
+       } else if (msg->any.head.cmd & HAMMER2_MSGF_DELETE) {
+               pthread_mutex_lock(&iocom->mtx);
+               state->txcmd |= HAMMER2_MSGF_DELETE;
+               if (state->rxcmd & HAMMER2_MSGF_DELETE) {
+                       if (state->msg == msg)
+                               state->msg = NULL;
+                       assert(state->flags & HAMMER2_STATE_INSERTED);
+                       if (state->txcmd & HAMMER2_MSGF_REPLY) {
+                               assert(msg->any.head.cmd & HAMMER2_MSGF_REPLY);
+                               RB_REMOVE(hammer2_state_tree,
+                                         &iocom->router.staterd_tree, state);
+                       } else {
+                               assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
+                               RB_REMOVE(hammer2_state_tree,
+                                         &iocom->router.statewr_tree, state);
+                       }
+                       state->flags &= ~HAMMER2_STATE_INSERTED;
+                       hammer2_state_free(state);
+               } else {
+                       ;
+               }
+               pthread_mutex_unlock(&iocom->mtx);
+               hammer2_msg_free(msg);
+       } else if (state->msg != msg) {
+               hammer2_msg_free(msg);
+       }
+}
+
+/*
+ * Called with iocom locked
  */
 void
-hammer2_replymsg(hammer2_msg_t *msg, uint16_t error)
+hammer2_state_free(hammer2_state_t *state)
 {
-       hammer2_persist_t *pers;
-       uint16_t t16;
-
-       assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
-
-       t16 = msg->any.head.source;
-       msg->any.head.source = msg->any.head.target;
-       msg->any.head.target = t16;
-       msg->any.head.error = error;
-       msg->any.head.cmd |= HAMMER2_MSGF_REPLY;
-       msg->aux_size = 0;
-       if ((pers = msg->persist) != NULL) {
-               assert(pers->lrep & HAMMER2_MSGF_DELETE);
-               msg->any.head.cmd |= pers->lrep & (HAMMER2_MSGF_CREATE |
-                                                  HAMMER2_MSGF_DELETE);
-               pers->lrep &= ~(HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE);
-       }
-       hammer2_ioq_write(msg);
+       hammer2_iocom_t *iocom = state->iocom;
+       hammer2_msg_t *msg;
+       char dummy;
+
+       if (DebugOpt) {
+               fprintf(stderr, "terminate state %p id=%08x\n",
+                       state, (uint32_t)state->msgid);
+       }
+       assert(state->any.any == NULL);
+       msg = state->msg;
+       state->msg = NULL;
+       if (msg)
+               hammer2_msg_free_locked(msg);
+       free(state);
+
+       /*
+        * When an iocom error is present we are trying to close down the
+        * iocom, but we have to wait for all states to terminate before
+        * we can do so.  The iocom rx code will terminate the receive side
+        * for all transactions by simulating incoming DELETE messages,
+        * but the state doesn't go away until both sides are terminated.
+        *
+        * We may have to wake up the rx code.
+        */
+       if (iocom->ioq_rx.error &&
+           RB_EMPTY(&iocom->router.staterd_tree) &&
+           RB_EMPTY(&iocom->router.statewr_tree)) {
+               dummy = 0;
+               write(iocom->wakeupfds[1], &dummy, 1);
+       }
+}
+
+const char *
+hammer2_basecmd_str(uint32_t cmd)
+{
+       static char buf[64];
+       char protobuf[32];
+       char cmdbuf[32];
+       const char *protostr;
+       const char *cmdstr;
+
+       switch(cmd & HAMMER2_MSGF_PROTOS) {
+       case HAMMER2_MSG_PROTO_LNK:
+               protostr = "LNK_";
+               break;
+       case HAMMER2_MSG_PROTO_DBG:
+               protostr = "DBG_";
+               break;
+       case HAMMER2_MSG_PROTO_DOM:
+               protostr = "DOM_";
+               break;
+       case HAMMER2_MSG_PROTO_CAC:
+               protostr = "CAC_";
+               break;
+       case HAMMER2_MSG_PROTO_QRM:
+               protostr = "QRM_";
+               break;
+       case HAMMER2_MSG_PROTO_BLK:
+               protostr = "BLK_";
+               break;
+       case HAMMER2_MSG_PROTO_VOP:
+               protostr = "VOP_";
+               break;
+       default:
+               snprintf(protobuf, sizeof(protobuf), "%x_",
+                       (cmd & HAMMER2_MSGF_PROTOS) >> 20);
+               protostr = protobuf;
+               break;
+       }
+
+       switch(cmd & (HAMMER2_MSGF_PROTOS |
+                     HAMMER2_MSGF_CMDS |
+                     HAMMER2_MSGF_SIZE)) {
+       case HAMMER2_LNK_PAD:
+               cmdstr = "PAD";
+               break;
+       case HAMMER2_LNK_PING:
+               cmdstr = "PING";
+               break;
+       case HAMMER2_LNK_AUTH:
+               cmdstr = "AUTH";
+               break;
+       case HAMMER2_LNK_CONN:
+               cmdstr = "CONN";
+               break;
+       case HAMMER2_LNK_SPAN:
+               cmdstr = "SPAN";
+               break;
+       case HAMMER2_LNK_ERROR:
+               if (cmd & HAMMER2_MSGF_DELETE)
+                       cmdstr = "RETURN";
+               else
+                       cmdstr = "RESULT";
+               break;
+       case HAMMER2_DBG_SHELL:
+               cmdstr = "SHELL";
+               break;
+       default:
+               snprintf(cmdbuf, sizeof(cmdbuf),
+                        "%06x", (cmd & (HAMMER2_MSGF_PROTOS |
+                                        HAMMER2_MSGF_CMDS |
+                                        HAMMER2_MSGF_SIZE)));
+               cmdstr = cmdbuf;
+               break;
+       }
+       snprintf(buf, sizeof(buf), "%s%s", protostr, cmdstr);
+       return (buf);
+}
+
+const char *
+hammer2_msg_str(hammer2_msg_t *msg)
+{
+       hammer2_state_t *state;
+       static char buf[256];
+       char errbuf[16];
+       char statebuf[64];
+       char flagbuf[64];
+       const char *statestr;
+       const char *errstr;
+       uint32_t basecmd;
+       int i;
+
+       /*
+        * Parse the state
+        */
+       if ((state = msg->state) != NULL) {
+               basecmd = (state->rxcmd & HAMMER2_MSGF_REPLY) ?
+                         state->txcmd : state->rxcmd;
+               snprintf(statebuf, sizeof(statebuf),
+                        " %s=%s,L=%s%s,R=%s%s",
+                        ((state->txcmd & HAMMER2_MSGF_REPLY) ?
+                               "rcvcmd" : "sndcmd"),
+                        hammer2_basecmd_str(basecmd),
+                        ((state->txcmd & HAMMER2_MSGF_CREATE) ? "C" : ""),
+                        ((state->txcmd & HAMMER2_MSGF_DELETE) ? "D" : ""),
+                        ((state->rxcmd & HAMMER2_MSGF_CREATE) ? "C" : ""),
+                        ((state->rxcmd & HAMMER2_MSGF_DELETE) ? "D" : "")
+               );
+               statestr = statebuf;
+       } else {
+               statestr = "";
+       }
+
+       /*
+        * Parse the error
+        */
+       switch(msg->any.head.error) {
+       case 0:
+               errstr = "";
+               break;
+       case HAMMER2_IOQ_ERROR_SYNC:
+               errstr = "err=IOQ:NOSYNC";
+               break;
+       case HAMMER2_IOQ_ERROR_EOF:
+               errstr = "err=IOQ:STREAMEOF";
+               break;
+       case HAMMER2_IOQ_ERROR_SOCK:
+               errstr = "err=IOQ:SOCKERR";
+               break;
+       case HAMMER2_IOQ_ERROR_FIELD:
+               errstr = "err=IOQ:BADFIELD";
+               break;
+       case HAMMER2_IOQ_ERROR_HCRC:
+               errstr = "err=IOQ:BADHCRC";
+               break;
+       case HAMMER2_IOQ_ERROR_XCRC:
+               errstr = "err=IOQ:BADXCRC";
+               break;
+       case HAMMER2_IOQ_ERROR_ACRC:
+               errstr = "err=IOQ:BADACRC";
+               break;
+       case HAMMER2_IOQ_ERROR_STATE:
+               errstr = "err=IOQ:BADSTATE";
+               break;
+       case HAMMER2_IOQ_ERROR_NOPEER:
+               errstr = "err=IOQ:PEERCONFIG";
+               break;
+       case HAMMER2_IOQ_ERROR_NORKEY:
+               errstr = "err=IOQ:BADRKEY";
+               break;
+       case HAMMER2_IOQ_ERROR_NOLKEY:
+               errstr = "err=IOQ:BADLKEY";
+               break;
+       case HAMMER2_IOQ_ERROR_KEYXCHGFAIL:
+               errstr = "err=IOQ:BADKEYXCHG";
+               break;
+       case HAMMER2_IOQ_ERROR_KEYFMT:
+               errstr = "err=IOQ:BADFMT";
+               break;
+       case HAMMER2_IOQ_ERROR_BADURANDOM:
+               errstr = "err=IOQ:BADRANDOM";
+               break;
+       case HAMMER2_IOQ_ERROR_MSGSEQ:
+               errstr = "err=IOQ:BADSEQ";
+               break;
+       case HAMMER2_IOQ_ERROR_EALREADY:
+               errstr = "err=IOQ:DUPMSG";
+               break;
+       case HAMMER2_IOQ_ERROR_TRANS:
+               errstr = "err=IOQ:BADTRANS";
+               break;
+       case HAMMER2_MSG_ERR_NOSUPP:
+               errstr = "err=NOSUPPORT";
+               break;
+       default:
+               snprintf(errbuf, sizeof(errbuf),
+                        " err=%d", msg->any.head.error);
+               errstr = errbuf;
+               break;
+       }
+
+       /*
+        * Message flags
+        */
+       i = 0;
+       if (msg->any.head.cmd & (HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE |
+                                HAMMER2_MSGF_ABORT | HAMMER2_MSGF_REPLY)) {
+               flagbuf[i++] = '|';
+               if (msg->any.head.cmd & HAMMER2_MSGF_CREATE)
+                       flagbuf[i++] = 'C';
+               if (msg->any.head.cmd & HAMMER2_MSGF_DELETE)
+                       flagbuf[i++] = 'D';
+               if (msg->any.head.cmd & HAMMER2_MSGF_REPLY)
+                       flagbuf[i++] = 'R';
+               if (msg->any.head.cmd & HAMMER2_MSGF_ABORT)
+                       flagbuf[i++] = 'A';
+       }
+       flagbuf[i] = 0;
+
+       /*
+        * Generate the buf
+        */
+       snprintf(buf, sizeof(buf),
+               "msg=%s%s %s id=%08x span=%08x %s",
+                hammer2_basecmd_str(msg->any.head.cmd),
+                flagbuf,
+                errstr,
+                (uint32_t)(intmax_t)msg->any.head.msgid,   /* for brevity */
+                (uint32_t)(intmax_t)msg->any.head.spanid,  /* for brevity */
+                statestr);
+
+       return(buf);
 }