blob: 2952c1277b1d840287819506bdb90483cbdd6559 [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
Philipp Reisnerb411b362009-09-25 16:07:19 -070052enum finish_epoch {
53 FE_STILL_LIVE,
54 FE_DESTROYED,
55 FE_RECYCLED,
56};
57
58static int drbd_do_handshake(struct drbd_conf *mdev);
59static int drbd_do_auth(struct drbd_conf *mdev);
60
61static enum finish_epoch drbd_may_finish_epoch(struct drbd_conf *, struct drbd_epoch *, enum epoch_event);
62static int e_end_block(struct drbd_conf *, struct drbd_work *, int);
63
Philipp Reisnerb411b362009-09-25 16:07:19 -070064
65#define GFP_TRY (__GFP_HIGHMEM | __GFP_NOWARN)
66
Lars Ellenberg45bb9122010-05-14 17:10:48 +020067/*
68 * some helper functions to deal with single linked page lists,
69 * page->private being our "next" pointer.
70 */
71
72/* If at least n pages are linked at head, get n pages off.
73 * Otherwise, don't modify head, and return NULL.
74 * Locking is the responsibility of the caller.
75 */
76static struct page *page_chain_del(struct page **head, int n)
77{
78 struct page *page;
79 struct page *tmp;
80
81 BUG_ON(!n);
82 BUG_ON(!head);
83
84 page = *head;
Philipp Reisner23ce4222010-05-20 13:35:31 +020085
86 if (!page)
87 return NULL;
88
Lars Ellenberg45bb9122010-05-14 17:10:48 +020089 while (page) {
90 tmp = page_chain_next(page);
91 if (--n == 0)
92 break; /* found sufficient pages */
93 if (tmp == NULL)
94 /* insufficient pages, don't use any of them. */
95 return NULL;
96 page = tmp;
97 }
98
99 /* add end of list marker for the returned list */
100 set_page_private(page, 0);
101 /* actual return value, and adjustment of head */
102 page = *head;
103 *head = tmp;
104 return page;
105}
106
107/* may be used outside of locks to find the tail of a (usually short)
108 * "private" page chain, before adding it back to a global chain head
109 * with page_chain_add() under a spinlock. */
110static struct page *page_chain_tail(struct page *page, int *len)
111{
112 struct page *tmp;
113 int i = 1;
114 while ((tmp = page_chain_next(page)))
115 ++i, page = tmp;
116 if (len)
117 *len = i;
118 return page;
119}
120
121static int page_chain_free(struct page *page)
122{
123 struct page *tmp;
124 int i = 0;
125 page_chain_for_each_safe(page, tmp) {
126 put_page(page);
127 ++i;
128 }
129 return i;
130}
131
132static void page_chain_add(struct page **head,
133 struct page *chain_first, struct page *chain_last)
134{
135#if 1
136 struct page *tmp;
137 tmp = page_chain_tail(chain_first, NULL);
138 BUG_ON(tmp != chain_last);
139#endif
140
141 /* add chain to head */
142 set_page_private(chain_last, (unsigned long)*head);
143 *head = chain_first;
144}
145
146static struct page *drbd_pp_first_pages_or_try_alloc(struct drbd_conf *mdev, int number)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700147{
148 struct page *page = NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200149 struct page *tmp = NULL;
150 int i = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700151
152 /* Yes, testing drbd_pp_vacant outside the lock is racy.
153 * So what. It saves a spin_lock. */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200154 if (drbd_pp_vacant >= number) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700155 spin_lock(&drbd_pp_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200156 page = page_chain_del(&drbd_pp_pool, number);
157 if (page)
158 drbd_pp_vacant -= number;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700159 spin_unlock(&drbd_pp_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200160 if (page)
161 return page;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700162 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200163
Philipp Reisnerb411b362009-09-25 16:07:19 -0700164 /* GFP_TRY, because we must not cause arbitrary write-out: in a DRBD
165 * "criss-cross" setup, that might cause write-out on some other DRBD,
166 * which in turn might block on the other node at this very place. */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200167 for (i = 0; i < number; i++) {
168 tmp = alloc_page(GFP_TRY);
169 if (!tmp)
170 break;
171 set_page_private(tmp, (unsigned long)page);
172 page = tmp;
173 }
174
175 if (i == number)
176 return page;
177
178 /* Not enough pages immediately available this time.
179 * No need to jump around here, drbd_pp_alloc will retry this
180 * function "soon". */
181 if (page) {
182 tmp = page_chain_tail(page, NULL);
183 spin_lock(&drbd_pp_lock);
184 page_chain_add(&drbd_pp_pool, page, tmp);
185 drbd_pp_vacant += i;
186 spin_unlock(&drbd_pp_lock);
187 }
188 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700189}
190
191/* kick lower level device, if we have more than (arbitrary number)
192 * reference counts on it, which typically are locally submitted io
193 * requests. don't use unacked_cnt, so we speed up proto A and B, too. */
194static void maybe_kick_lo(struct drbd_conf *mdev)
195{
196 if (atomic_read(&mdev->local_cnt) >= mdev->net_conf->unplug_watermark)
197 drbd_kick_lo(mdev);
198}
199
200static void reclaim_net_ee(struct drbd_conf *mdev, struct list_head *to_be_freed)
201{
202 struct drbd_epoch_entry *e;
203 struct list_head *le, *tle;
204
205 /* The EEs are always appended to the end of the list. Since
206 they are sent in order over the wire, they have to finish
207 in order. As soon as we see the first not finished we can
208 stop to examine the list... */
209
210 list_for_each_safe(le, tle, &mdev->net_ee) {
211 e = list_entry(le, struct drbd_epoch_entry, w.list);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200212 if (drbd_ee_has_active_page(e))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700213 break;
214 list_move(le, to_be_freed);
215 }
216}
217
218static void drbd_kick_lo_and_reclaim_net(struct drbd_conf *mdev)
219{
220 LIST_HEAD(reclaimed);
221 struct drbd_epoch_entry *e, *t;
222
223 maybe_kick_lo(mdev);
224 spin_lock_irq(&mdev->req_lock);
225 reclaim_net_ee(mdev, &reclaimed);
226 spin_unlock_irq(&mdev->req_lock);
227
228 list_for_each_entry_safe(e, t, &reclaimed, w.list)
Lars Ellenberg435f0742010-09-06 12:30:25 +0200229 drbd_free_net_ee(mdev, e);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700230}
231
232/**
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200233 * drbd_pp_alloc() - Returns @number pages, retries forever (or until signalled)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700234 * @mdev: DRBD device.
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200235 * @number: number of pages requested
236 * @retry: whether to retry, if not enough pages are available right now
Philipp Reisnerb411b362009-09-25 16:07:19 -0700237 *
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200238 * Tries to allocate number pages, first from our own page pool, then from
239 * the kernel, unless this allocation would exceed the max_buffers setting.
240 * Possibly retry until DRBD frees sufficient pages somewhere else.
241 *
242 * Returns a page chain linked via page->private.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700243 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200244static struct page *drbd_pp_alloc(struct drbd_conf *mdev, unsigned number, bool retry)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700245{
246 struct page *page = NULL;
247 DEFINE_WAIT(wait);
248
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200249 /* Yes, we may run up to @number over max_buffers. If we
250 * follow it strictly, the admin will get it wrong anyways. */
251 if (atomic_read(&mdev->pp_in_use) < mdev->net_conf->max_buffers)
252 page = drbd_pp_first_pages_or_try_alloc(mdev, number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700253
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200254 while (page == NULL) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700255 prepare_to_wait(&drbd_pp_wait, &wait, TASK_INTERRUPTIBLE);
256
257 drbd_kick_lo_and_reclaim_net(mdev);
258
259 if (atomic_read(&mdev->pp_in_use) < mdev->net_conf->max_buffers) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200260 page = drbd_pp_first_pages_or_try_alloc(mdev, number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700261 if (page)
262 break;
263 }
264
265 if (!retry)
266 break;
267
268 if (signal_pending(current)) {
269 dev_warn(DEV, "drbd_pp_alloc interrupted!\n");
270 break;
271 }
272
273 schedule();
274 }
275 finish_wait(&drbd_pp_wait, &wait);
276
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200277 if (page)
278 atomic_add(number, &mdev->pp_in_use);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700279 return page;
280}
281
282/* Must not be used from irq, as that may deadlock: see drbd_pp_alloc.
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200283 * Is also used from inside an other spin_lock_irq(&mdev->req_lock);
284 * Either links the page chain back to the global pool,
285 * or returns all pages to the system. */
Lars Ellenberg435f0742010-09-06 12:30:25 +0200286static void drbd_pp_free(struct drbd_conf *mdev, struct page *page, int is_net)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700287{
Lars Ellenberg435f0742010-09-06 12:30:25 +0200288 atomic_t *a = is_net ? &mdev->pp_in_use_by_net : &mdev->pp_in_use;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700289 int i;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200290
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200291 if (drbd_pp_vacant > (DRBD_MAX_SEGMENT_SIZE/PAGE_SIZE)*minor_count)
292 i = page_chain_free(page);
293 else {
294 struct page *tmp;
295 tmp = page_chain_tail(page, &i);
296 spin_lock(&drbd_pp_lock);
297 page_chain_add(&drbd_pp_pool, page, tmp);
298 drbd_pp_vacant += i;
299 spin_unlock(&drbd_pp_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700300 }
Lars Ellenberg435f0742010-09-06 12:30:25 +0200301 i = atomic_sub_return(i, a);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200302 if (i < 0)
Lars Ellenberg435f0742010-09-06 12:30:25 +0200303 dev_warn(DEV, "ASSERTION FAILED: %s: %d < 0\n",
304 is_net ? "pp_in_use_by_net" : "pp_in_use", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700305 wake_up(&drbd_pp_wait);
306}
307
308/*
309You need to hold the req_lock:
310 _drbd_wait_ee_list_empty()
311
312You must not have the req_lock:
313 drbd_free_ee()
314 drbd_alloc_ee()
315 drbd_init_ee()
316 drbd_release_ee()
317 drbd_ee_fix_bhs()
318 drbd_process_done_ee()
319 drbd_clear_done_ee()
320 drbd_wait_ee_list_empty()
321*/
322
323struct drbd_epoch_entry *drbd_alloc_ee(struct drbd_conf *mdev,
324 u64 id,
325 sector_t sector,
326 unsigned int data_size,
327 gfp_t gfp_mask) __must_hold(local)
328{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700329 struct drbd_epoch_entry *e;
330 struct page *page;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200331 unsigned nr_pages = (data_size + PAGE_SIZE -1) >> PAGE_SHIFT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700332
333 if (FAULT_ACTIVE(mdev, DRBD_FAULT_AL_EE))
334 return NULL;
335
336 e = mempool_alloc(drbd_ee_mempool, gfp_mask & ~__GFP_HIGHMEM);
337 if (!e) {
338 if (!(gfp_mask & __GFP_NOWARN))
339 dev_err(DEV, "alloc_ee: Allocation of an EE failed\n");
340 return NULL;
341 }
342
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200343 page = drbd_pp_alloc(mdev, nr_pages, (gfp_mask & __GFP_WAIT));
344 if (!page)
345 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700346
Philipp Reisnerb411b362009-09-25 16:07:19 -0700347 INIT_HLIST_NODE(&e->colision);
348 e->epoch = NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200349 e->mdev = mdev;
350 e->pages = page;
351 atomic_set(&e->pending_bios, 0);
352 e->size = data_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700353 e->flags = 0;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200354 e->sector = sector;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200355 e->block_id = id;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700356
Philipp Reisnerb411b362009-09-25 16:07:19 -0700357 return e;
358
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200359 fail:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700360 mempool_free(e, drbd_ee_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700361 return NULL;
362}
363
Lars Ellenberg435f0742010-09-06 12:30:25 +0200364void drbd_free_some_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e, int is_net)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700365{
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +0200366 if (e->flags & EE_HAS_DIGEST)
367 kfree(e->digest);
Lars Ellenberg435f0742010-09-06 12:30:25 +0200368 drbd_pp_free(mdev, e->pages, is_net);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200369 D_ASSERT(atomic_read(&e->pending_bios) == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700370 D_ASSERT(hlist_unhashed(&e->colision));
371 mempool_free(e, drbd_ee_mempool);
372}
373
374int drbd_release_ee(struct drbd_conf *mdev, struct list_head *list)
375{
376 LIST_HEAD(work_list);
377 struct drbd_epoch_entry *e, *t;
378 int count = 0;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200379 int is_net = list == &mdev->net_ee;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700380
381 spin_lock_irq(&mdev->req_lock);
382 list_splice_init(list, &work_list);
383 spin_unlock_irq(&mdev->req_lock);
384
385 list_for_each_entry_safe(e, t, &work_list, w.list) {
Lars Ellenberg435f0742010-09-06 12:30:25 +0200386 drbd_free_some_ee(mdev, e, is_net);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700387 count++;
388 }
389 return count;
390}
391
392
393/*
394 * This function is called from _asender only_
395 * but see also comments in _req_mod(,barrier_acked)
396 * and receive_Barrier.
397 *
398 * Move entries from net_ee to done_ee, if ready.
399 * Grab done_ee, call all callbacks, free the entries.
400 * The callbacks typically send out ACKs.
401 */
402static int drbd_process_done_ee(struct drbd_conf *mdev)
403{
404 LIST_HEAD(work_list);
405 LIST_HEAD(reclaimed);
406 struct drbd_epoch_entry *e, *t;
407 int ok = (mdev->state.conn >= C_WF_REPORT_PARAMS);
408
409 spin_lock_irq(&mdev->req_lock);
410 reclaim_net_ee(mdev, &reclaimed);
411 list_splice_init(&mdev->done_ee, &work_list);
412 spin_unlock_irq(&mdev->req_lock);
413
414 list_for_each_entry_safe(e, t, &reclaimed, w.list)
Lars Ellenberg435f0742010-09-06 12:30:25 +0200415 drbd_free_net_ee(mdev, e);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700416
417 /* possible callbacks here:
418 * e_end_block, and e_end_resync_block, e_send_discard_ack.
419 * all ignore the last argument.
420 */
421 list_for_each_entry_safe(e, t, &work_list, w.list) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700422 /* list_del not necessary, next/prev members not touched */
423 ok = e->w.cb(mdev, &e->w, !ok) && ok;
424 drbd_free_ee(mdev, e);
425 }
426 wake_up(&mdev->ee_wait);
427
428 return ok;
429}
430
431void _drbd_wait_ee_list_empty(struct drbd_conf *mdev, struct list_head *head)
432{
433 DEFINE_WAIT(wait);
434
435 /* avoids spin_lock/unlock
436 * and calling prepare_to_wait in the fast path */
437 while (!list_empty(head)) {
438 prepare_to_wait(&mdev->ee_wait, &wait, TASK_UNINTERRUPTIBLE);
439 spin_unlock_irq(&mdev->req_lock);
440 drbd_kick_lo(mdev);
441 schedule();
442 finish_wait(&mdev->ee_wait, &wait);
443 spin_lock_irq(&mdev->req_lock);
444 }
445}
446
447void drbd_wait_ee_list_empty(struct drbd_conf *mdev, struct list_head *head)
448{
449 spin_lock_irq(&mdev->req_lock);
450 _drbd_wait_ee_list_empty(mdev, head);
451 spin_unlock_irq(&mdev->req_lock);
452}
453
454/* see also kernel_accept; which is only present since 2.6.18.
455 * also we want to log which part of it failed, exactly */
456static int drbd_accept(struct drbd_conf *mdev, const char **what,
457 struct socket *sock, struct socket **newsock)
458{
459 struct sock *sk = sock->sk;
460 int err = 0;
461
462 *what = "listen";
463 err = sock->ops->listen(sock, 5);
464 if (err < 0)
465 goto out;
466
467 *what = "sock_create_lite";
468 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
469 newsock);
470 if (err < 0)
471 goto out;
472
473 *what = "accept";
474 err = sock->ops->accept(sock, *newsock, 0);
475 if (err < 0) {
476 sock_release(*newsock);
477 *newsock = NULL;
478 goto out;
479 }
480 (*newsock)->ops = sock->ops;
481
482out:
483 return err;
484}
485
486static int drbd_recv_short(struct drbd_conf *mdev, struct socket *sock,
487 void *buf, size_t size, int flags)
488{
489 mm_segment_t oldfs;
490 struct kvec iov = {
491 .iov_base = buf,
492 .iov_len = size,
493 };
494 struct msghdr msg = {
495 .msg_iovlen = 1,
496 .msg_iov = (struct iovec *)&iov,
497 .msg_flags = (flags ? flags : MSG_WAITALL | MSG_NOSIGNAL)
498 };
499 int rv;
500
501 oldfs = get_fs();
502 set_fs(KERNEL_DS);
503 rv = sock_recvmsg(sock, &msg, size, msg.msg_flags);
504 set_fs(oldfs);
505
506 return rv;
507}
508
509static int drbd_recv(struct drbd_conf *mdev, void *buf, size_t size)
510{
511 mm_segment_t oldfs;
512 struct kvec iov = {
513 .iov_base = buf,
514 .iov_len = size,
515 };
516 struct msghdr msg = {
517 .msg_iovlen = 1,
518 .msg_iov = (struct iovec *)&iov,
519 .msg_flags = MSG_WAITALL | MSG_NOSIGNAL
520 };
521 int rv;
522
523 oldfs = get_fs();
524 set_fs(KERNEL_DS);
525
526 for (;;) {
527 rv = sock_recvmsg(mdev->data.socket, &msg, size, msg.msg_flags);
528 if (rv == size)
529 break;
530
531 /* Note:
532 * ECONNRESET other side closed the connection
533 * ERESTARTSYS (on sock) we got a signal
534 */
535
536 if (rv < 0) {
537 if (rv == -ECONNRESET)
538 dev_info(DEV, "sock was reset by peer\n");
539 else if (rv != -ERESTARTSYS)
540 dev_err(DEV, "sock_recvmsg returned %d\n", rv);
541 break;
542 } else if (rv == 0) {
543 dev_info(DEV, "sock was shut down by peer\n");
544 break;
545 } else {
546 /* signal came in, or peer/link went down,
547 * after we read a partial message
548 */
549 /* D_ASSERT(signal_pending(current)); */
550 break;
551 }
552 };
553
554 set_fs(oldfs);
555
556 if (rv != size)
557 drbd_force_state(mdev, NS(conn, C_BROKEN_PIPE));
558
559 return rv;
560}
561
Lars Ellenberg5dbf1672010-05-25 16:18:01 +0200562/* quoting tcp(7):
563 * On individual connections, the socket buffer size must be set prior to the
564 * listen(2) or connect(2) calls in order to have it take effect.
565 * This is our wrapper to do so.
566 */
567static void drbd_setbufsize(struct socket *sock, unsigned int snd,
568 unsigned int rcv)
569{
570 /* open coded SO_SNDBUF, SO_RCVBUF */
571 if (snd) {
572 sock->sk->sk_sndbuf = snd;
573 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
574 }
575 if (rcv) {
576 sock->sk->sk_rcvbuf = rcv;
577 sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
578 }
579}
580
Philipp Reisnerb411b362009-09-25 16:07:19 -0700581static struct socket *drbd_try_connect(struct drbd_conf *mdev)
582{
583 const char *what;
584 struct socket *sock;
585 struct sockaddr_in6 src_in6;
586 int err;
587 int disconnect_on_error = 1;
588
589 if (!get_net_conf(mdev))
590 return NULL;
591
592 what = "sock_create_kern";
593 err = sock_create_kern(((struct sockaddr *)mdev->net_conf->my_addr)->sa_family,
594 SOCK_STREAM, IPPROTO_TCP, &sock);
595 if (err < 0) {
596 sock = NULL;
597 goto out;
598 }
599
600 sock->sk->sk_rcvtimeo =
601 sock->sk->sk_sndtimeo = mdev->net_conf->try_connect_int*HZ;
Lars Ellenberg5dbf1672010-05-25 16:18:01 +0200602 drbd_setbufsize(sock, mdev->net_conf->sndbuf_size,
603 mdev->net_conf->rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700604
605 /* explicitly bind to the configured IP as source IP
606 * for the outgoing connections.
607 * This is needed for multihomed hosts and to be
608 * able to use lo: interfaces for drbd.
609 * Make sure to use 0 as port number, so linux selects
610 * a free one dynamically.
611 */
612 memcpy(&src_in6, mdev->net_conf->my_addr,
613 min_t(int, mdev->net_conf->my_addr_len, sizeof(src_in6)));
614 if (((struct sockaddr *)mdev->net_conf->my_addr)->sa_family == AF_INET6)
615 src_in6.sin6_port = 0;
616 else
617 ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */
618
619 what = "bind before connect";
620 err = sock->ops->bind(sock,
621 (struct sockaddr *) &src_in6,
622 mdev->net_conf->my_addr_len);
623 if (err < 0)
624 goto out;
625
626 /* connect may fail, peer not yet available.
627 * stay C_WF_CONNECTION, don't go Disconnecting! */
628 disconnect_on_error = 0;
629 what = "connect";
630 err = sock->ops->connect(sock,
631 (struct sockaddr *)mdev->net_conf->peer_addr,
632 mdev->net_conf->peer_addr_len, 0);
633
634out:
635 if (err < 0) {
636 if (sock) {
637 sock_release(sock);
638 sock = NULL;
639 }
640 switch (-err) {
641 /* timeout, busy, signal pending */
642 case ETIMEDOUT: case EAGAIN: case EINPROGRESS:
643 case EINTR: case ERESTARTSYS:
644 /* peer not (yet) available, network problem */
645 case ECONNREFUSED: case ENETUNREACH:
646 case EHOSTDOWN: case EHOSTUNREACH:
647 disconnect_on_error = 0;
648 break;
649 default:
650 dev_err(DEV, "%s failed, err = %d\n", what, err);
651 }
652 if (disconnect_on_error)
653 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
654 }
655 put_net_conf(mdev);
656 return sock;
657}
658
659static struct socket *drbd_wait_for_connect(struct drbd_conf *mdev)
660{
661 int timeo, err;
662 struct socket *s_estab = NULL, *s_listen;
663 const char *what;
664
665 if (!get_net_conf(mdev))
666 return NULL;
667
668 what = "sock_create_kern";
669 err = sock_create_kern(((struct sockaddr *)mdev->net_conf->my_addr)->sa_family,
670 SOCK_STREAM, IPPROTO_TCP, &s_listen);
671 if (err) {
672 s_listen = NULL;
673 goto out;
674 }
675
676 timeo = mdev->net_conf->try_connect_int * HZ;
677 timeo += (random32() & 1) ? timeo / 7 : -timeo / 7; /* 28.5% random jitter */
678
679 s_listen->sk->sk_reuse = 1; /* SO_REUSEADDR */
680 s_listen->sk->sk_rcvtimeo = timeo;
681 s_listen->sk->sk_sndtimeo = timeo;
Lars Ellenberg5dbf1672010-05-25 16:18:01 +0200682 drbd_setbufsize(s_listen, mdev->net_conf->sndbuf_size,
683 mdev->net_conf->rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700684
685 what = "bind before listen";
686 err = s_listen->ops->bind(s_listen,
687 (struct sockaddr *) mdev->net_conf->my_addr,
688 mdev->net_conf->my_addr_len);
689 if (err < 0)
690 goto out;
691
692 err = drbd_accept(mdev, &what, s_listen, &s_estab);
693
694out:
695 if (s_listen)
696 sock_release(s_listen);
697 if (err < 0) {
698 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
699 dev_err(DEV, "%s failed, err = %d\n", what, err);
700 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
701 }
702 }
703 put_net_conf(mdev);
704
705 return s_estab;
706}
707
708static int drbd_send_fp(struct drbd_conf *mdev,
709 struct socket *sock, enum drbd_packets cmd)
710{
Philipp Reisner02918be2010-08-20 14:35:10 +0200711 struct p_header80 *h = &mdev->data.sbuf.header.h80;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700712
713 return _drbd_send_cmd(mdev, sock, cmd, h, sizeof(*h), 0);
714}
715
716static enum drbd_packets drbd_recv_fp(struct drbd_conf *mdev, struct socket *sock)
717{
Philipp Reisner02918be2010-08-20 14:35:10 +0200718 struct p_header80 *h = &mdev->data.rbuf.header.h80;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700719 int rr;
720
721 rr = drbd_recv_short(mdev, sock, h, sizeof(*h), 0);
722
723 if (rr == sizeof(*h) && h->magic == BE_DRBD_MAGIC)
724 return be16_to_cpu(h->command);
725
726 return 0xffff;
727}
728
729/**
730 * drbd_socket_okay() - Free the socket if its connection is not okay
731 * @mdev: DRBD device.
732 * @sock: pointer to the pointer to the socket.
733 */
734static int drbd_socket_okay(struct drbd_conf *mdev, struct socket **sock)
735{
736 int rr;
737 char tb[4];
738
739 if (!*sock)
740 return FALSE;
741
742 rr = drbd_recv_short(mdev, *sock, tb, 4, MSG_DONTWAIT | MSG_PEEK);
743
744 if (rr > 0 || rr == -EAGAIN) {
745 return TRUE;
746 } else {
747 sock_release(*sock);
748 *sock = NULL;
749 return FALSE;
750 }
751}
752
753/*
754 * return values:
755 * 1 yes, we have a valid connection
756 * 0 oops, did not work out, please try again
757 * -1 peer talks different language,
758 * no point in trying again, please go standalone.
759 * -2 We do not have a network config...
760 */
761static int drbd_connect(struct drbd_conf *mdev)
762{
763 struct socket *s, *sock, *msock;
764 int try, h, ok;
765
766 D_ASSERT(!mdev->data.socket);
767
Philipp Reisnerb411b362009-09-25 16:07:19 -0700768 if (drbd_request_state(mdev, NS(conn, C_WF_CONNECTION)) < SS_SUCCESS)
769 return -2;
770
771 clear_bit(DISCARD_CONCURRENT, &mdev->flags);
772
773 sock = NULL;
774 msock = NULL;
775
776 do {
777 for (try = 0;;) {
778 /* 3 tries, this should take less than a second! */
779 s = drbd_try_connect(mdev);
780 if (s || ++try >= 3)
781 break;
782 /* give the other side time to call bind() & listen() */
783 __set_current_state(TASK_INTERRUPTIBLE);
784 schedule_timeout(HZ / 10);
785 }
786
787 if (s) {
788 if (!sock) {
789 drbd_send_fp(mdev, s, P_HAND_SHAKE_S);
790 sock = s;
791 s = NULL;
792 } else if (!msock) {
793 drbd_send_fp(mdev, s, P_HAND_SHAKE_M);
794 msock = s;
795 s = NULL;
796 } else {
797 dev_err(DEV, "Logic error in drbd_connect()\n");
798 goto out_release_sockets;
799 }
800 }
801
802 if (sock && msock) {
803 __set_current_state(TASK_INTERRUPTIBLE);
804 schedule_timeout(HZ / 10);
805 ok = drbd_socket_okay(mdev, &sock);
806 ok = drbd_socket_okay(mdev, &msock) && ok;
807 if (ok)
808 break;
809 }
810
811retry:
812 s = drbd_wait_for_connect(mdev);
813 if (s) {
814 try = drbd_recv_fp(mdev, s);
815 drbd_socket_okay(mdev, &sock);
816 drbd_socket_okay(mdev, &msock);
817 switch (try) {
818 case P_HAND_SHAKE_S:
819 if (sock) {
820 dev_warn(DEV, "initial packet S crossed\n");
821 sock_release(sock);
822 }
823 sock = s;
824 break;
825 case P_HAND_SHAKE_M:
826 if (msock) {
827 dev_warn(DEV, "initial packet M crossed\n");
828 sock_release(msock);
829 }
830 msock = s;
831 set_bit(DISCARD_CONCURRENT, &mdev->flags);
832 break;
833 default:
834 dev_warn(DEV, "Error receiving initial packet\n");
835 sock_release(s);
836 if (random32() & 1)
837 goto retry;
838 }
839 }
840
841 if (mdev->state.conn <= C_DISCONNECTING)
842 goto out_release_sockets;
843 if (signal_pending(current)) {
844 flush_signals(current);
845 smp_rmb();
846 if (get_t_state(&mdev->receiver) == Exiting)
847 goto out_release_sockets;
848 }
849
850 if (sock && msock) {
851 ok = drbd_socket_okay(mdev, &sock);
852 ok = drbd_socket_okay(mdev, &msock) && ok;
853 if (ok)
854 break;
855 }
856 } while (1);
857
858 msock->sk->sk_reuse = 1; /* SO_REUSEADDR */
859 sock->sk->sk_reuse = 1; /* SO_REUSEADDR */
860
861 sock->sk->sk_allocation = GFP_NOIO;
862 msock->sk->sk_allocation = GFP_NOIO;
863
864 sock->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
865 msock->sk->sk_priority = TC_PRIO_INTERACTIVE;
866
Philipp Reisnerb411b362009-09-25 16:07:19 -0700867 /* NOT YET ...
868 * sock->sk->sk_sndtimeo = mdev->net_conf->timeout*HZ/10;
869 * sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
870 * first set it to the P_HAND_SHAKE timeout,
871 * which we set to 4x the configured ping_timeout. */
872 sock->sk->sk_sndtimeo =
873 sock->sk->sk_rcvtimeo = mdev->net_conf->ping_timeo*4*HZ/10;
874
875 msock->sk->sk_sndtimeo = mdev->net_conf->timeout*HZ/10;
876 msock->sk->sk_rcvtimeo = mdev->net_conf->ping_int*HZ;
877
878 /* we don't want delays.
879 * we use TCP_CORK where apropriate, though */
880 drbd_tcp_nodelay(sock);
881 drbd_tcp_nodelay(msock);
882
883 mdev->data.socket = sock;
884 mdev->meta.socket = msock;
885 mdev->last_received = jiffies;
886
887 D_ASSERT(mdev->asender.task == NULL);
888
889 h = drbd_do_handshake(mdev);
890 if (h <= 0)
891 return h;
892
893 if (mdev->cram_hmac_tfm) {
894 /* drbd_request_state(mdev, NS(conn, WFAuth)); */
Johannes Thomab10d96c2010-01-07 16:02:50 +0100895 switch (drbd_do_auth(mdev)) {
896 case -1:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700897 dev_err(DEV, "Authentication of peer failed\n");
898 return -1;
Johannes Thomab10d96c2010-01-07 16:02:50 +0100899 case 0:
900 dev_err(DEV, "Authentication of peer failed, trying again.\n");
901 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700902 }
903 }
904
905 if (drbd_request_state(mdev, NS(conn, C_WF_REPORT_PARAMS)) < SS_SUCCESS)
906 return 0;
907
908 sock->sk->sk_sndtimeo = mdev->net_conf->timeout*HZ/10;
909 sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
910
911 atomic_set(&mdev->packet_seq, 0);
912 mdev->peer_seq = 0;
913
914 drbd_thread_start(&mdev->asender);
915
Philipp Reisnerd5373382010-08-23 15:18:33 +0200916 if (mdev->agreed_pro_version < 95 && get_ldev(mdev)) {
917 drbd_setup_queue_param(mdev, DRBD_MAX_SIZE_H80_PACKET);
918 put_ldev(mdev);
919 }
920
Philipp Reisner7e2455c2010-04-22 14:50:23 +0200921 if (!drbd_send_protocol(mdev))
922 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700923 drbd_send_sync_param(mdev, &mdev->sync_conf);
Philipp Reisnere89b5912010-03-24 17:11:33 +0100924 drbd_send_sizes(mdev, 0, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700925 drbd_send_uuids(mdev);
926 drbd_send_state(mdev);
927 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
928 clear_bit(RESIZE_PENDING, &mdev->flags);
929
930 return 1;
931
932out_release_sockets:
933 if (sock)
934 sock_release(sock);
935 if (msock)
936 sock_release(msock);
937 return -1;
938}
939
Philipp Reisner02918be2010-08-20 14:35:10 +0200940static int drbd_recv_header(struct drbd_conf *mdev, enum drbd_packets *cmd, unsigned int *packet_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700941{
Philipp Reisner02918be2010-08-20 14:35:10 +0200942 union p_header *h = &mdev->data.rbuf.header;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700943 int r;
944
945 r = drbd_recv(mdev, h, sizeof(*h));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700946 if (unlikely(r != sizeof(*h))) {
947 dev_err(DEV, "short read expecting header on sock: r=%d\n", r);
948 return FALSE;
Philipp Reisner02918be2010-08-20 14:35:10 +0200949 }
950
951 if (likely(h->h80.magic == BE_DRBD_MAGIC)) {
952 *cmd = be16_to_cpu(h->h80.command);
953 *packet_size = be16_to_cpu(h->h80.length);
954 } else if (h->h95.magic == BE_DRBD_MAGIC_BIG) {
955 *cmd = be16_to_cpu(h->h95.command);
956 *packet_size = be32_to_cpu(h->h95.length);
957 } else {
Lars Ellenberg004352f2010-10-05 20:13:58 +0200958 dev_err(DEV, "magic?? on data m: 0x%08x c: %d l: %d\n",
959 be32_to_cpu(h->h80.magic),
960 be16_to_cpu(h->h80.command),
961 be16_to_cpu(h->h80.length));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700962 return FALSE;
963 }
964 mdev->last_received = jiffies;
965
966 return TRUE;
967}
968
Philipp Reisner2451fc32010-08-24 13:43:11 +0200969static void drbd_flush(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700970{
971 int rv;
972
973 if (mdev->write_ordering >= WO_bdev_flush && get_ldev(mdev)) {
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400974 rv = blkdev_issue_flush(mdev->ldev->backing_bdev, GFP_KERNEL,
975 NULL, BLKDEV_IFL_WAIT);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700976 if (rv) {
977 dev_err(DEV, "local disk flush failed with status %d\n", rv);
978 /* would rather check on EOPNOTSUPP, but that is not reliable.
979 * don't try again for ANY return value != 0
980 * if (rv == -EOPNOTSUPP) */
981 drbd_bump_write_ordering(mdev, WO_drain_io);
982 }
983 put_ldev(mdev);
984 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700985}
986
987/**
988 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
989 * @mdev: DRBD device.
990 * @epoch: Epoch object.
991 * @ev: Epoch event.
992 */
993static enum finish_epoch drbd_may_finish_epoch(struct drbd_conf *mdev,
994 struct drbd_epoch *epoch,
995 enum epoch_event ev)
996{
Philipp Reisner2451fc32010-08-24 13:43:11 +0200997 int epoch_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700998 struct drbd_epoch *next_epoch;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700999 enum finish_epoch rv = FE_STILL_LIVE;
1000
1001 spin_lock(&mdev->epoch_lock);
1002 do {
1003 next_epoch = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001004
1005 epoch_size = atomic_read(&epoch->epoch_size);
1006
1007 switch (ev & ~EV_CLEANUP) {
1008 case EV_PUT:
1009 atomic_dec(&epoch->active);
1010 break;
1011 case EV_GOT_BARRIER_NR:
1012 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001013 break;
1014 case EV_BECAME_LAST:
1015 /* nothing to do*/
1016 break;
1017 }
1018
Philipp Reisnerb411b362009-09-25 16:07:19 -07001019 if (epoch_size != 0 &&
1020 atomic_read(&epoch->active) == 0 &&
Philipp Reisner2451fc32010-08-24 13:43:11 +02001021 test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001022 if (!(ev & EV_CLEANUP)) {
1023 spin_unlock(&mdev->epoch_lock);
1024 drbd_send_b_ack(mdev, epoch->barrier_nr, epoch_size);
1025 spin_lock(&mdev->epoch_lock);
1026 }
1027 dec_unacked(mdev);
1028
1029 if (mdev->current_epoch != epoch) {
1030 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1031 list_del(&epoch->list);
1032 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
1033 mdev->epochs--;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001034 kfree(epoch);
1035
1036 if (rv == FE_STILL_LIVE)
1037 rv = FE_DESTROYED;
1038 } else {
1039 epoch->flags = 0;
1040 atomic_set(&epoch->epoch_size, 0);
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001041 /* atomic_set(&epoch->active, 0); is already zero */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001042 if (rv == FE_STILL_LIVE)
1043 rv = FE_RECYCLED;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001044 wake_up(&mdev->ee_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001045 }
1046 }
1047
1048 if (!next_epoch)
1049 break;
1050
1051 epoch = next_epoch;
1052 } while (1);
1053
1054 spin_unlock(&mdev->epoch_lock);
1055
Philipp Reisnerb411b362009-09-25 16:07:19 -07001056 return rv;
1057}
1058
1059/**
1060 * drbd_bump_write_ordering() - Fall back to an other write ordering method
1061 * @mdev: DRBD device.
1062 * @wo: Write ordering method to try.
1063 */
1064void drbd_bump_write_ordering(struct drbd_conf *mdev, enum write_ordering_e wo) __must_hold(local)
1065{
1066 enum write_ordering_e pwo;
1067 static char *write_ordering_str[] = {
1068 [WO_none] = "none",
1069 [WO_drain_io] = "drain",
1070 [WO_bdev_flush] = "flush",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001071 };
1072
1073 pwo = mdev->write_ordering;
1074 wo = min(pwo, wo);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001075 if (wo == WO_bdev_flush && mdev->ldev->dc.no_disk_flush)
1076 wo = WO_drain_io;
1077 if (wo == WO_drain_io && mdev->ldev->dc.no_disk_drain)
1078 wo = WO_none;
1079 mdev->write_ordering = wo;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001080 if (pwo != mdev->write_ordering || wo == WO_bdev_flush)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001081 dev_info(DEV, "Method to ensure write ordering: %s\n", write_ordering_str[mdev->write_ordering]);
1082}
1083
1084/**
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001085 * drbd_submit_ee()
1086 * @mdev: DRBD device.
1087 * @e: epoch entry
1088 * @rw: flag field, see bio->bi_rw
1089 */
1090/* TODO allocate from our own bio_set. */
1091int drbd_submit_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e,
1092 const unsigned rw, const int fault_type)
1093{
1094 struct bio *bios = NULL;
1095 struct bio *bio;
1096 struct page *page = e->pages;
1097 sector_t sector = e->sector;
1098 unsigned ds = e->size;
1099 unsigned n_bios = 0;
1100 unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
1101
1102 /* In most cases, we will only need one bio. But in case the lower
1103 * level restrictions happen to be different at this offset on this
1104 * side than those of the sending peer, we may need to submit the
1105 * request in more than one bio. */
1106next_bio:
1107 bio = bio_alloc(GFP_NOIO, nr_pages);
1108 if (!bio) {
1109 dev_err(DEV, "submit_ee: Allocation of a bio failed\n");
1110 goto fail;
1111 }
1112 /* > e->sector, unless this is the first bio */
1113 bio->bi_sector = sector;
1114 bio->bi_bdev = mdev->ldev->backing_bdev;
1115 /* we special case some flags in the multi-bio case, see below
Philipp Reisner2451fc32010-08-24 13:43:11 +02001116 * (REQ_UNPLUG) */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001117 bio->bi_rw = rw;
1118 bio->bi_private = e;
1119 bio->bi_end_io = drbd_endio_sec;
1120
1121 bio->bi_next = bios;
1122 bios = bio;
1123 ++n_bios;
1124
1125 page_chain_for_each(page) {
1126 unsigned len = min_t(unsigned, ds, PAGE_SIZE);
1127 if (!bio_add_page(bio, page, len, 0)) {
1128 /* a single page must always be possible! */
1129 BUG_ON(bio->bi_vcnt == 0);
1130 goto next_bio;
1131 }
1132 ds -= len;
1133 sector += len >> 9;
1134 --nr_pages;
1135 }
1136 D_ASSERT(page == NULL);
1137 D_ASSERT(ds == 0);
1138
1139 atomic_set(&e->pending_bios, n_bios);
1140 do {
1141 bio = bios;
1142 bios = bios->bi_next;
1143 bio->bi_next = NULL;
1144
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001145 /* strip off REQ_UNPLUG unless it is the last bio */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001146 if (bios)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001147 bio->bi_rw &= ~REQ_UNPLUG;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001148
1149 drbd_generic_make_request(mdev, fault_type, bio);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001150 } while (bios);
1151 maybe_kick_lo(mdev);
1152 return 0;
1153
1154fail:
1155 while (bios) {
1156 bio = bios;
1157 bios = bios->bi_next;
1158 bio_put(bio);
1159 }
1160 return -ENOMEM;
1161}
1162
Philipp Reisner02918be2010-08-20 14:35:10 +02001163static int receive_Barrier(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001164{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001165 int rv;
Philipp Reisner02918be2010-08-20 14:35:10 +02001166 struct p_barrier *p = &mdev->data.rbuf.barrier;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001167 struct drbd_epoch *epoch;
1168
Philipp Reisnerb411b362009-09-25 16:07:19 -07001169 inc_unacked(mdev);
1170
1171 if (mdev->net_conf->wire_protocol != DRBD_PROT_C)
1172 drbd_kick_lo(mdev);
1173
1174 mdev->current_epoch->barrier_nr = p->barrier;
1175 rv = drbd_may_finish_epoch(mdev, mdev->current_epoch, EV_GOT_BARRIER_NR);
1176
1177 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
1178 * the activity log, which means it would not be resynced in case the
1179 * R_PRIMARY crashes now.
1180 * Therefore we must send the barrier_ack after the barrier request was
1181 * completed. */
1182 switch (mdev->write_ordering) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001183 case WO_none:
1184 if (rv == FE_RECYCLED)
1185 return TRUE;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001186
1187 /* receiver context, in the writeout path of the other node.
1188 * avoid potential distributed deadlock */
1189 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1190 if (epoch)
1191 break;
1192 else
1193 dev_warn(DEV, "Allocation of an epoch failed, slowing down\n");
1194 /* Fall through */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001195
1196 case WO_bdev_flush:
1197 case WO_drain_io:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001198 drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
Philipp Reisner2451fc32010-08-24 13:43:11 +02001199 drbd_flush(mdev);
1200
1201 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1202 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1203 if (epoch)
1204 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001205 }
1206
Philipp Reisner2451fc32010-08-24 13:43:11 +02001207 epoch = mdev->current_epoch;
1208 wait_event(mdev->ee_wait, atomic_read(&epoch->epoch_size) == 0);
1209
1210 D_ASSERT(atomic_read(&epoch->active) == 0);
1211 D_ASSERT(epoch->flags == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001212
1213 return TRUE;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001214 default:
1215 dev_err(DEV, "Strangeness in mdev->write_ordering %d\n", mdev->write_ordering);
1216 return FALSE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001217 }
1218
1219 epoch->flags = 0;
1220 atomic_set(&epoch->epoch_size, 0);
1221 atomic_set(&epoch->active, 0);
1222
1223 spin_lock(&mdev->epoch_lock);
1224 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1225 list_add(&epoch->list, &mdev->current_epoch->list);
1226 mdev->current_epoch = epoch;
1227 mdev->epochs++;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001228 } else {
1229 /* The current_epoch got recycled while we allocated this one... */
1230 kfree(epoch);
1231 }
1232 spin_unlock(&mdev->epoch_lock);
1233
1234 return TRUE;
1235}
1236
1237/* used from receive_RSDataReply (recv_resync_read)
1238 * and from receive_Data */
1239static struct drbd_epoch_entry *
1240read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector, int data_size) __must_hold(local)
1241{
Lars Ellenberg66660322010-04-06 12:15:04 +02001242 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001243 struct drbd_epoch_entry *e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001244 struct page *page;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001245 int dgs, ds, rr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001246 void *dig_in = mdev->int_dig_in;
1247 void *dig_vv = mdev->int_dig_vv;
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001248 unsigned long *data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001249
1250 dgs = (mdev->agreed_pro_version >= 87 && mdev->integrity_r_tfm) ?
1251 crypto_hash_digestsize(mdev->integrity_r_tfm) : 0;
1252
1253 if (dgs) {
1254 rr = drbd_recv(mdev, dig_in, dgs);
1255 if (rr != dgs) {
1256 dev_warn(DEV, "short read receiving data digest: read %d expected %d\n",
1257 rr, dgs);
1258 return NULL;
1259 }
1260 }
1261
1262 data_size -= dgs;
1263
1264 ERR_IF(data_size & 0x1ff) return NULL;
1265 ERR_IF(data_size > DRBD_MAX_SEGMENT_SIZE) return NULL;
1266
Lars Ellenberg66660322010-04-06 12:15:04 +02001267 /* even though we trust out peer,
1268 * we sometimes have to double check. */
1269 if (sector + (data_size>>9) > capacity) {
1270 dev_err(DEV, "capacity: %llus < sector: %llus + size: %u\n",
1271 (unsigned long long)capacity,
1272 (unsigned long long)sector, data_size);
1273 return NULL;
1274 }
1275
Philipp Reisnerb411b362009-09-25 16:07:19 -07001276 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1277 * "criss-cross" setup, that might cause write-out on some other DRBD,
1278 * which in turn might block on the other node at this very place. */
1279 e = drbd_alloc_ee(mdev, id, sector, data_size, GFP_NOIO);
1280 if (!e)
1281 return NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001282
Philipp Reisnerb411b362009-09-25 16:07:19 -07001283 ds = data_size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001284 page = e->pages;
1285 page_chain_for_each(page) {
1286 unsigned len = min_t(int, ds, PAGE_SIZE);
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001287 data = kmap(page);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001288 rr = drbd_recv(mdev, data, len);
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001289 if (FAULT_ACTIVE(mdev, DRBD_FAULT_RECEIVE)) {
1290 dev_err(DEV, "Fault injection: Corrupting data on receive\n");
1291 data[0] = data[0] ^ (unsigned long)-1;
1292 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001293 kunmap(page);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001294 if (rr != len) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001295 drbd_free_ee(mdev, e);
1296 dev_warn(DEV, "short read receiving data: read %d expected %d\n",
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001297 rr, len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001298 return NULL;
1299 }
1300 ds -= rr;
1301 }
1302
1303 if (dgs) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001304 drbd_csum_ee(mdev, mdev->integrity_r_tfm, e, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001305 if (memcmp(dig_in, dig_vv, dgs)) {
1306 dev_err(DEV, "Digest integrity check FAILED.\n");
1307 drbd_bcast_ee(mdev, "digest failed",
1308 dgs, dig_in, dig_vv, e);
1309 drbd_free_ee(mdev, e);
1310 return NULL;
1311 }
1312 }
1313 mdev->recv_cnt += data_size>>9;
1314 return e;
1315}
1316
1317/* drbd_drain_block() just takes a data block
1318 * out of the socket input buffer, and discards it.
1319 */
1320static int drbd_drain_block(struct drbd_conf *mdev, int data_size)
1321{
1322 struct page *page;
1323 int rr, rv = 1;
1324 void *data;
1325
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001326 if (!data_size)
1327 return TRUE;
1328
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001329 page = drbd_pp_alloc(mdev, 1, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001330
1331 data = kmap(page);
1332 while (data_size) {
1333 rr = drbd_recv(mdev, data, min_t(int, data_size, PAGE_SIZE));
1334 if (rr != min_t(int, data_size, PAGE_SIZE)) {
1335 rv = 0;
1336 dev_warn(DEV, "short read receiving data: read %d expected %d\n",
1337 rr, min_t(int, data_size, PAGE_SIZE));
1338 break;
1339 }
1340 data_size -= rr;
1341 }
1342 kunmap(page);
Lars Ellenberg435f0742010-09-06 12:30:25 +02001343 drbd_pp_free(mdev, page, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001344 return rv;
1345}
1346
1347static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req,
1348 sector_t sector, int data_size)
1349{
1350 struct bio_vec *bvec;
1351 struct bio *bio;
1352 int dgs, rr, i, expect;
1353 void *dig_in = mdev->int_dig_in;
1354 void *dig_vv = mdev->int_dig_vv;
1355
1356 dgs = (mdev->agreed_pro_version >= 87 && mdev->integrity_r_tfm) ?
1357 crypto_hash_digestsize(mdev->integrity_r_tfm) : 0;
1358
1359 if (dgs) {
1360 rr = drbd_recv(mdev, dig_in, dgs);
1361 if (rr != dgs) {
1362 dev_warn(DEV, "short read receiving data reply digest: read %d expected %d\n",
1363 rr, dgs);
1364 return 0;
1365 }
1366 }
1367
1368 data_size -= dgs;
1369
1370 /* optimistically update recv_cnt. if receiving fails below,
1371 * we disconnect anyways, and counters will be reset. */
1372 mdev->recv_cnt += data_size>>9;
1373
1374 bio = req->master_bio;
1375 D_ASSERT(sector == bio->bi_sector);
1376
1377 bio_for_each_segment(bvec, bio, i) {
1378 expect = min_t(int, data_size, bvec->bv_len);
1379 rr = drbd_recv(mdev,
1380 kmap(bvec->bv_page)+bvec->bv_offset,
1381 expect);
1382 kunmap(bvec->bv_page);
1383 if (rr != expect) {
1384 dev_warn(DEV, "short read receiving data reply: "
1385 "read %d expected %d\n",
1386 rr, expect);
1387 return 0;
1388 }
1389 data_size -= rr;
1390 }
1391
1392 if (dgs) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001393 drbd_csum_bio(mdev, mdev->integrity_r_tfm, bio, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001394 if (memcmp(dig_in, dig_vv, dgs)) {
1395 dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n");
1396 return 0;
1397 }
1398 }
1399
1400 D_ASSERT(data_size == 0);
1401 return 1;
1402}
1403
1404/* e_end_resync_block() is called via
1405 * drbd_process_done_ee() by asender only */
1406static int e_end_resync_block(struct drbd_conf *mdev, struct drbd_work *w, int unused)
1407{
1408 struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w;
1409 sector_t sector = e->sector;
1410 int ok;
1411
1412 D_ASSERT(hlist_unhashed(&e->colision));
1413
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001414 if (likely((e->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001415 drbd_set_in_sync(mdev, sector, e->size);
1416 ok = drbd_send_ack(mdev, P_RS_WRITE_ACK, e);
1417 } else {
1418 /* Record failure to sync */
1419 drbd_rs_failed_io(mdev, sector, e->size);
1420
1421 ok = drbd_send_ack(mdev, P_NEG_ACK, e);
1422 }
1423 dec_unacked(mdev);
1424
1425 return ok;
1426}
1427
1428static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local)
1429{
1430 struct drbd_epoch_entry *e;
1431
1432 e = read_in_block(mdev, ID_SYNCER, sector, data_size);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001433 if (!e)
1434 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001435
1436 dec_rs_pending(mdev);
1437
Philipp Reisnerb411b362009-09-25 16:07:19 -07001438 inc_unacked(mdev);
1439 /* corresponding dec_unacked() in e_end_resync_block()
1440 * respective _drbd_clear_done_ee */
1441
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001442 e->w.cb = e_end_resync_block;
1443
Philipp Reisnerb411b362009-09-25 16:07:19 -07001444 spin_lock_irq(&mdev->req_lock);
1445 list_add(&e->w.list, &mdev->sync_ee);
1446 spin_unlock_irq(&mdev->req_lock);
1447
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001448 atomic_add(data_size >> 9, &mdev->rs_sect_ev);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001449 if (drbd_submit_ee(mdev, e, WRITE, DRBD_FAULT_RS_WR) == 0)
1450 return TRUE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001451
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001452 /* drbd_submit_ee currently fails for one reason only:
1453 * not being able to allocate enough bios.
1454 * Is dropping the connection going to help? */
1455 spin_lock_irq(&mdev->req_lock);
1456 list_del(&e->w.list);
1457 spin_unlock_irq(&mdev->req_lock);
1458
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001459 drbd_free_ee(mdev, e);
1460fail:
1461 put_ldev(mdev);
1462 return FALSE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001463}
1464
Philipp Reisner02918be2010-08-20 14:35:10 +02001465static int receive_DataReply(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001466{
1467 struct drbd_request *req;
1468 sector_t sector;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001469 int ok;
Philipp Reisner02918be2010-08-20 14:35:10 +02001470 struct p_data *p = &mdev->data.rbuf.data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001471
1472 sector = be64_to_cpu(p->sector);
1473
1474 spin_lock_irq(&mdev->req_lock);
1475 req = _ar_id_to_req(mdev, p->block_id, sector);
1476 spin_unlock_irq(&mdev->req_lock);
1477 if (unlikely(!req)) {
1478 dev_err(DEV, "Got a corrupt block_id/sector pair(1).\n");
1479 return FALSE;
1480 }
1481
1482 /* hlist_del(&req->colision) is done in _req_may_be_done, to avoid
1483 * special casing it there for the various failure cases.
1484 * still no race with drbd_fail_pending_reads */
1485 ok = recv_dless_read(mdev, req, sector, data_size);
1486
1487 if (ok)
1488 req_mod(req, data_received);
1489 /* else: nothing. handled from drbd_disconnect...
1490 * I don't think we may complete this just yet
1491 * in case we are "on-disconnect: freeze" */
1492
1493 return ok;
1494}
1495
Philipp Reisner02918be2010-08-20 14:35:10 +02001496static int receive_RSDataReply(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001497{
1498 sector_t sector;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001499 int ok;
Philipp Reisner02918be2010-08-20 14:35:10 +02001500 struct p_data *p = &mdev->data.rbuf.data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001501
1502 sector = be64_to_cpu(p->sector);
1503 D_ASSERT(p->block_id == ID_SYNCER);
1504
1505 if (get_ldev(mdev)) {
1506 /* data is submitted to disk within recv_resync_read.
1507 * corresponding put_ldev done below on error,
1508 * or in drbd_endio_write_sec. */
1509 ok = recv_resync_read(mdev, sector, data_size);
1510 } else {
1511 if (__ratelimit(&drbd_ratelimit_state))
1512 dev_err(DEV, "Can not write resync data to local disk.\n");
1513
1514 ok = drbd_drain_block(mdev, data_size);
1515
Lars Ellenberg2b2bf212010-10-06 11:46:55 +02001516 drbd_send_ack_dp(mdev, P_NEG_ACK, p, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001517 }
1518
Philipp Reisner778f2712010-07-06 11:14:00 +02001519 atomic_add(data_size >> 9, &mdev->rs_sect_in);
1520
Philipp Reisnerb411b362009-09-25 16:07:19 -07001521 return ok;
1522}
1523
1524/* e_end_block() is called via drbd_process_done_ee().
1525 * this means this function only runs in the asender thread
1526 */
1527static int e_end_block(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1528{
1529 struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w;
1530 sector_t sector = e->sector;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001531 int ok = 1, pcmd;
1532
Philipp Reisnerb411b362009-09-25 16:07:19 -07001533 if (mdev->net_conf->wire_protocol == DRBD_PROT_C) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001534 if (likely((e->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001535 pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
1536 mdev->state.conn <= C_PAUSED_SYNC_T &&
1537 e->flags & EE_MAY_SET_IN_SYNC) ?
1538 P_RS_WRITE_ACK : P_WRITE_ACK;
1539 ok &= drbd_send_ack(mdev, pcmd, e);
1540 if (pcmd == P_RS_WRITE_ACK)
1541 drbd_set_in_sync(mdev, sector, e->size);
1542 } else {
1543 ok = drbd_send_ack(mdev, P_NEG_ACK, e);
1544 /* we expect it to be marked out of sync anyways...
1545 * maybe assert this? */
1546 }
1547 dec_unacked(mdev);
1548 }
1549 /* we delete from the conflict detection hash _after_ we sent out the
1550 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
1551 if (mdev->net_conf->two_primaries) {
1552 spin_lock_irq(&mdev->req_lock);
1553 D_ASSERT(!hlist_unhashed(&e->colision));
1554 hlist_del_init(&e->colision);
1555 spin_unlock_irq(&mdev->req_lock);
1556 } else {
1557 D_ASSERT(hlist_unhashed(&e->colision));
1558 }
1559
1560 drbd_may_finish_epoch(mdev, e->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
1561
1562 return ok;
1563}
1564
1565static int e_send_discard_ack(struct drbd_conf *mdev, struct drbd_work *w, int unused)
1566{
1567 struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w;
1568 int ok = 1;
1569
1570 D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
1571 ok = drbd_send_ack(mdev, P_DISCARD_ACK, e);
1572
1573 spin_lock_irq(&mdev->req_lock);
1574 D_ASSERT(!hlist_unhashed(&e->colision));
1575 hlist_del_init(&e->colision);
1576 spin_unlock_irq(&mdev->req_lock);
1577
1578 dec_unacked(mdev);
1579
1580 return ok;
1581}
1582
1583/* Called from receive_Data.
1584 * Synchronize packets on sock with packets on msock.
1585 *
1586 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
1587 * packet traveling on msock, they are still processed in the order they have
1588 * been sent.
1589 *
1590 * Note: we don't care for Ack packets overtaking P_DATA packets.
1591 *
1592 * In case packet_seq is larger than mdev->peer_seq number, there are
1593 * outstanding packets on the msock. We wait for them to arrive.
1594 * In case we are the logically next packet, we update mdev->peer_seq
1595 * ourselves. Correctly handles 32bit wrap around.
1596 *
1597 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
1598 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
1599 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
1600 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
1601 *
1602 * returns 0 if we may process the packet,
1603 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
1604static int drbd_wait_peer_seq(struct drbd_conf *mdev, const u32 packet_seq)
1605{
1606 DEFINE_WAIT(wait);
1607 unsigned int p_seq;
1608 long timeout;
1609 int ret = 0;
1610 spin_lock(&mdev->peer_seq_lock);
1611 for (;;) {
1612 prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
1613 if (seq_le(packet_seq, mdev->peer_seq+1))
1614 break;
1615 if (signal_pending(current)) {
1616 ret = -ERESTARTSYS;
1617 break;
1618 }
1619 p_seq = mdev->peer_seq;
1620 spin_unlock(&mdev->peer_seq_lock);
1621 timeout = schedule_timeout(30*HZ);
1622 spin_lock(&mdev->peer_seq_lock);
1623 if (timeout == 0 && p_seq == mdev->peer_seq) {
1624 ret = -ETIMEDOUT;
1625 dev_err(DEV, "ASSERT FAILED waited 30 seconds for sequence update, forcing reconnect\n");
1626 break;
1627 }
1628 }
1629 finish_wait(&mdev->seq_wait, &wait);
1630 if (mdev->peer_seq+1 == packet_seq)
1631 mdev->peer_seq++;
1632 spin_unlock(&mdev->peer_seq_lock);
1633 return ret;
1634}
1635
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001636static unsigned long write_flags_to_bio(struct drbd_conf *mdev, u32 dpf)
1637{
1638 if (mdev->agreed_pro_version >= 95)
1639 return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
1640 (dpf & DP_UNPLUG ? REQ_UNPLUG : 0) |
1641 (dpf & DP_FUA ? REQ_FUA : 0) |
1642 (dpf & DP_FLUSH ? REQ_FUA : 0) |
1643 (dpf & DP_DISCARD ? REQ_DISCARD : 0);
1644 else
1645 return dpf & DP_RW_SYNC ? (REQ_SYNC | REQ_UNPLUG) : 0;
1646}
1647
Philipp Reisnerb411b362009-09-25 16:07:19 -07001648/* mirrored write */
Philipp Reisner02918be2010-08-20 14:35:10 +02001649static int receive_Data(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001650{
1651 sector_t sector;
1652 struct drbd_epoch_entry *e;
Philipp Reisner02918be2010-08-20 14:35:10 +02001653 struct p_data *p = &mdev->data.rbuf.data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001654 int rw = WRITE;
1655 u32 dp_flags;
1656
Philipp Reisnerb411b362009-09-25 16:07:19 -07001657 if (!get_ldev(mdev)) {
1658 if (__ratelimit(&drbd_ratelimit_state))
1659 dev_err(DEV, "Can not write mirrored data block "
1660 "to local disk.\n");
1661 spin_lock(&mdev->peer_seq_lock);
1662 if (mdev->peer_seq+1 == be32_to_cpu(p->seq_num))
1663 mdev->peer_seq++;
1664 spin_unlock(&mdev->peer_seq_lock);
1665
Lars Ellenberg2b2bf212010-10-06 11:46:55 +02001666 drbd_send_ack_dp(mdev, P_NEG_ACK, p, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001667 atomic_inc(&mdev->current_epoch->epoch_size);
1668 return drbd_drain_block(mdev, data_size);
1669 }
1670
1671 /* get_ldev(mdev) successful.
1672 * Corresponding put_ldev done either below (on various errors),
1673 * or in drbd_endio_write_sec, if we successfully submit the data at
1674 * the end of this function. */
1675
1676 sector = be64_to_cpu(p->sector);
1677 e = read_in_block(mdev, p->block_id, sector, data_size);
1678 if (!e) {
1679 put_ldev(mdev);
1680 return FALSE;
1681 }
1682
Philipp Reisnerb411b362009-09-25 16:07:19 -07001683 e->w.cb = e_end_block;
1684
1685 spin_lock(&mdev->epoch_lock);
1686 e->epoch = mdev->current_epoch;
1687 atomic_inc(&e->epoch->epoch_size);
1688 atomic_inc(&e->epoch->active);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001689 spin_unlock(&mdev->epoch_lock);
1690
1691 dp_flags = be32_to_cpu(p->dp_flags);
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001692 rw |= write_flags_to_bio(mdev, dp_flags);
1693
Philipp Reisnerb411b362009-09-25 16:07:19 -07001694 if (dp_flags & DP_MAY_SET_IN_SYNC)
1695 e->flags |= EE_MAY_SET_IN_SYNC;
1696
1697 /* I'm the receiver, I do hold a net_cnt reference. */
1698 if (!mdev->net_conf->two_primaries) {
1699 spin_lock_irq(&mdev->req_lock);
1700 } else {
1701 /* don't get the req_lock yet,
1702 * we may sleep in drbd_wait_peer_seq */
1703 const int size = e->size;
1704 const int discard = test_bit(DISCARD_CONCURRENT, &mdev->flags);
1705 DEFINE_WAIT(wait);
1706 struct drbd_request *i;
1707 struct hlist_node *n;
1708 struct hlist_head *slot;
1709 int first;
1710
1711 D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
1712 BUG_ON(mdev->ee_hash == NULL);
1713 BUG_ON(mdev->tl_hash == NULL);
1714
1715 /* conflict detection and handling:
1716 * 1. wait on the sequence number,
1717 * in case this data packet overtook ACK packets.
1718 * 2. check our hash tables for conflicting requests.
1719 * we only need to walk the tl_hash, since an ee can not
1720 * have a conflict with an other ee: on the submitting
1721 * node, the corresponding req had already been conflicting,
1722 * and a conflicting req is never sent.
1723 *
1724 * Note: for two_primaries, we are protocol C,
1725 * so there cannot be any request that is DONE
1726 * but still on the transfer log.
1727 *
1728 * unconditionally add to the ee_hash.
1729 *
1730 * if no conflicting request is found:
1731 * submit.
1732 *
1733 * if any conflicting request is found
1734 * that has not yet been acked,
1735 * AND I have the "discard concurrent writes" flag:
1736 * queue (via done_ee) the P_DISCARD_ACK; OUT.
1737 *
1738 * if any conflicting request is found:
1739 * block the receiver, waiting on misc_wait
1740 * until no more conflicting requests are there,
1741 * or we get interrupted (disconnect).
1742 *
1743 * we do not just write after local io completion of those
1744 * requests, but only after req is done completely, i.e.
1745 * we wait for the P_DISCARD_ACK to arrive!
1746 *
1747 * then proceed normally, i.e. submit.
1748 */
1749 if (drbd_wait_peer_seq(mdev, be32_to_cpu(p->seq_num)))
1750 goto out_interrupted;
1751
1752 spin_lock_irq(&mdev->req_lock);
1753
1754 hlist_add_head(&e->colision, ee_hash_slot(mdev, sector));
1755
1756#define OVERLAPS overlaps(i->sector, i->size, sector, size)
1757 slot = tl_hash_slot(mdev, sector);
1758 first = 1;
1759 for (;;) {
1760 int have_unacked = 0;
1761 int have_conflict = 0;
1762 prepare_to_wait(&mdev->misc_wait, &wait,
1763 TASK_INTERRUPTIBLE);
1764 hlist_for_each_entry(i, n, slot, colision) {
1765 if (OVERLAPS) {
1766 /* only ALERT on first iteration,
1767 * we may be woken up early... */
1768 if (first)
1769 dev_alert(DEV, "%s[%u] Concurrent local write detected!"
1770 " new: %llus +%u; pending: %llus +%u\n",
1771 current->comm, current->pid,
1772 (unsigned long long)sector, size,
1773 (unsigned long long)i->sector, i->size);
1774 if (i->rq_state & RQ_NET_PENDING)
1775 ++have_unacked;
1776 ++have_conflict;
1777 }
1778 }
1779#undef OVERLAPS
1780 if (!have_conflict)
1781 break;
1782
1783 /* Discard Ack only for the _first_ iteration */
1784 if (first && discard && have_unacked) {
1785 dev_alert(DEV, "Concurrent write! [DISCARD BY FLAG] sec=%llus\n",
1786 (unsigned long long)sector);
1787 inc_unacked(mdev);
1788 e->w.cb = e_send_discard_ack;
1789 list_add_tail(&e->w.list, &mdev->done_ee);
1790
1791 spin_unlock_irq(&mdev->req_lock);
1792
1793 /* we could probably send that P_DISCARD_ACK ourselves,
1794 * but I don't like the receiver using the msock */
1795
1796 put_ldev(mdev);
1797 wake_asender(mdev);
1798 finish_wait(&mdev->misc_wait, &wait);
1799 return TRUE;
1800 }
1801
1802 if (signal_pending(current)) {
1803 hlist_del_init(&e->colision);
1804
1805 spin_unlock_irq(&mdev->req_lock);
1806
1807 finish_wait(&mdev->misc_wait, &wait);
1808 goto out_interrupted;
1809 }
1810
1811 spin_unlock_irq(&mdev->req_lock);
1812 if (first) {
1813 first = 0;
1814 dev_alert(DEV, "Concurrent write! [W AFTERWARDS] "
1815 "sec=%llus\n", (unsigned long long)sector);
1816 } else if (discard) {
1817 /* we had none on the first iteration.
1818 * there must be none now. */
1819 D_ASSERT(have_unacked == 0);
1820 }
1821 schedule();
1822 spin_lock_irq(&mdev->req_lock);
1823 }
1824 finish_wait(&mdev->misc_wait, &wait);
1825 }
1826
1827 list_add(&e->w.list, &mdev->active_ee);
1828 spin_unlock_irq(&mdev->req_lock);
1829
1830 switch (mdev->net_conf->wire_protocol) {
1831 case DRBD_PROT_C:
1832 inc_unacked(mdev);
1833 /* corresponding dec_unacked() in e_end_block()
1834 * respective _drbd_clear_done_ee */
1835 break;
1836 case DRBD_PROT_B:
1837 /* I really don't like it that the receiver thread
1838 * sends on the msock, but anyways */
1839 drbd_send_ack(mdev, P_RECV_ACK, e);
1840 break;
1841 case DRBD_PROT_A:
1842 /* nothing to do */
1843 break;
1844 }
1845
Lars Ellenberg6719fb02010-10-18 23:04:07 +02001846 if (mdev->state.pdsk < D_INCONSISTENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001847 /* In case we have the only disk of the cluster, */
1848 drbd_set_out_of_sync(mdev, e->sector, e->size);
1849 e->flags |= EE_CALL_AL_COMPLETE_IO;
Lars Ellenberg6719fb02010-10-18 23:04:07 +02001850 e->flags &= ~EE_MAY_SET_IN_SYNC;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001851 drbd_al_begin_io(mdev, e->sector);
1852 }
1853
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001854 if (drbd_submit_ee(mdev, e, rw, DRBD_FAULT_DT_WR) == 0)
1855 return TRUE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001856
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001857 /* drbd_submit_ee currently fails for one reason only:
1858 * not being able to allocate enough bios.
1859 * Is dropping the connection going to help? */
1860 spin_lock_irq(&mdev->req_lock);
1861 list_del(&e->w.list);
1862 hlist_del_init(&e->colision);
1863 spin_unlock_irq(&mdev->req_lock);
1864 if (e->flags & EE_CALL_AL_COMPLETE_IO)
1865 drbd_al_complete_io(mdev, e->sector);
1866
Philipp Reisnerb411b362009-09-25 16:07:19 -07001867out_interrupted:
1868 /* yes, the epoch_size now is imbalanced.
1869 * but we drop the connection anyways, so we don't have a chance to
1870 * receive a barrier... atomic_inc(&mdev->epoch_size); */
1871 put_ldev(mdev);
1872 drbd_free_ee(mdev, e);
1873 return FALSE;
1874}
1875
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001876/* We may throttle resync, if the lower device seems to be busy,
1877 * and current sync rate is above c_min_rate.
1878 *
1879 * To decide whether or not the lower device is busy, we use a scheme similar
1880 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
1881 * (more than 64 sectors) of activity we cannot account for with our own resync
1882 * activity, it obviously is "busy".
1883 *
1884 * The current sync rate used here uses only the most recent two step marks,
1885 * to have a short time average so we can react faster.
1886 */
1887int drbd_rs_should_slow_down(struct drbd_conf *mdev)
1888{
1889 struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
1890 unsigned long db, dt, dbdt;
1891 int curr_events;
1892 int throttle = 0;
1893
1894 /* feature disabled? */
1895 if (mdev->sync_conf.c_min_rate == 0)
1896 return 0;
1897
1898 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
1899 (int)part_stat_read(&disk->part0, sectors[1]) -
1900 atomic_read(&mdev->rs_sect_ev);
1901 if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
1902 unsigned long rs_left;
1903 int i;
1904
1905 mdev->rs_last_events = curr_events;
1906
1907 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
1908 * approx. */
1909 i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-2) % DRBD_SYNC_MARKS;
1910 rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
1911
1912 dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
1913 if (!dt)
1914 dt++;
1915 db = mdev->rs_mark_left[i] - rs_left;
1916 dbdt = Bit2KB(db/dt);
1917
1918 if (dbdt > mdev->sync_conf.c_min_rate)
1919 throttle = 1;
1920 }
1921 return throttle;
1922}
1923
1924
Philipp Reisner02918be2010-08-20 14:35:10 +02001925static int receive_DataRequest(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int digest_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001926{
1927 sector_t sector;
1928 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
1929 struct drbd_epoch_entry *e;
1930 struct digest_info *di = NULL;
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02001931 int size, verb;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001932 unsigned int fault_type;
Philipp Reisner02918be2010-08-20 14:35:10 +02001933 struct p_block_req *p = &mdev->data.rbuf.block_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001934
1935 sector = be64_to_cpu(p->sector);
1936 size = be32_to_cpu(p->blksize);
1937
1938 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_SEGMENT_SIZE) {
1939 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
1940 (unsigned long long)sector, size);
1941 return FALSE;
1942 }
1943 if (sector + (size>>9) > capacity) {
1944 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
1945 (unsigned long long)sector, size);
1946 return FALSE;
1947 }
1948
1949 if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02001950 verb = 1;
1951 switch (cmd) {
1952 case P_DATA_REQUEST:
1953 drbd_send_ack_rp(mdev, P_NEG_DREPLY, p);
1954 break;
1955 case P_RS_DATA_REQUEST:
1956 case P_CSUM_RS_REQUEST:
1957 case P_OV_REQUEST:
1958 drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p);
1959 break;
1960 case P_OV_REPLY:
1961 verb = 0;
1962 dec_rs_pending(mdev);
1963 drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC);
1964 break;
1965 default:
1966 dev_err(DEV, "unexpected command (%s) in receive_DataRequest\n",
1967 cmdname(cmd));
1968 }
1969 if (verb && __ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001970 dev_err(DEV, "Can not satisfy peer's read request, "
1971 "no local data.\n");
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02001972
Lars Ellenberga821cc42010-09-06 12:31:37 +02001973 /* drain possibly payload */
1974 return drbd_drain_block(mdev, digest_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001975 }
1976
1977 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1978 * "criss-cross" setup, that might cause write-out on some other DRBD,
1979 * which in turn might block on the other node at this very place. */
1980 e = drbd_alloc_ee(mdev, p->block_id, sector, size, GFP_NOIO);
1981 if (!e) {
1982 put_ldev(mdev);
1983 return FALSE;
1984 }
1985
Philipp Reisner02918be2010-08-20 14:35:10 +02001986 switch (cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001987 case P_DATA_REQUEST:
1988 e->w.cb = w_e_end_data_req;
1989 fault_type = DRBD_FAULT_DT_RD;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02001990 /* application IO, don't drbd_rs_begin_io */
1991 goto submit;
1992
Philipp Reisnerb411b362009-09-25 16:07:19 -07001993 case P_RS_DATA_REQUEST:
1994 e->w.cb = w_e_end_rsdata_req;
1995 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001996 break;
1997
1998 case P_OV_REPLY:
1999 case P_CSUM_RS_REQUEST:
2000 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002001 di = kmalloc(sizeof(*di) + digest_size, GFP_NOIO);
2002 if (!di)
2003 goto out_free_e;
2004
2005 di->digest_size = digest_size;
2006 di->digest = (((char *)di)+sizeof(struct digest_info));
2007
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +02002008 e->digest = di;
2009 e->flags |= EE_HAS_DIGEST;
2010
Philipp Reisnerb411b362009-09-25 16:07:19 -07002011 if (drbd_recv(mdev, di->digest, digest_size) != digest_size)
2012 goto out_free_e;
2013
Philipp Reisner02918be2010-08-20 14:35:10 +02002014 if (cmd == P_CSUM_RS_REQUEST) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002015 D_ASSERT(mdev->agreed_pro_version >= 89);
2016 e->w.cb = w_e_end_csum_rs_req;
Philipp Reisner02918be2010-08-20 14:35:10 +02002017 } else if (cmd == P_OV_REPLY) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002018 e->w.cb = w_e_end_ov_reply;
2019 dec_rs_pending(mdev);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002020 /* drbd_rs_begin_io done when we sent this request,
2021 * but accounting still needs to be done. */
2022 goto submit_for_resync;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002023 }
2024 break;
2025
2026 case P_OV_REQUEST:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002027 if (mdev->ov_start_sector == ~(sector_t)0 &&
2028 mdev->agreed_pro_version >= 90) {
2029 mdev->ov_start_sector = sector;
2030 mdev->ov_position = sector;
2031 mdev->ov_left = mdev->rs_total - BM_SECT_TO_BIT(sector);
2032 dev_info(DEV, "Online Verify start sector: %llu\n",
2033 (unsigned long long)sector);
2034 }
2035 e->w.cb = w_e_end_ov_req;
2036 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002037 break;
2038
Philipp Reisnerb411b362009-09-25 16:07:19 -07002039 default:
2040 dev_err(DEV, "unexpected command (%s) in receive_DataRequest\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02002041 cmdname(cmd));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002042 fault_type = DRBD_FAULT_MAX;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002043 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002044 }
2045
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002046 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2047 * wrt the receiver, but it is not as straightforward as it may seem.
2048 * Various places in the resync start and stop logic assume resync
2049 * requests are processed in order, requeuing this on the worker thread
2050 * introduces a bunch of new code for synchronization between threads.
2051 *
2052 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2053 * "forever", throttling after drbd_rs_begin_io will lock that extent
2054 * for application writes for the same time. For now, just throttle
2055 * here, where the rest of the code expects the receiver to sleep for
2056 * a while, anyways.
2057 */
2058
2059 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2060 * this defers syncer requests for some time, before letting at least
2061 * on request through. The resync controller on the receiving side
2062 * will adapt to the incoming rate accordingly.
2063 *
2064 * We cannot throttle here if remote is Primary/SyncTarget:
2065 * we would also throttle its application reads.
2066 * In that case, throttling is done on the SyncTarget only.
2067 */
2068 if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev))
2069 msleep(100);
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002070 if (drbd_rs_begin_io(mdev, e->sector))
2071 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002072
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002073submit_for_resync:
2074 atomic_add(size >> 9, &mdev->rs_sect_ev);
2075
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002076submit:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002077 inc_unacked(mdev);
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002078 spin_lock_irq(&mdev->req_lock);
2079 list_add_tail(&e->w.list, &mdev->read_ee);
2080 spin_unlock_irq(&mdev->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002081
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002082 if (drbd_submit_ee(mdev, e, READ, fault_type) == 0)
2083 return TRUE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002084
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002085 /* drbd_submit_ee currently fails for one reason only:
2086 * not being able to allocate enough bios.
2087 * Is dropping the connection going to help? */
2088 spin_lock_irq(&mdev->req_lock);
2089 list_del(&e->w.list);
2090 spin_unlock_irq(&mdev->req_lock);
2091 /* no drbd_rs_complete_io(), we are dropping the connection anyways */
2092
Philipp Reisnerb411b362009-09-25 16:07:19 -07002093out_free_e:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002094 put_ldev(mdev);
2095 drbd_free_ee(mdev, e);
2096 return FALSE;
2097}
2098
2099static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
2100{
2101 int self, peer, rv = -100;
2102 unsigned long ch_self, ch_peer;
2103
2104 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2105 peer = mdev->p_uuid[UI_BITMAP] & 1;
2106
2107 ch_peer = mdev->p_uuid[UI_SIZE];
2108 ch_self = mdev->comm_bm_set;
2109
2110 switch (mdev->net_conf->after_sb_0p) {
2111 case ASB_CONSENSUS:
2112 case ASB_DISCARD_SECONDARY:
2113 case ASB_CALL_HELPER:
2114 dev_err(DEV, "Configuration error.\n");
2115 break;
2116 case ASB_DISCONNECT:
2117 break;
2118 case ASB_DISCARD_YOUNGER_PRI:
2119 if (self == 0 && peer == 1) {
2120 rv = -1;
2121 break;
2122 }
2123 if (self == 1 && peer == 0) {
2124 rv = 1;
2125 break;
2126 }
2127 /* Else fall through to one of the other strategies... */
2128 case ASB_DISCARD_OLDER_PRI:
2129 if (self == 0 && peer == 1) {
2130 rv = 1;
2131 break;
2132 }
2133 if (self == 1 && peer == 0) {
2134 rv = -1;
2135 break;
2136 }
2137 /* Else fall through to one of the other strategies... */
Lars Ellenbergad19bf62009-10-14 09:36:49 +02002138 dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
Philipp Reisnerb411b362009-09-25 16:07:19 -07002139 "Using discard-least-changes instead\n");
2140 case ASB_DISCARD_ZERO_CHG:
2141 if (ch_peer == 0 && ch_self == 0) {
2142 rv = test_bit(DISCARD_CONCURRENT, &mdev->flags)
2143 ? -1 : 1;
2144 break;
2145 } else {
2146 if (ch_peer == 0) { rv = 1; break; }
2147 if (ch_self == 0) { rv = -1; break; }
2148 }
2149 if (mdev->net_conf->after_sb_0p == ASB_DISCARD_ZERO_CHG)
2150 break;
2151 case ASB_DISCARD_LEAST_CHG:
2152 if (ch_self < ch_peer)
2153 rv = -1;
2154 else if (ch_self > ch_peer)
2155 rv = 1;
2156 else /* ( ch_self == ch_peer ) */
2157 /* Well, then use something else. */
2158 rv = test_bit(DISCARD_CONCURRENT, &mdev->flags)
2159 ? -1 : 1;
2160 break;
2161 case ASB_DISCARD_LOCAL:
2162 rv = -1;
2163 break;
2164 case ASB_DISCARD_REMOTE:
2165 rv = 1;
2166 }
2167
2168 return rv;
2169}
2170
2171static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
2172{
2173 int self, peer, hg, rv = -100;
2174
2175 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2176 peer = mdev->p_uuid[UI_BITMAP] & 1;
2177
2178 switch (mdev->net_conf->after_sb_1p) {
2179 case ASB_DISCARD_YOUNGER_PRI:
2180 case ASB_DISCARD_OLDER_PRI:
2181 case ASB_DISCARD_LEAST_CHG:
2182 case ASB_DISCARD_LOCAL:
2183 case ASB_DISCARD_REMOTE:
2184 dev_err(DEV, "Configuration error.\n");
2185 break;
2186 case ASB_DISCONNECT:
2187 break;
2188 case ASB_CONSENSUS:
2189 hg = drbd_asb_recover_0p(mdev);
2190 if (hg == -1 && mdev->state.role == R_SECONDARY)
2191 rv = hg;
2192 if (hg == 1 && mdev->state.role == R_PRIMARY)
2193 rv = hg;
2194 break;
2195 case ASB_VIOLENTLY:
2196 rv = drbd_asb_recover_0p(mdev);
2197 break;
2198 case ASB_DISCARD_SECONDARY:
2199 return mdev->state.role == R_PRIMARY ? 1 : -1;
2200 case ASB_CALL_HELPER:
2201 hg = drbd_asb_recover_0p(mdev);
2202 if (hg == -1 && mdev->state.role == R_PRIMARY) {
2203 self = drbd_set_role(mdev, R_SECONDARY, 0);
2204 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2205 * we might be here in C_WF_REPORT_PARAMS which is transient.
2206 * we do not need to wait for the after state change work either. */
2207 self = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2208 if (self != SS_SUCCESS) {
2209 drbd_khelper(mdev, "pri-lost-after-sb");
2210 } else {
2211 dev_warn(DEV, "Successfully gave up primary role.\n");
2212 rv = hg;
2213 }
2214 } else
2215 rv = hg;
2216 }
2217
2218 return rv;
2219}
2220
2221static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
2222{
2223 int self, peer, hg, rv = -100;
2224
2225 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2226 peer = mdev->p_uuid[UI_BITMAP] & 1;
2227
2228 switch (mdev->net_conf->after_sb_2p) {
2229 case ASB_DISCARD_YOUNGER_PRI:
2230 case ASB_DISCARD_OLDER_PRI:
2231 case ASB_DISCARD_LEAST_CHG:
2232 case ASB_DISCARD_LOCAL:
2233 case ASB_DISCARD_REMOTE:
2234 case ASB_CONSENSUS:
2235 case ASB_DISCARD_SECONDARY:
2236 dev_err(DEV, "Configuration error.\n");
2237 break;
2238 case ASB_VIOLENTLY:
2239 rv = drbd_asb_recover_0p(mdev);
2240 break;
2241 case ASB_DISCONNECT:
2242 break;
2243 case ASB_CALL_HELPER:
2244 hg = drbd_asb_recover_0p(mdev);
2245 if (hg == -1) {
2246 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2247 * we might be here in C_WF_REPORT_PARAMS which is transient.
2248 * we do not need to wait for the after state change work either. */
2249 self = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2250 if (self != SS_SUCCESS) {
2251 drbd_khelper(mdev, "pri-lost-after-sb");
2252 } else {
2253 dev_warn(DEV, "Successfully gave up primary role.\n");
2254 rv = hg;
2255 }
2256 } else
2257 rv = hg;
2258 }
2259
2260 return rv;
2261}
2262
2263static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
2264 u64 bits, u64 flags)
2265{
2266 if (!uuid) {
2267 dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
2268 return;
2269 }
2270 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
2271 text,
2272 (unsigned long long)uuid[UI_CURRENT],
2273 (unsigned long long)uuid[UI_BITMAP],
2274 (unsigned long long)uuid[UI_HISTORY_START],
2275 (unsigned long long)uuid[UI_HISTORY_END],
2276 (unsigned long long)bits,
2277 (unsigned long long)flags);
2278}
2279
2280/*
2281 100 after split brain try auto recover
2282 2 C_SYNC_SOURCE set BitMap
2283 1 C_SYNC_SOURCE use BitMap
2284 0 no Sync
2285 -1 C_SYNC_TARGET use BitMap
2286 -2 C_SYNC_TARGET set BitMap
2287 -100 after split brain, disconnect
2288-1000 unrelated data
2289 */
2290static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
2291{
2292 u64 self, peer;
2293 int i, j;
2294
2295 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2296 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2297
2298 *rule_nr = 10;
2299 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
2300 return 0;
2301
2302 *rule_nr = 20;
2303 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
2304 peer != UUID_JUST_CREATED)
2305 return -2;
2306
2307 *rule_nr = 30;
2308 if (self != UUID_JUST_CREATED &&
2309 (peer == UUID_JUST_CREATED || peer == (u64)0))
2310 return 2;
2311
2312 if (self == peer) {
2313 int rct, dc; /* roles at crash time */
2314
2315 if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
2316
2317 if (mdev->agreed_pro_version < 91)
2318 return -1001;
2319
2320 if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
2321 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
2322 dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
2323 drbd_uuid_set_bm(mdev, 0UL);
2324
2325 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2326 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2327 *rule_nr = 34;
2328 } else {
2329 dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
2330 *rule_nr = 36;
2331 }
2332
2333 return 1;
2334 }
2335
2336 if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
2337
2338 if (mdev->agreed_pro_version < 91)
2339 return -1001;
2340
2341 if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
2342 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
2343 dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
2344
2345 mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
2346 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
2347 mdev->p_uuid[UI_BITMAP] = 0UL;
2348
2349 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2350 *rule_nr = 35;
2351 } else {
2352 dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
2353 *rule_nr = 37;
2354 }
2355
2356 return -1;
2357 }
2358
2359 /* Common power [off|failure] */
2360 rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
2361 (mdev->p_uuid[UI_FLAGS] & 2);
2362 /* lowest bit is set when we were primary,
2363 * next bit (weight 2) is set when peer was primary */
2364 *rule_nr = 40;
2365
2366 switch (rct) {
2367 case 0: /* !self_pri && !peer_pri */ return 0;
2368 case 1: /* self_pri && !peer_pri */ return 1;
2369 case 2: /* !self_pri && peer_pri */ return -1;
2370 case 3: /* self_pri && peer_pri */
2371 dc = test_bit(DISCARD_CONCURRENT, &mdev->flags);
2372 return dc ? -1 : 1;
2373 }
2374 }
2375
2376 *rule_nr = 50;
2377 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2378 if (self == peer)
2379 return -1;
2380
2381 *rule_nr = 51;
2382 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2383 if (self == peer) {
2384 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2385 peer = mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1);
2386 if (self == peer) {
2387 /* The last P_SYNC_UUID did not get though. Undo the last start of
2388 resync as sync source modifications of the peer's UUIDs. */
2389
2390 if (mdev->agreed_pro_version < 91)
2391 return -1001;
2392
2393 mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
2394 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
2395 return -1;
2396 }
2397 }
2398
2399 *rule_nr = 60;
2400 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2401 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2402 peer = mdev->p_uuid[i] & ~((u64)1);
2403 if (self == peer)
2404 return -2;
2405 }
2406
2407 *rule_nr = 70;
2408 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2409 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2410 if (self == peer)
2411 return 1;
2412
2413 *rule_nr = 71;
2414 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2415 if (self == peer) {
2416 self = mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1);
2417 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2418 if (self == peer) {
2419 /* The last P_SYNC_UUID did not get though. Undo the last start of
2420 resync as sync source modifications of our UUIDs. */
2421
2422 if (mdev->agreed_pro_version < 91)
2423 return -1001;
2424
2425 _drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
2426 _drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
2427
2428 dev_info(DEV, "Undid last start of resync:\n");
2429
2430 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2431 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2432
2433 return 1;
2434 }
2435 }
2436
2437
2438 *rule_nr = 80;
Philipp Reisnerd8c2a362009-11-18 15:52:51 +01002439 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002440 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2441 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2442 if (self == peer)
2443 return 2;
2444 }
2445
2446 *rule_nr = 90;
2447 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2448 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2449 if (self == peer && self != ((u64)0))
2450 return 100;
2451
2452 *rule_nr = 100;
2453 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2454 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2455 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
2456 peer = mdev->p_uuid[j] & ~((u64)1);
2457 if (self == peer)
2458 return -100;
2459 }
2460 }
2461
2462 return -1000;
2463}
2464
2465/* drbd_sync_handshake() returns the new conn state on success, or
2466 CONN_MASK (-1) on failure.
2467 */
2468static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
2469 enum drbd_disk_state peer_disk) __must_hold(local)
2470{
2471 int hg, rule_nr;
2472 enum drbd_conns rv = C_MASK;
2473 enum drbd_disk_state mydisk;
2474
2475 mydisk = mdev->state.disk;
2476 if (mydisk == D_NEGOTIATING)
2477 mydisk = mdev->new_state_tmp.disk;
2478
2479 dev_info(DEV, "drbd_sync_handshake:\n");
2480 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0);
2481 drbd_uuid_dump(mdev, "peer", mdev->p_uuid,
2482 mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2483
2484 hg = drbd_uuid_compare(mdev, &rule_nr);
2485
2486 dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
2487
2488 if (hg == -1000) {
2489 dev_alert(DEV, "Unrelated data, aborting!\n");
2490 return C_MASK;
2491 }
2492 if (hg == -1001) {
2493 dev_alert(DEV, "To resolve this both sides have to support at least protocol\n");
2494 return C_MASK;
2495 }
2496
2497 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
2498 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
2499 int f = (hg == -100) || abs(hg) == 2;
2500 hg = mydisk > D_INCONSISTENT ? 1 : -1;
2501 if (f)
2502 hg = hg*2;
2503 dev_info(DEV, "Becoming sync %s due to disk states.\n",
2504 hg > 0 ? "source" : "target");
2505 }
2506
Adam Gandelman3a11a482010-04-08 16:48:23 -07002507 if (abs(hg) == 100)
2508 drbd_khelper(mdev, "initial-split-brain");
2509
Philipp Reisnerb411b362009-09-25 16:07:19 -07002510 if (hg == 100 || (hg == -100 && mdev->net_conf->always_asbp)) {
2511 int pcount = (mdev->state.role == R_PRIMARY)
2512 + (peer_role == R_PRIMARY);
2513 int forced = (hg == -100);
2514
2515 switch (pcount) {
2516 case 0:
2517 hg = drbd_asb_recover_0p(mdev);
2518 break;
2519 case 1:
2520 hg = drbd_asb_recover_1p(mdev);
2521 break;
2522 case 2:
2523 hg = drbd_asb_recover_2p(mdev);
2524 break;
2525 }
2526 if (abs(hg) < 100) {
2527 dev_warn(DEV, "Split-Brain detected, %d primaries, "
2528 "automatically solved. Sync from %s node\n",
2529 pcount, (hg < 0) ? "peer" : "this");
2530 if (forced) {
2531 dev_warn(DEV, "Doing a full sync, since"
2532 " UUIDs where ambiguous.\n");
2533 hg = hg*2;
2534 }
2535 }
2536 }
2537
2538 if (hg == -100) {
2539 if (mdev->net_conf->want_lose && !(mdev->p_uuid[UI_FLAGS]&1))
2540 hg = -1;
2541 if (!mdev->net_conf->want_lose && (mdev->p_uuid[UI_FLAGS]&1))
2542 hg = 1;
2543
2544 if (abs(hg) < 100)
2545 dev_warn(DEV, "Split-Brain detected, manually solved. "
2546 "Sync from %s node\n",
2547 (hg < 0) ? "peer" : "this");
2548 }
2549
2550 if (hg == -100) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01002551 /* FIXME this log message is not correct if we end up here
2552 * after an attempted attach on a diskless node.
2553 * We just refuse to attach -- well, we drop the "connection"
2554 * to that disk, in a way... */
Adam Gandelman3a11a482010-04-08 16:48:23 -07002555 dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002556 drbd_khelper(mdev, "split-brain");
2557 return C_MASK;
2558 }
2559
2560 if (hg > 0 && mydisk <= D_INCONSISTENT) {
2561 dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
2562 return C_MASK;
2563 }
2564
2565 if (hg < 0 && /* by intention we do not use mydisk here. */
2566 mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
2567 switch (mdev->net_conf->rr_conflict) {
2568 case ASB_CALL_HELPER:
2569 drbd_khelper(mdev, "pri-lost");
2570 /* fall through */
2571 case ASB_DISCONNECT:
2572 dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
2573 return C_MASK;
2574 case ASB_VIOLENTLY:
2575 dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
2576 "assumption\n");
2577 }
2578 }
2579
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002580 if (mdev->net_conf->dry_run || test_bit(CONN_DRY_RUN, &mdev->flags)) {
2581 if (hg == 0)
2582 dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
2583 else
2584 dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
2585 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
2586 abs(hg) >= 2 ? "full" : "bit-map based");
2587 return C_MASK;
2588 }
2589
Philipp Reisnerb411b362009-09-25 16:07:19 -07002590 if (abs(hg) >= 2) {
2591 dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
2592 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake"))
2593 return C_MASK;
2594 }
2595
2596 if (hg > 0) { /* become sync source. */
2597 rv = C_WF_BITMAP_S;
2598 } else if (hg < 0) { /* become sync target */
2599 rv = C_WF_BITMAP_T;
2600 } else {
2601 rv = C_CONNECTED;
2602 if (drbd_bm_total_weight(mdev)) {
2603 dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
2604 drbd_bm_total_weight(mdev));
2605 }
2606 }
2607
2608 return rv;
2609}
2610
2611/* returns 1 if invalid */
2612static int cmp_after_sb(enum drbd_after_sb_p peer, enum drbd_after_sb_p self)
2613{
2614 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
2615 if ((peer == ASB_DISCARD_REMOTE && self == ASB_DISCARD_LOCAL) ||
2616 (self == ASB_DISCARD_REMOTE && peer == ASB_DISCARD_LOCAL))
2617 return 0;
2618
2619 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
2620 if (peer == ASB_DISCARD_REMOTE || peer == ASB_DISCARD_LOCAL ||
2621 self == ASB_DISCARD_REMOTE || self == ASB_DISCARD_LOCAL)
2622 return 1;
2623
2624 /* everything else is valid if they are equal on both sides. */
2625 if (peer == self)
2626 return 0;
2627
2628 /* everything es is invalid. */
2629 return 1;
2630}
2631
Philipp Reisner02918be2010-08-20 14:35:10 +02002632static int receive_protocol(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002633{
Philipp Reisner02918be2010-08-20 14:35:10 +02002634 struct p_protocol *p = &mdev->data.rbuf.protocol;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002635 int p_proto, p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002636 int p_want_lose, p_two_primaries, cf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002637 char p_integrity_alg[SHARED_SECRET_MAX] = "";
2638
Philipp Reisnerb411b362009-09-25 16:07:19 -07002639 p_proto = be32_to_cpu(p->protocol);
2640 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
2641 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
2642 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002643 p_two_primaries = be32_to_cpu(p->two_primaries);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002644 cf = be32_to_cpu(p->conn_flags);
2645 p_want_lose = cf & CF_WANT_LOSE;
2646
2647 clear_bit(CONN_DRY_RUN, &mdev->flags);
2648
2649 if (cf & CF_DRY_RUN)
2650 set_bit(CONN_DRY_RUN, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002651
2652 if (p_proto != mdev->net_conf->wire_protocol) {
2653 dev_err(DEV, "incompatible communication protocols\n");
2654 goto disconnect;
2655 }
2656
2657 if (cmp_after_sb(p_after_sb_0p, mdev->net_conf->after_sb_0p)) {
2658 dev_err(DEV, "incompatible after-sb-0pri settings\n");
2659 goto disconnect;
2660 }
2661
2662 if (cmp_after_sb(p_after_sb_1p, mdev->net_conf->after_sb_1p)) {
2663 dev_err(DEV, "incompatible after-sb-1pri settings\n");
2664 goto disconnect;
2665 }
2666
2667 if (cmp_after_sb(p_after_sb_2p, mdev->net_conf->after_sb_2p)) {
2668 dev_err(DEV, "incompatible after-sb-2pri settings\n");
2669 goto disconnect;
2670 }
2671
2672 if (p_want_lose && mdev->net_conf->want_lose) {
2673 dev_err(DEV, "both sides have the 'want_lose' flag set\n");
2674 goto disconnect;
2675 }
2676
2677 if (p_two_primaries != mdev->net_conf->two_primaries) {
2678 dev_err(DEV, "incompatible setting of the two-primaries options\n");
2679 goto disconnect;
2680 }
2681
2682 if (mdev->agreed_pro_version >= 87) {
2683 unsigned char *my_alg = mdev->net_conf->integrity_alg;
2684
2685 if (drbd_recv(mdev, p_integrity_alg, data_size) != data_size)
2686 return FALSE;
2687
2688 p_integrity_alg[SHARED_SECRET_MAX-1] = 0;
2689 if (strcmp(p_integrity_alg, my_alg)) {
2690 dev_err(DEV, "incompatible setting of the data-integrity-alg\n");
2691 goto disconnect;
2692 }
2693 dev_info(DEV, "data-integrity-alg: %s\n",
2694 my_alg[0] ? my_alg : (unsigned char *)"<not-used>");
2695 }
2696
2697 return TRUE;
2698
2699disconnect:
2700 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
2701 return FALSE;
2702}
2703
2704/* helper function
2705 * input: alg name, feature name
2706 * return: NULL (alg name was "")
2707 * ERR_PTR(error) if something goes wrong
2708 * or the crypto hash ptr, if it worked out ok. */
2709struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
2710 const char *alg, const char *name)
2711{
2712 struct crypto_hash *tfm;
2713
2714 if (!alg[0])
2715 return NULL;
2716
2717 tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
2718 if (IS_ERR(tfm)) {
2719 dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
2720 alg, name, PTR_ERR(tfm));
2721 return tfm;
2722 }
2723 if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) {
2724 crypto_free_hash(tfm);
2725 dev_err(DEV, "\"%s\" is not a digest (%s)\n", alg, name);
2726 return ERR_PTR(-EINVAL);
2727 }
2728 return tfm;
2729}
2730
Philipp Reisner02918be2010-08-20 14:35:10 +02002731static int receive_SyncParam(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int packet_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002732{
2733 int ok = TRUE;
Philipp Reisner02918be2010-08-20 14:35:10 +02002734 struct p_rs_param_95 *p = &mdev->data.rbuf.rs_param_95;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002735 unsigned int header_size, data_size, exp_max_sz;
2736 struct crypto_hash *verify_tfm = NULL;
2737 struct crypto_hash *csums_tfm = NULL;
2738 const int apv = mdev->agreed_pro_version;
Philipp Reisner778f2712010-07-06 11:14:00 +02002739 int *rs_plan_s = NULL;
2740 int fifo_size = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002741
2742 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
2743 : apv == 88 ? sizeof(struct p_rs_param)
2744 + SHARED_SECRET_MAX
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002745 : apv <= 94 ? sizeof(struct p_rs_param_89)
2746 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002747
Philipp Reisner02918be2010-08-20 14:35:10 +02002748 if (packet_size > exp_max_sz) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002749 dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02002750 packet_size, exp_max_sz);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002751 return FALSE;
2752 }
2753
2754 if (apv <= 88) {
Philipp Reisner02918be2010-08-20 14:35:10 +02002755 header_size = sizeof(struct p_rs_param) - sizeof(struct p_header80);
2756 data_size = packet_size - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002757 } else if (apv <= 94) {
Philipp Reisner02918be2010-08-20 14:35:10 +02002758 header_size = sizeof(struct p_rs_param_89) - sizeof(struct p_header80);
2759 data_size = packet_size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002760 D_ASSERT(data_size == 0);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002761 } else {
Philipp Reisner02918be2010-08-20 14:35:10 +02002762 header_size = sizeof(struct p_rs_param_95) - sizeof(struct p_header80);
2763 data_size = packet_size - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002764 D_ASSERT(data_size == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002765 }
2766
2767 /* initialize verify_alg and csums_alg */
2768 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
2769
Philipp Reisner02918be2010-08-20 14:35:10 +02002770 if (drbd_recv(mdev, &p->head.payload, header_size) != header_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002771 return FALSE;
2772
2773 mdev->sync_conf.rate = be32_to_cpu(p->rate);
2774
2775 if (apv >= 88) {
2776 if (apv == 88) {
2777 if (data_size > SHARED_SECRET_MAX) {
2778 dev_err(DEV, "verify-alg too long, "
2779 "peer wants %u, accepting only %u byte\n",
2780 data_size, SHARED_SECRET_MAX);
2781 return FALSE;
2782 }
2783
2784 if (drbd_recv(mdev, p->verify_alg, data_size) != data_size)
2785 return FALSE;
2786
2787 /* we expect NUL terminated string */
2788 /* but just in case someone tries to be evil */
2789 D_ASSERT(p->verify_alg[data_size-1] == 0);
2790 p->verify_alg[data_size-1] = 0;
2791
2792 } else /* apv >= 89 */ {
2793 /* we still expect NUL terminated strings */
2794 /* but just in case someone tries to be evil */
2795 D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
2796 D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
2797 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
2798 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
2799 }
2800
2801 if (strcmp(mdev->sync_conf.verify_alg, p->verify_alg)) {
2802 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
2803 dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
2804 mdev->sync_conf.verify_alg, p->verify_alg);
2805 goto disconnect;
2806 }
2807 verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
2808 p->verify_alg, "verify-alg");
2809 if (IS_ERR(verify_tfm)) {
2810 verify_tfm = NULL;
2811 goto disconnect;
2812 }
2813 }
2814
2815 if (apv >= 89 && strcmp(mdev->sync_conf.csums_alg, p->csums_alg)) {
2816 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
2817 dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
2818 mdev->sync_conf.csums_alg, p->csums_alg);
2819 goto disconnect;
2820 }
2821 csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
2822 p->csums_alg, "csums-alg");
2823 if (IS_ERR(csums_tfm)) {
2824 csums_tfm = NULL;
2825 goto disconnect;
2826 }
2827 }
2828
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002829 if (apv > 94) {
2830 mdev->sync_conf.rate = be32_to_cpu(p->rate);
2831 mdev->sync_conf.c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
2832 mdev->sync_conf.c_delay_target = be32_to_cpu(p->c_delay_target);
2833 mdev->sync_conf.c_fill_target = be32_to_cpu(p->c_fill_target);
2834 mdev->sync_conf.c_max_rate = be32_to_cpu(p->c_max_rate);
Philipp Reisner778f2712010-07-06 11:14:00 +02002835
2836 fifo_size = (mdev->sync_conf.c_plan_ahead * 10 * SLEEP_TIME) / HZ;
2837 if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) {
2838 rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL);
2839 if (!rs_plan_s) {
2840 dev_err(DEV, "kmalloc of fifo_buffer failed");
2841 goto disconnect;
2842 }
2843 }
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002844 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002845
2846 spin_lock(&mdev->peer_seq_lock);
2847 /* lock against drbd_nl_syncer_conf() */
2848 if (verify_tfm) {
2849 strcpy(mdev->sync_conf.verify_alg, p->verify_alg);
2850 mdev->sync_conf.verify_alg_len = strlen(p->verify_alg) + 1;
2851 crypto_free_hash(mdev->verify_tfm);
2852 mdev->verify_tfm = verify_tfm;
2853 dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
2854 }
2855 if (csums_tfm) {
2856 strcpy(mdev->sync_conf.csums_alg, p->csums_alg);
2857 mdev->sync_conf.csums_alg_len = strlen(p->csums_alg) + 1;
2858 crypto_free_hash(mdev->csums_tfm);
2859 mdev->csums_tfm = csums_tfm;
2860 dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
2861 }
Philipp Reisner778f2712010-07-06 11:14:00 +02002862 if (fifo_size != mdev->rs_plan_s.size) {
2863 kfree(mdev->rs_plan_s.values);
2864 mdev->rs_plan_s.values = rs_plan_s;
2865 mdev->rs_plan_s.size = fifo_size;
2866 mdev->rs_planed = 0;
2867 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002868 spin_unlock(&mdev->peer_seq_lock);
2869 }
2870
2871 return ok;
2872disconnect:
2873 /* just for completeness: actually not needed,
2874 * as this is not reached if csums_tfm was ok. */
2875 crypto_free_hash(csums_tfm);
2876 /* but free the verify_tfm again, if csums_tfm did not work out */
2877 crypto_free_hash(verify_tfm);
2878 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
2879 return FALSE;
2880}
2881
2882static void drbd_setup_order_type(struct drbd_conf *mdev, int peer)
2883{
2884 /* sorry, we currently have no working implementation
2885 * of distributed TCQ */
2886}
2887
2888/* warn if the arguments differ by more than 12.5% */
2889static void warn_if_differ_considerably(struct drbd_conf *mdev,
2890 const char *s, sector_t a, sector_t b)
2891{
2892 sector_t d;
2893 if (a == 0 || b == 0)
2894 return;
2895 d = (a > b) ? (a - b) : (b - a);
2896 if (d > (a>>3) || d > (b>>3))
2897 dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
2898 (unsigned long long)a, (unsigned long long)b);
2899}
2900
Philipp Reisner02918be2010-08-20 14:35:10 +02002901static int receive_sizes(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002902{
Philipp Reisner02918be2010-08-20 14:35:10 +02002903 struct p_sizes *p = &mdev->data.rbuf.sizes;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002904 enum determine_dev_size dd = unchanged;
2905 unsigned int max_seg_s;
2906 sector_t p_size, p_usize, my_usize;
2907 int ldsc = 0; /* local disk size changed */
Philipp Reisnere89b5912010-03-24 17:11:33 +01002908 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002909
Philipp Reisnerb411b362009-09-25 16:07:19 -07002910 p_size = be64_to_cpu(p->d_size);
2911 p_usize = be64_to_cpu(p->u_size);
2912
2913 if (p_size == 0 && mdev->state.disk == D_DISKLESS) {
2914 dev_err(DEV, "some backing storage is needed\n");
2915 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
2916 return FALSE;
2917 }
2918
2919 /* just store the peer's disk size for now.
2920 * we still need to figure out whether we accept that. */
2921 mdev->p_size = p_size;
2922
2923#define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
2924 if (get_ldev(mdev)) {
2925 warn_if_differ_considerably(mdev, "lower level device sizes",
2926 p_size, drbd_get_max_capacity(mdev->ldev));
2927 warn_if_differ_considerably(mdev, "user requested size",
2928 p_usize, mdev->ldev->dc.disk_size);
2929
2930 /* if this is the first connect, or an otherwise expected
2931 * param exchange, choose the minimum */
2932 if (mdev->state.conn == C_WF_REPORT_PARAMS)
2933 p_usize = min_not_zero((sector_t)mdev->ldev->dc.disk_size,
2934 p_usize);
2935
2936 my_usize = mdev->ldev->dc.disk_size;
2937
2938 if (mdev->ldev->dc.disk_size != p_usize) {
2939 mdev->ldev->dc.disk_size = p_usize;
2940 dev_info(DEV, "Peer sets u_size to %lu sectors\n",
2941 (unsigned long)mdev->ldev->dc.disk_size);
2942 }
2943
2944 /* Never shrink a device with usable data during connect.
2945 But allow online shrinking if we are connected. */
Philipp Reisnera393db62009-12-22 13:35:52 +01002946 if (drbd_new_dev_size(mdev, mdev->ldev, 0) <
Philipp Reisnerb411b362009-09-25 16:07:19 -07002947 drbd_get_capacity(mdev->this_bdev) &&
2948 mdev->state.disk >= D_OUTDATED &&
2949 mdev->state.conn < C_CONNECTED) {
2950 dev_err(DEV, "The peer's disk size is too small!\n");
2951 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
2952 mdev->ldev->dc.disk_size = my_usize;
2953 put_ldev(mdev);
2954 return FALSE;
2955 }
2956 put_ldev(mdev);
2957 }
2958#undef min_not_zero
2959
Philipp Reisnere89b5912010-03-24 17:11:33 +01002960 ddsf = be16_to_cpu(p->dds_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002961 if (get_ldev(mdev)) {
Philipp Reisnere89b5912010-03-24 17:11:33 +01002962 dd = drbd_determin_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002963 put_ldev(mdev);
2964 if (dd == dev_size_error)
2965 return FALSE;
2966 drbd_md_sync(mdev);
2967 } else {
2968 /* I am diskless, need to accept the peer's size. */
2969 drbd_set_my_capacity(mdev, p_size);
2970 }
2971
Philipp Reisnerb411b362009-09-25 16:07:19 -07002972 if (get_ldev(mdev)) {
2973 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
2974 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
2975 ldsc = 1;
2976 }
2977
Lars Ellenberga1c88d02010-05-14 19:16:41 +02002978 if (mdev->agreed_pro_version < 94)
2979 max_seg_s = be32_to_cpu(p->max_segment_size);
Lars Ellenberg8979d9c2010-09-14 15:56:29 +02002980 else if (mdev->agreed_pro_version == 94)
2981 max_seg_s = DRBD_MAX_SIZE_H80_PACKET;
Lars Ellenberga1c88d02010-05-14 19:16:41 +02002982 else /* drbd 8.3.8 onwards */
2983 max_seg_s = DRBD_MAX_SEGMENT_SIZE;
2984
Philipp Reisnerb411b362009-09-25 16:07:19 -07002985 if (max_seg_s != queue_max_segment_size(mdev->rq_queue))
2986 drbd_setup_queue_param(mdev, max_seg_s);
2987
Philipp Reisnere89b5912010-03-24 17:11:33 +01002988 drbd_setup_order_type(mdev, be16_to_cpu(p->queue_order_type));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002989 put_ldev(mdev);
2990 }
2991
2992 if (mdev->state.conn > C_WF_REPORT_PARAMS) {
2993 if (be64_to_cpu(p->c_size) !=
2994 drbd_get_capacity(mdev->this_bdev) || ldsc) {
2995 /* we have different sizes, probably peer
2996 * needs to know my new size... */
Philipp Reisnere89b5912010-03-24 17:11:33 +01002997 drbd_send_sizes(mdev, 0, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002998 }
2999 if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
3000 (dd == grew && mdev->state.conn == C_CONNECTED)) {
3001 if (mdev->state.pdsk >= D_INCONSISTENT &&
Philipp Reisnere89b5912010-03-24 17:11:33 +01003002 mdev->state.disk >= D_INCONSISTENT) {
3003 if (ddsf & DDSF_NO_RESYNC)
3004 dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
3005 else
3006 resync_after_online_grow(mdev);
3007 } else
Philipp Reisnerb411b362009-09-25 16:07:19 -07003008 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
3009 }
3010 }
3011
3012 return TRUE;
3013}
3014
Philipp Reisner02918be2010-08-20 14:35:10 +02003015static int receive_uuids(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003016{
Philipp Reisner02918be2010-08-20 14:35:10 +02003017 struct p_uuids *p = &mdev->data.rbuf.uuids;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003018 u64 *p_uuid;
3019 int i;
3020
Philipp Reisnerb411b362009-09-25 16:07:19 -07003021 p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
3022
3023 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
3024 p_uuid[i] = be64_to_cpu(p->uuid[i]);
3025
3026 kfree(mdev->p_uuid);
3027 mdev->p_uuid = p_uuid;
3028
3029 if (mdev->state.conn < C_CONNECTED &&
3030 mdev->state.disk < D_INCONSISTENT &&
3031 mdev->state.role == R_PRIMARY &&
3032 (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
3033 dev_err(DEV, "Can only connect to data with current UUID=%016llX\n",
3034 (unsigned long long)mdev->ed_uuid);
3035 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
3036 return FALSE;
3037 }
3038
3039 if (get_ldev(mdev)) {
3040 int skip_initial_sync =
3041 mdev->state.conn == C_CONNECTED &&
3042 mdev->agreed_pro_version >= 90 &&
3043 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
3044 (p_uuid[UI_FLAGS] & 8);
3045 if (skip_initial_sync) {
3046 dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n");
3047 drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
3048 "clear_n_write from receive_uuids");
3049 _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]);
3050 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3051 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3052 CS_VERBOSE, NULL);
3053 drbd_md_sync(mdev);
3054 }
3055 put_ldev(mdev);
Philipp Reisner18a50fa2010-06-21 14:14:15 +02003056 } else if (mdev->state.disk < D_INCONSISTENT &&
3057 mdev->state.role == R_PRIMARY) {
3058 /* I am a diskless primary, the peer just created a new current UUID
3059 for me. */
3060 drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003061 }
3062
3063 /* Before we test for the disk state, we should wait until an eventually
3064 ongoing cluster wide state change is finished. That is important if
3065 we are primary and are detaching from our disk. We need to see the
3066 new disk state... */
3067 wait_event(mdev->misc_wait, !test_bit(CLUSTER_ST_CHANGE, &mdev->flags));
3068 if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT)
3069 drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
3070
3071 return TRUE;
3072}
3073
3074/**
3075 * convert_state() - Converts the peer's view of the cluster state to our point of view
3076 * @ps: The state as seen by the peer.
3077 */
3078static union drbd_state convert_state(union drbd_state ps)
3079{
3080 union drbd_state ms;
3081
3082 static enum drbd_conns c_tab[] = {
3083 [C_CONNECTED] = C_CONNECTED,
3084
3085 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
3086 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
3087 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
3088 [C_VERIFY_S] = C_VERIFY_T,
3089 [C_MASK] = C_MASK,
3090 };
3091
3092 ms.i = ps.i;
3093
3094 ms.conn = c_tab[ps.conn];
3095 ms.peer = ps.role;
3096 ms.role = ps.peer;
3097 ms.pdsk = ps.disk;
3098 ms.disk = ps.pdsk;
3099 ms.peer_isp = (ps.aftr_isp | ps.user_isp);
3100
3101 return ms;
3102}
3103
Philipp Reisner02918be2010-08-20 14:35:10 +02003104static int receive_req_state(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003105{
Philipp Reisner02918be2010-08-20 14:35:10 +02003106 struct p_req_state *p = &mdev->data.rbuf.req_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003107 union drbd_state mask, val;
3108 int rv;
3109
Philipp Reisnerb411b362009-09-25 16:07:19 -07003110 mask.i = be32_to_cpu(p->mask);
3111 val.i = be32_to_cpu(p->val);
3112
3113 if (test_bit(DISCARD_CONCURRENT, &mdev->flags) &&
3114 test_bit(CLUSTER_ST_CHANGE, &mdev->flags)) {
3115 drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG);
3116 return TRUE;
3117 }
3118
3119 mask = convert_state(mask);
3120 val = convert_state(val);
3121
3122 rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
3123
3124 drbd_send_sr_reply(mdev, rv);
3125 drbd_md_sync(mdev);
3126
3127 return TRUE;
3128}
3129
Philipp Reisner02918be2010-08-20 14:35:10 +02003130static int receive_state(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003131{
Philipp Reisner02918be2010-08-20 14:35:10 +02003132 struct p_state *p = &mdev->data.rbuf.state;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003133 union drbd_state os, ns, peer_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003134 enum drbd_disk_state real_peer_disk;
Philipp Reisner65d922c2010-06-16 16:18:09 +02003135 enum chg_state_flags cs_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003136 int rv;
3137
Philipp Reisnerb411b362009-09-25 16:07:19 -07003138 peer_state.i = be32_to_cpu(p->state);
3139
3140 real_peer_disk = peer_state.disk;
3141 if (peer_state.disk == D_NEGOTIATING) {
3142 real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
3143 dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
3144 }
3145
3146 spin_lock_irq(&mdev->req_lock);
3147 retry:
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003148 os = ns = mdev->state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003149 spin_unlock_irq(&mdev->req_lock);
3150
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003151 /* peer says his disk is uptodate, while we think it is inconsistent,
3152 * and this happens while we think we have a sync going on. */
3153 if (os.pdsk == D_INCONSISTENT && real_peer_disk == D_UP_TO_DATE &&
3154 os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) {
3155 /* If we are (becoming) SyncSource, but peer is still in sync
3156 * preparation, ignore its uptodate-ness to avoid flapping, it
3157 * will change to inconsistent once the peer reaches active
3158 * syncing states.
3159 * It may have changed syncer-paused flags, however, so we
3160 * cannot ignore this completely. */
3161 if (peer_state.conn > C_CONNECTED &&
3162 peer_state.conn < C_SYNC_SOURCE)
3163 real_peer_disk = D_INCONSISTENT;
3164
3165 /* if peer_state changes to connected at the same time,
3166 * it explicitly notifies us that it finished resync.
3167 * Maybe we should finish it up, too? */
3168 else if (os.conn >= C_SYNC_SOURCE &&
3169 peer_state.conn == C_CONNECTED) {
3170 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed)
3171 drbd_resync_finished(mdev);
3172 return TRUE;
3173 }
3174 }
3175
3176 /* peer says his disk is inconsistent, while we think it is uptodate,
3177 * and this happens while the peer still thinks we have a sync going on,
3178 * but we think we are already done with the sync.
3179 * We ignore this to avoid flapping pdsk.
3180 * This should not happen, if the peer is a recent version of drbd. */
3181 if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT &&
3182 os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE)
3183 real_peer_disk = D_UP_TO_DATE;
3184
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003185 if (ns.conn == C_WF_REPORT_PARAMS)
3186 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003187
3188 if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING &&
3189 get_ldev_if_state(mdev, D_NEGOTIATING)) {
3190 int cr; /* consider resync */
3191
3192 /* if we established a new connection */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003193 cr = (os.conn < C_CONNECTED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003194 /* if we had an established connection
3195 * and one of the nodes newly attaches a disk */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003196 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003197 (peer_state.disk == D_NEGOTIATING ||
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003198 os.disk == D_NEGOTIATING));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003199 /* if we have both been inconsistent, and the peer has been
3200 * forced to be UpToDate with --overwrite-data */
3201 cr |= test_bit(CONSIDER_RESYNC, &mdev->flags);
3202 /* if we had been plain connected, and the admin requested to
3203 * start a sync by "invalidate" or "invalidate-remote" */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003204 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003205 (peer_state.conn >= C_STARTING_SYNC_S &&
3206 peer_state.conn <= C_WF_BITMAP_T));
3207
3208 if (cr)
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003209 ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003210
3211 put_ldev(mdev);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003212 if (ns.conn == C_MASK) {
3213 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003214 if (mdev->state.disk == D_NEGOTIATING) {
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003215 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003216 } else if (peer_state.disk == D_NEGOTIATING) {
3217 dev_err(DEV, "Disk attach process on the peer node was aborted.\n");
3218 peer_state.disk = D_DISKLESS;
Lars Ellenberg580b9762010-02-26 23:15:23 +01003219 real_peer_disk = D_DISKLESS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003220 } else {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003221 if (test_and_clear_bit(CONN_DRY_RUN, &mdev->flags))
3222 return FALSE;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003223 D_ASSERT(os.conn == C_WF_REPORT_PARAMS);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003224 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
3225 return FALSE;
3226 }
3227 }
3228 }
3229
3230 spin_lock_irq(&mdev->req_lock);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003231 if (mdev->state.i != os.i)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003232 goto retry;
3233 clear_bit(CONSIDER_RESYNC, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003234 ns.peer = peer_state.role;
3235 ns.pdsk = real_peer_disk;
3236 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003237 if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003238 ns.disk = mdev->new_state_tmp.disk;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003239 cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD);
3240 if (ns.pdsk == D_CONSISTENT && is_susp(ns) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED &&
Philipp Reisner481c6f52010-06-22 14:03:27 +02003241 test_bit(NEW_CUR_UUID, &mdev->flags)) {
3242 /* Do not allow tl_restart(resend) for a rebooted peer. We can only allow this
3243 for temporal network outages! */
3244 spin_unlock_irq(&mdev->req_lock);
3245 dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
3246 tl_clear(mdev);
3247 drbd_uuid_new_current(mdev);
3248 clear_bit(NEW_CUR_UUID, &mdev->flags);
3249 drbd_force_state(mdev, NS2(conn, C_PROTOCOL_ERROR, susp, 0));
3250 return FALSE;
3251 }
Philipp Reisner65d922c2010-06-16 16:18:09 +02003252 rv = _drbd_set_state(mdev, ns, cs_flags, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003253 ns = mdev->state;
3254 spin_unlock_irq(&mdev->req_lock);
3255
3256 if (rv < SS_SUCCESS) {
3257 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
3258 return FALSE;
3259 }
3260
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003261 if (os.conn > C_WF_REPORT_PARAMS) {
3262 if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003263 peer_state.disk != D_NEGOTIATING ) {
3264 /* we want resync, peer has not yet decided to sync... */
3265 /* Nowadays only used when forcing a node into primary role and
3266 setting its disk to UpToDate with that */
3267 drbd_send_uuids(mdev);
3268 drbd_send_state(mdev);
3269 }
3270 }
3271
3272 mdev->net_conf->want_lose = 0;
3273
3274 drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
3275
3276 return TRUE;
3277}
3278
Philipp Reisner02918be2010-08-20 14:35:10 +02003279static int receive_sync_uuid(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003280{
Philipp Reisner02918be2010-08-20 14:35:10 +02003281 struct p_rs_uuid *p = &mdev->data.rbuf.rs_uuid;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003282
3283 wait_event(mdev->misc_wait,
3284 mdev->state.conn == C_WF_SYNC_UUID ||
3285 mdev->state.conn < C_CONNECTED ||
3286 mdev->state.disk < D_NEGOTIATING);
3287
3288 /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */
3289
Philipp Reisnerb411b362009-09-25 16:07:19 -07003290 /* Here the _drbd_uuid_ functions are right, current should
3291 _not_ be rotated into the history */
3292 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
3293 _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid));
3294 _drbd_uuid_set(mdev, UI_BITMAP, 0UL);
3295
3296 drbd_start_resync(mdev, C_SYNC_TARGET);
3297
3298 put_ldev(mdev);
3299 } else
3300 dev_err(DEV, "Ignoring SyncUUID packet!\n");
3301
3302 return TRUE;
3303}
3304
3305enum receive_bitmap_ret { OK, DONE, FAILED };
3306
3307static enum receive_bitmap_ret
Philipp Reisner02918be2010-08-20 14:35:10 +02003308receive_bitmap_plain(struct drbd_conf *mdev, unsigned int data_size,
3309 unsigned long *buffer, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003310{
3311 unsigned num_words = min_t(size_t, BM_PACKET_WORDS, c->bm_words - c->word_offset);
3312 unsigned want = num_words * sizeof(long);
3313
Philipp Reisner02918be2010-08-20 14:35:10 +02003314 if (want != data_size) {
3315 dev_err(DEV, "%s:want (%u) != data_size (%u)\n", __func__, want, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003316 return FAILED;
3317 }
3318 if (want == 0)
3319 return DONE;
3320 if (drbd_recv(mdev, buffer, want) != want)
3321 return FAILED;
3322
3323 drbd_bm_merge_lel(mdev, c->word_offset, num_words, buffer);
3324
3325 c->word_offset += num_words;
3326 c->bit_offset = c->word_offset * BITS_PER_LONG;
3327 if (c->bit_offset > c->bm_bits)
3328 c->bit_offset = c->bm_bits;
3329
3330 return OK;
3331}
3332
3333static enum receive_bitmap_ret
3334recv_bm_rle_bits(struct drbd_conf *mdev,
3335 struct p_compressed_bm *p,
3336 struct bm_xfer_ctx *c)
3337{
3338 struct bitstream bs;
3339 u64 look_ahead;
3340 u64 rl;
3341 u64 tmp;
3342 unsigned long s = c->bit_offset;
3343 unsigned long e;
Lars Ellenberg004352f2010-10-05 20:13:58 +02003344 int len = be16_to_cpu(p->head.length) - (sizeof(*p) - sizeof(p->head));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003345 int toggle = DCBP_get_start(p);
3346 int have;
3347 int bits;
3348
3349 bitstream_init(&bs, p->code, len, DCBP_get_pad_bits(p));
3350
3351 bits = bitstream_get_bits(&bs, &look_ahead, 64);
3352 if (bits < 0)
3353 return FAILED;
3354
3355 for (have = bits; have > 0; s += rl, toggle = !toggle) {
3356 bits = vli_decode_bits(&rl, look_ahead);
3357 if (bits <= 0)
3358 return FAILED;
3359
3360 if (toggle) {
3361 e = s + rl -1;
3362 if (e >= c->bm_bits) {
3363 dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
3364 return FAILED;
3365 }
3366 _drbd_bm_set_bits(mdev, s, e);
3367 }
3368
3369 if (have < bits) {
3370 dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
3371 have, bits, look_ahead,
3372 (unsigned int)(bs.cur.b - p->code),
3373 (unsigned int)bs.buf_len);
3374 return FAILED;
3375 }
3376 look_ahead >>= bits;
3377 have -= bits;
3378
3379 bits = bitstream_get_bits(&bs, &tmp, 64 - have);
3380 if (bits < 0)
3381 return FAILED;
3382 look_ahead |= tmp << have;
3383 have += bits;
3384 }
3385
3386 c->bit_offset = s;
3387 bm_xfer_ctx_bit_to_word_offset(c);
3388
3389 return (s == c->bm_bits) ? DONE : OK;
3390}
3391
3392static enum receive_bitmap_ret
3393decode_bitmap_c(struct drbd_conf *mdev,
3394 struct p_compressed_bm *p,
3395 struct bm_xfer_ctx *c)
3396{
3397 if (DCBP_get_code(p) == RLE_VLI_Bits)
3398 return recv_bm_rle_bits(mdev, p, c);
3399
3400 /* other variants had been implemented for evaluation,
3401 * but have been dropped as this one turned out to be "best"
3402 * during all our tests. */
3403
3404 dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
3405 drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR));
3406 return FAILED;
3407}
3408
3409void INFO_bm_xfer_stats(struct drbd_conf *mdev,
3410 const char *direction, struct bm_xfer_ctx *c)
3411{
3412 /* what would it take to transfer it "plaintext" */
Philipp Reisner0b70a132010-08-20 13:36:10 +02003413 unsigned plain = sizeof(struct p_header80) *
Philipp Reisnerb411b362009-09-25 16:07:19 -07003414 ((c->bm_words+BM_PACKET_WORDS-1)/BM_PACKET_WORDS+1)
3415 + c->bm_words * sizeof(long);
3416 unsigned total = c->bytes[0] + c->bytes[1];
3417 unsigned r;
3418
3419 /* total can not be zero. but just in case: */
3420 if (total == 0)
3421 return;
3422
3423 /* don't report if not compressed */
3424 if (total >= plain)
3425 return;
3426
3427 /* total < plain. check for overflow, still */
3428 r = (total > UINT_MAX/1000) ? (total / (plain/1000))
3429 : (1000 * total / plain);
3430
3431 if (r > 1000)
3432 r = 1000;
3433
3434 r = 1000 - r;
3435 dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
3436 "total %u; compression: %u.%u%%\n",
3437 direction,
3438 c->bytes[1], c->packets[1],
3439 c->bytes[0], c->packets[0],
3440 total, r/10, r % 10);
3441}
3442
3443/* Since we are processing the bitfield from lower addresses to higher,
3444 it does not matter if the process it in 32 bit chunks or 64 bit
3445 chunks as long as it is little endian. (Understand it as byte stream,
3446 beginning with the lowest byte...) If we would use big endian
3447 we would need to process it from the highest address to the lowest,
3448 in order to be agnostic to the 32 vs 64 bits issue.
3449
3450 returns 0 on failure, 1 if we successfully received it. */
Philipp Reisner02918be2010-08-20 14:35:10 +02003451static int receive_bitmap(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003452{
3453 struct bm_xfer_ctx c;
3454 void *buffer;
3455 enum receive_bitmap_ret ret;
3456 int ok = FALSE;
Philipp Reisner02918be2010-08-20 14:35:10 +02003457 struct p_header80 *h = &mdev->data.rbuf.header.h80;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003458
3459 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
3460
3461 drbd_bm_lock(mdev, "receive bitmap");
3462
3463 /* maybe we should use some per thread scratch page,
3464 * and allocate that during initial device creation? */
3465 buffer = (unsigned long *) __get_free_page(GFP_NOIO);
3466 if (!buffer) {
3467 dev_err(DEV, "failed to allocate one page buffer in %s\n", __func__);
3468 goto out;
3469 }
3470
3471 c = (struct bm_xfer_ctx) {
3472 .bm_bits = drbd_bm_bits(mdev),
3473 .bm_words = drbd_bm_words(mdev),
3474 };
3475
3476 do {
Philipp Reisner02918be2010-08-20 14:35:10 +02003477 if (cmd == P_BITMAP) {
3478 ret = receive_bitmap_plain(mdev, data_size, buffer, &c);
3479 } else if (cmd == P_COMPRESSED_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003480 /* MAYBE: sanity check that we speak proto >= 90,
3481 * and the feature is enabled! */
3482 struct p_compressed_bm *p;
3483
Philipp Reisner02918be2010-08-20 14:35:10 +02003484 if (data_size > BM_PACKET_PAYLOAD_BYTES) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003485 dev_err(DEV, "ReportCBitmap packet too large\n");
3486 goto out;
3487 }
3488 /* use the page buff */
3489 p = buffer;
3490 memcpy(p, h, sizeof(*h));
Philipp Reisner02918be2010-08-20 14:35:10 +02003491 if (drbd_recv(mdev, p->head.payload, data_size) != data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003492 goto out;
Lars Ellenberg004352f2010-10-05 20:13:58 +02003493 if (data_size <= (sizeof(*p) - sizeof(p->head))) {
3494 dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003495 return FAILED;
3496 }
3497 ret = decode_bitmap_c(mdev, p, &c);
3498 } else {
Philipp Reisner02918be2010-08-20 14:35:10 +02003499 dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003500 goto out;
3501 }
3502
Philipp Reisner02918be2010-08-20 14:35:10 +02003503 c.packets[cmd == P_BITMAP]++;
3504 c.bytes[cmd == P_BITMAP] += sizeof(struct p_header80) + data_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003505
3506 if (ret != OK)
3507 break;
3508
Philipp Reisner02918be2010-08-20 14:35:10 +02003509 if (!drbd_recv_header(mdev, &cmd, &data_size))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003510 goto out;
3511 } while (ret == OK);
3512 if (ret == FAILED)
3513 goto out;
3514
3515 INFO_bm_xfer_stats(mdev, "receive", &c);
3516
3517 if (mdev->state.conn == C_WF_BITMAP_T) {
3518 ok = !drbd_send_bitmap(mdev);
3519 if (!ok)
3520 goto out;
3521 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
3522 ok = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
3523 D_ASSERT(ok == SS_SUCCESS);
3524 } else if (mdev->state.conn != C_WF_BITMAP_S) {
3525 /* admin may have requested C_DISCONNECTING,
3526 * other threads may have noticed network errors */
3527 dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n",
3528 drbd_conn_str(mdev->state.conn));
3529 }
3530
3531 ok = TRUE;
3532 out:
3533 drbd_bm_unlock(mdev);
3534 if (ok && mdev->state.conn == C_WF_BITMAP_S)
3535 drbd_start_resync(mdev, C_SYNC_SOURCE);
3536 free_page((unsigned long) buffer);
3537 return ok;
3538}
3539
Philipp Reisner02918be2010-08-20 14:35:10 +02003540static int receive_skip(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003541{
3542 /* TODO zero copy sink :) */
3543 static char sink[128];
3544 int size, want, r;
3545
Philipp Reisner02918be2010-08-20 14:35:10 +02003546 dev_warn(DEV, "skipping unknown optional packet type %d, l: %d!\n",
3547 cmd, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003548
Philipp Reisner02918be2010-08-20 14:35:10 +02003549 size = data_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003550 while (size > 0) {
3551 want = min_t(int, size, sizeof(sink));
3552 r = drbd_recv(mdev, sink, want);
3553 ERR_IF(r <= 0) break;
3554 size -= r;
3555 }
3556 return size == 0;
3557}
3558
Philipp Reisner02918be2010-08-20 14:35:10 +02003559static int receive_UnplugRemote(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003560{
3561 if (mdev->state.disk >= D_INCONSISTENT)
3562 drbd_kick_lo(mdev);
3563
3564 /* Make sure we've acked all the TCP data associated
3565 * with the data requests being unplugged */
3566 drbd_tcp_quickack(mdev->data.socket);
3567
3568 return TRUE;
3569}
3570
Philipp Reisner02918be2010-08-20 14:35:10 +02003571typedef int (*drbd_cmd_handler_f)(struct drbd_conf *, enum drbd_packets cmd, unsigned int to_receive);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003572
Philipp Reisner02918be2010-08-20 14:35:10 +02003573struct data_cmd {
3574 int expect_payload;
3575 size_t pkt_size;
3576 drbd_cmd_handler_f function;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003577};
3578
Philipp Reisner02918be2010-08-20 14:35:10 +02003579static struct data_cmd drbd_cmd_handler[] = {
3580 [P_DATA] = { 1, sizeof(struct p_data), receive_Data },
3581 [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply },
3582 [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } ,
3583 [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } ,
3584 [P_BITMAP] = { 1, sizeof(struct p_header80), receive_bitmap } ,
3585 [P_COMPRESSED_BITMAP] = { 1, sizeof(struct p_header80), receive_bitmap } ,
3586 [P_UNPLUG_REMOTE] = { 0, sizeof(struct p_header80), receive_UnplugRemote },
3587 [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
3588 [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
3589 [P_SYNC_PARAM] = { 1, sizeof(struct p_header80), receive_SyncParam },
3590 [P_SYNC_PARAM89] = { 1, sizeof(struct p_header80), receive_SyncParam },
3591 [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol },
3592 [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids },
3593 [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes },
3594 [P_STATE] = { 0, sizeof(struct p_state), receive_state },
3595 [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state },
3596 [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid },
3597 [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
3598 [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest },
3599 [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest },
3600 [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip },
3601 /* anything missing from this table is in
3602 * the asender_tbl, see get_asender_cmd */
3603 [P_MAX_CMD] = { 0, 0, NULL },
3604};
3605
3606/* All handler functions that expect a sub-header get that sub-heder in
3607 mdev->data.rbuf.header.head.payload.
3608
3609 Usually in mdev->data.rbuf.header.head the callback can find the usual
3610 p_header, but they may not rely on that. Since there is also p_header95 !
3611 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003612
3613static void drbdd(struct drbd_conf *mdev)
3614{
Philipp Reisner02918be2010-08-20 14:35:10 +02003615 union p_header *header = &mdev->data.rbuf.header;
3616 unsigned int packet_size;
3617 enum drbd_packets cmd;
3618 size_t shs; /* sub header size */
3619 int rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003620
3621 while (get_t_state(&mdev->receiver) == Running) {
3622 drbd_thread_current_set_cpu(mdev);
Philipp Reisner02918be2010-08-20 14:35:10 +02003623 if (!drbd_recv_header(mdev, &cmd, &packet_size))
3624 goto err_out;
3625
3626 if (unlikely(cmd >= P_MAX_CMD || !drbd_cmd_handler[cmd].function)) {
3627 dev_err(DEV, "unknown packet type %d, l: %d!\n", cmd, packet_size);
3628 goto err_out;
Lars Ellenberg0b33a912009-11-16 15:58:04 +01003629 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003630
Philipp Reisner02918be2010-08-20 14:35:10 +02003631 shs = drbd_cmd_handler[cmd].pkt_size - sizeof(union p_header);
3632 rv = drbd_recv(mdev, &header->h80.payload, shs);
3633 if (unlikely(rv != shs)) {
3634 dev_err(DEV, "short read while reading sub header: rv=%d\n", rv);
3635 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003636 }
Philipp Reisner02918be2010-08-20 14:35:10 +02003637
3638 if (packet_size - shs > 0 && !drbd_cmd_handler[cmd].expect_payload) {
3639 dev_err(DEV, "No payload expected %s l:%d\n", cmdname(cmd), packet_size);
3640 goto err_out;
3641 }
3642
3643 rv = drbd_cmd_handler[cmd].function(mdev, cmd, packet_size - shs);
3644
3645 if (unlikely(!rv)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003646 dev_err(DEV, "error receiving %s, l: %d!\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02003647 cmdname(cmd), packet_size);
3648 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003649 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003650 }
Philipp Reisner02918be2010-08-20 14:35:10 +02003651
3652 if (0) {
3653 err_out:
3654 drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR));
3655 }
Lars Ellenberg856c50c2010-10-14 13:37:40 +02003656 /* If we leave here, we probably want to update at least the
3657 * "Connected" indicator on stable storage. Do so explicitly here. */
3658 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003659}
3660
Philipp Reisnerb411b362009-09-25 16:07:19 -07003661void drbd_flush_workqueue(struct drbd_conf *mdev)
3662{
3663 struct drbd_wq_barrier barr;
3664
3665 barr.w.cb = w_prev_work_done;
3666 init_completion(&barr.done);
3667 drbd_queue_work(&mdev->data.work, &barr.w);
3668 wait_for_completion(&barr.done);
3669}
3670
Philipp Reisnerf70b35112010-06-24 14:34:40 +02003671void drbd_free_tl_hash(struct drbd_conf *mdev)
3672{
3673 struct hlist_head *h;
3674
3675 spin_lock_irq(&mdev->req_lock);
3676
3677 if (!mdev->tl_hash || mdev->state.conn != C_STANDALONE) {
3678 spin_unlock_irq(&mdev->req_lock);
3679 return;
3680 }
3681 /* paranoia code */
3682 for (h = mdev->ee_hash; h < mdev->ee_hash + mdev->ee_hash_s; h++)
3683 if (h->first)
3684 dev_err(DEV, "ASSERT FAILED ee_hash[%u].first == %p, expected NULL\n",
3685 (int)(h - mdev->ee_hash), h->first);
3686 kfree(mdev->ee_hash);
3687 mdev->ee_hash = NULL;
3688 mdev->ee_hash_s = 0;
3689
3690 /* paranoia code */
3691 for (h = mdev->tl_hash; h < mdev->tl_hash + mdev->tl_hash_s; h++)
3692 if (h->first)
3693 dev_err(DEV, "ASSERT FAILED tl_hash[%u] == %p, expected NULL\n",
3694 (int)(h - mdev->tl_hash), h->first);
3695 kfree(mdev->tl_hash);
3696 mdev->tl_hash = NULL;
3697 mdev->tl_hash_s = 0;
3698 spin_unlock_irq(&mdev->req_lock);
3699}
3700
Philipp Reisnerb411b362009-09-25 16:07:19 -07003701static void drbd_disconnect(struct drbd_conf *mdev)
3702{
3703 enum drbd_fencing_p fp;
3704 union drbd_state os, ns;
3705 int rv = SS_UNKNOWN_ERROR;
3706 unsigned int i;
3707
3708 if (mdev->state.conn == C_STANDALONE)
3709 return;
3710 if (mdev->state.conn >= C_WF_CONNECTION)
3711 dev_err(DEV, "ASSERT FAILED cstate = %s, expected < WFConnection\n",
3712 drbd_conn_str(mdev->state.conn));
3713
3714 /* asender does not clean up anything. it must not interfere, either */
3715 drbd_thread_stop(&mdev->asender);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003716 drbd_free_sock(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003717
Philipp Reisner85719572010-07-21 10:20:17 +02003718 /* wait for current activity to cease. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003719 spin_lock_irq(&mdev->req_lock);
3720 _drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
3721 _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee);
3722 _drbd_wait_ee_list_empty(mdev, &mdev->read_ee);
3723 spin_unlock_irq(&mdev->req_lock);
3724
3725 /* We do not have data structures that would allow us to
3726 * get the rs_pending_cnt down to 0 again.
3727 * * On C_SYNC_TARGET we do not have any data structures describing
3728 * the pending RSDataRequest's we have sent.
3729 * * On C_SYNC_SOURCE there is no data structure that tracks
3730 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
3731 * And no, it is not the sum of the reference counts in the
3732 * resync_LRU. The resync_LRU tracks the whole operation including
3733 * the disk-IO, while the rs_pending_cnt only tracks the blocks
3734 * on the fly. */
3735 drbd_rs_cancel_all(mdev);
3736 mdev->rs_total = 0;
3737 mdev->rs_failed = 0;
3738 atomic_set(&mdev->rs_pending_cnt, 0);
3739 wake_up(&mdev->misc_wait);
3740
3741 /* make sure syncer is stopped and w_resume_next_sg queued */
3742 del_timer_sync(&mdev->resync_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003743 resync_timer_fn((unsigned long)mdev);
3744
Philipp Reisnerb411b362009-09-25 16:07:19 -07003745 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
3746 * w_make_resync_request etc. which may still be on the worker queue
3747 * to be "canceled" */
3748 drbd_flush_workqueue(mdev);
3749
3750 /* This also does reclaim_net_ee(). If we do this too early, we might
3751 * miss some resync ee and pages.*/
3752 drbd_process_done_ee(mdev);
3753
3754 kfree(mdev->p_uuid);
3755 mdev->p_uuid = NULL;
3756
Philipp Reisnerfb22c402010-09-08 23:20:21 +02003757 if (!is_susp(mdev->state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003758 tl_clear(mdev);
3759
Philipp Reisnerb411b362009-09-25 16:07:19 -07003760 dev_info(DEV, "Connection closed\n");
3761
3762 drbd_md_sync(mdev);
3763
3764 fp = FP_DONT_CARE;
3765 if (get_ldev(mdev)) {
3766 fp = mdev->ldev->dc.fencing;
3767 put_ldev(mdev);
3768 }
3769
Philipp Reisner87f7be42010-06-11 13:56:33 +02003770 if (mdev->state.role == R_PRIMARY && fp >= FP_RESOURCE && mdev->state.pdsk >= D_UNKNOWN)
3771 drbd_try_outdate_peer_async(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003772
3773 spin_lock_irq(&mdev->req_lock);
3774 os = mdev->state;
3775 if (os.conn >= C_UNCONNECTED) {
3776 /* Do not restart in case we are C_DISCONNECTING */
3777 ns = os;
3778 ns.conn = C_UNCONNECTED;
3779 rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
3780 }
3781 spin_unlock_irq(&mdev->req_lock);
3782
3783 if (os.conn == C_DISCONNECTING) {
Philipp Reisner84dfb9f2010-06-23 11:20:05 +02003784 wait_event(mdev->net_cnt_wait, atomic_read(&mdev->net_cnt) == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003785
Philipp Reisnerfb22c402010-09-08 23:20:21 +02003786 if (!is_susp(mdev->state)) {
Philipp Reisnerf70b35112010-06-24 14:34:40 +02003787 /* we must not free the tl_hash
3788 * while application io is still on the fly */
3789 wait_event(mdev->misc_wait, !atomic_read(&mdev->ap_bio_cnt));
3790 drbd_free_tl_hash(mdev);
3791 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003792
3793 crypto_free_hash(mdev->cram_hmac_tfm);
3794 mdev->cram_hmac_tfm = NULL;
3795
3796 kfree(mdev->net_conf);
3797 mdev->net_conf = NULL;
3798 drbd_request_state(mdev, NS(conn, C_STANDALONE));
3799 }
3800
3801 /* tcp_close and release of sendpage pages can be deferred. I don't
3802 * want to use SO_LINGER, because apparently it can be deferred for
3803 * more than 20 seconds (longest time I checked).
3804 *
3805 * Actually we don't care for exactly when the network stack does its
3806 * put_page(), but release our reference on these pages right here.
3807 */
3808 i = drbd_release_ee(mdev, &mdev->net_ee);
3809 if (i)
3810 dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
Lars Ellenberg435f0742010-09-06 12:30:25 +02003811 i = atomic_read(&mdev->pp_in_use_by_net);
3812 if (i)
3813 dev_info(DEV, "pp_in_use_by_net = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003814 i = atomic_read(&mdev->pp_in_use);
3815 if (i)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02003816 dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003817
3818 D_ASSERT(list_empty(&mdev->read_ee));
3819 D_ASSERT(list_empty(&mdev->active_ee));
3820 D_ASSERT(list_empty(&mdev->sync_ee));
3821 D_ASSERT(list_empty(&mdev->done_ee));
3822
3823 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
3824 atomic_set(&mdev->current_epoch->epoch_size, 0);
3825 D_ASSERT(list_empty(&mdev->current_epoch->list));
3826}
3827
3828/*
3829 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
3830 * we can agree on is stored in agreed_pro_version.
3831 *
3832 * feature flags and the reserved array should be enough room for future
3833 * enhancements of the handshake protocol, and possible plugins...
3834 *
3835 * for now, they are expected to be zero, but ignored.
3836 */
3837static int drbd_send_handshake(struct drbd_conf *mdev)
3838{
3839 /* ASSERT current == mdev->receiver ... */
3840 struct p_handshake *p = &mdev->data.sbuf.handshake;
3841 int ok;
3842
3843 if (mutex_lock_interruptible(&mdev->data.mutex)) {
3844 dev_err(DEV, "interrupted during initial handshake\n");
3845 return 0; /* interrupted. not ok. */
3846 }
3847
3848 if (mdev->data.socket == NULL) {
3849 mutex_unlock(&mdev->data.mutex);
3850 return 0;
3851 }
3852
3853 memset(p, 0, sizeof(*p));
3854 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
3855 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
3856 ok = _drbd_send_cmd( mdev, mdev->data.socket, P_HAND_SHAKE,
Philipp Reisner0b70a132010-08-20 13:36:10 +02003857 (struct p_header80 *)p, sizeof(*p), 0 );
Philipp Reisnerb411b362009-09-25 16:07:19 -07003858 mutex_unlock(&mdev->data.mutex);
3859 return ok;
3860}
3861
3862/*
3863 * return values:
3864 * 1 yes, we have a valid connection
3865 * 0 oops, did not work out, please try again
3866 * -1 peer talks different language,
3867 * no point in trying again, please go standalone.
3868 */
3869static int drbd_do_handshake(struct drbd_conf *mdev)
3870{
3871 /* ASSERT current == mdev->receiver ... */
3872 struct p_handshake *p = &mdev->data.rbuf.handshake;
Philipp Reisner02918be2010-08-20 14:35:10 +02003873 const int expect = sizeof(struct p_handshake) - sizeof(struct p_header80);
3874 unsigned int length;
3875 enum drbd_packets cmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003876 int rv;
3877
3878 rv = drbd_send_handshake(mdev);
3879 if (!rv)
3880 return 0;
3881
Philipp Reisner02918be2010-08-20 14:35:10 +02003882 rv = drbd_recv_header(mdev, &cmd, &length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003883 if (!rv)
3884 return 0;
3885
Philipp Reisner02918be2010-08-20 14:35:10 +02003886 if (cmd != P_HAND_SHAKE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003887 dev_err(DEV, "expected HandShake packet, received: %s (0x%04x)\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02003888 cmdname(cmd), cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003889 return -1;
3890 }
3891
Philipp Reisner02918be2010-08-20 14:35:10 +02003892 if (length != expect) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003893 dev_err(DEV, "expected HandShake length: %u, received: %u\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02003894 expect, length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003895 return -1;
3896 }
3897
3898 rv = drbd_recv(mdev, &p->head.payload, expect);
3899
3900 if (rv != expect) {
3901 dev_err(DEV, "short read receiving handshake packet: l=%u\n", rv);
3902 return 0;
3903 }
3904
Philipp Reisnerb411b362009-09-25 16:07:19 -07003905 p->protocol_min = be32_to_cpu(p->protocol_min);
3906 p->protocol_max = be32_to_cpu(p->protocol_max);
3907 if (p->protocol_max == 0)
3908 p->protocol_max = p->protocol_min;
3909
3910 if (PRO_VERSION_MAX < p->protocol_min ||
3911 PRO_VERSION_MIN > p->protocol_max)
3912 goto incompat;
3913
3914 mdev->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
3915
3916 dev_info(DEV, "Handshake successful: "
3917 "Agreed network protocol version %d\n", mdev->agreed_pro_version);
3918
3919 return 1;
3920
3921 incompat:
3922 dev_err(DEV, "incompatible DRBD dialects: "
3923 "I support %d-%d, peer supports %d-%d\n",
3924 PRO_VERSION_MIN, PRO_VERSION_MAX,
3925 p->protocol_min, p->protocol_max);
3926 return -1;
3927}
3928
3929#if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
3930static int drbd_do_auth(struct drbd_conf *mdev)
3931{
3932 dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
3933 dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01003934 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003935}
3936#else
3937#define CHALLENGE_LEN 64
Johannes Thomab10d96c2010-01-07 16:02:50 +01003938
3939/* Return value:
3940 1 - auth succeeded,
3941 0 - failed, try again (network error),
3942 -1 - auth failed, don't try again.
3943*/
3944
Philipp Reisnerb411b362009-09-25 16:07:19 -07003945static int drbd_do_auth(struct drbd_conf *mdev)
3946{
3947 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
3948 struct scatterlist sg;
3949 char *response = NULL;
3950 char *right_response = NULL;
3951 char *peers_ch = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003952 unsigned int key_len = strlen(mdev->net_conf->shared_secret);
3953 unsigned int resp_size;
3954 struct hash_desc desc;
Philipp Reisner02918be2010-08-20 14:35:10 +02003955 enum drbd_packets cmd;
3956 unsigned int length;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003957 int rv;
3958
3959 desc.tfm = mdev->cram_hmac_tfm;
3960 desc.flags = 0;
3961
3962 rv = crypto_hash_setkey(mdev->cram_hmac_tfm,
3963 (u8 *)mdev->net_conf->shared_secret, key_len);
3964 if (rv) {
3965 dev_err(DEV, "crypto_hash_setkey() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01003966 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003967 goto fail;
3968 }
3969
3970 get_random_bytes(my_challenge, CHALLENGE_LEN);
3971
3972 rv = drbd_send_cmd2(mdev, P_AUTH_CHALLENGE, my_challenge, CHALLENGE_LEN);
3973 if (!rv)
3974 goto fail;
3975
Philipp Reisner02918be2010-08-20 14:35:10 +02003976 rv = drbd_recv_header(mdev, &cmd, &length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003977 if (!rv)
3978 goto fail;
3979
Philipp Reisner02918be2010-08-20 14:35:10 +02003980 if (cmd != P_AUTH_CHALLENGE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003981 dev_err(DEV, "expected AuthChallenge packet, received: %s (0x%04x)\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02003982 cmdname(cmd), cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003983 rv = 0;
3984 goto fail;
3985 }
3986
Philipp Reisner02918be2010-08-20 14:35:10 +02003987 if (length > CHALLENGE_LEN * 2) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003988 dev_err(DEV, "expected AuthChallenge payload too big.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01003989 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003990 goto fail;
3991 }
3992
Philipp Reisner02918be2010-08-20 14:35:10 +02003993 peers_ch = kmalloc(length, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003994 if (peers_ch == NULL) {
3995 dev_err(DEV, "kmalloc of peers_ch failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01003996 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003997 goto fail;
3998 }
3999
Philipp Reisner02918be2010-08-20 14:35:10 +02004000 rv = drbd_recv(mdev, peers_ch, length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004001
Philipp Reisner02918be2010-08-20 14:35:10 +02004002 if (rv != length) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004003 dev_err(DEV, "short read AuthChallenge: l=%u\n", rv);
4004 rv = 0;
4005 goto fail;
4006 }
4007
4008 resp_size = crypto_hash_digestsize(mdev->cram_hmac_tfm);
4009 response = kmalloc(resp_size, GFP_NOIO);
4010 if (response == NULL) {
4011 dev_err(DEV, "kmalloc of response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004012 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004013 goto fail;
4014 }
4015
4016 sg_init_table(&sg, 1);
Philipp Reisner02918be2010-08-20 14:35:10 +02004017 sg_set_buf(&sg, peers_ch, length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004018
4019 rv = crypto_hash_digest(&desc, &sg, sg.length, response);
4020 if (rv) {
4021 dev_err(DEV, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004022 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004023 goto fail;
4024 }
4025
4026 rv = drbd_send_cmd2(mdev, P_AUTH_RESPONSE, response, resp_size);
4027 if (!rv)
4028 goto fail;
4029
Philipp Reisner02918be2010-08-20 14:35:10 +02004030 rv = drbd_recv_header(mdev, &cmd, &length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004031 if (!rv)
4032 goto fail;
4033
Philipp Reisner02918be2010-08-20 14:35:10 +02004034 if (cmd != P_AUTH_RESPONSE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004035 dev_err(DEV, "expected AuthResponse packet, received: %s (0x%04x)\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02004036 cmdname(cmd), cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004037 rv = 0;
4038 goto fail;
4039 }
4040
Philipp Reisner02918be2010-08-20 14:35:10 +02004041 if (length != resp_size) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004042 dev_err(DEV, "expected AuthResponse payload of wrong size\n");
4043 rv = 0;
4044 goto fail;
4045 }
4046
4047 rv = drbd_recv(mdev, response , resp_size);
4048
4049 if (rv != resp_size) {
4050 dev_err(DEV, "short read receiving AuthResponse: l=%u\n", rv);
4051 rv = 0;
4052 goto fail;
4053 }
4054
4055 right_response = kmalloc(resp_size, GFP_NOIO);
Julia Lawall2d1ee872009-12-27 22:27:11 +01004056 if (right_response == NULL) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004057 dev_err(DEV, "kmalloc of right_response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004058 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004059 goto fail;
4060 }
4061
4062 sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
4063
4064 rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
4065 if (rv) {
4066 dev_err(DEV, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004067 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004068 goto fail;
4069 }
4070
4071 rv = !memcmp(response, right_response, resp_size);
4072
4073 if (rv)
4074 dev_info(DEV, "Peer authenticated using %d bytes of '%s' HMAC\n",
4075 resp_size, mdev->net_conf->cram_hmac_alg);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004076 else
4077 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004078
4079 fail:
4080 kfree(peers_ch);
4081 kfree(response);
4082 kfree(right_response);
4083
4084 return rv;
4085}
4086#endif
4087
4088int drbdd_init(struct drbd_thread *thi)
4089{
4090 struct drbd_conf *mdev = thi->mdev;
4091 unsigned int minor = mdev_to_minor(mdev);
4092 int h;
4093
4094 sprintf(current->comm, "drbd%d_receiver", minor);
4095
4096 dev_info(DEV, "receiver (re)started\n");
4097
4098 do {
4099 h = drbd_connect(mdev);
4100 if (h == 0) {
4101 drbd_disconnect(mdev);
4102 __set_current_state(TASK_INTERRUPTIBLE);
4103 schedule_timeout(HZ);
4104 }
4105 if (h == -1) {
4106 dev_warn(DEV, "Discarding network configuration.\n");
4107 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
4108 }
4109 } while (h == 0);
4110
4111 if (h > 0) {
4112 if (get_net_conf(mdev)) {
4113 drbdd(mdev);
4114 put_net_conf(mdev);
4115 }
4116 }
4117
4118 drbd_disconnect(mdev);
4119
4120 dev_info(DEV, "receiver terminated\n");
4121 return 0;
4122}
4123
4124/* ********* acknowledge sender ******** */
4125
Philipp Reisner0b70a132010-08-20 13:36:10 +02004126static int got_RqSReply(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004127{
4128 struct p_req_state_reply *p = (struct p_req_state_reply *)h;
4129
4130 int retcode = be32_to_cpu(p->retcode);
4131
4132 if (retcode >= SS_SUCCESS) {
4133 set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
4134 } else {
4135 set_bit(CL_ST_CHG_FAIL, &mdev->flags);
4136 dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
4137 drbd_set_st_err_str(retcode), retcode);
4138 }
4139 wake_up(&mdev->state_wait);
4140
4141 return TRUE;
4142}
4143
Philipp Reisner0b70a132010-08-20 13:36:10 +02004144static int got_Ping(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004145{
4146 return drbd_send_ping_ack(mdev);
4147
4148}
4149
Philipp Reisner0b70a132010-08-20 13:36:10 +02004150static int got_PingAck(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004151{
4152 /* restore idle timeout */
4153 mdev->meta.socket->sk->sk_rcvtimeo = mdev->net_conf->ping_int*HZ;
Philipp Reisner309d1602010-03-02 15:03:44 +01004154 if (!test_and_set_bit(GOT_PING_ACK, &mdev->flags))
4155 wake_up(&mdev->misc_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004156
4157 return TRUE;
4158}
4159
Philipp Reisner0b70a132010-08-20 13:36:10 +02004160static int got_IsInSync(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004161{
4162 struct p_block_ack *p = (struct p_block_ack *)h;
4163 sector_t sector = be64_to_cpu(p->sector);
4164 int blksize = be32_to_cpu(p->blksize);
4165
4166 D_ASSERT(mdev->agreed_pro_version >= 89);
4167
4168 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4169
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004170 if (get_ldev(mdev)) {
4171 drbd_rs_complete_io(mdev, sector);
4172 drbd_set_in_sync(mdev, sector, blksize);
4173 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
4174 mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
4175 put_ldev(mdev);
4176 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004177 dec_rs_pending(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02004178 atomic_add(blksize >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004179
4180 return TRUE;
4181}
4182
4183/* when we receive the ACK for a write request,
4184 * verify that we actually know about it */
4185static struct drbd_request *_ack_id_to_req(struct drbd_conf *mdev,
4186 u64 id, sector_t sector)
4187{
4188 struct hlist_head *slot = tl_hash_slot(mdev, sector);
4189 struct hlist_node *n;
4190 struct drbd_request *req;
4191
4192 hlist_for_each_entry(req, n, slot, colision) {
4193 if ((unsigned long)req == (unsigned long)id) {
4194 if (req->sector != sector) {
4195 dev_err(DEV, "_ack_id_to_req: found req %p but it has "
4196 "wrong sector (%llus versus %llus)\n", req,
4197 (unsigned long long)req->sector,
4198 (unsigned long long)sector);
4199 break;
4200 }
4201 return req;
4202 }
4203 }
4204 dev_err(DEV, "_ack_id_to_req: failed to find req %p, sector %llus in list\n",
4205 (void *)(unsigned long)id, (unsigned long long)sector);
4206 return NULL;
4207}
4208
4209typedef struct drbd_request *(req_validator_fn)
4210 (struct drbd_conf *mdev, u64 id, sector_t sector);
4211
4212static int validate_req_change_req_state(struct drbd_conf *mdev,
4213 u64 id, sector_t sector, req_validator_fn validator,
4214 const char *func, enum drbd_req_event what)
4215{
4216 struct drbd_request *req;
4217 struct bio_and_error m;
4218
4219 spin_lock_irq(&mdev->req_lock);
4220 req = validator(mdev, id, sector);
4221 if (unlikely(!req)) {
4222 spin_unlock_irq(&mdev->req_lock);
4223 dev_err(DEV, "%s: got a corrupt block_id/sector pair\n", func);
4224 return FALSE;
4225 }
4226 __req_mod(req, what, &m);
4227 spin_unlock_irq(&mdev->req_lock);
4228
4229 if (m.bio)
4230 complete_master_bio(mdev, &m);
4231 return TRUE;
4232}
4233
Philipp Reisner0b70a132010-08-20 13:36:10 +02004234static int got_BlockAck(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004235{
4236 struct p_block_ack *p = (struct p_block_ack *)h;
4237 sector_t sector = be64_to_cpu(p->sector);
4238 int blksize = be32_to_cpu(p->blksize);
4239 enum drbd_req_event what;
4240
4241 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4242
4243 if (is_syncer_block_id(p->block_id)) {
4244 drbd_set_in_sync(mdev, sector, blksize);
4245 dec_rs_pending(mdev);
4246 return TRUE;
4247 }
4248 switch (be16_to_cpu(h->command)) {
4249 case P_RS_WRITE_ACK:
4250 D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
4251 what = write_acked_by_peer_and_sis;
4252 break;
4253 case P_WRITE_ACK:
4254 D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
4255 what = write_acked_by_peer;
4256 break;
4257 case P_RECV_ACK:
4258 D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_B);
4259 what = recv_acked_by_peer;
4260 break;
4261 case P_DISCARD_ACK:
4262 D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
4263 what = conflict_discarded_by_peer;
4264 break;
4265 default:
4266 D_ASSERT(0);
4267 return FALSE;
4268 }
4269
4270 return validate_req_change_req_state(mdev, p->block_id, sector,
4271 _ack_id_to_req, __func__ , what);
4272}
4273
Philipp Reisner0b70a132010-08-20 13:36:10 +02004274static int got_NegAck(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004275{
4276 struct p_block_ack *p = (struct p_block_ack *)h;
4277 sector_t sector = be64_to_cpu(p->sector);
4278
4279 if (__ratelimit(&drbd_ratelimit_state))
4280 dev_warn(DEV, "Got NegAck packet. Peer is in troubles?\n");
4281
4282 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4283
4284 if (is_syncer_block_id(p->block_id)) {
4285 int size = be32_to_cpu(p->blksize);
4286 dec_rs_pending(mdev);
4287 drbd_rs_failed_io(mdev, sector, size);
4288 return TRUE;
4289 }
4290 return validate_req_change_req_state(mdev, p->block_id, sector,
4291 _ack_id_to_req, __func__ , neg_acked);
4292}
4293
Philipp Reisner0b70a132010-08-20 13:36:10 +02004294static int got_NegDReply(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004295{
4296 struct p_block_ack *p = (struct p_block_ack *)h;
4297 sector_t sector = be64_to_cpu(p->sector);
4298
4299 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4300 dev_err(DEV, "Got NegDReply; Sector %llus, len %u; Fail original request.\n",
4301 (unsigned long long)sector, be32_to_cpu(p->blksize));
4302
4303 return validate_req_change_req_state(mdev, p->block_id, sector,
4304 _ar_id_to_req, __func__ , neg_acked);
4305}
4306
Philipp Reisner0b70a132010-08-20 13:36:10 +02004307static int got_NegRSDReply(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004308{
4309 sector_t sector;
4310 int size;
4311 struct p_block_ack *p = (struct p_block_ack *)h;
4312
4313 sector = be64_to_cpu(p->sector);
4314 size = be32_to_cpu(p->blksize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004315
4316 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4317
4318 dec_rs_pending(mdev);
4319
4320 if (get_ldev_if_state(mdev, D_FAILED)) {
4321 drbd_rs_complete_io(mdev, sector);
4322 drbd_rs_failed_io(mdev, sector, size);
4323 put_ldev(mdev);
4324 }
4325
4326 return TRUE;
4327}
4328
Philipp Reisner0b70a132010-08-20 13:36:10 +02004329static int got_BarrierAck(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004330{
4331 struct p_barrier_ack *p = (struct p_barrier_ack *)h;
4332
4333 tl_release(mdev, p->barrier, be32_to_cpu(p->set_size));
4334
4335 return TRUE;
4336}
4337
Philipp Reisner0b70a132010-08-20 13:36:10 +02004338static int got_OVResult(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004339{
4340 struct p_block_ack *p = (struct p_block_ack *)h;
4341 struct drbd_work *w;
4342 sector_t sector;
4343 int size;
4344
4345 sector = be64_to_cpu(p->sector);
4346 size = be32_to_cpu(p->blksize);
4347
4348 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4349
4350 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
4351 drbd_ov_oos_found(mdev, sector, size);
4352 else
4353 ov_oos_print(mdev);
4354
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004355 if (!get_ldev(mdev))
4356 return TRUE;
4357
Philipp Reisnerb411b362009-09-25 16:07:19 -07004358 drbd_rs_complete_io(mdev, sector);
4359 dec_rs_pending(mdev);
4360
4361 if (--mdev->ov_left == 0) {
4362 w = kmalloc(sizeof(*w), GFP_NOIO);
4363 if (w) {
4364 w->cb = w_ov_finished;
4365 drbd_queue_work_front(&mdev->data.work, w);
4366 } else {
4367 dev_err(DEV, "kmalloc(w) failed.");
4368 ov_oos_print(mdev);
4369 drbd_resync_finished(mdev);
4370 }
4371 }
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004372 put_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004373 return TRUE;
4374}
4375
Philipp Reisner02918be2010-08-20 14:35:10 +02004376static int got_skip(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisner0ced55a2010-04-30 15:26:20 +02004377{
Philipp Reisner0ced55a2010-04-30 15:26:20 +02004378 return TRUE;
4379}
4380
Philipp Reisnerb411b362009-09-25 16:07:19 -07004381struct asender_cmd {
4382 size_t pkt_size;
Philipp Reisner0b70a132010-08-20 13:36:10 +02004383 int (*process)(struct drbd_conf *mdev, struct p_header80 *h);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004384};
4385
4386static struct asender_cmd *get_asender_cmd(int cmd)
4387{
4388 static struct asender_cmd asender_tbl[] = {
4389 /* anything missing from this table is in
4390 * the drbd_cmd_handler (drbd_default_handler) table,
4391 * see the beginning of drbdd() */
Philipp Reisner0b70a132010-08-20 13:36:10 +02004392 [P_PING] = { sizeof(struct p_header80), got_Ping },
4393 [P_PING_ACK] = { sizeof(struct p_header80), got_PingAck },
Philipp Reisnerb411b362009-09-25 16:07:19 -07004394 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4395 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4396 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4397 [P_DISCARD_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4398 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
4399 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
4400 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply},
4401 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
4402 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
4403 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
4404 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
Philipp Reisner02918be2010-08-20 14:35:10 +02004405 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
Philipp Reisnerb411b362009-09-25 16:07:19 -07004406 [P_MAX_CMD] = { 0, NULL },
4407 };
4408 if (cmd > P_MAX_CMD || asender_tbl[cmd].process == NULL)
4409 return NULL;
4410 return &asender_tbl[cmd];
4411}
4412
4413int drbd_asender(struct drbd_thread *thi)
4414{
4415 struct drbd_conf *mdev = thi->mdev;
Philipp Reisner02918be2010-08-20 14:35:10 +02004416 struct p_header80 *h = &mdev->meta.rbuf.header.h80;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004417 struct asender_cmd *cmd = NULL;
4418
4419 int rv, len;
4420 void *buf = h;
4421 int received = 0;
Philipp Reisner0b70a132010-08-20 13:36:10 +02004422 int expect = sizeof(struct p_header80);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004423 int empty;
4424
4425 sprintf(current->comm, "drbd%d_asender", mdev_to_minor(mdev));
4426
4427 current->policy = SCHED_RR; /* Make this a realtime task! */
4428 current->rt_priority = 2; /* more important than all other tasks */
4429
4430 while (get_t_state(thi) == Running) {
4431 drbd_thread_current_set_cpu(mdev);
4432 if (test_and_clear_bit(SEND_PING, &mdev->flags)) {
4433 ERR_IF(!drbd_send_ping(mdev)) goto reconnect;
4434 mdev->meta.socket->sk->sk_rcvtimeo =
4435 mdev->net_conf->ping_timeo*HZ/10;
4436 }
4437
4438 /* conditionally cork;
4439 * it may hurt latency if we cork without much to send */
4440 if (!mdev->net_conf->no_cork &&
4441 3 < atomic_read(&mdev->unacked_cnt))
4442 drbd_tcp_cork(mdev->meta.socket);
4443 while (1) {
4444 clear_bit(SIGNAL_ASENDER, &mdev->flags);
4445 flush_signals(current);
Lars Ellenberg0f8488e2010-10-13 18:19:23 +02004446 if (!drbd_process_done_ee(mdev))
Philipp Reisnerb411b362009-09-25 16:07:19 -07004447 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004448 /* to avoid race with newly queued ACKs */
4449 set_bit(SIGNAL_ASENDER, &mdev->flags);
4450 spin_lock_irq(&mdev->req_lock);
4451 empty = list_empty(&mdev->done_ee);
4452 spin_unlock_irq(&mdev->req_lock);
4453 /* new ack may have been queued right here,
4454 * but then there is also a signal pending,
4455 * and we start over... */
4456 if (empty)
4457 break;
4458 }
4459 /* but unconditionally uncork unless disabled */
4460 if (!mdev->net_conf->no_cork)
4461 drbd_tcp_uncork(mdev->meta.socket);
4462
4463 /* short circuit, recv_msg would return EINTR anyways. */
4464 if (signal_pending(current))
4465 continue;
4466
4467 rv = drbd_recv_short(mdev, mdev->meta.socket,
4468 buf, expect-received, 0);
4469 clear_bit(SIGNAL_ASENDER, &mdev->flags);
4470
4471 flush_signals(current);
4472
4473 /* Note:
4474 * -EINTR (on meta) we got a signal
4475 * -EAGAIN (on meta) rcvtimeo expired
4476 * -ECONNRESET other side closed the connection
4477 * -ERESTARTSYS (on data) we got a signal
4478 * rv < 0 other than above: unexpected error!
4479 * rv == expected: full header or command
4480 * rv < expected: "woken" by signal during receive
4481 * rv == 0 : "connection shut down by peer"
4482 */
4483 if (likely(rv > 0)) {
4484 received += rv;
4485 buf += rv;
4486 } else if (rv == 0) {
4487 dev_err(DEV, "meta connection shut down by peer.\n");
4488 goto reconnect;
4489 } else if (rv == -EAGAIN) {
4490 if (mdev->meta.socket->sk->sk_rcvtimeo ==
4491 mdev->net_conf->ping_timeo*HZ/10) {
4492 dev_err(DEV, "PingAck did not arrive in time.\n");
4493 goto reconnect;
4494 }
4495 set_bit(SEND_PING, &mdev->flags);
4496 continue;
4497 } else if (rv == -EINTR) {
4498 continue;
4499 } else {
4500 dev_err(DEV, "sock_recvmsg returned %d\n", rv);
4501 goto reconnect;
4502 }
4503
4504 if (received == expect && cmd == NULL) {
4505 if (unlikely(h->magic != BE_DRBD_MAGIC)) {
Lars Ellenberg004352f2010-10-05 20:13:58 +02004506 dev_err(DEV, "magic?? on meta m: 0x%08x c: %d l: %d\n",
4507 be32_to_cpu(h->magic),
4508 be16_to_cpu(h->command),
4509 be16_to_cpu(h->length));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004510 goto reconnect;
4511 }
4512 cmd = get_asender_cmd(be16_to_cpu(h->command));
4513 len = be16_to_cpu(h->length);
4514 if (unlikely(cmd == NULL)) {
Lars Ellenberg004352f2010-10-05 20:13:58 +02004515 dev_err(DEV, "unknown command?? on meta m: 0x%08x c: %d l: %d\n",
4516 be32_to_cpu(h->magic),
4517 be16_to_cpu(h->command),
4518 be16_to_cpu(h->length));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004519 goto disconnect;
4520 }
4521 expect = cmd->pkt_size;
Philipp Reisner0b70a132010-08-20 13:36:10 +02004522 ERR_IF(len != expect-sizeof(struct p_header80))
Philipp Reisnerb411b362009-09-25 16:07:19 -07004523 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004524 }
4525 if (received == expect) {
4526 D_ASSERT(cmd != NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004527 if (!cmd->process(mdev, h))
4528 goto reconnect;
4529
4530 buf = h;
4531 received = 0;
Philipp Reisner0b70a132010-08-20 13:36:10 +02004532 expect = sizeof(struct p_header80);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004533 cmd = NULL;
4534 }
4535 }
4536
4537 if (0) {
4538reconnect:
4539 drbd_force_state(mdev, NS(conn, C_NETWORK_FAILURE));
Lars Ellenberg856c50c2010-10-14 13:37:40 +02004540 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004541 }
4542 if (0) {
4543disconnect:
4544 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Lars Ellenberg856c50c2010-10-14 13:37:40 +02004545 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004546 }
4547 clear_bit(SIGNAL_ASENDER, &mdev->flags);
4548
4549 D_ASSERT(mdev->state.conn < C_CONNECTED);
4550 dev_info(DEV, "asender terminated\n");
4551
4552 return 0;
4553}