1 // SPDX-License-Identifier: GPL-2.0-only
2 /******************************************************************************
3 *******************************************************************************
5 ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
6 ** Copyright (C) 2004-2021 Red Hat, Inc. All rights reserved.
9 *******************************************************************************
10 ******************************************************************************/
15 * This is the appallingly named "mid-level" comms layer. It takes care about
16 * deliver an on application layer "reliable" communication above the used
17 * lowcomms transport layer.
21 * Each nodes keeps track of all send DLM messages in send_queue with a sequence
22 * number. The receive will send an DLM_ACK message back for every DLM message
23 * received at the other side. If a reconnect happens in lowcomms we will send
24 * all unacknowledged dlm messages again. The receiving side might drop any already
25 * received message by comparing sequence numbers.
27 * How version detection works:
29 * Due the fact that dlm has pre-configured node addresses on every side
30 * it is in it's nature that every side connects at starts to transmit
31 * dlm messages which ends in a race. However DLM_RCOM_NAMES, DLM_RCOM_STATUS
32 * and their replies are the first messages which are exchanges. Due backwards
33 * compatibility these messages are not covered by the midcomms re-transmission
34 * layer. These messages have their own re-transmission handling in the dlm
35 * application layer. The version field of every node will be set on these RCOM
36 * messages as soon as they arrived and the node isn't yet part of the nodes
37 * hash. There exists also logic to detect version mismatched if something weird
38 * going on or the first messages isn't an expected one.
42 * The midcomms layer does a 4 way handshake for termination on DLM protocol
43 * like TCP supports it with half-closed socket support. SCTP doesn't support
44 * half-closed socket, so we do it on DLM layer. Also socket shutdown() can be
45 * interrupted by .e.g. tcp reset itself. Additional there exists the othercon
46 * paradigm in lowcomms which cannot be easily without breaking backwards
47 * compatibility. A node cannot send anything to another node when a DLM_FIN
48 * message was send. There exists additional logic to print a warning if
49 * DLM wants to do it. There exists a state handling like RFC 793 but reduced
50 * to termination only. The event "member removal event" describes the cluster
51 * manager removed the node from internal lists, at this point DLM does not
52 * send any message to the other node. There exists two cases:
54 * 1. The cluster member was removed and we received a FIN
56 * 2. We received a FIN but the member was not removed yet
58 * One of these cases will do the CLOSE_WAIT to LAST_ACK change.
64 * | add member/receive RCOM version
72 * +---------+ snd FIN / \ snd ACK +---------+
73 * | FIN |<----------------- ------------------>| CLOSE |
74 * | WAIT-1 |------------------ | WAIT |
75 * +---------+ rcv FIN \ +---------+
76 * | rcv ACK of FIN ------- | CLOSE | member
77 * | -------------- snd ACK | ------- | removal
78 * V x V snd FIN V event
79 * +---------+ +---------+ +---------+
80 * |FINWAIT-2| | CLOSING | | LAST-ACK|
81 * +---------+ +---------+ +---------+
82 * | rcv ACK of FIN | rcv ACK of FIN |
83 * | rcv FIN -------------- | -------------- |
85 * \ snd ACK +---------+ +---------+
86 * ------------------------>| CLOSED | | CLOSED |
87 * +---------+ +---------+
89 * NOTE: any state can interrupted by midcomms_close() and state will be
90 * switched to CLOSED in case of fencing. There exists also some timeout
91 * handling when we receive the version detection RCOM messages which is
92 * made by observation.
94 * Future improvements:
96 * There exists some known issues/improvements of the dlm handling. Some
97 * of them should be done in a next major dlm version bump which makes
98 * it incompatible with previous versions.
100 * Unaligned memory access:
102 * There exists cases when the dlm message buffer length is not aligned
103 * to 8 byte. However seems nobody detected any problem with it. This
104 * can be fixed in the next major version bump of dlm.
108 * The version detection and how it's done is related to backwards
109 * compatibility. There exists better ways to make a better handling.
110 * However this should be changed in the next major version bump of dlm.
114 * Currently we send an ack message for every dlm message. However we
115 * can ack multiple dlm messages with one ack by just delaying the ack
116 * message. Will reduce some traffic but makes the drop detection slower.
118 * Tail Size checking:
120 * There exists a message tail payload in e.g. DLM_MSG however we don't
121 * check it against the message length yet regarding to the receive buffer
122 * length. That need to be validated.
126 * At timeout places or weird sequence number behaviours we should send
127 * a fencing request to the cluster manager.
130 /* Debug switch to enable a 5 seconds sleep waiting of a termination.
131 * This can be useful to test fencing while termination is running.
132 * This requires a setup with only gfs2 as dlm user, so that the
133 * last umount will terminate the connection.
135 * However it became useful to test, while the 5 seconds block in umount
136 * just press the reset button. In a lot of dropping the termination
137 * process can could take several seconds.
139 #define DLM_DEBUG_FENCE_TERMINATION 0
143 #include "dlm_internal.h"
144 #include "lowcomms.h"
148 #include "midcomms.h"
150 /* init value for sequence numbers for testing purpose only e.g. overflows */
151 #define DLM_SEQ_INIT 0
152 /* 3 minutes wait to sync ending of dlm */
153 #define DLM_SHUTDOWN_TIMEOUT msecs_to_jiffies(3 * 60 * 1000)
154 #define DLM_VERSION_NOT_SET 0
156 struct midcomms_node {
161 /* These queues are unbound because we cannot drop any message in dlm.
162 * We could send a fence signal for a specific node to the cluster
163 * manager if queues hits some maximum value, however this handling
166 struct list_head send_queue;
167 spinlock_t send_queue_lock;
168 atomic_t send_queue_cnt;
169 #define DLM_NODE_FLAG_CLOSE 1
170 #define DLM_NODE_FLAG_STOP_TX 2
171 #define DLM_NODE_FLAG_STOP_RX 3
173 wait_queue_head_t shutdown_wait;
175 /* dlm tcp termination state */
177 #define DLM_ESTABLISHED 2
178 #define DLM_FIN_WAIT1 3
179 #define DLM_FIN_WAIT2 4
180 #define DLM_CLOSE_WAIT 5
181 #define DLM_LAST_ACK 6
182 #define DLM_CLOSING 7
184 spinlock_t state_lock;
186 /* counts how many lockspaces are using this node
187 * this refcount is necessary to determine if the
188 * node wants to disconnect.
192 /* not protected by srcu, node_hash lifetime */
195 struct hlist_node hlist;
200 const struct dlm_header *inner_hd;
201 struct midcomms_node *node;
202 struct dlm_opts *opts;
207 void (*ack_rcv)(struct midcomms_node *node);
209 /* get_mhandle/commit srcu idx exchange */
212 struct list_head list;
216 static struct hlist_head node_hash[CONN_HASH_SIZE];
217 static DEFINE_SPINLOCK(nodes_lock);
218 DEFINE_STATIC_SRCU(nodes_srcu);
220 /* This mutex prevents that midcomms_close() is running while
221 * stop() or remove(). As I experienced invalid memory access
222 * behaviours when DLM_DEBUG_FENCE_TERMINATION is enabled and
223 * resetting machines. I will end in some double deletion in nodes
226 static DEFINE_MUTEX(close_lock);
228 static inline const char *dlm_state_str(int state)
233 case DLM_ESTABLISHED:
234 return "ESTABLISHED";
250 const char *dlm_midcomms_state(struct midcomms_node *node)
252 return dlm_state_str(node->state);
255 unsigned long dlm_midcomms_flags(struct midcomms_node *node)
260 int dlm_midcomms_send_queue_cnt(struct midcomms_node *node)
262 return atomic_read(&node->send_queue_cnt);
265 uint32_t dlm_midcomms_version(struct midcomms_node *node)
267 return node->version;
270 static struct midcomms_node *__find_node(int nodeid, int r)
272 struct midcomms_node *node;
274 hlist_for_each_entry_rcu(node, &node_hash[r], hlist) {
275 if (node->nodeid == nodeid)
282 static void dlm_mhandle_release(struct rcu_head *rcu)
284 struct dlm_mhandle *mh = container_of(rcu, struct dlm_mhandle, rcu);
286 dlm_lowcomms_put_msg(mh->msg);
290 static void dlm_mhandle_delete(struct midcomms_node *node,
291 struct dlm_mhandle *mh)
293 list_del_rcu(&mh->list);
294 atomic_dec(&node->send_queue_cnt);
295 call_rcu(&mh->rcu, dlm_mhandle_release);
298 static void dlm_send_queue_flush(struct midcomms_node *node)
300 struct dlm_mhandle *mh;
302 pr_debug("flush midcomms send queue of node %d\n", node->nodeid);
305 spin_lock(&node->send_queue_lock);
306 list_for_each_entry_rcu(mh, &node->send_queue, list) {
307 dlm_mhandle_delete(node, mh);
309 spin_unlock(&node->send_queue_lock);
313 static void midcomms_node_reset(struct midcomms_node *node)
315 pr_debug("reset node %d\n", node->nodeid);
317 node->seq_next = DLM_SEQ_INIT;
318 node->seq_send = DLM_SEQ_INIT;
319 node->version = DLM_VERSION_NOT_SET;
322 dlm_send_queue_flush(node);
323 node->state = DLM_CLOSED;
324 wake_up(&node->shutdown_wait);
327 static struct midcomms_node *nodeid2node(int nodeid, gfp_t alloc)
329 struct midcomms_node *node, *tmp;
330 int r = nodeid_hash(nodeid);
332 node = __find_node(nodeid, r);
336 node = kmalloc(sizeof(*node), alloc);
340 node->nodeid = nodeid;
341 spin_lock_init(&node->state_lock);
342 spin_lock_init(&node->send_queue_lock);
343 atomic_set(&node->send_queue_cnt, 0);
344 INIT_LIST_HEAD(&node->send_queue);
345 init_waitqueue_head(&node->shutdown_wait);
347 midcomms_node_reset(node);
349 spin_lock(&nodes_lock);
350 /* check again if there was somebody else
351 * earlier here to add the node
353 tmp = __find_node(nodeid, r);
355 spin_unlock(&nodes_lock);
360 hlist_add_head_rcu(&node->hlist, &node_hash[r]);
361 spin_unlock(&nodes_lock);
363 node->debugfs = dlm_create_debug_comms_file(nodeid, node);
367 static int dlm_send_ack(int nodeid, uint32_t seq)
369 int mb_len = sizeof(struct dlm_header);
370 struct dlm_header *m_header;
374 msg = dlm_lowcomms_new_msg(nodeid, mb_len, GFP_NOFS, &ppc,
379 m_header = (struct dlm_header *)ppc;
381 m_header->h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
382 m_header->h_nodeid = dlm_our_nodeid();
383 m_header->h_length = mb_len;
384 m_header->h_cmd = DLM_ACK;
385 m_header->u.h_seq = seq;
387 header_out(m_header);
388 dlm_lowcomms_commit_msg(msg);
389 dlm_lowcomms_put_msg(msg);
394 static int dlm_send_fin(struct midcomms_node *node,
395 void (*ack_rcv)(struct midcomms_node *node))
397 int mb_len = sizeof(struct dlm_header);
398 struct dlm_header *m_header;
399 struct dlm_mhandle *mh;
402 mh = dlm_midcomms_get_mhandle(node->nodeid, mb_len, GFP_NOFS, &ppc);
406 mh->ack_rcv = ack_rcv;
408 m_header = (struct dlm_header *)ppc;
410 m_header->h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
411 m_header->h_nodeid = dlm_our_nodeid();
412 m_header->h_length = mb_len;
413 m_header->h_cmd = DLM_FIN;
415 header_out(m_header);
417 pr_debug("sending fin msg to node %d\n", node->nodeid);
418 dlm_midcomms_commit_mhandle(mh);
419 set_bit(DLM_NODE_FLAG_STOP_TX, &node->flags);
424 static void dlm_receive_ack(struct midcomms_node *node, uint32_t seq)
426 struct dlm_mhandle *mh;
429 list_for_each_entry_rcu(mh, &node->send_queue, list) {
430 if (before(mh->seq, seq)) {
434 /* send queue should be ordered */
439 spin_lock(&node->send_queue_lock);
440 list_for_each_entry_rcu(mh, &node->send_queue, list) {
441 if (before(mh->seq, seq)) {
442 dlm_mhandle_delete(node, mh);
444 /* send queue should be ordered */
448 spin_unlock(&node->send_queue_lock);
452 static void dlm_pas_fin_ack_rcv(struct midcomms_node *node)
454 spin_lock(&node->state_lock);
455 pr_debug("receive passive fin ack from node %d with state %s\n",
456 node->nodeid, dlm_state_str(node->state));
458 switch (node->state) {
461 midcomms_node_reset(node);
464 /* not valid but somehow we got what we want */
465 wake_up(&node->shutdown_wait);
468 spin_unlock(&node->state_lock);
469 log_print("%s: unexpected state: %d\n",
470 __func__, node->state);
474 spin_unlock(&node->state_lock);
477 static void dlm_midcomms_receive_buffer(union dlm_packet *p,
478 struct midcomms_node *node,
481 if (seq == node->seq_next) {
483 /* send ack before fin */
484 dlm_send_ack(node->nodeid, node->seq_next);
486 switch (p->header.h_cmd) {
488 spin_lock(&node->state_lock);
489 pr_debug("receive fin msg from node %d with state %s\n",
490 node->nodeid, dlm_state_str(node->state));
492 switch (node->state) {
493 case DLM_ESTABLISHED:
494 node->state = DLM_CLOSE_WAIT;
495 pr_debug("switch node %d to state %s\n",
496 node->nodeid, dlm_state_str(node->state));
497 /* passive shutdown DLM_LAST_ACK case 1
498 * additional we check if the node is used by
499 * cluster manager events at all.
501 if (node->users == 0) {
502 node->state = DLM_LAST_ACK;
503 pr_debug("switch node %d to state %s case 1\n",
504 node->nodeid, dlm_state_str(node->state));
505 spin_unlock(&node->state_lock);
510 node->state = DLM_CLOSING;
511 pr_debug("switch node %d to state %s\n",
512 node->nodeid, dlm_state_str(node->state));
515 midcomms_node_reset(node);
516 pr_debug("switch node %d to state %s\n",
517 node->nodeid, dlm_state_str(node->state));
518 wake_up(&node->shutdown_wait);
521 /* probably remove_member caught it, do nothing */
524 spin_unlock(&node->state_lock);
525 log_print("%s: unexpected state: %d\n",
526 __func__, node->state);
530 spin_unlock(&node->state_lock);
532 set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
535 WARN_ON(test_bit(DLM_NODE_FLAG_STOP_RX, &node->flags));
536 dlm_receive_buffer(p, node->nodeid);
540 /* retry to ack message which we already have by sending back
541 * current node->seq_next number as ack.
543 if (seq < node->seq_next)
544 dlm_send_ack(node->nodeid, node->seq_next);
546 log_print_ratelimited("ignore dlm msg because seq mismatch, seq: %u, expected: %u, nodeid: %d",
547 seq, node->seq_next, node->nodeid);
553 set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
554 dlm_send_fin(node, dlm_pas_fin_ack_rcv);
557 static struct midcomms_node *
558 dlm_midcomms_recv_node_lookup(int nodeid, const union dlm_packet *p,
559 uint16_t msglen, int (*cb)(struct midcomms_node *node))
561 struct midcomms_node *node = NULL;
562 gfp_t allocation = 0;
565 switch (p->header.h_cmd) {
567 if (msglen < sizeof(struct dlm_rcom)) {
568 log_print("rcom msg too small: %u, will skip this message from node %d",
573 switch (le32_to_cpu(p->rcom.rc_type)) {
576 case DLM_RCOM_NAMES_REPLY:
578 case DLM_RCOM_STATUS:
580 case DLM_RCOM_STATUS_REPLY:
581 node = nodeid2node(nodeid, 0);
583 spin_lock(&node->state_lock);
584 if (node->state != DLM_ESTABLISHED)
585 pr_debug("receive begin RCOM msg from node %d with state %s\n",
586 node->nodeid, dlm_state_str(node->state));
588 switch (node->state) {
590 node->state = DLM_ESTABLISHED;
591 pr_debug("switch node %d to state %s\n",
592 node->nodeid, dlm_state_str(node->state));
594 case DLM_ESTABLISHED:
597 /* some invalid state passive shutdown
598 * was failed, we try to reset and
599 * hope it will go on.
601 log_print("reset node %d because shutdown stuck",
604 midcomms_node_reset(node);
605 node->state = DLM_ESTABLISHED;
608 spin_unlock(&node->state_lock);
611 allocation = GFP_NOFS;
622 node = nodeid2node(nodeid, allocation);
624 switch (p->header.h_cmd) {
626 if (msglen < sizeof(struct dlm_opts)) {
627 log_print("opts msg too small: %u, will skip this message from node %d",
632 log_print_ratelimited("received dlm opts message nextcmd %d from node %d in an invalid sequence",
633 p->opts.o_nextcmd, nodeid);
636 log_print_ratelimited("received dlm message cmd %d from node %d in an invalid sequence",
637 p->header.h_cmd, nodeid);
651 static int dlm_midcomms_version_check_3_2(struct midcomms_node *node)
653 switch (node->version) {
654 case DLM_VERSION_NOT_SET:
655 node->version = DLM_VERSION_3_2;
656 log_print("version 0x%08x for node %d detected", DLM_VERSION_3_2,
659 case DLM_VERSION_3_2:
662 log_print_ratelimited("version mismatch detected, assumed 0x%08x but node %d has 0x%08x",
663 DLM_VERSION_3_2, node->nodeid, node->version);
670 static int dlm_opts_check_msglen(union dlm_packet *p, uint16_t msglen, int nodeid)
674 /* we only trust outer header msglen because
675 * it's checked against receive buffer length.
677 if (len < sizeof(struct dlm_opts))
679 len -= sizeof(struct dlm_opts);
681 if (len < le16_to_cpu(p->opts.o_optlen))
683 len -= le16_to_cpu(p->opts.o_optlen);
685 switch (p->opts.o_nextcmd) {
687 if (len < sizeof(struct dlm_header)) {
688 log_print("fin too small: %d, will skip this message from node %d",
695 if (len < sizeof(struct dlm_message)) {
696 log_print("msg too small: %d, will skip this message from node %d",
703 if (len < sizeof(struct dlm_rcom)) {
704 log_print("rcom msg too small: %d, will skip this message from node %d",
711 log_print("unsupported o_nextcmd received: %u, will skip this message from node %d",
712 p->opts.o_nextcmd, nodeid);
719 static void dlm_midcomms_receive_buffer_3_2(union dlm_packet *p, int nodeid)
721 uint16_t msglen = le16_to_cpu(p->header.h_length);
722 struct midcomms_node *node;
726 idx = srcu_read_lock(&nodes_srcu);
727 node = dlm_midcomms_recv_node_lookup(nodeid, p, msglen,
728 dlm_midcomms_version_check_3_2);
732 switch (p->header.h_cmd) {
734 /* these rcom message we use to determine version.
735 * they have their own retransmission handling and
736 * are the first messages of dlm.
738 * length already checked.
740 switch (le32_to_cpu(p->rcom.rc_type)) {
743 case DLM_RCOM_NAMES_REPLY:
745 case DLM_RCOM_STATUS:
747 case DLM_RCOM_STATUS_REPLY:
750 log_print("unsupported rcom type received: %u, will skip this message from node %d",
751 le32_to_cpu(p->rcom.rc_type), nodeid);
755 WARN_ON(test_bit(DLM_NODE_FLAG_STOP_RX, &node->flags));
756 dlm_receive_buffer(p, nodeid);
759 seq = le32_to_cpu(p->header.u.h_seq);
761 ret = dlm_opts_check_msglen(p, msglen, nodeid);
763 log_print("opts msg too small: %u, will skip this message from node %d",
768 p = (union dlm_packet *)((unsigned char *)p->opts.o_opts +
769 le16_to_cpu(p->opts.o_optlen));
771 /* recheck inner msglen just if it's not garbage */
772 msglen = le16_to_cpu(p->header.h_length);
773 switch (p->header.h_cmd) {
775 if (msglen < sizeof(struct dlm_rcom)) {
776 log_print("inner rcom msg too small: %u, will skip this message from node %d",
783 if (msglen < sizeof(struct dlm_message)) {
784 log_print("inner msg too small: %u, will skip this message from node %d",
791 if (msglen < sizeof(struct dlm_header)) {
792 log_print("inner fin too small: %u, will skip this message from node %d",
799 log_print("unsupported inner h_cmd received: %u, will skip this message from node %d",
804 dlm_midcomms_receive_buffer(p, node, seq);
807 seq = le32_to_cpu(p->header.u.h_seq);
808 dlm_receive_ack(node, seq);
811 log_print("unsupported h_cmd received: %u, will skip this message from node %d",
812 p->header.h_cmd, nodeid);
817 srcu_read_unlock(&nodes_srcu, idx);
820 static int dlm_midcomms_version_check_3_1(struct midcomms_node *node)
822 switch (node->version) {
823 case DLM_VERSION_NOT_SET:
824 node->version = DLM_VERSION_3_1;
825 log_print("version 0x%08x for node %d detected", DLM_VERSION_3_1,
828 case DLM_VERSION_3_1:
831 log_print_ratelimited("version mismatch detected, assumed 0x%08x but node %d has 0x%08x",
832 DLM_VERSION_3_1, node->nodeid, node->version);
839 static void dlm_midcomms_receive_buffer_3_1(union dlm_packet *p, int nodeid)
841 uint16_t msglen = le16_to_cpu(p->header.h_length);
842 struct midcomms_node *node;
845 idx = srcu_read_lock(&nodes_srcu);
846 node = dlm_midcomms_recv_node_lookup(nodeid, p, msglen,
847 dlm_midcomms_version_check_3_1);
849 srcu_read_unlock(&nodes_srcu, idx);
852 srcu_read_unlock(&nodes_srcu, idx);
854 switch (p->header.h_cmd) {
856 /* length already checked */
859 if (msglen < sizeof(struct dlm_message)) {
860 log_print("msg too small: %u, will skip this message from node %d",
867 log_print("unsupported h_cmd received: %u, will skip this message from node %d",
868 p->header.h_cmd, nodeid);
872 dlm_receive_buffer(p, nodeid);
876 * Called from the low-level comms layer to process a buffer of
880 int dlm_process_incoming_buffer(int nodeid, unsigned char *buf, int len)
882 const unsigned char *ptr = buf;
883 const struct dlm_header *hd;
887 while (len >= sizeof(struct dlm_header)) {
888 hd = (struct dlm_header *)ptr;
890 /* no message should be more than DLM_MAX_SOCKET_BUFSIZE or
891 * less than dlm_header size.
893 * Some messages does not have a 8 byte length boundary yet
894 * which can occur in a unaligned memory access of some dlm
895 * messages. However this problem need to be fixed at the
896 * sending side, for now it seems nobody run into architecture
897 * related issues yet but it slows down some processing.
898 * Fixing this issue should be scheduled in future by doing
899 * the next major version bump.
901 msglen = le16_to_cpu(hd->h_length);
902 if (msglen > DLM_MAX_SOCKET_BUFSIZE ||
903 msglen < sizeof(struct dlm_header)) {
904 log_print("received invalid length header: %u from node %d, will abort message parsing",
909 /* caller will take care that leftover
910 * will be parsed next call with more data
915 switch (le32_to_cpu(hd->h_version)) {
916 case DLM_VERSION_3_1:
917 dlm_midcomms_receive_buffer_3_1((union dlm_packet *)ptr, nodeid);
919 case DLM_VERSION_3_2:
920 dlm_midcomms_receive_buffer_3_2((union dlm_packet *)ptr, nodeid);
923 log_print("received invalid version header: %u from node %d, will skip this message",
924 le32_to_cpu(hd->h_version), nodeid);
936 void dlm_midcomms_unack_msg_resend(int nodeid)
938 struct midcomms_node *node;
939 struct dlm_mhandle *mh;
942 idx = srcu_read_lock(&nodes_srcu);
943 node = nodeid2node(nodeid, 0);
945 srcu_read_unlock(&nodes_srcu, idx);
949 /* old protocol, we don't support to retransmit on failure */
950 switch (node->version) {
951 case DLM_VERSION_3_2:
954 srcu_read_unlock(&nodes_srcu, idx);
959 list_for_each_entry_rcu(mh, &node->send_queue, list) {
963 ret = dlm_lowcomms_resend_msg(mh->msg);
965 log_print_ratelimited("retransmit dlm msg, seq %u, nodeid %d",
966 mh->seq, node->nodeid);
969 srcu_read_unlock(&nodes_srcu, idx);
972 static void dlm_fill_opts_header(struct dlm_opts *opts, uint16_t inner_len,
975 opts->o_header.h_cmd = DLM_OPTS;
976 opts->o_header.h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
977 opts->o_header.h_nodeid = dlm_our_nodeid();
978 opts->o_header.h_length = DLM_MIDCOMMS_OPT_LEN + inner_len;
979 opts->o_header.u.h_seq = seq;
980 header_out(&opts->o_header);
983 static void midcomms_new_msg_cb(struct dlm_mhandle *mh)
985 atomic_inc(&mh->node->send_queue_cnt);
987 spin_lock(&mh->node->send_queue_lock);
988 list_add_tail_rcu(&mh->list, &mh->node->send_queue);
989 spin_unlock(&mh->node->send_queue_lock);
991 mh->seq = mh->node->seq_send++;
994 static struct dlm_msg *dlm_midcomms_get_msg_3_2(struct dlm_mhandle *mh, int nodeid,
995 int len, gfp_t allocation, char **ppc)
997 struct dlm_opts *opts;
1000 msg = dlm_lowcomms_new_msg(nodeid, len + DLM_MIDCOMMS_OPT_LEN,
1001 allocation, ppc, midcomms_new_msg_cb, mh);
1005 opts = (struct dlm_opts *)*ppc;
1008 /* add possible options here */
1009 dlm_fill_opts_header(opts, len, mh->seq);
1011 *ppc += sizeof(*opts);
1012 mh->inner_hd = (const struct dlm_header *)*ppc;
1016 struct dlm_mhandle *dlm_midcomms_get_mhandle(int nodeid, int len,
1017 gfp_t allocation, char **ppc)
1019 struct midcomms_node *node;
1020 struct dlm_mhandle *mh;
1021 struct dlm_msg *msg;
1024 idx = srcu_read_lock(&nodes_srcu);
1025 node = nodeid2node(nodeid, 0);
1031 /* this is a bug, however we going on and hope it will be resolved */
1032 WARN_ON(test_bit(DLM_NODE_FLAG_STOP_TX, &node->flags));
1034 mh = kzalloc(sizeof(*mh), GFP_NOFS);
1041 switch (node->version) {
1042 case DLM_VERSION_3_1:
1043 msg = dlm_lowcomms_new_msg(nodeid, len, allocation, ppc,
1051 case DLM_VERSION_3_2:
1052 msg = dlm_midcomms_get_msg_3_2(mh, nodeid, len, allocation,
1068 /* keep in mind that is a must to call
1069 * dlm_midcomms_commit_msg() which releases
1070 * nodes_srcu using mh->idx which is assumed
1071 * here that the application will call it.
1076 srcu_read_unlock(&nodes_srcu, idx);
1080 static void dlm_midcomms_commit_msg_3_2(struct dlm_mhandle *mh)
1082 /* nexthdr chain for fast lookup */
1083 mh->opts->o_nextcmd = mh->inner_hd->h_cmd;
1084 mh->committed = true;
1085 dlm_lowcomms_commit_msg(mh->msg);
1088 void dlm_midcomms_commit_mhandle(struct dlm_mhandle *mh)
1090 switch (mh->node->version) {
1091 case DLM_VERSION_3_1:
1092 srcu_read_unlock(&nodes_srcu, mh->idx);
1094 dlm_lowcomms_commit_msg(mh->msg);
1095 dlm_lowcomms_put_msg(mh->msg);
1096 /* mh is not part of rcu list in this case */
1099 case DLM_VERSION_3_2:
1100 dlm_midcomms_commit_msg_3_2(mh);
1101 srcu_read_unlock(&nodes_srcu, mh->idx);
1104 srcu_read_unlock(&nodes_srcu, mh->idx);
1110 int dlm_midcomms_start(void)
1114 for (i = 0; i < CONN_HASH_SIZE; i++)
1115 INIT_HLIST_HEAD(&node_hash[i]);
1117 return dlm_lowcomms_start();
1120 static void dlm_act_fin_ack_rcv(struct midcomms_node *node)
1122 spin_lock(&node->state_lock);
1123 pr_debug("receive active fin ack from node %d with state %s\n",
1124 node->nodeid, dlm_state_str(node->state));
1126 switch (node->state) {
1128 node->state = DLM_FIN_WAIT2;
1129 pr_debug("switch node %d to state %s\n",
1130 node->nodeid, dlm_state_str(node->state));
1133 midcomms_node_reset(node);
1134 pr_debug("switch node %d to state %s\n",
1135 node->nodeid, dlm_state_str(node->state));
1136 wake_up(&node->shutdown_wait);
1139 /* not valid but somehow we got what we want */
1140 wake_up(&node->shutdown_wait);
1143 spin_unlock(&node->state_lock);
1144 log_print("%s: unexpected state: %d\n",
1145 __func__, node->state);
1149 spin_unlock(&node->state_lock);
1152 void dlm_midcomms_add_member(int nodeid)
1154 struct midcomms_node *node;
1157 if (nodeid == dlm_our_nodeid())
1160 idx = srcu_read_lock(&nodes_srcu);
1161 node = nodeid2node(nodeid, GFP_NOFS);
1163 srcu_read_unlock(&nodes_srcu, idx);
1167 spin_lock(&node->state_lock);
1169 pr_debug("receive add member from node %d with state %s\n",
1170 node->nodeid, dlm_state_str(node->state));
1171 switch (node->state) {
1172 case DLM_ESTABLISHED:
1175 node->state = DLM_ESTABLISHED;
1176 pr_debug("switch node %d to state %s\n",
1177 node->nodeid, dlm_state_str(node->state));
1180 /* some invalid state passive shutdown
1181 * was failed, we try to reset and
1182 * hope it will go on.
1184 log_print("reset node %d because shutdown stuck",
1187 midcomms_node_reset(node);
1188 node->state = DLM_ESTABLISHED;
1194 pr_debug("users inc count %d\n", node->users);
1195 spin_unlock(&node->state_lock);
1197 srcu_read_unlock(&nodes_srcu, idx);
1200 void dlm_midcomms_remove_member(int nodeid)
1202 struct midcomms_node *node;
1205 if (nodeid == dlm_our_nodeid())
1208 idx = srcu_read_lock(&nodes_srcu);
1209 node = nodeid2node(nodeid, 0);
1211 srcu_read_unlock(&nodes_srcu, idx);
1215 spin_lock(&node->state_lock);
1217 pr_debug("users dec count %d\n", node->users);
1219 /* hitting users count to zero means the
1220 * other side is running dlm_midcomms_stop()
1221 * we meet us to have a clean disconnect.
1223 if (node->users == 0) {
1224 pr_debug("receive remove member from node %d with state %s\n",
1225 node->nodeid, dlm_state_str(node->state));
1226 switch (node->state) {
1227 case DLM_ESTABLISHED:
1229 case DLM_CLOSE_WAIT:
1230 /* passive shutdown DLM_LAST_ACK case 2 */
1231 node->state = DLM_LAST_ACK;
1232 spin_unlock(&node->state_lock);
1234 pr_debug("switch node %d to state %s case 2\n",
1235 node->nodeid, dlm_state_str(node->state));
1238 /* probably receive fin caught it, do nothing */
1241 /* already gone, do nothing */
1244 log_print("%s: unexpected state: %d\n",
1245 __func__, node->state);
1249 spin_unlock(&node->state_lock);
1251 srcu_read_unlock(&nodes_srcu, idx);
1255 set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
1256 dlm_send_fin(node, dlm_pas_fin_ack_rcv);
1257 srcu_read_unlock(&nodes_srcu, idx);
1260 static void midcomms_node_release(struct rcu_head *rcu)
1262 struct midcomms_node *node = container_of(rcu, struct midcomms_node, rcu);
1264 WARN_ON(atomic_read(&node->send_queue_cnt));
1268 static void midcomms_shutdown(struct midcomms_node *node)
1272 /* old protocol, we don't wait for pending operations */
1273 switch (node->version) {
1274 case DLM_VERSION_3_2:
1280 spin_lock(&node->state_lock);
1281 pr_debug("receive active shutdown for node %d with state %s\n",
1282 node->nodeid, dlm_state_str(node->state));
1283 switch (node->state) {
1284 case DLM_ESTABLISHED:
1285 node->state = DLM_FIN_WAIT1;
1286 pr_debug("switch node %d to state %s case 2\n",
1287 node->nodeid, dlm_state_str(node->state));
1290 /* we have what we want */
1291 spin_unlock(&node->state_lock);
1294 /* busy to enter DLM_FIN_WAIT1, wait until passive
1295 * done in shutdown_wait to enter DLM_CLOSED.
1299 spin_unlock(&node->state_lock);
1301 if (node->state == DLM_FIN_WAIT1) {
1302 dlm_send_fin(node, dlm_act_fin_ack_rcv);
1304 if (DLM_DEBUG_FENCE_TERMINATION)
1308 /* wait for other side dlm + fin */
1309 ret = wait_event_timeout(node->shutdown_wait,
1310 node->state == DLM_CLOSED ||
1311 test_bit(DLM_NODE_FLAG_CLOSE, &node->flags),
1312 DLM_SHUTDOWN_TIMEOUT);
1313 if (!ret || test_bit(DLM_NODE_FLAG_CLOSE, &node->flags)) {
1314 pr_debug("active shutdown timed out for node %d with state %s\n",
1315 node->nodeid, dlm_state_str(node->state));
1316 midcomms_node_reset(node);
1320 pr_debug("active shutdown done for node %d with state %s\n",
1321 node->nodeid, dlm_state_str(node->state));
1324 void dlm_midcomms_shutdown(void)
1326 struct midcomms_node *node;
1329 mutex_lock(&close_lock);
1330 idx = srcu_read_lock(&nodes_srcu);
1331 for (i = 0; i < CONN_HASH_SIZE; i++) {
1332 hlist_for_each_entry_rcu(node, &node_hash[i], hlist) {
1333 midcomms_shutdown(node);
1335 dlm_delete_debug_comms_file(node->debugfs);
1337 spin_lock(&nodes_lock);
1338 hlist_del_rcu(&node->hlist);
1339 spin_unlock(&nodes_lock);
1341 call_srcu(&nodes_srcu, &node->rcu, midcomms_node_release);
1344 srcu_read_unlock(&nodes_srcu, idx);
1345 mutex_unlock(&close_lock);
1347 dlm_lowcomms_shutdown();
1350 int dlm_midcomms_close(int nodeid)
1352 struct midcomms_node *node;
1355 if (nodeid == dlm_our_nodeid())
1358 idx = srcu_read_lock(&nodes_srcu);
1359 /* Abort pending close/remove operation */
1360 node = nodeid2node(nodeid, 0);
1362 /* let shutdown waiters leave */
1363 set_bit(DLM_NODE_FLAG_CLOSE, &node->flags);
1364 wake_up(&node->shutdown_wait);
1366 srcu_read_unlock(&nodes_srcu, idx);
1368 synchronize_srcu(&nodes_srcu);
1370 idx = srcu_read_lock(&nodes_srcu);
1371 mutex_lock(&close_lock);
1372 node = nodeid2node(nodeid, 0);
1374 mutex_unlock(&close_lock);
1375 srcu_read_unlock(&nodes_srcu, idx);
1376 return dlm_lowcomms_close(nodeid);
1379 ret = dlm_lowcomms_close(nodeid);
1380 spin_lock(&node->state_lock);
1381 midcomms_node_reset(node);
1382 spin_unlock(&node->state_lock);
1383 srcu_read_unlock(&nodes_srcu, idx);
1384 mutex_unlock(&close_lock);