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.
112 * Tail Size checking:
114 * There exists a message tail payload in e.g. DLM_MSG however we don't
115 * check it against the message length yet regarding to the receive buffer
116 * length. That need to be validated.
120 * At timeout places or weird sequence number behaviours we should send
121 * a fencing request to the cluster manager.
124 /* Debug switch to enable a 5 seconds sleep waiting of a termination.
125 * This can be useful to test fencing while termination is running.
126 * This requires a setup with only gfs2 as dlm user, so that the
127 * last umount will terminate the connection.
129 * However it became useful to test, while the 5 seconds block in umount
130 * just press the reset button. In a lot of dropping the termination
131 * process can could take several seconds.
133 #define DLM_DEBUG_FENCE_TERMINATION 0
135 #include <trace/events/dlm.h>
138 #include "dlm_internal.h"
139 #include "lowcomms.h"
144 #include "midcomms.h"
146 /* init value for sequence numbers for testing purpose only e.g. overflows */
147 #define DLM_SEQ_INIT 0
148 /* 5 seconds wait to sync ending of dlm */
149 #define DLM_SHUTDOWN_TIMEOUT msecs_to_jiffies(5000)
150 #define DLM_VERSION_NOT_SET 0
151 #define DLM_SEND_ACK_BACK_MSG_THRESHOLD 32
152 #define DLM_RECV_ACK_BACK_MSG_THRESHOLD (DLM_SEND_ACK_BACK_MSG_THRESHOLD * 8)
154 struct midcomms_node {
159 /* These queues are unbound because we cannot drop any message in dlm.
160 * We could send a fence signal for a specific node to the cluster
161 * manager if queues hits some maximum value, however this handling
164 struct list_head send_queue;
165 spinlock_t send_queue_lock;
166 atomic_t send_queue_cnt;
167 #define DLM_NODE_FLAG_CLOSE 1
168 #define DLM_NODE_FLAG_STOP_TX 2
169 #define DLM_NODE_FLAG_STOP_RX 3
170 atomic_t ulp_delivered;
172 wait_queue_head_t shutdown_wait;
174 /* dlm tcp termination state */
176 #define DLM_ESTABLISHED 2
177 #define DLM_FIN_WAIT1 3
178 #define DLM_FIN_WAIT2 4
179 #define DLM_CLOSE_WAIT 5
180 #define DLM_LAST_ACK 6
181 #define DLM_CLOSING 7
183 spinlock_t state_lock;
185 /* counts how many lockspaces are using this node
186 * this refcount is necessary to determine if the
187 * node wants to disconnect.
191 /* not protected by srcu, node_hash lifetime */
194 struct hlist_node hlist;
199 const union dlm_packet *inner_p;
200 struct midcomms_node *node;
201 struct dlm_opts *opts;
206 void (*ack_rcv)(struct midcomms_node *node);
208 /* get_mhandle/commit srcu idx exchange */
211 struct list_head list;
215 static struct hlist_head node_hash[CONN_HASH_SIZE];
216 static DEFINE_SPINLOCK(nodes_lock);
217 DEFINE_STATIC_SRCU(nodes_srcu);
219 /* This mutex prevents that midcomms_close() is running while
220 * stop() or remove(). As I experienced invalid memory access
221 * behaviours when DLM_DEBUG_FENCE_TERMINATION is enabled and
222 * resetting machines. I will end in some double deletion in nodes
225 static DEFINE_MUTEX(close_lock);
227 struct kmem_cache *dlm_midcomms_cache_create(void)
229 return kmem_cache_create("dlm_mhandle", sizeof(struct dlm_mhandle),
233 static inline const char *dlm_state_str(int state)
238 case DLM_ESTABLISHED:
239 return "ESTABLISHED";
255 const char *dlm_midcomms_state(struct midcomms_node *node)
257 return dlm_state_str(node->state);
260 unsigned long dlm_midcomms_flags(struct midcomms_node *node)
265 int dlm_midcomms_send_queue_cnt(struct midcomms_node *node)
267 return atomic_read(&node->send_queue_cnt);
270 uint32_t dlm_midcomms_version(struct midcomms_node *node)
272 return node->version;
275 static struct midcomms_node *__find_node(int nodeid, int r)
277 struct midcomms_node *node;
279 hlist_for_each_entry_rcu(node, &node_hash[r], hlist) {
280 if (node->nodeid == nodeid)
287 static void dlm_mhandle_release(struct rcu_head *rcu)
289 struct dlm_mhandle *mh = container_of(rcu, struct dlm_mhandle, rcu);
291 dlm_lowcomms_put_msg(mh->msg);
292 dlm_free_mhandle(mh);
295 static void dlm_mhandle_delete(struct midcomms_node *node,
296 struct dlm_mhandle *mh)
298 list_del_rcu(&mh->list);
299 atomic_dec(&node->send_queue_cnt);
300 call_rcu(&mh->rcu, dlm_mhandle_release);
303 static void dlm_send_queue_flush(struct midcomms_node *node)
305 struct dlm_mhandle *mh;
307 pr_debug("flush midcomms send queue of node %d\n", node->nodeid);
310 spin_lock_bh(&node->send_queue_lock);
311 list_for_each_entry_rcu(mh, &node->send_queue, list) {
312 dlm_mhandle_delete(node, mh);
314 spin_unlock_bh(&node->send_queue_lock);
318 static void midcomms_node_reset(struct midcomms_node *node)
320 pr_debug("reset node %d\n", node->nodeid);
322 atomic_set(&node->seq_next, DLM_SEQ_INIT);
323 atomic_set(&node->seq_send, DLM_SEQ_INIT);
324 atomic_set(&node->ulp_delivered, 0);
325 node->version = DLM_VERSION_NOT_SET;
328 dlm_send_queue_flush(node);
329 node->state = DLM_CLOSED;
330 wake_up(&node->shutdown_wait);
333 static struct midcomms_node *nodeid2node(int nodeid)
335 return __find_node(nodeid, nodeid_hash(nodeid));
338 int dlm_midcomms_addr(int nodeid, struct sockaddr_storage *addr, int len)
340 int ret, r = nodeid_hash(nodeid);
341 struct midcomms_node *node;
343 ret = dlm_lowcomms_addr(nodeid, addr, len);
347 node = kmalloc(sizeof(*node), GFP_NOFS);
351 node->nodeid = nodeid;
352 spin_lock_init(&node->state_lock);
353 spin_lock_init(&node->send_queue_lock);
354 atomic_set(&node->send_queue_cnt, 0);
355 INIT_LIST_HEAD(&node->send_queue);
356 init_waitqueue_head(&node->shutdown_wait);
358 midcomms_node_reset(node);
360 spin_lock(&nodes_lock);
361 hlist_add_head_rcu(&node->hlist, &node_hash[r]);
362 spin_unlock(&nodes_lock);
364 node->debugfs = dlm_create_debug_comms_file(nodeid, node);
368 static int dlm_send_ack(int nodeid, uint32_t seq)
370 int mb_len = sizeof(struct dlm_header);
371 struct dlm_header *m_header;
375 msg = dlm_lowcomms_new_msg(nodeid, mb_len, GFP_ATOMIC, &ppc,
380 m_header = (struct dlm_header *)ppc;
382 m_header->h_version = cpu_to_le32(DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
383 m_header->h_nodeid = cpu_to_le32(dlm_our_nodeid());
384 m_header->h_length = cpu_to_le16(mb_len);
385 m_header->h_cmd = DLM_ACK;
386 m_header->u.h_seq = cpu_to_le32(seq);
388 dlm_lowcomms_commit_msg(msg);
389 dlm_lowcomms_put_msg(msg);
394 static void dlm_send_ack_threshold(struct midcomms_node *node,
400 /* let only send one user trigger threshold to send ack back */
402 oval = atomic_read(&node->ulp_delivered);
403 send_ack = (oval > threshold);
404 /* abort if threshold is not reached */
409 /* try to reset ulp_delivered counter */
410 } while (atomic_cmpxchg(&node->ulp_delivered, oval, nval) != oval);
413 dlm_send_ack(node->nodeid, atomic_read(&node->seq_next));
416 static int dlm_send_fin(struct midcomms_node *node,
417 void (*ack_rcv)(struct midcomms_node *node))
419 int mb_len = sizeof(struct dlm_header);
420 struct dlm_header *m_header;
421 struct dlm_mhandle *mh;
424 mh = dlm_midcomms_get_mhandle(node->nodeid, mb_len, GFP_ATOMIC, &ppc);
428 set_bit(DLM_NODE_FLAG_STOP_TX, &node->flags);
429 mh->ack_rcv = ack_rcv;
431 m_header = (struct dlm_header *)ppc;
433 m_header->h_version = cpu_to_le32(DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
434 m_header->h_nodeid = cpu_to_le32(dlm_our_nodeid());
435 m_header->h_length = cpu_to_le16(mb_len);
436 m_header->h_cmd = DLM_FIN;
438 pr_debug("sending fin msg to node %d\n", node->nodeid);
439 dlm_midcomms_commit_mhandle(mh, NULL, 0);
444 static void dlm_receive_ack(struct midcomms_node *node, uint32_t seq)
446 struct dlm_mhandle *mh;
449 list_for_each_entry_rcu(mh, &node->send_queue, list) {
450 if (before(mh->seq, seq)) {
454 /* send queue should be ordered */
459 spin_lock_bh(&node->send_queue_lock);
460 list_for_each_entry_rcu(mh, &node->send_queue, list) {
461 if (before(mh->seq, seq)) {
462 dlm_mhandle_delete(node, mh);
464 /* send queue should be ordered */
468 spin_unlock_bh(&node->send_queue_lock);
472 static void dlm_pas_fin_ack_rcv(struct midcomms_node *node)
474 spin_lock(&node->state_lock);
475 pr_debug("receive passive fin ack from node %d with state %s\n",
476 node->nodeid, dlm_state_str(node->state));
478 switch (node->state) {
481 midcomms_node_reset(node);
484 /* not valid but somehow we got what we want */
485 wake_up(&node->shutdown_wait);
488 spin_unlock(&node->state_lock);
489 log_print("%s: unexpected state: %d",
490 __func__, node->state);
494 spin_unlock(&node->state_lock);
497 static void dlm_receive_buffer_3_2_trace(uint32_t seq,
498 const union dlm_packet *p)
500 switch (p->header.h_cmd) {
502 trace_dlm_recv_message(dlm_our_nodeid(), seq, &p->message);
505 trace_dlm_recv_rcom(dlm_our_nodeid(), seq, &p->rcom);
512 static void dlm_midcomms_receive_buffer(const union dlm_packet *p,
513 struct midcomms_node *node,
516 bool is_expected_seq;
520 oval = atomic_read(&node->seq_next);
521 is_expected_seq = (oval == seq);
522 if (!is_expected_seq)
526 } while (atomic_cmpxchg(&node->seq_next, oval, nval) != oval);
528 if (is_expected_seq) {
529 switch (p->header.h_cmd) {
531 spin_lock(&node->state_lock);
532 pr_debug("receive fin msg from node %d with state %s\n",
533 node->nodeid, dlm_state_str(node->state));
535 switch (node->state) {
536 case DLM_ESTABLISHED:
537 dlm_send_ack(node->nodeid, nval);
539 /* passive shutdown DLM_LAST_ACK case 1
540 * additional we check if the node is used by
541 * cluster manager events at all.
543 if (node->users == 0) {
544 node->state = DLM_LAST_ACK;
545 pr_debug("switch node %d to state %s case 1\n",
546 node->nodeid, dlm_state_str(node->state));
547 set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
548 dlm_send_fin(node, dlm_pas_fin_ack_rcv);
550 node->state = DLM_CLOSE_WAIT;
551 pr_debug("switch node %d to state %s\n",
552 node->nodeid, dlm_state_str(node->state));
556 dlm_send_ack(node->nodeid, nval);
557 node->state = DLM_CLOSING;
558 set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
559 pr_debug("switch node %d to state %s\n",
560 node->nodeid, dlm_state_str(node->state));
563 dlm_send_ack(node->nodeid, nval);
564 midcomms_node_reset(node);
565 pr_debug("switch node %d to state %s\n",
566 node->nodeid, dlm_state_str(node->state));
569 /* probably remove_member caught it, do nothing */
572 spin_unlock(&node->state_lock);
573 log_print("%s: unexpected state: %d",
574 __func__, node->state);
578 spin_unlock(&node->state_lock);
581 WARN_ON_ONCE(test_bit(DLM_NODE_FLAG_STOP_RX, &node->flags));
582 dlm_receive_buffer_3_2_trace(seq, p);
583 dlm_receive_buffer(p, node->nodeid);
584 atomic_inc(&node->ulp_delivered);
585 /* unlikely case to send ack back when we don't transmit */
586 dlm_send_ack_threshold(node, DLM_RECV_ACK_BACK_MSG_THRESHOLD);
590 /* retry to ack message which we already have by sending back
591 * current node->seq_next number as ack.
594 dlm_send_ack(node->nodeid, oval);
596 log_print_ratelimited("ignore dlm msg because seq mismatch, seq: %u, expected: %u, nodeid: %d",
597 seq, oval, node->nodeid);
601 static int dlm_opts_check_msglen(const union dlm_packet *p, uint16_t msglen,
606 /* we only trust outer header msglen because
607 * it's checked against receive buffer length.
609 if (len < sizeof(struct dlm_opts))
611 len -= sizeof(struct dlm_opts);
613 if (len < le16_to_cpu(p->opts.o_optlen))
615 len -= le16_to_cpu(p->opts.o_optlen);
617 switch (p->opts.o_nextcmd) {
619 if (len < sizeof(struct dlm_header)) {
620 log_print("fin too small: %d, will skip this message from node %d",
627 if (len < sizeof(struct dlm_message)) {
628 log_print("msg too small: %d, will skip this message from node %d",
635 if (len < sizeof(struct dlm_rcom)) {
636 log_print("rcom msg too small: %d, will skip this message from node %d",
643 log_print("unsupported o_nextcmd received: %u, will skip this message from node %d",
644 p->opts.o_nextcmd, nodeid);
651 static void dlm_midcomms_receive_buffer_3_2(const union dlm_packet *p, int nodeid)
653 uint16_t msglen = le16_to_cpu(p->header.h_length);
654 struct midcomms_node *node;
658 idx = srcu_read_lock(&nodes_srcu);
659 node = nodeid2node(nodeid);
660 if (WARN_ON_ONCE(!node))
663 switch (node->version) {
664 case DLM_VERSION_NOT_SET:
665 node->version = DLM_VERSION_3_2;
666 wake_up(&node->shutdown_wait);
667 log_print("version 0x%08x for node %d detected", DLM_VERSION_3_2,
670 spin_lock(&node->state_lock);
671 switch (node->state) {
673 node->state = DLM_ESTABLISHED;
674 pr_debug("switch node %d to state %s\n",
675 node->nodeid, dlm_state_str(node->state));
680 spin_unlock(&node->state_lock);
683 case DLM_VERSION_3_2:
686 log_print_ratelimited("version mismatch detected, assumed 0x%08x but node %d has 0x%08x",
687 DLM_VERSION_3_2, node->nodeid, node->version);
691 switch (p->header.h_cmd) {
693 /* these rcom message we use to determine version.
694 * they have their own retransmission handling and
695 * are the first messages of dlm.
697 * length already checked.
699 switch (p->rcom.rc_type) {
700 case cpu_to_le32(DLM_RCOM_NAMES):
702 case cpu_to_le32(DLM_RCOM_NAMES_REPLY):
704 case cpu_to_le32(DLM_RCOM_STATUS):
706 case cpu_to_le32(DLM_RCOM_STATUS_REPLY):
709 log_print("unsupported rcom type received: %u, will skip this message from node %d",
710 le32_to_cpu(p->rcom.rc_type), nodeid);
714 WARN_ON_ONCE(test_bit(DLM_NODE_FLAG_STOP_RX, &node->flags));
715 dlm_receive_buffer(p, nodeid);
718 seq = le32_to_cpu(p->header.u.h_seq);
720 ret = dlm_opts_check_msglen(p, msglen, nodeid);
722 log_print("opts msg too small: %u, will skip this message from node %d",
727 p = (union dlm_packet *)((unsigned char *)p->opts.o_opts +
728 le16_to_cpu(p->opts.o_optlen));
730 /* recheck inner msglen just if it's not garbage */
731 msglen = le16_to_cpu(p->header.h_length);
732 switch (p->header.h_cmd) {
734 if (msglen < sizeof(struct dlm_rcom)) {
735 log_print("inner rcom msg too small: %u, will skip this message from node %d",
742 if (msglen < sizeof(struct dlm_message)) {
743 log_print("inner msg too small: %u, will skip this message from node %d",
750 if (msglen < sizeof(struct dlm_header)) {
751 log_print("inner fin too small: %u, will skip this message from node %d",
758 log_print("unsupported inner h_cmd received: %u, will skip this message from node %d",
763 dlm_midcomms_receive_buffer(p, node, seq);
766 seq = le32_to_cpu(p->header.u.h_seq);
767 dlm_receive_ack(node, seq);
770 log_print("unsupported h_cmd received: %u, will skip this message from node %d",
771 p->header.h_cmd, nodeid);
776 srcu_read_unlock(&nodes_srcu, idx);
779 static void dlm_midcomms_receive_buffer_3_1(const union dlm_packet *p, int nodeid)
781 uint16_t msglen = le16_to_cpu(p->header.h_length);
782 struct midcomms_node *node;
785 idx = srcu_read_lock(&nodes_srcu);
786 node = nodeid2node(nodeid);
787 if (WARN_ON_ONCE(!node)) {
788 srcu_read_unlock(&nodes_srcu, idx);
792 switch (node->version) {
793 case DLM_VERSION_NOT_SET:
794 node->version = DLM_VERSION_3_1;
795 wake_up(&node->shutdown_wait);
796 log_print("version 0x%08x for node %d detected", DLM_VERSION_3_1,
799 case DLM_VERSION_3_1:
802 log_print_ratelimited("version mismatch detected, assumed 0x%08x but node %d has 0x%08x",
803 DLM_VERSION_3_1, node->nodeid, node->version);
804 srcu_read_unlock(&nodes_srcu, idx);
807 srcu_read_unlock(&nodes_srcu, idx);
809 switch (p->header.h_cmd) {
811 /* length already checked */
814 if (msglen < sizeof(struct dlm_message)) {
815 log_print("msg too small: %u, will skip this message from node %d",
822 log_print("unsupported h_cmd received: %u, will skip this message from node %d",
823 p->header.h_cmd, nodeid);
827 dlm_receive_buffer(p, nodeid);
830 int dlm_validate_incoming_buffer(int nodeid, unsigned char *buf, int len)
832 const unsigned char *ptr = buf;
833 const struct dlm_header *hd;
837 while (len >= sizeof(struct dlm_header)) {
838 hd = (struct dlm_header *)ptr;
840 /* no message should be more than DLM_MAX_SOCKET_BUFSIZE or
841 * less than dlm_header size.
843 * Some messages does not have a 8 byte length boundary yet
844 * which can occur in a unaligned memory access of some dlm
845 * messages. However this problem need to be fixed at the
846 * sending side, for now it seems nobody run into architecture
847 * related issues yet but it slows down some processing.
848 * Fixing this issue should be scheduled in future by doing
849 * the next major version bump.
851 msglen = le16_to_cpu(hd->h_length);
852 if (msglen > DLM_MAX_SOCKET_BUFSIZE ||
853 msglen < sizeof(struct dlm_header)) {
854 log_print("received invalid length header: %u from node %d, will abort message parsing",
859 /* caller will take care that leftover
860 * will be parsed next call with more data
874 * Called from the low-level comms layer to process a buffer of
877 int dlm_process_incoming_buffer(int nodeid, unsigned char *buf, int len)
879 const unsigned char *ptr = buf;
880 const struct dlm_header *hd;
884 while (len >= sizeof(struct dlm_header)) {
885 hd = (struct dlm_header *)ptr;
887 msglen = le16_to_cpu(hd->h_length);
891 switch (hd->h_version) {
892 case cpu_to_le32(DLM_VERSION_3_1):
893 dlm_midcomms_receive_buffer_3_1((const union dlm_packet *)ptr, nodeid);
895 case cpu_to_le32(DLM_VERSION_3_2):
896 dlm_midcomms_receive_buffer_3_2((const union dlm_packet *)ptr, nodeid);
899 log_print("received invalid version header: %u from node %d, will skip this message",
900 le32_to_cpu(hd->h_version), nodeid);
912 void dlm_midcomms_unack_msg_resend(int nodeid)
914 struct midcomms_node *node;
915 struct dlm_mhandle *mh;
918 idx = srcu_read_lock(&nodes_srcu);
919 node = nodeid2node(nodeid);
920 if (WARN_ON_ONCE(!node)) {
921 srcu_read_unlock(&nodes_srcu, idx);
925 /* old protocol, we don't support to retransmit on failure */
926 switch (node->version) {
927 case DLM_VERSION_3_2:
930 srcu_read_unlock(&nodes_srcu, idx);
935 list_for_each_entry_rcu(mh, &node->send_queue, list) {
939 ret = dlm_lowcomms_resend_msg(mh->msg);
941 log_print_ratelimited("retransmit dlm msg, seq %u, nodeid %d",
942 mh->seq, node->nodeid);
945 srcu_read_unlock(&nodes_srcu, idx);
948 static void dlm_fill_opts_header(struct dlm_opts *opts, uint16_t inner_len,
951 opts->o_header.h_cmd = DLM_OPTS;
952 opts->o_header.h_version = cpu_to_le32(DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
953 opts->o_header.h_nodeid = cpu_to_le32(dlm_our_nodeid());
954 opts->o_header.h_length = cpu_to_le16(DLM_MIDCOMMS_OPT_LEN + inner_len);
955 opts->o_header.u.h_seq = cpu_to_le32(seq);
958 static void midcomms_new_msg_cb(void *data)
960 struct dlm_mhandle *mh = data;
962 atomic_inc(&mh->node->send_queue_cnt);
964 spin_lock_bh(&mh->node->send_queue_lock);
965 list_add_tail_rcu(&mh->list, &mh->node->send_queue);
966 spin_unlock_bh(&mh->node->send_queue_lock);
968 mh->seq = atomic_fetch_inc(&mh->node->seq_send);
971 static struct dlm_msg *dlm_midcomms_get_msg_3_2(struct dlm_mhandle *mh, int nodeid,
972 int len, gfp_t allocation, char **ppc)
974 struct dlm_opts *opts;
977 msg = dlm_lowcomms_new_msg(nodeid, len + DLM_MIDCOMMS_OPT_LEN,
978 allocation, ppc, midcomms_new_msg_cb, mh);
982 opts = (struct dlm_opts *)*ppc;
985 /* add possible options here */
986 dlm_fill_opts_header(opts, len, mh->seq);
988 *ppc += sizeof(*opts);
989 mh->inner_p = (const union dlm_packet *)*ppc;
993 /* avoid false positive for nodes_srcu, unlock happens in
994 * dlm_midcomms_commit_mhandle which is a must call if success
997 struct dlm_mhandle *dlm_midcomms_get_mhandle(int nodeid, int len,
998 gfp_t allocation, char **ppc)
1000 struct midcomms_node *node;
1001 struct dlm_mhandle *mh;
1002 struct dlm_msg *msg;
1005 idx = srcu_read_lock(&nodes_srcu);
1006 node = nodeid2node(nodeid);
1007 if (WARN_ON_ONCE(!node))
1010 /* this is a bug, however we going on and hope it will be resolved */
1011 WARN_ON_ONCE(test_bit(DLM_NODE_FLAG_STOP_TX, &node->flags));
1013 mh = dlm_allocate_mhandle(allocation);
1017 mh->committed = false;
1022 switch (node->version) {
1023 case DLM_VERSION_3_1:
1024 msg = dlm_lowcomms_new_msg(nodeid, len, allocation, ppc,
1027 dlm_free_mhandle(mh);
1032 case DLM_VERSION_3_2:
1033 msg = dlm_midcomms_get_msg_3_2(mh, nodeid, len, allocation,
1036 dlm_free_mhandle(mh);
1040 /* send ack back if necessary */
1041 dlm_send_ack_threshold(node, DLM_SEND_ACK_BACK_MSG_THRESHOLD);
1044 dlm_free_mhandle(mh);
1051 /* keep in mind that is a must to call
1052 * dlm_midcomms_commit_msg() which releases
1053 * nodes_srcu using mh->idx which is assumed
1054 * here that the application will call it.
1059 srcu_read_unlock(&nodes_srcu, idx);
1064 static void dlm_midcomms_commit_msg_3_2_trace(const struct dlm_mhandle *mh,
1065 const void *name, int namelen)
1067 switch (mh->inner_p->header.h_cmd) {
1069 trace_dlm_send_message(mh->node->nodeid, mh->seq,
1070 &mh->inner_p->message,
1074 trace_dlm_send_rcom(mh->node->nodeid, mh->seq,
1075 &mh->inner_p->rcom);
1078 /* nothing to trace */
1083 static void dlm_midcomms_commit_msg_3_2(struct dlm_mhandle *mh,
1084 const void *name, int namelen)
1086 /* nexthdr chain for fast lookup */
1087 mh->opts->o_nextcmd = mh->inner_p->header.h_cmd;
1088 mh->committed = true;
1089 dlm_midcomms_commit_msg_3_2_trace(mh, name, namelen);
1090 dlm_lowcomms_commit_msg(mh->msg);
1093 /* avoid false positive for nodes_srcu, lock was happen in
1094 * dlm_midcomms_get_mhandle
1097 void dlm_midcomms_commit_mhandle(struct dlm_mhandle *mh,
1098 const void *name, int namelen)
1101 switch (mh->node->version) {
1102 case DLM_VERSION_3_1:
1103 srcu_read_unlock(&nodes_srcu, mh->idx);
1105 dlm_lowcomms_commit_msg(mh->msg);
1106 dlm_lowcomms_put_msg(mh->msg);
1107 /* mh is not part of rcu list in this case */
1108 dlm_free_mhandle(mh);
1110 case DLM_VERSION_3_2:
1111 /* held rcu read lock here, because we sending the
1112 * dlm message out, when we do that we could receive
1113 * an ack back which releases the mhandle and we
1114 * get a use after free.
1117 dlm_midcomms_commit_msg_3_2(mh, name, namelen);
1118 srcu_read_unlock(&nodes_srcu, mh->idx);
1122 srcu_read_unlock(&nodes_srcu, mh->idx);
1129 int dlm_midcomms_start(void)
1131 return dlm_lowcomms_start();
1134 void dlm_midcomms_stop(void)
1136 dlm_lowcomms_stop();
1139 void dlm_midcomms_init(void)
1143 for (i = 0; i < CONN_HASH_SIZE; i++)
1144 INIT_HLIST_HEAD(&node_hash[i]);
1146 dlm_lowcomms_init();
1149 static void midcomms_node_release(struct rcu_head *rcu)
1151 struct midcomms_node *node = container_of(rcu, struct midcomms_node, rcu);
1153 WARN_ON_ONCE(atomic_read(&node->send_queue_cnt));
1154 dlm_send_queue_flush(node);
1158 void dlm_midcomms_exit(void)
1160 struct midcomms_node *node;
1163 idx = srcu_read_lock(&nodes_srcu);
1164 for (i = 0; i < CONN_HASH_SIZE; i++) {
1165 hlist_for_each_entry_rcu(node, &node_hash[i], hlist) {
1166 dlm_delete_debug_comms_file(node->debugfs);
1168 spin_lock(&nodes_lock);
1169 hlist_del_rcu(&node->hlist);
1170 spin_unlock(&nodes_lock);
1172 call_srcu(&nodes_srcu, &node->rcu, midcomms_node_release);
1175 srcu_read_unlock(&nodes_srcu, idx);
1177 dlm_lowcomms_exit();
1180 static void dlm_act_fin_ack_rcv(struct midcomms_node *node)
1182 spin_lock(&node->state_lock);
1183 pr_debug("receive active fin ack from node %d with state %s\n",
1184 node->nodeid, dlm_state_str(node->state));
1186 switch (node->state) {
1188 node->state = DLM_FIN_WAIT2;
1189 pr_debug("switch node %d to state %s\n",
1190 node->nodeid, dlm_state_str(node->state));
1193 midcomms_node_reset(node);
1194 pr_debug("switch node %d to state %s\n",
1195 node->nodeid, dlm_state_str(node->state));
1198 /* not valid but somehow we got what we want */
1199 wake_up(&node->shutdown_wait);
1202 spin_unlock(&node->state_lock);
1203 log_print("%s: unexpected state: %d",
1204 __func__, node->state);
1208 spin_unlock(&node->state_lock);
1211 void dlm_midcomms_add_member(int nodeid)
1213 struct midcomms_node *node;
1216 idx = srcu_read_lock(&nodes_srcu);
1217 node = nodeid2node(nodeid);
1218 if (WARN_ON_ONCE(!node)) {
1219 srcu_read_unlock(&nodes_srcu, idx);
1223 spin_lock(&node->state_lock);
1225 pr_debug("receive add member from node %d with state %s\n",
1226 node->nodeid, dlm_state_str(node->state));
1227 switch (node->state) {
1228 case DLM_ESTABLISHED:
1231 node->state = DLM_ESTABLISHED;
1232 pr_debug("switch node %d to state %s\n",
1233 node->nodeid, dlm_state_str(node->state));
1236 /* some invalid state passive shutdown
1237 * was failed, we try to reset and
1238 * hope it will go on.
1240 log_print("reset node %d because shutdown stuck",
1243 midcomms_node_reset(node);
1244 node->state = DLM_ESTABLISHED;
1250 pr_debug("node %d users inc count %d\n", nodeid, node->users);
1251 spin_unlock(&node->state_lock);
1253 srcu_read_unlock(&nodes_srcu, idx);
1256 void dlm_midcomms_remove_member(int nodeid)
1258 struct midcomms_node *node;
1261 idx = srcu_read_lock(&nodes_srcu);
1262 node = nodeid2node(nodeid);
1263 if (WARN_ON_ONCE(!node)) {
1264 srcu_read_unlock(&nodes_srcu, idx);
1268 spin_lock(&node->state_lock);
1270 pr_debug("node %d users dec count %d\n", nodeid, node->users);
1272 /* hitting users count to zero means the
1273 * other side is running dlm_midcomms_stop()
1274 * we meet us to have a clean disconnect.
1276 if (node->users == 0) {
1277 pr_debug("receive remove member from node %d with state %s\n",
1278 node->nodeid, dlm_state_str(node->state));
1279 switch (node->state) {
1280 case DLM_ESTABLISHED:
1282 case DLM_CLOSE_WAIT:
1283 /* passive shutdown DLM_LAST_ACK case 2 */
1284 node->state = DLM_LAST_ACK;
1285 pr_debug("switch node %d to state %s case 2\n",
1286 node->nodeid, dlm_state_str(node->state));
1287 set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
1288 dlm_send_fin(node, dlm_pas_fin_ack_rcv);
1291 /* probably receive fin caught it, do nothing */
1294 /* already gone, do nothing */
1297 log_print("%s: unexpected state: %d",
1298 __func__, node->state);
1302 spin_unlock(&node->state_lock);
1304 srcu_read_unlock(&nodes_srcu, idx);
1307 void dlm_midcomms_version_wait(void)
1309 struct midcomms_node *node;
1312 idx = srcu_read_lock(&nodes_srcu);
1313 for (i = 0; i < CONN_HASH_SIZE; i++) {
1314 hlist_for_each_entry_rcu(node, &node_hash[i], hlist) {
1315 ret = wait_event_timeout(node->shutdown_wait,
1316 node->version != DLM_VERSION_NOT_SET ||
1317 node->state == DLM_CLOSED ||
1318 test_bit(DLM_NODE_FLAG_CLOSE, &node->flags),
1319 DLM_SHUTDOWN_TIMEOUT);
1320 if (!ret || test_bit(DLM_NODE_FLAG_CLOSE, &node->flags))
1321 pr_debug("version wait timed out for node %d with state %s\n",
1322 node->nodeid, dlm_state_str(node->state));
1325 srcu_read_unlock(&nodes_srcu, idx);
1328 static void midcomms_shutdown(struct midcomms_node *node)
1332 /* old protocol, we don't wait for pending operations */
1333 switch (node->version) {
1334 case DLM_VERSION_3_2:
1340 spin_lock(&node->state_lock);
1341 pr_debug("receive active shutdown for node %d with state %s\n",
1342 node->nodeid, dlm_state_str(node->state));
1343 switch (node->state) {
1344 case DLM_ESTABLISHED:
1345 node->state = DLM_FIN_WAIT1;
1346 pr_debug("switch node %d to state %s case 2\n",
1347 node->nodeid, dlm_state_str(node->state));
1348 dlm_send_fin(node, dlm_act_fin_ack_rcv);
1351 /* we have what we want */
1354 /* busy to enter DLM_FIN_WAIT1, wait until passive
1355 * done in shutdown_wait to enter DLM_CLOSED.
1359 spin_unlock(&node->state_lock);
1361 if (DLM_DEBUG_FENCE_TERMINATION)
1364 /* wait for other side dlm + fin */
1365 ret = wait_event_timeout(node->shutdown_wait,
1366 node->state == DLM_CLOSED ||
1367 test_bit(DLM_NODE_FLAG_CLOSE, &node->flags),
1368 DLM_SHUTDOWN_TIMEOUT);
1370 pr_debug("active shutdown timed out for node %d with state %s\n",
1371 node->nodeid, dlm_state_str(node->state));
1373 pr_debug("active shutdown done for node %d with state %s\n",
1374 node->nodeid, dlm_state_str(node->state));
1377 void dlm_midcomms_shutdown(void)
1379 struct midcomms_node *node;
1382 mutex_lock(&close_lock);
1383 idx = srcu_read_lock(&nodes_srcu);
1384 for (i = 0; i < CONN_HASH_SIZE; i++) {
1385 hlist_for_each_entry_rcu(node, &node_hash[i], hlist) {
1386 midcomms_shutdown(node);
1389 srcu_read_unlock(&nodes_srcu, idx);
1390 mutex_unlock(&close_lock);
1392 dlm_lowcomms_shutdown();
1395 int dlm_midcomms_close(int nodeid)
1397 struct midcomms_node *node;
1400 idx = srcu_read_lock(&nodes_srcu);
1401 /* Abort pending close/remove operation */
1402 node = nodeid2node(nodeid);
1404 /* let shutdown waiters leave */
1405 set_bit(DLM_NODE_FLAG_CLOSE, &node->flags);
1406 wake_up(&node->shutdown_wait);
1408 srcu_read_unlock(&nodes_srcu, idx);
1410 synchronize_srcu(&nodes_srcu);
1412 mutex_lock(&close_lock);
1413 idx = srcu_read_lock(&nodes_srcu);
1414 node = nodeid2node(nodeid);
1416 srcu_read_unlock(&nodes_srcu, idx);
1417 mutex_unlock(&close_lock);
1418 return dlm_lowcomms_close(nodeid);
1421 ret = dlm_lowcomms_close(nodeid);
1422 dlm_delete_debug_comms_file(node->debugfs);
1424 spin_lock(&nodes_lock);
1425 hlist_del_rcu(&node->hlist);
1426 spin_unlock(&nodes_lock);
1427 srcu_read_unlock(&nodes_srcu, idx);
1429 /* wait that all readers left until flush send queue */
1430 synchronize_srcu(&nodes_srcu);
1432 /* drop all pending dlm messages, this is fine as
1433 * this function get called when the node is fenced
1435 dlm_send_queue_flush(node);
1437 call_srcu(&nodes_srcu, &node->rcu, midcomms_node_release);
1438 mutex_unlock(&close_lock);
1443 /* debug functionality to send raw dlm msg from user space */
1444 struct dlm_rawmsg_data {
1445 struct midcomms_node *node;
1449 static void midcomms_new_rawmsg_cb(void *data)
1451 struct dlm_rawmsg_data *rd = data;
1452 struct dlm_header *h = rd->buf;
1454 switch (h->h_version) {
1455 case cpu_to_le32(DLM_VERSION_3_1):
1461 h->u.h_seq = cpu_to_le32(atomic_fetch_inc(&rd->node->seq_send));
1470 int dlm_midcomms_rawmsg_send(struct midcomms_node *node, void *buf,
1473 struct dlm_rawmsg_data rd;
1474 struct dlm_msg *msg;
1480 msg = dlm_lowcomms_new_msg(node->nodeid, buflen, GFP_NOFS,
1481 &msgbuf, midcomms_new_rawmsg_cb, &rd);
1485 memcpy(msgbuf, buf, buflen);
1486 dlm_lowcomms_commit_msg(msg);