2 * Copyright (c) 2012 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * TODO: txcmd CREATE state is deferred by txmsgq, need to calculate
36 * a streaming response. See subr_diskiocom()'s diskiodone().
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/kernel.h>
42 #include <sys/systm.h>
43 #include <sys/queue.h>
45 #include <sys/malloc.h>
46 #include <sys/mount.h>
47 #include <sys/socket.h>
48 #include <sys/vnode.h>
52 #include <sys/thread.h>
53 #include <sys/globaldata.h>
54 #include <sys/limits.h>
58 RB_GENERATE(kdmsg_state_tree, kdmsg_state, rbnode, kdmsg_state_cmp);
60 static int kdmsg_msg_receive_handling(kdmsg_msg_t *msg);
61 static int kdmsg_state_msgrx(kdmsg_msg_t *msg);
62 static int kdmsg_state_msgtx(kdmsg_msg_t *msg);
63 static void kdmsg_state_cleanuprx(kdmsg_msg_t *msg);
64 static void kdmsg_state_cleanuptx(kdmsg_msg_t *msg);
65 static void kdmsg_state_abort(kdmsg_state_t *state);
66 static void kdmsg_state_free(kdmsg_state_t *state);
68 static void kdmsg_iocom_thread_rd(void *arg);
69 static void kdmsg_iocom_thread_wr(void *arg);
70 static int kdmsg_autorxmsg(kdmsg_msg_t *msg);
72 /*static struct lwkt_token kdmsg_token = LWKT_TOKEN_INITIALIZER(kdmsg_token);*/
75 * Initialize the roll-up communications structure for a network
76 * messaging session. This function does not install the socket.
79 kdmsg_iocom_init(kdmsg_iocom_t *iocom, void *handle, uint32_t flags,
80 struct malloc_type *mmsg,
81 int (*rcvmsg)(kdmsg_msg_t *msg))
83 bzero(iocom, sizeof(*iocom));
84 iocom->handle = handle;
86 iocom->rcvmsg = rcvmsg;
88 lockinit(&iocom->msglk, "h2msg", 0, 0);
89 TAILQ_INIT(&iocom->msgq);
90 RB_INIT(&iocom->staterd_tree);
91 RB_INIT(&iocom->statewr_tree);
93 iocom->state0.iocom = iocom;
94 iocom->state0.parent = &iocom->state0;
95 TAILQ_INIT(&iocom->state0.subq);
99 * [Re]connect using the passed file pointer. The caller must ref the
100 * fp for us. We own that ref now.
103 kdmsg_iocom_reconnect(kdmsg_iocom_t *iocom, struct file *fp,
104 const char *subsysname)
107 * Destroy the current connection
109 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
110 atomic_set_int(&iocom->msg_ctl, KDMSG_CLUSTERCTL_KILL);
111 while (iocom->msgrd_td || iocom->msgwr_td) {
112 wakeup(&iocom->msg_ctl);
113 lksleep(iocom, &iocom->msglk, 0, "clstrkl", hz);
117 * Drop communications descriptor
120 fdrop(iocom->msg_fp);
121 iocom->msg_fp = NULL;
125 * Setup new communications descriptor
130 iocom->flags &= ~KDMSG_IOCOMF_EXITNOACC;
132 lwkt_create(kdmsg_iocom_thread_rd, iocom, &iocom->msgrd_td,
133 NULL, 0, -1, "%s-msgrd", subsysname);
134 lwkt_create(kdmsg_iocom_thread_wr, iocom, &iocom->msgwr_td,
135 NULL, 0, -1, "%s-msgwr", subsysname);
136 lockmgr(&iocom->msglk, LK_RELEASE);
140 * Caller sets up iocom->auto_lnk_conn and iocom->auto_lnk_span, then calls
141 * this function to handle the state machine for LNK_CONN and LNK_SPAN.
143 static int kdmsg_lnk_conn_reply(kdmsg_state_t *state, kdmsg_msg_t *msg);
144 static int kdmsg_lnk_span_reply(kdmsg_state_t *state, kdmsg_msg_t *msg);
147 kdmsg_iocom_autoinitiate(kdmsg_iocom_t *iocom,
148 void (*auto_callback)(kdmsg_msg_t *msg))
152 iocom->auto_callback = auto_callback;
154 msg = kdmsg_msg_alloc(&iocom->state0,
155 DMSG_LNK_CONN | DMSGF_CREATE,
156 kdmsg_lnk_conn_reply, NULL);
157 iocom->auto_lnk_conn.head = msg->any.head;
158 msg->any.lnk_conn = iocom->auto_lnk_conn;
159 iocom->conn_state = msg->state;
160 kdmsg_msg_write(msg);
165 kdmsg_lnk_conn_reply(kdmsg_state_t *state, kdmsg_msg_t *msg)
167 kdmsg_iocom_t *iocom = state->iocom;
171 * Upon receipt of the LNK_CONN acknowledgement initiate an
172 * automatic SPAN if we were asked to. Used by e.g. xdisk, but
173 * not used by HAMMER2 which must manage more than one transmitted
176 if ((msg->any.head.cmd & DMSGF_CREATE) &&
177 (iocom->flags & KDMSG_IOCOMF_AUTOTXSPAN)) {
178 rmsg = kdmsg_msg_alloc(&iocom->state0,
179 DMSG_LNK_SPAN | DMSGF_CREATE,
180 kdmsg_lnk_span_reply, NULL);
181 iocom->auto_lnk_span.head = rmsg->any.head;
182 rmsg->any.lnk_span = iocom->auto_lnk_span;
183 kdmsg_msg_write(rmsg);
187 * Process shim after the CONN is acknowledged and before the CONN
188 * transaction is deleted. For deletions this gives device drivers
189 * the ability to interlock new operations on the circuit before
190 * it becomes illegal and panics.
192 if (iocom->auto_callback)
193 iocom->auto_callback(msg);
195 if ((state->txcmd & DMSGF_DELETE) == 0 &&
196 (msg->any.head.cmd & DMSGF_DELETE)) {
197 iocom->conn_state = NULL;
198 kdmsg_msg_reply(msg, 0);
206 kdmsg_lnk_span_reply(kdmsg_state_t *state, kdmsg_msg_t *msg)
209 * Be sure to process shim before terminating the SPAN
210 * transaction. Gives device drivers the ability to
211 * interlock new operations on the circuit before it
212 * becomes illegal and panics.
214 if (state->iocom->auto_callback)
215 state->iocom->auto_callback(msg);
217 if ((state->txcmd & DMSGF_DELETE) == 0 &&
218 (msg->any.head.cmd & DMSGF_DELETE)) {
219 kdmsg_msg_reply(msg, 0);
225 * Disconnect and clean up
228 kdmsg_iocom_uninit(kdmsg_iocom_t *iocom)
230 kdmsg_state_t *state;
233 * Ask the cluster controller to go away
235 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
236 atomic_set_int(&iocom->msg_ctl, KDMSG_CLUSTERCTL_KILL);
238 while (iocom->msgrd_td || iocom->msgwr_td) {
239 wakeup(&iocom->msg_ctl);
240 lksleep(iocom, &iocom->msglk, 0, "clstrkl", hz);
246 if ((state = iocom->freerd_state) != NULL) {
247 iocom->freerd_state = NULL;
248 kdmsg_state_free(state);
251 if ((state = iocom->freewr_state) != NULL) {
252 iocom->freewr_state = NULL;
253 kdmsg_state_free(state);
257 * Drop communications descriptor
260 fdrop(iocom->msg_fp);
261 iocom->msg_fp = NULL;
263 lockmgr(&iocom->msglk, LK_RELEASE);
267 * Cluster controller thread. Perform messaging functions. We have one
268 * thread for the reader and one for the writer. The writer handles
269 * shutdown requests (which should break the reader thread).
273 kdmsg_iocom_thread_rd(void *arg)
275 kdmsg_iocom_t *iocom = arg;
277 kdmsg_msg_t *msg = NULL;
282 while ((iocom->msg_ctl & KDMSG_CLUSTERCTL_KILL) == 0) {
284 * Retrieve the message from the pipe or socket.
286 error = fp_read(iocom->msg_fp, &hdr, sizeof(hdr),
287 NULL, 1, UIO_SYSSPACE);
290 if (hdr.magic != DMSG_HDR_MAGIC) {
291 kprintf("kdmsg: bad magic: %04x\n", hdr.magic);
295 hbytes = (hdr.cmd & DMSGF_SIZE) * DMSG_ALIGN;
296 if (hbytes < sizeof(hdr) || hbytes > DMSG_AUX_MAX) {
297 kprintf("kdmsg: bad header size %zd\n", hbytes);
302 /* XXX messy: mask cmd to avoid allocating state */
303 msg = kdmsg_msg_alloc(&iocom->state0,
304 hdr.cmd & DMSGF_BASECMDMASK,
307 msg->hdr_size = hbytes;
308 if (hbytes > sizeof(hdr)) {
309 error = fp_read(iocom->msg_fp, &msg->any.head + 1,
310 hbytes - sizeof(hdr),
311 NULL, 1, UIO_SYSSPACE);
313 kprintf("kdmsg: short msg received\n");
318 msg->aux_size = hdr.aux_bytes;
319 if (msg->aux_size > DMSG_AUX_MAX) {
320 kprintf("kdmsg: illegal msg payload size %zd\n",
326 abytes = DMSG_DOALIGN(msg->aux_size);
327 msg->aux_data = kmalloc(abytes, iocom->mmsg, M_WAITOK);
328 msg->flags |= KDMSG_FLAG_AUXALLOC;
329 error = fp_read(iocom->msg_fp, msg->aux_data,
330 abytes, NULL, 1, UIO_SYSSPACE);
332 kprintf("kdmsg: short msg payload received\n");
337 error = kdmsg_msg_receive_handling(msg);
342 kprintf("kdmsg: read failed error %d\n", error);
344 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
349 * Shutdown the socket before waiting for the transmit side.
351 * If we are dying due to e.g. a socket disconnect verses being
352 * killed explicity we have to set KILL in order to kick the tx
353 * side when it might not have any other work to do. KILL might
354 * already be set if we are in an unmount or reconnect.
356 fp_shutdown(iocom->msg_fp, SHUT_RDWR);
358 atomic_set_int(&iocom->msg_ctl, KDMSG_CLUSTERCTL_KILL);
359 wakeup(&iocom->msg_ctl);
362 * Wait for the transmit side to drain remaining messages
363 * before cleaning up the rx state. The transmit side will
364 * set KILLTX and wait for the rx side to completely finish
365 * (set msgrd_td to NULL) before cleaning up any remaining
368 lockmgr(&iocom->msglk, LK_RELEASE);
369 atomic_set_int(&iocom->msg_ctl, KDMSG_CLUSTERCTL_KILLRX);
370 wakeup(&iocom->msg_ctl);
371 while ((iocom->msg_ctl & KDMSG_CLUSTERCTL_KILLTX) == 0) {
372 wakeup(&iocom->msg_ctl);
373 tsleep(iocom, 0, "clstrkw", hz);
376 iocom->msgrd_td = NULL;
379 * iocom can be ripped out from under us at this point but
388 kdmsg_iocom_thread_wr(void *arg)
390 kdmsg_iocom_t *iocom = arg;
392 kdmsg_state_t *state;
402 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
404 while ((iocom->msg_ctl & KDMSG_CLUSTERCTL_KILL) == 0 && error == 0) {
406 * Sleep if no messages pending. Interlock with flag while
409 if (TAILQ_EMPTY(&iocom->msgq)) {
410 atomic_set_int(&iocom->msg_ctl,
411 KDMSG_CLUSTERCTL_SLEEPING);
412 lksleep(&iocom->msg_ctl, &iocom->msglk, 0, "msgwr", hz);
413 atomic_clear_int(&iocom->msg_ctl,
414 KDMSG_CLUSTERCTL_SLEEPING);
417 while ((msg = TAILQ_FIRST(&iocom->msgq)) != NULL) {
419 * Remove msg from the transmit queue and do
420 * persist and half-closed state handling.
422 TAILQ_REMOVE(&iocom->msgq, msg, qentry);
423 lockmgr(&iocom->msglk, LK_RELEASE);
425 error = kdmsg_state_msgtx(msg);
426 if (error == EALREADY) {
429 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
434 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
439 * Dump the message to the pipe or socket.
441 * We have to clean up the message as if the transmit
442 * succeeded even if it failed.
444 error = fp_write(iocom->msg_fp, &msg->any,
445 msg->hdr_size, &res, UIO_SYSSPACE);
446 if (error || res != msg->hdr_size) {
449 kdmsg_state_cleanuptx(msg);
450 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
454 abytes = DMSG_DOALIGN(msg->aux_size);
455 error = fp_write(iocom->msg_fp,
456 msg->aux_data, abytes,
458 if (error || res != abytes) {
461 kdmsg_state_cleanuptx(msg);
462 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
466 kdmsg_state_cleanuptx(msg);
467 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
472 * Cleanup messages pending transmission and release msgq lock.
475 kprintf("kdmsg: write failed error %d\n", error);
476 kprintf("thread_wr: Terminating iocom\n");
479 * Shutdown the socket. This will cause the rx thread to get an
480 * EOF and ensure that both threads get to a termination state.
482 fp_shutdown(iocom->msg_fp, SHUT_RDWR);
485 * Set KILLTX (which the rx side waits for), then wait for the RX
486 * side to completely finish before we clean out any remaining
489 lockmgr(&iocom->msglk, LK_RELEASE);
490 atomic_set_int(&iocom->msg_ctl, KDMSG_CLUSTERCTL_KILLTX);
491 wakeup(&iocom->msg_ctl);
492 while (iocom->msgrd_td) {
493 wakeup(&iocom->msg_ctl);
494 tsleep(iocom, 0, "clstrkw", hz);
496 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
499 * Simulate received MSGF_DELETE's for any remaining states.
500 * (For remote masters).
502 * Drain the message queue to handle any device initiated writes
503 * due to state callbacks.
506 RB_FOREACH(state, kdmsg_state_tree, &iocom->staterd_tree)
507 atomic_set_int(&state->flags, KDMSG_STATE_DYING);
508 RB_FOREACH(state, kdmsg_state_tree, &iocom->statewr_tree)
509 atomic_set_int(&state->flags, KDMSG_STATE_DYING);
510 kdmsg_drain_msgq(iocom);
511 RB_FOREACH(state, kdmsg_state_tree, &iocom->staterd_tree) {
512 if ((state->rxcmd & DMSGF_DELETE) == 0) {
513 lockmgr(&iocom->msglk, LK_RELEASE);
514 kdmsg_state_abort(state);
515 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
521 * Simulate received MSGF_DELETE's for any remaining states.
522 * (For local masters).
524 kdmsg_drain_msgq(iocom);
525 RB_FOREACH(state, kdmsg_state_tree, &iocom->statewr_tree) {
526 if ((state->rxcmd & DMSGF_DELETE) == 0) {
527 lockmgr(&iocom->msglk, LK_RELEASE);
528 kdmsg_state_abort(state);
529 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
535 * Retry until all work is done
538 panic("kdmsg: comm thread shutdown couldn't drain");
539 if (TAILQ_FIRST(&iocom->msgq) ||
540 RB_ROOT(&iocom->staterd_tree) ||
541 RB_ROOT(&iocom->statewr_tree)) {
544 iocom->flags |= KDMSG_IOCOMF_EXITNOACC;
546 lockmgr(&iocom->msglk, LK_RELEASE);
549 * The state trees had better be empty now
551 KKASSERT(RB_EMPTY(&iocom->staterd_tree));
552 KKASSERT(RB_EMPTY(&iocom->statewr_tree));
553 KKASSERT(iocom->conn_state == NULL);
555 if (iocom->exit_func) {
557 * iocom is invalid after we call the exit function.
559 iocom->msgwr_td = NULL;
560 iocom->exit_func(iocom);
563 * iocom can be ripped out from under us once msgwr_td is
564 * set to NULL. The wakeup is safe.
566 iocom->msgwr_td = NULL;
573 * This cleans out the pending transmit message queue, adjusting any
574 * persistent states properly in the process.
576 * Caller must hold pmp->iocom.msglk
579 kdmsg_drain_msgq(kdmsg_iocom_t *iocom)
584 * Clean out our pending transmit queue, executing the
585 * appropriate state adjustments. If this tries to open
586 * any new outgoing transactions we have to loop up and
589 while ((msg = TAILQ_FIRST(&iocom->msgq)) != NULL) {
590 TAILQ_REMOVE(&iocom->msgq, msg, qentry);
591 lockmgr(&iocom->msglk, LK_RELEASE);
592 if (kdmsg_state_msgtx(msg))
595 kdmsg_state_cleanuptx(msg);
596 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
601 * Do all processing required to handle a freshly received message
602 * after its low level header has been validated.
606 kdmsg_msg_receive_handling(kdmsg_msg_t *msg)
608 kdmsg_iocom_t *iocom = msg->state->iocom;
612 * State machine tracking, state assignment for msg,
613 * returns error and discard status. Errors are fatal
614 * to the connection except for EALREADY which forces
615 * a discard without execution.
617 error = kdmsg_state_msgrx(msg);
620 * Raw protocol or connection error
623 if (error == EALREADY)
625 } else if (msg->state && msg->state->func) {
627 * Message related to state which already has a
628 * handling function installed for it.
630 error = msg->state->func(msg->state, msg);
631 kdmsg_state_cleanuprx(msg);
632 } else if (iocom->flags & KDMSG_IOCOMF_AUTOANY) {
633 error = kdmsg_autorxmsg(msg);
634 kdmsg_state_cleanuprx(msg);
636 error = iocom->rcvmsg(msg);
637 kdmsg_state_cleanuprx(msg);
643 * Process state tracking for a message after reception, prior to
646 * Called with msglk held and the msg dequeued.
648 * All messages are called with dummy state and return actual state.
649 * (One-off messages often just return the same dummy state).
651 * May request that caller discard the message by setting *discardp to 1.
652 * The returned state is not used in this case and is allowed to be NULL.
656 * These routines handle persistent and command/reply message state via the
657 * CREATE and DELETE flags. The first message in a command or reply sequence
658 * sets CREATE, the last message in a command or reply sequence sets DELETE.
660 * There can be any number of intermediate messages belonging to the same
661 * sequence sent inbetween the CREATE message and the DELETE message,
662 * which set neither flag. This represents a streaming command or reply.
664 * Any command message received with CREATE set expects a reply sequence to
665 * be returned. Reply sequences work the same as command sequences except the
666 * REPLY bit is also sent. Both the command side and reply side can
667 * degenerate into a single message with both CREATE and DELETE set. Note
668 * that one side can be streaming and the other side not, or neither, or both.
670 * The msgid is unique for the initiator. That is, two sides sending a new
671 * message can use the same msgid without colliding.
675 * ABORT sequences work by setting the ABORT flag along with normal message
676 * state. However, ABORTs can also be sent on half-closed messages, that is
677 * even if the command or reply side has already sent a DELETE, as long as
678 * the message has not been fully closed it can still send an ABORT+DELETE
679 * to terminate the half-closed message state.
681 * Since ABORT+DELETEs can race we silently discard ABORT's for message
682 * state which has already been fully closed. REPLY+ABORT+DELETEs can
683 * also race, and in this situation the other side might have already
684 * initiated a new unrelated command with the same message id. Since
685 * the abort has not set the CREATE flag the situation can be detected
686 * and the message will also be discarded.
688 * Non-blocking requests can be initiated with ABORT+CREATE[+DELETE].
689 * The ABORT request is essentially integrated into the command instead
690 * of being sent later on. In this situation the command implementation
691 * detects that CREATE and ABORT are both set (vs ABORT alone) and can
692 * special-case non-blocking operation for the command.
694 * NOTE! Messages with ABORT set without CREATE or DELETE are considered
695 * to be mid-stream aborts for command/reply sequences. ABORTs on
696 * one-way messages are not supported.
698 * NOTE! If a command sequence does not support aborts the ABORT flag is
703 * One-off messages (no reply expected) are sent with neither CREATE or DELETE
704 * set. One-off messages cannot be aborted and typically aren't processed
705 * by these routines. The REPLY bit can be used to distinguish whether a
706 * one-off message is a command or reply. For example, one-off replies
707 * will typically just contain status updates.
711 kdmsg_state_msgrx(kdmsg_msg_t *msg)
713 kdmsg_iocom_t *iocom = msg->state->iocom;
714 kdmsg_state_t *state;
715 kdmsg_state_t *pstate;
716 kdmsg_state_t sdummy;
720 * Make sure a state structure is ready to go in case we need a new
721 * one. This is the only routine which uses freerd_state so no
722 * races are possible.
724 if ((state = iocom->freerd_state) == NULL) {
725 state = kmalloc(sizeof(*state), iocom->mmsg, M_WAITOK | M_ZERO);
726 state->flags = KDMSG_STATE_DYNAMIC;
727 state->iocom = iocom;
728 TAILQ_INIT(&state->subq);
729 iocom->freerd_state = state;
733 * Lock RB tree and locate existing persistent state, if any.
735 * If received msg is a command state is on staterd_tree.
736 * If received msg is a reply state is on statewr_tree.
738 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
740 sdummy.msgid = msg->any.head.msgid;
741 sdummy.iocom = iocom;
742 if (msg->any.head.cmd & DMSGF_REVTRANS) {
743 state = RB_FIND(kdmsg_state_tree, &iocom->statewr_tree,
746 state = RB_FIND(kdmsg_state_tree, &iocom->staterd_tree,
750 state = &iocom->state0;
754 * Short-cut one-off or mid-stream messages.
756 if ((msg->any.head.cmd & (DMSGF_CREATE | DMSGF_DELETE |
757 DMSGF_ABORT)) == 0) {
763 * Switch on CREATE, DELETE, REPLY, and also handle ABORT from
764 * inside the case statements.
766 switch(msg->any.head.cmd & (DMSGF_CREATE|DMSGF_DELETE|DMSGF_REPLY)) {
768 case DMSGF_CREATE | DMSGF_DELETE:
770 * New persistant command received.
772 if (state != &iocom->state0) {
773 kprintf("kdmsg_state_msgrx: duplicate transaction\n");
779 * Lookup the circuit. The circuit is an open transaction.
780 * the REVCIRC bit in the message tells us which side
781 * initiated the transaction representing the circuit.
783 if (msg->any.head.circuit) {
784 sdummy.msgid = msg->any.head.circuit;
786 if (msg->any.head.cmd & DMSGF_REVCIRC) {
787 pstate = RB_FIND(kdmsg_state_tree,
788 &iocom->statewr_tree,
791 pstate = RB_FIND(kdmsg_state_tree,
792 &iocom->staterd_tree,
795 if (pstate == NULL) {
796 kprintf("kdmsg_state_msgrx: "
797 "missing parent in stacked trans\n");
802 pstate = &iocom->state0;
808 state = iocom->freerd_state;
809 iocom->freerd_state = NULL;
812 state->parent = pstate;
813 KKASSERT(state->iocom == iocom);
814 state->flags |= KDMSG_STATE_INSERTED |
815 KDMSG_STATE_OPPOSITE;
816 state->icmd = msg->any.head.cmd & DMSGF_BASECMDMASK;
817 state->rxcmd = msg->any.head.cmd & ~DMSGF_DELETE;
818 state->txcmd = DMSGF_REPLY;
819 state->msgid = msg->any.head.msgid;
820 RB_INSERT(kdmsg_state_tree, &iocom->staterd_tree, state);
821 TAILQ_INSERT_TAIL(&pstate->subq, state, entry);
826 * Persistent state is expected but might not exist if an
827 * ABORT+DELETE races the close.
829 if (state == &iocom->state0) {
830 if (msg->any.head.cmd & DMSGF_ABORT) {
833 kprintf("kdmsg_state_msgrx: "
834 "no state for DELETE\n");
841 * Handle another ABORT+DELETE case if the msgid has already
844 if ((state->rxcmd & DMSGF_CREATE) == 0) {
845 if (msg->any.head.cmd & DMSGF_ABORT) {
848 kprintf("kdmsg_state_msgrx: "
849 "state reused for DELETE\n");
858 * Check for mid-stream ABORT command received, otherwise
861 if (msg->any.head.cmd & DMSGF_ABORT) {
862 if (state == &iocom->state0 ||
863 (state->rxcmd & DMSGF_CREATE) == 0) {
870 case DMSGF_REPLY | DMSGF_CREATE:
871 case DMSGF_REPLY | DMSGF_CREATE | DMSGF_DELETE:
873 * When receiving a reply with CREATE set the original
874 * persistent state message should already exist.
876 if (state == &iocom->state0) {
877 kprintf("kdmsg_state_msgrx: no state match for "
878 "REPLY cmd=%08x msgid=%016jx\n",
880 (intmax_t)msg->any.head.msgid);
884 state->rxcmd = msg->any.head.cmd & ~DMSGF_DELETE;
887 case DMSGF_REPLY | DMSGF_DELETE:
889 * Received REPLY+ABORT+DELETE in case where msgid has
890 * already been fully closed, ignore the message.
892 if (state == &iocom->state0) {
893 if (msg->any.head.cmd & DMSGF_ABORT) {
896 kprintf("kdmsg_state_msgrx: no state match "
897 "for REPLY|DELETE\n");
904 * Received REPLY+ABORT+DELETE in case where msgid has
905 * already been reused for an unrelated message,
906 * ignore the message.
908 if ((state->rxcmd & DMSGF_CREATE) == 0) {
909 if (msg->any.head.cmd & DMSGF_ABORT) {
912 kprintf("kdmsg_state_msgrx: state reused "
913 "for REPLY|DELETE\n");
922 * Check for mid-stream ABORT reply received to sent command.
924 if (msg->any.head.cmd & DMSGF_ABORT) {
925 if (state == &iocom->state0 ||
926 (state->rxcmd & DMSGF_CREATE) == 0) {
936 * Calculate the easy-switch() transactional command. Represents
937 * the outer-transaction command for any transaction-create or
938 * transaction-delete, and the inner message command for any
939 * non-transaction or inside-transaction command. tcmd will be
940 * set to 0 if the message state is illegal.
942 * The two can be told apart because outer-transaction commands
943 * always have a DMSGF_CREATE and/or DMSGF_DELETE flag.
946 lockmgr(&iocom->msglk, LK_RELEASE);
948 if (msg->any.head.cmd & (DMSGF_CREATE | DMSGF_DELETE)) {
949 if (state != &iocom->state0) {
950 msg->tcmd = (msg->state->icmd & DMSGF_BASECMDMASK) |
951 (msg->any.head.cmd & (DMSGF_CREATE |
958 msg->tcmd = msg->any.head.cmd & DMSGF_CMDSWMASK;
964 * Called instead of iocom->rcvmsg() if any of the AUTO flags are set.
965 * This routine must call iocom->rcvmsg() for anything not automatically
969 kdmsg_autorxmsg(kdmsg_msg_t *msg)
971 kdmsg_iocom_t *iocom = msg->state->iocom;
976 * Main switch processes transaction create/delete sequences only.
977 * Use icmd (DELETEs use DMSG_LNK_ERROR
979 * NOTE: If processing in-transaction messages you generally want
980 * an inner switch on msg->any.head.cmd.
983 cmd = (msg->state->icmd & DMSGF_BASECMDMASK) |
984 (msg->any.head.cmd & (DMSGF_CREATE |
992 case DMSG_LNK_CONN | DMSGF_CREATE:
993 case DMSG_LNK_CONN | DMSGF_CREATE | DMSGF_DELETE:
995 * Received LNK_CONN transaction. Transmit response and
996 * leave transaction open, which allows the other end to
997 * start to the SPAN protocol.
999 * Handle shim after acknowledging the CONN.
1001 if ((msg->any.head.cmd & DMSGF_DELETE) == 0) {
1002 if (iocom->flags & KDMSG_IOCOMF_AUTOCONN) {
1003 kdmsg_msg_result(msg, 0);
1004 if (iocom->auto_callback)
1005 iocom->auto_callback(msg);
1007 error = iocom->rcvmsg(msg);
1012 case DMSG_LNK_CONN | DMSGF_DELETE:
1014 * This message is usually simulated after a link is lost
1015 * to clean up the transaction.
1017 if (iocom->flags & KDMSG_IOCOMF_AUTOCONN) {
1018 if (iocom->auto_callback)
1019 iocom->auto_callback(msg);
1020 kdmsg_msg_reply(msg, 0);
1022 error = iocom->rcvmsg(msg);
1025 case DMSG_LNK_SPAN | DMSGF_CREATE:
1026 case DMSG_LNK_SPAN | DMSGF_CREATE | DMSGF_DELETE:
1028 * Received LNK_SPAN transaction. We do not have to respond
1029 * (except on termination), but we must leave the transaction
1032 * Handle shim after acknowledging the SPAN.
1034 if (iocom->flags & KDMSG_IOCOMF_AUTORXSPAN) {
1035 if ((msg->any.head.cmd & DMSGF_DELETE) == 0) {
1036 if (iocom->auto_callback)
1037 iocom->auto_callback(msg);
1042 error = iocom->rcvmsg(msg);
1046 case DMSG_LNK_SPAN | DMSGF_DELETE:
1048 * Process shims (auto_callback) before cleaning up the
1049 * circuit structure and closing the transactions. Device
1050 * driver should ensure that the circuit is not used after
1051 * the auto_callback() returns.
1053 * Handle shim before closing the SPAN transaction.
1055 if (iocom->flags & KDMSG_IOCOMF_AUTORXSPAN) {
1056 if (iocom->auto_callback)
1057 iocom->auto_callback(msg);
1058 kdmsg_msg_reply(msg, 0);
1060 error = iocom->rcvmsg(msg);
1065 * Anything unhandled goes into rcvmsg.
1067 * NOTE: Replies to link-level messages initiated by our side
1068 * are handled by the state callback, they are NOT
1071 error = iocom->rcvmsg(msg);
1078 * Post-receive-handling message and state cleanup. This routine is called
1079 * after the state function handling/callback to properly dispose of the
1080 * message and update or dispose of the state.
1084 kdmsg_state_cleanuprx(kdmsg_msg_t *msg)
1086 kdmsg_iocom_t *iocom = msg->state->iocom;
1087 kdmsg_state_t *state;
1088 kdmsg_state_t *pstate;
1090 if ((state = msg->state) == NULL) {
1091 kdmsg_msg_free(msg);
1092 } else if (msg->any.head.cmd & DMSGF_DELETE) {
1093 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
1094 KKASSERT((state->rxcmd & DMSGF_DELETE) == 0);
1095 state->rxcmd |= DMSGF_DELETE;
1096 if (state->txcmd & DMSGF_DELETE) {
1097 KKASSERT(state->flags & KDMSG_STATE_INSERTED);
1098 if (state->rxcmd & DMSGF_REPLY) {
1099 KKASSERT(msg->any.head.cmd &
1101 RB_REMOVE(kdmsg_state_tree,
1102 &iocom->statewr_tree, state);
1104 KKASSERT((msg->any.head.cmd &
1106 RB_REMOVE(kdmsg_state_tree,
1107 &iocom->staterd_tree, state);
1109 pstate = state->parent;
1110 TAILQ_REMOVE(&pstate->subq, state, entry);
1111 if (pstate != &pstate->iocom->state0 &&
1112 TAILQ_EMPTY(&pstate->subq) &&
1113 (pstate->flags & KDMSG_STATE_INSERTED) == 0) {
1114 kdmsg_state_free(pstate);
1116 state->flags &= ~KDMSG_STATE_INSERTED;
1117 state->parent = NULL;
1118 kdmsg_msg_free(msg);
1119 if (TAILQ_EMPTY(&state->subq))
1120 kdmsg_state_free(state);
1121 lockmgr(&iocom->msglk, LK_RELEASE);
1123 kdmsg_msg_free(msg);
1124 lockmgr(&iocom->msglk, LK_RELEASE);
1127 kdmsg_msg_free(msg);
1132 * Simulate receiving a message which terminates an active transaction
1133 * state. Our simulated received message must set DELETE and may also
1134 * have to set CREATE. It must also ensure that all fields are set such
1135 * that the receive handling code can find the state (kdmsg_state_msgrx())
1136 * or an endless loop will ensue.
1138 * This is used when the other end of the link is dead so the device driver
1139 * gets a completed transaction for all pending states.
1143 kdmsg_state_abort(kdmsg_state_t *state)
1148 * Prevent recursive aborts which could otherwise occur if the
1149 * simulated message reception runs state->func which then turns
1150 * around and tries to reply to a broken circuit when then calls
1151 * the state abort code again.
1153 if (state->flags & KDMSG_STATE_ABORTING)
1155 state->flags |= KDMSG_STATE_ABORTING;
1158 * NOTE: Args to kdmsg_msg_alloc() to avoid dynamic state allocation.
1160 * NOTE: We are simulating a received message using our state
1161 * (vs a message generated by the other side using its state),
1162 * so we must invert DMSGF_REVTRANS and DMSGF_REVCIRC.
1164 msg = kdmsg_msg_alloc(state, DMSG_LNK_ERROR, NULL, NULL);
1165 if ((state->rxcmd & DMSGF_CREATE) == 0)
1166 msg->any.head.cmd |= DMSGF_CREATE;
1167 msg->any.head.cmd |= DMSGF_DELETE | (state->rxcmd & DMSGF_REPLY);
1168 msg->any.head.cmd ^= (DMSGF_REVTRANS | DMSGF_REVCIRC);
1169 msg->any.head.error = DMSG_ERR_LOSTLINK;
1170 kdmsg_msg_receive_handling(msg);
1174 * Process state tracking for a message prior to transmission.
1176 * Called with msglk held and the msg dequeued. Returns non-zero if
1177 * the message is bad and should be deleted by the caller.
1179 * One-off messages are usually with dummy state and msg->state may be NULL
1180 * in this situation.
1182 * New transactions (when CREATE is set) will insert the state.
1184 * May request that caller discard the message by setting *discardp to 1.
1185 * A NULL state may be returned in this case.
1189 kdmsg_state_msgtx(kdmsg_msg_t *msg)
1191 kdmsg_iocom_t *iocom = msg->state->iocom;
1192 kdmsg_state_t *state;
1196 * Make sure a state structure is ready to go in case we need a new
1197 * one. This is the only routine which uses freewr_state so no
1198 * races are possible.
1200 if ((state = iocom->freewr_state) == NULL) {
1201 state = kmalloc(sizeof(*state), iocom->mmsg, M_WAITOK | M_ZERO);
1202 state->flags = KDMSG_STATE_DYNAMIC;
1203 state->iocom = iocom;
1204 iocom->freewr_state = state;
1208 * Lock RB tree. If persistent state is present it will have already
1209 * been assigned to msg.
1211 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
1215 * Short-cut one-off or mid-stream messages (state may be NULL).
1217 if ((msg->any.head.cmd & (DMSGF_CREATE | DMSGF_DELETE |
1218 DMSGF_ABORT)) == 0) {
1219 lockmgr(&iocom->msglk, LK_RELEASE);
1225 * Switch on CREATE, DELETE, REPLY, and also handle ABORT from
1226 * inside the case statements.
1228 switch(msg->any.head.cmd & (DMSGF_CREATE | DMSGF_DELETE |
1231 case DMSGF_CREATE | DMSGF_DELETE:
1233 * Insert the new persistent message state and mark
1234 * half-closed if DELETE is set. Since this is a new
1235 * message it isn't possible to transition into the fully
1236 * closed state here.
1238 * XXX state must be assigned and inserted by
1239 * kdmsg_msg_write(). txcmd is assigned by us
1242 KKASSERT(state != NULL);
1243 state->icmd = msg->any.head.cmd & DMSGF_BASECMDMASK;
1244 state->txcmd = msg->any.head.cmd & ~DMSGF_DELETE;
1245 state->rxcmd = DMSGF_REPLY;
1250 * Sent ABORT+DELETE in case where msgid has already
1251 * been fully closed, ignore the message.
1253 if (state == &iocom->state0) {
1254 if (msg->any.head.cmd & DMSGF_ABORT) {
1257 kprintf("kdmsg_state_msgtx: no state match "
1258 "for DELETE cmd=%08x msgid=%016jx\n",
1260 (intmax_t)msg->any.head.msgid);
1267 * Sent ABORT+DELETE in case where msgid has
1268 * already been reused for an unrelated message,
1269 * ignore the message.
1271 if ((state->txcmd & DMSGF_CREATE) == 0) {
1272 if (msg->any.head.cmd & DMSGF_ABORT) {
1275 kprintf("kdmsg_state_msgtx: state reused "
1285 * Check for mid-stream ABORT command sent
1287 if (msg->any.head.cmd & DMSGF_ABORT) {
1288 if (state == &state->iocom->state0 ||
1289 (state->txcmd & DMSGF_CREATE) == 0) {
1296 case DMSGF_REPLY | DMSGF_CREATE:
1297 case DMSGF_REPLY | DMSGF_CREATE | DMSGF_DELETE:
1299 * When transmitting a reply with CREATE set the original
1300 * persistent state message should already exist.
1302 if (state == &state->iocom->state0) {
1303 kprintf("kdmsg_state_msgtx: no state match "
1304 "for REPLY | CREATE\n");
1308 state->txcmd = msg->any.head.cmd & ~DMSGF_DELETE;
1311 case DMSGF_REPLY | DMSGF_DELETE:
1313 * When transmitting a reply with DELETE set the original
1314 * persistent state message should already exist.
1316 * This is very similar to the REPLY|CREATE|* case except
1317 * txcmd is already stored, so we just add the DELETE flag.
1319 * Sent REPLY+ABORT+DELETE in case where msgid has
1320 * already been fully closed, ignore the message.
1322 if (state == &state->iocom->state0) {
1323 if (msg->any.head.cmd & DMSGF_ABORT) {
1326 kprintf("kdmsg_state_msgtx: no state match "
1327 "for REPLY | DELETE\n");
1334 * Sent REPLY+ABORT+DELETE in case where msgid has already
1335 * been reused for an unrelated message, ignore the message.
1337 if ((state->txcmd & DMSGF_CREATE) == 0) {
1338 if (msg->any.head.cmd & DMSGF_ABORT) {
1341 kprintf("kdmsg_state_msgtx: state reused "
1342 "for REPLY | DELETE\n");
1351 * Check for mid-stream ABORT reply sent.
1353 * One-off REPLY messages are allowed for e.g. status updates.
1355 if (msg->any.head.cmd & DMSGF_ABORT) {
1356 if (state == &state->iocom->state0 ||
1357 (state->txcmd & DMSGF_CREATE) == 0) {
1365 lockmgr(&iocom->msglk, LK_RELEASE);
1371 kdmsg_state_cleanuptx(kdmsg_msg_t *msg)
1373 kdmsg_iocom_t *iocom = msg->state->iocom;
1374 kdmsg_state_t *state;
1375 kdmsg_state_t *pstate;
1377 if ((state = msg->state) == NULL) {
1378 kdmsg_msg_free(msg);
1379 } else if (msg->any.head.cmd & DMSGF_DELETE) {
1380 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
1381 KKASSERT((state->txcmd & DMSGF_DELETE) == 0);
1382 state->txcmd |= DMSGF_DELETE;
1383 if (state->rxcmd & DMSGF_DELETE) {
1384 KKASSERT(state->flags & KDMSG_STATE_INSERTED);
1385 if (state->txcmd & DMSGF_REPLY) {
1386 KKASSERT(msg->any.head.cmd &
1388 RB_REMOVE(kdmsg_state_tree,
1389 &iocom->staterd_tree, state);
1391 KKASSERT((msg->any.head.cmd &
1393 RB_REMOVE(kdmsg_state_tree,
1394 &iocom->statewr_tree, state);
1396 pstate = state->parent;
1397 TAILQ_REMOVE(&pstate->subq, state, entry);
1398 if (pstate != &pstate->iocom->state0 &&
1399 TAILQ_EMPTY(&pstate->subq) &&
1400 (pstate->flags & KDMSG_STATE_INSERTED) == 0) {
1401 kdmsg_state_free(pstate);
1403 state->flags &= ~KDMSG_STATE_INSERTED;
1404 state->parent = NULL;
1405 kdmsg_msg_free(msg);
1406 if (TAILQ_EMPTY(&state->subq))
1407 kdmsg_state_free(state);
1408 lockmgr(&iocom->msglk, LK_RELEASE);
1410 kdmsg_msg_free(msg);
1411 lockmgr(&iocom->msglk, LK_RELEASE);
1414 kdmsg_msg_free(msg);
1420 kdmsg_state_free(kdmsg_state_t *state)
1422 kdmsg_iocom_t *iocom = state->iocom;
1424 KKASSERT((state->flags & KDMSG_STATE_INSERTED) == 0);
1425 kfree(state, iocom->mmsg);
1429 kdmsg_msg_alloc(kdmsg_state_t *state, uint32_t cmd,
1430 int (*func)(kdmsg_state_t *, kdmsg_msg_t *), void *data)
1432 kdmsg_iocom_t *iocom = state->iocom;
1433 kdmsg_state_t *pstate;
1437 KKASSERT(iocom != NULL);
1438 hbytes = (cmd & DMSGF_SIZE) * DMSG_ALIGN;
1439 msg = kmalloc(offsetof(struct kdmsg_msg, any) + hbytes,
1440 iocom->mmsg, M_WAITOK | M_ZERO);
1441 msg->hdr_size = hbytes;
1443 if ((cmd & (DMSGF_CREATE | DMSGF_REPLY)) == DMSGF_CREATE) {
1445 * New transaction, requires tracking state and a unique
1446 * msgid to be allocated.
1449 state = kmalloc(sizeof(*state), iocom->mmsg, M_WAITOK | M_ZERO);
1450 TAILQ_INIT(&state->subq);
1451 state->iocom = iocom;
1452 state->parent = pstate;
1453 state->flags = KDMSG_STATE_DYNAMIC;
1455 state->any.any = data;
1456 state->msgid = (uint64_t)(uintptr_t)state;
1457 /*msg->any.head.msgid = state->msgid;XXX*/
1459 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
1460 if (RB_INSERT(kdmsg_state_tree, &iocom->statewr_tree, state))
1461 panic("duplicate msgid allocated");
1462 TAILQ_INSERT_TAIL(&pstate->subq, state, entry);
1463 state->flags |= KDMSG_STATE_INSERTED;
1464 lockmgr(&iocom->msglk, LK_RELEASE);
1466 pstate = state->parent;
1469 if (state->flags & KDMSG_STATE_OPPOSITE)
1470 cmd |= DMSGF_REVTRANS;
1471 if (pstate->flags & KDMSG_STATE_OPPOSITE)
1472 cmd |= DMSGF_REVCIRC;
1474 msg->any.head.magic = DMSG_HDR_MAGIC;
1475 msg->any.head.cmd = cmd;
1476 msg->any.head.msgid = state->msgid;
1477 msg->any.head.circuit = pstate->msgid;
1484 kdmsg_msg_free(kdmsg_msg_t *msg)
1486 kdmsg_iocom_t *iocom = msg->state->iocom;
1488 if ((msg->flags & KDMSG_FLAG_AUXALLOC) &&
1489 msg->aux_data && msg->aux_size) {
1490 kfree(msg->aux_data, iocom->mmsg);
1491 msg->flags &= ~KDMSG_FLAG_AUXALLOC;
1494 msg->aux_data = NULL;
1497 kfree(msg, iocom->mmsg);
1501 * Indexed messages are stored in a red-black tree indexed by their
1502 * msgid. Only persistent messages are indexed.
1505 kdmsg_state_cmp(kdmsg_state_t *state1, kdmsg_state_t *state2)
1507 if (state1->iocom < state2->iocom)
1509 if (state1->iocom > state2->iocom)
1511 if (state1->msgid < state2->msgid)
1513 if (state1->msgid > state2->msgid)
1519 * Write a message. All requisit command flags have been set.
1521 * If msg->state is non-NULL the message is written to the existing
1522 * transaction. msgid will be set accordingly.
1524 * If msg->state is NULL and CREATE is set new state is allocated and
1525 * (func, data) is installed. A msgid is assigned.
1527 * If msg->state is NULL and CREATE is not set the message is assumed
1528 * to be a one-way message. The originator must assign the msgid
1529 * (or leave it 0, which is typical.
1531 * This function merely queues the message to the management thread, it
1532 * does not write to the message socket/pipe.
1535 kdmsg_msg_write(kdmsg_msg_t *msg)
1537 kdmsg_iocom_t *iocom = msg->state->iocom;
1538 kdmsg_state_t *state;
1542 * Continuance or termination of existing transaction.
1543 * The transaction could have been initiated by either end.
1545 * (Function callback and aux data for the receive side can
1546 * be replaced or left alone).
1549 msg->any.head.msgid = state->msgid;
1550 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
1553 * One-off message (always uses msgid 0 to distinguish
1554 * between a possibly lost in-transaction message due to
1555 * competing aborts and a real one-off message?)
1558 msg->any.head.msgid = 0;
1559 lockmgr(&iocom->msglk, LK_EXCLUSIVE);
1563 * This flag is not set until after the tx thread has drained
1564 * the txmsgq and simulated responses. After that point the
1565 * txthread is dead and can no longer simulate responses.
1567 * Device drivers should never try to send a message once this
1568 * flag is set. They should have detected (through the state
1569 * closures) that the link is in trouble.
1571 if (iocom->flags & KDMSG_IOCOMF_EXITNOACC) {
1572 lockmgr(&iocom->msglk, LK_RELEASE);
1573 panic("kdmsg_msg_write: Attempt to write message to "
1574 "terminated iocom\n");
1578 * Finish up the msg fields. Note that msg->aux_size and the
1579 * aux_bytes stored in the message header represent the unaligned
1580 * (actual) bytes of data, but the buffer is sized to an aligned
1581 * size and the CRC is generated over the aligned length.
1583 msg->any.head.salt = /* (random << 8) | */ (iocom->msg_seq & 255);
1586 if (msg->aux_data && msg->aux_size) {
1587 uint32_t abytes = DMSG_DOALIGN(msg->aux_size);
1589 msg->any.head.aux_bytes = msg->aux_size;
1590 msg->any.head.aux_crc = iscsi_crc32(msg->aux_data, abytes);
1592 msg->any.head.hdr_crc = 0;
1593 msg->any.head.hdr_crc = iscsi_crc32(msg->any.buf, msg->hdr_size);
1595 TAILQ_INSERT_TAIL(&iocom->msgq, msg, qentry);
1597 if (iocom->msg_ctl & KDMSG_CLUSTERCTL_SLEEPING) {
1598 atomic_clear_int(&iocom->msg_ctl,
1599 KDMSG_CLUSTERCTL_SLEEPING);
1600 wakeup(&iocom->msg_ctl);
1603 lockmgr(&iocom->msglk, LK_RELEASE);
1607 * Reply to a message and terminate our side of the transaction.
1609 * If msg->state is non-NULL we are replying to a one-way message.
1612 kdmsg_msg_reply(kdmsg_msg_t *msg, uint32_t error)
1614 kdmsg_state_t *state = msg->state;
1619 * Reply with a simple error code and terminate the transaction.
1621 cmd = DMSG_LNK_ERROR;
1624 * Check if our direction has even been initiated yet, set CREATE.
1626 * Check what direction this is (command or reply direction). Note
1627 * that txcmd might not have been initiated yet.
1629 * If our direction has already been closed we just return without
1632 if (state != &state->iocom->state0) {
1633 if (state->txcmd & DMSGF_DELETE)
1635 if ((state->txcmd & DMSGF_CREATE) == 0)
1636 cmd |= DMSGF_CREATE;
1637 if (state->txcmd & DMSGF_REPLY)
1639 cmd |= DMSGF_DELETE;
1641 if ((msg->any.head.cmd & DMSGF_REPLY) == 0)
1645 nmsg = kdmsg_msg_alloc(state, cmd, NULL, NULL);
1646 nmsg->any.head.error = error;
1647 kdmsg_msg_write(nmsg);
1651 * Reply to a message and continue our side of the transaction.
1653 * If msg->state is non-NULL we are replying to a one-way message and this
1654 * function degenerates into the same as kdmsg_msg_reply().
1657 kdmsg_msg_result(kdmsg_msg_t *msg, uint32_t error)
1659 kdmsg_state_t *state = msg->state;
1664 * Return a simple result code, do NOT terminate the transaction.
1666 cmd = DMSG_LNK_ERROR;
1669 * Check if our direction has even been initiated yet, set CREATE.
1671 * Check what direction this is (command or reply direction). Note
1672 * that txcmd might not have been initiated yet.
1674 * If our direction has already been closed we just return without
1677 if (state != &state->iocom->state0) {
1678 if (state->txcmd & DMSGF_DELETE)
1680 if ((state->txcmd & DMSGF_CREATE) == 0)
1681 cmd |= DMSGF_CREATE;
1682 if (state->txcmd & DMSGF_REPLY)
1684 /* continuing transaction, do not set MSGF_DELETE */
1686 if ((msg->any.head.cmd & DMSGF_REPLY) == 0)
1690 nmsg = kdmsg_msg_alloc(state, cmd, NULL, NULL);
1691 nmsg->any.head.error = error;
1692 kdmsg_msg_write(nmsg);
1696 * Reply to a message and terminate our side of the transaction.
1698 * If msg->state is non-NULL we are replying to a one-way message.
1701 kdmsg_state_reply(kdmsg_state_t *state, uint32_t error)
1707 * Reply with a simple error code and terminate the transaction.
1709 cmd = DMSG_LNK_ERROR;
1712 * Check if our direction has even been initiated yet, set CREATE.
1714 * Check what direction this is (command or reply direction). Note
1715 * that txcmd might not have been initiated yet.
1717 * If our direction has already been closed we just return without
1721 if (state->txcmd & DMSGF_DELETE)
1723 if ((state->txcmd & DMSGF_CREATE) == 0)
1724 cmd |= DMSGF_CREATE;
1725 if (state->txcmd & DMSGF_REPLY)
1727 cmd |= DMSGF_DELETE;
1729 nmsg = kdmsg_msg_alloc(state, cmd, NULL, NULL);
1730 nmsg->any.head.error = error;
1731 kdmsg_msg_write(nmsg);
1735 * Reply to a message and continue our side of the transaction.
1737 * If msg->state is non-NULL we are replying to a one-way message and this
1738 * function degenerates into the same as kdmsg_msg_reply().
1741 kdmsg_state_result(kdmsg_state_t *state, uint32_t error)
1747 * Return a simple result code, do NOT terminate the transaction.
1749 cmd = DMSG_LNK_ERROR;
1752 * Check if our direction has even been initiated yet, set CREATE.
1754 * Check what direction this is (command or reply direction). Note
1755 * that txcmd might not have been initiated yet.
1757 * If our direction has already been closed we just return without
1761 if (state->txcmd & DMSGF_DELETE)
1763 if ((state->txcmd & DMSGF_CREATE) == 0)
1764 cmd |= DMSGF_CREATE;
1765 if (state->txcmd & DMSGF_REPLY)
1767 /* continuing transaction, do not set MSGF_DELETE */
1769 nmsg = kdmsg_msg_alloc(state, cmd, NULL, NULL);
1770 nmsg->any.head.error = error;
1771 kdmsg_msg_write(nmsg);