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