blob: 051f22dbb83aa24c0ef0c4df5fff4fd9b2dda246 [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
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010084 struct list_head writequeue; /* List of outgoing writequeue_entries */
85 spinlock_t writequeue_lock;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010086 void (*connect_action) (struct connection *); /* What to do to connect */
Alexander Aring055923b2020-07-27 09:13:38 -040087 void (*shutdown_action)(struct connection *con); /* What to do to shutdown */
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010088 int retries;
89#define MAX_CONNECT_RETRIES 3
Christine Caulfield5e9ccc32009-01-28 12:57:40 -060090 struct hlist_node list;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010091 struct connection *othercon;
Alexander Aringba868d92021-05-21 15:08:37 -040092 struct connection *sendcon;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010093 struct work_struct rwork; /* Receive workqueue */
94 struct work_struct swork; /* Send workqueue */
Alexander Aring055923b2020-07-27 09:13:38 -040095 wait_queue_head_t shutdown_wait; /* wait for graceful shutdown */
Alexander Aring4798cbb2020-09-24 10:31:26 -040096 unsigned char *rx_buf;
97 int rx_buflen;
98 int rx_leftover;
Alexander Aringa47666eb2020-08-27 15:02:49 -040099 struct rcu_head rcu;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100100};
101#define sock2con(x) ((struct connection *)(x)->sk_user_data)
102
Alexander Aringd11ccd42020-11-02 20:04:25 -0500103struct listen_connection {
104 struct socket *sock;
105 struct work_struct rwork;
106};
107
Alexander Aringf0747ebf2021-03-01 17:05:16 -0500108#define DLM_WQ_REMAIN_BYTES(e) (PAGE_SIZE - e->end)
109#define DLM_WQ_LENGTH_BYTES(e) (e->end - e->offset)
110
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100111/* An entry waiting to be sent */
112struct writequeue_entry {
113 struct list_head list;
114 struct page *page;
115 int offset;
116 int len;
117 int end;
118 int users;
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400119 int idx; /* get()/commit() idx exchange */
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100120 struct connection *con;
121};
122
David Teigland36b71a82012-07-26 12:44:30 -0500123struct dlm_node_addr {
124 struct list_head list;
125 int nodeid;
Alexander Aringe125fbe2021-03-01 17:05:09 -0500126 int mark;
David Teigland36b71a82012-07-26 12:44:30 -0500127 int addr_count;
Mike Christie98e1b602013-06-14 04:56:12 -0500128 int curr_addr_index;
David Teigland36b71a82012-07-26 12:44:30 -0500129 struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
130};
131
Bob Petersoncc661fc2017-09-12 08:55:23 +0000132static struct listen_sock_callbacks {
133 void (*sk_error_report)(struct sock *);
134 void (*sk_data_ready)(struct sock *);
135 void (*sk_state_change)(struct sock *);
136 void (*sk_write_space)(struct sock *);
137} listen_sock;
138
David Teigland36b71a82012-07-26 12:44:30 -0500139static LIST_HEAD(dlm_node_addrs);
140static DEFINE_SPINLOCK(dlm_node_addrs_spin);
141
Alexander Aringd11ccd42020-11-02 20:04:25 -0500142static struct listen_connection listen_con;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100143static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
144static int dlm_local_count;
Alexander Aring51746162021-03-01 17:05:13 -0500145int dlm_allow_conn;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100146
147/* Work queues */
148static struct workqueue_struct *recv_workqueue;
149static struct workqueue_struct *send_workqueue;
150
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600151static struct hlist_head connection_hash[CONN_HASH_SIZE];
Alexander Aringa47666eb2020-08-27 15:02:49 -0400152static DEFINE_SPINLOCK(connections_lock);
153DEFINE_STATIC_SRCU(connections_srcu);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100154
155static void process_recv_sockets(struct work_struct *work);
156static void process_send_sockets(struct work_struct *work);
157
Alexander Aring0672c3c2020-11-02 20:04:22 -0500158static void sctp_connect_to_sock(struct connection *con);
159static void tcp_connect_to_sock(struct connection *con);
Alexander Aring42873c92020-11-02 20:04:23 -0500160static void dlm_tcp_shutdown(struct connection *con);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600161
162/* This is deliberately very simple because most clusters have simple
163 sequential nodeids, so we should be able to go straight to a connection
164 struct in the array */
165static inline int nodeid_hash(int nodeid)
166{
167 return nodeid & (CONN_HASH_SIZE-1);
168}
169
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400170static struct connection *__find_con(int nodeid, int r)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600171{
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600172 struct connection *con;
173
Alexander Aringa47666eb2020-08-27 15:02:49 -0400174 hlist_for_each_entry_rcu(con, &connection_hash[r], list) {
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400175 if (con->nodeid == nodeid)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600176 return con;
177 }
Alexander Aringa47666eb2020-08-27 15:02:49 -0400178
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600179 return NULL;
180}
181
Alexander Aring6cde2102020-11-02 20:04:21 -0500182static int dlm_con_init(struct connection *con, int nodeid)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100183{
Alexander Aring4798cbb2020-09-24 10:31:26 -0400184 con->rx_buflen = dlm_config.ci_buffer_size;
185 con->rx_buf = kmalloc(con->rx_buflen, GFP_NOFS);
Alexander Aring6cde2102020-11-02 20:04:21 -0500186 if (!con->rx_buf)
187 return -ENOMEM;
Alexander Aring4798cbb2020-09-24 10:31:26 -0400188
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100189 con->nodeid = nodeid;
190 mutex_init(&con->sock_mutex);
191 INIT_LIST_HEAD(&con->writequeue);
192 spin_lock_init(&con->writequeue_lock);
193 INIT_WORK(&con->swork, process_send_sockets);
194 INIT_WORK(&con->rwork, process_recv_sockets);
Alexander Aring055923b2020-07-27 09:13:38 -0400195 init_waitqueue_head(&con->shutdown_wait);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100196
Alexander Aring42873c92020-11-02 20:04:23 -0500197 if (dlm_config.ci_protocol == 0) {
Alexander Aring0672c3c2020-11-02 20:04:22 -0500198 con->connect_action = tcp_connect_to_sock;
Alexander Aring42873c92020-11-02 20:04:23 -0500199 con->shutdown_action = dlm_tcp_shutdown;
200 } else {
Alexander Aring0672c3c2020-11-02 20:04:22 -0500201 con->connect_action = sctp_connect_to_sock;
Alexander Aring42873c92020-11-02 20:04:23 -0500202 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100203
Alexander Aring6cde2102020-11-02 20:04:21 -0500204 return 0;
205}
206
207/*
208 * If 'allocation' is zero then we don't attempt to create a new
209 * connection structure for this node.
210 */
211static struct connection *nodeid2con(int nodeid, gfp_t alloc)
212{
213 struct connection *con, *tmp;
214 int r, ret;
215
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400216 r = nodeid_hash(nodeid);
217 con = __find_con(nodeid, r);
Alexander Aring6cde2102020-11-02 20:04:21 -0500218 if (con || !alloc)
219 return con;
220
221 con = kzalloc(sizeof(*con), alloc);
222 if (!con)
223 return NULL;
224
225 ret = dlm_con_init(con, nodeid);
226 if (ret) {
227 kfree(con);
228 return NULL;
229 }
230
Alexander Aringa47666eb2020-08-27 15:02:49 -0400231 spin_lock(&connections_lock);
Alexander Aring4f2b30f2020-09-30 18:37:29 -0400232 /* Because multiple workqueues/threads calls this function it can
233 * race on multiple cpu's. Instead of locking hot path __find_con()
234 * we just check in rare cases of recently added nodes again
235 * under protection of connections_lock. If this is the case we
236 * abort our connection creation and return the existing connection.
237 */
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400238 tmp = __find_con(nodeid, r);
Alexander Aring4f2b30f2020-09-30 18:37:29 -0400239 if (tmp) {
240 spin_unlock(&connections_lock);
241 kfree(con->rx_buf);
242 kfree(con);
243 return tmp;
244 }
245
Alexander Aringa47666eb2020-08-27 15:02:49 -0400246 hlist_add_head_rcu(&con->list, &connection_hash[r]);
247 spin_unlock(&connections_lock);
248
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100249 return con;
250}
251
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600252/* Loop round all connections */
253static void foreach_conn(void (*conn_func)(struct connection *c))
254{
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400255 int i;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600256 struct connection *con;
257
258 for (i = 0; i < CONN_HASH_SIZE; i++) {
Alexander Aringa47666eb2020-08-27 15:02:49 -0400259 hlist_for_each_entry_rcu(con, &connection_hash[i], list)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600260 conn_func(con);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600261 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100262}
263
David Teigland36b71a82012-07-26 12:44:30 -0500264static struct dlm_node_addr *find_node_addr(int nodeid)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100265{
David Teigland36b71a82012-07-26 12:44:30 -0500266 struct dlm_node_addr *na;
267
268 list_for_each_entry(na, &dlm_node_addrs, list) {
269 if (na->nodeid == nodeid)
270 return na;
271 }
272 return NULL;
273}
274
Alexander Aring40c6b832020-11-02 20:04:27 -0500275static int addr_compare(const struct sockaddr_storage *x,
276 const struct sockaddr_storage *y)
David Teigland36b71a82012-07-26 12:44:30 -0500277{
278 switch (x->ss_family) {
279 case AF_INET: {
280 struct sockaddr_in *sinx = (struct sockaddr_in *)x;
281 struct sockaddr_in *siny = (struct sockaddr_in *)y;
282 if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
283 return 0;
284 if (sinx->sin_port != siny->sin_port)
285 return 0;
286 break;
287 }
288 case AF_INET6: {
289 struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
290 struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
291 if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
292 return 0;
293 if (sinx->sin6_port != siny->sin6_port)
294 return 0;
295 break;
296 }
297 default:
298 return 0;
299 }
300 return 1;
301}
302
303static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out,
Alexander Aringe125fbe2021-03-01 17:05:09 -0500304 struct sockaddr *sa_out, bool try_new_addr,
305 unsigned int *mark)
David Teigland36b71a82012-07-26 12:44:30 -0500306{
307 struct sockaddr_storage sas;
308 struct dlm_node_addr *na;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100309
310 if (!dlm_local_count)
311 return -1;
312
David Teigland36b71a82012-07-26 12:44:30 -0500313 spin_lock(&dlm_node_addrs_spin);
314 na = find_node_addr(nodeid);
Mike Christie98e1b602013-06-14 04:56:12 -0500315 if (na && na->addr_count) {
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300316 memcpy(&sas, na->addr[na->curr_addr_index],
317 sizeof(struct sockaddr_storage));
318
Mike Christie98e1b602013-06-14 04:56:12 -0500319 if (try_new_addr) {
320 na->curr_addr_index++;
321 if (na->curr_addr_index == na->addr_count)
322 na->curr_addr_index = 0;
323 }
Mike Christie98e1b602013-06-14 04:56:12 -0500324 }
David Teigland36b71a82012-07-26 12:44:30 -0500325 spin_unlock(&dlm_node_addrs_spin);
326
327 if (!na)
328 return -EEXIST;
329
330 if (!na->addr_count)
331 return -ENOENT;
332
Alexander Aringe125fbe2021-03-01 17:05:09 -0500333 *mark = na->mark;
334
David Teigland36b71a82012-07-26 12:44:30 -0500335 if (sas_out)
336 memcpy(sas_out, &sas, sizeof(struct sockaddr_storage));
337
338 if (!sa_out)
339 return 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100340
341 if (dlm_local_addr[0]->ss_family == AF_INET) {
David Teigland36b71a82012-07-26 12:44:30 -0500342 struct sockaddr_in *in4 = (struct sockaddr_in *) &sas;
343 struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100344 ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
345 } else {
David Teigland36b71a82012-07-26 12:44:30 -0500346 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sas;
347 struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000348 ret6->sin6_addr = in6->sin6_addr;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100349 }
350
351 return 0;
352}
353
Alexander Aringe125fbe2021-03-01 17:05:09 -0500354static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid,
355 unsigned int *mark)
David Teigland36b71a82012-07-26 12:44:30 -0500356{
357 struct dlm_node_addr *na;
358 int rv = -EEXIST;
Mike Christie98e1b602013-06-14 04:56:12 -0500359 int addr_i;
David Teigland36b71a82012-07-26 12:44:30 -0500360
361 spin_lock(&dlm_node_addrs_spin);
362 list_for_each_entry(na, &dlm_node_addrs, list) {
363 if (!na->addr_count)
364 continue;
365
Mike Christie98e1b602013-06-14 04:56:12 -0500366 for (addr_i = 0; addr_i < na->addr_count; addr_i++) {
367 if (addr_compare(na->addr[addr_i], addr)) {
368 *nodeid = na->nodeid;
Alexander Aringe125fbe2021-03-01 17:05:09 -0500369 *mark = na->mark;
Mike Christie98e1b602013-06-14 04:56:12 -0500370 rv = 0;
371 goto unlock;
372 }
373 }
David Teigland36b71a82012-07-26 12:44:30 -0500374 }
Mike Christie98e1b602013-06-14 04:56:12 -0500375unlock:
David Teigland36b71a82012-07-26 12:44:30 -0500376 spin_unlock(&dlm_node_addrs_spin);
377 return rv;
378}
379
Alexander Aring4f19d072020-11-02 20:04:28 -0500380/* caller need to held dlm_node_addrs_spin lock */
381static bool dlm_lowcomms_na_has_addr(const struct dlm_node_addr *na,
382 const struct sockaddr_storage *addr)
383{
384 int i;
385
386 for (i = 0; i < na->addr_count; i++) {
387 if (addr_compare(na->addr[i], addr))
388 return true;
389 }
390
391 return false;
392}
393
David Teigland36b71a82012-07-26 12:44:30 -0500394int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr, int len)
395{
396 struct sockaddr_storage *new_addr;
397 struct dlm_node_addr *new_node, *na;
Alexander Aring4f19d072020-11-02 20:04:28 -0500398 bool ret;
David Teigland36b71a82012-07-26 12:44:30 -0500399
400 new_node = kzalloc(sizeof(struct dlm_node_addr), GFP_NOFS);
401 if (!new_node)
402 return -ENOMEM;
403
404 new_addr = kzalloc(sizeof(struct sockaddr_storage), GFP_NOFS);
405 if (!new_addr) {
406 kfree(new_node);
407 return -ENOMEM;
408 }
409
410 memcpy(new_addr, addr, len);
411
412 spin_lock(&dlm_node_addrs_spin);
413 na = find_node_addr(nodeid);
414 if (!na) {
415 new_node->nodeid = nodeid;
416 new_node->addr[0] = new_addr;
417 new_node->addr_count = 1;
Alexander Aringe125fbe2021-03-01 17:05:09 -0500418 new_node->mark = dlm_config.ci_mark;
David Teigland36b71a82012-07-26 12:44:30 -0500419 list_add(&new_node->list, &dlm_node_addrs);
420 spin_unlock(&dlm_node_addrs_spin);
421 return 0;
422 }
423
Alexander Aring4f19d072020-11-02 20:04:28 -0500424 ret = dlm_lowcomms_na_has_addr(na, addr);
425 if (ret) {
426 spin_unlock(&dlm_node_addrs_spin);
427 kfree(new_addr);
428 kfree(new_node);
429 return -EEXIST;
430 }
431
David Teigland36b71a82012-07-26 12:44:30 -0500432 if (na->addr_count >= DLM_MAX_ADDR_COUNT) {
433 spin_unlock(&dlm_node_addrs_spin);
434 kfree(new_addr);
435 kfree(new_node);
436 return -ENOSPC;
437 }
438
439 na->addr[na->addr_count++] = new_addr;
440 spin_unlock(&dlm_node_addrs_spin);
441 kfree(new_node);
442 return 0;
443}
444
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100445/* Data available on socket or listen socket received a connect */
David S. Miller676d2362014-04-11 16:15:36 -0400446static void lowcomms_data_ready(struct sock *sk)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100447{
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000448 struct connection *con;
449
450 read_lock_bh(&sk->sk_callback_lock);
451 con = sock2con(sk);
Patrick Caulfieldafb853f2007-06-01 10:07:26 -0500452 if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100453 queue_work(recv_workqueue, &con->rwork);
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000454 read_unlock_bh(&sk->sk_callback_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100455}
456
Alexander Aringd11ccd42020-11-02 20:04:25 -0500457static void lowcomms_listen_data_ready(struct sock *sk)
458{
459 queue_work(recv_workqueue, &listen_con.rwork);
460}
461
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100462static void lowcomms_write_space(struct sock *sk)
463{
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000464 struct connection *con;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100465
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000466 read_lock_bh(&sk->sk_callback_lock);
467 con = sock2con(sk);
David Millerb36930d2010-11-10 21:56:39 -0800468 if (!con)
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000469 goto out;
David Millerb36930d2010-11-10 21:56:39 -0800470
Alexander Aring19633c72020-11-02 20:04:20 -0500471 if (!test_and_set_bit(CF_CONNECTED, &con->flags)) {
472 log_print("successful connected to node %d", con->nodeid);
473 queue_work(send_workqueue, &con->swork);
474 goto out;
475 }
476
David Millerb36930d2010-11-10 21:56:39 -0800477 clear_bit(SOCK_NOSPACE, &con->sock->flags);
478
479 if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) {
480 con->sock->sk->sk_write_pending--;
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800481 clear_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags);
David Millerb36930d2010-11-10 21:56:39 -0800482 }
483
Bob Peterson01da24d2017-09-12 08:55:14 +0000484 queue_work(send_workqueue, &con->swork);
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000485out:
486 read_unlock_bh(&sk->sk_callback_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100487}
488
489static inline void lowcomms_connect_sock(struct connection *con)
490{
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -0500491 if (test_bit(CF_CLOSE, &con->flags))
492 return;
Bob Peterson61d9102b2017-09-12 08:55:04 +0000493 queue_work(send_workqueue, &con->swork);
494 cond_resched();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100495}
496
497static void lowcomms_state_change(struct sock *sk)
498{
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300499 /* SCTP layer is not calling sk_data_ready when the connection
500 * is done, so we catch the signal through here. Also, it
501 * doesn't switch socket state when entering shutdown, so we
502 * skip the write in that case.
503 */
504 if (sk->sk_shutdown) {
505 if (sk->sk_shutdown == RCV_SHUTDOWN)
506 lowcomms_data_ready(sk);
507 } else if (sk->sk_state == TCP_ESTABLISHED) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100508 lowcomms_write_space(sk);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300509 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100510}
511
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500512int dlm_lowcomms_connect_node(int nodeid)
513{
514 struct connection *con;
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400515 int idx;
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500516
517 if (nodeid == dlm_our_nodeid())
518 return 0;
519
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400520 idx = srcu_read_lock(&connections_srcu);
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500521 con = nodeid2con(nodeid, GFP_NOFS);
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400522 if (!con) {
523 srcu_read_unlock(&connections_srcu, idx);
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500524 return -ENOMEM;
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400525 }
526
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500527 lowcomms_connect_sock(con);
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400528 srcu_read_unlock(&connections_srcu, idx);
529
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500530 return 0;
531}
532
Alexander Aringe125fbe2021-03-01 17:05:09 -0500533int dlm_lowcomms_nodes_set_mark(int nodeid, unsigned int mark)
534{
535 struct dlm_node_addr *na;
536
537 spin_lock(&dlm_node_addrs_spin);
538 na = find_node_addr(nodeid);
539 if (!na) {
540 spin_unlock(&dlm_node_addrs_spin);
541 return -ENOENT;
542 }
543
544 na->mark = mark;
545 spin_unlock(&dlm_node_addrs_spin);
546
547 return 0;
548}
549
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500550static void lowcomms_error_report(struct sock *sk)
551{
Bob Petersonb81171c2016-02-05 14:39:02 -0500552 struct connection *con;
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500553 struct sockaddr_storage saddr;
Bob Petersonb81171c2016-02-05 14:39:02 -0500554 void (*orig_report)(struct sock *) = NULL;
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500555
Bob Petersonb81171c2016-02-05 14:39:02 -0500556 read_lock_bh(&sk->sk_callback_lock);
557 con = sock2con(sk);
558 if (con == NULL)
559 goto out;
560
Bob Petersoncc661fc2017-09-12 08:55:23 +0000561 orig_report = listen_sock.sk_error_report;
Bob Peterson1a318332016-01-18 12:29:15 -0500562 if (con->sock == NULL ||
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100563 kernel_getpeername(con->sock, (struct sockaddr *)&saddr) < 0) {
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500564 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
565 "sending to node %d, port %d, "
566 "sk_err=%d/%d\n", dlm_our_nodeid(),
567 con->nodeid, dlm_config.ci_tcp_port,
568 sk->sk_err, sk->sk_err_soft);
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500569 } else if (saddr.ss_family == AF_INET) {
570 struct sockaddr_in *sin4 = (struct sockaddr_in *)&saddr;
571
572 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
573 "sending to node %d at %pI4, port %d, "
574 "sk_err=%d/%d\n", dlm_our_nodeid(),
575 con->nodeid, &sin4->sin_addr.s_addr,
576 dlm_config.ci_tcp_port, sk->sk_err,
577 sk->sk_err_soft);
578 } else {
579 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&saddr;
580
581 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
582 "sending to node %d at %u.%u.%u.%u, "
583 "port %d, sk_err=%d/%d\n", dlm_our_nodeid(),
584 con->nodeid, sin6->sin6_addr.s6_addr32[0],
585 sin6->sin6_addr.s6_addr32[1],
586 sin6->sin6_addr.s6_addr32[2],
587 sin6->sin6_addr.s6_addr32[3],
588 dlm_config.ci_tcp_port, sk->sk_err,
589 sk->sk_err_soft);
590 }
Alexander Aringba868d92021-05-21 15:08:37 -0400591
592 /* below sendcon only handling */
593 if (test_bit(CF_IS_OTHERCON, &con->flags))
594 con = con->sendcon;
595
596 switch (sk->sk_err) {
597 case ECONNREFUSED:
598 set_bit(CF_DELAY_CONNECT, &con->flags);
599 break;
600 default:
601 break;
602 }
603
604 if (!test_and_set_bit(CF_RECONNECT, &con->flags))
605 queue_work(send_workqueue, &con->swork);
606
Bob Petersonb81171c2016-02-05 14:39:02 -0500607out:
608 read_unlock_bh(&sk->sk_callback_lock);
609 if (orig_report)
610 orig_report(sk);
611}
612
613/* Note: sk_callback_lock must be locked before calling this function. */
Bob Petersoncc661fc2017-09-12 08:55:23 +0000614static void save_listen_callbacks(struct socket *sock)
Bob Petersonb81171c2016-02-05 14:39:02 -0500615{
Bob Petersoncc661fc2017-09-12 08:55:23 +0000616 struct sock *sk = sock->sk;
617
618 listen_sock.sk_data_ready = sk->sk_data_ready;
619 listen_sock.sk_state_change = sk->sk_state_change;
620 listen_sock.sk_write_space = sk->sk_write_space;
621 listen_sock.sk_error_report = sk->sk_error_report;
Bob Petersonb81171c2016-02-05 14:39:02 -0500622}
623
Bob Petersoncc661fc2017-09-12 08:55:23 +0000624static void restore_callbacks(struct socket *sock)
Bob Petersonb81171c2016-02-05 14:39:02 -0500625{
Bob Petersoncc661fc2017-09-12 08:55:23 +0000626 struct sock *sk = sock->sk;
627
Bob Petersonb81171c2016-02-05 14:39:02 -0500628 write_lock_bh(&sk->sk_callback_lock);
Bob Petersonb81171c2016-02-05 14:39:02 -0500629 sk->sk_user_data = NULL;
Bob Petersoncc661fc2017-09-12 08:55:23 +0000630 sk->sk_data_ready = listen_sock.sk_data_ready;
631 sk->sk_state_change = listen_sock.sk_state_change;
632 sk->sk_write_space = listen_sock.sk_write_space;
633 sk->sk_error_report = listen_sock.sk_error_report;
Bob Petersonb81171c2016-02-05 14:39:02 -0500634 write_unlock_bh(&sk->sk_callback_lock);
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500635}
636
Alexander Aringd11ccd42020-11-02 20:04:25 -0500637static void add_listen_sock(struct socket *sock, struct listen_connection *con)
638{
639 struct sock *sk = sock->sk;
640
641 write_lock_bh(&sk->sk_callback_lock);
642 save_listen_callbacks(sock);
643 con->sock = sock;
644
645 sk->sk_user_data = con;
646 sk->sk_allocation = GFP_NOFS;
647 /* Install a data_ready callback */
648 sk->sk_data_ready = lowcomms_listen_data_ready;
649 write_unlock_bh(&sk->sk_callback_lock);
650}
651
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100652/* Make a socket active */
tsutomu.owa@toshiba.co.jp988419a2017-09-12 08:55:32 +0000653static void add_sock(struct socket *sock, struct connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100654{
Bob Petersonb81171c2016-02-05 14:39:02 -0500655 struct sock *sk = sock->sk;
656
657 write_lock_bh(&sk->sk_callback_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100658 con->sock = sock;
659
Bob Petersonb81171c2016-02-05 14:39:02 -0500660 sk->sk_user_data = con;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100661 /* Install a data_ready callback */
Bob Petersonb81171c2016-02-05 14:39:02 -0500662 sk->sk_data_ready = lowcomms_data_ready;
663 sk->sk_write_space = lowcomms_write_space;
664 sk->sk_state_change = lowcomms_state_change;
665 sk->sk_allocation = GFP_NOFS;
666 sk->sk_error_report = lowcomms_error_report;
667 write_unlock_bh(&sk->sk_callback_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100668}
669
670/* Add the port number to an IPv6 or 4 sockaddr and return the address
671 length */
672static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
673 int *addr_len)
674{
675 saddr->ss_family = dlm_local_addr[0]->ss_family;
676 if (saddr->ss_family == AF_INET) {
677 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
678 in4_addr->sin_port = cpu_to_be16(port);
679 *addr_len = sizeof(struct sockaddr_in);
680 memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
681 } else {
682 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
683 in6_addr->sin6_port = cpu_to_be16(port);
684 *addr_len = sizeof(struct sockaddr_in6);
685 }
Patrick Caulfield01c8cab2007-07-17 16:53:15 +0100686 memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100687}
688
Alexander Aringd11ccd42020-11-02 20:04:25 -0500689static void dlm_close_sock(struct socket **sock)
690{
691 if (*sock) {
692 restore_callbacks(*sock);
693 sock_release(*sock);
694 *sock = NULL;
695 }
696}
697
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100698/* Close a remote connection and tidy up */
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300699static void close_connection(struct connection *con, bool and_other,
700 bool tx, bool rx)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100701{
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +0000702 bool closing = test_and_set_bit(CF_CLOSING, &con->flags);
703
tsutomu.owa@toshiba.co.jp0aa18462017-09-12 09:02:02 +0000704 if (tx && !closing && cancel_work_sync(&con->swork)) {
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300705 log_print("canceled swork for node %d", con->nodeid);
tsutomu.owa@toshiba.co.jp0aa18462017-09-12 09:02:02 +0000706 clear_bit(CF_WRITE_PENDING, &con->flags);
707 }
708 if (rx && !closing && cancel_work_sync(&con->rwork)) {
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300709 log_print("canceled rwork for node %d", con->nodeid);
tsutomu.owa@toshiba.co.jp0aa18462017-09-12 09:02:02 +0000710 clear_bit(CF_READ_PENDING, &con->flags);
711 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100712
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300713 mutex_lock(&con->sock_mutex);
Alexander Aringd11ccd42020-11-02 20:04:25 -0500714 dlm_close_sock(&con->sock);
715
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100716 if (con->othercon && and_other) {
717 /* Will only re-enter once. */
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300718 close_connection(con->othercon, false, true, true);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100719 }
Patrick Caulfield9e5f2822007-08-02 14:58:14 +0100720
Alexander Aring4798cbb2020-09-24 10:31:26 -0400721 con->rx_leftover = 0;
Patrick Caulfield61d96be02007-08-20 15:13:38 +0100722 con->retries = 0;
Alexander Aring19633c72020-11-02 20:04:20 -0500723 clear_bit(CF_CONNECTED, &con->flags);
Alexander Aringba868d92021-05-21 15:08:37 -0400724 clear_bit(CF_DELAY_CONNECT, &con->flags);
725 clear_bit(CF_RECONNECT, &con->flags);
Patrick Caulfield61d96be02007-08-20 15:13:38 +0100726 mutex_unlock(&con->sock_mutex);
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +0000727 clear_bit(CF_CLOSING, &con->flags);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100728}
729
Alexander Aring055923b2020-07-27 09:13:38 -0400730static void shutdown_connection(struct connection *con)
731{
732 int ret;
733
Alexander Aringeec054b2021-03-01 17:05:19 -0500734 flush_work(&con->swork);
Alexander Aring055923b2020-07-27 09:13:38 -0400735
736 mutex_lock(&con->sock_mutex);
737 /* nothing to shutdown */
738 if (!con->sock) {
739 mutex_unlock(&con->sock_mutex);
740 return;
741 }
742
743 set_bit(CF_SHUTDOWN, &con->flags);
744 ret = kernel_sock_shutdown(con->sock, SHUT_WR);
745 mutex_unlock(&con->sock_mutex);
746 if (ret) {
747 log_print("Connection %p failed to shutdown: %d will force close",
748 con, ret);
749 goto force_close;
750 } else {
751 ret = wait_event_timeout(con->shutdown_wait,
752 !test_bit(CF_SHUTDOWN, &con->flags),
753 DLM_SHUTDOWN_WAIT_TIMEOUT);
754 if (ret == 0) {
755 log_print("Connection %p shutdown timed out, will force close",
756 con);
757 goto force_close;
758 }
759 }
760
761 return;
762
763force_close:
764 clear_bit(CF_SHUTDOWN, &con->flags);
765 close_connection(con, false, true, true);
766}
767
768static void dlm_tcp_shutdown(struct connection *con)
769{
770 if (con->othercon)
771 shutdown_connection(con->othercon);
772 shutdown_connection(con);
773}
774
Alexander Aring4798cbb2020-09-24 10:31:26 -0400775static int con_realloc_receive_buf(struct connection *con, int newlen)
776{
777 unsigned char *newbuf;
778
779 newbuf = kmalloc(newlen, GFP_NOFS);
780 if (!newbuf)
781 return -ENOMEM;
782
783 /* copy any leftover from last receive */
784 if (con->rx_leftover)
785 memmove(newbuf, con->rx_buf, con->rx_leftover);
786
787 /* swap to new buffer space */
788 kfree(con->rx_buf);
789 con->rx_buflen = newlen;
790 con->rx_buf = newbuf;
791
792 return 0;
793}
794
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100795/* Data received from remote end */
796static int receive_from_sock(struct connection *con)
797{
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100798 int call_again_soon = 0;
Alexander Aring4798cbb2020-09-24 10:31:26 -0400799 struct msghdr msg;
800 struct kvec iov;
801 int ret, buflen;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100802
803 mutex_lock(&con->sock_mutex);
804
805 if (con->sock == NULL) {
806 ret = -EAGAIN;
807 goto out_close;
808 }
Alexander Aring4798cbb2020-09-24 10:31:26 -0400809
Alexander Aring4798cbb2020-09-24 10:31:26 -0400810 /* realloc if we get new buffer size to read out */
811 buflen = dlm_config.ci_buffer_size;
812 if (con->rx_buflen != buflen && con->rx_leftover <= buflen) {
813 ret = con_realloc_receive_buf(con, buflen);
814 if (ret < 0)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100815 goto out_resched;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100816 }
817
Alexander Aring4798cbb2020-09-24 10:31:26 -0400818 /* calculate new buffer parameter regarding last receive and
819 * possible leftover bytes
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100820 */
Alexander Aring4798cbb2020-09-24 10:31:26 -0400821 iov.iov_base = con->rx_buf + con->rx_leftover;
822 iov.iov_len = con->rx_buflen - con->rx_leftover;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100823
Alexander Aring4798cbb2020-09-24 10:31:26 -0400824 memset(&msg, 0, sizeof(msg));
825 msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
826 ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len,
827 msg.msg_flags);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100828 if (ret <= 0)
829 goto out_close;
Alexander Aring4798cbb2020-09-24 10:31:26 -0400830 else if (ret == iov.iov_len)
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300831 call_again_soon = 1;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100832
Alexander Aring4798cbb2020-09-24 10:31:26 -0400833 /* new buflen according readed bytes and leftover from last receive */
834 buflen = ret + con->rx_leftover;
835 ret = dlm_process_incoming_buffer(con->nodeid, con->rx_buf, buflen);
836 if (ret < 0)
837 goto out_close;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100838
Alexander Aring4798cbb2020-09-24 10:31:26 -0400839 /* calculate leftover bytes from process and put it into begin of
840 * the receive buffer, so next receive we have the full message
841 * at the start address of the receive buffer.
842 */
843 con->rx_leftover = buflen - ret;
844 if (con->rx_leftover) {
845 memmove(con->rx_buf, con->rx_buf + ret,
846 con->rx_leftover);
847 call_again_soon = true;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100848 }
849
850 if (call_again_soon)
851 goto out_resched;
Alexander Aring4798cbb2020-09-24 10:31:26 -0400852
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100853 mutex_unlock(&con->sock_mutex);
854 return 0;
855
856out_resched:
857 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
858 queue_work(recv_workqueue, &con->rwork);
859 mutex_unlock(&con->sock_mutex);
860 return -EAGAIN;
861
862out_close:
863 mutex_unlock(&con->sock_mutex);
Alexander Aringba868d92021-05-21 15:08:37 -0400864 if (ret == 0) {
Alexander Aring055923b2020-07-27 09:13:38 -0400865 close_connection(con, false, true, false);
Alexander Aringba868d92021-05-21 15:08:37 -0400866 log_print("connection %p got EOF from %d",
867 con, con->nodeid);
868 /* handling for tcp shutdown */
869 clear_bit(CF_SHUTDOWN, &con->flags);
870 wake_up(&con->shutdown_wait);
871 /* signal to breaking receive worker */
872 ret = -1;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100873 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100874 return ret;
875}
876
877/* Listening socket is busy, accept a connection */
Alexander Aringd11ccd42020-11-02 20:04:25 -0500878static int accept_from_sock(struct listen_connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100879{
880 int result;
881 struct sockaddr_storage peeraddr;
882 struct socket *newsock;
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400883 int len, idx;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100884 int nodeid;
885 struct connection *newcon;
886 struct connection *addcon;
Alexander Aring3f78cd72020-09-24 10:31:23 -0400887 unsigned int mark;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100888
David Teigland513ef592012-03-30 11:46:08 -0500889 if (!dlm_allow_conn) {
David Teigland513ef592012-03-30 11:46:08 -0500890 return -1;
891 }
David Teigland513ef592012-03-30 11:46:08 -0500892
Alexander Aringd11ccd42020-11-02 20:04:25 -0500893 if (!con->sock)
tsutomu.owa@toshiba.co.jp3421fb12017-09-12 09:01:38 +0000894 return -ENOTCONN;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100895
tsutomu.owa@toshiba.co.jp3421fb12017-09-12 09:01:38 +0000896 result = kernel_accept(con->sock, &newsock, O_NONBLOCK);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100897 if (result < 0)
898 goto accept_err;
899
900 /* Get the connected socket's peer */
901 memset(&peeraddr, 0, sizeof(peeraddr));
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100902 len = newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr, 2);
903 if (len < 0) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100904 result = -ECONNABORTED;
905 goto accept_err;
906 }
907
908 /* Get the new node's NODEID */
909 make_sockaddr(&peeraddr, 0, &len);
Alexander Aringe125fbe2021-03-01 17:05:09 -0500910 if (addr_to_nodeid(&peeraddr, &nodeid, &mark)) {
Masatake YAMATObcaadf52011-07-04 12:25:51 +0900911 unsigned char *b=(unsigned char *)&peeraddr;
David Teigland617e82e2007-04-26 13:46:49 -0500912 log_print("connect from non cluster node");
Masatake YAMATObcaadf52011-07-04 12:25:51 +0900913 print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
914 b, sizeof(struct sockaddr_storage));
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100915 sock_release(newsock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100916 return -1;
917 }
918
919 log_print("got connection from %d", nodeid);
920
921 /* Check to see if we already have a connection to this node. This
922 * could happen if the two nodes initiate a connection at roughly
923 * the same time and the connections cross on the wire.
924 * In this case we store the incoming one in "othercon"
925 */
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400926 idx = srcu_read_lock(&connections_srcu);
David Teigland748285c2009-05-15 10:50:57 -0500927 newcon = nodeid2con(nodeid, GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100928 if (!newcon) {
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400929 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100930 result = -ENOMEM;
931 goto accept_err;
932 }
Alexander Aringd11ccd42020-11-02 20:04:25 -0500933
Alexander Aringe125fbe2021-03-01 17:05:09 -0500934 sock_set_mark(newsock->sk, mark);
935
Alexander Aringd11ccd42020-11-02 20:04:25 -0500936 mutex_lock(&newcon->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100937 if (newcon->sock) {
938 struct connection *othercon = newcon->othercon;
939
940 if (!othercon) {
Alexander Aringa47666eb2020-08-27 15:02:49 -0400941 othercon = kzalloc(sizeof(*othercon), GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100942 if (!othercon) {
David Teigland617e82e2007-04-26 13:46:49 -0500943 log_print("failed to allocate incoming socket");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100944 mutex_unlock(&newcon->sock_mutex);
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400945 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100946 result = -ENOMEM;
947 goto accept_err;
948 }
Alexander Aring4798cbb2020-09-24 10:31:26 -0400949
Alexander Aring6cde2102020-11-02 20:04:21 -0500950 result = dlm_con_init(othercon, nodeid);
951 if (result < 0) {
Alexander Aring4798cbb2020-09-24 10:31:26 -0400952 kfree(othercon);
Yang Yingliang2fd8db22021-03-27 16:37:04 +0800953 mutex_unlock(&newcon->sock_mutex);
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400954 srcu_read_unlock(&connections_srcu, idx);
Alexander Aring4798cbb2020-09-24 10:31:26 -0400955 goto accept_err;
956 }
957
Alexander Aringe9a470a2021-03-01 17:05:11 -0500958 lockdep_set_subclass(&othercon->sock_mutex, 1);
Alexander Aring7443bc92021-05-21 15:08:36 -0400959 set_bit(CF_IS_OTHERCON, &othercon->flags);
Alexander Aring6cde2102020-11-02 20:04:21 -0500960 newcon->othercon = othercon;
Alexander Aringba868d92021-05-21 15:08:37 -0400961 othercon->sendcon = newcon;
Alexander Aringba3ab3c2020-07-27 09:13:37 -0400962 } else {
963 /* close other sock con if we have something new */
964 close_connection(othercon, false, true, false);
Patrick Caulfield61d96be02007-08-20 15:13:38 +0100965 }
Alexander Aringba3ab3c2020-07-27 09:13:37 -0400966
Alexander Aringe9a470a2021-03-01 17:05:11 -0500967 mutex_lock(&othercon->sock_mutex);
Alexander Aringba3ab3c2020-07-27 09:13:37 -0400968 add_sock(newsock, othercon);
969 addcon = othercon;
970 mutex_unlock(&othercon->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100971 }
972 else {
Bob Peterson3735b4b2016-09-23 14:23:26 -0400973 /* accept copies the sk after we've saved the callbacks, so we
974 don't want to save them a second time or comm errors will
975 result in calling sk_error_report recursively. */
tsutomu.owa@toshiba.co.jp988419a2017-09-12 08:55:32 +0000976 add_sock(newsock, newcon);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100977 addcon = newcon;
978 }
979
Alexander Aringb30a6242021-03-01 17:05:10 -0500980 set_bit(CF_CONNECTED, &addcon->flags);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100981 mutex_unlock(&newcon->sock_mutex);
982
983 /*
984 * Add it to the active queue in case we got data
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300985 * between processing the accept adding the socket
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100986 * to the read_sockets list
987 */
988 if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
989 queue_work(recv_workqueue, &addcon->rwork);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100990
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400991 srcu_read_unlock(&connections_srcu, idx);
992
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100993 return 0;
994
995accept_err:
tsutomu.owa@toshiba.co.jp3421fb12017-09-12 09:01:38 +0000996 if (newsock)
997 sock_release(newsock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100998
999 if (result != -EAGAIN)
David Teigland617e82e2007-04-26 13:46:49 -05001000 log_print("error accepting connection from node: %d", result);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001001 return result;
1002}
1003
1004static void free_entry(struct writequeue_entry *e)
1005{
1006 __free_page(e->page);
1007 kfree(e);
1008}
1009
Mike Christie5d689872013-06-14 04:56:13 -05001010/*
1011 * writequeue_entry_complete - try to delete and free write queue entry
1012 * @e: write queue entry to try to delete
1013 * @completed: bytes completed
1014 *
1015 * writequeue_lock must be held.
1016 */
1017static void writequeue_entry_complete(struct writequeue_entry *e, int completed)
1018{
1019 e->offset += completed;
1020 e->len -= completed;
1021
1022 if (e->len == 0 && e->users == 0) {
1023 list_del(&e->list);
1024 free_entry(e);
1025 }
1026}
1027
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001028/*
1029 * sctp_bind_addrs - bind a SCTP socket to all our addresses
1030 */
Alexander Aring13004e82020-11-02 20:04:24 -05001031static int sctp_bind_addrs(struct socket *sock, uint16_t port)
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001032{
1033 struct sockaddr_storage localaddr;
Christoph Hellwigc0425a42020-05-29 14:09:42 +02001034 struct sockaddr *addr = (struct sockaddr *)&localaddr;
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001035 int i, addr_len, result = 0;
1036
1037 for (i = 0; i < dlm_local_count; i++) {
1038 memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
1039 make_sockaddr(&localaddr, port, &addr_len);
1040
1041 if (!i)
Alexander Aring13004e82020-11-02 20:04:24 -05001042 result = kernel_bind(sock, addr, addr_len);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001043 else
Alexander Aring13004e82020-11-02 20:04:24 -05001044 result = sock_bind_add(sock->sk, addr, addr_len);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001045
1046 if (result < 0) {
1047 log_print("Can't bind to %d addr number %d, %d.\n",
1048 port, i + 1, result);
1049 break;
1050 }
1051 }
1052 return result;
1053}
1054
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001055/* Initiate an SCTP association.
1056 This is a special case of send_to_sock() in that we don't yet have a
1057 peeled-off socket for this association, so we use the listening socket
1058 and add the primary IP address of the remote node.
1059 */
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001060static void sctp_connect_to_sock(struct connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001061{
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001062 struct sockaddr_storage daddr;
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001063 int result;
1064 int addr_len;
1065 struct socket *sock;
Alexander Aring9c9f1682020-06-26 13:26:50 -04001066 unsigned int mark;
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001067
Mike Christie5d689872013-06-14 04:56:13 -05001068 mutex_lock(&con->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001069
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001070 /* Some odd races can cause double-connects, ignore them */
1071 if (con->retries++ > MAX_CONNECT_RETRIES)
1072 goto out;
1073
1074 if (con->sock) {
1075 log_print("node %d already connected.", con->nodeid);
1076 goto out;
1077 }
1078
1079 memset(&daddr, 0, sizeof(daddr));
Alexander Aringe125fbe2021-03-01 17:05:09 -05001080 result = nodeid_to_addr(con->nodeid, &daddr, NULL, true, &mark);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001081 if (result < 0) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001082 log_print("no address for nodeid %d", con->nodeid);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001083 goto out;
David Teigland04bedd72009-09-18 14:31:47 -05001084 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001085
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001086 /* Create a socket to communicate with */
1087 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1088 SOCK_STREAM, IPPROTO_SCTP, &sock);
1089 if (result < 0)
1090 goto socket_err;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001091
Alexander Aring9c9f1682020-06-26 13:26:50 -04001092 sock_set_mark(sock->sk, mark);
1093
tsutomu.owa@toshiba.co.jp988419a2017-09-12 08:55:32 +00001094 add_sock(sock, con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001095
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001096 /* Bind to all addresses. */
Alexander Aring13004e82020-11-02 20:04:24 -05001097 if (sctp_bind_addrs(con->sock, 0))
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001098 goto bind_err;
Mike Christie98e1b602013-06-14 04:56:12 -05001099
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001100 make_sockaddr(&daddr, dlm_config.ci_tcp_port, &addr_len);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001101
Alexander Aring2df6b762021-05-21 15:08:34 -04001102 log_print_ratelimited("connecting to %d", con->nodeid);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001103
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001104 /* Turn off Nagle's algorithm */
Christoph Hellwig40ef92c2020-05-29 14:09:40 +02001105 sctp_sock_set_nodelay(sock->sk);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001106
Gang Hef706d832018-05-02 10:28:35 -05001107 /*
1108 * Make sock->ops->connect() function return in specified time,
1109 * since O_NONBLOCK argument in connect() function does not work here,
1110 * then, we should restore the default value of this attribute.
1111 */
Christoph Hellwig76ee0782020-05-28 07:12:12 +02001112 sock_set_sndtimeo(sock->sk, 5);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001113 result = sock->ops->connect(sock, (struct sockaddr *)&daddr, addr_len,
Gang Heda3627c2018-05-29 11:09:22 +08001114 0);
Christoph Hellwig76ee0782020-05-28 07:12:12 +02001115 sock_set_sndtimeo(sock->sk, 0);
Gang Hef706d832018-05-02 10:28:35 -05001116
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001117 if (result == -EINPROGRESS)
1118 result = 0;
Alexander Aring19633c72020-11-02 20:04:20 -05001119 if (result == 0) {
1120 if (!test_and_set_bit(CF_CONNECTED, &con->flags))
1121 log_print("successful connected to node %d", con->nodeid);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001122 goto out;
Alexander Aring19633c72020-11-02 20:04:20 -05001123 }
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001124
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001125bind_err:
1126 con->sock = NULL;
1127 sock_release(sock);
1128
1129socket_err:
1130 /*
1131 * Some errors are fatal and this list might need adjusting. For other
1132 * errors we try again until the max number of retries is reached.
1133 */
1134 if (result != -EHOSTUNREACH &&
1135 result != -ENETUNREACH &&
1136 result != -ENETDOWN &&
1137 result != -EINVAL &&
1138 result != -EPROTONOSUPPORT) {
1139 log_print("connect %d try %d error %d", con->nodeid,
1140 con->retries, result);
1141 mutex_unlock(&con->sock_mutex);
1142 msleep(1000);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001143 lowcomms_connect_sock(con);
1144 return;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001145 }
Mike Christie5d689872013-06-14 04:56:13 -05001146
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001147out:
Mike Christie5d689872013-06-14 04:56:13 -05001148 mutex_unlock(&con->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001149}
1150
1151/* Connect a new socket to its peer */
1152static void tcp_connect_to_sock(struct connection *con)
1153{
Lon Hohberger6bd8fed2007-10-25 18:51:54 -04001154 struct sockaddr_storage saddr, src_addr;
Alexander Aringe125fbe2021-03-01 17:05:09 -05001155 unsigned int mark;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001156 int addr_len;
Casey Dahlina89d63a2009-07-14 12:17:51 -05001157 struct socket *sock = NULL;
David Teigland36b71a82012-07-26 12:44:30 -05001158 int result;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001159
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001160 mutex_lock(&con->sock_mutex);
1161 if (con->retries++ > MAX_CONNECT_RETRIES)
1162 goto out;
1163
1164 /* Some odd races can cause double-connects, ignore them */
David Teigland36b71a82012-07-26 12:44:30 -05001165 if (con->sock)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001166 goto out;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001167
1168 /* Create a socket to communicate with */
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001169 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1170 SOCK_STREAM, IPPROTO_TCP, &sock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001171 if (result < 0)
1172 goto out_err;
1173
1174 memset(&saddr, 0, sizeof(saddr));
Alexander Aringe125fbe2021-03-01 17:05:09 -05001175 result = nodeid_to_addr(con->nodeid, &saddr, NULL, false, &mark);
David Teigland36b71a82012-07-26 12:44:30 -05001176 if (result < 0) {
1177 log_print("no address for nodeid %d", con->nodeid);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001178 goto out_err;
David Teigland36b71a82012-07-26 12:44:30 -05001179 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001180
Alexander Aringe125fbe2021-03-01 17:05:09 -05001181 sock_set_mark(sock->sk, mark);
1182
tsutomu.owa@toshiba.co.jp988419a2017-09-12 08:55:32 +00001183 add_sock(sock, con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001184
Lon Hohberger6bd8fed2007-10-25 18:51:54 -04001185 /* Bind to our cluster-known address connecting to avoid
1186 routing problems */
1187 memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
1188 make_sockaddr(&src_addr, 0, &addr_len);
1189 result = sock->ops->bind(sock, (struct sockaddr *) &src_addr,
1190 addr_len);
1191 if (result < 0) {
1192 log_print("could not bind for connect: %d", result);
1193 /* This *may* not indicate a critical error */
1194 }
1195
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001196 make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
1197
Alexander Aring2df6b762021-05-21 15:08:34 -04001198 log_print_ratelimited("connecting to %d", con->nodeid);
David Teiglandcb2d45d2010-11-12 11:12:55 -06001199
1200 /* Turn off Nagle's algorithm */
Christoph Hellwig12abc5e2020-05-28 07:12:19 +02001201 tcp_sock_set_nodelay(sock->sk);
David Teiglandcb2d45d2010-11-12 11:12:55 -06001202
David Teigland36b71a82012-07-26 12:44:30 -05001203 result = sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001204 O_NONBLOCK);
1205 if (result == -EINPROGRESS)
1206 result = 0;
1207 if (result == 0)
1208 goto out;
1209
1210out_err:
1211 if (con->sock) {
1212 sock_release(con->sock);
1213 con->sock = NULL;
Casey Dahlina89d63a2009-07-14 12:17:51 -05001214 } else if (sock) {
1215 sock_release(sock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001216 }
1217 /*
1218 * Some errors are fatal and this list might need adjusting. For other
1219 * errors we try again until the max number of retries is reached.
1220 */
David Teigland36b71a82012-07-26 12:44:30 -05001221 if (result != -EHOSTUNREACH &&
1222 result != -ENETUNREACH &&
1223 result != -ENETDOWN &&
1224 result != -EINVAL &&
1225 result != -EPROTONOSUPPORT) {
1226 log_print("connect %d try %d error %d", con->nodeid,
1227 con->retries, result);
1228 mutex_unlock(&con->sock_mutex);
1229 msleep(1000);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001230 lowcomms_connect_sock(con);
David Teigland36b71a82012-07-26 12:44:30 -05001231 return;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001232 }
1233out:
1234 mutex_unlock(&con->sock_mutex);
1235 return;
1236}
1237
Alexander Aringd11ccd42020-11-02 20:04:25 -05001238/* On error caller must run dlm_close_sock() for the
1239 * listen connection socket.
1240 */
1241static int tcp_create_listen_sock(struct listen_connection *con,
1242 struct sockaddr_storage *saddr)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001243{
1244 struct socket *sock = NULL;
1245 int result = 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001246 int addr_len;
1247
1248 if (dlm_local_addr[0]->ss_family == AF_INET)
1249 addr_len = sizeof(struct sockaddr_in);
1250 else
1251 addr_len = sizeof(struct sockaddr_in6);
1252
1253 /* Create a socket to communicate with */
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001254 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1255 SOCK_STREAM, IPPROTO_TCP, &sock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001256 if (result < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001257 log_print("Can't create listening comms socket");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001258 goto create_out;
1259 }
1260
Alexander Aringa5b7ab62020-06-26 13:26:49 -04001261 sock_set_mark(sock->sk, dlm_config.ci_mark);
1262
David Teiglandcb2d45d2010-11-12 11:12:55 -06001263 /* Turn off Nagle's algorithm */
Christoph Hellwig12abc5e2020-05-28 07:12:19 +02001264 tcp_sock_set_nodelay(sock->sk);
David Teiglandcb2d45d2010-11-12 11:12:55 -06001265
Christoph Hellwigb58f0e82020-05-28 07:12:09 +02001266 sock_set_reuseaddr(sock->sk);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001267
Alexander Aringd11ccd42020-11-02 20:04:25 -05001268 add_listen_sock(sock, con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001269
1270 /* Bind to our port */
1271 make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
1272 result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
1273 if (result < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001274 log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001275 goto create_out;
1276 }
Christoph Hellwigce3d9542020-05-28 07:12:15 +02001277 sock_set_keepalive(sock->sk);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001278
1279 result = sock->ops->listen(sock, 5);
1280 if (result < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001281 log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001282 goto create_out;
1283 }
1284
Alexander Aringd11ccd42020-11-02 20:04:25 -05001285 return 0;
1286
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001287create_out:
Alexander Aringd11ccd42020-11-02 20:04:25 -05001288 return result;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001289}
1290
1291/* Get local addresses */
1292static void init_local(void)
1293{
1294 struct sockaddr_storage sas, *addr;
1295 int i;
1296
Patrick Caulfield30d3a232007-04-23 16:26:21 +01001297 dlm_local_count = 0;
David Teigland1b189b82012-03-21 09:18:34 -05001298 for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001299 if (dlm_our_addr(&sas, i))
1300 break;
1301
Amitoj Kaur Chawla5c93f562016-06-23 10:22:01 +05301302 addr = kmemdup(&sas, sizeof(*addr), GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001303 if (!addr)
1304 break;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001305 dlm_local_addr[dlm_local_count++] = addr;
1306 }
1307}
1308
Alexander Aring043697f2020-08-27 15:02:50 -04001309static void deinit_local(void)
1310{
1311 int i;
1312
1313 for (i = 0; i < dlm_local_count; i++)
1314 kfree(dlm_local_addr[i]);
1315}
1316
Alexander Aringd11ccd42020-11-02 20:04:25 -05001317/* Initialise SCTP socket and bind to all interfaces
1318 * On error caller must run dlm_close_sock() for the
1319 * listen connection socket.
1320 */
1321static int sctp_listen_for_all(struct listen_connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001322{
1323 struct socket *sock = NULL;
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001324 int result = -EINVAL;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001325
1326 log_print("Using SCTP for communications");
1327
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001328 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001329 SOCK_STREAM, IPPROTO_SCTP, &sock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001330 if (result < 0) {
1331 log_print("Can't create comms socket, check SCTP is loaded");
1332 goto out;
1333 }
1334
Christoph Hellwig26cfabf2020-05-28 07:12:16 +02001335 sock_set_rcvbuf(sock->sk, NEEDED_RMEM);
Alexander Aringa5b7ab62020-06-26 13:26:49 -04001336 sock_set_mark(sock->sk, dlm_config.ci_mark);
Christoph Hellwig40ef92c2020-05-29 14:09:40 +02001337 sctp_sock_set_nodelay(sock->sk);
Mike Christie86e92ad2013-06-14 04:56:14 -05001338
Alexander Aringd11ccd42020-11-02 20:04:25 -05001339 add_listen_sock(sock, con);
Bob Petersonb81171c2016-02-05 14:39:02 -05001340
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001341 /* Bind to all addresses. */
Alexander Aringd11ccd42020-11-02 20:04:25 -05001342 result = sctp_bind_addrs(con->sock, dlm_config.ci_tcp_port);
1343 if (result < 0)
1344 goto out;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001345
1346 result = sock->ops->listen(sock, 5);
1347 if (result < 0) {
1348 log_print("Can't set socket listening");
Alexander Aringd11ccd42020-11-02 20:04:25 -05001349 goto out;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001350 }
1351
1352 return 0;
1353
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001354out:
1355 return result;
1356}
1357
1358static int tcp_listen_for_all(void)
1359{
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001360 /* We don't support multi-homed hosts */
Alexander Aring1a26bfa2020-11-02 20:04:26 -05001361 if (dlm_local_count > 1) {
David Teigland617e82e2007-04-26 13:46:49 -05001362 log_print("TCP protocol can't handle multi-homed hosts, "
1363 "try SCTP");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001364 return -EINVAL;
1365 }
1366
1367 log_print("Using TCP for communications");
1368
Alexander Aringd11ccd42020-11-02 20:04:25 -05001369 return tcp_create_listen_sock(&listen_con, dlm_local_addr[0]);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001370}
1371
1372
1373
1374static struct writequeue_entry *new_writequeue_entry(struct connection *con,
1375 gfp_t allocation)
1376{
1377 struct writequeue_entry *entry;
1378
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001379 entry = kzalloc(sizeof(*entry), allocation);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001380 if (!entry)
1381 return NULL;
1382
Alexander Aringe1a7cbc2021-03-01 17:05:15 -05001383 entry->page = alloc_page(allocation | __GFP_ZERO);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001384 if (!entry->page) {
1385 kfree(entry);
1386 return NULL;
1387 }
1388
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001389 entry->con = con;
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001390 entry->users = 1;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001391
1392 return entry;
1393}
1394
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001395static struct writequeue_entry *new_wq_entry(struct connection *con, int len,
1396 gfp_t allocation, char **ppc)
1397{
1398 struct writequeue_entry *e;
1399
1400 spin_lock(&con->writequeue_lock);
1401 if (!list_empty(&con->writequeue)) {
1402 e = list_last_entry(&con->writequeue, struct writequeue_entry, list);
1403 if (DLM_WQ_REMAIN_BYTES(e) >= len) {
1404 *ppc = page_address(e->page) + e->end;
1405 e->end += len;
1406 e->users++;
1407 spin_unlock(&con->writequeue_lock);
1408
1409 return e;
1410 }
1411 }
1412 spin_unlock(&con->writequeue_lock);
1413
1414 e = new_writequeue_entry(con, allocation);
1415 if (!e)
1416 return NULL;
1417
1418 *ppc = page_address(e->page);
1419 e->end += len;
1420
1421 spin_lock(&con->writequeue_lock);
1422 list_add_tail(&e->list, &con->writequeue);
1423 spin_unlock(&con->writequeue_lock);
1424
1425 return e;
1426};
1427
David Teigland617e82e2007-04-26 13:46:49 -05001428void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001429{
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001430 struct writequeue_entry *e;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001431 struct connection *con;
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001432 int idx;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001433
Alexander Aringc45674f2021-03-01 17:05:14 -05001434 if (len > DEFAULT_BUFFER_SIZE ||
1435 len < sizeof(struct dlm_header)) {
1436 BUILD_BUG_ON(PAGE_SIZE < DEFAULT_BUFFER_SIZE);
Alexander Aring692f51c2020-11-02 20:04:18 -05001437 log_print("failed to allocate a buffer of size %d", len);
Alexander Aringc45674f2021-03-01 17:05:14 -05001438 WARN_ON(1);
Alexander Aring692f51c2020-11-02 20:04:18 -05001439 return NULL;
1440 }
1441
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001442 idx = srcu_read_lock(&connections_srcu);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001443 con = nodeid2con(nodeid, allocation);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001444 if (!con) {
1445 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001446 return NULL;
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001447 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001448
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001449 e = new_wq_entry(con, len, allocation, ppc);
1450 if (!e) {
1451 srcu_read_unlock(&connections_srcu, idx);
1452 return NULL;
1453 }
1454
1455 /* we assume if successful commit must called */
1456 e->idx = idx;
1457
1458 return e;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001459}
1460
1461void dlm_lowcomms_commit_buffer(void *mh)
1462{
1463 struct writequeue_entry *e = (struct writequeue_entry *)mh;
1464 struct connection *con = e->con;
1465 int users;
1466
1467 spin_lock(&con->writequeue_lock);
1468 users = --e->users;
1469 if (users)
1470 goto out;
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001471
1472 e->len = DLM_WQ_LENGTH_BYTES(e);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001473 spin_unlock(&con->writequeue_lock);
1474
Bob Peterson01da24d2017-09-12 08:55:14 +00001475 queue_work(send_workqueue, &con->swork);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001476 srcu_read_unlock(&connections_srcu, e->idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001477 return;
1478
1479out:
1480 spin_unlock(&con->writequeue_lock);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001481 srcu_read_unlock(&connections_srcu, e->idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001482 return;
1483}
1484
1485/* Send a message */
1486static void send_to_sock(struct connection *con)
1487{
1488 int ret = 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001489 const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1490 struct writequeue_entry *e;
1491 int len, offset;
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001492 int count = 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001493
1494 mutex_lock(&con->sock_mutex);
1495 if (con->sock == NULL)
1496 goto out_connect;
1497
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001498 spin_lock(&con->writequeue_lock);
1499 for (;;) {
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001500 if (list_empty(&con->writequeue))
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001501 break;
1502
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001503 e = list_first_entry(&con->writequeue, struct writequeue_entry, list);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001504 len = e->len;
1505 offset = e->offset;
1506 BUG_ON(len == 0 && e->users == 0);
1507 spin_unlock(&con->writequeue_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001508
1509 ret = 0;
1510 if (len) {
Paolo Bonzini1329e3f2009-08-24 13:18:04 -05001511 ret = kernel_sendpage(con->sock, e->page, offset, len,
1512 msg_flags);
Patrick Caulfieldd66f8272007-09-14 08:49:21 +01001513 if (ret == -EAGAIN || ret == 0) {
David Millerb36930d2010-11-10 21:56:39 -08001514 if (ret == -EAGAIN &&
Eric Dumazet9cd3e072015-11-29 20:03:10 -08001515 test_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags) &&
David Millerb36930d2010-11-10 21:56:39 -08001516 !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
1517 /* Notify TCP that we're limited by the
1518 * application window size.
1519 */
1520 set_bit(SOCK_NOSPACE, &con->sock->flags);
1521 con->sock->sk->sk_write_pending++;
1522 }
Patrick Caulfieldd66f8272007-09-14 08:49:21 +01001523 cond_resched();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001524 goto out;
Ying Xue9c5bef52012-08-13 14:29:55 +08001525 } else if (ret < 0)
Alexander Aringba868d92021-05-21 15:08:37 -04001526 goto out;
Patrick Caulfieldd66f8272007-09-14 08:49:21 +01001527 }
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001528
1529 /* Don't starve people filling buffers */
1530 if (++count >= MAX_SEND_MSG_COUNT) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001531 cond_resched();
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001532 count = 0;
1533 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001534
1535 spin_lock(&con->writequeue_lock);
Mike Christie5d689872013-06-14 04:56:13 -05001536 writequeue_entry_complete(e, ret);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001537 }
1538 spin_unlock(&con->writequeue_lock);
1539out:
1540 mutex_unlock(&con->sock_mutex);
1541 return;
1542
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001543out_connect:
1544 mutex_unlock(&con->sock_mutex);
Bob Peterson01da24d2017-09-12 08:55:14 +00001545 queue_work(send_workqueue, &con->swork);
1546 cond_resched();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001547}
1548
1549static void clean_one_writequeue(struct connection *con)
1550{
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001551 struct writequeue_entry *e, *safe;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001552
1553 spin_lock(&con->writequeue_lock);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001554 list_for_each_entry_safe(e, safe, &con->writequeue, list) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001555 list_del(&e->list);
1556 free_entry(e);
1557 }
1558 spin_unlock(&con->writequeue_lock);
1559}
1560
1561/* Called from recovery when it knows that a node has
1562 left the cluster */
1563int dlm_lowcomms_close(int nodeid)
1564{
1565 struct connection *con;
David Teigland36b71a82012-07-26 12:44:30 -05001566 struct dlm_node_addr *na;
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001567 int idx;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001568
1569 log_print("closing connection to node %d", nodeid);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001570 idx = srcu_read_lock(&connections_srcu);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001571 con = nodeid2con(nodeid, 0);
1572 if (con) {
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -05001573 set_bit(CF_CLOSE, &con->flags);
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -03001574 close_connection(con, true, true, true);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001575 clean_one_writequeue(con);
Alexander Aring53a5eda2020-11-02 20:04:19 -05001576 if (con->othercon)
1577 clean_one_writequeue(con->othercon);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001578 }
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001579 srcu_read_unlock(&connections_srcu, idx);
David Teigland36b71a82012-07-26 12:44:30 -05001580
1581 spin_lock(&dlm_node_addrs_spin);
1582 na = find_node_addr(nodeid);
1583 if (na) {
1584 list_del(&na->list);
1585 while (na->addr_count--)
1586 kfree(na->addr[na->addr_count]);
1587 kfree(na);
1588 }
1589 spin_unlock(&dlm_node_addrs_spin);
1590
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001591 return 0;
1592}
1593
1594/* Receive workqueue function */
1595static void process_recv_sockets(struct work_struct *work)
1596{
1597 struct connection *con = container_of(work, struct connection, rwork);
1598 int err;
1599
1600 clear_bit(CF_READ_PENDING, &con->flags);
1601 do {
Alexander Aringd11ccd42020-11-02 20:04:25 -05001602 err = receive_from_sock(con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001603 } while (!err);
1604}
1605
Alexander Aringd11ccd42020-11-02 20:04:25 -05001606static void process_listen_recv_socket(struct work_struct *work)
1607{
1608 accept_from_sock(&listen_con);
1609}
1610
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001611/* Send workqueue function */
1612static void process_send_sockets(struct work_struct *work)
1613{
1614 struct connection *con = container_of(work, struct connection, swork);
1615
Alexander Aring7443bc92021-05-21 15:08:36 -04001616 WARN_ON(test_bit(CF_IS_OTHERCON, &con->flags));
1617
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001618 clear_bit(CF_WRITE_PENDING, &con->flags);
Alexander Aringba868d92021-05-21 15:08:37 -04001619
1620 if (test_and_clear_bit(CF_RECONNECT, &con->flags))
1621 close_connection(con, false, false, true);
1622
1623 if (con->sock == NULL) { /* not mutex protected so check it inside too */
1624 if (test_and_clear_bit(CF_DELAY_CONNECT, &con->flags))
1625 msleep(1000);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001626 con->connect_action(con);
Alexander Aringba868d92021-05-21 15:08:37 -04001627 }
Bob Peterson01da24d2017-09-12 08:55:14 +00001628 if (!list_empty(&con->writequeue))
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -05001629 send_to_sock(con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001630}
1631
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001632static void work_stop(void)
1633{
David Windsorb3555162019-04-02 08:37:10 -04001634 if (recv_workqueue)
1635 destroy_workqueue(recv_workqueue);
1636 if (send_workqueue)
1637 destroy_workqueue(send_workqueue);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001638}
1639
1640static int work_start(void)
1641{
David Teiglande43f0552011-03-10 13:22:34 -06001642 recv_workqueue = alloc_workqueue("dlm_recv",
1643 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001644 if (!recv_workqueue) {
1645 log_print("can't start dlm_recv");
1646 return -ENOMEM;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001647 }
1648
David Teiglande43f0552011-03-10 13:22:34 -06001649 send_workqueue = alloc_workqueue("dlm_send",
1650 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001651 if (!send_workqueue) {
1652 log_print("can't start dlm_send");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001653 destroy_workqueue(recv_workqueue);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001654 return -ENOMEM;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001655 }
1656
1657 return 0;
1658}
1659
Alexander Aring9d232462021-03-01 17:05:20 -05001660static void shutdown_conn(struct connection *con)
1661{
1662 if (con->shutdown_action)
1663 con->shutdown_action(con);
1664}
1665
1666void dlm_lowcomms_shutdown(void)
1667{
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001668 int idx;
1669
Alexander Aring9d232462021-03-01 17:05:20 -05001670 /* Set all the flags to prevent any
1671 * socket activity.
1672 */
1673 dlm_allow_conn = 0;
1674
1675 if (recv_workqueue)
1676 flush_workqueue(recv_workqueue);
1677 if (send_workqueue)
1678 flush_workqueue(send_workqueue);
1679
1680 dlm_close_sock(&listen_con.sock);
1681
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001682 idx = srcu_read_lock(&connections_srcu);
Alexander Aring9d232462021-03-01 17:05:20 -05001683 foreach_conn(shutdown_conn);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001684 srcu_read_unlock(&connections_srcu, idx);
Alexander Aring9d232462021-03-01 17:05:20 -05001685}
1686
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001687static void _stop_conn(struct connection *con, bool and_other)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001688{
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001689 mutex_lock(&con->sock_mutex);
tsutomu.owa@toshiba.co.jp173a31f2017-09-12 09:01:24 +00001690 set_bit(CF_CLOSE, &con->flags);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001691 set_bit(CF_READ_PENDING, &con->flags);
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001692 set_bit(CF_WRITE_PENDING, &con->flags);
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +00001693 if (con->sock && con->sock->sk) {
1694 write_lock_bh(&con->sock->sk->sk_callback_lock);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001695 con->sock->sk->sk_user_data = NULL;
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +00001696 write_unlock_bh(&con->sock->sk->sk_callback_lock);
1697 }
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001698 if (con->othercon && and_other)
1699 _stop_conn(con->othercon, false);
1700 mutex_unlock(&con->sock_mutex);
1701}
1702
1703static void stop_conn(struct connection *con)
1704{
1705 _stop_conn(con, true);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001706}
1707
Alexander Aring4798cbb2020-09-24 10:31:26 -04001708static void connection_release(struct rcu_head *rcu)
1709{
1710 struct connection *con = container_of(rcu, struct connection, rcu);
1711
1712 kfree(con->rx_buf);
1713 kfree(con);
1714}
1715
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001716static void free_conn(struct connection *con)
1717{
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -03001718 close_connection(con, true, true, true);
Alexander Aringa47666eb2020-08-27 15:02:49 -04001719 spin_lock(&connections_lock);
1720 hlist_del_rcu(&con->list);
1721 spin_unlock(&connections_lock);
Alexander Aring948c47e2020-08-27 15:02:53 -04001722 if (con->othercon) {
1723 clean_one_writequeue(con->othercon);
Alexander Aring5cbec202020-11-02 20:04:16 -05001724 call_srcu(&connections_srcu, &con->othercon->rcu,
1725 connection_release);
Alexander Aring948c47e2020-08-27 15:02:53 -04001726 }
Alexander Aring0de98432020-08-27 15:02:52 -04001727 clean_one_writequeue(con);
Alexander Aring5cbec202020-11-02 20:04:16 -05001728 call_srcu(&connections_srcu, &con->rcu, connection_release);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001729}
1730
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001731static void work_flush(void)
1732{
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001733 int ok;
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001734 int i;
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001735 struct connection *con;
1736
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001737 do {
1738 ok = 1;
1739 foreach_conn(stop_conn);
David Windsorb3555162019-04-02 08:37:10 -04001740 if (recv_workqueue)
1741 flush_workqueue(recv_workqueue);
1742 if (send_workqueue)
1743 flush_workqueue(send_workqueue);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001744 for (i = 0; i < CONN_HASH_SIZE && ok; i++) {
Alexander Aringa47666eb2020-08-27 15:02:49 -04001745 hlist_for_each_entry_rcu(con, &connection_hash[i],
1746 list) {
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001747 ok &= test_bit(CF_READ_PENDING, &con->flags);
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001748 ok &= test_bit(CF_WRITE_PENDING, &con->flags);
1749 if (con->othercon) {
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001750 ok &= test_bit(CF_READ_PENDING,
1751 &con->othercon->flags);
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001752 ok &= test_bit(CF_WRITE_PENDING,
1753 &con->othercon->flags);
1754 }
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001755 }
1756 }
1757 } while (!ok);
1758}
1759
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001760void dlm_lowcomms_stop(void)
1761{
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001762 int idx;
1763
1764 idx = srcu_read_lock(&connections_srcu);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001765 work_flush();
Marcelo Ricardo Leitner3a8db792016-10-08 10:14:37 -03001766 foreach_conn(free_conn);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001767 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001768 work_stop();
Alexander Aring043697f2020-08-27 15:02:50 -04001769 deinit_local();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001770}
1771
1772int dlm_lowcomms_start(void)
1773{
1774 int error = -EINVAL;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001775 int i;
1776
1777 for (i = 0; i < CONN_HASH_SIZE; i++)
1778 INIT_HLIST_HEAD(&connection_hash[i]);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001779
1780 init_local();
1781 if (!dlm_local_count) {
David Teigland617e82e2007-04-26 13:46:49 -05001782 error = -ENOTCONN;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001783 log_print("no local IP address has been set");
David Teigland513ef592012-03-30 11:46:08 -05001784 goto fail;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001785 }
1786
Alexander Aringd11ccd42020-11-02 20:04:25 -05001787 INIT_WORK(&listen_con.rwork, process_listen_recv_socket);
1788
David Teigland513ef592012-03-30 11:46:08 -05001789 error = work_start();
1790 if (error)
Alexander Aringa47666eb2020-08-27 15:02:49 -04001791 goto fail;
David Teigland513ef592012-03-30 11:46:08 -05001792
1793 dlm_allow_conn = 1;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001794
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001795 /* Start listening */
1796 if (dlm_config.ci_protocol == 0)
1797 error = tcp_listen_for_all();
1798 else
Alexander Aringd11ccd42020-11-02 20:04:25 -05001799 error = sctp_listen_for_all(&listen_con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001800 if (error)
1801 goto fail_unlisten;
1802
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001803 return 0;
1804
1805fail_unlisten:
David Teigland513ef592012-03-30 11:46:08 -05001806 dlm_allow_conn = 0;
Alexander Aringd11ccd42020-11-02 20:04:25 -05001807 dlm_close_sock(&listen_con.sock);
David Teigland513ef592012-03-30 11:46:08 -05001808fail:
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001809 return error;
1810}
David Teigland36b71a82012-07-26 12:44:30 -05001811
1812void dlm_lowcomms_exit(void)
1813{
1814 struct dlm_node_addr *na, *safe;
1815
1816 spin_lock(&dlm_node_addrs_spin);
1817 list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) {
1818 list_del(&na->list);
1819 while (na->addr_count--)
1820 kfree(na->addr[na->addr_count]);
1821 kfree(na);
1822 }
1823 spin_unlock(&dlm_node_addrs_spin);
1824}