blob: c0435c4f5d84d81cb04f04c4cae694bdaef15ed1 [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);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700337 e->epoch = NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200338 e->mdev = mdev;
339 e->pages = page;
340 atomic_set(&e->pending_bios, 0);
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +0100341 e->i.size = data_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700342 e->flags = 0;
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +0100343 e->i.sector = sector;
Andreas Gruenbacher9a8e7752011-01-11 14:04:09 +0100344 /*
345 * The block_id is opaque to the receiver. It is not endianness
346 * converted, and sent back to the sender unchanged.
347 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200348 e->block_id = id;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700349
Philipp Reisnerb411b362009-09-25 16:07:19 -0700350 return e;
351
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200352 fail:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700353 mempool_free(e, drbd_ee_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700354 return NULL;
355}
356
Lars Ellenberg435f0742010-09-06 12:30:25 +0200357void drbd_free_some_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e, int is_net)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700358{
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +0200359 if (e->flags & EE_HAS_DIGEST)
360 kfree(e->digest);
Lars Ellenberg435f0742010-09-06 12:30:25 +0200361 drbd_pp_free(mdev, e->pages, is_net);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200362 D_ASSERT(atomic_read(&e->pending_bios) == 0);
Andreas Gruenbacher8b946252011-01-20 15:23:07 +0100363 D_ASSERT(drbd_interval_empty(&e->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700364 mempool_free(e, drbd_ee_mempool);
365}
366
367int drbd_release_ee(struct drbd_conf *mdev, struct list_head *list)
368{
369 LIST_HEAD(work_list);
370 struct drbd_epoch_entry *e, *t;
371 int count = 0;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200372 int is_net = list == &mdev->net_ee;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700373
Philipp Reisner87eeee42011-01-19 14:16:30 +0100374 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700375 list_splice_init(list, &work_list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100376 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700377
378 list_for_each_entry_safe(e, t, &work_list, w.list) {
Lars Ellenberg435f0742010-09-06 12:30:25 +0200379 drbd_free_some_ee(mdev, e, is_net);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700380 count++;
381 }
382 return count;
383}
384
385
386/*
387 * This function is called from _asender only_
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100388 * but see also comments in _req_mod(,BARRIER_ACKED)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700389 * and receive_Barrier.
390 *
391 * Move entries from net_ee to done_ee, if ready.
392 * Grab done_ee, call all callbacks, free the entries.
393 * The callbacks typically send out ACKs.
394 */
395static int drbd_process_done_ee(struct drbd_conf *mdev)
396{
397 LIST_HEAD(work_list);
398 LIST_HEAD(reclaimed);
399 struct drbd_epoch_entry *e, *t;
400 int ok = (mdev->state.conn >= C_WF_REPORT_PARAMS);
401
Philipp Reisner87eeee42011-01-19 14:16:30 +0100402 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700403 reclaim_net_ee(mdev, &reclaimed);
404 list_splice_init(&mdev->done_ee, &work_list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100405 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700406
407 list_for_each_entry_safe(e, t, &reclaimed, w.list)
Lars Ellenberg435f0742010-09-06 12:30:25 +0200408 drbd_free_net_ee(mdev, e);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700409
410 /* possible callbacks here:
411 * e_end_block, and e_end_resync_block, e_send_discard_ack.
412 * all ignore the last argument.
413 */
414 list_for_each_entry_safe(e, t, &work_list, w.list) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700415 /* list_del not necessary, next/prev members not touched */
416 ok = e->w.cb(mdev, &e->w, !ok) && ok;
417 drbd_free_ee(mdev, e);
418 }
419 wake_up(&mdev->ee_wait);
420
421 return ok;
422}
423
424void _drbd_wait_ee_list_empty(struct drbd_conf *mdev, struct list_head *head)
425{
426 DEFINE_WAIT(wait);
427
428 /* avoids spin_lock/unlock
429 * and calling prepare_to_wait in the fast path */
430 while (!list_empty(head)) {
431 prepare_to_wait(&mdev->ee_wait, &wait, TASK_UNINTERRUPTIBLE);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100432 spin_unlock_irq(&mdev->tconn->req_lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100433 io_schedule();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700434 finish_wait(&mdev->ee_wait, &wait);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100435 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700436 }
437}
438
439void drbd_wait_ee_list_empty(struct drbd_conf *mdev, struct list_head *head)
440{
Philipp Reisner87eeee42011-01-19 14:16:30 +0100441 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700442 _drbd_wait_ee_list_empty(mdev, head);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100443 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700444}
445
446/* see also kernel_accept; which is only present since 2.6.18.
447 * also we want to log which part of it failed, exactly */
448static int drbd_accept(struct drbd_conf *mdev, const char **what,
449 struct socket *sock, struct socket **newsock)
450{
451 struct sock *sk = sock->sk;
452 int err = 0;
453
454 *what = "listen";
455 err = sock->ops->listen(sock, 5);
456 if (err < 0)
457 goto out;
458
459 *what = "sock_create_lite";
460 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
461 newsock);
462 if (err < 0)
463 goto out;
464
465 *what = "accept";
466 err = sock->ops->accept(sock, *newsock, 0);
467 if (err < 0) {
468 sock_release(*newsock);
469 *newsock = NULL;
470 goto out;
471 }
472 (*newsock)->ops = sock->ops;
473
474out:
475 return err;
476}
477
478static int drbd_recv_short(struct drbd_conf *mdev, struct socket *sock,
479 void *buf, size_t size, int flags)
480{
481 mm_segment_t oldfs;
482 struct kvec iov = {
483 .iov_base = buf,
484 .iov_len = size,
485 };
486 struct msghdr msg = {
487 .msg_iovlen = 1,
488 .msg_iov = (struct iovec *)&iov,
489 .msg_flags = (flags ? flags : MSG_WAITALL | MSG_NOSIGNAL)
490 };
491 int rv;
492
493 oldfs = get_fs();
494 set_fs(KERNEL_DS);
495 rv = sock_recvmsg(sock, &msg, size, msg.msg_flags);
496 set_fs(oldfs);
497
498 return rv;
499}
500
501static int drbd_recv(struct drbd_conf *mdev, void *buf, size_t size)
502{
503 mm_segment_t oldfs;
504 struct kvec iov = {
505 .iov_base = buf,
506 .iov_len = size,
507 };
508 struct msghdr msg = {
509 .msg_iovlen = 1,
510 .msg_iov = (struct iovec *)&iov,
511 .msg_flags = MSG_WAITALL | MSG_NOSIGNAL
512 };
513 int rv;
514
515 oldfs = get_fs();
516 set_fs(KERNEL_DS);
517
518 for (;;) {
Philipp Reisnere42325a2011-01-19 13:55:45 +0100519 rv = sock_recvmsg(mdev->tconn->data.socket, &msg, size, msg.msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700520 if (rv == size)
521 break;
522
523 /* Note:
524 * ECONNRESET other side closed the connection
525 * ERESTARTSYS (on sock) we got a signal
526 */
527
528 if (rv < 0) {
529 if (rv == -ECONNRESET)
530 dev_info(DEV, "sock was reset by peer\n");
531 else if (rv != -ERESTARTSYS)
532 dev_err(DEV, "sock_recvmsg returned %d\n", rv);
533 break;
534 } else if (rv == 0) {
535 dev_info(DEV, "sock was shut down by peer\n");
536 break;
537 } else {
538 /* signal came in, or peer/link went down,
539 * after we read a partial message
540 */
541 /* D_ASSERT(signal_pending(current)); */
542 break;
543 }
544 };
545
546 set_fs(oldfs);
547
548 if (rv != size)
549 drbd_force_state(mdev, NS(conn, C_BROKEN_PIPE));
550
551 return rv;
552}
553
Lars Ellenberg5dbf1672010-05-25 16:18:01 +0200554/* quoting tcp(7):
555 * On individual connections, the socket buffer size must be set prior to the
556 * listen(2) or connect(2) calls in order to have it take effect.
557 * This is our wrapper to do so.
558 */
559static void drbd_setbufsize(struct socket *sock, unsigned int snd,
560 unsigned int rcv)
561{
562 /* open coded SO_SNDBUF, SO_RCVBUF */
563 if (snd) {
564 sock->sk->sk_sndbuf = snd;
565 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
566 }
567 if (rcv) {
568 sock->sk->sk_rcvbuf = rcv;
569 sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
570 }
571}
572
Philipp Reisnerb411b362009-09-25 16:07:19 -0700573static struct socket *drbd_try_connect(struct drbd_conf *mdev)
574{
575 const char *what;
576 struct socket *sock;
577 struct sockaddr_in6 src_in6;
578 int err;
579 int disconnect_on_error = 1;
580
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +0100581 if (!get_net_conf(mdev->tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700582 return NULL;
583
584 what = "sock_create_kern";
Philipp Reisner89e58e72011-01-19 13:12:45 +0100585 err = sock_create_kern(((struct sockaddr *)mdev->tconn->net_conf->my_addr)->sa_family,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700586 SOCK_STREAM, IPPROTO_TCP, &sock);
587 if (err < 0) {
588 sock = NULL;
589 goto out;
590 }
591
592 sock->sk->sk_rcvtimeo =
Philipp Reisner89e58e72011-01-19 13:12:45 +0100593 sock->sk->sk_sndtimeo = mdev->tconn->net_conf->try_connect_int*HZ;
594 drbd_setbufsize(sock, mdev->tconn->net_conf->sndbuf_size,
595 mdev->tconn->net_conf->rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700596
597 /* explicitly bind to the configured IP as source IP
598 * for the outgoing connections.
599 * This is needed for multihomed hosts and to be
600 * able to use lo: interfaces for drbd.
601 * Make sure to use 0 as port number, so linux selects
602 * a free one dynamically.
603 */
Philipp Reisner89e58e72011-01-19 13:12:45 +0100604 memcpy(&src_in6, mdev->tconn->net_conf->my_addr,
605 min_t(int, mdev->tconn->net_conf->my_addr_len, sizeof(src_in6)));
606 if (((struct sockaddr *)mdev->tconn->net_conf->my_addr)->sa_family == AF_INET6)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700607 src_in6.sin6_port = 0;
608 else
609 ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */
610
611 what = "bind before connect";
612 err = sock->ops->bind(sock,
613 (struct sockaddr *) &src_in6,
Philipp Reisner89e58e72011-01-19 13:12:45 +0100614 mdev->tconn->net_conf->my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700615 if (err < 0)
616 goto out;
617
618 /* connect may fail, peer not yet available.
619 * stay C_WF_CONNECTION, don't go Disconnecting! */
620 disconnect_on_error = 0;
621 what = "connect";
622 err = sock->ops->connect(sock,
Philipp Reisner89e58e72011-01-19 13:12:45 +0100623 (struct sockaddr *)mdev->tconn->net_conf->peer_addr,
624 mdev->tconn->net_conf->peer_addr_len, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700625
626out:
627 if (err < 0) {
628 if (sock) {
629 sock_release(sock);
630 sock = NULL;
631 }
632 switch (-err) {
633 /* timeout, busy, signal pending */
634 case ETIMEDOUT: case EAGAIN: case EINPROGRESS:
635 case EINTR: case ERESTARTSYS:
636 /* peer not (yet) available, network problem */
637 case ECONNREFUSED: case ENETUNREACH:
638 case EHOSTDOWN: case EHOSTUNREACH:
639 disconnect_on_error = 0;
640 break;
641 default:
642 dev_err(DEV, "%s failed, err = %d\n", what, err);
643 }
644 if (disconnect_on_error)
645 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
646 }
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +0100647 put_net_conf(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700648 return sock;
649}
650
651static struct socket *drbd_wait_for_connect(struct drbd_conf *mdev)
652{
653 int timeo, err;
654 struct socket *s_estab = NULL, *s_listen;
655 const char *what;
656
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +0100657 if (!get_net_conf(mdev->tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700658 return NULL;
659
660 what = "sock_create_kern";
Philipp Reisner89e58e72011-01-19 13:12:45 +0100661 err = sock_create_kern(((struct sockaddr *)mdev->tconn->net_conf->my_addr)->sa_family,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700662 SOCK_STREAM, IPPROTO_TCP, &s_listen);
663 if (err) {
664 s_listen = NULL;
665 goto out;
666 }
667
Philipp Reisner89e58e72011-01-19 13:12:45 +0100668 timeo = mdev->tconn->net_conf->try_connect_int * HZ;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700669 timeo += (random32() & 1) ? timeo / 7 : -timeo / 7; /* 28.5% random jitter */
670
671 s_listen->sk->sk_reuse = 1; /* SO_REUSEADDR */
672 s_listen->sk->sk_rcvtimeo = timeo;
673 s_listen->sk->sk_sndtimeo = timeo;
Philipp Reisner89e58e72011-01-19 13:12:45 +0100674 drbd_setbufsize(s_listen, mdev->tconn->net_conf->sndbuf_size,
675 mdev->tconn->net_conf->rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700676
677 what = "bind before listen";
678 err = s_listen->ops->bind(s_listen,
Philipp Reisner89e58e72011-01-19 13:12:45 +0100679 (struct sockaddr *) mdev->tconn->net_conf->my_addr,
680 mdev->tconn->net_conf->my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700681 if (err < 0)
682 goto out;
683
684 err = drbd_accept(mdev, &what, s_listen, &s_estab);
685
686out:
687 if (s_listen)
688 sock_release(s_listen);
689 if (err < 0) {
690 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
691 dev_err(DEV, "%s failed, err = %d\n", what, err);
692 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
693 }
694 }
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +0100695 put_net_conf(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700696
697 return s_estab;
698}
699
700static int drbd_send_fp(struct drbd_conf *mdev,
701 struct socket *sock, enum drbd_packets cmd)
702{
Philipp Reisnerc0129492011-01-19 16:58:16 +0100703 struct p_header *h = &mdev->tconn->data.sbuf.header;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700704
705 return _drbd_send_cmd(mdev, sock, cmd, h, sizeof(*h), 0);
706}
707
708static enum drbd_packets drbd_recv_fp(struct drbd_conf *mdev, struct socket *sock)
709{
Philipp Reisnere42325a2011-01-19 13:55:45 +0100710 struct p_header80 *h = &mdev->tconn->data.rbuf.header.h80;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700711 int rr;
712
713 rr = drbd_recv_short(mdev, sock, h, sizeof(*h), 0);
714
Andreas Gruenbacherca9bc122011-01-11 13:47:24 +0100715 if (rr == sizeof(*h) && h->magic == cpu_to_be32(DRBD_MAGIC))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700716 return be16_to_cpu(h->command);
717
718 return 0xffff;
719}
720
721/**
722 * drbd_socket_okay() - Free the socket if its connection is not okay
723 * @mdev: DRBD device.
724 * @sock: pointer to the pointer to the socket.
725 */
726static int drbd_socket_okay(struct drbd_conf *mdev, struct socket **sock)
727{
728 int rr;
729 char tb[4];
730
731 if (!*sock)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100732 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700733
734 rr = drbd_recv_short(mdev, *sock, tb, 4, MSG_DONTWAIT | MSG_PEEK);
735
736 if (rr > 0 || rr == -EAGAIN) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100737 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700738 } else {
739 sock_release(*sock);
740 *sock = NULL;
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100741 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700742 }
743}
744
745/*
746 * return values:
747 * 1 yes, we have a valid connection
748 * 0 oops, did not work out, please try again
749 * -1 peer talks different language,
750 * no point in trying again, please go standalone.
751 * -2 We do not have a network config...
752 */
753static int drbd_connect(struct drbd_conf *mdev)
754{
755 struct socket *s, *sock, *msock;
756 int try, h, ok;
757
Philipp Reisnere42325a2011-01-19 13:55:45 +0100758 D_ASSERT(!mdev->tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700759
Philipp Reisnerb411b362009-09-25 16:07:19 -0700760 if (drbd_request_state(mdev, NS(conn, C_WF_CONNECTION)) < SS_SUCCESS)
761 return -2;
762
763 clear_bit(DISCARD_CONCURRENT, &mdev->flags);
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100764 mdev->tconn->agreed_pro_version = 99;
765 /* agreed_pro_version must be smaller than 100 so we send the old
766 header (h80) in the first packet and in the handshake packet. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700767
768 sock = NULL;
769 msock = NULL;
770
771 do {
772 for (try = 0;;) {
773 /* 3 tries, this should take less than a second! */
774 s = drbd_try_connect(mdev);
775 if (s || ++try >= 3)
776 break;
777 /* give the other side time to call bind() & listen() */
Philipp Reisner20ee6392011-01-18 15:28:59 +0100778 schedule_timeout_interruptible(HZ / 10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700779 }
780
781 if (s) {
782 if (!sock) {
783 drbd_send_fp(mdev, s, P_HAND_SHAKE_S);
784 sock = s;
785 s = NULL;
786 } else if (!msock) {
787 drbd_send_fp(mdev, s, P_HAND_SHAKE_M);
788 msock = s;
789 s = NULL;
790 } else {
791 dev_err(DEV, "Logic error in drbd_connect()\n");
792 goto out_release_sockets;
793 }
794 }
795
796 if (sock && msock) {
Philipp Reisner89e58e72011-01-19 13:12:45 +0100797 schedule_timeout_interruptible(mdev->tconn->net_conf->ping_timeo*HZ/10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700798 ok = drbd_socket_okay(mdev, &sock);
799 ok = drbd_socket_okay(mdev, &msock) && ok;
800 if (ok)
801 break;
802 }
803
804retry:
805 s = drbd_wait_for_connect(mdev);
806 if (s) {
807 try = drbd_recv_fp(mdev, s);
808 drbd_socket_okay(mdev, &sock);
809 drbd_socket_okay(mdev, &msock);
810 switch (try) {
811 case P_HAND_SHAKE_S:
812 if (sock) {
813 dev_warn(DEV, "initial packet S crossed\n");
814 sock_release(sock);
815 }
816 sock = s;
817 break;
818 case P_HAND_SHAKE_M:
819 if (msock) {
820 dev_warn(DEV, "initial packet M crossed\n");
821 sock_release(msock);
822 }
823 msock = s;
824 set_bit(DISCARD_CONCURRENT, &mdev->flags);
825 break;
826 default:
827 dev_warn(DEV, "Error receiving initial packet\n");
828 sock_release(s);
829 if (random32() & 1)
830 goto retry;
831 }
832 }
833
834 if (mdev->state.conn <= C_DISCONNECTING)
835 goto out_release_sockets;
836 if (signal_pending(current)) {
837 flush_signals(current);
838 smp_rmb();
Philipp Reisnere6b3ea82011-01-19 14:02:01 +0100839 if (get_t_state(&mdev->tconn->receiver) == EXITING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700840 goto out_release_sockets;
841 }
842
843 if (sock && msock) {
844 ok = drbd_socket_okay(mdev, &sock);
845 ok = drbd_socket_okay(mdev, &msock) && ok;
846 if (ok)
847 break;
848 }
849 } while (1);
850
851 msock->sk->sk_reuse = 1; /* SO_REUSEADDR */
852 sock->sk->sk_reuse = 1; /* SO_REUSEADDR */
853
854 sock->sk->sk_allocation = GFP_NOIO;
855 msock->sk->sk_allocation = GFP_NOIO;
856
857 sock->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
858 msock->sk->sk_priority = TC_PRIO_INTERACTIVE;
859
Philipp Reisnerb411b362009-09-25 16:07:19 -0700860 /* NOT YET ...
Philipp Reisner89e58e72011-01-19 13:12:45 +0100861 * sock->sk->sk_sndtimeo = mdev->tconn->net_conf->timeout*HZ/10;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700862 * sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
863 * first set it to the P_HAND_SHAKE timeout,
864 * which we set to 4x the configured ping_timeout. */
865 sock->sk->sk_sndtimeo =
Philipp Reisner89e58e72011-01-19 13:12:45 +0100866 sock->sk->sk_rcvtimeo = mdev->tconn->net_conf->ping_timeo*4*HZ/10;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700867
Philipp Reisner89e58e72011-01-19 13:12:45 +0100868 msock->sk->sk_sndtimeo = mdev->tconn->net_conf->timeout*HZ/10;
869 msock->sk->sk_rcvtimeo = mdev->tconn->net_conf->ping_int*HZ;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700870
871 /* we don't want delays.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300872 * we use TCP_CORK where appropriate, though */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700873 drbd_tcp_nodelay(sock);
874 drbd_tcp_nodelay(msock);
875
Philipp Reisnere42325a2011-01-19 13:55:45 +0100876 mdev->tconn->data.socket = sock;
877 mdev->tconn->meta.socket = msock;
Philipp Reisner31890f42011-01-19 14:12:51 +0100878 mdev->tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700879
Philipp Reisnere6b3ea82011-01-19 14:02:01 +0100880 D_ASSERT(mdev->tconn->asender.task == NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700881
882 h = drbd_do_handshake(mdev);
883 if (h <= 0)
884 return h;
885
Philipp Reisnera0638452011-01-19 14:31:32 +0100886 if (mdev->tconn->cram_hmac_tfm) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700887 /* drbd_request_state(mdev, NS(conn, WFAuth)); */
Johannes Thomab10d96c2010-01-07 16:02:50 +0100888 switch (drbd_do_auth(mdev)) {
889 case -1:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700890 dev_err(DEV, "Authentication of peer failed\n");
891 return -1;
Johannes Thomab10d96c2010-01-07 16:02:50 +0100892 case 0:
893 dev_err(DEV, "Authentication of peer failed, trying again.\n");
894 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700895 }
896 }
897
898 if (drbd_request_state(mdev, NS(conn, C_WF_REPORT_PARAMS)) < SS_SUCCESS)
899 return 0;
900
Philipp Reisner89e58e72011-01-19 13:12:45 +0100901 sock->sk->sk_sndtimeo = mdev->tconn->net_conf->timeout*HZ/10;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700902 sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
903
904 atomic_set(&mdev->packet_seq, 0);
905 mdev->peer_seq = 0;
906
Philipp Reisnere6b3ea82011-01-19 14:02:01 +0100907 drbd_thread_start(&mdev->tconn->asender);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700908
Philipp Reisner148efa12011-01-15 00:21:15 +0100909 if (drbd_send_protocol(mdev) == -1)
Philipp Reisner7e2455c2010-04-22 14:50:23 +0200910 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700911 drbd_send_sync_param(mdev, &mdev->sync_conf);
Philipp Reisnere89b5912010-03-24 17:11:33 +0100912 drbd_send_sizes(mdev, 0, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700913 drbd_send_uuids(mdev);
914 drbd_send_state(mdev);
915 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
916 clear_bit(RESIZE_PENDING, &mdev->flags);
Philipp Reisner7fde2be2011-03-01 11:08:28 +0100917 mod_timer(&mdev->request_timer, jiffies + HZ); /* just start it here. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700918
919 return 1;
920
921out_release_sockets:
922 if (sock)
923 sock_release(sock);
924 if (msock)
925 sock_release(msock);
926 return -1;
927}
928
Philipp Reisner257d0af2011-01-26 12:15:29 +0100929static bool decode_header(struct drbd_conf *mdev, struct p_header *h, enum drbd_packets *cmd,
930 unsigned int *packet_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700931{
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100932 if (h->h80.magic == cpu_to_be32(DRBD_MAGIC)) {
Philipp Reisner02918be2010-08-20 14:35:10 +0200933 *cmd = be16_to_cpu(h->h80.command);
934 *packet_size = be16_to_cpu(h->h80.length);
Andreas Gruenbacherca9bc122011-01-11 13:47:24 +0100935 } else if (h->h95.magic == cpu_to_be16(DRBD_MAGIC_BIG)) {
Philipp Reisner02918be2010-08-20 14:35:10 +0200936 *cmd = be16_to_cpu(h->h95.command);
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100937 *packet_size = be32_to_cpu(h->h95.length) & 0x00ffffff;
Philipp Reisner02918be2010-08-20 14:35:10 +0200938 } else {
Lars Ellenberg004352f2010-10-05 20:13:58 +0200939 dev_err(DEV, "magic?? on data m: 0x%08x c: %d l: %d\n",
940 be32_to_cpu(h->h80.magic),
941 be16_to_cpu(h->h80.command),
942 be16_to_cpu(h->h80.length));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100943 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700944 }
Philipp Reisner257d0af2011-01-26 12:15:29 +0100945 return true;
946}
947
948static int drbd_recv_header(struct drbd_conf *mdev, enum drbd_packets *cmd, unsigned int *packet_size)
949{
950 struct p_header *h = &mdev->tconn->data.rbuf.header;
951 int r;
952
953 r = drbd_recv(mdev, h, sizeof(*h));
954 if (unlikely(r != sizeof(*h))) {
955 if (!signal_pending(current))
956 dev_warn(DEV, "short read expecting header on sock: r=%d\n", r);
957 return false;
958 }
959
960 r = decode_header(mdev, h, cmd, packet_size);
Philipp Reisner31890f42011-01-19 14:12:51 +0100961 mdev->tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700962
Philipp Reisner257d0af2011-01-26 12:15:29 +0100963 return r;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700964}
965
Philipp Reisner2451fc32010-08-24 13:43:11 +0200966static void drbd_flush(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700967{
968 int rv;
969
970 if (mdev->write_ordering >= WO_bdev_flush && get_ldev(mdev)) {
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400971 rv = blkdev_issue_flush(mdev->ldev->backing_bdev, GFP_KERNEL,
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200972 NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700973 if (rv) {
974 dev_err(DEV, "local disk flush failed with status %d\n", rv);
975 /* would rather check on EOPNOTSUPP, but that is not reliable.
976 * don't try again for ANY return value != 0
977 * if (rv == -EOPNOTSUPP) */
978 drbd_bump_write_ordering(mdev, WO_drain_io);
979 }
980 put_ldev(mdev);
981 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700982}
983
984/**
985 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
986 * @mdev: DRBD device.
987 * @epoch: Epoch object.
988 * @ev: Epoch event.
989 */
990static enum finish_epoch drbd_may_finish_epoch(struct drbd_conf *mdev,
991 struct drbd_epoch *epoch,
992 enum epoch_event ev)
993{
Philipp Reisner2451fc32010-08-24 13:43:11 +0200994 int epoch_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700995 struct drbd_epoch *next_epoch;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700996 enum finish_epoch rv = FE_STILL_LIVE;
997
998 spin_lock(&mdev->epoch_lock);
999 do {
1000 next_epoch = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001001
1002 epoch_size = atomic_read(&epoch->epoch_size);
1003
1004 switch (ev & ~EV_CLEANUP) {
1005 case EV_PUT:
1006 atomic_dec(&epoch->active);
1007 break;
1008 case EV_GOT_BARRIER_NR:
1009 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001010 break;
1011 case EV_BECAME_LAST:
1012 /* nothing to do*/
1013 break;
1014 }
1015
Philipp Reisnerb411b362009-09-25 16:07:19 -07001016 if (epoch_size != 0 &&
1017 atomic_read(&epoch->active) == 0 &&
Philipp Reisner2451fc32010-08-24 13:43:11 +02001018 test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001019 if (!(ev & EV_CLEANUP)) {
1020 spin_unlock(&mdev->epoch_lock);
1021 drbd_send_b_ack(mdev, epoch->barrier_nr, epoch_size);
1022 spin_lock(&mdev->epoch_lock);
1023 }
1024 dec_unacked(mdev);
1025
1026 if (mdev->current_epoch != epoch) {
1027 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1028 list_del(&epoch->list);
1029 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
1030 mdev->epochs--;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001031 kfree(epoch);
1032
1033 if (rv == FE_STILL_LIVE)
1034 rv = FE_DESTROYED;
1035 } else {
1036 epoch->flags = 0;
1037 atomic_set(&epoch->epoch_size, 0);
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001038 /* atomic_set(&epoch->active, 0); is already zero */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001039 if (rv == FE_STILL_LIVE)
1040 rv = FE_RECYCLED;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001041 wake_up(&mdev->ee_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001042 }
1043 }
1044
1045 if (!next_epoch)
1046 break;
1047
1048 epoch = next_epoch;
1049 } while (1);
1050
1051 spin_unlock(&mdev->epoch_lock);
1052
Philipp Reisnerb411b362009-09-25 16:07:19 -07001053 return rv;
1054}
1055
1056/**
1057 * drbd_bump_write_ordering() - Fall back to an other write ordering method
1058 * @mdev: DRBD device.
1059 * @wo: Write ordering method to try.
1060 */
1061void drbd_bump_write_ordering(struct drbd_conf *mdev, enum write_ordering_e wo) __must_hold(local)
1062{
1063 enum write_ordering_e pwo;
1064 static char *write_ordering_str[] = {
1065 [WO_none] = "none",
1066 [WO_drain_io] = "drain",
1067 [WO_bdev_flush] = "flush",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001068 };
1069
1070 pwo = mdev->write_ordering;
1071 wo = min(pwo, wo);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001072 if (wo == WO_bdev_flush && mdev->ldev->dc.no_disk_flush)
1073 wo = WO_drain_io;
1074 if (wo == WO_drain_io && mdev->ldev->dc.no_disk_drain)
1075 wo = WO_none;
1076 mdev->write_ordering = wo;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001077 if (pwo != mdev->write_ordering || wo == WO_bdev_flush)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001078 dev_info(DEV, "Method to ensure write ordering: %s\n", write_ordering_str[mdev->write_ordering]);
1079}
1080
1081/**
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001082 * drbd_submit_ee()
1083 * @mdev: DRBD device.
1084 * @e: epoch entry
1085 * @rw: flag field, see bio->bi_rw
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001086 *
1087 * May spread the pages to multiple bios,
1088 * depending on bio_add_page restrictions.
1089 *
1090 * Returns 0 if all bios have been submitted,
1091 * -ENOMEM if we could not allocate enough bios,
1092 * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a
1093 * single page to an empty bio (which should never happen and likely indicates
1094 * that the lower level IO stack is in some way broken). This has been observed
1095 * on certain Xen deployments.
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001096 */
1097/* TODO allocate from our own bio_set. */
1098int drbd_submit_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e,
1099 const unsigned rw, const int fault_type)
1100{
1101 struct bio *bios = NULL;
1102 struct bio *bio;
1103 struct page *page = e->pages;
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001104 sector_t sector = e->i.sector;
1105 unsigned ds = e->i.size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001106 unsigned n_bios = 0;
1107 unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001108 int err = -ENOMEM;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001109
1110 /* In most cases, we will only need one bio. But in case the lower
1111 * level restrictions happen to be different at this offset on this
1112 * side than those of the sending peer, we may need to submit the
1113 * request in more than one bio. */
1114next_bio:
1115 bio = bio_alloc(GFP_NOIO, nr_pages);
1116 if (!bio) {
1117 dev_err(DEV, "submit_ee: Allocation of a bio failed\n");
1118 goto fail;
1119 }
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001120 /* > e->i.sector, unless this is the first bio */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001121 bio->bi_sector = sector;
1122 bio->bi_bdev = mdev->ldev->backing_bdev;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001123 bio->bi_rw = rw;
1124 bio->bi_private = e;
1125 bio->bi_end_io = drbd_endio_sec;
1126
1127 bio->bi_next = bios;
1128 bios = bio;
1129 ++n_bios;
1130
1131 page_chain_for_each(page) {
1132 unsigned len = min_t(unsigned, ds, PAGE_SIZE);
1133 if (!bio_add_page(bio, page, len, 0)) {
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001134 /* A single page must always be possible!
1135 * But in case it fails anyways,
1136 * we deal with it, and complain (below). */
1137 if (bio->bi_vcnt == 0) {
1138 dev_err(DEV,
1139 "bio_add_page failed for len=%u, "
1140 "bi_vcnt=0 (bi_sector=%llu)\n",
1141 len, (unsigned long long)bio->bi_sector);
1142 err = -ENOSPC;
1143 goto fail;
1144 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001145 goto next_bio;
1146 }
1147 ds -= len;
1148 sector += len >> 9;
1149 --nr_pages;
1150 }
1151 D_ASSERT(page == NULL);
1152 D_ASSERT(ds == 0);
1153
1154 atomic_set(&e->pending_bios, n_bios);
1155 do {
1156 bio = bios;
1157 bios = bios->bi_next;
1158 bio->bi_next = NULL;
1159
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001160 drbd_generic_make_request(mdev, fault_type, bio);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001161 } while (bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001162 return 0;
1163
1164fail:
1165 while (bios) {
1166 bio = bios;
1167 bios = bios->bi_next;
1168 bio_put(bio);
1169 }
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001170 return err;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001171}
1172
Philipp Reisner02918be2010-08-20 14:35:10 +02001173static int receive_Barrier(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001174{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001175 int rv;
Philipp Reisnere42325a2011-01-19 13:55:45 +01001176 struct p_barrier *p = &mdev->tconn->data.rbuf.barrier;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001177 struct drbd_epoch *epoch;
1178
Philipp Reisnerb411b362009-09-25 16:07:19 -07001179 inc_unacked(mdev);
1180
Philipp Reisnerb411b362009-09-25 16:07:19 -07001181 mdev->current_epoch->barrier_nr = p->barrier;
1182 rv = drbd_may_finish_epoch(mdev, mdev->current_epoch, EV_GOT_BARRIER_NR);
1183
1184 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
1185 * the activity log, which means it would not be resynced in case the
1186 * R_PRIMARY crashes now.
1187 * Therefore we must send the barrier_ack after the barrier request was
1188 * completed. */
1189 switch (mdev->write_ordering) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001190 case WO_none:
1191 if (rv == FE_RECYCLED)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001192 return true;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001193
1194 /* receiver context, in the writeout path of the other node.
1195 * avoid potential distributed deadlock */
1196 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1197 if (epoch)
1198 break;
1199 else
1200 dev_warn(DEV, "Allocation of an epoch failed, slowing down\n");
1201 /* Fall through */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001202
1203 case WO_bdev_flush:
1204 case WO_drain_io:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001205 drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
Philipp Reisner2451fc32010-08-24 13:43:11 +02001206 drbd_flush(mdev);
1207
1208 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1209 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1210 if (epoch)
1211 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001212 }
1213
Philipp Reisner2451fc32010-08-24 13:43:11 +02001214 epoch = mdev->current_epoch;
1215 wait_event(mdev->ee_wait, atomic_read(&epoch->epoch_size) == 0);
1216
1217 D_ASSERT(atomic_read(&epoch->active) == 0);
1218 D_ASSERT(epoch->flags == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001219
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001220 return true;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001221 default:
1222 dev_err(DEV, "Strangeness in mdev->write_ordering %d\n", mdev->write_ordering);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001223 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001224 }
1225
1226 epoch->flags = 0;
1227 atomic_set(&epoch->epoch_size, 0);
1228 atomic_set(&epoch->active, 0);
1229
1230 spin_lock(&mdev->epoch_lock);
1231 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1232 list_add(&epoch->list, &mdev->current_epoch->list);
1233 mdev->current_epoch = epoch;
1234 mdev->epochs++;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001235 } else {
1236 /* The current_epoch got recycled while we allocated this one... */
1237 kfree(epoch);
1238 }
1239 spin_unlock(&mdev->epoch_lock);
1240
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001241 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001242}
1243
1244/* used from receive_RSDataReply (recv_resync_read)
1245 * and from receive_Data */
1246static struct drbd_epoch_entry *
1247read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector, int data_size) __must_hold(local)
1248{
Lars Ellenberg66660322010-04-06 12:15:04 +02001249 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001250 struct drbd_epoch_entry *e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001251 struct page *page;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001252 int dgs, ds, rr;
Philipp Reisnera0638452011-01-19 14:31:32 +01001253 void *dig_in = mdev->tconn->int_dig_in;
1254 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001255 unsigned long *data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001256
Philipp Reisnera0638452011-01-19 14:31:32 +01001257 dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_r_tfm) ?
1258 crypto_hash_digestsize(mdev->tconn->integrity_r_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001259
1260 if (dgs) {
1261 rr = drbd_recv(mdev, dig_in, dgs);
1262 if (rr != dgs) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01001263 if (!signal_pending(current))
1264 dev_warn(DEV,
1265 "short read receiving data digest: read %d expected %d\n",
1266 rr, dgs);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001267 return NULL;
1268 }
1269 }
1270
1271 data_size -= dgs;
1272
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001273 if (!expect(data_size != 0))
1274 return NULL;
1275 if (!expect(IS_ALIGNED(data_size, 512)))
1276 return NULL;
1277 if (!expect(data_size <= DRBD_MAX_BIO_SIZE))
1278 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001279
Lars Ellenberg66660322010-04-06 12:15:04 +02001280 /* even though we trust out peer,
1281 * we sometimes have to double check. */
1282 if (sector + (data_size>>9) > capacity) {
Lars Ellenbergfdda6542011-01-24 15:11:01 +01001283 dev_err(DEV, "request from peer beyond end of local disk: "
1284 "capacity: %llus < sector: %llus + size: %u\n",
Lars Ellenberg66660322010-04-06 12:15:04 +02001285 (unsigned long long)capacity,
1286 (unsigned long long)sector, data_size);
1287 return NULL;
1288 }
1289
Philipp Reisnerb411b362009-09-25 16:07:19 -07001290 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1291 * "criss-cross" setup, that might cause write-out on some other DRBD,
1292 * which in turn might block on the other node at this very place. */
1293 e = drbd_alloc_ee(mdev, id, sector, data_size, GFP_NOIO);
1294 if (!e)
1295 return NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001296
Philipp Reisnerb411b362009-09-25 16:07:19 -07001297 ds = data_size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001298 page = e->pages;
1299 page_chain_for_each(page) {
1300 unsigned len = min_t(int, ds, PAGE_SIZE);
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001301 data = kmap(page);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001302 rr = drbd_recv(mdev, data, len);
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +01001303 if (drbd_insert_fault(mdev, DRBD_FAULT_RECEIVE)) {
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001304 dev_err(DEV, "Fault injection: Corrupting data on receive\n");
1305 data[0] = data[0] ^ (unsigned long)-1;
1306 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001307 kunmap(page);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001308 if (rr != len) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001309 drbd_free_ee(mdev, e);
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01001310 if (!signal_pending(current))
1311 dev_warn(DEV, "short read receiving data: read %d expected %d\n",
1312 rr, len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001313 return NULL;
1314 }
1315 ds -= rr;
1316 }
1317
1318 if (dgs) {
Philipp Reisnera0638452011-01-19 14:31:32 +01001319 drbd_csum_ee(mdev, mdev->tconn->integrity_r_tfm, e, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001320 if (memcmp(dig_in, dig_vv, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001321 dev_err(DEV, "Digest integrity check FAILED: %llus +%u\n",
1322 (unsigned long long)sector, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001323 drbd_bcast_ee(mdev, "digest failed",
1324 dgs, dig_in, dig_vv, e);
1325 drbd_free_ee(mdev, e);
1326 return NULL;
1327 }
1328 }
1329 mdev->recv_cnt += data_size>>9;
1330 return e;
1331}
1332
1333/* drbd_drain_block() just takes a data block
1334 * out of the socket input buffer, and discards it.
1335 */
1336static int drbd_drain_block(struct drbd_conf *mdev, int data_size)
1337{
1338 struct page *page;
1339 int rr, rv = 1;
1340 void *data;
1341
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001342 if (!data_size)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001343 return true;
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001344
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001345 page = drbd_pp_alloc(mdev, 1, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001346
1347 data = kmap(page);
1348 while (data_size) {
1349 rr = drbd_recv(mdev, data, min_t(int, data_size, PAGE_SIZE));
1350 if (rr != min_t(int, data_size, PAGE_SIZE)) {
1351 rv = 0;
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01001352 if (!signal_pending(current))
1353 dev_warn(DEV,
1354 "short read receiving data: read %d expected %d\n",
1355 rr, min_t(int, data_size, PAGE_SIZE));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001356 break;
1357 }
1358 data_size -= rr;
1359 }
1360 kunmap(page);
Lars Ellenberg435f0742010-09-06 12:30:25 +02001361 drbd_pp_free(mdev, page, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001362 return rv;
1363}
1364
1365static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req,
1366 sector_t sector, int data_size)
1367{
1368 struct bio_vec *bvec;
1369 struct bio *bio;
1370 int dgs, rr, i, expect;
Philipp Reisnera0638452011-01-19 14:31:32 +01001371 void *dig_in = mdev->tconn->int_dig_in;
1372 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001373
Philipp Reisnera0638452011-01-19 14:31:32 +01001374 dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_r_tfm) ?
1375 crypto_hash_digestsize(mdev->tconn->integrity_r_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001376
1377 if (dgs) {
1378 rr = drbd_recv(mdev, dig_in, dgs);
1379 if (rr != dgs) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01001380 if (!signal_pending(current))
1381 dev_warn(DEV,
1382 "short read receiving data reply digest: read %d expected %d\n",
1383 rr, dgs);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001384 return 0;
1385 }
1386 }
1387
1388 data_size -= dgs;
1389
1390 /* optimistically update recv_cnt. if receiving fails below,
1391 * we disconnect anyways, and counters will be reset. */
1392 mdev->recv_cnt += data_size>>9;
1393
1394 bio = req->master_bio;
1395 D_ASSERT(sector == bio->bi_sector);
1396
1397 bio_for_each_segment(bvec, bio, i) {
1398 expect = min_t(int, data_size, bvec->bv_len);
1399 rr = drbd_recv(mdev,
1400 kmap(bvec->bv_page)+bvec->bv_offset,
1401 expect);
1402 kunmap(bvec->bv_page);
1403 if (rr != expect) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01001404 if (!signal_pending(current))
1405 dev_warn(DEV, "short read receiving data reply: "
1406 "read %d expected %d\n",
1407 rr, expect);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001408 return 0;
1409 }
1410 data_size -= rr;
1411 }
1412
1413 if (dgs) {
Philipp Reisnera0638452011-01-19 14:31:32 +01001414 drbd_csum_bio(mdev, mdev->tconn->integrity_r_tfm, bio, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001415 if (memcmp(dig_in, dig_vv, dgs)) {
1416 dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n");
1417 return 0;
1418 }
1419 }
1420
1421 D_ASSERT(data_size == 0);
1422 return 1;
1423}
1424
1425/* e_end_resync_block() is called via
1426 * drbd_process_done_ee() by asender only */
1427static int e_end_resync_block(struct drbd_conf *mdev, struct drbd_work *w, int unused)
1428{
1429 struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w;
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001430 sector_t sector = e->i.sector;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001431 int ok;
1432
Andreas Gruenbacher8b946252011-01-20 15:23:07 +01001433 D_ASSERT(drbd_interval_empty(&e->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001434
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001435 if (likely((e->flags & EE_WAS_ERROR) == 0)) {
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001436 drbd_set_in_sync(mdev, sector, e->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001437 ok = drbd_send_ack(mdev, P_RS_WRITE_ACK, e);
1438 } else {
1439 /* Record failure to sync */
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001440 drbd_rs_failed_io(mdev, sector, e->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001441
1442 ok = drbd_send_ack(mdev, P_NEG_ACK, e);
1443 }
1444 dec_unacked(mdev);
1445
1446 return ok;
1447}
1448
1449static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local)
1450{
1451 struct drbd_epoch_entry *e;
1452
1453 e = read_in_block(mdev, ID_SYNCER, sector, data_size);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001454 if (!e)
1455 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001456
1457 dec_rs_pending(mdev);
1458
Philipp Reisnerb411b362009-09-25 16:07:19 -07001459 inc_unacked(mdev);
1460 /* corresponding dec_unacked() in e_end_resync_block()
1461 * respective _drbd_clear_done_ee */
1462
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001463 e->w.cb = e_end_resync_block;
1464
Philipp Reisner87eeee42011-01-19 14:16:30 +01001465 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001466 list_add(&e->w.list, &mdev->sync_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001467 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001468
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001469 atomic_add(data_size >> 9, &mdev->rs_sect_ev);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001470 if (drbd_submit_ee(mdev, e, WRITE, DRBD_FAULT_RS_WR) == 0)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001471 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001472
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001473 /* don't care for the reason here */
1474 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01001475 spin_lock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001476 list_del(&e->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001477 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001478
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001479 drbd_free_ee(mdev, e);
1480fail:
1481 put_ldev(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001482 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001483}
1484
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001485static struct drbd_request *
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001486find_request(struct drbd_conf *mdev, struct rb_root *root, u64 id,
1487 sector_t sector, bool missing_ok, const char *func)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001488{
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001489 struct drbd_request *req;
1490
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001491 /* Request object according to our peer */
1492 req = (struct drbd_request *)(unsigned long)id;
1493 if (drbd_contains_interval(root, sector, &req->i))
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001494 return req;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001495 if (!missing_ok) {
1496 dev_err(DEV, "%s: failed to find request %lu, sector %llus\n", func,
1497 (unsigned long)id, (unsigned long long)sector);
1498 }
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001499 return NULL;
1500}
1501
Philipp Reisner02918be2010-08-20 14:35:10 +02001502static int receive_DataReply(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001503{
1504 struct drbd_request *req;
1505 sector_t sector;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001506 int ok;
Philipp Reisnere42325a2011-01-19 13:55:45 +01001507 struct p_data *p = &mdev->tconn->data.rbuf.data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001508
1509 sector = be64_to_cpu(p->sector);
1510
Philipp Reisner87eeee42011-01-19 14:16:30 +01001511 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001512 req = find_request(mdev, &mdev->read_requests, p->block_id, sector, false, __func__);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001513 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001514 if (unlikely(!req))
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001515 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001516
Bart Van Assche24c48302011-05-21 18:32:29 +02001517 /* hlist_del(&req->collision) is done in _req_may_be_done, to avoid
Philipp Reisnerb411b362009-09-25 16:07:19 -07001518 * special casing it there for the various failure cases.
1519 * still no race with drbd_fail_pending_reads */
1520 ok = recv_dless_read(mdev, req, sector, data_size);
1521
1522 if (ok)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001523 req_mod(req, DATA_RECEIVED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001524 /* else: nothing. handled from drbd_disconnect...
1525 * I don't think we may complete this just yet
1526 * in case we are "on-disconnect: freeze" */
1527
1528 return ok;
1529}
1530
Philipp Reisner02918be2010-08-20 14:35:10 +02001531static int receive_RSDataReply(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001532{
1533 sector_t sector;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001534 int ok;
Philipp Reisnere42325a2011-01-19 13:55:45 +01001535 struct p_data *p = &mdev->tconn->data.rbuf.data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001536
1537 sector = be64_to_cpu(p->sector);
1538 D_ASSERT(p->block_id == ID_SYNCER);
1539
1540 if (get_ldev(mdev)) {
1541 /* data is submitted to disk within recv_resync_read.
1542 * corresponding put_ldev done below on error,
Andreas Gruenbacher9c508422011-01-14 21:19:36 +01001543 * or in drbd_endio_sec. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001544 ok = recv_resync_read(mdev, sector, data_size);
1545 } else {
1546 if (__ratelimit(&drbd_ratelimit_state))
1547 dev_err(DEV, "Can not write resync data to local disk.\n");
1548
1549 ok = drbd_drain_block(mdev, data_size);
1550
Lars Ellenberg2b2bf212010-10-06 11:46:55 +02001551 drbd_send_ack_dp(mdev, P_NEG_ACK, p, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001552 }
1553
Philipp Reisner778f2712010-07-06 11:14:00 +02001554 atomic_add(data_size >> 9, &mdev->rs_sect_in);
1555
Philipp Reisnerb411b362009-09-25 16:07:19 -07001556 return ok;
1557}
1558
1559/* e_end_block() is called via drbd_process_done_ee().
1560 * this means this function only runs in the asender thread
1561 */
1562static int e_end_block(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1563{
1564 struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w;
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001565 sector_t sector = e->i.sector;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001566 int ok = 1, pcmd;
1567
Philipp Reisner89e58e72011-01-19 13:12:45 +01001568 if (mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001569 if (likely((e->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001570 pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
1571 mdev->state.conn <= C_PAUSED_SYNC_T &&
1572 e->flags & EE_MAY_SET_IN_SYNC) ?
1573 P_RS_WRITE_ACK : P_WRITE_ACK;
1574 ok &= drbd_send_ack(mdev, pcmd, e);
1575 if (pcmd == P_RS_WRITE_ACK)
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001576 drbd_set_in_sync(mdev, sector, e->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001577 } else {
1578 ok = drbd_send_ack(mdev, P_NEG_ACK, e);
1579 /* we expect it to be marked out of sync anyways...
1580 * maybe assert this? */
1581 }
1582 dec_unacked(mdev);
1583 }
1584 /* we delete from the conflict detection hash _after_ we sent out the
1585 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
Philipp Reisner89e58e72011-01-19 13:12:45 +01001586 if (mdev->tconn->net_conf->two_primaries) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01001587 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher8b946252011-01-20 15:23:07 +01001588 D_ASSERT(!drbd_interval_empty(&e->i));
1589 drbd_remove_interval(&mdev->epoch_entries, &e->i);
1590 drbd_clear_interval(&e->i);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001591 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +01001592 } else
Andreas Gruenbacher8b946252011-01-20 15:23:07 +01001593 D_ASSERT(drbd_interval_empty(&e->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001594
1595 drbd_may_finish_epoch(mdev, e->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
1596
1597 return ok;
1598}
1599
1600static int e_send_discard_ack(struct drbd_conf *mdev, struct drbd_work *w, int unused)
1601{
1602 struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w;
1603 int ok = 1;
1604
Philipp Reisner89e58e72011-01-19 13:12:45 +01001605 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001606 ok = drbd_send_ack(mdev, P_DISCARD_ACK, e);
1607
Philipp Reisner87eeee42011-01-19 14:16:30 +01001608 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher8b946252011-01-20 15:23:07 +01001609 D_ASSERT(!drbd_interval_empty(&e->i));
1610 drbd_remove_interval(&mdev->epoch_entries, &e->i);
1611 drbd_clear_interval(&e->i);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001612 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001613
1614 dec_unacked(mdev);
1615
1616 return ok;
1617}
1618
1619/* Called from receive_Data.
1620 * Synchronize packets on sock with packets on msock.
1621 *
1622 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
1623 * packet traveling on msock, they are still processed in the order they have
1624 * been sent.
1625 *
1626 * Note: we don't care for Ack packets overtaking P_DATA packets.
1627 *
1628 * In case packet_seq is larger than mdev->peer_seq number, there are
1629 * outstanding packets on the msock. We wait for them to arrive.
1630 * In case we are the logically next packet, we update mdev->peer_seq
1631 * ourselves. Correctly handles 32bit wrap around.
1632 *
1633 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
1634 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
1635 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
1636 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
1637 *
1638 * returns 0 if we may process the packet,
1639 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
1640static int drbd_wait_peer_seq(struct drbd_conf *mdev, const u32 packet_seq)
1641{
1642 DEFINE_WAIT(wait);
1643 unsigned int p_seq;
1644 long timeout;
1645 int ret = 0;
1646 spin_lock(&mdev->peer_seq_lock);
1647 for (;;) {
1648 prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
1649 if (seq_le(packet_seq, mdev->peer_seq+1))
1650 break;
1651 if (signal_pending(current)) {
1652 ret = -ERESTARTSYS;
1653 break;
1654 }
1655 p_seq = mdev->peer_seq;
1656 spin_unlock(&mdev->peer_seq_lock);
1657 timeout = schedule_timeout(30*HZ);
1658 spin_lock(&mdev->peer_seq_lock);
1659 if (timeout == 0 && p_seq == mdev->peer_seq) {
1660 ret = -ETIMEDOUT;
1661 dev_err(DEV, "ASSERT FAILED waited 30 seconds for sequence update, forcing reconnect\n");
1662 break;
1663 }
1664 }
1665 finish_wait(&mdev->seq_wait, &wait);
1666 if (mdev->peer_seq+1 == packet_seq)
1667 mdev->peer_seq++;
1668 spin_unlock(&mdev->peer_seq_lock);
1669 return ret;
1670}
1671
Lars Ellenberg688593c2010-11-17 22:25:03 +01001672/* see also bio_flags_to_wire()
1673 * DRBD_REQ_*, because we need to semantically map the flags to data packet
1674 * flags and back. We may replicate to other kernel versions. */
1675static unsigned long wire_flags_to_bio(struct drbd_conf *mdev, u32 dpf)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001676{
Lars Ellenberg688593c2010-11-17 22:25:03 +01001677 return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
1678 (dpf & DP_FUA ? REQ_FUA : 0) |
1679 (dpf & DP_FLUSH ? REQ_FLUSH : 0) |
1680 (dpf & DP_DISCARD ? REQ_DISCARD : 0);
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001681}
1682
Philipp Reisnerb411b362009-09-25 16:07:19 -07001683/* mirrored write */
Philipp Reisner02918be2010-08-20 14:35:10 +02001684static int receive_Data(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001685{
1686 sector_t sector;
1687 struct drbd_epoch_entry *e;
Philipp Reisnere42325a2011-01-19 13:55:45 +01001688 struct p_data *p = &mdev->tconn->data.rbuf.data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001689 int rw = WRITE;
1690 u32 dp_flags;
1691
Philipp Reisnerb411b362009-09-25 16:07:19 -07001692 if (!get_ldev(mdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001693 spin_lock(&mdev->peer_seq_lock);
1694 if (mdev->peer_seq+1 == be32_to_cpu(p->seq_num))
1695 mdev->peer_seq++;
1696 spin_unlock(&mdev->peer_seq_lock);
1697
Lars Ellenberg2b2bf212010-10-06 11:46:55 +02001698 drbd_send_ack_dp(mdev, P_NEG_ACK, p, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001699 atomic_inc(&mdev->current_epoch->epoch_size);
1700 return drbd_drain_block(mdev, data_size);
1701 }
1702
1703 /* get_ldev(mdev) successful.
1704 * Corresponding put_ldev done either below (on various errors),
Andreas Gruenbacher9c508422011-01-14 21:19:36 +01001705 * or in drbd_endio_sec, if we successfully submit the data at
Philipp Reisnerb411b362009-09-25 16:07:19 -07001706 * the end of this function. */
1707
1708 sector = be64_to_cpu(p->sector);
1709 e = read_in_block(mdev, p->block_id, sector, data_size);
1710 if (!e) {
1711 put_ldev(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001712 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001713 }
1714
Philipp Reisnerb411b362009-09-25 16:07:19 -07001715 e->w.cb = e_end_block;
1716
Lars Ellenberg688593c2010-11-17 22:25:03 +01001717 dp_flags = be32_to_cpu(p->dp_flags);
1718 rw |= wire_flags_to_bio(mdev, dp_flags);
1719
1720 if (dp_flags & DP_MAY_SET_IN_SYNC)
1721 e->flags |= EE_MAY_SET_IN_SYNC;
1722
Philipp Reisnerb411b362009-09-25 16:07:19 -07001723 spin_lock(&mdev->epoch_lock);
1724 e->epoch = mdev->current_epoch;
1725 atomic_inc(&e->epoch->epoch_size);
1726 atomic_inc(&e->epoch->active);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001727 spin_unlock(&mdev->epoch_lock);
1728
Philipp Reisnerb411b362009-09-25 16:07:19 -07001729 /* I'm the receiver, I do hold a net_cnt reference. */
Philipp Reisner89e58e72011-01-19 13:12:45 +01001730 if (!mdev->tconn->net_conf->two_primaries) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01001731 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001732 } else {
1733 /* don't get the req_lock yet,
1734 * we may sleep in drbd_wait_peer_seq */
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001735 const int size = e->i.size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001736 const int discard = test_bit(DISCARD_CONCURRENT, &mdev->flags);
1737 DEFINE_WAIT(wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001738 int first;
1739
Philipp Reisner89e58e72011-01-19 13:12:45 +01001740 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001741
1742 /* conflict detection and handling:
1743 * 1. wait on the sequence number,
1744 * in case this data packet overtook ACK packets.
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +01001745 * 2. check our interval trees for conflicting requests:
1746 * we only need to check the write_requests tree; the
1747 * epoch_entries tree cannot contain any overlaps because
1748 * they were already eliminated on the submitting node.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001749 *
1750 * Note: for two_primaries, we are protocol C,
1751 * so there cannot be any request that is DONE
1752 * but still on the transfer log.
1753 *
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +01001754 * unconditionally add to the epoch_entries tree.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001755 *
1756 * if no conflicting request is found:
1757 * submit.
1758 *
1759 * if any conflicting request is found
1760 * that has not yet been acked,
1761 * AND I have the "discard concurrent writes" flag:
1762 * queue (via done_ee) the P_DISCARD_ACK; OUT.
1763 *
1764 * if any conflicting request is found:
1765 * block the receiver, waiting on misc_wait
1766 * until no more conflicting requests are there,
1767 * or we get interrupted (disconnect).
1768 *
1769 * we do not just write after local io completion of those
1770 * requests, but only after req is done completely, i.e.
1771 * we wait for the P_DISCARD_ACK to arrive!
1772 *
1773 * then proceed normally, i.e. submit.
1774 */
1775 if (drbd_wait_peer_seq(mdev, be32_to_cpu(p->seq_num)))
1776 goto out_interrupted;
1777
Philipp Reisner87eeee42011-01-19 14:16:30 +01001778 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001779
Andreas Gruenbacher8b946252011-01-20 15:23:07 +01001780 drbd_insert_interval(&mdev->epoch_entries, &e->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001781
Philipp Reisnerb411b362009-09-25 16:07:19 -07001782 first = 1;
1783 for (;;) {
Andreas Gruenbacherde696712011-01-20 15:00:24 +01001784 struct drbd_interval *i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001785 int have_unacked = 0;
1786 int have_conflict = 0;
1787 prepare_to_wait(&mdev->misc_wait, &wait,
1788 TASK_INTERRUPTIBLE);
Andreas Gruenbacherde696712011-01-20 15:00:24 +01001789
1790 i = drbd_find_overlap(&mdev->write_requests, sector, size);
1791 if (i) {
1792 struct drbd_request *req2 =
1793 container_of(i, struct drbd_request, i);
1794
1795 /* only ALERT on first iteration,
1796 * we may be woken up early... */
1797 if (first)
1798 dev_alert(DEV, "%s[%u] Concurrent local write detected!"
1799 " new: %llus +%u; pending: %llus +%u\n",
1800 current->comm, current->pid,
1801 (unsigned long long)sector, size,
1802 (unsigned long long)req2->i.sector, req2->i.size);
1803 if (req2->rq_state & RQ_NET_PENDING)
1804 ++have_unacked;
1805 ++have_conflict;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001806 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001807 if (!have_conflict)
1808 break;
1809
1810 /* Discard Ack only for the _first_ iteration */
1811 if (first && discard && have_unacked) {
1812 dev_alert(DEV, "Concurrent write! [DISCARD BY FLAG] sec=%llus\n",
1813 (unsigned long long)sector);
1814 inc_unacked(mdev);
1815 e->w.cb = e_send_discard_ack;
1816 list_add_tail(&e->w.list, &mdev->done_ee);
1817
Philipp Reisner87eeee42011-01-19 14:16:30 +01001818 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001819
1820 /* we could probably send that P_DISCARD_ACK ourselves,
1821 * but I don't like the receiver using the msock */
1822
1823 put_ldev(mdev);
1824 wake_asender(mdev);
1825 finish_wait(&mdev->misc_wait, &wait);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001826 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001827 }
1828
1829 if (signal_pending(current)) {
Andreas Gruenbacher8b946252011-01-20 15:23:07 +01001830 drbd_remove_interval(&mdev->epoch_entries, &e->i);
1831 drbd_clear_interval(&e->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001832
Philipp Reisner87eeee42011-01-19 14:16:30 +01001833 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001834
1835 finish_wait(&mdev->misc_wait, &wait);
1836 goto out_interrupted;
1837 }
1838
Philipp Reisner87eeee42011-01-19 14:16:30 +01001839 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001840 if (first) {
1841 first = 0;
1842 dev_alert(DEV, "Concurrent write! [W AFTERWARDS] "
1843 "sec=%llus\n", (unsigned long long)sector);
1844 } else if (discard) {
1845 /* we had none on the first iteration.
1846 * there must be none now. */
1847 D_ASSERT(have_unacked == 0);
1848 }
1849 schedule();
Philipp Reisner87eeee42011-01-19 14:16:30 +01001850 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001851 }
1852 finish_wait(&mdev->misc_wait, &wait);
1853 }
1854
1855 list_add(&e->w.list, &mdev->active_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001856 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001857
Philipp Reisner89e58e72011-01-19 13:12:45 +01001858 switch (mdev->tconn->net_conf->wire_protocol) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001859 case DRBD_PROT_C:
1860 inc_unacked(mdev);
1861 /* corresponding dec_unacked() in e_end_block()
1862 * respective _drbd_clear_done_ee */
1863 break;
1864 case DRBD_PROT_B:
1865 /* I really don't like it that the receiver thread
1866 * sends on the msock, but anyways */
1867 drbd_send_ack(mdev, P_RECV_ACK, e);
1868 break;
1869 case DRBD_PROT_A:
1870 /* nothing to do */
1871 break;
1872 }
1873
Lars Ellenberg6719fb02010-10-18 23:04:07 +02001874 if (mdev->state.pdsk < D_INCONSISTENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001875 /* In case we have the only disk of the cluster, */
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001876 drbd_set_out_of_sync(mdev, e->i.sector, e->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001877 e->flags |= EE_CALL_AL_COMPLETE_IO;
Lars Ellenberg6719fb02010-10-18 23:04:07 +02001878 e->flags &= ~EE_MAY_SET_IN_SYNC;
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001879 drbd_al_begin_io(mdev, e->i.sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001880 }
1881
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001882 if (drbd_submit_ee(mdev, e, rw, DRBD_FAULT_DT_WR) == 0)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001883 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001884
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001885 /* don't care for the reason here */
1886 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01001887 spin_lock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001888 list_del(&e->w.list);
Andreas Gruenbacher8b946252011-01-20 15:23:07 +01001889 drbd_remove_interval(&mdev->epoch_entries, &e->i);
1890 drbd_clear_interval(&e->i);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001891 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001892 if (e->flags & EE_CALL_AL_COMPLETE_IO)
Andreas Gruenbacher010f6e62011-01-14 20:59:35 +01001893 drbd_al_complete_io(mdev, e->i.sector);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001894
Philipp Reisnerb411b362009-09-25 16:07:19 -07001895out_interrupted:
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001896 drbd_may_finish_epoch(mdev, e->epoch, EV_PUT + EV_CLEANUP);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001897 put_ldev(mdev);
1898 drbd_free_ee(mdev, e);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001899 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001900}
1901
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001902/* We may throttle resync, if the lower device seems to be busy,
1903 * and current sync rate is above c_min_rate.
1904 *
1905 * To decide whether or not the lower device is busy, we use a scheme similar
1906 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
1907 * (more than 64 sectors) of activity we cannot account for with our own resync
1908 * activity, it obviously is "busy".
1909 *
1910 * The current sync rate used here uses only the most recent two step marks,
1911 * to have a short time average so we can react faster.
1912 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01001913int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001914{
1915 struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
1916 unsigned long db, dt, dbdt;
Philipp Reisnere3555d82010-11-07 15:56:29 +01001917 struct lc_element *tmp;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001918 int curr_events;
1919 int throttle = 0;
1920
1921 /* feature disabled? */
1922 if (mdev->sync_conf.c_min_rate == 0)
1923 return 0;
1924
Philipp Reisnere3555d82010-11-07 15:56:29 +01001925 spin_lock_irq(&mdev->al_lock);
1926 tmp = lc_find(mdev->resync, BM_SECT_TO_EXT(sector));
1927 if (tmp) {
1928 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
1929 if (test_bit(BME_PRIORITY, &bm_ext->flags)) {
1930 spin_unlock_irq(&mdev->al_lock);
1931 return 0;
1932 }
1933 /* Do not slow down if app IO is already waiting for this extent */
1934 }
1935 spin_unlock_irq(&mdev->al_lock);
1936
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001937 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
1938 (int)part_stat_read(&disk->part0, sectors[1]) -
1939 atomic_read(&mdev->rs_sect_ev);
Philipp Reisnere3555d82010-11-07 15:56:29 +01001940
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001941 if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
1942 unsigned long rs_left;
1943 int i;
1944
1945 mdev->rs_last_events = curr_events;
1946
1947 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
1948 * approx. */
Lars Ellenberg2649f082010-11-05 10:05:47 +01001949 i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
1950
1951 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
1952 rs_left = mdev->ov_left;
1953 else
1954 rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001955
1956 dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
1957 if (!dt)
1958 dt++;
1959 db = mdev->rs_mark_left[i] - rs_left;
1960 dbdt = Bit2KB(db/dt);
1961
1962 if (dbdt > mdev->sync_conf.c_min_rate)
1963 throttle = 1;
1964 }
1965 return throttle;
1966}
1967
1968
Philipp Reisner02918be2010-08-20 14:35:10 +02001969static int receive_DataRequest(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int digest_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001970{
1971 sector_t sector;
1972 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
1973 struct drbd_epoch_entry *e;
1974 struct digest_info *di = NULL;
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02001975 int size, verb;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001976 unsigned int fault_type;
Philipp Reisnere42325a2011-01-19 13:55:45 +01001977 struct p_block_req *p = &mdev->tconn->data.rbuf.block_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001978
1979 sector = be64_to_cpu(p->sector);
1980 size = be32_to_cpu(p->blksize);
1981
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001982 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001983 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
1984 (unsigned long long)sector, size);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001985 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001986 }
1987 if (sector + (size>>9) > capacity) {
1988 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
1989 (unsigned long long)sector, size);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001990 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001991 }
1992
1993 if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02001994 verb = 1;
1995 switch (cmd) {
1996 case P_DATA_REQUEST:
1997 drbd_send_ack_rp(mdev, P_NEG_DREPLY, p);
1998 break;
1999 case P_RS_DATA_REQUEST:
2000 case P_CSUM_RS_REQUEST:
2001 case P_OV_REQUEST:
2002 drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p);
2003 break;
2004 case P_OV_REPLY:
2005 verb = 0;
2006 dec_rs_pending(mdev);
2007 drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC);
2008 break;
2009 default:
2010 dev_err(DEV, "unexpected command (%s) in receive_DataRequest\n",
2011 cmdname(cmd));
2012 }
2013 if (verb && __ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002014 dev_err(DEV, "Can not satisfy peer's read request, "
2015 "no local data.\n");
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002016
Lars Ellenberga821cc42010-09-06 12:31:37 +02002017 /* drain possibly payload */
2018 return drbd_drain_block(mdev, digest_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002019 }
2020
2021 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
2022 * "criss-cross" setup, that might cause write-out on some other DRBD,
2023 * which in turn might block on the other node at this very place. */
2024 e = drbd_alloc_ee(mdev, p->block_id, sector, size, GFP_NOIO);
2025 if (!e) {
2026 put_ldev(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002027 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002028 }
2029
Philipp Reisner02918be2010-08-20 14:35:10 +02002030 switch (cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002031 case P_DATA_REQUEST:
2032 e->w.cb = w_e_end_data_req;
2033 fault_type = DRBD_FAULT_DT_RD;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002034 /* application IO, don't drbd_rs_begin_io */
2035 goto submit;
2036
Philipp Reisnerb411b362009-09-25 16:07:19 -07002037 case P_RS_DATA_REQUEST:
2038 e->w.cb = w_e_end_rsdata_req;
2039 fault_type = DRBD_FAULT_RS_RD;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002040 /* used in the sector offset progress display */
2041 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002042 break;
2043
2044 case P_OV_REPLY:
2045 case P_CSUM_RS_REQUEST:
2046 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002047 di = kmalloc(sizeof(*di) + digest_size, GFP_NOIO);
2048 if (!di)
2049 goto out_free_e;
2050
2051 di->digest_size = digest_size;
2052 di->digest = (((char *)di)+sizeof(struct digest_info));
2053
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +02002054 e->digest = di;
2055 e->flags |= EE_HAS_DIGEST;
2056
Philipp Reisnerb411b362009-09-25 16:07:19 -07002057 if (drbd_recv(mdev, di->digest, digest_size) != digest_size)
2058 goto out_free_e;
2059
Philipp Reisner02918be2010-08-20 14:35:10 +02002060 if (cmd == P_CSUM_RS_REQUEST) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002061 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002062 e->w.cb = w_e_end_csum_rs_req;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002063 /* used in the sector offset progress display */
2064 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisner02918be2010-08-20 14:35:10 +02002065 } else if (cmd == P_OV_REPLY) {
Lars Ellenberg2649f082010-11-05 10:05:47 +01002066 /* track progress, we may need to throttle */
2067 atomic_add(size >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002068 e->w.cb = w_e_end_ov_reply;
2069 dec_rs_pending(mdev);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002070 /* drbd_rs_begin_io done when we sent this request,
2071 * but accounting still needs to be done. */
2072 goto submit_for_resync;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002073 }
2074 break;
2075
2076 case P_OV_REQUEST:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002077 if (mdev->ov_start_sector == ~(sector_t)0 &&
Philipp Reisner31890f42011-01-19 14:12:51 +01002078 mdev->tconn->agreed_pro_version >= 90) {
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002079 unsigned long now = jiffies;
2080 int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002081 mdev->ov_start_sector = sector;
2082 mdev->ov_position = sector;
Lars Ellenberg30b743a2010-11-05 09:39:06 +01002083 mdev->ov_left = drbd_bm_bits(mdev) - BM_SECT_TO_BIT(sector);
2084 mdev->rs_total = mdev->ov_left;
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002085 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
2086 mdev->rs_mark_left[i] = mdev->ov_left;
2087 mdev->rs_mark_time[i] = now;
2088 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002089 dev_info(DEV, "Online Verify start sector: %llu\n",
2090 (unsigned long long)sector);
2091 }
2092 e->w.cb = w_e_end_ov_req;
2093 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002094 break;
2095
Philipp Reisnerb411b362009-09-25 16:07:19 -07002096 default:
2097 dev_err(DEV, "unexpected command (%s) in receive_DataRequest\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02002098 cmdname(cmd));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002099 fault_type = DRBD_FAULT_MAX;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002100 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002101 }
2102
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002103 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2104 * wrt the receiver, but it is not as straightforward as it may seem.
2105 * Various places in the resync start and stop logic assume resync
2106 * requests are processed in order, requeuing this on the worker thread
2107 * introduces a bunch of new code for synchronization between threads.
2108 *
2109 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2110 * "forever", throttling after drbd_rs_begin_io will lock that extent
2111 * for application writes for the same time. For now, just throttle
2112 * here, where the rest of the code expects the receiver to sleep for
2113 * a while, anyways.
2114 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002115
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002116 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2117 * this defers syncer requests for some time, before letting at least
2118 * on request through. The resync controller on the receiving side
2119 * will adapt to the incoming rate accordingly.
2120 *
2121 * We cannot throttle here if remote is Primary/SyncTarget:
2122 * we would also throttle its application reads.
2123 * In that case, throttling is done on the SyncTarget only.
2124 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002125 if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev, sector))
2126 schedule_timeout_uninterruptible(HZ/10);
2127 if (drbd_rs_begin_io(mdev, sector))
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002128 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002129
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002130submit_for_resync:
2131 atomic_add(size >> 9, &mdev->rs_sect_ev);
2132
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002133submit:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002134 inc_unacked(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002135 spin_lock_irq(&mdev->tconn->req_lock);
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002136 list_add_tail(&e->w.list, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002137 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002138
Lars Ellenberg45bb9122010-05-14 17:10:48 +02002139 if (drbd_submit_ee(mdev, e, READ, fault_type) == 0)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002140 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002141
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002142 /* don't care for the reason here */
2143 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002144 spin_lock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002145 list_del(&e->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002146 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002147 /* no drbd_rs_complete_io(), we are dropping the connection anyways */
2148
Philipp Reisnerb411b362009-09-25 16:07:19 -07002149out_free_e:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002150 put_ldev(mdev);
2151 drbd_free_ee(mdev, e);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002152 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002153}
2154
2155static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
2156{
2157 int self, peer, rv = -100;
2158 unsigned long ch_self, ch_peer;
2159
2160 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2161 peer = mdev->p_uuid[UI_BITMAP] & 1;
2162
2163 ch_peer = mdev->p_uuid[UI_SIZE];
2164 ch_self = mdev->comm_bm_set;
2165
Philipp Reisner89e58e72011-01-19 13:12:45 +01002166 switch (mdev->tconn->net_conf->after_sb_0p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002167 case ASB_CONSENSUS:
2168 case ASB_DISCARD_SECONDARY:
2169 case ASB_CALL_HELPER:
2170 dev_err(DEV, "Configuration error.\n");
2171 break;
2172 case ASB_DISCONNECT:
2173 break;
2174 case ASB_DISCARD_YOUNGER_PRI:
2175 if (self == 0 && peer == 1) {
2176 rv = -1;
2177 break;
2178 }
2179 if (self == 1 && peer == 0) {
2180 rv = 1;
2181 break;
2182 }
2183 /* Else fall through to one of the other strategies... */
2184 case ASB_DISCARD_OLDER_PRI:
2185 if (self == 0 && peer == 1) {
2186 rv = 1;
2187 break;
2188 }
2189 if (self == 1 && peer == 0) {
2190 rv = -1;
2191 break;
2192 }
2193 /* Else fall through to one of the other strategies... */
Lars Ellenbergad19bf62009-10-14 09:36:49 +02002194 dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
Philipp Reisnerb411b362009-09-25 16:07:19 -07002195 "Using discard-least-changes instead\n");
2196 case ASB_DISCARD_ZERO_CHG:
2197 if (ch_peer == 0 && ch_self == 0) {
2198 rv = test_bit(DISCARD_CONCURRENT, &mdev->flags)
2199 ? -1 : 1;
2200 break;
2201 } else {
2202 if (ch_peer == 0) { rv = 1; break; }
2203 if (ch_self == 0) { rv = -1; break; }
2204 }
Philipp Reisner89e58e72011-01-19 13:12:45 +01002205 if (mdev->tconn->net_conf->after_sb_0p == ASB_DISCARD_ZERO_CHG)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002206 break;
2207 case ASB_DISCARD_LEAST_CHG:
2208 if (ch_self < ch_peer)
2209 rv = -1;
2210 else if (ch_self > ch_peer)
2211 rv = 1;
2212 else /* ( ch_self == ch_peer ) */
2213 /* Well, then use something else. */
2214 rv = test_bit(DISCARD_CONCURRENT, &mdev->flags)
2215 ? -1 : 1;
2216 break;
2217 case ASB_DISCARD_LOCAL:
2218 rv = -1;
2219 break;
2220 case ASB_DISCARD_REMOTE:
2221 rv = 1;
2222 }
2223
2224 return rv;
2225}
2226
2227static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
2228{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002229 int hg, rv = -100;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002230
Philipp Reisner89e58e72011-01-19 13:12:45 +01002231 switch (mdev->tconn->net_conf->after_sb_1p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002232 case ASB_DISCARD_YOUNGER_PRI:
2233 case ASB_DISCARD_OLDER_PRI:
2234 case ASB_DISCARD_LEAST_CHG:
2235 case ASB_DISCARD_LOCAL:
2236 case ASB_DISCARD_REMOTE:
2237 dev_err(DEV, "Configuration error.\n");
2238 break;
2239 case ASB_DISCONNECT:
2240 break;
2241 case ASB_CONSENSUS:
2242 hg = drbd_asb_recover_0p(mdev);
2243 if (hg == -1 && mdev->state.role == R_SECONDARY)
2244 rv = hg;
2245 if (hg == 1 && mdev->state.role == R_PRIMARY)
2246 rv = hg;
2247 break;
2248 case ASB_VIOLENTLY:
2249 rv = drbd_asb_recover_0p(mdev);
2250 break;
2251 case ASB_DISCARD_SECONDARY:
2252 return mdev->state.role == R_PRIMARY ? 1 : -1;
2253 case ASB_CALL_HELPER:
2254 hg = drbd_asb_recover_0p(mdev);
2255 if (hg == -1 && mdev->state.role == R_PRIMARY) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002256 enum drbd_state_rv rv2;
2257
2258 drbd_set_role(mdev, R_SECONDARY, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002259 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2260 * we might be here in C_WF_REPORT_PARAMS which is transient.
2261 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002262 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2263 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002264 drbd_khelper(mdev, "pri-lost-after-sb");
2265 } else {
2266 dev_warn(DEV, "Successfully gave up primary role.\n");
2267 rv = hg;
2268 }
2269 } else
2270 rv = hg;
2271 }
2272
2273 return rv;
2274}
2275
2276static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
2277{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002278 int hg, rv = -100;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002279
Philipp Reisner89e58e72011-01-19 13:12:45 +01002280 switch (mdev->tconn->net_conf->after_sb_2p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002281 case ASB_DISCARD_YOUNGER_PRI:
2282 case ASB_DISCARD_OLDER_PRI:
2283 case ASB_DISCARD_LEAST_CHG:
2284 case ASB_DISCARD_LOCAL:
2285 case ASB_DISCARD_REMOTE:
2286 case ASB_CONSENSUS:
2287 case ASB_DISCARD_SECONDARY:
2288 dev_err(DEV, "Configuration error.\n");
2289 break;
2290 case ASB_VIOLENTLY:
2291 rv = drbd_asb_recover_0p(mdev);
2292 break;
2293 case ASB_DISCONNECT:
2294 break;
2295 case ASB_CALL_HELPER:
2296 hg = drbd_asb_recover_0p(mdev);
2297 if (hg == -1) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002298 enum drbd_state_rv rv2;
2299
Philipp Reisnerb411b362009-09-25 16:07:19 -07002300 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2301 * we might be here in C_WF_REPORT_PARAMS which is transient.
2302 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002303 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2304 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002305 drbd_khelper(mdev, "pri-lost-after-sb");
2306 } else {
2307 dev_warn(DEV, "Successfully gave up primary role.\n");
2308 rv = hg;
2309 }
2310 } else
2311 rv = hg;
2312 }
2313
2314 return rv;
2315}
2316
2317static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
2318 u64 bits, u64 flags)
2319{
2320 if (!uuid) {
2321 dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
2322 return;
2323 }
2324 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
2325 text,
2326 (unsigned long long)uuid[UI_CURRENT],
2327 (unsigned long long)uuid[UI_BITMAP],
2328 (unsigned long long)uuid[UI_HISTORY_START],
2329 (unsigned long long)uuid[UI_HISTORY_END],
2330 (unsigned long long)bits,
2331 (unsigned long long)flags);
2332}
2333
2334/*
2335 100 after split brain try auto recover
2336 2 C_SYNC_SOURCE set BitMap
2337 1 C_SYNC_SOURCE use BitMap
2338 0 no Sync
2339 -1 C_SYNC_TARGET use BitMap
2340 -2 C_SYNC_TARGET set BitMap
2341 -100 after split brain, disconnect
2342-1000 unrelated data
Philipp Reisner4a23f262011-01-11 17:42:17 +01002343-1091 requires proto 91
2344-1096 requires proto 96
Philipp Reisnerb411b362009-09-25 16:07:19 -07002345 */
2346static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
2347{
2348 u64 self, peer;
2349 int i, j;
2350
2351 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2352 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2353
2354 *rule_nr = 10;
2355 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
2356 return 0;
2357
2358 *rule_nr = 20;
2359 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
2360 peer != UUID_JUST_CREATED)
2361 return -2;
2362
2363 *rule_nr = 30;
2364 if (self != UUID_JUST_CREATED &&
2365 (peer == UUID_JUST_CREATED || peer == (u64)0))
2366 return 2;
2367
2368 if (self == peer) {
2369 int rct, dc; /* roles at crash time */
2370
2371 if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
2372
Philipp Reisner31890f42011-01-19 14:12:51 +01002373 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002374 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002375
2376 if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
2377 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
2378 dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
2379 drbd_uuid_set_bm(mdev, 0UL);
2380
2381 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2382 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2383 *rule_nr = 34;
2384 } else {
2385 dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
2386 *rule_nr = 36;
2387 }
2388
2389 return 1;
2390 }
2391
2392 if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
2393
Philipp Reisner31890f42011-01-19 14:12:51 +01002394 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002395 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002396
2397 if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
2398 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
2399 dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
2400
2401 mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
2402 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
2403 mdev->p_uuid[UI_BITMAP] = 0UL;
2404
2405 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2406 *rule_nr = 35;
2407 } else {
2408 dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
2409 *rule_nr = 37;
2410 }
2411
2412 return -1;
2413 }
2414
2415 /* Common power [off|failure] */
2416 rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
2417 (mdev->p_uuid[UI_FLAGS] & 2);
2418 /* lowest bit is set when we were primary,
2419 * next bit (weight 2) is set when peer was primary */
2420 *rule_nr = 40;
2421
2422 switch (rct) {
2423 case 0: /* !self_pri && !peer_pri */ return 0;
2424 case 1: /* self_pri && !peer_pri */ return 1;
2425 case 2: /* !self_pri && peer_pri */ return -1;
2426 case 3: /* self_pri && peer_pri */
2427 dc = test_bit(DISCARD_CONCURRENT, &mdev->flags);
2428 return dc ? -1 : 1;
2429 }
2430 }
2431
2432 *rule_nr = 50;
2433 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2434 if (self == peer)
2435 return -1;
2436
2437 *rule_nr = 51;
2438 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2439 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002440 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002441 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) ==
2442 (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) :
2443 peer + UUID_NEW_BM_OFFSET == (mdev->p_uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002444 /* The last P_SYNC_UUID did not get though. Undo the last start of
2445 resync as sync source modifications of the peer's UUIDs. */
2446
Philipp Reisner31890f42011-01-19 14:12:51 +01002447 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002448 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002449
2450 mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
2451 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
Philipp Reisner4a23f262011-01-11 17:42:17 +01002452
2453 dev_info(DEV, "Did not got last syncUUID packet, corrected:\n");
2454 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2455
Philipp Reisnerb411b362009-09-25 16:07:19 -07002456 return -1;
2457 }
2458 }
2459
2460 *rule_nr = 60;
2461 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2462 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2463 peer = mdev->p_uuid[i] & ~((u64)1);
2464 if (self == peer)
2465 return -2;
2466 }
2467
2468 *rule_nr = 70;
2469 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2470 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2471 if (self == peer)
2472 return 1;
2473
2474 *rule_nr = 71;
2475 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2476 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002477 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002478 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) ==
2479 (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) :
2480 self + UUID_NEW_BM_OFFSET == (mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002481 /* The last P_SYNC_UUID did not get though. Undo the last start of
2482 resync as sync source modifications of our UUIDs. */
2483
Philipp Reisner31890f42011-01-19 14:12:51 +01002484 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002485 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002486
2487 _drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
2488 _drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
2489
Philipp Reisner4a23f262011-01-11 17:42:17 +01002490 dev_info(DEV, "Last syncUUID did not get through, corrected:\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002491 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2492 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2493
2494 return 1;
2495 }
2496 }
2497
2498
2499 *rule_nr = 80;
Philipp Reisnerd8c2a362009-11-18 15:52:51 +01002500 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002501 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2502 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2503 if (self == peer)
2504 return 2;
2505 }
2506
2507 *rule_nr = 90;
2508 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2509 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2510 if (self == peer && self != ((u64)0))
2511 return 100;
2512
2513 *rule_nr = 100;
2514 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2515 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2516 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
2517 peer = mdev->p_uuid[j] & ~((u64)1);
2518 if (self == peer)
2519 return -100;
2520 }
2521 }
2522
2523 return -1000;
2524}
2525
2526/* drbd_sync_handshake() returns the new conn state on success, or
2527 CONN_MASK (-1) on failure.
2528 */
2529static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
2530 enum drbd_disk_state peer_disk) __must_hold(local)
2531{
2532 int hg, rule_nr;
2533 enum drbd_conns rv = C_MASK;
2534 enum drbd_disk_state mydisk;
2535
2536 mydisk = mdev->state.disk;
2537 if (mydisk == D_NEGOTIATING)
2538 mydisk = mdev->new_state_tmp.disk;
2539
2540 dev_info(DEV, "drbd_sync_handshake:\n");
2541 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0);
2542 drbd_uuid_dump(mdev, "peer", mdev->p_uuid,
2543 mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2544
2545 hg = drbd_uuid_compare(mdev, &rule_nr);
2546
2547 dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
2548
2549 if (hg == -1000) {
2550 dev_alert(DEV, "Unrelated data, aborting!\n");
2551 return C_MASK;
2552 }
Philipp Reisner4a23f262011-01-11 17:42:17 +01002553 if (hg < -1000) {
2554 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 -07002555 return C_MASK;
2556 }
2557
2558 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
2559 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
2560 int f = (hg == -100) || abs(hg) == 2;
2561 hg = mydisk > D_INCONSISTENT ? 1 : -1;
2562 if (f)
2563 hg = hg*2;
2564 dev_info(DEV, "Becoming sync %s due to disk states.\n",
2565 hg > 0 ? "source" : "target");
2566 }
2567
Adam Gandelman3a11a482010-04-08 16:48:23 -07002568 if (abs(hg) == 100)
2569 drbd_khelper(mdev, "initial-split-brain");
2570
Philipp Reisner89e58e72011-01-19 13:12:45 +01002571 if (hg == 100 || (hg == -100 && mdev->tconn->net_conf->always_asbp)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002572 int pcount = (mdev->state.role == R_PRIMARY)
2573 + (peer_role == R_PRIMARY);
2574 int forced = (hg == -100);
2575
2576 switch (pcount) {
2577 case 0:
2578 hg = drbd_asb_recover_0p(mdev);
2579 break;
2580 case 1:
2581 hg = drbd_asb_recover_1p(mdev);
2582 break;
2583 case 2:
2584 hg = drbd_asb_recover_2p(mdev);
2585 break;
2586 }
2587 if (abs(hg) < 100) {
2588 dev_warn(DEV, "Split-Brain detected, %d primaries, "
2589 "automatically solved. Sync from %s node\n",
2590 pcount, (hg < 0) ? "peer" : "this");
2591 if (forced) {
2592 dev_warn(DEV, "Doing a full sync, since"
2593 " UUIDs where ambiguous.\n");
2594 hg = hg*2;
2595 }
2596 }
2597 }
2598
2599 if (hg == -100) {
Philipp Reisner89e58e72011-01-19 13:12:45 +01002600 if (mdev->tconn->net_conf->want_lose && !(mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002601 hg = -1;
Philipp Reisner89e58e72011-01-19 13:12:45 +01002602 if (!mdev->tconn->net_conf->want_lose && (mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002603 hg = 1;
2604
2605 if (abs(hg) < 100)
2606 dev_warn(DEV, "Split-Brain detected, manually solved. "
2607 "Sync from %s node\n",
2608 (hg < 0) ? "peer" : "this");
2609 }
2610
2611 if (hg == -100) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01002612 /* FIXME this log message is not correct if we end up here
2613 * after an attempted attach on a diskless node.
2614 * We just refuse to attach -- well, we drop the "connection"
2615 * to that disk, in a way... */
Adam Gandelman3a11a482010-04-08 16:48:23 -07002616 dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002617 drbd_khelper(mdev, "split-brain");
2618 return C_MASK;
2619 }
2620
2621 if (hg > 0 && mydisk <= D_INCONSISTENT) {
2622 dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
2623 return C_MASK;
2624 }
2625
2626 if (hg < 0 && /* by intention we do not use mydisk here. */
2627 mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
Philipp Reisner89e58e72011-01-19 13:12:45 +01002628 switch (mdev->tconn->net_conf->rr_conflict) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002629 case ASB_CALL_HELPER:
2630 drbd_khelper(mdev, "pri-lost");
2631 /* fall through */
2632 case ASB_DISCONNECT:
2633 dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
2634 return C_MASK;
2635 case ASB_VIOLENTLY:
2636 dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
2637 "assumption\n");
2638 }
2639 }
2640
Philipp Reisner89e58e72011-01-19 13:12:45 +01002641 if (mdev->tconn->net_conf->dry_run || test_bit(CONN_DRY_RUN, &mdev->flags)) {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002642 if (hg == 0)
2643 dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
2644 else
2645 dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
2646 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
2647 abs(hg) >= 2 ? "full" : "bit-map based");
2648 return C_MASK;
2649 }
2650
Philipp Reisnerb411b362009-09-25 16:07:19 -07002651 if (abs(hg) >= 2) {
2652 dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002653 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake",
2654 BM_LOCKED_SET_ALLOWED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002655 return C_MASK;
2656 }
2657
2658 if (hg > 0) { /* become sync source. */
2659 rv = C_WF_BITMAP_S;
2660 } else if (hg < 0) { /* become sync target */
2661 rv = C_WF_BITMAP_T;
2662 } else {
2663 rv = C_CONNECTED;
2664 if (drbd_bm_total_weight(mdev)) {
2665 dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
2666 drbd_bm_total_weight(mdev));
2667 }
2668 }
2669
2670 return rv;
2671}
2672
2673/* returns 1 if invalid */
2674static int cmp_after_sb(enum drbd_after_sb_p peer, enum drbd_after_sb_p self)
2675{
2676 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
2677 if ((peer == ASB_DISCARD_REMOTE && self == ASB_DISCARD_LOCAL) ||
2678 (self == ASB_DISCARD_REMOTE && peer == ASB_DISCARD_LOCAL))
2679 return 0;
2680
2681 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
2682 if (peer == ASB_DISCARD_REMOTE || peer == ASB_DISCARD_LOCAL ||
2683 self == ASB_DISCARD_REMOTE || self == ASB_DISCARD_LOCAL)
2684 return 1;
2685
2686 /* everything else is valid if they are equal on both sides. */
2687 if (peer == self)
2688 return 0;
2689
2690 /* everything es is invalid. */
2691 return 1;
2692}
2693
Philipp Reisner02918be2010-08-20 14:35:10 +02002694static int receive_protocol(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002695{
Philipp Reisnere42325a2011-01-19 13:55:45 +01002696 struct p_protocol *p = &mdev->tconn->data.rbuf.protocol;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002697 int p_proto, p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002698 int p_want_lose, p_two_primaries, cf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002699 char p_integrity_alg[SHARED_SECRET_MAX] = "";
2700
Philipp Reisnerb411b362009-09-25 16:07:19 -07002701 p_proto = be32_to_cpu(p->protocol);
2702 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
2703 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
2704 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002705 p_two_primaries = be32_to_cpu(p->two_primaries);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002706 cf = be32_to_cpu(p->conn_flags);
2707 p_want_lose = cf & CF_WANT_LOSE;
2708
2709 clear_bit(CONN_DRY_RUN, &mdev->flags);
2710
2711 if (cf & CF_DRY_RUN)
2712 set_bit(CONN_DRY_RUN, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002713
Philipp Reisner89e58e72011-01-19 13:12:45 +01002714 if (p_proto != mdev->tconn->net_conf->wire_protocol) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002715 dev_err(DEV, "incompatible communication protocols\n");
2716 goto disconnect;
2717 }
2718
Philipp Reisner89e58e72011-01-19 13:12:45 +01002719 if (cmp_after_sb(p_after_sb_0p, mdev->tconn->net_conf->after_sb_0p)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002720 dev_err(DEV, "incompatible after-sb-0pri settings\n");
2721 goto disconnect;
2722 }
2723
Philipp Reisner89e58e72011-01-19 13:12:45 +01002724 if (cmp_after_sb(p_after_sb_1p, mdev->tconn->net_conf->after_sb_1p)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002725 dev_err(DEV, "incompatible after-sb-1pri settings\n");
2726 goto disconnect;
2727 }
2728
Philipp Reisner89e58e72011-01-19 13:12:45 +01002729 if (cmp_after_sb(p_after_sb_2p, mdev->tconn->net_conf->after_sb_2p)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002730 dev_err(DEV, "incompatible after-sb-2pri settings\n");
2731 goto disconnect;
2732 }
2733
Philipp Reisner89e58e72011-01-19 13:12:45 +01002734 if (p_want_lose && mdev->tconn->net_conf->want_lose) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002735 dev_err(DEV, "both sides have the 'want_lose' flag set\n");
2736 goto disconnect;
2737 }
2738
Philipp Reisner89e58e72011-01-19 13:12:45 +01002739 if (p_two_primaries != mdev->tconn->net_conf->two_primaries) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002740 dev_err(DEV, "incompatible setting of the two-primaries options\n");
2741 goto disconnect;
2742 }
2743
Philipp Reisner31890f42011-01-19 14:12:51 +01002744 if (mdev->tconn->agreed_pro_version >= 87) {
Philipp Reisner89e58e72011-01-19 13:12:45 +01002745 unsigned char *my_alg = mdev->tconn->net_conf->integrity_alg;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002746
2747 if (drbd_recv(mdev, p_integrity_alg, data_size) != data_size)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002748 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002749
2750 p_integrity_alg[SHARED_SECRET_MAX-1] = 0;
2751 if (strcmp(p_integrity_alg, my_alg)) {
2752 dev_err(DEV, "incompatible setting of the data-integrity-alg\n");
2753 goto disconnect;
2754 }
2755 dev_info(DEV, "data-integrity-alg: %s\n",
2756 my_alg[0] ? my_alg : (unsigned char *)"<not-used>");
2757 }
2758
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002759 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002760
2761disconnect:
2762 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002763 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002764}
2765
2766/* helper function
2767 * input: alg name, feature name
2768 * return: NULL (alg name was "")
2769 * ERR_PTR(error) if something goes wrong
2770 * or the crypto hash ptr, if it worked out ok. */
2771struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
2772 const char *alg, const char *name)
2773{
2774 struct crypto_hash *tfm;
2775
2776 if (!alg[0])
2777 return NULL;
2778
2779 tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
2780 if (IS_ERR(tfm)) {
2781 dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
2782 alg, name, PTR_ERR(tfm));
2783 return tfm;
2784 }
2785 if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) {
2786 crypto_free_hash(tfm);
2787 dev_err(DEV, "\"%s\" is not a digest (%s)\n", alg, name);
2788 return ERR_PTR(-EINVAL);
2789 }
2790 return tfm;
2791}
2792
Philipp Reisner02918be2010-08-20 14:35:10 +02002793static int receive_SyncParam(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int packet_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002794{
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002795 int ok = true;
Philipp Reisnere42325a2011-01-19 13:55:45 +01002796 struct p_rs_param_95 *p = &mdev->tconn->data.rbuf.rs_param_95;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002797 unsigned int header_size, data_size, exp_max_sz;
2798 struct crypto_hash *verify_tfm = NULL;
2799 struct crypto_hash *csums_tfm = NULL;
Philipp Reisner31890f42011-01-19 14:12:51 +01002800 const int apv = mdev->tconn->agreed_pro_version;
Philipp Reisner778f2712010-07-06 11:14:00 +02002801 int *rs_plan_s = NULL;
2802 int fifo_size = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002803
2804 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
2805 : apv == 88 ? sizeof(struct p_rs_param)
2806 + SHARED_SECRET_MAX
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002807 : apv <= 94 ? sizeof(struct p_rs_param_89)
2808 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002809
Philipp Reisner02918be2010-08-20 14:35:10 +02002810 if (packet_size > exp_max_sz) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002811 dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02002812 packet_size, exp_max_sz);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002813 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002814 }
2815
2816 if (apv <= 88) {
Philipp Reisner257d0af2011-01-26 12:15:29 +01002817 header_size = sizeof(struct p_rs_param) - sizeof(struct p_header);
Philipp Reisner02918be2010-08-20 14:35:10 +02002818 data_size = packet_size - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002819 } else if (apv <= 94) {
Philipp Reisner257d0af2011-01-26 12:15:29 +01002820 header_size = sizeof(struct p_rs_param_89) - sizeof(struct p_header);
Philipp Reisner02918be2010-08-20 14:35:10 +02002821 data_size = packet_size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002822 D_ASSERT(data_size == 0);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002823 } else {
Philipp Reisner257d0af2011-01-26 12:15:29 +01002824 header_size = sizeof(struct p_rs_param_95) - sizeof(struct p_header);
Philipp Reisner02918be2010-08-20 14:35:10 +02002825 data_size = packet_size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002826 D_ASSERT(data_size == 0);
2827 }
2828
2829 /* initialize verify_alg and csums_alg */
2830 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
2831
Philipp Reisner02918be2010-08-20 14:35:10 +02002832 if (drbd_recv(mdev, &p->head.payload, header_size) != header_size)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002833 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002834
2835 mdev->sync_conf.rate = be32_to_cpu(p->rate);
2836
2837 if (apv >= 88) {
2838 if (apv == 88) {
2839 if (data_size > SHARED_SECRET_MAX) {
2840 dev_err(DEV, "verify-alg too long, "
2841 "peer wants %u, accepting only %u byte\n",
2842 data_size, SHARED_SECRET_MAX);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002843 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002844 }
2845
2846 if (drbd_recv(mdev, p->verify_alg, data_size) != data_size)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002847 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002848
2849 /* we expect NUL terminated string */
2850 /* but just in case someone tries to be evil */
2851 D_ASSERT(p->verify_alg[data_size-1] == 0);
2852 p->verify_alg[data_size-1] = 0;
2853
2854 } else /* apv >= 89 */ {
2855 /* we still expect NUL terminated strings */
2856 /* but just in case someone tries to be evil */
2857 D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
2858 D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
2859 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
2860 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
2861 }
2862
2863 if (strcmp(mdev->sync_conf.verify_alg, p->verify_alg)) {
2864 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
2865 dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
2866 mdev->sync_conf.verify_alg, p->verify_alg);
2867 goto disconnect;
2868 }
2869 verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
2870 p->verify_alg, "verify-alg");
2871 if (IS_ERR(verify_tfm)) {
2872 verify_tfm = NULL;
2873 goto disconnect;
2874 }
2875 }
2876
2877 if (apv >= 89 && strcmp(mdev->sync_conf.csums_alg, p->csums_alg)) {
2878 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
2879 dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
2880 mdev->sync_conf.csums_alg, p->csums_alg);
2881 goto disconnect;
2882 }
2883 csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
2884 p->csums_alg, "csums-alg");
2885 if (IS_ERR(csums_tfm)) {
2886 csums_tfm = NULL;
2887 goto disconnect;
2888 }
2889 }
2890
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002891 if (apv > 94) {
2892 mdev->sync_conf.rate = be32_to_cpu(p->rate);
2893 mdev->sync_conf.c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
2894 mdev->sync_conf.c_delay_target = be32_to_cpu(p->c_delay_target);
2895 mdev->sync_conf.c_fill_target = be32_to_cpu(p->c_fill_target);
2896 mdev->sync_conf.c_max_rate = be32_to_cpu(p->c_max_rate);
Philipp Reisner778f2712010-07-06 11:14:00 +02002897
2898 fifo_size = (mdev->sync_conf.c_plan_ahead * 10 * SLEEP_TIME) / HZ;
2899 if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) {
2900 rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL);
2901 if (!rs_plan_s) {
2902 dev_err(DEV, "kmalloc of fifo_buffer failed");
2903 goto disconnect;
2904 }
2905 }
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002906 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002907
2908 spin_lock(&mdev->peer_seq_lock);
2909 /* lock against drbd_nl_syncer_conf() */
2910 if (verify_tfm) {
2911 strcpy(mdev->sync_conf.verify_alg, p->verify_alg);
2912 mdev->sync_conf.verify_alg_len = strlen(p->verify_alg) + 1;
2913 crypto_free_hash(mdev->verify_tfm);
2914 mdev->verify_tfm = verify_tfm;
2915 dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
2916 }
2917 if (csums_tfm) {
2918 strcpy(mdev->sync_conf.csums_alg, p->csums_alg);
2919 mdev->sync_conf.csums_alg_len = strlen(p->csums_alg) + 1;
2920 crypto_free_hash(mdev->csums_tfm);
2921 mdev->csums_tfm = csums_tfm;
2922 dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
2923 }
Philipp Reisner778f2712010-07-06 11:14:00 +02002924 if (fifo_size != mdev->rs_plan_s.size) {
2925 kfree(mdev->rs_plan_s.values);
2926 mdev->rs_plan_s.values = rs_plan_s;
2927 mdev->rs_plan_s.size = fifo_size;
2928 mdev->rs_planed = 0;
2929 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002930 spin_unlock(&mdev->peer_seq_lock);
2931 }
2932
2933 return ok;
2934disconnect:
2935 /* just for completeness: actually not needed,
2936 * as this is not reached if csums_tfm was ok. */
2937 crypto_free_hash(csums_tfm);
2938 /* but free the verify_tfm again, if csums_tfm did not work out */
2939 crypto_free_hash(verify_tfm);
2940 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002941 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002942}
2943
Philipp Reisnerb411b362009-09-25 16:07:19 -07002944/* warn if the arguments differ by more than 12.5% */
2945static void warn_if_differ_considerably(struct drbd_conf *mdev,
2946 const char *s, sector_t a, sector_t b)
2947{
2948 sector_t d;
2949 if (a == 0 || b == 0)
2950 return;
2951 d = (a > b) ? (a - b) : (b - a);
2952 if (d > (a>>3) || d > (b>>3))
2953 dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
2954 (unsigned long long)a, (unsigned long long)b);
2955}
2956
Philipp Reisner02918be2010-08-20 14:35:10 +02002957static int receive_sizes(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002958{
Philipp Reisnere42325a2011-01-19 13:55:45 +01002959 struct p_sizes *p = &mdev->tconn->data.rbuf.sizes;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002960 enum determine_dev_size dd = unchanged;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002961 sector_t p_size, p_usize, my_usize;
2962 int ldsc = 0; /* local disk size changed */
Philipp Reisnere89b5912010-03-24 17:11:33 +01002963 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002964
Philipp Reisnerb411b362009-09-25 16:07:19 -07002965 p_size = be64_to_cpu(p->d_size);
2966 p_usize = be64_to_cpu(p->u_size);
2967
2968 if (p_size == 0 && mdev->state.disk == D_DISKLESS) {
2969 dev_err(DEV, "some backing storage is needed\n");
2970 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002971 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002972 }
2973
2974 /* just store the peer's disk size for now.
2975 * we still need to figure out whether we accept that. */
2976 mdev->p_size = p_size;
2977
Philipp Reisnerb411b362009-09-25 16:07:19 -07002978 if (get_ldev(mdev)) {
2979 warn_if_differ_considerably(mdev, "lower level device sizes",
2980 p_size, drbd_get_max_capacity(mdev->ldev));
2981 warn_if_differ_considerably(mdev, "user requested size",
2982 p_usize, mdev->ldev->dc.disk_size);
2983
2984 /* if this is the first connect, or an otherwise expected
2985 * param exchange, choose the minimum */
2986 if (mdev->state.conn == C_WF_REPORT_PARAMS)
2987 p_usize = min_not_zero((sector_t)mdev->ldev->dc.disk_size,
2988 p_usize);
2989
2990 my_usize = mdev->ldev->dc.disk_size;
2991
2992 if (mdev->ldev->dc.disk_size != p_usize) {
2993 mdev->ldev->dc.disk_size = p_usize;
2994 dev_info(DEV, "Peer sets u_size to %lu sectors\n",
2995 (unsigned long)mdev->ldev->dc.disk_size);
2996 }
2997
2998 /* Never shrink a device with usable data during connect.
2999 But allow online shrinking if we are connected. */
Philipp Reisnera393db62009-12-22 13:35:52 +01003000 if (drbd_new_dev_size(mdev, mdev->ldev, 0) <
Philipp Reisnerb411b362009-09-25 16:07:19 -07003001 drbd_get_capacity(mdev->this_bdev) &&
3002 mdev->state.disk >= D_OUTDATED &&
3003 mdev->state.conn < C_CONNECTED) {
3004 dev_err(DEV, "The peer's disk size is too small!\n");
3005 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
3006 mdev->ldev->dc.disk_size = my_usize;
3007 put_ldev(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003008 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003009 }
3010 put_ldev(mdev);
3011 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003012
Philipp Reisnere89b5912010-03-24 17:11:33 +01003013 ddsf = be16_to_cpu(p->dds_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003014 if (get_ldev(mdev)) {
Bart Van Assche24c48302011-05-21 18:32:29 +02003015 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003016 put_ldev(mdev);
3017 if (dd == dev_size_error)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003018 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003019 drbd_md_sync(mdev);
3020 } else {
3021 /* I am diskless, need to accept the peer's size. */
3022 drbd_set_my_capacity(mdev, p_size);
3023 }
3024
Philipp Reisner99432fc2011-05-20 16:39:13 +02003025 mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size);
3026 drbd_reconsider_max_bio_size(mdev);
3027
Philipp Reisnerb411b362009-09-25 16:07:19 -07003028 if (get_ldev(mdev)) {
3029 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
3030 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
3031 ldsc = 1;
3032 }
3033
Philipp Reisnerb411b362009-09-25 16:07:19 -07003034 put_ldev(mdev);
3035 }
3036
3037 if (mdev->state.conn > C_WF_REPORT_PARAMS) {
3038 if (be64_to_cpu(p->c_size) !=
3039 drbd_get_capacity(mdev->this_bdev) || ldsc) {
3040 /* we have different sizes, probably peer
3041 * needs to know my new size... */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003042 drbd_send_sizes(mdev, 0, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003043 }
3044 if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
3045 (dd == grew && mdev->state.conn == C_CONNECTED)) {
3046 if (mdev->state.pdsk >= D_INCONSISTENT &&
Philipp Reisnere89b5912010-03-24 17:11:33 +01003047 mdev->state.disk >= D_INCONSISTENT) {
3048 if (ddsf & DDSF_NO_RESYNC)
3049 dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
3050 else
3051 resync_after_online_grow(mdev);
3052 } else
Philipp Reisnerb411b362009-09-25 16:07:19 -07003053 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
3054 }
3055 }
3056
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003057 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003058}
3059
Philipp Reisner02918be2010-08-20 14:35:10 +02003060static int receive_uuids(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003061{
Philipp Reisnere42325a2011-01-19 13:55:45 +01003062 struct p_uuids *p = &mdev->tconn->data.rbuf.uuids;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003063 u64 *p_uuid;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003064 int i, updated_uuids = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003065
Philipp Reisnerb411b362009-09-25 16:07:19 -07003066 p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
3067
3068 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
3069 p_uuid[i] = be64_to_cpu(p->uuid[i]);
3070
3071 kfree(mdev->p_uuid);
3072 mdev->p_uuid = p_uuid;
3073
3074 if (mdev->state.conn < C_CONNECTED &&
3075 mdev->state.disk < D_INCONSISTENT &&
3076 mdev->state.role == R_PRIMARY &&
3077 (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
3078 dev_err(DEV, "Can only connect to data with current UUID=%016llX\n",
3079 (unsigned long long)mdev->ed_uuid);
3080 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003081 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003082 }
3083
3084 if (get_ldev(mdev)) {
3085 int skip_initial_sync =
3086 mdev->state.conn == C_CONNECTED &&
Philipp Reisner31890f42011-01-19 14:12:51 +01003087 mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003088 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
3089 (p_uuid[UI_FLAGS] & 8);
3090 if (skip_initial_sync) {
3091 dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n");
3092 drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003093 "clear_n_write from receive_uuids",
3094 BM_LOCKED_TEST_ALLOWED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003095 _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]);
3096 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3097 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3098 CS_VERBOSE, NULL);
3099 drbd_md_sync(mdev);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003100 updated_uuids = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003101 }
3102 put_ldev(mdev);
Philipp Reisner18a50fa2010-06-21 14:14:15 +02003103 } else if (mdev->state.disk < D_INCONSISTENT &&
3104 mdev->state.role == R_PRIMARY) {
3105 /* I am a diskless primary, the peer just created a new current UUID
3106 for me. */
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003107 updated_uuids = drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003108 }
3109
3110 /* Before we test for the disk state, we should wait until an eventually
3111 ongoing cluster wide state change is finished. That is important if
3112 we are primary and are detaching from our disk. We need to see the
3113 new disk state... */
3114 wait_event(mdev->misc_wait, !test_bit(CLUSTER_ST_CHANGE, &mdev->flags));
3115 if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT)
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003116 updated_uuids |= drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
3117
3118 if (updated_uuids)
3119 drbd_print_uuids(mdev, "receiver updated UUIDs to");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003120
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003121 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003122}
3123
3124/**
3125 * convert_state() - Converts the peer's view of the cluster state to our point of view
3126 * @ps: The state as seen by the peer.
3127 */
3128static union drbd_state convert_state(union drbd_state ps)
3129{
3130 union drbd_state ms;
3131
3132 static enum drbd_conns c_tab[] = {
3133 [C_CONNECTED] = C_CONNECTED,
3134
3135 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
3136 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
3137 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
3138 [C_VERIFY_S] = C_VERIFY_T,
3139 [C_MASK] = C_MASK,
3140 };
3141
3142 ms.i = ps.i;
3143
3144 ms.conn = c_tab[ps.conn];
3145 ms.peer = ps.role;
3146 ms.role = ps.peer;
3147 ms.pdsk = ps.disk;
3148 ms.disk = ps.pdsk;
3149 ms.peer_isp = (ps.aftr_isp | ps.user_isp);
3150
3151 return ms;
3152}
3153
Philipp Reisner02918be2010-08-20 14:35:10 +02003154static int receive_req_state(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003155{
Philipp Reisnere42325a2011-01-19 13:55:45 +01003156 struct p_req_state *p = &mdev->tconn->data.rbuf.req_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003157 union drbd_state mask, val;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01003158 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003159
Philipp Reisnerb411b362009-09-25 16:07:19 -07003160 mask.i = be32_to_cpu(p->mask);
3161 val.i = be32_to_cpu(p->val);
3162
3163 if (test_bit(DISCARD_CONCURRENT, &mdev->flags) &&
3164 test_bit(CLUSTER_ST_CHANGE, &mdev->flags)) {
3165 drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003166 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003167 }
3168
3169 mask = convert_state(mask);
3170 val = convert_state(val);
3171
3172 rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
3173
3174 drbd_send_sr_reply(mdev, rv);
3175 drbd_md_sync(mdev);
3176
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003177 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003178}
3179
Philipp Reisner02918be2010-08-20 14:35:10 +02003180static int receive_state(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003181{
Philipp Reisnere42325a2011-01-19 13:55:45 +01003182 struct p_state *p = &mdev->tconn->data.rbuf.state;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003183 union drbd_state os, ns, peer_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003184 enum drbd_disk_state real_peer_disk;
Philipp Reisner65d922c2010-06-16 16:18:09 +02003185 enum chg_state_flags cs_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003186 int rv;
3187
Philipp Reisnerb411b362009-09-25 16:07:19 -07003188 peer_state.i = be32_to_cpu(p->state);
3189
3190 real_peer_disk = peer_state.disk;
3191 if (peer_state.disk == D_NEGOTIATING) {
3192 real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
3193 dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
3194 }
3195
Philipp Reisner87eeee42011-01-19 14:16:30 +01003196 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003197 retry:
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003198 os = ns = mdev->state;
Philipp Reisner87eeee42011-01-19 14:16:30 +01003199 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003200
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003201 /* peer says his disk is uptodate, while we think it is inconsistent,
3202 * and this happens while we think we have a sync going on. */
3203 if (os.pdsk == D_INCONSISTENT && real_peer_disk == D_UP_TO_DATE &&
3204 os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) {
3205 /* If we are (becoming) SyncSource, but peer is still in sync
3206 * preparation, ignore its uptodate-ness to avoid flapping, it
3207 * will change to inconsistent once the peer reaches active
3208 * syncing states.
3209 * It may have changed syncer-paused flags, however, so we
3210 * cannot ignore this completely. */
3211 if (peer_state.conn > C_CONNECTED &&
3212 peer_state.conn < C_SYNC_SOURCE)
3213 real_peer_disk = D_INCONSISTENT;
3214
3215 /* if peer_state changes to connected at the same time,
3216 * it explicitly notifies us that it finished resync.
3217 * Maybe we should finish it up, too? */
3218 else if (os.conn >= C_SYNC_SOURCE &&
3219 peer_state.conn == C_CONNECTED) {
3220 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed)
3221 drbd_resync_finished(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003222 return true;
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003223 }
3224 }
3225
3226 /* peer says his disk is inconsistent, while we think it is uptodate,
3227 * and this happens while the peer still thinks we have a sync going on,
3228 * but we think we are already done with the sync.
3229 * We ignore this to avoid flapping pdsk.
3230 * This should not happen, if the peer is a recent version of drbd. */
3231 if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT &&
3232 os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE)
3233 real_peer_disk = D_UP_TO_DATE;
3234
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003235 if (ns.conn == C_WF_REPORT_PARAMS)
3236 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003237
Philipp Reisner67531712010-10-27 12:21:30 +02003238 if (peer_state.conn == C_AHEAD)
3239 ns.conn = C_BEHIND;
3240
Philipp Reisnerb411b362009-09-25 16:07:19 -07003241 if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING &&
3242 get_ldev_if_state(mdev, D_NEGOTIATING)) {
3243 int cr; /* consider resync */
3244
3245 /* if we established a new connection */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003246 cr = (os.conn < C_CONNECTED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003247 /* if we had an established connection
3248 * and one of the nodes newly attaches a disk */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003249 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003250 (peer_state.disk == D_NEGOTIATING ||
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003251 os.disk == D_NEGOTIATING));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003252 /* if we have both been inconsistent, and the peer has been
3253 * forced to be UpToDate with --overwrite-data */
3254 cr |= test_bit(CONSIDER_RESYNC, &mdev->flags);
3255 /* if we had been plain connected, and the admin requested to
3256 * start a sync by "invalidate" or "invalidate-remote" */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003257 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003258 (peer_state.conn >= C_STARTING_SYNC_S &&
3259 peer_state.conn <= C_WF_BITMAP_T));
3260
3261 if (cr)
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003262 ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003263
3264 put_ldev(mdev);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003265 if (ns.conn == C_MASK) {
3266 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003267 if (mdev->state.disk == D_NEGOTIATING) {
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003268 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003269 } else if (peer_state.disk == D_NEGOTIATING) {
3270 dev_err(DEV, "Disk attach process on the peer node was aborted.\n");
3271 peer_state.disk = D_DISKLESS;
Lars Ellenberg580b9762010-02-26 23:15:23 +01003272 real_peer_disk = D_DISKLESS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003273 } else {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003274 if (test_and_clear_bit(CONN_DRY_RUN, &mdev->flags))
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003275 return false;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003276 D_ASSERT(os.conn == C_WF_REPORT_PARAMS);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003277 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003278 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003279 }
3280 }
3281 }
3282
Philipp Reisner87eeee42011-01-19 14:16:30 +01003283 spin_lock_irq(&mdev->tconn->req_lock);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003284 if (mdev->state.i != os.i)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003285 goto retry;
3286 clear_bit(CONSIDER_RESYNC, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003287 ns.peer = peer_state.role;
3288 ns.pdsk = real_peer_disk;
3289 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003290 if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003291 ns.disk = mdev->new_state_tmp.disk;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003292 cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD);
3293 if (ns.pdsk == D_CONSISTENT && is_susp(ns) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED &&
Philipp Reisner481c6f52010-06-22 14:03:27 +02003294 test_bit(NEW_CUR_UUID, &mdev->flags)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01003295 /* Do not allow tl_restart(RESEND) for a rebooted peer. We can only allow this
Philipp Reisner481c6f52010-06-22 14:03:27 +02003296 for temporal network outages! */
Philipp Reisner87eeee42011-01-19 14:16:30 +01003297 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003298 dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
3299 tl_clear(mdev);
3300 drbd_uuid_new_current(mdev);
3301 clear_bit(NEW_CUR_UUID, &mdev->flags);
3302 drbd_force_state(mdev, NS2(conn, C_PROTOCOL_ERROR, susp, 0));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003303 return false;
Philipp Reisner481c6f52010-06-22 14:03:27 +02003304 }
Philipp Reisner65d922c2010-06-16 16:18:09 +02003305 rv = _drbd_set_state(mdev, ns, cs_flags, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003306 ns = mdev->state;
Philipp Reisner87eeee42011-01-19 14:16:30 +01003307 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003308
3309 if (rv < SS_SUCCESS) {
3310 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003311 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003312 }
3313
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003314 if (os.conn > C_WF_REPORT_PARAMS) {
3315 if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003316 peer_state.disk != D_NEGOTIATING ) {
3317 /* we want resync, peer has not yet decided to sync... */
3318 /* Nowadays only used when forcing a node into primary role and
3319 setting its disk to UpToDate with that */
3320 drbd_send_uuids(mdev);
3321 drbd_send_state(mdev);
3322 }
3323 }
3324
Philipp Reisner89e58e72011-01-19 13:12:45 +01003325 mdev->tconn->net_conf->want_lose = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003326
3327 drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
3328
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003329 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003330}
3331
Philipp Reisner02918be2010-08-20 14:35:10 +02003332static int receive_sync_uuid(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003333{
Philipp Reisnere42325a2011-01-19 13:55:45 +01003334 struct p_rs_uuid *p = &mdev->tconn->data.rbuf.rs_uuid;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003335
3336 wait_event(mdev->misc_wait,
3337 mdev->state.conn == C_WF_SYNC_UUID ||
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02003338 mdev->state.conn == C_BEHIND ||
Philipp Reisnerb411b362009-09-25 16:07:19 -07003339 mdev->state.conn < C_CONNECTED ||
3340 mdev->state.disk < D_NEGOTIATING);
3341
3342 /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */
3343
Philipp Reisnerb411b362009-09-25 16:07:19 -07003344 /* Here the _drbd_uuid_ functions are right, current should
3345 _not_ be rotated into the history */
3346 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
3347 _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid));
3348 _drbd_uuid_set(mdev, UI_BITMAP, 0UL);
3349
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003350 drbd_print_uuids(mdev, "updated sync uuid");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003351 drbd_start_resync(mdev, C_SYNC_TARGET);
3352
3353 put_ldev(mdev);
3354 } else
3355 dev_err(DEV, "Ignoring SyncUUID packet!\n");
3356
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003357 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003358}
3359
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003360/**
3361 * receive_bitmap_plain
3362 *
3363 * Return 0 when done, 1 when another iteration is needed, and a negative error
3364 * code upon failure.
3365 */
3366static int
Philipp Reisner02918be2010-08-20 14:35:10 +02003367receive_bitmap_plain(struct drbd_conf *mdev, unsigned int data_size,
3368 unsigned long *buffer, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003369{
3370 unsigned num_words = min_t(size_t, BM_PACKET_WORDS, c->bm_words - c->word_offset);
3371 unsigned want = num_words * sizeof(long);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003372 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003373
Philipp Reisner02918be2010-08-20 14:35:10 +02003374 if (want != data_size) {
3375 dev_err(DEV, "%s:want (%u) != data_size (%u)\n", __func__, want, data_size);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003376 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003377 }
3378 if (want == 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003379 return 0;
3380 err = drbd_recv(mdev, buffer, want);
3381 if (err != want) {
3382 if (err >= 0)
3383 err = -EIO;
3384 return err;
3385 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003386
3387 drbd_bm_merge_lel(mdev, c->word_offset, num_words, buffer);
3388
3389 c->word_offset += num_words;
3390 c->bit_offset = c->word_offset * BITS_PER_LONG;
3391 if (c->bit_offset > c->bm_bits)
3392 c->bit_offset = c->bm_bits;
3393
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003394 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003395}
3396
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003397/**
3398 * recv_bm_rle_bits
3399 *
3400 * Return 0 when done, 1 when another iteration is needed, and a negative error
3401 * code upon failure.
3402 */
3403static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07003404recv_bm_rle_bits(struct drbd_conf *mdev,
3405 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003406 struct bm_xfer_ctx *c,
3407 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003408{
3409 struct bitstream bs;
3410 u64 look_ahead;
3411 u64 rl;
3412 u64 tmp;
3413 unsigned long s = c->bit_offset;
3414 unsigned long e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003415 int toggle = DCBP_get_start(p);
3416 int have;
3417 int bits;
3418
3419 bitstream_init(&bs, p->code, len, DCBP_get_pad_bits(p));
3420
3421 bits = bitstream_get_bits(&bs, &look_ahead, 64);
3422 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003423 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003424
3425 for (have = bits; have > 0; s += rl, toggle = !toggle) {
3426 bits = vli_decode_bits(&rl, look_ahead);
3427 if (bits <= 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003428 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003429
3430 if (toggle) {
3431 e = s + rl -1;
3432 if (e >= c->bm_bits) {
3433 dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003434 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003435 }
3436 _drbd_bm_set_bits(mdev, s, e);
3437 }
3438
3439 if (have < bits) {
3440 dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
3441 have, bits, look_ahead,
3442 (unsigned int)(bs.cur.b - p->code),
3443 (unsigned int)bs.buf_len);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003444 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003445 }
3446 look_ahead >>= bits;
3447 have -= bits;
3448
3449 bits = bitstream_get_bits(&bs, &tmp, 64 - have);
3450 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003451 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003452 look_ahead |= tmp << have;
3453 have += bits;
3454 }
3455
3456 c->bit_offset = s;
3457 bm_xfer_ctx_bit_to_word_offset(c);
3458
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003459 return (s != c->bm_bits);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003460}
3461
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003462/**
3463 * decode_bitmap_c
3464 *
3465 * Return 0 when done, 1 when another iteration is needed, and a negative error
3466 * code upon failure.
3467 */
3468static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07003469decode_bitmap_c(struct drbd_conf *mdev,
3470 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003471 struct bm_xfer_ctx *c,
3472 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003473{
3474 if (DCBP_get_code(p) == RLE_VLI_Bits)
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003475 return recv_bm_rle_bits(mdev, p, c, len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003476
3477 /* other variants had been implemented for evaluation,
3478 * but have been dropped as this one turned out to be "best"
3479 * during all our tests. */
3480
3481 dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
3482 drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR));
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003483 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003484}
3485
3486void INFO_bm_xfer_stats(struct drbd_conf *mdev,
3487 const char *direction, struct bm_xfer_ctx *c)
3488{
3489 /* what would it take to transfer it "plaintext" */
Philipp Reisnerc0129492011-01-19 16:58:16 +01003490 unsigned plain = sizeof(struct p_header) *
Philipp Reisnerb411b362009-09-25 16:07:19 -07003491 ((c->bm_words+BM_PACKET_WORDS-1)/BM_PACKET_WORDS+1)
3492 + c->bm_words * sizeof(long);
3493 unsigned total = c->bytes[0] + c->bytes[1];
3494 unsigned r;
3495
3496 /* total can not be zero. but just in case: */
3497 if (total == 0)
3498 return;
3499
3500 /* don't report if not compressed */
3501 if (total >= plain)
3502 return;
3503
3504 /* total < plain. check for overflow, still */
3505 r = (total > UINT_MAX/1000) ? (total / (plain/1000))
3506 : (1000 * total / plain);
3507
3508 if (r > 1000)
3509 r = 1000;
3510
3511 r = 1000 - r;
3512 dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
3513 "total %u; compression: %u.%u%%\n",
3514 direction,
3515 c->bytes[1], c->packets[1],
3516 c->bytes[0], c->packets[0],
3517 total, r/10, r % 10);
3518}
3519
3520/* Since we are processing the bitfield from lower addresses to higher,
3521 it does not matter if the process it in 32 bit chunks or 64 bit
3522 chunks as long as it is little endian. (Understand it as byte stream,
3523 beginning with the lowest byte...) If we would use big endian
3524 we would need to process it from the highest address to the lowest,
3525 in order to be agnostic to the 32 vs 64 bits issue.
3526
3527 returns 0 on failure, 1 if we successfully received it. */
Philipp Reisner02918be2010-08-20 14:35:10 +02003528static int receive_bitmap(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003529{
3530 struct bm_xfer_ctx c;
3531 void *buffer;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003532 int err;
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003533 int ok = false;
Philipp Reisner257d0af2011-01-26 12:15:29 +01003534 struct p_header *h = &mdev->tconn->data.rbuf.header;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003535
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003536 drbd_bm_lock(mdev, "receive bitmap", BM_LOCKED_SET_ALLOWED);
3537 /* you are supposed to send additional out-of-sync information
3538 * if you actually set bits during this phase */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003539
3540 /* maybe we should use some per thread scratch page,
3541 * and allocate that during initial device creation? */
3542 buffer = (unsigned long *) __get_free_page(GFP_NOIO);
3543 if (!buffer) {
3544 dev_err(DEV, "failed to allocate one page buffer in %s\n", __func__);
3545 goto out;
3546 }
3547
3548 c = (struct bm_xfer_ctx) {
3549 .bm_bits = drbd_bm_bits(mdev),
3550 .bm_words = drbd_bm_words(mdev),
3551 };
3552
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003553 for(;;) {
Philipp Reisner02918be2010-08-20 14:35:10 +02003554 if (cmd == P_BITMAP) {
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003555 err = receive_bitmap_plain(mdev, data_size, buffer, &c);
Philipp Reisner02918be2010-08-20 14:35:10 +02003556 } else if (cmd == P_COMPRESSED_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003557 /* MAYBE: sanity check that we speak proto >= 90,
3558 * and the feature is enabled! */
3559 struct p_compressed_bm *p;
3560
Philipp Reisner02918be2010-08-20 14:35:10 +02003561 if (data_size > BM_PACKET_PAYLOAD_BYTES) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003562 dev_err(DEV, "ReportCBitmap packet too large\n");
3563 goto out;
3564 }
3565 /* use the page buff */
3566 p = buffer;
3567 memcpy(p, h, sizeof(*h));
Philipp Reisner02918be2010-08-20 14:35:10 +02003568 if (drbd_recv(mdev, p->head.payload, data_size) != data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003569 goto out;
Lars Ellenberg004352f2010-10-05 20:13:58 +02003570 if (data_size <= (sizeof(*p) - sizeof(p->head))) {
3571 dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", data_size);
Andreas Gruenbacher78fcbda2010-12-10 22:18:27 +01003572 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003573 }
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003574 err = decode_bitmap_c(mdev, p, &c, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003575 } else {
Philipp Reisner02918be2010-08-20 14:35:10 +02003576 dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003577 goto out;
3578 }
3579
Philipp Reisner02918be2010-08-20 14:35:10 +02003580 c.packets[cmd == P_BITMAP]++;
Philipp Reisner257d0af2011-01-26 12:15:29 +01003581 c.bytes[cmd == P_BITMAP] += sizeof(struct p_header) + data_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003582
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003583 if (err <= 0) {
3584 if (err < 0)
3585 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003586 break;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003587 }
Philipp Reisner02918be2010-08-20 14:35:10 +02003588 if (!drbd_recv_header(mdev, &cmd, &data_size))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003589 goto out;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003590 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003591
3592 INFO_bm_xfer_stats(mdev, "receive", &c);
3593
3594 if (mdev->state.conn == C_WF_BITMAP_T) {
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01003595 enum drbd_state_rv rv;
3596
Philipp Reisnerb411b362009-09-25 16:07:19 -07003597 ok = !drbd_send_bitmap(mdev);
3598 if (!ok)
3599 goto out;
3600 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01003601 rv = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
3602 D_ASSERT(rv == SS_SUCCESS);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003603 } else if (mdev->state.conn != C_WF_BITMAP_S) {
3604 /* admin may have requested C_DISCONNECTING,
3605 * other threads may have noticed network errors */
3606 dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n",
3607 drbd_conn_str(mdev->state.conn));
3608 }
3609
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003610 ok = true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003611 out:
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003612 drbd_bm_unlock(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003613 if (ok && mdev->state.conn == C_WF_BITMAP_S)
3614 drbd_start_resync(mdev, C_SYNC_SOURCE);
3615 free_page((unsigned long) buffer);
3616 return ok;
3617}
3618
Philipp Reisner02918be2010-08-20 14:35:10 +02003619static int receive_skip(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003620{
3621 /* TODO zero copy sink :) */
3622 static char sink[128];
3623 int size, want, r;
3624
Philipp Reisner02918be2010-08-20 14:35:10 +02003625 dev_warn(DEV, "skipping unknown optional packet type %d, l: %d!\n",
3626 cmd, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003627
Philipp Reisner02918be2010-08-20 14:35:10 +02003628 size = data_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003629 while (size > 0) {
3630 want = min_t(int, size, sizeof(sink));
3631 r = drbd_recv(mdev, sink, want);
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01003632 if (!expect(r > 0))
3633 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003634 size -= r;
3635 }
3636 return size == 0;
3637}
3638
Philipp Reisner02918be2010-08-20 14:35:10 +02003639static int receive_UnplugRemote(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003640{
Philipp Reisnerb411b362009-09-25 16:07:19 -07003641 /* Make sure we've acked all the TCP data associated
3642 * with the data requests being unplugged */
Philipp Reisnere42325a2011-01-19 13:55:45 +01003643 drbd_tcp_quickack(mdev->tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003644
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003645 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003646}
3647
Philipp Reisner73a01a12010-10-27 14:33:00 +02003648static int receive_out_of_sync(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
3649{
Philipp Reisnere42325a2011-01-19 13:55:45 +01003650 struct p_block_desc *p = &mdev->tconn->data.rbuf.block_desc;
Philipp Reisner73a01a12010-10-27 14:33:00 +02003651
Lars Ellenbergf735e3632010-12-17 21:06:18 +01003652 switch (mdev->state.conn) {
3653 case C_WF_SYNC_UUID:
3654 case C_WF_BITMAP_T:
3655 case C_BEHIND:
3656 break;
3657 default:
3658 dev_err(DEV, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n",
3659 drbd_conn_str(mdev->state.conn));
3660 }
3661
Philipp Reisner73a01a12010-10-27 14:33:00 +02003662 drbd_set_out_of_sync(mdev, be64_to_cpu(p->sector), be32_to_cpu(p->blksize));
3663
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003664 return true;
Philipp Reisner73a01a12010-10-27 14:33:00 +02003665}
3666
Philipp Reisner02918be2010-08-20 14:35:10 +02003667typedef int (*drbd_cmd_handler_f)(struct drbd_conf *, enum drbd_packets cmd, unsigned int to_receive);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003668
Philipp Reisner02918be2010-08-20 14:35:10 +02003669struct data_cmd {
3670 int expect_payload;
3671 size_t pkt_size;
3672 drbd_cmd_handler_f function;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003673};
3674
Philipp Reisner02918be2010-08-20 14:35:10 +02003675static struct data_cmd drbd_cmd_handler[] = {
3676 [P_DATA] = { 1, sizeof(struct p_data), receive_Data },
3677 [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply },
3678 [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } ,
3679 [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } ,
Philipp Reisner257d0af2011-01-26 12:15:29 +01003680 [P_BITMAP] = { 1, sizeof(struct p_header), receive_bitmap } ,
3681 [P_COMPRESSED_BITMAP] = { 1, sizeof(struct p_header), receive_bitmap } ,
3682 [P_UNPLUG_REMOTE] = { 0, sizeof(struct p_header), receive_UnplugRemote },
Philipp Reisner02918be2010-08-20 14:35:10 +02003683 [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
3684 [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
Philipp Reisner257d0af2011-01-26 12:15:29 +01003685 [P_SYNC_PARAM] = { 1, sizeof(struct p_header), receive_SyncParam },
3686 [P_SYNC_PARAM89] = { 1, sizeof(struct p_header), receive_SyncParam },
Philipp Reisner02918be2010-08-20 14:35:10 +02003687 [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol },
3688 [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids },
3689 [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes },
3690 [P_STATE] = { 0, sizeof(struct p_state), receive_state },
3691 [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state },
3692 [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid },
3693 [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
3694 [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest },
3695 [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest },
3696 [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip },
Philipp Reisner73a01a12010-10-27 14:33:00 +02003697 [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync },
Philipp Reisner02918be2010-08-20 14:35:10 +02003698 /* anything missing from this table is in
3699 * the asender_tbl, see get_asender_cmd */
3700 [P_MAX_CMD] = { 0, 0, NULL },
3701};
3702
3703/* All handler functions that expect a sub-header get that sub-heder in
Philipp Reisnere42325a2011-01-19 13:55:45 +01003704 mdev->tconn->data.rbuf.header.head.payload.
Philipp Reisner02918be2010-08-20 14:35:10 +02003705
Philipp Reisnere42325a2011-01-19 13:55:45 +01003706 Usually in mdev->tconn->data.rbuf.header.head the callback can find the usual
Philipp Reisner02918be2010-08-20 14:35:10 +02003707 p_header, but they may not rely on that. Since there is also p_header95 !
3708 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003709
3710static void drbdd(struct drbd_conf *mdev)
3711{
Philipp Reisnerc0129492011-01-19 16:58:16 +01003712 struct p_header *header = &mdev->tconn->data.rbuf.header;
Philipp Reisner02918be2010-08-20 14:35:10 +02003713 unsigned int packet_size;
3714 enum drbd_packets cmd;
3715 size_t shs; /* sub header size */
3716 int rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003717
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01003718 while (get_t_state(&mdev->tconn->receiver) == RUNNING) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003719 drbd_thread_current_set_cpu(mdev);
Philipp Reisner02918be2010-08-20 14:35:10 +02003720 if (!drbd_recv_header(mdev, &cmd, &packet_size))
3721 goto err_out;
3722
3723 if (unlikely(cmd >= P_MAX_CMD || !drbd_cmd_handler[cmd].function)) {
3724 dev_err(DEV, "unknown packet type %d, l: %d!\n", cmd, packet_size);
3725 goto err_out;
Lars Ellenberg0b33a912009-11-16 15:58:04 +01003726 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003727
Philipp Reisnerc0129492011-01-19 16:58:16 +01003728 shs = drbd_cmd_handler[cmd].pkt_size - sizeof(struct p_header);
Philipp Reisner02918be2010-08-20 14:35:10 +02003729 if (packet_size - shs > 0 && !drbd_cmd_handler[cmd].expect_payload) {
3730 dev_err(DEV, "No payload expected %s l:%d\n", cmdname(cmd), packet_size);
3731 goto err_out;
3732 }
3733
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02003734 if (shs) {
Philipp Reisnerc0129492011-01-19 16:58:16 +01003735 rv = drbd_recv(mdev, &header->payload, shs);
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02003736 if (unlikely(rv != shs)) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01003737 if (!signal_pending(current))
3738 dev_warn(DEV, "short read while reading sub header: rv=%d\n", rv);
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02003739 goto err_out;
3740 }
3741 }
3742
Philipp Reisner02918be2010-08-20 14:35:10 +02003743 rv = drbd_cmd_handler[cmd].function(mdev, cmd, packet_size - shs);
3744
3745 if (unlikely(!rv)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003746 dev_err(DEV, "error receiving %s, l: %d!\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02003747 cmdname(cmd), packet_size);
3748 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003749 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003750 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003751
Philipp Reisner02918be2010-08-20 14:35:10 +02003752 if (0) {
3753 err_out:
3754 drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003755 }
Lars Ellenberg856c50c2010-10-14 13:37:40 +02003756 /* If we leave here, we probably want to update at least the
3757 * "Connected" indicator on stable storage. Do so explicitly here. */
3758 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003759}
3760
Philipp Reisner191d3cc2011-01-19 14:53:22 +01003761void drbd_flush_workqueue(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003762{
3763 struct drbd_wq_barrier barr;
3764
3765 barr.w.cb = w_prev_work_done;
3766 init_completion(&barr.done);
Philipp Reisner191d3cc2011-01-19 14:53:22 +01003767 drbd_queue_work(&tconn->data.work, &barr.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003768 wait_for_completion(&barr.done);
3769}
3770
3771static void drbd_disconnect(struct drbd_conf *mdev)
3772{
3773 enum drbd_fencing_p fp;
3774 union drbd_state os, ns;
3775 int rv = SS_UNKNOWN_ERROR;
3776 unsigned int i;
3777
3778 if (mdev->state.conn == C_STANDALONE)
3779 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003780
3781 /* asender does not clean up anything. it must not interfere, either */
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01003782 drbd_thread_stop(&mdev->tconn->asender);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003783 drbd_free_sock(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003784
Philipp Reisner85719572010-07-21 10:20:17 +02003785 /* wait for current activity to cease. */
Philipp Reisner87eeee42011-01-19 14:16:30 +01003786 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003787 _drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
3788 _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee);
3789 _drbd_wait_ee_list_empty(mdev, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003790 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003791
3792 /* We do not have data structures that would allow us to
3793 * get the rs_pending_cnt down to 0 again.
3794 * * On C_SYNC_TARGET we do not have any data structures describing
3795 * the pending RSDataRequest's we have sent.
3796 * * On C_SYNC_SOURCE there is no data structure that tracks
3797 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
3798 * And no, it is not the sum of the reference counts in the
3799 * resync_LRU. The resync_LRU tracks the whole operation including
3800 * the disk-IO, while the rs_pending_cnt only tracks the blocks
3801 * on the fly. */
3802 drbd_rs_cancel_all(mdev);
3803 mdev->rs_total = 0;
3804 mdev->rs_failed = 0;
3805 atomic_set(&mdev->rs_pending_cnt, 0);
3806 wake_up(&mdev->misc_wait);
3807
Philipp Reisner7fde2be2011-03-01 11:08:28 +01003808 del_timer(&mdev->request_timer);
3809
Philipp Reisnerb411b362009-09-25 16:07:19 -07003810 /* make sure syncer is stopped and w_resume_next_sg queued */
3811 del_timer_sync(&mdev->resync_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003812 resync_timer_fn((unsigned long)mdev);
3813
Philipp Reisnerb411b362009-09-25 16:07:19 -07003814 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
3815 * w_make_resync_request etc. which may still be on the worker queue
3816 * to be "canceled" */
Philipp Reisner191d3cc2011-01-19 14:53:22 +01003817 drbd_flush_workqueue(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003818
3819 /* This also does reclaim_net_ee(). If we do this too early, we might
3820 * miss some resync ee and pages.*/
3821 drbd_process_done_ee(mdev);
3822
3823 kfree(mdev->p_uuid);
3824 mdev->p_uuid = NULL;
3825
Philipp Reisnerfb22c402010-09-08 23:20:21 +02003826 if (!is_susp(mdev->state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003827 tl_clear(mdev);
3828
Philipp Reisnerb411b362009-09-25 16:07:19 -07003829 dev_info(DEV, "Connection closed\n");
3830
3831 drbd_md_sync(mdev);
3832
3833 fp = FP_DONT_CARE;
3834 if (get_ldev(mdev)) {
3835 fp = mdev->ldev->dc.fencing;
3836 put_ldev(mdev);
3837 }
3838
Philipp Reisner87f7be42010-06-11 13:56:33 +02003839 if (mdev->state.role == R_PRIMARY && fp >= FP_RESOURCE && mdev->state.pdsk >= D_UNKNOWN)
3840 drbd_try_outdate_peer_async(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003841
Philipp Reisner87eeee42011-01-19 14:16:30 +01003842 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003843 os = mdev->state;
3844 if (os.conn >= C_UNCONNECTED) {
3845 /* Do not restart in case we are C_DISCONNECTING */
3846 ns = os;
3847 ns.conn = C_UNCONNECTED;
3848 rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
3849 }
Philipp Reisner87eeee42011-01-19 14:16:30 +01003850 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003851
3852 if (os.conn == C_DISCONNECTING) {
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01003853 wait_event(mdev->tconn->net_cnt_wait, atomic_read(&mdev->tconn->net_cnt) == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003854
Philipp Reisnera0638452011-01-19 14:31:32 +01003855 crypto_free_hash(mdev->tconn->cram_hmac_tfm);
3856 mdev->tconn->cram_hmac_tfm = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003857
Philipp Reisner89e58e72011-01-19 13:12:45 +01003858 kfree(mdev->tconn->net_conf);
3859 mdev->tconn->net_conf = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003860 drbd_request_state(mdev, NS(conn, C_STANDALONE));
3861 }
3862
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003863 /* serialize with bitmap writeout triggered by the state change,
3864 * if any. */
3865 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
3866
Philipp Reisnerb411b362009-09-25 16:07:19 -07003867 /* tcp_close and release of sendpage pages can be deferred. I don't
3868 * want to use SO_LINGER, because apparently it can be deferred for
3869 * more than 20 seconds (longest time I checked).
3870 *
3871 * Actually we don't care for exactly when the network stack does its
3872 * put_page(), but release our reference on these pages right here.
3873 */
3874 i = drbd_release_ee(mdev, &mdev->net_ee);
3875 if (i)
3876 dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
Lars Ellenberg435f0742010-09-06 12:30:25 +02003877 i = atomic_read(&mdev->pp_in_use_by_net);
3878 if (i)
3879 dev_info(DEV, "pp_in_use_by_net = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003880 i = atomic_read(&mdev->pp_in_use);
3881 if (i)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02003882 dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003883
3884 D_ASSERT(list_empty(&mdev->read_ee));
3885 D_ASSERT(list_empty(&mdev->active_ee));
3886 D_ASSERT(list_empty(&mdev->sync_ee));
3887 D_ASSERT(list_empty(&mdev->done_ee));
3888
3889 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
3890 atomic_set(&mdev->current_epoch->epoch_size, 0);
3891 D_ASSERT(list_empty(&mdev->current_epoch->list));
3892}
3893
3894/*
3895 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
3896 * we can agree on is stored in agreed_pro_version.
3897 *
3898 * feature flags and the reserved array should be enough room for future
3899 * enhancements of the handshake protocol, and possible plugins...
3900 *
3901 * for now, they are expected to be zero, but ignored.
3902 */
3903static int drbd_send_handshake(struct drbd_conf *mdev)
3904{
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01003905 /* ASSERT current == mdev->tconn->receiver ... */
Philipp Reisnere42325a2011-01-19 13:55:45 +01003906 struct p_handshake *p = &mdev->tconn->data.sbuf.handshake;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003907 int ok;
3908
Philipp Reisnere42325a2011-01-19 13:55:45 +01003909 if (mutex_lock_interruptible(&mdev->tconn->data.mutex)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003910 dev_err(DEV, "interrupted during initial handshake\n");
3911 return 0; /* interrupted. not ok. */
3912 }
3913
Philipp Reisnere42325a2011-01-19 13:55:45 +01003914 if (mdev->tconn->data.socket == NULL) {
3915 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003916 return 0;
3917 }
3918
3919 memset(p, 0, sizeof(*p));
3920 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
3921 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
Philipp Reisnerc0129492011-01-19 16:58:16 +01003922 ok = _drbd_send_cmd(mdev, mdev->tconn->data.socket, P_HAND_SHAKE,
3923 &p->head, sizeof(*p), 0 );
Philipp Reisnere42325a2011-01-19 13:55:45 +01003924 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003925 return ok;
3926}
3927
3928/*
3929 * return values:
3930 * 1 yes, we have a valid connection
3931 * 0 oops, did not work out, please try again
3932 * -1 peer talks different language,
3933 * no point in trying again, please go standalone.
3934 */
3935static int drbd_do_handshake(struct drbd_conf *mdev)
3936{
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01003937 /* ASSERT current == mdev->tconn->receiver ... */
Philipp Reisnere42325a2011-01-19 13:55:45 +01003938 struct p_handshake *p = &mdev->tconn->data.rbuf.handshake;
Philipp Reisner02918be2010-08-20 14:35:10 +02003939 const int expect = sizeof(struct p_handshake) - sizeof(struct p_header80);
3940 unsigned int length;
3941 enum drbd_packets cmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003942 int rv;
3943
3944 rv = drbd_send_handshake(mdev);
3945 if (!rv)
3946 return 0;
3947
Philipp Reisner02918be2010-08-20 14:35:10 +02003948 rv = drbd_recv_header(mdev, &cmd, &length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003949 if (!rv)
3950 return 0;
3951
Philipp Reisner02918be2010-08-20 14:35:10 +02003952 if (cmd != P_HAND_SHAKE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003953 dev_err(DEV, "expected HandShake packet, received: %s (0x%04x)\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02003954 cmdname(cmd), cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003955 return -1;
3956 }
3957
Philipp Reisner02918be2010-08-20 14:35:10 +02003958 if (length != expect) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003959 dev_err(DEV, "expected HandShake length: %u, received: %u\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02003960 expect, length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003961 return -1;
3962 }
3963
3964 rv = drbd_recv(mdev, &p->head.payload, expect);
3965
3966 if (rv != expect) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01003967 if (!signal_pending(current))
3968 dev_warn(DEV, "short read receiving handshake packet: l=%u\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003969 return 0;
3970 }
3971
Philipp Reisnerb411b362009-09-25 16:07:19 -07003972 p->protocol_min = be32_to_cpu(p->protocol_min);
3973 p->protocol_max = be32_to_cpu(p->protocol_max);
3974 if (p->protocol_max == 0)
3975 p->protocol_max = p->protocol_min;
3976
3977 if (PRO_VERSION_MAX < p->protocol_min ||
3978 PRO_VERSION_MIN > p->protocol_max)
3979 goto incompat;
3980
Philipp Reisner31890f42011-01-19 14:12:51 +01003981 mdev->tconn->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003982
3983 dev_info(DEV, "Handshake successful: "
Philipp Reisner31890f42011-01-19 14:12:51 +01003984 "Agreed network protocol version %d\n", mdev->tconn->agreed_pro_version);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003985
3986 return 1;
3987
3988 incompat:
3989 dev_err(DEV, "incompatible DRBD dialects: "
3990 "I support %d-%d, peer supports %d-%d\n",
3991 PRO_VERSION_MIN, PRO_VERSION_MAX,
3992 p->protocol_min, p->protocol_max);
3993 return -1;
3994}
3995
3996#if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
3997static int drbd_do_auth(struct drbd_conf *mdev)
3998{
3999 dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
4000 dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004001 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004002}
4003#else
4004#define CHALLENGE_LEN 64
Johannes Thomab10d96c2010-01-07 16:02:50 +01004005
4006/* Return value:
4007 1 - auth succeeded,
4008 0 - failed, try again (network error),
4009 -1 - auth failed, don't try again.
4010*/
4011
Philipp Reisnerb411b362009-09-25 16:07:19 -07004012static int drbd_do_auth(struct drbd_conf *mdev)
4013{
4014 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
4015 struct scatterlist sg;
4016 char *response = NULL;
4017 char *right_response = NULL;
4018 char *peers_ch = NULL;
Philipp Reisner89e58e72011-01-19 13:12:45 +01004019 unsigned int key_len = strlen(mdev->tconn->net_conf->shared_secret);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004020 unsigned int resp_size;
4021 struct hash_desc desc;
Philipp Reisner02918be2010-08-20 14:35:10 +02004022 enum drbd_packets cmd;
4023 unsigned int length;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004024 int rv;
4025
Philipp Reisnera0638452011-01-19 14:31:32 +01004026 desc.tfm = mdev->tconn->cram_hmac_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004027 desc.flags = 0;
4028
Philipp Reisnera0638452011-01-19 14:31:32 +01004029 rv = crypto_hash_setkey(mdev->tconn->cram_hmac_tfm,
Philipp Reisner89e58e72011-01-19 13:12:45 +01004030 (u8 *)mdev->tconn->net_conf->shared_secret, key_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004031 if (rv) {
4032 dev_err(DEV, "crypto_hash_setkey() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004033 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004034 goto fail;
4035 }
4036
4037 get_random_bytes(my_challenge, CHALLENGE_LEN);
4038
4039 rv = drbd_send_cmd2(mdev, P_AUTH_CHALLENGE, my_challenge, CHALLENGE_LEN);
4040 if (!rv)
4041 goto fail;
4042
Philipp Reisner02918be2010-08-20 14:35:10 +02004043 rv = drbd_recv_header(mdev, &cmd, &length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004044 if (!rv)
4045 goto fail;
4046
Philipp Reisner02918be2010-08-20 14:35:10 +02004047 if (cmd != P_AUTH_CHALLENGE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004048 dev_err(DEV, "expected AuthChallenge packet, received: %s (0x%04x)\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02004049 cmdname(cmd), cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004050 rv = 0;
4051 goto fail;
4052 }
4053
Philipp Reisner02918be2010-08-20 14:35:10 +02004054 if (length > CHALLENGE_LEN * 2) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004055 dev_err(DEV, "expected AuthChallenge payload too big.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004056 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004057 goto fail;
4058 }
4059
Philipp Reisner02918be2010-08-20 14:35:10 +02004060 peers_ch = kmalloc(length, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004061 if (peers_ch == NULL) {
4062 dev_err(DEV, "kmalloc of peers_ch failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004063 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004064 goto fail;
4065 }
4066
Philipp Reisner02918be2010-08-20 14:35:10 +02004067 rv = drbd_recv(mdev, peers_ch, length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004068
Philipp Reisner02918be2010-08-20 14:35:10 +02004069 if (rv != length) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01004070 if (!signal_pending(current))
4071 dev_warn(DEV, "short read AuthChallenge: l=%u\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004072 rv = 0;
4073 goto fail;
4074 }
4075
Philipp Reisnera0638452011-01-19 14:31:32 +01004076 resp_size = crypto_hash_digestsize(mdev->tconn->cram_hmac_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004077 response = kmalloc(resp_size, GFP_NOIO);
4078 if (response == NULL) {
4079 dev_err(DEV, "kmalloc of response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004080 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004081 goto fail;
4082 }
4083
4084 sg_init_table(&sg, 1);
Philipp Reisner02918be2010-08-20 14:35:10 +02004085 sg_set_buf(&sg, peers_ch, length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004086
4087 rv = crypto_hash_digest(&desc, &sg, sg.length, response);
4088 if (rv) {
4089 dev_err(DEV, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004090 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004091 goto fail;
4092 }
4093
4094 rv = drbd_send_cmd2(mdev, P_AUTH_RESPONSE, response, resp_size);
4095 if (!rv)
4096 goto fail;
4097
Philipp Reisner02918be2010-08-20 14:35:10 +02004098 rv = drbd_recv_header(mdev, &cmd, &length);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004099 if (!rv)
4100 goto fail;
4101
Philipp Reisner02918be2010-08-20 14:35:10 +02004102 if (cmd != P_AUTH_RESPONSE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004103 dev_err(DEV, "expected AuthResponse packet, received: %s (0x%04x)\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02004104 cmdname(cmd), cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004105 rv = 0;
4106 goto fail;
4107 }
4108
Philipp Reisner02918be2010-08-20 14:35:10 +02004109 if (length != resp_size) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004110 dev_err(DEV, "expected AuthResponse payload of wrong size\n");
4111 rv = 0;
4112 goto fail;
4113 }
4114
4115 rv = drbd_recv(mdev, response , resp_size);
4116
4117 if (rv != resp_size) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01004118 if (!signal_pending(current))
4119 dev_warn(DEV, "short read receiving AuthResponse: l=%u\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004120 rv = 0;
4121 goto fail;
4122 }
4123
4124 right_response = kmalloc(resp_size, GFP_NOIO);
Julia Lawall2d1ee872009-12-27 22:27:11 +01004125 if (right_response == NULL) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004126 dev_err(DEV, "kmalloc of right_response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004127 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004128 goto fail;
4129 }
4130
4131 sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
4132
4133 rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
4134 if (rv) {
4135 dev_err(DEV, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004136 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004137 goto fail;
4138 }
4139
4140 rv = !memcmp(response, right_response, resp_size);
4141
4142 if (rv)
4143 dev_info(DEV, "Peer authenticated using %d bytes of '%s' HMAC\n",
Philipp Reisner89e58e72011-01-19 13:12:45 +01004144 resp_size, mdev->tconn->net_conf->cram_hmac_alg);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004145 else
4146 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004147
4148 fail:
4149 kfree(peers_ch);
4150 kfree(response);
4151 kfree(right_response);
4152
4153 return rv;
4154}
4155#endif
4156
4157int drbdd_init(struct drbd_thread *thi)
4158{
4159 struct drbd_conf *mdev = thi->mdev;
4160 unsigned int minor = mdev_to_minor(mdev);
4161 int h;
4162
4163 sprintf(current->comm, "drbd%d_receiver", minor);
4164
4165 dev_info(DEV, "receiver (re)started\n");
4166
4167 do {
4168 h = drbd_connect(mdev);
4169 if (h == 0) {
4170 drbd_disconnect(mdev);
Philipp Reisner20ee6392011-01-18 15:28:59 +01004171 schedule_timeout_interruptible(HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004172 }
4173 if (h == -1) {
4174 dev_warn(DEV, "Discarding network configuration.\n");
4175 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
4176 }
4177 } while (h == 0);
4178
4179 if (h > 0) {
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01004180 if (get_net_conf(mdev->tconn)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004181 drbdd(mdev);
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01004182 put_net_conf(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004183 }
4184 }
4185
4186 drbd_disconnect(mdev);
4187
4188 dev_info(DEV, "receiver terminated\n");
4189 return 0;
4190}
4191
4192/* ********* acknowledge sender ******** */
4193
Philipp Reisner257d0af2011-01-26 12:15:29 +01004194static int got_RqSReply(struct drbd_conf *mdev, enum drbd_packets cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004195{
Philipp Reisner257d0af2011-01-26 12:15:29 +01004196 struct p_req_state_reply *p = &mdev->tconn->meta.rbuf.req_state_reply;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004197
4198 int retcode = be32_to_cpu(p->retcode);
4199
4200 if (retcode >= SS_SUCCESS) {
4201 set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
4202 } else {
4203 set_bit(CL_ST_CHG_FAIL, &mdev->flags);
4204 dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
4205 drbd_set_st_err_str(retcode), retcode);
4206 }
4207 wake_up(&mdev->state_wait);
4208
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004209 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004210}
4211
Philipp Reisner257d0af2011-01-26 12:15:29 +01004212static int got_Ping(struct drbd_conf *mdev, enum drbd_packets cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004213{
4214 return drbd_send_ping_ack(mdev);
4215
4216}
4217
Philipp Reisner257d0af2011-01-26 12:15:29 +01004218static int got_PingAck(struct drbd_conf *mdev, enum drbd_packets cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004219{
4220 /* restore idle timeout */
Philipp Reisnere42325a2011-01-19 13:55:45 +01004221 mdev->tconn->meta.socket->sk->sk_rcvtimeo = mdev->tconn->net_conf->ping_int*HZ;
Philipp Reisner309d1602010-03-02 15:03:44 +01004222 if (!test_and_set_bit(GOT_PING_ACK, &mdev->flags))
4223 wake_up(&mdev->misc_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004224
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004225 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004226}
4227
Philipp Reisner257d0af2011-01-26 12:15:29 +01004228static int got_IsInSync(struct drbd_conf *mdev, enum drbd_packets cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004229{
Philipp Reisner257d0af2011-01-26 12:15:29 +01004230 struct p_block_ack *p = &mdev->tconn->meta.rbuf.block_ack;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004231 sector_t sector = be64_to_cpu(p->sector);
4232 int blksize = be32_to_cpu(p->blksize);
4233
Philipp Reisner31890f42011-01-19 14:12:51 +01004234 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004235
4236 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4237
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004238 if (get_ldev(mdev)) {
4239 drbd_rs_complete_io(mdev, sector);
4240 drbd_set_in_sync(mdev, sector, blksize);
4241 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
4242 mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
4243 put_ldev(mdev);
4244 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004245 dec_rs_pending(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02004246 atomic_add(blksize >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004247
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004248 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004249}
4250
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004251static int
4252validate_req_change_req_state(struct drbd_conf *mdev, u64 id, sector_t sector,
4253 struct rb_root *root, const char *func,
4254 enum drbd_req_event what, bool missing_ok)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004255{
4256 struct drbd_request *req;
4257 struct bio_and_error m;
4258
Philipp Reisner87eeee42011-01-19 14:16:30 +01004259 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004260 req = find_request(mdev, root, id, sector, missing_ok, func);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004261 if (unlikely(!req)) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01004262 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004263 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004264 }
4265 __req_mod(req, what, &m);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004266 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004267
4268 if (m.bio)
4269 complete_master_bio(mdev, &m);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004270 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004271}
4272
Philipp Reisner257d0af2011-01-26 12:15:29 +01004273static int got_BlockAck(struct drbd_conf *mdev, enum drbd_packets cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004274{
Philipp Reisner257d0af2011-01-26 12:15:29 +01004275 struct p_block_ack *p = &mdev->tconn->meta.rbuf.block_ack;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004276 sector_t sector = be64_to_cpu(p->sector);
4277 int blksize = be32_to_cpu(p->blksize);
4278 enum drbd_req_event what;
4279
4280 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4281
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004282 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004283 drbd_set_in_sync(mdev, sector, blksize);
4284 dec_rs_pending(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004285 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004286 }
Philipp Reisner257d0af2011-01-26 12:15:29 +01004287 switch (cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004288 case P_RS_WRITE_ACK:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004289 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004290 what = WRITE_ACKED_BY_PEER_AND_SIS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004291 break;
4292 case P_WRITE_ACK:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004293 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004294 what = WRITE_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004295 break;
4296 case P_RECV_ACK:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004297 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_B);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004298 what = RECV_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004299 break;
4300 case P_DISCARD_ACK:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004301 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004302 what = CONFLICT_DISCARDED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004303 break;
4304 default:
4305 D_ASSERT(0);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004306 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004307 }
4308
4309 return validate_req_change_req_state(mdev, p->block_id, sector,
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004310 &mdev->write_requests, __func__,
4311 what, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004312}
4313
Philipp Reisner257d0af2011-01-26 12:15:29 +01004314static int got_NegAck(struct drbd_conf *mdev, enum drbd_packets cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004315{
Philipp Reisner257d0af2011-01-26 12:15:29 +01004316 struct p_block_ack *p = &mdev->tconn->meta.rbuf.block_ack;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004317 sector_t sector = be64_to_cpu(p->sector);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004318 int size = be32_to_cpu(p->blksize);
Philipp Reisner89e58e72011-01-19 13:12:45 +01004319 bool missing_ok = mdev->tconn->net_conf->wire_protocol == DRBD_PROT_A ||
4320 mdev->tconn->net_conf->wire_protocol == DRBD_PROT_B;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004321 bool found;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004322
4323 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4324
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004325 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004326 dec_rs_pending(mdev);
4327 drbd_rs_failed_io(mdev, sector, size);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004328 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004329 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01004330
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004331 found = validate_req_change_req_state(mdev, p->block_id, sector,
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004332 &mdev->write_requests, __func__,
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004333 NEG_ACKED, missing_ok);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004334 if (!found) {
4335 /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs.
4336 The master bio might already be completed, therefore the
4337 request is no longer in the collision hash. */
4338 /* In Protocol B we might already have got a P_RECV_ACK
4339 but then get a P_NEG_ACK afterwards. */
4340 if (!missing_ok)
Philipp Reisner2deb8332011-01-17 18:39:18 +01004341 return false;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004342 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004343 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01004344 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004345}
4346
Philipp Reisner257d0af2011-01-26 12:15:29 +01004347static int got_NegDReply(struct drbd_conf *mdev, enum drbd_packets cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004348{
Philipp Reisner257d0af2011-01-26 12:15:29 +01004349 struct p_block_ack *p = &mdev->tconn->meta.rbuf.block_ack;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004350 sector_t sector = be64_to_cpu(p->sector);
4351
4352 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4353 dev_err(DEV, "Got NegDReply; Sector %llus, len %u; Fail original request.\n",
4354 (unsigned long long)sector, be32_to_cpu(p->blksize));
4355
4356 return validate_req_change_req_state(mdev, p->block_id, sector,
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004357 &mdev->read_requests, __func__,
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004358 NEG_ACKED, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004359}
4360
Philipp Reisner257d0af2011-01-26 12:15:29 +01004361static int got_NegRSDReply(struct drbd_conf *mdev, enum drbd_packets cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004362{
4363 sector_t sector;
4364 int size;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004365 struct p_block_ack *p = &mdev->tconn->meta.rbuf.block_ack;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004366
4367 sector = be64_to_cpu(p->sector);
4368 size = be32_to_cpu(p->blksize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004369
4370 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4371
4372 dec_rs_pending(mdev);
4373
4374 if (get_ldev_if_state(mdev, D_FAILED)) {
4375 drbd_rs_complete_io(mdev, sector);
Philipp Reisner257d0af2011-01-26 12:15:29 +01004376 switch (cmd) {
Philipp Reisnerd612d302010-12-27 10:53:28 +01004377 case P_NEG_RS_DREPLY:
4378 drbd_rs_failed_io(mdev, sector, size);
4379 case P_RS_CANCEL:
4380 break;
4381 default:
4382 D_ASSERT(0);
4383 put_ldev(mdev);
4384 return false;
4385 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004386 put_ldev(mdev);
4387 }
4388
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004389 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004390}
4391
Philipp Reisner257d0af2011-01-26 12:15:29 +01004392static int got_BarrierAck(struct drbd_conf *mdev, enum drbd_packets cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004393{
Philipp Reisner257d0af2011-01-26 12:15:29 +01004394 struct p_barrier_ack *p = &mdev->tconn->meta.rbuf.barrier_ack;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004395
4396 tl_release(mdev, p->barrier, be32_to_cpu(p->set_size));
4397
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02004398 if (mdev->state.conn == C_AHEAD &&
4399 atomic_read(&mdev->ap_in_flight) == 0 &&
Philipp Reisner370a43e2011-01-14 16:03:11 +01004400 !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->current_epoch->flags)) {
4401 mdev->start_resync_timer.expires = jiffies + HZ;
4402 add_timer(&mdev->start_resync_timer);
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02004403 }
4404
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004405 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004406}
4407
Philipp Reisner257d0af2011-01-26 12:15:29 +01004408static int got_OVResult(struct drbd_conf *mdev, enum drbd_packets cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004409{
Philipp Reisner257d0af2011-01-26 12:15:29 +01004410 struct p_block_ack *p = &mdev->tconn->meta.rbuf.block_ack;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004411 struct drbd_work *w;
4412 sector_t sector;
4413 int size;
4414
4415 sector = be64_to_cpu(p->sector);
4416 size = be32_to_cpu(p->blksize);
4417
4418 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4419
4420 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
4421 drbd_ov_oos_found(mdev, sector, size);
4422 else
4423 ov_oos_print(mdev);
4424
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004425 if (!get_ldev(mdev))
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004426 return true;
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004427
Philipp Reisnerb411b362009-09-25 16:07:19 -07004428 drbd_rs_complete_io(mdev, sector);
4429 dec_rs_pending(mdev);
4430
Lars Ellenbergea5442a2010-11-05 09:48:01 +01004431 --mdev->ov_left;
4432
4433 /* let's advance progress step marks only for every other megabyte */
4434 if ((mdev->ov_left & 0x200) == 0x200)
4435 drbd_advance_rs_marks(mdev, mdev->ov_left);
4436
4437 if (mdev->ov_left == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004438 w = kmalloc(sizeof(*w), GFP_NOIO);
4439 if (w) {
4440 w->cb = w_ov_finished;
Philipp Reisnere42325a2011-01-19 13:55:45 +01004441 drbd_queue_work_front(&mdev->tconn->data.work, w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004442 } else {
4443 dev_err(DEV, "kmalloc(w) failed.");
4444 ov_oos_print(mdev);
4445 drbd_resync_finished(mdev);
4446 }
4447 }
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004448 put_ldev(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004449 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004450}
4451
Philipp Reisner257d0af2011-01-26 12:15:29 +01004452static int got_skip(struct drbd_conf *mdev, enum drbd_packets cmd)
Philipp Reisner0ced55a2010-04-30 15:26:20 +02004453{
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004454 return true;
Philipp Reisner0ced55a2010-04-30 15:26:20 +02004455}
4456
Philipp Reisnerb411b362009-09-25 16:07:19 -07004457struct asender_cmd {
4458 size_t pkt_size;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004459 int (*process)(struct drbd_conf *mdev, enum drbd_packets cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004460};
4461
4462static struct asender_cmd *get_asender_cmd(int cmd)
4463{
4464 static struct asender_cmd asender_tbl[] = {
4465 /* anything missing from this table is in
4466 * the drbd_cmd_handler (drbd_default_handler) table,
4467 * see the beginning of drbdd() */
Philipp Reisner257d0af2011-01-26 12:15:29 +01004468 [P_PING] = { sizeof(struct p_header), got_Ping },
4469 [P_PING_ACK] = { sizeof(struct p_header), got_PingAck },
Philipp Reisnerb411b362009-09-25 16:07:19 -07004470 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4471 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4472 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4473 [P_DISCARD_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4474 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
4475 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
4476 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply},
4477 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
4478 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
4479 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
4480 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
Philipp Reisner02918be2010-08-20 14:35:10 +02004481 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
Philipp Reisnerd612d302010-12-27 10:53:28 +01004482 [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply},
Philipp Reisnerb411b362009-09-25 16:07:19 -07004483 [P_MAX_CMD] = { 0, NULL },
4484 };
4485 if (cmd > P_MAX_CMD || asender_tbl[cmd].process == NULL)
4486 return NULL;
4487 return &asender_tbl[cmd];
4488}
4489
4490int drbd_asender(struct drbd_thread *thi)
4491{
4492 struct drbd_conf *mdev = thi->mdev;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004493 struct p_header *h = &mdev->tconn->meta.rbuf.header;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004494 struct asender_cmd *cmd = NULL;
4495
Philipp Reisner257d0af2011-01-26 12:15:29 +01004496 int rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004497 void *buf = h;
4498 int received = 0;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004499 int expect = sizeof(struct p_header);
Lars Ellenbergf36af182011-03-09 22:44:55 +01004500 int ping_timeout_active = 0;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004501 int empty, pkt_size;
4502 enum drbd_packets cmd_nr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004503
4504 sprintf(current->comm, "drbd%d_asender", mdev_to_minor(mdev));
4505
4506 current->policy = SCHED_RR; /* Make this a realtime task! */
4507 current->rt_priority = 2; /* more important than all other tasks */
4508
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01004509 while (get_t_state(thi) == RUNNING) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004510 drbd_thread_current_set_cpu(mdev);
4511 if (test_and_clear_bit(SEND_PING, &mdev->flags)) {
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01004512 if (!drbd_send_ping(mdev)) {
4513 dev_err(DEV, "drbd_send_ping has failed\n");
4514 goto reconnect;
4515 }
Philipp Reisnere42325a2011-01-19 13:55:45 +01004516 mdev->tconn->meta.socket->sk->sk_rcvtimeo =
Philipp Reisner89e58e72011-01-19 13:12:45 +01004517 mdev->tconn->net_conf->ping_timeo*HZ/10;
Lars Ellenbergf36af182011-03-09 22:44:55 +01004518 ping_timeout_active = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004519 }
4520
4521 /* conditionally cork;
4522 * it may hurt latency if we cork without much to send */
Philipp Reisner89e58e72011-01-19 13:12:45 +01004523 if (!mdev->tconn->net_conf->no_cork &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07004524 3 < atomic_read(&mdev->unacked_cnt))
Philipp Reisnere42325a2011-01-19 13:55:45 +01004525 drbd_tcp_cork(mdev->tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004526 while (1) {
4527 clear_bit(SIGNAL_ASENDER, &mdev->flags);
4528 flush_signals(current);
Lars Ellenberg0f8488e2010-10-13 18:19:23 +02004529 if (!drbd_process_done_ee(mdev))
Philipp Reisnerb411b362009-09-25 16:07:19 -07004530 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004531 /* to avoid race with newly queued ACKs */
4532 set_bit(SIGNAL_ASENDER, &mdev->flags);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004533 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004534 empty = list_empty(&mdev->done_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004535 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004536 /* new ack may have been queued right here,
4537 * but then there is also a signal pending,
4538 * and we start over... */
4539 if (empty)
4540 break;
4541 }
4542 /* but unconditionally uncork unless disabled */
Philipp Reisner89e58e72011-01-19 13:12:45 +01004543 if (!mdev->tconn->net_conf->no_cork)
Philipp Reisnere42325a2011-01-19 13:55:45 +01004544 drbd_tcp_uncork(mdev->tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004545
4546 /* short circuit, recv_msg would return EINTR anyways. */
4547 if (signal_pending(current))
4548 continue;
4549
Philipp Reisnere42325a2011-01-19 13:55:45 +01004550 rv = drbd_recv_short(mdev, mdev->tconn->meta.socket,
Philipp Reisnerb411b362009-09-25 16:07:19 -07004551 buf, expect-received, 0);
4552 clear_bit(SIGNAL_ASENDER, &mdev->flags);
4553
4554 flush_signals(current);
4555
4556 /* Note:
4557 * -EINTR (on meta) we got a signal
4558 * -EAGAIN (on meta) rcvtimeo expired
4559 * -ECONNRESET other side closed the connection
4560 * -ERESTARTSYS (on data) we got a signal
4561 * rv < 0 other than above: unexpected error!
4562 * rv == expected: full header or command
4563 * rv < expected: "woken" by signal during receive
4564 * rv == 0 : "connection shut down by peer"
4565 */
4566 if (likely(rv > 0)) {
4567 received += rv;
4568 buf += rv;
4569 } else if (rv == 0) {
4570 dev_err(DEV, "meta connection shut down by peer.\n");
4571 goto reconnect;
4572 } else if (rv == -EAGAIN) {
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02004573 /* If the data socket received something meanwhile,
4574 * that is good enough: peer is still alive. */
Philipp Reisner31890f42011-01-19 14:12:51 +01004575 if (time_after(mdev->tconn->last_received,
Philipp Reisnere42325a2011-01-19 13:55:45 +01004576 jiffies - mdev->tconn->meta.socket->sk->sk_rcvtimeo))
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02004577 continue;
Lars Ellenbergf36af182011-03-09 22:44:55 +01004578 if (ping_timeout_active) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004579 dev_err(DEV, "PingAck did not arrive in time.\n");
4580 goto reconnect;
4581 }
4582 set_bit(SEND_PING, &mdev->flags);
4583 continue;
4584 } else if (rv == -EINTR) {
4585 continue;
4586 } else {
4587 dev_err(DEV, "sock_recvmsg returned %d\n", rv);
4588 goto reconnect;
4589 }
4590
4591 if (received == expect && cmd == NULL) {
Philipp Reisner257d0af2011-01-26 12:15:29 +01004592 if (!decode_header(mdev, h, &cmd_nr, &pkt_size))
Philipp Reisnerb411b362009-09-25 16:07:19 -07004593 goto reconnect;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004594 cmd = get_asender_cmd(cmd_nr);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004595 if (unlikely(cmd == NULL)) {
Philipp Reisner257d0af2011-01-26 12:15:29 +01004596 dev_err(DEV, "unknown command %d on meta (l: %d)\n",
4597 cmd_nr, pkt_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004598 goto disconnect;
4599 }
4600 expect = cmd->pkt_size;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004601 if (pkt_size != expect - sizeof(struct p_header)) {
4602 dev_err(DEV, "Wrong packet size on meta (c: %d, l: %d)\n",
4603 cmd_nr, pkt_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004604 goto reconnect;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004605 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004606 }
4607 if (received == expect) {
Philipp Reisner31890f42011-01-19 14:12:51 +01004608 mdev->tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004609 D_ASSERT(cmd != NULL);
Philipp Reisner257d0af2011-01-26 12:15:29 +01004610 if (!cmd->process(mdev, cmd_nr))
Philipp Reisnerb411b362009-09-25 16:07:19 -07004611 goto reconnect;
4612
Lars Ellenbergf36af182011-03-09 22:44:55 +01004613 /* the idle_timeout (ping-int)
4614 * has been restored in got_PingAck() */
4615 if (cmd == get_asender_cmd(P_PING_ACK))
4616 ping_timeout_active = 0;
4617
Philipp Reisnerb411b362009-09-25 16:07:19 -07004618 buf = h;
4619 received = 0;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004620 expect = sizeof(struct p_header);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004621 cmd = NULL;
4622 }
4623 }
4624
4625 if (0) {
4626reconnect:
4627 drbd_force_state(mdev, NS(conn, C_NETWORK_FAILURE));
Lars Ellenberg856c50c2010-10-14 13:37:40 +02004628 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004629 }
4630 if (0) {
4631disconnect:
4632 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Lars Ellenberg856c50c2010-10-14 13:37:40 +02004633 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004634 }
4635 clear_bit(SIGNAL_ASENDER, &mdev->flags);
4636
4637 D_ASSERT(mdev->state.conn < C_CONNECTED);
4638 dev_info(DEV, "asender terminated\n");
4639
4640 return 0;
4641}