Linux Audio

Check our new training course

Loading...
v6.13.7
   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-2009 Red Hat, Inc.  All rights reserved.
   7**
 
 
 
   8**
   9*******************************************************************************
  10******************************************************************************/
  11
  12/*
  13 * lowcomms.c
  14 *
  15 * This is the "low-level" comms layer.
  16 *
  17 * It is responsible for sending/receiving messages
  18 * from other nodes in the cluster.
  19 *
  20 * Cluster nodes are referred to by their nodeids. nodeids are
  21 * simply 32 bit numbers to the locking module - if they need to
  22 * be expanded for the cluster infrastructure then that is its
  23 * responsibility. It is this layer's
  24 * responsibility to resolve these into IP address or
  25 * whatever it needs for inter-node communication.
  26 *
  27 * The comms level is two kernel threads that deal mainly with
  28 * the receiving of messages from other nodes and passing them
  29 * up to the mid-level comms layer (which understands the
  30 * message format) for execution by the locking core, and
  31 * a send thread which does all the setting up of connections
  32 * to remote nodes and the sending of data. Threads are not allowed
  33 * to send their own data because it may cause them to wait in times
  34 * of high load. Also, this way, the sending thread can collect together
  35 * messages bound for one node and send them in one block.
  36 *
  37 * lowcomms will choose to use either TCP or SCTP as its transport layer
  38 * depending on the configuration variable 'protocol'. This should be set
  39 * to 0 (default) for TCP or 1 for SCTP. It should be configured using a
  40 * cluster-wide mechanism as it must be the same on all nodes of the cluster
  41 * for the DLM to function.
  42 *
  43 */
  44
  45#include <asm/ioctls.h>
  46#include <net/sock.h>
  47#include <net/tcp.h>
  48#include <linux/pagemap.h>
  49#include <linux/file.h>
  50#include <linux/mutex.h>
  51#include <linux/sctp.h>
  52#include <linux/slab.h>
  53#include <net/sctp/sctp.h>
  54#include <net/ipv6.h>
  55
  56#include <trace/events/dlm.h>
  57#include <trace/events/sock.h>
  58
  59#include "dlm_internal.h"
  60#include "lowcomms.h"
  61#include "midcomms.h"
  62#include "memory.h"
  63#include "config.h"
  64
  65#define DLM_SHUTDOWN_WAIT_TIMEOUT msecs_to_jiffies(5000)
  66#define DLM_MAX_PROCESS_BUFFERS 24
  67#define NEEDED_RMEM (4*1024*1024)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  68
  69struct connection {
  70	struct socket *sock;	/* NULL if not connected */
  71	uint32_t nodeid;	/* So we know who we are in the list */
  72	/* this semaphore is used to allow parallel recv/send in read
  73	 * lock mode. When we release a sock we need to held the write lock.
  74	 *
  75	 * However this is locking code and not nice. When we remove the
  76	 * othercon handling we can look into other mechanism to synchronize
  77	 * io handling to call sock_release() at the right time.
  78	 */
  79	struct rw_semaphore sock_lock;
  80	unsigned long flags;
  81#define CF_APP_LIMITED 0
  82#define CF_RECV_PENDING 1
  83#define CF_SEND_PENDING 2
  84#define CF_RECV_INTR 3
  85#define CF_IO_STOP 4
  86#define CF_IS_OTHERCON 5
 
 
  87	struct list_head writequeue;  /* List of outgoing writequeue_entries */
  88	spinlock_t writequeue_lock;
 
 
 
 
  89	int retries;
 
  90	struct hlist_node list;
  91	/* due some connect()/accept() races we currently have this cross over
  92	 * connection attempt second connection for one node.
  93	 *
  94	 * There is a solution to avoid the race by introducing a connect
  95	 * rule as e.g. our_nodeid > nodeid_to_connect who is allowed to
  96	 * connect. Otherside can connect but will only be considered that
  97	 * the other side wants to have a reconnect.
  98	 *
  99	 * However changing to this behaviour will break backwards compatible.
 100	 * In a DLM protocol major version upgrade we should remove this!
 101	 */
 102	struct connection *othercon;
 103	struct work_struct rwork; /* receive worker */
 104	struct work_struct swork; /* send worker */
 105	wait_queue_head_t shutdown_wait;
 106	unsigned char rx_leftover_buf[DLM_MAX_SOCKET_BUFSIZE];
 107	int rx_leftover;
 108	int mark;
 109	int addr_count;
 110	int curr_addr_index;
 111	struct sockaddr_storage addr[DLM_MAX_ADDR_COUNT];
 112	spinlock_t addrs_lock;
 113	struct rcu_head rcu;
 114};
 115#define sock2con(x) ((struct connection *)(x)->sk_user_data)
 116
 117struct listen_connection {
 118	struct socket *sock;
 119	struct work_struct rwork;
 120};
 121
 122#define DLM_WQ_REMAIN_BYTES(e) (PAGE_SIZE - e->end)
 123#define DLM_WQ_LENGTH_BYTES(e) (e->end - e->offset)
 124
 125/* An entry waiting to be sent */
 126struct writequeue_entry {
 127	struct list_head list;
 128	struct page *page;
 129	int offset;
 130	int len;
 131	int end;
 132	int users;
 133	bool dirty;
 134	struct connection *con;
 135	struct list_head msgs;
 136	struct kref ref;
 137};
 138
 139struct dlm_msg {
 140	struct writequeue_entry *entry;
 141	struct dlm_msg *orig_msg;
 142	bool retransmit;
 143	void *ppc;
 144	int len;
 145	int idx; /* new()/commit() idx exchange */
 146
 147	struct list_head list;
 148	struct kref ref;
 149};
 150
 151struct processqueue_entry {
 152	unsigned char *buf;
 153	int nodeid;
 154	int buflen;
 155
 156	struct list_head list;
 157};
 158
 159struct dlm_proto_ops {
 160	bool try_new_addr;
 161	const char *name;
 162	int proto;
 163
 164	void (*sockopts)(struct socket *sock);
 165	int (*bind)(struct socket *sock);
 166	int (*listen_validate)(void);
 167	void (*listen_sockopts)(struct socket *sock);
 168	int (*listen_bind)(struct socket *sock);
 169};
 170
 171static struct listen_sock_callbacks {
 172	void (*sk_error_report)(struct sock *);
 173	void (*sk_data_ready)(struct sock *);
 174	void (*sk_state_change)(struct sock *);
 175	void (*sk_write_space)(struct sock *);
 176} listen_sock;
 177
 178static struct listen_connection listen_con;
 179static struct sockaddr_storage dlm_local_addr[DLM_MAX_ADDR_COUNT];
 180static int dlm_local_count;
 
 181
 182/* Work queues */
 183static struct workqueue_struct *io_workqueue;
 184static struct workqueue_struct *process_workqueue;
 185
 186static struct hlist_head connection_hash[CONN_HASH_SIZE];
 187static DEFINE_SPINLOCK(connections_lock);
 188DEFINE_STATIC_SRCU(connections_srcu);
 189
 190static const struct dlm_proto_ops *dlm_proto_ops;
 191
 192#define DLM_IO_SUCCESS 0
 193#define DLM_IO_END 1
 194#define DLM_IO_EOF 2
 195#define DLM_IO_RESCHED 3
 196#define DLM_IO_FLUSH 4
 197
 198static void process_recv_sockets(struct work_struct *work);
 199static void process_send_sockets(struct work_struct *work);
 200static void process_dlm_messages(struct work_struct *work);
 201
 202static DECLARE_WORK(process_work, process_dlm_messages);
 203static DEFINE_SPINLOCK(processqueue_lock);
 204static bool process_dlm_messages_pending;
 205static DECLARE_WAIT_QUEUE_HEAD(processqueue_wq);
 206static atomic_t processqueue_count;
 207static LIST_HEAD(processqueue);
 208
 209bool dlm_lowcomms_is_running(void)
 
 
 
 210{
 211	return !!listen_con.sock;
 212}
 213
 214static void lowcomms_queue_swork(struct connection *con)
 215{
 216	assert_spin_locked(&con->writequeue_lock);
 217
 218	if (!test_bit(CF_IO_STOP, &con->flags) &&
 219	    !test_bit(CF_APP_LIMITED, &con->flags) &&
 220	    !test_and_set_bit(CF_SEND_PENDING, &con->flags))
 221		queue_work(io_workqueue, &con->swork);
 222}
 223
 224static void lowcomms_queue_rwork(struct connection *con)
 225{
 226#ifdef CONFIG_LOCKDEP
 227	WARN_ON_ONCE(!lockdep_sock_is_held(con->sock->sk));
 228#endif
 229
 230	if (!test_bit(CF_IO_STOP, &con->flags) &&
 231	    !test_and_set_bit(CF_RECV_PENDING, &con->flags))
 232		queue_work(io_workqueue, &con->rwork);
 233}
 234
 235static void writequeue_entry_ctor(void *data)
 236{
 237	struct writequeue_entry *entry = data;
 238
 239	INIT_LIST_HEAD(&entry->msgs);
 240}
 241
 242struct kmem_cache *dlm_lowcomms_writequeue_cache_create(void)
 243{
 244	return kmem_cache_create("dlm_writequeue", sizeof(struct writequeue_entry),
 245				 0, 0, writequeue_entry_ctor);
 246}
 247
 248struct kmem_cache *dlm_lowcomms_msg_cache_create(void)
 249{
 250	return KMEM_CACHE(dlm_msg, 0);
 251}
 252
 253/* need to held writequeue_lock */
 254static struct writequeue_entry *con_next_wq(struct connection *con)
 255{
 256	struct writequeue_entry *e;
 257
 258	e = list_first_entry_or_null(&con->writequeue, struct writequeue_entry,
 259				     list);
 260	/* if len is zero nothing is to send, if there are users filling
 261	 * buffers we wait until the users are done so we can send more.
 262	 */
 263	if (!e || e->users || e->len == 0)
 264		return NULL;
 265
 266	return e;
 267}
 268
 269static struct connection *__find_con(int nodeid, int r)
 270{
 
 271	struct connection *con;
 272
 273	hlist_for_each_entry_rcu(con, &connection_hash[r], list) {
 
 
 274		if (con->nodeid == nodeid)
 275			return con;
 276	}
 277
 278	return NULL;
 279}
 280
 281static void dlm_con_init(struct connection *con, int nodeid)
 282{
 283	con->nodeid = nodeid;
 284	init_rwsem(&con->sock_lock);
 285	INIT_LIST_HEAD(&con->writequeue);
 286	spin_lock_init(&con->writequeue_lock);
 287	INIT_WORK(&con->swork, process_send_sockets);
 288	INIT_WORK(&con->rwork, process_recv_sockets);
 289	spin_lock_init(&con->addrs_lock);
 290	init_waitqueue_head(&con->shutdown_wait);
 291}
 292
 293/*
 294 * If 'allocation' is zero then we don't attempt to create a new
 295 * connection structure for this node.
 296 */
 297static struct connection *nodeid2con(int nodeid, gfp_t alloc)
 298{
 299	struct connection *con, *tmp;
 300	int r;
 301
 302	r = nodeid_hash(nodeid);
 303	con = __find_con(nodeid, r);
 304	if (con || !alloc)
 305		return con;
 306
 307	con = kzalloc(sizeof(*con), alloc);
 308	if (!con)
 309		return NULL;
 310
 311	dlm_con_init(con, nodeid);
 
 312
 313	spin_lock(&connections_lock);
 314	/* Because multiple workqueues/threads calls this function it can
 315	 * race on multiple cpu's. Instead of locking hot path __find_con()
 316	 * we just check in rare cases of recently added nodes again
 317	 * under protection of connections_lock. If this is the case we
 318	 * abort our connection creation and return the existing connection.
 319	 */
 320	tmp = __find_con(nodeid, r);
 321	if (tmp) {
 322		spin_unlock(&connections_lock);
 323		kfree(con);
 324		return tmp;
 
 
 325	}
 326
 327	hlist_add_head_rcu(&con->list, &connection_hash[r]);
 328	spin_unlock(&connections_lock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 329
 330	return con;
 331}
 332
 333static int addr_compare(const struct sockaddr_storage *x,
 334			const struct sockaddr_storage *y)
 
 
 
 
 
 
 
 
 
 
 335{
 336	switch (x->ss_family) {
 337	case AF_INET: {
 338		struct sockaddr_in *sinx = (struct sockaddr_in *)x;
 339		struct sockaddr_in *siny = (struct sockaddr_in *)y;
 340		if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
 341			return 0;
 342		if (sinx->sin_port != siny->sin_port)
 343			return 0;
 344		break;
 345	}
 346	case AF_INET6: {
 347		struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
 348		struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
 349		if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
 350			return 0;
 351		if (sinx->sin6_port != siny->sin6_port)
 352			return 0;
 353		break;
 354	}
 355	default:
 356		return 0;
 357	}
 358	return 1;
 359}
 360
 361static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out,
 362			  struct sockaddr *sa_out, bool try_new_addr,
 363			  unsigned int *mark)
 364{
 365	struct sockaddr_storage sas;
 366	struct connection *con;
 367	int idx;
 368
 369	if (!dlm_local_count)
 370		return -1;
 371
 372	idx = srcu_read_lock(&connections_srcu);
 373	con = nodeid2con(nodeid, 0);
 374	if (!con) {
 375		srcu_read_unlock(&connections_srcu, idx);
 376		return -ENOENT;
 377	}
 378
 379	spin_lock(&con->addrs_lock);
 380	if (!con->addr_count) {
 381		spin_unlock(&con->addrs_lock);
 382		srcu_read_unlock(&connections_srcu, idx);
 383		return -ENOENT;
 384	}
 
 385
 386	memcpy(&sas, &con->addr[con->curr_addr_index],
 387	       sizeof(struct sockaddr_storage));
 388
 389	if (try_new_addr) {
 390		con->curr_addr_index++;
 391		if (con->curr_addr_index == con->addr_count)
 392			con->curr_addr_index = 0;
 393	}
 394
 395	*mark = con->mark;
 396	spin_unlock(&con->addrs_lock);
 397
 398	if (sas_out)
 399		memcpy(sas_out, &sas, sizeof(struct sockaddr_storage));
 400
 401	if (!sa_out) {
 402		srcu_read_unlock(&connections_srcu, idx);
 403		return 0;
 404	}
 405
 406	if (dlm_local_addr[0].ss_family == AF_INET) {
 407		struct sockaddr_in *in4  = (struct sockaddr_in *) &sas;
 408		struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out;
 409		ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
 410	} else {
 411		struct sockaddr_in6 *in6  = (struct sockaddr_in6 *) &sas;
 412		struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out;
 413		ret6->sin6_addr = in6->sin6_addr;
 414	}
 415
 416	srcu_read_unlock(&connections_srcu, idx);
 417	return 0;
 418}
 419
 420static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid,
 421			  unsigned int *mark)
 422{
 423	struct connection *con;
 424	int i, idx, addr_i;
 425
 426	idx = srcu_read_lock(&connections_srcu);
 427	for (i = 0; i < CONN_HASH_SIZE; i++) {
 428		hlist_for_each_entry_rcu(con, &connection_hash[i], list) {
 429			WARN_ON_ONCE(!con->addr_count);
 430
 431			spin_lock(&con->addrs_lock);
 432			for (addr_i = 0; addr_i < con->addr_count; addr_i++) {
 433				if (addr_compare(&con->addr[addr_i], addr)) {
 434					*nodeid = con->nodeid;
 435					*mark = con->mark;
 436					spin_unlock(&con->addrs_lock);
 437					srcu_read_unlock(&connections_srcu, idx);
 438					return 0;
 439				}
 440			}
 441			spin_unlock(&con->addrs_lock);
 442		}
 443	}
 444	srcu_read_unlock(&connections_srcu, idx);
 445
 446	return -ENOENT;
 447}
 448
 449static bool dlm_lowcomms_con_has_addr(const struct connection *con,
 450				      const struct sockaddr_storage *addr)
 451{
 452	int i;
 
 453
 454	for (i = 0; i < con->addr_count; i++) {
 455		if (addr_compare(&con->addr[i], addr))
 456			return true;
 457	}
 458
 459	return false;
 460}
 461
 462int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr)
 463{
 464	struct connection *con;
 465	bool ret;
 466	int idx;
 467
 468	idx = srcu_read_lock(&connections_srcu);
 469	con = nodeid2con(nodeid, GFP_NOFS);
 470	if (!con) {
 471		srcu_read_unlock(&connections_srcu, idx);
 472		return -ENOMEM;
 473	}
 474
 475	spin_lock(&con->addrs_lock);
 476	if (!con->addr_count) {
 477		memcpy(&con->addr[0], addr, sizeof(*addr));
 478		con->addr_count = 1;
 479		con->mark = dlm_config.ci_mark;
 480		spin_unlock(&con->addrs_lock);
 481		srcu_read_unlock(&connections_srcu, idx);
 482		return 0;
 483	}
 484
 485	ret = dlm_lowcomms_con_has_addr(con, addr);
 486	if (ret) {
 487		spin_unlock(&con->addrs_lock);
 488		srcu_read_unlock(&connections_srcu, idx);
 489		return -EEXIST;
 
 
 
 
 490	}
 491
 492	if (con->addr_count >= DLM_MAX_ADDR_COUNT) {
 493		spin_unlock(&con->addrs_lock);
 494		srcu_read_unlock(&connections_srcu, idx);
 
 495		return -ENOSPC;
 496	}
 497
 498	memcpy(&con->addr[con->addr_count++], addr, sizeof(*addr));
 499	srcu_read_unlock(&connections_srcu, idx);
 500	spin_unlock(&con->addrs_lock);
 501	return 0;
 502}
 503
 504/* Data available on socket or listen socket received a connect */
 505static void lowcomms_data_ready(struct sock *sk)
 506{
 507	struct connection *con = sock2con(sk);
 508
 509	trace_sk_data_ready(sk);
 510
 511	set_bit(CF_RECV_INTR, &con->flags);
 512	lowcomms_queue_rwork(con);
 513}
 514
 515static void lowcomms_write_space(struct sock *sk)
 516{
 517	struct connection *con = sock2con(sk);
 518
 
 
 
 519	clear_bit(SOCK_NOSPACE, &con->sock->flags);
 520
 521	spin_lock_bh(&con->writequeue_lock);
 522	if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) {
 523		con->sock->sk->sk_write_pending--;
 524		clear_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags);
 525	}
 526
 527	lowcomms_queue_swork(con);
 528	spin_unlock_bh(&con->writequeue_lock);
 529}
 530
 531static void lowcomms_state_change(struct sock *sk)
 532{
 533	/* SCTP layer is not calling sk_data_ready when the connection
 534	 * is done, so we catch the signal through here.
 535	 */
 536	if (sk->sk_shutdown == RCV_SHUTDOWN)
 537		lowcomms_data_ready(sk);
 538}
 539
 540static void lowcomms_listen_data_ready(struct sock *sk)
 541{
 542	trace_sk_data_ready(sk);
 543
 544	queue_work(io_workqueue, &listen_con.rwork);
 
 
 
 
 
 
 
 
 545}
 546
 547int dlm_lowcomms_connect_node(int nodeid)
 548{
 549	struct connection *con;
 550	int idx;
 551
 552	idx = srcu_read_lock(&connections_srcu);
 553	con = nodeid2con(nodeid, 0);
 554	if (WARN_ON_ONCE(!con)) {
 555		srcu_read_unlock(&connections_srcu, idx);
 556		return -ENOENT;
 557	}
 558
 559	down_read(&con->sock_lock);
 560	if (!con->sock) {
 561		spin_lock_bh(&con->writequeue_lock);
 562		lowcomms_queue_swork(con);
 563		spin_unlock_bh(&con->writequeue_lock);
 564	}
 565	up_read(&con->sock_lock);
 566	srcu_read_unlock(&connections_srcu, idx);
 567
 568	cond_resched();
 
 
 
 569	return 0;
 570}
 571
 572int dlm_lowcomms_nodes_set_mark(int nodeid, unsigned int mark)
 573{
 574	struct connection *con;
 575	int idx;
 576
 577	idx = srcu_read_lock(&connections_srcu);
 578	con = nodeid2con(nodeid, 0);
 579	if (!con) {
 580		srcu_read_unlock(&connections_srcu, idx);
 581		return -ENOENT;
 582	}
 583
 584	spin_lock(&con->addrs_lock);
 585	con->mark = mark;
 586	spin_unlock(&con->addrs_lock);
 587	srcu_read_unlock(&connections_srcu, idx);
 588	return 0;
 589}
 590
 591static void lowcomms_error_report(struct sock *sk)
 592{
 593	struct connection *con = sock2con(sk);
 594	struct inet_sock *inet;
 595
 596	inet = inet_sk(sk);
 597	switch (sk->sk_family) {
 598	case AF_INET:
 599		printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
 600				   "sending to node %d at %pI4, dport %d, "
 601				   "sk_err=%d/%d\n", dlm_our_nodeid(),
 602				   con->nodeid, &inet->inet_daddr,
 603				   ntohs(inet->inet_dport), sk->sk_err,
 604				   READ_ONCE(sk->sk_err_soft));
 605		break;
 606#if IS_ENABLED(CONFIG_IPV6)
 607	case AF_INET6:
 608		printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
 609				   "sending to node %d at %pI6c, "
 610				   "dport %d, sk_err=%d/%d\n", dlm_our_nodeid(),
 611				   con->nodeid, &sk->sk_v6_daddr,
 612				   ntohs(inet->inet_dport), sk->sk_err,
 613				   READ_ONCE(sk->sk_err_soft));
 614		break;
 615#endif
 616	default:
 617		printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
 618				   "invalid socket family %d set, "
 619				   "sk_err=%d/%d\n", dlm_our_nodeid(),
 620				   sk->sk_family, sk->sk_err,
 621				   READ_ONCE(sk->sk_err_soft));
 622		break;
 623	}
 624
 625	dlm_midcomms_unack_msg_resend(con->nodeid);
 626
 627	listen_sock.sk_error_report(sk);
 
 
 
 
 
 
 
 
 
 
 
 
 
 628}
 629
 630static void restore_callbacks(struct sock *sk)
 
 631{
 632#ifdef CONFIG_LOCKDEP
 633	WARN_ON_ONCE(!lockdep_sock_is_held(sk));
 634#endif
 
 
 
 
 635
 
 
 
 
 636	sk->sk_user_data = NULL;
 637	sk->sk_data_ready = listen_sock.sk_data_ready;
 638	sk->sk_state_change = listen_sock.sk_state_change;
 639	sk->sk_write_space = listen_sock.sk_write_space;
 640	sk->sk_error_report = listen_sock.sk_error_report;
 
 
 641}
 642
 643/* Make a socket active */
 644static void add_sock(struct socket *sock, struct connection *con)
 645{
 646	struct sock *sk = sock->sk;
 647
 648	lock_sock(sk);
 649	con->sock = sock;
 650
 651	sk->sk_user_data = con;
 
 
 
 652	sk->sk_data_ready = lowcomms_data_ready;
 653	sk->sk_write_space = lowcomms_write_space;
 654	if (dlm_config.ci_protocol == DLM_PROTO_SCTP)
 655		sk->sk_state_change = lowcomms_state_change;
 656	sk->sk_allocation = GFP_NOFS;
 657	sk->sk_use_task_frag = false;
 658	sk->sk_error_report = lowcomms_error_report;
 659	release_sock(sk);
 660}
 661
 662/* Add the port number to an IPv6 or 4 sockaddr and return the address
 663   length */
 664static void make_sockaddr(struct sockaddr_storage *saddr, __be16 port,
 665			  int *addr_len)
 666{
 667	saddr->ss_family =  dlm_local_addr[0].ss_family;
 668	if (saddr->ss_family == AF_INET) {
 669		struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
 670		in4_addr->sin_port = port;
 671		*addr_len = sizeof(struct sockaddr_in);
 672		memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
 673	} else {
 674		struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
 675		in6_addr->sin6_port = port;
 676		*addr_len = sizeof(struct sockaddr_in6);
 677	}
 678	memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
 679}
 680
 681static void dlm_page_release(struct kref *kref)
 682{
 683	struct writequeue_entry *e = container_of(kref, struct writequeue_entry,
 684						  ref);
 685
 686	__free_page(e->page);
 687	dlm_free_writequeue(e);
 688}
 689
 690static void dlm_msg_release(struct kref *kref)
 691{
 692	struct dlm_msg *msg = container_of(kref, struct dlm_msg, ref);
 693
 694	kref_put(&msg->entry->ref, dlm_page_release);
 695	dlm_free_msg(msg);
 696}
 697
 698static void free_entry(struct writequeue_entry *e)
 699{
 700	struct dlm_msg *msg, *tmp;
 701
 702	list_for_each_entry_safe(msg, tmp, &e->msgs, list) {
 703		if (msg->orig_msg) {
 704			msg->orig_msg->retransmit = false;
 705			kref_put(&msg->orig_msg->ref, dlm_msg_release);
 706		}
 707
 708		list_del(&msg->list);
 709		kref_put(&msg->ref, dlm_msg_release);
 710	}
 711
 712	list_del(&e->list);
 713	kref_put(&e->ref, dlm_page_release);
 714}
 715
 716static void dlm_close_sock(struct socket **sock)
 717{
 718	lock_sock((*sock)->sk);
 719	restore_callbacks((*sock)->sk);
 720	release_sock((*sock)->sk);
 721
 722	sock_release(*sock);
 723	*sock = NULL;
 724}
 725
 726static void allow_connection_io(struct connection *con)
 727{
 728	if (con->othercon)
 729		clear_bit(CF_IO_STOP, &con->othercon->flags);
 730	clear_bit(CF_IO_STOP, &con->flags);
 731}
 732
 733static void stop_connection_io(struct connection *con)
 734{
 735	if (con->othercon)
 736		stop_connection_io(con->othercon);
 
 
 
 
 737
 738	spin_lock_bh(&con->writequeue_lock);
 739	set_bit(CF_IO_STOP, &con->flags);
 740	spin_unlock_bh(&con->writequeue_lock);
 741
 742	down_write(&con->sock_lock);
 743	if (con->sock) {
 744		lock_sock(con->sock->sk);
 745		restore_callbacks(con->sock->sk);
 746		release_sock(con->sock->sk);
 
 
 
 
 
 
 
 
 
 747	}
 748	up_write(&con->sock_lock);
 749
 750	cancel_work_sync(&con->swork);
 751	cancel_work_sync(&con->rwork);
 752}
 753
 754/* Close a remote connection and tidy up */
 755static void close_connection(struct connection *con, bool and_other)
 756{
 757	struct writequeue_entry *e;
 
 
 
 
 
 
 758
 759	if (con->othercon && and_other)
 760		close_connection(con->othercon, false);
 761
 762	down_write(&con->sock_lock);
 763	if (!con->sock) {
 764		up_write(&con->sock_lock);
 765		return;
 
 
 
 766	}
 767
 768	dlm_close_sock(&con->sock);
 
 
 
 
 
 
 
 
 
 769
 770	/* if we send a writequeue entry only a half way, we drop the
 771	 * whole entry because reconnection and that we not start of the
 772	 * middle of a msg which will confuse the other end.
 773	 *
 774	 * we can always drop messages because retransmits, but what we
 775	 * cannot allow is to transmit half messages which may be processed
 776	 * at the other side.
 777	 *
 778	 * our policy is to start on a clean state when disconnects, we don't
 779	 * know what's send/received on transport layer in this case.
 
 
 780	 */
 781	spin_lock_bh(&con->writequeue_lock);
 782	if (!list_empty(&con->writequeue)) {
 783		e = list_first_entry(&con->writequeue, struct writequeue_entry,
 784				     list);
 785		if (e->dirty)
 786			free_entry(e);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 787	}
 788	spin_unlock_bh(&con->writequeue_lock);
 789
 790	con->rx_leftover = 0;
 791	con->retries = 0;
 792	clear_bit(CF_APP_LIMITED, &con->flags);
 793	clear_bit(CF_RECV_PENDING, &con->flags);
 794	clear_bit(CF_SEND_PENDING, &con->flags);
 795	up_write(&con->sock_lock);
 796}
 797
 798static void shutdown_connection(struct connection *con, bool and_other)
 799{
 800	int ret;
 801
 802	if (con->othercon && and_other)
 803		shutdown_connection(con->othercon, false);
 804
 805	flush_workqueue(io_workqueue);
 806	down_read(&con->sock_lock);
 807	/* nothing to shutdown */
 808	if (!con->sock) {
 809		up_read(&con->sock_lock);
 810		return;
 811	}
 812
 813	ret = kernel_sock_shutdown(con->sock, SHUT_WR);
 814	up_read(&con->sock_lock);
 815	if (ret) {
 816		log_print("Connection %p failed to shutdown: %d will force close",
 817			  con, ret);
 818		goto force_close;
 819	} else {
 820		ret = wait_event_timeout(con->shutdown_wait, !con->sock,
 821					 DLM_SHUTDOWN_WAIT_TIMEOUT);
 822		if (ret == 0) {
 823			log_print("Connection %p shutdown timed out, will force close",
 824				  con);
 825			goto force_close;
 826		}
 827	}
 828
 829	return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 830
 831force_close:
 832	close_connection(con, false);
 833}
 834
 835static struct processqueue_entry *new_processqueue_entry(int nodeid,
 836							 int buflen)
 837{
 838	struct processqueue_entry *pentry;
 839
 840	pentry = kmalloc(sizeof(*pentry), GFP_NOFS);
 841	if (!pentry)
 842		return NULL;
 
 
 843
 844	pentry->buf = kmalloc(buflen, GFP_NOFS);
 845	if (!pentry->buf) {
 846		kfree(pentry);
 847		return NULL;
 848	}
 
 849
 850	pentry->nodeid = nodeid;
 851	return pentry;
 852}
 853
 854static void free_processqueue_entry(struct processqueue_entry *pentry)
 855{
 856	kfree(pentry->buf);
 857	kfree(pentry);
 858}
 859
 860static void process_dlm_messages(struct work_struct *work)
 861{
 862	struct processqueue_entry *pentry;
 863
 864	spin_lock_bh(&processqueue_lock);
 865	pentry = list_first_entry_or_null(&processqueue,
 866					  struct processqueue_entry, list);
 867	if (WARN_ON_ONCE(!pentry)) {
 868		process_dlm_messages_pending = false;
 869		spin_unlock_bh(&processqueue_lock);
 870		return;
 871	}
 872
 873	list_del(&pentry->list);
 874	if (atomic_dec_and_test(&processqueue_count))
 875		wake_up(&processqueue_wq);
 876	spin_unlock_bh(&processqueue_lock);
 877
 878	for (;;) {
 879		dlm_process_incoming_buffer(pentry->nodeid, pentry->buf,
 880					    pentry->buflen);
 881		free_processqueue_entry(pentry);
 882
 883		spin_lock_bh(&processqueue_lock);
 884		pentry = list_first_entry_or_null(&processqueue,
 885						  struct processqueue_entry, list);
 886		if (!pentry) {
 887			process_dlm_messages_pending = false;
 888			spin_unlock_bh(&processqueue_lock);
 889			break;
 890		}
 891
 892		list_del(&pentry->list);
 893		if (atomic_dec_and_test(&processqueue_count))
 894			wake_up(&processqueue_wq);
 895		spin_unlock_bh(&processqueue_lock);
 
 
 896	}
 897}
 898
 899/* Data received from remote end */
 900static int receive_from_sock(struct connection *con, int buflen)
 901{
 902	struct processqueue_entry *pentry;
 903	int ret, buflen_real;
 904	struct msghdr msg;
 905	struct kvec iov;
 906
 907	pentry = new_processqueue_entry(con->nodeid, buflen);
 908	if (!pentry)
 909		return DLM_IO_RESCHED;
 
 
 
 
 
 
 
 
 910
 911	memcpy(pentry->buf, con->rx_leftover_buf, con->rx_leftover);
 912
 913	/* calculate new buffer parameter regarding last receive and
 914	 * possible leftover bytes
 
 
 915	 */
 916	iov.iov_base = pentry->buf + con->rx_leftover;
 917	iov.iov_len = buflen - con->rx_leftover;
 
 
 
 
 
 
 918
 919	memset(&msg, 0, sizeof(msg));
 920	msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
 921	clear_bit(CF_RECV_INTR, &con->flags);
 922again:
 923	ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len,
 924			     msg.msg_flags);
 925	trace_dlm_recv(con->nodeid, ret);
 926	if (ret == -EAGAIN) {
 927		lock_sock(con->sock->sk);
 928		if (test_and_clear_bit(CF_RECV_INTR, &con->flags)) {
 929			release_sock(con->sock->sk);
 930			goto again;
 
 
 931		}
 932
 933		clear_bit(CF_RECV_PENDING, &con->flags);
 934		release_sock(con->sock->sk);
 935		free_processqueue_entry(pentry);
 936		return DLM_IO_END;
 937	} else if (ret == 0) {
 938		/* close will clear CF_RECV_PENDING */
 939		free_processqueue_entry(pentry);
 940		return DLM_IO_EOF;
 941	} else if (ret < 0) {
 942		free_processqueue_entry(pentry);
 943		return ret;
 944	}
 945
 946	/* new buflen according readed bytes and leftover from last receive */
 947	buflen_real = ret + con->rx_leftover;
 948	ret = dlm_validate_incoming_buffer(con->nodeid, pentry->buf,
 949					   buflen_real);
 950	if (ret < 0) {
 951		free_processqueue_entry(pentry);
 952		return ret;
 953	}
 954
 955	pentry->buflen = ret;
 956
 957	/* calculate leftover bytes from process and put it into begin of
 958	 * the receive buffer, so next receive we have the full message
 959	 * at the start address of the receive buffer.
 
 960	 */
 961	con->rx_leftover = buflen_real - ret;
 962	memmove(con->rx_leftover_buf, pentry->buf + ret,
 963		con->rx_leftover);
 964
 965	spin_lock_bh(&processqueue_lock);
 966	ret = atomic_inc_return(&processqueue_count);
 967	list_add_tail(&pentry->list, &processqueue);
 968	if (!process_dlm_messages_pending) {
 969		process_dlm_messages_pending = true;
 970		queue_work(process_workqueue, &process_work);
 971	}
 972	spin_unlock_bh(&processqueue_lock);
 973
 974	if (ret > DLM_MAX_PROCESS_BUFFERS)
 975		return DLM_IO_FLUSH;
 
 
 
 976
 977	return DLM_IO_SUCCESS;
 
 
 978}
 979
 980/* Listening socket is busy, accept a connection */
 981static int accept_from_sock(void)
 982{
 983	struct sockaddr_storage peeraddr;
 984	int len, idx, result, nodeid;
 
 
 
 985	struct connection *newcon;
 
 986	struct socket *newsock;
 987	unsigned int mark;
 988
 989	result = kernel_accept(listen_con.sock, &newsock, O_NONBLOCK);
 990	if (result == -EAGAIN)
 991		return DLM_IO_END;
 992	else if (result < 0)
 993		goto accept_err;
 994
 995	/* Get the connected socket's peer */
 996	memset(&peeraddr, 0, sizeof(peeraddr));
 997	len = newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr, 2);
 998	if (len < 0) {
 999		result = -ECONNABORTED;
1000		goto accept_err;
1001	}
 
