blob: b84a9c9fd3f84321762b1d786b402c8f3bba3660 [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 Reisner87eeee42011-01-19 14:16:30 +0100213 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700214 reclaim_net_ee(mdev, &reclaimed);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100215 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700216
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. */
Philipp Reisner89e58e72011-01-19 13:12:45 +0100240 if (atomic_read(&mdev->pp_in_use) < mdev->tconn->net_conf->max_buffers)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200241 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
Philipp Reisner89e58e72011-01-19 13:12:45 +0100248 if (atomic_read(&mdev->pp_in_use) < mdev->tconn->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.
Philipp Reisner87eeee42011-01-19 14:16:30 +0100272 * Is also used from inside an other spin_lock_irq(&mdev->tconn->req_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200273 * 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
Andreas Gruenbacher8b946252011-01-20 15:23:07 +0100336 drbd_clear_interval(&e->i);
Andreas Gruenbacher53840642011-01-28 10:31:04 +0100337 e->i.size = data_size;
338 e->i.sector = sector;
339 e->i.waiting = false;
340
Philipp Reisnerb411b362009-09-25 16:07:19 -0700341 e->epoch = NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200342 e->mdev = mdev;
343 e->pages = page;
344 atomic_set(&e->pending_bios, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700345 e->flags = 0;
Andreas Gruenbacher9a8e7752011-01-11 14:04:09 +0100346 /*
347 * The block_id is opaque to the receiver. It is not endianness
348 * converted, and sent back to the sender unchanged.
349 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200350 e->block_id = id;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700351
Philipp Reisnerb411b362009-09-25 16:07:19 -0700352 return e;
353
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200354 fail:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700355 mempool_free(e, drbd_ee_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700356 return NULL;
357}
358
Lars Ellenberg435f0742010-09-06 12:30:25 +0200359void drbd_free_some_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e, int is_net)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700360{
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +0200361 if (e->flags & EE_HAS_DIGEST)
362 kfree(e->digest);
Lars Ellenberg435f0742010-09-06 12:30:25 +0200363 drbd_pp_free(mdev, e->pages, is_net);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200364 D_ASSERT(atomic_read(&e->pending_bios) == 0);
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
Philipp Reisner87eeee42011-01-19 14:16:30 +0100376 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700377 list_splice_init(list, &work_list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100378 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700379
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_
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100390 * but see also comments in _req_mod(,BARRIER_ACKED)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700391 * 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
Philipp Reisner87eeee42011-01-19 14:16:30 +0100404 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700405 reclaim_net_ee(mdev, &reclaimed);
406 list_splice_init(&mdev->done_ee, &work_list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100407 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700408
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);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100434 spin_unlock_irq(&mdev->tconn->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);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100437 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700438 }
439}
440
441void drbd_wait_ee_list_empty(struct drbd_conf *mdev, struct list_head *head)
442{
Philipp Reisner87eeee42011-01-19 14:16:30 +0100443 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700444 _drbd_wait_ee_list_empty(mdev, head);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100445 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700446}
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 (;;) {
Philipp Reisnere42325a2011-01-19 13:55:45 +0100521 rv = sock_recvmsg(mdev->tconn->data.socket, &msg, size, msg.msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700522 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
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +0100583 if (!get_net_conf(mdev->tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700584 return NULL;
585
586 what = "sock_create_kern";
Philipp Reisner89e58e72011-01-19 13:12:45 +0100587 err = sock_create_kern(((struct sockaddr *)mdev->tconn->net_conf->my_addr)->sa_family,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700588 SOCK_STREAM, IPPROTO_TCP, &sock);
589 if (err < 0) {
590 sock = NULL;
591 goto out;
592 }
593
594 sock->sk->sk_rcvtimeo =
Philipp Reisner89e58e72011-01-19 13:12:45 +0100595 sock->sk->sk_sndtimeo = mdev->tconn->net_conf->try_connect_int*HZ;
596 drbd_setbufsize(sock, mdev->tconn->net_conf->sndbuf_size,
597 mdev->tconn->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 */
Philipp Reisner89e58e72011-01-19 13:12:45 +0100606 memcpy(&src_in6, mdev->tconn->net_conf->my_addr,
607 min_t(int, mdev->tconn->net_conf->my_addr_len, sizeof(src_in6)));
608 if (((struct sockaddr *)mdev->tconn->net_conf->my_addr)->sa_family == AF_INET6)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700609 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,
Philipp Reisner89e58e72011-01-19 13:12:45 +0100616 mdev->tconn->net_conf->my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700617 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,
Philipp Reisner89e58e72011-01-19 13:12:45 +0100625 (struct sockaddr *)mdev->tconn->net_conf->peer_addr,
626 mdev->tconn->net_conf->peer_addr_len, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700627
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 }
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +0100649 put_net_conf(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700650 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
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +0100659 if (!get_net_conf(mdev->tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700660 return NULL;
661
662 what = "sock_create_kern";
Philipp Reisner89e58e72011-01-19 13:12:45 +0100663 err = sock_create_kern(((struct sockaddr *)mdev->tconn->net_conf->my_addr)->sa_family,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700664 SOCK_STREAM, IPPROTO_TCP, &s_listen);
665 if (err) {
666 s_listen = NULL;
667 goto out;
668 }
669
Philipp Reisner89e58e72011-01-19 13:12:45 +0100670 timeo = mdev->tconn->net_conf->try_connect_int * HZ;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700671 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;
Philipp Reisner89e58e72011-01-19 13:12:45 +0100676 drbd_setbufsize(s_listen, mdev->tconn->net_conf->sndbuf_size,
677 mdev->tconn->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,
Philipp Reisner89e58e72011-01-19 13:12:45 +0100681 (struct sockaddr *) mdev->tconn->net_conf->my_addr,
682 mdev->tconn->net_conf->my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700683 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 }
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +0100697 put_net_conf(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700698
699 return s_estab;
700}
701
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100702static int drbd_send_fp(struct drbd_conf *mdev, struct socket *sock,
703 enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700704{
Philipp Reisnerc0129492011-01-19 16:58:16 +0100705 struct p_header *h = &mdev->tconn->data.sbuf.header;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700706
707 return _drbd_send_cmd(mdev, sock, cmd, h, sizeof(*h), 0);
708}
709
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100710static enum drbd_packet drbd_recv_fp(struct drbd_conf *mdev,
711 struct socket *sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700712{
Philipp Reisnere42325a2011-01-19 13:55:45 +0100713 struct p_header80 *h = &mdev->tconn->data.rbuf.header.h80;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700714 int rr;
715
716 rr = drbd_recv_short(mdev, sock, h, sizeof(*h), 0);
717
Andreas Gruenbacherca9bc122011-01-11 13:47:24 +0100718 if (rr == sizeof(*h) && h->magic == cpu_to_be32(DRBD_MAGIC))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700719 return be16_to_cpu(h->command);
720
721 return 0xffff;
722}
723
724/**
725 * drbd_socket_okay() - Free the socket if its connection is not okay
726 * @mdev: DRBD device.
727 * @sock: pointer to the pointer to the socket.
728 */
729static int drbd_socket_okay(struct drbd_conf *mdev, struct socket **sock)
730{
731 int rr;
732 char tb[4];
733
734 if (!*sock)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100735 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700736
737 rr = drbd_recv_short(mdev, *sock, tb, 4, MSG_DONTWAIT | MSG_PEEK);
738
739 if (rr > 0 || rr == -EAGAIN) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100740 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700741 } else {
742 sock_release(*sock);
743 *sock = NULL;
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100744 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700745 }
746}
747
748/*
749 * return values:
750 * 1 yes, we have a valid connection
751 * 0 oops, did not work out, please try again
752 * -1 peer talks different language,
753 * no point in trying again, please go standalone.
754 * -2 We do not have a network config...
755 */
756static int drbd_connect(struct drbd_conf *mdev)
757{
758 struct socket *s, *sock, *msock;
759 int try, h, ok;
760
Philipp Reisnere42325a2011-01-19 13:55:45 +0100761 D_ASSERT(!mdev->tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700762
Philipp Reisnerb411b362009-09-25 16:07:19 -0700763 if (drbd_request_state(mdev, NS(conn, C_WF_CONNECTION)) < SS_SUCCESS)
764 return -2;
765
766 clear_bit(DISCARD_CONCURRENT, &mdev->flags);
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100767 mdev->tconn->agreed_pro_version = 99;
768 /* agreed_pro_version must be smaller than 100 so we send the old
769 header (h80) in the first packet and in the handshake packet. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700770
771 sock = NULL;
772 msock = NULL;
773
774 do {
775 for (try = 0;;) {
776 /* 3 tries, this should take less than a second! */
777 s = drbd_try_connect(mdev);
778 if (s || ++try >= 3)
779 break;
780 /* give the other side time to call bind() & listen() */
Philipp Reisner20ee6392011-01-18 15:28:59 +0100781 schedule_timeout_interruptible(HZ / 10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700782 }
783
784 if (s) {
785 if (!sock) {
786 drbd_send_fp(mdev, s, P_HAND_SHAKE_S);
787 sock = s;
788 s = NULL;
789 } else if (!msock) {
790 drbd_send_fp(mdev, s, P_HAND_SHAKE_M);
791 msock = s;
792 s = NULL;
793 } else {
794 dev_err(DEV, "Logic error in drbd_connect()\n");
795 goto out_release_sockets;
796 }
797 }
798
799 if (sock && msock) {
Philipp Reisner89e58e72011-01-19 13:12:45 +0100800 schedule_timeout_interruptible(mdev->tconn->net_conf->ping_timeo*HZ/10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700801 ok = drbd_socket_okay(mdev, &sock);
802 ok = drbd_socket_okay(mdev, &msock) && ok;
803 if (ok)
804 break;
805 }
806
807retry:
808 s = drbd_wait_for_connect(mdev);
809 if (s) {
810 try = drbd_recv_fp(mdev, s);
811 drbd_socket_okay(mdev, &sock);
812 drbd_socket_okay(mdev, &msock);
813 switch (try) {
814 case P_HAND_SHAKE_S:
815 if (sock) {
816 dev_warn(DEV, "initial packet S crossed\n");
817 sock_release(sock);
818 }
819 sock = s;
820 break;
821 case P_HAND_SHAKE_M:
822 if (msock) {
823 dev_warn(DEV, "initial packet M crossed\n");
824 sock_release(msock);
825 }
826 msock = s;
827 set_bit(DISCARD_CONCURRENT, &mdev->flags);
828 break;
829 default:
830 dev_warn(DEV, "Error receiving initial packet\n");
831 sock_release(s);
832 if (random32() & 1)
833 goto retry;
834 }
835 }
836
837 if (mdev->state.conn <= C_DISCONNECTING)
838 goto out_release_sockets;
839 if (signal_pending(current)) {
840 flush_signals(current);
841 smp_rmb();
Philipp Reisnere6b3ea82011-01-19 14:02:01 +0100842 if (get_t_state(&mdev->tconn->receiver) == EXITING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700843 goto out_release_sockets;
844 }
845
846 if (sock && msock) {
847 ok = drbd_socket_okay(mdev, &sock);
848 ok = drbd_socket_okay(mdev, &msock) && ok;
849 if (ok)
850 break;
851 }
852 } while (1);
853
854 msock->sk->sk_reuse = 1; /* SO_REUSEADDR */
855 sock->sk->sk_reuse = 1; /* SO_REUSEADDR */
856
857 sock->sk->sk_allocation = GFP_NOIO;
858 msock->sk->sk_allocation = GFP_NOIO;
859
860 sock->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
861 msock->sk->sk_priority = TC_PRIO_INTERACTIVE;
862
Philipp Reisnerb411b362009-09-25 16:07:19 -0700863 /* NOT YET ...
Philipp Reisner89e58e72011-01-19 13:12:45 +0100864 * sock->sk->sk_sndtimeo = mdev->tconn->net_conf->timeout*HZ/10;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700865 * sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
866 * first set it to the P_HAND_SHAKE timeout,
867 * which we set to 4x the configured ping_timeout. */
868 sock->sk->sk_sndtimeo =
Philipp Reisner89e58e72011-01-19 13:12:45 +0100869 sock->sk->sk_rcvtimeo = mdev->tconn->net_conf->ping_timeo*4*HZ/10;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700870
Philipp Reisner89e58e72011-01-19 13:12:45 +0100871 msock->sk->sk_sndtimeo = mdev->tconn->net_conf->timeout*HZ/10;
872 msock->sk->sk_rcvtimeo = mdev->tconn->net_conf->ping_int*HZ;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700873
874 /* we don't want delays.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300875 * we use TCP_CORK where appropriate, though */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700876 drbd_tcp_nodelay(sock);
877 drbd_tcp_nodelay(msock);
878
Philipp Reisnere42325a2011-01-19 13:55:45 +0100879 mdev->tconn->data.socket = sock;
880 mdev->tconn->meta.socket = msock;
Philipp Reisner31890f42011-01-19 14:12:51 +0100881 mdev->tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700882
Philipp Reisnere6b3ea82011-01-19 14:02:01 +0100883 D_ASSERT(mdev->tconn->asender.task == NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700884
885 h = drbd_do_handshake(mdev);
886 if (h <= 0)
887 return h;
888
Philipp Reisnera0638452011-01-19 14:31:32 +0100889 if (mdev->tconn->cram_hmac_tfm) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700890 /* drbd_request_state(mdev, NS(conn, WFAuth)); */
Johannes Thomab10d96c2010-01-07 16:02:50 +0100891 switch (drbd_do_auth(mdev)) {
892 case -1:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700893 dev_err(DEV, "Authentication of peer failed\n");
894 return -1;
Johannes Thomab10d96c2010-01-07 16:02:50 +0100895 case 0:
896 dev_err(DEV, "Authentication of peer failed, trying again.\n");
897 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700898 }
899 }
900
901 if (drbd_request_state(mdev, NS(conn, C_WF_REPORT_PARAMS)) < SS_SUCCESS)
902 return 0;
903
Philipp Reisner89e58e72011-01-19 13:12:45 +0100904 sock->sk->sk_sndtimeo = mdev->tconn->net_conf->timeout*HZ/10;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700905 sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
906
907 atomic_set(&mdev->packet_seq, 0);
908 mdev->peer_seq = 0;
909
Philipp Reisnere6b3ea82011-01-19 14:02:01 +0100910 drbd_thread_start(&mdev->tconn->asender);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700911
Philipp Reisner148efa12011-01-15 00:21:15 +0100912 if (drbd_send_protocol(mdev) == -1)
Philipp Reisner7e2455c2010-04-22 14:50:23 +0200913 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700914 drbd_send_sync_param(mdev, &mdev->sync_conf);
Philipp Reisnere89b5912010-03-24 17:11:33 +0100915 drbd_send_sizes(mdev, 0, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700916 drbd_send_uuids(mdev);
917 drbd_send_state(mdev);
918 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
919 clear_bit(RESIZE_PENDING, &mdev->flags);
Philipp Reisner7fde2be2011-03-01 11:08:28 +0100920 mod_timer(&mdev->request_timer, jiffies + HZ); /* just start it here. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700921
922 return 1;
923
924out_release_sockets:
925 if (sock)
926 sock_release(sock);
927 if (msock)
928 sock_release(msock);
929 return -1;
930}
931
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100932static bool decode_header(struct drbd_conf *mdev, struct p_header *h,
933 enum drbd_packet *cmd, unsigned int *packet_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700934{
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100935 if (h->h80.magic == cpu_to_be32(DRBD_MAGIC)) {
Philipp Reisner02918be2010-08-20 14:35:10 +0200936 *cmd = be16_to_cpu(h->h80.command);
937 *packet_size = be16_to_cpu(h->h80.length);
Andreas Gruenbacherca9bc122011-01-11 13:47:24 +0100938 } else if (h->h95.magic == cpu_to_be16(DRBD_MAGIC_BIG)) {
Philipp Reisner02918be2010-08-20 14:35:10 +0200939 *cmd = be16_to_cpu(h->h95.command);
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100940 *packet_size = be32_to_cpu(h->h95.length) & 0x00ffffff;
Philipp Reisner02918be2010-08-20 14:35:10 +0200941 } else {
Lars Ellenberg004352f2010-10-05 20:13:58 +0200942 dev_err(DEV, "magic?? on data m: 0x%08x c: %d l: %d\n",
943 be32_to_cpu(h->h80.magic),
944 be16_to_cpu(h->h80.command),
945 be16_to_cpu(h->h80.length));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100946 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700947 }
Philipp Reisner257d0af2011-01-26 12:15:29 +0100948 return true;
949}
950
Andreas Gruenbacherd8763022011-01-26 17:39:41 +0100951static int drbd_recv_header(struct drbd_conf *mdev, enum drbd_packet *cmd,
952 unsigned int *packet_size)
Philipp Reisner257d0af2011-01-26 12:15:29 +0100953{
954 struct p_header *h = &mdev->tconn->data.rbuf.header;
955 int r;
956
957 r = drbd_recv(mdev, h, sizeof(*h));
958 if (unlikely(r != sizeof(*h))) {
959 if (!signal_pending(current))
960 dev_warn(DEV, "short read expecting header on sock: r=%d\n", r);
961 return false;
962 }
963
964 r = decode_header(mdev, h, cmd, packet_size);
Philipp Reisner31890f42011-01-19 14:12:51 +0100965 mdev->tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700966
Philipp Reisner257d0af2011-01-26 12:15:29 +0100967 return r;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700968}
969
Philipp Reisner2451fc32010-08-24 13:43:11 +0200970static void drbd_flush(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700971{
972 int rv;
973
974 if (mdev->write_ordering >= WO_bdev_flush && get_ldev(mdev)) {
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400975 rv = blkdev_issue_flush(mdev->ldev->backing_bdev, GFP_KERNEL,
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200976 NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700977 if (rv) {
978 dev_err(DEV, "local disk flush failed with status %d\n", rv);
979 /* would rather check on EOPNOTSUPP, but that is not reliable.
980 * don't try again for ANY return value != 0
981 * if (rv == -EOPNOTSUPP) */
982 drbd_bump_write_ordering(mdev, WO_drain_io);
983 }
984 put_ldev(mdev);
985 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700986}
987
988/**
989 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
990 * @mdev: DRBD device.
991 * @epoch: Epoch object.
992 * @ev: Epoch event.
993 */
994static enum finish_epoch drbd_may_finish_epoch(struct drbd_conf *mdev,
995 struct drbd_epoch *epoch,
996 enum epoch_event ev)
997{
Philipp Reisner2451fc32010-08-24 13:43:11 +0200998 int epoch_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700999 struct drbd_epoch *next_epoch;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001000 enum finish_epoch rv = FE_STILL_LIVE;
1001
1002 spin_lock(&mdev->epoch_lock);
1003 do {
1004 next_epoch = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001005
1006 epoch_size = atomic_read(&epoch->epoch_size);
1007
1008 switch (ev & ~EV_CLEANUP) {
1009 case EV_PUT:
1010 atomic_dec(&epoch->active);
1011 break;
1012 case EV_GOT_BARRIER_NR:
1013 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001014 break;
1015 case EV_BECAME_LAST:
1016 /* nothing to do*/
1017 break;
1018 }
1019
Philipp Reisnerb411b362009-09-25 16:07:19 -07001020 if (epoch_size != 0 &&
1021 atomic_read(&epoch->active) == 0 &&
Philipp Reisner2451fc32010-08-24 13:43:11 +02001022 test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001023 if (!(ev & EV_CLEANUP)) {
1024 spin_unlock(&mdev->epoch_lock);
1025 drbd_send_b_ack(mdev, epoch->barrier_nr, epoch_size);
1026 spin_lock(&mdev->epoch_lock);
1027 }
1028 dec_unacked(mdev);
1029
1030 if (mdev->current_epoch != epoch) {
1031 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1032 list_del(&epoch->list);
1033 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
1034 mdev->epochs--;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001035 kfree(epoch);
1036
1037 if (rv == FE_STILL_LIVE)
1038 rv = FE_DESTROYED;
1039 } else {
1040 epoch->flags = 0;
1041 atomic_set(&epoch->epoch_size, 0);
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001042 /* atomic_set(&epoch->active, 0); is already zero */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001043 if (rv == FE_STILL_LIVE)
1044 rv = FE_RECYCLED;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001045 wake_up(&mdev->ee_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001046 }
1047 }
1048
1049 if (!next_epoch)
1050 break;
1051
1052 epoch = next_epoch;
1053 } while (1);
1054
1055 spin_unlock(&mdev->epoch_lock);
1056
Philipp Reisnerb411b362009-09-25 16:07:19 -07001057 return rv;
1058}
1059
1060/**
1061 * drbd_bump_write_ordering() - Fall back to an other write ordering method
1062 * @mdev: DRBD device.
1063 * @wo: Write ordering method to try.
1064 */
1065void drbd_bump_write_ordering(struct drbd_conf *mdev, enum write_ordering_e wo) __must_hold(local)
1066{
1067 enum write_ordering_e pwo;
1068 static char *write_ordering_str[] = {
1069 [WO_none] = "none",
1070 [WO_drain_io] = "drain",
1071 [WO_bdev_flush] = "flush",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001072 };
1073
1074 pwo = mdev->write_ordering;
1075 wo = min(pwo, wo);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001076 if (wo == WO_bdev_flush && mdev->ldev->dc.no_disk_flush)
1077 wo = WO_drain_io;
1078 if (wo == WO_drain_io && mdev->ldev->dc.no_disk_drain)
1079 wo = WO_none;
1080 mdev->write_ordering = wo;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001081 if (pwo != mdev->write_ordering || wo == WO_bdev_flush)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001082 dev_info(DEV, "Method to ensure write ordering: %s\n", write_ordering_str[mdev->write_ordering]);
1083}
1084
1085/**
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001086 * drbd_submit_ee()
1087 * @mdev: DRBD device.
1088 * @e: epoch entry
1089 * @rw: flag field, see bio->bi_rw
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001090 *
1091 * May spread the pages to multiple bios,
1092 * depending on bio_add_page restrictions.
1093 *
1094 * Returns 0 if all bios have been submitted,
1095 * -ENOMEM if we could not allocate enough bios,
1096 * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a
1097 * single page to an empty bio (which should never happen and likely indicates
1098 * that the lower level IO stack is in some way broken). This has been observed
1099 * on certain Xen deployments.
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001100 */
1101/* TODO allocate from our own bio_set. */
1102int drbd_submit_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e,
1103 const unsigned rw, const int fault_type)
1104{
1105 struct bio *bios = NULL;
1106 struct bio *bio;
1107 struct page *page = e->pages;
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001108 sector_t sector = e->i.sector;
1109 unsigned ds = e->i.size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001110 unsigned n_bios = 0;
1111 unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001112 int err = -ENOMEM;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001113
1114 /* In most cases, we will only need one bio. But in case the lower
1115 * level restrictions happen to be different at this offset on this
1116 * side than those of the sending peer, we may need to submit the
1117 * request in more than one bio. */
1118next_bio:
1119 bio = bio_alloc(GFP_NOIO, nr_pages);
1120 if (!bio) {
1121 dev_err(DEV, "submit_ee: Allocation of a bio failed\n");
1122 goto fail;
1123 }
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001124 /* > e->i.sector, unless this is the first bio */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001125 bio->bi_sector = sector;
1126 bio->bi_bdev = mdev->ldev->backing_bdev;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001127 bio->bi_rw = rw;
1128 bio->bi_private = e;
1129 bio->bi_end_io = drbd_endio_sec;
1130
1131 bio->bi_next = bios;
1132 bios = bio;
1133 ++n_bios;
1134
1135 page_chain_for_each(page) {
1136 unsigned len = min_t(unsigned, ds, PAGE_SIZE);
1137 if (!bio_add_page(bio, page, len, 0)) {
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001138 /* A single page must always be possible!
1139 * But in case it fails anyways,
1140 * we deal with it, and complain (below). */
1141 if (bio->bi_vcnt == 0) {
1142 dev_err(DEV,
1143 "bio_add_page failed for len=%u, "
1144 "bi_vcnt=0 (bi_sector=%llu)\n",
1145 len, (unsigned long long)bio->bi_sector);
1146 err = -ENOSPC;
1147 goto fail;
1148 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001149 goto next_bio;
1150 }
1151 ds -= len;
1152 sector += len >> 9;
1153 --nr_pages;
1154 }
1155 D_ASSERT(page == NULL);
1156 D_ASSERT(ds == 0);
1157
1158 atomic_set(&e->pending_bios, n_bios);
1159 do {
1160 bio = bios;
1161 bios = bios->bi_next;
1162 bio->bi_next = NULL;
1163
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001164 drbd_generic_make_request(mdev, fault_type, bio);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001165 } while (bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001166 return 0;
1167
1168fail:
1169 while (bios) {
1170 bio = bios;
1171 bios = bios->bi_next;
1172 bio_put(bio);
1173 }
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001174 return err;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001175}
1176
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001177static void drbd_remove_epoch_entry_interval(struct drbd_conf *mdev,
1178 struct drbd_epoch_entry *e)
1179{
1180 struct drbd_interval *i = &e->i;
1181
1182 drbd_remove_interval(&mdev->write_requests, i);
1183 drbd_clear_interval(i);
1184
1185 /* Wake up any processes waiting for this epoch entry to complete. */
1186 if (i->waiting)
1187 wake_up(&mdev->misc_wait);
1188}
1189
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001190static int receive_Barrier(struct drbd_conf *mdev, enum drbd_packet cmd,
1191 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001192{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001193 int rv;
Philipp Reisnere42325a2011-01-19 13:55:45 +01001194 struct p_barrier *p = &mdev->tconn->data.rbuf.barrier;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001195 struct drbd_epoch *epoch;
1196
Philipp Reisnerb411b362009-09-25 16:07:19 -07001197 inc_unacked(mdev);
1198
Philipp Reisnerb411b362009-09-25 16:07:19 -07001199 mdev->current_epoch->barrier_nr = p->barrier;
1200 rv = drbd_may_finish_epoch(mdev, mdev->current_epoch, EV_GOT_BARRIER_NR);
1201
1202 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
1203 * the activity log, which means it would not be resynced in case the
1204 * R_PRIMARY crashes now.
1205 * Therefore we must send the barrier_ack after the barrier request was
1206 * completed. */
1207 switch (mdev->write_ordering) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001208 case WO_none:
1209 if (rv == FE_RECYCLED)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001210 return true;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001211
1212 /* receiver context, in the writeout path of the other node.
1213 * avoid potential distributed deadlock */
1214 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1215 if (epoch)
1216 break;
1217 else
1218 dev_warn(DEV, "Allocation of an epoch failed, slowing down\n");
1219 /* Fall through */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001220
1221 case WO_bdev_flush:
1222 case WO_drain_io:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001223 drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
Philipp Reisner2451fc32010-08-24 13:43:11 +02001224 drbd_flush(mdev);
1225
1226 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1227 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1228 if (epoch)
1229 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001230 }
1231
Philipp Reisner2451fc32010-08-24 13:43:11 +02001232 epoch = mdev->current_epoch;
1233 wait_event(mdev->ee_wait, atomic_read(&epoch->epoch_size) == 0);
1234
1235 D_ASSERT(atomic_read(&epoch->active) == 0);
1236 D_ASSERT(epoch->flags == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001237
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001238 return true;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001239 default:
1240 dev_err(DEV, "Strangeness in mdev->write_ordering %d\n", mdev->write_ordering);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001241 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001242 }
1243
1244 epoch->flags = 0;
1245 atomic_set(&epoch->epoch_size, 0);
1246 atomic_set(&epoch->active, 0);
1247
1248 spin_lock(&mdev->epoch_lock);
1249 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1250 list_add(&epoch->list, &mdev->current_epoch->list);
1251 mdev->current_epoch = epoch;
1252 mdev->epochs++;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001253 } else {
1254 /* The current_epoch got recycled while we allocated this one... */
1255 kfree(epoch);
1256 }
1257 spin_unlock(&mdev->epoch_lock);
1258
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001259 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001260}
1261
1262/* used from receive_RSDataReply (recv_resync_read)
1263 * and from receive_Data */
1264static struct drbd_epoch_entry *
1265read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector, int data_size) __must_hold(local)
1266{
Lars Ellenberg66660322010-04-06 12:15:04 +02001267 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001268 struct drbd_epoch_entry *e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001269 struct page *page;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001270 int dgs, ds, rr;
Philipp Reisnera0638452011-01-19 14:31:32 +01001271 void *dig_in = mdev->tconn->int_dig_in;
1272 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001273 unsigned long *data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001274
Philipp Reisnera0638452011-01-19 14:31:32 +01001275 dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_r_tfm) ?
1276 crypto_hash_digestsize(mdev->tconn->integrity_r_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001277
1278 if (dgs) {
1279 rr = drbd_recv(mdev, dig_in, dgs);
1280 if (rr != dgs) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01001281 if (!signal_pending(current))
1282 dev_warn(DEV,
1283 "short read receiving data digest: read %d expected %d\n",
1284 rr, dgs);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001285 return NULL;
1286 }
1287 }
1288
1289 data_size -= dgs;
1290
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001291 if (!expect(data_size != 0))
1292 return NULL;
1293 if (!expect(IS_ALIGNED(data_size, 512)))
1294 return NULL;
1295 if (!expect(data_size <= DRBD_MAX_BIO_SIZE))
1296 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001297
Lars Ellenberg66660322010-04-06 12:15:04 +02001298 /* even though we trust out peer,
1299 * we sometimes have to double check. */
1300 if (sector + (data_size>>9) > capacity) {
Lars Ellenbergfdda6542011-01-24 15:11:01 +01001301 dev_err(DEV, "request from peer beyond end of local disk: "
1302 "capacity: %llus < sector: %llus + size: %u\n",
Lars Ellenberg66660322010-04-06 12:15:04 +02001303 (unsigned long long)capacity,
1304 (unsigned long long)sector, data_size);
1305 return NULL;
1306 }
1307
Philipp Reisnerb411b362009-09-25 16:07:19 -07001308 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1309 * "criss-cross" setup, that might cause write-out on some other DRBD,
1310 * which in turn might block on the other node at this very place. */
1311 e = drbd_alloc_ee(mdev, id, sector, data_size, GFP_NOIO);
1312 if (!e)
1313 return NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001314
Philipp Reisnerb411b362009-09-25 16:07:19 -07001315 ds = data_size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001316 page = e->pages;
1317 page_chain_for_each(page) {
1318 unsigned len = min_t(int, ds, PAGE_SIZE);
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001319 data = kmap(page);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001320 rr = drbd_recv(mdev, data, len);
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +01001321 if (drbd_insert_fault(mdev, DRBD_FAULT_RECEIVE)) {
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001322 dev_err(DEV, "Fault injection: Corrupting data on receive\n");
1323 data[0] = data[0] ^ (unsigned long)-1;
1324 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001325 kunmap(page);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001326 if (rr != len) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001327 drbd_free_ee(mdev, e);
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01001328 if (!signal_pending(current))
1329 dev_warn(DEV, "short read receiving data: read %d expected %d\n",
1330 rr, len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001331 return NULL;
1332 }
1333 ds -= rr;
1334 }
1335
1336 if (dgs) {
Philipp Reisnera0638452011-01-19 14:31:32 +01001337 drbd_csum_ee(mdev, mdev->tconn->integrity_r_tfm, e, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001338 if (memcmp(dig_in, dig_vv, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001339 dev_err(DEV, "Digest integrity check FAILED: %llus +%u\n",
1340 (unsigned long long)sector, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001341 drbd_bcast_ee(mdev, "digest failed",
1342 dgs, dig_in, dig_vv, e);
1343 drbd_free_ee(mdev, e);
1344 return NULL;
1345 }
1346 }
1347 mdev->recv_cnt += data_size>>9;
1348 return e;
1349}
1350
1351/* drbd_drain_block() just takes a data block
1352 * out of the socket input buffer, and discards it.
1353 */
1354static int drbd_drain_block(struct drbd_conf *mdev, int data_size)
1355{
1356 struct page *page;
1357 int rr, rv = 1;
1358 void *data;
1359
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001360 if (!data_size)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001361 return true;
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001362
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001363 page = drbd_pp_alloc(mdev, 1, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001364
1365 data = kmap(page);
1366 while (data_size) {
1367 rr = drbd_recv(mdev, data, min_t(int, data_size, PAGE_SIZE));
1368 if (rr != min_t(int, data_size, PAGE_SIZE)) {
1369 rv = 0;
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01001370 if (!signal_pending(current))
1371 dev_warn(DEV,
1372 "short read receiving data: read %d expected %d\n",
1373 rr, min_t(int, data_size, PAGE_SIZE));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001374 break;
1375 }
1376 data_size -= rr;
1377 }
1378 kunmap(page);
Lars Ellenberg435f0742010-09-06 12:30:25 +02001379 drbd_pp_free(mdev, page, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001380 return rv;
1381}
1382
1383static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req,
1384 sector_t sector, int data_size)
1385{
1386 struct bio_vec *bvec;
1387 struct bio *bio;
1388 int dgs, rr, i, expect;
Philipp Reisnera0638452011-01-19 14:31:32 +01001389 void *dig_in = mdev->tconn->int_dig_in;
1390 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001391
Philipp Reisnera0638452011-01-19 14:31:32 +01001392 dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_r_tfm) ?
1393 crypto_hash_digestsize(mdev->tconn->integrity_r_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001394
1395 if (dgs) {
1396 rr = drbd_recv(mdev, dig_in, dgs);
1397 if (rr != dgs) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01001398 if (!signal_pending(current))
1399 dev_warn(DEV,
1400 "short read receiving data reply digest: read %d expected %d\n",
1401 rr, dgs);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001402 return 0;
1403 }
1404 }
1405
1406 data_size -= dgs;
1407
1408 /* optimistically update recv_cnt. if receiving fails below,
1409 * we disconnect anyways, and counters will be reset. */
1410 mdev->recv_cnt += data_size>>9;
1411
1412 bio = req->master_bio;
1413 D_ASSERT(sector == bio->bi_sector);
1414
1415 bio_for_each_segment(bvec, bio, i) {
1416 expect = min_t(int, data_size, bvec->bv_len);
1417 rr = drbd_recv(mdev,
1418 kmap(bvec->bv_page)+bvec->bv_offset,
1419 expect);
1420 kunmap(bvec->bv_page);
1421 if (rr != expect) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01001422 if (!signal_pending(current))
1423 dev_warn(DEV, "short read receiving data reply: "
1424 "read %d expected %d\n",
1425 rr, expect);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001426 return 0;
1427 }
1428 data_size -= rr;
1429 }
1430
1431 if (dgs) {
Philipp Reisnera0638452011-01-19 14:31:32 +01001432 drbd_csum_bio(mdev, mdev->tconn->integrity_r_tfm, bio, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001433 if (memcmp(dig_in, dig_vv, dgs)) {
1434 dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n");
1435 return 0;
1436 }
1437 }
1438
1439 D_ASSERT(data_size == 0);
1440 return 1;
1441}
1442
1443/* e_end_resync_block() is called via
1444 * drbd_process_done_ee() by asender only */
1445static int e_end_resync_block(struct drbd_conf *mdev, struct drbd_work *w, int unused)
1446{
1447 struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w;
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001448 sector_t sector = e->i.sector;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001449 int ok;
1450
Andreas Gruenbacher8b946252011-01-20 15:23:07 +01001451 D_ASSERT(drbd_interval_empty(&e->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001452
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001453 if (likely((e->flags & EE_WAS_ERROR) == 0)) {
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001454 drbd_set_in_sync(mdev, sector, e->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001455 ok = drbd_send_ack(mdev, P_RS_WRITE_ACK, e);
1456 } else {
1457 /* Record failure to sync */
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001458 drbd_rs_failed_io(mdev, sector, e->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001459
1460 ok = drbd_send_ack(mdev, P_NEG_ACK, e);
1461 }
1462 dec_unacked(mdev);
1463
1464 return ok;
1465}
1466
1467static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local)
1468{
1469 struct drbd_epoch_entry *e;
1470
1471 e = read_in_block(mdev, ID_SYNCER, sector, data_size);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001472 if (!e)
1473 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001474
1475 dec_rs_pending(mdev);
1476
Philipp Reisnerb411b362009-09-25 16:07:19 -07001477 inc_unacked(mdev);
1478 /* corresponding dec_unacked() in e_end_resync_block()
1479 * respective _drbd_clear_done_ee */
1480
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001481 e->w.cb = e_end_resync_block;
1482
Philipp Reisner87eeee42011-01-19 14:16:30 +01001483 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001484 list_add(&e->w.list, &mdev->sync_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001485 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001486
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001487 atomic_add(data_size >> 9, &mdev->rs_sect_ev);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001488 if (drbd_submit_ee(mdev, e, WRITE, DRBD_FAULT_RS_WR) == 0)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001489 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001490
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001491 /* don't care for the reason here */
1492 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01001493 spin_lock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001494 list_del(&e->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001495 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001496
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001497 drbd_free_ee(mdev, e);
1498fail:
1499 put_ldev(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001500 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001501}
1502
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001503static struct drbd_request *
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001504find_request(struct drbd_conf *mdev, struct rb_root *root, u64 id,
1505 sector_t sector, bool missing_ok, const char *func)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001506{
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001507 struct drbd_request *req;
1508
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001509 /* Request object according to our peer */
1510 req = (struct drbd_request *)(unsigned long)id;
1511 if (drbd_contains_interval(root, sector, &req->i))
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001512 return req;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001513 if (!missing_ok) {
1514 dev_err(DEV, "%s: failed to find request %lu, sector %llus\n", func,
1515 (unsigned long)id, (unsigned long long)sector);
1516 }
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001517 return NULL;
1518}
1519
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001520static int receive_DataReply(struct drbd_conf *mdev, enum drbd_packet cmd,
1521 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001522{
1523 struct drbd_request *req;
1524 sector_t sector;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001525 int ok;
Philipp Reisnere42325a2011-01-19 13:55:45 +01001526 struct p_data *p = &mdev->tconn->data.rbuf.data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001527
1528 sector = be64_to_cpu(p->sector);
1529
Philipp Reisner87eeee42011-01-19 14:16:30 +01001530 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001531 req = find_request(mdev, &mdev->read_requests, p->block_id, sector, false, __func__);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001532 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001533 if (unlikely(!req))
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001534 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001535
Bart Van Assche24c48302011-05-21 18:32:29 +02001536 /* hlist_del(&req->collision) is done in _req_may_be_done, to avoid
Philipp Reisnerb411b362009-09-25 16:07:19 -07001537 * special casing it there for the various failure cases.
1538 * still no race with drbd_fail_pending_reads */
1539 ok = recv_dless_read(mdev, req, sector, data_size);
1540
1541 if (ok)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001542 req_mod(req, DATA_RECEIVED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001543 /* else: nothing. handled from drbd_disconnect...
1544 * I don't think we may complete this just yet
1545 * in case we are "on-disconnect: freeze" */
1546
1547 return ok;
1548}
1549
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001550static int receive_RSDataReply(struct drbd_conf *mdev, enum drbd_packet cmd,
1551 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001552{
1553 sector_t sector;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001554 int ok;
Philipp Reisnere42325a2011-01-19 13:55:45 +01001555 struct p_data *p = &mdev->tconn->data.rbuf.data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001556
1557 sector = be64_to_cpu(p->sector);
1558 D_ASSERT(p->block_id == ID_SYNCER);
1559
1560 if (get_ldev(mdev)) {
1561 /* data is submitted to disk within recv_resync_read.
1562 * corresponding put_ldev done below on error,
Andreas Gruenbacher9c508422011-01-14 21:19:36 +01001563 * or in drbd_endio_sec. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001564 ok = recv_resync_read(mdev, sector, data_size);
1565 } else {
1566 if (__ratelimit(&drbd_ratelimit_state))
1567 dev_err(DEV, "Can not write resync data to local disk.\n");
1568
1569 ok = drbd_drain_block(mdev, data_size);
1570
Lars Ellenberg2b2bf212010-10-06 11:46:55 +02001571 drbd_send_ack_dp(mdev, P_NEG_ACK, p, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001572 }
1573
Philipp Reisner778f2712010-07-06 11:14:00 +02001574 atomic_add(data_size >> 9, &mdev->rs_sect_in);
1575
Philipp Reisnerb411b362009-09-25 16:07:19 -07001576 return ok;
1577}
1578
1579/* e_end_block() is called via drbd_process_done_ee().
1580 * this means this function only runs in the asender thread
1581 */
1582static int e_end_block(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1583{
1584 struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w;
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001585 sector_t sector = e->i.sector;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001586 int ok = 1, pcmd;
1587
Philipp Reisner89e58e72011-01-19 13:12:45 +01001588 if (mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001589 if (likely((e->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001590 pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
1591 mdev->state.conn <= C_PAUSED_SYNC_T &&
1592 e->flags & EE_MAY_SET_IN_SYNC) ?
1593 P_RS_WRITE_ACK : P_WRITE_ACK;
1594 ok &= drbd_send_ack(mdev, pcmd, e);
1595 if (pcmd == P_RS_WRITE_ACK)
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001596 drbd_set_in_sync(mdev, sector, e->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001597 } else {
1598 ok = drbd_send_ack(mdev, P_NEG_ACK, e);
1599 /* we expect it to be marked out of sync anyways...
1600 * maybe assert this? */
1601 }
1602 dec_unacked(mdev);
1603 }
1604 /* we delete from the conflict detection hash _after_ we sent out the
1605 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
Philipp Reisner89e58e72011-01-19 13:12:45 +01001606 if (mdev->tconn->net_conf->two_primaries) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01001607 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher8b946252011-01-20 15:23:07 +01001608 D_ASSERT(!drbd_interval_empty(&e->i));
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001609 drbd_remove_epoch_entry_interval(mdev, e);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001610 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +01001611 } else
Andreas Gruenbacher8b946252011-01-20 15:23:07 +01001612 D_ASSERT(drbd_interval_empty(&e->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001613
1614 drbd_may_finish_epoch(mdev, e->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
1615
1616 return ok;
1617}
1618
1619static int e_send_discard_ack(struct drbd_conf *mdev, struct drbd_work *w, int unused)
1620{
1621 struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w;
1622 int ok = 1;
1623
Philipp Reisner89e58e72011-01-19 13:12:45 +01001624 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001625 ok = drbd_send_ack(mdev, P_DISCARD_ACK, e);
1626
Philipp Reisner87eeee42011-01-19 14:16:30 +01001627 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher8b946252011-01-20 15:23:07 +01001628 D_ASSERT(!drbd_interval_empty(&e->i));
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001629 drbd_remove_epoch_entry_interval(mdev, e);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001630 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001631
1632 dec_unacked(mdev);
1633
1634 return ok;
1635}
1636
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001637static bool seq_greater(u32 a, u32 b)
1638{
1639 /*
1640 * We assume 32-bit wrap-around here.
1641 * For 24-bit wrap-around, we would have to shift:
1642 * a <<= 8; b <<= 8;
1643 */
1644 return (s32)a - (s32)b > 0;
1645}
1646
1647static u32 seq_max(u32 a, u32 b)
1648{
1649 return seq_greater(a, b) ? a : b;
1650}
1651
1652static void update_peer_seq(struct drbd_conf *mdev, unsigned int new_seq)
1653{
1654 unsigned int m;
1655
1656 spin_lock(&mdev->peer_seq_lock);
1657 m = seq_max(mdev->peer_seq, new_seq);
1658 mdev->peer_seq = m;
1659 spin_unlock(&mdev->peer_seq_lock);
1660 if (m == new_seq)
1661 wake_up(&mdev->seq_wait);
1662}
1663
Philipp Reisnerb411b362009-09-25 16:07:19 -07001664/* Called from receive_Data.
1665 * Synchronize packets on sock with packets on msock.
1666 *
1667 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
1668 * packet traveling on msock, they are still processed in the order they have
1669 * been sent.
1670 *
1671 * Note: we don't care for Ack packets overtaking P_DATA packets.
1672 *
1673 * In case packet_seq is larger than mdev->peer_seq number, there are
1674 * outstanding packets on the msock. We wait for them to arrive.
1675 * In case we are the logically next packet, we update mdev->peer_seq
1676 * ourselves. Correctly handles 32bit wrap around.
1677 *
1678 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
1679 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
1680 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
1681 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
1682 *
1683 * returns 0 if we may process the packet,
1684 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
1685static int drbd_wait_peer_seq(struct drbd_conf *mdev, const u32 packet_seq)
1686{
1687 DEFINE_WAIT(wait);
1688 unsigned int p_seq;
1689 long timeout;
1690 int ret = 0;
1691 spin_lock(&mdev->peer_seq_lock);
1692 for (;;) {
1693 prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001694 if (!seq_greater(packet_seq, mdev->peer_seq + 1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001695 break;
1696 if (signal_pending(current)) {
1697 ret = -ERESTARTSYS;
1698 break;
1699 }
1700 p_seq = mdev->peer_seq;
1701 spin_unlock(&mdev->peer_seq_lock);
1702 timeout = schedule_timeout(30*HZ);
1703 spin_lock(&mdev->peer_seq_lock);
1704 if (timeout == 0 && p_seq == mdev->peer_seq) {
1705 ret = -ETIMEDOUT;
1706 dev_err(DEV, "ASSERT FAILED waited 30 seconds for sequence update, forcing reconnect\n");
1707 break;
1708 }
1709 }
1710 finish_wait(&mdev->seq_wait, &wait);
1711 if (mdev->peer_seq+1 == packet_seq)
1712 mdev->peer_seq++;
1713 spin_unlock(&mdev->peer_seq_lock);
1714 return ret;
1715}
1716
Lars Ellenberg688593c2010-11-17 22:25:03 +01001717/* see also bio_flags_to_wire()
1718 * DRBD_REQ_*, because we need to semantically map the flags to data packet
1719 * flags and back. We may replicate to other kernel versions. */
1720static unsigned long wire_flags_to_bio(struct drbd_conf *mdev, u32 dpf)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001721{
Lars Ellenberg688593c2010-11-17 22:25:03 +01001722 return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
1723 (dpf & DP_FUA ? REQ_FUA : 0) |
1724 (dpf & DP_FLUSH ? REQ_FLUSH : 0) |
1725 (dpf & DP_DISCARD ? REQ_DISCARD : 0);
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001726}
1727
Philipp Reisnerb411b362009-09-25 16:07:19 -07001728/* mirrored write */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001729static int receive_Data(struct drbd_conf *mdev, enum drbd_packet cmd,
1730 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001731{
1732 sector_t sector;
1733 struct drbd_epoch_entry *e;
Philipp Reisnere42325a2011-01-19 13:55:45 +01001734 struct p_data *p = &mdev->tconn->data.rbuf.data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001735 int rw = WRITE;
1736 u32 dp_flags;
1737
Philipp Reisnerb411b362009-09-25 16:07:19 -07001738 if (!get_ldev(mdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001739 spin_lock(&mdev->peer_seq_lock);
1740 if (mdev->peer_seq+1 == be32_to_cpu(p->seq_num))
1741 mdev->peer_seq++;
1742 spin_unlock(&mdev->peer_seq_lock);
1743
Lars Ellenberg2b2bf212010-10-06 11:46:55 +02001744 drbd_send_ack_dp(mdev, P_NEG_ACK, p, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001745 atomic_inc(&mdev->current_epoch->epoch_size);
1746 return drbd_drain_block(mdev, data_size);
1747 }
1748
1749 /* get_ldev(mdev) successful.
1750 * Corresponding put_ldev done either below (on various errors),
Andreas Gruenbacher9c508422011-01-14 21:19:36 +01001751 * or in drbd_endio_sec, if we successfully submit the data at
Philipp Reisnerb411b362009-09-25 16:07:19 -07001752 * the end of this function. */
1753
1754 sector = be64_to_cpu(p->sector);
1755 e = read_in_block(mdev, p->block_id, sector, data_size);
1756 if (!e) {
1757 put_ldev(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001758 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001759 }
1760
Philipp Reisnerb411b362009-09-25 16:07:19 -07001761 e->w.cb = e_end_block;
1762
Lars Ellenberg688593c2010-11-17 22:25:03 +01001763 dp_flags = be32_to_cpu(p->dp_flags);
1764 rw |= wire_flags_to_bio(mdev, dp_flags);
1765
1766 if (dp_flags & DP_MAY_SET_IN_SYNC)
1767 e->flags |= EE_MAY_SET_IN_SYNC;
1768
Philipp Reisnerb411b362009-09-25 16:07:19 -07001769 spin_lock(&mdev->epoch_lock);
1770 e->epoch = mdev->current_epoch;
1771 atomic_inc(&e->epoch->epoch_size);
1772 atomic_inc(&e->epoch->active);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001773 spin_unlock(&mdev->epoch_lock);
1774
Philipp Reisnerb411b362009-09-25 16:07:19 -07001775 /* I'm the receiver, I do hold a net_cnt reference. */
Philipp Reisner89e58e72011-01-19 13:12:45 +01001776 if (!mdev->tconn->net_conf->two_primaries) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01001777 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001778 } else {
1779 /* don't get the req_lock yet,
1780 * we may sleep in drbd_wait_peer_seq */
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001781 const int size = e->i.size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001782 const int discard = test_bit(DISCARD_CONCURRENT, &mdev->flags);
1783 DEFINE_WAIT(wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001784 int first;
1785
Philipp Reisner89e58e72011-01-19 13:12:45 +01001786 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001787
1788 /* conflict detection and handling:
1789 * 1. wait on the sequence number,
1790 * in case this data packet overtook ACK packets.
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +01001791 * 2. check our interval trees for conflicting requests:
1792 * we only need to check the write_requests tree; the
1793 * epoch_entries tree cannot contain any overlaps because
1794 * they were already eliminated on the submitting node.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001795 *
1796 * Note: for two_primaries, we are protocol C,
1797 * so there cannot be any request that is DONE
1798 * but still on the transfer log.
1799 *
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +01001800 * unconditionally add to the epoch_entries tree.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001801 *
1802 * if no conflicting request is found:
1803 * submit.
1804 *
1805 * if any conflicting request is found
1806 * that has not yet been acked,
1807 * AND I have the "discard concurrent writes" flag:
1808 * queue (via done_ee) the P_DISCARD_ACK; OUT.
1809 *
1810 * if any conflicting request is found:
1811 * block the receiver, waiting on misc_wait
1812 * until no more conflicting requests are there,
1813 * or we get interrupted (disconnect).
1814 *
1815 * we do not just write after local io completion of those
1816 * requests, but only after req is done completely, i.e.
1817 * we wait for the P_DISCARD_ACK to arrive!
1818 *
1819 * then proceed normally, i.e. submit.
1820 */
1821 if (drbd_wait_peer_seq(mdev, be32_to_cpu(p->seq_num)))
1822 goto out_interrupted;
1823
Philipp Reisner87eeee42011-01-19 14:16:30 +01001824 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001825
Andreas Gruenbacher8b946252011-01-20 15:23:07 +01001826 drbd_insert_interval(&mdev->epoch_entries, &e->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001827
Philipp Reisnerb411b362009-09-25 16:07:19 -07001828 first = 1;
1829 for (;;) {
Andreas Gruenbacherde696712011-01-20 15:00:24 +01001830 struct drbd_interval *i;
Andreas Gruenbachera500c2e2011-01-27 14:12:23 +01001831 struct drbd_request *req2;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001832 int have_unacked = 0;
1833 int have_conflict = 0;
1834 prepare_to_wait(&mdev->misc_wait, &wait,
1835 TASK_INTERRUPTIBLE);
Andreas Gruenbacherde696712011-01-20 15:00:24 +01001836
1837 i = drbd_find_overlap(&mdev->write_requests, sector, size);
1838 if (i) {
Andreas Gruenbachera500c2e2011-01-27 14:12:23 +01001839 req2 = container_of(i, struct drbd_request, i);
Andreas Gruenbacherde696712011-01-20 15:00:24 +01001840
1841 /* only ALERT on first iteration,
1842 * we may be woken up early... */
1843 if (first)
1844 dev_alert(DEV, "%s[%u] Concurrent local write detected!"
1845 " new: %llus +%u; pending: %llus +%u\n",
1846 current->comm, current->pid,
1847 (unsigned long long)sector, size,
1848 (unsigned long long)req2->i.sector, req2->i.size);
1849 if (req2->rq_state & RQ_NET_PENDING)
1850 ++have_unacked;
1851 ++have_conflict;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001852 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001853 if (!have_conflict)
1854 break;
1855
1856 /* Discard Ack only for the _first_ iteration */
1857 if (first && discard && have_unacked) {
1858 dev_alert(DEV, "Concurrent write! [DISCARD BY FLAG] sec=%llus\n",
1859 (unsigned long long)sector);
1860 inc_unacked(mdev);
1861 e->w.cb = e_send_discard_ack;
1862 list_add_tail(&e->w.list, &mdev->done_ee);
1863
Philipp Reisner87eeee42011-01-19 14:16:30 +01001864 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001865
1866 /* we could probably send that P_DISCARD_ACK ourselves,
1867 * but I don't like the receiver using the msock */
1868
1869 put_ldev(mdev);
1870 wake_asender(mdev);
1871 finish_wait(&mdev->misc_wait, &wait);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001872 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001873 }
1874
1875 if (signal_pending(current)) {
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001876 drbd_remove_epoch_entry_interval(mdev, e);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001877 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001878 finish_wait(&mdev->misc_wait, &wait);
1879 goto out_interrupted;
1880 }
1881
Andreas Gruenbachera500c2e2011-01-27 14:12:23 +01001882 /* Indicate to wake up mdev->misc_wait upon completion. */
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001883 i->waiting = true;
Andreas Gruenbachera500c2e2011-01-27 14:12:23 +01001884
Philipp Reisner87eeee42011-01-19 14:16:30 +01001885 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001886 if (first) {
1887 first = 0;
1888 dev_alert(DEV, "Concurrent write! [W AFTERWARDS] "
1889 "sec=%llus\n", (unsigned long long)sector);
1890 } else if (discard) {
1891 /* we had none on the first iteration.
1892 * there must be none now. */
1893 D_ASSERT(have_unacked == 0);
1894 }
1895 schedule();
Philipp Reisner87eeee42011-01-19 14:16:30 +01001896 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001897 }
1898 finish_wait(&mdev->misc_wait, &wait);
1899 }
1900
1901 list_add(&e->w.list, &mdev->active_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001902 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001903
Philipp Reisner89e58e72011-01-19 13:12:45 +01001904 switch (mdev->tconn->net_conf->wire_protocol) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001905 case DRBD_PROT_C:
1906 inc_unacked(mdev);
1907 /* corresponding dec_unacked() in e_end_block()
1908 * respective _drbd_clear_done_ee */
1909 break;
1910 case DRBD_PROT_B:
1911 /* I really don't like it that the receiver thread
1912 * sends on the msock, but anyways */
1913 drbd_send_ack(mdev, P_RECV_ACK, e);
1914 break;
1915 case DRBD_PROT_A:
1916 /* nothing to do */
1917 break;
1918 }
1919
Lars Ellenberg6719fb02010-10-18 23:04:07 +02001920 if (mdev->state.pdsk < D_INCONSISTENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001921 /* In case we have the only disk of the cluster, */
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001922 drbd_set_out_of_sync(mdev, e->i.sector, e->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001923 e->flags |= EE_CALL_AL_COMPLETE_IO;
Lars Ellenberg6719fb02010-10-18 23:04:07 +02001924 e->flags &= ~EE_MAY_SET_IN_SYNC;
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001925 drbd_al_begin_io(mdev, e->i.sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001926 }
1927
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001928 if (drbd_submit_ee(mdev, e, rw, DRBD_FAULT_DT_WR) == 0)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001929 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001930
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001931 /* don't care for the reason here */
1932 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01001933 spin_lock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001934 list_del(&e->w.list);
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001935 drbd_remove_epoch_entry_interval(mdev, e);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001936 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001937 if (e->flags & EE_CALL_AL_COMPLETE_IO)
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001938 drbd_al_complete_io(mdev, e->i.sector);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001939
Philipp Reisnerb411b362009-09-25 16:07:19 -07001940out_interrupted:
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001941 drbd_may_finish_epoch(mdev, e->epoch, EV_PUT + EV_CLEANUP);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001942 put_ldev(mdev);
1943 drbd_free_ee(mdev, e);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001944 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001945}
1946
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001947/* We may throttle resync, if the lower device seems to be busy,
1948 * and current sync rate is above c_min_rate.
1949 *
1950 * To decide whether or not the lower device is busy, we use a scheme similar
1951 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
1952 * (more than 64 sectors) of activity we cannot account for with our own resync
1953 * activity, it obviously is "busy".
1954 *
1955 * The current sync rate used here uses only the most recent two step marks,
1956 * to have a short time average so we can react faster.
1957 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01001958int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001959{
1960 struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
1961 unsigned long db, dt, dbdt;
Philipp Reisnere3555d82010-11-07 15:56:29 +01001962 struct lc_element *tmp;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001963 int curr_events;
1964 int throttle = 0;
1965
1966 /* feature disabled? */
1967 if (mdev->sync_conf.c_min_rate == 0)
1968 return 0;
1969
Philipp Reisnere3555d82010-11-07 15:56:29 +01001970 spin_lock_irq(&mdev->al_lock);
1971 tmp = lc_find(mdev->resync, BM_SECT_TO_EXT(sector));
1972 if (tmp) {
1973 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
1974 if (test_bit(BME_PRIORITY, &bm_ext->flags)) {
1975 spin_unlock_irq(&mdev->al_lock);
1976 return 0;
1977 }
1978 /* Do not slow down if app IO is already waiting for this extent */
1979 }
1980 spin_unlock_irq(&mdev->al_lock);
1981
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001982 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
1983 (int)part_stat_read(&disk->part0, sectors[1]) -
1984 atomic_read(&mdev->rs_sect_ev);
Philipp Reisnere3555d82010-11-07 15:56:29 +01001985
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001986 if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
1987 unsigned long rs_left;
1988 int i;
1989
1990 mdev->rs_last_events = curr_events;
1991
1992 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
1993 * approx. */
Lars Ellenberg2649f082010-11-05 10:05:47 +01001994 i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
1995
1996 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
1997 rs_left = mdev->ov_left;
1998 else
1999 rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002000
2001 dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
2002 if (!dt)
2003 dt++;
2004 db = mdev->rs_mark_left[i] - rs_left;
2005 dbdt = Bit2KB(db/dt);
2006
2007 if (dbdt > mdev->sync_conf.c_min_rate)
2008 throttle = 1;
2009 }
2010 return throttle;
2011}
2012
2013
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01002014static int receive_DataRequest(struct drbd_conf *mdev, enum drbd_packet cmd,
2015 unsigned int digest_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002016{
2017 sector_t sector;
2018 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
2019 struct drbd_epoch_entry *e;
2020 struct digest_info *di = NULL;
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002021 int size, verb;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002022 unsigned int fault_type;
Philipp Reisnere42325a2011-01-19 13:55:45 +01002023 struct p_block_req *p = &mdev->tconn->data.rbuf.block_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002024
2025 sector = be64_to_cpu(p->sector);
2026 size = be32_to_cpu(p->blksize);
2027
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01002028 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002029 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2030 (unsigned long long)sector, size);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002031 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002032 }
2033 if (sector + (size>>9) > capacity) {
2034 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2035 (unsigned long long)sector, size);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002036 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002037 }
2038
2039 if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002040 verb = 1;
2041 switch (cmd) {
2042 case P_DATA_REQUEST:
2043 drbd_send_ack_rp(mdev, P_NEG_DREPLY, p);
2044 break;
2045 case P_RS_DATA_REQUEST:
2046 case P_CSUM_RS_REQUEST:
2047 case P_OV_REQUEST:
2048 drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p);
2049 break;
2050 case P_OV_REPLY:
2051 verb = 0;
2052 dec_rs_pending(mdev);
2053 drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC);
2054 break;
2055 default:
2056 dev_err(DEV, "unexpected command (%s) in receive_DataRequest\n",
2057 cmdname(cmd));
2058 }
2059 if (verb && __ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002060 dev_err(DEV, "Can not satisfy peer's read request, "
2061 "no local data.\n");
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002062
Lars Ellenberga821cc42010-09-06 12:31:37 +02002063 /* drain possibly payload */
2064 return drbd_drain_block(mdev, digest_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002065 }
2066
2067 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
2068 * "criss-cross" setup, that might cause write-out on some other DRBD,
2069 * which in turn might block on the other node at this very place. */
2070 e = drbd_alloc_ee(mdev, p->block_id, sector, size, GFP_NOIO);
2071 if (!e) {
2072 put_ldev(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002073 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002074 }
2075
Philipp Reisner02918be2010-08-20 14:35:10 +02002076 switch (cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002077 case P_DATA_REQUEST:
2078 e->w.cb = w_e_end_data_req;
2079 fault_type = DRBD_FAULT_DT_RD;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002080 /* application IO, don't drbd_rs_begin_io */
2081 goto submit;
2082
Philipp Reisnerb411b362009-09-25 16:07:19 -07002083 case P_RS_DATA_REQUEST:
2084 e->w.cb = w_e_end_rsdata_req;
2085 fault_type = DRBD_FAULT_RS_RD;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002086 /* used in the sector offset progress display */
2087 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002088 break;
2089
2090 case P_OV_REPLY:
2091 case P_CSUM_RS_REQUEST:
2092 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002093 di = kmalloc(sizeof(*di) + digest_size, GFP_NOIO);
2094 if (!di)
2095 goto out_free_e;
2096
2097 di->digest_size = digest_size;
2098 di->digest = (((char *)di)+sizeof(struct digest_info));
2099
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +02002100 e->digest = di;
2101 e->flags |= EE_HAS_DIGEST;
2102
Philipp Reisnerb411b362009-09-25 16:07:19 -07002103 if (drbd_recv(mdev, di->digest, digest_size) != digest_size)
2104 goto out_free_e;
2105
Philipp Reisner02918be2010-08-20 14:35:10 +02002106 if (cmd == P_CSUM_RS_REQUEST) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002107 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002108 e->w.cb = w_e_end_csum_rs_req;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002109 /* used in the sector offset progress display */
2110 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisner02918be2010-08-20 14:35:10 +02002111 } else if (cmd == P_OV_REPLY) {
Lars Ellenberg2649f082010-11-05 10:05:47 +01002112 /* track progress, we may need to throttle */
2113 atomic_add(size >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002114 e->w.cb = w_e_end_ov_reply;
2115 dec_rs_pending(mdev);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002116 /* drbd_rs_begin_io done when we sent this request,
2117 * but accounting still needs to be done. */
2118 goto submit_for_resync;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002119 }
2120 break;
2121
2122 case P_OV_REQUEST:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002123 if (mdev->ov_start_sector == ~(sector_t)0 &&
Philipp Reisner31890f42011-01-19 14:12:51 +01002124 mdev->tconn->agreed_pro_version >= 90) {
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002125 unsigned long now = jiffies;
2126 int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002127 mdev->ov_start_sector = sector;
2128 mdev->ov_position = sector;
Lars Ellenberg30b743a2010-11-05 09:39:06 +01002129 mdev->ov_left = drbd_bm_bits(mdev) - BM_SECT_TO_BIT(sector);
2130 mdev->rs_total = mdev->ov_left;
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002131 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
2132 mdev->rs_mark_left[i] = mdev->ov_left;
2133 mdev->rs_mark_time[i] = now;
2134 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002135 dev_info(DEV, "Online Verify start sector: %llu\n",
2136 (unsigned long long)sector);
2137 }
2138 e->w.cb = w_e_end_ov_req;
2139 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002140 break;
2141
Philipp Reisnerb411b362009-09-25 16:07:19 -07002142 default:
2143 dev_err(DEV, "unexpected command (%s) in receive_DataRequest\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02002144 cmdname(cmd));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002145 fault_type = DRBD_FAULT_MAX;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002146 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002147 }
2148
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002149 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2150 * wrt the receiver, but it is not as straightforward as it may seem.
2151 * Various places in the resync start and stop logic assume resync
2152 * requests are processed in order, requeuing this on the worker thread
2153 * introduces a bunch of new code for synchronization between threads.
2154 *
2155 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2156 * "forever", throttling after drbd_rs_begin_io will lock that extent
2157 * for application writes for the same time. For now, just throttle
2158 * here, where the rest of the code expects the receiver to sleep for
2159 * a while, anyways.
2160 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002161
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002162 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2163 * this defers syncer requests for some time, before letting at least
2164 * on request through. The resync controller on the receiving side
2165 * will adapt to the incoming rate accordingly.
2166 *
2167 * We cannot throttle here if remote is Primary/SyncTarget:
2168 * we would also throttle its application reads.
2169 * In that case, throttling is done on the SyncTarget only.
2170 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002171 if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev, sector))
2172 schedule_timeout_uninterruptible(HZ/10);
2173 if (drbd_rs_begin_io(mdev, sector))
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002174 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002175
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002176submit_for_resync:
2177 atomic_add(size >> 9, &mdev->rs_sect_ev);
2178
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002179submit:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002180 inc_unacked(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002181 spin_lock_irq(&mdev->tconn->req_lock);
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002182 list_add_tail(&e->w.list, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002183 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002184
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002185 if (drbd_submit_ee(mdev, e, READ, fault_type) == 0)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002186 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002187
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002188 /* don't care for the reason here */
2189 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002190 spin_lock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002191 list_del(&e->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002192 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002193 /* no drbd_rs_complete_io(), we are dropping the connection anyways */
2194
Philipp Reisnerb411b362009-09-25 16:07:19 -07002195out_free_e:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002196 put_ldev(mdev);
2197 drbd_free_ee(mdev, e);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002198 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002199}
2200
2201static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
2202{
2203 int self, peer, rv = -100;
2204 unsigned long ch_self, ch_peer;
2205
2206 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2207 peer = mdev->p_uuid[UI_BITMAP] & 1;
2208
2209 ch_peer = mdev->p_uuid[UI_SIZE];
2210 ch_self = mdev->comm_bm_set;
2211
Philipp Reisner89e58e72011-01-19 13:12:45 +01002212 switch (mdev->tconn->net_conf->after_sb_0p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002213 case ASB_CONSENSUS:
2214 case ASB_DISCARD_SECONDARY:
2215 case ASB_CALL_HELPER:
2216 dev_err(DEV, "Configuration error.\n");
2217 break;
2218 case ASB_DISCONNECT:
2219 break;
2220 case ASB_DISCARD_YOUNGER_PRI:
2221 if (self == 0 && peer == 1) {
2222 rv = -1;
2223 break;
2224 }
2225 if (self == 1 && peer == 0) {
2226 rv = 1;
2227 break;
2228 }
2229 /* Else fall through to one of the other strategies... */
2230 case ASB_DISCARD_OLDER_PRI:
2231 if (self == 0 && peer == 1) {
2232 rv = 1;
2233 break;
2234 }
2235 if (self == 1 && peer == 0) {
2236 rv = -1;
2237 break;
2238 }
2239 /* Else fall through to one of the other strategies... */
Lars Ellenbergad19bf62009-10-14 09:36:49 +02002240 dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
Philipp Reisnerb411b362009-09-25 16:07:19 -07002241 "Using discard-least-changes instead\n");
2242 case ASB_DISCARD_ZERO_CHG:
2243 if (ch_peer == 0 && ch_self == 0) {
2244 rv = test_bit(DISCARD_CONCURRENT, &mdev->flags)
2245 ? -1 : 1;
2246 break;
2247 } else {
2248 if (ch_peer == 0) { rv = 1; break; }
2249 if (ch_self == 0) { rv = -1; break; }
2250 }
Philipp Reisner89e58e72011-01-19 13:12:45 +01002251 if (mdev->tconn->net_conf->after_sb_0p == ASB_DISCARD_ZERO_CHG)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002252 break;
2253 case ASB_DISCARD_LEAST_CHG:
2254 if (ch_self < ch_peer)
2255 rv = -1;
2256 else if (ch_self > ch_peer)
2257 rv = 1;
2258 else /* ( ch_self == ch_peer ) */
2259 /* Well, then use something else. */
2260 rv = test_bit(DISCARD_CONCURRENT, &mdev->flags)
2261 ? -1 : 1;
2262 break;
2263 case ASB_DISCARD_LOCAL:
2264 rv = -1;
2265 break;
2266 case ASB_DISCARD_REMOTE:
2267 rv = 1;
2268 }
2269
2270 return rv;
2271}
2272
2273static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
2274{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002275 int hg, rv = -100;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002276
Philipp Reisner89e58e72011-01-19 13:12:45 +01002277 switch (mdev->tconn->net_conf->after_sb_1p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002278 case ASB_DISCARD_YOUNGER_PRI:
2279 case ASB_DISCARD_OLDER_PRI:
2280 case ASB_DISCARD_LEAST_CHG:
2281 case ASB_DISCARD_LOCAL:
2282 case ASB_DISCARD_REMOTE:
2283 dev_err(DEV, "Configuration error.\n");
2284 break;
2285 case ASB_DISCONNECT:
2286 break;
2287 case ASB_CONSENSUS:
2288 hg = drbd_asb_recover_0p(mdev);
2289 if (hg == -1 && mdev->state.role == R_SECONDARY)
2290 rv = hg;
2291 if (hg == 1 && mdev->state.role == R_PRIMARY)
2292 rv = hg;
2293 break;
2294 case ASB_VIOLENTLY:
2295 rv = drbd_asb_recover_0p(mdev);
2296 break;
2297 case ASB_DISCARD_SECONDARY:
2298 return mdev->state.role == R_PRIMARY ? 1 : -1;
2299 case ASB_CALL_HELPER:
2300 hg = drbd_asb_recover_0p(mdev);
2301 if (hg == -1 && mdev->state.role == R_PRIMARY) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002302 enum drbd_state_rv rv2;
2303
2304 drbd_set_role(mdev, R_SECONDARY, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002305 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2306 * we might be here in C_WF_REPORT_PARAMS which is transient.
2307 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002308 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2309 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002310 drbd_khelper(mdev, "pri-lost-after-sb");
2311 } else {
2312 dev_warn(DEV, "Successfully gave up primary role.\n");
2313 rv = hg;
2314 }
2315 } else
2316 rv = hg;
2317 }
2318
2319 return rv;
2320}
2321
2322static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
2323{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002324 int hg, rv = -100;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002325
Philipp Reisner89e58e72011-01-19 13:12:45 +01002326 switch (mdev->tconn->net_conf->after_sb_2p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002327 case ASB_DISCARD_YOUNGER_PRI:
2328 case ASB_DISCARD_OLDER_PRI:
2329 case ASB_DISCARD_LEAST_CHG:
2330 case ASB_DISCARD_LOCAL:
2331 case ASB_DISCARD_REMOTE:
2332 case ASB_CONSENSUS:
2333 case ASB_DISCARD_SECONDARY:
2334 dev_err(DEV, "Configuration error.\n");
2335 break;
2336 case ASB_VIOLENTLY:
2337 rv = drbd_asb_recover_0p(mdev);
2338 break;
2339 case ASB_DISCONNECT:
2340 break;
2341 case ASB_CALL_HELPER:
2342 hg = drbd_asb_recover_0p(mdev);
2343 if (hg == -1) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002344 enum drbd_state_rv rv2;
2345
Philipp Reisnerb411b362009-09-25 16:07:19 -07002346 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2347 * we might be here in C_WF_REPORT_PARAMS which is transient.
2348 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002349 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2350 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002351 drbd_khelper(mdev, "pri-lost-after-sb");
2352 } else {
2353 dev_warn(DEV, "Successfully gave up primary role.\n");
2354 rv = hg;
2355 }
2356 } else
2357 rv = hg;
2358 }
2359
2360 return rv;
2361}
2362
2363static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
2364 u64 bits, u64 flags)
2365{
2366 if (!uuid) {
2367 dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
2368 return;
2369 }
2370 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
2371 text,
2372 (unsigned long long)uuid[UI_CURRENT],
2373 (unsigned long long)uuid[UI_BITMAP],
2374 (unsigned long long)uuid[UI_HISTORY_START],
2375 (unsigned long long)uuid[UI_HISTORY_END],
2376 (unsigned long long)bits,
2377 (unsigned long long)flags);
2378}
2379
2380/*
2381 100 after split brain try auto recover
2382 2 C_SYNC_SOURCE set BitMap
2383 1 C_SYNC_SOURCE use BitMap
2384 0 no Sync
2385 -1 C_SYNC_TARGET use BitMap
2386 -2 C_SYNC_TARGET set BitMap
2387 -100 after split brain, disconnect
2388-1000 unrelated data
Philipp Reisner4a23f262011-01-11 17:42:17 +01002389-1091 requires proto 91
2390-1096 requires proto 96
Philipp Reisnerb411b362009-09-25 16:07:19 -07002391 */
2392static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
2393{
2394 u64 self, peer;
2395 int i, j;
2396
2397 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2398 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2399
2400 *rule_nr = 10;
2401 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
2402 return 0;
2403
2404 *rule_nr = 20;
2405 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
2406 peer != UUID_JUST_CREATED)
2407 return -2;
2408
2409 *rule_nr = 30;
2410 if (self != UUID_JUST_CREATED &&
2411 (peer == UUID_JUST_CREATED || peer == (u64)0))
2412 return 2;
2413
2414 if (self == peer) {
2415 int rct, dc; /* roles at crash time */
2416
2417 if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
2418
Philipp Reisner31890f42011-01-19 14:12:51 +01002419 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002420 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002421
2422 if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
2423 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
2424 dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
2425 drbd_uuid_set_bm(mdev, 0UL);
2426
2427 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2428 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2429 *rule_nr = 34;
2430 } else {
2431 dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
2432 *rule_nr = 36;
2433 }
2434
2435 return 1;
2436 }
2437
2438 if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
2439
Philipp Reisner31890f42011-01-19 14:12:51 +01002440 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002441 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002442
2443 if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
2444 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
2445 dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
2446
2447 mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
2448 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
2449 mdev->p_uuid[UI_BITMAP] = 0UL;
2450
2451 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2452 *rule_nr = 35;
2453 } else {
2454 dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
2455 *rule_nr = 37;
2456 }
2457
2458 return -1;
2459 }
2460
2461 /* Common power [off|failure] */
2462 rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
2463 (mdev->p_uuid[UI_FLAGS] & 2);
2464 /* lowest bit is set when we were primary,
2465 * next bit (weight 2) is set when peer was primary */
2466 *rule_nr = 40;
2467
2468 switch (rct) {
2469 case 0: /* !self_pri && !peer_pri */ return 0;
2470 case 1: /* self_pri && !peer_pri */ return 1;
2471 case 2: /* !self_pri && peer_pri */ return -1;
2472 case 3: /* self_pri && peer_pri */
2473 dc = test_bit(DISCARD_CONCURRENT, &mdev->flags);
2474 return dc ? -1 : 1;
2475 }
2476 }
2477
2478 *rule_nr = 50;
2479 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2480 if (self == peer)
2481 return -1;
2482
2483 *rule_nr = 51;
2484 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2485 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002486 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002487 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) ==
2488 (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) :
2489 peer + UUID_NEW_BM_OFFSET == (mdev->p_uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002490 /* The last P_SYNC_UUID did not get though. Undo the last start of
2491 resync as sync source modifications of the peer's UUIDs. */
2492
Philipp Reisner31890f42011-01-19 14:12:51 +01002493 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002494 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002495
2496 mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
2497 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
Philipp Reisner4a23f262011-01-11 17:42:17 +01002498
2499 dev_info(DEV, "Did not got last syncUUID packet, corrected:\n");
2500 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2501
Philipp Reisnerb411b362009-09-25 16:07:19 -07002502 return -1;
2503 }
2504 }
2505
2506 *rule_nr = 60;
2507 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2508 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2509 peer = mdev->p_uuid[i] & ~((u64)1);
2510 if (self == peer)
2511 return -2;
2512 }
2513
2514 *rule_nr = 70;
2515 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2516 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2517 if (self == peer)
2518 return 1;
2519
2520 *rule_nr = 71;
2521 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2522 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002523 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002524 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) ==
2525 (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) :
2526 self + UUID_NEW_BM_OFFSET == (mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002527 /* The last P_SYNC_UUID did not get though. Undo the last start of
2528 resync as sync source modifications of our UUIDs. */
2529
Philipp Reisner31890f42011-01-19 14:12:51 +01002530 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002531 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002532
2533 _drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
2534 _drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
2535
Philipp Reisner4a23f262011-01-11 17:42:17 +01002536 dev_info(DEV, "Last syncUUID did not get through, corrected:\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002537 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2538 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2539
2540 return 1;
2541 }
2542 }
2543
2544
2545 *rule_nr = 80;
Philipp Reisnerd8c2a362009-11-18 15:52:51 +01002546 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002547 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2548 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2549 if (self == peer)
2550 return 2;
2551 }
2552
2553 *rule_nr = 90;
2554 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2555 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2556 if (self == peer && self != ((u64)0))
2557 return 100;
2558
2559 *rule_nr = 100;
2560 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2561 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2562 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
2563 peer = mdev->p_uuid[j] & ~((u64)1);
2564 if (self == peer)
2565 return -100;
2566 }
2567 }
2568
2569 return -1000;
2570}
2571
2572/* drbd_sync_handshake() returns the new conn state on success, or
2573 CONN_MASK (-1) on failure.
2574 */
2575static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
2576 enum drbd_disk_state peer_disk) __must_hold(local)
2577{
2578 int hg, rule_nr;
2579 enum drbd_conns rv = C_MASK;
2580 enum drbd_disk_state mydisk;
2581
2582 mydisk = mdev->state.disk;
2583 if (mydisk == D_NEGOTIATING)
2584 mydisk = mdev->new_state_tmp.disk;
2585
2586 dev_info(DEV, "drbd_sync_handshake:\n");
2587 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0);
2588 drbd_uuid_dump(mdev, "peer", mdev->p_uuid,
2589 mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2590
2591 hg = drbd_uuid_compare(mdev, &rule_nr);
2592
2593 dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
2594
2595 if (hg == -1000) {
2596 dev_alert(DEV, "Unrelated data, aborting!\n");
2597 return C_MASK;
2598 }
Philipp Reisner4a23f262011-01-11 17:42:17 +01002599 if (hg < -1000) {
2600 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 -07002601 return C_MASK;
2602 }
2603
2604 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
2605 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
2606 int f = (hg == -100) || abs(hg) == 2;
2607 hg = mydisk > D_INCONSISTENT ? 1 : -1;
2608 if (f)
2609 hg = hg*2;
2610 dev_info(DEV, "Becoming sync %s due to disk states.\n",
2611 hg > 0 ? "source" : "target");
2612 }
2613
Adam Gandelman3a11a482010-04-08 16:48:23 -07002614 if (abs(hg) == 100)
2615 drbd_khelper(mdev, "initial-split-brain");
2616
Philipp Reisner89e58e72011-01-19 13:12:45 +01002617 if (hg == 100 || (hg == -100 && mdev->tconn->net_conf->always_asbp)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002618 int pcount = (mdev->state.role == R_PRIMARY)
2619 + (peer_role == R_PRIMARY);
2620 int forced = (hg == -100);
2621
2622 switch (pcount) {
2623 case 0:
2624 hg = drbd_asb_recover_0p(mdev);
2625 break;
2626 case 1:
2627 hg = drbd_asb_recover_1p(mdev);
2628 break;
2629 case 2:
2630 hg = drbd_asb_recover_2p(mdev);
2631 break;
2632 }
2633 if (abs(hg) < 100) {
2634 dev_warn(DEV, "Split-Brain detected, %d primaries, "
2635 "automatically solved. Sync from %s node\n",
2636 pcount, (hg < 0) ? "peer" : "this");
2637 if (forced) {
2638 dev_warn(DEV, "Doing a full sync, since"
2639 " UUIDs where ambiguous.\n");
2640 hg = hg*2;
2641 }
2642 }
2643 }
2644
2645 if (hg == -100) {
Philipp Reisner89e58e72011-01-19 13:12:45 +01002646 if (mdev->tconn->net_conf->want_lose && !(mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002647 hg = -1;
Philipp Reisner89e58e72011-01-19 13:12:45 +01002648 if (!mdev->tconn->net_conf->want_lose && (mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002649 hg = 1;
2650
2651 if (abs(hg) < 100)
2652 dev_warn(DEV, "Split-Brain detected, manually solved. "
2653 "Sync from %s node\n",
2654 (hg < 0) ? "peer" : "this");
2655 }
2656
2657 if (hg == -100) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01002658 /* FIXME this log message is not correct if we end up here
2659 * after an attempted attach on a diskless node.
2660 * We just refuse to attach -- well, we drop the "connection"
2661 * to that disk, in a way... */
Adam Gandelman3a11a482010-04-08 16:48:23 -07002662 dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002663 drbd_khelper(mdev, "split-brain");
2664 return C_MASK;
2665 }
2666
2667 if (hg > 0 && mydisk <= D_INCONSISTENT) {
2668 dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
2669 return C_MASK;
2670 }
2671
2672 if (hg < 0 && /* by intention we do not use mydisk here. */
2673 mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
Philipp Reisner89e58e72011-01-19 13:12:45 +01002674 switch (mdev->tconn->net_conf->rr_conflict) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002675 case ASB_CALL_HELPER:
2676 drbd_khelper(mdev, "pri-lost");
2677 /* fall through */
2678 case ASB_DISCONNECT:
2679 dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
2680 return C_MASK;
2681 case ASB_VIOLENTLY:
2682 dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
2683 "assumption\n");
2684 }
2685 }
2686
Philipp Reisner89e58e72011-01-19 13:12:45 +01002687 if (mdev->tconn->net_conf->dry_run || test_bit(CONN_DRY_RUN, &mdev->flags)) {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002688 if (hg == 0)
2689 dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
2690 else
2691 dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
2692 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
2693 abs(hg) >= 2 ? "full" : "bit-map based");
2694 return C_MASK;
2695 }
2696
Philipp Reisnerb411b362009-09-25 16:07:19 -07002697 if (abs(hg) >= 2) {
2698 dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002699 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake",
2700 BM_LOCKED_SET_ALLOWED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002701 return C_MASK;
2702 }
2703
2704 if (hg > 0) { /* become sync source. */
2705 rv = C_WF_BITMAP_S;
2706 } else if (hg < 0) { /* become sync target */
2707 rv = C_WF_BITMAP_T;
2708 } else {
2709 rv = C_CONNECTED;
2710 if (drbd_bm_total_weight(mdev)) {
2711 dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
2712 drbd_bm_total_weight(mdev));
2713 }
2714 }
2715
2716 return rv;
2717}
2718
2719/* returns 1 if invalid */
2720static int cmp_after_sb(enum drbd_after_sb_p peer, enum drbd_after_sb_p self)
2721{
2722 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
2723 if ((peer == ASB_DISCARD_REMOTE && self == ASB_DISCARD_LOCAL) ||
2724 (self == ASB_DISCARD_REMOTE && peer == ASB_DISCARD_LOCAL))
2725 return 0;
2726
2727 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
2728 if (peer == ASB_DISCARD_REMOTE || peer == ASB_DISCARD_LOCAL ||
2729 self == ASB_DISCARD_REMOTE || self == ASB_DISCARD_LOCAL)
2730 return 1;
2731
2732 /* everything else is valid if they are equal on both sides. */
2733 if (peer == self)
2734 return 0;
2735
2736 /* everything es is invalid. */
2737 return 1;
2738}
2739
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01002740static int receive_protocol(struct drbd_conf *mdev, enum drbd_packet cmd,
2741 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002742{
Philipp Reisnere42325a2011-01-19 13:55:45 +01002743 struct p_protocol *p = &mdev->tconn->data.rbuf.protocol;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002744 int p_proto, p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002745 int p_want_lose, p_two_primaries, cf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002746 char p_integrity_alg[SHARED_SECRET_MAX] = "";
2747
Philipp Reisnerb411b362009-09-25 16:07:19 -07002748 p_proto = be32_to_cpu(p->protocol);
2749 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
2750 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
2751 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002752 p_two_primaries = be32_to_cpu(p->two_primaries);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002753 cf = be32_to_cpu(p->conn_flags);
2754 p_want_lose = cf & CF_WANT_LOSE;
2755
2756 clear_bit(CONN_DRY_RUN, &mdev->flags);
2757
2758 if (cf & CF_DRY_RUN)
2759 set_bit(CONN_DRY_RUN, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002760
Philipp Reisner89e58e72011-01-19 13:12:45 +01002761 if (p_proto != mdev->tconn->net_conf->wire_protocol) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002762 dev_err(DEV, "incompatible communication protocols\n");
2763 goto disconnect;
2764 }
2765
Philipp Reisner89e58e72011-01-19 13:12:45 +01002766 if (cmp_after_sb(p_after_sb_0p, mdev->tconn->net_conf->after_sb_0p)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002767 dev_err(DEV, "incompatible after-sb-0pri settings\n");
2768 goto disconnect;
2769 }
2770
Philipp Reisner89e58e72011-01-19 13:12:45 +01002771 if (cmp_after_sb(p_after_sb_1p, mdev->tconn->net_conf->after_sb_1p)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002772 dev_err(DEV, "incompatible after-sb-1pri settings\n");
2773 goto disconnect;
2774 }
2775
Philipp Reisner89e58e72011-01-19 13:12:45 +01002776 if (cmp_after_sb(p_after_sb_2p, mdev->tconn->net_conf->after_sb_2p)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002777 dev_err(DEV, "incompatible after-sb-2pri settings\n");
2778 goto disconnect;
2779 }
2780
Philipp Reisner89e58e72011-01-19 13:12:45 +01002781 if (p_want_lose && mdev->tconn->net_conf->want_lose) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002782 dev_err(DEV, "both sides have the 'want_lose' flag set\n");
2783 goto disconnect;
2784 }
2785
Philipp Reisner89e58e72011-01-19 13:12:45 +01002786 if (p_two_primaries != mdev->tconn->net_conf->two_primaries) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002787 dev_err(DEV, "incompatible setting of the two-primaries options\n");
2788 goto disconnect;
2789 }
2790
Philipp Reisner31890f42011-01-19 14:12:51 +01002791 if (mdev->tconn->agreed_pro_version >= 87) {
Philipp Reisner89e58e72011-01-19 13:12:45 +01002792 unsigned char *my_alg = mdev->tconn->net_conf->integrity_alg;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002793
2794 if (drbd_recv(mdev, p_integrity_alg, data_size) != data_size)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002795 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002796
2797 p_integrity_alg[SHARED_SECRET_MAX-1] = 0;
2798 if (strcmp(p_integrity_alg, my_alg)) {
2799 dev_err(DEV, "incompatible setting of the data-integrity-alg\n");
2800 goto disconnect;
2801 }
2802 dev_info(DEV, "data-integrity-alg: %s\n",
2803 my_alg[0] ? my_alg : (unsigned char *)"<not-used>");
2804 }
2805
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002806 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002807
2808disconnect:
2809 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002810 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002811}
2812
2813/* helper function
2814 * input: alg name, feature name
2815 * return: NULL (alg name was "")
2816 * ERR_PTR(error) if something goes wrong
2817 * or the crypto hash ptr, if it worked out ok. */
2818struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
2819 const char *alg, const char *name)
2820{
2821 struct crypto_hash *tfm;
2822
2823 if (!alg[0])
2824 return NULL;
2825
2826 tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
2827 if (IS_ERR(tfm)) {
2828 dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
2829 alg, name, PTR_ERR(tfm));
2830 return tfm;
2831 }
2832 if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) {
2833 crypto_free_hash(tfm);
2834 dev_err(DEV, "\"%s\" is not a digest (%s)\n", alg, name);
2835 return ERR_PTR(-EINVAL);
2836 }
2837 return tfm;
2838}
2839
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01002840static int receive_SyncParam(struct drbd_conf *mdev, enum drbd_packet cmd,
2841 unsigned int packet_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002842{
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002843 int ok = true;
Philipp Reisnere42325a2011-01-19 13:55:45 +01002844 struct p_rs_param_95 *p = &mdev->tconn->data.rbuf.rs_param_95;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002845 unsigned int header_size, data_size, exp_max_sz;
2846 struct crypto_hash *verify_tfm = NULL;
2847 struct crypto_hash *csums_tfm = NULL;
Philipp Reisner31890f42011-01-19 14:12:51 +01002848 const int apv = mdev->tconn->agreed_pro_version;
Philipp Reisner778f2712010-07-06 11:14:00 +02002849 int *rs_plan_s = NULL;
2850 int fifo_size = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002851
2852 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
2853 : apv == 88 ? sizeof(struct p_rs_param)
2854 + SHARED_SECRET_MAX
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002855 : apv <= 94 ? sizeof(struct p_rs_param_89)
2856 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002857
Philipp Reisner02918be2010-08-20 14:35:10 +02002858 if (packet_size > exp_max_sz) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002859 dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02002860 packet_size, exp_max_sz);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002861 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002862 }
2863
2864 if (apv <= 88) {
Philipp Reisner257d0af2011-01-26 12:15:29 +01002865 header_size = sizeof(struct p_rs_param) - sizeof(struct p_header);
Philipp Reisner02918be2010-08-20 14:35:10 +02002866 data_size = packet_size - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002867 } else if (apv <= 94) {
Philipp Reisner257d0af2011-01-26 12:15:29 +01002868 header_size = sizeof(struct p_rs_param_89) - sizeof(struct p_header);
Philipp Reisner02918be2010-08-20 14:35:10 +02002869 data_size = packet_size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002870 D_ASSERT(data_size == 0);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002871 } else {
Philipp Reisner257d0af2011-01-26 12:15:29 +01002872 header_size = sizeof(struct p_rs_param_95) - sizeof(struct p_header);
Philipp Reisner02918be2010-08-20 14:35:10 +02002873 data_size = packet_size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002874 D_ASSERT(data_size == 0);
2875 }
2876
2877 /* initialize verify_alg and csums_alg */
2878 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
2879
Philipp Reisner02918be2010-08-20 14:35:10 +02002880 if (drbd_recv(mdev, &p->head.payload, header_size) != header_size)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002881 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002882
2883 mdev->sync_conf.rate = be32_to_cpu(p->rate);
2884
2885 if (apv >= 88) {
2886 if (apv == 88) {
2887 if (data_size > SHARED_SECRET_MAX) {
2888 dev_err(DEV, "verify-alg too long, "
2889 "peer wants %u, accepting only %u byte\n",
2890 data_size, SHARED_SECRET_MAX);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002891 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002892 }
2893
2894 if (drbd_recv(mdev, p->verify_alg, data_size) != data_size)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002895 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002896
2897 /* we expect NUL terminated string */
2898 /* but just in case someone tries to be evil */
2899 D_ASSERT(p->verify_alg[data_size-1] == 0);
2900 p->verify_alg[data_size-1] = 0;
2901
2902 } else /* apv >= 89 */ {
2903 /* we still expect NUL terminated strings */
2904 /* but just in case someone tries to be evil */
2905 D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
2906 D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
2907 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
2908 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
2909 }
2910
2911 if (strcmp(mdev->sync_conf.verify_alg, p->verify_alg)) {
2912 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
2913 dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
2914 mdev->sync_conf.verify_alg, p->verify_alg);
2915 goto disconnect;
2916 }
2917 verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
2918 p->verify_alg, "verify-alg");
2919 if (IS_ERR(verify_tfm)) {
2920 verify_tfm = NULL;
2921 goto disconnect;
2922 }
2923 }
2924
2925 if (apv >= 89 && strcmp(mdev->sync_conf.csums_alg, p->csums_alg)) {
2926 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
2927 dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
2928 mdev->sync_conf.csums_alg, p->csums_alg);
2929 goto disconnect;
2930 }
2931 csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
2932 p->csums_alg, "csums-alg");
2933 if (IS_ERR(csums_tfm)) {
2934 csums_tfm = NULL;
2935 goto disconnect;
2936 }
2937 }
2938
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002939 if (apv > 94) {
2940 mdev->sync_conf.rate = be32_to_cpu(p->rate);
2941 mdev->sync_conf.c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
2942 mdev->sync_conf.c_delay_target = be32_to_cpu(p->c_delay_target);
2943 mdev->sync_conf.c_fill_target = be32_to_cpu(p->c_fill_target);
2944 mdev->sync_conf.c_max_rate = be32_to_cpu(p->c_max_rate);
Philipp Reisner778f2712010-07-06 11:14:00 +02002945
2946 fifo_size = (mdev->sync_conf.c_plan_ahead * 10 * SLEEP_TIME) / HZ;
2947 if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) {
2948 rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL);
2949 if (!rs_plan_s) {
2950 dev_err(DEV, "kmalloc of fifo_buffer failed");
2951 goto disconnect;
2952 }
2953 }
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002954 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002955
2956 spin_lock(&mdev->peer_seq_lock);
2957 /* lock against drbd_nl_syncer_conf() */
2958 if (verify_tfm) {
2959 strcpy(mdev->sync_conf.verify_alg, p->verify_alg);
2960 mdev->sync_conf.verify_alg_len = strlen(p->verify_alg) + 1;
2961 crypto_free_hash(mdev->verify_tfm);
2962 mdev->verify_tfm = verify_tfm;
2963 dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
2964 }
2965 if (csums_tfm) {
2966 strcpy(mdev->sync_conf.csums_alg, p->csums_alg);
2967 mdev->sync_conf.csums_alg_len = strlen(p->csums_alg) + 1;
2968 crypto_free_hash(mdev->csums_tfm);
2969 mdev->csums_tfm = csums_tfm;
2970 dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
2971 }
Philipp Reisner778f2712010-07-06 11:14:00 +02002972 if (fifo_size != mdev->rs_plan_s.size) {
2973 kfree(mdev->rs_plan_s.values);
2974 mdev->rs_plan_s.values = rs_plan_s;
2975 mdev->rs_plan_s.size = fifo_size;
2976 mdev->rs_planed = 0;
2977 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002978 spin_unlock(&mdev->peer_seq_lock);
2979 }
2980
2981 return ok;
2982disconnect:
2983 /* just for completeness: actually not needed,
2984 * as this is not reached if csums_tfm was ok. */
2985 crypto_free_hash(csums_tfm);
2986 /* but free the verify_tfm again, if csums_tfm did not work out */
2987 crypto_free_hash(verify_tfm);
2988 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002989 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002990}
2991
Philipp Reisnerb411b362009-09-25 16:07:19 -07002992/* warn if the arguments differ by more than 12.5% */
2993static void warn_if_differ_considerably(struct drbd_conf *mdev,
2994 const char *s, sector_t a, sector_t b)
2995{
2996 sector_t d;
2997 if (a == 0 || b == 0)
2998 return;
2999 d = (a > b) ? (a - b) : (b - a);
3000 if (d > (a>>3) || d > (b>>3))
3001 dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
3002 (unsigned long long)a, (unsigned long long)b);
3003}
3004
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003005static int receive_sizes(struct drbd_conf *mdev, enum drbd_packet cmd,
3006 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003007{
Philipp Reisnere42325a2011-01-19 13:55:45 +01003008 struct p_sizes *p = &mdev->tconn->data.rbuf.sizes;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003009 enum determine_dev_size dd = unchanged;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003010 sector_t p_size, p_usize, my_usize;
3011 int ldsc = 0; /* local disk size changed */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003012 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003013
Philipp Reisnerb411b362009-09-25 16:07:19 -07003014 p_size = be64_to_cpu(p->d_size);
3015 p_usize = be64_to_cpu(p->u_size);
3016
3017 if (p_size == 0 && mdev->state.disk == D_DISKLESS) {
3018 dev_err(DEV, "some backing storage is needed\n");
3019 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003020 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003021 }
3022
3023 /* just store the peer's disk size for now.
3024 * we still need to figure out whether we accept that. */
3025 mdev->p_size = p_size;
3026
Philipp Reisnerb411b362009-09-25 16:07:19 -07003027 if (get_ldev(mdev)) {
3028 warn_if_differ_considerably(mdev, "lower level device sizes",
3029 p_size, drbd_get_max_capacity(mdev->ldev));
3030 warn_if_differ_considerably(mdev, "user requested size",
3031 p_usize, mdev->ldev->dc.disk_size);
3032
3033 /* if this is the first connect, or an otherwise expected
3034 * param exchange, choose the minimum */
3035 if (mdev->state.conn == C_WF_REPORT_PARAMS)
3036 p_usize = min_not_zero((sector_t)mdev->ldev->dc.disk_size,
3037 p_usize);
3038
3039 my_usize = mdev->ldev->dc.disk_size;
3040
3041 if (mdev->ldev->dc.disk_size != p_usize) {
3042 mdev->ldev->dc.disk_size = p_usize;
3043 dev_info(DEV, "Peer sets u_size to %lu sectors\n",
3044 (unsigned long)mdev->ldev->dc.disk_size);
3045 }
3046
3047 /* Never shrink a device with usable data during connect.
3048 But allow online shrinking if we are connected. */
Philipp Reisnera393db62009-12-22 13:35:52 +01003049 if (drbd_new_dev_size(mdev, mdev->ldev, 0) <
Philipp Reisnerb411b362009-09-25 16:07:19 -07003050 drbd_get_capacity(mdev->this_bdev) &&
3051 mdev->state.disk >= D_OUTDATED &&
3052 mdev->state.conn < C_CONNECTED) {
3053 dev_err(DEV, "The peer's disk size is too small!\n");
3054 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
3055 mdev->ldev->dc.disk_size = my_usize;
3056 put_ldev(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003057 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003058 }
3059 put_ldev(mdev);
3060 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003061
Philipp Reisnere89b5912010-03-24 17:11:33 +01003062 ddsf = be16_to_cpu(p->dds_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003063 if (get_ldev(mdev)) {
Bart Van Assche24c48302011-05-21 18:32:29 +02003064 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003065 put_ldev(mdev);
3066 if (dd == dev_size_error)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003067 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003068 drbd_md_sync(mdev);
3069 } else {
3070 /* I am diskless, need to accept the peer's size. */
3071 drbd_set_my_capacity(mdev, p_size);
3072 }
3073
Philipp Reisner99432fc2011-05-20 16:39:13 +02003074 mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size);
3075 drbd_reconsider_max_bio_size(mdev);
3076
Philipp Reisnerb411b362009-09-25 16:07:19 -07003077 if (get_ldev(mdev)) {
3078 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
3079 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
3080 ldsc = 1;
3081 }
3082
Philipp Reisnerb411b362009-09-25 16:07:19 -07003083 put_ldev(mdev);
3084 }
3085
3086 if (mdev->state.conn > C_WF_REPORT_PARAMS) {
3087 if (be64_to_cpu(p->c_size) !=
3088 drbd_get_capacity(mdev->this_bdev) || ldsc) {
3089 /* we have different sizes, probably peer
3090 * needs to know my new size... */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003091 drbd_send_sizes(mdev, 0, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003092 }
3093 if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
3094 (dd == grew && mdev->state.conn == C_CONNECTED)) {
3095 if (mdev->state.pdsk >= D_INCONSISTENT &&
Philipp Reisnere89b5912010-03-24 17:11:33 +01003096 mdev->state.disk >= D_INCONSISTENT) {
3097 if (ddsf & DDSF_NO_RESYNC)
3098 dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
3099 else
3100 resync_after_online_grow(mdev);
3101 } else
Philipp Reisnerb411b362009-09-25 16:07:19 -07003102 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
3103 }
3104 }
3105
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003106 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003107}
3108
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003109static int receive_uuids(struct drbd_conf *mdev, enum drbd_packet cmd,
3110 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003111{
Philipp Reisnere42325a2011-01-19 13:55:45 +01003112 struct p_uuids *p = &mdev->tconn->data.rbuf.uuids;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003113 u64 *p_uuid;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003114 int i, updated_uuids = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003115
Philipp Reisnerb411b362009-09-25 16:07:19 -07003116 p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
3117
3118 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
3119 p_uuid[i] = be64_to_cpu(p->uuid[i]);
3120
3121 kfree(mdev->p_uuid);
3122 mdev->p_uuid = p_uuid;
3123
3124 if (mdev->state.conn < C_CONNECTED &&
3125 mdev->state.disk < D_INCONSISTENT &&
3126 mdev->state.role == R_PRIMARY &&
3127 (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
3128 dev_err(DEV, "Can only connect to data with current UUID=%016llX\n",
3129 (unsigned long long)mdev->ed_uuid);
3130 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003131 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003132 }
3133
3134 if (get_ldev(mdev)) {
3135 int skip_initial_sync =
3136 mdev->state.conn == C_CONNECTED &&
Philipp Reisner31890f42011-01-19 14:12:51 +01003137 mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003138 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
3139 (p_uuid[UI_FLAGS] & 8);
3140 if (skip_initial_sync) {
3141 dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n");
3142 drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003143 "clear_n_write from receive_uuids",
3144 BM_LOCKED_TEST_ALLOWED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003145 _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]);
3146 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3147 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3148 CS_VERBOSE, NULL);
3149 drbd_md_sync(mdev);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003150 updated_uuids = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003151 }
3152 put_ldev(mdev);
Philipp Reisner18a50fa2010-06-21 14:14:15 +02003153 } else if (mdev->state.disk < D_INCONSISTENT &&
3154 mdev->state.role == R_PRIMARY) {
3155 /* I am a diskless primary, the peer just created a new current UUID
3156 for me. */
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003157 updated_uuids = drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003158 }
3159
3160 /* Before we test for the disk state, we should wait until an eventually
3161 ongoing cluster wide state change is finished. That is important if
3162 we are primary and are detaching from our disk. We need to see the
3163 new disk state... */
3164 wait_event(mdev->misc_wait, !test_bit(CLUSTER_ST_CHANGE, &mdev->flags));
3165 if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT)
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003166 updated_uuids |= drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
3167
3168 if (updated_uuids)
3169 drbd_print_uuids(mdev, "receiver updated UUIDs to");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003170
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003171 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003172}
3173
3174/**
3175 * convert_state() - Converts the peer's view of the cluster state to our point of view
3176 * @ps: The state as seen by the peer.
3177 */
3178static union drbd_state convert_state(union drbd_state ps)
3179{
3180 union drbd_state ms;
3181
3182 static enum drbd_conns c_tab[] = {
3183 [C_CONNECTED] = C_CONNECTED,
3184
3185 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
3186 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
3187 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
3188 [C_VERIFY_S] = C_VERIFY_T,
3189 [C_MASK] = C_MASK,
3190 };
3191
3192 ms.i = ps.i;
3193
3194 ms.conn = c_tab[ps.conn];
3195 ms.peer = ps.role;
3196 ms.role = ps.peer;
3197 ms.pdsk = ps.disk;
3198 ms.disk = ps.pdsk;
3199 ms.peer_isp = (ps.aftr_isp | ps.user_isp);
3200
3201 return ms;
3202}
3203
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003204static int receive_req_state(struct drbd_conf *mdev, enum drbd_packet cmd,
3205 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003206{
Philipp Reisnere42325a2011-01-19 13:55:45 +01003207 struct p_req_state *p = &mdev->tconn->data.rbuf.req_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003208 union drbd_state mask, val;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01003209 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003210
Philipp Reisnerb411b362009-09-25 16:07:19 -07003211 mask.i = be32_to_cpu(p->mask);
3212 val.i = be32_to_cpu(p->val);
3213
3214 if (test_bit(DISCARD_CONCURRENT, &mdev->flags) &&
3215 test_bit(CLUSTER_ST_CHANGE, &mdev->flags)) {
3216 drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003217 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003218 }
3219
3220 mask = convert_state(mask);
3221 val = convert_state(val);
3222
3223 rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
3224
3225 drbd_send_sr_reply(mdev, rv);
3226 drbd_md_sync(mdev);
3227
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003228 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003229}
3230
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003231static int receive_state(struct drbd_conf *mdev, enum drbd_packet cmd,
3232 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003233{
Philipp Reisnere42325a2011-01-19 13:55:45 +01003234 struct p_state *p = &mdev->tconn->data.rbuf.state;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003235 union drbd_state os, ns, peer_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003236 enum drbd_disk_state real_peer_disk;
Philipp Reisner65d922c2010-06-16 16:18:09 +02003237 enum chg_state_flags cs_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003238 int rv;
3239
Philipp Reisnerb411b362009-09-25 16:07:19 -07003240 peer_state.i = be32_to_cpu(p->state);
3241
3242 real_peer_disk = peer_state.disk;
3243 if (peer_state.disk == D_NEGOTIATING) {
3244 real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
3245 dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
3246 }
3247
Philipp Reisner87eeee42011-01-19 14:16:30 +01003248 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003249 retry:
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003250 os = ns = mdev->state;
Philipp Reisner87eeee42011-01-19 14:16:30 +01003251 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003252
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003253 /* peer says his disk is uptodate, while we think it is inconsistent,
3254 * and this happens while we think we have a sync going on. */
3255 if (os.pdsk == D_INCONSISTENT && real_peer_disk == D_UP_TO_DATE &&
3256 os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) {
3257 /* If we are (becoming) SyncSource, but peer is still in sync
3258 * preparation, ignore its uptodate-ness to avoid flapping, it
3259 * will change to inconsistent once the peer reaches active
3260 * syncing states.
3261 * It may have changed syncer-paused flags, however, so we
3262 * cannot ignore this completely. */
3263 if (peer_state.conn > C_CONNECTED &&
3264 peer_state.conn < C_SYNC_SOURCE)
3265 real_peer_disk = D_INCONSISTENT;
3266
3267 /* if peer_state changes to connected at the same time,
3268 * it explicitly notifies us that it finished resync.
3269 * Maybe we should finish it up, too? */
3270 else if (os.conn >= C_SYNC_SOURCE &&
3271 peer_state.conn == C_CONNECTED) {
3272 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed)
3273 drbd_resync_finished(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003274 return true;
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003275 }
3276 }
3277
3278 /* peer says his disk is inconsistent, while we think it is uptodate,
3279 * and this happens while the peer still thinks we have a sync going on,
3280 * but we think we are already done with the sync.
3281 * We ignore this to avoid flapping pdsk.
3282 * This should not happen, if the peer is a recent version of drbd. */
3283 if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT &&
3284 os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE)
3285 real_peer_disk = D_UP_TO_DATE;
3286
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003287 if (ns.conn == C_WF_REPORT_PARAMS)
3288 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003289
Philipp Reisner67531712010-10-27 12:21:30 +02003290 if (peer_state.conn == C_AHEAD)
3291 ns.conn = C_BEHIND;
3292
Philipp Reisnerb411b362009-09-25 16:07:19 -07003293 if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING &&
3294 get_ldev_if_state(mdev, D_NEGOTIATING)) {
3295 int cr; /* consider resync */
3296
3297 /* if we established a new connection */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003298 cr = (os.conn < C_CONNECTED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003299 /* if we had an established connection
3300 * and one of the nodes newly attaches a disk */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003301 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003302 (peer_state.disk == D_NEGOTIATING ||
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003303 os.disk == D_NEGOTIATING));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003304 /* if we have both been inconsistent, and the peer has been
3305 * forced to be UpToDate with --overwrite-data */
3306 cr |= test_bit(CONSIDER_RESYNC, &mdev->flags);
3307 /* if we had been plain connected, and the admin requested to
3308 * start a sync by "invalidate" or "invalidate-remote" */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003309 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003310 (peer_state.conn >= C_STARTING_SYNC_S &&
3311 peer_state.conn <= C_WF_BITMAP_T));
3312
3313 if (cr)
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003314 ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003315
3316 put_ldev(mdev);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003317 if (ns.conn == C_MASK) {
3318 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003319 if (mdev->state.disk == D_NEGOTIATING) {
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003320 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003321 } else if (peer_state.disk == D_NEGOTIATING) {
3322 dev_err(DEV, "Disk attach process on the peer node was aborted.\n");
3323 peer_state.disk = D_DISKLESS;
Lars Ellenberg580b9762010-02-26 23:15:23 +01003324 real_peer_disk = D_DISKLESS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003325 } else {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003326 if (test_and_clear_bit(CONN_DRY_RUN, &mdev->flags))
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003327 return false;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003328 D_ASSERT(os.conn == C_WF_REPORT_PARAMS);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003329 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003330 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003331 }
3332 }
3333 }
3334
Philipp Reisner87eeee42011-01-19 14:16:30 +01003335 spin_lock_irq(&mdev->tconn->req_lock);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003336 if (mdev->state.i != os.i)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003337 goto retry;
3338 clear_bit(CONSIDER_RESYNC, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003339 ns.peer = peer_state.role;
3340 ns.pdsk = real_peer_disk;
3341 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003342 if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003343 ns.disk = mdev->new_state_tmp.disk;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003344 cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD);
3345 if (ns.pdsk == D_CONSISTENT && is_susp(ns) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED &&
Philipp Reisner481c6f52010-06-22 14:03:27 +02003346 test_bit(NEW_CUR_UUID, &mdev->flags)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01003347 /* Do not allow tl_restart(RESEND) for a rebooted peer. We can only allow this
Philipp Reisner481c6f52010-06-22 14:03:27 +02003348 for temporal network outages! */
Philipp Reisner87eeee42011-01-19 14:16:30 +01003349 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003350 dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
3351 tl_clear(mdev);
3352 drbd_uuid_new_current(mdev);
3353 clear_bit(NEW_CUR_UUID, &mdev->flags);
3354 drbd_force_state(mdev, NS2(conn, C_PROTOCOL_ERROR, susp, 0));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003355 return false;
Philipp Reisner481c6f52010-06-22 14:03:27 +02003356 }
Philipp Reisner65d922c2010-06-16 16:18:09 +02003357 rv = _drbd_set_state(mdev, ns, cs_flags, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003358 ns = mdev->state;
Philipp Reisner87eeee42011-01-19 14:16:30 +01003359 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003360
3361 if (rv < SS_SUCCESS) {
3362 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003363 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003364 }
3365
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003366 if (os.conn > C_WF_REPORT_PARAMS) {
3367 if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003368 peer_state.disk != D_NEGOTIATING ) {
3369 /* we want resync, peer has not yet decided to sync... */
3370 /* Nowadays only used when forcing a node into primary role and
3371 setting its disk to UpToDate with that */
3372 drbd_send_uuids(mdev);
3373 drbd_send_state(mdev);
3374 }
3375 }
3376
Philipp Reisner89e58e72011-01-19 13:12:45 +01003377 mdev->tconn->net_conf->want_lose = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003378
3379 drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
3380
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003381 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003382}
3383
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003384static int receive_sync_uuid(struct drbd_conf *mdev, enum drbd_packet cmd,
3385 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003386{
Philipp Reisnere42325a2011-01-19 13:55:45 +01003387 struct p_rs_uuid *p = &mdev->tconn->data.rbuf.rs_uuid;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003388
3389 wait_event(mdev->misc_wait,
3390 mdev->state.conn == C_WF_SYNC_UUID ||
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02003391 mdev->state.conn == C_BEHIND ||
Philipp Reisnerb411b362009-09-25 16:07:19 -07003392 mdev->state.conn < C_CONNECTED ||
3393 mdev->state.disk < D_NEGOTIATING);
3394
3395 /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */
3396
Philipp Reisnerb411b362009-09-25 16:07:19 -07003397 /* Here the _drbd_uuid_ functions are right, current should
3398 _not_ be rotated into the history */
3399 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
3400 _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid));
3401 _drbd_uuid_set(mdev, UI_BITMAP, 0UL);
3402
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003403 drbd_print_uuids(mdev, "updated sync uuid");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003404 drbd_start_resync(mdev, C_SYNC_TARGET);
3405
3406 put_ldev(mdev);
3407 } else
3408 dev_err(DEV, "Ignoring SyncUUID packet!\n");
3409
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003410 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003411}
3412
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003413/**
3414 * receive_bitmap_plain
3415 *
3416 * Return 0 when done, 1 when another iteration is needed, and a negative error
3417 * code upon failure.
3418 */
3419static int
Philipp Reisner02918be2010-08-20 14:35:10 +02003420receive_bitmap_plain(struct drbd_conf *mdev, unsigned int data_size,
3421 unsigned long *buffer, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003422{
3423 unsigned num_words = min_t(size_t, BM_PACKET_WORDS, c->bm_words - c->word_offset);
3424 unsigned want = num_words * sizeof(long);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003425 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003426
Philipp Reisner02918be2010-08-20 14:35:10 +02003427 if (want != data_size) {
3428 dev_err(DEV, "%s:want (%u) != data_size (%u)\n", __func__, want, data_size);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003429 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003430 }
3431 if (want == 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003432 return 0;
3433 err = drbd_recv(mdev, buffer, want);
3434 if (err != want) {
3435 if (err >= 0)
3436 err = -EIO;
3437 return err;
3438 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003439
3440 drbd_bm_merge_lel(mdev, c->word_offset, num_words, buffer);
3441
3442 c->word_offset += num_words;
3443 c->bit_offset = c->word_offset * BITS_PER_LONG;
3444 if (c->bit_offset > c->bm_bits)
3445 c->bit_offset = c->bm_bits;
3446
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003447 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003448}
3449
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003450/**
3451 * recv_bm_rle_bits
3452 *
3453 * Return 0 when done, 1 when another iteration is needed, and a negative error
3454 * code upon failure.
3455 */
3456static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07003457recv_bm_rle_bits(struct drbd_conf *mdev,
3458 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003459 struct bm_xfer_ctx *c,
3460 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003461{
3462 struct bitstream bs;
3463 u64 look_ahead;
3464 u64 rl;
3465 u64 tmp;
3466 unsigned long s = c->bit_offset;
3467 unsigned long e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003468 int toggle = DCBP_get_start(p);
3469 int have;
3470 int bits;
3471
3472 bitstream_init(&bs, p->code, len, DCBP_get_pad_bits(p));
3473
3474 bits = bitstream_get_bits(&bs, &look_ahead, 64);
3475 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003476 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003477
3478 for (have = bits; have > 0; s += rl, toggle = !toggle) {
3479 bits = vli_decode_bits(&rl, look_ahead);
3480 if (bits <= 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003481 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003482
3483 if (toggle) {
3484 e = s + rl -1;
3485 if (e >= c->bm_bits) {
3486 dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003487 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003488 }
3489 _drbd_bm_set_bits(mdev, s, e);
3490 }
3491
3492 if (have < bits) {
3493 dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
3494 have, bits, look_ahead,
3495 (unsigned int)(bs.cur.b - p->code),
3496 (unsigned int)bs.buf_len);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003497 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003498 }
3499 look_ahead >>= bits;
3500 have -= bits;
3501
3502 bits = bitstream_get_bits(&bs, &tmp, 64 - have);
3503 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003504 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003505 look_ahead |= tmp << have;
3506 have += bits;
3507 }
3508
3509 c->bit_offset = s;
3510 bm_xfer_ctx_bit_to_word_offset(c);
3511
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003512 return (s != c->bm_bits);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003513}
3514
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003515/**
3516 * decode_bitmap_c
3517 *
3518 * Return 0 when done, 1 when another iteration is needed, and a negative error
3519 * code upon failure.
3520 */
3521static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07003522decode_bitmap_c(struct drbd_conf *mdev,
3523 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003524 struct bm_xfer_ctx *c,
3525 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003526{
3527 if (DCBP_get_code(p) == RLE_VLI_Bits)
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003528 return recv_bm_rle_bits(mdev, p, c, len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003529
3530 /* other variants had been implemented for evaluation,
3531 * but have been dropped as this one turned out to be "best"
3532 * during all our tests. */
3533
3534 dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
3535 drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR));
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003536 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003537}
3538
3539void INFO_bm_xfer_stats(struct drbd_conf *mdev,
3540 const char *direction, struct bm_xfer_ctx *c)
3541{
3542 /* what would it take to transfer it "plaintext" */
Philipp Reisnerc0129492011-01-19 16:58:16 +01003543 unsigned plain = sizeof(struct p_header) *
Philipp Reisnerb411b362009-09-25 16:07:19 -07003544 ((c->bm_words+BM_PACKET_WORDS-1)/BM_PACKET_WORDS+1)
3545 + c->bm_words * sizeof(long);
3546 unsigned total = c->bytes[0] + c->bytes[1];
3547 unsigned r;
3548
3549 /* total can not be zero. but just in case: */
3550 if (total == 0)
3551 return;
3552
3553 /* don't report if not compressed */
3554 if (total >= plain)
3555 return;
3556
3557 /* total < plain. check for overflow, still */
3558 r = (total > UINT_MAX/1000) ? (total / (plain/1000))
3559 : (1000 * total / plain);
3560
3561 if (r > 1000)
3562 r = 1000;
3563
3564 r = 1000 - r;
3565 dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
3566 "total %u; compression: %u.%u%%\n",
3567 direction,
3568 c->bytes[1], c->packets[1],
3569 c->bytes[0], c->packets[0],
3570 total, r/10, r % 10);
3571}
3572
3573/* Since we are processing the bitfield from lower addresses to higher,
3574 it does not matter if the process it in 32 bit chunks or 64 bit
3575 chunks as long as it is little endian. (Understand it as byte stream,
3576 beginning with the lowest byte...) If we would use big endian
3577 we would need to process it from the highest address to the lowest,
3578 in order to be agnostic to the 32 vs 64 bits issue.
3579
3580 returns 0 on failure, 1 if we successfully received it. */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003581static int receive_bitmap(struct drbd_conf *mdev, enum drbd_packet cmd,
3582 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003583{
3584 struct bm_xfer_ctx c;
3585 void *buffer;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003586 int err;
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003587 int ok = false;
Philipp Reisner257d0af2011-01-26 12:15:29 +01003588 struct p_header *h = &mdev->tconn->data.rbuf.header;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003589
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003590 drbd_bm_lock(mdev, "receive bitmap", BM_LOCKED_SET_ALLOWED);
3591 /* you are supposed to send additional out-of-sync information
3592 * if you actually set bits during this phase */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003593
3594 /* maybe we should use some per thread scratch page,
3595 * and allocate that during initial device creation? */
3596 buffer = (unsigned long *) __get_free_page(GFP_NOIO);
3597 if (!buffer) {
3598 dev_err(DEV, "failed to allocate one page buffer in %s\n", __func__);
3599 goto out;
3600 }
3601
3602 c = (struct bm_xfer_ctx) {
3603 .bm_bits = drbd_bm_bits(mdev),
3604 .bm_words = drbd_bm_words(mdev),
3605 };
3606
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003607 for(;;) {
Philipp Reisner02918be2010-08-20 14:35:10 +02003608 if (cmd == P_BITMAP) {
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003609 err = receive_bitmap_plain(mdev, data_size, buffer, &c);
Philipp Reisner02918be2010-08-20 14:35:10 +02003610 } else if (cmd == P_COMPRESSED_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003611 /* MAYBE: sanity check that we speak proto >= 90,
3612 * and the feature is enabled! */
3613 struct p_compressed_bm *p;
3614
Philipp Reisner02918be2010-08-20 14:35:10 +02003615 if (data_size > BM_PACKET_PAYLOAD_BYTES) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003616 dev_err(DEV, "ReportCBitmap packet too large\n");
3617 goto out;
3618 }
3619 /* use the page buff */
3620 p = buffer;
3621 memcpy(p, h, sizeof(*h));
Philipp Reisner02918be2010-08-20 14:35:10 +02003622 if (drbd_recv(mdev, p->head.payload, data_size) != data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003623 goto out;
Lars Ellenberg004352f2010-10-05 20:13:58 +02003624 if (data_size <= (sizeof(*p) - sizeof(p->head))) {
3625 dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", data_size);
Andreas Gruenbacher78fcbda2010-12-10 22:18:27 +01003626 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003627 }
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003628 err = decode_bitmap_c(mdev, p, &c, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003629 } else {
Philipp Reisner02918be2010-08-20 14:35:10 +02003630 dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003631 goto out;
3632 }
3633
Philipp Reisner02918be2010-08-20 14:35:10 +02003634 c.packets[cmd == P_BITMAP]++;
Philipp Reisner257d0af2011-01-26 12:15:29 +01003635 c.bytes[cmd == P_BITMAP] += sizeof(struct p_header) + data_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003636
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003637 if (err <= 0) {
3638 if (err < 0)
3639 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003640 break;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003641 }
Philipp Reisner02918be2010-08-20 14:35:10 +02003642 if (!drbd_recv_header(mdev, &cmd, &data_size))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003643 goto out;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003644 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003645
3646 INFO_bm_xfer_stats(mdev, "receive", &c);
3647
3648 if (mdev->state.conn == C_WF_BITMAP_T) {
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01003649 enum drbd_state_rv rv;
3650
Philipp Reisnerb411b362009-09-25 16:07:19 -07003651 ok = !drbd_send_bitmap(mdev);
3652 if (!ok)
3653 goto out;
3654 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01003655 rv = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
3656 D_ASSERT(rv == SS_SUCCESS);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003657 } else if (mdev->state.conn != C_WF_BITMAP_S) {
3658 /* admin may have requested C_DISCONNECTING,
3659 * other threads may have noticed network errors */
3660 dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n",
3661 drbd_conn_str(mdev->state.conn));
3662 }
3663
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003664 ok = true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003665 out:
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003666 drbd_bm_unlock(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003667 if (ok && mdev->state.conn == C_WF_BITMAP_S)
3668 drbd_start_resync(mdev, C_SYNC_SOURCE);
3669 free_page((unsigned long) buffer);
3670 return ok;
3671}
3672
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003673static int receive_skip(struct drbd_conf *mdev, enum drbd_packet cmd,
3674 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003675{
3676 /* TODO zero copy sink :) */
3677 static char sink[128];
3678 int size, want, r;
3679
Philipp Reisner02918be2010-08-20 14:35:10 +02003680 dev_warn(DEV, "skipping unknown optional packet type %d, l: %d!\n",
3681 cmd, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003682
Philipp Reisner02918be2010-08-20 14:35:10 +02003683 size = data_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003684 while (size > 0) {
3685 want = min_t(int, size, sizeof(sink));
3686 r = drbd_recv(mdev, sink, want);
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01003687 if (!expect(r > 0))
3688 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003689 size -= r;
3690 }
3691 return size == 0;
3692}
3693
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003694static int receive_UnplugRemote(struct drbd_conf *mdev, enum drbd_packet cmd,
3695 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003696{
Philipp Reisnerb411b362009-09-25 16:07:19 -07003697 /* Make sure we've acked all the TCP data associated
3698 * with the data requests being unplugged */
Philipp Reisnere42325a2011-01-19 13:55:45 +01003699 drbd_tcp_quickack(mdev->tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003700
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003701 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003702}
3703
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003704static int receive_out_of_sync(struct drbd_conf *mdev, enum drbd_packet cmd,
3705 unsigned int data_size)
Philipp Reisner73a01a12010-10-27 14:33:00 +02003706{
Philipp Reisnere42325a2011-01-19 13:55:45 +01003707 struct p_block_desc *p = &mdev->tconn->data.rbuf.block_desc;
Philipp Reisner73a01a12010-10-27 14:33:00 +02003708
Lars Ellenbergf735e3632010-12-17 21:06:18 +01003709 switch (mdev->state.conn) {
3710 case C_WF_SYNC_UUID:
3711 case C_WF_BITMAP_T:
3712 case C_BEHIND:
3713 break;
3714 default:
3715 dev_err(DEV, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n",
3716 drbd_conn_str(mdev->state.conn));
3717 }
3718
Philipp Reisner73a01a12010-10-27 14:33:00 +02003719 drbd_set_out_of_sync(mdev, be64_to_cpu(p->sector), be32_to_cpu(p->blksize));
3720
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003721 return true;
Philipp Reisner73a01a12010-10-27 14:33:00 +02003722}
3723
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003724typedef int (*drbd_cmd_handler_f)(struct drbd_conf *, enum drbd_packet cmd,
3725 unsigned int to_receive);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003726
Philipp Reisner02918be2010-08-20 14:35:10 +02003727struct data_cmd {
3728 int expect_payload;
3729 size_t pkt_size;
3730 drbd_cmd_handler_f function;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003731};
3732
Philipp Reisner02918be2010-08-20 14:35:10 +02003733static struct data_cmd drbd_cmd_handler[] = {
3734 [P_DATA] = { 1, sizeof(struct p_data), receive_Data },
3735 [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply },
3736 [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } ,
3737 [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } ,
Philipp Reisner257d0af2011-01-26 12:15:29 +01003738 [P_BITMAP] = { 1, sizeof(struct p_header), receive_bitmap } ,
3739 [P_COMPRESSED_BITMAP] = { 1, sizeof(struct p_header), receive_bitmap } ,
3740 [P_UNPLUG_REMOTE] = { 0, sizeof(struct p_header), receive_UnplugRemote },
Philipp Reisner02918be2010-08-20 14:35:10 +02003741 [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
3742 [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
Philipp Reisner257d0af2011-01-26 12:15:29 +01003743 [P_SYNC_PARAM] = { 1, sizeof(struct p_header), receive_SyncParam },
3744 [P_SYNC_PARAM89] = { 1, sizeof(struct p_header), receive_SyncParam },
Philipp Reisner02918be2010-08-20 14:35:10 +02003745 [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol },
3746 [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids },
3747 [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes },
3748 [P_STATE] = { 0, sizeof(struct p_state), receive_state },
3749 [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state },
3750 [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid },
3751 [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
3752 [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest },
3753 [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest },
3754 [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip },
Philipp Reisner73a01a12010-10-27 14:33:00 +02003755 [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync },
Philipp Reisner02918be2010-08-20 14:35:10 +02003756 /* anything missing from this table is in
3757 * the asender_tbl, see get_asender_cmd */
3758 [P_MAX_CMD] = { 0, 0, NULL },
3759};
3760
3761/* All handler functions that expect a sub-header get that sub-heder in
Philipp Reisnere42325a2011-01-19 13:55:45 +01003762 mdev->tconn->data.rbuf.header.head.payload.
Philipp Reisner02918be2010-08-20 14:35:10 +02003763
Philipp Reisnere42325a2011-01-19 13:55:45 +01003764 Usually in mdev->tconn->data.rbuf.header.head the callback can find the usual
Philipp Reisner02918be2010-08-20 14:35:10 +02003765 p_header, but they may not rely on that. Since there is also p_header95 !
3766 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003767
3768static void drbdd(struct drbd_conf *mdev)
3769{
Philipp Reisnerc0129492011-01-19 16:58:16 +01003770 struct p_header *header = &mdev->tconn->data.rbuf.header;
Philipp Reisner02918be2010-08-20 14:35:10 +02003771 unsigned int packet_size;
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003772 enum drbd_packet cmd;
Philipp Reisner02918be2010-08-20 14:35:10 +02003773 size_t shs; /* sub header size */
3774 int rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003775
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01003776 while (get_t_state(&mdev->tconn->receiver) == RUNNING) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003777 drbd_thread_current_set_cpu(mdev);
Philipp Reisner02918be2010-08-20 14:35:10 +02003778 if (!drbd_recv_header(mdev, &cmd, &packet_size))
3779 goto err_out;
3780
3781 if (unlikely(cmd >= P_MAX_CMD || !drbd_cmd_handler[cmd].function)) {
3782 dev_err(DEV, "unknown packet type %d, l: %d!\n", cmd, packet_size);
3783 goto err_out;
Lars Ellenberg0b33a912009-11-16 15:58:04 +01003784 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003785
Philipp Reisnerc0129492011-01-19 16:58:16 +01003786 shs = drbd_cmd_handler[cmd].pkt_size - sizeof(struct p_header);
Philipp Reisner02918be2010-08-20 14:35:10 +02003787 if (packet_size - shs > 0 && !drbd_cmd_handler[cmd].expect_payload) {
3788 dev_err(DEV, "No payload expected %s l:%d\n", cmdname(cmd), packet_size);
3789 goto err_out;
3790 }
3791
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02003792 if (shs) {
Philipp Reisnerc0129492011-01-19 16:58:16 +01003793 rv = drbd_recv(mdev, &header->payload, shs);
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02003794 if (unlikely(rv != shs)) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01003795 if (!signal_pending(current))
3796 dev_warn(DEV, "short read while reading sub header: rv=%d\n", rv);
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02003797 goto err_out;
3798 }
3799 }
3800
Philipp Reisner02918be2010-08-20 14:35:10 +02003801 rv = drbd_cmd_handler[cmd].function(mdev, cmd, packet_size - shs);
3802
3803 if (unlikely(!rv)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003804 dev_err(DEV, "error receiving %s, l: %d!\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02003805 cmdname(cmd), packet_size);
3806 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003807 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003808 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003809
Philipp Reisner02918be2010-08-20 14:35:10 +02003810 if (0) {
3811 err_out:
3812 drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003813 }
Lars Ellenberg856c50c2010-10-14 13:37:40 +02003814 /* If we leave here, we probably want to update at least the
3815 * "Connected" indicator on stable storage. Do so explicitly here. */
3816 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003817}
3818
Philipp Reisner191d3cc2011-01-19 14:53:22 +01003819void drbd_flush_workqueue(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003820{
3821 struct drbd_wq_barrier barr;
3822
3823 barr.w.cb = w_prev_work_done;
3824 init_completion(&barr.done);
Philipp Reisner191d3cc2011-01-19 14:53:22 +01003825 drbd_queue_work(&tconn->data.work, &barr.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003826 wait_for_completion(&barr.done);
3827}
3828
3829static void drbd_disconnect(struct drbd_conf *mdev)
3830{
3831 enum drbd_fencing_p fp;
3832 union drbd_state os, ns;
3833 int rv = SS_UNKNOWN_ERROR;
3834 unsigned int i;
3835
3836 if (mdev->state.conn == C_STANDALONE)
3837 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003838
3839 /* asender does not clean up anything. it must not interfere, either */
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01003840 drbd_thread_stop(&mdev->tconn->asender);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003841 drbd_free_sock(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003842
Philipp Reisner85719572010-07-21 10:20:17 +02003843 /* wait for current activity to cease. */
Philipp Reisner87eeee42011-01-19 14:16:30 +01003844 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003845 _drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
3846 _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee);
3847 _drbd_wait_ee_list_empty(mdev, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003848 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003849
3850 /* We do not have data structures that would allow us to
3851 * get the rs_pending_cnt down to 0 again.
3852 * * On C_SYNC_TARGET we do not have any data structures describing
3853 * the pending RSDataRequest's we have sent.
3854 * * On C_SYNC_SOURCE there is no data structure that tracks
3855 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
3856 * And no, it is not the sum of the reference counts in the
3857 * resync_LRU. The resync_LRU tracks the whole operation including
3858 * the disk-IO, while the rs_pending_cnt only tracks the blocks
3859 * on the fly. */
3860 drbd_rs_cancel_all(mdev);
3861 mdev->rs_total = 0;
3862 mdev->rs_failed = 0;
3863 atomic_set(&mdev->rs_pending_cnt, 0);
3864 wake_up(&mdev->misc_wait);
3865
Philipp Reisner7fde2be2011-03-01 11:08:28 +01003866 del_timer(&mdev->request_timer);
3867
Philipp Reisnerb411b362009-09-25 16:07:19 -07003868 /* make sure syncer is stopped and w_resume_next_sg queued */
3869 del_timer_sync(&mdev->resync_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003870 resync_timer_fn((unsigned long)mdev);
3871
Philipp Reisnerb411b362009-09-25 16:07:19 -07003872 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
3873 * w_make_resync_request etc. which may still be on the worker queue
3874 * to be "canceled" */
Philipp Reisner191d3cc2011-01-19 14:53:22 +01003875 drbd_flush_workqueue(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003876
3877 /* This also does reclaim_net_ee(). If we do this too early, we might
3878 * miss some resync ee and pages.*/
3879 drbd_process_done_ee(mdev);
3880
3881 kfree(mdev->p_uuid);
3882 mdev->p_uuid = NULL;
3883
Philipp Reisnerfb22c402010-09-08 23:20:21 +02003884 if (!is_susp(mdev->state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003885 tl_clear(mdev);
3886
Philipp Reisnerb411b362009-09-25 16:07:19 -07003887 dev_info(DEV, "Connection closed\n");
3888
3889 drbd_md_sync(mdev);
3890
3891 fp = FP_DONT_CARE;
3892 if (get_ldev(mdev)) {
3893 fp = mdev->ldev->dc.fencing;
3894 put_ldev(mdev);
3895 }
3896
Philipp Reisner87f7be42010-06-11 13:56:33 +02003897 if (mdev->state.role == R_PRIMARY && fp >= FP_RESOURCE && mdev->state.pdsk >= D_UNKNOWN)
3898 drbd_try_outdate_peer_async(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003899
Philipp Reisner87eeee42011-01-19 14:16:30 +01003900 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003901 os = mdev->state;
3902 if (os.conn >= C_UNCONNECTED) {
3903 /* Do not restart in case we are C_DISCONNECTING */
3904 ns = os;
3905 ns.conn = C_UNCONNECTED;
3906 rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
3907 }
Philipp Reisner87eeee42011-01-19 14:16:30 +01003908 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003909
3910 if (os.conn == C_DISCONNECTING) {
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01003911 wait_event(mdev->tconn->net_cnt_wait, atomic_read(&mdev->tconn->net_cnt) == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003912
Philipp Reisnera0638452011-01-19 14:31:32 +01003913 crypto_free_hash(mdev->tconn->cram_hmac_tfm);
3914 mdev->tconn->cram_hmac_tfm = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003915
Philipp Reisner89e58e72011-01-19 13:12:45 +01003916 kfree(mdev->tconn->net_conf);
3917 mdev->tconn->net_conf = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003918 drbd_request_state(mdev, NS(conn, C_STANDALONE));
3919 }
3920
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003921 /* serialize with bitmap writeout triggered by the state change,
3922 * if any. */
3923 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
3924
Philipp Reisnerb411b362009-09-25 16:07:19 -07003925 /* tcp_close and release of sendpage pages can be deferred. I don't
3926 * want to use SO_LINGER, because apparently it can be deferred for
3927 * more than 20 seconds (longest time I checked).
3928 *
3929 * Actually we don't care for exactly when the network stack does its
3930 * put_page(), but release our reference on these pages right here.
3931 */
3932 i = drbd_release_ee(mdev, &mdev->net_ee);
3933 if (i)
3934 dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
Lars Ellenberg435f0742010-09-06 12:30:25 +02003935 i = atomic_read(&mdev->pp_in_use_by_net);
3936 if (i)
3937 dev_info(DEV, "pp_in_use_by_net = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003938 i = atomic_read(&mdev->pp_in_use);
3939 if (i)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02003940 dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003941
3942 D_ASSERT(list_empty(&mdev->read_ee));
3943 D_ASSERT(list_empty(&mdev->active_ee));
3944 D_ASSERT(list_empty(&mdev->sync_ee));
3945 D_ASSERT(list_empty(&mdev->done_ee));
3946
3947 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
3948 atomic_set(&mdev->current_epoch->epoch_size, 0);
3949 D_ASSERT(list_empty(&mdev->current_epoch->list));
3950}
3951
3952/*
3953 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
3954 * we can agree on is stored in agreed_pro_version.
3955 *
3956 * feature flags and the reserved array should be enough room for future
3957 * enhancements of the handshake protocol, and possible plugins...
3958 *
3959 * for now, they are expected to be zero, but ignored.
3960 */
3961static int drbd_send_handshake(struct drbd_conf *mdev)
3962{
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01003963 /* ASSERT current == mdev->tconn->receiver ... */
Philipp Reisnere42325a2011-01-19 13:55:45 +01003964 struct p_handshake *p = &mdev->tconn->data.sbuf.handshake;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003965 int ok;
3966
Philipp Reisnere42325a2011-01-19 13:55:45 +01003967 if (mutex_lock_interruptible(&mdev->tconn->data.mutex)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003968 dev_err(DEV, "interrupted during initial handshake\n");
3969 return 0; /* interrupted. not ok. */
3970 }
3971
Philipp Reisnere42325a2011-01-19 13:55:45 +01003972 if (mdev->tconn->data.socket == NULL) {
3973 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003974 return 0;
3975 }
3976
3977 memset(p, 0, sizeof(*p));
3978 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
3979 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
Philipp Reisnerc0129492011-01-19 16:58:16 +01003980 ok = _drbd_send_cmd(mdev, mdev->tconn->data.socket, P_HAND_SHAKE,
3981 &p->head, sizeof(*p), 0 );
Philipp Reisnere42325a2011-01-19 13:55:45 +01003982 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003983 return ok;
3984}
3985
3986/*
3987 * return values:
3988 * 1 yes, we have a valid connection
3989 * 0 oops, did not work out, please try again
3990 * -1 peer talks different language,
3991 * no point in trying again, please go standalone.
3992 */
3993static int drbd_do_handshake(struct drbd_conf *mdev)
3994{
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01003995 /* ASSERT current == mdev->tconn->receiver ... */
Philipp Reisnere42325a2011-01-19 13:55:45 +01003996 struct p_handshake *p = &mdev->tconn->data.rbuf.handshake;
Philipp Reisner02918be2010-08-20 14:35:10 +02003997 const int expect = sizeof(struct p_handshake) - sizeof(struct p_header80);
3998 unsigned int length;
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003999 enum drbd_packet cmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004000 int rv;
4001
4002 rv = drbd_send_handshake(mdev);
4003 if (!rv)
4004 return 0;
4005
Philipp Reisner02918be2010-08-20 14:35:10 +02004006 rv = drbd_recv_header(mdev, &cmd, &length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004007 if (!rv)
4008 return 0;
4009
Philipp Reisner02918be2010-08-20 14:35:10 +02004010 if (cmd != P_HAND_SHAKE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004011 dev_err(DEV, "expected HandShake packet, received: %s (0x%04x)\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02004012 cmdname(cmd), cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004013 return -1;
4014 }
4015
Philipp Reisner02918be2010-08-20 14:35:10 +02004016 if (length != expect) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004017 dev_err(DEV, "expected HandShake length: %u, received: %u\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02004018 expect, length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004019 return -1;
4020 }
4021
4022 rv = drbd_recv(mdev, &p->head.payload, expect);
4023
4024 if (rv != expect) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01004025 if (!signal_pending(current))
4026 dev_warn(DEV, "short read receiving handshake packet: l=%u\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004027 return 0;
4028 }
4029
Philipp Reisnerb411b362009-09-25 16:07:19 -07004030 p->protocol_min = be32_to_cpu(p->protocol_min);
4031 p->protocol_max = be32_to_cpu(p->protocol_max);
4032 if (p->protocol_max == 0)
4033 p->protocol_max = p->protocol_min;
4034
4035 if (PRO_VERSION_MAX < p->protocol_min ||
4036 PRO_VERSION_MIN > p->protocol_max)
4037 goto incompat;
4038
Philipp Reisner31890f42011-01-19 14:12:51 +01004039 mdev->tconn->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004040
4041 dev_info(DEV, "Handshake successful: "
Philipp Reisner31890f42011-01-19 14:12:51 +01004042 "Agreed network protocol version %d\n", mdev->tconn->agreed_pro_version);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004043
4044 return 1;
4045
4046 incompat:
4047 dev_err(DEV, "incompatible DRBD dialects: "
4048 "I support %d-%d, peer supports %d-%d\n",
4049 PRO_VERSION_MIN, PRO_VERSION_MAX,
4050 p->protocol_min, p->protocol_max);
4051 return -1;
4052}
4053
4054#if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
4055static int drbd_do_auth(struct drbd_conf *mdev)
4056{
4057 dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
4058 dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004059 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004060}
4061#else
4062#define CHALLENGE_LEN 64
Johannes Thomab10d96c2010-01-07 16:02:50 +01004063
4064/* Return value:
4065 1 - auth succeeded,
4066 0 - failed, try again (network error),
4067 -1 - auth failed, don't try again.
4068*/
4069
Philipp Reisnerb411b362009-09-25 16:07:19 -07004070static int drbd_do_auth(struct drbd_conf *mdev)
4071{
4072 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
4073 struct scatterlist sg;
4074 char *response = NULL;
4075 char *right_response = NULL;
4076 char *peers_ch = NULL;
Philipp Reisner89e58e72011-01-19 13:12:45 +01004077 unsigned int key_len = strlen(mdev->tconn->net_conf->shared_secret);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004078 unsigned int resp_size;
4079 struct hash_desc desc;
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004080 enum drbd_packet cmd;
Philipp Reisner02918be2010-08-20 14:35:10 +02004081 unsigned int length;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004082 int rv;
4083
Philipp Reisnera0638452011-01-19 14:31:32 +01004084 desc.tfm = mdev->tconn->cram_hmac_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004085 desc.flags = 0;
4086
Philipp Reisnera0638452011-01-19 14:31:32 +01004087 rv = crypto_hash_setkey(mdev->tconn->cram_hmac_tfm,
Philipp Reisner89e58e72011-01-19 13:12:45 +01004088 (u8 *)mdev->tconn->net_conf->shared_secret, key_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004089 if (rv) {
4090 dev_err(DEV, "crypto_hash_setkey() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004091 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004092 goto fail;
4093 }
4094
4095 get_random_bytes(my_challenge, CHALLENGE_LEN);
4096
4097 rv = drbd_send_cmd2(mdev, P_AUTH_CHALLENGE, my_challenge, CHALLENGE_LEN);
4098 if (!rv)
4099 goto fail;
4100
Philipp Reisner02918be2010-08-20 14:35:10 +02004101 rv = drbd_recv_header(mdev, &cmd, &length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004102 if (!rv)
4103 goto fail;
4104
Philipp Reisner02918be2010-08-20 14:35:10 +02004105 if (cmd != P_AUTH_CHALLENGE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004106 dev_err(DEV, "expected AuthChallenge packet, received: %s (0x%04x)\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02004107 cmdname(cmd), cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004108 rv = 0;
4109 goto fail;
4110 }
4111
Philipp Reisner02918be2010-08-20 14:35:10 +02004112 if (length > CHALLENGE_LEN * 2) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004113 dev_err(DEV, "expected AuthChallenge payload too big.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004114 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004115 goto fail;
4116 }
4117
Philipp Reisner02918be2010-08-20 14:35:10 +02004118 peers_ch = kmalloc(length, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004119 if (peers_ch == NULL) {
4120 dev_err(DEV, "kmalloc of peers_ch failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004121 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004122 goto fail;
4123 }
4124
Philipp Reisner02918be2010-08-20 14:35:10 +02004125 rv = drbd_recv(mdev, peers_ch, length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004126
Philipp Reisner02918be2010-08-20 14:35:10 +02004127 if (rv != length) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01004128 if (!signal_pending(current))
4129 dev_warn(DEV, "short read AuthChallenge: l=%u\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004130 rv = 0;
4131 goto fail;
4132 }
4133
Philipp Reisnera0638452011-01-19 14:31:32 +01004134 resp_size = crypto_hash_digestsize(mdev->tconn->cram_hmac_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004135 response = kmalloc(resp_size, GFP_NOIO);
4136 if (response == NULL) {
4137 dev_err(DEV, "kmalloc of response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004138 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004139 goto fail;
4140 }
4141
4142 sg_init_table(&sg, 1);
Philipp Reisner02918be2010-08-20 14:35:10 +02004143 sg_set_buf(&sg, peers_ch, length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004144
4145 rv = crypto_hash_digest(&desc, &sg, sg.length, response);
4146 if (rv) {
4147 dev_err(DEV, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004148 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004149 goto fail;
4150 }
4151
4152 rv = drbd_send_cmd2(mdev, P_AUTH_RESPONSE, response, resp_size);
4153 if (!rv)
4154 goto fail;
4155
Philipp Reisner02918be2010-08-20 14:35:10 +02004156 rv = drbd_recv_header(mdev, &cmd, &length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004157 if (!rv)
4158 goto fail;
4159
Philipp Reisner02918be2010-08-20 14:35:10 +02004160 if (cmd != P_AUTH_RESPONSE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004161 dev_err(DEV, "expected AuthResponse packet, received: %s (0x%04x)\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02004162 cmdname(cmd), cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004163 rv = 0;
4164 goto fail;
4165 }
4166
Philipp Reisner02918be2010-08-20 14:35:10 +02004167 if (length != resp_size) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004168 dev_err(DEV, "expected AuthResponse payload of wrong size\n");
4169 rv = 0;
4170 goto fail;
4171 }
4172
4173 rv = drbd_recv(mdev, response , resp_size);
4174
4175 if (rv != resp_size) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01004176 if (!signal_pending(current))
4177 dev_warn(DEV, "short read receiving AuthResponse: l=%u\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004178 rv = 0;
4179 goto fail;
4180 }
4181
4182 right_response = kmalloc(resp_size, GFP_NOIO);
Julia Lawall2d1ee872009-12-27 22:27:11 +01004183 if (right_response == NULL) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004184 dev_err(DEV, "kmalloc of right_response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004185 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004186 goto fail;
4187 }
4188
4189 sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
4190
4191 rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
4192 if (rv) {
4193 dev_err(DEV, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004194 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004195 goto fail;
4196 }
4197
4198 rv = !memcmp(response, right_response, resp_size);
4199
4200 if (rv)
4201 dev_info(DEV, "Peer authenticated using %d bytes of '%s' HMAC\n",
Philipp Reisner89e58e72011-01-19 13:12:45 +01004202 resp_size, mdev->tconn->net_conf->cram_hmac_alg);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004203 else
4204 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004205
4206 fail:
4207 kfree(peers_ch);
4208 kfree(response);
4209 kfree(right_response);
4210
4211 return rv;
4212}
4213#endif
4214
4215int drbdd_init(struct drbd_thread *thi)
4216{
4217 struct drbd_conf *mdev = thi->mdev;
4218 unsigned int minor = mdev_to_minor(mdev);
4219 int h;
4220
4221 sprintf(current->comm, "drbd%d_receiver", minor);
4222
4223 dev_info(DEV, "receiver (re)started\n");
4224
4225 do {
4226 h = drbd_connect(mdev);
4227 if (h == 0) {
4228 drbd_disconnect(mdev);
Philipp Reisner20ee6392011-01-18 15:28:59 +01004229 schedule_timeout_interruptible(HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004230 }
4231 if (h == -1) {
4232 dev_warn(DEV, "Discarding network configuration.\n");
4233 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
4234 }
4235 } while (h == 0);
4236
4237 if (h > 0) {
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01004238 if (get_net_conf(mdev->tconn)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004239 drbdd(mdev);
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01004240 put_net_conf(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004241 }
4242 }
4243
4244 drbd_disconnect(mdev);
4245
4246 dev_info(DEV, "receiver terminated\n");
4247 return 0;
4248}
4249
4250/* ********* acknowledge sender ******** */
4251
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004252static int got_RqSReply(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004253{
Philipp Reisner257d0af2011-01-26 12:15:29 +01004254 struct p_req_state_reply *p = &mdev->tconn->meta.rbuf.req_state_reply;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004255
4256 int retcode = be32_to_cpu(p->retcode);
4257
4258 if (retcode >= SS_SUCCESS) {
4259 set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
4260 } else {
4261 set_bit(CL_ST_CHG_FAIL, &mdev->flags);
4262 dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
4263 drbd_set_st_err_str(retcode), retcode);
4264 }
4265 wake_up(&mdev->state_wait);
4266
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004267 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004268}
4269
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004270static int got_Ping(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004271{
4272 return drbd_send_ping_ack(mdev);
4273
4274}
4275
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004276static int got_PingAck(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004277{
4278 /* restore idle timeout */
Philipp Reisnere42325a2011-01-19 13:55:45 +01004279 mdev->tconn->meta.socket->sk->sk_rcvtimeo = mdev->tconn->net_conf->ping_int*HZ;
Philipp Reisner309d1602010-03-02 15:03:44 +01004280 if (!test_and_set_bit(GOT_PING_ACK, &mdev->flags))
4281 wake_up(&mdev->misc_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004282
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004283 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004284}
4285
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004286static int got_IsInSync(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004287{
Philipp Reisner257d0af2011-01-26 12:15:29 +01004288 struct p_block_ack *p = &mdev->tconn->meta.rbuf.block_ack;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004289 sector_t sector = be64_to_cpu(p->sector);
4290 int blksize = be32_to_cpu(p->blksize);
4291
Philipp Reisner31890f42011-01-19 14:12:51 +01004292 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004293
4294 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4295
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004296 if (get_ldev(mdev)) {
4297 drbd_rs_complete_io(mdev, sector);
4298 drbd_set_in_sync(mdev, sector, blksize);
4299 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
4300 mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
4301 put_ldev(mdev);
4302 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004303 dec_rs_pending(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02004304 atomic_add(blksize >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004305
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004306 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004307}
4308
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004309static int
4310validate_req_change_req_state(struct drbd_conf *mdev, u64 id, sector_t sector,
4311 struct rb_root *root, const char *func,
4312 enum drbd_req_event what, bool missing_ok)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004313{
4314 struct drbd_request *req;
4315 struct bio_and_error m;
4316
Philipp Reisner87eeee42011-01-19 14:16:30 +01004317 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004318 req = find_request(mdev, root, id, sector, missing_ok, func);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004319 if (unlikely(!req)) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01004320 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004321 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004322 }
4323 __req_mod(req, what, &m);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004324 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004325
4326 if (m.bio)
4327 complete_master_bio(mdev, &m);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004328 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004329}
4330
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004331static int got_BlockAck(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004332{
Philipp Reisner257d0af2011-01-26 12:15:29 +01004333 struct p_block_ack *p = &mdev->tconn->meta.rbuf.block_ack;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004334 sector_t sector = be64_to_cpu(p->sector);
4335 int blksize = be32_to_cpu(p->blksize);
4336 enum drbd_req_event what;
4337
4338 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4339
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004340 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004341 drbd_set_in_sync(mdev, sector, blksize);
4342 dec_rs_pending(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004343 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004344 }
Philipp Reisner257d0af2011-01-26 12:15:29 +01004345 switch (cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004346 case P_RS_WRITE_ACK:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004347 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004348 what = WRITE_ACKED_BY_PEER_AND_SIS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004349 break;
4350 case P_WRITE_ACK:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004351 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004352 what = WRITE_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004353 break;
4354 case P_RECV_ACK:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004355 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_B);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004356 what = RECV_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004357 break;
4358 case P_DISCARD_ACK:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004359 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004360 what = CONFLICT_DISCARDED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004361 break;
4362 default:
4363 D_ASSERT(0);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004364 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004365 }
4366
4367 return validate_req_change_req_state(mdev, p->block_id, sector,
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004368 &mdev->write_requests, __func__,
4369 what, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004370}
4371
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004372static int got_NegAck(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004373{
Philipp Reisner257d0af2011-01-26 12:15:29 +01004374 struct p_block_ack *p = &mdev->tconn->meta.rbuf.block_ack;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004375 sector_t sector = be64_to_cpu(p->sector);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004376 int size = be32_to_cpu(p->blksize);
Philipp Reisner89e58e72011-01-19 13:12:45 +01004377 bool missing_ok = mdev->tconn->net_conf->wire_protocol == DRBD_PROT_A ||
4378 mdev->tconn->net_conf->wire_protocol == DRBD_PROT_B;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004379 bool found;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004380
4381 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4382
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004383 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004384 dec_rs_pending(mdev);
4385 drbd_rs_failed_io(mdev, sector, size);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004386 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004387 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01004388
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004389 found = validate_req_change_req_state(mdev, p->block_id, sector,
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004390 &mdev->write_requests, __func__,
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004391 NEG_ACKED, missing_ok);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004392 if (!found) {
4393 /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs.
4394 The master bio might already be completed, therefore the
4395 request is no longer in the collision hash. */
4396 /* In Protocol B we might already have got a P_RECV_ACK
4397 but then get a P_NEG_ACK afterwards. */
4398 if (!missing_ok)
Philipp Reisner2deb8332011-01-17 18:39:18 +01004399 return false;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004400 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004401 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01004402 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004403}
4404
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004405static int got_NegDReply(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004406{
Philipp Reisner257d0af2011-01-26 12:15:29 +01004407 struct p_block_ack *p = &mdev->tconn->meta.rbuf.block_ack;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004408 sector_t sector = be64_to_cpu(p->sector);
4409
4410 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4411 dev_err(DEV, "Got NegDReply; Sector %llus, len %u; Fail original request.\n",
4412 (unsigned long long)sector, be32_to_cpu(p->blksize));
4413
4414 return validate_req_change_req_state(mdev, p->block_id, sector,
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004415 &mdev->read_requests, __func__,
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004416 NEG_ACKED, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004417}
4418
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004419static int got_NegRSDReply(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004420{
4421 sector_t sector;
4422 int size;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004423 struct p_block_ack *p = &mdev->tconn->meta.rbuf.block_ack;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004424
4425 sector = be64_to_cpu(p->sector);
4426 size = be32_to_cpu(p->blksize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004427
4428 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4429
4430 dec_rs_pending(mdev);
4431
4432 if (get_ldev_if_state(mdev, D_FAILED)) {
4433 drbd_rs_complete_io(mdev, sector);
Philipp Reisner257d0af2011-01-26 12:15:29 +01004434 switch (cmd) {
Philipp Reisnerd612d302010-12-27 10:53:28 +01004435 case P_NEG_RS_DREPLY:
4436 drbd_rs_failed_io(mdev, sector, size);
4437 case P_RS_CANCEL:
4438 break;
4439 default:
4440 D_ASSERT(0);
4441 put_ldev(mdev);
4442 return false;
4443 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004444 put_ldev(mdev);
4445 }
4446
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004447 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004448}
4449
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004450static int got_BarrierAck(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004451{
Philipp Reisner257d0af2011-01-26 12:15:29 +01004452 struct p_barrier_ack *p = &mdev->tconn->meta.rbuf.barrier_ack;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004453
4454 tl_release(mdev, p->barrier, be32_to_cpu(p->set_size));
4455
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02004456 if (mdev->state.conn == C_AHEAD &&
4457 atomic_read(&mdev->ap_in_flight) == 0 &&
Philipp Reisner370a43e2011-01-14 16:03:11 +01004458 !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->current_epoch->flags)) {
4459 mdev->start_resync_timer.expires = jiffies + HZ;
4460 add_timer(&mdev->start_resync_timer);
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02004461 }
4462
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004463 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004464}
4465
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004466static int got_OVResult(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004467{
Philipp Reisner257d0af2011-01-26 12:15:29 +01004468 struct p_block_ack *p = &mdev->tconn->meta.rbuf.block_ack;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004469 struct drbd_work *w;
4470 sector_t sector;
4471 int size;
4472
4473 sector = be64_to_cpu(p->sector);
4474 size = be32_to_cpu(p->blksize);
4475
4476 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4477
4478 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
4479 drbd_ov_oos_found(mdev, sector, size);
4480 else
4481 ov_oos_print(mdev);
4482
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004483 if (!get_ldev(mdev))
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004484 return true;
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004485
Philipp Reisnerb411b362009-09-25 16:07:19 -07004486 drbd_rs_complete_io(mdev, sector);
4487 dec_rs_pending(mdev);
4488
Lars Ellenbergea5442a2010-11-05 09:48:01 +01004489 --mdev->ov_left;
4490
4491 /* let's advance progress step marks only for every other megabyte */
4492 if ((mdev->ov_left & 0x200) == 0x200)
4493 drbd_advance_rs_marks(mdev, mdev->ov_left);
4494
4495 if (mdev->ov_left == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004496 w = kmalloc(sizeof(*w), GFP_NOIO);
4497 if (w) {
4498 w->cb = w_ov_finished;
Philipp Reisnere42325a2011-01-19 13:55:45 +01004499 drbd_queue_work_front(&mdev->tconn->data.work, w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004500 } else {
4501 dev_err(DEV, "kmalloc(w) failed.");
4502 ov_oos_print(mdev);
4503 drbd_resync_finished(mdev);
4504 }
4505 }
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004506 put_ldev(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004507 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004508}
4509
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004510static int got_skip(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisner0ced55a2010-04-30 15:26:20 +02004511{
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004512 return true;
Philipp Reisner0ced55a2010-04-30 15:26:20 +02004513}
4514
Philipp Reisnerb411b362009-09-25 16:07:19 -07004515struct asender_cmd {
4516 size_t pkt_size;
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004517 int (*process)(struct drbd_conf *mdev, enum drbd_packet cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004518};
4519
4520static struct asender_cmd *get_asender_cmd(int cmd)
4521{
4522 static struct asender_cmd asender_tbl[] = {
4523 /* anything missing from this table is in
4524 * the drbd_cmd_handler (drbd_default_handler) table,
4525 * see the beginning of drbdd() */
Philipp Reisner257d0af2011-01-26 12:15:29 +01004526 [P_PING] = { sizeof(struct p_header), got_Ping },
4527 [P_PING_ACK] = { sizeof(struct p_header), got_PingAck },
Philipp Reisnerb411b362009-09-25 16:07:19 -07004528 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4529 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4530 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4531 [P_DISCARD_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4532 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
4533 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
4534 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply},
4535 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
4536 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
4537 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
4538 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
Philipp Reisner02918be2010-08-20 14:35:10 +02004539 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
Philipp Reisnerd612d302010-12-27 10:53:28 +01004540 [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply},
Philipp Reisnerb411b362009-09-25 16:07:19 -07004541 [P_MAX_CMD] = { 0, NULL },
4542 };
4543 if (cmd > P_MAX_CMD || asender_tbl[cmd].process == NULL)
4544 return NULL;
4545 return &asender_tbl[cmd];
4546}
4547
4548int drbd_asender(struct drbd_thread *thi)
4549{
4550 struct drbd_conf *mdev = thi->mdev;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004551 struct p_header *h = &mdev->tconn->meta.rbuf.header;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004552 struct asender_cmd *cmd = NULL;
4553
Philipp Reisner257d0af2011-01-26 12:15:29 +01004554 int rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004555 void *buf = h;
4556 int received = 0;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004557 int expect = sizeof(struct p_header);
Lars Ellenbergf36af182011-03-09 22:44:55 +01004558 int ping_timeout_active = 0;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004559 int empty, pkt_size;
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004560 enum drbd_packet cmd_nr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004561
4562 sprintf(current->comm, "drbd%d_asender", mdev_to_minor(mdev));
4563
4564 current->policy = SCHED_RR; /* Make this a realtime task! */
4565 current->rt_priority = 2; /* more important than all other tasks */
4566
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01004567 while (get_t_state(thi) == RUNNING) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004568 drbd_thread_current_set_cpu(mdev);
4569 if (test_and_clear_bit(SEND_PING, &mdev->flags)) {
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01004570 if (!drbd_send_ping(mdev)) {
4571 dev_err(DEV, "drbd_send_ping has failed\n");
4572 goto reconnect;
4573 }
Philipp Reisnere42325a2011-01-19 13:55:45 +01004574 mdev->tconn->meta.socket->sk->sk_rcvtimeo =
Philipp Reisner89e58e72011-01-19 13:12:45 +01004575 mdev->tconn->net_conf->ping_timeo*HZ/10;
Lars Ellenbergf36af182011-03-09 22:44:55 +01004576 ping_timeout_active = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004577 }
4578
4579 /* conditionally cork;
4580 * it may hurt latency if we cork without much to send */
Philipp Reisner89e58e72011-01-19 13:12:45 +01004581 if (!mdev->tconn->net_conf->no_cork &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07004582 3 < atomic_read(&mdev->unacked_cnt))
Philipp Reisnere42325a2011-01-19 13:55:45 +01004583 drbd_tcp_cork(mdev->tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004584 while (1) {
4585 clear_bit(SIGNAL_ASENDER, &mdev->flags);
4586 flush_signals(current);
Lars Ellenberg0f8488e2010-10-13 18:19:23 +02004587 if (!drbd_process_done_ee(mdev))
Philipp Reisnerb411b362009-09-25 16:07:19 -07004588 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004589 /* to avoid race with newly queued ACKs */
4590 set_bit(SIGNAL_ASENDER, &mdev->flags);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004591 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004592 empty = list_empty(&mdev->done_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004593 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004594 /* new ack may have been queued right here,
4595 * but then there is also a signal pending,
4596 * and we start over... */
4597 if (empty)
4598 break;
4599 }
4600 /* but unconditionally uncork unless disabled */
Philipp Reisner89e58e72011-01-19 13:12:45 +01004601 if (!mdev->tconn->net_conf->no_cork)
Philipp Reisnere42325a2011-01-19 13:55:45 +01004602 drbd_tcp_uncork(mdev->tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004603
4604 /* short circuit, recv_msg would return EINTR anyways. */
4605 if (signal_pending(current))
4606 continue;
4607
Philipp Reisnere42325a2011-01-19 13:55:45 +01004608 rv = drbd_recv_short(mdev, mdev->tconn->meta.socket,
Philipp Reisnerb411b362009-09-25 16:07:19 -07004609 buf, expect-received, 0);
4610 clear_bit(SIGNAL_ASENDER, &mdev->flags);
4611
4612 flush_signals(current);
4613
4614 /* Note:
4615 * -EINTR (on meta) we got a signal
4616 * -EAGAIN (on meta) rcvtimeo expired
4617 * -ECONNRESET other side closed the connection
4618 * -ERESTARTSYS (on data) we got a signal
4619 * rv < 0 other than above: unexpected error!
4620 * rv == expected: full header or command
4621 * rv < expected: "woken" by signal during receive
4622 * rv == 0 : "connection shut down by peer"
4623 */
4624 if (likely(rv > 0)) {
4625 received += rv;
4626 buf += rv;
4627 } else if (rv == 0) {
4628 dev_err(DEV, "meta connection shut down by peer.\n");
4629 goto reconnect;
4630 } else if (rv == -EAGAIN) {
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02004631 /* If the data socket received something meanwhile,
4632 * that is good enough: peer is still alive. */
Philipp Reisner31890f42011-01-19 14:12:51 +01004633 if (time_after(mdev->tconn->last_received,
Philipp Reisnere42325a2011-01-19 13:55:45 +01004634 jiffies - mdev->tconn->meta.socket->sk->sk_rcvtimeo))
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02004635 continue;
Lars Ellenbergf36af182011-03-09 22:44:55 +01004636 if (ping_timeout_active) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004637 dev_err(DEV, "PingAck did not arrive in time.\n");
4638 goto reconnect;
4639 }
4640 set_bit(SEND_PING, &mdev->flags);
4641 continue;
4642 } else if (rv == -EINTR) {
4643 continue;
4644 } else {
4645 dev_err(DEV, "sock_recvmsg returned %d\n", rv);
4646 goto reconnect;
4647 }
4648
4649 if (received == expect && cmd == NULL) {
Philipp Reisner257d0af2011-01-26 12:15:29 +01004650 if (!decode_header(mdev, h, &cmd_nr, &pkt_size))
Philipp Reisnerb411b362009-09-25 16:07:19 -07004651 goto reconnect;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004652 cmd = get_asender_cmd(cmd_nr);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004653 if (unlikely(cmd == NULL)) {
Philipp Reisner257d0af2011-01-26 12:15:29 +01004654 dev_err(DEV, "unknown command %d on meta (l: %d)\n",
4655 cmd_nr, pkt_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004656 goto disconnect;
4657 }
4658 expect = cmd->pkt_size;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004659 if (pkt_size != expect - sizeof(struct p_header)) {
4660 dev_err(DEV, "Wrong packet size on meta (c: %d, l: %d)\n",
4661 cmd_nr, pkt_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004662 goto reconnect;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004663 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004664 }
4665 if (received == expect) {
Philipp Reisner31890f42011-01-19 14:12:51 +01004666 mdev->tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004667 D_ASSERT(cmd != NULL);
Philipp Reisner257d0af2011-01-26 12:15:29 +01004668 if (!cmd->process(mdev, cmd_nr))
Philipp Reisnerb411b362009-09-25 16:07:19 -07004669 goto reconnect;
4670
Lars Ellenbergf36af182011-03-09 22:44:55 +01004671 /* the idle_timeout (ping-int)
4672 * has been restored in got_PingAck() */
4673 if (cmd == get_asender_cmd(P_PING_ACK))
4674 ping_timeout_active = 0;
4675
Philipp Reisnerb411b362009-09-25 16:07:19 -07004676 buf = h;
4677 received = 0;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004678 expect = sizeof(struct p_header);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004679 cmd = NULL;
4680 }
4681 }
4682
4683 if (0) {
4684reconnect:
4685 drbd_force_state(mdev, NS(conn, C_NETWORK_FAILURE));
Lars Ellenberg856c50c2010-10-14 13:37:40 +02004686 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004687 }
4688 if (0) {
4689disconnect:
4690 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Lars Ellenberg856c50c2010-10-14 13:37:40 +02004691 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004692 }
4693 clear_bit(SIGNAL_ASENDER, &mdev->flags);
4694
4695 D_ASSERT(mdev->state.conn < C_CONNECTED);
4696 dev_info(DEV, "asender terminated\n");
4697
4698 return 0;
4699}