Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | ******************************************************************************* |
| 3 | ** |
| 4 | ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 5 | ** Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved. |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 6 | ** |
| 7 | ** This copyrighted material is made available to anyone wishing to use, |
| 8 | ** modify, copy, or redistribute it subject to the terms and conditions |
| 9 | ** of the GNU General Public License v.2. |
| 10 | ** |
| 11 | ******************************************************************************* |
| 12 | ******************************************************************************/ |
| 13 | |
| 14 | /* |
| 15 | * lowcomms.c |
| 16 | * |
| 17 | * This is the "low-level" comms layer. |
| 18 | * |
| 19 | * It is responsible for sending/receiving messages |
| 20 | * from other nodes in the cluster. |
| 21 | * |
| 22 | * Cluster nodes are referred to by their nodeids. nodeids are |
| 23 | * simply 32 bit numbers to the locking module - if they need to |
Joe Perches | 2cf12c0 | 2009-01-22 13:26:47 -0800 | [diff] [blame] | 24 | * be expanded for the cluster infrastructure then that is its |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 25 | * responsibility. It is this layer's |
| 26 | * responsibility to resolve these into IP address or |
| 27 | * whatever it needs for inter-node communication. |
| 28 | * |
| 29 | * The comms level is two kernel threads that deal mainly with |
| 30 | * the receiving of messages from other nodes and passing them |
| 31 | * up to the mid-level comms layer (which understands the |
| 32 | * message format) for execution by the locking core, and |
| 33 | * a send thread which does all the setting up of connections |
| 34 | * to remote nodes and the sending of data. Threads are not allowed |
| 35 | * to send their own data because it may cause them to wait in times |
| 36 | * of high load. Also, this way, the sending thread can collect together |
| 37 | * messages bound for one node and send them in one block. |
| 38 | * |
Joe Perches | 2cf12c0 | 2009-01-22 13:26:47 -0800 | [diff] [blame] | 39 | * 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] | 40 | * depending on the configuration variable 'protocol'. This should be set |
Joe Perches | 2cf12c0 | 2009-01-22 13:26:47 -0800 | [diff] [blame] | 41 | * 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] | 42 | * cluster-wide mechanism as it must be the same on all nodes of the cluster |
| 43 | * for the DLM to function. |
| 44 | * |
| 45 | */ |
| 46 | |
| 47 | #include <asm/ioctls.h> |
| 48 | #include <net/sock.h> |
| 49 | #include <net/tcp.h> |
| 50 | #include <linux/pagemap.h> |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 51 | #include <linux/file.h> |
Matthias Kaehlcke | 7a936ce | 2008-05-12 10:04:51 -0500 | [diff] [blame] | 52 | #include <linux/mutex.h> |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 53 | #include <linux/sctp.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 54 | #include <linux/slab.h> |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 55 | #include <net/sctp/user.h> |
Joe Perches | 44ad532 | 2009-01-22 13:24:49 -0800 | [diff] [blame] | 56 | #include <net/ipv6.h> |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 57 | |
| 58 | #include "dlm_internal.h" |
| 59 | #include "lowcomms.h" |
| 60 | #include "midcomms.h" |
| 61 | #include "config.h" |
| 62 | |
| 63 | #define NEEDED_RMEM (4*1024*1024) |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 64 | #define CONN_HASH_SIZE 32 |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 65 | |
| 66 | struct cbuf { |
| 67 | unsigned int base; |
| 68 | unsigned int len; |
| 69 | unsigned int mask; |
| 70 | }; |
| 71 | |
| 72 | static void cbuf_add(struct cbuf *cb, int n) |
| 73 | { |
| 74 | cb->len += n; |
| 75 | } |
| 76 | |
| 77 | static int cbuf_data(struct cbuf *cb) |
| 78 | { |
| 79 | return ((cb->base + cb->len) & cb->mask); |
| 80 | } |
| 81 | |
| 82 | static void cbuf_init(struct cbuf *cb, int size) |
| 83 | { |
| 84 | cb->base = cb->len = 0; |
| 85 | cb->mask = size-1; |
| 86 | } |
| 87 | |
| 88 | static void cbuf_eat(struct cbuf *cb, int n) |
| 89 | { |
| 90 | cb->len -= n; |
| 91 | cb->base += n; |
| 92 | cb->base &= cb->mask; |
| 93 | } |
| 94 | |
| 95 | static bool cbuf_empty(struct cbuf *cb) |
| 96 | { |
| 97 | return cb->len == 0; |
| 98 | } |
| 99 | |
| 100 | struct connection { |
| 101 | struct socket *sock; /* NULL if not connected */ |
| 102 | uint32_t nodeid; /* So we know who we are in the list */ |
| 103 | struct mutex sock_mutex; |
| 104 | unsigned long flags; |
| 105 | #define CF_READ_PENDING 1 |
| 106 | #define CF_WRITE_PENDING 2 |
| 107 | #define CF_CONNECT_PENDING 3 |
| 108 | #define CF_INIT_PENDING 4 |
| 109 | #define CF_IS_OTHERCON 5 |
Lars Marowsky-Bree | 063c4c9 | 2009-08-11 16:18:23 -0500 | [diff] [blame] | 110 | #define CF_CLOSE 6 |
David Miller | b36930d | 2010-11-10 21:56:39 -0800 | [diff] [blame] | 111 | #define CF_APP_LIMITED 7 |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 112 | struct list_head writequeue; /* List of outgoing writequeue_entries */ |
| 113 | spinlock_t writequeue_lock; |
| 114 | int (*rx_action) (struct connection *); /* What to do when active */ |
| 115 | void (*connect_action) (struct connection *); /* What to do to connect */ |
| 116 | struct page *rx_page; |
| 117 | struct cbuf cb; |
| 118 | int retries; |
| 119 | #define MAX_CONNECT_RETRIES 3 |
| 120 | int sctp_assoc; |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 121 | struct hlist_node list; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 122 | struct connection *othercon; |
| 123 | struct work_struct rwork; /* Receive workqueue */ |
| 124 | struct work_struct swork; /* Send workqueue */ |
| 125 | }; |
| 126 | #define sock2con(x) ((struct connection *)(x)->sk_user_data) |
| 127 | |
| 128 | /* An entry waiting to be sent */ |
| 129 | struct writequeue_entry { |
| 130 | struct list_head list; |
| 131 | struct page *page; |
| 132 | int offset; |
| 133 | int len; |
| 134 | int end; |
| 135 | int users; |
| 136 | struct connection *con; |
| 137 | }; |
| 138 | |
| 139 | static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT]; |
| 140 | static int dlm_local_count; |
| 141 | |
| 142 | /* Work queues */ |
| 143 | static struct workqueue_struct *recv_workqueue; |
| 144 | static struct workqueue_struct *send_workqueue; |
| 145 | |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 146 | static struct hlist_head connection_hash[CONN_HASH_SIZE]; |
Matthias Kaehlcke | 7a936ce | 2008-05-12 10:04:51 -0500 | [diff] [blame] | 147 | static DEFINE_MUTEX(connections_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 148 | static struct kmem_cache *con_cache; |
| 149 | |
| 150 | static void process_recv_sockets(struct work_struct *work); |
| 151 | static void process_send_sockets(struct work_struct *work); |
| 152 | |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 153 | |
| 154 | /* This is deliberately very simple because most clusters have simple |
| 155 | sequential nodeids, so we should be able to go straight to a connection |
| 156 | struct in the array */ |
| 157 | static inline int nodeid_hash(int nodeid) |
| 158 | { |
| 159 | return nodeid & (CONN_HASH_SIZE-1); |
| 160 | } |
| 161 | |
| 162 | static struct connection *__find_con(int nodeid) |
| 163 | { |
| 164 | int r; |
| 165 | struct hlist_node *h; |
| 166 | struct connection *con; |
| 167 | |
| 168 | r = nodeid_hash(nodeid); |
| 169 | |
| 170 | hlist_for_each_entry(con, h, &connection_hash[r], list) { |
| 171 | if (con->nodeid == nodeid) |
| 172 | return con; |
| 173 | } |
| 174 | return NULL; |
| 175 | } |
| 176 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 177 | /* |
| 178 | * If 'allocation' is zero then we don't attempt to create a new |
| 179 | * connection structure for this node. |
| 180 | */ |
| 181 | static struct connection *__nodeid2con(int nodeid, gfp_t alloc) |
| 182 | { |
| 183 | struct connection *con = NULL; |
| 184 | int r; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 185 | |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 186 | con = __find_con(nodeid); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 187 | if (con || !alloc) |
| 188 | return con; |
| 189 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 190 | con = kmem_cache_zalloc(con_cache, alloc); |
| 191 | if (!con) |
| 192 | return NULL; |
| 193 | |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 194 | r = nodeid_hash(nodeid); |
| 195 | hlist_add_head(&con->list, &connection_hash[r]); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 196 | |
| 197 | con->nodeid = nodeid; |
| 198 | mutex_init(&con->sock_mutex); |
| 199 | INIT_LIST_HEAD(&con->writequeue); |
| 200 | spin_lock_init(&con->writequeue_lock); |
| 201 | INIT_WORK(&con->swork, process_send_sockets); |
| 202 | INIT_WORK(&con->rwork, process_recv_sockets); |
| 203 | |
| 204 | /* Setup action pointers for child sockets */ |
| 205 | if (con->nodeid) { |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 206 | struct connection *zerocon = __find_con(0); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 207 | |
| 208 | con->connect_action = zerocon->connect_action; |
| 209 | if (!con->rx_action) |
| 210 | con->rx_action = zerocon->rx_action; |
| 211 | } |
| 212 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 213 | return con; |
| 214 | } |
| 215 | |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 216 | /* Loop round all connections */ |
| 217 | static void foreach_conn(void (*conn_func)(struct connection *c)) |
| 218 | { |
| 219 | int i; |
| 220 | struct hlist_node *h, *n; |
| 221 | struct connection *con; |
| 222 | |
| 223 | for (i = 0; i < CONN_HASH_SIZE; i++) { |
| 224 | hlist_for_each_entry_safe(con, h, n, &connection_hash[i], list){ |
| 225 | conn_func(con); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 230 | static struct connection *nodeid2con(int nodeid, gfp_t allocation) |
| 231 | { |
| 232 | struct connection *con; |
| 233 | |
Matthias Kaehlcke | 7a936ce | 2008-05-12 10:04:51 -0500 | [diff] [blame] | 234 | mutex_lock(&connections_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 235 | con = __nodeid2con(nodeid, allocation); |
Matthias Kaehlcke | 7a936ce | 2008-05-12 10:04:51 -0500 | [diff] [blame] | 236 | mutex_unlock(&connections_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 237 | |
| 238 | return con; |
| 239 | } |
| 240 | |
| 241 | /* This is a bit drastic, but only called when things go wrong */ |
| 242 | static struct connection *assoc2con(int assoc_id) |
| 243 | { |
| 244 | int i; |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 245 | struct hlist_node *h; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 246 | struct connection *con; |
| 247 | |
Matthias Kaehlcke | 7a936ce | 2008-05-12 10:04:51 -0500 | [diff] [blame] | 248 | mutex_lock(&connections_lock); |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 249 | |
| 250 | for (i = 0 ; i < CONN_HASH_SIZE; i++) { |
| 251 | hlist_for_each_entry(con, h, &connection_hash[i], list) { |
Julia Lawall | f70cb33 | 2010-08-03 23:34:16 +0200 | [diff] [blame] | 252 | if (con->sctp_assoc == assoc_id) { |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 253 | mutex_unlock(&connections_lock); |
| 254 | return con; |
| 255 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 256 | } |
| 257 | } |
Matthias Kaehlcke | 7a936ce | 2008-05-12 10:04:51 -0500 | [diff] [blame] | 258 | mutex_unlock(&connections_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 259 | return NULL; |
| 260 | } |
| 261 | |
| 262 | static int nodeid_to_addr(int nodeid, struct sockaddr *retaddr) |
| 263 | { |
| 264 | struct sockaddr_storage addr; |
| 265 | int error; |
| 266 | |
| 267 | if (!dlm_local_count) |
| 268 | return -1; |
| 269 | |
| 270 | error = dlm_nodeid_to_addr(nodeid, &addr); |
| 271 | if (error) |
| 272 | return error; |
| 273 | |
| 274 | if (dlm_local_addr[0]->ss_family == AF_INET) { |
| 275 | struct sockaddr_in *in4 = (struct sockaddr_in *) &addr; |
| 276 | struct sockaddr_in *ret4 = (struct sockaddr_in *) retaddr; |
| 277 | ret4->sin_addr.s_addr = in4->sin_addr.s_addr; |
| 278 | } else { |
| 279 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &addr; |
| 280 | struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) retaddr; |
Joe Perches | 44ad532 | 2009-01-22 13:24:49 -0800 | [diff] [blame] | 281 | ipv6_addr_copy(&ret6->sin6_addr, &in6->sin6_addr); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | /* Data available on socket or listen socket received a connect */ |
| 288 | static void lowcomms_data_ready(struct sock *sk, int count_unused) |
| 289 | { |
| 290 | struct connection *con = sock2con(sk); |
Patrick Caulfield | afb853f | 2007-06-01 10:07:26 -0500 | [diff] [blame] | 291 | if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags)) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 292 | queue_work(recv_workqueue, &con->rwork); |
| 293 | } |
| 294 | |
| 295 | static void lowcomms_write_space(struct sock *sk) |
| 296 | { |
| 297 | struct connection *con = sock2con(sk); |
| 298 | |
David Miller | b36930d | 2010-11-10 21:56:39 -0800 | [diff] [blame] | 299 | if (!con) |
| 300 | return; |
| 301 | |
| 302 | clear_bit(SOCK_NOSPACE, &con->sock->flags); |
| 303 | |
| 304 | if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) { |
| 305 | con->sock->sk->sk_write_pending--; |
| 306 | clear_bit(SOCK_ASYNC_NOSPACE, &con->sock->flags); |
| 307 | } |
| 308 | |
| 309 | if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 310 | queue_work(send_workqueue, &con->swork); |
| 311 | } |
| 312 | |
| 313 | static inline void lowcomms_connect_sock(struct connection *con) |
| 314 | { |
Lars Marowsky-Bree | 063c4c9 | 2009-08-11 16:18:23 -0500 | [diff] [blame] | 315 | if (test_bit(CF_CLOSE, &con->flags)) |
| 316 | return; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 317 | if (!test_and_set_bit(CF_CONNECT_PENDING, &con->flags)) |
| 318 | queue_work(send_workqueue, &con->swork); |
| 319 | } |
| 320 | |
| 321 | static void lowcomms_state_change(struct sock *sk) |
| 322 | { |
| 323 | if (sk->sk_state == TCP_ESTABLISHED) |
| 324 | lowcomms_write_space(sk); |
| 325 | } |
| 326 | |
Christine Caulfield | 391fbdc | 2009-05-07 10:54:16 -0500 | [diff] [blame] | 327 | int dlm_lowcomms_connect_node(int nodeid) |
| 328 | { |
| 329 | struct connection *con; |
| 330 | |
David Teigland | 04bedd7 | 2009-09-18 14:31:47 -0500 | [diff] [blame] | 331 | /* with sctp there's no connecting without sending */ |
| 332 | if (dlm_config.ci_protocol != 0) |
| 333 | return 0; |
| 334 | |
Christine Caulfield | 391fbdc | 2009-05-07 10:54:16 -0500 | [diff] [blame] | 335 | if (nodeid == dlm_our_nodeid()) |
| 336 | return 0; |
| 337 | |
| 338 | con = nodeid2con(nodeid, GFP_NOFS); |
| 339 | if (!con) |
| 340 | return -ENOMEM; |
| 341 | lowcomms_connect_sock(con); |
| 342 | return 0; |
| 343 | } |
| 344 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 345 | /* Make a socket active */ |
| 346 | static int add_sock(struct socket *sock, struct connection *con) |
| 347 | { |
| 348 | con->sock = sock; |
| 349 | |
| 350 | /* Install a data_ready callback */ |
| 351 | con->sock->sk->sk_data_ready = lowcomms_data_ready; |
| 352 | con->sock->sk->sk_write_space = lowcomms_write_space; |
| 353 | con->sock->sk->sk_state_change = lowcomms_state_change; |
| 354 | con->sock->sk->sk_user_data = con; |
Steven Whitehouse | d6d7b70 | 2008-11-12 16:49:48 -0600 | [diff] [blame] | 355 | con->sock->sk->sk_allocation = GFP_NOFS; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | /* Add the port number to an IPv6 or 4 sockaddr and return the address |
| 360 | length */ |
| 361 | static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port, |
| 362 | int *addr_len) |
| 363 | { |
| 364 | saddr->ss_family = dlm_local_addr[0]->ss_family; |
| 365 | if (saddr->ss_family == AF_INET) { |
| 366 | struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr; |
| 367 | in4_addr->sin_port = cpu_to_be16(port); |
| 368 | *addr_len = sizeof(struct sockaddr_in); |
| 369 | memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero)); |
| 370 | } else { |
| 371 | struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr; |
| 372 | in6_addr->sin6_port = cpu_to_be16(port); |
| 373 | *addr_len = sizeof(struct sockaddr_in6); |
| 374 | } |
Patrick Caulfield | 01c8cab | 2007-07-17 16:53:15 +0100 | [diff] [blame] | 375 | memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /* Close a remote connection and tidy up */ |
| 379 | static void close_connection(struct connection *con, bool and_other) |
| 380 | { |
| 381 | mutex_lock(&con->sock_mutex); |
| 382 | |
| 383 | if (con->sock) { |
| 384 | sock_release(con->sock); |
| 385 | con->sock = NULL; |
| 386 | } |
| 387 | if (con->othercon && and_other) { |
| 388 | /* Will only re-enter once. */ |
| 389 | close_connection(con->othercon, false); |
| 390 | } |
| 391 | if (con->rx_page) { |
| 392 | __free_page(con->rx_page); |
| 393 | con->rx_page = NULL; |
| 394 | } |
Patrick Caulfield | 9e5f282 | 2007-08-02 14:58:14 +0100 | [diff] [blame] | 395 | |
Patrick Caulfield | 61d96be0 | 2007-08-20 15:13:38 +0100 | [diff] [blame] | 396 | con->retries = 0; |
| 397 | mutex_unlock(&con->sock_mutex); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /* We only send shutdown messages to nodes that are not part of the cluster */ |
| 401 | static void sctp_send_shutdown(sctp_assoc_t associd) |
| 402 | { |
| 403 | static char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; |
| 404 | struct msghdr outmessage; |
| 405 | struct cmsghdr *cmsg; |
| 406 | struct sctp_sndrcvinfo *sinfo; |
| 407 | int ret; |
| 408 | struct connection *con; |
| 409 | |
| 410 | con = nodeid2con(0,0); |
| 411 | BUG_ON(con == NULL); |
| 412 | |
| 413 | outmessage.msg_name = NULL; |
| 414 | outmessage.msg_namelen = 0; |
| 415 | outmessage.msg_control = outcmsg; |
| 416 | outmessage.msg_controllen = sizeof(outcmsg); |
| 417 | outmessage.msg_flags = MSG_EOR; |
| 418 | |
| 419 | cmsg = CMSG_FIRSTHDR(&outmessage); |
| 420 | cmsg->cmsg_level = IPPROTO_SCTP; |
| 421 | cmsg->cmsg_type = SCTP_SNDRCV; |
| 422 | cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); |
| 423 | outmessage.msg_controllen = cmsg->cmsg_len; |
| 424 | sinfo = CMSG_DATA(cmsg); |
| 425 | memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo)); |
| 426 | |
| 427 | sinfo->sinfo_flags |= MSG_EOF; |
| 428 | sinfo->sinfo_assoc_id = associd; |
| 429 | |
| 430 | ret = kernel_sendmsg(con->sock, &outmessage, NULL, 0, 0); |
| 431 | |
| 432 | if (ret != 0) |
| 433 | log_print("send EOF to node failed: %d", ret); |
| 434 | } |
| 435 | |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 436 | static void sctp_init_failed_foreach(struct connection *con) |
| 437 | { |
| 438 | con->sctp_assoc = 0; |
| 439 | if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) { |
| 440 | if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) |
| 441 | queue_work(send_workqueue, &con->swork); |
| 442 | } |
| 443 | } |
| 444 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 445 | /* INIT failed but we don't know which node... |
| 446 | restart INIT on all pending nodes */ |
| 447 | static void sctp_init_failed(void) |
| 448 | { |
Matthias Kaehlcke | 7a936ce | 2008-05-12 10:04:51 -0500 | [diff] [blame] | 449 | mutex_lock(&connections_lock); |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 450 | |
| 451 | foreach_conn(sctp_init_failed_foreach); |
| 452 | |
Matthias Kaehlcke | 7a936ce | 2008-05-12 10:04:51 -0500 | [diff] [blame] | 453 | mutex_unlock(&connections_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /* Something happened to an association */ |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 457 | static void process_sctp_notification(struct connection *con, |
| 458 | struct msghdr *msg, char *buf) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 459 | { |
| 460 | union sctp_notification *sn = (union sctp_notification *)buf; |
| 461 | |
| 462 | if (sn->sn_header.sn_type == SCTP_ASSOC_CHANGE) { |
| 463 | switch (sn->sn_assoc_change.sac_state) { |
| 464 | |
| 465 | case SCTP_COMM_UP: |
| 466 | case SCTP_RESTART: |
| 467 | { |
| 468 | /* Check that the new node is in the lockspace */ |
| 469 | struct sctp_prim prim; |
| 470 | int nodeid; |
| 471 | int prim_len, ret; |
| 472 | int addr_len; |
| 473 | struct connection *new_con; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 474 | sctp_peeloff_arg_t parg; |
| 475 | int parglen = sizeof(parg); |
David Teigland | 6861f35 | 2009-09-24 15:58:23 -0500 | [diff] [blame] | 476 | int err; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 477 | |
| 478 | /* |
| 479 | * We get this before any data for an association. |
| 480 | * We verify that the node is in the cluster and |
| 481 | * then peel off a socket for it. |
| 482 | */ |
| 483 | if ((int)sn->sn_assoc_change.sac_assoc_id <= 0) { |
| 484 | log_print("COMM_UP for invalid assoc ID %d", |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 485 | (int)sn->sn_assoc_change.sac_assoc_id); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 486 | sctp_init_failed(); |
| 487 | return; |
| 488 | } |
| 489 | memset(&prim, 0, sizeof(struct sctp_prim)); |
| 490 | prim_len = sizeof(struct sctp_prim); |
| 491 | prim.ssp_assoc_id = sn->sn_assoc_change.sac_assoc_id; |
| 492 | |
| 493 | ret = kernel_getsockopt(con->sock, |
| 494 | IPPROTO_SCTP, |
| 495 | SCTP_PRIMARY_ADDR, |
| 496 | (char*)&prim, |
| 497 | &prim_len); |
| 498 | if (ret < 0) { |
| 499 | log_print("getsockopt/sctp_primary_addr on " |
| 500 | "new assoc %d failed : %d", |
| 501 | (int)sn->sn_assoc_change.sac_assoc_id, |
| 502 | ret); |
| 503 | |
| 504 | /* Retry INIT later */ |
| 505 | new_con = assoc2con(sn->sn_assoc_change.sac_assoc_id); |
| 506 | if (new_con) |
| 507 | clear_bit(CF_CONNECT_PENDING, &con->flags); |
| 508 | return; |
| 509 | } |
| 510 | make_sockaddr(&prim.ssp_addr, 0, &addr_len); |
| 511 | if (dlm_addr_to_nodeid(&prim.ssp_addr, &nodeid)) { |
| 512 | int i; |
| 513 | unsigned char *b=(unsigned char *)&prim.ssp_addr; |
| 514 | log_print("reject connect from unknown addr"); |
| 515 | for (i=0; i<sizeof(struct sockaddr_storage);i++) |
| 516 | printk("%02x ", b[i]); |
| 517 | printk("\n"); |
| 518 | sctp_send_shutdown(prim.ssp_assoc_id); |
| 519 | return; |
| 520 | } |
| 521 | |
David Teigland | 748285c | 2009-05-15 10:50:57 -0500 | [diff] [blame] | 522 | new_con = nodeid2con(nodeid, GFP_NOFS); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 523 | if (!new_con) |
| 524 | return; |
| 525 | |
| 526 | /* Peel off a new sock */ |
| 527 | parg.associd = sn->sn_assoc_change.sac_assoc_id; |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 528 | ret = kernel_getsockopt(con->sock, IPPROTO_SCTP, |
| 529 | SCTP_SOCKOPT_PEELOFF, |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 530 | (void *)&parg, &parglen); |
David Teigland | 6861f35 | 2009-09-24 15:58:23 -0500 | [diff] [blame] | 531 | if (ret < 0) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 532 | log_print("Can't peel off a socket for " |
David Teigland | 6861f35 | 2009-09-24 15:58:23 -0500 | [diff] [blame] | 533 | "connection %d to node %d: err=%d", |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 534 | parg.associd, nodeid, ret); |
David Teigland | 6861f35 | 2009-09-24 15:58:23 -0500 | [diff] [blame] | 535 | return; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 536 | } |
David Teigland | 6861f35 | 2009-09-24 15:58:23 -0500 | [diff] [blame] | 537 | new_con->sock = sockfd_lookup(parg.sd, &err); |
| 538 | if (!new_con->sock) { |
| 539 | log_print("sockfd_lookup error %d", err); |
| 540 | return; |
| 541 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 542 | add_sock(new_con->sock, new_con); |
David Teigland | 6861f35 | 2009-09-24 15:58:23 -0500 | [diff] [blame] | 543 | sockfd_put(new_con->sock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 544 | |
David Teigland | 6861f35 | 2009-09-24 15:58:23 -0500 | [diff] [blame] | 545 | log_print("connecting to %d sctp association %d", |
| 546 | nodeid, (int)sn->sn_assoc_change.sac_assoc_id); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 547 | |
| 548 | /* Send any pending writes */ |
| 549 | clear_bit(CF_CONNECT_PENDING, &new_con->flags); |
| 550 | clear_bit(CF_INIT_PENDING, &con->flags); |
| 551 | if (!test_and_set_bit(CF_WRITE_PENDING, &new_con->flags)) { |
| 552 | queue_work(send_workqueue, &new_con->swork); |
| 553 | } |
| 554 | if (!test_and_set_bit(CF_READ_PENDING, &new_con->flags)) |
| 555 | queue_work(recv_workqueue, &new_con->rwork); |
| 556 | } |
| 557 | break; |
| 558 | |
| 559 | case SCTP_COMM_LOST: |
| 560 | case SCTP_SHUTDOWN_COMP: |
| 561 | { |
| 562 | con = assoc2con(sn->sn_assoc_change.sac_assoc_id); |
| 563 | if (con) { |
| 564 | con->sctp_assoc = 0; |
| 565 | } |
| 566 | } |
| 567 | break; |
| 568 | |
| 569 | /* We don't know which INIT failed, so clear the PENDING flags |
| 570 | * on them all. if assoc_id is zero then it will then try |
| 571 | * again */ |
| 572 | |
| 573 | case SCTP_CANT_STR_ASSOC: |
| 574 | { |
| 575 | log_print("Can't start SCTP association - retrying"); |
| 576 | sctp_init_failed(); |
| 577 | } |
| 578 | break; |
| 579 | |
| 580 | default: |
| 581 | log_print("unexpected SCTP assoc change id=%d state=%d", |
| 582 | (int)sn->sn_assoc_change.sac_assoc_id, |
| 583 | sn->sn_assoc_change.sac_state); |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | /* Data received from remote end */ |
| 589 | static int receive_from_sock(struct connection *con) |
| 590 | { |
| 591 | int ret = 0; |
| 592 | struct msghdr msg = {}; |
| 593 | struct kvec iov[2]; |
| 594 | unsigned len; |
| 595 | int r; |
| 596 | int call_again_soon = 0; |
| 597 | int nvec; |
| 598 | char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; |
| 599 | |
| 600 | mutex_lock(&con->sock_mutex); |
| 601 | |
| 602 | if (con->sock == NULL) { |
| 603 | ret = -EAGAIN; |
| 604 | goto out_close; |
| 605 | } |
| 606 | |
| 607 | if (con->rx_page == NULL) { |
| 608 | /* |
| 609 | * This doesn't need to be atomic, but I think it should |
| 610 | * improve performance if it is. |
| 611 | */ |
| 612 | con->rx_page = alloc_page(GFP_ATOMIC); |
| 613 | if (con->rx_page == NULL) |
| 614 | goto out_resched; |
| 615 | cbuf_init(&con->cb, PAGE_CACHE_SIZE); |
| 616 | } |
| 617 | |
| 618 | /* Only SCTP needs these really */ |
| 619 | memset(&incmsg, 0, sizeof(incmsg)); |
| 620 | msg.msg_control = incmsg; |
| 621 | msg.msg_controllen = sizeof(incmsg); |
| 622 | |
| 623 | /* |
| 624 | * iov[0] is the bit of the circular buffer between the current end |
| 625 | * point (cb.base + cb.len) and the end of the buffer. |
| 626 | */ |
| 627 | iov[0].iov_len = con->cb.base - cbuf_data(&con->cb); |
| 628 | iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb); |
| 629 | iov[1].iov_len = 0; |
| 630 | nvec = 1; |
| 631 | |
| 632 | /* |
| 633 | * iov[1] is the bit of the circular buffer between the start of the |
| 634 | * buffer and the start of the currently used section (cb.base) |
| 635 | */ |
| 636 | if (cbuf_data(&con->cb) >= con->cb.base) { |
| 637 | iov[0].iov_len = PAGE_CACHE_SIZE - cbuf_data(&con->cb); |
| 638 | iov[1].iov_len = con->cb.base; |
| 639 | iov[1].iov_base = page_address(con->rx_page); |
| 640 | nvec = 2; |
| 641 | } |
| 642 | len = iov[0].iov_len + iov[1].iov_len; |
| 643 | |
| 644 | r = ret = kernel_recvmsg(con->sock, &msg, iov, nvec, len, |
| 645 | MSG_DONTWAIT | MSG_NOSIGNAL); |
| 646 | if (ret <= 0) |
| 647 | goto out_close; |
| 648 | |
| 649 | /* Process SCTP notifications */ |
| 650 | if (msg.msg_flags & MSG_NOTIFICATION) { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 651 | msg.msg_control = incmsg; |
| 652 | msg.msg_controllen = sizeof(incmsg); |
| 653 | |
| 654 | process_sctp_notification(con, &msg, |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 655 | page_address(con->rx_page) + con->cb.base); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 656 | mutex_unlock(&con->sock_mutex); |
| 657 | return 0; |
| 658 | } |
| 659 | BUG_ON(con->nodeid == 0); |
| 660 | |
| 661 | if (ret == len) |
| 662 | call_again_soon = 1; |
| 663 | cbuf_add(&con->cb, ret); |
| 664 | ret = dlm_process_incoming_buffer(con->nodeid, |
| 665 | page_address(con->rx_page), |
| 666 | con->cb.base, con->cb.len, |
| 667 | PAGE_CACHE_SIZE); |
| 668 | if (ret == -EBADMSG) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 669 | log_print("lowcomms: addr=%p, base=%u, len=%u, " |
| 670 | "iov_len=%u, iov_base[0]=%p, read=%d", |
| 671 | page_address(con->rx_page), con->cb.base, con->cb.len, |
| 672 | len, iov[0].iov_base, r); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 673 | } |
| 674 | if (ret < 0) |
| 675 | goto out_close; |
| 676 | cbuf_eat(&con->cb, ret); |
| 677 | |
| 678 | if (cbuf_empty(&con->cb) && !call_again_soon) { |
| 679 | __free_page(con->rx_page); |
| 680 | con->rx_page = NULL; |
| 681 | } |
| 682 | |
| 683 | if (call_again_soon) |
| 684 | goto out_resched; |
| 685 | mutex_unlock(&con->sock_mutex); |
| 686 | return 0; |
| 687 | |
| 688 | out_resched: |
| 689 | if (!test_and_set_bit(CF_READ_PENDING, &con->flags)) |
| 690 | queue_work(recv_workqueue, &con->rwork); |
| 691 | mutex_unlock(&con->sock_mutex); |
| 692 | return -EAGAIN; |
| 693 | |
| 694 | out_close: |
| 695 | mutex_unlock(&con->sock_mutex); |
Patrick Caulfield | 9e5f282 | 2007-08-02 14:58:14 +0100 | [diff] [blame] | 696 | if (ret != -EAGAIN) { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 697 | close_connection(con, false); |
| 698 | /* Reconnect when there is something to send */ |
| 699 | } |
| 700 | /* Don't return success if we really got EOF */ |
| 701 | if (ret == 0) |
| 702 | ret = -EAGAIN; |
| 703 | |
| 704 | return ret; |
| 705 | } |
| 706 | |
| 707 | /* Listening socket is busy, accept a connection */ |
| 708 | static int tcp_accept_from_sock(struct connection *con) |
| 709 | { |
| 710 | int result; |
| 711 | struct sockaddr_storage peeraddr; |
| 712 | struct socket *newsock; |
| 713 | int len; |
| 714 | int nodeid; |
| 715 | struct connection *newcon; |
| 716 | struct connection *addcon; |
| 717 | |
| 718 | memset(&peeraddr, 0, sizeof(peeraddr)); |
| 719 | result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM, |
| 720 | IPPROTO_TCP, &newsock); |
| 721 | if (result < 0) |
| 722 | return -ENOMEM; |
| 723 | |
| 724 | mutex_lock_nested(&con->sock_mutex, 0); |
| 725 | |
| 726 | result = -ENOTCONN; |
| 727 | if (con->sock == NULL) |
| 728 | goto accept_err; |
| 729 | |
| 730 | newsock->type = con->sock->type; |
| 731 | newsock->ops = con->sock->ops; |
| 732 | |
| 733 | result = con->sock->ops->accept(con->sock, newsock, O_NONBLOCK); |
| 734 | if (result < 0) |
| 735 | goto accept_err; |
| 736 | |
| 737 | /* Get the connected socket's peer */ |
| 738 | memset(&peeraddr, 0, sizeof(peeraddr)); |
| 739 | if (newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr, |
| 740 | &len, 2)) { |
| 741 | result = -ECONNABORTED; |
| 742 | goto accept_err; |
| 743 | } |
| 744 | |
| 745 | /* Get the new node's NODEID */ |
| 746 | make_sockaddr(&peeraddr, 0, &len); |
| 747 | if (dlm_addr_to_nodeid(&peeraddr, &nodeid)) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 748 | log_print("connect from non cluster node"); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 749 | sock_release(newsock); |
| 750 | mutex_unlock(&con->sock_mutex); |
| 751 | return -1; |
| 752 | } |
| 753 | |
| 754 | log_print("got connection from %d", nodeid); |
| 755 | |
| 756 | /* Check to see if we already have a connection to this node. This |
| 757 | * could happen if the two nodes initiate a connection at roughly |
| 758 | * the same time and the connections cross on the wire. |
| 759 | * In this case we store the incoming one in "othercon" |
| 760 | */ |
David Teigland | 748285c | 2009-05-15 10:50:57 -0500 | [diff] [blame] | 761 | newcon = nodeid2con(nodeid, GFP_NOFS); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 762 | if (!newcon) { |
| 763 | result = -ENOMEM; |
| 764 | goto accept_err; |
| 765 | } |
| 766 | mutex_lock_nested(&newcon->sock_mutex, 1); |
| 767 | if (newcon->sock) { |
| 768 | struct connection *othercon = newcon->othercon; |
| 769 | |
| 770 | if (!othercon) { |
David Teigland | 748285c | 2009-05-15 10:50:57 -0500 | [diff] [blame] | 771 | othercon = kmem_cache_zalloc(con_cache, GFP_NOFS); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 772 | if (!othercon) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 773 | log_print("failed to allocate incoming socket"); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 774 | mutex_unlock(&newcon->sock_mutex); |
| 775 | result = -ENOMEM; |
| 776 | goto accept_err; |
| 777 | } |
| 778 | othercon->nodeid = nodeid; |
| 779 | othercon->rx_action = receive_from_sock; |
| 780 | mutex_init(&othercon->sock_mutex); |
| 781 | INIT_WORK(&othercon->swork, process_send_sockets); |
| 782 | INIT_WORK(&othercon->rwork, process_recv_sockets); |
| 783 | set_bit(CF_IS_OTHERCON, &othercon->flags); |
Patrick Caulfield | 61d96be0 | 2007-08-20 15:13:38 +0100 | [diff] [blame] | 784 | } |
| 785 | if (!othercon->sock) { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 786 | newcon->othercon = othercon; |
Patrick Caulfield | 97d8483 | 2007-06-27 11:36:23 +0100 | [diff] [blame] | 787 | othercon->sock = newsock; |
| 788 | newsock->sk->sk_user_data = othercon; |
| 789 | add_sock(newsock, othercon); |
| 790 | addcon = othercon; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 791 | } |
Patrick Caulfield | 97d8483 | 2007-06-27 11:36:23 +0100 | [diff] [blame] | 792 | else { |
| 793 | printk("Extra connection from node %d attempted\n", nodeid); |
| 794 | result = -EAGAIN; |
akpm@linux-foundation.org | f4fadb23c | 2007-06-27 14:43:37 -0700 | [diff] [blame] | 795 | mutex_unlock(&newcon->sock_mutex); |
Patrick Caulfield | 97d8483 | 2007-06-27 11:36:23 +0100 | [diff] [blame] | 796 | goto accept_err; |
| 797 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 798 | } |
| 799 | else { |
| 800 | newsock->sk->sk_user_data = newcon; |
| 801 | newcon->rx_action = receive_from_sock; |
| 802 | add_sock(newsock, newcon); |
| 803 | addcon = newcon; |
| 804 | } |
| 805 | |
| 806 | mutex_unlock(&newcon->sock_mutex); |
| 807 | |
| 808 | /* |
| 809 | * Add it to the active queue in case we got data |
| 810 | * beween processing the accept adding the socket |
| 811 | * to the read_sockets list |
| 812 | */ |
| 813 | if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags)) |
| 814 | queue_work(recv_workqueue, &addcon->rwork); |
| 815 | mutex_unlock(&con->sock_mutex); |
| 816 | |
| 817 | return 0; |
| 818 | |
| 819 | accept_err: |
| 820 | mutex_unlock(&con->sock_mutex); |
| 821 | sock_release(newsock); |
| 822 | |
| 823 | if (result != -EAGAIN) |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 824 | log_print("error accepting connection from node: %d", result); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 825 | return result; |
| 826 | } |
| 827 | |
| 828 | static void free_entry(struct writequeue_entry *e) |
| 829 | { |
| 830 | __free_page(e->page); |
| 831 | kfree(e); |
| 832 | } |
| 833 | |
| 834 | /* Initiate an SCTP association. |
| 835 | This is a special case of send_to_sock() in that we don't yet have a |
| 836 | peeled-off socket for this association, so we use the listening socket |
| 837 | and add the primary IP address of the remote node. |
| 838 | */ |
| 839 | static void sctp_init_assoc(struct connection *con) |
| 840 | { |
| 841 | struct sockaddr_storage rem_addr; |
| 842 | char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; |
| 843 | struct msghdr outmessage; |
| 844 | struct cmsghdr *cmsg; |
| 845 | struct sctp_sndrcvinfo *sinfo; |
| 846 | struct connection *base_con; |
| 847 | struct writequeue_entry *e; |
| 848 | int len, offset; |
| 849 | int ret; |
| 850 | int addrlen; |
| 851 | struct kvec iov[1]; |
| 852 | |
| 853 | if (test_and_set_bit(CF_INIT_PENDING, &con->flags)) |
| 854 | return; |
| 855 | |
| 856 | if (con->retries++ > MAX_CONNECT_RETRIES) |
| 857 | return; |
| 858 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 859 | if (nodeid_to_addr(con->nodeid, (struct sockaddr *)&rem_addr)) { |
| 860 | log_print("no address for nodeid %d", con->nodeid); |
| 861 | return; |
| 862 | } |
| 863 | base_con = nodeid2con(0, 0); |
| 864 | BUG_ON(base_con == NULL); |
| 865 | |
| 866 | make_sockaddr(&rem_addr, dlm_config.ci_tcp_port, &addrlen); |
| 867 | |
| 868 | outmessage.msg_name = &rem_addr; |
| 869 | outmessage.msg_namelen = addrlen; |
| 870 | outmessage.msg_control = outcmsg; |
| 871 | outmessage.msg_controllen = sizeof(outcmsg); |
| 872 | outmessage.msg_flags = MSG_EOR; |
| 873 | |
| 874 | spin_lock(&con->writequeue_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 875 | |
David Teigland | 04bedd7 | 2009-09-18 14:31:47 -0500 | [diff] [blame] | 876 | if (list_empty(&con->writequeue)) { |
| 877 | spin_unlock(&con->writequeue_lock); |
| 878 | log_print("writequeue empty for nodeid %d", con->nodeid); |
| 879 | return; |
| 880 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 881 | |
David Teigland | 04bedd7 | 2009-09-18 14:31:47 -0500 | [diff] [blame] | 882 | e = list_first_entry(&con->writequeue, struct writequeue_entry, list); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 883 | len = e->len; |
| 884 | offset = e->offset; |
| 885 | spin_unlock(&con->writequeue_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 886 | |
| 887 | /* Send the first block off the write queue */ |
| 888 | iov[0].iov_base = page_address(e->page)+offset; |
| 889 | iov[0].iov_len = len; |
| 890 | |
| 891 | cmsg = CMSG_FIRSTHDR(&outmessage); |
| 892 | cmsg->cmsg_level = IPPROTO_SCTP; |
| 893 | cmsg->cmsg_type = SCTP_SNDRCV; |
| 894 | cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); |
| 895 | sinfo = CMSG_DATA(cmsg); |
| 896 | memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo)); |
| 897 | sinfo->sinfo_ppid = cpu_to_le32(dlm_our_nodeid()); |
| 898 | outmessage.msg_controllen = cmsg->cmsg_len; |
| 899 | |
| 900 | ret = kernel_sendmsg(base_con->sock, &outmessage, iov, 1, len); |
| 901 | if (ret < 0) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 902 | log_print("Send first packet to node %d failed: %d", |
| 903 | con->nodeid, ret); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 904 | |
| 905 | /* Try again later */ |
| 906 | clear_bit(CF_CONNECT_PENDING, &con->flags); |
| 907 | clear_bit(CF_INIT_PENDING, &con->flags); |
| 908 | } |
| 909 | else { |
| 910 | spin_lock(&con->writequeue_lock); |
| 911 | e->offset += ret; |
| 912 | e->len -= ret; |
| 913 | |
| 914 | if (e->len == 0 && e->users == 0) { |
| 915 | list_del(&e->list); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 916 | free_entry(e); |
| 917 | } |
| 918 | spin_unlock(&con->writequeue_lock); |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | /* Connect a new socket to its peer */ |
| 923 | static void tcp_connect_to_sock(struct connection *con) |
| 924 | { |
| 925 | int result = -EHOSTUNREACH; |
Lon Hohberger | 6bd8fed | 2007-10-25 18:51:54 -0400 | [diff] [blame] | 926 | struct sockaddr_storage saddr, src_addr; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 927 | int addr_len; |
Casey Dahlin | a89d63a | 2009-07-14 12:17:51 -0500 | [diff] [blame] | 928 | struct socket *sock = NULL; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 929 | |
| 930 | if (con->nodeid == 0) { |
| 931 | log_print("attempt to connect sock 0 foiled"); |
| 932 | return; |
| 933 | } |
| 934 | |
| 935 | mutex_lock(&con->sock_mutex); |
| 936 | if (con->retries++ > MAX_CONNECT_RETRIES) |
| 937 | goto out; |
| 938 | |
| 939 | /* Some odd races can cause double-connects, ignore them */ |
| 940 | if (con->sock) { |
| 941 | result = 0; |
| 942 | goto out; |
| 943 | } |
| 944 | |
| 945 | /* Create a socket to communicate with */ |
| 946 | result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM, |
| 947 | IPPROTO_TCP, &sock); |
| 948 | if (result < 0) |
| 949 | goto out_err; |
| 950 | |
| 951 | memset(&saddr, 0, sizeof(saddr)); |
Casey Dahlin | b5711b8 | 2009-07-28 12:29:05 -0500 | [diff] [blame] | 952 | if (dlm_nodeid_to_addr(con->nodeid, &saddr)) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 953 | goto out_err; |
| 954 | |
| 955 | sock->sk->sk_user_data = con; |
| 956 | con->rx_action = receive_from_sock; |
| 957 | con->connect_action = tcp_connect_to_sock; |
| 958 | add_sock(sock, con); |
| 959 | |
Lon Hohberger | 6bd8fed | 2007-10-25 18:51:54 -0400 | [diff] [blame] | 960 | /* Bind to our cluster-known address connecting to avoid |
| 961 | routing problems */ |
| 962 | memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr)); |
| 963 | make_sockaddr(&src_addr, 0, &addr_len); |
| 964 | result = sock->ops->bind(sock, (struct sockaddr *) &src_addr, |
| 965 | addr_len); |
| 966 | if (result < 0) { |
| 967 | log_print("could not bind for connect: %d", result); |
| 968 | /* This *may* not indicate a critical error */ |
| 969 | } |
| 970 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 971 | make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len); |
| 972 | |
| 973 | log_print("connecting to %d", con->nodeid); |
| 974 | result = |
| 975 | sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len, |
| 976 | O_NONBLOCK); |
| 977 | if (result == -EINPROGRESS) |
| 978 | result = 0; |
| 979 | if (result == 0) |
| 980 | goto out; |
| 981 | |
| 982 | out_err: |
| 983 | if (con->sock) { |
| 984 | sock_release(con->sock); |
| 985 | con->sock = NULL; |
Casey Dahlin | a89d63a | 2009-07-14 12:17:51 -0500 | [diff] [blame] | 986 | } else if (sock) { |
| 987 | sock_release(sock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 988 | } |
| 989 | /* |
| 990 | * Some errors are fatal and this list might need adjusting. For other |
| 991 | * errors we try again until the max number of retries is reached. |
| 992 | */ |
| 993 | if (result != -EHOSTUNREACH && result != -ENETUNREACH && |
Marcin Slusarz | 0035a4b | 2008-05-11 22:01:29 +0200 | [diff] [blame] | 994 | result != -ENETDOWN && result != -EINVAL |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 995 | && result != -EPROTONOSUPPORT) { |
| 996 | lowcomms_connect_sock(con); |
| 997 | result = 0; |
| 998 | } |
| 999 | out: |
| 1000 | mutex_unlock(&con->sock_mutex); |
| 1001 | return; |
| 1002 | } |
| 1003 | |
| 1004 | static struct socket *tcp_create_listen_sock(struct connection *con, |
| 1005 | struct sockaddr_storage *saddr) |
| 1006 | { |
| 1007 | struct socket *sock = NULL; |
| 1008 | int result = 0; |
| 1009 | int one = 1; |
| 1010 | int addr_len; |
| 1011 | |
| 1012 | if (dlm_local_addr[0]->ss_family == AF_INET) |
| 1013 | addr_len = sizeof(struct sockaddr_in); |
| 1014 | else |
| 1015 | addr_len = sizeof(struct sockaddr_in6); |
| 1016 | |
| 1017 | /* Create a socket to communicate with */ |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1018 | result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM, |
| 1019 | IPPROTO_TCP, &sock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1020 | if (result < 0) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1021 | log_print("Can't create listening comms socket"); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1022 | goto create_out; |
| 1023 | } |
| 1024 | |
| 1025 | result = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, |
| 1026 | (char *)&one, sizeof(one)); |
| 1027 | |
| 1028 | if (result < 0) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1029 | log_print("Failed to set SO_REUSEADDR on socket: %d", result); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1030 | } |
| 1031 | sock->sk->sk_user_data = con; |
| 1032 | con->rx_action = tcp_accept_from_sock; |
| 1033 | con->connect_action = tcp_connect_to_sock; |
| 1034 | con->sock = sock; |
| 1035 | |
| 1036 | /* Bind to our port */ |
| 1037 | make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len); |
| 1038 | result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len); |
| 1039 | if (result < 0) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1040 | 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] | 1041 | sock_release(sock); |
| 1042 | sock = NULL; |
| 1043 | con->sock = NULL; |
| 1044 | goto create_out; |
| 1045 | } |
| 1046 | result = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, |
| 1047 | (char *)&one, sizeof(one)); |
| 1048 | if (result < 0) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1049 | log_print("Set keepalive failed: %d", result); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | result = sock->ops->listen(sock, 5); |
| 1053 | if (result < 0) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1054 | 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] | 1055 | sock_release(sock); |
| 1056 | sock = NULL; |
| 1057 | goto create_out; |
| 1058 | } |
| 1059 | |
| 1060 | create_out: |
| 1061 | return sock; |
| 1062 | } |
| 1063 | |
| 1064 | /* Get local addresses */ |
| 1065 | static void init_local(void) |
| 1066 | { |
| 1067 | struct sockaddr_storage sas, *addr; |
| 1068 | int i; |
| 1069 | |
Patrick Caulfield | 30d3a23 | 2007-04-23 16:26:21 +0100 | [diff] [blame] | 1070 | dlm_local_count = 0; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1071 | for (i = 0; i < DLM_MAX_ADDR_COUNT - 1; i++) { |
| 1072 | if (dlm_our_addr(&sas, i)) |
| 1073 | break; |
| 1074 | |
David Teigland | 573c24c | 2009-11-30 16:34:43 -0600 | [diff] [blame] | 1075 | addr = kmalloc(sizeof(*addr), GFP_NOFS); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1076 | if (!addr) |
| 1077 | break; |
| 1078 | memcpy(addr, &sas, sizeof(*addr)); |
| 1079 | dlm_local_addr[dlm_local_count++] = addr; |
| 1080 | } |
| 1081 | } |
| 1082 | |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1083 | /* Bind to an IP address. SCTP allows multiple address so it can do |
| 1084 | multi-homing */ |
| 1085 | static int add_sctp_bind_addr(struct connection *sctp_con, |
| 1086 | struct sockaddr_storage *addr, |
| 1087 | int addr_len, int num) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1088 | { |
| 1089 | int result = 0; |
| 1090 | |
| 1091 | if (num == 1) |
| 1092 | result = kernel_bind(sctp_con->sock, |
| 1093 | (struct sockaddr *) addr, |
| 1094 | addr_len); |
| 1095 | else |
| 1096 | result = kernel_setsockopt(sctp_con->sock, SOL_SCTP, |
| 1097 | SCTP_SOCKOPT_BINDX_ADD, |
| 1098 | (char *)addr, addr_len); |
| 1099 | |
| 1100 | if (result < 0) |
| 1101 | log_print("Can't bind to port %d addr number %d", |
| 1102 | dlm_config.ci_tcp_port, num); |
| 1103 | |
| 1104 | return result; |
| 1105 | } |
| 1106 | |
| 1107 | /* Initialise SCTP socket and bind to all interfaces */ |
| 1108 | static int sctp_listen_for_all(void) |
| 1109 | { |
| 1110 | struct socket *sock = NULL; |
| 1111 | struct sockaddr_storage localaddr; |
| 1112 | struct sctp_event_subscribe subscribe; |
| 1113 | int result = -EINVAL, num = 1, i, addr_len; |
David Teigland | 573c24c | 2009-11-30 16:34:43 -0600 | [diff] [blame] | 1114 | struct connection *con = nodeid2con(0, GFP_NOFS); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1115 | int bufsize = NEEDED_RMEM; |
| 1116 | |
| 1117 | if (!con) |
| 1118 | return -ENOMEM; |
| 1119 | |
| 1120 | log_print("Using SCTP for communications"); |
| 1121 | |
| 1122 | result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_SEQPACKET, |
| 1123 | IPPROTO_SCTP, &sock); |
| 1124 | if (result < 0) { |
| 1125 | log_print("Can't create comms socket, check SCTP is loaded"); |
| 1126 | goto out; |
| 1127 | } |
| 1128 | |
| 1129 | /* Listen for events */ |
| 1130 | memset(&subscribe, 0, sizeof(subscribe)); |
| 1131 | subscribe.sctp_data_io_event = 1; |
| 1132 | subscribe.sctp_association_event = 1; |
| 1133 | subscribe.sctp_send_failure_event = 1; |
| 1134 | subscribe.sctp_shutdown_event = 1; |
| 1135 | subscribe.sctp_partial_delivery_event = 1; |
| 1136 | |
David S. Miller | df61c95 | 2007-11-06 23:48:57 -0800 | [diff] [blame] | 1137 | result = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUFFORCE, |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1138 | (char *)&bufsize, sizeof(bufsize)); |
| 1139 | if (result) |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1140 | log_print("Error increasing buffer space on socket %d", result); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1141 | |
| 1142 | result = kernel_setsockopt(sock, SOL_SCTP, SCTP_EVENTS, |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1143 | (char *)&subscribe, sizeof(subscribe)); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1144 | if (result < 0) { |
| 1145 | log_print("Failed to set SCTP_EVENTS on socket: result=%d", |
| 1146 | result); |
| 1147 | goto create_delsock; |
| 1148 | } |
| 1149 | |
| 1150 | /* Init con struct */ |
| 1151 | sock->sk->sk_user_data = con; |
| 1152 | con->sock = sock; |
| 1153 | con->sock->sk->sk_data_ready = lowcomms_data_ready; |
| 1154 | con->rx_action = receive_from_sock; |
| 1155 | con->connect_action = sctp_init_assoc; |
| 1156 | |
| 1157 | /* Bind to all interfaces. */ |
| 1158 | for (i = 0; i < dlm_local_count; i++) { |
| 1159 | memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr)); |
| 1160 | make_sockaddr(&localaddr, dlm_config.ci_tcp_port, &addr_len); |
| 1161 | |
| 1162 | result = add_sctp_bind_addr(con, &localaddr, addr_len, num); |
| 1163 | if (result) |
| 1164 | goto create_delsock; |
| 1165 | ++num; |
| 1166 | } |
| 1167 | |
| 1168 | result = sock->ops->listen(sock, 5); |
| 1169 | if (result < 0) { |
| 1170 | log_print("Can't set socket listening"); |
| 1171 | goto create_delsock; |
| 1172 | } |
| 1173 | |
| 1174 | return 0; |
| 1175 | |
| 1176 | create_delsock: |
| 1177 | sock_release(sock); |
| 1178 | con->sock = NULL; |
| 1179 | out: |
| 1180 | return result; |
| 1181 | } |
| 1182 | |
| 1183 | static int tcp_listen_for_all(void) |
| 1184 | { |
| 1185 | struct socket *sock = NULL; |
David Teigland | 573c24c | 2009-11-30 16:34:43 -0600 | [diff] [blame] | 1186 | struct connection *con = nodeid2con(0, GFP_NOFS); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1187 | int result = -EINVAL; |
| 1188 | |
| 1189 | if (!con) |
| 1190 | return -ENOMEM; |
| 1191 | |
| 1192 | /* We don't support multi-homed hosts */ |
| 1193 | if (dlm_local_addr[1] != NULL) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1194 | log_print("TCP protocol can't handle multi-homed hosts, " |
| 1195 | "try SCTP"); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1196 | return -EINVAL; |
| 1197 | } |
| 1198 | |
| 1199 | log_print("Using TCP for communications"); |
| 1200 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1201 | sock = tcp_create_listen_sock(con, dlm_local_addr[0]); |
| 1202 | if (sock) { |
| 1203 | add_sock(sock, con); |
| 1204 | result = 0; |
| 1205 | } |
| 1206 | else { |
| 1207 | result = -EADDRINUSE; |
| 1208 | } |
| 1209 | |
| 1210 | return result; |
| 1211 | } |
| 1212 | |
| 1213 | |
| 1214 | |
| 1215 | static struct writequeue_entry *new_writequeue_entry(struct connection *con, |
| 1216 | gfp_t allocation) |
| 1217 | { |
| 1218 | struct writequeue_entry *entry; |
| 1219 | |
| 1220 | entry = kmalloc(sizeof(struct writequeue_entry), allocation); |
| 1221 | if (!entry) |
| 1222 | return NULL; |
| 1223 | |
| 1224 | entry->page = alloc_page(allocation); |
| 1225 | if (!entry->page) { |
| 1226 | kfree(entry); |
| 1227 | return NULL; |
| 1228 | } |
| 1229 | |
| 1230 | entry->offset = 0; |
| 1231 | entry->len = 0; |
| 1232 | entry->end = 0; |
| 1233 | entry->users = 0; |
| 1234 | entry->con = con; |
| 1235 | |
| 1236 | return entry; |
| 1237 | } |
| 1238 | |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1239 | void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc) |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1240 | { |
| 1241 | struct connection *con; |
| 1242 | struct writequeue_entry *e; |
| 1243 | int offset = 0; |
| 1244 | int users = 0; |
| 1245 | |
| 1246 | con = nodeid2con(nodeid, allocation); |
| 1247 | if (!con) |
| 1248 | return NULL; |
| 1249 | |
| 1250 | spin_lock(&con->writequeue_lock); |
| 1251 | e = list_entry(con->writequeue.prev, struct writequeue_entry, list); |
| 1252 | if ((&e->list == &con->writequeue) || |
| 1253 | (PAGE_CACHE_SIZE - e->end < len)) { |
| 1254 | e = NULL; |
| 1255 | } else { |
| 1256 | offset = e->end; |
| 1257 | e->end += len; |
| 1258 | users = e->users++; |
| 1259 | } |
| 1260 | spin_unlock(&con->writequeue_lock); |
| 1261 | |
| 1262 | if (e) { |
| 1263 | got_one: |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1264 | *ppc = page_address(e->page) + offset; |
| 1265 | return e; |
| 1266 | } |
| 1267 | |
| 1268 | e = new_writequeue_entry(con, allocation); |
| 1269 | if (e) { |
| 1270 | spin_lock(&con->writequeue_lock); |
| 1271 | offset = e->end; |
| 1272 | e->end += len; |
| 1273 | users = e->users++; |
| 1274 | list_add_tail(&e->list, &con->writequeue); |
| 1275 | spin_unlock(&con->writequeue_lock); |
| 1276 | goto got_one; |
| 1277 | } |
| 1278 | return NULL; |
| 1279 | } |
| 1280 | |
| 1281 | void dlm_lowcomms_commit_buffer(void *mh) |
| 1282 | { |
| 1283 | struct writequeue_entry *e = (struct writequeue_entry *)mh; |
| 1284 | struct connection *con = e->con; |
| 1285 | int users; |
| 1286 | |
| 1287 | spin_lock(&con->writequeue_lock); |
| 1288 | users = --e->users; |
| 1289 | if (users) |
| 1290 | goto out; |
| 1291 | e->len = e->end - e->offset; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1292 | spin_unlock(&con->writequeue_lock); |
| 1293 | |
| 1294 | if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) { |
| 1295 | queue_work(send_workqueue, &con->swork); |
| 1296 | } |
| 1297 | return; |
| 1298 | |
| 1299 | out: |
| 1300 | spin_unlock(&con->writequeue_lock); |
| 1301 | return; |
| 1302 | } |
| 1303 | |
| 1304 | /* Send a message */ |
| 1305 | static void send_to_sock(struct connection *con) |
| 1306 | { |
| 1307 | int ret = 0; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1308 | const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL; |
| 1309 | struct writequeue_entry *e; |
| 1310 | int len, offset; |
| 1311 | |
| 1312 | mutex_lock(&con->sock_mutex); |
| 1313 | if (con->sock == NULL) |
| 1314 | goto out_connect; |
| 1315 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1316 | spin_lock(&con->writequeue_lock); |
| 1317 | for (;;) { |
| 1318 | e = list_entry(con->writequeue.next, struct writequeue_entry, |
| 1319 | list); |
| 1320 | if ((struct list_head *) e == &con->writequeue) |
| 1321 | break; |
| 1322 | |
| 1323 | len = e->len; |
| 1324 | offset = e->offset; |
| 1325 | BUG_ON(len == 0 && e->users == 0); |
| 1326 | spin_unlock(&con->writequeue_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1327 | |
| 1328 | ret = 0; |
| 1329 | if (len) { |
Paolo Bonzini | 1329e3f | 2009-08-24 13:18:04 -0500 | [diff] [blame] | 1330 | ret = kernel_sendpage(con->sock, e->page, offset, len, |
| 1331 | msg_flags); |
Patrick Caulfield | d66f827 | 2007-09-14 08:49:21 +0100 | [diff] [blame] | 1332 | if (ret == -EAGAIN || ret == 0) { |
David Miller | b36930d | 2010-11-10 21:56:39 -0800 | [diff] [blame] | 1333 | if (ret == -EAGAIN && |
| 1334 | test_bit(SOCK_ASYNC_NOSPACE, &con->sock->flags) && |
| 1335 | !test_and_set_bit(CF_APP_LIMITED, &con->flags)) { |
| 1336 | /* Notify TCP that we're limited by the |
| 1337 | * application window size. |
| 1338 | */ |
| 1339 | set_bit(SOCK_NOSPACE, &con->sock->flags); |
| 1340 | con->sock->sk->sk_write_pending++; |
| 1341 | } |
Patrick Caulfield | d66f827 | 2007-09-14 08:49:21 +0100 | [diff] [blame] | 1342 | cond_resched(); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1343 | goto out; |
Patrick Caulfield | d66f827 | 2007-09-14 08:49:21 +0100 | [diff] [blame] | 1344 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1345 | if (ret <= 0) |
| 1346 | goto send_error; |
Patrick Caulfield | d66f827 | 2007-09-14 08:49:21 +0100 | [diff] [blame] | 1347 | } |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1348 | /* Don't starve people filling buffers */ |
| 1349 | cond_resched(); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1350 | |
| 1351 | spin_lock(&con->writequeue_lock); |
| 1352 | e->offset += ret; |
| 1353 | e->len -= ret; |
| 1354 | |
| 1355 | if (e->len == 0 && e->users == 0) { |
| 1356 | list_del(&e->list); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1357 | free_entry(e); |
| 1358 | continue; |
| 1359 | } |
| 1360 | } |
| 1361 | spin_unlock(&con->writequeue_lock); |
| 1362 | out: |
| 1363 | mutex_unlock(&con->sock_mutex); |
| 1364 | return; |
| 1365 | |
| 1366 | send_error: |
| 1367 | mutex_unlock(&con->sock_mutex); |
| 1368 | close_connection(con, false); |
| 1369 | lowcomms_connect_sock(con); |
| 1370 | return; |
| 1371 | |
| 1372 | out_connect: |
| 1373 | mutex_unlock(&con->sock_mutex); |
| 1374 | if (!test_bit(CF_INIT_PENDING, &con->flags)) |
| 1375 | lowcomms_connect_sock(con); |
| 1376 | return; |
| 1377 | } |
| 1378 | |
| 1379 | static void clean_one_writequeue(struct connection *con) |
| 1380 | { |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1381 | struct writequeue_entry *e, *safe; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1382 | |
| 1383 | spin_lock(&con->writequeue_lock); |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1384 | list_for_each_entry_safe(e, safe, &con->writequeue, list) { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1385 | list_del(&e->list); |
| 1386 | free_entry(e); |
| 1387 | } |
| 1388 | spin_unlock(&con->writequeue_lock); |
| 1389 | } |
| 1390 | |
| 1391 | /* Called from recovery when it knows that a node has |
| 1392 | left the cluster */ |
| 1393 | int dlm_lowcomms_close(int nodeid) |
| 1394 | { |
| 1395 | struct connection *con; |
| 1396 | |
| 1397 | log_print("closing connection to node %d", nodeid); |
| 1398 | con = nodeid2con(nodeid, 0); |
| 1399 | if (con) { |
Lars Marowsky-Bree | 063c4c9 | 2009-08-11 16:18:23 -0500 | [diff] [blame] | 1400 | clear_bit(CF_CONNECT_PENDING, &con->flags); |
| 1401 | clear_bit(CF_WRITE_PENDING, &con->flags); |
| 1402 | set_bit(CF_CLOSE, &con->flags); |
| 1403 | if (cancel_work_sync(&con->swork)) |
| 1404 | log_print("canceled swork for node %d", nodeid); |
| 1405 | if (cancel_work_sync(&con->rwork)) |
| 1406 | log_print("canceled rwork for node %d", nodeid); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1407 | clean_one_writequeue(con); |
| 1408 | close_connection(con, true); |
| 1409 | } |
| 1410 | return 0; |
| 1411 | } |
| 1412 | |
| 1413 | /* Receive workqueue function */ |
| 1414 | static void process_recv_sockets(struct work_struct *work) |
| 1415 | { |
| 1416 | struct connection *con = container_of(work, struct connection, rwork); |
| 1417 | int err; |
| 1418 | |
| 1419 | clear_bit(CF_READ_PENDING, &con->flags); |
| 1420 | do { |
| 1421 | err = con->rx_action(con); |
| 1422 | } while (!err); |
| 1423 | } |
| 1424 | |
| 1425 | /* Send workqueue function */ |
| 1426 | static void process_send_sockets(struct work_struct *work) |
| 1427 | { |
| 1428 | struct connection *con = container_of(work, struct connection, swork); |
| 1429 | |
| 1430 | if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) { |
| 1431 | con->connect_action(con); |
Lars Marowsky-Bree | 063c4c9 | 2009-08-11 16:18:23 -0500 | [diff] [blame] | 1432 | set_bit(CF_WRITE_PENDING, &con->flags); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1433 | } |
Lars Marowsky-Bree | 063c4c9 | 2009-08-11 16:18:23 -0500 | [diff] [blame] | 1434 | if (test_and_clear_bit(CF_WRITE_PENDING, &con->flags)) |
| 1435 | send_to_sock(con); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | |
| 1439 | /* Discard all entries on the write queues */ |
| 1440 | static void clean_writequeues(void) |
| 1441 | { |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1442 | foreach_conn(clean_one_writequeue); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | static void work_stop(void) |
| 1446 | { |
| 1447 | destroy_workqueue(recv_workqueue); |
| 1448 | destroy_workqueue(send_workqueue); |
| 1449 | } |
| 1450 | |
| 1451 | static int work_start(void) |
| 1452 | { |
| 1453 | int error; |
Steven Whitehouse | dcce240 | 2010-11-12 12:12:29 +0000 | [diff] [blame^] | 1454 | recv_workqueue = alloc_workqueue("dlm_recv", WQ_MEM_RECLAIM | |
| 1455 | WQ_HIGHPRI | WQ_FREEZEABLE, 0); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1456 | error = IS_ERR(recv_workqueue); |
| 1457 | if (error) { |
| 1458 | log_print("can't start dlm_recv %d", error); |
| 1459 | return error; |
| 1460 | } |
| 1461 | |
Steven Whitehouse | dcce240 | 2010-11-12 12:12:29 +0000 | [diff] [blame^] | 1462 | send_workqueue = alloc_workqueue("dlm_send", WQ_MEM_RECLAIM | |
| 1463 | WQ_HIGHPRI | WQ_FREEZEABLE, 0); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1464 | error = IS_ERR(send_workqueue); |
| 1465 | if (error) { |
| 1466 | log_print("can't start dlm_send %d", error); |
| 1467 | destroy_workqueue(recv_workqueue); |
| 1468 | return error; |
| 1469 | } |
| 1470 | |
| 1471 | return 0; |
| 1472 | } |
| 1473 | |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1474 | static void stop_conn(struct connection *con) |
| 1475 | { |
| 1476 | con->flags |= 0x0F; |
Christine Caulfield | 391fbdc | 2009-05-07 10:54:16 -0500 | [diff] [blame] | 1477 | if (con->sock && con->sock->sk) |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1478 | con->sock->sk->sk_user_data = NULL; |
| 1479 | } |
| 1480 | |
| 1481 | static void free_conn(struct connection *con) |
| 1482 | { |
| 1483 | close_connection(con, true); |
| 1484 | if (con->othercon) |
| 1485 | kmem_cache_free(con_cache, con->othercon); |
| 1486 | hlist_del(&con->list); |
| 1487 | kmem_cache_free(con_cache, con); |
| 1488 | } |
| 1489 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1490 | void dlm_lowcomms_stop(void) |
| 1491 | { |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1492 | /* Set all the flags to prevent any |
| 1493 | socket activity. |
| 1494 | */ |
Matthias Kaehlcke | 7a936ce | 2008-05-12 10:04:51 -0500 | [diff] [blame] | 1495 | mutex_lock(&connections_lock); |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1496 | foreach_conn(stop_conn); |
Matthias Kaehlcke | 7a936ce | 2008-05-12 10:04:51 -0500 | [diff] [blame] | 1497 | mutex_unlock(&connections_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1498 | |
| 1499 | work_stop(); |
| 1500 | |
Matthias Kaehlcke | 7a936ce | 2008-05-12 10:04:51 -0500 | [diff] [blame] | 1501 | mutex_lock(&connections_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1502 | clean_writequeues(); |
| 1503 | |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1504 | foreach_conn(free_conn); |
| 1505 | |
Matthias Kaehlcke | 7a936ce | 2008-05-12 10:04:51 -0500 | [diff] [blame] | 1506 | mutex_unlock(&connections_lock); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1507 | kmem_cache_destroy(con_cache); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | int dlm_lowcomms_start(void) |
| 1511 | { |
| 1512 | int error = -EINVAL; |
| 1513 | struct connection *con; |
Christine Caulfield | 5e9ccc3 | 2009-01-28 12:57:40 -0600 | [diff] [blame] | 1514 | int i; |
| 1515 | |
| 1516 | for (i = 0; i < CONN_HASH_SIZE; i++) |
| 1517 | INIT_HLIST_HEAD(&connection_hash[i]); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1518 | |
| 1519 | init_local(); |
| 1520 | if (!dlm_local_count) { |
David Teigland | 617e82e | 2007-04-26 13:46:49 -0500 | [diff] [blame] | 1521 | error = -ENOTCONN; |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1522 | log_print("no local IP address has been set"); |
| 1523 | goto out; |
| 1524 | } |
| 1525 | |
| 1526 | error = -ENOMEM; |
| 1527 | con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection), |
| 1528 | __alignof__(struct connection), 0, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1529 | NULL); |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1530 | if (!con_cache) |
| 1531 | goto out; |
| 1532 | |
Patrick Caulfield | 6ed7257b | 2007-04-17 15:39:57 +0100 | [diff] [blame] | 1533 | /* Start listening */ |
| 1534 | if (dlm_config.ci_protocol == 0) |
| 1535 | error = tcp_listen_for_all(); |
| 1536 | else |
| 1537 | error = sctp_listen_for_all(); |
| 1538 | if (error) |
| 1539 | goto fail_unlisten; |
| 1540 | |
| 1541 | error = work_start(); |
| 1542 | if (error) |
| 1543 | goto fail_unlisten; |
| 1544 | |
| 1545 | return 0; |
| 1546 | |
| 1547 | fail_unlisten: |
| 1548 | con = nodeid2con(0,0); |
| 1549 | if (con) { |
| 1550 | close_connection(con, false); |
| 1551 | kmem_cache_free(con_cache, con); |
| 1552 | } |
| 1553 | kmem_cache_destroy(con_cache); |
| 1554 | |
| 1555 | out: |
| 1556 | return error; |
| 1557 | } |