1002
1003	/* Get the new node's NODEID */
1004	make_sockaddr(&peeraddr, 0, &len);
1005	if (addr_to_nodeid(&peeraddr, &nodeid, &mark)) {
1006		switch (peeraddr.ss_family) {
1007		case AF_INET: {
1008			struct sockaddr_in *sin = (struct sockaddr_in *)&peeraddr;
1009
1010			log_print("connect from non cluster IPv4 node %pI4",
1011				  &sin->sin_addr);
1012			break;
1013		}
1014#if IS_ENABLED(CONFIG_IPV6)
1015		case AF_INET6: {
1016			struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&peeraddr;
1017
1018			log_print("connect from non cluster IPv6 node %pI6c",
1019				  &sin6->sin6_addr);
1020			break;
1021		}
1022#endif
1023		default:
1024			log_print("invalid family from non cluster node");
1025			break;
1026		}
1027
1028		sock_release(newsock);
1029		return -1;
 
 
 
1030	}
1031
1032	log_print("got connection from %d", nodeid);
 
 
 
 
 
 
 
 
1033
1034	/*  Check to see if we already have a connection to this node. This
1035	 *  could happen if the two nodes initiate a connection at roughly
1036	 *  the same time and the connections cross on the wire.
1037	 *  In this case we store the incoming one in "othercon"
1038	 */
1039	idx = srcu_read_lock(&connections_srcu);
1040	newcon = nodeid2con(nodeid, 0);
1041	if (WARN_ON_ONCE(!newcon)) {
1042		srcu_read_unlock(&connections_srcu, idx);
1043		result = -ENOENT;
1044		goto accept_err;
1045	}
1046
1047	sock_set_mark(newsock->sk, mark);
1048
1049	down_write(&newcon->sock_lock);
1050	if (newcon->sock) {
1051		struct connection *othercon = newcon->othercon;
1052
1053		if (!othercon) {
1054			othercon = kzalloc(sizeof(*othercon), GFP_NOFS);
1055			if (!othercon) {
1056				log_print("failed to allocate incoming socket");
1057				up_write(&newcon->sock_lock);
1058				srcu_read_unlock(&connections_srcu, idx);
1059				result = -ENOMEM;
1060				goto accept_err;
1061			}
1062
1063			dlm_con_init(othercon, nodeid);
1064			lockdep_set_subclass(&othercon->sock_lock, 1);
1065			newcon->othercon = othercon;
 
1066			set_bit(CF_IS_OTHERCON, &othercon->flags);
 
 
 
 
 
 
 
1067		} else {
1068			/* close other sock con if we have something new */
1069			close_connection(othercon, false);
 
 
1070		}
 
 
 
 
 
 
1071
1072		down_write(&othercon->sock_lock);
1073		add_sock(newsock, othercon);
1074
1075		/* check if we receved something while adding */
1076		lock_sock(othercon->sock->sk);
1077		lowcomms_queue_rwork(othercon);
1078		release_sock(othercon->sock->sk);
1079		up_write(&othercon->sock_lock);
1080	}
1081	else {
1082		/* accept copies the sk after we've saved the callbacks, so we
1083		   don't want to save them a second time or comm errors will
1084		   result in calling sk_error_report recursively. */
1085		add_sock(newsock, newcon);
1086
1087		/* check if we receved something while adding */
1088		lock_sock(newcon->sock->sk);
1089		lowcomms_queue_rwork(newcon);
1090		release_sock(newcon->sock->sk);
1091	}
1092	up_write(&newcon->sock_lock);
1093	srcu_read_unlock(&connections_srcu, idx);
 
