blob: 6d500ebc614533551f0f609342bb890761741fea [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);
Alexander Aringbcbfea42021-11-30 14:47:15 -0500204 /* if len is zero nothing is to send, if there are users filling
205 * buffers we wait until the users are done so we can send more.
206 */
207 if (e->users || e->len == 0)
Alexander Aring66d59552021-07-16 16:22:39 -0400208 return NULL;
209
210 return e;
211}
212
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400213static struct connection *__find_con(int nodeid, int r)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600214{
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600215 struct connection *con;
216
Alexander Aringa47666eb2020-08-27 15:02:49 -0400217 hlist_for_each_entry_rcu(con, &connection_hash[r], list) {
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400218 if (con->nodeid == nodeid)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600219 return con;
220 }
Alexander Aringa47666eb2020-08-27 15:02:49 -0400221
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600222 return NULL;
223}
224
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400225static bool tcp_eof_condition(struct connection *con)
226{
227 return atomic_read(&con->writequeue_cnt);
228}
229
Alexander Aring6cde2102020-11-02 20:04:21 -0500230static int dlm_con_init(struct connection *con, int nodeid)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100231{
Alexander Aring4798cbb2020-09-24 10:31:26 -0400232 con->rx_buflen = dlm_config.ci_buffer_size;
233 con->rx_buf = kmalloc(con->rx_buflen, GFP_NOFS);
Alexander Aring6cde2102020-11-02 20:04:21 -0500234 if (!con->rx_buf)
235 return -ENOMEM;
Alexander Aring4798cbb2020-09-24 10:31:26 -0400236
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100237 con->nodeid = nodeid;
238 mutex_init(&con->sock_mutex);
239 INIT_LIST_HEAD(&con->writequeue);
240 spin_lock_init(&con->writequeue_lock);
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400241 atomic_set(&con->writequeue_cnt, 0);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100242 INIT_WORK(&con->swork, process_send_sockets);
243 INIT_WORK(&con->rwork, process_recv_sockets);
Alexander Aring055923b2020-07-27 09:13:38 -0400244 init_waitqueue_head(&con->shutdown_wait);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100245
Alexander Aring6cde2102020-11-02 20:04:21 -0500246 return 0;
247}
248
249/*
250 * If 'allocation' is zero then we don't attempt to create a new
251 * connection structure for this node.
252 */
253static struct connection *nodeid2con(int nodeid, gfp_t alloc)
254{
255 struct connection *con, *tmp;
256 int r, ret;
257
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400258 r = nodeid_hash(nodeid);
259 con = __find_con(nodeid, r);
Alexander Aring6cde2102020-11-02 20:04:21 -0500260 if (con || !alloc)
261 return con;
262
263 con = kzalloc(sizeof(*con), alloc);
264 if (!con)
265 return NULL;
266
267 ret = dlm_con_init(con, nodeid);
268 if (ret) {
269 kfree(con);
270 return NULL;
271 }
272
Alexander Aringc51b0222021-07-16 16:22:44 -0400273 mutex_init(&con->wq_alloc);
274
Alexander Aringa47666eb2020-08-27 15:02:49 -0400275 spin_lock(&connections_lock);
Alexander Aring4f2b30f2020-09-30 18:37:29 -0400276 /* Because multiple workqueues/threads calls this function it can
277 * race on multiple cpu's. Instead of locking hot path __find_con()
278 * we just check in rare cases of recently added nodes again
279 * under protection of connections_lock. If this is the case we
280 * abort our connection creation and return the existing connection.
281 */
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400282 tmp = __find_con(nodeid, r);
Alexander Aring4f2b30f2020-09-30 18:37:29 -0400283 if (tmp) {
284 spin_unlock(&connections_lock);
285 kfree(con->rx_buf);
286 kfree(con);
287 return tmp;
288 }
289
Alexander Aringa47666eb2020-08-27 15:02:49 -0400290 hlist_add_head_rcu(&con->list, &connection_hash[r]);
291 spin_unlock(&connections_lock);
292
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100293 return con;
294}
295
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600296/* Loop round all connections */
297static void foreach_conn(void (*conn_func)(struct connection *c))
298{
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400299 int i;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600300 struct connection *con;
301
302 for (i = 0; i < CONN_HASH_SIZE; i++) {
Alexander Aringa47666eb2020-08-27 15:02:49 -0400303 hlist_for_each_entry_rcu(con, &connection_hash[i], list)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600304 conn_func(con);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600305 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100306}
307
David Teigland36b71a82012-07-26 12:44:30 -0500308static struct dlm_node_addr *find_node_addr(int nodeid)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100309{
David Teigland36b71a82012-07-26 12:44:30 -0500310 struct dlm_node_addr *na;
311
312 list_for_each_entry(na, &dlm_node_addrs, list) {
313 if (na->nodeid == nodeid)
314 return na;
315 }
316 return NULL;
317}
318
Alexander Aring40c6b832020-11-02 20:04:27 -0500319static int addr_compare(const struct sockaddr_storage *x,
320 const struct sockaddr_storage *y)
David Teigland36b71a82012-07-26 12:44:30 -0500321{
322 switch (x->ss_family) {
323 case AF_INET: {
324 struct sockaddr_in *sinx = (struct sockaddr_in *)x;
325 struct sockaddr_in *siny = (struct sockaddr_in *)y;
326 if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
327 return 0;
328 if (sinx->sin_port != siny->sin_port)
329 return 0;
330 break;
331 }
332 case AF_INET6: {
333 struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
334 struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
335 if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
336 return 0;
337 if (sinx->sin6_port != siny->sin6_port)
338 return 0;
339 break;
340 }
341 default:
342 return 0;
343 }
344 return 1;
345}
346
347static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out,
Alexander Aringe125fbe2021-03-01 17:05:09 -0500348 struct sockaddr *sa_out, bool try_new_addr,
349 unsigned int *mark)
David Teigland36b71a82012-07-26 12:44:30 -0500350{
351 struct sockaddr_storage sas;
352 struct dlm_node_addr *na;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100353
354 if (!dlm_local_count)
355 return -1;
356
David Teigland36b71a82012-07-26 12:44:30 -0500357 spin_lock(&dlm_node_addrs_spin);
358 na = find_node_addr(nodeid);
Mike Christie98e1b602013-06-14 04:56:12 -0500359 if (na && na->addr_count) {
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300360 memcpy(&sas, na->addr[na->curr_addr_index],
361 sizeof(struct sockaddr_storage));
362
Mike Christie98e1b602013-06-14 04:56:12 -0500363 if (try_new_addr) {
364 na->curr_addr_index++;
365 if (na->curr_addr_index == na->addr_count)
366 na->curr_addr_index = 0;
367 }
Mike Christie98e1b602013-06-14 04:56:12 -0500368 }
David Teigland36b71a82012-07-26 12:44:30 -0500369 spin_unlock(&dlm_node_addrs_spin);
370
371 if (!na)
372 return -EEXIST;
373
374 if (!na->addr_count)
375 return -ENOENT;
376
Alexander Aringe125fbe2021-03-01 17:05:09 -0500377 *mark = na->mark;
378
David Teigland36b71a82012-07-26 12:44:30 -0500379 if (sas_out)
380 memcpy(sas_out, &sas, sizeof(struct sockaddr_storage));
381
382 if (!sa_out)
383 return 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100384
385 if (dlm_local_addr[0]->ss_family == AF_INET) {
David Teigland36b71a82012-07-26 12:44:30 -0500386 struct sockaddr_in *in4 = (struct sockaddr_in *) &sas;
387 struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100388 ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
389 } else {
David Teigland36b71a82012-07-26 12:44:30 -0500390 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sas;
391 struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000392 ret6->sin6_addr = in6->sin6_addr;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100393 }
394
395 return 0;
396}
397
Alexander Aringe125fbe2021-03-01 17:05:09 -0500398static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid,
399 unsigned int *mark)
David Teigland36b71a82012-07-26 12:44:30 -0500400{
401 struct dlm_node_addr *na;
402 int rv = -EEXIST;
Mike Christie98e1b602013-06-14 04:56:12 -0500403 int addr_i;
David Teigland36b71a82012-07-26 12:44:30 -0500404
405 spin_lock(&dlm_node_addrs_spin);
406 list_for_each_entry(na, &dlm_node_addrs, list) {
407 if (!na->addr_count)
408 continue;
409
Mike Christie98e1b602013-06-14 04:56:12 -0500410 for (addr_i = 0; addr_i < na->addr_count; addr_i++) {
411 if (addr_compare(na->addr[addr_i], addr)) {
412 *nodeid = na->nodeid;
Alexander Aringe125fbe2021-03-01 17:05:09 -0500413 *mark = na->mark;
Mike Christie98e1b602013-06-14 04:56:12 -0500414 rv = 0;
415 goto unlock;
416 }
417 }
David Teigland36b71a82012-07-26 12:44:30 -0500418 }
Mike Christie98e1b602013-06-14 04:56:12 -0500419unlock:
David Teigland36b71a82012-07-26 12:44:30 -0500420 spin_unlock(&dlm_node_addrs_spin);
421 return rv;
422}
423
Alexander Aring4f19d072020-11-02 20:04:28 -0500424/* caller need to held dlm_node_addrs_spin lock */
425static bool dlm_lowcomms_na_has_addr(const struct dlm_node_addr *na,
426 const struct sockaddr_storage *addr)
427{
428 int i;
429
430 for (i = 0; i < na->addr_count; i++) {
431 if (addr_compare(na->addr[i], addr))
432 return true;
433 }
434
435 return false;
436}
437
David Teigland36b71a82012-07-26 12:44:30 -0500438int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr, int len)
439{
440 struct sockaddr_storage *new_addr;
441 struct dlm_node_addr *new_node, *na;
Alexander Aring4f19d072020-11-02 20:04:28 -0500442 bool ret;
David Teigland36b71a82012-07-26 12:44:30 -0500443
444 new_node = kzalloc(sizeof(struct dlm_node_addr), GFP_NOFS);
445 if (!new_node)
446 return -ENOMEM;
447
448 new_addr = kzalloc(sizeof(struct sockaddr_storage), GFP_NOFS);
449 if (!new_addr) {
450 kfree(new_node);
451 return -ENOMEM;
452 }
453
454 memcpy(new_addr, addr, len);
455
456 spin_lock(&dlm_node_addrs_spin);
457 na = find_node_addr(nodeid);
458 if (!na) {
459 new_node->nodeid = nodeid;
460 new_node->addr[0] = new_addr;
461 new_node->addr_count = 1;
Alexander Aringe125fbe2021-03-01 17:05:09 -0500462 new_node->mark = dlm_config.ci_mark;
David Teigland36b71a82012-07-26 12:44:30 -0500463 list_add(&new_node->list, &dlm_node_addrs);
464 spin_unlock(&dlm_node_addrs_spin);
465 return 0;
466 }
467
Alexander Aring4f19d072020-11-02 20:04:28 -0500468 ret = dlm_lowcomms_na_has_addr(na, addr);
469 if (ret) {
470 spin_unlock(&dlm_node_addrs_spin);
471 kfree(new_addr);
472 kfree(new_node);
473 return -EEXIST;
474 }
475
David Teigland36b71a82012-07-26 12:44:30 -0500476 if (na->addr_count >= DLM_MAX_ADDR_COUNT) {
477 spin_unlock(&dlm_node_addrs_spin);
478 kfree(new_addr);
479 kfree(new_node);
480 return -ENOSPC;
481 }
482
483 na->addr[na->addr_count++] = new_addr;
484 spin_unlock(&dlm_node_addrs_spin);
485 kfree(new_node);
486 return 0;
487}
488
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100489/* Data available on socket or listen socket received a connect */
David S. Miller676d2362014-04-11 16:15:36 -0400490static void lowcomms_data_ready(struct sock *sk)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100491{
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000492 struct connection *con;
493
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000494 con = sock2con(sk);
Patrick Caulfieldafb853f2007-06-01 10:07:26 -0500495 if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100496 queue_work(recv_workqueue, &con->rwork);
497}
498
Alexander Aringd11ccd42020-11-02 20:04:25 -0500499static void lowcomms_listen_data_ready(struct sock *sk)
500{
Alexander Aring9a4139a2021-06-02 09:45:18 -0400501 if (!dlm_allow_conn)
502 return;
503
Alexander Aringd11ccd42020-11-02 20:04:25 -0500504 queue_work(recv_workqueue, &listen_con.rwork);
505}
506
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100507static void lowcomms_write_space(struct sock *sk)
508{
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000509 struct connection *con;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100510
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000511 con = sock2con(sk);
David Millerb36930d2010-11-10 21:56:39 -0800512 if (!con)
Alexander Aring92c44602021-11-15 08:57:06 -0500513 return;
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);
Alexander Aring92c44602021-11-15 08:57:06 -0500518 return;
Alexander Aring19633c72020-11-02 20:04:20 -0500519 }
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);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100529}
530
531static inline void lowcomms_connect_sock(struct connection *con)
532{
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -0500533 if (test_bit(CF_CLOSE, &con->flags))
534 return;
Bob Peterson61d9102b2017-09-12 08:55:04 +0000535 queue_work(send_workqueue, &con->swork);
536 cond_resched();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100537}
538
539static void lowcomms_state_change(struct sock *sk)
540{
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300541 /* SCTP layer is not calling sk_data_ready when the connection
542 * is done, so we catch the signal through here. Also, it
543 * doesn't switch socket state when entering shutdown, so we
544 * skip the write in that case.
545 */
546 if (sk->sk_shutdown) {
547 if (sk->sk_shutdown == RCV_SHUTDOWN)
548 lowcomms_data_ready(sk);
549 } else if (sk->sk_state == TCP_ESTABLISHED) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100550 lowcomms_write_space(sk);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300551 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100552}
553
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500554int dlm_lowcomms_connect_node(int nodeid)
555{
556 struct connection *con;
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400557 int idx;
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500558
559 if (nodeid == dlm_our_nodeid())
560 return 0;
561
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400562 idx = srcu_read_lock(&connections_srcu);
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500563 con = nodeid2con(nodeid, GFP_NOFS);
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400564 if (!con) {
565 srcu_read_unlock(&connections_srcu, idx);
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500566 return -ENOMEM;
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400567 }
568
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500569 lowcomms_connect_sock(con);
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400570 srcu_read_unlock(&connections_srcu, idx);
571
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500572 return 0;
573}
574
Alexander Aringe125fbe2021-03-01 17:05:09 -0500575int dlm_lowcomms_nodes_set_mark(int nodeid, unsigned int mark)
576{
577 struct dlm_node_addr *na;
578
579 spin_lock(&dlm_node_addrs_spin);
580 na = find_node_addr(nodeid);
581 if (!na) {
582 spin_unlock(&dlm_node_addrs_spin);
583 return -ENOENT;
584 }
585
586 na->mark = mark;
587 spin_unlock(&dlm_node_addrs_spin);
588
589 return 0;
590}
591
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500592static void lowcomms_error_report(struct sock *sk)
593{
Bob Petersonb81171c2016-02-05 14:39:02 -0500594 struct connection *con;
Bob Petersonb81171c2016-02-05 14:39:02 -0500595 void (*orig_report)(struct sock *) = NULL;
Alexander Aring4c3d9052021-11-15 08:57:05 -0500596 struct inet_sock *inet;
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500597
Bob Petersonb81171c2016-02-05 14:39:02 -0500598 con = sock2con(sk);
599 if (con == NULL)
600 goto out;
601
Bob Petersoncc661fc2017-09-12 08:55:23 +0000602 orig_report = listen_sock.sk_error_report;
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500603
Alexander Aring4c3d9052021-11-15 08:57:05 -0500604 inet = inet_sk(sk);
605 switch (sk->sk_family) {
606 case AF_INET:
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500607 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
Alexander Aring4c3d9052021-11-15 08:57:05 -0500608 "sending to node %d at %pI4, dport %d, "
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500609 "sk_err=%d/%d\n", dlm_our_nodeid(),
Alexander Aring4c3d9052021-11-15 08:57:05 -0500610 con->nodeid, &inet->inet_daddr,
611 ntohs(inet->inet_dport), sk->sk_err,
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500612 sk->sk_err_soft);
Alexander Aring4c3d9052021-11-15 08:57:05 -0500613 break;
Alexander Aring1b9beda2021-11-17 09:20:43 -0500614#if IS_ENABLED(CONFIG_IPV6)
Alexander Aring4c3d9052021-11-15 08:57:05 -0500615 case AF_INET6:
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500616 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
Alexander Aring4c3d9052021-11-15 08:57:05 -0500617 "sending to node %d at %pI6c, "
618 "dport %d, sk_err=%d/%d\n", dlm_our_nodeid(),
619 con->nodeid, &sk->sk_v6_daddr,
620 ntohs(inet->inet_dport), sk->sk_err,
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500621 sk->sk_err_soft);
Alexander Aring4c3d9052021-11-15 08:57:05 -0500622 break;
Alexander Aring1b9beda2021-11-17 09:20:43 -0500623#endif
Alexander Aring4c3d9052021-11-15 08:57:05 -0500624 default:
625 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
626 "invalid socket family %d set, "
627 "sk_err=%d/%d\n", dlm_our_nodeid(),
628 sk->sk_family, sk->sk_err, sk->sk_err_soft);
629 goto out;
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500630 }
Alexander Aringba868d92021-05-21 15:08:37 -0400631
632 /* below sendcon only handling */
633 if (test_bit(CF_IS_OTHERCON, &con->flags))
634 con = con->sendcon;
635
636 switch (sk->sk_err) {
637 case ECONNREFUSED:
638 set_bit(CF_DELAY_CONNECT, &con->flags);
639 break;
640 default:
641 break;
642 }
643
644 if (!test_and_set_bit(CF_RECONNECT, &con->flags))
645 queue_work(send_workqueue, &con->swork);
646
Bob Petersonb81171c2016-02-05 14:39:02 -0500647out:
Bob Petersonb81171c2016-02-05 14:39:02 -0500648 if (orig_report)
649 orig_report(sk);
650}
651
652/* Note: sk_callback_lock must be locked before calling this function. */
Bob Petersoncc661fc2017-09-12 08:55:23 +0000653static void save_listen_callbacks(struct socket *sock)
Bob Petersonb81171c2016-02-05 14:39:02 -0500654{
Bob Petersoncc661fc2017-09-12 08:55:23 +0000655 struct sock *sk = sock->sk;
656
657 listen_sock.sk_data_ready = sk->sk_data_ready;
658 listen_sock.sk_state_change = sk->sk_state_change;
659 listen_sock.sk_write_space = sk->sk_write_space;
660 listen_sock.sk_error_report = sk->sk_error_report;
Bob Petersonb81171c2016-02-05 14:39:02 -0500661}
662
Bob Petersoncc661fc2017-09-12 08:55:23 +0000663static void restore_callbacks(struct socket *sock)
Bob Petersonb81171c2016-02-05 14:39:02 -0500664{
Bob Petersoncc661fc2017-09-12 08:55:23 +0000665 struct sock *sk = sock->sk;
666
Alexander Aring92c44602021-11-15 08:57:06 -0500667 lock_sock(sk);
Bob Petersonb81171c2016-02-05 14:39:02 -0500668 sk->sk_user_data = NULL;
Bob Petersoncc661fc2017-09-12 08:55:23 +0000669 sk->sk_data_ready = listen_sock.sk_data_ready;
670 sk->sk_state_change = listen_sock.sk_state_change;
671 sk->sk_write_space = listen_sock.sk_write_space;
672 sk->sk_error_report = listen_sock.sk_error_report;
Alexander Aring92c44602021-11-15 08:57:06 -0500673 release_sock(sk);
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500674}
675
Alexander Aringd11ccd42020-11-02 20:04:25 -0500676static void add_listen_sock(struct socket *sock, struct listen_connection *con)
677{
678 struct sock *sk = sock->sk;
679
Alexander Aring92c44602021-11-15 08:57:06 -0500680 lock_sock(sk);
Alexander Aringd11ccd42020-11-02 20:04:25 -0500681 save_listen_callbacks(sock);
682 con->sock = sock;
683
684 sk->sk_user_data = con;
685 sk->sk_allocation = GFP_NOFS;
686 /* Install a data_ready callback */
687 sk->sk_data_ready = lowcomms_listen_data_ready;
Alexander Aring92c44602021-11-15 08:57:06 -0500688 release_sock(sk);
Alexander Aringd11ccd42020-11-02 20:04:25 -0500689}
690
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100691/* Make a socket active */
tsutomu.owa@toshiba.co.jp988419a2017-09-12 08:55:32 +0000692static void add_sock(struct socket *sock, struct connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100693{
Bob Petersonb81171c2016-02-05 14:39:02 -0500694 struct sock *sk = sock->sk;
695
Alexander Aring92c44602021-11-15 08:57:06 -0500696 lock_sock(sk);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100697 con->sock = sock;
698
Bob Petersonb81171c2016-02-05 14:39:02 -0500699 sk->sk_user_data = con;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100700 /* Install a data_ready callback */
Bob Petersonb81171c2016-02-05 14:39:02 -0500701 sk->sk_data_ready = lowcomms_data_ready;
702 sk->sk_write_space = lowcomms_write_space;
703 sk->sk_state_change = lowcomms_state_change;
704 sk->sk_allocation = GFP_NOFS;
705 sk->sk_error_report = lowcomms_error_report;
Alexander Aring92c44602021-11-15 08:57:06 -0500706 release_sock(sk);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100707}
708
709/* Add the port number to an IPv6 or 4 sockaddr and return the address
710 length */
711static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
712 int *addr_len)
713{
714 saddr->ss_family = dlm_local_addr[0]->ss_family;
715 if (saddr->ss_family == AF_INET) {
716 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
717 in4_addr->sin_port = cpu_to_be16(port);
718 *addr_len = sizeof(struct sockaddr_in);
719 memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
720 } else {
721 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
722 in6_addr->sin6_port = cpu_to_be16(port);
723 *addr_len = sizeof(struct sockaddr_in6);
724 }
Patrick Caulfield01c8cab2007-07-17 16:53:15 +0100725 memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100726}
727
Alexander Aring706474f2021-05-21 15:08:48 -0400728static void dlm_page_release(struct kref *kref)
729{
730 struct writequeue_entry *e = container_of(kref, struct writequeue_entry,
731 ref);
732
733 __free_page(e->page);
734 kfree(e);
735}
736
737static void dlm_msg_release(struct kref *kref)
738{
739 struct dlm_msg *msg = container_of(kref, struct dlm_msg, ref);
740
741 kref_put(&msg->entry->ref, dlm_page_release);
742 kfree(msg);
743}
744
745static void free_entry(struct writequeue_entry *e)
746{
747 struct dlm_msg *msg, *tmp;
748
749 list_for_each_entry_safe(msg, tmp, &e->msgs, list) {
750 if (msg->orig_msg) {
751 msg->orig_msg->retransmit = false;
752 kref_put(&msg->orig_msg->ref, dlm_msg_release);
753 }
754
755 list_del(&msg->list);
756 kref_put(&msg->ref, dlm_msg_release);
757 }
758
759 list_del(&e->list);
760 atomic_dec(&e->con->writequeue_cnt);
761 kref_put(&e->ref, dlm_page_release);
762}
763
Alexander Aringd11ccd42020-11-02 20:04:25 -0500764static void dlm_close_sock(struct socket **sock)
765{
766 if (*sock) {
767 restore_callbacks(*sock);
768 sock_release(*sock);
769 *sock = NULL;
770 }
771}
772
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100773/* Close a remote connection and tidy up */
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300774static void close_connection(struct connection *con, bool and_other,
775 bool tx, bool rx)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100776{
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +0000777 bool closing = test_and_set_bit(CF_CLOSING, &con->flags);
Alexander Aring706474f2021-05-21 15:08:48 -0400778 struct writequeue_entry *e;
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +0000779
tsutomu.owa@toshiba.co.jp0aa18462017-09-12 09:02:02 +0000780 if (tx && !closing && cancel_work_sync(&con->swork)) {
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300781 log_print("canceled swork for node %d", con->nodeid);
tsutomu.owa@toshiba.co.jp0aa18462017-09-12 09:02:02 +0000782 clear_bit(CF_WRITE_PENDING, &con->flags);
783 }
784 if (rx && !closing && cancel_work_sync(&con->rwork)) {
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300785 log_print("canceled rwork for node %d", con->nodeid);
tsutomu.owa@toshiba.co.jp0aa18462017-09-12 09:02:02 +0000786 clear_bit(CF_READ_PENDING, &con->flags);
787 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100788
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300789 mutex_lock(&con->sock_mutex);
Alexander Aringd11ccd42020-11-02 20:04:25 -0500790 dlm_close_sock(&con->sock);
791
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100792 if (con->othercon && and_other) {
793 /* Will only re-enter once. */
Alexander Aringc6aa00e32021-05-21 15:08:38 -0400794 close_connection(con->othercon, false, tx, rx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100795 }
Patrick Caulfield9e5f2822007-08-02 14:58:14 +0100796
Alexander Aring706474f2021-05-21 15:08:48 -0400797 /* if we send a writequeue entry only a half way, we drop the
798 * whole entry because reconnection and that we not start of the
799 * middle of a msg which will confuse the other end.
800 *
801 * we can always drop messages because retransmits, but what we
802 * cannot allow is to transmit half messages which may be processed
803 * at the other side.
804 *
805 * our policy is to start on a clean state when disconnects, we don't
806 * know what's send/received on transport layer in this case.
807 */
808 spin_lock(&con->writequeue_lock);
809 if (!list_empty(&con->writequeue)) {
810 e = list_first_entry(&con->writequeue, struct writequeue_entry,
811 list);
812 if (e->dirty)
813 free_entry(e);
814 }
815 spin_unlock(&con->writequeue_lock);
816
Alexander Aring4798cbb2020-09-24 10:31:26 -0400817 con->rx_leftover = 0;
Patrick Caulfield61d96be02007-08-20 15:13:38 +0100818 con->retries = 0;
Alexander Aring052849b2021-07-16 16:22:37 -0400819 clear_bit(CF_APP_LIMITED, &con->flags);
Alexander Aring19633c72020-11-02 20:04:20 -0500820 clear_bit(CF_CONNECTED, &con->flags);
Alexander Aringba868d92021-05-21 15:08:37 -0400821 clear_bit(CF_DELAY_CONNECT, &con->flags);
822 clear_bit(CF_RECONNECT, &con->flags);
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400823 clear_bit(CF_EOF, &con->flags);
Patrick Caulfield61d96be02007-08-20 15:13:38 +0100824 mutex_unlock(&con->sock_mutex);
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +0000825 clear_bit(CF_CLOSING, &con->flags);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100826}
827
Alexander Aring055923b2020-07-27 09:13:38 -0400828static void shutdown_connection(struct connection *con)
829{
830 int ret;
831
Alexander Aringeec054b2021-03-01 17:05:19 -0500832 flush_work(&con->swork);
Alexander Aring055923b2020-07-27 09:13:38 -0400833
834 mutex_lock(&con->sock_mutex);
835 /* nothing to shutdown */
836 if (!con->sock) {
837 mutex_unlock(&con->sock_mutex);
838 return;
839 }
840
841 set_bit(CF_SHUTDOWN, &con->flags);
842 ret = kernel_sock_shutdown(con->sock, SHUT_WR);
843 mutex_unlock(&con->sock_mutex);
844 if (ret) {
845 log_print("Connection %p failed to shutdown: %d will force close",
846 con, ret);
847 goto force_close;
848 } else {
849 ret = wait_event_timeout(con->shutdown_wait,
850 !test_bit(CF_SHUTDOWN, &con->flags),
851 DLM_SHUTDOWN_WAIT_TIMEOUT);
852 if (ret == 0) {
853 log_print("Connection %p shutdown timed out, will force close",
854 con);
855 goto force_close;
856 }
857 }
858
859 return;
860
861force_close:
862 clear_bit(CF_SHUTDOWN, &con->flags);
863 close_connection(con, false, true, true);
864}
865
866static void dlm_tcp_shutdown(struct connection *con)
867{
868 if (con->othercon)
869 shutdown_connection(con->othercon);
870 shutdown_connection(con);
871}
872
Alexander Aring4798cbb2020-09-24 10:31:26 -0400873static int con_realloc_receive_buf(struct connection *con, int newlen)
874{
875 unsigned char *newbuf;
876
877 newbuf = kmalloc(newlen, GFP_NOFS);
878 if (!newbuf)
879 return -ENOMEM;
880
881 /* copy any leftover from last receive */
882 if (con->rx_leftover)
883 memmove(newbuf, con->rx_buf, con->rx_leftover);
884
885 /* swap to new buffer space */
886 kfree(con->rx_buf);
887 con->rx_buflen = newlen;
888 con->rx_buf = newbuf;
889
890 return 0;
891}
892
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100893/* Data received from remote end */
894static int receive_from_sock(struct connection *con)
895{
Alexander Aring4798cbb2020-09-24 10:31:26 -0400896 struct msghdr msg;
897 struct kvec iov;
898 int ret, buflen;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100899
900 mutex_lock(&con->sock_mutex);
901
902 if (con->sock == NULL) {
903 ret = -EAGAIN;
904 goto out_close;
905 }
Alexander Aring4798cbb2020-09-24 10:31:26 -0400906
Alexander Aring4798cbb2020-09-24 10:31:26 -0400907 /* realloc if we get new buffer size to read out */
908 buflen = dlm_config.ci_buffer_size;
909 if (con->rx_buflen != buflen && con->rx_leftover <= buflen) {
910 ret = con_realloc_receive_buf(con, buflen);
911 if (ret < 0)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100912 goto out_resched;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100913 }
914
Alexander Aring62699b32021-07-16 16:22:45 -0400915 for (;;) {
916 /* calculate new buffer parameter regarding last receive and
917 * possible leftover bytes
918 */
919 iov.iov_base = con->rx_buf + con->rx_leftover;
920 iov.iov_len = con->rx_buflen - con->rx_leftover;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100921
Alexander Aring62699b32021-07-16 16:22:45 -0400922 memset(&msg, 0, sizeof(msg));
923 msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
924 ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len,
925 msg.msg_flags);
Alexander Aring92732372021-11-02 15:17:16 -0400926 trace_dlm_recv(con->nodeid, ret);
Alexander Aring62699b32021-07-16 16:22:45 -0400927 if (ret == -EAGAIN)
928 break;
929 else if (ret <= 0)
930 goto out_close;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100931
Alexander Aring62699b32021-07-16 16:22:45 -0400932 /* new buflen according readed bytes and leftover from last receive */
933 buflen = ret + con->rx_leftover;
934 ret = dlm_process_incoming_buffer(con->nodeid, con->rx_buf, buflen);
935 if (ret < 0)
936 goto out_close;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100937
Alexander Aring62699b32021-07-16 16:22:45 -0400938 /* calculate leftover bytes from process and put it into begin of
939 * the receive buffer, so next receive we have the full message
940 * at the start address of the receive buffer.
941 */
942 con->rx_leftover = buflen - ret;
943 if (con->rx_leftover) {
944 memmove(con->rx_buf, con->rx_buf + ret,
945 con->rx_leftover);
946 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100947 }
948
Alexander Aringb97f8522021-08-18 16:27:13 -0400949 dlm_midcomms_receive_done(con->nodeid);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100950 mutex_unlock(&con->sock_mutex);
951 return 0;
952
953out_resched:
954 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
955 queue_work(recv_workqueue, &con->rwork);
956 mutex_unlock(&con->sock_mutex);
957 return -EAGAIN;
958
959out_close:
Alexander Aringba868d92021-05-21 15:08:37 -0400960 if (ret == 0) {
Alexander Aringba868d92021-05-21 15:08:37 -0400961 log_print("connection %p got EOF from %d",
962 con, con->nodeid);
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400963
Alexander Aringa66c0082021-07-16 16:22:40 -0400964 if (dlm_proto_ops->eof_condition &&
965 dlm_proto_ops->eof_condition(con)) {
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400966 set_bit(CF_EOF, &con->flags);
967 mutex_unlock(&con->sock_mutex);
968 } else {
969 mutex_unlock(&con->sock_mutex);
970 close_connection(con, false, true, false);
971
972 /* handling for tcp shutdown */
973 clear_bit(CF_SHUTDOWN, &con->flags);
974 wake_up(&con->shutdown_wait);
975 }
976
Alexander Aringba868d92021-05-21 15:08:37 -0400977 /* signal to breaking receive worker */
978 ret = -1;
Alexander Aring8aa31cb2021-05-21 15:08:39 -0400979 } else {
980 mutex_unlock(&con->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100981 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100982 return ret;
983}
984
985/* Listening socket is busy, accept a connection */
Alexander Aringd11ccd42020-11-02 20:04:25 -0500986static int accept_from_sock(struct listen_connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100987{
988 int result;
989 struct sockaddr_storage peeraddr;
990 struct socket *newsock;
Alexander Aringb38bc9c2021-05-21 15:08:35 -0400991 int len, idx;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100992 int nodeid;
993 struct connection *newcon;
994 struct connection *addcon;
Alexander Aring3f78cd72020-09-24 10:31:23 -0400995 unsigned int mark;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100996
Alexander Aringd11ccd42020-11-02 20:04:25 -0500997 if (!con->sock)
tsutomu.owa@toshiba.co.jp3421fb12017-09-12 09:01:38 +0000998 return -ENOTCONN;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100999
tsutomu.owa@toshiba.co.jp3421fb12017-09-12 09:01:38 +00001000 result = kernel_accept(con->sock, &newsock, O_NONBLOCK);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001001 if (result < 0)
1002 goto accept_err;
1003
1004 /* Get the connected socket's peer */
1005 memset(&peeraddr, 0, sizeof(peeraddr));
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001006 len = newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr, 2);
1007 if (len < 0) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001008 result = -ECONNABORTED;
1009 goto accept_err;
1010 }
1011
1012 /* Get the new node's NODEID */
1013 make_sockaddr(&peeraddr, 0, &len);
Alexander Aringe125fbe2021-03-01 17:05:09 -05001014 if (addr_to_nodeid(&peeraddr, &nodeid, &mark)) {
Masatake YAMATObcaadf52011-07-04 12:25:51 +09001015 unsigned char *b=(unsigned char *)&peeraddr;
David Teigland617e82e2007-04-26 13:46:49 -05001016 log_print("connect from non cluster node");
Masatake YAMATObcaadf52011-07-04 12:25:51 +09001017 print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
1018 b, sizeof(struct sockaddr_storage));
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001019 sock_release(newsock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001020 return -1;
1021 }
1022
1023 log_print("got connection from %d", nodeid);
1024
1025 /* Check to see if we already have a connection to this node. This
1026 * could happen if the two nodes initiate a connection at roughly
1027 * the same time and the connections cross on the wire.
1028 * In this case we store the incoming one in "othercon"
1029 */
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001030 idx = srcu_read_lock(&connections_srcu);
David Teigland748285c2009-05-15 10:50:57 -05001031 newcon = nodeid2con(nodeid, GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001032 if (!newcon) {
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001033 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001034 result = -ENOMEM;
1035 goto accept_err;
1036 }
Alexander Aringd11ccd42020-11-02 20:04:25 -05001037
Alexander Aringe125fbe2021-03-01 17:05:09 -05001038 sock_set_mark(newsock->sk, mark);
1039
Alexander Aringd11ccd42020-11-02 20:04:25 -05001040 mutex_lock(&newcon->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001041 if (newcon->sock) {
1042 struct connection *othercon = newcon->othercon;
1043
1044 if (!othercon) {
Alexander Aringa47666eb2020-08-27 15:02:49 -04001045 othercon = kzalloc(sizeof(*othercon), GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001046 if (!othercon) {
David Teigland617e82e2007-04-26 13:46:49 -05001047 log_print("failed to allocate incoming socket");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001048 mutex_unlock(&newcon->sock_mutex);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001049 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001050 result = -ENOMEM;
1051 goto accept_err;
1052 }
Alexander Aring4798cbb2020-09-24 10:31:26 -04001053
Alexander Aring6cde2102020-11-02 20:04:21 -05001054 result = dlm_con_init(othercon, nodeid);
1055 if (result < 0) {
Alexander Aring4798cbb2020-09-24 10:31:26 -04001056 kfree(othercon);
Yang Yingliang2fd8db22021-03-27 16:37:04 +08001057 mutex_unlock(&newcon->sock_mutex);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001058 srcu_read_unlock(&connections_srcu, idx);
Alexander Aring4798cbb2020-09-24 10:31:26 -04001059 goto accept_err;
1060 }
1061
Alexander Aringe9a470a2021-03-01 17:05:11 -05001062 lockdep_set_subclass(&othercon->sock_mutex, 1);
Alexander Aring7443bc92021-05-21 15:08:36 -04001063 set_bit(CF_IS_OTHERCON, &othercon->flags);
Alexander Aring6cde2102020-11-02 20:04:21 -05001064 newcon->othercon = othercon;
Alexander Aringba868d92021-05-21 15:08:37 -04001065 othercon->sendcon = newcon;
Alexander Aringba3ab3c2020-07-27 09:13:37 -04001066 } else {
1067 /* close other sock con if we have something new */
1068 close_connection(othercon, false, true, false);
Patrick Caulfield61d96be02007-08-20 15:13:38 +01001069 }
Alexander Aringba3ab3c2020-07-27 09:13:37 -04001070
Alexander Aringe9a470a2021-03-01 17:05:11 -05001071 mutex_lock(&othercon->sock_mutex);
Alexander Aringba3ab3c2020-07-27 09:13:37 -04001072 add_sock(newsock, othercon);
1073 addcon = othercon;
1074 mutex_unlock(&othercon->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001075 }
1076 else {
Bob Peterson3735b4b2016-09-23 14:23:26 -04001077 /* accept copies the sk after we've saved the callbacks, so we
1078 don't want to save them a second time or comm errors will
1079 result in calling sk_error_report recursively. */
tsutomu.owa@toshiba.co.jp988419a2017-09-12 08:55:32 +00001080 add_sock(newsock, newcon);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001081 addcon = newcon;
1082 }
1083
Alexander Aringb30a6242021-03-01 17:05:10 -05001084 set_bit(CF_CONNECTED, &addcon->flags);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001085 mutex_unlock(&newcon->sock_mutex);
1086
1087 /*
1088 * Add it to the active queue in case we got data
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001089 * between processing the accept adding the socket
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001090 * to the read_sockets list
1091 */
1092 if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
1093 queue_work(recv_workqueue, &addcon->rwork);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001094
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001095 srcu_read_unlock(&connections_srcu, idx);
1096
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001097 return 0;
1098
1099accept_err:
tsutomu.owa@toshiba.co.jp3421fb12017-09-12 09:01:38 +00001100 if (newsock)
1101 sock_release(newsock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001102
1103 if (result != -EAGAIN)
David Teigland617e82e2007-04-26 13:46:49 -05001104 log_print("error accepting connection from node: %d", result);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001105 return result;
1106}
1107
Mike Christie5d689872013-06-14 04:56:13 -05001108/*
1109 * writequeue_entry_complete - try to delete and free write queue entry
1110 * @e: write queue entry to try to delete
1111 * @completed: bytes completed
1112 *
1113 * writequeue_lock must be held.
1114 */
1115static void writequeue_entry_complete(struct writequeue_entry *e, int completed)
1116{
1117 e->offset += completed;
1118 e->len -= completed;
Alexander Aring706474f2021-05-21 15:08:48 -04001119 /* signal that page was half way transmitted */
1120 e->dirty = true;
Mike Christie5d689872013-06-14 04:56:13 -05001121
Alexander Aring8f2dc782021-05-21 15:08:42 -04001122 if (e->len == 0 && e->users == 0)
Mike Christie5d689872013-06-14 04:56:13 -05001123 free_entry(e);
Mike Christie5d689872013-06-14 04:56:13 -05001124}
1125
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001126/*
1127 * sctp_bind_addrs - bind a SCTP socket to all our addresses
1128 */
Alexander Aring13004e82020-11-02 20:04:24 -05001129static int sctp_bind_addrs(struct socket *sock, uint16_t port)
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001130{
1131 struct sockaddr_storage localaddr;
Christoph Hellwigc0425a42020-05-29 14:09:42 +02001132 struct sockaddr *addr = (struct sockaddr *)&localaddr;
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001133 int i, addr_len, result = 0;
1134
1135 for (i = 0; i < dlm_local_count; i++) {
1136 memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
1137 make_sockaddr(&localaddr, port, &addr_len);
1138
1139 if (!i)
Alexander Aring13004e82020-11-02 20:04:24 -05001140 result = kernel_bind(sock, addr, addr_len);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001141 else
Alexander Aring13004e82020-11-02 20:04:24 -05001142 result = sock_bind_add(sock->sk, addr, addr_len);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001143
1144 if (result < 0) {
1145 log_print("Can't bind to %d addr number %d, %d.\n",
1146 port, i + 1, result);
1147 break;
1148 }
1149 }
1150 return result;
1151}
1152
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001153/* Get local addresses */
1154static void init_local(void)
1155{
1156 struct sockaddr_storage sas, *addr;
1157 int i;
1158
Patrick Caulfield30d3a232007-04-23 16:26:21 +01001159 dlm_local_count = 0;
David Teigland1b189b82012-03-21 09:18:34 -05001160 for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001161 if (dlm_our_addr(&sas, i))
1162 break;
1163
Amitoj Kaur Chawla5c93f562016-06-23 10:22:01 +05301164 addr = kmemdup(&sas, sizeof(*addr), GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001165 if (!addr)
1166 break;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001167 dlm_local_addr[dlm_local_count++] = addr;
1168 }
1169}
1170
Alexander Aring043697f2020-08-27 15:02:50 -04001171static void deinit_local(void)
1172{
1173 int i;
1174
1175 for (i = 0; i < dlm_local_count; i++)
1176 kfree(dlm_local_addr[i]);
1177}
1178
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001179static struct writequeue_entry *new_writequeue_entry(struct connection *con,
1180 gfp_t allocation)
1181{
1182 struct writequeue_entry *entry;
1183
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001184 entry = kzalloc(sizeof(*entry), allocation);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001185 if (!entry)
1186 return NULL;
1187
Alexander Aringe1a7cbc2021-03-01 17:05:15 -05001188 entry->page = alloc_page(allocation | __GFP_ZERO);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001189 if (!entry->page) {
1190 kfree(entry);
1191 return NULL;
1192 }
1193
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001194 entry->con = con;
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001195 entry->users = 1;
Alexander Aring8f2dc782021-05-21 15:08:42 -04001196 kref_init(&entry->ref);
1197 INIT_LIST_HEAD(&entry->msgs);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001198
1199 return entry;
1200}
1201
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001202static struct writequeue_entry *new_wq_entry(struct connection *con, int len,
Alexander Aring8f2dc782021-05-21 15:08:42 -04001203 gfp_t allocation, char **ppc,
Alexander Aring5c16feb2021-11-02 15:17:19 -04001204 void (*cb)(void *data), void *data)
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001205{
1206 struct writequeue_entry *e;
1207
1208 spin_lock(&con->writequeue_lock);
1209 if (!list_empty(&con->writequeue)) {
1210 e = list_last_entry(&con->writequeue, struct writequeue_entry, list);
1211 if (DLM_WQ_REMAIN_BYTES(e) >= len) {
Alexander Aring8f2dc782021-05-21 15:08:42 -04001212 kref_get(&e->ref);
1213
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001214 *ppc = page_address(e->page) + e->end;
Alexander Aring8f2dc782021-05-21 15:08:42 -04001215 if (cb)
Alexander Aring5c16feb2021-11-02 15:17:19 -04001216 cb(data);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001217
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001218 e->end += len;
1219 e->users++;
1220 spin_unlock(&con->writequeue_lock);
1221
1222 return e;
1223 }
1224 }
1225 spin_unlock(&con->writequeue_lock);
1226
1227 e = new_writequeue_entry(con, allocation);
1228 if (!e)
1229 return NULL;
1230
Alexander Aring8f2dc782021-05-21 15:08:42 -04001231 kref_get(&e->ref);
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001232 *ppc = page_address(e->page);
1233 e->end += len;
Alexander Aring8aa31cb2021-05-21 15:08:39 -04001234 atomic_inc(&con->writequeue_cnt);
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001235
1236 spin_lock(&con->writequeue_lock);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001237 if (cb)
Alexander Aring5c16feb2021-11-02 15:17:19 -04001238 cb(data);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001239
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001240 list_add_tail(&e->list, &con->writequeue);
1241 spin_unlock(&con->writequeue_lock);
1242
1243 return e;
1244};
1245
Alexander Aring2874d1a2021-05-21 15:08:43 -04001246static struct dlm_msg *dlm_lowcomms_new_msg_con(struct connection *con, int len,
1247 gfp_t allocation, char **ppc,
Alexander Aring5c16feb2021-11-02 15:17:19 -04001248 void (*cb)(void *data),
1249 void *data)
Alexander Aring2874d1a2021-05-21 15:08:43 -04001250{
1251 struct writequeue_entry *e;
1252 struct dlm_msg *msg;
Alexander Aringc51b0222021-07-16 16:22:44 -04001253 bool sleepable;
Alexander Aring2874d1a2021-05-21 15:08:43 -04001254
1255 msg = kzalloc(sizeof(*msg), allocation);
1256 if (!msg)
1257 return NULL;
1258
Alexander Aringc51b0222021-07-16 16:22:44 -04001259 /* this mutex is being used as a wait to avoid multiple "fast"
1260 * new writequeue page list entry allocs in new_wq_entry in
1261 * normal operation which is sleepable context. Without it
1262 * we could end in multiple writequeue entries with one
1263 * dlm message because multiple callers were waiting at
1264 * the writequeue_lock in new_wq_entry().
1265 */
1266 sleepable = gfpflags_normal_context(allocation);
1267 if (sleepable)
1268 mutex_lock(&con->wq_alloc);
1269
Alexander Aring2874d1a2021-05-21 15:08:43 -04001270 kref_init(&msg->ref);
1271
Alexander Aring5c16feb2021-11-02 15:17:19 -04001272 e = new_wq_entry(con, len, allocation, ppc, cb, data);
Alexander Aring2874d1a2021-05-21 15:08:43 -04001273 if (!e) {
Alexander Aringc51b0222021-07-16 16:22:44 -04001274 if (sleepable)
1275 mutex_unlock(&con->wq_alloc);
1276
Alexander Aring2874d1a2021-05-21 15:08:43 -04001277 kfree(msg);
1278 return NULL;
1279 }
1280
Alexander Aringc51b0222021-07-16 16:22:44 -04001281 if (sleepable)
1282 mutex_unlock(&con->wq_alloc);
1283
Alexander Aring2874d1a2021-05-21 15:08:43 -04001284 msg->ppc = *ppc;
1285 msg->len = len;
1286 msg->entry = e;
1287
1288 return msg;
1289}
1290
Alexander Aring8f2dc782021-05-21 15:08:42 -04001291struct dlm_msg *dlm_lowcomms_new_msg(int nodeid, int len, gfp_t allocation,
Alexander Aring5c16feb2021-11-02 15:17:19 -04001292 char **ppc, void (*cb)(void *data),
1293 void *data)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001294{
1295 struct connection *con;
Alexander Aring8f2dc782021-05-21 15:08:42 -04001296 struct dlm_msg *msg;
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001297 int idx;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001298
Alexander Aringd10a0b82021-06-02 09:45:20 -04001299 if (len > DLM_MAX_SOCKET_BUFSIZE ||
Alexander Aringc45674f2021-03-01 17:05:14 -05001300 len < sizeof(struct dlm_header)) {
Alexander Aringd10a0b82021-06-02 09:45:20 -04001301 BUILD_BUG_ON(PAGE_SIZE < DLM_MAX_SOCKET_BUFSIZE);
Alexander Aring692f51c2020-11-02 20:04:18 -05001302 log_print("failed to allocate a buffer of size %d", len);
Alexander Aringc45674f2021-03-01 17:05:14 -05001303 WARN_ON(1);
Alexander Aring692f51c2020-11-02 20:04:18 -05001304 return NULL;
1305 }
1306
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001307 idx = srcu_read_lock(&connections_srcu);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001308 con = nodeid2con(nodeid, allocation);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001309 if (!con) {
1310 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001311 return NULL;
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001312 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001313
Alexander Aring5c16feb2021-11-02 15:17:19 -04001314 msg = dlm_lowcomms_new_msg_con(con, len, allocation, ppc, cb, data);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001315 if (!msg) {
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001316 srcu_read_unlock(&connections_srcu, idx);
1317 return NULL;
1318 }
1319
Alexander Aring8f2dc782021-05-21 15:08:42 -04001320 /* we assume if successful commit must called */
1321 msg->idx = idx;
Alexander Aring8f2dc782021-05-21 15:08:42 -04001322 return msg;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001323}
1324
Alexander Aring2874d1a2021-05-21 15:08:43 -04001325static void _dlm_lowcomms_commit_msg(struct dlm_msg *msg)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001326{
Alexander Aring8f2dc782021-05-21 15:08:42 -04001327 struct writequeue_entry *e = msg->entry;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001328 struct connection *con = e->con;
1329 int users;
1330
1331 spin_lock(&con->writequeue_lock);
Alexander Aring8f2dc782021-05-21 15:08:42 -04001332 kref_get(&msg->ref);
1333 list_add(&msg->list, &e->msgs);
1334
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001335 users = --e->users;
1336 if (users)
1337 goto out;
Alexander Aringf0747ebf2021-03-01 17:05:16 -05001338
1339 e->len = DLM_WQ_LENGTH_BYTES(e);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001340 spin_unlock(&con->writequeue_lock);
1341
Bob Peterson01da24d2017-09-12 08:55:14 +00001342 queue_work(send_workqueue, &con->swork);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001343 return;
1344
1345out:
1346 spin_unlock(&con->writequeue_lock);
1347 return;
1348}
1349
Alexander Aring2874d1a2021-05-21 15:08:43 -04001350void dlm_lowcomms_commit_msg(struct dlm_msg *msg)
1351{
1352 _dlm_lowcomms_commit_msg(msg);
1353 srcu_read_unlock(&connections_srcu, msg->idx);
1354}
1355
Alexander Aring8f2dc782021-05-21 15:08:42 -04001356void dlm_lowcomms_put_msg(struct dlm_msg *msg)
1357{
1358 kref_put(&msg->ref, dlm_msg_release);
1359}
1360
Alexander Aring2874d1a2021-05-21 15:08:43 -04001361/* does not held connections_srcu, usage workqueue only */
1362int dlm_lowcomms_resend_msg(struct dlm_msg *msg)
1363{
1364 struct dlm_msg *msg_resend;
1365 char *ppc;
1366
1367 if (msg->retransmit)
1368 return 1;
1369
1370 msg_resend = dlm_lowcomms_new_msg_con(msg->entry->con, msg->len,
1371 GFP_ATOMIC, &ppc, NULL, NULL);
1372 if (!msg_resend)
1373 return -ENOMEM;
1374
1375 msg->retransmit = true;
1376 kref_get(&msg->ref);
1377 msg_resend->orig_msg = msg;
1378
1379 memcpy(ppc, msg->ppc, msg->len);
1380 _dlm_lowcomms_commit_msg(msg_resend);
1381 dlm_lowcomms_put_msg(msg_resend);
1382
1383 return 0;
1384}
1385
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001386/* Send a message */
1387static void send_to_sock(struct connection *con)
1388{
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001389 const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1390 struct writequeue_entry *e;
Alexander Aring66d59552021-07-16 16:22:39 -04001391 int len, offset, ret;
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001392 int count = 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001393
1394 mutex_lock(&con->sock_mutex);
1395 if (con->sock == NULL)
1396 goto out_connect;
1397
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001398 spin_lock(&con->writequeue_lock);
1399 for (;;) {
Alexander Aring66d59552021-07-16 16:22:39 -04001400 e = con_next_wq(con);
1401 if (!e)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001402 break;
1403
1404 len = e->len;
1405 offset = e->offset;
1406 BUG_ON(len == 0 && e->users == 0);
1407 spin_unlock(&con->writequeue_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001408
Alexander Aring66d59552021-07-16 16:22:39 -04001409 ret = kernel_sendpage(con->sock, e->page, offset, len,
1410 msg_flags);
Alexander Aring92732372021-11-02 15:17:16 -04001411 trace_dlm_send(con->nodeid, ret);
Alexander Aring66d59552021-07-16 16:22:39 -04001412 if (ret == -EAGAIN || ret == 0) {
1413 if (ret == -EAGAIN &&
1414 test_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags) &&
1415 !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
1416 /* Notify TCP that we're limited by the
1417 * application window size.
1418 */
1419 set_bit(SOCK_NOSPACE, &con->sock->flags);
1420 con->sock->sk->sk_write_pending++;
1421 }
1422 cond_resched();
1423 goto out;
1424 } else if (ret < 0)
1425 goto out;
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001426
1427 /* Don't starve people filling buffers */
1428 if (++count >= MAX_SEND_MSG_COUNT) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001429 cond_resched();
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001430 count = 0;
1431 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001432
1433 spin_lock(&con->writequeue_lock);
Mike Christie5d689872013-06-14 04:56:13 -05001434 writequeue_entry_complete(e, ret);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001435 }
1436 spin_unlock(&con->writequeue_lock);
Alexander Aring8aa31cb2021-05-21 15:08:39 -04001437
1438 /* close if we got EOF */
1439 if (test_and_clear_bit(CF_EOF, &con->flags)) {
1440 mutex_unlock(&con->sock_mutex);
1441 close_connection(con, false, false, true);
1442
1443 /* handling for tcp shutdown */
1444 clear_bit(CF_SHUTDOWN, &con->flags);
1445 wake_up(&con->shutdown_wait);
1446 } else {
1447 mutex_unlock(&con->sock_mutex);
1448 }
1449
1450 return;
1451
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001452out:
1453 mutex_unlock(&con->sock_mutex);
1454 return;
1455
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001456out_connect:
1457 mutex_unlock(&con->sock_mutex);
Bob Peterson01da24d2017-09-12 08:55:14 +00001458 queue_work(send_workqueue, &con->swork);
1459 cond_resched();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001460}
1461
1462static void clean_one_writequeue(struct connection *con)
1463{
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001464 struct writequeue_entry *e, *safe;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001465
1466 spin_lock(&con->writequeue_lock);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001467 list_for_each_entry_safe(e, safe, &con->writequeue, list) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001468 free_entry(e);
1469 }
1470 spin_unlock(&con->writequeue_lock);
1471}
1472
1473/* Called from recovery when it knows that a node has
1474 left the cluster */
1475int dlm_lowcomms_close(int nodeid)
1476{
1477 struct connection *con;
David Teigland36b71a82012-07-26 12:44:30 -05001478 struct dlm_node_addr *na;
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001479 int idx;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001480
1481 log_print("closing connection to node %d", nodeid);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001482 idx = srcu_read_lock(&connections_srcu);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001483 con = nodeid2con(nodeid, 0);
1484 if (con) {
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -05001485 set_bit(CF_CLOSE, &con->flags);
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -03001486 close_connection(con, true, true, true);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001487 clean_one_writequeue(con);
Alexander Aring53a5eda2020-11-02 20:04:19 -05001488 if (con->othercon)
1489 clean_one_writequeue(con->othercon);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001490 }
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001491 srcu_read_unlock(&connections_srcu, idx);
David Teigland36b71a82012-07-26 12:44:30 -05001492
1493 spin_lock(&dlm_node_addrs_spin);
1494 na = find_node_addr(nodeid);
1495 if (na) {
1496 list_del(&na->list);
1497 while (na->addr_count--)
1498 kfree(na->addr[na->addr_count]);
1499 kfree(na);
1500 }
1501 spin_unlock(&dlm_node_addrs_spin);
1502
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001503 return 0;
1504}
1505
1506/* Receive workqueue function */
1507static void process_recv_sockets(struct work_struct *work)
1508{
1509 struct connection *con = container_of(work, struct connection, rwork);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001510
1511 clear_bit(CF_READ_PENDING, &con->flags);
Alexander Aring62699b32021-07-16 16:22:45 -04001512 receive_from_sock(con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001513}
1514
Alexander Aringd11ccd42020-11-02 20:04:25 -05001515static void process_listen_recv_socket(struct work_struct *work)
1516{
1517 accept_from_sock(&listen_con);
1518}
1519
Alexander Aring8728a452021-07-16 16:22:43 -04001520static void dlm_connect(struct connection *con)
1521{
1522 struct sockaddr_storage addr;
1523 int result, addr_len;
1524 struct socket *sock;
1525 unsigned int mark;
1526
1527 /* Some odd races can cause double-connects, ignore them */
1528 if (con->retries++ > MAX_CONNECT_RETRIES)
1529 return;
1530
1531 if (con->sock) {
1532 log_print("node %d already connected.", con->nodeid);
1533 return;
1534 }
1535
1536 memset(&addr, 0, sizeof(addr));
1537 result = nodeid_to_addr(con->nodeid, &addr, NULL,
1538 dlm_proto_ops->try_new_addr, &mark);
1539 if (result < 0) {
1540 log_print("no address for nodeid %d", con->nodeid);
1541 return;
1542 }
1543
1544 /* Create a socket to communicate with */
1545 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1546 SOCK_STREAM, dlm_proto_ops->proto, &sock);
1547 if (result < 0)
1548 goto socket_err;
1549
1550 sock_set_mark(sock->sk, mark);
1551 dlm_proto_ops->sockopts(sock);
1552
1553 add_sock(sock, con);
1554
1555 result = dlm_proto_ops->bind(sock);
1556 if (result < 0)
1557 goto add_sock_err;
1558
1559 log_print_ratelimited("connecting to %d", con->nodeid);
1560 make_sockaddr(&addr, dlm_config.ci_tcp_port, &addr_len);
1561 result = dlm_proto_ops->connect(con, sock, (struct sockaddr *)&addr,
1562 addr_len);
1563 if (result < 0)
1564 goto add_sock_err;
1565
1566 return;
1567
1568add_sock_err:
1569 dlm_close_sock(&con->sock);
1570
1571socket_err:
1572 /*
1573 * Some errors are fatal and this list might need adjusting. For other
1574 * errors we try again until the max number of retries is reached.
1575 */
1576 if (result != -EHOSTUNREACH &&
1577 result != -ENETUNREACH &&
1578 result != -ENETDOWN &&
1579 result != -EINVAL &&
1580 result != -EPROTONOSUPPORT) {
1581 log_print("connect %d try %d error %d", con->nodeid,
1582 con->retries, result);
1583 msleep(1000);
1584 lowcomms_connect_sock(con);
1585 }
1586}
1587
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001588/* Send workqueue function */
1589static void process_send_sockets(struct work_struct *work)
1590{
1591 struct connection *con = container_of(work, struct connection, swork);
1592
Alexander Aring7443bc92021-05-21 15:08:36 -04001593 WARN_ON(test_bit(CF_IS_OTHERCON, &con->flags));
1594
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001595 clear_bit(CF_WRITE_PENDING, &con->flags);
Alexander Aringba868d92021-05-21 15:08:37 -04001596
Alexander Aring489d8e52021-05-21 15:08:46 -04001597 if (test_and_clear_bit(CF_RECONNECT, &con->flags)) {
Alexander Aringba868d92021-05-21 15:08:37 -04001598 close_connection(con, false, false, true);
Alexander Aring489d8e52021-05-21 15:08:46 -04001599 dlm_midcomms_unack_msg_resend(con->nodeid);
1600 }
Alexander Aringba868d92021-05-21 15:08:37 -04001601
Alexander Aring8728a452021-07-16 16:22:43 -04001602 if (con->sock == NULL) {
Alexander Aringba868d92021-05-21 15:08:37 -04001603 if (test_and_clear_bit(CF_DELAY_CONNECT, &con->flags))
1604 msleep(1000);
Alexander Aring8728a452021-07-16 16:22:43 -04001605
1606 mutex_lock(&con->sock_mutex);
1607 dlm_connect(con);
1608 mutex_unlock(&con->sock_mutex);
Alexander Aringba868d92021-05-21 15:08:37 -04001609 }
Alexander Aring8728a452021-07-16 16:22:43 -04001610
Bob Peterson01da24d2017-09-12 08:55:14 +00001611 if (!list_empty(&con->writequeue))
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -05001612 send_to_sock(con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001613}
1614
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001615static void work_stop(void)
1616{
Alexander Aringfcef0e62021-06-02 09:45:15 -04001617 if (recv_workqueue) {
David Windsorb3555162019-04-02 08:37:10 -04001618 destroy_workqueue(recv_workqueue);
Alexander Aringfcef0e62021-06-02 09:45:15 -04001619 recv_workqueue = NULL;
1620 }
1621
1622 if (send_workqueue) {
David Windsorb3555162019-04-02 08:37:10 -04001623 destroy_workqueue(send_workqueue);
Alexander Aringfcef0e62021-06-02 09:45:15 -04001624 send_workqueue = NULL;
1625 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001626}
1627
1628static int work_start(void)
1629{
Alexander Aring6c6a1cc2021-06-02 09:45:17 -04001630 recv_workqueue = alloc_ordered_workqueue("dlm_recv", WQ_MEM_RECLAIM);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001631 if (!recv_workqueue) {
1632 log_print("can't start dlm_recv");
1633 return -ENOMEM;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001634 }
1635
Alexander Aring6c6a1cc2021-06-02 09:45:17 -04001636 send_workqueue = alloc_ordered_workqueue("dlm_send", WQ_MEM_RECLAIM);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001637 if (!send_workqueue) {
1638 log_print("can't start dlm_send");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001639 destroy_workqueue(recv_workqueue);
Alexander Aringfcef0e62021-06-02 09:45:15 -04001640 recv_workqueue = NULL;
Namhyung Kimb9d41052010-12-13 13:42:24 -06001641 return -ENOMEM;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001642 }
1643
1644 return 0;
1645}
1646
Alexander Aring9d232462021-03-01 17:05:20 -05001647static void shutdown_conn(struct connection *con)
1648{
Alexander Aringa66c0082021-07-16 16:22:40 -04001649 if (dlm_proto_ops->shutdown_action)
1650 dlm_proto_ops->shutdown_action(con);
Alexander Aring9d232462021-03-01 17:05:20 -05001651}
1652
1653void dlm_lowcomms_shutdown(void)
1654{
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001655 int idx;
1656
Alexander Aring9d232462021-03-01 17:05:20 -05001657 /* Set all the flags to prevent any
1658 * socket activity.
1659 */
1660 dlm_allow_conn = 0;
1661
1662 if (recv_workqueue)
1663 flush_workqueue(recv_workqueue);
1664 if (send_workqueue)
1665 flush_workqueue(send_workqueue);
1666
1667 dlm_close_sock(&listen_con.sock);
1668
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001669 idx = srcu_read_lock(&connections_srcu);
Alexander Aring9d232462021-03-01 17:05:20 -05001670 foreach_conn(shutdown_conn);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001671 srcu_read_unlock(&connections_srcu, idx);
Alexander Aring9d232462021-03-01 17:05:20 -05001672}
1673
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001674static void _stop_conn(struct connection *con, bool and_other)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001675{
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001676 mutex_lock(&con->sock_mutex);
tsutomu.owa@toshiba.co.jp173a31f2017-09-12 09:01:24 +00001677 set_bit(CF_CLOSE, &con->flags);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001678 set_bit(CF_READ_PENDING, &con->flags);
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001679 set_bit(CF_WRITE_PENDING, &con->flags);
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +00001680 if (con->sock && con->sock->sk) {
Alexander Aring92c44602021-11-15 08:57:06 -05001681 lock_sock(con->sock->sk);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001682 con->sock->sk->sk_user_data = NULL;
Alexander Aring92c44602021-11-15 08:57:06 -05001683 release_sock(con->sock->sk);
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +00001684 }
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001685 if (con->othercon && and_other)
1686 _stop_conn(con->othercon, false);
1687 mutex_unlock(&con->sock_mutex);
1688}
1689
1690static void stop_conn(struct connection *con)
1691{
1692 _stop_conn(con, true);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001693}
1694
Alexander Aring4798cbb2020-09-24 10:31:26 -04001695static void connection_release(struct rcu_head *rcu)
1696{
1697 struct connection *con = container_of(rcu, struct connection, rcu);
1698
1699 kfree(con->rx_buf);
1700 kfree(con);
1701}
1702
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001703static void free_conn(struct connection *con)
1704{
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -03001705 close_connection(con, true, true, true);
Alexander Aringa47666eb2020-08-27 15:02:49 -04001706 spin_lock(&connections_lock);
1707 hlist_del_rcu(&con->list);
1708 spin_unlock(&connections_lock);
Alexander Aring948c47e2020-08-27 15:02:53 -04001709 if (con->othercon) {
1710 clean_one_writequeue(con->othercon);
Alexander Aring5cbec202020-11-02 20:04:16 -05001711 call_srcu(&connections_srcu, &con->othercon->rcu,
1712 connection_release);
Alexander Aring948c47e2020-08-27 15:02:53 -04001713 }
Alexander Aring0de98432020-08-27 15:02:52 -04001714 clean_one_writequeue(con);
Alexander Aring5cbec202020-11-02 20:04:16 -05001715 call_srcu(&connections_srcu, &con->rcu, connection_release);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001716}
1717
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001718static void work_flush(void)
1719{
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001720 int ok;
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001721 int i;
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001722 struct connection *con;
1723
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001724 do {
1725 ok = 1;
1726 foreach_conn(stop_conn);
David Windsorb3555162019-04-02 08:37:10 -04001727 if (recv_workqueue)
1728 flush_workqueue(recv_workqueue);
1729 if (send_workqueue)
1730 flush_workqueue(send_workqueue);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001731 for (i = 0; i < CONN_HASH_SIZE && ok; i++) {
Alexander Aringa47666eb2020-08-27 15:02:49 -04001732 hlist_for_each_entry_rcu(con, &connection_hash[i],
1733 list) {
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001734 ok &= test_bit(CF_READ_PENDING, &con->flags);
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001735 ok &= test_bit(CF_WRITE_PENDING, &con->flags);
1736 if (con->othercon) {
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001737 ok &= test_bit(CF_READ_PENDING,
1738 &con->othercon->flags);
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001739 ok &= test_bit(CF_WRITE_PENDING,
1740 &con->othercon->flags);
1741 }
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001742 }
1743 }
1744 } while (!ok);
1745}
1746
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001747void dlm_lowcomms_stop(void)
1748{
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001749 int idx;
1750
1751 idx = srcu_read_lock(&connections_srcu);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001752 work_flush();
Marcelo Ricardo Leitner3a8db792016-10-08 10:14:37 -03001753 foreach_conn(free_conn);
Alexander Aringb38bc9c2021-05-21 15:08:35 -04001754 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001755 work_stop();
Alexander Aring043697f2020-08-27 15:02:50 -04001756 deinit_local();
Alexander Aringa66c0082021-07-16 16:22:40 -04001757
1758 dlm_proto_ops = NULL;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001759}
1760
Alexander Aring2dc6b112021-07-16 16:22:41 -04001761static int dlm_listen_for_all(void)
1762{
1763 struct socket *sock;
1764 int result;
1765
1766 log_print("Using %s for communications",
1767 dlm_proto_ops->name);
1768
Alexander Aring90d21fc2021-07-16 16:22:42 -04001769 result = dlm_proto_ops->listen_validate();
1770 if (result < 0)
1771 return result;
Alexander Aring2dc6b112021-07-16 16:22:41 -04001772
1773 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1774 SOCK_STREAM, dlm_proto_ops->proto, &sock);
1775 if (result < 0) {
Alexander Aringfe933672021-11-02 15:17:10 -04001776 log_print("Can't create comms socket: %d", result);
Alexander Aring2dc6b112021-07-16 16:22:41 -04001777 goto out;
1778 }
1779
1780 sock_set_mark(sock->sk, dlm_config.ci_mark);
1781 dlm_proto_ops->listen_sockopts(sock);
1782
1783 result = dlm_proto_ops->listen_bind(sock);
1784 if (result < 0)
1785 goto out;
1786
1787 save_listen_callbacks(sock);
1788 add_listen_sock(sock, &listen_con);
1789
1790 INIT_WORK(&listen_con.rwork, process_listen_recv_socket);
1791 result = sock->ops->listen(sock, 5);
1792 if (result < 0) {
1793 dlm_close_sock(&listen_con.sock);
1794 goto out;
1795 }
1796
1797 return 0;
1798
1799out:
1800 sock_release(sock);
1801 return result;
1802}
1803
Alexander Aring8728a452021-07-16 16:22:43 -04001804static int dlm_tcp_bind(struct socket *sock)
1805{
1806 struct sockaddr_storage src_addr;
1807 int result, addr_len;
1808
1809 /* Bind to our cluster-known address connecting to avoid
1810 * routing problems.
1811 */
1812 memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
1813 make_sockaddr(&src_addr, 0, &addr_len);
1814
1815 result = sock->ops->bind(sock, (struct sockaddr *)&src_addr,
1816 addr_len);
1817 if (result < 0) {
1818 /* This *may* not indicate a critical error */
1819 log_print("could not bind for connect: %d", result);
1820 }
1821
1822 return 0;
1823}
1824
1825static int dlm_tcp_connect(struct connection *con, struct socket *sock,
1826 struct sockaddr *addr, int addr_len)
1827{
1828 int ret;
1829
1830 ret = sock->ops->connect(sock, addr, addr_len, O_NONBLOCK);
1831 switch (ret) {
1832 case -EINPROGRESS:
1833 fallthrough;
1834 case 0:
1835 return 0;
1836 }
1837
1838 return ret;
1839}
1840
Alexander Aring2dc6b112021-07-16 16:22:41 -04001841static int dlm_tcp_listen_validate(void)
1842{
1843 /* We don't support multi-homed hosts */
1844 if (dlm_local_count > 1) {
1845 log_print("TCP protocol can't handle multi-homed hosts, try SCTP");
1846 return -EINVAL;
1847 }
1848
1849 return 0;
1850}
1851
1852static void dlm_tcp_sockopts(struct socket *sock)
1853{
1854 /* Turn off Nagle's algorithm */
1855 tcp_sock_set_nodelay(sock->sk);
1856}
1857
1858static void dlm_tcp_listen_sockopts(struct socket *sock)
1859{
1860 dlm_tcp_sockopts(sock);
1861 sock_set_reuseaddr(sock->sk);
1862}
1863
1864static int dlm_tcp_listen_bind(struct socket *sock)
1865{
1866 int addr_len;
1867
1868 /* Bind to our port */
1869 make_sockaddr(dlm_local_addr[0], dlm_config.ci_tcp_port, &addr_len);
1870 return sock->ops->bind(sock, (struct sockaddr *)dlm_local_addr[0],
1871 addr_len);
1872}
1873
Alexander Aringa66c0082021-07-16 16:22:40 -04001874static const struct dlm_proto_ops dlm_tcp_ops = {
Alexander Aring2dc6b112021-07-16 16:22:41 -04001875 .name = "TCP",
1876 .proto = IPPROTO_TCP,
Alexander Aring8728a452021-07-16 16:22:43 -04001877 .connect = dlm_tcp_connect,
1878 .sockopts = dlm_tcp_sockopts,
1879 .bind = dlm_tcp_bind,
Alexander Aring2dc6b112021-07-16 16:22:41 -04001880 .listen_validate = dlm_tcp_listen_validate,
1881 .listen_sockopts = dlm_tcp_listen_sockopts,
1882 .listen_bind = dlm_tcp_listen_bind,
Alexander Aringa66c0082021-07-16 16:22:40 -04001883 .shutdown_action = dlm_tcp_shutdown,
1884 .eof_condition = tcp_eof_condition,
1885};
1886
Alexander Aring8728a452021-07-16 16:22:43 -04001887static int dlm_sctp_bind(struct socket *sock)
1888{
1889 return sctp_bind_addrs(sock, 0);
1890}
1891
1892static int dlm_sctp_connect(struct connection *con, struct socket *sock,
1893 struct sockaddr *addr, int addr_len)
1894{
1895 int ret;
1896
1897 /*
1898 * Make sock->ops->connect() function return in specified time,
1899 * since O_NONBLOCK argument in connect() function does not work here,
1900 * then, we should restore the default value of this attribute.
1901 */
1902 sock_set_sndtimeo(sock->sk, 5);
1903 ret = sock->ops->connect(sock, addr, addr_len, 0);
1904 sock_set_sndtimeo(sock->sk, 0);
1905 if (ret < 0)
1906 return ret;
1907
1908 if (!test_and_set_bit(CF_CONNECTED, &con->flags))
1909 log_print("successful connected to node %d", con->nodeid);
1910
1911 return 0;
1912}
1913
Alexander Aring90d21fc2021-07-16 16:22:42 -04001914static int dlm_sctp_listen_validate(void)
1915{
1916 if (!IS_ENABLED(CONFIG_IP_SCTP)) {
1917 log_print("SCTP is not enabled by this kernel");
1918 return -EOPNOTSUPP;
1919 }
1920
1921 request_module("sctp");
1922 return 0;
1923}
1924
Alexander Aring2dc6b112021-07-16 16:22:41 -04001925static int dlm_sctp_bind_listen(struct socket *sock)
1926{
1927 return sctp_bind_addrs(sock, dlm_config.ci_tcp_port);
1928}
1929
1930static void dlm_sctp_sockopts(struct socket *sock)
1931{
1932 /* Turn off Nagle's algorithm */
1933 sctp_sock_set_nodelay(sock->sk);
1934 sock_set_rcvbuf(sock->sk, NEEDED_RMEM);
1935}
1936
Alexander Aringa66c0082021-07-16 16:22:40 -04001937static const struct dlm_proto_ops dlm_sctp_ops = {
Alexander Aring2dc6b112021-07-16 16:22:41 -04001938 .name = "SCTP",
1939 .proto = IPPROTO_SCTP,
Alexander Aring8728a452021-07-16 16:22:43 -04001940 .try_new_addr = true,
1941 .connect = dlm_sctp_connect,
1942 .sockopts = dlm_sctp_sockopts,
1943 .bind = dlm_sctp_bind,
Alexander Aring90d21fc2021-07-16 16:22:42 -04001944 .listen_validate = dlm_sctp_listen_validate,
Alexander Aring2dc6b112021-07-16 16:22:41 -04001945 .listen_sockopts = dlm_sctp_sockopts,
1946 .listen_bind = dlm_sctp_bind_listen,
Alexander Aringa66c0082021-07-16 16:22:40 -04001947};
1948
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001949int dlm_lowcomms_start(void)
1950{
1951 int error = -EINVAL;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001952 int i;
1953
1954 for (i = 0; i < CONN_HASH_SIZE; i++)
1955 INIT_HLIST_HEAD(&connection_hash[i]);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001956
1957 init_local();
1958 if (!dlm_local_count) {
David Teigland617e82e2007-04-26 13:46:49 -05001959 error = -ENOTCONN;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001960 log_print("no local IP address has been set");
David Teigland513ef592012-03-30 11:46:08 -05001961 goto fail;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001962 }
1963
Alexander Aringd11ccd42020-11-02 20:04:25 -05001964 INIT_WORK(&listen_con.rwork, process_listen_recv_socket);
1965
David Teigland513ef592012-03-30 11:46:08 -05001966 error = work_start();
1967 if (error)
Alexander Aringfcef0e62021-06-02 09:45:15 -04001968 goto fail_local;
David Teigland513ef592012-03-30 11:46:08 -05001969
1970 dlm_allow_conn = 1;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001971
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001972 /* Start listening */
Alexander Aringac7d5d02021-06-02 09:45:19 -04001973 switch (dlm_config.ci_protocol) {
1974 case DLM_PROTO_TCP:
Alexander Aringa66c0082021-07-16 16:22:40 -04001975 dlm_proto_ops = &dlm_tcp_ops;
Alexander Aringac7d5d02021-06-02 09:45:19 -04001976 break;
1977 case DLM_PROTO_SCTP:
Alexander Aringa66c0082021-07-16 16:22:40 -04001978 dlm_proto_ops = &dlm_sctp_ops;
Alexander Aringac7d5d02021-06-02 09:45:19 -04001979 break;
1980 default:
1981 log_print("Invalid protocol identifier %d set",
1982 dlm_config.ci_protocol);
1983 error = -EINVAL;
Alexander Aring2dc6b112021-07-16 16:22:41 -04001984 goto fail_proto_ops;
Alexander Aringac7d5d02021-06-02 09:45:19 -04001985 }
Alexander Aring2dc6b112021-07-16 16:22:41 -04001986
1987 error = dlm_listen_for_all();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001988 if (error)
Alexander Aring2dc6b112021-07-16 16:22:41 -04001989 goto fail_listen;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001990
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001991 return 0;
1992
Alexander Aring2dc6b112021-07-16 16:22:41 -04001993fail_listen:
1994 dlm_proto_ops = NULL;
1995fail_proto_ops:
David Teigland513ef592012-03-30 11:46:08 -05001996 dlm_allow_conn = 0;
Alexander Aringd11ccd42020-11-02 20:04:25 -05001997 dlm_close_sock(&listen_con.sock);
Alexander Aringfcef0e62021-06-02 09:45:15 -04001998 work_stop();
1999fail_local:
2000 deinit_local();
David Teigland513ef592012-03-30 11:46:08 -05002001fail:
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01002002 return error;
2003}
David Teigland36b71a82012-07-26 12:44:30 -05002004
2005void dlm_lowcomms_exit(void)
2006{
2007 struct dlm_node_addr *na, *safe;
2008
2009 spin_lock(&dlm_node_addrs_spin);
2010 list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) {
2011 list_del(&na->list);
2012 while (na->addr_count--)
2013 kfree(na->addr[na->addr_count]);
2014 kfree(na);
2015 }
2016 spin_unlock(&dlm_node_addrs_spin);
2017}