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