1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 drbd_receiver.c
4
5 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
6
7 Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
8 Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
9 Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
10
11 */
12
13
14 #include <linux/module.h>
15
16 #include <linux/uaccess.h>
17 #include <net/sock.h>
18
19 #include <linux/drbd.h>
20 #include <linux/fs.h>
21 #include <linux/file.h>
22 #include <linux/in.h>
23 #include <linux/mm.h>
24 #include <linux/memcontrol.h>
25 #include <linux/mm_inline.h>
26 #include <linux/slab.h>
27 #include <uapi/linux/sched/types.h>
28 #include <linux/sched/signal.h>
29 #include <linux/pkt_sched.h>
30 #include <linux/unistd.h>
31 #include <linux/vmalloc.h>
32 #include <linux/random.h>
33 #include <linux/string.h>
34 #include <linux/scatterlist.h>
35 #include <linux/part_stat.h>
36 #include <linux/mempool.h>
37 #include "drbd_int.h"
38 #include "drbd_protocol.h"
39 #include "drbd_req.h"
40 #include "drbd_vli.h"
41
42 #define PRO_FEATURES (DRBD_FF_TRIM|DRBD_FF_THIN_RESYNC|DRBD_FF_WSAME|DRBD_FF_WZEROES)
43
44 struct packet_info {
45 enum drbd_packet cmd;
46 unsigned int size;
47 unsigned int vnr;
48 void *data;
49 };
50
51 enum finish_epoch {
52 FE_STILL_LIVE,
53 FE_DESTROYED,
54 FE_RECYCLED,
55 };
56
57 static int drbd_do_features(struct drbd_connection *connection);
58 static int drbd_do_auth(struct drbd_connection *connection);
59 static int drbd_disconnected(struct drbd_peer_device *);
60 static void conn_wait_active_ee_empty(struct drbd_connection *connection);
61 static enum finish_epoch drbd_may_finish_epoch(struct drbd_connection *, struct drbd_epoch *, enum epoch_event);
62 static int e_end_block(struct drbd_work *, int);
63
64
65 #define GFP_TRY (__GFP_HIGHMEM | __GFP_NOWARN)
66
__drbd_alloc_pages(unsigned int number)67 static struct page *__drbd_alloc_pages(unsigned int number)
68 {
69 struct page *page = NULL;
70 struct page *tmp = NULL;
71 unsigned int i = 0;
72
73 /* GFP_TRY, because we must not cause arbitrary write-out: in a DRBD
74 * "criss-cross" setup, that might cause write-out on some other DRBD,
75 * which in turn might block on the other node at this very place. */
76 for (i = 0; i < number; i++) {
77 tmp = mempool_alloc(&drbd_buffer_page_pool, GFP_TRY);
78 if (!tmp)
79 goto fail;
80 set_page_private(tmp, (unsigned long)page);
81 page = tmp;
82 }
83 return page;
84 fail:
85 page_chain_for_each_safe(page, tmp) {
86 set_page_private(page, 0);
87 mempool_free(page, &drbd_buffer_page_pool);
88 }
89 return NULL;
90 }
91
92 /**
93 * drbd_alloc_pages() - Returns @number pages, retries forever (or until signalled)
94 * @peer_device: DRBD device.
95 * @number: number of pages requested
96 * @retry: whether to retry, if not enough pages are available right now
97 *
98 * Tries to allocate number pages, first from our own page pool, then from
99 * the kernel.
100 * Possibly retry until DRBD frees sufficient pages somewhere else.
101 *
102 * If this allocation would exceed the max_buffers setting, we throttle
103 * allocation (schedule_timeout) to give the system some room to breathe.
104 *
105 * We do not use max-buffers as hard limit, because it could lead to
106 * congestion and further to a distributed deadlock during online-verify or
107 * (checksum based) resync, if the max-buffers, socket buffer sizes and
108 * resync-rate settings are mis-configured.
109 *
110 * Returns a page chain linked via page->private.
111 */
drbd_alloc_pages(struct drbd_peer_device * peer_device,unsigned int number,bool retry)112 struct page *drbd_alloc_pages(struct drbd_peer_device *peer_device, unsigned int number,
113 bool retry)
114 {
115 struct drbd_device *device = peer_device->device;
116 struct page *page;
117 struct net_conf *nc;
118 unsigned int mxb;
119
120 rcu_read_lock();
121 nc = rcu_dereference(peer_device->connection->net_conf);
122 mxb = nc ? nc->max_buffers : 1000000;
123 rcu_read_unlock();
124
125 if (atomic_read(&device->pp_in_use) >= mxb)
126 schedule_timeout_interruptible(HZ / 10);
127 page = __drbd_alloc_pages(number);
128
129 if (page)
130 atomic_add(number, &device->pp_in_use);
131 return page;
132 }
133
134 /* Must not be used from irq, as that may deadlock: see drbd_alloc_pages.
135 * Is also used from inside an other spin_lock_irq(&resource->req_lock);
136 * Either links the page chain back to the global pool,
137 * or returns all pages to the system. */
drbd_free_pages(struct drbd_device * device,struct page * page)138 static void drbd_free_pages(struct drbd_device *device, struct page *page)
139 {
140 struct page *tmp;
141 int i = 0;
142
143 if (page == NULL)
144 return;
145
146 page_chain_for_each_safe(page, tmp) {
147 set_page_private(page, 0);
148 if (page_count(page) == 1)
149 mempool_free(page, &drbd_buffer_page_pool);
150 else
151 put_page(page);
152 i++;
153 }
154 i = atomic_sub_return(i, &device->pp_in_use);
155 if (i < 0)
156 drbd_warn(device, "ASSERTION FAILED: pp_in_use: %d < 0\n", i);
157 }
158
159 /*
160 You need to hold the req_lock:
161 _drbd_wait_ee_list_empty()
162
163 You must not have the req_lock:
164 drbd_free_peer_req()
165 drbd_alloc_peer_req()
166 drbd_free_peer_reqs()
167 drbd_ee_fix_bhs()
168 drbd_finish_peer_reqs()
169 drbd_clear_done_ee()
170 drbd_wait_ee_list_empty()
171 */
172
173 /* normal: payload_size == request size (bi_size)
174 * w_same: payload_size == logical_block_size
175 * trim: payload_size == 0 */
176 struct drbd_peer_request *
drbd_alloc_peer_req(struct drbd_peer_device * peer_device,u64 id,sector_t sector,unsigned int request_size,unsigned int payload_size,gfp_t gfp_mask)177 drbd_alloc_peer_req(struct drbd_peer_device *peer_device, u64 id, sector_t sector,
178 unsigned int request_size, unsigned int payload_size, gfp_t gfp_mask) __must_hold(local)
179 {
180 struct drbd_device *device = peer_device->device;
181 struct drbd_peer_request *peer_req;
182 struct page *page = NULL;
183 unsigned int nr_pages = PFN_UP(payload_size);
184
185 if (drbd_insert_fault(device, DRBD_FAULT_AL_EE))
186 return NULL;
187
188 peer_req = mempool_alloc(&drbd_ee_mempool, gfp_mask & ~__GFP_HIGHMEM);
189 if (!peer_req) {
190 if (!(gfp_mask & __GFP_NOWARN))
191 drbd_err(device, "%s: allocation failed\n", __func__);
192 return NULL;
193 }
194
195 if (nr_pages) {
196 page = drbd_alloc_pages(peer_device, nr_pages,
197 gfpflags_allow_blocking(gfp_mask));
198 if (!page)
199 goto fail;
200 if (!mempool_is_saturated(&drbd_buffer_page_pool))
201 peer_req->flags |= EE_RELEASE_TO_MEMPOOL;
202 }
203
204 memset(peer_req, 0, sizeof(*peer_req));
205 INIT_LIST_HEAD(&peer_req->w.list);
206 drbd_clear_interval(&peer_req->i);
207 peer_req->i.size = request_size;
208 peer_req->i.sector = sector;
209 peer_req->submit_jif = jiffies;
210 peer_req->peer_device = peer_device;
211 peer_req->pages = page;
212 /*
213 * The block_id is opaque to the receiver. It is not endianness
214 * converted, and sent back to the sender unchanged.
215 */
216 peer_req->block_id = id;
217
218 return peer_req;
219
220 fail:
221 mempool_free(peer_req, &drbd_ee_mempool);
222 return NULL;
223 }
224
drbd_free_peer_req(struct drbd_device * device,struct drbd_peer_request * peer_req)225 void drbd_free_peer_req(struct drbd_device *device, struct drbd_peer_request *peer_req)
226 {
227 might_sleep();
228 if (peer_req->flags & EE_HAS_DIGEST)
229 kfree(peer_req->digest);
230 drbd_free_pages(device, peer_req->pages);
231 D_ASSERT(device, atomic_read(&peer_req->pending_bios) == 0);
232 D_ASSERT(device, drbd_interval_empty(&peer_req->i));
233 if (!expect(device, !(peer_req->flags & EE_CALL_AL_COMPLETE_IO))) {
234 peer_req->flags &= ~EE_CALL_AL_COMPLETE_IO;
235 drbd_al_complete_io(device, &peer_req->i);
236 }
237 mempool_free(peer_req, &drbd_ee_mempool);
238 }
239
drbd_free_peer_reqs(struct drbd_device * device,struct list_head * list)240 int drbd_free_peer_reqs(struct drbd_device *device, struct list_head *list)
241 {
242 LIST_HEAD(work_list);
243 struct drbd_peer_request *peer_req, *t;
244 int count = 0;
245
246 spin_lock_irq(&device->resource->req_lock);
247 list_splice_init(list, &work_list);
248 spin_unlock_irq(&device->resource->req_lock);
249
250 list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
251 drbd_free_peer_req(device, peer_req);
252 count++;
253 }
254 return count;
255 }
256
257 /*
258 * See also comments in _req_mod(,BARRIER_ACKED) and receive_Barrier.
259 */
drbd_finish_peer_reqs(struct drbd_device * device)260 static int drbd_finish_peer_reqs(struct drbd_device *device)
261 {
262 LIST_HEAD(work_list);
263 struct drbd_peer_request *peer_req, *t;
264 int err = 0;
265
266 spin_lock_irq(&device->resource->req_lock);
267 list_splice_init(&device->done_ee, &work_list);
268 spin_unlock_irq(&device->resource->req_lock);
269
270 /* possible callbacks here:
271 * e_end_block, and e_end_resync_block, e_send_superseded.
272 * all ignore the last argument.
273 */
274 list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
275 int err2;
276
277 /* list_del not necessary, next/prev members not touched */
278 err2 = peer_req->w.cb(&peer_req->w, !!err);
279 if (!err)
280 err = err2;
281 drbd_free_peer_req(device, peer_req);
282 }
283 wake_up(&device->ee_wait);
284
285 return err;
286 }
287
_drbd_wait_ee_list_empty(struct drbd_device * device,struct list_head * head)288 static void _drbd_wait_ee_list_empty(struct drbd_device *device,
289 struct list_head *head)
290 {
291 DEFINE_WAIT(wait);
292
293 /* avoids spin_lock/unlock
294 * and calling prepare_to_wait in the fast path */
295 while (!list_empty(head)) {
296 prepare_to_wait(&device->ee_wait, &wait, TASK_UNINTERRUPTIBLE);
297 spin_unlock_irq(&device->resource->req_lock);
298 io_schedule();
299 finish_wait(&device->ee_wait, &wait);
300 spin_lock_irq(&device->resource->req_lock);
301 }
302 }
303
drbd_wait_ee_list_empty(struct drbd_device * device,struct list_head * head)304 static void drbd_wait_ee_list_empty(struct drbd_device *device,
305 struct list_head *head)
306 {
307 spin_lock_irq(&device->resource->req_lock);
308 _drbd_wait_ee_list_empty(device, head);
309 spin_unlock_irq(&device->resource->req_lock);
310 }
311
drbd_recv_short(struct socket * sock,void * buf,size_t size,int flags)312 static int drbd_recv_short(struct socket *sock, void *buf, size_t size, int flags)
313 {
314 struct kvec iov = {
315 .iov_base = buf,
316 .iov_len = size,
317 };
318 struct msghdr msg = {
319 .msg_flags = (flags ? flags : MSG_WAITALL | MSG_NOSIGNAL)
320 };
321 iov_iter_kvec(&msg.msg_iter, ITER_DEST, &iov, 1, size);
322 return sock_recvmsg(sock, &msg, msg.msg_flags);
323 }
324
drbd_recv(struct drbd_connection * connection,void * buf,size_t size)325 static int drbd_recv(struct drbd_connection *connection, void *buf, size_t size)
326 {
327 int rv;
328
329 rv = drbd_recv_short(connection->data.socket, buf, size, 0);
330
331 if (rv < 0) {
332 if (rv == -ECONNRESET)
333 drbd_info(connection, "sock was reset by peer\n");
334 else if (rv != -ERESTARTSYS)
335 drbd_err(connection, "sock_recvmsg returned %d\n", rv);
336 } else if (rv == 0) {
337 if (test_bit(DISCONNECT_SENT, &connection->flags)) {
338 long t;
339 rcu_read_lock();
340 t = rcu_dereference(connection->net_conf)->ping_timeo * HZ/10;
341 rcu_read_unlock();
342
343 t = wait_event_timeout(connection->ping_wait, connection->cstate < C_WF_REPORT_PARAMS, t);
344
345 if (t)
346 goto out;
347 }
348 drbd_info(connection, "sock was shut down by peer\n");
349 }
350
351 if (rv != size)
352 conn_request_state(connection, NS(conn, C_BROKEN_PIPE), CS_HARD);
353
354 out:
355 return rv;
356 }
357
drbd_recv_all(struct drbd_connection * connection,void * buf,size_t size)358 static int drbd_recv_all(struct drbd_connection *connection, void *buf, size_t size)
359 {
360 int err;
361
362 err = drbd_recv(connection, buf, size);
363 if (err != size) {
364 if (err >= 0)
365 err = -EIO;
366 } else
367 err = 0;
368 return err;
369 }
370
drbd_recv_all_warn(struct drbd_connection * connection,void * buf,size_t size)371 static int drbd_recv_all_warn(struct drbd_connection *connection, void *buf, size_t size)
372 {
373 int err;
374
375 err = drbd_recv_all(connection, buf, size);
376 if (err && !signal_pending(current))
377 drbd_warn(connection, "short read (expected size %d)\n", (int)size);
378 return err;
379 }
380
381 /* quoting tcp(7):
382 * On individual connections, the socket buffer size must be set prior to the
383 * listen(2) or connect(2) calls in order to have it take effect.
384 * This is our wrapper to do so.
385 */
drbd_setbufsize(struct socket * sock,unsigned int snd,unsigned int rcv)386 static void drbd_setbufsize(struct socket *sock, unsigned int snd,
387 unsigned int rcv)
388 {
389 /* open coded SO_SNDBUF, SO_RCVBUF */
390 if (snd) {
391 sock->sk->sk_sndbuf = snd;
392 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
393 }
394 if (rcv) {
395 sock->sk->sk_rcvbuf = rcv;
396 sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
397 }
398 }
399
drbd_try_connect(struct drbd_connection * connection)400 static struct socket *drbd_try_connect(struct drbd_connection *connection)
401 {
402 const char *what;
403 struct socket *sock;
404 struct sockaddr_in6 src_in6;
405 struct sockaddr_in6 peer_in6;
406 struct net_conf *nc;
407 int err, peer_addr_len, my_addr_len;
408 int sndbuf_size, rcvbuf_size, connect_int;
409 int disconnect_on_error = 1;
410
411 rcu_read_lock();
412 nc = rcu_dereference(connection->net_conf);
413 if (!nc) {
414 rcu_read_unlock();
415 return NULL;
416 }
417 sndbuf_size = nc->sndbuf_size;
418 rcvbuf_size = nc->rcvbuf_size;
419 connect_int = nc->connect_int;
420 rcu_read_unlock();
421
422 my_addr_len = min_t(int, connection->my_addr_len, sizeof(src_in6));
423 memcpy(&src_in6, &connection->my_addr, my_addr_len);
424
425 if (((struct sockaddr *)&connection->my_addr)->sa_family == AF_INET6)
426 src_in6.sin6_port = 0;
427 else
428 ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */
429
430 peer_addr_len = min_t(int, connection->peer_addr_len, sizeof(src_in6));
431 memcpy(&peer_in6, &connection->peer_addr, peer_addr_len);
432
433 what = "sock_create_kern";
434 err = sock_create_kern(&init_net, ((struct sockaddr *)&src_in6)->sa_family,
435 SOCK_STREAM, IPPROTO_TCP, &sock);
436 if (err < 0) {
437 sock = NULL;
438 goto out;
439 }
440
441 sock->sk->sk_rcvtimeo =
442 sock->sk->sk_sndtimeo = connect_int * HZ;
443 drbd_setbufsize(sock, sndbuf_size, rcvbuf_size);
444
445 /* explicitly bind to the configured IP as source IP
446 * for the outgoing connections.
447 * This is needed for multihomed hosts and to be
448 * able to use lo: interfaces for drbd.
449 * Make sure to use 0 as port number, so linux selects
450 * a free one dynamically.
451 */
452 what = "bind before connect";
453 err = sock->ops->bind(sock, (struct sockaddr_unsized *) &src_in6, my_addr_len);
454 if (err < 0)
455 goto out;
456
457 /* connect may fail, peer not yet available.
458 * stay C_WF_CONNECTION, don't go Disconnecting! */
459 disconnect_on_error = 0;
460 what = "connect";
461 err = sock->ops->connect(sock, (struct sockaddr_unsized *) &peer_in6, peer_addr_len, 0);
462
463 out:
464 if (err < 0) {
465 if (sock) {
466 sock_release(sock);
467 sock = NULL;
468 }
469 switch (-err) {
470 /* timeout, busy, signal pending */
471 case ETIMEDOUT: case EAGAIN: case EINPROGRESS:
472 case EINTR: case ERESTARTSYS:
473 /* peer not (yet) available, network problem */
474 case ECONNREFUSED: case ENETUNREACH:
475 case EHOSTDOWN: case EHOSTUNREACH:
476 disconnect_on_error = 0;
477 break;
478 default:
479 drbd_err(connection, "%s failed, err = %d\n", what, err);
480 }
481 if (disconnect_on_error)
482 conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD);
483 }
484
485 return sock;
486 }
487
488 struct accept_wait_data {
489 struct drbd_connection *connection;
490 struct socket *s_listen;
491 struct completion door_bell;
492 void (*original_sk_state_change)(struct sock *sk);
493
494 };
495
drbd_incoming_connection(struct sock * sk)496 static void drbd_incoming_connection(struct sock *sk)
497 {
498 struct accept_wait_data *ad = sk->sk_user_data;
499 void (*state_change)(struct sock *sk);
500
501 state_change = ad->original_sk_state_change;
502 if (sk->sk_state == TCP_ESTABLISHED)
503 complete(&ad->door_bell);
504 state_change(sk);
505 }
506
prepare_listen_socket(struct drbd_connection * connection,struct accept_wait_data * ad)507 static int prepare_listen_socket(struct drbd_connection *connection, struct accept_wait_data *ad)
508 {
509 int err, sndbuf_size, rcvbuf_size, my_addr_len;
510 struct sockaddr_in6 my_addr;
511 struct socket *s_listen;
512 struct net_conf *nc;
513 const char *what;
514
515 rcu_read_lock();
516 nc = rcu_dereference(connection->net_conf);
517 if (!nc) {
518 rcu_read_unlock();
519 return -EIO;
520 }
521 sndbuf_size = nc->sndbuf_size;
522 rcvbuf_size = nc->rcvbuf_size;
523 rcu_read_unlock();
524
525 my_addr_len = min_t(int, connection->my_addr_len, sizeof(struct sockaddr_in6));
526 memcpy(&my_addr, &connection->my_addr, my_addr_len);
527
528 what = "sock_create_kern";
529 err = sock_create_kern(&init_net, ((struct sockaddr *)&my_addr)->sa_family,
530 SOCK_STREAM, IPPROTO_TCP, &s_listen);
531 if (err) {
532 s_listen = NULL;
533 goto out;
534 }
535
536 s_listen->sk->sk_reuse = SK_CAN_REUSE; /* SO_REUSEADDR */
537 drbd_setbufsize(s_listen, sndbuf_size, rcvbuf_size);
538
539 what = "bind before listen";
540 err = s_listen->ops->bind(s_listen, (struct sockaddr_unsized *)&my_addr, my_addr_len);
541 if (err < 0)
542 goto out;
543
544 ad->s_listen = s_listen;
545 write_lock_bh(&s_listen->sk->sk_callback_lock);
546 ad->original_sk_state_change = s_listen->sk->sk_state_change;
547 s_listen->sk->sk_state_change = drbd_incoming_connection;
548 s_listen->sk->sk_user_data = ad;
549 write_unlock_bh(&s_listen->sk->sk_callback_lock);
550
551 what = "listen";
552 err = s_listen->ops->listen(s_listen, 5);
553 if (err < 0)
554 goto out;
555
556 return 0;
557 out:
558 if (s_listen)
559 sock_release(s_listen);
560 if (err < 0) {
561 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
562 drbd_err(connection, "%s failed, err = %d\n", what, err);
563 conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD);
564 }
565 }
566
567 return -EIO;
568 }
569
unregister_state_change(struct sock * sk,struct accept_wait_data * ad)570 static void unregister_state_change(struct sock *sk, struct accept_wait_data *ad)
571 {
572 write_lock_bh(&sk->sk_callback_lock);
573 sk->sk_state_change = ad->original_sk_state_change;
574 sk->sk_user_data = NULL;
575 write_unlock_bh(&sk->sk_callback_lock);
576 }
577
drbd_wait_for_connect(struct drbd_connection * connection,struct accept_wait_data * ad)578 static struct socket *drbd_wait_for_connect(struct drbd_connection *connection, struct accept_wait_data *ad)
579 {
580 int timeo, connect_int, err = 0;
581 struct socket *s_estab = NULL;
582 struct net_conf *nc;
583
584 rcu_read_lock();
585 nc = rcu_dereference(connection->net_conf);
586 if (!nc) {
587 rcu_read_unlock();
588 return NULL;
589 }
590 connect_int = nc->connect_int;
591 rcu_read_unlock();
592
593 timeo = connect_int * HZ;
594 /* 28.5% random jitter */
595 timeo += get_random_u32_below(2) ? timeo / 7 : -timeo / 7;
596
597 err = wait_for_completion_interruptible_timeout(&ad->door_bell, timeo);
598 if (err <= 0)
599 return NULL;
600
601 err = kernel_accept(ad->s_listen, &s_estab, 0);
602 if (err < 0) {
603 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
604 drbd_err(connection, "accept failed, err = %d\n", err);
605 conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD);
606 }
607 }
608
609 if (s_estab)
610 unregister_state_change(s_estab->sk, ad);
611
612 return s_estab;
613 }
614
615 static int decode_header(struct drbd_connection *, void *, struct packet_info *);
616
send_first_packet(struct drbd_connection * connection,struct drbd_socket * sock,enum drbd_packet cmd)617 static int send_first_packet(struct drbd_connection *connection, struct drbd_socket *sock,
618 enum drbd_packet cmd)
619 {
620 if (!conn_prepare_command(connection, sock))
621 return -EIO;
622 return conn_send_command(connection, sock, cmd, 0, NULL, 0);
623 }
624
receive_first_packet(struct drbd_connection * connection,struct socket * sock)625 static int receive_first_packet(struct drbd_connection *connection, struct socket *sock)
626 {
627 unsigned int header_size = drbd_header_size(connection);
628 struct packet_info pi;
629 struct net_conf *nc;
630 int err;
631
632 rcu_read_lock();
633 nc = rcu_dereference(connection->net_conf);
634 if (!nc) {
635 rcu_read_unlock();
636 return -EIO;
637 }
638 sock->sk->sk_rcvtimeo = nc->ping_timeo * 4 * HZ / 10;
639 rcu_read_unlock();
640
641 err = drbd_recv_short(sock, connection->data.rbuf, header_size, 0);
642 if (err != header_size) {
643 if (err >= 0)
644 err = -EIO;
645 return err;
646 }
647 err = decode_header(connection, connection->data.rbuf, &pi);
648 if (err)
649 return err;
650 return pi.cmd;
651 }
652
653 /**
654 * drbd_socket_okay() - Free the socket if its connection is not okay
655 * @sock: pointer to the pointer to the socket.
656 */
drbd_socket_okay(struct socket ** sock)657 static bool drbd_socket_okay(struct socket **sock)
658 {
659 int rr;
660 char tb[4];
661
662 if (!*sock)
663 return false;
664
665 rr = drbd_recv_short(*sock, tb, 4, MSG_DONTWAIT | MSG_PEEK);
666
667 if (rr > 0 || rr == -EAGAIN) {
668 return true;
669 } else {
670 sock_release(*sock);
671 *sock = NULL;
672 return false;
673 }
674 }
675
connection_established(struct drbd_connection * connection,struct socket ** sock1,struct socket ** sock2)676 static bool connection_established(struct drbd_connection *connection,
677 struct socket **sock1,
678 struct socket **sock2)
679 {
680 struct net_conf *nc;
681 int timeout;
682 bool ok;
683
684 if (!*sock1 || !*sock2)
685 return false;
686
687 rcu_read_lock();
688 nc = rcu_dereference(connection->net_conf);
689 timeout = (nc->sock_check_timeo ?: nc->ping_timeo) * HZ / 10;
690 rcu_read_unlock();
691 schedule_timeout_interruptible(timeout);
692
693 ok = drbd_socket_okay(sock1);
694 ok = drbd_socket_okay(sock2) && ok;
695
696 return ok;
697 }
698
699 /* Gets called if a connection is established, or if a new minor gets created
700 in a connection */
drbd_connected(struct drbd_peer_device * peer_device)701 int drbd_connected(struct drbd_peer_device *peer_device)
702 {
703 struct drbd_device *device = peer_device->device;
704 int err;
705
706 atomic_set(&device->packet_seq, 0);
707 device->peer_seq = 0;
708
709 device->state_mutex = peer_device->connection->agreed_pro_version < 100 ?
710 &peer_device->connection->cstate_mutex :
711 &device->own_state_mutex;
712
713 err = drbd_send_sync_param(peer_device);
714 if (!err)
715 err = drbd_send_sizes(peer_device, 0, 0);
716 if (!err)
717 err = drbd_send_uuids(peer_device);
718 if (!err)
719 err = drbd_send_current_state(peer_device);
720 clear_bit(USE_DEGR_WFC_T, &device->flags);
721 clear_bit(RESIZE_PENDING, &device->flags);
722 atomic_set(&device->ap_in_flight, 0);
723 mod_timer(&device->request_timer, jiffies + HZ); /* just start it here. */
724 return err;
725 }
726
727 /*
728 * return values:
729 * 1 yes, we have a valid connection
730 * 0 oops, did not work out, please try again
731 * -1 peer talks different language,
732 * no point in trying again, please go standalone.
733 * -2 We do not have a network config...
734 */
conn_connect(struct drbd_connection * connection)735 static int conn_connect(struct drbd_connection *connection)
736 {
737 struct drbd_socket sock, msock;
738 struct drbd_peer_device *peer_device;
739 struct net_conf *nc;
740 int vnr, timeout, h;
741 bool discard_my_data, ok;
742 enum drbd_state_rv rv;
743 struct accept_wait_data ad = {
744 .connection = connection,
745 .door_bell = COMPLETION_INITIALIZER_ONSTACK(ad.door_bell),
746 };
747
748 clear_bit(DISCONNECT_SENT, &connection->flags);
749 if (conn_request_state(connection, NS(conn, C_WF_CONNECTION), CS_VERBOSE) < SS_SUCCESS)
750 return -2;
751
752 mutex_init(&sock.mutex);
753 sock.sbuf = connection->data.sbuf;
754 sock.rbuf = connection->data.rbuf;
755 sock.socket = NULL;
756 mutex_init(&msock.mutex);
757 msock.sbuf = connection->meta.sbuf;
758 msock.rbuf = connection->meta.rbuf;
759 msock.socket = NULL;
760
761 /* Assume that the peer only understands protocol 80 until we know better. */
762 connection->agreed_pro_version = 80;
763
764 if (prepare_listen_socket(connection, &ad))
765 return 0;
766
767 do {
768 struct socket *s;
769
770 s = drbd_try_connect(connection);
771 if (s) {
772 if (!sock.socket) {
773 sock.socket = s;
774 send_first_packet(connection, &sock, P_INITIAL_DATA);
775 } else if (!msock.socket) {
776 clear_bit(RESOLVE_CONFLICTS, &connection->flags);
777 msock.socket = s;
778 send_first_packet(connection, &msock, P_INITIAL_META);
779 } else {
780 drbd_err(connection, "Logic error in conn_connect()\n");
781 goto out_release_sockets;
782 }
783 }
784
785 if (connection_established(connection, &sock.socket, &msock.socket))
786 break;
787
788 retry:
789 s = drbd_wait_for_connect(connection, &ad);
790 if (s) {
791 int fp = receive_first_packet(connection, s);
792 drbd_socket_okay(&sock.socket);
793 drbd_socket_okay(&msock.socket);
794 switch (fp) {
795 case P_INITIAL_DATA:
796 if (sock.socket) {
797 drbd_warn(connection, "initial packet S crossed\n");
798 sock_release(sock.socket);
799 sock.socket = s;
800 goto randomize;
801 }
802 sock.socket = s;
803 break;
804 case P_INITIAL_META:
805 set_bit(RESOLVE_CONFLICTS, &connection->flags);
806 if (msock.socket) {
807 drbd_warn(connection, "initial packet M crossed\n");
808 sock_release(msock.socket);
809 msock.socket = s;
810 goto randomize;
811 }
812 msock.socket = s;
813 break;
814 default:
815 drbd_warn(connection, "Error receiving initial packet\n");
816 sock_release(s);
817 randomize:
818 if (get_random_u32_below(2))
819 goto retry;
820 }
821 }
822
823 if (connection->cstate <= C_DISCONNECTING)
824 goto out_release_sockets;
825 if (signal_pending(current)) {
826 flush_signals(current);
827 smp_rmb();
828 if (get_t_state(&connection->receiver) == EXITING)
829 goto out_release_sockets;
830 }
831
832 ok = connection_established(connection, &sock.socket, &msock.socket);
833 } while (!ok);
834
835 if (ad.s_listen)
836 sock_release(ad.s_listen);
837
838 sock.socket->sk->sk_reuse = SK_CAN_REUSE; /* SO_REUSEADDR */
839 msock.socket->sk->sk_reuse = SK_CAN_REUSE; /* SO_REUSEADDR */
840
841 sock.socket->sk->sk_allocation = GFP_NOIO;
842 msock.socket->sk->sk_allocation = GFP_NOIO;
843
844 sock.socket->sk->sk_use_task_frag = false;
845 msock.socket->sk->sk_use_task_frag = false;
846
847 sock.socket->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
848 msock.socket->sk->sk_priority = TC_PRIO_INTERACTIVE;
849
850 /* NOT YET ...
851 * sock.socket->sk->sk_sndtimeo = connection->net_conf->timeout*HZ/10;
852 * sock.socket->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
853 * first set it to the P_CONNECTION_FEATURES timeout,
854 * which we set to 4x the configured ping_timeout. */
855 rcu_read_lock();
856 nc = rcu_dereference(connection->net_conf);
857
858 sock.socket->sk->sk_sndtimeo =
859 sock.socket->sk->sk_rcvtimeo = nc->ping_timeo*4*HZ/10;
860
861 msock.socket->sk->sk_rcvtimeo = nc->ping_int*HZ;
862 timeout = nc->timeout * HZ / 10;
863 discard_my_data = nc->discard_my_data;
864 rcu_read_unlock();
865
866 msock.socket->sk->sk_sndtimeo = timeout;
867
868 /* we don't want delays.
869 * we use TCP_CORK where appropriate, though */
870 tcp_sock_set_nodelay(sock.socket->sk);
871 tcp_sock_set_nodelay(msock.socket->sk);
872
873 connection->data.socket = sock.socket;
874 connection->meta.socket = msock.socket;
875 connection->last_received = jiffies;
876
877 h = drbd_do_features(connection);
878 if (h <= 0)
879 return h;
880
881 if (connection->cram_hmac_tfm) {
882 /* drbd_request_state(device, NS(conn, WFAuth)); */
883 switch (drbd_do_auth(connection)) {
884 case -1:
885 drbd_err(connection, "Authentication of peer failed\n");
886 return -1;
887 case 0:
888 drbd_err(connection, "Authentication of peer failed, trying again.\n");
889 return 0;
890 }
891 }
892
893 connection->data.socket->sk->sk_sndtimeo = timeout;
894 connection->data.socket->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
895
896 if (drbd_send_protocol(connection) == -EOPNOTSUPP)
897 return -1;
898
899 /* Prevent a race between resync-handshake and
900 * being promoted to Primary.
901 *
902 * Grab and release the state mutex, so we know that any current
903 * drbd_set_role() is finished, and any incoming drbd_set_role
904 * will see the STATE_SENT flag, and wait for it to be cleared.
905 */
906 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
907 mutex_lock(peer_device->device->state_mutex);
908
909 /* avoid a race with conn_request_state( C_DISCONNECTING ) */
910 spin_lock_irq(&connection->resource->req_lock);
911 set_bit(STATE_SENT, &connection->flags);
912 spin_unlock_irq(&connection->resource->req_lock);
913
914 idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
915 mutex_unlock(peer_device->device->state_mutex);
916
917 rcu_read_lock();
918 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
919 struct drbd_device *device = peer_device->device;
920 kref_get(&device->kref);
921 rcu_read_unlock();
922
923 if (discard_my_data)
924 set_bit(DISCARD_MY_DATA, &device->flags);
925 else
926 clear_bit(DISCARD_MY_DATA, &device->flags);
927
928 drbd_connected(peer_device);
929 kref_put(&device->kref, drbd_destroy_device);
930 rcu_read_lock();
931 }
932 rcu_read_unlock();
933
934 rv = conn_request_state(connection, NS(conn, C_WF_REPORT_PARAMS), CS_VERBOSE);
935 if (rv < SS_SUCCESS || connection->cstate != C_WF_REPORT_PARAMS) {
936 clear_bit(STATE_SENT, &connection->flags);
937 return 0;
938 }
939
940 drbd_thread_start(&connection->ack_receiver);
941 /* opencoded create_singlethread_workqueue(),
942 * to be able to use format string arguments */
943 connection->ack_sender =
944 alloc_ordered_workqueue("drbd_as_%s", WQ_MEM_RECLAIM, connection->resource->name);
945 if (!connection->ack_sender) {
946 drbd_err(connection, "Failed to create workqueue ack_sender\n");
947 return 0;
948 }
949
950 mutex_lock(&connection->resource->conf_update);
951 /* The discard_my_data flag is a single-shot modifier to the next
952 * connection attempt, the handshake of which is now well underway.
953 * No need for rcu style copying of the whole struct
954 * just to clear a single value. */
955 connection->net_conf->discard_my_data = 0;
956 mutex_unlock(&connection->resource->conf_update);
957
958 return h;
959
960 out_release_sockets:
961 if (ad.s_listen)
962 sock_release(ad.s_listen);
963 if (sock.socket)
964 sock_release(sock.socket);
965 if (msock.socket)
966 sock_release(msock.socket);
967 return -1;
968 }
969
decode_header(struct drbd_connection * connection,void * header,struct packet_info * pi)970 static int decode_header(struct drbd_connection *connection, void *header, struct packet_info *pi)
971 {
972 unsigned int header_size = drbd_header_size(connection);
973
974 if (header_size == sizeof(struct p_header100) &&
975 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC_100)) {
976 struct p_header100 *h = header;
977 if (h->pad != 0) {
978 drbd_err(connection, "Header padding is not zero\n");
979 return -EINVAL;
980 }
981 pi->vnr = be16_to_cpu(h->volume);
982 pi->cmd = be16_to_cpu(h->command);
983 pi->size = be32_to_cpu(h->length);
984 } else if (header_size == sizeof(struct p_header95) &&
985 *(__be16 *)header == cpu_to_be16(DRBD_MAGIC_BIG)) {
986 struct p_header95 *h = header;
987 pi->cmd = be16_to_cpu(h->command);
988 pi->size = be32_to_cpu(h->length);
989 pi->vnr = 0;
990 } else if (header_size == sizeof(struct p_header80) &&
991 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC)) {
992 struct p_header80 *h = header;
993 pi->cmd = be16_to_cpu(h->command);
994 pi->size = be16_to_cpu(h->length);
995 pi->vnr = 0;
996 } else {
997 drbd_err(connection, "Wrong magic value 0x%08x in protocol version %d\n",
998 be32_to_cpu(*(__be32 *)header),
999 connection->agreed_pro_version);
1000 return -EINVAL;
1001 }
1002 pi->data = header + header_size;
1003 return 0;
1004 }
1005
drbd_unplug_all_devices(struct drbd_connection * connection)1006 static void drbd_unplug_all_devices(struct drbd_connection *connection)
1007 {
1008 if (current->plug == &connection->receiver_plug) {
1009 blk_finish_plug(&connection->receiver_plug);
1010 blk_start_plug(&connection->receiver_plug);
1011 } /* else: maybe just schedule() ?? */
1012 }
1013
drbd_recv_header(struct drbd_connection * connection,struct packet_info * pi)1014 static int drbd_recv_header(struct drbd_connection *connection, struct packet_info *pi)
1015 {
1016 void *buffer = connection->data.rbuf;
1017 int err;
1018
1019 err = drbd_recv_all_warn(connection, buffer, drbd_header_size(connection));
1020 if (err)
1021 return err;
1022
1023 err = decode_header(connection, buffer, pi);
1024 connection->last_received = jiffies;
1025
1026 return err;
1027 }
1028
drbd_recv_header_maybe_unplug(struct drbd_connection * connection,struct packet_info * pi)1029 static int drbd_recv_header_maybe_unplug(struct drbd_connection *connection, struct packet_info *pi)
1030 {
1031 void *buffer = connection->data.rbuf;
1032 unsigned int size = drbd_header_size(connection);
1033 int err;
1034
1035 err = drbd_recv_short(connection->data.socket, buffer, size, MSG_NOSIGNAL|MSG_DONTWAIT);
1036 if (err != size) {
1037 /* If we have nothing in the receive buffer now, to reduce
1038 * application latency, try to drain the backend queues as
1039 * quickly as possible, and let remote TCP know what we have
1040 * received so far. */
1041 if (err == -EAGAIN) {
1042 tcp_sock_set_quickack(connection->data.socket->sk, 2);
1043 drbd_unplug_all_devices(connection);
1044 }
1045 if (err > 0) {
1046 buffer += err;
1047 size -= err;
1048 }
1049 err = drbd_recv_all_warn(connection, buffer, size);
1050 if (err)
1051 return err;
1052 }
1053
1054 err = decode_header(connection, connection->data.rbuf, pi);
1055 connection->last_received = jiffies;
1056
1057 return err;
1058 }
1059 /* This is blkdev_issue_flush, but asynchronous.
1060 * We want to submit to all component volumes in parallel,
1061 * then wait for all completions.
1062 */
1063 struct issue_flush_context {
1064 atomic_t pending;
1065 int error;
1066 struct completion done;
1067 };
1068 struct one_flush_context {
1069 struct drbd_device *device;
1070 struct issue_flush_context *ctx;
1071 };
1072
one_flush_endio(struct bio * bio)1073 static void one_flush_endio(struct bio *bio)
1074 {
1075 struct one_flush_context *octx = bio->bi_private;
1076 struct drbd_device *device = octx->device;
1077 struct issue_flush_context *ctx = octx->ctx;
1078
1079 if (bio->bi_status) {
1080 ctx->error = blk_status_to_errno(bio->bi_status);
1081 drbd_info(device, "local disk FLUSH FAILED with status %d\n", bio->bi_status);
1082 }
1083 kfree(octx);
1084 bio_put(bio);
1085
1086 clear_bit(FLUSH_PENDING, &device->flags);
1087 put_ldev(device);
1088 kref_put(&device->kref, drbd_destroy_device);
1089
1090 if (atomic_dec_and_test(&ctx->pending))
1091 complete(&ctx->done);
1092 }
1093
submit_one_flush(struct drbd_device * device,struct issue_flush_context * ctx)1094 static void submit_one_flush(struct drbd_device *device, struct issue_flush_context *ctx)
1095 {
1096 struct bio *bio = bio_alloc(device->ldev->backing_bdev, 0,
1097 REQ_OP_WRITE | REQ_PREFLUSH, GFP_NOIO);
1098 struct one_flush_context *octx = kmalloc_obj(*octx, GFP_NOIO);
1099
1100 if (!octx) {
1101 drbd_warn(device, "Could not allocate a octx, CANNOT ISSUE FLUSH\n");
1102 /* FIXME: what else can I do now? disconnecting or detaching
1103 * really does not help to improve the state of the world, either.
1104 */
1105 bio_put(bio);
1106
1107 ctx->error = -ENOMEM;
1108 put_ldev(device);
1109 kref_put(&device->kref, drbd_destroy_device);
1110 return;
1111 }
1112
1113 octx->device = device;
1114 octx->ctx = ctx;
1115 bio->bi_private = octx;
1116 bio->bi_end_io = one_flush_endio;
1117
1118 device->flush_jif = jiffies;
1119 set_bit(FLUSH_PENDING, &device->flags);
1120 atomic_inc(&ctx->pending);
1121 submit_bio(bio);
1122 }
1123
drbd_flush(struct drbd_connection * connection)1124 static void drbd_flush(struct drbd_connection *connection)
1125 {
1126 if (connection->resource->write_ordering >= WO_BDEV_FLUSH) {
1127 struct drbd_peer_device *peer_device;
1128 struct issue_flush_context ctx;
1129 int vnr;
1130
1131 atomic_set(&ctx.pending, 1);
1132 ctx.error = 0;
1133 init_completion(&ctx.done);
1134
1135 rcu_read_lock();
1136 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1137 struct drbd_device *device = peer_device->device;
1138
1139 if (!get_ldev(device))
1140 continue;
1141 kref_get(&device->kref);
1142 rcu_read_unlock();
1143
1144 submit_one_flush(device, &ctx);
1145
1146 rcu_read_lock();
1147 }
1148 rcu_read_unlock();
1149
1150 /* Do we want to add a timeout,
1151 * if disk-timeout is set? */
1152 if (!atomic_dec_and_test(&ctx.pending))
1153 wait_for_completion(&ctx.done);
1154
1155 if (ctx.error) {
1156 /* would rather check on EOPNOTSUPP, but that is not reliable.
1157 * don't try again for ANY return value != 0
1158 * if (rv == -EOPNOTSUPP) */
1159 /* Any error is already reported by bio_endio callback. */
1160 drbd_bump_write_ordering(connection->resource, NULL, WO_DRAIN_IO);
1161 }
1162 }
1163 }
1164
1165 /**
1166 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
1167 * @connection: DRBD connection.
1168 * @epoch: Epoch object.
1169 * @ev: Epoch event.
1170 */
drbd_may_finish_epoch(struct drbd_connection * connection,struct drbd_epoch * epoch,enum epoch_event ev)1171 static enum finish_epoch drbd_may_finish_epoch(struct drbd_connection *connection,
1172 struct drbd_epoch *epoch,
1173 enum epoch_event ev)
1174 {
1175 int epoch_size;
1176 struct drbd_epoch *next_epoch;
1177 enum finish_epoch rv = FE_STILL_LIVE;
1178
1179 spin_lock(&connection->epoch_lock);
1180 do {
1181 next_epoch = NULL;
1182
1183 epoch_size = atomic_read(&epoch->epoch_size);
1184
1185 switch (ev & ~EV_CLEANUP) {
1186 case EV_PUT:
1187 atomic_dec(&epoch->active);
1188 break;
1189 case EV_GOT_BARRIER_NR:
1190 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
1191 break;
1192 case EV_BECAME_LAST:
1193 /* nothing to do*/
1194 break;
1195 }
1196
1197 if (epoch_size != 0 &&
1198 atomic_read(&epoch->active) == 0 &&
1199 (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags) || ev & EV_CLEANUP)) {
1200 if (!(ev & EV_CLEANUP)) {
1201 spin_unlock(&connection->epoch_lock);
1202 drbd_send_b_ack(epoch->connection, epoch->barrier_nr, epoch_size);
1203 spin_lock(&connection->epoch_lock);
1204 }
1205 #if 0
1206 /* FIXME: dec unacked on connection, once we have
1207 * something to count pending connection packets in. */
1208 if (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags))
1209 dec_unacked(epoch->connection);
1210 #endif
1211
1212 if (connection->current_epoch != epoch) {
1213 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1214 list_del(&epoch->list);
1215 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
1216 connection->epochs--;
1217 kfree(epoch);
1218
1219 if (rv == FE_STILL_LIVE)
1220 rv = FE_DESTROYED;
1221 } else {
1222 epoch->flags = 0;
1223 atomic_set(&epoch->epoch_size, 0);
1224 /* atomic_set(&epoch->active, 0); is already zero */
1225 if (rv == FE_STILL_LIVE)
1226 rv = FE_RECYCLED;
1227 }
1228 }
1229
1230 if (!next_epoch)
1231 break;
1232
1233 epoch = next_epoch;
1234 } while (1);
1235
1236 spin_unlock(&connection->epoch_lock);
1237
1238 return rv;
1239 }
1240
1241 static enum write_ordering_e
max_allowed_wo(struct drbd_backing_dev * bdev,enum write_ordering_e wo)1242 max_allowed_wo(struct drbd_backing_dev *bdev, enum write_ordering_e wo)
1243 {
1244 struct disk_conf *dc;
1245
1246 dc = rcu_dereference(bdev->disk_conf);
1247
1248 if (wo == WO_BDEV_FLUSH && !dc->disk_flushes)
1249 wo = WO_DRAIN_IO;
1250 if (wo == WO_DRAIN_IO && !dc->disk_drain)
1251 wo = WO_NONE;
1252
1253 return wo;
1254 }
1255
1256 /*
1257 * drbd_bump_write_ordering() - Fall back to an other write ordering method
1258 * @wo: Write ordering method to try.
1259 */
drbd_bump_write_ordering(struct drbd_resource * resource,struct drbd_backing_dev * bdev,enum write_ordering_e wo)1260 void drbd_bump_write_ordering(struct drbd_resource *resource, struct drbd_backing_dev *bdev,
1261 enum write_ordering_e wo)
1262 {
1263 struct drbd_device *device;
1264 enum write_ordering_e pwo;
1265 int vnr;
1266 static char *write_ordering_str[] = {
1267 [WO_NONE] = "none",
1268 [WO_DRAIN_IO] = "drain",
1269 [WO_BDEV_FLUSH] = "flush",
1270 };
1271
1272 pwo = resource->write_ordering;
1273 if (wo != WO_BDEV_FLUSH)
1274 wo = min(pwo, wo);
1275 rcu_read_lock();
1276 idr_for_each_entry(&resource->devices, device, vnr) {
1277 if (get_ldev(device)) {
1278 wo = max_allowed_wo(device->ldev, wo);
1279 if (device->ldev == bdev)
1280 bdev = NULL;
1281 put_ldev(device);
1282 }
1283 }
1284
1285 if (bdev)
1286 wo = max_allowed_wo(bdev, wo);
1287
1288 rcu_read_unlock();
1289
1290 resource->write_ordering = wo;
1291 if (pwo != resource->write_ordering || wo == WO_BDEV_FLUSH)
1292 drbd_info(resource, "Method to ensure write ordering: %s\n", write_ordering_str[resource->write_ordering]);
1293 }
1294
1295 /*
1296 * Mapping "discard" to ZEROOUT with UNMAP does not work for us:
1297 * Drivers have to "announce" q->limits.max_write_zeroes_sectors, or it
1298 * will directly go to fallback mode, submitting normal writes, and
1299 * never even try to UNMAP.
1300 *
1301 * And dm-thin does not do this (yet), mostly because in general it has
1302 * to assume that "skip_block_zeroing" is set. See also:
1303 * https://www.mail-archive.com/dm-devel%40redhat.com/msg07965.html
1304 * https://www.redhat.com/archives/dm-devel/2018-January/msg00271.html
1305 *
1306 * We *may* ignore the discard-zeroes-data setting, if so configured.
1307 *
1308 * Assumption is that this "discard_zeroes_data=0" is only because the backend
1309 * may ignore partial unaligned discards.
1310 *
1311 * LVM/DM thin as of at least
1312 * LVM version: 2.02.115(2)-RHEL7 (2015-01-28)
1313 * Library version: 1.02.93-RHEL7 (2015-01-28)
1314 * Driver version: 4.29.0
1315 * still behaves this way.
1316 *
1317 * For unaligned (wrt. alignment and granularity) or too small discards,
1318 * we zero-out the initial (and/or) trailing unaligned partial chunks,
1319 * but discard all the aligned full chunks.
1320 *
1321 * At least for LVM/DM thin, with skip_block_zeroing=false,
1322 * the result is effectively "discard_zeroes_data=1".
1323 */
1324 /* flags: EE_TRIM|EE_ZEROOUT */
drbd_issue_discard_or_zero_out(struct drbd_device * device,sector_t start,unsigned int nr_sectors,int flags)1325 int drbd_issue_discard_or_zero_out(struct drbd_device *device, sector_t start, unsigned int nr_sectors, int flags)
1326 {
1327 struct block_device *bdev = device->ldev->backing_bdev;
1328 sector_t tmp, nr;
1329 unsigned int max_discard_sectors, granularity;
1330 int alignment;
1331 int err = 0;
1332
1333 if ((flags & EE_ZEROOUT) || !(flags & EE_TRIM))
1334 goto zero_out;
1335
1336 /* Zero-sector (unknown) and one-sector granularities are the same. */
1337 granularity = max(bdev_discard_granularity(bdev) >> 9, 1U);
1338 alignment = (bdev_discard_alignment(bdev) >> 9) % granularity;
1339
1340 max_discard_sectors = min(bdev_max_discard_sectors(bdev), (1U << 22));
1341 max_discard_sectors -= max_discard_sectors % granularity;
1342 if (unlikely(!max_discard_sectors))
1343 goto zero_out;
1344
1345 if (nr_sectors < granularity)
1346 goto zero_out;
1347
1348 tmp = start;
1349 if (sector_div(tmp, granularity) != alignment) {
1350 if (nr_sectors < 2*granularity)
1351 goto zero_out;
1352 /* start + gran - (start + gran - align) % gran */
1353 tmp = start + granularity - alignment;
1354 tmp = start + granularity - sector_div(tmp, granularity);
1355
1356 nr = tmp - start;
1357 /* don't flag BLKDEV_ZERO_NOUNMAP, we don't know how many
1358 * layers are below us, some may have smaller granularity */
1359 err |= blkdev_issue_zeroout(bdev, start, nr, GFP_NOIO, 0);
1360 nr_sectors -= nr;
1361 start = tmp;
1362 }
1363 while (nr_sectors >= max_discard_sectors) {
1364 err |= blkdev_issue_discard(bdev, start, max_discard_sectors,
1365 GFP_NOIO);
1366 nr_sectors -= max_discard_sectors;
1367 start += max_discard_sectors;
1368 }
1369 if (nr_sectors) {
1370 /* max_discard_sectors is unsigned int (and a multiple of
1371 * granularity, we made sure of that above already);
1372 * nr is < max_discard_sectors;
1373 * I don't need sector_div here, even though nr is sector_t */
1374 nr = nr_sectors;
1375 nr -= (unsigned int)nr % granularity;
1376 if (nr) {
1377 err |= blkdev_issue_discard(bdev, start, nr, GFP_NOIO);
1378 nr_sectors -= nr;
1379 start += nr;
1380 }
1381 }
1382 zero_out:
1383 if (nr_sectors) {
1384 err |= blkdev_issue_zeroout(bdev, start, nr_sectors, GFP_NOIO,
1385 (flags & EE_TRIM) ? 0 : BLKDEV_ZERO_NOUNMAP);
1386 }
1387 return err != 0;
1388 }
1389
can_do_reliable_discards(struct drbd_device * device)1390 static bool can_do_reliable_discards(struct drbd_device *device)
1391 {
1392 struct disk_conf *dc;
1393 bool can_do;
1394
1395 if (!bdev_max_discard_sectors(device->ldev->backing_bdev))
1396 return false;
1397
1398 rcu_read_lock();
1399 dc = rcu_dereference(device->ldev->disk_conf);
1400 can_do = dc->discard_zeroes_if_aligned;
1401 rcu_read_unlock();
1402 return can_do;
1403 }
1404
drbd_issue_peer_discard_or_zero_out(struct drbd_device * device,struct drbd_peer_request * peer_req)1405 static void drbd_issue_peer_discard_or_zero_out(struct drbd_device *device, struct drbd_peer_request *peer_req)
1406 {
1407 /* If the backend cannot discard, or does not guarantee
1408 * read-back zeroes in discarded ranges, we fall back to
1409 * zero-out. Unless configuration specifically requested
1410 * otherwise. */
1411 if (!can_do_reliable_discards(device))
1412 peer_req->flags |= EE_ZEROOUT;
1413
1414 if (drbd_issue_discard_or_zero_out(device, peer_req->i.sector,
1415 peer_req->i.size >> 9, peer_req->flags & (EE_ZEROOUT|EE_TRIM)))
1416 peer_req->flags |= EE_WAS_ERROR;
1417 drbd_endio_write_sec_final(peer_req);
1418 }
1419
peer_request_fault_type(struct drbd_peer_request * peer_req)1420 static int peer_request_fault_type(struct drbd_peer_request *peer_req)
1421 {
1422 if (peer_req_op(peer_req) == REQ_OP_READ) {
1423 return peer_req->flags & EE_APPLICATION ?
1424 DRBD_FAULT_DT_RD : DRBD_FAULT_RS_RD;
1425 } else {
1426 return peer_req->flags & EE_APPLICATION ?
1427 DRBD_FAULT_DT_WR : DRBD_FAULT_RS_WR;
1428 }
1429 }
1430
1431 /**
1432 * drbd_submit_peer_request()
1433 * @peer_req: peer request
1434 *
1435 * May spread the pages to multiple bios,
1436 * depending on bio_add_page restrictions.
1437 *
1438 * Returns 0 if all bios have been submitted,
1439 * -ENOMEM if we could not allocate enough bios,
1440 * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a
1441 * single page to an empty bio (which should never happen and likely indicates
1442 * that the lower level IO stack is in some way broken). This has been observed
1443 * on certain Xen deployments.
1444 */
1445 /* TODO allocate from our own bio_set. */
drbd_submit_peer_request(struct drbd_peer_request * peer_req)1446 int drbd_submit_peer_request(struct drbd_peer_request *peer_req)
1447 {
1448 struct drbd_device *device = peer_req->peer_device->device;
1449 struct bio *bios = NULL;
1450 struct bio *bio;
1451 struct page *page = peer_req->pages;
1452 sector_t sector = peer_req->i.sector;
1453 unsigned int data_size = peer_req->i.size;
1454 unsigned int n_bios = 0;
1455 unsigned int nr_pages = PFN_UP(data_size);
1456
1457 /* TRIM/DISCARD: for now, always use the helper function
1458 * blkdev_issue_zeroout(..., discard=true).
1459 * It's synchronous, but it does the right thing wrt. bio splitting.
1460 * Correctness first, performance later. Next step is to code an
1461 * asynchronous variant of the same.
1462 */
1463 if (peer_req->flags & (EE_TRIM | EE_ZEROOUT)) {
1464 /* wait for all pending IO completions, before we start
1465 * zeroing things out. */
1466 conn_wait_active_ee_empty(peer_req->peer_device->connection);
1467 /* add it to the active list now,
1468 * so we can find it to present it in debugfs */
1469 peer_req->submit_jif = jiffies;
1470 peer_req->flags |= EE_SUBMITTED;
1471
1472 /* If this was a resync request from receive_rs_deallocated(),
1473 * it is already on the sync_ee list */
1474 if (list_empty(&peer_req->w.list)) {
1475 spin_lock_irq(&device->resource->req_lock);
1476 list_add_tail(&peer_req->w.list, &device->active_ee);
1477 spin_unlock_irq(&device->resource->req_lock);
1478 }
1479
1480 drbd_issue_peer_discard_or_zero_out(device, peer_req);
1481 return 0;
1482 }
1483
1484 /* In most cases, we will only need one bio. But in case the lower
1485 * level restrictions happen to be different at this offset on this
1486 * side than those of the sending peer, we may need to submit the
1487 * request in more than one bio.
1488 *
1489 * Plain bio_alloc is good enough here, this is no DRBD internally
1490 * generated bio, but a bio allocated on behalf of the peer.
1491 */
1492 next_bio:
1493 /* _DISCARD, _WRITE_ZEROES handled above.
1494 * REQ_OP_FLUSH (empty flush) not expected,
1495 * should have been mapped to a "drbd protocol barrier".
1496 * REQ_OP_SECURE_ERASE: I don't see how we could ever support that.
1497 */
1498 if (!(peer_req_op(peer_req) == REQ_OP_WRITE ||
1499 peer_req_op(peer_req) == REQ_OP_READ)) {
1500 drbd_err(device, "Invalid bio op received: 0x%x\n", peer_req->opf);
1501 return -EINVAL;
1502 }
1503
1504 bio = bio_alloc(device->ldev->backing_bdev, nr_pages, peer_req->opf, GFP_NOIO);
1505 /* > peer_req->i.sector, unless this is the first bio */
1506 bio->bi_iter.bi_sector = sector;
1507 bio->bi_private = peer_req;
1508 bio->bi_end_io = drbd_peer_request_endio;
1509
1510 bio->bi_next = bios;
1511 bios = bio;
1512 ++n_bios;
1513
1514 page_chain_for_each(page) {
1515 unsigned len = min_t(unsigned, data_size, PAGE_SIZE);
1516 if (!bio_add_page(bio, page, len, 0))
1517 goto next_bio;
1518 data_size -= len;
1519 sector += len >> 9;
1520 --nr_pages;
1521 }
1522 D_ASSERT(device, data_size == 0);
1523 D_ASSERT(device, page == NULL);
1524
1525 atomic_set(&peer_req->pending_bios, n_bios);
1526 /* for debugfs: update timestamp, mark as submitted */
1527 peer_req->submit_jif = jiffies;
1528 peer_req->flags |= EE_SUBMITTED;
1529 do {
1530 bio = bios;
1531 bios = bios->bi_next;
1532 bio->bi_next = NULL;
1533
1534 drbd_submit_bio_noacct(device, peer_request_fault_type(peer_req), bio);
1535 } while (bios);
1536 return 0;
1537 }
1538
drbd_remove_epoch_entry_interval(struct drbd_device * device,struct drbd_peer_request * peer_req)1539 static void drbd_remove_epoch_entry_interval(struct drbd_device *device,
1540 struct drbd_peer_request *peer_req)
1541 {
1542 struct drbd_interval *i = &peer_req->i;
1543
1544 drbd_remove_interval(&device->write_requests, i);
1545 drbd_clear_interval(i);
1546
1547 /* Wake up any processes waiting for this peer request to complete. */
1548 if (i->waiting)
1549 wake_up(&device->misc_wait);
1550 }
1551
conn_wait_active_ee_empty(struct drbd_connection * connection)1552 static void conn_wait_active_ee_empty(struct drbd_connection *connection)
1553 {
1554 struct drbd_peer_device *peer_device;
1555 int vnr;
1556
1557 rcu_read_lock();
1558 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1559 struct drbd_device *device = peer_device->device;
1560
1561 kref_get(&device->kref);
1562 rcu_read_unlock();
1563 drbd_wait_ee_list_empty(device, &device->active_ee);
1564 kref_put(&device->kref, drbd_destroy_device);
1565 rcu_read_lock();
1566 }
1567 rcu_read_unlock();
1568 }
1569
receive_Barrier(struct drbd_connection * connection,struct packet_info * pi)1570 static int receive_Barrier(struct drbd_connection *connection, struct packet_info *pi)
1571 {
1572 int rv;
1573 struct p_barrier *p = pi->data;
1574 struct drbd_epoch *epoch;
1575
1576 /* FIXME these are unacked on connection,
1577 * not a specific (peer)device.
1578 */
1579 connection->current_epoch->barrier_nr = p->barrier;
1580 connection->current_epoch->connection = connection;
1581 rv = drbd_may_finish_epoch(connection, connection->current_epoch, EV_GOT_BARRIER_NR);
1582
1583 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
1584 * the activity log, which means it would not be resynced in case the
1585 * R_PRIMARY crashes now.
1586 * Therefore we must send the barrier_ack after the barrier request was
1587 * completed. */
1588 switch (connection->resource->write_ordering) {
1589 case WO_NONE:
1590 if (rv == FE_RECYCLED)
1591 return 0;
1592
1593 /* receiver context, in the writeout path of the other node.
1594 * avoid potential distributed deadlock */
1595 epoch = kmalloc_obj(struct drbd_epoch, GFP_NOIO);
1596 if (epoch)
1597 break;
1598 else
1599 drbd_warn(connection, "Allocation of an epoch failed, slowing down\n");
1600 fallthrough;
1601
1602 case WO_BDEV_FLUSH:
1603 case WO_DRAIN_IO:
1604 conn_wait_active_ee_empty(connection);
1605 drbd_flush(connection);
1606
1607 if (atomic_read(&connection->current_epoch->epoch_size)) {
1608 epoch = kmalloc_obj(struct drbd_epoch, GFP_NOIO);
1609 if (epoch)
1610 break;
1611 }
1612
1613 return 0;
1614 default:
1615 drbd_err(connection, "Strangeness in connection->write_ordering %d\n",
1616 connection->resource->write_ordering);
1617 return -EIO;
1618 }
1619
1620 epoch->flags = 0;
1621 atomic_set(&epoch->epoch_size, 0);
1622 atomic_set(&epoch->active, 0);
1623
1624 spin_lock(&connection->epoch_lock);
1625 if (atomic_read(&connection->current_epoch->epoch_size)) {
1626 list_add(&epoch->list, &connection->current_epoch->list);
1627 connection->current_epoch = epoch;
1628 connection->epochs++;
1629 } else {
1630 /* The current_epoch got recycled while we allocated this one... */
1631 kfree(epoch);
1632 }
1633 spin_unlock(&connection->epoch_lock);
1634
1635 return 0;
1636 }
1637
1638 /* quick wrapper in case payload size != request_size (write same) */
drbd_csum_ee_size(struct crypto_shash * h,struct drbd_peer_request * r,void * d,unsigned int payload_size)1639 static void drbd_csum_ee_size(struct crypto_shash *h,
1640 struct drbd_peer_request *r, void *d,
1641 unsigned int payload_size)
1642 {
1643 unsigned int tmp = r->i.size;
1644 r->i.size = payload_size;
1645 drbd_csum_ee(h, r, d);
1646 r->i.size = tmp;
1647 }
1648
1649 /* used from receive_RSDataReply (recv_resync_read)
1650 * and from receive_Data.
1651 * data_size: actual payload ("data in")
1652 * for normal writes that is bi_size.
1653 * for discards, that is zero.
1654 * for write same, it is logical_block_size.
1655 * both trim and write same have the bi_size ("data len to be affected")
1656 * as extra argument in the packet header.
1657 */
1658 static struct drbd_peer_request *
read_in_block(struct drbd_peer_device * peer_device,u64 id,sector_t sector,struct packet_info * pi)1659 read_in_block(struct drbd_peer_device *peer_device, u64 id, sector_t sector,
1660 struct packet_info *pi) __must_hold(local)
1661 {
1662 struct drbd_device *device = peer_device->device;
1663 const sector_t capacity = get_capacity(device->vdisk);
1664 struct drbd_peer_request *peer_req;
1665 struct page *page;
1666 int digest_size, err;
1667 unsigned int data_size = pi->size, ds;
1668 void *dig_in = peer_device->connection->int_dig_in;
1669 void *dig_vv = peer_device->connection->int_dig_vv;
1670 unsigned long *data;
1671 struct p_trim *trim = (pi->cmd == P_TRIM) ? pi->data : NULL;
1672 struct p_trim *zeroes = (pi->cmd == P_ZEROES) ? pi->data : NULL;
1673
1674 digest_size = 0;
1675 if (!trim && peer_device->connection->peer_integrity_tfm) {
1676 digest_size = crypto_shash_digestsize(peer_device->connection->peer_integrity_tfm);
1677 /*
1678 * FIXME: Receive the incoming digest into the receive buffer
1679 * here, together with its struct p_data?
1680 */
1681 err = drbd_recv_all_warn(peer_device->connection, dig_in, digest_size);
1682 if (err)
1683 return NULL;
1684 data_size -= digest_size;
1685 }
1686
1687 /* assume request_size == data_size, but special case trim. */
1688 ds = data_size;
1689 if (trim) {
1690 if (!expect(peer_device, data_size == 0))
1691 return NULL;
1692 ds = be32_to_cpu(trim->size);
1693 } else if (zeroes) {
1694 if (!expect(peer_device, data_size == 0))
1695 return NULL;
1696 ds = be32_to_cpu(zeroes->size);
1697 }
1698
1699 if (!expect(peer_device, IS_ALIGNED(ds, 512)))
1700 return NULL;
1701 if (trim || zeroes) {
1702 if (!expect(peer_device, ds <= (DRBD_MAX_BBIO_SECTORS << 9)))
1703 return NULL;
1704 } else if (!expect(peer_device, ds <= DRBD_MAX_BIO_SIZE))
1705 return NULL;
1706
1707 /* even though we trust out peer,
1708 * we sometimes have to double check. */
1709 if (sector + (ds>>9) > capacity) {
1710 drbd_err(device, "request from peer beyond end of local disk: "
1711 "capacity: %llus < sector: %llus + size: %u\n",
1712 (unsigned long long)capacity,
1713 (unsigned long long)sector, ds);
1714 return NULL;
1715 }
1716
1717 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1718 * "criss-cross" setup, that might cause write-out on some other DRBD,
1719 * which in turn might block on the other node at this very place. */
1720 peer_req = drbd_alloc_peer_req(peer_device, id, sector, ds, data_size, GFP_NOIO);
1721 if (!peer_req)
1722 return NULL;
1723
1724 peer_req->flags |= EE_WRITE;
1725 if (trim) {
1726 peer_req->flags |= EE_TRIM;
1727 return peer_req;
1728 }
1729 if (zeroes) {
1730 peer_req->flags |= EE_ZEROOUT;
1731 return peer_req;
1732 }
1733
1734 /* receive payload size bytes into page chain */
1735 ds = data_size;
1736 page = peer_req->pages;
1737 page_chain_for_each(page) {
1738 unsigned len = min_t(int, ds, PAGE_SIZE);
1739 data = kmap_local_page(page);
1740 err = drbd_recv_all_warn(peer_device->connection, data, len);
1741 if (drbd_insert_fault(device, DRBD_FAULT_RECEIVE)) {
1742 drbd_err(device, "Fault injection: Corrupting data on receive\n");
1743 data[0] = data[0] ^ (unsigned long)-1;
1744 }
1745 kunmap_local(data);
1746 if (err) {
1747 drbd_free_peer_req(device, peer_req);
1748 return NULL;
1749 }
1750 ds -= len;
1751 }
1752
1753 if (digest_size) {
1754 drbd_csum_ee_size(peer_device->connection->peer_integrity_tfm, peer_req, dig_vv, data_size);
1755 if (memcmp(dig_in, dig_vv, digest_size)) {
1756 drbd_err(device, "Digest integrity check FAILED: %llus +%u\n",
1757 (unsigned long long)sector, data_size);
1758 drbd_free_peer_req(device, peer_req);
1759 return NULL;
1760 }
1761 }
1762 device->recv_cnt += data_size >> 9;
1763 return peer_req;
1764 }
1765
1766 /* drbd_drain_block() just takes a data block
1767 * out of the socket input buffer, and discards it.
1768 */
drbd_drain_block(struct drbd_peer_device * peer_device,int data_size)1769 static int drbd_drain_block(struct drbd_peer_device *peer_device, int data_size)
1770 {
1771 struct page *page;
1772 int err = 0;
1773 void *data;
1774
1775 if (!data_size)
1776 return 0;
1777
1778 page = drbd_alloc_pages(peer_device, 1, 1);
1779
1780 data = kmap_local_page(page);
1781 while (data_size) {
1782 unsigned int len = min_t(int, data_size, PAGE_SIZE);
1783
1784 err = drbd_recv_all_warn(peer_device->connection, data, len);
1785 if (err)
1786 break;
1787 data_size -= len;
1788 }
1789 kunmap_local(data);
1790 drbd_free_pages(peer_device->device, page);
1791 return err;
1792 }
1793
recv_dless_read(struct drbd_peer_device * peer_device,struct drbd_request * req,sector_t sector,int data_size)1794 static int recv_dless_read(struct drbd_peer_device *peer_device, struct drbd_request *req,
1795 sector_t sector, int data_size)
1796 {
1797 struct bio_vec bvec;
1798 struct bvec_iter iter;
1799 struct bio *bio;
1800 int digest_size, err, expect;
1801 void *dig_in = peer_device->connection->int_dig_in;
1802 void *dig_vv = peer_device->connection->int_dig_vv;
1803
1804 digest_size = 0;
1805 if (peer_device->connection->peer_integrity_tfm) {
1806 digest_size = crypto_shash_digestsize(peer_device->connection->peer_integrity_tfm);
1807 err = drbd_recv_all_warn(peer_device->connection, dig_in, digest_size);
1808 if (err)
1809 return err;
1810 data_size -= digest_size;
1811 }
1812
1813 if (data_size < 0) {
1814 drbd_err(peer_device, "Invalid data reply size\n");
1815 return -EIO;
1816 }
1817
1818 /* optimistically update recv_cnt. if receiving fails below,
1819 * we disconnect anyways, and counters will be reset. */
1820 peer_device->device->recv_cnt += data_size>>9;
1821
1822 bio = req->master_bio;
1823 D_ASSERT(peer_device->device, sector == bio->bi_iter.bi_sector);
1824
1825 bio_for_each_segment(bvec, bio, iter) {
1826 void *mapped = bvec_kmap_local(&bvec);
1827 expect = min_t(int, data_size, bvec.bv_len);
1828 err = drbd_recv_all_warn(peer_device->connection, mapped, expect);
1829 kunmap_local(mapped);
1830 if (err)
1831 return err;
1832 data_size -= expect;
1833 }
1834
1835 if (digest_size) {
1836 drbd_csum_bio(peer_device->connection->peer_integrity_tfm, bio, dig_vv);
1837 if (memcmp(dig_in, dig_vv, digest_size)) {
1838 drbd_err(peer_device, "Digest integrity check FAILED. Broken NICs?\n");
1839 return -EINVAL;
1840 }
1841 }
1842
1843 D_ASSERT(peer_device->device, data_size == 0);
1844 return 0;
1845 }
1846
1847 /*
1848 * e_end_resync_block() is called in ack_sender context via
1849 * drbd_finish_peer_reqs().
1850 */
e_end_resync_block(struct drbd_work * w,int unused)1851 static int e_end_resync_block(struct drbd_work *w, int unused)
1852 {
1853 struct drbd_peer_request *peer_req =
1854 container_of(w, struct drbd_peer_request, w);
1855 struct drbd_peer_device *peer_device = peer_req->peer_device;
1856 struct drbd_device *device = peer_device->device;
1857 sector_t sector = peer_req->i.sector;
1858 int err;
1859
1860 D_ASSERT(device, drbd_interval_empty(&peer_req->i));
1861
1862 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1863 drbd_set_in_sync(peer_device, sector, peer_req->i.size);
1864 err = drbd_send_ack(peer_device, P_RS_WRITE_ACK, peer_req);
1865 } else {
1866 /* Record failure to sync */
1867 drbd_rs_failed_io(peer_device, sector, peer_req->i.size);
1868
1869 err = drbd_send_ack(peer_device, P_NEG_ACK, peer_req);
1870 }
1871 dec_unacked(device);
1872
1873 return err;
1874 }
1875
recv_resync_read(struct drbd_peer_device * peer_device,sector_t sector,struct packet_info * pi)1876 static int recv_resync_read(struct drbd_peer_device *peer_device, sector_t sector,
1877 struct packet_info *pi) __releases(local)
1878 {
1879 struct drbd_device *device = peer_device->device;
1880 struct drbd_peer_request *peer_req;
1881
1882 peer_req = read_in_block(peer_device, ID_SYNCER, sector, pi);
1883 if (!peer_req)
1884 goto fail;
1885
1886 dec_rs_pending(peer_device);
1887
1888 inc_unacked(device);
1889 /* corresponding dec_unacked() in e_end_resync_block()
1890 * respective _drbd_clear_done_ee */
1891
1892 peer_req->w.cb = e_end_resync_block;
1893 peer_req->opf = REQ_OP_WRITE;
1894 peer_req->submit_jif = jiffies;
1895
1896 spin_lock_irq(&device->resource->req_lock);
1897 list_add_tail(&peer_req->w.list, &device->sync_ee);
1898 spin_unlock_irq(&device->resource->req_lock);
1899
1900 atomic_add(pi->size >> 9, &device->rs_sect_ev);
1901 if (drbd_submit_peer_request(peer_req) == 0)
1902 return 0;
1903
1904 /* don't care for the reason here */
1905 drbd_err(device, "submit failed, triggering re-connect\n");
1906 spin_lock_irq(&device->resource->req_lock);
1907 list_del(&peer_req->w.list);
1908 spin_unlock_irq(&device->resource->req_lock);
1909
1910 drbd_free_peer_req(device, peer_req);
1911 fail:
1912 put_ldev(device);
1913 return -EIO;
1914 }
1915
1916 static struct drbd_request *
find_request(struct drbd_device * device,struct rb_root * root,u64 id,sector_t sector,bool missing_ok,const char * func)1917 find_request(struct drbd_device *device, struct rb_root *root, u64 id,
1918 sector_t sector, bool missing_ok, const char *func)
1919 {
1920 struct drbd_request *req;
1921
1922 /* Request object according to our peer */
1923 req = (struct drbd_request *)(unsigned long)id;
1924 if (drbd_contains_interval(root, sector, &req->i) && req->i.local)
1925 return req;
1926 if (!missing_ok) {
1927 drbd_err(device, "%s: failed to find request 0x%lx, sector %llus\n", func,
1928 (unsigned long)id, (unsigned long long)sector);
1929 }
1930 return NULL;
1931 }
1932
receive_DataReply(struct drbd_connection * connection,struct packet_info * pi)1933 static int receive_DataReply(struct drbd_connection *connection, struct packet_info *pi)
1934 {
1935 struct drbd_peer_device *peer_device;
1936 struct drbd_device *device;
1937 struct drbd_request *req;
1938 sector_t sector;
1939 int err;
1940 struct p_data *p = pi->data;
1941
1942 peer_device = conn_peer_device(connection, pi->vnr);
1943 if (!peer_device)
1944 return -EIO;
1945 device = peer_device->device;
1946
1947 sector = be64_to_cpu(p->sector);
1948
1949 spin_lock_irq(&device->resource->req_lock);
1950 req = find_request(device, &device->read_requests, p->block_id, sector, false, __func__);
1951 spin_unlock_irq(&device->resource->req_lock);
1952 if (unlikely(!req))
1953 return -EIO;
1954
1955 err = recv_dless_read(peer_device, req, sector, pi->size);
1956 if (!err)
1957 req_mod(req, DATA_RECEIVED, peer_device);
1958 /* else: nothing. handled from drbd_disconnect...
1959 * I don't think we may complete this just yet
1960 * in case we are "on-disconnect: freeze" */
1961
1962 return err;
1963 }
1964
receive_RSDataReply(struct drbd_connection * connection,struct packet_info * pi)1965 static int receive_RSDataReply(struct drbd_connection *connection, struct packet_info *pi)
1966 {
1967 struct drbd_peer_device *peer_device;
1968 struct drbd_device *device;
1969 sector_t sector;
1970 int err;
1971 struct p_data *p = pi->data;
1972
1973 peer_device = conn_peer_device(connection, pi->vnr);
1974 if (!peer_device)
1975 return -EIO;
1976 device = peer_device->device;
1977
1978 sector = be64_to_cpu(p->sector);
1979 D_ASSERT(device, p->block_id == ID_SYNCER);
1980
1981 if (get_ldev(device)) {
1982 /* data is submitted to disk within recv_resync_read.
1983 * corresponding put_ldev done below on error,
1984 * or in drbd_peer_request_endio. */
1985 err = recv_resync_read(peer_device, sector, pi);
1986 } else {
1987 if (drbd_ratelimit())
1988 drbd_err(device, "Can not write resync data to local disk.\n");
1989
1990 err = drbd_drain_block(peer_device, pi->size);
1991
1992 drbd_send_ack_dp(peer_device, P_NEG_ACK, p, pi->size);
1993 }
1994
1995 atomic_add(pi->size >> 9, &device->rs_sect_in);
1996
1997 return err;
1998 }
1999
restart_conflicting_writes(struct drbd_device * device,sector_t sector,int size)2000 static void restart_conflicting_writes(struct drbd_device *device,
2001 sector_t sector, int size)
2002 {
2003 struct drbd_interval *i;
2004 struct drbd_request *req;
2005
2006 drbd_for_each_overlap(i, &device->write_requests, sector, size) {
2007 if (!i->local)
2008 continue;
2009 req = container_of(i, struct drbd_request, i);
2010 if (req->rq_state & RQ_LOCAL_PENDING ||
2011 !(req->rq_state & RQ_POSTPONED))
2012 continue;
2013 /* as it is RQ_POSTPONED, this will cause it to
2014 * be queued on the retry workqueue. */
2015 __req_mod(req, CONFLICT_RESOLVED, NULL, NULL);
2016 }
2017 }
2018
2019 /*
2020 * e_end_block() is called in ack_sender context via drbd_finish_peer_reqs().
2021 */
e_end_block(struct drbd_work * w,int cancel)2022 static int e_end_block(struct drbd_work *w, int cancel)
2023 {
2024 struct drbd_peer_request *peer_req =
2025 container_of(w, struct drbd_peer_request, w);
2026 struct drbd_peer_device *peer_device = peer_req->peer_device;
2027 struct drbd_device *device = peer_device->device;
2028 sector_t sector = peer_req->i.sector;
2029 int err = 0, pcmd;
2030
2031 if (peer_req->flags & EE_SEND_WRITE_ACK) {
2032 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
2033 pcmd = (device->state.conn >= C_SYNC_SOURCE &&
2034 device->state.conn <= C_PAUSED_SYNC_T &&
2035 peer_req->flags & EE_MAY_SET_IN_SYNC) ?
2036 P_RS_WRITE_ACK : P_WRITE_ACK;
2037 err = drbd_send_ack(peer_device, pcmd, peer_req);
2038 if (pcmd == P_RS_WRITE_ACK)
2039 drbd_set_in_sync(peer_device, sector, peer_req->i.size);
2040 } else {
2041 err = drbd_send_ack(peer_device, P_NEG_ACK, peer_req);
2042 /* we expect it to be marked out of sync anyways...
2043 * maybe assert this? */
2044 }
2045 dec_unacked(device);
2046 }
2047
2048 /* we delete from the conflict detection hash _after_ we sent out the
2049 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
2050 if (peer_req->flags & EE_IN_INTERVAL_TREE) {
2051 spin_lock_irq(&device->resource->req_lock);
2052 D_ASSERT(device, !drbd_interval_empty(&peer_req->i));
2053 drbd_remove_epoch_entry_interval(device, peer_req);
2054 if (peer_req->flags & EE_RESTART_REQUESTS)
2055 restart_conflicting_writes(device, sector, peer_req->i.size);
2056 spin_unlock_irq(&device->resource->req_lock);
2057 } else
2058 D_ASSERT(device, drbd_interval_empty(&peer_req->i));
2059
2060 drbd_may_finish_epoch(peer_device->connection, peer_req->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
2061
2062 return err;
2063 }
2064
e_send_ack(struct drbd_work * w,enum drbd_packet ack)2065 static int e_send_ack(struct drbd_work *w, enum drbd_packet ack)
2066 {
2067 struct drbd_peer_request *peer_req =
2068 container_of(w, struct drbd_peer_request, w);
2069 struct drbd_peer_device *peer_device = peer_req->peer_device;
2070 int err;
2071
2072 err = drbd_send_ack(peer_device, ack, peer_req);
2073 dec_unacked(peer_device->device);
2074
2075 return err;
2076 }
2077
e_send_superseded(struct drbd_work * w,int unused)2078 static int e_send_superseded(struct drbd_work *w, int unused)
2079 {
2080 return e_send_ack(w, P_SUPERSEDED);
2081 }
2082
e_send_retry_write(struct drbd_work * w,int unused)2083 static int e_send_retry_write(struct drbd_work *w, int unused)
2084 {
2085 struct drbd_peer_request *peer_req =
2086 container_of(w, struct drbd_peer_request, w);
2087 struct drbd_connection *connection = peer_req->peer_device->connection;
2088
2089 return e_send_ack(w, connection->agreed_pro_version >= 100 ?
2090 P_RETRY_WRITE : P_SUPERSEDED);
2091 }
2092
seq_greater(u32 a,u32 b)2093 static bool seq_greater(u32 a, u32 b)
2094 {
2095 /*
2096 * We assume 32-bit wrap-around here.
2097 * For 24-bit wrap-around, we would have to shift:
2098 * a <<= 8; b <<= 8;
2099 */
2100 return (s32)a - (s32)b > 0;
2101 }
2102
seq_max(u32 a,u32 b)2103 static u32 seq_max(u32 a, u32 b)
2104 {
2105 return seq_greater(a, b) ? a : b;
2106 }
2107
update_peer_seq(struct drbd_peer_device * peer_device,unsigned int peer_seq)2108 static void update_peer_seq(struct drbd_peer_device *peer_device, unsigned int peer_seq)
2109 {
2110 struct drbd_device *device = peer_device->device;
2111 unsigned int newest_peer_seq;
2112
2113 if (test_bit(RESOLVE_CONFLICTS, &peer_device->connection->flags)) {
2114 spin_lock(&device->peer_seq_lock);
2115 newest_peer_seq = seq_max(device->peer_seq, peer_seq);
2116 device->peer_seq = newest_peer_seq;
2117 spin_unlock(&device->peer_seq_lock);
2118 /* wake up only if we actually changed device->peer_seq */
2119 if (peer_seq == newest_peer_seq)
2120 wake_up(&device->seq_wait);
2121 }
2122 }
2123
overlaps(sector_t s1,int l1,sector_t s2,int l2)2124 static inline int overlaps(sector_t s1, int l1, sector_t s2, int l2)
2125 {
2126 return !((s1 + (l1>>9) <= s2) || (s1 >= s2 + (l2>>9)));
2127 }
2128
2129 /* maybe change sync_ee into interval trees as well? */
overlapping_resync_write(struct drbd_device * device,struct drbd_peer_request * peer_req)2130 static bool overlapping_resync_write(struct drbd_device *device, struct drbd_peer_request *peer_req)
2131 {
2132 struct drbd_peer_request *rs_req;
2133 bool rv = false;
2134
2135 spin_lock_irq(&device->resource->req_lock);
2136 list_for_each_entry(rs_req, &device->sync_ee, w.list) {
2137 if (overlaps(peer_req->i.sector, peer_req->i.size,
2138 rs_req->i.sector, rs_req->i.size)) {
2139 rv = true;
2140 break;
2141 }
2142 }
2143 spin_unlock_irq(&device->resource->req_lock);
2144
2145 return rv;
2146 }
2147
2148 /* Called from receive_Data.
2149 * Synchronize packets on sock with packets on msock.
2150 *
2151 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
2152 * packet traveling on msock, they are still processed in the order they have
2153 * been sent.
2154 *
2155 * Note: we don't care for Ack packets overtaking P_DATA packets.
2156 *
2157 * In case packet_seq is larger than device->peer_seq number, there are
2158 * outstanding packets on the msock. We wait for them to arrive.
2159 * In case we are the logically next packet, we update device->peer_seq
2160 * ourselves. Correctly handles 32bit wrap around.
2161 *
2162 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
2163 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
2164 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
2165 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
2166 *
2167 * returns 0 if we may process the packet,
2168 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
wait_for_and_update_peer_seq(struct drbd_peer_device * peer_device,const u32 peer_seq)2169 static int wait_for_and_update_peer_seq(struct drbd_peer_device *peer_device, const u32 peer_seq)
2170 {
2171 struct drbd_device *device = peer_device->device;
2172 DEFINE_WAIT(wait);
2173 long timeout;
2174 int ret = 0, tp;
2175
2176 if (!test_bit(RESOLVE_CONFLICTS, &peer_device->connection->flags))
2177 return 0;
2178
2179 spin_lock(&device->peer_seq_lock);
2180 for (;;) {
2181 if (!seq_greater(peer_seq - 1, device->peer_seq)) {
2182 device->peer_seq = seq_max(device->peer_seq, peer_seq);
2183 break;
2184 }
2185
2186 if (signal_pending(current)) {
2187 ret = -ERESTARTSYS;
2188 break;
2189 }
2190
2191 rcu_read_lock();
2192 tp = rcu_dereference(peer_device->connection->net_conf)->two_primaries;
2193 rcu_read_unlock();
2194
2195 if (!tp)
2196 break;
2197
2198 /* Only need to wait if two_primaries is enabled */
2199 prepare_to_wait(&device->seq_wait, &wait, TASK_INTERRUPTIBLE);
2200 spin_unlock(&device->peer_seq_lock);
2201 rcu_read_lock();
2202 timeout = rcu_dereference(peer_device->connection->net_conf)->ping_timeo*HZ/10;
2203 rcu_read_unlock();
2204 timeout = schedule_timeout(timeout);
2205 spin_lock(&device->peer_seq_lock);
2206 if (!timeout) {
2207 ret = -ETIMEDOUT;
2208 drbd_err(device, "Timed out waiting for missing ack packets; disconnecting\n");
2209 break;
2210 }
2211 }
2212 spin_unlock(&device->peer_seq_lock);
2213 finish_wait(&device->seq_wait, &wait);
2214 return ret;
2215 }
2216
wire_flags_to_bio_op(u32 dpf)2217 static enum req_op wire_flags_to_bio_op(u32 dpf)
2218 {
2219 if (dpf & DP_ZEROES)
2220 return REQ_OP_WRITE_ZEROES;
2221 if (dpf & DP_DISCARD)
2222 return REQ_OP_DISCARD;
2223 else
2224 return REQ_OP_WRITE;
2225 }
2226
2227 /* see also bio_flags_to_wire() */
wire_flags_to_bio(struct drbd_connection * connection,u32 dpf)2228 static blk_opf_t wire_flags_to_bio(struct drbd_connection *connection, u32 dpf)
2229 {
2230 return wire_flags_to_bio_op(dpf) |
2231 (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
2232 (dpf & DP_FUA ? REQ_FUA : 0) |
2233 (dpf & DP_FLUSH ? REQ_PREFLUSH : 0);
2234 }
2235
fail_postponed_requests(struct drbd_device * device,sector_t sector,unsigned int size)2236 static void fail_postponed_requests(struct drbd_device *device, sector_t sector,
2237 unsigned int size)
2238 {
2239 struct drbd_peer_device *peer_device = first_peer_device(device);
2240 struct drbd_interval *i;
2241
2242 repeat:
2243 drbd_for_each_overlap(i, &device->write_requests, sector, size) {
2244 struct drbd_request *req;
2245 struct bio_and_error m;
2246
2247 if (!i->local)
2248 continue;
2249 req = container_of(i, struct drbd_request, i);
2250 if (!(req->rq_state & RQ_POSTPONED))
2251 continue;
2252 req->rq_state &= ~RQ_POSTPONED;
2253 __req_mod(req, NEG_ACKED, peer_device, &m);
2254 spin_unlock_irq(&device->resource->req_lock);
2255 if (m.bio)
2256 complete_master_bio(device, &m);
2257 spin_lock_irq(&device->resource->req_lock);
2258 goto repeat;
2259 }
2260 }
2261
handle_write_conflicts(struct drbd_device * device,struct drbd_peer_request * peer_req)2262 static int handle_write_conflicts(struct drbd_device *device,
2263 struct drbd_peer_request *peer_req)
2264 {
2265 struct drbd_connection *connection = peer_req->peer_device->connection;
2266 bool resolve_conflicts = test_bit(RESOLVE_CONFLICTS, &connection->flags);
2267 sector_t sector = peer_req->i.sector;
2268 const unsigned int size = peer_req->i.size;
2269 struct drbd_interval *i;
2270 bool equal;
2271 int err;
2272
2273 /*
2274 * Inserting the peer request into the write_requests tree will prevent
2275 * new conflicting local requests from being added.
2276 */
2277 drbd_insert_interval(&device->write_requests, &peer_req->i);
2278
2279 repeat:
2280 drbd_for_each_overlap(i, &device->write_requests, sector, size) {
2281 if (i == &peer_req->i)
2282 continue;
2283 if (i->completed)
2284 continue;
2285
2286 if (!i->local) {
2287 /*
2288 * Our peer has sent a conflicting remote request; this
2289 * should not happen in a two-node setup. Wait for the
2290 * earlier peer request to complete.
2291 */
2292 err = drbd_wait_misc(device, i);
2293 if (err)
2294 goto out;
2295 goto repeat;
2296 }
2297
2298 equal = i->sector == sector && i->size == size;
2299 if (resolve_conflicts) {
2300 /*
2301 * If the peer request is fully contained within the
2302 * overlapping request, it can be considered overwritten
2303 * and thus superseded; otherwise, it will be retried
2304 * once all overlapping requests have completed.
2305 */
2306 bool superseded = i->sector <= sector && i->sector +
2307 (i->size >> 9) >= sector + (size >> 9);
2308
2309 if (!equal)
2310 drbd_alert(device, "Concurrent writes detected: "
2311 "local=%llus +%u, remote=%llus +%u, "
2312 "assuming %s came first\n",
2313 (unsigned long long)i->sector, i->size,
2314 (unsigned long long)sector, size,
2315 superseded ? "local" : "remote");
2316
2317 peer_req->w.cb = superseded ? e_send_superseded :
2318 e_send_retry_write;
2319 list_add_tail(&peer_req->w.list, &device->done_ee);
2320 /* put is in drbd_send_acks_wf() */
2321 kref_get(&device->kref);
2322 if (!queue_work(connection->ack_sender,
2323 &peer_req->peer_device->send_acks_work))
2324 kref_put(&device->kref, drbd_destroy_device);
2325
2326 err = -ENOENT;
2327 goto out;
2328 } else {
2329 struct drbd_request *req =
2330 container_of(i, struct drbd_request, i);
2331
2332 if (!equal)
2333 drbd_alert(device, "Concurrent writes detected: "
2334 "local=%llus +%u, remote=%llus +%u\n",
2335 (unsigned long long)i->sector, i->size,
2336 (unsigned long long)sector, size);
2337
2338 if (req->rq_state & RQ_LOCAL_PENDING ||
2339 !(req->rq_state & RQ_POSTPONED)) {
2340 /*
2341 * Wait for the node with the discard flag to
2342 * decide if this request has been superseded
2343 * or needs to be retried.
2344 * Requests that have been superseded will
2345 * disappear from the write_requests tree.
2346 *
2347 * In addition, wait for the conflicting
2348 * request to finish locally before submitting
2349 * the conflicting peer request.
2350 */
2351 err = drbd_wait_misc(device, &req->i);
2352 if (err) {
2353 _conn_request_state(connection, NS(conn, C_TIMEOUT), CS_HARD);
2354 fail_postponed_requests(device, sector, size);
2355 goto out;
2356 }
2357 goto repeat;
2358 }
2359 /*
2360 * Remember to restart the conflicting requests after
2361 * the new peer request has completed.
2362 */
2363 peer_req->flags |= EE_RESTART_REQUESTS;
2364 }
2365 }
2366 err = 0;
2367
2368 out:
2369 if (err)
2370 drbd_remove_epoch_entry_interval(device, peer_req);
2371 return err;
2372 }
2373
2374 /* mirrored write */
receive_Data(struct drbd_connection * connection,struct packet_info * pi)2375 static int receive_Data(struct drbd_connection *connection, struct packet_info *pi)
2376 {
2377 struct drbd_peer_device *peer_device;
2378 struct drbd_device *device;
2379 struct net_conf *nc;
2380 sector_t sector;
2381 struct drbd_peer_request *peer_req;
2382 struct p_data *p = pi->data;
2383 u32 peer_seq = be32_to_cpu(p->seq_num);
2384 u32 dp_flags;
2385 int err, tp;
2386
2387 peer_device = conn_peer_device(connection, pi->vnr);
2388 if (!peer_device)
2389 return -EIO;
2390 device = peer_device->device;
2391
2392 if (!get_ldev(device)) {
2393 int err2;
2394
2395 err = wait_for_and_update_peer_seq(peer_device, peer_seq);
2396 drbd_send_ack_dp(peer_device, P_NEG_ACK, p, pi->size);
2397 atomic_inc(&connection->current_epoch->epoch_size);
2398 err2 = drbd_drain_block(peer_device, pi->size);
2399 if (!err)
2400 err = err2;
2401 return err;
2402 }
2403
2404 /*
2405 * Corresponding put_ldev done either below (on various errors), or in
2406 * drbd_peer_request_endio, if we successfully submit the data at the
2407 * end of this function.
2408 */
2409
2410 sector = be64_to_cpu(p->sector);
2411 peer_req = read_in_block(peer_device, p->block_id, sector, pi);
2412 if (!peer_req) {
2413 put_ldev(device);
2414 return -EIO;
2415 }
2416
2417 peer_req->w.cb = e_end_block;
2418 peer_req->submit_jif = jiffies;
2419 peer_req->flags |= EE_APPLICATION;
2420
2421 dp_flags = be32_to_cpu(p->dp_flags);
2422 peer_req->opf = wire_flags_to_bio(connection, dp_flags);
2423 if (pi->cmd == P_TRIM) {
2424 D_ASSERT(peer_device, peer_req->i.size > 0);
2425 D_ASSERT(peer_device, peer_req_op(peer_req) == REQ_OP_DISCARD);
2426 D_ASSERT(peer_device, peer_req->pages == NULL);
2427 /* need to play safe: an older DRBD sender
2428 * may mean zero-out while sending P_TRIM. */
2429 if (0 == (connection->agreed_features & DRBD_FF_WZEROES))
2430 peer_req->flags |= EE_ZEROOUT;
2431 } else if (pi->cmd == P_ZEROES) {
2432 D_ASSERT(peer_device, peer_req->i.size > 0);
2433 D_ASSERT(peer_device, peer_req_op(peer_req) == REQ_OP_WRITE_ZEROES);
2434 D_ASSERT(peer_device, peer_req->pages == NULL);
2435 /* Do (not) pass down BLKDEV_ZERO_NOUNMAP? */
2436 if (dp_flags & DP_DISCARD)
2437 peer_req->flags |= EE_TRIM;
2438 } else if (peer_req->pages == NULL) {
2439 D_ASSERT(device, peer_req->i.size == 0);
2440 D_ASSERT(device, dp_flags & DP_FLUSH);
2441 }
2442
2443 if (dp_flags & DP_MAY_SET_IN_SYNC)
2444 peer_req->flags |= EE_MAY_SET_IN_SYNC;
2445
2446 spin_lock(&connection->epoch_lock);
2447 peer_req->epoch = connection->current_epoch;
2448 atomic_inc(&peer_req->epoch->epoch_size);
2449 atomic_inc(&peer_req->epoch->active);
2450 spin_unlock(&connection->epoch_lock);
2451
2452 rcu_read_lock();
2453 nc = rcu_dereference(peer_device->connection->net_conf);
2454 tp = nc->two_primaries;
2455 if (peer_device->connection->agreed_pro_version < 100) {
2456 switch (nc->wire_protocol) {
2457 case DRBD_PROT_C:
2458 dp_flags |= DP_SEND_WRITE_ACK;
2459 break;
2460 case DRBD_PROT_B:
2461 dp_flags |= DP_SEND_RECEIVE_ACK;
2462 break;
2463 }
2464 }
2465 rcu_read_unlock();
2466
2467 if (dp_flags & DP_SEND_WRITE_ACK) {
2468 peer_req->flags |= EE_SEND_WRITE_ACK;
2469 inc_unacked(device);
2470 /* corresponding dec_unacked() in e_end_block()
2471 * respective _drbd_clear_done_ee */
2472 }
2473
2474 if (dp_flags & DP_SEND_RECEIVE_ACK) {
2475 /* I really don't like it that the receiver thread
2476 * sends on the msock, but anyways */
2477 drbd_send_ack(peer_device, P_RECV_ACK, peer_req);
2478 }
2479
2480 if (tp) {
2481 /* two primaries implies protocol C */
2482 D_ASSERT(device, dp_flags & DP_SEND_WRITE_ACK);
2483 peer_req->flags |= EE_IN_INTERVAL_TREE;
2484 err = wait_for_and_update_peer_seq(peer_device, peer_seq);
2485 if (err)
2486 goto out_interrupted;
2487 spin_lock_irq(&device->resource->req_lock);
2488 err = handle_write_conflicts(device, peer_req);
2489 if (err) {
2490 spin_unlock_irq(&device->resource->req_lock);
2491 if (err == -ENOENT) {
2492 put_ldev(device);
2493 return 0;
2494 }
2495 goto out_interrupted;
2496 }
2497 } else {
2498 update_peer_seq(peer_device, peer_seq);
2499 spin_lock_irq(&device->resource->req_lock);
2500 }
2501 /* TRIM and is processed synchronously,
2502 * we wait for all pending requests, respectively wait for
2503 * active_ee to become empty in drbd_submit_peer_request();
2504 * better not add ourselves here. */
2505 if ((peer_req->flags & (EE_TRIM | EE_ZEROOUT)) == 0)
2506 list_add_tail(&peer_req->w.list, &device->active_ee);
2507 spin_unlock_irq(&device->resource->req_lock);
2508
2509 if (device->state.conn == C_SYNC_TARGET)
2510 wait_event(device->ee_wait, !overlapping_resync_write(device, peer_req));
2511
2512 if (device->state.pdsk < D_INCONSISTENT) {
2513 /* In case we have the only disk of the cluster, */
2514 drbd_set_out_of_sync(peer_device, peer_req->i.sector, peer_req->i.size);
2515 peer_req->flags &= ~EE_MAY_SET_IN_SYNC;
2516 drbd_al_begin_io(device, &peer_req->i);
2517 peer_req->flags |= EE_CALL_AL_COMPLETE_IO;
2518 }
2519
2520 err = drbd_submit_peer_request(peer_req);
2521 if (!err)
2522 return 0;
2523
2524 /* don't care for the reason here */
2525 drbd_err(device, "submit failed, triggering re-connect\n");
2526 spin_lock_irq(&device->resource->req_lock);
2527 list_del(&peer_req->w.list);
2528 drbd_remove_epoch_entry_interval(device, peer_req);
2529 spin_unlock_irq(&device->resource->req_lock);
2530 if (peer_req->flags & EE_CALL_AL_COMPLETE_IO) {
2531 peer_req->flags &= ~EE_CALL_AL_COMPLETE_IO;
2532 drbd_al_complete_io(device, &peer_req->i);
2533 }
2534
2535 out_interrupted:
2536 drbd_may_finish_epoch(connection, peer_req->epoch, EV_PUT | EV_CLEANUP);
2537 put_ldev(device);
2538 drbd_free_peer_req(device, peer_req);
2539 return err;
2540 }
2541
2542 /* We may throttle resync, if the lower device seems to be busy,
2543 * and current sync rate is above c_min_rate.
2544 *
2545 * To decide whether or not the lower device is busy, we use a scheme similar
2546 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
2547 * (more than 64 sectors) of activity we cannot account for with our own resync
2548 * activity, it obviously is "busy".
2549 *
2550 * The current sync rate used here uses only the most recent two step marks,
2551 * to have a short time average so we can react faster.
2552 */
drbd_rs_should_slow_down(struct drbd_peer_device * peer_device,sector_t sector,bool throttle_if_app_is_waiting)2553 bool drbd_rs_should_slow_down(struct drbd_peer_device *peer_device, sector_t sector,
2554 bool throttle_if_app_is_waiting)
2555 {
2556 struct drbd_device *device = peer_device->device;
2557 struct lc_element *tmp;
2558 bool throttle = drbd_rs_c_min_rate_throttle(device);
2559
2560 if (!throttle || throttle_if_app_is_waiting)
2561 return throttle;
2562
2563 spin_lock_irq(&device->al_lock);
2564 tmp = lc_find(device->resync, BM_SECT_TO_EXT(sector));
2565 if (tmp) {
2566 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
2567 if (test_bit(BME_PRIORITY, &bm_ext->flags))
2568 throttle = false;
2569 /* Do not slow down if app IO is already waiting for this extent,
2570 * and our progress is necessary for application IO to complete. */
2571 }
2572 spin_unlock_irq(&device->al_lock);
2573
2574 return throttle;
2575 }
2576
drbd_rs_c_min_rate_throttle(struct drbd_device * device)2577 bool drbd_rs_c_min_rate_throttle(struct drbd_device *device)
2578 {
2579 struct gendisk *disk = device->ldev->backing_bdev->bd_disk;
2580 unsigned long db, dt, dbdt;
2581 unsigned int c_min_rate;
2582 int curr_events;
2583
2584 rcu_read_lock();
2585 c_min_rate = rcu_dereference(device->ldev->disk_conf)->c_min_rate;
2586 rcu_read_unlock();
2587
2588 /* feature disabled? */
2589 if (c_min_rate == 0)
2590 return false;
2591
2592 curr_events = (int)part_stat_read_accum(disk->part0, sectors) -
2593 atomic_read(&device->rs_sect_ev);
2594
2595 if (atomic_read(&device->ap_actlog_cnt)
2596 || curr_events - device->rs_last_events > 64) {
2597 unsigned long rs_left;
2598 int i;
2599
2600 device->rs_last_events = curr_events;
2601
2602 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
2603 * approx. */
2604 i = (device->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
2605
2606 if (device->state.conn == C_VERIFY_S || device->state.conn == C_VERIFY_T)
2607 rs_left = device->ov_left;
2608 else
2609 rs_left = drbd_bm_total_weight(device) - device->rs_failed;
2610
2611 dt = ((long)jiffies - (long)device->rs_mark_time[i]) / HZ;
2612 if (!dt)
2613 dt++;
2614 db = device->rs_mark_left[i] - rs_left;
2615 dbdt = Bit2KB(db/dt);
2616
2617 if (dbdt > c_min_rate)
2618 return true;
2619 }
2620 return false;
2621 }
2622
receive_DataRequest(struct drbd_connection * connection,struct packet_info * pi)2623 static int receive_DataRequest(struct drbd_connection *connection, struct packet_info *pi)
2624 {
2625 struct drbd_peer_device *peer_device;
2626 struct drbd_device *device;
2627 sector_t sector;
2628 sector_t capacity;
2629 struct drbd_peer_request *peer_req;
2630 struct digest_info *di = NULL;
2631 int size, verb;
2632 struct p_block_req *p = pi->data;
2633
2634 peer_device = conn_peer_device(connection, pi->vnr);
2635 if (!peer_device)
2636 return -EIO;
2637 device = peer_device->device;
2638 capacity = get_capacity(device->vdisk);
2639
2640 sector = be64_to_cpu(p->sector);
2641 size = be32_to_cpu(p->blksize);
2642
2643 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
2644 drbd_err(device, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2645 (unsigned long long)sector, size);
2646 return -EINVAL;
2647 }
2648 if (sector + (size>>9) > capacity) {
2649 drbd_err(device, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2650 (unsigned long long)sector, size);
2651 return -EINVAL;
2652 }
2653
2654 if (!get_ldev_if_state(device, D_UP_TO_DATE)) {
2655 verb = 1;
2656 switch (pi->cmd) {
2657 case P_DATA_REQUEST:
2658 drbd_send_ack_rp(peer_device, P_NEG_DREPLY, p);
2659 break;
2660 case P_RS_THIN_REQ:
2661 case P_RS_DATA_REQUEST:
2662 case P_CSUM_RS_REQUEST:
2663 case P_OV_REQUEST:
2664 drbd_send_ack_rp(peer_device, P_NEG_RS_DREPLY , p);
2665 break;
2666 case P_OV_REPLY:
2667 verb = 0;
2668 dec_rs_pending(peer_device);
2669 drbd_send_ack_ex(peer_device, P_OV_RESULT, sector, size, ID_IN_SYNC);
2670 break;
2671 default:
2672 BUG();
2673 }
2674 if (verb && drbd_ratelimit())
2675 drbd_err(device, "Can not satisfy peer's read request, "
2676 "no local data.\n");
2677
2678 /* drain possibly payload */
2679 return drbd_drain_block(peer_device, pi->size);
2680 }
2681
2682 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
2683 * "criss-cross" setup, that might cause write-out on some other DRBD,
2684 * which in turn might block on the other node at this very place. */
2685 peer_req = drbd_alloc_peer_req(peer_device, p->block_id, sector, size,
2686 size, GFP_NOIO);
2687 if (!peer_req) {
2688 put_ldev(device);
2689 return -ENOMEM;
2690 }
2691 peer_req->opf = REQ_OP_READ;
2692
2693 switch (pi->cmd) {
2694 case P_DATA_REQUEST:
2695 peer_req->w.cb = w_e_end_data_req;
2696 /* application IO, don't drbd_rs_begin_io */
2697 peer_req->flags |= EE_APPLICATION;
2698 goto submit;
2699
2700 case P_RS_THIN_REQ:
2701 /* If at some point in the future we have a smart way to
2702 find out if this data block is completely deallocated,
2703 then we would do something smarter here than reading
2704 the block... */
2705 peer_req->flags |= EE_RS_THIN_REQ;
2706 fallthrough;
2707 case P_RS_DATA_REQUEST:
2708 peer_req->w.cb = w_e_end_rsdata_req;
2709 /* used in the sector offset progress display */
2710 device->bm_resync_fo = BM_SECT_TO_BIT(sector);
2711 break;
2712
2713 case P_OV_REPLY:
2714 case P_CSUM_RS_REQUEST:
2715 di = kmalloc(sizeof(*di) + pi->size, GFP_NOIO);
2716 if (!di)
2717 goto out_free_e;
2718
2719 di->digest_size = pi->size;
2720 di->digest = (((char *)di)+sizeof(struct digest_info));
2721
2722 peer_req->digest = di;
2723 peer_req->flags |= EE_HAS_DIGEST;
2724
2725 if (drbd_recv_all(peer_device->connection, di->digest, pi->size))
2726 goto out_free_e;
2727
2728 if (pi->cmd == P_CSUM_RS_REQUEST) {
2729 D_ASSERT(device, peer_device->connection->agreed_pro_version >= 89);
2730 peer_req->w.cb = w_e_end_csum_rs_req;
2731 /* used in the sector offset progress display */
2732 device->bm_resync_fo = BM_SECT_TO_BIT(sector);
2733 /* remember to report stats in drbd_resync_finished */
2734 device->use_csums = true;
2735 } else if (pi->cmd == P_OV_REPLY) {
2736 /* track progress, we may need to throttle */
2737 atomic_add(size >> 9, &device->rs_sect_in);
2738 peer_req->w.cb = w_e_end_ov_reply;
2739 dec_rs_pending(peer_device);
2740 /* drbd_rs_begin_io done when we sent this request,
2741 * but accounting still needs to be done. */
2742 goto submit_for_resync;
2743 }
2744 break;
2745
2746 case P_OV_REQUEST:
2747 if (device->ov_start_sector == ~(sector_t)0 &&
2748 peer_device->connection->agreed_pro_version >= 90) {
2749 unsigned long now = jiffies;
2750 int i;
2751 device->ov_start_sector = sector;
2752 device->ov_position = sector;
2753 device->ov_left = drbd_bm_bits(device) - BM_SECT_TO_BIT(sector);
2754 device->rs_total = device->ov_left;
2755 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
2756 device->rs_mark_left[i] = device->ov_left;
2757 device->rs_mark_time[i] = now;
2758 }
2759 drbd_info(device, "Online Verify start sector: %llu\n",
2760 (unsigned long long)sector);
2761 }
2762 peer_req->w.cb = w_e_end_ov_req;
2763 break;
2764
2765 default:
2766 BUG();
2767 }
2768
2769 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2770 * wrt the receiver, but it is not as straightforward as it may seem.
2771 * Various places in the resync start and stop logic assume resync
2772 * requests are processed in order, requeuing this on the worker thread
2773 * introduces a bunch of new code for synchronization between threads.
2774 *
2775 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2776 * "forever", throttling after drbd_rs_begin_io will lock that extent
2777 * for application writes for the same time. For now, just throttle
2778 * here, where the rest of the code expects the receiver to sleep for
2779 * a while, anyways.
2780 */
2781
2782 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2783 * this defers syncer requests for some time, before letting at least
2784 * on request through. The resync controller on the receiving side
2785 * will adapt to the incoming rate accordingly.
2786 *
2787 * We cannot throttle here if remote is Primary/SyncTarget:
2788 * we would also throttle its application reads.
2789 * In that case, throttling is done on the SyncTarget only.
2790 */
2791
2792 /* Even though this may be a resync request, we do add to "read_ee";
2793 * "sync_ee" is only used for resync WRITEs.
2794 * Add to list early, so debugfs can find this request
2795 * even if we have to sleep below. */
2796 spin_lock_irq(&device->resource->req_lock);
2797 list_add_tail(&peer_req->w.list, &device->read_ee);
2798 spin_unlock_irq(&device->resource->req_lock);
2799
2800 update_receiver_timing_details(connection, drbd_rs_should_slow_down);
2801 if (device->state.peer != R_PRIMARY
2802 && drbd_rs_should_slow_down(peer_device, sector, false))
2803 schedule_timeout_uninterruptible(HZ/10);
2804 update_receiver_timing_details(connection, drbd_rs_begin_io);
2805 if (drbd_rs_begin_io(device, sector))
2806 goto out_free_e;
2807
2808 submit_for_resync:
2809 atomic_add(size >> 9, &device->rs_sect_ev);
2810
2811 submit:
2812 update_receiver_timing_details(connection, drbd_submit_peer_request);
2813 inc_unacked(device);
2814 if (drbd_submit_peer_request(peer_req) == 0)
2815 return 0;
2816
2817 /* don't care for the reason here */
2818 drbd_err(device, "submit failed, triggering re-connect\n");
2819
2820 out_free_e:
2821 spin_lock_irq(&device->resource->req_lock);
2822 list_del(&peer_req->w.list);
2823 spin_unlock_irq(&device->resource->req_lock);
2824 /* no drbd_rs_complete_io(), we are dropping the connection anyways */
2825
2826 put_ldev(device);
2827 drbd_free_peer_req(device, peer_req);
2828 return -EIO;
2829 }
2830
2831 /*
2832 * drbd_asb_recover_0p - Recover after split-brain with no remaining primaries
2833 */
drbd_asb_recover_0p(struct drbd_peer_device * peer_device)2834 static int drbd_asb_recover_0p(struct drbd_peer_device *peer_device) __must_hold(local)
2835 {
2836 struct drbd_device *device = peer_device->device;
2837 int self, peer, rv = -100;
2838 unsigned long ch_self, ch_peer;
2839 enum drbd_after_sb_p after_sb_0p;
2840
2841 self = device->ldev->md.uuid[UI_BITMAP] & 1;
2842 peer = device->p_uuid[UI_BITMAP] & 1;
2843
2844 ch_peer = device->p_uuid[UI_SIZE];
2845 ch_self = device->comm_bm_set;
2846
2847 rcu_read_lock();
2848 after_sb_0p = rcu_dereference(peer_device->connection->net_conf)->after_sb_0p;
2849 rcu_read_unlock();
2850 switch (after_sb_0p) {
2851 case ASB_CONSENSUS:
2852 case ASB_DISCARD_SECONDARY:
2853 case ASB_CALL_HELPER:
2854 case ASB_VIOLENTLY:
2855 drbd_err(device, "Configuration error.\n");
2856 break;
2857 case ASB_DISCONNECT:
2858 break;
2859 case ASB_DISCARD_YOUNGER_PRI:
2860 if (self == 0 && peer == 1) {
2861 rv = -1;
2862 break;
2863 }
2864 if (self == 1 && peer == 0) {
2865 rv = 1;
2866 break;
2867 }
2868 fallthrough; /* to one of the other strategies */
2869 case ASB_DISCARD_OLDER_PRI:
2870 if (self == 0 && peer == 1) {
2871 rv = 1;
2872 break;
2873 }
2874 if (self == 1 && peer == 0) {
2875 rv = -1;
2876 break;
2877 }
2878 /* Else fall through to one of the other strategies... */
2879 drbd_warn(device, "Discard younger/older primary did not find a decision\n"
2880 "Using discard-least-changes instead\n");
2881 fallthrough;
2882 case ASB_DISCARD_ZERO_CHG:
2883 if (ch_peer == 0 && ch_self == 0) {
2884 rv = test_bit(RESOLVE_CONFLICTS, &peer_device->connection->flags)
2885 ? -1 : 1;
2886 break;
2887 } else {
2888 if (ch_peer == 0) { rv = 1; break; }
2889 if (ch_self == 0) { rv = -1; break; }
2890 }
2891 if (after_sb_0p == ASB_DISCARD_ZERO_CHG)
2892 break;
2893 fallthrough;
2894 case ASB_DISCARD_LEAST_CHG:
2895 if (ch_self < ch_peer)
2896 rv = -1;
2897 else if (ch_self > ch_peer)
2898 rv = 1;
2899 else /* ( ch_self == ch_peer ) */
2900 /* Well, then use something else. */
2901 rv = test_bit(RESOLVE_CONFLICTS, &peer_device->connection->flags)
2902 ? -1 : 1;
2903 break;
2904 case ASB_DISCARD_LOCAL:
2905 rv = -1;
2906 break;
2907 case ASB_DISCARD_REMOTE:
2908 rv = 1;
2909 }
2910
2911 return rv;
2912 }
2913
2914 /*
2915 * drbd_asb_recover_1p - Recover after split-brain with one remaining primary
2916 */
drbd_asb_recover_1p(struct drbd_peer_device * peer_device)2917 static int drbd_asb_recover_1p(struct drbd_peer_device *peer_device) __must_hold(local)
2918 {
2919 struct drbd_device *device = peer_device->device;
2920 int hg, rv = -100;
2921 enum drbd_after_sb_p after_sb_1p;
2922
2923 rcu_read_lock();
2924 after_sb_1p = rcu_dereference(peer_device->connection->net_conf)->after_sb_1p;
2925 rcu_read_unlock();
2926 switch (after_sb_1p) {
2927 case ASB_DISCARD_YOUNGER_PRI:
2928 case ASB_DISCARD_OLDER_PRI:
2929 case ASB_DISCARD_LEAST_CHG:
2930 case ASB_DISCARD_LOCAL:
2931 case ASB_DISCARD_REMOTE:
2932 case ASB_DISCARD_ZERO_CHG:
2933 drbd_err(device, "Configuration error.\n");
2934 break;
2935 case ASB_DISCONNECT:
2936 break;
2937 case ASB_CONSENSUS:
2938 hg = drbd_asb_recover_0p(peer_device);
2939 if (hg == -1 && device->state.role == R_SECONDARY)
2940 rv = hg;
2941 if (hg == 1 && device->state.role == R_PRIMARY)
2942 rv = hg;
2943 break;
2944 case ASB_VIOLENTLY:
2945 rv = drbd_asb_recover_0p(peer_device);
2946 break;
2947 case ASB_DISCARD_SECONDARY:
2948 return device->state.role == R_PRIMARY ? 1 : -1;
2949 case ASB_CALL_HELPER:
2950 hg = drbd_asb_recover_0p(peer_device);
2951 if (hg == -1 && device->state.role == R_PRIMARY) {
2952 enum drbd_state_rv rv2;
2953
2954 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2955 * we might be here in C_WF_REPORT_PARAMS which is transient.
2956 * we do not need to wait for the after state change work either. */
2957 rv2 = drbd_change_state(device, CS_VERBOSE, NS(role, R_SECONDARY));
2958 if (rv2 != SS_SUCCESS) {
2959 drbd_khelper(device, "pri-lost-after-sb");
2960 } else {
2961 drbd_warn(device, "Successfully gave up primary role.\n");
2962 rv = hg;
2963 }
2964 } else
2965 rv = hg;
2966 }
2967
2968 return rv;
2969 }
2970
2971 /*
2972 * drbd_asb_recover_2p - Recover after split-brain with two remaining primaries
2973 */
drbd_asb_recover_2p(struct drbd_peer_device * peer_device)2974 static int drbd_asb_recover_2p(struct drbd_peer_device *peer_device) __must_hold(local)
2975 {
2976 struct drbd_device *device = peer_device->device;
2977 int hg, rv = -100;
2978 enum drbd_after_sb_p after_sb_2p;
2979
2980 rcu_read_lock();
2981 after_sb_2p = rcu_dereference(peer_device->connection->net_conf)->after_sb_2p;
2982 rcu_read_unlock();
2983 switch (after_sb_2p) {
2984 case ASB_DISCARD_YOUNGER_PRI:
2985 case ASB_DISCARD_OLDER_PRI:
2986 case ASB_DISCARD_LEAST_CHG:
2987 case ASB_DISCARD_LOCAL:
2988 case ASB_DISCARD_REMOTE:
2989 case ASB_CONSENSUS:
2990 case ASB_DISCARD_SECONDARY:
2991 case ASB_DISCARD_ZERO_CHG:
2992 drbd_err(device, "Configuration error.\n");
2993 break;
2994 case ASB_VIOLENTLY:
2995 rv = drbd_asb_recover_0p(peer_device);
2996 break;
2997 case ASB_DISCONNECT:
2998 break;
2999 case ASB_CALL_HELPER:
3000 hg = drbd_asb_recover_0p(peer_device);
3001 if (hg == -1) {
3002 enum drbd_state_rv rv2;
3003
3004 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
3005 * we might be here in C_WF_REPORT_PARAMS which is transient.
3006 * we do not need to wait for the after state change work either. */
3007 rv2 = drbd_change_state(device, CS_VERBOSE, NS(role, R_SECONDARY));
3008 if (rv2 != SS_SUCCESS) {
3009 drbd_khelper(device, "pri-lost-after-sb");
3010 } else {
3011 drbd_warn(device, "Successfully gave up primary role.\n");
3012 rv = hg;
3013 }
3014 } else
3015 rv = hg;
3016 }
3017
3018 return rv;
3019 }
3020
drbd_uuid_dump(struct drbd_device * device,char * text,u64 * uuid,u64 bits,u64 flags)3021 static void drbd_uuid_dump(struct drbd_device *device, char *text, u64 *uuid,
3022 u64 bits, u64 flags)
3023 {
3024 if (!uuid) {
3025 drbd_info(device, "%s uuid info vanished while I was looking!\n", text);
3026 return;
3027 }
3028 drbd_info(device, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
3029 text,
3030 (unsigned long long)uuid[UI_CURRENT],
3031 (unsigned long long)uuid[UI_BITMAP],
3032 (unsigned long long)uuid[UI_HISTORY_START],
3033 (unsigned long long)uuid[UI_HISTORY_END],
3034 (unsigned long long)bits,
3035 (unsigned long long)flags);
3036 }
3037
3038 /*
3039 100 after split brain try auto recover
3040 2 C_SYNC_SOURCE set BitMap
3041 1 C_SYNC_SOURCE use BitMap
3042 0 no Sync
3043 -1 C_SYNC_TARGET use BitMap
3044 -2 C_SYNC_TARGET set BitMap
3045 -100 after split brain, disconnect
3046 -1000 unrelated data
3047 -1091 requires proto 91
3048 -1096 requires proto 96
3049 */
3050
drbd_uuid_compare(struct drbd_peer_device * const peer_device,enum drbd_role const peer_role,int * rule_nr)3051 static int drbd_uuid_compare(struct drbd_peer_device *const peer_device,
3052 enum drbd_role const peer_role, int *rule_nr) __must_hold(local)
3053 {
3054 struct drbd_connection *const connection = peer_device->connection;
3055 struct drbd_device *device = peer_device->device;
3056 u64 self, peer;
3057 int i, j;
3058
3059 self = device->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
3060 peer = device->p_uuid[UI_CURRENT] & ~((u64)1);
3061
3062 *rule_nr = 10;
3063 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
3064 return 0;
3065
3066 *rule_nr = 20;
3067 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
3068 peer != UUID_JUST_CREATED)
3069 return -2;
3070
3071 *rule_nr = 30;
3072 if (self != UUID_JUST_CREATED &&
3073 (peer == UUID_JUST_CREATED || peer == (u64)0))
3074 return 2;
3075
3076 if (self == peer) {
3077 int rct, dc; /* roles at crash time */
3078
3079 if (device->p_uuid[UI_BITMAP] == (u64)0 && device->ldev->md.uuid[UI_BITMAP] != (u64)0) {
3080
3081 if (connection->agreed_pro_version < 91)
3082 return -1091;
3083
3084 if ((device->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (device->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
3085 (device->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (device->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
3086 drbd_info(device, "was SyncSource, missed the resync finished event, corrected myself:\n");
3087 drbd_uuid_move_history(device);
3088 device->ldev->md.uuid[UI_HISTORY_START] = device->ldev->md.uuid[UI_BITMAP];
3089 device->ldev->md.uuid[UI_BITMAP] = 0;
3090
3091 drbd_uuid_dump(device, "self", device->ldev->md.uuid,
3092 device->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(device) : 0, 0);
3093 *rule_nr = 34;
3094 } else {
3095 drbd_info(device, "was SyncSource (peer failed to write sync_uuid)\n");
3096 *rule_nr = 36;
3097 }
3098
3099 return 1;
3100 }
3101
3102 if (device->ldev->md.uuid[UI_BITMAP] == (u64)0 && device->p_uuid[UI_BITMAP] != (u64)0) {
3103
3104 if (connection->agreed_pro_version < 91)
3105 return -1091;
3106
3107 if ((device->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (device->p_uuid[UI_BITMAP] & ~((u64)1)) &&
3108 (device->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (device->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
3109 drbd_info(device, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
3110
3111 device->p_uuid[UI_HISTORY_START + 1] = device->p_uuid[UI_HISTORY_START];
3112 device->p_uuid[UI_HISTORY_START] = device->p_uuid[UI_BITMAP];
3113 device->p_uuid[UI_BITMAP] = 0UL;
3114
3115 drbd_uuid_dump(device, "peer", device->p_uuid, device->p_uuid[UI_SIZE], device->p_uuid[UI_FLAGS]);
3116 *rule_nr = 35;
3117 } else {
3118 drbd_info(device, "was SyncTarget (failed to write sync_uuid)\n");
3119 *rule_nr = 37;
3120 }
3121
3122 return -1;
3123 }
3124
3125 /* Common power [off|failure] */
3126 rct = (test_bit(CRASHED_PRIMARY, &device->flags) ? 1 : 0) +
3127 (device->p_uuid[UI_FLAGS] & 2);
3128 /* lowest bit is set when we were primary,
3129 * next bit (weight 2) is set when peer was primary */
3130 *rule_nr = 40;
3131
3132 /* Neither has the "crashed primary" flag set,
3133 * only a replication link hickup. */
3134 if (rct == 0)
3135 return 0;
3136
3137 /* Current UUID equal and no bitmap uuid; does not necessarily
3138 * mean this was a "simultaneous hard crash", maybe IO was
3139 * frozen, so no UUID-bump happened.
3140 * This is a protocol change, overload DRBD_FF_WSAME as flag
3141 * for "new-enough" peer DRBD version. */
3142 if (device->state.role == R_PRIMARY || peer_role == R_PRIMARY) {
3143 *rule_nr = 41;
3144 if (!(connection->agreed_features & DRBD_FF_WSAME)) {
3145 drbd_warn(peer_device, "Equivalent unrotated UUIDs, but current primary present.\n");
3146 return -(0x10000 | PRO_VERSION_MAX | (DRBD_FF_WSAME << 8));
3147 }
3148 if (device->state.role == R_PRIMARY && peer_role == R_PRIMARY) {
3149 /* At least one has the "crashed primary" bit set,
3150 * both are primary now, but neither has rotated its UUIDs?
3151 * "Can not happen." */
3152 drbd_err(peer_device, "Equivalent unrotated UUIDs, but both are primary. Can not resolve this.\n");
3153 return -100;
3154 }
3155 if (device->state.role == R_PRIMARY)
3156 return 1;
3157 return -1;
3158 }
3159
3160 /* Both are secondary.
3161 * Really looks like recovery from simultaneous hard crash.
3162 * Check which had been primary before, and arbitrate. */
3163 switch (rct) {
3164 case 0: /* !self_pri && !peer_pri */ return 0; /* already handled */
3165 case 1: /* self_pri && !peer_pri */ return 1;
3166 case 2: /* !self_pri && peer_pri */ return -1;
3167 case 3: /* self_pri && peer_pri */
3168 dc = test_bit(RESOLVE_CONFLICTS, &connection->flags);
3169 return dc ? -1 : 1;
3170 }
3171 }
3172
3173 *rule_nr = 50;
3174 peer = device->p_uuid[UI_BITMAP] & ~((u64)1);
3175 if (self == peer)
3176 return -1;
3177
3178 *rule_nr = 51;
3179 peer = device->p_uuid[UI_HISTORY_START] & ~((u64)1);
3180 if (self == peer) {
3181 if (connection->agreed_pro_version < 96 ?
3182 (device->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) ==
3183 (device->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) :
3184 peer + UUID_NEW_BM_OFFSET == (device->p_uuid[UI_BITMAP] & ~((u64)1))) {
3185 /* The last P_SYNC_UUID did not get though. Undo the last start of
3186 resync as sync source modifications of the peer's UUIDs. */
3187
3188 if (connection->agreed_pro_version < 91)
3189 return -1091;
3190
3191 device->p_uuid[UI_BITMAP] = device->p_uuid[UI_HISTORY_START];
3192 device->p_uuid[UI_HISTORY_START] = device->p_uuid[UI_HISTORY_START + 1];
3193
3194 drbd_info(device, "Lost last syncUUID packet, corrected:\n");
3195 drbd_uuid_dump(device, "peer", device->p_uuid, device->p_uuid[UI_SIZE], device->p_uuid[UI_FLAGS]);
3196
3197 return -1;
3198 }
3199 }
3200
3201 *rule_nr = 60;
3202 self = device->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
3203 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
3204 peer = device->p_uuid[i] & ~((u64)1);
3205 if (self == peer)
3206 return -2;
3207 }
3208
3209 *rule_nr = 70;
3210 self = device->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
3211 peer = device->p_uuid[UI_CURRENT] & ~((u64)1);
3212 if (self == peer)
3213 return 1;
3214
3215 *rule_nr = 71;
3216 self = device->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
3217 if (self == peer) {
3218 if (connection->agreed_pro_version < 96 ?
3219 (device->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) ==
3220 (device->p_uuid[UI_HISTORY_START] & ~((u64)1)) :
3221 self + UUID_NEW_BM_OFFSET == (device->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) {
3222 /* The last P_SYNC_UUID did not get though. Undo the last start of
3223 resync as sync source modifications of our UUIDs. */
3224
3225 if (connection->agreed_pro_version < 91)
3226 return -1091;
3227
3228 __drbd_uuid_set(device, UI_BITMAP, device->ldev->md.uuid[UI_HISTORY_START]);
3229 __drbd_uuid_set(device, UI_HISTORY_START, device->ldev->md.uuid[UI_HISTORY_START + 1]);
3230
3231 drbd_info(device, "Last syncUUID did not get through, corrected:\n");
3232 drbd_uuid_dump(device, "self", device->ldev->md.uuid,
3233 device->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(device) : 0, 0);
3234
3235 return 1;
3236 }
3237 }
3238
3239
3240 *rule_nr = 80;
3241 peer = device->p_uuid[UI_CURRENT] & ~((u64)1);
3242 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
3243 self = device->ldev->md.uuid[i] & ~((u64)1);
3244 if (self == peer)
3245 return 2;
3246 }
3247
3248 *rule_nr = 90;
3249 self = device->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
3250 peer = device->p_uuid[UI_BITMAP] & ~((u64)1);
3251 if (self == peer && self != ((u64)0))
3252 return 100;
3253
3254 *rule_nr = 100;
3255 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
3256 self = device->ldev->md.uuid[i] & ~((u64)1);
3257 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
3258 peer = device->p_uuid[j] & ~((u64)1);
3259 if (self == peer)
3260 return -100;
3261 }
3262 }
3263
3264 return -1000;
3265 }
3266
3267 /* drbd_sync_handshake() returns the new conn state on success, or
3268 CONN_MASK (-1) on failure.
3269 */
drbd_sync_handshake(struct drbd_peer_device * peer_device,enum drbd_role peer_role,enum drbd_disk_state peer_disk)3270 static enum drbd_conns drbd_sync_handshake(struct drbd_peer_device *peer_device,
3271 enum drbd_role peer_role,
3272 enum drbd_disk_state peer_disk) __must_hold(local)
3273 {
3274 struct drbd_device *device = peer_device->device;
3275 enum drbd_conns rv = C_MASK;
3276 enum drbd_disk_state mydisk;
3277 struct net_conf *nc;
3278 int hg, rule_nr, rr_conflict, tentative, always_asbp;
3279
3280 mydisk = device->state.disk;
3281 if (mydisk == D_NEGOTIATING)
3282 mydisk = device->new_state_tmp.disk;
3283
3284 drbd_info(device, "drbd_sync_handshake:\n");
3285
3286 spin_lock_irq(&device->ldev->md.uuid_lock);
3287 drbd_uuid_dump(device, "self", device->ldev->md.uuid, device->comm_bm_set, 0);
3288 drbd_uuid_dump(device, "peer", device->p_uuid,
3289 device->p_uuid[UI_SIZE], device->p_uuid[UI_FLAGS]);
3290
3291 hg = drbd_uuid_compare(peer_device, peer_role, &rule_nr);
3292 spin_unlock_irq(&device->ldev->md.uuid_lock);
3293
3294 drbd_info(device, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
3295
3296 if (hg == -1000) {
3297 drbd_alert(device, "Unrelated data, aborting!\n");
3298 return C_MASK;
3299 }
3300 if (hg < -0x10000) {
3301 int proto, fflags;
3302 hg = -hg;
3303 proto = hg & 0xff;
3304 fflags = (hg >> 8) & 0xff;
3305 drbd_alert(device, "To resolve this both sides have to support at least protocol %d and feature flags 0x%x\n",
3306 proto, fflags);
3307 return C_MASK;
3308 }
3309 if (hg < -1000) {
3310 drbd_alert(device, "To resolve this both sides have to support at least protocol %d\n", -hg - 1000);
3311 return C_MASK;
3312 }
3313
3314 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
3315 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
3316 int f = (hg == -100) || abs(hg) == 2;
3317 hg = mydisk > D_INCONSISTENT ? 1 : -1;
3318 if (f)
3319 hg = hg*2;
3320 drbd_info(device, "Becoming sync %s due to disk states.\n",
3321 hg > 0 ? "source" : "target");
3322 }
3323
3324 if (abs(hg) == 100)
3325 drbd_khelper(device, "initial-split-brain");
3326
3327 rcu_read_lock();
3328 nc = rcu_dereference(peer_device->connection->net_conf);
3329 always_asbp = nc->always_asbp;
3330 rr_conflict = nc->rr_conflict;
3331 tentative = nc->tentative;
3332 rcu_read_unlock();
3333
3334 if (hg == 100 || (hg == -100 && always_asbp)) {
3335 int pcount = (device->state.role == R_PRIMARY)
3336 + (peer_role == R_PRIMARY);
3337 int forced = (hg == -100);
3338
3339 switch (pcount) {
3340 case 0:
3341 hg = drbd_asb_recover_0p(peer_device);
3342 break;
3343 case 1:
3344 hg = drbd_asb_recover_1p(peer_device);
3345 break;
3346 case 2:
3347 hg = drbd_asb_recover_2p(peer_device);
3348 break;
3349 }
3350 if (abs(hg) < 100) {
3351 drbd_warn(device, "Split-Brain detected, %d primaries, "
3352 "automatically solved. Sync from %s node\n",
3353 pcount, (hg < 0) ? "peer" : "this");
3354 if (forced) {
3355 drbd_warn(device, "Doing a full sync, since"
3356 " UUIDs where ambiguous.\n");
3357 hg = hg*2;
3358 }
3359 }
3360 }
3361
3362 if (hg == -100) {
3363 if (test_bit(DISCARD_MY_DATA, &device->flags) && !(device->p_uuid[UI_FLAGS]&1))
3364 hg = -1;
3365 if (!test_bit(DISCARD_MY_DATA, &device->flags) && (device->p_uuid[UI_FLAGS]&1))
3366 hg = 1;
3367
3368 if (abs(hg) < 100)
3369 drbd_warn(device, "Split-Brain detected, manually solved. "
3370 "Sync from %s node\n",
3371 (hg < 0) ? "peer" : "this");
3372 }
3373
3374 if (hg == -100) {
3375 /* FIXME this log message is not correct if we end up here
3376 * after an attempted attach on a diskless node.
3377 * We just refuse to attach -- well, we drop the "connection"
3378 * to that disk, in a way... */
3379 drbd_alert(device, "Split-Brain detected but unresolved, dropping connection!\n");
3380 drbd_khelper(device, "split-brain");
3381 return C_MASK;
3382 }
3383
3384 if (hg > 0 && mydisk <= D_INCONSISTENT) {
3385 drbd_err(device, "I shall become SyncSource, but I am inconsistent!\n");
3386 return C_MASK;
3387 }
3388
3389 if (hg < 0 && /* by intention we do not use mydisk here. */
3390 device->state.role == R_PRIMARY && device->state.disk >= D_CONSISTENT) {
3391 switch (rr_conflict) {
3392 case ASB_CALL_HELPER:
3393 drbd_khelper(device, "pri-lost");
3394 fallthrough;
3395 case ASB_DISCONNECT:
3396 drbd_err(device, "I shall become SyncTarget, but I am primary!\n");
3397 return C_MASK;
3398 case ASB_VIOLENTLY:
3399 drbd_warn(device, "Becoming SyncTarget, violating the stable-data"
3400 "assumption\n");
3401 }
3402 }
3403
3404 if (tentative || test_bit(CONN_DRY_RUN, &peer_device->connection->flags)) {
3405 if (hg == 0)
3406 drbd_info(device, "dry-run connect: No resync, would become Connected immediately.\n");
3407 else
3408 drbd_info(device, "dry-run connect: Would become %s, doing a %s resync.",
3409 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
3410 abs(hg) >= 2 ? "full" : "bit-map based");
3411 return C_MASK;
3412 }
3413
3414 if (abs(hg) >= 2) {
3415 drbd_info(device, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
3416 if (drbd_bitmap_io(device, &drbd_bmio_set_n_write, "set_n_write from sync_handshake",
3417 BM_LOCKED_SET_ALLOWED, NULL))
3418 return C_MASK;
3419 }
3420
3421 if (hg > 0) { /* become sync source. */
3422 rv = C_WF_BITMAP_S;
3423 } else if (hg < 0) { /* become sync target */
3424 rv = C_WF_BITMAP_T;
3425 } else {
3426 rv = C_CONNECTED;
3427 if (drbd_bm_total_weight(device)) {
3428 drbd_info(device, "No resync, but %lu bits in bitmap!\n",
3429 drbd_bm_total_weight(device));
3430 }
3431 }
3432
3433 return rv;
3434 }
3435
convert_after_sb(enum drbd_after_sb_p peer)3436 static enum drbd_after_sb_p convert_after_sb(enum drbd_after_sb_p peer)
3437 {
3438 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
3439 if (peer == ASB_DISCARD_REMOTE)
3440 return ASB_DISCARD_LOCAL;
3441
3442 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
3443 if (peer == ASB_DISCARD_LOCAL)
3444 return ASB_DISCARD_REMOTE;
3445
3446 /* everything else is valid if they are equal on both sides. */
3447 return peer;
3448 }
3449
receive_protocol(struct drbd_connection * connection,struct packet_info * pi)3450 static int receive_protocol(struct drbd_connection *connection, struct packet_info *pi)
3451 {
3452 struct p_protocol *p = pi->data;
3453 enum drbd_after_sb_p p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
3454 int p_proto, p_discard_my_data, p_two_primaries, cf;
3455 struct net_conf *nc, *old_net_conf, *new_net_conf = NULL;
3456 char integrity_alg[SHARED_SECRET_MAX] = "";
3457 struct crypto_shash *peer_integrity_tfm = NULL;
3458 void *int_dig_in = NULL, *int_dig_vv = NULL;
3459
3460 p_proto = be32_to_cpu(p->protocol);
3461 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
3462 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
3463 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
3464 p_two_primaries = be32_to_cpu(p->two_primaries);
3465 cf = be32_to_cpu(p->conn_flags);
3466 p_discard_my_data = cf & CF_DISCARD_MY_DATA;
3467
3468 if (connection->agreed_pro_version >= 87) {
3469 int err;
3470
3471 if (pi->size > sizeof(integrity_alg))
3472 return -EIO;
3473 err = drbd_recv_all(connection, integrity_alg, pi->size);
3474 if (err)
3475 return err;
3476 integrity_alg[SHARED_SECRET_MAX - 1] = 0;
3477 }
3478
3479 if (pi->cmd != P_PROTOCOL_UPDATE) {
3480 clear_bit(CONN_DRY_RUN, &connection->flags);
3481
3482 if (cf & CF_DRY_RUN)
3483 set_bit(CONN_DRY_RUN, &connection->flags);
3484
3485 rcu_read_lock();
3486 nc = rcu_dereference(connection->net_conf);
3487
3488 if (p_proto != nc->wire_protocol) {
3489 drbd_err(connection, "incompatible %s settings\n", "protocol");
3490 goto disconnect_rcu_unlock;
3491 }
3492
3493 if (convert_after_sb(p_after_sb_0p) != nc->after_sb_0p) {
3494 drbd_err(connection, "incompatible %s settings\n", "after-sb-0pri");
3495 goto disconnect_rcu_unlock;
3496 }
3497
3498 if (convert_after_sb(p_after_sb_1p) != nc->after_sb_1p) {
3499 drbd_err(connection, "incompatible %s settings\n", "after-sb-1pri");
3500 goto disconnect_rcu_unlock;
3501 }
3502
3503 if (convert_after_sb(p_after_sb_2p) != nc->after_sb_2p) {
3504 drbd_err(connection, "incompatible %s settings\n", "after-sb-2pri");
3505 goto disconnect_rcu_unlock;
3506 }
3507
3508 if (p_discard_my_data && nc->discard_my_data) {
3509 drbd_err(connection, "incompatible %s settings\n", "discard-my-data");
3510 goto disconnect_rcu_unlock;
3511 }
3512
3513 if (p_two_primaries != nc->two_primaries) {
3514 drbd_err(connection, "incompatible %s settings\n", "allow-two-primaries");
3515 goto disconnect_rcu_unlock;
3516 }
3517
3518 if (strcmp(integrity_alg, nc->integrity_alg)) {
3519 drbd_err(connection, "incompatible %s settings\n", "data-integrity-alg");
3520 goto disconnect_rcu_unlock;
3521 }
3522
3523 rcu_read_unlock();
3524 }
3525
3526 if (integrity_alg[0]) {
3527 int hash_size;
3528
3529 /*
3530 * We can only change the peer data integrity algorithm
3531 * here. Changing our own data integrity algorithm
3532 * requires that we send a P_PROTOCOL_UPDATE packet at
3533 * the same time; otherwise, the peer has no way to
3534 * tell between which packets the algorithm should
3535 * change.
3536 */
3537
3538 peer_integrity_tfm = crypto_alloc_shash(integrity_alg, 0, 0);
3539 if (IS_ERR(peer_integrity_tfm)) {
3540 peer_integrity_tfm = NULL;
3541 drbd_err(connection, "peer data-integrity-alg %s not supported\n",
3542 integrity_alg);
3543 goto disconnect;
3544 }
3545
3546 hash_size = crypto_shash_digestsize(peer_integrity_tfm);
3547 int_dig_in = kmalloc(hash_size, GFP_KERNEL);
3548 int_dig_vv = kmalloc(hash_size, GFP_KERNEL);
3549 if (!(int_dig_in && int_dig_vv)) {
3550 drbd_err(connection, "Allocation of buffers for data integrity checking failed\n");
3551 goto disconnect;
3552 }
3553 }
3554
3555 new_net_conf = kmalloc_obj(struct net_conf);
3556 if (!new_net_conf)
3557 goto disconnect;
3558
3559 mutex_lock(&connection->data.mutex);
3560 mutex_lock(&connection->resource->conf_update);
3561 old_net_conf = connection->net_conf;
3562 *new_net_conf = *old_net_conf;
3563
3564 new_net_conf->wire_protocol = p_proto;
3565 new_net_conf->after_sb_0p = convert_after_sb(p_after_sb_0p);
3566 new_net_conf->after_sb_1p = convert_after_sb(p_after_sb_1p);
3567 new_net_conf->after_sb_2p = convert_after_sb(p_after_sb_2p);
3568 new_net_conf->two_primaries = p_two_primaries;
3569
3570 rcu_assign_pointer(connection->net_conf, new_net_conf);
3571 mutex_unlock(&connection->resource->conf_update);
3572 mutex_unlock(&connection->data.mutex);
3573
3574 crypto_free_shash(connection->peer_integrity_tfm);
3575 kfree(connection->int_dig_in);
3576 kfree(connection->int_dig_vv);
3577 connection->peer_integrity_tfm = peer_integrity_tfm;
3578 connection->int_dig_in = int_dig_in;
3579 connection->int_dig_vv = int_dig_vv;
3580
3581 if (strcmp(old_net_conf->integrity_alg, integrity_alg))
3582 drbd_info(connection, "peer data-integrity-alg: %s\n",
3583 integrity_alg[0] ? integrity_alg : "(none)");
3584
3585 kvfree_rcu_mightsleep(old_net_conf);
3586 return 0;
3587
3588 disconnect_rcu_unlock:
3589 rcu_read_unlock();
3590 disconnect:
3591 crypto_free_shash(peer_integrity_tfm);
3592 kfree(int_dig_in);
3593 kfree(int_dig_vv);
3594 conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD);
3595 return -EIO;
3596 }
3597
3598 /* helper function
3599 * input: alg name, feature name
3600 * return: NULL (alg name was "")
3601 * ERR_PTR(error) if something goes wrong
3602 * or the crypto hash ptr, if it worked out ok. */
drbd_crypto_alloc_digest_safe(const struct drbd_device * device,const char * alg,const char * name)3603 static struct crypto_shash *drbd_crypto_alloc_digest_safe(
3604 const struct drbd_device *device,
3605 const char *alg, const char *name)
3606 {
3607 struct crypto_shash *tfm;
3608
3609 if (!alg[0])
3610 return NULL;
3611
3612 tfm = crypto_alloc_shash(alg, 0, 0);
3613 if (IS_ERR(tfm)) {
3614 drbd_err(device, "Can not allocate \"%s\" as %s (reason: %ld)\n",
3615 alg, name, PTR_ERR(tfm));
3616 return tfm;
3617 }
3618 return tfm;
3619 }
3620
ignore_remaining_packet(struct drbd_connection * connection,struct packet_info * pi)3621 static int ignore_remaining_packet(struct drbd_connection *connection, struct packet_info *pi)
3622 {
3623 void *buffer = connection->data.rbuf;
3624 int size = pi->size;
3625
3626 while (size) {
3627 int s = min_t(int, size, DRBD_SOCKET_BUFFER_SIZE);
3628 s = drbd_recv(connection, buffer, s);
3629 if (s <= 0) {
3630 if (s < 0)
3631 return s;
3632 break;
3633 }
3634 size -= s;
3635 }
3636 if (size)
3637 return -EIO;
3638 return 0;
3639 }
3640
3641 /*
3642 * config_unknown_volume - device configuration command for unknown volume
3643 *
3644 * When a device is added to an existing connection, the node on which the
3645 * device is added first will send configuration commands to its peer but the
3646 * peer will not know about the device yet. It will warn and ignore these
3647 * commands. Once the device is added on the second node, the second node will
3648 * send the same device configuration commands, but in the other direction.
3649 *
3650 * (We can also end up here if drbd is misconfigured.)
3651 */
config_unknown_volume(struct drbd_connection * connection,struct packet_info * pi)3652 static int config_unknown_volume(struct drbd_connection *connection, struct packet_info *pi)
3653 {
3654 drbd_warn(connection, "%s packet received for volume %u, which is not configured locally\n",
3655 cmdname(pi->cmd), pi->vnr);
3656 return ignore_remaining_packet(connection, pi);
3657 }
3658
receive_SyncParam(struct drbd_connection * connection,struct packet_info * pi)3659 static int receive_SyncParam(struct drbd_connection *connection, struct packet_info *pi)
3660 {
3661 struct drbd_peer_device *peer_device;
3662 struct drbd_device *device;
3663 struct p_rs_param_95 *p;
3664 unsigned int header_size, data_size, exp_max_sz;
3665 struct crypto_shash *verify_tfm = NULL;
3666 struct crypto_shash *csums_tfm = NULL;
3667 struct net_conf *old_net_conf, *new_net_conf = NULL;
3668 struct disk_conf *old_disk_conf = NULL, *new_disk_conf = NULL;
3669 const int apv = connection->agreed_pro_version;
3670 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
3671 unsigned int fifo_size = 0;
3672 int err;
3673
3674 peer_device = conn_peer_device(connection, pi->vnr);
3675 if (!peer_device)
3676 return config_unknown_volume(connection, pi);
3677 device = peer_device->device;
3678
3679 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
3680 : apv == 88 ? sizeof(struct p_rs_param)
3681 + SHARED_SECRET_MAX
3682 : apv <= 94 ? sizeof(struct p_rs_param_89)
3683 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
3684
3685 if (pi->size > exp_max_sz) {
3686 drbd_err(device, "SyncParam packet too long: received %u, expected <= %u bytes\n",
3687 pi->size, exp_max_sz);
3688 return -EIO;
3689 }
3690
3691 if (apv <= 88) {
3692 header_size = sizeof(struct p_rs_param);
3693 data_size = pi->size - header_size;
3694 } else if (apv <= 94) {
3695 header_size = sizeof(struct p_rs_param_89);
3696 data_size = pi->size - header_size;
3697 D_ASSERT(device, data_size == 0);
3698 } else {
3699 header_size = sizeof(struct p_rs_param_95);
3700 data_size = pi->size - header_size;
3701 D_ASSERT(device, data_size == 0);
3702 }
3703
3704 /* initialize verify_alg and csums_alg */
3705 p = pi->data;
3706 BUILD_BUG_ON(sizeof(p->algs) != 2 * SHARED_SECRET_MAX);
3707 memset(&p->algs, 0, sizeof(p->algs));
3708
3709 err = drbd_recv_all(peer_device->connection, p, header_size);
3710 if (err)
3711 return err;
3712
3713 mutex_lock(&connection->resource->conf_update);
3714 old_net_conf = peer_device->connection->net_conf;
3715 if (get_ldev(device)) {
3716 new_disk_conf = kzalloc_obj(struct disk_conf);
3717 if (!new_disk_conf) {
3718 put_ldev(device);
3719 mutex_unlock(&connection->resource->conf_update);
3720 drbd_err(device, "Allocation of new disk_conf failed\n");
3721 return -ENOMEM;
3722 }
3723
3724 old_disk_conf = device->ldev->disk_conf;
3725 *new_disk_conf = *old_disk_conf;
3726
3727 new_disk_conf->resync_rate = be32_to_cpu(p->resync_rate);
3728 }
3729
3730 if (apv >= 88) {
3731 if (apv == 88) {
3732 if (data_size > SHARED_SECRET_MAX || data_size == 0) {
3733 drbd_err(device, "verify-alg of wrong size, "
3734 "peer wants %u, accepting only up to %u byte\n",
3735 data_size, SHARED_SECRET_MAX);
3736 goto reconnect;
3737 }
3738
3739 err = drbd_recv_all(peer_device->connection, p->verify_alg, data_size);
3740 if (err)
3741 goto reconnect;
3742 /* we expect NUL terminated string */
3743 /* but just in case someone tries to be evil */
3744 D_ASSERT(device, p->verify_alg[data_size-1] == 0);
3745 p->verify_alg[data_size-1] = 0;
3746
3747 } else /* apv >= 89 */ {
3748 /* we still expect NUL terminated strings */
3749 /* but just in case someone tries to be evil */
3750 D_ASSERT(device, p->verify_alg[SHARED_SECRET_MAX-1] == 0);
3751 D_ASSERT(device, p->csums_alg[SHARED_SECRET_MAX-1] == 0);
3752 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
3753 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
3754 }
3755
3756 if (strcmp(old_net_conf->verify_alg, p->verify_alg)) {
3757 if (device->state.conn == C_WF_REPORT_PARAMS) {
3758 drbd_err(device, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
3759 old_net_conf->verify_alg, p->verify_alg);
3760 goto disconnect;
3761 }
3762 verify_tfm = drbd_crypto_alloc_digest_safe(device,
3763 p->verify_alg, "verify-alg");
3764 if (IS_ERR(verify_tfm)) {
3765 verify_tfm = NULL;
3766 goto disconnect;
3767 }
3768 }
3769
3770 if (apv >= 89 && strcmp(old_net_conf->csums_alg, p->csums_alg)) {
3771 if (device->state.conn == C_WF_REPORT_PARAMS) {
3772 drbd_err(device, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
3773 old_net_conf->csums_alg, p->csums_alg);
3774 goto disconnect;
3775 }
3776 csums_tfm = drbd_crypto_alloc_digest_safe(device,
3777 p->csums_alg, "csums-alg");
3778 if (IS_ERR(csums_tfm)) {
3779 csums_tfm = NULL;
3780 goto disconnect;
3781 }
3782 }
3783
3784 if (apv > 94 && new_disk_conf) {
3785 new_disk_conf->c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
3786 new_disk_conf->c_delay_target = be32_to_cpu(p->c_delay_target);
3787 new_disk_conf->c_fill_target = be32_to_cpu(p->c_fill_target);
3788 new_disk_conf->c_max_rate = be32_to_cpu(p->c_max_rate);
3789
3790 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
3791 if (fifo_size != device->rs_plan_s->size) {
3792 new_plan = fifo_alloc(fifo_size);
3793 if (!new_plan) {
3794 drbd_err(device, "kmalloc of fifo_buffer failed");
3795 put_ldev(device);
3796 goto disconnect;
3797 }
3798 }
3799 }
3800
3801 if (verify_tfm || csums_tfm) {
3802 new_net_conf = kzalloc_obj(struct net_conf);
3803 if (!new_net_conf)
3804 goto disconnect;
3805
3806 *new_net_conf = *old_net_conf;
3807
3808 if (verify_tfm) {
3809 strscpy(new_net_conf->verify_alg, p->verify_alg);
3810 new_net_conf->verify_alg_len = strlen(p->verify_alg) + 1;
3811 crypto_free_shash(peer_device->connection->verify_tfm);
3812 peer_device->connection->verify_tfm = verify_tfm;
3813 drbd_info(device, "using verify-alg: \"%s\"\n", p->verify_alg);
3814 }
3815 if (csums_tfm) {
3816 strscpy(new_net_conf->csums_alg, p->csums_alg);
3817 new_net_conf->csums_alg_len = strlen(p->csums_alg) + 1;
3818 crypto_free_shash(peer_device->connection->csums_tfm);
3819 peer_device->connection->csums_tfm = csums_tfm;
3820 drbd_info(device, "using csums-alg: \"%s\"\n", p->csums_alg);
3821 }
3822 rcu_assign_pointer(connection->net_conf, new_net_conf);
3823 }
3824 }
3825
3826 if (new_disk_conf) {
3827 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
3828 put_ldev(device);
3829 }
3830
3831 if (new_plan) {
3832 old_plan = device->rs_plan_s;
3833 rcu_assign_pointer(device->rs_plan_s, new_plan);
3834 }
3835
3836 mutex_unlock(&connection->resource->conf_update);
3837 synchronize_rcu();
3838 if (new_net_conf)
3839 kfree(old_net_conf);
3840 kfree(old_disk_conf);
3841 kfree(old_plan);
3842
3843 return 0;
3844
3845 reconnect:
3846 if (new_disk_conf) {
3847 put_ldev(device);
3848 kfree(new_disk_conf);
3849 }
3850 mutex_unlock(&connection->resource->conf_update);
3851 return -EIO;
3852
3853 disconnect:
3854 kfree(new_plan);
3855 if (new_disk_conf) {
3856 put_ldev(device);
3857 kfree(new_disk_conf);
3858 }
3859 mutex_unlock(&connection->resource->conf_update);
3860 /* just for completeness: actually not needed,
3861 * as this is not reached if csums_tfm was ok. */
3862 crypto_free_shash(csums_tfm);
3863 /* but free the verify_tfm again, if csums_tfm did not work out */
3864 crypto_free_shash(verify_tfm);
3865 conn_request_state(peer_device->connection, NS(conn, C_DISCONNECTING), CS_HARD);
3866 return -EIO;
3867 }
3868
3869 /* warn if the arguments differ by more than 12.5% */
warn_if_differ_considerably(struct drbd_device * device,const char * s,sector_t a,sector_t b)3870 static void warn_if_differ_considerably(struct drbd_device *device,
3871 const char *s, sector_t a, sector_t b)
3872 {
3873 sector_t d;
3874 if (a == 0 || b == 0)
3875 return;
3876 d = (a > b) ? (a - b) : (b - a);
3877 if (d > (a>>3) || d > (b>>3))
3878 drbd_warn(device, "Considerable difference in %s: %llus vs. %llus\n", s,
3879 (unsigned long long)a, (unsigned long long)b);
3880 }
3881
receive_sizes(struct drbd_connection * connection,struct packet_info * pi)3882 static int receive_sizes(struct drbd_connection *connection, struct packet_info *pi)
3883 {
3884 struct drbd_peer_device *peer_device;
3885 struct drbd_device *device;
3886 struct p_sizes *p = pi->data;
3887 struct o_qlim *o = (connection->agreed_features & DRBD_FF_WSAME) ? p->qlim : NULL;
3888 enum determine_dev_size dd = DS_UNCHANGED;
3889 sector_t p_size, p_usize, p_csize, my_usize;
3890 sector_t new_size, cur_size;
3891 int ldsc = 0; /* local disk size changed */
3892 enum dds_flags ddsf;
3893
3894 peer_device = conn_peer_device(connection, pi->vnr);
3895 if (!peer_device)
3896 return config_unknown_volume(connection, pi);
3897 device = peer_device->device;
3898 cur_size = get_capacity(device->vdisk);
3899
3900 p_size = be64_to_cpu(p->d_size);
3901 p_usize = be64_to_cpu(p->u_size);
3902 p_csize = be64_to_cpu(p->c_size);
3903
3904 /* just store the peer's disk size for now.
3905 * we still need to figure out whether we accept that. */
3906 device->p_size = p_size;
3907
3908 if (get_ldev(device)) {
3909 rcu_read_lock();
3910 my_usize = rcu_dereference(device->ldev->disk_conf)->disk_size;
3911 rcu_read_unlock();
3912
3913 warn_if_differ_considerably(device, "lower level device sizes",
3914 p_size, drbd_get_max_capacity(device->ldev));
3915 warn_if_differ_considerably(device, "user requested size",
3916 p_usize, my_usize);
3917
3918 /* if this is the first connect, or an otherwise expected
3919 * param exchange, choose the minimum */
3920 if (device->state.conn == C_WF_REPORT_PARAMS)
3921 p_usize = min_not_zero(my_usize, p_usize);
3922
3923 /* Never shrink a device with usable data during connect,
3924 * or "attach" on the peer.
3925 * But allow online shrinking if we are connected. */
3926 new_size = drbd_new_dev_size(device, device->ldev, p_usize, 0);
3927 if (new_size < cur_size &&
3928 device->state.disk >= D_OUTDATED &&
3929 (device->state.conn < C_CONNECTED || device->state.pdsk == D_DISKLESS)) {
3930 drbd_err(device, "The peer's disk size is too small! (%llu < %llu sectors)\n",
3931 (unsigned long long)new_size, (unsigned long long)cur_size);
3932 conn_request_state(peer_device->connection, NS(conn, C_DISCONNECTING), CS_HARD);
3933 put_ldev(device);
3934 return -EIO;
3935 }
3936
3937 if (my_usize != p_usize) {
3938 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
3939
3940 new_disk_conf = kzalloc_obj(struct disk_conf);
3941 if (!new_disk_conf) {
3942 put_ldev(device);
3943 return -ENOMEM;
3944 }
3945
3946 mutex_lock(&connection->resource->conf_update);
3947 old_disk_conf = device->ldev->disk_conf;
3948 *new_disk_conf = *old_disk_conf;
3949 new_disk_conf->disk_size = p_usize;
3950
3951 rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
3952 mutex_unlock(&connection->resource->conf_update);
3953 kvfree_rcu_mightsleep(old_disk_conf);
3954
3955 drbd_info(device, "Peer sets u_size to %lu sectors (old: %lu)\n",
3956 (unsigned long)p_usize, (unsigned long)my_usize);
3957 }
3958
3959 put_ldev(device);
3960 }
3961
3962 device->peer_max_bio_size = be32_to_cpu(p->max_bio_size);
3963 /* Leave drbd_reconsider_queue_parameters() before drbd_determine_dev_size().
3964 In case we cleared the QUEUE_FLAG_DISCARD from our queue in
3965 drbd_reconsider_queue_parameters(), we can be sure that after
3966 drbd_determine_dev_size() no REQ_DISCARDs are in the queue. */
3967
3968 ddsf = be16_to_cpu(p->dds_flags);
3969 if (get_ldev(device)) {
3970 drbd_reconsider_queue_parameters(device, device->ldev, o);
3971 dd = drbd_determine_dev_size(device, ddsf, NULL);
3972 put_ldev(device);
3973 if (dd == DS_ERROR)
3974 return -EIO;
3975 drbd_md_sync(device);
3976 } else {
3977 /*
3978 * I am diskless, need to accept the peer's *current* size.
3979 * I must NOT accept the peers backing disk size,
3980 * it may have been larger than mine all along...
3981 *
3982 * At this point, the peer knows more about my disk, or at
3983 * least about what we last agreed upon, than myself.
3984 * So if his c_size is less than his d_size, the most likely
3985 * reason is that *my* d_size was smaller last time we checked.
3986 *
3987 * However, if he sends a zero current size,
3988 * take his (user-capped or) backing disk size anyways.
3989 *
3990 * Unless of course he does not have a disk himself.
3991 * In which case we ignore this completely.
3992 */
3993 sector_t new_size = p_csize ?: p_usize ?: p_size;
3994 drbd_reconsider_queue_parameters(device, NULL, o);
3995 if (new_size == 0) {
3996 /* Ignore, peer does not know nothing. */
3997 } else if (new_size == cur_size) {
3998 /* nothing to do */
3999 } else if (cur_size != 0 && p_size == 0) {
4000 drbd_warn(device, "Ignored diskless peer device size (peer:%llu != me:%llu sectors)!\n",
4001 (unsigned long long)new_size, (unsigned long long)cur_size);
4002 } else if (new_size < cur_size && device->state.role == R_PRIMARY) {
4003 drbd_err(device, "The peer's device size is too small! (%llu < %llu sectors); demote me first!\n",
4004 (unsigned long long)new_size, (unsigned long long)cur_size);
4005 conn_request_state(peer_device->connection, NS(conn, C_DISCONNECTING), CS_HARD);
4006 return -EIO;
4007 } else {
4008 /* I believe the peer, if
4009 * - I don't have a current size myself
4010 * - we agree on the size anyways
4011 * - I do have a current size, am Secondary,
4012 * and he has the only disk
4013 * - I do have a current size, am Primary,
4014 * and he has the only disk,
4015 * which is larger than my current size
4016 */
4017 drbd_set_my_capacity(device, new_size);
4018 }
4019 }
4020
4021 if (get_ldev(device)) {
4022 if (device->ldev->known_size != drbd_get_capacity(device->ldev->backing_bdev)) {
4023 device->ldev->known_size = drbd_get_capacity(device->ldev->backing_bdev);
4024 ldsc = 1;
4025 }
4026
4027 put_ldev(device);
4028 }
4029
4030 if (device->state.conn > C_WF_REPORT_PARAMS) {
4031 if (be64_to_cpu(p->c_size) != get_capacity(device->vdisk) ||
4032 ldsc) {
4033 /* we have different sizes, probably peer
4034 * needs to know my new size... */
4035 drbd_send_sizes(peer_device, 0, ddsf);
4036 }
4037 if (test_and_clear_bit(RESIZE_PENDING, &device->flags) ||
4038 (dd == DS_GREW && device->state.conn == C_CONNECTED)) {
4039 if (device->state.pdsk >= D_INCONSISTENT &&
4040 device->state.disk >= D_INCONSISTENT) {
4041 if (ddsf & DDSF_NO_RESYNC)
4042 drbd_info(device, "Resync of new storage suppressed with --assume-clean\n");
4043 else
4044 resync_after_online_grow(device);
4045 } else
4046 set_bit(RESYNC_AFTER_NEG, &device->flags);
4047 }
4048 }
4049
4050 return 0;
4051 }
4052
receive_uuids(struct drbd_connection * connection,struct packet_info * pi)4053 static int receive_uuids(struct drbd_connection *connection, struct packet_info *pi)
4054 {
4055 struct drbd_peer_device *peer_device;
4056 struct drbd_device *device;
4057 struct p_uuids *p = pi->data;
4058 u64 *p_uuid;
4059 int i, updated_uuids = 0;
4060
4061 peer_device = conn_peer_device(connection, pi->vnr);
4062 if (!peer_device)
4063 return config_unknown_volume(connection, pi);
4064 device = peer_device->device;
4065
4066 p_uuid = kmalloc_array(UI_EXTENDED_SIZE, sizeof(*p_uuid), GFP_NOIO);
4067 if (!p_uuid)
4068 return false;
4069
4070 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
4071 p_uuid[i] = be64_to_cpu(p->uuid[i]);
4072
4073 kfree(device->p_uuid);
4074 device->p_uuid = p_uuid;
4075
4076 if ((device->state.conn < C_CONNECTED || device->state.pdsk == D_DISKLESS) &&
4077 device->state.disk < D_INCONSISTENT &&
4078 device->state.role == R_PRIMARY &&
4079 (device->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
4080 drbd_err(device, "Can only connect to data with current UUID=%016llX\n",
4081 (unsigned long long)device->ed_uuid);
4082 conn_request_state(peer_device->connection, NS(conn, C_DISCONNECTING), CS_HARD);
4083 return -EIO;
4084 }
4085
4086 if (get_ldev(device)) {
4087 int skip_initial_sync =
4088 device->state.conn == C_CONNECTED &&
4089 peer_device->connection->agreed_pro_version >= 90 &&
4090 device->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
4091 (p_uuid[UI_FLAGS] & 8);
4092 if (skip_initial_sync) {
4093 drbd_info(device, "Accepted new current UUID, preparing to skip initial sync\n");
4094 drbd_bitmap_io(device, &drbd_bmio_clear_n_write,
4095 "clear_n_write from receive_uuids",
4096 BM_LOCKED_TEST_ALLOWED, NULL);
4097 _drbd_uuid_set(device, UI_CURRENT, p_uuid[UI_CURRENT]);
4098 _drbd_uuid_set(device, UI_BITMAP, 0);
4099 _drbd_set_state(_NS2(device, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
4100 CS_VERBOSE, NULL);
4101 drbd_md_sync(device);
4102 updated_uuids = 1;
4103 }
4104 put_ldev(device);
4105 } else if (device->state.disk < D_INCONSISTENT &&
4106 device->state.role == R_PRIMARY) {
4107 /* I am a diskless primary, the peer just created a new current UUID
4108 for me. */
4109 updated_uuids = drbd_set_ed_uuid(device, p_uuid[UI_CURRENT]);
4110 }
4111
4112 /* Before we test for the disk state, we should wait until an eventually
4113 ongoing cluster wide state change is finished. That is important if
4114 we are primary and are detaching from our disk. We need to see the
4115 new disk state... */
4116 mutex_lock(device->state_mutex);
4117 mutex_unlock(device->state_mutex);
4118 if (device->state.conn >= C_CONNECTED && device->state.disk < D_INCONSISTENT)
4119 updated_uuids |= drbd_set_ed_uuid(device, p_uuid[UI_CURRENT]);
4120
4121 if (updated_uuids)
4122 drbd_print_uuids(device, "receiver updated UUIDs to");
4123
4124 return 0;
4125 }
4126
4127 /**
4128 * convert_state() - Converts the peer's view of the cluster state to our point of view
4129 * @ps: The state as seen by the peer.
4130 */
convert_state(union drbd_state ps)4131 static union drbd_state convert_state(union drbd_state ps)
4132 {
4133 union drbd_state ms;
4134
4135 static enum drbd_conns c_tab[] = {
4136 [C_WF_REPORT_PARAMS] = C_WF_REPORT_PARAMS,
4137 [C_CONNECTED] = C_CONNECTED,
4138
4139 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
4140 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
4141 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
4142 [C_VERIFY_S] = C_VERIFY_T,
4143 [C_MASK] = C_MASK,
4144 };
4145
4146 ms.i = ps.i;
4147
4148 ms.conn = c_tab[ps.conn];
4149 ms.peer = ps.role;
4150 ms.role = ps.peer;
4151 ms.pdsk = ps.disk;
4152 ms.disk = ps.pdsk;
4153 ms.peer_isp = (ps.aftr_isp | ps.user_isp);
4154
4155 return ms;
4156 }
4157
receive_req_state(struct drbd_connection * connection,struct packet_info * pi)4158 static int receive_req_state(struct drbd_connection *connection, struct packet_info *pi)
4159 {
4160 struct drbd_peer_device *peer_device;
4161 struct drbd_device *device;
4162 struct p_req_state *p = pi->data;
4163 union drbd_state mask, val;
4164 enum drbd_state_rv rv;
4165
4166 peer_device = conn_peer_device(connection, pi->vnr);
4167 if (!peer_device)
4168 return -EIO;
4169 device = peer_device->device;
4170
4171 mask.i = be32_to_cpu(p->mask);
4172 val.i = be32_to_cpu(p->val);
4173
4174 if (test_bit(RESOLVE_CONFLICTS, &peer_device->connection->flags) &&
4175 mutex_is_locked(device->state_mutex)) {
4176 drbd_send_sr_reply(peer_device, SS_CONCURRENT_ST_CHG);
4177 return 0;
4178 }
4179
4180 mask = convert_state(mask);
4181 val = convert_state(val);
4182
4183 rv = drbd_change_state(device, CS_VERBOSE, mask, val);
4184 drbd_send_sr_reply(peer_device, rv);
4185
4186 drbd_md_sync(device);
4187
4188 return 0;
4189 }
4190
receive_req_conn_state(struct drbd_connection * connection,struct packet_info * pi)4191 static int receive_req_conn_state(struct drbd_connection *connection, struct packet_info *pi)
4192 {
4193 struct p_req_state *p = pi->data;
4194 union drbd_state mask, val;
4195 enum drbd_state_rv rv;
4196
4197 mask.i = be32_to_cpu(p->mask);
4198 val.i = be32_to_cpu(p->val);
4199
4200 if (test_bit(RESOLVE_CONFLICTS, &connection->flags) &&
4201 mutex_is_locked(&connection->cstate_mutex)) {
4202 conn_send_sr_reply(connection, SS_CONCURRENT_ST_CHG);
4203 return 0;
4204 }
4205
4206 mask = convert_state(mask);
4207 val = convert_state(val);
4208
4209 rv = conn_request_state(connection, mask, val, CS_VERBOSE | CS_LOCAL_ONLY | CS_IGN_OUTD_FAIL);
4210 conn_send_sr_reply(connection, rv);
4211
4212 return 0;
4213 }
4214
receive_state(struct drbd_connection * connection,struct packet_info * pi)4215 static int receive_state(struct drbd_connection *connection, struct packet_info *pi)
4216 {
4217 struct drbd_peer_device *peer_device;
4218 struct drbd_device *device;
4219 struct p_state *p = pi->data;
4220 union drbd_state os, ns, peer_state;
4221 enum drbd_disk_state real_peer_disk;
4222 enum chg_state_flags cs_flags;
4223 int rv;
4224
4225 peer_device = conn_peer_device(connection, pi->vnr);
4226 if (!peer_device)
4227 return config_unknown_volume(connection, pi);
4228 device = peer_device->device;
4229
4230 peer_state.i = be32_to_cpu(p->state);
4231
4232 real_peer_disk = peer_state.disk;
4233 if (peer_state.disk == D_NEGOTIATING) {
4234 real_peer_disk = device->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
4235 drbd_info(device, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
4236 }
4237
4238 spin_lock_irq(&device->resource->req_lock);
4239 retry:
4240 os = ns = drbd_read_state(device);
4241 spin_unlock_irq(&device->resource->req_lock);
4242
4243 /* If some other part of the code (ack_receiver thread, timeout)
4244 * already decided to close the connection again,
4245 * we must not "re-establish" it here. */
4246 if (os.conn <= C_TEAR_DOWN)
4247 return -ECONNRESET;
4248
4249 /* If this is the "end of sync" confirmation, usually the peer disk
4250 * transitions from D_INCONSISTENT to D_UP_TO_DATE. For empty (0 bits
4251 * set) resync started in PausedSyncT, or if the timing of pause-/
4252 * unpause-sync events has been "just right", the peer disk may
4253 * transition from D_CONSISTENT to D_UP_TO_DATE as well.
4254 */
4255 if ((os.pdsk == D_INCONSISTENT || os.pdsk == D_CONSISTENT) &&
4256 real_peer_disk == D_UP_TO_DATE &&
4257 os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) {
4258 /* If we are (becoming) SyncSource, but peer is still in sync
4259 * preparation, ignore its uptodate-ness to avoid flapping, it
4260 * will change to inconsistent once the peer reaches active
4261 * syncing states.
4262 * It may have changed syncer-paused flags, however, so we
4263 * cannot ignore this completely. */
4264 if (peer_state.conn > C_CONNECTED &&
4265 peer_state.conn < C_SYNC_SOURCE)
4266 real_peer_disk = D_INCONSISTENT;
4267
4268 /* if peer_state changes to connected at the same time,
4269 * it explicitly notifies us that it finished resync.
4270 * Maybe we should finish it up, too? */
4271 else if (os.conn >= C_SYNC_SOURCE &&
4272 peer_state.conn == C_CONNECTED) {
4273 if (drbd_bm_total_weight(device) <= device->rs_failed)
4274 drbd_resync_finished(peer_device);
4275 return 0;
4276 }
4277 }
4278
4279 /* explicit verify finished notification, stop sector reached. */
4280 if (os.conn == C_VERIFY_T && os.disk == D_UP_TO_DATE &&
4281 peer_state.conn == C_CONNECTED && real_peer_disk == D_UP_TO_DATE) {
4282 ov_out_of_sync_print(peer_device);
4283 drbd_resync_finished(peer_device);
4284 return 0;
4285 }
4286
4287 /* peer says his disk is inconsistent, while we think it is uptodate,
4288 * and this happens while the peer still thinks we have a sync going on,
4289 * but we think we are already done with the sync.
4290 * We ignore this to avoid flapping pdsk.
4291 * This should not happen, if the peer is a recent version of drbd. */
4292 if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT &&
4293 os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE)
4294 real_peer_disk = D_UP_TO_DATE;
4295
4296 if (ns.conn == C_WF_REPORT_PARAMS)
4297 ns.conn = C_CONNECTED;
4298
4299 if (peer_state.conn == C_AHEAD)
4300 ns.conn = C_BEHIND;
4301
4302 /* TODO:
4303 * if (primary and diskless and peer uuid != effective uuid)
4304 * abort attach on peer;
4305 *
4306 * If this node does not have good data, was already connected, but
4307 * the peer did a late attach only now, trying to "negotiate" with me,
4308 * AND I am currently Primary, possibly frozen, with some specific
4309 * "effective" uuid, this should never be reached, really, because
4310 * we first send the uuids, then the current state.
4311 *
4312 * In this scenario, we already dropped the connection hard
4313 * when we received the unsuitable uuids (receive_uuids().
4314 *
4315 * Should we want to change this, that is: not drop the connection in
4316 * receive_uuids() already, then we would need to add a branch here
4317 * that aborts the attach of "unsuitable uuids" on the peer in case
4318 * this node is currently Diskless Primary.
4319 */
4320
4321 if (device->p_uuid && peer_state.disk >= D_NEGOTIATING &&
4322 get_ldev_if_state(device, D_NEGOTIATING)) {
4323 int cr; /* consider resync */
4324
4325 /* if we established a new connection */
4326 cr = (os.conn < C_CONNECTED);
4327 /* if we had an established connection
4328 * and one of the nodes newly attaches a disk */
4329 cr |= (os.conn == C_CONNECTED &&
4330 (peer_state.disk == D_NEGOTIATING ||
4331 os.disk == D_NEGOTIATING));
4332 /* if we have both been inconsistent, and the peer has been
4333 * forced to be UpToDate with --force */
4334 cr |= test_bit(CONSIDER_RESYNC, &device->flags);
4335 /* if we had been plain connected, and the admin requested to
4336 * start a sync by "invalidate" or "invalidate-remote" */
4337 cr |= (os.conn == C_CONNECTED &&
4338 (peer_state.conn >= C_STARTING_SYNC_S &&
4339 peer_state.conn <= C_WF_BITMAP_T));
4340
4341 if (cr)
4342 ns.conn = drbd_sync_handshake(peer_device, peer_state.role, real_peer_disk);
4343
4344 put_ldev(device);
4345 if (ns.conn == C_MASK) {
4346 ns.conn = C_CONNECTED;
4347 if (device->state.disk == D_NEGOTIATING) {
4348 drbd_force_state(device, NS(disk, D_FAILED));
4349 } else if (peer_state.disk == D_NEGOTIATING) {
4350 drbd_err(device, "Disk attach process on the peer node was aborted.\n");
4351 peer_state.disk = D_DISKLESS;
4352 real_peer_disk = D_DISKLESS;
4353 } else {
4354 if (test_and_clear_bit(CONN_DRY_RUN, &peer_device->connection->flags))
4355 return -EIO;
4356 D_ASSERT(device, os.conn == C_WF_REPORT_PARAMS);
4357 conn_request_state(peer_device->connection, NS(conn, C_DISCONNECTING), CS_HARD);
4358 return -EIO;
4359 }
4360 }
4361 }
4362
4363 spin_lock_irq(&device->resource->req_lock);
4364 if (os.i != drbd_read_state(device).i)
4365 goto retry;
4366 clear_bit(CONSIDER_RESYNC, &device->flags);
4367 ns.peer = peer_state.role;
4368 ns.pdsk = real_peer_disk;
4369 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
4370 if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
4371 ns.disk = device->new_state_tmp.disk;
4372 cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD);
4373 if (ns.pdsk == D_CONSISTENT && drbd_suspended(device) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED &&
4374 test_bit(NEW_CUR_UUID, &device->flags)) {
4375 /* Do not allow tl_restart(RESEND) for a rebooted peer. We can only allow this
4376 for temporal network outages! */
4377 spin_unlock_irq(&device->resource->req_lock);
4378 drbd_err(device, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
4379 tl_clear(peer_device->connection);
4380 drbd_uuid_new_current(device);
4381 clear_bit(NEW_CUR_UUID, &device->flags);
4382 conn_request_state(peer_device->connection, NS2(conn, C_PROTOCOL_ERROR, susp, 0), CS_HARD);
4383 return -EIO;
4384 }
4385 rv = _drbd_set_state(device, ns, cs_flags, NULL);
4386 ns = drbd_read_state(device);
4387 spin_unlock_irq(&device->resource->req_lock);
4388
4389 if (rv < SS_SUCCESS) {
4390 conn_request_state(peer_device->connection, NS(conn, C_DISCONNECTING), CS_HARD);
4391 return -EIO;
4392 }
4393
4394 if (os.conn > C_WF_REPORT_PARAMS) {
4395 if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
4396 peer_state.disk != D_NEGOTIATING ) {
4397 /* we want resync, peer has not yet decided to sync... */
4398 /* Nowadays only used when forcing a node into primary role and
4399 setting its disk to UpToDate with that */
4400 drbd_send_uuids(peer_device);
4401 drbd_send_current_state(peer_device);
4402 }
4403 }
4404
4405 clear_bit(DISCARD_MY_DATA, &device->flags);
4406
4407 drbd_md_sync(device); /* update connected indicator, la_size_sect, ... */
4408
4409 return 0;
4410 }
4411
receive_sync_uuid(struct drbd_connection * connection,struct packet_info * pi)4412 static int receive_sync_uuid(struct drbd_connection *connection, struct packet_info *pi)
4413 {
4414 struct drbd_peer_device *peer_device;
4415 struct drbd_device *device;
4416 struct p_rs_uuid *p = pi->data;
4417
4418 peer_device = conn_peer_device(connection, pi->vnr);
4419 if (!peer_device)
4420 return -EIO;
4421 device = peer_device->device;
4422
4423 wait_event(device->misc_wait,
4424 device->state.conn == C_WF_SYNC_UUID ||
4425 device->state.conn == C_BEHIND ||
4426 device->state.conn < C_CONNECTED ||
4427 device->state.disk < D_NEGOTIATING);
4428
4429 /* D_ASSERT(device, device->state.conn == C_WF_SYNC_UUID ); */
4430
4431 /* Here the _drbd_uuid_ functions are right, current should
4432 _not_ be rotated into the history */
4433 if (get_ldev_if_state(device, D_NEGOTIATING)) {
4434 _drbd_uuid_set(device, UI_CURRENT, be64_to_cpu(p->uuid));
4435 _drbd_uuid_set(device, UI_BITMAP, 0UL);
4436
4437 drbd_print_uuids(device, "updated sync uuid");
4438 drbd_start_resync(device, C_SYNC_TARGET);
4439
4440 put_ldev(device);
4441 } else
4442 drbd_err(device, "Ignoring SyncUUID packet!\n");
4443
4444 return 0;
4445 }
4446
4447 /*
4448 * receive_bitmap_plain
4449 *
4450 * Return 0 when done, 1 when another iteration is needed, and a negative error
4451 * code upon failure.
4452 */
4453 static int
receive_bitmap_plain(struct drbd_peer_device * peer_device,unsigned int size,unsigned long * p,struct bm_xfer_ctx * c)4454 receive_bitmap_plain(struct drbd_peer_device *peer_device, unsigned int size,
4455 unsigned long *p, struct bm_xfer_ctx *c)
4456 {
4457 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE -
4458 drbd_header_size(peer_device->connection);
4459 unsigned int num_words = min_t(size_t, data_size / sizeof(*p),
4460 c->bm_words - c->word_offset);
4461 unsigned int want = num_words * sizeof(*p);
4462 int err;
4463
4464 if (want != size) {
4465 drbd_err(peer_device, "%s:want (%u) != size (%u)\n", __func__, want, size);
4466 return -EIO;
4467 }
4468 if (want == 0)
4469 return 0;
4470 err = drbd_recv_all(peer_device->connection, p, want);
4471 if (err)
4472 return err;
4473
4474 drbd_bm_merge_lel(peer_device->device, c->word_offset, num_words, p);
4475
4476 c->word_offset += num_words;
4477 c->bit_offset = c->word_offset * BITS_PER_LONG;
4478 if (c->bit_offset > c->bm_bits)
4479 c->bit_offset = c->bm_bits;
4480
4481 return 1;
4482 }
4483
dcbp_get_code(struct p_compressed_bm * p)4484 static enum drbd_bitmap_code dcbp_get_code(struct p_compressed_bm *p)
4485 {
4486 return (enum drbd_bitmap_code)(p->encoding & 0x0f);
4487 }
4488
dcbp_get_start(struct p_compressed_bm * p)4489 static int dcbp_get_start(struct p_compressed_bm *p)
4490 {
4491 return (p->encoding & 0x80) != 0;
4492 }
4493
dcbp_get_pad_bits(struct p_compressed_bm * p)4494 static int dcbp_get_pad_bits(struct p_compressed_bm *p)
4495 {
4496 return (p->encoding >> 4) & 0x7;
4497 }
4498
4499 /*
4500 * recv_bm_rle_bits
4501 *
4502 * Return 0 when done, 1 when another iteration is needed, and a negative error
4503 * code upon failure.
4504 */
4505 static int
recv_bm_rle_bits(struct drbd_peer_device * peer_device,struct p_compressed_bm * p,struct bm_xfer_ctx * c,unsigned int len)4506 recv_bm_rle_bits(struct drbd_peer_device *peer_device,
4507 struct p_compressed_bm *p,
4508 struct bm_xfer_ctx *c,
4509 unsigned int len)
4510 {
4511 struct bitstream bs;
4512 u64 look_ahead;
4513 u64 rl;
4514 u64 tmp;
4515 unsigned long s = c->bit_offset;
4516 unsigned long e;
4517 int toggle = dcbp_get_start(p);
4518 int have;
4519 int bits;
4520
4521 bitstream_init(&bs, p->code, len, dcbp_get_pad_bits(p));
4522
4523 bits = bitstream_get_bits(&bs, &look_ahead, 64);
4524 if (bits < 0)
4525 return -EIO;
4526
4527 for (have = bits; have > 0; s += rl, toggle = !toggle) {
4528 bits = vli_decode_bits(&rl, look_ahead);
4529 if (bits <= 0)
4530 return -EIO;
4531
4532 if (toggle) {
4533 e = s + rl -1;
4534 if (e >= c->bm_bits) {
4535 drbd_err(peer_device, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
4536 return -EIO;
4537 }
4538 _drbd_bm_set_bits(peer_device->device, s, e);
4539 }
4540
4541 if (have < bits) {
4542 drbd_err(peer_device, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
4543 have, bits, look_ahead,
4544 (unsigned int)(bs.cur.b - p->code),
4545 (unsigned int)bs.buf_len);
4546 return -EIO;
4547 }
4548 /* if we consumed all 64 bits, assign 0; >> 64 is "undefined"; */
4549 if (likely(bits < 64))
4550 look_ahead >>= bits;
4551 else
4552 look_ahead = 0;
4553 have -= bits;
4554
4555 bits = bitstream_get_bits(&bs, &tmp, 64 - have);
4556 if (bits < 0)
4557 return -EIO;
4558 look_ahead |= tmp << have;
4559 have += bits;
4560 }
4561
4562 c->bit_offset = s;
4563 bm_xfer_ctx_bit_to_word_offset(c);
4564
4565 return (s != c->bm_bits);
4566 }
4567
4568 /*
4569 * decode_bitmap_c
4570 *
4571 * Return 0 when done, 1 when another iteration is needed, and a negative error
4572 * code upon failure.
4573 */
4574 static int
decode_bitmap_c(struct drbd_peer_device * peer_device,struct p_compressed_bm * p,struct bm_xfer_ctx * c,unsigned int len)4575 decode_bitmap_c(struct drbd_peer_device *peer_device,
4576 struct p_compressed_bm *p,
4577 struct bm_xfer_ctx *c,
4578 unsigned int len)
4579 {
4580 if (dcbp_get_code(p) == RLE_VLI_Bits)
4581 return recv_bm_rle_bits(peer_device, p, c, len - sizeof(*p));
4582
4583 /* other variants had been implemented for evaluation,
4584 * but have been dropped as this one turned out to be "best"
4585 * during all our tests. */
4586
4587 drbd_err(peer_device, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
4588 conn_request_state(peer_device->connection, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
4589 return -EIO;
4590 }
4591
INFO_bm_xfer_stats(struct drbd_peer_device * peer_device,const char * direction,struct bm_xfer_ctx * c)4592 void INFO_bm_xfer_stats(struct drbd_peer_device *peer_device,
4593 const char *direction, struct bm_xfer_ctx *c)
4594 {
4595 /* what would it take to transfer it "plaintext" */
4596 unsigned int header_size = drbd_header_size(peer_device->connection);
4597 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE - header_size;
4598 unsigned int plain =
4599 header_size * (DIV_ROUND_UP(c->bm_words, data_size) + 1) +
4600 c->bm_words * sizeof(unsigned long);
4601 unsigned int total = c->bytes[0] + c->bytes[1];
4602 unsigned int r;
4603
4604 /* total can not be zero. but just in case: */
4605 if (total == 0)
4606 return;
4607
4608 /* don't report if not compressed */
4609 if (total >= plain)
4610 return;
4611
4612 /* total < plain. check for overflow, still */
4613 r = (total > UINT_MAX/1000) ? (total / (plain/1000))
4614 : (1000 * total / plain);
4615
4616 if (r > 1000)
4617 r = 1000;
4618
4619 r = 1000 - r;
4620 drbd_info(peer_device, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
4621 "total %u; compression: %u.%u%%\n",
4622 direction,
4623 c->bytes[1], c->packets[1],
4624 c->bytes[0], c->packets[0],
4625 total, r/10, r % 10);
4626 }
4627
4628 /* Since we are processing the bitfield from lower addresses to higher,
4629 it does not matter if the process it in 32 bit chunks or 64 bit
4630 chunks as long as it is little endian. (Understand it as byte stream,
4631 beginning with the lowest byte...) If we would use big endian
4632 we would need to process it from the highest address to the lowest,
4633 in order to be agnostic to the 32 vs 64 bits issue.
4634
4635 returns 0 on failure, 1 if we successfully received it. */
receive_bitmap(struct drbd_connection * connection,struct packet_info * pi)4636 static int receive_bitmap(struct drbd_connection *connection, struct packet_info *pi)
4637 {
4638 struct drbd_peer_device *peer_device;
4639 struct drbd_device *device;
4640 struct bm_xfer_ctx c;
4641 int err;
4642
4643 peer_device = conn_peer_device(connection, pi->vnr);
4644 if (!peer_device)
4645 return -EIO;
4646 device = peer_device->device;
4647
4648 drbd_bm_lock(device, "receive bitmap", BM_LOCKED_SET_ALLOWED);
4649 /* you are supposed to send additional out-of-sync information
4650 * if you actually set bits during this phase */
4651
4652 c = (struct bm_xfer_ctx) {
4653 .bm_bits = drbd_bm_bits(device),
4654 .bm_words = drbd_bm_words(device),
4655 };
4656
4657 for(;;) {
4658 if (pi->cmd == P_BITMAP)
4659 err = receive_bitmap_plain(peer_device, pi->size, pi->data, &c);
4660 else if (pi->cmd == P_COMPRESSED_BITMAP) {
4661 /* MAYBE: sanity check that we speak proto >= 90,
4662 * and the feature is enabled! */
4663 struct p_compressed_bm *p = pi->data;
4664
4665 if (pi->size > DRBD_SOCKET_BUFFER_SIZE - drbd_header_size(connection)) {
4666 drbd_err(device, "ReportCBitmap packet too large\n");
4667 err = -EIO;
4668 goto out;
4669 }
4670 if (pi->size <= sizeof(*p)) {
4671 drbd_err(device, "ReportCBitmap packet too small (l:%u)\n", pi->size);
4672 err = -EIO;
4673 goto out;
4674 }
4675 err = drbd_recv_all(peer_device->connection, p, pi->size);
4676 if (err)
4677 goto out;
4678 err = decode_bitmap_c(peer_device, p, &c, pi->size);
4679 } else {
4680 drbd_warn(device, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", pi->cmd);
4681 err = -EIO;
4682 goto out;
4683 }
4684
4685 c.packets[pi->cmd == P_BITMAP]++;
4686 c.bytes[pi->cmd == P_BITMAP] += drbd_header_size(connection) + pi->size;
4687
4688 if (err <= 0) {
4689 if (err < 0)
4690 goto out;
4691 break;
4692 }
4693 err = drbd_recv_header(peer_device->connection, pi);
4694 if (err)
4695 goto out;
4696 }
4697
4698 INFO_bm_xfer_stats(peer_device, "receive", &c);
4699
4700 if (device->state.conn == C_WF_BITMAP_T) {
4701 enum drbd_state_rv rv;
4702
4703 err = drbd_send_bitmap(device, peer_device);
4704 if (err)
4705 goto out;
4706 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
4707 rv = _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
4708 D_ASSERT(device, rv == SS_SUCCESS);
4709 } else if (device->state.conn != C_WF_BITMAP_S) {
4710 /* admin may have requested C_DISCONNECTING,
4711 * other threads may have noticed network errors */
4712 drbd_info(device, "unexpected cstate (%s) in receive_bitmap\n",
4713 drbd_conn_str(device->state.conn));
4714 }
4715 err = 0;
4716
4717 out:
4718 drbd_bm_unlock(device);
4719 if (!err && device->state.conn == C_WF_BITMAP_S)
4720 drbd_start_resync(device, C_SYNC_SOURCE);
4721 return err;
4722 }
4723
receive_skip(struct drbd_connection * connection,struct packet_info * pi)4724 static int receive_skip(struct drbd_connection *connection, struct packet_info *pi)
4725 {
4726 drbd_warn(connection, "skipping unknown optional packet type %d, l: %d!\n",
4727 pi->cmd, pi->size);
4728
4729 return ignore_remaining_packet(connection, pi);
4730 }
4731
receive_UnplugRemote(struct drbd_connection * connection,struct packet_info * pi)4732 static int receive_UnplugRemote(struct drbd_connection *connection, struct packet_info *pi)
4733 {
4734 /* Make sure we've acked all the TCP data associated
4735 * with the data requests being unplugged */
4736 tcp_sock_set_quickack(connection->data.socket->sk, 2);
4737 return 0;
4738 }
4739
receive_out_of_sync(struct drbd_connection * connection,struct packet_info * pi)4740 static int receive_out_of_sync(struct drbd_connection *connection, struct packet_info *pi)
4741 {
4742 struct drbd_peer_device *peer_device;
4743 struct drbd_device *device;
4744 struct p_block_desc *p = pi->data;
4745
4746 peer_device = conn_peer_device(connection, pi->vnr);
4747 if (!peer_device)
4748 return -EIO;
4749 device = peer_device->device;
4750
4751 switch (device->state.conn) {
4752 case C_WF_SYNC_UUID:
4753 case C_WF_BITMAP_T:
4754 case C_BEHIND:
4755 break;
4756 default:
4757 drbd_err(device, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n",
4758 drbd_conn_str(device->state.conn));
4759 }
4760
4761 drbd_set_out_of_sync(peer_device, be64_to_cpu(p->sector), be32_to_cpu(p->blksize));
4762
4763 return 0;
4764 }
4765
receive_rs_deallocated(struct drbd_connection * connection,struct packet_info * pi)4766 static int receive_rs_deallocated(struct drbd_connection *connection, struct packet_info *pi)
4767 {
4768 struct drbd_peer_device *peer_device;
4769 struct p_block_desc *p = pi->data;
4770 struct drbd_device *device;
4771 sector_t sector;
4772 int size, err = 0;
4773
4774 peer_device = conn_peer_device(connection, pi->vnr);
4775 if (!peer_device)
4776 return -EIO;
4777 device = peer_device->device;
4778
4779 sector = be64_to_cpu(p->sector);
4780 size = be32_to_cpu(p->blksize);
4781
4782 dec_rs_pending(peer_device);
4783
4784 if (get_ldev(device)) {
4785 struct drbd_peer_request *peer_req;
4786
4787 peer_req = drbd_alloc_peer_req(peer_device, ID_SYNCER, sector,
4788 size, 0, GFP_NOIO);
4789 if (!peer_req) {
4790 put_ldev(device);
4791 return -ENOMEM;
4792 }
4793
4794 peer_req->w.cb = e_end_resync_block;
4795 peer_req->opf = REQ_OP_DISCARD;
4796 peer_req->submit_jif = jiffies;
4797 peer_req->flags |= EE_TRIM;
4798
4799 spin_lock_irq(&device->resource->req_lock);
4800 list_add_tail(&peer_req->w.list, &device->sync_ee);
4801 spin_unlock_irq(&device->resource->req_lock);
4802
4803 atomic_add(pi->size >> 9, &device->rs_sect_ev);
4804 err = drbd_submit_peer_request(peer_req);
4805
4806 if (err) {
4807 spin_lock_irq(&device->resource->req_lock);
4808 list_del(&peer_req->w.list);
4809 spin_unlock_irq(&device->resource->req_lock);
4810
4811 drbd_free_peer_req(device, peer_req);
4812 put_ldev(device);
4813 err = 0;
4814 goto fail;
4815 }
4816
4817 inc_unacked(device);
4818
4819 /* No put_ldev() here. Gets called in drbd_endio_write_sec_final(),
4820 as well as drbd_rs_complete_io() */
4821 } else {
4822 fail:
4823 drbd_rs_complete_io(device, sector);
4824 drbd_send_ack_ex(peer_device, P_NEG_ACK, sector, size, ID_SYNCER);
4825 }
4826
4827 atomic_add(size >> 9, &device->rs_sect_in);
4828
4829 return err;
4830 }
4831
4832 struct data_cmd {
4833 int expect_payload;
4834 unsigned int pkt_size;
4835 int (*fn)(struct drbd_connection *, struct packet_info *);
4836 };
4837
4838 static struct data_cmd drbd_cmd_handler[] = {
4839 [P_DATA] = { 1, sizeof(struct p_data), receive_Data },
4840 [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply },
4841 [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } ,
4842 [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } ,
4843 [P_BITMAP] = { 1, 0, receive_bitmap } ,
4844 [P_COMPRESSED_BITMAP] = { 1, 0, receive_bitmap } ,
4845 [P_UNPLUG_REMOTE] = { 0, 0, receive_UnplugRemote },
4846 [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4847 [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4848 [P_SYNC_PARAM] = { 1, 0, receive_SyncParam },
4849 [P_SYNC_PARAM89] = { 1, 0, receive_SyncParam },
4850 [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol },
4851 [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids },
4852 [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes },
4853 [P_STATE] = { 0, sizeof(struct p_state), receive_state },
4854 [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state },
4855 [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid },
4856 [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4857 [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4858 [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4859 [P_RS_THIN_REQ] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4860 [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip },
4861 [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync },
4862 [P_CONN_ST_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_conn_state },
4863 [P_PROTOCOL_UPDATE] = { 1, sizeof(struct p_protocol), receive_protocol },
4864 [P_TRIM] = { 0, sizeof(struct p_trim), receive_Data },
4865 [P_ZEROES] = { 0, sizeof(struct p_trim), receive_Data },
4866 [P_RS_DEALLOCATED] = { 0, sizeof(struct p_block_desc), receive_rs_deallocated },
4867 };
4868
drbdd(struct drbd_connection * connection)4869 static void drbdd(struct drbd_connection *connection)
4870 {
4871 struct packet_info pi;
4872 size_t shs; /* sub header size */
4873 int err;
4874
4875 while (get_t_state(&connection->receiver) == RUNNING) {
4876 struct data_cmd const *cmd;
4877
4878 drbd_thread_current_set_cpu(&connection->receiver);
4879 update_receiver_timing_details(connection, drbd_recv_header_maybe_unplug);
4880 if (drbd_recv_header_maybe_unplug(connection, &pi))
4881 goto err_out;
4882
4883 cmd = &drbd_cmd_handler[pi.cmd];
4884 if (unlikely(pi.cmd >= ARRAY_SIZE(drbd_cmd_handler) || !cmd->fn)) {
4885 drbd_err(connection, "Unexpected data packet %s (0x%04x)",
4886 cmdname(pi.cmd), pi.cmd);
4887 goto err_out;
4888 }
4889
4890 shs = cmd->pkt_size;
4891 if (pi.cmd == P_SIZES && connection->agreed_features & DRBD_FF_WSAME)
4892 shs += sizeof(struct o_qlim);
4893 if (pi.size > shs && !cmd->expect_payload) {
4894 drbd_err(connection, "No payload expected %s l:%d\n",
4895 cmdname(pi.cmd), pi.size);
4896 goto err_out;
4897 }
4898 if (pi.size < shs) {
4899 drbd_err(connection, "%s: unexpected packet size, expected:%d received:%d\n",
4900 cmdname(pi.cmd), (int)shs, pi.size);
4901 goto err_out;
4902 }
4903
4904 if (shs) {
4905 update_receiver_timing_details(connection, drbd_recv_all_warn);
4906 err = drbd_recv_all_warn(connection, pi.data, shs);
4907 if (err)
4908 goto err_out;
4909 pi.size -= shs;
4910 }
4911
4912 update_receiver_timing_details(connection, cmd->fn);
4913 err = cmd->fn(connection, &pi);
4914 if (err) {
4915 drbd_err(connection, "error receiving %s, e: %d l: %d!\n",
4916 cmdname(pi.cmd), err, pi.size);
4917 goto err_out;
4918 }
4919 }
4920 return;
4921
4922 err_out:
4923 conn_request_state(connection, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
4924 }
4925
conn_disconnect(struct drbd_connection * connection)4926 static void conn_disconnect(struct drbd_connection *connection)
4927 {
4928 struct drbd_peer_device *peer_device;
4929 enum drbd_conns oc;
4930 int vnr;
4931
4932 if (connection->cstate == C_STANDALONE)
4933 return;
4934
4935 /* We are about to start the cleanup after connection loss.
4936 * Make sure drbd_make_request knows about that.
4937 * Usually we should be in some network failure state already,
4938 * but just in case we are not, we fix it up here.
4939 */
4940 conn_request_state(connection, NS(conn, C_NETWORK_FAILURE), CS_HARD);
4941
4942 /* ack_receiver does not clean up anything. it must not interfere, either */
4943 drbd_thread_stop(&connection->ack_receiver);
4944 if (connection->ack_sender) {
4945 destroy_workqueue(connection->ack_sender);
4946 connection->ack_sender = NULL;
4947 }
4948 drbd_free_sock(connection);
4949
4950 rcu_read_lock();
4951 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
4952 struct drbd_device *device = peer_device->device;
4953 kref_get(&device->kref);
4954 rcu_read_unlock();
4955 drbd_disconnected(peer_device);
4956 kref_put(&device->kref, drbd_destroy_device);
4957 rcu_read_lock();
4958 }
4959 rcu_read_unlock();
4960
4961 if (!list_empty(&connection->current_epoch->list))
4962 drbd_err(connection, "ASSERTION FAILED: connection->current_epoch->list not empty\n");
4963 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
4964 atomic_set(&connection->current_epoch->epoch_size, 0);
4965 connection->send.seen_any_write_yet = false;
4966
4967 drbd_info(connection, "Connection closed\n");
4968
4969 if (conn_highest_role(connection) == R_PRIMARY && conn_highest_pdsk(connection) >= D_UNKNOWN)
4970 conn_try_outdate_peer_async(connection);
4971
4972 spin_lock_irq(&connection->resource->req_lock);
4973 oc = connection->cstate;
4974 if (oc >= C_UNCONNECTED)
4975 _conn_request_state(connection, NS(conn, C_UNCONNECTED), CS_VERBOSE);
4976
4977 spin_unlock_irq(&connection->resource->req_lock);
4978
4979 if (oc == C_DISCONNECTING)
4980 conn_request_state(connection, NS(conn, C_STANDALONE), CS_VERBOSE | CS_HARD);
4981 }
4982
drbd_disconnected(struct drbd_peer_device * peer_device)4983 static int drbd_disconnected(struct drbd_peer_device *peer_device)
4984 {
4985 struct drbd_device *device = peer_device->device;
4986 unsigned int i;
4987
4988 /* wait for current activity to cease. */
4989 spin_lock_irq(&device->resource->req_lock);
4990 _drbd_wait_ee_list_empty(device, &device->active_ee);
4991 _drbd_wait_ee_list_empty(device, &device->sync_ee);
4992 _drbd_wait_ee_list_empty(device, &device->read_ee);
4993 spin_unlock_irq(&device->resource->req_lock);
4994
4995 /* We do not have data structures that would allow us to
4996 * get the rs_pending_cnt down to 0 again.
4997 * * On C_SYNC_TARGET we do not have any data structures describing
4998 * the pending RSDataRequest's we have sent.
4999 * * On C_SYNC_SOURCE there is no data structure that tracks
5000 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
5001 * And no, it is not the sum of the reference counts in the
5002 * resync_LRU. The resync_LRU tracks the whole operation including
5003 * the disk-IO, while the rs_pending_cnt only tracks the blocks
5004 * on the fly. */
5005 drbd_rs_cancel_all(device);
5006 device->rs_total = 0;
5007 device->rs_failed = 0;
5008 atomic_set(&device->rs_pending_cnt, 0);
5009 wake_up(&device->misc_wait);
5010
5011 timer_delete_sync(&device->resync_timer);
5012 resync_timer_fn(&device->resync_timer);
5013
5014 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
5015 * w_make_resync_request etc. which may still be on the worker queue
5016 * to be "canceled" */
5017 drbd_flush_workqueue(&peer_device->connection->sender_work);
5018
5019 drbd_finish_peer_reqs(device);
5020
5021 /* This second workqueue flush is necessary, since drbd_finish_peer_reqs()
5022 might have issued a work again. The one before drbd_finish_peer_reqs() is
5023 necessary to reclain net_ee in drbd_finish_peer_reqs(). */
5024 drbd_flush_workqueue(&peer_device->connection->sender_work);
5025
5026 /* need to do it again, drbd_finish_peer_reqs() may have populated it
5027 * again via drbd_try_clear_on_disk_bm(). */
5028 drbd_rs_cancel_all(device);
5029
5030 kfree(device->p_uuid);
5031 device->p_uuid = NULL;
5032
5033 if (!drbd_suspended(device))
5034 tl_clear(peer_device->connection);
5035
5036 drbd_md_sync(device);
5037
5038 if (get_ldev(device)) {
5039 drbd_bitmap_io(device, &drbd_bm_write_copy_pages,
5040 "write from disconnected", BM_LOCKED_CHANGE_ALLOWED, NULL);
5041 put_ldev(device);
5042 }
5043
5044 i = atomic_read(&device->pp_in_use_by_net);
5045 if (i)
5046 drbd_info(device, "pp_in_use_by_net = %d, expected 0\n", i);
5047 i = atomic_read(&device->pp_in_use);
5048 if (i)
5049 drbd_info(device, "pp_in_use = %d, expected 0\n", i);
5050
5051 D_ASSERT(device, list_empty(&device->read_ee));
5052 D_ASSERT(device, list_empty(&device->active_ee));
5053 D_ASSERT(device, list_empty(&device->sync_ee));
5054 D_ASSERT(device, list_empty(&device->done_ee));
5055
5056 return 0;
5057 }
5058
5059 /*
5060 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
5061 * we can agree on is stored in agreed_pro_version.
5062 *
5063 * feature flags and the reserved array should be enough room for future
5064 * enhancements of the handshake protocol, and possible plugins...
5065 *
5066 * for now, they are expected to be zero, but ignored.
5067 */
drbd_send_features(struct drbd_connection * connection)5068 static int drbd_send_features(struct drbd_connection *connection)
5069 {
5070 struct drbd_socket *sock;
5071 struct p_connection_features *p;
5072
5073 sock = &connection->data;
5074 p = conn_prepare_command(connection, sock);
5075 if (!p)
5076 return -EIO;
5077 memset(p, 0, sizeof(*p));
5078 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
5079 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
5080 p->feature_flags = cpu_to_be32(PRO_FEATURES);
5081 return conn_send_command(connection, sock, P_CONNECTION_FEATURES, sizeof(*p), NULL, 0);
5082 }
5083
5084 /*
5085 * return values:
5086 * 1 yes, we have a valid connection
5087 * 0 oops, did not work out, please try again
5088 * -1 peer talks different language,
5089 * no point in trying again, please go standalone.
5090 */
drbd_do_features(struct drbd_connection * connection)5091 static int drbd_do_features(struct drbd_connection *connection)
5092 {
5093 /* ASSERT current == connection->receiver ... */
5094 struct p_connection_features *p;
5095 const int expect = sizeof(struct p_connection_features);
5096 struct packet_info pi;
5097 int err;
5098
5099 err = drbd_send_features(connection);
5100 if (err)
5101 return 0;
5102
5103 err = drbd_recv_header(connection, &pi);
5104 if (err)
5105 return 0;
5106
5107 if (pi.cmd != P_CONNECTION_FEATURES) {
5108 drbd_err(connection, "expected ConnectionFeatures packet, received: %s (0x%04x)\n",
5109 cmdname(pi.cmd), pi.cmd);
5110 return -1;
5111 }
5112
5113 if (pi.size != expect) {
5114 drbd_err(connection, "expected ConnectionFeatures length: %u, received: %u\n",
5115 expect, pi.size);
5116 return -1;
5117 }
5118
5119 p = pi.data;
5120 err = drbd_recv_all_warn(connection, p, expect);
5121 if (err)
5122 return 0;
5123
5124 p->protocol_min = be32_to_cpu(p->protocol_min);
5125 p->protocol_max = be32_to_cpu(p->protocol_max);
5126 if (p->protocol_max == 0)
5127 p->protocol_max = p->protocol_min;
5128
5129 if (PRO_VERSION_MAX < p->protocol_min ||
5130 PRO_VERSION_MIN > p->protocol_max)
5131 goto incompat;
5132
5133 connection->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
5134 connection->agreed_features = PRO_FEATURES & be32_to_cpu(p->feature_flags);
5135
5136 drbd_info(connection, "Handshake successful: "
5137 "Agreed network protocol version %d\n", connection->agreed_pro_version);
5138
5139 drbd_info(connection, "Feature flags enabled on protocol level: 0x%x%s%s%s%s.\n",
5140 connection->agreed_features,
5141 connection->agreed_features & DRBD_FF_TRIM ? " TRIM" : "",
5142 connection->agreed_features & DRBD_FF_THIN_RESYNC ? " THIN_RESYNC" : "",
5143 connection->agreed_features & DRBD_FF_WSAME ? " WRITE_SAME" : "",
5144 connection->agreed_features & DRBD_FF_WZEROES ? " WRITE_ZEROES" :
5145 connection->agreed_features ? "" : " none");
5146
5147 return 1;
5148
5149 incompat:
5150 drbd_err(connection, "incompatible DRBD dialects: "
5151 "I support %d-%d, peer supports %d-%d\n",
5152 PRO_VERSION_MIN, PRO_VERSION_MAX,
5153 p->protocol_min, p->protocol_max);
5154 return -1;
5155 }
5156
5157 #if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
drbd_do_auth(struct drbd_connection * connection)5158 static int drbd_do_auth(struct drbd_connection *connection)
5159 {
5160 drbd_err(connection, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
5161 drbd_err(connection, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
5162 return -1;
5163 }
5164 #else
5165 #define CHALLENGE_LEN 64
5166
5167 /* Return value:
5168 1 - auth succeeded,
5169 0 - failed, try again (network error),
5170 -1 - auth failed, don't try again.
5171 */
5172
drbd_do_auth(struct drbd_connection * connection)5173 static int drbd_do_auth(struct drbd_connection *connection)
5174 {
5175 struct drbd_socket *sock;
5176 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
5177 char *response = NULL;
5178 char *right_response = NULL;
5179 char *peers_ch = NULL;
5180 unsigned int key_len;
5181 char secret[SHARED_SECRET_MAX]; /* 64 byte */
5182 unsigned int resp_size;
5183 struct shash_desc *desc;
5184 struct packet_info pi;
5185 struct net_conf *nc;
5186 int err, rv;
5187
5188 /* FIXME: Put the challenge/response into the preallocated socket buffer. */
5189
5190 rcu_read_lock();
5191 nc = rcu_dereference(connection->net_conf);
5192 key_len = strlen(nc->shared_secret);
5193 memcpy(secret, nc->shared_secret, key_len);
5194 rcu_read_unlock();
5195
5196 desc = kmalloc(sizeof(struct shash_desc) +
5197 crypto_shash_descsize(connection->cram_hmac_tfm),
5198 GFP_KERNEL);
5199 if (!desc) {
5200 rv = -1;
5201 goto fail;
5202 }
5203 desc->tfm = connection->cram_hmac_tfm;
5204
5205 rv = crypto_shash_setkey(connection->cram_hmac_tfm, (u8 *)secret, key_len);
5206 if (rv) {
5207 drbd_err(connection, "crypto_shash_setkey() failed with %d\n", rv);
5208 rv = -1;
5209 goto fail;
5210 }
5211
5212 get_random_bytes(my_challenge, CHALLENGE_LEN);
5213
5214 sock = &connection->data;
5215 if (!conn_prepare_command(connection, sock)) {
5216 rv = 0;
5217 goto fail;
5218 }
5219 rv = !conn_send_command(connection, sock, P_AUTH_CHALLENGE, 0,
5220 my_challenge, CHALLENGE_LEN);
5221 if (!rv)
5222 goto fail;
5223
5224 err = drbd_recv_header(connection, &pi);
5225 if (err) {
5226 rv = 0;
5227 goto fail;
5228 }
5229
5230 if (pi.cmd != P_AUTH_CHALLENGE) {
5231 drbd_err(connection, "expected AuthChallenge packet, received: %s (0x%04x)\n",
5232 cmdname(pi.cmd), pi.cmd);
5233 rv = -1;
5234 goto fail;
5235 }
5236
5237 if (pi.size > CHALLENGE_LEN * 2) {
5238 drbd_err(connection, "expected AuthChallenge payload too big.\n");
5239 rv = -1;
5240 goto fail;
5241 }
5242
5243 if (pi.size < CHALLENGE_LEN) {
5244 drbd_err(connection, "AuthChallenge payload too small.\n");
5245 rv = -1;
5246 goto fail;
5247 }
5248
5249 peers_ch = kmalloc(pi.size, GFP_NOIO);
5250 if (!peers_ch) {
5251 rv = -1;
5252 goto fail;
5253 }
5254
5255 err = drbd_recv_all_warn(connection, peers_ch, pi.size);
5256 if (err) {
5257 rv = 0;
5258 goto fail;
5259 }
5260
5261 if (!memcmp(my_challenge, peers_ch, CHALLENGE_LEN)) {
5262 drbd_err(connection, "Peer presented the same challenge!\n");
5263 rv = -1;
5264 goto fail;
5265 }
5266
5267 resp_size = crypto_shash_digestsize(connection->cram_hmac_tfm);
5268 response = kmalloc(resp_size, GFP_NOIO);
5269 if (!response) {
5270 rv = -1;
5271 goto fail;
5272 }
5273
5274 rv = crypto_shash_digest(desc, peers_ch, pi.size, response);
5275 if (rv) {
5276 drbd_err(connection, "crypto_hash_digest() failed with %d\n", rv);
5277 rv = -1;
5278 goto fail;
5279 }
5280
5281 if (!conn_prepare_command(connection, sock)) {
5282 rv = 0;
5283 goto fail;
5284 }
5285 rv = !conn_send_command(connection, sock, P_AUTH_RESPONSE, 0,
5286 response, resp_size);
5287 if (!rv)
5288 goto fail;
5289
5290 err = drbd_recv_header(connection, &pi);
5291 if (err) {
5292 rv = 0;
5293 goto fail;
5294 }
5295
5296 if (pi.cmd != P_AUTH_RESPONSE) {
5297 drbd_err(connection, "expected AuthResponse packet, received: %s (0x%04x)\n",
5298 cmdname(pi.cmd), pi.cmd);
5299 rv = 0;
5300 goto fail;
5301 }
5302
5303 if (pi.size != resp_size) {
5304 drbd_err(connection, "expected AuthResponse payload of wrong size\n");
5305 rv = 0;
5306 goto fail;
5307 }
5308
5309 err = drbd_recv_all_warn(connection, response , resp_size);
5310 if (err) {
5311 rv = 0;
5312 goto fail;
5313 }
5314
5315 right_response = kmalloc(resp_size, GFP_NOIO);
5316 if (!right_response) {
5317 rv = -1;
5318 goto fail;
5319 }
5320
5321 rv = crypto_shash_digest(desc, my_challenge, CHALLENGE_LEN,
5322 right_response);
5323 if (rv) {
5324 drbd_err(connection, "crypto_hash_digest() failed with %d\n", rv);
5325 rv = -1;
5326 goto fail;
5327 }
5328
5329 rv = !memcmp(response, right_response, resp_size);
5330
5331 if (rv)
5332 drbd_info(connection, "Peer authenticated using %d bytes HMAC\n",
5333 resp_size);
5334 else
5335 rv = -1;
5336
5337 fail:
5338 kfree(peers_ch);
5339 kfree(response);
5340 kfree(right_response);
5341 if (desc) {
5342 shash_desc_zero(desc);
5343 kfree(desc);
5344 }
5345
5346 return rv;
5347 }
5348 #endif
5349
drbd_receiver(struct drbd_thread * thi)5350 int drbd_receiver(struct drbd_thread *thi)
5351 {
5352 struct drbd_connection *connection = thi->connection;
5353 int h;
5354
5355 drbd_info(connection, "receiver (re)started\n");
5356
5357 do {
5358 h = conn_connect(connection);
5359 if (h == 0) {
5360 conn_disconnect(connection);
5361 schedule_timeout_interruptible(HZ);
5362 }
5363 if (h == -1) {
5364 drbd_warn(connection, "Discarding network configuration.\n");
5365 conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD);
5366 }
5367 } while (h == 0);
5368
5369 if (h > 0) {
5370 blk_start_plug(&connection->receiver_plug);
5371 drbdd(connection);
5372 blk_finish_plug(&connection->receiver_plug);
5373 }
5374
5375 conn_disconnect(connection);
5376
5377 drbd_info(connection, "receiver terminated\n");
5378 return 0;
5379 }
5380
5381 /* ********* acknowledge sender ******** */
5382
got_conn_RqSReply(struct drbd_connection * connection,struct packet_info * pi)5383 static int got_conn_RqSReply(struct drbd_connection *connection, struct packet_info *pi)
5384 {
5385 struct p_req_state_reply *p = pi->data;
5386 int retcode = be32_to_cpu(p->retcode);
5387
5388 if (retcode >= SS_SUCCESS) {
5389 set_bit(CONN_WD_ST_CHG_OKAY, &connection->flags);
5390 } else {
5391 set_bit(CONN_WD_ST_CHG_FAIL, &connection->flags);
5392 drbd_err(connection, "Requested state change failed by peer: %s (%d)\n",
5393 drbd_set_st_err_str(retcode), retcode);
5394 }
5395 wake_up(&connection->ping_wait);
5396
5397 return 0;
5398 }
5399
got_RqSReply(struct drbd_connection * connection,struct packet_info * pi)5400 static int got_RqSReply(struct drbd_connection *connection, struct packet_info *pi)
5401 {
5402 struct drbd_peer_device *peer_device;
5403 struct drbd_device *device;
5404 struct p_req_state_reply *p = pi->data;
5405 int retcode = be32_to_cpu(p->retcode);
5406
5407 peer_device = conn_peer_device(connection, pi->vnr);
5408 if (!peer_device)
5409 return -EIO;
5410 device = peer_device->device;
5411
5412 if (test_bit(CONN_WD_ST_CHG_REQ, &connection->flags)) {
5413 D_ASSERT(device, connection->agreed_pro_version < 100);
5414 return got_conn_RqSReply(connection, pi);
5415 }
5416
5417 if (retcode >= SS_SUCCESS) {
5418 set_bit(CL_ST_CHG_SUCCESS, &device->flags);
5419 } else {
5420 set_bit(CL_ST_CHG_FAIL, &device->flags);
5421 drbd_err(device, "Requested state change failed by peer: %s (%d)\n",
5422 drbd_set_st_err_str(retcode), retcode);
5423 }
5424 wake_up(&device->state_wait);
5425
5426 return 0;
5427 }
5428
got_Ping(struct drbd_connection * connection,struct packet_info * pi)5429 static int got_Ping(struct drbd_connection *connection, struct packet_info *pi)
5430 {
5431 return drbd_send_ping_ack(connection);
5432
5433 }
5434
got_PingAck(struct drbd_connection * connection,struct packet_info * pi)5435 static int got_PingAck(struct drbd_connection *connection, struct packet_info *pi)
5436 {
5437 /* restore idle timeout */
5438 connection->meta.socket->sk->sk_rcvtimeo = connection->net_conf->ping_int*HZ;
5439 if (!test_and_set_bit(GOT_PING_ACK, &connection->flags))
5440 wake_up(&connection->ping_wait);
5441
5442 return 0;
5443 }
5444
got_IsInSync(struct drbd_connection * connection,struct packet_info * pi)5445 static int got_IsInSync(struct drbd_connection *connection, struct packet_info *pi)
5446 {
5447 struct drbd_peer_device *peer_device;
5448 struct drbd_device *device;
5449 struct p_block_ack *p = pi->data;
5450 sector_t sector = be64_to_cpu(p->sector);
5451 int blksize = be32_to_cpu(p->blksize);
5452
5453 peer_device = conn_peer_device(connection, pi->vnr);
5454 if (!peer_device)
5455 return -EIO;
5456 device = peer_device->device;
5457
5458 D_ASSERT(device, peer_device->connection->agreed_pro_version >= 89);
5459
5460 update_peer_seq(peer_device, be32_to_cpu(p->seq_num));
5461
5462 if (get_ldev(device)) {
5463 drbd_rs_complete_io(device, sector);
5464 drbd_set_in_sync(peer_device, sector, blksize);
5465 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
5466 device->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
5467 put_ldev(device);
5468 }
5469 dec_rs_pending(peer_device);
5470 atomic_add(blksize >> 9, &device->rs_sect_in);
5471
5472 return 0;
5473 }
5474
5475 static int
validate_req_change_req_state(struct drbd_peer_device * peer_device,u64 id,sector_t sector,struct rb_root * root,const char * func,enum drbd_req_event what,bool missing_ok)5476 validate_req_change_req_state(struct drbd_peer_device *peer_device, u64 id, sector_t sector,
5477 struct rb_root *root, const char *func,
5478 enum drbd_req_event what, bool missing_ok)
5479 {
5480 struct drbd_device *device = peer_device->device;
5481 struct drbd_request *req;
5482 struct bio_and_error m;
5483
5484 spin_lock_irq(&device->resource->req_lock);
5485 req = find_request(device, root, id, sector, missing_ok, func);
5486 if (unlikely(!req)) {
5487 spin_unlock_irq(&device->resource->req_lock);
5488 return -EIO;
5489 }
5490 __req_mod(req, what, peer_device, &m);
5491 spin_unlock_irq(&device->resource->req_lock);
5492
5493 if (m.bio)
5494 complete_master_bio(device, &m);
5495 return 0;
5496 }
5497
got_BlockAck(struct drbd_connection * connection,struct packet_info * pi)5498 static int got_BlockAck(struct drbd_connection *connection, struct packet_info *pi)
5499 {
5500 struct drbd_peer_device *peer_device;
5501 struct drbd_device *device;
5502 struct p_block_ack *p = pi->data;
5503 sector_t sector = be64_to_cpu(p->sector);
5504 int blksize = be32_to_cpu(p->blksize);
5505 enum drbd_req_event what;
5506
5507 peer_device = conn_peer_device(connection, pi->vnr);
5508 if (!peer_device)
5509 return -EIO;
5510 device = peer_device->device;
5511
5512 update_peer_seq(peer_device, be32_to_cpu(p->seq_num));
5513
5514 if (p->block_id == ID_SYNCER) {
5515 drbd_set_in_sync(peer_device, sector, blksize);
5516 dec_rs_pending(peer_device);
5517 return 0;
5518 }
5519 switch (pi->cmd) {
5520 case P_RS_WRITE_ACK:
5521 what = WRITE_ACKED_BY_PEER_AND_SIS;
5522 break;
5523 case P_WRITE_ACK:
5524 what = WRITE_ACKED_BY_PEER;
5525 break;
5526 case P_RECV_ACK:
5527 what = RECV_ACKED_BY_PEER;
5528 break;
5529 case P_SUPERSEDED:
5530 what = CONFLICT_RESOLVED;
5531 break;
5532 case P_RETRY_WRITE:
5533 what = POSTPONE_WRITE;
5534 break;
5535 default:
5536 BUG();
5537 }
5538
5539 return validate_req_change_req_state(peer_device, p->block_id, sector,
5540 &device->write_requests, __func__,
5541 what, false);
5542 }
5543
got_NegAck(struct drbd_connection * connection,struct packet_info * pi)5544 static int got_NegAck(struct drbd_connection *connection, struct packet_info *pi)
5545 {
5546 struct drbd_peer_device *peer_device;
5547 struct drbd_device *device;
5548 struct p_block_ack *p = pi->data;
5549 sector_t sector = be64_to_cpu(p->sector);
5550 int size = be32_to_cpu(p->blksize);
5551 int err;
5552
5553 peer_device = conn_peer_device(connection, pi->vnr);
5554 if (!peer_device)
5555 return -EIO;
5556 device = peer_device->device;
5557
5558 update_peer_seq(peer_device, be32_to_cpu(p->seq_num));
5559
5560 if (p->block_id == ID_SYNCER) {
5561 dec_rs_pending(peer_device);
5562 drbd_rs_failed_io(peer_device, sector, size);
5563 return 0;
5564 }
5565
5566 err = validate_req_change_req_state(peer_device, p->block_id, sector,
5567 &device->write_requests, __func__,
5568 NEG_ACKED, true);
5569 if (err) {
5570 /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs.
5571 The master bio might already be completed, therefore the
5572 request is no longer in the collision hash. */
5573 /* In Protocol B we might already have got a P_RECV_ACK
5574 but then get a P_NEG_ACK afterwards. */
5575 drbd_set_out_of_sync(peer_device, sector, size);
5576 }
5577 return 0;
5578 }
5579
got_NegDReply(struct drbd_connection * connection,struct packet_info * pi)5580 static int got_NegDReply(struct drbd_connection *connection, struct packet_info *pi)
5581 {
5582 struct drbd_peer_device *peer_device;
5583 struct drbd_device *device;
5584 struct p_block_ack *p = pi->data;
5585 sector_t sector = be64_to_cpu(p->sector);
5586
5587 peer_device = conn_peer_device(connection, pi->vnr);
5588 if (!peer_device)
5589 return -EIO;
5590 device = peer_device->device;
5591
5592 update_peer_seq(peer_device, be32_to_cpu(p->seq_num));
5593
5594 drbd_err(device, "Got NegDReply; Sector %llus, len %u.\n",
5595 (unsigned long long)sector, be32_to_cpu(p->blksize));
5596
5597 return validate_req_change_req_state(peer_device, p->block_id, sector,
5598 &device->read_requests, __func__,
5599 NEG_ACKED, false);
5600 }
5601
got_NegRSDReply(struct drbd_connection * connection,struct packet_info * pi)5602 static int got_NegRSDReply(struct drbd_connection *connection, struct packet_info *pi)
5603 {
5604 struct drbd_peer_device *peer_device;
5605 struct drbd_device *device;
5606 sector_t sector;
5607 int size;
5608 struct p_block_ack *p = pi->data;
5609
5610 peer_device = conn_peer_device(connection, pi->vnr);
5611 if (!peer_device)
5612 return -EIO;
5613 device = peer_device->device;
5614
5615 sector = be64_to_cpu(p->sector);
5616 size = be32_to_cpu(p->blksize);
5617
5618 update_peer_seq(peer_device, be32_to_cpu(p->seq_num));
5619
5620 dec_rs_pending(peer_device);
5621
5622 if (get_ldev_if_state(device, D_FAILED)) {
5623 drbd_rs_complete_io(device, sector);
5624 switch (pi->cmd) {
5625 case P_NEG_RS_DREPLY:
5626 drbd_rs_failed_io(peer_device, sector, size);
5627 break;
5628 case P_RS_CANCEL:
5629 break;
5630 default:
5631 BUG();
5632 }
5633 put_ldev(device);
5634 }
5635
5636 return 0;
5637 }
5638
got_BarrierAck(struct drbd_connection * connection,struct packet_info * pi)5639 static int got_BarrierAck(struct drbd_connection *connection, struct packet_info *pi)
5640 {
5641 struct p_barrier_ack *p = pi->data;
5642 struct drbd_peer_device *peer_device;
5643 int vnr;
5644
5645 tl_release(connection, p->barrier, be32_to_cpu(p->set_size));
5646
5647 rcu_read_lock();
5648 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
5649 struct drbd_device *device = peer_device->device;
5650
5651 if (device->state.conn == C_AHEAD &&
5652 atomic_read(&device->ap_in_flight) == 0 &&
5653 !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &device->flags)) {
5654 device->start_resync_timer.expires = jiffies + HZ;
5655 add_timer(&device->start_resync_timer);
5656 }
5657 }
5658 rcu_read_unlock();
5659
5660 return 0;
5661 }
5662
got_OVResult(struct drbd_connection * connection,struct packet_info * pi)5663 static int got_OVResult(struct drbd_connection *connection, struct packet_info *pi)
5664 {
5665 struct drbd_peer_device *peer_device;
5666 struct drbd_device *device;
5667 struct p_block_ack *p = pi->data;
5668 struct drbd_device_work *dw;
5669 sector_t sector;
5670 int size;
5671
5672 peer_device = conn_peer_device(connection, pi->vnr);
5673 if (!peer_device)
5674 return -EIO;
5675 device = peer_device->device;
5676
5677 sector = be64_to_cpu(p->sector);
5678 size = be32_to_cpu(p->blksize);
5679
5680 update_peer_seq(peer_device, be32_to_cpu(p->seq_num));
5681
5682 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
5683 drbd_ov_out_of_sync_found(peer_device, sector, size);
5684 else
5685 ov_out_of_sync_print(peer_device);
5686
5687 if (!get_ldev(device))
5688 return 0;
5689
5690 drbd_rs_complete_io(device, sector);
5691 dec_rs_pending(peer_device);
5692
5693 --device->ov_left;
5694
5695 /* let's advance progress step marks only for every other megabyte */
5696 if ((device->ov_left & 0x200) == 0x200)
5697 drbd_advance_rs_marks(peer_device, device->ov_left);
5698
5699 if (device->ov_left == 0) {
5700 dw = kmalloc_obj(*dw, GFP_NOIO);
5701 if (dw) {
5702 dw->w.cb = w_ov_finished;
5703 dw->device = device;
5704 drbd_queue_work(&peer_device->connection->sender_work, &dw->w);
5705 } else {
5706 drbd_err(device, "kmalloc(dw) failed.");
5707 ov_out_of_sync_print(peer_device);
5708 drbd_resync_finished(peer_device);
5709 }
5710 }
5711 put_ldev(device);
5712 return 0;
5713 }
5714
got_skip(struct drbd_connection * connection,struct packet_info * pi)5715 static int got_skip(struct drbd_connection *connection, struct packet_info *pi)
5716 {
5717 return 0;
5718 }
5719
5720 struct meta_sock_cmd {
5721 size_t pkt_size;
5722 int (*fn)(struct drbd_connection *connection, struct packet_info *);
5723 };
5724
set_rcvtimeo(struct drbd_connection * connection,bool ping_timeout)5725 static void set_rcvtimeo(struct drbd_connection *connection, bool ping_timeout)
5726 {
5727 long t;
5728 struct net_conf *nc;
5729
5730 rcu_read_lock();
5731 nc = rcu_dereference(connection->net_conf);
5732 t = ping_timeout ? nc->ping_timeo : nc->ping_int;
5733 rcu_read_unlock();
5734
5735 t *= HZ;
5736 if (ping_timeout)
5737 t /= 10;
5738
5739 connection->meta.socket->sk->sk_rcvtimeo = t;
5740 }
5741
set_ping_timeout(struct drbd_connection * connection)5742 static void set_ping_timeout(struct drbd_connection *connection)
5743 {
5744 set_rcvtimeo(connection, 1);
5745 }
5746
set_idle_timeout(struct drbd_connection * connection)5747 static void set_idle_timeout(struct drbd_connection *connection)
5748 {
5749 set_rcvtimeo(connection, 0);
5750 }
5751
5752 static struct meta_sock_cmd ack_receiver_tbl[] = {
5753 [P_PING] = { 0, got_Ping },
5754 [P_PING_ACK] = { 0, got_PingAck },
5755 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5756 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5757 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5758 [P_SUPERSEDED] = { sizeof(struct p_block_ack), got_BlockAck },
5759 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
5760 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
5761 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply },
5762 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
5763 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
5764 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
5765 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
5766 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
5767 [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply },
5768 [P_CONN_ST_CHG_REPLY]={ sizeof(struct p_req_state_reply), got_conn_RqSReply },
5769 [P_RETRY_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
5770 };
5771
drbd_ack_receiver(struct drbd_thread * thi)5772 int drbd_ack_receiver(struct drbd_thread *thi)
5773 {
5774 struct drbd_connection *connection = thi->connection;
5775 struct meta_sock_cmd *cmd = NULL;
5776 struct packet_info pi;
5777 unsigned long pre_recv_jif;
5778 int rv;
5779 void *buf = connection->meta.rbuf;
5780 int received = 0;
5781 unsigned int header_size = drbd_header_size(connection);
5782 int expect = header_size;
5783 bool ping_timeout_active = false;
5784
5785 sched_set_fifo_low(current);
5786
5787 while (get_t_state(thi) == RUNNING) {
5788 drbd_thread_current_set_cpu(thi);
5789
5790 if (test_and_clear_bit(SEND_PING, &connection->flags)) {
5791 if (drbd_send_ping(connection)) {
5792 drbd_err(connection, "drbd_send_ping has failed\n");
5793 goto reconnect;
5794 }
5795 set_ping_timeout(connection);
5796 ping_timeout_active = true;
5797 }
5798
5799 pre_recv_jif = jiffies;
5800 rv = drbd_recv_short(connection->meta.socket, buf, expect-received, 0);
5801
5802 /* Note:
5803 * -EINTR (on meta) we got a signal
5804 * -EAGAIN (on meta) rcvtimeo expired
5805 * -ECONNRESET other side closed the connection
5806 * -ERESTARTSYS (on data) we got a signal
5807 * rv < 0 other than above: unexpected error!
5808 * rv == expected: full header or command
5809 * rv < expected: "woken" by signal during receive
5810 * rv == 0 : "connection shut down by peer"
5811 */
5812 if (likely(rv > 0)) {
5813 received += rv;
5814 buf += rv;
5815 } else if (rv == 0) {
5816 if (test_bit(DISCONNECT_SENT, &connection->flags)) {
5817 long t;
5818 rcu_read_lock();
5819 t = rcu_dereference(connection->net_conf)->ping_timeo * HZ/10;
5820 rcu_read_unlock();
5821
5822 t = wait_event_timeout(connection->ping_wait,
5823 connection->cstate < C_WF_REPORT_PARAMS,
5824 t);
5825 if (t)
5826 break;
5827 }
5828 drbd_err(connection, "meta connection shut down by peer.\n");
5829 goto reconnect;
5830 } else if (rv == -EAGAIN) {
5831 /* If the data socket received something meanwhile,
5832 * that is good enough: peer is still alive. */
5833 if (time_after(connection->last_received, pre_recv_jif))
5834 continue;
5835 if (ping_timeout_active) {
5836 drbd_err(connection, "PingAck did not arrive in time.\n");
5837 goto reconnect;
5838 }
5839 set_bit(SEND_PING, &connection->flags);
5840 continue;
5841 } else if (rv == -EINTR) {
5842 /* maybe drbd_thread_stop(): the while condition will notice.
5843 * maybe woken for send_ping: we'll send a ping above,
5844 * and change the rcvtimeo */
5845 flush_signals(current);
5846 continue;
5847 } else {
5848 drbd_err(connection, "sock_recvmsg returned %d\n", rv);
5849 goto reconnect;
5850 }
5851
5852 if (received == expect && cmd == NULL) {
5853 if (decode_header(connection, connection->meta.rbuf, &pi))
5854 goto reconnect;
5855 cmd = &ack_receiver_tbl[pi.cmd];
5856 if (pi.cmd >= ARRAY_SIZE(ack_receiver_tbl) || !cmd->fn) {
5857 drbd_err(connection, "Unexpected meta packet %s (0x%04x)\n",
5858 cmdname(pi.cmd), pi.cmd);
5859 goto disconnect;
5860 }
5861 expect = header_size + cmd->pkt_size;
5862 if (pi.size != expect - header_size) {
5863 drbd_err(connection, "Wrong packet size on meta (c: %d, l: %d)\n",
5864 pi.cmd, pi.size);
5865 goto reconnect;
5866 }
5867 }
5868 if (received == expect) {
5869 bool err;
5870
5871 err = cmd->fn(connection, &pi);
5872 if (err) {
5873 drbd_err(connection, "%ps failed\n", cmd->fn);
5874 goto reconnect;
5875 }
5876
5877 connection->last_received = jiffies;
5878
5879 if (cmd == &ack_receiver_tbl[P_PING_ACK]) {
5880 set_idle_timeout(connection);
5881 ping_timeout_active = false;
5882 }
5883
5884 buf = connection->meta.rbuf;
5885 received = 0;
5886 expect = header_size;
5887 cmd = NULL;
5888 }
5889 }
5890
5891 if (0) {
5892 reconnect:
5893 conn_request_state(connection, NS(conn, C_NETWORK_FAILURE), CS_HARD);
5894 conn_md_sync(connection);
5895 }
5896 if (0) {
5897 disconnect:
5898 conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD);
5899 }
5900
5901 drbd_info(connection, "ack_receiver terminated\n");
5902
5903 return 0;
5904 }
5905
drbd_send_acks_wf(struct work_struct * ws)5906 void drbd_send_acks_wf(struct work_struct *ws)
5907 {
5908 struct drbd_peer_device *peer_device =
5909 container_of(ws, struct drbd_peer_device, send_acks_work);
5910 struct drbd_connection *connection = peer_device->connection;
5911 struct drbd_device *device = peer_device->device;
5912 struct net_conf *nc;
5913 int tcp_cork, err;
5914
5915 rcu_read_lock();
5916 nc = rcu_dereference(connection->net_conf);
5917 tcp_cork = nc->tcp_cork;
5918 rcu_read_unlock();
5919
5920 if (tcp_cork)
5921 tcp_sock_set_cork(connection->meta.socket->sk, true);
5922
5923 err = drbd_finish_peer_reqs(device);
5924 kref_put(&device->kref, drbd_destroy_device);
5925 /* get is in drbd_endio_write_sec_final(). That is necessary to keep the
5926 struct work_struct send_acks_work alive, which is in the peer_device object */
5927
5928 if (err) {
5929 conn_request_state(connection, NS(conn, C_NETWORK_FAILURE), CS_HARD);
5930 return;
5931 }
5932
5933 if (tcp_cork)
5934 tcp_sock_set_cork(connection->meta.socket->sk, false);
5935
5936 return;
5937 }
5938