blob: df9827ec12f310ef0218b50f24608f969c112d30 [file] [log] [blame]
Thomas Gleixner2522fe42019-05-28 09:57:20 -07001// SPDX-License-Identifier: GPL-2.0-only
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01002/******************************************************************************
3*******************************************************************************
4**
5** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06006** Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01007**
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01008**
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
Joe Perches2cf12c02009-01-22 13:26:47 -080022 * be expanded for the cluster infrastructure then that is its
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010023 * 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 *
Joe Perches2cf12c02009-01-22 13:26:47 -080037 * lowcomms will choose to use either TCP or SCTP as its transport layer
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010038 * depending on the configuration variable 'protocol'. This should be set
Joe Perches2cf12c02009-01-22 13:26:47 -080039 * to 0 (default) for TCP or 1 for SCTP. It should be configured using a
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010040 * 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>
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010049#include <linux/file.h>
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -050050#include <linux/mutex.h>
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010051#include <linux/sctp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/slab.h>
Benjamin Poirier2f2d76c2012-03-08 05:55:59 +000053#include <net/sctp/sctp.h>
Joe Perches44ad5322009-01-22 13:24:49 -080054#include <net/ipv6.h>
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010055
56#include "dlm_internal.h"
57#include "lowcomms.h"
58#include "midcomms.h"
59#include "config.h"
60
61#define NEEDED_RMEM (4*1024*1024)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -060062#define CONN_HASH_SIZE 32
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010063
Bob Petersonf92c8dd2010-11-12 11:15:20 -060064/* Number of messages to send before rescheduling */
65#define MAX_SEND_MSG_COUNT 25
Alexander Aring055923b2020-07-27 09:13:38 -040066#define DLM_SHUTDOWN_WAIT_TIMEOUT msecs_to_jiffies(10000)
Bob Petersonf92c8dd2010-11-12 11:15:20 -060067
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010068struct connection {
69 struct socket *sock; /* NULL if not connected */
70 uint32_t nodeid; /* So we know who we are in the list */
71 struct mutex sock_mutex;
72 unsigned long flags;
73#define CF_READ_PENDING 1
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +000074#define CF_WRITE_PENDING 2
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010075#define CF_INIT_PENDING 4
76#define CF_IS_OTHERCON 5
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -050077#define CF_CLOSE 6
David Millerb36930d2010-11-10 21:56:39 -080078#define CF_APP_LIMITED 7
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +000079#define CF_CLOSING 8
Alexander Aring055923b2020-07-27 09:13:38 -040080#define CF_SHUTDOWN 9
Alexander Aring19633c72020-11-02 20:04:20 -050081#define CF_CONNECTED 10
Alexander Aringba868d92021-05-21 15:08:37 -040082#define CF_RECONNECT 11
83#define CF_DELAY_CONNECT 12
Alexander Aring8aa31cb2021-05-21 15:08:39 -040084#define CF_EOF 13
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010085 struct list_head writequeue; /* List of outgoing writequeue_entries */
86 spinlock_t writequeue_lock;
Alexander Aring8aa31cb2021-05-21 15:08:39 -040087 atomic_t writequeue_cnt;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010088 void (*connect_action) (struct connection *); /* What to do to connect */
Alexander Aring055923b2020-07-27 09:13:38 -040089 void (*shutdown_action)(struct connection *con); /* What to do to shutdown */
Alexander Aring8aa31cb2021-05-21 15:08:39 -040090 bool (*eof_condition)(struct connection *con); /* What to do to eof check */
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010091 int retries;
92#define MAX_CONNECT_RETRIES 3
Christine Caulfield5e9ccc32009-01-28 12:57:40 -060093 struct hlist_node list;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010094 struct connection *othercon;
Alexander Aringba868d92021-05-21 15:08:37 -040095 struct connection *sendcon;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010096 struct work_struct rwork; /* Receive workqueue */
97 struct work_struct swork; /* Send workqueue */
Alexander Aring055923b2020-07-27 09:13:38 -040098 wait_queue_head_t shutdown_wait; /* wait for graceful shutdown */
Alexander Aring4798cbb2020-09-24 10:31:26 -040099 unsigned char *rx_buf;
100 int rx_buflen;
101 int rx_leftover;
Alexander Aringa47666eb2020-08-27 15:02:49 -0400102 struct rcu_head rcu;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100103};
104#define sock2con(x) ((struct connection *)(x)->sk_user_data)
105
Alexander Aringd11ccd42020-11-02 20:04:25 -0500106struct listen_connection {
107 struct socket *sock;
108 struct work_struct rwork;
109};
110
Alexander Aringf0747ebf2021-03-01 17:05:16 -0500111#define DLM_WQ_REMAIN_BYTES(e) (PAGE_SIZE - e->end)
112#define DLM_WQ_LENGTH_BYTES(e) (e->end - e->offset)
113
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100114/* An entry waiting to be sent */
115struct writequeue_entry {
116 struct list_head list;
117 struct page *page;
118 int offset;
119 int len;
120 int end;
121 int users;
122 struct connection *con;
Alexander Aring8f2dc782021-05-21 15:08:42 -0400123 struct list_head msgs;
124 struct kref ref;
125};
126
127struct dlm_msg {
128 struct writequeue_entry *entry;
Alexander Aring2874d1a2021-05-21 15:08:43 -0400129 struct dlm_msg *orig_msg;
130 bool retransmit;
Alexander Aring8f2dc782021-05-21 15:08:42 -0400131 void *ppc;
132 int len;
133 int idx; /* new()/commit() idx exchange */
134
135 struct list_head list;
136 struct kref ref;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100137};
138
David Teigland36b71a82012-07-26 12:44:30 -0500139struct dlm_node_addr {
140 struct list_head list;
141 int nodeid;
Alexander Aringe125fbe2021-03-01 17:05:09 -0500142 int mark;
David Teigland36b71a82012-07-26 12:44:30 -0500143 int addr_count;
Mike Christie98e1b602013-06-14 04:56:12 -0500144 int curr_addr_index;
David Teigland36b71a82012-07-26 12:44:30 -0500145 struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
146};
147
Bob Petersoncc661fc2017-09-12 08:55:23 +0000148static struct listen_sock_callbacks {
149 void (*sk_error_report)(struct sock *);
150 void (*sk_data_ready)(struct sock *);
151 void (*sk_state_change)(struct sock *);
152 void (*sk_write_space)(struct sock *);
153} listen_sock;
154
David Teigland36b71a82012-07-26 12:44:30 -0500155static LIST_HEAD(dlm_node_addrs);
156static DEFINE_SPINLOCK(dlm_node_addrs_spin);
157
Alexander Aringd11ccd42020-11-02 20:04:25 -0500158static struct listen_connection listen_con;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100159static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
160static int dlm_local_count;
Alexander Aring51746162021-03-01 17:05:13 -0500161int dlm_allow_conn;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100162
163/* Work queues */
164static struct workqueue_struct *recv_workqueue;
165static struct workqueue_struct *send_workqueue;
166
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600167static struct hlist_head connection_hash[CONN_HASH_SIZE];
Alexander Aringa47666eb2020-08-27 15:02:49 -0400168static DEFINE_SPINLOCK(connections_lock);
169DEFINE_STATIC_SRCU(connections_srcu);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100170
171static void process_recv_sockets(struct work_struct *work);
172static void process_send_sockets(struct work_struct *work);
173
Alexander Aring0672c3c2020-11-02 20:04:22 -0500174static void sctp_connect_to_sock(struct connection *con);
175static void tcp_connect_to_sock(struct connection *con);
Alexander Aring42873c92020-11-02 20:04:23 -0500176static void dlm_tcp_shutdown(struct connection *con);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600177
178/* This is deliberately very simple because most clusters have simple
179 sequential nodeids, so we should be able to go straight to a connection
180 struct in the array */
181static inline int nodeid_hash(int nodeid)
182{
183 return nodeid & (CONN_HASH_SIZE-1);
184}
185
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400186static struct connection *__find_con(int nodeid, int r)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600187{
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600188 struct connection *con;
189
Alexander Aringa47666eb2020-08-27 15:02:49 -0400190 hlist_for_each_entry_rcu(con, &connection_hash[r], list) {
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400191 if (con->nodeid == nodeid)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600192 return con;
193 }
Alexander Aringa47666eb2020-08-27 15:02:49 -0400194
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600195 return NULL;
196}
197
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400198static bool tcp_eof_condition(struct connection *con)
199{
200 return atomic_read(&con->writequeue_cnt);
201}
202
Alexander Aring6cde2102020-11-02 20:04:21 -0500203static int dlm_con_init(struct connection *con, int nodeid)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100204{
Alexander Aring4798cbb2020-09-24 10:31:26 -0400205 con->rx_buflen = dlm_config.ci_buffer_size;
206 con->rx_buf = kmalloc(con->rx_buflen, GFP_NOFS);
Alexander Aring6cde2102020-11-02 20:04:21 -0500207 if (!con->rx_buf)
208 return -ENOMEM;
Alexander Aring4798cbb2020-09-24 10:31:26 -0400209
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100210 con->nodeid = nodeid;
211 mutex_init(&con->sock_mutex);
212 INIT_LIST_HEAD(&con->writequeue);
213 spin_lock_init(&con->writequeue_lock);
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400214 atomic_set(&con->writequeue_cnt, 0);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100215 INIT_WORK(&con->swork, process_send_sockets);
216 INIT_WORK(&con->rwork, process_recv_sockets);
Alexander Aring055923b2020-07-27 09:13:38 -0400217 init_waitqueue_head(&con->shutdown_wait);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100218
Alexander Aring42873c92020-11-02 20:04:23 -0500219 if (dlm_config.ci_protocol == 0) {
Alexander Aring0672c3c2020-11-02 20:04:22 -0500220 con->connect_action = tcp_connect_to_sock;
Alexander Aring42873c92020-11-02 20:04:23 -0500221 con->shutdown_action = dlm_tcp_shutdown;
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400222 con->eof_condition = tcp_eof_condition;
Alexander Aring42873c92020-11-02 20:04:23 -0500223 } else {
Alexander Aring0672c3c2020-11-02 20:04:22 -0500224 con->connect_action = sctp_connect_to_sock;
Alexander Aring42873c92020-11-02 20:04:23 -0500225 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100226
Alexander Aring6cde2102020-11-02 20:04:21 -0500227 return 0;
228}
229
230/*
231 * If 'allocation' is zero then we don't attempt to create a new
232 * connection structure for this node.
233 */
234static struct connection *nodeid2con(int nodeid, gfp_t alloc)
235{
236 struct connection *con, *tmp;
237 int r, ret;
238
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400239 r = nodeid_hash(nodeid);
240 con = __find_con(nodeid, r);
Alexander Aring6cde2102020-11-02 20:04:21 -0500241 if (con || !alloc)
242 return con;
243
244 con = kzalloc(sizeof(*con), alloc);
245 if (!con)
246 return NULL;
247
248 ret = dlm_con_init(con, nodeid);
249 if (ret) {
250 kfree(con);
251 return NULL;
252 }
253
Alexander Aringa47666eb2020-08-27 15:02:49 -0400254 spin_lock(&connections_lock);
Alexander Aring4f2b30f2020-09-30 18:37:29 -0400255 /* Because multiple workqueues/threads calls this function it can
256 * race on multiple cpu's. Instead of locking hot path __find_con()
257 * we just check in rare cases of recently added nodes again
258 * under protection of connections_lock. If this is the case we
259 * abort our connection creation and return the existing connection.
260 */
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400261 tmp = __find_con(nodeid, r);
Alexander Aring4f2b30f2020-09-30 18:37:29 -0400262 if (tmp) {
263 spin_unlock(&connections_lock);
264 kfree(con->rx_buf);
265 kfree(con);
266 return tmp;
267 }
268
Alexander Aringa47666eb2020-08-27 15:02:49 -0400269 hlist_add_head_rcu(&con->list, &connection_hash[r]);
270 spin_unlock(&connections_lock);
271
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100272 return con;
273}
274
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600275/* Loop round all connections */
276static void foreach_conn(void (*conn_func)(struct connection *c))
277{
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400278 int i;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600279 struct connection *con;
280
281 for (i = 0; i < CONN_HASH_SIZE; i++) {
Alexander Aringa47666eb2020-08-27 15:02:49 -0400282 hlist_for_each_entry_rcu(con, &connection_hash[i], list)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600283 conn_func(con);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600284 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100285}
286
David Teigland36b71a82012-07-26 12:44:30 -0500287static struct dlm_node_addr *find_node_addr(int nodeid)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100288{
David Teigland36b71a82012-07-26 12:44:30 -0500289 struct dlm_node_addr *na;
290
291 list_for_each_entry(na, &dlm_node_addrs, list) {
292 if (na->nodeid == nodeid)
293 return na;
294 }
295 return NULL;
296}
297
Alexander Aring40c6b832020-11-02 20:04:27 -0500298static int addr_compare(const struct sockaddr_storage *x,
299 const struct sockaddr_storage *y)
David Teigland36b71a82012-07-26 12:44:30 -0500300{
301 switch (x->ss_family) {
302 case AF_INET: {
303 struct sockaddr_in *sinx = (struct sockaddr_in *)x;
304 struct sockaddr_in *siny = (struct sockaddr_in *)y;
305 if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
306 return 0;
307 if (sinx->sin_port != siny->sin_port)
308 return 0;
309 break;
310 }
311 case AF_INET6: {
312 struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
313 struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
314 if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
315 return 0;
316 if (sinx->sin6_port != siny->sin6_port)
317 return 0;
318 break;
319 }
320 default:
321 return 0;
322 }
323 return 1;
324}
325
326static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out,
Alexander Aringe125fbe2021-03-01 17:05:09 -0500327 struct sockaddr *sa_out, bool try_new_addr,
328 unsigned int *mark)
David Teigland36b71a82012-07-26 12:44:30 -0500329{
330 struct sockaddr_storage sas;
331 struct dlm_node_addr *na;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100332
333 if (!dlm_local_count)
334 return -1;
335
David Teigland36b71a82012-07-26 12:44:30 -0500336 spin_lock(&dlm_node_addrs_spin);
337 na = find_node_addr(nodeid);
Mike Christie98e1b602013-06-14 04:56:12 -0500338 if (na && na->addr_count) {
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300339 memcpy(&sas, na->addr[na->curr_addr_index],
340 sizeof(struct sockaddr_storage));
341
Mike Christie98e1b602013-06-14 04:56:12 -0500342 if (try_new_addr) {
343 na->curr_addr_index++;
344 if (na->curr_addr_index == na->addr_count)
345 na->curr_addr_index = 0;
346 }
Mike Christie98e1b602013-06-14 04:56:12 -0500347 }
David Teigland36b71a82012-07-26 12:44:30 -0500348 spin_unlock(&dlm_node_addrs_spin);
349
350 if (!na)
351 return -EEXIST;
352
353 if (!na->addr_count)
354 return -ENOENT;
355
Alexander Aringe125fbe2021-03-01 17:05:09 -0500356 *mark = na->mark;
357
David Teigland36b71a82012-07-26 12:44:30 -0500358 if (sas_out)
359 memcpy(sas_out, &sas, sizeof(struct sockaddr_storage));
360
361 if (!sa_out)
362 return 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100363
364 if (dlm_local_addr[0]->ss_family == AF_INET) {
David Teigland36b71a82012-07-26 12:44:30 -0500365 struct sockaddr_in *in4 = (struct sockaddr_in *) &sas;
366 struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100367 ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
368 } else {
David Teigland36b71a82012-07-26 12:44:30 -0500369 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sas;
370 struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000371 ret6->sin6_addr = in6->sin6_addr;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100372 }
373
374 return 0;
375}
376
Alexander Aringe125fbe2021-03-01 17:05:09 -0500377static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid,
378 unsigned int *mark)
David Teigland36b71a82012-07-26 12:44:30 -0500379{
380 struct dlm_node_addr *na;
381 int rv = -EEXIST;
Mike Christie98e1b602013-06-14 04:56:12 -0500382 int addr_i;
David Teigland36b71a82012-07-26 12:44:30 -0500383
384 spin_lock(&dlm_node_addrs_spin);
385 list_for_each_entry(na, &dlm_node_addrs, list) {
386 if (!na->addr_count)
387 continue;
388
Mike Christie98e1b602013-06-14 04:56:12 -0500389 for (addr_i = 0; addr_i < na->addr_count; addr_i++) {
390 if (addr_compare(na->addr[addr_i], addr)) {
391 *nodeid = na->nodeid;
Alexander Aringe125fbe2021-03-01 17:05:09 -0500392 *mark = na->mark;
Mike Christie98e1b602013-06-14 04:56:12 -0500393 rv = 0;
394 goto unlock;
395 }
396 }
David Teigland36b71a82012-07-26 12:44:30 -0500397 }
Mike Christie98e1b602013-06-14 04:56:12 -0500398unlock:
David Teigland36b71a82012-07-26 12:44:30 -0500399 spin_unlock(&dlm_node_addrs_spin);
400 return rv;
401}
402
Alexander Aring4f19d072020-11-02 20:04:28 -0500403/* caller need to held dlm_node_addrs_spin lock */
404static bool dlm_lowcomms_na_has_addr(const struct dlm_node_addr *na,
405 const struct sockaddr_storage *addr)
406{
407 int i;
408
409 for (i = 0; i < na->addr_count; i++) {
410 if (addr_compare(na->addr[i], addr))
411 return true;
412 }
413
414 return false;
415}
416
David Teigland36b71a82012-07-26 12:44:30 -0500417int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr, int len)
418{
419 struct sockaddr_storage *new_addr;
420 struct dlm_node_addr *new_node, *na;
Alexander Aring4f19d072020-11-02 20:04:28 -0500421 bool ret;
David Teigland36b71a82012-07-26 12:44:30 -0500422
423 new_node = kzalloc(sizeof(struct dlm_node_addr), GFP_NOFS);
424 if (!new_node)
425 return -ENOMEM;
426
427 new_addr = kzalloc(sizeof(struct sockaddr_storage), GFP_NOFS);
428 if (!new_addr) {
429 kfree(new_node);
430 return -ENOMEM;
431 }
432
433 memcpy(new_addr, addr, len);
434
435 spin_lock(&dlm_node_addrs_spin);
436 na = find_node_addr(nodeid);
437 if (!na) {
438 new_node->nodeid = nodeid;
439 new_node->addr[0] = new_addr;
440 new_node->addr_count = 1;
Alexander Aringe125fbe2021-03-01 17:05:09 -0500441 new_node->mark = dlm_config.ci_mark;
David Teigland36b71a82012-07-26 12:44:30 -0500442 list_add(&new_node->list, &dlm_node_addrs);
443 spin_unlock(&dlm_node_addrs_spin);
444 return 0;
445 }
446
Alexander Aring4f19d072020-11-02 20:04:28 -0500447 ret = dlm_lowcomms_na_has_addr(na, addr);
448 if (ret) {
449 spin_unlock(&dlm_node_addrs_spin);
450 kfree(new_addr);
451 kfree(new_node);
452 return -EEXIST;
453 }
454
David Teigland36b71a82012-07-26 12:44:30 -0500455 if (na->addr_count >= DLM_MAX_ADDR_COUNT) {
456 spin_unlock(&dlm_node_addrs_spin);
457 kfree(new_addr);
458 kfree(new_node);
459 return -ENOSPC;
460 }
461
462 na->addr[na->addr_count++] = new_addr;
463 spin_unlock(&dlm_node_addrs_spin);
464 kfree(new_node);
465 return 0;
466}
467
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100468/* Data available on socket or listen socket received a connect */
David S. Miller676d2362014-04-11 16:15:36 -0400469static void lowcomms_data_ready(struct sock *sk)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100470{
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000471 struct connection *con;
472
473 read_lock_bh(&sk->sk_callback_lock);
474 con = sock2con(sk);
Patrick Caulfieldafb853f2007-06-01 10:07:26 -0500475 if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100476 queue_work(recv_workqueue, &con->rwork);
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000477 read_unlock_bh(&sk->sk_callback_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100478}
479
Alexander Aringd11ccd42020-11-02 20:04:25 -0500480static void lowcomms_listen_data_ready(struct sock *sk)
481{
482 queue_work(recv_workqueue, &listen_con.rwork);
483}
484
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100485static void lowcomms_write_space(struct sock *sk)
486{
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000487 struct connection *con;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100488
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000489 read_lock_bh(&sk->sk_callback_lock);
490 con = sock2con(sk);
David Millerb36930d2010-11-10 21:56:39 -0800491 if (!con)
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000492 goto out;
David Millerb36930d2010-11-10 21:56:39 -0800493
Alexander Aring19633c72020-11-02 20:04:20 -0500494 if (!test_and_set_bit(CF_CONNECTED, &con->flags)) {
495 log_print("successful connected to node %d", con->nodeid);
496 queue_work(send_workqueue, &con->swork);
497 goto out;
498 }
499
David Millerb36930d2010-11-10 21:56:39 -0800500 clear_bit(SOCK_NOSPACE, &con->sock->flags);
501
502 if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) {
503 con->sock->sk->sk_write_pending--;
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800504 clear_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags);
David Millerb36930d2010-11-10 21:56:39 -0800505 }
506
Bob Peterson01da24d2017-09-12 08:55:14 +0000507 queue_work(send_workqueue, &con->swork);
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000508out:
509 read_unlock_bh(&sk->sk_callback_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100510}
511
512static inline void lowcomms_connect_sock(struct connection *con)
513{
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -0500514 if (test_bit(CF_CLOSE, &con->flags))
515 return;
Bob Peterson61d9102b2017-09-12 08:55:04 +0000516 queue_work(send_workqueue, &con->swork);
517 cond_resched();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100518}
519
520static void lowcomms_state_change(struct sock *sk)
521{
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300522 /* SCTP layer is not calling sk_data_ready when the connection
523 * is done, so we catch the signal through here. Also, it
524 * doesn't switch socket state when entering shutdown, so we
525 * skip the write in that case.
526 */
527 if (sk->sk_shutdown) {
528 if (sk->sk_shutdown == RCV_SHUTDOWN)
529 lowcomms_data_ready(sk);
530 } else if (sk->sk_state == TCP_ESTABLISHED) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100531 lowcomms_write_space(sk);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300532 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100533}
534
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500535int dlm_lowcomms_connect_node(int nodeid)
536{
537 struct connection *con;
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400538 int idx;
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500539
540 if (nodeid == dlm_our_nodeid())
541 return 0;
542
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400543 idx = srcu_read_lock(&connections_srcu);
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500544 con = nodeid2con(nodeid, GFP_NOFS);
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400545 if (!con) {
546 srcu_read_unlock(&connections_srcu, idx);
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500547 return -ENOMEM;
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400548 }
549
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500550 lowcomms_connect_sock(con);
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400551 srcu_read_unlock(&connections_srcu, idx);
552
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500553 return 0;
554}
555
Alexander Aringe125fbe2021-03-01 17:05:09 -0500556int dlm_lowcomms_nodes_set_mark(int nodeid, unsigned int mark)
557{
558 struct dlm_node_addr *na;
559
560 spin_lock(&dlm_node_addrs_spin);
561 na = find_node_addr(nodeid);
562 if (!na) {
563 spin_unlock(&dlm_node_addrs_spin);
564 return -ENOENT;
565 }
566
567 na->mark = mark;
568 spin_unlock(&dlm_node_addrs_spin);
569
570 return 0;
571}
572
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500573static void lowcomms_error_report(struct sock *sk)
574{
Bob Petersonb81171c2016-02-05 14:39:02 -0500575 struct connection *con;
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500576 struct sockaddr_storage saddr;
Bob Petersonb81171c2016-02-05 14:39:02 -0500577 void (*orig_report)(struct sock *) = NULL;
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500578
Bob Petersonb81171c2016-02-05 14:39:02 -0500579 read_lock_bh(&sk->sk_callback_lock);
580 con = sock2con(sk);
581 if (con == NULL)
582 goto out;
583
Bob Petersoncc661fc2017-09-12 08:55:23 +0000584 orig_report = listen_sock.sk_error_report;
Bob Peterson1a318332016-01-18 12:29:15 -0500585 if (con->sock == NULL ||
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100586 kernel_getpeername(con->sock, (struct sockaddr *)&saddr) < 0) {
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500587 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
588 "sending to node %d, port %d, "
589 "sk_err=%d/%d\n", dlm_our_nodeid(),
590 con->nodeid, dlm_config.ci_tcp_port,
591 sk->sk_err, sk->sk_err_soft);
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500592 } else if (saddr.ss_family == AF_INET) {
593 struct sockaddr_in *sin4 = (struct sockaddr_in *)&saddr;
594
595 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
596 "sending to node %d at %pI4, port %d, "
597 "sk_err=%d/%d\n", dlm_our_nodeid(),
598 con->nodeid, &sin4->sin_addr.s_addr,
599 dlm_config.ci_tcp_port, sk->sk_err,
600 sk->sk_err_soft);
601 } else {
602 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&saddr;
603
604 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
605 "sending to node %d at %u.%u.%u.%u, "
606 "port %d, sk_err=%d/%d\n", dlm_our_nodeid(),
607 con->nodeid, sin6->sin6_addr.s6_addr32[0],
608 sin6->sin6_addr.s6_addr32[1],
609 sin6->sin6_addr.s6_addr32[2],
610 sin6->sin6_addr.s6_addr32[3],
611 dlm_config.ci_tcp_port, sk->sk_err,
612 sk->sk_err_soft);
613 }
Alexander Aringba868d92021-05-21 15:08:37 -0400614
615 /* below sendcon only handling */
616 if (test_bit(CF_IS_OTHERCON, &con->flags))
617 con = con->sendcon;
618
619 switch (sk->sk_err) {
620 case ECONNREFUSED:
621 set_bit(CF_DELAY_CONNECT, &con->flags);
622 break;
623 default:
624 break;
625 }
626
627 if (!test_and_set_bit(CF_RECONNECT, &con->flags))
628 queue_work(send_workqueue, &con->swork);
629
Bob Petersonb81171c2016-02-05 14:39:02 -0500630out:
631 read_unlock_bh(&sk->sk_callback_lock);
632 if (orig_report)
633 orig_report(sk);
634}
635
636/* Note: sk_callback_lock must be locked before calling this function. */
Bob Petersoncc661fc2017-09-12 08:55:23 +0000637static void save_listen_callbacks(struct socket *sock)
Bob Petersonb81171c2016-02-05 14:39:02 -0500638{
Bob Petersoncc661fc2017-09-12 08:55:23 +0000639 struct sock *sk = sock->sk;
640
641 listen_sock.sk_data_ready = sk->sk_data_ready;
642 listen_sock.sk_state_change = sk->sk_state_change;
643 listen_sock.sk_write_space = sk->sk_write_space;
644 listen_sock.sk_error_report = sk->sk_error_report;
Bob Petersonb81171c2016-02-05 14:39:02 -0500645}
646
Bob Petersoncc661fc2017-09-12 08:55:23 +0000647static void restore_callbacks(struct socket *sock)
Bob Petersonb81171c2016-02-05 14:39:02 -0500648{
Bob Petersoncc661fc2017-09-12 08:55:23 +0000649 struct sock *sk = sock->sk;
650
Bob Petersonb81171c2016-02-05 14:39:02 -0500651 write_lock_bh(&sk->sk_callback_lock);
Bob Petersonb81171c2016-02-05 14:39:02 -0500652 sk->sk_user_data = NULL;
Bob Petersoncc661fc2017-09-12 08:55:23 +0000653 sk->sk_data_ready = listen_sock.sk_data_ready;
654 sk->sk_state_change = listen_sock.sk_state_change;
655 sk->sk_write_space = listen_sock.sk_write_space;
656 sk->sk_error_report = listen_sock.sk_error_report;
Bob Petersonb81171c2016-02-05 14:39:02 -0500657 write_unlock_bh(&sk->sk_callback_lock);
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500658}
659
Alexander Aringd11ccd42020-11-02 20:04:25 -0500660static void add_listen_sock(struct socket *sock, struct listen_connection *con)
661{
662 struct sock *sk = sock->sk;
663
664 write_lock_bh(&sk->sk_callback_lock);
665 save_listen_callbacks(sock);
666 con->sock = sock;
667
668 sk->sk_user_data = con;
669 sk->sk_allocation = GFP_NOFS;
670 /* Install a data_ready callback */
671 sk->sk_data_ready = lowcomms_listen_data_ready;
672 write_unlock_bh(&sk->sk_callback_lock);
673}
674
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100675/* Make a socket active */
tsutomu.owa@toshiba.co.jp988419a2017-09-12 08:55:32 +0000676static void add_sock(struct socket *sock, struct connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100677{
Bob Petersonb81171c2016-02-05 14:39:02 -0500678 struct sock *sk = sock->sk;
679
680 write_lock_bh(&sk->sk_callback_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100681 con->sock = sock;
682
Bob Petersonb81171c2016-02-05 14:39:02 -0500683 sk->sk_user_data = con;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100684 /* Install a data_ready callback */
Bob Petersonb81171c2016-02-05 14:39:02 -0500685 sk->sk_data_ready = lowcomms_data_ready;
686 sk->sk_write_space = lowcomms_write_space;
687 sk->sk_state_change = lowcomms_state_change;
688 sk->sk_allocation = GFP_NOFS;
689 sk->sk_error_report = lowcomms_error_report;
690 write_unlock_bh(&sk->sk_callback_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100691}
692
693/* Add the port number to an IPv6 or 4 sockaddr and return the address
694 length */
695static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
696 int *addr_len)
697{
698 saddr->ss_family = dlm_local_addr[0]->ss_family;
699 if (saddr->ss_family == AF_INET) {
700 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
701 in4_addr->sin_port = cpu_to_be16(port);
702 *addr_len = sizeof(struct sockaddr_in);
703 memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
704 } else {
705 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
706 in6_addr->sin6_port = cpu_to_be16(port);
707 *addr_len = sizeof(struct sockaddr_in6);
708 }
Patrick Caulfield01c8cab2007-07-17 16:53:15 +0100709 memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100710}
711
Alexander Aringd11ccd42020-11-02 20:04:25 -0500712static void dlm_close_sock(struct socket **sock)
713{
714 if (*sock) {
715 restore_callbacks(*sock);
716 sock_release(*sock);
717 *sock = NULL;
718 }
719}
720
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100721/* Close a remote connection and tidy up */
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300722static void close_connection(struct connection *con, bool and_other,
723 bool tx, bool rx)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100724{
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +0000725 bool closing = test_and_set_bit(CF_CLOSING, &con->flags);
726
tsutomu.owa@toshiba.co.jp0aa18462017-09-12 09:02:02 +0000727 if (tx && !closing && cancel_work_sync(&con->swork)) {
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300728 log_print("canceled swork for node %d", con->nodeid);
tsutomu.owa@toshiba.co.jp0aa18462017-09-12 09:02:02 +0000729 clear_bit(CF_WRITE_PENDING, &con->flags);
730 }
731 if (rx && !closing && cancel_work_sync(&con->rwork)) {
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300732 log_print("canceled rwork for node %d", con->nodeid);
tsutomu.owa@toshiba.co.jp0aa18462017-09-12 09:02:02 +0000733 clear_bit(CF_READ_PENDING, &con->flags);
734 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100735
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300736 mutex_lock(&con->sock_mutex);
Alexander Aringd11ccd42020-11-02 20:04:25 -0500737 dlm_close_sock(&con->sock);
738
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100739 if (con->othercon && and_other) {
740 /* Will only re-enter once. */
Alexander Aringc6aa00e32021-05-21 15:08:38 -0400741 close_connection(con->othercon, false, tx, rx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100742 }
Patrick Caulfield9e5f2822007-08-02 14:58:14 +0100743
Alexander Aring4798cbb2020-09-24 10:31:26 -0400744 con->rx_leftover = 0;
Patrick Caulfield61d96be02007-08-20 15:13:38 +0100745 con->retries = 0;
Alexander Aring19633c72020-11-02 20:04:20 -0500746 clear_bit(CF_CONNECTED, &con->flags);
Alexander Aringba868d92021-05-21 15:08:37 -0400747 clear_bit(CF_DELAY_CONNECT, &con->flags);
748 clear_bit(CF_RECONNECT, &con->flags);
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400749 clear_bit(CF_EOF, &con->flags);
Patrick Caulfield61d96be02007-08-20 15:13:38 +0100750 mutex_unlock(&con->sock_mutex);
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +0000751 clear_bit(CF_CLOSING, &con->flags);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100752}
753
Alexander Aring055923b2020-07-27 09:13:38 -0400754static void shutdown_connection(struct connection *con)
755{
756 int ret;
757
Alexander Aringeec054b2021-03-01 17:05:19 -0500758 flush_work(&con->swork);
Alexander Aring055923b2020-07-27 09:13:38 -0400759
760 mutex_lock(&con->sock_mutex);
761 /* nothing to shutdown */
762 if (!con->sock) {
763 mutex_unlock(&con->sock_mutex);
764 return;
765 }
766
767 set_bit(CF_SHUTDOWN, &con->flags);
768 ret = kernel_sock_shutdown(con->sock, SHUT_WR);
769 mutex_unlock(&con->sock_mutex);
770 if (ret) {
771 log_print("Connection %p failed to shutdown: %d will force close",
772 con, ret);
773 goto force_close;
774 } else {
775 ret = wait_event_timeout(con->shutdown_wait,
776 !test_bit(CF_SHUTDOWN, &con->flags),
777 DLM_SHUTDOWN_WAIT_TIMEOUT);
778 if (ret == 0) {
779 log_print("Connection %p shutdown timed out, will force close",
780 con);
781 goto force_close;
782 }
783 }
784
785 return;
786
787force_close:
788 clear_bit(CF_SHUTDOWN, &con->flags);
789 close_connection(con, false, true, true);
790}
791
792static void dlm_tcp_shutdown(struct connection *con)
793{
794 if (con->othercon)
795 shutdown_connection(con->othercon);
796 shutdown_connection(con);
797}
798
Alexander Aring4798cbb2020-09-24 10:31:26 -0400799static int con_realloc_receive_buf(struct connection *con, int newlen)
800{
801 unsigned char *newbuf;
802
803 newbuf = kmalloc(newlen, GFP_NOFS);
804 if (!newbuf)
805 return -ENOMEM;
806
807 /* copy any leftover from last receive */
808 if (con->rx_leftover)
809 memmove(newbuf, con->rx_buf, con->rx_leftover);
810
811 /* swap to new buffer space */
812 kfree(con->rx_buf);
813 con->rx_buflen = newlen;
814 con->rx_buf = newbuf;
815
816 return 0;
817}
818
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100819/* Data received from remote end */
820static int receive_from_sock(struct connection *con)
821{
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100822 int call_again_soon = 0;
Alexander Aring4798cbb2020-09-24 10:31:26 -0400823 struct msghdr msg;
824 struct kvec iov;
825 int ret, buflen;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100826
827 mutex_lock(&con->sock_mutex);
828
829 if (con->sock == NULL) {
830 ret = -EAGAIN;
831 goto out_close;
832 }
Alexander Aring4798cbb2020-09-24 10:31:26 -0400833
Alexander Aring4798cbb2020-09-24 10:31:26 -0400834 /* realloc if we get new buffer size to read out */
835 buflen = dlm_config.ci_buffer_size;
836 if (con->rx_buflen != buflen && con->rx_leftover <= buflen) {
837 ret = con_realloc_receive_buf(con, buflen);
838 if (ret < 0)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100839 goto out_resched;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100840 }
841
Alexander Aring4798cbb2020-09-24 10:31:26 -0400842 /* calculate new buffer parameter regarding last receive and
843 * possible leftover bytes
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100844 */
Alexander Aring4798cbb2020-09-24 10:31:26 -0400845 iov.iov_base = con->rx_buf + con->rx_leftover;
846 iov.iov_len = con->rx_buflen - con->rx_leftover;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100847
Alexander Aring4798cbb2020-09-24 10:31:26 -0400848 memset(&msg, 0, sizeof(msg));
849 msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
850 ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len,
851 msg.msg_flags);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100852 if (ret <= 0)
853 goto out_close;
Alexander Aring4798cbb2020-09-24 10:31:26 -0400854 else if (ret == iov.iov_len)
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300855 call_again_soon = 1;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100856
Alexander Aring4798cbb2020-09-24 10:31:26 -0400857 /* 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;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100862
Alexander Aring4798cbb2020-09-24 10:31:26 -0400863 /* 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 call_again_soon = true;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100872 }
873
874 if (call_again_soon)
875 goto out_resched;
Alexander Aring4798cbb2020-09-24 10:31:26 -0400876
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100877 mutex_unlock(&con->sock_mutex);
878 return 0;
879
880out_resched:
881 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
882 queue_work(recv_workqueue, &con->rwork);
883 mutex_unlock(&con->sock_mutex);
884 return -EAGAIN;
885
886out_close:
Alexander Aringba868d92021-05-21 15:08:37 -0400887 if (ret == 0) {
Alexander Aringba868d92021-05-21 15:08:37 -0400888 log_print("connection %p got EOF from %d",
889 con, con->nodeid);
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400890
891 if (con->eof_condition && con->eof_condition(con)) {
892 set_bit(CF_EOF, &con->flags);
893 mutex_unlock(&con->sock_mutex);
894 } else {
895 mutex_unlock(&con->sock_mutex);
896 close_connection(con, false, true, false);
897
898 /* handling for tcp shutdown */
899 clear_bit(CF_SHUTDOWN, &con->flags);
900 wake_up(&con->shutdown_wait);
901 }
902
Alexander Aringba868d92021-05-21 15:08:37 -0400903 /* signal to breaking receive worker */
904 ret = -1;
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400905 } else {
906 mutex_unlock(&con->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100907 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100908 return ret;
909}
910
911/* Listening socket is busy, accept a connection */
Alexander Aringd11ccd42020-11-02 20:04:25 -0500912static int accept_from_sock(struct listen_connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100913{
914 int result;
915 struct sockaddr_storage peeraddr;
916 struct socket *newsock;
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400917 int len, idx;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100918 int nodeid;
919 struct connection *newcon;
920 struct connection *addcon;
Alexander Aring3f78cd72020-09-24 10:31:23 -0400921 unsigned int mark;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100922
David Teigland513ef592012-03-30 11:46:08 -0500923 if (!dlm_allow_conn) {
David Teigland513ef592012-03-30 11:46:08 -0500924 return -1;
925 }
David Teigland513ef592012-03-30 11:46:08 -0500926
Alexander Aringd11ccd42020-11-02 20:04:25 -0500927 if (!con->sock)
tsutomu.owa@toshiba.co.jp3421fb12017-09-12 09:01:38 +0000928 return -ENOTCONN;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100929
tsutomu.owa@toshiba.co.jp3421fb12017-09-12 09:01:38 +0000930 result = kernel_accept(con->sock, &newsock, O_NONBLOCK);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100931 if (result < 0)
932 goto accept_err;
933
934 /* Get the connected socket's peer */
935 memset(&peeraddr, 0, sizeof(peeraddr));
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100936 len = newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr, 2);
937 if (len < 0) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100938 result = -ECONNABORTED;
939 goto accept_err;
940 }
941
942 /* Get the new node's NODEID */
943 make_sockaddr(&peeraddr, 0, &len);
Alexander Aringe125fbe2021-03-01 17:05:09 -0500944 if (addr_to_nodeid(&peeraddr, &nodeid, &mark)) {
Masatake YAMATObcaadf52011-07-04 12:25:51 +0900945 unsigned char *b=(unsigned char *)&peeraddr;
David Teigland617e82e2007-04-26 13:46:49 -0500946 log_print("connect from non cluster node");
Masatake YAMATObcaadf52011-07-04 12:25:51 +0900947 print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
948 b, sizeof(struct sockaddr_storage));
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100949 sock_release(newsock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100950 return -1;
951 }
952
953 log_print("got connection from %d", nodeid);
954
955 /* Check to see if we already have a connection to this node. This
956 * could happen if the two nodes initiate a connection at roughly
957 * the same time and the connections cross on the wire.
958 * In this case we store the incoming one in "othercon"
959 */
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400960 idx = srcu_read_lock(&connections_srcu);
David Teigland748285c2009-05-15 10:50:57 -0500961 newcon = nodeid2con(nodeid, GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100962 if (!newcon) {
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400963 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100964 result = -ENOMEM;
965 goto accept_err;
966 }
Alexander Aringd11ccd42020-11-02 20:04:25 -0500967
Alexander Aringe125fbe2021-03-01 17:05:09 -0500968 sock_set_mark(newsock->sk, mark);
969
Alexander Aringd11ccd42020-11-02 20:04:25 -0500970 mutex_lock(&newcon->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100971 if (newcon->sock) {
972 struct connection *othercon = newcon->othercon;
973
974 if (!othercon) {
Alexander Aringa47666eb2020-08-27 15:02:49 -0400975 othercon = kzalloc(sizeof(*othercon), GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100976 if (!othercon) {
David Teigland617e82e2007-04-26 13:46:49 -0500977 log_print("failed to allocate incoming socket");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100978 mutex_unlock(&newcon->sock_mutex);
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400979 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100980 result = -ENOMEM;
981 goto accept_err;
982 }
Alexander Aring4798cbb2020-09-24 10:31:26 -0400983
Alexander Aring6cde2102020-11-02 20:04:21 -0500984 result = dlm_con_init(othercon, nodeid);
985 if (result < 0) {
Alexander Aring4798cbb2020-09-24 10:31:26 -0400986 kfree(othercon);
Yang Yingliang2fd8db22021-03-27 16:37:04 +0800987 mutex_unlock(&newcon->sock_mutex);
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400988 srcu_read_unlock(&connections_srcu, idx);
Alexander Aring4798cbb2020-09-24 10:31:26 -0400989 goto accept_err;
990 }
991
Alexander Aringe9a470a2021-03-01 17:05:11 -0500992 lockdep_set_subclass(&othercon->sock_mutex, 1);
Alexander Aring7443bc92021-05-21 15:08:36 -0400993 set_bit(CF_IS_OTHERCON, &othercon->flags);
Alexander Aring6cde2102020-11-02 20:04:21 -0500994 newcon->othercon = othercon;
Alexander Aringba868d92021-05-21 15:08:37 -0400995 othercon->sendcon = newcon;
Alexander Aringba3ab3c2020-07-27 09:13:37 -0400996 } else {
997 /* close other sock con if we have something new */
998 close_connection(othercon, false, true, false);
Patrick Caulfield61d96be02007-08-20 15:13:38 +0100999 }
Alexander Aringba3ab3c2020-07-27 09:13:37 -04001000
Alexander Aringe9a470a2021-03-01 17:05:11 -05001001 mutex_lock(&othercon->sock_mutex);
Alexander Aringba3ab3c2020-07-27 09:13:37 -04001002 add_sock(newsock, othercon);
1003 addcon = othercon;
1004 mutex_unlock(&othercon->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001005 }
1006 else {
Bob Peterson3735b4b2016-09-23 14:23:26 -04001007 /* accept copies the sk after we've saved the callbacks, so we
1008 don't want to save them a second time or comm errors will
1009 result in calling sk_error_report recursively. */
tsutomu.owa@toshiba.co.jp988419a2017-09-12 08:55:32 +00001010 add_sock(newsock, newcon);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001011 addcon = newcon;
1012 }
1013
Alexander Aringb30a6242021-03-01 17:05:10 -05001014 set_bit(CF_CONNECTED, &addcon->flags);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001015 mutex_unlock(&newcon->sock_mutex);
1016
1017 /*
1018 * Add it to the active queue in case we got data
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001019 * between processing the accept adding the socket
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001020 * to the read_sockets list
1021 */
1022 if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
1023 queue_work(recv_workqueue, &addcon->rwork);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001024
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001025 srcu_read_unlock(&connections_srcu, idx);
1026
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001027 return 0;
1028
1029accept_err:
tsutomu.owa@toshiba.co.jp3421fb12017-09-12 09:01:38 +00001030 if (newsock)
1031 sock_release(newsock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001032
1033 if (result != -EAGAIN)
David Teigland617e82e2007-04-26 13:46:49 -05001034 log_print("error accepting connection from node: %d", result);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001035 return result;
1036}
1037
Alexander Aring8f2dc782021-05-21 15:08:42 -04001038static void dlm_page_release(struct kref *kref)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001039{
Alexander Aring8f2dc782021-05-21 15:08:42 -04001040 struct writequeue_entry *e = container_of(kref, struct writequeue_entry,
1041 ref);
1042
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001043 __free_page(e->page);
1044 kfree(e);
1045}
1046
Alexander Aring8f2dc782021-05-21 15:08:42 -04001047static void dlm_msg_release(struct kref *kref)
1048{
1049 struct dlm_msg *msg = container_of(kref, struct dlm_msg, ref);
1050
1051 kref_put(&msg->entry->ref, dlm_page_release);
1052 kfree(msg);
1053}
1054
1055static void free_entry(struct writequeue_entry *e)
1056{
1057 struct dlm_msg *msg, *tmp;
1058
1059 list_for_each_entry_safe(msg, tmp, &e->msgs, list) {
Alexander Aring2874d1a2021-05-21 15:08:43 -04001060 if (msg->orig_msg) {
1061 msg->orig_msg->retransmit = false;
1062 kref_put(&msg->orig_msg->ref, dlm_msg_release);
1063 }
Alexander Aring8f2dc782021-05-21 15:08:42 -04001064 list_del(&msg->list);
1065 kref_put(&msg->ref, dlm_msg_release);
1066 }
1067
1068 list_del(&e->list);
1069 atomic_dec(&e->con->writequeue_cnt);
1070 kref_put(&e->ref, dlm_page_release);
1071}
1072
Mike Christie5d689872013-06-14 04:56:13 -05001073/*
1074 * writequeue_entry_complete - try to delete and free write queue entry
1075 * @e: write queue entry to try to delete
1076 * @completed: bytes completed
1077 *
1078 * writequeue_lock must be held.
1079 */
1080static void writequeue_entry_complete(struct writequeue_entry *e, int completed)
1081{
1082 e->offset += completed;
1083 e->len -= completed;
1084
Alexander Aring8f2dc782021-05-21 15:08:42 -04001085 if (e->len == 0 && e->users == 0)
Mike Christie5d689872013-06-14 04:56:13 -05001086 free_entry(e);
Mike Christie5d689872013-06-14 04:56:13 -05001087}
1088
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001089/*
1090 * sctp_bind_addrs - bind a SCTP socket to all our addresses
1091 */
Alexander Aring13004e82020-11-02 20:04:24 -05001092static int sctp_bind_addrs(struct socket *sock, uint16_t port)
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001093{
1094 struct sockaddr_storage localaddr;
Christoph Hellwigc0425a42020-05-29 14:09:42 +02001095 struct sockaddr *addr = (struct sockaddr *)&localaddr;
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001096 int i, addr_len, result = 0;
1097
1098 for (i = 0; i < dlm_local_count; i++) {
1099 memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
1100 make_sockaddr(&localaddr, port, &addr_len);
1101
1102 if (!i)
Alexander Aring13004e82020-11-02 20:04:24 -05001103 result = kernel_bind(sock, addr, addr_len);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001104 else
Alexander Aring13004e82020-11-02 20:04:24 -05001105 result = sock_bind_add(sock->sk, addr, addr_len);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001106
1107 if (result < 0) {
1108 log_print("Can't bind to %d addr number %d, %d.\n",
1109 port, i + 1, result);
1110 break;
1111 }
1112 }
1113 return result;
1114}
1115
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001116/* Initiate an SCTP association.
1117 This is a special case of send_to_sock() in that we don't yet have a
1118 peeled-off socket for this association, so we use the listening socket
1119 and add the primary IP address of the remote node.
1120 */
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001121static void sctp_connect_to_sock(struct connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001122{
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001123 struct sockaddr_storage daddr;
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001124 int result;
1125 int addr_len;
1126 struct socket *sock;
Alexander Aring9c9f1682020-06-26 13:26:50 -04001127 unsigned int mark;
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001128
Mike Christie5d689872013-06-14 04:56:13 -05001129 mutex_lock(&con->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001130
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001131 /* Some odd races can cause double-connects, ignore them */
1132 if (con->retries++ > MAX_CONNECT_RETRIES)
1133 goto out;
1134
1135 if (con->sock) {
1136 log_print("node %d already connected.", con->nodeid);
1137 goto out;
1138 }
1139
1140 memset(&daddr, 0, sizeof(daddr));
Alexander Aringe125fbe2021-03-01 17:05:09 -05001141 result = nodeid_to_addr(con->nodeid, &daddr, NULL, true, &mark);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001142 if (result < 0) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001143 log_print("no address for nodeid %d", con->nodeid);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001144 goto out;
David Teigland04bedd72009-09-18 14:31:47 -05001145 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001146
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001147 /* Create a socket to communicate with */
1148 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1149 SOCK_STREAM, IPPROTO_SCTP, &sock);
1150 if (result < 0)
1151 goto socket_err;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001152
Alexander Aring9c9f1682020-06-26 13:26:50 -04001153 sock_set_mark(sock->sk, mark);
1154
tsutomu.owa@toshiba.co.jp988419a2017-09-12 08:55:32 +00001155 add_sock(sock, con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001156
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001157 /* Bind to all addresses. */
Alexander Aring13004e82020-11-02 20:04:24 -05001158 if (sctp_bind_addrs(con->sock, 0))
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001159 goto bind_err;
Mike Christie98e1b602013-06-14 04:56:12 -05001160
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001161 make_sockaddr(&daddr, dlm_config.ci_tcp_port, &addr_len);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001162
Alexander Aring2df6b762021-05-21 15:08:34 -04001163 log_print_ratelimited("connecting to %d", con->nodeid);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001164
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001165 /* Turn off Nagle's algorithm */
Christoph Hellwig40ef92c2020-05-29 14:09:40 +02001166 sctp_sock_set_nodelay(sock->sk);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001167
Gang Hef706d832018-05-02 10:28:35 -05001168 /*
1169 * Make sock->ops->connect() function return in specified time,
1170 * since O_NONBLOCK argument in connect() function does not work here,
1171 * then, we should restore the default value of this attribute.
1172 */
Christoph Hellwig76ee0782020-05-28 07:12:12 +02001173 sock_set_sndtimeo(sock->sk, 5);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001174 result = sock->ops->connect(sock, (struct sockaddr *)&daddr, addr_len,
Gang Heda3627c2018-05-29 11:09:22 +08001175 0);
Christoph Hellwig76ee0782020-05-28 07:12:12 +02001176 sock_set_sndtimeo(sock->sk, 0);
Gang Hef706d832018-05-02 10:28:35 -05001177
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001178 if (result == -EINPROGRESS)
1179 result = 0;
Alexander Aring19633c72020-11-02 20:04:20 -05001180 if (result == 0) {
1181 if (!test_and_set_bit(CF_CONNECTED, &con->flags))
1182 log_print("successful connected to node %d", con->nodeid);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001183 goto out;
Alexander Aring19633c72020-11-02 20:04:20 -05001184 }
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001185
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001186bind_err:
1187 con->sock = NULL;
1188 sock_release(sock);
1189
1190socket_err:
1191 /*
1192 * Some errors are fatal and this list might need adjusting. For other
1193 * errors we try again until the max number of retries is reached.
1194 */
1195 if (result != -EHOSTUNREACH &&
1196 result != -ENETUNREACH &&
1197 result != -ENETDOWN &&
1198 result != -EINVAL &&
1199 result != -EPROTONOSUPPORT) {
1200 log_print("connect %d try %d error %d", con->nodeid,
1201 con->retries, result);
1202 mutex_unlock(&con->sock_mutex);
1203 msleep(1000);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001204 lowcomms_connect_sock(con);
1205 return;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001206 }
Mike Christie5d689872013-06-14 04:56:13 -05001207
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001208out:
Mike Christie5d689872013-06-14 04:56:13 -05001209 mutex_unlock(&con->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001210}
1211
1212/* Connect a new socket to its peer */
1213static void tcp_connect_to_sock(struct connection *con)
1214{
Lon Hohberger6bd8fed2007-10-25 18:51:54 -04001215 struct sockaddr_storage saddr, src_addr;
Alexander Aringe125fbe2021-03-01 17:05:09 -05001216 unsigned int mark;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001217 int addr_len;
Casey Dahlina89d63a2009-07-14 12:17:51 -05001218 struct socket *sock = NULL;
David Teigland36b71a82012-07-26 12:44:30 -05001219 int result;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001220
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001221 mutex_lock(&con->sock_mutex);
1222 if (con->retries++ > MAX_CONNECT_RETRIES)
1223 goto out;
1224
1225 /* Some odd races can cause double-connects, ignore them */
David Teigland36b71a82012-07-26 12:44:30 -05001226 if (con->sock)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001227 goto out;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001228
1229 /* Create a socket to communicate with */
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001230 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1231 SOCK_STREAM, IPPROTO_TCP, &sock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001232 if (result < 0)
1233 goto out_err;
1234
1235 memset(&saddr, 0, sizeof(saddr));
Alexander Aringe125fbe2021-03-01 17:05:09 -05001236 result = nodeid_to_addr(con->nodeid, &saddr, NULL, false, &mark);
David Teigland36b71a82012-07-26 12:44:30 -05001237 if (result < 0) {
1238 log_print("no address for nodeid %d", con->nodeid);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001239 goto out_err;
David Teigland36b71a82012-07-26 12:44:30 -05001240 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001241
Alexander Aringe125fbe2021-03-01 17:05:09 -05001242 sock_set_mark(sock->sk, mark);
1243
tsutomu.owa@toshiba.co.jp988419a2017-09-12 08:55:32 +00001244 add_sock(sock, con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001245
Lon Hohberger6bd8fed2007-10-25 18:51:54 -04001246 /* Bind to our cluster-known address connecting to avoid
1247 routing problems */
1248 memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
1249 make_sockaddr(&src_addr, 0, &addr_len);
1250 result = sock->ops->bind(sock, (struct sockaddr *) &src_addr,
1251 addr_len);
1252 if (result < 0) {
1253 log_print("could not bind for connect: %d", result);
1254 /* This *may* not indicate a critical error */
1255 }
1256
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001257 make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
1258
Alexander Aring2df6b762021-05-21 15:08:34 -04001259 log_print_ratelimited("connecting to %d", con->nodeid);
David Teiglandcb2d45d2010-11-12 11:12:55 -06001260
1261 /* Turn off Nagle's algorithm */
Christoph Hellwig12abc5e2020-05-28 07:12:19 +02001262 tcp_sock_set_nodelay(sock->sk);
David Teiglandcb2d45d2010-11-12 11:12:55 -06001263
David Teigland36b71a82012-07-26 12:44:30 -05001264 result = sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001265 O_NONBLOCK);
1266 if (result == -EINPROGRESS)
1267 result = 0;
1268 if (result == 0)
1269 goto out;
1270
1271out_err:
1272 if (con->sock) {
1273 sock_release(con->sock);
1274 con->sock = NULL;
Casey Dahlina89d63a2009-07-14 12:17:51 -05001275 } else if (sock) {
1276 sock_release(sock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001277 }
1278 /*
1279 * Some errors are fatal and this list might need adjusting. For other
1280 * errors we try again until the max number of retries is reached.
1281 */
David Teigland36b71a82012-07-26 12:44:30 -05001282 if (result != -EHOSTUNREACH &&
1283 result != -ENETUNREACH &&
1284 result != -ENETDOWN &&
1285 result != -EINVAL &&
1286 result != -EPROTONOSUPPORT) {
1287 log_print("connect %d try %d error %d", con->nodeid,
1288 con->retries, result);
1289 mutex_unlock(&con->sock_mutex);
1290 msleep(1000);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001291 lowcomms_connect_sock(con);
David Teigland36b71a82012-07-26 12:44:30 -05001292 return;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001293 }
1294out:
1295 mutex_unlock(&con->sock_mutex);
1296 return;
1297}
1298
Alexander Aringd11ccd42020-11-02 20:04:25 -05001299/* On error caller must run dlm_close_sock() for the
1300 * listen connection socket.
1301 */
1302static int tcp_create_listen_sock(struct listen_connection *con,
1303 struct sockaddr_storage *saddr)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001304{
1305 struct socket *sock = NULL;
1306 int result = 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001307 int addr_len;
1308
1309 if (dlm_local_addr[0]->ss_family == AF_INET)
1310 addr_len = sizeof(struct sockaddr_in);
1311 else
1312 addr_len = sizeof(struct sockaddr_in6);
1313
1314 /* Create a socket to communicate with */
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001315 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1316 SOCK_STREAM, IPPROTO_TCP, &sock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001317 if (result < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001318 log_print("Can't create listening comms socket");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001319 goto create_out;
1320 }
1321
Alexander Aringa5b7ab62020-06-26 13:26:49 -04001322 sock_set_mark(sock->sk, dlm_config.ci_mark);
1323
David Teiglandcb2d45d2010-11-12 11:12:55 -06001324 /* Turn off Nagle's algorithm */
Christoph Hellwig12abc5e2020-05-28 07:12:19 +02001325 tcp_sock_set_nodelay(sock->sk);
David Teiglandcb2d45d2010-11-12 11:12:55 -06001326
Christoph Hellwigb58f0e82020-05-28 07:12:09 +02001327 sock_set_reuseaddr(sock->sk);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001328
Alexander Aringd11ccd42020-11-02 20:04:25 -05001329 add_listen_sock(sock, con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001330
1331 /* Bind to our port */
1332 make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
1333 result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
1334 if (result < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001335 log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001336 goto create_out;
1337 }
Christoph Hellwigce3d9542020-05-28 07:12:15 +02001338 sock_set_keepalive(sock->sk);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001339
1340 result = sock->ops->listen(sock, 5);
1341 if (result < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001342 log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001343 goto create_out;
1344 }
1345
Alexander Aringd11ccd42020-11-02 20:04:25 -05001346 return 0;
1347
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001348create_out:
Alexander Aringd11ccd42020-11-02 20:04:25 -05001349 return result;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001350}
1351
1352/* Get local addresses */
1353static void init_local(void)
1354{
1355 struct sockaddr_storage sas, *addr;
1356 int i;
1357
Patrick Caulfield30d3a232007-04-23 16:26:21 +01001358 dlm_local_count = 0;
David Teigland1b189b82012-03-21 09:18:34 -05001359 for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001360 if (dlm_our_addr(&sas, i))
1361 break;
1362
Amitoj Kaur Chawla5c93f562016-06-23 10:22:01 +05301363 addr = kmemdup(&sas, sizeof(*addr), GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001364 if (!addr)
1365 break;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001366 dlm_local_addr[dlm_local_count++] = addr;
1367 }
1368}
1369
Alexander Aring043697f2020-08-27 15:02:50 -04001370static void deinit_local(void)
1371{
1372 int i;
1373
1374 for (i = 0; i < dlm_local_count; i++)
1375 kfree(dlm_local_addr[i]);
1376}
1377
Alexander Aringd11ccd42020-11-02 20:04:25 -05001378/* Initialise SCTP socket and bind to all interfaces
1379 * On error caller must run dlm_close_sock() for the
1380 * listen connection socket.
1381 */
1382static int sctp_listen_for_all(struct listen_connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001383{
1384 struct socket *sock = NULL;
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001385 int result = -EINVAL;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001386
1387 log_print("Using SCTP for communications");
1388
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001389 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001390 SOCK_STREAM, IPPROTO_SCTP, &sock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001391 if (result < 0) {
1392 log_print("Can't create comms socket, check SCTP is loaded");
1393 goto out;
1394 }
1395
Christoph Hellwig26cfabf2020-05-28 07:12:16 +02001396 sock_set_rcvbuf(sock->sk, NEEDED_RMEM);
Alexander Aringa5b7ab62020-06-26 13:26:49 -04001397 sock_set_mark(sock->sk, dlm_config.ci_mark);
Christoph Hellwig40ef92c2020-05-29 14:09:40 +02001398 sctp_sock_set_nodelay(sock->sk);
Mike Christie86e92ad2013-06-14 04:56:14 -05001399
Alexander Aringd11ccd42020-11-02 20:04:25 -05001400 add_listen_sock(sock, con);
Bob Petersonb81171c2016-02-05 14:39:02 -05001401
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001402 /* Bind to all addresses. */
Alexander Aringd11ccd42020-11-02 20:04:25 -05001403 result = sctp_bind_addrs(con->sock, dlm_config.ci_tcp_port);
1404 if (result < 0)
1405 goto out;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001406
1407 result = sock->ops->listen(sock, 5);
1408 if (result < 0) {
1409 log_print("Can't set socket listening");
Alexander Aringd11ccd42020-11-02 20:04:25 -05001410 goto out;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001411 }
1412
1413 return 0;
1414
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001415out:
1416 return result;
1417}
1418
1419static int tcp_listen_for_all(void)
1420{
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001421 /* We don't support multi-homed hosts */
Alexander Aring1a26bfa2020-11-02 20:04:26 -05001422 if (dlm_local_count > 1) {
David Teigland617e82e2007-04-26 13:46:49 -05001423 log_print("TCP protocol can't handle multi-homed hosts, "
1424 "try SCTP");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001425 return -EINVAL;
1426 }
1427
1428 log_print("Using TCP for communications");
1429
Alexander Aringd11ccd42020-11-02 20:04:25 -05001430 return tcp_create_listen_sock(&listen_con, dlm_local_addr[0]);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001431}
1432
1433
1434
1435static struct writequeue_entry *new_writequeue_entry(struct connection *con,
1436 gfp_t allocation)
1437{
1438 struct writequeue_entry *entry;
1439
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001440 entry = kzalloc(sizeof(*entry), allocation);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001441 if (!entry)
1442 return NULL;
1443
Alexander Aringe1a7cbc2021-03-01 17:05:15 -05001444 entry->page = alloc_page(allocation | __GFP_ZERO);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001445 if (!entry->page) {
1446 kfree(entry);
1447 return NULL;
1448 }
1449
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001450 entry->con = con;
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001451 entry->users = 1;
Alexander Aring8f2dc782021-05-21 15:08:42 -04001452 kref_init(&entry->ref);
1453 INIT_LIST_HEAD(&entry->msgs);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001454
1455 return entry;
1456}
1457
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001458static struct writequeue_entry *new_wq_entry(struct connection *con, int len,
Alexander Aring8f2dc782021-05-21 15:08:42 -04001459 gfp_t allocation, char **ppc,
1460 void (*cb)(struct dlm_mhandle *mh),
1461 struct dlm_mhandle *mh)
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001462{
1463 struct writequeue_entry *e;
1464
1465 spin_lock(&con->writequeue_lock);
1466 if (!list_empty(&con->writequeue)) {
1467 e = list_last_entry(&con->writequeue, struct writequeue_entry, list);
1468 if (DLM_WQ_REMAIN_BYTES(e) >= len) {
Alexander Aring8f2dc782021-05-21 15:08:42 -04001469 kref_get(&e->ref);
1470
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001471 *ppc = page_address(e->page) + e->end;
Alexander Aring8f2dc782021-05-21 15:08:42 -04001472 if (cb)
1473 cb(mh);
1474
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001475 e->end += len;
1476 e->users++;
1477 spin_unlock(&con->writequeue_lock);
1478
1479 return e;
1480 }
1481 }
1482 spin_unlock(&con->writequeue_lock);
1483
1484 e = new_writequeue_entry(con, allocation);
1485 if (!e)
1486 return NULL;
1487
Alexander Aring8f2dc782021-05-21 15:08:42 -04001488 kref_get(&e->ref);
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001489 *ppc = page_address(e->page);
1490 e->end += len;
Alexander Aring8aa31cb2021-05-21 15:08:39 -04001491 atomic_inc(&con->writequeue_cnt);
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001492
1493 spin_lock(&con->writequeue_lock);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001494 if (cb)
1495 cb(mh);
1496
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001497 list_add_tail(&e->list, &con->writequeue);
1498 spin_unlock(&con->writequeue_lock);
1499
1500 return e;
1501};
1502
Alexander Aring2874d1a2021-05-21 15:08:43 -04001503static struct dlm_msg *dlm_lowcomms_new_msg_con(struct connection *con, int len,
1504 gfp_t allocation, char **ppc,
1505 void (*cb)(struct dlm_mhandle *mh),
1506 struct dlm_mhandle *mh)
1507{
1508 struct writequeue_entry *e;
1509 struct dlm_msg *msg;
1510
1511 msg = kzalloc(sizeof(*msg), allocation);
1512 if (!msg)
1513 return NULL;
1514
1515 kref_init(&msg->ref);
1516
1517 e = new_wq_entry(con, len, allocation, ppc, cb, mh);
1518 if (!e) {
1519 kfree(msg);
1520 return NULL;
1521 }
1522
1523 msg->ppc = *ppc;
1524 msg->len = len;
1525 msg->entry = e;
1526
1527 return msg;
1528}
1529
Alexander Aring8f2dc782021-05-21 15:08:42 -04001530struct dlm_msg *dlm_lowcomms_new_msg(int nodeid, int len, gfp_t allocation,
1531 char **ppc, void (*cb)(struct dlm_mhandle *mh),
1532 struct dlm_mhandle *mh)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001533{
1534 struct connection *con;
Alexander Aring8f2dc782021-05-21 15:08:42 -04001535 struct dlm_msg *msg;
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001536 int idx;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001537
Alexander Aringc45674f2021-03-01 17:05:14 -05001538 if (len > DEFAULT_BUFFER_SIZE ||
1539 len < sizeof(struct dlm_header)) {
1540 BUILD_BUG_ON(PAGE_SIZE < DEFAULT_BUFFER_SIZE);
Alexander Aring692f51c2020-11-02 20:04:18 -05001541 log_print("failed to allocate a buffer of size %d", len);
Alexander Aringc45674f2021-03-01 17:05:14 -05001542 WARN_ON(1);
Alexander Aring692f51c2020-11-02 20:04:18 -05001543 return NULL;
1544 }
1545
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001546 idx = srcu_read_lock(&connections_srcu);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001547 con = nodeid2con(nodeid, allocation);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001548 if (!con) {
1549 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001550 return NULL;
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001551 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001552
Alexander Aring2874d1a2021-05-21 15:08:43 -04001553 msg = dlm_lowcomms_new_msg_con(con, len, allocation, ppc, cb, mh);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001554 if (!msg) {
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001555 srcu_read_unlock(&connections_srcu, idx);
1556 return NULL;
1557 }
1558
Alexander Aring8f2dc782021-05-21 15:08:42 -04001559 /* we assume if successful commit must called */
1560 msg->idx = idx;
Alexander Aring8f2dc782021-05-21 15:08:42 -04001561 return msg;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001562}
1563
Alexander Aring2874d1a2021-05-21 15:08:43 -04001564static void _dlm_lowcomms_commit_msg(struct dlm_msg *msg)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001565{
Alexander Aring8f2dc782021-05-21 15:08:42 -04001566 struct writequeue_entry *e = msg->entry;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001567 struct connection *con = e->con;
1568 int users;
1569
1570 spin_lock(&con->writequeue_lock);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001571 kref_get(&msg->ref);
1572 list_add(&msg->list, &e->msgs);
1573
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001574 users = --e->users;
1575 if (users)
1576 goto out;
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001577
1578 e->len = DLM_WQ_LENGTH_BYTES(e);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001579 spin_unlock(&con->writequeue_lock);
1580
Bob Peterson01da24d2017-09-12 08:55:14 +00001581 queue_work(send_workqueue, &con->swork);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001582 return;
1583
1584out:
1585 spin_unlock(&con->writequeue_lock);
1586 return;
1587}
1588
Alexander Aring2874d1a2021-05-21 15:08:43 -04001589void dlm_lowcomms_commit_msg(struct dlm_msg *msg)
1590{
1591 _dlm_lowcomms_commit_msg(msg);
1592 srcu_read_unlock(&connections_srcu, msg->idx);
1593}
1594
Alexander Aring8f2dc782021-05-21 15:08:42 -04001595void dlm_lowcomms_put_msg(struct dlm_msg *msg)
1596{
1597 kref_put(&msg->ref, dlm_msg_release);
1598}
1599
Alexander Aring2874d1a2021-05-21 15:08:43 -04001600/* does not held connections_srcu, usage workqueue only */
1601int dlm_lowcomms_resend_msg(struct dlm_msg *msg)
1602{
1603 struct dlm_msg *msg_resend;
1604 char *ppc;
1605
1606 if (msg->retransmit)
1607 return 1;
1608
1609 msg_resend = dlm_lowcomms_new_msg_con(msg->entry->con, msg->len,
1610 GFP_ATOMIC, &ppc, NULL, NULL);
1611 if (!msg_resend)
1612 return -ENOMEM;
1613
1614 msg->retransmit = true;
1615 kref_get(&msg->ref);
1616 msg_resend->orig_msg = msg;
1617
1618 memcpy(ppc, msg->ppc, msg->len);
1619 _dlm_lowcomms_commit_msg(msg_resend);
1620 dlm_lowcomms_put_msg(msg_resend);
1621
1622 return 0;
1623}
1624
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001625/* Send a message */
1626static void send_to_sock(struct connection *con)
1627{
1628 int ret = 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001629 const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1630 struct writequeue_entry *e;
1631 int len, offset;
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001632 int count = 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001633
1634 mutex_lock(&con->sock_mutex);
1635 if (con->sock == NULL)
1636 goto out_connect;
1637
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001638 spin_lock(&con->writequeue_lock);
1639 for (;;) {
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001640 if (list_empty(&con->writequeue))
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001641 break;
1642
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001643 e = list_first_entry(&con->writequeue, struct writequeue_entry, list);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001644 len = e->len;
1645 offset = e->offset;
1646 BUG_ON(len == 0 && e->users == 0);
1647 spin_unlock(&con->writequeue_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001648
1649 ret = 0;
1650 if (len) {
Paolo Bonzini1329e3f2009-08-24 13:18:04 -05001651 ret = kernel_sendpage(con->sock, e->page, offset, len,
1652 msg_flags);
Patrick Caulfieldd66f8272007-09-14 08:49:21 +01001653 if (ret == -EAGAIN || ret == 0) {
David Millerb36930d2010-11-10 21:56:39 -08001654 if (ret == -EAGAIN &&
Eric Dumazet9cd3e072015-11-29 20:03:10 -08001655 test_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags) &&
David Millerb36930d2010-11-10 21:56:39 -08001656 !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
1657 /* Notify TCP that we're limited by the
1658 * application window size.
1659 */
1660 set_bit(SOCK_NOSPACE, &con->sock->flags);
1661 con->sock->sk->sk_write_pending++;
1662 }
Patrick Caulfieldd66f8272007-09-14 08:49:21 +01001663 cond_resched();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001664 goto out;
Ying Xue9c5bef52012-08-13 14:29:55 +08001665 } else if (ret < 0)
Alexander Aringba868d92021-05-21 15:08:37 -04001666 goto out;
Patrick Caulfieldd66f8272007-09-14 08:49:21 +01001667 }
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001668
1669 /* Don't starve people filling buffers */
1670 if (++count >= MAX_SEND_MSG_COUNT) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001671 cond_resched();
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001672 count = 0;
1673 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001674
1675 spin_lock(&con->writequeue_lock);
Mike Christie5d689872013-06-14 04:56:13 -05001676 writequeue_entry_complete(e, ret);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001677 }
1678 spin_unlock(&con->writequeue_lock);
Alexander Aring8aa31cb2021-05-21 15:08:39 -04001679
1680 /* close if we got EOF */
1681 if (test_and_clear_bit(CF_EOF, &con->flags)) {
1682 mutex_unlock(&con->sock_mutex);
1683 close_connection(con, false, false, true);
1684
1685 /* handling for tcp shutdown */
1686 clear_bit(CF_SHUTDOWN, &con->flags);
1687 wake_up(&con->shutdown_wait);
1688 } else {
1689 mutex_unlock(&con->sock_mutex);
1690 }
1691
1692 return;
1693
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001694out:
1695 mutex_unlock(&con->sock_mutex);
1696 return;
1697
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001698out_connect:
1699 mutex_unlock(&con->sock_mutex);
Bob Peterson01da24d2017-09-12 08:55:14 +00001700 queue_work(send_workqueue, &con->swork);
1701 cond_resched();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001702}
1703
1704static void clean_one_writequeue(struct connection *con)
1705{
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001706 struct writequeue_entry *e, *safe;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001707
1708 spin_lock(&con->writequeue_lock);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001709 list_for_each_entry_safe(e, safe, &con->writequeue, list) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001710 free_entry(e);
1711 }
1712 spin_unlock(&con->writequeue_lock);
1713}
1714
1715/* Called from recovery when it knows that a node has
1716 left the cluster */
1717int dlm_lowcomms_close(int nodeid)
1718{
1719 struct connection *con;
David Teigland36b71a82012-07-26 12:44:30 -05001720 struct dlm_node_addr *na;
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001721 int idx;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001722
1723 log_print("closing connection to node %d", nodeid);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001724 idx = srcu_read_lock(&connections_srcu);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001725 con = nodeid2con(nodeid, 0);
1726 if (con) {
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -05001727 set_bit(CF_CLOSE, &con->flags);
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -03001728 close_connection(con, true, true, true);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001729 clean_one_writequeue(con);
Alexander Aring53a5eda2020-11-02 20:04:19 -05001730 if (con->othercon)
1731 clean_one_writequeue(con->othercon);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001732 }
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001733 srcu_read_unlock(&connections_srcu, idx);
David Teigland36b71a82012-07-26 12:44:30 -05001734
1735 spin_lock(&dlm_node_addrs_spin);
1736 na = find_node_addr(nodeid);
1737 if (na) {
1738 list_del(&na->list);
1739 while (na->addr_count--)
1740 kfree(na->addr[na->addr_count]);
1741 kfree(na);
1742 }
1743 spin_unlock(&dlm_node_addrs_spin);
1744
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001745 return 0;
1746}
1747
1748/* Receive workqueue function */
1749static void process_recv_sockets(struct work_struct *work)
1750{
1751 struct connection *con = container_of(work, struct connection, rwork);
1752 int err;
1753
1754 clear_bit(CF_READ_PENDING, &con->flags);
1755 do {
Alexander Aringd11ccd42020-11-02 20:04:25 -05001756 err = receive_from_sock(con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001757 } while (!err);
1758}
1759
Alexander Aringd11ccd42020-11-02 20:04:25 -05001760static void process_listen_recv_socket(struct work_struct *work)
1761{
1762 accept_from_sock(&listen_con);
1763}
1764
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001765/* Send workqueue function */
1766static void process_send_sockets(struct work_struct *work)
1767{
1768 struct connection *con = container_of(work, struct connection, swork);
1769
Alexander Aring7443bc92021-05-21 15:08:36 -04001770 WARN_ON(test_bit(CF_IS_OTHERCON, &con->flags));
1771
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001772 clear_bit(CF_WRITE_PENDING, &con->flags);
Alexander Aringba868d92021-05-21 15:08:37 -04001773
1774 if (test_and_clear_bit(CF_RECONNECT, &con->flags))
1775 close_connection(con, false, false, true);
1776
1777 if (con->sock == NULL) { /* not mutex protected so check it inside too */
1778 if (test_and_clear_bit(CF_DELAY_CONNECT, &con->flags))
1779 msleep(1000);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001780 con->connect_action(con);
Alexander Aringba868d92021-05-21 15:08:37 -04001781 }
Bob Peterson01da24d2017-09-12 08:55:14 +00001782 if (!list_empty(&con->writequeue))
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -05001783 send_to_sock(con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001784}
1785
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001786static void work_stop(void)
1787{
David Windsorb3555162019-04-02 08:37:10 -04001788 if (recv_workqueue)
1789 destroy_workqueue(recv_workqueue);
1790 if (send_workqueue)
1791 destroy_workqueue(send_workqueue);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001792}
1793
1794static int work_start(void)
1795{
David Teiglande43f0552011-03-10 13:22:34 -06001796 recv_workqueue = alloc_workqueue("dlm_recv",
1797 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001798 if (!recv_workqueue) {
1799 log_print("can't start dlm_recv");
1800 return -ENOMEM;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001801 }
1802
David Teiglande43f0552011-03-10 13:22:34 -06001803 send_workqueue = alloc_workqueue("dlm_send",
1804 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001805 if (!send_workqueue) {
1806 log_print("can't start dlm_send");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001807 destroy_workqueue(recv_workqueue);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001808 return -ENOMEM;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001809 }
1810
1811 return 0;
1812}
1813
Alexander Aring9d232462021-03-01 17:05:20 -05001814static void shutdown_conn(struct connection *con)
1815{
1816 if (con->shutdown_action)
1817 con->shutdown_action(con);
1818}
1819
1820void dlm_lowcomms_shutdown(void)
1821{
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001822 int idx;
1823
Alexander Aring9d232462021-03-01 17:05:20 -05001824 /* Set all the flags to prevent any
1825 * socket activity.
1826 */
1827 dlm_allow_conn = 0;
1828
1829 if (recv_workqueue)
1830 flush_workqueue(recv_workqueue);
1831 if (send_workqueue)
1832 flush_workqueue(send_workqueue);
1833
1834 dlm_close_sock(&listen_con.sock);
1835
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001836 idx = srcu_read_lock(&connections_srcu);
Alexander Aring9d232462021-03-01 17:05:20 -05001837 foreach_conn(shutdown_conn);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001838 srcu_read_unlock(&connections_srcu, idx);
Alexander Aring9d232462021-03-01 17:05:20 -05001839}
1840
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001841static void _stop_conn(struct connection *con, bool and_other)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001842{
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001843 mutex_lock(&con->sock_mutex);
tsutomu.owa@toshiba.co.jp173a31f2017-09-12 09:01:24 +00001844 set_bit(CF_CLOSE, &con->flags);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001845 set_bit(CF_READ_PENDING, &con->flags);
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001846 set_bit(CF_WRITE_PENDING, &con->flags);
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +00001847 if (con->sock && con->sock->sk) {
1848 write_lock_bh(&con->sock->sk->sk_callback_lock);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001849 con->sock->sk->sk_user_data = NULL;
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +00001850 write_unlock_bh(&con->sock->sk->sk_callback_lock);
1851 }
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001852 if (con->othercon && and_other)
1853 _stop_conn(con->othercon, false);
1854 mutex_unlock(&con->sock_mutex);
1855}
1856
1857static void stop_conn(struct connection *con)
1858{
1859 _stop_conn(con, true);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001860}
1861
Alexander Aring4798cbb2020-09-24 10:31:26 -04001862static void connection_release(struct rcu_head *rcu)
1863{
1864 struct connection *con = container_of(rcu, struct connection, rcu);
1865
1866 kfree(con->rx_buf);
1867 kfree(con);
1868}
1869
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001870static void free_conn(struct connection *con)
1871{
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -03001872 close_connection(con, true, true, true);
Alexander Aringa47666eb2020-08-27 15:02:49 -04001873 spin_lock(&connections_lock);
1874 hlist_del_rcu(&con->list);
1875 spin_unlock(&connections_lock);
Alexander Aring948c47e2020-08-27 15:02:53 -04001876 if (con->othercon) {
1877 clean_one_writequeue(con->othercon);
Alexander Aring5cbec202020-11-02 20:04:16 -05001878 call_srcu(&connections_srcu, &con->othercon->rcu,
1879 connection_release);
Alexander Aring948c47e2020-08-27 15:02:53 -04001880 }
Alexander Aring0de98432020-08-27 15:02:52 -04001881 clean_one_writequeue(con);
Alexander Aring5cbec202020-11-02 20:04:16 -05001882 call_srcu(&connections_srcu, &con->rcu, connection_release);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001883}
1884
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001885static void work_flush(void)
1886{
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001887 int ok;
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001888 int i;
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001889 struct connection *con;
1890
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001891 do {
1892 ok = 1;
1893 foreach_conn(stop_conn);
David Windsorb3555162019-04-02 08:37:10 -04001894 if (recv_workqueue)
1895 flush_workqueue(recv_workqueue);
1896 if (send_workqueue)
1897 flush_workqueue(send_workqueue);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001898 for (i = 0; i < CONN_HASH_SIZE && ok; i++) {
Alexander Aringa47666eb2020-08-27 15:02:49 -04001899 hlist_for_each_entry_rcu(con, &connection_hash[i],
1900 list) {
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001901 ok &= test_bit(CF_READ_PENDING, &con->flags);
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001902 ok &= test_bit(CF_WRITE_PENDING, &con->flags);
1903 if (con->othercon) {
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001904 ok &= test_bit(CF_READ_PENDING,
1905 &con->othercon->flags);
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001906 ok &= test_bit(CF_WRITE_PENDING,
1907 &con->othercon->flags);
1908 }
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001909 }
1910 }
1911 } while (!ok);
1912}
1913
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001914void dlm_lowcomms_stop(void)
1915{
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001916 int idx;
1917
1918 idx = srcu_read_lock(&connections_srcu);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001919 work_flush();
Marcelo Ricardo Leitner3a8db792016-10-08 10:14:37 -03001920 foreach_conn(free_conn);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001921 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001922 work_stop();
Alexander Aring043697f2020-08-27 15:02:50 -04001923 deinit_local();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001924}
1925
1926int dlm_lowcomms_start(void)
1927{
1928 int error = -EINVAL;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001929 int i;
1930
1931 for (i = 0; i < CONN_HASH_SIZE; i++)
1932 INIT_HLIST_HEAD(&connection_hash[i]);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001933
1934 init_local();
1935 if (!dlm_local_count) {
David Teigland617e82e2007-04-26 13:46:49 -05001936 error = -ENOTCONN;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001937 log_print("no local IP address has been set");
David Teigland513ef592012-03-30 11:46:08 -05001938 goto fail;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001939 }
1940
Alexander Aringd11ccd42020-11-02 20:04:25 -05001941 INIT_WORK(&listen_con.rwork, process_listen_recv_socket);
1942
David Teigland513ef592012-03-30 11:46:08 -05001943 error = work_start();
1944 if (error)
Alexander Aringa47666eb2020-08-27 15:02:49 -04001945 goto fail;
David Teigland513ef592012-03-30 11:46:08 -05001946
1947 dlm_allow_conn = 1;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001948
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001949 /* Start listening */
1950 if (dlm_config.ci_protocol == 0)
1951 error = tcp_listen_for_all();
1952 else
Alexander Aringd11ccd42020-11-02 20:04:25 -05001953 error = sctp_listen_for_all(&listen_con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001954 if (error)
1955 goto fail_unlisten;
1956
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001957 return 0;
1958
1959fail_unlisten:
David Teigland513ef592012-03-30 11:46:08 -05001960 dlm_allow_conn = 0;
Alexander Aringd11ccd42020-11-02 20:04:25 -05001961 dlm_close_sock(&listen_con.sock);
David Teigland513ef592012-03-30 11:46:08 -05001962fail:
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001963 return error;
1964}
David Teigland36b71a82012-07-26 12:44:30 -05001965
1966void dlm_lowcomms_exit(void)
1967{
1968 struct dlm_node_addr *na, *safe;
1969
1970 spin_lock(&dlm_node_addrs_spin);
1971 list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) {
1972 list_del(&na->list);
1973 while (na->addr_count--)
1974 kfree(na->addr[na->addr_count]);
1975 kfree(na);
1976 }
1977 spin_unlock(&dlm_node_addrs_spin);
1978}