1094
1095	return DLM_IO_SUCCESS;
1096
1097accept_err:
 
1098	if (newsock)
1099		sock_release(newsock);
 
 
1100
1101	return result;
 
 
 
 
 
 
1102}
1103
1104/*
1105 * writequeue_entry_complete - try to delete and free write queue entry
1106 * @e: write queue entry to try to delete
1107 * @completed: bytes completed
1108 *
1109 * writequeue_lock must be held.
1110 */
1111static void writequeue_entry_complete(struct writequeue_entry *e, int completed)
1112{
1113	e->offset += completed;
1114	e->len -= completed;
1115	/* signal that page was half way transmitted */
1116	e->dirty = true;
1117
1118	if (e->len == 0 && e->users == 0)
 
1119		free_entry(e);
 
1120}
1121
1122/*
1123 * sctp_bind_addrs - bind a SCTP socket to all our addresses
1124 */
1125static int sctp_bind_addrs(struct socket *sock, __be16 port)
1126{
1127	struct sockaddr_storage localaddr;
1128	struct sockaddr *addr = (struct sockaddr *)&localaddr;
1129	int i, addr_len, result = 0;
1130
1131	for (i = 0; i < dlm_local_count; i++) {
1132		memcpy(&localaddr, &dlm_local_addr[i], sizeof(localaddr));
1133		make_sockaddr(&localaddr, port, &addr_len);
1134
1135		if (!i)
1136			result = kernel_bind(sock, addr, addr_len);
 
 
1137		else
1138			result = sock_bind_add(sock->sk, addr, addr_len);
 
 
1139
1140		if (result < 0) {
1141			log_print("Can't bind to %d addr number %d, %d.\n",
1142				  port, i + 1, result);
1143			break;
1144		}
1145	}
1146	return result;
1147}
1148
1149/* Get local addresses */
1150static void init_local(void)
 
 
 
 
1151{
1152	struct sockaddr_storage sas;
1153	int i;
1154
1155	dlm_local_count = 0;
1156	for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
1157		if (dlm_our_addr(&sas, i))
1158			break;
1159
1160		memcpy(&dlm_local_addr[dlm_local_count++], &sas, sizeof(sas));
 
 
1161	}
1162}
1163
1164static struct writequeue_entry *new_writequeue_entry(struct connection *con)
1165{
1166	struct writequeue_entry *entry;
1167
1168	entry = dlm_allocate_writequeue();
1169	if (!entry)
1170		return NULL;
1171
1172	entry->page = alloc_page(GFP_ATOMIC | __GFP_ZERO);
1173	if (!entry->page) {
1174		dlm_free_writequeue(entry);
1175		return NULL;
1176	}
1177
1178	entry->offset = 0;
1179	entry->len = 0;
1180	entry->end = 0;
1181	entry->dirty = false;
1182	entry->con = con;
1183	entry->users = 1;
1184	kref_init(&entry->ref);
1185	return entry;
1186}
1187
1188static struct writequeue_entry *new_wq_entry(struct connection *con, int len,
1189					     char **ppc, void (*cb)(void *data),
1190					     void *data)
1191{
1192	struct writequeue_entry *e;
1193
1194	spin_lock_bh(&con->writequeue_lock);
1195	if (!list_empty(&con->writequeue)) {
1196		e = list_last_entry(&con->writequeue, struct writequeue_entry, list);
1197		if (DLM_WQ_REMAIN_BYTES(e) >= len) {
1198			kref_get(&e->ref);
1199
1200			*ppc = page_address(e->page) + e->end;
1201			if (cb)
1202				cb(data);
1203
1204			e->end += len;
1205			e->users++;
1206			goto out;
1207		}
1208	}
1209
1210	e = new_writequeue_entry(con);
1211	if (!e)
1212		goto out;
1213
1214	kref_get(&e->ref);
1215	*ppc = page_address(e->page);
1216	e->end += len;
1217	if (cb)
1218		cb(data);
1219
1220	list_add_tail(&e->list, &con->writequeue);
1221
1222out:
1223	spin_unlock_bh(&con->writequeue_lock);
1224	return e;
1225};
1226
1227static struct dlm_msg *dlm_lowcomms_new_msg_con(struct connection *con, int len,
1228						char **ppc, void (*cb)(void *data),
1229						void *data)
1230{
1231	struct writequeue_entry *e;
1232	struct dlm_msg *msg;
1233
1234	msg = dlm_allocate_msg();
1235	if (!msg)
1236		return NULL;
1237
1238	kref_init(&msg->ref);
 
 
1239
1240	e = new_wq_entry(con, len, ppc, cb, data);
1241	if (!e) {
1242		dlm_free_msg(msg);
1243		return NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
1244	}
1245
1246	msg->retransmit = false;
1247	msg->orig_msg = NULL;
1248	msg->ppc = *ppc;
1249	msg->len = len;
1250	msg->entry = e;
1251
1252	return msg;
1253}
1254
1255/* avoid false positive for nodes_srcu, unlock happens in
1256 * dlm_lowcomms_commit_msg which is a must call if success
1257 */
1258#ifndef __CHECKER__
1259struct dlm_msg *dlm_lowcomms_new_msg(int nodeid, int len, char **ppc,
1260				     void (*cb)(void *data), void *data)
1261{
1262	struct connection *con;
1263	struct dlm_msg *msg;
1264	int idx;
 
 
1265
1266	if (len > DLM_MAX_SOCKET_BUFSIZE ||
1267	    len < sizeof(struct dlm_header)) {
1268		BUILD_BUG_ON(PAGE_SIZE < DLM_MAX_SOCKET_BUFSIZE);
1269		log_print("failed to allocate a buffer of size %d", len);
1270		WARN_ON_ONCE(1);
1271		return NULL;
1272	}
1273
1274	idx = srcu_read_lock(&connections_srcu);
1275	con = nodeid2con(nodeid, 0);
1276	if (WARN_ON_ONCE(!con)) {
1277		srcu_read_unlock(&connections_srcu, idx);
1278		return NULL;
1279	}
1280
1281	msg = dlm_lowcomms_new_msg_con(con, len, ppc, cb, data);
1282	if (!msg) {
1283		srcu_read_unlock(&connections_srcu, idx);
1284		return NULL;
 
 
 
 
 
 
 
 
 
 
 
1285	}
1286
1287	/* for dlm_lowcomms_commit_msg() */
1288	kref_get(&msg->ref);
1289	/* we assume if successful commit must called */
1290	msg->idx = idx;
1291	return msg;
1292}
1293#endif
1294
1295static void _dlm_lowcomms_commit_msg(struct dlm_msg *msg)
1296{
1297	struct writequeue_entry *e = msg->entry;
1298	struct connection *con = e->con;
1299	int users;
 
 
 
 
 
1300
1301	spin_lock_bh(&con->writequeue_lock);
1302	kref_get(&msg->ref);
1303	list_add(&msg->list, &e->msgs);
1304
1305	users = --e->users;
1306	if (users)
1307		goto out;
1308
1309	e->len = DLM_WQ_LENGTH_BYTES(e);
 
 
1310
1311	lowcomms_queue_swork(con);
 
 
 
 
 
1312
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1313out:
1314	spin_unlock_bh(&con->writequeue_lock);
 
1315	return;
1316}
1317
1318/* avoid false positive for nodes_srcu, lock was happen in
1319 * dlm_lowcomms_new_msg
1320 */
1321#ifndef __CHECKER__
1322void dlm_lowcomms_commit_msg(struct dlm_msg *msg)
1323{
1324	_dlm_lowcomms_commit_msg(msg);
1325	srcu_read_unlock(&connections_srcu, msg->idx);
1326	/* because dlm_lowcomms_new_msg() */
1327	kref_put(&msg->ref, dlm_msg_release);
1328}
1329#endif
1330
1331void dlm_lowcomms_put_msg(struct dlm_msg *msg)
1332{
1333	kref_put(&msg->ref, dlm_msg_release);
1334}
1335
1336/* does not held connections_srcu, usage lowcomms_error_report only */
1337int dlm_lowcomms_resend_msg(struct dlm_msg *msg)
1338{
1339	struct dlm_msg *msg_resend;
1340	char *ppc;
 
 
1341
1342	if (msg->retransmit)
1343		return 1;
 
 
1344
1345	msg_resend = dlm_lowcomms_new_msg_con(msg->entry->con, msg->len, &ppc,
1346					      NULL, NULL);
1347	if (!msg_resend)
1348		return -ENOMEM;
 
 
 
1349
1350	msg->retransmit = true;
1351	kref_get(&msg->ref);
1352	msg_resend->orig_msg = msg;
1353
1354	memcpy(ppc, msg->ppc, msg->len);
1355	_dlm_lowcomms_commit_msg(msg_resend);
1356	dlm_lowcomms_put_msg(msg_resend);
1357
1358	return 0;
1359}
1360
1361/* Send a message */
1362static int send_to_sock(struct connection *con)
1363{
1364	struct writequeue_entry *e;
1365	struct bio_vec bvec;
1366	struct msghdr msg = {
1367		.msg_flags = MSG_SPLICE_PAGES | MSG_DONTWAIT | MSG_NOSIGNAL,
1368	};
1369	int len, offset, ret;
1370
1371	spin_lock_bh(&con->writequeue_lock);
1372	e = con_next_wq(con);
1373	if (!e) {
1374		clear_bit(CF_SEND_PENDING, &con->flags);
1375		spin_unlock_bh(&con->writequeue_lock);
1376		return DLM_IO_END;
1377	}
1378
1379	len = e->len;
1380	offset = e->offset;
1381	WARN_ON_ONCE(len == 0 && e->users == 0);
1382	spin_unlock_bh(&con->writequeue_lock);
1383
1384	bvec_set_page(&bvec, e->page, len, offset);
1385	iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, len);
1386	ret = sock_sendmsg(con->sock, &msg);
1387	trace_dlm_send(con->nodeid, ret);
1388	if (ret == -EAGAIN || ret == 0) {
1389		lock_sock(con->sock->sk);
1390		spin_lock_bh(&con->writequeue_lock);
1391		if (test_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags) &&
1392		    !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
1393			/* Notify TCP that we're limited by the
1394			 * application window size.
1395			 */
1396			set_bit(SOCK_NOSPACE, &con->sock->sk->sk_socket->flags);
1397			con->sock->sk->sk_write_pending++;
1398
1399			clear_bit(CF_SEND_PENDING, &con->flags);
1400			spin_unlock_bh(&con->writequeue_lock);
1401			release_sock(con->sock->sk);
1402
1403			/* wait for write_space() event */
1404			return DLM_IO_END;
1405		}
1406		spin_unlock_bh(&con->writequeue_lock);
1407		release_sock(con->sock->sk);
1408
1409		return DLM_IO_RESCHED;
1410	} else if (ret < 0) {
1411		return ret;
 
 
 
 
 
 
 
 
 
 
 
1412	}
1413
1414	spin_lock_bh(&con->writequeue_lock);
1415	writequeue_entry_complete(e, ret);
1416	spin_unlock_bh(&con->writequeue_lock);
 
 
 
 
1417
1418	return DLM_IO_SUCCESS;
 
