blob: 2e5318f9422e6f7288c6232f2b33036be747daa1 [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 Reisner77351055b2011-02-07 17:24:26 +010051struct packet_info {
52 enum drbd_packet cmd;
53 int size;
54 int vnr;
55};
56
Philipp Reisnerb411b362009-09-25 16:07:19 -070057enum finish_epoch {
58 FE_STILL_LIVE,
59 FE_DESTROYED,
60 FE_RECYCLED,
61};
62
Philipp Reisner65d11ed2011-02-07 17:35:59 +010063static int drbd_do_handshake(struct drbd_tconn *tconn);
Philipp Reisner13e60372011-02-08 09:54:40 +010064static int drbd_do_auth(struct drbd_tconn *tconn);
Philipp Reisner360cc742011-02-08 14:29:53 +010065static int drbd_disconnected(int vnr, void *p, void *data);
Philipp Reisnerb411b362009-09-25 16:07:19 -070066
67static enum finish_epoch drbd_may_finish_epoch(struct drbd_conf *, struct drbd_epoch *, enum epoch_event);
68static int e_end_block(struct drbd_conf *, struct drbd_work *, int);
69
Philipp Reisnerb411b362009-09-25 16:07:19 -070070
71#define GFP_TRY (__GFP_HIGHMEM | __GFP_NOWARN)
72
Lars Ellenberg45bb9122010-05-14 17:10:48 +020073/*
74 * some helper functions to deal with single linked page lists,
75 * page->private being our "next" pointer.
76 */
77
78/* If at least n pages are linked at head, get n pages off.
79 * Otherwise, don't modify head, and return NULL.
80 * Locking is the responsibility of the caller.
81 */
82static struct page *page_chain_del(struct page **head, int n)
83{
84 struct page *page;
85 struct page *tmp;
86
87 BUG_ON(!n);
88 BUG_ON(!head);
89
90 page = *head;
Philipp Reisner23ce4222010-05-20 13:35:31 +020091
92 if (!page)
93 return NULL;
94
Lars Ellenberg45bb9122010-05-14 17:10:48 +020095 while (page) {
96 tmp = page_chain_next(page);
97 if (--n == 0)
98 break; /* found sufficient pages */
99 if (tmp == NULL)
100 /* insufficient pages, don't use any of them. */
101 return NULL;
102 page = tmp;
103 }
104
105 /* add end of list marker for the returned list */
106 set_page_private(page, 0);
107 /* actual return value, and adjustment of head */
108 page = *head;
109 *head = tmp;
110 return page;
111}
112
113/* may be used outside of locks to find the tail of a (usually short)
114 * "private" page chain, before adding it back to a global chain head
115 * with page_chain_add() under a spinlock. */
116static struct page *page_chain_tail(struct page *page, int *len)
117{
118 struct page *tmp;
119 int i = 1;
120 while ((tmp = page_chain_next(page)))
121 ++i, page = tmp;
122 if (len)
123 *len = i;
124 return page;
125}
126
127static int page_chain_free(struct page *page)
128{
129 struct page *tmp;
130 int i = 0;
131 page_chain_for_each_safe(page, tmp) {
132 put_page(page);
133 ++i;
134 }
135 return i;
136}
137
138static void page_chain_add(struct page **head,
139 struct page *chain_first, struct page *chain_last)
140{
141#if 1
142 struct page *tmp;
143 tmp = page_chain_tail(chain_first, NULL);
144 BUG_ON(tmp != chain_last);
145#endif
146
147 /* add chain to head */
148 set_page_private(chain_last, (unsigned long)*head);
149 *head = chain_first;
150}
151
152static struct page *drbd_pp_first_pages_or_try_alloc(struct drbd_conf *mdev, int number)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700153{
154 struct page *page = NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200155 struct page *tmp = NULL;
156 int i = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700157
158 /* Yes, testing drbd_pp_vacant outside the lock is racy.
159 * So what. It saves a spin_lock. */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200160 if (drbd_pp_vacant >= number) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700161 spin_lock(&drbd_pp_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200162 page = page_chain_del(&drbd_pp_pool, number);
163 if (page)
164 drbd_pp_vacant -= number;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700165 spin_unlock(&drbd_pp_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200166 if (page)
167 return page;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700168 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200169
Philipp Reisnerb411b362009-09-25 16:07:19 -0700170 /* GFP_TRY, because we must not cause arbitrary write-out: in a DRBD
171 * "criss-cross" setup, that might cause write-out on some other DRBD,
172 * which in turn might block on the other node at this very place. */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200173 for (i = 0; i < number; i++) {
174 tmp = alloc_page(GFP_TRY);
175 if (!tmp)
176 break;
177 set_page_private(tmp, (unsigned long)page);
178 page = tmp;
179 }
180
181 if (i == number)
182 return page;
183
184 /* Not enough pages immediately available this time.
185 * No need to jump around here, drbd_pp_alloc will retry this
186 * function "soon". */
187 if (page) {
188 tmp = page_chain_tail(page, NULL);
189 spin_lock(&drbd_pp_lock);
190 page_chain_add(&drbd_pp_pool, page, tmp);
191 drbd_pp_vacant += i;
192 spin_unlock(&drbd_pp_lock);
193 }
194 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700195}
196
Philipp Reisnerb411b362009-09-25 16:07:19 -0700197static void reclaim_net_ee(struct drbd_conf *mdev, struct list_head *to_be_freed)
198{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100199 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700200 struct list_head *le, *tle;
201
202 /* The EEs are always appended to the end of the list. Since
203 they are sent in order over the wire, they have to finish
204 in order. As soon as we see the first not finished we can
205 stop to examine the list... */
206
207 list_for_each_safe(le, tle, &mdev->net_ee) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100208 peer_req = list_entry(le, struct drbd_peer_request, w.list);
209 if (drbd_ee_has_active_page(peer_req))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700210 break;
211 list_move(le, to_be_freed);
212 }
213}
214
215static void drbd_kick_lo_and_reclaim_net(struct drbd_conf *mdev)
216{
217 LIST_HEAD(reclaimed);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100218 struct drbd_peer_request *peer_req, *t;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700219
Philipp Reisner87eeee42011-01-19 14:16:30 +0100220 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700221 reclaim_net_ee(mdev, &reclaimed);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100222 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700223
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100224 list_for_each_entry_safe(peer_req, t, &reclaimed, w.list)
225 drbd_free_net_ee(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700226}
227
228/**
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200229 * drbd_pp_alloc() - Returns @number pages, retries forever (or until signalled)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700230 * @mdev: DRBD device.
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200231 * @number: number of pages requested
232 * @retry: whether to retry, if not enough pages are available right now
Philipp Reisnerb411b362009-09-25 16:07:19 -0700233 *
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200234 * Tries to allocate number pages, first from our own page pool, then from
235 * the kernel, unless this allocation would exceed the max_buffers setting.
236 * Possibly retry until DRBD frees sufficient pages somewhere else.
237 *
238 * Returns a page chain linked via page->private.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700239 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200240static struct page *drbd_pp_alloc(struct drbd_conf *mdev, unsigned number, bool retry)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700241{
242 struct page *page = NULL;
243 DEFINE_WAIT(wait);
244
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200245 /* Yes, we may run up to @number over max_buffers. If we
246 * follow it strictly, the admin will get it wrong anyways. */
Philipp Reisner89e58e72011-01-19 13:12:45 +0100247 if (atomic_read(&mdev->pp_in_use) < mdev->tconn->net_conf->max_buffers)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200248 page = drbd_pp_first_pages_or_try_alloc(mdev, number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700249
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200250 while (page == NULL) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700251 prepare_to_wait(&drbd_pp_wait, &wait, TASK_INTERRUPTIBLE);
252
253 drbd_kick_lo_and_reclaim_net(mdev);
254
Philipp Reisner89e58e72011-01-19 13:12:45 +0100255 if (atomic_read(&mdev->pp_in_use) < mdev->tconn->net_conf->max_buffers) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200256 page = drbd_pp_first_pages_or_try_alloc(mdev, number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700257 if (page)
258 break;
259 }
260
261 if (!retry)
262 break;
263
264 if (signal_pending(current)) {
265 dev_warn(DEV, "drbd_pp_alloc interrupted!\n");
266 break;
267 }
268
269 schedule();
270 }
271 finish_wait(&drbd_pp_wait, &wait);
272
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200273 if (page)
274 atomic_add(number, &mdev->pp_in_use);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700275 return page;
276}
277
278/* Must not be used from irq, as that may deadlock: see drbd_pp_alloc.
Philipp Reisner87eeee42011-01-19 14:16:30 +0100279 * Is also used from inside an other spin_lock_irq(&mdev->tconn->req_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200280 * Either links the page chain back to the global pool,
281 * or returns all pages to the system. */
Lars Ellenberg435f0742010-09-06 12:30:25 +0200282static void drbd_pp_free(struct drbd_conf *mdev, struct page *page, int is_net)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700283{
Lars Ellenberg435f0742010-09-06 12:30:25 +0200284 atomic_t *a = is_net ? &mdev->pp_in_use_by_net : &mdev->pp_in_use;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700285 int i;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200286
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100287 if (drbd_pp_vacant > (DRBD_MAX_BIO_SIZE/PAGE_SIZE)*minor_count)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200288 i = page_chain_free(page);
289 else {
290 struct page *tmp;
291 tmp = page_chain_tail(page, &i);
292 spin_lock(&drbd_pp_lock);
293 page_chain_add(&drbd_pp_pool, page, tmp);
294 drbd_pp_vacant += i;
295 spin_unlock(&drbd_pp_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700296 }
Lars Ellenberg435f0742010-09-06 12:30:25 +0200297 i = atomic_sub_return(i, a);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200298 if (i < 0)
Lars Ellenberg435f0742010-09-06 12:30:25 +0200299 dev_warn(DEV, "ASSERTION FAILED: %s: %d < 0\n",
300 is_net ? "pp_in_use_by_net" : "pp_in_use", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700301 wake_up(&drbd_pp_wait);
302}
303
304/*
305You need to hold the req_lock:
306 _drbd_wait_ee_list_empty()
307
308You must not have the req_lock:
309 drbd_free_ee()
310 drbd_alloc_ee()
311 drbd_init_ee()
312 drbd_release_ee()
313 drbd_ee_fix_bhs()
314 drbd_process_done_ee()
315 drbd_clear_done_ee()
316 drbd_wait_ee_list_empty()
317*/
318
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +0100319struct drbd_peer_request *
320drbd_alloc_ee(struct drbd_conf *mdev, u64 id, sector_t sector,
321 unsigned int data_size, gfp_t gfp_mask) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700322{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100323 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700324 struct page *page;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200325 unsigned nr_pages = (data_size + PAGE_SIZE -1) >> PAGE_SHIFT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700326
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100327 if (drbd_insert_fault(mdev, DRBD_FAULT_AL_EE))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700328 return NULL;
329
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100330 peer_req = mempool_alloc(drbd_ee_mempool, gfp_mask & ~__GFP_HIGHMEM);
331 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700332 if (!(gfp_mask & __GFP_NOWARN))
333 dev_err(DEV, "alloc_ee: Allocation of an EE failed\n");
334 return NULL;
335 }
336
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200337 page = drbd_pp_alloc(mdev, nr_pages, (gfp_mask & __GFP_WAIT));
338 if (!page)
339 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700340
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100341 drbd_clear_interval(&peer_req->i);
342 peer_req->i.size = data_size;
343 peer_req->i.sector = sector;
344 peer_req->i.local = false;
345 peer_req->i.waiting = false;
Andreas Gruenbacher53840642011-01-28 10:31:04 +0100346
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100347 peer_req->epoch = NULL;
348 peer_req->mdev = mdev;
349 peer_req->pages = page;
350 atomic_set(&peer_req->pending_bios, 0);
351 peer_req->flags = 0;
Andreas Gruenbacher9a8e7752011-01-11 14:04:09 +0100352 /*
353 * The block_id is opaque to the receiver. It is not endianness
354 * converted, and sent back to the sender unchanged.
355 */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100356 peer_req->block_id = id;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700357
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100358 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700359
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200360 fail:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100361 mempool_free(peer_req, drbd_ee_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700362 return NULL;
363}
364
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100365void drbd_free_some_ee(struct drbd_conf *mdev, struct drbd_peer_request *peer_req,
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +0100366 int is_net)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700367{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100368 if (peer_req->flags & EE_HAS_DIGEST)
369 kfree(peer_req->digest);
370 drbd_pp_free(mdev, peer_req->pages, is_net);
371 D_ASSERT(atomic_read(&peer_req->pending_bios) == 0);
372 D_ASSERT(drbd_interval_empty(&peer_req->i));
373 mempool_free(peer_req, drbd_ee_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700374}
375
376int drbd_release_ee(struct drbd_conf *mdev, struct list_head *list)
377{
378 LIST_HEAD(work_list);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100379 struct drbd_peer_request *peer_req, *t;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700380 int count = 0;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200381 int is_net = list == &mdev->net_ee;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700382
Philipp Reisner87eeee42011-01-19 14:16:30 +0100383 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700384 list_splice_init(list, &work_list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100385 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700386
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100387 list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
388 drbd_free_some_ee(mdev, peer_req, is_net);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700389 count++;
390 }
391 return count;
392}
393
394
395/*
396 * This function is called from _asender only_
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100397 * but see also comments in _req_mod(,BARRIER_ACKED)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700398 * and receive_Barrier.
399 *
400 * Move entries from net_ee to done_ee, if ready.
401 * Grab done_ee, call all callbacks, free the entries.
402 * The callbacks typically send out ACKs.
403 */
404static int drbd_process_done_ee(struct drbd_conf *mdev)
405{
406 LIST_HEAD(work_list);
407 LIST_HEAD(reclaimed);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100408 struct drbd_peer_request *peer_req, *t;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700409 int ok = (mdev->state.conn >= C_WF_REPORT_PARAMS);
410
Philipp Reisner87eeee42011-01-19 14:16:30 +0100411 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700412 reclaim_net_ee(mdev, &reclaimed);
413 list_splice_init(&mdev->done_ee, &work_list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100414 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700415
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100416 list_for_each_entry_safe(peer_req, t, &reclaimed, w.list)
417 drbd_free_net_ee(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700418
419 /* possible callbacks here:
420 * e_end_block, and e_end_resync_block, e_send_discard_ack.
421 * all ignore the last argument.
422 */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100423 list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700424 /* list_del not necessary, next/prev members not touched */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100425 ok = peer_req->w.cb(mdev, &peer_req->w, !ok) && ok;
426 drbd_free_ee(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700427 }
428 wake_up(&mdev->ee_wait);
429
430 return ok;
431}
432
433void _drbd_wait_ee_list_empty(struct drbd_conf *mdev, struct list_head *head)
434{
435 DEFINE_WAIT(wait);
436
437 /* avoids spin_lock/unlock
438 * and calling prepare_to_wait in the fast path */
439 while (!list_empty(head)) {
440 prepare_to_wait(&mdev->ee_wait, &wait, TASK_UNINTERRUPTIBLE);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100441 spin_unlock_irq(&mdev->tconn->req_lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100442 io_schedule();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700443 finish_wait(&mdev->ee_wait, &wait);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100444 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700445 }
446}
447
448void drbd_wait_ee_list_empty(struct drbd_conf *mdev, struct list_head *head)
449{
Philipp Reisner87eeee42011-01-19 14:16:30 +0100450 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700451 _drbd_wait_ee_list_empty(mdev, head);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100452 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700453}
454
455/* see also kernel_accept; which is only present since 2.6.18.
456 * also we want to log which part of it failed, exactly */
Philipp Reisner76536202011-02-07 14:09:54 +0100457static int drbd_accept(const char **what, struct socket *sock, struct socket **newsock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700458{
459 struct sock *sk = sock->sk;
460 int err = 0;
461
462 *what = "listen";
463 err = sock->ops->listen(sock, 5);
464 if (err < 0)
465 goto out;
466
467 *what = "sock_create_lite";
468 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
469 newsock);
470 if (err < 0)
471 goto out;
472
473 *what = "accept";
474 err = sock->ops->accept(sock, *newsock, 0);
475 if (err < 0) {
476 sock_release(*newsock);
477 *newsock = NULL;
478 goto out;
479 }
480 (*newsock)->ops = sock->ops;
481
482out:
483 return err;
484}
485
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100486static int drbd_recv_short(struct socket *sock, void *buf, size_t size, int flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700487{
488 mm_segment_t oldfs;
489 struct kvec iov = {
490 .iov_base = buf,
491 .iov_len = size,
492 };
493 struct msghdr msg = {
494 .msg_iovlen = 1,
495 .msg_iov = (struct iovec *)&iov,
496 .msg_flags = (flags ? flags : MSG_WAITALL | MSG_NOSIGNAL)
497 };
498 int rv;
499
500 oldfs = get_fs();
501 set_fs(KERNEL_DS);
502 rv = sock_recvmsg(sock, &msg, size, msg.msg_flags);
503 set_fs(oldfs);
504
505 return rv;
506}
507
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100508static int drbd_recv(struct drbd_tconn *tconn, void *buf, size_t size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700509{
510 mm_segment_t oldfs;
511 struct kvec iov = {
512 .iov_base = buf,
513 .iov_len = size,
514 };
515 struct msghdr msg = {
516 .msg_iovlen = 1,
517 .msg_iov = (struct iovec *)&iov,
518 .msg_flags = MSG_WAITALL | MSG_NOSIGNAL
519 };
520 int rv;
521
522 oldfs = get_fs();
523 set_fs(KERNEL_DS);
524
525 for (;;) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100526 rv = sock_recvmsg(tconn->data.socket, &msg, size, msg.msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700527 if (rv == size)
528 break;
529
530 /* Note:
531 * ECONNRESET other side closed the connection
532 * ERESTARTSYS (on sock) we got a signal
533 */
534
535 if (rv < 0) {
536 if (rv == -ECONNRESET)
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100537 conn_info(tconn, "sock was reset by peer\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700538 else if (rv != -ERESTARTSYS)
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100539 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700540 break;
541 } else if (rv == 0) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100542 conn_info(tconn, "sock was shut down by peer\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700543 break;
544 } else {
545 /* signal came in, or peer/link went down,
546 * after we read a partial message
547 */
548 /* D_ASSERT(signal_pending(current)); */
549 break;
550 }
551 };
552
553 set_fs(oldfs);
554
555 if (rv != size)
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100556 drbd_force_state(tconn->volume0, NS(conn, C_BROKEN_PIPE));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700557
558 return rv;
559}
560
Lars Ellenberg5dbf1672010-05-25 16:18:01 +0200561/* quoting tcp(7):
562 * On individual connections, the socket buffer size must be set prior to the
563 * listen(2) or connect(2) calls in order to have it take effect.
564 * This is our wrapper to do so.
565 */
566static void drbd_setbufsize(struct socket *sock, unsigned int snd,
567 unsigned int rcv)
568{
569 /* open coded SO_SNDBUF, SO_RCVBUF */
570 if (snd) {
571 sock->sk->sk_sndbuf = snd;
572 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
573 }
574 if (rcv) {
575 sock->sk->sk_rcvbuf = rcv;
576 sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
577 }
578}
579
Philipp Reisnereac3e992011-02-07 14:05:07 +0100580static struct socket *drbd_try_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700581{
582 const char *what;
583 struct socket *sock;
584 struct sockaddr_in6 src_in6;
585 int err;
586 int disconnect_on_error = 1;
587
Philipp Reisnereac3e992011-02-07 14:05:07 +0100588 if (!get_net_conf(tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700589 return NULL;
590
591 what = "sock_create_kern";
Philipp Reisnereac3e992011-02-07 14:05:07 +0100592 err = sock_create_kern(((struct sockaddr *)tconn->net_conf->my_addr)->sa_family,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700593 SOCK_STREAM, IPPROTO_TCP, &sock);
594 if (err < 0) {
595 sock = NULL;
596 goto out;
597 }
598
599 sock->sk->sk_rcvtimeo =
Philipp Reisnereac3e992011-02-07 14:05:07 +0100600 sock->sk->sk_sndtimeo = tconn->net_conf->try_connect_int*HZ;
601 drbd_setbufsize(sock, tconn->net_conf->sndbuf_size,
602 tconn->net_conf->rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700603
604 /* explicitly bind to the configured IP as source IP
605 * for the outgoing connections.
606 * This is needed for multihomed hosts and to be
607 * able to use lo: interfaces for drbd.
608 * Make sure to use 0 as port number, so linux selects
609 * a free one dynamically.
610 */
Philipp Reisnereac3e992011-02-07 14:05:07 +0100611 memcpy(&src_in6, tconn->net_conf->my_addr,
612 min_t(int, tconn->net_conf->my_addr_len, sizeof(src_in6)));
613 if (((struct sockaddr *)tconn->net_conf->my_addr)->sa_family == AF_INET6)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700614 src_in6.sin6_port = 0;
615 else
616 ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */
617
618 what = "bind before connect";
619 err = sock->ops->bind(sock,
620 (struct sockaddr *) &src_in6,
Philipp Reisnereac3e992011-02-07 14:05:07 +0100621 tconn->net_conf->my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700622 if (err < 0)
623 goto out;
624
625 /* connect may fail, peer not yet available.
626 * stay C_WF_CONNECTION, don't go Disconnecting! */
627 disconnect_on_error = 0;
628 what = "connect";
629 err = sock->ops->connect(sock,
Philipp Reisnereac3e992011-02-07 14:05:07 +0100630 (struct sockaddr *)tconn->net_conf->peer_addr,
631 tconn->net_conf->peer_addr_len, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700632
633out:
634 if (err < 0) {
635 if (sock) {
636 sock_release(sock);
637 sock = NULL;
638 }
639 switch (-err) {
640 /* timeout, busy, signal pending */
641 case ETIMEDOUT: case EAGAIN: case EINPROGRESS:
642 case EINTR: case ERESTARTSYS:
643 /* peer not (yet) available, network problem */
644 case ECONNREFUSED: case ENETUNREACH:
645 case EHOSTDOWN: case EHOSTUNREACH:
646 disconnect_on_error = 0;
647 break;
648 default:
Philipp Reisnereac3e992011-02-07 14:05:07 +0100649 conn_err(tconn, "%s failed, err = %d\n", what, err);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700650 }
651 if (disconnect_on_error)
Philipp Reisnereac3e992011-02-07 14:05:07 +0100652 drbd_force_state(tconn->volume0, NS(conn, C_DISCONNECTING));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700653 }
Philipp Reisnereac3e992011-02-07 14:05:07 +0100654 put_net_conf(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700655 return sock;
656}
657
Philipp Reisner76536202011-02-07 14:09:54 +0100658static struct socket *drbd_wait_for_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700659{
660 int timeo, err;
661 struct socket *s_estab = NULL, *s_listen;
662 const char *what;
663
Philipp Reisner76536202011-02-07 14:09:54 +0100664 if (!get_net_conf(tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700665 return NULL;
666
667 what = "sock_create_kern";
Philipp Reisner76536202011-02-07 14:09:54 +0100668 err = sock_create_kern(((struct sockaddr *)tconn->net_conf->my_addr)->sa_family,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700669 SOCK_STREAM, IPPROTO_TCP, &s_listen);
670 if (err) {
671 s_listen = NULL;
672 goto out;
673 }
674
Philipp Reisner76536202011-02-07 14:09:54 +0100675 timeo = tconn->net_conf->try_connect_int * HZ;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700676 timeo += (random32() & 1) ? timeo / 7 : -timeo / 7; /* 28.5% random jitter */
677
678 s_listen->sk->sk_reuse = 1; /* SO_REUSEADDR */
679 s_listen->sk->sk_rcvtimeo = timeo;
680 s_listen->sk->sk_sndtimeo = timeo;
Philipp Reisner76536202011-02-07 14:09:54 +0100681 drbd_setbufsize(s_listen, tconn->net_conf->sndbuf_size,
682 tconn->net_conf->rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700683
684 what = "bind before listen";
685 err = s_listen->ops->bind(s_listen,
Philipp Reisner76536202011-02-07 14:09:54 +0100686 (struct sockaddr *) tconn->net_conf->my_addr,
687 tconn->net_conf->my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700688 if (err < 0)
689 goto out;
690
Philipp Reisner76536202011-02-07 14:09:54 +0100691 err = drbd_accept(&what, s_listen, &s_estab);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700692
693out:
694 if (s_listen)
695 sock_release(s_listen);
696 if (err < 0) {
697 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
Philipp Reisner76536202011-02-07 14:09:54 +0100698 conn_err(tconn, "%s failed, err = %d\n", what, err);
699 drbd_force_state(tconn->volume0, NS(conn, C_DISCONNECTING));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700700 }
701 }
Philipp Reisner76536202011-02-07 14:09:54 +0100702 put_net_conf(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700703
704 return s_estab;
705}
706
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100707static int drbd_send_fp(struct drbd_tconn *tconn, struct socket *sock, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700708{
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100709 struct p_header *h = &tconn->data.sbuf.header;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700710
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100711 return _conn_send_cmd(tconn, 0, sock, cmd, h, sizeof(*h), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700712}
713
Philipp Reisnera25b63f2011-02-07 15:43:45 +0100714static enum drbd_packet drbd_recv_fp(struct drbd_tconn *tconn, struct socket *sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700715{
Philipp Reisnera25b63f2011-02-07 15:43:45 +0100716 struct p_header80 *h = &tconn->data.rbuf.header.h80;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700717 int rr;
718
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100719 rr = drbd_recv_short(sock, h, sizeof(*h), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700720
Andreas Gruenbacherca9bc122011-01-11 13:47:24 +0100721 if (rr == sizeof(*h) && h->magic == cpu_to_be32(DRBD_MAGIC))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700722 return be16_to_cpu(h->command);
723
724 return 0xffff;
725}
726
727/**
728 * drbd_socket_okay() - Free the socket if its connection is not okay
Philipp Reisnerb411b362009-09-25 16:07:19 -0700729 * @sock: pointer to the pointer to the socket.
730 */
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100731static int drbd_socket_okay(struct socket **sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700732{
733 int rr;
734 char tb[4];
735
736 if (!*sock)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100737 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700738
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100739 rr = drbd_recv_short(*sock, tb, 4, MSG_DONTWAIT | MSG_PEEK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700740
741 if (rr > 0 || rr == -EAGAIN) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100742 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700743 } else {
744 sock_release(*sock);
745 *sock = NULL;
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100746 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700747 }
748}
749
Philipp Reisner907599e2011-02-08 11:25:37 +0100750static int drbd_connected(int vnr, void *p, void *data)
751{
752 struct drbd_conf *mdev = (struct drbd_conf *)p;
753 int ok = 1;
754
755 atomic_set(&mdev->packet_seq, 0);
756 mdev->peer_seq = 0;
757
758 ok &= drbd_send_sync_param(mdev, &mdev->sync_conf);
759 ok &= drbd_send_sizes(mdev, 0, 0);
760 ok &= drbd_send_uuids(mdev);
761 ok &= drbd_send_state(mdev);
762 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
763 clear_bit(RESIZE_PENDING, &mdev->flags);
764
765 return !ok;
766}
767
Philipp Reisnerb411b362009-09-25 16:07:19 -0700768/*
769 * return values:
770 * 1 yes, we have a valid connection
771 * 0 oops, did not work out, please try again
772 * -1 peer talks different language,
773 * no point in trying again, please go standalone.
774 * -2 We do not have a network config...
775 */
Philipp Reisner907599e2011-02-08 11:25:37 +0100776static int drbd_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700777{
778 struct socket *s, *sock, *msock;
779 int try, h, ok;
780
Philipp Reisner907599e2011-02-08 11:25:37 +0100781 if (drbd_request_state(tconn->volume0, NS(conn, C_WF_CONNECTION)) < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700782 return -2;
783
Philipp Reisner907599e2011-02-08 11:25:37 +0100784 clear_bit(DISCARD_CONCURRENT, &tconn->flags);
785 tconn->agreed_pro_version = 99;
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100786 /* agreed_pro_version must be smaller than 100 so we send the old
787 header (h80) in the first packet and in the handshake packet. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700788
789 sock = NULL;
790 msock = NULL;
791
792 do {
793 for (try = 0;;) {
794 /* 3 tries, this should take less than a second! */
Philipp Reisner907599e2011-02-08 11:25:37 +0100795 s = drbd_try_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700796 if (s || ++try >= 3)
797 break;
798 /* give the other side time to call bind() & listen() */
Philipp Reisner20ee6392011-01-18 15:28:59 +0100799 schedule_timeout_interruptible(HZ / 10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700800 }
801
802 if (s) {
803 if (!sock) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100804 drbd_send_fp(tconn, s, P_HAND_SHAKE_S);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700805 sock = s;
806 s = NULL;
807 } else if (!msock) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100808 drbd_send_fp(tconn, s, P_HAND_SHAKE_M);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700809 msock = s;
810 s = NULL;
811 } else {
Philipp Reisner907599e2011-02-08 11:25:37 +0100812 conn_err(tconn, "Logic error in drbd_connect()\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700813 goto out_release_sockets;
814 }
815 }
816
817 if (sock && msock) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100818 schedule_timeout_interruptible(tconn->net_conf->ping_timeo*HZ/10);
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100819 ok = drbd_socket_okay(&sock);
820 ok = drbd_socket_okay(&msock) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700821 if (ok)
822 break;
823 }
824
825retry:
Philipp Reisner907599e2011-02-08 11:25:37 +0100826 s = drbd_wait_for_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700827 if (s) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100828 try = drbd_recv_fp(tconn, s);
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100829 drbd_socket_okay(&sock);
830 drbd_socket_okay(&msock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700831 switch (try) {
832 case P_HAND_SHAKE_S:
833 if (sock) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100834 conn_warn(tconn, "initial packet S crossed\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700835 sock_release(sock);
836 }
837 sock = s;
838 break;
839 case P_HAND_SHAKE_M:
840 if (msock) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100841 conn_warn(tconn, "initial packet M crossed\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700842 sock_release(msock);
843 }
844 msock = s;
Philipp Reisner907599e2011-02-08 11:25:37 +0100845 set_bit(DISCARD_CONCURRENT, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700846 break;
847 default:
Philipp Reisner907599e2011-02-08 11:25:37 +0100848 conn_warn(tconn, "Error receiving initial packet\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700849 sock_release(s);
850 if (random32() & 1)
851 goto retry;
852 }
853 }
854
Philipp Reisner907599e2011-02-08 11:25:37 +0100855 if (tconn->volume0->state.conn <= C_DISCONNECTING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700856 goto out_release_sockets;
857 if (signal_pending(current)) {
858 flush_signals(current);
859 smp_rmb();
Philipp Reisner907599e2011-02-08 11:25:37 +0100860 if (get_t_state(&tconn->receiver) == EXITING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700861 goto out_release_sockets;
862 }
863
864 if (sock && msock) {
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100865 ok = drbd_socket_okay(&sock);
866 ok = drbd_socket_okay(&msock) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700867 if (ok)
868 break;
869 }
870 } while (1);
871
872 msock->sk->sk_reuse = 1; /* SO_REUSEADDR */
873 sock->sk->sk_reuse = 1; /* SO_REUSEADDR */
874
875 sock->sk->sk_allocation = GFP_NOIO;
876 msock->sk->sk_allocation = GFP_NOIO;
877
878 sock->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
879 msock->sk->sk_priority = TC_PRIO_INTERACTIVE;
880
Philipp Reisnerb411b362009-09-25 16:07:19 -0700881 /* NOT YET ...
Philipp Reisner907599e2011-02-08 11:25:37 +0100882 * sock->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700883 * sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
884 * first set it to the P_HAND_SHAKE timeout,
885 * which we set to 4x the configured ping_timeout. */
886 sock->sk->sk_sndtimeo =
Philipp Reisner907599e2011-02-08 11:25:37 +0100887 sock->sk->sk_rcvtimeo = tconn->net_conf->ping_timeo*4*HZ/10;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700888
Philipp Reisner907599e2011-02-08 11:25:37 +0100889 msock->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
890 msock->sk->sk_rcvtimeo = tconn->net_conf->ping_int*HZ;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700891
892 /* we don't want delays.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300893 * we use TCP_CORK where appropriate, though */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700894 drbd_tcp_nodelay(sock);
895 drbd_tcp_nodelay(msock);
896
Philipp Reisner907599e2011-02-08 11:25:37 +0100897 tconn->data.socket = sock;
898 tconn->meta.socket = msock;
899 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700900
Philipp Reisner907599e2011-02-08 11:25:37 +0100901 h = drbd_do_handshake(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700902 if (h <= 0)
903 return h;
904
Philipp Reisner907599e2011-02-08 11:25:37 +0100905 if (tconn->cram_hmac_tfm) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700906 /* drbd_request_state(mdev, NS(conn, WFAuth)); */
Philipp Reisner907599e2011-02-08 11:25:37 +0100907 switch (drbd_do_auth(tconn)) {
Johannes Thomab10d96c2010-01-07 16:02:50 +0100908 case -1:
Philipp Reisner907599e2011-02-08 11:25:37 +0100909 conn_err(tconn, "Authentication of peer failed\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700910 return -1;
Johannes Thomab10d96c2010-01-07 16:02:50 +0100911 case 0:
Philipp Reisner907599e2011-02-08 11:25:37 +0100912 conn_err(tconn, "Authentication of peer failed, trying again.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +0100913 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700914 }
915 }
916
Philipp Reisner907599e2011-02-08 11:25:37 +0100917 if (drbd_request_state(tconn->volume0, NS(conn, C_WF_REPORT_PARAMS)) < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700918 return 0;
919
Philipp Reisner907599e2011-02-08 11:25:37 +0100920 sock->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700921 sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
922
Philipp Reisner907599e2011-02-08 11:25:37 +0100923 drbd_thread_start(&tconn->asender);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700924
Philipp Reisner907599e2011-02-08 11:25:37 +0100925 if (drbd_send_protocol(tconn) == -1)
Philipp Reisner7e2455c2010-04-22 14:50:23 +0200926 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700927
Philipp Reisner907599e2011-02-08 11:25:37 +0100928 return !idr_for_each(&tconn->volumes, drbd_connected, tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700929
930out_release_sockets:
931 if (sock)
932 sock_release(sock);
933 if (msock)
934 sock_release(msock);
935 return -1;
936}
937
Philipp Reisnerce243852011-02-07 17:27:47 +0100938static bool decode_header(struct drbd_tconn *tconn, struct p_header *h, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700939{
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100940 if (h->h80.magic == cpu_to_be32(DRBD_MAGIC)) {
Philipp Reisner77351055b2011-02-07 17:24:26 +0100941 pi->cmd = be16_to_cpu(h->h80.command);
942 pi->size = be16_to_cpu(h->h80.length);
Philipp Reisnereefc2f72011-02-08 12:55:24 +0100943 pi->vnr = 0;
Andreas Gruenbacherca9bc122011-01-11 13:47:24 +0100944 } else if (h->h95.magic == cpu_to_be16(DRBD_MAGIC_BIG)) {
Philipp Reisner77351055b2011-02-07 17:24:26 +0100945 pi->cmd = be16_to_cpu(h->h95.command);
946 pi->size = be32_to_cpu(h->h95.length) & 0x00ffffff;
947 pi->vnr = 0;
Philipp Reisner02918be2010-08-20 14:35:10 +0200948 } else {
Philipp Reisnerce243852011-02-07 17:27:47 +0100949 conn_err(tconn, "magic?? on data m: 0x%08x c: %d l: %d\n",
Lars Ellenberg004352f2010-10-05 20:13:58 +0200950 be32_to_cpu(h->h80.magic),
951 be16_to_cpu(h->h80.command),
952 be16_to_cpu(h->h80.length));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100953 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700954 }
Philipp Reisner257d0af2011-01-26 12:15:29 +0100955 return true;
956}
957
Philipp Reisner9ba7aa02011-02-07 17:32:41 +0100958static int drbd_recv_header(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner257d0af2011-01-26 12:15:29 +0100959{
Philipp Reisner9ba7aa02011-02-07 17:32:41 +0100960 struct p_header *h = &tconn->data.rbuf.header;
Philipp Reisner257d0af2011-01-26 12:15:29 +0100961 int r;
962
Philipp Reisner9ba7aa02011-02-07 17:32:41 +0100963 r = drbd_recv(tconn, h, sizeof(*h));
Philipp Reisner257d0af2011-01-26 12:15:29 +0100964 if (unlikely(r != sizeof(*h))) {
965 if (!signal_pending(current))
Philipp Reisner9ba7aa02011-02-07 17:32:41 +0100966 conn_warn(tconn, "short read expecting header on sock: r=%d\n", r);
Philipp Reisner257d0af2011-01-26 12:15:29 +0100967 return false;
968 }
969
Philipp Reisner9ba7aa02011-02-07 17:32:41 +0100970 r = decode_header(tconn, h, pi);
971 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700972
Philipp Reisner257d0af2011-01-26 12:15:29 +0100973 return r;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700974}
975
Philipp Reisner2451fc32010-08-24 13:43:11 +0200976static void drbd_flush(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700977{
978 int rv;
979
980 if (mdev->write_ordering >= WO_bdev_flush && get_ldev(mdev)) {
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +0400981 rv = blkdev_issue_flush(mdev->ldev->backing_bdev, GFP_KERNEL,
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200982 NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700983 if (rv) {
984 dev_err(DEV, "local disk flush failed with status %d\n", rv);
985 /* would rather check on EOPNOTSUPP, but that is not reliable.
986 * don't try again for ANY return value != 0
987 * if (rv == -EOPNOTSUPP) */
988 drbd_bump_write_ordering(mdev, WO_drain_io);
989 }
990 put_ldev(mdev);
991 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700992}
993
994/**
995 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
996 * @mdev: DRBD device.
997 * @epoch: Epoch object.
998 * @ev: Epoch event.
999 */
1000static enum finish_epoch drbd_may_finish_epoch(struct drbd_conf *mdev,
1001 struct drbd_epoch *epoch,
1002 enum epoch_event ev)
1003{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001004 int epoch_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001005 struct drbd_epoch *next_epoch;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001006 enum finish_epoch rv = FE_STILL_LIVE;
1007
1008 spin_lock(&mdev->epoch_lock);
1009 do {
1010 next_epoch = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001011
1012 epoch_size = atomic_read(&epoch->epoch_size);
1013
1014 switch (ev & ~EV_CLEANUP) {
1015 case EV_PUT:
1016 atomic_dec(&epoch->active);
1017 break;
1018 case EV_GOT_BARRIER_NR:
1019 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001020 break;
1021 case EV_BECAME_LAST:
1022 /* nothing to do*/
1023 break;
1024 }
1025
Philipp Reisnerb411b362009-09-25 16:07:19 -07001026 if (epoch_size != 0 &&
1027 atomic_read(&epoch->active) == 0 &&
Philipp Reisner2451fc32010-08-24 13:43:11 +02001028 test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001029 if (!(ev & EV_CLEANUP)) {
1030 spin_unlock(&mdev->epoch_lock);
1031 drbd_send_b_ack(mdev, epoch->barrier_nr, epoch_size);
1032 spin_lock(&mdev->epoch_lock);
1033 }
1034 dec_unacked(mdev);
1035
1036 if (mdev->current_epoch != epoch) {
1037 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1038 list_del(&epoch->list);
1039 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
1040 mdev->epochs--;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001041 kfree(epoch);
1042
1043 if (rv == FE_STILL_LIVE)
1044 rv = FE_DESTROYED;
1045 } else {
1046 epoch->flags = 0;
1047 atomic_set(&epoch->epoch_size, 0);
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001048 /* atomic_set(&epoch->active, 0); is already zero */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001049 if (rv == FE_STILL_LIVE)
1050 rv = FE_RECYCLED;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001051 wake_up(&mdev->ee_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001052 }
1053 }
1054
1055 if (!next_epoch)
1056 break;
1057
1058 epoch = next_epoch;
1059 } while (1);
1060
1061 spin_unlock(&mdev->epoch_lock);
1062
Philipp Reisnerb411b362009-09-25 16:07:19 -07001063 return rv;
1064}
1065
1066/**
1067 * drbd_bump_write_ordering() - Fall back to an other write ordering method
1068 * @mdev: DRBD device.
1069 * @wo: Write ordering method to try.
1070 */
1071void drbd_bump_write_ordering(struct drbd_conf *mdev, enum write_ordering_e wo) __must_hold(local)
1072{
1073 enum write_ordering_e pwo;
1074 static char *write_ordering_str[] = {
1075 [WO_none] = "none",
1076 [WO_drain_io] = "drain",
1077 [WO_bdev_flush] = "flush",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001078 };
1079
1080 pwo = mdev->write_ordering;
1081 wo = min(pwo, wo);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001082 if (wo == WO_bdev_flush && mdev->ldev->dc.no_disk_flush)
1083 wo = WO_drain_io;
1084 if (wo == WO_drain_io && mdev->ldev->dc.no_disk_drain)
1085 wo = WO_none;
1086 mdev->write_ordering = wo;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001087 if (pwo != mdev->write_ordering || wo == WO_bdev_flush)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001088 dev_info(DEV, "Method to ensure write ordering: %s\n", write_ordering_str[mdev->write_ordering]);
1089}
1090
1091/**
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001092 * drbd_submit_ee()
1093 * @mdev: DRBD device.
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001094 * @peer_req: peer request
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001095 * @rw: flag field, see bio->bi_rw
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001096 *
1097 * May spread the pages to multiple bios,
1098 * depending on bio_add_page restrictions.
1099 *
1100 * Returns 0 if all bios have been submitted,
1101 * -ENOMEM if we could not allocate enough bios,
1102 * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a
1103 * single page to an empty bio (which should never happen and likely indicates
1104 * that the lower level IO stack is in some way broken). This has been observed
1105 * on certain Xen deployments.
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001106 */
1107/* TODO allocate from our own bio_set. */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001108int drbd_submit_ee(struct drbd_conf *mdev, struct drbd_peer_request *peer_req,
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001109 const unsigned rw, const int fault_type)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001110{
1111 struct bio *bios = NULL;
1112 struct bio *bio;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001113 struct page *page = peer_req->pages;
1114 sector_t sector = peer_req->i.sector;
1115 unsigned ds = peer_req->i.size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001116 unsigned n_bios = 0;
1117 unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001118 int err = -ENOMEM;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001119
1120 /* In most cases, we will only need one bio. But in case the lower
1121 * level restrictions happen to be different at this offset on this
1122 * side than those of the sending peer, we may need to submit the
1123 * request in more than one bio. */
1124next_bio:
1125 bio = bio_alloc(GFP_NOIO, nr_pages);
1126 if (!bio) {
1127 dev_err(DEV, "submit_ee: Allocation of a bio failed\n");
1128 goto fail;
1129 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001130 /* > peer_req->i.sector, unless this is the first bio */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001131 bio->bi_sector = sector;
1132 bio->bi_bdev = mdev->ldev->backing_bdev;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001133 bio->bi_rw = rw;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001134 bio->bi_private = peer_req;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001135 bio->bi_end_io = drbd_endio_sec;
1136
1137 bio->bi_next = bios;
1138 bios = bio;
1139 ++n_bios;
1140
1141 page_chain_for_each(page) {
1142 unsigned len = min_t(unsigned, ds, PAGE_SIZE);
1143 if (!bio_add_page(bio, page, len, 0)) {
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001144 /* A single page must always be possible!
1145 * But in case it fails anyways,
1146 * we deal with it, and complain (below). */
1147 if (bio->bi_vcnt == 0) {
1148 dev_err(DEV,
1149 "bio_add_page failed for len=%u, "
1150 "bi_vcnt=0 (bi_sector=%llu)\n",
1151 len, (unsigned long long)bio->bi_sector);
1152 err = -ENOSPC;
1153 goto fail;
1154 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001155 goto next_bio;
1156 }
1157 ds -= len;
1158 sector += len >> 9;
1159 --nr_pages;
1160 }
1161 D_ASSERT(page == NULL);
1162 D_ASSERT(ds == 0);
1163
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001164 atomic_set(&peer_req->pending_bios, n_bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001165 do {
1166 bio = bios;
1167 bios = bios->bi_next;
1168 bio->bi_next = NULL;
1169
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001170 drbd_generic_make_request(mdev, fault_type, bio);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001171 } while (bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001172 return 0;
1173
1174fail:
1175 while (bios) {
1176 bio = bios;
1177 bios = bios->bi_next;
1178 bio_put(bio);
1179 }
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001180 return err;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001181}
1182
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001183static void drbd_remove_epoch_entry_interval(struct drbd_conf *mdev,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001184 struct drbd_peer_request *peer_req)
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001185{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001186 struct drbd_interval *i = &peer_req->i;
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001187
1188 drbd_remove_interval(&mdev->write_requests, i);
1189 drbd_clear_interval(i);
1190
Andreas Gruenbacher6c852be2011-02-04 15:38:52 +01001191 /* Wake up any processes waiting for this peer request to complete. */
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001192 if (i->waiting)
1193 wake_up(&mdev->misc_wait);
1194}
1195
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001196static int receive_Barrier(struct drbd_conf *mdev, enum drbd_packet cmd,
1197 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001198{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001199 int rv;
Philipp Reisnere42325a2011-01-19 13:55:45 +01001200 struct p_barrier *p = &mdev->tconn->data.rbuf.barrier;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001201 struct drbd_epoch *epoch;
1202
Philipp Reisnerb411b362009-09-25 16:07:19 -07001203 inc_unacked(mdev);
1204
Philipp Reisnerb411b362009-09-25 16:07:19 -07001205 mdev->current_epoch->barrier_nr = p->barrier;
1206 rv = drbd_may_finish_epoch(mdev, mdev->current_epoch, EV_GOT_BARRIER_NR);
1207
1208 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
1209 * the activity log, which means it would not be resynced in case the
1210 * R_PRIMARY crashes now.
1211 * Therefore we must send the barrier_ack after the barrier request was
1212 * completed. */
1213 switch (mdev->write_ordering) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001214 case WO_none:
1215 if (rv == FE_RECYCLED)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001216 return true;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001217
1218 /* receiver context, in the writeout path of the other node.
1219 * avoid potential distributed deadlock */
1220 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1221 if (epoch)
1222 break;
1223 else
1224 dev_warn(DEV, "Allocation of an epoch failed, slowing down\n");
1225 /* Fall through */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001226
1227 case WO_bdev_flush:
1228 case WO_drain_io:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001229 drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
Philipp Reisner2451fc32010-08-24 13:43:11 +02001230 drbd_flush(mdev);
1231
1232 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1233 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1234 if (epoch)
1235 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001236 }
1237
Philipp Reisner2451fc32010-08-24 13:43:11 +02001238 epoch = mdev->current_epoch;
1239 wait_event(mdev->ee_wait, atomic_read(&epoch->epoch_size) == 0);
1240
1241 D_ASSERT(atomic_read(&epoch->active) == 0);
1242 D_ASSERT(epoch->flags == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001243
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001244 return true;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001245 default:
1246 dev_err(DEV, "Strangeness in mdev->write_ordering %d\n", mdev->write_ordering);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001247 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001248 }
1249
1250 epoch->flags = 0;
1251 atomic_set(&epoch->epoch_size, 0);
1252 atomic_set(&epoch->active, 0);
1253
1254 spin_lock(&mdev->epoch_lock);
1255 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1256 list_add(&epoch->list, &mdev->current_epoch->list);
1257 mdev->current_epoch = epoch;
1258 mdev->epochs++;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001259 } else {
1260 /* The current_epoch got recycled while we allocated this one... */
1261 kfree(epoch);
1262 }
1263 spin_unlock(&mdev->epoch_lock);
1264
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001265 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001266}
1267
1268/* used from receive_RSDataReply (recv_resync_read)
1269 * and from receive_Data */
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001270static struct drbd_peer_request *
1271read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector,
1272 int data_size) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001273{
Lars Ellenberg66660322010-04-06 12:15:04 +02001274 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001275 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001276 struct page *page;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001277 int dgs, ds, rr;
Philipp Reisnera0638452011-01-19 14:31:32 +01001278 void *dig_in = mdev->tconn->int_dig_in;
1279 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001280 unsigned long *data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001281
Philipp Reisnera0638452011-01-19 14:31:32 +01001282 dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_r_tfm) ?
1283 crypto_hash_digestsize(mdev->tconn->integrity_r_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001284
1285 if (dgs) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +01001286 rr = drbd_recv(mdev->tconn, dig_in, dgs);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001287 if (rr != dgs) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01001288 if (!signal_pending(current))
1289 dev_warn(DEV,
1290 "short read receiving data digest: read %d expected %d\n",
1291 rr, dgs);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001292 return NULL;
1293 }
1294 }
1295
1296 data_size -= dgs;
1297
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001298 if (!expect(data_size != 0))
1299 return NULL;
1300 if (!expect(IS_ALIGNED(data_size, 512)))
1301 return NULL;
1302 if (!expect(data_size <= DRBD_MAX_BIO_SIZE))
1303 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001304
Lars Ellenberg66660322010-04-06 12:15:04 +02001305 /* even though we trust out peer,
1306 * we sometimes have to double check. */
1307 if (sector + (data_size>>9) > capacity) {
Lars Ellenbergfdda6542011-01-24 15:11:01 +01001308 dev_err(DEV, "request from peer beyond end of local disk: "
1309 "capacity: %llus < sector: %llus + size: %u\n",
Lars Ellenberg66660322010-04-06 12:15:04 +02001310 (unsigned long long)capacity,
1311 (unsigned long long)sector, data_size);
1312 return NULL;
1313 }
1314
Philipp Reisnerb411b362009-09-25 16:07:19 -07001315 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1316 * "criss-cross" setup, that might cause write-out on some other DRBD,
1317 * which in turn might block on the other node at this very place. */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001318 peer_req = drbd_alloc_ee(mdev, id, sector, data_size, GFP_NOIO);
1319 if (!peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001320 return NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001321
Philipp Reisnerb411b362009-09-25 16:07:19 -07001322 ds = data_size;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001323 page = peer_req->pages;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001324 page_chain_for_each(page) {
1325 unsigned len = min_t(int, ds, PAGE_SIZE);
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001326 data = kmap(page);
Philipp Reisnerde0ff332011-02-07 16:56:20 +01001327 rr = drbd_recv(mdev->tconn, data, len);
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +01001328 if (drbd_insert_fault(mdev, DRBD_FAULT_RECEIVE)) {
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001329 dev_err(DEV, "Fault injection: Corrupting data on receive\n");
1330 data[0] = data[0] ^ (unsigned long)-1;
1331 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001332 kunmap(page);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001333 if (rr != len) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001334 drbd_free_ee(mdev, peer_req);
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01001335 if (!signal_pending(current))
1336 dev_warn(DEV, "short read receiving data: read %d expected %d\n",
1337 rr, len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001338 return NULL;
1339 }
1340 ds -= rr;
1341 }
1342
1343 if (dgs) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001344 drbd_csum_ee(mdev, mdev->tconn->integrity_r_tfm, peer_req, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001345 if (memcmp(dig_in, dig_vv, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001346 dev_err(DEV, "Digest integrity check FAILED: %llus +%u\n",
1347 (unsigned long long)sector, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001348 drbd_bcast_ee(mdev, "digest failed",
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001349 dgs, dig_in, dig_vv, peer_req);
1350 drbd_free_ee(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001351 return NULL;
1352 }
1353 }
1354 mdev->recv_cnt += data_size>>9;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001355 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001356}
1357
1358/* drbd_drain_block() just takes a data block
1359 * out of the socket input buffer, and discards it.
1360 */
1361static int drbd_drain_block(struct drbd_conf *mdev, int data_size)
1362{
1363 struct page *page;
1364 int rr, rv = 1;
1365 void *data;
1366
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001367 if (!data_size)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001368 return true;
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001369
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001370 page = drbd_pp_alloc(mdev, 1, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001371
1372 data = kmap(page);
1373 while (data_size) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +01001374 rr = drbd_recv(mdev->tconn, data, min_t(int, data_size, PAGE_SIZE));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001375 if (rr != min_t(int, data_size, PAGE_SIZE)) {
1376 rv = 0;
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01001377 if (!signal_pending(current))
1378 dev_warn(DEV,
1379 "short read receiving data: read %d expected %d\n",
1380 rr, min_t(int, data_size, PAGE_SIZE));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001381 break;
1382 }
1383 data_size -= rr;
1384 }
1385 kunmap(page);
Lars Ellenberg435f0742010-09-06 12:30:25 +02001386 drbd_pp_free(mdev, page, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001387 return rv;
1388}
1389
1390static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req,
1391 sector_t sector, int data_size)
1392{
1393 struct bio_vec *bvec;
1394 struct bio *bio;
1395 int dgs, rr, i, expect;
Philipp Reisnera0638452011-01-19 14:31:32 +01001396 void *dig_in = mdev->tconn->int_dig_in;
1397 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001398
Philipp Reisnera0638452011-01-19 14:31:32 +01001399 dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_r_tfm) ?
1400 crypto_hash_digestsize(mdev->tconn->integrity_r_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001401
1402 if (dgs) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +01001403 rr = drbd_recv(mdev->tconn, dig_in, dgs);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001404 if (rr != dgs) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01001405 if (!signal_pending(current))
1406 dev_warn(DEV,
1407 "short read receiving data reply digest: read %d expected %d\n",
1408 rr, dgs);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001409 return 0;
1410 }
1411 }
1412
1413 data_size -= dgs;
1414
1415 /* optimistically update recv_cnt. if receiving fails below,
1416 * we disconnect anyways, and counters will be reset. */
1417 mdev->recv_cnt += data_size>>9;
1418
1419 bio = req->master_bio;
1420 D_ASSERT(sector == bio->bi_sector);
1421
1422 bio_for_each_segment(bvec, bio, i) {
1423 expect = min_t(int, data_size, bvec->bv_len);
Philipp Reisnerde0ff332011-02-07 16:56:20 +01001424 rr = drbd_recv(mdev->tconn,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001425 kmap(bvec->bv_page)+bvec->bv_offset,
1426 expect);
1427 kunmap(bvec->bv_page);
1428 if (rr != expect) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01001429 if (!signal_pending(current))
1430 dev_warn(DEV, "short read receiving data reply: "
1431 "read %d expected %d\n",
1432 rr, expect);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001433 return 0;
1434 }
1435 data_size -= rr;
1436 }
1437
1438 if (dgs) {
Philipp Reisnera0638452011-01-19 14:31:32 +01001439 drbd_csum_bio(mdev, mdev->tconn->integrity_r_tfm, bio, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001440 if (memcmp(dig_in, dig_vv, dgs)) {
1441 dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n");
1442 return 0;
1443 }
1444 }
1445
1446 D_ASSERT(data_size == 0);
1447 return 1;
1448}
1449
1450/* e_end_resync_block() is called via
1451 * drbd_process_done_ee() by asender only */
1452static int e_end_resync_block(struct drbd_conf *mdev, struct drbd_work *w, int unused)
1453{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001454 struct drbd_peer_request *peer_req = (struct drbd_peer_request *)w;
1455 sector_t sector = peer_req->i.sector;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001456 int ok;
1457
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001458 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001459
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001460 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1461 drbd_set_in_sync(mdev, sector, peer_req->i.size);
1462 ok = drbd_send_ack(mdev, P_RS_WRITE_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001463 } else {
1464 /* Record failure to sync */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001465 drbd_rs_failed_io(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001466
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001467 ok = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001468 }
1469 dec_unacked(mdev);
1470
1471 return ok;
1472}
1473
1474static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local)
1475{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001476 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001477
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001478 peer_req = read_in_block(mdev, ID_SYNCER, sector, data_size);
1479 if (!peer_req)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001480 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001481
1482 dec_rs_pending(mdev);
1483
Philipp Reisnerb411b362009-09-25 16:07:19 -07001484 inc_unacked(mdev);
1485 /* corresponding dec_unacked() in e_end_resync_block()
1486 * respective _drbd_clear_done_ee */
1487
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001488 peer_req->w.cb = e_end_resync_block;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001489
Philipp Reisner87eeee42011-01-19 14:16:30 +01001490 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001491 list_add(&peer_req->w.list, &mdev->sync_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001492 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001493
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001494 atomic_add(data_size >> 9, &mdev->rs_sect_ev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001495 if (drbd_submit_ee(mdev, peer_req, WRITE, DRBD_FAULT_RS_WR) == 0)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001496 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001497
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001498 /* don't care for the reason here */
1499 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01001500 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001501 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001502 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001503
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001504 drbd_free_ee(mdev, peer_req);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001505fail:
1506 put_ldev(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001507 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001508}
1509
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001510static struct drbd_request *
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001511find_request(struct drbd_conf *mdev, struct rb_root *root, u64 id,
1512 sector_t sector, bool missing_ok, const char *func)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001513{
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001514 struct drbd_request *req;
1515
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001516 /* Request object according to our peer */
1517 req = (struct drbd_request *)(unsigned long)id;
Andreas Gruenbacher5e472262011-01-27 14:42:51 +01001518 if (drbd_contains_interval(root, sector, &req->i) && req->i.local)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001519 return req;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001520 if (!missing_ok) {
1521 dev_err(DEV, "%s: failed to find request %lu, sector %llus\n", func,
1522 (unsigned long)id, (unsigned long long)sector);
1523 }
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001524 return NULL;
1525}
1526
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001527static int receive_DataReply(struct drbd_conf *mdev, enum drbd_packet cmd,
1528 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001529{
1530 struct drbd_request *req;
1531 sector_t sector;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001532 int ok;
Philipp Reisnere42325a2011-01-19 13:55:45 +01001533 struct p_data *p = &mdev->tconn->data.rbuf.data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001534
1535 sector = be64_to_cpu(p->sector);
1536
Philipp Reisner87eeee42011-01-19 14:16:30 +01001537 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001538 req = find_request(mdev, &mdev->read_requests, p->block_id, sector, false, __func__);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001539 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001540 if (unlikely(!req))
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001541 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001542
Bart Van Assche24c48302011-05-21 18:32:29 +02001543 /* hlist_del(&req->collision) is done in _req_may_be_done, to avoid
Philipp Reisnerb411b362009-09-25 16:07:19 -07001544 * special casing it there for the various failure cases.
1545 * still no race with drbd_fail_pending_reads */
1546 ok = recv_dless_read(mdev, req, sector, data_size);
1547
1548 if (ok)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001549 req_mod(req, DATA_RECEIVED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001550 /* else: nothing. handled from drbd_disconnect...
1551 * I don't think we may complete this just yet
1552 * in case we are "on-disconnect: freeze" */
1553
1554 return ok;
1555}
1556
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001557static int receive_RSDataReply(struct drbd_conf *mdev, enum drbd_packet cmd,
1558 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001559{
1560 sector_t sector;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001561 int ok;
Philipp Reisnere42325a2011-01-19 13:55:45 +01001562 struct p_data *p = &mdev->tconn->data.rbuf.data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001563
1564 sector = be64_to_cpu(p->sector);
1565 D_ASSERT(p->block_id == ID_SYNCER);
1566
1567 if (get_ldev(mdev)) {
1568 /* data is submitted to disk within recv_resync_read.
1569 * corresponding put_ldev done below on error,
Andreas Gruenbacher9c508422011-01-14 21:19:36 +01001570 * or in drbd_endio_sec. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001571 ok = recv_resync_read(mdev, sector, data_size);
1572 } else {
1573 if (__ratelimit(&drbd_ratelimit_state))
1574 dev_err(DEV, "Can not write resync data to local disk.\n");
1575
1576 ok = drbd_drain_block(mdev, data_size);
1577
Lars Ellenberg2b2bf212010-10-06 11:46:55 +02001578 drbd_send_ack_dp(mdev, P_NEG_ACK, p, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001579 }
1580
Philipp Reisner778f2712010-07-06 11:14:00 +02001581 atomic_add(data_size >> 9, &mdev->rs_sect_in);
1582
Philipp Reisnerb411b362009-09-25 16:07:19 -07001583 return ok;
1584}
1585
1586/* e_end_block() is called via drbd_process_done_ee().
1587 * this means this function only runs in the asender thread
1588 */
1589static int e_end_block(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1590{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001591 struct drbd_peer_request *peer_req = (struct drbd_peer_request *)w;
1592 sector_t sector = peer_req->i.sector;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001593 int ok = 1, pcmd;
1594
Philipp Reisner89e58e72011-01-19 13:12:45 +01001595 if (mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001596 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001597 pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
1598 mdev->state.conn <= C_PAUSED_SYNC_T &&
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001599 peer_req->flags & EE_MAY_SET_IN_SYNC) ?
Philipp Reisnerb411b362009-09-25 16:07:19 -07001600 P_RS_WRITE_ACK : P_WRITE_ACK;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001601 ok &= drbd_send_ack(mdev, pcmd, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001602 if (pcmd == P_RS_WRITE_ACK)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001603 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001604 } else {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001605 ok = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001606 /* we expect it to be marked out of sync anyways...
1607 * maybe assert this? */
1608 }
1609 dec_unacked(mdev);
1610 }
1611 /* we delete from the conflict detection hash _after_ we sent out the
1612 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
Philipp Reisner89e58e72011-01-19 13:12:45 +01001613 if (mdev->tconn->net_conf->two_primaries) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01001614 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001615 D_ASSERT(!drbd_interval_empty(&peer_req->i));
1616 drbd_remove_epoch_entry_interval(mdev, peer_req);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001617 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +01001618 } else
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001619 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001620
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001621 drbd_may_finish_epoch(mdev, peer_req->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001622
1623 return ok;
1624}
1625
1626static int e_send_discard_ack(struct drbd_conf *mdev, struct drbd_work *w, int unused)
1627{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001628 struct drbd_peer_request *peer_req = (struct drbd_peer_request *)w;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001629 int ok = 1;
1630
Philipp Reisner89e58e72011-01-19 13:12:45 +01001631 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001632 ok = drbd_send_ack(mdev, P_DISCARD_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001633
Philipp Reisner87eeee42011-01-19 14:16:30 +01001634 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001635 D_ASSERT(!drbd_interval_empty(&peer_req->i));
1636 drbd_remove_epoch_entry_interval(mdev, peer_req);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001637 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001638
1639 dec_unacked(mdev);
1640
1641 return ok;
1642}
1643
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001644static bool seq_greater(u32 a, u32 b)
1645{
1646 /*
1647 * We assume 32-bit wrap-around here.
1648 * For 24-bit wrap-around, we would have to shift:
1649 * a <<= 8; b <<= 8;
1650 */
1651 return (s32)a - (s32)b > 0;
1652}
1653
1654static u32 seq_max(u32 a, u32 b)
1655{
1656 return seq_greater(a, b) ? a : b;
1657}
1658
Andreas Gruenbacher43ae0772011-02-03 18:42:08 +01001659static void update_peer_seq(struct drbd_conf *mdev, unsigned int peer_seq)
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001660{
Andreas Gruenbacher43ae0772011-02-03 18:42:08 +01001661 unsigned int old_peer_seq;
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001662
1663 spin_lock(&mdev->peer_seq_lock);
Andreas Gruenbacher43ae0772011-02-03 18:42:08 +01001664 old_peer_seq = mdev->peer_seq;
1665 mdev->peer_seq = seq_max(mdev->peer_seq, peer_seq);
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001666 spin_unlock(&mdev->peer_seq_lock);
Andreas Gruenbacher43ae0772011-02-03 18:42:08 +01001667 if (old_peer_seq != peer_seq)
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001668 wake_up(&mdev->seq_wait);
1669}
1670
Philipp Reisnerb411b362009-09-25 16:07:19 -07001671/* Called from receive_Data.
1672 * Synchronize packets on sock with packets on msock.
1673 *
1674 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
1675 * packet traveling on msock, they are still processed in the order they have
1676 * been sent.
1677 *
1678 * Note: we don't care for Ack packets overtaking P_DATA packets.
1679 *
1680 * In case packet_seq is larger than mdev->peer_seq number, there are
1681 * outstanding packets on the msock. We wait for them to arrive.
1682 * In case we are the logically next packet, we update mdev->peer_seq
1683 * ourselves. Correctly handles 32bit wrap around.
1684 *
1685 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
1686 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
1687 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
1688 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
1689 *
1690 * returns 0 if we may process the packet,
1691 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
1692static int drbd_wait_peer_seq(struct drbd_conf *mdev, const u32 packet_seq)
1693{
1694 DEFINE_WAIT(wait);
1695 unsigned int p_seq;
1696 long timeout;
1697 int ret = 0;
1698 spin_lock(&mdev->peer_seq_lock);
1699 for (;;) {
1700 prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001701 if (!seq_greater(packet_seq, mdev->peer_seq + 1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001702 break;
1703 if (signal_pending(current)) {
1704 ret = -ERESTARTSYS;
1705 break;
1706 }
1707 p_seq = mdev->peer_seq;
1708 spin_unlock(&mdev->peer_seq_lock);
1709 timeout = schedule_timeout(30*HZ);
1710 spin_lock(&mdev->peer_seq_lock);
1711 if (timeout == 0 && p_seq == mdev->peer_seq) {
1712 ret = -ETIMEDOUT;
1713 dev_err(DEV, "ASSERT FAILED waited 30 seconds for sequence update, forcing reconnect\n");
1714 break;
1715 }
1716 }
1717 finish_wait(&mdev->seq_wait, &wait);
1718 if (mdev->peer_seq+1 == packet_seq)
1719 mdev->peer_seq++;
1720 spin_unlock(&mdev->peer_seq_lock);
1721 return ret;
1722}
1723
Lars Ellenberg688593c2010-11-17 22:25:03 +01001724/* see also bio_flags_to_wire()
1725 * DRBD_REQ_*, because we need to semantically map the flags to data packet
1726 * flags and back. We may replicate to other kernel versions. */
1727static unsigned long wire_flags_to_bio(struct drbd_conf *mdev, u32 dpf)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001728{
Lars Ellenberg688593c2010-11-17 22:25:03 +01001729 return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
1730 (dpf & DP_FUA ? REQ_FUA : 0) |
1731 (dpf & DP_FLUSH ? REQ_FLUSH : 0) |
1732 (dpf & DP_DISCARD ? REQ_DISCARD : 0);
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001733}
1734
Philipp Reisnerb411b362009-09-25 16:07:19 -07001735/* mirrored write */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001736static int receive_Data(struct drbd_conf *mdev, enum drbd_packet cmd,
1737 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001738{
1739 sector_t sector;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001740 struct drbd_peer_request *peer_req;
Philipp Reisnere42325a2011-01-19 13:55:45 +01001741 struct p_data *p = &mdev->tconn->data.rbuf.data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001742 int rw = WRITE;
1743 u32 dp_flags;
1744
Philipp Reisnerb411b362009-09-25 16:07:19 -07001745 if (!get_ldev(mdev)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001746 spin_lock(&mdev->peer_seq_lock);
1747 if (mdev->peer_seq+1 == be32_to_cpu(p->seq_num))
1748 mdev->peer_seq++;
1749 spin_unlock(&mdev->peer_seq_lock);
1750
Lars Ellenberg2b2bf212010-10-06 11:46:55 +02001751 drbd_send_ack_dp(mdev, P_NEG_ACK, p, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001752 atomic_inc(&mdev->current_epoch->epoch_size);
1753 return drbd_drain_block(mdev, data_size);
1754 }
1755
1756 /* get_ldev(mdev) successful.
1757 * Corresponding put_ldev done either below (on various errors),
Andreas Gruenbacher9c508422011-01-14 21:19:36 +01001758 * or in drbd_endio_sec, if we successfully submit the data at
Philipp Reisnerb411b362009-09-25 16:07:19 -07001759 * the end of this function. */
1760
1761 sector = be64_to_cpu(p->sector);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001762 peer_req = read_in_block(mdev, p->block_id, sector, data_size);
1763 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001764 put_ldev(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001765 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001766 }
1767
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001768 peer_req->w.cb = e_end_block;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001769
Lars Ellenberg688593c2010-11-17 22:25:03 +01001770 dp_flags = be32_to_cpu(p->dp_flags);
1771 rw |= wire_flags_to_bio(mdev, dp_flags);
1772
1773 if (dp_flags & DP_MAY_SET_IN_SYNC)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001774 peer_req->flags |= EE_MAY_SET_IN_SYNC;
Lars Ellenberg688593c2010-11-17 22:25:03 +01001775
Philipp Reisnerb411b362009-09-25 16:07:19 -07001776 spin_lock(&mdev->epoch_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001777 peer_req->epoch = mdev->current_epoch;
1778 atomic_inc(&peer_req->epoch->epoch_size);
1779 atomic_inc(&peer_req->epoch->active);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001780 spin_unlock(&mdev->epoch_lock);
1781
Philipp Reisnerb411b362009-09-25 16:07:19 -07001782 /* I'm the receiver, I do hold a net_cnt reference. */
Philipp Reisner89e58e72011-01-19 13:12:45 +01001783 if (!mdev->tconn->net_conf->two_primaries) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01001784 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001785 } else {
1786 /* don't get the req_lock yet,
1787 * we may sleep in drbd_wait_peer_seq */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001788 const int size = peer_req->i.size;
Philipp Reisner25703f82011-02-07 14:35:25 +01001789 const int discard = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001790 DEFINE_WAIT(wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001791 int first;
1792
Philipp Reisner89e58e72011-01-19 13:12:45 +01001793 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001794
1795 /* conflict detection and handling:
1796 * 1. wait on the sequence number,
1797 * in case this data packet overtook ACK packets.
Andreas Gruenbacher5e472262011-01-27 14:42:51 +01001798 * 2. check for conflicting write requests.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001799 *
1800 * Note: for two_primaries, we are protocol C,
1801 * so there cannot be any request that is DONE
1802 * but still on the transfer log.
1803 *
Philipp Reisnerb411b362009-09-25 16:07:19 -07001804 * if no conflicting request is found:
1805 * submit.
1806 *
1807 * if any conflicting request is found
1808 * that has not yet been acked,
1809 * AND I have the "discard concurrent writes" flag:
1810 * queue (via done_ee) the P_DISCARD_ACK; OUT.
1811 *
1812 * if any conflicting request is found:
1813 * block the receiver, waiting on misc_wait
1814 * until no more conflicting requests are there,
1815 * or we get interrupted (disconnect).
1816 *
1817 * we do not just write after local io completion of those
1818 * requests, but only after req is done completely, i.e.
1819 * we wait for the P_DISCARD_ACK to arrive!
1820 *
1821 * then proceed normally, i.e. submit.
1822 */
1823 if (drbd_wait_peer_seq(mdev, be32_to_cpu(p->seq_num)))
1824 goto out_interrupted;
1825
Philipp Reisner87eeee42011-01-19 14:16:30 +01001826 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001827
Philipp Reisnerb411b362009-09-25 16:07:19 -07001828 first = 1;
1829 for (;;) {
Andreas Gruenbacherde696712011-01-20 15:00:24 +01001830 struct drbd_interval *i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001831 int have_unacked = 0;
1832 int have_conflict = 0;
1833 prepare_to_wait(&mdev->misc_wait, &wait,
1834 TASK_INTERRUPTIBLE);
Andreas Gruenbacherde696712011-01-20 15:00:24 +01001835
1836 i = drbd_find_overlap(&mdev->write_requests, sector, size);
1837 if (i) {
Andreas Gruenbacherde696712011-01-20 15:00:24 +01001838 /* only ALERT on first iteration,
1839 * we may be woken up early... */
1840 if (first)
Andreas Gruenbacher5e472262011-01-27 14:42:51 +01001841 dev_alert(DEV, "%s[%u] Concurrent %s write detected!"
Andreas Gruenbacherde696712011-01-20 15:00:24 +01001842 " new: %llus +%u; pending: %llus +%u\n",
1843 current->comm, current->pid,
Andreas Gruenbacher5e472262011-01-27 14:42:51 +01001844 i->local ? "local" : "remote",
Andreas Gruenbacherde696712011-01-20 15:00:24 +01001845 (unsigned long long)sector, size,
Andreas Gruenbacher5e472262011-01-27 14:42:51 +01001846 (unsigned long long)i->sector, i->size);
1847
1848 if (i->local) {
1849 struct drbd_request *req2;
1850
1851 req2 = container_of(i, struct drbd_request, i);
1852 if (req2->rq_state & RQ_NET_PENDING)
1853 ++have_unacked;
1854 }
Andreas Gruenbacherde696712011-01-20 15:00:24 +01001855 ++have_conflict;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001856 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001857 if (!have_conflict)
1858 break;
1859
1860 /* Discard Ack only for the _first_ iteration */
1861 if (first && discard && have_unacked) {
1862 dev_alert(DEV, "Concurrent write! [DISCARD BY FLAG] sec=%llus\n",
1863 (unsigned long long)sector);
1864 inc_unacked(mdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001865 peer_req->w.cb = e_send_discard_ack;
1866 list_add_tail(&peer_req->w.list, &mdev->done_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001867
Philipp Reisner87eeee42011-01-19 14:16:30 +01001868 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001869
1870 /* we could probably send that P_DISCARD_ACK ourselves,
1871 * but I don't like the receiver using the msock */
1872
1873 put_ldev(mdev);
Philipp Reisner0625ac12011-02-07 14:49:19 +01001874 wake_asender(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001875 finish_wait(&mdev->misc_wait, &wait);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001876 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001877 }
1878
1879 if (signal_pending(current)) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01001880 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001881 finish_wait(&mdev->misc_wait, &wait);
1882 goto out_interrupted;
1883 }
1884
Andreas Gruenbachera500c2e2011-01-27 14:12:23 +01001885 /* Indicate to wake up mdev->misc_wait upon completion. */
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001886 i->waiting = true;
Andreas Gruenbachera500c2e2011-01-27 14:12:23 +01001887
Philipp Reisner87eeee42011-01-19 14:16:30 +01001888 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001889 if (first) {
1890 first = 0;
1891 dev_alert(DEV, "Concurrent write! [W AFTERWARDS] "
1892 "sec=%llus\n", (unsigned long long)sector);
1893 } else if (discard) {
1894 /* we had none on the first iteration.
1895 * there must be none now. */
1896 D_ASSERT(have_unacked == 0);
1897 }
1898 schedule();
Philipp Reisner87eeee42011-01-19 14:16:30 +01001899 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001900 }
1901 finish_wait(&mdev->misc_wait, &wait);
Andreas Gruenbacher5e472262011-01-27 14:42:51 +01001902
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001903 drbd_insert_interval(&mdev->write_requests, &peer_req->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001904 }
1905
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001906 list_add(&peer_req->w.list, &mdev->active_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001907 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001908
Philipp Reisner89e58e72011-01-19 13:12:45 +01001909 switch (mdev->tconn->net_conf->wire_protocol) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001910 case DRBD_PROT_C:
1911 inc_unacked(mdev);
1912 /* corresponding dec_unacked() in e_end_block()
1913 * respective _drbd_clear_done_ee */
1914 break;
1915 case DRBD_PROT_B:
1916 /* I really don't like it that the receiver thread
1917 * sends on the msock, but anyways */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001918 drbd_send_ack(mdev, P_RECV_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001919 break;
1920 case DRBD_PROT_A:
1921 /* nothing to do */
1922 break;
1923 }
1924
Lars Ellenberg6719fb02010-10-18 23:04:07 +02001925 if (mdev->state.pdsk < D_INCONSISTENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001926 /* In case we have the only disk of the cluster, */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001927 drbd_set_out_of_sync(mdev, peer_req->i.sector, peer_req->i.size);
1928 peer_req->flags |= EE_CALL_AL_COMPLETE_IO;
1929 peer_req->flags &= ~EE_MAY_SET_IN_SYNC;
1930 drbd_al_begin_io(mdev, peer_req->i.sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001931 }
1932
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001933 if (drbd_submit_ee(mdev, peer_req, rw, DRBD_FAULT_DT_WR) == 0)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001934 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001935
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001936 /* don't care for the reason here */
1937 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01001938 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001939 list_del(&peer_req->w.list);
1940 drbd_remove_epoch_entry_interval(mdev, peer_req);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001941 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001942 if (peer_req->flags & EE_CALL_AL_COMPLETE_IO)
1943 drbd_al_complete_io(mdev, peer_req->i.sector);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001944
Philipp Reisnerb411b362009-09-25 16:07:19 -07001945out_interrupted:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001946 drbd_may_finish_epoch(mdev, peer_req->epoch, EV_PUT + EV_CLEANUP);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001947 put_ldev(mdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001948 drbd_free_ee(mdev, peer_req);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001949 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001950}
1951
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001952/* We may throttle resync, if the lower device seems to be busy,
1953 * and current sync rate is above c_min_rate.
1954 *
1955 * To decide whether or not the lower device is busy, we use a scheme similar
1956 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
1957 * (more than 64 sectors) of activity we cannot account for with our own resync
1958 * activity, it obviously is "busy".
1959 *
1960 * The current sync rate used here uses only the most recent two step marks,
1961 * to have a short time average so we can react faster.
1962 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01001963int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001964{
1965 struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
1966 unsigned long db, dt, dbdt;
Philipp Reisnere3555d82010-11-07 15:56:29 +01001967 struct lc_element *tmp;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001968 int curr_events;
1969 int throttle = 0;
1970
1971 /* feature disabled? */
1972 if (mdev->sync_conf.c_min_rate == 0)
1973 return 0;
1974
Philipp Reisnere3555d82010-11-07 15:56:29 +01001975 spin_lock_irq(&mdev->al_lock);
1976 tmp = lc_find(mdev->resync, BM_SECT_TO_EXT(sector));
1977 if (tmp) {
1978 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
1979 if (test_bit(BME_PRIORITY, &bm_ext->flags)) {
1980 spin_unlock_irq(&mdev->al_lock);
1981 return 0;
1982 }
1983 /* Do not slow down if app IO is already waiting for this extent */
1984 }
1985 spin_unlock_irq(&mdev->al_lock);
1986
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001987 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
1988 (int)part_stat_read(&disk->part0, sectors[1]) -
1989 atomic_read(&mdev->rs_sect_ev);
Philipp Reisnere3555d82010-11-07 15:56:29 +01001990
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001991 if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
1992 unsigned long rs_left;
1993 int i;
1994
1995 mdev->rs_last_events = curr_events;
1996
1997 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
1998 * approx. */
Lars Ellenberg2649f082010-11-05 10:05:47 +01001999 i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
2000
2001 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
2002 rs_left = mdev->ov_left;
2003 else
2004 rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002005
2006 dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
2007 if (!dt)
2008 dt++;
2009 db = mdev->rs_mark_left[i] - rs_left;
2010 dbdt = Bit2KB(db/dt);
2011
2012 if (dbdt > mdev->sync_conf.c_min_rate)
2013 throttle = 1;
2014 }
2015 return throttle;
2016}
2017
2018
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01002019static int receive_DataRequest(struct drbd_conf *mdev, enum drbd_packet cmd,
2020 unsigned int digest_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002021{
2022 sector_t sector;
2023 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002024 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002025 struct digest_info *di = NULL;
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002026 int size, verb;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002027 unsigned int fault_type;
Philipp Reisnere42325a2011-01-19 13:55:45 +01002028 struct p_block_req *p = &mdev->tconn->data.rbuf.block_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002029
2030 sector = be64_to_cpu(p->sector);
2031 size = be32_to_cpu(p->blksize);
2032
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01002033 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002034 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2035 (unsigned long long)sector, size);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002036 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002037 }
2038 if (sector + (size>>9) > capacity) {
2039 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2040 (unsigned long long)sector, size);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002041 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002042 }
2043
2044 if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002045 verb = 1;
2046 switch (cmd) {
2047 case P_DATA_REQUEST:
2048 drbd_send_ack_rp(mdev, P_NEG_DREPLY, p);
2049 break;
2050 case P_RS_DATA_REQUEST:
2051 case P_CSUM_RS_REQUEST:
2052 case P_OV_REQUEST:
2053 drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p);
2054 break;
2055 case P_OV_REPLY:
2056 verb = 0;
2057 dec_rs_pending(mdev);
2058 drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC);
2059 break;
2060 default:
2061 dev_err(DEV, "unexpected command (%s) in receive_DataRequest\n",
2062 cmdname(cmd));
2063 }
2064 if (verb && __ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002065 dev_err(DEV, "Can not satisfy peer's read request, "
2066 "no local data.\n");
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002067
Lars Ellenberga821cc42010-09-06 12:31:37 +02002068 /* drain possibly payload */
2069 return drbd_drain_block(mdev, digest_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002070 }
2071
2072 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
2073 * "criss-cross" setup, that might cause write-out on some other DRBD,
2074 * which in turn might block on the other node at this very place. */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002075 peer_req = drbd_alloc_ee(mdev, p->block_id, sector, size, GFP_NOIO);
2076 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002077 put_ldev(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002078 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002079 }
2080
Philipp Reisner02918be2010-08-20 14:35:10 +02002081 switch (cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002082 case P_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002083 peer_req->w.cb = w_e_end_data_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002084 fault_type = DRBD_FAULT_DT_RD;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002085 /* application IO, don't drbd_rs_begin_io */
2086 goto submit;
2087
Philipp Reisnerb411b362009-09-25 16:07:19 -07002088 case P_RS_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002089 peer_req->w.cb = w_e_end_rsdata_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002090 fault_type = DRBD_FAULT_RS_RD;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002091 /* used in the sector offset progress display */
2092 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002093 break;
2094
2095 case P_OV_REPLY:
2096 case P_CSUM_RS_REQUEST:
2097 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002098 di = kmalloc(sizeof(*di) + digest_size, GFP_NOIO);
2099 if (!di)
2100 goto out_free_e;
2101
2102 di->digest_size = digest_size;
2103 di->digest = (((char *)di)+sizeof(struct digest_info));
2104
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002105 peer_req->digest = di;
2106 peer_req->flags |= EE_HAS_DIGEST;
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +02002107
Philipp Reisnerde0ff332011-02-07 16:56:20 +01002108 if (drbd_recv(mdev->tconn, di->digest, digest_size) != digest_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002109 goto out_free_e;
2110
Philipp Reisner02918be2010-08-20 14:35:10 +02002111 if (cmd == P_CSUM_RS_REQUEST) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002112 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002113 peer_req->w.cb = w_e_end_csum_rs_req;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002114 /* used in the sector offset progress display */
2115 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisner02918be2010-08-20 14:35:10 +02002116 } else if (cmd == P_OV_REPLY) {
Lars Ellenberg2649f082010-11-05 10:05:47 +01002117 /* track progress, we may need to throttle */
2118 atomic_add(size >> 9, &mdev->rs_sect_in);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002119 peer_req->w.cb = w_e_end_ov_reply;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002120 dec_rs_pending(mdev);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002121 /* drbd_rs_begin_io done when we sent this request,
2122 * but accounting still needs to be done. */
2123 goto submit_for_resync;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002124 }
2125 break;
2126
2127 case P_OV_REQUEST:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002128 if (mdev->ov_start_sector == ~(sector_t)0 &&
Philipp Reisner31890f42011-01-19 14:12:51 +01002129 mdev->tconn->agreed_pro_version >= 90) {
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002130 unsigned long now = jiffies;
2131 int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002132 mdev->ov_start_sector = sector;
2133 mdev->ov_position = sector;
Lars Ellenberg30b743a2010-11-05 09:39:06 +01002134 mdev->ov_left = drbd_bm_bits(mdev) - BM_SECT_TO_BIT(sector);
2135 mdev->rs_total = mdev->ov_left;
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002136 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
2137 mdev->rs_mark_left[i] = mdev->ov_left;
2138 mdev->rs_mark_time[i] = now;
2139 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002140 dev_info(DEV, "Online Verify start sector: %llu\n",
2141 (unsigned long long)sector);
2142 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002143 peer_req->w.cb = w_e_end_ov_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002144 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002145 break;
2146
Philipp Reisnerb411b362009-09-25 16:07:19 -07002147 default:
2148 dev_err(DEV, "unexpected command (%s) in receive_DataRequest\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02002149 cmdname(cmd));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002150 fault_type = DRBD_FAULT_MAX;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002151 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002152 }
2153
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002154 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2155 * wrt the receiver, but it is not as straightforward as it may seem.
2156 * Various places in the resync start and stop logic assume resync
2157 * requests are processed in order, requeuing this on the worker thread
2158 * introduces a bunch of new code for synchronization between threads.
2159 *
2160 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2161 * "forever", throttling after drbd_rs_begin_io will lock that extent
2162 * for application writes for the same time. For now, just throttle
2163 * here, where the rest of the code expects the receiver to sleep for
2164 * a while, anyways.
2165 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002166
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002167 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2168 * this defers syncer requests for some time, before letting at least
2169 * on request through. The resync controller on the receiving side
2170 * will adapt to the incoming rate accordingly.
2171 *
2172 * We cannot throttle here if remote is Primary/SyncTarget:
2173 * we would also throttle its application reads.
2174 * In that case, throttling is done on the SyncTarget only.
2175 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002176 if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev, sector))
2177 schedule_timeout_uninterruptible(HZ/10);
2178 if (drbd_rs_begin_io(mdev, sector))
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002179 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002180
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002181submit_for_resync:
2182 atomic_add(size >> 9, &mdev->rs_sect_ev);
2183
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002184submit:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002185 inc_unacked(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002186 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002187 list_add_tail(&peer_req->w.list, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002188 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002189
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002190 if (drbd_submit_ee(mdev, peer_req, READ, fault_type) == 0)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002191 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002192
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002193 /* don't care for the reason here */
2194 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002195 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002196 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002197 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002198 /* no drbd_rs_complete_io(), we are dropping the connection anyways */
2199
Philipp Reisnerb411b362009-09-25 16:07:19 -07002200out_free_e:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002201 put_ldev(mdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002202 drbd_free_ee(mdev, peer_req);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002203 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002204}
2205
2206static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
2207{
2208 int self, peer, rv = -100;
2209 unsigned long ch_self, ch_peer;
2210
2211 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2212 peer = mdev->p_uuid[UI_BITMAP] & 1;
2213
2214 ch_peer = mdev->p_uuid[UI_SIZE];
2215 ch_self = mdev->comm_bm_set;
2216
Philipp Reisner89e58e72011-01-19 13:12:45 +01002217 switch (mdev->tconn->net_conf->after_sb_0p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002218 case ASB_CONSENSUS:
2219 case ASB_DISCARD_SECONDARY:
2220 case ASB_CALL_HELPER:
2221 dev_err(DEV, "Configuration error.\n");
2222 break;
2223 case ASB_DISCONNECT:
2224 break;
2225 case ASB_DISCARD_YOUNGER_PRI:
2226 if (self == 0 && peer == 1) {
2227 rv = -1;
2228 break;
2229 }
2230 if (self == 1 && peer == 0) {
2231 rv = 1;
2232 break;
2233 }
2234 /* Else fall through to one of the other strategies... */
2235 case ASB_DISCARD_OLDER_PRI:
2236 if (self == 0 && peer == 1) {
2237 rv = 1;
2238 break;
2239 }
2240 if (self == 1 && peer == 0) {
2241 rv = -1;
2242 break;
2243 }
2244 /* Else fall through to one of the other strategies... */
Lars Ellenbergad19bf62009-10-14 09:36:49 +02002245 dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
Philipp Reisnerb411b362009-09-25 16:07:19 -07002246 "Using discard-least-changes instead\n");
2247 case ASB_DISCARD_ZERO_CHG:
2248 if (ch_peer == 0 && ch_self == 0) {
Philipp Reisner25703f82011-02-07 14:35:25 +01002249 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002250 ? -1 : 1;
2251 break;
2252 } else {
2253 if (ch_peer == 0) { rv = 1; break; }
2254 if (ch_self == 0) { rv = -1; break; }
2255 }
Philipp Reisner89e58e72011-01-19 13:12:45 +01002256 if (mdev->tconn->net_conf->after_sb_0p == ASB_DISCARD_ZERO_CHG)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002257 break;
2258 case ASB_DISCARD_LEAST_CHG:
2259 if (ch_self < ch_peer)
2260 rv = -1;
2261 else if (ch_self > ch_peer)
2262 rv = 1;
2263 else /* ( ch_self == ch_peer ) */
2264 /* Well, then use something else. */
Philipp Reisner25703f82011-02-07 14:35:25 +01002265 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002266 ? -1 : 1;
2267 break;
2268 case ASB_DISCARD_LOCAL:
2269 rv = -1;
2270 break;
2271 case ASB_DISCARD_REMOTE:
2272 rv = 1;
2273 }
2274
2275 return rv;
2276}
2277
2278static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
2279{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002280 int hg, rv = -100;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002281
Philipp Reisner89e58e72011-01-19 13:12:45 +01002282 switch (mdev->tconn->net_conf->after_sb_1p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002283 case ASB_DISCARD_YOUNGER_PRI:
2284 case ASB_DISCARD_OLDER_PRI:
2285 case ASB_DISCARD_LEAST_CHG:
2286 case ASB_DISCARD_LOCAL:
2287 case ASB_DISCARD_REMOTE:
2288 dev_err(DEV, "Configuration error.\n");
2289 break;
2290 case ASB_DISCONNECT:
2291 break;
2292 case ASB_CONSENSUS:
2293 hg = drbd_asb_recover_0p(mdev);
2294 if (hg == -1 && mdev->state.role == R_SECONDARY)
2295 rv = hg;
2296 if (hg == 1 && mdev->state.role == R_PRIMARY)
2297 rv = hg;
2298 break;
2299 case ASB_VIOLENTLY:
2300 rv = drbd_asb_recover_0p(mdev);
2301 break;
2302 case ASB_DISCARD_SECONDARY:
2303 return mdev->state.role == R_PRIMARY ? 1 : -1;
2304 case ASB_CALL_HELPER:
2305 hg = drbd_asb_recover_0p(mdev);
2306 if (hg == -1 && mdev->state.role == R_PRIMARY) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002307 enum drbd_state_rv rv2;
2308
2309 drbd_set_role(mdev, R_SECONDARY, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002310 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2311 * we might be here in C_WF_REPORT_PARAMS which is transient.
2312 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002313 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2314 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002315 drbd_khelper(mdev, "pri-lost-after-sb");
2316 } else {
2317 dev_warn(DEV, "Successfully gave up primary role.\n");
2318 rv = hg;
2319 }
2320 } else
2321 rv = hg;
2322 }
2323
2324 return rv;
2325}
2326
2327static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
2328{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002329 int hg, rv = -100;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002330
Philipp Reisner89e58e72011-01-19 13:12:45 +01002331 switch (mdev->tconn->net_conf->after_sb_2p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002332 case ASB_DISCARD_YOUNGER_PRI:
2333 case ASB_DISCARD_OLDER_PRI:
2334 case ASB_DISCARD_LEAST_CHG:
2335 case ASB_DISCARD_LOCAL:
2336 case ASB_DISCARD_REMOTE:
2337 case ASB_CONSENSUS:
2338 case ASB_DISCARD_SECONDARY:
2339 dev_err(DEV, "Configuration error.\n");
2340 break;
2341 case ASB_VIOLENTLY:
2342 rv = drbd_asb_recover_0p(mdev);
2343 break;
2344 case ASB_DISCONNECT:
2345 break;
2346 case ASB_CALL_HELPER:
2347 hg = drbd_asb_recover_0p(mdev);
2348 if (hg == -1) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002349 enum drbd_state_rv rv2;
2350
Philipp Reisnerb411b362009-09-25 16:07:19 -07002351 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2352 * we might be here in C_WF_REPORT_PARAMS which is transient.
2353 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002354 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2355 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002356 drbd_khelper(mdev, "pri-lost-after-sb");
2357 } else {
2358 dev_warn(DEV, "Successfully gave up primary role.\n");
2359 rv = hg;
2360 }
2361 } else
2362 rv = hg;
2363 }
2364
2365 return rv;
2366}
2367
2368static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
2369 u64 bits, u64 flags)
2370{
2371 if (!uuid) {
2372 dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
2373 return;
2374 }
2375 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
2376 text,
2377 (unsigned long long)uuid[UI_CURRENT],
2378 (unsigned long long)uuid[UI_BITMAP],
2379 (unsigned long long)uuid[UI_HISTORY_START],
2380 (unsigned long long)uuid[UI_HISTORY_END],
2381 (unsigned long long)bits,
2382 (unsigned long long)flags);
2383}
2384
2385/*
2386 100 after split brain try auto recover
2387 2 C_SYNC_SOURCE set BitMap
2388 1 C_SYNC_SOURCE use BitMap
2389 0 no Sync
2390 -1 C_SYNC_TARGET use BitMap
2391 -2 C_SYNC_TARGET set BitMap
2392 -100 after split brain, disconnect
2393-1000 unrelated data
Philipp Reisner4a23f262011-01-11 17:42:17 +01002394-1091 requires proto 91
2395-1096 requires proto 96
Philipp Reisnerb411b362009-09-25 16:07:19 -07002396 */
2397static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
2398{
2399 u64 self, peer;
2400 int i, j;
2401
2402 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2403 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2404
2405 *rule_nr = 10;
2406 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
2407 return 0;
2408
2409 *rule_nr = 20;
2410 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
2411 peer != UUID_JUST_CREATED)
2412 return -2;
2413
2414 *rule_nr = 30;
2415 if (self != UUID_JUST_CREATED &&
2416 (peer == UUID_JUST_CREATED || peer == (u64)0))
2417 return 2;
2418
2419 if (self == peer) {
2420 int rct, dc; /* roles at crash time */
2421
2422 if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
2423
Philipp Reisner31890f42011-01-19 14:12:51 +01002424 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002425 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002426
2427 if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
2428 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
2429 dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
2430 drbd_uuid_set_bm(mdev, 0UL);
2431
2432 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2433 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2434 *rule_nr = 34;
2435 } else {
2436 dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
2437 *rule_nr = 36;
2438 }
2439
2440 return 1;
2441 }
2442
2443 if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
2444
Philipp Reisner31890f42011-01-19 14:12:51 +01002445 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002446 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002447
2448 if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
2449 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
2450 dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
2451
2452 mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
2453 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
2454 mdev->p_uuid[UI_BITMAP] = 0UL;
2455
2456 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2457 *rule_nr = 35;
2458 } else {
2459 dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
2460 *rule_nr = 37;
2461 }
2462
2463 return -1;
2464 }
2465
2466 /* Common power [off|failure] */
2467 rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
2468 (mdev->p_uuid[UI_FLAGS] & 2);
2469 /* lowest bit is set when we were primary,
2470 * next bit (weight 2) is set when peer was primary */
2471 *rule_nr = 40;
2472
2473 switch (rct) {
2474 case 0: /* !self_pri && !peer_pri */ return 0;
2475 case 1: /* self_pri && !peer_pri */ return 1;
2476 case 2: /* !self_pri && peer_pri */ return -1;
2477 case 3: /* self_pri && peer_pri */
Philipp Reisner25703f82011-02-07 14:35:25 +01002478 dc = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002479 return dc ? -1 : 1;
2480 }
2481 }
2482
2483 *rule_nr = 50;
2484 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2485 if (self == peer)
2486 return -1;
2487
2488 *rule_nr = 51;
2489 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2490 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002491 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002492 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) ==
2493 (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) :
2494 peer + UUID_NEW_BM_OFFSET == (mdev->p_uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002495 /* The last P_SYNC_UUID did not get though. Undo the last start of
2496 resync as sync source modifications of the peer's UUIDs. */
2497
Philipp Reisner31890f42011-01-19 14:12:51 +01002498 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002499 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002500
2501 mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
2502 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
Philipp Reisner4a23f262011-01-11 17:42:17 +01002503
2504 dev_info(DEV, "Did not got last syncUUID packet, corrected:\n");
2505 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2506
Philipp Reisnerb411b362009-09-25 16:07:19 -07002507 return -1;
2508 }
2509 }
2510
2511 *rule_nr = 60;
2512 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2513 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2514 peer = mdev->p_uuid[i] & ~((u64)1);
2515 if (self == peer)
2516 return -2;
2517 }
2518
2519 *rule_nr = 70;
2520 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2521 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2522 if (self == peer)
2523 return 1;
2524
2525 *rule_nr = 71;
2526 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2527 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002528 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002529 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) ==
2530 (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) :
2531 self + UUID_NEW_BM_OFFSET == (mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002532 /* The last P_SYNC_UUID did not get though. Undo the last start of
2533 resync as sync source modifications of our UUIDs. */
2534
Philipp Reisner31890f42011-01-19 14:12:51 +01002535 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002536 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002537
2538 _drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
2539 _drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
2540
Philipp Reisner4a23f262011-01-11 17:42:17 +01002541 dev_info(DEV, "Last syncUUID did not get through, corrected:\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002542 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2543 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2544
2545 return 1;
2546 }
2547 }
2548
2549
2550 *rule_nr = 80;
Philipp Reisnerd8c2a362009-11-18 15:52:51 +01002551 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002552 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2553 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2554 if (self == peer)
2555 return 2;
2556 }
2557
2558 *rule_nr = 90;
2559 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2560 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2561 if (self == peer && self != ((u64)0))
2562 return 100;
2563
2564 *rule_nr = 100;
2565 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2566 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2567 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
2568 peer = mdev->p_uuid[j] & ~((u64)1);
2569 if (self == peer)
2570 return -100;
2571 }
2572 }
2573
2574 return -1000;
2575}
2576
2577/* drbd_sync_handshake() returns the new conn state on success, or
2578 CONN_MASK (-1) on failure.
2579 */
2580static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
2581 enum drbd_disk_state peer_disk) __must_hold(local)
2582{
2583 int hg, rule_nr;
2584 enum drbd_conns rv = C_MASK;
2585 enum drbd_disk_state mydisk;
2586
2587 mydisk = mdev->state.disk;
2588 if (mydisk == D_NEGOTIATING)
2589 mydisk = mdev->new_state_tmp.disk;
2590
2591 dev_info(DEV, "drbd_sync_handshake:\n");
2592 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0);
2593 drbd_uuid_dump(mdev, "peer", mdev->p_uuid,
2594 mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2595
2596 hg = drbd_uuid_compare(mdev, &rule_nr);
2597
2598 dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
2599
2600 if (hg == -1000) {
2601 dev_alert(DEV, "Unrelated data, aborting!\n");
2602 return C_MASK;
2603 }
Philipp Reisner4a23f262011-01-11 17:42:17 +01002604 if (hg < -1000) {
2605 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 -07002606 return C_MASK;
2607 }
2608
2609 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
2610 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
2611 int f = (hg == -100) || abs(hg) == 2;
2612 hg = mydisk > D_INCONSISTENT ? 1 : -1;
2613 if (f)
2614 hg = hg*2;
2615 dev_info(DEV, "Becoming sync %s due to disk states.\n",
2616 hg > 0 ? "source" : "target");
2617 }
2618
Adam Gandelman3a11a482010-04-08 16:48:23 -07002619 if (abs(hg) == 100)
2620 drbd_khelper(mdev, "initial-split-brain");
2621
Philipp Reisner89e58e72011-01-19 13:12:45 +01002622 if (hg == 100 || (hg == -100 && mdev->tconn->net_conf->always_asbp)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002623 int pcount = (mdev->state.role == R_PRIMARY)
2624 + (peer_role == R_PRIMARY);
2625 int forced = (hg == -100);
2626
2627 switch (pcount) {
2628 case 0:
2629 hg = drbd_asb_recover_0p(mdev);
2630 break;
2631 case 1:
2632 hg = drbd_asb_recover_1p(mdev);
2633 break;
2634 case 2:
2635 hg = drbd_asb_recover_2p(mdev);
2636 break;
2637 }
2638 if (abs(hg) < 100) {
2639 dev_warn(DEV, "Split-Brain detected, %d primaries, "
2640 "automatically solved. Sync from %s node\n",
2641 pcount, (hg < 0) ? "peer" : "this");
2642 if (forced) {
2643 dev_warn(DEV, "Doing a full sync, since"
2644 " UUIDs where ambiguous.\n");
2645 hg = hg*2;
2646 }
2647 }
2648 }
2649
2650 if (hg == -100) {
Philipp Reisner89e58e72011-01-19 13:12:45 +01002651 if (mdev->tconn->net_conf->want_lose && !(mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002652 hg = -1;
Philipp Reisner89e58e72011-01-19 13:12:45 +01002653 if (!mdev->tconn->net_conf->want_lose && (mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002654 hg = 1;
2655
2656 if (abs(hg) < 100)
2657 dev_warn(DEV, "Split-Brain detected, manually solved. "
2658 "Sync from %s node\n",
2659 (hg < 0) ? "peer" : "this");
2660 }
2661
2662 if (hg == -100) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01002663 /* FIXME this log message is not correct if we end up here
2664 * after an attempted attach on a diskless node.
2665 * We just refuse to attach -- well, we drop the "connection"
2666 * to that disk, in a way... */
Adam Gandelman3a11a482010-04-08 16:48:23 -07002667 dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002668 drbd_khelper(mdev, "split-brain");
2669 return C_MASK;
2670 }
2671
2672 if (hg > 0 && mydisk <= D_INCONSISTENT) {
2673 dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
2674 return C_MASK;
2675 }
2676
2677 if (hg < 0 && /* by intention we do not use mydisk here. */
2678 mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
Philipp Reisner89e58e72011-01-19 13:12:45 +01002679 switch (mdev->tconn->net_conf->rr_conflict) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002680 case ASB_CALL_HELPER:
2681 drbd_khelper(mdev, "pri-lost");
2682 /* fall through */
2683 case ASB_DISCONNECT:
2684 dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
2685 return C_MASK;
2686 case ASB_VIOLENTLY:
2687 dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
2688 "assumption\n");
2689 }
2690 }
2691
Philipp Reisner89e58e72011-01-19 13:12:45 +01002692 if (mdev->tconn->net_conf->dry_run || test_bit(CONN_DRY_RUN, &mdev->flags)) {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002693 if (hg == 0)
2694 dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
2695 else
2696 dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
2697 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
2698 abs(hg) >= 2 ? "full" : "bit-map based");
2699 return C_MASK;
2700 }
2701
Philipp Reisnerb411b362009-09-25 16:07:19 -07002702 if (abs(hg) >= 2) {
2703 dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002704 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake",
2705 BM_LOCKED_SET_ALLOWED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002706 return C_MASK;
2707 }
2708
2709 if (hg > 0) { /* become sync source. */
2710 rv = C_WF_BITMAP_S;
2711 } else if (hg < 0) { /* become sync target */
2712 rv = C_WF_BITMAP_T;
2713 } else {
2714 rv = C_CONNECTED;
2715 if (drbd_bm_total_weight(mdev)) {
2716 dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
2717 drbd_bm_total_weight(mdev));
2718 }
2719 }
2720
2721 return rv;
2722}
2723
2724/* returns 1 if invalid */
2725static int cmp_after_sb(enum drbd_after_sb_p peer, enum drbd_after_sb_p self)
2726{
2727 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
2728 if ((peer == ASB_DISCARD_REMOTE && self == ASB_DISCARD_LOCAL) ||
2729 (self == ASB_DISCARD_REMOTE && peer == ASB_DISCARD_LOCAL))
2730 return 0;
2731
2732 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
2733 if (peer == ASB_DISCARD_REMOTE || peer == ASB_DISCARD_LOCAL ||
2734 self == ASB_DISCARD_REMOTE || self == ASB_DISCARD_LOCAL)
2735 return 1;
2736
2737 /* everything else is valid if they are equal on both sides. */
2738 if (peer == self)
2739 return 0;
2740
2741 /* everything es is invalid. */
2742 return 1;
2743}
2744
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01002745static int receive_protocol(struct drbd_conf *mdev, enum drbd_packet cmd,
2746 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002747{
Philipp Reisnere42325a2011-01-19 13:55:45 +01002748 struct p_protocol *p = &mdev->tconn->data.rbuf.protocol;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002749 int p_proto, p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002750 int p_want_lose, p_two_primaries, cf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002751 char p_integrity_alg[SHARED_SECRET_MAX] = "";
2752
Philipp Reisnerb411b362009-09-25 16:07:19 -07002753 p_proto = be32_to_cpu(p->protocol);
2754 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
2755 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
2756 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002757 p_two_primaries = be32_to_cpu(p->two_primaries);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002758 cf = be32_to_cpu(p->conn_flags);
2759 p_want_lose = cf & CF_WANT_LOSE;
2760
2761 clear_bit(CONN_DRY_RUN, &mdev->flags);
2762
2763 if (cf & CF_DRY_RUN)
2764 set_bit(CONN_DRY_RUN, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002765
Philipp Reisner89e58e72011-01-19 13:12:45 +01002766 if (p_proto != mdev->tconn->net_conf->wire_protocol) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002767 dev_err(DEV, "incompatible communication protocols\n");
2768 goto disconnect;
2769 }
2770
Philipp Reisner89e58e72011-01-19 13:12:45 +01002771 if (cmp_after_sb(p_after_sb_0p, mdev->tconn->net_conf->after_sb_0p)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002772 dev_err(DEV, "incompatible after-sb-0pri settings\n");
2773 goto disconnect;
2774 }
2775
Philipp Reisner89e58e72011-01-19 13:12:45 +01002776 if (cmp_after_sb(p_after_sb_1p, mdev->tconn->net_conf->after_sb_1p)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002777 dev_err(DEV, "incompatible after-sb-1pri settings\n");
2778 goto disconnect;
2779 }
2780
Philipp Reisner89e58e72011-01-19 13:12:45 +01002781 if (cmp_after_sb(p_after_sb_2p, mdev->tconn->net_conf->after_sb_2p)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002782 dev_err(DEV, "incompatible after-sb-2pri settings\n");
2783 goto disconnect;
2784 }
2785
Philipp Reisner89e58e72011-01-19 13:12:45 +01002786 if (p_want_lose && mdev->tconn->net_conf->want_lose) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002787 dev_err(DEV, "both sides have the 'want_lose' flag set\n");
2788 goto disconnect;
2789 }
2790
Philipp Reisner89e58e72011-01-19 13:12:45 +01002791 if (p_two_primaries != mdev->tconn->net_conf->two_primaries) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002792 dev_err(DEV, "incompatible setting of the two-primaries options\n");
2793 goto disconnect;
2794 }
2795
Philipp Reisner31890f42011-01-19 14:12:51 +01002796 if (mdev->tconn->agreed_pro_version >= 87) {
Philipp Reisner89e58e72011-01-19 13:12:45 +01002797 unsigned char *my_alg = mdev->tconn->net_conf->integrity_alg;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002798
Philipp Reisnerde0ff332011-02-07 16:56:20 +01002799 if (drbd_recv(mdev->tconn, p_integrity_alg, data_size) != data_size)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002800 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002801
2802 p_integrity_alg[SHARED_SECRET_MAX-1] = 0;
2803 if (strcmp(p_integrity_alg, my_alg)) {
2804 dev_err(DEV, "incompatible setting of the data-integrity-alg\n");
2805 goto disconnect;
2806 }
2807 dev_info(DEV, "data-integrity-alg: %s\n",
2808 my_alg[0] ? my_alg : (unsigned char *)"<not-used>");
2809 }
2810
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002811 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002812
2813disconnect:
2814 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002815 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002816}
2817
2818/* helper function
2819 * input: alg name, feature name
2820 * return: NULL (alg name was "")
2821 * ERR_PTR(error) if something goes wrong
2822 * or the crypto hash ptr, if it worked out ok. */
2823struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
2824 const char *alg, const char *name)
2825{
2826 struct crypto_hash *tfm;
2827
2828 if (!alg[0])
2829 return NULL;
2830
2831 tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
2832 if (IS_ERR(tfm)) {
2833 dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
2834 alg, name, PTR_ERR(tfm));
2835 return tfm;
2836 }
2837 if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) {
2838 crypto_free_hash(tfm);
2839 dev_err(DEV, "\"%s\" is not a digest (%s)\n", alg, name);
2840 return ERR_PTR(-EINVAL);
2841 }
2842 return tfm;
2843}
2844
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01002845static int receive_SyncParam(struct drbd_conf *mdev, enum drbd_packet cmd,
2846 unsigned int packet_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002847{
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002848 int ok = true;
Philipp Reisnere42325a2011-01-19 13:55:45 +01002849 struct p_rs_param_95 *p = &mdev->tconn->data.rbuf.rs_param_95;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002850 unsigned int header_size, data_size, exp_max_sz;
2851 struct crypto_hash *verify_tfm = NULL;
2852 struct crypto_hash *csums_tfm = NULL;
Philipp Reisner31890f42011-01-19 14:12:51 +01002853 const int apv = mdev->tconn->agreed_pro_version;
Philipp Reisner778f2712010-07-06 11:14:00 +02002854 int *rs_plan_s = NULL;
2855 int fifo_size = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002856
2857 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
2858 : apv == 88 ? sizeof(struct p_rs_param)
2859 + SHARED_SECRET_MAX
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002860 : apv <= 94 ? sizeof(struct p_rs_param_89)
2861 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002862
Philipp Reisner02918be2010-08-20 14:35:10 +02002863 if (packet_size > exp_max_sz) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002864 dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02002865 packet_size, exp_max_sz);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002866 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002867 }
2868
2869 if (apv <= 88) {
Philipp Reisner257d0af2011-01-26 12:15:29 +01002870 header_size = sizeof(struct p_rs_param) - sizeof(struct p_header);
Philipp Reisner02918be2010-08-20 14:35:10 +02002871 data_size = packet_size - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002872 } else if (apv <= 94) {
Philipp Reisner257d0af2011-01-26 12:15:29 +01002873 header_size = sizeof(struct p_rs_param_89) - sizeof(struct p_header);
Philipp Reisner02918be2010-08-20 14:35:10 +02002874 data_size = packet_size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002875 D_ASSERT(data_size == 0);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002876 } else {
Philipp Reisner257d0af2011-01-26 12:15:29 +01002877 header_size = sizeof(struct p_rs_param_95) - sizeof(struct p_header);
Philipp Reisner02918be2010-08-20 14:35:10 +02002878 data_size = packet_size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002879 D_ASSERT(data_size == 0);
2880 }
2881
2882 /* initialize verify_alg and csums_alg */
2883 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
2884
Philipp Reisnerde0ff332011-02-07 16:56:20 +01002885 if (drbd_recv(mdev->tconn, &p->head.payload, header_size) != header_size)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002886 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002887
2888 mdev->sync_conf.rate = be32_to_cpu(p->rate);
2889
2890 if (apv >= 88) {
2891 if (apv == 88) {
2892 if (data_size > SHARED_SECRET_MAX) {
2893 dev_err(DEV, "verify-alg too long, "
2894 "peer wants %u, accepting only %u byte\n",
2895 data_size, SHARED_SECRET_MAX);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002896 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002897 }
2898
Philipp Reisnerde0ff332011-02-07 16:56:20 +01002899 if (drbd_recv(mdev->tconn, p->verify_alg, data_size) != data_size)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002900 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002901
2902 /* we expect NUL terminated string */
2903 /* but just in case someone tries to be evil */
2904 D_ASSERT(p->verify_alg[data_size-1] == 0);
2905 p->verify_alg[data_size-1] = 0;
2906
2907 } else /* apv >= 89 */ {
2908 /* we still expect NUL terminated strings */
2909 /* but just in case someone tries to be evil */
2910 D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
2911 D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
2912 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
2913 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
2914 }
2915
2916 if (strcmp(mdev->sync_conf.verify_alg, p->verify_alg)) {
2917 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
2918 dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
2919 mdev->sync_conf.verify_alg, p->verify_alg);
2920 goto disconnect;
2921 }
2922 verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
2923 p->verify_alg, "verify-alg");
2924 if (IS_ERR(verify_tfm)) {
2925 verify_tfm = NULL;
2926 goto disconnect;
2927 }
2928 }
2929
2930 if (apv >= 89 && strcmp(mdev->sync_conf.csums_alg, p->csums_alg)) {
2931 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
2932 dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
2933 mdev->sync_conf.csums_alg, p->csums_alg);
2934 goto disconnect;
2935 }
2936 csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
2937 p->csums_alg, "csums-alg");
2938 if (IS_ERR(csums_tfm)) {
2939 csums_tfm = NULL;
2940 goto disconnect;
2941 }
2942 }
2943
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002944 if (apv > 94) {
2945 mdev->sync_conf.rate = be32_to_cpu(p->rate);
2946 mdev->sync_conf.c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
2947 mdev->sync_conf.c_delay_target = be32_to_cpu(p->c_delay_target);
2948 mdev->sync_conf.c_fill_target = be32_to_cpu(p->c_fill_target);
2949 mdev->sync_conf.c_max_rate = be32_to_cpu(p->c_max_rate);
Philipp Reisner778f2712010-07-06 11:14:00 +02002950
2951 fifo_size = (mdev->sync_conf.c_plan_ahead * 10 * SLEEP_TIME) / HZ;
2952 if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) {
2953 rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL);
2954 if (!rs_plan_s) {
2955 dev_err(DEV, "kmalloc of fifo_buffer failed");
2956 goto disconnect;
2957 }
2958 }
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002959 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002960
2961 spin_lock(&mdev->peer_seq_lock);
2962 /* lock against drbd_nl_syncer_conf() */
2963 if (verify_tfm) {
2964 strcpy(mdev->sync_conf.verify_alg, p->verify_alg);
2965 mdev->sync_conf.verify_alg_len = strlen(p->verify_alg) + 1;
2966 crypto_free_hash(mdev->verify_tfm);
2967 mdev->verify_tfm = verify_tfm;
2968 dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
2969 }
2970 if (csums_tfm) {
2971 strcpy(mdev->sync_conf.csums_alg, p->csums_alg);
2972 mdev->sync_conf.csums_alg_len = strlen(p->csums_alg) + 1;
2973 crypto_free_hash(mdev->csums_tfm);
2974 mdev->csums_tfm = csums_tfm;
2975 dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
2976 }
Philipp Reisner778f2712010-07-06 11:14:00 +02002977 if (fifo_size != mdev->rs_plan_s.size) {
2978 kfree(mdev->rs_plan_s.values);
2979 mdev->rs_plan_s.values = rs_plan_s;
2980 mdev->rs_plan_s.size = fifo_size;
2981 mdev->rs_planed = 0;
2982 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002983 spin_unlock(&mdev->peer_seq_lock);
2984 }
2985
2986 return ok;
2987disconnect:
2988 /* just for completeness: actually not needed,
2989 * as this is not reached if csums_tfm was ok. */
2990 crypto_free_hash(csums_tfm);
2991 /* but free the verify_tfm again, if csums_tfm did not work out */
2992 crypto_free_hash(verify_tfm);
2993 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01002994 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002995}
2996
Philipp Reisnerb411b362009-09-25 16:07:19 -07002997/* warn if the arguments differ by more than 12.5% */
2998static void warn_if_differ_considerably(struct drbd_conf *mdev,
2999 const char *s, sector_t a, sector_t b)
3000{
3001 sector_t d;
3002 if (a == 0 || b == 0)
3003 return;
3004 d = (a > b) ? (a - b) : (b - a);
3005 if (d > (a>>3) || d > (b>>3))
3006 dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
3007 (unsigned long long)a, (unsigned long long)b);
3008}
3009
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003010static int receive_sizes(struct drbd_conf *mdev, enum drbd_packet cmd,
3011 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003012{
Philipp Reisnere42325a2011-01-19 13:55:45 +01003013 struct p_sizes *p = &mdev->tconn->data.rbuf.sizes;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003014 enum determine_dev_size dd = unchanged;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003015 sector_t p_size, p_usize, my_usize;
3016 int ldsc = 0; /* local disk size changed */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003017 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003018
Philipp Reisnerb411b362009-09-25 16:07:19 -07003019 p_size = be64_to_cpu(p->d_size);
3020 p_usize = be64_to_cpu(p->u_size);
3021
3022 if (p_size == 0 && mdev->state.disk == D_DISKLESS) {
3023 dev_err(DEV, "some backing storage is needed\n");
3024 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003025 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003026 }
3027
3028 /* just store the peer's disk size for now.
3029 * we still need to figure out whether we accept that. */
3030 mdev->p_size = p_size;
3031
Philipp Reisnerb411b362009-09-25 16:07:19 -07003032 if (get_ldev(mdev)) {
3033 warn_if_differ_considerably(mdev, "lower level device sizes",
3034 p_size, drbd_get_max_capacity(mdev->ldev));
3035 warn_if_differ_considerably(mdev, "user requested size",
3036 p_usize, mdev->ldev->dc.disk_size);
3037
3038 /* if this is the first connect, or an otherwise expected
3039 * param exchange, choose the minimum */
3040 if (mdev->state.conn == C_WF_REPORT_PARAMS)
3041 p_usize = min_not_zero((sector_t)mdev->ldev->dc.disk_size,
3042 p_usize);
3043
3044 my_usize = mdev->ldev->dc.disk_size;
3045
3046 if (mdev->ldev->dc.disk_size != p_usize) {
3047 mdev->ldev->dc.disk_size = p_usize;
3048 dev_info(DEV, "Peer sets u_size to %lu sectors\n",
3049 (unsigned long)mdev->ldev->dc.disk_size);
3050 }
3051
3052 /* Never shrink a device with usable data during connect.
3053 But allow online shrinking if we are connected. */
Philipp Reisnera393db62009-12-22 13:35:52 +01003054 if (drbd_new_dev_size(mdev, mdev->ldev, 0) <
Philipp Reisnerb411b362009-09-25 16:07:19 -07003055 drbd_get_capacity(mdev->this_bdev) &&
3056 mdev->state.disk >= D_OUTDATED &&
3057 mdev->state.conn < C_CONNECTED) {
3058 dev_err(DEV, "The peer's disk size is too small!\n");
3059 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
3060 mdev->ldev->dc.disk_size = my_usize;
3061 put_ldev(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003062 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003063 }
3064 put_ldev(mdev);
3065 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003066
Philipp Reisnere89b5912010-03-24 17:11:33 +01003067 ddsf = be16_to_cpu(p->dds_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003068 if (get_ldev(mdev)) {
Bart Van Assche24c48302011-05-21 18:32:29 +02003069 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003070 put_ldev(mdev);
3071 if (dd == dev_size_error)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003072 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003073 drbd_md_sync(mdev);
3074 } else {
3075 /* I am diskless, need to accept the peer's size. */
3076 drbd_set_my_capacity(mdev, p_size);
3077 }
3078
Philipp Reisner99432fc2011-05-20 16:39:13 +02003079 mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size);
3080 drbd_reconsider_max_bio_size(mdev);
3081
Philipp Reisnerb411b362009-09-25 16:07:19 -07003082 if (get_ldev(mdev)) {
3083 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
3084 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
3085 ldsc = 1;
3086 }
3087
Philipp Reisnerb411b362009-09-25 16:07:19 -07003088 put_ldev(mdev);
3089 }
3090
3091 if (mdev->state.conn > C_WF_REPORT_PARAMS) {
3092 if (be64_to_cpu(p->c_size) !=
3093 drbd_get_capacity(mdev->this_bdev) || ldsc) {
3094 /* we have different sizes, probably peer
3095 * needs to know my new size... */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003096 drbd_send_sizes(mdev, 0, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003097 }
3098 if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
3099 (dd == grew && mdev->state.conn == C_CONNECTED)) {
3100 if (mdev->state.pdsk >= D_INCONSISTENT &&
Philipp Reisnere89b5912010-03-24 17:11:33 +01003101 mdev->state.disk >= D_INCONSISTENT) {
3102 if (ddsf & DDSF_NO_RESYNC)
3103 dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
3104 else
3105 resync_after_online_grow(mdev);
3106 } else
Philipp Reisnerb411b362009-09-25 16:07:19 -07003107 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
3108 }
3109 }
3110
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003111 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003112}
3113
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003114static int receive_uuids(struct drbd_conf *mdev, enum drbd_packet cmd,
3115 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003116{
Philipp Reisnere42325a2011-01-19 13:55:45 +01003117 struct p_uuids *p = &mdev->tconn->data.rbuf.uuids;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003118 u64 *p_uuid;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003119 int i, updated_uuids = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003120
Philipp Reisnerb411b362009-09-25 16:07:19 -07003121 p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
3122
3123 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
3124 p_uuid[i] = be64_to_cpu(p->uuid[i]);
3125
3126 kfree(mdev->p_uuid);
3127 mdev->p_uuid = p_uuid;
3128
3129 if (mdev->state.conn < C_CONNECTED &&
3130 mdev->state.disk < D_INCONSISTENT &&
3131 mdev->state.role == R_PRIMARY &&
3132 (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
3133 dev_err(DEV, "Can only connect to data with current UUID=%016llX\n",
3134 (unsigned long long)mdev->ed_uuid);
3135 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003136 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003137 }
3138
3139 if (get_ldev(mdev)) {
3140 int skip_initial_sync =
3141 mdev->state.conn == C_CONNECTED &&
Philipp Reisner31890f42011-01-19 14:12:51 +01003142 mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003143 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
3144 (p_uuid[UI_FLAGS] & 8);
3145 if (skip_initial_sync) {
3146 dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n");
3147 drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003148 "clear_n_write from receive_uuids",
3149 BM_LOCKED_TEST_ALLOWED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003150 _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]);
3151 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3152 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3153 CS_VERBOSE, NULL);
3154 drbd_md_sync(mdev);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003155 updated_uuids = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003156 }
3157 put_ldev(mdev);
Philipp Reisner18a50fa2010-06-21 14:14:15 +02003158 } else if (mdev->state.disk < D_INCONSISTENT &&
3159 mdev->state.role == R_PRIMARY) {
3160 /* I am a diskless primary, the peer just created a new current UUID
3161 for me. */
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003162 updated_uuids = drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003163 }
3164
3165 /* Before we test for the disk state, we should wait until an eventually
3166 ongoing cluster wide state change is finished. That is important if
3167 we are primary and are detaching from our disk. We need to see the
3168 new disk state... */
3169 wait_event(mdev->misc_wait, !test_bit(CLUSTER_ST_CHANGE, &mdev->flags));
3170 if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT)
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003171 updated_uuids |= drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
3172
3173 if (updated_uuids)
3174 drbd_print_uuids(mdev, "receiver updated UUIDs to");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003175
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003176 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003177}
3178
3179/**
3180 * convert_state() - Converts the peer's view of the cluster state to our point of view
3181 * @ps: The state as seen by the peer.
3182 */
3183static union drbd_state convert_state(union drbd_state ps)
3184{
3185 union drbd_state ms;
3186
3187 static enum drbd_conns c_tab[] = {
3188 [C_CONNECTED] = C_CONNECTED,
3189
3190 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
3191 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
3192 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
3193 [C_VERIFY_S] = C_VERIFY_T,
3194 [C_MASK] = C_MASK,
3195 };
3196
3197 ms.i = ps.i;
3198
3199 ms.conn = c_tab[ps.conn];
3200 ms.peer = ps.role;
3201 ms.role = ps.peer;
3202 ms.pdsk = ps.disk;
3203 ms.disk = ps.pdsk;
3204 ms.peer_isp = (ps.aftr_isp | ps.user_isp);
3205
3206 return ms;
3207}
3208
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003209static int receive_req_state(struct drbd_conf *mdev, enum drbd_packet cmd,
3210 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003211{
Philipp Reisnere42325a2011-01-19 13:55:45 +01003212 struct p_req_state *p = &mdev->tconn->data.rbuf.req_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003213 union drbd_state mask, val;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01003214 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003215
Philipp Reisnerb411b362009-09-25 16:07:19 -07003216 mask.i = be32_to_cpu(p->mask);
3217 val.i = be32_to_cpu(p->val);
3218
Philipp Reisner25703f82011-02-07 14:35:25 +01003219 if (test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags) &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003220 test_bit(CLUSTER_ST_CHANGE, &mdev->flags)) {
3221 drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003222 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003223 }
3224
3225 mask = convert_state(mask);
3226 val = convert_state(val);
3227
3228 rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
3229
3230 drbd_send_sr_reply(mdev, rv);
3231 drbd_md_sync(mdev);
3232
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003233 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003234}
3235
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003236static int receive_state(struct drbd_conf *mdev, enum drbd_packet cmd,
3237 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003238{
Philipp Reisnere42325a2011-01-19 13:55:45 +01003239 struct p_state *p = &mdev->tconn->data.rbuf.state;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003240 union drbd_state os, ns, peer_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003241 enum drbd_disk_state real_peer_disk;
Philipp Reisner65d922c2010-06-16 16:18:09 +02003242 enum chg_state_flags cs_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003243 int rv;
3244
Philipp Reisnerb411b362009-09-25 16:07:19 -07003245 peer_state.i = be32_to_cpu(p->state);
3246
3247 real_peer_disk = peer_state.disk;
3248 if (peer_state.disk == D_NEGOTIATING) {
3249 real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
3250 dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
3251 }
3252
Philipp Reisner87eeee42011-01-19 14:16:30 +01003253 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003254 retry:
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003255 os = ns = mdev->state;
Philipp Reisner87eeee42011-01-19 14:16:30 +01003256 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003257
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003258 /* peer says his disk is uptodate, while we think it is inconsistent,
3259 * and this happens while we think we have a sync going on. */
3260 if (os.pdsk == D_INCONSISTENT && real_peer_disk == D_UP_TO_DATE &&
3261 os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) {
3262 /* If we are (becoming) SyncSource, but peer is still in sync
3263 * preparation, ignore its uptodate-ness to avoid flapping, it
3264 * will change to inconsistent once the peer reaches active
3265 * syncing states.
3266 * It may have changed syncer-paused flags, however, so we
3267 * cannot ignore this completely. */
3268 if (peer_state.conn > C_CONNECTED &&
3269 peer_state.conn < C_SYNC_SOURCE)
3270 real_peer_disk = D_INCONSISTENT;
3271
3272 /* if peer_state changes to connected at the same time,
3273 * it explicitly notifies us that it finished resync.
3274 * Maybe we should finish it up, too? */
3275 else if (os.conn >= C_SYNC_SOURCE &&
3276 peer_state.conn == C_CONNECTED) {
3277 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed)
3278 drbd_resync_finished(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003279 return true;
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003280 }
3281 }
3282
3283 /* peer says his disk is inconsistent, while we think it is uptodate,
3284 * and this happens while the peer still thinks we have a sync going on,
3285 * but we think we are already done with the sync.
3286 * We ignore this to avoid flapping pdsk.
3287 * This should not happen, if the peer is a recent version of drbd. */
3288 if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT &&
3289 os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE)
3290 real_peer_disk = D_UP_TO_DATE;
3291
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003292 if (ns.conn == C_WF_REPORT_PARAMS)
3293 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003294
Philipp Reisner67531712010-10-27 12:21:30 +02003295 if (peer_state.conn == C_AHEAD)
3296 ns.conn = C_BEHIND;
3297
Philipp Reisnerb411b362009-09-25 16:07:19 -07003298 if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING &&
3299 get_ldev_if_state(mdev, D_NEGOTIATING)) {
3300 int cr; /* consider resync */
3301
3302 /* if we established a new connection */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003303 cr = (os.conn < C_CONNECTED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003304 /* if we had an established connection
3305 * and one of the nodes newly attaches a disk */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003306 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003307 (peer_state.disk == D_NEGOTIATING ||
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003308 os.disk == D_NEGOTIATING));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003309 /* if we have both been inconsistent, and the peer has been
3310 * forced to be UpToDate with --overwrite-data */
3311 cr |= test_bit(CONSIDER_RESYNC, &mdev->flags);
3312 /* if we had been plain connected, and the admin requested to
3313 * start a sync by "invalidate" or "invalidate-remote" */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003314 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003315 (peer_state.conn >= C_STARTING_SYNC_S &&
3316 peer_state.conn <= C_WF_BITMAP_T));
3317
3318 if (cr)
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003319 ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003320
3321 put_ldev(mdev);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003322 if (ns.conn == C_MASK) {
3323 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003324 if (mdev->state.disk == D_NEGOTIATING) {
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003325 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003326 } else if (peer_state.disk == D_NEGOTIATING) {
3327 dev_err(DEV, "Disk attach process on the peer node was aborted.\n");
3328 peer_state.disk = D_DISKLESS;
Lars Ellenberg580b9762010-02-26 23:15:23 +01003329 real_peer_disk = D_DISKLESS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003330 } else {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003331 if (test_and_clear_bit(CONN_DRY_RUN, &mdev->flags))
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003332 return false;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003333 D_ASSERT(os.conn == C_WF_REPORT_PARAMS);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003334 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003335 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003336 }
3337 }
3338 }
3339
Philipp Reisner87eeee42011-01-19 14:16:30 +01003340 spin_lock_irq(&mdev->tconn->req_lock);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003341 if (mdev->state.i != os.i)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003342 goto retry;
3343 clear_bit(CONSIDER_RESYNC, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003344 ns.peer = peer_state.role;
3345 ns.pdsk = real_peer_disk;
3346 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003347 if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003348 ns.disk = mdev->new_state_tmp.disk;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003349 cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD);
3350 if (ns.pdsk == D_CONSISTENT && is_susp(ns) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED &&
Philipp Reisner481c6f52010-06-22 14:03:27 +02003351 test_bit(NEW_CUR_UUID, &mdev->flags)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01003352 /* Do not allow tl_restart(RESEND) for a rebooted peer. We can only allow this
Philipp Reisner481c6f52010-06-22 14:03:27 +02003353 for temporal network outages! */
Philipp Reisner87eeee42011-01-19 14:16:30 +01003354 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003355 dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
3356 tl_clear(mdev);
3357 drbd_uuid_new_current(mdev);
3358 clear_bit(NEW_CUR_UUID, &mdev->flags);
3359 drbd_force_state(mdev, NS2(conn, C_PROTOCOL_ERROR, susp, 0));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003360 return false;
Philipp Reisner481c6f52010-06-22 14:03:27 +02003361 }
Philipp Reisner65d922c2010-06-16 16:18:09 +02003362 rv = _drbd_set_state(mdev, ns, cs_flags, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003363 ns = mdev->state;
Philipp Reisner87eeee42011-01-19 14:16:30 +01003364 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003365
3366 if (rv < SS_SUCCESS) {
3367 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003368 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003369 }
3370
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003371 if (os.conn > C_WF_REPORT_PARAMS) {
3372 if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003373 peer_state.disk != D_NEGOTIATING ) {
3374 /* we want resync, peer has not yet decided to sync... */
3375 /* Nowadays only used when forcing a node into primary role and
3376 setting its disk to UpToDate with that */
3377 drbd_send_uuids(mdev);
3378 drbd_send_state(mdev);
3379 }
3380 }
3381
Philipp Reisner89e58e72011-01-19 13:12:45 +01003382 mdev->tconn->net_conf->want_lose = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003383
3384 drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
3385
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003386 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003387}
3388
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003389static int receive_sync_uuid(struct drbd_conf *mdev, enum drbd_packet cmd,
3390 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003391{
Philipp Reisnere42325a2011-01-19 13:55:45 +01003392 struct p_rs_uuid *p = &mdev->tconn->data.rbuf.rs_uuid;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003393
3394 wait_event(mdev->misc_wait,
3395 mdev->state.conn == C_WF_SYNC_UUID ||
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02003396 mdev->state.conn == C_BEHIND ||
Philipp Reisnerb411b362009-09-25 16:07:19 -07003397 mdev->state.conn < C_CONNECTED ||
3398 mdev->state.disk < D_NEGOTIATING);
3399
3400 /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */
3401
Philipp Reisnerb411b362009-09-25 16:07:19 -07003402 /* Here the _drbd_uuid_ functions are right, current should
3403 _not_ be rotated into the history */
3404 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
3405 _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid));
3406 _drbd_uuid_set(mdev, UI_BITMAP, 0UL);
3407
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003408 drbd_print_uuids(mdev, "updated sync uuid");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003409 drbd_start_resync(mdev, C_SYNC_TARGET);
3410
3411 put_ldev(mdev);
3412 } else
3413 dev_err(DEV, "Ignoring SyncUUID packet!\n");
3414
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003415 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003416}
3417
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003418/**
3419 * receive_bitmap_plain
3420 *
3421 * Return 0 when done, 1 when another iteration is needed, and a negative error
3422 * code upon failure.
3423 */
3424static int
Philipp Reisner02918be2010-08-20 14:35:10 +02003425receive_bitmap_plain(struct drbd_conf *mdev, unsigned int data_size,
3426 unsigned long *buffer, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003427{
3428 unsigned num_words = min_t(size_t, BM_PACKET_WORDS, c->bm_words - c->word_offset);
3429 unsigned want = num_words * sizeof(long);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003430 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003431
Philipp Reisner02918be2010-08-20 14:35:10 +02003432 if (want != data_size) {
3433 dev_err(DEV, "%s:want (%u) != data_size (%u)\n", __func__, want, data_size);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003434 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003435 }
3436 if (want == 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003437 return 0;
Philipp Reisnerde0ff332011-02-07 16:56:20 +01003438 err = drbd_recv(mdev->tconn, buffer, want);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003439 if (err != want) {
3440 if (err >= 0)
3441 err = -EIO;
3442 return err;
3443 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003444
3445 drbd_bm_merge_lel(mdev, c->word_offset, num_words, buffer);
3446
3447 c->word_offset += num_words;
3448 c->bit_offset = c->word_offset * BITS_PER_LONG;
3449 if (c->bit_offset > c->bm_bits)
3450 c->bit_offset = c->bm_bits;
3451
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003452 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003453}
3454
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003455/**
3456 * recv_bm_rle_bits
3457 *
3458 * Return 0 when done, 1 when another iteration is needed, and a negative error
3459 * code upon failure.
3460 */
3461static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07003462recv_bm_rle_bits(struct drbd_conf *mdev,
3463 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003464 struct bm_xfer_ctx *c,
3465 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003466{
3467 struct bitstream bs;
3468 u64 look_ahead;
3469 u64 rl;
3470 u64 tmp;
3471 unsigned long s = c->bit_offset;
3472 unsigned long e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003473 int toggle = DCBP_get_start(p);
3474 int have;
3475 int bits;
3476
3477 bitstream_init(&bs, p->code, len, DCBP_get_pad_bits(p));
3478
3479 bits = bitstream_get_bits(&bs, &look_ahead, 64);
3480 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003481 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003482
3483 for (have = bits; have > 0; s += rl, toggle = !toggle) {
3484 bits = vli_decode_bits(&rl, look_ahead);
3485 if (bits <= 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003486 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003487
3488 if (toggle) {
3489 e = s + rl -1;
3490 if (e >= c->bm_bits) {
3491 dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003492 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003493 }
3494 _drbd_bm_set_bits(mdev, s, e);
3495 }
3496
3497 if (have < bits) {
3498 dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
3499 have, bits, look_ahead,
3500 (unsigned int)(bs.cur.b - p->code),
3501 (unsigned int)bs.buf_len);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003502 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003503 }
3504 look_ahead >>= bits;
3505 have -= bits;
3506
3507 bits = bitstream_get_bits(&bs, &tmp, 64 - have);
3508 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003509 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003510 look_ahead |= tmp << have;
3511 have += bits;
3512 }
3513
3514 c->bit_offset = s;
3515 bm_xfer_ctx_bit_to_word_offset(c);
3516
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003517 return (s != c->bm_bits);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003518}
3519
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003520/**
3521 * decode_bitmap_c
3522 *
3523 * Return 0 when done, 1 when another iteration is needed, and a negative error
3524 * code upon failure.
3525 */
3526static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07003527decode_bitmap_c(struct drbd_conf *mdev,
3528 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003529 struct bm_xfer_ctx *c,
3530 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003531{
3532 if (DCBP_get_code(p) == RLE_VLI_Bits)
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003533 return recv_bm_rle_bits(mdev, p, c, len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003534
3535 /* other variants had been implemented for evaluation,
3536 * but have been dropped as this one turned out to be "best"
3537 * during all our tests. */
3538
3539 dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
3540 drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR));
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003541 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003542}
3543
3544void INFO_bm_xfer_stats(struct drbd_conf *mdev,
3545 const char *direction, struct bm_xfer_ctx *c)
3546{
3547 /* what would it take to transfer it "plaintext" */
Philipp Reisnerc0129492011-01-19 16:58:16 +01003548 unsigned plain = sizeof(struct p_header) *
Philipp Reisnerb411b362009-09-25 16:07:19 -07003549 ((c->bm_words+BM_PACKET_WORDS-1)/BM_PACKET_WORDS+1)
3550 + c->bm_words * sizeof(long);
3551 unsigned total = c->bytes[0] + c->bytes[1];
3552 unsigned r;
3553
3554 /* total can not be zero. but just in case: */
3555 if (total == 0)
3556 return;
3557
3558 /* don't report if not compressed */
3559 if (total >= plain)
3560 return;
3561
3562 /* total < plain. check for overflow, still */
3563 r = (total > UINT_MAX/1000) ? (total / (plain/1000))
3564 : (1000 * total / plain);
3565
3566 if (r > 1000)
3567 r = 1000;
3568
3569 r = 1000 - r;
3570 dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
3571 "total %u; compression: %u.%u%%\n",
3572 direction,
3573 c->bytes[1], c->packets[1],
3574 c->bytes[0], c->packets[0],
3575 total, r/10, r % 10);
3576}
3577
3578/* Since we are processing the bitfield from lower addresses to higher,
3579 it does not matter if the process it in 32 bit chunks or 64 bit
3580 chunks as long as it is little endian. (Understand it as byte stream,
3581 beginning with the lowest byte...) If we would use big endian
3582 we would need to process it from the highest address to the lowest,
3583 in order to be agnostic to the 32 vs 64 bits issue.
3584
3585 returns 0 on failure, 1 if we successfully received it. */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003586static int receive_bitmap(struct drbd_conf *mdev, enum drbd_packet cmd,
3587 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003588{
3589 struct bm_xfer_ctx c;
3590 void *buffer;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003591 int err;
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003592 int ok = false;
Philipp Reisner257d0af2011-01-26 12:15:29 +01003593 struct p_header *h = &mdev->tconn->data.rbuf.header;
Philipp Reisner77351055b2011-02-07 17:24:26 +01003594 struct packet_info pi;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003595
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003596 drbd_bm_lock(mdev, "receive bitmap", BM_LOCKED_SET_ALLOWED);
3597 /* you are supposed to send additional out-of-sync information
3598 * if you actually set bits during this phase */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003599
3600 /* maybe we should use some per thread scratch page,
3601 * and allocate that during initial device creation? */
3602 buffer = (unsigned long *) __get_free_page(GFP_NOIO);
3603 if (!buffer) {
3604 dev_err(DEV, "failed to allocate one page buffer in %s\n", __func__);
3605 goto out;
3606 }
3607
3608 c = (struct bm_xfer_ctx) {
3609 .bm_bits = drbd_bm_bits(mdev),
3610 .bm_words = drbd_bm_words(mdev),
3611 };
3612
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003613 for(;;) {
Philipp Reisner02918be2010-08-20 14:35:10 +02003614 if (cmd == P_BITMAP) {
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003615 err = receive_bitmap_plain(mdev, data_size, buffer, &c);
Philipp Reisner02918be2010-08-20 14:35:10 +02003616 } else if (cmd == P_COMPRESSED_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003617 /* MAYBE: sanity check that we speak proto >= 90,
3618 * and the feature is enabled! */
3619 struct p_compressed_bm *p;
3620
Philipp Reisner02918be2010-08-20 14:35:10 +02003621 if (data_size > BM_PACKET_PAYLOAD_BYTES) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003622 dev_err(DEV, "ReportCBitmap packet too large\n");
3623 goto out;
3624 }
3625 /* use the page buff */
3626 p = buffer;
3627 memcpy(p, h, sizeof(*h));
Philipp Reisnerde0ff332011-02-07 16:56:20 +01003628 if (drbd_recv(mdev->tconn, p->head.payload, data_size) != data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003629 goto out;
Lars Ellenberg004352f2010-10-05 20:13:58 +02003630 if (data_size <= (sizeof(*p) - sizeof(p->head))) {
3631 dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", data_size);
Andreas Gruenbacher78fcbda2010-12-10 22:18:27 +01003632 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003633 }
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003634 err = decode_bitmap_c(mdev, p, &c, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003635 } else {
Philipp Reisner02918be2010-08-20 14:35:10 +02003636 dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003637 goto out;
3638 }
3639
Philipp Reisner02918be2010-08-20 14:35:10 +02003640 c.packets[cmd == P_BITMAP]++;
Philipp Reisner257d0af2011-01-26 12:15:29 +01003641 c.bytes[cmd == P_BITMAP] += sizeof(struct p_header) + data_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003642
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003643 if (err <= 0) {
3644 if (err < 0)
3645 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003646 break;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003647 }
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01003648 if (!drbd_recv_header(mdev->tconn, &pi))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003649 goto out;
Philipp Reisner77351055b2011-02-07 17:24:26 +01003650 cmd = pi.cmd;
3651 data_size = pi.size;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003652 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003653
3654 INFO_bm_xfer_stats(mdev, "receive", &c);
3655
3656 if (mdev->state.conn == C_WF_BITMAP_T) {
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01003657 enum drbd_state_rv rv;
3658
Philipp Reisnerb411b362009-09-25 16:07:19 -07003659 ok = !drbd_send_bitmap(mdev);
3660 if (!ok)
3661 goto out;
3662 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01003663 rv = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
3664 D_ASSERT(rv == SS_SUCCESS);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003665 } else if (mdev->state.conn != C_WF_BITMAP_S) {
3666 /* admin may have requested C_DISCONNECTING,
3667 * other threads may have noticed network errors */
3668 dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n",
3669 drbd_conn_str(mdev->state.conn));
3670 }
3671
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003672 ok = true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003673 out:
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003674 drbd_bm_unlock(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003675 if (ok && mdev->state.conn == C_WF_BITMAP_S)
3676 drbd_start_resync(mdev, C_SYNC_SOURCE);
3677 free_page((unsigned long) buffer);
3678 return ok;
3679}
3680
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003681static int receive_skip(struct drbd_conf *mdev, enum drbd_packet cmd,
3682 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003683{
3684 /* TODO zero copy sink :) */
3685 static char sink[128];
3686 int size, want, r;
3687
Philipp Reisner02918be2010-08-20 14:35:10 +02003688 dev_warn(DEV, "skipping unknown optional packet type %d, l: %d!\n",
3689 cmd, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003690
Philipp Reisner02918be2010-08-20 14:35:10 +02003691 size = data_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003692 while (size > 0) {
3693 want = min_t(int, size, sizeof(sink));
Philipp Reisnerde0ff332011-02-07 16:56:20 +01003694 r = drbd_recv(mdev->tconn, sink, want);
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01003695 if (!expect(r > 0))
3696 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003697 size -= r;
3698 }
3699 return size == 0;
3700}
3701
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003702static int receive_UnplugRemote(struct drbd_conf *mdev, enum drbd_packet cmd,
3703 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003704{
Philipp Reisnerb411b362009-09-25 16:07:19 -07003705 /* Make sure we've acked all the TCP data associated
3706 * with the data requests being unplugged */
Philipp Reisnere42325a2011-01-19 13:55:45 +01003707 drbd_tcp_quickack(mdev->tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003708
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003709 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003710}
3711
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003712static int receive_out_of_sync(struct drbd_conf *mdev, enum drbd_packet cmd,
3713 unsigned int data_size)
Philipp Reisner73a01a12010-10-27 14:33:00 +02003714{
Philipp Reisnere42325a2011-01-19 13:55:45 +01003715 struct p_block_desc *p = &mdev->tconn->data.rbuf.block_desc;
Philipp Reisner73a01a12010-10-27 14:33:00 +02003716
Lars Ellenbergf735e3632010-12-17 21:06:18 +01003717 switch (mdev->state.conn) {
3718 case C_WF_SYNC_UUID:
3719 case C_WF_BITMAP_T:
3720 case C_BEHIND:
3721 break;
3722 default:
3723 dev_err(DEV, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n",
3724 drbd_conn_str(mdev->state.conn));
3725 }
3726
Philipp Reisner73a01a12010-10-27 14:33:00 +02003727 drbd_set_out_of_sync(mdev, be64_to_cpu(p->sector), be32_to_cpu(p->blksize));
3728
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01003729 return true;
Philipp Reisner73a01a12010-10-27 14:33:00 +02003730}
3731
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003732typedef int (*drbd_cmd_handler_f)(struct drbd_conf *, enum drbd_packet cmd,
3733 unsigned int to_receive);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003734
Philipp Reisner02918be2010-08-20 14:35:10 +02003735struct data_cmd {
3736 int expect_payload;
3737 size_t pkt_size;
3738 drbd_cmd_handler_f function;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003739};
3740
Philipp Reisner02918be2010-08-20 14:35:10 +02003741static struct data_cmd drbd_cmd_handler[] = {
3742 [P_DATA] = { 1, sizeof(struct p_data), receive_Data },
3743 [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply },
3744 [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } ,
3745 [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } ,
Philipp Reisner257d0af2011-01-26 12:15:29 +01003746 [P_BITMAP] = { 1, sizeof(struct p_header), receive_bitmap } ,
3747 [P_COMPRESSED_BITMAP] = { 1, sizeof(struct p_header), receive_bitmap } ,
3748 [P_UNPLUG_REMOTE] = { 0, sizeof(struct p_header), receive_UnplugRemote },
Philipp Reisner02918be2010-08-20 14:35:10 +02003749 [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
3750 [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
Philipp Reisner257d0af2011-01-26 12:15:29 +01003751 [P_SYNC_PARAM] = { 1, sizeof(struct p_header), receive_SyncParam },
3752 [P_SYNC_PARAM89] = { 1, sizeof(struct p_header), receive_SyncParam },
Philipp Reisner02918be2010-08-20 14:35:10 +02003753 [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol },
3754 [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids },
3755 [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes },
3756 [P_STATE] = { 0, sizeof(struct p_state), receive_state },
3757 [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state },
3758 [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid },
3759 [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
3760 [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest },
3761 [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest },
3762 [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip },
Philipp Reisner73a01a12010-10-27 14:33:00 +02003763 [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync },
Philipp Reisner02918be2010-08-20 14:35:10 +02003764 /* anything missing from this table is in
3765 * the asender_tbl, see get_asender_cmd */
3766 [P_MAX_CMD] = { 0, 0, NULL },
3767};
3768
3769/* All handler functions that expect a sub-header get that sub-heder in
Philipp Reisnere42325a2011-01-19 13:55:45 +01003770 mdev->tconn->data.rbuf.header.head.payload.
Philipp Reisner02918be2010-08-20 14:35:10 +02003771
Philipp Reisnere42325a2011-01-19 13:55:45 +01003772 Usually in mdev->tconn->data.rbuf.header.head the callback can find the usual
Philipp Reisner02918be2010-08-20 14:35:10 +02003773 p_header, but they may not rely on that. Since there is also p_header95 !
3774 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003775
Philipp Reisnereefc2f72011-02-08 12:55:24 +01003776static void drbdd(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003777{
Philipp Reisnereefc2f72011-02-08 12:55:24 +01003778 struct p_header *header = &tconn->data.rbuf.header;
Philipp Reisner77351055b2011-02-07 17:24:26 +01003779 struct packet_info pi;
Philipp Reisner02918be2010-08-20 14:35:10 +02003780 size_t shs; /* sub header size */
3781 int rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003782
Philipp Reisnereefc2f72011-02-08 12:55:24 +01003783 while (get_t_state(&tconn->receiver) == RUNNING) {
3784 drbd_thread_current_set_cpu(&tconn->receiver);
3785 if (!drbd_recv_header(tconn, &pi))
Philipp Reisner02918be2010-08-20 14:35:10 +02003786 goto err_out;
3787
Philipp Reisner77351055b2011-02-07 17:24:26 +01003788 if (unlikely(pi.cmd >= P_MAX_CMD || !drbd_cmd_handler[pi.cmd].function)) {
Philipp Reisnereefc2f72011-02-08 12:55:24 +01003789 conn_err(tconn, "unknown packet type %d, l: %d!\n", pi.cmd, pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02003790 goto err_out;
Lars Ellenberg0b33a912009-11-16 15:58:04 +01003791 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003792
Philipp Reisner77351055b2011-02-07 17:24:26 +01003793 shs = drbd_cmd_handler[pi.cmd].pkt_size - sizeof(struct p_header);
3794 if (pi.size - shs > 0 && !drbd_cmd_handler[pi.cmd].expect_payload) {
Philipp Reisnereefc2f72011-02-08 12:55:24 +01003795 conn_err(tconn, "No payload expected %s l:%d\n", cmdname(pi.cmd), pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02003796 goto err_out;
3797 }
3798
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02003799 if (shs) {
Philipp Reisnereefc2f72011-02-08 12:55:24 +01003800 rv = drbd_recv(tconn, &header->payload, shs);
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02003801 if (unlikely(rv != shs)) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01003802 if (!signal_pending(current))
Philipp Reisnereefc2f72011-02-08 12:55:24 +01003803 conn_warn(tconn, "short read while reading sub header: rv=%d\n", rv);
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02003804 goto err_out;
3805 }
3806 }
3807
Philipp Reisnereefc2f72011-02-08 12:55:24 +01003808 rv = drbd_cmd_handler[pi.cmd].function(vnr_to_mdev(tconn, pi.vnr), pi.cmd, pi.size - shs);
Philipp Reisner02918be2010-08-20 14:35:10 +02003809
3810 if (unlikely(!rv)) {
Philipp Reisnereefc2f72011-02-08 12:55:24 +01003811 conn_err(tconn, "error receiving %s, l: %d!\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01003812 cmdname(pi.cmd), pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02003813 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003814 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003815 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003816
Philipp Reisner02918be2010-08-20 14:35:10 +02003817 if (0) {
3818 err_out:
Philipp Reisnereefc2f72011-02-08 12:55:24 +01003819 drbd_force_state(tconn->volume0, NS(conn, C_PROTOCOL_ERROR));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003820 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003821}
3822
Philipp Reisner191d3cc2011-01-19 14:53:22 +01003823void drbd_flush_workqueue(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003824{
3825 struct drbd_wq_barrier barr;
3826
3827 barr.w.cb = w_prev_work_done;
3828 init_completion(&barr.done);
Philipp Reisner191d3cc2011-01-19 14:53:22 +01003829 drbd_queue_work(&tconn->data.work, &barr.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003830 wait_for_completion(&barr.done);
3831}
3832
Philipp Reisner360cc742011-02-08 14:29:53 +01003833static void drbd_disconnect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003834{
Philipp Reisnerb411b362009-09-25 16:07:19 -07003835 union drbd_state os, ns;
3836 int rv = SS_UNKNOWN_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003837
Philipp Reisner360cc742011-02-08 14:29:53 +01003838 if (tconn->volume0->state.conn == C_STANDALONE)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003839 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003840
3841 /* asender does not clean up anything. it must not interfere, either */
Philipp Reisner360cc742011-02-08 14:29:53 +01003842 drbd_thread_stop(&tconn->asender);
3843 drbd_free_sock(tconn);
3844
3845 idr_for_each(&tconn->volumes, drbd_disconnected, tconn);
3846
3847 conn_info(tconn, "Connection closed\n");
3848
3849 spin_lock_irq(&tconn->req_lock);
3850 os = tconn->volume0->state;
3851 if (os.conn >= C_UNCONNECTED) {
3852 /* Do not restart in case we are C_DISCONNECTING */
3853 ns.i = os.i;
3854 ns.conn = C_UNCONNECTED;
3855 rv = _drbd_set_state(tconn->volume0, ns, CS_VERBOSE, NULL);
3856 }
3857 spin_unlock_irq(&tconn->req_lock);
3858
3859 if (os.conn == C_DISCONNECTING) {
3860 wait_event(tconn->net_cnt_wait, atomic_read(&tconn->net_cnt) == 0);
3861
3862 crypto_free_hash(tconn->cram_hmac_tfm);
3863 tconn->cram_hmac_tfm = NULL;
3864
3865 kfree(tconn->net_conf);
3866 tconn->net_conf = NULL;
3867 drbd_request_state(tconn->volume0, NS(conn, C_STANDALONE));
3868 }
3869}
3870
3871static int drbd_disconnected(int vnr, void *p, void *data)
3872{
3873 struct drbd_conf *mdev = (struct drbd_conf *)p;
3874 enum drbd_fencing_p fp;
3875 unsigned int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003876
Philipp Reisner85719572010-07-21 10:20:17 +02003877 /* wait for current activity to cease. */
Philipp Reisner87eeee42011-01-19 14:16:30 +01003878 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003879 _drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
3880 _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee);
3881 _drbd_wait_ee_list_empty(mdev, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003882 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003883
3884 /* We do not have data structures that would allow us to
3885 * get the rs_pending_cnt down to 0 again.
3886 * * On C_SYNC_TARGET we do not have any data structures describing
3887 * the pending RSDataRequest's we have sent.
3888 * * On C_SYNC_SOURCE there is no data structure that tracks
3889 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
3890 * And no, it is not the sum of the reference counts in the
3891 * resync_LRU. The resync_LRU tracks the whole operation including
3892 * the disk-IO, while the rs_pending_cnt only tracks the blocks
3893 * on the fly. */
3894 drbd_rs_cancel_all(mdev);
3895 mdev->rs_total = 0;
3896 mdev->rs_failed = 0;
3897 atomic_set(&mdev->rs_pending_cnt, 0);
3898 wake_up(&mdev->misc_wait);
3899
Philipp Reisner7fde2be2011-03-01 11:08:28 +01003900 del_timer(&mdev->request_timer);
3901
Philipp Reisnerb411b362009-09-25 16:07:19 -07003902 /* make sure syncer is stopped and w_resume_next_sg queued */
3903 del_timer_sync(&mdev->resync_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003904 resync_timer_fn((unsigned long)mdev);
3905
Philipp Reisnerb411b362009-09-25 16:07:19 -07003906 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
3907 * w_make_resync_request etc. which may still be on the worker queue
3908 * to be "canceled" */
Philipp Reisner191d3cc2011-01-19 14:53:22 +01003909 drbd_flush_workqueue(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003910
3911 /* This also does reclaim_net_ee(). If we do this too early, we might
3912 * miss some resync ee and pages.*/
3913 drbd_process_done_ee(mdev);
3914
3915 kfree(mdev->p_uuid);
3916 mdev->p_uuid = NULL;
3917
Philipp Reisnerfb22c402010-09-08 23:20:21 +02003918 if (!is_susp(mdev->state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003919 tl_clear(mdev);
3920
Philipp Reisnerb411b362009-09-25 16:07:19 -07003921 drbd_md_sync(mdev);
3922
3923 fp = FP_DONT_CARE;
3924 if (get_ldev(mdev)) {
3925 fp = mdev->ldev->dc.fencing;
3926 put_ldev(mdev);
3927 }
3928
Philipp Reisner87f7be42010-06-11 13:56:33 +02003929 if (mdev->state.role == R_PRIMARY && fp >= FP_RESOURCE && mdev->state.pdsk >= D_UNKNOWN)
3930 drbd_try_outdate_peer_async(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003931
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003932 /* serialize with bitmap writeout triggered by the state change,
3933 * if any. */
3934 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
3935
Philipp Reisnerb411b362009-09-25 16:07:19 -07003936 /* tcp_close and release of sendpage pages can be deferred. I don't
3937 * want to use SO_LINGER, because apparently it can be deferred for
3938 * more than 20 seconds (longest time I checked).
3939 *
3940 * Actually we don't care for exactly when the network stack does its
3941 * put_page(), but release our reference on these pages right here.
3942 */
3943 i = drbd_release_ee(mdev, &mdev->net_ee);
3944 if (i)
3945 dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
Lars Ellenberg435f0742010-09-06 12:30:25 +02003946 i = atomic_read(&mdev->pp_in_use_by_net);
3947 if (i)
3948 dev_info(DEV, "pp_in_use_by_net = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003949 i = atomic_read(&mdev->pp_in_use);
3950 if (i)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02003951 dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003952
3953 D_ASSERT(list_empty(&mdev->read_ee));
3954 D_ASSERT(list_empty(&mdev->active_ee));
3955 D_ASSERT(list_empty(&mdev->sync_ee));
3956 D_ASSERT(list_empty(&mdev->done_ee));
3957
3958 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
3959 atomic_set(&mdev->current_epoch->epoch_size, 0);
3960 D_ASSERT(list_empty(&mdev->current_epoch->list));
Philipp Reisner360cc742011-02-08 14:29:53 +01003961
3962 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003963}
3964
3965/*
3966 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
3967 * we can agree on is stored in agreed_pro_version.
3968 *
3969 * feature flags and the reserved array should be enough room for future
3970 * enhancements of the handshake protocol, and possible plugins...
3971 *
3972 * for now, they are expected to be zero, but ignored.
3973 */
Philipp Reisner8a22ccc2011-02-07 16:47:12 +01003974static int drbd_send_handshake(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003975{
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01003976 /* ASSERT current == mdev->tconn->receiver ... */
Philipp Reisner8a22ccc2011-02-07 16:47:12 +01003977 struct p_handshake *p = &tconn->data.sbuf.handshake;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003978 int ok;
3979
Philipp Reisner8a22ccc2011-02-07 16:47:12 +01003980 if (mutex_lock_interruptible(&tconn->data.mutex)) {
3981 conn_err(tconn, "interrupted during initial handshake\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003982 return 0; /* interrupted. not ok. */
3983 }
3984
Philipp Reisner8a22ccc2011-02-07 16:47:12 +01003985 if (tconn->data.socket == NULL) {
3986 mutex_unlock(&tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003987 return 0;
3988 }
3989
3990 memset(p, 0, sizeof(*p));
3991 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
3992 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
Philipp Reisner8a22ccc2011-02-07 16:47:12 +01003993 ok = _conn_send_cmd(tconn, 0, tconn->data.socket, P_HAND_SHAKE,
3994 &p->head, sizeof(*p), 0);
3995 mutex_unlock(&tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003996 return ok;
3997}
3998
3999/*
4000 * return values:
4001 * 1 yes, we have a valid connection
4002 * 0 oops, did not work out, please try again
4003 * -1 peer talks different language,
4004 * no point in trying again, please go standalone.
4005 */
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004006static int drbd_do_handshake(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004007{
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004008 /* ASSERT current == tconn->receiver ... */
4009 struct p_handshake *p = &tconn->data.rbuf.handshake;
Philipp Reisner02918be2010-08-20 14:35:10 +02004010 const int expect = sizeof(struct p_handshake) - sizeof(struct p_header80);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004011 struct packet_info pi;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004012 int rv;
4013
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004014 rv = drbd_send_handshake(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004015 if (!rv)
4016 return 0;
4017
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004018 rv = drbd_recv_header(tconn, &pi);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004019 if (!rv)
4020 return 0;
4021
Philipp Reisner77351055b2011-02-07 17:24:26 +01004022 if (pi.cmd != P_HAND_SHAKE) {
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004023 conn_err(tconn, "expected HandShake packet, received: %s (0x%04x)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004024 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004025 return -1;
4026 }
4027
Philipp Reisner77351055b2011-02-07 17:24:26 +01004028 if (pi.size != expect) {
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004029 conn_err(tconn, "expected HandShake length: %u, received: %u\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004030 expect, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004031 return -1;
4032 }
4033
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004034 rv = drbd_recv(tconn, &p->head.payload, expect);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004035
4036 if (rv != expect) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01004037 if (!signal_pending(current))
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004038 conn_warn(tconn, "short read receiving handshake packet: l=%u\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004039 return 0;
4040 }
4041
Philipp Reisnerb411b362009-09-25 16:07:19 -07004042 p->protocol_min = be32_to_cpu(p->protocol_min);
4043 p->protocol_max = be32_to_cpu(p->protocol_max);
4044 if (p->protocol_max == 0)
4045 p->protocol_max = p->protocol_min;
4046
4047 if (PRO_VERSION_MAX < p->protocol_min ||
4048 PRO_VERSION_MIN > p->protocol_max)
4049 goto incompat;
4050
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004051 tconn->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004052
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004053 conn_info(tconn, "Handshake successful: "
4054 "Agreed network protocol version %d\n", tconn->agreed_pro_version);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004055
4056 return 1;
4057
4058 incompat:
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004059 conn_err(tconn, "incompatible DRBD dialects: "
Philipp Reisnerb411b362009-09-25 16:07:19 -07004060 "I support %d-%d, peer supports %d-%d\n",
4061 PRO_VERSION_MIN, PRO_VERSION_MAX,
4062 p->protocol_min, p->protocol_max);
4063 return -1;
4064}
4065
4066#if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
Philipp Reisner13e60372011-02-08 09:54:40 +01004067static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004068{
4069 dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
4070 dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004071 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004072}
4073#else
4074#define CHALLENGE_LEN 64
Johannes Thomab10d96c2010-01-07 16:02:50 +01004075
4076/* Return value:
4077 1 - auth succeeded,
4078 0 - failed, try again (network error),
4079 -1 - auth failed, don't try again.
4080*/
4081
Philipp Reisner13e60372011-02-08 09:54:40 +01004082static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004083{
4084 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
4085 struct scatterlist sg;
4086 char *response = NULL;
4087 char *right_response = NULL;
4088 char *peers_ch = NULL;
Philipp Reisner13e60372011-02-08 09:54:40 +01004089 unsigned int key_len = strlen(tconn->net_conf->shared_secret);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004090 unsigned int resp_size;
4091 struct hash_desc desc;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004092 struct packet_info pi;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004093 int rv;
4094
Philipp Reisner13e60372011-02-08 09:54:40 +01004095 desc.tfm = tconn->cram_hmac_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004096 desc.flags = 0;
4097
Philipp Reisner13e60372011-02-08 09:54:40 +01004098 rv = crypto_hash_setkey(tconn->cram_hmac_tfm,
4099 (u8 *)tconn->net_conf->shared_secret, key_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004100 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004101 conn_err(tconn, "crypto_hash_setkey() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004102 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004103 goto fail;
4104 }
4105
4106 get_random_bytes(my_challenge, CHALLENGE_LEN);
4107
Philipp Reisner13e60372011-02-08 09:54:40 +01004108 rv = conn_send_cmd2(tconn, P_AUTH_CHALLENGE, my_challenge, CHALLENGE_LEN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004109 if (!rv)
4110 goto fail;
4111
Philipp Reisner13e60372011-02-08 09:54:40 +01004112 rv = drbd_recv_header(tconn, &pi);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004113 if (!rv)
4114 goto fail;
4115
Philipp Reisner77351055b2011-02-07 17:24:26 +01004116 if (pi.cmd != P_AUTH_CHALLENGE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004117 conn_err(tconn, "expected AuthChallenge packet, received: %s (0x%04x)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004118 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004119 rv = 0;
4120 goto fail;
4121 }
4122
Philipp Reisner77351055b2011-02-07 17:24:26 +01004123 if (pi.size > CHALLENGE_LEN * 2) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004124 conn_err(tconn, "expected AuthChallenge payload too big.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004125 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004126 goto fail;
4127 }
4128
Philipp Reisner77351055b2011-02-07 17:24:26 +01004129 peers_ch = kmalloc(pi.size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004130 if (peers_ch == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004131 conn_err(tconn, "kmalloc of peers_ch failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004132 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004133 goto fail;
4134 }
4135
Philipp Reisner13e60372011-02-08 09:54:40 +01004136 rv = drbd_recv(tconn, peers_ch, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004137
Philipp Reisner77351055b2011-02-07 17:24:26 +01004138 if (rv != pi.size) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01004139 if (!signal_pending(current))
Philipp Reisner13e60372011-02-08 09:54:40 +01004140 conn_warn(tconn, "short read AuthChallenge: l=%u\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004141 rv = 0;
4142 goto fail;
4143 }
4144
Philipp Reisner13e60372011-02-08 09:54:40 +01004145 resp_size = crypto_hash_digestsize(tconn->cram_hmac_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004146 response = kmalloc(resp_size, GFP_NOIO);
4147 if (response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004148 conn_err(tconn, "kmalloc of response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004149 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004150 goto fail;
4151 }
4152
4153 sg_init_table(&sg, 1);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004154 sg_set_buf(&sg, peers_ch, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004155
4156 rv = crypto_hash_digest(&desc, &sg, sg.length, response);
4157 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004158 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004159 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004160 goto fail;
4161 }
4162
Philipp Reisner13e60372011-02-08 09:54:40 +01004163 rv = conn_send_cmd2(tconn, P_AUTH_RESPONSE, response, resp_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004164 if (!rv)
4165 goto fail;
4166
Philipp Reisner13e60372011-02-08 09:54:40 +01004167 rv = drbd_recv_header(tconn, &pi);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004168 if (!rv)
4169 goto fail;
4170
Philipp Reisner77351055b2011-02-07 17:24:26 +01004171 if (pi.cmd != P_AUTH_RESPONSE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004172 conn_err(tconn, "expected AuthResponse packet, received: %s (0x%04x)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004173 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004174 rv = 0;
4175 goto fail;
4176 }
4177
Philipp Reisner77351055b2011-02-07 17:24:26 +01004178 if (pi.size != resp_size) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004179 conn_err(tconn, "expected AuthResponse payload of wrong size\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004180 rv = 0;
4181 goto fail;
4182 }
4183
Philipp Reisner13e60372011-02-08 09:54:40 +01004184 rv = drbd_recv(tconn, response , resp_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004185
4186 if (rv != resp_size) {
Lars Ellenberg0ddc5542011-01-21 12:35:15 +01004187 if (!signal_pending(current))
Philipp Reisner13e60372011-02-08 09:54:40 +01004188 conn_warn(tconn, "short read receiving AuthResponse: l=%u\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004189 rv = 0;
4190 goto fail;
4191 }
4192
4193 right_response = kmalloc(resp_size, GFP_NOIO);
Julia Lawall2d1ee872009-12-27 22:27:11 +01004194 if (right_response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004195 conn_err(tconn, "kmalloc of right_response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004196 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004197 goto fail;
4198 }
4199
4200 sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
4201
4202 rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
4203 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004204 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004205 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004206 goto fail;
4207 }
4208
4209 rv = !memcmp(response, right_response, resp_size);
4210
4211 if (rv)
Philipp Reisner13e60372011-02-08 09:54:40 +01004212 conn_info(tconn, "Peer authenticated using %d bytes of '%s' HMAC\n",
4213 resp_size, tconn->net_conf->cram_hmac_alg);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004214 else
4215 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004216
4217 fail:
4218 kfree(peers_ch);
4219 kfree(response);
4220 kfree(right_response);
4221
4222 return rv;
4223}
4224#endif
4225
4226int drbdd_init(struct drbd_thread *thi)
4227{
4228 struct drbd_conf *mdev = thi->mdev;
4229 unsigned int minor = mdev_to_minor(mdev);
4230 int h;
4231
4232 sprintf(current->comm, "drbd%d_receiver", minor);
4233
4234 dev_info(DEV, "receiver (re)started\n");
4235
4236 do {
Philipp Reisner907599e2011-02-08 11:25:37 +01004237 h = drbd_connect(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004238 if (h == 0) {
Philipp Reisner360cc742011-02-08 14:29:53 +01004239 drbd_disconnect(mdev->tconn);
Philipp Reisner20ee6392011-01-18 15:28:59 +01004240 schedule_timeout_interruptible(HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004241 }
4242 if (h == -1) {
4243 dev_warn(DEV, "Discarding network configuration.\n");
4244 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
4245 }
4246 } while (h == 0);
4247
4248 if (h > 0) {
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01004249 if (get_net_conf(mdev->tconn)) {
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004250 drbdd(mdev->tconn);
Philipp Reisnerb2fb6dbe2011-01-19 13:48:44 +01004251 put_net_conf(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004252 }
4253 }
4254
Philipp Reisner360cc742011-02-08 14:29:53 +01004255 drbd_disconnect(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004256
4257 dev_info(DEV, "receiver terminated\n");
4258 return 0;
4259}
4260
4261/* ********* acknowledge sender ******** */
4262
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004263static int got_RqSReply(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004264{
Philipp Reisner257d0af2011-01-26 12:15:29 +01004265 struct p_req_state_reply *p = &mdev->tconn->meta.rbuf.req_state_reply;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004266
4267 int retcode = be32_to_cpu(p->retcode);
4268
4269 if (retcode >= SS_SUCCESS) {
4270 set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
4271 } else {
4272 set_bit(CL_ST_CHG_FAIL, &mdev->flags);
4273 dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
4274 drbd_set_st_err_str(retcode), retcode);
4275 }
4276 wake_up(&mdev->state_wait);
4277
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004278 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004279}
4280
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004281static int got_Ping(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004282{
4283 return drbd_send_ping_ack(mdev);
4284
4285}
4286
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004287static int got_PingAck(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004288{
4289 /* restore idle timeout */
Philipp Reisnere42325a2011-01-19 13:55:45 +01004290 mdev->tconn->meta.socket->sk->sk_rcvtimeo = mdev->tconn->net_conf->ping_int*HZ;
Philipp Reisner309d1602010-03-02 15:03:44 +01004291 if (!test_and_set_bit(GOT_PING_ACK, &mdev->flags))
4292 wake_up(&mdev->misc_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004293
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004294 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004295}
4296
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004297static int got_IsInSync(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004298{
Philipp Reisner257d0af2011-01-26 12:15:29 +01004299 struct p_block_ack *p = &mdev->tconn->meta.rbuf.block_ack;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004300 sector_t sector = be64_to_cpu(p->sector);
4301 int blksize = be32_to_cpu(p->blksize);
4302
Philipp Reisner31890f42011-01-19 14:12:51 +01004303 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004304
4305 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4306
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004307 if (get_ldev(mdev)) {
4308 drbd_rs_complete_io(mdev, sector);
4309 drbd_set_in_sync(mdev, sector, blksize);
4310 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
4311 mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
4312 put_ldev(mdev);
4313 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004314 dec_rs_pending(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02004315 atomic_add(blksize >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004316
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004317 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004318}
4319
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004320static int
4321validate_req_change_req_state(struct drbd_conf *mdev, u64 id, sector_t sector,
4322 struct rb_root *root, const char *func,
4323 enum drbd_req_event what, bool missing_ok)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004324{
4325 struct drbd_request *req;
4326 struct bio_and_error m;
4327
Philipp Reisner87eeee42011-01-19 14:16:30 +01004328 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004329 req = find_request(mdev, root, id, sector, missing_ok, func);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004330 if (unlikely(!req)) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01004331 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004332 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004333 }
4334 __req_mod(req, what, &m);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004335 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004336
4337 if (m.bio)
4338 complete_master_bio(mdev, &m);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004339 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004340}
4341
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004342static int got_BlockAck(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004343{
Philipp Reisner257d0af2011-01-26 12:15:29 +01004344 struct p_block_ack *p = &mdev->tconn->meta.rbuf.block_ack;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004345 sector_t sector = be64_to_cpu(p->sector);
4346 int blksize = be32_to_cpu(p->blksize);
4347 enum drbd_req_event what;
4348
4349 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4350
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004351 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004352 drbd_set_in_sync(mdev, sector, blksize);
4353 dec_rs_pending(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004354 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004355 }
Philipp Reisner257d0af2011-01-26 12:15:29 +01004356 switch (cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004357 case P_RS_WRITE_ACK:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004358 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004359 what = WRITE_ACKED_BY_PEER_AND_SIS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004360 break;
4361 case P_WRITE_ACK:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004362 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004363 what = WRITE_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004364 break;
4365 case P_RECV_ACK:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004366 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_B);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004367 what = RECV_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004368 break;
4369 case P_DISCARD_ACK:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004370 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004371 what = CONFLICT_DISCARDED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004372 break;
4373 default:
4374 D_ASSERT(0);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004375 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004376 }
4377
4378 return validate_req_change_req_state(mdev, p->block_id, sector,
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004379 &mdev->write_requests, __func__,
4380 what, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004381}
4382
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004383static int got_NegAck(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004384{
Philipp Reisner257d0af2011-01-26 12:15:29 +01004385 struct p_block_ack *p = &mdev->tconn->meta.rbuf.block_ack;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004386 sector_t sector = be64_to_cpu(p->sector);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004387 int size = be32_to_cpu(p->blksize);
Philipp Reisner89e58e72011-01-19 13:12:45 +01004388 bool missing_ok = mdev->tconn->net_conf->wire_protocol == DRBD_PROT_A ||
4389 mdev->tconn->net_conf->wire_protocol == DRBD_PROT_B;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004390 bool found;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004391
4392 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4393
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004394 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004395 dec_rs_pending(mdev);
4396 drbd_rs_failed_io(mdev, sector, size);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004397 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004398 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01004399
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004400 found = validate_req_change_req_state(mdev, p->block_id, sector,
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004401 &mdev->write_requests, __func__,
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004402 NEG_ACKED, missing_ok);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004403 if (!found) {
4404 /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs.
4405 The master bio might already be completed, therefore the
4406 request is no longer in the collision hash. */
4407 /* In Protocol B we might already have got a P_RECV_ACK
4408 but then get a P_NEG_ACK afterwards. */
4409 if (!missing_ok)
Philipp Reisner2deb8332011-01-17 18:39:18 +01004410 return false;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004411 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004412 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01004413 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004414}
4415
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004416static int got_NegDReply(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004417{
Philipp Reisner257d0af2011-01-26 12:15:29 +01004418 struct p_block_ack *p = &mdev->tconn->meta.rbuf.block_ack;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004419 sector_t sector = be64_to_cpu(p->sector);
4420
4421 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4422 dev_err(DEV, "Got NegDReply; Sector %llus, len %u; Fail original request.\n",
4423 (unsigned long long)sector, be32_to_cpu(p->blksize));
4424
4425 return validate_req_change_req_state(mdev, p->block_id, sector,
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004426 &mdev->read_requests, __func__,
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004427 NEG_ACKED, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004428}
4429
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004430static int got_NegRSDReply(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004431{
4432 sector_t sector;
4433 int size;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004434 struct p_block_ack *p = &mdev->tconn->meta.rbuf.block_ack;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004435
4436 sector = be64_to_cpu(p->sector);
4437 size = be32_to_cpu(p->blksize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004438
4439 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4440
4441 dec_rs_pending(mdev);
4442
4443 if (get_ldev_if_state(mdev, D_FAILED)) {
4444 drbd_rs_complete_io(mdev, sector);
Philipp Reisner257d0af2011-01-26 12:15:29 +01004445 switch (cmd) {
Philipp Reisnerd612d302010-12-27 10:53:28 +01004446 case P_NEG_RS_DREPLY:
4447 drbd_rs_failed_io(mdev, sector, size);
4448 case P_RS_CANCEL:
4449 break;
4450 default:
4451 D_ASSERT(0);
4452 put_ldev(mdev);
4453 return false;
4454 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004455 put_ldev(mdev);
4456 }
4457
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004458 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004459}
4460
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004461static int got_BarrierAck(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004462{
Philipp Reisner257d0af2011-01-26 12:15:29 +01004463 struct p_barrier_ack *p = &mdev->tconn->meta.rbuf.barrier_ack;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004464
4465 tl_release(mdev, p->barrier, be32_to_cpu(p->set_size));
4466
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02004467 if (mdev->state.conn == C_AHEAD &&
4468 atomic_read(&mdev->ap_in_flight) == 0 &&
Philipp Reisner370a43e2011-01-14 16:03:11 +01004469 !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->current_epoch->flags)) {
4470 mdev->start_resync_timer.expires = jiffies + HZ;
4471 add_timer(&mdev->start_resync_timer);
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02004472 }
4473
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004474 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004475}
4476
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004477static int got_OVResult(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004478{
Philipp Reisner257d0af2011-01-26 12:15:29 +01004479 struct p_block_ack *p = &mdev->tconn->meta.rbuf.block_ack;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004480 struct drbd_work *w;
4481 sector_t sector;
4482 int size;
4483
4484 sector = be64_to_cpu(p->sector);
4485 size = be32_to_cpu(p->blksize);
4486
4487 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4488
4489 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
4490 drbd_ov_oos_found(mdev, sector, size);
4491 else
4492 ov_oos_print(mdev);
4493
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004494 if (!get_ldev(mdev))
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004495 return true;
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004496
Philipp Reisnerb411b362009-09-25 16:07:19 -07004497 drbd_rs_complete_io(mdev, sector);
4498 dec_rs_pending(mdev);
4499
Lars Ellenbergea5442a2010-11-05 09:48:01 +01004500 --mdev->ov_left;
4501
4502 /* let's advance progress step marks only for every other megabyte */
4503 if ((mdev->ov_left & 0x200) == 0x200)
4504 drbd_advance_rs_marks(mdev, mdev->ov_left);
4505
4506 if (mdev->ov_left == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004507 w = kmalloc(sizeof(*w), GFP_NOIO);
4508 if (w) {
4509 w->cb = w_ov_finished;
Philipp Reisnere42325a2011-01-19 13:55:45 +01004510 drbd_queue_work_front(&mdev->tconn->data.work, w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004511 } else {
4512 dev_err(DEV, "kmalloc(w) failed.");
4513 ov_oos_print(mdev);
4514 drbd_resync_finished(mdev);
4515 }
4516 }
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004517 put_ldev(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004518 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004519}
4520
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004521static int got_skip(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisner0ced55a2010-04-30 15:26:20 +02004522{
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004523 return true;
Philipp Reisner0ced55a2010-04-30 15:26:20 +02004524}
4525
Philipp Reisnerb411b362009-09-25 16:07:19 -07004526struct asender_cmd {
4527 size_t pkt_size;
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004528 int (*process)(struct drbd_conf *mdev, enum drbd_packet cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004529};
4530
4531static struct asender_cmd *get_asender_cmd(int cmd)
4532{
4533 static struct asender_cmd asender_tbl[] = {
4534 /* anything missing from this table is in
4535 * the drbd_cmd_handler (drbd_default_handler) table,
4536 * see the beginning of drbdd() */
Philipp Reisner257d0af2011-01-26 12:15:29 +01004537 [P_PING] = { sizeof(struct p_header), got_Ping },
4538 [P_PING_ACK] = { sizeof(struct p_header), got_PingAck },
Philipp Reisnerb411b362009-09-25 16:07:19 -07004539 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4540 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4541 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4542 [P_DISCARD_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4543 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
4544 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
4545 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply},
4546 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
4547 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
4548 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
4549 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
Philipp Reisner02918be2010-08-20 14:35:10 +02004550 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
Philipp Reisnerd612d302010-12-27 10:53:28 +01004551 [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply},
Philipp Reisnerb411b362009-09-25 16:07:19 -07004552 [P_MAX_CMD] = { 0, NULL },
4553 };
4554 if (cmd > P_MAX_CMD || asender_tbl[cmd].process == NULL)
4555 return NULL;
4556 return &asender_tbl[cmd];
4557}
4558
4559int drbd_asender(struct drbd_thread *thi)
4560{
4561 struct drbd_conf *mdev = thi->mdev;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004562 struct p_header *h = &mdev->tconn->meta.rbuf.header;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004563 struct asender_cmd *cmd = NULL;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004564 struct packet_info pi;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004565
Philipp Reisner257d0af2011-01-26 12:15:29 +01004566 int rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004567 void *buf = h;
4568 int received = 0;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004569 int expect = sizeof(struct p_header);
Lars Ellenbergf36af182011-03-09 22:44:55 +01004570 int ping_timeout_active = 0;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004571 int empty;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004572
4573 sprintf(current->comm, "drbd%d_asender", mdev_to_minor(mdev));
4574
4575 current->policy = SCHED_RR; /* Make this a realtime task! */
4576 current->rt_priority = 2; /* more important than all other tasks */
4577
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01004578 while (get_t_state(thi) == RUNNING) {
Philipp Reisner80822282011-02-08 12:46:30 +01004579 drbd_thread_current_set_cpu(thi);
Philipp Reisnere43ef192011-02-07 14:40:40 +01004580 if (test_and_clear_bit(SEND_PING, &mdev->tconn->flags)) {
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01004581 if (!drbd_send_ping(mdev)) {
4582 dev_err(DEV, "drbd_send_ping has failed\n");
4583 goto reconnect;
4584 }
Philipp Reisnere42325a2011-01-19 13:55:45 +01004585 mdev->tconn->meta.socket->sk->sk_rcvtimeo =
Philipp Reisner89e58e72011-01-19 13:12:45 +01004586 mdev->tconn->net_conf->ping_timeo*HZ/10;
Lars Ellenbergf36af182011-03-09 22:44:55 +01004587 ping_timeout_active = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004588 }
4589
4590 /* conditionally cork;
4591 * it may hurt latency if we cork without much to send */
Philipp Reisner89e58e72011-01-19 13:12:45 +01004592 if (!mdev->tconn->net_conf->no_cork &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07004593 3 < atomic_read(&mdev->unacked_cnt))
Philipp Reisnere42325a2011-01-19 13:55:45 +01004594 drbd_tcp_cork(mdev->tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004595 while (1) {
Philipp Reisner808e37b2011-02-07 14:44:14 +01004596 clear_bit(SIGNAL_ASENDER, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004597 flush_signals(current);
Lars Ellenberg0f8488e2010-10-13 18:19:23 +02004598 if (!drbd_process_done_ee(mdev))
Philipp Reisnerb411b362009-09-25 16:07:19 -07004599 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004600 /* to avoid race with newly queued ACKs */
Philipp Reisner808e37b2011-02-07 14:44:14 +01004601 set_bit(SIGNAL_ASENDER, &mdev->tconn->flags);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004602 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004603 empty = list_empty(&mdev->done_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004604 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004605 /* new ack may have been queued right here,
4606 * but then there is also a signal pending,
4607 * and we start over... */
4608 if (empty)
4609 break;
4610 }
4611 /* but unconditionally uncork unless disabled */
Philipp Reisner89e58e72011-01-19 13:12:45 +01004612 if (!mdev->tconn->net_conf->no_cork)
Philipp Reisnere42325a2011-01-19 13:55:45 +01004613 drbd_tcp_uncork(mdev->tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004614
4615 /* short circuit, recv_msg would return EINTR anyways. */
4616 if (signal_pending(current))
4617 continue;
4618
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +01004619 rv = drbd_recv_short(mdev->tconn->meta.socket, buf, expect-received, 0);
Philipp Reisner808e37b2011-02-07 14:44:14 +01004620 clear_bit(SIGNAL_ASENDER, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004621
4622 flush_signals(current);
4623
4624 /* Note:
4625 * -EINTR (on meta) we got a signal
4626 * -EAGAIN (on meta) rcvtimeo expired
4627 * -ECONNRESET other side closed the connection
4628 * -ERESTARTSYS (on data) we got a signal
4629 * rv < 0 other than above: unexpected error!
4630 * rv == expected: full header or command
4631 * rv < expected: "woken" by signal during receive
4632 * rv == 0 : "connection shut down by peer"
4633 */
4634 if (likely(rv > 0)) {
4635 received += rv;
4636 buf += rv;
4637 } else if (rv == 0) {
4638 dev_err(DEV, "meta connection shut down by peer.\n");
4639 goto reconnect;
4640 } else if (rv == -EAGAIN) {
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02004641 /* If the data socket received something meanwhile,
4642 * that is good enough: peer is still alive. */
Philipp Reisner31890f42011-01-19 14:12:51 +01004643 if (time_after(mdev->tconn->last_received,
Philipp Reisnere42325a2011-01-19 13:55:45 +01004644 jiffies - mdev->tconn->meta.socket->sk->sk_rcvtimeo))
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02004645 continue;
Lars Ellenbergf36af182011-03-09 22:44:55 +01004646 if (ping_timeout_active) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004647 dev_err(DEV, "PingAck did not arrive in time.\n");
4648 goto reconnect;
4649 }
Philipp Reisnere43ef192011-02-07 14:40:40 +01004650 set_bit(SEND_PING, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004651 continue;
4652 } else if (rv == -EINTR) {
4653 continue;
4654 } else {
4655 dev_err(DEV, "sock_recvmsg returned %d\n", rv);
4656 goto reconnect;
4657 }
4658
4659 if (received == expect && cmd == NULL) {
Philipp Reisnerce243852011-02-07 17:27:47 +01004660 if (!decode_header(mdev->tconn, h, &pi))
Philipp Reisnerb411b362009-09-25 16:07:19 -07004661 goto reconnect;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004662 cmd = get_asender_cmd(pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004663 if (unlikely(cmd == NULL)) {
Philipp Reisner257d0af2011-01-26 12:15:29 +01004664 dev_err(DEV, "unknown command %d on meta (l: %d)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004665 pi.cmd, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004666 goto disconnect;
4667 }
4668 expect = cmd->pkt_size;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004669 if (pi.size != expect - sizeof(struct p_header)) {
Philipp Reisner257d0af2011-01-26 12:15:29 +01004670 dev_err(DEV, "Wrong packet size on meta (c: %d, l: %d)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004671 pi.cmd, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004672 goto reconnect;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004673 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004674 }
4675 if (received == expect) {
Philipp Reisner31890f42011-01-19 14:12:51 +01004676 mdev->tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004677 D_ASSERT(cmd != NULL);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004678 if (!cmd->process(mdev, pi.cmd))
Philipp Reisnerb411b362009-09-25 16:07:19 -07004679 goto reconnect;
4680
Lars Ellenbergf36af182011-03-09 22:44:55 +01004681 /* the idle_timeout (ping-int)
4682 * has been restored in got_PingAck() */
4683 if (cmd == get_asender_cmd(P_PING_ACK))
4684 ping_timeout_active = 0;
4685
Philipp Reisnerb411b362009-09-25 16:07:19 -07004686 buf = h;
4687 received = 0;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004688 expect = sizeof(struct p_header);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004689 cmd = NULL;
4690 }
4691 }
4692
4693 if (0) {
4694reconnect:
4695 drbd_force_state(mdev, NS(conn, C_NETWORK_FAILURE));
Lars Ellenberg856c50c2010-10-14 13:37:40 +02004696 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004697 }
4698 if (0) {
4699disconnect:
4700 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
Lars Ellenberg856c50c2010-10-14 13:37:40 +02004701 drbd_md_sync(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004702 }
Philipp Reisner808e37b2011-02-07 14:44:14 +01004703 clear_bit(SIGNAL_ASENDER, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004704
4705 D_ASSERT(mdev->state.conn < C_CONNECTED);
4706 dev_info(DEV, "asender terminated\n");
4707
4708 return 0;
4709}