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