1419}
1420
1421static void clean_one_writequeue(struct connection *con)
 
1422{
1423	struct writequeue_entry *e, *safe;
 
1424
1425	spin_lock_bh(&con->writequeue_lock);
1426	list_for_each_entry_safe(e, safe, &con->writequeue, list) {
1427		free_entry(e);
 
 
 
 
 
 
 
1428	}
1429	spin_unlock_bh(&con->writequeue_lock);
1430}
1431
1432static void connection_release(struct rcu_head *rcu)
 
1433{
1434	struct connection *con = container_of(rcu, struct connection, rcu);
1435
1436	WARN_ON_ONCE(!list_empty(&con->writequeue));
1437	WARN_ON_ONCE(con->sock);
1438	kfree(con);
1439}
1440
1441/* Called from recovery when it knows that a node has
1442   left the cluster */
1443int dlm_lowcomms_close(int nodeid)
1444{
1445	struct connection *con;
1446	int idx;
1447
1448	log_print("closing connection to node %d", nodeid);
1449
1450	idx = srcu_read_lock(&connections_srcu);
1451	con = nodeid2con(nodeid, 0);
1452	if (WARN_ON_ONCE(!con)) {
1453		srcu_read_unlock(&connections_srcu, idx);
1454		return -ENOENT;
1455	}
1456
1457	stop_connection_io(con);
1458	log_print("io handling for node: %d stopped", nodeid);
1459	close_connection(con, true);
1460
1461	spin_lock(&connections_lock);
1462	hlist_del_rcu(&con->list);
1463	spin_unlock(&connections_lock);
1464
1465	clean_one_writequeue(con);
1466	call_srcu(&connections_srcu, &con->rcu, connection_release);
1467	if (con->othercon) {
1468		clean_one_writequeue(con->othercon);
1469		call_srcu(&connections_srcu, &con->othercon->rcu, connection_release);
1470	}
1471	srcu_read_unlock(&connections_srcu, idx);
1472
1473	/* for debugging we print when we are done to compare with other
1474	 * messages in between. This function need to be correctly synchronized
1475	 * with io handling
1476	 */
1477	log_print("closing connection to node %d done", nodeid);
1478
1479	return 0;
1480}
1481
1482/* Receive worker function */
1483static void process_recv_sockets(struct work_struct *work)
1484{
1485	struct connection *con = container_of(work, struct connection, rwork);
1486	int ret, buflen;
1487
1488	down_read(&con->sock_lock);
1489	if (!con->sock) {
1490		up_read(&con->sock_lock);
1491		return;
1492	}
 
 
1493
1494	buflen = READ_ONCE(dlm_config.ci_buffer_size);
1495	do {
1496		ret = receive_from_sock(con, buflen);
1497	} while (ret == DLM_IO_SUCCESS);
1498	up_read(&con->sock_lock);
1499
1500	switch (ret) {
1501	case DLM_IO_END:
1502		/* CF_RECV_PENDING cleared */
1503		break;
1504	case DLM_IO_EOF:
1505		close_connection(con, false);
1506		wake_up(&con->shutdown_wait);
1507		/* CF_RECV_PENDING cleared */
1508		break;
1509	case DLM_IO_FLUSH:
1510		/* we can't flush the process_workqueue here because a
1511		 * WQ_MEM_RECLAIM workequeue can occurr a deadlock for a non
1512		 * WQ_MEM_RECLAIM workqueue such as process_workqueue. Instead
1513		 * we have a waitqueue to wait until all messages are
1514		 * processed.
1515		 *
1516		 * This handling is only necessary to backoff the sender and
1517		 * not queue all messages from the socket layer into DLM
1518		 * processqueue. When DLM is capable to parse multiple messages
1519		 * on an e.g. per socket basis this handling can might be
1520		 * removed. Especially in a message burst we are too slow to
1521		 * process messages and the queue will fill up memory.
1522		 */
1523		wait_event(processqueue_wq, !atomic_read(&processqueue_count));
1524		fallthrough;
1525	case DLM_IO_RESCHED:
1526		cond_resched();
1527		queue_work(io_workqueue, &con->rwork);
1528		/* CF_RECV_PENDING not cleared */
1529		break;
1530	default:
1531		if (ret < 0) {
1532			if (test_bit(CF_IS_OTHERCON, &con->flags)) {
1533				close_connection(con, false);
1534			} else {
1535				spin_lock_bh(&con->writequeue_lock);
1536				lowcomms_queue_swork(con);
1537				spin_unlock_bh(&con->writequeue_lock);
1538			}
1539
1540			/* CF_RECV_PENDING cleared for othercon
1541			 * we trigger send queue if not already done
1542			 * and process_send_sockets will handle it
1543			 */
1544			break;
1545		}
1546
1547		WARN_ON_ONCE(1);
1548		break;
 
 
1549	}
1550}
1551
1552static void process_listen_recv_socket(struct work_struct *work)
1553{
1554	int ret;
1555
1556	if (WARN_ON_ONCE(!listen_con.sock))
1557		return;
1558
1559	do {
1560		ret = accept_from_sock();
1561	} while (ret == DLM_IO_SUCCESS);
1562
1563	if (ret < 0)
1564		log_print("critical error accepting connection: %d", ret);
 
 
 
1565}
1566
1567static int dlm_connect(struct connection *con)
1568{
1569	struct sockaddr_storage addr;
1570	int result, addr_len;
1571	struct socket *sock;
1572	unsigned int mark;
1573
1574	memset(&addr, 0, sizeof(addr));
1575	result = nodeid_to_addr(con->nodeid, &addr, NULL,
1576				dlm_proto_ops->try_new_addr, &mark);
1577	if (result < 0) {
1578		log_print("no address for nodeid %d", con->nodeid);
1579		return result;
1580	}
1581
1582	/* Create a socket to communicate with */
1583	result = sock_create_kern(&init_net, dlm_local_addr[0].ss_family,
1584				  SOCK_STREAM, dlm_proto_ops->proto, &sock);
1585	if (result < 0)
1586		return result;
1587
1588	sock_set_mark(sock->sk, mark);
1589	dlm_proto_ops->sockopts(sock);
1590
1591	result = dlm_proto_ops->bind(sock);
1592	if (result < 0) {
1593		sock_release(sock);
1594		return result;
 
1595	}
1596
1597	add_sock(sock, con);
1598
1599	log_print_ratelimited("connecting to %d", con->nodeid);
1600	make_sockaddr(&addr, dlm_config.ci_tcp_port, &addr_len);
1601	result = kernel_connect(sock, (struct sockaddr *)&addr, addr_len, 0);
1602	switch (result) {
1603	case -EINPROGRESS:
1604		/* not an error */
1605		fallthrough;
1606	case 0:
1607		break;
1608	default:
1609		if (result < 0)
1610			dlm_close_sock(&con->sock);
1611
1612		break;
 
 
 
 
 
 
1613	}
1614
1615	return result;
1616}
1617
1618/* Send worker function */
1619static void process_send_sockets(struct work_struct *work)
 
 
1620{
1621	struct connection *con = container_of(work, struct connection, swork);
1622	int ret;
1623
1624	WARN_ON_ONCE(test_bit(CF_IS_OTHERCON, &con->flags));
 
 
1625
1626	down_read(&con->sock_lock);
1627	if (!con->sock) {
1628		up_read(&con->sock_lock);
1629		down_write(&con->sock_lock);
1630		if (!con->sock) {
1631			ret = dlm_connect(con);
1632			switch (ret) {
1633			case 0:
1634				break;
1635			default:
1636				/* CF_SEND_PENDING not cleared */
1637				up_write(&con->sock_lock);
1638				log_print("connect to node %d try %d error %d",
1639					  con->nodeid, con->retries++, ret);
1640				msleep(1000);
1641				/* For now we try forever to reconnect. In
1642				 * future we should send a event to cluster
1643				 * manager to fence itself after certain amount
1644				 * of retries.
1645				 */
1646				queue_work(io_workqueue, &con->swork);
1647				return;
1648			}
1649		}
1650		downgrade_write(&con->sock_lock);
1651	}
1652
1653	do {
1654		ret = send_to_sock(con);
1655	} while (ret == DLM_IO_SUCCESS);
1656	up_read(&con->sock_lock);
1657
1658	switch (ret) {
1659	case DLM_IO_END:
1660		/* CF_SEND_PENDING cleared */
1661		break;
1662	case DLM_IO_RESCHED:
1663		/* CF_SEND_PENDING not cleared */
1664		cond_resched();
1665		queue_work(io_workqueue, &con->swork);
1666		break;
1667	default:
1668		if (ret < 0) {
1669			close_connection(con, false);
1670
1671			/* CF_SEND_PENDING cleared */
1672			spin_lock_bh(&con->writequeue_lock);
1673			lowcomms_queue_swork(con);
1674			spin_unlock_bh(&con->writequeue_lock);
1675			break;
1676		}
1677
1678		WARN_ON_ONCE(1);
1679		break;
1680	}
1681}
1682
1683static void work_stop(void)
1684{
1685	if (io_workqueue) {
1686		destroy_workqueue(io_workqueue);
1687		io_workqueue = NULL;
1688	}
 
 
 
1689
1690	if (process_workqueue) {
1691		destroy_workqueue(process_workqueue);
1692		process_workqueue = NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1693	}
 
1694}
1695
1696static int work_start(void)
1697{
1698	io_workqueue = alloc_workqueue("dlm_io", WQ_HIGHPRI | WQ_MEM_RECLAIM |
1699				       WQ_UNBOUND, 0);
1700	if (!io_workqueue) {
1701		log_print("can't start dlm_io");
1702		return -ENOMEM;
1703	}
1704
1705	process_workqueue = alloc_workqueue("dlm_process", WQ_HIGHPRI | WQ_BH, 0);
1706	if (!process_workqueue) {
1707		log_print("can't start dlm_process");
1708		destroy_workqueue(io_workqueue);
1709		io_workqueue = NULL;
1710		return -ENOMEM;
 
 
 
1711	}
 
1712
1713	return 0;
 
 
1714}
1715
1716void dlm_lowcomms_shutdown(void)
 
1717{
1718	struct connection *con;
1719	int i, idx;
 
 
 
1720
1721	/* stop lowcomms_listen_data_ready calls */
1722	lock_sock(listen_con.sock->sk);
1723	listen_con.sock->sk->sk_data_ready = listen_sock.sk_data_ready;
1724	release_sock(listen_con.sock->sk);
1725
1726	cancel_work_sync(&listen_con.rwork);
1727	dlm_close_sock(&listen_con.sock);
 
 
 
 
1728
1729	idx = srcu_read_lock(&connections_srcu);
1730	for (i = 0; i < CONN_HASH_SIZE; i++) {
1731		hlist_for_each_entry_rcu(con, &connection_hash[i], list) {
1732			shutdown_connection(con, true);
1733			stop_connection_io(con);
1734			flush_workqueue(process_workqueue);
1735			close_connection(con, true);
1736
1737			clean_one_writequeue(con);
1738			if (con->othercon)
1739				clean_one_writequeue(con->othercon);
1740			allow_connection_io(con);
 
 
 
 
 
 
 
 
 
 
 
1741		}
1742	}
1743	srcu_read_unlock(&connections_srcu, idx);
1744}
1745
1746void dlm_lowcomms_stop(void)
1747{
1748	work_stop();
1749	dlm_proto_ops = NULL;
1750}
1751
1752static int dlm_listen_for_all(void)
1753{
1754	struct socket *sock;
1755	int result;
1756
1757	log_print("Using %s for communications",
1758		  dlm_proto_ops->name);
1759
1760	result = dlm_proto_ops->listen_validate();
1761	if (result < 0)
1762		return result;
1763
1764	result = sock_create_kern(&init_net, dlm_local_addr[0].ss_family,
1765				  SOCK_STREAM, dlm_proto_ops->proto, &sock);
1766	if (result < 0) {
1767		log_print("Can't create comms socket: %d", result);
1768		return result;
1769	}
 
 
 
 
1770
1771	sock_set_mark(sock->sk, dlm_config.ci_mark);
1772	dlm_proto_ops->listen_sockopts(sock);
 
 
 
1773
1774	result = dlm_proto_ops->listen_bind(sock);
1775	if (result < 0)
1776		goto out;
 
1777
1778	lock_sock(sock->sk);
1779	listen_sock.sk_data_ready = sock->sk->sk_data_ready;
1780	listen_sock.sk_write_space = sock->sk->sk_write_space;
1781	listen_sock.sk_error_report = sock->sk->sk_error_report;
1782	listen_sock.sk_state_change = sock->sk->sk_state_change;
1783
1784	listen_con.sock = sock;
1785
1786	sock->sk->sk_allocation = GFP_NOFS;
1787	sock->sk->sk_use_task_frag = false;
1788	sock->sk->sk_data_ready = lowcomms_listen_data_ready;
1789	release_sock(sock->sk);
1790
1791	result = sock->ops->listen(sock, 128);
1792	if (result < 0) {
1793		dlm_close_sock(&listen_con.sock);
1794		return result;
1795	}
1796
1797	return 0;
1798
1799out:
1800	sock_release(sock);
1801	return result;
1802}
1803
1804static int dlm_tcp_bind(struct socket *sock)
 
 
1805{
1806	struct sockaddr_storage src_addr;
1807	int result, addr_len;
1808
1809	/* Bind to our cluster-known address connecting to avoid
1810	 * routing problems.
1811	 */
1812	memcpy(&src_addr, &dlm_local_addr[0], sizeof(src_addr));
1813	make_sockaddr(&src_addr, 0, &addr_len);
 
 
1814
1815	result = kernel_bind(sock, (struct sockaddr *)&src_addr,
1816			     addr_len);
1817	if (result < 0) {
1818		/* This *may* not indicate a critical error */
1819		log_print("could not bind for connect: %d", result);
 
 
1820	}
 
1821
1822	return 0;
1823}
1824
1825static int dlm_tcp_listen_validate(void)
 
1826{
1827	/* We don't support multi-homed hosts */
1828	if (dlm_local_count > 1) {
1829		log_print("TCP protocol can't handle multi-homed hosts, try SCTP");
1830		return -EINVAL;
1831	}
1832
1833	return 0;
 
 
 
1834}
1835
1836static void dlm_tcp_sockopts(struct socket *sock)
 
