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 "lockspace.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 /* 3 minutes wait to sync ending of dlm */
149 #define DLM_SHUTDOWN_TIMEOUT msecs_to_jiffies(3 * 60 * 1000)
150 #define DLM_VERSION_NOT_SET 0
152 struct midcomms_node {
157 /* These queues are unbound because we cannot drop any message in dlm.
158 * We could send a fence signal for a specific node to the cluster
159 * manager if queues hits some maximum value, however this handling
162 struct list_head send_queue;
163 spinlock_t send_queue_lock;
164 atomic_t send_queue_cnt;
165 #define DLM_NODE_FLAG_CLOSE 1
166 #define DLM_NODE_FLAG_STOP_TX 2
167 #define DLM_NODE_FLAG_STOP_RX 3
168 #define DLM_NODE_ULP_DELIVERED 4
170 wait_queue_head_t shutdown_wait;
172 /* dlm tcp termination state */
174 #define DLM_ESTABLISHED 2
175 #define DLM_FIN_WAIT1 3
176 #define DLM_FIN_WAIT2 4
177 #define DLM_CLOSE_WAIT 5
178 #define DLM_LAST_ACK 6
179 #define DLM_CLOSING 7
181 spinlock_t state_lock;
183 /* counts how many lockspaces are using this node
184 * this refcount is necessary to determine if the
185 * node wants to disconnect.
189 /* not protected by srcu, node_hash lifetime */
192 struct hlist_node hlist;
197 const struct dlm_header *inner_hd;
198 struct midcomms_node *node;
199 struct dlm_opts *opts;
204 void (*ack_rcv)(struct midcomms_node *node);
206 /* get_mhandle/commit srcu idx exchange */
209 struct list_head list;
213 static struct hlist_head node_hash[CONN_HASH_SIZE];
214 static DEFINE_SPINLOCK(nodes_lock);
215 DEFINE_STATIC_SRCU(nodes_srcu);
217 /* This mutex prevents that midcomms_close() is running while
218 * stop() or remove(). As I experienced invalid memory access
219 * behaviours when DLM_DEBUG_FENCE_TERMINATION is enabled and
220 * resetting machines. I will end in some double deletion in nodes
223 static DEFINE_MUTEX(close_lock);
225 struct kmem_cache *dlm_midcomms_cache_create(void)
227 return kmem_cache_create("dlm_mhandle", sizeof(struct dlm_mhandle),
231 static inline const char *dlm_state_str(int state)
236 case DLM_ESTABLISHED:
237 return "ESTABLISHED";
253 const char *dlm_midcomms_state(struct midcomms_node *node)
255 return dlm_state_str(node->state);
258 unsigned long dlm_midcomms_flags(struct midcomms_node *node)
263 int dlm_midcomms_send_queue_cnt(struct midcomms_node *node)
265 return atomic_read(&node->send_queue_cnt);
268 uint32_t dlm_midcomms_version(struct midcomms_node *node)
270 return node->version;
273 static struct midcomms_node *__find_node(int nodeid, int r)
275 struct midcomms_node *node;
277 hlist_for_each_entry_rcu(node, &node_hash[r], hlist) {
278 if (node->nodeid == nodeid)
285 static void dlm_mhandle_release(struct rcu_head *rcu)
287 struct dlm_mhandle *mh = container_of(rcu, struct dlm_mhandle, rcu);
289 dlm_lowcomms_put_msg(mh->msg);
290 dlm_free_mhandle(mh);
293 static void dlm_mhandle_delete(struct midcomms_node *node,
294 struct dlm_mhandle *mh)
296 list_del_rcu(&mh->list);
297 atomic_dec(&node->send_queue_cnt);
298 call_rcu(&mh->rcu, dlm_mhandle_release);
301 static void dlm_send_queue_flush(struct midcomms_node *node)
303 struct dlm_mhandle *mh;
305 pr_debug("flush midcomms send queue of node %d\n", node->nodeid);
308 spin_lock(&node->send_queue_lock);
309 list_for_each_entry_rcu(mh, &node->send_queue, list) {
310 dlm_mhandle_delete(node, mh);
312 spin_unlock(&node->send_queue_lock);
316 static void midcomms_node_reset(struct midcomms_node *node)
318 pr_debug("reset node %d\n", node->nodeid);
320 node->seq_next = DLM_SEQ_INIT;
321 node->seq_send = DLM_SEQ_INIT;
322 node->version = DLM_VERSION_NOT_SET;
325 dlm_send_queue_flush(node);
326 node->state = DLM_CLOSED;
327 wake_up(&node->shutdown_wait);
330 static struct midcomms_node *nodeid2node(int nodeid, gfp_t alloc)
332 struct midcomms_node *node, *tmp;
333 int r = nodeid_hash(nodeid);
335 node = __find_node(nodeid, r);
339 node = kmalloc(sizeof(*node), alloc);
343 node->nodeid = nodeid;
344 spin_lock_init(&node->state_lock);
345 spin_lock_init(&node->send_queue_lock);
346 atomic_set(&node->send_queue_cnt, 0);
347 INIT_LIST_HEAD(&node->send_queue);
348 init_waitqueue_head(&node->shutdown_wait);
350 midcomms_node_reset(node);
352 spin_lock(&nodes_lock);
353 /* check again if there was somebody else
354 * earlier here to add the node
356 tmp = __find_node(nodeid, r);
358 spin_unlock(&nodes_lock);
363 hlist_add_head_rcu(&node->hlist, &node_hash[r]);
364 spin_unlock(&nodes_lock);
366 node->debugfs = dlm_create_debug_comms_file(nodeid, node);
370 static int dlm_send_ack(int nodeid, uint32_t seq)
372 int mb_len = sizeof(struct dlm_header);
373 struct dlm_header *m_header;
377 msg = dlm_lowcomms_new_msg(nodeid, mb_len, GFP_NOFS, &ppc,
382 m_header = (struct dlm_header *)ppc;
384 m_header->h_version = cpu_to_le32(DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
385 m_header->h_nodeid = cpu_to_le32(dlm_our_nodeid());
386 m_header->h_length = cpu_to_le16(mb_len);
387 m_header->h_cmd = DLM_ACK;
388 m_header->u.h_seq = cpu_to_le32(seq);
390 dlm_lowcomms_commit_msg(msg);
391 dlm_lowcomms_put_msg(msg);
396 static int dlm_send_fin(struct midcomms_node *node,
397 void (*ack_rcv)(struct midcomms_node *node))
399 int mb_len = sizeof(struct dlm_header);
400 struct dlm_header *m_header;
401 struct dlm_mhandle *mh;
404 mh = dlm_midcomms_get_mhandle(node->nodeid, mb_len, GFP_NOFS, &ppc);
408 mh->ack_rcv = ack_rcv;
410 m_header = (struct dlm_header *)ppc;
412 m_header->h_version = cpu_to_le32(DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
413 m_header->h_nodeid = cpu_to_le32(dlm_our_nodeid());
414 m_header->h_length = cpu_to_le16(mb_len);
415 m_header->h_cmd = DLM_FIN;
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) {
484 switch (p->header.h_cmd) {
486 /* send ack before fin */
487 dlm_send_ack(node->nodeid, node->seq_next);
489 spin_lock(&node->state_lock);
490 pr_debug("receive fin msg from node %d with state %s\n",
491 node->nodeid, dlm_state_str(node->state));
493 switch (node->state) {
494 case DLM_ESTABLISHED:
495 node->state = DLM_CLOSE_WAIT;
496 pr_debug("switch node %d to state %s\n",
497 node->nodeid, dlm_state_str(node->state));
498 /* passive shutdown DLM_LAST_ACK case 1
499 * additional we check if the node is used by
500 * cluster manager events at all.
502 if (node->users == 0) {
503 node->state = DLM_LAST_ACK;
504 pr_debug("switch node %d to state %s case 1\n",
505 node->nodeid, dlm_state_str(node->state));
506 spin_unlock(&node->state_lock);
511 node->state = DLM_CLOSING;
512 pr_debug("switch node %d to state %s\n",
513 node->nodeid, dlm_state_str(node->state));
516 midcomms_node_reset(node);
517 pr_debug("switch node %d to state %s\n",
518 node->nodeid, dlm_state_str(node->state));
519 wake_up(&node->shutdown_wait);
522 /* probably remove_member caught it, do nothing */
525 spin_unlock(&node->state_lock);
526 log_print("%s: unexpected state: %d\n",
527 __func__, node->state);
531 spin_unlock(&node->state_lock);
533 set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
536 WARN_ON(test_bit(DLM_NODE_FLAG_STOP_RX, &node->flags));
537 dlm_receive_buffer(p, node->nodeid);
538 set_bit(DLM_NODE_ULP_DELIVERED, &node->flags);
542 /* retry to ack message which we already have by sending back
543 * current node->seq_next number as ack.
545 if (seq < node->seq_next)
546 dlm_send_ack(node->nodeid, node->seq_next);
548 log_print_ratelimited("ignore dlm msg because seq mismatch, seq: %u, expected: %u, nodeid: %d",
549 seq, node->seq_next, node->nodeid);
555 set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
556 dlm_send_fin(node, dlm_pas_fin_ack_rcv);
559 static struct midcomms_node *
560 dlm_midcomms_recv_node_lookup(int nodeid, const union dlm_packet *p,
561 uint16_t msglen, int (*cb)(struct midcomms_node *node))
563 struct midcomms_node *node = NULL;
564 gfp_t allocation = 0;
567 switch (p->header.h_cmd) {
569 if (msglen < sizeof(struct dlm_rcom)) {
570 log_print("rcom msg too small: %u, will skip this message from node %d",
575 switch (p->rcom.rc_type) {
576 case cpu_to_le32(DLM_RCOM_NAMES):
578 case cpu_to_le32(DLM_RCOM_NAMES_REPLY):
580 case cpu_to_le32(DLM_RCOM_STATUS):
582 case cpu_to_le32(DLM_RCOM_STATUS_REPLY):
583 node = nodeid2node(nodeid, 0);
585 spin_lock(&node->state_lock);
586 if (node->state != DLM_ESTABLISHED)
587 pr_debug("receive begin RCOM msg from node %d with state %s\n",
588 node->nodeid, dlm_state_str(node->state));
590 switch (node->state) {
592 node->state = DLM_ESTABLISHED;
593 pr_debug("switch node %d to state %s\n",
594 node->nodeid, dlm_state_str(node->state));
596 case DLM_ESTABLISHED:
599 /* some invalid state passive shutdown
600 * was failed, we try to reset and
601 * hope it will go on.
603 log_print("reset node %d because shutdown stuck",
606 midcomms_node_reset(node);
607 node->state = DLM_ESTABLISHED;
610 spin_unlock(&node->state_lock);
613 allocation = GFP_NOFS;
624 node = nodeid2node(nodeid, allocation);
626 switch (p->header.h_cmd) {
628 if (msglen < sizeof(struct dlm_opts)) {
629 log_print("opts msg too small: %u, will skip this message from node %d",
634 log_print_ratelimited("received dlm opts message nextcmd %d from node %d in an invalid sequence",
635 p->opts.o_nextcmd, nodeid);
638 log_print_ratelimited("received dlm message cmd %d from node %d in an invalid sequence",
639 p->header.h_cmd, nodeid);
653 static int dlm_midcomms_version_check_3_2(struct midcomms_node *node)
655 switch (node->version) {
656 case DLM_VERSION_NOT_SET:
657 node->version = DLM_VERSION_3_2;
658 log_print("version 0x%08x for node %d detected", DLM_VERSION_3_2,
661 case DLM_VERSION_3_2:
664 log_print_ratelimited("version mismatch detected, assumed 0x%08x but node %d has 0x%08x",
665 DLM_VERSION_3_2, node->nodeid, node->version);
672 static int dlm_opts_check_msglen(union dlm_packet *p, uint16_t msglen, int nodeid)
676 /* we only trust outer header msglen because
677 * it's checked against receive buffer length.
679 if (len < sizeof(struct dlm_opts))
681 len -= sizeof(struct dlm_opts);
683 if (len < le16_to_cpu(p->opts.o_optlen))
685 len -= le16_to_cpu(p->opts.o_optlen);
687 switch (p->opts.o_nextcmd) {
689 if (len < sizeof(struct dlm_header)) {
690 log_print("fin too small: %d, will skip this message from node %d",
697 if (len < sizeof(struct dlm_message)) {
698 log_print("msg too small: %d, will skip this message from node %d",
705 if (len < sizeof(struct dlm_rcom)) {
706 log_print("rcom msg too small: %d, will skip this message from node %d",
713 log_print("unsupported o_nextcmd received: %u, will skip this message from node %d",
714 p->opts.o_nextcmd, nodeid);
721 static void dlm_midcomms_receive_buffer_3_2(union dlm_packet *p, int nodeid)
723 uint16_t msglen = le16_to_cpu(p->header.h_length);
724 struct midcomms_node *node;
728 idx = srcu_read_lock(&nodes_srcu);
729 node = dlm_midcomms_recv_node_lookup(nodeid, p, msglen,
730 dlm_midcomms_version_check_3_2);
734 switch (p->header.h_cmd) {
736 /* these rcom message we use to determine version.
737 * they have their own retransmission handling and
738 * are the first messages of dlm.
740 * length already checked.
742 switch (p->rcom.rc_type) {
743 case cpu_to_le32(DLM_RCOM_NAMES):
745 case cpu_to_le32(DLM_RCOM_NAMES_REPLY):
747 case cpu_to_le32(DLM_RCOM_STATUS):
749 case cpu_to_le32(DLM_RCOM_STATUS_REPLY):
752 log_print("unsupported rcom type received: %u, will skip this message from node %d",
753 le32_to_cpu(p->rcom.rc_type), nodeid);
757 WARN_ON(test_bit(DLM_NODE_FLAG_STOP_RX, &node->flags));
758 dlm_receive_buffer(p, nodeid);
761 seq = le32_to_cpu(p->header.u.h_seq);
763 ret = dlm_opts_check_msglen(p, msglen, nodeid);
765 log_print("opts msg too small: %u, will skip this message from node %d",
770 p = (union dlm_packet *)((unsigned char *)p->opts.o_opts +
771 le16_to_cpu(p->opts.o_optlen));
773 /* recheck inner msglen just if it's not garbage */
774 msglen = le16_to_cpu(p->header.h_length);
775 switch (p->header.h_cmd) {
777 if (msglen < sizeof(struct dlm_rcom)) {
778 log_print("inner rcom msg too small: %u, will skip this message from node %d",
785 if (msglen < sizeof(struct dlm_message)) {
786 log_print("inner msg too small: %u, will skip this message from node %d",
793 if (msglen < sizeof(struct dlm_header)) {
794 log_print("inner fin too small: %u, will skip this message from node %d",
801 log_print("unsupported inner h_cmd received: %u, will skip this message from node %d",
806 dlm_midcomms_receive_buffer(p, node, seq);
809 seq = le32_to_cpu(p->header.u.h_seq);
810 dlm_receive_ack(node, seq);
813 log_print("unsupported h_cmd received: %u, will skip this message from node %d",
814 p->header.h_cmd, nodeid);
819 srcu_read_unlock(&nodes_srcu, idx);
822 static int dlm_midcomms_version_check_3_1(struct midcomms_node *node)
824 switch (node->version) {
825 case DLM_VERSION_NOT_SET:
826 node->version = DLM_VERSION_3_1;
827 log_print("version 0x%08x for node %d detected", DLM_VERSION_3_1,
830 case DLM_VERSION_3_1:
833 log_print_ratelimited("version mismatch detected, assumed 0x%08x but node %d has 0x%08x",
834 DLM_VERSION_3_1, node->nodeid, node->version);
841 static void dlm_midcomms_receive_buffer_3_1(union dlm_packet *p, int nodeid)
843 uint16_t msglen = le16_to_cpu(p->header.h_length);
844 struct midcomms_node *node;
847 idx = srcu_read_lock(&nodes_srcu);
848 node = dlm_midcomms_recv_node_lookup(nodeid, p, msglen,
849 dlm_midcomms_version_check_3_1);
851 srcu_read_unlock(&nodes_srcu, idx);
854 srcu_read_unlock(&nodes_srcu, idx);
856 switch (p->header.h_cmd) {
858 /* length already checked */
861 if (msglen < sizeof(struct dlm_message)) {
862 log_print("msg too small: %u, will skip this message from node %d",
869 log_print("unsupported h_cmd received: %u, will skip this message from node %d",
870 p->header.h_cmd, nodeid);
874 dlm_receive_buffer(p, nodeid);
878 * Called from the low-level comms layer to process a buffer of
882 int dlm_process_incoming_buffer(int nodeid, unsigned char *buf, int len)
884 const unsigned char *ptr = buf;
885 const struct dlm_header *hd;
889 while (len >= sizeof(struct dlm_header)) {
890 hd = (struct dlm_header *)ptr;
892 /* no message should be more than DLM_MAX_SOCKET_BUFSIZE or
893 * less than dlm_header size.
895 * Some messages does not have a 8 byte length boundary yet
896 * which can occur in a unaligned memory access of some dlm
897 * messages. However this problem need to be fixed at the
898 * sending side, for now it seems nobody run into architecture
899 * related issues yet but it slows down some processing.
900 * Fixing this issue should be scheduled in future by doing
901 * the next major version bump.
903 msglen = le16_to_cpu(hd->h_length);
904 if (msglen > DLM_MAX_SOCKET_BUFSIZE ||
905 msglen < sizeof(struct dlm_header)) {
906 log_print("received invalid length header: %u from node %d, will abort message parsing",
911 /* caller will take care that leftover
912 * will be parsed next call with more data
917 switch (hd->h_version) {
918 case cpu_to_le32(DLM_VERSION_3_1):
919 dlm_midcomms_receive_buffer_3_1((union dlm_packet *)ptr, nodeid);
921 case cpu_to_le32(DLM_VERSION_3_2):
922 dlm_midcomms_receive_buffer_3_2((union dlm_packet *)ptr, nodeid);
925 log_print("received invalid version header: %u from node %d, will skip this message",
926 le32_to_cpu(hd->h_version), nodeid);
938 void dlm_midcomms_receive_done(int nodeid)
940 struct midcomms_node *node;
943 idx = srcu_read_lock(&nodes_srcu);
944 node = nodeid2node(nodeid, 0);
946 srcu_read_unlock(&nodes_srcu, idx);
950 /* old protocol, we do nothing */
951 switch (node->version) {
952 case DLM_VERSION_3_2:
955 srcu_read_unlock(&nodes_srcu, idx);
959 /* do nothing if we didn't delivered stateful to ulp */
960 if (!test_and_clear_bit(DLM_NODE_ULP_DELIVERED,
962 srcu_read_unlock(&nodes_srcu, idx);
966 spin_lock(&node->state_lock);
967 /* we only ack if state is ESTABLISHED */
968 switch (node->state) {
969 case DLM_ESTABLISHED:
970 spin_unlock(&node->state_lock);
971 dlm_send_ack(node->nodeid, node->seq_next);
974 spin_unlock(&node->state_lock);
975 /* do nothing FIN has it's own ack send */
978 srcu_read_unlock(&nodes_srcu, idx);
981 void dlm_midcomms_unack_msg_resend(int nodeid)
983 struct midcomms_node *node;
984 struct dlm_mhandle *mh;
987 idx = srcu_read_lock(&nodes_srcu);
988 node = nodeid2node(nodeid, 0);
990 srcu_read_unlock(&nodes_srcu, idx);
994 /* old protocol, we don't support to retransmit on failure */
995 switch (node->version) {
996 case DLM_VERSION_3_2:
999 srcu_read_unlock(&nodes_srcu, idx);
1004 list_for_each_entry_rcu(mh, &node->send_queue, list) {
1008 ret = dlm_lowcomms_resend_msg(mh->msg);
1010 log_print_ratelimited("retransmit dlm msg, seq %u, nodeid %d",
1011 mh->seq, node->nodeid);
1014 srcu_read_unlock(&nodes_srcu, idx);
1017 static void dlm_fill_opts_header(struct dlm_opts *opts, uint16_t inner_len,
1020 opts->o_header.h_cmd = DLM_OPTS;
1021 opts->o_header.h_version = cpu_to_le32(DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
1022 opts->o_header.h_nodeid = cpu_to_le32(dlm_our_nodeid());
1023 opts->o_header.h_length = cpu_to_le16(DLM_MIDCOMMS_OPT_LEN + inner_len);
1024 opts->o_header.u.h_seq = cpu_to_le32(seq);
1027 static void midcomms_new_msg_cb(void *data)
1029 struct dlm_mhandle *mh = data;
1031 atomic_inc(&mh->node->send_queue_cnt);
1033 spin_lock(&mh->node->send_queue_lock);
1034 list_add_tail_rcu(&mh->list, &mh->node->send_queue);
1035 spin_unlock(&mh->node->send_queue_lock);
1037 mh->seq = mh->node->seq_send++;
1040 static struct dlm_msg *dlm_midcomms_get_msg_3_2(struct dlm_mhandle *mh, int nodeid,
1041 int len, gfp_t allocation, char **ppc)
1043 struct dlm_opts *opts;
1044 struct dlm_msg *msg;
1046 msg = dlm_lowcomms_new_msg(nodeid, len + DLM_MIDCOMMS_OPT_LEN,
1047 allocation, ppc, midcomms_new_msg_cb, mh);
1051 opts = (struct dlm_opts *)*ppc;
1054 /* add possible options here */
1055 dlm_fill_opts_header(opts, len, mh->seq);
1057 *ppc += sizeof(*opts);
1058 mh->inner_hd = (const struct dlm_header *)*ppc;
1062 /* avoid false positive for nodes_srcu, unlock happens in
1063 * dlm_midcomms_commit_mhandle which is a must call if success
1066 struct dlm_mhandle *dlm_midcomms_get_mhandle(int nodeid, int len,
1067 gfp_t allocation, char **ppc)
1069 struct midcomms_node *node;
1070 struct dlm_mhandle *mh;
1071 struct dlm_msg *msg;
1074 idx = srcu_read_lock(&nodes_srcu);
1075 node = nodeid2node(nodeid, 0);
1081 /* this is a bug, however we going on and hope it will be resolved */
1082 WARN_ON(test_bit(DLM_NODE_FLAG_STOP_TX, &node->flags));
1084 mh = dlm_allocate_mhandle();
1088 mh->committed = false;
1093 switch (node->version) {
1094 case DLM_VERSION_3_1:
1095 msg = dlm_lowcomms_new_msg(nodeid, len, allocation, ppc,
1098 dlm_free_mhandle(mh);
1103 case DLM_VERSION_3_2:
1104 msg = dlm_midcomms_get_msg_3_2(mh, nodeid, len, allocation,
1107 dlm_free_mhandle(mh);
1113 dlm_free_mhandle(mh);
1120 /* keep in mind that is a must to call
1121 * dlm_midcomms_commit_msg() which releases
1122 * nodes_srcu using mh->idx which is assumed
1123 * here that the application will call it.
1128 srcu_read_unlock(&nodes_srcu, idx);
1133 static void dlm_midcomms_commit_msg_3_2(struct dlm_mhandle *mh)
1135 /* nexthdr chain for fast lookup */
1136 mh->opts->o_nextcmd = mh->inner_hd->h_cmd;
1137 mh->committed = true;
1138 dlm_lowcomms_commit_msg(mh->msg);
1141 /* avoid false positive for nodes_srcu, lock was happen in
1142 * dlm_midcomms_get_mhandle
1145 void dlm_midcomms_commit_mhandle(struct dlm_mhandle *mh)
1147 switch (mh->node->version) {
1148 case DLM_VERSION_3_1:
1149 srcu_read_unlock(&nodes_srcu, mh->idx);
1151 dlm_lowcomms_commit_msg(mh->msg);
1152 dlm_lowcomms_put_msg(mh->msg);
1153 /* mh is not part of rcu list in this case */
1154 dlm_free_mhandle(mh);
1156 case DLM_VERSION_3_2:
1157 dlm_midcomms_commit_msg_3_2(mh);
1158 srcu_read_unlock(&nodes_srcu, mh->idx);
1161 srcu_read_unlock(&nodes_srcu, mh->idx);
1168 int dlm_midcomms_start(void)
1172 for (i = 0; i < CONN_HASH_SIZE; i++)
1173 INIT_HLIST_HEAD(&node_hash[i]);
1175 return dlm_lowcomms_start();
1178 static void dlm_act_fin_ack_rcv(struct midcomms_node *node)
1180 spin_lock(&node->state_lock);
1181 pr_debug("receive active fin ack from node %d with state %s\n",
1182 node->nodeid, dlm_state_str(node->state));
1184 switch (node->state) {
1186 node->state = DLM_FIN_WAIT2;
1187 pr_debug("switch node %d to state %s\n",
1188 node->nodeid, dlm_state_str(node->state));
1191 midcomms_node_reset(node);
1192 pr_debug("switch node %d to state %s\n",
1193 node->nodeid, dlm_state_str(node->state));
1194 wake_up(&node->shutdown_wait);
1197 /* not valid but somehow we got what we want */
1198 wake_up(&node->shutdown_wait);
1201 spin_unlock(&node->state_lock);
1202 log_print("%s: unexpected state: %d\n",
1203 __func__, node->state);
1207 spin_unlock(&node->state_lock);
1210 void dlm_midcomms_add_member(int nodeid)
1212 struct midcomms_node *node;
1215 if (nodeid == dlm_our_nodeid())
1218 idx = srcu_read_lock(&nodes_srcu);
1219 node = nodeid2node(nodeid, GFP_NOFS);
1221 srcu_read_unlock(&nodes_srcu, idx);
1225 spin_lock(&node->state_lock);
1227 pr_debug("receive add member from node %d with state %s\n",
1228 node->nodeid, dlm_state_str(node->state));
1229 switch (node->state) {
1230 case DLM_ESTABLISHED:
1233 node->state = DLM_ESTABLISHED;
1234 pr_debug("switch node %d to state %s\n",
1235 node->nodeid, dlm_state_str(node->state));
1238 /* some invalid state passive shutdown
1239 * was failed, we try to reset and
1240 * hope it will go on.
1242 log_print("reset node %d because shutdown stuck",
1245 midcomms_node_reset(node);
1246 node->state = DLM_ESTABLISHED;
1252 pr_debug("node %d users inc count %d\n", nodeid, node->users);
1253 spin_unlock(&node->state_lock);
1255 srcu_read_unlock(&nodes_srcu, idx);
1258 void dlm_midcomms_remove_member(int nodeid)
1260 struct midcomms_node *node;
1263 if (nodeid == dlm_our_nodeid())
1266 idx = srcu_read_lock(&nodes_srcu);
1267 node = nodeid2node(nodeid, 0);
1269 srcu_read_unlock(&nodes_srcu, idx);
1273 spin_lock(&node->state_lock);
1275 pr_debug("node %d users dec count %d\n", nodeid, node->users);
1277 /* hitting users count to zero means the
1278 * other side is running dlm_midcomms_stop()
1279 * we meet us to have a clean disconnect.
1281 if (node->users == 0) {
1282 pr_debug("receive remove member from node %d with state %s\n",
1283 node->nodeid, dlm_state_str(node->state));
1284 switch (node->state) {
1285 case DLM_ESTABLISHED:
1287 case DLM_CLOSE_WAIT:
1288 /* passive shutdown DLM_LAST_ACK case 2 */
1289 node->state = DLM_LAST_ACK;
1290 spin_unlock(&node->state_lock);
1292 pr_debug("switch node %d to state %s case 2\n",
1293 node->nodeid, dlm_state_str(node->state));
1296 /* probably receive fin caught it, do nothing */
1299 /* already gone, do nothing */
1302 log_print("%s: unexpected state: %d\n",
1303 __func__, node->state);
1307 spin_unlock(&node->state_lock);
1309 srcu_read_unlock(&nodes_srcu, idx);
1313 set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
1314 dlm_send_fin(node, dlm_pas_fin_ack_rcv);
1315 srcu_read_unlock(&nodes_srcu, idx);
1318 static void midcomms_node_release(struct rcu_head *rcu)
1320 struct midcomms_node *node = container_of(rcu, struct midcomms_node, rcu);
1322 WARN_ON(atomic_read(&node->send_queue_cnt));
1326 static void midcomms_shutdown(struct midcomms_node *node)
1330 /* old protocol, we don't wait for pending operations */
1331 switch (node->version) {
1332 case DLM_VERSION_3_2:
1338 spin_lock(&node->state_lock);
1339 pr_debug("receive active shutdown for node %d with state %s\n",
1340 node->nodeid, dlm_state_str(node->state));
1341 switch (node->state) {
1342 case DLM_ESTABLISHED:
1343 node->state = DLM_FIN_WAIT1;
1344 pr_debug("switch node %d to state %s case 2\n",
1345 node->nodeid, dlm_state_str(node->state));
1348 /* we have what we want */
1349 spin_unlock(&node->state_lock);
1352 /* busy to enter DLM_FIN_WAIT1, wait until passive
1353 * done in shutdown_wait to enter DLM_CLOSED.
1357 spin_unlock(&node->state_lock);
1359 if (node->state == DLM_FIN_WAIT1) {
1360 dlm_send_fin(node, dlm_act_fin_ack_rcv);
1362 if (DLM_DEBUG_FENCE_TERMINATION)
1366 /* wait for other side dlm + fin */
1367 ret = wait_event_timeout(node->shutdown_wait,
1368 node->state == DLM_CLOSED ||
1369 test_bit(DLM_NODE_FLAG_CLOSE, &node->flags),
1370 DLM_SHUTDOWN_TIMEOUT);
1371 if (!ret || test_bit(DLM_NODE_FLAG_CLOSE, &node->flags)) {
1372 pr_debug("active shutdown timed out for node %d with state %s\n",
1373 node->nodeid, dlm_state_str(node->state));
1374 midcomms_node_reset(node);
1378 pr_debug("active shutdown done for node %d with state %s\n",
1379 node->nodeid, dlm_state_str(node->state));
1382 void dlm_midcomms_shutdown(void)
1384 struct midcomms_node *node;
1387 mutex_lock(&close_lock);
1388 idx = srcu_read_lock(&nodes_srcu);
1389 for (i = 0; i < CONN_HASH_SIZE; i++) {
1390 hlist_for_each_entry_rcu(node, &node_hash[i], hlist) {
1391 midcomms_shutdown(node);
1393 dlm_delete_debug_comms_file(node->debugfs);
1395 spin_lock(&nodes_lock);
1396 hlist_del_rcu(&node->hlist);
1397 spin_unlock(&nodes_lock);
1399 call_srcu(&nodes_srcu, &node->rcu, midcomms_node_release);
1402 srcu_read_unlock(&nodes_srcu, idx);
1403 mutex_unlock(&close_lock);
1405 dlm_lowcomms_shutdown();
1408 int dlm_midcomms_close(int nodeid)
1410 struct midcomms_node *node;
1413 if (nodeid == dlm_our_nodeid())
1416 dlm_stop_lockspaces_check();
1418 idx = srcu_read_lock(&nodes_srcu);
1419 /* Abort pending close/remove operation */
1420 node = nodeid2node(nodeid, 0);
1422 /* let shutdown waiters leave */
1423 set_bit(DLM_NODE_FLAG_CLOSE, &node->flags);
1424 wake_up(&node->shutdown_wait);
1426 srcu_read_unlock(&nodes_srcu, idx);
1428 synchronize_srcu(&nodes_srcu);
1430 idx = srcu_read_lock(&nodes_srcu);
1431 mutex_lock(&close_lock);
1432 node = nodeid2node(nodeid, 0);
1434 mutex_unlock(&close_lock);
1435 srcu_read_unlock(&nodes_srcu, idx);
1436 return dlm_lowcomms_close(nodeid);
1439 ret = dlm_lowcomms_close(nodeid);
1440 spin_lock(&node->state_lock);
1441 midcomms_node_reset(node);
1442 spin_unlock(&node->state_lock);
1443 srcu_read_unlock(&nodes_srcu, idx);
1444 mutex_unlock(&close_lock);
1449 /* debug functionality to send raw dlm msg from user space */
1450 struct dlm_rawmsg_data {
1451 struct midcomms_node *node;
1455 static void midcomms_new_rawmsg_cb(void *data)
1457 struct dlm_rawmsg_data *rd = data;
1458 struct dlm_header *h = rd->buf;
1460 switch (h->h_version) {
1461 case cpu_to_le32(DLM_VERSION_3_1):
1467 h->u.h_seq = cpu_to_le32(rd->node->seq_send++);
1476 int dlm_midcomms_rawmsg_send(struct midcomms_node *node, void *buf,
1479 struct dlm_rawmsg_data rd;
1480 struct dlm_msg *msg;
1486 msg = dlm_lowcomms_new_msg(node->nodeid, buflen, GFP_NOFS,
1487 &msgbuf, midcomms_new_rawmsg_cb, &rd);
1491 memcpy(msgbuf, buf, buflen);
1492 dlm_lowcomms_commit_msg(msg);