GNU Linux-libre 6.1.86-gnu
[releases.git] / fs / dlm / midcomms.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /******************************************************************************
3 *******************************************************************************
4 **
5 **  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
6 **  Copyright (C) 2004-2021 Red Hat, Inc.  All rights reserved.
7 **
8 **
9 *******************************************************************************
10 ******************************************************************************/
11
12 /*
13  * midcomms.c
14  *
15  * This is the appallingly named "mid-level" comms layer. It takes care about
16  * deliver an on application layer "reliable" communication above the used
17  * lowcomms transport layer.
18  *
19  * How it works:
20  *
21  * Each nodes keeps track of all send DLM messages in send_queue with a sequence
22  * number. The receive will send an DLM_ACK message back for every DLM message
23  * received at the other side. If a reconnect happens in lowcomms we will send
24  * all unacknowledged dlm messages again. The receiving side might drop any already
25  * received message by comparing sequence numbers.
26  *
27  * How version detection works:
28  *
29  * Due the fact that dlm has pre-configured node addresses on every side
30  * it is in it's nature that every side connects at starts to transmit
31  * dlm messages which ends in a race. However DLM_RCOM_NAMES, DLM_RCOM_STATUS
32  * and their replies are the first messages which are exchanges. Due backwards
33  * compatibility these messages are not covered by the midcomms re-transmission
34  * layer. These messages have their own re-transmission handling in the dlm
35  * application layer. The version field of every node will be set on these RCOM
36  * messages as soon as they arrived and the node isn't yet part of the nodes
37  * hash. There exists also logic to detect version mismatched if something weird
38  * going on or the first messages isn't an expected one.
39  *
40  * Termination:
41  *
42  * The midcomms layer does a 4 way handshake for termination on DLM protocol
43  * like TCP supports it with half-closed socket support. SCTP doesn't support
44  * half-closed socket, so we do it on DLM layer. Also socket shutdown() can be
45  * interrupted by .e.g. tcp reset itself. Additional there exists the othercon
46  * paradigm in lowcomms which cannot be easily without breaking backwards
47  * compatibility. A node cannot send anything to another node when a DLM_FIN
48  * message was send. There exists additional logic to print a warning if
49  * DLM wants to do it. There exists a state handling like RFC 793 but reduced
50  * to termination only. The event "member removal event" describes the cluster
51  * manager removed the node from internal lists, at this point DLM does not
52  * send any message to the other node. There exists two cases:
53  *
54  * 1. The cluster member was removed and we received a FIN
55  * OR
56  * 2. We received a FIN but the member was not removed yet
57  *
58  * One of these cases will do the CLOSE_WAIT to LAST_ACK change.
59  *
60  *
61  *                              +---------+
62  *                              | CLOSED  |
63  *                              +---------+
64  *                                   | add member/receive RCOM version
65  *                                   |            detection msg
66  *                                   V
67  *                              +---------+
68  *                              |  ESTAB  |
69  *                              +---------+
70  *                       CLOSE    |     |    rcv FIN
71  *                      -------   |     |    -------
72  * +---------+          snd FIN  /       \   snd ACK          +---------+
73  * |  FIN    |<-----------------           ------------------>|  CLOSE  |
74  * | WAIT-1  |------------------                              |   WAIT  |
75  * +---------+          rcv FIN  \                            +---------+
76  * | rcv ACK of FIN   -------   |                            CLOSE  | member
77  * | --------------   snd ACK   |                           ------- | removal
78  * V        x                   V                           snd FIN V event
79  * +---------+                  +---------+                   +---------+
80  * |FINWAIT-2|                  | CLOSING |                   | LAST-ACK|
81  * +---------+                  +---------+                   +---------+
82  * |                rcv ACK of FIN |                 rcv ACK of FIN |
83  * |  rcv FIN       -------------- |                 -------------- |
84  * |  -------              x       V                        x       V
85  *  \ snd ACK                 +---------+                   +---------+
86  *   ------------------------>| CLOSED  |                   | CLOSED  |
87  *                            +---------+                   +---------+
88  *
89  * NOTE: any state can interrupted by midcomms_close() and state will be
90  * switched to CLOSED in case of fencing. There exists also some timeout
91  * handling when we receive the version detection RCOM messages which is
92  * made by observation.
93  *
94  * Future improvements:
95  *
96  * There exists some known issues/improvements of the dlm handling. Some
97  * of them should be done in a next major dlm version bump which makes
98  * it incompatible with previous versions.
99  *
100  * Unaligned memory access:
101  *
102  * There exists cases when the dlm message buffer length is not aligned
103  * to 8 byte. However seems nobody detected any problem with it. This
104  * can be fixed in the next major version bump of dlm.
105  *
106  * Version detection:
107  *
108  * The version detection and how it's done is related to backwards
109  * compatibility. There exists better ways to make a better handling.
110  * However this should be changed in the next major version bump of dlm.
111  *
112  * Tail Size checking:
113  *
114  * There exists a message tail payload in e.g. DLM_MSG however we don't
115  * check it against the message length yet regarding to the receive buffer
116  * length. That need to be validated.
117  *
118  * Fencing bad nodes:
119  *
120  * At timeout places or weird sequence number behaviours we should send
121  * a fencing request to the cluster manager.
122  */
123
124 /* Debug switch to enable a 5 seconds sleep waiting of a termination.
125  * This can be useful to test fencing while termination is running.
126  * This requires a setup with only gfs2 as dlm user, so that the
127  * last umount will terminate the connection.
128  *
129  * However it became useful to test, while the 5 seconds block in umount
130  * just press the reset button. In a lot of dropping the termination
131  * process can could take several seconds.
132  */
133 #define DLM_DEBUG_FENCE_TERMINATION     0
134
135 #include <trace/events/dlm.h>
136 #include <net/tcp.h>
137
138 #include "dlm_internal.h"
139 #include "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 union dlm_packet *inner_p;
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_ATOMIC, &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_ATOMIC, &ppc);
405         if (!mh)
406                 return -ENOMEM;
407
408         set_bit(DLM_NODE_FLAG_STOP_TX, &node->flags);
409         mh->ack_rcv = ack_rcv;
410
411         m_header = (struct dlm_header *)ppc;
412
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);
416         m_header->h_cmd = DLM_FIN;
417
418         pr_debug("sending fin msg to node %d\n", node->nodeid);
419         dlm_midcomms_commit_mhandle(mh, NULL, 0);
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_ONCE(1);
472                 return;
473         }
474         spin_unlock(&node->state_lock);
475 }
476
477 static void dlm_receive_buffer_3_2_trace(uint32_t seq, union dlm_packet *p)
478 {
479         switch (p->header.h_cmd) {
480         case DLM_MSG:
481                 trace_dlm_recv_message(seq, &p->message);
482                 break;
483         case DLM_RCOM:
484                 trace_dlm_recv_rcom(seq, &p->rcom);
485                 break;
486         default:
487                 break;
488         }
489 }
490
491 static void dlm_midcomms_receive_buffer(union dlm_packet *p,
492                                         struct midcomms_node *node,
493                                         uint32_t seq)
494 {
495         if (seq == node->seq_next) {
496                 node->seq_next++;
497
498                 switch (p->header.h_cmd) {
499                 case DLM_FIN:
500                         spin_lock(&node->state_lock);
501                         pr_debug("receive fin msg from node %d with state %s\n",
502                                  node->nodeid, dlm_state_str(node->state));
503
504                         switch (node->state) {
505                         case DLM_ESTABLISHED:
506                                 dlm_send_ack(node->nodeid, node->seq_next);
507
508                                 node->state = DLM_CLOSE_WAIT;
509                                 pr_debug("switch node %d to state %s\n",
510                                          node->nodeid, dlm_state_str(node->state));
511                                 /* passive shutdown DLM_LAST_ACK case 1
512                                  * additional we check if the node is used by
513                                  * cluster manager events at all.
514                                  */
515                                 if (node->users == 0) {
516                                         node->state = DLM_LAST_ACK;
517                                         pr_debug("switch node %d to state %s case 1\n",
518                                                  node->nodeid, dlm_state_str(node->state));
519                                         set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
520                                         dlm_send_fin(node, dlm_pas_fin_ack_rcv);
521                                 }
522                                 break;
523                         case DLM_FIN_WAIT1:
524                                 dlm_send_ack(node->nodeid, node->seq_next);
525                                 node->state = DLM_CLOSING;
526                                 set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
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                                 dlm_send_ack(node->nodeid, node->seq_next);
532                                 midcomms_node_reset(node);
533                                 pr_debug("switch node %d to state %s\n",
534                                          node->nodeid, dlm_state_str(node->state));
535                                 wake_up(&node->shutdown_wait);
536                                 break;
537                         case DLM_LAST_ACK:
538                                 /* probably remove_member caught it, do nothing */
539                                 break;
540                         default:
541                                 spin_unlock(&node->state_lock);
542                                 log_print("%s: unexpected state: %d\n",
543                                           __func__, node->state);
544                                 WARN_ON_ONCE(1);
545                                 return;
546                         }
547                         spin_unlock(&node->state_lock);
548                         break;
549                 default:
550                         WARN_ON_ONCE(test_bit(DLM_NODE_FLAG_STOP_RX, &node->flags));
551                         dlm_receive_buffer_3_2_trace(seq, p);
552                         dlm_receive_buffer(p, node->nodeid);
553                         set_bit(DLM_NODE_ULP_DELIVERED, &node->flags);
554                         break;
555                 }
556         } else {
557                 /* retry to ack message which we already have by sending back
558                  * current node->seq_next number as ack.
559                  */
560                 if (seq < node->seq_next)
561                         dlm_send_ack(node->nodeid, node->seq_next);
562
563                 log_print_ratelimited("ignore dlm msg because seq mismatch, seq: %u, expected: %u, nodeid: %d",
564                                       seq, node->seq_next, node->nodeid);
565         }
566 }
567
568 static struct midcomms_node *
569 dlm_midcomms_recv_node_lookup(int nodeid, const union dlm_packet *p,
570                               uint16_t msglen, int (*cb)(struct midcomms_node *node))
571 {
572         struct midcomms_node *node = NULL;
573         gfp_t allocation = 0;
574         int ret;
575
576         switch (p->header.h_cmd) {
577         case DLM_RCOM:
578                 if (msglen < sizeof(struct dlm_rcom)) {
579                         log_print("rcom msg too small: %u, will skip this message from node %d",
580                                   msglen, nodeid);
581                         return NULL;
582                 }
583
584                 switch (p->rcom.rc_type) {
585                 case cpu_to_le32(DLM_RCOM_NAMES):
586                         fallthrough;
587                 case cpu_to_le32(DLM_RCOM_NAMES_REPLY):
588                         fallthrough;
589                 case cpu_to_le32(DLM_RCOM_STATUS):
590                         fallthrough;
591                 case cpu_to_le32(DLM_RCOM_STATUS_REPLY):
592                         node = nodeid2node(nodeid, 0);
593                         if (node) {
594                                 spin_lock(&node->state_lock);
595                                 if (node->state != DLM_ESTABLISHED)
596                                         pr_debug("receive begin RCOM msg from node %d with state %s\n",
597                                                  node->nodeid, dlm_state_str(node->state));
598
599                                 switch (node->state) {
600                                 case DLM_CLOSED:
601                                         node->state = DLM_ESTABLISHED;
602                                         pr_debug("switch node %d to state %s\n",
603                                                  node->nodeid, dlm_state_str(node->state));
604                                         break;
605                                 case DLM_ESTABLISHED:
606                                         break;
607                                 default:
608                                         /* some invalid state passive shutdown
609                                          * was failed, we try to reset and
610                                          * hope it will go on.
611                                          */
612                                         log_print("reset node %d because shutdown stuck",
613                                                   node->nodeid);
614
615                                         midcomms_node_reset(node);
616                                         node->state = DLM_ESTABLISHED;
617                                         break;
618                                 }
619                                 spin_unlock(&node->state_lock);
620                         }
621
622                         allocation = GFP_NOFS;
623                         break;
624                 default:
625                         break;
626                 }
627
628                 break;
629         default:
630                 break;
631         }
632
633         node = nodeid2node(nodeid, allocation);
634         if (!node) {
635                 switch (p->header.h_cmd) {
636                 case DLM_OPTS:
637                         if (msglen < sizeof(struct dlm_opts)) {
638                                 log_print("opts msg too small: %u, will skip this message from node %d",
639                                           msglen, nodeid);
640                                 return NULL;
641                         }
642
643                         log_print_ratelimited("received dlm opts message nextcmd %d from node %d in an invalid sequence",
644                                               p->opts.o_nextcmd, nodeid);
645                         break;
646                 default:
647                         log_print_ratelimited("received dlm message cmd %d from node %d in an invalid sequence",
648                                               p->header.h_cmd, nodeid);
649                         break;
650                 }
651
652                 return NULL;
653         }
654
655         ret = cb(node);
656         if (ret < 0)
657                 return NULL;
658
659         return node;
660 }
661
662 static int dlm_midcomms_version_check_3_2(struct midcomms_node *node)
663 {
664         switch (node->version) {
665         case DLM_VERSION_NOT_SET:
666                 node->version = DLM_VERSION_3_2;
667                 log_print("version 0x%08x for node %d detected", DLM_VERSION_3_2,
668                           node->nodeid);
669                 break;
670         case DLM_VERSION_3_2:
671                 break;
672         default:
673                 log_print_ratelimited("version mismatch detected, assumed 0x%08x but node %d has 0x%08x",
674                                       DLM_VERSION_3_2, node->nodeid, node->version);
675                 return -1;
676         }
677
678         return 0;
679 }
680
681 static int dlm_opts_check_msglen(union dlm_packet *p, uint16_t msglen, int nodeid)
682 {
683         int len = msglen;
684
685         /* we only trust outer header msglen because
686          * it's checked against receive buffer length.
687          */
688         if (len < sizeof(struct dlm_opts))
689                 return -1;
690         len -= sizeof(struct dlm_opts);
691
692         if (len < le16_to_cpu(p->opts.o_optlen))
693                 return -1;
694         len -= le16_to_cpu(p->opts.o_optlen);
695
696         switch (p->opts.o_nextcmd) {
697         case DLM_FIN:
698                 if (len < sizeof(struct dlm_header)) {
699                         log_print("fin too small: %d, will skip this message from node %d",
700                                   len, nodeid);
701                         return -1;
702                 }
703
704                 break;
705         case DLM_MSG:
706                 if (len < sizeof(struct dlm_message)) {
707                         log_print("msg too small: %d, will skip this message from node %d",
708                                   msglen, nodeid);
709                         return -1;
710                 }
711
712                 break;
713         case DLM_RCOM:
714                 if (len < sizeof(struct dlm_rcom)) {
715                         log_print("rcom msg too small: %d, will skip this message from node %d",
716                                   len, nodeid);
717                         return -1;
718                 }
719
720                 break;
721         default:
722                 log_print("unsupported o_nextcmd received: %u, will skip this message from node %d",
723                           p->opts.o_nextcmd, nodeid);
724                 return -1;
725         }
726
727         return 0;
728 }
729
730 static void dlm_midcomms_receive_buffer_3_2(union dlm_packet *p, int nodeid)
731 {
732         uint16_t msglen = le16_to_cpu(p->header.h_length);
733         struct midcomms_node *node;
734         uint32_t seq;
735         int ret, idx;
736
737         idx = srcu_read_lock(&nodes_srcu);
738         node = dlm_midcomms_recv_node_lookup(nodeid, p, msglen,
739                                              dlm_midcomms_version_check_3_2);
740         if (!node)
741                 goto out;
742
743         switch (p->header.h_cmd) {
744         case DLM_RCOM:
745                 /* these rcom message we use to determine version.
746                  * they have their own retransmission handling and
747                  * are the first messages of dlm.
748                  *
749                  * length already checked.
750                  */
751                 switch (p->rcom.rc_type) {
752                 case cpu_to_le32(DLM_RCOM_NAMES):
753                         fallthrough;
754                 case cpu_to_le32(DLM_RCOM_NAMES_REPLY):
755                         fallthrough;
756                 case cpu_to_le32(DLM_RCOM_STATUS):
757                         fallthrough;
758                 case cpu_to_le32(DLM_RCOM_STATUS_REPLY):
759                         break;
760                 default:
761                         log_print("unsupported rcom type received: %u, will skip this message from node %d",
762                                   le32_to_cpu(p->rcom.rc_type), nodeid);
763                         goto out;
764                 }
765
766                 WARN_ON_ONCE(test_bit(DLM_NODE_FLAG_STOP_RX, &node->flags));
767                 dlm_receive_buffer(p, nodeid);
768                 break;
769         case DLM_OPTS:
770                 seq = le32_to_cpu(p->header.u.h_seq);
771
772                 ret = dlm_opts_check_msglen(p, msglen, nodeid);
773                 if (ret < 0) {
774                         log_print("opts msg too small: %u, will skip this message from node %d",
775                                   msglen, nodeid);
776                         goto out;
777                 }
778
779                 p = (union dlm_packet *)((unsigned char *)p->opts.o_opts +
780                                          le16_to_cpu(p->opts.o_optlen));
781
782                 /* recheck inner msglen just if it's not garbage */
783                 msglen = le16_to_cpu(p->header.h_length);
784                 switch (p->header.h_cmd) {
785                 case DLM_RCOM:
786                         if (msglen < sizeof(struct dlm_rcom)) {
787                                 log_print("inner rcom msg too small: %u, will skip this message from node %d",
788                                           msglen, nodeid);
789                                 goto out;
790                         }
791
792                         break;
793                 case DLM_MSG:
794                         if (msglen < sizeof(struct dlm_message)) {
795                                 log_print("inner msg too small: %u, will skip this message from node %d",
796                                           msglen, nodeid);
797                                 goto out;
798                         }
799
800                         break;
801                 case DLM_FIN:
802                         if (msglen < sizeof(struct dlm_header)) {
803                                 log_print("inner fin too small: %u, will skip this message from node %d",
804                                           msglen, nodeid);
805                                 goto out;
806                         }
807
808                         break;
809                 default:
810                         log_print("unsupported inner h_cmd received: %u, will skip this message from node %d",
811                                   msglen, nodeid);
812                         goto out;
813                 }
814
815                 dlm_midcomms_receive_buffer(p, node, seq);
816                 break;
817         case DLM_ACK:
818                 seq = le32_to_cpu(p->header.u.h_seq);
819                 dlm_receive_ack(node, seq);
820                 break;
821         default:
822                 log_print("unsupported h_cmd received: %u, will skip this message from node %d",
823                           p->header.h_cmd, nodeid);
824                 break;
825         }
826
827 out:
828         srcu_read_unlock(&nodes_srcu, idx);
829 }
830
831 static int dlm_midcomms_version_check_3_1(struct midcomms_node *node)
832 {
833         switch (node->version) {
834         case DLM_VERSION_NOT_SET:
835                 node->version = DLM_VERSION_3_1;
836                 log_print("version 0x%08x for node %d detected", DLM_VERSION_3_1,
837                           node->nodeid);
838                 break;
839         case DLM_VERSION_3_1:
840                 break;
841         default:
842                 log_print_ratelimited("version mismatch detected, assumed 0x%08x but node %d has 0x%08x",
843                                       DLM_VERSION_3_1, node->nodeid, node->version);
844                 return -1;
845         }
846
847         return 0;
848 }
849
850 static void dlm_midcomms_receive_buffer_3_1(union dlm_packet *p, int nodeid)
851 {
852         uint16_t msglen = le16_to_cpu(p->header.h_length);
853         struct midcomms_node *node;
854         int idx;
855
856         idx = srcu_read_lock(&nodes_srcu);
857         node = dlm_midcomms_recv_node_lookup(nodeid, p, msglen,
858                                              dlm_midcomms_version_check_3_1);
859         if (!node) {
860                 srcu_read_unlock(&nodes_srcu, idx);
861                 return;
862         }
863         srcu_read_unlock(&nodes_srcu, idx);
864
865         switch (p->header.h_cmd) {
866         case DLM_RCOM:
867                 /* length already checked */
868                 break;
869         case DLM_MSG:
870                 if (msglen < sizeof(struct dlm_message)) {
871                         log_print("msg too small: %u, will skip this message from node %d",
872                                   msglen, nodeid);
873                         return;
874                 }
875
876                 break;
877         default:
878                 log_print("unsupported h_cmd received: %u, will skip this message from node %d",
879                           p->header.h_cmd, nodeid);
880                 return;
881         }
882
883         dlm_receive_buffer(p, nodeid);
884 }
885
886 /*
887  * Called from the low-level comms layer to process a buffer of
888  * commands.
889  */
890
891 int dlm_process_incoming_buffer(int nodeid, unsigned char *buf, int len)
892 {
893         const unsigned char *ptr = buf;
894         const struct dlm_header *hd;
895         uint16_t msglen;
896         int ret = 0;
897
898         while (len >= sizeof(struct dlm_header)) {
899                 hd = (struct dlm_header *)ptr;
900
901                 /* no message should be more than DLM_MAX_SOCKET_BUFSIZE or
902                  * less than dlm_header size.
903                  *
904                  * Some messages does not have a 8 byte length boundary yet
905                  * which can occur in a unaligned memory access of some dlm
906                  * messages. However this problem need to be fixed at the
907                  * sending side, for now it seems nobody run into architecture
908                  * related issues yet but it slows down some processing.
909                  * Fixing this issue should be scheduled in future by doing
910                  * the next major version bump.
911                  */
912                 msglen = le16_to_cpu(hd->h_length);
913                 if (msglen > DLM_MAX_SOCKET_BUFSIZE ||
914                     msglen < sizeof(struct dlm_header)) {
915                         log_print("received invalid length header: %u from node %d, will abort message parsing",
916                                   msglen, nodeid);
917                         return -EBADMSG;
918                 }
919
920                 /* caller will take care that leftover
921                  * will be parsed next call with more data
922                  */
923                 if (msglen > len)
924                         break;
925
926                 switch (hd->h_version) {
927                 case cpu_to_le32(DLM_VERSION_3_1):
928                         dlm_midcomms_receive_buffer_3_1((union dlm_packet *)ptr, nodeid);
929                         break;
930                 case cpu_to_le32(DLM_VERSION_3_2):
931                         dlm_midcomms_receive_buffer_3_2((union dlm_packet *)ptr, nodeid);
932                         break;
933                 default:
934                         log_print("received invalid version header: %u from node %d, will skip this message",
935                                   le32_to_cpu(hd->h_version), nodeid);
936                         break;
937                 }
938
939                 ret += msglen;
940                 len -= msglen;
941                 ptr += msglen;
942         }
943
944         return ret;
945 }
946
947 void dlm_midcomms_receive_done(int nodeid)
948 {
949         struct midcomms_node *node;
950         int idx;
951
952         idx = srcu_read_lock(&nodes_srcu);
953         node = nodeid2node(nodeid, 0);
954         if (!node) {
955                 srcu_read_unlock(&nodes_srcu, idx);
956                 return;
957         }
958
959         /* old protocol, we do nothing */
960         switch (node->version) {
961         case DLM_VERSION_3_2:
962                 break;
963         default:
964                 srcu_read_unlock(&nodes_srcu, idx);
965                 return;
966         }
967
968         /* do nothing if we didn't delivered stateful to ulp */
969         if (!test_and_clear_bit(DLM_NODE_ULP_DELIVERED,
970                                 &node->flags)) {
971                 srcu_read_unlock(&nodes_srcu, idx);
972                 return;
973         }
974
975         spin_lock(&node->state_lock);
976         /* we only ack if state is ESTABLISHED */
977         switch (node->state) {
978         case DLM_ESTABLISHED:
979                 spin_unlock(&node->state_lock);
980                 dlm_send_ack(node->nodeid, node->seq_next);
981                 break;
982         default:
983                 spin_unlock(&node->state_lock);
984                 /* do nothing FIN has it's own ack send */
985                 break;
986         }
987         srcu_read_unlock(&nodes_srcu, idx);
988 }
989
990 void dlm_midcomms_unack_msg_resend(int nodeid)
991 {
992         struct midcomms_node *node;
993         struct dlm_mhandle *mh;
994         int idx, ret;
995
996         idx = srcu_read_lock(&nodes_srcu);
997         node = nodeid2node(nodeid, 0);
998         if (!node) {
999                 srcu_read_unlock(&nodes_srcu, idx);
1000                 return;
1001         }
1002
1003         /* old protocol, we don't support to retransmit on failure */
1004         switch (node->version) {
1005         case DLM_VERSION_3_2:
1006                 break;
1007         default:
1008                 srcu_read_unlock(&nodes_srcu, idx);
1009                 return;
1010         }
1011
1012         rcu_read_lock();
1013         list_for_each_entry_rcu(mh, &node->send_queue, list) {
1014                 if (!mh->committed)
1015                         continue;
1016
1017                 ret = dlm_lowcomms_resend_msg(mh->msg);
1018                 if (!ret)
1019                         log_print_ratelimited("retransmit dlm msg, seq %u, nodeid %d",
1020                                               mh->seq, node->nodeid);
1021         }
1022         rcu_read_unlock();
1023         srcu_read_unlock(&nodes_srcu, idx);
1024 }
1025
1026 static void dlm_fill_opts_header(struct dlm_opts *opts, uint16_t inner_len,
1027                                  uint32_t seq)
1028 {
1029         opts->o_header.h_cmd = DLM_OPTS;
1030         opts->o_header.h_version = cpu_to_le32(DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
1031         opts->o_header.h_nodeid = cpu_to_le32(dlm_our_nodeid());
1032         opts->o_header.h_length = cpu_to_le16(DLM_MIDCOMMS_OPT_LEN + inner_len);
1033         opts->o_header.u.h_seq = cpu_to_le32(seq);
1034 }
1035
1036 static void midcomms_new_msg_cb(void *data)
1037 {
1038         struct dlm_mhandle *mh = data;
1039
1040         atomic_inc(&mh->node->send_queue_cnt);
1041
1042         spin_lock(&mh->node->send_queue_lock);
1043         list_add_tail_rcu(&mh->list, &mh->node->send_queue);
1044         spin_unlock(&mh->node->send_queue_lock);
1045
1046         mh->seq = mh->node->seq_send++;
1047 }
1048
1049 static struct dlm_msg *dlm_midcomms_get_msg_3_2(struct dlm_mhandle *mh, int nodeid,
1050                                                 int len, gfp_t allocation, char **ppc)
1051 {
1052         struct dlm_opts *opts;
1053         struct dlm_msg *msg;
1054
1055         msg = dlm_lowcomms_new_msg(nodeid, len + DLM_MIDCOMMS_OPT_LEN,
1056                                    allocation, ppc, midcomms_new_msg_cb, mh);
1057         if (!msg)
1058                 return NULL;
1059
1060         opts = (struct dlm_opts *)*ppc;
1061         mh->opts = opts;
1062
1063         /* add possible options here */
1064         dlm_fill_opts_header(opts, len, mh->seq);
1065
1066         *ppc += sizeof(*opts);
1067         mh->inner_p = (const union dlm_packet *)*ppc;
1068         return msg;
1069 }
1070
1071 /* avoid false positive for nodes_srcu, unlock happens in
1072  * dlm_midcomms_commit_mhandle which is a must call if success
1073  */
1074 #ifndef __CHECKER__
1075 struct dlm_mhandle *dlm_midcomms_get_mhandle(int nodeid, int len,
1076                                              gfp_t allocation, char **ppc)
1077 {
1078         struct midcomms_node *node;
1079         struct dlm_mhandle *mh;
1080         struct dlm_msg *msg;
1081         int idx;
1082
1083         idx = srcu_read_lock(&nodes_srcu);
1084         node = nodeid2node(nodeid, 0);
1085         if (!node) {
1086                 WARN_ON_ONCE(1);
1087                 goto err;
1088         }
1089
1090         /* this is a bug, however we going on and hope it will be resolved */
1091         WARN_ON_ONCE(test_bit(DLM_NODE_FLAG_STOP_TX, &node->flags));
1092
1093         mh = dlm_allocate_mhandle();
1094         if (!mh)
1095                 goto err;
1096
1097         mh->committed = false;
1098         mh->ack_rcv = NULL;
1099         mh->idx = idx;
1100         mh->node = node;
1101
1102         switch (node->version) {
1103         case DLM_VERSION_3_1:
1104                 msg = dlm_lowcomms_new_msg(nodeid, len, allocation, ppc,
1105                                            NULL, NULL);
1106                 if (!msg) {
1107                         dlm_free_mhandle(mh);
1108                         goto err;
1109                 }
1110
1111                 break;
1112         case DLM_VERSION_3_2:
1113                 msg = dlm_midcomms_get_msg_3_2(mh, nodeid, len, allocation,
1114                                                ppc);
1115                 if (!msg) {
1116                         dlm_free_mhandle(mh);
1117                         goto err;
1118                 }
1119
1120                 break;
1121         default:
1122                 dlm_free_mhandle(mh);
1123                 WARN_ON_ONCE(1);
1124                 goto err;
1125         }
1126
1127         mh->msg = msg;
1128
1129         /* keep in mind that is a must to call
1130          * dlm_midcomms_commit_msg() which releases
1131          * nodes_srcu using mh->idx which is assumed
1132          * here that the application will call it.
1133          */
1134         return mh;
1135
1136 err:
1137         srcu_read_unlock(&nodes_srcu, idx);
1138         return NULL;
1139 }
1140 #endif
1141
1142 static void dlm_midcomms_commit_msg_3_2_trace(const struct dlm_mhandle *mh,
1143                                               const void *name, int namelen)
1144 {
1145         switch (mh->inner_p->header.h_cmd) {
1146         case DLM_MSG:
1147                 trace_dlm_send_message(mh->seq, &mh->inner_p->message,
1148                                        name, namelen);
1149                 break;
1150         case DLM_RCOM:
1151                 trace_dlm_send_rcom(mh->seq, &mh->inner_p->rcom);
1152                 break;
1153         default:
1154                 /* nothing to trace */
1155                 break;
1156         }
1157 }
1158
1159 static void dlm_midcomms_commit_msg_3_2(struct dlm_mhandle *mh,
1160                                         const void *name, int namelen)
1161 {
1162         /* nexthdr chain for fast lookup */
1163         mh->opts->o_nextcmd = mh->inner_p->header.h_cmd;
1164         mh->committed = true;
1165         dlm_midcomms_commit_msg_3_2_trace(mh, name, namelen);
1166         dlm_lowcomms_commit_msg(mh->msg);
1167 }
1168
1169 /* avoid false positive for nodes_srcu, lock was happen in
1170  * dlm_midcomms_get_mhandle
1171  */
1172 #ifndef __CHECKER__
1173 void dlm_midcomms_commit_mhandle(struct dlm_mhandle *mh,
1174                                  const void *name, int namelen)
1175 {
1176
1177         switch (mh->node->version) {
1178         case DLM_VERSION_3_1:
1179                 srcu_read_unlock(&nodes_srcu, mh->idx);
1180
1181                 dlm_lowcomms_commit_msg(mh->msg);
1182                 dlm_lowcomms_put_msg(mh->msg);
1183                 /* mh is not part of rcu list in this case */
1184                 dlm_free_mhandle(mh);
1185                 break;
1186         case DLM_VERSION_3_2:
1187                 /* held rcu read lock here, because we sending the
1188                  * dlm message out, when we do that we could receive
1189                  * an ack back which releases the mhandle and we
1190                  * get a use after free.
1191                  */
1192                 rcu_read_lock();
1193                 dlm_midcomms_commit_msg_3_2(mh, name, namelen);
1194                 srcu_read_unlock(&nodes_srcu, mh->idx);
1195                 rcu_read_unlock();
1196                 break;
1197         default:
1198                 srcu_read_unlock(&nodes_srcu, mh->idx);
1199                 WARN_ON_ONCE(1);
1200                 break;
1201         }
1202 }
1203 #endif
1204
1205 int dlm_midcomms_start(void)
1206 {
1207         return dlm_lowcomms_start();
1208 }
1209
1210 void dlm_midcomms_stop(void)
1211 {
1212         dlm_lowcomms_stop();
1213 }
1214
1215 void dlm_midcomms_init(void)
1216 {
1217         int i;
1218
1219         for (i = 0; i < CONN_HASH_SIZE; i++)
1220                 INIT_HLIST_HEAD(&node_hash[i]);
1221
1222         dlm_lowcomms_init();
1223 }
1224
1225 void dlm_midcomms_exit(void)
1226 {
1227         dlm_lowcomms_exit();
1228 }
1229
1230 static void dlm_act_fin_ack_rcv(struct midcomms_node *node)
1231 {
1232         spin_lock(&node->state_lock);
1233         pr_debug("receive active fin ack from node %d with state %s\n",
1234                  node->nodeid, dlm_state_str(node->state));
1235
1236         switch (node->state) {
1237         case DLM_FIN_WAIT1:
1238                 node->state = DLM_FIN_WAIT2;
1239                 pr_debug("switch node %d to state %s\n",
1240                          node->nodeid, dlm_state_str(node->state));
1241                 break;
1242         case DLM_CLOSING:
1243                 midcomms_node_reset(node);
1244                 pr_debug("switch node %d to state %s\n",
1245                          node->nodeid, dlm_state_str(node->state));
1246                 wake_up(&node->shutdown_wait);
1247                 break;
1248         case DLM_CLOSED:
1249                 /* not valid but somehow we got what we want */
1250                 wake_up(&node->shutdown_wait);
1251                 break;
1252         default:
1253                 spin_unlock(&node->state_lock);
1254                 log_print("%s: unexpected state: %d\n",
1255                           __func__, node->state);
1256                 WARN_ON_ONCE(1);
1257                 return;
1258         }
1259         spin_unlock(&node->state_lock);
1260 }
1261
1262 void dlm_midcomms_add_member(int nodeid)
1263 {
1264         struct midcomms_node *node;
1265         int idx;
1266
1267         if (nodeid == dlm_our_nodeid())
1268                 return;
1269
1270         idx = srcu_read_lock(&nodes_srcu);
1271         node = nodeid2node(nodeid, GFP_NOFS);
1272         if (!node) {
1273                 srcu_read_unlock(&nodes_srcu, idx);
1274                 return;
1275         }
1276
1277         spin_lock(&node->state_lock);
1278         if (!node->users) {
1279                 pr_debug("receive add member from node %d with state %s\n",
1280                          node->nodeid, dlm_state_str(node->state));
1281                 switch (node->state) {
1282                 case DLM_ESTABLISHED:
1283                         break;
1284                 case DLM_CLOSED:
1285                         node->state = DLM_ESTABLISHED;
1286                         pr_debug("switch node %d to state %s\n",
1287                                  node->nodeid, dlm_state_str(node->state));
1288                         break;
1289                 default:
1290                         /* some invalid state passive shutdown
1291                          * was failed, we try to reset and
1292                          * hope it will go on.
1293                          */
1294                         log_print("reset node %d because shutdown stuck",
1295                                   node->nodeid);
1296
1297                         midcomms_node_reset(node);
1298                         node->state = DLM_ESTABLISHED;
1299                         break;
1300                 }
1301         }
1302
1303         node->users++;
1304         pr_debug("node %d users inc count %d\n", nodeid, node->users);
1305         spin_unlock(&node->state_lock);
1306
1307         srcu_read_unlock(&nodes_srcu, idx);
1308 }
1309
1310 void dlm_midcomms_remove_member(int nodeid)
1311 {
1312         struct midcomms_node *node;
1313         int idx;
1314
1315         if (nodeid == dlm_our_nodeid())
1316                 return;
1317
1318         idx = srcu_read_lock(&nodes_srcu);
1319         node = nodeid2node(nodeid, 0);
1320         if (!node) {
1321                 srcu_read_unlock(&nodes_srcu, idx);
1322                 return;
1323         }
1324
1325         spin_lock(&node->state_lock);
1326         node->users--;
1327         pr_debug("node %d users dec count %d\n", nodeid, node->users);
1328
1329         /* hitting users count to zero means the
1330          * other side is running dlm_midcomms_stop()
1331          * we meet us to have a clean disconnect.
1332          */
1333         if (node->users == 0) {
1334                 pr_debug("receive remove member from node %d with state %s\n",
1335                          node->nodeid, dlm_state_str(node->state));
1336                 switch (node->state) {
1337                 case DLM_ESTABLISHED:
1338                         break;
1339                 case DLM_CLOSE_WAIT:
1340                         /* passive shutdown DLM_LAST_ACK case 2 */
1341                         node->state = DLM_LAST_ACK;
1342                         pr_debug("switch node %d to state %s case 2\n",
1343                                  node->nodeid, dlm_state_str(node->state));
1344                         set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
1345                         dlm_send_fin(node, dlm_pas_fin_ack_rcv);
1346                         break;
1347                 case DLM_LAST_ACK:
1348                         /* probably receive fin caught it, do nothing */
1349                         break;
1350                 case DLM_CLOSED:
1351                         /* already gone, do nothing */
1352                         break;
1353                 default:
1354                         log_print("%s: unexpected state: %d\n",
1355                                   __func__, node->state);
1356                         break;
1357                 }
1358         }
1359         spin_unlock(&node->state_lock);
1360
1361         srcu_read_unlock(&nodes_srcu, idx);
1362 }
1363
1364 static void midcomms_node_release(struct rcu_head *rcu)
1365 {
1366         struct midcomms_node *node = container_of(rcu, struct midcomms_node, rcu);
1367
1368         WARN_ON_ONCE(atomic_read(&node->send_queue_cnt));
1369         dlm_send_queue_flush(node);
1370         kfree(node);
1371 }
1372
1373 static void midcomms_shutdown(struct midcomms_node *node)
1374 {
1375         int ret;
1376
1377         /* old protocol, we don't wait for pending operations */
1378         switch (node->version) {
1379         case DLM_VERSION_3_2:
1380                 break;
1381         default:
1382                 return;
1383         }
1384
1385         spin_lock(&node->state_lock);
1386         pr_debug("receive active shutdown for node %d with state %s\n",
1387                  node->nodeid, dlm_state_str(node->state));
1388         switch (node->state) {
1389         case DLM_ESTABLISHED:
1390                 node->state = DLM_FIN_WAIT1;
1391                 pr_debug("switch node %d to state %s case 2\n",
1392                          node->nodeid, dlm_state_str(node->state));
1393                 dlm_send_fin(node, dlm_act_fin_ack_rcv);
1394                 break;
1395         case DLM_CLOSED:
1396                 /* we have what we want */
1397                 spin_unlock(&node->state_lock);
1398                 return;
1399         default:
1400                 /* busy to enter DLM_FIN_WAIT1, wait until passive
1401                  * done in shutdown_wait to enter DLM_CLOSED.
1402                  */
1403                 break;
1404         }
1405         spin_unlock(&node->state_lock);
1406
1407         if (DLM_DEBUG_FENCE_TERMINATION)
1408                 msleep(5000);
1409
1410         /* wait for other side dlm + fin */
1411         ret = wait_event_timeout(node->shutdown_wait,
1412                                  node->state == DLM_CLOSED ||
1413                                  test_bit(DLM_NODE_FLAG_CLOSE, &node->flags),
1414                                  DLM_SHUTDOWN_TIMEOUT);
1415         if (!ret || test_bit(DLM_NODE_FLAG_CLOSE, &node->flags)) {
1416                 pr_debug("active shutdown timed out for node %d with state %s\n",
1417                          node->nodeid, dlm_state_str(node->state));
1418                 midcomms_node_reset(node);
1419                 return;
1420         }
1421
1422         pr_debug("active shutdown done for node %d with state %s\n",
1423                  node->nodeid, dlm_state_str(node->state));
1424 }
1425
1426 void dlm_midcomms_shutdown(void)
1427 {
1428         struct midcomms_node *node;
1429         int i, idx;
1430
1431         mutex_lock(&close_lock);
1432         idx = srcu_read_lock(&nodes_srcu);
1433         for (i = 0; i < CONN_HASH_SIZE; i++) {
1434                 hlist_for_each_entry_rcu(node, &node_hash[i], hlist) {
1435                         midcomms_shutdown(node);
1436
1437                         dlm_delete_debug_comms_file(node->debugfs);
1438
1439                         spin_lock(&nodes_lock);
1440                         hlist_del_rcu(&node->hlist);
1441                         spin_unlock(&nodes_lock);
1442
1443                         call_srcu(&nodes_srcu, &node->rcu, midcomms_node_release);
1444                 }
1445         }
1446         srcu_read_unlock(&nodes_srcu, idx);
1447         mutex_unlock(&close_lock);
1448
1449         dlm_lowcomms_shutdown();
1450 }
1451
1452 int dlm_midcomms_close(int nodeid)
1453 {
1454         struct midcomms_node *node;
1455         int idx, ret;
1456
1457         if (nodeid == dlm_our_nodeid())
1458                 return 0;
1459
1460         idx = srcu_read_lock(&nodes_srcu);
1461         /* Abort pending close/remove operation */
1462         node = nodeid2node(nodeid, 0);
1463         if (node) {
1464                 /* let shutdown waiters leave */
1465                 set_bit(DLM_NODE_FLAG_CLOSE, &node->flags);
1466                 wake_up(&node->shutdown_wait);
1467         }
1468         srcu_read_unlock(&nodes_srcu, idx);
1469
1470         synchronize_srcu(&nodes_srcu);
1471
1472         idx = srcu_read_lock(&nodes_srcu);
1473         mutex_lock(&close_lock);
1474         node = nodeid2node(nodeid, 0);
1475         if (!node) {
1476                 mutex_unlock(&close_lock);
1477                 srcu_read_unlock(&nodes_srcu, idx);
1478                 return dlm_lowcomms_close(nodeid);
1479         }
1480
1481         ret = dlm_lowcomms_close(nodeid);
1482         spin_lock(&node->state_lock);
1483         midcomms_node_reset(node);
1484         spin_unlock(&node->state_lock);
1485         srcu_read_unlock(&nodes_srcu, idx);
1486         mutex_unlock(&close_lock);
1487
1488         return ret;
1489 }
1490
1491 /* debug functionality to send raw dlm msg from user space */
1492 struct dlm_rawmsg_data {
1493         struct midcomms_node *node;
1494         void *buf;
1495 };
1496
1497 static void midcomms_new_rawmsg_cb(void *data)
1498 {
1499         struct dlm_rawmsg_data *rd = data;
1500         struct dlm_header *h = rd->buf;
1501
1502         switch (h->h_version) {
1503         case cpu_to_le32(DLM_VERSION_3_1):
1504                 break;
1505         default:
1506                 switch (h->h_cmd) {
1507                 case DLM_OPTS:
1508                         if (!h->u.h_seq)
1509                                 h->u.h_seq = cpu_to_le32(rd->node->seq_send++);
1510                         break;
1511                 default:
1512                         break;
1513                 }
1514                 break;
1515         }
1516 }
1517
1518 int dlm_midcomms_rawmsg_send(struct midcomms_node *node, void *buf,
1519                              int buflen)
1520 {
1521         struct dlm_rawmsg_data rd;
1522         struct dlm_msg *msg;
1523         char *msgbuf;
1524
1525         rd.node = node;
1526         rd.buf = buf;
1527
1528         msg = dlm_lowcomms_new_msg(node->nodeid, buflen, GFP_NOFS,
1529                                    &msgbuf, midcomms_new_rawmsg_cb, &rd);
1530         if (!msg)
1531                 return -ENOMEM;
1532
1533         memcpy(msgbuf, buf, buflen);
1534         dlm_lowcomms_commit_msg(msg);
1535         return 0;
1536 }
1537