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