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
38 * Process state tracking for a message after reception, prior to
41 * Called with msglk held and the msg dequeued.
43 * All messages are called with dummy state and return actual state.
44 * (One-off messages often just return the same dummy state).
46 * May request that caller discard the message by setting *discardp to 1.
47 * The returned state is not used in this case and is allowed to be NULL.
51 * These routines handle persistent and command/reply message state via the
52 * CREATE and DELETE flags. The first message in a command or reply sequence
53 * sets CREATE, the last message in a command or reply sequence sets DELETE.
55 * There can be any number of intermediate messages belonging to the same
56 * sequence sent inbetween the CREATE message and the DELETE message,
57 * which set neither flag. This represents a streaming command or reply.
59 * Any command message received with CREATE set expects a reply sequence to
60 * be returned. Reply sequences work the same as command sequences except the
61 * REPLY bit is also sent. Both the command side and reply side can
62 * degenerate into a single message with both CREATE and DELETE set. Note
63 * that one side can be streaming and the other side not, or neither, or both.
65 * The msgid is unique for the initiator. That is, two sides sending a new
66 * message can use the same msgid without colliding.
70 * ABORT sequences work by setting the ABORT flag along with normal message
71 * state. However, ABORTs can also be sent on half-closed messages, that is
72 * even if the command or reply side has already sent a DELETE, as long as
73 * the message has not been fully closed it can still send an ABORT+DELETE
74 * to terminate the half-closed message state.
76 * Since ABORT+DELETEs can race we silently discard ABORT's for message
77 * state which has already been fully closed. REPLY+ABORT+DELETEs can
78 * also race, and in this situation the other side might have already
79 * initiated a new unrelated command with the same message id. Since
80 * the abort has not set the CREATE flag the situation can be detected
81 * and the message will also be discarded.
83 * Non-blocking requests can be initiated with ABORT+CREATE[+DELETE].
84 * The ABORT request is essentially integrated into the command instead
85 * of being sent later on. In this situation the command implementation
86 * detects that CREATE and ABORT are both set (vs ABORT alone) and can
87 * special-case non-blocking operation for the command.
89 * NOTE! Messages with ABORT set without CREATE or DELETE are considered
90 * to be mid-stream aborts for command/reply sequences. ABORTs on
91 * one-way messages are not supported.
93 * NOTE! If a command sequence does not support aborts the ABORT flag is
98 * One-off messages (no reply expected) are sent with neither CREATE or DELETE
99 * set. One-off messages cannot be aborted and typically aren't processed
100 * by these routines. The REPLY bit can be used to distinguish whether a
101 * one-off message is a command or reply. For example, one-off replies
102 * will typically just contain status updates.
105 hammer2_state_msgrx(hammer2_pfsmount_t *pmp, hammer2_msg_t *msg)
107 hammer2_state_t *state;
111 * Make sure a state structure is ready to go in case we need a new
112 * one. This is the only routine which uses freerd_state so no
113 * races are possible.
115 if ((state = pmp->freerd_state) == NULL) {
116 state = kmalloc(sizeof(*state), pmp->mmsg, M_WAITOK | M_ZERO);
118 state->flags = HAMMER2_STATE_DYNAMIC;
119 pmp->freerd_state = state;
123 * Lock RB tree and locate existing persistent state, if any.
125 * If received msg is a command state is on staterd_tree.
126 * If received msg is a reply state is on statewr_tree.
128 lockmgr(&pmp->msglk, LK_EXCLUSIVE);
130 state->msgid = msg->any.head.msgid;
131 if (msg->any.head.cmd & HAMMER2_MSGF_REPLY)
132 state = RB_FIND(hammer2_state_tree, &pmp->statewr_tree, state);
134 state = RB_FIND(hammer2_state_tree, &pmp->staterd_tree, state);
138 * Short-cut one-off or mid-stream messages (state may be NULL).
140 if ((msg->any.head.cmd & (HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE |
141 HAMMER2_MSGF_ABORT)) == 0) {
142 lockmgr(&pmp->msglk, LK_RELEASE);
147 * Switch on CREATE, DELETE, REPLY, and also handle ABORT from
148 * inside the case statements.
150 switch(msg->any.head.cmd & (HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE |
151 HAMMER2_MSGF_REPLY)) {
152 case HAMMER2_MSGF_CREATE:
153 case HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE:
155 * New persistant command received.
158 kprintf("hammer2_state_msgrx: duplicate transaction\n");
162 state = pmp->freerd_state;
163 pmp->freerd_state = NULL;
166 state->rxcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
167 RB_INSERT(hammer2_state_tree, &pmp->staterd_tree, state);
168 state->flags |= HAMMER2_STATE_INSERTED;
171 case HAMMER2_MSGF_DELETE:
173 * Persistent state is expected but might not exist if an
174 * ABORT+DELETE races the close.
177 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
180 kprintf("hammer2_state_msgrx: no state "
188 * Handle another ABORT+DELETE case if the msgid has already
191 if ((state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
192 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
195 kprintf("hammer2_state_msgrx: state reused "
205 * Check for mid-stream ABORT command received, otherwise
208 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
210 (state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
217 case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_CREATE:
218 case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE:
220 * When receiving a reply with CREATE set the original
221 * persistent state message should already exist.
224 kprintf("hammer2_state_msgrx: no state match for "
229 state->rxcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
232 case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_DELETE:
234 * Received REPLY+ABORT+DELETE in case where msgid has
235 * already been fully closed, ignore the message.
238 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
241 kprintf("hammer2_state_msgrx: no state match "
242 "for REPLY|DELETE\n");
249 * Received REPLY+ABORT+DELETE in case where msgid has
250 * already been reused for an unrelated message,
251 * ignore the message.
253 if ((state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
254 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
257 kprintf("hammer2_state_msgrx: state reused "
258 "for REPLY|DELETE\n");
265 case HAMMER2_MSGF_REPLY:
267 * Check for mid-stream ABORT reply received to sent command.
269 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
271 (state->rxcmd & HAMMER2_MSGF_CREATE) == 0) {
279 lockmgr(&pmp->msglk, LK_RELEASE);
284 hammer2_state_cleanuprx(hammer2_pfsmount_t *pmp, hammer2_msg_t *msg)
286 hammer2_state_t *state;
288 if ((state = msg->state) == NULL) {
289 hammer2_msg_free(pmp, msg);
290 } else if (msg->any.head.cmd & HAMMER2_MSGF_DELETE) {
291 lockmgr(&pmp->msglk, LK_EXCLUSIVE);
292 state->rxcmd |= HAMMER2_MSGF_DELETE;
293 if (state->txcmd & HAMMER2_MSGF_DELETE) {
294 if (state->msg == msg)
296 KKASSERT(state->flags & HAMMER2_STATE_INSERTED);
297 if (msg->any.head.cmd & HAMMER2_MSGF_REPLY) {
298 RB_REMOVE(hammer2_state_tree,
299 &pmp->statewr_tree, state);
301 RB_REMOVE(hammer2_state_tree,
302 &pmp->staterd_tree, state);
304 state->flags &= ~HAMMER2_STATE_INSERTED;
305 lockmgr(&pmp->msglk, LK_RELEASE);
306 hammer2_state_free(state);
308 lockmgr(&pmp->msglk, LK_RELEASE);
310 hammer2_msg_free(pmp, msg);
311 } else if (state->msg != msg) {
312 hammer2_msg_free(pmp, msg);
317 * Process state tracking for a message prior to transmission.
319 * Called with msglk held and the msg dequeued.
321 * One-off messages are usually with dummy state and msg->state may be NULL
324 * New transactions (when CREATE is set) will insert the state.
326 * May request that caller discard the message by setting *discardp to 1.
327 * A NULL state may be returned in this case.
330 hammer2_state_msgtx(hammer2_pfsmount_t *pmp, hammer2_msg_t *msg)
332 hammer2_state_t *state;
336 * Make sure a state structure is ready to go in case we need a new
337 * one. This is the only routine which uses freewr_state so no
338 * races are possible.
340 if ((state = pmp->freewr_state) == NULL) {
341 state = kmalloc(sizeof(*state), pmp->mmsg, M_WAITOK | M_ZERO);
343 state->flags = HAMMER2_STATE_DYNAMIC;
344 pmp->freewr_state = state;
348 * Lock RB tree. If persistent state is present it will have already
349 * been assigned to msg.
351 lockmgr(&pmp->msglk, LK_EXCLUSIVE);
355 * Short-cut one-off or mid-stream messages (state may be NULL).
357 if ((msg->any.head.cmd & (HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE |
358 HAMMER2_MSGF_ABORT)) == 0) {
359 lockmgr(&pmp->msglk, LK_RELEASE);
365 * Switch on CREATE, DELETE, REPLY, and also handle ABORT from
366 * inside the case statements.
368 switch(msg->any.head.cmd & (HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE |
369 HAMMER2_MSGF_REPLY)) {
370 case HAMMER2_MSGF_CREATE:
371 case HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE:
373 * Insert the new persistent message state and mark
374 * half-closed if DELETE is set. Since this is a new
375 * message it isn't possible to transition into the fully
379 state = pmp->freerd_state;
380 pmp->freerd_state = NULL;
384 KKASSERT((state->flags & HAMMER2_STATE_INSERTED) == 0);
385 if (RB_INSERT(hammer2_state_tree, &pmp->staterd_tree, state)) {
386 kprintf("hammer2_state_msgtx: duplicate transaction\n");
390 state->flags |= HAMMER2_STATE_INSERTED;
391 state->txcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
394 case HAMMER2_MSGF_DELETE:
396 * Sent ABORT+DELETE in case where msgid has already
397 * been fully closed, ignore the message.
400 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
403 kprintf("hammer2_state_msgtx: no state match "
411 * Sent ABORT+DELETE in case where msgid has
412 * already been reused for an unrelated message,
413 * ignore the message.
415 if ((state->txcmd & HAMMER2_MSGF_CREATE) == 0) {
416 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
419 kprintf("hammer2_state_msgtx: state reused "
429 * Check for mid-stream ABORT command sent
431 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
433 (state->txcmd & HAMMER2_MSGF_CREATE) == 0) {
440 case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_CREATE:
441 case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_CREATE | HAMMER2_MSGF_DELETE:
443 * When transmitting a reply with CREATE set the original
444 * persistent state message should already exist.
447 kprintf("hammer2_state_msgtx: no state match "
448 "for REPLY | CREATE\n");
452 state->txcmd = msg->any.head.cmd & ~HAMMER2_MSGF_DELETE;
455 case HAMMER2_MSGF_REPLY | HAMMER2_MSGF_DELETE:
457 * When transmitting a reply with DELETE set the original
458 * persistent state message should already exist.
460 * This is very similar to the REPLY|CREATE|* case except
461 * txcmd is already stored, so we just add the DELETE flag.
463 * Sent REPLY+ABORT+DELETE in case where msgid has
464 * already been fully closed, ignore the message.
467 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
470 kprintf("hammer2_state_msgtx: no state match "
471 "for REPLY | DELETE\n");
478 * Sent REPLY+ABORT+DELETE in case where msgid has already
479 * been reused for an unrelated message, ignore the message.
481 if ((state->txcmd & HAMMER2_MSGF_CREATE) == 0) {
482 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
485 kprintf("hammer2_state_msgtx: state reused "
486 "for REPLY | DELETE\n");
493 case HAMMER2_MSGF_REPLY:
495 * Check for mid-stream ABORT reply sent.
497 * One-off REPLY messages are allowed for e.g. status updates.
499 if (msg->any.head.cmd & HAMMER2_MSGF_ABORT) {
501 (state->txcmd & HAMMER2_MSGF_CREATE) == 0) {
509 lockmgr(&pmp->msglk, LK_RELEASE);
514 hammer2_state_cleanuptx(hammer2_pfsmount_t *pmp, hammer2_msg_t *msg)
516 hammer2_state_t *state;
518 if ((state = msg->state) == NULL) {
519 hammer2_msg_free(pmp, msg);
520 } else if (msg->any.head.cmd & HAMMER2_MSGF_DELETE) {
521 lockmgr(&pmp->msglk, LK_EXCLUSIVE);
522 state->txcmd |= HAMMER2_MSGF_DELETE;
523 if (state->rxcmd & HAMMER2_MSGF_DELETE) {
524 if (state->msg == msg)
526 KKASSERT(state->flags & HAMMER2_STATE_INSERTED);
527 if (msg->any.head.cmd & HAMMER2_MSGF_REPLY) {
528 RB_REMOVE(hammer2_state_tree,
529 &pmp->staterd_tree, state);
531 RB_REMOVE(hammer2_state_tree,
532 &pmp->statewr_tree, state);
534 state->flags &= ~HAMMER2_STATE_INSERTED;
535 lockmgr(&pmp->msglk, LK_RELEASE);
536 hammer2_state_free(state);
538 lockmgr(&pmp->msglk, LK_RELEASE);
540 hammer2_msg_free(pmp, msg);
541 } else if (state->msg != msg) {
542 hammer2_msg_free(pmp, msg);
547 hammer2_state_free(hammer2_state_t *state)
549 hammer2_pfsmount_t *pmp = state->pmp;
554 kfree(state, pmp->mmsg);
556 hammer2_msg_free(pmp, msg);
560 hammer2_msg_free(hammer2_pfsmount_t *pmp, hammer2_msg_t *msg)
562 if (msg->aux_data && msg->aux_size) {
563 kfree(msg->aux_data, pmp->mmsg);
564 msg->aux_data = NULL;
567 kfree(msg, pmp->mmsg);
571 * Indexed messages are stored in a red-black tree indexed by their
572 * msgid. Only persistent messages are indexed.
575 hammer2_state_cmp(hammer2_state_t *state1, hammer2_state_t *state2)
577 if (state1->msgid < state2->msgid)
579 if (state1->msgid > state2->msgid)
585 * Execute a received message.
587 * If msg is part of a transaction msg->state will be non-NULL. In this
588 * situation state->msg saves the original command and can be replaced
589 * by ongoing activity if desired. Any msg which does not match state->msg
590 * will be destroyed after we return.
592 * Single-element transactions set both CREATE and DELETE.
594 * Non-blocking transactions also set ABORT with CREATE.
596 * Specific transactional ABORTs can also be received and must be handled,
597 * and can be received even when the other end has closed (after DELETE
598 * received) if we have not yet closed our end of the transaction.
601 hammer2_msg_execute(hammer2_pfsmount_t *pmp __unused, hammer2_msg_t *msg __unused)