blob: 74ed3ac263f36d792576a8c5ae01224602cdbc16 [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;
Andreas Gruenbachere2857212011-03-25 00:57:38 +010053 unsigned int size;
54 unsigned int vnr;
Philipp Reisner77351055b2011-02-07 17:24:26 +010055};
56
Philipp Reisnerb411b362009-09-25 16:07:19 -070057enum finish_epoch {
58 FE_STILL_LIVE,
59 FE_DESTROYED,
60 FE_RECYCLED,
61};
62
Andreas Gruenbacher60381782011-03-28 17:05:50 +020063static int drbd_do_features(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);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +010068static int e_end_block(struct drbd_work *, int);
Philipp Reisnerb411b362009-09-25 16:07:19 -070069
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
Philipp Reisner81a5d602011-02-22 19:53:16 -0500287 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;
Philipp Reisnera21e9292011-02-08 15:08:49 +0100348 peer_req->w.mdev = mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100349 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
Philipp Reisner32862ec2011-02-08 16:41:01 +0100395/* See also comments in _req_mod(,BARRIER_ACKED)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700396 * and receive_Barrier.
397 *
398 * Move entries from net_ee to done_ee, if ready.
399 * Grab done_ee, call all callbacks, free the entries.
400 * The callbacks typically send out ACKs.
401 */
402static int drbd_process_done_ee(struct drbd_conf *mdev)
403{
404 LIST_HEAD(work_list);
405 LIST_HEAD(reclaimed);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100406 struct drbd_peer_request *peer_req, *t;
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100407 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700408
Philipp Reisner87eeee42011-01-19 14:16:30 +0100409 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700410 reclaim_net_ee(mdev, &reclaimed);
411 list_splice_init(&mdev->done_ee, &work_list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100412 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700413
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100414 list_for_each_entry_safe(peer_req, t, &reclaimed, w.list)
415 drbd_free_net_ee(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700416
417 /* possible callbacks here:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100418 * e_end_block, and e_end_resync_block, e_send_discard_write.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700419 * all ignore the last argument.
420 */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100421 list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100422 int err2;
423
Philipp Reisnerb411b362009-09-25 16:07:19 -0700424 /* list_del not necessary, next/prev members not touched */
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100425 err2 = peer_req->w.cb(&peer_req->w, !!err);
426 if (!err)
427 err = err2;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100428 drbd_free_ee(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700429 }
430 wake_up(&mdev->ee_wait);
431
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100432 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700433}
434
435void _drbd_wait_ee_list_empty(struct drbd_conf *mdev, struct list_head *head)
436{
437 DEFINE_WAIT(wait);
438
439 /* avoids spin_lock/unlock
440 * and calling prepare_to_wait in the fast path */
441 while (!list_empty(head)) {
442 prepare_to_wait(&mdev->ee_wait, &wait, TASK_UNINTERRUPTIBLE);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100443 spin_unlock_irq(&mdev->tconn->req_lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100444 io_schedule();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700445 finish_wait(&mdev->ee_wait, &wait);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100446 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700447 }
448}
449
450void drbd_wait_ee_list_empty(struct drbd_conf *mdev, struct list_head *head)
451{
Philipp Reisner87eeee42011-01-19 14:16:30 +0100452 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700453 _drbd_wait_ee_list_empty(mdev, head);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100454 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700455}
456
457/* see also kernel_accept; which is only present since 2.6.18.
458 * also we want to log which part of it failed, exactly */
Philipp Reisner76536202011-02-07 14:09:54 +0100459static int drbd_accept(const char **what, struct socket *sock, struct socket **newsock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700460{
461 struct sock *sk = sock->sk;
462 int err = 0;
463
464 *what = "listen";
465 err = sock->ops->listen(sock, 5);
466 if (err < 0)
467 goto out;
468
469 *what = "sock_create_lite";
470 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
471 newsock);
472 if (err < 0)
473 goto out;
474
475 *what = "accept";
476 err = sock->ops->accept(sock, *newsock, 0);
477 if (err < 0) {
478 sock_release(*newsock);
479 *newsock = NULL;
480 goto out;
481 }
482 (*newsock)->ops = sock->ops;
483
484out:
485 return err;
486}
487
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100488static int drbd_recv_short(struct socket *sock, void *buf, size_t size, int flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700489{
490 mm_segment_t oldfs;
491 struct kvec iov = {
492 .iov_base = buf,
493 .iov_len = size,
494 };
495 struct msghdr msg = {
496 .msg_iovlen = 1,
497 .msg_iov = (struct iovec *)&iov,
498 .msg_flags = (flags ? flags : MSG_WAITALL | MSG_NOSIGNAL)
499 };
500 int rv;
501
502 oldfs = get_fs();
503 set_fs(KERNEL_DS);
504 rv = sock_recvmsg(sock, &msg, size, msg.msg_flags);
505 set_fs(oldfs);
506
507 return rv;
508}
509
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100510static int drbd_recv(struct drbd_tconn *tconn, void *buf, size_t size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700511{
512 mm_segment_t oldfs;
513 struct kvec iov = {
514 .iov_base = buf,
515 .iov_len = size,
516 };
517 struct msghdr msg = {
518 .msg_iovlen = 1,
519 .msg_iov = (struct iovec *)&iov,
520 .msg_flags = MSG_WAITALL | MSG_NOSIGNAL
521 };
522 int rv;
523
524 oldfs = get_fs();
525 set_fs(KERNEL_DS);
526
527 for (;;) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100528 rv = sock_recvmsg(tconn->data.socket, &msg, size, msg.msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700529 if (rv == size)
530 break;
531
532 /* Note:
533 * ECONNRESET other side closed the connection
534 * ERESTARTSYS (on sock) we got a signal
535 */
536
537 if (rv < 0) {
538 if (rv == -ECONNRESET)
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100539 conn_info(tconn, "sock was reset by peer\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700540 else if (rv != -ERESTARTSYS)
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100541 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700542 break;
543 } else if (rv == 0) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100544 conn_info(tconn, "sock was shut down by peer\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700545 break;
546 } else {
547 /* signal came in, or peer/link went down,
548 * after we read a partial message
549 */
550 /* D_ASSERT(signal_pending(current)); */
551 break;
552 }
553 };
554
555 set_fs(oldfs);
556
557 if (rv != size)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100558 conn_request_state(tconn, NS(conn, C_BROKEN_PIPE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700559
560 return rv;
561}
562
Andreas Gruenbacherc6967742011-03-17 17:15:20 +0100563static int drbd_recv_all(struct drbd_tconn *tconn, void *buf, size_t size)
564{
565 int err;
566
567 err = drbd_recv(tconn, buf, size);
568 if (err != size) {
569 if (err >= 0)
570 err = -EIO;
571 } else
572 err = 0;
573 return err;
574}
575
Andreas Gruenbachera5c31902011-03-24 03:28:04 +0100576static int drbd_recv_all_warn(struct drbd_tconn *tconn, void *buf, size_t size)
577{
578 int err;
579
580 err = drbd_recv_all(tconn, buf, size);
581 if (err && !signal_pending(current))
582 conn_warn(tconn, "short read (expected size %d)\n", (int)size);
583 return err;
584}
585
Lars Ellenberg5dbf1672010-05-25 16:18:01 +0200586/* quoting tcp(7):
587 * On individual connections, the socket buffer size must be set prior to the
588 * listen(2) or connect(2) calls in order to have it take effect.
589 * This is our wrapper to do so.
590 */
591static void drbd_setbufsize(struct socket *sock, unsigned int snd,
592 unsigned int rcv)
593{
594 /* open coded SO_SNDBUF, SO_RCVBUF */
595 if (snd) {
596 sock->sk->sk_sndbuf = snd;
597 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
598 }
599 if (rcv) {
600 sock->sk->sk_rcvbuf = rcv;
601 sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
602 }
603}
604
Philipp Reisnereac3e992011-02-07 14:05:07 +0100605static struct socket *drbd_try_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700606{
607 const char *what;
608 struct socket *sock;
609 struct sockaddr_in6 src_in6;
610 int err;
611 int disconnect_on_error = 1;
612
Philipp Reisnereac3e992011-02-07 14:05:07 +0100613 if (!get_net_conf(tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700614 return NULL;
615
616 what = "sock_create_kern";
Philipp Reisnereac3e992011-02-07 14:05:07 +0100617 err = sock_create_kern(((struct sockaddr *)tconn->net_conf->my_addr)->sa_family,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700618 SOCK_STREAM, IPPROTO_TCP, &sock);
619 if (err < 0) {
620 sock = NULL;
621 goto out;
622 }
623
624 sock->sk->sk_rcvtimeo =
Philipp Reisnereac3e992011-02-07 14:05:07 +0100625 sock->sk->sk_sndtimeo = tconn->net_conf->try_connect_int*HZ;
626 drbd_setbufsize(sock, tconn->net_conf->sndbuf_size,
627 tconn->net_conf->rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700628
629 /* explicitly bind to the configured IP as source IP
630 * for the outgoing connections.
631 * This is needed for multihomed hosts and to be
632 * able to use lo: interfaces for drbd.
633 * Make sure to use 0 as port number, so linux selects
634 * a free one dynamically.
635 */
Philipp Reisnereac3e992011-02-07 14:05:07 +0100636 memcpy(&src_in6, tconn->net_conf->my_addr,
637 min_t(int, tconn->net_conf->my_addr_len, sizeof(src_in6)));
638 if (((struct sockaddr *)tconn->net_conf->my_addr)->sa_family == AF_INET6)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700639 src_in6.sin6_port = 0;
640 else
641 ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */
642
643 what = "bind before connect";
644 err = sock->ops->bind(sock,
645 (struct sockaddr *) &src_in6,
Philipp Reisnereac3e992011-02-07 14:05:07 +0100646 tconn->net_conf->my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700647 if (err < 0)
648 goto out;
649
650 /* connect may fail, peer not yet available.
651 * stay C_WF_CONNECTION, don't go Disconnecting! */
652 disconnect_on_error = 0;
653 what = "connect";
654 err = sock->ops->connect(sock,
Philipp Reisnereac3e992011-02-07 14:05:07 +0100655 (struct sockaddr *)tconn->net_conf->peer_addr,
656 tconn->net_conf->peer_addr_len, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700657
658out:
659 if (err < 0) {
660 if (sock) {
661 sock_release(sock);
662 sock = NULL;
663 }
664 switch (-err) {
665 /* timeout, busy, signal pending */
666 case ETIMEDOUT: case EAGAIN: case EINPROGRESS:
667 case EINTR: case ERESTARTSYS:
668 /* peer not (yet) available, network problem */
669 case ECONNREFUSED: case ENETUNREACH:
670 case EHOSTDOWN: case EHOSTUNREACH:
671 disconnect_on_error = 0;
672 break;
673 default:
Philipp Reisnereac3e992011-02-07 14:05:07 +0100674 conn_err(tconn, "%s failed, err = %d\n", what, err);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700675 }
676 if (disconnect_on_error)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100677 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700678 }
Philipp Reisnereac3e992011-02-07 14:05:07 +0100679 put_net_conf(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700680 return sock;
681}
682
Philipp Reisner76536202011-02-07 14:09:54 +0100683static struct socket *drbd_wait_for_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700684{
685 int timeo, err;
686 struct socket *s_estab = NULL, *s_listen;
687 const char *what;
688
Philipp Reisner76536202011-02-07 14:09:54 +0100689 if (!get_net_conf(tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700690 return NULL;
691
692 what = "sock_create_kern";
Philipp Reisner76536202011-02-07 14:09:54 +0100693 err = sock_create_kern(((struct sockaddr *)tconn->net_conf->my_addr)->sa_family,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700694 SOCK_STREAM, IPPROTO_TCP, &s_listen);
695 if (err) {
696 s_listen = NULL;
697 goto out;
698 }
699
Philipp Reisner76536202011-02-07 14:09:54 +0100700 timeo = tconn->net_conf->try_connect_int * HZ;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700701 timeo += (random32() & 1) ? timeo / 7 : -timeo / 7; /* 28.5% random jitter */
702
703 s_listen->sk->sk_reuse = 1; /* SO_REUSEADDR */
704 s_listen->sk->sk_rcvtimeo = timeo;
705 s_listen->sk->sk_sndtimeo = timeo;
Philipp Reisner76536202011-02-07 14:09:54 +0100706 drbd_setbufsize(s_listen, tconn->net_conf->sndbuf_size,
707 tconn->net_conf->rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700708
709 what = "bind before listen";
710 err = s_listen->ops->bind(s_listen,
Philipp Reisner76536202011-02-07 14:09:54 +0100711 (struct sockaddr *) tconn->net_conf->my_addr,
712 tconn->net_conf->my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700713 if (err < 0)
714 goto out;
715
Philipp Reisner76536202011-02-07 14:09:54 +0100716 err = drbd_accept(&what, s_listen, &s_estab);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700717
718out:
719 if (s_listen)
720 sock_release(s_listen);
721 if (err < 0) {
722 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
Philipp Reisner76536202011-02-07 14:09:54 +0100723 conn_err(tconn, "%s failed, err = %d\n", what, err);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100724 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700725 }
726 }
Philipp Reisner76536202011-02-07 14:09:54 +0100727 put_net_conf(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700728
729 return s_estab;
730}
731
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200732static int decode_header(struct drbd_tconn *, struct p_header *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700733
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200734static int send_first_packet(struct drbd_tconn *tconn, struct drbd_socket *sock,
735 enum drbd_packet cmd)
736{
737 if (!conn_prepare_command(tconn, sock))
738 return -EIO;
739 return conn_send_command(tconn, sock, cmd, sizeof(struct p_header), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700740}
741
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200742static int receive_first_packet(struct drbd_tconn *tconn, struct socket *sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700743{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200744 unsigned int header_size = drbd_header_size(tconn);
745 struct packet_info pi;
746 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700747
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200748 err = drbd_recv_short(sock, tconn->data.rbuf, header_size, 0);
749 if (err != header_size) {
750 if (err >= 0)
751 err = -EIO;
752 return err;
753 }
754 err = decode_header(tconn, tconn->data.rbuf, &pi);
755 if (err)
756 return err;
757 return pi.cmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700758}
759
760/**
761 * drbd_socket_okay() - Free the socket if its connection is not okay
Philipp Reisnerb411b362009-09-25 16:07:19 -0700762 * @sock: pointer to the pointer to the socket.
763 */
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100764static int drbd_socket_okay(struct socket **sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700765{
766 int rr;
767 char tb[4];
768
769 if (!*sock)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100770 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700771
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100772 rr = drbd_recv_short(*sock, tb, 4, MSG_DONTWAIT | MSG_PEEK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700773
774 if (rr > 0 || rr == -EAGAIN) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100775 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700776 } else {
777 sock_release(*sock);
778 *sock = NULL;
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100779 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700780 }
781}
Philipp Reisner2325eb62011-03-15 16:56:18 +0100782/* Gets called if a connection is established, or if a new minor gets created
783 in a connection */
784int drbd_connected(int vnr, void *p, void *data)
Philipp Reisner907599e2011-02-08 11:25:37 +0100785{
786 struct drbd_conf *mdev = (struct drbd_conf *)p;
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100787 int err;
Philipp Reisner907599e2011-02-08 11:25:37 +0100788
789 atomic_set(&mdev->packet_seq, 0);
790 mdev->peer_seq = 0;
791
Philipp Reisner8410da8f02011-02-11 20:11:10 +0100792 mdev->state_mutex = mdev->tconn->agreed_pro_version < 100 ?
793 &mdev->tconn->cstate_mutex :
794 &mdev->own_state_mutex;
795
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100796 err = drbd_send_sync_param(mdev);
797 if (!err)
798 err = drbd_send_sizes(mdev, 0, 0);
799 if (!err)
800 err = drbd_send_uuids(mdev);
801 if (!err)
802 err = drbd_send_state(mdev);
Philipp Reisner907599e2011-02-08 11:25:37 +0100803 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
804 clear_bit(RESIZE_PENDING, &mdev->flags);
Philipp Reisner8b924f12011-03-01 11:08:28 +0100805 mod_timer(&mdev->request_timer, jiffies + HZ); /* just start it here. */
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100806 return err;
Philipp Reisner907599e2011-02-08 11:25:37 +0100807}
808
Philipp Reisnerb411b362009-09-25 16:07:19 -0700809/*
810 * return values:
811 * 1 yes, we have a valid connection
812 * 0 oops, did not work out, please try again
813 * -1 peer talks different language,
814 * no point in trying again, please go standalone.
815 * -2 We do not have a network config...
816 */
Philipp Reisner907599e2011-02-08 11:25:37 +0100817static int drbd_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700818{
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200819 struct socket *sock, *msock;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700820 int try, h, ok;
821
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100822 if (conn_request_state(tconn, NS(conn, C_WF_CONNECTION), CS_VERBOSE) < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700823 return -2;
824
Philipp Reisner907599e2011-02-08 11:25:37 +0100825 clear_bit(DISCARD_CONCURRENT, &tconn->flags);
Andreas Gruenbacher0916e0e2011-03-21 14:10:15 +0100826
827 /* Assume that the peer only understands protocol 80 until we know better. */
828 tconn->agreed_pro_version = 80;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700829
Philipp Reisnerb411b362009-09-25 16:07:19 -0700830 do {
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200831 struct socket *s;
832
Philipp Reisnerb411b362009-09-25 16:07:19 -0700833 for (try = 0;;) {
834 /* 3 tries, this should take less than a second! */
Philipp Reisner907599e2011-02-08 11:25:37 +0100835 s = drbd_try_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700836 if (s || ++try >= 3)
837 break;
838 /* give the other side time to call bind() & listen() */
Philipp Reisner20ee6392011-01-18 15:28:59 +0100839 schedule_timeout_interruptible(HZ / 10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700840 }
841
842 if (s) {
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200843 if (!tconn->data.socket) {
844 tconn->data.socket = s;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200845 send_first_packet(tconn, &tconn->data, P_INITIAL_DATA);
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200846 } else if (!tconn->meta.socket) {
847 tconn->meta.socket = s;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200848 send_first_packet(tconn, &tconn->meta, P_INITIAL_META);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700849 } else {
Philipp Reisner907599e2011-02-08 11:25:37 +0100850 conn_err(tconn, "Logic error in drbd_connect()\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700851 goto out_release_sockets;
852 }
853 }
854
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200855 if (tconn->data.socket && tconn->meta.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100856 schedule_timeout_interruptible(tconn->net_conf->ping_timeo*HZ/10);
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200857 ok = drbd_socket_okay(&tconn->data.socket);
858 ok = drbd_socket_okay(&tconn->meta.socket) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700859 if (ok)
860 break;
861 }
862
863retry:
Philipp Reisner907599e2011-02-08 11:25:37 +0100864 s = drbd_wait_for_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700865 if (s) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200866 try = receive_first_packet(tconn, s);
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200867 drbd_socket_okay(&tconn->data.socket);
868 drbd_socket_okay(&tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700869 switch (try) {
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200870 case P_INITIAL_DATA:
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200871 if (tconn->data.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100872 conn_warn(tconn, "initial packet S crossed\n");
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200873 sock_release(tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700874 }
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200875 tconn->data.socket = s;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700876 break;
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200877 case P_INITIAL_META:
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200878 if (tconn->meta.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100879 conn_warn(tconn, "initial packet M crossed\n");
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200880 sock_release(tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700881 }
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200882 tconn->meta.socket = s;
Philipp Reisner907599e2011-02-08 11:25:37 +0100883 set_bit(DISCARD_CONCURRENT, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700884 break;
885 default:
Philipp Reisner907599e2011-02-08 11:25:37 +0100886 conn_warn(tconn, "Error receiving initial packet\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700887 sock_release(s);
888 if (random32() & 1)
889 goto retry;
890 }
891 }
892
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100893 if (tconn->cstate <= C_DISCONNECTING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700894 goto out_release_sockets;
895 if (signal_pending(current)) {
896 flush_signals(current);
897 smp_rmb();
Philipp Reisner907599e2011-02-08 11:25:37 +0100898 if (get_t_state(&tconn->receiver) == EXITING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700899 goto out_release_sockets;
900 }
901
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200902 if (tconn->data.socket && &tconn->meta.socket) {
903 ok = drbd_socket_okay(&tconn->data.socket);
904 ok = drbd_socket_okay(&tconn->meta.socket) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700905 if (ok)
906 break;
907 }
908 } while (1);
909
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200910 sock = tconn->data.socket;
911 msock = tconn->meta.socket;
912
Philipp Reisnerb411b362009-09-25 16:07:19 -0700913 msock->sk->sk_reuse = 1; /* SO_REUSEADDR */
914 sock->sk->sk_reuse = 1; /* SO_REUSEADDR */
915
916 sock->sk->sk_allocation = GFP_NOIO;
917 msock->sk->sk_allocation = GFP_NOIO;
918
919 sock->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
920 msock->sk->sk_priority = TC_PRIO_INTERACTIVE;
921
Philipp Reisnerb411b362009-09-25 16:07:19 -0700922 /* NOT YET ...
Philipp Reisner907599e2011-02-08 11:25:37 +0100923 * sock->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700924 * sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
Andreas Gruenbacher60381782011-03-28 17:05:50 +0200925 * first set it to the P_CONNECTION_FEATURES timeout,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700926 * which we set to 4x the configured ping_timeout. */
927 sock->sk->sk_sndtimeo =
Philipp Reisner907599e2011-02-08 11:25:37 +0100928 sock->sk->sk_rcvtimeo = tconn->net_conf->ping_timeo*4*HZ/10;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700929
Philipp Reisner907599e2011-02-08 11:25:37 +0100930 msock->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
931 msock->sk->sk_rcvtimeo = tconn->net_conf->ping_int*HZ;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700932
933 /* we don't want delays.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300934 * we use TCP_CORK where appropriate, though */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700935 drbd_tcp_nodelay(sock);
936 drbd_tcp_nodelay(msock);
937
Philipp Reisner907599e2011-02-08 11:25:37 +0100938 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700939
Andreas Gruenbacher60381782011-03-28 17:05:50 +0200940 h = drbd_do_features(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700941 if (h <= 0)
942 return h;
943
Philipp Reisner907599e2011-02-08 11:25:37 +0100944 if (tconn->cram_hmac_tfm) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700945 /* drbd_request_state(mdev, NS(conn, WFAuth)); */
Philipp Reisner907599e2011-02-08 11:25:37 +0100946 switch (drbd_do_auth(tconn)) {
Johannes Thomab10d96c2010-01-07 16:02:50 +0100947 case -1:
Philipp Reisner907599e2011-02-08 11:25:37 +0100948 conn_err(tconn, "Authentication of peer failed\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700949 return -1;
Johannes Thomab10d96c2010-01-07 16:02:50 +0100950 case 0:
Philipp Reisner907599e2011-02-08 11:25:37 +0100951 conn_err(tconn, "Authentication of peer failed, trying again.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +0100952 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700953 }
954 }
955
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100956 if (conn_request_state(tconn, NS(conn, C_WF_REPORT_PARAMS), CS_VERBOSE) < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700957 return 0;
958
Philipp Reisner907599e2011-02-08 11:25:37 +0100959 sock->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700960 sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
961
Philipp Reisner907599e2011-02-08 11:25:37 +0100962 drbd_thread_start(&tconn->asender);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700963
Andreas Gruenbacher387eb302011-03-16 01:05:37 +0100964 if (drbd_send_protocol(tconn) == -EOPNOTSUPP)
Philipp Reisner7e2455c2010-04-22 14:50:23 +0200965 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700966
Philipp Reisner907599e2011-02-08 11:25:37 +0100967 return !idr_for_each(&tconn->volumes, drbd_connected, tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700968
969out_release_sockets:
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200970 if (tconn->data.socket) {
971 sock_release(tconn->data.socket);
972 tconn->data.socket = NULL;
973 }
974 if (tconn->meta.socket) {
975 sock_release(tconn->meta.socket);
976 tconn->meta.socket = NULL;
977 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700978 return -1;
979}
980
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +0100981static int decode_header(struct drbd_tconn *tconn, struct p_header *h, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700982{
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100983 if (h->h80.magic == cpu_to_be32(DRBD_MAGIC)) {
Philipp Reisner77351055b2011-02-07 17:24:26 +0100984 pi->cmd = be16_to_cpu(h->h80.command);
985 pi->size = be16_to_cpu(h->h80.length);
Philipp Reisnereefc2f72011-02-08 12:55:24 +0100986 pi->vnr = 0;
Andreas Gruenbacherca9bc122011-01-11 13:47:24 +0100987 } else if (h->h95.magic == cpu_to_be16(DRBD_MAGIC_BIG)) {
Philipp Reisner77351055b2011-02-07 17:24:26 +0100988 pi->cmd = be16_to_cpu(h->h95.command);
989 pi->size = be32_to_cpu(h->h95.length) & 0x00ffffff;
990 pi->vnr = 0;
Philipp Reisner02918be2010-08-20 14:35:10 +0200991 } else {
Philipp Reisnerce243852011-02-07 17:27:47 +0100992 conn_err(tconn, "magic?? on data m: 0x%08x c: %d l: %d\n",
Lars Ellenberg004352f2010-10-05 20:13:58 +0200993 be32_to_cpu(h->h80.magic),
994 be16_to_cpu(h->h80.command),
995 be16_to_cpu(h->h80.length));
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +0100996 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700997 }
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +0100998 return 0;
Philipp Reisner257d0af2011-01-26 12:15:29 +0100999}
1000
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001001static int drbd_recv_header(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner257d0af2011-01-26 12:15:29 +01001002{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01001003 struct p_header *h = tconn->data.rbuf;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001004 int err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001005
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02001006 err = drbd_recv_all_warn(tconn, h, drbd_header_size(tconn));
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001007 if (err)
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001008 return err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001009
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001010 err = decode_header(tconn, h, pi);
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001011 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001012
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001013 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001014}
1015
Philipp Reisner2451fc32010-08-24 13:43:11 +02001016static void drbd_flush(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001017{
1018 int rv;
1019
1020 if (mdev->write_ordering >= WO_bdev_flush && get_ldev(mdev)) {
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +04001021 rv = blkdev_issue_flush(mdev->ldev->backing_bdev, GFP_KERNEL,
Christoph Hellwigdd3932e2010-09-16 20:51:46 +02001022 NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001023 if (rv) {
1024 dev_err(DEV, "local disk flush failed with status %d\n", rv);
1025 /* would rather check on EOPNOTSUPP, but that is not reliable.
1026 * don't try again for ANY return value != 0
1027 * if (rv == -EOPNOTSUPP) */
1028 drbd_bump_write_ordering(mdev, WO_drain_io);
1029 }
1030 put_ldev(mdev);
1031 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001032}
1033
1034/**
1035 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
1036 * @mdev: DRBD device.
1037 * @epoch: Epoch object.
1038 * @ev: Epoch event.
1039 */
1040static enum finish_epoch drbd_may_finish_epoch(struct drbd_conf *mdev,
1041 struct drbd_epoch *epoch,
1042 enum epoch_event ev)
1043{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001044 int epoch_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001045 struct drbd_epoch *next_epoch;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001046 enum finish_epoch rv = FE_STILL_LIVE;
1047
1048 spin_lock(&mdev->epoch_lock);
1049 do {
1050 next_epoch = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001051
1052 epoch_size = atomic_read(&epoch->epoch_size);
1053
1054 switch (ev & ~EV_CLEANUP) {
1055 case EV_PUT:
1056 atomic_dec(&epoch->active);
1057 break;
1058 case EV_GOT_BARRIER_NR:
1059 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001060 break;
1061 case EV_BECAME_LAST:
1062 /* nothing to do*/
1063 break;
1064 }
1065
Philipp Reisnerb411b362009-09-25 16:07:19 -07001066 if (epoch_size != 0 &&
1067 atomic_read(&epoch->active) == 0 &&
Philipp Reisner2451fc32010-08-24 13:43:11 +02001068 test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001069 if (!(ev & EV_CLEANUP)) {
1070 spin_unlock(&mdev->epoch_lock);
1071 drbd_send_b_ack(mdev, epoch->barrier_nr, epoch_size);
1072 spin_lock(&mdev->epoch_lock);
1073 }
1074 dec_unacked(mdev);
1075
1076 if (mdev->current_epoch != epoch) {
1077 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1078 list_del(&epoch->list);
1079 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
1080 mdev->epochs--;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001081 kfree(epoch);
1082
1083 if (rv == FE_STILL_LIVE)
1084 rv = FE_DESTROYED;
1085 } else {
1086 epoch->flags = 0;
1087 atomic_set(&epoch->epoch_size, 0);
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001088 /* atomic_set(&epoch->active, 0); is already zero */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001089 if (rv == FE_STILL_LIVE)
1090 rv = FE_RECYCLED;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001091 wake_up(&mdev->ee_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001092 }
1093 }
1094
1095 if (!next_epoch)
1096 break;
1097
1098 epoch = next_epoch;
1099 } while (1);
1100
1101 spin_unlock(&mdev->epoch_lock);
1102
Philipp Reisnerb411b362009-09-25 16:07:19 -07001103 return rv;
1104}
1105
1106/**
1107 * drbd_bump_write_ordering() - Fall back to an other write ordering method
1108 * @mdev: DRBD device.
1109 * @wo: Write ordering method to try.
1110 */
1111void drbd_bump_write_ordering(struct drbd_conf *mdev, enum write_ordering_e wo) __must_hold(local)
1112{
1113 enum write_ordering_e pwo;
1114 static char *write_ordering_str[] = {
1115 [WO_none] = "none",
1116 [WO_drain_io] = "drain",
1117 [WO_bdev_flush] = "flush",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001118 };
1119
1120 pwo = mdev->write_ordering;
1121 wo = min(pwo, wo);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001122 if (wo == WO_bdev_flush && mdev->ldev->dc.no_disk_flush)
1123 wo = WO_drain_io;
1124 if (wo == WO_drain_io && mdev->ldev->dc.no_disk_drain)
1125 wo = WO_none;
1126 mdev->write_ordering = wo;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001127 if (pwo != mdev->write_ordering || wo == WO_bdev_flush)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001128 dev_info(DEV, "Method to ensure write ordering: %s\n", write_ordering_str[mdev->write_ordering]);
1129}
1130
1131/**
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001132 * drbd_submit_peer_request()
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001133 * @mdev: DRBD device.
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001134 * @peer_req: peer request
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001135 * @rw: flag field, see bio->bi_rw
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001136 *
1137 * May spread the pages to multiple bios,
1138 * depending on bio_add_page restrictions.
1139 *
1140 * Returns 0 if all bios have been submitted,
1141 * -ENOMEM if we could not allocate enough bios,
1142 * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a
1143 * single page to an empty bio (which should never happen and likely indicates
1144 * that the lower level IO stack is in some way broken). This has been observed
1145 * on certain Xen deployments.
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001146 */
1147/* TODO allocate from our own bio_set. */
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001148int drbd_submit_peer_request(struct drbd_conf *mdev,
1149 struct drbd_peer_request *peer_req,
1150 const unsigned rw, const int fault_type)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001151{
1152 struct bio *bios = NULL;
1153 struct bio *bio;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001154 struct page *page = peer_req->pages;
1155 sector_t sector = peer_req->i.sector;
1156 unsigned ds = peer_req->i.size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001157 unsigned n_bios = 0;
1158 unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001159 int err = -ENOMEM;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001160
1161 /* In most cases, we will only need one bio. But in case the lower
1162 * level restrictions happen to be different at this offset on this
1163 * side than those of the sending peer, we may need to submit the
Lars Ellenbergda4a75d2011-02-23 17:02:01 +01001164 * request in more than one bio.
1165 *
1166 * Plain bio_alloc is good enough here, this is no DRBD internally
1167 * generated bio, but a bio allocated on behalf of the peer.
1168 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001169next_bio:
1170 bio = bio_alloc(GFP_NOIO, nr_pages);
1171 if (!bio) {
1172 dev_err(DEV, "submit_ee: Allocation of a bio failed\n");
1173 goto fail;
1174 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001175 /* > peer_req->i.sector, unless this is the first bio */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001176 bio->bi_sector = sector;
1177 bio->bi_bdev = mdev->ldev->backing_bdev;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001178 bio->bi_rw = rw;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001179 bio->bi_private = peer_req;
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001180 bio->bi_end_io = drbd_peer_request_endio;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001181
1182 bio->bi_next = bios;
1183 bios = bio;
1184 ++n_bios;
1185
1186 page_chain_for_each(page) {
1187 unsigned len = min_t(unsigned, ds, PAGE_SIZE);
1188 if (!bio_add_page(bio, page, len, 0)) {
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001189 /* A single page must always be possible!
1190 * But in case it fails anyways,
1191 * we deal with it, and complain (below). */
1192 if (bio->bi_vcnt == 0) {
1193 dev_err(DEV,
1194 "bio_add_page failed for len=%u, "
1195 "bi_vcnt=0 (bi_sector=%llu)\n",
1196 len, (unsigned long long)bio->bi_sector);
1197 err = -ENOSPC;
1198 goto fail;
1199 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001200 goto next_bio;
1201 }
1202 ds -= len;
1203 sector += len >> 9;
1204 --nr_pages;
1205 }
1206 D_ASSERT(page == NULL);
1207 D_ASSERT(ds == 0);
1208
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001209 atomic_set(&peer_req->pending_bios, n_bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001210 do {
1211 bio = bios;
1212 bios = bios->bi_next;
1213 bio->bi_next = NULL;
1214
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001215 drbd_generic_make_request(mdev, fault_type, bio);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001216 } while (bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001217 return 0;
1218
1219fail:
1220 while (bios) {
1221 bio = bios;
1222 bios = bios->bi_next;
1223 bio_put(bio);
1224 }
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001225 return err;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001226}
1227
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001228static void drbd_remove_epoch_entry_interval(struct drbd_conf *mdev,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001229 struct drbd_peer_request *peer_req)
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001230{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001231 struct drbd_interval *i = &peer_req->i;
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001232
1233 drbd_remove_interval(&mdev->write_requests, i);
1234 drbd_clear_interval(i);
1235
Andreas Gruenbacher6c852be2011-02-04 15:38:52 +01001236 /* Wake up any processes waiting for this peer request to complete. */
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001237 if (i->waiting)
1238 wake_up(&mdev->misc_wait);
1239}
1240
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001241static int receive_Barrier(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001242{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001243 struct drbd_conf *mdev;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001244 int rv;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001245 struct p_barrier *p = tconn->data.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001246 struct drbd_epoch *epoch;
1247
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001248 mdev = vnr_to_mdev(tconn, pi->vnr);
1249 if (!mdev)
1250 return -EIO;
1251
Philipp Reisnerb411b362009-09-25 16:07:19 -07001252 inc_unacked(mdev);
1253
Philipp Reisnerb411b362009-09-25 16:07:19 -07001254 mdev->current_epoch->barrier_nr = p->barrier;
1255 rv = drbd_may_finish_epoch(mdev, mdev->current_epoch, EV_GOT_BARRIER_NR);
1256
1257 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
1258 * the activity log, which means it would not be resynced in case the
1259 * R_PRIMARY crashes now.
1260 * Therefore we must send the barrier_ack after the barrier request was
1261 * completed. */
1262 switch (mdev->write_ordering) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001263 case WO_none:
1264 if (rv == FE_RECYCLED)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001265 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001266
1267 /* receiver context, in the writeout path of the other node.
1268 * avoid potential distributed deadlock */
1269 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1270 if (epoch)
1271 break;
1272 else
1273 dev_warn(DEV, "Allocation of an epoch failed, slowing down\n");
1274 /* Fall through */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001275
1276 case WO_bdev_flush:
1277 case WO_drain_io:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001278 drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
Philipp Reisner2451fc32010-08-24 13:43:11 +02001279 drbd_flush(mdev);
1280
1281 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1282 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1283 if (epoch)
1284 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001285 }
1286
Philipp Reisner2451fc32010-08-24 13:43:11 +02001287 epoch = mdev->current_epoch;
1288 wait_event(mdev->ee_wait, atomic_read(&epoch->epoch_size) == 0);
1289
1290 D_ASSERT(atomic_read(&epoch->active) == 0);
1291 D_ASSERT(epoch->flags == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001292
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001293 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001294 default:
1295 dev_err(DEV, "Strangeness in mdev->write_ordering %d\n", mdev->write_ordering);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001296 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001297 }
1298
1299 epoch->flags = 0;
1300 atomic_set(&epoch->epoch_size, 0);
1301 atomic_set(&epoch->active, 0);
1302
1303 spin_lock(&mdev->epoch_lock);
1304 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1305 list_add(&epoch->list, &mdev->current_epoch->list);
1306 mdev->current_epoch = epoch;
1307 mdev->epochs++;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001308 } else {
1309 /* The current_epoch got recycled while we allocated this one... */
1310 kfree(epoch);
1311 }
1312 spin_unlock(&mdev->epoch_lock);
1313
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001314 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001315}
1316
1317/* used from receive_RSDataReply (recv_resync_read)
1318 * and from receive_Data */
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001319static struct drbd_peer_request *
1320read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector,
1321 int data_size) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001322{
Lars Ellenberg66660322010-04-06 12:15:04 +02001323 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001324 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001325 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001326 int dgs, ds, err;
Philipp Reisnera0638452011-01-19 14:31:32 +01001327 void *dig_in = mdev->tconn->int_dig_in;
1328 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001329 unsigned long *data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001330
Philipp Reisnera0638452011-01-19 14:31:32 +01001331 dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_r_tfm) ?
1332 crypto_hash_digestsize(mdev->tconn->integrity_r_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001333
1334 if (dgs) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001335 /*
1336 * FIXME: Receive the incoming digest into the receive buffer
1337 * here, together with its struct p_data?
1338 */
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001339 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1340 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001341 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001342 }
1343
1344 data_size -= dgs;
1345
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001346 if (!expect(data_size != 0))
1347 return NULL;
1348 if (!expect(IS_ALIGNED(data_size, 512)))
1349 return NULL;
1350 if (!expect(data_size <= DRBD_MAX_BIO_SIZE))
1351 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001352
Lars Ellenberg66660322010-04-06 12:15:04 +02001353 /* even though we trust out peer,
1354 * we sometimes have to double check. */
1355 if (sector + (data_size>>9) > capacity) {
Lars Ellenbergfdda6542011-01-24 15:11:01 +01001356 dev_err(DEV, "request from peer beyond end of local disk: "
1357 "capacity: %llus < sector: %llus + size: %u\n",
Lars Ellenberg66660322010-04-06 12:15:04 +02001358 (unsigned long long)capacity,
1359 (unsigned long long)sector, data_size);
1360 return NULL;
1361 }
1362
Philipp Reisnerb411b362009-09-25 16:07:19 -07001363 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1364 * "criss-cross" setup, that might cause write-out on some other DRBD,
1365 * which in turn might block on the other node at this very place. */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001366 peer_req = drbd_alloc_ee(mdev, id, sector, data_size, GFP_NOIO);
1367 if (!peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001368 return NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001369
Philipp Reisnerb411b362009-09-25 16:07:19 -07001370 ds = data_size;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001371 page = peer_req->pages;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001372 page_chain_for_each(page) {
1373 unsigned len = min_t(int, ds, PAGE_SIZE);
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001374 data = kmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001375 err = drbd_recv_all_warn(mdev->tconn, data, len);
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +01001376 if (drbd_insert_fault(mdev, DRBD_FAULT_RECEIVE)) {
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001377 dev_err(DEV, "Fault injection: Corrupting data on receive\n");
1378 data[0] = data[0] ^ (unsigned long)-1;
1379 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001380 kunmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001381 if (err) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001382 drbd_free_ee(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001383 return NULL;
1384 }
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001385 ds -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001386 }
1387
1388 if (dgs) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001389 drbd_csum_ee(mdev, mdev->tconn->integrity_r_tfm, peer_req, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001390 if (memcmp(dig_in, dig_vv, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001391 dev_err(DEV, "Digest integrity check FAILED: %llus +%u\n",
1392 (unsigned long long)sector, data_size);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001393 drbd_free_ee(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001394 return NULL;
1395 }
1396 }
1397 mdev->recv_cnt += data_size>>9;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001398 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001399}
1400
1401/* drbd_drain_block() just takes a data block
1402 * out of the socket input buffer, and discards it.
1403 */
1404static int drbd_drain_block(struct drbd_conf *mdev, int data_size)
1405{
1406 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001407 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001408 void *data;
1409
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001410 if (!data_size)
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001411 return 0;
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001412
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001413 page = drbd_pp_alloc(mdev, 1, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001414
1415 data = kmap(page);
1416 while (data_size) {
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001417 unsigned int len = min_t(int, data_size, PAGE_SIZE);
1418
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001419 err = drbd_recv_all_warn(mdev->tconn, data, len);
1420 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001421 break;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001422 data_size -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001423 }
1424 kunmap(page);
Lars Ellenberg435f0742010-09-06 12:30:25 +02001425 drbd_pp_free(mdev, page, 0);
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001426 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001427}
1428
1429static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req,
1430 sector_t sector, int data_size)
1431{
1432 struct bio_vec *bvec;
1433 struct bio *bio;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001434 int dgs, err, i, expect;
Philipp Reisnera0638452011-01-19 14:31:32 +01001435 void *dig_in = mdev->tconn->int_dig_in;
1436 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001437
Philipp Reisnera0638452011-01-19 14:31:32 +01001438 dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_r_tfm) ?
1439 crypto_hash_digestsize(mdev->tconn->integrity_r_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001440
1441 if (dgs) {
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001442 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1443 if (err)
1444 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001445 }
1446
1447 data_size -= dgs;
1448
1449 /* optimistically update recv_cnt. if receiving fails below,
1450 * we disconnect anyways, and counters will be reset. */
1451 mdev->recv_cnt += data_size>>9;
1452
1453 bio = req->master_bio;
1454 D_ASSERT(sector == bio->bi_sector);
1455
1456 bio_for_each_segment(bvec, bio, i) {
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001457 void *mapped = kmap(bvec->bv_page) + bvec->bv_offset;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001458 expect = min_t(int, data_size, bvec->bv_len);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001459 err = drbd_recv_all_warn(mdev->tconn, mapped, expect);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001460 kunmap(bvec->bv_page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001461 if (err)
1462 return err;
1463 data_size -= expect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001464 }
1465
1466 if (dgs) {
Philipp Reisnera0638452011-01-19 14:31:32 +01001467 drbd_csum_bio(mdev, mdev->tconn->integrity_r_tfm, bio, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001468 if (memcmp(dig_in, dig_vv, dgs)) {
1469 dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n");
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001470 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001471 }
1472 }
1473
1474 D_ASSERT(data_size == 0);
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001475 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001476}
1477
1478/* e_end_resync_block() is called via
1479 * drbd_process_done_ee() by asender only */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001480static int e_end_resync_block(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001481{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001482 struct drbd_peer_request *peer_req =
1483 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001484 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001485 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001486 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001487
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001488 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001489
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001490 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1491 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001492 err = drbd_send_ack(mdev, P_RS_WRITE_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001493 } else {
1494 /* Record failure to sync */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001495 drbd_rs_failed_io(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001496
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001497 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001498 }
1499 dec_unacked(mdev);
1500
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001501 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001502}
1503
1504static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local)
1505{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001506 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001507
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001508 peer_req = read_in_block(mdev, ID_SYNCER, sector, data_size);
1509 if (!peer_req)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001510 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001511
1512 dec_rs_pending(mdev);
1513
Philipp Reisnerb411b362009-09-25 16:07:19 -07001514 inc_unacked(mdev);
1515 /* corresponding dec_unacked() in e_end_resync_block()
1516 * respective _drbd_clear_done_ee */
1517
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001518 peer_req->w.cb = e_end_resync_block;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001519
Philipp Reisner87eeee42011-01-19 14:16:30 +01001520 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001521 list_add(&peer_req->w.list, &mdev->sync_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001522 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001523
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001524 atomic_add(data_size >> 9, &mdev->rs_sect_ev);
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001525 if (drbd_submit_peer_request(mdev, peer_req, WRITE, DRBD_FAULT_RS_WR) == 0)
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001526 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001527
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001528 /* don't care for the reason here */
1529 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01001530 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001531 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001532 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001533
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001534 drbd_free_ee(mdev, peer_req);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001535fail:
1536 put_ldev(mdev);
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001537 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001538}
1539
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001540static struct drbd_request *
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001541find_request(struct drbd_conf *mdev, struct rb_root *root, u64 id,
1542 sector_t sector, bool missing_ok, const char *func)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001543{
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001544 struct drbd_request *req;
1545
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001546 /* Request object according to our peer */
1547 req = (struct drbd_request *)(unsigned long)id;
Andreas Gruenbacher5e472262011-01-27 14:42:51 +01001548 if (drbd_contains_interval(root, sector, &req->i) && req->i.local)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001549 return req;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001550 if (!missing_ok) {
1551 dev_err(DEV, "%s: failed to find request %lu, sector %llus\n", func,
1552 (unsigned long)id, (unsigned long long)sector);
1553 }
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001554 return NULL;
1555}
1556
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001557static int receive_DataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001558{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001559 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001560 struct drbd_request *req;
1561 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001562 int err;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001563 struct p_data *p = tconn->data.rbuf;
1564
1565 mdev = vnr_to_mdev(tconn, pi->vnr);
1566 if (!mdev)
1567 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001568
1569 sector = be64_to_cpu(p->sector);
1570
Philipp Reisner87eeee42011-01-19 14:16:30 +01001571 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001572 req = find_request(mdev, &mdev->read_requests, p->block_id, sector, false, __func__);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001573 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001574 if (unlikely(!req))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001575 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001576
Bart Van Assche24c48302011-05-21 18:32:29 +02001577 /* hlist_del(&req->collision) is done in _req_may_be_done, to avoid
Philipp Reisnerb411b362009-09-25 16:07:19 -07001578 * special casing it there for the various failure cases.
1579 * still no race with drbd_fail_pending_reads */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001580 err = recv_dless_read(mdev, req, sector, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001581 if (!err)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001582 req_mod(req, DATA_RECEIVED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001583 /* else: nothing. handled from drbd_disconnect...
1584 * I don't think we may complete this just yet
1585 * in case we are "on-disconnect: freeze" */
1586
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001587 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001588}
1589
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001590static int receive_RSDataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001591{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001592 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001593 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001594 int err;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001595 struct p_data *p = tconn->data.rbuf;
1596
1597 mdev = vnr_to_mdev(tconn, pi->vnr);
1598 if (!mdev)
1599 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001600
1601 sector = be64_to_cpu(p->sector);
1602 D_ASSERT(p->block_id == ID_SYNCER);
1603
1604 if (get_ldev(mdev)) {
1605 /* data is submitted to disk within recv_resync_read.
1606 * corresponding put_ldev done below on error,
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001607 * or in drbd_peer_request_endio. */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001608 err = recv_resync_read(mdev, sector, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001609 } else {
1610 if (__ratelimit(&drbd_ratelimit_state))
1611 dev_err(DEV, "Can not write resync data to local disk.\n");
1612
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001613 err = drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001614
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001615 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001616 }
1617
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001618 atomic_add(pi->size >> 9, &mdev->rs_sect_in);
Philipp Reisner778f2712010-07-06 11:14:00 +02001619
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001620 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001621}
1622
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001623static int w_restart_write(struct drbd_work *w, int cancel)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001624{
1625 struct drbd_request *req = container_of(w, struct drbd_request, w);
1626 struct drbd_conf *mdev = w->mdev;
1627 struct bio *bio;
1628 unsigned long start_time;
1629 unsigned long flags;
1630
1631 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
1632 if (!expect(req->rq_state & RQ_POSTPONED)) {
1633 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001634 return -EIO;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001635 }
1636 bio = req->master_bio;
1637 start_time = req->start_time;
1638 /* Postponed requests will not have their master_bio completed! */
1639 __req_mod(req, DISCARD_WRITE, NULL);
1640 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
1641
1642 while (__drbd_make_request(mdev, bio, start_time))
1643 /* retry */ ;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001644 return 0;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001645}
1646
1647static void restart_conflicting_writes(struct drbd_conf *mdev,
1648 sector_t sector, int size)
1649{
1650 struct drbd_interval *i;
1651 struct drbd_request *req;
1652
1653 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1654 if (!i->local)
1655 continue;
1656 req = container_of(i, struct drbd_request, i);
1657 if (req->rq_state & RQ_LOCAL_PENDING ||
1658 !(req->rq_state & RQ_POSTPONED))
1659 continue;
1660 if (expect(list_empty(&req->w.list))) {
1661 req->w.mdev = mdev;
1662 req->w.cb = w_restart_write;
1663 drbd_queue_work(&mdev->tconn->data.work, &req->w);
1664 }
1665 }
1666}
1667
Philipp Reisnerb411b362009-09-25 16:07:19 -07001668/* e_end_block() is called via drbd_process_done_ee().
1669 * this means this function only runs in the asender thread
1670 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001671static int e_end_block(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001672{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001673 struct drbd_peer_request *peer_req =
1674 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001675 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001676 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001677 int err = 0, pcmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001678
Philipp Reisner89e58e72011-01-19 13:12:45 +01001679 if (mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001680 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001681 pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
1682 mdev->state.conn <= C_PAUSED_SYNC_T &&
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001683 peer_req->flags & EE_MAY_SET_IN_SYNC) ?
Philipp Reisnerb411b362009-09-25 16:07:19 -07001684 P_RS_WRITE_ACK : P_WRITE_ACK;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001685 err = drbd_send_ack(mdev, pcmd, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001686 if (pcmd == P_RS_WRITE_ACK)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001687 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001688 } else {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001689 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001690 /* we expect it to be marked out of sync anyways...
1691 * maybe assert this? */
1692 }
1693 dec_unacked(mdev);
1694 }
1695 /* we delete from the conflict detection hash _after_ we sent out the
1696 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
Philipp Reisner89e58e72011-01-19 13:12:45 +01001697 if (mdev->tconn->net_conf->two_primaries) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01001698 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001699 D_ASSERT(!drbd_interval_empty(&peer_req->i));
1700 drbd_remove_epoch_entry_interval(mdev, peer_req);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001701 if (peer_req->flags & EE_RESTART_REQUESTS)
1702 restart_conflicting_writes(mdev, sector, peer_req->i.size);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001703 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +01001704 } else
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001705 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001706
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001707 drbd_may_finish_epoch(mdev, peer_req->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001708
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001709 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001710}
1711
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001712static int e_send_ack(struct drbd_work *w, enum drbd_packet ack)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001713{
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001714 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001715 struct drbd_peer_request *peer_req =
1716 container_of(w, struct drbd_peer_request, w);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001717 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001718
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001719 err = drbd_send_ack(mdev, ack, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001720 dec_unacked(mdev);
1721
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001722 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001723}
1724
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001725static int e_send_discard_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001726{
1727 return e_send_ack(w, P_DISCARD_WRITE);
1728}
1729
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001730static int e_send_retry_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001731{
1732 struct drbd_tconn *tconn = w->mdev->tconn;
1733
1734 return e_send_ack(w, tconn->agreed_pro_version >= 100 ?
1735 P_RETRY_WRITE : P_DISCARD_WRITE);
1736}
1737
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001738static bool seq_greater(u32 a, u32 b)
1739{
1740 /*
1741 * We assume 32-bit wrap-around here.
1742 * For 24-bit wrap-around, we would have to shift:
1743 * a <<= 8; b <<= 8;
1744 */
1745 return (s32)a - (s32)b > 0;
1746}
1747
1748static u32 seq_max(u32 a, u32 b)
1749{
1750 return seq_greater(a, b) ? a : b;
1751}
1752
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001753static bool need_peer_seq(struct drbd_conf *mdev)
1754{
1755 struct drbd_tconn *tconn = mdev->tconn;
1756
1757 /*
1758 * We only need to keep track of the last packet_seq number of our peer
1759 * if we are in dual-primary mode and we have the discard flag set; see
1760 * handle_write_conflicts().
1761 */
1762 return tconn->net_conf->two_primaries &&
1763 test_bit(DISCARD_CONCURRENT, &tconn->flags);
1764}
1765
Andreas Gruenbacher43ae0772011-02-03 18:42:08 +01001766static void update_peer_seq(struct drbd_conf *mdev, unsigned int peer_seq)
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001767{
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001768 unsigned int newest_peer_seq;
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001769
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001770 if (need_peer_seq(mdev)) {
1771 spin_lock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001772 newest_peer_seq = seq_max(mdev->peer_seq, peer_seq);
1773 mdev->peer_seq = newest_peer_seq;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001774 spin_unlock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001775 /* wake up only if we actually changed mdev->peer_seq */
1776 if (peer_seq == newest_peer_seq)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001777 wake_up(&mdev->seq_wait);
1778 }
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001779}
1780
Philipp Reisnerb411b362009-09-25 16:07:19 -07001781/* Called from receive_Data.
1782 * Synchronize packets on sock with packets on msock.
1783 *
1784 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
1785 * packet traveling on msock, they are still processed in the order they have
1786 * been sent.
1787 *
1788 * Note: we don't care for Ack packets overtaking P_DATA packets.
1789 *
1790 * In case packet_seq is larger than mdev->peer_seq number, there are
1791 * outstanding packets on the msock. We wait for them to arrive.
1792 * In case we are the logically next packet, we update mdev->peer_seq
1793 * ourselves. Correctly handles 32bit wrap around.
1794 *
1795 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
1796 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
1797 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
1798 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
1799 *
1800 * returns 0 if we may process the packet,
1801 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001802static int wait_for_and_update_peer_seq(struct drbd_conf *mdev, const u32 peer_seq)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001803{
1804 DEFINE_WAIT(wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001805 long timeout;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001806 int ret;
1807
1808 if (!need_peer_seq(mdev))
1809 return 0;
1810
Philipp Reisnerb411b362009-09-25 16:07:19 -07001811 spin_lock(&mdev->peer_seq_lock);
1812 for (;;) {
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001813 if (!seq_greater(peer_seq - 1, mdev->peer_seq)) {
1814 mdev->peer_seq = seq_max(mdev->peer_seq, peer_seq);
1815 ret = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001816 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001817 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001818 if (signal_pending(current)) {
1819 ret = -ERESTARTSYS;
1820 break;
1821 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001822 prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001823 spin_unlock(&mdev->peer_seq_lock);
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001824 timeout = mdev->tconn->net_conf->ping_timeo*HZ/10;
1825 timeout = schedule_timeout(timeout);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001826 spin_lock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001827 if (!timeout) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001828 ret = -ETIMEDOUT;
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001829 dev_err(DEV, "Timed out waiting for missing ack packets; disconnecting\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001830 break;
1831 }
1832 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001833 spin_unlock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001834 finish_wait(&mdev->seq_wait, &wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001835 return ret;
1836}
1837
Lars Ellenberg688593c2010-11-17 22:25:03 +01001838/* see also bio_flags_to_wire()
1839 * DRBD_REQ_*, because we need to semantically map the flags to data packet
1840 * flags and back. We may replicate to other kernel versions. */
1841static unsigned long wire_flags_to_bio(struct drbd_conf *mdev, u32 dpf)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001842{
Lars Ellenberg688593c2010-11-17 22:25:03 +01001843 return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
1844 (dpf & DP_FUA ? REQ_FUA : 0) |
1845 (dpf & DP_FLUSH ? REQ_FLUSH : 0) |
1846 (dpf & DP_DISCARD ? REQ_DISCARD : 0);
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001847}
1848
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001849static void fail_postponed_requests(struct drbd_conf *mdev, sector_t sector,
1850 unsigned int size)
1851{
1852 struct drbd_interval *i;
1853
1854 repeat:
1855 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1856 struct drbd_request *req;
1857 struct bio_and_error m;
1858
1859 if (!i->local)
1860 continue;
1861 req = container_of(i, struct drbd_request, i);
1862 if (!(req->rq_state & RQ_POSTPONED))
1863 continue;
1864 req->rq_state &= ~RQ_POSTPONED;
1865 __req_mod(req, NEG_ACKED, &m);
1866 spin_unlock_irq(&mdev->tconn->req_lock);
1867 if (m.bio)
1868 complete_master_bio(mdev, &m);
1869 spin_lock_irq(&mdev->tconn->req_lock);
1870 goto repeat;
1871 }
1872}
1873
1874static int handle_write_conflicts(struct drbd_conf *mdev,
1875 struct drbd_peer_request *peer_req)
1876{
1877 struct drbd_tconn *tconn = mdev->tconn;
1878 bool resolve_conflicts = test_bit(DISCARD_CONCURRENT, &tconn->flags);
1879 sector_t sector = peer_req->i.sector;
1880 const unsigned int size = peer_req->i.size;
1881 struct drbd_interval *i;
1882 bool equal;
1883 int err;
1884
1885 /*
1886 * Inserting the peer request into the write_requests tree will prevent
1887 * new conflicting local requests from being added.
1888 */
1889 drbd_insert_interval(&mdev->write_requests, &peer_req->i);
1890
1891 repeat:
1892 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1893 if (i == &peer_req->i)
1894 continue;
1895
1896 if (!i->local) {
1897 /*
1898 * Our peer has sent a conflicting remote request; this
1899 * should not happen in a two-node setup. Wait for the
1900 * earlier peer request to complete.
1901 */
1902 err = drbd_wait_misc(mdev, i);
1903 if (err)
1904 goto out;
1905 goto repeat;
1906 }
1907
1908 equal = i->sector == sector && i->size == size;
1909 if (resolve_conflicts) {
1910 /*
1911 * If the peer request is fully contained within the
1912 * overlapping request, it can be discarded; otherwise,
1913 * it will be retried once all overlapping requests
1914 * have completed.
1915 */
1916 bool discard = i->sector <= sector && i->sector +
1917 (i->size >> 9) >= sector + (size >> 9);
1918
1919 if (!equal)
1920 dev_alert(DEV, "Concurrent writes detected: "
1921 "local=%llus +%u, remote=%llus +%u, "
1922 "assuming %s came first\n",
1923 (unsigned long long)i->sector, i->size,
1924 (unsigned long long)sector, size,
1925 discard ? "local" : "remote");
1926
1927 inc_unacked(mdev);
1928 peer_req->w.cb = discard ? e_send_discard_write :
1929 e_send_retry_write;
1930 list_add_tail(&peer_req->w.list, &mdev->done_ee);
1931 wake_asender(mdev->tconn);
1932
1933 err = -ENOENT;
1934 goto out;
1935 } else {
1936 struct drbd_request *req =
1937 container_of(i, struct drbd_request, i);
1938
1939 if (!equal)
1940 dev_alert(DEV, "Concurrent writes detected: "
1941 "local=%llus +%u, remote=%llus +%u\n",
1942 (unsigned long long)i->sector, i->size,
1943 (unsigned long long)sector, size);
1944
1945 if (req->rq_state & RQ_LOCAL_PENDING ||
1946 !(req->rq_state & RQ_POSTPONED)) {
1947 /*
1948 * Wait for the node with the discard flag to
1949 * decide if this request will be discarded or
1950 * retried. Requests that are discarded will
1951 * disappear from the write_requests tree.
1952 *
1953 * In addition, wait for the conflicting
1954 * request to finish locally before submitting
1955 * the conflicting peer request.
1956 */
1957 err = drbd_wait_misc(mdev, &req->i);
1958 if (err) {
1959 _conn_request_state(mdev->tconn,
1960 NS(conn, C_TIMEOUT),
1961 CS_HARD);
1962 fail_postponed_requests(mdev, sector, size);
1963 goto out;
1964 }
1965 goto repeat;
1966 }
1967 /*
1968 * Remember to restart the conflicting requests after
1969 * the new peer request has completed.
1970 */
1971 peer_req->flags |= EE_RESTART_REQUESTS;
1972 }
1973 }
1974 err = 0;
1975
1976 out:
1977 if (err)
1978 drbd_remove_epoch_entry_interval(mdev, peer_req);
1979 return err;
1980}
1981
Philipp Reisnerb411b362009-09-25 16:07:19 -07001982/* mirrored write */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001983static int receive_Data(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001984{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001985 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001986 sector_t sector;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001987 struct drbd_peer_request *peer_req;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001988 struct p_data *p = tconn->data.rbuf;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001989 u32 peer_seq = be32_to_cpu(p->seq_num);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001990 int rw = WRITE;
1991 u32 dp_flags;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001992 int err;
1993
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001994 mdev = vnr_to_mdev(tconn, pi->vnr);
1995 if (!mdev)
1996 return -EIO;
1997
Philipp Reisnerb411b362009-09-25 16:07:19 -07001998 if (!get_ldev(mdev)) {
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001999 int err2;
2000
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002001 err = wait_for_and_update_peer_seq(mdev, peer_seq);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002002 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002003 atomic_inc(&mdev->current_epoch->epoch_size);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002004 err2 = drbd_drain_block(mdev, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002005 if (!err)
2006 err = err2;
2007 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002008 }
2009
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01002010 /*
2011 * Corresponding put_ldev done either below (on various errors), or in
2012 * drbd_peer_request_endio, if we successfully submit the data at the
2013 * end of this function.
2014 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002015
2016 sector = be64_to_cpu(p->sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002017 peer_req = read_in_block(mdev, p->block_id, sector, pi->size);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002018 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002019 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002020 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002021 }
2022
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002023 peer_req->w.cb = e_end_block;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002024
Lars Ellenberg688593c2010-11-17 22:25:03 +01002025 dp_flags = be32_to_cpu(p->dp_flags);
2026 rw |= wire_flags_to_bio(mdev, dp_flags);
2027
2028 if (dp_flags & DP_MAY_SET_IN_SYNC)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002029 peer_req->flags |= EE_MAY_SET_IN_SYNC;
Lars Ellenberg688593c2010-11-17 22:25:03 +01002030
Philipp Reisnerb411b362009-09-25 16:07:19 -07002031 spin_lock(&mdev->epoch_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002032 peer_req->epoch = mdev->current_epoch;
2033 atomic_inc(&peer_req->epoch->epoch_size);
2034 atomic_inc(&peer_req->epoch->active);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002035 spin_unlock(&mdev->epoch_lock);
2036
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002037 if (mdev->tconn->net_conf->two_primaries) {
2038 err = wait_for_and_update_peer_seq(mdev, peer_seq);
2039 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002040 goto out_interrupted;
Philipp Reisner87eeee42011-01-19 14:16:30 +01002041 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002042 err = handle_write_conflicts(mdev, peer_req);
2043 if (err) {
2044 spin_unlock_irq(&mdev->tconn->req_lock);
2045 if (err == -ENOENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002046 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002047 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002048 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002049 goto out_interrupted;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002050 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002051 } else
2052 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002053 list_add(&peer_req->w.list, &mdev->active_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002054 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002055
Philipp Reisner89e58e72011-01-19 13:12:45 +01002056 switch (mdev->tconn->net_conf->wire_protocol) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002057 case DRBD_PROT_C:
2058 inc_unacked(mdev);
2059 /* corresponding dec_unacked() in e_end_block()
2060 * respective _drbd_clear_done_ee */
2061 break;
2062 case DRBD_PROT_B:
2063 /* I really don't like it that the receiver thread
2064 * sends on the msock, but anyways */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002065 drbd_send_ack(mdev, P_RECV_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002066 break;
2067 case DRBD_PROT_A:
2068 /* nothing to do */
2069 break;
2070 }
2071
Lars Ellenberg6719fb02010-10-18 23:04:07 +02002072 if (mdev->state.pdsk < D_INCONSISTENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002073 /* In case we have the only disk of the cluster, */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002074 drbd_set_out_of_sync(mdev, peer_req->i.sector, peer_req->i.size);
2075 peer_req->flags |= EE_CALL_AL_COMPLETE_IO;
2076 peer_req->flags &= ~EE_MAY_SET_IN_SYNC;
Lars Ellenberg181286a2011-03-31 15:18:56 +02002077 drbd_al_begin_io(mdev, &peer_req->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002078 }
2079
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002080 err = drbd_submit_peer_request(mdev, peer_req, rw, DRBD_FAULT_DT_WR);
2081 if (!err)
2082 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002083
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002084 /* don't care for the reason here */
2085 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002086 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002087 list_del(&peer_req->w.list);
2088 drbd_remove_epoch_entry_interval(mdev, peer_req);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002089 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002090 if (peer_req->flags & EE_CALL_AL_COMPLETE_IO)
Lars Ellenberg181286a2011-03-31 15:18:56 +02002091 drbd_al_complete_io(mdev, &peer_req->i);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002092
Philipp Reisnerb411b362009-09-25 16:07:19 -07002093out_interrupted:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002094 drbd_may_finish_epoch(mdev, peer_req->epoch, EV_PUT + EV_CLEANUP);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002095 put_ldev(mdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002096 drbd_free_ee(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002097 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002098}
2099
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002100/* We may throttle resync, if the lower device seems to be busy,
2101 * and current sync rate is above c_min_rate.
2102 *
2103 * To decide whether or not the lower device is busy, we use a scheme similar
2104 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
2105 * (more than 64 sectors) of activity we cannot account for with our own resync
2106 * activity, it obviously is "busy".
2107 *
2108 * The current sync rate used here uses only the most recent two step marks,
2109 * to have a short time average so we can react faster.
2110 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002111int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002112{
2113 struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
2114 unsigned long db, dt, dbdt;
Philipp Reisnere3555d82010-11-07 15:56:29 +01002115 struct lc_element *tmp;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002116 int curr_events;
2117 int throttle = 0;
2118
2119 /* feature disabled? */
Lars Ellenbergf3990022011-03-23 14:31:09 +01002120 if (mdev->ldev->dc.c_min_rate == 0)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002121 return 0;
2122
Philipp Reisnere3555d82010-11-07 15:56:29 +01002123 spin_lock_irq(&mdev->al_lock);
2124 tmp = lc_find(mdev->resync, BM_SECT_TO_EXT(sector));
2125 if (tmp) {
2126 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
2127 if (test_bit(BME_PRIORITY, &bm_ext->flags)) {
2128 spin_unlock_irq(&mdev->al_lock);
2129 return 0;
2130 }
2131 /* Do not slow down if app IO is already waiting for this extent */
2132 }
2133 spin_unlock_irq(&mdev->al_lock);
2134
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002135 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
2136 (int)part_stat_read(&disk->part0, sectors[1]) -
2137 atomic_read(&mdev->rs_sect_ev);
Philipp Reisnere3555d82010-11-07 15:56:29 +01002138
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002139 if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
2140 unsigned long rs_left;
2141 int i;
2142
2143 mdev->rs_last_events = curr_events;
2144
2145 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
2146 * approx. */
Lars Ellenberg2649f082010-11-05 10:05:47 +01002147 i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
2148
2149 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
2150 rs_left = mdev->ov_left;
2151 else
2152 rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002153
2154 dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
2155 if (!dt)
2156 dt++;
2157 db = mdev->rs_mark_left[i] - rs_left;
2158 dbdt = Bit2KB(db/dt);
2159
Lars Ellenbergf3990022011-03-23 14:31:09 +01002160 if (dbdt > mdev->ldev->dc.c_min_rate)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002161 throttle = 1;
2162 }
2163 return throttle;
2164}
2165
2166
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002167static int receive_DataRequest(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002168{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002169 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002170 sector_t sector;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002171 sector_t capacity;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002172 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002173 struct digest_info *di = NULL;
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002174 int size, verb;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002175 unsigned int fault_type;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002176 struct p_block_req *p = tconn->data.rbuf;
2177
2178 mdev = vnr_to_mdev(tconn, pi->vnr);
2179 if (!mdev)
2180 return -EIO;
2181 capacity = drbd_get_capacity(mdev->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002182
2183 sector = be64_to_cpu(p->sector);
2184 size = be32_to_cpu(p->blksize);
2185
Andreas Gruenbacherc670a392011-02-21 12:41:39 +01002186 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002187 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2188 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002189 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002190 }
2191 if (sector + (size>>9) > capacity) {
2192 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2193 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002194 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002195 }
2196
2197 if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002198 verb = 1;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002199 switch (pi->cmd) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002200 case P_DATA_REQUEST:
2201 drbd_send_ack_rp(mdev, P_NEG_DREPLY, p);
2202 break;
2203 case P_RS_DATA_REQUEST:
2204 case P_CSUM_RS_REQUEST:
2205 case P_OV_REQUEST:
2206 drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p);
2207 break;
2208 case P_OV_REPLY:
2209 verb = 0;
2210 dec_rs_pending(mdev);
2211 drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC);
2212 break;
2213 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002214 BUG();
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002215 }
2216 if (verb && __ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002217 dev_err(DEV, "Can not satisfy peer's read request, "
2218 "no local data.\n");
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002219
Lars Ellenberga821cc42010-09-06 12:31:37 +02002220 /* drain possibly payload */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002221 return drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002222 }
2223
2224 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
2225 * "criss-cross" setup, that might cause write-out on some other DRBD,
2226 * which in turn might block on the other node at this very place. */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002227 peer_req = drbd_alloc_ee(mdev, p->block_id, sector, size, GFP_NOIO);
2228 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002229 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002230 return -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002231 }
2232
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002233 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002234 case P_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002235 peer_req->w.cb = w_e_end_data_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002236 fault_type = DRBD_FAULT_DT_RD;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002237 /* application IO, don't drbd_rs_begin_io */
2238 goto submit;
2239
Philipp Reisnerb411b362009-09-25 16:07:19 -07002240 case P_RS_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002241 peer_req->w.cb = w_e_end_rsdata_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002242 fault_type = DRBD_FAULT_RS_RD;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002243 /* used in the sector offset progress display */
2244 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002245 break;
2246
2247 case P_OV_REPLY:
2248 case P_CSUM_RS_REQUEST:
2249 fault_type = DRBD_FAULT_RS_RD;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002250 di = kmalloc(sizeof(*di) + pi->size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002251 if (!di)
2252 goto out_free_e;
2253
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002254 di->digest_size = pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002255 di->digest = (((char *)di)+sizeof(struct digest_info));
2256
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002257 peer_req->digest = di;
2258 peer_req->flags |= EE_HAS_DIGEST;
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +02002259
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002260 if (drbd_recv_all(mdev->tconn, di->digest, pi->size))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002261 goto out_free_e;
2262
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002263 if (pi->cmd == P_CSUM_RS_REQUEST) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002264 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002265 peer_req->w.cb = w_e_end_csum_rs_req;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002266 /* used in the sector offset progress display */
2267 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002268 } else if (pi->cmd == P_OV_REPLY) {
Lars Ellenberg2649f082010-11-05 10:05:47 +01002269 /* track progress, we may need to throttle */
2270 atomic_add(size >> 9, &mdev->rs_sect_in);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002271 peer_req->w.cb = w_e_end_ov_reply;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002272 dec_rs_pending(mdev);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002273 /* drbd_rs_begin_io done when we sent this request,
2274 * but accounting still needs to be done. */
2275 goto submit_for_resync;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002276 }
2277 break;
2278
2279 case P_OV_REQUEST:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002280 if (mdev->ov_start_sector == ~(sector_t)0 &&
Philipp Reisner31890f42011-01-19 14:12:51 +01002281 mdev->tconn->agreed_pro_version >= 90) {
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002282 unsigned long now = jiffies;
2283 int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002284 mdev->ov_start_sector = sector;
2285 mdev->ov_position = sector;
Lars Ellenberg30b743a2010-11-05 09:39:06 +01002286 mdev->ov_left = drbd_bm_bits(mdev) - BM_SECT_TO_BIT(sector);
2287 mdev->rs_total = mdev->ov_left;
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002288 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
2289 mdev->rs_mark_left[i] = mdev->ov_left;
2290 mdev->rs_mark_time[i] = now;
2291 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002292 dev_info(DEV, "Online Verify start sector: %llu\n",
2293 (unsigned long long)sector);
2294 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002295 peer_req->w.cb = w_e_end_ov_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002296 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002297 break;
2298
Philipp Reisnerb411b362009-09-25 16:07:19 -07002299 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002300 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002301 }
2302
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002303 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2304 * wrt the receiver, but it is not as straightforward as it may seem.
2305 * Various places in the resync start and stop logic assume resync
2306 * requests are processed in order, requeuing this on the worker thread
2307 * introduces a bunch of new code for synchronization between threads.
2308 *
2309 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2310 * "forever", throttling after drbd_rs_begin_io will lock that extent
2311 * for application writes for the same time. For now, just throttle
2312 * here, where the rest of the code expects the receiver to sleep for
2313 * a while, anyways.
2314 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002315
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002316 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2317 * this defers syncer requests for some time, before letting at least
2318 * on request through. The resync controller on the receiving side
2319 * will adapt to the incoming rate accordingly.
2320 *
2321 * We cannot throttle here if remote is Primary/SyncTarget:
2322 * we would also throttle its application reads.
2323 * In that case, throttling is done on the SyncTarget only.
2324 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002325 if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev, sector))
2326 schedule_timeout_uninterruptible(HZ/10);
2327 if (drbd_rs_begin_io(mdev, sector))
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002328 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002329
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002330submit_for_resync:
2331 atomic_add(size >> 9, &mdev->rs_sect_ev);
2332
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002333submit:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002334 inc_unacked(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002335 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002336 list_add_tail(&peer_req->w.list, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002337 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002338
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01002339 if (drbd_submit_peer_request(mdev, peer_req, READ, fault_type) == 0)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002340 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002341
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002342 /* don't care for the reason here */
2343 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002344 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002345 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002346 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002347 /* no drbd_rs_complete_io(), we are dropping the connection anyways */
2348
Philipp Reisnerb411b362009-09-25 16:07:19 -07002349out_free_e:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002350 put_ldev(mdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002351 drbd_free_ee(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002352 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002353}
2354
2355static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
2356{
2357 int self, peer, rv = -100;
2358 unsigned long ch_self, ch_peer;
2359
2360 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2361 peer = mdev->p_uuid[UI_BITMAP] & 1;
2362
2363 ch_peer = mdev->p_uuid[UI_SIZE];
2364 ch_self = mdev->comm_bm_set;
2365
Philipp Reisner89e58e72011-01-19 13:12:45 +01002366 switch (mdev->tconn->net_conf->after_sb_0p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002367 case ASB_CONSENSUS:
2368 case ASB_DISCARD_SECONDARY:
2369 case ASB_CALL_HELPER:
2370 dev_err(DEV, "Configuration error.\n");
2371 break;
2372 case ASB_DISCONNECT:
2373 break;
2374 case ASB_DISCARD_YOUNGER_PRI:
2375 if (self == 0 && peer == 1) {
2376 rv = -1;
2377 break;
2378 }
2379 if (self == 1 && peer == 0) {
2380 rv = 1;
2381 break;
2382 }
2383 /* Else fall through to one of the other strategies... */
2384 case ASB_DISCARD_OLDER_PRI:
2385 if (self == 0 && peer == 1) {
2386 rv = 1;
2387 break;
2388 }
2389 if (self == 1 && peer == 0) {
2390 rv = -1;
2391 break;
2392 }
2393 /* Else fall through to one of the other strategies... */
Lars Ellenbergad19bf62009-10-14 09:36:49 +02002394 dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
Philipp Reisnerb411b362009-09-25 16:07:19 -07002395 "Using discard-least-changes instead\n");
2396 case ASB_DISCARD_ZERO_CHG:
2397 if (ch_peer == 0 && ch_self == 0) {
Philipp Reisner25703f82011-02-07 14:35:25 +01002398 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002399 ? -1 : 1;
2400 break;
2401 } else {
2402 if (ch_peer == 0) { rv = 1; break; }
2403 if (ch_self == 0) { rv = -1; break; }
2404 }
Philipp Reisner89e58e72011-01-19 13:12:45 +01002405 if (mdev->tconn->net_conf->after_sb_0p == ASB_DISCARD_ZERO_CHG)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002406 break;
2407 case ASB_DISCARD_LEAST_CHG:
2408 if (ch_self < ch_peer)
2409 rv = -1;
2410 else if (ch_self > ch_peer)
2411 rv = 1;
2412 else /* ( ch_self == ch_peer ) */
2413 /* Well, then use something else. */
Philipp Reisner25703f82011-02-07 14:35:25 +01002414 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002415 ? -1 : 1;
2416 break;
2417 case ASB_DISCARD_LOCAL:
2418 rv = -1;
2419 break;
2420 case ASB_DISCARD_REMOTE:
2421 rv = 1;
2422 }
2423
2424 return rv;
2425}
2426
2427static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
2428{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002429 int hg, rv = -100;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002430
Philipp Reisner89e58e72011-01-19 13:12:45 +01002431 switch (mdev->tconn->net_conf->after_sb_1p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002432 case ASB_DISCARD_YOUNGER_PRI:
2433 case ASB_DISCARD_OLDER_PRI:
2434 case ASB_DISCARD_LEAST_CHG:
2435 case ASB_DISCARD_LOCAL:
2436 case ASB_DISCARD_REMOTE:
2437 dev_err(DEV, "Configuration error.\n");
2438 break;
2439 case ASB_DISCONNECT:
2440 break;
2441 case ASB_CONSENSUS:
2442 hg = drbd_asb_recover_0p(mdev);
2443 if (hg == -1 && mdev->state.role == R_SECONDARY)
2444 rv = hg;
2445 if (hg == 1 && mdev->state.role == R_PRIMARY)
2446 rv = hg;
2447 break;
2448 case ASB_VIOLENTLY:
2449 rv = drbd_asb_recover_0p(mdev);
2450 break;
2451 case ASB_DISCARD_SECONDARY:
2452 return mdev->state.role == R_PRIMARY ? 1 : -1;
2453 case ASB_CALL_HELPER:
2454 hg = drbd_asb_recover_0p(mdev);
2455 if (hg == -1 && mdev->state.role == R_PRIMARY) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002456 enum drbd_state_rv rv2;
2457
2458 drbd_set_role(mdev, R_SECONDARY, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002459 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2460 * we might be here in C_WF_REPORT_PARAMS which is transient.
2461 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002462 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2463 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002464 drbd_khelper(mdev, "pri-lost-after-sb");
2465 } else {
2466 dev_warn(DEV, "Successfully gave up primary role.\n");
2467 rv = hg;
2468 }
2469 } else
2470 rv = hg;
2471 }
2472
2473 return rv;
2474}
2475
2476static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
2477{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002478 int hg, rv = -100;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002479
Philipp Reisner89e58e72011-01-19 13:12:45 +01002480 switch (mdev->tconn->net_conf->after_sb_2p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002481 case ASB_DISCARD_YOUNGER_PRI:
2482 case ASB_DISCARD_OLDER_PRI:
2483 case ASB_DISCARD_LEAST_CHG:
2484 case ASB_DISCARD_LOCAL:
2485 case ASB_DISCARD_REMOTE:
2486 case ASB_CONSENSUS:
2487 case ASB_DISCARD_SECONDARY:
2488 dev_err(DEV, "Configuration error.\n");
2489 break;
2490 case ASB_VIOLENTLY:
2491 rv = drbd_asb_recover_0p(mdev);
2492 break;
2493 case ASB_DISCONNECT:
2494 break;
2495 case ASB_CALL_HELPER:
2496 hg = drbd_asb_recover_0p(mdev);
2497 if (hg == -1) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002498 enum drbd_state_rv rv2;
2499
Philipp Reisnerb411b362009-09-25 16:07:19 -07002500 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2501 * we might be here in C_WF_REPORT_PARAMS which is transient.
2502 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002503 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2504 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002505 drbd_khelper(mdev, "pri-lost-after-sb");
2506 } else {
2507 dev_warn(DEV, "Successfully gave up primary role.\n");
2508 rv = hg;
2509 }
2510 } else
2511 rv = hg;
2512 }
2513
2514 return rv;
2515}
2516
2517static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
2518 u64 bits, u64 flags)
2519{
2520 if (!uuid) {
2521 dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
2522 return;
2523 }
2524 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
2525 text,
2526 (unsigned long long)uuid[UI_CURRENT],
2527 (unsigned long long)uuid[UI_BITMAP],
2528 (unsigned long long)uuid[UI_HISTORY_START],
2529 (unsigned long long)uuid[UI_HISTORY_END],
2530 (unsigned long long)bits,
2531 (unsigned long long)flags);
2532}
2533
2534/*
2535 100 after split brain try auto recover
2536 2 C_SYNC_SOURCE set BitMap
2537 1 C_SYNC_SOURCE use BitMap
2538 0 no Sync
2539 -1 C_SYNC_TARGET use BitMap
2540 -2 C_SYNC_TARGET set BitMap
2541 -100 after split brain, disconnect
2542-1000 unrelated data
Philipp Reisner4a23f262011-01-11 17:42:17 +01002543-1091 requires proto 91
2544-1096 requires proto 96
Philipp Reisnerb411b362009-09-25 16:07:19 -07002545 */
2546static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
2547{
2548 u64 self, peer;
2549 int i, j;
2550
2551 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2552 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2553
2554 *rule_nr = 10;
2555 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
2556 return 0;
2557
2558 *rule_nr = 20;
2559 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
2560 peer != UUID_JUST_CREATED)
2561 return -2;
2562
2563 *rule_nr = 30;
2564 if (self != UUID_JUST_CREATED &&
2565 (peer == UUID_JUST_CREATED || peer == (u64)0))
2566 return 2;
2567
2568 if (self == peer) {
2569 int rct, dc; /* roles at crash time */
2570
2571 if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
2572
Philipp Reisner31890f42011-01-19 14:12:51 +01002573 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002574 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002575
2576 if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
2577 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
2578 dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
2579 drbd_uuid_set_bm(mdev, 0UL);
2580
2581 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2582 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2583 *rule_nr = 34;
2584 } else {
2585 dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
2586 *rule_nr = 36;
2587 }
2588
2589 return 1;
2590 }
2591
2592 if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
2593
Philipp Reisner31890f42011-01-19 14:12:51 +01002594 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002595 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002596
2597 if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
2598 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
2599 dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
2600
2601 mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
2602 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
2603 mdev->p_uuid[UI_BITMAP] = 0UL;
2604
2605 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2606 *rule_nr = 35;
2607 } else {
2608 dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
2609 *rule_nr = 37;
2610 }
2611
2612 return -1;
2613 }
2614
2615 /* Common power [off|failure] */
2616 rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
2617 (mdev->p_uuid[UI_FLAGS] & 2);
2618 /* lowest bit is set when we were primary,
2619 * next bit (weight 2) is set when peer was primary */
2620 *rule_nr = 40;
2621
2622 switch (rct) {
2623 case 0: /* !self_pri && !peer_pri */ return 0;
2624 case 1: /* self_pri && !peer_pri */ return 1;
2625 case 2: /* !self_pri && peer_pri */ return -1;
2626 case 3: /* self_pri && peer_pri */
Philipp Reisner25703f82011-02-07 14:35:25 +01002627 dc = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002628 return dc ? -1 : 1;
2629 }
2630 }
2631
2632 *rule_nr = 50;
2633 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2634 if (self == peer)
2635 return -1;
2636
2637 *rule_nr = 51;
2638 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2639 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002640 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002641 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) ==
2642 (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) :
2643 peer + UUID_NEW_BM_OFFSET == (mdev->p_uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002644 /* The last P_SYNC_UUID did not get though. Undo the last start of
2645 resync as sync source modifications of the peer's UUIDs. */
2646
Philipp Reisner31890f42011-01-19 14:12:51 +01002647 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002648 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002649
2650 mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
2651 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
Philipp Reisner4a23f262011-01-11 17:42:17 +01002652
2653 dev_info(DEV, "Did not got last syncUUID packet, corrected:\n");
2654 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2655
Philipp Reisnerb411b362009-09-25 16:07:19 -07002656 return -1;
2657 }
2658 }
2659
2660 *rule_nr = 60;
2661 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2662 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2663 peer = mdev->p_uuid[i] & ~((u64)1);
2664 if (self == peer)
2665 return -2;
2666 }
2667
2668 *rule_nr = 70;
2669 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2670 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2671 if (self == peer)
2672 return 1;
2673
2674 *rule_nr = 71;
2675 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2676 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002677 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002678 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) ==
2679 (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) :
2680 self + UUID_NEW_BM_OFFSET == (mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002681 /* The last P_SYNC_UUID did not get though. Undo the last start of
2682 resync as sync source modifications of our UUIDs. */
2683
Philipp Reisner31890f42011-01-19 14:12:51 +01002684 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002685 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002686
2687 _drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
2688 _drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
2689
Philipp Reisner4a23f262011-01-11 17:42:17 +01002690 dev_info(DEV, "Last syncUUID did not get through, corrected:\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002691 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2692 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2693
2694 return 1;
2695 }
2696 }
2697
2698
2699 *rule_nr = 80;
Philipp Reisnerd8c2a362009-11-18 15:52:51 +01002700 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002701 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2702 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2703 if (self == peer)
2704 return 2;
2705 }
2706
2707 *rule_nr = 90;
2708 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2709 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2710 if (self == peer && self != ((u64)0))
2711 return 100;
2712
2713 *rule_nr = 100;
2714 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2715 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2716 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
2717 peer = mdev->p_uuid[j] & ~((u64)1);
2718 if (self == peer)
2719 return -100;
2720 }
2721 }
2722
2723 return -1000;
2724}
2725
2726/* drbd_sync_handshake() returns the new conn state on success, or
2727 CONN_MASK (-1) on failure.
2728 */
2729static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
2730 enum drbd_disk_state peer_disk) __must_hold(local)
2731{
2732 int hg, rule_nr;
2733 enum drbd_conns rv = C_MASK;
2734 enum drbd_disk_state mydisk;
2735
2736 mydisk = mdev->state.disk;
2737 if (mydisk == D_NEGOTIATING)
2738 mydisk = mdev->new_state_tmp.disk;
2739
2740 dev_info(DEV, "drbd_sync_handshake:\n");
2741 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0);
2742 drbd_uuid_dump(mdev, "peer", mdev->p_uuid,
2743 mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2744
2745 hg = drbd_uuid_compare(mdev, &rule_nr);
2746
2747 dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
2748
2749 if (hg == -1000) {
2750 dev_alert(DEV, "Unrelated data, aborting!\n");
2751 return C_MASK;
2752 }
Philipp Reisner4a23f262011-01-11 17:42:17 +01002753 if (hg < -1000) {
2754 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 -07002755 return C_MASK;
2756 }
2757
2758 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
2759 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
2760 int f = (hg == -100) || abs(hg) == 2;
2761 hg = mydisk > D_INCONSISTENT ? 1 : -1;
2762 if (f)
2763 hg = hg*2;
2764 dev_info(DEV, "Becoming sync %s due to disk states.\n",
2765 hg > 0 ? "source" : "target");
2766 }
2767
Adam Gandelman3a11a482010-04-08 16:48:23 -07002768 if (abs(hg) == 100)
2769 drbd_khelper(mdev, "initial-split-brain");
2770
Philipp Reisner89e58e72011-01-19 13:12:45 +01002771 if (hg == 100 || (hg == -100 && mdev->tconn->net_conf->always_asbp)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002772 int pcount = (mdev->state.role == R_PRIMARY)
2773 + (peer_role == R_PRIMARY);
2774 int forced = (hg == -100);
2775
2776 switch (pcount) {
2777 case 0:
2778 hg = drbd_asb_recover_0p(mdev);
2779 break;
2780 case 1:
2781 hg = drbd_asb_recover_1p(mdev);
2782 break;
2783 case 2:
2784 hg = drbd_asb_recover_2p(mdev);
2785 break;
2786 }
2787 if (abs(hg) < 100) {
2788 dev_warn(DEV, "Split-Brain detected, %d primaries, "
2789 "automatically solved. Sync from %s node\n",
2790 pcount, (hg < 0) ? "peer" : "this");
2791 if (forced) {
2792 dev_warn(DEV, "Doing a full sync, since"
2793 " UUIDs where ambiguous.\n");
2794 hg = hg*2;
2795 }
2796 }
2797 }
2798
2799 if (hg == -100) {
Philipp Reisner89e58e72011-01-19 13:12:45 +01002800 if (mdev->tconn->net_conf->want_lose && !(mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002801 hg = -1;
Philipp Reisner89e58e72011-01-19 13:12:45 +01002802 if (!mdev->tconn->net_conf->want_lose && (mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002803 hg = 1;
2804
2805 if (abs(hg) < 100)
2806 dev_warn(DEV, "Split-Brain detected, manually solved. "
2807 "Sync from %s node\n",
2808 (hg < 0) ? "peer" : "this");
2809 }
2810
2811 if (hg == -100) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01002812 /* FIXME this log message is not correct if we end up here
2813 * after an attempted attach on a diskless node.
2814 * We just refuse to attach -- well, we drop the "connection"
2815 * to that disk, in a way... */
Adam Gandelman3a11a482010-04-08 16:48:23 -07002816 dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002817 drbd_khelper(mdev, "split-brain");
2818 return C_MASK;
2819 }
2820
2821 if (hg > 0 && mydisk <= D_INCONSISTENT) {
2822 dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
2823 return C_MASK;
2824 }
2825
2826 if (hg < 0 && /* by intention we do not use mydisk here. */
2827 mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
Philipp Reisner89e58e72011-01-19 13:12:45 +01002828 switch (mdev->tconn->net_conf->rr_conflict) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002829 case ASB_CALL_HELPER:
2830 drbd_khelper(mdev, "pri-lost");
2831 /* fall through */
2832 case ASB_DISCONNECT:
2833 dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
2834 return C_MASK;
2835 case ASB_VIOLENTLY:
2836 dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
2837 "assumption\n");
2838 }
2839 }
2840
Philipp Reisner8169e412011-03-15 18:40:27 +01002841 if (mdev->tconn->net_conf->dry_run || test_bit(CONN_DRY_RUN, &mdev->tconn->flags)) {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002842 if (hg == 0)
2843 dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
2844 else
2845 dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
2846 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
2847 abs(hg) >= 2 ? "full" : "bit-map based");
2848 return C_MASK;
2849 }
2850
Philipp Reisnerb411b362009-09-25 16:07:19 -07002851 if (abs(hg) >= 2) {
2852 dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002853 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake",
2854 BM_LOCKED_SET_ALLOWED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002855 return C_MASK;
2856 }
2857
2858 if (hg > 0) { /* become sync source. */
2859 rv = C_WF_BITMAP_S;
2860 } else if (hg < 0) { /* become sync target */
2861 rv = C_WF_BITMAP_T;
2862 } else {
2863 rv = C_CONNECTED;
2864 if (drbd_bm_total_weight(mdev)) {
2865 dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
2866 drbd_bm_total_weight(mdev));
2867 }
2868 }
2869
2870 return rv;
2871}
2872
2873/* returns 1 if invalid */
2874static int cmp_after_sb(enum drbd_after_sb_p peer, enum drbd_after_sb_p self)
2875{
2876 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
2877 if ((peer == ASB_DISCARD_REMOTE && self == ASB_DISCARD_LOCAL) ||
2878 (self == ASB_DISCARD_REMOTE && peer == ASB_DISCARD_LOCAL))
2879 return 0;
2880
2881 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
2882 if (peer == ASB_DISCARD_REMOTE || peer == ASB_DISCARD_LOCAL ||
2883 self == ASB_DISCARD_REMOTE || self == ASB_DISCARD_LOCAL)
2884 return 1;
2885
2886 /* everything else is valid if they are equal on both sides. */
2887 if (peer == self)
2888 return 0;
2889
2890 /* everything es is invalid. */
2891 return 1;
2892}
2893
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002894static int receive_protocol(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002895{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01002896 struct p_protocol *p = tconn->data.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002897 int p_proto, p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002898 int p_want_lose, p_two_primaries, cf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002899 char p_integrity_alg[SHARED_SECRET_MAX] = "";
2900
Philipp Reisnerb411b362009-09-25 16:07:19 -07002901 p_proto = be32_to_cpu(p->protocol);
2902 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
2903 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
2904 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002905 p_two_primaries = be32_to_cpu(p->two_primaries);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002906 cf = be32_to_cpu(p->conn_flags);
2907 p_want_lose = cf & CF_WANT_LOSE;
2908
Philipp Reisner7204624c2011-03-15 18:51:47 +01002909 clear_bit(CONN_DRY_RUN, &tconn->flags);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002910
2911 if (cf & CF_DRY_RUN)
Philipp Reisner7204624c2011-03-15 18:51:47 +01002912 set_bit(CONN_DRY_RUN, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002913
Philipp Reisner7204624c2011-03-15 18:51:47 +01002914 if (p_proto != tconn->net_conf->wire_protocol) {
2915 conn_err(tconn, "incompatible communication protocols\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002916 goto disconnect;
2917 }
2918
Philipp Reisner7204624c2011-03-15 18:51:47 +01002919 if (cmp_after_sb(p_after_sb_0p, tconn->net_conf->after_sb_0p)) {
2920 conn_err(tconn, "incompatible after-sb-0pri settings\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002921 goto disconnect;
2922 }
2923
Philipp Reisner7204624c2011-03-15 18:51:47 +01002924 if (cmp_after_sb(p_after_sb_1p, tconn->net_conf->after_sb_1p)) {
2925 conn_err(tconn, "incompatible after-sb-1pri settings\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002926 goto disconnect;
2927 }
2928
Philipp Reisner7204624c2011-03-15 18:51:47 +01002929 if (cmp_after_sb(p_after_sb_2p, tconn->net_conf->after_sb_2p)) {
2930 conn_err(tconn, "incompatible after-sb-2pri settings\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002931 goto disconnect;
2932 }
2933
Philipp Reisner7204624c2011-03-15 18:51:47 +01002934 if (p_want_lose && tconn->net_conf->want_lose) {
2935 conn_err(tconn, "both sides have the 'want_lose' flag set\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002936 goto disconnect;
2937 }
2938
Philipp Reisner7204624c2011-03-15 18:51:47 +01002939 if (p_two_primaries != tconn->net_conf->two_primaries) {
2940 conn_err(tconn, "incompatible setting of the two-primaries options\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002941 goto disconnect;
2942 }
2943
Philipp Reisner7204624c2011-03-15 18:51:47 +01002944 if (tconn->agreed_pro_version >= 87) {
2945 unsigned char *my_alg = tconn->net_conf->integrity_alg;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002946 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002947
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002948 err = drbd_recv_all(tconn, p_integrity_alg, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002949 if (err)
2950 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002951
2952 p_integrity_alg[SHARED_SECRET_MAX-1] = 0;
2953 if (strcmp(p_integrity_alg, my_alg)) {
Philipp Reisner7204624c2011-03-15 18:51:47 +01002954 conn_err(tconn, "incompatible setting of the data-integrity-alg\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002955 goto disconnect;
2956 }
Philipp Reisner7204624c2011-03-15 18:51:47 +01002957 conn_info(tconn, "data-integrity-alg: %s\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -07002958 my_alg[0] ? my_alg : (unsigned char *)"<not-used>");
2959 }
2960
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002961 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002962
2963disconnect:
Philipp Reisner7204624c2011-03-15 18:51:47 +01002964 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002965 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002966}
2967
2968/* helper function
2969 * input: alg name, feature name
2970 * return: NULL (alg name was "")
2971 * ERR_PTR(error) if something goes wrong
2972 * or the crypto hash ptr, if it worked out ok. */
2973struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
2974 const char *alg, const char *name)
2975{
2976 struct crypto_hash *tfm;
2977
2978 if (!alg[0])
2979 return NULL;
2980
2981 tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
2982 if (IS_ERR(tfm)) {
2983 dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
2984 alg, name, PTR_ERR(tfm));
2985 return tfm;
2986 }
2987 if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) {
2988 crypto_free_hash(tfm);
2989 dev_err(DEV, "\"%s\" is not a digest (%s)\n", alg, name);
2990 return ERR_PTR(-EINVAL);
2991 }
2992 return tfm;
2993}
2994
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002995static int ignore_remaining_packet(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002996{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002997 void *buffer = tconn->data.rbuf;
2998 int size = pi->size;
2999
3000 while (size) {
3001 int s = min_t(int, size, DRBD_SOCKET_BUFFER_SIZE);
3002 s = drbd_recv(tconn, buffer, s);
3003 if (s <= 0) {
3004 if (s < 0)
3005 return s;
3006 break;
3007 }
3008 size -= s;
3009 }
3010 if (size)
3011 return -EIO;
3012 return 0;
3013}
3014
3015/*
3016 * config_unknown_volume - device configuration command for unknown volume
3017 *
3018 * When a device is added to an existing connection, the node on which the
3019 * device is added first will send configuration commands to its peer but the
3020 * peer will not know about the device yet. It will warn and ignore these
3021 * commands. Once the device is added on the second node, the second node will
3022 * send the same device configuration commands, but in the other direction.
3023 *
3024 * (We can also end up here if drbd is misconfigured.)
3025 */
3026static int config_unknown_volume(struct drbd_tconn *tconn, struct packet_info *pi)
3027{
3028 conn_warn(tconn, "Volume %u unknown; ignoring %s packet\n",
3029 pi->vnr, cmdname(pi->cmd));
3030 return ignore_remaining_packet(tconn, pi);
3031}
3032
3033static int receive_SyncParam(struct drbd_tconn *tconn, struct packet_info *pi)
3034{
3035 struct drbd_conf *mdev;
3036 struct p_rs_param_95 *p = tconn->data.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003037 unsigned int header_size, data_size, exp_max_sz;
3038 struct crypto_hash *verify_tfm = NULL;
3039 struct crypto_hash *csums_tfm = NULL;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003040 const int apv = tconn->agreed_pro_version;
Philipp Reisner778f2712010-07-06 11:14:00 +02003041 int *rs_plan_s = NULL;
3042 int fifo_size = 0;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003043 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003044
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003045 mdev = vnr_to_mdev(tconn, pi->vnr);
3046 if (!mdev)
3047 return config_unknown_volume(tconn, pi);
3048
Philipp Reisnerb411b362009-09-25 16:07:19 -07003049 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
3050 : apv == 88 ? sizeof(struct p_rs_param)
3051 + SHARED_SECRET_MAX
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003052 : apv <= 94 ? sizeof(struct p_rs_param_89)
3053 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003054
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003055 if (pi->size > exp_max_sz) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003056 dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003057 pi->size, exp_max_sz);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003058 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003059 }
3060
3061 if (apv <= 88) {
Philipp Reisner257d0af2011-01-26 12:15:29 +01003062 header_size = sizeof(struct p_rs_param) - sizeof(struct p_header);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003063 data_size = pi->size - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003064 } else if (apv <= 94) {
Philipp Reisner257d0af2011-01-26 12:15:29 +01003065 header_size = sizeof(struct p_rs_param_89) - sizeof(struct p_header);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003066 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003067 D_ASSERT(data_size == 0);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003068 } else {
Philipp Reisner257d0af2011-01-26 12:15:29 +01003069 header_size = sizeof(struct p_rs_param_95) - sizeof(struct p_header);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003070 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003071 D_ASSERT(data_size == 0);
3072 }
3073
3074 /* initialize verify_alg and csums_alg */
3075 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
3076
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003077 err = drbd_recv_all(mdev->tconn, &p->head.payload, header_size);
3078 if (err)
3079 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003080
Lars Ellenbergf3990022011-03-23 14:31:09 +01003081 if (get_ldev(mdev)) {
3082 mdev->ldev->dc.resync_rate = be32_to_cpu(p->rate);
3083 put_ldev(mdev);
3084 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003085
3086 if (apv >= 88) {
3087 if (apv == 88) {
3088 if (data_size > SHARED_SECRET_MAX) {
3089 dev_err(DEV, "verify-alg too long, "
3090 "peer wants %u, accepting only %u byte\n",
3091 data_size, SHARED_SECRET_MAX);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003092 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003093 }
3094
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003095 err = drbd_recv_all(mdev->tconn, p->verify_alg, data_size);
3096 if (err)
3097 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003098
3099 /* we expect NUL terminated string */
3100 /* but just in case someone tries to be evil */
3101 D_ASSERT(p->verify_alg[data_size-1] == 0);
3102 p->verify_alg[data_size-1] = 0;
3103
3104 } else /* apv >= 89 */ {
3105 /* we still expect NUL terminated strings */
3106 /* but just in case someone tries to be evil */
3107 D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
3108 D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
3109 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
3110 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
3111 }
3112
Lars Ellenbergf3990022011-03-23 14:31:09 +01003113 if (strcmp(mdev->tconn->net_conf->verify_alg, p->verify_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003114 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3115 dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
Lars Ellenbergf3990022011-03-23 14:31:09 +01003116 mdev->tconn->net_conf->verify_alg, p->verify_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003117 goto disconnect;
3118 }
3119 verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
3120 p->verify_alg, "verify-alg");
3121 if (IS_ERR(verify_tfm)) {
3122 verify_tfm = NULL;
3123 goto disconnect;
3124 }
3125 }
3126
Lars Ellenbergf3990022011-03-23 14:31:09 +01003127 if (apv >= 89 && strcmp(mdev->tconn->net_conf->csums_alg, p->csums_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003128 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3129 dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
Lars Ellenbergf3990022011-03-23 14:31:09 +01003130 mdev->tconn->net_conf->csums_alg, p->csums_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003131 goto disconnect;
3132 }
3133 csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
3134 p->csums_alg, "csums-alg");
3135 if (IS_ERR(csums_tfm)) {
3136 csums_tfm = NULL;
3137 goto disconnect;
3138 }
3139 }
3140
Lars Ellenbergf3990022011-03-23 14:31:09 +01003141 if (apv > 94 && get_ldev(mdev)) {
3142 mdev->ldev->dc.resync_rate = be32_to_cpu(p->rate);
3143 mdev->ldev->dc.c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
3144 mdev->ldev->dc.c_delay_target = be32_to_cpu(p->c_delay_target);
3145 mdev->ldev->dc.c_fill_target = be32_to_cpu(p->c_fill_target);
3146 mdev->ldev->dc.c_max_rate = be32_to_cpu(p->c_max_rate);
Philipp Reisner778f2712010-07-06 11:14:00 +02003147
Lars Ellenbergf3990022011-03-23 14:31:09 +01003148 fifo_size = (mdev->ldev->dc.c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Philipp Reisner778f2712010-07-06 11:14:00 +02003149 if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) {
3150 rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL);
3151 if (!rs_plan_s) {
3152 dev_err(DEV, "kmalloc of fifo_buffer failed");
Lars Ellenbergf3990022011-03-23 14:31:09 +01003153 put_ldev(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02003154 goto disconnect;
3155 }
3156 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01003157 put_ldev(mdev);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003158 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003159
3160 spin_lock(&mdev->peer_seq_lock);
3161 /* lock against drbd_nl_syncer_conf() */
3162 if (verify_tfm) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01003163 strcpy(mdev->tconn->net_conf->verify_alg, p->verify_alg);
3164 mdev->tconn->net_conf->verify_alg_len = strlen(p->verify_alg) + 1;
3165 crypto_free_hash(mdev->tconn->verify_tfm);
3166 mdev->tconn->verify_tfm = verify_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003167 dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
3168 }
3169 if (csums_tfm) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01003170 strcpy(mdev->tconn->net_conf->csums_alg, p->csums_alg);
3171 mdev->tconn->net_conf->csums_alg_len = strlen(p->csums_alg) + 1;
3172 crypto_free_hash(mdev->tconn->csums_tfm);
3173 mdev->tconn->csums_tfm = csums_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003174 dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
3175 }
Philipp Reisner778f2712010-07-06 11:14:00 +02003176 if (fifo_size != mdev->rs_plan_s.size) {
3177 kfree(mdev->rs_plan_s.values);
3178 mdev->rs_plan_s.values = rs_plan_s;
3179 mdev->rs_plan_s.size = fifo_size;
3180 mdev->rs_planed = 0;
3181 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003182 spin_unlock(&mdev->peer_seq_lock);
3183 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003184 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003185
Philipp Reisnerb411b362009-09-25 16:07:19 -07003186disconnect:
3187 /* just for completeness: actually not needed,
3188 * as this is not reached if csums_tfm was ok. */
3189 crypto_free_hash(csums_tfm);
3190 /* but free the verify_tfm again, if csums_tfm did not work out */
3191 crypto_free_hash(verify_tfm);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003192 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003193 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003194}
3195
Philipp Reisnerb411b362009-09-25 16:07:19 -07003196/* warn if the arguments differ by more than 12.5% */
3197static void warn_if_differ_considerably(struct drbd_conf *mdev,
3198 const char *s, sector_t a, sector_t b)
3199{
3200 sector_t d;
3201 if (a == 0 || b == 0)
3202 return;
3203 d = (a > b) ? (a - b) : (b - a);
3204 if (d > (a>>3) || d > (b>>3))
3205 dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
3206 (unsigned long long)a, (unsigned long long)b);
3207}
3208
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003209static int receive_sizes(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003210{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003211 struct drbd_conf *mdev;
3212 struct p_sizes *p = tconn->data.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003213 enum determine_dev_size dd = unchanged;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003214 sector_t p_size, p_usize, my_usize;
3215 int ldsc = 0; /* local disk size changed */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003216 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003217
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003218 mdev = vnr_to_mdev(tconn, pi->vnr);
3219 if (!mdev)
3220 return config_unknown_volume(tconn, pi);
3221
Philipp Reisnerb411b362009-09-25 16:07:19 -07003222 p_size = be64_to_cpu(p->d_size);
3223 p_usize = be64_to_cpu(p->u_size);
3224
Philipp Reisnerb411b362009-09-25 16:07:19 -07003225 /* just store the peer's disk size for now.
3226 * we still need to figure out whether we accept that. */
3227 mdev->p_size = p_size;
3228
Philipp Reisnerb411b362009-09-25 16:07:19 -07003229 if (get_ldev(mdev)) {
3230 warn_if_differ_considerably(mdev, "lower level device sizes",
3231 p_size, drbd_get_max_capacity(mdev->ldev));
3232 warn_if_differ_considerably(mdev, "user requested size",
3233 p_usize, mdev->ldev->dc.disk_size);
3234
3235 /* if this is the first connect, or an otherwise expected
3236 * param exchange, choose the minimum */
3237 if (mdev->state.conn == C_WF_REPORT_PARAMS)
3238 p_usize = min_not_zero((sector_t)mdev->ldev->dc.disk_size,
3239 p_usize);
3240
3241 my_usize = mdev->ldev->dc.disk_size;
3242
3243 if (mdev->ldev->dc.disk_size != p_usize) {
3244 mdev->ldev->dc.disk_size = p_usize;
3245 dev_info(DEV, "Peer sets u_size to %lu sectors\n",
3246 (unsigned long)mdev->ldev->dc.disk_size);
3247 }
3248
3249 /* Never shrink a device with usable data during connect.
3250 But allow online shrinking if we are connected. */
Philipp Reisnera393db62009-12-22 13:35:52 +01003251 if (drbd_new_dev_size(mdev, mdev->ldev, 0) <
Philipp Reisnerb411b362009-09-25 16:07:19 -07003252 drbd_get_capacity(mdev->this_bdev) &&
3253 mdev->state.disk >= D_OUTDATED &&
3254 mdev->state.conn < C_CONNECTED) {
3255 dev_err(DEV, "The peer's disk size is too small!\n");
Philipp Reisner38fa9982011-03-15 18:24:49 +01003256 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003257 mdev->ldev->dc.disk_size = my_usize;
3258 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003259 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003260 }
3261 put_ldev(mdev);
3262 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003263
Philipp Reisnere89b5912010-03-24 17:11:33 +01003264 ddsf = be16_to_cpu(p->dds_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003265 if (get_ldev(mdev)) {
Bart Van Assche24c48302011-05-21 18:32:29 +02003266 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003267 put_ldev(mdev);
3268 if (dd == dev_size_error)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003269 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003270 drbd_md_sync(mdev);
3271 } else {
3272 /* I am diskless, need to accept the peer's size. */
3273 drbd_set_my_capacity(mdev, p_size);
3274 }
3275
Philipp Reisner99432fc2011-05-20 16:39:13 +02003276 mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size);
3277 drbd_reconsider_max_bio_size(mdev);
3278
Philipp Reisnerb411b362009-09-25 16:07:19 -07003279 if (get_ldev(mdev)) {
3280 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
3281 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
3282 ldsc = 1;
3283 }
3284
Philipp Reisnerb411b362009-09-25 16:07:19 -07003285 put_ldev(mdev);
3286 }
3287
3288 if (mdev->state.conn > C_WF_REPORT_PARAMS) {
3289 if (be64_to_cpu(p->c_size) !=
3290 drbd_get_capacity(mdev->this_bdev) || ldsc) {
3291 /* we have different sizes, probably peer
3292 * needs to know my new size... */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003293 drbd_send_sizes(mdev, 0, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003294 }
3295 if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
3296 (dd == grew && mdev->state.conn == C_CONNECTED)) {
3297 if (mdev->state.pdsk >= D_INCONSISTENT &&
Philipp Reisnere89b5912010-03-24 17:11:33 +01003298 mdev->state.disk >= D_INCONSISTENT) {
3299 if (ddsf & DDSF_NO_RESYNC)
3300 dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
3301 else
3302 resync_after_online_grow(mdev);
3303 } else
Philipp Reisnerb411b362009-09-25 16:07:19 -07003304 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
3305 }
3306 }
3307
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003308 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003309}
3310
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003311static int receive_uuids(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003312{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003313 struct drbd_conf *mdev;
3314 struct p_uuids *p = tconn->data.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003315 u64 *p_uuid;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003316 int i, updated_uuids = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003317
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003318 mdev = vnr_to_mdev(tconn, pi->vnr);
3319 if (!mdev)
3320 return config_unknown_volume(tconn, pi);
3321
Philipp Reisnerb411b362009-09-25 16:07:19 -07003322 p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
3323
3324 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
3325 p_uuid[i] = be64_to_cpu(p->uuid[i]);
3326
3327 kfree(mdev->p_uuid);
3328 mdev->p_uuid = p_uuid;
3329
3330 if (mdev->state.conn < C_CONNECTED &&
3331 mdev->state.disk < D_INCONSISTENT &&
3332 mdev->state.role == R_PRIMARY &&
3333 (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
3334 dev_err(DEV, "Can only connect to data with current UUID=%016llX\n",
3335 (unsigned long long)mdev->ed_uuid);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003336 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003337 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003338 }
3339
3340 if (get_ldev(mdev)) {
3341 int skip_initial_sync =
3342 mdev->state.conn == C_CONNECTED &&
Philipp Reisner31890f42011-01-19 14:12:51 +01003343 mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003344 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
3345 (p_uuid[UI_FLAGS] & 8);
3346 if (skip_initial_sync) {
3347 dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n");
3348 drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003349 "clear_n_write from receive_uuids",
3350 BM_LOCKED_TEST_ALLOWED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003351 _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]);
3352 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3353 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3354 CS_VERBOSE, NULL);
3355 drbd_md_sync(mdev);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003356 updated_uuids = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003357 }
3358 put_ldev(mdev);
Philipp Reisner18a50fa2010-06-21 14:14:15 +02003359 } else if (mdev->state.disk < D_INCONSISTENT &&
3360 mdev->state.role == R_PRIMARY) {
3361 /* I am a diskless primary, the peer just created a new current UUID
3362 for me. */
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003363 updated_uuids = drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003364 }
3365
3366 /* Before we test for the disk state, we should wait until an eventually
3367 ongoing cluster wide state change is finished. That is important if
3368 we are primary and are detaching from our disk. We need to see the
3369 new disk state... */
Philipp Reisner8410da8f02011-02-11 20:11:10 +01003370 mutex_lock(mdev->state_mutex);
3371 mutex_unlock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003372 if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT)
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003373 updated_uuids |= drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
3374
3375 if (updated_uuids)
3376 drbd_print_uuids(mdev, "receiver updated UUIDs to");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003377
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003378 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003379}
3380
3381/**
3382 * convert_state() - Converts the peer's view of the cluster state to our point of view
3383 * @ps: The state as seen by the peer.
3384 */
3385static union drbd_state convert_state(union drbd_state ps)
3386{
3387 union drbd_state ms;
3388
3389 static enum drbd_conns c_tab[] = {
3390 [C_CONNECTED] = C_CONNECTED,
3391
3392 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
3393 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
3394 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
3395 [C_VERIFY_S] = C_VERIFY_T,
3396 [C_MASK] = C_MASK,
3397 };
3398
3399 ms.i = ps.i;
3400
3401 ms.conn = c_tab[ps.conn];
3402 ms.peer = ps.role;
3403 ms.role = ps.peer;
3404 ms.pdsk = ps.disk;
3405 ms.disk = ps.pdsk;
3406 ms.peer_isp = (ps.aftr_isp | ps.user_isp);
3407
3408 return ms;
3409}
3410
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003411static int receive_req_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003412{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003413 struct drbd_conf *mdev;
3414 struct p_req_state *p = tconn->data.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003415 union drbd_state mask, val;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01003416 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003417
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003418 mdev = vnr_to_mdev(tconn, pi->vnr);
3419 if (!mdev)
3420 return -EIO;
3421
Philipp Reisnerb411b362009-09-25 16:07:19 -07003422 mask.i = be32_to_cpu(p->mask);
3423 val.i = be32_to_cpu(p->val);
3424
Philipp Reisner25703f82011-02-07 14:35:25 +01003425 if (test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags) &&
Philipp Reisner8410da8f02011-02-11 20:11:10 +01003426 mutex_is_locked(mdev->state_mutex)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003427 drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003428 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003429 }
3430
3431 mask = convert_state(mask);
3432 val = convert_state(val);
3433
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003434 rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
3435 drbd_send_sr_reply(mdev, rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003436
Philipp Reisnerb411b362009-09-25 16:07:19 -07003437 drbd_md_sync(mdev);
3438
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003439 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003440}
3441
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003442static int receive_req_conn_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003443{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01003444 struct p_req_state *p = tconn->data.rbuf;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003445 union drbd_state mask, val;
3446 enum drbd_state_rv rv;
3447
3448 mask.i = be32_to_cpu(p->mask);
3449 val.i = be32_to_cpu(p->val);
3450
3451 if (test_bit(DISCARD_CONCURRENT, &tconn->flags) &&
3452 mutex_is_locked(&tconn->cstate_mutex)) {
3453 conn_send_sr_reply(tconn, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003454 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003455 }
3456
3457 mask = convert_state(mask);
3458 val = convert_state(val);
3459
Philipp Reisner778bcf22011-03-28 12:55:03 +02003460 rv = conn_request_state(tconn, mask, val, CS_VERBOSE | CS_LOCAL_ONLY | CS_IGN_OUTD_FAIL);
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003461 conn_send_sr_reply(tconn, rv);
3462
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003463 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003464}
3465
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003466static int receive_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003467{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003468 struct drbd_conf *mdev;
3469 struct p_state *p = tconn->data.rbuf;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003470 union drbd_state os, ns, peer_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003471 enum drbd_disk_state real_peer_disk;
Philipp Reisner65d922c2010-06-16 16:18:09 +02003472 enum chg_state_flags cs_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003473 int rv;
3474
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003475 mdev = vnr_to_mdev(tconn, pi->vnr);
3476 if (!mdev)
3477 return config_unknown_volume(tconn, pi);
3478
Philipp Reisnerb411b362009-09-25 16:07:19 -07003479 peer_state.i = be32_to_cpu(p->state);
3480
3481 real_peer_disk = peer_state.disk;
3482 if (peer_state.disk == D_NEGOTIATING) {
3483 real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
3484 dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
3485 }
3486
Philipp Reisner87eeee42011-01-19 14:16:30 +01003487 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003488 retry:
Philipp Reisner78bae592011-03-28 15:40:12 +02003489 os = ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003490 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003491
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003492 /* peer says his disk is uptodate, while we think it is inconsistent,
3493 * and this happens while we think we have a sync going on. */
3494 if (os.pdsk == D_INCONSISTENT && real_peer_disk == D_UP_TO_DATE &&
3495 os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) {
3496 /* If we are (becoming) SyncSource, but peer is still in sync
3497 * preparation, ignore its uptodate-ness to avoid flapping, it
3498 * will change to inconsistent once the peer reaches active
3499 * syncing states.
3500 * It may have changed syncer-paused flags, however, so we
3501 * cannot ignore this completely. */
3502 if (peer_state.conn > C_CONNECTED &&
3503 peer_state.conn < C_SYNC_SOURCE)
3504 real_peer_disk = D_INCONSISTENT;
3505
3506 /* if peer_state changes to connected at the same time,
3507 * it explicitly notifies us that it finished resync.
3508 * Maybe we should finish it up, too? */
3509 else if (os.conn >= C_SYNC_SOURCE &&
3510 peer_state.conn == C_CONNECTED) {
3511 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed)
3512 drbd_resync_finished(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003513 return 0;
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003514 }
3515 }
3516
3517 /* peer says his disk is inconsistent, while we think it is uptodate,
3518 * and this happens while the peer still thinks we have a sync going on,
3519 * but we think we are already done with the sync.
3520 * We ignore this to avoid flapping pdsk.
3521 * This should not happen, if the peer is a recent version of drbd. */
3522 if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT &&
3523 os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE)
3524 real_peer_disk = D_UP_TO_DATE;
3525
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003526 if (ns.conn == C_WF_REPORT_PARAMS)
3527 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003528
Philipp Reisner67531712010-10-27 12:21:30 +02003529 if (peer_state.conn == C_AHEAD)
3530 ns.conn = C_BEHIND;
3531
Philipp Reisnerb411b362009-09-25 16:07:19 -07003532 if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING &&
3533 get_ldev_if_state(mdev, D_NEGOTIATING)) {
3534 int cr; /* consider resync */
3535
3536 /* if we established a new connection */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003537 cr = (os.conn < C_CONNECTED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003538 /* if we had an established connection
3539 * and one of the nodes newly attaches a disk */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003540 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003541 (peer_state.disk == D_NEGOTIATING ||
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003542 os.disk == D_NEGOTIATING));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003543 /* if we have both been inconsistent, and the peer has been
3544 * forced to be UpToDate with --overwrite-data */
3545 cr |= test_bit(CONSIDER_RESYNC, &mdev->flags);
3546 /* if we had been plain connected, and the admin requested to
3547 * start a sync by "invalidate" or "invalidate-remote" */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003548 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003549 (peer_state.conn >= C_STARTING_SYNC_S &&
3550 peer_state.conn <= C_WF_BITMAP_T));
3551
3552 if (cr)
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003553 ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003554
3555 put_ldev(mdev);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003556 if (ns.conn == C_MASK) {
3557 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003558 if (mdev->state.disk == D_NEGOTIATING) {
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003559 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003560 } else if (peer_state.disk == D_NEGOTIATING) {
3561 dev_err(DEV, "Disk attach process on the peer node was aborted.\n");
3562 peer_state.disk = D_DISKLESS;
Lars Ellenberg580b9762010-02-26 23:15:23 +01003563 real_peer_disk = D_DISKLESS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003564 } else {
Philipp Reisner8169e412011-03-15 18:40:27 +01003565 if (test_and_clear_bit(CONN_DRY_RUN, &mdev->tconn->flags))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003566 return -EIO;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003567 D_ASSERT(os.conn == C_WF_REPORT_PARAMS);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003568 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003569 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003570 }
3571 }
3572 }
3573
Philipp Reisner87eeee42011-01-19 14:16:30 +01003574 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner78bae592011-03-28 15:40:12 +02003575 if (os.i != drbd_read_state(mdev).i)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003576 goto retry;
3577 clear_bit(CONSIDER_RESYNC, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003578 ns.peer = peer_state.role;
3579 ns.pdsk = real_peer_disk;
3580 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003581 if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003582 ns.disk = mdev->new_state_tmp.disk;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003583 cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD);
Philipp Reisner2aebfab2011-03-28 16:48:11 +02003584 if (ns.pdsk == D_CONSISTENT && drbd_suspended(mdev) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED &&
Philipp Reisner481c6f52010-06-22 14:03:27 +02003585 test_bit(NEW_CUR_UUID, &mdev->flags)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01003586 /* Do not allow tl_restart(RESEND) for a rebooted peer. We can only allow this
Philipp Reisner481c6f52010-06-22 14:03:27 +02003587 for temporal network outages! */
Philipp Reisner87eeee42011-01-19 14:16:30 +01003588 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003589 dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01003590 tl_clear(mdev->tconn);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003591 drbd_uuid_new_current(mdev);
3592 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003593 conn_request_state(mdev->tconn, NS2(conn, C_PROTOCOL_ERROR, susp, 0), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003594 return -EIO;
Philipp Reisner481c6f52010-06-22 14:03:27 +02003595 }
Philipp Reisner65d922c2010-06-16 16:18:09 +02003596 rv = _drbd_set_state(mdev, ns, cs_flags, NULL);
Philipp Reisner78bae592011-03-28 15:40:12 +02003597 ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003598 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003599
3600 if (rv < SS_SUCCESS) {
Philipp Reisner38fa9982011-03-15 18:24:49 +01003601 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003602 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003603 }
3604
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003605 if (os.conn > C_WF_REPORT_PARAMS) {
3606 if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003607 peer_state.disk != D_NEGOTIATING ) {
3608 /* we want resync, peer has not yet decided to sync... */
3609 /* Nowadays only used when forcing a node into primary role and
3610 setting its disk to UpToDate with that */
3611 drbd_send_uuids(mdev);
3612 drbd_send_state(mdev);
3613 }
3614 }
3615
Philipp Reisner89e58e72011-01-19 13:12:45 +01003616 mdev->tconn->net_conf->want_lose = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003617
3618 drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
3619
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003620 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003621}
3622
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003623static int receive_sync_uuid(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003624{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003625 struct drbd_conf *mdev;
3626 struct p_rs_uuid *p = tconn->data.rbuf;
3627
3628 mdev = vnr_to_mdev(tconn, pi->vnr);
3629 if (!mdev)
3630 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003631
3632 wait_event(mdev->misc_wait,
3633 mdev->state.conn == C_WF_SYNC_UUID ||
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02003634 mdev->state.conn == C_BEHIND ||
Philipp Reisnerb411b362009-09-25 16:07:19 -07003635 mdev->state.conn < C_CONNECTED ||
3636 mdev->state.disk < D_NEGOTIATING);
3637
3638 /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */
3639
Philipp Reisnerb411b362009-09-25 16:07:19 -07003640 /* Here the _drbd_uuid_ functions are right, current should
3641 _not_ be rotated into the history */
3642 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
3643 _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid));
3644 _drbd_uuid_set(mdev, UI_BITMAP, 0UL);
3645
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003646 drbd_print_uuids(mdev, "updated sync uuid");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003647 drbd_start_resync(mdev, C_SYNC_TARGET);
3648
3649 put_ldev(mdev);
3650 } else
3651 dev_err(DEV, "Ignoring SyncUUID packet!\n");
3652
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003653 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003654}
3655
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003656/**
3657 * receive_bitmap_plain
3658 *
3659 * Return 0 when done, 1 when another iteration is needed, and a negative error
3660 * code upon failure.
3661 */
3662static int
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003663receive_bitmap_plain(struct drbd_conf *mdev, unsigned int size,
Andreas Gruenbacherfc568152011-03-24 21:23:50 +01003664 struct p_header *h, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003665{
Andreas Gruenbacherfc568152011-03-24 21:23:50 +01003666 unsigned long *buffer = (unsigned long *)h->payload;
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003667 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE -
3668 drbd_header_size(mdev->tconn);
3669 unsigned int num_words = min_t(size_t, data_size / sizeof(unsigned long),
3670 c->bm_words - c->word_offset);
3671 unsigned int want = num_words * sizeof(unsigned long);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003672 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003673
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003674 if (want != size) {
3675 dev_err(DEV, "%s:want (%u) != size (%u)\n", __func__, want, size);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003676 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003677 }
3678 if (want == 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003679 return 0;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003680 err = drbd_recv_all(mdev->tconn, buffer, want);
3681 if (err)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003682 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003683
3684 drbd_bm_merge_lel(mdev, c->word_offset, num_words, buffer);
3685
3686 c->word_offset += num_words;
3687 c->bit_offset = c->word_offset * BITS_PER_LONG;
3688 if (c->bit_offset > c->bm_bits)
3689 c->bit_offset = c->bm_bits;
3690
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003691 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003692}
3693
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003694static enum drbd_bitmap_code dcbp_get_code(struct p_compressed_bm *p)
3695{
3696 return (enum drbd_bitmap_code)(p->encoding & 0x0f);
3697}
3698
3699static int dcbp_get_start(struct p_compressed_bm *p)
3700{
3701 return (p->encoding & 0x80) != 0;
3702}
3703
3704static int dcbp_get_pad_bits(struct p_compressed_bm *p)
3705{
3706 return (p->encoding >> 4) & 0x7;
3707}
3708
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003709/**
3710 * recv_bm_rle_bits
3711 *
3712 * Return 0 when done, 1 when another iteration is needed, and a negative error
3713 * code upon failure.
3714 */
3715static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07003716recv_bm_rle_bits(struct drbd_conf *mdev,
3717 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003718 struct bm_xfer_ctx *c,
3719 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003720{
3721 struct bitstream bs;
3722 u64 look_ahead;
3723 u64 rl;
3724 u64 tmp;
3725 unsigned long s = c->bit_offset;
3726 unsigned long e;
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003727 int toggle = dcbp_get_start(p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003728 int have;
3729 int bits;
3730
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003731 bitstream_init(&bs, p->code, len, dcbp_get_pad_bits(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003732
3733 bits = bitstream_get_bits(&bs, &look_ahead, 64);
3734 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003735 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003736
3737 for (have = bits; have > 0; s += rl, toggle = !toggle) {
3738 bits = vli_decode_bits(&rl, look_ahead);
3739 if (bits <= 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003740 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003741
3742 if (toggle) {
3743 e = s + rl -1;
3744 if (e >= c->bm_bits) {
3745 dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003746 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003747 }
3748 _drbd_bm_set_bits(mdev, s, e);
3749 }
3750
3751 if (have < bits) {
3752 dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
3753 have, bits, look_ahead,
3754 (unsigned int)(bs.cur.b - p->code),
3755 (unsigned int)bs.buf_len);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003756 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003757 }
3758 look_ahead >>= bits;
3759 have -= bits;
3760
3761 bits = bitstream_get_bits(&bs, &tmp, 64 - have);
3762 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003763 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003764 look_ahead |= tmp << have;
3765 have += bits;
3766 }
3767
3768 c->bit_offset = s;
3769 bm_xfer_ctx_bit_to_word_offset(c);
3770
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003771 return (s != c->bm_bits);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003772}
3773
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003774/**
3775 * decode_bitmap_c
3776 *
3777 * Return 0 when done, 1 when another iteration is needed, and a negative error
3778 * code upon failure.
3779 */
3780static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07003781decode_bitmap_c(struct drbd_conf *mdev,
3782 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003783 struct bm_xfer_ctx *c,
3784 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003785{
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003786 if (dcbp_get_code(p) == RLE_VLI_Bits)
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003787 return recv_bm_rle_bits(mdev, p, c, len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003788
3789 /* other variants had been implemented for evaluation,
3790 * but have been dropped as this one turned out to be "best"
3791 * during all our tests. */
3792
3793 dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003794 conn_request_state(mdev->tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003795 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003796}
3797
3798void INFO_bm_xfer_stats(struct drbd_conf *mdev,
3799 const char *direction, struct bm_xfer_ctx *c)
3800{
3801 /* what would it take to transfer it "plaintext" */
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003802 unsigned int header_size = drbd_header_size(mdev->tconn);
3803 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE - header_size;
3804 unsigned int plain =
3805 header_size * (DIV_ROUND_UP(c->bm_words, data_size) + 1) +
3806 c->bm_words * sizeof(unsigned long);
3807 unsigned int total = c->bytes[0] + c->bytes[1];
3808 unsigned int r;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003809
3810 /* total can not be zero. but just in case: */
3811 if (total == 0)
3812 return;
3813
3814 /* don't report if not compressed */
3815 if (total >= plain)
3816 return;
3817
3818 /* total < plain. check for overflow, still */
3819 r = (total > UINT_MAX/1000) ? (total / (plain/1000))
3820 : (1000 * total / plain);
3821
3822 if (r > 1000)
3823 r = 1000;
3824
3825 r = 1000 - r;
3826 dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
3827 "total %u; compression: %u.%u%%\n",
3828 direction,
3829 c->bytes[1], c->packets[1],
3830 c->bytes[0], c->packets[0],
3831 total, r/10, r % 10);
3832}
3833
3834/* Since we are processing the bitfield from lower addresses to higher,
3835 it does not matter if the process it in 32 bit chunks or 64 bit
3836 chunks as long as it is little endian. (Understand it as byte stream,
3837 beginning with the lowest byte...) If we would use big endian
3838 we would need to process it from the highest address to the lowest,
3839 in order to be agnostic to the 32 vs 64 bits issue.
3840
3841 returns 0 on failure, 1 if we successfully received it. */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003842static int receive_bitmap(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003843{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003844 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003845 struct bm_xfer_ctx c;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003846 int err;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003847 struct p_header *h = tconn->data.rbuf;
3848
3849 mdev = vnr_to_mdev(tconn, pi->vnr);
3850 if (!mdev)
3851 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003852
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003853 drbd_bm_lock(mdev, "receive bitmap", BM_LOCKED_SET_ALLOWED);
3854 /* you are supposed to send additional out-of-sync information
3855 * if you actually set bits during this phase */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003856
Philipp Reisnerb411b362009-09-25 16:07:19 -07003857 c = (struct bm_xfer_ctx) {
3858 .bm_bits = drbd_bm_bits(mdev),
3859 .bm_words = drbd_bm_words(mdev),
3860 };
3861
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003862 for(;;) {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003863 if (pi->cmd == P_BITMAP) {
3864 err = receive_bitmap_plain(mdev, pi->size, h, &c);
3865 } else if (pi->cmd == P_COMPRESSED_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003866 /* MAYBE: sanity check that we speak proto >= 90,
3867 * and the feature is enabled! */
3868 struct p_compressed_bm *p;
3869
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003870 if (pi->size > DRBD_SOCKET_BUFFER_SIZE - drbd_header_size(tconn)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003871 dev_err(DEV, "ReportCBitmap packet too large\n");
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003872 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003873 goto out;
3874 }
Andreas Gruenbacherfc568152011-03-24 21:23:50 +01003875
3876 p = mdev->tconn->data.rbuf;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003877 err = drbd_recv_all(mdev->tconn, p->head.payload, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003878 if (err)
3879 goto out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003880 if (pi->size <= (sizeof(*p) - sizeof(p->head))) {
3881 dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003882 err = -EIO;
Andreas Gruenbacher78fcbda2010-12-10 22:18:27 +01003883 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003884 }
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003885 err = decode_bitmap_c(mdev, p, &c, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003886 } else {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003887 dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", pi->cmd);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003888 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003889 goto out;
3890 }
3891
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003892 c.packets[pi->cmd == P_BITMAP]++;
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003893 c.bytes[pi->cmd == P_BITMAP] += drbd_header_size(tconn) + pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003894
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003895 if (err <= 0) {
3896 if (err < 0)
3897 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003898 break;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003899 }
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003900 err = drbd_recv_header(mdev->tconn, pi);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003901 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003902 goto out;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003903 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003904
3905 INFO_bm_xfer_stats(mdev, "receive", &c);
3906
3907 if (mdev->state.conn == C_WF_BITMAP_T) {
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01003908 enum drbd_state_rv rv;
3909
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003910 err = drbd_send_bitmap(mdev);
3911 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003912 goto out;
3913 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01003914 rv = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
3915 D_ASSERT(rv == SS_SUCCESS);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003916 } else if (mdev->state.conn != C_WF_BITMAP_S) {
3917 /* admin may have requested C_DISCONNECTING,
3918 * other threads may have noticed network errors */
3919 dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n",
3920 drbd_conn_str(mdev->state.conn));
3921 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003922 err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003923
Philipp Reisnerb411b362009-09-25 16:07:19 -07003924 out:
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003925 drbd_bm_unlock(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003926 if (!err && mdev->state.conn == C_WF_BITMAP_S)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003927 drbd_start_resync(mdev, C_SYNC_SOURCE);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003928 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003929}
3930
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003931static int receive_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003932{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003933 conn_warn(tconn, "skipping unknown optional packet type %d, l: %d!\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003934 pi->cmd, pi->size);
Philipp Reisner2de876e2011-03-15 14:38:01 +01003935
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003936 return ignore_remaining_packet(tconn, pi);
Philipp Reisner2de876e2011-03-15 14:38:01 +01003937}
3938
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003939static int receive_UnplugRemote(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003940{
Philipp Reisnerb411b362009-09-25 16:07:19 -07003941 /* Make sure we've acked all the TCP data associated
3942 * with the data requests being unplugged */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003943 drbd_tcp_quickack(tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003944
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003945 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003946}
3947
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003948static int receive_out_of_sync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner73a01a12010-10-27 14:33:00 +02003949{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003950 struct drbd_conf *mdev;
3951 struct p_block_desc *p = tconn->data.rbuf;
3952
3953 mdev = vnr_to_mdev(tconn, pi->vnr);
3954 if (!mdev)
3955 return -EIO;
Philipp Reisner73a01a12010-10-27 14:33:00 +02003956
Lars Ellenbergf735e3632010-12-17 21:06:18 +01003957 switch (mdev->state.conn) {
3958 case C_WF_SYNC_UUID:
3959 case C_WF_BITMAP_T:
3960 case C_BEHIND:
3961 break;
3962 default:
3963 dev_err(DEV, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n",
3964 drbd_conn_str(mdev->state.conn));
3965 }
3966
Philipp Reisner73a01a12010-10-27 14:33:00 +02003967 drbd_set_out_of_sync(mdev, be64_to_cpu(p->sector), be32_to_cpu(p->blksize));
3968
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003969 return 0;
Philipp Reisner73a01a12010-10-27 14:33:00 +02003970}
3971
Philipp Reisner02918be2010-08-20 14:35:10 +02003972struct data_cmd {
3973 int expect_payload;
3974 size_t pkt_size;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003975 int (*fn)(struct drbd_tconn *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003976};
3977
Philipp Reisner02918be2010-08-20 14:35:10 +02003978static struct data_cmd drbd_cmd_handler[] = {
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003979 [P_DATA] = { 1, sizeof(struct p_data), receive_Data },
3980 [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply },
3981 [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } ,
3982 [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } ,
3983 [P_BITMAP] = { 1, sizeof(struct p_header), receive_bitmap } ,
3984 [P_COMPRESSED_BITMAP] = { 1, sizeof(struct p_header), receive_bitmap } ,
3985 [P_UNPLUG_REMOTE] = { 0, sizeof(struct p_header), receive_UnplugRemote },
3986 [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
3987 [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
3988 [P_SYNC_PARAM] = { 1, sizeof(struct p_header), receive_SyncParam },
3989 [P_SYNC_PARAM89] = { 1, sizeof(struct p_header), receive_SyncParam },
3990 [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol },
3991 [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids },
3992 [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes },
3993 [P_STATE] = { 0, sizeof(struct p_state), receive_state },
3994 [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state },
3995 [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid },
3996 [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
3997 [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest },
3998 [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest },
3999 [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip },
4000 [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync },
4001 [P_CONN_ST_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_conn_state },
Philipp Reisner02918be2010-08-20 14:35:10 +02004002};
4003
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004004static void drbdd(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004005{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01004006 struct p_header *header = tconn->data.rbuf;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004007 struct packet_info pi;
Philipp Reisner02918be2010-08-20 14:35:10 +02004008 size_t shs; /* sub header size */
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004009 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004010
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004011 while (get_t_state(&tconn->receiver) == RUNNING) {
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004012 struct data_cmd *cmd;
4013
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004014 drbd_thread_current_set_cpu(&tconn->receiver);
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004015 if (drbd_recv_header(tconn, &pi))
Philipp Reisner02918be2010-08-20 14:35:10 +02004016 goto err_out;
4017
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004018 cmd = &drbd_cmd_handler[pi.cmd];
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004019 if (unlikely(pi.cmd >= ARRAY_SIZE(drbd_cmd_handler) || !cmd->fn)) {
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004020 conn_err(tconn, "unknown packet type %d, l: %d!\n", pi.cmd, pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004021 goto err_out;
Lars Ellenberg0b33a912009-11-16 15:58:04 +01004022 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004023
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004024 shs = cmd->pkt_size - sizeof(struct p_header);
4025 if (pi.size - shs > 0 && !cmd->expect_payload) {
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004026 conn_err(tconn, "No payload expected %s l:%d\n", cmdname(pi.cmd), pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004027 goto err_out;
4028 }
4029
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004030 if (shs) {
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004031 err = drbd_recv_all_warn(tconn, &header->payload, shs);
4032 if (err)
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004033 goto err_out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004034 pi.size -= shs;
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004035 }
4036
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004037 err = cmd->fn(tconn, &pi);
4038 if (err) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004039 conn_err(tconn, "error receiving %s, e: %d l: %d!\n",
4040 cmdname(pi.cmd), err, pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004041 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004042 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004043 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004044 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004045
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004046 err_out:
4047 conn_request_state(tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004048}
4049
Philipp Reisner0e29d162011-02-18 14:23:11 +01004050void conn_flush_workqueue(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004051{
4052 struct drbd_wq_barrier barr;
4053
4054 barr.w.cb = w_prev_work_done;
Philipp Reisner0e29d162011-02-18 14:23:11 +01004055 barr.w.tconn = tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004056 init_completion(&barr.done);
Philipp Reisner0e29d162011-02-18 14:23:11 +01004057 drbd_queue_work(&tconn->data.work, &barr.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004058 wait_for_completion(&barr.done);
4059}
4060
Philipp Reisner360cc742011-02-08 14:29:53 +01004061static void drbd_disconnect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004062{
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004063 enum drbd_conns oc;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004064 int rv = SS_UNKNOWN_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004065
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004066 if (tconn->cstate == C_STANDALONE)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004067 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004068
4069 /* asender does not clean up anything. it must not interfere, either */
Philipp Reisner360cc742011-02-08 14:29:53 +01004070 drbd_thread_stop(&tconn->asender);
4071 drbd_free_sock(tconn);
4072
4073 idr_for_each(&tconn->volumes, drbd_disconnected, tconn);
Philipp Reisner360cc742011-02-08 14:29:53 +01004074 conn_info(tconn, "Connection closed\n");
4075
Philipp Reisnercb703452011-03-24 11:03:07 +01004076 if (conn_highest_role(tconn) == R_PRIMARY && conn_highest_pdsk(tconn) >= D_UNKNOWN)
4077 conn_try_outdate_peer_async(tconn);
4078
Philipp Reisner360cc742011-02-08 14:29:53 +01004079 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004080 oc = tconn->cstate;
4081 if (oc >= C_UNCONNECTED)
4082 rv = _conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
4083
Philipp Reisner360cc742011-02-08 14:29:53 +01004084 spin_unlock_irq(&tconn->req_lock);
4085
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004086 if (oc == C_DISCONNECTING) {
Philipp Reisner360cc742011-02-08 14:29:53 +01004087 wait_event(tconn->net_cnt_wait, atomic_read(&tconn->net_cnt) == 0);
4088
4089 crypto_free_hash(tconn->cram_hmac_tfm);
4090 tconn->cram_hmac_tfm = NULL;
4091
4092 kfree(tconn->net_conf);
4093 tconn->net_conf = NULL;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004094 conn_request_state(tconn, NS(conn, C_STANDALONE), CS_VERBOSE);
Philipp Reisner360cc742011-02-08 14:29:53 +01004095 }
4096}
4097
4098static int drbd_disconnected(int vnr, void *p, void *data)
4099{
4100 struct drbd_conf *mdev = (struct drbd_conf *)p;
4101 enum drbd_fencing_p fp;
4102 unsigned int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004103
Philipp Reisner85719572010-07-21 10:20:17 +02004104 /* wait for current activity to cease. */
Philipp Reisner87eeee42011-01-19 14:16:30 +01004105 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004106 _drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
4107 _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee);
4108 _drbd_wait_ee_list_empty(mdev, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004109 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004110
4111 /* We do not have data structures that would allow us to
4112 * get the rs_pending_cnt down to 0 again.
4113 * * On C_SYNC_TARGET we do not have any data structures describing
4114 * the pending RSDataRequest's we have sent.
4115 * * On C_SYNC_SOURCE there is no data structure that tracks
4116 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
4117 * And no, it is not the sum of the reference counts in the
4118 * resync_LRU. The resync_LRU tracks the whole operation including
4119 * the disk-IO, while the rs_pending_cnt only tracks the blocks
4120 * on the fly. */
4121 drbd_rs_cancel_all(mdev);
4122 mdev->rs_total = 0;
4123 mdev->rs_failed = 0;
4124 atomic_set(&mdev->rs_pending_cnt, 0);
4125 wake_up(&mdev->misc_wait);
4126
Philipp Reisner7fde2be2011-03-01 11:08:28 +01004127 del_timer(&mdev->request_timer);
4128
Philipp Reisnerb411b362009-09-25 16:07:19 -07004129 del_timer_sync(&mdev->resync_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004130 resync_timer_fn((unsigned long)mdev);
4131
Philipp Reisnerb411b362009-09-25 16:07:19 -07004132 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
4133 * w_make_resync_request etc. which may still be on the worker queue
4134 * to be "canceled" */
Philipp Reisnera21e9292011-02-08 15:08:49 +01004135 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004136
4137 /* This also does reclaim_net_ee(). If we do this too early, we might
4138 * miss some resync ee and pages.*/
4139 drbd_process_done_ee(mdev);
4140
4141 kfree(mdev->p_uuid);
4142 mdev->p_uuid = NULL;
4143
Philipp Reisner2aebfab2011-03-28 16:48:11 +02004144 if (!drbd_suspended(mdev))
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01004145 tl_clear(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004146
Philipp Reisnerb411b362009-09-25 16:07:19 -07004147 drbd_md_sync(mdev);
4148
4149 fp = FP_DONT_CARE;
4150 if (get_ldev(mdev)) {
4151 fp = mdev->ldev->dc.fencing;
4152 put_ldev(mdev);
4153 }
4154
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004155 /* serialize with bitmap writeout triggered by the state change,
4156 * if any. */
4157 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
4158
Philipp Reisnerb411b362009-09-25 16:07:19 -07004159 /* tcp_close and release of sendpage pages can be deferred. I don't
4160 * want to use SO_LINGER, because apparently it can be deferred for
4161 * more than 20 seconds (longest time I checked).
4162 *
4163 * Actually we don't care for exactly when the network stack does its
4164 * put_page(), but release our reference on these pages right here.
4165 */
4166 i = drbd_release_ee(mdev, &mdev->net_ee);
4167 if (i)
4168 dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
Lars Ellenberg435f0742010-09-06 12:30:25 +02004169 i = atomic_read(&mdev->pp_in_use_by_net);
4170 if (i)
4171 dev_info(DEV, "pp_in_use_by_net = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004172 i = atomic_read(&mdev->pp_in_use);
4173 if (i)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02004174 dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004175
4176 D_ASSERT(list_empty(&mdev->read_ee));
4177 D_ASSERT(list_empty(&mdev->active_ee));
4178 D_ASSERT(list_empty(&mdev->sync_ee));
4179 D_ASSERT(list_empty(&mdev->done_ee));
4180
4181 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
4182 atomic_set(&mdev->current_epoch->epoch_size, 0);
4183 D_ASSERT(list_empty(&mdev->current_epoch->list));
Philipp Reisner360cc742011-02-08 14:29:53 +01004184
4185 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004186}
4187
4188/*
4189 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
4190 * we can agree on is stored in agreed_pro_version.
4191 *
4192 * feature flags and the reserved array should be enough room for future
4193 * enhancements of the handshake protocol, and possible plugins...
4194 *
4195 * for now, they are expected to be zero, but ignored.
4196 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004197static int drbd_send_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004198{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004199 struct drbd_socket *sock;
4200 struct p_connection_features *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004201
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004202 sock = &tconn->data;
4203 p = conn_prepare_command(tconn, sock);
4204 if (!p)
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004205 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004206 memset(p, 0, sizeof(*p));
4207 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
4208 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004209 return conn_send_command(tconn, sock, P_CONNECTION_FEATURES, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004210}
4211
4212/*
4213 * return values:
4214 * 1 yes, we have a valid connection
4215 * 0 oops, did not work out, please try again
4216 * -1 peer talks different language,
4217 * no point in trying again, please go standalone.
4218 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004219static int drbd_do_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004220{
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004221 /* ASSERT current == tconn->receiver ... */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004222 struct p_connection_features *p = tconn->data.rbuf;
4223 const int expect = sizeof(struct p_connection_features) - sizeof(struct p_header80);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004224 struct packet_info pi;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004225 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004226
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004227 err = drbd_send_features(tconn);
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004228 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004229 return 0;
4230
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004231 err = drbd_recv_header(tconn, &pi);
4232 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004233 return 0;
4234
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004235 if (pi.cmd != P_CONNECTION_FEATURES) {
4236 conn_err(tconn, "expected ConnectionFeatures packet, received: %s (0x%04x)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004237 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004238 return -1;
4239 }
4240
Philipp Reisner77351055b2011-02-07 17:24:26 +01004241 if (pi.size != expect) {
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004242 conn_err(tconn, "expected ConnectionFeatures length: %u, received: %u\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004243 expect, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004244 return -1;
4245 }
4246
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004247 err = drbd_recv_all_warn(tconn, &p->head.payload, expect);
4248 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004249 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004250
Philipp Reisnerb411b362009-09-25 16:07:19 -07004251 p->protocol_min = be32_to_cpu(p->protocol_min);
4252 p->protocol_max = be32_to_cpu(p->protocol_max);
4253 if (p->protocol_max == 0)
4254 p->protocol_max = p->protocol_min;
4255
4256 if (PRO_VERSION_MAX < p->protocol_min ||
4257 PRO_VERSION_MIN > p->protocol_max)
4258 goto incompat;
4259
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004260 tconn->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004261
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004262 conn_info(tconn, "Handshake successful: "
4263 "Agreed network protocol version %d\n", tconn->agreed_pro_version);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004264
4265 return 1;
4266
4267 incompat:
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004268 conn_err(tconn, "incompatible DRBD dialects: "
Philipp Reisnerb411b362009-09-25 16:07:19 -07004269 "I support %d-%d, peer supports %d-%d\n",
4270 PRO_VERSION_MIN, PRO_VERSION_MAX,
4271 p->protocol_min, p->protocol_max);
4272 return -1;
4273}
4274
4275#if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
Philipp Reisner13e60372011-02-08 09:54:40 +01004276static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004277{
4278 dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
4279 dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004280 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004281}
4282#else
4283#define CHALLENGE_LEN 64
Johannes Thomab10d96c2010-01-07 16:02:50 +01004284
4285/* Return value:
4286 1 - auth succeeded,
4287 0 - failed, try again (network error),
4288 -1 - auth failed, don't try again.
4289*/
4290
Philipp Reisner13e60372011-02-08 09:54:40 +01004291static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004292{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004293 struct drbd_socket *sock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004294 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
4295 struct scatterlist sg;
4296 char *response = NULL;
4297 char *right_response = NULL;
4298 char *peers_ch = NULL;
Philipp Reisner13e60372011-02-08 09:54:40 +01004299 unsigned int key_len = strlen(tconn->net_conf->shared_secret);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004300 unsigned int resp_size;
4301 struct hash_desc desc;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004302 struct packet_info pi;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004303 int err, rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004304
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004305 /* FIXME: Put the challenge/response into the preallocated socket buffer. */
4306
Philipp Reisner13e60372011-02-08 09:54:40 +01004307 desc.tfm = tconn->cram_hmac_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004308 desc.flags = 0;
4309
Philipp Reisner13e60372011-02-08 09:54:40 +01004310 rv = crypto_hash_setkey(tconn->cram_hmac_tfm,
4311 (u8 *)tconn->net_conf->shared_secret, key_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004312 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004313 conn_err(tconn, "crypto_hash_setkey() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004314 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004315 goto fail;
4316 }
4317
4318 get_random_bytes(my_challenge, CHALLENGE_LEN);
4319
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004320 sock = &tconn->data;
4321 if (!conn_prepare_command(tconn, sock)) {
4322 rv = 0;
4323 goto fail;
4324 }
4325 rv = !conn_send_command(tconn, sock, P_AUTH_CHALLENGE,
4326 sizeof(struct p_header),
4327 my_challenge, CHALLENGE_LEN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004328 if (!rv)
4329 goto fail;
4330
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004331 err = drbd_recv_header(tconn, &pi);
4332 if (err) {
4333 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004334 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004335 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004336
Philipp Reisner77351055b2011-02-07 17:24:26 +01004337 if (pi.cmd != P_AUTH_CHALLENGE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004338 conn_err(tconn, "expected AuthChallenge packet, received: %s (0x%04x)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004339 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004340 rv = 0;
4341 goto fail;
4342 }
4343
Philipp Reisner77351055b2011-02-07 17:24:26 +01004344 if (pi.size > CHALLENGE_LEN * 2) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004345 conn_err(tconn, "expected AuthChallenge payload too big.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004346 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004347 goto fail;
4348 }
4349
Philipp Reisner77351055b2011-02-07 17:24:26 +01004350 peers_ch = kmalloc(pi.size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004351 if (peers_ch == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004352 conn_err(tconn, "kmalloc of peers_ch failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004353 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004354 goto fail;
4355 }
4356
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004357 err = drbd_recv_all_warn(tconn, peers_ch, pi.size);
4358 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004359 rv = 0;
4360 goto fail;
4361 }
4362
Philipp Reisner13e60372011-02-08 09:54:40 +01004363 resp_size = crypto_hash_digestsize(tconn->cram_hmac_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004364 response = kmalloc(resp_size, GFP_NOIO);
4365 if (response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004366 conn_err(tconn, "kmalloc of response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004367 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004368 goto fail;
4369 }
4370
4371 sg_init_table(&sg, 1);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004372 sg_set_buf(&sg, peers_ch, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004373
4374 rv = crypto_hash_digest(&desc, &sg, sg.length, response);
4375 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004376 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004377 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004378 goto fail;
4379 }
4380
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004381 if (!conn_prepare_command(tconn, sock)) {
4382 rv = 0;
4383 goto fail;
4384 }
4385 rv = !conn_send_command(tconn, sock, P_AUTH_RESPONSE,
4386 sizeof(struct p_header),
4387 response, resp_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004388 if (!rv)
4389 goto fail;
4390
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004391 err = drbd_recv_header(tconn, &pi);
4392 if (err) {
4393 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004394 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004395 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004396
Philipp Reisner77351055b2011-02-07 17:24:26 +01004397 if (pi.cmd != P_AUTH_RESPONSE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004398 conn_err(tconn, "expected AuthResponse packet, received: %s (0x%04x)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004399 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004400 rv = 0;
4401 goto fail;
4402 }
4403
Philipp Reisner77351055b2011-02-07 17:24:26 +01004404 if (pi.size != resp_size) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004405 conn_err(tconn, "expected AuthResponse payload of wrong size\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004406 rv = 0;
4407 goto fail;
4408 }
4409
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004410 err = drbd_recv_all_warn(tconn, response , resp_size);
4411 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004412 rv = 0;
4413 goto fail;
4414 }
4415
4416 right_response = kmalloc(resp_size, GFP_NOIO);
Julia Lawall2d1ee872009-12-27 22:27:11 +01004417 if (right_response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004418 conn_err(tconn, "kmalloc of right_response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004419 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004420 goto fail;
4421 }
4422
4423 sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
4424
4425 rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
4426 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004427 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004428 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004429 goto fail;
4430 }
4431
4432 rv = !memcmp(response, right_response, resp_size);
4433
4434 if (rv)
Philipp Reisner13e60372011-02-08 09:54:40 +01004435 conn_info(tconn, "Peer authenticated using %d bytes of '%s' HMAC\n",
4436 resp_size, tconn->net_conf->cram_hmac_alg);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004437 else
4438 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004439
4440 fail:
4441 kfree(peers_ch);
4442 kfree(response);
4443 kfree(right_response);
4444
4445 return rv;
4446}
4447#endif
4448
4449int drbdd_init(struct drbd_thread *thi)
4450{
Philipp Reisner392c8802011-02-09 10:33:31 +01004451 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004452 int h;
4453
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004454 conn_info(tconn, "receiver (re)started\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004455
4456 do {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004457 h = drbd_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004458 if (h == 0) {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004459 drbd_disconnect(tconn);
Philipp Reisner20ee6392011-01-18 15:28:59 +01004460 schedule_timeout_interruptible(HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004461 }
4462 if (h == -1) {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004463 conn_warn(tconn, "Discarding network configuration.\n");
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004464 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004465 }
4466 } while (h == 0);
4467
4468 if (h > 0) {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004469 if (get_net_conf(tconn)) {
4470 drbdd(tconn);
4471 put_net_conf(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004472 }
4473 }
4474
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004475 drbd_disconnect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004476
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004477 conn_info(tconn, "receiver terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004478 return 0;
4479}
4480
4481/* ********* acknowledge sender ******** */
4482
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004483static int got_conn_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004484{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01004485 struct p_req_state_reply *p = tconn->meta.rbuf;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004486 int retcode = be32_to_cpu(p->retcode);
4487
4488 if (retcode >= SS_SUCCESS) {
4489 set_bit(CONN_WD_ST_CHG_OKAY, &tconn->flags);
4490 } else {
4491 set_bit(CONN_WD_ST_CHG_FAIL, &tconn->flags);
4492 conn_err(tconn, "Requested state change failed by peer: %s (%d)\n",
4493 drbd_set_st_err_str(retcode), retcode);
4494 }
4495 wake_up(&tconn->ping_wait);
4496
4497 return true;
4498}
4499
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004500static int got_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004501{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004502 struct drbd_conf *mdev;
4503 struct p_req_state_reply *p = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004504 int retcode = be32_to_cpu(p->retcode);
4505
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004506 mdev = vnr_to_mdev(tconn, pi->vnr);
4507 if (!mdev)
4508 return false;
4509
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004510 if (retcode >= SS_SUCCESS) {
4511 set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
4512 } else {
4513 set_bit(CL_ST_CHG_FAIL, &mdev->flags);
4514 dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
4515 drbd_set_st_err_str(retcode), retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004516 }
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004517 wake_up(&mdev->state_wait);
4518
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004519 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004520}
4521
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004522static int got_Ping(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004523{
Andreas Gruenbachera17647a2011-04-01 12:49:42 +02004524 return !drbd_send_ping_ack(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004525
4526}
4527
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004528static int got_PingAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004529{
4530 /* restore idle timeout */
Philipp Reisner2a67d8b2011-02-09 14:10:32 +01004531 tconn->meta.socket->sk->sk_rcvtimeo = tconn->net_conf->ping_int*HZ;
4532 if (!test_and_set_bit(GOT_PING_ACK, &tconn->flags))
4533 wake_up(&tconn->ping_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004534
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004535 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004536}
4537
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004538static int got_IsInSync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004539{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004540 struct drbd_conf *mdev;
4541 struct p_block_ack *p = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004542 sector_t sector = be64_to_cpu(p->sector);
4543 int blksize = be32_to_cpu(p->blksize);
4544
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004545 mdev = vnr_to_mdev(tconn, pi->vnr);
4546 if (!mdev)
4547 return false;
4548
Philipp Reisner31890f42011-01-19 14:12:51 +01004549 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004550
4551 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4552
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004553 if (get_ldev(mdev)) {
4554 drbd_rs_complete_io(mdev, sector);
4555 drbd_set_in_sync(mdev, sector, blksize);
4556 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
4557 mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
4558 put_ldev(mdev);
4559 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004560 dec_rs_pending(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02004561 atomic_add(blksize >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004562
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004563 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004564}
4565
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004566static int
4567validate_req_change_req_state(struct drbd_conf *mdev, u64 id, sector_t sector,
4568 struct rb_root *root, const char *func,
4569 enum drbd_req_event what, bool missing_ok)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004570{
4571 struct drbd_request *req;
4572 struct bio_and_error m;
4573
Philipp Reisner87eeee42011-01-19 14:16:30 +01004574 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004575 req = find_request(mdev, root, id, sector, missing_ok, func);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004576 if (unlikely(!req)) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01004577 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004578 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004579 }
4580 __req_mod(req, what, &m);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004581 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004582
4583 if (m.bio)
4584 complete_master_bio(mdev, &m);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004585 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004586}
4587
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004588static int got_BlockAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004589{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004590 struct drbd_conf *mdev;
4591 struct p_block_ack *p = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004592 sector_t sector = be64_to_cpu(p->sector);
4593 int blksize = be32_to_cpu(p->blksize);
4594 enum drbd_req_event what;
4595
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004596 mdev = vnr_to_mdev(tconn, pi->vnr);
4597 if (!mdev)
4598 return false;
4599
Philipp Reisnerb411b362009-09-25 16:07:19 -07004600 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4601
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004602 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004603 drbd_set_in_sync(mdev, sector, blksize);
4604 dec_rs_pending(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004605 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004606 }
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004607 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004608 case P_RS_WRITE_ACK:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004609 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004610 what = WRITE_ACKED_BY_PEER_AND_SIS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004611 break;
4612 case P_WRITE_ACK:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004613 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004614 what = WRITE_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004615 break;
4616 case P_RECV_ACK:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004617 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_B);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004618 what = RECV_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004619 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004620 case P_DISCARD_WRITE:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004621 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004622 what = DISCARD_WRITE;
4623 break;
4624 case P_RETRY_WRITE:
4625 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
4626 what = POSTPONE_WRITE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004627 break;
4628 default:
4629 D_ASSERT(0);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004630 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004631 }
4632
4633 return validate_req_change_req_state(mdev, p->block_id, sector,
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004634 &mdev->write_requests, __func__,
4635 what, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004636}
4637
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004638static int got_NegAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004639{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004640 struct drbd_conf *mdev;
4641 struct p_block_ack *p = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004642 sector_t sector = be64_to_cpu(p->sector);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004643 int size = be32_to_cpu(p->blksize);
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004644 bool missing_ok = tconn->net_conf->wire_protocol == DRBD_PROT_A ||
4645 tconn->net_conf->wire_protocol == DRBD_PROT_B;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004646 bool found;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004647
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004648 mdev = vnr_to_mdev(tconn, pi->vnr);
4649 if (!mdev)
4650 return false;
4651
Philipp Reisnerb411b362009-09-25 16:07:19 -07004652 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4653
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004654 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004655 dec_rs_pending(mdev);
4656 drbd_rs_failed_io(mdev, sector, size);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004657 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004658 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01004659
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004660 found = validate_req_change_req_state(mdev, p->block_id, sector,
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004661 &mdev->write_requests, __func__,
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004662 NEG_ACKED, missing_ok);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004663 if (!found) {
4664 /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs.
4665 The master bio might already be completed, therefore the
4666 request is no longer in the collision hash. */
4667 /* In Protocol B we might already have got a P_RECV_ACK
4668 but then get a P_NEG_ACK afterwards. */
4669 if (!missing_ok)
Philipp Reisner2deb8332011-01-17 18:39:18 +01004670 return false;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004671 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004672 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01004673 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004674}
4675
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004676static int got_NegDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004677{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004678 struct drbd_conf *mdev;
4679 struct p_block_ack *p = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004680 sector_t sector = be64_to_cpu(p->sector);
4681
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004682 mdev = vnr_to_mdev(tconn, pi->vnr);
4683 if (!mdev)
4684 return false;
4685
Philipp Reisnerb411b362009-09-25 16:07:19 -07004686 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004687
Philipp Reisnerb411b362009-09-25 16:07:19 -07004688 dev_err(DEV, "Got NegDReply; Sector %llus, len %u; Fail original request.\n",
4689 (unsigned long long)sector, be32_to_cpu(p->blksize));
4690
4691 return validate_req_change_req_state(mdev, p->block_id, sector,
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004692 &mdev->read_requests, __func__,
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004693 NEG_ACKED, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004694}
4695
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004696static int got_NegRSDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004697{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004698 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004699 sector_t sector;
4700 int size;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004701 struct p_block_ack *p = tconn->meta.rbuf;
4702
4703 mdev = vnr_to_mdev(tconn, pi->vnr);
4704 if (!mdev)
4705 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004706
4707 sector = be64_to_cpu(p->sector);
4708 size = be32_to_cpu(p->blksize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004709
4710 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4711
4712 dec_rs_pending(mdev);
4713
4714 if (get_ldev_if_state(mdev, D_FAILED)) {
4715 drbd_rs_complete_io(mdev, sector);
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004716 switch (pi->cmd) {
Philipp Reisnerd612d302010-12-27 10:53:28 +01004717 case P_NEG_RS_DREPLY:
4718 drbd_rs_failed_io(mdev, sector, size);
4719 case P_RS_CANCEL:
4720 break;
4721 default:
4722 D_ASSERT(0);
4723 put_ldev(mdev);
4724 return false;
4725 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004726 put_ldev(mdev);
4727 }
4728
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004729 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004730}
4731
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004732static int got_BarrierAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004733{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004734 struct drbd_conf *mdev;
4735 struct p_barrier_ack *p = tconn->meta.rbuf;
4736
4737 mdev = vnr_to_mdev(tconn, pi->vnr);
4738 if (!mdev)
4739 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004740
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01004741 tl_release(mdev->tconn, p->barrier, be32_to_cpu(p->set_size));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004742
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02004743 if (mdev->state.conn == C_AHEAD &&
4744 atomic_read(&mdev->ap_in_flight) == 0 &&
Philipp Reisner370a43e2011-01-14 16:03:11 +01004745 !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->current_epoch->flags)) {
4746 mdev->start_resync_timer.expires = jiffies + HZ;
4747 add_timer(&mdev->start_resync_timer);
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02004748 }
4749
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004750 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004751}
4752
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004753static int got_OVResult(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004754{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004755 struct drbd_conf *mdev;
4756 struct p_block_ack *p = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004757 struct drbd_work *w;
4758 sector_t sector;
4759 int size;
4760
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004761 mdev = vnr_to_mdev(tconn, pi->vnr);
4762 if (!mdev)
4763 return false;
4764
Philipp Reisnerb411b362009-09-25 16:07:19 -07004765 sector = be64_to_cpu(p->sector);
4766 size = be32_to_cpu(p->blksize);
4767
4768 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4769
4770 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01004771 drbd_ov_out_of_sync_found(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004772 else
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01004773 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004774
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004775 if (!get_ldev(mdev))
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004776 return true;
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004777
Philipp Reisnerb411b362009-09-25 16:07:19 -07004778 drbd_rs_complete_io(mdev, sector);
4779 dec_rs_pending(mdev);
4780
Lars Ellenbergea5442a2010-11-05 09:48:01 +01004781 --mdev->ov_left;
4782
4783 /* let's advance progress step marks only for every other megabyte */
4784 if ((mdev->ov_left & 0x200) == 0x200)
4785 drbd_advance_rs_marks(mdev, mdev->ov_left);
4786
4787 if (mdev->ov_left == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004788 w = kmalloc(sizeof(*w), GFP_NOIO);
4789 if (w) {
4790 w->cb = w_ov_finished;
Philipp Reisnera21e9292011-02-08 15:08:49 +01004791 w->mdev = mdev;
Philipp Reisnere42325a2011-01-19 13:55:45 +01004792 drbd_queue_work_front(&mdev->tconn->data.work, w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004793 } else {
4794 dev_err(DEV, "kmalloc(w) failed.");
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01004795 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004796 drbd_resync_finished(mdev);
4797 }
4798 }
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004799 put_ldev(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004800 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004801}
4802
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004803static int got_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner0ced55a2010-04-30 15:26:20 +02004804{
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004805 return true;
Philipp Reisner0ced55a2010-04-30 15:26:20 +02004806}
4807
Philipp Reisner32862ec2011-02-08 16:41:01 +01004808static int tconn_process_done_ee(struct drbd_tconn *tconn)
4809{
Philipp Reisner082a3432011-03-15 16:05:42 +01004810 struct drbd_conf *mdev;
4811 int i, not_empty = 0;
Philipp Reisner32862ec2011-02-08 16:41:01 +01004812
4813 do {
4814 clear_bit(SIGNAL_ASENDER, &tconn->flags);
4815 flush_signals(current);
Philipp Reisner082a3432011-03-15 16:05:42 +01004816 idr_for_each_entry(&tconn->volumes, mdev, i) {
Andreas Gruenbachere2b30322011-03-16 17:16:12 +01004817 if (drbd_process_done_ee(mdev))
Philipp Reisner082a3432011-03-15 16:05:42 +01004818 return 1; /* error */
4819 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01004820 set_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisner082a3432011-03-15 16:05:42 +01004821
4822 spin_lock_irq(&tconn->req_lock);
4823 idr_for_each_entry(&tconn->volumes, mdev, i) {
4824 not_empty = !list_empty(&mdev->done_ee);
4825 if (not_empty)
4826 break;
4827 }
4828 spin_unlock_irq(&tconn->req_lock);
Philipp Reisner32862ec2011-02-08 16:41:01 +01004829 } while (not_empty);
4830
4831 return 0;
4832}
4833
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004834struct asender_cmd {
4835 size_t pkt_size;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004836 int (*fn)(struct drbd_tconn *tconn, struct packet_info *);
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004837};
4838
4839static struct asender_cmd asender_tbl[] = {
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004840 [P_PING] = { sizeof(struct p_header), got_Ping },
4841 [P_PING_ACK] = { sizeof(struct p_header), got_PingAck },
4842 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4843 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4844 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4845 [P_DISCARD_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
4846 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
4847 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
4848 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply },
4849 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
4850 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
4851 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
4852 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
4853 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
4854 [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply },
4855 [P_CONN_ST_CHG_REPLY]={ sizeof(struct p_req_state_reply), got_conn_RqSReply },
4856 [P_RETRY_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004857};
4858
Philipp Reisnerb411b362009-09-25 16:07:19 -07004859int drbd_asender(struct drbd_thread *thi)
4860{
Philipp Reisner392c8802011-02-09 10:33:31 +01004861 struct drbd_tconn *tconn = thi->tconn;
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01004862 struct p_header *h = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004863 struct asender_cmd *cmd = NULL;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004864 struct packet_info pi;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004865 int rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004866 void *buf = h;
4867 int received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02004868 unsigned int header_size = drbd_header_size(tconn);
4869 int expect = header_size;
Lars Ellenbergf36af182011-03-09 22:44:55 +01004870 int ping_timeout_active = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004871
Philipp Reisnerb411b362009-09-25 16:07:19 -07004872 current->policy = SCHED_RR; /* Make this a realtime task! */
4873 current->rt_priority = 2; /* more important than all other tasks */
4874
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01004875 while (get_t_state(thi) == RUNNING) {
Philipp Reisner80822282011-02-08 12:46:30 +01004876 drbd_thread_current_set_cpu(thi);
Philipp Reisner32862ec2011-02-08 16:41:01 +01004877 if (test_and_clear_bit(SEND_PING, &tconn->flags)) {
Andreas Gruenbachera17647a2011-04-01 12:49:42 +02004878 if (drbd_send_ping(tconn)) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004879 conn_err(tconn, "drbd_send_ping has failed\n");
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01004880 goto reconnect;
4881 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01004882 tconn->meta.socket->sk->sk_rcvtimeo =
4883 tconn->net_conf->ping_timeo*HZ/10;
Lars Ellenbergf36af182011-03-09 22:44:55 +01004884 ping_timeout_active = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004885 }
4886
Philipp Reisner32862ec2011-02-08 16:41:01 +01004887 /* TODO: conditionally cork; it may hurt latency if we cork without
4888 much to send */
4889 if (!tconn->net_conf->no_cork)
4890 drbd_tcp_cork(tconn->meta.socket);
Philipp Reisner082a3432011-03-15 16:05:42 +01004891 if (tconn_process_done_ee(tconn)) {
4892 conn_err(tconn, "tconn_process_done_ee() failed\n");
Philipp Reisner32862ec2011-02-08 16:41:01 +01004893 goto reconnect;
Philipp Reisner082a3432011-03-15 16:05:42 +01004894 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004895 /* but unconditionally uncork unless disabled */
Philipp Reisner32862ec2011-02-08 16:41:01 +01004896 if (!tconn->net_conf->no_cork)
4897 drbd_tcp_uncork(tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004898
4899 /* short circuit, recv_msg would return EINTR anyways. */
4900 if (signal_pending(current))
4901 continue;
4902
Philipp Reisner32862ec2011-02-08 16:41:01 +01004903 rv = drbd_recv_short(tconn->meta.socket, buf, expect-received, 0);
4904 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004905
4906 flush_signals(current);
4907
4908 /* Note:
4909 * -EINTR (on meta) we got a signal
4910 * -EAGAIN (on meta) rcvtimeo expired
4911 * -ECONNRESET other side closed the connection
4912 * -ERESTARTSYS (on data) we got a signal
4913 * rv < 0 other than above: unexpected error!
4914 * rv == expected: full header or command
4915 * rv < expected: "woken" by signal during receive
4916 * rv == 0 : "connection shut down by peer"
4917 */
4918 if (likely(rv > 0)) {
4919 received += rv;
4920 buf += rv;
4921 } else if (rv == 0) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004922 conn_err(tconn, "meta connection shut down by peer.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004923 goto reconnect;
4924 } else if (rv == -EAGAIN) {
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02004925 /* If the data socket received something meanwhile,
4926 * that is good enough: peer is still alive. */
Philipp Reisner32862ec2011-02-08 16:41:01 +01004927 if (time_after(tconn->last_received,
4928 jiffies - tconn->meta.socket->sk->sk_rcvtimeo))
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02004929 continue;
Lars Ellenbergf36af182011-03-09 22:44:55 +01004930 if (ping_timeout_active) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004931 conn_err(tconn, "PingAck did not arrive in time.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004932 goto reconnect;
4933 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01004934 set_bit(SEND_PING, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004935 continue;
4936 } else if (rv == -EINTR) {
4937 continue;
4938 } else {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004939 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004940 goto reconnect;
4941 }
4942
4943 if (received == expect && cmd == NULL) {
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01004944 if (decode_header(tconn, h, &pi))
Philipp Reisnerb411b362009-09-25 16:07:19 -07004945 goto reconnect;
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004946 cmd = &asender_tbl[pi.cmd];
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004947 if (pi.cmd >= ARRAY_SIZE(asender_tbl) || !cmd->fn) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004948 conn_err(tconn, "unknown command %d on meta (l: %d)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004949 pi.cmd, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004950 goto disconnect;
4951 }
4952 expect = cmd->pkt_size;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02004953 if (pi.size != expect - header_size) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004954 conn_err(tconn, "Wrong packet size on meta (c: %d, l: %d)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004955 pi.cmd, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004956 goto reconnect;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004957 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004958 }
4959 if (received == expect) {
Philipp Reisnera4fbda82011-03-16 11:13:17 +01004960 bool rv;
4961
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004962 rv = cmd->fn(tconn, &pi);
4963 if (!rv) {
4964 conn_err(tconn, "%pf failed\n", cmd->fn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004965 goto reconnect;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004966 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004967
Philipp Reisnera4fbda82011-03-16 11:13:17 +01004968 tconn->last_received = jiffies;
4969
Lars Ellenbergf36af182011-03-09 22:44:55 +01004970 /* the idle_timeout (ping-int)
4971 * has been restored in got_PingAck() */
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004972 if (cmd == &asender_tbl[P_PING_ACK])
Lars Ellenbergf36af182011-03-09 22:44:55 +01004973 ping_timeout_active = 0;
4974
Philipp Reisnerb411b362009-09-25 16:07:19 -07004975 buf = h;
4976 received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02004977 expect = header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004978 cmd = NULL;
4979 }
4980 }
4981
4982 if (0) {
4983reconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004984 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004985 }
4986 if (0) {
4987disconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004988 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004989 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01004990 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004991
Philipp Reisner32862ec2011-02-08 16:41:01 +01004992 conn_info(tconn, "asender terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004993
4994 return 0;
4995}