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