xref: /openbmc/linux/fs/dlm/midcomms.c (revision d28a1de5)
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 <net/tcp.h>
136 
137 #include "dlm_internal.h"
138 #include "lockspace.h"
139 #include "lowcomms.h"
140 #include "config.h"
141 #include "memory.h"
142 #include "lock.h"
143 #include "util.h"
144 #include "midcomms.h"
145 
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
151 
152 struct midcomms_node {
153 	int nodeid;
154 	uint32_t version;
155 	uint32_t seq_send;
156 	uint32_t seq_next;
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
160 	 * not supported yet.
161 	 */
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
169 	unsigned long flags;
170 	wait_queue_head_t shutdown_wait;
171 
172 	/* dlm tcp termination state */
173 #define DLM_CLOSED	1
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
180 	int state;
181 	spinlock_t state_lock;
182 
183 	/* counts how many lockspaces are using this node
184 	 * this refcount is necessary to determine if the
185 	 * node wants to disconnect.
186 	 */
187 	int users;
188 
189 	/* not protected by srcu, node_hash lifetime */
190 	void *debugfs;
191 
192 	struct hlist_node hlist;
193 	struct rcu_head rcu;
194 };
195 
196 struct dlm_mhandle {
197 	const struct dlm_header *inner_hd;
198 	struct midcomms_node *node;
199 	struct dlm_opts *opts;
200 	struct dlm_msg *msg;
201 	bool committed;
202 	uint32_t seq;
203 
204 	void (*ack_rcv)(struct midcomms_node *node);
205 
206 	/* get_mhandle/commit srcu idx exchange */
207 	int idx;
208 
209 	struct list_head list;
210 	struct rcu_head rcu;
211 };
212 
213 static struct hlist_head node_hash[CONN_HASH_SIZE];
214 static DEFINE_SPINLOCK(nodes_lock);
215 DEFINE_STATIC_SRCU(nodes_srcu);
216 
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
221  * datastructure.
222  */
223 static DEFINE_MUTEX(close_lock);
224 
225 struct kmem_cache *dlm_midcomms_cache_create(void)
226 {
227 	return kmem_cache_create("dlm_mhandle", sizeof(struct dlm_mhandle),
228 				 0, 0, NULL);
229 }
230 
231 static inline const char *dlm_state_str(int state)
232 {
233 	switch (state) {
234 	case DLM_CLOSED:
235 		return "CLOSED";
236 	case DLM_ESTABLISHED:
237 		return "ESTABLISHED";
238 	case DLM_FIN_WAIT1:
239 		return "FIN_WAIT1";
240 	case DLM_FIN_WAIT2:
241 		return "FIN_WAIT2";
242 	case DLM_CLOSE_WAIT:
243 		return "CLOSE_WAIT";
244 	case DLM_LAST_ACK:
245 		return "LAST_ACK";
246 	case DLM_CLOSING:
247 		return "CLOSING";
248 	default:
249 		return "UNKNOWN";
250 	}
251 }
252 
253 const char *dlm_midcomms_state(struct midcomms_node *node)
254 {
255 	return dlm_state_str(node->state);
256 }
257 
258 unsigned long dlm_midcomms_flags(struct midcomms_node *node)
259 {
260 	return node->flags;
261 }
262 
263 int dlm_midcomms_send_queue_cnt(struct midcomms_node *node)
264 {
265 	return atomic_read(&node->send_queue_cnt);
266 }
267 
268 uint32_t dlm_midcomms_version(struct midcomms_node *node)
269 {
270 	return node->version;
271 }
272 
273 static struct midcomms_node *__find_node(int nodeid, int r)
274 {
275 	struct midcomms_node *node;
276 
277 	hlist_for_each_entry_rcu(node, &node_hash[r], hlist) {
278 		if (node->nodeid == nodeid)
279 			return node;
280 	}
281 
282 	return NULL;
283 }
284 
285 static void dlm_mhandle_release(struct rcu_head *rcu)
286 {
287 	struct dlm_mhandle *mh = container_of(rcu, struct dlm_mhandle, rcu);
288 
289 	dlm_lowcomms_put_msg(mh->msg);
290 	dlm_free_mhandle(mh);
291 }
292 
293 static void dlm_mhandle_delete(struct midcomms_node *node,
294 			       struct dlm_mhandle *mh)
295 {
296 	list_del_rcu(&mh->list);
297 	atomic_dec(&node->send_queue_cnt);
298 	call_rcu(&mh->rcu, dlm_mhandle_release);
299 }
300 
301 static void dlm_send_queue_flush(struct midcomms_node *node)
302 {
303 	struct dlm_mhandle *mh;
304 
305 	pr_debug("flush midcomms send queue of node %d\n", node->nodeid);
306 
307 	rcu_read_lock();
308 	spin_lock(&node->send_queue_lock);
309 	list_for_each_entry_rcu(mh, &node->send_queue, list) {
310 		dlm_mhandle_delete(node, mh);
311 	}
312 	spin_unlock(&node->send_queue_lock);
313 	rcu_read_unlock();
314 }
315 
316 static void midcomms_node_reset(struct midcomms_node *node)
317 {
318 	pr_debug("reset node %d\n", node->nodeid);
319 
320 	node->seq_next = DLM_SEQ_INIT;
321 	node->seq_send = DLM_SEQ_INIT;
322 	node->version = DLM_VERSION_NOT_SET;
323 	node->flags = 0;
324 
325 	dlm_send_queue_flush(node);
326 	node->state = DLM_CLOSED;
327 	wake_up(&node->shutdown_wait);
328 }
329 
330 static struct midcomms_node *nodeid2node(int nodeid, gfp_t alloc)
331 {
332 	struct midcomms_node *node, *tmp;
333 	int r = nodeid_hash(nodeid);
334 
335 	node = __find_node(nodeid, r);
336 	if (node || !alloc)
337 		return node;
338 
339 	node = kmalloc(sizeof(*node), alloc);
340 	if (!node)
341 		return NULL;
342 
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);
349 	node->users = 0;
350 	midcomms_node_reset(node);
351 
352 	spin_lock(&nodes_lock);
353 	/* check again if there was somebody else
354 	 * earlier here to add the node
355 	 */
356 	tmp = __find_node(nodeid, r);
357 	if (tmp) {
358 		spin_unlock(&nodes_lock);
359 		kfree(node);
360 		return tmp;
361 	}
362 
363 	hlist_add_head_rcu(&node->hlist, &node_hash[r]);
364 	spin_unlock(&nodes_lock);
365 
366 	node->debugfs = dlm_create_debug_comms_file(nodeid, node);
367 	return node;
368 }
369 
370 static int dlm_send_ack(int nodeid, uint32_t seq)
371 {
372 	int mb_len = sizeof(struct dlm_header);
373 	struct dlm_header *m_header;
374 	struct dlm_msg *msg;
375 	char *ppc;
376 
377 	msg = dlm_lowcomms_new_msg(nodeid, mb_len, GFP_NOFS, &ppc,
378 				   NULL, NULL);
379 	if (!msg)
380 		return -ENOMEM;
381 
382 	m_header = (struct dlm_header *)ppc;
383 
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);
389 
390 	dlm_lowcomms_commit_msg(msg);
391 	dlm_lowcomms_put_msg(msg);
392 
393 	return 0;
394 }
395 
396 static int dlm_send_fin(struct midcomms_node *node,
397 			void (*ack_rcv)(struct midcomms_node *node))
398 {
399 	int mb_len = sizeof(struct dlm_header);
400 	struct dlm_header *m_header;
401 	struct dlm_mhandle *mh;
402 	char *ppc;
403 
404 	mh = dlm_midcomms_get_mhandle(node->nodeid, mb_len, GFP_NOFS, &ppc);
405 	if (!mh)
406 		return -ENOMEM;
407 
408 	mh->ack_rcv = ack_rcv;
409 
410 	m_header = (struct dlm_header *)ppc;
411 
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;
416 
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);
420 
421 	return 0;
422 }
423 
424 static void dlm_receive_ack(struct midcomms_node *node, uint32_t seq)
425 {
426 	struct dlm_mhandle *mh;
427 
428 	rcu_read_lock();
429 	list_for_each_entry_rcu(mh, &node->send_queue, list) {
430 		if (before(mh->seq, seq)) {
431 			if (mh->ack_rcv)
432 				mh->ack_rcv(node);
433 		} else {
434 			/* send queue should be ordered */
435 			break;
436 		}
437 	}
438 
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);
443 		} else {
444 			/* send queue should be ordered */
445 			break;
446 		}
447 	}
448 	spin_unlock(&node->send_queue_lock);
449 	rcu_read_unlock();
450 }
451 
452 static void dlm_pas_fin_ack_rcv(struct midcomms_node *node)
453 {
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));
457 
458 	switch (node->state) {
459 	case DLM_LAST_ACK:
460 		/* DLM_CLOSED */
461 		midcomms_node_reset(node);
462 		break;
463 	case DLM_CLOSED:
464 		/* not valid but somehow we got what we want */
465 		wake_up(&node->shutdown_wait);
466 		break;
467 	default:
468 		spin_unlock(&node->state_lock);
469 		log_print("%s: unexpected state: %d\n",
470 			  __func__, node->state);
471 		WARN_ON(1);
472 		return;
473 	}
474 	spin_unlock(&node->state_lock);
475 }
476 
477 static void dlm_midcomms_receive_buffer(union dlm_packet *p,
478 					struct midcomms_node *node,
479 					uint32_t seq)
480 {
481 	if (seq == node->seq_next) {
482 		node->seq_next++;
483 
484 		switch (p->header.h_cmd) {
485 		case DLM_FIN:
486 			/* send ack before fin */
487 			dlm_send_ack(node->nodeid, node->seq_next);
488 
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));
492 
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.
501 				 */
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);
507 					goto send_fin;
508 				}
509 				break;
510 			case DLM_FIN_WAIT1:
511 				node->state = DLM_CLOSING;
512 				pr_debug("switch node %d to state %s\n",
513 					 node->nodeid, dlm_state_str(node->state));
514 				break;
515 			case DLM_FIN_WAIT2:
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);
520 				break;
521 			case DLM_LAST_ACK:
522 				/* probably remove_member caught it, do nothing */
523 				break;
524 			default:
525 				spin_unlock(&node->state_lock);
526 				log_print("%s: unexpected state: %d\n",
527 					  __func__, node->state);
528 				WARN_ON(1);
529 				return;
530 			}
531 			spin_unlock(&node->state_lock);
532 
533 			set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
534 			break;
535 		default:
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);
539 			break;
540 		}
541 	} else {
542 		/* retry to ack message which we already have by sending back
543 		 * current node->seq_next number as ack.
544 		 */
545 		if (seq < node->seq_next)
546 			dlm_send_ack(node->nodeid, node->seq_next);
547 
548 		log_print_ratelimited("ignore dlm msg because seq mismatch, seq: %u, expected: %u, nodeid: %d",
549 				      seq, node->seq_next, node->nodeid);
550 	}
551 
552 	return;
553 
554 send_fin:
555 	set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
556 	dlm_send_fin(node, dlm_pas_fin_ack_rcv);
557 }
558 
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))
562 {
563 	struct midcomms_node *node = NULL;
564 	gfp_t allocation = 0;
565 	int ret;
566 
567 	switch (p->header.h_cmd) {
568 	case DLM_RCOM:
569 		if (msglen < sizeof(struct dlm_rcom)) {
570 			log_print("rcom msg too small: %u, will skip this message from node %d",
571 				  msglen, nodeid);
572 			return NULL;
573 		}
574 
575 		switch (p->rcom.rc_type) {
576 		case cpu_to_le32(DLM_RCOM_NAMES):
577 			fallthrough;
578 		case cpu_to_le32(DLM_RCOM_NAMES_REPLY):
579 			fallthrough;
580 		case cpu_to_le32(DLM_RCOM_STATUS):
581 			fallthrough;
582 		case cpu_to_le32(DLM_RCOM_STATUS_REPLY):
583 			node = nodeid2node(nodeid, 0);
584 			if (node) {
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));
589 
590 				switch (node->state) {
591 				case DLM_CLOSED:
592 					node->state = DLM_ESTABLISHED;
593 					pr_debug("switch node %d to state %s\n",
594 						 node->nodeid, dlm_state_str(node->state));
595 					break;
596 				case DLM_ESTABLISHED:
597 					break;
598 				default:
599 					/* some invalid state passive shutdown
600 					 * was failed, we try to reset and
601 					 * hope it will go on.
602 					 */
603 					log_print("reset node %d because shutdown stuck",
604 						  node->nodeid);
605 
606 					midcomms_node_reset(node);
607 					node->state = DLM_ESTABLISHED;
608 					break;
609 				}
610 				spin_unlock(&node->state_lock);
611 			}
612 
613 			allocation = GFP_NOFS;
614 			break;
615 		default:
616 			break;
617 		}
618 
619 		break;
620 	default:
621 		break;
622 	}
623 
624 	node = nodeid2node(nodeid, allocation);
625 	if (!node) {
626 		switch (p->header.h_cmd) {
627 		case DLM_OPTS:
628 			if (msglen < sizeof(struct dlm_opts)) {
629 				log_print("opts msg too small: %u, will skip this message from node %d",
630 					  msglen, nodeid);
631 				return NULL;
632 			}
633 
634 			log_print_ratelimited("received dlm opts message nextcmd %d from node %d in an invalid sequence",
635 					      p->opts.o_nextcmd, nodeid);
636 			break;
637 		default:
638 			log_print_ratelimited("received dlm message cmd %d from node %d in an invalid sequence",
639 					      p->header.h_cmd, nodeid);
640 			break;
641 		}
642 
643 		return NULL;
644 	}
645 
646 	ret = cb(node);
647 	if (ret < 0)
648 		return NULL;
649 
650 	return node;
651 }
652 
653 static int dlm_midcomms_version_check_3_2(struct midcomms_node *node)
654 {
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,
659 			  node->nodeid);
660 		break;
661 	case DLM_VERSION_3_2:
662 		break;
663 	default:
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);
666 		return -1;
667 	}
668 
669 	return 0;
670 }
671 
672 static int dlm_opts_check_msglen(union dlm_packet *p, uint16_t msglen, int nodeid)
673 {
674 	int len = msglen;
675 
676 	/* we only trust outer header msglen because
677 	 * it's checked against receive buffer length.
678 	 */
679 	if (len < sizeof(struct dlm_opts))
680 		return -1;
681 	len -= sizeof(struct dlm_opts);
682 
683 	if (len < le16_to_cpu(p->opts.o_optlen))
684 		return -1;
685 	len -= le16_to_cpu(p->opts.o_optlen);
686 
687 	switch (p->opts.o_nextcmd) {
688 	case DLM_FIN:
689 		if (len < sizeof(struct dlm_header)) {
690 			log_print("fin too small: %d, will skip this message from node %d",
691 				  len, nodeid);
692 			return -1;
693 		}
694 
695 		break;
696 	case DLM_MSG:
697 		if (len < sizeof(struct dlm_message)) {
698 			log_print("msg too small: %d, will skip this message from node %d",
699 				  msglen, nodeid);
700 			return -1;
701 		}
702 
703 		break;
704 	case DLM_RCOM:
705 		if (len < sizeof(struct dlm_rcom)) {
706 			log_print("rcom msg too small: %d, will skip this message from node %d",
707 				  len, nodeid);
708 			return -1;
709 		}
710 
711 		break;
712 	default:
713 		log_print("unsupported o_nextcmd received: %u, will skip this message from node %d",
714 			  p->opts.o_nextcmd, nodeid);
715 		return -1;
716 	}
717 
718 	return 0;
719 }
720 
721 static void dlm_midcomms_receive_buffer_3_2(union dlm_packet *p, int nodeid)
722 {
723 	uint16_t msglen = le16_to_cpu(p->header.h_length);
724 	struct midcomms_node *node;
725 	uint32_t seq;
726 	int ret, idx;
727 
728 	idx = srcu_read_lock(&nodes_srcu);
729 	node = dlm_midcomms_recv_node_lookup(nodeid, p, msglen,
730 					     dlm_midcomms_version_check_3_2);
731 	if (!node)
732 		goto out;
733 
734 	switch (p->header.h_cmd) {
735 	case DLM_RCOM:
736 		/* these rcom message we use to determine version.
737 		 * they have their own retransmission handling and
738 		 * are the first messages of dlm.
739 		 *
740 		 * length already checked.
741 		 */
742 		switch (p->rcom.rc_type) {
743 		case cpu_to_le32(DLM_RCOM_NAMES):
744 			fallthrough;
745 		case cpu_to_le32(DLM_RCOM_NAMES_REPLY):
746 			fallthrough;
747 		case cpu_to_le32(DLM_RCOM_STATUS):
748 			fallthrough;
749 		case cpu_to_le32(DLM_RCOM_STATUS_REPLY):
750 			break;
751 		default:
752 			log_print("unsupported rcom type received: %u, will skip this message from node %d",
753 				  le32_to_cpu(p->rcom.rc_type), nodeid);
754 			goto out;
755 		}
756 
757 		WARN_ON(test_bit(DLM_NODE_FLAG_STOP_RX, &node->flags));
758 		dlm_receive_buffer(p, nodeid);
759 		break;
760 	case DLM_OPTS:
761 		seq = le32_to_cpu(p->header.u.h_seq);
762 
763 		ret = dlm_opts_check_msglen(p, msglen, nodeid);
764 		if (ret < 0) {
765 			log_print("opts msg too small: %u, will skip this message from node %d",
766 				  msglen, nodeid);
767 			goto out;
768 		}
769 
770 		p = (union dlm_packet *)((unsigned char *)p->opts.o_opts +
771 					 le16_to_cpu(p->opts.o_optlen));
772 
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) {
776 		case DLM_RCOM:
777 			if (msglen < sizeof(struct dlm_rcom)) {
778 				log_print("inner rcom msg too small: %u, will skip this message from node %d",
779 					  msglen, nodeid);
780 				goto out;
781 			}
782 
783 			break;
784 		case DLM_MSG:
785 			if (msglen < sizeof(struct dlm_message)) {
786 				log_print("inner msg too small: %u, will skip this message from node %d",
787 					  msglen, nodeid);
788 				goto out;
789 			}
790 
791 			break;
792 		case DLM_FIN:
793 			if (msglen < sizeof(struct dlm_header)) {
794 				log_print("inner fin too small: %u, will skip this message from node %d",
795 					  msglen, nodeid);
796 				goto out;
797 			}
798 
799 			break;
800 		default:
801 			log_print("unsupported inner h_cmd received: %u, will skip this message from node %d",
802 				  msglen, nodeid);
803 			goto out;
804 		}
805 
806 		dlm_midcomms_receive_buffer(p, node, seq);
807 		break;
808 	case DLM_ACK:
809 		seq = le32_to_cpu(p->header.u.h_seq);
810 		dlm_receive_ack(node, seq);
811 		break;
812 	default:
813 		log_print("unsupported h_cmd received: %u, will skip this message from node %d",
814 			  p->header.h_cmd, nodeid);
815 		break;
816 	}
817 
818 out:
819 	srcu_read_unlock(&nodes_srcu, idx);
820 }
821 
822 static int dlm_midcomms_version_check_3_1(struct midcomms_node *node)
823 {
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,
828 			  node->nodeid);
829 		break;
830 	case DLM_VERSION_3_1:
831 		break;
832 	default:
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);
835 		return -1;
836 	}
837 
838 	return 0;
839 }
840 
841 static void dlm_midcomms_receive_buffer_3_1(union dlm_packet *p, int nodeid)
842 {
843 	uint16_t msglen = le16_to_cpu(p->header.h_length);
844 	struct midcomms_node *node;
845 	int idx;
846 
847 	idx = srcu_read_lock(&nodes_srcu);
848 	node = dlm_midcomms_recv_node_lookup(nodeid, p, msglen,
849 					     dlm_midcomms_version_check_3_1);
850 	if (!node) {
851 		srcu_read_unlock(&nodes_srcu, idx);
852 		return;
853 	}
854 	srcu_read_unlock(&nodes_srcu, idx);
855 
856 	switch (p->header.h_cmd) {
857 	case DLM_RCOM:
858 		/* length already checked */
859 		break;
860 	case DLM_MSG:
861 		if (msglen < sizeof(struct dlm_message)) {
862 			log_print("msg too small: %u, will skip this message from node %d",
863 				  msglen, nodeid);
864 			return;
865 		}
866 
867 		break;
868 	default:
869 		log_print("unsupported h_cmd received: %u, will skip this message from node %d",
870 			  p->header.h_cmd, nodeid);
871 		return;
872 	}
873 
874 	dlm_receive_buffer(p, nodeid);
875 }
876 
877 /*
878  * Called from the low-level comms layer to process a buffer of
879  * commands.
880  */
881 
882 int dlm_process_incoming_buffer(int nodeid, unsigned char *buf, int len)
883 {
884 	const unsigned char *ptr = buf;
885 	const struct dlm_header *hd;
886 	uint16_t msglen;
887 	int ret = 0;
888 
889 	while (len >= sizeof(struct dlm_header)) {
890 		hd = (struct dlm_header *)ptr;
891 
892 		/* no message should be more than DLM_MAX_SOCKET_BUFSIZE or
893 		 * less than dlm_header size.
894 		 *
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.
902 		 */
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",
907 				  msglen, nodeid);
908 			return -EBADMSG;
909 		}
910 
911 		/* caller will take care that leftover
912 		 * will be parsed next call with more data
913 		 */
914 		if (msglen > len)
915 			break;
916 
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);
920 			break;
921 		case cpu_to_le32(DLM_VERSION_3_2):
922 			dlm_midcomms_receive_buffer_3_2((union dlm_packet *)ptr, nodeid);
923 			break;
924 		default:
925 			log_print("received invalid version header: %u from node %d, will skip this message",
926 				  le32_to_cpu(hd->h_version), nodeid);
927 			break;
928 		}
929 
930 		ret += msglen;
931 		len -= msglen;
932 		ptr += msglen;
933 	}
934 
935 	return ret;
936 }
937 
938 void dlm_midcomms_receive_done(int nodeid)
939 {
940 	struct midcomms_node *node;
941 	int idx;
942 
943 	idx = srcu_read_lock(&nodes_srcu);
944 	node = nodeid2node(nodeid, 0);
945 	if (!node) {
946 		srcu_read_unlock(&nodes_srcu, idx);
947 		return;
948 	}
949 
950 	/* old protocol, we do nothing */
951 	switch (node->version) {
952 	case DLM_VERSION_3_2:
953 		break;
954 	default:
955 		srcu_read_unlock(&nodes_srcu, idx);
956 		return;
957 	}
958 
959 	/* do nothing if we didn't delivered stateful to ulp */
960 	if (!test_and_clear_bit(DLM_NODE_ULP_DELIVERED,
961 				&node->flags)) {
962 		srcu_read_unlock(&nodes_srcu, idx);
963 		return;
964 	}
965 
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);
972 		break;
973 	default:
974 		spin_unlock(&node->state_lock);
975 		/* do nothing FIN has it's own ack send */
976 		break;
977 	}
978 	srcu_read_unlock(&nodes_srcu, idx);
979 }
980 
981 void dlm_midcomms_unack_msg_resend(int nodeid)
982 {
983 	struct midcomms_node *node;
984 	struct dlm_mhandle *mh;
985 	int idx, ret;
986 
987 	idx = srcu_read_lock(&nodes_srcu);
988 	node = nodeid2node(nodeid, 0);
989 	if (!node) {
990 		srcu_read_unlock(&nodes_srcu, idx);
991 		return;
992 	}
993 
994 	/* old protocol, we don't support to retransmit on failure */
995 	switch (node->version) {
996 	case DLM_VERSION_3_2:
997 		break;
998 	default:
999 		srcu_read_unlock(&nodes_srcu, idx);
1000 		return;
1001 	}
1002 
1003 	rcu_read_lock();
1004 	list_for_each_entry_rcu(mh, &node->send_queue, list) {
1005 		if (!mh->committed)
1006 			continue;
1007 
1008 		ret = dlm_lowcomms_resend_msg(mh->msg);
1009 		if (!ret)
1010 			log_print_ratelimited("retransmit dlm msg, seq %u, nodeid %d",
1011 					      mh->seq, node->nodeid);
1012 	}
1013 	rcu_read_unlock();
1014 	srcu_read_unlock(&nodes_srcu, idx);
1015 }
1016 
1017 static void dlm_fill_opts_header(struct dlm_opts *opts, uint16_t inner_len,
1018 				 uint32_t seq)
1019 {
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);
1025 }
1026 
1027 static void midcomms_new_msg_cb(void *data)
1028 {
1029 	struct dlm_mhandle *mh = data;
1030 
1031 	atomic_inc(&mh->node->send_queue_cnt);
1032 
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);
1036 
1037 	mh->seq = mh->node->seq_send++;
1038 }
1039 
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)
1042 {
1043 	struct dlm_opts *opts;
1044 	struct dlm_msg *msg;
1045 
1046 	msg = dlm_lowcomms_new_msg(nodeid, len + DLM_MIDCOMMS_OPT_LEN,
1047 				   allocation, ppc, midcomms_new_msg_cb, mh);
1048 	if (!msg)
1049 		return NULL;
1050 
1051 	opts = (struct dlm_opts *)*ppc;
1052 	mh->opts = opts;
1053 
1054 	/* add possible options here */
1055 	dlm_fill_opts_header(opts, len, mh->seq);
1056 
1057 	*ppc += sizeof(*opts);
1058 	mh->inner_hd = (const struct dlm_header *)*ppc;
1059 	return msg;
1060 }
1061 
1062 /* avoid false positive for nodes_srcu, unlock happens in
1063  * dlm_midcomms_commit_mhandle which is a must call if success
1064  */
1065 #ifndef __CHECKER__
1066 struct dlm_mhandle *dlm_midcomms_get_mhandle(int nodeid, int len,
1067 					     gfp_t allocation, char **ppc)
1068 {
1069 	struct midcomms_node *node;
1070 	struct dlm_mhandle *mh;
1071 	struct dlm_msg *msg;
1072 	int idx;
1073 
1074 	idx = srcu_read_lock(&nodes_srcu);
1075 	node = nodeid2node(nodeid, 0);
1076 	if (!node) {
1077 		WARN_ON_ONCE(1);
1078 		goto err;
1079 	}
1080 
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));
1083 
1084 	mh = dlm_allocate_mhandle();
1085 	if (!mh)
1086 		goto err;
1087 
1088 	mh->committed = false;
1089 	mh->ack_rcv = NULL;
1090 	mh->idx = idx;
1091 	mh->node = node;
1092 
1093 	switch (node->version) {
1094 	case DLM_VERSION_3_1:
1095 		msg = dlm_lowcomms_new_msg(nodeid, len, allocation, ppc,
1096 					   NULL, NULL);
1097 		if (!msg) {
1098 			dlm_free_mhandle(mh);
1099 			goto err;
1100 		}
1101 
1102 		break;
1103 	case DLM_VERSION_3_2:
1104 		msg = dlm_midcomms_get_msg_3_2(mh, nodeid, len, allocation,
1105 					       ppc);
1106 		if (!msg) {
1107 			dlm_free_mhandle(mh);
1108 			goto err;
1109 		}
1110 
1111 		break;
1112 	default:
1113 		dlm_free_mhandle(mh);
1114 		WARN_ON(1);
1115 		goto err;
1116 	}
1117 
1118 	mh->msg = msg;
1119 
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.
1124 	 */
1125 	return mh;
1126 
1127 err:
1128 	srcu_read_unlock(&nodes_srcu, idx);
1129 	return NULL;
1130 }
1131 #endif
1132 
1133 static void dlm_midcomms_commit_msg_3_2(struct dlm_mhandle *mh)
1134 {
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);
1139 }
1140 
1141 /* avoid false positive for nodes_srcu, lock was happen in
1142  * dlm_midcomms_get_mhandle
1143  */
1144 #ifndef __CHECKER__
1145 void dlm_midcomms_commit_mhandle(struct dlm_mhandle *mh)
1146 {
1147 	switch (mh->node->version) {
1148 	case DLM_VERSION_3_1:
1149 		srcu_read_unlock(&nodes_srcu, mh->idx);
1150 
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);
1155 		break;
1156 	case DLM_VERSION_3_2:
1157 		dlm_midcomms_commit_msg_3_2(mh);
1158 		srcu_read_unlock(&nodes_srcu, mh->idx);
1159 		break;
1160 	default:
1161 		srcu_read_unlock(&nodes_srcu, mh->idx);
1162 		WARN_ON(1);
1163 		break;
1164 	}
1165 }
1166 #endif
1167 
1168 int dlm_midcomms_start(void)
1169 {
1170 	int i;
1171 
1172 	for (i = 0; i < CONN_HASH_SIZE; i++)
1173 		INIT_HLIST_HEAD(&node_hash[i]);
1174 
1175 	return dlm_lowcomms_start();
1176 }
1177 
1178 static void dlm_act_fin_ack_rcv(struct midcomms_node *node)
1179 {
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));
1183 
1184 	switch (node->state) {
1185 	case DLM_FIN_WAIT1:
1186 		node->state = DLM_FIN_WAIT2;
1187 		pr_debug("switch node %d to state %s\n",
1188 			 node->nodeid, dlm_state_str(node->state));
1189 		break;
1190 	case DLM_CLOSING:
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);
1195 		break;
1196 	case DLM_CLOSED:
1197 		/* not valid but somehow we got what we want */
1198 		wake_up(&node->shutdown_wait);
1199 		break;
1200 	default:
1201 		spin_unlock(&node->state_lock);
1202 		log_print("%s: unexpected state: %d\n",
1203 			  __func__, node->state);
1204 		WARN_ON(1);
1205 		return;
1206 	}
1207 	spin_unlock(&node->state_lock);
1208 }
1209 
1210 void dlm_midcomms_add_member(int nodeid)
1211 {
1212 	struct midcomms_node *node;
1213 	int idx;
1214 
1215 	if (nodeid == dlm_our_nodeid())
1216 		return;
1217 
1218 	idx = srcu_read_lock(&nodes_srcu);
1219 	node = nodeid2node(nodeid, GFP_NOFS);
1220 	if (!node) {
1221 		srcu_read_unlock(&nodes_srcu, idx);
1222 		return;
1223 	}
1224 
1225 	spin_lock(&node->state_lock);
1226 	if (!node->users) {
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:
1231 			break;
1232 		case DLM_CLOSED:
1233 			node->state = DLM_ESTABLISHED;
1234 			pr_debug("switch node %d to state %s\n",
1235 				 node->nodeid, dlm_state_str(node->state));
1236 			break;
1237 		default:
1238 			/* some invalid state passive shutdown
1239 			 * was failed, we try to reset and
1240 			 * hope it will go on.
1241 			 */
1242 			log_print("reset node %d because shutdown stuck",
1243 				  node->nodeid);
1244 
1245 			midcomms_node_reset(node);
1246 			node->state = DLM_ESTABLISHED;
1247 			break;
1248 		}
1249 	}
1250 
1251 	node->users++;
1252 	pr_debug("node %d users inc count %d\n", nodeid, node->users);
1253 	spin_unlock(&node->state_lock);
1254 
1255 	srcu_read_unlock(&nodes_srcu, idx);
1256 }
1257 
1258 void dlm_midcomms_remove_member(int nodeid)
1259 {
1260 	struct midcomms_node *node;
1261 	int idx;
1262 
1263 	if (nodeid == dlm_our_nodeid())
1264 		return;
1265 
1266 	idx = srcu_read_lock(&nodes_srcu);
1267 	node = nodeid2node(nodeid, 0);
1268 	if (!node) {
1269 		srcu_read_unlock(&nodes_srcu, idx);
1270 		return;
1271 	}
1272 
1273 	spin_lock(&node->state_lock);
1274 	node->users--;
1275 	pr_debug("node %d users dec count %d\n", nodeid, node->users);
1276 
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.
1280 	 */
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:
1286 			break;
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);
1291 
1292 			pr_debug("switch node %d to state %s case 2\n",
1293 				 node->nodeid, dlm_state_str(node->state));
1294 			goto send_fin;
1295 		case DLM_LAST_ACK:
1296 			/* probably receive fin caught it, do nothing */
1297 			break;
1298 		case DLM_CLOSED:
1299 			/* already gone, do nothing */
1300 			break;
1301 		default:
1302 			log_print("%s: unexpected state: %d\n",
1303 				  __func__, node->state);
1304 			break;
1305 		}
1306 	}
1307 	spin_unlock(&node->state_lock);
1308 
1309 	srcu_read_unlock(&nodes_srcu, idx);
1310 	return;
1311 
1312 send_fin:
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);
1316 }
1317 
1318 static void midcomms_node_release(struct rcu_head *rcu)
1319 {
1320 	struct midcomms_node *node = container_of(rcu, struct midcomms_node, rcu);
1321 
1322 	WARN_ON(atomic_read(&node->send_queue_cnt));
1323 	kfree(node);
1324 }
1325 
1326 static void midcomms_shutdown(struct midcomms_node *node)
1327 {
1328 	int ret;
1329 
1330 	/* old protocol, we don't wait for pending operations */
1331 	switch (node->version) {
1332 	case DLM_VERSION_3_2:
1333 		break;
1334 	default:
1335 		return;
1336 	}
1337 
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));
1346 		break;
1347 	case DLM_CLOSED:
1348 		/* we have what we want */
1349 		spin_unlock(&node->state_lock);
1350 		return;
1351 	default:
1352 		/* busy to enter DLM_FIN_WAIT1, wait until passive
1353 		 * done in shutdown_wait to enter DLM_CLOSED.
1354 		 */
1355 		break;
1356 	}
1357 	spin_unlock(&node->state_lock);
1358 
1359 	if (node->state == DLM_FIN_WAIT1) {
1360 		dlm_send_fin(node, dlm_act_fin_ack_rcv);
1361 
1362 		if (DLM_DEBUG_FENCE_TERMINATION)
1363 			msleep(5000);
1364 	}
1365 
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);
1375 		return;
1376 	}
1377 
1378 	pr_debug("active shutdown done for node %d with state %s\n",
1379 		 node->nodeid, dlm_state_str(node->state));
1380 }
1381 
1382 void dlm_midcomms_shutdown(void)
1383 {
1384 	struct midcomms_node *node;
1385 	int i, idx;
1386 
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);
1392 
1393 			dlm_delete_debug_comms_file(node->debugfs);
1394 
1395 			spin_lock(&nodes_lock);
1396 			hlist_del_rcu(&node->hlist);
1397 			spin_unlock(&nodes_lock);
1398 
1399 			call_srcu(&nodes_srcu, &node->rcu, midcomms_node_release);
1400 		}
1401 	}
1402 	srcu_read_unlock(&nodes_srcu, idx);
1403 	mutex_unlock(&close_lock);
1404 
1405 	dlm_lowcomms_shutdown();
1406 }
1407 
1408 int dlm_midcomms_close(int nodeid)
1409 {
1410 	struct midcomms_node *node;
1411 	int idx, ret;
1412 
1413 	if (nodeid == dlm_our_nodeid())
1414 		return 0;
1415 
1416 	dlm_stop_lockspaces_check();
1417 
1418 	idx = srcu_read_lock(&nodes_srcu);
1419 	/* Abort pending close/remove operation */
1420 	node = nodeid2node(nodeid, 0);
1421 	if (node) {
1422 		/* let shutdown waiters leave */
1423 		set_bit(DLM_NODE_FLAG_CLOSE, &node->flags);
1424 		wake_up(&node->shutdown_wait);
1425 	}
1426 	srcu_read_unlock(&nodes_srcu, idx);
1427 
1428 	synchronize_srcu(&nodes_srcu);
1429 
1430 	idx = srcu_read_lock(&nodes_srcu);
1431 	mutex_lock(&close_lock);
1432 	node = nodeid2node(nodeid, 0);
1433 	if (!node) {
1434 		mutex_unlock(&close_lock);
1435 		srcu_read_unlock(&nodes_srcu, idx);
1436 		return dlm_lowcomms_close(nodeid);
1437 	}
1438 
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);
1445 
1446 	return ret;
1447 }
1448 
1449 /* debug functionality to send raw dlm msg from user space */
1450 struct dlm_rawmsg_data {
1451 	struct midcomms_node *node;
1452 	void *buf;
1453 };
1454 
1455 static void midcomms_new_rawmsg_cb(void *data)
1456 {
1457 	struct dlm_rawmsg_data *rd = data;
1458 	struct dlm_header *h = rd->buf;
1459 
1460 	switch (h->h_version) {
1461 	case cpu_to_le32(DLM_VERSION_3_1):
1462 		break;
1463 	default:
1464 		switch (h->h_cmd) {
1465 		case DLM_OPTS:
1466 			if (!h->u.h_seq)
1467 				h->u.h_seq = cpu_to_le32(rd->node->seq_send++);
1468 			break;
1469 		default:
1470 			break;
1471 		}
1472 		break;
1473 	}
1474 }
1475 
1476 int dlm_midcomms_rawmsg_send(struct midcomms_node *node, void *buf,
1477 			     int buflen)
1478 {
1479 	struct dlm_rawmsg_data rd;
1480 	struct dlm_msg *msg;
1481 	char *msgbuf;
1482 
1483 	rd.node = node;
1484 	rd.buf = buf;
1485 
1486 	msg = dlm_lowcomms_new_msg(node->nodeid, buflen, GFP_NOFS,
1487 				   &msgbuf, midcomms_new_rawmsg_cb, &rd);
1488 	if (!msg)
1489 		return -ENOMEM;
1490 
1491 	memcpy(msgbuf, buf, buflen);
1492 	dlm_lowcomms_commit_msg(msg);
1493 	return 0;
1494 }
1495 
1496