1837{
1838	/* Turn off Nagle's algorithm */
1839	tcp_sock_set_nodelay(sock->sk);
1840}
1841
1842static void dlm_tcp_listen_sockopts(struct socket *sock)
1843{
1844	dlm_tcp_sockopts(sock);
1845	sock_set_reuseaddr(sock->sk);
1846}
1847
1848static int dlm_tcp_listen_bind(struct socket *sock)
1849{
1850	int addr_len;
1851
1852	/* Bind to our port */
1853	make_sockaddr(&dlm_local_addr[0], dlm_config.ci_tcp_port, &addr_len);
1854	return kernel_bind(sock, (struct sockaddr *)&dlm_local_addr[0],
1855			   addr_len);
1856}
1857
1858static const struct dlm_proto_ops dlm_tcp_ops = {
1859	.name = "TCP",
1860	.proto = IPPROTO_TCP,
1861	.sockopts = dlm_tcp_sockopts,
1862	.bind = dlm_tcp_bind,
1863	.listen_validate = dlm_tcp_listen_validate,
1864	.listen_sockopts = dlm_tcp_listen_sockopts,
1865	.listen_bind = dlm_tcp_listen_bind,
1866};
1867
1868static int dlm_sctp_bind(struct socket *sock)
1869{
1870	return sctp_bind_addrs(sock, 0);
 
1871}
1872
1873static int dlm_sctp_listen_validate(void)
1874{
1875	if (!IS_ENABLED(CONFIG_IP_SCTP)) {
1876		log_print("SCTP is not enabled by this kernel");
1877		return -EOPNOTSUPP;
 
 
 
 
 
 
 
 
 
 
1878	}
1879
1880	request_module("sctp");
1881	return 0;
1882}
1883
1884static int dlm_sctp_bind_listen(struct socket *sock)
1885{
1886	return sctp_bind_addrs(sock, dlm_config.ci_tcp_port);
 
 
1887}
1888
1889static void dlm_sctp_sockopts(struct socket *sock)
1890{
1891	/* Turn off Nagle's algorithm */
1892	sctp_sock_set_nodelay(sock->sk);
1893	sock_set_rcvbuf(sock->sk, NEEDED_RMEM);
 
 
1894}
1895
1896static const struct dlm_proto_ops dlm_sctp_ops = {
1897	.name = "SCTP",
1898	.proto = IPPROTO_SCTP,
1899	.try_new_addr = true,
1900	.sockopts = dlm_sctp_sockopts,
1901	.bind = dlm_sctp_bind,
1902	.listen_validate = dlm_sctp_listen_validate,
1903	.listen_sockopts = dlm_sctp_sockopts,
1904	.listen_bind = dlm_sctp_bind_listen,
1905};
 
 
 
 
 
 
 
 
 
 
1906
1907int dlm_lowcomms_start(void)
1908{
1909	int error;
 
 
 
 
 
1910
1911	init_local();
1912	if (!dlm_local_count) {
1913		error = -ENOTCONN;
1914		log_print("no local IP address has been set");
1915		goto fail;
1916	}
1917
 
 
 
 
 
 
 
1918	error = work_start();
1919	if (error)
1920		goto fail;
1921
1922	/* Start listening */
1923	switch (dlm_config.ci_protocol) {
1924	case DLM_PROTO_TCP:
1925		dlm_proto_ops = &dlm_tcp_ops;
1926		break;
1927	case DLM_PROTO_SCTP:
1928		dlm_proto_ops = &dlm_sctp_ops;
1929		break;
1930	default:
1931		log_print("Invalid protocol identifier %d set",
1932			  dlm_config.ci_protocol);
1933		error = -EINVAL;
1934		goto fail_proto_ops;
1935	}
1936
1937	error = dlm_listen_for_all();
 
 
 
 
1938	if (error)
1939		goto fail_listen;
1940
1941	return 0;
1942
1943fail_listen:
1944	dlm_proto_ops = NULL;
1945fail_proto_ops:
1946	work_stop();
 
 
 
 
 
1947fail:
1948	return error;
1949}
1950
1951void dlm_lowcomms_init(void)
1952{
1953	int i;
1954
1955	for (i = 0; i < CONN_HASH_SIZE; i++)
1956		INIT_HLIST_HEAD(&connection_hash[i]);
1957
1958	INIT_WORK(&listen_con.rwork, process_listen_recv_socket);
1959}
1960
1961void dlm_lowcomms_exit(void)
1962{
1963	struct connection *con;
1964	int i, idx;
1965
1966	idx = srcu_read_lock(&connections_srcu);
1967	for (i = 0; i < CONN_HASH_SIZE; i++) {
1968		hlist_for_each_entry_rcu(con, &connection_hash[i], list) {
1969			spin_lock(&connections_lock);
1970			hlist_del_rcu(&con->list);
1971			spin_unlock(&connections_lock);
1972
1973			if (con->othercon)
1974				call_srcu(&connections_srcu, &con->othercon->rcu,
1975					  connection_release);
1976			call_srcu(&connections_srcu, &con->rcu, connection_release);
1977		}
1978	}
1979	srcu_read_unlock(&connections_srcu, idx);
1980}
v4.6
 
   1/******************************************************************************
   2*******************************************************************************
   3**
   4**  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
   5**  Copyright (C) 2004-2009 Red Hat, Inc.  All rights reserved.
   6**
   7**  This copyrighted material is made available to anyone wishing to use,
   8**  modify, copy, or redistribute it subject to the terms and conditions
   9**  of the GNU General Public License v.2.
  10**
  11*******************************************************************************
  12******************************************************************************/
  13
  14/*
  15 * lowcomms.c
  16 *
  17 * This is the "low-level" comms layer.
  18 *
  19 * It is responsible for sending/receiving messages
  20 * from other nodes in the cluster.
  21 *
  22 * Cluster nodes are referred to by their nodeids. nodeids are
  23 * simply 32 bit numbers to the locking module - if they need to
  24 * be expanded for the cluster infrastructure then that is its
  25 * responsibility. It is this layer's
  26 * responsibility to resolve these into IP address or
  27 * whatever it needs for inter-node communication.
  28 *
  29 * The comms level is two kernel threads that deal mainly with
  30 * the receiving of messages from other nodes and passing them
  31 * up to the mid-level comms layer (which understands the
  32 * message format) for execution by the locking core, and
  33 * a send thread which does all the setting up of connections
  34 * to remote nodes and the sending of data. Threads are not allowed
  35 * to send their own data because it may cause them to wait in times
  36 * of high load. Also, this way, the sending thread can collect together
  37 * messages bound for one node and send them in one block.
  38 *
  39 * lowcomms will choose to use either TCP or SCTP as its transport layer
  40 * depending on the configuration variable 'protocol'. This should be set
  41 * to 0 (default) for TCP or 1 for SCTP. It should be configured using a
  42 * cluster-wide mechanism as it must be the same on all nodes of the cluster
  43 * for the DLM to function.
  44 *
  45 */
  46
  47#include <asm/ioctls.h>
  48#include <net/sock.h>
  49#include <net/tcp.h>
  50#include <linux/pagemap.h>
  51#include <linux/file.h>
  52#include <linux/mutex.h>
  53#include <linux/sctp.h>
  54#include <linux/slab.h>
  55#include <net/sctp/sctp.h>
  56#include <net/ipv6.h>
  57
 
 
 
  58#include "dlm_internal.h"
  59#include "lowcomms.h"
  60#include "midcomms.h"
 
  61#include "config.h"
  62
 
 
  63#define NEEDED_RMEM (4*1024*1024)
  64#define CONN_HASH_SIZE 32
  65
  66/* Number of messages to send before rescheduling */
  67#define MAX_SEND_MSG_COUNT 25
  68
  69struct cbuf {
  70	unsigned int base;
  71	unsigned int len;
  72	unsigned int mask;
  73};
  74
  75static void cbuf_add(struct cbuf *cb, int n)
  76{
  77	cb->len += n;
  78}
  79
  80static int cbuf_data(struct cbuf *cb)
  81{
  82	return ((cb->base + cb->len) & cb->mask);
  83}
  84
  85static void cbuf_init(struct cbuf *cb, int size)
  86{
  87	cb->base = cb->len = 0;
  88	cb->mask = size-1;
  89}
  90
  91static void cbuf_eat(struct cbuf *cb, int n)
  92{
  93	cb->len  -= n;
  94	cb->base += n;
  95	cb->base &= cb->mask;
  96}
  97
  98static bool cbuf_empty(struct cbuf *cb)
  99{
 100	return cb->len == 0;
 101}
 102
 103struct connection {
 104	struct socket *sock;	/* NULL if not connected */
 105	uint32_t nodeid;	/* So we know who we are in the list */
 106	struct mutex sock_mutex;
 
 
 
 
 
 
 
 107	unsigned long flags;
 108#define CF_READ_PENDING 1
 109#define CF_WRITE_PENDING 2
 110#define CF_CONNECT_PENDING 3
 111#define CF_INIT_PENDING 4
 
 112#define CF_IS_OTHERCON 5
 113#define CF_CLOSE 6
 114#define CF_APP_LIMITED 7
 115	struct list_head writequeue;  /* List of outgoing writequeue_entries */
 116	spinlock_t writequeue_lock;
 117	int (*rx_action) (struct connection *);	/* What to do when active */
 118	void (*connect_action) (struct connection *);	/* What to do to connect */
 119	struct page *rx_page;
 120	struct cbuf cb;
 121	int retries;
 122#define MAX_CONNECT_RETRIES 3
 123	struct hlist_node list;
 
 
 
 
 
 
 
 
 
 
 
 124	struct connection *othercon;
 125	struct work_struct rwork; /* Receive workqueue */
 126	struct work_struct swork; /* Send workqueue */
 127	void (*orig_error_report)(struct sock *);
 128	void (*orig_data_ready)(struct sock *);
 129	void (*orig_state_change)(struct sock *);
 130	void (*orig_write_space)(struct sock *);
 
 
 
 
 
 131};
 132#define sock2con(x) ((struct connection *)(x)->sk_user_data)
 133
 
 
 
 
 
 
 
 
 134/* An entry waiting to be sent */
 135struct writequeue_entry {
 136	struct list_head list;
 137	struct page *page;
 138	int offset;
 139	int len;
 140	int end;
 141	int users;
 
 142	struct connection *con;
 
 
 143};
 144
 145struct dlm_node_addr {
 
 
 
 
 
 
 
 146	struct list_head list;
 
 
 
 
 
 147	int nodeid;
 148	int addr_count;
 149	int curr_addr_index;
 150	struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
 
 
 
 
 
 
 
 
 
 
 
 
 151};
 152
 153static LIST_HEAD(dlm_node_addrs);
 154static DEFINE_SPINLOCK(dlm_node_addrs_spin);
 
 
 
 
 155
 156static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
 
 157static int dlm_local_count;
 158static int dlm_allow_conn;
 159
 160/* Work queues */
 161static struct workqueue_struct *recv_workqueue;
 162static struct workqueue_struct *send_workqueue;
 163
 164static struct hlist_head connection_hash[CONN_HASH_SIZE];
 165static DEFINE_MUTEX(connections_lock);
 166static struct kmem_cache *con_cache;
 
 
 
 
 
 
 
 
 167
 168static void process_recv_sockets(struct work_struct *work);
 169static void process_send_sockets(struct work_struct *work);
 
 170
 
 
 
 
 
 
 171
 172/* This is deliberately very simple because most clusters have simple
 173   sequential nodeids, so we should be able to go straight to a connection
 174   struct in the array */
 175static inline int nodeid_hash(int nodeid)
 176{
 177	return nodeid & (CONN_HASH_SIZE-1);
 178}
 179
 180static struct connection *__find_con(int nodeid)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 181{
 182	int r;
 183	struct connection *con;
 184
 185	r = nodeid_hash(nodeid);
 186
 187	hlist_for_each_entry(con, &connection_hash[r], list) {
 188		if (con->nodeid == nodeid)
 189			return con;
 190	}
 
 191	return NULL;
 192}
 193
 
 
 
 
 
 
 
 
 
 
 
 
 194/*
 195 * If 'allocation' is zero then we don't attempt to create a new
 196 * connection structure for this node.
 197 */
 198static struct connection *__nodeid2con(int nodeid, gfp_t alloc)
 199{
 200	struct connection *con = NULL;
 201	int r;
 202
 203	con = __find_con(nodeid);
 
 204	if (con || !alloc)
 205		return con;
 206
 207	con = kmem_cache_zalloc(con_cache, alloc);
 208	if (!con)
 209		return NULL;
 210
 211	r = nodeid_hash(nodeid);
 212	hlist_add_head(&con->list, &connection_hash[r]);
 213
 214	con->nodeid = nodeid;
 215	mutex_init(&con->sock_mutex);
 216	INIT_LIST_HEAD(&con->writequeue);
 217	spin_lock_init(&con->writequeue_lock);
 218	INIT_WORK(&con->swork, process_send_sockets);
 219	INIT_WORK(&con->rwork, process_recv_sockets);
 220
 221	/* Setup action pointers for child sockets */
 222	if (con->nodeid) {
 223		struct connection *zerocon = __find_con(0);
 224
 225		con->connect_action = zerocon->connect_action;
 226		if (!con->rx_action)
 227			con->rx_action = zerocon->rx_action;
 228	}
 229
 230	return con;
 231}
 232
 233/* Loop round all connections */
 234static void foreach_conn(void (*conn_func)(struct connection *c))
 235{
 236	int i;
 237	struct hlist_node *n;
 238	struct connection *con;
 239
 240	for (i = 0; i < CONN_HASH_SIZE; i++) {
 241		hlist_for_each_entry_safe(con, n, &connection_hash[i], list)
 242			conn_func(con);
 243	}
 244}
 245
 246static struct connection *nodeid2con(int nodeid, gfp_t allocation)
 247{
 248	struct connection *con;
 249
 250	mutex_lock(&connections_lock);
 251	con = __nodeid2con(nodeid, allocation);
 252	mutex_unlock(&connections_lock);
 253
 254	return con;
 255}
 256
 257static struct dlm_node_addr *find_node_addr(int nodeid)
 258{
 259	struct dlm_node_addr *na;
 260
 261	list_for_each_entry(na, &dlm_node_addrs, list) {
 262		if (na->nodeid == nodeid)
 263			return na;
 264	}
 265	return NULL;
 266}
 267
 268static int addr_compare(struct sockaddr_storage *x, struct sockaddr_storage *y)
 269{
 270	switch (x->ss_family) {
 271	case AF_INET: {
 272		struct sockaddr_in *sinx = (struct sockaddr_in *)x;
 273		struct sockaddr_in *siny = (struct sockaddr_in *)y;
 274		if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
 275			return 0;
 276		if (sinx->sin_port != siny->sin_port)
 277			return 0;
 278		break;
 279	}
 280	case AF_INET6: {
 281		struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
 282		struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
 283		if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
 284			return 0;
 285		if (sinx->sin6_port != siny->sin6_port)
 286			return 0;
 287		break;
 288	}
 289	default:
 290		return 0;
 291	}
 292	return 1;
 293}
 294
 295static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out,
 296			  struct sockaddr *sa_out, bool try_new_addr)
 
 297{
 298	struct sockaddr_storage sas;
 299	struct dlm_node_addr *na;
 
 300
 301	if (!dlm_local_count)
 302		return -1;
 303
 304	spin_lock(&dlm_node_addrs_spin);
 305	na = find_node_addr(nodeid);
 306	if (na && na->addr_count) {
 307		memcpy(&sas, na->addr[na->curr_addr_index],
 308		       sizeof(struct sockaddr_storage));
 309
 310		if (try_new_addr) {
 311			na->curr_addr_index++;
 312			if (na->curr_addr_index == na->addr_count)
 313				na->curr_addr_index = 0;
 314		}
 
 315	}
 316	spin_unlock(&dlm_node_addrs_spin);
 317
 318	if (!na)
 319		return -EEXIST;
 
 
 
 
 
 
 320
 321	if (!na->addr_count)
 322		return -ENOENT;
 323
 324	if (sas_out)
 325		memcpy(sas_out, &sas, sizeof(struct sockaddr_storage));
 326
 327	if (!sa_out)
 
 328		return 0;
 
 329
 330	if (dlm_local_addr[0]->ss_family == AF_INET) {
 331		struct sockaddr_in *in4  = (struct sockaddr_in *) &sas;
 332		struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out;
 333		ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
 334	} else {
 335		struct sockaddr_in6 *in6  = (struct sockaddr_in6 *) &sas;
 336		struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out;
 337		ret6->sin6_addr = in6->sin6_addr;
 338	}
 339
 
 340	return 0;
 341}
 342
 343static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid)
 
 344{
 345	struct dlm_node_addr *na;
 346	int rv = -EEXIST;
 347	int addr_i;
 348
 349	spin_lock(&dlm_node_addrs_spin);
 350	list_for_each_entry(na, &dlm_node_addrs, list) {
 351		if (!na->addr_count)
 352			continue;
 353
 354		for (addr_i = 0; addr_i < na->addr_count; addr_i++) {
 355			if (addr_compare(na->addr[addr_i], addr)) {
 356				*nodeid = na->nodeid;
 357				rv = 0;
 358				goto unlock;
 
 
 
 359			}
 
 360		}
 361	}
 362unlock:
 363	spin_unlock(&dlm_node_addrs_spin);
 364	return rv;
 365}
 366
 367int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr, int len)
 
 368{
 369	struct sockaddr_storage *new_addr;
 370	struct dlm_node_addr *new_node, *na;
 371
 372	new_node = kzalloc(sizeof(struct dlm_node_addr), GFP_NOFS);
 373	if (!new_node)
 374		return -ENOMEM;
 
 
 
 
 
 
 
 
 
 
 375
 376	new_addr = kzalloc(sizeof(struct sockaddr_storage), GFP_NOFS);
 377	if (!new_addr) {
 378		kfree(new_node);
 
 379		return -ENOMEM;
 380	}
 381
 382	memcpy(new_addr, addr, len);
 
 
 
 
 
 
 
 
 383
 384	spin_lock(&dlm_node_addrs_spin);
 385	na = find_node_addr(nodeid);
 386	if (!na) {
 387		new_node->nodeid = nodeid;
 388		new_node->addr[0] = new_addr;
 389		new_node->addr_count = 1;
 390		list_add(&new_node->list, &dlm_node_addrs);
 391		spin_unlock(&dlm_node_addrs_spin);
 392		return 0;
 393	}
 394
 395	if (na->addr_count >= DLM_MAX_ADDR_COUNT) {
 396		spin_unlock(&dlm_node_addrs_spin);
 397		kfree(new_addr);
 398		kfree(new_node);
 399		return -ENOSPC;
 400	}
 401
 402	na->addr[na->addr_count++] = new_addr;
 403	spin_unlock(&dlm_node_addrs_spin);
 404	kfree(new_node);
 405	return 0;
 406}
 407
 408/* Data available on socket or listen socket received a connect */
 409static void lowcomms_data_ready(struct sock *sk)
 410{
 411	struct connection *con = sock2con(sk);
 412	if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
 413		queue_work(recv_workqueue, &con->rwork);
 
 
 
 414}
 415
 416static void lowcomms_write_space(struct sock *sk)
 417{
 418	struct connection *con = sock2con(sk);
 419
 420	if (!con)
 421		return;
 422
 423	clear_bit(SOCK_NOSPACE, &con->sock->flags);
 424
 
 425	if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) {
 426		con->sock->sk->sk_write_pending--;
 427		clear_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags);
 428	}
 429
 430	if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags))
 431		queue_work(send_workqueue, &con->swork);
 432}
 433
 434static inline void lowcomms_connect_sock(struct connection *con)
 435{
 436	if (test_bit(CF_CLOSE, &con->flags))
 437		return;
 438	if (!test_and_set_bit(CF_CONNECT_PENDING, &con->flags))
 439		queue_work(send_workqueue, &con->swork);
 
 440}
 441
 442static void lowcomms_state_change(struct sock *sk)
 443{
 444	/* SCTP layer is not calling sk_data_ready when the connection
 445	 * is done, so we catch the signal through here. Also, it
 446	 * doesn't switch socket state when entering shutdown, so we
 447	 * skip the write in that case.
 448	 */
 449	if (sk->sk_shutdown) {
 450		if (sk->sk_shutdown == RCV_SHUTDOWN)
 451			lowcomms_data_ready(sk);
 452	} else if (sk->sk_state == TCP_ESTABLISHED) {
 453		lowcomms_write_space(sk);
 454	}
 455}
 456
 457int dlm_lowcomms_connect_node(int nodeid)
 458{
 459	struct connection *con;
 
 
 
 
 
 
 
 
 460
 461	if (nodeid == dlm_our_nodeid())
 462		return 0;
 
 
 
 
 
 
 463
 464	con = nodeid2con(nodeid, GFP_NOFS);
 465	if (!con)
 466		return -ENOMEM;
 467	lowcomms_connect_sock(con);
 468	return 0;
 469}
 470
 471static void lowcomms_error_report(struct sock *sk)
 472{
 473	struct connection *con;
 474	struct sockaddr_storage saddr;
 475	int buflen;
 476	void (*orig_report)(struct sock *) = NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 477
 478	read_lock_bh(&sk->sk_callback_lock);
 479	con = sock2con(sk);
 480	if (con == NULL)
 481		goto out;
 482
 483	orig_report = con->orig_error_report;
 484	if (con->sock == NULL ||
 485	    kernel_getpeername(con->sock, (struct sockaddr *)&saddr, &buflen)) {
 486		printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
 487				   "sending to node %d, port %d, "
 488				   "sk_err=%d/%d\n", dlm_our_nodeid(),
 489				   con->nodeid, dlm_config.ci_tcp_port,
 490				   sk->sk_err, sk->sk_err_soft);
 491	} else if (saddr.ss_family == AF_INET) {
 492		struct sockaddr_in *sin4 = (struct sockaddr_in *)&saddr;
 493
 
 
 
 
 
 
 
 
 
 
 494		printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
 495				   "sending to node %d at %pI4, port %d, "
 496				   "sk_err=%d/%d\n", dlm_our_nodeid(),
 497				   con->nodeid, &sin4->sin_addr.s_addr,
 498				   dlm_config.ci_tcp_port, sk->sk_err,
 499				   sk->sk_err_soft);
 500	} else {
 501		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&saddr;
 
 502
 503		printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
 504				   "sending to node %d at %u.%u.%u.%u, "
 505				   "port %d, sk_err=%d/%d\n", dlm_our_nodeid(),
 506				   con->nodeid, sin6->sin6_addr.s6_addr32[0],
 507				   sin6->sin6_addr.s6_addr32[1],
 508				   sin6->sin6_addr.s6_addr32[2],
 509				   sin6->sin6_addr.s6_addr32[3],
 510				   dlm_config.ci_tcp_port, sk->sk_err,
 511				   sk->sk_err_soft);
 512	}
 513out:
 514	read_unlock_bh(&sk->sk_callback_lock);
 515	if (orig_report)
 516		orig_report(sk);
 517}
 518
 519/* Note: sk_callback_lock must be locked before calling this function. */
 520static void save_callbacks(struct connection *con, struct sock *sk)
 521{
 522	lock_sock(sk);
 523	con->orig_data_ready = sk->sk_data_ready;
 524	con->orig_state_change = sk->sk_state_change;
 525	con->orig_write_space = sk->sk_write_space;
 526	con->orig_error_report = sk->sk_error_report;
 527	release_sock(sk);
 528}
 529
 530static void restore_callbacks(struct connection *con, struct sock *sk)
 531{
 532	write_lock_bh(&sk->sk_callback_lock);
 533	lock_sock(sk);
 534	sk->sk_user_data = NULL;
 535	sk->sk_data_ready = con->orig_data_ready;
 536	sk->sk_state_change = con->orig_state_change;
 537	sk->sk_write_space = con->orig_write_space;
 538	sk->sk_error_report = con->orig_error_report;
 539	release_sock(sk);
 540	write_unlock_bh(&sk->sk_callback_lock);
 541}
 542
 543/* Make a socket active */
 544static void add_sock(struct socket *sock, struct connection *con)
 545{
 546	struct sock *sk = sock->sk;
 547
 548	write_lock_bh(&sk->sk_callback_lock);
 549	con->sock = sock;
 550
 551	sk->sk_user_data = con;
 552	if (!test_bit(CF_IS_OTHERCON, &con->flags))
 553		save_callbacks(con, sk);
 554	/* Install a data_ready callback */
 555	sk->sk_data_ready = lowcomms_data_ready;
 556	sk->sk_write_space = lowcomms_write_space;
 557	sk->sk_state_change = lowcomms_state_change;
 
 558	sk->sk_allocation = GFP_NOFS;
 
 559	sk->sk_error_report = lowcomms_error_report;
 560	write_unlock_bh(&sk->sk_callback_lock);
 561}
 562
 563/* Add the port number to an IPv6 or 4 sockaddr and return the address
 564   length */
 565static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
 566			  int *addr_len)
 567{
 568	saddr->ss_family =  dlm_local_addr[0]->ss_family;
 569	if (saddr->ss_family == AF_INET) {
 570		struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
 571		in4_addr->sin_port = cpu_to_be16(port);
 572		*addr_len = sizeof(struct sockaddr_in);
 573		memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
 574	} else {
 575		struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
 576		in6_addr->sin6_port = cpu_to_be16(port);
 577		*addr_len = sizeof(struct sockaddr_in6);
 578	}
 579	memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
 580}
 581
 582/* Close a remote connection and tidy up */
 583static void close_connection(struct connection *con, bool and_other,
 584			     bool tx, bool rx)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 585{
 586	clear_bit(CF_CONNECT_PENDING, &con->flags);
 587	clear_bit(CF_WRITE_PENDING, &con->flags);
 588	if (tx && cancel_work_sync(&con->swork))
 589		log_print("canceled swork for node %d", con->nodeid);
 590	if (rx && cancel_work_sync(&con->rwork))
 591		log_print("canceled rwork for node %d", con->nodeid);
 592
 593	mutex_lock(&con->sock_mutex);
 
 
 
 
 594	if (con->sock) {
 595		if (!test_bit(CF_IS_OTHERCON, &con->flags))
 596			restore_callbacks(con, con->sock->sk);
 597		sock_release(con->sock);
 598		con->sock = NULL;
 599	}
 600	if (con->othercon && and_other) {
 601		/* Will only re-enter once. */
 602		close_connection(con->othercon, false, true, true);
 603	}
 604	if (con->rx_page) {
 605		__free_page(con->rx_page);
 606		con->rx_page = NULL;
 607	}
 
 608
 609	con->retries = 0;
 610	mutex_unlock(&con->sock_mutex);
 611}
 612
 613/* Data received from remote end */
 614static int receive_from_sock(struct connection *con)
 615{
 616	int ret = 0;
 617	struct msghdr msg = {};
 618	struct kvec iov[2];
 619	unsigned len;
 620	int r;
 621	int call_again_soon = 0;
 622	int nvec;
 623
 624	mutex_lock(&con->sock_mutex);
 
 625
 626	if (con->sock == NULL) {
 627		ret = -EAGAIN;
 628		goto out_close;
 629	}
 630	if (con->nodeid == 0) {
 631		ret = -EINVAL;
 632		goto out_close;
 633	}
 634
 635	if (con->rx_page == NULL) {
 636		/*
 637		 * This doesn't need to be atomic, but I think it should
 638		 * improve performance if it is.
 639		 */
 640		con->rx_page = alloc_page(GFP_ATOMIC);
 641		if (con->rx_page == NULL)
 642			goto out_resched;
 643		cbuf_init(&con->cb, PAGE_SIZE);
 644	}
 645
 646	/*
 647	 * iov[0] is the bit of the circular buffer between the current end
 648	 * point (cb.base + cb.len) and the end of the buffer.
 649	 */
 650	iov[0].iov_len = con->cb.base - cbuf_data(&con->cb);
 651	iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb);
 652	iov[1].iov_len = 0;
 653	nvec = 1;
 654
 655	/*
 656	 * iov[1] is the bit of the circular buffer between the start of the
 657	 * buffer and the start of the currently used section (cb.base)
 658	 */
 659	if (cbuf_data(&con->cb) >= con->cb.base) {
 660		iov[0].iov_len = PAGE_SIZE - cbuf_data(&con->cb);
 661		iov[1].iov_len = con->cb.base;
 662		iov[1].iov_base = page_address(con->rx_page);
 663		nvec = 2;
 664	}
 665	len = iov[0].iov_len + iov[1].iov_len;
 666
 667	r = ret = kernel_recvmsg(con->sock, &msg, iov, nvec, len,
 668			       MSG_DONTWAIT | MSG_NOSIGNAL);
 669	if (ret <= 0)
 670		goto out_close;
 671	else if (ret == len)
 672		call_again_soon = 1;
 673
 674	cbuf_add(&con->cb, ret);
 675	ret = dlm_process_incoming_buffer(con->nodeid,
 676					  page_address(con->rx_page),
 677					  con->cb.base, con->cb.len,
 678					  PAGE_SIZE);
 679	if (ret == -EBADMSG) {
 680		log_print("lowcomms: addr=%p, base=%u, len=%u, read=%d",
 681			  page_address(con->rx_page), con->cb.base,
 682			  con->cb.len, r);
 683	}
 684	if (ret < 0)
 685		goto out_close;
 686	cbuf_eat(&con->cb, ret);
 
 
 
 
 
 
 
 
 
 
 
 
 
 687
 688	if (cbuf_empty(&con->cb) && !call_again_soon) {
 689		__free_page(con->rx_page);
 690		con->rx_page = NULL;
 
 
 
 691	}
 692
 693	if (call_again_soon)
 694		goto out_resched;
 695	mutex_unlock(&con->sock_mutex);
 696	return 0;
 
 
 
 
 
 
 
 
 
 
 
 697
 698out_resched:
 699	if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
 700		queue_work(recv_workqueue, &con->rwork);
 701	mutex_unlock(&con->sock_mutex);
 702	return -EAGAIN;
 703
 704out_close:
 705	mutex_unlock(&con->sock_mutex);
 706	if (ret != -EAGAIN) {
 707		close_connection(con, false, true, false);
 708		/* Reconnect when there is something to send */
 709	}
 710	/* Don't return success if we really got EOF */
 711	if (ret == 0)
 712		ret = -EAGAIN;
 713
 714	return ret;
 
 715}
 716
 717/* Listening socket is busy, accept a connection */
 718static int tcp_accept_from_sock(struct connection *con)
 719{
 720	int result;
 721	struct sockaddr_storage peeraddr;
 722	struct socket *newsock;
 723	int len;
 724	int nodeid;
 725	struct connection *newcon;
 726	struct connection *addcon;
 727
 728	mutex_lock(&connections_lock);
 729	if (!dlm_allow_conn) {
 730		mutex_unlock(&connections_lock);
 731		return -1;
 732	}
 733	mutex_unlock(&connections_lock);
 734
 735	memset(&peeraddr, 0, sizeof(peeraddr));
 736	result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
 737				  SOCK_STREAM, IPPROTO_TCP, &newsock);
 738	if (result < 0)
 739		return -ENOMEM;
 
 
 
 
 740
 741	mutex_lock_nested(&con->sock_mutex, 0);
 
 
 742
 743	result = -ENOTCONN;
 744	if (con->sock == NULL)
 745		goto accept_err;
 
 
 
 
 
 746
 747	newsock->type = con->sock->type;
 748	newsock->ops = con->sock->ops;
 
 
 749
 750	result = con->sock->ops->accept(con->sock, newsock, O_NONBLOCK);
 751	if (result < 0)
 752		goto accept_err;
 
 
 
 
 
 
 
 
 
 
 753
 754	/* Get the connected socket's peer */
 755	memset(&peeraddr, 0, sizeof(peeraddr));
 756	if (newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr,
 757				  &len, 2)) {
 758		result = -ECONNABORTED;
 759		goto accept_err;
 760	}
 
 
 
 
 
 
 
 
 
 761
 762	/* Get the new node's NODEID */
 763	make_sockaddr(&peeraddr, 0, &len);
 764	if (addr_to_nodeid(&peeraddr, &nodeid)) {
 765		unsigned char *b=(unsigned char *)&peeraddr;
 766		log_print("connect from non cluster node");
 767		print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE, 
 768				     b, sizeof(struct sockaddr_storage));
 769		sock_release(newsock);
 770		mutex_unlock(&con->sock_mutex);
 771		return -1;
 772	}
 773
 774	log_print("got connection from %d", nodeid);
 775
 776	/*  Check to see if we already have a connection to this node. This
 777	 *  could happen if the two nodes initiate a connection at roughly
 778	 *  the same time and the connections cross on the wire.
 779	 *  In this case we store the incoming one in "othercon"
 780	 */
 781	newcon = nodeid2con(nodeid, GFP_NOFS);
 782	if (!newcon) {
 783		result = -ENOMEM;
 784		goto accept_err;
 785	}
 786	mutex_lock_nested(&newcon->sock_mutex, 1);
 787	if (newcon->sock) {
 788		struct connection *othercon = newcon->othercon;
 789
 790		if (!othercon) {
 791			othercon = kmem_cache_zalloc(con_cache, GFP_NOFS);
 792			if (!othercon) {
 793				log_print("failed to allocate incoming socket");
 794				mutex_unlock(&newcon->sock_mutex);
 795				result = -ENOMEM;
 796				goto accept_err;
 797			}
 798			othercon->nodeid = nodeid;
 799			othercon->rx_action = receive_from_sock;
 800			mutex_init(&othercon->sock_mutex);
 801			INIT_WORK(&othercon->swork, process_send_sockets);
 802			INIT_WORK(&othercon->rwork, process_recv_sockets);
 803			set_bit(CF_IS_OTHERCON, &othercon->flags);
 804		}
 805		if (!othercon->sock) {
 806			newcon->othercon = othercon;
 807			othercon->sock = newsock;
 808			newsock->sk->sk_user_data = othercon;
 809			add_sock(newsock, othercon);
 810			addcon = othercon;
 811		}
 812		else {
 813			printk("Extra connection from node %d attempted\n", nodeid);
 814			result = -EAGAIN;
 815			mutex_unlock(&newcon->sock_mutex);
 816			goto accept_err;
 817		}
 818	}
 819	else {
 820		newsock->sk->sk_user_data = newcon;
 821		newcon->rx_action = receive_from_sock;
 822		add_sock(newsock, newcon);
 823		addcon = newcon;
 
 
 824	}
 825
 826	mutex_unlock(&newcon->sock_mutex);
 827
 828	/*
 829	 * Add it to the active queue in case we got data
 830	 * between processing the accept adding the socket
 831	 * to the read_sockets list
 832	 */
 833	if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
 834		queue_work(recv_workqueue, &addcon->rwork);
 835	mutex_unlock(&con->sock_mutex);
 
 
 
 
 
 
 
 
 
 836
 837	return 0;
 838
 839accept_err:
 840	mutex_unlock(&con->sock_mutex);
 841	sock_release(newsock);
 842
 843	if (result != -EAGAIN)
 844		log_print("error accepting connection from node: %d", result);
 845	return result;
 846}
 847
 848static int sctp_accept_from_sock(struct connection *con)
 
 849{
 850	/* Check that the new node is in the lockspace */
 851	struct sctp_prim prim;
 852	int nodeid;
 853	int prim_len, ret;
 854	int addr_len;
 855	struct connection *newcon;
 856	struct connection *addcon;
 857	struct socket *newsock;
 
 858
 859	mutex_lock(&connections_lock);
 860	if (!dlm_allow_conn) {
 861		mutex_unlock(&connections_lock);
 862		return -1;
 
 
 
 
 
 
 
 
 863	}
 864	mutex_unlock(&connections_lock);
 865
 866	mutex_lock_nested(&con->sock_mutex, 0);
 
 
 
 
 
 867
 868	ret = kernel_accept(con->sock, &newsock, O_NONBLOCK);
 869	if (ret < 0)
 870		goto accept_err;
 
 
 
 
 871
 872	memset(&prim, 0, sizeof(struct sctp_prim));
 873	prim_len = sizeof(struct sctp_prim);
 
 
 
 
 
 
 
 874
 875	ret = kernel_getsockopt(newsock, IPPROTO_SCTP, SCTP_PRIMARY_ADDR,
 876				(char *)&prim, &prim_len);
 877	if (ret < 0) {
 878		log_print("getsockopt/sctp_primary_addr failed: %d", ret);
 879		goto accept_err;
 880	}
 881
 882	make_sockaddr(&prim.ssp_addr, 0, &addr_len);
 883	if (addr_to_nodeid(&prim.ssp_addr, &nodeid)) {
 884		unsigned char *b = (unsigned char *)&prim.ssp_addr;
 885
 886		log_print("reject connect from unknown addr");
 887		print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
 888				     b, sizeof(struct sockaddr_storage));
 889		goto accept_err;
 890	}
 891
 892	newcon = nodeid2con(nodeid, GFP_NOFS);
 893	if (!newcon) {
 894		ret = -ENOMEM;
 
 
 
 
 
 
 
 895		goto accept_err;
 896	}
 897
 898	mutex_lock_nested(&newcon->sock_mutex, 1);
 899
 
 900	if (newcon->sock) {
 901		struct connection *othercon = newcon->othercon;
 902
 903		if (!othercon) {
 904			othercon = kmem_cache_zalloc(con_cache, GFP_NOFS);
 905			if (!othercon) {
 906				log_print("failed to allocate incoming socket");
 907				mutex_unlock(&newcon->sock_mutex);
 908				ret = -ENOMEM;
 
 909				goto accept_err;
 910			}
 911			othercon->nodeid = nodeid;
 912			othercon->rx_action = receive_from_sock;
 913			mutex_init(&othercon->sock_mutex);
 914			INIT_WORK(&othercon->swork, process_send_sockets);
 915			INIT_WORK(&othercon->rwork, process_recv_sockets);
 916			set_bit(CF_IS_OTHERCON, &othercon->flags);
 917		}
 918		if (!othercon->sock) {
 919			newcon->othercon = othercon;
 920			othercon->sock = newsock;
 921			newsock->sk->sk_user_data = othercon;
 922			add_sock(newsock, othercon);
 923			addcon = othercon;
 924		} else {
 925			printk("Extra connection from node %d attempted\n", nodeid);
 926			ret = -EAGAIN;
 927			mutex_unlock(&newcon->sock_mutex);
 928			goto accept_err;
 929		}
 930	} else {
 931		newsock->sk->sk_user_data = newcon;
 932		newcon->rx_action = receive_from_sock;
 933		add_sock(newsock, newcon);
 934		addcon = newcon;
 935	}
 936
 937	log_print("connected to %d", nodeid);
 
 938
 939	mutex_unlock(&newcon->sock_mutex);
 
 
 
 
 
 
 
 
 
 
 940
 941	/*
 942	 * Add it to the active queue in case we got data
 943	 * between processing the accept adding the socket
 944	 * to the read_sockets list
 945	 */
 946	if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
 947		queue_work(recv_workqueue, &addcon->rwork);
 948	mutex_unlock(&con->sock_mutex);
 949
 950	return 0;
 951
 952accept_err:
 953	mutex_unlock(&con->sock_mutex);
 954	if (newsock)
 955		sock_release(newsock);
 956	if (ret != -EAGAIN)
 957		log_print("error accepting connection from node: %d", ret);
 958
 959	return ret;
 960}
 961
 962static void free_entry(struct writequeue_entry *e)
 963{
 964	__free_page(e->page);
 965	kfree(e);
 966}
 967
 968/*
 969 * writequeue_entry_complete - try to delete and free write queue entry
 970 * @e: write queue entry to try to delete
 971 * @completed: bytes completed
 972 *
 973 * writequeue_lock must be held.
 974 */
 975static void writequeue_entry_complete(struct writequeue_entry *e, int completed)
 976{
 977	e->offset += completed;
 978	e->len -= completed;
 
 
 979
 980	if (e->len == 0 && e->users == 0) {
 981		list_del(&e->list);
 982		free_entry(e);
 983	}
 984}
 985
 986/*
 987 * sctp_bind_addrs - bind a SCTP socket to all our addresses
 988 */
 989static int sctp_bind_addrs(struct connection *con, uint16_t port)
 990{
 991	struct sockaddr_storage localaddr;
 
 992	int i, addr_len, result = 0;
 993
 994	for (i = 0; i < dlm_local_count; i++) {
 995		memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
 996		make_sockaddr(&localaddr, port, &addr_len);
 997
 998		if (!i)
 999			result = kernel_bind(con->sock,
1000					     (struct sockaddr *)&localaddr,
1001					     addr_len);
1002		else
1003			result = kernel_setsockopt(con->sock, SOL_SCTP,
1004						   SCTP_SOCKOPT_BINDX_ADD,
1005						   (char *)&localaddr, addr_len);
1006
1007		if (result < 0) {
1008			log_print("Can't bind to %d addr number %d, %d.\n",
1009				  port, i + 1, result);
1010			break;
1011		}
1012	}
1013	return result;
1014}
1015
1016/* Initiate an SCTP association.
1017   This is a special case of send_to_sock() in that we don't yet have a
1018   peeled-off socket for this association, so we use the listening socket
1019   and add the primary IP address of the remote node.
1020 */
1021static void sctp_connect_to_sock(struct connection *con)
1022{
1023	struct sockaddr_storage daddr;
1024	int one = 1;
1025	int result;
1026	int addr_len;
1027	struct socket *sock;
 
 
1028
1029	if (con->nodeid == 0) {
1030		log_print("attempt to connect sock 0 foiled");
1031		return;
1032	}
 
1033
1034	mutex_lock(&con->sock_mutex);
 
 
1035
1036	/* Some odd races can cause double-connects, ignore them */
1037	if (con->retries++ > MAX_CONNECT_RETRIES)
1038		goto out;
1039
1040	if (con->sock) {
1041		log_print("node %d already connected.", con->nodeid);
1042		goto out;
 
1043	}
1044
1045	memset(&daddr, 0, sizeof(daddr));
1046	result = nodeid_to_addr(con->nodeid, &daddr, NULL, true);
1047	if (result < 0) {
1048		log_print("no address for nodeid %d", con->nodeid);
1049		goto out;
1050	}
 
 
 
1051
1052	/* Create a socket to communicate with */
1053	result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1054				  SOCK_STREAM, IPPROTO_SCTP, &sock);
1055	if (result < 0)
1056		goto socket_err;
1057
1058	sock->sk->sk_user_data = con;
1059	con->rx_action = receive_from_sock;
1060	con->connect_action = sctp_connect_to_sock;
1061	add_sock(sock, con);
 
 
 
 
 
 
 
 
 
 
 
