blob: 7f85594b663a899fad2dcad14bcc2ca10b2c979b [file] [log] [blame]
Thomas Gleixner2522fe42019-05-28 09:57:20 -07001// SPDX-License-Identifier: GPL-2.0-only
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01002/******************************************************************************
3*******************************************************************************
4**
5** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06006** Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01007**
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01008**
9*******************************************************************************
10******************************************************************************/
11
12/*
13 * lowcomms.c
14 *
15 * This is the "low-level" comms layer.
16 *
17 * It is responsible for sending/receiving messages
18 * from other nodes in the cluster.
19 *
20 * Cluster nodes are referred to by their nodeids. nodeids are
21 * simply 32 bit numbers to the locking module - if they need to
Joe Perches2cf12c02009-01-22 13:26:47 -080022 * be expanded for the cluster infrastructure then that is its
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010023 * responsibility. It is this layer's
24 * responsibility to resolve these into IP address or
25 * whatever it needs for inter-node communication.
26 *
27 * The comms level is two kernel threads that deal mainly with
28 * the receiving of messages from other nodes and passing them
29 * up to the mid-level comms layer (which understands the
30 * message format) for execution by the locking core, and
31 * a send thread which does all the setting up of connections
32 * to remote nodes and the sending of data. Threads are not allowed
33 * to send their own data because it may cause them to wait in times
34 * of high load. Also, this way, the sending thread can collect together
35 * messages bound for one node and send them in one block.
36 *
Joe Perches2cf12c02009-01-22 13:26:47 -080037 * lowcomms will choose to use either TCP or SCTP as its transport layer
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010038 * depending on the configuration variable 'protocol'. This should be set
Joe Perches2cf12c02009-01-22 13:26:47 -080039 * to 0 (default) for TCP or 1 for SCTP. It should be configured using a
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010040 * cluster-wide mechanism as it must be the same on all nodes of the cluster
41 * for the DLM to function.
42 *
43 */
44
45#include <asm/ioctls.h>
46#include <net/sock.h>
47#include <net/tcp.h>
48#include <linux/pagemap.h>
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010049#include <linux/file.h>
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -050050#include <linux/mutex.h>
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010051#include <linux/sctp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/slab.h>
Benjamin Poirier2f2d76c2012-03-08 05:55:59 +000053#include <net/sctp/sctp.h>
Joe Perches44ad5322009-01-22 13:24:49 -080054#include <net/ipv6.h>
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010055
56#include "dlm_internal.h"
57#include "lowcomms.h"
58#include "midcomms.h"
59#include "config.h"
60
61#define NEEDED_RMEM (4*1024*1024)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -060062#define CONN_HASH_SIZE 32
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010063
Bob Petersonf92c8dd2010-11-12 11:15:20 -060064/* Number of messages to send before rescheduling */
65#define MAX_SEND_MSG_COUNT 25
Alexander Aring055923b2020-07-27 09:13:38 -040066#define DLM_SHUTDOWN_WAIT_TIMEOUT msecs_to_jiffies(10000)
Bob Petersonf92c8dd2010-11-12 11:15:20 -060067
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010068struct connection {
69 struct socket *sock; /* NULL if not connected */
70 uint32_t nodeid; /* So we know who we are in the list */
71 struct mutex sock_mutex;
72 unsigned long flags;
73#define CF_READ_PENDING 1
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +000074#define CF_WRITE_PENDING 2
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010075#define CF_INIT_PENDING 4
76#define CF_IS_OTHERCON 5
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -050077#define CF_CLOSE 6
David Millerb36930d2010-11-10 21:56:39 -080078#define CF_APP_LIMITED 7
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +000079#define CF_CLOSING 8
Alexander Aring055923b2020-07-27 09:13:38 -040080#define CF_SHUTDOWN 9
Alexander Aring19633c72020-11-02 20:04:20 -050081#define CF_CONNECTED 10
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010082 struct list_head writequeue; /* List of outgoing writequeue_entries */
83 spinlock_t writequeue_lock;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010084 void (*connect_action) (struct connection *); /* What to do to connect */
Alexander Aring055923b2020-07-27 09:13:38 -040085 void (*shutdown_action)(struct connection *con); /* What to do to shutdown */
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010086 int retries;
87#define MAX_CONNECT_RETRIES 3
Christine Caulfield5e9ccc32009-01-28 12:57:40 -060088 struct hlist_node list;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010089 struct connection *othercon;
90 struct work_struct rwork; /* Receive workqueue */
91 struct work_struct swork; /* Send workqueue */
Alexander Aring055923b2020-07-27 09:13:38 -040092 wait_queue_head_t shutdown_wait; /* wait for graceful shutdown */
Alexander Aring4798cbb2020-09-24 10:31:26 -040093 unsigned char *rx_buf;
94 int rx_buflen;
95 int rx_leftover;
Alexander Aringa47666eb2020-08-27 15:02:49 -040096 struct rcu_head rcu;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010097};
98#define sock2con(x) ((struct connection *)(x)->sk_user_data)
99
Alexander Aringd11ccd42020-11-02 20:04:25 -0500100struct listen_connection {
101 struct socket *sock;
102 struct work_struct rwork;
103};
104
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100105/* An entry waiting to be sent */
106struct writequeue_entry {
107 struct list_head list;
108 struct page *page;
109 int offset;
110 int len;
111 int end;
112 int users;
113 struct connection *con;
114};
115
David Teigland36b71a82012-07-26 12:44:30 -0500116struct dlm_node_addr {
117 struct list_head list;
118 int nodeid;
119 int addr_count;
Mike Christie98e1b602013-06-14 04:56:12 -0500120 int curr_addr_index;
David Teigland36b71a82012-07-26 12:44:30 -0500121 struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
122};
123
Bob Petersoncc661fc2017-09-12 08:55:23 +0000124static struct listen_sock_callbacks {
125 void (*sk_error_report)(struct sock *);
126 void (*sk_data_ready)(struct sock *);
127 void (*sk_state_change)(struct sock *);
128 void (*sk_write_space)(struct sock *);
129} listen_sock;
130
David Teigland36b71a82012-07-26 12:44:30 -0500131static LIST_HEAD(dlm_node_addrs);
132static DEFINE_SPINLOCK(dlm_node_addrs_spin);
133
Alexander Aringd11ccd42020-11-02 20:04:25 -0500134static struct listen_connection listen_con;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100135static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
136static int dlm_local_count;
David Teigland513ef592012-03-30 11:46:08 -0500137static int dlm_allow_conn;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100138
139/* Work queues */
140static struct workqueue_struct *recv_workqueue;
141static struct workqueue_struct *send_workqueue;
142
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600143static struct hlist_head connection_hash[CONN_HASH_SIZE];
Alexander Aringa47666eb2020-08-27 15:02:49 -0400144static DEFINE_SPINLOCK(connections_lock);
145DEFINE_STATIC_SRCU(connections_srcu);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100146
147static void process_recv_sockets(struct work_struct *work);
148static void process_send_sockets(struct work_struct *work);
149
Alexander Aring0672c3c2020-11-02 20:04:22 -0500150static void sctp_connect_to_sock(struct connection *con);
151static void tcp_connect_to_sock(struct connection *con);
Alexander Aring42873c92020-11-02 20:04:23 -0500152static void dlm_tcp_shutdown(struct connection *con);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600153
154/* This is deliberately very simple because most clusters have simple
155 sequential nodeids, so we should be able to go straight to a connection
156 struct in the array */
157static inline int nodeid_hash(int nodeid)
158{
159 return nodeid & (CONN_HASH_SIZE-1);
160}
161
162static struct connection *__find_con(int nodeid)
163{
Alexander Aringa47666eb2020-08-27 15:02:49 -0400164 int r, idx;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600165 struct connection *con;
166
167 r = nodeid_hash(nodeid);
168
Alexander Aringa47666eb2020-08-27 15:02:49 -0400169 idx = srcu_read_lock(&connections_srcu);
170 hlist_for_each_entry_rcu(con, &connection_hash[r], list) {
171 if (con->nodeid == nodeid) {
172 srcu_read_unlock(&connections_srcu, idx);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600173 return con;
Alexander Aringa47666eb2020-08-27 15:02:49 -0400174 }
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600175 }
Alexander Aringa47666eb2020-08-27 15:02:49 -0400176 srcu_read_unlock(&connections_srcu, idx);
177
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600178 return NULL;
179}
180
Alexander Aring6cde2102020-11-02 20:04:21 -0500181static int dlm_con_init(struct connection *con, int nodeid)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100182{
Alexander Aring4798cbb2020-09-24 10:31:26 -0400183 con->rx_buflen = dlm_config.ci_buffer_size;
184 con->rx_buf = kmalloc(con->rx_buflen, GFP_NOFS);
Alexander Aring6cde2102020-11-02 20:04:21 -0500185 if (!con->rx_buf)
186 return -ENOMEM;
Alexander Aring4798cbb2020-09-24 10:31:26 -0400187
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100188 con->nodeid = nodeid;
189 mutex_init(&con->sock_mutex);
190 INIT_LIST_HEAD(&con->writequeue);
191 spin_lock_init(&con->writequeue_lock);
192 INIT_WORK(&con->swork, process_send_sockets);
193 INIT_WORK(&con->rwork, process_recv_sockets);
Alexander Aring055923b2020-07-27 09:13:38 -0400194 init_waitqueue_head(&con->shutdown_wait);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100195
Alexander Aring42873c92020-11-02 20:04:23 -0500196 if (dlm_config.ci_protocol == 0) {
Alexander Aring0672c3c2020-11-02 20:04:22 -0500197 con->connect_action = tcp_connect_to_sock;
Alexander Aring42873c92020-11-02 20:04:23 -0500198 con->shutdown_action = dlm_tcp_shutdown;
199 } else {
Alexander Aring0672c3c2020-11-02 20:04:22 -0500200 con->connect_action = sctp_connect_to_sock;
Alexander Aring42873c92020-11-02 20:04:23 -0500201 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100202
Alexander Aring6cde2102020-11-02 20:04:21 -0500203 return 0;
204}
205
206/*
207 * If 'allocation' is zero then we don't attempt to create a new
208 * connection structure for this node.
209 */
210static struct connection *nodeid2con(int nodeid, gfp_t alloc)
211{
212 struct connection *con, *tmp;
213 int r, ret;
214
215 con = __find_con(nodeid);
216 if (con || !alloc)
217 return con;
218
219 con = kzalloc(sizeof(*con), alloc);
220 if (!con)
221 return NULL;
222
223 ret = dlm_con_init(con, nodeid);
224 if (ret) {
225 kfree(con);
226 return NULL;
227 }
228
Alexander Aringa47666eb2020-08-27 15:02:49 -0400229 r = nodeid_hash(nodeid);
230
231 spin_lock(&connections_lock);
Alexander Aring4f2b30f2020-09-30 18:37:29 -0400232 /* Because multiple workqueues/threads calls this function it can
233 * race on multiple cpu's. Instead of locking hot path __find_con()
234 * we just check in rare cases of recently added nodes again
235 * under protection of connections_lock. If this is the case we
236 * abort our connection creation and return the existing connection.
237 */
238 tmp = __find_con(nodeid);
239 if (tmp) {
240 spin_unlock(&connections_lock);
241 kfree(con->rx_buf);
242 kfree(con);
243 return tmp;
244 }
245
Alexander Aringa47666eb2020-08-27 15:02:49 -0400246 hlist_add_head_rcu(&con->list, &connection_hash[r]);
247 spin_unlock(&connections_lock);
248
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100249 return con;
250}
251
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600252/* Loop round all connections */
253static void foreach_conn(void (*conn_func)(struct connection *c))
254{
Alexander Aringa47666eb2020-08-27 15:02:49 -0400255 int i, idx;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600256 struct connection *con;
257
Alexander Aringa47666eb2020-08-27 15:02:49 -0400258 idx = srcu_read_lock(&connections_srcu);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600259 for (i = 0; i < CONN_HASH_SIZE; i++) {
Alexander Aringa47666eb2020-08-27 15:02:49 -0400260 hlist_for_each_entry_rcu(con, &connection_hash[i], list)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600261 conn_func(con);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600262 }
Alexander Aringa47666eb2020-08-27 15:02:49 -0400263 srcu_read_unlock(&connections_srcu, idx);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100264}
265
David Teigland36b71a82012-07-26 12:44:30 -0500266static struct dlm_node_addr *find_node_addr(int nodeid)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100267{
David Teigland36b71a82012-07-26 12:44:30 -0500268 struct dlm_node_addr *na;
269
270 list_for_each_entry(na, &dlm_node_addrs, list) {
271 if (na->nodeid == nodeid)
272 return na;
273 }
274 return NULL;
275}
276
Alexander Aring40c6b832020-11-02 20:04:27 -0500277static int addr_compare(const struct sockaddr_storage *x,
278 const struct sockaddr_storage *y)
David Teigland36b71a82012-07-26 12:44:30 -0500279{
280 switch (x->ss_family) {
281 case AF_INET: {
282 struct sockaddr_in *sinx = (struct sockaddr_in *)x;
283 struct sockaddr_in *siny = (struct sockaddr_in *)y;
284 if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
285 return 0;
286 if (sinx->sin_port != siny->sin_port)
287 return 0;
288 break;
289 }
290 case AF_INET6: {
291 struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
292 struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
293 if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
294 return 0;
295 if (sinx->sin6_port != siny->sin6_port)
296 return 0;
297 break;
298 }
299 default:
300 return 0;
301 }
302 return 1;
303}
304
305static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out,
Mike Christie98e1b602013-06-14 04:56:12 -0500306 struct sockaddr *sa_out, bool try_new_addr)
David Teigland36b71a82012-07-26 12:44:30 -0500307{
308 struct sockaddr_storage sas;
309 struct dlm_node_addr *na;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100310
311 if (!dlm_local_count)
312 return -1;
313
David Teigland36b71a82012-07-26 12:44:30 -0500314 spin_lock(&dlm_node_addrs_spin);
315 na = find_node_addr(nodeid);
Mike Christie98e1b602013-06-14 04:56:12 -0500316 if (na && na->addr_count) {
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300317 memcpy(&sas, na->addr[na->curr_addr_index],
318 sizeof(struct sockaddr_storage));
319
Mike Christie98e1b602013-06-14 04:56:12 -0500320 if (try_new_addr) {
321 na->curr_addr_index++;
322 if (na->curr_addr_index == na->addr_count)
323 na->curr_addr_index = 0;
324 }
Mike Christie98e1b602013-06-14 04:56:12 -0500325 }
David Teigland36b71a82012-07-26 12:44:30 -0500326 spin_unlock(&dlm_node_addrs_spin);
327
328 if (!na)
329 return -EEXIST;
330
331 if (!na->addr_count)
332 return -ENOENT;
333
334 if (sas_out)
335 memcpy(sas_out, &sas, sizeof(struct sockaddr_storage));
336
337 if (!sa_out)
338 return 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100339
340 if (dlm_local_addr[0]->ss_family == AF_INET) {
David Teigland36b71a82012-07-26 12:44:30 -0500341 struct sockaddr_in *in4 = (struct sockaddr_in *) &sas;
342 struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100343 ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
344 } else {
David Teigland36b71a82012-07-26 12:44:30 -0500345 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sas;
346 struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000347 ret6->sin6_addr = in6->sin6_addr;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100348 }
349
350 return 0;
351}
352
David Teigland36b71a82012-07-26 12:44:30 -0500353static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid)
354{
355 struct dlm_node_addr *na;
356 int rv = -EEXIST;
Mike Christie98e1b602013-06-14 04:56:12 -0500357 int addr_i;
David Teigland36b71a82012-07-26 12:44:30 -0500358
359 spin_lock(&dlm_node_addrs_spin);
360 list_for_each_entry(na, &dlm_node_addrs, list) {
361 if (!na->addr_count)
362 continue;
363
Mike Christie98e1b602013-06-14 04:56:12 -0500364 for (addr_i = 0; addr_i < na->addr_count; addr_i++) {
365 if (addr_compare(na->addr[addr_i], addr)) {
366 *nodeid = na->nodeid;
367 rv = 0;
368 goto unlock;
369 }
370 }
David Teigland36b71a82012-07-26 12:44:30 -0500371 }
Mike Christie98e1b602013-06-14 04:56:12 -0500372unlock:
David Teigland36b71a82012-07-26 12:44:30 -0500373 spin_unlock(&dlm_node_addrs_spin);
374 return rv;
375}
376
377int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr, int len)
378{
379 struct sockaddr_storage *new_addr;
380 struct dlm_node_addr *new_node, *na;
381
382 new_node = kzalloc(sizeof(struct dlm_node_addr), GFP_NOFS);
383 if (!new_node)
384 return -ENOMEM;
385
386 new_addr = kzalloc(sizeof(struct sockaddr_storage), GFP_NOFS);
387 if (!new_addr) {
388 kfree(new_node);
389 return -ENOMEM;
390 }
391
392 memcpy(new_addr, addr, len);
393
394 spin_lock(&dlm_node_addrs_spin);
395 na = find_node_addr(nodeid);
396 if (!na) {
397 new_node->nodeid = nodeid;
398 new_node->addr[0] = new_addr;
399 new_node->addr_count = 1;
400 list_add(&new_node->list, &dlm_node_addrs);
401 spin_unlock(&dlm_node_addrs_spin);
402 return 0;
403 }
404
405 if (na->addr_count >= DLM_MAX_ADDR_COUNT) {
406 spin_unlock(&dlm_node_addrs_spin);
407 kfree(new_addr);
408 kfree(new_node);
409 return -ENOSPC;
410 }
411
412 na->addr[na->addr_count++] = new_addr;
413 spin_unlock(&dlm_node_addrs_spin);
414 kfree(new_node);
415 return 0;
416}
417
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100418/* Data available on socket or listen socket received a connect */
David S. Miller676d2362014-04-11 16:15:36 -0400419static void lowcomms_data_ready(struct sock *sk)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100420{
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000421 struct connection *con;
422
423 read_lock_bh(&sk->sk_callback_lock);
424 con = sock2con(sk);
Patrick Caulfieldafb853f2007-06-01 10:07:26 -0500425 if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100426 queue_work(recv_workqueue, &con->rwork);
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000427 read_unlock_bh(&sk->sk_callback_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100428}
429
Alexander Aringd11ccd42020-11-02 20:04:25 -0500430static void lowcomms_listen_data_ready(struct sock *sk)
431{
432 queue_work(recv_workqueue, &listen_con.rwork);
433}
434
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100435static void lowcomms_write_space(struct sock *sk)
436{
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000437 struct connection *con;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100438
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000439 read_lock_bh(&sk->sk_callback_lock);
440 con = sock2con(sk);
David Millerb36930d2010-11-10 21:56:39 -0800441 if (!con)
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000442 goto out;
David Millerb36930d2010-11-10 21:56:39 -0800443
Alexander Aring19633c72020-11-02 20:04:20 -0500444 if (!test_and_set_bit(CF_CONNECTED, &con->flags)) {
445 log_print("successful connected to node %d", con->nodeid);
446 queue_work(send_workqueue, &con->swork);
447 goto out;
448 }
449
David Millerb36930d2010-11-10 21:56:39 -0800450 clear_bit(SOCK_NOSPACE, &con->sock->flags);
451
452 if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) {
453 con->sock->sk->sk_write_pending--;
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800454 clear_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags);
David Millerb36930d2010-11-10 21:56:39 -0800455 }
456
Bob Peterson01da24d2017-09-12 08:55:14 +0000457 queue_work(send_workqueue, &con->swork);
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +0000458out:
459 read_unlock_bh(&sk->sk_callback_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100460}
461
462static inline void lowcomms_connect_sock(struct connection *con)
463{
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -0500464 if (test_bit(CF_CLOSE, &con->flags))
465 return;
Bob Peterson61d9102b2017-09-12 08:55:04 +0000466 queue_work(send_workqueue, &con->swork);
467 cond_resched();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100468}
469
470static void lowcomms_state_change(struct sock *sk)
471{
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300472 /* SCTP layer is not calling sk_data_ready when the connection
473 * is done, so we catch the signal through here. Also, it
474 * doesn't switch socket state when entering shutdown, so we
475 * skip the write in that case.
476 */
477 if (sk->sk_shutdown) {
478 if (sk->sk_shutdown == RCV_SHUTDOWN)
479 lowcomms_data_ready(sk);
480 } else if (sk->sk_state == TCP_ESTABLISHED) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100481 lowcomms_write_space(sk);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300482 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100483}
484
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500485int dlm_lowcomms_connect_node(int nodeid)
486{
487 struct connection *con;
488
489 if (nodeid == dlm_our_nodeid())
490 return 0;
491
492 con = nodeid2con(nodeid, GFP_NOFS);
493 if (!con)
494 return -ENOMEM;
495 lowcomms_connect_sock(con);
496 return 0;
497}
498
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500499static void lowcomms_error_report(struct sock *sk)
500{
Bob Petersonb81171c2016-02-05 14:39:02 -0500501 struct connection *con;
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500502 struct sockaddr_storage saddr;
Bob Petersonb81171c2016-02-05 14:39:02 -0500503 void (*orig_report)(struct sock *) = NULL;
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500504
Bob Petersonb81171c2016-02-05 14:39:02 -0500505 read_lock_bh(&sk->sk_callback_lock);
506 con = sock2con(sk);
507 if (con == NULL)
508 goto out;
509
Bob Petersoncc661fc2017-09-12 08:55:23 +0000510 orig_report = listen_sock.sk_error_report;
Bob Peterson1a318332016-01-18 12:29:15 -0500511 if (con->sock == NULL ||
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100512 kernel_getpeername(con->sock, (struct sockaddr *)&saddr) < 0) {
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500513 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
514 "sending to node %d, port %d, "
515 "sk_err=%d/%d\n", dlm_our_nodeid(),
516 con->nodeid, dlm_config.ci_tcp_port,
517 sk->sk_err, sk->sk_err_soft);
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500518 } else if (saddr.ss_family == AF_INET) {
519 struct sockaddr_in *sin4 = (struct sockaddr_in *)&saddr;
520
521 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
522 "sending to node %d at %pI4, port %d, "
523 "sk_err=%d/%d\n", dlm_our_nodeid(),
524 con->nodeid, &sin4->sin_addr.s_addr,
525 dlm_config.ci_tcp_port, sk->sk_err,
526 sk->sk_err_soft);
527 } else {
528 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&saddr;
529
530 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
531 "sending to node %d at %u.%u.%u.%u, "
532 "port %d, sk_err=%d/%d\n", dlm_our_nodeid(),
533 con->nodeid, sin6->sin6_addr.s6_addr32[0],
534 sin6->sin6_addr.s6_addr32[1],
535 sin6->sin6_addr.s6_addr32[2],
536 sin6->sin6_addr.s6_addr32[3],
537 dlm_config.ci_tcp_port, sk->sk_err,
538 sk->sk_err_soft);
539 }
Bob Petersonb81171c2016-02-05 14:39:02 -0500540out:
541 read_unlock_bh(&sk->sk_callback_lock);
542 if (orig_report)
543 orig_report(sk);
544}
545
546/* Note: sk_callback_lock must be locked before calling this function. */
Bob Petersoncc661fc2017-09-12 08:55:23 +0000547static void save_listen_callbacks(struct socket *sock)
Bob Petersonb81171c2016-02-05 14:39:02 -0500548{
Bob Petersoncc661fc2017-09-12 08:55:23 +0000549 struct sock *sk = sock->sk;
550
551 listen_sock.sk_data_ready = sk->sk_data_ready;
552 listen_sock.sk_state_change = sk->sk_state_change;
553 listen_sock.sk_write_space = sk->sk_write_space;
554 listen_sock.sk_error_report = sk->sk_error_report;
Bob Petersonb81171c2016-02-05 14:39:02 -0500555}
556
Bob Petersoncc661fc2017-09-12 08:55:23 +0000557static void restore_callbacks(struct socket *sock)
Bob Petersonb81171c2016-02-05 14:39:02 -0500558{
Bob Petersoncc661fc2017-09-12 08:55:23 +0000559 struct sock *sk = sock->sk;
560
Bob Petersonb81171c2016-02-05 14:39:02 -0500561 write_lock_bh(&sk->sk_callback_lock);
Bob Petersonb81171c2016-02-05 14:39:02 -0500562 sk->sk_user_data = NULL;
Bob Petersoncc661fc2017-09-12 08:55:23 +0000563 sk->sk_data_ready = listen_sock.sk_data_ready;
564 sk->sk_state_change = listen_sock.sk_state_change;
565 sk->sk_write_space = listen_sock.sk_write_space;
566 sk->sk_error_report = listen_sock.sk_error_report;
Bob Petersonb81171c2016-02-05 14:39:02 -0500567 write_unlock_bh(&sk->sk_callback_lock);
Bob Petersonb3a5bbf2015-08-27 09:34:47 -0500568}
569
Alexander Aringd11ccd42020-11-02 20:04:25 -0500570static void add_listen_sock(struct socket *sock, struct listen_connection *con)
571{
572 struct sock *sk = sock->sk;
573
574 write_lock_bh(&sk->sk_callback_lock);
575 save_listen_callbacks(sock);
576 con->sock = sock;
577
578 sk->sk_user_data = con;
579 sk->sk_allocation = GFP_NOFS;
580 /* Install a data_ready callback */
581 sk->sk_data_ready = lowcomms_listen_data_ready;
582 write_unlock_bh(&sk->sk_callback_lock);
583}
584
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100585/* Make a socket active */
tsutomu.owa@toshiba.co.jp988419a2017-09-12 08:55:32 +0000586static void add_sock(struct socket *sock, struct connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100587{
Bob Petersonb81171c2016-02-05 14:39:02 -0500588 struct sock *sk = sock->sk;
589
590 write_lock_bh(&sk->sk_callback_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100591 con->sock = sock;
592
Bob Petersonb81171c2016-02-05 14:39:02 -0500593 sk->sk_user_data = con;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100594 /* Install a data_ready callback */
Bob Petersonb81171c2016-02-05 14:39:02 -0500595 sk->sk_data_ready = lowcomms_data_ready;
596 sk->sk_write_space = lowcomms_write_space;
597 sk->sk_state_change = lowcomms_state_change;
598 sk->sk_allocation = GFP_NOFS;
599 sk->sk_error_report = lowcomms_error_report;
600 write_unlock_bh(&sk->sk_callback_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100601}
602
603/* Add the port number to an IPv6 or 4 sockaddr and return the address
604 length */
605static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
606 int *addr_len)
607{
608 saddr->ss_family = dlm_local_addr[0]->ss_family;
609 if (saddr->ss_family == AF_INET) {
610 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
611 in4_addr->sin_port = cpu_to_be16(port);
612 *addr_len = sizeof(struct sockaddr_in);
613 memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
614 } else {
615 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
616 in6_addr->sin6_port = cpu_to_be16(port);
617 *addr_len = sizeof(struct sockaddr_in6);
618 }
Patrick Caulfield01c8cab2007-07-17 16:53:15 +0100619 memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100620}
621
Alexander Aringd11ccd42020-11-02 20:04:25 -0500622static void dlm_close_sock(struct socket **sock)
623{
624 if (*sock) {
625 restore_callbacks(*sock);
626 sock_release(*sock);
627 *sock = NULL;
628 }
629}
630
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100631/* Close a remote connection and tidy up */
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300632static void close_connection(struct connection *con, bool and_other,
633 bool tx, bool rx)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100634{
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +0000635 bool closing = test_and_set_bit(CF_CLOSING, &con->flags);
636
tsutomu.owa@toshiba.co.jp0aa18462017-09-12 09:02:02 +0000637 if (tx && !closing && cancel_work_sync(&con->swork)) {
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300638 log_print("canceled swork for node %d", con->nodeid);
tsutomu.owa@toshiba.co.jp0aa18462017-09-12 09:02:02 +0000639 clear_bit(CF_WRITE_PENDING, &con->flags);
640 }
641 if (rx && !closing && cancel_work_sync(&con->rwork)) {
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300642 log_print("canceled rwork for node %d", con->nodeid);
tsutomu.owa@toshiba.co.jp0aa18462017-09-12 09:02:02 +0000643 clear_bit(CF_READ_PENDING, &con->flags);
644 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100645
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300646 mutex_lock(&con->sock_mutex);
Alexander Aringd11ccd42020-11-02 20:04:25 -0500647 dlm_close_sock(&con->sock);
648
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100649 if (con->othercon && and_other) {
650 /* Will only re-enter once. */
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -0300651 close_connection(con->othercon, false, true, true);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100652 }
Patrick Caulfield9e5f2822007-08-02 14:58:14 +0100653
Alexander Aring4798cbb2020-09-24 10:31:26 -0400654 con->rx_leftover = 0;
Patrick Caulfield61d96be02007-08-20 15:13:38 +0100655 con->retries = 0;
Alexander Aring19633c72020-11-02 20:04:20 -0500656 clear_bit(CF_CONNECTED, &con->flags);
Patrick Caulfield61d96be02007-08-20 15:13:38 +0100657 mutex_unlock(&con->sock_mutex);
tsutomu.owa@toshiba.co.jpb2a66622017-09-12 08:55:50 +0000658 clear_bit(CF_CLOSING, &con->flags);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100659}
660
Alexander Aring055923b2020-07-27 09:13:38 -0400661static void shutdown_connection(struct connection *con)
662{
663 int ret;
664
665 if (cancel_work_sync(&con->swork)) {
666 log_print("canceled swork for node %d", con->nodeid);
667 clear_bit(CF_WRITE_PENDING, &con->flags);
668 }
669
670 mutex_lock(&con->sock_mutex);
671 /* nothing to shutdown */
672 if (!con->sock) {
673 mutex_unlock(&con->sock_mutex);
674 return;
675 }
676
677 set_bit(CF_SHUTDOWN, &con->flags);
678 ret = kernel_sock_shutdown(con->sock, SHUT_WR);
679 mutex_unlock(&con->sock_mutex);
680 if (ret) {
681 log_print("Connection %p failed to shutdown: %d will force close",
682 con, ret);
683 goto force_close;
684 } else {
685 ret = wait_event_timeout(con->shutdown_wait,
686 !test_bit(CF_SHUTDOWN, &con->flags),
687 DLM_SHUTDOWN_WAIT_TIMEOUT);
688 if (ret == 0) {
689 log_print("Connection %p shutdown timed out, will force close",
690 con);
691 goto force_close;
692 }
693 }
694
695 return;
696
697force_close:
698 clear_bit(CF_SHUTDOWN, &con->flags);
699 close_connection(con, false, true, true);
700}
701
702static void dlm_tcp_shutdown(struct connection *con)
703{
704 if (con->othercon)
705 shutdown_connection(con->othercon);
706 shutdown_connection(con);
707}
708
Alexander Aring4798cbb2020-09-24 10:31:26 -0400709static int con_realloc_receive_buf(struct connection *con, int newlen)
710{
711 unsigned char *newbuf;
712
713 newbuf = kmalloc(newlen, GFP_NOFS);
714 if (!newbuf)
715 return -ENOMEM;
716
717 /* copy any leftover from last receive */
718 if (con->rx_leftover)
719 memmove(newbuf, con->rx_buf, con->rx_leftover);
720
721 /* swap to new buffer space */
722 kfree(con->rx_buf);
723 con->rx_buflen = newlen;
724 con->rx_buf = newbuf;
725
726 return 0;
727}
728
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100729/* Data received from remote end */
730static int receive_from_sock(struct connection *con)
731{
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100732 int call_again_soon = 0;
Alexander Aring4798cbb2020-09-24 10:31:26 -0400733 struct msghdr msg;
734 struct kvec iov;
735 int ret, buflen;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100736
737 mutex_lock(&con->sock_mutex);
738
739 if (con->sock == NULL) {
740 ret = -EAGAIN;
741 goto out_close;
742 }
Alexander Aring4798cbb2020-09-24 10:31:26 -0400743
Alexander Aring4798cbb2020-09-24 10:31:26 -0400744 /* realloc if we get new buffer size to read out */
745 buflen = dlm_config.ci_buffer_size;
746 if (con->rx_buflen != buflen && con->rx_leftover <= buflen) {
747 ret = con_realloc_receive_buf(con, buflen);
748 if (ret < 0)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100749 goto out_resched;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100750 }
751
Alexander Aring4798cbb2020-09-24 10:31:26 -0400752 /* calculate new buffer parameter regarding last receive and
753 * possible leftover bytes
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100754 */
Alexander Aring4798cbb2020-09-24 10:31:26 -0400755 iov.iov_base = con->rx_buf + con->rx_leftover;
756 iov.iov_len = con->rx_buflen - con->rx_leftover;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100757
Alexander Aring4798cbb2020-09-24 10:31:26 -0400758 memset(&msg, 0, sizeof(msg));
759 msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
760 ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len,
761 msg.msg_flags);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100762 if (ret <= 0)
763 goto out_close;
Alexander Aring4798cbb2020-09-24 10:31:26 -0400764 else if (ret == iov.iov_len)
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300765 call_again_soon = 1;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100766
Alexander Aring4798cbb2020-09-24 10:31:26 -0400767 /* new buflen according readed bytes and leftover from last receive */
768 buflen = ret + con->rx_leftover;
769 ret = dlm_process_incoming_buffer(con->nodeid, con->rx_buf, buflen);
770 if (ret < 0)
771 goto out_close;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100772
Alexander Aring4798cbb2020-09-24 10:31:26 -0400773 /* calculate leftover bytes from process and put it into begin of
774 * the receive buffer, so next receive we have the full message
775 * at the start address of the receive buffer.
776 */
777 con->rx_leftover = buflen - ret;
778 if (con->rx_leftover) {
779 memmove(con->rx_buf, con->rx_buf + ret,
780 con->rx_leftover);
781 call_again_soon = true;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100782 }
783
784 if (call_again_soon)
785 goto out_resched;
Alexander Aring4798cbb2020-09-24 10:31:26 -0400786
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100787 mutex_unlock(&con->sock_mutex);
788 return 0;
789
790out_resched:
791 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
792 queue_work(recv_workqueue, &con->rwork);
793 mutex_unlock(&con->sock_mutex);
794 return -EAGAIN;
795
796out_close:
797 mutex_unlock(&con->sock_mutex);
Patrick Caulfield9e5f2822007-08-02 14:58:14 +0100798 if (ret != -EAGAIN) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100799 /* Reconnect when there is something to send */
Alexander Aring055923b2020-07-27 09:13:38 -0400800 close_connection(con, false, true, false);
801 if (ret == 0) {
802 log_print("connection %p got EOF from %d",
803 con, con->nodeid);
804 /* handling for tcp shutdown */
805 clear_bit(CF_SHUTDOWN, &con->flags);
806 wake_up(&con->shutdown_wait);
807 /* signal to breaking receive worker */
808 ret = -1;
809 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100810 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100811 return ret;
812}
813
814/* Listening socket is busy, accept a connection */
Alexander Aringd11ccd42020-11-02 20:04:25 -0500815static int accept_from_sock(struct listen_connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100816{
817 int result;
818 struct sockaddr_storage peeraddr;
819 struct socket *newsock;
820 int len;
821 int nodeid;
822 struct connection *newcon;
823 struct connection *addcon;
Alexander Aring3f78cd72020-09-24 10:31:23 -0400824 unsigned int mark;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100825
David Teigland513ef592012-03-30 11:46:08 -0500826 if (!dlm_allow_conn) {
David Teigland513ef592012-03-30 11:46:08 -0500827 return -1;
828 }
David Teigland513ef592012-03-30 11:46:08 -0500829
Alexander Aringd11ccd42020-11-02 20:04:25 -0500830 if (!con->sock)
tsutomu.owa@toshiba.co.jp3421fb12017-09-12 09:01:38 +0000831 return -ENOTCONN;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100832
tsutomu.owa@toshiba.co.jp3421fb12017-09-12 09:01:38 +0000833 result = kernel_accept(con->sock, &newsock, O_NONBLOCK);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100834 if (result < 0)
835 goto accept_err;
836
837 /* Get the connected socket's peer */
838 memset(&peeraddr, 0, sizeof(peeraddr));
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100839 len = newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr, 2);
840 if (len < 0) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100841 result = -ECONNABORTED;
842 goto accept_err;
843 }
844
845 /* Get the new node's NODEID */
846 make_sockaddr(&peeraddr, 0, &len);
David Teigland36b71a82012-07-26 12:44:30 -0500847 if (addr_to_nodeid(&peeraddr, &nodeid)) {
Masatake YAMATObcaadf52011-07-04 12:25:51 +0900848 unsigned char *b=(unsigned char *)&peeraddr;
David Teigland617e82e2007-04-26 13:46:49 -0500849 log_print("connect from non cluster node");
Masatake YAMATObcaadf52011-07-04 12:25:51 +0900850 print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
851 b, sizeof(struct sockaddr_storage));
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100852 sock_release(newsock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100853 return -1;
854 }
855
Alexander Aring3f78cd72020-09-24 10:31:23 -0400856 dlm_comm_mark(nodeid, &mark);
857 sock_set_mark(newsock->sk, mark);
858
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100859 log_print("got connection from %d", nodeid);
860
861 /* Check to see if we already have a connection to this node. This
862 * could happen if the two nodes initiate a connection at roughly
863 * the same time and the connections cross on the wire.
864 * In this case we store the incoming one in "othercon"
865 */
David Teigland748285c2009-05-15 10:50:57 -0500866 newcon = nodeid2con(nodeid, GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100867 if (!newcon) {
868 result = -ENOMEM;
869 goto accept_err;
870 }
Alexander Aringd11ccd42020-11-02 20:04:25 -0500871
872 mutex_lock(&newcon->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100873 if (newcon->sock) {
874 struct connection *othercon = newcon->othercon;
875
876 if (!othercon) {
Alexander Aringa47666eb2020-08-27 15:02:49 -0400877 othercon = kzalloc(sizeof(*othercon), GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100878 if (!othercon) {
David Teigland617e82e2007-04-26 13:46:49 -0500879 log_print("failed to allocate incoming socket");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100880 mutex_unlock(&newcon->sock_mutex);
881 result = -ENOMEM;
882 goto accept_err;
883 }
Alexander Aring4798cbb2020-09-24 10:31:26 -0400884
Alexander Aring6cde2102020-11-02 20:04:21 -0500885 result = dlm_con_init(othercon, nodeid);
886 if (result < 0) {
Alexander Aring4798cbb2020-09-24 10:31:26 -0400887 kfree(othercon);
Alexander Aring4798cbb2020-09-24 10:31:26 -0400888 goto accept_err;
889 }
890
Alexander Aring6cde2102020-11-02 20:04:21 -0500891 newcon->othercon = othercon;
Alexander Aringba3ab3c2020-07-27 09:13:37 -0400892 } else {
893 /* close other sock con if we have something new */
894 close_connection(othercon, false, true, false);
Patrick Caulfield61d96be02007-08-20 15:13:38 +0100895 }
Alexander Aringba3ab3c2020-07-27 09:13:37 -0400896
Alexander Aringd11ccd42020-11-02 20:04:25 -0500897 mutex_lock_nested(&othercon->sock_mutex, 1);
Alexander Aringba3ab3c2020-07-27 09:13:37 -0400898 add_sock(newsock, othercon);
899 addcon = othercon;
900 mutex_unlock(&othercon->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100901 }
902 else {
Bob Peterson3735b4b2016-09-23 14:23:26 -0400903 /* accept copies the sk after we've saved the callbacks, so we
904 don't want to save them a second time or comm errors will
905 result in calling sk_error_report recursively. */
tsutomu.owa@toshiba.co.jp988419a2017-09-12 08:55:32 +0000906 add_sock(newsock, newcon);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100907 addcon = newcon;
908 }
909
910 mutex_unlock(&newcon->sock_mutex);
911
912 /*
913 * Add it to the active queue in case we got data
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300914 * between processing the accept adding the socket
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100915 * to the read_sockets list
916 */
917 if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
918 queue_work(recv_workqueue, &addcon->rwork);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100919
920 return 0;
921
922accept_err:
tsutomu.owa@toshiba.co.jp3421fb12017-09-12 09:01:38 +0000923 if (newsock)
924 sock_release(newsock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100925
926 if (result != -EAGAIN)
David Teigland617e82e2007-04-26 13:46:49 -0500927 log_print("error accepting connection from node: %d", result);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100928 return result;
929}
930
931static void free_entry(struct writequeue_entry *e)
932{
933 __free_page(e->page);
934 kfree(e);
935}
936
Mike Christie5d689872013-06-14 04:56:13 -0500937/*
938 * writequeue_entry_complete - try to delete and free write queue entry
939 * @e: write queue entry to try to delete
940 * @completed: bytes completed
941 *
942 * writequeue_lock must be held.
943 */
944static void writequeue_entry_complete(struct writequeue_entry *e, int completed)
945{
946 e->offset += completed;
947 e->len -= completed;
948
949 if (e->len == 0 && e->users == 0) {
950 list_del(&e->list);
951 free_entry(e);
952 }
953}
954
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300955/*
956 * sctp_bind_addrs - bind a SCTP socket to all our addresses
957 */
Alexander Aring13004e82020-11-02 20:04:24 -0500958static int sctp_bind_addrs(struct socket *sock, uint16_t port)
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300959{
960 struct sockaddr_storage localaddr;
Christoph Hellwigc0425a42020-05-29 14:09:42 +0200961 struct sockaddr *addr = (struct sockaddr *)&localaddr;
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300962 int i, addr_len, result = 0;
963
964 for (i = 0; i < dlm_local_count; i++) {
965 memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
966 make_sockaddr(&localaddr, port, &addr_len);
967
968 if (!i)
Alexander Aring13004e82020-11-02 20:04:24 -0500969 result = kernel_bind(sock, addr, addr_len);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300970 else
Alexander Aring13004e82020-11-02 20:04:24 -0500971 result = sock_bind_add(sock->sk, addr, addr_len);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300972
973 if (result < 0) {
974 log_print("Can't bind to %d addr number %d, %d.\n",
975 port, i + 1, result);
976 break;
977 }
978 }
979 return result;
980}
981
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100982/* Initiate an SCTP association.
983 This is a special case of send_to_sock() in that we don't yet have a
984 peeled-off socket for this association, so we use the listening socket
985 and add the primary IP address of the remote node.
986 */
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300987static void sctp_connect_to_sock(struct connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100988{
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300989 struct sockaddr_storage daddr;
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300990 int result;
991 int addr_len;
992 struct socket *sock;
Alexander Aring9c9f1682020-06-26 13:26:50 -0400993 unsigned int mark;
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300994
Alexander Aring3f78cd72020-09-24 10:31:23 -0400995 dlm_comm_mark(con->nodeid, &mark);
Alexander Aring0461e0d2020-09-24 10:31:22 -0400996
Mike Christie5d689872013-06-14 04:56:13 -0500997 mutex_lock(&con->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100998
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -0300999 /* Some odd races can cause double-connects, ignore them */
1000 if (con->retries++ > MAX_CONNECT_RETRIES)
1001 goto out;
1002
1003 if (con->sock) {
1004 log_print("node %d already connected.", con->nodeid);
1005 goto out;
1006 }
1007
1008 memset(&daddr, 0, sizeof(daddr));
1009 result = nodeid_to_addr(con->nodeid, &daddr, NULL, true);
1010 if (result < 0) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001011 log_print("no address for nodeid %d", con->nodeid);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001012 goto out;
David Teigland04bedd72009-09-18 14:31:47 -05001013 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001014
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001015 /* Create a socket to communicate with */
1016 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1017 SOCK_STREAM, IPPROTO_SCTP, &sock);
1018 if (result < 0)
1019 goto socket_err;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001020
Alexander Aring9c9f1682020-06-26 13:26:50 -04001021 sock_set_mark(sock->sk, mark);
1022
tsutomu.owa@toshiba.co.jp988419a2017-09-12 08:55:32 +00001023 add_sock(sock, con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001024
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001025 /* Bind to all addresses. */
Alexander Aring13004e82020-11-02 20:04:24 -05001026 if (sctp_bind_addrs(con->sock, 0))
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001027 goto bind_err;
Mike Christie98e1b602013-06-14 04:56:12 -05001028
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001029 make_sockaddr(&daddr, dlm_config.ci_tcp_port, &addr_len);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001030
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001031 log_print("connecting to %d", con->nodeid);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001032
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001033 /* Turn off Nagle's algorithm */
Christoph Hellwig40ef92c2020-05-29 14:09:40 +02001034 sctp_sock_set_nodelay(sock->sk);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001035
Gang Hef706d832018-05-02 10:28:35 -05001036 /*
1037 * Make sock->ops->connect() function return in specified time,
1038 * since O_NONBLOCK argument in connect() function does not work here,
1039 * then, we should restore the default value of this attribute.
1040 */
Christoph Hellwig76ee0782020-05-28 07:12:12 +02001041 sock_set_sndtimeo(sock->sk, 5);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001042 result = sock->ops->connect(sock, (struct sockaddr *)&daddr, addr_len,
Gang Heda3627c2018-05-29 11:09:22 +08001043 0);
Christoph Hellwig76ee0782020-05-28 07:12:12 +02001044 sock_set_sndtimeo(sock->sk, 0);
Gang Hef706d832018-05-02 10:28:35 -05001045
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001046 if (result == -EINPROGRESS)
1047 result = 0;
Alexander Aring19633c72020-11-02 20:04:20 -05001048 if (result == 0) {
1049 if (!test_and_set_bit(CF_CONNECTED, &con->flags))
1050 log_print("successful connected to node %d", con->nodeid);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001051 goto out;
Alexander Aring19633c72020-11-02 20:04:20 -05001052 }
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001053
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001054bind_err:
1055 con->sock = NULL;
1056 sock_release(sock);
1057
1058socket_err:
1059 /*
1060 * Some errors are fatal and this list might need adjusting. For other
1061 * errors we try again until the max number of retries is reached.
1062 */
1063 if (result != -EHOSTUNREACH &&
1064 result != -ENETUNREACH &&
1065 result != -ENETDOWN &&
1066 result != -EINVAL &&
1067 result != -EPROTONOSUPPORT) {
1068 log_print("connect %d try %d error %d", con->nodeid,
1069 con->retries, result);
1070 mutex_unlock(&con->sock_mutex);
1071 msleep(1000);
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001072 lowcomms_connect_sock(con);
1073 return;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001074 }
Mike Christie5d689872013-06-14 04:56:13 -05001075
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001076out:
Mike Christie5d689872013-06-14 04:56:13 -05001077 mutex_unlock(&con->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001078}
1079
1080/* Connect a new socket to its peer */
1081static void tcp_connect_to_sock(struct connection *con)
1082{
Lon Hohberger6bd8fed2007-10-25 18:51:54 -04001083 struct sockaddr_storage saddr, src_addr;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001084 int addr_len;
Casey Dahlina89d63a2009-07-14 12:17:51 -05001085 struct socket *sock = NULL;
Alexander Aring9c9f1682020-06-26 13:26:50 -04001086 unsigned int mark;
David Teigland36b71a82012-07-26 12:44:30 -05001087 int result;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001088
Alexander Aring3f78cd72020-09-24 10:31:23 -04001089 dlm_comm_mark(con->nodeid, &mark);
Alexander Aring0461e0d2020-09-24 10:31:22 -04001090
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001091 mutex_lock(&con->sock_mutex);
1092 if (con->retries++ > MAX_CONNECT_RETRIES)
1093 goto out;
1094
1095 /* Some odd races can cause double-connects, ignore them */
David Teigland36b71a82012-07-26 12:44:30 -05001096 if (con->sock)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001097 goto out;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001098
1099 /* Create a socket to communicate with */
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001100 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1101 SOCK_STREAM, IPPROTO_TCP, &sock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001102 if (result < 0)
1103 goto out_err;
1104
Alexander Aring9c9f1682020-06-26 13:26:50 -04001105 sock_set_mark(sock->sk, mark);
1106
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001107 memset(&saddr, 0, sizeof(saddr));
Mike Christie98e1b602013-06-14 04:56:12 -05001108 result = nodeid_to_addr(con->nodeid, &saddr, NULL, false);
David Teigland36b71a82012-07-26 12:44:30 -05001109 if (result < 0) {
1110 log_print("no address for nodeid %d", con->nodeid);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001111 goto out_err;
David Teigland36b71a82012-07-26 12:44:30 -05001112 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001113
tsutomu.owa@toshiba.co.jp988419a2017-09-12 08:55:32 +00001114 add_sock(sock, con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001115
Lon Hohberger6bd8fed2007-10-25 18:51:54 -04001116 /* Bind to our cluster-known address connecting to avoid
1117 routing problems */
1118 memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
1119 make_sockaddr(&src_addr, 0, &addr_len);
1120 result = sock->ops->bind(sock, (struct sockaddr *) &src_addr,
1121 addr_len);
1122 if (result < 0) {
1123 log_print("could not bind for connect: %d", result);
1124 /* This *may* not indicate a critical error */
1125 }
1126
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001127 make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
1128
1129 log_print("connecting to %d", con->nodeid);
David Teiglandcb2d45d2010-11-12 11:12:55 -06001130
1131 /* Turn off Nagle's algorithm */
Christoph Hellwig12abc5e2020-05-28 07:12:19 +02001132 tcp_sock_set_nodelay(sock->sk);
David Teiglandcb2d45d2010-11-12 11:12:55 -06001133
David Teigland36b71a82012-07-26 12:44:30 -05001134 result = sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001135 O_NONBLOCK);
1136 if (result == -EINPROGRESS)
1137 result = 0;
1138 if (result == 0)
1139 goto out;
1140
1141out_err:
1142 if (con->sock) {
1143 sock_release(con->sock);
1144 con->sock = NULL;
Casey Dahlina89d63a2009-07-14 12:17:51 -05001145 } else if (sock) {
1146 sock_release(sock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001147 }
1148 /*
1149 * Some errors are fatal and this list might need adjusting. For other
1150 * errors we try again until the max number of retries is reached.
1151 */
David Teigland36b71a82012-07-26 12:44:30 -05001152 if (result != -EHOSTUNREACH &&
1153 result != -ENETUNREACH &&
1154 result != -ENETDOWN &&
1155 result != -EINVAL &&
1156 result != -EPROTONOSUPPORT) {
1157 log_print("connect %d try %d error %d", con->nodeid,
1158 con->retries, result);
1159 mutex_unlock(&con->sock_mutex);
1160 msleep(1000);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001161 lowcomms_connect_sock(con);
David Teigland36b71a82012-07-26 12:44:30 -05001162 return;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001163 }
1164out:
1165 mutex_unlock(&con->sock_mutex);
1166 return;
1167}
1168
Alexander Aringd11ccd42020-11-02 20:04:25 -05001169/* On error caller must run dlm_close_sock() for the
1170 * listen connection socket.
1171 */
1172static int tcp_create_listen_sock(struct listen_connection *con,
1173 struct sockaddr_storage *saddr)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001174{
1175 struct socket *sock = NULL;
1176 int result = 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001177 int addr_len;
1178
1179 if (dlm_local_addr[0]->ss_family == AF_INET)
1180 addr_len = sizeof(struct sockaddr_in);
1181 else
1182 addr_len = sizeof(struct sockaddr_in6);
1183
1184 /* Create a socket to communicate with */
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001185 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1186 SOCK_STREAM, IPPROTO_TCP, &sock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001187 if (result < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001188 log_print("Can't create listening comms socket");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001189 goto create_out;
1190 }
1191
Alexander Aringa5b7ab62020-06-26 13:26:49 -04001192 sock_set_mark(sock->sk, dlm_config.ci_mark);
1193
David Teiglandcb2d45d2010-11-12 11:12:55 -06001194 /* Turn off Nagle's algorithm */
Christoph Hellwig12abc5e2020-05-28 07:12:19 +02001195 tcp_sock_set_nodelay(sock->sk);
David Teiglandcb2d45d2010-11-12 11:12:55 -06001196
Christoph Hellwigb58f0e82020-05-28 07:12:09 +02001197 sock_set_reuseaddr(sock->sk);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001198
Alexander Aringd11ccd42020-11-02 20:04:25 -05001199 add_listen_sock(sock, con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001200
1201 /* Bind to our port */
1202 make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
1203 result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
1204 if (result < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001205 log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001206 goto create_out;
1207 }
Christoph Hellwigce3d9542020-05-28 07:12:15 +02001208 sock_set_keepalive(sock->sk);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001209
1210 result = sock->ops->listen(sock, 5);
1211 if (result < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001212 log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001213 goto create_out;
1214 }
1215
Alexander Aringd11ccd42020-11-02 20:04:25 -05001216 return 0;
1217
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001218create_out:
Alexander Aringd11ccd42020-11-02 20:04:25 -05001219 return result;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001220}
1221
1222/* Get local addresses */
1223static void init_local(void)
1224{
1225 struct sockaddr_storage sas, *addr;
1226 int i;
1227
Patrick Caulfield30d3a232007-04-23 16:26:21 +01001228 dlm_local_count = 0;
David Teigland1b189b82012-03-21 09:18:34 -05001229 for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001230 if (dlm_our_addr(&sas, i))
1231 break;
1232
Amitoj Kaur Chawla5c93f562016-06-23 10:22:01 +05301233 addr = kmemdup(&sas, sizeof(*addr), GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001234 if (!addr)
1235 break;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001236 dlm_local_addr[dlm_local_count++] = addr;
1237 }
1238}
1239
Alexander Aring043697f2020-08-27 15:02:50 -04001240static void deinit_local(void)
1241{
1242 int i;
1243
1244 for (i = 0; i < dlm_local_count; i++)
1245 kfree(dlm_local_addr[i]);
1246}
1247
Alexander Aringd11ccd42020-11-02 20:04:25 -05001248/* Initialise SCTP socket and bind to all interfaces
1249 * On error caller must run dlm_close_sock() for the
1250 * listen connection socket.
1251 */
1252static int sctp_listen_for_all(struct listen_connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001253{
1254 struct socket *sock = NULL;
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001255 int result = -EINVAL;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001256
1257 log_print("Using SCTP for communications");
1258
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -05001259 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001260 SOCK_STREAM, IPPROTO_SCTP, &sock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001261 if (result < 0) {
1262 log_print("Can't create comms socket, check SCTP is loaded");
1263 goto out;
1264 }
1265
Christoph Hellwig26cfabf2020-05-28 07:12:16 +02001266 sock_set_rcvbuf(sock->sk, NEEDED_RMEM);
Alexander Aringa5b7ab62020-06-26 13:26:49 -04001267 sock_set_mark(sock->sk, dlm_config.ci_mark);
Christoph Hellwig40ef92c2020-05-29 14:09:40 +02001268 sctp_sock_set_nodelay(sock->sk);
Mike Christie86e92ad2013-06-14 04:56:14 -05001269
Alexander Aringd11ccd42020-11-02 20:04:25 -05001270 add_listen_sock(sock, con);
Bob Petersonb81171c2016-02-05 14:39:02 -05001271
Marcelo Ricardo Leitneree44b4b2015-08-11 19:22:23 -03001272 /* Bind to all addresses. */
Alexander Aringd11ccd42020-11-02 20:04:25 -05001273 result = sctp_bind_addrs(con->sock, dlm_config.ci_tcp_port);
1274 if (result < 0)
1275 goto out;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001276
1277 result = sock->ops->listen(sock, 5);
1278 if (result < 0) {
1279 log_print("Can't set socket listening");
Alexander Aringd11ccd42020-11-02 20:04:25 -05001280 goto out;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001281 }
1282
1283 return 0;
1284
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001285out:
1286 return result;
1287}
1288
1289static int tcp_listen_for_all(void)
1290{
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001291 /* We don't support multi-homed hosts */
Alexander Aring1a26bfa2020-11-02 20:04:26 -05001292 if (dlm_local_count > 1) {
David Teigland617e82e2007-04-26 13:46:49 -05001293 log_print("TCP protocol can't handle multi-homed hosts, "
1294 "try SCTP");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001295 return -EINVAL;
1296 }
1297
1298 log_print("Using TCP for communications");
1299
Alexander Aringd11ccd42020-11-02 20:04:25 -05001300 return tcp_create_listen_sock(&listen_con, dlm_local_addr[0]);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001301}
1302
1303
1304
1305static struct writequeue_entry *new_writequeue_entry(struct connection *con,
1306 gfp_t allocation)
1307{
1308 struct writequeue_entry *entry;
1309
1310 entry = kmalloc(sizeof(struct writequeue_entry), allocation);
1311 if (!entry)
1312 return NULL;
1313
1314 entry->page = alloc_page(allocation);
1315 if (!entry->page) {
1316 kfree(entry);
1317 return NULL;
1318 }
1319
1320 entry->offset = 0;
1321 entry->len = 0;
1322 entry->end = 0;
1323 entry->users = 0;
1324 entry->con = con;
1325
1326 return entry;
1327}
1328
David Teigland617e82e2007-04-26 13:46:49 -05001329void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001330{
1331 struct connection *con;
1332 struct writequeue_entry *e;
1333 int offset = 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001334
Alexander Aring692f51c2020-11-02 20:04:18 -05001335 if (len > LOWCOMMS_MAX_TX_BUFFER_LEN) {
1336 BUILD_BUG_ON(PAGE_SIZE < LOWCOMMS_MAX_TX_BUFFER_LEN);
1337 log_print("failed to allocate a buffer of size %d", len);
1338 return NULL;
1339 }
1340
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001341 con = nodeid2con(nodeid, allocation);
1342 if (!con)
1343 return NULL;
1344
1345 spin_lock(&con->writequeue_lock);
1346 e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
1347 if ((&e->list == &con->writequeue) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001348 (PAGE_SIZE - e->end < len)) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001349 e = NULL;
1350 } else {
1351 offset = e->end;
1352 e->end += len;
Wei Yongjuneeee2b52012-10-18 22:57:19 +08001353 e->users++;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001354 }
1355 spin_unlock(&con->writequeue_lock);
1356
1357 if (e) {
1358 got_one:
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001359 *ppc = page_address(e->page) + offset;
1360 return e;
1361 }
1362
1363 e = new_writequeue_entry(con, allocation);
1364 if (e) {
1365 spin_lock(&con->writequeue_lock);
1366 offset = e->end;
1367 e->end += len;
Wei Yongjuneeee2b52012-10-18 22:57:19 +08001368 e->users++;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001369 list_add_tail(&e->list, &con->writequeue);
1370 spin_unlock(&con->writequeue_lock);
1371 goto got_one;
1372 }
1373 return NULL;
1374}
1375
1376void dlm_lowcomms_commit_buffer(void *mh)
1377{
1378 struct writequeue_entry *e = (struct writequeue_entry *)mh;
1379 struct connection *con = e->con;
1380 int users;
1381
1382 spin_lock(&con->writequeue_lock);
1383 users = --e->users;
1384 if (users)
1385 goto out;
1386 e->len = e->end - e->offset;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001387 spin_unlock(&con->writequeue_lock);
1388
Bob Peterson01da24d2017-09-12 08:55:14 +00001389 queue_work(send_workqueue, &con->swork);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001390 return;
1391
1392out:
1393 spin_unlock(&con->writequeue_lock);
1394 return;
1395}
1396
1397/* Send a message */
1398static void send_to_sock(struct connection *con)
1399{
1400 int ret = 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001401 const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1402 struct writequeue_entry *e;
1403 int len, offset;
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001404 int count = 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001405
1406 mutex_lock(&con->sock_mutex);
1407 if (con->sock == NULL)
1408 goto out_connect;
1409
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001410 spin_lock(&con->writequeue_lock);
1411 for (;;) {
1412 e = list_entry(con->writequeue.next, struct writequeue_entry,
1413 list);
1414 if ((struct list_head *) e == &con->writequeue)
1415 break;
1416
1417 len = e->len;
1418 offset = e->offset;
1419 BUG_ON(len == 0 && e->users == 0);
1420 spin_unlock(&con->writequeue_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001421
1422 ret = 0;
1423 if (len) {
Paolo Bonzini1329e3f2009-08-24 13:18:04 -05001424 ret = kernel_sendpage(con->sock, e->page, offset, len,
1425 msg_flags);
Patrick Caulfieldd66f8272007-09-14 08:49:21 +01001426 if (ret == -EAGAIN || ret == 0) {
David Millerb36930d2010-11-10 21:56:39 -08001427 if (ret == -EAGAIN &&
Eric Dumazet9cd3e072015-11-29 20:03:10 -08001428 test_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags) &&
David Millerb36930d2010-11-10 21:56:39 -08001429 !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
1430 /* Notify TCP that we're limited by the
1431 * application window size.
1432 */
1433 set_bit(SOCK_NOSPACE, &con->sock->flags);
1434 con->sock->sk->sk_write_pending++;
1435 }
Patrick Caulfieldd66f8272007-09-14 08:49:21 +01001436 cond_resched();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001437 goto out;
Ying Xue9c5bef52012-08-13 14:29:55 +08001438 } else if (ret < 0)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001439 goto send_error;
Patrick Caulfieldd66f8272007-09-14 08:49:21 +01001440 }
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001441
1442 /* Don't starve people filling buffers */
1443 if (++count >= MAX_SEND_MSG_COUNT) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001444 cond_resched();
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001445 count = 0;
1446 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001447
1448 spin_lock(&con->writequeue_lock);
Mike Christie5d689872013-06-14 04:56:13 -05001449 writequeue_entry_complete(e, ret);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001450 }
1451 spin_unlock(&con->writequeue_lock);
1452out:
1453 mutex_unlock(&con->sock_mutex);
1454 return;
1455
1456send_error:
1457 mutex_unlock(&con->sock_mutex);
Alexander Aringba3ab3c2020-07-27 09:13:37 -04001458 close_connection(con, false, false, true);
Bob Peterson01da24d2017-09-12 08:55:14 +00001459 /* Requeue the send work. When the work daemon runs again, it will try
1460 a new connection, then call this function again. */
1461 queue_work(send_workqueue, &con->swork);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001462 return;
1463
1464out_connect:
1465 mutex_unlock(&con->sock_mutex);
Bob Peterson01da24d2017-09-12 08:55:14 +00001466 queue_work(send_workqueue, &con->swork);
1467 cond_resched();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001468}
1469
1470static void clean_one_writequeue(struct connection *con)
1471{
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001472 struct writequeue_entry *e, *safe;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001473
1474 spin_lock(&con->writequeue_lock);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001475 list_for_each_entry_safe(e, safe, &con->writequeue, list) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001476 list_del(&e->list);
1477 free_entry(e);
1478 }
1479 spin_unlock(&con->writequeue_lock);
1480}
1481
1482/* Called from recovery when it knows that a node has
1483 left the cluster */
1484int dlm_lowcomms_close(int nodeid)
1485{
1486 struct connection *con;
David Teigland36b71a82012-07-26 12:44:30 -05001487 struct dlm_node_addr *na;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001488
1489 log_print("closing connection to node %d", nodeid);
1490 con = nodeid2con(nodeid, 0);
1491 if (con) {
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -05001492 set_bit(CF_CLOSE, &con->flags);
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -03001493 close_connection(con, true, true, true);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001494 clean_one_writequeue(con);
Alexander Aring53a5eda2020-11-02 20:04:19 -05001495 if (con->othercon)
1496 clean_one_writequeue(con->othercon);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001497 }
David Teigland36b71a82012-07-26 12:44:30 -05001498
1499 spin_lock(&dlm_node_addrs_spin);
1500 na = find_node_addr(nodeid);
1501 if (na) {
1502 list_del(&na->list);
1503 while (na->addr_count--)
1504 kfree(na->addr[na->addr_count]);
1505 kfree(na);
1506 }
1507 spin_unlock(&dlm_node_addrs_spin);
1508
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001509 return 0;
1510}
1511
1512/* Receive workqueue function */
1513static void process_recv_sockets(struct work_struct *work)
1514{
1515 struct connection *con = container_of(work, struct connection, rwork);
1516 int err;
1517
1518 clear_bit(CF_READ_PENDING, &con->flags);
1519 do {
Alexander Aringd11ccd42020-11-02 20:04:25 -05001520 err = receive_from_sock(con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001521 } while (!err);
1522}
1523
Alexander Aringd11ccd42020-11-02 20:04:25 -05001524static void process_listen_recv_socket(struct work_struct *work)
1525{
1526 accept_from_sock(&listen_con);
1527}
1528
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001529/* Send workqueue function */
1530static void process_send_sockets(struct work_struct *work)
1531{
1532 struct connection *con = container_of(work, struct connection, swork);
1533
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001534 clear_bit(CF_WRITE_PENDING, &con->flags);
Bob Peterson61d9102b2017-09-12 08:55:04 +00001535 if (con->sock == NULL) /* not mutex protected so check it inside too */
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001536 con->connect_action(con);
Bob Peterson01da24d2017-09-12 08:55:14 +00001537 if (!list_empty(&con->writequeue))
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -05001538 send_to_sock(con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001539}
1540
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001541static void work_stop(void)
1542{
David Windsorb3555162019-04-02 08:37:10 -04001543 if (recv_workqueue)
1544 destroy_workqueue(recv_workqueue);
1545 if (send_workqueue)
1546 destroy_workqueue(send_workqueue);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001547}
1548
1549static int work_start(void)
1550{
David Teiglande43f0552011-03-10 13:22:34 -06001551 recv_workqueue = alloc_workqueue("dlm_recv",
1552 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001553 if (!recv_workqueue) {
1554 log_print("can't start dlm_recv");
1555 return -ENOMEM;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001556 }
1557
David Teiglande43f0552011-03-10 13:22:34 -06001558 send_workqueue = alloc_workqueue("dlm_send",
1559 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001560 if (!send_workqueue) {
1561 log_print("can't start dlm_send");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001562 destroy_workqueue(recv_workqueue);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001563 return -ENOMEM;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001564 }
1565
1566 return 0;
1567}
1568
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001569static void _stop_conn(struct connection *con, bool and_other)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001570{
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001571 mutex_lock(&con->sock_mutex);
tsutomu.owa@toshiba.co.jp173a31f2017-09-12 09:01:24 +00001572 set_bit(CF_CLOSE, &con->flags);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001573 set_bit(CF_READ_PENDING, &con->flags);
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001574 set_bit(CF_WRITE_PENDING, &con->flags);
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +00001575 if (con->sock && con->sock->sk) {
1576 write_lock_bh(&con->sock->sk->sk_callback_lock);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001577 con->sock->sk->sk_user_data = NULL;
tsutomu.owa@toshiba.co.jp93eaade2017-09-12 09:01:55 +00001578 write_unlock_bh(&con->sock->sk->sk_callback_lock);
1579 }
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001580 if (con->othercon && and_other)
1581 _stop_conn(con->othercon, false);
1582 mutex_unlock(&con->sock_mutex);
1583}
1584
1585static void stop_conn(struct connection *con)
1586{
1587 _stop_conn(con, true);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001588}
1589
Alexander Aring055923b2020-07-27 09:13:38 -04001590static void shutdown_conn(struct connection *con)
1591{
1592 if (con->shutdown_action)
1593 con->shutdown_action(con);
1594}
1595
Alexander Aring4798cbb2020-09-24 10:31:26 -04001596static void connection_release(struct rcu_head *rcu)
1597{
1598 struct connection *con = container_of(rcu, struct connection, rcu);
1599
1600 kfree(con->rx_buf);
1601 kfree(con);
1602}
1603
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001604static void free_conn(struct connection *con)
1605{
Marcelo Ricardo Leitner0d737a82015-08-11 19:22:21 -03001606 close_connection(con, true, true, true);
Alexander Aringa47666eb2020-08-27 15:02:49 -04001607 spin_lock(&connections_lock);
1608 hlist_del_rcu(&con->list);
1609 spin_unlock(&connections_lock);
Alexander Aring948c47e2020-08-27 15:02:53 -04001610 if (con->othercon) {
1611 clean_one_writequeue(con->othercon);
Alexander Aring5cbec202020-11-02 20:04:16 -05001612 call_srcu(&connections_srcu, &con->othercon->rcu,
1613 connection_release);
Alexander Aring948c47e2020-08-27 15:02:53 -04001614 }
Alexander Aring0de98432020-08-27 15:02:52 -04001615 clean_one_writequeue(con);
Alexander Aring5cbec202020-11-02 20:04:16 -05001616 call_srcu(&connections_srcu, &con->rcu, connection_release);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001617}
1618
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001619static void work_flush(void)
1620{
Alexander Aringa47666eb2020-08-27 15:02:49 -04001621 int ok, idx;
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001622 int i;
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001623 struct connection *con;
1624
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001625 do {
1626 ok = 1;
1627 foreach_conn(stop_conn);
David Windsorb3555162019-04-02 08:37:10 -04001628 if (recv_workqueue)
1629 flush_workqueue(recv_workqueue);
1630 if (send_workqueue)
1631 flush_workqueue(send_workqueue);
Alexander Aringa47666eb2020-08-27 15:02:49 -04001632 idx = srcu_read_lock(&connections_srcu);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001633 for (i = 0; i < CONN_HASH_SIZE && ok; i++) {
Alexander Aringa47666eb2020-08-27 15:02:49 -04001634 hlist_for_each_entry_rcu(con, &connection_hash[i],
1635 list) {
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001636 ok &= test_bit(CF_READ_PENDING, &con->flags);
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001637 ok &= test_bit(CF_WRITE_PENDING, &con->flags);
1638 if (con->othercon) {
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001639 ok &= test_bit(CF_READ_PENDING,
1640 &con->othercon->flags);
tsutomu.owa@toshiba.co.jp8a4abb02017-09-12 09:01:16 +00001641 ok &= test_bit(CF_WRITE_PENDING,
1642 &con->othercon->flags);
1643 }
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001644 }
1645 }
Alexander Aringa47666eb2020-08-27 15:02:49 -04001646 srcu_read_unlock(&connections_srcu, idx);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001647 } while (!ok);
1648}
1649
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001650void dlm_lowcomms_stop(void)
1651{
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001652 /* Set all the flags to prevent any
1653 socket activity.
1654 */
David Teigland513ef592012-03-30 11:46:08 -05001655 dlm_allow_conn = 0;
Alexander Aringaa7ab1e2020-08-27 15:02:48 -04001656
1657 if (recv_workqueue)
1658 flush_workqueue(recv_workqueue);
1659 if (send_workqueue)
1660 flush_workqueue(send_workqueue);
1661
Alexander Aringd11ccd42020-11-02 20:04:25 -05001662 dlm_close_sock(&listen_con.sock);
1663
Alexander Aring055923b2020-07-27 09:13:38 -04001664 foreach_conn(shutdown_conn);
tsutomu.owa@toshiba.co.jpf0fb83c2017-09-12 08:55:40 +00001665 work_flush();
Marcelo Ricardo Leitner3a8db792016-10-08 10:14:37 -03001666 foreach_conn(free_conn);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001667 work_stop();
Alexander Aring043697f2020-08-27 15:02:50 -04001668 deinit_local();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001669}
1670
1671int dlm_lowcomms_start(void)
1672{
1673 int error = -EINVAL;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001674 int i;
1675
1676 for (i = 0; i < CONN_HASH_SIZE; i++)
1677 INIT_HLIST_HEAD(&connection_hash[i]);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001678
1679 init_local();
1680 if (!dlm_local_count) {
David Teigland617e82e2007-04-26 13:46:49 -05001681 error = -ENOTCONN;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001682 log_print("no local IP address has been set");
David Teigland513ef592012-03-30 11:46:08 -05001683 goto fail;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001684 }
1685
Alexander Aringd11ccd42020-11-02 20:04:25 -05001686 INIT_WORK(&listen_con.rwork, process_listen_recv_socket);
1687
David Teigland513ef592012-03-30 11:46:08 -05001688 error = work_start();
1689 if (error)
Alexander Aringa47666eb2020-08-27 15:02:49 -04001690 goto fail;
David Teigland513ef592012-03-30 11:46:08 -05001691
1692 dlm_allow_conn = 1;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001693
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001694 /* Start listening */
1695 if (dlm_config.ci_protocol == 0)
1696 error = tcp_listen_for_all();
1697 else
Alexander Aringd11ccd42020-11-02 20:04:25 -05001698 error = sctp_listen_for_all(&listen_con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001699 if (error)
1700 goto fail_unlisten;
1701
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001702 return 0;
1703
1704fail_unlisten:
David Teigland513ef592012-03-30 11:46:08 -05001705 dlm_allow_conn = 0;
Alexander Aringd11ccd42020-11-02 20:04:25 -05001706 dlm_close_sock(&listen_con.sock);
David Teigland513ef592012-03-30 11:46:08 -05001707fail:
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001708 return error;
1709}
David Teigland36b71a82012-07-26 12:44:30 -05001710
1711void dlm_lowcomms_exit(void)
1712{
1713 struct dlm_node_addr *na, *safe;
1714
1715 spin_lock(&dlm_node_addrs_spin);
1716 list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) {
1717 list_del(&na->list);
1718 while (na->addr_count--)
1719 kfree(na->addr[na->addr_count]);
1720 kfree(na);
1721 }
1722 spin_unlock(&dlm_node_addrs_spin);
1723}