blob: c7750849c495460eceb5d6894a4e00c282aaf76f [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
491 read_lock_bh(&sk->sk_callback_lock);
492 con = sock2con(sk);
Patrick Caulfieldafb853f2007-06-01 10:07:26 -0500493 if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100494 queue_work(recv_workqueue, &con->rwork);
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000495 read_unlock_bh(&sk->sk_callback_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100496}
497
Alexander Aringd11ccd42020-11-02 20:04:25 -0500498static void lowcomms_listen_data_ready(struct sock *sk)
499{
Alexander Aring9a4139a2021-06-02 09:45:18 -0400500 if (!dlm_allow_conn)
501 return;
502
Alexander Aringd11ccd42020-11-02 20:04:25 -0500503 queue_work(recv_workqueue, &listen_con.rwork);
504}
505
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100506static void lowcomms_write_space(struct sock *sk)
507{
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000508 struct connection *con;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100509
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000510 read_lock_bh(&sk->sk_callback_lock);
511 con = sock2con(sk);
David Millerb36930d2010-11-10 21:56:39 -0800512 if (!con)
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000513 goto out;
David Millerb36930d2010-11-10 21:56:39 -0800514
Alexander Aring19633c72020-11-02 20:04:20 -0500515 if (!test_and_set_bit(CF_CONNECTED, &con->flags)) {
516 log_print("successful connected to node %d", con->nodeid);
517 queue_work(send_workqueue, &con->swork);
518 goto out;
519 }
520
David Millerb36930d2010-11-10 21:56:39 -0800521 clear_bit(SOCK_NOSPACE, &con->sock->flags);
522
523 if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) {
524 con->sock->sk->sk_write_pending--;
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800525 clear_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags);
David Millerb36930d2010-11-10 21:56:39 -0800526 }
527
Bob Peterson01da24d2017-09-12 08:55:14 +0000528 queue_work(send_workqueue, &con->swork);
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000529out:
530 read_unlock_bh(&sk->sk_callback_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100531}
532
533static inline void lowcomms_connect_sock(struct connection *con)
534{
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -0500535 if (test_bit(CF_CLOSE, &con->flags))
536 return;
Bob Peterson61d9102b2017-09-12 08:55:04 +0000537 queue_work(send_workqueue, &con->swork);
538 cond_resched();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100539}
540
541static void lowcomms_state_change(struct sock *sk)
542{
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300543 /* SCTP layer is not calling sk_data_ready when the connection
544 * is done, so we catch the signal through here. Also, it
545 * doesn't switch socket state when entering shutdown, so we
546 * skip the write in that case.
547 */
548 if (sk->sk_shutdown) {
549 if (sk->sk_shutdown == RCV_SHUTDOWN)
550 lowcomms_data_ready(sk);
551 } else if (sk->sk_state == TCP_ESTABLISHED) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100552 lowcomms_write_space(sk);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300553 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100554}
555
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500556int dlm_lowcomms_connect_node(int nodeid)
557{
558 struct connection *con;
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400559 int idx;
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500560
561 if (nodeid == dlm_our_nodeid())
562 return 0;
563
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400564 idx = srcu_read_lock(&connections_srcu);
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500565 con = nodeid2con(nodeid, GFP_NOFS);
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400566 if (!con) {
567 srcu_read_unlock(&connections_srcu, idx);
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500568 return -ENOMEM;
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400569 }
570
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500571 lowcomms_connect_sock(con);
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400572 srcu_read_unlock(&connections_srcu, idx);
573
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500574 return 0;
575}
576
Alexander Aringe125fbe2021-03-01 17:05:09 -0500577int dlm_lowcomms_nodes_set_mark(int nodeid, unsigned int mark)
578{
579 struct dlm_node_addr *na;
580
581 spin_lock(&dlm_node_addrs_spin);
582 na = find_node_addr(nodeid);
583 if (!na) {
584 spin_unlock(&dlm_node_addrs_spin);
585 return -ENOENT;
586 }
587
588 na->mark = mark;
589 spin_unlock(&dlm_node_addrs_spin);
590
591 return 0;
592}
593
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500594static void lowcomms_error_report(struct sock *sk)
595{
Bob Petersonb81171c2016-02-05 14:39:02 -0500596 struct connection *con;
Bob Petersonb81171c2016-02-05 14:39:02 -0500597 void (*orig_report)(struct sock *) = NULL;
Alexander Aring4c3d9052021-11-15 08:57:05 -0500598 struct inet_sock *inet;
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500599
Bob Petersonb81171c2016-02-05 14:39:02 -0500600 read_lock_bh(&sk->sk_callback_lock);
601 con = sock2con(sk);
602 if (con == NULL)
603 goto out;
604
Bob Petersoncc661fc2017-09-12 08:55:23 +0000605 orig_report = listen_sock.sk_error_report;
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500606
Alexander Aring4c3d9052021-11-15 08:57:05 -0500607 inet = inet_sk(sk);
608 switch (sk->sk_family) {
609 case AF_INET:
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500610 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
Alexander Aring4c3d9052021-11-15 08:57:05 -0500611 "sending to node %d at %pI4, dport %d, "
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500612 "sk_err=%d/%d\n", dlm_our_nodeid(),
Alexander Aring4c3d9052021-11-15 08:57:05 -0500613 con->nodeid, &inet->inet_daddr,
614 ntohs(inet->inet_dport), sk->sk_err,
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500615 sk->sk_err_soft);
Alexander Aring4c3d9052021-11-15 08:57:05 -0500616 break;
617 case AF_INET6:
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500618 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
Alexander Aring4c3d9052021-11-15 08:57:05 -0500619 "sending to node %d at %pI6c, "
620 "dport %d, sk_err=%d/%d\n", dlm_our_nodeid(),
621 con->nodeid, &sk->sk_v6_daddr,
622 ntohs(inet->inet_dport), sk->sk_err,
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500623 sk->sk_err_soft);
Alexander Aring4c3d9052021-11-15 08:57:05 -0500624 break;
625 default:
626 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
627 "invalid socket family %d set, "
628 "sk_err=%d/%d\n", dlm_our_nodeid(),
629 sk->sk_family, sk->sk_err, sk->sk_err_soft);
630 goto out;
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500631 }
Alexander Aringba868d92021-05-21 15:08:37 -0400632
633 /* below sendcon only handling */
634 if (test_bit(CF_IS_OTHERCON, &con->flags))
635 con = con->sendcon;
636
637 switch (sk->sk_err) {
638 case ECONNREFUSED:
639 set_bit(CF_DELAY_CONNECT, &con->flags);
640 break;
641 default:
642 break;
643 }
644
645 if (!test_and_set_bit(CF_RECONNECT, &con->flags))
646 queue_work(send_workqueue, &con->swork);
647
Bob Petersonb81171c2016-02-05 14:39:02 -0500648out:
649 read_unlock_bh(&sk->sk_callback_lock);
650 if (orig_report)
651 orig_report(sk);
652}
653
654/* Note: sk_callback_lock must be locked before calling this function. */
Bob Petersoncc661fc2017-09-12 08:55:23 +0000655static void save_listen_callbacks(struct socket *sock)
Bob Petersonb81171c2016-02-05 14:39:02 -0500656{
Bob Petersoncc661fc2017-09-12 08:55:23 +0000657 struct sock *sk = sock->sk;
658
659 listen_sock.sk_data_ready = sk->sk_data_ready;
660 listen_sock.sk_state_change = sk->sk_state_change;
661 listen_sock.sk_write_space = sk->sk_write_space;
662 listen_sock.sk_error_report = sk->sk_error_report;
Bob Petersonb81171c2016-02-05 14:39:02 -0500663}
664
Bob Petersoncc661fc2017-09-12 08:55:23 +0000665static void restore_callbacks(struct socket *sock)
Bob Petersonb81171c2016-02-05 14:39:02 -0500666{
Bob Petersoncc661fc2017-09-12 08:55:23 +0000667 struct sock *sk = sock->sk;
668
Bob Petersonb81171c2016-02-05 14:39:02 -0500669 write_lock_bh(&sk->sk_callback_lock);
Bob Petersonb81171c2016-02-05 14:39:02 -0500670 sk->sk_user_data = NULL;
Bob Petersoncc661fc2017-09-12 08:55:23 +0000671 sk->sk_data_ready = listen_sock.sk_data_ready;
672 sk->sk_state_change = listen_sock.sk_state_change;
673 sk->sk_write_space = listen_sock.sk_write_space;
674 sk->sk_error_report = listen_sock.sk_error_report;
Bob Petersonb81171c2016-02-05 14:39:02 -0500675 write_unlock_bh(&sk->sk_callback_lock);
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500676}
677
Alexander Aringd11ccd42020-11-02 20:04:25 -0500678static void add_listen_sock(struct socket *sock, struct listen_connection *con)
679{
680 struct sock *sk = sock->sk;
681
682 write_lock_bh(&sk->sk_callback_lock);
683 save_listen_callbacks(sock);
684 con->sock = sock;
685
686 sk->sk_user_data = con;
687 sk->sk_allocation = GFP_NOFS;
688 /* Install a data_ready callback */
689 sk->sk_data_ready = lowcomms_listen_data_ready;
690 write_unlock_bh(&sk->sk_callback_lock);
691}
692
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100693/* Make a socket active */
tsutomu.owa@toshiba.co.jp988419a2017-09-12 08:55:32 +0000694static void add_sock(struct socket *sock, struct connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100695{
Bob Petersonb81171c2016-02-05 14:39:02 -0500696 struct sock *sk = sock->sk;
697
698 write_lock_bh(&sk->sk_callback_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100699 con->sock = sock;
700
Bob Petersonb81171c2016-02-05 14:39:02 -0500701 sk->sk_user_data = con;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100702 /* Install a data_ready callback */
Bob Petersonb81171c2016-02-05 14:39:02 -0500703 sk->sk_data_ready = lowcomms_data_ready;
704 sk->sk_write_space = lowcomms_write_space;
705 sk->sk_state_change = lowcomms_state_change;
706 sk->sk_allocation = GFP_NOFS;
707 sk->sk_error_report = lowcomms_error_report;
708 write_unlock_bh(&sk->sk_callback_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100709}
710
711/* Add the port number to an IPv6 or 4 sockaddr and return the address
712 length */
713static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
714 int *addr_len)
715{
716 saddr->ss_family = dlm_local_addr[0]->ss_family;
717 if (saddr->ss_family == AF_INET) {
718 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
719 in4_addr->sin_port = cpu_to_be16(port);
720 *addr_len = sizeof(struct sockaddr_in);
721 memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
722 } else {
723 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
724 in6_addr->sin6_port = cpu_to_be16(port);
725 *addr_len = sizeof(struct sockaddr_in6);
726 }
Patrick Caulfield01c8cab2007-07-17 16:53:15 +0100727 memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100728}
729
Alexander Aring706474f2021-05-21 15:08:48 -0400730static void dlm_page_release(struct kref *kref)
731{
732 struct writequeue_entry *e = container_of(kref, struct writequeue_entry,
733 ref);
734
735 __free_page(e->page);
736 kfree(e);
737}
738
739static void dlm_msg_release(struct kref *kref)
740{
741 struct dlm_msg *msg = container_of(kref, struct dlm_msg, ref);
742
743 kref_put(&msg->entry->ref, dlm_page_release);
744 kfree(msg);
745}
746
747static void free_entry(struct writequeue_entry *e)
748{
749 struct dlm_msg *msg, *tmp;
750
751 list_for_each_entry_safe(msg, tmp, &e->msgs, list) {
752 if (msg->orig_msg) {
753 msg->orig_msg->retransmit = false;
754 kref_put(&msg->orig_msg->ref, dlm_msg_release);
755 }
756
757 list_del(&msg->list);
758 kref_put(&msg->ref, dlm_msg_release);
759 }
760
761 list_del(&e->list);
762 atomic_dec(&e->con->writequeue_cnt);
763 kref_put(&e->ref, dlm_page_release);
764}
765
Alexander Aringd11ccd42020-11-02 20:04:25 -0500766static void dlm_close_sock(struct socket **sock)
767{
768 if (*sock) {
769 restore_callbacks(*sock);
770 sock_release(*sock);
771 *sock = NULL;
772 }
773}
774
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100775/* Close a remote connection and tidy up */
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300776static void close_connection(struct connection *con, bool and_other,
777 bool tx, bool rx)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100778{
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +0000779 bool closing = test_and_set_bit(CF_CLOSING, &con->flags);
Alexander Aring706474f2021-05-21 15:08:48 -0400780 struct writequeue_entry *e;
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +0000781
tsutomu.owa@toshiba.co.jp0aa18462017-09-12 09:02:02 +0000782 if (tx && !closing && cancel_work_sync(&con->swork)) {
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300783 log_print("canceled swork for node %d", con->nodeid);
tsutomu.owa@toshiba.co.jp0aa18462017-09-12 09:02:02 +0000784 clear_bit(CF_WRITE_PENDING, &con->flags);
785 }
786 if (rx && !closing && cancel_work_sync(&con->rwork)) {
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300787 log_print("canceled rwork for node %d", con->nodeid);
tsutomu.owa@toshiba.co.jp0aa18462017-09-12 09:02:02 +0000788 clear_bit(CF_READ_PENDING, &con->flags);
789 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100790
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300791 mutex_lock(&con->sock_mutex);
Alexander Aringd11ccd42020-11-02 20:04:25 -0500792 dlm_close_sock(&con->sock);
793
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100794 if (con->othercon && and_other) {
795 /* Will only re-enter once. */
Alexander Aringc6aa00e32021-05-21 15:08:38 -0400796 close_connection(con->othercon, false, tx, rx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100797 }
Patrick Caulfield9e5f2822007-08-02 14:58:14 +0100798
Alexander Aring706474f2021-05-21 15:08:48 -0400799 /* if we send a writequeue entry only a half way, we drop the
800 * whole entry because reconnection and that we not start of the
801 * middle of a msg which will confuse the other end.
802 *
803 * we can always drop messages because retransmits, but what we
804 * cannot allow is to transmit half messages which may be processed
805 * at the other side.
806 *
807 * our policy is to start on a clean state when disconnects, we don't
808 * know what's send/received on transport layer in this case.
809 */
810 spin_lock(&con->writequeue_lock);
811 if (!list_empty(&con->writequeue)) {
812 e = list_first_entry(&con->writequeue, struct writequeue_entry,
813 list);
814 if (e->dirty)
815 free_entry(e);
816 }
817 spin_unlock(&con->writequeue_lock);
818
Alexander Aring4798cbb2020-09-24 10:31:26 -0400819 con->rx_leftover = 0;
Patrick Caulfield61d96be02007-08-20 15:13:38 +0100820 con->retries = 0;
Alexander Aring052849b2021-07-16 16:22:37 -0400821 clear_bit(CF_APP_LIMITED, &con->flags);
Alexander Aring19633c72020-11-02 20:04:20 -0500822 clear_bit(CF_CONNECTED, &con->flags);
Alexander Aringba868d92021-05-21 15:08:37 -0400823 clear_bit(CF_DELAY_CONNECT, &con->flags);
824 clear_bit(CF_RECONNECT, &con->flags);
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400825 clear_bit(CF_EOF, &con->flags);
Patrick Caulfield61d96be02007-08-20 15:13:38 +0100826 mutex_unlock(&con->sock_mutex);
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +0000827 clear_bit(CF_CLOSING, &con->flags);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100828}
829
Alexander Aring055923b2020-07-27 09:13:38 -0400830static void shutdown_connection(struct connection *con)
831{
832 int ret;
833
Alexander Aringeec054b2021-03-01 17:05:19 -0500834 flush_work(&con->swork);
Alexander Aring055923b2020-07-27 09:13:38 -0400835
836 mutex_lock(&con->sock_mutex);
837 /* nothing to shutdown */
838 if (!con->sock) {
839 mutex_unlock(&con->sock_mutex);
840 return;
841 }
842
843 set_bit(CF_SHUTDOWN, &con->flags);
844 ret = kernel_sock_shutdown(con->sock, SHUT_WR);
845 mutex_unlock(&con->sock_mutex);
846 if (ret) {
847 log_print("Connection %p failed to shutdown: %d will force close",
848 con, ret);
849 goto force_close;
850 } else {
851 ret = wait_event_timeout(con->shutdown_wait,
852 !test_bit(CF_SHUTDOWN, &con->flags),
853 DLM_SHUTDOWN_WAIT_TIMEOUT);
854 if (ret == 0) {
855 log_print("Connection %p shutdown timed out, will force close",
856 con);
857 goto force_close;
858 }
859 }
860
861 return;
862
863force_close:
864 clear_bit(CF_SHUTDOWN, &con->flags);
865 close_connection(con, false, true, true);
866}
867
868static void dlm_tcp_shutdown(struct connection *con)
869{
870 if (con->othercon)
871 shutdown_connection(con->othercon);
872 shutdown_connection(con);
873}
874
Alexander Aring4798cbb2020-09-24 10:31:26 -0400875static int con_realloc_receive_buf(struct connection *con, int newlen)
876{
877 unsigned char *newbuf;
878
879 newbuf = kmalloc(newlen, GFP_NOFS);
880 if (!newbuf)
881 return -ENOMEM;
882
883 /* copy any leftover from last receive */
884 if (con->rx_leftover)
885 memmove(newbuf, con->rx_buf, con->rx_leftover);
886
887 /* swap to new buffer space */
888 kfree(con->rx_buf);
889 con->rx_buflen = newlen;
890 con->rx_buf = newbuf;
891
892 return 0;
893}
894
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100895/* Data received from remote end */
896static int receive_from_sock(struct connection *con)
897{
Alexander Aring4798cbb2020-09-24 10:31:26 -0400898 struct msghdr msg;
899 struct kvec iov;
900 int ret, buflen;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100901
902 mutex_lock(&con->sock_mutex);
903
904 if (con->sock == NULL) {
905 ret = -EAGAIN;
906 goto out_close;
907 }
Alexander Aring4798cbb2020-09-24 10:31:26 -0400908
Alexander Aring4798cbb2020-09-24 10:31:26 -0400909 /* realloc if we get new buffer size to read out */
910 buflen = dlm_config.ci_buffer_size;
911 if (con->rx_buflen != buflen && con->rx_leftover <= buflen) {
912 ret = con_realloc_receive_buf(con, buflen);
913 if (ret < 0)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100914 goto out_resched;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100915 }
916
Alexander Aring62699b32021-07-16 16:22:45 -0400917 for (;;) {
918 /* calculate new buffer parameter regarding last receive and
919 * possible leftover bytes
920 */
921 iov.iov_base = con->rx_buf + con->rx_leftover;
922 iov.iov_len = con->rx_buflen - con->rx_leftover;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100923
Alexander Aring62699b32021-07-16 16:22:45 -0400924 memset(&msg, 0, sizeof(msg));
925 msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
926 ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len,
927 msg.msg_flags);
Alexander Aring92732372021-11-02 15:17:16 -0400928 trace_dlm_recv(con->nodeid, ret);
Alexander Aring62699b32021-07-16 16:22:45 -0400929 if (ret == -EAGAIN)
930 break;
931 else if (ret <= 0)
932 goto out_close;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100933
Alexander Aring62699b32021-07-16 16:22:45 -0400934 /* new buflen according readed bytes and leftover from last receive */
935 buflen = ret + con->rx_leftover;
936 ret = dlm_process_incoming_buffer(con->nodeid, con->rx_buf, buflen);
937 if (ret < 0)
938 goto out_close;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100939
Alexander Aring62699b32021-07-16 16:22:45 -0400940 /* calculate leftover bytes from process and put it into begin of
941 * the receive buffer, so next receive we have the full message
942 * at the start address of the receive buffer.
943 */
944 con->rx_leftover = buflen - ret;
945 if (con->rx_leftover) {
946 memmove(con->rx_buf, con->rx_buf + ret,
947 con->rx_leftover);
948 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100949 }
950
Alexander Aringb97f8522021-08-18 16:27:13 -0400951 dlm_midcomms_receive_done(con->nodeid);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100952 mutex_unlock(&con->sock_mutex);
953 return 0;
954
955out_resched:
956 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
957 queue_work(recv_workqueue, &con->rwork);
958 mutex_unlock(&con->sock_mutex);
959 return -EAGAIN;
960
961out_close:
Alexander Aringba868d92021-05-21 15:08:37 -0400962 if (ret == 0) {
Alexander Aringba868d92021-05-21 15:08:37 -0400963 log_print("connection %p got EOF from %d",
964 con, con->nodeid);
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400965
Alexander Aringa66c0082021-07-16 16:22:40 -0400966 if (dlm_proto_ops->eof_condition &&
967 dlm_proto_ops->eof_condition(con)) {
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400968 set_bit(CF_EOF, &con->flags);
969 mutex_unlock(&con->sock_mutex);
970 } else {
971 mutex_unlock(&con->sock_mutex);
972 close_connection(con, false, true, false);
973
974 /* handling for tcp shutdown */
975 clear_bit(CF_SHUTDOWN, &con->flags);
976 wake_up(&con->shutdown_wait);
977 }
978
Alexander Aringba868d92021-05-21 15:08:37 -0400979 /* signal to breaking receive worker */
980 ret = -1;
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400981 } else {
982 mutex_unlock(&con->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100983 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100984 return ret;
985}
986
987/* Listening socket is busy, accept a connection */
Alexander Aringd11ccd42020-11-02 20:04:25 -0500988static int accept_from_sock(struct listen_connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100989{
990 int result;
991 struct sockaddr_storage peeraddr;
992 struct socket *newsock;
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400993 int len, idx;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100994 int nodeid;
995 struct connection *newcon;
996 struct connection *addcon;
Alexander Aring3f78cd72020-09-24 10:31:23 -0400997 unsigned int mark;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100998
Alexander Aringd11ccd42020-11-02 20:04:25 -0500999 if (!con->sock)
tsutomu.owa@toshiba.co.jp3421fb12017-09-12 09:01:38 +00001000 return -ENOTCONN;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001001
tsutomu.owa@toshiba.co.jp3421fb12017-09-12 09:01:38 +00001002 result = kernel_accept(con->sock, &newsock, O_NONBLOCK);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001003 if (result < 0)
1004 goto accept_err;
1005
1006 /* Get the connected socket's peer */
1007 memset(&peeraddr, 0, sizeof(peeraddr));
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001008 len = newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr, 2);
1009 if (len < 0) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001010 result = -ECONNABORTED;
1011 goto accept_err;
1012 }
1013
1014 /* Get the new node's NODEID */
1015 make_sockaddr(&peeraddr, 0, &len);
Alexander Aringe125fbe2021-03-01 17:05:09 -05001016 if (addr_to_nodeid(&peeraddr, &nodeid, &mark)) {
Masatake YAMATObcaadf52011-07-04 12:25:51 +09001017 unsigned char *b=(unsigned char *)&peeraddr;
David Teigland617e82e2007-04-26 13:46:49 -05001018 log_print("connect from non cluster node");
Masatake YAMATObcaadf52011-07-04 12:25:51 +09001019 print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
1020 b, sizeof(struct sockaddr_storage));
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001021 sock_release(newsock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001022 return -1;
1023 }
1024
1025 log_print("got connection from %d", nodeid);
1026
1027 /* Check to see if we already have a connection to this node. This
1028 * could happen if the two nodes initiate a connection at roughly
1029 * the same time and the connections cross on the wire.
1030 * In this case we store the incoming one in "othercon"
1031 */
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001032 idx = srcu_read_lock(&connections_srcu);
David Teigland748285c2009-05-15 10:50:57 -05001033 newcon = nodeid2con(nodeid, GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001034 if (!newcon) {
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001035 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001036 result = -ENOMEM;
1037 goto accept_err;
1038 }
Alexander Aringd11ccd42020-11-02 20:04:25 -05001039
Alexander Aringe125fbe2021-03-01 17:05:09 -05001040 sock_set_mark(newsock->sk, mark);
1041
Alexander Aringd11ccd42020-11-02 20:04:25 -05001042 mutex_lock(&newcon->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001043 if (newcon->sock) {
1044 struct connection *othercon = newcon->othercon;
1045
1046 if (!othercon) {
Alexander Aringa47666eb2020-08-27 15:02:49 -04001047 othercon = kzalloc(sizeof(*othercon), GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001048 if (!othercon) {
David Teigland617e82e2007-04-26 13:46:49 -05001049 log_print("failed to allocate incoming socket");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001050 mutex_unlock(&newcon->sock_mutex);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001051 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001052 result = -ENOMEM;
1053 goto accept_err;
1054 }
Alexander Aring4798cbb2020-09-24 10:31:26 -04001055
Alexander Aring6cde2102020-11-02 20:04:21 -05001056 result = dlm_con_init(othercon, nodeid);
1057 if (result < 0) {
Alexander Aring4798cbb2020-09-24 10:31:26 -04001058 kfree(othercon);
Yang Yingliang2fd8db22021-03-27 16:37:04 +08001059 mutex_unlock(&newcon->sock_mutex);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001060 srcu_read_unlock(&connections_srcu, idx);
Alexander Aring4798cbb2020-09-24 10:31:26 -04001061 goto accept_err;
1062 }
1063
Alexander Aringe9a470a2021-03-01 17:05:11 -05001064 lockdep_set_subclass(&othercon->sock_mutex, 1);
Alexander Aring7443bc92021-05-21 15:08:36 -04001065 set_bit(CF_IS_OTHERCON, &othercon->flags);
Alexander Aring6cde2102020-11-02 20:04:21 -05001066 newcon->othercon = othercon;
Alexander Aringba868d92021-05-21 15:08:37 -04001067 othercon->sendcon = newcon;
Alexander Aringba3ab3c2020-07-27 09:13:37 -04001068 } else {
1069 /* close other sock con if we have something new */
1070 close_connection(othercon, false, true, false);
Patrick Caulfield61d96be02007-08-20 15:13:38 +01001071 }
Alexander Aringba3ab3c2020-07-27 09:13:37 -04001072
Alexander Aringe9a470a2021-03-01 17:05:11 -05001073 mutex_lock(&othercon->sock_mutex);
Alexander Aringba3ab3c2020-07-27 09:13:37 -04001074 add_sock(newsock, othercon);
1075 addcon = othercon;
1076 mutex_unlock(&othercon->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001077 }
1078 else {
Bob Peterson3735b4b2016-09-23 14:23:26 -04001079 /* accept copies the sk after we've saved the callbacks, so we
1080 don't want to save them a second time or comm errors will
1081 result in calling sk_error_report recursively. */
tsutomu.owa@toshiba.co.jp988419a2017-09-12 08:55:32 +00001082 add_sock(newsock, newcon);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001083 addcon = newcon;
1084 }
1085
Alexander Aringb30a6242021-03-01 17:05:10 -05001086 set_bit(CF_CONNECTED, &addcon->flags);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001087 mutex_unlock(&newcon->sock_mutex);
1088
1089 /*
1090 * Add it to the active queue in case we got data
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001091 * between processing the accept adding the socket
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001092 * to the read_sockets list
1093 */
1094 if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
1095 queue_work(recv_workqueue, &addcon->rwork);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001096
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001097 srcu_read_unlock(&connections_srcu, idx);
1098
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001099 return 0;
1100
1101accept_err:
tsutomu.owa@toshiba.co.jp3421fb12017-09-12 09:01:38 +00001102 if (newsock)
1103 sock_release(newsock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001104
1105 if (result != -EAGAIN)
David Teigland617e82e2007-04-26 13:46:49 -05001106 log_print("error accepting connection from node: %d", result);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001107 return result;
1108}
1109
Mike Christie5d689872013-06-14 04:56:13 -05001110/*
1111 * writequeue_entry_complete - try to delete and free write queue entry
1112 * @e: write queue entry to try to delete
1113 * @completed: bytes completed
1114 *
1115 * writequeue_lock must be held.
1116 */
1117static void writequeue_entry_complete(struct writequeue_entry *e, int completed)
1118{
1119 e->offset += completed;
1120 e->len -= completed;
Alexander Aring706474f2021-05-21 15:08:48 -04001121 /* signal that page was half way transmitted */
1122 e->dirty = true;
Mike Christie5d689872013-06-14 04:56:13 -05001123
Alexander Aring8f2dc782021-05-21 15:08:42 -04001124 if (e->len == 0 && e->users == 0)
Mike Christie5d689872013-06-14 04:56:13 -05001125 free_entry(e);
Mike Christie5d689872013-06-14 04:56:13 -05001126}
1127
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001128/*
1129 * sctp_bind_addrs - bind a SCTP socket to all our addresses
1130 */
Alexander Aring13004e82020-11-02 20:04:24 -05001131static int sctp_bind_addrs(struct socket *sock, uint16_t port)
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001132{
1133 struct sockaddr_storage localaddr;
Christoph Hellwigc0425a42020-05-29 14:09:42 +02001134 struct sockaddr *addr = (struct sockaddr *)&localaddr;
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001135 int i, addr_len, result = 0;
1136
1137 for (i = 0; i < dlm_local_count; i++) {
1138 memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
1139 make_sockaddr(&localaddr, port, &addr_len);
1140
1141 if (!i)
Alexander Aring13004e82020-11-02 20:04:24 -05001142 result = kernel_bind(sock, addr, addr_len);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001143 else
Alexander Aring13004e82020-11-02 20:04:24 -05001144 result = sock_bind_add(sock->sk, addr, addr_len);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001145
1146 if (result < 0) {
1147 log_print("Can't bind to %d addr number %d, %d.\n",
1148 port, i + 1, result);
1149 break;
1150 }
1151 }
1152 return result;
1153}
1154
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001155/* Get local addresses */
1156static void init_local(void)
1157{
1158 struct sockaddr_storage sas, *addr;
1159 int i;
1160
Patrick Caulfield30d3a232007-04-23 16:26:21 +01001161 dlm_local_count = 0;
David Teigland1b189b82012-03-21 09:18:34 -05001162 for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001163 if (dlm_our_addr(&sas, i))
1164 break;
1165
Amitoj Kaur Chawla5c93f562016-06-23 10:22:01 +05301166 addr = kmemdup(&sas, sizeof(*addr), GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001167 if (!addr)
1168 break;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001169 dlm_local_addr[dlm_local_count++] = addr;
1170 }
1171}
1172
Alexander Aring043697f2020-08-27 15:02:50 -04001173static void deinit_local(void)
1174{
1175 int i;
1176
1177 for (i = 0; i < dlm_local_count; i++)
1178 kfree(dlm_local_addr[i]);
1179}
1180
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001181static struct writequeue_entry *new_writequeue_entry(struct connection *con,
1182 gfp_t allocation)
1183{
1184 struct writequeue_entry *entry;
1185
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001186 entry = kzalloc(sizeof(*entry), allocation);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001187 if (!entry)
1188 return NULL;
1189
Alexander Aringe1a7cbc2021-03-01 17:05:15 -05001190 entry->page = alloc_page(allocation | __GFP_ZERO);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001191 if (!entry->page) {
1192 kfree(entry);
1193 return NULL;
1194 }
1195
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001196 entry->con = con;
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001197 entry->users = 1;
Alexander Aring8f2dc782021-05-21 15:08:42 -04001198 kref_init(&entry->ref);
1199 INIT_LIST_HEAD(&entry->msgs);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001200
1201 return entry;
1202}
1203
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001204static struct writequeue_entry *new_wq_entry(struct connection *con, int len,
Alexander Aring8f2dc782021-05-21 15:08:42 -04001205 gfp_t allocation, char **ppc,
Alexander Aring5c16feb2021-11-02 15:17:19 -04001206 void (*cb)(void *data), void *data)
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001207{
1208 struct writequeue_entry *e;
1209
1210 spin_lock(&con->writequeue_lock);
1211 if (!list_empty(&con->writequeue)) {
1212 e = list_last_entry(&con->writequeue, struct writequeue_entry, list);
1213 if (DLM_WQ_REMAIN_BYTES(e) >= len) {
Alexander Aring8f2dc782021-05-21 15:08:42 -04001214 kref_get(&e->ref);
1215
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001216 *ppc = page_address(e->page) + e->end;
Alexander Aring8f2dc782021-05-21 15:08:42 -04001217 if (cb)
Alexander Aring5c16feb2021-11-02 15:17:19 -04001218 cb(data);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001219
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001220 e->end += len;
1221 e->users++;
1222 spin_unlock(&con->writequeue_lock);
1223
1224 return e;
1225 }
1226 }
1227 spin_unlock(&con->writequeue_lock);
1228
1229 e = new_writequeue_entry(con, allocation);
1230 if (!e)
1231 return NULL;
1232
Alexander Aring8f2dc782021-05-21 15:08:42 -04001233 kref_get(&e->ref);
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001234 *ppc = page_address(e->page);
1235 e->end += len;
Alexander Aring8aa31cb2021-05-21 15:08:39 -04001236 atomic_inc(&con->writequeue_cnt);
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001237
1238 spin_lock(&con->writequeue_lock);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001239 if (cb)
Alexander Aring5c16feb2021-11-02 15:17:19 -04001240 cb(data);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001241
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001242 list_add_tail(&e->list, &con->writequeue);
1243 spin_unlock(&con->writequeue_lock);
1244
1245 return e;
1246};
1247
Alexander Aring2874d1a2021-05-21 15:08:43 -04001248static struct dlm_msg *dlm_lowcomms_new_msg_con(struct connection *con, int len,
1249 gfp_t allocation, char **ppc,
Alexander Aring5c16feb2021-11-02 15:17:19 -04001250 void (*cb)(void *data),
1251 void *data)
Alexander Aring2874d1a2021-05-21 15:08:43 -04001252{
1253 struct writequeue_entry *e;
1254 struct dlm_msg *msg;
Alexander Aringc51b0222021-07-16 16:22:44 -04001255 bool sleepable;
Alexander Aring2874d1a2021-05-21 15:08:43 -04001256
1257 msg = kzalloc(sizeof(*msg), allocation);
1258 if (!msg)
1259 return NULL;
1260
Alexander Aringc51b0222021-07-16 16:22:44 -04001261 /* this mutex is being used as a wait to avoid multiple "fast"
1262 * new writequeue page list entry allocs in new_wq_entry in
1263 * normal operation which is sleepable context. Without it
1264 * we could end in multiple writequeue entries with one
1265 * dlm message because multiple callers were waiting at
1266 * the writequeue_lock in new_wq_entry().
1267 */
1268 sleepable = gfpflags_normal_context(allocation);
1269 if (sleepable)
1270 mutex_lock(&con->wq_alloc);
1271
Alexander Aring2874d1a2021-05-21 15:08:43 -04001272 kref_init(&msg->ref);
1273
Alexander Aring5c16feb2021-11-02 15:17:19 -04001274 e = new_wq_entry(con, len, allocation, ppc, cb, data);
Alexander Aring2874d1a2021-05-21 15:08:43 -04001275 if (!e) {
Alexander Aringc51b0222021-07-16 16:22:44 -04001276 if (sleepable)
1277 mutex_unlock(&con->wq_alloc);
1278
Alexander Aring2874d1a2021-05-21 15:08:43 -04001279 kfree(msg);
1280 return NULL;
1281 }
1282
Alexander Aringc51b0222021-07-16 16:22:44 -04001283 if (sleepable)
1284 mutex_unlock(&con->wq_alloc);
1285
Alexander Aring2874d1a2021-05-21 15:08:43 -04001286 msg->ppc = *ppc;
1287 msg->len = len;
1288 msg->entry = e;
1289
1290 return msg;
1291}
1292
Alexander Aring8f2dc782021-05-21 15:08:42 -04001293struct dlm_msg *dlm_lowcomms_new_msg(int nodeid, int len, gfp_t allocation,
Alexander Aring5c16feb2021-11-02 15:17:19 -04001294 char **ppc, void (*cb)(void *data),
1295 void *data)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001296{
1297 struct connection *con;
Alexander Aring8f2dc782021-05-21 15:08:42 -04001298 struct dlm_msg *msg;
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001299 int idx;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001300
Alexander Aringd10a0b82021-06-02 09:45:20 -04001301 if (len > DLM_MAX_SOCKET_BUFSIZE ||
Alexander Aringc45674f2021-03-01 17:05:14 -05001302 len < sizeof(struct dlm_header)) {
Alexander Aringd10a0b82021-06-02 09:45:20 -04001303 BUILD_BUG_ON(PAGE_SIZE < DLM_MAX_SOCKET_BUFSIZE);
Alexander Aring692f51c2020-11-02 20:04:18 -05001304 log_print("failed to allocate a buffer of size %d", len);
Alexander Aringc45674f2021-03-01 17:05:14 -05001305 WARN_ON(1);
Alexander Aring692f51c2020-11-02 20:04:18 -05001306 return NULL;
1307 }
1308
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001309 idx = srcu_read_lock(&connections_srcu);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001310 con = nodeid2con(nodeid, allocation);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001311 if (!con) {
1312 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001313 return NULL;
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001314 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001315
Alexander Aring5c16feb2021-11-02 15:17:19 -04001316 msg = dlm_lowcomms_new_msg_con(con, len, allocation, ppc, cb, data);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001317 if (!msg) {
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001318 srcu_read_unlock(&connections_srcu, idx);
1319 return NULL;
1320 }
1321
Alexander Aring8f2dc782021-05-21 15:08:42 -04001322 /* we assume if successful commit must called */
1323 msg->idx = idx;
Alexander Aring8f2dc782021-05-21 15:08:42 -04001324 return msg;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001325}
1326
Alexander Aring2874d1a2021-05-21 15:08:43 -04001327static void _dlm_lowcomms_commit_msg(struct dlm_msg *msg)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001328{
Alexander Aring8f2dc782021-05-21 15:08:42 -04001329 struct writequeue_entry *e = msg->entry;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001330 struct connection *con = e->con;
1331 int users;
1332
1333 spin_lock(&con->writequeue_lock);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001334 kref_get(&msg->ref);
1335 list_add(&msg->list, &e->msgs);
1336
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001337 users = --e->users;
1338 if (users)
1339 goto out;
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001340
1341 e->len = DLM_WQ_LENGTH_BYTES(e);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001342 spin_unlock(&con->writequeue_lock);
1343
Bob Peterson01da24d2017-09-12 08:55:14 +00001344 queue_work(send_workqueue, &con->swork);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001345 return;
1346
1347out:
1348 spin_unlock(&con->writequeue_lock);
1349 return;
1350}
1351
Alexander Aring2874d1a2021-05-21 15:08:43 -04001352void dlm_lowcomms_commit_msg(struct dlm_msg *msg)
1353{
1354 _dlm_lowcomms_commit_msg(msg);
1355 srcu_read_unlock(&connections_srcu, msg->idx);
1356}
1357
Alexander Aring8f2dc782021-05-21 15:08:42 -04001358void dlm_lowcomms_put_msg(struct dlm_msg *msg)
1359{
1360 kref_put(&msg->ref, dlm_msg_release);
1361}
1362
Alexander Aring2874d1a2021-05-21 15:08:43 -04001363/* does not held connections_srcu, usage workqueue only */
1364int dlm_lowcomms_resend_msg(struct dlm_msg *msg)
1365{
1366 struct dlm_msg *msg_resend;
1367 char *ppc;
1368
1369 if (msg->retransmit)
1370 return 1;
1371
1372 msg_resend = dlm_lowcomms_new_msg_con(msg->entry->con, msg->len,
1373 GFP_ATOMIC, &ppc, NULL, NULL);
1374 if (!msg_resend)
1375 return -ENOMEM;
1376
1377 msg->retransmit = true;
1378 kref_get(&msg->ref);
1379 msg_resend->orig_msg = msg;
1380
1381 memcpy(ppc, msg->ppc, msg->len);
1382 _dlm_lowcomms_commit_msg(msg_resend);
1383 dlm_lowcomms_put_msg(msg_resend);
1384
1385 return 0;
1386}
1387
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001388/* Send a message */
1389static void send_to_sock(struct connection *con)
1390{
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001391 const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1392 struct writequeue_entry *e;
Alexander Aring66d59552021-07-16 16:22:39 -04001393 int len, offset, ret;
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001394 int count = 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001395
1396 mutex_lock(&con->sock_mutex);
1397 if (con->sock == NULL)
1398 goto out_connect;
1399
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001400 spin_lock(&con->writequeue_lock);
1401 for (;;) {
Alexander Aring66d59552021-07-16 16:22:39 -04001402 e = con_next_wq(con);
1403 if (!e)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001404 break;
1405
1406 len = e->len;
1407 offset = e->offset;
1408 BUG_ON(len == 0 && e->users == 0);
1409 spin_unlock(&con->writequeue_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001410
Alexander Aring66d59552021-07-16 16:22:39 -04001411 ret = kernel_sendpage(con->sock, e->page, offset, len,
1412 msg_flags);
Alexander Aring92732372021-11-02 15:17:16 -04001413 trace_dlm_send(con->nodeid, ret);
Alexander Aring66d59552021-07-16 16:22:39 -04001414 if (ret == -EAGAIN || ret == 0) {
1415 if (ret == -EAGAIN &&
1416 test_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags) &&
1417 !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
1418 /* Notify TCP that we're limited by the
1419 * application window size.
1420 */
1421 set_bit(SOCK_NOSPACE, &con->sock->flags);
1422 con->sock->sk->sk_write_pending++;
1423 }
1424 cond_resched();
1425 goto out;
1426 } else if (ret < 0)
1427 goto out;
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001428
1429 /* Don't starve people filling buffers */
1430 if (++count >= MAX_SEND_MSG_COUNT) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001431 cond_resched();
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001432 count = 0;
1433 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001434
1435 spin_lock(&con->writequeue_lock);
Mike Christie5d689872013-06-14 04:56:13 -05001436 writequeue_entry_complete(e, ret);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001437 }
1438 spin_unlock(&con->writequeue_lock);
Alexander Aring8aa31cb2021-05-21 15:08:39 -04001439
1440 /* close if we got EOF */
1441 if (test_and_clear_bit(CF_EOF, &con->flags)) {
1442 mutex_unlock(&con->sock_mutex);
1443 close_connection(con, false, false, true);
1444
1445 /* handling for tcp shutdown */
1446 clear_bit(CF_SHUTDOWN, &con->flags);
1447 wake_up(&con->shutdown_wait);
1448 } else {
1449 mutex_unlock(&con->sock_mutex);
1450 }
1451
1452 return;
1453
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001454out:
1455 mutex_unlock(&con->sock_mutex);
1456 return;
1457
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001458out_connect:
1459 mutex_unlock(&con->sock_mutex);
Bob Peterson01da24d2017-09-12 08:55:14 +00001460 queue_work(send_workqueue, &con->swork);
1461 cond_resched();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001462}
1463
1464static void clean_one_writequeue(struct connection *con)
1465{
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001466 struct writequeue_entry *e, *safe;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001467
1468 spin_lock(&con->writequeue_lock);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001469 list_for_each_entry_safe(e, safe, &con->writequeue, list) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001470 free_entry(e);
1471 }
1472 spin_unlock(&con->writequeue_lock);
1473}
1474
1475/* Called from recovery when it knows that a node has
1476 left the cluster */
1477int dlm_lowcomms_close(int nodeid)
1478{
1479 struct connection *con;
David Teigland36b71a82012-07-26 12:44:30 -05001480 struct dlm_node_addr *na;
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001481 int idx;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001482
1483 log_print("closing connection to node %d", nodeid);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001484 idx = srcu_read_lock(&connections_srcu);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001485 con = nodeid2con(nodeid, 0);
1486 if (con) {
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -05001487 set_bit(CF_CLOSE, &con->flags);
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -03001488 close_connection(con, true, true, true);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001489 clean_one_writequeue(con);
Alexander Aring53a5eda2020-11-02 20:04:19 -05001490 if (con->othercon)
1491 clean_one_writequeue(con->othercon);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001492 }
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001493 srcu_read_unlock(&connections_srcu, idx);
David Teigland36b71a82012-07-26 12:44:30 -05001494
1495 spin_lock(&dlm_node_addrs_spin);
1496 na = find_node_addr(nodeid);
1497 if (na) {
1498 list_del(&na->list);
1499 while (na->addr_count--)
1500 kfree(na->addr[na->addr_count]);
1501 kfree(na);
1502 }
1503 spin_unlock(&dlm_node_addrs_spin);
1504
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001505 return 0;
1506}
1507
1508/* Receive workqueue function */
1509static void process_recv_sockets(struct work_struct *work)
1510{
1511 struct connection *con = container_of(work, struct connection, rwork);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001512
1513 clear_bit(CF_READ_PENDING, &con->flags);
Alexander Aring62699b32021-07-16 16:22:45 -04001514 receive_from_sock(con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001515}
1516
Alexander Aringd11ccd42020-11-02 20:04:25 -05001517static void process_listen_recv_socket(struct work_struct *work)
1518{
1519 accept_from_sock(&listen_con);
1520}
1521
Alexander Aring8728a452021-07-16 16:22:43 -04001522static void dlm_connect(struct connection *con)
1523{
1524 struct sockaddr_storage addr;
1525 int result, addr_len;
1526 struct socket *sock;
1527 unsigned int mark;
1528
1529 /* Some odd races can cause double-connects, ignore them */
1530 if (con->retries++ > MAX_CONNECT_RETRIES)
1531 return;
1532
1533 if (con->sock) {
1534 log_print("node %d already connected.", con->nodeid);
1535 return;
1536 }
1537
1538 memset(&addr, 0, sizeof(addr));
1539 result = nodeid_to_addr(con->nodeid, &addr, NULL,
1540 dlm_proto_ops->try_new_addr, &mark);
1541 if (result < 0) {
1542 log_print("no address for nodeid %d", con->nodeid);
1543 return;
1544 }
1545
1546 /* Create a socket to communicate with */
1547 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1548 SOCK_STREAM, dlm_proto_ops->proto, &sock);
1549 if (result < 0)
1550 goto socket_err;
1551
1552 sock_set_mark(sock->sk, mark);
1553 dlm_proto_ops->sockopts(sock);
1554
1555 add_sock(sock, con);
1556
1557 result = dlm_proto_ops->bind(sock);
1558 if (result < 0)
1559 goto add_sock_err;
1560
1561 log_print_ratelimited("connecting to %d", con->nodeid);
1562 make_sockaddr(&addr, dlm_config.ci_tcp_port, &addr_len);
1563 result = dlm_proto_ops->connect(con, sock, (struct sockaddr *)&addr,
1564 addr_len);
1565 if (result < 0)
1566 goto add_sock_err;
1567
1568 return;
1569
1570add_sock_err:
1571 dlm_close_sock(&con->sock);
1572
1573socket_err:
1574 /*
1575 * Some errors are fatal and this list might need adjusting. For other
1576 * errors we try again until the max number of retries is reached.
1577 */
1578 if (result != -EHOSTUNREACH &&
1579 result != -ENETUNREACH &&
1580 result != -ENETDOWN &&
1581 result != -EINVAL &&
1582 result != -EPROTONOSUPPORT) {
1583 log_print("connect %d try %d error %d", con->nodeid,
1584 con->retries, result);
1585 msleep(1000);
1586 lowcomms_connect_sock(con);
1587 }
1588}
1589
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001590/* Send workqueue function */
1591static void process_send_sockets(struct work_struct *work)
1592{
1593 struct connection *con = container_of(work, struct connection, swork);
1594
Alexander Aring7443bc92021-05-21 15:08:36 -04001595 WARN_ON(test_bit(CF_IS_OTHERCON, &con->flags));
1596
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001597 clear_bit(CF_WRITE_PENDING, &con->flags);
Alexander Aringba868d92021-05-21 15:08:37 -04001598
Alexander Aring489d8e52021-05-21 15:08:46 -04001599 if (test_and_clear_bit(CF_RECONNECT, &con->flags)) {
Alexander Aringba868d92021-05-21 15:08:37 -04001600 close_connection(con, false, false, true);
Alexander Aring489d8e52021-05-21 15:08:46 -04001601 dlm_midcomms_unack_msg_resend(con->nodeid);
1602 }
Alexander Aringba868d92021-05-21 15:08:37 -04001603
Alexander Aring8728a452021-07-16 16:22:43 -04001604 if (con->sock == NULL) {
Alexander Aringba868d92021-05-21 15:08:37 -04001605 if (test_and_clear_bit(CF_DELAY_CONNECT, &con->flags))
1606 msleep(1000);
Alexander Aring8728a452021-07-16 16:22:43 -04001607
1608 mutex_lock(&con->sock_mutex);
1609 dlm_connect(con);
1610 mutex_unlock(&con->sock_mutex);
Alexander Aringba868d92021-05-21 15:08:37 -04001611 }
Alexander Aring8728a452021-07-16 16:22:43 -04001612
Bob Peterson01da24d2017-09-12 08:55:14 +00001613 if (!list_empty(&con->writequeue))
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -05001614 send_to_sock(con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001615}
1616
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001617static void work_stop(void)
1618{
Alexander Aringfcef0e62021-06-02 09:45:15 -04001619 if (recv_workqueue) {
David Windsorb3555162019-04-02 08:37:10 -04001620 destroy_workqueue(recv_workqueue);
Alexander Aringfcef0e62021-06-02 09:45:15 -04001621 recv_workqueue = NULL;
1622 }
1623
1624 if (send_workqueue) {
David Windsorb3555162019-04-02 08:37:10 -04001625 destroy_workqueue(send_workqueue);
Alexander Aringfcef0e62021-06-02 09:45:15 -04001626 send_workqueue = NULL;
1627 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001628}
1629
1630static int work_start(void)
1631{
Alexander Aring6c6a1cc2021-06-02 09:45:17 -04001632 recv_workqueue = alloc_ordered_workqueue("dlm_recv", WQ_MEM_RECLAIM);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001633 if (!recv_workqueue) {
1634 log_print("can't start dlm_recv");
1635 return -ENOMEM;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001636 }
1637
Alexander Aring6c6a1cc2021-06-02 09:45:17 -04001638 send_workqueue = alloc_ordered_workqueue("dlm_send", WQ_MEM_RECLAIM);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001639 if (!send_workqueue) {
1640 log_print("can't start dlm_send");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001641 destroy_workqueue(recv_workqueue);
Alexander Aringfcef0e62021-06-02 09:45:15 -04001642 recv_workqueue = NULL;
Namhyung Kimb9d41052010-12-13 13:42:24 -06001643 return -ENOMEM;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001644 }
1645
1646 return 0;
1647}
1648
Alexander Aring9d232462021-03-01 17:05:20 -05001649static void shutdown_conn(struct connection *con)
1650{
Alexander Aringa66c0082021-07-16 16:22:40 -04001651 if (dlm_proto_ops->shutdown_action)
1652 dlm_proto_ops->shutdown_action(con);
Alexander Aring9d232462021-03-01 17:05:20 -05001653}
1654
1655void dlm_lowcomms_shutdown(void)
1656{
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001657 int idx;
1658
Alexander Aring9d232462021-03-01 17:05:20 -05001659 /* Set all the flags to prevent any
1660 * socket activity.
1661 */
1662 dlm_allow_conn = 0;
1663
1664 if (recv_workqueue)
1665 flush_workqueue(recv_workqueue);
1666 if (send_workqueue)
1667 flush_workqueue(send_workqueue);
1668
1669 dlm_close_sock(&listen_con.sock);
1670
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001671 idx = srcu_read_lock(&connections_srcu);
Alexander Aring9d232462021-03-01 17:05:20 -05001672 foreach_conn(shutdown_conn);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001673 srcu_read_unlock(&connections_srcu, idx);
Alexander Aring9d232462021-03-01 17:05:20 -05001674}
1675
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001676static void _stop_conn(struct connection *con, bool and_other)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001677{
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001678 mutex_lock(&con->sock_mutex);
tsutomu.owa@toshiba.co.jp173a31f2017-09-12 09:01:24 +00001679 set_bit(CF_CLOSE, &con->flags);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001680 set_bit(CF_READ_PENDING, &con->flags);
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001681 set_bit(CF_WRITE_PENDING, &con->flags);
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +00001682 if (con->sock && con->sock->sk) {
1683 write_lock_bh(&con->sock->sk->sk_callback_lock);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001684 con->sock->sk->sk_user_data = NULL;
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +00001685 write_unlock_bh(&con->sock->sk->sk_callback_lock);
1686 }
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001687 if (con->othercon && and_other)
1688 _stop_conn(con->othercon, false);
1689 mutex_unlock(&con->sock_mutex);
1690}
1691
1692static void stop_conn(struct connection *con)
1693{
1694 _stop_conn(con, true);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001695}
1696
Alexander Aring4798cbb2020-09-24 10:31:26 -04001697static void connection_release(struct rcu_head *rcu)
1698{
1699 struct connection *con = container_of(rcu, struct connection, rcu);
1700
1701 kfree(con->rx_buf);
1702 kfree(con);
1703}
1704
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001705static void free_conn(struct connection *con)
1706{
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -03001707 close_connection(con, true, true, true);
Alexander Aringa47666eb2020-08-27 15:02:49 -04001708 spin_lock(&connections_lock);
1709 hlist_del_rcu(&con->list);
1710 spin_unlock(&connections_lock);
Alexander Aring948c47e2020-08-27 15:02:53 -04001711 if (con->othercon) {
1712 clean_one_writequeue(con->othercon);
Alexander Aring5cbec202020-11-02 20:04:16 -05001713 call_srcu(&connections_srcu, &con->othercon->rcu,
1714 connection_release);
Alexander Aring948c47e2020-08-27 15:02:53 -04001715 }
Alexander Aring0de98432020-08-27 15:02:52 -04001716 clean_one_writequeue(con);
Alexander Aring5cbec202020-11-02 20:04:16 -05001717 call_srcu(&connections_srcu, &con->rcu, connection_release);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001718}
1719
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001720static void work_flush(void)
1721{
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001722 int ok;
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001723 int i;
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001724 struct connection *con;
1725
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001726 do {
1727 ok = 1;
1728 foreach_conn(stop_conn);
David Windsorb3555162019-04-02 08:37:10 -04001729 if (recv_workqueue)
1730 flush_workqueue(recv_workqueue);
1731 if (send_workqueue)
1732 flush_workqueue(send_workqueue);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001733 for (i = 0; i < CONN_HASH_SIZE && ok; i++) {
Alexander Aringa47666eb2020-08-27 15:02:49 -04001734 hlist_for_each_entry_rcu(con, &connection_hash[i],
1735 list) {
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001736 ok &= test_bit(CF_READ_PENDING, &con->flags);
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001737 ok &= test_bit(CF_WRITE_PENDING, &con->flags);
1738 if (con->othercon) {
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001739 ok &= test_bit(CF_READ_PENDING,
1740 &con->othercon->flags);
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001741 ok &= test_bit(CF_WRITE_PENDING,
1742 &con->othercon->flags);
1743 }
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001744 }
1745 }
1746 } while (!ok);
1747}
1748
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001749void dlm_lowcomms_stop(void)
1750{
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001751 int idx;
1752
1753 idx = srcu_read_lock(&connections_srcu);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001754 work_flush();
Marcelo Ricardo Leitner3a8db792016-10-08 10:14:37 -03001755 foreach_conn(free_conn);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001756 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001757 work_stop();
Alexander Aring043697f2020-08-27 15:02:50 -04001758 deinit_local();
Alexander Aringa66c0082021-07-16 16:22:40 -04001759
1760 dlm_proto_ops = NULL;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001761}
1762
Alexander Aring2dc6b112021-07-16 16:22:41 -04001763static int dlm_listen_for_all(void)
1764{
1765 struct socket *sock;
1766 int result;
1767
1768 log_print("Using %s for communications",
1769 dlm_proto_ops->name);
1770
Alexander Aring90d21fc2021-07-16 16:22:42 -04001771 result = dlm_proto_ops->listen_validate();
1772 if (result < 0)
1773 return result;
Alexander Aring2dc6b112021-07-16 16:22:41 -04001774
1775 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1776 SOCK_STREAM, dlm_proto_ops->proto, &sock);
1777 if (result < 0) {
Alexander Aringfe933672021-11-02 15:17:10 -04001778 log_print("Can't create comms socket: %d", result);
Alexander Aring2dc6b112021-07-16 16:22:41 -04001779 goto out;
1780 }
1781
1782 sock_set_mark(sock->sk, dlm_config.ci_mark);
1783 dlm_proto_ops->listen_sockopts(sock);
1784
1785 result = dlm_proto_ops->listen_bind(sock);
1786 if (result < 0)
1787 goto out;
1788
1789 save_listen_callbacks(sock);
1790 add_listen_sock(sock, &listen_con);
1791
1792 INIT_WORK(&listen_con.rwork, process_listen_recv_socket);
1793 result = sock->ops->listen(sock, 5);
1794 if (result < 0) {
1795 dlm_close_sock(&listen_con.sock);
1796 goto out;
1797 }
1798
1799 return 0;
1800
1801out:
1802 sock_release(sock);
1803 return result;
1804}
1805
Alexander Aring8728a452021-07-16 16:22:43 -04001806static int dlm_tcp_bind(struct socket *sock)
1807{
1808 struct sockaddr_storage src_addr;
1809 int result, addr_len;
1810
1811 /* Bind to our cluster-known address connecting to avoid
1812 * routing problems.
1813 */
1814 memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
1815 make_sockaddr(&src_addr, 0, &addr_len);
1816
1817 result = sock->ops->bind(sock, (struct sockaddr *)&src_addr,
1818 addr_len);
1819 if (result < 0) {
1820 /* This *may* not indicate a critical error */
1821 log_print("could not bind for connect: %d", result);
1822 }
1823
1824 return 0;
1825}
1826
1827static int dlm_tcp_connect(struct connection *con, struct socket *sock,
1828 struct sockaddr *addr, int addr_len)
1829{
1830 int ret;
1831
1832 ret = sock->ops->connect(sock, addr, addr_len, O_NONBLOCK);
1833 switch (ret) {
1834 case -EINPROGRESS:
1835 fallthrough;
1836 case 0:
1837 return 0;
1838 }
1839
1840 return ret;
1841}
1842
Alexander Aring2dc6b112021-07-16 16:22:41 -04001843static int dlm_tcp_listen_validate(void)
1844{
1845 /* We don't support multi-homed hosts */
1846 if (dlm_local_count > 1) {
1847 log_print("TCP protocol can't handle multi-homed hosts, try SCTP");
1848 return -EINVAL;
1849 }
1850
1851 return 0;
1852}
1853
1854static void dlm_tcp_sockopts(struct socket *sock)
1855{
1856 /* Turn off Nagle's algorithm */
1857 tcp_sock_set_nodelay(sock->sk);
1858}
1859
1860static void dlm_tcp_listen_sockopts(struct socket *sock)
1861{
1862 dlm_tcp_sockopts(sock);
1863 sock_set_reuseaddr(sock->sk);
1864}
1865
1866static int dlm_tcp_listen_bind(struct socket *sock)
1867{
1868 int addr_len;
1869
1870 /* Bind to our port */
1871 make_sockaddr(dlm_local_addr[0], dlm_config.ci_tcp_port, &addr_len);
1872 return sock->ops->bind(sock, (struct sockaddr *)dlm_local_addr[0],
1873 addr_len);
1874}
1875
Alexander Aringa66c0082021-07-16 16:22:40 -04001876static const struct dlm_proto_ops dlm_tcp_ops = {
Alexander Aring2dc6b112021-07-16 16:22:41 -04001877 .name = "TCP",
1878 .proto = IPPROTO_TCP,
Alexander Aring8728a452021-07-16 16:22:43 -04001879 .connect = dlm_tcp_connect,
1880 .sockopts = dlm_tcp_sockopts,
1881 .bind = dlm_tcp_bind,
Alexander Aring2dc6b112021-07-16 16:22:41 -04001882 .listen_validate = dlm_tcp_listen_validate,
1883 .listen_sockopts = dlm_tcp_listen_sockopts,
1884 .listen_bind = dlm_tcp_listen_bind,
Alexander Aringa66c0082021-07-16 16:22:40 -04001885 .shutdown_action = dlm_tcp_shutdown,
1886 .eof_condition = tcp_eof_condition,
1887};
1888
Alexander Aring8728a452021-07-16 16:22:43 -04001889static int dlm_sctp_bind(struct socket *sock)
1890{
1891 return sctp_bind_addrs(sock, 0);
1892}
1893
1894static int dlm_sctp_connect(struct connection *con, struct socket *sock,
1895 struct sockaddr *addr, int addr_len)
1896{
1897 int ret;
1898
1899 /*
1900 * Make sock->ops->connect() function return in specified time,
1901 * since O_NONBLOCK argument in connect() function does not work here,
1902 * then, we should restore the default value of this attribute.
1903 */
1904 sock_set_sndtimeo(sock->sk, 5);
1905 ret = sock->ops->connect(sock, addr, addr_len, 0);
1906 sock_set_sndtimeo(sock->sk, 0);
1907 if (ret < 0)
1908 return ret;
1909
1910 if (!test_and_set_bit(CF_CONNECTED, &con->flags))
1911 log_print("successful connected to node %d", con->nodeid);
1912
1913 return 0;
1914}
1915
Alexander Aring90d21fc2021-07-16 16:22:42 -04001916static int dlm_sctp_listen_validate(void)
1917{
1918 if (!IS_ENABLED(CONFIG_IP_SCTP)) {
1919 log_print("SCTP is not enabled by this kernel");
1920 return -EOPNOTSUPP;
1921 }
1922
1923 request_module("sctp");
1924 return 0;
1925}
1926
Alexander Aring2dc6b112021-07-16 16:22:41 -04001927static int dlm_sctp_bind_listen(struct socket *sock)
1928{
1929 return sctp_bind_addrs(sock, dlm_config.ci_tcp_port);
1930}
1931
1932static void dlm_sctp_sockopts(struct socket *sock)
1933{
1934 /* Turn off Nagle's algorithm */
1935 sctp_sock_set_nodelay(sock->sk);
1936 sock_set_rcvbuf(sock->sk, NEEDED_RMEM);
1937}
1938
Alexander Aringa66c0082021-07-16 16:22:40 -04001939static const struct dlm_proto_ops dlm_sctp_ops = {
Alexander Aring2dc6b112021-07-16 16:22:41 -04001940 .name = "SCTP",
1941 .proto = IPPROTO_SCTP,
Alexander Aring8728a452021-07-16 16:22:43 -04001942 .try_new_addr = true,
1943 .connect = dlm_sctp_connect,
1944 .sockopts = dlm_sctp_sockopts,
1945 .bind = dlm_sctp_bind,
Alexander Aring90d21fc2021-07-16 16:22:42 -04001946 .listen_validate = dlm_sctp_listen_validate,
Alexander Aring2dc6b112021-07-16 16:22:41 -04001947 .listen_sockopts = dlm_sctp_sockopts,
1948 .listen_bind = dlm_sctp_bind_listen,
Alexander Aringa66c0082021-07-16 16:22:40 -04001949};
1950
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001951int dlm_lowcomms_start(void)
1952{
1953 int error = -EINVAL;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001954 int i;
1955
1956 for (i = 0; i < CONN_HASH_SIZE; i++)
1957 INIT_HLIST_HEAD(&connection_hash[i]);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001958
1959 init_local();
1960 if (!dlm_local_count) {
David Teigland617e82e2007-04-26 13:46:49 -05001961 error = -ENOTCONN;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001962 log_print("no local IP address has been set");
David Teigland513ef592012-03-30 11:46:08 -05001963 goto fail;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001964 }
1965
Alexander Aringd11ccd42020-11-02 20:04:25 -05001966 INIT_WORK(&listen_con.rwork, process_listen_recv_socket);
1967
David Teigland513ef592012-03-30 11:46:08 -05001968 error = work_start();
1969 if (error)
Alexander Aringfcef0e62021-06-02 09:45:15 -04001970 goto fail_local;
David Teigland513ef592012-03-30 11:46:08 -05001971
1972 dlm_allow_conn = 1;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001973
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001974 /* Start listening */
Alexander Aringac7d5d02021-06-02 09:45:19 -04001975 switch (dlm_config.ci_protocol) {
1976 case DLM_PROTO_TCP:
Alexander Aringa66c0082021-07-16 16:22:40 -04001977 dlm_proto_ops = &dlm_tcp_ops;
Alexander Aringac7d5d02021-06-02 09:45:19 -04001978 break;
1979 case DLM_PROTO_SCTP:
Alexander Aringa66c0082021-07-16 16:22:40 -04001980 dlm_proto_ops = &dlm_sctp_ops;
Alexander Aringac7d5d02021-06-02 09:45:19 -04001981 break;
1982 default:
1983 log_print("Invalid protocol identifier %d set",
1984 dlm_config.ci_protocol);
1985 error = -EINVAL;
Alexander Aring2dc6b112021-07-16 16:22:41 -04001986 goto fail_proto_ops;
Alexander Aringac7d5d02021-06-02 09:45:19 -04001987 }
Alexander Aring2dc6b112021-07-16 16:22:41 -04001988
1989 error = dlm_listen_for_all();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001990 if (error)
Alexander Aring2dc6b112021-07-16 16:22:41 -04001991 goto fail_listen;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001992
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001993 return 0;
1994
Alexander Aring2dc6b112021-07-16 16:22:41 -04001995fail_listen:
1996 dlm_proto_ops = NULL;
1997fail_proto_ops:
David Teigland513ef592012-03-30 11:46:08 -05001998 dlm_allow_conn = 0;
Alexander Aringd11ccd42020-11-02 20:04:25 -05001999 dlm_close_sock(&listen_con.sock);
Alexander Aringfcef0e62021-06-02 09:45:15 -04002000 work_stop();
2001fail_local:
2002 deinit_local();
David Teigland513ef592012-03-30 11:46:08 -05002003fail:
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01002004 return error;
2005}
David Teigland36b71a82012-07-26 12:44:30 -05002006
2007void dlm_lowcomms_exit(void)
2008{
2009 struct dlm_node_addr *na, *safe;
2010
2011 spin_lock(&dlm_node_addrs_spin);
2012 list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) {
2013 list_del(&na->list);
2014 while (na->addr_count--)
2015 kfree(na->addr[na->addr_count]);
2016 kfree(na);
2017 }
2018 spin_unlock(&dlm_node_addrs_spin);
2019}