blob: a0fbbfc77d8561478b5a7446ea0159b9bc15b49a [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>
Philipp Reisnerb411b362009-09-25 16:07:19 -070039#include <linux/pkt_sched.h>
40#define __KERNEL_SYSCALLS__
41#include <linux/unistd.h>
42#include <linux/vmalloc.h>
43#include <linux/random.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070044#include <linux/string.h>
45#include <linux/scatterlist.h>
46#include "drbd_int.h"
Philipp Reisnerb411b362009-09-25 16:07:19 -070047#include "drbd_req.h"
48
49#include "drbd_vli.h"
50
Philipp Reisnerb411b362009-09-25 16:07:19 -070051enum finish_epoch {
52 FE_STILL_LIVE,
53 FE_DESTROYED,
54 FE_RECYCLED,
55};
56
57static int drbd_do_handshake(struct drbd_conf *mdev);
58static int drbd_do_auth(struct drbd_conf *mdev);
59
60static enum finish_epoch drbd_may_finish_epoch(struct drbd_conf *, struct drbd_epoch *, enum epoch_event);
61static int e_end_block(struct drbd_conf *, struct drbd_work *, int);
62
Philipp Reisnerb411b362009-09-25 16:07:19 -070063
64#define GFP_TRY (__GFP_HIGHMEM | __GFP_NOWARN)
65
Lars Ellenberg45bb9122010-05-14 17:10:48 +020066/*
67 * some helper functions to deal with single linked page lists,
68 * page->private being our "next" pointer.
69 */
70
71/* If at least n pages are linked at head, get n pages off.
72 * Otherwise, don't modify head, and return NULL.
73 * Locking is the responsibility of the caller.
74 */
75static struct page *page_chain_del(struct page **head, int n)
76{
77 struct page *page;
78 struct page *tmp;
79
80 BUG_ON(!n);
81 BUG_ON(!head);
82
83 page = *head;
Philipp Reisner23ce4222010-05-20 13:35:31 +020084
85 if (!page)
86 return NULL;
87
Lars Ellenberg45bb9122010-05-14 17:10:48 +020088 while (page) {
89 tmp = page_chain_next(page);
90 if (--n == 0)
91 break; /* found sufficient pages */
92 if (tmp == NULL)
93 /* insufficient pages, don't use any of them. */
94 return NULL;
95 page = tmp;
96 }
97
98 /* add end of list marker for the returned list */
99 set_page_private(page, 0);
100 /* actual return value, and adjustment of head */
101 page = *head;
102 *head = tmp;
103 return page;
104}
105
106/* may be used outside of locks to find the tail of a (usually short)
107 * "private" page chain, before adding it back to a global chain head
108 * with page_chain_add() under a spinlock. */
109static struct page *page_chain_tail(struct page *page, int *len)
110{
111 struct page *tmp;
112 int i = 1;
113 while ((tmp = page_chain_next(page)))
114 ++i, page = tmp;
115 if (len)
116 *len = i;
117 return page;
118}
119
120static int page_chain_free(struct page *page)
121{
122 struct page *tmp;
123 int i = 0;
124 page_chain_for_each_safe(page, tmp) {
125 put_page(page);
126 ++i;
127 }
128 return i;
129}
130
131static void page_chain_add(struct page **head,
132 struct page *chain_first, struct page *chain_last)
133{
134#if 1
135 struct page *tmp;
136 tmp = page_chain_tail(chain_first, NULL);
137 BUG_ON(tmp != chain_last);
138#endif
139
140 /* add chain to head */
141 set_page_private(chain_last, (unsigned long)*head);
142 *head = chain_first;
143}
144
145static struct page *drbd_pp_first_pages_or_try_alloc(struct drbd_conf *mdev, int number)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700146{
147 struct page *page = NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200148 struct page *tmp = NULL;
149 int i = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700150
151 /* Yes, testing drbd_pp_vacant outside the lock is racy.
152 * So what. It saves a spin_lock. */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200153 if (drbd_pp_vacant >= number) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700154 spin_lock(&drbd_pp_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200155 page = page_chain_del(&drbd_pp_pool, number);
156 if (page)
157 drbd_pp_vacant -= number;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700158 spin_unlock(&drbd_pp_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200159 if (page)
160 return page;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700161 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200162
Philipp Reisnerb411b362009-09-25 16:07:19 -0700163 /* GFP_TRY, because we must not cause arbitrary write-out: in a DRBD
164 * "criss-cross" setup, that might cause write-out on some other DRBD,
165 * which in turn might block on the other node at this very place. */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200166 for (i = 0; i < number; i++) {
167 tmp = alloc_page(GFP_TRY);
168 if (!tmp)
169 break;
170 set_page_private(tmp, (unsigned long)page);
171 page = tmp;
172 }
173
174 if (i == number)
175 return page;
176
177 /* Not enough pages immediately available this time.
178 * No need to jump around here, drbd_pp_alloc will retry this
179 * function "soon". */
180 if (page) {
181 tmp = page_chain_tail(page, NULL);
182 spin_lock(&drbd_pp_lock);
183 page_chain_add(&drbd_pp_pool, page, tmp);
184 drbd_pp_vacant += i;
185 spin_unlock(&drbd_pp_lock);
186 }
187 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700188}
189
Philipp Reisnerb411b362009-09-25 16:07:19 -0700190static void reclaim_net_ee(struct drbd_conf *mdev, struct list_head *to_be_freed)
191{
192 struct drbd_epoch_entry *e;
193 struct list_head *le, *tle;
194
195 /* The EEs are always appended to the end of the list. Since
196 they are sent in order over the wire, they have to finish
197 in order. As soon as we see the first not finished we can
198 stop to examine the list... */
199
200 list_for_each_safe(le, tle, &mdev->net_ee) {
201 e = list_entry(le, struct drbd_epoch_entry, w.list);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200202 if (drbd_ee_has_active_page(e))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700203 break;
204 list_move(le, to_be_freed);
205 }
206}
207
208static void drbd_kick_lo_and_reclaim_net(struct drbd_conf *mdev)
209{
210 LIST_HEAD(reclaimed);
211 struct drbd_epoch_entry *e, *t;
212
Philipp Reisnerb411b362009-09-25 16:07:19 -0700213 spin_lock_irq(&mdev->req_lock);
214 reclaim_net_ee(mdev, &reclaimed);
215 spin_unlock_irq(&mdev->req_lock);
216
217 list_for_each_entry_safe(e, t, &reclaimed, w.list)
Lars Ellenberg435f0742010-09-06 12:30:25 +0200218 drbd_free_net_ee(mdev, e);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700219}
220
221/**
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200222 * drbd_pp_alloc() - Returns @number pages, retries forever (or until signalled)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700223 * @mdev: DRBD device.
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200224 * @number: number of pages requested
225 * @retry: whether to retry, if not enough pages are available right now
Philipp Reisnerb411b362009-09-25 16:07:19 -0700226 *
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200227 * Tries to allocate number pages, first from our own page pool, then from
228 * the kernel, unless this allocation would exceed the max_buffers setting.
229 * Possibly retry until DRBD frees sufficient pages somewhere else.
230 *
231 * Returns a page chain linked via page->private.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700232 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200233static struct page *drbd_pp_alloc(struct drbd_conf *mdev, unsigned number, bool retry)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700234{
235 struct page *page = NULL;
236 DEFINE_WAIT(wait);
237
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200238 /* Yes, we may run up to @number over max_buffers. If we
239 * follow it strictly, the admin will get it wrong anyways. */
240 if (atomic_read(&mdev->pp_in_use) < mdev->net_conf->max_buffers)
241 page = drbd_pp_first_pages_or_try_alloc(mdev, number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700242
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200243 while (page == NULL) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700244 prepare_to_wait(&drbd_pp_wait, &wait, TASK_INTERRUPTIBLE);
245
246 drbd_kick_lo_and_reclaim_net(mdev);
247
248 if (atomic_read(&mdev->pp_in_use) < mdev->net_conf->max_buffers) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200249 page = drbd_pp_first_pages_or_try_alloc(mdev, number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700250 if (page)
251 break;
252 }
253
254 if (!retry)
255 break;
256
257 if (signal_pending(current)) {
258 dev_warn(DEV, "drbd_pp_alloc interrupted!\n");
259 break;
260 }
261
262 schedule();
263 }
264 finish_wait(&drbd_pp_wait, &wait);
265
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200266 if (page)
267 atomic_add(number, &mdev->pp_in_use);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700268 return page;
269}
270
271/* Must not be used from irq, as that may deadlock: see drbd_pp_alloc.
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200272 * Is also used from inside an other spin_lock_irq(&mdev->req_lock);
273 * Either links the page chain back to the global pool,
274 * or returns all pages to the system. */
Lars Ellenberg435f0742010-09-06 12:30:25 +0200275static void drbd_pp_free(struct drbd_conf *mdev, struct page *page, int is_net)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700276{
Lars Ellenberg435f0742010-09-06 12:30:25 +0200277 atomic_t *a = is_net ? &mdev->pp_in_use_by_net : &mdev->pp_in_use;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700278 int i;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200279
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100280 if (drbd_pp_vacant > (DRBD_MAX_BIO_SIZE/PAGE_SIZE)*minor_count)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200281 i = page_chain_free(page);
282 else {
283 struct page *tmp;
284 tmp = page_chain_tail(page, &i);
285 spin_lock(&drbd_pp_lock);
286 page_chain_add(&drbd_pp_pool, page, tmp);
287 drbd_pp_vacant += i;
288 spin_unlock(&drbd_pp_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700289 }
Lars Ellenberg435f0742010-09-06 12:30:25 +0200290 i = atomic_sub_return(i, a);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200291 if (i < 0)
Lars Ellenberg435f0742010-09-06 12:30:25 +0200292 dev_warn(DEV, "ASSERTION FAILED: %s: %d < 0\n",
293 is_net ? "pp_in_use_by_net" : "pp_in_use", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700294 wake_up(&drbd_pp_wait);
295}
296
297/*
298You need to hold the req_lock:
299 _drbd_wait_ee_list_empty()
300
301You must not have the req_lock:
302 drbd_free_ee()
303 drbd_alloc_ee()
304 drbd_init_ee()
305 drbd_release_ee()
306 drbd_ee_fix_bhs()
307 drbd_process_done_ee()
308 drbd_clear_done_ee()
309 drbd_wait_ee_list_empty()
310*/
311
312struct drbd_epoch_entry *drbd_alloc_ee(struct drbd_conf *mdev,
313 u64 id,
314 sector_t sector,
315 unsigned int data_size,
316 gfp_t gfp_mask) __must_hold(local)
317{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700318 struct drbd_epoch_entry *e;
319 struct page *page;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200320 unsigned nr_pages = (data_size + PAGE_SIZE -1) >> PAGE_SHIFT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700321
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100322 if (drbd_insert_fault(mdev, DRBD_FAULT_AL_EE))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700323 return NULL;
324
325 e = mempool_alloc(drbd_ee_mempool, gfp_mask & ~__GFP_HIGHMEM);
326 if (!e) {
327 if (!(gfp_mask & __GFP_NOWARN))
328 dev_err(DEV, "alloc_ee: Allocation of an EE failed\n");
329 return NULL;
330 }
331
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200332 page = drbd_pp_alloc(mdev, nr_pages, (gfp_mask & __GFP_WAIT));
333 if (!page)
334 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700335
Bart Van Assche24c48302011-05-21 18:32:29 +0200336 INIT_HLIST_NODE(&e->collision);
Andreas Gruenbacher8b946252011-01-20 15:23:07 +0100337 drbd_clear_interval(&e->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700338 e->epoch = NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200339 e->mdev = mdev;
340 e->pages = page;
341 atomic_set(&e->pending_bios, 0);
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +0100342 e->i.size = data_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700343 e->flags = 0;
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +0100344 e->i.sector = sector;
Andreas Gruenbacher9a8e7752011-01-11 14:04:09 +0100345 /*
346 * The block_id is opaque to the receiver. It is not endianness
347 * converted, and sent back to the sender unchanged.
348 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200349 e->block_id = id;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700350
Philipp Reisnerb411b362009-09-25 16:07:19 -0700351 return e;
352
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200353 fail:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700354 mempool_free(e, drbd_ee_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700355 return NULL;
356}
357
Lars Ellenberg435f0742010-09-06 12:30:25 +0200358void drbd_free_some_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e, int is_net)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700359{
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +0200360 if (e->flags & EE_HAS_DIGEST)
361 kfree(e->digest);
Lars Ellenberg435f0742010-09-06 12:30:25 +0200362 drbd_pp_free(mdev, e->pages, is_net);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200363 D_ASSERT(atomic_read(&e->pending_bios) == 0);
Bart Van Assche24c48302011-05-21 18:32:29 +0200364 D_ASSERT(hlist_unhashed(&e->collision));
Andreas Gruenbacher8b946252011-01-20 15:23:07 +0100365 D_ASSERT(drbd_interval_empty(&e->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700366 mempool_free(e, drbd_ee_mempool);
367}
368
369int drbd_release_ee(struct drbd_conf *mdev, struct list_head *list)
370{
371 LIST_HEAD(work_list);
372 struct drbd_epoch_entry *e, *t;
373 int count = 0;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200374 int is_net = list == &mdev->net_ee;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700375
376 spin_lock_irq(&mdev->req_lock);
377 list_splice_init(list, &work_list);
378 spin_unlock_irq(&mdev->req_lock);
379
380 list_for_each_entry_safe(e, t, &work_list, w.list) {
Lars Ellenberg435f0742010-09-06 12:30:25 +0200381 drbd_free_some_ee(mdev, e, is_net);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700382 count++;
383 }
384 return count;
385}
386
387
388/*
389 * This function is called from _asender only_
390 * but see also comments in _req_mod(,barrier_acked)
391 * and receive_Barrier.
392 *
393 * Move entries from net_ee to done_ee, if ready.
394 * Grab done_ee, call all callbacks, free the entries.
395 * The callbacks typically send out ACKs.
396 */
397static int drbd_process_done_ee(struct drbd_conf *mdev)
398{
399 LIST_HEAD(work_list);
400 LIST_HEAD(reclaimed);
401 struct drbd_epoch_entry *e, *t;
402 int ok = (mdev->state.conn >= C_WF_REPORT_PARAMS);
403
404 spin_lock_irq(&mdev->req_lock);
405 reclaim_net_ee(mdev, &reclaimed);
406 list_splice_init(&mdev->done_ee, &work_list);
407 spin_unlock_irq(&mdev->req_lock);
408
409 list_for_each_entry_safe(e, t, &reclaimed, w.list)
Lars Ellenberg435f0742010-09-06 12:30:25 +0200410 drbd_free_net_ee(mdev, e);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700411
412 /* possible callbacks here:
413 * e_end_block, and e_end_resync_block, e_send_discard_ack.
414 * all ignore the last argument.
415 */
416 list_for_each_entry_safe(e, t, &work_list, w.list) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700417 /* list_del not necessary, next/prev members not touched */
418 ok = e->w.cb(mdev, &e->w, !ok) && ok;
419 drbd_free_ee(mdev, e);
420 }
421 wake_up(&mdev->ee_wait);
422
423 return ok;
424}
425
426void _drbd_wait_ee_list_empty(struct drbd_conf *mdev, struct list_head *head)
427{
428 DEFINE_WAIT(wait);
429
430 /* avoids spin_lock/unlock
431 * and calling prepare_to_wait in the fast path */
432 while (!list_empty(head)) {
433 prepare_to_wait(&mdev->ee_wait, &wait, TASK_UNINTERRUPTIBLE);
434 spin_unlock_irq(&mdev->req_lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100435 io_schedule();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700436 finish_wait(&mdev->ee_wait, &wait);
437 spin_lock_irq(&mdev->req_lock);
438 }
439}
440
441void drbd_wait_ee_list_empty(struct drbd_conf *mdev, struct list_head *head)
442{
443 spin_lock_irq(&mdev->req_lock);
444 _drbd_wait_ee_list_empty(mdev, head);
445 spin_unlock_irq(&mdev->req_lock);
446}
447
448/* see also kernel_accept; which is only present since 2.6.18.
449 * also we want to log which part of it failed, exactly */
450static int drbd_accept(struct drbd_conf *mdev, const char **what,
451 struct socket *sock, struct socket **newsock)
452{
453 struct sock *sk = sock->sk;
454 int err = 0;
455
456 *what = "listen";
457 err = sock->ops->listen(sock, 5);
458 if (err < 0)
459 goto out;
460
461 *what = "sock_create_lite";
462 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
463 newsock);
464 if (err < 0)
465 goto out;
466
467 *what = "accept";
468 err = sock->ops->accept(sock, *newsock, 0);
469 if (err < 0) {
470 sock_release(*newsock);
471 *newsock = NULL;
472 goto out;
473 }
474 (*newsock)->ops = sock->ops;
475
476out:
477 return err;
478}
479
480static int drbd_recv_short(struct drbd_conf *mdev, struct socket *sock,
481 void *buf, size_t size, int flags)
482{
483 mm_segment_t oldfs;
484 struct kvec iov = {
485 .iov_base = buf,
486 .iov_len = size,
487 };
488 struct msghdr msg = {
489 .msg_iovlen = 1,
490 .msg_iov = (struct iovec *)&iov,
491 .msg_flags = (flags ? flags : MSG_WAITALL | MSG_NOSIGNAL)
492 };
493 int rv;
494
495 oldfs = get_fs();
496 set_fs(KERNEL_DS);
497 rv = sock_recvmsg(sock, &msg, size, msg.msg_flags);
498 set_fs(oldfs);
499
500 return rv;
501}
502
503static int drbd_recv(struct drbd_conf *mdev, void *buf, size_t size)
504{
505 mm_segment_t oldfs;
506 struct kvec iov = {
507 .iov_base = buf,
508 .iov_len = size,
509 };
510 struct msghdr msg = {
511 .msg_iovlen = 1,
512 .msg_iov = (struct iovec *)&iov,
513 .msg_flags = MSG_WAITALL | MSG_NOSIGNAL
514 };
515 int rv;
516
517 oldfs = get_fs();
518 set_fs(KERNEL_DS);
519
520 for (;;) {
521 rv = sock_recvmsg(mdev->data.socket, &msg, size, msg.msg_flags);
522 if (rv == size)
523 break;
524
525 /* Note:
526 * ECONNRESET other side closed the connection
527 * ERESTARTSYS (on sock) we got a signal
528 */
529
530 if (rv < 0) {
531 if (rv == -ECONNRESET)
532 dev_info(DEV, "sock was reset by peer\n");
533 else if (rv != -ERESTARTSYS)
534 dev_err(DEV, "sock_recvmsg returned %d\n", rv);
535 break;
536 } else if (rv == 0) {
537 dev_info(DEV, "sock was shut down by peer\n");
538 break;
539 } else {
540 /* signal came in, or peer/link went down,
541 * after we read a partial message
542 */
543 /* D_ASSERT(signal_pending(current)); */
544 break;
545 }
546 };
547
548 set_fs(oldfs);
549
550 if (rv != size)
551 drbd_force_state(mdev, NS(conn, C_BROKEN_PIPE));
552
553 return rv;
554}
555
Lars Ellenberg5dbf1672010-05-25 16:18:01 +0200556/* quoting tcp(7):
557 * On individual connections, the socket buffer size must be set prior to the
558 * listen(2) or connect(2) calls in order to have it take effect.
559 * This is our wrapper to do so.
560 */
561static void drbd_setbufsize(struct socket *sock, unsigned int snd,
562 unsigned int rcv)
563{
564 /* open coded SO_SNDBUF, SO_RCVBUF */
565 if (snd) {
566 sock->sk->sk_sndbuf = snd;
567 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
568 }
569 if (rcv) {
570 sock->sk->sk_rcvbuf = rcv;
571 sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
572 }
573}
574
Philipp Reisnerb411b362009-09-25 16:07:19 -0700575static struct socket *drbd_try_connect(struct drbd_conf *mdev)
576{
577 const char *what;
578 struct socket *sock;
579 struct sockaddr_in6 src_in6;
580 int err;
581 int disconnect_on_error = 1;
582
583 if (!get_net_conf(mdev))
584 return NULL;
585
586 what = "sock_create_kern";
587 err = sock_create_kern(((struct sockaddr *)mdev->net_conf->my_addr)->sa_family,
588 SOCK_STREAM, IPPROTO_TCP, &sock);
589 if (err < 0) {
590 sock = NULL;
591 goto out;
592 }
593
594 sock->sk->sk_rcvtimeo =
595 sock->sk->sk_sndtimeo = mdev->net_conf->try_connect_int*HZ;
Lars Ellenberg5dbf1672010-05-25 16:18:01 +0200596 drbd_setbufsize(sock, mdev->net_conf->sndbuf_size,
597 mdev->net_conf->rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700598
599 /* explicitly bind to the configured IP as source IP
600 * for the outgoing connections.
601 * This is needed for multihomed hosts and to be
602 * able to use lo: interfaces for drbd.
603 * Make sure to use 0 as port number, so linux selects
604 * a free one dynamically.
605 */
606 memcpy(&src_in6, mdev->net_conf->my_addr,
607 min_t(int, mdev->net_conf->my_addr_len, sizeof(src_in6)));
608 if (((struct sockaddr *)mdev->net_conf->my_addr)->sa_family == AF_INET6)
609 src_in6.sin6_port = 0;
610 else
611 ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */
612
613 what = "bind before connect";
614 err = sock->ops->bind(sock,
615 (struct sockaddr *) &src_in6,
616 mdev->net_conf->my_addr_len);
617 if (err < 0)
618 goto out;
619
620 /* connect may fail, peer not yet available.
621 * stay C_WF_CONNECTION, don't go Disconnecting! */
622 disconnect_on_error = 0;
623 what = "connect";
624 err = sock->ops->connect(sock,
625 (struct sockaddr *)mdev->net_conf->peer_addr,
626 mdev->net_conf->peer_addr_len, 0);
627
628out:
629 if (err < 0) {
630 if (sock) {
631 sock_release(sock);
632 sock = NULL;
633 }
634 switch (-err) {
635 /* timeout, busy, signal pending */
636 case ETIMEDOUT: case EAGAIN: case EINPROGRESS:
637 case EINTR: case ERESTARTSYS:
638 /* peer not (yet) available, network problem */
639 case ECONNREFUSED: case ENETUNREACH:
640 case EHOSTDOWN: case EHOSTUNREACH:
641 disconnect_on_error = 0;
642 break;
643 default:
644 dev_err(DEV, "%s failed, err = %d\n", what, err);
645 }
646 if (disconnect_on_error)
647 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
648 }
649 put_net_conf(mdev);
650 return sock;
651}
652
653static struct socket *drbd_wait_for_connect(struct drbd_conf *mdev)
654{
655 int timeo, err;
656 struct socket *s_estab = NULL, *s_listen;
657 const char *what;
658
659 if (!get_net_conf(mdev))
660 return NULL;
661
662 what = "sock_create_kern";
663 err = sock_create_kern(((struct sockaddr *)mdev->net_conf->my_addr)->sa_family,
664 SOCK_STREAM, IPPROTO_TCP, &s_listen);
665 if (err) {
666 s_listen = NULL;
667 goto out;
668 }
669
670 timeo = mdev->net_conf->try_connect_int * HZ;
671 timeo += (random32() & 1) ? timeo / 7 : -timeo / 7; /* 28.5% random jitter */
672
673 s_listen->sk->sk_reuse = 1; /* SO_REUSEADDR */
674 s_listen->sk->sk_rcvtimeo = timeo;
675 s_listen->sk->sk_sndtimeo = timeo;
Lars Ellenberg5dbf1672010-05-25 16:18:01 +0200676 drbd_setbufsize(s_listen, mdev->net_conf->sndbuf_size,
677 mdev->net_conf->rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700678
679 what = "bind before listen";
680 err = s_listen->ops->bind(s_listen,
681 (struct sockaddr *) mdev->net_conf->my_addr,
682 mdev->net_conf->my_addr_len);
683 if (err < 0)
684 goto out;
685
686 err = drbd_accept(mdev, &what, s_listen, &s_estab);
687
688out:
689 if (s_listen)
690 sock_release(s_listen);
691 if (err < 0) {
692 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
693 dev_err(DEV, "%s failed, err = %d\n", what, err);
694 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
695 }
696 }
697 put_net_conf(mdev);
698
699 return s_estab;
700}
701
702static int drbd_send_fp(struct drbd_conf *mdev,
703 struct socket *sock, enum drbd_packets cmd)
704{
Philipp Reisner02918be2010-08-20 14:35:10 +0200705 struct p_header80 *h = &mdev->data.sbuf.header.h80;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700706
707 return _drbd_send_cmd(mdev, sock, cmd, h, sizeof(*h), 0);
708}
709
710static enum drbd_packets drbd_recv_fp(struct drbd_conf *mdev, struct socket *sock)
711{
Philipp Reisner02918be2010-08-20 14:35:10 +0200712 struct p_header80 *h = &mdev->data.rbuf.header.h80;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700713 int rr;
714
715 rr = drbd_recv_short(mdev, sock, h, sizeof(*h), 0);
716
Andreas Gruenbacherca9bc122011-01-11 13:47:24 +0100717 if (rr == sizeof(*h) && h->magic == cpu_to_be32(DRBD_MAGIC))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700718 return be16_to_cpu(h->command);
719
720 return 0xffff;
721}
722
723/**
724 * drbd_socket_okay() - Free the socket if its connection is not okay
725 * @mdev: DRBD device.
726 * @sock: pointer to the pointer to the socket.
727 */
728static int drbd_socket_okay(struct drbd_conf *mdev, struct socket **sock)
729{
730 int rr;
731 char tb[4];
732
733 if (!*sock)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100734 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700735
736 rr = drbd_recv_short(mdev, *sock, tb, 4, MSG_DONTWAIT | MSG_PEEK);
737
738 if (rr > 0 || rr == -EAGAIN) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100739 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700740 } else {
741 sock_release(*sock);
742 *sock = NULL;
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100743 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700744 }
745}
746
747/*
748 * return values:
749 * 1 yes, we have a valid connection
750 * 0 oops, did not work out, please try again
751 * -1 peer talks different language,
752 * no point in trying again, please go standalone.
753 * -2 We do not have a network config...
754 */
755static int drbd_connect(struct drbd_conf *mdev)
756{
757 struct socket *s, *sock, *msock;
758 int try, h, ok;
759
760 D_ASSERT(!mdev->data.socket);
761
Philipp Reisnerb411b362009-09-25 16:07:19 -0700762 if (drbd_request_state(mdev, NS(conn, C_WF_CONNECTION)) < SS_SUCCESS)
763 return -2;
764
765 clear_bit(DISCARD_CONCURRENT, &mdev->flags);
766
767 sock = NULL;
768 msock = NULL;
769
770 do {
771 for (try = 0;;) {
772 /* 3 tries, this should take less than a second! */
773 s = drbd_try_connect(mdev);
774 if (s || ++try >= 3)
775 break;
776 /* give the other side time to call bind() & listen() */
Philipp Reisner20ee6392011-01-18 15:28:59 +0100777 schedule_timeout_interruptible(HZ / 10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700778 }
779
780 if (s) {
781 if (!sock) {
782 drbd_send_fp(mdev, s, P_HAND_SHAKE_S);
783 sock = s;
784 s = NULL;
785 } else if (!msock) {
786 drbd_send_fp(mdev, s, P_HAND_SHAKE_M);
787 msock = s;
788 s = NULL;
789 } else {
790 dev_err(DEV, "Logic error in drbd_connect()\n");
791 goto out_release_sockets;
792 }
793 }
794
795 if (sock && msock) {
Philipp Reisnera8e40792011-05-13 12:03:55 +0200796 schedule_timeout_interruptible(mdev->net_conf->ping_timeo*HZ/10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700797 ok = drbd_socket_okay(mdev, &sock);
798 ok = drbd_socket_okay(mdev, &msock) && ok;
799 if (ok)
800 break;
801 }
802
803retry:
804 s = drbd_wait_for_connect(mdev);
805 if (s) {
806 try = drbd_recv_fp(mdev, s);
807 drbd_socket_okay(mdev, &sock);
808 drbd_socket_okay(mdev, &msock);
809 switch (try) {
810 case P_HAND_SHAKE_S:
811 if (sock) {
812 dev_warn(DEV, "initial packet S crossed\n");
813 sock_release(sock);
814 }
815 sock = s;
816 break;
817 case P_HAND_SHAKE_M:
818 if (msock) {
819 dev_warn(DEV, "initial packet M crossed\n");
820 sock_release(msock);
821 }
822 msock = s;
823 set_bit(DISCARD_CONCURRENT, &mdev->flags);
824 break;
825 default:
826 dev_warn(DEV, "Error receiving initial packet\n");
827 sock_release(s);
828 if (random32() & 1)
829 goto retry;
830 }
831 }
832
833 if (mdev->state.conn <= C_DISCONNECTING)
834 goto out_release_sockets;
835 if (signal_pending(current)) {
836 flush_signals(current);
837 smp_rmb();
838 if (get_t_state(&mdev->receiver) == Exiting)
839 goto out_release_sockets;
840 }
841
842 if (sock && msock) {
843 ok = drbd_socket_okay(mdev, &sock);
844 ok = drbd_socket_okay(mdev, &msock) && ok;
845 if (ok)
846 break;
847 }
848 } while (1);
849
850 msock->sk->sk_reuse = 1; /* SO_REUSEADDR */
851 sock->sk->sk_reuse = 1; /* SO_REUSEADDR */
852
853 sock->sk->sk_allocation = GFP_NOIO;
854 msock->sk->sk_allocation = GFP_NOIO;
855
856 sock->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
857 msock->sk->sk_priority = TC_PRIO_INTERACTIVE;
858
Philipp Reisnerb411b362009-09-25 16:07:19 -0700859 /* NOT YET ...
860 * sock->sk->sk_sndtimeo = mdev->net_conf->timeout*HZ/10;
861 * sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
862 * first set it to the P_HAND_SHAKE timeout,
863 * which we set to 4x the configured ping_timeout. */
864 sock->sk->sk_sndtimeo =
865 sock->sk->sk_rcvtimeo = mdev->net_conf->ping_timeo*4*HZ/10;
866
867 msock->sk->sk_sndtimeo = mdev->net_conf->timeout*HZ/10;
868 msock->sk->sk_rcvtimeo = mdev->net_conf->ping_int*HZ;
869
870 /* we don't want delays.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300871 * we use TCP_CORK where appropriate, though */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700872 drbd_tcp_nodelay(sock);
873 drbd_tcp_nodelay(msock);
874
875 mdev->data.socket = sock;
876 mdev->meta.socket = msock;
877 mdev->last_received = jiffies;
878
879 D_ASSERT(mdev->asender.task == NULL);
880
881 h = drbd_do_handshake(mdev);
882 if (h <= 0)
883 return h;
884
885 if (mdev->cram_hmac_tfm) {
886 /* drbd_request_state(mdev, NS(conn, WFAuth)); */
Johannes Thomab10d96c2010-01-07 16:02:50 +0100887 switch (drbd_do_auth(mdev)) {
888 case -1:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700889 dev_err(DEV, "Authentication of peer failed\n");
890 return -1;
Johannes Thomab10d96c2010-01-07 16:02:50 +0100891 case 0:
892 dev_err(DEV, "Authentication of peer failed, trying again.\n");
893 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700894 }
895 }
896
897 if (drbd_request_state(mdev, NS(conn, C_WF_REPORT_PARAMS)) < SS_SUCCESS)
898 return 0;
899
900 sock->sk->sk_sndtimeo = mdev->net_conf->timeout*HZ/10;
901 sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
902
903 atomic_set(&mdev->packet_seq, 0);
904 mdev->peer_seq = 0;
905
906 drbd_thread_start(&mdev->asender);
907
Philipp Reisner148efa12011-01-15 00:21:15 +0100908 if (drbd_send_protocol(mdev) == -1)
Philipp Reisner7e2455c2010-04-22 14:50:23 +0200909 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700910 drbd_send_sync_param(mdev, &mdev->sync_conf);
Philipp Reisnere89b5912010-03-24 17:11:33 +0100911 drbd_send_sizes(mdev, 0, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700912 drbd_send_uuids(mdev);
913 drbd_send_state(mdev);
914 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
915 clear_bit(RESIZE_PENDING, &mdev->flags);
Philipp Reisner7fde2be2011-03-01 11:08:28 +0100916 mod_timer(&mdev->request_timer, jiffies + HZ); /* just start it here. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700917
918 return 1;
919
920out_release_sockets:
921 if (sock)
922 sock_release(sock);
923 if (msock)
924 sock_release(msock);
925 return -1;
926}
927
Philipp Reisner02918be2010-08-20 14:35:10 +0200928static int drbd_recv_header(struct drbd_conf *mdev, enum drbd_packets *cmd, unsigned int *packet_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700929{
Philipp Reisner02918be2010-08-20 14:35:10 +0200930 union p_header *h = &mdev->data.rbuf.header;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700931 int r;
932
933 r = drbd_recv(mdev, h, sizeof(*h));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700934 if (unlikely(r != sizeof(*h))) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +0100935 if (!signal_pending(current))
936 dev_warn(DEV, "short read expecting header on sock: r=%d\n", r);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100937 return false;
Philipp Reisner02918be2010-08-20 14:35:10 +0200938 }
939
Andreas Gruenbacherca9bc122011-01-11 13:47:24 +0100940 if (likely(h->h80.magic == cpu_to_be32(DRBD_MAGIC))) {
Philipp Reisner02918be2010-08-20 14:35:10 +0200941 *cmd = be16_to_cpu(h->h80.command);
942 *packet_size = be16_to_cpu(h->h80.length);
Andreas Gruenbacherca9bc122011-01-11 13:47:24 +0100943 } else if (h->h95.magic == cpu_to_be16(DRBD_MAGIC_BIG)) {
Philipp Reisner02918be2010-08-20 14:35:10 +0200944 *cmd = be16_to_cpu(h->h95.command);
945 *packet_size = be32_to_cpu(h->h95.length);
946 } else {
Lars Ellenberg004352f2010-10-05 20:13:58 +0200947 dev_err(DEV, "magic?? on data m: 0x%08x c: %d l: %d\n",
948 be32_to_cpu(h->h80.magic),
949 be16_to_cpu(h->h80.command),
950 be16_to_cpu(h->h80.length));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100951 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700952 }
953 mdev->last_received = jiffies;
954
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100955 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700956}
957
Philipp Reisner2451fc32010-08-24 13:43:11 +0200958static void drbd_flush(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700959{
960 int rv;
961
962 if (mdev->write_ordering >= WO_bdev_flush && get_ldev(mdev)) {
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400963 rv = blkdev_issue_flush(mdev->ldev->backing_bdev, GFP_KERNEL,
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200964 NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700965 if (rv) {
966 dev_err(DEV, "local disk flush failed with status %d\n", rv);
967 /* would rather check on EOPNOTSUPP, but that is not reliable.
968 * don't try again for ANY return value != 0
969 * if (rv == -EOPNOTSUPP) */
970 drbd_bump_write_ordering(mdev, WO_drain_io);
971 }
972 put_ldev(mdev);
973 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700974}
975
976/**
977 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
978 * @mdev: DRBD device.
979 * @epoch: Epoch object.
980 * @ev: Epoch event.
981 */
982static enum finish_epoch drbd_may_finish_epoch(struct drbd_conf *mdev,
983 struct drbd_epoch *epoch,
984 enum epoch_event ev)
985{
Philipp Reisner2451fc32010-08-24 13:43:11 +0200986 int epoch_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700987 struct drbd_epoch *next_epoch;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700988 enum finish_epoch rv = FE_STILL_LIVE;
989
990 spin_lock(&mdev->epoch_lock);
991 do {
992 next_epoch = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700993
994 epoch_size = atomic_read(&epoch->epoch_size);
995
996 switch (ev & ~EV_CLEANUP) {
997 case EV_PUT:
998 atomic_dec(&epoch->active);
999 break;
1000 case EV_GOT_BARRIER_NR:
1001 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001002 break;
1003 case EV_BECAME_LAST:
1004 /* nothing to do*/
1005 break;
1006 }
1007
Philipp Reisnerb411b362009-09-25 16:07:19 -07001008 if (epoch_size != 0 &&
1009 atomic_read(&epoch->active) == 0 &&
Philipp Reisner2451fc32010-08-24 13:43:11 +02001010 test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001011 if (!(ev & EV_CLEANUP)) {
1012 spin_unlock(&mdev->epoch_lock);
1013 drbd_send_b_ack(mdev, epoch->barrier_nr, epoch_size);
1014 spin_lock(&mdev->epoch_lock);
1015 }
1016 dec_unacked(mdev);
1017
1018 if (mdev->current_epoch != epoch) {
1019 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1020 list_del(&epoch->list);
1021 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
1022 mdev->epochs--;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001023 kfree(epoch);
1024
1025 if (rv == FE_STILL_LIVE)
1026 rv = FE_DESTROYED;
1027 } else {
1028 epoch->flags = 0;
1029 atomic_set(&epoch->epoch_size, 0);
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001030 /* atomic_set(&epoch->active, 0); is already zero */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001031 if (rv == FE_STILL_LIVE)
1032 rv = FE_RECYCLED;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001033 wake_up(&mdev->ee_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001034 }
1035 }
1036
1037 if (!next_epoch)
1038 break;
1039
1040 epoch = next_epoch;
1041 } while (1);
1042
1043 spin_unlock(&mdev->epoch_lock);
1044
Philipp Reisnerb411b362009-09-25 16:07:19 -07001045 return rv;
1046}
1047
1048/**
1049 * drbd_bump_write_ordering() - Fall back to an other write ordering method
1050 * @mdev: DRBD device.
1051 * @wo: Write ordering method to try.
1052 */
1053void drbd_bump_write_ordering(struct drbd_conf *mdev, enum write_ordering_e wo) __must_hold(local)
1054{
1055 enum write_ordering_e pwo;
1056 static char *write_ordering_str[] = {
1057 [WO_none] = "none",
1058 [WO_drain_io] = "drain",
1059 [WO_bdev_flush] = "flush",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001060 };
1061
1062 pwo = mdev->write_ordering;
1063 wo = min(pwo, wo);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001064 if (wo == WO_bdev_flush && mdev->ldev->dc.no_disk_flush)
1065 wo = WO_drain_io;
1066 if (wo == WO_drain_io && mdev->ldev->dc.no_disk_drain)
1067 wo = WO_none;
1068 mdev->write_ordering = wo;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001069 if (pwo != mdev->write_ordering || wo == WO_bdev_flush)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001070 dev_info(DEV, "Method to ensure write ordering: %s\n", write_ordering_str[mdev->write_ordering]);
1071}
1072
1073/**
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001074 * drbd_submit_ee()
1075 * @mdev: DRBD device.
1076 * @e: epoch entry
1077 * @rw: flag field, see bio->bi_rw
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001078 *
1079 * May spread the pages to multiple bios,
1080 * depending on bio_add_page restrictions.
1081 *
1082 * Returns 0 if all bios have been submitted,
1083 * -ENOMEM if we could not allocate enough bios,
1084 * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a
1085 * single page to an empty bio (which should never happen and likely indicates
1086 * that the lower level IO stack is in some way broken). This has been observed
1087 * on certain Xen deployments.
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001088 */
1089/* TODO allocate from our own bio_set. */
1090int drbd_submit_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e,
1091 const unsigned rw, const int fault_type)
1092{
1093 struct bio *bios = NULL;
1094 struct bio *bio;
1095 struct page *page = e->pages;
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001096 sector_t sector = e->i.sector;
1097 unsigned ds = e->i.size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001098 unsigned n_bios = 0;
1099 unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001100 int err = -ENOMEM;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001101
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 }
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001112 /* > e->i.sector, unless this is the first bio */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001113 bio->bi_sector = sector;
1114 bio->bi_bdev = mdev->ldev->backing_bdev;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001115 bio->bi_rw = rw;
1116 bio->bi_private = e;
1117 bio->bi_end_io = drbd_endio_sec;
1118
1119 bio->bi_next = bios;
1120 bios = bio;
1121 ++n_bios;
1122
1123 page_chain_for_each(page) {
1124 unsigned len = min_t(unsigned, ds, PAGE_SIZE);
1125 if (!bio_add_page(bio, page, len, 0)) {
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001126 /* A single page must always be possible!
1127 * But in case it fails anyways,
1128 * we deal with it, and complain (below). */
1129 if (bio->bi_vcnt == 0) {
1130 dev_err(DEV,
1131 "bio_add_page failed for len=%u, "
1132 "bi_vcnt=0 (bi_sector=%llu)\n",
1133 len, (unsigned long long)bio->bi_sector);
1134 err = -ENOSPC;
1135 goto fail;
1136 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001137 goto next_bio;
1138 }
1139 ds -= len;
1140 sector += len >> 9;
1141 --nr_pages;
1142 }
1143 D_ASSERT(page == NULL);
1144 D_ASSERT(ds == 0);
1145
1146 atomic_set(&e->pending_bios, n_bios);
1147 do {
1148 bio = bios;
1149 bios = bios->bi_next;
1150 bio->bi_next = NULL;
1151
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001152 drbd_generic_make_request(mdev, fault_type, bio);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001153 } while (bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001154 return 0;
1155
1156fail:
1157 while (bios) {
1158 bio = bios;
1159 bios = bios->bi_next;
1160 bio_put(bio);
1161 }
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001162 return err;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001163}
1164
Philipp Reisner02918be2010-08-20 14:35:10 +02001165static int receive_Barrier(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001166{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001167 int rv;
Philipp Reisner02918be2010-08-20 14:35:10 +02001168 struct p_barrier *p = &mdev->data.rbuf.barrier;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001169 struct drbd_epoch *epoch;
1170
Philipp Reisnerb411b362009-09-25 16:07:19 -07001171 inc_unacked(mdev);
1172
Philipp Reisnerb411b362009-09-25 16:07:19 -07001173 mdev->current_epoch->barrier_nr = p->barrier;
1174 rv = drbd_may_finish_epoch(mdev, mdev->current_epoch, EV_GOT_BARRIER_NR);
1175
1176 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
1177 * the activity log, which means it would not be resynced in case the
1178 * R_PRIMARY crashes now.
1179 * Therefore we must send the barrier_ack after the barrier request was
1180 * completed. */
1181 switch (mdev->write_ordering) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001182 case WO_none:
1183 if (rv == FE_RECYCLED)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001184 return true;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001185
1186 /* receiver context, in the writeout path of the other node.
1187 * avoid potential distributed deadlock */
1188 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1189 if (epoch)
1190 break;
1191 else
1192 dev_warn(DEV, "Allocation of an epoch failed, slowing down\n");
1193 /* Fall through */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001194
1195 case WO_bdev_flush:
1196 case WO_drain_io:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001197 drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
Philipp Reisner2451fc32010-08-24 13:43:11 +02001198 drbd_flush(mdev);
1199
1200 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1201 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1202 if (epoch)
1203 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001204 }
1205
Philipp Reisner2451fc32010-08-24 13:43:11 +02001206 epoch = mdev->current_epoch;
1207 wait_event(mdev->ee_wait, atomic_read(&epoch->epoch_size) == 0);
1208
1209 D_ASSERT(atomic_read(&epoch->active) == 0);
1210 D_ASSERT(epoch->flags == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001211
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001212 return true;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001213 default:
1214 dev_err(DEV, "Strangeness in mdev->write_ordering %d\n", mdev->write_ordering);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001215 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001216 }
1217
1218 epoch->flags = 0;
1219 atomic_set(&epoch->epoch_size, 0);
1220 atomic_set(&epoch->active, 0);
1221
1222 spin_lock(&mdev->epoch_lock);
1223 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1224 list_add(&epoch->list, &mdev->current_epoch->list);
1225 mdev->current_epoch = epoch;
1226 mdev->epochs++;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001227 } else {
1228 /* The current_epoch got recycled while we allocated this one... */
1229 kfree(epoch);
1230 }
1231 spin_unlock(&mdev->epoch_lock);
1232
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001233 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001234}
1235
1236/* used from receive_RSDataReply (recv_resync_read)
1237 * and from receive_Data */
1238static struct drbd_epoch_entry *
1239read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector, int data_size) __must_hold(local)
1240{
Lars Ellenberg66660322010-04-06 12:15:04 +02001241 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001242 struct drbd_epoch_entry *e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001243 struct page *page;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001244 int dgs, ds, rr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001245 void *dig_in = mdev->int_dig_in;
1246 void *dig_vv = mdev->int_dig_vv;
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001247 unsigned long *data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001248
1249 dgs = (mdev->agreed_pro_version >= 87 && mdev->integrity_r_tfm) ?
1250 crypto_hash_digestsize(mdev->integrity_r_tfm) : 0;
1251
1252 if (dgs) {
1253 rr = drbd_recv(mdev, dig_in, dgs);
1254 if (rr != dgs) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01001255 if (!signal_pending(current))
1256 dev_warn(DEV,
1257 "short read receiving data digest: read %d expected %d\n",
1258 rr, dgs);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001259 return NULL;
1260 }
1261 }
1262
1263 data_size -= dgs;
1264
Philipp Reisnerd07c9c12011-01-20 16:49:33 +01001265 ERR_IF(data_size == 0) return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001266 ERR_IF(data_size & 0x1ff) return NULL;
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001267 ERR_IF(data_size > DRBD_MAX_BIO_SIZE) return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001268
Lars Ellenberg66660322010-04-06 12:15:04 +02001269 /* even though we trust out peer,
1270 * we sometimes have to double check. */
1271 if (sector + (data_size>>9) > capacity) {
Lars Ellenbergfdda6542011-01-24 15:11:01 +01001272 dev_err(DEV, "request from peer beyond end of local disk: "
1273 "capacity: %llus < sector: %llus + size: %u\n",
Lars Ellenberg66660322010-04-06 12:15:04 +02001274 (unsigned long long)capacity,
1275 (unsigned long long)sector, data_size);
1276 return NULL;
1277 }
1278
Philipp Reisnerb411b362009-09-25 16:07:19 -07001279 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1280 * "criss-cross" setup, that might cause write-out on some other DRBD,
1281 * which in turn might block on the other node at this very place. */
1282 e = drbd_alloc_ee(mdev, id, sector, data_size, GFP_NOIO);
1283 if (!e)
1284 return NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001285
Philipp Reisnerb411b362009-09-25 16:07:19 -07001286 ds = data_size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001287 page = e->pages;
1288 page_chain_for_each(page) {
1289 unsigned len = min_t(int, ds, PAGE_SIZE);
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001290 data = kmap(page);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001291 rr = drbd_recv(mdev, data, len);
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +01001292 if (drbd_insert_fault(mdev, DRBD_FAULT_RECEIVE)) {
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001293 dev_err(DEV, "Fault injection: Corrupting data on receive\n");
1294 data[0] = data[0] ^ (unsigned long)-1;
1295 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001296 kunmap(page);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001297 if (rr != len) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001298 drbd_free_ee(mdev, e);
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01001299 if (!signal_pending(current))
1300 dev_warn(DEV, "short read receiving data: read %d expected %d\n",
1301 rr, len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001302 return NULL;
1303 }
1304 ds -= rr;
1305 }
1306
1307 if (dgs) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001308 drbd_csum_ee(mdev, mdev->integrity_r_tfm, e, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001309 if (memcmp(dig_in, dig_vv, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001310 dev_err(DEV, "Digest integrity check FAILED: %llus +%u\n",
1311 (unsigned long long)sector, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001312 drbd_bcast_ee(mdev, "digest failed",
1313 dgs, dig_in, dig_vv, e);
1314 drbd_free_ee(mdev, e);
1315 return NULL;
1316 }
1317 }
1318 mdev->recv_cnt += data_size>>9;
1319 return e;
1320}
1321
1322/* drbd_drain_block() just takes a data block
1323 * out of the socket input buffer, and discards it.
1324 */
1325static int drbd_drain_block(struct drbd_conf *mdev, int data_size)
1326{
1327 struct page *page;
1328 int rr, rv = 1;
1329 void *data;
1330
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001331 if (!data_size)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001332 return true;
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001333
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001334 page = drbd_pp_alloc(mdev, 1, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001335
1336 data = kmap(page);
1337 while (data_size) {
1338 rr = drbd_recv(mdev, data, min_t(int, data_size, PAGE_SIZE));
1339 if (rr != min_t(int, data_size, PAGE_SIZE)) {
1340 rv = 0;
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01001341 if (!signal_pending(current))
1342 dev_warn(DEV,
1343 "short read receiving data: read %d expected %d\n",
1344 rr, min_t(int, data_size, PAGE_SIZE));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001345 break;
1346 }
1347 data_size -= rr;
1348 }
1349 kunmap(page);
Lars Ellenberg435f0742010-09-06 12:30:25 +02001350 drbd_pp_free(mdev, page, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001351 return rv;
1352}
1353
1354static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req,
1355 sector_t sector, int data_size)
1356{
1357 struct bio_vec *bvec;
1358 struct bio *bio;
1359 int dgs, rr, i, expect;
1360 void *dig_in = mdev->int_dig_in;
1361 void *dig_vv = mdev->int_dig_vv;
1362
1363 dgs = (mdev->agreed_pro_version >= 87 && mdev->integrity_r_tfm) ?
1364 crypto_hash_digestsize(mdev->integrity_r_tfm) : 0;
1365
1366 if (dgs) {
1367 rr = drbd_recv(mdev, dig_in, dgs);
1368 if (rr != dgs) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01001369 if (!signal_pending(current))
1370 dev_warn(DEV,
1371 "short read receiving data reply digest: read %d expected %d\n",
1372 rr, dgs);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001373 return 0;
1374 }
1375 }
1376
1377 data_size -= dgs;
1378
1379 /* optimistically update recv_cnt. if receiving fails below,
1380 * we disconnect anyways, and counters will be reset. */
1381 mdev->recv_cnt += data_size>>9;
1382
1383 bio = req->master_bio;
1384 D_ASSERT(sector == bio->bi_sector);
1385
1386 bio_for_each_segment(bvec, bio, i) {
1387 expect = min_t(int, data_size, bvec->bv_len);
1388 rr = drbd_recv(mdev,
1389 kmap(bvec->bv_page)+bvec->bv_offset,
1390 expect);
1391 kunmap(bvec->bv_page);
1392 if (rr != expect) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01001393 if (!signal_pending(current))
1394 dev_warn(DEV, "short read receiving data reply: "
1395 "read %d expected %d\n",
1396 rr, expect);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001397 return 0;
1398 }
1399 data_size -= rr;
1400 }
1401
1402 if (dgs) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001403 drbd_csum_bio(mdev, mdev->integrity_r_tfm, bio, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001404 if (memcmp(dig_in, dig_vv, dgs)) {
1405 dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n");
1406 return 0;
1407 }
1408 }
1409
1410 D_ASSERT(data_size == 0);
1411 return 1;
1412}
1413
1414/* e_end_resync_block() is called via
1415 * drbd_process_done_ee() by asender only */
1416static int e_end_resync_block(struct drbd_conf *mdev, struct drbd_work *w, int unused)
1417{
1418 struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w;
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001419 sector_t sector = e->i.sector;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001420 int ok;
1421
Bart Van Assche24c48302011-05-21 18:32:29 +02001422 D_ASSERT(hlist_unhashed(&e->collision));
Andreas Gruenbacher8b946252011-01-20 15:23:07 +01001423 D_ASSERT(drbd_interval_empty(&e->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001424
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001425 if (likely((e->flags & EE_WAS_ERROR) == 0)) {
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001426 drbd_set_in_sync(mdev, sector, e->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001427 ok = drbd_send_ack(mdev, P_RS_WRITE_ACK, e);
1428 } else {
1429 /* Record failure to sync */
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001430 drbd_rs_failed_io(mdev, sector, e->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001431
1432 ok = drbd_send_ack(mdev, P_NEG_ACK, e);
1433 }
1434 dec_unacked(mdev);
1435
1436 return ok;
1437}
1438
1439static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local)
1440{
1441 struct drbd_epoch_entry *e;
1442
1443 e = read_in_block(mdev, ID_SYNCER, sector, data_size);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001444 if (!e)
1445 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001446
1447 dec_rs_pending(mdev);
1448
Philipp Reisnerb411b362009-09-25 16:07:19 -07001449 inc_unacked(mdev);
1450 /* corresponding dec_unacked() in e_end_resync_block()
1451 * respective _drbd_clear_done_ee */
1452
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001453 e->w.cb = e_end_resync_block;
1454
Philipp Reisnerb411b362009-09-25 16:07:19 -07001455 spin_lock_irq(&mdev->req_lock);
1456 list_add(&e->w.list, &mdev->sync_ee);
1457 spin_unlock_irq(&mdev->req_lock);
1458
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001459 atomic_add(data_size >> 9, &mdev->rs_sect_ev);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001460 if (drbd_submit_ee(mdev, e, WRITE, DRBD_FAULT_RS_WR) == 0)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001461 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001462
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001463 /* don't care for the reason here */
1464 dev_err(DEV, "submit failed, triggering re-connect\n");
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001465 spin_lock_irq(&mdev->req_lock);
1466 list_del(&e->w.list);
1467 spin_unlock_irq(&mdev->req_lock);
1468
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001469 drbd_free_ee(mdev, e);
1470fail:
1471 put_ldev(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001472 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001473}
1474
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001475static struct drbd_request *
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001476find_request(struct drbd_conf *mdev, struct rb_root *root, u64 id,
1477 sector_t sector, bool missing_ok, const char *func)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001478{
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001479 struct drbd_request *req;
1480
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001481 /* Request object according to our peer */
1482 req = (struct drbd_request *)(unsigned long)id;
1483 if (drbd_contains_interval(root, sector, &req->i))
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001484 return req;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001485 if (!missing_ok) {
1486 dev_err(DEV, "%s: failed to find request %lu, sector %llus\n", func,
1487 (unsigned long)id, (unsigned long long)sector);
1488 }
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001489 return NULL;
1490}
1491
Philipp Reisner02918be2010-08-20 14:35:10 +02001492static int receive_DataReply(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001493{
1494 struct drbd_request *req;
1495 sector_t sector;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001496 int ok;
Philipp Reisner02918be2010-08-20 14:35:10 +02001497 struct p_data *p = &mdev->data.rbuf.data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001498
1499 sector = be64_to_cpu(p->sector);
1500
1501 spin_lock_irq(&mdev->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001502 req = find_request(mdev, &mdev->read_requests, p->block_id, sector, false, __func__);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001503 spin_unlock_irq(&mdev->req_lock);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001504 if (unlikely(!req))
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001505 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001506
Bart Van Assche24c48302011-05-21 18:32:29 +02001507 /* hlist_del(&req->collision) is done in _req_may_be_done, to avoid
Philipp Reisnerb411b362009-09-25 16:07:19 -07001508 * special casing it there for the various failure cases.
1509 * still no race with drbd_fail_pending_reads */
1510 ok = recv_dless_read(mdev, req, sector, data_size);
1511
1512 if (ok)
1513 req_mod(req, data_received);
1514 /* else: nothing. handled from drbd_disconnect...
1515 * I don't think we may complete this just yet
1516 * in case we are "on-disconnect: freeze" */
1517
1518 return ok;
1519}
1520
Philipp Reisner02918be2010-08-20 14:35:10 +02001521static int receive_RSDataReply(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001522{
1523 sector_t sector;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001524 int ok;
Philipp Reisner02918be2010-08-20 14:35:10 +02001525 struct p_data *p = &mdev->data.rbuf.data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001526
1527 sector = be64_to_cpu(p->sector);
1528 D_ASSERT(p->block_id == ID_SYNCER);
1529
1530 if (get_ldev(mdev)) {
1531 /* data is submitted to disk within recv_resync_read.
1532 * corresponding put_ldev done below on error,
Andreas Gruenbacher9c508422011-01-14 21:19:36 +01001533 * or in drbd_endio_sec. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001534 ok = recv_resync_read(mdev, sector, data_size);
1535 } else {
1536 if (__ratelimit(&drbd_ratelimit_state))
1537 dev_err(DEV, "Can not write resync data to local disk.\n");
1538
1539 ok = drbd_drain_block(mdev, data_size);
1540
Lars Ellenberg2b2bf212010-10-06 11:46:55 +02001541 drbd_send_ack_dp(mdev, P_NEG_ACK, p, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001542 }
1543
Philipp Reisner778f2712010-07-06 11:14:00 +02001544 atomic_add(data_size >> 9, &mdev->rs_sect_in);
1545
Philipp Reisnerb411b362009-09-25 16:07:19 -07001546 return ok;
1547}
1548
1549/* e_end_block() is called via drbd_process_done_ee().
1550 * this means this function only runs in the asender thread
1551 */
1552static int e_end_block(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1553{
1554 struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w;
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001555 sector_t sector = e->i.sector;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001556 int ok = 1, pcmd;
1557
Philipp Reisnerb411b362009-09-25 16:07:19 -07001558 if (mdev->net_conf->wire_protocol == DRBD_PROT_C) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001559 if (likely((e->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001560 pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
1561 mdev->state.conn <= C_PAUSED_SYNC_T &&
1562 e->flags & EE_MAY_SET_IN_SYNC) ?
1563 P_RS_WRITE_ACK : P_WRITE_ACK;
1564 ok &= drbd_send_ack(mdev, pcmd, e);
1565 if (pcmd == P_RS_WRITE_ACK)
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001566 drbd_set_in_sync(mdev, sector, e->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001567 } else {
1568 ok = drbd_send_ack(mdev, P_NEG_ACK, e);
1569 /* we expect it to be marked out of sync anyways...
1570 * maybe assert this? */
1571 }
1572 dec_unacked(mdev);
1573 }
1574 /* we delete from the conflict detection hash _after_ we sent out the
1575 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
1576 if (mdev->net_conf->two_primaries) {
1577 spin_lock_irq(&mdev->req_lock);
Bart Van Assche24c48302011-05-21 18:32:29 +02001578 D_ASSERT(!hlist_unhashed(&e->collision));
1579 hlist_del_init(&e->collision);
Andreas Gruenbacher8b946252011-01-20 15:23:07 +01001580 D_ASSERT(!drbd_interval_empty(&e->i));
1581 drbd_remove_interval(&mdev->epoch_entries, &e->i);
1582 drbd_clear_interval(&e->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001583 spin_unlock_irq(&mdev->req_lock);
1584 } else {
Bart Van Assche24c48302011-05-21 18:32:29 +02001585 D_ASSERT(hlist_unhashed(&e->collision));
Andreas Gruenbacher8b946252011-01-20 15:23:07 +01001586 D_ASSERT(drbd_interval_empty(&e->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001587 }
1588
1589 drbd_may_finish_epoch(mdev, e->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
1590
1591 return ok;
1592}
1593
1594static int e_send_discard_ack(struct drbd_conf *mdev, struct drbd_work *w, int unused)
1595{
1596 struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w;
1597 int ok = 1;
1598
1599 D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
1600 ok = drbd_send_ack(mdev, P_DISCARD_ACK, e);
1601
1602 spin_lock_irq(&mdev->req_lock);
Bart Van Assche24c48302011-05-21 18:32:29 +02001603 D_ASSERT(!hlist_unhashed(&e->collision));
1604 hlist_del_init(&e->collision);
Andreas Gruenbacher8b946252011-01-20 15:23:07 +01001605 D_ASSERT(!drbd_interval_empty(&e->i));
1606 drbd_remove_interval(&mdev->epoch_entries, &e->i);
1607 drbd_clear_interval(&e->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001608 spin_unlock_irq(&mdev->req_lock);
1609
1610 dec_unacked(mdev);
1611
1612 return ok;
1613}
1614
1615/* Called from receive_Data.
1616 * Synchronize packets on sock with packets on msock.
1617 *
1618 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
1619 * packet traveling on msock, they are still processed in the order they have
1620 * been sent.
1621 *
1622 * Note: we don't care for Ack packets overtaking P_DATA packets.
1623 *
1624 * In case packet_seq is larger than mdev->peer_seq number, there are
1625 * outstanding packets on the msock. We wait for them to arrive.
1626 * In case we are the logically next packet, we update mdev->peer_seq
1627 * ourselves. Correctly handles 32bit wrap around.
1628 *
1629 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
1630 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
1631 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
1632 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
1633 *
1634 * returns 0 if we may process the packet,
1635 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
1636static int drbd_wait_peer_seq(struct drbd_conf *mdev, const u32 packet_seq)
1637{
1638 DEFINE_WAIT(wait);
1639 unsigned int p_seq;
1640 long timeout;
1641 int ret = 0;
1642 spin_lock(&mdev->peer_seq_lock);
1643 for (;;) {
1644 prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
1645 if (seq_le(packet_seq, mdev->peer_seq+1))
1646 break;
1647 if (signal_pending(current)) {
1648 ret = -ERESTARTSYS;
1649 break;
1650 }
1651 p_seq = mdev->peer_seq;
1652 spin_unlock(&mdev->peer_seq_lock);
1653 timeout = schedule_timeout(30*HZ);
1654 spin_lock(&mdev->peer_seq_lock);
1655 if (timeout == 0 && p_seq == mdev->peer_seq) {
1656 ret = -ETIMEDOUT;
1657 dev_err(DEV, "ASSERT FAILED waited 30 seconds for sequence update, forcing reconnect\n");
1658 break;
1659 }
1660 }
1661 finish_wait(&mdev->seq_wait, &wait);
1662 if (mdev->peer_seq+1 == packet_seq)
1663 mdev->peer_seq++;
1664 spin_unlock(&mdev->peer_seq_lock);
1665 return ret;
1666}
1667
Lars Ellenberg688593c2010-11-17 22:25:03 +01001668/* see also bio_flags_to_wire()
1669 * DRBD_REQ_*, because we need to semantically map the flags to data packet
1670 * flags and back. We may replicate to other kernel versions. */
1671static unsigned long wire_flags_to_bio(struct drbd_conf *mdev, u32 dpf)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001672{
Lars Ellenberg688593c2010-11-17 22:25:03 +01001673 return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
1674 (dpf & DP_FUA ? REQ_FUA : 0) |
1675 (dpf & DP_FLUSH ? REQ_FLUSH : 0) |
1676 (dpf & DP_DISCARD ? REQ_DISCARD : 0);
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001677}
1678
Philipp Reisnerb411b362009-09-25 16:07:19 -07001679/* mirrored write */
Philipp Reisner02918be2010-08-20 14:35:10 +02001680static int receive_Data(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001681{
1682 sector_t sector;
1683 struct drbd_epoch_entry *e;
Philipp Reisner02918be2010-08-20 14:35:10 +02001684 struct p_data *p = &mdev->data.rbuf.data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001685 int rw = WRITE;
1686 u32 dp_flags;
1687
Philipp Reisnerb411b362009-09-25 16:07:19 -07001688 if (!get_ldev(mdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001689 spin_lock(&mdev->peer_seq_lock);
1690 if (mdev->peer_seq+1 == be32_to_cpu(p->seq_num))
1691 mdev->peer_seq++;
1692 spin_unlock(&mdev->peer_seq_lock);
1693
Lars Ellenberg2b2bf212010-10-06 11:46:55 +02001694 drbd_send_ack_dp(mdev, P_NEG_ACK, p, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001695 atomic_inc(&mdev->current_epoch->epoch_size);
1696 return drbd_drain_block(mdev, data_size);
1697 }
1698
1699 /* get_ldev(mdev) successful.
1700 * Corresponding put_ldev done either below (on various errors),
Andreas Gruenbacher9c508422011-01-14 21:19:36 +01001701 * or in drbd_endio_sec, if we successfully submit the data at
Philipp Reisnerb411b362009-09-25 16:07:19 -07001702 * the end of this function. */
1703
1704 sector = be64_to_cpu(p->sector);
1705 e = read_in_block(mdev, p->block_id, sector, data_size);
1706 if (!e) {
1707 put_ldev(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001708 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001709 }
1710
Philipp Reisnerb411b362009-09-25 16:07:19 -07001711 e->w.cb = e_end_block;
1712
Lars Ellenberg688593c2010-11-17 22:25:03 +01001713 dp_flags = be32_to_cpu(p->dp_flags);
1714 rw |= wire_flags_to_bio(mdev, dp_flags);
1715
1716 if (dp_flags & DP_MAY_SET_IN_SYNC)
1717 e->flags |= EE_MAY_SET_IN_SYNC;
1718
Philipp Reisnerb411b362009-09-25 16:07:19 -07001719 spin_lock(&mdev->epoch_lock);
1720 e->epoch = mdev->current_epoch;
1721 atomic_inc(&e->epoch->epoch_size);
1722 atomic_inc(&e->epoch->active);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001723 spin_unlock(&mdev->epoch_lock);
1724
Philipp Reisnerb411b362009-09-25 16:07:19 -07001725 /* I'm the receiver, I do hold a net_cnt reference. */
1726 if (!mdev->net_conf->two_primaries) {
1727 spin_lock_irq(&mdev->req_lock);
1728 } else {
1729 /* don't get the req_lock yet,
1730 * we may sleep in drbd_wait_peer_seq */
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001731 const int size = e->i.size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001732 const int discard = test_bit(DISCARD_CONCURRENT, &mdev->flags);
1733 DEFINE_WAIT(wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001734 int first;
1735
1736 D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
1737 BUG_ON(mdev->ee_hash == NULL);
1738 BUG_ON(mdev->tl_hash == NULL);
1739
1740 /* conflict detection and handling:
1741 * 1. wait on the sequence number,
1742 * in case this data packet overtook ACK packets.
1743 * 2. check our hash tables for conflicting requests.
1744 * we only need to walk the tl_hash, since an ee can not
1745 * have a conflict with an other ee: on the submitting
1746 * node, the corresponding req had already been conflicting,
1747 * and a conflicting req is never sent.
1748 *
1749 * Note: for two_primaries, we are protocol C,
1750 * so there cannot be any request that is DONE
1751 * but still on the transfer log.
1752 *
1753 * unconditionally add to the ee_hash.
1754 *
1755 * if no conflicting request is found:
1756 * submit.
1757 *
1758 * if any conflicting request is found
1759 * that has not yet been acked,
1760 * AND I have the "discard concurrent writes" flag:
1761 * queue (via done_ee) the P_DISCARD_ACK; OUT.
1762 *
1763 * if any conflicting request is found:
1764 * block the receiver, waiting on misc_wait
1765 * until no more conflicting requests are there,
1766 * or we get interrupted (disconnect).
1767 *
1768 * we do not just write after local io completion of those
1769 * requests, but only after req is done completely, i.e.
1770 * we wait for the P_DISCARD_ACK to arrive!
1771 *
1772 * then proceed normally, i.e. submit.
1773 */
1774 if (drbd_wait_peer_seq(mdev, be32_to_cpu(p->seq_num)))
1775 goto out_interrupted;
1776
1777 spin_lock_irq(&mdev->req_lock);
1778
Bart Van Assche24c48302011-05-21 18:32:29 +02001779 hlist_add_head(&e->collision, ee_hash_slot(mdev, sector));
Andreas Gruenbacher8b946252011-01-20 15:23:07 +01001780 drbd_insert_interval(&mdev->epoch_entries, &e->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001781
Philipp Reisnerb411b362009-09-25 16:07:19 -07001782 first = 1;
1783 for (;;) {
Andreas Gruenbacherde696712011-01-20 15:00:24 +01001784 struct drbd_interval *i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001785 int have_unacked = 0;
1786 int have_conflict = 0;
1787 prepare_to_wait(&mdev->misc_wait, &wait,
1788 TASK_INTERRUPTIBLE);
Andreas Gruenbacherde696712011-01-20 15:00:24 +01001789
1790 i = drbd_find_overlap(&mdev->write_requests, sector, size);
1791 if (i) {
1792 struct drbd_request *req2 =
1793 container_of(i, struct drbd_request, i);
1794
1795 /* only ALERT on first iteration,
1796 * we may be woken up early... */
1797 if (first)
1798 dev_alert(DEV, "%s[%u] Concurrent local write detected!"
1799 " new: %llus +%u; pending: %llus +%u\n",
1800 current->comm, current->pid,
1801 (unsigned long long)sector, size,
1802 (unsigned long long)req2->i.sector, req2->i.size);
1803 if (req2->rq_state & RQ_NET_PENDING)
1804 ++have_unacked;
1805 ++have_conflict;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001806 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001807 if (!have_conflict)
1808 break;
1809
1810 /* Discard Ack only for the _first_ iteration */
1811 if (first && discard && have_unacked) {
1812 dev_alert(DEV, "Concurrent write! [DISCARD BY FLAG] sec=%llus\n",
1813 (unsigned long long)sector);
1814 inc_unacked(mdev);
1815 e->w.cb = e_send_discard_ack;
1816 list_add_tail(&e->w.list, &mdev->done_ee);
1817
1818 spin_unlock_irq(&mdev->req_lock);
1819
1820 /* we could probably send that P_DISCARD_ACK ourselves,
1821 * but I don't like the receiver using the msock */
1822
1823 put_ldev(mdev);
1824 wake_asender(mdev);
1825 finish_wait(&mdev->misc_wait, &wait);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001826 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001827 }
1828
1829 if (signal_pending(current)) {
Bart Van Assche24c48302011-05-21 18:32:29 +02001830 hlist_del_init(&e->collision);
Andreas Gruenbacher8b946252011-01-20 15:23:07 +01001831 drbd_remove_interval(&mdev->epoch_entries, &e->i);
1832 drbd_clear_interval(&e->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001833
1834 spin_unlock_irq(&mdev->req_lock);
1835
1836 finish_wait(&mdev->misc_wait, &wait);
1837 goto out_interrupted;
1838 }
1839
1840 spin_unlock_irq(&mdev->req_lock);
1841 if (first) {
1842 first = 0;
1843 dev_alert(DEV, "Concurrent write! [W AFTERWARDS] "
1844 "sec=%llus\n", (unsigned long long)sector);
1845 } else if (discard) {
1846 /* we had none on the first iteration.
1847 * there must be none now. */
1848 D_ASSERT(have_unacked == 0);
1849 }
1850 schedule();
1851 spin_lock_irq(&mdev->req_lock);
1852 }
1853 finish_wait(&mdev->misc_wait, &wait);
1854 }
1855
1856 list_add(&e->w.list, &mdev->active_ee);
1857 spin_unlock_irq(&mdev->req_lock);
1858
1859 switch (mdev->net_conf->wire_protocol) {
1860 case DRBD_PROT_C:
1861 inc_unacked(mdev);
1862 /* corresponding dec_unacked() in e_end_block()
1863 * respective _drbd_clear_done_ee */
1864 break;
1865 case DRBD_PROT_B:
1866 /* I really don't like it that the receiver thread
1867 * sends on the msock, but anyways */
1868 drbd_send_ack(mdev, P_RECV_ACK, e);
1869 break;
1870 case DRBD_PROT_A:
1871 /* nothing to do */
1872 break;
1873 }
1874
Lars Ellenberg6719fb02010-10-18 23:04:07 +02001875 if (mdev->state.pdsk < D_INCONSISTENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001876 /* In case we have the only disk of the cluster, */
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001877 drbd_set_out_of_sync(mdev, e->i.sector, e->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001878 e->flags |= EE_CALL_AL_COMPLETE_IO;
Lars Ellenberg6719fb02010-10-18 23:04:07 +02001879 e->flags &= ~EE_MAY_SET_IN_SYNC;
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001880 drbd_al_begin_io(mdev, e->i.sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001881 }
1882
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001883 if (drbd_submit_ee(mdev, e, rw, DRBD_FAULT_DT_WR) == 0)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001884 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001885
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001886 /* don't care for the reason here */
1887 dev_err(DEV, "submit failed, triggering re-connect\n");
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001888 spin_lock_irq(&mdev->req_lock);
1889 list_del(&e->w.list);
Bart Van Assche24c48302011-05-21 18:32:29 +02001890 hlist_del_init(&e->collision);
Andreas Gruenbacher8b946252011-01-20 15:23:07 +01001891 drbd_remove_interval(&mdev->epoch_entries, &e->i);
1892 drbd_clear_interval(&e->i);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001893 spin_unlock_irq(&mdev->req_lock);
1894 if (e->flags & EE_CALL_AL_COMPLETE_IO)
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001895 drbd_al_complete_io(mdev, e->i.sector);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001896
Philipp Reisnerb411b362009-09-25 16:07:19 -07001897out_interrupted:
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001898 drbd_may_finish_epoch(mdev, e->epoch, EV_PUT + EV_CLEANUP);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001899 put_ldev(mdev);
1900 drbd_free_ee(mdev, e);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001901 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001902}
1903
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001904/* We may throttle resync, if the lower device seems to be busy,
1905 * and current sync rate is above c_min_rate.
1906 *
1907 * To decide whether or not the lower device is busy, we use a scheme similar
1908 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
1909 * (more than 64 sectors) of activity we cannot account for with our own resync
1910 * activity, it obviously is "busy".
1911 *
1912 * The current sync rate used here uses only the most recent two step marks,
1913 * to have a short time average so we can react faster.
1914 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01001915int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001916{
1917 struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
1918 unsigned long db, dt, dbdt;
Philipp Reisnere3555d82010-11-07 15:56:29 +01001919 struct lc_element *tmp;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001920 int curr_events;
1921 int throttle = 0;
1922
1923 /* feature disabled? */
1924 if (mdev->sync_conf.c_min_rate == 0)
1925 return 0;
1926
Philipp Reisnere3555d82010-11-07 15:56:29 +01001927 spin_lock_irq(&mdev->al_lock);
1928 tmp = lc_find(mdev->resync, BM_SECT_TO_EXT(sector));
1929 if (tmp) {
1930 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
1931 if (test_bit(BME_PRIORITY, &bm_ext->flags)) {
1932 spin_unlock_irq(&mdev->al_lock);
1933 return 0;
1934 }
1935 /* Do not slow down if app IO is already waiting for this extent */
1936 }
1937 spin_unlock_irq(&mdev->al_lock);
1938
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001939 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
1940 (int)part_stat_read(&disk->part0, sectors[1]) -
1941 atomic_read(&mdev->rs_sect_ev);
Philipp Reisnere3555d82010-11-07 15:56:29 +01001942
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001943 if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
1944 unsigned long rs_left;
1945 int i;
1946
1947 mdev->rs_last_events = curr_events;
1948
1949 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
1950 * approx. */
Lars Ellenberg2649f082010-11-05 10:05:47 +01001951 i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
1952
1953 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
1954 rs_left = mdev->ov_left;
1955 else
1956 rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001957
1958 dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
1959 if (!dt)
1960 dt++;
1961 db = mdev->rs_mark_left[i] - rs_left;
1962 dbdt = Bit2KB(db/dt);
1963
1964 if (dbdt > mdev->sync_conf.c_min_rate)
1965 throttle = 1;
1966 }
1967 return throttle;
1968}
1969
1970
Philipp Reisner02918be2010-08-20 14:35:10 +02001971static int receive_DataRequest(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int digest_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001972{
1973 sector_t sector;
1974 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
1975 struct drbd_epoch_entry *e;
1976 struct digest_info *di = NULL;
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02001977 int size, verb;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001978 unsigned int fault_type;
Philipp Reisner02918be2010-08-20 14:35:10 +02001979 struct p_block_req *p = &mdev->data.rbuf.block_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001980
1981 sector = be64_to_cpu(p->sector);
1982 size = be32_to_cpu(p->blksize);
1983
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001984 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001985 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
1986 (unsigned long long)sector, size);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001987 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001988 }
1989 if (sector + (size>>9) > capacity) {
1990 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
1991 (unsigned long long)sector, size);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001992 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001993 }
1994
1995 if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02001996 verb = 1;
1997 switch (cmd) {
1998 case P_DATA_REQUEST:
1999 drbd_send_ack_rp(mdev, P_NEG_DREPLY, p);
2000 break;
2001 case P_RS_DATA_REQUEST:
2002 case P_CSUM_RS_REQUEST:
2003 case P_OV_REQUEST:
2004 drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p);
2005 break;
2006 case P_OV_REPLY:
2007 verb = 0;
2008 dec_rs_pending(mdev);
2009 drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC);
2010 break;
2011 default:
2012 dev_err(DEV, "unexpected command (%s) in receive_DataRequest\n",
2013 cmdname(cmd));
2014 }
2015 if (verb && __ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002016 dev_err(DEV, "Can not satisfy peer's read request, "
2017 "no local data.\n");
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002018
Lars Ellenberga821cc42010-09-06 12:31:37 +02002019 /* drain possibly payload */
2020 return drbd_drain_block(mdev, digest_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002021 }
2022
2023 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
2024 * "criss-cross" setup, that might cause write-out on some other DRBD,
2025 * which in turn might block on the other node at this very place. */
2026 e = drbd_alloc_ee(mdev, p->block_id, sector, size, GFP_NOIO);
2027 if (!e) {
2028 put_ldev(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002029 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002030 }
2031
Philipp Reisner02918be2010-08-20 14:35:10 +02002032 switch (cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002033 case P_DATA_REQUEST:
2034 e->w.cb = w_e_end_data_req;
2035 fault_type = DRBD_FAULT_DT_RD;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002036 /* application IO, don't drbd_rs_begin_io */
2037 goto submit;
2038
Philipp Reisnerb411b362009-09-25 16:07:19 -07002039 case P_RS_DATA_REQUEST:
2040 e->w.cb = w_e_end_rsdata_req;
2041 fault_type = DRBD_FAULT_RS_RD;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002042 /* used in the sector offset progress display */
2043 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002044 break;
2045
2046 case P_OV_REPLY:
2047 case P_CSUM_RS_REQUEST:
2048 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002049 di = kmalloc(sizeof(*di) + digest_size, GFP_NOIO);
2050 if (!di)
2051 goto out_free_e;
2052
2053 di->digest_size = digest_size;
2054 di->digest = (((char *)di)+sizeof(struct digest_info));
2055
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +02002056 e->digest = di;
2057 e->flags |= EE_HAS_DIGEST;
2058
Philipp Reisnerb411b362009-09-25 16:07:19 -07002059 if (drbd_recv(mdev, di->digest, digest_size) != digest_size)
2060 goto out_free_e;
2061
Philipp Reisner02918be2010-08-20 14:35:10 +02002062 if (cmd == P_CSUM_RS_REQUEST) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002063 D_ASSERT(mdev->agreed_pro_version >= 89);
2064 e->w.cb = w_e_end_csum_rs_req;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002065 /* used in the sector offset progress display */
2066 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisner02918be2010-08-20 14:35:10 +02002067 } else if (cmd == P_OV_REPLY) {
Lars Ellenberg2649f082010-11-05 10:05:47 +01002068 /* track progress, we may need to throttle */
2069 atomic_add(size >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002070 e->w.cb = w_e_end_ov_reply;
2071 dec_rs_pending(mdev);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002072 /* drbd_rs_begin_io done when we sent this request,
2073 * but accounting still needs to be done. */
2074 goto submit_for_resync;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002075 }
2076 break;
2077
2078 case P_OV_REQUEST:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002079 if (mdev->ov_start_sector == ~(sector_t)0 &&
2080 mdev->agreed_pro_version >= 90) {
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002081 unsigned long now = jiffies;
2082 int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002083 mdev->ov_start_sector = sector;
2084 mdev->ov_position = sector;
Lars Ellenberg30b743a2010-11-05 09:39:06 +01002085 mdev->ov_left = drbd_bm_bits(mdev) - BM_SECT_TO_BIT(sector);
2086 mdev->rs_total = mdev->ov_left;
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002087 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
2088 mdev->rs_mark_left[i] = mdev->ov_left;
2089 mdev->rs_mark_time[i] = now;
2090 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002091 dev_info(DEV, "Online Verify start sector: %llu\n",
2092 (unsigned long long)sector);
2093 }
2094 e->w.cb = w_e_end_ov_req;
2095 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002096 break;
2097
Philipp Reisnerb411b362009-09-25 16:07:19 -07002098 default:
2099 dev_err(DEV, "unexpected command (%s) in receive_DataRequest\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02002100 cmdname(cmd));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002101 fault_type = DRBD_FAULT_MAX;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002102 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002103 }
2104
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002105 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2106 * wrt the receiver, but it is not as straightforward as it may seem.
2107 * Various places in the resync start and stop logic assume resync
2108 * requests are processed in order, requeuing this on the worker thread
2109 * introduces a bunch of new code for synchronization between threads.
2110 *
2111 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2112 * "forever", throttling after drbd_rs_begin_io will lock that extent
2113 * for application writes for the same time. For now, just throttle
2114 * here, where the rest of the code expects the receiver to sleep for
2115 * a while, anyways.
2116 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002117
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002118 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2119 * this defers syncer requests for some time, before letting at least
2120 * on request through. The resync controller on the receiving side
2121 * will adapt to the incoming rate accordingly.
2122 *
2123 * We cannot throttle here if remote is Primary/SyncTarget:
2124 * we would also throttle its application reads.
2125 * In that case, throttling is done on the SyncTarget only.
2126 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002127 if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev, sector))
2128 schedule_timeout_uninterruptible(HZ/10);
2129 if (drbd_rs_begin_io(mdev, sector))
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002130 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002131
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002132submit_for_resync:
2133 atomic_add(size >> 9, &mdev->rs_sect_ev);
2134
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002135submit:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002136 inc_unacked(mdev);
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002137 spin_lock_irq(&mdev->req_lock);
2138 list_add_tail(&e->w.list, &mdev->read_ee);
2139 spin_unlock_irq(&mdev->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002140
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002141 if (drbd_submit_ee(mdev, e, READ, fault_type) == 0)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002142 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002143
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002144 /* don't care for the reason here */
2145 dev_err(DEV, "submit failed, triggering re-connect\n");
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002146 spin_lock_irq(&mdev->req_lock);
2147 list_del(&e->w.list);
2148 spin_unlock_irq(&mdev->req_lock);
2149 /* no drbd_rs_complete_io(), we are dropping the connection anyways */
2150
Philipp Reisnerb411b362009-09-25 16:07:19 -07002151out_free_e:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002152 put_ldev(mdev);
2153 drbd_free_ee(mdev, e);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002154 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002155}
2156
2157static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
2158{
2159 int self, peer, rv = -100;
2160 unsigned long ch_self, ch_peer;
2161
2162 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2163 peer = mdev->p_uuid[UI_BITMAP] & 1;
2164
2165 ch_peer = mdev->p_uuid[UI_SIZE];
2166 ch_self = mdev->comm_bm_set;
2167
2168 switch (mdev->net_conf->after_sb_0p) {
2169 case ASB_CONSENSUS:
2170 case ASB_DISCARD_SECONDARY:
2171 case ASB_CALL_HELPER:
2172 dev_err(DEV, "Configuration error.\n");
2173 break;
2174 case ASB_DISCONNECT:
2175 break;
2176 case ASB_DISCARD_YOUNGER_PRI:
2177 if (self == 0 && peer == 1) {
2178 rv = -1;
2179 break;
2180 }
2181 if (self == 1 && peer == 0) {
2182 rv = 1;
2183 break;
2184 }
2185 /* Else fall through to one of the other strategies... */
2186 case ASB_DISCARD_OLDER_PRI:
2187 if (self == 0 && peer == 1) {
2188 rv = 1;
2189 break;
2190 }
2191 if (self == 1 && peer == 0) {
2192 rv = -1;
2193 break;
2194 }
2195 /* Else fall through to one of the other strategies... */
Lars Ellenbergad19bf62009-10-14 09:36:49 +02002196 dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
Philipp Reisnerb411b362009-09-25 16:07:19 -07002197 "Using discard-least-changes instead\n");
2198 case ASB_DISCARD_ZERO_CHG:
2199 if (ch_peer == 0 && ch_self == 0) {
2200 rv = test_bit(DISCARD_CONCURRENT, &mdev->flags)
2201 ? -1 : 1;
2202 break;
2203 } else {
2204 if (ch_peer == 0) { rv = 1; break; }
2205 if (ch_self == 0) { rv = -1; break; }
2206 }
2207 if (mdev->net_conf->after_sb_0p == ASB_DISCARD_ZERO_CHG)
2208 break;
2209 case ASB_DISCARD_LEAST_CHG:
2210 if (ch_self < ch_peer)
2211 rv = -1;
2212 else if (ch_self > ch_peer)
2213 rv = 1;
2214 else /* ( ch_self == ch_peer ) */
2215 /* Well, then use something else. */
2216 rv = test_bit(DISCARD_CONCURRENT, &mdev->flags)
2217 ? -1 : 1;
2218 break;
2219 case ASB_DISCARD_LOCAL:
2220 rv = -1;
2221 break;
2222 case ASB_DISCARD_REMOTE:
2223 rv = 1;
2224 }
2225
2226 return rv;
2227}
2228
2229static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
2230{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002231 int hg, rv = -100;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002232
2233 switch (mdev->net_conf->after_sb_1p) {
2234 case ASB_DISCARD_YOUNGER_PRI:
2235 case ASB_DISCARD_OLDER_PRI:
2236 case ASB_DISCARD_LEAST_CHG:
2237 case ASB_DISCARD_LOCAL:
2238 case ASB_DISCARD_REMOTE:
2239 dev_err(DEV, "Configuration error.\n");
2240 break;
2241 case ASB_DISCONNECT:
2242 break;
2243 case ASB_CONSENSUS:
2244 hg = drbd_asb_recover_0p(mdev);
2245 if (hg == -1 && mdev->state.role == R_SECONDARY)
2246 rv = hg;
2247 if (hg == 1 && mdev->state.role == R_PRIMARY)
2248 rv = hg;
2249 break;
2250 case ASB_VIOLENTLY:
2251 rv = drbd_asb_recover_0p(mdev);
2252 break;
2253 case ASB_DISCARD_SECONDARY:
2254 return mdev->state.role == R_PRIMARY ? 1 : -1;
2255 case ASB_CALL_HELPER:
2256 hg = drbd_asb_recover_0p(mdev);
2257 if (hg == -1 && mdev->state.role == R_PRIMARY) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002258 enum drbd_state_rv rv2;
2259
2260 drbd_set_role(mdev, R_SECONDARY, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002261 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2262 * we might be here in C_WF_REPORT_PARAMS which is transient.
2263 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002264 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2265 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002266 drbd_khelper(mdev, "pri-lost-after-sb");
2267 } else {
2268 dev_warn(DEV, "Successfully gave up primary role.\n");
2269 rv = hg;
2270 }
2271 } else
2272 rv = hg;
2273 }
2274
2275 return rv;
2276}
2277
2278static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
2279{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002280 int hg, rv = -100;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002281
2282 switch (mdev->net_conf->after_sb_2p) {
2283 case ASB_DISCARD_YOUNGER_PRI:
2284 case ASB_DISCARD_OLDER_PRI:
2285 case ASB_DISCARD_LEAST_CHG:
2286 case ASB_DISCARD_LOCAL:
2287 case ASB_DISCARD_REMOTE:
2288 case ASB_CONSENSUS:
2289 case ASB_DISCARD_SECONDARY:
2290 dev_err(DEV, "Configuration error.\n");
2291 break;
2292 case ASB_VIOLENTLY:
2293 rv = drbd_asb_recover_0p(mdev);
2294 break;
2295 case ASB_DISCONNECT:
2296 break;
2297 case ASB_CALL_HELPER:
2298 hg = drbd_asb_recover_0p(mdev);
2299 if (hg == -1) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002300 enum drbd_state_rv rv2;
2301
Philipp Reisnerb411b362009-09-25 16:07:19 -07002302 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2303 * we might be here in C_WF_REPORT_PARAMS which is transient.
2304 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002305 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2306 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002307 drbd_khelper(mdev, "pri-lost-after-sb");
2308 } else {
2309 dev_warn(DEV, "Successfully gave up primary role.\n");
2310 rv = hg;
2311 }
2312 } else
2313 rv = hg;
2314 }
2315
2316 return rv;
2317}
2318
2319static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
2320 u64 bits, u64 flags)
2321{
2322 if (!uuid) {
2323 dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
2324 return;
2325 }
2326 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
2327 text,
2328 (unsigned long long)uuid[UI_CURRENT],
2329 (unsigned long long)uuid[UI_BITMAP],
2330 (unsigned long long)uuid[UI_HISTORY_START],
2331 (unsigned long long)uuid[UI_HISTORY_END],
2332 (unsigned long long)bits,
2333 (unsigned long long)flags);
2334}
2335
2336/*
2337 100 after split brain try auto recover
2338 2 C_SYNC_SOURCE set BitMap
2339 1 C_SYNC_SOURCE use BitMap
2340 0 no Sync
2341 -1 C_SYNC_TARGET use BitMap
2342 -2 C_SYNC_TARGET set BitMap
2343 -100 after split brain, disconnect
2344-1000 unrelated data
Philipp Reisner4a23f262011-01-11 17:42:17 +01002345-1091 requires proto 91
2346-1096 requires proto 96
Philipp Reisnerb411b362009-09-25 16:07:19 -07002347 */
2348static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
2349{
2350 u64 self, peer;
2351 int i, j;
2352
2353 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2354 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2355
2356 *rule_nr = 10;
2357 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
2358 return 0;
2359
2360 *rule_nr = 20;
2361 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
2362 peer != UUID_JUST_CREATED)
2363 return -2;
2364
2365 *rule_nr = 30;
2366 if (self != UUID_JUST_CREATED &&
2367 (peer == UUID_JUST_CREATED || peer == (u64)0))
2368 return 2;
2369
2370 if (self == peer) {
2371 int rct, dc; /* roles at crash time */
2372
2373 if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
2374
2375 if (mdev->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002376 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002377
2378 if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
2379 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
2380 dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
2381 drbd_uuid_set_bm(mdev, 0UL);
2382
2383 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2384 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2385 *rule_nr = 34;
2386 } else {
2387 dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
2388 *rule_nr = 36;
2389 }
2390
2391 return 1;
2392 }
2393
2394 if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
2395
2396 if (mdev->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002397 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002398
2399 if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
2400 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
2401 dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
2402
2403 mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
2404 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
2405 mdev->p_uuid[UI_BITMAP] = 0UL;
2406
2407 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2408 *rule_nr = 35;
2409 } else {
2410 dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
2411 *rule_nr = 37;
2412 }
2413
2414 return -1;
2415 }
2416
2417 /* Common power [off|failure] */
2418 rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
2419 (mdev->p_uuid[UI_FLAGS] & 2);
2420 /* lowest bit is set when we were primary,
2421 * next bit (weight 2) is set when peer was primary */
2422 *rule_nr = 40;
2423
2424 switch (rct) {
2425 case 0: /* !self_pri && !peer_pri */ return 0;
2426 case 1: /* self_pri && !peer_pri */ return 1;
2427 case 2: /* !self_pri && peer_pri */ return -1;
2428 case 3: /* self_pri && peer_pri */
2429 dc = test_bit(DISCARD_CONCURRENT, &mdev->flags);
2430 return dc ? -1 : 1;
2431 }
2432 }
2433
2434 *rule_nr = 50;
2435 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2436 if (self == peer)
2437 return -1;
2438
2439 *rule_nr = 51;
2440 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2441 if (self == peer) {
Philipp Reisner4a23f262011-01-11 17:42:17 +01002442 if (mdev->agreed_pro_version < 96 ?
2443 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) ==
2444 (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) :
2445 peer + UUID_NEW_BM_OFFSET == (mdev->p_uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002446 /* The last P_SYNC_UUID did not get though. Undo the last start of
2447 resync as sync source modifications of the peer's UUIDs. */
2448
2449 if (mdev->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002450 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002451
2452 mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
2453 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
Philipp Reisner4a23f262011-01-11 17:42:17 +01002454
2455 dev_info(DEV, "Did not got last syncUUID packet, corrected:\n");
2456 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2457
Philipp Reisnerb411b362009-09-25 16:07:19 -07002458 return -1;
2459 }
2460 }
2461
2462 *rule_nr = 60;
2463 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2464 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2465 peer = mdev->p_uuid[i] & ~((u64)1);
2466 if (self == peer)
2467 return -2;
2468 }
2469
2470 *rule_nr = 70;
2471 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2472 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2473 if (self == peer)
2474 return 1;
2475
2476 *rule_nr = 71;
2477 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2478 if (self == peer) {
Philipp Reisner4a23f262011-01-11 17:42:17 +01002479 if (mdev->agreed_pro_version < 96 ?
2480 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) ==
2481 (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) :
2482 self + UUID_NEW_BM_OFFSET == (mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002483 /* The last P_SYNC_UUID did not get though. Undo the last start of
2484 resync as sync source modifications of our UUIDs. */
2485
2486 if (mdev->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002487 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002488
2489 _drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
2490 _drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
2491
Philipp Reisner4a23f262011-01-11 17:42:17 +01002492 dev_info(DEV, "Last syncUUID did not get through, corrected:\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002493 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2494 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2495
2496 return 1;
2497 }
2498 }
2499
2500
2501 *rule_nr = 80;
Philipp Reisnerd8c2a362009-11-18 15:52:51 +01002502 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002503 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2504 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2505 if (self == peer)
2506 return 2;
2507 }
2508
2509 *rule_nr = 90;
2510 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2511 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2512 if (self == peer && self != ((u64)0))
2513 return 100;
2514
2515 *rule_nr = 100;
2516 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2517 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2518 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
2519 peer = mdev->p_uuid[j] & ~((u64)1);
2520 if (self == peer)
2521 return -100;
2522 }
2523 }
2524
2525 return -1000;
2526}
2527
2528/* drbd_sync_handshake() returns the new conn state on success, or
2529 CONN_MASK (-1) on failure.
2530 */
2531static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
2532 enum drbd_disk_state peer_disk) __must_hold(local)
2533{
2534 int hg, rule_nr;
2535 enum drbd_conns rv = C_MASK;
2536 enum drbd_disk_state mydisk;
2537
2538 mydisk = mdev->state.disk;
2539 if (mydisk == D_NEGOTIATING)
2540 mydisk = mdev->new_state_tmp.disk;
2541
2542 dev_info(DEV, "drbd_sync_handshake:\n");
2543 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0);
2544 drbd_uuid_dump(mdev, "peer", mdev->p_uuid,
2545 mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2546
2547 hg = drbd_uuid_compare(mdev, &rule_nr);
2548
2549 dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
2550
2551 if (hg == -1000) {
2552 dev_alert(DEV, "Unrelated data, aborting!\n");
2553 return C_MASK;
2554 }
Philipp Reisner4a23f262011-01-11 17:42:17 +01002555 if (hg < -1000) {
2556 dev_alert(DEV, "To resolve this both sides have to support at least protocol %d\n", -hg - 1000);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002557 return C_MASK;
2558 }
2559
2560 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
2561 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
2562 int f = (hg == -100) || abs(hg) == 2;
2563 hg = mydisk > D_INCONSISTENT ? 1 : -1;
2564 if (f)
2565 hg = hg*2;
2566 dev_info(DEV, "Becoming sync %s due to disk states.\n",
2567 hg > 0 ? "source" : "target");
2568 }
2569
Adam Gandelman3a11a482010-04-08 16:48:23 -07002570 if (abs(hg) == 100)
2571 drbd_khelper(mdev, "initial-split-brain");
2572
Philipp Reisnerb411b362009-09-25 16:07:19 -07002573 if (hg == 100 || (hg == -100 && mdev->net_conf->always_asbp)) {
2574 int pcount = (mdev->state.role == R_PRIMARY)
2575 + (peer_role == R_PRIMARY);
2576 int forced = (hg == -100);
2577
2578 switch (pcount) {
2579 case 0:
2580 hg = drbd_asb_recover_0p(mdev);
2581 break;
2582 case 1:
2583 hg = drbd_asb_recover_1p(mdev);
2584 break;
2585 case 2:
2586 hg = drbd_asb_recover_2p(mdev);
2587 break;
2588 }
2589 if (abs(hg) < 100) {
2590 dev_warn(DEV, "Split-Brain detected, %d primaries, "
2591 "automatically solved. Sync from %s node\n",
2592 pcount, (hg < 0) ? "peer" : "this");
2593 if (forced) {
2594 dev_warn(DEV, "Doing a full sync, since"
2595 " UUIDs where ambiguous.\n");
2596 hg = hg*2;
2597 }
2598 }
2599 }
2600
2601 if (hg == -100) {
2602 if (mdev->net_conf->want_lose && !(mdev->p_uuid[UI_FLAGS]&1))
2603 hg = -1;
2604 if (!mdev->net_conf->want_lose && (mdev->p_uuid[UI_FLAGS]&1))
2605 hg = 1;
2606
2607 if (abs(hg) < 100)
2608 dev_warn(DEV, "Split-Brain detected, manually solved. "
2609 "Sync from %s node\n",
2610 (hg < 0) ? "peer" : "this");
2611 }
2612
2613 if (hg == -100) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01002614 /* FIXME this log message is not correct if we end up here
2615 * after an attempted attach on a diskless node.
2616 * We just refuse to attach -- well, we drop the "connection"
2617 * to that disk, in a way... */
Adam Gandelman3a11a482010-04-08 16:48:23 -07002618 dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002619 drbd_khelper(mdev, "split-brain");
2620 return C_MASK;
2621 }
2622
2623 if (hg > 0 && mydisk <= D_INCONSISTENT) {
2624 dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
2625 return C_MASK;
2626 }
2627
2628 if (hg < 0 && /* by intention we do not use mydisk here. */
2629 mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
2630 switch (mdev->net_conf->rr_conflict) {
2631 case ASB_CALL_HELPER:
2632 drbd_khelper(mdev, "pri-lost");
2633 /* fall through */
2634 case ASB_DISCONNECT:
2635 dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
2636 return C_MASK;
2637 case ASB_VIOLENTLY:
2638 dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
2639 "assumption\n");
2640 }
2641 }
2642
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002643 if (mdev->net_conf->dry_run || test_bit(CONN_DRY_RUN, &mdev->flags)) {
2644 if (hg == 0)
2645 dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
2646 else
2647 dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
2648 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
2649 abs(hg) >= 2 ? "full" : "bit-map based");
2650 return C_MASK;
2651 }
2652
Philipp Reisnerb411b362009-09-25 16:07:19 -07002653 if (abs(hg) >= 2) {
2654 dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002655 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake",
2656 BM_LOCKED_SET_ALLOWED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002657 return C_MASK;
2658 }
2659
2660 if (hg > 0) { /* become sync source. */
2661 rv = C_WF_BITMAP_S;
2662 } else if (hg < 0) { /* become sync target */
2663 rv = C_WF_BITMAP_T;
2664 } else {
2665 rv = C_CONNECTED;
2666 if (drbd_bm_total_weight(mdev)) {
2667 dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
2668 drbd_bm_total_weight(mdev));
2669 }
2670 }
2671
2672 return rv;
2673}
2674
2675/* returns 1 if invalid */
2676static int cmp_after_sb(enum drbd_after_sb_p peer, enum drbd_after_sb_p self)
2677{
2678 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
2679 if ((peer == ASB_DISCARD_REMOTE && self == ASB_DISCARD_LOCAL) ||
2680 (self == ASB_DISCARD_REMOTE && peer == ASB_DISCARD_LOCAL))
2681 return 0;
2682
2683 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
2684 if (peer == ASB_DISCARD_REMOTE || peer == ASB_DISCARD_LOCAL ||
2685 self == ASB_DISCARD_REMOTE || self == ASB_DISCARD_LOCAL)
2686 return 1;
2687
2688 /* everything else is valid if they are equal on both sides. */
2689 if (peer == self)
2690 return 0;
2691
2692 /* everything es is invalid. */
2693 return 1;
2694}
2695
Philipp Reisner02918be2010-08-20 14:35:10 +02002696static int receive_protocol(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002697{
Philipp Reisner02918be2010-08-20 14:35:10 +02002698 struct p_protocol *p = &mdev->data.rbuf.protocol;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002699 int p_proto, p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002700 int p_want_lose, p_two_primaries, cf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002701 char p_integrity_alg[SHARED_SECRET_MAX] = "";
2702
Philipp Reisnerb411b362009-09-25 16:07:19 -07002703 p_proto = be32_to_cpu(p->protocol);
2704 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
2705 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
2706 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002707 p_two_primaries = be32_to_cpu(p->two_primaries);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002708 cf = be32_to_cpu(p->conn_flags);
2709 p_want_lose = cf & CF_WANT_LOSE;
2710
2711 clear_bit(CONN_DRY_RUN, &mdev->flags);
2712
2713 if (cf & CF_DRY_RUN)
2714 set_bit(CONN_DRY_RUN, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002715
2716 if (p_proto != mdev->net_conf->wire_protocol) {
2717 dev_err(DEV, "incompatible communication protocols\n");
2718 goto disconnect;
2719 }
2720
2721 if (cmp_after_sb(p_after_sb_0p, mdev->net_conf->after_sb_0p)) {
2722 dev_err(DEV, "incompatible after-sb-0pri settings\n");
2723 goto disconnect;
2724 }
2725
2726 if (cmp_after_sb(p_after_sb_1p, mdev->net_conf->after_sb_1p)) {
2727 dev_err(DEV, "incompatible after-sb-1pri settings\n");
2728 goto disconnect;
2729 }
2730
2731 if (cmp_after_sb(p_after_sb_2p, mdev->net_conf->after_sb_2p)) {
2732 dev_err(DEV, "incompatible after-sb-2pri settings\n");
2733 goto disconnect;
2734 }
2735
2736 if (p_want_lose && mdev->net_conf->want_lose) {
2737 dev_err(DEV, "both sides have the 'want_lose' flag set\n");
2738 goto disconnect;
2739 }
2740
2741 if (p_two_primaries != mdev->net_conf->two_primaries) {
2742 dev_err(DEV, "incompatible setting of the two-primaries options\n");
2743 goto disconnect;
2744 }
2745
2746 if (mdev->agreed_pro_version >= 87) {
2747 unsigned char *my_alg = mdev->net_conf->integrity_alg;
2748
2749 if (drbd_recv(mdev, p_integrity_alg, data_size) != data_size)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002750 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002751
2752 p_integrity_alg[SHARED_SECRET_MAX-1] = 0;
2753 if (strcmp(p_integrity_alg, my_alg)) {
2754 dev_err(DEV, "incompatible setting of the data-integrity-alg\n");
2755 goto disconnect;
2756 }
2757 dev_info(DEV, "data-integrity-alg: %s\n",
2758 my_alg[0] ? my_alg : (unsigned char *)"<not-used>");
2759 }
2760
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002761 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002762
2763disconnect:
2764 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002765 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002766}
2767
2768/* helper function
2769 * input: alg name, feature name
2770 * return: NULL (alg name was "")
2771 * ERR_PTR(error) if something goes wrong
2772 * or the crypto hash ptr, if it worked out ok. */
2773struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
2774 const char *alg, const char *name)
2775{
2776 struct crypto_hash *tfm;
2777
2778 if (!alg[0])
2779 return NULL;
2780
2781 tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
2782 if (IS_ERR(tfm)) {
2783 dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
2784 alg, name, PTR_ERR(tfm));
2785 return tfm;
2786 }
2787 if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) {
2788 crypto_free_hash(tfm);
2789 dev_err(DEV, "\"%s\" is not a digest (%s)\n", alg, name);
2790 return ERR_PTR(-EINVAL);
2791 }
2792 return tfm;
2793}
2794
Philipp Reisner02918be2010-08-20 14:35:10 +02002795static int receive_SyncParam(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int packet_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002796{
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002797 int ok = true;
Philipp Reisner02918be2010-08-20 14:35:10 +02002798 struct p_rs_param_95 *p = &mdev->data.rbuf.rs_param_95;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002799 unsigned int header_size, data_size, exp_max_sz;
2800 struct crypto_hash *verify_tfm = NULL;
2801 struct crypto_hash *csums_tfm = NULL;
2802 const int apv = mdev->agreed_pro_version;
Philipp Reisner778f2712010-07-06 11:14:00 +02002803 int *rs_plan_s = NULL;
2804 int fifo_size = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002805
2806 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
2807 : apv == 88 ? sizeof(struct p_rs_param)
2808 + SHARED_SECRET_MAX
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002809 : apv <= 94 ? sizeof(struct p_rs_param_89)
2810 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002811
Philipp Reisner02918be2010-08-20 14:35:10 +02002812 if (packet_size > exp_max_sz) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002813 dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02002814 packet_size, exp_max_sz);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002815 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002816 }
2817
2818 if (apv <= 88) {
Philipp Reisner02918be2010-08-20 14:35:10 +02002819 header_size = sizeof(struct p_rs_param) - sizeof(struct p_header80);
2820 data_size = packet_size - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002821 } else if (apv <= 94) {
Philipp Reisner02918be2010-08-20 14:35:10 +02002822 header_size = sizeof(struct p_rs_param_89) - sizeof(struct p_header80);
2823 data_size = packet_size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002824 D_ASSERT(data_size == 0);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002825 } else {
Philipp Reisner02918be2010-08-20 14:35:10 +02002826 header_size = sizeof(struct p_rs_param_95) - sizeof(struct p_header80);
2827 data_size = packet_size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002828 D_ASSERT(data_size == 0);
2829 }
2830
2831 /* initialize verify_alg and csums_alg */
2832 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
2833
Philipp Reisner02918be2010-08-20 14:35:10 +02002834 if (drbd_recv(mdev, &p->head.payload, header_size) != header_size)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002835 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002836
2837 mdev->sync_conf.rate = be32_to_cpu(p->rate);
2838
2839 if (apv >= 88) {
2840 if (apv == 88) {
2841 if (data_size > SHARED_SECRET_MAX) {
2842 dev_err(DEV, "verify-alg too long, "
2843 "peer wants %u, accepting only %u byte\n",
2844 data_size, SHARED_SECRET_MAX);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002845 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002846 }
2847
2848 if (drbd_recv(mdev, p->verify_alg, data_size) != data_size)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002849 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002850
2851 /* we expect NUL terminated string */
2852 /* but just in case someone tries to be evil */
2853 D_ASSERT(p->verify_alg[data_size-1] == 0);
2854 p->verify_alg[data_size-1] = 0;
2855
2856 } else /* apv >= 89 */ {
2857 /* we still expect NUL terminated strings */
2858 /* but just in case someone tries to be evil */
2859 D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
2860 D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
2861 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
2862 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
2863 }
2864
2865 if (strcmp(mdev->sync_conf.verify_alg, p->verify_alg)) {
2866 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
2867 dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
2868 mdev->sync_conf.verify_alg, p->verify_alg);
2869 goto disconnect;
2870 }
2871 verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
2872 p->verify_alg, "verify-alg");
2873 if (IS_ERR(verify_tfm)) {
2874 verify_tfm = NULL;
2875 goto disconnect;
2876 }
2877 }
2878
2879 if (apv >= 89 && strcmp(mdev->sync_conf.csums_alg, p->csums_alg)) {
2880 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
2881 dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
2882 mdev->sync_conf.csums_alg, p->csums_alg);
2883 goto disconnect;
2884 }
2885 csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
2886 p->csums_alg, "csums-alg");
2887 if (IS_ERR(csums_tfm)) {
2888 csums_tfm = NULL;
2889 goto disconnect;
2890 }
2891 }
2892
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002893 if (apv > 94) {
2894 mdev->sync_conf.rate = be32_to_cpu(p->rate);
2895 mdev->sync_conf.c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
2896 mdev->sync_conf.c_delay_target = be32_to_cpu(p->c_delay_target);
2897 mdev->sync_conf.c_fill_target = be32_to_cpu(p->c_fill_target);
2898 mdev->sync_conf.c_max_rate = be32_to_cpu(p->c_max_rate);
Philipp Reisner778f2712010-07-06 11:14:00 +02002899
2900 fifo_size = (mdev->sync_conf.c_plan_ahead * 10 * SLEEP_TIME) / HZ;
2901 if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) {
2902 rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL);
2903 if (!rs_plan_s) {
2904 dev_err(DEV, "kmalloc of fifo_buffer failed");
2905 goto disconnect;
2906 }
2907 }
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002908 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002909
2910 spin_lock(&mdev->peer_seq_lock);
2911 /* lock against drbd_nl_syncer_conf() */
2912 if (verify_tfm) {
2913 strcpy(mdev->sync_conf.verify_alg, p->verify_alg);
2914 mdev->sync_conf.verify_alg_len = strlen(p->verify_alg) + 1;
2915 crypto_free_hash(mdev->verify_tfm);
2916 mdev->verify_tfm = verify_tfm;
2917 dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
2918 }
2919 if (csums_tfm) {
2920 strcpy(mdev->sync_conf.csums_alg, p->csums_alg);
2921 mdev->sync_conf.csums_alg_len = strlen(p->csums_alg) + 1;
2922 crypto_free_hash(mdev->csums_tfm);
2923 mdev->csums_tfm = csums_tfm;
2924 dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
2925 }
Philipp Reisner778f2712010-07-06 11:14:00 +02002926 if (fifo_size != mdev->rs_plan_s.size) {
2927 kfree(mdev->rs_plan_s.values);
2928 mdev->rs_plan_s.values = rs_plan_s;
2929 mdev->rs_plan_s.size = fifo_size;
2930 mdev->rs_planed = 0;
2931 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002932 spin_unlock(&mdev->peer_seq_lock);
2933 }
2934
2935 return ok;
2936disconnect:
2937 /* just for completeness: actually not needed,
2938 * as this is not reached if csums_tfm was ok. */
2939 crypto_free_hash(csums_tfm);
2940 /* but free the verify_tfm again, if csums_tfm did not work out */
2941 crypto_free_hash(verify_tfm);
2942 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002943 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002944}
2945
Philipp Reisnerb411b362009-09-25 16:07:19 -07002946/* warn if the arguments differ by more than 12.5% */
2947static void warn_if_differ_considerably(struct drbd_conf *mdev,
2948 const char *s, sector_t a, sector_t b)
2949{
2950 sector_t d;
2951 if (a == 0 || b == 0)
2952 return;
2953 d = (a > b) ? (a - b) : (b - a);
2954 if (d > (a>>3) || d > (b>>3))
2955 dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
2956 (unsigned long long)a, (unsigned long long)b);
2957}
2958
Philipp Reisner02918be2010-08-20 14:35:10 +02002959static int receive_sizes(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002960{
Philipp Reisner02918be2010-08-20 14:35:10 +02002961 struct p_sizes *p = &mdev->data.rbuf.sizes;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002962 enum determine_dev_size dd = unchanged;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002963 sector_t p_size, p_usize, my_usize;
2964 int ldsc = 0; /* local disk size changed */
Philipp Reisnere89b5912010-03-24 17:11:33 +01002965 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002966
Philipp Reisnerb411b362009-09-25 16:07:19 -07002967 p_size = be64_to_cpu(p->d_size);
2968 p_usize = be64_to_cpu(p->u_size);
2969
2970 if (p_size == 0 && mdev->state.disk == D_DISKLESS) {
2971 dev_err(DEV, "some backing storage is needed\n");
2972 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002973 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002974 }
2975
2976 /* just store the peer's disk size for now.
2977 * we still need to figure out whether we accept that. */
2978 mdev->p_size = p_size;
2979
Philipp Reisnerb411b362009-09-25 16:07:19 -07002980 if (get_ldev(mdev)) {
2981 warn_if_differ_considerably(mdev, "lower level device sizes",
2982 p_size, drbd_get_max_capacity(mdev->ldev));
2983 warn_if_differ_considerably(mdev, "user requested size",
2984 p_usize, mdev->ldev->dc.disk_size);
2985
2986 /* if this is the first connect, or an otherwise expected
2987 * param exchange, choose the minimum */
2988 if (mdev->state.conn == C_WF_REPORT_PARAMS)
2989 p_usize = min_not_zero((sector_t)mdev->ldev->dc.disk_size,
2990 p_usize);
2991
2992 my_usize = mdev->ldev->dc.disk_size;
2993
2994 if (mdev->ldev->dc.disk_size != p_usize) {
2995 mdev->ldev->dc.disk_size = p_usize;
2996 dev_info(DEV, "Peer sets u_size to %lu sectors\n",
2997 (unsigned long)mdev->ldev->dc.disk_size);
2998 }
2999
3000 /* Never shrink a device with usable data during connect.
3001 But allow online shrinking if we are connected. */
Philipp Reisnera393db62009-12-22 13:35:52 +01003002 if (drbd_new_dev_size(mdev, mdev->ldev, 0) <
Philipp Reisnerb411b362009-09-25 16:07:19 -07003003 drbd_get_capacity(mdev->this_bdev) &&
3004 mdev->state.disk >= D_OUTDATED &&
3005 mdev->state.conn < C_CONNECTED) {
3006 dev_err(DEV, "The peer's disk size is too small!\n");
3007 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
3008 mdev->ldev->dc.disk_size = my_usize;
3009 put_ldev(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003010 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003011 }
3012 put_ldev(mdev);
3013 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003014
Philipp Reisnere89b5912010-03-24 17:11:33 +01003015 ddsf = be16_to_cpu(p->dds_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003016 if (get_ldev(mdev)) {
Bart Van Assche24c48302011-05-21 18:32:29 +02003017 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003018 put_ldev(mdev);
3019 if (dd == dev_size_error)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003020 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003021 drbd_md_sync(mdev);
3022 } else {
3023 /* I am diskless, need to accept the peer's size. */
3024 drbd_set_my_capacity(mdev, p_size);
3025 }
3026
Philipp Reisner99432fc2011-05-20 16:39:13 +02003027 mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size);
3028 drbd_reconsider_max_bio_size(mdev);
3029
Philipp Reisnerb411b362009-09-25 16:07:19 -07003030 if (get_ldev(mdev)) {
3031 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
3032 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
3033 ldsc = 1;
3034 }
3035
Philipp Reisnerb411b362009-09-25 16:07:19 -07003036 put_ldev(mdev);
3037 }
3038
3039 if (mdev->state.conn > C_WF_REPORT_PARAMS) {
3040 if (be64_to_cpu(p->c_size) !=
3041 drbd_get_capacity(mdev->this_bdev) || ldsc) {
3042 /* we have different sizes, probably peer
3043 * needs to know my new size... */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003044 drbd_send_sizes(mdev, 0, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003045 }
3046 if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
3047 (dd == grew && mdev->state.conn == C_CONNECTED)) {
3048 if (mdev->state.pdsk >= D_INCONSISTENT &&
Philipp Reisnere89b5912010-03-24 17:11:33 +01003049 mdev->state.disk >= D_INCONSISTENT) {
3050 if (ddsf & DDSF_NO_RESYNC)
3051 dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
3052 else
3053 resync_after_online_grow(mdev);
3054 } else
Philipp Reisnerb411b362009-09-25 16:07:19 -07003055 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
3056 }
3057 }
3058
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003059 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003060}
3061
Philipp Reisner02918be2010-08-20 14:35:10 +02003062static int receive_uuids(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003063{
Philipp Reisner02918be2010-08-20 14:35:10 +02003064 struct p_uuids *p = &mdev->data.rbuf.uuids;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003065 u64 *p_uuid;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003066 int i, updated_uuids = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003067
Philipp Reisnerb411b362009-09-25 16:07:19 -07003068 p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
3069
3070 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
3071 p_uuid[i] = be64_to_cpu(p->uuid[i]);
3072
3073 kfree(mdev->p_uuid);
3074 mdev->p_uuid = p_uuid;
3075
3076 if (mdev->state.conn < C_CONNECTED &&
3077 mdev->state.disk < D_INCONSISTENT &&
3078 mdev->state.role == R_PRIMARY &&
3079 (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
3080 dev_err(DEV, "Can only connect to data with current UUID=%016llX\n",
3081 (unsigned long long)mdev->ed_uuid);
3082 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003083 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003084 }
3085
3086 if (get_ldev(mdev)) {
3087 int skip_initial_sync =
3088 mdev->state.conn == C_CONNECTED &&
3089 mdev->agreed_pro_version >= 90 &&
3090 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
3091 (p_uuid[UI_FLAGS] & 8);
3092 if (skip_initial_sync) {
3093 dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n");
3094 drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003095 "clear_n_write from receive_uuids",
3096 BM_LOCKED_TEST_ALLOWED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003097 _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]);
3098 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3099 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3100 CS_VERBOSE, NULL);
3101 drbd_md_sync(mdev);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003102 updated_uuids = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003103 }
3104 put_ldev(mdev);
Philipp Reisner18a50fa2010-06-21 14:14:15 +02003105 } else if (mdev->state.disk < D_INCONSISTENT &&
3106 mdev->state.role == R_PRIMARY) {
3107 /* I am a diskless primary, the peer just created a new current UUID
3108 for me. */
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003109 updated_uuids = drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003110 }
3111
3112 /* Before we test for the disk state, we should wait until an eventually
3113 ongoing cluster wide state change is finished. That is important if
3114 we are primary and are detaching from our disk. We need to see the
3115 new disk state... */
3116 wait_event(mdev->misc_wait, !test_bit(CLUSTER_ST_CHANGE, &mdev->flags));
3117 if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT)
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003118 updated_uuids |= drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
3119
3120 if (updated_uuids)
3121 drbd_print_uuids(mdev, "receiver updated UUIDs to");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003122
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003123 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003124}
3125
3126/**
3127 * convert_state() - Converts the peer's view of the cluster state to our point of view
3128 * @ps: The state as seen by the peer.
3129 */
3130static union drbd_state convert_state(union drbd_state ps)
3131{
3132 union drbd_state ms;
3133
3134 static enum drbd_conns c_tab[] = {
3135 [C_CONNECTED] = C_CONNECTED,
3136
3137 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
3138 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
3139 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
3140 [C_VERIFY_S] = C_VERIFY_T,
3141 [C_MASK] = C_MASK,
3142 };
3143
3144 ms.i = ps.i;
3145
3146 ms.conn = c_tab[ps.conn];
3147 ms.peer = ps.role;
3148 ms.role = ps.peer;
3149 ms.pdsk = ps.disk;
3150 ms.disk = ps.pdsk;
3151 ms.peer_isp = (ps.aftr_isp | ps.user_isp);
3152
3153 return ms;
3154}
3155
Philipp Reisner02918be2010-08-20 14:35:10 +02003156static int receive_req_state(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003157{
Philipp Reisner02918be2010-08-20 14:35:10 +02003158 struct p_req_state *p = &mdev->data.rbuf.req_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003159 union drbd_state mask, val;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01003160 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003161
Philipp Reisnerb411b362009-09-25 16:07:19 -07003162 mask.i = be32_to_cpu(p->mask);
3163 val.i = be32_to_cpu(p->val);
3164
3165 if (test_bit(DISCARD_CONCURRENT, &mdev->flags) &&
3166 test_bit(CLUSTER_ST_CHANGE, &mdev->flags)) {
3167 drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003168 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003169 }
3170
3171 mask = convert_state(mask);
3172 val = convert_state(val);
3173
3174 rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
3175
3176 drbd_send_sr_reply(mdev, rv);
3177 drbd_md_sync(mdev);
3178
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003179 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003180}
3181
Philipp Reisner02918be2010-08-20 14:35:10 +02003182static int receive_state(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003183{
Philipp Reisner02918be2010-08-20 14:35:10 +02003184 struct p_state *p = &mdev->data.rbuf.state;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003185 union drbd_state os, ns, peer_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003186 enum drbd_disk_state real_peer_disk;
Philipp Reisner65d922c2010-06-16 16:18:09 +02003187 enum chg_state_flags cs_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003188 int rv;
3189
Philipp Reisnerb411b362009-09-25 16:07:19 -07003190 peer_state.i = be32_to_cpu(p->state);
3191
3192 real_peer_disk = peer_state.disk;
3193 if (peer_state.disk == D_NEGOTIATING) {
3194 real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
3195 dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
3196 }
3197
3198 spin_lock_irq(&mdev->req_lock);
3199 retry:
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003200 os = ns = mdev->state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003201 spin_unlock_irq(&mdev->req_lock);
3202
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003203 /* peer says his disk is uptodate, while we think it is inconsistent,
3204 * and this happens while we think we have a sync going on. */
3205 if (os.pdsk == D_INCONSISTENT && real_peer_disk == D_UP_TO_DATE &&
3206 os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) {
3207 /* If we are (becoming) SyncSource, but peer is still in sync
3208 * preparation, ignore its uptodate-ness to avoid flapping, it
3209 * will change to inconsistent once the peer reaches active
3210 * syncing states.
3211 * It may have changed syncer-paused flags, however, so we
3212 * cannot ignore this completely. */
3213 if (peer_state.conn > C_CONNECTED &&
3214 peer_state.conn < C_SYNC_SOURCE)
3215 real_peer_disk = D_INCONSISTENT;
3216
3217 /* if peer_state changes to connected at the same time,
3218 * it explicitly notifies us that it finished resync.
3219 * Maybe we should finish it up, too? */
3220 else if (os.conn >= C_SYNC_SOURCE &&
3221 peer_state.conn == C_CONNECTED) {
3222 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed)
3223 drbd_resync_finished(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003224 return true;
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003225 }
3226 }
3227
3228 /* peer says his disk is inconsistent, while we think it is uptodate,
3229 * and this happens while the peer still thinks we have a sync going on,
3230 * but we think we are already done with the sync.
3231 * We ignore this to avoid flapping pdsk.
3232 * This should not happen, if the peer is a recent version of drbd. */
3233 if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT &&
3234 os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE)
3235 real_peer_disk = D_UP_TO_DATE;
3236
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003237 if (ns.conn == C_WF_REPORT_PARAMS)
3238 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003239
Philipp Reisner67531712010-10-27 12:21:30 +02003240 if (peer_state.conn == C_AHEAD)
3241 ns.conn = C_BEHIND;
3242
Philipp Reisnerb411b362009-09-25 16:07:19 -07003243 if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING &&
3244 get_ldev_if_state(mdev, D_NEGOTIATING)) {
3245 int cr; /* consider resync */
3246
3247 /* if we established a new connection */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003248 cr = (os.conn < C_CONNECTED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003249 /* if we had an established connection
3250 * and one of the nodes newly attaches a disk */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003251 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003252 (peer_state.disk == D_NEGOTIATING ||
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003253 os.disk == D_NEGOTIATING));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003254 /* if we have both been inconsistent, and the peer has been
3255 * forced to be UpToDate with --overwrite-data */
3256 cr |= test_bit(CONSIDER_RESYNC, &mdev->flags);
3257 /* if we had been plain connected, and the admin requested to
3258 * start a sync by "invalidate" or "invalidate-remote" */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003259 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003260 (peer_state.conn >= C_STARTING_SYNC_S &&
3261 peer_state.conn <= C_WF_BITMAP_T));
3262
3263 if (cr)
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003264 ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003265
3266 put_ldev(mdev);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003267 if (ns.conn == C_MASK) {
3268 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003269 if (mdev->state.disk == D_NEGOTIATING) {
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003270 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003271 } else if (peer_state.disk == D_NEGOTIATING) {
3272 dev_err(DEV, "Disk attach process on the peer node was aborted.\n");
3273 peer_state.disk = D_DISKLESS;
Lars Ellenberg580b9762010-02-26 23:15:23 +01003274 real_peer_disk = D_DISKLESS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003275 } else {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003276 if (test_and_clear_bit(CONN_DRY_RUN, &mdev->flags))
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003277 return false;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003278 D_ASSERT(os.conn == C_WF_REPORT_PARAMS);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003279 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003280 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003281 }
3282 }
3283 }
3284
3285 spin_lock_irq(&mdev->req_lock);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003286 if (mdev->state.i != os.i)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003287 goto retry;
3288 clear_bit(CONSIDER_RESYNC, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003289 ns.peer = peer_state.role;
3290 ns.pdsk = real_peer_disk;
3291 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003292 if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003293 ns.disk = mdev->new_state_tmp.disk;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003294 cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD);
3295 if (ns.pdsk == D_CONSISTENT && is_susp(ns) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED &&
Philipp Reisner481c6f52010-06-22 14:03:27 +02003296 test_bit(NEW_CUR_UUID, &mdev->flags)) {
3297 /* Do not allow tl_restart(resend) for a rebooted peer. We can only allow this
3298 for temporal network outages! */
3299 spin_unlock_irq(&mdev->req_lock);
3300 dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
3301 tl_clear(mdev);
3302 drbd_uuid_new_current(mdev);
3303 clear_bit(NEW_CUR_UUID, &mdev->flags);
3304 drbd_force_state(mdev, NS2(conn, C_PROTOCOL_ERROR, susp, 0));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003305 return false;
Philipp Reisner481c6f52010-06-22 14:03:27 +02003306 }
Philipp Reisner65d922c2010-06-16 16:18:09 +02003307 rv = _drbd_set_state(mdev, ns, cs_flags, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003308 ns = mdev->state;
3309 spin_unlock_irq(&mdev->req_lock);
3310
3311 if (rv < SS_SUCCESS) {
3312 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003313 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003314 }
3315
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003316 if (os.conn > C_WF_REPORT_PARAMS) {
3317 if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003318 peer_state.disk != D_NEGOTIATING ) {
3319 /* we want resync, peer has not yet decided to sync... */
3320 /* Nowadays only used when forcing a node into primary role and
3321 setting its disk to UpToDate with that */
3322 drbd_send_uuids(mdev);
3323 drbd_send_state(mdev);
3324 }
3325 }
3326
3327 mdev->net_conf->want_lose = 0;
3328
3329 drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
3330
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003331 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003332}
3333
Philipp Reisner02918be2010-08-20 14:35:10 +02003334static int receive_sync_uuid(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003335{
Philipp Reisner02918be2010-08-20 14:35:10 +02003336 struct p_rs_uuid *p = &mdev->data.rbuf.rs_uuid;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003337
3338 wait_event(mdev->misc_wait,
3339 mdev->state.conn == C_WF_SYNC_UUID ||
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02003340 mdev->state.conn == C_BEHIND ||
Philipp Reisnerb411b362009-09-25 16:07:19 -07003341 mdev->state.conn < C_CONNECTED ||
3342 mdev->state.disk < D_NEGOTIATING);
3343
3344 /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */
3345
Philipp Reisnerb411b362009-09-25 16:07:19 -07003346 /* Here the _drbd_uuid_ functions are right, current should
3347 _not_ be rotated into the history */
3348 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
3349 _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid));
3350 _drbd_uuid_set(mdev, UI_BITMAP, 0UL);
3351
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003352 drbd_print_uuids(mdev, "updated sync uuid");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003353 drbd_start_resync(mdev, C_SYNC_TARGET);
3354
3355 put_ldev(mdev);
3356 } else
3357 dev_err(DEV, "Ignoring SyncUUID packet!\n");
3358
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003359 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003360}
3361
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003362/**
3363 * receive_bitmap_plain
3364 *
3365 * Return 0 when done, 1 when another iteration is needed, and a negative error
3366 * code upon failure.
3367 */
3368static int
Philipp Reisner02918be2010-08-20 14:35:10 +02003369receive_bitmap_plain(struct drbd_conf *mdev, unsigned int data_size,
3370 unsigned long *buffer, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003371{
3372 unsigned num_words = min_t(size_t, BM_PACKET_WORDS, c->bm_words - c->word_offset);
3373 unsigned want = num_words * sizeof(long);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003374 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003375
Philipp Reisner02918be2010-08-20 14:35:10 +02003376 if (want != data_size) {
3377 dev_err(DEV, "%s:want (%u) != data_size (%u)\n", __func__, want, data_size);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003378 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003379 }
3380 if (want == 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003381 return 0;
3382 err = drbd_recv(mdev, buffer, want);
3383 if (err != want) {
3384 if (err >= 0)
3385 err = -EIO;
3386 return err;
3387 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003388
3389 drbd_bm_merge_lel(mdev, c->word_offset, num_words, buffer);
3390
3391 c->word_offset += num_words;
3392 c->bit_offset = c->word_offset * BITS_PER_LONG;
3393 if (c->bit_offset > c->bm_bits)
3394 c->bit_offset = c->bm_bits;
3395
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003396 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003397}
3398
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003399/**
3400 * recv_bm_rle_bits
3401 *
3402 * Return 0 when done, 1 when another iteration is needed, and a negative error
3403 * code upon failure.
3404 */
3405static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07003406recv_bm_rle_bits(struct drbd_conf *mdev,
3407 struct p_compressed_bm *p,
3408 struct bm_xfer_ctx *c)
3409{
3410 struct bitstream bs;
3411 u64 look_ahead;
3412 u64 rl;
3413 u64 tmp;
3414 unsigned long s = c->bit_offset;
3415 unsigned long e;
Lars Ellenberg004352f2010-10-05 20:13:58 +02003416 int len = be16_to_cpu(p->head.length) - (sizeof(*p) - sizeof(p->head));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003417 int toggle = DCBP_get_start(p);
3418 int have;
3419 int bits;
3420
3421 bitstream_init(&bs, p->code, len, DCBP_get_pad_bits(p));
3422
3423 bits = bitstream_get_bits(&bs, &look_ahead, 64);
3424 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003425 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003426
3427 for (have = bits; have > 0; s += rl, toggle = !toggle) {
3428 bits = vli_decode_bits(&rl, look_ahead);
3429 if (bits <= 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003430 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003431
3432 if (toggle) {
3433 e = s + rl -1;
3434 if (e >= c->bm_bits) {
3435 dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003436 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003437 }
3438 _drbd_bm_set_bits(mdev, s, e);
3439 }
3440
3441 if (have < bits) {
3442 dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
3443 have, bits, look_ahead,
3444 (unsigned int)(bs.cur.b - p->code),
3445 (unsigned int)bs.buf_len);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003446 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003447 }
3448 look_ahead >>= bits;
3449 have -= bits;
3450
3451 bits = bitstream_get_bits(&bs, &tmp, 64 - have);
3452 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003453 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003454 look_ahead |= tmp << have;
3455 have += bits;
3456 }
3457
3458 c->bit_offset = s;
3459 bm_xfer_ctx_bit_to_word_offset(c);
3460
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003461 return (s != c->bm_bits);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003462}
3463
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003464/**
3465 * decode_bitmap_c
3466 *
3467 * Return 0 when done, 1 when another iteration is needed, and a negative error
3468 * code upon failure.
3469 */
3470static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07003471decode_bitmap_c(struct drbd_conf *mdev,
3472 struct p_compressed_bm *p,
3473 struct bm_xfer_ctx *c)
3474{
3475 if (DCBP_get_code(p) == RLE_VLI_Bits)
3476 return recv_bm_rle_bits(mdev, p, c);
3477
3478 /* other variants had been implemented for evaluation,
3479 * but have been dropped as this one turned out to be "best"
3480 * during all our tests. */
3481
3482 dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
3483 drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR));
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003484 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003485}
3486
3487void INFO_bm_xfer_stats(struct drbd_conf *mdev,
3488 const char *direction, struct bm_xfer_ctx *c)
3489{
3490 /* what would it take to transfer it "plaintext" */
Philipp Reisner0b70a132010-08-20 13:36:10 +02003491 unsigned plain = sizeof(struct p_header80) *
Philipp Reisnerb411b362009-09-25 16:07:19 -07003492 ((c->bm_words+BM_PACKET_WORDS-1)/BM_PACKET_WORDS+1)
3493 + c->bm_words * sizeof(long);
3494 unsigned total = c->bytes[0] + c->bytes[1];
3495 unsigned r;
3496
3497 /* total can not be zero. but just in case: */
3498 if (total == 0)
3499 return;
3500
3501 /* don't report if not compressed */
3502 if (total >= plain)
3503 return;
3504
3505 /* total < plain. check for overflow, still */
3506 r = (total > UINT_MAX/1000) ? (total / (plain/1000))
3507 : (1000 * total / plain);
3508
3509 if (r > 1000)
3510 r = 1000;
3511
3512 r = 1000 - r;
3513 dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
3514 "total %u; compression: %u.%u%%\n",
3515 direction,
3516 c->bytes[1], c->packets[1],
3517 c->bytes[0], c->packets[0],
3518 total, r/10, r % 10);
3519}
3520
3521/* Since we are processing the bitfield from lower addresses to higher,
3522 it does not matter if the process it in 32 bit chunks or 64 bit
3523 chunks as long as it is little endian. (Understand it as byte stream,
3524 beginning with the lowest byte...) If we would use big endian
3525 we would need to process it from the highest address to the lowest,
3526 in order to be agnostic to the 32 vs 64 bits issue.
3527
3528 returns 0 on failure, 1 if we successfully received it. */
Philipp Reisner02918be2010-08-20 14:35:10 +02003529static int receive_bitmap(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003530{
3531 struct bm_xfer_ctx c;
3532 void *buffer;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003533 int err;
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003534 int ok = false;
Philipp Reisner02918be2010-08-20 14:35:10 +02003535 struct p_header80 *h = &mdev->data.rbuf.header.h80;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003536
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003537 drbd_bm_lock(mdev, "receive bitmap", BM_LOCKED_SET_ALLOWED);
3538 /* you are supposed to send additional out-of-sync information
3539 * if you actually set bits during this phase */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003540
3541 /* maybe we should use some per thread scratch page,
3542 * and allocate that during initial device creation? */
3543 buffer = (unsigned long *) __get_free_page(GFP_NOIO);
3544 if (!buffer) {
3545 dev_err(DEV, "failed to allocate one page buffer in %s\n", __func__);
3546 goto out;
3547 }
3548
3549 c = (struct bm_xfer_ctx) {
3550 .bm_bits = drbd_bm_bits(mdev),
3551 .bm_words = drbd_bm_words(mdev),
3552 };
3553
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003554 for(;;) {
Philipp Reisner02918be2010-08-20 14:35:10 +02003555 if (cmd == P_BITMAP) {
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003556 err = receive_bitmap_plain(mdev, data_size, buffer, &c);
Philipp Reisner02918be2010-08-20 14:35:10 +02003557 } else if (cmd == P_COMPRESSED_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003558 /* MAYBE: sanity check that we speak proto >= 90,
3559 * and the feature is enabled! */
3560 struct p_compressed_bm *p;
3561
Philipp Reisner02918be2010-08-20 14:35:10 +02003562 if (data_size > BM_PACKET_PAYLOAD_BYTES) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003563 dev_err(DEV, "ReportCBitmap packet too large\n");
3564 goto out;
3565 }
3566 /* use the page buff */
3567 p = buffer;
3568 memcpy(p, h, sizeof(*h));
Philipp Reisner02918be2010-08-20 14:35:10 +02003569 if (drbd_recv(mdev, p->head.payload, data_size) != data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003570 goto out;
Lars Ellenberg004352f2010-10-05 20:13:58 +02003571 if (data_size <= (sizeof(*p) - sizeof(p->head))) {
3572 dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", data_size);
Andreas Gruenbacher78fcbda2010-12-10 22:18:27 +01003573 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003574 }
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003575 err = decode_bitmap_c(mdev, p, &c);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003576 } else {
Philipp Reisner02918be2010-08-20 14:35:10 +02003577 dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003578 goto out;
3579 }
3580
Philipp Reisner02918be2010-08-20 14:35:10 +02003581 c.packets[cmd == P_BITMAP]++;
3582 c.bytes[cmd == P_BITMAP] += sizeof(struct p_header80) + data_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003583
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003584 if (err <= 0) {
3585 if (err < 0)
3586 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003587 break;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003588 }
Philipp Reisner02918be2010-08-20 14:35:10 +02003589 if (!drbd_recv_header(mdev, &cmd, &data_size))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003590 goto out;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003591 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003592
3593 INFO_bm_xfer_stats(mdev, "receive", &c);
3594
3595 if (mdev->state.conn == C_WF_BITMAP_T) {
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01003596 enum drbd_state_rv rv;
3597
Philipp Reisnerb411b362009-09-25 16:07:19 -07003598 ok = !drbd_send_bitmap(mdev);
3599 if (!ok)
3600 goto out;
3601 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01003602 rv = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
3603 D_ASSERT(rv == SS_SUCCESS);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003604 } else if (mdev->state.conn != C_WF_BITMAP_S) {
3605 /* admin may have requested C_DISCONNECTING,
3606 * other threads may have noticed network errors */
3607 dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n",
3608 drbd_conn_str(mdev->state.conn));
3609 }
3610
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003611 ok = true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003612 out:
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003613 drbd_bm_unlock(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003614 if (ok && mdev->state.conn == C_WF_BITMAP_S)
3615 drbd_start_resync(mdev, C_SYNC_SOURCE);
3616 free_page((unsigned long) buffer);
3617 return ok;
3618}
3619
Philipp Reisner02918be2010-08-20 14:35:10 +02003620static int receive_skip(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003621{
3622 /* TODO zero copy sink :) */
3623 static char sink[128];
3624 int size, want, r;
3625
Philipp Reisner02918be2010-08-20 14:35:10 +02003626 dev_warn(DEV, "skipping unknown optional packet type %d, l: %d!\n",
3627 cmd, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003628
Philipp Reisner02918be2010-08-20 14:35:10 +02003629 size = data_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003630 while (size > 0) {
3631 want = min_t(int, size, sizeof(sink));
3632 r = drbd_recv(mdev, sink, want);
3633 ERR_IF(r <= 0) break;
3634 size -= r;
3635 }
3636 return size == 0;
3637}
3638
Philipp Reisner02918be2010-08-20 14:35:10 +02003639static int receive_UnplugRemote(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003640{
Philipp Reisnerb411b362009-09-25 16:07:19 -07003641 /* Make sure we've acked all the TCP data associated
3642 * with the data requests being unplugged */
3643 drbd_tcp_quickack(mdev->data.socket);
3644
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003645 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003646}
3647
Philipp Reisner73a01a12010-10-27 14:33:00 +02003648static int receive_out_of_sync(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
3649{
3650 struct p_block_desc *p = &mdev->data.rbuf.block_desc;
3651
Lars Ellenbergf735e3632010-12-17 21:06:18 +01003652 switch (mdev->state.conn) {
3653 case C_WF_SYNC_UUID:
3654 case C_WF_BITMAP_T:
3655 case C_BEHIND:
3656 break;
3657 default:
3658 dev_err(DEV, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n",
3659 drbd_conn_str(mdev->state.conn));
3660 }
3661
Philipp Reisner73a01a12010-10-27 14:33:00 +02003662 drbd_set_out_of_sync(mdev, be64_to_cpu(p->sector), be32_to_cpu(p->blksize));
3663
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003664 return true;
Philipp Reisner73a01a12010-10-27 14:33:00 +02003665}
3666
Philipp Reisner02918be2010-08-20 14:35:10 +02003667typedef int (*drbd_cmd_handler_f)(struct drbd_conf *, enum drbd_packets cmd, unsigned int to_receive);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003668
Philipp Reisner02918be2010-08-20 14:35:10 +02003669struct data_cmd {
3670 int expect_payload;
3671 size_t pkt_size;
3672 drbd_cmd_handler_f function;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003673};
3674
Philipp Reisner02918be2010-08-20 14:35:10 +02003675static struct data_cmd drbd_cmd_handler[] = {
3676 [P_DATA] = { 1, sizeof(struct p_data), receive_Data },
3677 [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply },
3678 [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } ,
3679 [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } ,
3680 [P_BITMAP] = { 1, sizeof(struct p_header80), receive_bitmap } ,
3681 [P_COMPRESSED_BITMAP] = { 1, sizeof(struct p_header80), receive_bitmap } ,
3682 [P_UNPLUG_REMOTE] = { 0, sizeof(struct p_header80), receive_UnplugRemote },
3683 [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
3684 [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
3685 [P_SYNC_PARAM] = { 1, sizeof(struct p_header80), receive_SyncParam },
3686 [P_SYNC_PARAM89] = { 1, sizeof(struct p_header80), receive_SyncParam },
3687 [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol },
3688 [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids },
3689 [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes },
3690 [P_STATE] = { 0, sizeof(struct p_state), receive_state },
3691 [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state },
3692 [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid },
3693 [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
3694 [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest },
3695 [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest },
3696 [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip },
Philipp Reisner73a01a12010-10-27 14:33:00 +02003697 [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync },
Philipp Reisner02918be2010-08-20 14:35:10 +02003698 /* anything missing from this table is in
3699 * the asender_tbl, see get_asender_cmd */
3700 [P_MAX_CMD] = { 0, 0, NULL },
3701};
3702
3703/* All handler functions that expect a sub-header get that sub-heder in
3704 mdev->data.rbuf.header.head.payload.
3705
3706 Usually in mdev->data.rbuf.header.head the callback can find the usual
3707 p_header, but they may not rely on that. Since there is also p_header95 !
3708 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003709
3710static void drbdd(struct drbd_conf *mdev)
3711{
Philipp Reisner02918be2010-08-20 14:35:10 +02003712 union p_header *header = &mdev->data.rbuf.header;
3713 unsigned int packet_size;
3714 enum drbd_packets cmd;
3715 size_t shs; /* sub header size */
3716 int rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003717
3718 while (get_t_state(&mdev->receiver) == Running) {
3719 drbd_thread_current_set_cpu(mdev);
Philipp Reisner02918be2010-08-20 14:35:10 +02003720 if (!drbd_recv_header(mdev, &cmd, &packet_size))
3721 goto err_out;
3722
3723 if (unlikely(cmd >= P_MAX_CMD || !drbd_cmd_handler[cmd].function)) {
3724 dev_err(DEV, "unknown packet type %d, l: %d!\n", cmd, packet_size);
3725 goto err_out;
Lars Ellenberg0b33a912009-11-16 15:58:04 +01003726 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003727
Philipp Reisner02918be2010-08-20 14:35:10 +02003728 shs = drbd_cmd_handler[cmd].pkt_size - sizeof(union p_header);
Philipp Reisner02918be2010-08-20 14:35:10 +02003729 if (packet_size - shs > 0 && !drbd_cmd_handler[cmd].expect_payload) {
3730 dev_err(DEV, "No payload expected %s l:%d\n", cmdname(cmd), packet_size);
3731 goto err_out;
3732 }
3733
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02003734 if (shs) {
3735 rv = drbd_recv(mdev, &header->h80.payload, shs);
3736 if (unlikely(rv != shs)) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01003737 if (!signal_pending(current))
3738 dev_warn(DEV, "short read while reading sub header: rv=%d\n", rv);
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02003739 goto err_out;
3740 }
3741 }
3742
Philipp Reisner02918be2010-08-20 14:35:10 +02003743 rv = drbd_cmd_handler[cmd].function(mdev, cmd, packet_size - shs);
3744
3745 if (unlikely(!rv)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003746 dev_err(DEV, "error receiving %s, l: %d!\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02003747 cmdname(cmd), packet_size);
3748 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003749 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003750 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003751
Philipp Reisner02918be2010-08-20 14:35:10 +02003752 if (0) {
3753 err_out:
3754 drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003755 }
Lars Ellenberg856c50c2010-10-14 13:37:40 +02003756 /* If we leave here, we probably want to update at least the
3757 * "Connected" indicator on stable storage. Do so explicitly here. */
3758 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003759}
3760
3761void drbd_flush_workqueue(struct drbd_conf *mdev)
3762{
3763 struct drbd_wq_barrier barr;
3764
3765 barr.w.cb = w_prev_work_done;
3766 init_completion(&barr.done);
3767 drbd_queue_work(&mdev->data.work, &barr.w);
3768 wait_for_completion(&barr.done);
3769}
3770
3771static void drbd_disconnect(struct drbd_conf *mdev)
3772{
3773 enum drbd_fencing_p fp;
3774 union drbd_state os, ns;
3775 int rv = SS_UNKNOWN_ERROR;
3776 unsigned int i;
3777
3778 if (mdev->state.conn == C_STANDALONE)
3779 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003780
3781 /* asender does not clean up anything. it must not interfere, either */
3782 drbd_thread_stop(&mdev->asender);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003783 drbd_free_sock(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003784
Philipp Reisner85719572010-07-21 10:20:17 +02003785 /* wait for current activity to cease. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003786 spin_lock_irq(&mdev->req_lock);
3787 _drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
3788 _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee);
3789 _drbd_wait_ee_list_empty(mdev, &mdev->read_ee);
3790 spin_unlock_irq(&mdev->req_lock);
3791
3792 /* We do not have data structures that would allow us to
3793 * get the rs_pending_cnt down to 0 again.
3794 * * On C_SYNC_TARGET we do not have any data structures describing
3795 * the pending RSDataRequest's we have sent.
3796 * * On C_SYNC_SOURCE there is no data structure that tracks
3797 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
3798 * And no, it is not the sum of the reference counts in the
3799 * resync_LRU. The resync_LRU tracks the whole operation including
3800 * the disk-IO, while the rs_pending_cnt only tracks the blocks
3801 * on the fly. */
3802 drbd_rs_cancel_all(mdev);
3803 mdev->rs_total = 0;
3804 mdev->rs_failed = 0;
3805 atomic_set(&mdev->rs_pending_cnt, 0);
3806 wake_up(&mdev->misc_wait);
3807
Philipp Reisner7fde2be2011-03-01 11:08:28 +01003808 del_timer(&mdev->request_timer);
3809
Philipp Reisnerb411b362009-09-25 16:07:19 -07003810 /* make sure syncer is stopped and w_resume_next_sg queued */
3811 del_timer_sync(&mdev->resync_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003812 resync_timer_fn((unsigned long)mdev);
3813
Philipp Reisnerb411b362009-09-25 16:07:19 -07003814 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
3815 * w_make_resync_request etc. which may still be on the worker queue
3816 * to be "canceled" */
3817 drbd_flush_workqueue(mdev);
3818
3819 /* This also does reclaim_net_ee(). If we do this too early, we might
3820 * miss some resync ee and pages.*/
3821 drbd_process_done_ee(mdev);
3822
3823 kfree(mdev->p_uuid);
3824 mdev->p_uuid = NULL;
3825
Philipp Reisnerfb22c402010-09-08 23:20:21 +02003826 if (!is_susp(mdev->state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003827 tl_clear(mdev);
3828
Philipp Reisnerb411b362009-09-25 16:07:19 -07003829 dev_info(DEV, "Connection closed\n");
3830
3831 drbd_md_sync(mdev);
3832
3833 fp = FP_DONT_CARE;
3834 if (get_ldev(mdev)) {
3835 fp = mdev->ldev->dc.fencing;
3836 put_ldev(mdev);
3837 }
3838
Philipp Reisner87f7be42010-06-11 13:56:33 +02003839 if (mdev->state.role == R_PRIMARY && fp >= FP_RESOURCE && mdev->state.pdsk >= D_UNKNOWN)
3840 drbd_try_outdate_peer_async(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003841
3842 spin_lock_irq(&mdev->req_lock);
3843 os = mdev->state;
3844 if (os.conn >= C_UNCONNECTED) {
3845 /* Do not restart in case we are C_DISCONNECTING */
3846 ns = os;
3847 ns.conn = C_UNCONNECTED;
3848 rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
3849 }
3850 spin_unlock_irq(&mdev->req_lock);
3851
3852 if (os.conn == C_DISCONNECTING) {
Philipp Reisner84dfb9f2010-06-23 11:20:05 +02003853 wait_event(mdev->net_cnt_wait, atomic_read(&mdev->net_cnt) == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003854
Philipp Reisnerb411b362009-09-25 16:07:19 -07003855 crypto_free_hash(mdev->cram_hmac_tfm);
3856 mdev->cram_hmac_tfm = NULL;
3857
3858 kfree(mdev->net_conf);
3859 mdev->net_conf = NULL;
3860 drbd_request_state(mdev, NS(conn, C_STANDALONE));
3861 }
3862
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003863 /* serialize with bitmap writeout triggered by the state change,
3864 * if any. */
3865 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
3866
Philipp Reisnerb411b362009-09-25 16:07:19 -07003867 /* tcp_close and release of sendpage pages can be deferred. I don't
3868 * want to use SO_LINGER, because apparently it can be deferred for
3869 * more than 20 seconds (longest time I checked).
3870 *
3871 * Actually we don't care for exactly when the network stack does its
3872 * put_page(), but release our reference on these pages right here.
3873 */
3874 i = drbd_release_ee(mdev, &mdev->net_ee);
3875 if (i)
3876 dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
Lars Ellenberg435f0742010-09-06 12:30:25 +02003877 i = atomic_read(&mdev->pp_in_use_by_net);
3878 if (i)
3879 dev_info(DEV, "pp_in_use_by_net = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003880 i = atomic_read(&mdev->pp_in_use);
3881 if (i)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02003882 dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003883
3884 D_ASSERT(list_empty(&mdev->read_ee));
3885 D_ASSERT(list_empty(&mdev->active_ee));
3886 D_ASSERT(list_empty(&mdev->sync_ee));
3887 D_ASSERT(list_empty(&mdev->done_ee));
3888
3889 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
3890 atomic_set(&mdev->current_epoch->epoch_size, 0);
3891 D_ASSERT(list_empty(&mdev->current_epoch->list));
3892}
3893
3894/*
3895 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
3896 * we can agree on is stored in agreed_pro_version.
3897 *
3898 * feature flags and the reserved array should be enough room for future
3899 * enhancements of the handshake protocol, and possible plugins...
3900 *
3901 * for now, they are expected to be zero, but ignored.
3902 */
3903static int drbd_send_handshake(struct drbd_conf *mdev)
3904{
3905 /* ASSERT current == mdev->receiver ... */
3906 struct p_handshake *p = &mdev->data.sbuf.handshake;
3907 int ok;
3908
3909 if (mutex_lock_interruptible(&mdev->data.mutex)) {
3910 dev_err(DEV, "interrupted during initial handshake\n");
3911 return 0; /* interrupted. not ok. */
3912 }
3913
3914 if (mdev->data.socket == NULL) {
3915 mutex_unlock(&mdev->data.mutex);
3916 return 0;
3917 }
3918
3919 memset(p, 0, sizeof(*p));
3920 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
3921 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
3922 ok = _drbd_send_cmd( mdev, mdev->data.socket, P_HAND_SHAKE,
Philipp Reisner0b70a132010-08-20 13:36:10 +02003923 (struct p_header80 *)p, sizeof(*p), 0 );
Philipp Reisnerb411b362009-09-25 16:07:19 -07003924 mutex_unlock(&mdev->data.mutex);
3925 return ok;
3926}
3927
3928/*
3929 * return values:
3930 * 1 yes, we have a valid connection
3931 * 0 oops, did not work out, please try again
3932 * -1 peer talks different language,
3933 * no point in trying again, please go standalone.
3934 */
3935static int drbd_do_handshake(struct drbd_conf *mdev)
3936{
3937 /* ASSERT current == mdev->receiver ... */
3938 struct p_handshake *p = &mdev->data.rbuf.handshake;
Philipp Reisner02918be2010-08-20 14:35:10 +02003939 const int expect = sizeof(struct p_handshake) - sizeof(struct p_header80);
3940 unsigned int length;
3941 enum drbd_packets cmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003942 int rv;
3943
3944 rv = drbd_send_handshake(mdev);
3945 if (!rv)
3946 return 0;
3947
Philipp Reisner02918be2010-08-20 14:35:10 +02003948 rv = drbd_recv_header(mdev, &cmd, &length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003949 if (!rv)
3950 return 0;
3951
Philipp Reisner02918be2010-08-20 14:35:10 +02003952 if (cmd != P_HAND_SHAKE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003953 dev_err(DEV, "expected HandShake packet, received: %s (0x%04x)\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02003954 cmdname(cmd), cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003955 return -1;
3956 }
3957
Philipp Reisner02918be2010-08-20 14:35:10 +02003958 if (length != expect) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003959 dev_err(DEV, "expected HandShake length: %u, received: %u\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02003960 expect, length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003961 return -1;
3962 }
3963
3964 rv = drbd_recv(mdev, &p->head.payload, expect);
3965
3966 if (rv != expect) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01003967 if (!signal_pending(current))
3968 dev_warn(DEV, "short read receiving handshake packet: l=%u\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003969 return 0;
3970 }
3971
Philipp Reisnerb411b362009-09-25 16:07:19 -07003972 p->protocol_min = be32_to_cpu(p->protocol_min);
3973 p->protocol_max = be32_to_cpu(p->protocol_max);
3974 if (p->protocol_max == 0)
3975 p->protocol_max = p->protocol_min;
3976
3977 if (PRO_VERSION_MAX < p->protocol_min ||
3978 PRO_VERSION_MIN > p->protocol_max)
3979 goto incompat;
3980
3981 mdev->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
3982
3983 dev_info(DEV, "Handshake successful: "
3984 "Agreed network protocol version %d\n", mdev->agreed_pro_version);
3985
3986 return 1;
3987
3988 incompat:
3989 dev_err(DEV, "incompatible DRBD dialects: "
3990 "I support %d-%d, peer supports %d-%d\n",
3991 PRO_VERSION_MIN, PRO_VERSION_MAX,
3992 p->protocol_min, p->protocol_max);
3993 return -1;
3994}
3995
3996#if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
3997static int drbd_do_auth(struct drbd_conf *mdev)
3998{
3999 dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
4000 dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004001 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004002}
4003#else
4004#define CHALLENGE_LEN 64
Johannes Thomab10d96c2010-01-07 16:02:50 +01004005
4006/* Return value:
4007 1 - auth succeeded,
4008 0 - failed, try again (network error),
4009 -1 - auth failed, don't try again.
4010*/
4011
Philipp Reisnerb411b362009-09-25 16:07:19 -07004012static int drbd_do_auth(struct drbd_conf *mdev)
4013{
4014 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
4015 struct scatterlist sg;
4016 char *response = NULL;
4017 char *right_response = NULL;
4018 char *peers_ch = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004019 unsigned int key_len = strlen(mdev->net_conf->shared_secret);
4020 unsigned int resp_size;
4021 struct hash_desc desc;
Philipp Reisner02918be2010-08-20 14:35:10 +02004022 enum drbd_packets cmd;
4023 unsigned int length;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004024 int rv;
4025
4026 desc.tfm = mdev->cram_hmac_tfm;
4027 desc.flags = 0;
4028
4029 rv = crypto_hash_setkey(mdev->cram_hmac_tfm,
4030 (u8 *)mdev->net_conf->shared_secret, key_len);
4031 if (rv) {
4032 dev_err(DEV, "crypto_hash_setkey() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004033 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004034 goto fail;
4035 }
4036
4037 get_random_bytes(my_challenge, CHALLENGE_LEN);
4038
4039 rv = drbd_send_cmd2(mdev, P_AUTH_CHALLENGE, my_challenge, CHALLENGE_LEN);
4040 if (!rv)
4041 goto fail;
4042
Philipp Reisner02918be2010-08-20 14:35:10 +02004043 rv = drbd_recv_header(mdev, &cmd, &length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004044 if (!rv)
4045 goto fail;
4046
Philipp Reisner02918be2010-08-20 14:35:10 +02004047 if (cmd != P_AUTH_CHALLENGE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004048 dev_err(DEV, "expected AuthChallenge packet, received: %s (0x%04x)\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02004049 cmdname(cmd), cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004050 rv = 0;
4051 goto fail;
4052 }
4053
Philipp Reisner02918be2010-08-20 14:35:10 +02004054 if (length > CHALLENGE_LEN * 2) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004055 dev_err(DEV, "expected AuthChallenge payload too big.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004056 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004057 goto fail;
4058 }
4059
Philipp Reisner02918be2010-08-20 14:35:10 +02004060 peers_ch = kmalloc(length, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004061 if (peers_ch == NULL) {
4062 dev_err(DEV, "kmalloc of peers_ch failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004063 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004064 goto fail;
4065 }
4066
Philipp Reisner02918be2010-08-20 14:35:10 +02004067 rv = drbd_recv(mdev, peers_ch, length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004068
Philipp Reisner02918be2010-08-20 14:35:10 +02004069 if (rv != length) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01004070 if (!signal_pending(current))
4071 dev_warn(DEV, "short read AuthChallenge: l=%u\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004072 rv = 0;
4073 goto fail;
4074 }
4075
4076 resp_size = crypto_hash_digestsize(mdev->cram_hmac_tfm);
4077 response = kmalloc(resp_size, GFP_NOIO);
4078 if (response == NULL) {
4079 dev_err(DEV, "kmalloc of response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004080 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004081 goto fail;
4082 }
4083
4084 sg_init_table(&sg, 1);
Philipp Reisner02918be2010-08-20 14:35:10 +02004085 sg_set_buf(&sg, peers_ch, length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004086
4087 rv = crypto_hash_digest(&desc, &sg, sg.length, response);
4088 if (rv) {
4089 dev_err(DEV, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004090 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004091 goto fail;
4092 }
4093
4094 rv = drbd_send_cmd2(mdev, P_AUTH_RESPONSE, response, resp_size);
4095 if (!rv)
4096 goto fail;
4097
Philipp Reisner02918be2010-08-20 14:35:10 +02004098 rv = drbd_recv_header(mdev, &cmd, &length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004099 if (!rv)
4100 goto fail;
4101
Philipp Reisner02918be2010-08-20 14:35:10 +02004102 if (cmd != P_AUTH_RESPONSE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004103 dev_err(DEV, "expected AuthResponse packet, received: %s (0x%04x)\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02004104 cmdname(cmd), cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004105 rv = 0;
4106 goto fail;
4107 }
4108
Philipp Reisner02918be2010-08-20 14:35:10 +02004109 if (length != resp_size) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004110 dev_err(DEV, "expected AuthResponse payload of wrong size\n");
4111 rv = 0;
4112 goto fail;
4113 }
4114
4115 rv = drbd_recv(mdev, response , resp_size);
4116
4117 if (rv != resp_size) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01004118 if (!signal_pending(current))
4119 dev_warn(DEV, "short read receiving AuthResponse: l=%u\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004120 rv = 0;
4121 goto fail;
4122 }
4123
4124 right_response = kmalloc(resp_size, GFP_NOIO);
Julia Lawall2d1ee872009-12-27 22:27:11 +01004125 if (right_response == NULL) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004126 dev_err(DEV, "kmalloc of right_response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004127 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004128 goto fail;
4129 }
4130
4131 sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
4132
4133 rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
4134 if (rv) {
4135 dev_err(DEV, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004136 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004137 goto fail;
4138 }
4139
4140 rv = !memcmp(response, right_response, resp_size);
4141
4142 if (rv)
4143 dev_info(DEV, "Peer authenticated using %d bytes of '%s' HMAC\n",
4144 resp_size, mdev->net_conf->cram_hmac_alg);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004145 else
4146 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004147
4148 fail:
4149 kfree(peers_ch);
4150 kfree(response);
4151 kfree(right_response);
4152
4153 return rv;
4154}
4155#endif
4156
4157int drbdd_init(struct drbd_thread *thi)
4158{
4159 struct drbd_conf *mdev = thi->mdev;
4160 unsigned int minor = mdev_to_minor(mdev);
4161 int h;
4162
4163 sprintf(current->comm, "drbd%d_receiver", minor);
4164
4165 dev_info(DEV, "receiver (re)started\n");
4166
4167 do {
4168 h = drbd_connect(mdev);
4169 if (h == 0) {
4170 drbd_disconnect(mdev);
Philipp Reisner20ee6392011-01-18 15:28:59 +01004171 schedule_timeout_interruptible(HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004172 }
4173 if (h == -1) {
4174 dev_warn(DEV, "Discarding network configuration.\n");
4175 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
4176 }
4177 } while (h == 0);
4178
4179 if (h > 0) {
4180 if (get_net_conf(mdev)) {
4181 drbdd(mdev);
4182 put_net_conf(mdev);
4183 }
4184 }
4185
4186 drbd_disconnect(mdev);
4187
4188 dev_info(DEV, "receiver terminated\n");
4189 return 0;
4190}
4191
4192/* ********* acknowledge sender ******** */
4193
Philipp Reisner0b70a132010-08-20 13:36:10 +02004194static int got_RqSReply(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004195{
4196 struct p_req_state_reply *p = (struct p_req_state_reply *)h;
4197
4198 int retcode = be32_to_cpu(p->retcode);
4199
4200 if (retcode >= SS_SUCCESS) {
4201 set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
4202 } else {
4203 set_bit(CL_ST_CHG_FAIL, &mdev->flags);
4204 dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
4205 drbd_set_st_err_str(retcode), retcode);
4206 }
4207 wake_up(&mdev->state_wait);
4208
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004209 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004210}
4211
Philipp Reisner0b70a132010-08-20 13:36:10 +02004212static int got_Ping(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004213{
4214 return drbd_send_ping_ack(mdev);
4215
4216}
4217
Philipp Reisner0b70a132010-08-20 13:36:10 +02004218static int got_PingAck(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004219{
4220 /* restore idle timeout */
4221 mdev->meta.socket->sk->sk_rcvtimeo = mdev->net_conf->ping_int*HZ;
Philipp Reisner309d1602010-03-02 15:03:44 +01004222 if (!test_and_set_bit(GOT_PING_ACK, &mdev->flags))
4223 wake_up(&mdev->misc_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004224
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004225 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004226}
4227
Philipp Reisner0b70a132010-08-20 13:36:10 +02004228static int got_IsInSync(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004229{
4230 struct p_block_ack *p = (struct p_block_ack *)h;
4231 sector_t sector = be64_to_cpu(p->sector);
4232 int blksize = be32_to_cpu(p->blksize);
4233
4234 D_ASSERT(mdev->agreed_pro_version >= 89);
4235
4236 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4237
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004238 if (get_ldev(mdev)) {
4239 drbd_rs_complete_io(mdev, sector);
4240 drbd_set_in_sync(mdev, sector, blksize);
4241 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
4242 mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
4243 put_ldev(mdev);
4244 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004245 dec_rs_pending(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02004246 atomic_add(blksize >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004247
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004248 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004249}
4250
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004251static int
4252validate_req_change_req_state(struct drbd_conf *mdev, u64 id, sector_t sector,
4253 struct rb_root *root, const char *func,
4254 enum drbd_req_event what, bool missing_ok)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004255{
4256 struct drbd_request *req;
4257 struct bio_and_error m;
4258
4259 spin_lock_irq(&mdev->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004260 req = find_request(mdev, root, id, sector, missing_ok, func);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004261 if (unlikely(!req)) {
4262 spin_unlock_irq(&mdev->req_lock);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004263 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004264 }
4265 __req_mod(req, what, &m);
4266 spin_unlock_irq(&mdev->req_lock);
4267
4268 if (m.bio)
4269 complete_master_bio(mdev, &m);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004270 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004271}
4272
Philipp Reisner0b70a132010-08-20 13:36:10 +02004273static int got_BlockAck(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004274{
4275 struct p_block_ack *p = (struct p_block_ack *)h;
4276 sector_t sector = be64_to_cpu(p->sector);
4277 int blksize = be32_to_cpu(p->blksize);
4278 enum drbd_req_event what;
4279
4280 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4281
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004282 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004283 drbd_set_in_sync(mdev, sector, blksize);
4284 dec_rs_pending(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004285 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004286 }
4287 switch (be16_to_cpu(h->command)) {
4288 case P_RS_WRITE_ACK:
4289 D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
4290 what = write_acked_by_peer_and_sis;
4291 break;
4292 case P_WRITE_ACK:
4293 D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
4294 what = write_acked_by_peer;
4295 break;
4296 case P_RECV_ACK:
4297 D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_B);
4298 what = recv_acked_by_peer;
4299 break;
4300 case P_DISCARD_ACK:
4301 D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
4302 what = conflict_discarded_by_peer;
4303 break;
4304 default:
4305 D_ASSERT(0);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004306 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004307 }
4308
4309 return validate_req_change_req_state(mdev, p->block_id, sector,
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004310 &mdev->write_requests, __func__,
4311 what, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004312}
4313
Philipp Reisner0b70a132010-08-20 13:36:10 +02004314static int got_NegAck(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004315{
4316 struct p_block_ack *p = (struct p_block_ack *)h;
4317 sector_t sector = be64_to_cpu(p->sector);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004318 int size = be32_to_cpu(p->blksize);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004319 bool missing_ok = mdev->net_conf->wire_protocol == DRBD_PROT_A ||
4320 mdev->net_conf->wire_protocol == DRBD_PROT_B;
4321 bool found;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004322
4323 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4324
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004325 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004326 dec_rs_pending(mdev);
4327 drbd_rs_failed_io(mdev, sector, size);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004328 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004329 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01004330
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004331 found = validate_req_change_req_state(mdev, p->block_id, sector,
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004332 &mdev->write_requests, __func__,
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004333 neg_acked, missing_ok);
4334 if (!found) {
4335 /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs.
4336 The master bio might already be completed, therefore the
4337 request is no longer in the collision hash. */
4338 /* In Protocol B we might already have got a P_RECV_ACK
4339 but then get a P_NEG_ACK afterwards. */
4340 if (!missing_ok)
Philipp Reisner2deb8332011-01-17 18:39:18 +01004341 return false;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004342 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004343 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01004344 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004345}
4346
Philipp Reisner0b70a132010-08-20 13:36:10 +02004347static int got_NegDReply(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004348{
4349 struct p_block_ack *p = (struct p_block_ack *)h;
4350 sector_t sector = be64_to_cpu(p->sector);
4351
4352 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4353 dev_err(DEV, "Got NegDReply; Sector %llus, len %u; Fail original request.\n",
4354 (unsigned long long)sector, be32_to_cpu(p->blksize));
4355
4356 return validate_req_change_req_state(mdev, p->block_id, sector,
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004357 &mdev->read_requests, __func__,
4358 neg_acked, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004359}
4360
Philipp Reisner0b70a132010-08-20 13:36:10 +02004361static int got_NegRSDReply(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004362{
4363 sector_t sector;
4364 int size;
4365 struct p_block_ack *p = (struct p_block_ack *)h;
4366
4367 sector = be64_to_cpu(p->sector);
4368 size = be32_to_cpu(p->blksize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004369
4370 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4371
4372 dec_rs_pending(mdev);
4373
4374 if (get_ldev_if_state(mdev, D_FAILED)) {
4375 drbd_rs_complete_io(mdev, sector);
Philipp Reisnerd612d302010-12-27 10:53:28 +01004376 switch (be16_to_cpu(h->command)) {
4377 case P_NEG_RS_DREPLY:
4378 drbd_rs_failed_io(mdev, sector, size);
4379 case P_RS_CANCEL:
4380 break;
4381 default:
4382 D_ASSERT(0);
4383 put_ldev(mdev);
4384 return false;
4385 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004386 put_ldev(mdev);
4387 }
4388
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004389 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004390}
4391
Philipp Reisner0b70a132010-08-20 13:36:10 +02004392static int got_BarrierAck(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004393{
4394 struct p_barrier_ack *p = (struct p_barrier_ack *)h;
4395
4396 tl_release(mdev, p->barrier, be32_to_cpu(p->set_size));
4397
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02004398 if (mdev->state.conn == C_AHEAD &&
4399 atomic_read(&mdev->ap_in_flight) == 0 &&
Philipp Reisner370a43e2011-01-14 16:03:11 +01004400 !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->current_epoch->flags)) {
4401 mdev->start_resync_timer.expires = jiffies + HZ;
4402 add_timer(&mdev->start_resync_timer);
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02004403 }
4404
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004405 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004406}
4407
Philipp Reisner0b70a132010-08-20 13:36:10 +02004408static int got_OVResult(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004409{
4410 struct p_block_ack *p = (struct p_block_ack *)h;
4411 struct drbd_work *w;
4412 sector_t sector;
4413 int size;
4414
4415 sector = be64_to_cpu(p->sector);
4416 size = be32_to_cpu(p->blksize);
4417
4418 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4419
4420 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
4421 drbd_ov_oos_found(mdev, sector, size);
4422 else
4423 ov_oos_print(mdev);
4424
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004425 if (!get_ldev(mdev))
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004426 return true;
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004427
Philipp Reisnerb411b362009-09-25 16:07:19 -07004428 drbd_rs_complete_io(mdev, sector);
4429 dec_rs_pending(mdev);
4430
Lars Ellenbergea5442a2010-11-05 09:48:01 +01004431 --mdev->ov_left;
4432
4433 /* let's advance progress step marks only for every other megabyte */
4434 if ((mdev->ov_left & 0x200) == 0x200)
4435 drbd_advance_rs_marks(mdev, mdev->ov_left);
4436
4437 if (mdev->ov_left == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004438 w = kmalloc(sizeof(*w), GFP_NOIO);
4439 if (w) {
4440 w->cb = w_ov_finished;
4441 drbd_queue_work_front(&mdev->data.work, w);
4442 } else {
4443 dev_err(DEV, "kmalloc(w) failed.");
4444 ov_oos_print(mdev);
4445 drbd_resync_finished(mdev);
4446 }
4447 }
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004448 put_ldev(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004449 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004450}
4451
Philipp Reisner02918be2010-08-20 14:35:10 +02004452static int got_skip(struct drbd_conf *mdev, struct p_header80 *h)
Philipp Reisner0ced55a2010-04-30 15:26:20 +02004453{
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004454 return true;
Philipp Reisner0ced55a2010-04-30 15:26:20 +02004455}
4456
Philipp Reisnerb411b362009-09-25 16:07:19 -07004457struct asender_cmd {
4458 size_t pkt_size;
Philipp Reisner0b70a132010-08-20 13:36:10 +02004459 int (*process)(struct drbd_conf *mdev, struct p_header80 *h);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004460};
4461
4462static struct asender_cmd *get_asender_cmd(int cmd)
4463{
4464 static struct asender_cmd asender_tbl[] = {
4465 /* anything missing from this table is in
4466 * the drbd_cmd_handler (drbd_default_handler) table,
4467 * see the beginning of drbdd() */
Philipp Reisner0b70a132010-08-20 13:36:10 +02004468 [P_PING] = { sizeof(struct p_header80), got_Ping },
4469 [P_PING_ACK] = { sizeof(struct p_header80), got_PingAck },
Philipp Reisnerb411b362009-09-25 16:07:19 -07004470 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4471 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4472 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4473 [P_DISCARD_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4474 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
4475 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
4476 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply},
4477 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
4478 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
4479 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
4480 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
Philipp Reisner02918be2010-08-20 14:35:10 +02004481 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
Philipp Reisnerd612d302010-12-27 10:53:28 +01004482 [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply},
Philipp Reisnerb411b362009-09-25 16:07:19 -07004483 [P_MAX_CMD] = { 0, NULL },
4484 };
4485 if (cmd > P_MAX_CMD || asender_tbl[cmd].process == NULL)
4486 return NULL;
4487 return &asender_tbl[cmd];
4488}
4489
4490int drbd_asender(struct drbd_thread *thi)
4491{
4492 struct drbd_conf *mdev = thi->mdev;
Philipp Reisner02918be2010-08-20 14:35:10 +02004493 struct p_header80 *h = &mdev->meta.rbuf.header.h80;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004494 struct asender_cmd *cmd = NULL;
4495
4496 int rv, len;
4497 void *buf = h;
4498 int received = 0;
Philipp Reisner0b70a132010-08-20 13:36:10 +02004499 int expect = sizeof(struct p_header80);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004500 int empty;
Lars Ellenbergf36af182011-03-09 22:44:55 +01004501 int ping_timeout_active = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004502
4503 sprintf(current->comm, "drbd%d_asender", mdev_to_minor(mdev));
4504
4505 current->policy = SCHED_RR; /* Make this a realtime task! */
4506 current->rt_priority = 2; /* more important than all other tasks */
4507
4508 while (get_t_state(thi) == Running) {
4509 drbd_thread_current_set_cpu(mdev);
4510 if (test_and_clear_bit(SEND_PING, &mdev->flags)) {
4511 ERR_IF(!drbd_send_ping(mdev)) goto reconnect;
4512 mdev->meta.socket->sk->sk_rcvtimeo =
4513 mdev->net_conf->ping_timeo*HZ/10;
Lars Ellenbergf36af182011-03-09 22:44:55 +01004514 ping_timeout_active = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004515 }
4516
4517 /* conditionally cork;
4518 * it may hurt latency if we cork without much to send */
4519 if (!mdev->net_conf->no_cork &&
4520 3 < atomic_read(&mdev->unacked_cnt))
4521 drbd_tcp_cork(mdev->meta.socket);
4522 while (1) {
4523 clear_bit(SIGNAL_ASENDER, &mdev->flags);
4524 flush_signals(current);
Lars Ellenberg0f8488e2010-10-13 18:19:23 +02004525 if (!drbd_process_done_ee(mdev))
Philipp Reisnerb411b362009-09-25 16:07:19 -07004526 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004527 /* to avoid race with newly queued ACKs */
4528 set_bit(SIGNAL_ASENDER, &mdev->flags);
4529 spin_lock_irq(&mdev->req_lock);
4530 empty = list_empty(&mdev->done_ee);
4531 spin_unlock_irq(&mdev->req_lock);
4532 /* new ack may have been queued right here,
4533 * but then there is also a signal pending,
4534 * and we start over... */
4535 if (empty)
4536 break;
4537 }
4538 /* but unconditionally uncork unless disabled */
4539 if (!mdev->net_conf->no_cork)
4540 drbd_tcp_uncork(mdev->meta.socket);
4541
4542 /* short circuit, recv_msg would return EINTR anyways. */
4543 if (signal_pending(current))
4544 continue;
4545
4546 rv = drbd_recv_short(mdev, mdev->meta.socket,
4547 buf, expect-received, 0);
4548 clear_bit(SIGNAL_ASENDER, &mdev->flags);
4549
4550 flush_signals(current);
4551
4552 /* Note:
4553 * -EINTR (on meta) we got a signal
4554 * -EAGAIN (on meta) rcvtimeo expired
4555 * -ECONNRESET other side closed the connection
4556 * -ERESTARTSYS (on data) we got a signal
4557 * rv < 0 other than above: unexpected error!
4558 * rv == expected: full header or command
4559 * rv < expected: "woken" by signal during receive
4560 * rv == 0 : "connection shut down by peer"
4561 */
4562 if (likely(rv > 0)) {
4563 received += rv;
4564 buf += rv;
4565 } else if (rv == 0) {
4566 dev_err(DEV, "meta connection shut down by peer.\n");
4567 goto reconnect;
4568 } else if (rv == -EAGAIN) {
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02004569 /* If the data socket received something meanwhile,
4570 * that is good enough: peer is still alive. */
4571 if (time_after(mdev->last_received,
4572 jiffies - mdev->meta.socket->sk->sk_rcvtimeo))
4573 continue;
Lars Ellenbergf36af182011-03-09 22:44:55 +01004574 if (ping_timeout_active) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004575 dev_err(DEV, "PingAck did not arrive in time.\n");
4576 goto reconnect;
4577 }
4578 set_bit(SEND_PING, &mdev->flags);
4579 continue;
4580 } else if (rv == -EINTR) {
4581 continue;
4582 } else {
4583 dev_err(DEV, "sock_recvmsg returned %d\n", rv);
4584 goto reconnect;
4585 }
4586
4587 if (received == expect && cmd == NULL) {
Andreas Gruenbacherca9bc122011-01-11 13:47:24 +01004588 if (unlikely(h->magic != cpu_to_be32(DRBD_MAGIC))) {
Lars Ellenberg004352f2010-10-05 20:13:58 +02004589 dev_err(DEV, "magic?? on meta m: 0x%08x c: %d l: %d\n",
4590 be32_to_cpu(h->magic),
4591 be16_to_cpu(h->command),
4592 be16_to_cpu(h->length));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004593 goto reconnect;
4594 }
4595 cmd = get_asender_cmd(be16_to_cpu(h->command));
4596 len = be16_to_cpu(h->length);
4597 if (unlikely(cmd == NULL)) {
Lars Ellenberg004352f2010-10-05 20:13:58 +02004598 dev_err(DEV, "unknown command?? on meta m: 0x%08x c: %d l: %d\n",
4599 be32_to_cpu(h->magic),
4600 be16_to_cpu(h->command),
4601 be16_to_cpu(h->length));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004602 goto disconnect;
4603 }
4604 expect = cmd->pkt_size;
Philipp Reisner0b70a132010-08-20 13:36:10 +02004605 ERR_IF(len != expect-sizeof(struct p_header80))
Philipp Reisnerb411b362009-09-25 16:07:19 -07004606 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004607 }
4608 if (received == expect) {
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02004609 mdev->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004610 D_ASSERT(cmd != NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004611 if (!cmd->process(mdev, h))
4612 goto reconnect;
4613
Lars Ellenbergf36af182011-03-09 22:44:55 +01004614 /* the idle_timeout (ping-int)
4615 * has been restored in got_PingAck() */
4616 if (cmd == get_asender_cmd(P_PING_ACK))
4617 ping_timeout_active = 0;
4618
Philipp Reisnerb411b362009-09-25 16:07:19 -07004619 buf = h;
4620 received = 0;
Philipp Reisner0b70a132010-08-20 13:36:10 +02004621 expect = sizeof(struct p_header80);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004622 cmd = NULL;
4623 }
4624 }
4625
4626 if (0) {
4627reconnect:
4628 drbd_force_state(mdev, NS(conn, C_NETWORK_FAILURE));
Lars Ellenberg856c50c2010-10-14 13:37:40 +02004629 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004630 }
4631 if (0) {
4632disconnect:
4633 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Lars Ellenberg856c50c2010-10-14 13:37:40 +02004634 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004635 }
4636 clear_bit(SIGNAL_ASENDER, &mdev->flags);
4637
4638 D_ASSERT(mdev->state.conn < C_CONNECTED);
4639 dev_info(DEV, "asender terminated\n");
4640
4641 return 0;
4642}