blob: fe308644a63ca9a004f58b328def9669668136bd [file] [log] [blame]
Philipp Reisnerb411b362009-09-25 16:07:19 -07001/*
2 drbd_receiver.c
3
4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5
6 Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
7 Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8 Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
9
10 drbd is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 drbd is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with drbd; see the file COPYING. If not, write to
22 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25
Philipp Reisnerb411b362009-09-25 16:07:19 -070026#include <linux/module.h>
27
28#include <asm/uaccess.h>
29#include <net/sock.h>
30
Philipp Reisnerb411b362009-09-25 16:07:19 -070031#include <linux/drbd.h>
32#include <linux/fs.h>
33#include <linux/file.h>
34#include <linux/in.h>
35#include <linux/mm.h>
36#include <linux/memcontrol.h>
37#include <linux/mm_inline.h>
38#include <linux/slab.h>
39#include <linux/smp_lock.h>
40#include <linux/pkt_sched.h>
41#define __KERNEL_SYSCALLS__
42#include <linux/unistd.h>
43#include <linux/vmalloc.h>
44#include <linux/random.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070045#include <linux/string.h>
46#include <linux/scatterlist.h>
47#include "drbd_int.h"
Philipp Reisnerb411b362009-09-25 16:07:19 -070048#include "drbd_req.h"
49
50#include "drbd_vli.h"
51
52struct flush_work {
53 struct drbd_work w;
54 struct drbd_epoch *epoch;
55};
56
57enum finish_epoch {
58 FE_STILL_LIVE,
59 FE_DESTROYED,
60 FE_RECYCLED,
61};
62
63static int drbd_do_handshake(struct drbd_conf *mdev);
64static int drbd_do_auth(struct drbd_conf *mdev);
65
66static enum finish_epoch drbd_may_finish_epoch(struct drbd_conf *, struct drbd_epoch *, enum epoch_event);
67static int e_end_block(struct drbd_conf *, struct drbd_work *, int);
68
69static struct drbd_epoch *previous_epoch(struct drbd_conf *mdev, struct drbd_epoch *epoch)
70{
71 struct drbd_epoch *prev;
72 spin_lock(&mdev->epoch_lock);
73 prev = list_entry(epoch->list.prev, struct drbd_epoch, list);
74 if (prev == epoch || prev == mdev->current_epoch)
75 prev = NULL;
76 spin_unlock(&mdev->epoch_lock);
77 return prev;
78}
79
80#define GFP_TRY (__GFP_HIGHMEM | __GFP_NOWARN)
81
Lars Ellenberg45bb9122010-05-14 17:10:48 +020082/*
83 * some helper functions to deal with single linked page lists,
84 * page->private being our "next" pointer.
85 */
86
87/* If at least n pages are linked at head, get n pages off.
88 * Otherwise, don't modify head, and return NULL.
89 * Locking is the responsibility of the caller.
90 */
91static struct page *page_chain_del(struct page **head, int n)
92{
93 struct page *page;
94 struct page *tmp;
95
96 BUG_ON(!n);
97 BUG_ON(!head);
98
99 page = *head;
Philipp Reisner23ce4222010-05-20 13:35:31 +0200100
101 if (!page)
102 return NULL;
103
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200104 while (page) {
105 tmp = page_chain_next(page);
106 if (--n == 0)
107 break; /* found sufficient pages */
108 if (tmp == NULL)
109 /* insufficient pages, don't use any of them. */
110 return NULL;
111 page = tmp;
112 }
113
114 /* add end of list marker for the returned list */
115 set_page_private(page, 0);
116 /* actual return value, and adjustment of head */
117 page = *head;
118 *head = tmp;
119 return page;
120}
121
122/* may be used outside of locks to find the tail of a (usually short)
123 * "private" page chain, before adding it back to a global chain head
124 * with page_chain_add() under a spinlock. */
125static struct page *page_chain_tail(struct page *page, int *len)
126{
127 struct page *tmp;
128 int i = 1;
129 while ((tmp = page_chain_next(page)))
130 ++i, page = tmp;
131 if (len)
132 *len = i;
133 return page;
134}
135
136static int page_chain_free(struct page *page)
137{
138 struct page *tmp;
139 int i = 0;
140 page_chain_for_each_safe(page, tmp) {
141 put_page(page);
142 ++i;
143 }
144 return i;
145}
146
147static void page_chain_add(struct page **head,
148 struct page *chain_first, struct page *chain_last)
149{
150#if 1
151 struct page *tmp;
152 tmp = page_chain_tail(chain_first, NULL);
153 BUG_ON(tmp != chain_last);
154#endif
155
156 /* add chain to head */
157 set_page_private(chain_last, (unsigned long)*head);
158 *head = chain_first;
159}
160
161static struct page *drbd_pp_first_pages_or_try_alloc(struct drbd_conf *mdev, int number)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700162{
163 struct page *page = NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200164 struct page *tmp = NULL;
165 int i = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700166
167 /* Yes, testing drbd_pp_vacant outside the lock is racy.
168 * So what. It saves a spin_lock. */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200169 if (drbd_pp_vacant >= number) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700170 spin_lock(&drbd_pp_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200171 page = page_chain_del(&drbd_pp_pool, number);
172 if (page)
173 drbd_pp_vacant -= number;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700174 spin_unlock(&drbd_pp_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200175 if (page)
176 return page;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700177 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200178
Philipp Reisnerb411b362009-09-25 16:07:19 -0700179 /* GFP_TRY, because we must not cause arbitrary write-out: in a DRBD
180 * "criss-cross" setup, that might cause write-out on some other DRBD,
181 * which in turn might block on the other node at this very place. */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200182 for (i = 0; i < number; i++) {
183 tmp = alloc_page(GFP_TRY);
184 if (!tmp)
185 break;
186 set_page_private(tmp, (unsigned long)page);
187 page = tmp;
188 }
189
190 if (i == number)
191 return page;
192
193 /* Not enough pages immediately available this time.
194 * No need to jump around here, drbd_pp_alloc will retry this
195 * function "soon". */
196 if (page) {
197 tmp = page_chain_tail(page, NULL);
198 spin_lock(&drbd_pp_lock);
199 page_chain_add(&drbd_pp_pool, page, tmp);
200 drbd_pp_vacant += i;
201 spin_unlock(&drbd_pp_lock);
202 }
203 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700204}
205
206/* kick lower level device, if we have more than (arbitrary number)
207 * reference counts on it, which typically are locally submitted io
208 * requests. don't use unacked_cnt, so we speed up proto A and B, too. */
209static void maybe_kick_lo(struct drbd_conf *mdev)
210{
211 if (atomic_read(&mdev->local_cnt) >= mdev->net_conf->unplug_watermark)
212 drbd_kick_lo(mdev);
213}
214
215static void reclaim_net_ee(struct drbd_conf *mdev, struct list_head *to_be_freed)
216{
217 struct drbd_epoch_entry *e;
218 struct list_head *le, *tle;
219
220 /* The EEs are always appended to the end of the list. Since
221 they are sent in order over the wire, they have to finish
222 in order. As soon as we see the first not finished we can
223 stop to examine the list... */
224
225 list_for_each_safe(le, tle, &mdev->net_ee) {
226 e = list_entry(le, struct drbd_epoch_entry, w.list);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200227 if (drbd_ee_has_active_page(e))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700228 break;
229 list_move(le, to_be_freed);
230 }
231}
232
233static void drbd_kick_lo_and_reclaim_net(struct drbd_conf *mdev)
234{
235 LIST_HEAD(reclaimed);
236 struct drbd_epoch_entry *e, *t;
237
238 maybe_kick_lo(mdev);
239 spin_lock_irq(&mdev->req_lock);
240 reclaim_net_ee(mdev, &reclaimed);
241 spin_unlock_irq(&mdev->req_lock);
242
243 list_for_each_entry_safe(e, t, &reclaimed, w.list)
244 drbd_free_ee(mdev, e);
245}
246
247/**
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200248 * drbd_pp_alloc() - Returns @number pages, retries forever (or until signalled)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700249 * @mdev: DRBD device.
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200250 * @number: number of pages requested
251 * @retry: whether to retry, if not enough pages are available right now
Philipp Reisnerb411b362009-09-25 16:07:19 -0700252 *
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200253 * Tries to allocate number pages, first from our own page pool, then from
254 * the kernel, unless this allocation would exceed the max_buffers setting.
255 * Possibly retry until DRBD frees sufficient pages somewhere else.
256 *
257 * Returns a page chain linked via page->private.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700258 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200259static struct page *drbd_pp_alloc(struct drbd_conf *mdev, unsigned number, bool retry)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700260{
261 struct page *page = NULL;
262 DEFINE_WAIT(wait);
263
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200264 /* Yes, we may run up to @number over max_buffers. If we
265 * follow it strictly, the admin will get it wrong anyways. */
266 if (atomic_read(&mdev->pp_in_use) < mdev->net_conf->max_buffers)
267 page = drbd_pp_first_pages_or_try_alloc(mdev, number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700268
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200269 while (page == NULL) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700270 prepare_to_wait(&drbd_pp_wait, &wait, TASK_INTERRUPTIBLE);
271
272 drbd_kick_lo_and_reclaim_net(mdev);
273
274 if (atomic_read(&mdev->pp_in_use) < mdev->net_conf->max_buffers) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200275 page = drbd_pp_first_pages_or_try_alloc(mdev, number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700276 if (page)
277 break;
278 }
279
280 if (!retry)
281 break;
282
283 if (signal_pending(current)) {
284 dev_warn(DEV, "drbd_pp_alloc interrupted!\n");
285 break;
286 }
287
288 schedule();
289 }
290 finish_wait(&drbd_pp_wait, &wait);
291
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200292 if (page)
293 atomic_add(number, &mdev->pp_in_use);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700294 return page;
295}
296
297/* Must not be used from irq, as that may deadlock: see drbd_pp_alloc.
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200298 * Is also used from inside an other spin_lock_irq(&mdev->req_lock);
299 * Either links the page chain back to the global pool,
300 * or returns all pages to the system. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700301static void drbd_pp_free(struct drbd_conf *mdev, struct page *page)
302{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700303 int i;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200304 if (drbd_pp_vacant > (DRBD_MAX_SEGMENT_SIZE/PAGE_SIZE)*minor_count)
305 i = page_chain_free(page);
306 else {
307 struct page *tmp;
308 tmp = page_chain_tail(page, &i);
309 spin_lock(&drbd_pp_lock);
310 page_chain_add(&drbd_pp_pool, page, tmp);
311 drbd_pp_vacant += i;
312 spin_unlock(&drbd_pp_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700313 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200314 atomic_sub(i, &mdev->pp_in_use);
315 i = atomic_read(&mdev->pp_in_use);
316 if (i < 0)
317 dev_warn(DEV, "ASSERTION FAILED: pp_in_use: %d < 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700318 wake_up(&drbd_pp_wait);
319}
320
321/*
322You need to hold the req_lock:
323 _drbd_wait_ee_list_empty()
324
325You must not have the req_lock:
326 drbd_free_ee()
327 drbd_alloc_ee()
328 drbd_init_ee()
329 drbd_release_ee()
330 drbd_ee_fix_bhs()
331 drbd_process_done_ee()
332 drbd_clear_done_ee()
333 drbd_wait_ee_list_empty()
334*/
335
336struct drbd_epoch_entry *drbd_alloc_ee(struct drbd_conf *mdev,
337 u64 id,
338 sector_t sector,
339 unsigned int data_size,
340 gfp_t gfp_mask) __must_hold(local)
341{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700342 struct drbd_epoch_entry *e;
343 struct page *page;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200344 unsigned nr_pages = (data_size + PAGE_SIZE -1) >> PAGE_SHIFT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700345
346 if (FAULT_ACTIVE(mdev, DRBD_FAULT_AL_EE))
347 return NULL;
348
349 e = mempool_alloc(drbd_ee_mempool, gfp_mask & ~__GFP_HIGHMEM);
350 if (!e) {
351 if (!(gfp_mask & __GFP_NOWARN))
352 dev_err(DEV, "alloc_ee: Allocation of an EE failed\n");
353 return NULL;
354 }
355
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200356 page = drbd_pp_alloc(mdev, nr_pages, (gfp_mask & __GFP_WAIT));
357 if (!page)
358 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700359
Philipp Reisnerb411b362009-09-25 16:07:19 -0700360 INIT_HLIST_NODE(&e->colision);
361 e->epoch = NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200362 e->mdev = mdev;
363 e->pages = page;
364 atomic_set(&e->pending_bios, 0);
365 e->size = data_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700366 e->flags = 0;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200367 e->sector = sector;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200368 e->block_id = id;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700369
Philipp Reisnerb411b362009-09-25 16:07:19 -0700370 return e;
371
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200372 fail:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700373 mempool_free(e, drbd_ee_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700374 return NULL;
375}
376
377void drbd_free_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e)
378{
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +0200379 if (e->flags & EE_HAS_DIGEST)
380 kfree(e->digest);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200381 drbd_pp_free(mdev, e->pages);
382 D_ASSERT(atomic_read(&e->pending_bios) == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700383 D_ASSERT(hlist_unhashed(&e->colision));
384 mempool_free(e, drbd_ee_mempool);
385}
386
387int drbd_release_ee(struct drbd_conf *mdev, struct list_head *list)
388{
389 LIST_HEAD(work_list);
390 struct drbd_epoch_entry *e, *t;
391 int count = 0;
392
393 spin_lock_irq(&mdev->req_lock);
394 list_splice_init(list, &work_list);
395 spin_unlock_irq(&mdev->req_lock);
396
397 list_for_each_entry_safe(e, t, &work_list, w.list) {
398 drbd_free_ee(mdev, e);
399 count++;
400 }
401 return count;
402}
403
404
405/*
406 * This function is called from _asender only_
407 * but see also comments in _req_mod(,barrier_acked)
408 * and receive_Barrier.
409 *
410 * Move entries from net_ee to done_ee, if ready.
411 * Grab done_ee, call all callbacks, free the entries.
412 * The callbacks typically send out ACKs.
413 */
414static int drbd_process_done_ee(struct drbd_conf *mdev)
415{
416 LIST_HEAD(work_list);
417 LIST_HEAD(reclaimed);
418 struct drbd_epoch_entry *e, *t;
419 int ok = (mdev->state.conn >= C_WF_REPORT_PARAMS);
420
421 spin_lock_irq(&mdev->req_lock);
422 reclaim_net_ee(mdev, &reclaimed);
423 list_splice_init(&mdev->done_ee, &work_list);
424 spin_unlock_irq(&mdev->req_lock);
425
426 list_for_each_entry_safe(e, t, &reclaimed, w.list)
427 drbd_free_ee(mdev, e);
428
429 /* possible callbacks here:
430 * e_end_block, and e_end_resync_block, e_send_discard_ack.
431 * all ignore the last argument.
432 */
433 list_for_each_entry_safe(e, t, &work_list, w.list) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700434 /* list_del not necessary, next/prev members not touched */
435 ok = e->w.cb(mdev, &e->w, !ok) && ok;
436 drbd_free_ee(mdev, e);
437 }
438 wake_up(&mdev->ee_wait);
439
440 return ok;
441}
442
443void _drbd_wait_ee_list_empty(struct drbd_conf *mdev, struct list_head *head)
444{
445 DEFINE_WAIT(wait);
446
447 /* avoids spin_lock/unlock
448 * and calling prepare_to_wait in the fast path */
449 while (!list_empty(head)) {
450 prepare_to_wait(&mdev->ee_wait, &wait, TASK_UNINTERRUPTIBLE);
451 spin_unlock_irq(&mdev->req_lock);
452 drbd_kick_lo(mdev);
453 schedule();
454 finish_wait(&mdev->ee_wait, &wait);
455 spin_lock_irq(&mdev->req_lock);
456 }
457}
458
459void drbd_wait_ee_list_empty(struct drbd_conf *mdev, struct list_head *head)
460{
461 spin_lock_irq(&mdev->req_lock);
462 _drbd_wait_ee_list_empty(mdev, head);
463 spin_unlock_irq(&mdev->req_lock);
464}
465
466/* see also kernel_accept; which is only present since 2.6.18.
467 * also we want to log which part of it failed, exactly */
468static int drbd_accept(struct drbd_conf *mdev, const char **what,
469 struct socket *sock, struct socket **newsock)
470{
471 struct sock *sk = sock->sk;
472 int err = 0;
473
474 *what = "listen";
475 err = sock->ops->listen(sock, 5);
476 if (err < 0)
477 goto out;
478
479 *what = "sock_create_lite";
480 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
481 newsock);
482 if (err < 0)
483 goto out;
484
485 *what = "accept";
486 err = sock->ops->accept(sock, *newsock, 0);
487 if (err < 0) {
488 sock_release(*newsock);
489 *newsock = NULL;
490 goto out;
491 }
492 (*newsock)->ops = sock->ops;
493
494out:
495 return err;
496}
497
498static int drbd_recv_short(struct drbd_conf *mdev, struct socket *sock,
499 void *buf, size_t size, int flags)
500{
501 mm_segment_t oldfs;
502 struct kvec iov = {
503 .iov_base = buf,
504 .iov_len = size,
505 };
506 struct msghdr msg = {
507 .msg_iovlen = 1,
508 .msg_iov = (struct iovec *)&iov,
509 .msg_flags = (flags ? flags : MSG_WAITALL | MSG_NOSIGNAL)
510 };
511 int rv;
512
513 oldfs = get_fs();
514 set_fs(KERNEL_DS);
515 rv = sock_recvmsg(sock, &msg, size, msg.msg_flags);
516 set_fs(oldfs);
517
518 return rv;
519}
520
521static int drbd_recv(struct drbd_conf *mdev, void *buf, size_t size)
522{
523 mm_segment_t oldfs;
524 struct kvec iov = {
525 .iov_base = buf,
526 .iov_len = size,
527 };
528 struct msghdr msg = {
529 .msg_iovlen = 1,
530 .msg_iov = (struct iovec *)&iov,
531 .msg_flags = MSG_WAITALL | MSG_NOSIGNAL
532 };
533 int rv;
534
535 oldfs = get_fs();
536 set_fs(KERNEL_DS);
537
538 for (;;) {
539 rv = sock_recvmsg(mdev->data.socket, &msg, size, msg.msg_flags);
540 if (rv == size)
541 break;
542
543 /* Note:
544 * ECONNRESET other side closed the connection
545 * ERESTARTSYS (on sock) we got a signal
546 */
547
548 if (rv < 0) {
549 if (rv == -ECONNRESET)
550 dev_info(DEV, "sock was reset by peer\n");
551 else if (rv != -ERESTARTSYS)
552 dev_err(DEV, "sock_recvmsg returned %d\n", rv);
553 break;
554 } else if (rv == 0) {
555 dev_info(DEV, "sock was shut down by peer\n");
556 break;
557 } else {
558 /* signal came in, or peer/link went down,
559 * after we read a partial message
560 */
561 /* D_ASSERT(signal_pending(current)); */
562 break;
563 }
564 };
565
566 set_fs(oldfs);
567
568 if (rv != size)
569 drbd_force_state(mdev, NS(conn, C_BROKEN_PIPE));
570
571 return rv;
572}
573
Lars Ellenberg5dbf1672010-05-25 16:18:01 +0200574/* quoting tcp(7):
575 * On individual connections, the socket buffer size must be set prior to the
576 * listen(2) or connect(2) calls in order to have it take effect.
577 * This is our wrapper to do so.
578 */
579static void drbd_setbufsize(struct socket *sock, unsigned int snd,
580 unsigned int rcv)
581{
582 /* open coded SO_SNDBUF, SO_RCVBUF */
583 if (snd) {
584 sock->sk->sk_sndbuf = snd;
585 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
586 }
587 if (rcv) {
588 sock->sk->sk_rcvbuf = rcv;
589 sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
590 }
591}
592
Philipp Reisnerb411b362009-09-25 16:07:19 -0700593static struct socket *drbd_try_connect(struct drbd_conf *mdev)
594{
595 const char *what;
596 struct socket *sock;
597 struct sockaddr_in6 src_in6;
598 int err;
599 int disconnect_on_error = 1;
600
601 if (!get_net_conf(mdev))
602 return NULL;
603
604 what = "sock_create_kern";
605 err = sock_create_kern(((struct sockaddr *)mdev->net_conf->my_addr)->sa_family,
606 SOCK_STREAM, IPPROTO_TCP, &sock);
607 if (err < 0) {
608 sock = NULL;
609 goto out;
610 }
611
612 sock->sk->sk_rcvtimeo =
613 sock->sk->sk_sndtimeo = mdev->net_conf->try_connect_int*HZ;
Lars Ellenberg5dbf1672010-05-25 16:18:01 +0200614 drbd_setbufsize(sock, mdev->net_conf->sndbuf_size,
615 mdev->net_conf->rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700616
617 /* explicitly bind to the configured IP as source IP
618 * for the outgoing connections.
619 * This is needed for multihomed hosts and to be
620 * able to use lo: interfaces for drbd.
621 * Make sure to use 0 as port number, so linux selects
622 * a free one dynamically.
623 */
624 memcpy(&src_in6, mdev->net_conf->my_addr,
625 min_t(int, mdev->net_conf->my_addr_len, sizeof(src_in6)));
626 if (((struct sockaddr *)mdev->net_conf->my_addr)->sa_family == AF_INET6)
627 src_in6.sin6_port = 0;
628 else
629 ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */
630
631 what = "bind before connect";
632 err = sock->ops->bind(sock,
633 (struct sockaddr *) &src_in6,
634 mdev->net_conf->my_addr_len);
635 if (err < 0)
636 goto out;
637
638 /* connect may fail, peer not yet available.
639 * stay C_WF_CONNECTION, don't go Disconnecting! */
640 disconnect_on_error = 0;
641 what = "connect";
642 err = sock->ops->connect(sock,
643 (struct sockaddr *)mdev->net_conf->peer_addr,
644 mdev->net_conf->peer_addr_len, 0);
645
646out:
647 if (err < 0) {
648 if (sock) {
649 sock_release(sock);
650 sock = NULL;
651 }
652 switch (-err) {
653 /* timeout, busy, signal pending */
654 case ETIMEDOUT: case EAGAIN: case EINPROGRESS:
655 case EINTR: case ERESTARTSYS:
656 /* peer not (yet) available, network problem */
657 case ECONNREFUSED: case ENETUNREACH:
658 case EHOSTDOWN: case EHOSTUNREACH:
659 disconnect_on_error = 0;
660 break;
661 default:
662 dev_err(DEV, "%s failed, err = %d\n", what, err);
663 }
664 if (disconnect_on_error)
665 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
666 }
667 put_net_conf(mdev);
668 return sock;
669}
670
671static struct socket *drbd_wait_for_connect(struct drbd_conf *mdev)
672{
673 int timeo, err;
674 struct socket *s_estab = NULL, *s_listen;
675 const char *what;
676
677 if (!get_net_conf(mdev))
678 return NULL;
679
680 what = "sock_create_kern";
681 err = sock_create_kern(((struct sockaddr *)mdev->net_conf->my_addr)->sa_family,
682 SOCK_STREAM, IPPROTO_TCP, &s_listen);
683 if (err) {
684 s_listen = NULL;
685 goto out;
686 }
687
688 timeo = mdev->net_conf->try_connect_int * HZ;
689 timeo += (random32() & 1) ? timeo / 7 : -timeo / 7; /* 28.5% random jitter */
690
691 s_listen->sk->sk_reuse = 1; /* SO_REUSEADDR */
692 s_listen->sk->sk_rcvtimeo = timeo;
693 s_listen->sk->sk_sndtimeo = timeo;
Lars Ellenberg5dbf1672010-05-25 16:18:01 +0200694 drbd_setbufsize(s_listen, mdev->net_conf->sndbuf_size,
695 mdev->net_conf->rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700696
697 what = "bind before listen";
698 err = s_listen->ops->bind(s_listen,
699 (struct sockaddr *) mdev->net_conf->my_addr,
700 mdev->net_conf->my_addr_len);
701 if (err < 0)
702 goto out;
703
704 err = drbd_accept(mdev, &what, s_listen, &s_estab);
705
706out:
707 if (s_listen)
708 sock_release(s_listen);
709 if (err < 0) {
710 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
711 dev_err(DEV, "%s failed, err = %d\n", what, err);
712 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
713 }
714 }
715 put_net_conf(mdev);
716
717 return s_estab;
718}
719
720static int drbd_send_fp(struct drbd_conf *mdev,
721 struct socket *sock, enum drbd_packets cmd)
722{
Philipp Reisner02918be2010-08-20 14:35:10 +0200723 struct p_header80 *h = &mdev->data.sbuf.header.h80;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700724
725 return _drbd_send_cmd(mdev, sock, cmd, h, sizeof(*h), 0);
726}
727
728static enum drbd_packets drbd_recv_fp(struct drbd_conf *mdev, struct socket *sock)
729{
Philipp Reisner02918be2010-08-20 14:35:10 +0200730 struct p_header80 *h = &mdev->data.rbuf.header.h80;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700731 int rr;
732
733 rr = drbd_recv_short(mdev, sock, h, sizeof(*h), 0);
734
735 if (rr == sizeof(*h) && h->magic == BE_DRBD_MAGIC)
736 return be16_to_cpu(h->command);
737
738 return 0xffff;
739}
740
741/**
742 * drbd_socket_okay() - Free the socket if its connection is not okay
743 * @mdev: DRBD device.
744 * @sock: pointer to the pointer to the socket.
745 */
746static int drbd_socket_okay(struct drbd_conf *mdev, struct socket **sock)
747{
748 int rr;
749 char tb[4];
750
751 if (!*sock)
752 return FALSE;
753
754 rr = drbd_recv_short(mdev, *sock, tb, 4, MSG_DONTWAIT | MSG_PEEK);
755
756 if (rr > 0 || rr == -EAGAIN) {
757 return TRUE;
758 } else {
759 sock_release(*sock);
760 *sock = NULL;
761 return FALSE;
762 }
763}
764
765/*
766 * return values:
767 * 1 yes, we have a valid connection
768 * 0 oops, did not work out, please try again
769 * -1 peer talks different language,
770 * no point in trying again, please go standalone.
771 * -2 We do not have a network config...
772 */
773static int drbd_connect(struct drbd_conf *mdev)
774{
775 struct socket *s, *sock, *msock;
776 int try, h, ok;
777
778 D_ASSERT(!mdev->data.socket);
779
Philipp Reisnerb411b362009-09-25 16:07:19 -0700780 if (drbd_request_state(mdev, NS(conn, C_WF_CONNECTION)) < SS_SUCCESS)
781 return -2;
782
783 clear_bit(DISCARD_CONCURRENT, &mdev->flags);
784
785 sock = NULL;
786 msock = NULL;
787
788 do {
789 for (try = 0;;) {
790 /* 3 tries, this should take less than a second! */
791 s = drbd_try_connect(mdev);
792 if (s || ++try >= 3)
793 break;
794 /* give the other side time to call bind() & listen() */
795 __set_current_state(TASK_INTERRUPTIBLE);
796 schedule_timeout(HZ / 10);
797 }
798
799 if (s) {
800 if (!sock) {
801 drbd_send_fp(mdev, s, P_HAND_SHAKE_S);
802 sock = s;
803 s = NULL;
804 } else if (!msock) {
805 drbd_send_fp(mdev, s, P_HAND_SHAKE_M);
806 msock = s;
807 s = NULL;
808 } else {
809 dev_err(DEV, "Logic error in drbd_connect()\n");
810 goto out_release_sockets;
811 }
812 }
813
814 if (sock && msock) {
815 __set_current_state(TASK_INTERRUPTIBLE);
816 schedule_timeout(HZ / 10);
817 ok = drbd_socket_okay(mdev, &sock);
818 ok = drbd_socket_okay(mdev, &msock) && ok;
819 if (ok)
820 break;
821 }
822
823retry:
824 s = drbd_wait_for_connect(mdev);
825 if (s) {
826 try = drbd_recv_fp(mdev, s);
827 drbd_socket_okay(mdev, &sock);
828 drbd_socket_okay(mdev, &msock);
829 switch (try) {
830 case P_HAND_SHAKE_S:
831 if (sock) {
832 dev_warn(DEV, "initial packet S crossed\n");
833 sock_release(sock);
834 }
835 sock = s;
836 break;
837 case P_HAND_SHAKE_M:
838 if (msock) {
839 dev_warn(DEV, "initial packet M crossed\n");
840 sock_release(msock);
841 }
842 msock = s;
843 set_bit(DISCARD_CONCURRENT, &mdev->flags);
844 break;
845 default:
846 dev_warn(DEV, "Error receiving initial packet\n");
847 sock_release(s);
848 if (random32() & 1)
849 goto retry;
850 }
851 }
852
853 if (mdev->state.conn <= C_DISCONNECTING)
854 goto out_release_sockets;
855 if (signal_pending(current)) {
856 flush_signals(current);
857 smp_rmb();
858 if (get_t_state(&mdev->receiver) == Exiting)
859 goto out_release_sockets;
860 }
861
862 if (sock && msock) {
863 ok = drbd_socket_okay(mdev, &sock);
864 ok = drbd_socket_okay(mdev, &msock) && ok;
865 if (ok)
866 break;
867 }
868 } while (1);
869
870 msock->sk->sk_reuse = 1; /* SO_REUSEADDR */
871 sock->sk->sk_reuse = 1; /* SO_REUSEADDR */
872
873 sock->sk->sk_allocation = GFP_NOIO;
874 msock->sk->sk_allocation = GFP_NOIO;
875
876 sock->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
877 msock->sk->sk_priority = TC_PRIO_INTERACTIVE;
878
Philipp Reisnerb411b362009-09-25 16:07:19 -0700879 /* NOT YET ...
880 * sock->sk->sk_sndtimeo = mdev->net_conf->timeout*HZ/10;
881 * sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
882 * first set it to the P_HAND_SHAKE timeout,
883 * which we set to 4x the configured ping_timeout. */
884 sock->sk->sk_sndtimeo =
885 sock->sk->sk_rcvtimeo = mdev->net_conf->ping_timeo*4*HZ/10;
886
887 msock->sk->sk_sndtimeo = mdev->net_conf->timeout*HZ/10;
888 msock->sk->sk_rcvtimeo = mdev->net_conf->ping_int*HZ;
889
890 /* we don't want delays.
891 * we use TCP_CORK where apropriate, though */
892 drbd_tcp_nodelay(sock);
893 drbd_tcp_nodelay(msock);
894
895 mdev->data.socket = sock;
896 mdev->meta.socket = msock;
897 mdev->last_received = jiffies;
898
899 D_ASSERT(mdev->asender.task == NULL);
900
901 h = drbd_do_handshake(mdev);
902 if (h <= 0)
903 return h;
904
905 if (mdev->cram_hmac_tfm) {
906 /* drbd_request_state(mdev, NS(conn, WFAuth)); */
Johannes Thomab10d96c2010-01-07 16:02:50 +0100907 switch (drbd_do_auth(mdev)) {
908 case -1:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700909 dev_err(DEV, "Authentication of peer failed\n");
910 return -1;
Johannes Thomab10d96c2010-01-07 16:02:50 +0100911 case 0:
912 dev_err(DEV, "Authentication of peer failed, trying again.\n");
913 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700914 }
915 }
916
917 if (drbd_request_state(mdev, NS(conn, C_WF_REPORT_PARAMS)) < SS_SUCCESS)
918 return 0;
919
920 sock->sk->sk_sndtimeo = mdev->net_conf->timeout*HZ/10;
921 sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
922
923 atomic_set(&mdev->packet_seq, 0);
924 mdev->peer_seq = 0;
925
926 drbd_thread_start(&mdev->asender);
927
Philipp Reisner7e2455c2010-04-22 14:50:23 +0200928 if (!drbd_send_protocol(mdev))
929 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700930 drbd_send_sync_param(mdev, &mdev->sync_conf);
Philipp Reisnere89b5912010-03-24 17:11:33 +0100931 drbd_send_sizes(mdev, 0, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700932 drbd_send_uuids(mdev);
933 drbd_send_state(mdev);
934 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
935 clear_bit(RESIZE_PENDING, &mdev->flags);
936
937 return 1;
938
939out_release_sockets:
940 if (sock)
941 sock_release(sock);
942 if (msock)
943 sock_release(msock);
944 return -1;
945}
946
Philipp Reisner02918be2010-08-20 14:35:10 +0200947static int drbd_recv_header(struct drbd_conf *mdev, enum drbd_packets *cmd, unsigned int *packet_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700948{
Philipp Reisner02918be2010-08-20 14:35:10 +0200949 union p_header *h = &mdev->data.rbuf.header;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700950 int r;
951
952 r = drbd_recv(mdev, h, sizeof(*h));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700953 if (unlikely(r != sizeof(*h))) {
954 dev_err(DEV, "short read expecting header on sock: r=%d\n", r);
955 return FALSE;
Philipp Reisner02918be2010-08-20 14:35:10 +0200956 }
957
958 if (likely(h->h80.magic == BE_DRBD_MAGIC)) {
959 *cmd = be16_to_cpu(h->h80.command);
960 *packet_size = be16_to_cpu(h->h80.length);
961 } else if (h->h95.magic == BE_DRBD_MAGIC_BIG) {
962 *cmd = be16_to_cpu(h->h95.command);
963 *packet_size = be32_to_cpu(h->h95.length);
964 } else {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700965 dev_err(DEV, "magic?? on data m: 0x%lx c: %d l: %d\n",
Philipp Reisner02918be2010-08-20 14:35:10 +0200966 (long)be32_to_cpu(h->h80.magic),
967 h->h80.command, h->h80.length);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700968 return FALSE;
969 }
970 mdev->last_received = jiffies;
971
972 return TRUE;
973}
974
975static enum finish_epoch drbd_flush_after_epoch(struct drbd_conf *mdev, struct drbd_epoch *epoch)
976{
977 int rv;
978
979 if (mdev->write_ordering >= WO_bdev_flush && get_ldev(mdev)) {
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400980 rv = blkdev_issue_flush(mdev->ldev->backing_bdev, GFP_KERNEL,
981 NULL, BLKDEV_IFL_WAIT);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700982 if (rv) {
983 dev_err(DEV, "local disk flush failed with status %d\n", rv);
984 /* would rather check on EOPNOTSUPP, but that is not reliable.
985 * don't try again for ANY return value != 0
986 * if (rv == -EOPNOTSUPP) */
987 drbd_bump_write_ordering(mdev, WO_drain_io);
988 }
989 put_ldev(mdev);
990 }
991
992 return drbd_may_finish_epoch(mdev, epoch, EV_BARRIER_DONE);
993}
994
995static int w_flush(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
996{
997 struct flush_work *fw = (struct flush_work *)w;
998 struct drbd_epoch *epoch = fw->epoch;
999
1000 kfree(w);
1001
1002 if (!test_and_set_bit(DE_BARRIER_IN_NEXT_EPOCH_ISSUED, &epoch->flags))
1003 drbd_flush_after_epoch(mdev, epoch);
1004
1005 drbd_may_finish_epoch(mdev, epoch, EV_PUT |
1006 (mdev->state.conn < C_CONNECTED ? EV_CLEANUP : 0));
1007
1008 return 1;
1009}
1010
1011/**
1012 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
1013 * @mdev: DRBD device.
1014 * @epoch: Epoch object.
1015 * @ev: Epoch event.
1016 */
1017static enum finish_epoch drbd_may_finish_epoch(struct drbd_conf *mdev,
1018 struct drbd_epoch *epoch,
1019 enum epoch_event ev)
1020{
1021 int finish, epoch_size;
1022 struct drbd_epoch *next_epoch;
1023 int schedule_flush = 0;
1024 enum finish_epoch rv = FE_STILL_LIVE;
1025
1026 spin_lock(&mdev->epoch_lock);
1027 do {
1028 next_epoch = NULL;
1029 finish = 0;
1030
1031 epoch_size = atomic_read(&epoch->epoch_size);
1032
1033 switch (ev & ~EV_CLEANUP) {
1034 case EV_PUT:
1035 atomic_dec(&epoch->active);
1036 break;
1037 case EV_GOT_BARRIER_NR:
1038 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
1039
1040 /* Special case: If we just switched from WO_bio_barrier to
1041 WO_bdev_flush we should not finish the current epoch */
1042 if (test_bit(DE_CONTAINS_A_BARRIER, &epoch->flags) && epoch_size == 1 &&
1043 mdev->write_ordering != WO_bio_barrier &&
1044 epoch == mdev->current_epoch)
1045 clear_bit(DE_CONTAINS_A_BARRIER, &epoch->flags);
1046 break;
1047 case EV_BARRIER_DONE:
1048 set_bit(DE_BARRIER_IN_NEXT_EPOCH_DONE, &epoch->flags);
1049 break;
1050 case EV_BECAME_LAST:
1051 /* nothing to do*/
1052 break;
1053 }
1054
Philipp Reisnerb411b362009-09-25 16:07:19 -07001055 if (epoch_size != 0 &&
1056 atomic_read(&epoch->active) == 0 &&
1057 test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags) &&
1058 epoch->list.prev == &mdev->current_epoch->list &&
1059 !test_bit(DE_IS_FINISHING, &epoch->flags)) {
1060 /* Nearly all conditions are met to finish that epoch... */
1061 if (test_bit(DE_BARRIER_IN_NEXT_EPOCH_DONE, &epoch->flags) ||
1062 mdev->write_ordering == WO_none ||
1063 (epoch_size == 1 && test_bit(DE_CONTAINS_A_BARRIER, &epoch->flags)) ||
1064 ev & EV_CLEANUP) {
1065 finish = 1;
1066 set_bit(DE_IS_FINISHING, &epoch->flags);
1067 } else if (!test_bit(DE_BARRIER_IN_NEXT_EPOCH_ISSUED, &epoch->flags) &&
1068 mdev->write_ordering == WO_bio_barrier) {
1069 atomic_inc(&epoch->active);
1070 schedule_flush = 1;
1071 }
1072 }
1073 if (finish) {
1074 if (!(ev & EV_CLEANUP)) {
1075 spin_unlock(&mdev->epoch_lock);
1076 drbd_send_b_ack(mdev, epoch->barrier_nr, epoch_size);
1077 spin_lock(&mdev->epoch_lock);
1078 }
1079 dec_unacked(mdev);
1080
1081 if (mdev->current_epoch != epoch) {
1082 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1083 list_del(&epoch->list);
1084 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
1085 mdev->epochs--;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001086 kfree(epoch);
1087
1088 if (rv == FE_STILL_LIVE)
1089 rv = FE_DESTROYED;
1090 } else {
1091 epoch->flags = 0;
1092 atomic_set(&epoch->epoch_size, 0);
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001093 /* atomic_set(&epoch->active, 0); is already zero */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001094 if (rv == FE_STILL_LIVE)
1095 rv = FE_RECYCLED;
1096 }
1097 }
1098
1099 if (!next_epoch)
1100 break;
1101
1102 epoch = next_epoch;
1103 } while (1);
1104
1105 spin_unlock(&mdev->epoch_lock);
1106
1107 if (schedule_flush) {
1108 struct flush_work *fw;
1109 fw = kmalloc(sizeof(*fw), GFP_ATOMIC);
1110 if (fw) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001111 fw->w.cb = w_flush;
1112 fw->epoch = epoch;
1113 drbd_queue_work(&mdev->data.work, &fw->w);
1114 } else {
1115 dev_warn(DEV, "Could not kmalloc a flush_work obj\n");
1116 set_bit(DE_BARRIER_IN_NEXT_EPOCH_ISSUED, &epoch->flags);
1117 /* That is not a recursion, only one level */
1118 drbd_may_finish_epoch(mdev, epoch, EV_BARRIER_DONE);
1119 drbd_may_finish_epoch(mdev, epoch, EV_PUT);
1120 }
1121 }
1122
1123 return rv;
1124}
1125
1126/**
1127 * drbd_bump_write_ordering() - Fall back to an other write ordering method
1128 * @mdev: DRBD device.
1129 * @wo: Write ordering method to try.
1130 */
1131void drbd_bump_write_ordering(struct drbd_conf *mdev, enum write_ordering_e wo) __must_hold(local)
1132{
1133 enum write_ordering_e pwo;
1134 static char *write_ordering_str[] = {
1135 [WO_none] = "none",
1136 [WO_drain_io] = "drain",
1137 [WO_bdev_flush] = "flush",
1138 [WO_bio_barrier] = "barrier",
1139 };
1140
1141 pwo = mdev->write_ordering;
1142 wo = min(pwo, wo);
1143 if (wo == WO_bio_barrier && mdev->ldev->dc.no_disk_barrier)
1144 wo = WO_bdev_flush;
1145 if (wo == WO_bdev_flush && mdev->ldev->dc.no_disk_flush)
1146 wo = WO_drain_io;
1147 if (wo == WO_drain_io && mdev->ldev->dc.no_disk_drain)
1148 wo = WO_none;
1149 mdev->write_ordering = wo;
1150 if (pwo != mdev->write_ordering || wo == WO_bio_barrier)
1151 dev_info(DEV, "Method to ensure write ordering: %s\n", write_ordering_str[mdev->write_ordering]);
1152}
1153
1154/**
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001155 * drbd_submit_ee()
1156 * @mdev: DRBD device.
1157 * @e: epoch entry
1158 * @rw: flag field, see bio->bi_rw
1159 */
1160/* TODO allocate from our own bio_set. */
1161int drbd_submit_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e,
1162 const unsigned rw, const int fault_type)
1163{
1164 struct bio *bios = NULL;
1165 struct bio *bio;
1166 struct page *page = e->pages;
1167 sector_t sector = e->sector;
1168 unsigned ds = e->size;
1169 unsigned n_bios = 0;
1170 unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
1171
1172 /* In most cases, we will only need one bio. But in case the lower
1173 * level restrictions happen to be different at this offset on this
1174 * side than those of the sending peer, we may need to submit the
1175 * request in more than one bio. */
1176next_bio:
1177 bio = bio_alloc(GFP_NOIO, nr_pages);
1178 if (!bio) {
1179 dev_err(DEV, "submit_ee: Allocation of a bio failed\n");
1180 goto fail;
1181 }
1182 /* > e->sector, unless this is the first bio */
1183 bio->bi_sector = sector;
1184 bio->bi_bdev = mdev->ldev->backing_bdev;
1185 /* we special case some flags in the multi-bio case, see below
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001186 * (REQ_UNPLUG, REQ_HARDBARRIER) */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001187 bio->bi_rw = rw;
1188 bio->bi_private = e;
1189 bio->bi_end_io = drbd_endio_sec;
1190
1191 bio->bi_next = bios;
1192 bios = bio;
1193 ++n_bios;
1194
1195 page_chain_for_each(page) {
1196 unsigned len = min_t(unsigned, ds, PAGE_SIZE);
1197 if (!bio_add_page(bio, page, len, 0)) {
1198 /* a single page must always be possible! */
1199 BUG_ON(bio->bi_vcnt == 0);
1200 goto next_bio;
1201 }
1202 ds -= len;
1203 sector += len >> 9;
1204 --nr_pages;
1205 }
1206 D_ASSERT(page == NULL);
1207 D_ASSERT(ds == 0);
1208
1209 atomic_set(&e->pending_bios, n_bios);
1210 do {
1211 bio = bios;
1212 bios = bios->bi_next;
1213 bio->bi_next = NULL;
1214
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001215 /* strip off REQ_UNPLUG unless it is the last bio */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001216 if (bios)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001217 bio->bi_rw &= ~REQ_UNPLUG;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001218
1219 drbd_generic_make_request(mdev, fault_type, bio);
1220
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001221 /* strip off REQ_HARDBARRIER,
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001222 * unless it is the first or last bio */
1223 if (bios && bios->bi_next)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001224 bios->bi_rw &= ~REQ_HARDBARRIER;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001225 } while (bios);
1226 maybe_kick_lo(mdev);
1227 return 0;
1228
1229fail:
1230 while (bios) {
1231 bio = bios;
1232 bios = bios->bi_next;
1233 bio_put(bio);
1234 }
1235 return -ENOMEM;
1236}
1237
1238/**
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001239 * w_e_reissue() - Worker callback; Resubmit a bio, without REQ_HARDBARRIER set
Philipp Reisnerb411b362009-09-25 16:07:19 -07001240 * @mdev: DRBD device.
1241 * @w: work object.
1242 * @cancel: The connection will be closed anyways (unused in this callback)
1243 */
1244int w_e_reissue(struct drbd_conf *mdev, struct drbd_work *w, int cancel) __releases(local)
1245{
1246 struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001247 /* We leave DE_CONTAINS_A_BARRIER and EE_IS_BARRIER in place,
1248 (and DE_BARRIER_IN_NEXT_EPOCH_ISSUED in the previous Epoch)
1249 so that we can finish that epoch in drbd_may_finish_epoch().
1250 That is necessary if we already have a long chain of Epochs, before
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001251 we realize that REQ_HARDBARRIER is actually not supported */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001252
1253 /* As long as the -ENOTSUPP on the barrier is reported immediately
1254 that will never trigger. If it is reported late, we will just
1255 print that warning and continue correctly for all future requests
1256 with WO_bdev_flush */
1257 if (previous_epoch(mdev, e->epoch))
1258 dev_warn(DEV, "Write ordering was not enforced (one time event)\n");
1259
Philipp Reisnerb411b362009-09-25 16:07:19 -07001260 /* we still have a local reference,
1261 * get_ldev was done in receive_Data. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001262
1263 e->w.cb = e_end_block;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001264 if (drbd_submit_ee(mdev, e, WRITE, DRBD_FAULT_DT_WR) != 0) {
1265 /* drbd_submit_ee fails for one reason only:
1266 * if was not able to allocate sufficient bios.
1267 * requeue, try again later. */
1268 e->w.cb = w_e_reissue;
1269 drbd_queue_work(&mdev->data.work, &e->w);
1270 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001271 return 1;
1272}
1273
Philipp Reisner02918be2010-08-20 14:35:10 +02001274static int receive_Barrier(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001275{
1276 int rv, issue_flush;
Philipp Reisner02918be2010-08-20 14:35:10 +02001277 struct p_barrier *p = &mdev->data.rbuf.barrier;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001278 struct drbd_epoch *epoch;
1279
Philipp Reisnerb411b362009-09-25 16:07:19 -07001280 inc_unacked(mdev);
1281
1282 if (mdev->net_conf->wire_protocol != DRBD_PROT_C)
1283 drbd_kick_lo(mdev);
1284
1285 mdev->current_epoch->barrier_nr = p->barrier;
1286 rv = drbd_may_finish_epoch(mdev, mdev->current_epoch, EV_GOT_BARRIER_NR);
1287
1288 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
1289 * the activity log, which means it would not be resynced in case the
1290 * R_PRIMARY crashes now.
1291 * Therefore we must send the barrier_ack after the barrier request was
1292 * completed. */
1293 switch (mdev->write_ordering) {
1294 case WO_bio_barrier:
1295 case WO_none:
1296 if (rv == FE_RECYCLED)
1297 return TRUE;
1298 break;
1299
1300 case WO_bdev_flush:
1301 case WO_drain_io:
Philipp Reisner367a8d72009-12-29 15:56:01 +01001302 if (rv == FE_STILL_LIVE) {
1303 set_bit(DE_BARRIER_IN_NEXT_EPOCH_ISSUED, &mdev->current_epoch->flags);
1304 drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
1305 rv = drbd_flush_after_epoch(mdev, mdev->current_epoch);
1306 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001307 if (rv == FE_RECYCLED)
1308 return TRUE;
1309
1310 /* The asender will send all the ACKs and barrier ACKs out, since
1311 all EEs moved from the active_ee to the done_ee. We need to
1312 provide a new epoch object for the EEs that come in soon */
1313 break;
1314 }
1315
1316 /* receiver context, in the writeout path of the other node.
1317 * avoid potential distributed deadlock */
1318 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1319 if (!epoch) {
1320 dev_warn(DEV, "Allocation of an epoch failed, slowing down\n");
Dan Carpenterd3db7b42010-01-23 15:45:22 +03001321 issue_flush = !test_and_set_bit(DE_BARRIER_IN_NEXT_EPOCH_ISSUED, &mdev->current_epoch->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001322 drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
1323 if (issue_flush) {
1324 rv = drbd_flush_after_epoch(mdev, mdev->current_epoch);
1325 if (rv == FE_RECYCLED)
1326 return TRUE;
1327 }
1328
1329 drbd_wait_ee_list_empty(mdev, &mdev->done_ee);
1330
1331 return TRUE;
1332 }
1333
1334 epoch->flags = 0;
1335 atomic_set(&epoch->epoch_size, 0);
1336 atomic_set(&epoch->active, 0);
1337
1338 spin_lock(&mdev->epoch_lock);
1339 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1340 list_add(&epoch->list, &mdev->current_epoch->list);
1341 mdev->current_epoch = epoch;
1342 mdev->epochs++;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001343 } else {
1344 /* The current_epoch got recycled while we allocated this one... */
1345 kfree(epoch);
1346 }
1347 spin_unlock(&mdev->epoch_lock);
1348
1349 return TRUE;
1350}
1351
1352/* used from receive_RSDataReply (recv_resync_read)
1353 * and from receive_Data */
1354static struct drbd_epoch_entry *
1355read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector, int data_size) __must_hold(local)
1356{
Lars Ellenberg66660322010-04-06 12:15:04 +02001357 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001358 struct drbd_epoch_entry *e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001359 struct page *page;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001360 int dgs, ds, rr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001361 void *dig_in = mdev->int_dig_in;
1362 void *dig_vv = mdev->int_dig_vv;
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001363 unsigned long *data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001364
1365 dgs = (mdev->agreed_pro_version >= 87 && mdev->integrity_r_tfm) ?
1366 crypto_hash_digestsize(mdev->integrity_r_tfm) : 0;
1367
1368 if (dgs) {
1369 rr = drbd_recv(mdev, dig_in, dgs);
1370 if (rr != dgs) {
1371 dev_warn(DEV, "short read receiving data digest: read %d expected %d\n",
1372 rr, dgs);
1373 return NULL;
1374 }
1375 }
1376
1377 data_size -= dgs;
1378
1379 ERR_IF(data_size & 0x1ff) return NULL;
1380 ERR_IF(data_size > DRBD_MAX_SEGMENT_SIZE) return NULL;
1381
Lars Ellenberg66660322010-04-06 12:15:04 +02001382 /* even though we trust out peer,
1383 * we sometimes have to double check. */
1384 if (sector + (data_size>>9) > capacity) {
1385 dev_err(DEV, "capacity: %llus < sector: %llus + size: %u\n",
1386 (unsigned long long)capacity,
1387 (unsigned long long)sector, data_size);
1388 return NULL;
1389 }
1390
Philipp Reisnerb411b362009-09-25 16:07:19 -07001391 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1392 * "criss-cross" setup, that might cause write-out on some other DRBD,
1393 * which in turn might block on the other node at this very place. */
1394 e = drbd_alloc_ee(mdev, id, sector, data_size, GFP_NOIO);
1395 if (!e)
1396 return NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001397
Philipp Reisnerb411b362009-09-25 16:07:19 -07001398 ds = data_size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001399 page = e->pages;
1400 page_chain_for_each(page) {
1401 unsigned len = min_t(int, ds, PAGE_SIZE);
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001402 data = kmap(page);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001403 rr = drbd_recv(mdev, data, len);
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001404 if (FAULT_ACTIVE(mdev, DRBD_FAULT_RECEIVE)) {
1405 dev_err(DEV, "Fault injection: Corrupting data on receive\n");
1406 data[0] = data[0] ^ (unsigned long)-1;
1407 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001408 kunmap(page);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001409 if (rr != len) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001410 drbd_free_ee(mdev, e);
1411 dev_warn(DEV, "short read receiving data: read %d expected %d\n",
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001412 rr, len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001413 return NULL;
1414 }
1415 ds -= rr;
1416 }
1417
1418 if (dgs) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001419 drbd_csum_ee(mdev, mdev->integrity_r_tfm, e, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001420 if (memcmp(dig_in, dig_vv, dgs)) {
1421 dev_err(DEV, "Digest integrity check FAILED.\n");
1422 drbd_bcast_ee(mdev, "digest failed",
1423 dgs, dig_in, dig_vv, e);
1424 drbd_free_ee(mdev, e);
1425 return NULL;
1426 }
1427 }
1428 mdev->recv_cnt += data_size>>9;
1429 return e;
1430}
1431
1432/* drbd_drain_block() just takes a data block
1433 * out of the socket input buffer, and discards it.
1434 */
1435static int drbd_drain_block(struct drbd_conf *mdev, int data_size)
1436{
1437 struct page *page;
1438 int rr, rv = 1;
1439 void *data;
1440
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001441 if (!data_size)
1442 return TRUE;
1443
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001444 page = drbd_pp_alloc(mdev, 1, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001445
1446 data = kmap(page);
1447 while (data_size) {
1448 rr = drbd_recv(mdev, data, min_t(int, data_size, PAGE_SIZE));
1449 if (rr != min_t(int, data_size, PAGE_SIZE)) {
1450 rv = 0;
1451 dev_warn(DEV, "short read receiving data: read %d expected %d\n",
1452 rr, min_t(int, data_size, PAGE_SIZE));
1453 break;
1454 }
1455 data_size -= rr;
1456 }
1457 kunmap(page);
1458 drbd_pp_free(mdev, page);
1459 return rv;
1460}
1461
1462static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req,
1463 sector_t sector, int data_size)
1464{
1465 struct bio_vec *bvec;
1466 struct bio *bio;
1467 int dgs, rr, i, expect;
1468 void *dig_in = mdev->int_dig_in;
1469 void *dig_vv = mdev->int_dig_vv;
1470
1471 dgs = (mdev->agreed_pro_version >= 87 && mdev->integrity_r_tfm) ?
1472 crypto_hash_digestsize(mdev->integrity_r_tfm) : 0;
1473
1474 if (dgs) {
1475 rr = drbd_recv(mdev, dig_in, dgs);
1476 if (rr != dgs) {
1477 dev_warn(DEV, "short read receiving data reply digest: read %d expected %d\n",
1478 rr, dgs);
1479 return 0;
1480 }
1481 }
1482
1483 data_size -= dgs;
1484
1485 /* optimistically update recv_cnt. if receiving fails below,
1486 * we disconnect anyways, and counters will be reset. */
1487 mdev->recv_cnt += data_size>>9;
1488
1489 bio = req->master_bio;
1490 D_ASSERT(sector == bio->bi_sector);
1491
1492 bio_for_each_segment(bvec, bio, i) {
1493 expect = min_t(int, data_size, bvec->bv_len);
1494 rr = drbd_recv(mdev,
1495 kmap(bvec->bv_page)+bvec->bv_offset,
1496 expect);
1497 kunmap(bvec->bv_page);
1498 if (rr != expect) {
1499 dev_warn(DEV, "short read receiving data reply: "
1500 "read %d expected %d\n",
1501 rr, expect);
1502 return 0;
1503 }
1504 data_size -= rr;
1505 }
1506
1507 if (dgs) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001508 drbd_csum_bio(mdev, mdev->integrity_r_tfm, bio, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001509 if (memcmp(dig_in, dig_vv, dgs)) {
1510 dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n");
1511 return 0;
1512 }
1513 }
1514
1515 D_ASSERT(data_size == 0);
1516 return 1;
1517}
1518
1519/* e_end_resync_block() is called via
1520 * drbd_process_done_ee() by asender only */
1521static int e_end_resync_block(struct drbd_conf *mdev, struct drbd_work *w, int unused)
1522{
1523 struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w;
1524 sector_t sector = e->sector;
1525 int ok;
1526
1527 D_ASSERT(hlist_unhashed(&e->colision));
1528
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001529 if (likely((e->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001530 drbd_set_in_sync(mdev, sector, e->size);
1531 ok = drbd_send_ack(mdev, P_RS_WRITE_ACK, e);
1532 } else {
1533 /* Record failure to sync */
1534 drbd_rs_failed_io(mdev, sector, e->size);
1535
1536 ok = drbd_send_ack(mdev, P_NEG_ACK, e);
1537 }
1538 dec_unacked(mdev);
1539
1540 return ok;
1541}
1542
1543static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local)
1544{
1545 struct drbd_epoch_entry *e;
1546
1547 e = read_in_block(mdev, ID_SYNCER, sector, data_size);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001548 if (!e)
1549 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001550
1551 dec_rs_pending(mdev);
1552
Philipp Reisnerb411b362009-09-25 16:07:19 -07001553 inc_unacked(mdev);
1554 /* corresponding dec_unacked() in e_end_resync_block()
1555 * respective _drbd_clear_done_ee */
1556
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001557 e->w.cb = e_end_resync_block;
1558
Philipp Reisnerb411b362009-09-25 16:07:19 -07001559 spin_lock_irq(&mdev->req_lock);
1560 list_add(&e->w.list, &mdev->sync_ee);
1561 spin_unlock_irq(&mdev->req_lock);
1562
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001563 atomic_add(data_size >> 9, &mdev->rs_sect_ev);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001564 if (drbd_submit_ee(mdev, e, WRITE, DRBD_FAULT_RS_WR) == 0)
1565 return TRUE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001566
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001567 drbd_free_ee(mdev, e);
1568fail:
1569 put_ldev(mdev);
1570 return FALSE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001571}
1572
Philipp Reisner02918be2010-08-20 14:35:10 +02001573static int receive_DataReply(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001574{
1575 struct drbd_request *req;
1576 sector_t sector;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001577 int ok;
Philipp Reisner02918be2010-08-20 14:35:10 +02001578 struct p_data *p = &mdev->data.rbuf.data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001579
1580 sector = be64_to_cpu(p->sector);
1581
1582 spin_lock_irq(&mdev->req_lock);
1583 req = _ar_id_to_req(mdev, p->block_id, sector);
1584 spin_unlock_irq(&mdev->req_lock);
1585 if (unlikely(!req)) {
1586 dev_err(DEV, "Got a corrupt block_id/sector pair(1).\n");
1587 return FALSE;
1588 }
1589
1590 /* hlist_del(&req->colision) is done in _req_may_be_done, to avoid
1591 * special casing it there for the various failure cases.
1592 * still no race with drbd_fail_pending_reads */
1593 ok = recv_dless_read(mdev, req, sector, data_size);
1594
1595 if (ok)
1596 req_mod(req, data_received);
1597 /* else: nothing. handled from drbd_disconnect...
1598 * I don't think we may complete this just yet
1599 * in case we are "on-disconnect: freeze" */
1600
1601 return ok;
1602}
1603
Philipp Reisner02918be2010-08-20 14:35:10 +02001604static int receive_RSDataReply(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001605{
1606 sector_t sector;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001607 int ok;
Philipp Reisner02918be2010-08-20 14:35:10 +02001608 struct p_data *p = &mdev->data.rbuf.data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001609
1610 sector = be64_to_cpu(p->sector);
1611 D_ASSERT(p->block_id == ID_SYNCER);
1612
1613 if (get_ldev(mdev)) {
1614 /* data is submitted to disk within recv_resync_read.
1615 * corresponding put_ldev done below on error,
1616 * or in drbd_endio_write_sec. */
1617 ok = recv_resync_read(mdev, sector, data_size);
1618 } else {
1619 if (__ratelimit(&drbd_ratelimit_state))
1620 dev_err(DEV, "Can not write resync data to local disk.\n");
1621
1622 ok = drbd_drain_block(mdev, data_size);
1623
1624 drbd_send_ack_dp(mdev, P_NEG_ACK, p);
1625 }
1626
Philipp Reisner778f2712010-07-06 11:14:00 +02001627 atomic_add(data_size >> 9, &mdev->rs_sect_in);
1628
Philipp Reisnerb411b362009-09-25 16:07:19 -07001629 return ok;
1630}
1631
1632/* e_end_block() is called via drbd_process_done_ee().
1633 * this means this function only runs in the asender thread
1634 */
1635static int e_end_block(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1636{
1637 struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w;
1638 sector_t sector = e->sector;
1639 struct drbd_epoch *epoch;
1640 int ok = 1, pcmd;
1641
1642 if (e->flags & EE_IS_BARRIER) {
1643 epoch = previous_epoch(mdev, e->epoch);
1644 if (epoch)
1645 drbd_may_finish_epoch(mdev, epoch, EV_BARRIER_DONE + (cancel ? EV_CLEANUP : 0));
1646 }
1647
1648 if (mdev->net_conf->wire_protocol == DRBD_PROT_C) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001649 if (likely((e->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001650 pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
1651 mdev->state.conn <= C_PAUSED_SYNC_T &&
1652 e->flags & EE_MAY_SET_IN_SYNC) ?
1653 P_RS_WRITE_ACK : P_WRITE_ACK;
1654 ok &= drbd_send_ack(mdev, pcmd, e);
1655 if (pcmd == P_RS_WRITE_ACK)
1656 drbd_set_in_sync(mdev, sector, e->size);
1657 } else {
1658 ok = drbd_send_ack(mdev, P_NEG_ACK, e);
1659 /* we expect it to be marked out of sync anyways...
1660 * maybe assert this? */
1661 }
1662 dec_unacked(mdev);
1663 }
1664 /* we delete from the conflict detection hash _after_ we sent out the
1665 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
1666 if (mdev->net_conf->two_primaries) {
1667 spin_lock_irq(&mdev->req_lock);
1668 D_ASSERT(!hlist_unhashed(&e->colision));
1669 hlist_del_init(&e->colision);
1670 spin_unlock_irq(&mdev->req_lock);
1671 } else {
1672 D_ASSERT(hlist_unhashed(&e->colision));
1673 }
1674
1675 drbd_may_finish_epoch(mdev, e->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
1676
1677 return ok;
1678}
1679
1680static int e_send_discard_ack(struct drbd_conf *mdev, struct drbd_work *w, int unused)
1681{
1682 struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w;
1683 int ok = 1;
1684
1685 D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
1686 ok = drbd_send_ack(mdev, P_DISCARD_ACK, e);
1687
1688 spin_lock_irq(&mdev->req_lock);
1689 D_ASSERT(!hlist_unhashed(&e->colision));
1690 hlist_del_init(&e->colision);
1691 spin_unlock_irq(&mdev->req_lock);
1692
1693 dec_unacked(mdev);
1694
1695 return ok;
1696}
1697
1698/* Called from receive_Data.
1699 * Synchronize packets on sock with packets on msock.
1700 *
1701 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
1702 * packet traveling on msock, they are still processed in the order they have
1703 * been sent.
1704 *
1705 * Note: we don't care for Ack packets overtaking P_DATA packets.
1706 *
1707 * In case packet_seq is larger than mdev->peer_seq number, there are
1708 * outstanding packets on the msock. We wait for them to arrive.
1709 * In case we are the logically next packet, we update mdev->peer_seq
1710 * ourselves. Correctly handles 32bit wrap around.
1711 *
1712 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
1713 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
1714 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
1715 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
1716 *
1717 * returns 0 if we may process the packet,
1718 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
1719static int drbd_wait_peer_seq(struct drbd_conf *mdev, const u32 packet_seq)
1720{
1721 DEFINE_WAIT(wait);
1722 unsigned int p_seq;
1723 long timeout;
1724 int ret = 0;
1725 spin_lock(&mdev->peer_seq_lock);
1726 for (;;) {
1727 prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
1728 if (seq_le(packet_seq, mdev->peer_seq+1))
1729 break;
1730 if (signal_pending(current)) {
1731 ret = -ERESTARTSYS;
1732 break;
1733 }
1734 p_seq = mdev->peer_seq;
1735 spin_unlock(&mdev->peer_seq_lock);
1736 timeout = schedule_timeout(30*HZ);
1737 spin_lock(&mdev->peer_seq_lock);
1738 if (timeout == 0 && p_seq == mdev->peer_seq) {
1739 ret = -ETIMEDOUT;
1740 dev_err(DEV, "ASSERT FAILED waited 30 seconds for sequence update, forcing reconnect\n");
1741 break;
1742 }
1743 }
1744 finish_wait(&mdev->seq_wait, &wait);
1745 if (mdev->peer_seq+1 == packet_seq)
1746 mdev->peer_seq++;
1747 spin_unlock(&mdev->peer_seq_lock);
1748 return ret;
1749}
1750
1751/* mirrored write */
Philipp Reisner02918be2010-08-20 14:35:10 +02001752static int receive_Data(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001753{
1754 sector_t sector;
1755 struct drbd_epoch_entry *e;
Philipp Reisner02918be2010-08-20 14:35:10 +02001756 struct p_data *p = &mdev->data.rbuf.data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001757 int rw = WRITE;
1758 u32 dp_flags;
1759
Philipp Reisnerb411b362009-09-25 16:07:19 -07001760 if (!get_ldev(mdev)) {
1761 if (__ratelimit(&drbd_ratelimit_state))
1762 dev_err(DEV, "Can not write mirrored data block "
1763 "to local disk.\n");
1764 spin_lock(&mdev->peer_seq_lock);
1765 if (mdev->peer_seq+1 == be32_to_cpu(p->seq_num))
1766 mdev->peer_seq++;
1767 spin_unlock(&mdev->peer_seq_lock);
1768
1769 drbd_send_ack_dp(mdev, P_NEG_ACK, p);
1770 atomic_inc(&mdev->current_epoch->epoch_size);
1771 return drbd_drain_block(mdev, data_size);
1772 }
1773
1774 /* get_ldev(mdev) successful.
1775 * Corresponding put_ldev done either below (on various errors),
1776 * or in drbd_endio_write_sec, if we successfully submit the data at
1777 * the end of this function. */
1778
1779 sector = be64_to_cpu(p->sector);
1780 e = read_in_block(mdev, p->block_id, sector, data_size);
1781 if (!e) {
1782 put_ldev(mdev);
1783 return FALSE;
1784 }
1785
Philipp Reisnerb411b362009-09-25 16:07:19 -07001786 e->w.cb = e_end_block;
1787
1788 spin_lock(&mdev->epoch_lock);
1789 e->epoch = mdev->current_epoch;
1790 atomic_inc(&e->epoch->epoch_size);
1791 atomic_inc(&e->epoch->active);
1792
1793 if (mdev->write_ordering == WO_bio_barrier && atomic_read(&e->epoch->epoch_size) == 1) {
1794 struct drbd_epoch *epoch;
1795 /* Issue a barrier if we start a new epoch, and the previous epoch
1796 was not a epoch containing a single request which already was
1797 a Barrier. */
1798 epoch = list_entry(e->epoch->list.prev, struct drbd_epoch, list);
1799 if (epoch == e->epoch) {
1800 set_bit(DE_CONTAINS_A_BARRIER, &e->epoch->flags);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001801 rw |= REQ_HARDBARRIER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001802 e->flags |= EE_IS_BARRIER;
1803 } else {
1804 if (atomic_read(&epoch->epoch_size) > 1 ||
1805 !test_bit(DE_CONTAINS_A_BARRIER, &epoch->flags)) {
1806 set_bit(DE_BARRIER_IN_NEXT_EPOCH_ISSUED, &epoch->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001807 set_bit(DE_CONTAINS_A_BARRIER, &e->epoch->flags);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001808 rw |= REQ_HARDBARRIER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001809 e->flags |= EE_IS_BARRIER;
1810 }
1811 }
1812 }
1813 spin_unlock(&mdev->epoch_lock);
1814
1815 dp_flags = be32_to_cpu(p->dp_flags);
1816 if (dp_flags & DP_HARDBARRIER) {
1817 dev_err(DEV, "ASSERT FAILED would have submitted barrier request\n");
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001818 /* rw |= REQ_HARDBARRIER; */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001819 }
1820 if (dp_flags & DP_RW_SYNC)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001821 rw |= REQ_SYNC | REQ_UNPLUG;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001822 if (dp_flags & DP_MAY_SET_IN_SYNC)
1823 e->flags |= EE_MAY_SET_IN_SYNC;
1824
1825 /* I'm the receiver, I do hold a net_cnt reference. */
1826 if (!mdev->net_conf->two_primaries) {
1827 spin_lock_irq(&mdev->req_lock);
1828 } else {
1829 /* don't get the req_lock yet,
1830 * we may sleep in drbd_wait_peer_seq */
1831 const int size = e->size;
1832 const int discard = test_bit(DISCARD_CONCURRENT, &mdev->flags);
1833 DEFINE_WAIT(wait);
1834 struct drbd_request *i;
1835 struct hlist_node *n;
1836 struct hlist_head *slot;
1837 int first;
1838
1839 D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
1840 BUG_ON(mdev->ee_hash == NULL);
1841 BUG_ON(mdev->tl_hash == NULL);
1842
1843 /* conflict detection and handling:
1844 * 1. wait on the sequence number,
1845 * in case this data packet overtook ACK packets.
1846 * 2. check our hash tables for conflicting requests.
1847 * we only need to walk the tl_hash, since an ee can not
1848 * have a conflict with an other ee: on the submitting
1849 * node, the corresponding req had already been conflicting,
1850 * and a conflicting req is never sent.
1851 *
1852 * Note: for two_primaries, we are protocol C,
1853 * so there cannot be any request that is DONE
1854 * but still on the transfer log.
1855 *
1856 * unconditionally add to the ee_hash.
1857 *
1858 * if no conflicting request is found:
1859 * submit.
1860 *
1861 * if any conflicting request is found
1862 * that has not yet been acked,
1863 * AND I have the "discard concurrent writes" flag:
1864 * queue (via done_ee) the P_DISCARD_ACK; OUT.
1865 *
1866 * if any conflicting request is found:
1867 * block the receiver, waiting on misc_wait
1868 * until no more conflicting requests are there,
1869 * or we get interrupted (disconnect).
1870 *
1871 * we do not just write after local io completion of those
1872 * requests, but only after req is done completely, i.e.
1873 * we wait for the P_DISCARD_ACK to arrive!
1874 *
1875 * then proceed normally, i.e. submit.
1876 */
1877 if (drbd_wait_peer_seq(mdev, be32_to_cpu(p->seq_num)))
1878 goto out_interrupted;
1879
1880 spin_lock_irq(&mdev->req_lock);
1881
1882 hlist_add_head(&e->colision, ee_hash_slot(mdev, sector));
1883
1884#define OVERLAPS overlaps(i->sector, i->size, sector, size)
1885 slot = tl_hash_slot(mdev, sector);
1886 first = 1;
1887 for (;;) {
1888 int have_unacked = 0;
1889 int have_conflict = 0;
1890 prepare_to_wait(&mdev->misc_wait, &wait,
1891 TASK_INTERRUPTIBLE);
1892 hlist_for_each_entry(i, n, slot, colision) {
1893 if (OVERLAPS) {
1894 /* only ALERT on first iteration,
1895 * we may be woken up early... */
1896 if (first)
1897 dev_alert(DEV, "%s[%u] Concurrent local write detected!"
1898 " new: %llus +%u; pending: %llus +%u\n",
1899 current->comm, current->pid,
1900 (unsigned long long)sector, size,
1901 (unsigned long long)i->sector, i->size);
1902 if (i->rq_state & RQ_NET_PENDING)
1903 ++have_unacked;
1904 ++have_conflict;
1905 }
1906 }
1907#undef OVERLAPS
1908 if (!have_conflict)
1909 break;
1910
1911 /* Discard Ack only for the _first_ iteration */
1912 if (first && discard && have_unacked) {
1913 dev_alert(DEV, "Concurrent write! [DISCARD BY FLAG] sec=%llus\n",
1914 (unsigned long long)sector);
1915 inc_unacked(mdev);
1916 e->w.cb = e_send_discard_ack;
1917 list_add_tail(&e->w.list, &mdev->done_ee);
1918
1919 spin_unlock_irq(&mdev->req_lock);
1920
1921 /* we could probably send that P_DISCARD_ACK ourselves,
1922 * but I don't like the receiver using the msock */
1923
1924 put_ldev(mdev);
1925 wake_asender(mdev);
1926 finish_wait(&mdev->misc_wait, &wait);
1927 return TRUE;
1928 }
1929
1930 if (signal_pending(current)) {
1931 hlist_del_init(&e->colision);
1932
1933 spin_unlock_irq(&mdev->req_lock);
1934
1935 finish_wait(&mdev->misc_wait, &wait);
1936 goto out_interrupted;
1937 }
1938
1939 spin_unlock_irq(&mdev->req_lock);
1940 if (first) {
1941 first = 0;
1942 dev_alert(DEV, "Concurrent write! [W AFTERWARDS] "
1943 "sec=%llus\n", (unsigned long long)sector);
1944 } else if (discard) {
1945 /* we had none on the first iteration.
1946 * there must be none now. */
1947 D_ASSERT(have_unacked == 0);
1948 }
1949 schedule();
1950 spin_lock_irq(&mdev->req_lock);
1951 }
1952 finish_wait(&mdev->misc_wait, &wait);
1953 }
1954
1955 list_add(&e->w.list, &mdev->active_ee);
1956 spin_unlock_irq(&mdev->req_lock);
1957
1958 switch (mdev->net_conf->wire_protocol) {
1959 case DRBD_PROT_C:
1960 inc_unacked(mdev);
1961 /* corresponding dec_unacked() in e_end_block()
1962 * respective _drbd_clear_done_ee */
1963 break;
1964 case DRBD_PROT_B:
1965 /* I really don't like it that the receiver thread
1966 * sends on the msock, but anyways */
1967 drbd_send_ack(mdev, P_RECV_ACK, e);
1968 break;
1969 case DRBD_PROT_A:
1970 /* nothing to do */
1971 break;
1972 }
1973
1974 if (mdev->state.pdsk == D_DISKLESS) {
1975 /* In case we have the only disk of the cluster, */
1976 drbd_set_out_of_sync(mdev, e->sector, e->size);
1977 e->flags |= EE_CALL_AL_COMPLETE_IO;
1978 drbd_al_begin_io(mdev, e->sector);
1979 }
1980
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001981 if (drbd_submit_ee(mdev, e, rw, DRBD_FAULT_DT_WR) == 0)
1982 return TRUE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001983
1984out_interrupted:
1985 /* yes, the epoch_size now is imbalanced.
1986 * but we drop the connection anyways, so we don't have a chance to
1987 * receive a barrier... atomic_inc(&mdev->epoch_size); */
1988 put_ldev(mdev);
1989 drbd_free_ee(mdev, e);
1990 return FALSE;
1991}
1992
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001993/* We may throttle resync, if the lower device seems to be busy,
1994 * and current sync rate is above c_min_rate.
1995 *
1996 * To decide whether or not the lower device is busy, we use a scheme similar
1997 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
1998 * (more than 64 sectors) of activity we cannot account for with our own resync
1999 * activity, it obviously is "busy".
2000 *
2001 * The current sync rate used here uses only the most recent two step marks,
2002 * to have a short time average so we can react faster.
2003 */
2004int drbd_rs_should_slow_down(struct drbd_conf *mdev)
2005{
2006 struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
2007 unsigned long db, dt, dbdt;
2008 int curr_events;
2009 int throttle = 0;
2010
2011 /* feature disabled? */
2012 if (mdev->sync_conf.c_min_rate == 0)
2013 return 0;
2014
2015 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
2016 (int)part_stat_read(&disk->part0, sectors[1]) -
2017 atomic_read(&mdev->rs_sect_ev);
2018 if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
2019 unsigned long rs_left;
2020 int i;
2021
2022 mdev->rs_last_events = curr_events;
2023
2024 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
2025 * approx. */
2026 i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-2) % DRBD_SYNC_MARKS;
2027 rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
2028
2029 dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
2030 if (!dt)
2031 dt++;
2032 db = mdev->rs_mark_left[i] - rs_left;
2033 dbdt = Bit2KB(db/dt);
2034
2035 if (dbdt > mdev->sync_conf.c_min_rate)
2036 throttle = 1;
2037 }
2038 return throttle;
2039}
2040
2041
Philipp Reisner02918be2010-08-20 14:35:10 +02002042static int receive_DataRequest(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int digest_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002043{
2044 sector_t sector;
2045 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
2046 struct drbd_epoch_entry *e;
2047 struct digest_info *di = NULL;
Philipp Reisner02918be2010-08-20 14:35:10 +02002048 int size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002049 unsigned int fault_type;
Philipp Reisner02918be2010-08-20 14:35:10 +02002050 struct p_block_req *p = &mdev->data.rbuf.block_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002051
2052 sector = be64_to_cpu(p->sector);
2053 size = be32_to_cpu(p->blksize);
2054
2055 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_SEGMENT_SIZE) {
2056 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2057 (unsigned long long)sector, size);
2058 return FALSE;
2059 }
2060 if (sector + (size>>9) > capacity) {
2061 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2062 (unsigned long long)sector, size);
2063 return FALSE;
2064 }
2065
2066 if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
2067 if (__ratelimit(&drbd_ratelimit_state))
2068 dev_err(DEV, "Can not satisfy peer's read request, "
2069 "no local data.\n");
Philipp Reisner02918be2010-08-20 14:35:10 +02002070 drbd_send_ack_rp(mdev, cmd == P_DATA_REQUEST ? P_NEG_DREPLY :
Philipp Reisnerb411b362009-09-25 16:07:19 -07002071 P_NEG_RS_DREPLY , p);
Philipp Reisner02918be2010-08-20 14:35:10 +02002072 return TRUE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002073 }
2074
2075 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
2076 * "criss-cross" setup, that might cause write-out on some other DRBD,
2077 * which in turn might block on the other node at this very place. */
2078 e = drbd_alloc_ee(mdev, p->block_id, sector, size, GFP_NOIO);
2079 if (!e) {
2080 put_ldev(mdev);
2081 return FALSE;
2082 }
2083
Philipp Reisner02918be2010-08-20 14:35:10 +02002084 switch (cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002085 case P_DATA_REQUEST:
2086 e->w.cb = w_e_end_data_req;
2087 fault_type = DRBD_FAULT_DT_RD;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002088 /* application IO, don't drbd_rs_begin_io */
2089 goto submit;
2090
Philipp Reisnerb411b362009-09-25 16:07:19 -07002091 case P_RS_DATA_REQUEST:
2092 e->w.cb = w_e_end_rsdata_req;
2093 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002094 break;
2095
2096 case P_OV_REPLY:
2097 case P_CSUM_RS_REQUEST:
2098 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002099 di = kmalloc(sizeof(*di) + digest_size, GFP_NOIO);
2100 if (!di)
2101 goto out_free_e;
2102
2103 di->digest_size = digest_size;
2104 di->digest = (((char *)di)+sizeof(struct digest_info));
2105
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +02002106 e->digest = di;
2107 e->flags |= EE_HAS_DIGEST;
2108
Philipp Reisnerb411b362009-09-25 16:07:19 -07002109 if (drbd_recv(mdev, di->digest, digest_size) != digest_size)
2110 goto out_free_e;
2111
Philipp Reisner02918be2010-08-20 14:35:10 +02002112 if (cmd == P_CSUM_RS_REQUEST) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002113 D_ASSERT(mdev->agreed_pro_version >= 89);
2114 e->w.cb = w_e_end_csum_rs_req;
Philipp Reisner02918be2010-08-20 14:35:10 +02002115 } else if (cmd == P_OV_REPLY) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002116 e->w.cb = w_e_end_ov_reply;
2117 dec_rs_pending(mdev);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002118 /* drbd_rs_begin_io done when we sent this request,
2119 * but accounting still needs to be done. */
2120 goto submit_for_resync;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002121 }
2122 break;
2123
2124 case P_OV_REQUEST:
2125 if (mdev->state.conn >= C_CONNECTED &&
2126 mdev->state.conn != C_VERIFY_T)
2127 dev_warn(DEV, "ASSERT FAILED: got P_OV_REQUEST while being %s\n",
2128 drbd_conn_str(mdev->state.conn));
2129 if (mdev->ov_start_sector == ~(sector_t)0 &&
2130 mdev->agreed_pro_version >= 90) {
2131 mdev->ov_start_sector = sector;
2132 mdev->ov_position = sector;
2133 mdev->ov_left = mdev->rs_total - BM_SECT_TO_BIT(sector);
2134 dev_info(DEV, "Online Verify start sector: %llu\n",
2135 (unsigned long long)sector);
2136 }
2137 e->w.cb = w_e_end_ov_req;
2138 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002139 break;
2140
Philipp Reisnerb411b362009-09-25 16:07:19 -07002141 default:
2142 dev_err(DEV, "unexpected command (%s) in receive_DataRequest\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02002143 cmdname(cmd));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002144 fault_type = DRBD_FAULT_MAX;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002145 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002146 }
2147
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002148 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2149 * wrt the receiver, but it is not as straightforward as it may seem.
2150 * Various places in the resync start and stop logic assume resync
2151 * requests are processed in order, requeuing this on the worker thread
2152 * introduces a bunch of new code for synchronization between threads.
2153 *
2154 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2155 * "forever", throttling after drbd_rs_begin_io will lock that extent
2156 * for application writes for the same time. For now, just throttle
2157 * here, where the rest of the code expects the receiver to sleep for
2158 * a while, anyways.
2159 */
2160
2161 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2162 * this defers syncer requests for some time, before letting at least
2163 * on request through. The resync controller on the receiving side
2164 * will adapt to the incoming rate accordingly.
2165 *
2166 * We cannot throttle here if remote is Primary/SyncTarget:
2167 * we would also throttle its application reads.
2168 * In that case, throttling is done on the SyncTarget only.
2169 */
2170 if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev))
2171 msleep(100);
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002172 if (drbd_rs_begin_io(mdev, e->sector))
2173 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002174
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002175submit_for_resync:
2176 atomic_add(size >> 9, &mdev->rs_sect_ev);
2177
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002178submit:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002179 inc_unacked(mdev);
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002180 spin_lock_irq(&mdev->req_lock);
2181 list_add_tail(&e->w.list, &mdev->read_ee);
2182 spin_unlock_irq(&mdev->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002183
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002184 if (drbd_submit_ee(mdev, e, READ, fault_type) == 0)
2185 return TRUE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002186
2187out_free_e:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002188 put_ldev(mdev);
2189 drbd_free_ee(mdev, e);
2190 return FALSE;
2191}
2192
2193static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
2194{
2195 int self, peer, rv = -100;
2196 unsigned long ch_self, ch_peer;
2197
2198 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2199 peer = mdev->p_uuid[UI_BITMAP] & 1;
2200
2201 ch_peer = mdev->p_uuid[UI_SIZE];
2202 ch_self = mdev->comm_bm_set;
2203
2204 switch (mdev->net_conf->after_sb_0p) {
2205 case ASB_CONSENSUS:
2206 case ASB_DISCARD_SECONDARY:
2207 case ASB_CALL_HELPER:
2208 dev_err(DEV, "Configuration error.\n");
2209 break;
2210 case ASB_DISCONNECT:
2211 break;
2212 case ASB_DISCARD_YOUNGER_PRI:
2213 if (self == 0 && peer == 1) {
2214 rv = -1;
2215 break;
2216 }
2217 if (self == 1 && peer == 0) {
2218 rv = 1;
2219 break;
2220 }
2221 /* Else fall through to one of the other strategies... */
2222 case ASB_DISCARD_OLDER_PRI:
2223 if (self == 0 && peer == 1) {
2224 rv = 1;
2225 break;
2226 }
2227 if (self == 1 && peer == 0) {
2228 rv = -1;
2229 break;
2230 }
2231 /* Else fall through to one of the other strategies... */
Lars Ellenbergad19bf62009-10-14 09:36:49 +02002232 dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
Philipp Reisnerb411b362009-09-25 16:07:19 -07002233 "Using discard-least-changes instead\n");
2234 case ASB_DISCARD_ZERO_CHG:
2235 if (ch_peer == 0 && ch_self == 0) {
2236 rv = test_bit(DISCARD_CONCURRENT, &mdev->flags)
2237 ? -1 : 1;
2238 break;
2239 } else {
2240 if (ch_peer == 0) { rv = 1; break; }
2241 if (ch_self == 0) { rv = -1; break; }
2242 }
2243 if (mdev->net_conf->after_sb_0p == ASB_DISCARD_ZERO_CHG)
2244 break;
2245 case ASB_DISCARD_LEAST_CHG:
2246 if (ch_self < ch_peer)
2247 rv = -1;
2248 else if (ch_self > ch_peer)
2249 rv = 1;
2250 else /* ( ch_self == ch_peer ) */
2251 /* Well, then use something else. */
2252 rv = test_bit(DISCARD_CONCURRENT, &mdev->flags)
2253 ? -1 : 1;
2254 break;
2255 case ASB_DISCARD_LOCAL:
2256 rv = -1;
2257 break;
2258 case ASB_DISCARD_REMOTE:
2259 rv = 1;
2260 }
2261
2262 return rv;
2263}
2264
2265static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
2266{
2267 int self, peer, hg, rv = -100;
2268
2269 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2270 peer = mdev->p_uuid[UI_BITMAP] & 1;
2271
2272 switch (mdev->net_conf->after_sb_1p) {
2273 case ASB_DISCARD_YOUNGER_PRI:
2274 case ASB_DISCARD_OLDER_PRI:
2275 case ASB_DISCARD_LEAST_CHG:
2276 case ASB_DISCARD_LOCAL:
2277 case ASB_DISCARD_REMOTE:
2278 dev_err(DEV, "Configuration error.\n");
2279 break;
2280 case ASB_DISCONNECT:
2281 break;
2282 case ASB_CONSENSUS:
2283 hg = drbd_asb_recover_0p(mdev);
2284 if (hg == -1 && mdev->state.role == R_SECONDARY)
2285 rv = hg;
2286 if (hg == 1 && mdev->state.role == R_PRIMARY)
2287 rv = hg;
2288 break;
2289 case ASB_VIOLENTLY:
2290 rv = drbd_asb_recover_0p(mdev);
2291 break;
2292 case ASB_DISCARD_SECONDARY:
2293 return mdev->state.role == R_PRIMARY ? 1 : -1;
2294 case ASB_CALL_HELPER:
2295 hg = drbd_asb_recover_0p(mdev);
2296 if (hg == -1 && mdev->state.role == R_PRIMARY) {
2297 self = drbd_set_role(mdev, R_SECONDARY, 0);
2298 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2299 * we might be here in C_WF_REPORT_PARAMS which is transient.
2300 * we do not need to wait for the after state change work either. */
2301 self = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2302 if (self != SS_SUCCESS) {
2303 drbd_khelper(mdev, "pri-lost-after-sb");
2304 } else {
2305 dev_warn(DEV, "Successfully gave up primary role.\n");
2306 rv = hg;
2307 }
2308 } else
2309 rv = hg;
2310 }
2311
2312 return rv;
2313}
2314
2315static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
2316{
2317 int self, peer, hg, rv = -100;
2318
2319 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2320 peer = mdev->p_uuid[UI_BITMAP] & 1;
2321
2322 switch (mdev->net_conf->after_sb_2p) {
2323 case ASB_DISCARD_YOUNGER_PRI:
2324 case ASB_DISCARD_OLDER_PRI:
2325 case ASB_DISCARD_LEAST_CHG:
2326 case ASB_DISCARD_LOCAL:
2327 case ASB_DISCARD_REMOTE:
2328 case ASB_CONSENSUS:
2329 case ASB_DISCARD_SECONDARY:
2330 dev_err(DEV, "Configuration error.\n");
2331 break;
2332 case ASB_VIOLENTLY:
2333 rv = drbd_asb_recover_0p(mdev);
2334 break;
2335 case ASB_DISCONNECT:
2336 break;
2337 case ASB_CALL_HELPER:
2338 hg = drbd_asb_recover_0p(mdev);
2339 if (hg == -1) {
2340 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2341 * we might be here in C_WF_REPORT_PARAMS which is transient.
2342 * we do not need to wait for the after state change work either. */
2343 self = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2344 if (self != SS_SUCCESS) {
2345 drbd_khelper(mdev, "pri-lost-after-sb");
2346 } else {
2347 dev_warn(DEV, "Successfully gave up primary role.\n");
2348 rv = hg;
2349 }
2350 } else
2351 rv = hg;
2352 }
2353
2354 return rv;
2355}
2356
2357static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
2358 u64 bits, u64 flags)
2359{
2360 if (!uuid) {
2361 dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
2362 return;
2363 }
2364 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
2365 text,
2366 (unsigned long long)uuid[UI_CURRENT],
2367 (unsigned long long)uuid[UI_BITMAP],
2368 (unsigned long long)uuid[UI_HISTORY_START],
2369 (unsigned long long)uuid[UI_HISTORY_END],
2370 (unsigned long long)bits,
2371 (unsigned long long)flags);
2372}
2373
2374/*
2375 100 after split brain try auto recover
2376 2 C_SYNC_SOURCE set BitMap
2377 1 C_SYNC_SOURCE use BitMap
2378 0 no Sync
2379 -1 C_SYNC_TARGET use BitMap
2380 -2 C_SYNC_TARGET set BitMap
2381 -100 after split brain, disconnect
2382-1000 unrelated data
2383 */
2384static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
2385{
2386 u64 self, peer;
2387 int i, j;
2388
2389 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2390 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2391
2392 *rule_nr = 10;
2393 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
2394 return 0;
2395
2396 *rule_nr = 20;
2397 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
2398 peer != UUID_JUST_CREATED)
2399 return -2;
2400
2401 *rule_nr = 30;
2402 if (self != UUID_JUST_CREATED &&
2403 (peer == UUID_JUST_CREATED || peer == (u64)0))
2404 return 2;
2405
2406 if (self == peer) {
2407 int rct, dc; /* roles at crash time */
2408
2409 if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
2410
2411 if (mdev->agreed_pro_version < 91)
2412 return -1001;
2413
2414 if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
2415 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
2416 dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
2417 drbd_uuid_set_bm(mdev, 0UL);
2418
2419 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2420 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2421 *rule_nr = 34;
2422 } else {
2423 dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
2424 *rule_nr = 36;
2425 }
2426
2427 return 1;
2428 }
2429
2430 if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
2431
2432 if (mdev->agreed_pro_version < 91)
2433 return -1001;
2434
2435 if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
2436 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
2437 dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
2438
2439 mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
2440 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
2441 mdev->p_uuid[UI_BITMAP] = 0UL;
2442
2443 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2444 *rule_nr = 35;
2445 } else {
2446 dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
2447 *rule_nr = 37;
2448 }
2449
2450 return -1;
2451 }
2452
2453 /* Common power [off|failure] */
2454 rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
2455 (mdev->p_uuid[UI_FLAGS] & 2);
2456 /* lowest bit is set when we were primary,
2457 * next bit (weight 2) is set when peer was primary */
2458 *rule_nr = 40;
2459
2460 switch (rct) {
2461 case 0: /* !self_pri && !peer_pri */ return 0;
2462 case 1: /* self_pri && !peer_pri */ return 1;
2463 case 2: /* !self_pri && peer_pri */ return -1;
2464 case 3: /* self_pri && peer_pri */
2465 dc = test_bit(DISCARD_CONCURRENT, &mdev->flags);
2466 return dc ? -1 : 1;
2467 }
2468 }
2469
2470 *rule_nr = 50;
2471 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2472 if (self == peer)
2473 return -1;
2474
2475 *rule_nr = 51;
2476 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2477 if (self == peer) {
2478 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2479 peer = mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1);
2480 if (self == peer) {
2481 /* The last P_SYNC_UUID did not get though. Undo the last start of
2482 resync as sync source modifications of the peer's UUIDs. */
2483
2484 if (mdev->agreed_pro_version < 91)
2485 return -1001;
2486
2487 mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
2488 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
2489 return -1;
2490 }
2491 }
2492
2493 *rule_nr = 60;
2494 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2495 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2496 peer = mdev->p_uuid[i] & ~((u64)1);
2497 if (self == peer)
2498 return -2;
2499 }
2500
2501 *rule_nr = 70;
2502 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2503 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2504 if (self == peer)
2505 return 1;
2506
2507 *rule_nr = 71;
2508 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2509 if (self == peer) {
2510 self = mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1);
2511 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2512 if (self == peer) {
2513 /* The last P_SYNC_UUID did not get though. Undo the last start of
2514 resync as sync source modifications of our UUIDs. */
2515
2516 if (mdev->agreed_pro_version < 91)
2517 return -1001;
2518
2519 _drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
2520 _drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
2521
2522 dev_info(DEV, "Undid last start of resync:\n");
2523
2524 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2525 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2526
2527 return 1;
2528 }
2529 }
2530
2531
2532 *rule_nr = 80;
Philipp Reisnerd8c2a362009-11-18 15:52:51 +01002533 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002534 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2535 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2536 if (self == peer)
2537 return 2;
2538 }
2539
2540 *rule_nr = 90;
2541 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2542 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2543 if (self == peer && self != ((u64)0))
2544 return 100;
2545
2546 *rule_nr = 100;
2547 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2548 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2549 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
2550 peer = mdev->p_uuid[j] & ~((u64)1);
2551 if (self == peer)
2552 return -100;
2553 }
2554 }
2555
2556 return -1000;
2557}
2558
2559/* drbd_sync_handshake() returns the new conn state on success, or
2560 CONN_MASK (-1) on failure.
2561 */
2562static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
2563 enum drbd_disk_state peer_disk) __must_hold(local)
2564{
2565 int hg, rule_nr;
2566 enum drbd_conns rv = C_MASK;
2567 enum drbd_disk_state mydisk;
2568
2569 mydisk = mdev->state.disk;
2570 if (mydisk == D_NEGOTIATING)
2571 mydisk = mdev->new_state_tmp.disk;
2572
2573 dev_info(DEV, "drbd_sync_handshake:\n");
2574 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0);
2575 drbd_uuid_dump(mdev, "peer", mdev->p_uuid,
2576 mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2577
2578 hg = drbd_uuid_compare(mdev, &rule_nr);
2579
2580 dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
2581
2582 if (hg == -1000) {
2583 dev_alert(DEV, "Unrelated data, aborting!\n");
2584 return C_MASK;
2585 }
2586 if (hg == -1001) {
2587 dev_alert(DEV, "To resolve this both sides have to support at least protocol\n");
2588 return C_MASK;
2589 }
2590
2591 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
2592 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
2593 int f = (hg == -100) || abs(hg) == 2;
2594 hg = mydisk > D_INCONSISTENT ? 1 : -1;
2595 if (f)
2596 hg = hg*2;
2597 dev_info(DEV, "Becoming sync %s due to disk states.\n",
2598 hg > 0 ? "source" : "target");
2599 }
2600
Adam Gandelman3a11a482010-04-08 16:48:23 -07002601 if (abs(hg) == 100)
2602 drbd_khelper(mdev, "initial-split-brain");
2603
Philipp Reisnerb411b362009-09-25 16:07:19 -07002604 if (hg == 100 || (hg == -100 && mdev->net_conf->always_asbp)) {
2605 int pcount = (mdev->state.role == R_PRIMARY)
2606 + (peer_role == R_PRIMARY);
2607 int forced = (hg == -100);
2608
2609 switch (pcount) {
2610 case 0:
2611 hg = drbd_asb_recover_0p(mdev);
2612 break;
2613 case 1:
2614 hg = drbd_asb_recover_1p(mdev);
2615 break;
2616 case 2:
2617 hg = drbd_asb_recover_2p(mdev);
2618 break;
2619 }
2620 if (abs(hg) < 100) {
2621 dev_warn(DEV, "Split-Brain detected, %d primaries, "
2622 "automatically solved. Sync from %s node\n",
2623 pcount, (hg < 0) ? "peer" : "this");
2624 if (forced) {
2625 dev_warn(DEV, "Doing a full sync, since"
2626 " UUIDs where ambiguous.\n");
2627 hg = hg*2;
2628 }
2629 }
2630 }
2631
2632 if (hg == -100) {
2633 if (mdev->net_conf->want_lose && !(mdev->p_uuid[UI_FLAGS]&1))
2634 hg = -1;
2635 if (!mdev->net_conf->want_lose && (mdev->p_uuid[UI_FLAGS]&1))
2636 hg = 1;
2637
2638 if (abs(hg) < 100)
2639 dev_warn(DEV, "Split-Brain detected, manually solved. "
2640 "Sync from %s node\n",
2641 (hg < 0) ? "peer" : "this");
2642 }
2643
2644 if (hg == -100) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01002645 /* FIXME this log message is not correct if we end up here
2646 * after an attempted attach on a diskless node.
2647 * We just refuse to attach -- well, we drop the "connection"
2648 * to that disk, in a way... */
Adam Gandelman3a11a482010-04-08 16:48:23 -07002649 dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002650 drbd_khelper(mdev, "split-brain");
2651 return C_MASK;
2652 }
2653
2654 if (hg > 0 && mydisk <= D_INCONSISTENT) {
2655 dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
2656 return C_MASK;
2657 }
2658
2659 if (hg < 0 && /* by intention we do not use mydisk here. */
2660 mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
2661 switch (mdev->net_conf->rr_conflict) {
2662 case ASB_CALL_HELPER:
2663 drbd_khelper(mdev, "pri-lost");
2664 /* fall through */
2665 case ASB_DISCONNECT:
2666 dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
2667 return C_MASK;
2668 case ASB_VIOLENTLY:
2669 dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
2670 "assumption\n");
2671 }
2672 }
2673
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002674 if (mdev->net_conf->dry_run || test_bit(CONN_DRY_RUN, &mdev->flags)) {
2675 if (hg == 0)
2676 dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
2677 else
2678 dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
2679 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
2680 abs(hg) >= 2 ? "full" : "bit-map based");
2681 return C_MASK;
2682 }
2683
Philipp Reisnerb411b362009-09-25 16:07:19 -07002684 if (abs(hg) >= 2) {
2685 dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
2686 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake"))
2687 return C_MASK;
2688 }
2689
2690 if (hg > 0) { /* become sync source. */
2691 rv = C_WF_BITMAP_S;
2692 } else if (hg < 0) { /* become sync target */
2693 rv = C_WF_BITMAP_T;
2694 } else {
2695 rv = C_CONNECTED;
2696 if (drbd_bm_total_weight(mdev)) {
2697 dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
2698 drbd_bm_total_weight(mdev));
2699 }
2700 }
2701
2702 return rv;
2703}
2704
2705/* returns 1 if invalid */
2706static int cmp_after_sb(enum drbd_after_sb_p peer, enum drbd_after_sb_p self)
2707{
2708 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
2709 if ((peer == ASB_DISCARD_REMOTE && self == ASB_DISCARD_LOCAL) ||
2710 (self == ASB_DISCARD_REMOTE && peer == ASB_DISCARD_LOCAL))
2711 return 0;
2712
2713 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
2714 if (peer == ASB_DISCARD_REMOTE || peer == ASB_DISCARD_LOCAL ||
2715 self == ASB_DISCARD_REMOTE || self == ASB_DISCARD_LOCAL)
2716 return 1;
2717
2718 /* everything else is valid if they are equal on both sides. */
2719 if (peer == self)
2720 return 0;
2721
2722 /* everything es is invalid. */
2723 return 1;
2724}
2725
Philipp Reisner02918be2010-08-20 14:35:10 +02002726static int receive_protocol(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002727{
Philipp Reisner02918be2010-08-20 14:35:10 +02002728 struct p_protocol *p = &mdev->data.rbuf.protocol;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002729 int p_proto, p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002730 int p_want_lose, p_two_primaries, cf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002731 char p_integrity_alg[SHARED_SECRET_MAX] = "";
2732
Philipp Reisnerb411b362009-09-25 16:07:19 -07002733 p_proto = be32_to_cpu(p->protocol);
2734 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
2735 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
2736 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002737 p_two_primaries = be32_to_cpu(p->two_primaries);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002738 cf = be32_to_cpu(p->conn_flags);
2739 p_want_lose = cf & CF_WANT_LOSE;
2740
2741 clear_bit(CONN_DRY_RUN, &mdev->flags);
2742
2743 if (cf & CF_DRY_RUN)
2744 set_bit(CONN_DRY_RUN, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002745
2746 if (p_proto != mdev->net_conf->wire_protocol) {
2747 dev_err(DEV, "incompatible communication protocols\n");
2748 goto disconnect;
2749 }
2750
2751 if (cmp_after_sb(p_after_sb_0p, mdev->net_conf->after_sb_0p)) {
2752 dev_err(DEV, "incompatible after-sb-0pri settings\n");
2753 goto disconnect;
2754 }
2755
2756 if (cmp_after_sb(p_after_sb_1p, mdev->net_conf->after_sb_1p)) {
2757 dev_err(DEV, "incompatible after-sb-1pri settings\n");
2758 goto disconnect;
2759 }
2760
2761 if (cmp_after_sb(p_after_sb_2p, mdev->net_conf->after_sb_2p)) {
2762 dev_err(DEV, "incompatible after-sb-2pri settings\n");
2763 goto disconnect;
2764 }
2765
2766 if (p_want_lose && mdev->net_conf->want_lose) {
2767 dev_err(DEV, "both sides have the 'want_lose' flag set\n");
2768 goto disconnect;
2769 }
2770
2771 if (p_two_primaries != mdev->net_conf->two_primaries) {
2772 dev_err(DEV, "incompatible setting of the two-primaries options\n");
2773 goto disconnect;
2774 }
2775
2776 if (mdev->agreed_pro_version >= 87) {
2777 unsigned char *my_alg = mdev->net_conf->integrity_alg;
2778
2779 if (drbd_recv(mdev, p_integrity_alg, data_size) != data_size)
2780 return FALSE;
2781
2782 p_integrity_alg[SHARED_SECRET_MAX-1] = 0;
2783 if (strcmp(p_integrity_alg, my_alg)) {
2784 dev_err(DEV, "incompatible setting of the data-integrity-alg\n");
2785 goto disconnect;
2786 }
2787 dev_info(DEV, "data-integrity-alg: %s\n",
2788 my_alg[0] ? my_alg : (unsigned char *)"<not-used>");
2789 }
2790
2791 return TRUE;
2792
2793disconnect:
2794 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
2795 return FALSE;
2796}
2797
2798/* helper function
2799 * input: alg name, feature name
2800 * return: NULL (alg name was "")
2801 * ERR_PTR(error) if something goes wrong
2802 * or the crypto hash ptr, if it worked out ok. */
2803struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
2804 const char *alg, const char *name)
2805{
2806 struct crypto_hash *tfm;
2807
2808 if (!alg[0])
2809 return NULL;
2810
2811 tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
2812 if (IS_ERR(tfm)) {
2813 dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
2814 alg, name, PTR_ERR(tfm));
2815 return tfm;
2816 }
2817 if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) {
2818 crypto_free_hash(tfm);
2819 dev_err(DEV, "\"%s\" is not a digest (%s)\n", alg, name);
2820 return ERR_PTR(-EINVAL);
2821 }
2822 return tfm;
2823}
2824
Philipp Reisner02918be2010-08-20 14:35:10 +02002825static int receive_SyncParam(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int packet_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002826{
2827 int ok = TRUE;
Philipp Reisner02918be2010-08-20 14:35:10 +02002828 struct p_rs_param_95 *p = &mdev->data.rbuf.rs_param_95;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002829 unsigned int header_size, data_size, exp_max_sz;
2830 struct crypto_hash *verify_tfm = NULL;
2831 struct crypto_hash *csums_tfm = NULL;
2832 const int apv = mdev->agreed_pro_version;
Philipp Reisner778f2712010-07-06 11:14:00 +02002833 int *rs_plan_s = NULL;
2834 int fifo_size = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002835
2836 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
2837 : apv == 88 ? sizeof(struct p_rs_param)
2838 + SHARED_SECRET_MAX
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002839 : apv <= 94 ? sizeof(struct p_rs_param_89)
2840 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002841
Philipp Reisner02918be2010-08-20 14:35:10 +02002842 if (packet_size > exp_max_sz) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002843 dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02002844 packet_size, exp_max_sz);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002845 return FALSE;
2846 }
2847
2848 if (apv <= 88) {
Philipp Reisner02918be2010-08-20 14:35:10 +02002849 header_size = sizeof(struct p_rs_param) - sizeof(struct p_header80);
2850 data_size = packet_size - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002851 } else if (apv <= 94) {
Philipp Reisner02918be2010-08-20 14:35:10 +02002852 header_size = sizeof(struct p_rs_param_89) - sizeof(struct p_header80);
2853 data_size = packet_size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002854 D_ASSERT(data_size == 0);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002855 } else {
Philipp Reisner02918be2010-08-20 14:35:10 +02002856 header_size = sizeof(struct p_rs_param_95) - sizeof(struct p_header80);
2857 data_size = packet_size - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002858 D_ASSERT(data_size == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002859 }
2860
2861 /* initialize verify_alg and csums_alg */
2862 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
2863
Philipp Reisner02918be2010-08-20 14:35:10 +02002864 if (drbd_recv(mdev, &p->head.payload, header_size) != header_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002865 return FALSE;
2866
2867 mdev->sync_conf.rate = be32_to_cpu(p->rate);
2868
2869 if (apv >= 88) {
2870 if (apv == 88) {
2871 if (data_size > SHARED_SECRET_MAX) {
2872 dev_err(DEV, "verify-alg too long, "
2873 "peer wants %u, accepting only %u byte\n",
2874 data_size, SHARED_SECRET_MAX);
2875 return FALSE;
2876 }
2877
2878 if (drbd_recv(mdev, p->verify_alg, data_size) != data_size)
2879 return FALSE;
2880
2881 /* we expect NUL terminated string */
2882 /* but just in case someone tries to be evil */
2883 D_ASSERT(p->verify_alg[data_size-1] == 0);
2884 p->verify_alg[data_size-1] = 0;
2885
2886 } else /* apv >= 89 */ {
2887 /* we still expect NUL terminated strings */
2888 /* but just in case someone tries to be evil */
2889 D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
2890 D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
2891 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
2892 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
2893 }
2894
2895 if (strcmp(mdev->sync_conf.verify_alg, p->verify_alg)) {
2896 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
2897 dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
2898 mdev->sync_conf.verify_alg, p->verify_alg);
2899 goto disconnect;
2900 }
2901 verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
2902 p->verify_alg, "verify-alg");
2903 if (IS_ERR(verify_tfm)) {
2904 verify_tfm = NULL;
2905 goto disconnect;
2906 }
2907 }
2908
2909 if (apv >= 89 && strcmp(mdev->sync_conf.csums_alg, p->csums_alg)) {
2910 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
2911 dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
2912 mdev->sync_conf.csums_alg, p->csums_alg);
2913 goto disconnect;
2914 }
2915 csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
2916 p->csums_alg, "csums-alg");
2917 if (IS_ERR(csums_tfm)) {
2918 csums_tfm = NULL;
2919 goto disconnect;
2920 }
2921 }
2922
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002923 if (apv > 94) {
2924 mdev->sync_conf.rate = be32_to_cpu(p->rate);
2925 mdev->sync_conf.c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
2926 mdev->sync_conf.c_delay_target = be32_to_cpu(p->c_delay_target);
2927 mdev->sync_conf.c_fill_target = be32_to_cpu(p->c_fill_target);
2928 mdev->sync_conf.c_max_rate = be32_to_cpu(p->c_max_rate);
Philipp Reisner778f2712010-07-06 11:14:00 +02002929
2930 fifo_size = (mdev->sync_conf.c_plan_ahead * 10 * SLEEP_TIME) / HZ;
2931 if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) {
2932 rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL);
2933 if (!rs_plan_s) {
2934 dev_err(DEV, "kmalloc of fifo_buffer failed");
2935 goto disconnect;
2936 }
2937 }
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002938 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002939
2940 spin_lock(&mdev->peer_seq_lock);
2941 /* lock against drbd_nl_syncer_conf() */
2942 if (verify_tfm) {
2943 strcpy(mdev->sync_conf.verify_alg, p->verify_alg);
2944 mdev->sync_conf.verify_alg_len = strlen(p->verify_alg) + 1;
2945 crypto_free_hash(mdev->verify_tfm);
2946 mdev->verify_tfm = verify_tfm;
2947 dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
2948 }
2949 if (csums_tfm) {
2950 strcpy(mdev->sync_conf.csums_alg, p->csums_alg);
2951 mdev->sync_conf.csums_alg_len = strlen(p->csums_alg) + 1;
2952 crypto_free_hash(mdev->csums_tfm);
2953 mdev->csums_tfm = csums_tfm;
2954 dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
2955 }
Philipp Reisner778f2712010-07-06 11:14:00 +02002956 if (fifo_size != mdev->rs_plan_s.size) {
2957 kfree(mdev->rs_plan_s.values);
2958 mdev->rs_plan_s.values = rs_plan_s;
2959 mdev->rs_plan_s.size = fifo_size;
2960 mdev->rs_planed = 0;
2961 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002962 spin_unlock(&mdev->peer_seq_lock);
2963 }
2964
2965 return ok;
2966disconnect:
2967 /* just for completeness: actually not needed,
2968 * as this is not reached if csums_tfm was ok. */
2969 crypto_free_hash(csums_tfm);
2970 /* but free the verify_tfm again, if csums_tfm did not work out */
2971 crypto_free_hash(verify_tfm);
2972 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
2973 return FALSE;
2974}
2975
2976static void drbd_setup_order_type(struct drbd_conf *mdev, int peer)
2977{
2978 /* sorry, we currently have no working implementation
2979 * of distributed TCQ */
2980}
2981
2982/* warn if the arguments differ by more than 12.5% */
2983static void warn_if_differ_considerably(struct drbd_conf *mdev,
2984 const char *s, sector_t a, sector_t b)
2985{
2986 sector_t d;
2987 if (a == 0 || b == 0)
2988 return;
2989 d = (a > b) ? (a - b) : (b - a);
2990 if (d > (a>>3) || d > (b>>3))
2991 dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
2992 (unsigned long long)a, (unsigned long long)b);
2993}
2994
Philipp Reisner02918be2010-08-20 14:35:10 +02002995static int receive_sizes(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002996{
Philipp Reisner02918be2010-08-20 14:35:10 +02002997 struct p_sizes *p = &mdev->data.rbuf.sizes;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002998 enum determine_dev_size dd = unchanged;
2999 unsigned int max_seg_s;
3000 sector_t p_size, p_usize, my_usize;
3001 int ldsc = 0; /* local disk size changed */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003002 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003003
Philipp Reisnerb411b362009-09-25 16:07:19 -07003004 p_size = be64_to_cpu(p->d_size);
3005 p_usize = be64_to_cpu(p->u_size);
3006
3007 if (p_size == 0 && mdev->state.disk == D_DISKLESS) {
3008 dev_err(DEV, "some backing storage is needed\n");
3009 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
3010 return FALSE;
3011 }
3012
3013 /* just store the peer's disk size for now.
3014 * we still need to figure out whether we accept that. */
3015 mdev->p_size = p_size;
3016
3017#define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
3018 if (get_ldev(mdev)) {
3019 warn_if_differ_considerably(mdev, "lower level device sizes",
3020 p_size, drbd_get_max_capacity(mdev->ldev));
3021 warn_if_differ_considerably(mdev, "user requested size",
3022 p_usize, mdev->ldev->dc.disk_size);
3023
3024 /* if this is the first connect, or an otherwise expected
3025 * param exchange, choose the minimum */
3026 if (mdev->state.conn == C_WF_REPORT_PARAMS)
3027 p_usize = min_not_zero((sector_t)mdev->ldev->dc.disk_size,
3028 p_usize);
3029
3030 my_usize = mdev->ldev->dc.disk_size;
3031
3032 if (mdev->ldev->dc.disk_size != p_usize) {
3033 mdev->ldev->dc.disk_size = p_usize;
3034 dev_info(DEV, "Peer sets u_size to %lu sectors\n",
3035 (unsigned long)mdev->ldev->dc.disk_size);
3036 }
3037
3038 /* Never shrink a device with usable data during connect.
3039 But allow online shrinking if we are connected. */
Philipp Reisnera393db62009-12-22 13:35:52 +01003040 if (drbd_new_dev_size(mdev, mdev->ldev, 0) <
Philipp Reisnerb411b362009-09-25 16:07:19 -07003041 drbd_get_capacity(mdev->this_bdev) &&
3042 mdev->state.disk >= D_OUTDATED &&
3043 mdev->state.conn < C_CONNECTED) {
3044 dev_err(DEV, "The peer's disk size is too small!\n");
3045 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
3046 mdev->ldev->dc.disk_size = my_usize;
3047 put_ldev(mdev);
3048 return FALSE;
3049 }
3050 put_ldev(mdev);
3051 }
3052#undef min_not_zero
3053
Philipp Reisnere89b5912010-03-24 17:11:33 +01003054 ddsf = be16_to_cpu(p->dds_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003055 if (get_ldev(mdev)) {
Philipp Reisnere89b5912010-03-24 17:11:33 +01003056 dd = drbd_determin_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003057 put_ldev(mdev);
3058 if (dd == dev_size_error)
3059 return FALSE;
3060 drbd_md_sync(mdev);
3061 } else {
3062 /* I am diskless, need to accept the peer's size. */
3063 drbd_set_my_capacity(mdev, p_size);
3064 }
3065
Philipp Reisnerb411b362009-09-25 16:07:19 -07003066 if (get_ldev(mdev)) {
3067 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
3068 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
3069 ldsc = 1;
3070 }
3071
Lars Ellenberga1c88d02010-05-14 19:16:41 +02003072 if (mdev->agreed_pro_version < 94)
3073 max_seg_s = be32_to_cpu(p->max_segment_size);
3074 else /* drbd 8.3.8 onwards */
3075 max_seg_s = DRBD_MAX_SEGMENT_SIZE;
3076
Philipp Reisnerb411b362009-09-25 16:07:19 -07003077 if (max_seg_s != queue_max_segment_size(mdev->rq_queue))
3078 drbd_setup_queue_param(mdev, max_seg_s);
3079
Philipp Reisnere89b5912010-03-24 17:11:33 +01003080 drbd_setup_order_type(mdev, be16_to_cpu(p->queue_order_type));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003081 put_ldev(mdev);
3082 }
3083
3084 if (mdev->state.conn > C_WF_REPORT_PARAMS) {
3085 if (be64_to_cpu(p->c_size) !=
3086 drbd_get_capacity(mdev->this_bdev) || ldsc) {
3087 /* we have different sizes, probably peer
3088 * needs to know my new size... */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003089 drbd_send_sizes(mdev, 0, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003090 }
3091 if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
3092 (dd == grew && mdev->state.conn == C_CONNECTED)) {
3093 if (mdev->state.pdsk >= D_INCONSISTENT &&
Philipp Reisnere89b5912010-03-24 17:11:33 +01003094 mdev->state.disk >= D_INCONSISTENT) {
3095 if (ddsf & DDSF_NO_RESYNC)
3096 dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
3097 else
3098 resync_after_online_grow(mdev);
3099 } else
Philipp Reisnerb411b362009-09-25 16:07:19 -07003100 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
3101 }
3102 }
3103
3104 return TRUE;
3105}
3106
Philipp Reisner02918be2010-08-20 14:35:10 +02003107static int receive_uuids(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003108{
Philipp Reisner02918be2010-08-20 14:35:10 +02003109 struct p_uuids *p = &mdev->data.rbuf.uuids;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003110 u64 *p_uuid;
3111 int i;
3112
Philipp Reisnerb411b362009-09-25 16:07:19 -07003113 p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
3114
3115 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
3116 p_uuid[i] = be64_to_cpu(p->uuid[i]);
3117
3118 kfree(mdev->p_uuid);
3119 mdev->p_uuid = p_uuid;
3120
3121 if (mdev->state.conn < C_CONNECTED &&
3122 mdev->state.disk < D_INCONSISTENT &&
3123 mdev->state.role == R_PRIMARY &&
3124 (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
3125 dev_err(DEV, "Can only connect to data with current UUID=%016llX\n",
3126 (unsigned long long)mdev->ed_uuid);
3127 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
3128 return FALSE;
3129 }
3130
3131 if (get_ldev(mdev)) {
3132 int skip_initial_sync =
3133 mdev->state.conn == C_CONNECTED &&
3134 mdev->agreed_pro_version >= 90 &&
3135 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
3136 (p_uuid[UI_FLAGS] & 8);
3137 if (skip_initial_sync) {
3138 dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n");
3139 drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
3140 "clear_n_write from receive_uuids");
3141 _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]);
3142 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3143 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3144 CS_VERBOSE, NULL);
3145 drbd_md_sync(mdev);
3146 }
3147 put_ldev(mdev);
Philipp Reisner18a50fa2010-06-21 14:14:15 +02003148 } else if (mdev->state.disk < D_INCONSISTENT &&
3149 mdev->state.role == R_PRIMARY) {
3150 /* I am a diskless primary, the peer just created a new current UUID
3151 for me. */
3152 drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003153 }
3154
3155 /* Before we test for the disk state, we should wait until an eventually
3156 ongoing cluster wide state change is finished. That is important if
3157 we are primary and are detaching from our disk. We need to see the
3158 new disk state... */
3159 wait_event(mdev->misc_wait, !test_bit(CLUSTER_ST_CHANGE, &mdev->flags));
3160 if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT)
3161 drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
3162
3163 return TRUE;
3164}
3165
3166/**
3167 * convert_state() - Converts the peer's view of the cluster state to our point of view
3168 * @ps: The state as seen by the peer.
3169 */
3170static union drbd_state convert_state(union drbd_state ps)
3171{
3172 union drbd_state ms;
3173
3174 static enum drbd_conns c_tab[] = {
3175 [C_CONNECTED] = C_CONNECTED,
3176
3177 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
3178 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
3179 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
3180 [C_VERIFY_S] = C_VERIFY_T,
3181 [C_MASK] = C_MASK,
3182 };
3183
3184 ms.i = ps.i;
3185
3186 ms.conn = c_tab[ps.conn];
3187 ms.peer = ps.role;
3188 ms.role = ps.peer;
3189 ms.pdsk = ps.disk;
3190 ms.disk = ps.pdsk;
3191 ms.peer_isp = (ps.aftr_isp | ps.user_isp);
3192
3193 return ms;
3194}
3195
Philipp Reisner02918be2010-08-20 14:35:10 +02003196static int receive_req_state(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003197{
Philipp Reisner02918be2010-08-20 14:35:10 +02003198 struct p_req_state *p = &mdev->data.rbuf.req_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003199 union drbd_state mask, val;
3200 int rv;
3201
Philipp Reisnerb411b362009-09-25 16:07:19 -07003202 mask.i = be32_to_cpu(p->mask);
3203 val.i = be32_to_cpu(p->val);
3204
3205 if (test_bit(DISCARD_CONCURRENT, &mdev->flags) &&
3206 test_bit(CLUSTER_ST_CHANGE, &mdev->flags)) {
3207 drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG);
3208 return TRUE;
3209 }
3210
3211 mask = convert_state(mask);
3212 val = convert_state(val);
3213
3214 rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
3215
3216 drbd_send_sr_reply(mdev, rv);
3217 drbd_md_sync(mdev);
3218
3219 return TRUE;
3220}
3221
Philipp Reisner02918be2010-08-20 14:35:10 +02003222static int receive_state(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003223{
Philipp Reisner02918be2010-08-20 14:35:10 +02003224 struct p_state *p = &mdev->data.rbuf.state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003225 enum drbd_conns nconn, oconn;
3226 union drbd_state ns, peer_state;
3227 enum drbd_disk_state real_peer_disk;
Philipp Reisner65d922c2010-06-16 16:18:09 +02003228 enum chg_state_flags cs_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003229 int rv;
3230
Philipp Reisnerb411b362009-09-25 16:07:19 -07003231 peer_state.i = be32_to_cpu(p->state);
3232
3233 real_peer_disk = peer_state.disk;
3234 if (peer_state.disk == D_NEGOTIATING) {
3235 real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
3236 dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
3237 }
3238
3239 spin_lock_irq(&mdev->req_lock);
3240 retry:
3241 oconn = nconn = mdev->state.conn;
3242 spin_unlock_irq(&mdev->req_lock);
3243
3244 if (nconn == C_WF_REPORT_PARAMS)
3245 nconn = C_CONNECTED;
3246
3247 if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING &&
3248 get_ldev_if_state(mdev, D_NEGOTIATING)) {
3249 int cr; /* consider resync */
3250
3251 /* if we established a new connection */
3252 cr = (oconn < C_CONNECTED);
3253 /* if we had an established connection
3254 * and one of the nodes newly attaches a disk */
3255 cr |= (oconn == C_CONNECTED &&
3256 (peer_state.disk == D_NEGOTIATING ||
3257 mdev->state.disk == D_NEGOTIATING));
3258 /* if we have both been inconsistent, and the peer has been
3259 * forced to be UpToDate with --overwrite-data */
3260 cr |= test_bit(CONSIDER_RESYNC, &mdev->flags);
3261 /* if we had been plain connected, and the admin requested to
3262 * start a sync by "invalidate" or "invalidate-remote" */
3263 cr |= (oconn == C_CONNECTED &&
3264 (peer_state.conn >= C_STARTING_SYNC_S &&
3265 peer_state.conn <= C_WF_BITMAP_T));
3266
3267 if (cr)
3268 nconn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk);
3269
3270 put_ldev(mdev);
3271 if (nconn == C_MASK) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01003272 nconn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003273 if (mdev->state.disk == D_NEGOTIATING) {
3274 drbd_force_state(mdev, NS(disk, D_DISKLESS));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003275 } else if (peer_state.disk == D_NEGOTIATING) {
3276 dev_err(DEV, "Disk attach process on the peer node was aborted.\n");
3277 peer_state.disk = D_DISKLESS;
Lars Ellenberg580b9762010-02-26 23:15:23 +01003278 real_peer_disk = D_DISKLESS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003279 } else {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003280 if (test_and_clear_bit(CONN_DRY_RUN, &mdev->flags))
3281 return FALSE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003282 D_ASSERT(oconn == C_WF_REPORT_PARAMS);
3283 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
3284 return FALSE;
3285 }
3286 }
3287 }
3288
3289 spin_lock_irq(&mdev->req_lock);
3290 if (mdev->state.conn != oconn)
3291 goto retry;
3292 clear_bit(CONSIDER_RESYNC, &mdev->flags);
3293 ns.i = mdev->state.i;
3294 ns.conn = nconn;
3295 ns.peer = peer_state.role;
3296 ns.pdsk = real_peer_disk;
3297 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
3298 if ((nconn == C_CONNECTED || nconn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
3299 ns.disk = mdev->new_state_tmp.disk;
Philipp Reisner65d922c2010-06-16 16:18:09 +02003300 cs_flags = CS_VERBOSE + (oconn < C_CONNECTED && nconn >= C_CONNECTED ? 0 : CS_HARD);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003301 if (ns.pdsk == D_CONSISTENT && ns.susp && nconn == C_CONNECTED && oconn < C_CONNECTED &&
3302 test_bit(NEW_CUR_UUID, &mdev->flags)) {
3303 /* Do not allow tl_restart(resend) for a rebooted peer. We can only allow this
3304 for temporal network outages! */
3305 spin_unlock_irq(&mdev->req_lock);
3306 dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
3307 tl_clear(mdev);
3308 drbd_uuid_new_current(mdev);
3309 clear_bit(NEW_CUR_UUID, &mdev->flags);
3310 drbd_force_state(mdev, NS2(conn, C_PROTOCOL_ERROR, susp, 0));
3311 return FALSE;
3312 }
Philipp Reisner65d922c2010-06-16 16:18:09 +02003313 rv = _drbd_set_state(mdev, ns, cs_flags, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003314 ns = mdev->state;
3315 spin_unlock_irq(&mdev->req_lock);
3316
3317 if (rv < SS_SUCCESS) {
3318 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
3319 return FALSE;
3320 }
3321
3322 if (oconn > C_WF_REPORT_PARAMS) {
3323 if (nconn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
3324 peer_state.disk != D_NEGOTIATING ) {
3325 /* we want resync, peer has not yet decided to sync... */
3326 /* Nowadays only used when forcing a node into primary role and
3327 setting its disk to UpToDate with that */
3328 drbd_send_uuids(mdev);
3329 drbd_send_state(mdev);
3330 }
3331 }
3332
3333 mdev->net_conf->want_lose = 0;
3334
3335 drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
3336
3337 return TRUE;
3338}
3339
Philipp Reisner02918be2010-08-20 14:35:10 +02003340static int receive_sync_uuid(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003341{
Philipp Reisner02918be2010-08-20 14:35:10 +02003342 struct p_rs_uuid *p = &mdev->data.rbuf.rs_uuid;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003343
3344 wait_event(mdev->misc_wait,
3345 mdev->state.conn == C_WF_SYNC_UUID ||
3346 mdev->state.conn < C_CONNECTED ||
3347 mdev->state.disk < D_NEGOTIATING);
3348
3349 /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */
3350
Philipp Reisnerb411b362009-09-25 16:07:19 -07003351 /* Here the _drbd_uuid_ functions are right, current should
3352 _not_ be rotated into the history */
3353 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
3354 _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid));
3355 _drbd_uuid_set(mdev, UI_BITMAP, 0UL);
3356
3357 drbd_start_resync(mdev, C_SYNC_TARGET);
3358
3359 put_ldev(mdev);
3360 } else
3361 dev_err(DEV, "Ignoring SyncUUID packet!\n");
3362
3363 return TRUE;
3364}
3365
3366enum receive_bitmap_ret { OK, DONE, FAILED };
3367
3368static enum receive_bitmap_ret
Philipp Reisner02918be2010-08-20 14:35:10 +02003369receive_bitmap_plain(struct drbd_conf *mdev, unsigned int data_size,
3370 unsigned long *buffer, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003371{
3372 unsigned num_words = min_t(size_t, BM_PACKET_WORDS, c->bm_words - c->word_offset);
3373 unsigned want = num_words * sizeof(long);
3374
Philipp Reisner02918be2010-08-20 14:35:10 +02003375 if (want != data_size) {
3376 dev_err(DEV, "%s:want (%u) != data_size (%u)\n", __func__, want, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003377 return FAILED;
3378 }
3379 if (want == 0)
3380 return DONE;
3381 if (drbd_recv(mdev, buffer, want) != want)
3382 return FAILED;
3383
3384 drbd_bm_merge_lel(mdev, c->word_offset, num_words, buffer);
3385
3386 c->word_offset += num_words;
3387 c->bit_offset = c->word_offset * BITS_PER_LONG;
3388 if (c->bit_offset > c->bm_bits)
3389 c->bit_offset = c->bm_bits;
3390
3391 return OK;
3392}
3393
3394static enum receive_bitmap_ret
3395recv_bm_rle_bits(struct drbd_conf *mdev,
3396 struct p_compressed_bm *p,
3397 struct bm_xfer_ctx *c)
3398{
3399 struct bitstream bs;
3400 u64 look_ahead;
3401 u64 rl;
3402 u64 tmp;
3403 unsigned long s = c->bit_offset;
3404 unsigned long e;
3405 int len = p->head.length - (sizeof(*p) - sizeof(p->head));
3406 int toggle = DCBP_get_start(p);
3407 int have;
3408 int bits;
3409
3410 bitstream_init(&bs, p->code, len, DCBP_get_pad_bits(p));
3411
3412 bits = bitstream_get_bits(&bs, &look_ahead, 64);
3413 if (bits < 0)
3414 return FAILED;
3415
3416 for (have = bits; have > 0; s += rl, toggle = !toggle) {
3417 bits = vli_decode_bits(&rl, look_ahead);
3418 if (bits <= 0)
3419 return FAILED;
3420
3421 if (toggle) {
3422 e = s + rl -1;
3423 if (e >= c->bm_bits) {
3424 dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
3425 return FAILED;
3426 }
3427 _drbd_bm_set_bits(mdev, s, e);
3428 }
3429
3430 if (have < bits) {
3431 dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
3432 have, bits, look_ahead,
3433 (unsigned int)(bs.cur.b - p->code),
3434 (unsigned int)bs.buf_len);
3435 return FAILED;
3436 }
3437 look_ahead >>= bits;
3438 have -= bits;
3439
3440 bits = bitstream_get_bits(&bs, &tmp, 64 - have);
3441 if (bits < 0)
3442 return FAILED;
3443 look_ahead |= tmp << have;
3444 have += bits;
3445 }
3446
3447 c->bit_offset = s;
3448 bm_xfer_ctx_bit_to_word_offset(c);
3449
3450 return (s == c->bm_bits) ? DONE : OK;
3451}
3452
3453static enum receive_bitmap_ret
3454decode_bitmap_c(struct drbd_conf *mdev,
3455 struct p_compressed_bm *p,
3456 struct bm_xfer_ctx *c)
3457{
3458 if (DCBP_get_code(p) == RLE_VLI_Bits)
3459 return recv_bm_rle_bits(mdev, p, c);
3460
3461 /* other variants had been implemented for evaluation,
3462 * but have been dropped as this one turned out to be "best"
3463 * during all our tests. */
3464
3465 dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
3466 drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR));
3467 return FAILED;
3468}
3469
3470void INFO_bm_xfer_stats(struct drbd_conf *mdev,
3471 const char *direction, struct bm_xfer_ctx *c)
3472{
3473 /* what would it take to transfer it "plaintext" */
Philipp Reisner0b70a132010-08-20 13:36:10 +02003474 unsigned plain = sizeof(struct p_header80) *
Philipp Reisnerb411b362009-09-25 16:07:19 -07003475 ((c->bm_words+BM_PACKET_WORDS-1)/BM_PACKET_WORDS+1)
3476 + c->bm_words * sizeof(long);
3477 unsigned total = c->bytes[0] + c->bytes[1];
3478 unsigned r;
3479
3480 /* total can not be zero. but just in case: */
3481 if (total == 0)
3482 return;
3483
3484 /* don't report if not compressed */
3485 if (total >= plain)
3486 return;
3487
3488 /* total < plain. check for overflow, still */
3489 r = (total > UINT_MAX/1000) ? (total / (plain/1000))
3490 : (1000 * total / plain);
3491
3492 if (r > 1000)
3493 r = 1000;
3494
3495 r = 1000 - r;
3496 dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
3497 "total %u; compression: %u.%u%%\n",
3498 direction,
3499 c->bytes[1], c->packets[1],
3500 c->bytes[0], c->packets[0],
3501 total, r/10, r % 10);
3502}
3503
3504/* Since we are processing the bitfield from lower addresses to higher,
3505 it does not matter if the process it in 32 bit chunks or 64 bit
3506 chunks as long as it is little endian. (Understand it as byte stream,
3507 beginning with the lowest byte...) If we would use big endian
3508 we would need to process it from the highest address to the lowest,
3509 in order to be agnostic to the 32 vs 64 bits issue.
3510
3511 returns 0 on failure, 1 if we successfully received it. */
Philipp Reisner02918be2010-08-20 14:35:10 +02003512static int receive_bitmap(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003513{
3514 struct bm_xfer_ctx c;
3515 void *buffer;
3516 enum receive_bitmap_ret ret;
3517 int ok = FALSE;
Philipp Reisner02918be2010-08-20 14:35:10 +02003518 struct p_header80 *h = &mdev->data.rbuf.header.h80;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003519
3520 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
3521
3522 drbd_bm_lock(mdev, "receive bitmap");
3523
3524 /* maybe we should use some per thread scratch page,
3525 * and allocate that during initial device creation? */
3526 buffer = (unsigned long *) __get_free_page(GFP_NOIO);
3527 if (!buffer) {
3528 dev_err(DEV, "failed to allocate one page buffer in %s\n", __func__);
3529 goto out;
3530 }
3531
3532 c = (struct bm_xfer_ctx) {
3533 .bm_bits = drbd_bm_bits(mdev),
3534 .bm_words = drbd_bm_words(mdev),
3535 };
3536
3537 do {
Philipp Reisner02918be2010-08-20 14:35:10 +02003538 if (cmd == P_BITMAP) {
3539 ret = receive_bitmap_plain(mdev, data_size, buffer, &c);
3540 } else if (cmd == P_COMPRESSED_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003541 /* MAYBE: sanity check that we speak proto >= 90,
3542 * and the feature is enabled! */
3543 struct p_compressed_bm *p;
3544
Philipp Reisner02918be2010-08-20 14:35:10 +02003545 if (data_size > BM_PACKET_PAYLOAD_BYTES) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003546 dev_err(DEV, "ReportCBitmap packet too large\n");
3547 goto out;
3548 }
3549 /* use the page buff */
3550 p = buffer;
3551 memcpy(p, h, sizeof(*h));
Philipp Reisner02918be2010-08-20 14:35:10 +02003552 if (drbd_recv(mdev, p->head.payload, data_size) != data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003553 goto out;
3554 if (p->head.length <= (sizeof(*p) - sizeof(p->head))) {
3555 dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", p->head.length);
3556 return FAILED;
3557 }
3558 ret = decode_bitmap_c(mdev, p, &c);
3559 } else {
Philipp Reisner02918be2010-08-20 14:35:10 +02003560 dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003561 goto out;
3562 }
3563
Philipp Reisner02918be2010-08-20 14:35:10 +02003564 c.packets[cmd == P_BITMAP]++;
3565 c.bytes[cmd == P_BITMAP] += sizeof(struct p_header80) + data_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003566
3567 if (ret != OK)
3568 break;
3569
Philipp Reisner02918be2010-08-20 14:35:10 +02003570 if (!drbd_recv_header(mdev, &cmd, &data_size))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003571 goto out;
3572 } while (ret == OK);
3573 if (ret == FAILED)
3574 goto out;
3575
3576 INFO_bm_xfer_stats(mdev, "receive", &c);
3577
3578 if (mdev->state.conn == C_WF_BITMAP_T) {
3579 ok = !drbd_send_bitmap(mdev);
3580 if (!ok)
3581 goto out;
3582 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
3583 ok = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
3584 D_ASSERT(ok == SS_SUCCESS);
3585 } else if (mdev->state.conn != C_WF_BITMAP_S) {
3586 /* admin may have requested C_DISCONNECTING,
3587 * other threads may have noticed network errors */
3588 dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n",
3589 drbd_conn_str(mdev->state.conn));
3590 }
3591
3592 ok = TRUE;
3593 out:
3594 drbd_bm_unlock(mdev);
3595 if (ok && mdev->state.conn == C_WF_BITMAP_S)
3596 drbd_start_resync(mdev, C_SYNC_SOURCE);
3597 free_page((unsigned long) buffer);
3598 return ok;
3599}
3600
Philipp Reisner02918be2010-08-20 14:35:10 +02003601static int receive_skip(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003602{
3603 /* TODO zero copy sink :) */
3604 static char sink[128];
3605 int size, want, r;
3606
Philipp Reisner02918be2010-08-20 14:35:10 +02003607 dev_warn(DEV, "skipping unknown optional packet type %d, l: %d!\n",
3608 cmd, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003609
Philipp Reisner02918be2010-08-20 14:35:10 +02003610 size = data_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003611 while (size > 0) {
3612 want = min_t(int, size, sizeof(sink));
3613 r = drbd_recv(mdev, sink, want);
3614 ERR_IF(r <= 0) break;
3615 size -= r;
3616 }
3617 return size == 0;
3618}
3619
Philipp Reisner02918be2010-08-20 14:35:10 +02003620static int receive_UnplugRemote(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003621{
3622 if (mdev->state.disk >= D_INCONSISTENT)
3623 drbd_kick_lo(mdev);
3624
3625 /* Make sure we've acked all the TCP data associated
3626 * with the data requests being unplugged */
3627 drbd_tcp_quickack(mdev->data.socket);
3628
3629 return TRUE;
3630}
3631
Philipp Reisner02918be2010-08-20 14:35:10 +02003632typedef int (*drbd_cmd_handler_f)(struct drbd_conf *, enum drbd_packets cmd, unsigned int to_receive);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003633
Philipp Reisner02918be2010-08-20 14:35:10 +02003634struct data_cmd {
3635 int expect_payload;
3636 size_t pkt_size;
3637 drbd_cmd_handler_f function;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003638};
3639
Philipp Reisner02918be2010-08-20 14:35:10 +02003640static struct data_cmd drbd_cmd_handler[] = {
3641 [P_DATA] = { 1, sizeof(struct p_data), receive_Data },
3642 [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply },
3643 [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } ,
3644 [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } ,
3645 [P_BITMAP] = { 1, sizeof(struct p_header80), receive_bitmap } ,
3646 [P_COMPRESSED_BITMAP] = { 1, sizeof(struct p_header80), receive_bitmap } ,
3647 [P_UNPLUG_REMOTE] = { 0, sizeof(struct p_header80), receive_UnplugRemote },
3648 [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
3649 [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
3650 [P_SYNC_PARAM] = { 1, sizeof(struct p_header80), receive_SyncParam },
3651 [P_SYNC_PARAM89] = { 1, sizeof(struct p_header80), receive_SyncParam },
3652 [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol },
3653 [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids },
3654 [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes },
3655 [P_STATE] = { 0, sizeof(struct p_state), receive_state },
3656 [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state },
3657 [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid },
3658 [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
3659 [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest },
3660 [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest },
3661 [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip },
3662 /* anything missing from this table is in
3663 * the asender_tbl, see get_asender_cmd */
3664 [P_MAX_CMD] = { 0, 0, NULL },
3665};
3666
3667/* All handler functions that expect a sub-header get that sub-heder in
3668 mdev->data.rbuf.header.head.payload.
3669
3670 Usually in mdev->data.rbuf.header.head the callback can find the usual
3671 p_header, but they may not rely on that. Since there is also p_header95 !
3672 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003673
3674static void drbdd(struct drbd_conf *mdev)
3675{
Philipp Reisner02918be2010-08-20 14:35:10 +02003676 union p_header *header = &mdev->data.rbuf.header;
3677 unsigned int packet_size;
3678 enum drbd_packets cmd;
3679 size_t shs; /* sub header size */
3680 int rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003681
3682 while (get_t_state(&mdev->receiver) == Running) {
3683 drbd_thread_current_set_cpu(mdev);
Philipp Reisner02918be2010-08-20 14:35:10 +02003684 if (!drbd_recv_header(mdev, &cmd, &packet_size))
3685 goto err_out;
3686
3687 if (unlikely(cmd >= P_MAX_CMD || !drbd_cmd_handler[cmd].function)) {
3688 dev_err(DEV, "unknown packet type %d, l: %d!\n", cmd, packet_size);
3689 goto err_out;
Lars Ellenberg0b33a912009-11-16 15:58:04 +01003690 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003691
Philipp Reisner02918be2010-08-20 14:35:10 +02003692 shs = drbd_cmd_handler[cmd].pkt_size - sizeof(union p_header);
3693 rv = drbd_recv(mdev, &header->h80.payload, shs);
3694 if (unlikely(rv != shs)) {
3695 dev_err(DEV, "short read while reading sub header: rv=%d\n", rv);
3696 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003697 }
Philipp Reisner02918be2010-08-20 14:35:10 +02003698
3699 if (packet_size - shs > 0 && !drbd_cmd_handler[cmd].expect_payload) {
3700 dev_err(DEV, "No payload expected %s l:%d\n", cmdname(cmd), packet_size);
3701 goto err_out;
3702 }
3703
3704 rv = drbd_cmd_handler[cmd].function(mdev, cmd, packet_size - shs);
3705
3706 if (unlikely(!rv)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003707 dev_err(DEV, "error receiving %s, l: %d!\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02003708 cmdname(cmd), packet_size);
3709 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003710 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003711 }
Philipp Reisner02918be2010-08-20 14:35:10 +02003712
3713 if (0) {
3714 err_out:
3715 drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR));
3716 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003717}
3718
Philipp Reisnerb411b362009-09-25 16:07:19 -07003719void drbd_flush_workqueue(struct drbd_conf *mdev)
3720{
3721 struct drbd_wq_barrier barr;
3722
3723 barr.w.cb = w_prev_work_done;
3724 init_completion(&barr.done);
3725 drbd_queue_work(&mdev->data.work, &barr.w);
3726 wait_for_completion(&barr.done);
3727}
3728
Philipp Reisnerf70b35112010-06-24 14:34:40 +02003729void drbd_free_tl_hash(struct drbd_conf *mdev)
3730{
3731 struct hlist_head *h;
3732
3733 spin_lock_irq(&mdev->req_lock);
3734
3735 if (!mdev->tl_hash || mdev->state.conn != C_STANDALONE) {
3736 spin_unlock_irq(&mdev->req_lock);
3737 return;
3738 }
3739 /* paranoia code */
3740 for (h = mdev->ee_hash; h < mdev->ee_hash + mdev->ee_hash_s; h++)
3741 if (h->first)
3742 dev_err(DEV, "ASSERT FAILED ee_hash[%u].first == %p, expected NULL\n",
3743 (int)(h - mdev->ee_hash), h->first);
3744 kfree(mdev->ee_hash);
3745 mdev->ee_hash = NULL;
3746 mdev->ee_hash_s = 0;
3747
3748 /* paranoia code */
3749 for (h = mdev->tl_hash; h < mdev->tl_hash + mdev->tl_hash_s; h++)
3750 if (h->first)
3751 dev_err(DEV, "ASSERT FAILED tl_hash[%u] == %p, expected NULL\n",
3752 (int)(h - mdev->tl_hash), h->first);
3753 kfree(mdev->tl_hash);
3754 mdev->tl_hash = NULL;
3755 mdev->tl_hash_s = 0;
3756 spin_unlock_irq(&mdev->req_lock);
3757}
3758
Philipp Reisnerb411b362009-09-25 16:07:19 -07003759static void drbd_disconnect(struct drbd_conf *mdev)
3760{
3761 enum drbd_fencing_p fp;
3762 union drbd_state os, ns;
3763 int rv = SS_UNKNOWN_ERROR;
3764 unsigned int i;
3765
3766 if (mdev->state.conn == C_STANDALONE)
3767 return;
3768 if (mdev->state.conn >= C_WF_CONNECTION)
3769 dev_err(DEV, "ASSERT FAILED cstate = %s, expected < WFConnection\n",
3770 drbd_conn_str(mdev->state.conn));
3771
3772 /* asender does not clean up anything. it must not interfere, either */
3773 drbd_thread_stop(&mdev->asender);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003774 drbd_free_sock(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003775
Philipp Reisner85719572010-07-21 10:20:17 +02003776 /* wait for current activity to cease. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003777 spin_lock_irq(&mdev->req_lock);
3778 _drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
3779 _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee);
3780 _drbd_wait_ee_list_empty(mdev, &mdev->read_ee);
3781 spin_unlock_irq(&mdev->req_lock);
3782
3783 /* We do not have data structures that would allow us to
3784 * get the rs_pending_cnt down to 0 again.
3785 * * On C_SYNC_TARGET we do not have any data structures describing
3786 * the pending RSDataRequest's we have sent.
3787 * * On C_SYNC_SOURCE there is no data structure that tracks
3788 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
3789 * And no, it is not the sum of the reference counts in the
3790 * resync_LRU. The resync_LRU tracks the whole operation including
3791 * the disk-IO, while the rs_pending_cnt only tracks the blocks
3792 * on the fly. */
3793 drbd_rs_cancel_all(mdev);
3794 mdev->rs_total = 0;
3795 mdev->rs_failed = 0;
3796 atomic_set(&mdev->rs_pending_cnt, 0);
3797 wake_up(&mdev->misc_wait);
3798
3799 /* make sure syncer is stopped and w_resume_next_sg queued */
3800 del_timer_sync(&mdev->resync_timer);
3801 set_bit(STOP_SYNC_TIMER, &mdev->flags);
3802 resync_timer_fn((unsigned long)mdev);
3803
Philipp Reisnerb411b362009-09-25 16:07:19 -07003804 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
3805 * w_make_resync_request etc. which may still be on the worker queue
3806 * to be "canceled" */
3807 drbd_flush_workqueue(mdev);
3808
3809 /* This also does reclaim_net_ee(). If we do this too early, we might
3810 * miss some resync ee and pages.*/
3811 drbd_process_done_ee(mdev);
3812
3813 kfree(mdev->p_uuid);
3814 mdev->p_uuid = NULL;
3815
3816 if (!mdev->state.susp)
3817 tl_clear(mdev);
3818
Philipp Reisnerb411b362009-09-25 16:07:19 -07003819 dev_info(DEV, "Connection closed\n");
3820
3821 drbd_md_sync(mdev);
3822
3823 fp = FP_DONT_CARE;
3824 if (get_ldev(mdev)) {
3825 fp = mdev->ldev->dc.fencing;
3826 put_ldev(mdev);
3827 }
3828
Philipp Reisner87f7be42010-06-11 13:56:33 +02003829 if (mdev->state.role == R_PRIMARY && fp >= FP_RESOURCE && mdev->state.pdsk >= D_UNKNOWN)
3830 drbd_try_outdate_peer_async(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003831
3832 spin_lock_irq(&mdev->req_lock);
3833 os = mdev->state;
3834 if (os.conn >= C_UNCONNECTED) {
3835 /* Do not restart in case we are C_DISCONNECTING */
3836 ns = os;
3837 ns.conn = C_UNCONNECTED;
3838 rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
3839 }
3840 spin_unlock_irq(&mdev->req_lock);
3841
3842 if (os.conn == C_DISCONNECTING) {
Philipp Reisner84dfb9f2010-06-23 11:20:05 +02003843 wait_event(mdev->net_cnt_wait, atomic_read(&mdev->net_cnt) == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003844
Philipp Reisnerf70b35112010-06-24 14:34:40 +02003845 if (!mdev->state.susp) {
3846 /* we must not free the tl_hash
3847 * while application io is still on the fly */
3848 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
3849 drbd_free_tl_hash(mdev);
3850 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003851
3852 crypto_free_hash(mdev->cram_hmac_tfm);
3853 mdev->cram_hmac_tfm = NULL;
3854
3855 kfree(mdev->net_conf);
3856 mdev->net_conf = NULL;
3857 drbd_request_state(mdev, NS(conn, C_STANDALONE));
3858 }
3859
3860 /* tcp_close and release of sendpage pages can be deferred. I don't
3861 * want to use SO_LINGER, because apparently it can be deferred for
3862 * more than 20 seconds (longest time I checked).
3863 *
3864 * Actually we don't care for exactly when the network stack does its
3865 * put_page(), but release our reference on these pages right here.
3866 */
3867 i = drbd_release_ee(mdev, &mdev->net_ee);
3868 if (i)
3869 dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
3870 i = atomic_read(&mdev->pp_in_use);
3871 if (i)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02003872 dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003873
3874 D_ASSERT(list_empty(&mdev->read_ee));
3875 D_ASSERT(list_empty(&mdev->active_ee));
3876 D_ASSERT(list_empty(&mdev->sync_ee));
3877 D_ASSERT(list_empty(&mdev->done_ee));
3878
3879 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
3880 atomic_set(&mdev->current_epoch->epoch_size, 0);
3881 D_ASSERT(list_empty(&mdev->current_epoch->list));
3882}
3883
3884/*
3885 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
3886 * we can agree on is stored in agreed_pro_version.
3887 *
3888 * feature flags and the reserved array should be enough room for future
3889 * enhancements of the handshake protocol, and possible plugins...
3890 *
3891 * for now, they are expected to be zero, but ignored.
3892 */
3893static int drbd_send_handshake(struct drbd_conf *mdev)
3894{
3895 /* ASSERT current == mdev->receiver ... */
3896 struct p_handshake *p = &mdev->data.sbuf.handshake;
3897 int ok;
3898
3899 if (mutex_lock_interruptible(&mdev->data.mutex)) {
3900 dev_err(DEV, "interrupted during initial handshake\n");
3901 return 0; /* interrupted. not ok. */
3902 }
3903
3904 if (mdev->data.socket == NULL) {
3905 mutex_unlock(&mdev->data.mutex);
3906 return 0;
3907 }
3908
3909 memset(p, 0, sizeof(*p));
3910 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
3911 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
3912 ok = _drbd_send_cmd( mdev, mdev->data.socket, P_HAND_SHAKE,
Philipp Reisner0b70a132010-08-20 13:36:10 +02003913 (struct p_header80 *)p, sizeof(*p), 0 );
Philipp Reisnerb411b362009-09-25 16:07:19 -07003914 mutex_unlock(&mdev->data.mutex);
3915 return ok;
3916}
3917
3918/*
3919 * return values:
3920 * 1 yes, we have a valid connection
3921 * 0 oops, did not work out, please try again
3922 * -1 peer talks different language,
3923 * no point in trying again, please go standalone.
3924 */
3925static int drbd_do_handshake(struct drbd_conf *mdev)
3926{
3927 /* ASSERT current == mdev->receiver ... */
3928 struct p_handshake *p = &mdev->data.rbuf.handshake;
Philipp Reisner02918be2010-08-20 14:35:10 +02003929 const int expect = sizeof(struct p_handshake) - sizeof(struct p_header80);
3930 unsigned int length;
3931 enum drbd_packets cmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003932 int rv;
3933
3934 rv = drbd_send_handshake(mdev);
3935 if (!rv)
3936 return 0;
3937
Philipp Reisner02918be2010-08-20 14:35:10 +02003938 rv = drbd_recv_header(mdev, &cmd, &length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003939 if (!rv)
3940 return 0;
3941
Philipp Reisner02918be2010-08-20 14:35:10 +02003942 if (cmd != P_HAND_SHAKE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003943 dev_err(DEV, "expected HandShake packet, received: %s (0x%04x)\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02003944 cmdname(cmd), cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003945 return -1;
3946 }
3947
Philipp Reisner02918be2010-08-20 14:35:10 +02003948 if (length != expect) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003949 dev_err(DEV, "expected HandShake length: %u, received: %u\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02003950 expect, length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003951 return -1;
3952 }
3953
3954 rv = drbd_recv(mdev, &p->head.payload, expect);
3955
3956 if (rv != expect) {
3957 dev_err(DEV, "short read receiving handshake packet: l=%u\n", rv);
3958 return 0;
3959 }
3960
Philipp Reisnerb411b362009-09-25 16:07:19 -07003961 p->protocol_min = be32_to_cpu(p->protocol_min);
3962 p->protocol_max = be32_to_cpu(p->protocol_max);
3963 if (p->protocol_max == 0)
3964 p->protocol_max = p->protocol_min;
3965
3966 if (PRO_VERSION_MAX < p->protocol_min ||
3967 PRO_VERSION_MIN > p->protocol_max)
3968 goto incompat;
3969
3970 mdev->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
3971
3972 dev_info(DEV, "Handshake successful: "
3973 "Agreed network protocol version %d\n", mdev->agreed_pro_version);
3974
3975 return 1;
3976
3977 incompat:
3978 dev_err(DEV, "incompatible DRBD dialects: "
3979 "I support %d-%d, peer supports %d-%d\n",
3980 PRO_VERSION_MIN, PRO_VERSION_MAX,
3981 p->protocol_min, p->protocol_max);
3982 return -1;
3983}
3984
3985#if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
3986static int drbd_do_auth(struct drbd_conf *mdev)
3987{
3988 dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
3989 dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01003990 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003991}
3992#else
3993#define CHALLENGE_LEN 64
Johannes Thomab10d96c2010-01-07 16:02:50 +01003994
3995/* Return value:
3996 1 - auth succeeded,
3997 0 - failed, try again (network error),
3998 -1 - auth failed, don't try again.
3999*/
4000
Philipp Reisnerb411b362009-09-25 16:07:19 -07004001static int drbd_do_auth(struct drbd_conf *mdev)
4002{
4003 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
4004 struct scatterlist sg;
4005 char *response = NULL;
4006 char *right_response = NULL;
4007 char *peers_ch = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004008 unsigned int key_len = strlen(mdev->net_conf->shared_secret);
4009 unsigned int resp_size;
4010 struct hash_desc desc;
Philipp Reisner02918be2010-08-20 14:35:10 +02004011 enum drbd_packets cmd;
4012 unsigned int length;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004013 int rv;
4014
4015 desc.tfm = mdev->cram_hmac_tfm;
4016 desc.flags = 0;
4017
4018 rv = crypto_hash_setkey(mdev->cram_hmac_tfm,
4019 (u8 *)mdev->net_conf->shared_secret, key_len);
4020 if (rv) {
4021 dev_err(DEV, "crypto_hash_setkey() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004022 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004023 goto fail;
4024 }
4025
4026 get_random_bytes(my_challenge, CHALLENGE_LEN);
4027
4028 rv = drbd_send_cmd2(mdev, P_AUTH_CHALLENGE, my_challenge, CHALLENGE_LEN);
4029 if (!rv)
4030 goto fail;
4031
Philipp Reisner02918be2010-08-20 14:35:10 +02004032 rv = drbd_recv_header(mdev, &cmd, &length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004033 if (!rv)
4034 goto fail;
4035
Philipp Reisner02918be2010-08-20 14:35:10 +02004036 if (cmd != P_AUTH_CHALLENGE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004037 dev_err(DEV, "expected AuthChallenge packet, received: %s (0x%04x)\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02004038 cmdname(cmd), cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004039 rv = 0;
4040 goto fail;
4041 }
4042
Philipp Reisner02918be2010-08-20 14:35:10 +02004043 if (length > CHALLENGE_LEN * 2) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004044 dev_err(DEV, "expected AuthChallenge payload too big.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004045 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004046 goto fail;
4047 }
4048
Philipp Reisner02918be2010-08-20 14:35:10 +02004049 peers_ch = kmalloc(length, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004050 if (peers_ch == NULL) {
4051 dev_err(DEV, "kmalloc of peers_ch failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004052 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004053 goto fail;
4054 }
4055
Philipp Reisner02918be2010-08-20 14:35:10 +02004056 rv = drbd_recv(mdev, peers_ch, length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004057
Philipp Reisner02918be2010-08-20 14:35:10 +02004058 if (rv != length) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004059 dev_err(DEV, "short read AuthChallenge: l=%u\n", rv);
4060 rv = 0;
4061 goto fail;
4062 }
4063
4064 resp_size = crypto_hash_digestsize(mdev->cram_hmac_tfm);
4065 response = kmalloc(resp_size, GFP_NOIO);
4066 if (response == NULL) {
4067 dev_err(DEV, "kmalloc of response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004068 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004069 goto fail;
4070 }
4071
4072 sg_init_table(&sg, 1);
Philipp Reisner02918be2010-08-20 14:35:10 +02004073 sg_set_buf(&sg, peers_ch, length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004074
4075 rv = crypto_hash_digest(&desc, &sg, sg.length, response);
4076 if (rv) {
4077 dev_err(DEV, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004078 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004079 goto fail;
4080 }
4081
4082 rv = drbd_send_cmd2(mdev, P_AUTH_RESPONSE, response, resp_size);
4083 if (!rv)
4084 goto fail;
4085
Philipp Reisner02918be2010-08-20 14:35:10 +02004086 rv = drbd_recv_header(mdev, &cmd, &length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004087 if (!rv)
4088 goto fail;
4089
Philipp Reisner02918be2010-08-20 14:35:10 +02004090 if (cmd != P_AUTH_RESPONSE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004091 dev_err(DEV, "expected AuthResponse packet, received: %s (0x%04x)\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02004092 cmdname(cmd), cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004093 rv = 0;
4094 goto fail;
4095 }
4096
Philipp Reisner02918be2010-08-20 14:35:10 +02004097 if (length != resp_size) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004098 dev_err(DEV, "expected AuthResponse payload of wrong size\n");
4099 rv = 0;
4100 goto fail;
4101 }
4102
4103 rv = drbd_recv(mdev, response , resp_size);
4104
4105 if (rv != resp_size) {
4106 dev_err(DEV, "short read receiving AuthResponse: l=%u\n", rv);
4107 rv = 0;
4108 goto fail;
4109 }
4110
4111 right_response = kmalloc(resp_size, GFP_NOIO);
Julia Lawall2d1ee872009-12-27 22:27:11 +01004112 if (right_response == NULL) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004113 dev_err(DEV, "kmalloc of right_response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004114 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004115 goto fail;
4116 }
4117
4118 sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
4119
4120 rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
4121 if (rv) {
4122 dev_err(DEV, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004123 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004124 goto fail;
4125 }
4126
4127 rv = !memcmp(response, right_response, resp_size);
4128
4129 if (rv)
4130 dev_info(DEV, "Peer authenticated using %d bytes of '%s' HMAC\n",
4131 resp_size, mdev->net_conf->cram_hmac_alg);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004132 else
4133 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004134
4135 fail:
4136 kfree(peers_ch);
4137 kfree(response);
4138 kfree(right_response);
4139
4140 return rv;
4141}
4142#endif
4143
4144int drbdd_init(struct drbd_thread *thi)
4145{
4146 struct drbd_conf *mdev = thi->mdev;
4147 unsigned int minor = mdev_to_minor(mdev);
4148 int h;
4149
4150 sprintf(current->comm, "drbd%d_receiver", minor);
4151
4152 dev_info(DEV, "receiver (re)started\n");
4153
4154 do {
4155 h = drbd_connect(mdev);
4156 if (h == 0) {
4157 drbd_disconnect(mdev);
4158 __set_current_state(TASK_INTERRUPTIBLE);
4159 schedule_timeout(HZ);
4160 }
4161 if (h == -1) {
4162 dev_warn(DEV, "Discarding network configuration.\n");
4163 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
4164 }
4165 } while (h == 0);
4166
4167 if (h > 0) {
4168 if (get_net_conf(mdev)) {
4169 drbdd(mdev);
4170 put_net_conf(mdev);
4171 }
4172 }
4173
4174 drbd_disconnect(mdev);
4175
4176 dev_info(DEV, "receiver terminated\n");
4177 return 0;
4178}
4179
4180/* ********* acknowledge sender ******** */
4181
Philipp Reisner0b70a132010-08-20 13:36:10 +02004182static int got_RqSReply(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004183{
4184 struct p_req_state_reply *p = (struct p_req_state_reply *)h;
4185
4186 int retcode = be32_to_cpu(p->retcode);
4187
4188 if (retcode >= SS_SUCCESS) {
4189 set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
4190 } else {
4191 set_bit(CL_ST_CHG_FAIL, &mdev->flags);
4192 dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
4193 drbd_set_st_err_str(retcode), retcode);
4194 }
4195 wake_up(&mdev->state_wait);
4196
4197 return TRUE;
4198}
4199
Philipp Reisner0b70a132010-08-20 13:36:10 +02004200static int got_Ping(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004201{
4202 return drbd_send_ping_ack(mdev);
4203
4204}
4205
Philipp Reisner0b70a132010-08-20 13:36:10 +02004206static int got_PingAck(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004207{
4208 /* restore idle timeout */
4209 mdev->meta.socket->sk->sk_rcvtimeo = mdev->net_conf->ping_int*HZ;
Philipp Reisner309d1602010-03-02 15:03:44 +01004210 if (!test_and_set_bit(GOT_PING_ACK, &mdev->flags))
4211 wake_up(&mdev->misc_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004212
4213 return TRUE;
4214}
4215
Philipp Reisner0b70a132010-08-20 13:36:10 +02004216static int got_IsInSync(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004217{
4218 struct p_block_ack *p = (struct p_block_ack *)h;
4219 sector_t sector = be64_to_cpu(p->sector);
4220 int blksize = be32_to_cpu(p->blksize);
4221
4222 D_ASSERT(mdev->agreed_pro_version >= 89);
4223
4224 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4225
4226 drbd_rs_complete_io(mdev, sector);
4227 drbd_set_in_sync(mdev, sector, blksize);
4228 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
4229 mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
4230 dec_rs_pending(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02004231 atomic_add(blksize >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004232
4233 return TRUE;
4234}
4235
4236/* when we receive the ACK for a write request,
4237 * verify that we actually know about it */
4238static struct drbd_request *_ack_id_to_req(struct drbd_conf *mdev,
4239 u64 id, sector_t sector)
4240{
4241 struct hlist_head *slot = tl_hash_slot(mdev, sector);
4242 struct hlist_node *n;
4243 struct drbd_request *req;
4244
4245 hlist_for_each_entry(req, n, slot, colision) {
4246 if ((unsigned long)req == (unsigned long)id) {
4247 if (req->sector != sector) {
4248 dev_err(DEV, "_ack_id_to_req: found req %p but it has "
4249 "wrong sector (%llus versus %llus)\n", req,
4250 (unsigned long long)req->sector,
4251 (unsigned long long)sector);
4252 break;
4253 }
4254 return req;
4255 }
4256 }
4257 dev_err(DEV, "_ack_id_to_req: failed to find req %p, sector %llus in list\n",
4258 (void *)(unsigned long)id, (unsigned long long)sector);
4259 return NULL;
4260}
4261
4262typedef struct drbd_request *(req_validator_fn)
4263 (struct drbd_conf *mdev, u64 id, sector_t sector);
4264
4265static int validate_req_change_req_state(struct drbd_conf *mdev,
4266 u64 id, sector_t sector, req_validator_fn validator,
4267 const char *func, enum drbd_req_event what)
4268{
4269 struct drbd_request *req;
4270 struct bio_and_error m;
4271
4272 spin_lock_irq(&mdev->req_lock);
4273 req = validator(mdev, id, sector);
4274 if (unlikely(!req)) {
4275 spin_unlock_irq(&mdev->req_lock);
4276 dev_err(DEV, "%s: got a corrupt block_id/sector pair\n", func);
4277 return FALSE;
4278 }
4279 __req_mod(req, what, &m);
4280 spin_unlock_irq(&mdev->req_lock);
4281
4282 if (m.bio)
4283 complete_master_bio(mdev, &m);
4284 return TRUE;
4285}
4286
Philipp Reisner0b70a132010-08-20 13:36:10 +02004287static int got_BlockAck(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004288{
4289 struct p_block_ack *p = (struct p_block_ack *)h;
4290 sector_t sector = be64_to_cpu(p->sector);
4291 int blksize = be32_to_cpu(p->blksize);
4292 enum drbd_req_event what;
4293
4294 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4295
4296 if (is_syncer_block_id(p->block_id)) {
4297 drbd_set_in_sync(mdev, sector, blksize);
4298 dec_rs_pending(mdev);
4299 return TRUE;
4300 }
4301 switch (be16_to_cpu(h->command)) {
4302 case P_RS_WRITE_ACK:
4303 D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
4304 what = write_acked_by_peer_and_sis;
4305 break;
4306 case P_WRITE_ACK:
4307 D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
4308 what = write_acked_by_peer;
4309 break;
4310 case P_RECV_ACK:
4311 D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_B);
4312 what = recv_acked_by_peer;
4313 break;
4314 case P_DISCARD_ACK:
4315 D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
4316 what = conflict_discarded_by_peer;
4317 break;
4318 default:
4319 D_ASSERT(0);
4320 return FALSE;
4321 }
4322
4323 return validate_req_change_req_state(mdev, p->block_id, sector,
4324 _ack_id_to_req, __func__ , what);
4325}
4326
Philipp Reisner0b70a132010-08-20 13:36:10 +02004327static int got_NegAck(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004328{
4329 struct p_block_ack *p = (struct p_block_ack *)h;
4330 sector_t sector = be64_to_cpu(p->sector);
4331
4332 if (__ratelimit(&drbd_ratelimit_state))
4333 dev_warn(DEV, "Got NegAck packet. Peer is in troubles?\n");
4334
4335 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4336
4337 if (is_syncer_block_id(p->block_id)) {
4338 int size = be32_to_cpu(p->blksize);
4339 dec_rs_pending(mdev);
4340 drbd_rs_failed_io(mdev, sector, size);
4341 return TRUE;
4342 }
4343 return validate_req_change_req_state(mdev, p->block_id, sector,
4344 _ack_id_to_req, __func__ , neg_acked);
4345}
4346
Philipp Reisner0b70a132010-08-20 13:36:10 +02004347static int got_NegDReply(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004348{
4349 struct p_block_ack *p = (struct p_block_ack *)h;
4350 sector_t sector = be64_to_cpu(p->sector);
4351
4352 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4353 dev_err(DEV, "Got NegDReply; Sector %llus, len %u; Fail original request.\n",
4354 (unsigned long long)sector, be32_to_cpu(p->blksize));
4355
4356 return validate_req_change_req_state(mdev, p->block_id, sector,
4357 _ar_id_to_req, __func__ , neg_acked);
4358}
4359
Philipp Reisner0b70a132010-08-20 13:36:10 +02004360static int got_NegRSDReply(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004361{
4362 sector_t sector;
4363 int size;
4364 struct p_block_ack *p = (struct p_block_ack *)h;
4365
4366 sector = be64_to_cpu(p->sector);
4367 size = be32_to_cpu(p->blksize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004368
4369 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4370
4371 dec_rs_pending(mdev);
4372
4373 if (get_ldev_if_state(mdev, D_FAILED)) {
4374 drbd_rs_complete_io(mdev, sector);
4375 drbd_rs_failed_io(mdev, sector, size);
4376 put_ldev(mdev);
4377 }
4378
4379 return TRUE;
4380}
4381
Philipp Reisner0b70a132010-08-20 13:36:10 +02004382static int got_BarrierAck(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004383{
4384 struct p_barrier_ack *p = (struct p_barrier_ack *)h;
4385
4386 tl_release(mdev, p->barrier, be32_to_cpu(p->set_size));
4387
4388 return TRUE;
4389}
4390
Philipp Reisner0b70a132010-08-20 13:36:10 +02004391static int got_OVResult(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004392{
4393 struct p_block_ack *p = (struct p_block_ack *)h;
4394 struct drbd_work *w;
4395 sector_t sector;
4396 int size;
4397
4398 sector = be64_to_cpu(p->sector);
4399 size = be32_to_cpu(p->blksize);
4400
4401 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4402
4403 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
4404 drbd_ov_oos_found(mdev, sector, size);
4405 else
4406 ov_oos_print(mdev);
4407
4408 drbd_rs_complete_io(mdev, sector);
4409 dec_rs_pending(mdev);
4410
4411 if (--mdev->ov_left == 0) {
4412 w = kmalloc(sizeof(*w), GFP_NOIO);
4413 if (w) {
4414 w->cb = w_ov_finished;
4415 drbd_queue_work_front(&mdev->data.work, w);
4416 } else {
4417 dev_err(DEV, "kmalloc(w) failed.");
4418 ov_oos_print(mdev);
4419 drbd_resync_finished(mdev);
4420 }
4421 }
4422 return TRUE;
4423}
4424
Philipp Reisner02918be2010-08-20 14:35:10 +02004425static int got_skip(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisner0ced55a2010-04-30 15:26:20 +02004426{
Philipp Reisner0ced55a2010-04-30 15:26:20 +02004427 return TRUE;
4428}
4429
Philipp Reisnerb411b362009-09-25 16:07:19 -07004430struct asender_cmd {
4431 size_t pkt_size;
Philipp Reisner0b70a132010-08-20 13:36:10 +02004432 int (*process)(struct drbd_conf *mdev, struct p_header80 *h);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004433};
4434
4435static struct asender_cmd *get_asender_cmd(int cmd)
4436{
4437 static struct asender_cmd asender_tbl[] = {
4438 /* anything missing from this table is in
4439 * the drbd_cmd_handler (drbd_default_handler) table,
4440 * see the beginning of drbdd() */
Philipp Reisner0b70a132010-08-20 13:36:10 +02004441 [P_PING] = { sizeof(struct p_header80), got_Ping },
4442 [P_PING_ACK] = { sizeof(struct p_header80), got_PingAck },
Philipp Reisnerb411b362009-09-25 16:07:19 -07004443 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4444 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4445 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4446 [P_DISCARD_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4447 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
4448 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
4449 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply},
4450 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
4451 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
4452 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
4453 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
Philipp Reisner02918be2010-08-20 14:35:10 +02004454 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
Philipp Reisnerb411b362009-09-25 16:07:19 -07004455 [P_MAX_CMD] = { 0, NULL },
4456 };
4457 if (cmd > P_MAX_CMD || asender_tbl[cmd].process == NULL)
4458 return NULL;
4459 return &asender_tbl[cmd];
4460}
4461
4462int drbd_asender(struct drbd_thread *thi)
4463{
4464 struct drbd_conf *mdev = thi->mdev;
Philipp Reisner02918be2010-08-20 14:35:10 +02004465 struct p_header80 *h = &mdev->meta.rbuf.header.h80;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004466 struct asender_cmd *cmd = NULL;
4467
4468 int rv, len;
4469 void *buf = h;
4470 int received = 0;
Philipp Reisner0b70a132010-08-20 13:36:10 +02004471 int expect = sizeof(struct p_header80);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004472 int empty;
4473
4474 sprintf(current->comm, "drbd%d_asender", mdev_to_minor(mdev));
4475
4476 current->policy = SCHED_RR; /* Make this a realtime task! */
4477 current->rt_priority = 2; /* more important than all other tasks */
4478
4479 while (get_t_state(thi) == Running) {
4480 drbd_thread_current_set_cpu(mdev);
4481 if (test_and_clear_bit(SEND_PING, &mdev->flags)) {
4482 ERR_IF(!drbd_send_ping(mdev)) goto reconnect;
4483 mdev->meta.socket->sk->sk_rcvtimeo =
4484 mdev->net_conf->ping_timeo*HZ/10;
4485 }
4486
4487 /* conditionally cork;
4488 * it may hurt latency if we cork without much to send */
4489 if (!mdev->net_conf->no_cork &&
4490 3 < atomic_read(&mdev->unacked_cnt))
4491 drbd_tcp_cork(mdev->meta.socket);
4492 while (1) {
4493 clear_bit(SIGNAL_ASENDER, &mdev->flags);
4494 flush_signals(current);
4495 if (!drbd_process_done_ee(mdev)) {
4496 dev_err(DEV, "process_done_ee() = NOT_OK\n");
4497 goto reconnect;
4498 }
4499 /* to avoid race with newly queued ACKs */
4500 set_bit(SIGNAL_ASENDER, &mdev->flags);
4501 spin_lock_irq(&mdev->req_lock);
4502 empty = list_empty(&mdev->done_ee);
4503 spin_unlock_irq(&mdev->req_lock);
4504 /* new ack may have been queued right here,
4505 * but then there is also a signal pending,
4506 * and we start over... */
4507 if (empty)
4508 break;
4509 }
4510 /* but unconditionally uncork unless disabled */
4511 if (!mdev->net_conf->no_cork)
4512 drbd_tcp_uncork(mdev->meta.socket);
4513
4514 /* short circuit, recv_msg would return EINTR anyways. */
4515 if (signal_pending(current))
4516 continue;
4517
4518 rv = drbd_recv_short(mdev, mdev->meta.socket,
4519 buf, expect-received, 0);
4520 clear_bit(SIGNAL_ASENDER, &mdev->flags);
4521
4522 flush_signals(current);
4523
4524 /* Note:
4525 * -EINTR (on meta) we got a signal
4526 * -EAGAIN (on meta) rcvtimeo expired
4527 * -ECONNRESET other side closed the connection
4528 * -ERESTARTSYS (on data) we got a signal
4529 * rv < 0 other than above: unexpected error!
4530 * rv == expected: full header or command
4531 * rv < expected: "woken" by signal during receive
4532 * rv == 0 : "connection shut down by peer"
4533 */
4534 if (likely(rv > 0)) {
4535 received += rv;
4536 buf += rv;
4537 } else if (rv == 0) {
4538 dev_err(DEV, "meta connection shut down by peer.\n");
4539 goto reconnect;
4540 } else if (rv == -EAGAIN) {
4541 if (mdev->meta.socket->sk->sk_rcvtimeo ==
4542 mdev->net_conf->ping_timeo*HZ/10) {
4543 dev_err(DEV, "PingAck did not arrive in time.\n");
4544 goto reconnect;
4545 }
4546 set_bit(SEND_PING, &mdev->flags);
4547 continue;
4548 } else if (rv == -EINTR) {
4549 continue;
4550 } else {
4551 dev_err(DEV, "sock_recvmsg returned %d\n", rv);
4552 goto reconnect;
4553 }
4554
4555 if (received == expect && cmd == NULL) {
4556 if (unlikely(h->magic != BE_DRBD_MAGIC)) {
4557 dev_err(DEV, "magic?? on meta m: 0x%lx c: %d l: %d\n",
4558 (long)be32_to_cpu(h->magic),
4559 h->command, h->length);
4560 goto reconnect;
4561 }
4562 cmd = get_asender_cmd(be16_to_cpu(h->command));
4563 len = be16_to_cpu(h->length);
4564 if (unlikely(cmd == NULL)) {
4565 dev_err(DEV, "unknown command?? on meta m: 0x%lx c: %d l: %d\n",
4566 (long)be32_to_cpu(h->magic),
4567 h->command, h->length);
4568 goto disconnect;
4569 }
4570 expect = cmd->pkt_size;
Philipp Reisner0b70a132010-08-20 13:36:10 +02004571 ERR_IF(len != expect-sizeof(struct p_header80))
Philipp Reisnerb411b362009-09-25 16:07:19 -07004572 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004573 }
4574 if (received == expect) {
4575 D_ASSERT(cmd != NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004576 if (!cmd->process(mdev, h))
4577 goto reconnect;
4578
4579 buf = h;
4580 received = 0;
Philipp Reisner0b70a132010-08-20 13:36:10 +02004581 expect = sizeof(struct p_header80);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004582 cmd = NULL;
4583 }
4584 }
4585
4586 if (0) {
4587reconnect:
4588 drbd_force_state(mdev, NS(conn, C_NETWORK_FAILURE));
4589 }
4590 if (0) {
4591disconnect:
4592 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
4593 }
4594 clear_bit(SIGNAL_ASENDER, &mdev->flags);
4595
4596 D_ASSERT(mdev->state.conn < C_CONNECTED);
4597 dev_info(DEV, "asender terminated\n");
4598
4599 return 0;
4600}