xref: /openbmc/linux/fs/dlm/midcomms.c (revision 47ebd031)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /******************************************************************************
3 *******************************************************************************
4 **
5 **  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
6 **  Copyright (C) 2004-2021 Red Hat, Inc.  All rights reserved.
7 **
8 **
9 *******************************************************************************
10 ******************************************************************************/
11 
12 /*
13  * midcomms.c
14  *
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.
18  *
19  * How it works:
20  *
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.
26  *
27  * How version detection works:
28  *
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.
39  *
40  * Termination:
41  *
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:
53  *
54  * 1. The cluster member was removed and we received a FIN
55  * OR
56  * 2. We received a FIN but the member was not removed yet
57  *
58  * One of these cases will do the CLOSE_WAIT to LAST_ACK change.
59  *
60  *
61  *                              +---------+
62  *                              | CLOSED  |
63  *                              +---------+
64  *                                   | add member/receive RCOM version
65  *                                   |            detection msg
66  *                                   V
67  *                              +---------+
68  *                              |  ESTAB  |
69  *                              +---------+
70  *                       CLOSE    |     |    rcv FIN
71  *                      -------   |     |    -------
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       -------------- |                 -------------- |
84  * |  -------              x       V                        x       V
85  *  \ snd ACK                 +---------+                   +---------+
86  *   ------------------------>| CLOSED  |                   | CLOSED  |
87  *                            +---------+                   +---------+
88  *
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.
93  *
94  * Future improvements:
95  *
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.
99  *
100  * Unaligned memory access:
101  *
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.
105  *
106  * Version detection:
107  *
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.
111  *
112  * Tail Size checking:
113  *
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.
117  *
118  * Fencing bad nodes:
119  *
120  * At timeout places or weird sequence number behaviours we should send
121  * a fencing request to the cluster manager.
122  */
123 
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.
128  *
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.
132  */
133 #define DLM_DEBUG_FENCE_TERMINATION	0
134 
135 #include <trace/events/dlm.h>
136 #include <net/tcp.h>
137 
138 #include "dlm_internal.h"
139 #include "lockspace.h"
140 #include "lowcomms.h"
141 #include "config.h"
142 #include "memory.h"
143 #include "lock.h"
144 #include "util.h"
145 #include "midcomms.h"
146 
147 /* init value for sequence numbers for testing purpose only e.g. overflows */
148 #define DLM_SEQ_INIT		0
149 /* 5 seconds wait to sync ending of dlm */
150 #define DLM_SHUTDOWN_TIMEOUT	msecs_to_jiffies(5000)
151 #define DLM_VERSION_NOT_SET	0
152 
153 struct midcomms_node {
154 	int nodeid;
155 	uint32_t version;
156 	uint32_t seq_send;
157 	uint32_t seq_next;
158 	/* These queues are unbound because we cannot drop any message in dlm.
159 	 * We could send a fence signal for a specific node to the cluster
160 	 * manager if queues hits some maximum value, however this handling
161 	 * not supported yet.
162 	 */
163 	struct list_head send_queue;
164 	spinlock_t send_queue_lock;
165 	atomic_t send_queue_cnt;
166 #define DLM_NODE_FLAG_CLOSE	1
167 #define DLM_NODE_FLAG_STOP_TX	2
168 #define DLM_NODE_FLAG_STOP_RX	3
169 #define DLM_NODE_ULP_DELIVERED	4
170 	unsigned long flags;
171 	wait_queue_head_t shutdown_wait;
172 
173 	/* dlm tcp termination state */
174 #define DLM_CLOSED	1
175 #define DLM_ESTABLISHED	2
176 #define DLM_FIN_WAIT1	3
177 #define DLM_FIN_WAIT2	4
178 #define DLM_CLOSE_WAIT	5
179 #define DLM_LAST_ACK	6
180 #define DLM_CLOSING	7
181 	int state;
182 	spinlock_t state_lock;
183 
184 	/* counts how many lockspaces are using this node
185 	 * this refcount is necessary to determine if the
186 	 * node wants to disconnect.
187 	 */
188 	int users;
189 
190 	/* not protected by srcu, node_hash lifetime */
191 	void *debugfs;
192 
193 	struct hlist_node hlist;
194 	struct rcu_head rcu;
195 };
196 
197 struct dlm_mhandle {
198 	const union dlm_packet *inner_p;
199 	struct midcomms_node *node;
200 	struct dlm_opts *opts;
201 	struct dlm_msg *msg;
202 	bool committed;
203 	uint32_t seq;
204 
205 	void (*ack_rcv)(struct midcomms_node *node);
206 
207 	/* get_mhandle/commit srcu idx exchange */
208 	int idx;
209 
210 	struct list_head list;
211 	struct rcu_head rcu;
212 };
213 
214 static struct hlist_head node_hash[CONN_HASH_SIZE];
215 static DEFINE_SPINLOCK(nodes_lock);
216 DEFINE_STATIC_SRCU(nodes_srcu);
217 
218 /* This mutex prevents that midcomms_close() is running while
219  * stop() or remove(). As I experienced invalid memory access
220  * behaviours when DLM_DEBUG_FENCE_TERMINATION is enabled and
221  * resetting machines. I will end in some double deletion in nodes
222  * datastructure.
223  */
224 static DEFINE_MUTEX(close_lock);
225 
226 struct kmem_cache *dlm_midcomms_cache_create(void)
227 {
228 	return kmem_cache_create("dlm_mhandle", sizeof(struct dlm_mhandle),
229 				 0, 0, NULL);
230 }
231 
232 static inline const char *dlm_state_str(int state)
233 {
234 	switch (state) {
235 	case DLM_CLOSED:
236 		return "CLOSED";
237 	case DLM_ESTABLISHED:
238 		return "ESTABLISHED";
239 	case DLM_FIN_WAIT1:
240 		return "FIN_WAIT1";
241 	case DLM_FIN_WAIT2:
242 		return "FIN_WAIT2";
243 	case DLM_CLOSE_WAIT:
244 		return "CLOSE_WAIT";
245 	case DLM_LAST_ACK:
246 		return "LAST_ACK";
247 	case DLM_CLOSING:
248 		return "CLOSING";
249 	default:
250 		return "UNKNOWN";
251 	}
252 }
253 
254 const char *dlm_midcomms_state(struct midcomms_node *node)
255 {
256 	return dlm_state_str(node->state);
257 }
258 
259 unsigned long dlm_midcomms_flags(struct midcomms_node *node)
260 {
261 	return node->flags;
262 }
263 
264 int dlm_midcomms_send_queue_cnt(struct midcomms_node *node)
265 {
266 	return atomic_read(&node->send_queue_cnt);
267 }
268 
269 uint32_t dlm_midcomms_version(struct midcomms_node *node)
270 {
271 	return node->version;
272 }
273 
274 static struct midcomms_node *__find_node(int nodeid, int r)
275 {
276 	struct midcomms_node *node;
277 
278 	hlist_for_each_entry_rcu(node, &node_hash[r], hlist) {
279 		if (node->nodeid == nodeid)
280 			return node;
281 	}
282 
283 	return NULL;
284 }
285 
286 static void dlm_mhandle_release(struct rcu_head *rcu)
287 {
288 	struct dlm_mhandle *mh = container_of(rcu, struct dlm_mhandle, rcu);
289 
290 	dlm_lowcomms_put_msg(mh->msg);
291 	dlm_free_mhandle(mh);
292 }
293 
294 static void dlm_mhandle_delete(struct midcomms_node *node,
295 			       struct dlm_mhandle *mh)
296 {
297 	list_del_rcu(&mh->list);
298 	atomic_dec(&node->send_queue_cnt);
299 	call_rcu(&mh->rcu, dlm_mhandle_release);
300 }
301 
302 static void dlm_send_queue_flush(struct midcomms_node *node)
303 {
304 	struct dlm_mhandle *mh;
305 
306 	pr_debug("flush midcomms send queue of node %d\n", node->nodeid);
307 
308 	rcu_read_lock();
309 	spin_lock_bh(&node->send_queue_lock);
310 	list_for_each_entry_rcu(mh, &node->send_queue, list) {
311 		dlm_mhandle_delete(node, mh);
312 	}
313 	spin_unlock_bh(&node->send_queue_lock);
314 	rcu_read_unlock();
315 }
316 
317 static void midcomms_node_reset(struct midcomms_node *node)
318 {
319 	pr_debug("reset node %d\n", node->nodeid);
320 
321 	node->seq_next = DLM_SEQ_INIT;
322 	node->seq_send = DLM_SEQ_INIT;
323 	node->version = DLM_VERSION_NOT_SET;
324 	node->flags = 0;
325 
326 	dlm_send_queue_flush(node);
327 	node->state = DLM_CLOSED;
328 	wake_up(&node->shutdown_wait);
329 }
330 
331 static struct midcomms_node *nodeid2node(int nodeid, gfp_t alloc)
332 {
333 	struct midcomms_node *node, *tmp;
334 	int r = nodeid_hash(nodeid);
335 
336 	node = __find_node(nodeid, r);
337 	if (node || !alloc)
338 		return node;
339 
340 	node = kmalloc(sizeof(*node), alloc);
341 	if (!node)
342 		return NULL;
343 
344 	node->nodeid = nodeid;
345 	spin_lock_init(&node->state_lock);
346 	spin_lock_init(&node->send_queue_lock);
347 	atomic_set(&node->send_queue_cnt, 0);
348 	INIT_LIST_HEAD(&node->send_queue);
349 	init_waitqueue_head(&node->shutdown_wait);
350 	node->users = 0;
351 	midcomms_node_reset(node);
352 
353 	spin_lock(&nodes_lock);
354 	/* check again if there was somebody else
355 	 * earlier here to add the node
356 	 */
357 	tmp = __find_node(nodeid, r);
358 	if (tmp) {
359 		spin_unlock(&nodes_lock);
360 		kfree(node);
361 		return tmp;
362 	}
363 
364 	hlist_add_head_rcu(&node->hlist, &node_hash[r]);
365 	spin_unlock(&nodes_lock);
366 
367 	node->debugfs = dlm_create_debug_comms_file(nodeid, node);
368 	return node;
369 }
370 
371 static int dlm_send_ack(int nodeid, uint32_t seq)
372 {
373 	int mb_len = sizeof(struct dlm_header);
374 	struct dlm_header *m_header;
375 	struct dlm_msg *msg;
376 	char *ppc;
377 
378 	msg = dlm_lowcomms_new_msg(nodeid, mb_len, GFP_ATOMIC, &ppc,
379 				   NULL, NULL);
380 	if (!msg)
381 		return -ENOMEM;
382 
383 	m_header = (struct dlm_header *)ppc;
384 
385 	m_header->h_version = cpu_to_le32(DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
386 	m_header->h_nodeid = cpu_to_le32(dlm_our_nodeid());
387 	m_header->h_length = cpu_to_le16(mb_len);
388 	m_header->h_cmd = DLM_ACK;
389 	m_header->u.h_seq = cpu_to_le32(seq);
390 
391 	dlm_lowcomms_commit_msg(msg);
392 	dlm_lowcomms_put_msg(msg);
393 
394 	return 0;
395 }
396 
397 static int dlm_send_fin(struct midcomms_node *node,
398 			void (*ack_rcv)(struct midcomms_node *node))
399 {
400 	int mb_len = sizeof(struct dlm_header);
401 	struct dlm_header *m_header;
402 	struct dlm_mhandle *mh;
403 	char *ppc;
404 
405 	mh = dlm_midcomms_get_mhandle(node->nodeid, mb_len, GFP_ATOMIC, &ppc);
406 	if (!mh)
407 		return -ENOMEM;
408 
409 	set_bit(DLM_NODE_FLAG_STOP_TX, &node->flags);
410 	mh->ack_rcv = ack_rcv;
411 
412 	m_header = (struct dlm_header *)ppc;
413 
414 	m_header->h_version = cpu_to_le32(DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
415 	m_header->h_nodeid = cpu_to_le32(dlm_our_nodeid());
416 	m_header->h_length = cpu_to_le16(mb_len);
417 	m_header->h_cmd = DLM_FIN;
418 
419 	pr_debug("sending fin msg to node %d\n", node->nodeid);
420 	dlm_midcomms_commit_mhandle(mh, NULL, 0);
421 
422 	return 0;
423 }
424 
425 static void dlm_receive_ack(struct midcomms_node *node, uint32_t seq)
426 {
427 	struct dlm_mhandle *mh;
428 
429 	rcu_read_lock();
430 	list_for_each_entry_rcu(mh, &node->send_queue, list) {
431 		if (before(mh->seq, seq)) {
432 			if (mh->ack_rcv)
433 				mh->ack_rcv(node);
434 		} else {
435 			/* send queue should be ordered */
436 			break;
437 		}
438 	}
439 
440 	spin_lock_bh(&node->send_queue_lock);
441 	list_for_each_entry_rcu(mh, &node->send_queue, list) {
442 		if (before(mh->seq, seq)) {
443 			dlm_mhandle_delete(node, mh);
444 		} else {
445 			/* send queue should be ordered */
446 			break;
447 		}
448 	}
449 	spin_unlock_bh(&node->send_queue_lock);
450 	rcu_read_unlock();
451 }
452 
453 static void dlm_pas_fin_ack_rcv(struct midcomms_node *node)
454 {
455 	spin_lock(&node->state_lock);
456 	pr_debug("receive passive fin ack from node %d with state %s\n",
457 		 node->nodeid, dlm_state_str(node->state));
458 
459 	switch (node->state) {
460 	case DLM_LAST_ACK:
461 		/* DLM_CLOSED */
462 		midcomms_node_reset(node);
463 		break;
464 	case DLM_CLOSED:
465 		/* not valid but somehow we got what we want */
466 		wake_up(&node->shutdown_wait);
467 		break;
468 	default:
469 		spin_unlock(&node->state_lock);
470 		log_print("%s: unexpected state: %d",
471 			  __func__, node->state);
472 		WARN_ON_ONCE(1);
473 		return;
474 	}
475 	spin_unlock(&node->state_lock);
476 }
477 
478 static void dlm_receive_buffer_3_2_trace(uint32_t seq, union dlm_packet *p)
479 {
480 	switch (p->header.h_cmd) {
481 	case DLM_MSG:
482 		trace_dlm_recv_message(dlm_our_nodeid(), seq, &p->message);
483 		break;
484 	case DLM_RCOM:
485 		trace_dlm_recv_rcom(dlm_our_nodeid(), seq, &p->rcom);
486 		break;
487 	default:
488 		break;
489 	}
490 }
491 
492 static void dlm_midcomms_receive_buffer(union dlm_packet *p,
493 					struct midcomms_node *node,
494 					uint32_t seq)
495 {
496 	if (seq == node->seq_next) {
497 		node->seq_next++;
498 
499 		switch (p->header.h_cmd) {
500 		case DLM_FIN:
501 			spin_lock(&node->state_lock);
502 			pr_debug("receive fin msg from node %d with state %s\n",
503 				 node->nodeid, dlm_state_str(node->state));
504 
505 			switch (node->state) {
506 			case DLM_ESTABLISHED:
507 				dlm_send_ack(node->nodeid, node->seq_next);
508 
509 				/* passive shutdown DLM_LAST_ACK case 1
510 				 * additional we check if the node is used by
511 				 * cluster manager events at all.
512 				 */
513 				if (node->users == 0) {
514 					node->state = DLM_LAST_ACK;
515 					pr_debug("switch node %d to state %s case 1\n",
516 						 node->nodeid, dlm_state_str(node->state));
517 					set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
518 					dlm_send_fin(node, dlm_pas_fin_ack_rcv);
519 				} else {
520 					node->state = DLM_CLOSE_WAIT;
521 					pr_debug("switch node %d to state %s\n",
522 						 node->nodeid, dlm_state_str(node->state));
523 				}
524 				break;
525 			case DLM_FIN_WAIT1:
526 				dlm_send_ack(node->nodeid, node->seq_next);
527 				node->state = DLM_CLOSING;
528 				set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
529 				pr_debug("switch node %d to state %s\n",
530 					 node->nodeid, dlm_state_str(node->state));
531 				break;
532 			case DLM_FIN_WAIT2:
533 				dlm_send_ack(node->nodeid, node->seq_next);
534 				midcomms_node_reset(node);
535 				pr_debug("switch node %d to state %s\n",
536 					 node->nodeid, dlm_state_str(node->state));
537 				break;
538 			case DLM_LAST_ACK:
539 				/* probably remove_member caught it, do nothing */
540 				break;
541 			default:
542 				spin_unlock(&node->state_lock);
543 				log_print("%s: unexpected state: %d",
544 					  __func__, node->state);
545 				WARN_ON_ONCE(1);
546 				return;
547 			}
548 			spin_unlock(&node->state_lock);
549 			break;
550 		default:
551 			WARN_ON_ONCE(test_bit(DLM_NODE_FLAG_STOP_RX, &node->flags));
552 			dlm_receive_buffer_3_2_trace(seq, p);
553 			dlm_receive_buffer(p, node->nodeid);
554 			set_bit(DLM_NODE_ULP_DELIVERED, &node->flags);
555 			break;
556 		}
557 	} else {
558 		/* retry to ack message which we already have by sending back
559 		 * current node->seq_next number as ack.
560 		 */
561 		if (seq < node->seq_next)
562 			dlm_send_ack(node->nodeid, node->seq_next);
563 
564 		log_print_ratelimited("ignore dlm msg because seq mismatch, seq: %u, expected: %u, nodeid: %d",
565 				      seq, node->seq_next, node->nodeid);
566 	}
567 }
568 
569 static struct midcomms_node *
570 dlm_midcomms_recv_node_lookup(int nodeid, const union dlm_packet *p,
571 			      uint16_t msglen, int (*cb)(struct midcomms_node *node))
572 {
573 	struct midcomms_node *node = NULL;
574 	gfp_t allocation = 0;
575 	int ret;
576 
577 	switch (p->header.h_cmd) {
578 	case DLM_RCOM:
579 		if (msglen < sizeof(struct dlm_rcom)) {
580 			log_print("rcom msg too small: %u, will skip this message from node %d",
581 				  msglen, nodeid);
582 			return NULL;
583 		}
584 
585 		switch (p->rcom.rc_type) {
586 		case cpu_to_le32(DLM_RCOM_NAMES):
587 			fallthrough;
588 		case cpu_to_le32(DLM_RCOM_NAMES_REPLY):
589 			fallthrough;
590 		case cpu_to_le32(DLM_RCOM_STATUS):
591 			fallthrough;
592 		case cpu_to_le32(DLM_RCOM_STATUS_REPLY):
593 			node = nodeid2node(nodeid, 0);
594 			if (node) {
595 				spin_lock(&node->state_lock);
596 				if (node->state != DLM_ESTABLISHED)
597 					pr_debug("receive begin RCOM msg from node %d with state %s\n",
598 						 node->nodeid, dlm_state_str(node->state));
599 
600 				switch (node->state) {
601 				case DLM_CLOSED:
602 					node->state = DLM_ESTABLISHED;
603 					pr_debug("switch node %d to state %s\n",
604 						 node->nodeid, dlm_state_str(node->state));
605 					break;
606 				case DLM_ESTABLISHED:
607 					break;
608 				default:
609 					spin_unlock(&node->state_lock);
610 					return NULL;
611 				}
612 				spin_unlock(&node->state_lock);
613 			}
614 
615 			allocation = GFP_NOFS;
616 			break;
617 		default:
618 			break;
619 		}
620 
621 		break;
622 	default:
623 		break;
624 	}
625 
626 	node = nodeid2node(nodeid, allocation);
627 	if (!node) {
628 		switch (p->header.h_cmd) {
629 		case DLM_OPTS:
630 			if (msglen < sizeof(struct dlm_opts)) {
631 				log_print("opts msg too small: %u, will skip this message from node %d",
632 					  msglen, nodeid);
633 				return NULL;
634 			}
635 
636 			log_print_ratelimited("received dlm opts message nextcmd %d from node %d in an invalid sequence",
637 					      p->opts.o_nextcmd, nodeid);
638 			break;
639 		default:
640 			log_print_ratelimited("received dlm message cmd %d from node %d in an invalid sequence",
641 					      p->header.h_cmd, nodeid);
642 			break;
643 		}
644 
645 		return NULL;
646 	}
647 
648 	ret = cb(node);
649 	if (ret < 0)
650 		return NULL;
651 
652 	return node;
653 }
654 
655 static int dlm_midcomms_version_check_3_2(struct midcomms_node *node)
656 {
657 	switch (node->version) {
658 	case DLM_VERSION_NOT_SET:
659 		node->version = DLM_VERSION_3_2;
660 		wake_up(&node->shutdown_wait);
661 		log_print("version 0x%08x for node %d detected", DLM_VERSION_3_2,
662 			  node->nodeid);
663 		break;
664 	case DLM_VERSION_3_2:
665 		break;
666 	default:
667 		log_print_ratelimited("version mismatch detected, assumed 0x%08x but node %d has 0x%08x",
668 				      DLM_VERSION_3_2, node->nodeid, node->version);
669 		return -1;
670 	}
671 
672 	return 0;
673 }
674 
675 static int dlm_opts_check_msglen(union dlm_packet *p, uint16_t msglen, int nodeid)
676 {
677 	int len = msglen;
678 
679 	/* we only trust outer header msglen because
680 	 * it's checked against receive buffer length.
681 	 */
682 	if (len < sizeof(struct dlm_opts))
683 		return -1;
684 	len -= sizeof(struct dlm_opts);
685 
686 	if (len < le16_to_cpu(p->opts.o_optlen))
687 		return -1;
688 	len -= le16_to_cpu(p->opts.o_optlen);
689 
690 	switch (p->opts.o_nextcmd) {
691 	case DLM_FIN:
692 		if (len < sizeof(struct dlm_header)) {
693 			log_print("fin too small: %d, will skip this message from node %d",
694 				  len, nodeid);
695 			return -1;
696 		}
697 
698 		break;
699 	case DLM_MSG:
700 		if (len < sizeof(struct dlm_message)) {
701 			log_print("msg too small: %d, will skip this message from node %d",
702 				  msglen, nodeid);
703 			return -1;
704 		}
705 
706 		break;
707 	case DLM_RCOM:
708 		if (len < sizeof(struct dlm_rcom)) {
709 			log_print("rcom msg too small: %d, will skip this message from node %d",
710 				  len, nodeid);
711 			return -1;
712 		}
713 
714 		break;
715 	default:
716 		log_print("unsupported o_nextcmd received: %u, will skip this message from node %d",
717 			  p->opts.o_nextcmd, nodeid);
718 		return -1;
719 	}
720 
721 	return 0;
722 }
723 
724 static void dlm_midcomms_receive_buffer_3_2(union dlm_packet *p, int nodeid)
725 {
726 	uint16_t msglen = le16_to_cpu(p->header.h_length);
727 	struct midcomms_node *node;
728 	uint32_t seq;
729 	int ret, idx;
730 
731 	idx = srcu_read_lock(&nodes_srcu);
732 	node = dlm_midcomms_recv_node_lookup(nodeid, p, msglen,
733 					     dlm_midcomms_version_check_3_2);
734 	if (!node)
735 		goto out;
736 
737 	switch (p->header.h_cmd) {
738 	case DLM_RCOM:
739 		/* these rcom message we use to determine version.
740 		 * they have their own retransmission handling and
741 		 * are the first messages of dlm.
742 		 *
743 		 * length already checked.
744 		 */
745 		switch (p->rcom.rc_type) {
746 		case cpu_to_le32(DLM_RCOM_NAMES):
747 			fallthrough;
748 		case cpu_to_le32(DLM_RCOM_NAMES_REPLY):
749 			fallthrough;
750 		case cpu_to_le32(DLM_RCOM_STATUS):
751 			fallthrough;
752 		case cpu_to_le32(DLM_RCOM_STATUS_REPLY):
753 			break;
754 		default:
755 			log_print("unsupported rcom type received: %u, will skip this message from node %d",
756 				  le32_to_cpu(p->rcom.rc_type), nodeid);
757 			goto out;
758 		}
759 
760 		WARN_ON_ONCE(test_bit(DLM_NODE_FLAG_STOP_RX, &node->flags));
761 		dlm_receive_buffer(p, nodeid);
762 		break;
763 	case DLM_OPTS:
764 		seq = le32_to_cpu(p->header.u.h_seq);
765 
766 		ret = dlm_opts_check_msglen(p, msglen, nodeid);
767 		if (ret < 0) {
768 			log_print("opts msg too small: %u, will skip this message from node %d",
769 				  msglen, nodeid);
770 			goto out;
771 		}
772 
773 		p = (union dlm_packet *)((unsigned char *)p->opts.o_opts +
774 					 le16_to_cpu(p->opts.o_optlen));
775 
776 		/* recheck inner msglen just if it's not garbage */
777 		msglen = le16_to_cpu(p->header.h_length);
778 		switch (p->header.h_cmd) {
779 		case DLM_RCOM:
780 			if (msglen < sizeof(struct dlm_rcom)) {
781 				log_print("inner rcom msg too small: %u, will skip this message from node %d",
782 					  msglen, nodeid);
783 				goto out;
784 			}
785 
786 			break;
787 		case DLM_MSG:
788 			if (msglen < sizeof(struct dlm_message)) {
789 				log_print("inner msg too small: %u, will skip this message from node %d",
790 					  msglen, nodeid);
791 				goto out;
792 			}
793 
794 			break;
795 		case DLM_FIN:
796 			if (msglen < sizeof(struct dlm_header)) {
797 				log_print("inner fin too small: %u, will skip this message from node %d",
798 					  msglen, nodeid);
799 				goto out;
800 			}
801 
802 			break;
803 		default:
804 			log_print("unsupported inner h_cmd received: %u, will skip this message from node %d",
805 				  msglen, nodeid);
806 			goto out;
807 		}
808 
809 		dlm_midcomms_receive_buffer(p, node, seq);
810 		break;
811 	case DLM_ACK:
812 		seq = le32_to_cpu(p->header.u.h_seq);
813 		dlm_receive_ack(node, seq);
814 		break;
815 	default:
816 		log_print("unsupported h_cmd received: %u, will skip this message from node %d",
817 			  p->header.h_cmd, nodeid);
818 		break;
819 	}
820 
821 out:
822 	srcu_read_unlock(&nodes_srcu, idx);
823 }
824 
825 static int dlm_midcomms_version_check_3_1(struct midcomms_node *node)
826 {
827 	switch (node->version) {
828 	case DLM_VERSION_NOT_SET:
829 		node->version = DLM_VERSION_3_1;
830 		wake_up(&node->shutdown_wait);
831 		log_print("version 0x%08x for node %d detected", DLM_VERSION_3_1,
832 			  node->nodeid);
833 		break;
834 	case DLM_VERSION_3_1:
835 		break;
836 	default:
837 		log_print_ratelimited("version mismatch detected, assumed 0x%08x but node %d has 0x%08x",
838 				      DLM_VERSION_3_1, node->nodeid, node->version);
839 		return -1;
840 	}
841 
842 	return 0;
843 }
844 
845 static void dlm_midcomms_receive_buffer_3_1(union dlm_packet *p, int nodeid)
846 {
847 	uint16_t msglen = le16_to_cpu(p->header.h_length);
848 	struct midcomms_node *node;
849 	int idx;
850 
851 	idx = srcu_read_lock(&nodes_srcu);
852 	node = dlm_midcomms_recv_node_lookup(nodeid, p, msglen,
853 					     dlm_midcomms_version_check_3_1);
854 	if (!node) {
855 		srcu_read_unlock(&nodes_srcu, idx);
856 		return;
857 	}
858 	srcu_read_unlock(&nodes_srcu, idx);
859 
860 	switch (p->header.h_cmd) {
861 	case DLM_RCOM:
862 		/* length already checked */
863 		break;
864 	case DLM_MSG:
865 		if (msglen < sizeof(struct dlm_message)) {
866 			log_print("msg too small: %u, will skip this message from node %d",
867 				  msglen, nodeid);
868 			return;
869 		}
870 
871 		break;
872 	default:
873 		log_print("unsupported h_cmd received: %u, will skip this message from node %d",
874 			  p->header.h_cmd, nodeid);
875 		return;
876 	}
877 
878 	dlm_receive_buffer(p, nodeid);
879 }
880 
881 int dlm_validate_incoming_buffer(int nodeid, unsigned char *buf, int len)
882 {
883 	const unsigned char *ptr = buf;
884 	const struct dlm_header *hd;
885 	uint16_t msglen;
886 	int ret = 0;
887 
888 	while (len >= sizeof(struct dlm_header)) {
889 		hd = (struct dlm_header *)ptr;
890 
891 		/* no message should be more than DLM_MAX_SOCKET_BUFSIZE or
892 		 * less than dlm_header size.
893 		 *
894 		 * Some messages does not have a 8 byte length boundary yet
895 		 * which can occur in a unaligned memory access of some dlm
896 		 * messages. However this problem need to be fixed at the
897 		 * sending side, for now it seems nobody run into architecture
898 		 * related issues yet but it slows down some processing.
899 		 * Fixing this issue should be scheduled in future by doing
900 		 * the next major version bump.
901 		 */
902 		msglen = le16_to_cpu(hd->h_length);
903 		if (msglen > DLM_MAX_SOCKET_BUFSIZE ||
904 		    msglen < sizeof(struct dlm_header)) {
905 			log_print("received invalid length header: %u from node %d, will abort message parsing",
906 				  msglen, nodeid);
907 			return -EBADMSG;
908 		}
909 
910 		/* caller will take care that leftover
911 		 * will be parsed next call with more data
912 		 */
913 		if (msglen > len)
914 			break;
915 
916 		ret += msglen;
917 		len -= msglen;
918 		ptr += msglen;
919 	}
920 
921 	return ret;
922 }
923 
924 /*
925  * Called from the low-level comms layer to process a buffer of
926  * commands.
927  */
928 int dlm_process_incoming_buffer(int nodeid, unsigned char *buf, int len)
929 {
930 	const unsigned char *ptr = buf;
931 	const struct dlm_header *hd;
932 	uint16_t msglen;
933 	int ret = 0;
934 
935 	while (len >= sizeof(struct dlm_header)) {
936 		hd = (struct dlm_header *)ptr;
937 
938 		msglen = le16_to_cpu(hd->h_length);
939 		if (msglen > len)
940 			break;
941 
942 		switch (hd->h_version) {
943 		case cpu_to_le32(DLM_VERSION_3_1):
944 			dlm_midcomms_receive_buffer_3_1((union dlm_packet *)ptr, nodeid);
945 			break;
946 		case cpu_to_le32(DLM_VERSION_3_2):
947 			dlm_midcomms_receive_buffer_3_2((union dlm_packet *)ptr, nodeid);
948 			break;
949 		default:
950 			log_print("received invalid version header: %u from node %d, will skip this message",
951 				  le32_to_cpu(hd->h_version), nodeid);
952 			break;
953 		}
954 
955 		ret += msglen;
956 		len -= msglen;
957 		ptr += msglen;
958 	}
959 
960 	return ret;
961 }
962 
963 void dlm_midcomms_receive_done(int nodeid)
964 {
965 	struct midcomms_node *node;
966 	int idx;
967 
968 	idx = srcu_read_lock(&nodes_srcu);
969 	node = nodeid2node(nodeid, 0);
970 	if (!node) {
971 		srcu_read_unlock(&nodes_srcu, idx);
972 		return;
973 	}
974 
975 	/* old protocol, we do nothing */
976 	switch (node->version) {
977 	case DLM_VERSION_3_2:
978 		break;
979 	default:
980 		srcu_read_unlock(&nodes_srcu, idx);
981 		return;
982 	}
983 
984 	/* do nothing if we didn't delivered stateful to ulp */
985 	if (!test_and_clear_bit(DLM_NODE_ULP_DELIVERED,
986 				&node->flags)) {
987 		srcu_read_unlock(&nodes_srcu, idx);
988 		return;
989 	}
990 
991 	spin_lock(&node->state_lock);
992 	/* we only ack if state is ESTABLISHED */
993 	switch (node->state) {
994 	case DLM_ESTABLISHED:
995 		spin_unlock(&node->state_lock);
996 		dlm_send_ack(node->nodeid, node->seq_next);
997 		break;
998 	default:
999 		spin_unlock(&node->state_lock);
1000 		/* do nothing FIN has it's own ack send */
1001 		break;
1002 	}
1003 	srcu_read_unlock(&nodes_srcu, idx);
1004 }
1005 
1006 void dlm_midcomms_unack_msg_resend(int nodeid)
1007 {
1008 	struct midcomms_node *node;
1009 	struct dlm_mhandle *mh;
1010 	int idx, ret;
1011 
1012 	idx = srcu_read_lock(&nodes_srcu);
1013 	node = nodeid2node(nodeid, 0);
1014 	if (!node) {
1015 		srcu_read_unlock(&nodes_srcu, idx);
1016 		return;
1017 	}
1018 
1019 	/* old protocol, we don't support to retransmit on failure */
1020 	switch (node->version) {
1021 	case DLM_VERSION_3_2:
1022 		break;
1023 	default:
1024 		srcu_read_unlock(&nodes_srcu, idx);
1025 		return;
1026 	}
1027 
1028 	rcu_read_lock();
1029 	list_for_each_entry_rcu(mh, &node->send_queue, list) {
1030 		if (!mh->committed)
1031 			continue;
1032 
1033 		ret = dlm_lowcomms_resend_msg(mh->msg);
1034 		if (!ret)
1035 			log_print_ratelimited("retransmit dlm msg, seq %u, nodeid %d",
1036 					      mh->seq, node->nodeid);
1037 	}
1038 	rcu_read_unlock();
1039 	srcu_read_unlock(&nodes_srcu, idx);
1040 }
1041 
1042 static void dlm_fill_opts_header(struct dlm_opts *opts, uint16_t inner_len,
1043 				 uint32_t seq)
1044 {
1045 	opts->o_header.h_cmd = DLM_OPTS;
1046 	opts->o_header.h_version = cpu_to_le32(DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
1047 	opts->o_header.h_nodeid = cpu_to_le32(dlm_our_nodeid());
1048 	opts->o_header.h_length = cpu_to_le16(DLM_MIDCOMMS_OPT_LEN + inner_len);
1049 	opts->o_header.u.h_seq = cpu_to_le32(seq);
1050 }
1051 
1052 static void midcomms_new_msg_cb(void *data)
1053 {
1054 	struct dlm_mhandle *mh = data;
1055 
1056 	atomic_inc(&mh->node->send_queue_cnt);
1057 
1058 	spin_lock_bh(&mh->node->send_queue_lock);
1059 	list_add_tail_rcu(&mh->list, &mh->node->send_queue);
1060 	spin_unlock_bh(&mh->node->send_queue_lock);
1061 
1062 	mh->seq = mh->node->seq_send++;
1063 }
1064 
1065 static struct dlm_msg *dlm_midcomms_get_msg_3_2(struct dlm_mhandle *mh, int nodeid,
1066 						int len, gfp_t allocation, char **ppc)
1067 {
1068 	struct dlm_opts *opts;
1069 	struct dlm_msg *msg;
1070 
1071 	msg = dlm_lowcomms_new_msg(nodeid, len + DLM_MIDCOMMS_OPT_LEN,
1072 				   allocation, ppc, midcomms_new_msg_cb, mh);
1073 	if (!msg)
1074 		return NULL;
1075 
1076 	opts = (struct dlm_opts *)*ppc;
1077 	mh->opts = opts;
1078 
1079 	/* add possible options here */
1080 	dlm_fill_opts_header(opts, len, mh->seq);
1081 
1082 	*ppc += sizeof(*opts);
1083 	mh->inner_p = (const union dlm_packet *)*ppc;
1084 	return msg;
1085 }
1086 
1087 /* avoid false positive for nodes_srcu, unlock happens in
1088  * dlm_midcomms_commit_mhandle which is a must call if success
1089  */
1090 #ifndef __CHECKER__
1091 struct dlm_mhandle *dlm_midcomms_get_mhandle(int nodeid, int len,
1092 					     gfp_t allocation, char **ppc)
1093 {
1094 	struct midcomms_node *node;
1095 	struct dlm_mhandle *mh;
1096 	struct dlm_msg *msg;
1097 	int idx;
1098 
1099 	idx = srcu_read_lock(&nodes_srcu);
1100 	node = nodeid2node(nodeid, 0);
1101 	if (!node) {
1102 		WARN_ON_ONCE(1);
1103 		goto err;
1104 	}
1105 
1106 	/* this is a bug, however we going on and hope it will be resolved */
1107 	WARN_ON_ONCE(test_bit(DLM_NODE_FLAG_STOP_TX, &node->flags));
1108 
1109 	mh = dlm_allocate_mhandle(allocation);
1110 	if (!mh)
1111 		goto err;
1112 
1113 	mh->committed = false;
1114 	mh->ack_rcv = NULL;
1115 	mh->idx = idx;
1116 	mh->node = node;
1117 
1118 	switch (node->version) {
1119 	case DLM_VERSION_3_1:
1120 		msg = dlm_lowcomms_new_msg(nodeid, len, allocation, ppc,
1121 					   NULL, NULL);
1122 		if (!msg) {
1123 			dlm_free_mhandle(mh);
1124 			goto err;
1125 		}
1126 
1127 		break;
1128 	case DLM_VERSION_3_2:
1129 		msg = dlm_midcomms_get_msg_3_2(mh, nodeid, len, allocation,
1130 					       ppc);
1131 		if (!msg) {
1132 			dlm_free_mhandle(mh);
1133 			goto err;
1134 		}
1135 
1136 		break;
1137 	default:
1138 		dlm_free_mhandle(mh);
1139 		WARN_ON_ONCE(1);
1140 		goto err;
1141 	}
1142 
1143 	mh->msg = msg;
1144 
1145 	/* keep in mind that is a must to call
1146 	 * dlm_midcomms_commit_msg() which releases
1147 	 * nodes_srcu using mh->idx which is assumed
1148 	 * here that the application will call it.
1149 	 */
1150 	return mh;
1151 
1152 err:
1153 	srcu_read_unlock(&nodes_srcu, idx);
1154 	return NULL;
1155 }
1156 #endif
1157 
1158 static void dlm_midcomms_commit_msg_3_2_trace(const struct dlm_mhandle *mh,
1159 					      const void *name, int namelen)
1160 {
1161 	switch (mh->inner_p->header.h_cmd) {
1162 	case DLM_MSG:
1163 		trace_dlm_send_message(mh->node->nodeid, mh->seq,
1164 				       &mh->inner_p->message,
1165 				       name, namelen);
1166 		break;
1167 	case DLM_RCOM:
1168 		trace_dlm_send_rcom(mh->node->nodeid, mh->seq,
1169 				    &mh->inner_p->rcom);
1170 		break;
1171 	default:
1172 		/* nothing to trace */
1173 		break;
1174 	}
1175 }
1176 
1177 static void dlm_midcomms_commit_msg_3_2(struct dlm_mhandle *mh,
1178 					const void *name, int namelen)
1179 {
1180 	/* nexthdr chain for fast lookup */
1181 	mh->opts->o_nextcmd = mh->inner_p->header.h_cmd;
1182 	mh->committed = true;
1183 	dlm_midcomms_commit_msg_3_2_trace(mh, name, namelen);
1184 	dlm_lowcomms_commit_msg(mh->msg);
1185 }
1186 
1187 /* avoid false positive for nodes_srcu, lock was happen in
1188  * dlm_midcomms_get_mhandle
1189  */
1190 #ifndef __CHECKER__
1191 void dlm_midcomms_commit_mhandle(struct dlm_mhandle *mh,
1192 				 const void *name, int namelen)
1193 {
1194 
1195 	switch (mh->node->version) {
1196 	case DLM_VERSION_3_1:
1197 		srcu_read_unlock(&nodes_srcu, mh->idx);
1198 
1199 		dlm_lowcomms_commit_msg(mh->msg);
1200 		dlm_lowcomms_put_msg(mh->msg);
1201 		/* mh is not part of rcu list in this case */
1202 		dlm_free_mhandle(mh);
1203 		break;
1204 	case DLM_VERSION_3_2:
1205 		/* held rcu read lock here, because we sending the
1206 		 * dlm message out, when we do that we could receive
1207 		 * an ack back which releases the mhandle and we
1208 		 * get a use after free.
1209 		 */
1210 		rcu_read_lock();
1211 		dlm_midcomms_commit_msg_3_2(mh, name, namelen);
1212 		srcu_read_unlock(&nodes_srcu, mh->idx);
1213 		rcu_read_unlock();
1214 		break;
1215 	default:
1216 		srcu_read_unlock(&nodes_srcu, mh->idx);
1217 		WARN_ON_ONCE(1);
1218 		break;
1219 	}
1220 }
1221 #endif
1222 
1223 int dlm_midcomms_start(void)
1224 {
1225 	return dlm_lowcomms_start();
1226 }
1227 
1228 void dlm_midcomms_stop(void)
1229 {
1230 	dlm_lowcomms_stop();
1231 }
1232 
1233 void dlm_midcomms_init(void)
1234 {
1235 	int i;
1236 
1237 	for (i = 0; i < CONN_HASH_SIZE; i++)
1238 		INIT_HLIST_HEAD(&node_hash[i]);
1239 
1240 	dlm_lowcomms_init();
1241 }
1242 
1243 void dlm_midcomms_exit(void)
1244 {
1245 	dlm_lowcomms_exit();
1246 }
1247 
1248 static void dlm_act_fin_ack_rcv(struct midcomms_node *node)
1249 {
1250 	spin_lock(&node->state_lock);
1251 	pr_debug("receive active fin ack from node %d with state %s\n",
1252 		 node->nodeid, dlm_state_str(node->state));
1253 
1254 	switch (node->state) {
1255 	case DLM_FIN_WAIT1:
1256 		node->state = DLM_FIN_WAIT2;
1257 		pr_debug("switch node %d to state %s\n",
1258 			 node->nodeid, dlm_state_str(node->state));
1259 		break;
1260 	case DLM_CLOSING:
1261 		midcomms_node_reset(node);
1262 		pr_debug("switch node %d to state %s\n",
1263 			 node->nodeid, dlm_state_str(node->state));
1264 		break;
1265 	case DLM_CLOSED:
1266 		/* not valid but somehow we got what we want */
1267 		wake_up(&node->shutdown_wait);
1268 		break;
1269 	default:
1270 		spin_unlock(&node->state_lock);
1271 		log_print("%s: unexpected state: %d",
1272 			  __func__, node->state);
1273 		WARN_ON_ONCE(1);
1274 		return;
1275 	}
1276 	spin_unlock(&node->state_lock);
1277 }
1278 
1279 void dlm_midcomms_add_member(int nodeid)
1280 {
1281 	struct midcomms_node *node;
1282 	int idx;
1283 
1284 	if (nodeid == dlm_our_nodeid())
1285 		return;
1286 
1287 	idx = srcu_read_lock(&nodes_srcu);
1288 	node = nodeid2node(nodeid, GFP_NOFS);
1289 	if (!node) {
1290 		srcu_read_unlock(&nodes_srcu, idx);
1291 		return;
1292 	}
1293 
1294 	spin_lock(&node->state_lock);
1295 	if (!node->users) {
1296 		pr_debug("receive add member from node %d with state %s\n",
1297 			 node->nodeid, dlm_state_str(node->state));
1298 		switch (node->state) {
1299 		case DLM_ESTABLISHED:
1300 			break;
1301 		case DLM_CLOSED:
1302 			node->state = DLM_ESTABLISHED;
1303 			pr_debug("switch node %d to state %s\n",
1304 				 node->nodeid, dlm_state_str(node->state));
1305 			break;
1306 		default:
1307 			/* some invalid state passive shutdown
1308 			 * was failed, we try to reset and
1309 			 * hope it will go on.
1310 			 */
1311 			log_print("reset node %d because shutdown stuck",
1312 				  node->nodeid);
1313 
1314 			midcomms_node_reset(node);
1315 			node->state = DLM_ESTABLISHED;
1316 			break;
1317 		}
1318 	}
1319 
1320 	node->users++;
1321 	pr_debug("node %d users inc count %d\n", nodeid, node->users);
1322 	spin_unlock(&node->state_lock);
1323 
1324 	srcu_read_unlock(&nodes_srcu, idx);
1325 }
1326 
1327 void dlm_midcomms_remove_member(int nodeid)
1328 {
1329 	struct midcomms_node *node;
1330 	int idx;
1331 
1332 	if (nodeid == dlm_our_nodeid())
1333 		return;
1334 
1335 	idx = srcu_read_lock(&nodes_srcu);
1336 	node = nodeid2node(nodeid, 0);
1337 	if (!node) {
1338 		srcu_read_unlock(&nodes_srcu, idx);
1339 		return;
1340 	}
1341 
1342 	spin_lock(&node->state_lock);
1343 	node->users--;
1344 	pr_debug("node %d users dec count %d\n", nodeid, node->users);
1345 
1346 	/* hitting users count to zero means the
1347 	 * other side is running dlm_midcomms_stop()
1348 	 * we meet us to have a clean disconnect.
1349 	 */
1350 	if (node->users == 0) {
1351 		pr_debug("receive remove member from node %d with state %s\n",
1352 			 node->nodeid, dlm_state_str(node->state));
1353 		switch (node->state) {
1354 		case DLM_ESTABLISHED:
1355 			break;
1356 		case DLM_CLOSE_WAIT:
1357 			/* passive shutdown DLM_LAST_ACK case 2 */
1358 			node->state = DLM_LAST_ACK;
1359 			pr_debug("switch node %d to state %s case 2\n",
1360 				 node->nodeid, dlm_state_str(node->state));
1361 			set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
1362 			dlm_send_fin(node, dlm_pas_fin_ack_rcv);
1363 			break;
1364 		case DLM_LAST_ACK:
1365 			/* probably receive fin caught it, do nothing */
1366 			break;
1367 		case DLM_CLOSED:
1368 			/* already gone, do nothing */
1369 			break;
1370 		default:
1371 			log_print("%s: unexpected state: %d",
1372 				  __func__, node->state);
1373 			break;
1374 		}
1375 	}
1376 	spin_unlock(&node->state_lock);
1377 
1378 	srcu_read_unlock(&nodes_srcu, idx);
1379 }
1380 
1381 static void midcomms_node_release(struct rcu_head *rcu)
1382 {
1383 	struct midcomms_node *node = container_of(rcu, struct midcomms_node, rcu);
1384 
1385 	WARN_ON_ONCE(atomic_read(&node->send_queue_cnt));
1386 	dlm_send_queue_flush(node);
1387 	kfree(node);
1388 }
1389 
1390 void dlm_midcomms_version_wait(void)
1391 {
1392 	struct midcomms_node *node;
1393 	int i, idx, ret;
1394 
1395 	idx = srcu_read_lock(&nodes_srcu);
1396 	for (i = 0; i < CONN_HASH_SIZE; i++) {
1397 		hlist_for_each_entry_rcu(node, &node_hash[i], hlist) {
1398 			ret = wait_event_timeout(node->shutdown_wait,
1399 						 node->version != DLM_VERSION_NOT_SET ||
1400 						 node->state == DLM_CLOSED ||
1401 						 test_bit(DLM_NODE_FLAG_CLOSE, &node->flags),
1402 						 DLM_SHUTDOWN_TIMEOUT);
1403 			if (!ret || test_bit(DLM_NODE_FLAG_CLOSE, &node->flags))
1404 				pr_debug("version wait timed out for node %d with state %s\n",
1405 					 node->nodeid, dlm_state_str(node->state));
1406 		}
1407 	}
1408 	srcu_read_unlock(&nodes_srcu, idx);
1409 }
1410 
1411 static void midcomms_shutdown(struct midcomms_node *node)
1412 {
1413 	int ret;
1414 
1415 	/* old protocol, we don't wait for pending operations */
1416 	switch (node->version) {
1417 	case DLM_VERSION_3_2:
1418 		break;
1419 	default:
1420 		return;
1421 	}
1422 
1423 	spin_lock(&node->state_lock);
1424 	pr_debug("receive active shutdown for node %d with state %s\n",
1425 		 node->nodeid, dlm_state_str(node->state));
1426 	switch (node->state) {
1427 	case DLM_ESTABLISHED:
1428 		node->state = DLM_FIN_WAIT1;
1429 		pr_debug("switch node %d to state %s case 2\n",
1430 			 node->nodeid, dlm_state_str(node->state));
1431 		dlm_send_fin(node, dlm_act_fin_ack_rcv);
1432 		break;
1433 	case DLM_CLOSED:
1434 		/* we have what we want */
1435 		break;
1436 	default:
1437 		/* busy to enter DLM_FIN_WAIT1, wait until passive
1438 		 * done in shutdown_wait to enter DLM_CLOSED.
1439 		 */
1440 		break;
1441 	}
1442 	spin_unlock(&node->state_lock);
1443 
1444 	if (DLM_DEBUG_FENCE_TERMINATION)
1445 		msleep(5000);
1446 
1447 	/* wait for other side dlm + fin */
1448 	ret = wait_event_timeout(node->shutdown_wait,
1449 				 node->state == DLM_CLOSED ||
1450 				 test_bit(DLM_NODE_FLAG_CLOSE, &node->flags),
1451 				 DLM_SHUTDOWN_TIMEOUT);
1452 	if (!ret || test_bit(DLM_NODE_FLAG_CLOSE, &node->flags))
1453 		pr_debug("active shutdown timed out for node %d with state %s\n",
1454 			 node->nodeid, dlm_state_str(node->state));
1455 	else
1456 		pr_debug("active shutdown done for node %d with state %s\n",
1457 			 node->nodeid, dlm_state_str(node->state));
1458 }
1459 
1460 void dlm_midcomms_shutdown(void)
1461 {
1462 	struct midcomms_node *node;
1463 	int i, idx;
1464 
1465 	mutex_lock(&close_lock);
1466 	idx = srcu_read_lock(&nodes_srcu);
1467 	for (i = 0; i < CONN_HASH_SIZE; i++) {
1468 		hlist_for_each_entry_rcu(node, &node_hash[i], hlist) {
1469 			midcomms_shutdown(node);
1470 
1471 			dlm_delete_debug_comms_file(node->debugfs);
1472 
1473 			spin_lock(&nodes_lock);
1474 			hlist_del_rcu(&node->hlist);
1475 			spin_unlock(&nodes_lock);
1476 
1477 			call_srcu(&nodes_srcu, &node->rcu, midcomms_node_release);
1478 		}
1479 	}
1480 	srcu_read_unlock(&nodes_srcu, idx);
1481 	mutex_unlock(&close_lock);
1482 
1483 	dlm_lowcomms_shutdown();
1484 }
1485 
1486 int dlm_midcomms_close(int nodeid)
1487 {
1488 	struct midcomms_node *node;
1489 	int idx, ret;
1490 
1491 	if (nodeid == dlm_our_nodeid())
1492 		return 0;
1493 
1494 	dlm_stop_lockspaces_check();
1495 
1496 	idx = srcu_read_lock(&nodes_srcu);
1497 	/* Abort pending close/remove operation */
1498 	node = nodeid2node(nodeid, 0);
1499 	if (node) {
1500 		/* let shutdown waiters leave */
1501 		set_bit(DLM_NODE_FLAG_CLOSE, &node->flags);
1502 		wake_up(&node->shutdown_wait);
1503 	}
1504 	srcu_read_unlock(&nodes_srcu, idx);
1505 
1506 	synchronize_srcu(&nodes_srcu);
1507 
1508 	idx = srcu_read_lock(&nodes_srcu);
1509 	mutex_lock(&close_lock);
1510 	node = nodeid2node(nodeid, 0);
1511 	if (!node) {
1512 		mutex_unlock(&close_lock);
1513 		srcu_read_unlock(&nodes_srcu, idx);
1514 		return dlm_lowcomms_close(nodeid);
1515 	}
1516 
1517 	ret = dlm_lowcomms_close(nodeid);
1518 	spin_lock(&node->state_lock);
1519 	midcomms_node_reset(node);
1520 	spin_unlock(&node->state_lock);
1521 	srcu_read_unlock(&nodes_srcu, idx);
1522 	mutex_unlock(&close_lock);
1523 
1524 	return ret;
1525 }
1526 
1527 /* debug functionality to send raw dlm msg from user space */
1528 struct dlm_rawmsg_data {
1529 	struct midcomms_node *node;
1530 	void *buf;
1531 };
1532 
1533 static void midcomms_new_rawmsg_cb(void *data)
1534 {
1535 	struct dlm_rawmsg_data *rd = data;
1536 	struct dlm_header *h = rd->buf;
1537 
1538 	switch (h->h_version) {
1539 	case cpu_to_le32(DLM_VERSION_3_1):
1540 		break;
1541 	default:
1542 		switch (h->h_cmd) {
1543 		case DLM_OPTS:
1544 			if (!h->u.h_seq)
1545 				h->u.h_seq = cpu_to_le32(rd->node->seq_send++);
1546 			break;
1547 		default:
1548 			break;
1549 		}
1550 		break;
1551 	}
1552 }
1553 
1554 int dlm_midcomms_rawmsg_send(struct midcomms_node *node, void *buf,
1555 			     int buflen)
1556 {
1557 	struct dlm_rawmsg_data rd;
1558 	struct dlm_msg *msg;
1559 	char *msgbuf;
1560 
1561 	rd.node = node;
1562 	rd.buf = buf;
1563 
1564 	msg = dlm_lowcomms_new_msg(node->nodeid, buflen, GFP_NOFS,
1565 				   &msgbuf, midcomms_new_rawmsg_cb, &rd);
1566 	if (!msg)
1567 		return -ENOMEM;
1568 
1569 	memcpy(msgbuf, buf, buflen);
1570 	dlm_lowcomms_commit_msg(msg);
1571 	return 0;
1572 }
1573 
1574