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