1062
1063	/* Bind to all addresses. */
1064	if (sctp_bind_addrs(con, 0))
1065		goto bind_err;
1066
1067	make_sockaddr(&daddr, dlm_config.ci_tcp_port, &addr_len);
 
 
 
 
1068
1069	log_print("connecting to %d", con->nodeid);
1070
1071	/* Turn off Nagle's algorithm */
1072	kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one,
1073			  sizeof(one));
 
1074
1075	result = sock->ops->connect(sock, (struct sockaddr *)&daddr, addr_len,
1076				   O_NONBLOCK);
1077	if (result == -EINPROGRESS)
1078		result = 0;
1079	if (result == 0)
1080		goto out;
1081
 
 
 
1082
1083bind_err:
1084	con->sock = NULL;
1085	sock_release(sock);
1086
1087socket_err:
1088	/*
1089	 * Some errors are fatal and this list might need adjusting. For other
1090	 * errors we try again until the max number of retries is reached.
1091	 */
1092	if (result != -EHOSTUNREACH &&
1093	    result != -ENETUNREACH &&
1094	    result != -ENETDOWN &&
1095	    result != -EINVAL &&
1096	    result != -EPROTONOSUPPORT) {
1097		log_print("connect %d try %d error %d", con->nodeid,
1098			  con->retries, result);
1099		mutex_unlock(&con->sock_mutex);
1100		msleep(1000);
1101		clear_bit(CF_CONNECT_PENDING, &con->flags);
1102		lowcomms_connect_sock(con);
1103		return;
1104	}
1105
1106out:
1107	mutex_unlock(&con->sock_mutex);
1108	set_bit(CF_WRITE_PENDING, &con->flags);
 
 
 
 
1109}
1110
1111/* Connect a new socket to its peer */
1112static void tcp_connect_to_sock(struct connection *con)
 
 
 
 
1113{
1114	struct sockaddr_storage saddr, src_addr;
1115	int addr_len;
1116	struct socket *sock = NULL;
1117	int one = 1;
1118	int result;
1119
1120	if (con->nodeid == 0) {
1121		log_print("attempt to connect sock 0 foiled");
1122		return;
 
 
 
1123	}
1124
1125	mutex_lock(&con->sock_mutex);
1126	if (con->retries++ > MAX_CONNECT_RETRIES)
1127		goto out;
 
 
 
1128
1129	/* Some odd races can cause double-connects, ignore them */
1130	if (con->sock)
1131		goto out;
1132
1133	/* Create a socket to communicate with */
1134	result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1135				  SOCK_STREAM, IPPROTO_TCP, &sock);
1136	if (result < 0)
1137		goto out_err;
1138
1139	memset(&saddr, 0, sizeof(saddr));
1140	result = nodeid_to_addr(con->nodeid, &saddr, NULL, false);
1141	if (result < 0) {
1142		log_print("no address for nodeid %d", con->nodeid);
1143		goto out_err;
1144	}
1145
1146	sock->sk->sk_user_data = con;
1147	con->rx_action = receive_from_sock;
1148	con->connect_action = tcp_connect_to_sock;
1149	add_sock(sock, con);
 
 
 
