blob: f7fc1ac76ce83a5f250852f70783df2e724df5d1 [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
Alexander Aring92732372021-11-02 15:17:16 -040056#include <trace/events/dlm.h>
57
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010058#include "dlm_internal.h"
59#include "lowcomms.h"
60#include "midcomms.h"
61#include "config.h"
62
63#define NEEDED_RMEM (4*1024*1024)
64
Bob Petersonf92c8dd2010-11-12 11:15:20 -060065/* Number of messages to send before rescheduling */
66#define MAX_SEND_MSG_COUNT 25
Alexander Aring055923b2020-07-27 09:13:38 -040067#define DLM_SHUTDOWN_WAIT_TIMEOUT msecs_to_jiffies(10000)
Bob Petersonf92c8dd2010-11-12 11:15:20 -060068
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010069struct connection {
70 struct socket *sock; /* NULL if not connected */
71 uint32_t nodeid; /* So we know who we are in the list */
72 struct mutex sock_mutex;
73 unsigned long flags;
74#define CF_READ_PENDING 1
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +000075#define CF_WRITE_PENDING 2
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010076#define CF_INIT_PENDING 4
77#define CF_IS_OTHERCON 5
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -050078#define CF_CLOSE 6
David Millerb36930d2010-11-10 21:56:39 -080079#define CF_APP_LIMITED 7
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +000080#define CF_CLOSING 8
Alexander Aring055923b2020-07-27 09:13:38 -040081#define CF_SHUTDOWN 9
Alexander Aring19633c72020-11-02 20:04:20 -050082#define CF_CONNECTED 10
Alexander Aringba868d92021-05-21 15:08:37 -040083#define CF_RECONNECT 11
84#define CF_DELAY_CONNECT 12
Alexander Aring8aa31cb2021-05-21 15:08:39 -040085#define CF_EOF 13
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010086 struct list_head writequeue; /* List of outgoing writequeue_entries */
87 spinlock_t writequeue_lock;
Alexander Aring8aa31cb2021-05-21 15:08:39 -040088 atomic_t writequeue_cnt;
Alexander Aringc51b0222021-07-16 16:22:44 -040089 struct mutex wq_alloc;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010090 int retries;
91#define MAX_CONNECT_RETRIES 3
Christine Caulfield5e9ccc32009-01-28 12:57:40 -060092 struct hlist_node list;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010093 struct connection *othercon;
Alexander Aringba868d92021-05-21 15:08:37 -040094 struct connection *sendcon;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010095 struct work_struct rwork; /* Receive workqueue */
96 struct work_struct swork; /* Send workqueue */
Alexander Aring055923b2020-07-27 09:13:38 -040097 wait_queue_head_t shutdown_wait; /* wait for graceful shutdown */
Alexander Aring4798cbb2020-09-24 10:31:26 -040098 unsigned char *rx_buf;
99 int rx_buflen;
100 int rx_leftover;
Alexander Aringa47666eb2020-08-27 15:02:49 -0400101 struct rcu_head rcu;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100102};
103#define sock2con(x) ((struct connection *)(x)->sk_user_data)
104
Alexander Aringd11ccd42020-11-02 20:04:25 -0500105struct listen_connection {
106 struct socket *sock;
107 struct work_struct rwork;
108};
109
Alexander Aringf0747ebf2021-03-01 17:05:16 -0500110#define DLM_WQ_REMAIN_BYTES(e) (PAGE_SIZE - e->end)
111#define DLM_WQ_LENGTH_BYTES(e) (e->end - e->offset)
112
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100113/* An entry waiting to be sent */
114struct writequeue_entry {
115 struct list_head list;
116 struct page *page;
117 int offset;
118 int len;
119 int end;
120 int users;
Alexander Aring706474f2021-05-21 15:08:48 -0400121 bool dirty;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100122 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
Alexander Aringa66c0082021-07-16 16:22:40 -0400148struct dlm_proto_ops {
Alexander Aring8728a452021-07-16 16:22:43 -0400149 bool try_new_addr;
Alexander Aring2dc6b112021-07-16 16:22:41 -0400150 const char *name;
151 int proto;
152
Alexander Aring8728a452021-07-16 16:22:43 -0400153 int (*connect)(struct connection *con, struct socket *sock,
154 struct sockaddr *addr, int addr_len);
155 void (*sockopts)(struct socket *sock);
156 int (*bind)(struct socket *sock);
Alexander Aring2dc6b112021-07-16 16:22:41 -0400157 int (*listen_validate)(void);
158 void (*listen_sockopts)(struct socket *sock);
159 int (*listen_bind)(struct socket *sock);
Alexander Aringa66c0082021-07-16 16:22:40 -0400160 /* What to do to shutdown */
161 void (*shutdown_action)(struct connection *con);
162 /* What to do to eof check */
163 bool (*eof_condition)(struct connection *con);
164};
165
Bob Petersoncc661fc2017-09-12 08:55:23 +0000166static struct listen_sock_callbacks {
167 void (*sk_error_report)(struct sock *);
168 void (*sk_data_ready)(struct sock *);
169 void (*sk_state_change)(struct sock *);
170 void (*sk_write_space)(struct sock *);
171} listen_sock;
172
David Teigland36b71a82012-07-26 12:44:30 -0500173static LIST_HEAD(dlm_node_addrs);
174static DEFINE_SPINLOCK(dlm_node_addrs_spin);
175
Alexander Aringd11ccd42020-11-02 20:04:25 -0500176static struct listen_connection listen_con;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100177static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
178static int dlm_local_count;
Alexander Aring51746162021-03-01 17:05:13 -0500179int dlm_allow_conn;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100180
181/* Work queues */
182static struct workqueue_struct *recv_workqueue;
183static struct workqueue_struct *send_workqueue;
184
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600185static struct hlist_head connection_hash[CONN_HASH_SIZE];
Alexander Aringa47666eb2020-08-27 15:02:49 -0400186static DEFINE_SPINLOCK(connections_lock);
187DEFINE_STATIC_SRCU(connections_srcu);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100188
Alexander Aringa66c0082021-07-16 16:22:40 -0400189static const struct dlm_proto_ops *dlm_proto_ops;
190
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100191static void process_recv_sockets(struct work_struct *work);
192static void process_send_sockets(struct work_struct *work);
193
Alexander Aring66d59552021-07-16 16:22:39 -0400194/* need to held writequeue_lock */
195static struct writequeue_entry *con_next_wq(struct connection *con)
196{
197 struct writequeue_entry *e;
198
199 if (list_empty(&con->writequeue))
200 return NULL;
201
202 e = list_first_entry(&con->writequeue, struct writequeue_entry,
203 list);
204 if (e->len == 0)
205 return NULL;
206
207 return e;
208}
209
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400210static struct connection *__find_con(int nodeid, int r)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600211{
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600212 struct connection *con;
213
Alexander Aringa47666eb2020-08-27 15:02:49 -0400214 hlist_for_each_entry_rcu(con, &connection_hash[r], list) {
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400215 if (con->nodeid == nodeid)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600216 return con;
217 }
Alexander Aringa47666eb2020-08-27 15:02:49 -0400218
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600219 return NULL;
220}
221
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400222static bool tcp_eof_condition(struct connection *con)
223{
224 return atomic_read(&con->writequeue_cnt);
225}
226
Alexander Aring6cde2102020-11-02 20:04:21 -0500227static int dlm_con_init(struct connection *con, int nodeid)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100228{
Alexander Aring4798cbb2020-09-24 10:31:26 -0400229 con->rx_buflen = dlm_config.ci_buffer_size;
230 con->rx_buf = kmalloc(con->rx_buflen, GFP_NOFS);
Alexander Aring6cde2102020-11-02 20:04:21 -0500231 if (!con->rx_buf)
232 return -ENOMEM;
Alexander Aring4798cbb2020-09-24 10:31:26 -0400233
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100234 con->nodeid = nodeid;
235 mutex_init(&con->sock_mutex);
236 INIT_LIST_HEAD(&con->writequeue);
237 spin_lock_init(&con->writequeue_lock);
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400238 atomic_set(&con->writequeue_cnt, 0);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100239 INIT_WORK(&con->swork, process_send_sockets);
240 INIT_WORK(&con->rwork, process_recv_sockets);
Alexander Aring055923b2020-07-27 09:13:38 -0400241 init_waitqueue_head(&con->shutdown_wait);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100242
Alexander Aring6cde2102020-11-02 20:04:21 -0500243 return 0;
244}
245
246/*
247 * If 'allocation' is zero then we don't attempt to create a new
248 * connection structure for this node.
249 */
250static struct connection *nodeid2con(int nodeid, gfp_t alloc)
251{
252 struct connection *con, *tmp;
253 int r, ret;
254
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400255 r = nodeid_hash(nodeid);
256 con = __find_con(nodeid, r);
Alexander Aring6cde2102020-11-02 20:04:21 -0500257 if (con || !alloc)
258 return con;
259
260 con = kzalloc(sizeof(*con), alloc);
261 if (!con)
262 return NULL;
263
264 ret = dlm_con_init(con, nodeid);
265 if (ret) {
266 kfree(con);
267 return NULL;
268 }
269
Alexander Aringc51b0222021-07-16 16:22:44 -0400270 mutex_init(&con->wq_alloc);
271
Alexander Aringa47666eb2020-08-27 15:02:49 -0400272 spin_lock(&connections_lock);
Alexander Aring4f2b30f2020-09-30 18:37:29 -0400273 /* Because multiple workqueues/threads calls this function it can
274 * race on multiple cpu's. Instead of locking hot path __find_con()
275 * we just check in rare cases of recently added nodes again
276 * under protection of connections_lock. If this is the case we
277 * abort our connection creation and return the existing connection.
278 */
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400279 tmp = __find_con(nodeid, r);
Alexander Aring4f2b30f2020-09-30 18:37:29 -0400280 if (tmp) {
281 spin_unlock(&connections_lock);
282 kfree(con->rx_buf);
283 kfree(con);
284 return tmp;
285 }
286
Alexander Aringa47666eb2020-08-27 15:02:49 -0400287 hlist_add_head_rcu(&con->list, &connection_hash[r]);
288 spin_unlock(&connections_lock);
289
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100290 return con;
291}
292
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600293/* Loop round all connections */
294static void foreach_conn(void (*conn_func)(struct connection *c))
295{
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400296 int i;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600297 struct connection *con;
298
299 for (i = 0; i < CONN_HASH_SIZE; i++) {
Alexander Aringa47666eb2020-08-27 15:02:49 -0400300 hlist_for_each_entry_rcu(con, &connection_hash[i], list)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600301 conn_func(con);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600302 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100303}
304
David Teigland36b71a82012-07-26 12:44:30 -0500305static struct dlm_node_addr *find_node_addr(int nodeid)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100306{
David Teigland36b71a82012-07-26 12:44:30 -0500307 struct dlm_node_addr *na;
308
309 list_for_each_entry(na, &dlm_node_addrs, list) {
310 if (na->nodeid == nodeid)
311 return na;
312 }
313 return NULL;
314}
315
Alexander Aring40c6b832020-11-02 20:04:27 -0500316static int addr_compare(const struct sockaddr_storage *x,
317 const struct sockaddr_storage *y)
David Teigland36b71a82012-07-26 12:44:30 -0500318{
319 switch (x->ss_family) {
320 case AF_INET: {
321 struct sockaddr_in *sinx = (struct sockaddr_in *)x;
322 struct sockaddr_in *siny = (struct sockaddr_in *)y;
323 if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
324 return 0;
325 if (sinx->sin_port != siny->sin_port)
326 return 0;
327 break;
328 }
329 case AF_INET6: {
330 struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
331 struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
332 if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
333 return 0;
334 if (sinx->sin6_port != siny->sin6_port)
335 return 0;
336 break;
337 }
338 default:
339 return 0;
340 }
341 return 1;
342}
343
344static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out,
Alexander Aringe125fbe2021-03-01 17:05:09 -0500345 struct sockaddr *sa_out, bool try_new_addr,
346 unsigned int *mark)
David Teigland36b71a82012-07-26 12:44:30 -0500347{
348 struct sockaddr_storage sas;
349 struct dlm_node_addr *na;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100350
351 if (!dlm_local_count)
352 return -1;
353
David Teigland36b71a82012-07-26 12:44:30 -0500354 spin_lock(&dlm_node_addrs_spin);
355 na = find_node_addr(nodeid);
Mike Christie98e1b602013-06-14 04:56:12 -0500356 if (na && na->addr_count) {
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300357 memcpy(&sas, na->addr[na->curr_addr_index],
358 sizeof(struct sockaddr_storage));
359
Mike Christie98e1b602013-06-14 04:56:12 -0500360 if (try_new_addr) {
361 na->curr_addr_index++;
362 if (na->curr_addr_index == na->addr_count)
363 na->curr_addr_index = 0;
364 }
Mike Christie98e1b602013-06-14 04:56:12 -0500365 }
David Teigland36b71a82012-07-26 12:44:30 -0500366 spin_unlock(&dlm_node_addrs_spin);
367
368 if (!na)
369 return -EEXIST;
370
371 if (!na->addr_count)
372 return -ENOENT;
373
Alexander Aringe125fbe2021-03-01 17:05:09 -0500374 *mark = na->mark;
375
David Teigland36b71a82012-07-26 12:44:30 -0500376 if (sas_out)
377 memcpy(sas_out, &sas, sizeof(struct sockaddr_storage));
378
379 if (!sa_out)
380 return 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100381
382 if (dlm_local_addr[0]->ss_family == AF_INET) {
David Teigland36b71a82012-07-26 12:44:30 -0500383 struct sockaddr_in *in4 = (struct sockaddr_in *) &sas;
384 struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100385 ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
386 } else {
David Teigland36b71a82012-07-26 12:44:30 -0500387 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sas;
388 struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000389 ret6->sin6_addr = in6->sin6_addr;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100390 }
391
392 return 0;
393}
394
Alexander Aringe125fbe2021-03-01 17:05:09 -0500395static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid,
396 unsigned int *mark)
David Teigland36b71a82012-07-26 12:44:30 -0500397{
398 struct dlm_node_addr *na;
399 int rv = -EEXIST;
Mike Christie98e1b602013-06-14 04:56:12 -0500400 int addr_i;
David Teigland36b71a82012-07-26 12:44:30 -0500401
402 spin_lock(&dlm_node_addrs_spin);
403 list_for_each_entry(na, &dlm_node_addrs, list) {
404 if (!na->addr_count)
405 continue;
406
Mike Christie98e1b602013-06-14 04:56:12 -0500407 for (addr_i = 0; addr_i < na->addr_count; addr_i++) {
408 if (addr_compare(na->addr[addr_i], addr)) {
409 *nodeid = na->nodeid;
Alexander Aringe125fbe2021-03-01 17:05:09 -0500410 *mark = na->mark;
Mike Christie98e1b602013-06-14 04:56:12 -0500411 rv = 0;
412 goto unlock;
413 }
414 }
David Teigland36b71a82012-07-26 12:44:30 -0500415 }
Mike Christie98e1b602013-06-14 04:56:12 -0500416unlock:
David Teigland36b71a82012-07-26 12:44:30 -0500417 spin_unlock(&dlm_node_addrs_spin);
418 return rv;
419}
420
Alexander Aring4f19d072020-11-02 20:04:28 -0500421/* caller need to held dlm_node_addrs_spin lock */
422static bool dlm_lowcomms_na_has_addr(const struct dlm_node_addr *na,
423 const struct sockaddr_storage *addr)
424{
425 int i;
426
427 for (i = 0; i < na->addr_count; i++) {
428 if (addr_compare(na->addr[i], addr))
429 return true;
430 }
431
432 return false;
433}
434
David Teigland36b71a82012-07-26 12:44:30 -0500435int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr, int len)
436{
437 struct sockaddr_storage *new_addr;
438 struct dlm_node_addr *new_node, *na;
Alexander Aring4f19d072020-11-02 20:04:28 -0500439 bool ret;
David Teigland36b71a82012-07-26 12:44:30 -0500440
441 new_node = kzalloc(sizeof(struct dlm_node_addr), GFP_NOFS);
442 if (!new_node)
443 return -ENOMEM;
444
445 new_addr = kzalloc(sizeof(struct sockaddr_storage), GFP_NOFS);
446 if (!new_addr) {
447 kfree(new_node);
448 return -ENOMEM;
449 }
450
451 memcpy(new_addr, addr, len);
452
453 spin_lock(&dlm_node_addrs_spin);
454 na = find_node_addr(nodeid);
455 if (!na) {
456 new_node->nodeid = nodeid;
457 new_node->addr[0] = new_addr;
458 new_node->addr_count = 1;
Alexander Aringe125fbe2021-03-01 17:05:09 -0500459 new_node->mark = dlm_config.ci_mark;
David Teigland36b71a82012-07-26 12:44:30 -0500460 list_add(&new_node->list, &dlm_node_addrs);
461 spin_unlock(&dlm_node_addrs_spin);
462 return 0;
463 }
464
Alexander Aring4f19d072020-11-02 20:04:28 -0500465 ret = dlm_lowcomms_na_has_addr(na, addr);
466 if (ret) {
467 spin_unlock(&dlm_node_addrs_spin);
468 kfree(new_addr);
469 kfree(new_node);
470 return -EEXIST;
471 }
472
David Teigland36b71a82012-07-26 12:44:30 -0500473 if (na->addr_count >= DLM_MAX_ADDR_COUNT) {
474 spin_unlock(&dlm_node_addrs_spin);
475 kfree(new_addr);
476 kfree(new_node);
477 return -ENOSPC;
478 }
479
480 na->addr[na->addr_count++] = new_addr;
481 spin_unlock(&dlm_node_addrs_spin);
482 kfree(new_node);
483 return 0;
484}
485
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100486/* Data available on socket or listen socket received a connect */
David S. Miller676d2362014-04-11 16:15:36 -0400487static void lowcomms_data_ready(struct sock *sk)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100488{
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000489 struct connection *con;
490
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000491 con = sock2con(sk);
Patrick Caulfieldafb853f2007-06-01 10:07:26 -0500492 if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100493 queue_work(recv_workqueue, &con->rwork);
494}
495
Alexander Aringd11ccd42020-11-02 20:04:25 -0500496static void lowcomms_listen_data_ready(struct sock *sk)
497{
Alexander Aring9a4139a2021-06-02 09:45:18 -0400498 if (!dlm_allow_conn)
499 return;
500
Alexander Aringd11ccd42020-11-02 20:04:25 -0500501 queue_work(recv_workqueue, &listen_con.rwork);
502}
503
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100504static void lowcomms_write_space(struct sock *sk)
505{
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000506 struct connection *con;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100507
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000508 con = sock2con(sk);
David Millerb36930d2010-11-10 21:56:39 -0800509 if (!con)
Alexander Aring92c44602021-11-15 08:57:06 -0500510 return;
David Millerb36930d2010-11-10 21:56:39 -0800511
Alexander Aring19633c72020-11-02 20:04:20 -0500512 if (!test_and_set_bit(CF_CONNECTED, &con->flags)) {
513 log_print("successful connected to node %d", con->nodeid);
514 queue_work(send_workqueue, &con->swork);
Alexander Aring92c44602021-11-15 08:57:06 -0500515 return;
Alexander Aring19633c72020-11-02 20:04:20 -0500516 }
517
David Millerb36930d2010-11-10 21:56:39 -0800518 clear_bit(SOCK_NOSPACE, &con->sock->flags);
519
520 if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) {
521 con->sock->sk->sk_write_pending--;
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800522 clear_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags);
David Millerb36930d2010-11-10 21:56:39 -0800523 }
524
Bob Peterson01da24d2017-09-12 08:55:14 +0000525 queue_work(send_workqueue, &con->swork);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100526}
527
528static inline void lowcomms_connect_sock(struct connection *con)
529{
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -0500530 if (test_bit(CF_CLOSE, &con->flags))
531 return;
Bob Peterson61d9102b2017-09-12 08:55:04 +0000532 queue_work(send_workqueue, &con->swork);
533 cond_resched();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100534}
535
536static void lowcomms_state_change(struct sock *sk)
537{
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300538 /* SCTP layer is not calling sk_data_ready when the connection
539 * is done, so we catch the signal through here. Also, it
540 * doesn't switch socket state when entering shutdown, so we
541 * skip the write in that case.
542 */
543 if (sk->sk_shutdown) {
544 if (sk->sk_shutdown == RCV_SHUTDOWN)
545 lowcomms_data_ready(sk);
546 } else if (sk->sk_state == TCP_ESTABLISHED) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100547 lowcomms_write_space(sk);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300548 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100549}
550
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500551int dlm_lowcomms_connect_node(int nodeid)
552{
553 struct connection *con;
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400554 int idx;
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500555
556 if (nodeid == dlm_our_nodeid())
557 return 0;
558
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400559 idx = srcu_read_lock(&connections_srcu);
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500560 con = nodeid2con(nodeid, GFP_NOFS);
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400561 if (!con) {
562 srcu_read_unlock(&connections_srcu, idx);
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500563 return -ENOMEM;
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400564 }
565
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500566 lowcomms_connect_sock(con);
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400567 srcu_read_unlock(&connections_srcu, idx);
568
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500569 return 0;
570}
571
Alexander Aringe125fbe2021-03-01 17:05:09 -0500572int dlm_lowcomms_nodes_set_mark(int nodeid, unsigned int mark)
573{
574 struct dlm_node_addr *na;
575
576 spin_lock(&dlm_node_addrs_spin);
577 na = find_node_addr(nodeid);
578 if (!na) {
579 spin_unlock(&dlm_node_addrs_spin);
580 return -ENOENT;
581 }
582
583 na->mark = mark;
584 spin_unlock(&dlm_node_addrs_spin);
585
586 return 0;
587}
588
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500589static void lowcomms_error_report(struct sock *sk)
590{
Bob Petersonb81171c2016-02-05 14:39:02 -0500591 struct connection *con;
Bob Petersonb81171c2016-02-05 14:39:02 -0500592 void (*orig_report)(struct sock *) = NULL;
Alexander Aring4c3d9052021-11-15 08:57:05 -0500593 struct inet_sock *inet;
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500594
Bob Petersonb81171c2016-02-05 14:39:02 -0500595 con = sock2con(sk);
596 if (con == NULL)
597 goto out;
598
Bob Petersoncc661fc2017-09-12 08:55:23 +0000599 orig_report = listen_sock.sk_error_report;
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500600
Alexander Aring4c3d9052021-11-15 08:57:05 -0500601 inet = inet_sk(sk);
602 switch (sk->sk_family) {
603 case AF_INET:
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500604 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
Alexander Aring4c3d9052021-11-15 08:57:05 -0500605 "sending to node %d at %pI4, dport %d, "
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500606 "sk_err=%d/%d\n", dlm_our_nodeid(),
Alexander Aring4c3d9052021-11-15 08:57:05 -0500607 con->nodeid, &inet->inet_daddr,
608 ntohs(inet->inet_dport), sk->sk_err,
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500609 sk->sk_err_soft);
Alexander Aring4c3d9052021-11-15 08:57:05 -0500610 break;
Alexander Aring1b9beda2021-11-17 09:20:43 -0500611#if IS_ENABLED(CONFIG_IPV6)
Alexander Aring4c3d9052021-11-15 08:57:05 -0500612 case AF_INET6:
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500613 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
Alexander Aring4c3d9052021-11-15 08:57:05 -0500614 "sending to node %d at %pI6c, "
615 "dport %d, sk_err=%d/%d\n", dlm_our_nodeid(),
616 con->nodeid, &sk->sk_v6_daddr,
617 ntohs(inet->inet_dport), sk->sk_err,
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500618 sk->sk_err_soft);
Alexander Aring4c3d9052021-11-15 08:57:05 -0500619 break;
Alexander Aring1b9beda2021-11-17 09:20:43 -0500620#endif
Alexander Aring4c3d9052021-11-15 08:57:05 -0500621 default:
622 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
623 "invalid socket family %d set, "
624 "sk_err=%d/%d\n", dlm_our_nodeid(),
625 sk->sk_family, sk->sk_err, sk->sk_err_soft);
626 goto out;
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500627 }
Alexander Aringba868d92021-05-21 15:08:37 -0400628
629 /* below sendcon only handling */
630 if (test_bit(CF_IS_OTHERCON, &con->flags))
631 con = con->sendcon;
632
633 switch (sk->sk_err) {
634 case ECONNREFUSED:
635 set_bit(CF_DELAY_CONNECT, &con->flags);
636 break;
637 default:
638 break;
639 }
640
641 if (!test_and_set_bit(CF_RECONNECT, &con->flags))
642 queue_work(send_workqueue, &con->swork);
643
Bob Petersonb81171c2016-02-05 14:39:02 -0500644out:
Bob Petersonb81171c2016-02-05 14:39:02 -0500645 if (orig_report)
646 orig_report(sk);
647}
648
649/* Note: sk_callback_lock must be locked before calling this function. */
Bob Petersoncc661fc2017-09-12 08:55:23 +0000650static void save_listen_callbacks(struct socket *sock)
Bob Petersonb81171c2016-02-05 14:39:02 -0500651{
Bob Petersoncc661fc2017-09-12 08:55:23 +0000652 struct sock *sk = sock->sk;
653
654 listen_sock.sk_data_ready = sk->sk_data_ready;
655 listen_sock.sk_state_change = sk->sk_state_change;
656 listen_sock.sk_write_space = sk->sk_write_space;
657 listen_sock.sk_error_report = sk->sk_error_report;
Bob Petersonb81171c2016-02-05 14:39:02 -0500658}
659
Bob Petersoncc661fc2017-09-12 08:55:23 +0000660static void restore_callbacks(struct socket *sock)
Bob Petersonb81171c2016-02-05 14:39:02 -0500661{
Bob Petersoncc661fc2017-09-12 08:55:23 +0000662 struct sock *sk = sock->sk;
663
Alexander Aring92c44602021-11-15 08:57:06 -0500664 lock_sock(sk);
Bob Petersonb81171c2016-02-05 14:39:02 -0500665 sk->sk_user_data = NULL;
Bob Petersoncc661fc2017-09-12 08:55:23 +0000666 sk->sk_data_ready = listen_sock.sk_data_ready;
667 sk->sk_state_change = listen_sock.sk_state_change;
668 sk->sk_write_space = listen_sock.sk_write_space;
669 sk->sk_error_report = listen_sock.sk_error_report;
Alexander Aring92c44602021-11-15 08:57:06 -0500670 release_sock(sk);
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500671}
672
Alexander Aringd11ccd42020-11-02 20:04:25 -0500673static void add_listen_sock(struct socket *sock, struct listen_connection *con)
674{
675 struct sock *sk = sock->sk;
676
Alexander Aring92c44602021-11-15 08:57:06 -0500677 lock_sock(sk);
Alexander Aringd11ccd42020-11-02 20:04:25 -0500678 save_listen_callbacks(sock);
679 con->sock = sock;
680
681 sk->sk_user_data = con;
682 sk->sk_allocation = GFP_NOFS;
683 /* Install a data_ready callback */
684 sk->sk_data_ready = lowcomms_listen_data_ready;
Alexander Aring92c44602021-11-15 08:57:06 -0500685 release_sock(sk);
Alexander Aringd11ccd42020-11-02 20:04:25 -0500686}
687
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100688/* Make a socket active */
tsutomu.owa@toshiba.co.jp988419a2017-09-12 08:55:32 +0000689static void add_sock(struct socket *sock, struct connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100690{
Bob Petersonb81171c2016-02-05 14:39:02 -0500691 struct sock *sk = sock->sk;
692
Alexander Aring92c44602021-11-15 08:57:06 -0500693 lock_sock(sk);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100694 con->sock = sock;
695
Bob Petersonb81171c2016-02-05 14:39:02 -0500696 sk->sk_user_data = con;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100697 /* Install a data_ready callback */
Bob Petersonb81171c2016-02-05 14:39:02 -0500698 sk->sk_data_ready = lowcomms_data_ready;
699 sk->sk_write_space = lowcomms_write_space;
700 sk->sk_state_change = lowcomms_state_change;
701 sk->sk_allocation = GFP_NOFS;
702 sk->sk_error_report = lowcomms_error_report;
Alexander Aring92c44602021-11-15 08:57:06 -0500703 release_sock(sk);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100704}
705
706/* Add the port number to an IPv6 or 4 sockaddr and return the address
707 length */
708static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
709 int *addr_len)
710{
711 saddr->ss_family = dlm_local_addr[0]->ss_family;
712 if (saddr->ss_family == AF_INET) {
713 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
714 in4_addr->sin_port = cpu_to_be16(port);
715 *addr_len = sizeof(struct sockaddr_in);
716 memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
717 } else {
718 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
719 in6_addr->sin6_port = cpu_to_be16(port);
720 *addr_len = sizeof(struct sockaddr_in6);
721 }
Patrick Caulfield01c8cab2007-07-17 16:53:15 +0100722 memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100723}
724
Alexander Aring706474f2021-05-21 15:08:48 -0400725static void dlm_page_release(struct kref *kref)
726{
727 struct writequeue_entry *e = container_of(kref, struct writequeue_entry,
728 ref);
729
730 __free_page(e->page);
731 kfree(e);
732}
733
734static void dlm_msg_release(struct kref *kref)
735{
736 struct dlm_msg *msg = container_of(kref, struct dlm_msg, ref);
737
738 kref_put(&msg->entry->ref, dlm_page_release);
739 kfree(msg);
740}
741
742static void free_entry(struct writequeue_entry *e)
743{
744 struct dlm_msg *msg, *tmp;
745
746 list_for_each_entry_safe(msg, tmp, &e->msgs, list) {
747 if (msg->orig_msg) {
748 msg->orig_msg->retransmit = false;
749 kref_put(&msg->orig_msg->ref, dlm_msg_release);
750 }
751
752 list_del(&msg->list);
753 kref_put(&msg->ref, dlm_msg_release);
754 }
755
756 list_del(&e->list);
757 atomic_dec(&e->con->writequeue_cnt);
758 kref_put(&e->ref, dlm_page_release);
759}
760
Alexander Aringd11ccd42020-11-02 20:04:25 -0500761static void dlm_close_sock(struct socket **sock)
762{
763 if (*sock) {
764 restore_callbacks(*sock);
765 sock_release(*sock);
766 *sock = NULL;
767 }
768}
769
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100770/* Close a remote connection and tidy up */
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300771static void close_connection(struct connection *con, bool and_other,
772 bool tx, bool rx)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100773{
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +0000774 bool closing = test_and_set_bit(CF_CLOSING, &con->flags);
Alexander Aring706474f2021-05-21 15:08:48 -0400775 struct writequeue_entry *e;
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +0000776
tsutomu.owa@toshiba.co.jp0aa18462017-09-12 09:02:02 +0000777 if (tx && !closing && cancel_work_sync(&con->swork)) {
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300778 log_print("canceled swork for node %d", con->nodeid);
tsutomu.owa@toshiba.co.jp0aa18462017-09-12 09:02:02 +0000779 clear_bit(CF_WRITE_PENDING, &con->flags);
780 }
781 if (rx && !closing && cancel_work_sync(&con->rwork)) {
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300782 log_print("canceled rwork for node %d", con->nodeid);
tsutomu.owa@toshiba.co.jp0aa18462017-09-12 09:02:02 +0000783 clear_bit(CF_READ_PENDING, &con->flags);
784 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100785
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300786 mutex_lock(&con->sock_mutex);
Alexander Aringd11ccd42020-11-02 20:04:25 -0500787 dlm_close_sock(&con->sock);
788
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100789 if (con->othercon && and_other) {
790 /* Will only re-enter once. */
Alexander Aringc6aa00e32021-05-21 15:08:38 -0400791 close_connection(con->othercon, false, tx, rx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100792 }
Patrick Caulfield9e5f2822007-08-02 14:58:14 +0100793
Alexander Aring706474f2021-05-21 15:08:48 -0400794 /* if we send a writequeue entry only a half way, we drop the
795 * whole entry because reconnection and that we not start of the
796 * middle of a msg which will confuse the other end.
797 *
798 * we can always drop messages because retransmits, but what we
799 * cannot allow is to transmit half messages which may be processed
800 * at the other side.
801 *
802 * our policy is to start on a clean state when disconnects, we don't
803 * know what's send/received on transport layer in this case.
804 */
805 spin_lock(&con->writequeue_lock);
806 if (!list_empty(&con->writequeue)) {
807 e = list_first_entry(&con->writequeue, struct writequeue_entry,
808 list);
809 if (e->dirty)
810 free_entry(e);
811 }
812 spin_unlock(&con->writequeue_lock);
813
Alexander Aring4798cbb2020-09-24 10:31:26 -0400814 con->rx_leftover = 0;
Patrick Caulfield61d96be02007-08-20 15:13:38 +0100815 con->retries = 0;
Alexander Aring052849b2021-07-16 16:22:37 -0400816 clear_bit(CF_APP_LIMITED, &con->flags);
Alexander Aring19633c72020-11-02 20:04:20 -0500817 clear_bit(CF_CONNECTED, &con->flags);
Alexander Aringba868d92021-05-21 15:08:37 -0400818 clear_bit(CF_DELAY_CONNECT, &con->flags);
819 clear_bit(CF_RECONNECT, &con->flags);
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400820 clear_bit(CF_EOF, &con->flags);
Patrick Caulfield61d96be02007-08-20 15:13:38 +0100821 mutex_unlock(&con->sock_mutex);
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +0000822 clear_bit(CF_CLOSING, &con->flags);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100823}
824
Alexander Aring055923b2020-07-27 09:13:38 -0400825static void shutdown_connection(struct connection *con)
826{
827 int ret;
828
Alexander Aringeec054b2021-03-01 17:05:19 -0500829 flush_work(&con->swork);
Alexander Aring055923b2020-07-27 09:13:38 -0400830
831 mutex_lock(&con->sock_mutex);
832 /* nothing to shutdown */
833 if (!con->sock) {
834 mutex_unlock(&con->sock_mutex);
835 return;
836 }
837
838 set_bit(CF_SHUTDOWN, &con->flags);
839 ret = kernel_sock_shutdown(con->sock, SHUT_WR);
840 mutex_unlock(&con->sock_mutex);
841 if (ret) {
842 log_print("Connection %p failed to shutdown: %d will force close",
843 con, ret);
844 goto force_close;
845 } else {
846 ret = wait_event_timeout(con->shutdown_wait,
847 !test_bit(CF_SHUTDOWN, &con->flags),
848 DLM_SHUTDOWN_WAIT_TIMEOUT);
849 if (ret == 0) {
850 log_print("Connection %p shutdown timed out, will force close",
851 con);
852 goto force_close;
853 }
854 }
855
856 return;
857
858force_close:
859 clear_bit(CF_SHUTDOWN, &con->flags);
860 close_connection(con, false, true, true);
861}
862
863static void dlm_tcp_shutdown(struct connection *con)
864{
865 if (con->othercon)
866 shutdown_connection(con->othercon);
867 shutdown_connection(con);
868}
869
Alexander Aring4798cbb2020-09-24 10:31:26 -0400870static int con_realloc_receive_buf(struct connection *con, int newlen)
871{
872 unsigned char *newbuf;
873
874 newbuf = kmalloc(newlen, GFP_NOFS);
875 if (!newbuf)
876 return -ENOMEM;
877
878 /* copy any leftover from last receive */
879 if (con->rx_leftover)
880 memmove(newbuf, con->rx_buf, con->rx_leftover);
881
882 /* swap to new buffer space */
883 kfree(con->rx_buf);
884 con->rx_buflen = newlen;
885 con->rx_buf = newbuf;
886
887 return 0;
888}
889
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100890/* Data received from remote end */
891static int receive_from_sock(struct connection *con)
892{
Alexander Aring4798cbb2020-09-24 10:31:26 -0400893 struct msghdr msg;
894 struct kvec iov;
895 int ret, buflen;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100896
897 mutex_lock(&con->sock_mutex);
898
899 if (con->sock == NULL) {
900 ret = -EAGAIN;
901 goto out_close;
902 }
Alexander Aring4798cbb2020-09-24 10:31:26 -0400903
Alexander Aring4798cbb2020-09-24 10:31:26 -0400904 /* realloc if we get new buffer size to read out */
905 buflen = dlm_config.ci_buffer_size;
906 if (con->rx_buflen != buflen && con->rx_leftover <= buflen) {
907 ret = con_realloc_receive_buf(con, buflen);
908 if (ret < 0)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100909 goto out_resched;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100910 }
911
Alexander Aring62699b32021-07-16 16:22:45 -0400912 for (;;) {
913 /* calculate new buffer parameter regarding last receive and
914 * possible leftover bytes
915 */
916 iov.iov_base = con->rx_buf + con->rx_leftover;
917 iov.iov_len = con->rx_buflen - con->rx_leftover;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100918
Alexander Aring62699b32021-07-16 16:22:45 -0400919 memset(&msg, 0, sizeof(msg));
920 msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
921 ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len,
922 msg.msg_flags);
Alexander Aring92732372021-11-02 15:17:16 -0400923 trace_dlm_recv(con->nodeid, ret);
Alexander Aring62699b32021-07-16 16:22:45 -0400924 if (ret == -EAGAIN)
925 break;
926 else if (ret <= 0)
927 goto out_close;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100928
Alexander Aring62699b32021-07-16 16:22:45 -0400929 /* new buflen according readed bytes and leftover from last receive */
930 buflen = ret + con->rx_leftover;
931 ret = dlm_process_incoming_buffer(con->nodeid, con->rx_buf, buflen);
932 if (ret < 0)
933 goto out_close;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100934
Alexander Aring62699b32021-07-16 16:22:45 -0400935 /* calculate leftover bytes from process and put it into begin of
936 * the receive buffer, so next receive we have the full message
937 * at the start address of the receive buffer.
938 */
939 con->rx_leftover = buflen - ret;
940 if (con->rx_leftover) {
941 memmove(con->rx_buf, con->rx_buf + ret,
942 con->rx_leftover);
943 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100944 }
945
Alexander Aringb97f8522021-08-18 16:27:13 -0400946 dlm_midcomms_receive_done(con->nodeid);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100947 mutex_unlock(&con->sock_mutex);
948 return 0;
949
950out_resched:
951 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
952 queue_work(recv_workqueue, &con->rwork);
953 mutex_unlock(&con->sock_mutex);
954 return -EAGAIN;
955
956out_close:
Alexander Aringba868d92021-05-21 15:08:37 -0400957 if (ret == 0) {
Alexander Aringba868d92021-05-21 15:08:37 -0400958 log_print("connection %p got EOF from %d",
959 con, con->nodeid);
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400960
Alexander Aringa66c0082021-07-16 16:22:40 -0400961 if (dlm_proto_ops->eof_condition &&
962 dlm_proto_ops->eof_condition(con)) {
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400963 set_bit(CF_EOF, &con->flags);
964 mutex_unlock(&con->sock_mutex);
965 } else {
966 mutex_unlock(&con->sock_mutex);
967 close_connection(con, false, true, false);
968
969 /* handling for tcp shutdown */
970 clear_bit(CF_SHUTDOWN, &con->flags);
971 wake_up(&con->shutdown_wait);
972 }
973
Alexander Aringba868d92021-05-21 15:08:37 -0400974 /* signal to breaking receive worker */
975 ret = -1;
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400976 } else {
977 mutex_unlock(&con->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100978 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100979 return ret;
980}
981
982/* Listening socket is busy, accept a connection */
Alexander Aringd11ccd42020-11-02 20:04:25 -0500983static int accept_from_sock(struct listen_connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100984{
985 int result;
986 struct sockaddr_storage peeraddr;
987 struct socket *newsock;
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400988 int len, idx;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100989 int nodeid;
990 struct connection *newcon;
991 struct connection *addcon;
Alexander Aring3f78cd72020-09-24 10:31:23 -0400992 unsigned int mark;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100993
Alexander Aringd11ccd42020-11-02 20:04:25 -0500994 if (!con->sock)
tsutomu.owa@toshiba.co.jp3421fb12017-09-12 09:01:38 +0000995 return -ENOTCONN;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100996
tsutomu.owa@toshiba.co.jp3421fb12017-09-12 09:01:38 +0000997 result = kernel_accept(con->sock, &newsock, O_NONBLOCK);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100998 if (result < 0)
999 goto accept_err;
1000
1001 /* Get the connected socket's peer */
1002 memset(&peeraddr, 0, sizeof(peeraddr));
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001003 len = newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr, 2);
1004 if (len < 0) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001005 result = -ECONNABORTED;
1006 goto accept_err;
1007 }
1008
1009 /* Get the new node's NODEID */
1010 make_sockaddr(&peeraddr, 0, &len);
Alexander Aringe125fbe2021-03-01 17:05:09 -05001011 if (addr_to_nodeid(&peeraddr, &nodeid, &mark)) {
Masatake YAMATObcaadf52011-07-04 12:25:51 +09001012 unsigned char *b=(unsigned char *)&peeraddr;
David Teigland617e82e2007-04-26 13:46:49 -05001013 log_print("connect from non cluster node");
Masatake YAMATObcaadf52011-07-04 12:25:51 +09001014 print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
1015 b, sizeof(struct sockaddr_storage));
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001016 sock_release(newsock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001017 return -1;
1018 }
1019
1020 log_print("got connection from %d", nodeid);
1021
1022 /* Check to see if we already have a connection to this node. This
1023 * could happen if the two nodes initiate a connection at roughly
1024 * the same time and the connections cross on the wire.
1025 * In this case we store the incoming one in "othercon"
1026 */
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001027 idx = srcu_read_lock(&connections_srcu);
David Teigland748285c2009-05-15 10:50:57 -05001028 newcon = nodeid2con(nodeid, GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001029 if (!newcon) {
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001030 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001031 result = -ENOMEM;
1032 goto accept_err;
1033 }
Alexander Aringd11ccd42020-11-02 20:04:25 -05001034
Alexander Aringe125fbe2021-03-01 17:05:09 -05001035 sock_set_mark(newsock->sk, mark);
1036
Alexander Aringd11ccd42020-11-02 20:04:25 -05001037 mutex_lock(&newcon->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001038 if (newcon->sock) {
1039 struct connection *othercon = newcon->othercon;
1040
1041 if (!othercon) {
Alexander Aringa47666eb2020-08-27 15:02:49 -04001042 othercon = kzalloc(sizeof(*othercon), GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001043 if (!othercon) {
David Teigland617e82e2007-04-26 13:46:49 -05001044 log_print("failed to allocate incoming socket");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001045 mutex_unlock(&newcon->sock_mutex);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001046 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001047 result = -ENOMEM;
1048 goto accept_err;
1049 }
Alexander Aring4798cbb2020-09-24 10:31:26 -04001050
Alexander Aring6cde2102020-11-02 20:04:21 -05001051 result = dlm_con_init(othercon, nodeid);
1052 if (result < 0) {
Alexander Aring4798cbb2020-09-24 10:31:26 -04001053 kfree(othercon);
Yang Yingliang2fd8db22021-03-27 16:37:04 +08001054 mutex_unlock(&newcon->sock_mutex);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001055 srcu_read_unlock(&connections_srcu, idx);
Alexander Aring4798cbb2020-09-24 10:31:26 -04001056 goto accept_err;
1057 }
1058
Alexander Aringe9a470a2021-03-01 17:05:11 -05001059 lockdep_set_subclass(&othercon->sock_mutex, 1);
Alexander Aring7443bc92021-05-21 15:08:36 -04001060 set_bit(CF_IS_OTHERCON, &othercon->flags);
Alexander Aring6cde2102020-11-02 20:04:21 -05001061 newcon->othercon = othercon;
Alexander Aringba868d92021-05-21 15:08:37 -04001062 othercon->sendcon = newcon;
Alexander Aringba3ab3c2020-07-27 09:13:37 -04001063 } else {
1064 /* close other sock con if we have something new */
1065 close_connection(othercon, false, true, false);
Patrick Caulfield61d96be02007-08-20 15:13:38 +01001066 }
Alexander Aringba3ab3c2020-07-27 09:13:37 -04001067
Alexander Aringe9a470a2021-03-01 17:05:11 -05001068 mutex_lock(&othercon->sock_mutex);
Alexander Aringba3ab3c2020-07-27 09:13:37 -04001069 add_sock(newsock, othercon);
1070 addcon = othercon;
1071 mutex_unlock(&othercon->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001072 }
1073 else {
Bob Peterson3735b4b2016-09-23 14:23:26 -04001074 /* accept copies the sk after we've saved the callbacks, so we
1075 don't want to save them a second time or comm errors will
1076 result in calling sk_error_report recursively. */
tsutomu.owa@toshiba.co.jp988419a2017-09-12 08:55:32 +00001077 add_sock(newsock, newcon);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001078 addcon = newcon;
1079 }
1080
Alexander Aringb30a6242021-03-01 17:05:10 -05001081 set_bit(CF_CONNECTED, &addcon->flags);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001082 mutex_unlock(&newcon->sock_mutex);
1083
1084 /*
1085 * Add it to the active queue in case we got data
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001086 * between processing the accept adding the socket
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001087 * to the read_sockets list
1088 */
1089 if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
1090 queue_work(recv_workqueue, &addcon->rwork);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001091
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001092 srcu_read_unlock(&connections_srcu, idx);
1093
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001094 return 0;
1095
1096accept_err:
tsutomu.owa@toshiba.co.jp3421fb12017-09-12 09:01:38 +00001097 if (newsock)
1098 sock_release(newsock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001099
1100 if (result != -EAGAIN)
David Teigland617e82e2007-04-26 13:46:49 -05001101 log_print("error accepting connection from node: %d", result);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001102 return result;
1103}
1104
Mike Christie5d689872013-06-14 04:56:13 -05001105/*
1106 * writequeue_entry_complete - try to delete and free write queue entry
1107 * @e: write queue entry to try to delete
1108 * @completed: bytes completed
1109 *
1110 * writequeue_lock must be held.
1111 */
1112static void writequeue_entry_complete(struct writequeue_entry *e, int completed)
1113{
1114 e->offset += completed;
1115 e->len -= completed;
Alexander Aring706474f2021-05-21 15:08:48 -04001116 /* signal that page was half way transmitted */
1117 e->dirty = true;
Mike Christie5d689872013-06-14 04:56:13 -05001118
Alexander Aring8f2dc782021-05-21 15:08:42 -04001119 if (e->len == 0 && e->users == 0)
Mike Christie5d689872013-06-14 04:56:13 -05001120 free_entry(e);
Mike Christie5d689872013-06-14 04:56:13 -05001121}
1122
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001123/*
1124 * sctp_bind_addrs - bind a SCTP socket to all our addresses
1125 */
Alexander Aring13004e82020-11-02 20:04:24 -05001126static int sctp_bind_addrs(struct socket *sock, uint16_t port)
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001127{
1128 struct sockaddr_storage localaddr;
Christoph Hellwigc0425a42020-05-29 14:09:42 +02001129 struct sockaddr *addr = (struct sockaddr *)&localaddr;
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001130 int i, addr_len, result = 0;
1131
1132 for (i = 0; i < dlm_local_count; i++) {
1133 memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
1134 make_sockaddr(&localaddr, port, &addr_len);
1135
1136 if (!i)
Alexander Aring13004e82020-11-02 20:04:24 -05001137 result = kernel_bind(sock, addr, addr_len);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001138 else
Alexander Aring13004e82020-11-02 20:04:24 -05001139 result = sock_bind_add(sock->sk, addr, addr_len);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001140
1141 if (result < 0) {
1142 log_print("Can't bind to %d addr number %d, %d.\n",
1143 port, i + 1, result);
1144 break;
1145 }
1146 }
1147 return result;
1148}
1149
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001150/* Get local addresses */
1151static void init_local(void)
1152{
1153 struct sockaddr_storage sas, *addr;
1154 int i;
1155
Patrick Caulfield30d3a232007-04-23 16:26:21 +01001156 dlm_local_count = 0;
David Teigland1b189b82012-03-21 09:18:34 -05001157 for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001158 if (dlm_our_addr(&sas, i))
1159 break;
1160
Amitoj Kaur Chawla5c93f562016-06-23 10:22:01 +05301161 addr = kmemdup(&sas, sizeof(*addr), GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001162 if (!addr)
1163 break;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001164 dlm_local_addr[dlm_local_count++] = addr;
1165 }
1166}
1167
Alexander Aring043697f2020-08-27 15:02:50 -04001168static void deinit_local(void)
1169{
1170 int i;
1171
1172 for (i = 0; i < dlm_local_count; i++)
1173 kfree(dlm_local_addr[i]);
1174}
1175
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001176static struct writequeue_entry *new_writequeue_entry(struct connection *con,
1177 gfp_t allocation)
1178{
1179 struct writequeue_entry *entry;
1180
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001181 entry = kzalloc(sizeof(*entry), allocation);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001182 if (!entry)
1183 return NULL;
1184
Alexander Aringe1a7cbc2021-03-01 17:05:15 -05001185 entry->page = alloc_page(allocation | __GFP_ZERO);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001186 if (!entry->page) {
1187 kfree(entry);
1188 return NULL;
1189 }
1190
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001191 entry->con = con;
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001192 entry->users = 1;
Alexander Aring8f2dc782021-05-21 15:08:42 -04001193 kref_init(&entry->ref);
1194 INIT_LIST_HEAD(&entry->msgs);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001195
1196 return entry;
1197}
1198
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001199static struct writequeue_entry *new_wq_entry(struct connection *con, int len,
Alexander Aring8f2dc782021-05-21 15:08:42 -04001200 gfp_t allocation, char **ppc,
Alexander Aring5c16feb2021-11-02 15:17:19 -04001201 void (*cb)(void *data), void *data)
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001202{
1203 struct writequeue_entry *e;
1204
1205 spin_lock(&con->writequeue_lock);
1206 if (!list_empty(&con->writequeue)) {
1207 e = list_last_entry(&con->writequeue, struct writequeue_entry, list);
1208 if (DLM_WQ_REMAIN_BYTES(e) >= len) {
Alexander Aring8f2dc782021-05-21 15:08:42 -04001209 kref_get(&e->ref);
1210
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001211 *ppc = page_address(e->page) + e->end;
Alexander Aring8f2dc782021-05-21 15:08:42 -04001212 if (cb)
Alexander Aring5c16feb2021-11-02 15:17:19 -04001213 cb(data);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001214
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001215 e->end += len;
1216 e->users++;
1217 spin_unlock(&con->writequeue_lock);
1218
1219 return e;
1220 }
1221 }
1222 spin_unlock(&con->writequeue_lock);
1223
1224 e = new_writequeue_entry(con, allocation);
1225 if (!e)
1226 return NULL;
1227
Alexander Aring8f2dc782021-05-21 15:08:42 -04001228 kref_get(&e->ref);
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001229 *ppc = page_address(e->page);
1230 e->end += len;
Alexander Aring8aa31cb2021-05-21 15:08:39 -04001231 atomic_inc(&con->writequeue_cnt);
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001232
1233 spin_lock(&con->writequeue_lock);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001234 if (cb)
Alexander Aring5c16feb2021-11-02 15:17:19 -04001235 cb(data);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001236
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001237 list_add_tail(&e->list, &con->writequeue);
1238 spin_unlock(&con->writequeue_lock);
1239
1240 return e;
1241};
1242
Alexander Aring2874d1a2021-05-21 15:08:43 -04001243static struct dlm_msg *dlm_lowcomms_new_msg_con(struct connection *con, int len,
1244 gfp_t allocation, char **ppc,
Alexander Aring5c16feb2021-11-02 15:17:19 -04001245 void (*cb)(void *data),
1246 void *data)
Alexander Aring2874d1a2021-05-21 15:08:43 -04001247{
1248 struct writequeue_entry *e;
1249 struct dlm_msg *msg;
Alexander Aringc51b0222021-07-16 16:22:44 -04001250 bool sleepable;
Alexander Aring2874d1a2021-05-21 15:08:43 -04001251
1252 msg = kzalloc(sizeof(*msg), allocation);
1253 if (!msg)
1254 return NULL;
1255
Alexander Aringc51b0222021-07-16 16:22:44 -04001256 /* this mutex is being used as a wait to avoid multiple "fast"
1257 * new writequeue page list entry allocs in new_wq_entry in
1258 * normal operation which is sleepable context. Without it
1259 * we could end in multiple writequeue entries with one
1260 * dlm message because multiple callers were waiting at
1261 * the writequeue_lock in new_wq_entry().
1262 */
1263 sleepable = gfpflags_normal_context(allocation);
1264 if (sleepable)
1265 mutex_lock(&con->wq_alloc);
1266
Alexander Aring2874d1a2021-05-21 15:08:43 -04001267 kref_init(&msg->ref);
1268
Alexander Aring5c16feb2021-11-02 15:17:19 -04001269 e = new_wq_entry(con, len, allocation, ppc, cb, data);
Alexander Aring2874d1a2021-05-21 15:08:43 -04001270 if (!e) {
Alexander Aringc51b0222021-07-16 16:22:44 -04001271 if (sleepable)
1272 mutex_unlock(&con->wq_alloc);
1273
Alexander Aring2874d1a2021-05-21 15:08:43 -04001274 kfree(msg);
1275 return NULL;
1276 }
1277
Alexander Aringc51b0222021-07-16 16:22:44 -04001278 if (sleepable)
1279 mutex_unlock(&con->wq_alloc);
1280
Alexander Aring2874d1a2021-05-21 15:08:43 -04001281 msg->ppc = *ppc;
1282 msg->len = len;
1283 msg->entry = e;
1284
1285 return msg;
1286}
1287
Alexander Aring8f2dc782021-05-21 15:08:42 -04001288struct dlm_msg *dlm_lowcomms_new_msg(int nodeid, int len, gfp_t allocation,
Alexander Aring5c16feb2021-11-02 15:17:19 -04001289 char **ppc, void (*cb)(void *data),
1290 void *data)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001291{
1292 struct connection *con;
Alexander Aring8f2dc782021-05-21 15:08:42 -04001293 struct dlm_msg *msg;
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001294 int idx;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001295
Alexander Aringd10a0b82021-06-02 09:45:20 -04001296 if (len > DLM_MAX_SOCKET_BUFSIZE ||
Alexander Aringc45674f2021-03-01 17:05:14 -05001297 len < sizeof(struct dlm_header)) {
Alexander Aringd10a0b82021-06-02 09:45:20 -04001298 BUILD_BUG_ON(PAGE_SIZE < DLM_MAX_SOCKET_BUFSIZE);
Alexander Aring692f51c2020-11-02 20:04:18 -05001299 log_print("failed to allocate a buffer of size %d", len);
Alexander Aringc45674f2021-03-01 17:05:14 -05001300 WARN_ON(1);
Alexander Aring692f51c2020-11-02 20:04:18 -05001301 return NULL;
1302 }
1303
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001304 idx = srcu_read_lock(&connections_srcu);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001305 con = nodeid2con(nodeid, allocation);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001306 if (!con) {
1307 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001308 return NULL;
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001309 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001310
Alexander Aring5c16feb2021-11-02 15:17:19 -04001311 msg = dlm_lowcomms_new_msg_con(con, len, allocation, ppc, cb, data);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001312 if (!msg) {
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001313 srcu_read_unlock(&connections_srcu, idx);
1314 return NULL;
1315 }
1316
Alexander Aring8f2dc782021-05-21 15:08:42 -04001317 /* we assume if successful commit must called */
1318 msg->idx = idx;
Alexander Aring8f2dc782021-05-21 15:08:42 -04001319 return msg;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001320}
1321
Alexander Aring2874d1a2021-05-21 15:08:43 -04001322static void _dlm_lowcomms_commit_msg(struct dlm_msg *msg)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001323{
Alexander Aring8f2dc782021-05-21 15:08:42 -04001324 struct writequeue_entry *e = msg->entry;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001325 struct connection *con = e->con;
1326 int users;
1327
1328 spin_lock(&con->writequeue_lock);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001329 kref_get(&msg->ref);
1330 list_add(&msg->list, &e->msgs);
1331
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001332 users = --e->users;
1333 if (users)
1334 goto out;
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001335
1336 e->len = DLM_WQ_LENGTH_BYTES(e);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001337 spin_unlock(&con->writequeue_lock);
1338
Bob Peterson01da24d2017-09-12 08:55:14 +00001339 queue_work(send_workqueue, &con->swork);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001340 return;
1341
1342out:
1343 spin_unlock(&con->writequeue_lock);
1344 return;
1345}
1346
Alexander Aring2874d1a2021-05-21 15:08:43 -04001347void dlm_lowcomms_commit_msg(struct dlm_msg *msg)
1348{
1349 _dlm_lowcomms_commit_msg(msg);
1350 srcu_read_unlock(&connections_srcu, msg->idx);
1351}
1352
Alexander Aring8f2dc782021-05-21 15:08:42 -04001353void dlm_lowcomms_put_msg(struct dlm_msg *msg)
1354{
1355 kref_put(&msg->ref, dlm_msg_release);
1356}
1357
Alexander Aring2874d1a2021-05-21 15:08:43 -04001358/* does not held connections_srcu, usage workqueue only */
1359int dlm_lowcomms_resend_msg(struct dlm_msg *msg)
1360{
1361 struct dlm_msg *msg_resend;
1362 char *ppc;
1363
1364 if (msg->retransmit)
1365 return 1;
1366
1367 msg_resend = dlm_lowcomms_new_msg_con(msg->entry->con, msg->len,
1368 GFP_ATOMIC, &ppc, NULL, NULL);
1369 if (!msg_resend)
1370 return -ENOMEM;
1371
1372 msg->retransmit = true;
1373 kref_get(&msg->ref);
1374 msg_resend->orig_msg = msg;
1375
1376 memcpy(ppc, msg->ppc, msg->len);
1377 _dlm_lowcomms_commit_msg(msg_resend);
1378 dlm_lowcomms_put_msg(msg_resend);
1379
1380 return 0;
1381}
1382
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001383/* Send a message */
1384static void send_to_sock(struct connection *con)
1385{
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001386 const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1387 struct writequeue_entry *e;
Alexander Aring66d59552021-07-16 16:22:39 -04001388 int len, offset, ret;
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001389 int count = 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001390
1391 mutex_lock(&con->sock_mutex);
1392 if (con->sock == NULL)
1393 goto out_connect;
1394
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001395 spin_lock(&con->writequeue_lock);
1396 for (;;) {
Alexander Aring66d59552021-07-16 16:22:39 -04001397 e = con_next_wq(con);
1398 if (!e)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001399 break;
1400
1401 len = e->len;
1402 offset = e->offset;
1403 BUG_ON(len == 0 && e->users == 0);
1404 spin_unlock(&con->writequeue_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001405
Alexander Aring66d59552021-07-16 16:22:39 -04001406 ret = kernel_sendpage(con->sock, e->page, offset, len,
1407 msg_flags);
Alexander Aring92732372021-11-02 15:17:16 -04001408 trace_dlm_send(con->nodeid, ret);
Alexander Aring66d59552021-07-16 16:22:39 -04001409 if (ret == -EAGAIN || ret == 0) {
1410 if (ret == -EAGAIN &&
1411 test_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags) &&
1412 !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
1413 /* Notify TCP that we're limited by the
1414 * application window size.
1415 */
1416 set_bit(SOCK_NOSPACE, &con->sock->flags);
1417 con->sock->sk->sk_write_pending++;
1418 }
1419 cond_resched();
1420 goto out;
1421 } else if (ret < 0)
1422 goto out;
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001423
1424 /* Don't starve people filling buffers */
1425 if (++count >= MAX_SEND_MSG_COUNT) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001426 cond_resched();
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001427 count = 0;
1428 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001429
1430 spin_lock(&con->writequeue_lock);
Mike Christie5d689872013-06-14 04:56:13 -05001431 writequeue_entry_complete(e, ret);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001432 }
1433 spin_unlock(&con->writequeue_lock);
Alexander Aring8aa31cb2021-05-21 15:08:39 -04001434
1435 /* close if we got EOF */
1436 if (test_and_clear_bit(CF_EOF, &con->flags)) {
1437 mutex_unlock(&con->sock_mutex);
1438 close_connection(con, false, false, true);
1439
1440 /* handling for tcp shutdown */
1441 clear_bit(CF_SHUTDOWN, &con->flags);
1442 wake_up(&con->shutdown_wait);
1443 } else {
1444 mutex_unlock(&con->sock_mutex);
1445 }
1446
1447 return;
1448
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001449out:
1450 mutex_unlock(&con->sock_mutex);
1451 return;
1452
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001453out_connect:
1454 mutex_unlock(&con->sock_mutex);
Bob Peterson01da24d2017-09-12 08:55:14 +00001455 queue_work(send_workqueue, &con->swork);
1456 cond_resched();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001457}
1458
1459static void clean_one_writequeue(struct connection *con)
1460{
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001461 struct writequeue_entry *e, *safe;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001462
1463 spin_lock(&con->writequeue_lock);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001464 list_for_each_entry_safe(e, safe, &con->writequeue, list) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001465 free_entry(e);
1466 }
1467 spin_unlock(&con->writequeue_lock);
1468}
1469
1470/* Called from recovery when it knows that a node has
1471 left the cluster */
1472int dlm_lowcomms_close(int nodeid)
1473{
1474 struct connection *con;
David Teigland36b71a82012-07-26 12:44:30 -05001475 struct dlm_node_addr *na;
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001476 int idx;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001477
1478 log_print("closing connection to node %d", nodeid);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001479 idx = srcu_read_lock(&connections_srcu);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001480 con = nodeid2con(nodeid, 0);
1481 if (con) {
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -05001482 set_bit(CF_CLOSE, &con->flags);
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -03001483 close_connection(con, true, true, true);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001484 clean_one_writequeue(con);
Alexander Aring53a5eda2020-11-02 20:04:19 -05001485 if (con->othercon)
1486 clean_one_writequeue(con->othercon);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001487 }
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001488 srcu_read_unlock(&connections_srcu, idx);
David Teigland36b71a82012-07-26 12:44:30 -05001489
1490 spin_lock(&dlm_node_addrs_spin);
1491 na = find_node_addr(nodeid);
1492 if (na) {
1493 list_del(&na->list);
1494 while (na->addr_count--)
1495 kfree(na->addr[na->addr_count]);
1496 kfree(na);
1497 }
1498 spin_unlock(&dlm_node_addrs_spin);
1499
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001500 return 0;
1501}
1502
1503/* Receive workqueue function */
1504static void process_recv_sockets(struct work_struct *work)
1505{
1506 struct connection *con = container_of(work, struct connection, rwork);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001507
1508 clear_bit(CF_READ_PENDING, &con->flags);
Alexander Aring62699b32021-07-16 16:22:45 -04001509 receive_from_sock(con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001510}
1511
Alexander Aringd11ccd42020-11-02 20:04:25 -05001512static void process_listen_recv_socket(struct work_struct *work)
1513{
1514 accept_from_sock(&listen_con);
1515}
1516
Alexander Aring8728a452021-07-16 16:22:43 -04001517static void dlm_connect(struct connection *con)
1518{
1519 struct sockaddr_storage addr;
1520 int result, addr_len;
1521 struct socket *sock;
1522 unsigned int mark;
1523
1524 /* Some odd races can cause double-connects, ignore them */
1525 if (con->retries++ > MAX_CONNECT_RETRIES)
1526 return;
1527
1528 if (con->sock) {
1529 log_print("node %d already connected.", con->nodeid);
1530 return;
1531 }
1532
1533 memset(&addr, 0, sizeof(addr));
1534 result = nodeid_to_addr(con->nodeid, &addr, NULL,
1535 dlm_proto_ops->try_new_addr, &mark);
1536 if (result < 0) {
1537 log_print("no address for nodeid %d", con->nodeid);
1538 return;
1539 }
1540
1541 /* Create a socket to communicate with */
1542 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1543 SOCK_STREAM, dlm_proto_ops->proto, &sock);
1544 if (result < 0)
1545 goto socket_err;
1546
1547 sock_set_mark(sock->sk, mark);
1548 dlm_proto_ops->sockopts(sock);
1549
1550 add_sock(sock, con);
1551
1552 result = dlm_proto_ops->bind(sock);
1553 if (result < 0)
1554 goto add_sock_err;
1555
1556 log_print_ratelimited("connecting to %d", con->nodeid);
1557 make_sockaddr(&addr, dlm_config.ci_tcp_port, &addr_len);
1558 result = dlm_proto_ops->connect(con, sock, (struct sockaddr *)&addr,
1559 addr_len);
1560 if (result < 0)
1561 goto add_sock_err;
1562
1563 return;
1564
1565add_sock_err:
1566 dlm_close_sock(&con->sock);
1567
1568socket_err:
1569 /*
1570 * Some errors are fatal and this list might need adjusting. For other
1571 * errors we try again until the max number of retries is reached.
1572 */
1573 if (result != -EHOSTUNREACH &&
1574 result != -ENETUNREACH &&
1575 result != -ENETDOWN &&
1576 result != -EINVAL &&
1577 result != -EPROTONOSUPPORT) {
1578 log_print("connect %d try %d error %d", con->nodeid,
1579 con->retries, result);
1580 msleep(1000);
1581 lowcomms_connect_sock(con);
1582 }
1583}
1584
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001585/* Send workqueue function */
1586static void process_send_sockets(struct work_struct *work)
1587{
1588 struct connection *con = container_of(work, struct connection, swork);
1589
Alexander Aring7443bc92021-05-21 15:08:36 -04001590 WARN_ON(test_bit(CF_IS_OTHERCON, &con->flags));
1591
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001592 clear_bit(CF_WRITE_PENDING, &con->flags);
Alexander Aringba868d92021-05-21 15:08:37 -04001593
Alexander Aring489d8e52021-05-21 15:08:46 -04001594 if (test_and_clear_bit(CF_RECONNECT, &con->flags)) {
Alexander Aringba868d92021-05-21 15:08:37 -04001595 close_connection(con, false, false, true);
Alexander Aring489d8e52021-05-21 15:08:46 -04001596 dlm_midcomms_unack_msg_resend(con->nodeid);
1597 }
Alexander Aringba868d92021-05-21 15:08:37 -04001598
Alexander Aring8728a452021-07-16 16:22:43 -04001599 if (con->sock == NULL) {
Alexander Aringba868d92021-05-21 15:08:37 -04001600 if (test_and_clear_bit(CF_DELAY_CONNECT, &con->flags))
1601 msleep(1000);
Alexander Aring8728a452021-07-16 16:22:43 -04001602
1603 mutex_lock(&con->sock_mutex);
1604 dlm_connect(con);
1605 mutex_unlock(&con->sock_mutex);
Alexander Aringba868d92021-05-21 15:08:37 -04001606 }
Alexander Aring8728a452021-07-16 16:22:43 -04001607
Bob Peterson01da24d2017-09-12 08:55:14 +00001608 if (!list_empty(&con->writequeue))
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -05001609 send_to_sock(con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001610}
1611
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001612static void work_stop(void)
1613{
Alexander Aringfcef0e62021-06-02 09:45:15 -04001614 if (recv_workqueue) {
David Windsorb3555162019-04-02 08:37:10 -04001615 destroy_workqueue(recv_workqueue);
Alexander Aringfcef0e62021-06-02 09:45:15 -04001616 recv_workqueue = NULL;
1617 }
1618
1619 if (send_workqueue) {
David Windsorb3555162019-04-02 08:37:10 -04001620 destroy_workqueue(send_workqueue);
Alexander Aringfcef0e62021-06-02 09:45:15 -04001621 send_workqueue = NULL;
1622 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001623}
1624
1625static int work_start(void)
1626{
Alexander Aring6c6a1cc2021-06-02 09:45:17 -04001627 recv_workqueue = alloc_ordered_workqueue("dlm_recv", WQ_MEM_RECLAIM);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001628 if (!recv_workqueue) {
1629 log_print("can't start dlm_recv");
1630 return -ENOMEM;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001631 }
1632
Alexander Aring6c6a1cc2021-06-02 09:45:17 -04001633 send_workqueue = alloc_ordered_workqueue("dlm_send", WQ_MEM_RECLAIM);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001634 if (!send_workqueue) {
1635 log_print("can't start dlm_send");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001636 destroy_workqueue(recv_workqueue);
Alexander Aringfcef0e62021-06-02 09:45:15 -04001637 recv_workqueue = NULL;
Namhyung Kimb9d41052010-12-13 13:42:24 -06001638 return -ENOMEM;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001639 }
1640
1641 return 0;
1642}
1643
Alexander Aring9d232462021-03-01 17:05:20 -05001644static void shutdown_conn(struct connection *con)
1645{
Alexander Aringa66c0082021-07-16 16:22:40 -04001646 if (dlm_proto_ops->shutdown_action)
1647 dlm_proto_ops->shutdown_action(con);
Alexander Aring9d232462021-03-01 17:05:20 -05001648}
1649
1650void dlm_lowcomms_shutdown(void)
1651{
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001652 int idx;
1653
Alexander Aring9d232462021-03-01 17:05:20 -05001654 /* Set all the flags to prevent any
1655 * socket activity.
1656 */
1657 dlm_allow_conn = 0;
1658
1659 if (recv_workqueue)
1660 flush_workqueue(recv_workqueue);
1661 if (send_workqueue)
1662 flush_workqueue(send_workqueue);
1663
1664 dlm_close_sock(&listen_con.sock);
1665
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001666 idx = srcu_read_lock(&connections_srcu);
Alexander Aring9d232462021-03-01 17:05:20 -05001667 foreach_conn(shutdown_conn);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001668 srcu_read_unlock(&connections_srcu, idx);
Alexander Aring9d232462021-03-01 17:05:20 -05001669}
1670
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001671static void _stop_conn(struct connection *con, bool and_other)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001672{
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001673 mutex_lock(&con->sock_mutex);
tsutomu.owa@toshiba.co.jp173a31f2017-09-12 09:01:24 +00001674 set_bit(CF_CLOSE, &con->flags);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001675 set_bit(CF_READ_PENDING, &con->flags);
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001676 set_bit(CF_WRITE_PENDING, &con->flags);
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +00001677 if (con->sock && con->sock->sk) {
Alexander Aring92c44602021-11-15 08:57:06 -05001678 lock_sock(con->sock->sk);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001679 con->sock->sk->sk_user_data = NULL;
Alexander Aring92c44602021-11-15 08:57:06 -05001680 release_sock(con->sock->sk);
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +00001681 }
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001682 if (con->othercon && and_other)
1683 _stop_conn(con->othercon, false);
1684 mutex_unlock(&con->sock_mutex);
1685}
1686
1687static void stop_conn(struct connection *con)
1688{
1689 _stop_conn(con, true);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001690}
1691
Alexander Aring4798cbb2020-09-24 10:31:26 -04001692static void connection_release(struct rcu_head *rcu)
1693{
1694 struct connection *con = container_of(rcu, struct connection, rcu);
1695
1696 kfree(con->rx_buf);
1697 kfree(con);
1698}
1699
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001700static void free_conn(struct connection *con)
1701{
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -03001702 close_connection(con, true, true, true);
Alexander Aringa47666eb2020-08-27 15:02:49 -04001703 spin_lock(&connections_lock);
1704 hlist_del_rcu(&con->list);
1705 spin_unlock(&connections_lock);
Alexander Aring948c47e2020-08-27 15:02:53 -04001706 if (con->othercon) {
1707 clean_one_writequeue(con->othercon);
Alexander Aring5cbec202020-11-02 20:04:16 -05001708 call_srcu(&connections_srcu, &con->othercon->rcu,
1709 connection_release);
Alexander Aring948c47e2020-08-27 15:02:53 -04001710 }
Alexander Aring0de98432020-08-27 15:02:52 -04001711 clean_one_writequeue(con);
Alexander Aring5cbec202020-11-02 20:04:16 -05001712 call_srcu(&connections_srcu, &con->rcu, connection_release);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001713}
1714
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001715static void work_flush(void)
1716{
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001717 int ok;
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001718 int i;
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001719 struct connection *con;
1720
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001721 do {
1722 ok = 1;
1723 foreach_conn(stop_conn);
David Windsorb3555162019-04-02 08:37:10 -04001724 if (recv_workqueue)
1725 flush_workqueue(recv_workqueue);
1726 if (send_workqueue)
1727 flush_workqueue(send_workqueue);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001728 for (i = 0; i < CONN_HASH_SIZE && ok; i++) {
Alexander Aringa47666eb2020-08-27 15:02:49 -04001729 hlist_for_each_entry_rcu(con, &connection_hash[i],
1730 list) {
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001731 ok &= test_bit(CF_READ_PENDING, &con->flags);
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001732 ok &= test_bit(CF_WRITE_PENDING, &con->flags);
1733 if (con->othercon) {
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001734 ok &= test_bit(CF_READ_PENDING,
1735 &con->othercon->flags);
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001736 ok &= test_bit(CF_WRITE_PENDING,
1737 &con->othercon->flags);
1738 }
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001739 }
1740 }
1741 } while (!ok);
1742}
1743
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001744void dlm_lowcomms_stop(void)
1745{
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001746 int idx;
1747
1748 idx = srcu_read_lock(&connections_srcu);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001749 work_flush();
Marcelo Ricardo Leitner3a8db792016-10-08 10:14:37 -03001750 foreach_conn(free_conn);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001751 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001752 work_stop();
Alexander Aring043697f2020-08-27 15:02:50 -04001753 deinit_local();
Alexander Aringa66c0082021-07-16 16:22:40 -04001754
1755 dlm_proto_ops = NULL;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001756}
1757
Alexander Aring2dc6b112021-07-16 16:22:41 -04001758static int dlm_listen_for_all(void)
1759{
1760 struct socket *sock;
1761 int result;
1762
1763 log_print("Using %s for communications",
1764 dlm_proto_ops->name);
1765
Alexander Aring90d21fc2021-07-16 16:22:42 -04001766 result = dlm_proto_ops->listen_validate();
1767 if (result < 0)
1768 return result;
Alexander Aring2dc6b112021-07-16 16:22:41 -04001769
1770 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1771 SOCK_STREAM, dlm_proto_ops->proto, &sock);
1772 if (result < 0) {
Alexander Aringfe933672021-11-02 15:17:10 -04001773 log_print("Can't create comms socket: %d", result);
Alexander Aring2dc6b112021-07-16 16:22:41 -04001774 goto out;
1775 }
1776
1777 sock_set_mark(sock->sk, dlm_config.ci_mark);
1778 dlm_proto_ops->listen_sockopts(sock);
1779
1780 result = dlm_proto_ops->listen_bind(sock);
1781 if (result < 0)
1782 goto out;
1783
1784 save_listen_callbacks(sock);
1785 add_listen_sock(sock, &listen_con);
1786
1787 INIT_WORK(&listen_con.rwork, process_listen_recv_socket);
1788 result = sock->ops->listen(sock, 5);
1789 if (result < 0) {
1790 dlm_close_sock(&listen_con.sock);
1791 goto out;
1792 }
1793
1794 return 0;
1795
1796out:
1797 sock_release(sock);
1798 return result;
1799}
1800
Alexander Aring8728a452021-07-16 16:22:43 -04001801static int dlm_tcp_bind(struct socket *sock)
1802{
1803 struct sockaddr_storage src_addr;
1804 int result, addr_len;
1805
1806 /* Bind to our cluster-known address connecting to avoid
1807 * routing problems.
1808 */
1809 memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
1810 make_sockaddr(&src_addr, 0, &addr_len);
1811
1812 result = sock->ops->bind(sock, (struct sockaddr *)&src_addr,
1813 addr_len);
1814 if (result < 0) {
1815 /* This *may* not indicate a critical error */
1816 log_print("could not bind for connect: %d", result);
1817 }
1818
1819 return 0;
1820}
1821
1822static int dlm_tcp_connect(struct connection *con, struct socket *sock,
1823 struct sockaddr *addr, int addr_len)
1824{
1825 int ret;
1826
1827 ret = sock->ops->connect(sock, addr, addr_len, O_NONBLOCK);
1828 switch (ret) {
1829 case -EINPROGRESS:
1830 fallthrough;
1831 case 0:
1832 return 0;
1833 }
1834
1835 return ret;
1836}
1837
Alexander Aring2dc6b112021-07-16 16:22:41 -04001838static int dlm_tcp_listen_validate(void)
1839{
1840 /* We don't support multi-homed hosts */
1841 if (dlm_local_count > 1) {
1842 log_print("TCP protocol can't handle multi-homed hosts, try SCTP");
1843 return -EINVAL;
1844 }
1845
1846 return 0;
1847}
1848
1849static void dlm_tcp_sockopts(struct socket *sock)
1850{
1851 /* Turn off Nagle's algorithm */
1852 tcp_sock_set_nodelay(sock->sk);
1853}
1854
1855static void dlm_tcp_listen_sockopts(struct socket *sock)
1856{
1857 dlm_tcp_sockopts(sock);
1858 sock_set_reuseaddr(sock->sk);
1859}
1860
1861static int dlm_tcp_listen_bind(struct socket *sock)
1862{
1863 int addr_len;
1864
1865 /* Bind to our port */
1866 make_sockaddr(dlm_local_addr[0], dlm_config.ci_tcp_port, &addr_len);
1867 return sock->ops->bind(sock, (struct sockaddr *)dlm_local_addr[0],
1868 addr_len);
1869}
1870
Alexander Aringa66c0082021-07-16 16:22:40 -04001871static const struct dlm_proto_ops dlm_tcp_ops = {
Alexander Aring2dc6b112021-07-16 16:22:41 -04001872 .name = "TCP",
1873 .proto = IPPROTO_TCP,
Alexander Aring8728a452021-07-16 16:22:43 -04001874 .connect = dlm_tcp_connect,
1875 .sockopts = dlm_tcp_sockopts,
1876 .bind = dlm_tcp_bind,
Alexander Aring2dc6b112021-07-16 16:22:41 -04001877 .listen_validate = dlm_tcp_listen_validate,
1878 .listen_sockopts = dlm_tcp_listen_sockopts,
1879 .listen_bind = dlm_tcp_listen_bind,
Alexander Aringa66c0082021-07-16 16:22:40 -04001880 .shutdown_action = dlm_tcp_shutdown,
1881 .eof_condition = tcp_eof_condition,
1882};
1883
Alexander Aring8728a452021-07-16 16:22:43 -04001884static int dlm_sctp_bind(struct socket *sock)
1885{
1886 return sctp_bind_addrs(sock, 0);
1887}
1888
1889static int dlm_sctp_connect(struct connection *con, struct socket *sock,
1890 struct sockaddr *addr, int addr_len)
1891{
1892 int ret;
1893
1894 /*
1895 * Make sock->ops->connect() function return in specified time,
1896 * since O_NONBLOCK argument in connect() function does not work here,
1897 * then, we should restore the default value of this attribute.
1898 */
1899 sock_set_sndtimeo(sock->sk, 5);
1900 ret = sock->ops->connect(sock, addr, addr_len, 0);
1901 sock_set_sndtimeo(sock->sk, 0);
1902 if (ret < 0)
1903 return ret;
1904
1905 if (!test_and_set_bit(CF_CONNECTED, &con->flags))
1906 log_print("successful connected to node %d", con->nodeid);
1907
1908 return 0;
1909}
1910
Alexander Aring90d21fc2021-07-16 16:22:42 -04001911static int dlm_sctp_listen_validate(void)
1912{
1913 if (!IS_ENABLED(CONFIG_IP_SCTP)) {
1914 log_print("SCTP is not enabled by this kernel");
1915 return -EOPNOTSUPP;
1916 }
1917
1918 request_module("sctp");
1919 return 0;
1920}
1921
Alexander Aring2dc6b112021-07-16 16:22:41 -04001922static int dlm_sctp_bind_listen(struct socket *sock)
1923{
1924 return sctp_bind_addrs(sock, dlm_config.ci_tcp_port);
1925}
1926
1927static void dlm_sctp_sockopts(struct socket *sock)
1928{
1929 /* Turn off Nagle's algorithm */
1930 sctp_sock_set_nodelay(sock->sk);
1931 sock_set_rcvbuf(sock->sk, NEEDED_RMEM);
1932}
1933
Alexander Aringa66c0082021-07-16 16:22:40 -04001934static const struct dlm_proto_ops dlm_sctp_ops = {
Alexander Aring2dc6b112021-07-16 16:22:41 -04001935 .name = "SCTP",
1936 .proto = IPPROTO_SCTP,
Alexander Aring8728a452021-07-16 16:22:43 -04001937 .try_new_addr = true,
1938 .connect = dlm_sctp_connect,
1939 .sockopts = dlm_sctp_sockopts,
1940 .bind = dlm_sctp_bind,
Alexander Aring90d21fc2021-07-16 16:22:42 -04001941 .listen_validate = dlm_sctp_listen_validate,
Alexander Aring2dc6b112021-07-16 16:22:41 -04001942 .listen_sockopts = dlm_sctp_sockopts,
1943 .listen_bind = dlm_sctp_bind_listen,
Alexander Aringa66c0082021-07-16 16:22:40 -04001944};
1945
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001946int dlm_lowcomms_start(void)
1947{
1948 int error = -EINVAL;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001949 int i;
1950
1951 for (i = 0; i < CONN_HASH_SIZE; i++)
1952 INIT_HLIST_HEAD(&connection_hash[i]);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001953
1954 init_local();
1955 if (!dlm_local_count) {
David Teigland617e82e2007-04-26 13:46:49 -05001956 error = -ENOTCONN;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001957 log_print("no local IP address has been set");
David Teigland513ef592012-03-30 11:46:08 -05001958 goto fail;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001959 }
1960
Alexander Aringd11ccd42020-11-02 20:04:25 -05001961 INIT_WORK(&listen_con.rwork, process_listen_recv_socket);
1962
David Teigland513ef592012-03-30 11:46:08 -05001963 error = work_start();
1964 if (error)
Alexander Aringfcef0e62021-06-02 09:45:15 -04001965 goto fail_local;
David Teigland513ef592012-03-30 11:46:08 -05001966
1967 dlm_allow_conn = 1;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001968
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001969 /* Start listening */
Alexander Aringac7d5d02021-06-02 09:45:19 -04001970 switch (dlm_config.ci_protocol) {
1971 case DLM_PROTO_TCP:
Alexander Aringa66c0082021-07-16 16:22:40 -04001972 dlm_proto_ops = &dlm_tcp_ops;
Alexander Aringac7d5d02021-06-02 09:45:19 -04001973 break;
1974 case DLM_PROTO_SCTP:
Alexander Aringa66c0082021-07-16 16:22:40 -04001975 dlm_proto_ops = &dlm_sctp_ops;
Alexander Aringac7d5d02021-06-02 09:45:19 -04001976 break;
1977 default:
1978 log_print("Invalid protocol identifier %d set",
1979 dlm_config.ci_protocol);
1980 error = -EINVAL;
Alexander Aring2dc6b112021-07-16 16:22:41 -04001981 goto fail_proto_ops;
Alexander Aringac7d5d02021-06-02 09:45:19 -04001982 }
Alexander Aring2dc6b112021-07-16 16:22:41 -04001983
1984 error = dlm_listen_for_all();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001985 if (error)
Alexander Aring2dc6b112021-07-16 16:22:41 -04001986 goto fail_listen;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001987
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001988 return 0;
1989
Alexander Aring2dc6b112021-07-16 16:22:41 -04001990fail_listen:
1991 dlm_proto_ops = NULL;
1992fail_proto_ops:
David Teigland513ef592012-03-30 11:46:08 -05001993 dlm_allow_conn = 0;
Alexander Aringd11ccd42020-11-02 20:04:25 -05001994 dlm_close_sock(&listen_con.sock);
Alexander Aringfcef0e62021-06-02 09:45:15 -04001995 work_stop();
1996fail_local:
1997 deinit_local();
David Teigland513ef592012-03-30 11:46:08 -05001998fail:
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001999 return error;
2000}
David Teigland36b71a82012-07-26 12:44:30 -05002001
2002void dlm_lowcomms_exit(void)
2003{
2004 struct dlm_node_addr *na, *safe;
2005
2006 spin_lock(&dlm_node_addrs_spin);
2007 list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) {
2008 list_del(&na->list);
2009 while (na->addr_count--)
2010 kfree(na->addr[na->addr_count]);
2011 kfree(na);
2012 }
2013 spin_unlock(&dlm_node_addrs_spin);
2014}