fs: dlm: add dst nodeid for msg tracing
[linux-block.git] / fs / dlm / midcomms.c
CommitLineData
2522fe45 1// SPDX-License-Identifier: GPL-2.0-only
e7fd4179
DT
2/******************************************************************************
3*******************************************************************************
4**
5** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
489d8e55 6** Copyright (C) 2004-2021 Red Hat, Inc. All rights reserved.
e7fd4179 7**
e7fd4179
DT
8**
9*******************************************************************************
10******************************************************************************/
11
12/*
13 * midcomms.c
14 *
489d8e55
AA
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.
e7fd4179 18 *
489d8e55 19 * How it works:
e7fd4179 20 *
489d8e55
AA
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 *
489d8e55
AA
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.
e7fd4179
DT
122 */
123
489d8e55
AA
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
e01c4b7b 135#include <trace/events/dlm.h>
489d8e55
AA
136#include <net/tcp.h>
137
e7fd4179 138#include "dlm_internal.h"
2c3fa6ae 139#include "lockspace.h"
e7fd4179
DT
140#include "lowcomms.h"
141#include "config.h"
6c547f26 142#include "memory.h"
e7fd4179 143#include "lock.h"
489d8e55 144#include "util.h"
e7fd4179
DT
145#include "midcomms.h"
146
489d8e55
AA
147/* init value for sequence numbers for testing purpose only e.g. overflows */
148#define DLM_SEQ_INIT 0
149/* 3 minutes wait to sync ending of dlm */
150#define DLM_SHUTDOWN_TIMEOUT msecs_to_jiffies(3 * 60 * 1000)
151#define DLM_VERSION_NOT_SET 0
152
153struct 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
b97f8525 169#define DLM_NODE_ULP_DELIVERED 4
489d8e55
AA
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
5b2f981f
AA
190 /* not protected by srcu, node_hash lifetime */
191 void *debugfs;
192
489d8e55
AA
193 struct hlist_node hlist;
194 struct rcu_head rcu;
195};
196
197struct dlm_mhandle {
5b787667 198 const union dlm_packet *inner_p;
489d8e55
AA
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
214static struct hlist_head node_hash[CONN_HASH_SIZE];
215static DEFINE_SPINLOCK(nodes_lock);
216DEFINE_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 */
224static DEFINE_MUTEX(close_lock);
225
6c547f26
AA
226struct 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
489d8e55 232static inline const char *dlm_state_str(int state)
a070a91c 233{
489d8e55
AA
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 }
a070a91c
AA
252}
253
5b2f981f
AA
254const char *dlm_midcomms_state(struct midcomms_node *node)
255{
256 return dlm_state_str(node->state);
257}
258
259unsigned long dlm_midcomms_flags(struct midcomms_node *node)
260{
261 return node->flags;
262}
263
264int dlm_midcomms_send_queue_cnt(struct midcomms_node *node)
265{
266 return atomic_read(&node->send_queue_cnt);
267}
268
269uint32_t dlm_midcomms_version(struct midcomms_node *node)
270{
271 return node->version;
272}
273
489d8e55 274static struct midcomms_node *__find_node(int nodeid, int r)
a070a91c 275{
489d8e55
AA
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;
a070a91c
AA
284}
285
489d8e55
AA
286static void dlm_mhandle_release(struct rcu_head *rcu)
287{
288 struct dlm_mhandle *mh = container_of(rcu, struct dlm_mhandle, rcu);
a070a91c 289
489d8e55 290 dlm_lowcomms_put_msg(mh->msg);
6c547f26 291 dlm_free_mhandle(mh);
489d8e55 292}
a070a91c 293
f5fe8d51
AA
294static 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
489d8e55 302static void dlm_send_queue_flush(struct midcomms_node *node)
a070a91c 303{
489d8e55
AA
304 struct dlm_mhandle *mh;
305
306 pr_debug("flush midcomms send queue of node %d\n", node->nodeid);
307
308 rcu_read_lock();
f5fe8d51 309 spin_lock(&node->send_queue_lock);
489d8e55 310 list_for_each_entry_rcu(mh, &node->send_queue, list) {
f5fe8d51 311 dlm_mhandle_delete(node, mh);
489d8e55 312 }
f5fe8d51 313 spin_unlock(&node->send_queue_lock);
489d8e55 314 rcu_read_unlock();
a070a91c
AA
315}
316
489d8e55 317static void midcomms_node_reset(struct midcomms_node *node)
a070a91c 318{
489d8e55
AA
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);
a070a91c
AA
329}
330
489d8e55
AA
331static 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);
5b2f981f
AA
366
367 node->debugfs = dlm_create_debug_comms_file(nodeid, node);
489d8e55
AA
368 return node;
369}
370
371static 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_NOFS, &ppc,
379 NULL, NULL);
380 if (!msg)
381 return -ENOMEM;
382
383 m_header = (struct dlm_header *)ppc;
384
3428785a
AA
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);
489d8e55 388 m_header->h_cmd = DLM_ACK;
3428785a 389 m_header->u.h_seq = cpu_to_le32(seq);
489d8e55 390
489d8e55
AA
391 dlm_lowcomms_commit_msg(msg);
392 dlm_lowcomms_put_msg(msg);
393
394 return 0;
395}
396
397static 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_NOFS, &ppc);
406 if (!mh)
407 return -ENOMEM;
408
409 mh->ack_rcv = ack_rcv;
410
411 m_header = (struct dlm_header *)ppc;
412
3428785a
AA
413 m_header->h_version = cpu_to_le32(DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
414 m_header->h_nodeid = cpu_to_le32(dlm_our_nodeid());
415 m_header->h_length = cpu_to_le16(mb_len);
489d8e55
AA
416 m_header->h_cmd = DLM_FIN;
417
489d8e55 418 pr_debug("sending fin msg to node %d\n", node->nodeid);
e01c4b7b 419 dlm_midcomms_commit_mhandle(mh, NULL, 0);
489d8e55
AA
420 set_bit(DLM_NODE_FLAG_STOP_TX, &node->flags);
421
422 return 0;
423}
424
425static 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)) {
489d8e55
AA
432 if (mh->ack_rcv)
433 mh->ack_rcv(node);
f5fe8d51
AA
434 } else {
435 /* send queue should be ordered */
436 break;
437 }
438 }
489d8e55 439
f5fe8d51
AA
440 spin_lock(&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);
489d8e55
AA
444 } else {
445 /* send queue should be ordered */
446 break;
447 }
448 }
f5fe8d51 449 spin_unlock(&node->send_queue_lock);
489d8e55
AA
450 rcu_read_unlock();
451}
452
453static 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\n",
471 __func__, node->state);
775af207 472 WARN_ON_ONCE(1);
489d8e55
AA
473 return;
474 }
475 spin_unlock(&node->state_lock);
476}
477
e01c4b7b
AA
478static 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:
17827754 482 trace_dlm_recv_message(dlm_our_nodeid(), seq, &p->message);
e01c4b7b
AA
483 break;
484 case DLM_RCOM:
17827754 485 trace_dlm_recv_rcom(dlm_our_nodeid(), seq, &p->rcom);
e01c4b7b
AA
486 break;
487 default:
488 break;
489 }
490}
491
489d8e55
AA
492static 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++;
489d8e55
AA
498
499 switch (p->header.h_cmd) {
500 case DLM_FIN:
b97f8525
AA
501 /* send ack before fin */
502 dlm_send_ack(node->nodeid, node->seq_next);
503
489d8e55
AA
504 spin_lock(&node->state_lock);
505 pr_debug("receive fin msg from node %d with state %s\n",
506 node->nodeid, dlm_state_str(node->state));
507
508 switch (node->state) {
509 case DLM_ESTABLISHED:
510 node->state = DLM_CLOSE_WAIT;
511 pr_debug("switch node %d to state %s\n",
512 node->nodeid, dlm_state_str(node->state));
513 /* passive shutdown DLM_LAST_ACK case 1
514 * additional we check if the node is used by
515 * cluster manager events at all.
516 */
517 if (node->users == 0) {
518 node->state = DLM_LAST_ACK;
519 pr_debug("switch node %d to state %s case 1\n",
520 node->nodeid, dlm_state_str(node->state));
521 spin_unlock(&node->state_lock);
522 goto send_fin;
523 }
524 break;
525 case DLM_FIN_WAIT1:
526 node->state = DLM_CLOSING;
527 pr_debug("switch node %d to state %s\n",
528 node->nodeid, dlm_state_str(node->state));
529 break;
530 case DLM_FIN_WAIT2:
531 midcomms_node_reset(node);
532 pr_debug("switch node %d to state %s\n",
533 node->nodeid, dlm_state_str(node->state));
534 wake_up(&node->shutdown_wait);
535 break;
536 case DLM_LAST_ACK:
537 /* probably remove_member caught it, do nothing */
538 break;
539 default:
540 spin_unlock(&node->state_lock);
541 log_print("%s: unexpected state: %d\n",
542 __func__, node->state);
775af207 543 WARN_ON_ONCE(1);
489d8e55
AA
544 return;
545 }
546 spin_unlock(&node->state_lock);
547
548 set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
549 break;
550 default:
775af207 551 WARN_ON_ONCE(test_bit(DLM_NODE_FLAG_STOP_RX, &node->flags));
e01c4b7b 552 dlm_receive_buffer_3_2_trace(seq, p);
489d8e55 553 dlm_receive_buffer(p, node->nodeid);
b97f8525 554 set_bit(DLM_NODE_ULP_DELIVERED, &node->flags);
489d8e55
AA
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 return;
569
570send_fin:
571 set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
572 dlm_send_fin(node, dlm_pas_fin_ack_rcv);
573}
574
575static struct midcomms_node *
576dlm_midcomms_recv_node_lookup(int nodeid, const union dlm_packet *p,
577 uint16_t msglen, int (*cb)(struct midcomms_node *node))
578{
579 struct midcomms_node *node = NULL;
580 gfp_t allocation = 0;
581 int ret;
582
583 switch (p->header.h_cmd) {
584 case DLM_RCOM:
585 if (msglen < sizeof(struct dlm_rcom)) {
586 log_print("rcom msg too small: %u, will skip this message from node %d",
587 msglen, nodeid);
588 return NULL;
589 }
590
14a92fd7
AA
591 switch (p->rcom.rc_type) {
592 case cpu_to_le32(DLM_RCOM_NAMES):
489d8e55 593 fallthrough;
14a92fd7 594 case cpu_to_le32(DLM_RCOM_NAMES_REPLY):
489d8e55 595 fallthrough;
14a92fd7 596 case cpu_to_le32(DLM_RCOM_STATUS):
489d8e55 597 fallthrough;
14a92fd7 598 case cpu_to_le32(DLM_RCOM_STATUS_REPLY):
489d8e55
AA
599 node = nodeid2node(nodeid, 0);
600 if (node) {
601 spin_lock(&node->state_lock);
602 if (node->state != DLM_ESTABLISHED)
603 pr_debug("receive begin RCOM msg from node %d with state %s\n",
604 node->nodeid, dlm_state_str(node->state));
605
606 switch (node->state) {
607 case DLM_CLOSED:
608 node->state = DLM_ESTABLISHED;
609 pr_debug("switch node %d to state %s\n",
610 node->nodeid, dlm_state_str(node->state));
611 break;
612 case DLM_ESTABLISHED:
613 break;
614 default:
615 /* some invalid state passive shutdown
616 * was failed, we try to reset and
617 * hope it will go on.
618 */
7d3848c0 619 log_print("reset node %d because shutdown stuck",
489d8e55
AA
620 node->nodeid);
621
622 midcomms_node_reset(node);
623 node->state = DLM_ESTABLISHED;
624 break;
625 }
626 spin_unlock(&node->state_lock);
627 }
628
629 allocation = GFP_NOFS;
630 break;
631 default:
632 break;
633 }
634
635 break;
636 default:
637 break;
638 }
639
640 node = nodeid2node(nodeid, allocation);
641 if (!node) {
957adb68
AA
642 switch (p->header.h_cmd) {
643 case DLM_OPTS:
644 if (msglen < sizeof(struct dlm_opts)) {
645 log_print("opts msg too small: %u, will skip this message from node %d",
646 msglen, nodeid);
647 return NULL;
648 }
649
650 log_print_ratelimited("received dlm opts message nextcmd %d from node %d in an invalid sequence",
651 p->opts.o_nextcmd, nodeid);
652 break;
653 default:
654 log_print_ratelimited("received dlm message cmd %d from node %d in an invalid sequence",
655 p->header.h_cmd, nodeid);
656 break;
657 }
658
489d8e55
AA
659 return NULL;
660 }
661
662 ret = cb(node);
663 if (ret < 0)
664 return NULL;
665
666 return node;
667}
668
669static int dlm_midcomms_version_check_3_2(struct midcomms_node *node)
670{
671 switch (node->version) {
672 case DLM_VERSION_NOT_SET:
673 node->version = DLM_VERSION_3_2;
674 log_print("version 0x%08x for node %d detected", DLM_VERSION_3_2,
675 node->nodeid);
676 break;
677 case DLM_VERSION_3_2:
678 break;
679 default:
680 log_print_ratelimited("version mismatch detected, assumed 0x%08x but node %d has 0x%08x",
681 DLM_VERSION_3_2, node->nodeid, node->version);
682 return -1;
683 }
684
685 return 0;
686}
687
688static int dlm_opts_check_msglen(union dlm_packet *p, uint16_t msglen, int nodeid)
689{
690 int len = msglen;
691
692 /* we only trust outer header msglen because
693 * it's checked against receive buffer length.
694 */
695 if (len < sizeof(struct dlm_opts))
696 return -1;
697 len -= sizeof(struct dlm_opts);
698
699 if (len < le16_to_cpu(p->opts.o_optlen))
700 return -1;
701 len -= le16_to_cpu(p->opts.o_optlen);
702
703 switch (p->opts.o_nextcmd) {
704 case DLM_FIN:
705 if (len < sizeof(struct dlm_header)) {
706 log_print("fin too small: %d, will skip this message from node %d",
707 len, nodeid);
708 return -1;
709 }
710
711 break;
712 case DLM_MSG:
713 if (len < sizeof(struct dlm_message)) {
714 log_print("msg too small: %d, will skip this message from node %d",
715 msglen, nodeid);
716 return -1;
717 }
718
719 break;
720 case DLM_RCOM:
721 if (len < sizeof(struct dlm_rcom)) {
722 log_print("rcom msg too small: %d, will skip this message from node %d",
723 len, nodeid);
724 return -1;
725 }
726
727 break;
728 default:
729 log_print("unsupported o_nextcmd received: %u, will skip this message from node %d",
730 p->opts.o_nextcmd, nodeid);
731 return -1;
732 }
733
734 return 0;
735}
736
737static void dlm_midcomms_receive_buffer_3_2(union dlm_packet *p, int nodeid)
738{
739 uint16_t msglen = le16_to_cpu(p->header.h_length);
740 struct midcomms_node *node;
741 uint32_t seq;
742 int ret, idx;
743
744 idx = srcu_read_lock(&nodes_srcu);
745 node = dlm_midcomms_recv_node_lookup(nodeid, p, msglen,
746 dlm_midcomms_version_check_3_2);
747 if (!node)
748 goto out;
749
750 switch (p->header.h_cmd) {
751 case DLM_RCOM:
752 /* these rcom message we use to determine version.
753 * they have their own retransmission handling and
754 * are the first messages of dlm.
755 *
756 * length already checked.
757 */
14a92fd7
AA
758 switch (p->rcom.rc_type) {
759 case cpu_to_le32(DLM_RCOM_NAMES):
489d8e55 760 fallthrough;
14a92fd7 761 case cpu_to_le32(DLM_RCOM_NAMES_REPLY):
489d8e55 762 fallthrough;
14a92fd7 763 case cpu_to_le32(DLM_RCOM_STATUS):
489d8e55 764 fallthrough;
14a92fd7 765 case cpu_to_le32(DLM_RCOM_STATUS_REPLY):
489d8e55
AA
766 break;
767 default:
768 log_print("unsupported rcom type received: %u, will skip this message from node %d",
769 le32_to_cpu(p->rcom.rc_type), nodeid);
770 goto out;
771 }
772
775af207 773 WARN_ON_ONCE(test_bit(DLM_NODE_FLAG_STOP_RX, &node->flags));
489d8e55
AA
774 dlm_receive_buffer(p, nodeid);
775 break;
776 case DLM_OPTS:
777 seq = le32_to_cpu(p->header.u.h_seq);
778
779 ret = dlm_opts_check_msglen(p, msglen, nodeid);
780 if (ret < 0) {
781 log_print("opts msg too small: %u, will skip this message from node %d",
782 msglen, nodeid);
783 goto out;
784 }
785
786 p = (union dlm_packet *)((unsigned char *)p->opts.o_opts +
787 le16_to_cpu(p->opts.o_optlen));
788
789 /* recheck inner msglen just if it's not garbage */
790 msglen = le16_to_cpu(p->header.h_length);
791 switch (p->header.h_cmd) {
792 case DLM_RCOM:
793 if (msglen < sizeof(struct dlm_rcom)) {
794 log_print("inner rcom msg too small: %u, will skip this message from node %d",
795 msglen, nodeid);
796 goto out;
797 }
798
799 break;
800 case DLM_MSG:
801 if (msglen < sizeof(struct dlm_message)) {
802 log_print("inner msg too small: %u, will skip this message from node %d",
803 msglen, nodeid);
804 goto out;
805 }
806
807 break;
808 case DLM_FIN:
809 if (msglen < sizeof(struct dlm_header)) {
810 log_print("inner fin too small: %u, will skip this message from node %d",
811 msglen, nodeid);
812 goto out;
813 }
814
815 break;
816 default:
817 log_print("unsupported inner h_cmd received: %u, will skip this message from node %d",
818 msglen, nodeid);
819 goto out;
820 }
821
822 dlm_midcomms_receive_buffer(p, node, seq);
823 break;
824 case DLM_ACK:
825 seq = le32_to_cpu(p->header.u.h_seq);
826 dlm_receive_ack(node, seq);
827 break;
828 default:
829 log_print("unsupported h_cmd received: %u, will skip this message from node %d",
830 p->header.h_cmd, nodeid);
831 break;
832 }
833
834out:
835 srcu_read_unlock(&nodes_srcu, idx);
836}
837
838static int dlm_midcomms_version_check_3_1(struct midcomms_node *node)
a070a91c 839{
489d8e55
AA
840 switch (node->version) {
841 case DLM_VERSION_NOT_SET:
842 node->version = DLM_VERSION_3_1;
843 log_print("version 0x%08x for node %d detected", DLM_VERSION_3_1,
844 node->nodeid);
845 break;
846 case DLM_VERSION_3_1:
847 break;
848 default:
849 log_print_ratelimited("version mismatch detected, assumed 0x%08x but node %d has 0x%08x",
850 DLM_VERSION_3_1, node->nodeid, node->version);
851 return -1;
852 }
853
854 return 0;
855}
856
857static void dlm_midcomms_receive_buffer_3_1(union dlm_packet *p, int nodeid)
858{
859 uint16_t msglen = le16_to_cpu(p->header.h_length);
860 struct midcomms_node *node;
861 int idx;
862
863 idx = srcu_read_lock(&nodes_srcu);
864 node = dlm_midcomms_recv_node_lookup(nodeid, p, msglen,
865 dlm_midcomms_version_check_3_1);
866 if (!node) {
867 srcu_read_unlock(&nodes_srcu, idx);
868 return;
869 }
870 srcu_read_unlock(&nodes_srcu, idx);
871
872 switch (p->header.h_cmd) {
873 case DLM_RCOM:
874 /* length already checked */
875 break;
876 case DLM_MSG:
877 if (msglen < sizeof(struct dlm_message)) {
878 log_print("msg too small: %u, will skip this message from node %d",
879 msglen, nodeid);
880 return;
881 }
882
883 break;
884 default:
885 log_print("unsupported h_cmd received: %u, will skip this message from node %d",
886 p->header.h_cmd, nodeid);
887 return;
888 }
889
890 dlm_receive_buffer(p, nodeid);
a070a91c
AA
891}
892
e7fd4179
DT
893/*
894 * Called from the low-level comms layer to process a buffer of
895 * commands.
e7fd4179
DT
896 */
897
4798cbbf 898int dlm_process_incoming_buffer(int nodeid, unsigned char *buf, int len)
e7fd4179 899{
4798cbbf
AA
900 const unsigned char *ptr = buf;
901 const struct dlm_header *hd;
e7fd4179 902 uint16_t msglen;
4798cbbf 903 int ret = 0;
e7fd4179 904
4798cbbf
AA
905 while (len >= sizeof(struct dlm_header)) {
906 hd = (struct dlm_header *)ptr;
907
d10a0b88 908 /* no message should be more than DLM_MAX_SOCKET_BUFSIZE or
df9e06b8
AA
909 * less than dlm_header size.
910 *
911 * Some messages does not have a 8 byte length boundary yet
912 * which can occur in a unaligned memory access of some dlm
913 * messages. However this problem need to be fixed at the
914 * sending side, for now it seems nobody run into architecture
915 * related issues yet but it slows down some processing.
916 * Fixing this issue should be scheduled in future by doing
917 * the next major version bump.
4798cbbf 918 */
df9e06b8 919 msglen = le16_to_cpu(hd->h_length);
d10a0b88 920 if (msglen > DLM_MAX_SOCKET_BUFSIZE ||
710176e8
AA
921 msglen < sizeof(struct dlm_header)) {
922 log_print("received invalid length header: %u from node %d, will abort message parsing",
923 msglen, nodeid);
4798cbbf 924 return -EBADMSG;
e7fd4179 925 }
e7fd4179 926
4798cbbf
AA
927 /* caller will take care that leftover
928 * will be parsed next call with more data
929 */
e7fd4179
DT
930 if (msglen > len)
931 break;
932
658bd576
AA
933 switch (hd->h_version) {
934 case cpu_to_le32(DLM_VERSION_3_1):
489d8e55 935 dlm_midcomms_receive_buffer_3_1((union dlm_packet *)ptr, nodeid);
4798cbbf 936 break;
658bd576 937 case cpu_to_le32(DLM_VERSION_3_2):
489d8e55 938 dlm_midcomms_receive_buffer_3_2((union dlm_packet *)ptr, nodeid);
4798cbbf
AA
939 break;
940 default:
489d8e55
AA
941 log_print("received invalid version header: %u from node %d, will skip this message",
942 le32_to_cpu(hd->h_version), nodeid);
943 break;
4798cbbf 944 }
e7fd4179
DT
945
946 ret += msglen;
e7fd4179 947 len -= msglen;
4798cbbf 948 ptr += msglen;
e7fd4179
DT
949 }
950
4798cbbf 951 return ret;
e7fd4179 952}
489d8e55 953
b97f8525
AA
954void dlm_midcomms_receive_done(int nodeid)
955{
956 struct midcomms_node *node;
957 int idx;
958
959 idx = srcu_read_lock(&nodes_srcu);
960 node = nodeid2node(nodeid, 0);
961 if (!node) {
962 srcu_read_unlock(&nodes_srcu, idx);
963 return;
964 }
965
966 /* old protocol, we do nothing */
967 switch (node->version) {
968 case DLM_VERSION_3_2:
969 break;
970 default:
971 srcu_read_unlock(&nodes_srcu, idx);
972 return;
973 }
974
975 /* do nothing if we didn't delivered stateful to ulp */
976 if (!test_and_clear_bit(DLM_NODE_ULP_DELIVERED,
977 &node->flags)) {
978 srcu_read_unlock(&nodes_srcu, idx);
979 return;
980 }
981
982 spin_lock(&node->state_lock);
983 /* we only ack if state is ESTABLISHED */
984 switch (node->state) {
985 case DLM_ESTABLISHED:
986 spin_unlock(&node->state_lock);
987 dlm_send_ack(node->nodeid, node->seq_next);
988 break;
989 default:
990 spin_unlock(&node->state_lock);
991 /* do nothing FIN has it's own ack send */
992 break;
c8b9f34e 993 }
b97f8525
AA
994 srcu_read_unlock(&nodes_srcu, idx);
995}
996
489d8e55
AA
997void dlm_midcomms_unack_msg_resend(int nodeid)
998{
999 struct midcomms_node *node;
1000 struct dlm_mhandle *mh;
1001 int idx, ret;
1002
1003 idx = srcu_read_lock(&nodes_srcu);
1004 node = nodeid2node(nodeid, 0);
1005 if (!node) {
1006 srcu_read_unlock(&nodes_srcu, idx);
1007 return;
1008 }
1009
1010 /* old protocol, we don't support to retransmit on failure */
1011 switch (node->version) {
1012 case DLM_VERSION_3_2:
1013 break;
1014 default:
1015 srcu_read_unlock(&nodes_srcu, idx);
1016 return;
1017 }
1018
1019 rcu_read_lock();
1020 list_for_each_entry_rcu(mh, &node->send_queue, list) {
1021 if (!mh->committed)
1022 continue;
1023
1024 ret = dlm_lowcomms_resend_msg(mh->msg);
1025 if (!ret)
1026 log_print_ratelimited("retransmit dlm msg, seq %u, nodeid %d",
1027 mh->seq, node->nodeid);
1028 }
1029 rcu_read_unlock();
1030 srcu_read_unlock(&nodes_srcu, idx);
1031}
1032
1033static void dlm_fill_opts_header(struct dlm_opts *opts, uint16_t inner_len,
1034 uint32_t seq)
1035{
1036 opts->o_header.h_cmd = DLM_OPTS;
3428785a
AA
1037 opts->o_header.h_version = cpu_to_le32(DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
1038 opts->o_header.h_nodeid = cpu_to_le32(dlm_our_nodeid());
1039 opts->o_header.h_length = cpu_to_le16(DLM_MIDCOMMS_OPT_LEN + inner_len);
1040 opts->o_header.u.h_seq = cpu_to_le32(seq);
489d8e55
AA
1041}
1042
5c16febb 1043static void midcomms_new_msg_cb(void *data)
489d8e55 1044{
5c16febb
AA
1045 struct dlm_mhandle *mh = data;
1046
489d8e55
AA
1047 atomic_inc(&mh->node->send_queue_cnt);
1048
1049 spin_lock(&mh->node->send_queue_lock);
1050 list_add_tail_rcu(&mh->list, &mh->node->send_queue);
1051 spin_unlock(&mh->node->send_queue_lock);
1052
1053 mh->seq = mh->node->seq_send++;
1054}
1055
1056static struct dlm_msg *dlm_midcomms_get_msg_3_2(struct dlm_mhandle *mh, int nodeid,
1057 int len, gfp_t allocation, char **ppc)
1058{
1059 struct dlm_opts *opts;
1060 struct dlm_msg *msg;
1061
1062 msg = dlm_lowcomms_new_msg(nodeid, len + DLM_MIDCOMMS_OPT_LEN,
1063 allocation, ppc, midcomms_new_msg_cb, mh);
1064 if (!msg)
1065 return NULL;
1066
1067 opts = (struct dlm_opts *)*ppc;
1068 mh->opts = opts;
1069
1070 /* add possible options here */
1071 dlm_fill_opts_header(opts, len, mh->seq);
1072
1073 *ppc += sizeof(*opts);
5b787667 1074 mh->inner_p = (const union dlm_packet *)*ppc;
489d8e55
AA
1075 return msg;
1076}
1077
a8449f23
AA
1078/* avoid false positive for nodes_srcu, unlock happens in
1079 * dlm_midcomms_commit_mhandle which is a must call if success
1080 */
1081#ifndef __CHECKER__
489d8e55
AA
1082struct dlm_mhandle *dlm_midcomms_get_mhandle(int nodeid, int len,
1083 gfp_t allocation, char **ppc)
1084{
1085 struct midcomms_node *node;
1086 struct dlm_mhandle *mh;
1087 struct dlm_msg *msg;
1088 int idx;
1089
1090 idx = srcu_read_lock(&nodes_srcu);
1091 node = nodeid2node(nodeid, 0);
1092 if (!node) {
1093 WARN_ON_ONCE(1);
1094 goto err;
1095 }
1096
1097 /* this is a bug, however we going on and hope it will be resolved */
775af207 1098 WARN_ON_ONCE(test_bit(DLM_NODE_FLAG_STOP_TX, &node->flags));
489d8e55 1099
e1711fe3 1100 mh = dlm_allocate_mhandle(allocation);
489d8e55
AA
1101 if (!mh)
1102 goto err;
1103
6c547f26
AA
1104 mh->committed = false;
1105 mh->ack_rcv = NULL;
489d8e55
AA
1106 mh->idx = idx;
1107 mh->node = node;
1108
1109 switch (node->version) {
1110 case DLM_VERSION_3_1:
1111 msg = dlm_lowcomms_new_msg(nodeid, len, allocation, ppc,
1112 NULL, NULL);
1113 if (!msg) {
6c547f26 1114 dlm_free_mhandle(mh);
489d8e55
AA
1115 goto err;
1116 }
1117
1118 break;
1119 case DLM_VERSION_3_2:
1120 msg = dlm_midcomms_get_msg_3_2(mh, nodeid, len, allocation,
1121 ppc);
1122 if (!msg) {
6c547f26 1123 dlm_free_mhandle(mh);
489d8e55
AA
1124 goto err;
1125 }
1126
1127 break;
1128 default:
6c547f26 1129 dlm_free_mhandle(mh);
775af207 1130 WARN_ON_ONCE(1);
489d8e55
AA
1131 goto err;
1132 }
1133
1134 mh->msg = msg;
1135
1136 /* keep in mind that is a must to call
1137 * dlm_midcomms_commit_msg() which releases
1138 * nodes_srcu using mh->idx which is assumed
1139 * here that the application will call it.
1140 */
1141 return mh;
1142
1143err:
1144 srcu_read_unlock(&nodes_srcu, idx);
1145 return NULL;
1146}
a8449f23 1147#endif
489d8e55 1148
e01c4b7b
AA
1149static void dlm_midcomms_commit_msg_3_2_trace(const struct dlm_mhandle *mh,
1150 const void *name, int namelen)
1151{
1152 switch (mh->inner_p->header.h_cmd) {
1153 case DLM_MSG:
17827754
AA
1154 trace_dlm_send_message(mh->node->nodeid, mh->seq,
1155 &mh->inner_p->message,
e01c4b7b
AA
1156 name, namelen);
1157 break;
1158 case DLM_RCOM:
17827754
AA
1159 trace_dlm_send_rcom(mh->node->nodeid, mh->seq,
1160 &mh->inner_p->rcom);
e01c4b7b
AA
1161 break;
1162 default:
1163 /* nothing to trace */
1164 break;
1165 }
1166}
1167
1168static void dlm_midcomms_commit_msg_3_2(struct dlm_mhandle *mh,
1169 const void *name, int namelen)
489d8e55
AA
1170{
1171 /* nexthdr chain for fast lookup */
5b787667 1172 mh->opts->o_nextcmd = mh->inner_p->header.h_cmd;
489d8e55 1173 mh->committed = true;
e01c4b7b 1174 dlm_midcomms_commit_msg_3_2_trace(mh, name, namelen);
489d8e55
AA
1175 dlm_lowcomms_commit_msg(mh->msg);
1176}
1177
a8449f23
AA
1178/* avoid false positive for nodes_srcu, lock was happen in
1179 * dlm_midcomms_get_mhandle
1180 */
1181#ifndef __CHECKER__
e01c4b7b
AA
1182void dlm_midcomms_commit_mhandle(struct dlm_mhandle *mh,
1183 const void *name, int namelen)
489d8e55 1184{
e01c4b7b 1185
489d8e55
AA
1186 switch (mh->node->version) {
1187 case DLM_VERSION_3_1:
1188 srcu_read_unlock(&nodes_srcu, mh->idx);
1189
1190 dlm_lowcomms_commit_msg(mh->msg);
1191 dlm_lowcomms_put_msg(mh->msg);
1192 /* mh is not part of rcu list in this case */
6c547f26 1193 dlm_free_mhandle(mh);
489d8e55
AA
1194 break;
1195 case DLM_VERSION_3_2:
e01c4b7b 1196 dlm_midcomms_commit_msg_3_2(mh, name, namelen);
489d8e55
AA
1197 srcu_read_unlock(&nodes_srcu, mh->idx);
1198 break;
1199 default:
1200 srcu_read_unlock(&nodes_srcu, mh->idx);
775af207 1201 WARN_ON_ONCE(1);
489d8e55
AA
1202 break;
1203 }
1204}
a8449f23 1205#endif
489d8e55
AA
1206
1207int dlm_midcomms_start(void)
1208{
1209 int i;
1210
1211 for (i = 0; i < CONN_HASH_SIZE; i++)
1212 INIT_HLIST_HEAD(&node_hash[i]);
1213
1214 return dlm_lowcomms_start();
1215}
1216
1217static void dlm_act_fin_ack_rcv(struct midcomms_node *node)
1218{
1219 spin_lock(&node->state_lock);
1220 pr_debug("receive active fin ack from node %d with state %s\n",
1221 node->nodeid, dlm_state_str(node->state));
1222
1223 switch (node->state) {
1224 case DLM_FIN_WAIT1:
1225 node->state = DLM_FIN_WAIT2;
1226 pr_debug("switch node %d to state %s\n",
1227 node->nodeid, dlm_state_str(node->state));
1228 break;
1229 case DLM_CLOSING:
1230 midcomms_node_reset(node);
1231 pr_debug("switch node %d to state %s\n",
1232 node->nodeid, dlm_state_str(node->state));
1233 wake_up(&node->shutdown_wait);
1234 break;
1235 case DLM_CLOSED:
1236 /* not valid but somehow we got what we want */
1237 wake_up(&node->shutdown_wait);
1238 break;
1239 default:
1240 spin_unlock(&node->state_lock);
1241 log_print("%s: unexpected state: %d\n",
1242 __func__, node->state);
775af207 1243 WARN_ON_ONCE(1);
489d8e55
AA
1244 return;
1245 }
1246 spin_unlock(&node->state_lock);
1247}
1248
1249void dlm_midcomms_add_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, GFP_NOFS);
1259 if (!node) {
1260 srcu_read_unlock(&nodes_srcu, idx);
1261 return;
1262 }
1263
1264 spin_lock(&node->state_lock);
1265 if (!node->users) {
1266 pr_debug("receive add member from node %d with state %s\n",
1267 node->nodeid, dlm_state_str(node->state));
1268 switch (node->state) {
1269 case DLM_ESTABLISHED:
1270 break;
1271 case DLM_CLOSED:
1272 node->state = DLM_ESTABLISHED;
1273 pr_debug("switch node %d to state %s\n",
1274 node->nodeid, dlm_state_str(node->state));
1275 break;
1276 default:
1277 /* some invalid state passive shutdown
1278 * was failed, we try to reset and
1279 * hope it will go on.
1280 */
7d3848c0 1281 log_print("reset node %d because shutdown stuck",
489d8e55
AA
1282 node->nodeid);
1283
1284 midcomms_node_reset(node);
1285 node->state = DLM_ESTABLISHED;
1286 break;
1287 }
1288 }
1289
1290 node->users++;
1aafd9c2 1291 pr_debug("node %d users inc count %d\n", nodeid, node->users);
489d8e55
AA
1292 spin_unlock(&node->state_lock);
1293
1294 srcu_read_unlock(&nodes_srcu, idx);
1295}
1296
1297void dlm_midcomms_remove_member(int nodeid)
1298{
1299 struct midcomms_node *node;
1300 int idx;
1301
1302 if (nodeid == dlm_our_nodeid())
1303 return;
1304
1305 idx = srcu_read_lock(&nodes_srcu);
1306 node = nodeid2node(nodeid, 0);
1307 if (!node) {
1308 srcu_read_unlock(&nodes_srcu, idx);
1309 return;
1310 }
1311
1312 spin_lock(&node->state_lock);
1313 node->users--;
1aafd9c2 1314 pr_debug("node %d users dec count %d\n", nodeid, node->users);
489d8e55
AA
1315
1316 /* hitting users count to zero means the
1317 * other side is running dlm_midcomms_stop()
1318 * we meet us to have a clean disconnect.
1319 */
1320 if (node->users == 0) {
1321 pr_debug("receive remove member from node %d with state %s\n",
1322 node->nodeid, dlm_state_str(node->state));
1323 switch (node->state) {
1324 case DLM_ESTABLISHED:
1325 break;
1326 case DLM_CLOSE_WAIT:
1327 /* passive shutdown DLM_LAST_ACK case 2 */
1328 node->state = DLM_LAST_ACK;
1329 spin_unlock(&node->state_lock);
1330
1331 pr_debug("switch node %d to state %s case 2\n",
1332 node->nodeid, dlm_state_str(node->state));
1333 goto send_fin;
1334 case DLM_LAST_ACK:
1335 /* probably receive fin caught it, do nothing */
1336 break;
1337 case DLM_CLOSED:
1338 /* already gone, do nothing */
1339 break;
1340 default:
1341 log_print("%s: unexpected state: %d\n",
1342 __func__, node->state);
1343 break;
1344 }
1345 }
1346 spin_unlock(&node->state_lock);
1347
1348 srcu_read_unlock(&nodes_srcu, idx);
1349 return;
1350
1351send_fin:
1352 set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
1353 dlm_send_fin(node, dlm_pas_fin_ack_rcv);
1354 srcu_read_unlock(&nodes_srcu, idx);
1355}
1356
1357static void midcomms_node_release(struct rcu_head *rcu)
1358{
1359 struct midcomms_node *node = container_of(rcu, struct midcomms_node, rcu);
1360
775af207 1361 WARN_ON_ONCE(atomic_read(&node->send_queue_cnt));
489d8e55
AA
1362 kfree(node);
1363}
1364
1365static void midcomms_shutdown(struct midcomms_node *node)
1366{
1367 int ret;
1368
1369 /* old protocol, we don't wait for pending operations */
1370 switch (node->version) {
1371 case DLM_VERSION_3_2:
1372 break;
1373 default:
1374 return;
1375 }
1376
1377 spin_lock(&node->state_lock);
1378 pr_debug("receive active shutdown for node %d with state %s\n",
1379 node->nodeid, dlm_state_str(node->state));
1380 switch (node->state) {
1381 case DLM_ESTABLISHED:
1382 node->state = DLM_FIN_WAIT1;
1383 pr_debug("switch node %d to state %s case 2\n",
1384 node->nodeid, dlm_state_str(node->state));
1385 break;
1386 case DLM_CLOSED:
1387 /* we have what we want */
1388 spin_unlock(&node->state_lock);
1389 return;
1390 default:
1391 /* busy to enter DLM_FIN_WAIT1, wait until passive
1392 * done in shutdown_wait to enter DLM_CLOSED.
1393 */
1394 break;
1395 }
1396 spin_unlock(&node->state_lock);
1397
1398 if (node->state == DLM_FIN_WAIT1) {
1399 dlm_send_fin(node, dlm_act_fin_ack_rcv);
1400
1401 if (DLM_DEBUG_FENCE_TERMINATION)
1402 msleep(5000);
1403 }
1404
1405 /* wait for other side dlm + fin */
1406 ret = wait_event_timeout(node->shutdown_wait,
1407 node->state == DLM_CLOSED ||
1408 test_bit(DLM_NODE_FLAG_CLOSE, &node->flags),
1409 DLM_SHUTDOWN_TIMEOUT);
1410 if (!ret || test_bit(DLM_NODE_FLAG_CLOSE, &node->flags)) {
1411 pr_debug("active shutdown timed out for node %d with state %s\n",
1412 node->nodeid, dlm_state_str(node->state));
1413 midcomms_node_reset(node);
1414 return;
1415 }
1416
1417 pr_debug("active shutdown done for node %d with state %s\n",
1418 node->nodeid, dlm_state_str(node->state));
1419}
1420
1421void dlm_midcomms_shutdown(void)
1422{
1423 struct midcomms_node *node;
1424 int i, idx;
1425
1426 mutex_lock(&close_lock);
1427 idx = srcu_read_lock(&nodes_srcu);
1428 for (i = 0; i < CONN_HASH_SIZE; i++) {
1429 hlist_for_each_entry_rcu(node, &node_hash[i], hlist) {
1430 midcomms_shutdown(node);
1431
5b2f981f
AA
1432 dlm_delete_debug_comms_file(node->debugfs);
1433
489d8e55
AA
1434 spin_lock(&nodes_lock);
1435 hlist_del_rcu(&node->hlist);
1436 spin_unlock(&nodes_lock);
1437
1438 call_srcu(&nodes_srcu, &node->rcu, midcomms_node_release);
1439 }
1440 }
1441 srcu_read_unlock(&nodes_srcu, idx);
1442 mutex_unlock(&close_lock);
1443
1444 dlm_lowcomms_shutdown();
1445}
1446
1447int dlm_midcomms_close(int nodeid)
1448{
1449 struct midcomms_node *node;
1450 int idx, ret;
1451
1452 if (nodeid == dlm_our_nodeid())
1453 return 0;
1454
2c3fa6ae
AA
1455 dlm_stop_lockspaces_check();
1456
489d8e55
AA
1457 idx = srcu_read_lock(&nodes_srcu);
1458 /* Abort pending close/remove operation */
1459 node = nodeid2node(nodeid, 0);
1460 if (node) {
1461 /* let shutdown waiters leave */
1462 set_bit(DLM_NODE_FLAG_CLOSE, &node->flags);
1463 wake_up(&node->shutdown_wait);
1464 }
1465 srcu_read_unlock(&nodes_srcu, idx);
1466
1467 synchronize_srcu(&nodes_srcu);
1468
1469 idx = srcu_read_lock(&nodes_srcu);
1470 mutex_lock(&close_lock);
1471 node = nodeid2node(nodeid, 0);
1472 if (!node) {
1473 mutex_unlock(&close_lock);
1474 srcu_read_unlock(&nodes_srcu, idx);
1475 return dlm_lowcomms_close(nodeid);
1476 }
1477
1478 ret = dlm_lowcomms_close(nodeid);
1479 spin_lock(&node->state_lock);
1480 midcomms_node_reset(node);
1481 spin_unlock(&node->state_lock);
1482 srcu_read_unlock(&nodes_srcu, idx);
1483 mutex_unlock(&close_lock);
1484
1485 return ret;
1486}
9af5b8f0
AA
1487
1488/* debug functionality to send raw dlm msg from user space */
1489struct dlm_rawmsg_data {
1490 struct midcomms_node *node;
1491 void *buf;
1492};
1493
1494static void midcomms_new_rawmsg_cb(void *data)
1495{
1496 struct dlm_rawmsg_data *rd = data;
1497 struct dlm_header *h = rd->buf;
1498
1499 switch (h->h_version) {
1500 case cpu_to_le32(DLM_VERSION_3_1):
1501 break;
1502 default:
1503 switch (h->h_cmd) {
1504 case DLM_OPTS:
1505 if (!h->u.h_seq)
3428785a 1506 h->u.h_seq = cpu_to_le32(rd->node->seq_send++);
9af5b8f0
AA
1507 break;
1508 default:
1509 break;
1510 }
1511 break;
1512 }
1513}
1514
1515int dlm_midcomms_rawmsg_send(struct midcomms_node *node, void *buf,
1516 int buflen)
1517{
1518 struct dlm_rawmsg_data rd;
1519 struct dlm_msg *msg;
1520 char *msgbuf;
1521
1522 rd.node = node;
1523 rd.buf = buf;
1524
1525 msg = dlm_lowcomms_new_msg(node->nodeid, buflen, GFP_NOFS,
1526 &msgbuf, midcomms_new_rawmsg_cb, &rd);
1527 if (!msg)
1528 return -ENOMEM;
1529
1530 memcpy(msgbuf, buf, buflen);
1531 dlm_lowcomms_commit_msg(msg);
1532 return 0;
1533}
1534