1150
1151	/* Bind to our cluster-known address connecting to avoid
1152	   routing problems */
1153	memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
1154	make_sockaddr(&src_addr, 0, &addr_len);
1155	result = sock->ops->bind(sock, (struct sockaddr *) &src_addr,
1156				 addr_len);
1157	if (result < 0) {
1158		log_print("could not bind for connect: %d", result);
1159		/* This *may* not indicate a critical error */
1160	}
1161
1162	make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
 
 
1163
1164	log_print("connecting to %d", con->nodeid);
 
 
1165
1166	/* Turn off Nagle's algorithm */
1167	kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one,
1168			  sizeof(one));
1169
1170	result = sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
1171				   O_NONBLOCK);
1172	if (result == -EINPROGRESS)
1173		result = 0;
1174	if (result == 0)
1175		goto out;
1176
1177out_err:
1178	if (con->sock) {
1179		sock_release(con->sock);
1180		con->sock = NULL;
1181	} else if (sock) {
1182		sock_release(sock);
1183	}
1184	/*
1185	 * Some errors are fatal and this list might need adjusting. For other
1186	 * errors we try again until the max number of retries is reached.
1187	 */
1188	if (result != -EHOSTUNREACH &&
1189	    result != -ENETUNREACH &&
1190	    result != -ENETDOWN && 
1191	    result != -EINVAL &&
1192	    result != -EPROTONOSUPPORT) {
1193		log_print("connect %d try %d error %d", con->nodeid,
1194			  con->retries, result);
1195		mutex_unlock(&con->sock_mutex);
1196		msleep(1000);
1197		clear_bit(CF_CONNECT_PENDING, &con->flags);
1198		lowcomms_connect_sock(con);
1199		return;
1200	}
1201out:
1202	mutex_unlock(&con->sock_mutex);
1203	set_bit(CF_WRITE_PENDING, &con->flags);
1204	return;
1205}
1206
1207static struct socket *tcp_create_listen_sock(struct connection *con,
1208					     struct sockaddr_storage *saddr)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1209{
1210	struct socket *sock = NULL;
1211	int result = 0;
1212	int one = 1;
1213	int addr_len;
1214
1215	if (dlm_local_addr[0]->ss_family == AF_INET)
1216		addr_len = sizeof(struct sockaddr_in);
1217	else
1218		addr_len = sizeof(struct sockaddr_in6);
1219
1220	/* Create a socket to communicate with */
1221	result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1222				  SOCK_STREAM, IPPROTO_TCP, &sock);
1223	if (result < 0) {
1224		log_print("Can't create listening comms socket");
1225		goto create_out;
1226	}
1227
1228	/* Turn off Nagle's algorithm */
1229	kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one,
1230			  sizeof(one));
 
 
 
 
1231
1232	result = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
1233				   (char *)&one, sizeof(one));
1234
1235	if (result < 0) {
1236		log_print("Failed to set SO_REUSEADDR on socket: %d", result);
1237	}
1238	sock->sk->sk_user_data = con;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1239
1240	con->rx_action = tcp_accept_from_sock;
1241	con->connect_action = tcp_connect_to_sock;
 
 
 
