Linux Audio

Check our new training course

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