Thomas Gleixner | 2522fe4 | 2019-05-28 09:57:20 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 2 | /****************************************************************************** |
| 3 | ******************************************************************************* |
| 4 | ** |
| 5 | ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 6 | ** Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved. |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 7 | ** |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 8 | ** |
| 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 Perches | 2cf12c0 | 2009-01-22 13:26:47 -0800 | [diff] [blame] | 22 | * be expanded for the cluster infrastructure then that is its |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 23 | * 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 Perches | 2cf12c0 | 2009-01-22 13:26:47 -0800 | [diff] [blame] | 37 | * lowcomms will choose to use either TCP or SCTP as its transport layer |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 38 | * depending on the configuration variable 'protocol'. This should be set |
Joe Perches | 2cf12c0 | 2009-01-22 13:26:47 -0800 | [diff] [blame] | 39 | * to 0 (default) for TCP or 1 for SCTP. It should be configured using a |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 40 | * 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 Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 49 | #include <linux/file.h> |
Matthias Kaehlcke | 7a936ce | 2008-05-12 10:04:51 -0500 | [diff] [blame] | 50 | #include <linux/mutex.h> |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 51 | #include <linux/sctp.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 52 | #include <linux/slab.h> |
Benjamin Poirier | 2f2d76c | 2012-03-08 05:55:59 +0000 | [diff] [blame] | 53 | #include <net/sctp/sctp.h> |
Joe Perches | 44ad532 | 2009-01-22 13:24:49 -0800 | [diff] [blame] | 54 | #include <net/ipv6.h> |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 55 | |
| 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 Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 62 | #define CONN_HASH_SIZE 32 |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 63 | |
Bob Peterson | f92c8dd | 2010-11-12 11:15:20 -0600 | [diff] [blame] | 64 | /* Number of messages to send before rescheduling */ |
| 65 | #define MAX_SEND_MSG_COUNT 25 |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 66 | #define DLM_SHUTDOWN_WAIT_TIMEOUT msecs_to_jiffies(10000) |
Bob Peterson | f92c8dd | 2010-11-12 11:15:20 -0600 | [diff] [blame] | 67 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 68 | struct 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.jp | 8a4abb0 | 2017-09-12 09:01:16 +0000 | [diff] [blame] | 74 | #define CF_WRITE_PENDING 2 |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 75 | #define CF_INIT_PENDING 4 |
| 76 | #define CF_IS_OTHERCON 5 |
Lars Marowsky-Bree | 063c4c9 | 2009-08-11 16:18:23 -0500 | [diff] [blame] | 77 | #define CF_CLOSE 6 |
David Miller | b36930d | 2010-11-10 21:56:39 -0800 | [diff] [blame] | 78 | #define CF_APP_LIMITED 7 |
tsutomu.owa@toshiba.co.jp | b2a6662 | 2017-09-12 08:55:50 +0000 | [diff] [blame] | 79 | #define CF_CLOSING 8 |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 80 | #define CF_SHUTDOWN 9 |
Alexander Aring | 19633c7 | 2020-11-02 20:04:20 -0500 | [diff] [blame] | 81 | #define CF_CONNECTED 10 |
Alexander Aring | ba868d9 | 2021-05-21 15:08:37 -0400 | [diff] [blame] | 82 | #define CF_RECONNECT 11 |
| 83 | #define CF_DELAY_CONNECT 12 |
Alexander Aring | 8aa31cb | 2021-05-21 15:08:39 -0400 | [diff] [blame] | 84 | #define CF_EOF 13 |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 85 | struct list_head writequeue; /* List of outgoing writequeue_entries */ |
| 86 | spinlock_t writequeue_lock; |
Alexander Aring | 8aa31cb | 2021-05-21 15:08:39 -0400 | [diff] [blame] | 87 | atomic_t writequeue_cnt; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 88 | void (*connect_action) (struct connection *); /* What to do to connect */ |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 89 | void (*shutdown_action)(struct connection *con); /* What to do to shutdown */ |
Alexander Aring | 8aa31cb | 2021-05-21 15:08:39 -0400 | [diff] [blame] | 90 | bool (*eof_condition)(struct connection *con); /* What to do to eof check */ |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 91 | int retries; |
| 92 | #define MAX_CONNECT_RETRIES 3 |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 93 | struct hlist_node list; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 94 | struct connection *othercon; |
Alexander Aring | ba868d9 | 2021-05-21 15:08:37 -0400 | [diff] [blame] | 95 | struct connection *sendcon; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 96 | struct work_struct rwork; /* Receive workqueue */ |
| 97 | struct work_struct swork; /* Send workqueue */ |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 98 | wait_queue_head_t shutdown_wait; /* wait for graceful shutdown */ |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 99 | unsigned char *rx_buf; |
| 100 | int rx_buflen; |
| 101 | int rx_leftover; |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 102 | struct rcu_head rcu; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 103 | }; |
| 104 | #define sock2con(x) ((struct connection *)(x)->sk_user_data) |
| 105 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 106 | struct listen_connection { |
| 107 | struct socket *sock; |
| 108 | struct work_struct rwork; |
| 109 | }; |
| 110 | |
Alexander Aring | f0747ebf | 2021-03-01 17:05:16 -0500 | [diff] [blame] | 111 | #define DLM_WQ_REMAIN_BYTES(e) (PAGE_SIZE - e->end) |
| 112 | #define DLM_WQ_LENGTH_BYTES(e) (e->end - e->offset) |
| 113 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 114 | /* An entry waiting to be sent */ |
| 115 | struct writequeue_entry { |
| 116 | struct list_head list; |
| 117 | struct page *page; |
| 118 | int offset; |
| 119 | int len; |
| 120 | int end; |
| 121 | int users; |
| 122 | struct connection *con; |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame^] | 123 | struct list_head msgs; |
| 124 | struct kref ref; |
| 125 | }; |
| 126 | |
| 127 | struct dlm_msg { |
| 128 | struct writequeue_entry *entry; |
| 129 | void *ppc; |
| 130 | int len; |
| 131 | int idx; /* new()/commit() idx exchange */ |
| 132 | |
| 133 | struct list_head list; |
| 134 | struct kref ref; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 135 | }; |
| 136 | |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 137 | struct dlm_node_addr { |
| 138 | struct list_head list; |
| 139 | int nodeid; |
Alexander Aring | e125fbe | 2021-03-01 17:05:09 -0500 | [diff] [blame] | 140 | int mark; |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 141 | int addr_count; |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 142 | int curr_addr_index; |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 143 | struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT]; |
| 144 | }; |
| 145 | |
Bob Peterson | cc661fc | 2017-09-12 08:55:23 +0000 | [diff] [blame] | 146 | static struct listen_sock_callbacks { |
| 147 | void (*sk_error_report)(struct sock *); |
| 148 | void (*sk_data_ready)(struct sock *); |
| 149 | void (*sk_state_change)(struct sock *); |
| 150 | void (*sk_write_space)(struct sock *); |
| 151 | } listen_sock; |
| 152 | |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 153 | static LIST_HEAD(dlm_node_addrs); |
| 154 | static DEFINE_SPINLOCK(dlm_node_addrs_spin); |
| 155 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 156 | static struct listen_connection listen_con; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 157 | static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT]; |
| 158 | static int dlm_local_count; |
Alexander Aring | 5174616 | 2021-03-01 17:05:13 -0500 | [diff] [blame] | 159 | int dlm_allow_conn; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 160 | |
| 161 | /* Work queues */ |
| 162 | static struct workqueue_struct *recv_workqueue; |
| 163 | static struct workqueue_struct *send_workqueue; |
| 164 | |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 165 | static struct hlist_head connection_hash[CONN_HASH_SIZE]; |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 166 | static DEFINE_SPINLOCK(connections_lock); |
| 167 | DEFINE_STATIC_SRCU(connections_srcu); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 168 | |
| 169 | static void process_recv_sockets(struct work_struct *work); |
| 170 | static void process_send_sockets(struct work_struct *work); |
| 171 | |
Alexander Aring | 0672c3c | 2020-11-02 20:04:22 -0500 | [diff] [blame] | 172 | static void sctp_connect_to_sock(struct connection *con); |
| 173 | static void tcp_connect_to_sock(struct connection *con); |
Alexander Aring | 42873c9 | 2020-11-02 20:04:23 -0500 | [diff] [blame] | 174 | static void dlm_tcp_shutdown(struct connection *con); |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 175 | |
| 176 | /* This is deliberately very simple because most clusters have simple |
| 177 | sequential nodeids, so we should be able to go straight to a connection |
| 178 | struct in the array */ |
| 179 | static inline int nodeid_hash(int nodeid) |
| 180 | { |
| 181 | return nodeid & (CONN_HASH_SIZE-1); |
| 182 | } |
| 183 | |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 184 | static struct connection *__find_con(int nodeid, int r) |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 185 | { |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 186 | struct connection *con; |
| 187 | |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 188 | hlist_for_each_entry_rcu(con, &connection_hash[r], list) { |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 189 | if (con->nodeid == nodeid) |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 190 | return con; |
| 191 | } |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 192 | |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 193 | return NULL; |
| 194 | } |
| 195 | |
Alexander Aring | 8aa31cb | 2021-05-21 15:08:39 -0400 | [diff] [blame] | 196 | static bool tcp_eof_condition(struct connection *con) |
| 197 | { |
| 198 | return atomic_read(&con->writequeue_cnt); |
| 199 | } |
| 200 | |
Alexander Aring | 6cde210 | 2020-11-02 20:04:21 -0500 | [diff] [blame] | 201 | static int dlm_con_init(struct connection *con, int nodeid) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 202 | { |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 203 | con->rx_buflen = dlm_config.ci_buffer_size; |
| 204 | con->rx_buf = kmalloc(con->rx_buflen, GFP_NOFS); |
Alexander Aring | 6cde210 | 2020-11-02 20:04:21 -0500 | [diff] [blame] | 205 | if (!con->rx_buf) |
| 206 | return -ENOMEM; |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 207 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 208 | con->nodeid = nodeid; |
| 209 | mutex_init(&con->sock_mutex); |
| 210 | INIT_LIST_HEAD(&con->writequeue); |
| 211 | spin_lock_init(&con->writequeue_lock); |
Alexander Aring | 8aa31cb | 2021-05-21 15:08:39 -0400 | [diff] [blame] | 212 | atomic_set(&con->writequeue_cnt, 0); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 213 | INIT_WORK(&con->swork, process_send_sockets); |
| 214 | INIT_WORK(&con->rwork, process_recv_sockets); |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 215 | init_waitqueue_head(&con->shutdown_wait); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 216 | |
Alexander Aring | 42873c9 | 2020-11-02 20:04:23 -0500 | [diff] [blame] | 217 | if (dlm_config.ci_protocol == 0) { |
Alexander Aring | 0672c3c | 2020-11-02 20:04:22 -0500 | [diff] [blame] | 218 | con->connect_action = tcp_connect_to_sock; |
Alexander Aring | 42873c9 | 2020-11-02 20:04:23 -0500 | [diff] [blame] | 219 | con->shutdown_action = dlm_tcp_shutdown; |
Alexander Aring | 8aa31cb | 2021-05-21 15:08:39 -0400 | [diff] [blame] | 220 | con->eof_condition = tcp_eof_condition; |
Alexander Aring | 42873c9 | 2020-11-02 20:04:23 -0500 | [diff] [blame] | 221 | } else { |
Alexander Aring | 0672c3c | 2020-11-02 20:04:22 -0500 | [diff] [blame] | 222 | con->connect_action = sctp_connect_to_sock; |
Alexander Aring | 42873c9 | 2020-11-02 20:04:23 -0500 | [diff] [blame] | 223 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 224 | |
Alexander Aring | 6cde210 | 2020-11-02 20:04:21 -0500 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * If 'allocation' is zero then we don't attempt to create a new |
| 230 | * connection structure for this node. |
| 231 | */ |
| 232 | static struct connection *nodeid2con(int nodeid, gfp_t alloc) |
| 233 | { |
| 234 | struct connection *con, *tmp; |
| 235 | int r, ret; |
| 236 | |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 237 | r = nodeid_hash(nodeid); |
| 238 | con = __find_con(nodeid, r); |
Alexander Aring | 6cde210 | 2020-11-02 20:04:21 -0500 | [diff] [blame] | 239 | if (con || !alloc) |
| 240 | return con; |
| 241 | |
| 242 | con = kzalloc(sizeof(*con), alloc); |
| 243 | if (!con) |
| 244 | return NULL; |
| 245 | |
| 246 | ret = dlm_con_init(con, nodeid); |
| 247 | if (ret) { |
| 248 | kfree(con); |
| 249 | return NULL; |
| 250 | } |
| 251 | |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 252 | spin_lock(&connections_lock); |
Alexander Aring | 4f2b30f | 2020-09-30 18:37:29 -0400 | [diff] [blame] | 253 | /* Because multiple workqueues/threads calls this function it can |
| 254 | * race on multiple cpu's. Instead of locking hot path __find_con() |
| 255 | * we just check in rare cases of recently added nodes again |
| 256 | * under protection of connections_lock. If this is the case we |
| 257 | * abort our connection creation and return the existing connection. |
| 258 | */ |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 259 | tmp = __find_con(nodeid, r); |
Alexander Aring | 4f2b30f | 2020-09-30 18:37:29 -0400 | [diff] [blame] | 260 | if (tmp) { |
| 261 | spin_unlock(&connections_lock); |
| 262 | kfree(con->rx_buf); |
| 263 | kfree(con); |
| 264 | return tmp; |
| 265 | } |
| 266 | |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 267 | hlist_add_head_rcu(&con->list, &connection_hash[r]); |
| 268 | spin_unlock(&connections_lock); |
| 269 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 270 | return con; |
| 271 | } |
| 272 | |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 273 | /* Loop round all connections */ |
| 274 | static void foreach_conn(void (*conn_func)(struct connection *c)) |
| 275 | { |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 276 | int i; |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 277 | struct connection *con; |
| 278 | |
| 279 | for (i = 0; i < CONN_HASH_SIZE; i++) { |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 280 | hlist_for_each_entry_rcu(con, &connection_hash[i], list) |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 281 | conn_func(con); |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 282 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 283 | } |
| 284 | |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 285 | static struct dlm_node_addr *find_node_addr(int nodeid) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 286 | { |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 287 | struct dlm_node_addr *na; |
| 288 | |
| 289 | list_for_each_entry(na, &dlm_node_addrs, list) { |
| 290 | if (na->nodeid == nodeid) |
| 291 | return na; |
| 292 | } |
| 293 | return NULL; |
| 294 | } |
| 295 | |
Alexander Aring | 40c6b83 | 2020-11-02 20:04:27 -0500 | [diff] [blame] | 296 | static int addr_compare(const struct sockaddr_storage *x, |
| 297 | const struct sockaddr_storage *y) |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 298 | { |
| 299 | switch (x->ss_family) { |
| 300 | case AF_INET: { |
| 301 | struct sockaddr_in *sinx = (struct sockaddr_in *)x; |
| 302 | struct sockaddr_in *siny = (struct sockaddr_in *)y; |
| 303 | if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr) |
| 304 | return 0; |
| 305 | if (sinx->sin_port != siny->sin_port) |
| 306 | return 0; |
| 307 | break; |
| 308 | } |
| 309 | case AF_INET6: { |
| 310 | struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x; |
| 311 | struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y; |
| 312 | if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr)) |
| 313 | return 0; |
| 314 | if (sinx->sin6_port != siny->sin6_port) |
| 315 | return 0; |
| 316 | break; |
| 317 | } |
| 318 | default: |
| 319 | return 0; |
| 320 | } |
| 321 | return 1; |
| 322 | } |
| 323 | |
| 324 | static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out, |
Alexander Aring | e125fbe | 2021-03-01 17:05:09 -0500 | [diff] [blame] | 325 | struct sockaddr *sa_out, bool try_new_addr, |
| 326 | unsigned int *mark) |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 327 | { |
| 328 | struct sockaddr_storage sas; |
| 329 | struct dlm_node_addr *na; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 330 | |
| 331 | if (!dlm_local_count) |
| 332 | return -1; |
| 333 | |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 334 | spin_lock(&dlm_node_addrs_spin); |
| 335 | na = find_node_addr(nodeid); |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 336 | if (na && na->addr_count) { |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 337 | memcpy(&sas, na->addr[na->curr_addr_index], |
| 338 | sizeof(struct sockaddr_storage)); |
| 339 | |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 340 | if (try_new_addr) { |
| 341 | na->curr_addr_index++; |
| 342 | if (na->curr_addr_index == na->addr_count) |
| 343 | na->curr_addr_index = 0; |
| 344 | } |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 345 | } |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 346 | spin_unlock(&dlm_node_addrs_spin); |
| 347 | |
| 348 | if (!na) |
| 349 | return -EEXIST; |
| 350 | |
| 351 | if (!na->addr_count) |
| 352 | return -ENOENT; |
| 353 | |
Alexander Aring | e125fbe | 2021-03-01 17:05:09 -0500 | [diff] [blame] | 354 | *mark = na->mark; |
| 355 | |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 356 | if (sas_out) |
| 357 | memcpy(sas_out, &sas, sizeof(struct sockaddr_storage)); |
| 358 | |
| 359 | if (!sa_out) |
| 360 | return 0; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 361 | |
| 362 | if (dlm_local_addr[0]->ss_family == AF_INET) { |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 363 | struct sockaddr_in *in4 = (struct sockaddr_in *) &sas; |
| 364 | struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 365 | ret4->sin_addr.s_addr = in4->sin_addr.s_addr; |
| 366 | } else { |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 367 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sas; |
| 368 | struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 369 | ret6->sin6_addr = in6->sin6_addr; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | return 0; |
| 373 | } |
| 374 | |
Alexander Aring | e125fbe | 2021-03-01 17:05:09 -0500 | [diff] [blame] | 375 | static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid, |
| 376 | unsigned int *mark) |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 377 | { |
| 378 | struct dlm_node_addr *na; |
| 379 | int rv = -EEXIST; |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 380 | int addr_i; |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 381 | |
| 382 | spin_lock(&dlm_node_addrs_spin); |
| 383 | list_for_each_entry(na, &dlm_node_addrs, list) { |
| 384 | if (!na->addr_count) |
| 385 | continue; |
| 386 | |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 387 | for (addr_i = 0; addr_i < na->addr_count; addr_i++) { |
| 388 | if (addr_compare(na->addr[addr_i], addr)) { |
| 389 | *nodeid = na->nodeid; |
Alexander Aring | e125fbe | 2021-03-01 17:05:09 -0500 | [diff] [blame] | 390 | *mark = na->mark; |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 391 | rv = 0; |
| 392 | goto unlock; |
| 393 | } |
| 394 | } |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 395 | } |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 396 | unlock: |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 397 | spin_unlock(&dlm_node_addrs_spin); |
| 398 | return rv; |
| 399 | } |
| 400 | |
Alexander Aring | 4f19d07 | 2020-11-02 20:04:28 -0500 | [diff] [blame] | 401 | /* caller need to held dlm_node_addrs_spin lock */ |
| 402 | static bool dlm_lowcomms_na_has_addr(const struct dlm_node_addr *na, |
| 403 | const struct sockaddr_storage *addr) |
| 404 | { |
| 405 | int i; |
| 406 | |
| 407 | for (i = 0; i < na->addr_count; i++) { |
| 408 | if (addr_compare(na->addr[i], addr)) |
| 409 | return true; |
| 410 | } |
| 411 | |
| 412 | return false; |
| 413 | } |
| 414 | |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 415 | int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr, int len) |
| 416 | { |
| 417 | struct sockaddr_storage *new_addr; |
| 418 | struct dlm_node_addr *new_node, *na; |
Alexander Aring | 4f19d07 | 2020-11-02 20:04:28 -0500 | [diff] [blame] | 419 | bool ret; |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 420 | |
| 421 | new_node = kzalloc(sizeof(struct dlm_node_addr), GFP_NOFS); |
| 422 | if (!new_node) |
| 423 | return -ENOMEM; |
| 424 | |
| 425 | new_addr = kzalloc(sizeof(struct sockaddr_storage), GFP_NOFS); |
| 426 | if (!new_addr) { |
| 427 | kfree(new_node); |
| 428 | return -ENOMEM; |
| 429 | } |
| 430 | |
| 431 | memcpy(new_addr, addr, len); |
| 432 | |
| 433 | spin_lock(&dlm_node_addrs_spin); |
| 434 | na = find_node_addr(nodeid); |
| 435 | if (!na) { |
| 436 | new_node->nodeid = nodeid; |
| 437 | new_node->addr[0] = new_addr; |
| 438 | new_node->addr_count = 1; |
Alexander Aring | e125fbe | 2021-03-01 17:05:09 -0500 | [diff] [blame] | 439 | new_node->mark = dlm_config.ci_mark; |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 440 | list_add(&new_node->list, &dlm_node_addrs); |
| 441 | spin_unlock(&dlm_node_addrs_spin); |
| 442 | return 0; |
| 443 | } |
| 444 | |
Alexander Aring | 4f19d07 | 2020-11-02 20:04:28 -0500 | [diff] [blame] | 445 | ret = dlm_lowcomms_na_has_addr(na, addr); |
| 446 | if (ret) { |
| 447 | spin_unlock(&dlm_node_addrs_spin); |
| 448 | kfree(new_addr); |
| 449 | kfree(new_node); |
| 450 | return -EEXIST; |
| 451 | } |
| 452 | |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 453 | if (na->addr_count >= DLM_MAX_ADDR_COUNT) { |
| 454 | spin_unlock(&dlm_node_addrs_spin); |
| 455 | kfree(new_addr); |
| 456 | kfree(new_node); |
| 457 | return -ENOSPC; |
| 458 | } |
| 459 | |
| 460 | na->addr[na->addr_count++] = new_addr; |
| 461 | spin_unlock(&dlm_node_addrs_spin); |
| 462 | kfree(new_node); |
| 463 | return 0; |
| 464 | } |
| 465 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 466 | /* Data available on socket or listen socket received a connect */ |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 467 | static void lowcomms_data_ready(struct sock *sk) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 468 | { |
tsutomu.owa@toshiba.co.jp | 93eaade | 2017-09-12 09:01:55 +0000 | [diff] [blame] | 469 | struct connection *con; |
| 470 | |
| 471 | read_lock_bh(&sk->sk_callback_lock); |
| 472 | con = sock2con(sk); |
Patrick Caulfield | afb853f | 2007-06-01 10:07:26 -0500 | [diff] [blame] | 473 | if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags)) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 474 | queue_work(recv_workqueue, &con->rwork); |
tsutomu.owa@toshiba.co.jp | 93eaade | 2017-09-12 09:01:55 +0000 | [diff] [blame] | 475 | read_unlock_bh(&sk->sk_callback_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 476 | } |
| 477 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 478 | static void lowcomms_listen_data_ready(struct sock *sk) |
| 479 | { |
| 480 | queue_work(recv_workqueue, &listen_con.rwork); |
| 481 | } |
| 482 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 483 | static void lowcomms_write_space(struct sock *sk) |
| 484 | { |
tsutomu.owa@toshiba.co.jp | 93eaade | 2017-09-12 09:01:55 +0000 | [diff] [blame] | 485 | struct connection *con; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 486 | |
tsutomu.owa@toshiba.co.jp | 93eaade | 2017-09-12 09:01:55 +0000 | [diff] [blame] | 487 | read_lock_bh(&sk->sk_callback_lock); |
| 488 | con = sock2con(sk); |
David Miller | b36930d | 2010-11-10 21:56:39 -0800 | [diff] [blame] | 489 | if (!con) |
tsutomu.owa@toshiba.co.jp | 93eaade | 2017-09-12 09:01:55 +0000 | [diff] [blame] | 490 | goto out; |
David Miller | b36930d | 2010-11-10 21:56:39 -0800 | [diff] [blame] | 491 | |
Alexander Aring | 19633c7 | 2020-11-02 20:04:20 -0500 | [diff] [blame] | 492 | if (!test_and_set_bit(CF_CONNECTED, &con->flags)) { |
| 493 | log_print("successful connected to node %d", con->nodeid); |
| 494 | queue_work(send_workqueue, &con->swork); |
| 495 | goto out; |
| 496 | } |
| 497 | |
David Miller | b36930d | 2010-11-10 21:56:39 -0800 | [diff] [blame] | 498 | clear_bit(SOCK_NOSPACE, &con->sock->flags); |
| 499 | |
| 500 | if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) { |
| 501 | con->sock->sk->sk_write_pending--; |
Eric Dumazet | 9cd3e07 | 2015-11-29 20:03:10 -0800 | [diff] [blame] | 502 | clear_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags); |
David Miller | b36930d | 2010-11-10 21:56:39 -0800 | [diff] [blame] | 503 | } |
| 504 | |
Bob Peterson | 01da24d | 2017-09-12 08:55:14 +0000 | [diff] [blame] | 505 | queue_work(send_workqueue, &con->swork); |
tsutomu.owa@toshiba.co.jp | 93eaade | 2017-09-12 09:01:55 +0000 | [diff] [blame] | 506 | out: |
| 507 | read_unlock_bh(&sk->sk_callback_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | static inline void lowcomms_connect_sock(struct connection *con) |
| 511 | { |
Lars Marowsky-Bree | 063c4c9 | 2009-08-11 16:18:23 -0500 | [diff] [blame] | 512 | if (test_bit(CF_CLOSE, &con->flags)) |
| 513 | return; |
Bob Peterson | 61d9102b | 2017-09-12 08:55:04 +0000 | [diff] [blame] | 514 | queue_work(send_workqueue, &con->swork); |
| 515 | cond_resched(); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | static void lowcomms_state_change(struct sock *sk) |
| 519 | { |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 520 | /* SCTP layer is not calling sk_data_ready when the connection |
| 521 | * is done, so we catch the signal through here. Also, it |
| 522 | * doesn't switch socket state when entering shutdown, so we |
| 523 | * skip the write in that case. |
| 524 | */ |
| 525 | if (sk->sk_shutdown) { |
| 526 | if (sk->sk_shutdown == RCV_SHUTDOWN) |
| 527 | lowcomms_data_ready(sk); |
| 528 | } else if (sk->sk_state == TCP_ESTABLISHED) { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 529 | lowcomms_write_space(sk); |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 530 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 531 | } |
| 532 | |
Christine Caulfield | 391fbdc | 2009-05-07 10:54:16 -0500 | [diff] [blame] | 533 | int dlm_lowcomms_connect_node(int nodeid) |
| 534 | { |
| 535 | struct connection *con; |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 536 | int idx; |
Christine Caulfield | 391fbdc | 2009-05-07 10:54:16 -0500 | [diff] [blame] | 537 | |
| 538 | if (nodeid == dlm_our_nodeid()) |
| 539 | return 0; |
| 540 | |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 541 | idx = srcu_read_lock(&connections_srcu); |
Christine Caulfield | 391fbdc | 2009-05-07 10:54:16 -0500 | [diff] [blame] | 542 | con = nodeid2con(nodeid, GFP_NOFS); |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 543 | if (!con) { |
| 544 | srcu_read_unlock(&connections_srcu, idx); |
Christine Caulfield | 391fbdc | 2009-05-07 10:54:16 -0500 | [diff] [blame] | 545 | return -ENOMEM; |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 546 | } |
| 547 | |
Christine Caulfield | 391fbdc | 2009-05-07 10:54:16 -0500 | [diff] [blame] | 548 | lowcomms_connect_sock(con); |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 549 | srcu_read_unlock(&connections_srcu, idx); |
| 550 | |
Christine Caulfield | 391fbdc | 2009-05-07 10:54:16 -0500 | [diff] [blame] | 551 | return 0; |
| 552 | } |
| 553 | |
Alexander Aring | e125fbe | 2021-03-01 17:05:09 -0500 | [diff] [blame] | 554 | int dlm_lowcomms_nodes_set_mark(int nodeid, unsigned int mark) |
| 555 | { |
| 556 | struct dlm_node_addr *na; |
| 557 | |
| 558 | spin_lock(&dlm_node_addrs_spin); |
| 559 | na = find_node_addr(nodeid); |
| 560 | if (!na) { |
| 561 | spin_unlock(&dlm_node_addrs_spin); |
| 562 | return -ENOENT; |
| 563 | } |
| 564 | |
| 565 | na->mark = mark; |
| 566 | spin_unlock(&dlm_node_addrs_spin); |
| 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 571 | static void lowcomms_error_report(struct sock *sk) |
| 572 | { |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 573 | struct connection *con; |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 574 | struct sockaddr_storage saddr; |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 575 | void (*orig_report)(struct sock *) = NULL; |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 576 | |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 577 | read_lock_bh(&sk->sk_callback_lock); |
| 578 | con = sock2con(sk); |
| 579 | if (con == NULL) |
| 580 | goto out; |
| 581 | |
Bob Peterson | cc661fc | 2017-09-12 08:55:23 +0000 | [diff] [blame] | 582 | orig_report = listen_sock.sk_error_report; |
Bob Peterson | 1a31833 | 2016-01-18 12:29:15 -0500 | [diff] [blame] | 583 | if (con->sock == NULL || |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 584 | kernel_getpeername(con->sock, (struct sockaddr *)&saddr) < 0) { |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 585 | printk_ratelimited(KERN_ERR "dlm: node %d: socket error " |
| 586 | "sending to node %d, port %d, " |
| 587 | "sk_err=%d/%d\n", dlm_our_nodeid(), |
| 588 | con->nodeid, dlm_config.ci_tcp_port, |
| 589 | sk->sk_err, sk->sk_err_soft); |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 590 | } else if (saddr.ss_family == AF_INET) { |
| 591 | struct sockaddr_in *sin4 = (struct sockaddr_in *)&saddr; |
| 592 | |
| 593 | printk_ratelimited(KERN_ERR "dlm: node %d: socket error " |
| 594 | "sending to node %d at %pI4, port %d, " |
| 595 | "sk_err=%d/%d\n", dlm_our_nodeid(), |
| 596 | con->nodeid, &sin4->sin_addr.s_addr, |
| 597 | dlm_config.ci_tcp_port, sk->sk_err, |
| 598 | sk->sk_err_soft); |
| 599 | } else { |
| 600 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&saddr; |
| 601 | |
| 602 | printk_ratelimited(KERN_ERR "dlm: node %d: socket error " |
| 603 | "sending to node %d at %u.%u.%u.%u, " |
| 604 | "port %d, sk_err=%d/%d\n", dlm_our_nodeid(), |
| 605 | con->nodeid, sin6->sin6_addr.s6_addr32[0], |
| 606 | sin6->sin6_addr.s6_addr32[1], |
| 607 | sin6->sin6_addr.s6_addr32[2], |
| 608 | sin6->sin6_addr.s6_addr32[3], |
| 609 | dlm_config.ci_tcp_port, sk->sk_err, |
| 610 | sk->sk_err_soft); |
| 611 | } |
Alexander Aring | ba868d9 | 2021-05-21 15:08:37 -0400 | [diff] [blame] | 612 | |
| 613 | /* below sendcon only handling */ |
| 614 | if (test_bit(CF_IS_OTHERCON, &con->flags)) |
| 615 | con = con->sendcon; |
| 616 | |
| 617 | switch (sk->sk_err) { |
| 618 | case ECONNREFUSED: |
| 619 | set_bit(CF_DELAY_CONNECT, &con->flags); |
| 620 | break; |
| 621 | default: |
| 622 | break; |
| 623 | } |
| 624 | |
| 625 | if (!test_and_set_bit(CF_RECONNECT, &con->flags)) |
| 626 | queue_work(send_workqueue, &con->swork); |
| 627 | |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 628 | out: |
| 629 | read_unlock_bh(&sk->sk_callback_lock); |
| 630 | if (orig_report) |
| 631 | orig_report(sk); |
| 632 | } |
| 633 | |
| 634 | /* Note: sk_callback_lock must be locked before calling this function. */ |
Bob Peterson | cc661fc | 2017-09-12 08:55:23 +0000 | [diff] [blame] | 635 | static void save_listen_callbacks(struct socket *sock) |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 636 | { |
Bob Peterson | cc661fc | 2017-09-12 08:55:23 +0000 | [diff] [blame] | 637 | struct sock *sk = sock->sk; |
| 638 | |
| 639 | listen_sock.sk_data_ready = sk->sk_data_ready; |
| 640 | listen_sock.sk_state_change = sk->sk_state_change; |
| 641 | listen_sock.sk_write_space = sk->sk_write_space; |
| 642 | listen_sock.sk_error_report = sk->sk_error_report; |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 643 | } |
| 644 | |
Bob Peterson | cc661fc | 2017-09-12 08:55:23 +0000 | [diff] [blame] | 645 | static void restore_callbacks(struct socket *sock) |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 646 | { |
Bob Peterson | cc661fc | 2017-09-12 08:55:23 +0000 | [diff] [blame] | 647 | struct sock *sk = sock->sk; |
| 648 | |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 649 | write_lock_bh(&sk->sk_callback_lock); |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 650 | sk->sk_user_data = NULL; |
Bob Peterson | cc661fc | 2017-09-12 08:55:23 +0000 | [diff] [blame] | 651 | sk->sk_data_ready = listen_sock.sk_data_ready; |
| 652 | sk->sk_state_change = listen_sock.sk_state_change; |
| 653 | sk->sk_write_space = listen_sock.sk_write_space; |
| 654 | sk->sk_error_report = listen_sock.sk_error_report; |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 655 | write_unlock_bh(&sk->sk_callback_lock); |
Bob Peterson | b3a5bbf | 2015-08-27 09:34:47 -0500 | [diff] [blame] | 656 | } |
| 657 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 658 | static void add_listen_sock(struct socket *sock, struct listen_connection *con) |
| 659 | { |
| 660 | struct sock *sk = sock->sk; |
| 661 | |
| 662 | write_lock_bh(&sk->sk_callback_lock); |
| 663 | save_listen_callbacks(sock); |
| 664 | con->sock = sock; |
| 665 | |
| 666 | sk->sk_user_data = con; |
| 667 | sk->sk_allocation = GFP_NOFS; |
| 668 | /* Install a data_ready callback */ |
| 669 | sk->sk_data_ready = lowcomms_listen_data_ready; |
| 670 | write_unlock_bh(&sk->sk_callback_lock); |
| 671 | } |
| 672 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 673 | /* Make a socket active */ |
tsutomu.owa@toshiba.co.jp | 988419a | 2017-09-12 08:55:32 +0000 | [diff] [blame] | 674 | static void add_sock(struct socket *sock, struct connection *con) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 675 | { |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 676 | struct sock *sk = sock->sk; |
| 677 | |
| 678 | write_lock_bh(&sk->sk_callback_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 679 | con->sock = sock; |
| 680 | |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 681 | sk->sk_user_data = con; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 682 | /* Install a data_ready callback */ |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 683 | sk->sk_data_ready = lowcomms_data_ready; |
| 684 | sk->sk_write_space = lowcomms_write_space; |
| 685 | sk->sk_state_change = lowcomms_state_change; |
| 686 | sk->sk_allocation = GFP_NOFS; |
| 687 | sk->sk_error_report = lowcomms_error_report; |
| 688 | write_unlock_bh(&sk->sk_callback_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | /* Add the port number to an IPv6 or 4 sockaddr and return the address |
| 692 | length */ |
| 693 | static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port, |
| 694 | int *addr_len) |
| 695 | { |
| 696 | saddr->ss_family = dlm_local_addr[0]->ss_family; |
| 697 | if (saddr->ss_family == AF_INET) { |
| 698 | struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr; |
| 699 | in4_addr->sin_port = cpu_to_be16(port); |
| 700 | *addr_len = sizeof(struct sockaddr_in); |
| 701 | memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero)); |
| 702 | } else { |
| 703 | struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr; |
| 704 | in6_addr->sin6_port = cpu_to_be16(port); |
| 705 | *addr_len = sizeof(struct sockaddr_in6); |
| 706 | } |
Patrick Caulfield | 01c8cab | 2007-07-17 16:53:15 +0100 | [diff] [blame] | 707 | memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 708 | } |
| 709 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 710 | static void dlm_close_sock(struct socket **sock) |
| 711 | { |
| 712 | if (*sock) { |
| 713 | restore_callbacks(*sock); |
| 714 | sock_release(*sock); |
| 715 | *sock = NULL; |
| 716 | } |
| 717 | } |
| 718 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 719 | /* Close a remote connection and tidy up */ |
Marcelo Ricardo Leitner | 0d737a8 | 2015-08-11 19:22:21 -0300 | [diff] [blame] | 720 | static void close_connection(struct connection *con, bool and_other, |
| 721 | bool tx, bool rx) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 722 | { |
tsutomu.owa@toshiba.co.jp | b2a6662 | 2017-09-12 08:55:50 +0000 | [diff] [blame] | 723 | bool closing = test_and_set_bit(CF_CLOSING, &con->flags); |
| 724 | |
tsutomu.owa@toshiba.co.jp | 0aa1846 | 2017-09-12 09:02:02 +0000 | [diff] [blame] | 725 | if (tx && !closing && cancel_work_sync(&con->swork)) { |
Marcelo Ricardo Leitner | 0d737a8 | 2015-08-11 19:22:21 -0300 | [diff] [blame] | 726 | log_print("canceled swork for node %d", con->nodeid); |
tsutomu.owa@toshiba.co.jp | 0aa1846 | 2017-09-12 09:02:02 +0000 | [diff] [blame] | 727 | clear_bit(CF_WRITE_PENDING, &con->flags); |
| 728 | } |
| 729 | if (rx && !closing && cancel_work_sync(&con->rwork)) { |
Marcelo Ricardo Leitner | 0d737a8 | 2015-08-11 19:22:21 -0300 | [diff] [blame] | 730 | log_print("canceled rwork for node %d", con->nodeid); |
tsutomu.owa@toshiba.co.jp | 0aa1846 | 2017-09-12 09:02:02 +0000 | [diff] [blame] | 731 | clear_bit(CF_READ_PENDING, &con->flags); |
| 732 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 733 | |
Marcelo Ricardo Leitner | 0d737a8 | 2015-08-11 19:22:21 -0300 | [diff] [blame] | 734 | mutex_lock(&con->sock_mutex); |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 735 | dlm_close_sock(&con->sock); |
| 736 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 737 | if (con->othercon && and_other) { |
| 738 | /* Will only re-enter once. */ |
Alexander Aring | c6aa00e3 | 2021-05-21 15:08:38 -0400 | [diff] [blame] | 739 | close_connection(con->othercon, false, tx, rx); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 740 | } |
Patrick Caulfield | 9e5f282 | 2007-08-02 14:58:14 +0100 | [diff] [blame] | 741 | |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 742 | con->rx_leftover = 0; |
Patrick Caulfield | 61d96be0 | 2007-08-20 15:13:38 +0100 | [diff] [blame] | 743 | con->retries = 0; |
Alexander Aring | 19633c7 | 2020-11-02 20:04:20 -0500 | [diff] [blame] | 744 | clear_bit(CF_CONNECTED, &con->flags); |
Alexander Aring | ba868d9 | 2021-05-21 15:08:37 -0400 | [diff] [blame] | 745 | clear_bit(CF_DELAY_CONNECT, &con->flags); |
| 746 | clear_bit(CF_RECONNECT, &con->flags); |
Alexander Aring | 8aa31cb | 2021-05-21 15:08:39 -0400 | [diff] [blame] | 747 | clear_bit(CF_EOF, &con->flags); |
Patrick Caulfield | 61d96be0 | 2007-08-20 15:13:38 +0100 | [diff] [blame] | 748 | mutex_unlock(&con->sock_mutex); |
tsutomu.owa@toshiba.co.jp | b2a6662 | 2017-09-12 08:55:50 +0000 | [diff] [blame] | 749 | clear_bit(CF_CLOSING, &con->flags); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 750 | } |
| 751 | |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 752 | static void shutdown_connection(struct connection *con) |
| 753 | { |
| 754 | int ret; |
| 755 | |
Alexander Aring | eec054b | 2021-03-01 17:05:19 -0500 | [diff] [blame] | 756 | flush_work(&con->swork); |
Alexander Aring | 055923b | 2020-07-27 09:13:38 -0400 | [diff] [blame] | 757 | |
| 758 | mutex_lock(&con->sock_mutex); |
| 759 | /* nothing to shutdown */ |
| 760 | if (!con->sock) { |
| 761 | mutex_unlock(&con->sock_mutex); |
| 762 | return; |
| 763 | } |
| 764 | |
| 765 | set_bit(CF_SHUTDOWN, &con->flags); |
| 766 | ret = kernel_sock_shutdown(con->sock, SHUT_WR); |
| 767 | mutex_unlock(&con->sock_mutex); |
| 768 | if (ret) { |
| 769 | log_print("Connection %p failed to shutdown: %d will force close", |
| 770 | con, ret); |
| 771 | goto force_close; |
| 772 | } else { |
| 773 | ret = wait_event_timeout(con->shutdown_wait, |
| 774 | !test_bit(CF_SHUTDOWN, &con->flags), |
| 775 | DLM_SHUTDOWN_WAIT_TIMEOUT); |
| 776 | if (ret == 0) { |
| 777 | log_print("Connection %p shutdown timed out, will force close", |
| 778 | con); |
| 779 | goto force_close; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | return; |
| 784 | |
| 785 | force_close: |
| 786 | clear_bit(CF_SHUTDOWN, &con->flags); |
| 787 | close_connection(con, false, true, true); |
| 788 | } |
| 789 | |
| 790 | static void dlm_tcp_shutdown(struct connection *con) |
| 791 | { |
| 792 | if (con->othercon) |
| 793 | shutdown_connection(con->othercon); |
| 794 | shutdown_connection(con); |
| 795 | } |
| 796 | |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 797 | static int con_realloc_receive_buf(struct connection *con, int newlen) |
| 798 | { |
| 799 | unsigned char *newbuf; |
| 800 | |
| 801 | newbuf = kmalloc(newlen, GFP_NOFS); |
| 802 | if (!newbuf) |
| 803 | return -ENOMEM; |
| 804 | |
| 805 | /* copy any leftover from last receive */ |
| 806 | if (con->rx_leftover) |
| 807 | memmove(newbuf, con->rx_buf, con->rx_leftover); |
| 808 | |
| 809 | /* swap to new buffer space */ |
| 810 | kfree(con->rx_buf); |
| 811 | con->rx_buflen = newlen; |
| 812 | con->rx_buf = newbuf; |
| 813 | |
| 814 | return 0; |
| 815 | } |
| 816 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 817 | /* Data received from remote end */ |
| 818 | static int receive_from_sock(struct connection *con) |
| 819 | { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 820 | int call_again_soon = 0; |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 821 | struct msghdr msg; |
| 822 | struct kvec iov; |
| 823 | int ret, buflen; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 824 | |
| 825 | mutex_lock(&con->sock_mutex); |
| 826 | |
| 827 | if (con->sock == NULL) { |
| 828 | ret = -EAGAIN; |
| 829 | goto out_close; |
| 830 | } |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 831 | |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 832 | /* realloc if we get new buffer size to read out */ |
| 833 | buflen = dlm_config.ci_buffer_size; |
| 834 | if (con->rx_buflen != buflen && con->rx_leftover <= buflen) { |
| 835 | ret = con_realloc_receive_buf(con, buflen); |
| 836 | if (ret < 0) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 837 | goto out_resched; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 838 | } |
| 839 | |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 840 | /* calculate new buffer parameter regarding last receive and |
| 841 | * possible leftover bytes |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 842 | */ |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 843 | iov.iov_base = con->rx_buf + con->rx_leftover; |
| 844 | iov.iov_len = con->rx_buflen - con->rx_leftover; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 845 | |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 846 | memset(&msg, 0, sizeof(msg)); |
| 847 | msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL; |
| 848 | ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len, |
| 849 | msg.msg_flags); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 850 | if (ret <= 0) |
| 851 | goto out_close; |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 852 | else if (ret == iov.iov_len) |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 853 | call_again_soon = 1; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 854 | |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 855 | /* new buflen according readed bytes and leftover from last receive */ |
| 856 | buflen = ret + con->rx_leftover; |
| 857 | ret = dlm_process_incoming_buffer(con->nodeid, con->rx_buf, buflen); |
| 858 | if (ret < 0) |
| 859 | goto out_close; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 860 | |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 861 | /* calculate leftover bytes from process and put it into begin of |
| 862 | * the receive buffer, so next receive we have the full message |
| 863 | * at the start address of the receive buffer. |
| 864 | */ |
| 865 | con->rx_leftover = buflen - ret; |
| 866 | if (con->rx_leftover) { |
| 867 | memmove(con->rx_buf, con->rx_buf + ret, |
| 868 | con->rx_leftover); |
| 869 | call_again_soon = true; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | if (call_again_soon) |
| 873 | goto out_resched; |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 874 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 875 | mutex_unlock(&con->sock_mutex); |
| 876 | return 0; |
| 877 | |
| 878 | out_resched: |
| 879 | if (!test_and_set_bit(CF_READ_PENDING, &con->flags)) |
| 880 | queue_work(recv_workqueue, &con->rwork); |
| 881 | mutex_unlock(&con->sock_mutex); |
| 882 | return -EAGAIN; |
| 883 | |
| 884 | out_close: |
Alexander Aring | ba868d9 | 2021-05-21 15:08:37 -0400 | [diff] [blame] | 885 | if (ret == 0) { |
Alexander Aring | ba868d9 | 2021-05-21 15:08:37 -0400 | [diff] [blame] | 886 | log_print("connection %p got EOF from %d", |
| 887 | con, con->nodeid); |
Alexander Aring | 8aa31cb | 2021-05-21 15:08:39 -0400 | [diff] [blame] | 888 | |
| 889 | if (con->eof_condition && con->eof_condition(con)) { |
| 890 | set_bit(CF_EOF, &con->flags); |
| 891 | mutex_unlock(&con->sock_mutex); |
| 892 | } else { |
| 893 | mutex_unlock(&con->sock_mutex); |
| 894 | close_connection(con, false, true, false); |
| 895 | |
| 896 | /* handling for tcp shutdown */ |
| 897 | clear_bit(CF_SHUTDOWN, &con->flags); |
| 898 | wake_up(&con->shutdown_wait); |
| 899 | } |
| 900 | |
Alexander Aring | ba868d9 | 2021-05-21 15:08:37 -0400 | [diff] [blame] | 901 | /* signal to breaking receive worker */ |
| 902 | ret = -1; |
Alexander Aring | 8aa31cb | 2021-05-21 15:08:39 -0400 | [diff] [blame] | 903 | } else { |
| 904 | mutex_unlock(&con->sock_mutex); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 905 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 906 | return ret; |
| 907 | } |
| 908 | |
| 909 | /* Listening socket is busy, accept a connection */ |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 910 | static int accept_from_sock(struct listen_connection *con) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 911 | { |
| 912 | int result; |
| 913 | struct sockaddr_storage peeraddr; |
| 914 | struct socket *newsock; |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 915 | int len, idx; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 916 | int nodeid; |
| 917 | struct connection *newcon; |
| 918 | struct connection *addcon; |
Alexander Aring | 3f78cd7 | 2020-09-24 10:31:23 -0400 | [diff] [blame] | 919 | unsigned int mark; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 920 | |
David Teigland | 513ef59 | 2012-03-30 11:46:08 -0500 | [diff] [blame] | 921 | if (!dlm_allow_conn) { |
David Teigland | 513ef59 | 2012-03-30 11:46:08 -0500 | [diff] [blame] | 922 | return -1; |
| 923 | } |
David Teigland | 513ef59 | 2012-03-30 11:46:08 -0500 | [diff] [blame] | 924 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 925 | if (!con->sock) |
tsutomu.owa@toshiba.co.jp | 3421fb1 | 2017-09-12 09:01:38 +0000 | [diff] [blame] | 926 | return -ENOTCONN; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 927 | |
tsutomu.owa@toshiba.co.jp | 3421fb1 | 2017-09-12 09:01:38 +0000 | [diff] [blame] | 928 | result = kernel_accept(con->sock, &newsock, O_NONBLOCK); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 929 | if (result < 0) |
| 930 | goto accept_err; |
| 931 | |
| 932 | /* Get the connected socket's peer */ |
| 933 | memset(&peeraddr, 0, sizeof(peeraddr)); |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 934 | len = newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr, 2); |
| 935 | if (len < 0) { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 936 | result = -ECONNABORTED; |
| 937 | goto accept_err; |
| 938 | } |
| 939 | |
| 940 | /* Get the new node's NODEID */ |
| 941 | make_sockaddr(&peeraddr, 0, &len); |
Alexander Aring | e125fbe | 2021-03-01 17:05:09 -0500 | [diff] [blame] | 942 | if (addr_to_nodeid(&peeraddr, &nodeid, &mark)) { |
Masatake YAMATO | bcaadf5 | 2011-07-04 12:25:51 +0900 | [diff] [blame] | 943 | unsigned char *b=(unsigned char *)&peeraddr; |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 944 | log_print("connect from non cluster node"); |
Masatake YAMATO | bcaadf5 | 2011-07-04 12:25:51 +0900 | [diff] [blame] | 945 | print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE, |
| 946 | b, sizeof(struct sockaddr_storage)); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 947 | sock_release(newsock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 948 | return -1; |
| 949 | } |
| 950 | |
| 951 | log_print("got connection from %d", nodeid); |
| 952 | |
| 953 | /* Check to see if we already have a connection to this node. This |
| 954 | * could happen if the two nodes initiate a connection at roughly |
| 955 | * the same time and the connections cross on the wire. |
| 956 | * In this case we store the incoming one in "othercon" |
| 957 | */ |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 958 | idx = srcu_read_lock(&connections_srcu); |
David Teigland | 748285c | 2009-05-15 10:50:57 -0500 | [diff] [blame] | 959 | newcon = nodeid2con(nodeid, GFP_NOFS); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 960 | if (!newcon) { |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 961 | srcu_read_unlock(&connections_srcu, idx); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 962 | result = -ENOMEM; |
| 963 | goto accept_err; |
| 964 | } |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 965 | |
Alexander Aring | e125fbe | 2021-03-01 17:05:09 -0500 | [diff] [blame] | 966 | sock_set_mark(newsock->sk, mark); |
| 967 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 968 | mutex_lock(&newcon->sock_mutex); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 969 | if (newcon->sock) { |
| 970 | struct connection *othercon = newcon->othercon; |
| 971 | |
| 972 | if (!othercon) { |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 973 | othercon = kzalloc(sizeof(*othercon), GFP_NOFS); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 974 | if (!othercon) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 975 | log_print("failed to allocate incoming socket"); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 976 | mutex_unlock(&newcon->sock_mutex); |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 977 | srcu_read_unlock(&connections_srcu, idx); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 978 | result = -ENOMEM; |
| 979 | goto accept_err; |
| 980 | } |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 981 | |
Alexander Aring | 6cde210 | 2020-11-02 20:04:21 -0500 | [diff] [blame] | 982 | result = dlm_con_init(othercon, nodeid); |
| 983 | if (result < 0) { |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 984 | kfree(othercon); |
Yang Yingliang | 2fd8db2 | 2021-03-27 16:37:04 +0800 | [diff] [blame] | 985 | mutex_unlock(&newcon->sock_mutex); |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 986 | srcu_read_unlock(&connections_srcu, idx); |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 987 | goto accept_err; |
| 988 | } |
| 989 | |
Alexander Aring | e9a470a | 2021-03-01 17:05:11 -0500 | [diff] [blame] | 990 | lockdep_set_subclass(&othercon->sock_mutex, 1); |
Alexander Aring | 7443bc9 | 2021-05-21 15:08:36 -0400 | [diff] [blame] | 991 | set_bit(CF_IS_OTHERCON, &othercon->flags); |
Alexander Aring | 6cde210 | 2020-11-02 20:04:21 -0500 | [diff] [blame] | 992 | newcon->othercon = othercon; |
Alexander Aring | ba868d9 | 2021-05-21 15:08:37 -0400 | [diff] [blame] | 993 | othercon->sendcon = newcon; |
Alexander Aring | ba3ab3c | 2020-07-27 09:13:37 -0400 | [diff] [blame] | 994 | } else { |
| 995 | /* close other sock con if we have something new */ |
| 996 | close_connection(othercon, false, true, false); |
Patrick Caulfield | 61d96be0 | 2007-08-20 15:13:38 +0100 | [diff] [blame] | 997 | } |
Alexander Aring | ba3ab3c | 2020-07-27 09:13:37 -0400 | [diff] [blame] | 998 | |
Alexander Aring | e9a470a | 2021-03-01 17:05:11 -0500 | [diff] [blame] | 999 | mutex_lock(&othercon->sock_mutex); |
Alexander Aring | ba3ab3c | 2020-07-27 09:13:37 -0400 | [diff] [blame] | 1000 | add_sock(newsock, othercon); |
| 1001 | addcon = othercon; |
| 1002 | mutex_unlock(&othercon->sock_mutex); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1003 | } |
| 1004 | else { |
Bob Peterson | 3735b4b | 2016-09-23 14:23:26 -0400 | [diff] [blame] | 1005 | /* accept copies the sk after we've saved the callbacks, so we |
| 1006 | don't want to save them a second time or comm errors will |
| 1007 | result in calling sk_error_report recursively. */ |
tsutomu.owa@toshiba.co.jp | 988419a | 2017-09-12 08:55:32 +0000 | [diff] [blame] | 1008 | add_sock(newsock, newcon); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1009 | addcon = newcon; |
| 1010 | } |
| 1011 | |
Alexander Aring | b30a624 | 2021-03-01 17:05:10 -0500 | [diff] [blame] | 1012 | set_bit(CF_CONNECTED, &addcon->flags); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1013 | mutex_unlock(&newcon->sock_mutex); |
| 1014 | |
| 1015 | /* |
| 1016 | * Add it to the active queue in case we got data |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1017 | * between processing the accept adding the socket |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1018 | * to the read_sockets list |
| 1019 | */ |
| 1020 | if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags)) |
| 1021 | queue_work(recv_workqueue, &addcon->rwork); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1022 | |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1023 | srcu_read_unlock(&connections_srcu, idx); |
| 1024 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1025 | return 0; |
| 1026 | |
| 1027 | accept_err: |
tsutomu.owa@toshiba.co.jp | 3421fb1 | 2017-09-12 09:01:38 +0000 | [diff] [blame] | 1028 | if (newsock) |
| 1029 | sock_release(newsock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1030 | |
| 1031 | if (result != -EAGAIN) |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1032 | log_print("error accepting connection from node: %d", result); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1033 | return result; |
| 1034 | } |
| 1035 | |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame^] | 1036 | static void dlm_page_release(struct kref *kref) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1037 | { |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame^] | 1038 | struct writequeue_entry *e = container_of(kref, struct writequeue_entry, |
| 1039 | ref); |
| 1040 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1041 | __free_page(e->page); |
| 1042 | kfree(e); |
| 1043 | } |
| 1044 | |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame^] | 1045 | static void dlm_msg_release(struct kref *kref) |
| 1046 | { |
| 1047 | struct dlm_msg *msg = container_of(kref, struct dlm_msg, ref); |
| 1048 | |
| 1049 | kref_put(&msg->entry->ref, dlm_page_release); |
| 1050 | kfree(msg); |
| 1051 | } |
| 1052 | |
| 1053 | static void free_entry(struct writequeue_entry *e) |
| 1054 | { |
| 1055 | struct dlm_msg *msg, *tmp; |
| 1056 | |
| 1057 | list_for_each_entry_safe(msg, tmp, &e->msgs, list) { |
| 1058 | list_del(&msg->list); |
| 1059 | kref_put(&msg->ref, dlm_msg_release); |
| 1060 | } |
| 1061 | |
| 1062 | list_del(&e->list); |
| 1063 | atomic_dec(&e->con->writequeue_cnt); |
| 1064 | kref_put(&e->ref, dlm_page_release); |
| 1065 | } |
| 1066 | |
Mike Christie | 5d68987 | 2013-06-14 04:56:13 -0500 | [diff] [blame] | 1067 | /* |
| 1068 | * writequeue_entry_complete - try to delete and free write queue entry |
| 1069 | * @e: write queue entry to try to delete |
| 1070 | * @completed: bytes completed |
| 1071 | * |
| 1072 | * writequeue_lock must be held. |
| 1073 | */ |
| 1074 | static void writequeue_entry_complete(struct writequeue_entry *e, int completed) |
| 1075 | { |
| 1076 | e->offset += completed; |
| 1077 | e->len -= completed; |
| 1078 | |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame^] | 1079 | if (e->len == 0 && e->users == 0) |
Mike Christie | 5d68987 | 2013-06-14 04:56:13 -0500 | [diff] [blame] | 1080 | free_entry(e); |
Mike Christie | 5d68987 | 2013-06-14 04:56:13 -0500 | [diff] [blame] | 1081 | } |
| 1082 | |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1083 | /* |
| 1084 | * sctp_bind_addrs - bind a SCTP socket to all our addresses |
| 1085 | */ |
Alexander Aring | 13004e8 | 2020-11-02 20:04:24 -0500 | [diff] [blame] | 1086 | static int sctp_bind_addrs(struct socket *sock, uint16_t port) |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1087 | { |
| 1088 | struct sockaddr_storage localaddr; |
Christoph Hellwig | c0425a4 | 2020-05-29 14:09:42 +0200 | [diff] [blame] | 1089 | struct sockaddr *addr = (struct sockaddr *)&localaddr; |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1090 | int i, addr_len, result = 0; |
| 1091 | |
| 1092 | for (i = 0; i < dlm_local_count; i++) { |
| 1093 | memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr)); |
| 1094 | make_sockaddr(&localaddr, port, &addr_len); |
| 1095 | |
| 1096 | if (!i) |
Alexander Aring | 13004e8 | 2020-11-02 20:04:24 -0500 | [diff] [blame] | 1097 | result = kernel_bind(sock, addr, addr_len); |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1098 | else |
Alexander Aring | 13004e8 | 2020-11-02 20:04:24 -0500 | [diff] [blame] | 1099 | result = sock_bind_add(sock->sk, addr, addr_len); |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1100 | |
| 1101 | if (result < 0) { |
| 1102 | log_print("Can't bind to %d addr number %d, %d.\n", |
| 1103 | port, i + 1, result); |
| 1104 | break; |
| 1105 | } |
| 1106 | } |
| 1107 | return result; |
| 1108 | } |
| 1109 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1110 | /* Initiate an SCTP association. |
| 1111 | This is a special case of send_to_sock() in that we don't yet have a |
| 1112 | peeled-off socket for this association, so we use the listening socket |
| 1113 | and add the primary IP address of the remote node. |
| 1114 | */ |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1115 | static void sctp_connect_to_sock(struct connection *con) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1116 | { |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1117 | struct sockaddr_storage daddr; |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1118 | int result; |
| 1119 | int addr_len; |
| 1120 | struct socket *sock; |
Alexander Aring | 9c9f168 | 2020-06-26 13:26:50 -0400 | [diff] [blame] | 1121 | unsigned int mark; |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1122 | |
Mike Christie | 5d68987 | 2013-06-14 04:56:13 -0500 | [diff] [blame] | 1123 | mutex_lock(&con->sock_mutex); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1124 | |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1125 | /* Some odd races can cause double-connects, ignore them */ |
| 1126 | if (con->retries++ > MAX_CONNECT_RETRIES) |
| 1127 | goto out; |
| 1128 | |
| 1129 | if (con->sock) { |
| 1130 | log_print("node %d already connected.", con->nodeid); |
| 1131 | goto out; |
| 1132 | } |
| 1133 | |
| 1134 | memset(&daddr, 0, sizeof(daddr)); |
Alexander Aring | e125fbe | 2021-03-01 17:05:09 -0500 | [diff] [blame] | 1135 | result = nodeid_to_addr(con->nodeid, &daddr, NULL, true, &mark); |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1136 | if (result < 0) { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1137 | log_print("no address for nodeid %d", con->nodeid); |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1138 | goto out; |
David Teigland | 04bedd7 | 2009-09-18 14:31:47 -0500 | [diff] [blame] | 1139 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1140 | |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1141 | /* Create a socket to communicate with */ |
| 1142 | result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family, |
| 1143 | SOCK_STREAM, IPPROTO_SCTP, &sock); |
| 1144 | if (result < 0) |
| 1145 | goto socket_err; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1146 | |
Alexander Aring | 9c9f168 | 2020-06-26 13:26:50 -0400 | [diff] [blame] | 1147 | sock_set_mark(sock->sk, mark); |
| 1148 | |
tsutomu.owa@toshiba.co.jp | 988419a | 2017-09-12 08:55:32 +0000 | [diff] [blame] | 1149 | add_sock(sock, con); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1150 | |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1151 | /* Bind to all addresses. */ |
Alexander Aring | 13004e8 | 2020-11-02 20:04:24 -0500 | [diff] [blame] | 1152 | if (sctp_bind_addrs(con->sock, 0)) |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1153 | goto bind_err; |
Mike Christie | 98e1b60 | 2013-06-14 04:56:12 -0500 | [diff] [blame] | 1154 | |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1155 | make_sockaddr(&daddr, dlm_config.ci_tcp_port, &addr_len); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1156 | |
Alexander Aring | 2df6b76 | 2021-05-21 15:08:34 -0400 | [diff] [blame] | 1157 | log_print_ratelimited("connecting to %d", con->nodeid); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1158 | |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1159 | /* Turn off Nagle's algorithm */ |
Christoph Hellwig | 40ef92c | 2020-05-29 14:09:40 +0200 | [diff] [blame] | 1160 | sctp_sock_set_nodelay(sock->sk); |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1161 | |
Gang He | f706d83 | 2018-05-02 10:28:35 -0500 | [diff] [blame] | 1162 | /* |
| 1163 | * Make sock->ops->connect() function return in specified time, |
| 1164 | * since O_NONBLOCK argument in connect() function does not work here, |
| 1165 | * then, we should restore the default value of this attribute. |
| 1166 | */ |
Christoph Hellwig | 76ee078 | 2020-05-28 07:12:12 +0200 | [diff] [blame] | 1167 | sock_set_sndtimeo(sock->sk, 5); |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1168 | result = sock->ops->connect(sock, (struct sockaddr *)&daddr, addr_len, |
Gang He | da3627c | 2018-05-29 11:09:22 +0800 | [diff] [blame] | 1169 | 0); |
Christoph Hellwig | 76ee078 | 2020-05-28 07:12:12 +0200 | [diff] [blame] | 1170 | sock_set_sndtimeo(sock->sk, 0); |
Gang He | f706d83 | 2018-05-02 10:28:35 -0500 | [diff] [blame] | 1171 | |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1172 | if (result == -EINPROGRESS) |
| 1173 | result = 0; |
Alexander Aring | 19633c7 | 2020-11-02 20:04:20 -0500 | [diff] [blame] | 1174 | if (result == 0) { |
| 1175 | if (!test_and_set_bit(CF_CONNECTED, &con->flags)) |
| 1176 | log_print("successful connected to node %d", con->nodeid); |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1177 | goto out; |
Alexander Aring | 19633c7 | 2020-11-02 20:04:20 -0500 | [diff] [blame] | 1178 | } |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1179 | |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1180 | bind_err: |
| 1181 | con->sock = NULL; |
| 1182 | sock_release(sock); |
| 1183 | |
| 1184 | socket_err: |
| 1185 | /* |
| 1186 | * Some errors are fatal and this list might need adjusting. For other |
| 1187 | * errors we try again until the max number of retries is reached. |
| 1188 | */ |
| 1189 | if (result != -EHOSTUNREACH && |
| 1190 | result != -ENETUNREACH && |
| 1191 | result != -ENETDOWN && |
| 1192 | result != -EINVAL && |
| 1193 | result != -EPROTONOSUPPORT) { |
| 1194 | log_print("connect %d try %d error %d", con->nodeid, |
| 1195 | con->retries, result); |
| 1196 | mutex_unlock(&con->sock_mutex); |
| 1197 | msleep(1000); |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1198 | lowcomms_connect_sock(con); |
| 1199 | return; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1200 | } |
Mike Christie | 5d68987 | 2013-06-14 04:56:13 -0500 | [diff] [blame] | 1201 | |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1202 | out: |
Mike Christie | 5d68987 | 2013-06-14 04:56:13 -0500 | [diff] [blame] | 1203 | mutex_unlock(&con->sock_mutex); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | /* Connect a new socket to its peer */ |
| 1207 | static void tcp_connect_to_sock(struct connection *con) |
| 1208 | { |
Lon Hohberger | 6bd8fed | 2007-10-25 18:51:54 -0400 | [diff] [blame] | 1209 | struct sockaddr_storage saddr, src_addr; |
Alexander Aring | e125fbe | 2021-03-01 17:05:09 -0500 | [diff] [blame] | 1210 | unsigned int mark; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1211 | int addr_len; |
Casey Dahlin | a89d63a | 2009-07-14 12:17:51 -0500 | [diff] [blame] | 1212 | struct socket *sock = NULL; |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 1213 | int result; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1214 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1215 | mutex_lock(&con->sock_mutex); |
| 1216 | if (con->retries++ > MAX_CONNECT_RETRIES) |
| 1217 | goto out; |
| 1218 | |
| 1219 | /* Some odd races can cause double-connects, ignore them */ |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 1220 | if (con->sock) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1221 | goto out; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1222 | |
| 1223 | /* Create a socket to communicate with */ |
Eric W. Biederman | eeb1bd5 | 2015-05-08 21:08:05 -0500 | [diff] [blame] | 1224 | result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family, |
| 1225 | SOCK_STREAM, IPPROTO_TCP, &sock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1226 | if (result < 0) |
| 1227 | goto out_err; |
| 1228 | |
| 1229 | memset(&saddr, 0, sizeof(saddr)); |
Alexander Aring | e125fbe | 2021-03-01 17:05:09 -0500 | [diff] [blame] | 1230 | result = nodeid_to_addr(con->nodeid, &saddr, NULL, false, &mark); |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 1231 | if (result < 0) { |
| 1232 | log_print("no address for nodeid %d", con->nodeid); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1233 | goto out_err; |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 1234 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1235 | |
Alexander Aring | e125fbe | 2021-03-01 17:05:09 -0500 | [diff] [blame] | 1236 | sock_set_mark(sock->sk, mark); |
| 1237 | |
tsutomu.owa@toshiba.co.jp | 988419a | 2017-09-12 08:55:32 +0000 | [diff] [blame] | 1238 | add_sock(sock, con); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1239 | |
Lon Hohberger | 6bd8fed | 2007-10-25 18:51:54 -0400 | [diff] [blame] | 1240 | /* Bind to our cluster-known address connecting to avoid |
| 1241 | routing problems */ |
| 1242 | memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr)); |
| 1243 | make_sockaddr(&src_addr, 0, &addr_len); |
| 1244 | result = sock->ops->bind(sock, (struct sockaddr *) &src_addr, |
| 1245 | addr_len); |
| 1246 | if (result < 0) { |
| 1247 | log_print("could not bind for connect: %d", result); |
| 1248 | /* This *may* not indicate a critical error */ |
| 1249 | } |
| 1250 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1251 | make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len); |
| 1252 | |
Alexander Aring | 2df6b76 | 2021-05-21 15:08:34 -0400 | [diff] [blame] | 1253 | log_print_ratelimited("connecting to %d", con->nodeid); |
David Teigland | cb2d45d | 2010-11-12 11:12:55 -0600 | [diff] [blame] | 1254 | |
| 1255 | /* Turn off Nagle's algorithm */ |
Christoph Hellwig | 12abc5e | 2020-05-28 07:12:19 +0200 | [diff] [blame] | 1256 | tcp_sock_set_nodelay(sock->sk); |
David Teigland | cb2d45d | 2010-11-12 11:12:55 -0600 | [diff] [blame] | 1257 | |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 1258 | result = sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len, |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1259 | O_NONBLOCK); |
| 1260 | if (result == -EINPROGRESS) |
| 1261 | result = 0; |
| 1262 | if (result == 0) |
| 1263 | goto out; |
| 1264 | |
| 1265 | out_err: |
| 1266 | if (con->sock) { |
| 1267 | sock_release(con->sock); |
| 1268 | con->sock = NULL; |
Casey Dahlin | a89d63a | 2009-07-14 12:17:51 -0500 | [diff] [blame] | 1269 | } else if (sock) { |
| 1270 | sock_release(sock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1271 | } |
| 1272 | /* |
| 1273 | * Some errors are fatal and this list might need adjusting. For other |
| 1274 | * errors we try again until the max number of retries is reached. |
| 1275 | */ |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 1276 | if (result != -EHOSTUNREACH && |
| 1277 | result != -ENETUNREACH && |
| 1278 | result != -ENETDOWN && |
| 1279 | result != -EINVAL && |
| 1280 | result != -EPROTONOSUPPORT) { |
| 1281 | log_print("connect %d try %d error %d", con->nodeid, |
| 1282 | con->retries, result); |
| 1283 | mutex_unlock(&con->sock_mutex); |
| 1284 | msleep(1000); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1285 | lowcomms_connect_sock(con); |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 1286 | return; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1287 | } |
| 1288 | out: |
| 1289 | mutex_unlock(&con->sock_mutex); |
| 1290 | return; |
| 1291 | } |
| 1292 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 1293 | /* On error caller must run dlm_close_sock() for the |
| 1294 | * listen connection socket. |
| 1295 | */ |
| 1296 | static int tcp_create_listen_sock(struct listen_connection *con, |
| 1297 | struct sockaddr_storage *saddr) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1298 | { |
| 1299 | struct socket *sock = NULL; |
| 1300 | int result = 0; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1301 | int addr_len; |
| 1302 | |
| 1303 | if (dlm_local_addr[0]->ss_family == AF_INET) |
| 1304 | addr_len = sizeof(struct sockaddr_in); |
| 1305 | else |
| 1306 | addr_len = sizeof(struct sockaddr_in6); |
| 1307 | |
| 1308 | /* Create a socket to communicate with */ |
Eric W. Biederman | eeb1bd5 | 2015-05-08 21:08:05 -0500 | [diff] [blame] | 1309 | result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family, |
| 1310 | SOCK_STREAM, IPPROTO_TCP, &sock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1311 | if (result < 0) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1312 | log_print("Can't create listening comms socket"); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1313 | goto create_out; |
| 1314 | } |
| 1315 | |
Alexander Aring | a5b7ab6 | 2020-06-26 13:26:49 -0400 | [diff] [blame] | 1316 | sock_set_mark(sock->sk, dlm_config.ci_mark); |
| 1317 | |
David Teigland | cb2d45d | 2010-11-12 11:12:55 -0600 | [diff] [blame] | 1318 | /* Turn off Nagle's algorithm */ |
Christoph Hellwig | 12abc5e | 2020-05-28 07:12:19 +0200 | [diff] [blame] | 1319 | tcp_sock_set_nodelay(sock->sk); |
David Teigland | cb2d45d | 2010-11-12 11:12:55 -0600 | [diff] [blame] | 1320 | |
Christoph Hellwig | b58f0e8 | 2020-05-28 07:12:09 +0200 | [diff] [blame] | 1321 | sock_set_reuseaddr(sock->sk); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1322 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 1323 | add_listen_sock(sock, con); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1324 | |
| 1325 | /* Bind to our port */ |
| 1326 | make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len); |
| 1327 | result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len); |
| 1328 | if (result < 0) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1329 | log_print("Can't bind to port %d", dlm_config.ci_tcp_port); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1330 | goto create_out; |
| 1331 | } |
Christoph Hellwig | ce3d954 | 2020-05-28 07:12:15 +0200 | [diff] [blame] | 1332 | sock_set_keepalive(sock->sk); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1333 | |
| 1334 | result = sock->ops->listen(sock, 5); |
| 1335 | if (result < 0) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1336 | log_print("Can't listen on port %d", dlm_config.ci_tcp_port); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1337 | goto create_out; |
| 1338 | } |
| 1339 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 1340 | return 0; |
| 1341 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1342 | create_out: |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 1343 | return result; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | /* Get local addresses */ |
| 1347 | static void init_local(void) |
| 1348 | { |
| 1349 | struct sockaddr_storage sas, *addr; |
| 1350 | int i; |
| 1351 | |
Patrick Caulfield | 30d3a23 | 2007-04-23 16:26:21 +0100 | [diff] [blame] | 1352 | dlm_local_count = 0; |
David Teigland | 1b189b8 | 2012-03-21 09:18:34 -0500 | [diff] [blame] | 1353 | for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1354 | if (dlm_our_addr(&sas, i)) |
| 1355 | break; |
| 1356 | |
Amitoj Kaur Chawla | 5c93f56 | 2016-06-23 10:22:01 +0530 | [diff] [blame] | 1357 | addr = kmemdup(&sas, sizeof(*addr), GFP_NOFS); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1358 | if (!addr) |
| 1359 | break; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1360 | dlm_local_addr[dlm_local_count++] = addr; |
| 1361 | } |
| 1362 | } |
| 1363 | |
Alexander Aring | 043697f | 2020-08-27 15:02:50 -0400 | [diff] [blame] | 1364 | static void deinit_local(void) |
| 1365 | { |
| 1366 | int i; |
| 1367 | |
| 1368 | for (i = 0; i < dlm_local_count; i++) |
| 1369 | kfree(dlm_local_addr[i]); |
| 1370 | } |
| 1371 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 1372 | /* Initialise SCTP socket and bind to all interfaces |
| 1373 | * On error caller must run dlm_close_sock() for the |
| 1374 | * listen connection socket. |
| 1375 | */ |
| 1376 | static int sctp_listen_for_all(struct listen_connection *con) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1377 | { |
| 1378 | struct socket *sock = NULL; |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1379 | int result = -EINVAL; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1380 | |
| 1381 | log_print("Using SCTP for communications"); |
| 1382 | |
Eric W. Biederman | eeb1bd5 | 2015-05-08 21:08:05 -0500 | [diff] [blame] | 1383 | result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family, |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1384 | SOCK_STREAM, IPPROTO_SCTP, &sock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1385 | if (result < 0) { |
| 1386 | log_print("Can't create comms socket, check SCTP is loaded"); |
| 1387 | goto out; |
| 1388 | } |
| 1389 | |
Christoph Hellwig | 26cfabf | 2020-05-28 07:12:16 +0200 | [diff] [blame] | 1390 | sock_set_rcvbuf(sock->sk, NEEDED_RMEM); |
Alexander Aring | a5b7ab6 | 2020-06-26 13:26:49 -0400 | [diff] [blame] | 1391 | sock_set_mark(sock->sk, dlm_config.ci_mark); |
Christoph Hellwig | 40ef92c | 2020-05-29 14:09:40 +0200 | [diff] [blame] | 1392 | sctp_sock_set_nodelay(sock->sk); |
Mike Christie | 86e92ad | 2013-06-14 04:56:14 -0500 | [diff] [blame] | 1393 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 1394 | add_listen_sock(sock, con); |
Bob Peterson | b81171c | 2016-02-05 14:39:02 -0500 | [diff] [blame] | 1395 | |
Marcelo Ricardo Leitner | ee44b4b | 2015-08-11 19:22:23 -0300 | [diff] [blame] | 1396 | /* Bind to all addresses. */ |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 1397 | result = sctp_bind_addrs(con->sock, dlm_config.ci_tcp_port); |
| 1398 | if (result < 0) |
| 1399 | goto out; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1400 | |
| 1401 | result = sock->ops->listen(sock, 5); |
| 1402 | if (result < 0) { |
| 1403 | log_print("Can't set socket listening"); |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 1404 | goto out; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | return 0; |
| 1408 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1409 | out: |
| 1410 | return result; |
| 1411 | } |
| 1412 | |
| 1413 | static int tcp_listen_for_all(void) |
| 1414 | { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1415 | /* We don't support multi-homed hosts */ |
Alexander Aring | 1a26bfa | 2020-11-02 20:04:26 -0500 | [diff] [blame] | 1416 | if (dlm_local_count > 1) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1417 | log_print("TCP protocol can't handle multi-homed hosts, " |
| 1418 | "try SCTP"); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1419 | return -EINVAL; |
| 1420 | } |
| 1421 | |
| 1422 | log_print("Using TCP for communications"); |
| 1423 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 1424 | return tcp_create_listen_sock(&listen_con, dlm_local_addr[0]); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | |
| 1428 | |
| 1429 | static struct writequeue_entry *new_writequeue_entry(struct connection *con, |
| 1430 | gfp_t allocation) |
| 1431 | { |
| 1432 | struct writequeue_entry *entry; |
| 1433 | |
Alexander Aring | f0747ebf | 2021-03-01 17:05:16 -0500 | [diff] [blame] | 1434 | entry = kzalloc(sizeof(*entry), allocation); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1435 | if (!entry) |
| 1436 | return NULL; |
| 1437 | |
Alexander Aring | e1a7cbc | 2021-03-01 17:05:15 -0500 | [diff] [blame] | 1438 | entry->page = alloc_page(allocation | __GFP_ZERO); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1439 | if (!entry->page) { |
| 1440 | kfree(entry); |
| 1441 | return NULL; |
| 1442 | } |
| 1443 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1444 | entry->con = con; |
Alexander Aring | f0747ebf | 2021-03-01 17:05:16 -0500 | [diff] [blame] | 1445 | entry->users = 1; |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame^] | 1446 | kref_init(&entry->ref); |
| 1447 | INIT_LIST_HEAD(&entry->msgs); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1448 | |
| 1449 | return entry; |
| 1450 | } |
| 1451 | |
Alexander Aring | f0747ebf | 2021-03-01 17:05:16 -0500 | [diff] [blame] | 1452 | static struct writequeue_entry *new_wq_entry(struct connection *con, int len, |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame^] | 1453 | gfp_t allocation, char **ppc, |
| 1454 | void (*cb)(struct dlm_mhandle *mh), |
| 1455 | struct dlm_mhandle *mh) |
Alexander Aring | f0747ebf | 2021-03-01 17:05:16 -0500 | [diff] [blame] | 1456 | { |
| 1457 | struct writequeue_entry *e; |
| 1458 | |
| 1459 | spin_lock(&con->writequeue_lock); |
| 1460 | if (!list_empty(&con->writequeue)) { |
| 1461 | e = list_last_entry(&con->writequeue, struct writequeue_entry, list); |
| 1462 | if (DLM_WQ_REMAIN_BYTES(e) >= len) { |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame^] | 1463 | kref_get(&e->ref); |
| 1464 | |
Alexander Aring | f0747ebf | 2021-03-01 17:05:16 -0500 | [diff] [blame] | 1465 | *ppc = page_address(e->page) + e->end; |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame^] | 1466 | if (cb) |
| 1467 | cb(mh); |
| 1468 | |
Alexander Aring | f0747ebf | 2021-03-01 17:05:16 -0500 | [diff] [blame] | 1469 | e->end += len; |
| 1470 | e->users++; |
| 1471 | spin_unlock(&con->writequeue_lock); |
| 1472 | |
| 1473 | return e; |
| 1474 | } |
| 1475 | } |
| 1476 | spin_unlock(&con->writequeue_lock); |
| 1477 | |
| 1478 | e = new_writequeue_entry(con, allocation); |
| 1479 | if (!e) |
| 1480 | return NULL; |
| 1481 | |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame^] | 1482 | kref_get(&e->ref); |
Alexander Aring | f0747ebf | 2021-03-01 17:05:16 -0500 | [diff] [blame] | 1483 | *ppc = page_address(e->page); |
| 1484 | e->end += len; |
Alexander Aring | 8aa31cb | 2021-05-21 15:08:39 -0400 | [diff] [blame] | 1485 | atomic_inc(&con->writequeue_cnt); |
Alexander Aring | f0747ebf | 2021-03-01 17:05:16 -0500 | [diff] [blame] | 1486 | |
| 1487 | spin_lock(&con->writequeue_lock); |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame^] | 1488 | if (cb) |
| 1489 | cb(mh); |
| 1490 | |
Alexander Aring | f0747ebf | 2021-03-01 17:05:16 -0500 | [diff] [blame] | 1491 | list_add_tail(&e->list, &con->writequeue); |
| 1492 | spin_unlock(&con->writequeue_lock); |
| 1493 | |
| 1494 | return e; |
| 1495 | }; |
| 1496 | |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame^] | 1497 | struct dlm_msg *dlm_lowcomms_new_msg(int nodeid, int len, gfp_t allocation, |
| 1498 | char **ppc, void (*cb)(struct dlm_mhandle *mh), |
| 1499 | struct dlm_mhandle *mh) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1500 | { |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1501 | struct writequeue_entry *e; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1502 | struct connection *con; |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame^] | 1503 | struct dlm_msg *msg; |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1504 | int idx; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1505 | |
Alexander Aring | c45674f | 2021-03-01 17:05:14 -0500 | [diff] [blame] | 1506 | if (len > DEFAULT_BUFFER_SIZE || |
| 1507 | len < sizeof(struct dlm_header)) { |
| 1508 | BUILD_BUG_ON(PAGE_SIZE < DEFAULT_BUFFER_SIZE); |
Alexander Aring | 692f51c | 2020-11-02 20:04:18 -0500 | [diff] [blame] | 1509 | log_print("failed to allocate a buffer of size %d", len); |
Alexander Aring | c45674f | 2021-03-01 17:05:14 -0500 | [diff] [blame] | 1510 | WARN_ON(1); |
Alexander Aring | 692f51c | 2020-11-02 20:04:18 -0500 | [diff] [blame] | 1511 | return NULL; |
| 1512 | } |
| 1513 | |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1514 | idx = srcu_read_lock(&connections_srcu); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1515 | con = nodeid2con(nodeid, allocation); |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1516 | if (!con) { |
| 1517 | srcu_read_unlock(&connections_srcu, idx); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1518 | return NULL; |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1519 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1520 | |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame^] | 1521 | msg = kzalloc(sizeof(*msg), allocation); |
| 1522 | if (!msg) { |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1523 | srcu_read_unlock(&connections_srcu, idx); |
| 1524 | return NULL; |
| 1525 | } |
| 1526 | |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame^] | 1527 | kref_init(&msg->ref); |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1528 | |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame^] | 1529 | e = new_wq_entry(con, len, allocation, ppc, cb, mh); |
| 1530 | if (!e) { |
| 1531 | srcu_read_unlock(&connections_srcu, idx); |
| 1532 | kfree(msg); |
| 1533 | return NULL; |
| 1534 | } |
| 1535 | |
| 1536 | msg->ppc = *ppc; |
| 1537 | msg->len = len; |
| 1538 | msg->entry = e; |
| 1539 | |
| 1540 | /* we assume if successful commit must called */ |
| 1541 | msg->idx = idx; |
| 1542 | |
| 1543 | return msg; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1544 | } |
| 1545 | |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame^] | 1546 | void dlm_lowcomms_commit_msg(struct dlm_msg *msg) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1547 | { |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame^] | 1548 | struct writequeue_entry *e = msg->entry; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1549 | struct connection *con = e->con; |
| 1550 | int users; |
| 1551 | |
| 1552 | spin_lock(&con->writequeue_lock); |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame^] | 1553 | kref_get(&msg->ref); |
| 1554 | list_add(&msg->list, &e->msgs); |
| 1555 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1556 | users = --e->users; |
| 1557 | if (users) |
| 1558 | goto out; |
Alexander Aring | f0747ebf | 2021-03-01 17:05:16 -0500 | [diff] [blame] | 1559 | |
| 1560 | e->len = DLM_WQ_LENGTH_BYTES(e); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1561 | spin_unlock(&con->writequeue_lock); |
| 1562 | |
Bob Peterson | 01da24d | 2017-09-12 08:55:14 +0000 | [diff] [blame] | 1563 | queue_work(send_workqueue, &con->swork); |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame^] | 1564 | srcu_read_unlock(&connections_srcu, msg->idx); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1565 | return; |
| 1566 | |
| 1567 | out: |
| 1568 | spin_unlock(&con->writequeue_lock); |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame^] | 1569 | srcu_read_unlock(&connections_srcu, msg->idx); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1570 | return; |
| 1571 | } |
| 1572 | |
Alexander Aring | 8f2dc78 | 2021-05-21 15:08:42 -0400 | [diff] [blame^] | 1573 | void dlm_lowcomms_put_msg(struct dlm_msg *msg) |
| 1574 | { |
| 1575 | kref_put(&msg->ref, dlm_msg_release); |
| 1576 | } |
| 1577 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1578 | /* Send a message */ |
| 1579 | static void send_to_sock(struct connection *con) |
| 1580 | { |
| 1581 | int ret = 0; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1582 | const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL; |
| 1583 | struct writequeue_entry *e; |
| 1584 | int len, offset; |
Bob Peterson | f92c8dd | 2010-11-12 11:15:20 -0600 | [diff] [blame] | 1585 | int count = 0; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1586 | |
| 1587 | mutex_lock(&con->sock_mutex); |
| 1588 | if (con->sock == NULL) |
| 1589 | goto out_connect; |
| 1590 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1591 | spin_lock(&con->writequeue_lock); |
| 1592 | for (;;) { |
Alexander Aring | f0747ebf | 2021-03-01 17:05:16 -0500 | [diff] [blame] | 1593 | if (list_empty(&con->writequeue)) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1594 | break; |
| 1595 | |
Alexander Aring | f0747ebf | 2021-03-01 17:05:16 -0500 | [diff] [blame] | 1596 | e = list_first_entry(&con->writequeue, struct writequeue_entry, list); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1597 | len = e->len; |
| 1598 | offset = e->offset; |
| 1599 | BUG_ON(len == 0 && e->users == 0); |
| 1600 | spin_unlock(&con->writequeue_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1601 | |
| 1602 | ret = 0; |
| 1603 | if (len) { |
Paolo Bonzini | 1329e3f | 2009-08-24 13:18:04 -0500 | [diff] [blame] | 1604 | ret = kernel_sendpage(con->sock, e->page, offset, len, |
| 1605 | msg_flags); |
Patrick Caulfield | d66f827 | 2007-09-14 08:49:21 +0100 | [diff] [blame] | 1606 | if (ret == -EAGAIN || ret == 0) { |
David Miller | b36930d | 2010-11-10 21:56:39 -0800 | [diff] [blame] | 1607 | if (ret == -EAGAIN && |
Eric Dumazet | 9cd3e07 | 2015-11-29 20:03:10 -0800 | [diff] [blame] | 1608 | test_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags) && |
David Miller | b36930d | 2010-11-10 21:56:39 -0800 | [diff] [blame] | 1609 | !test_and_set_bit(CF_APP_LIMITED, &con->flags)) { |
| 1610 | /* Notify TCP that we're limited by the |
| 1611 | * application window size. |
| 1612 | */ |
| 1613 | set_bit(SOCK_NOSPACE, &con->sock->flags); |
| 1614 | con->sock->sk->sk_write_pending++; |
| 1615 | } |
Patrick Caulfield | d66f827 | 2007-09-14 08:49:21 +0100 | [diff] [blame] | 1616 | cond_resched(); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1617 | goto out; |
Ying Xue | 9c5bef5 | 2012-08-13 14:29:55 +0800 | [diff] [blame] | 1618 | } else if (ret < 0) |
Alexander Aring | ba868d9 | 2021-05-21 15:08:37 -0400 | [diff] [blame] | 1619 | goto out; |
Patrick Caulfield | d66f827 | 2007-09-14 08:49:21 +0100 | [diff] [blame] | 1620 | } |
Bob Peterson | f92c8dd | 2010-11-12 11:15:20 -0600 | [diff] [blame] | 1621 | |
| 1622 | /* Don't starve people filling buffers */ |
| 1623 | if (++count >= MAX_SEND_MSG_COUNT) { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1624 | cond_resched(); |
Bob Peterson | f92c8dd | 2010-11-12 11:15:20 -0600 | [diff] [blame] | 1625 | count = 0; |
| 1626 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1627 | |
| 1628 | spin_lock(&con->writequeue_lock); |
Mike Christie | 5d68987 | 2013-06-14 04:56:13 -0500 | [diff] [blame] | 1629 | writequeue_entry_complete(e, ret); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1630 | } |
| 1631 | spin_unlock(&con->writequeue_lock); |
Alexander Aring | 8aa31cb | 2021-05-21 15:08:39 -0400 | [diff] [blame] | 1632 | |
| 1633 | /* close if we got EOF */ |
| 1634 | if (test_and_clear_bit(CF_EOF, &con->flags)) { |
| 1635 | mutex_unlock(&con->sock_mutex); |
| 1636 | close_connection(con, false, false, true); |
| 1637 | |
| 1638 | /* handling for tcp shutdown */ |
| 1639 | clear_bit(CF_SHUTDOWN, &con->flags); |
| 1640 | wake_up(&con->shutdown_wait); |
| 1641 | } else { |
| 1642 | mutex_unlock(&con->sock_mutex); |
| 1643 | } |
| 1644 | |
| 1645 | return; |
| 1646 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1647 | out: |
| 1648 | mutex_unlock(&con->sock_mutex); |
| 1649 | return; |
| 1650 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1651 | out_connect: |
| 1652 | mutex_unlock(&con->sock_mutex); |
Bob Peterson | 01da24d | 2017-09-12 08:55:14 +0000 | [diff] [blame] | 1653 | queue_work(send_workqueue, &con->swork); |
| 1654 | cond_resched(); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | static void clean_one_writequeue(struct connection *con) |
| 1658 | { |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1659 | struct writequeue_entry *e, *safe; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1660 | |
| 1661 | spin_lock(&con->writequeue_lock); |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1662 | list_for_each_entry_safe(e, safe, &con->writequeue, list) { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1663 | free_entry(e); |
| 1664 | } |
| 1665 | spin_unlock(&con->writequeue_lock); |
| 1666 | } |
| 1667 | |
| 1668 | /* Called from recovery when it knows that a node has |
| 1669 | left the cluster */ |
| 1670 | int dlm_lowcomms_close(int nodeid) |
| 1671 | { |
| 1672 | struct connection *con; |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 1673 | struct dlm_node_addr *na; |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1674 | int idx; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1675 | |
| 1676 | log_print("closing connection to node %d", nodeid); |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1677 | idx = srcu_read_lock(&connections_srcu); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1678 | con = nodeid2con(nodeid, 0); |
| 1679 | if (con) { |
Lars Marowsky-Bree | 063c4c9 | 2009-08-11 16:18:23 -0500 | [diff] [blame] | 1680 | set_bit(CF_CLOSE, &con->flags); |
Marcelo Ricardo Leitner | 0d737a8 | 2015-08-11 19:22:21 -0300 | [diff] [blame] | 1681 | close_connection(con, true, true, true); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1682 | clean_one_writequeue(con); |
Alexander Aring | 53a5eda | 2020-11-02 20:04:19 -0500 | [diff] [blame] | 1683 | if (con->othercon) |
| 1684 | clean_one_writequeue(con->othercon); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1685 | } |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1686 | srcu_read_unlock(&connections_srcu, idx); |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 1687 | |
| 1688 | spin_lock(&dlm_node_addrs_spin); |
| 1689 | na = find_node_addr(nodeid); |
| 1690 | if (na) { |
| 1691 | list_del(&na->list); |
| 1692 | while (na->addr_count--) |
| 1693 | kfree(na->addr[na->addr_count]); |
| 1694 | kfree(na); |
| 1695 | } |
| 1696 | spin_unlock(&dlm_node_addrs_spin); |
| 1697 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1698 | return 0; |
| 1699 | } |
| 1700 | |
| 1701 | /* Receive workqueue function */ |
| 1702 | static void process_recv_sockets(struct work_struct *work) |
| 1703 | { |
| 1704 | struct connection *con = container_of(work, struct connection, rwork); |
| 1705 | int err; |
| 1706 | |
| 1707 | clear_bit(CF_READ_PENDING, &con->flags); |
| 1708 | do { |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 1709 | err = receive_from_sock(con); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1710 | } while (!err); |
| 1711 | } |
| 1712 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 1713 | static void process_listen_recv_socket(struct work_struct *work) |
| 1714 | { |
| 1715 | accept_from_sock(&listen_con); |
| 1716 | } |
| 1717 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1718 | /* Send workqueue function */ |
| 1719 | static void process_send_sockets(struct work_struct *work) |
| 1720 | { |
| 1721 | struct connection *con = container_of(work, struct connection, swork); |
| 1722 | |
Alexander Aring | 7443bc9 | 2021-05-21 15:08:36 -0400 | [diff] [blame] | 1723 | WARN_ON(test_bit(CF_IS_OTHERCON, &con->flags)); |
| 1724 | |
tsutomu.owa@toshiba.co.jp | 8a4abb0 | 2017-09-12 09:01:16 +0000 | [diff] [blame] | 1725 | clear_bit(CF_WRITE_PENDING, &con->flags); |
Alexander Aring | ba868d9 | 2021-05-21 15:08:37 -0400 | [diff] [blame] | 1726 | |
| 1727 | if (test_and_clear_bit(CF_RECONNECT, &con->flags)) |
| 1728 | close_connection(con, false, false, true); |
| 1729 | |
| 1730 | if (con->sock == NULL) { /* not mutex protected so check it inside too */ |
| 1731 | if (test_and_clear_bit(CF_DELAY_CONNECT, &con->flags)) |
| 1732 | msleep(1000); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1733 | con->connect_action(con); |
Alexander Aring | ba868d9 | 2021-05-21 15:08:37 -0400 | [diff] [blame] | 1734 | } |
Bob Peterson | 01da24d | 2017-09-12 08:55:14 +0000 | [diff] [blame] | 1735 | if (!list_empty(&con->writequeue)) |
Lars Marowsky-Bree | 063c4c9 | 2009-08-11 16:18:23 -0500 | [diff] [blame] | 1736 | send_to_sock(con); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1737 | } |
| 1738 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1739 | static void work_stop(void) |
| 1740 | { |
David Windsor | b355516 | 2019-04-02 08:37:10 -0400 | [diff] [blame] | 1741 | if (recv_workqueue) |
| 1742 | destroy_workqueue(recv_workqueue); |
| 1743 | if (send_workqueue) |
| 1744 | destroy_workqueue(send_workqueue); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | static int work_start(void) |
| 1748 | { |
David Teigland | e43f055 | 2011-03-10 13:22:34 -0600 | [diff] [blame] | 1749 | recv_workqueue = alloc_workqueue("dlm_recv", |
| 1750 | WQ_UNBOUND | WQ_MEM_RECLAIM, 1); |
Namhyung Kim | b9d4105 | 2010-12-13 13:42:24 -0600 | [diff] [blame] | 1751 | if (!recv_workqueue) { |
| 1752 | log_print("can't start dlm_recv"); |
| 1753 | return -ENOMEM; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1754 | } |
| 1755 | |
David Teigland | e43f055 | 2011-03-10 13:22:34 -0600 | [diff] [blame] | 1756 | send_workqueue = alloc_workqueue("dlm_send", |
| 1757 | WQ_UNBOUND | WQ_MEM_RECLAIM, 1); |
Namhyung Kim | b9d4105 | 2010-12-13 13:42:24 -0600 | [diff] [blame] | 1758 | if (!send_workqueue) { |
| 1759 | log_print("can't start dlm_send"); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1760 | destroy_workqueue(recv_workqueue); |
Namhyung Kim | b9d4105 | 2010-12-13 13:42:24 -0600 | [diff] [blame] | 1761 | return -ENOMEM; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | return 0; |
| 1765 | } |
| 1766 | |
Alexander Aring | 9d23246 | 2021-03-01 17:05:20 -0500 | [diff] [blame] | 1767 | static void shutdown_conn(struct connection *con) |
| 1768 | { |
| 1769 | if (con->shutdown_action) |
| 1770 | con->shutdown_action(con); |
| 1771 | } |
| 1772 | |
| 1773 | void dlm_lowcomms_shutdown(void) |
| 1774 | { |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1775 | int idx; |
| 1776 | |
Alexander Aring | 9d23246 | 2021-03-01 17:05:20 -0500 | [diff] [blame] | 1777 | /* Set all the flags to prevent any |
| 1778 | * socket activity. |
| 1779 | */ |
| 1780 | dlm_allow_conn = 0; |
| 1781 | |
| 1782 | if (recv_workqueue) |
| 1783 | flush_workqueue(recv_workqueue); |
| 1784 | if (send_workqueue) |
| 1785 | flush_workqueue(send_workqueue); |
| 1786 | |
| 1787 | dlm_close_sock(&listen_con.sock); |
| 1788 | |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1789 | idx = srcu_read_lock(&connections_srcu); |
Alexander Aring | 9d23246 | 2021-03-01 17:05:20 -0500 | [diff] [blame] | 1790 | foreach_conn(shutdown_conn); |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1791 | srcu_read_unlock(&connections_srcu, idx); |
Alexander Aring | 9d23246 | 2021-03-01 17:05:20 -0500 | [diff] [blame] | 1792 | } |
| 1793 | |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1794 | static void _stop_conn(struct connection *con, bool and_other) |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1795 | { |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1796 | mutex_lock(&con->sock_mutex); |
tsutomu.owa@toshiba.co.jp | 173a31f | 2017-09-12 09:01:24 +0000 | [diff] [blame] | 1797 | set_bit(CF_CLOSE, &con->flags); |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1798 | set_bit(CF_READ_PENDING, &con->flags); |
tsutomu.owa@toshiba.co.jp | 8a4abb0 | 2017-09-12 09:01:16 +0000 | [diff] [blame] | 1799 | set_bit(CF_WRITE_PENDING, &con->flags); |
tsutomu.owa@toshiba.co.jp | 93eaade | 2017-09-12 09:01:55 +0000 | [diff] [blame] | 1800 | if (con->sock && con->sock->sk) { |
| 1801 | write_lock_bh(&con->sock->sk->sk_callback_lock); |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1802 | con->sock->sk->sk_user_data = NULL; |
tsutomu.owa@toshiba.co.jp | 93eaade | 2017-09-12 09:01:55 +0000 | [diff] [blame] | 1803 | write_unlock_bh(&con->sock->sk->sk_callback_lock); |
| 1804 | } |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1805 | if (con->othercon && and_other) |
| 1806 | _stop_conn(con->othercon, false); |
| 1807 | mutex_unlock(&con->sock_mutex); |
| 1808 | } |
| 1809 | |
| 1810 | static void stop_conn(struct connection *con) |
| 1811 | { |
| 1812 | _stop_conn(con, true); |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1813 | } |
| 1814 | |
Alexander Aring | 4798cbb | 2020-09-24 10:31:26 -0400 | [diff] [blame] | 1815 | static void connection_release(struct rcu_head *rcu) |
| 1816 | { |
| 1817 | struct connection *con = container_of(rcu, struct connection, rcu); |
| 1818 | |
| 1819 | kfree(con->rx_buf); |
| 1820 | kfree(con); |
| 1821 | } |
| 1822 | |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1823 | static void free_conn(struct connection *con) |
| 1824 | { |
Marcelo Ricardo Leitner | 0d737a8 | 2015-08-11 19:22:21 -0300 | [diff] [blame] | 1825 | close_connection(con, true, true, true); |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 1826 | spin_lock(&connections_lock); |
| 1827 | hlist_del_rcu(&con->list); |
| 1828 | spin_unlock(&connections_lock); |
Alexander Aring | 948c47e | 2020-08-27 15:02:53 -0400 | [diff] [blame] | 1829 | if (con->othercon) { |
| 1830 | clean_one_writequeue(con->othercon); |
Alexander Aring | 5cbec20 | 2020-11-02 20:04:16 -0500 | [diff] [blame] | 1831 | call_srcu(&connections_srcu, &con->othercon->rcu, |
| 1832 | connection_release); |
Alexander Aring | 948c47e | 2020-08-27 15:02:53 -0400 | [diff] [blame] | 1833 | } |
Alexander Aring | 0de9843 | 2020-08-27 15:02:52 -0400 | [diff] [blame] | 1834 | clean_one_writequeue(con); |
Alexander Aring | 5cbec20 | 2020-11-02 20:04:16 -0500 | [diff] [blame] | 1835 | call_srcu(&connections_srcu, &con->rcu, connection_release); |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1836 | } |
| 1837 | |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1838 | static void work_flush(void) |
| 1839 | { |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1840 | int ok; |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1841 | int i; |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1842 | struct connection *con; |
| 1843 | |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1844 | do { |
| 1845 | ok = 1; |
| 1846 | foreach_conn(stop_conn); |
David Windsor | b355516 | 2019-04-02 08:37:10 -0400 | [diff] [blame] | 1847 | if (recv_workqueue) |
| 1848 | flush_workqueue(recv_workqueue); |
| 1849 | if (send_workqueue) |
| 1850 | flush_workqueue(send_workqueue); |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1851 | for (i = 0; i < CONN_HASH_SIZE && ok; i++) { |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 1852 | hlist_for_each_entry_rcu(con, &connection_hash[i], |
| 1853 | list) { |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1854 | ok &= test_bit(CF_READ_PENDING, &con->flags); |
tsutomu.owa@toshiba.co.jp | 8a4abb0 | 2017-09-12 09:01:16 +0000 | [diff] [blame] | 1855 | ok &= test_bit(CF_WRITE_PENDING, &con->flags); |
| 1856 | if (con->othercon) { |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1857 | ok &= test_bit(CF_READ_PENDING, |
| 1858 | &con->othercon->flags); |
tsutomu.owa@toshiba.co.jp | 8a4abb0 | 2017-09-12 09:01:16 +0000 | [diff] [blame] | 1859 | ok &= test_bit(CF_WRITE_PENDING, |
| 1860 | &con->othercon->flags); |
| 1861 | } |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1862 | } |
| 1863 | } |
| 1864 | } while (!ok); |
| 1865 | } |
| 1866 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1867 | void dlm_lowcomms_stop(void) |
| 1868 | { |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1869 | int idx; |
| 1870 | |
| 1871 | idx = srcu_read_lock(&connections_srcu); |
tsutomu.owa@toshiba.co.jp | f0fb83c | 2017-09-12 08:55:40 +0000 | [diff] [blame] | 1872 | work_flush(); |
Marcelo Ricardo Leitner | 3a8db79 | 2016-10-08 10:14:37 -0300 | [diff] [blame] | 1873 | foreach_conn(free_conn); |
Alexander Aring | b38bc9c | 2021-05-21 15:08:35 -0400 | [diff] [blame] | 1874 | srcu_read_unlock(&connections_srcu, idx); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1875 | work_stop(); |
Alexander Aring | 043697f | 2020-08-27 15:02:50 -0400 | [diff] [blame] | 1876 | deinit_local(); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | int dlm_lowcomms_start(void) |
| 1880 | { |
| 1881 | int error = -EINVAL; |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1882 | int i; |
| 1883 | |
| 1884 | for (i = 0; i < CONN_HASH_SIZE; i++) |
| 1885 | INIT_HLIST_HEAD(&connection_hash[i]); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1886 | |
| 1887 | init_local(); |
| 1888 | if (!dlm_local_count) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1889 | error = -ENOTCONN; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1890 | log_print("no local IP address has been set"); |
David Teigland | 513ef59 | 2012-03-30 11:46:08 -0500 | [diff] [blame] | 1891 | goto fail; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1892 | } |
| 1893 | |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 1894 | INIT_WORK(&listen_con.rwork, process_listen_recv_socket); |
| 1895 | |
David Teigland | 513ef59 | 2012-03-30 11:46:08 -0500 | [diff] [blame] | 1896 | error = work_start(); |
| 1897 | if (error) |
Alexander Aring | a47666eb | 2020-08-27 15:02:49 -0400 | [diff] [blame] | 1898 | goto fail; |
David Teigland | 513ef59 | 2012-03-30 11:46:08 -0500 | [diff] [blame] | 1899 | |
| 1900 | dlm_allow_conn = 1; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1901 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1902 | /* Start listening */ |
| 1903 | if (dlm_config.ci_protocol == 0) |
| 1904 | error = tcp_listen_for_all(); |
| 1905 | else |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 1906 | error = sctp_listen_for_all(&listen_con); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1907 | if (error) |
| 1908 | goto fail_unlisten; |
| 1909 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1910 | return 0; |
| 1911 | |
| 1912 | fail_unlisten: |
David Teigland | 513ef59 | 2012-03-30 11:46:08 -0500 | [diff] [blame] | 1913 | dlm_allow_conn = 0; |
Alexander Aring | d11ccd4 | 2020-11-02 20:04:25 -0500 | [diff] [blame] | 1914 | dlm_close_sock(&listen_con.sock); |
David Teigland | 513ef59 | 2012-03-30 11:46:08 -0500 | [diff] [blame] | 1915 | fail: |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1916 | return error; |
| 1917 | } |
David Teigland | 36b71a8 | 2012-07-26 12:44:30 -0500 | [diff] [blame] | 1918 | |
| 1919 | void dlm_lowcomms_exit(void) |
| 1920 | { |
| 1921 | struct dlm_node_addr *na, *safe; |
| 1922 | |
| 1923 | spin_lock(&dlm_node_addrs_spin); |
| 1924 | list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) { |
| 1925 | list_del(&na->list); |
| 1926 | while (na->addr_count--) |
| 1927 | kfree(na->addr[na->addr_count]); |
| 1928 | kfree(na); |
| 1929 | } |
| 1930 | spin_unlock(&dlm_node_addrs_spin); |
| 1931 | } |