1242
1243	/* Bind to our port */
1244	make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
1245	result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
1246	if (result < 0) {
1247		log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
1248		sock_release(sock);
1249		sock = NULL;
1250		con->sock = NULL;
1251		goto create_out;
1252	}
1253	result = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
1254				 (char *)&one, sizeof(one));
1255	if (result < 0) {
1256		log_print("Set keepalive failed: %d", result);
1257	}
1258
1259	result = sock->ops->listen(sock, 5);
1260	if (result < 0) {
1261		log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
1262		sock_release(sock);
1263		sock = NULL;
1264		goto create_out;
1265	}
1266
1267create_out:
1268	return sock;
1269}
1270
1271/* Get local addresses */
1272static void init_local(void)
1273{
1274	struct sockaddr_storage sas, *addr;
1275	int i;
1276
1277	dlm_local_count = 0;
1278	for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
1279		if (dlm_our_addr(&sas, i))
1280			break;
1281
1282		addr = kmalloc(sizeof(*addr), GFP_NOFS);
1283		if (!addr)
1284			break;
1285		memcpy(addr, &sas, sizeof(*addr));
1286		dlm_local_addr[dlm_local_count++] = addr;
1287	}
 
1288}
1289
1290/* Initialise SCTP socket and bind to all interfaces */
1291static int sctp_listen_for_all(void)
1292{
1293	struct socket *sock = NULL;
1294	int result = -EINVAL;
1295	struct connection *con = nodeid2con(0, GFP_NOFS);
1296	int bufsize = NEEDED_RMEM;
1297	int one = 1;
 
1298
1299	if (!con)
1300		return -ENOMEM;
 
 
 
 
1301
1302	log_print("Using SCTP for communications");
1303
1304	result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1305				  SOCK_STREAM, IPPROTO_SCTP, &sock);
1306	if (result < 0) {
1307		log_print("Can't create comms socket, check SCTP is loaded");
1308		goto out;
1309	}
1310
1311	result = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUFFORCE,
1312				 (char *)&bufsize, sizeof(bufsize));
1313	if (result)
1314		log_print("Error increasing buffer space on socket %d", result);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1315
1316	result = kernel_setsockopt(sock, SOL_SCTP, SCTP_NODELAY, (char *)&one,
1317				   sizeof(one));
1318	if (result < 0)
1319		log_print("Could not set SCTP NODELAY error %d\n", result);
 
1320
1321	write_lock_bh(&sock->sk->sk_callback_lock);
1322	/* Init con struct */
1323	sock->sk->sk_user_data = con;
1324	con->sock = sock;
1325	con->sock->sk->sk_data_ready = lowcomms_data_ready;
1326	con->rx_action = sctp_accept_from_sock;
1327	con->connect_action = sctp_connect_to_sock;
1328
1329	write_unlock_bh(&sock->sk->sk_callback_lock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1330
1331	/* Bind to all addresses. */
1332	if (sctp_bind_addrs(con, dlm_config.ci_tcp_port))
1333		goto create_delsock;
 
 
 
1334
1335	result = sock->ops->listen(sock, 5);
1336	if (result < 0) {
1337		log_print("Can't set socket listening");
1338		goto create_delsock;
1339	}
 
 
 
 
 
1340
1341	return 0;
 
 
 
 
 
1342
1343create_delsock:
1344	sock_release(sock);
1345	con->sock = NULL;
1346out:
1347	return result;
1348}
1349
1350static int tcp_listen_for_all(void)
1351{
1352	struct socket *sock = NULL;
1353	struct connection *con = nodeid2con(0, GFP_NOFS);
1354	int result = -EINVAL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1355
1356	if (!con)
1357		return -ENOMEM;
1358
1359	/* We don't support multi-homed hosts */
1360	if (dlm_local_addr[1] != NULL) {
1361		log_print("TCP protocol can't handle multi-homed hosts, "
1362			  "try SCTP");
1363		return -EINVAL;
1364	}
1365
1366	log_print("Using TCP for communications");
 
 
 
 
 
 
 
 
 
 
 
 
 
1367
1368	sock = tcp_create_listen_sock(con, dlm_local_addr[0]);
1369	if (sock) {
1370		add_sock(sock, con);
1371		result = 0;
1372	}
1373	else {
1374		result = -EADDRINUSE;
1375	}
1376
1377	return result;
1378}
1379
1380
1381
1382static struct writequeue_entry *new_writequeue_entry(struct connection *con,
1383						     gfp_t allocation)
1384{
1385	struct writequeue_entry *entry;
 
1386
1387	entry = kmalloc(sizeof(struct writequeue_entry), allocation);
1388	if (!entry)
1389		return NULL;
1390
1391	entry->page = alloc_page(allocation);
1392	if (!entry->page) {
1393		kfree(entry);
1394		return NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1395	}
1396
1397	entry->offset = 0;
1398	entry->len = 0;
1399	entry->end = 0;
1400	entry->users = 0;
1401	entry->con = con;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1402
1403	return entry;
 
 
1404}
1405
1406void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
1407{
1408	struct connection *con;
1409	struct writequeue_entry *e;
1410	int offset = 0;
1411
1412	con = nodeid2con(nodeid, allocation);
1413	if (!con)
1414		return NULL;
1415
1416	spin_lock(&con->writequeue_lock);
1417	e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
1418	if ((&e->list == &con->writequeue) ||
1419	    (PAGE_SIZE - e->end < len)) {
1420		e = NULL;
1421	} else {
1422		offset = e->end;
1423		e->end += len;
1424		e->users++;
1425	}
1426	spin_unlock(&con->writequeue_lock);
1427
1428	if (e) {
1429	got_one:
1430		*ppc = page_address(e->page) + offset;
1431		return e;
1432	}
1433
1434	e = new_writequeue_entry(con, allocation);
1435	if (e) {
1436		spin_lock(&con->writequeue_lock);
1437		offset = e->end;
1438		e->end += len;
1439		e->users++;
1440		list_add_tail(&e->list, &con->writequeue);
1441		spin_unlock(&con->writequeue_lock);
1442		goto got_one;
1443	}
1444	return NULL;
1445}
1446
1447void dlm_lowcomms_commit_buffer(void *mh)
1448{
1449	struct writequeue_entry *e = (struct writequeue_entry *)mh;
1450	struct connection *con = e->con;
1451	int users;
 
 
 
1452
1453	spin_lock(&con->writequeue_lock);
1454	users = --e->users;
1455	if (users)
1456		goto out;
1457	e->len = e->end - e->offset;
1458	spin_unlock(&con->writequeue_lock);
1459
1460	if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
1461		queue_work(send_workqueue, &con->swork);
1462	}
1463	return;
1464
1465out:
1466	spin_unlock(&con->writequeue_lock);
1467	return;
1468}
1469
1470/* Send a message */
1471static void send_to_sock(struct connection *con)
1472{
1473	int ret = 0;
1474	const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1475	struct writequeue_entry *e;
1476	int len, offset;
1477	int count = 0;
1478
1479	mutex_lock(&con->sock_mutex);
1480	if (con->sock == NULL)
1481		goto out_connect;
 
1482
1483	spin_lock(&con->writequeue_lock);
1484	for (;;) {
1485		e = list_entry(con->writequeue.next, struct writequeue_entry,
1486			       list);
1487		if ((struct list_head *) e == &con->writequeue)
1488			break;
1489
1490		len = e->len;
1491		offset = e->offset;
1492		BUG_ON(len == 0 && e->users == 0);
1493		spin_unlock(&con->writequeue_lock);
1494
1495		ret = 0;
1496		if (len) {
1497			ret = kernel_sendpage(con->sock, e->page, offset, len,
1498					      msg_flags);
1499			if (ret == -EAGAIN || ret == 0) {
1500				if (ret == -EAGAIN &&
1501				    test_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags) &&
1502				    !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
1503					/* Notify TCP that we're limited by the
1504					 * application window size.
1505					 */
1506					set_bit(SOCK_NOSPACE, &con->sock->flags);
1507					con->sock->sk->sk_write_pending++;
1508				}
1509				cond_resched();
1510				goto out;
1511			} else if (ret < 0)
1512				goto send_error;
1513		}
 
 
 
1514
1515		/* Don't starve people filling buffers */
1516		if (++count >= MAX_SEND_MSG_COUNT) {
1517			cond_resched();
1518			count = 0;
1519		}
 
 
 
 
 
 
 
 
 
 
 
 
1520
1521		spin_lock(&con->writequeue_lock);
1522		writequeue_entry_complete(e, ret);
 
 
 
1523	}
1524	spin_unlock(&con->writequeue_lock);
1525out:
1526	mutex_unlock(&con->sock_mutex);
1527	return;
1528
1529send_error:
1530	mutex_unlock(&con->sock_mutex);
1531	close_connection(con, false, false, true);
1532	lowcomms_connect_sock(con);
1533	return;
1534
1535out_connect:
1536	mutex_unlock(&con->sock_mutex);
1537	lowcomms_connect_sock(con);
1538}
1539
1540static void clean_one_writequeue(struct connection *con)
1541{
1542	struct writequeue_entry *e, *safe;
 
 
 
 
 
 
 
 
 
1543
1544	spin_lock(&con->writequeue_lock);
1545	list_for_each_entry_safe(e, safe, &con->writequeue, list) {
1546		list_del(&e->list);
1547		free_entry(e);
1548	}
1549	spin_unlock(&con->writequeue_lock);
 
 
 
 
 
1550}
1551
1552/* Called from recovery when it knows that a node has
1553   left the cluster */
1554int dlm_lowcomms_close(int nodeid)
1555{
1556	struct connection *con;
1557	struct dlm_node_addr *na;
1558
1559	log_print("closing connection to node %d", nodeid);
1560	con = nodeid2con(nodeid, 0);
1561	if (con) {
1562		set_bit(CF_CLOSE, &con->flags);
1563		close_connection(con, true, true, true);
1564		clean_one_writequeue(con);
1565	}
1566
1567	spin_lock(&dlm_node_addrs_spin);
1568	na = find_node_addr(nodeid);
1569	if (na) {
1570		list_del(&na->list);
1571		while (na->addr_count--)
1572			kfree(na->addr[na->addr_count]);
1573		kfree(na);
1574	}
1575	spin_unlock(&dlm_node_addrs_spin);
1576
1577	return 0;
1578}
1579
1580/* Receive workqueue function */
1581static void process_recv_sockets(struct work_struct *work)
1582{
1583	struct connection *con = container_of(work, struct connection, rwork);
1584	int err;
 
 
 
1585
1586	clear_bit(CF_READ_PENDING, &con->flags);
1587	do {
1588		err = con->rx_action(con);
1589	} while (!err);
1590}
1591
1592/* Send workqueue function */
1593static void process_send_sockets(struct work_struct *work)
1594{
1595	struct connection *con = container_of(work, struct connection, swork);
 
 
1596
1597	if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags))
1598		con->connect_action(con);
1599	if (test_and_clear_bit(CF_WRITE_PENDING, &con->flags))
1600		send_to_sock(con);
1601}
1602
 
 
 
1603
1604/* Discard all entries on the write queues */
1605static void clean_writequeues(void)
1606{
1607	foreach_conn(clean_one_writequeue);
1608}
1609
1610static void work_stop(void)
 
 
 
 
 
 
 
 
 
 
1611{
1612	destroy_workqueue(recv_workqueue);
1613	destroy_workqueue(send_workqueue);
1614}
1615
1616static int work_start(void)
1617{
1618	recv_workqueue = alloc_workqueue("dlm_recv",
1619					 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
1620	if (!recv_workqueue) {
1621		log_print("can't start dlm_recv");
1622		return -ENOMEM;
1623	}
1624
1625	send_workqueue = alloc_workqueue("dlm_send",
1626					 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
1627	if (!send_workqueue) {
1628		log_print("can't start dlm_send");
1629		destroy_workqueue(recv_workqueue);
1630		return -ENOMEM;
1631	}
1632
 
1633	return 0;
1634}
1635
1636static void stop_conn(struct connection *con)
1637{
1638	con->flags |= 0x0F;
1639	if (con->sock && con->sock->sk)
1640		con->sock->sk->sk_user_data = NULL;
1641}
1642
1643static void free_conn(struct connection *con)
1644{
1645	close_connection(con, true, true, true);
1646	if (con->othercon)
1647		kmem_cache_free(con_cache, con->othercon);
1648	hlist_del(&con->list);
1649	kmem_cache_free(con_cache, con);
1650}
1651
1652void dlm_lowcomms_stop(void)
1653{
1654	/* Set all the flags to prevent any
1655	   socket activity.
1656	*/
1657	mutex_lock(&connections_lock);
1658	dlm_allow_conn = 0;
1659	foreach_conn(stop_conn);
1660	mutex_unlock(&connections_lock);
1661
1662	work_stop();
1663
1664	mutex_lock(&connections_lock);
1665	clean_writequeues();
1666
1667	foreach_conn(free_conn);
1668
1669	mutex_unlock(&connections_lock);
1670	kmem_cache_destroy(con_cache);
1671}
1672
1673int dlm_lowcomms_start(void)
1674{
1675	int error = -EINVAL;
1676	struct connection *con;
1677	int i;
1678
1679	for (i = 0; i < CONN_HASH_SIZE; i++)
1680		INIT_HLIST_HEAD(&connection_hash[i]);
1681
1682	init_local();
1683	if (!dlm_local_count) {
1684		error = -ENOTCONN;
1685		log_print("no local IP address has been set");
1686		goto fail;
1687	}
1688
1689	error = -ENOMEM;
1690	con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection),
1691				      __alignof__(struct connection), 0,
1692				      NULL);
1693	if (!con_cache)
1694		goto fail;
1695
1696	error = work_start();
1697	if (error)
1698		goto fail_destroy;
1699
1700	dlm_allow_conn = 1;
 
 
 
 
 
 
 
 
 
 
 
 
 
1701
1702	/* Start listening */
1703	if (dlm_config.ci_protocol == 0)
1704		error = tcp_listen_for_all();
1705	else
1706		error = sctp_listen_for_all();
1707	if (error)
1708		goto fail_unlisten;
1709
1710	return 0;
1711
1712fail_unlisten:
1713	dlm_allow_conn = 0;
1714	con = nodeid2con(0,0);
1715	if (con) {
1716		close_connection(con, false, true, true);
1717		kmem_cache_free(con_cache, con);
1718	}
1719fail_destroy:
1720	kmem_cache_destroy(con_cache);
1721fail:
1722	return error;
1723}
1724
 
 
 
 
 
 
 
 
 
 
1725void dlm_lowcomms_exit(void)
1726{
1727	struct dlm_node_addr *na, *safe;
 
1728
1729	spin_lock(&dlm_node_addrs_spin);
1730	list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) {
1731		list_del(&na->list);
1732		while (na->addr_count--)
1733			kfree(na->addr[na->addr_count]);
1734		kfree(na);
 
 
 
 
 
 
1735	}
1736	spin_unlock(&dlm_node_addrs_spin);
1737}