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