blob: 9b3321e2c3cd9e54b204fd80cab53165f78a8188 [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 Reisner0b70a132010-08-20 13:36:10 +0200723 struct p_header80 *h = (struct p_header80 *) &mdev->data.sbuf.header;
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 Reisner0b70a132010-08-20 13:36:10 +0200730 struct p_header80 *h = (struct p_header80 *) &mdev->data.sbuf.header;
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 Reisner0b70a132010-08-20 13:36:10 +0200947static int drbd_recv_header(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700948{
949 int r;
950
951 r = drbd_recv(mdev, h, sizeof(*h));
952
953 if (unlikely(r != sizeof(*h))) {
954 dev_err(DEV, "short read expecting header on sock: r=%d\n", r);
955 return FALSE;
956 };
957 h->command = be16_to_cpu(h->command);
958 h->length = be16_to_cpu(h->length);
959 if (unlikely(h->magic != BE_DRBD_MAGIC)) {
960 dev_err(DEV, "magic?? on data m: 0x%lx c: %d l: %d\n",
961 (long)be32_to_cpu(h->magic),
962 h->command, h->length);
963 return FALSE;
964 }
965 mdev->last_received = jiffies;
966
967 return TRUE;
968}
969
970static enum finish_epoch drbd_flush_after_epoch(struct drbd_conf *mdev, struct drbd_epoch *epoch)
971{
972 int rv;
973
974 if (mdev->write_ordering >= WO_bdev_flush && get_ldev(mdev)) {
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400975 rv = blkdev_issue_flush(mdev->ldev->backing_bdev, GFP_KERNEL,
976 NULL, BLKDEV_IFL_WAIT);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700977 if (rv) {
978 dev_err(DEV, "local disk flush failed with status %d\n", rv);
979 /* would rather check on EOPNOTSUPP, but that is not reliable.
980 * don't try again for ANY return value != 0
981 * if (rv == -EOPNOTSUPP) */
982 drbd_bump_write_ordering(mdev, WO_drain_io);
983 }
984 put_ldev(mdev);
985 }
986
987 return drbd_may_finish_epoch(mdev, epoch, EV_BARRIER_DONE);
988}
989
990static int w_flush(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
991{
992 struct flush_work *fw = (struct flush_work *)w;
993 struct drbd_epoch *epoch = fw->epoch;
994
995 kfree(w);
996
997 if (!test_and_set_bit(DE_BARRIER_IN_NEXT_EPOCH_ISSUED, &epoch->flags))
998 drbd_flush_after_epoch(mdev, epoch);
999
1000 drbd_may_finish_epoch(mdev, epoch, EV_PUT |
1001 (mdev->state.conn < C_CONNECTED ? EV_CLEANUP : 0));
1002
1003 return 1;
1004}
1005
1006/**
1007 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
1008 * @mdev: DRBD device.
1009 * @epoch: Epoch object.
1010 * @ev: Epoch event.
1011 */
1012static enum finish_epoch drbd_may_finish_epoch(struct drbd_conf *mdev,
1013 struct drbd_epoch *epoch,
1014 enum epoch_event ev)
1015{
1016 int finish, epoch_size;
1017 struct drbd_epoch *next_epoch;
1018 int schedule_flush = 0;
1019 enum finish_epoch rv = FE_STILL_LIVE;
1020
1021 spin_lock(&mdev->epoch_lock);
1022 do {
1023 next_epoch = NULL;
1024 finish = 0;
1025
1026 epoch_size = atomic_read(&epoch->epoch_size);
1027
1028 switch (ev & ~EV_CLEANUP) {
1029 case EV_PUT:
1030 atomic_dec(&epoch->active);
1031 break;
1032 case EV_GOT_BARRIER_NR:
1033 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
1034
1035 /* Special case: If we just switched from WO_bio_barrier to
1036 WO_bdev_flush we should not finish the current epoch */
1037 if (test_bit(DE_CONTAINS_A_BARRIER, &epoch->flags) && epoch_size == 1 &&
1038 mdev->write_ordering != WO_bio_barrier &&
1039 epoch == mdev->current_epoch)
1040 clear_bit(DE_CONTAINS_A_BARRIER, &epoch->flags);
1041 break;
1042 case EV_BARRIER_DONE:
1043 set_bit(DE_BARRIER_IN_NEXT_EPOCH_DONE, &epoch->flags);
1044 break;
1045 case EV_BECAME_LAST:
1046 /* nothing to do*/
1047 break;
1048 }
1049
Philipp Reisnerb411b362009-09-25 16:07:19 -07001050 if (epoch_size != 0 &&
1051 atomic_read(&epoch->active) == 0 &&
1052 test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags) &&
1053 epoch->list.prev == &mdev->current_epoch->list &&
1054 !test_bit(DE_IS_FINISHING, &epoch->flags)) {
1055 /* Nearly all conditions are met to finish that epoch... */
1056 if (test_bit(DE_BARRIER_IN_NEXT_EPOCH_DONE, &epoch->flags) ||
1057 mdev->write_ordering == WO_none ||
1058 (epoch_size == 1 && test_bit(DE_CONTAINS_A_BARRIER, &epoch->flags)) ||
1059 ev & EV_CLEANUP) {
1060 finish = 1;
1061 set_bit(DE_IS_FINISHING, &epoch->flags);
1062 } else if (!test_bit(DE_BARRIER_IN_NEXT_EPOCH_ISSUED, &epoch->flags) &&
1063 mdev->write_ordering == WO_bio_barrier) {
1064 atomic_inc(&epoch->active);
1065 schedule_flush = 1;
1066 }
1067 }
1068 if (finish) {
1069 if (!(ev & EV_CLEANUP)) {
1070 spin_unlock(&mdev->epoch_lock);
1071 drbd_send_b_ack(mdev, epoch->barrier_nr, epoch_size);
1072 spin_lock(&mdev->epoch_lock);
1073 }
1074 dec_unacked(mdev);
1075
1076 if (mdev->current_epoch != epoch) {
1077 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1078 list_del(&epoch->list);
1079 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
1080 mdev->epochs--;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001081 kfree(epoch);
1082
1083 if (rv == FE_STILL_LIVE)
1084 rv = FE_DESTROYED;
1085 } else {
1086 epoch->flags = 0;
1087 atomic_set(&epoch->epoch_size, 0);
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001088 /* atomic_set(&epoch->active, 0); is already zero */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001089 if (rv == FE_STILL_LIVE)
1090 rv = FE_RECYCLED;
1091 }
1092 }
1093
1094 if (!next_epoch)
1095 break;
1096
1097 epoch = next_epoch;
1098 } while (1);
1099
1100 spin_unlock(&mdev->epoch_lock);
1101
1102 if (schedule_flush) {
1103 struct flush_work *fw;
1104 fw = kmalloc(sizeof(*fw), GFP_ATOMIC);
1105 if (fw) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001106 fw->w.cb = w_flush;
1107 fw->epoch = epoch;
1108 drbd_queue_work(&mdev->data.work, &fw->w);
1109 } else {
1110 dev_warn(DEV, "Could not kmalloc a flush_work obj\n");
1111 set_bit(DE_BARRIER_IN_NEXT_EPOCH_ISSUED, &epoch->flags);
1112 /* That is not a recursion, only one level */
1113 drbd_may_finish_epoch(mdev, epoch, EV_BARRIER_DONE);
1114 drbd_may_finish_epoch(mdev, epoch, EV_PUT);
1115 }
1116 }
1117
1118 return rv;
1119}
1120
1121/**
1122 * drbd_bump_write_ordering() - Fall back to an other write ordering method
1123 * @mdev: DRBD device.
1124 * @wo: Write ordering method to try.
1125 */
1126void drbd_bump_write_ordering(struct drbd_conf *mdev, enum write_ordering_e wo) __must_hold(local)
1127{
1128 enum write_ordering_e pwo;
1129 static char *write_ordering_str[] = {
1130 [WO_none] = "none",
1131 [WO_drain_io] = "drain",
1132 [WO_bdev_flush] = "flush",
1133 [WO_bio_barrier] = "barrier",
1134 };
1135
1136 pwo = mdev->write_ordering;
1137 wo = min(pwo, wo);
1138 if (wo == WO_bio_barrier && mdev->ldev->dc.no_disk_barrier)
1139 wo = WO_bdev_flush;
1140 if (wo == WO_bdev_flush && mdev->ldev->dc.no_disk_flush)
1141 wo = WO_drain_io;
1142 if (wo == WO_drain_io && mdev->ldev->dc.no_disk_drain)
1143 wo = WO_none;
1144 mdev->write_ordering = wo;
1145 if (pwo != mdev->write_ordering || wo == WO_bio_barrier)
1146 dev_info(DEV, "Method to ensure write ordering: %s\n", write_ordering_str[mdev->write_ordering]);
1147}
1148
1149/**
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001150 * drbd_submit_ee()
1151 * @mdev: DRBD device.
1152 * @e: epoch entry
1153 * @rw: flag field, see bio->bi_rw
1154 */
1155/* TODO allocate from our own bio_set. */
1156int drbd_submit_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e,
1157 const unsigned rw, const int fault_type)
1158{
1159 struct bio *bios = NULL;
1160 struct bio *bio;
1161 struct page *page = e->pages;
1162 sector_t sector = e->sector;
1163 unsigned ds = e->size;
1164 unsigned n_bios = 0;
1165 unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
1166
1167 /* In most cases, we will only need one bio. But in case the lower
1168 * level restrictions happen to be different at this offset on this
1169 * side than those of the sending peer, we may need to submit the
1170 * request in more than one bio. */
1171next_bio:
1172 bio = bio_alloc(GFP_NOIO, nr_pages);
1173 if (!bio) {
1174 dev_err(DEV, "submit_ee: Allocation of a bio failed\n");
1175 goto fail;
1176 }
1177 /* > e->sector, unless this is the first bio */
1178 bio->bi_sector = sector;
1179 bio->bi_bdev = mdev->ldev->backing_bdev;
1180 /* we special case some flags in the multi-bio case, see below
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001181 * (REQ_UNPLUG, REQ_HARDBARRIER) */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001182 bio->bi_rw = rw;
1183 bio->bi_private = e;
1184 bio->bi_end_io = drbd_endio_sec;
1185
1186 bio->bi_next = bios;
1187 bios = bio;
1188 ++n_bios;
1189
1190 page_chain_for_each(page) {
1191 unsigned len = min_t(unsigned, ds, PAGE_SIZE);
1192 if (!bio_add_page(bio, page, len, 0)) {
1193 /* a single page must always be possible! */
1194 BUG_ON(bio->bi_vcnt == 0);
1195 goto next_bio;
1196 }
1197 ds -= len;
1198 sector += len >> 9;
1199 --nr_pages;
1200 }
1201 D_ASSERT(page == NULL);
1202 D_ASSERT(ds == 0);
1203
1204 atomic_set(&e->pending_bios, n_bios);
1205 do {
1206 bio = bios;
1207 bios = bios->bi_next;
1208 bio->bi_next = NULL;
1209
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001210 /* strip off REQ_UNPLUG unless it is the last bio */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001211 if (bios)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001212 bio->bi_rw &= ~REQ_UNPLUG;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001213
1214 drbd_generic_make_request(mdev, fault_type, bio);
1215
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001216 /* strip off REQ_HARDBARRIER,
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001217 * unless it is the first or last bio */
1218 if (bios && bios->bi_next)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001219 bios->bi_rw &= ~REQ_HARDBARRIER;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001220 } while (bios);
1221 maybe_kick_lo(mdev);
1222 return 0;
1223
1224fail:
1225 while (bios) {
1226 bio = bios;
1227 bios = bios->bi_next;
1228 bio_put(bio);
1229 }
1230 return -ENOMEM;
1231}
1232
1233/**
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001234 * w_e_reissue() - Worker callback; Resubmit a bio, without REQ_HARDBARRIER set
Philipp Reisnerb411b362009-09-25 16:07:19 -07001235 * @mdev: DRBD device.
1236 * @w: work object.
1237 * @cancel: The connection will be closed anyways (unused in this callback)
1238 */
1239int w_e_reissue(struct drbd_conf *mdev, struct drbd_work *w, int cancel) __releases(local)
1240{
1241 struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001242 /* We leave DE_CONTAINS_A_BARRIER and EE_IS_BARRIER in place,
1243 (and DE_BARRIER_IN_NEXT_EPOCH_ISSUED in the previous Epoch)
1244 so that we can finish that epoch in drbd_may_finish_epoch().
1245 That is necessary if we already have a long chain of Epochs, before
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001246 we realize that REQ_HARDBARRIER is actually not supported */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001247
1248 /* As long as the -ENOTSUPP on the barrier is reported immediately
1249 that will never trigger. If it is reported late, we will just
1250 print that warning and continue correctly for all future requests
1251 with WO_bdev_flush */
1252 if (previous_epoch(mdev, e->epoch))
1253 dev_warn(DEV, "Write ordering was not enforced (one time event)\n");
1254
Philipp Reisnerb411b362009-09-25 16:07:19 -07001255 /* we still have a local reference,
1256 * get_ldev was done in receive_Data. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001257
1258 e->w.cb = e_end_block;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001259 if (drbd_submit_ee(mdev, e, WRITE, DRBD_FAULT_DT_WR) != 0) {
1260 /* drbd_submit_ee fails for one reason only:
1261 * if was not able to allocate sufficient bios.
1262 * requeue, try again later. */
1263 e->w.cb = w_e_reissue;
1264 drbd_queue_work(&mdev->data.work, &e->w);
1265 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001266 return 1;
1267}
1268
Philipp Reisner0b70a132010-08-20 13:36:10 +02001269static int receive_Barrier(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001270{
1271 int rv, issue_flush;
1272 struct p_barrier *p = (struct p_barrier *)h;
1273 struct drbd_epoch *epoch;
1274
1275 ERR_IF(h->length != (sizeof(*p)-sizeof(*h))) return FALSE;
1276
1277 rv = drbd_recv(mdev, h->payload, h->length);
1278 ERR_IF(rv != h->length) return FALSE;
1279
1280 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 Reisner0b70a132010-08-20 13:36:10 +02001573static int receive_DataReply(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001574{
1575 struct drbd_request *req;
1576 sector_t sector;
1577 unsigned int header_size, data_size;
1578 int ok;
1579 struct p_data *p = (struct p_data *)h;
1580
1581 header_size = sizeof(*p) - sizeof(*h);
1582 data_size = h->length - header_size;
1583
1584 ERR_IF(data_size == 0) return FALSE;
1585
1586 if (drbd_recv(mdev, h->payload, header_size) != header_size)
1587 return FALSE;
1588
1589 sector = be64_to_cpu(p->sector);
1590
1591 spin_lock_irq(&mdev->req_lock);
1592 req = _ar_id_to_req(mdev, p->block_id, sector);
1593 spin_unlock_irq(&mdev->req_lock);
1594 if (unlikely(!req)) {
1595 dev_err(DEV, "Got a corrupt block_id/sector pair(1).\n");
1596 return FALSE;
1597 }
1598
1599 /* hlist_del(&req->colision) is done in _req_may_be_done, to avoid
1600 * special casing it there for the various failure cases.
1601 * still no race with drbd_fail_pending_reads */
1602 ok = recv_dless_read(mdev, req, sector, data_size);
1603
1604 if (ok)
1605 req_mod(req, data_received);
1606 /* else: nothing. handled from drbd_disconnect...
1607 * I don't think we may complete this just yet
1608 * in case we are "on-disconnect: freeze" */
1609
1610 return ok;
1611}
1612
Philipp Reisner0b70a132010-08-20 13:36:10 +02001613static int receive_RSDataReply(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001614{
1615 sector_t sector;
1616 unsigned int header_size, data_size;
1617 int ok;
1618 struct p_data *p = (struct p_data *)h;
1619
1620 header_size = sizeof(*p) - sizeof(*h);
1621 data_size = h->length - header_size;
1622
1623 ERR_IF(data_size == 0) return FALSE;
1624
1625 if (drbd_recv(mdev, h->payload, header_size) != header_size)
1626 return FALSE;
1627
1628 sector = be64_to_cpu(p->sector);
1629 D_ASSERT(p->block_id == ID_SYNCER);
1630
1631 if (get_ldev(mdev)) {
1632 /* data is submitted to disk within recv_resync_read.
1633 * corresponding put_ldev done below on error,
1634 * or in drbd_endio_write_sec. */
1635 ok = recv_resync_read(mdev, sector, data_size);
1636 } else {
1637 if (__ratelimit(&drbd_ratelimit_state))
1638 dev_err(DEV, "Can not write resync data to local disk.\n");
1639
1640 ok = drbd_drain_block(mdev, data_size);
1641
1642 drbd_send_ack_dp(mdev, P_NEG_ACK, p);
1643 }
1644
Philipp Reisner778f2712010-07-06 11:14:00 +02001645 atomic_add(data_size >> 9, &mdev->rs_sect_in);
1646
Philipp Reisnerb411b362009-09-25 16:07:19 -07001647 return ok;
1648}
1649
1650/* e_end_block() is called via drbd_process_done_ee().
1651 * this means this function only runs in the asender thread
1652 */
1653static int e_end_block(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1654{
1655 struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w;
1656 sector_t sector = e->sector;
1657 struct drbd_epoch *epoch;
1658 int ok = 1, pcmd;
1659
1660 if (e->flags & EE_IS_BARRIER) {
1661 epoch = previous_epoch(mdev, e->epoch);
1662 if (epoch)
1663 drbd_may_finish_epoch(mdev, epoch, EV_BARRIER_DONE + (cancel ? EV_CLEANUP : 0));
1664 }
1665
1666 if (mdev->net_conf->wire_protocol == DRBD_PROT_C) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001667 if (likely((e->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001668 pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
1669 mdev->state.conn <= C_PAUSED_SYNC_T &&
1670 e->flags & EE_MAY_SET_IN_SYNC) ?
1671 P_RS_WRITE_ACK : P_WRITE_ACK;
1672 ok &= drbd_send_ack(mdev, pcmd, e);
1673 if (pcmd == P_RS_WRITE_ACK)
1674 drbd_set_in_sync(mdev, sector, e->size);
1675 } else {
1676 ok = drbd_send_ack(mdev, P_NEG_ACK, e);
1677 /* we expect it to be marked out of sync anyways...
1678 * maybe assert this? */
1679 }
1680 dec_unacked(mdev);
1681 }
1682 /* we delete from the conflict detection hash _after_ we sent out the
1683 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
1684 if (mdev->net_conf->two_primaries) {
1685 spin_lock_irq(&mdev->req_lock);
1686 D_ASSERT(!hlist_unhashed(&e->colision));
1687 hlist_del_init(&e->colision);
1688 spin_unlock_irq(&mdev->req_lock);
1689 } else {
1690 D_ASSERT(hlist_unhashed(&e->colision));
1691 }
1692
1693 drbd_may_finish_epoch(mdev, e->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
1694
1695 return ok;
1696}
1697
1698static int e_send_discard_ack(struct drbd_conf *mdev, struct drbd_work *w, int unused)
1699{
1700 struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w;
1701 int ok = 1;
1702
1703 D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
1704 ok = drbd_send_ack(mdev, P_DISCARD_ACK, e);
1705
1706 spin_lock_irq(&mdev->req_lock);
1707 D_ASSERT(!hlist_unhashed(&e->colision));
1708 hlist_del_init(&e->colision);
1709 spin_unlock_irq(&mdev->req_lock);
1710
1711 dec_unacked(mdev);
1712
1713 return ok;
1714}
1715
1716/* Called from receive_Data.
1717 * Synchronize packets on sock with packets on msock.
1718 *
1719 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
1720 * packet traveling on msock, they are still processed in the order they have
1721 * been sent.
1722 *
1723 * Note: we don't care for Ack packets overtaking P_DATA packets.
1724 *
1725 * In case packet_seq is larger than mdev->peer_seq number, there are
1726 * outstanding packets on the msock. We wait for them to arrive.
1727 * In case we are the logically next packet, we update mdev->peer_seq
1728 * ourselves. Correctly handles 32bit wrap around.
1729 *
1730 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
1731 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
1732 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
1733 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
1734 *
1735 * returns 0 if we may process the packet,
1736 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
1737static int drbd_wait_peer_seq(struct drbd_conf *mdev, const u32 packet_seq)
1738{
1739 DEFINE_WAIT(wait);
1740 unsigned int p_seq;
1741 long timeout;
1742 int ret = 0;
1743 spin_lock(&mdev->peer_seq_lock);
1744 for (;;) {
1745 prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
1746 if (seq_le(packet_seq, mdev->peer_seq+1))
1747 break;
1748 if (signal_pending(current)) {
1749 ret = -ERESTARTSYS;
1750 break;
1751 }
1752 p_seq = mdev->peer_seq;
1753 spin_unlock(&mdev->peer_seq_lock);
1754 timeout = schedule_timeout(30*HZ);
1755 spin_lock(&mdev->peer_seq_lock);
1756 if (timeout == 0 && p_seq == mdev->peer_seq) {
1757 ret = -ETIMEDOUT;
1758 dev_err(DEV, "ASSERT FAILED waited 30 seconds for sequence update, forcing reconnect\n");
1759 break;
1760 }
1761 }
1762 finish_wait(&mdev->seq_wait, &wait);
1763 if (mdev->peer_seq+1 == packet_seq)
1764 mdev->peer_seq++;
1765 spin_unlock(&mdev->peer_seq_lock);
1766 return ret;
1767}
1768
1769/* mirrored write */
Philipp Reisner0b70a132010-08-20 13:36:10 +02001770static int receive_Data(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001771{
1772 sector_t sector;
1773 struct drbd_epoch_entry *e;
1774 struct p_data *p = (struct p_data *)h;
1775 int header_size, data_size;
1776 int rw = WRITE;
1777 u32 dp_flags;
1778
1779 header_size = sizeof(*p) - sizeof(*h);
1780 data_size = h->length - header_size;
1781
1782 ERR_IF(data_size == 0) return FALSE;
1783
1784 if (drbd_recv(mdev, h->payload, header_size) != header_size)
1785 return FALSE;
1786
1787 if (!get_ldev(mdev)) {
1788 if (__ratelimit(&drbd_ratelimit_state))
1789 dev_err(DEV, "Can not write mirrored data block "
1790 "to local disk.\n");
1791 spin_lock(&mdev->peer_seq_lock);
1792 if (mdev->peer_seq+1 == be32_to_cpu(p->seq_num))
1793 mdev->peer_seq++;
1794 spin_unlock(&mdev->peer_seq_lock);
1795
1796 drbd_send_ack_dp(mdev, P_NEG_ACK, p);
1797 atomic_inc(&mdev->current_epoch->epoch_size);
1798 return drbd_drain_block(mdev, data_size);
1799 }
1800
1801 /* get_ldev(mdev) successful.
1802 * Corresponding put_ldev done either below (on various errors),
1803 * or in drbd_endio_write_sec, if we successfully submit the data at
1804 * the end of this function. */
1805
1806 sector = be64_to_cpu(p->sector);
1807 e = read_in_block(mdev, p->block_id, sector, data_size);
1808 if (!e) {
1809 put_ldev(mdev);
1810 return FALSE;
1811 }
1812
Philipp Reisnerb411b362009-09-25 16:07:19 -07001813 e->w.cb = e_end_block;
1814
1815 spin_lock(&mdev->epoch_lock);
1816 e->epoch = mdev->current_epoch;
1817 atomic_inc(&e->epoch->epoch_size);
1818 atomic_inc(&e->epoch->active);
1819
1820 if (mdev->write_ordering == WO_bio_barrier && atomic_read(&e->epoch->epoch_size) == 1) {
1821 struct drbd_epoch *epoch;
1822 /* Issue a barrier if we start a new epoch, and the previous epoch
1823 was not a epoch containing a single request which already was
1824 a Barrier. */
1825 epoch = list_entry(e->epoch->list.prev, struct drbd_epoch, list);
1826 if (epoch == e->epoch) {
1827 set_bit(DE_CONTAINS_A_BARRIER, &e->epoch->flags);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001828 rw |= REQ_HARDBARRIER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001829 e->flags |= EE_IS_BARRIER;
1830 } else {
1831 if (atomic_read(&epoch->epoch_size) > 1 ||
1832 !test_bit(DE_CONTAINS_A_BARRIER, &epoch->flags)) {
1833 set_bit(DE_BARRIER_IN_NEXT_EPOCH_ISSUED, &epoch->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001834 set_bit(DE_CONTAINS_A_BARRIER, &e->epoch->flags);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001835 rw |= REQ_HARDBARRIER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001836 e->flags |= EE_IS_BARRIER;
1837 }
1838 }
1839 }
1840 spin_unlock(&mdev->epoch_lock);
1841
1842 dp_flags = be32_to_cpu(p->dp_flags);
1843 if (dp_flags & DP_HARDBARRIER) {
1844 dev_err(DEV, "ASSERT FAILED would have submitted barrier request\n");
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001845 /* rw |= REQ_HARDBARRIER; */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001846 }
1847 if (dp_flags & DP_RW_SYNC)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001848 rw |= REQ_SYNC | REQ_UNPLUG;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001849 if (dp_flags & DP_MAY_SET_IN_SYNC)
1850 e->flags |= EE_MAY_SET_IN_SYNC;
1851
1852 /* I'm the receiver, I do hold a net_cnt reference. */
1853 if (!mdev->net_conf->two_primaries) {
1854 spin_lock_irq(&mdev->req_lock);
1855 } else {
1856 /* don't get the req_lock yet,
1857 * we may sleep in drbd_wait_peer_seq */
1858 const int size = e->size;
1859 const int discard = test_bit(DISCARD_CONCURRENT, &mdev->flags);
1860 DEFINE_WAIT(wait);
1861 struct drbd_request *i;
1862 struct hlist_node *n;
1863 struct hlist_head *slot;
1864 int first;
1865
1866 D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
1867 BUG_ON(mdev->ee_hash == NULL);
1868 BUG_ON(mdev->tl_hash == NULL);
1869
1870 /* conflict detection and handling:
1871 * 1. wait on the sequence number,
1872 * in case this data packet overtook ACK packets.
1873 * 2. check our hash tables for conflicting requests.
1874 * we only need to walk the tl_hash, since an ee can not
1875 * have a conflict with an other ee: on the submitting
1876 * node, the corresponding req had already been conflicting,
1877 * and a conflicting req is never sent.
1878 *
1879 * Note: for two_primaries, we are protocol C,
1880 * so there cannot be any request that is DONE
1881 * but still on the transfer log.
1882 *
1883 * unconditionally add to the ee_hash.
1884 *
1885 * if no conflicting request is found:
1886 * submit.
1887 *
1888 * if any conflicting request is found
1889 * that has not yet been acked,
1890 * AND I have the "discard concurrent writes" flag:
1891 * queue (via done_ee) the P_DISCARD_ACK; OUT.
1892 *
1893 * if any conflicting request is found:
1894 * block the receiver, waiting on misc_wait
1895 * until no more conflicting requests are there,
1896 * or we get interrupted (disconnect).
1897 *
1898 * we do not just write after local io completion of those
1899 * requests, but only after req is done completely, i.e.
1900 * we wait for the P_DISCARD_ACK to arrive!
1901 *
1902 * then proceed normally, i.e. submit.
1903 */
1904 if (drbd_wait_peer_seq(mdev, be32_to_cpu(p->seq_num)))
1905 goto out_interrupted;
1906
1907 spin_lock_irq(&mdev->req_lock);
1908
1909 hlist_add_head(&e->colision, ee_hash_slot(mdev, sector));
1910
1911#define OVERLAPS overlaps(i->sector, i->size, sector, size)
1912 slot = tl_hash_slot(mdev, sector);
1913 first = 1;
1914 for (;;) {
1915 int have_unacked = 0;
1916 int have_conflict = 0;
1917 prepare_to_wait(&mdev->misc_wait, &wait,
1918 TASK_INTERRUPTIBLE);
1919 hlist_for_each_entry(i, n, slot, colision) {
1920 if (OVERLAPS) {
1921 /* only ALERT on first iteration,
1922 * we may be woken up early... */
1923 if (first)
1924 dev_alert(DEV, "%s[%u] Concurrent local write detected!"
1925 " new: %llus +%u; pending: %llus +%u\n",
1926 current->comm, current->pid,
1927 (unsigned long long)sector, size,
1928 (unsigned long long)i->sector, i->size);
1929 if (i->rq_state & RQ_NET_PENDING)
1930 ++have_unacked;
1931 ++have_conflict;
1932 }
1933 }
1934#undef OVERLAPS
1935 if (!have_conflict)
1936 break;
1937
1938 /* Discard Ack only for the _first_ iteration */
1939 if (first && discard && have_unacked) {
1940 dev_alert(DEV, "Concurrent write! [DISCARD BY FLAG] sec=%llus\n",
1941 (unsigned long long)sector);
1942 inc_unacked(mdev);
1943 e->w.cb = e_send_discard_ack;
1944 list_add_tail(&e->w.list, &mdev->done_ee);
1945
1946 spin_unlock_irq(&mdev->req_lock);
1947
1948 /* we could probably send that P_DISCARD_ACK ourselves,
1949 * but I don't like the receiver using the msock */
1950
1951 put_ldev(mdev);
1952 wake_asender(mdev);
1953 finish_wait(&mdev->misc_wait, &wait);
1954 return TRUE;
1955 }
1956
1957 if (signal_pending(current)) {
1958 hlist_del_init(&e->colision);
1959
1960 spin_unlock_irq(&mdev->req_lock);
1961
1962 finish_wait(&mdev->misc_wait, &wait);
1963 goto out_interrupted;
1964 }
1965
1966 spin_unlock_irq(&mdev->req_lock);
1967 if (first) {
1968 first = 0;
1969 dev_alert(DEV, "Concurrent write! [W AFTERWARDS] "
1970 "sec=%llus\n", (unsigned long long)sector);
1971 } else if (discard) {
1972 /* we had none on the first iteration.
1973 * there must be none now. */
1974 D_ASSERT(have_unacked == 0);
1975 }
1976 schedule();
1977 spin_lock_irq(&mdev->req_lock);
1978 }
1979 finish_wait(&mdev->misc_wait, &wait);
1980 }
1981
1982 list_add(&e->w.list, &mdev->active_ee);
1983 spin_unlock_irq(&mdev->req_lock);
1984
1985 switch (mdev->net_conf->wire_protocol) {
1986 case DRBD_PROT_C:
1987 inc_unacked(mdev);
1988 /* corresponding dec_unacked() in e_end_block()
1989 * respective _drbd_clear_done_ee */
1990 break;
1991 case DRBD_PROT_B:
1992 /* I really don't like it that the receiver thread
1993 * sends on the msock, but anyways */
1994 drbd_send_ack(mdev, P_RECV_ACK, e);
1995 break;
1996 case DRBD_PROT_A:
1997 /* nothing to do */
1998 break;
1999 }
2000
2001 if (mdev->state.pdsk == D_DISKLESS) {
2002 /* In case we have the only disk of the cluster, */
2003 drbd_set_out_of_sync(mdev, e->sector, e->size);
2004 e->flags |= EE_CALL_AL_COMPLETE_IO;
2005 drbd_al_begin_io(mdev, e->sector);
2006 }
2007
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002008 if (drbd_submit_ee(mdev, e, rw, DRBD_FAULT_DT_WR) == 0)
2009 return TRUE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002010
2011out_interrupted:
2012 /* yes, the epoch_size now is imbalanced.
2013 * but we drop the connection anyways, so we don't have a chance to
2014 * receive a barrier... atomic_inc(&mdev->epoch_size); */
2015 put_ldev(mdev);
2016 drbd_free_ee(mdev, e);
2017 return FALSE;
2018}
2019
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002020/* We may throttle resync, if the lower device seems to be busy,
2021 * and current sync rate is above c_min_rate.
2022 *
2023 * To decide whether or not the lower device is busy, we use a scheme similar
2024 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
2025 * (more than 64 sectors) of activity we cannot account for with our own resync
2026 * activity, it obviously is "busy".
2027 *
2028 * The current sync rate used here uses only the most recent two step marks,
2029 * to have a short time average so we can react faster.
2030 */
2031int drbd_rs_should_slow_down(struct drbd_conf *mdev)
2032{
2033 struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
2034 unsigned long db, dt, dbdt;
2035 int curr_events;
2036 int throttle = 0;
2037
2038 /* feature disabled? */
2039 if (mdev->sync_conf.c_min_rate == 0)
2040 return 0;
2041
2042 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
2043 (int)part_stat_read(&disk->part0, sectors[1]) -
2044 atomic_read(&mdev->rs_sect_ev);
2045 if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
2046 unsigned long rs_left;
2047 int i;
2048
2049 mdev->rs_last_events = curr_events;
2050
2051 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
2052 * approx. */
2053 i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-2) % DRBD_SYNC_MARKS;
2054 rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
2055
2056 dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
2057 if (!dt)
2058 dt++;
2059 db = mdev->rs_mark_left[i] - rs_left;
2060 dbdt = Bit2KB(db/dt);
2061
2062 if (dbdt > mdev->sync_conf.c_min_rate)
2063 throttle = 1;
2064 }
2065 return throttle;
2066}
2067
2068
Philipp Reisner0b70a132010-08-20 13:36:10 +02002069static int receive_DataRequest(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002070{
2071 sector_t sector;
2072 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
2073 struct drbd_epoch_entry *e;
2074 struct digest_info *di = NULL;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002075 struct p_block_req *p = (struct p_block_req *)h;
2076 const int brps = sizeof(*p)-sizeof(*h);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002077 int size, digest_size;
2078 unsigned int fault_type;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002079
Philipp Reisnerb411b362009-09-25 16:07:19 -07002080
2081 if (drbd_recv(mdev, h->payload, brps) != brps)
2082 return FALSE;
2083
2084 sector = be64_to_cpu(p->sector);
2085 size = be32_to_cpu(p->blksize);
2086
2087 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_SEGMENT_SIZE) {
2088 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2089 (unsigned long long)sector, size);
2090 return FALSE;
2091 }
2092 if (sector + (size>>9) > capacity) {
2093 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2094 (unsigned long long)sector, size);
2095 return FALSE;
2096 }
2097
2098 if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
2099 if (__ratelimit(&drbd_ratelimit_state))
2100 dev_err(DEV, "Can not satisfy peer's read request, "
2101 "no local data.\n");
2102 drbd_send_ack_rp(mdev, h->command == P_DATA_REQUEST ? P_NEG_DREPLY :
2103 P_NEG_RS_DREPLY , p);
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02002104 return drbd_drain_block(mdev, h->length - brps);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002105 }
2106
2107 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
2108 * "criss-cross" setup, that might cause write-out on some other DRBD,
2109 * which in turn might block on the other node at this very place. */
2110 e = drbd_alloc_ee(mdev, p->block_id, sector, size, GFP_NOIO);
2111 if (!e) {
2112 put_ldev(mdev);
2113 return FALSE;
2114 }
2115
Philipp Reisnerb411b362009-09-25 16:07:19 -07002116 switch (h->command) {
2117 case P_DATA_REQUEST:
2118 e->w.cb = w_e_end_data_req;
2119 fault_type = DRBD_FAULT_DT_RD;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002120 /* application IO, don't drbd_rs_begin_io */
2121 goto submit;
2122
Philipp Reisnerb411b362009-09-25 16:07:19 -07002123 case P_RS_DATA_REQUEST:
2124 e->w.cb = w_e_end_rsdata_req;
2125 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002126 break;
2127
2128 case P_OV_REPLY:
2129 case P_CSUM_RS_REQUEST:
2130 fault_type = DRBD_FAULT_RS_RD;
2131 digest_size = h->length - brps ;
2132 di = kmalloc(sizeof(*di) + digest_size, GFP_NOIO);
2133 if (!di)
2134 goto out_free_e;
2135
2136 di->digest_size = digest_size;
2137 di->digest = (((char *)di)+sizeof(struct digest_info));
2138
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +02002139 e->digest = di;
2140 e->flags |= EE_HAS_DIGEST;
2141
Philipp Reisnerb411b362009-09-25 16:07:19 -07002142 if (drbd_recv(mdev, di->digest, digest_size) != digest_size)
2143 goto out_free_e;
2144
Philipp Reisnerb411b362009-09-25 16:07:19 -07002145 if (h->command == P_CSUM_RS_REQUEST) {
2146 D_ASSERT(mdev->agreed_pro_version >= 89);
2147 e->w.cb = w_e_end_csum_rs_req;
2148 } else if (h->command == P_OV_REPLY) {
2149 e->w.cb = w_e_end_ov_reply;
2150 dec_rs_pending(mdev);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002151 /* drbd_rs_begin_io done when we sent this request,
2152 * but accounting still needs to be done. */
2153 goto submit_for_resync;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002154 }
2155 break;
2156
2157 case P_OV_REQUEST:
2158 if (mdev->state.conn >= C_CONNECTED &&
2159 mdev->state.conn != C_VERIFY_T)
2160 dev_warn(DEV, "ASSERT FAILED: got P_OV_REQUEST while being %s\n",
2161 drbd_conn_str(mdev->state.conn));
2162 if (mdev->ov_start_sector == ~(sector_t)0 &&
2163 mdev->agreed_pro_version >= 90) {
2164 mdev->ov_start_sector = sector;
2165 mdev->ov_position = sector;
2166 mdev->ov_left = mdev->rs_total - BM_SECT_TO_BIT(sector);
2167 dev_info(DEV, "Online Verify start sector: %llu\n",
2168 (unsigned long long)sector);
2169 }
2170 e->w.cb = w_e_end_ov_req;
2171 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002172 break;
2173
Philipp Reisnerb411b362009-09-25 16:07:19 -07002174 default:
2175 dev_err(DEV, "unexpected command (%s) in receive_DataRequest\n",
2176 cmdname(h->command));
2177 fault_type = DRBD_FAULT_MAX;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002178 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002179 }
2180
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002181 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2182 * wrt the receiver, but it is not as straightforward as it may seem.
2183 * Various places in the resync start and stop logic assume resync
2184 * requests are processed in order, requeuing this on the worker thread
2185 * introduces a bunch of new code for synchronization between threads.
2186 *
2187 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2188 * "forever", throttling after drbd_rs_begin_io will lock that extent
2189 * for application writes for the same time. For now, just throttle
2190 * here, where the rest of the code expects the receiver to sleep for
2191 * a while, anyways.
2192 */
2193
2194 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2195 * this defers syncer requests for some time, before letting at least
2196 * on request through. The resync controller on the receiving side
2197 * will adapt to the incoming rate accordingly.
2198 *
2199 * We cannot throttle here if remote is Primary/SyncTarget:
2200 * we would also throttle its application reads.
2201 * In that case, throttling is done on the SyncTarget only.
2202 */
2203 if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev))
2204 msleep(100);
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002205 if (drbd_rs_begin_io(mdev, e->sector))
2206 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002207
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002208submit_for_resync:
2209 atomic_add(size >> 9, &mdev->rs_sect_ev);
2210
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002211submit:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002212 inc_unacked(mdev);
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002213 spin_lock_irq(&mdev->req_lock);
2214 list_add_tail(&e->w.list, &mdev->read_ee);
2215 spin_unlock_irq(&mdev->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002216
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002217 if (drbd_submit_ee(mdev, e, READ, fault_type) == 0)
2218 return TRUE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002219
2220out_free_e:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002221 put_ldev(mdev);
2222 drbd_free_ee(mdev, e);
2223 return FALSE;
2224}
2225
2226static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
2227{
2228 int self, peer, rv = -100;
2229 unsigned long ch_self, ch_peer;
2230
2231 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2232 peer = mdev->p_uuid[UI_BITMAP] & 1;
2233
2234 ch_peer = mdev->p_uuid[UI_SIZE];
2235 ch_self = mdev->comm_bm_set;
2236
2237 switch (mdev->net_conf->after_sb_0p) {
2238 case ASB_CONSENSUS:
2239 case ASB_DISCARD_SECONDARY:
2240 case ASB_CALL_HELPER:
2241 dev_err(DEV, "Configuration error.\n");
2242 break;
2243 case ASB_DISCONNECT:
2244 break;
2245 case ASB_DISCARD_YOUNGER_PRI:
2246 if (self == 0 && peer == 1) {
2247 rv = -1;
2248 break;
2249 }
2250 if (self == 1 && peer == 0) {
2251 rv = 1;
2252 break;
2253 }
2254 /* Else fall through to one of the other strategies... */
2255 case ASB_DISCARD_OLDER_PRI:
2256 if (self == 0 && peer == 1) {
2257 rv = 1;
2258 break;
2259 }
2260 if (self == 1 && peer == 0) {
2261 rv = -1;
2262 break;
2263 }
2264 /* Else fall through to one of the other strategies... */
Lars Ellenbergad19bf62009-10-14 09:36:49 +02002265 dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
Philipp Reisnerb411b362009-09-25 16:07:19 -07002266 "Using discard-least-changes instead\n");
2267 case ASB_DISCARD_ZERO_CHG:
2268 if (ch_peer == 0 && ch_self == 0) {
2269 rv = test_bit(DISCARD_CONCURRENT, &mdev->flags)
2270 ? -1 : 1;
2271 break;
2272 } else {
2273 if (ch_peer == 0) { rv = 1; break; }
2274 if (ch_self == 0) { rv = -1; break; }
2275 }
2276 if (mdev->net_conf->after_sb_0p == ASB_DISCARD_ZERO_CHG)
2277 break;
2278 case ASB_DISCARD_LEAST_CHG:
2279 if (ch_self < ch_peer)
2280 rv = -1;
2281 else if (ch_self > ch_peer)
2282 rv = 1;
2283 else /* ( ch_self == ch_peer ) */
2284 /* Well, then use something else. */
2285 rv = test_bit(DISCARD_CONCURRENT, &mdev->flags)
2286 ? -1 : 1;
2287 break;
2288 case ASB_DISCARD_LOCAL:
2289 rv = -1;
2290 break;
2291 case ASB_DISCARD_REMOTE:
2292 rv = 1;
2293 }
2294
2295 return rv;
2296}
2297
2298static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
2299{
2300 int self, peer, hg, rv = -100;
2301
2302 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2303 peer = mdev->p_uuid[UI_BITMAP] & 1;
2304
2305 switch (mdev->net_conf->after_sb_1p) {
2306 case ASB_DISCARD_YOUNGER_PRI:
2307 case ASB_DISCARD_OLDER_PRI:
2308 case ASB_DISCARD_LEAST_CHG:
2309 case ASB_DISCARD_LOCAL:
2310 case ASB_DISCARD_REMOTE:
2311 dev_err(DEV, "Configuration error.\n");
2312 break;
2313 case ASB_DISCONNECT:
2314 break;
2315 case ASB_CONSENSUS:
2316 hg = drbd_asb_recover_0p(mdev);
2317 if (hg == -1 && mdev->state.role == R_SECONDARY)
2318 rv = hg;
2319 if (hg == 1 && mdev->state.role == R_PRIMARY)
2320 rv = hg;
2321 break;
2322 case ASB_VIOLENTLY:
2323 rv = drbd_asb_recover_0p(mdev);
2324 break;
2325 case ASB_DISCARD_SECONDARY:
2326 return mdev->state.role == R_PRIMARY ? 1 : -1;
2327 case ASB_CALL_HELPER:
2328 hg = drbd_asb_recover_0p(mdev);
2329 if (hg == -1 && mdev->state.role == R_PRIMARY) {
2330 self = drbd_set_role(mdev, R_SECONDARY, 0);
2331 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2332 * we might be here in C_WF_REPORT_PARAMS which is transient.
2333 * we do not need to wait for the after state change work either. */
2334 self = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2335 if (self != SS_SUCCESS) {
2336 drbd_khelper(mdev, "pri-lost-after-sb");
2337 } else {
2338 dev_warn(DEV, "Successfully gave up primary role.\n");
2339 rv = hg;
2340 }
2341 } else
2342 rv = hg;
2343 }
2344
2345 return rv;
2346}
2347
2348static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
2349{
2350 int self, peer, hg, rv = -100;
2351
2352 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2353 peer = mdev->p_uuid[UI_BITMAP] & 1;
2354
2355 switch (mdev->net_conf->after_sb_2p) {
2356 case ASB_DISCARD_YOUNGER_PRI:
2357 case ASB_DISCARD_OLDER_PRI:
2358 case ASB_DISCARD_LEAST_CHG:
2359 case ASB_DISCARD_LOCAL:
2360 case ASB_DISCARD_REMOTE:
2361 case ASB_CONSENSUS:
2362 case ASB_DISCARD_SECONDARY:
2363 dev_err(DEV, "Configuration error.\n");
2364 break;
2365 case ASB_VIOLENTLY:
2366 rv = drbd_asb_recover_0p(mdev);
2367 break;
2368 case ASB_DISCONNECT:
2369 break;
2370 case ASB_CALL_HELPER:
2371 hg = drbd_asb_recover_0p(mdev);
2372 if (hg == -1) {
2373 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2374 * we might be here in C_WF_REPORT_PARAMS which is transient.
2375 * we do not need to wait for the after state change work either. */
2376 self = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2377 if (self != SS_SUCCESS) {
2378 drbd_khelper(mdev, "pri-lost-after-sb");
2379 } else {
2380 dev_warn(DEV, "Successfully gave up primary role.\n");
2381 rv = hg;
2382 }
2383 } else
2384 rv = hg;
2385 }
2386
2387 return rv;
2388}
2389
2390static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
2391 u64 bits, u64 flags)
2392{
2393 if (!uuid) {
2394 dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
2395 return;
2396 }
2397 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
2398 text,
2399 (unsigned long long)uuid[UI_CURRENT],
2400 (unsigned long long)uuid[UI_BITMAP],
2401 (unsigned long long)uuid[UI_HISTORY_START],
2402 (unsigned long long)uuid[UI_HISTORY_END],
2403 (unsigned long long)bits,
2404 (unsigned long long)flags);
2405}
2406
2407/*
2408 100 after split brain try auto recover
2409 2 C_SYNC_SOURCE set BitMap
2410 1 C_SYNC_SOURCE use BitMap
2411 0 no Sync
2412 -1 C_SYNC_TARGET use BitMap
2413 -2 C_SYNC_TARGET set BitMap
2414 -100 after split brain, disconnect
2415-1000 unrelated data
2416 */
2417static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
2418{
2419 u64 self, peer;
2420 int i, j;
2421
2422 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2423 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2424
2425 *rule_nr = 10;
2426 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
2427 return 0;
2428
2429 *rule_nr = 20;
2430 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
2431 peer != UUID_JUST_CREATED)
2432 return -2;
2433
2434 *rule_nr = 30;
2435 if (self != UUID_JUST_CREATED &&
2436 (peer == UUID_JUST_CREATED || peer == (u64)0))
2437 return 2;
2438
2439 if (self == peer) {
2440 int rct, dc; /* roles at crash time */
2441
2442 if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
2443
2444 if (mdev->agreed_pro_version < 91)
2445 return -1001;
2446
2447 if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
2448 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
2449 dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
2450 drbd_uuid_set_bm(mdev, 0UL);
2451
2452 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2453 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2454 *rule_nr = 34;
2455 } else {
2456 dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
2457 *rule_nr = 36;
2458 }
2459
2460 return 1;
2461 }
2462
2463 if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
2464
2465 if (mdev->agreed_pro_version < 91)
2466 return -1001;
2467
2468 if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
2469 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
2470 dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
2471
2472 mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
2473 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
2474 mdev->p_uuid[UI_BITMAP] = 0UL;
2475
2476 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2477 *rule_nr = 35;
2478 } else {
2479 dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
2480 *rule_nr = 37;
2481 }
2482
2483 return -1;
2484 }
2485
2486 /* Common power [off|failure] */
2487 rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
2488 (mdev->p_uuid[UI_FLAGS] & 2);
2489 /* lowest bit is set when we were primary,
2490 * next bit (weight 2) is set when peer was primary */
2491 *rule_nr = 40;
2492
2493 switch (rct) {
2494 case 0: /* !self_pri && !peer_pri */ return 0;
2495 case 1: /* self_pri && !peer_pri */ return 1;
2496 case 2: /* !self_pri && peer_pri */ return -1;
2497 case 3: /* self_pri && peer_pri */
2498 dc = test_bit(DISCARD_CONCURRENT, &mdev->flags);
2499 return dc ? -1 : 1;
2500 }
2501 }
2502
2503 *rule_nr = 50;
2504 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2505 if (self == peer)
2506 return -1;
2507
2508 *rule_nr = 51;
2509 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2510 if (self == peer) {
2511 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2512 peer = mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1);
2513 if (self == peer) {
2514 /* The last P_SYNC_UUID did not get though. Undo the last start of
2515 resync as sync source modifications of the peer's UUIDs. */
2516
2517 if (mdev->agreed_pro_version < 91)
2518 return -1001;
2519
2520 mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
2521 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
2522 return -1;
2523 }
2524 }
2525
2526 *rule_nr = 60;
2527 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2528 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2529 peer = mdev->p_uuid[i] & ~((u64)1);
2530 if (self == peer)
2531 return -2;
2532 }
2533
2534 *rule_nr = 70;
2535 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2536 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2537 if (self == peer)
2538 return 1;
2539
2540 *rule_nr = 71;
2541 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2542 if (self == peer) {
2543 self = mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1);
2544 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2545 if (self == peer) {
2546 /* The last P_SYNC_UUID did not get though. Undo the last start of
2547 resync as sync source modifications of our UUIDs. */
2548
2549 if (mdev->agreed_pro_version < 91)
2550 return -1001;
2551
2552 _drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
2553 _drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
2554
2555 dev_info(DEV, "Undid last start of resync:\n");
2556
2557 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2558 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2559
2560 return 1;
2561 }
2562 }
2563
2564
2565 *rule_nr = 80;
Philipp Reisnerd8c2a362009-11-18 15:52:51 +01002566 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002567 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2568 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2569 if (self == peer)
2570 return 2;
2571 }
2572
2573 *rule_nr = 90;
2574 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2575 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2576 if (self == peer && self != ((u64)0))
2577 return 100;
2578
2579 *rule_nr = 100;
2580 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2581 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2582 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
2583 peer = mdev->p_uuid[j] & ~((u64)1);
2584 if (self == peer)
2585 return -100;
2586 }
2587 }
2588
2589 return -1000;
2590}
2591
2592/* drbd_sync_handshake() returns the new conn state on success, or
2593 CONN_MASK (-1) on failure.
2594 */
2595static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
2596 enum drbd_disk_state peer_disk) __must_hold(local)
2597{
2598 int hg, rule_nr;
2599 enum drbd_conns rv = C_MASK;
2600 enum drbd_disk_state mydisk;
2601
2602 mydisk = mdev->state.disk;
2603 if (mydisk == D_NEGOTIATING)
2604 mydisk = mdev->new_state_tmp.disk;
2605
2606 dev_info(DEV, "drbd_sync_handshake:\n");
2607 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0);
2608 drbd_uuid_dump(mdev, "peer", mdev->p_uuid,
2609 mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2610
2611 hg = drbd_uuid_compare(mdev, &rule_nr);
2612
2613 dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
2614
2615 if (hg == -1000) {
2616 dev_alert(DEV, "Unrelated data, aborting!\n");
2617 return C_MASK;
2618 }
2619 if (hg == -1001) {
2620 dev_alert(DEV, "To resolve this both sides have to support at least protocol\n");
2621 return C_MASK;
2622 }
2623
2624 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
2625 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
2626 int f = (hg == -100) || abs(hg) == 2;
2627 hg = mydisk > D_INCONSISTENT ? 1 : -1;
2628 if (f)
2629 hg = hg*2;
2630 dev_info(DEV, "Becoming sync %s due to disk states.\n",
2631 hg > 0 ? "source" : "target");
2632 }
2633
Adam Gandelman3a11a482010-04-08 16:48:23 -07002634 if (abs(hg) == 100)
2635 drbd_khelper(mdev, "initial-split-brain");
2636
Philipp Reisnerb411b362009-09-25 16:07:19 -07002637 if (hg == 100 || (hg == -100 && mdev->net_conf->always_asbp)) {
2638 int pcount = (mdev->state.role == R_PRIMARY)
2639 + (peer_role == R_PRIMARY);
2640 int forced = (hg == -100);
2641
2642 switch (pcount) {
2643 case 0:
2644 hg = drbd_asb_recover_0p(mdev);
2645 break;
2646 case 1:
2647 hg = drbd_asb_recover_1p(mdev);
2648 break;
2649 case 2:
2650 hg = drbd_asb_recover_2p(mdev);
2651 break;
2652 }
2653 if (abs(hg) < 100) {
2654 dev_warn(DEV, "Split-Brain detected, %d primaries, "
2655 "automatically solved. Sync from %s node\n",
2656 pcount, (hg < 0) ? "peer" : "this");
2657 if (forced) {
2658 dev_warn(DEV, "Doing a full sync, since"
2659 " UUIDs where ambiguous.\n");
2660 hg = hg*2;
2661 }
2662 }
2663 }
2664
2665 if (hg == -100) {
2666 if (mdev->net_conf->want_lose && !(mdev->p_uuid[UI_FLAGS]&1))
2667 hg = -1;
2668 if (!mdev->net_conf->want_lose && (mdev->p_uuid[UI_FLAGS]&1))
2669 hg = 1;
2670
2671 if (abs(hg) < 100)
2672 dev_warn(DEV, "Split-Brain detected, manually solved. "
2673 "Sync from %s node\n",
2674 (hg < 0) ? "peer" : "this");
2675 }
2676
2677 if (hg == -100) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01002678 /* FIXME this log message is not correct if we end up here
2679 * after an attempted attach on a diskless node.
2680 * We just refuse to attach -- well, we drop the "connection"
2681 * to that disk, in a way... */
Adam Gandelman3a11a482010-04-08 16:48:23 -07002682 dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002683 drbd_khelper(mdev, "split-brain");
2684 return C_MASK;
2685 }
2686
2687 if (hg > 0 && mydisk <= D_INCONSISTENT) {
2688 dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
2689 return C_MASK;
2690 }
2691
2692 if (hg < 0 && /* by intention we do not use mydisk here. */
2693 mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
2694 switch (mdev->net_conf->rr_conflict) {
2695 case ASB_CALL_HELPER:
2696 drbd_khelper(mdev, "pri-lost");
2697 /* fall through */
2698 case ASB_DISCONNECT:
2699 dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
2700 return C_MASK;
2701 case ASB_VIOLENTLY:
2702 dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
2703 "assumption\n");
2704 }
2705 }
2706
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002707 if (mdev->net_conf->dry_run || test_bit(CONN_DRY_RUN, &mdev->flags)) {
2708 if (hg == 0)
2709 dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
2710 else
2711 dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
2712 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
2713 abs(hg) >= 2 ? "full" : "bit-map based");
2714 return C_MASK;
2715 }
2716
Philipp Reisnerb411b362009-09-25 16:07:19 -07002717 if (abs(hg) >= 2) {
2718 dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
2719 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake"))
2720 return C_MASK;
2721 }
2722
2723 if (hg > 0) { /* become sync source. */
2724 rv = C_WF_BITMAP_S;
2725 } else if (hg < 0) { /* become sync target */
2726 rv = C_WF_BITMAP_T;
2727 } else {
2728 rv = C_CONNECTED;
2729 if (drbd_bm_total_weight(mdev)) {
2730 dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
2731 drbd_bm_total_weight(mdev));
2732 }
2733 }
2734
2735 return rv;
2736}
2737
2738/* returns 1 if invalid */
2739static int cmp_after_sb(enum drbd_after_sb_p peer, enum drbd_after_sb_p self)
2740{
2741 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
2742 if ((peer == ASB_DISCARD_REMOTE && self == ASB_DISCARD_LOCAL) ||
2743 (self == ASB_DISCARD_REMOTE && peer == ASB_DISCARD_LOCAL))
2744 return 0;
2745
2746 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
2747 if (peer == ASB_DISCARD_REMOTE || peer == ASB_DISCARD_LOCAL ||
2748 self == ASB_DISCARD_REMOTE || self == ASB_DISCARD_LOCAL)
2749 return 1;
2750
2751 /* everything else is valid if they are equal on both sides. */
2752 if (peer == self)
2753 return 0;
2754
2755 /* everything es is invalid. */
2756 return 1;
2757}
2758
Philipp Reisner0b70a132010-08-20 13:36:10 +02002759static int receive_protocol(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002760{
2761 struct p_protocol *p = (struct p_protocol *)h;
2762 int header_size, data_size;
2763 int p_proto, p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002764 int p_want_lose, p_two_primaries, cf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002765 char p_integrity_alg[SHARED_SECRET_MAX] = "";
2766
2767 header_size = sizeof(*p) - sizeof(*h);
2768 data_size = h->length - header_size;
2769
2770 if (drbd_recv(mdev, h->payload, header_size) != header_size)
2771 return FALSE;
2772
2773 p_proto = be32_to_cpu(p->protocol);
2774 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
2775 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
2776 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002777 p_two_primaries = be32_to_cpu(p->two_primaries);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002778 cf = be32_to_cpu(p->conn_flags);
2779 p_want_lose = cf & CF_WANT_LOSE;
2780
2781 clear_bit(CONN_DRY_RUN, &mdev->flags);
2782
2783 if (cf & CF_DRY_RUN)
2784 set_bit(CONN_DRY_RUN, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002785
2786 if (p_proto != mdev->net_conf->wire_protocol) {
2787 dev_err(DEV, "incompatible communication protocols\n");
2788 goto disconnect;
2789 }
2790
2791 if (cmp_after_sb(p_after_sb_0p, mdev->net_conf->after_sb_0p)) {
2792 dev_err(DEV, "incompatible after-sb-0pri settings\n");
2793 goto disconnect;
2794 }
2795
2796 if (cmp_after_sb(p_after_sb_1p, mdev->net_conf->after_sb_1p)) {
2797 dev_err(DEV, "incompatible after-sb-1pri settings\n");
2798 goto disconnect;
2799 }
2800
2801 if (cmp_after_sb(p_after_sb_2p, mdev->net_conf->after_sb_2p)) {
2802 dev_err(DEV, "incompatible after-sb-2pri settings\n");
2803 goto disconnect;
2804 }
2805
2806 if (p_want_lose && mdev->net_conf->want_lose) {
2807 dev_err(DEV, "both sides have the 'want_lose' flag set\n");
2808 goto disconnect;
2809 }
2810
2811 if (p_two_primaries != mdev->net_conf->two_primaries) {
2812 dev_err(DEV, "incompatible setting of the two-primaries options\n");
2813 goto disconnect;
2814 }
2815
2816 if (mdev->agreed_pro_version >= 87) {
2817 unsigned char *my_alg = mdev->net_conf->integrity_alg;
2818
2819 if (drbd_recv(mdev, p_integrity_alg, data_size) != data_size)
2820 return FALSE;
2821
2822 p_integrity_alg[SHARED_SECRET_MAX-1] = 0;
2823 if (strcmp(p_integrity_alg, my_alg)) {
2824 dev_err(DEV, "incompatible setting of the data-integrity-alg\n");
2825 goto disconnect;
2826 }
2827 dev_info(DEV, "data-integrity-alg: %s\n",
2828 my_alg[0] ? my_alg : (unsigned char *)"<not-used>");
2829 }
2830
2831 return TRUE;
2832
2833disconnect:
2834 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
2835 return FALSE;
2836}
2837
2838/* helper function
2839 * input: alg name, feature name
2840 * return: NULL (alg name was "")
2841 * ERR_PTR(error) if something goes wrong
2842 * or the crypto hash ptr, if it worked out ok. */
2843struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
2844 const char *alg, const char *name)
2845{
2846 struct crypto_hash *tfm;
2847
2848 if (!alg[0])
2849 return NULL;
2850
2851 tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
2852 if (IS_ERR(tfm)) {
2853 dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
2854 alg, name, PTR_ERR(tfm));
2855 return tfm;
2856 }
2857 if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) {
2858 crypto_free_hash(tfm);
2859 dev_err(DEV, "\"%s\" is not a digest (%s)\n", alg, name);
2860 return ERR_PTR(-EINVAL);
2861 }
2862 return tfm;
2863}
2864
Philipp Reisner0b70a132010-08-20 13:36:10 +02002865static int receive_SyncParam(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002866{
2867 int ok = TRUE;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002868 struct p_rs_param_95 *p = (struct p_rs_param_95 *)h;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002869 unsigned int header_size, data_size, exp_max_sz;
2870 struct crypto_hash *verify_tfm = NULL;
2871 struct crypto_hash *csums_tfm = NULL;
2872 const int apv = mdev->agreed_pro_version;
Philipp Reisner778f2712010-07-06 11:14:00 +02002873 int *rs_plan_s = NULL;
2874 int fifo_size = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002875
2876 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
2877 : apv == 88 ? sizeof(struct p_rs_param)
2878 + SHARED_SECRET_MAX
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002879 : apv <= 94 ? sizeof(struct p_rs_param_89)
2880 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002881
2882 if (h->length > exp_max_sz) {
2883 dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
2884 h->length, exp_max_sz);
2885 return FALSE;
2886 }
2887
2888 if (apv <= 88) {
2889 header_size = sizeof(struct p_rs_param) - sizeof(*h);
2890 data_size = h->length - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002891 } else if (apv <= 94) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002892 header_size = sizeof(struct p_rs_param_89) - sizeof(*h);
2893 data_size = h->length - header_size;
2894 D_ASSERT(data_size == 0);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002895 } else {
2896 header_size = sizeof(struct p_rs_param_95) - sizeof(*h);
2897 data_size = h->length - header_size;
2898 D_ASSERT(data_size == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002899 }
2900
2901 /* initialize verify_alg and csums_alg */
2902 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
2903
2904 if (drbd_recv(mdev, h->payload, header_size) != header_size)
2905 return FALSE;
2906
2907 mdev->sync_conf.rate = be32_to_cpu(p->rate);
2908
2909 if (apv >= 88) {
2910 if (apv == 88) {
2911 if (data_size > SHARED_SECRET_MAX) {
2912 dev_err(DEV, "verify-alg too long, "
2913 "peer wants %u, accepting only %u byte\n",
2914 data_size, SHARED_SECRET_MAX);
2915 return FALSE;
2916 }
2917
2918 if (drbd_recv(mdev, p->verify_alg, data_size) != data_size)
2919 return FALSE;
2920
2921 /* we expect NUL terminated string */
2922 /* but just in case someone tries to be evil */
2923 D_ASSERT(p->verify_alg[data_size-1] == 0);
2924 p->verify_alg[data_size-1] = 0;
2925
2926 } else /* apv >= 89 */ {
2927 /* we still expect NUL terminated strings */
2928 /* but just in case someone tries to be evil */
2929 D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
2930 D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
2931 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
2932 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
2933 }
2934
2935 if (strcmp(mdev->sync_conf.verify_alg, p->verify_alg)) {
2936 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
2937 dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
2938 mdev->sync_conf.verify_alg, p->verify_alg);
2939 goto disconnect;
2940 }
2941 verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
2942 p->verify_alg, "verify-alg");
2943 if (IS_ERR(verify_tfm)) {
2944 verify_tfm = NULL;
2945 goto disconnect;
2946 }
2947 }
2948
2949 if (apv >= 89 && strcmp(mdev->sync_conf.csums_alg, p->csums_alg)) {
2950 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
2951 dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
2952 mdev->sync_conf.csums_alg, p->csums_alg);
2953 goto disconnect;
2954 }
2955 csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
2956 p->csums_alg, "csums-alg");
2957 if (IS_ERR(csums_tfm)) {
2958 csums_tfm = NULL;
2959 goto disconnect;
2960 }
2961 }
2962
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002963 if (apv > 94) {
2964 mdev->sync_conf.rate = be32_to_cpu(p->rate);
2965 mdev->sync_conf.c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
2966 mdev->sync_conf.c_delay_target = be32_to_cpu(p->c_delay_target);
2967 mdev->sync_conf.c_fill_target = be32_to_cpu(p->c_fill_target);
2968 mdev->sync_conf.c_max_rate = be32_to_cpu(p->c_max_rate);
Philipp Reisner778f2712010-07-06 11:14:00 +02002969
2970 fifo_size = (mdev->sync_conf.c_plan_ahead * 10 * SLEEP_TIME) / HZ;
2971 if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) {
2972 rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL);
2973 if (!rs_plan_s) {
2974 dev_err(DEV, "kmalloc of fifo_buffer failed");
2975 goto disconnect;
2976 }
2977 }
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002978 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002979
2980 spin_lock(&mdev->peer_seq_lock);
2981 /* lock against drbd_nl_syncer_conf() */
2982 if (verify_tfm) {
2983 strcpy(mdev->sync_conf.verify_alg, p->verify_alg);
2984 mdev->sync_conf.verify_alg_len = strlen(p->verify_alg) + 1;
2985 crypto_free_hash(mdev->verify_tfm);
2986 mdev->verify_tfm = verify_tfm;
2987 dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
2988 }
2989 if (csums_tfm) {
2990 strcpy(mdev->sync_conf.csums_alg, p->csums_alg);
2991 mdev->sync_conf.csums_alg_len = strlen(p->csums_alg) + 1;
2992 crypto_free_hash(mdev->csums_tfm);
2993 mdev->csums_tfm = csums_tfm;
2994 dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
2995 }
Philipp Reisner778f2712010-07-06 11:14:00 +02002996 if (fifo_size != mdev->rs_plan_s.size) {
2997 kfree(mdev->rs_plan_s.values);
2998 mdev->rs_plan_s.values = rs_plan_s;
2999 mdev->rs_plan_s.size = fifo_size;
3000 mdev->rs_planed = 0;
3001 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003002 spin_unlock(&mdev->peer_seq_lock);
3003 }
3004
3005 return ok;
3006disconnect:
3007 /* just for completeness: actually not needed,
3008 * as this is not reached if csums_tfm was ok. */
3009 crypto_free_hash(csums_tfm);
3010 /* but free the verify_tfm again, if csums_tfm did not work out */
3011 crypto_free_hash(verify_tfm);
3012 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
3013 return FALSE;
3014}
3015
3016static void drbd_setup_order_type(struct drbd_conf *mdev, int peer)
3017{
3018 /* sorry, we currently have no working implementation
3019 * of distributed TCQ */
3020}
3021
3022/* warn if the arguments differ by more than 12.5% */
3023static void warn_if_differ_considerably(struct drbd_conf *mdev,
3024 const char *s, sector_t a, sector_t b)
3025{
3026 sector_t d;
3027 if (a == 0 || b == 0)
3028 return;
3029 d = (a > b) ? (a - b) : (b - a);
3030 if (d > (a>>3) || d > (b>>3))
3031 dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
3032 (unsigned long long)a, (unsigned long long)b);
3033}
3034
Philipp Reisner0b70a132010-08-20 13:36:10 +02003035static int receive_sizes(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003036{
3037 struct p_sizes *p = (struct p_sizes *)h;
3038 enum determine_dev_size dd = unchanged;
3039 unsigned int max_seg_s;
3040 sector_t p_size, p_usize, my_usize;
3041 int ldsc = 0; /* local disk size changed */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003042 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003043
3044 ERR_IF(h->length != (sizeof(*p)-sizeof(*h))) return FALSE;
3045 if (drbd_recv(mdev, h->payload, h->length) != h->length)
3046 return FALSE;
3047
3048 p_size = be64_to_cpu(p->d_size);
3049 p_usize = be64_to_cpu(p->u_size);
3050
3051 if (p_size == 0 && mdev->state.disk == D_DISKLESS) {
3052 dev_err(DEV, "some backing storage is needed\n");
3053 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
3054 return FALSE;
3055 }
3056
3057 /* just store the peer's disk size for now.
3058 * we still need to figure out whether we accept that. */
3059 mdev->p_size = p_size;
3060
3061#define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
3062 if (get_ldev(mdev)) {
3063 warn_if_differ_considerably(mdev, "lower level device sizes",
3064 p_size, drbd_get_max_capacity(mdev->ldev));
3065 warn_if_differ_considerably(mdev, "user requested size",
3066 p_usize, mdev->ldev->dc.disk_size);
3067
3068 /* if this is the first connect, or an otherwise expected
3069 * param exchange, choose the minimum */
3070 if (mdev->state.conn == C_WF_REPORT_PARAMS)
3071 p_usize = min_not_zero((sector_t)mdev->ldev->dc.disk_size,
3072 p_usize);
3073
3074 my_usize = mdev->ldev->dc.disk_size;
3075
3076 if (mdev->ldev->dc.disk_size != p_usize) {
3077 mdev->ldev->dc.disk_size = p_usize;
3078 dev_info(DEV, "Peer sets u_size to %lu sectors\n",
3079 (unsigned long)mdev->ldev->dc.disk_size);
3080 }
3081
3082 /* Never shrink a device with usable data during connect.
3083 But allow online shrinking if we are connected. */
Philipp Reisnera393db62009-12-22 13:35:52 +01003084 if (drbd_new_dev_size(mdev, mdev->ldev, 0) <
Philipp Reisnerb411b362009-09-25 16:07:19 -07003085 drbd_get_capacity(mdev->this_bdev) &&
3086 mdev->state.disk >= D_OUTDATED &&
3087 mdev->state.conn < C_CONNECTED) {
3088 dev_err(DEV, "The peer's disk size is too small!\n");
3089 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
3090 mdev->ldev->dc.disk_size = my_usize;
3091 put_ldev(mdev);
3092 return FALSE;
3093 }
3094 put_ldev(mdev);
3095 }
3096#undef min_not_zero
3097
Philipp Reisnere89b5912010-03-24 17:11:33 +01003098 ddsf = be16_to_cpu(p->dds_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003099 if (get_ldev(mdev)) {
Philipp Reisnere89b5912010-03-24 17:11:33 +01003100 dd = drbd_determin_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003101 put_ldev(mdev);
3102 if (dd == dev_size_error)
3103 return FALSE;
3104 drbd_md_sync(mdev);
3105 } else {
3106 /* I am diskless, need to accept the peer's size. */
3107 drbd_set_my_capacity(mdev, p_size);
3108 }
3109
Philipp Reisnerb411b362009-09-25 16:07:19 -07003110 if (get_ldev(mdev)) {
3111 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
3112 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
3113 ldsc = 1;
3114 }
3115
Lars Ellenberga1c88d02010-05-14 19:16:41 +02003116 if (mdev->agreed_pro_version < 94)
3117 max_seg_s = be32_to_cpu(p->max_segment_size);
3118 else /* drbd 8.3.8 onwards */
3119 max_seg_s = DRBD_MAX_SEGMENT_SIZE;
3120
Philipp Reisnerb411b362009-09-25 16:07:19 -07003121 if (max_seg_s != queue_max_segment_size(mdev->rq_queue))
3122 drbd_setup_queue_param(mdev, max_seg_s);
3123
Philipp Reisnere89b5912010-03-24 17:11:33 +01003124 drbd_setup_order_type(mdev, be16_to_cpu(p->queue_order_type));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003125 put_ldev(mdev);
3126 }
3127
3128 if (mdev->state.conn > C_WF_REPORT_PARAMS) {
3129 if (be64_to_cpu(p->c_size) !=
3130 drbd_get_capacity(mdev->this_bdev) || ldsc) {
3131 /* we have different sizes, probably peer
3132 * needs to know my new size... */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003133 drbd_send_sizes(mdev, 0, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003134 }
3135 if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
3136 (dd == grew && mdev->state.conn == C_CONNECTED)) {
3137 if (mdev->state.pdsk >= D_INCONSISTENT &&
Philipp Reisnere89b5912010-03-24 17:11:33 +01003138 mdev->state.disk >= D_INCONSISTENT) {
3139 if (ddsf & DDSF_NO_RESYNC)
3140 dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
3141 else
3142 resync_after_online_grow(mdev);
3143 } else
Philipp Reisnerb411b362009-09-25 16:07:19 -07003144 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
3145 }
3146 }
3147
3148 return TRUE;
3149}
3150
Philipp Reisner0b70a132010-08-20 13:36:10 +02003151static int receive_uuids(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003152{
3153 struct p_uuids *p = (struct p_uuids *)h;
3154 u64 *p_uuid;
3155 int i;
3156
3157 ERR_IF(h->length != (sizeof(*p)-sizeof(*h))) return FALSE;
3158 if (drbd_recv(mdev, h->payload, h->length) != h->length)
3159 return FALSE;
3160
3161 p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
3162
3163 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
3164 p_uuid[i] = be64_to_cpu(p->uuid[i]);
3165
3166 kfree(mdev->p_uuid);
3167 mdev->p_uuid = p_uuid;
3168
3169 if (mdev->state.conn < C_CONNECTED &&
3170 mdev->state.disk < D_INCONSISTENT &&
3171 mdev->state.role == R_PRIMARY &&
3172 (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
3173 dev_err(DEV, "Can only connect to data with current UUID=%016llX\n",
3174 (unsigned long long)mdev->ed_uuid);
3175 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
3176 return FALSE;
3177 }
3178
3179 if (get_ldev(mdev)) {
3180 int skip_initial_sync =
3181 mdev->state.conn == C_CONNECTED &&
3182 mdev->agreed_pro_version >= 90 &&
3183 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
3184 (p_uuid[UI_FLAGS] & 8);
3185 if (skip_initial_sync) {
3186 dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n");
3187 drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
3188 "clear_n_write from receive_uuids");
3189 _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]);
3190 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3191 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3192 CS_VERBOSE, NULL);
3193 drbd_md_sync(mdev);
3194 }
3195 put_ldev(mdev);
Philipp Reisner18a50fa2010-06-21 14:14:15 +02003196 } else if (mdev->state.disk < D_INCONSISTENT &&
3197 mdev->state.role == R_PRIMARY) {
3198 /* I am a diskless primary, the peer just created a new current UUID
3199 for me. */
3200 drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003201 }
3202
3203 /* Before we test for the disk state, we should wait until an eventually
3204 ongoing cluster wide state change is finished. That is important if
3205 we are primary and are detaching from our disk. We need to see the
3206 new disk state... */
3207 wait_event(mdev->misc_wait, !test_bit(CLUSTER_ST_CHANGE, &mdev->flags));
3208 if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT)
3209 drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
3210
3211 return TRUE;
3212}
3213
3214/**
3215 * convert_state() - Converts the peer's view of the cluster state to our point of view
3216 * @ps: The state as seen by the peer.
3217 */
3218static union drbd_state convert_state(union drbd_state ps)
3219{
3220 union drbd_state ms;
3221
3222 static enum drbd_conns c_tab[] = {
3223 [C_CONNECTED] = C_CONNECTED,
3224
3225 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
3226 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
3227 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
3228 [C_VERIFY_S] = C_VERIFY_T,
3229 [C_MASK] = C_MASK,
3230 };
3231
3232 ms.i = ps.i;
3233
3234 ms.conn = c_tab[ps.conn];
3235 ms.peer = ps.role;
3236 ms.role = ps.peer;
3237 ms.pdsk = ps.disk;
3238 ms.disk = ps.pdsk;
3239 ms.peer_isp = (ps.aftr_isp | ps.user_isp);
3240
3241 return ms;
3242}
3243
Philipp Reisner0b70a132010-08-20 13:36:10 +02003244static int receive_req_state(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003245{
3246 struct p_req_state *p = (struct p_req_state *)h;
3247 union drbd_state mask, val;
3248 int rv;
3249
3250 ERR_IF(h->length != (sizeof(*p)-sizeof(*h))) return FALSE;
3251 if (drbd_recv(mdev, h->payload, h->length) != h->length)
3252 return FALSE;
3253
3254 mask.i = be32_to_cpu(p->mask);
3255 val.i = be32_to_cpu(p->val);
3256
3257 if (test_bit(DISCARD_CONCURRENT, &mdev->flags) &&
3258 test_bit(CLUSTER_ST_CHANGE, &mdev->flags)) {
3259 drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG);
3260 return TRUE;
3261 }
3262
3263 mask = convert_state(mask);
3264 val = convert_state(val);
3265
3266 rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
3267
3268 drbd_send_sr_reply(mdev, rv);
3269 drbd_md_sync(mdev);
3270
3271 return TRUE;
3272}
3273
Philipp Reisner0b70a132010-08-20 13:36:10 +02003274static int receive_state(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003275{
3276 struct p_state *p = (struct p_state *)h;
3277 enum drbd_conns nconn, oconn;
3278 union drbd_state ns, peer_state;
3279 enum drbd_disk_state real_peer_disk;
Philipp Reisner65d922c2010-06-16 16:18:09 +02003280 enum chg_state_flags cs_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003281 int rv;
3282
3283 ERR_IF(h->length != (sizeof(*p)-sizeof(*h)))
3284 return FALSE;
3285
3286 if (drbd_recv(mdev, h->payload, h->length) != h->length)
3287 return FALSE;
3288
3289 peer_state.i = be32_to_cpu(p->state);
3290
3291 real_peer_disk = peer_state.disk;
3292 if (peer_state.disk == D_NEGOTIATING) {
3293 real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
3294 dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
3295 }
3296
3297 spin_lock_irq(&mdev->req_lock);
3298 retry:
3299 oconn = nconn = mdev->state.conn;
3300 spin_unlock_irq(&mdev->req_lock);
3301
3302 if (nconn == C_WF_REPORT_PARAMS)
3303 nconn = C_CONNECTED;
3304
3305 if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING &&
3306 get_ldev_if_state(mdev, D_NEGOTIATING)) {
3307 int cr; /* consider resync */
3308
3309 /* if we established a new connection */
3310 cr = (oconn < C_CONNECTED);
3311 /* if we had an established connection
3312 * and one of the nodes newly attaches a disk */
3313 cr |= (oconn == C_CONNECTED &&
3314 (peer_state.disk == D_NEGOTIATING ||
3315 mdev->state.disk == D_NEGOTIATING));
3316 /* if we have both been inconsistent, and the peer has been
3317 * forced to be UpToDate with --overwrite-data */
3318 cr |= test_bit(CONSIDER_RESYNC, &mdev->flags);
3319 /* if we had been plain connected, and the admin requested to
3320 * start a sync by "invalidate" or "invalidate-remote" */
3321 cr |= (oconn == C_CONNECTED &&
3322 (peer_state.conn >= C_STARTING_SYNC_S &&
3323 peer_state.conn <= C_WF_BITMAP_T));
3324
3325 if (cr)
3326 nconn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk);
3327
3328 put_ldev(mdev);
3329 if (nconn == C_MASK) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01003330 nconn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003331 if (mdev->state.disk == D_NEGOTIATING) {
3332 drbd_force_state(mdev, NS(disk, D_DISKLESS));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003333 } else if (peer_state.disk == D_NEGOTIATING) {
3334 dev_err(DEV, "Disk attach process on the peer node was aborted.\n");
3335 peer_state.disk = D_DISKLESS;
Lars Ellenberg580b9762010-02-26 23:15:23 +01003336 real_peer_disk = D_DISKLESS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003337 } else {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003338 if (test_and_clear_bit(CONN_DRY_RUN, &mdev->flags))
3339 return FALSE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003340 D_ASSERT(oconn == C_WF_REPORT_PARAMS);
3341 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
3342 return FALSE;
3343 }
3344 }
3345 }
3346
3347 spin_lock_irq(&mdev->req_lock);
3348 if (mdev->state.conn != oconn)
3349 goto retry;
3350 clear_bit(CONSIDER_RESYNC, &mdev->flags);
3351 ns.i = mdev->state.i;
3352 ns.conn = nconn;
3353 ns.peer = peer_state.role;
3354 ns.pdsk = real_peer_disk;
3355 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
3356 if ((nconn == C_CONNECTED || nconn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
3357 ns.disk = mdev->new_state_tmp.disk;
Philipp Reisner65d922c2010-06-16 16:18:09 +02003358 cs_flags = CS_VERBOSE + (oconn < C_CONNECTED && nconn >= C_CONNECTED ? 0 : CS_HARD);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003359 if (ns.pdsk == D_CONSISTENT && ns.susp && nconn == C_CONNECTED && oconn < C_CONNECTED &&
3360 test_bit(NEW_CUR_UUID, &mdev->flags)) {
3361 /* Do not allow tl_restart(resend) for a rebooted peer. We can only allow this
3362 for temporal network outages! */
3363 spin_unlock_irq(&mdev->req_lock);
3364 dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
3365 tl_clear(mdev);
3366 drbd_uuid_new_current(mdev);
3367 clear_bit(NEW_CUR_UUID, &mdev->flags);
3368 drbd_force_state(mdev, NS2(conn, C_PROTOCOL_ERROR, susp, 0));
3369 return FALSE;
3370 }
Philipp Reisner65d922c2010-06-16 16:18:09 +02003371 rv = _drbd_set_state(mdev, ns, cs_flags, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003372 ns = mdev->state;
3373 spin_unlock_irq(&mdev->req_lock);
3374
3375 if (rv < SS_SUCCESS) {
3376 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
3377 return FALSE;
3378 }
3379
3380 if (oconn > C_WF_REPORT_PARAMS) {
3381 if (nconn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
3382 peer_state.disk != D_NEGOTIATING ) {
3383 /* we want resync, peer has not yet decided to sync... */
3384 /* Nowadays only used when forcing a node into primary role and
3385 setting its disk to UpToDate with that */
3386 drbd_send_uuids(mdev);
3387 drbd_send_state(mdev);
3388 }
3389 }
3390
3391 mdev->net_conf->want_lose = 0;
3392
3393 drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
3394
3395 return TRUE;
3396}
3397
Philipp Reisner0b70a132010-08-20 13:36:10 +02003398static int receive_sync_uuid(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003399{
3400 struct p_rs_uuid *p = (struct p_rs_uuid *)h;
3401
3402 wait_event(mdev->misc_wait,
3403 mdev->state.conn == C_WF_SYNC_UUID ||
3404 mdev->state.conn < C_CONNECTED ||
3405 mdev->state.disk < D_NEGOTIATING);
3406
3407 /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */
3408
3409 ERR_IF(h->length != (sizeof(*p)-sizeof(*h))) return FALSE;
3410 if (drbd_recv(mdev, h->payload, h->length) != h->length)
3411 return FALSE;
3412
3413 /* Here the _drbd_uuid_ functions are right, current should
3414 _not_ be rotated into the history */
3415 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
3416 _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid));
3417 _drbd_uuid_set(mdev, UI_BITMAP, 0UL);
3418
3419 drbd_start_resync(mdev, C_SYNC_TARGET);
3420
3421 put_ldev(mdev);
3422 } else
3423 dev_err(DEV, "Ignoring SyncUUID packet!\n");
3424
3425 return TRUE;
3426}
3427
3428enum receive_bitmap_ret { OK, DONE, FAILED };
3429
3430static enum receive_bitmap_ret
Philipp Reisner0b70a132010-08-20 13:36:10 +02003431receive_bitmap_plain(struct drbd_conf *mdev, struct p_header80 *h,
Philipp Reisnerb411b362009-09-25 16:07:19 -07003432 unsigned long *buffer, struct bm_xfer_ctx *c)
3433{
3434 unsigned num_words = min_t(size_t, BM_PACKET_WORDS, c->bm_words - c->word_offset);
3435 unsigned want = num_words * sizeof(long);
3436
3437 if (want != h->length) {
3438 dev_err(DEV, "%s:want (%u) != h->length (%u)\n", __func__, want, h->length);
3439 return FAILED;
3440 }
3441 if (want == 0)
3442 return DONE;
3443 if (drbd_recv(mdev, buffer, want) != want)
3444 return FAILED;
3445
3446 drbd_bm_merge_lel(mdev, c->word_offset, num_words, buffer);
3447
3448 c->word_offset += num_words;
3449 c->bit_offset = c->word_offset * BITS_PER_LONG;
3450 if (c->bit_offset > c->bm_bits)
3451 c->bit_offset = c->bm_bits;
3452
3453 return OK;
3454}
3455
3456static enum receive_bitmap_ret
3457recv_bm_rle_bits(struct drbd_conf *mdev,
3458 struct p_compressed_bm *p,
3459 struct bm_xfer_ctx *c)
3460{
3461 struct bitstream bs;
3462 u64 look_ahead;
3463 u64 rl;
3464 u64 tmp;
3465 unsigned long s = c->bit_offset;
3466 unsigned long e;
3467 int len = p->head.length - (sizeof(*p) - sizeof(p->head));
3468 int toggle = DCBP_get_start(p);
3469 int have;
3470 int bits;
3471
3472 bitstream_init(&bs, p->code, len, DCBP_get_pad_bits(p));
3473
3474 bits = bitstream_get_bits(&bs, &look_ahead, 64);
3475 if (bits < 0)
3476 return FAILED;
3477
3478 for (have = bits; have > 0; s += rl, toggle = !toggle) {
3479 bits = vli_decode_bits(&rl, look_ahead);
3480 if (bits <= 0)
3481 return FAILED;
3482
3483 if (toggle) {
3484 e = s + rl -1;
3485 if (e >= c->bm_bits) {
3486 dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
3487 return FAILED;
3488 }
3489 _drbd_bm_set_bits(mdev, s, e);
3490 }
3491
3492 if (have < bits) {
3493 dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
3494 have, bits, look_ahead,
3495 (unsigned int)(bs.cur.b - p->code),
3496 (unsigned int)bs.buf_len);
3497 return FAILED;
3498 }
3499 look_ahead >>= bits;
3500 have -= bits;
3501
3502 bits = bitstream_get_bits(&bs, &tmp, 64 - have);
3503 if (bits < 0)
3504 return FAILED;
3505 look_ahead |= tmp << have;
3506 have += bits;
3507 }
3508
3509 c->bit_offset = s;
3510 bm_xfer_ctx_bit_to_word_offset(c);
3511
3512 return (s == c->bm_bits) ? DONE : OK;
3513}
3514
3515static enum receive_bitmap_ret
3516decode_bitmap_c(struct drbd_conf *mdev,
3517 struct p_compressed_bm *p,
3518 struct bm_xfer_ctx *c)
3519{
3520 if (DCBP_get_code(p) == RLE_VLI_Bits)
3521 return recv_bm_rle_bits(mdev, p, c);
3522
3523 /* other variants had been implemented for evaluation,
3524 * but have been dropped as this one turned out to be "best"
3525 * during all our tests. */
3526
3527 dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
3528 drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR));
3529 return FAILED;
3530}
3531
3532void INFO_bm_xfer_stats(struct drbd_conf *mdev,
3533 const char *direction, struct bm_xfer_ctx *c)
3534{
3535 /* what would it take to transfer it "plaintext" */
Philipp Reisner0b70a132010-08-20 13:36:10 +02003536 unsigned plain = sizeof(struct p_header80) *
Philipp Reisnerb411b362009-09-25 16:07:19 -07003537 ((c->bm_words+BM_PACKET_WORDS-1)/BM_PACKET_WORDS+1)
3538 + c->bm_words * sizeof(long);
3539 unsigned total = c->bytes[0] + c->bytes[1];
3540 unsigned r;
3541
3542 /* total can not be zero. but just in case: */
3543 if (total == 0)
3544 return;
3545
3546 /* don't report if not compressed */
3547 if (total >= plain)
3548 return;
3549
3550 /* total < plain. check for overflow, still */
3551 r = (total > UINT_MAX/1000) ? (total / (plain/1000))
3552 : (1000 * total / plain);
3553
3554 if (r > 1000)
3555 r = 1000;
3556
3557 r = 1000 - r;
3558 dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
3559 "total %u; compression: %u.%u%%\n",
3560 direction,
3561 c->bytes[1], c->packets[1],
3562 c->bytes[0], c->packets[0],
3563 total, r/10, r % 10);
3564}
3565
3566/* Since we are processing the bitfield from lower addresses to higher,
3567 it does not matter if the process it in 32 bit chunks or 64 bit
3568 chunks as long as it is little endian. (Understand it as byte stream,
3569 beginning with the lowest byte...) If we would use big endian
3570 we would need to process it from the highest address to the lowest,
3571 in order to be agnostic to the 32 vs 64 bits issue.
3572
3573 returns 0 on failure, 1 if we successfully received it. */
Philipp Reisner0b70a132010-08-20 13:36:10 +02003574static int receive_bitmap(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003575{
3576 struct bm_xfer_ctx c;
3577 void *buffer;
3578 enum receive_bitmap_ret ret;
3579 int ok = FALSE;
3580
3581 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
3582
3583 drbd_bm_lock(mdev, "receive bitmap");
3584
3585 /* maybe we should use some per thread scratch page,
3586 * and allocate that during initial device creation? */
3587 buffer = (unsigned long *) __get_free_page(GFP_NOIO);
3588 if (!buffer) {
3589 dev_err(DEV, "failed to allocate one page buffer in %s\n", __func__);
3590 goto out;
3591 }
3592
3593 c = (struct bm_xfer_ctx) {
3594 .bm_bits = drbd_bm_bits(mdev),
3595 .bm_words = drbd_bm_words(mdev),
3596 };
3597
3598 do {
3599 if (h->command == P_BITMAP) {
3600 ret = receive_bitmap_plain(mdev, h, buffer, &c);
3601 } else if (h->command == P_COMPRESSED_BITMAP) {
3602 /* MAYBE: sanity check that we speak proto >= 90,
3603 * and the feature is enabled! */
3604 struct p_compressed_bm *p;
3605
3606 if (h->length > BM_PACKET_PAYLOAD_BYTES) {
3607 dev_err(DEV, "ReportCBitmap packet too large\n");
3608 goto out;
3609 }
3610 /* use the page buff */
3611 p = buffer;
3612 memcpy(p, h, sizeof(*h));
3613 if (drbd_recv(mdev, p->head.payload, h->length) != h->length)
3614 goto out;
3615 if (p->head.length <= (sizeof(*p) - sizeof(p->head))) {
3616 dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", p->head.length);
3617 return FAILED;
3618 }
3619 ret = decode_bitmap_c(mdev, p, &c);
3620 } else {
3621 dev_warn(DEV, "receive_bitmap: h->command neither ReportBitMap nor ReportCBitMap (is 0x%x)", h->command);
3622 goto out;
3623 }
3624
3625 c.packets[h->command == P_BITMAP]++;
Philipp Reisner0b70a132010-08-20 13:36:10 +02003626 c.bytes[h->command == P_BITMAP] += sizeof(struct p_header80) + h->length;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003627
3628 if (ret != OK)
3629 break;
3630
3631 if (!drbd_recv_header(mdev, h))
3632 goto out;
3633 } while (ret == OK);
3634 if (ret == FAILED)
3635 goto out;
3636
3637 INFO_bm_xfer_stats(mdev, "receive", &c);
3638
3639 if (mdev->state.conn == C_WF_BITMAP_T) {
3640 ok = !drbd_send_bitmap(mdev);
3641 if (!ok)
3642 goto out;
3643 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
3644 ok = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
3645 D_ASSERT(ok == SS_SUCCESS);
3646 } else if (mdev->state.conn != C_WF_BITMAP_S) {
3647 /* admin may have requested C_DISCONNECTING,
3648 * other threads may have noticed network errors */
3649 dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n",
3650 drbd_conn_str(mdev->state.conn));
3651 }
3652
3653 ok = TRUE;
3654 out:
3655 drbd_bm_unlock(mdev);
3656 if (ok && mdev->state.conn == C_WF_BITMAP_S)
3657 drbd_start_resync(mdev, C_SYNC_SOURCE);
3658 free_page((unsigned long) buffer);
3659 return ok;
3660}
3661
Philipp Reisner0b70a132010-08-20 13:36:10 +02003662static int receive_skip_(struct drbd_conf *mdev, struct p_header80 *h, int silent)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003663{
3664 /* TODO zero copy sink :) */
3665 static char sink[128];
3666 int size, want, r;
3667
Lars Ellenberge7f52df2010-08-03 20:20:20 +02003668 if (!silent)
3669 dev_warn(DEV, "skipping unknown optional packet type %d, l: %d!\n",
3670 h->command, h->length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003671
3672 size = h->length;
3673 while (size > 0) {
3674 want = min_t(int, size, sizeof(sink));
3675 r = drbd_recv(mdev, sink, want);
3676 ERR_IF(r <= 0) break;
3677 size -= r;
3678 }
3679 return size == 0;
3680}
3681
Philipp Reisner0b70a132010-08-20 13:36:10 +02003682static int receive_skip(struct drbd_conf *mdev, struct p_header80 *h)
Lars Ellenberge7f52df2010-08-03 20:20:20 +02003683{
3684 return receive_skip_(mdev, h, 0);
3685}
3686
Philipp Reisner0b70a132010-08-20 13:36:10 +02003687static int receive_skip_silent(struct drbd_conf *mdev, struct p_header80 *h)
Lars Ellenberge7f52df2010-08-03 20:20:20 +02003688{
3689 return receive_skip_(mdev, h, 1);
3690}
3691
Philipp Reisner0b70a132010-08-20 13:36:10 +02003692static int receive_UnplugRemote(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003693{
3694 if (mdev->state.disk >= D_INCONSISTENT)
3695 drbd_kick_lo(mdev);
3696
3697 /* Make sure we've acked all the TCP data associated
3698 * with the data requests being unplugged */
3699 drbd_tcp_quickack(mdev->data.socket);
3700
3701 return TRUE;
3702}
3703
Philipp Reisner0b70a132010-08-20 13:36:10 +02003704typedef int (*drbd_cmd_handler_f)(struct drbd_conf *, struct p_header80 *);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003705
3706static drbd_cmd_handler_f drbd_default_handler[] = {
3707 [P_DATA] = receive_Data,
3708 [P_DATA_REPLY] = receive_DataReply,
3709 [P_RS_DATA_REPLY] = receive_RSDataReply,
3710 [P_BARRIER] = receive_Barrier,
3711 [P_BITMAP] = receive_bitmap,
3712 [P_COMPRESSED_BITMAP] = receive_bitmap,
3713 [P_UNPLUG_REMOTE] = receive_UnplugRemote,
3714 [P_DATA_REQUEST] = receive_DataRequest,
3715 [P_RS_DATA_REQUEST] = receive_DataRequest,
3716 [P_SYNC_PARAM] = receive_SyncParam,
3717 [P_SYNC_PARAM89] = receive_SyncParam,
3718 [P_PROTOCOL] = receive_protocol,
3719 [P_UUIDS] = receive_uuids,
3720 [P_SIZES] = receive_sizes,
3721 [P_STATE] = receive_state,
3722 [P_STATE_CHG_REQ] = receive_req_state,
3723 [P_SYNC_UUID] = receive_sync_uuid,
3724 [P_OV_REQUEST] = receive_DataRequest,
3725 [P_OV_REPLY] = receive_DataRequest,
3726 [P_CSUM_RS_REQUEST] = receive_DataRequest,
Lars Ellenberge7f52df2010-08-03 20:20:20 +02003727 [P_DELAY_PROBE] = receive_skip_silent,
Philipp Reisnerb411b362009-09-25 16:07:19 -07003728 /* anything missing from this table is in
3729 * the asender_tbl, see get_asender_cmd */
3730 [P_MAX_CMD] = NULL,
3731};
3732
3733static drbd_cmd_handler_f *drbd_cmd_handler = drbd_default_handler;
3734static drbd_cmd_handler_f *drbd_opt_cmd_handler;
3735
3736static void drbdd(struct drbd_conf *mdev)
3737{
3738 drbd_cmd_handler_f handler;
Philipp Reisner0b70a132010-08-20 13:36:10 +02003739 struct p_header80 *header = &mdev->data.rbuf.header;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003740
3741 while (get_t_state(&mdev->receiver) == Running) {
3742 drbd_thread_current_set_cpu(mdev);
Lars Ellenberg0b33a912009-11-16 15:58:04 +01003743 if (!drbd_recv_header(mdev, header)) {
3744 drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003745 break;
Lars Ellenberg0b33a912009-11-16 15:58:04 +01003746 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003747
3748 if (header->command < P_MAX_CMD)
3749 handler = drbd_cmd_handler[header->command];
3750 else if (P_MAY_IGNORE < header->command
3751 && header->command < P_MAX_OPT_CMD)
3752 handler = drbd_opt_cmd_handler[header->command-P_MAY_IGNORE];
3753 else if (header->command > P_MAX_OPT_CMD)
3754 handler = receive_skip;
3755 else
3756 handler = NULL;
3757
3758 if (unlikely(!handler)) {
3759 dev_err(DEV, "unknown packet type %d, l: %d!\n",
3760 header->command, header->length);
3761 drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR));
3762 break;
3763 }
3764 if (unlikely(!handler(mdev, header))) {
3765 dev_err(DEV, "error receiving %s, l: %d!\n",
3766 cmdname(header->command), header->length);
3767 drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR));
3768 break;
3769 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003770 }
3771}
3772
Philipp Reisnerb411b362009-09-25 16:07:19 -07003773void drbd_flush_workqueue(struct drbd_conf *mdev)
3774{
3775 struct drbd_wq_barrier barr;
3776
3777 barr.w.cb = w_prev_work_done;
3778 init_completion(&barr.done);
3779 drbd_queue_work(&mdev->data.work, &barr.w);
3780 wait_for_completion(&barr.done);
3781}
3782
Philipp Reisnerf70b35112010-06-24 14:34:40 +02003783void drbd_free_tl_hash(struct drbd_conf *mdev)
3784{
3785 struct hlist_head *h;
3786
3787 spin_lock_irq(&mdev->req_lock);
3788
3789 if (!mdev->tl_hash || mdev->state.conn != C_STANDALONE) {
3790 spin_unlock_irq(&mdev->req_lock);
3791 return;
3792 }
3793 /* paranoia code */
3794 for (h = mdev->ee_hash; h < mdev->ee_hash + mdev->ee_hash_s; h++)
3795 if (h->first)
3796 dev_err(DEV, "ASSERT FAILED ee_hash[%u].first == %p, expected NULL\n",
3797 (int)(h - mdev->ee_hash), h->first);
3798 kfree(mdev->ee_hash);
3799 mdev->ee_hash = NULL;
3800 mdev->ee_hash_s = 0;
3801
3802 /* paranoia code */
3803 for (h = mdev->tl_hash; h < mdev->tl_hash + mdev->tl_hash_s; h++)
3804 if (h->first)
3805 dev_err(DEV, "ASSERT FAILED tl_hash[%u] == %p, expected NULL\n",
3806 (int)(h - mdev->tl_hash), h->first);
3807 kfree(mdev->tl_hash);
3808 mdev->tl_hash = NULL;
3809 mdev->tl_hash_s = 0;
3810 spin_unlock_irq(&mdev->req_lock);
3811}
3812
Philipp Reisnerb411b362009-09-25 16:07:19 -07003813static void drbd_disconnect(struct drbd_conf *mdev)
3814{
3815 enum drbd_fencing_p fp;
3816 union drbd_state os, ns;
3817 int rv = SS_UNKNOWN_ERROR;
3818 unsigned int i;
3819
3820 if (mdev->state.conn == C_STANDALONE)
3821 return;
3822 if (mdev->state.conn >= C_WF_CONNECTION)
3823 dev_err(DEV, "ASSERT FAILED cstate = %s, expected < WFConnection\n",
3824 drbd_conn_str(mdev->state.conn));
3825
3826 /* asender does not clean up anything. it must not interfere, either */
3827 drbd_thread_stop(&mdev->asender);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003828 drbd_free_sock(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003829
Philipp Reisner85719572010-07-21 10:20:17 +02003830 /* wait for current activity to cease. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003831 spin_lock_irq(&mdev->req_lock);
3832 _drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
3833 _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee);
3834 _drbd_wait_ee_list_empty(mdev, &mdev->read_ee);
3835 spin_unlock_irq(&mdev->req_lock);
3836
3837 /* We do not have data structures that would allow us to
3838 * get the rs_pending_cnt down to 0 again.
3839 * * On C_SYNC_TARGET we do not have any data structures describing
3840 * the pending RSDataRequest's we have sent.
3841 * * On C_SYNC_SOURCE there is no data structure that tracks
3842 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
3843 * And no, it is not the sum of the reference counts in the
3844 * resync_LRU. The resync_LRU tracks the whole operation including
3845 * the disk-IO, while the rs_pending_cnt only tracks the blocks
3846 * on the fly. */
3847 drbd_rs_cancel_all(mdev);
3848 mdev->rs_total = 0;
3849 mdev->rs_failed = 0;
3850 atomic_set(&mdev->rs_pending_cnt, 0);
3851 wake_up(&mdev->misc_wait);
3852
3853 /* make sure syncer is stopped and w_resume_next_sg queued */
3854 del_timer_sync(&mdev->resync_timer);
3855 set_bit(STOP_SYNC_TIMER, &mdev->flags);
3856 resync_timer_fn((unsigned long)mdev);
3857
Philipp Reisnerb411b362009-09-25 16:07:19 -07003858 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
3859 * w_make_resync_request etc. which may still be on the worker queue
3860 * to be "canceled" */
3861 drbd_flush_workqueue(mdev);
3862
3863 /* This also does reclaim_net_ee(). If we do this too early, we might
3864 * miss some resync ee and pages.*/
3865 drbd_process_done_ee(mdev);
3866
3867 kfree(mdev->p_uuid);
3868 mdev->p_uuid = NULL;
3869
3870 if (!mdev->state.susp)
3871 tl_clear(mdev);
3872
Philipp Reisnerb411b362009-09-25 16:07:19 -07003873 dev_info(DEV, "Connection closed\n");
3874
3875 drbd_md_sync(mdev);
3876
3877 fp = FP_DONT_CARE;
3878 if (get_ldev(mdev)) {
3879 fp = mdev->ldev->dc.fencing;
3880 put_ldev(mdev);
3881 }
3882
Philipp Reisner87f7be42010-06-11 13:56:33 +02003883 if (mdev->state.role == R_PRIMARY && fp >= FP_RESOURCE && mdev->state.pdsk >= D_UNKNOWN)
3884 drbd_try_outdate_peer_async(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003885
3886 spin_lock_irq(&mdev->req_lock);
3887 os = mdev->state;
3888 if (os.conn >= C_UNCONNECTED) {
3889 /* Do not restart in case we are C_DISCONNECTING */
3890 ns = os;
3891 ns.conn = C_UNCONNECTED;
3892 rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
3893 }
3894 spin_unlock_irq(&mdev->req_lock);
3895
3896 if (os.conn == C_DISCONNECTING) {
Philipp Reisner84dfb9f2010-06-23 11:20:05 +02003897 wait_event(mdev->net_cnt_wait, atomic_read(&mdev->net_cnt) == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003898
Philipp Reisnerf70b35112010-06-24 14:34:40 +02003899 if (!mdev->state.susp) {
3900 /* we must not free the tl_hash
3901 * while application io is still on the fly */
3902 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
3903 drbd_free_tl_hash(mdev);
3904 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003905
3906 crypto_free_hash(mdev->cram_hmac_tfm);
3907 mdev->cram_hmac_tfm = NULL;
3908
3909 kfree(mdev->net_conf);
3910 mdev->net_conf = NULL;
3911 drbd_request_state(mdev, NS(conn, C_STANDALONE));
3912 }
3913
3914 /* tcp_close and release of sendpage pages can be deferred. I don't
3915 * want to use SO_LINGER, because apparently it can be deferred for
3916 * more than 20 seconds (longest time I checked).
3917 *
3918 * Actually we don't care for exactly when the network stack does its
3919 * put_page(), but release our reference on these pages right here.
3920 */
3921 i = drbd_release_ee(mdev, &mdev->net_ee);
3922 if (i)
3923 dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
3924 i = atomic_read(&mdev->pp_in_use);
3925 if (i)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02003926 dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003927
3928 D_ASSERT(list_empty(&mdev->read_ee));
3929 D_ASSERT(list_empty(&mdev->active_ee));
3930 D_ASSERT(list_empty(&mdev->sync_ee));
3931 D_ASSERT(list_empty(&mdev->done_ee));
3932
3933 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
3934 atomic_set(&mdev->current_epoch->epoch_size, 0);
3935 D_ASSERT(list_empty(&mdev->current_epoch->list));
3936}
3937
3938/*
3939 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
3940 * we can agree on is stored in agreed_pro_version.
3941 *
3942 * feature flags and the reserved array should be enough room for future
3943 * enhancements of the handshake protocol, and possible plugins...
3944 *
3945 * for now, they are expected to be zero, but ignored.
3946 */
3947static int drbd_send_handshake(struct drbd_conf *mdev)
3948{
3949 /* ASSERT current == mdev->receiver ... */
3950 struct p_handshake *p = &mdev->data.sbuf.handshake;
3951 int ok;
3952
3953 if (mutex_lock_interruptible(&mdev->data.mutex)) {
3954 dev_err(DEV, "interrupted during initial handshake\n");
3955 return 0; /* interrupted. not ok. */
3956 }
3957
3958 if (mdev->data.socket == NULL) {
3959 mutex_unlock(&mdev->data.mutex);
3960 return 0;
3961 }
3962
3963 memset(p, 0, sizeof(*p));
3964 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
3965 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
3966 ok = _drbd_send_cmd( mdev, mdev->data.socket, P_HAND_SHAKE,
Philipp Reisner0b70a132010-08-20 13:36:10 +02003967 (struct p_header80 *)p, sizeof(*p), 0 );
Philipp Reisnerb411b362009-09-25 16:07:19 -07003968 mutex_unlock(&mdev->data.mutex);
3969 return ok;
3970}
3971
3972/*
3973 * return values:
3974 * 1 yes, we have a valid connection
3975 * 0 oops, did not work out, please try again
3976 * -1 peer talks different language,
3977 * no point in trying again, please go standalone.
3978 */
3979static int drbd_do_handshake(struct drbd_conf *mdev)
3980{
3981 /* ASSERT current == mdev->receiver ... */
3982 struct p_handshake *p = &mdev->data.rbuf.handshake;
3983 const int expect = sizeof(struct p_handshake)
Philipp Reisner0b70a132010-08-20 13:36:10 +02003984 -sizeof(struct p_header80);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003985 int rv;
3986
3987 rv = drbd_send_handshake(mdev);
3988 if (!rv)
3989 return 0;
3990
3991 rv = drbd_recv_header(mdev, &p->head);
3992 if (!rv)
3993 return 0;
3994
3995 if (p->head.command != P_HAND_SHAKE) {
3996 dev_err(DEV, "expected HandShake packet, received: %s (0x%04x)\n",
3997 cmdname(p->head.command), p->head.command);
3998 return -1;
3999 }
4000
4001 if (p->head.length != expect) {
4002 dev_err(DEV, "expected HandShake length: %u, received: %u\n",
4003 expect, p->head.length);
4004 return -1;
4005 }
4006
4007 rv = drbd_recv(mdev, &p->head.payload, expect);
4008
4009 if (rv != expect) {
4010 dev_err(DEV, "short read receiving handshake packet: l=%u\n", rv);
4011 return 0;
4012 }
4013
Philipp Reisnerb411b362009-09-25 16:07:19 -07004014 p->protocol_min = be32_to_cpu(p->protocol_min);
4015 p->protocol_max = be32_to_cpu(p->protocol_max);
4016 if (p->protocol_max == 0)
4017 p->protocol_max = p->protocol_min;
4018
4019 if (PRO_VERSION_MAX < p->protocol_min ||
4020 PRO_VERSION_MIN > p->protocol_max)
4021 goto incompat;
4022
4023 mdev->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
4024
4025 dev_info(DEV, "Handshake successful: "
4026 "Agreed network protocol version %d\n", mdev->agreed_pro_version);
4027
4028 return 1;
4029
4030 incompat:
4031 dev_err(DEV, "incompatible DRBD dialects: "
4032 "I support %d-%d, peer supports %d-%d\n",
4033 PRO_VERSION_MIN, PRO_VERSION_MAX,
4034 p->protocol_min, p->protocol_max);
4035 return -1;
4036}
4037
4038#if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
4039static int drbd_do_auth(struct drbd_conf *mdev)
4040{
4041 dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
4042 dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004043 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004044}
4045#else
4046#define CHALLENGE_LEN 64
Johannes Thomab10d96c2010-01-07 16:02:50 +01004047
4048/* Return value:
4049 1 - auth succeeded,
4050 0 - failed, try again (network error),
4051 -1 - auth failed, don't try again.
4052*/
4053
Philipp Reisnerb411b362009-09-25 16:07:19 -07004054static int drbd_do_auth(struct drbd_conf *mdev)
4055{
4056 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
4057 struct scatterlist sg;
4058 char *response = NULL;
4059 char *right_response = NULL;
4060 char *peers_ch = NULL;
Philipp Reisner0b70a132010-08-20 13:36:10 +02004061 struct p_header80 p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004062 unsigned int key_len = strlen(mdev->net_conf->shared_secret);
4063 unsigned int resp_size;
4064 struct hash_desc desc;
4065 int rv;
4066
4067 desc.tfm = mdev->cram_hmac_tfm;
4068 desc.flags = 0;
4069
4070 rv = crypto_hash_setkey(mdev->cram_hmac_tfm,
4071 (u8 *)mdev->net_conf->shared_secret, key_len);
4072 if (rv) {
4073 dev_err(DEV, "crypto_hash_setkey() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004074 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004075 goto fail;
4076 }
4077
4078 get_random_bytes(my_challenge, CHALLENGE_LEN);
4079
4080 rv = drbd_send_cmd2(mdev, P_AUTH_CHALLENGE, my_challenge, CHALLENGE_LEN);
4081 if (!rv)
4082 goto fail;
4083
4084 rv = drbd_recv_header(mdev, &p);
4085 if (!rv)
4086 goto fail;
4087
4088 if (p.command != P_AUTH_CHALLENGE) {
4089 dev_err(DEV, "expected AuthChallenge packet, received: %s (0x%04x)\n",
4090 cmdname(p.command), p.command);
4091 rv = 0;
4092 goto fail;
4093 }
4094
4095 if (p.length > CHALLENGE_LEN*2) {
4096 dev_err(DEV, "expected AuthChallenge payload too big.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004097 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004098 goto fail;
4099 }
4100
4101 peers_ch = kmalloc(p.length, GFP_NOIO);
4102 if (peers_ch == NULL) {
4103 dev_err(DEV, "kmalloc of peers_ch failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004104 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004105 goto fail;
4106 }
4107
4108 rv = drbd_recv(mdev, peers_ch, p.length);
4109
4110 if (rv != p.length) {
4111 dev_err(DEV, "short read AuthChallenge: l=%u\n", rv);
4112 rv = 0;
4113 goto fail;
4114 }
4115
4116 resp_size = crypto_hash_digestsize(mdev->cram_hmac_tfm);
4117 response = kmalloc(resp_size, GFP_NOIO);
4118 if (response == NULL) {
4119 dev_err(DEV, "kmalloc of response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004120 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004121 goto fail;
4122 }
4123
4124 sg_init_table(&sg, 1);
4125 sg_set_buf(&sg, peers_ch, p.length);
4126
4127 rv = crypto_hash_digest(&desc, &sg, sg.length, response);
4128 if (rv) {
4129 dev_err(DEV, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004130 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004131 goto fail;
4132 }
4133
4134 rv = drbd_send_cmd2(mdev, P_AUTH_RESPONSE, response, resp_size);
4135 if (!rv)
4136 goto fail;
4137
4138 rv = drbd_recv_header(mdev, &p);
4139 if (!rv)
4140 goto fail;
4141
4142 if (p.command != P_AUTH_RESPONSE) {
4143 dev_err(DEV, "expected AuthResponse packet, received: %s (0x%04x)\n",
4144 cmdname(p.command), p.command);
4145 rv = 0;
4146 goto fail;
4147 }
4148
4149 if (p.length != resp_size) {
4150 dev_err(DEV, "expected AuthResponse payload of wrong size\n");
4151 rv = 0;
4152 goto fail;
4153 }
4154
4155 rv = drbd_recv(mdev, response , resp_size);
4156
4157 if (rv != resp_size) {
4158 dev_err(DEV, "short read receiving AuthResponse: l=%u\n", rv);
4159 rv = 0;
4160 goto fail;
4161 }
4162
4163 right_response = kmalloc(resp_size, GFP_NOIO);
Julia Lawall2d1ee872009-12-27 22:27:11 +01004164 if (right_response == NULL) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004165 dev_err(DEV, "kmalloc of right_response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004166 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004167 goto fail;
4168 }
4169
4170 sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
4171
4172 rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
4173 if (rv) {
4174 dev_err(DEV, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004175 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004176 goto fail;
4177 }
4178
4179 rv = !memcmp(response, right_response, resp_size);
4180
4181 if (rv)
4182 dev_info(DEV, "Peer authenticated using %d bytes of '%s' HMAC\n",
4183 resp_size, mdev->net_conf->cram_hmac_alg);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004184 else
4185 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004186
4187 fail:
4188 kfree(peers_ch);
4189 kfree(response);
4190 kfree(right_response);
4191
4192 return rv;
4193}
4194#endif
4195
4196int drbdd_init(struct drbd_thread *thi)
4197{
4198 struct drbd_conf *mdev = thi->mdev;
4199 unsigned int minor = mdev_to_minor(mdev);
4200 int h;
4201
4202 sprintf(current->comm, "drbd%d_receiver", minor);
4203
4204 dev_info(DEV, "receiver (re)started\n");
4205
4206 do {
4207 h = drbd_connect(mdev);
4208 if (h == 0) {
4209 drbd_disconnect(mdev);
4210 __set_current_state(TASK_INTERRUPTIBLE);
4211 schedule_timeout(HZ);
4212 }
4213 if (h == -1) {
4214 dev_warn(DEV, "Discarding network configuration.\n");
4215 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
4216 }
4217 } while (h == 0);
4218
4219 if (h > 0) {
4220 if (get_net_conf(mdev)) {
4221 drbdd(mdev);
4222 put_net_conf(mdev);
4223 }
4224 }
4225
4226 drbd_disconnect(mdev);
4227
4228 dev_info(DEV, "receiver terminated\n");
4229 return 0;
4230}
4231
4232/* ********* acknowledge sender ******** */
4233
Philipp Reisner0b70a132010-08-20 13:36:10 +02004234static int got_RqSReply(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004235{
4236 struct p_req_state_reply *p = (struct p_req_state_reply *)h;
4237
4238 int retcode = be32_to_cpu(p->retcode);
4239
4240 if (retcode >= SS_SUCCESS) {
4241 set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
4242 } else {
4243 set_bit(CL_ST_CHG_FAIL, &mdev->flags);
4244 dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
4245 drbd_set_st_err_str(retcode), retcode);
4246 }
4247 wake_up(&mdev->state_wait);
4248
4249 return TRUE;
4250}
4251
Philipp Reisner0b70a132010-08-20 13:36:10 +02004252static int got_Ping(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004253{
4254 return drbd_send_ping_ack(mdev);
4255
4256}
4257
Philipp Reisner0b70a132010-08-20 13:36:10 +02004258static int got_PingAck(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004259{
4260 /* restore idle timeout */
4261 mdev->meta.socket->sk->sk_rcvtimeo = mdev->net_conf->ping_int*HZ;
Philipp Reisner309d1602010-03-02 15:03:44 +01004262 if (!test_and_set_bit(GOT_PING_ACK, &mdev->flags))
4263 wake_up(&mdev->misc_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004264
4265 return TRUE;
4266}
4267
Philipp Reisner0b70a132010-08-20 13:36:10 +02004268static int got_IsInSync(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004269{
4270 struct p_block_ack *p = (struct p_block_ack *)h;
4271 sector_t sector = be64_to_cpu(p->sector);
4272 int blksize = be32_to_cpu(p->blksize);
4273
4274 D_ASSERT(mdev->agreed_pro_version >= 89);
4275
4276 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4277
4278 drbd_rs_complete_io(mdev, sector);
4279 drbd_set_in_sync(mdev, sector, blksize);
4280 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
4281 mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
4282 dec_rs_pending(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02004283 atomic_add(blksize >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004284
4285 return TRUE;
4286}
4287
4288/* when we receive the ACK for a write request,
4289 * verify that we actually know about it */
4290static struct drbd_request *_ack_id_to_req(struct drbd_conf *mdev,
4291 u64 id, sector_t sector)
4292{
4293 struct hlist_head *slot = tl_hash_slot(mdev, sector);
4294 struct hlist_node *n;
4295 struct drbd_request *req;
4296
4297 hlist_for_each_entry(req, n, slot, colision) {
4298 if ((unsigned long)req == (unsigned long)id) {
4299 if (req->sector != sector) {
4300 dev_err(DEV, "_ack_id_to_req: found req %p but it has "
4301 "wrong sector (%llus versus %llus)\n", req,
4302 (unsigned long long)req->sector,
4303 (unsigned long long)sector);
4304 break;
4305 }
4306 return req;
4307 }
4308 }
4309 dev_err(DEV, "_ack_id_to_req: failed to find req %p, sector %llus in list\n",
4310 (void *)(unsigned long)id, (unsigned long long)sector);
4311 return NULL;
4312}
4313
4314typedef struct drbd_request *(req_validator_fn)
4315 (struct drbd_conf *mdev, u64 id, sector_t sector);
4316
4317static int validate_req_change_req_state(struct drbd_conf *mdev,
4318 u64 id, sector_t sector, req_validator_fn validator,
4319 const char *func, enum drbd_req_event what)
4320{
4321 struct drbd_request *req;
4322 struct bio_and_error m;
4323
4324 spin_lock_irq(&mdev->req_lock);
4325 req = validator(mdev, id, sector);
4326 if (unlikely(!req)) {
4327 spin_unlock_irq(&mdev->req_lock);
4328 dev_err(DEV, "%s: got a corrupt block_id/sector pair\n", func);
4329 return FALSE;
4330 }
4331 __req_mod(req, what, &m);
4332 spin_unlock_irq(&mdev->req_lock);
4333
4334 if (m.bio)
4335 complete_master_bio(mdev, &m);
4336 return TRUE;
4337}
4338
Philipp Reisner0b70a132010-08-20 13:36:10 +02004339static int got_BlockAck(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004340{
4341 struct p_block_ack *p = (struct p_block_ack *)h;
4342 sector_t sector = be64_to_cpu(p->sector);
4343 int blksize = be32_to_cpu(p->blksize);
4344 enum drbd_req_event what;
4345
4346 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4347
4348 if (is_syncer_block_id(p->block_id)) {
4349 drbd_set_in_sync(mdev, sector, blksize);
4350 dec_rs_pending(mdev);
4351 return TRUE;
4352 }
4353 switch (be16_to_cpu(h->command)) {
4354 case P_RS_WRITE_ACK:
4355 D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
4356 what = write_acked_by_peer_and_sis;
4357 break;
4358 case P_WRITE_ACK:
4359 D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
4360 what = write_acked_by_peer;
4361 break;
4362 case P_RECV_ACK:
4363 D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_B);
4364 what = recv_acked_by_peer;
4365 break;
4366 case P_DISCARD_ACK:
4367 D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
4368 what = conflict_discarded_by_peer;
4369 break;
4370 default:
4371 D_ASSERT(0);
4372 return FALSE;
4373 }
4374
4375 return validate_req_change_req_state(mdev, p->block_id, sector,
4376 _ack_id_to_req, __func__ , what);
4377}
4378
Philipp Reisner0b70a132010-08-20 13:36:10 +02004379static int got_NegAck(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004380{
4381 struct p_block_ack *p = (struct p_block_ack *)h;
4382 sector_t sector = be64_to_cpu(p->sector);
4383
4384 if (__ratelimit(&drbd_ratelimit_state))
4385 dev_warn(DEV, "Got NegAck packet. Peer is in troubles?\n");
4386
4387 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4388
4389 if (is_syncer_block_id(p->block_id)) {
4390 int size = be32_to_cpu(p->blksize);
4391 dec_rs_pending(mdev);
4392 drbd_rs_failed_io(mdev, sector, size);
4393 return TRUE;
4394 }
4395 return validate_req_change_req_state(mdev, p->block_id, sector,
4396 _ack_id_to_req, __func__ , neg_acked);
4397}
4398
Philipp Reisner0b70a132010-08-20 13:36:10 +02004399static int got_NegDReply(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004400{
4401 struct p_block_ack *p = (struct p_block_ack *)h;
4402 sector_t sector = be64_to_cpu(p->sector);
4403
4404 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4405 dev_err(DEV, "Got NegDReply; Sector %llus, len %u; Fail original request.\n",
4406 (unsigned long long)sector, be32_to_cpu(p->blksize));
4407
4408 return validate_req_change_req_state(mdev, p->block_id, sector,
4409 _ar_id_to_req, __func__ , neg_acked);
4410}
4411
Philipp Reisner0b70a132010-08-20 13:36:10 +02004412static int got_NegRSDReply(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004413{
4414 sector_t sector;
4415 int size;
4416 struct p_block_ack *p = (struct p_block_ack *)h;
4417
4418 sector = be64_to_cpu(p->sector);
4419 size = be32_to_cpu(p->blksize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004420
4421 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4422
4423 dec_rs_pending(mdev);
4424
4425 if (get_ldev_if_state(mdev, D_FAILED)) {
4426 drbd_rs_complete_io(mdev, sector);
4427 drbd_rs_failed_io(mdev, sector, size);
4428 put_ldev(mdev);
4429 }
4430
4431 return TRUE;
4432}
4433
Philipp Reisner0b70a132010-08-20 13:36:10 +02004434static int got_BarrierAck(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004435{
4436 struct p_barrier_ack *p = (struct p_barrier_ack *)h;
4437
4438 tl_release(mdev, p->barrier, be32_to_cpu(p->set_size));
4439
4440 return TRUE;
4441}
4442
Philipp Reisner0b70a132010-08-20 13:36:10 +02004443static int got_OVResult(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004444{
4445 struct p_block_ack *p = (struct p_block_ack *)h;
4446 struct drbd_work *w;
4447 sector_t sector;
4448 int size;
4449
4450 sector = be64_to_cpu(p->sector);
4451 size = be32_to_cpu(p->blksize);
4452
4453 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4454
4455 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
4456 drbd_ov_oos_found(mdev, sector, size);
4457 else
4458 ov_oos_print(mdev);
4459
4460 drbd_rs_complete_io(mdev, sector);
4461 dec_rs_pending(mdev);
4462
4463 if (--mdev->ov_left == 0) {
4464 w = kmalloc(sizeof(*w), GFP_NOIO);
4465 if (w) {
4466 w->cb = w_ov_finished;
4467 drbd_queue_work_front(&mdev->data.work, w);
4468 } else {
4469 dev_err(DEV, "kmalloc(w) failed.");
4470 ov_oos_print(mdev);
4471 drbd_resync_finished(mdev);
4472 }
4473 }
4474 return TRUE;
4475}
4476
Philipp Reisner0b70a132010-08-20 13:36:10 +02004477static int got_something_to_ignore_m(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisner0ced55a2010-04-30 15:26:20 +02004478{
Lars Ellenberge7f52df2010-08-03 20:20:20 +02004479 /* IGNORE */
Philipp Reisner0ced55a2010-04-30 15:26:20 +02004480 return TRUE;
4481}
4482
Philipp Reisnerb411b362009-09-25 16:07:19 -07004483struct asender_cmd {
4484 size_t pkt_size;
Philipp Reisner0b70a132010-08-20 13:36:10 +02004485 int (*process)(struct drbd_conf *mdev, struct p_header80 *h);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004486};
4487
4488static struct asender_cmd *get_asender_cmd(int cmd)
4489{
4490 static struct asender_cmd asender_tbl[] = {
4491 /* anything missing from this table is in
4492 * the drbd_cmd_handler (drbd_default_handler) table,
4493 * see the beginning of drbdd() */
Philipp Reisner0b70a132010-08-20 13:36:10 +02004494 [P_PING] = { sizeof(struct p_header80), got_Ping },
4495 [P_PING_ACK] = { sizeof(struct p_header80), got_PingAck },
Philipp Reisnerb411b362009-09-25 16:07:19 -07004496 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4497 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4498 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4499 [P_DISCARD_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4500 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
4501 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
4502 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply},
4503 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
4504 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
4505 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
4506 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
Philipp Reisner0b70a132010-08-20 13:36:10 +02004507 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_something_to_ignore_m },
Philipp Reisnerb411b362009-09-25 16:07:19 -07004508 [P_MAX_CMD] = { 0, NULL },
4509 };
4510 if (cmd > P_MAX_CMD || asender_tbl[cmd].process == NULL)
4511 return NULL;
4512 return &asender_tbl[cmd];
4513}
4514
4515int drbd_asender(struct drbd_thread *thi)
4516{
4517 struct drbd_conf *mdev = thi->mdev;
Philipp Reisner0b70a132010-08-20 13:36:10 +02004518 struct p_header80 *h = &mdev->meta.rbuf.header;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004519 struct asender_cmd *cmd = NULL;
4520
4521 int rv, len;
4522 void *buf = h;
4523 int received = 0;
Philipp Reisner0b70a132010-08-20 13:36:10 +02004524 int expect = sizeof(struct p_header80);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004525 int empty;
4526
4527 sprintf(current->comm, "drbd%d_asender", mdev_to_minor(mdev));
4528
4529 current->policy = SCHED_RR; /* Make this a realtime task! */
4530 current->rt_priority = 2; /* more important than all other tasks */
4531
4532 while (get_t_state(thi) == Running) {
4533 drbd_thread_current_set_cpu(mdev);
4534 if (test_and_clear_bit(SEND_PING, &mdev->flags)) {
4535 ERR_IF(!drbd_send_ping(mdev)) goto reconnect;
4536 mdev->meta.socket->sk->sk_rcvtimeo =
4537 mdev->net_conf->ping_timeo*HZ/10;
4538 }
4539
4540 /* conditionally cork;
4541 * it may hurt latency if we cork without much to send */
4542 if (!mdev->net_conf->no_cork &&
4543 3 < atomic_read(&mdev->unacked_cnt))
4544 drbd_tcp_cork(mdev->meta.socket);
4545 while (1) {
4546 clear_bit(SIGNAL_ASENDER, &mdev->flags);
4547 flush_signals(current);
4548 if (!drbd_process_done_ee(mdev)) {
4549 dev_err(DEV, "process_done_ee() = NOT_OK\n");
4550 goto reconnect;
4551 }
4552 /* to avoid race with newly queued ACKs */
4553 set_bit(SIGNAL_ASENDER, &mdev->flags);
4554 spin_lock_irq(&mdev->req_lock);
4555 empty = list_empty(&mdev->done_ee);
4556 spin_unlock_irq(&mdev->req_lock);
4557 /* new ack may have been queued right here,
4558 * but then there is also a signal pending,
4559 * and we start over... */
4560 if (empty)
4561 break;
4562 }
4563 /* but unconditionally uncork unless disabled */
4564 if (!mdev->net_conf->no_cork)
4565 drbd_tcp_uncork(mdev->meta.socket);
4566
4567 /* short circuit, recv_msg would return EINTR anyways. */
4568 if (signal_pending(current))
4569 continue;
4570
4571 rv = drbd_recv_short(mdev, mdev->meta.socket,
4572 buf, expect-received, 0);
4573 clear_bit(SIGNAL_ASENDER, &mdev->flags);
4574
4575 flush_signals(current);
4576
4577 /* Note:
4578 * -EINTR (on meta) we got a signal
4579 * -EAGAIN (on meta) rcvtimeo expired
4580 * -ECONNRESET other side closed the connection
4581 * -ERESTARTSYS (on data) we got a signal
4582 * rv < 0 other than above: unexpected error!
4583 * rv == expected: full header or command
4584 * rv < expected: "woken" by signal during receive
4585 * rv == 0 : "connection shut down by peer"
4586 */
4587 if (likely(rv > 0)) {
4588 received += rv;
4589 buf += rv;
4590 } else if (rv == 0) {
4591 dev_err(DEV, "meta connection shut down by peer.\n");
4592 goto reconnect;
4593 } else if (rv == -EAGAIN) {
4594 if (mdev->meta.socket->sk->sk_rcvtimeo ==
4595 mdev->net_conf->ping_timeo*HZ/10) {
4596 dev_err(DEV, "PingAck did not arrive in time.\n");
4597 goto reconnect;
4598 }
4599 set_bit(SEND_PING, &mdev->flags);
4600 continue;
4601 } else if (rv == -EINTR) {
4602 continue;
4603 } else {
4604 dev_err(DEV, "sock_recvmsg returned %d\n", rv);
4605 goto reconnect;
4606 }
4607
4608 if (received == expect && cmd == NULL) {
4609 if (unlikely(h->magic != BE_DRBD_MAGIC)) {
4610 dev_err(DEV, "magic?? on meta m: 0x%lx c: %d l: %d\n",
4611 (long)be32_to_cpu(h->magic),
4612 h->command, h->length);
4613 goto reconnect;
4614 }
4615 cmd = get_asender_cmd(be16_to_cpu(h->command));
4616 len = be16_to_cpu(h->length);
4617 if (unlikely(cmd == NULL)) {
4618 dev_err(DEV, "unknown command?? on meta m: 0x%lx c: %d l: %d\n",
4619 (long)be32_to_cpu(h->magic),
4620 h->command, h->length);
4621 goto disconnect;
4622 }
4623 expect = cmd->pkt_size;
Philipp Reisner0b70a132010-08-20 13:36:10 +02004624 ERR_IF(len != expect-sizeof(struct p_header80))
Philipp Reisnerb411b362009-09-25 16:07:19 -07004625 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004626 }
4627 if (received == expect) {
4628 D_ASSERT(cmd != NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004629 if (!cmd->process(mdev, h))
4630 goto reconnect;
4631
4632 buf = h;
4633 received = 0;
Philipp Reisner0b70a132010-08-20 13:36:10 +02004634 expect = sizeof(struct p_header80);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004635 cmd = NULL;
4636 }
4637 }
4638
4639 if (0) {
4640reconnect:
4641 drbd_force_state(mdev, NS(conn, C_NETWORK_FAILURE));
4642 }
4643 if (0) {
4644disconnect:
4645 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
4646 }
4647 clear_bit(SIGNAL_ASENDER, &mdev->flags);
4648
4649 D_ASSERT(mdev->state.conn < C_CONNECTED);
4650 dev_info(DEV, "asender terminated\n");
4651
4652 return 0;
4653}