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
137 #include "dlm_internal.h"
138 #include "lowcomms.h"
142 #include "midcomms.h"
144 /* init value for sequence numbers for testing purpose only e.g. overflows */
145 #define DLM_SEQ_INIT 0
146 /* 3 minutes wait to sync ending of dlm */
147 #define DLM_SHUTDOWN_TIMEOUT msecs_to_jiffies(3 * 60 * 1000)
148 #define DLM_VERSION_NOT_SET 0
150 struct midcomms_node {
155 /* These queues are unbound because we cannot drop any message in dlm.
156 * We could send a fence signal for a specific node to the cluster
157 * manager if queues hits some maximum value, however this handling
160 struct list_head send_queue;
161 spinlock_t send_queue_lock;
162 atomic_t send_queue_cnt;
163 #define DLM_NODE_FLAG_CLOSE 1
164 #define DLM_NODE_FLAG_STOP_TX 2
165 #define DLM_NODE_FLAG_STOP_RX 3
166 #define DLM_NODE_ULP_DELIVERED 4
168 wait_queue_head_t shutdown_wait;
170 /* dlm tcp termination state */
172 #define DLM_ESTABLISHED 2
173 #define DLM_FIN_WAIT1 3
174 #define DLM_FIN_WAIT2 4
175 #define DLM_CLOSE_WAIT 5
176 #define DLM_LAST_ACK 6
177 #define DLM_CLOSING 7
179 spinlock_t state_lock;
181 /* counts how many lockspaces are using this node
182 * this refcount is necessary to determine if the
183 * node wants to disconnect.
187 /* not protected by srcu, node_hash lifetime */
190 struct hlist_node hlist;
195 const struct dlm_header *inner_hd;
196 struct midcomms_node *node;
197 struct dlm_opts *opts;
202 void (*ack_rcv)(struct midcomms_node *node);
204 /* get_mhandle/commit srcu idx exchange */
207 struct list_head list;
211 static struct hlist_head node_hash[CONN_HASH_SIZE];
212 static DEFINE_SPINLOCK(nodes_lock);
213 DEFINE_STATIC_SRCU(nodes_srcu);
215 /* This mutex prevents that midcomms_close() is running while
216 * stop() or remove(). As I experienced invalid memory access
217 * behaviours when DLM_DEBUG_FENCE_TERMINATION is enabled and
218 * resetting machines. I will end in some double deletion in nodes
221 static DEFINE_MUTEX(close_lock);
223 static inline const char *dlm_state_str(int state)
228 case DLM_ESTABLISHED:
229 return "ESTABLISHED";
245 const char *dlm_midcomms_state(struct midcomms_node *node)
247 return dlm_state_str(node->state);
250 unsigned long dlm_midcomms_flags(struct midcomms_node *node)
255 int dlm_midcomms_send_queue_cnt(struct midcomms_node *node)
257 return atomic_read(&node->send_queue_cnt);
260 uint32_t dlm_midcomms_version(struct midcomms_node *node)
262 return node->version;
265 static struct midcomms_node *__find_node(int nodeid, int r)
267 struct midcomms_node *node;
269 hlist_for_each_entry_rcu(node, &node_hash[r], hlist) {
270 if (node->nodeid == nodeid)
277 static void dlm_mhandle_release(struct rcu_head *rcu)
279 struct dlm_mhandle *mh = container_of(rcu, struct dlm_mhandle, rcu);
281 dlm_lowcomms_put_msg(mh->msg);
285 static void dlm_mhandle_delete(struct midcomms_node *node,
286 struct dlm_mhandle *mh)
288 list_del_rcu(&mh->list);
289 atomic_dec(&node->send_queue_cnt);
290 call_rcu(&mh->rcu, dlm_mhandle_release);
293 static void dlm_send_queue_flush(struct midcomms_node *node)
295 struct dlm_mhandle *mh;
297 pr_debug("flush midcomms send queue of node %d\n", node->nodeid);
300 spin_lock(&node->send_queue_lock);
301 list_for_each_entry_rcu(mh, &node->send_queue, list) {
302 dlm_mhandle_delete(node, mh);
304 spin_unlock(&node->send_queue_lock);
308 static void midcomms_node_reset(struct midcomms_node *node)
310 pr_debug("reset node %d\n", node->nodeid);
312 node->seq_next = DLM_SEQ_INIT;
313 node->seq_send = DLM_SEQ_INIT;
314 node->version = DLM_VERSION_NOT_SET;
317 dlm_send_queue_flush(node);
318 node->state = DLM_CLOSED;
319 wake_up(&node->shutdown_wait);
322 static struct midcomms_node *nodeid2node(int nodeid, gfp_t alloc)
324 struct midcomms_node *node, *tmp;
325 int r = nodeid_hash(nodeid);
327 node = __find_node(nodeid, r);
331 node = kmalloc(sizeof(*node), alloc);
335 node->nodeid = nodeid;
336 spin_lock_init(&node->state_lock);
337 spin_lock_init(&node->send_queue_lock);
338 atomic_set(&node->send_queue_cnt, 0);
339 INIT_LIST_HEAD(&node->send_queue);
340 init_waitqueue_head(&node->shutdown_wait);
342 midcomms_node_reset(node);
344 spin_lock(&nodes_lock);
345 /* check again if there was somebody else
346 * earlier here to add the node
348 tmp = __find_node(nodeid, r);
350 spin_unlock(&nodes_lock);
355 hlist_add_head_rcu(&node->hlist, &node_hash[r]);
356 spin_unlock(&nodes_lock);
358 node->debugfs = dlm_create_debug_comms_file(nodeid, node);
362 static int dlm_send_ack(int nodeid, uint32_t seq)
364 int mb_len = sizeof(struct dlm_header);
365 struct dlm_header *m_header;
369 msg = dlm_lowcomms_new_msg(nodeid, mb_len, GFP_NOFS, &ppc,
374 m_header = (struct dlm_header *)ppc;
376 m_header->h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
377 m_header->h_nodeid = dlm_our_nodeid();
378 m_header->h_length = mb_len;
379 m_header->h_cmd = DLM_ACK;
380 m_header->u.h_seq = seq;
382 header_out(m_header);
383 dlm_lowcomms_commit_msg(msg);
384 dlm_lowcomms_put_msg(msg);
389 static int dlm_send_fin(struct midcomms_node *node,
390 void (*ack_rcv)(struct midcomms_node *node))
392 int mb_len = sizeof(struct dlm_header);
393 struct dlm_header *m_header;
394 struct dlm_mhandle *mh;
397 mh = dlm_midcomms_get_mhandle(node->nodeid, mb_len, GFP_NOFS, &ppc);
401 mh->ack_rcv = ack_rcv;
403 m_header = (struct dlm_header *)ppc;
405 m_header->h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
406 m_header->h_nodeid = dlm_our_nodeid();
407 m_header->h_length = mb_len;
408 m_header->h_cmd = DLM_FIN;
410 header_out(m_header);
412 pr_debug("sending fin msg to node %d\n", node->nodeid);
413 dlm_midcomms_commit_mhandle(mh);
414 set_bit(DLM_NODE_FLAG_STOP_TX, &node->flags);
419 static void dlm_receive_ack(struct midcomms_node *node, uint32_t seq)
421 struct dlm_mhandle *mh;
424 list_for_each_entry_rcu(mh, &node->send_queue, list) {
425 if (before(mh->seq, seq)) {
429 /* send queue should be ordered */
434 spin_lock(&node->send_queue_lock);
435 list_for_each_entry_rcu(mh, &node->send_queue, list) {
436 if (before(mh->seq, seq)) {
437 dlm_mhandle_delete(node, mh);
439 /* send queue should be ordered */
443 spin_unlock(&node->send_queue_lock);
447 static void dlm_pas_fin_ack_rcv(struct midcomms_node *node)
449 spin_lock(&node->state_lock);
450 pr_debug("receive passive fin ack from node %d with state %s\n",
451 node->nodeid, dlm_state_str(node->state));
453 switch (node->state) {
456 midcomms_node_reset(node);
459 /* not valid but somehow we got what we want */
460 wake_up(&node->shutdown_wait);
463 spin_unlock(&node->state_lock);
464 log_print("%s: unexpected state: %d\n",
465 __func__, node->state);
469 spin_unlock(&node->state_lock);
472 static void dlm_midcomms_receive_buffer(union dlm_packet *p,
473 struct midcomms_node *node,
476 if (seq == node->seq_next) {
479 switch (p->header.h_cmd) {
481 /* send ack before fin */
482 dlm_send_ack(node->nodeid, node->seq_next);
484 spin_lock(&node->state_lock);
485 pr_debug("receive fin msg from node %d with state %s\n",
486 node->nodeid, dlm_state_str(node->state));
488 switch (node->state) {
489 case DLM_ESTABLISHED:
490 node->state = DLM_CLOSE_WAIT;
491 pr_debug("switch node %d to state %s\n",
492 node->nodeid, dlm_state_str(node->state));
493 /* passive shutdown DLM_LAST_ACK case 1
494 * additional we check if the node is used by
495 * cluster manager events at all.
497 if (node->users == 0) {
498 node->state = DLM_LAST_ACK;
499 pr_debug("switch node %d to state %s case 1\n",
500 node->nodeid, dlm_state_str(node->state));
501 spin_unlock(&node->state_lock);
506 node->state = DLM_CLOSING;
507 pr_debug("switch node %d to state %s\n",
508 node->nodeid, dlm_state_str(node->state));
511 midcomms_node_reset(node);
512 pr_debug("switch node %d to state %s\n",
513 node->nodeid, dlm_state_str(node->state));
514 wake_up(&node->shutdown_wait);
517 /* probably remove_member caught it, do nothing */
520 spin_unlock(&node->state_lock);
521 log_print("%s: unexpected state: %d\n",
522 __func__, node->state);
526 spin_unlock(&node->state_lock);
528 set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
531 WARN_ON(test_bit(DLM_NODE_FLAG_STOP_RX, &node->flags));
532 dlm_receive_buffer(p, node->nodeid);
533 set_bit(DLM_NODE_ULP_DELIVERED, &node->flags);
537 /* retry to ack message which we already have by sending back
538 * current node->seq_next number as ack.
540 if (seq < node->seq_next)
541 dlm_send_ack(node->nodeid, node->seq_next);
543 log_print_ratelimited("ignore dlm msg because seq mismatch, seq: %u, expected: %u, nodeid: %d",
544 seq, node->seq_next, node->nodeid);
550 set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
551 dlm_send_fin(node, dlm_pas_fin_ack_rcv);
554 static struct midcomms_node *
555 dlm_midcomms_recv_node_lookup(int nodeid, const union dlm_packet *p,
556 uint16_t msglen, int (*cb)(struct midcomms_node *node))
558 struct midcomms_node *node = NULL;
559 gfp_t allocation = 0;
562 switch (p->header.h_cmd) {
564 if (msglen < sizeof(struct dlm_rcom)) {
565 log_print("rcom msg too small: %u, will skip this message from node %d",
570 switch (le32_to_cpu(p->rcom.rc_type)) {
573 case DLM_RCOM_NAMES_REPLY:
575 case DLM_RCOM_STATUS:
577 case DLM_RCOM_STATUS_REPLY:
578 node = nodeid2node(nodeid, 0);
580 spin_lock(&node->state_lock);
581 if (node->state != DLM_ESTABLISHED)
582 pr_debug("receive begin RCOM msg from node %d with state %s\n",
583 node->nodeid, dlm_state_str(node->state));
585 switch (node->state) {
587 node->state = DLM_ESTABLISHED;
588 pr_debug("switch node %d to state %s\n",
589 node->nodeid, dlm_state_str(node->state));
591 case DLM_ESTABLISHED:
594 /* some invalid state passive shutdown
595 * was failed, we try to reset and
596 * hope it will go on.
598 log_print("reset node %d because shutdown stuck",
601 midcomms_node_reset(node);
602 node->state = DLM_ESTABLISHED;
605 spin_unlock(&node->state_lock);
608 allocation = GFP_NOFS;
619 node = nodeid2node(nodeid, allocation);
621 switch (p->header.h_cmd) {
623 if (msglen < sizeof(struct dlm_opts)) {
624 log_print("opts msg too small: %u, will skip this message from node %d",
629 log_print_ratelimited("received dlm opts message nextcmd %d from node %d in an invalid sequence",
630 p->opts.o_nextcmd, nodeid);
633 log_print_ratelimited("received dlm message cmd %d from node %d in an invalid sequence",
634 p->header.h_cmd, nodeid);
648 static int dlm_midcomms_version_check_3_2(struct midcomms_node *node)
650 switch (node->version) {
651 case DLM_VERSION_NOT_SET:
652 node->version = DLM_VERSION_3_2;
653 log_print("version 0x%08x for node %d detected", DLM_VERSION_3_2,
656 case DLM_VERSION_3_2:
659 log_print_ratelimited("version mismatch detected, assumed 0x%08x but node %d has 0x%08x",
660 DLM_VERSION_3_2, node->nodeid, node->version);
667 static int dlm_opts_check_msglen(union dlm_packet *p, uint16_t msglen, int nodeid)
671 /* we only trust outer header msglen because
672 * it's checked against receive buffer length.
674 if (len < sizeof(struct dlm_opts))
676 len -= sizeof(struct dlm_opts);
678 if (len < le16_to_cpu(p->opts.o_optlen))
680 len -= le16_to_cpu(p->opts.o_optlen);
682 switch (p->opts.o_nextcmd) {
684 if (len < sizeof(struct dlm_header)) {
685 log_print("fin too small: %d, will skip this message from node %d",
692 if (len < sizeof(struct dlm_message)) {
693 log_print("msg too small: %d, will skip this message from node %d",
700 if (len < sizeof(struct dlm_rcom)) {
701 log_print("rcom msg too small: %d, will skip this message from node %d",
708 log_print("unsupported o_nextcmd received: %u, will skip this message from node %d",
709 p->opts.o_nextcmd, nodeid);
716 static void dlm_midcomms_receive_buffer_3_2(union dlm_packet *p, int nodeid)
718 uint16_t msglen = le16_to_cpu(p->header.h_length);
719 struct midcomms_node *node;
723 idx = srcu_read_lock(&nodes_srcu);
724 node = dlm_midcomms_recv_node_lookup(nodeid, p, msglen,
725 dlm_midcomms_version_check_3_2);
729 switch (p->header.h_cmd) {
731 /* these rcom message we use to determine version.
732 * they have their own retransmission handling and
733 * are the first messages of dlm.
735 * length already checked.
737 switch (le32_to_cpu(p->rcom.rc_type)) {
740 case DLM_RCOM_NAMES_REPLY:
742 case DLM_RCOM_STATUS:
744 case DLM_RCOM_STATUS_REPLY:
747 log_print("unsupported rcom type received: %u, will skip this message from node %d",
748 le32_to_cpu(p->rcom.rc_type), nodeid);
752 WARN_ON(test_bit(DLM_NODE_FLAG_STOP_RX, &node->flags));
753 dlm_receive_buffer(p, nodeid);
756 seq = le32_to_cpu(p->header.u.h_seq);
758 ret = dlm_opts_check_msglen(p, msglen, nodeid);
760 log_print("opts msg too small: %u, will skip this message from node %d",
765 p = (union dlm_packet *)((unsigned char *)p->opts.o_opts +
766 le16_to_cpu(p->opts.o_optlen));
768 /* recheck inner msglen just if it's not garbage */
769 msglen = le16_to_cpu(p->header.h_length);
770 switch (p->header.h_cmd) {
772 if (msglen < sizeof(struct dlm_rcom)) {
773 log_print("inner rcom msg too small: %u, will skip this message from node %d",
780 if (msglen < sizeof(struct dlm_message)) {
781 log_print("inner msg too small: %u, will skip this message from node %d",
788 if (msglen < sizeof(struct dlm_header)) {
789 log_print("inner fin too small: %u, will skip this message from node %d",
796 log_print("unsupported inner h_cmd received: %u, will skip this message from node %d",
801 dlm_midcomms_receive_buffer(p, node, seq);
804 seq = le32_to_cpu(p->header.u.h_seq);
805 dlm_receive_ack(node, seq);
808 log_print("unsupported h_cmd received: %u, will skip this message from node %d",
809 p->header.h_cmd, nodeid);
814 srcu_read_unlock(&nodes_srcu, idx);
817 static int dlm_midcomms_version_check_3_1(struct midcomms_node *node)
819 switch (node->version) {
820 case DLM_VERSION_NOT_SET:
821 node->version = DLM_VERSION_3_1;
822 log_print("version 0x%08x for node %d detected", DLM_VERSION_3_1,
825 case DLM_VERSION_3_1:
828 log_print_ratelimited("version mismatch detected, assumed 0x%08x but node %d has 0x%08x",
829 DLM_VERSION_3_1, node->nodeid, node->version);
836 static void dlm_midcomms_receive_buffer_3_1(union dlm_packet *p, int nodeid)
838 uint16_t msglen = le16_to_cpu(p->header.h_length);
839 struct midcomms_node *node;
842 idx = srcu_read_lock(&nodes_srcu);
843 node = dlm_midcomms_recv_node_lookup(nodeid, p, msglen,
844 dlm_midcomms_version_check_3_1);
846 srcu_read_unlock(&nodes_srcu, idx);
849 srcu_read_unlock(&nodes_srcu, idx);
851 switch (p->header.h_cmd) {
853 /* length already checked */
856 if (msglen < sizeof(struct dlm_message)) {
857 log_print("msg too small: %u, will skip this message from node %d",
864 log_print("unsupported h_cmd received: %u, will skip this message from node %d",
865 p->header.h_cmd, nodeid);
869 dlm_receive_buffer(p, nodeid);
873 * 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 /* no message should be more than DLM_MAX_SOCKET_BUFSIZE or
888 * less than dlm_header size.
890 * Some messages does not have a 8 byte length boundary yet
891 * which can occur in a unaligned memory access of some dlm
892 * messages. However this problem need to be fixed at the
893 * sending side, for now it seems nobody run into architecture
894 * related issues yet but it slows down some processing.
895 * Fixing this issue should be scheduled in future by doing
896 * the next major version bump.
898 msglen = le16_to_cpu(hd->h_length);
899 if (msglen > DLM_MAX_SOCKET_BUFSIZE ||
900 msglen < sizeof(struct dlm_header)) {
901 log_print("received invalid length header: %u from node %d, will abort message parsing",
906 /* caller will take care that leftover
907 * will be parsed next call with more data
912 switch (le32_to_cpu(hd->h_version)) {
913 case DLM_VERSION_3_1:
914 dlm_midcomms_receive_buffer_3_1((union dlm_packet *)ptr, nodeid);
916 case DLM_VERSION_3_2:
917 dlm_midcomms_receive_buffer_3_2((union dlm_packet *)ptr, nodeid);
920 log_print("received invalid version header: %u from node %d, will skip this message",
921 le32_to_cpu(hd->h_version), nodeid);
933 void dlm_midcomms_receive_done(int nodeid)
935 struct midcomms_node *node;
938 idx = srcu_read_lock(&nodes_srcu);
939 node = nodeid2node(nodeid, 0);
941 srcu_read_unlock(&nodes_srcu, idx);
945 /* old protocol, we do nothing */
946 switch (node->version) {
947 case DLM_VERSION_3_2:
950 srcu_read_unlock(&nodes_srcu, idx);
954 /* do nothing if we didn't delivered stateful to ulp */
955 if (!test_and_clear_bit(DLM_NODE_ULP_DELIVERED,
957 srcu_read_unlock(&nodes_srcu, idx);
961 spin_lock(&node->state_lock);
962 /* we only ack if state is ESTABLISHED */
963 switch (node->state) {
964 case DLM_ESTABLISHED:
965 spin_unlock(&node->state_lock);
966 dlm_send_ack(node->nodeid, node->seq_next);
969 spin_unlock(&node->state_lock);
970 /* do nothing FIN has it's own ack send */
973 srcu_read_unlock(&nodes_srcu, idx);
976 void dlm_midcomms_unack_msg_resend(int nodeid)
978 struct midcomms_node *node;
979 struct dlm_mhandle *mh;
982 idx = srcu_read_lock(&nodes_srcu);
983 node = nodeid2node(nodeid, 0);
985 srcu_read_unlock(&nodes_srcu, idx);
989 /* old protocol, we don't support to retransmit on failure */
990 switch (node->version) {
991 case DLM_VERSION_3_2:
994 srcu_read_unlock(&nodes_srcu, idx);
999 list_for_each_entry_rcu(mh, &node->send_queue, list) {
1003 ret = dlm_lowcomms_resend_msg(mh->msg);
1005 log_print_ratelimited("retransmit dlm msg, seq %u, nodeid %d",
1006 mh->seq, node->nodeid);
1009 srcu_read_unlock(&nodes_srcu, idx);
1012 static void dlm_fill_opts_header(struct dlm_opts *opts, uint16_t inner_len,
1015 opts->o_header.h_cmd = DLM_OPTS;
1016 opts->o_header.h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
1017 opts->o_header.h_nodeid = dlm_our_nodeid();
1018 opts->o_header.h_length = DLM_MIDCOMMS_OPT_LEN + inner_len;
1019 opts->o_header.u.h_seq = seq;
1020 header_out(&opts->o_header);
1023 static void midcomms_new_msg_cb(struct dlm_mhandle *mh)
1025 atomic_inc(&mh->node->send_queue_cnt);
1027 spin_lock(&mh->node->send_queue_lock);
1028 list_add_tail_rcu(&mh->list, &mh->node->send_queue);
1029 spin_unlock(&mh->node->send_queue_lock);
1031 mh->seq = mh->node->seq_send++;
1034 static struct dlm_msg *dlm_midcomms_get_msg_3_2(struct dlm_mhandle *mh, int nodeid,
1035 int len, gfp_t allocation, char **ppc)
1037 struct dlm_opts *opts;
1038 struct dlm_msg *msg;
1040 msg = dlm_lowcomms_new_msg(nodeid, len + DLM_MIDCOMMS_OPT_LEN,
1041 allocation, ppc, midcomms_new_msg_cb, mh);
1045 opts = (struct dlm_opts *)*ppc;
1048 /* add possible options here */
1049 dlm_fill_opts_header(opts, len, mh->seq);
1051 *ppc += sizeof(*opts);
1052 mh->inner_hd = (const struct dlm_header *)*ppc;
1056 struct dlm_mhandle *dlm_midcomms_get_mhandle(int nodeid, int len,
1057 gfp_t allocation, char **ppc)
1059 struct midcomms_node *node;
1060 struct dlm_mhandle *mh;
1061 struct dlm_msg *msg;
1064 idx = srcu_read_lock(&nodes_srcu);
1065 node = nodeid2node(nodeid, 0);
1071 /* this is a bug, however we going on and hope it will be resolved */
1072 WARN_ON(test_bit(DLM_NODE_FLAG_STOP_TX, &node->flags));
1074 mh = kzalloc(sizeof(*mh), GFP_NOFS);
1081 switch (node->version) {
1082 case DLM_VERSION_3_1:
1083 msg = dlm_lowcomms_new_msg(nodeid, len, allocation, ppc,
1091 case DLM_VERSION_3_2:
1092 msg = dlm_midcomms_get_msg_3_2(mh, nodeid, len, allocation,
1108 /* keep in mind that is a must to call
1109 * dlm_midcomms_commit_msg() which releases
1110 * nodes_srcu using mh->idx which is assumed
1111 * here that the application will call it.
1116 srcu_read_unlock(&nodes_srcu, idx);
1120 static void dlm_midcomms_commit_msg_3_2(struct dlm_mhandle *mh)
1122 /* nexthdr chain for fast lookup */
1123 mh->opts->o_nextcmd = mh->inner_hd->h_cmd;
1124 mh->committed = true;
1125 dlm_lowcomms_commit_msg(mh->msg);
1128 void dlm_midcomms_commit_mhandle(struct dlm_mhandle *mh)
1130 switch (mh->node->version) {
1131 case DLM_VERSION_3_1:
1132 srcu_read_unlock(&nodes_srcu, mh->idx);
1134 dlm_lowcomms_commit_msg(mh->msg);
1135 dlm_lowcomms_put_msg(mh->msg);
1136 /* mh is not part of rcu list in this case */
1139 case DLM_VERSION_3_2:
1140 dlm_midcomms_commit_msg_3_2(mh);
1141 srcu_read_unlock(&nodes_srcu, mh->idx);
1144 srcu_read_unlock(&nodes_srcu, mh->idx);
1150 int dlm_midcomms_start(void)
1154 for (i = 0; i < CONN_HASH_SIZE; i++)
1155 INIT_HLIST_HEAD(&node_hash[i]);
1157 return dlm_lowcomms_start();
1160 static void dlm_act_fin_ack_rcv(struct midcomms_node *node)
1162 spin_lock(&node->state_lock);
1163 pr_debug("receive active fin ack from node %d with state %s\n",
1164 node->nodeid, dlm_state_str(node->state));
1166 switch (node->state) {
1168 node->state = DLM_FIN_WAIT2;
1169 pr_debug("switch node %d to state %s\n",
1170 node->nodeid, dlm_state_str(node->state));
1173 midcomms_node_reset(node);
1174 pr_debug("switch node %d to state %s\n",
1175 node->nodeid, dlm_state_str(node->state));
1176 wake_up(&node->shutdown_wait);
1179 /* not valid but somehow we got what we want */
1180 wake_up(&node->shutdown_wait);
1183 spin_unlock(&node->state_lock);
1184 log_print("%s: unexpected state: %d\n",
1185 __func__, node->state);
1189 spin_unlock(&node->state_lock);
1192 void dlm_midcomms_add_member(int nodeid)
1194 struct midcomms_node *node;
1197 if (nodeid == dlm_our_nodeid())
1200 idx = srcu_read_lock(&nodes_srcu);
1201 node = nodeid2node(nodeid, GFP_NOFS);
1203 srcu_read_unlock(&nodes_srcu, idx);
1207 spin_lock(&node->state_lock);
1209 pr_debug("receive add member from node %d with state %s\n",
1210 node->nodeid, dlm_state_str(node->state));
1211 switch (node->state) {
1212 case DLM_ESTABLISHED:
1215 node->state = DLM_ESTABLISHED;
1216 pr_debug("switch node %d to state %s\n",
1217 node->nodeid, dlm_state_str(node->state));
1220 /* some invalid state passive shutdown
1221 * was failed, we try to reset and
1222 * hope it will go on.
1224 log_print("reset node %d because shutdown stuck",
1227 midcomms_node_reset(node);
1228 node->state = DLM_ESTABLISHED;
1234 pr_debug("users inc count %d\n", node->users);
1235 spin_unlock(&node->state_lock);
1237 srcu_read_unlock(&nodes_srcu, idx);
1240 void dlm_midcomms_remove_member(int nodeid)
1242 struct midcomms_node *node;
1245 if (nodeid == dlm_our_nodeid())
1248 idx = srcu_read_lock(&nodes_srcu);
1249 node = nodeid2node(nodeid, 0);
1251 srcu_read_unlock(&nodes_srcu, idx);
1255 spin_lock(&node->state_lock);
1257 pr_debug("users dec count %d\n", node->users);
1259 /* hitting users count to zero means the
1260 * other side is running dlm_midcomms_stop()
1261 * we meet us to have a clean disconnect.
1263 if (node->users == 0) {
1264 pr_debug("receive remove member from node %d with state %s\n",
1265 node->nodeid, dlm_state_str(node->state));
1266 switch (node->state) {
1267 case DLM_ESTABLISHED:
1269 case DLM_CLOSE_WAIT:
1270 /* passive shutdown DLM_LAST_ACK case 2 */
1271 node->state = DLM_LAST_ACK;
1272 spin_unlock(&node->state_lock);
1274 pr_debug("switch node %d to state %s case 2\n",
1275 node->nodeid, dlm_state_str(node->state));
1278 /* probably receive fin caught it, do nothing */
1281 /* already gone, do nothing */
1284 log_print("%s: unexpected state: %d\n",
1285 __func__, node->state);
1289 spin_unlock(&node->state_lock);
1291 srcu_read_unlock(&nodes_srcu, idx);
1295 set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
1296 dlm_send_fin(node, dlm_pas_fin_ack_rcv);
1297 srcu_read_unlock(&nodes_srcu, idx);
1300 static void midcomms_node_release(struct rcu_head *rcu)
1302 struct midcomms_node *node = container_of(rcu, struct midcomms_node, rcu);
1304 WARN_ON(atomic_read(&node->send_queue_cnt));
1308 static void midcomms_shutdown(struct midcomms_node *node)
1312 /* old protocol, we don't wait for pending operations */
1313 switch (node->version) {
1314 case DLM_VERSION_3_2:
1320 spin_lock(&node->state_lock);
1321 pr_debug("receive active shutdown for node %d with state %s\n",
1322 node->nodeid, dlm_state_str(node->state));
1323 switch (node->state) {
1324 case DLM_ESTABLISHED:
1325 node->state = DLM_FIN_WAIT1;
1326 pr_debug("switch node %d to state %s case 2\n",
1327 node->nodeid, dlm_state_str(node->state));
1330 /* we have what we want */
1331 spin_unlock(&node->state_lock);
1334 /* busy to enter DLM_FIN_WAIT1, wait until passive
1335 * done in shutdown_wait to enter DLM_CLOSED.
1339 spin_unlock(&node->state_lock);
1341 if (node->state == DLM_FIN_WAIT1) {
1342 dlm_send_fin(node, dlm_act_fin_ack_rcv);
1344 if (DLM_DEBUG_FENCE_TERMINATION)
1348 /* wait for other side dlm + fin */
1349 ret = wait_event_timeout(node->shutdown_wait,
1350 node->state == DLM_CLOSED ||
1351 test_bit(DLM_NODE_FLAG_CLOSE, &node->flags),
1352 DLM_SHUTDOWN_TIMEOUT);
1353 if (!ret || test_bit(DLM_NODE_FLAG_CLOSE, &node->flags)) {
1354 pr_debug("active shutdown timed out for node %d with state %s\n",
1355 node->nodeid, dlm_state_str(node->state));
1356 midcomms_node_reset(node);
1360 pr_debug("active shutdown done for node %d with state %s\n",
1361 node->nodeid, dlm_state_str(node->state));
1364 void dlm_midcomms_shutdown(void)
1366 struct midcomms_node *node;
1369 mutex_lock(&close_lock);
1370 idx = srcu_read_lock(&nodes_srcu);
1371 for (i = 0; i < CONN_HASH_SIZE; i++) {
1372 hlist_for_each_entry_rcu(node, &node_hash[i], hlist) {
1373 midcomms_shutdown(node);
1375 dlm_delete_debug_comms_file(node->debugfs);
1377 spin_lock(&nodes_lock);
1378 hlist_del_rcu(&node->hlist);
1379 spin_unlock(&nodes_lock);
1381 call_srcu(&nodes_srcu, &node->rcu, midcomms_node_release);
1384 srcu_read_unlock(&nodes_srcu, idx);
1385 mutex_unlock(&close_lock);
1387 dlm_lowcomms_shutdown();
1390 int dlm_midcomms_close(int nodeid)
1392 struct midcomms_node *node;
1395 if (nodeid == dlm_our_nodeid())
1398 idx = srcu_read_lock(&nodes_srcu);
1399 /* Abort pending close/remove operation */
1400 node = nodeid2node(nodeid, 0);
1402 /* let shutdown waiters leave */
1403 set_bit(DLM_NODE_FLAG_CLOSE, &node->flags);
1404 wake_up(&node->shutdown_wait);
1406 srcu_read_unlock(&nodes_srcu, idx);
1408 synchronize_srcu(&nodes_srcu);
1410 idx = srcu_read_lock(&nodes_srcu);
1411 mutex_lock(&close_lock);
1412 node = nodeid2node(nodeid, 0);
1414 mutex_unlock(&close_lock);
1415 srcu_read_unlock(&nodes_srcu, idx);
1416 return dlm_lowcomms_close(nodeid);
1419 ret = dlm_lowcomms_close(nodeid);
1420 spin_lock(&node->state_lock);
1421 midcomms_node_reset(node);
1422 spin_unlock(&node->state_lock);
1423 srcu_read_unlock(&nodes_srcu, idx);
1424 mutex_unlock(&close_lock);