blob: edad6acb1d3c69c9ab41e59f4f54cbcf15f8c1bd [file] [log] [blame]
Philipp Reisnerb411b362009-09-25 16:07:19 -07001/*
2 drbd_receiver.c
3
4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5
6 Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
7 Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8 Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
9
10 drbd is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 drbd is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with drbd; see the file COPYING. If not, write to
22 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25
Philipp Reisnerb411b362009-09-25 16:07:19 -070026#include <linux/module.h>
27
28#include <asm/uaccess.h>
29#include <net/sock.h>
30
Philipp Reisnerb411b362009-09-25 16:07:19 -070031#include <linux/drbd.h>
32#include <linux/fs.h>
33#include <linux/file.h>
34#include <linux/in.h>
35#include <linux/mm.h>
36#include <linux/memcontrol.h>
37#include <linux/mm_inline.h>
38#include <linux/slab.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070039#include <linux/pkt_sched.h>
40#define __KERNEL_SYSCALLS__
41#include <linux/unistd.h>
42#include <linux/vmalloc.h>
43#include <linux/random.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070044#include <linux/string.h>
45#include <linux/scatterlist.h>
46#include "drbd_int.h"
Philipp Reisnerb411b362009-09-25 16:07:19 -070047#include "drbd_req.h"
48
49#include "drbd_vli.h"
50
Philipp Reisner77351055b2011-02-07 17:24:26 +010051struct packet_info {
52 enum drbd_packet cmd;
53 int size;
54 int vnr;
55};
56
Philipp Reisnerb411b362009-09-25 16:07:19 -070057enum finish_epoch {
58 FE_STILL_LIVE,
59 FE_DESTROYED,
60 FE_RECYCLED,
61};
62
Philipp Reisnera4fbda82011-03-16 11:13:17 +010063enum mdev_or_conn {
64 MDEV,
65 CONN,
66};
67
Philipp Reisner65d11ed2011-02-07 17:35:59 +010068static int drbd_do_handshake(struct drbd_tconn *tconn);
Philipp Reisner13e60372011-02-08 09:54:40 +010069static int drbd_do_auth(struct drbd_tconn *tconn);
Philipp Reisner360cc742011-02-08 14:29:53 +010070static int drbd_disconnected(int vnr, void *p, void *data);
Philipp Reisnerb411b362009-09-25 16:07:19 -070071
72static enum finish_epoch drbd_may_finish_epoch(struct drbd_conf *, struct drbd_epoch *, enum epoch_event);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +010073static int e_end_block(struct drbd_work *, int);
Philipp Reisnerb411b362009-09-25 16:07:19 -070074
Philipp Reisnerb411b362009-09-25 16:07:19 -070075
76#define GFP_TRY (__GFP_HIGHMEM | __GFP_NOWARN)
77
Lars Ellenberg45bb9122010-05-14 17:10:48 +020078/*
79 * some helper functions to deal with single linked page lists,
80 * page->private being our "next" pointer.
81 */
82
83/* If at least n pages are linked at head, get n pages off.
84 * Otherwise, don't modify head, and return NULL.
85 * Locking is the responsibility of the caller.
86 */
87static struct page *page_chain_del(struct page **head, int n)
88{
89 struct page *page;
90 struct page *tmp;
91
92 BUG_ON(!n);
93 BUG_ON(!head);
94
95 page = *head;
Philipp Reisner23ce4222010-05-20 13:35:31 +020096
97 if (!page)
98 return NULL;
99
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200100 while (page) {
101 tmp = page_chain_next(page);
102 if (--n == 0)
103 break; /* found sufficient pages */
104 if (tmp == NULL)
105 /* insufficient pages, don't use any of them. */
106 return NULL;
107 page = tmp;
108 }
109
110 /* add end of list marker for the returned list */
111 set_page_private(page, 0);
112 /* actual return value, and adjustment of head */
113 page = *head;
114 *head = tmp;
115 return page;
116}
117
118/* may be used outside of locks to find the tail of a (usually short)
119 * "private" page chain, before adding it back to a global chain head
120 * with page_chain_add() under a spinlock. */
121static struct page *page_chain_tail(struct page *page, int *len)
122{
123 struct page *tmp;
124 int i = 1;
125 while ((tmp = page_chain_next(page)))
126 ++i, page = tmp;
127 if (len)
128 *len = i;
129 return page;
130}
131
132static int page_chain_free(struct page *page)
133{
134 struct page *tmp;
135 int i = 0;
136 page_chain_for_each_safe(page, tmp) {
137 put_page(page);
138 ++i;
139 }
140 return i;
141}
142
143static void page_chain_add(struct page **head,
144 struct page *chain_first, struct page *chain_last)
145{
146#if 1
147 struct page *tmp;
148 tmp = page_chain_tail(chain_first, NULL);
149 BUG_ON(tmp != chain_last);
150#endif
151
152 /* add chain to head */
153 set_page_private(chain_last, (unsigned long)*head);
154 *head = chain_first;
155}
156
157static struct page *drbd_pp_first_pages_or_try_alloc(struct drbd_conf *mdev, int number)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700158{
159 struct page *page = NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200160 struct page *tmp = NULL;
161 int i = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700162
163 /* Yes, testing drbd_pp_vacant outside the lock is racy.
164 * So what. It saves a spin_lock. */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200165 if (drbd_pp_vacant >= number) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700166 spin_lock(&drbd_pp_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200167 page = page_chain_del(&drbd_pp_pool, number);
168 if (page)
169 drbd_pp_vacant -= number;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700170 spin_unlock(&drbd_pp_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200171 if (page)
172 return page;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700173 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200174
Philipp Reisnerb411b362009-09-25 16:07:19 -0700175 /* GFP_TRY, because we must not cause arbitrary write-out: in a DRBD
176 * "criss-cross" setup, that might cause write-out on some other DRBD,
177 * which in turn might block on the other node at this very place. */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200178 for (i = 0; i < number; i++) {
179 tmp = alloc_page(GFP_TRY);
180 if (!tmp)
181 break;
182 set_page_private(tmp, (unsigned long)page);
183 page = tmp;
184 }
185
186 if (i == number)
187 return page;
188
189 /* Not enough pages immediately available this time.
190 * No need to jump around here, drbd_pp_alloc will retry this
191 * function "soon". */
192 if (page) {
193 tmp = page_chain_tail(page, NULL);
194 spin_lock(&drbd_pp_lock);
195 page_chain_add(&drbd_pp_pool, page, tmp);
196 drbd_pp_vacant += i;
197 spin_unlock(&drbd_pp_lock);
198 }
199 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700200}
201
Philipp Reisnerb411b362009-09-25 16:07:19 -0700202static void reclaim_net_ee(struct drbd_conf *mdev, struct list_head *to_be_freed)
203{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100204 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700205 struct list_head *le, *tle;
206
207 /* The EEs are always appended to the end of the list. Since
208 they are sent in order over the wire, they have to finish
209 in order. As soon as we see the first not finished we can
210 stop to examine the list... */
211
212 list_for_each_safe(le, tle, &mdev->net_ee) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100213 peer_req = list_entry(le, struct drbd_peer_request, w.list);
214 if (drbd_ee_has_active_page(peer_req))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700215 break;
216 list_move(le, to_be_freed);
217 }
218}
219
220static void drbd_kick_lo_and_reclaim_net(struct drbd_conf *mdev)
221{
222 LIST_HEAD(reclaimed);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100223 struct drbd_peer_request *peer_req, *t;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700224
Philipp Reisner87eeee42011-01-19 14:16:30 +0100225 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700226 reclaim_net_ee(mdev, &reclaimed);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100227 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700228
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100229 list_for_each_entry_safe(peer_req, t, &reclaimed, w.list)
230 drbd_free_net_ee(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700231}
232
233/**
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200234 * drbd_pp_alloc() - Returns @number pages, retries forever (or until signalled)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700235 * @mdev: DRBD device.
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200236 * @number: number of pages requested
237 * @retry: whether to retry, if not enough pages are available right now
Philipp Reisnerb411b362009-09-25 16:07:19 -0700238 *
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200239 * Tries to allocate number pages, first from our own page pool, then from
240 * the kernel, unless this allocation would exceed the max_buffers setting.
241 * Possibly retry until DRBD frees sufficient pages somewhere else.
242 *
243 * Returns a page chain linked via page->private.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700244 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200245static struct page *drbd_pp_alloc(struct drbd_conf *mdev, unsigned number, bool retry)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700246{
247 struct page *page = NULL;
248 DEFINE_WAIT(wait);
249
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200250 /* Yes, we may run up to @number over max_buffers. If we
251 * follow it strictly, the admin will get it wrong anyways. */
Philipp Reisner89e58e72011-01-19 13:12:45 +0100252 if (atomic_read(&mdev->pp_in_use) < mdev->tconn->net_conf->max_buffers)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200253 page = drbd_pp_first_pages_or_try_alloc(mdev, number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700254
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200255 while (page == NULL) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700256 prepare_to_wait(&drbd_pp_wait, &wait, TASK_INTERRUPTIBLE);
257
258 drbd_kick_lo_and_reclaim_net(mdev);
259
Philipp Reisner89e58e72011-01-19 13:12:45 +0100260 if (atomic_read(&mdev->pp_in_use) < mdev->tconn->net_conf->max_buffers) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200261 page = drbd_pp_first_pages_or_try_alloc(mdev, number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700262 if (page)
263 break;
264 }
265
266 if (!retry)
267 break;
268
269 if (signal_pending(current)) {
270 dev_warn(DEV, "drbd_pp_alloc interrupted!\n");
271 break;
272 }
273
274 schedule();
275 }
276 finish_wait(&drbd_pp_wait, &wait);
277
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200278 if (page)
279 atomic_add(number, &mdev->pp_in_use);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700280 return page;
281}
282
283/* Must not be used from irq, as that may deadlock: see drbd_pp_alloc.
Philipp Reisner87eeee42011-01-19 14:16:30 +0100284 * Is also used from inside an other spin_lock_irq(&mdev->tconn->req_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200285 * Either links the page chain back to the global pool,
286 * or returns all pages to the system. */
Lars Ellenberg435f0742010-09-06 12:30:25 +0200287static void drbd_pp_free(struct drbd_conf *mdev, struct page *page, int is_net)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700288{
Lars Ellenberg435f0742010-09-06 12:30:25 +0200289 atomic_t *a = is_net ? &mdev->pp_in_use_by_net : &mdev->pp_in_use;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700290 int i;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200291
Philipp Reisner81a5d602011-02-22 19:53:16 -0500292 if (drbd_pp_vacant > (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * minor_count)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200293 i = page_chain_free(page);
294 else {
295 struct page *tmp;
296 tmp = page_chain_tail(page, &i);
297 spin_lock(&drbd_pp_lock);
298 page_chain_add(&drbd_pp_pool, page, tmp);
299 drbd_pp_vacant += i;
300 spin_unlock(&drbd_pp_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700301 }
Lars Ellenberg435f0742010-09-06 12:30:25 +0200302 i = atomic_sub_return(i, a);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200303 if (i < 0)
Lars Ellenberg435f0742010-09-06 12:30:25 +0200304 dev_warn(DEV, "ASSERTION FAILED: %s: %d < 0\n",
305 is_net ? "pp_in_use_by_net" : "pp_in_use", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700306 wake_up(&drbd_pp_wait);
307}
308
309/*
310You need to hold the req_lock:
311 _drbd_wait_ee_list_empty()
312
313You must not have the req_lock:
314 drbd_free_ee()
315 drbd_alloc_ee()
316 drbd_init_ee()
317 drbd_release_ee()
318 drbd_ee_fix_bhs()
319 drbd_process_done_ee()
320 drbd_clear_done_ee()
321 drbd_wait_ee_list_empty()
322*/
323
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +0100324struct drbd_peer_request *
325drbd_alloc_ee(struct drbd_conf *mdev, u64 id, sector_t sector,
326 unsigned int data_size, gfp_t gfp_mask) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700327{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100328 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700329 struct page *page;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200330 unsigned nr_pages = (data_size + PAGE_SIZE -1) >> PAGE_SHIFT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700331
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100332 if (drbd_insert_fault(mdev, DRBD_FAULT_AL_EE))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700333 return NULL;
334
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100335 peer_req = mempool_alloc(drbd_ee_mempool, gfp_mask & ~__GFP_HIGHMEM);
336 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700337 if (!(gfp_mask & __GFP_NOWARN))
338 dev_err(DEV, "alloc_ee: Allocation of an EE failed\n");
339 return NULL;
340 }
341
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200342 page = drbd_pp_alloc(mdev, nr_pages, (gfp_mask & __GFP_WAIT));
343 if (!page)
344 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700345
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100346 drbd_clear_interval(&peer_req->i);
347 peer_req->i.size = data_size;
348 peer_req->i.sector = sector;
349 peer_req->i.local = false;
350 peer_req->i.waiting = false;
Andreas Gruenbacher53840642011-01-28 10:31:04 +0100351
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100352 peer_req->epoch = NULL;
Philipp Reisnera21e9292011-02-08 15:08:49 +0100353 peer_req->w.mdev = mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100354 peer_req->pages = page;
355 atomic_set(&peer_req->pending_bios, 0);
356 peer_req->flags = 0;
Andreas Gruenbacher9a8e7752011-01-11 14:04:09 +0100357 /*
358 * The block_id is opaque to the receiver. It is not endianness
359 * converted, and sent back to the sender unchanged.
360 */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100361 peer_req->block_id = id;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700362
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100363 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700364
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200365 fail:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100366 mempool_free(peer_req, drbd_ee_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700367 return NULL;
368}
369
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100370void drbd_free_some_ee(struct drbd_conf *mdev, struct drbd_peer_request *peer_req,
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +0100371 int is_net)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700372{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100373 if (peer_req->flags & EE_HAS_DIGEST)
374 kfree(peer_req->digest);
375 drbd_pp_free(mdev, peer_req->pages, is_net);
376 D_ASSERT(atomic_read(&peer_req->pending_bios) == 0);
377 D_ASSERT(drbd_interval_empty(&peer_req->i));
378 mempool_free(peer_req, drbd_ee_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700379}
380
381int drbd_release_ee(struct drbd_conf *mdev, struct list_head *list)
382{
383 LIST_HEAD(work_list);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100384 struct drbd_peer_request *peer_req, *t;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700385 int count = 0;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200386 int is_net = list == &mdev->net_ee;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700387
Philipp Reisner87eeee42011-01-19 14:16:30 +0100388 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700389 list_splice_init(list, &work_list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100390 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700391
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100392 list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
393 drbd_free_some_ee(mdev, peer_req, is_net);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700394 count++;
395 }
396 return count;
397}
398
399
Philipp Reisner32862ec2011-02-08 16:41:01 +0100400/* See also comments in _req_mod(,BARRIER_ACKED)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700401 * and receive_Barrier.
402 *
403 * Move entries from net_ee to done_ee, if ready.
404 * Grab done_ee, call all callbacks, free the entries.
405 * The callbacks typically send out ACKs.
406 */
407static int drbd_process_done_ee(struct drbd_conf *mdev)
408{
409 LIST_HEAD(work_list);
410 LIST_HEAD(reclaimed);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100411 struct drbd_peer_request *peer_req, *t;
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100412 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700413
Philipp Reisner87eeee42011-01-19 14:16:30 +0100414 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700415 reclaim_net_ee(mdev, &reclaimed);
416 list_splice_init(&mdev->done_ee, &work_list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100417 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700418
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100419 list_for_each_entry_safe(peer_req, t, &reclaimed, w.list)
420 drbd_free_net_ee(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700421
422 /* possible callbacks here:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100423 * e_end_block, and e_end_resync_block, e_send_discard_write.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700424 * all ignore the last argument.
425 */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100426 list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100427 int err2;
428
Philipp Reisnerb411b362009-09-25 16:07:19 -0700429 /* list_del not necessary, next/prev members not touched */
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100430 err2 = peer_req->w.cb(&peer_req->w, !!err);
431 if (!err)
432 err = err2;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100433 drbd_free_ee(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700434 }
435 wake_up(&mdev->ee_wait);
436
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100437 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700438}
439
440void _drbd_wait_ee_list_empty(struct drbd_conf *mdev, struct list_head *head)
441{
442 DEFINE_WAIT(wait);
443
444 /* avoids spin_lock/unlock
445 * and calling prepare_to_wait in the fast path */
446 while (!list_empty(head)) {
447 prepare_to_wait(&mdev->ee_wait, &wait, TASK_UNINTERRUPTIBLE);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100448 spin_unlock_irq(&mdev->tconn->req_lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100449 io_schedule();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700450 finish_wait(&mdev->ee_wait, &wait);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100451 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700452 }
453}
454
455void drbd_wait_ee_list_empty(struct drbd_conf *mdev, struct list_head *head)
456{
Philipp Reisner87eeee42011-01-19 14:16:30 +0100457 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700458 _drbd_wait_ee_list_empty(mdev, head);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100459 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700460}
461
462/* see also kernel_accept; which is only present since 2.6.18.
463 * also we want to log which part of it failed, exactly */
Philipp Reisner76536202011-02-07 14:09:54 +0100464static int drbd_accept(const char **what, struct socket *sock, struct socket **newsock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700465{
466 struct sock *sk = sock->sk;
467 int err = 0;
468
469 *what = "listen";
470 err = sock->ops->listen(sock, 5);
471 if (err < 0)
472 goto out;
473
474 *what = "sock_create_lite";
475 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
476 newsock);
477 if (err < 0)
478 goto out;
479
480 *what = "accept";
481 err = sock->ops->accept(sock, *newsock, 0);
482 if (err < 0) {
483 sock_release(*newsock);
484 *newsock = NULL;
485 goto out;
486 }
487 (*newsock)->ops = sock->ops;
488
489out:
490 return err;
491}
492
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100493static int drbd_recv_short(struct socket *sock, void *buf, size_t size, int flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700494{
495 mm_segment_t oldfs;
496 struct kvec iov = {
497 .iov_base = buf,
498 .iov_len = size,
499 };
500 struct msghdr msg = {
501 .msg_iovlen = 1,
502 .msg_iov = (struct iovec *)&iov,
503 .msg_flags = (flags ? flags : MSG_WAITALL | MSG_NOSIGNAL)
504 };
505 int rv;
506
507 oldfs = get_fs();
508 set_fs(KERNEL_DS);
509 rv = sock_recvmsg(sock, &msg, size, msg.msg_flags);
510 set_fs(oldfs);
511
512 return rv;
513}
514
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100515static int drbd_recv(struct drbd_tconn *tconn, void *buf, size_t size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700516{
517 mm_segment_t oldfs;
518 struct kvec iov = {
519 .iov_base = buf,
520 .iov_len = size,
521 };
522 struct msghdr msg = {
523 .msg_iovlen = 1,
524 .msg_iov = (struct iovec *)&iov,
525 .msg_flags = MSG_WAITALL | MSG_NOSIGNAL
526 };
527 int rv;
528
529 oldfs = get_fs();
530 set_fs(KERNEL_DS);
531
532 for (;;) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100533 rv = sock_recvmsg(tconn->data.socket, &msg, size, msg.msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700534 if (rv == size)
535 break;
536
537 /* Note:
538 * ECONNRESET other side closed the connection
539 * ERESTARTSYS (on sock) we got a signal
540 */
541
542 if (rv < 0) {
543 if (rv == -ECONNRESET)
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100544 conn_info(tconn, "sock was reset by peer\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700545 else if (rv != -ERESTARTSYS)
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100546 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700547 break;
548 } else if (rv == 0) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100549 conn_info(tconn, "sock was shut down by peer\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700550 break;
551 } else {
552 /* signal came in, or peer/link went down,
553 * after we read a partial message
554 */
555 /* D_ASSERT(signal_pending(current)); */
556 break;
557 }
558 };
559
560 set_fs(oldfs);
561
562 if (rv != size)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100563 conn_request_state(tconn, NS(conn, C_BROKEN_PIPE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700564
565 return rv;
566}
567
Andreas Gruenbacherc6967742011-03-17 17:15:20 +0100568static int drbd_recv_all(struct drbd_tconn *tconn, void *buf, size_t size)
569{
570 int err;
571
572 err = drbd_recv(tconn, buf, size);
573 if (err != size) {
574 if (err >= 0)
575 err = -EIO;
576 } else
577 err = 0;
578 return err;
579}
580
Andreas Gruenbachera5c31902011-03-24 03:28:04 +0100581static int drbd_recv_all_warn(struct drbd_tconn *tconn, void *buf, size_t size)
582{
583 int err;
584
585 err = drbd_recv_all(tconn, buf, size);
586 if (err && !signal_pending(current))
587 conn_warn(tconn, "short read (expected size %d)\n", (int)size);
588 return err;
589}
590
Lars Ellenberg5dbf1672010-05-25 16:18:01 +0200591/* quoting tcp(7):
592 * On individual connections, the socket buffer size must be set prior to the
593 * listen(2) or connect(2) calls in order to have it take effect.
594 * This is our wrapper to do so.
595 */
596static void drbd_setbufsize(struct socket *sock, unsigned int snd,
597 unsigned int rcv)
598{
599 /* open coded SO_SNDBUF, SO_RCVBUF */
600 if (snd) {
601 sock->sk->sk_sndbuf = snd;
602 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
603 }
604 if (rcv) {
605 sock->sk->sk_rcvbuf = rcv;
606 sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
607 }
608}
609
Philipp Reisnereac3e992011-02-07 14:05:07 +0100610static struct socket *drbd_try_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700611{
612 const char *what;
613 struct socket *sock;
614 struct sockaddr_in6 src_in6;
615 int err;
616 int disconnect_on_error = 1;
617
Philipp Reisnereac3e992011-02-07 14:05:07 +0100618 if (!get_net_conf(tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700619 return NULL;
620
621 what = "sock_create_kern";
Philipp Reisnereac3e992011-02-07 14:05:07 +0100622 err = sock_create_kern(((struct sockaddr *)tconn->net_conf->my_addr)->sa_family,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700623 SOCK_STREAM, IPPROTO_TCP, &sock);
624 if (err < 0) {
625 sock = NULL;
626 goto out;
627 }
628
629 sock->sk->sk_rcvtimeo =
Philipp Reisnereac3e992011-02-07 14:05:07 +0100630 sock->sk->sk_sndtimeo = tconn->net_conf->try_connect_int*HZ;
631 drbd_setbufsize(sock, tconn->net_conf->sndbuf_size,
632 tconn->net_conf->rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700633
634 /* explicitly bind to the configured IP as source IP
635 * for the outgoing connections.
636 * This is needed for multihomed hosts and to be
637 * able to use lo: interfaces for drbd.
638 * Make sure to use 0 as port number, so linux selects
639 * a free one dynamically.
640 */
Philipp Reisnereac3e992011-02-07 14:05:07 +0100641 memcpy(&src_in6, tconn->net_conf->my_addr,
642 min_t(int, tconn->net_conf->my_addr_len, sizeof(src_in6)));
643 if (((struct sockaddr *)tconn->net_conf->my_addr)->sa_family == AF_INET6)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700644 src_in6.sin6_port = 0;
645 else
646 ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */
647
648 what = "bind before connect";
649 err = sock->ops->bind(sock,
650 (struct sockaddr *) &src_in6,
Philipp Reisnereac3e992011-02-07 14:05:07 +0100651 tconn->net_conf->my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700652 if (err < 0)
653 goto out;
654
655 /* connect may fail, peer not yet available.
656 * stay C_WF_CONNECTION, don't go Disconnecting! */
657 disconnect_on_error = 0;
658 what = "connect";
659 err = sock->ops->connect(sock,
Philipp Reisnereac3e992011-02-07 14:05:07 +0100660 (struct sockaddr *)tconn->net_conf->peer_addr,
661 tconn->net_conf->peer_addr_len, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700662
663out:
664 if (err < 0) {
665 if (sock) {
666 sock_release(sock);
667 sock = NULL;
668 }
669 switch (-err) {
670 /* timeout, busy, signal pending */
671 case ETIMEDOUT: case EAGAIN: case EINPROGRESS:
672 case EINTR: case ERESTARTSYS:
673 /* peer not (yet) available, network problem */
674 case ECONNREFUSED: case ENETUNREACH:
675 case EHOSTDOWN: case EHOSTUNREACH:
676 disconnect_on_error = 0;
677 break;
678 default:
Philipp Reisnereac3e992011-02-07 14:05:07 +0100679 conn_err(tconn, "%s failed, err = %d\n", what, err);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700680 }
681 if (disconnect_on_error)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100682 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700683 }
Philipp Reisnereac3e992011-02-07 14:05:07 +0100684 put_net_conf(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700685 return sock;
686}
687
Philipp Reisner76536202011-02-07 14:09:54 +0100688static struct socket *drbd_wait_for_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700689{
690 int timeo, err;
691 struct socket *s_estab = NULL, *s_listen;
692 const char *what;
693
Philipp Reisner76536202011-02-07 14:09:54 +0100694 if (!get_net_conf(tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700695 return NULL;
696
697 what = "sock_create_kern";
Philipp Reisner76536202011-02-07 14:09:54 +0100698 err = sock_create_kern(((struct sockaddr *)tconn->net_conf->my_addr)->sa_family,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700699 SOCK_STREAM, IPPROTO_TCP, &s_listen);
700 if (err) {
701 s_listen = NULL;
702 goto out;
703 }
704
Philipp Reisner76536202011-02-07 14:09:54 +0100705 timeo = tconn->net_conf->try_connect_int * HZ;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700706 timeo += (random32() & 1) ? timeo / 7 : -timeo / 7; /* 28.5% random jitter */
707
708 s_listen->sk->sk_reuse = 1; /* SO_REUSEADDR */
709 s_listen->sk->sk_rcvtimeo = timeo;
710 s_listen->sk->sk_sndtimeo = timeo;
Philipp Reisner76536202011-02-07 14:09:54 +0100711 drbd_setbufsize(s_listen, tconn->net_conf->sndbuf_size,
712 tconn->net_conf->rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700713
714 what = "bind before listen";
715 err = s_listen->ops->bind(s_listen,
Philipp Reisner76536202011-02-07 14:09:54 +0100716 (struct sockaddr *) tconn->net_conf->my_addr,
717 tconn->net_conf->my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700718 if (err < 0)
719 goto out;
720
Philipp Reisner76536202011-02-07 14:09:54 +0100721 err = drbd_accept(&what, s_listen, &s_estab);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700722
723out:
724 if (s_listen)
725 sock_release(s_listen);
726 if (err < 0) {
727 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
Philipp Reisner76536202011-02-07 14:09:54 +0100728 conn_err(tconn, "%s failed, err = %d\n", what, err);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100729 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700730 }
731 }
Philipp Reisner76536202011-02-07 14:09:54 +0100732 put_net_conf(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700733
734 return s_estab;
735}
736
Philipp Reisnerd38e7872011-02-07 15:32:04 +0100737static int drbd_send_fp(struct drbd_tconn *tconn, struct socket *sock, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700738{
Andreas Gruenbacher5a87d922011-03-24 21:17:52 +0100739 struct p_header *h = tconn->data.sbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700740
Andreas Gruenbacherecf23632011-03-15 23:48:25 +0100741 return !_conn_send_cmd(tconn, 0, sock, cmd, h, sizeof(*h), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700742}
743
Philipp Reisnera25b63f2011-02-07 15:43:45 +0100744static enum drbd_packet drbd_recv_fp(struct drbd_tconn *tconn, struct socket *sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700745{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +0100746 struct p_header80 *h = tconn->data.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700747 int rr;
748
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100749 rr = drbd_recv_short(sock, h, sizeof(*h), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700750
Andreas Gruenbacherca9bc122011-01-11 13:47:24 +0100751 if (rr == sizeof(*h) && h->magic == cpu_to_be32(DRBD_MAGIC))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700752 return be16_to_cpu(h->command);
753
754 return 0xffff;
755}
756
757/**
758 * drbd_socket_okay() - Free the socket if its connection is not okay
Philipp Reisnerb411b362009-09-25 16:07:19 -0700759 * @sock: pointer to the pointer to the socket.
760 */
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100761static int drbd_socket_okay(struct socket **sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700762{
763 int rr;
764 char tb[4];
765
766 if (!*sock)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100767 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700768
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100769 rr = drbd_recv_short(*sock, tb, 4, MSG_DONTWAIT | MSG_PEEK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700770
771 if (rr > 0 || rr == -EAGAIN) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100772 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700773 } else {
774 sock_release(*sock);
775 *sock = NULL;
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100776 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700777 }
778}
Philipp Reisner2325eb62011-03-15 16:56:18 +0100779/* Gets called if a connection is established, or if a new minor gets created
780 in a connection */
781int drbd_connected(int vnr, void *p, void *data)
Philipp Reisner907599e2011-02-08 11:25:37 +0100782{
783 struct drbd_conf *mdev = (struct drbd_conf *)p;
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100784 int err;
Philipp Reisner907599e2011-02-08 11:25:37 +0100785
786 atomic_set(&mdev->packet_seq, 0);
787 mdev->peer_seq = 0;
788
Philipp Reisner8410da82011-02-11 20:11:10 +0100789 mdev->state_mutex = mdev->tconn->agreed_pro_version < 100 ?
790 &mdev->tconn->cstate_mutex :
791 &mdev->own_state_mutex;
792
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100793 err = drbd_send_sync_param(mdev);
794 if (!err)
795 err = drbd_send_sizes(mdev, 0, 0);
796 if (!err)
797 err = drbd_send_uuids(mdev);
798 if (!err)
799 err = drbd_send_state(mdev);
Philipp Reisner907599e2011-02-08 11:25:37 +0100800 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
801 clear_bit(RESIZE_PENDING, &mdev->flags);
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100802 return err;
Philipp Reisner907599e2011-02-08 11:25:37 +0100803}
804
Philipp Reisnerb411b362009-09-25 16:07:19 -0700805/*
806 * return values:
807 * 1 yes, we have a valid connection
808 * 0 oops, did not work out, please try again
809 * -1 peer talks different language,
810 * no point in trying again, please go standalone.
811 * -2 We do not have a network config...
812 */
Philipp Reisner907599e2011-02-08 11:25:37 +0100813static int drbd_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700814{
815 struct socket *s, *sock, *msock;
816 int try, h, ok;
817
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100818 if (conn_request_state(tconn, NS(conn, C_WF_CONNECTION), CS_VERBOSE) < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700819 return -2;
820
Philipp Reisner907599e2011-02-08 11:25:37 +0100821 clear_bit(DISCARD_CONCURRENT, &tconn->flags);
Andreas Gruenbacher0916e0e2011-03-21 14:10:15 +0100822
823 /* Assume that the peer only understands protocol 80 until we know better. */
824 tconn->agreed_pro_version = 80;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700825
826 sock = NULL;
827 msock = NULL;
828
829 do {
830 for (try = 0;;) {
831 /* 3 tries, this should take less than a second! */
Philipp Reisner907599e2011-02-08 11:25:37 +0100832 s = drbd_try_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700833 if (s || ++try >= 3)
834 break;
835 /* give the other side time to call bind() & listen() */
Philipp Reisner20ee6392011-01-18 15:28:59 +0100836 schedule_timeout_interruptible(HZ / 10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700837 }
838
839 if (s) {
840 if (!sock) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100841 drbd_send_fp(tconn, s, P_HAND_SHAKE_S);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700842 sock = s;
843 s = NULL;
844 } else if (!msock) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100845 drbd_send_fp(tconn, s, P_HAND_SHAKE_M);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700846 msock = s;
847 s = NULL;
848 } else {
Philipp Reisner907599e2011-02-08 11:25:37 +0100849 conn_err(tconn, "Logic error in drbd_connect()\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700850 goto out_release_sockets;
851 }
852 }
853
854 if (sock && msock) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100855 schedule_timeout_interruptible(tconn->net_conf->ping_timeo*HZ/10);
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100856 ok = drbd_socket_okay(&sock);
857 ok = drbd_socket_okay(&msock) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700858 if (ok)
859 break;
860 }
861
862retry:
Philipp Reisner907599e2011-02-08 11:25:37 +0100863 s = drbd_wait_for_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700864 if (s) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100865 try = drbd_recv_fp(tconn, s);
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100866 drbd_socket_okay(&sock);
867 drbd_socket_okay(&msock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700868 switch (try) {
869 case P_HAND_SHAKE_S:
870 if (sock) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100871 conn_warn(tconn, "initial packet S crossed\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700872 sock_release(sock);
873 }
874 sock = s;
875 break;
876 case P_HAND_SHAKE_M:
877 if (msock) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100878 conn_warn(tconn, "initial packet M crossed\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700879 sock_release(msock);
880 }
881 msock = s;
Philipp Reisner907599e2011-02-08 11:25:37 +0100882 set_bit(DISCARD_CONCURRENT, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700883 break;
884 default:
Philipp Reisner907599e2011-02-08 11:25:37 +0100885 conn_warn(tconn, "Error receiving initial packet\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700886 sock_release(s);
887 if (random32() & 1)
888 goto retry;
889 }
890 }
891
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100892 if (tconn->cstate <= C_DISCONNECTING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700893 goto out_release_sockets;
894 if (signal_pending(current)) {
895 flush_signals(current);
896 smp_rmb();
Philipp Reisner907599e2011-02-08 11:25:37 +0100897 if (get_t_state(&tconn->receiver) == EXITING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700898 goto out_release_sockets;
899 }
900
901 if (sock && msock) {
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100902 ok = drbd_socket_okay(&sock);
903 ok = drbd_socket_okay(&msock) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700904 if (ok)
905 break;
906 }
907 } while (1);
908
909 msock->sk->sk_reuse = 1; /* SO_REUSEADDR */
910 sock->sk->sk_reuse = 1; /* SO_REUSEADDR */
911
912 sock->sk->sk_allocation = GFP_NOIO;
913 msock->sk->sk_allocation = GFP_NOIO;
914
915 sock->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
916 msock->sk->sk_priority = TC_PRIO_INTERACTIVE;
917
Philipp Reisnerb411b362009-09-25 16:07:19 -0700918 /* NOT YET ...
Philipp Reisner907599e2011-02-08 11:25:37 +0100919 * sock->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700920 * sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
921 * first set it to the P_HAND_SHAKE timeout,
922 * which we set to 4x the configured ping_timeout. */
923 sock->sk->sk_sndtimeo =
Philipp Reisner907599e2011-02-08 11:25:37 +0100924 sock->sk->sk_rcvtimeo = tconn->net_conf->ping_timeo*4*HZ/10;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700925
Philipp Reisner907599e2011-02-08 11:25:37 +0100926 msock->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
927 msock->sk->sk_rcvtimeo = tconn->net_conf->ping_int*HZ;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700928
929 /* we don't want delays.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300930 * we use TCP_CORK where appropriate, though */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700931 drbd_tcp_nodelay(sock);
932 drbd_tcp_nodelay(msock);
933
Philipp Reisner907599e2011-02-08 11:25:37 +0100934 tconn->data.socket = sock;
935 tconn->meta.socket = msock;
936 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700937
Philipp Reisner907599e2011-02-08 11:25:37 +0100938 h = drbd_do_handshake(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700939 if (h <= 0)
940 return h;
941
Philipp Reisner907599e2011-02-08 11:25:37 +0100942 if (tconn->cram_hmac_tfm) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700943 /* drbd_request_state(mdev, NS(conn, WFAuth)); */
Philipp Reisner907599e2011-02-08 11:25:37 +0100944 switch (drbd_do_auth(tconn)) {
Johannes Thomab10d96c2010-01-07 16:02:50 +0100945 case -1:
Philipp Reisner907599e2011-02-08 11:25:37 +0100946 conn_err(tconn, "Authentication of peer failed\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700947 return -1;
Johannes Thomab10d96c2010-01-07 16:02:50 +0100948 case 0:
Philipp Reisner907599e2011-02-08 11:25:37 +0100949 conn_err(tconn, "Authentication of peer failed, trying again.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +0100950 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700951 }
952 }
953
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100954 if (conn_request_state(tconn, NS(conn, C_WF_REPORT_PARAMS), CS_VERBOSE) < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700955 return 0;
956
Philipp Reisner907599e2011-02-08 11:25:37 +0100957 sock->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700958 sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
959
Philipp Reisner907599e2011-02-08 11:25:37 +0100960 drbd_thread_start(&tconn->asender);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700961
Andreas Gruenbacher387eb302011-03-16 01:05:37 +0100962 if (drbd_send_protocol(tconn) == -EOPNOTSUPP)
Philipp Reisner7e2455c2010-04-22 14:50:23 +0200963 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700964
Philipp Reisner907599e2011-02-08 11:25:37 +0100965 return !idr_for_each(&tconn->volumes, drbd_connected, tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700966
967out_release_sockets:
968 if (sock)
969 sock_release(sock);
970 if (msock)
971 sock_release(msock);
972 return -1;
973}
974
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +0100975static int decode_header(struct drbd_tconn *tconn, struct p_header *h, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700976{
Philipp Reisnerfd340c12011-01-19 16:57:39 +0100977 if (h->h80.magic == cpu_to_be32(DRBD_MAGIC)) {
Philipp Reisner77351055b2011-02-07 17:24:26 +0100978 pi->cmd = be16_to_cpu(h->h80.command);
979 pi->size = be16_to_cpu(h->h80.length);
Philipp Reisnereefc2f72011-02-08 12:55:24 +0100980 pi->vnr = 0;
Andreas Gruenbacherca9bc122011-01-11 13:47:24 +0100981 } else if (h->h95.magic == cpu_to_be16(DRBD_MAGIC_BIG)) {
Philipp Reisner77351055b2011-02-07 17:24:26 +0100982 pi->cmd = be16_to_cpu(h->h95.command);
983 pi->size = be32_to_cpu(h->h95.length) & 0x00ffffff;
984 pi->vnr = 0;
Philipp Reisner02918be2010-08-20 14:35:10 +0200985 } else {
Philipp Reisnerce243852011-02-07 17:27:47 +0100986 conn_err(tconn, "magic?? on data m: 0x%08x c: %d l: %d\n",
Lars Ellenberg004352f2010-10-05 20:13:58 +0200987 be32_to_cpu(h->h80.magic),
988 be16_to_cpu(h->h80.command),
989 be16_to_cpu(h->h80.length));
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +0100990 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700991 }
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +0100992 return 0;
Philipp Reisner257d0af2011-01-26 12:15:29 +0100993}
994
Philipp Reisner9ba7aa02011-02-07 17:32:41 +0100995static int drbd_recv_header(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner257d0af2011-01-26 12:15:29 +0100996{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +0100997 struct p_header *h = tconn->data.rbuf;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +0100998 int err;
Philipp Reisner257d0af2011-01-26 12:15:29 +0100999
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001000 err = drbd_recv_all_warn(tconn, h, sizeof(*h));
1001 if (err)
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001002 return err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001003
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001004 err = decode_header(tconn, h, pi);
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001005 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001006
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001007 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001008}
1009
Philipp Reisner2451fc32010-08-24 13:43:11 +02001010static void drbd_flush(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001011{
1012 int rv;
1013
1014 if (mdev->write_ordering >= WO_bdev_flush && get_ldev(mdev)) {
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +04001015 rv = blkdev_issue_flush(mdev->ldev->backing_bdev, GFP_KERNEL,
Christoph Hellwigdd3932e2010-09-16 20:51:46 +02001016 NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001017 if (rv) {
1018 dev_err(DEV, "local disk flush failed with status %d\n", rv);
1019 /* would rather check on EOPNOTSUPP, but that is not reliable.
1020 * don't try again for ANY return value != 0
1021 * if (rv == -EOPNOTSUPP) */
1022 drbd_bump_write_ordering(mdev, WO_drain_io);
1023 }
1024 put_ldev(mdev);
1025 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001026}
1027
1028/**
1029 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
1030 * @mdev: DRBD device.
1031 * @epoch: Epoch object.
1032 * @ev: Epoch event.
1033 */
1034static enum finish_epoch drbd_may_finish_epoch(struct drbd_conf *mdev,
1035 struct drbd_epoch *epoch,
1036 enum epoch_event ev)
1037{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001038 int epoch_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001039 struct drbd_epoch *next_epoch;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001040 enum finish_epoch rv = FE_STILL_LIVE;
1041
1042 spin_lock(&mdev->epoch_lock);
1043 do {
1044 next_epoch = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001045
1046 epoch_size = atomic_read(&epoch->epoch_size);
1047
1048 switch (ev & ~EV_CLEANUP) {
1049 case EV_PUT:
1050 atomic_dec(&epoch->active);
1051 break;
1052 case EV_GOT_BARRIER_NR:
1053 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001054 break;
1055 case EV_BECAME_LAST:
1056 /* nothing to do*/
1057 break;
1058 }
1059
Philipp Reisnerb411b362009-09-25 16:07:19 -07001060 if (epoch_size != 0 &&
1061 atomic_read(&epoch->active) == 0 &&
Philipp Reisner2451fc32010-08-24 13:43:11 +02001062 test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001063 if (!(ev & EV_CLEANUP)) {
1064 spin_unlock(&mdev->epoch_lock);
1065 drbd_send_b_ack(mdev, epoch->barrier_nr, epoch_size);
1066 spin_lock(&mdev->epoch_lock);
1067 }
1068 dec_unacked(mdev);
1069
1070 if (mdev->current_epoch != epoch) {
1071 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1072 list_del(&epoch->list);
1073 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
1074 mdev->epochs--;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001075 kfree(epoch);
1076
1077 if (rv == FE_STILL_LIVE)
1078 rv = FE_DESTROYED;
1079 } else {
1080 epoch->flags = 0;
1081 atomic_set(&epoch->epoch_size, 0);
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001082 /* atomic_set(&epoch->active, 0); is already zero */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001083 if (rv == FE_STILL_LIVE)
1084 rv = FE_RECYCLED;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001085 wake_up(&mdev->ee_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001086 }
1087 }
1088
1089 if (!next_epoch)
1090 break;
1091
1092 epoch = next_epoch;
1093 } while (1);
1094
1095 spin_unlock(&mdev->epoch_lock);
1096
Philipp Reisnerb411b362009-09-25 16:07:19 -07001097 return rv;
1098}
1099
1100/**
1101 * drbd_bump_write_ordering() - Fall back to an other write ordering method
1102 * @mdev: DRBD device.
1103 * @wo: Write ordering method to try.
1104 */
1105void drbd_bump_write_ordering(struct drbd_conf *mdev, enum write_ordering_e wo) __must_hold(local)
1106{
1107 enum write_ordering_e pwo;
1108 static char *write_ordering_str[] = {
1109 [WO_none] = "none",
1110 [WO_drain_io] = "drain",
1111 [WO_bdev_flush] = "flush",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001112 };
1113
1114 pwo = mdev->write_ordering;
1115 wo = min(pwo, wo);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001116 if (wo == WO_bdev_flush && mdev->ldev->dc.no_disk_flush)
1117 wo = WO_drain_io;
1118 if (wo == WO_drain_io && mdev->ldev->dc.no_disk_drain)
1119 wo = WO_none;
1120 mdev->write_ordering = wo;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001121 if (pwo != mdev->write_ordering || wo == WO_bdev_flush)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001122 dev_info(DEV, "Method to ensure write ordering: %s\n", write_ordering_str[mdev->write_ordering]);
1123}
1124
1125/**
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001126 * drbd_submit_peer_request()
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001127 * @mdev: DRBD device.
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001128 * @peer_req: peer request
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001129 * @rw: flag field, see bio->bi_rw
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001130 *
1131 * May spread the pages to multiple bios,
1132 * depending on bio_add_page restrictions.
1133 *
1134 * Returns 0 if all bios have been submitted,
1135 * -ENOMEM if we could not allocate enough bios,
1136 * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a
1137 * single page to an empty bio (which should never happen and likely indicates
1138 * that the lower level IO stack is in some way broken). This has been observed
1139 * on certain Xen deployments.
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001140 */
1141/* TODO allocate from our own bio_set. */
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001142int drbd_submit_peer_request(struct drbd_conf *mdev,
1143 struct drbd_peer_request *peer_req,
1144 const unsigned rw, const int fault_type)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001145{
1146 struct bio *bios = NULL;
1147 struct bio *bio;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001148 struct page *page = peer_req->pages;
1149 sector_t sector = peer_req->i.sector;
1150 unsigned ds = peer_req->i.size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001151 unsigned n_bios = 0;
1152 unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001153 int err = -ENOMEM;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001154
1155 /* In most cases, we will only need one bio. But in case the lower
1156 * level restrictions happen to be different at this offset on this
1157 * side than those of the sending peer, we may need to submit the
Lars Ellenbergda4a75d2011-02-23 17:02:01 +01001158 * request in more than one bio.
1159 *
1160 * Plain bio_alloc is good enough here, this is no DRBD internally
1161 * generated bio, but a bio allocated on behalf of the peer.
1162 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001163next_bio:
1164 bio = bio_alloc(GFP_NOIO, nr_pages);
1165 if (!bio) {
1166 dev_err(DEV, "submit_ee: Allocation of a bio failed\n");
1167 goto fail;
1168 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001169 /* > peer_req->i.sector, unless this is the first bio */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001170 bio->bi_sector = sector;
1171 bio->bi_bdev = mdev->ldev->backing_bdev;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001172 bio->bi_rw = rw;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001173 bio->bi_private = peer_req;
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001174 bio->bi_end_io = drbd_peer_request_endio;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001175
1176 bio->bi_next = bios;
1177 bios = bio;
1178 ++n_bios;
1179
1180 page_chain_for_each(page) {
1181 unsigned len = min_t(unsigned, ds, PAGE_SIZE);
1182 if (!bio_add_page(bio, page, len, 0)) {
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001183 /* A single page must always be possible!
1184 * But in case it fails anyways,
1185 * we deal with it, and complain (below). */
1186 if (bio->bi_vcnt == 0) {
1187 dev_err(DEV,
1188 "bio_add_page failed for len=%u, "
1189 "bi_vcnt=0 (bi_sector=%llu)\n",
1190 len, (unsigned long long)bio->bi_sector);
1191 err = -ENOSPC;
1192 goto fail;
1193 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001194 goto next_bio;
1195 }
1196 ds -= len;
1197 sector += len >> 9;
1198 --nr_pages;
1199 }
1200 D_ASSERT(page == NULL);
1201 D_ASSERT(ds == 0);
1202
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001203 atomic_set(&peer_req->pending_bios, n_bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001204 do {
1205 bio = bios;
1206 bios = bios->bi_next;
1207 bio->bi_next = NULL;
1208
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001209 drbd_generic_make_request(mdev, fault_type, bio);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001210 } while (bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001211 return 0;
1212
1213fail:
1214 while (bios) {
1215 bio = bios;
1216 bios = bios->bi_next;
1217 bio_put(bio);
1218 }
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001219 return err;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001220}
1221
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001222static void drbd_remove_epoch_entry_interval(struct drbd_conf *mdev,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001223 struct drbd_peer_request *peer_req)
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001224{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001225 struct drbd_interval *i = &peer_req->i;
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001226
1227 drbd_remove_interval(&mdev->write_requests, i);
1228 drbd_clear_interval(i);
1229
Andreas Gruenbacher6c852be2011-02-04 15:38:52 +01001230 /* Wake up any processes waiting for this peer request to complete. */
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001231 if (i->waiting)
1232 wake_up(&mdev->misc_wait);
1233}
1234
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001235static int receive_Barrier(struct drbd_conf *mdev, enum drbd_packet cmd,
1236 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001237{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001238 int rv;
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01001239 struct p_barrier *p = mdev->tconn->data.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001240 struct drbd_epoch *epoch;
1241
Philipp Reisnerb411b362009-09-25 16:07:19 -07001242 inc_unacked(mdev);
1243
Philipp Reisnerb411b362009-09-25 16:07:19 -07001244 mdev->current_epoch->barrier_nr = p->barrier;
1245 rv = drbd_may_finish_epoch(mdev, mdev->current_epoch, EV_GOT_BARRIER_NR);
1246
1247 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
1248 * the activity log, which means it would not be resynced in case the
1249 * R_PRIMARY crashes now.
1250 * Therefore we must send the barrier_ack after the barrier request was
1251 * completed. */
1252 switch (mdev->write_ordering) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001253 case WO_none:
1254 if (rv == FE_RECYCLED)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001255 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001256
1257 /* receiver context, in the writeout path of the other node.
1258 * avoid potential distributed deadlock */
1259 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1260 if (epoch)
1261 break;
1262 else
1263 dev_warn(DEV, "Allocation of an epoch failed, slowing down\n");
1264 /* Fall through */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001265
1266 case WO_bdev_flush:
1267 case WO_drain_io:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001268 drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
Philipp Reisner2451fc32010-08-24 13:43:11 +02001269 drbd_flush(mdev);
1270
1271 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1272 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1273 if (epoch)
1274 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001275 }
1276
Philipp Reisner2451fc32010-08-24 13:43:11 +02001277 epoch = mdev->current_epoch;
1278 wait_event(mdev->ee_wait, atomic_read(&epoch->epoch_size) == 0);
1279
1280 D_ASSERT(atomic_read(&epoch->active) == 0);
1281 D_ASSERT(epoch->flags == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001282
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001283 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001284 default:
1285 dev_err(DEV, "Strangeness in mdev->write_ordering %d\n", mdev->write_ordering);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001286 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001287 }
1288
1289 epoch->flags = 0;
1290 atomic_set(&epoch->epoch_size, 0);
1291 atomic_set(&epoch->active, 0);
1292
1293 spin_lock(&mdev->epoch_lock);
1294 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1295 list_add(&epoch->list, &mdev->current_epoch->list);
1296 mdev->current_epoch = epoch;
1297 mdev->epochs++;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001298 } else {
1299 /* The current_epoch got recycled while we allocated this one... */
1300 kfree(epoch);
1301 }
1302 spin_unlock(&mdev->epoch_lock);
1303
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001304 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001305}
1306
1307/* used from receive_RSDataReply (recv_resync_read)
1308 * and from receive_Data */
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001309static struct drbd_peer_request *
1310read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector,
1311 int data_size) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001312{
Lars Ellenberg66660322010-04-06 12:15:04 +02001313 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001314 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001315 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001316 int dgs, ds, err;
Philipp Reisnera0638452011-01-19 14:31:32 +01001317 void *dig_in = mdev->tconn->int_dig_in;
1318 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001319 unsigned long *data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001320
Philipp Reisnera0638452011-01-19 14:31:32 +01001321 dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_r_tfm) ?
1322 crypto_hash_digestsize(mdev->tconn->integrity_r_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001323
1324 if (dgs) {
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001325 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1326 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001327 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001328 }
1329
1330 data_size -= dgs;
1331
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001332 if (!expect(data_size != 0))
1333 return NULL;
1334 if (!expect(IS_ALIGNED(data_size, 512)))
1335 return NULL;
1336 if (!expect(data_size <= DRBD_MAX_BIO_SIZE))
1337 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001338
Lars Ellenberg66660322010-04-06 12:15:04 +02001339 /* even though we trust out peer,
1340 * we sometimes have to double check. */
1341 if (sector + (data_size>>9) > capacity) {
Lars Ellenbergfdda6542011-01-24 15:11:01 +01001342 dev_err(DEV, "request from peer beyond end of local disk: "
1343 "capacity: %llus < sector: %llus + size: %u\n",
Lars Ellenberg66660322010-04-06 12:15:04 +02001344 (unsigned long long)capacity,
1345 (unsigned long long)sector, data_size);
1346 return NULL;
1347 }
1348
Philipp Reisnerb411b362009-09-25 16:07:19 -07001349 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1350 * "criss-cross" setup, that might cause write-out on some other DRBD,
1351 * which in turn might block on the other node at this very place. */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001352 peer_req = drbd_alloc_ee(mdev, id, sector, data_size, GFP_NOIO);
1353 if (!peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001354 return NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001355
Philipp Reisnerb411b362009-09-25 16:07:19 -07001356 ds = data_size;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001357 page = peer_req->pages;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001358 page_chain_for_each(page) {
1359 unsigned len = min_t(int, ds, PAGE_SIZE);
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001360 data = kmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001361 err = drbd_recv_all_warn(mdev->tconn, data, len);
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +01001362 if (drbd_insert_fault(mdev, DRBD_FAULT_RECEIVE)) {
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001363 dev_err(DEV, "Fault injection: Corrupting data on receive\n");
1364 data[0] = data[0] ^ (unsigned long)-1;
1365 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001366 kunmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001367 if (err) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001368 drbd_free_ee(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001369 return NULL;
1370 }
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001371 ds -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001372 }
1373
1374 if (dgs) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001375 drbd_csum_ee(mdev, mdev->tconn->integrity_r_tfm, peer_req, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001376 if (memcmp(dig_in, dig_vv, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001377 dev_err(DEV, "Digest integrity check FAILED: %llus +%u\n",
1378 (unsigned long long)sector, data_size);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001379 drbd_free_ee(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001380 return NULL;
1381 }
1382 }
1383 mdev->recv_cnt += data_size>>9;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001384 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001385}
1386
1387/* drbd_drain_block() just takes a data block
1388 * out of the socket input buffer, and discards it.
1389 */
1390static int drbd_drain_block(struct drbd_conf *mdev, int data_size)
1391{
1392 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001393 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001394 void *data;
1395
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001396 if (!data_size)
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001397 return 0;
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001398
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001399 page = drbd_pp_alloc(mdev, 1, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001400
1401 data = kmap(page);
1402 while (data_size) {
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001403 unsigned int len = min_t(int, data_size, PAGE_SIZE);
1404
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001405 err = drbd_recv_all_warn(mdev->tconn, data, len);
1406 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001407 break;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001408 data_size -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001409 }
1410 kunmap(page);
Lars Ellenberg435f0742010-09-06 12:30:25 +02001411 drbd_pp_free(mdev, page, 0);
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001412 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001413}
1414
1415static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req,
1416 sector_t sector, int data_size)
1417{
1418 struct bio_vec *bvec;
1419 struct bio *bio;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001420 int dgs, err, i, expect;
Philipp Reisnera0638452011-01-19 14:31:32 +01001421 void *dig_in = mdev->tconn->int_dig_in;
1422 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001423
Philipp Reisnera0638452011-01-19 14:31:32 +01001424 dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_r_tfm) ?
1425 crypto_hash_digestsize(mdev->tconn->integrity_r_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001426
1427 if (dgs) {
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001428 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1429 if (err)
1430 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001431 }
1432
1433 data_size -= dgs;
1434
1435 /* optimistically update recv_cnt. if receiving fails below,
1436 * we disconnect anyways, and counters will be reset. */
1437 mdev->recv_cnt += data_size>>9;
1438
1439 bio = req->master_bio;
1440 D_ASSERT(sector == bio->bi_sector);
1441
1442 bio_for_each_segment(bvec, bio, i) {
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001443 void *mapped = kmap(bvec->bv_page) + bvec->bv_offset;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001444 expect = min_t(int, data_size, bvec->bv_len);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001445 err = drbd_recv_all_warn(mdev->tconn, mapped, expect);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001446 kunmap(bvec->bv_page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001447 if (err)
1448 return err;
1449 data_size -= expect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001450 }
1451
1452 if (dgs) {
Philipp Reisnera0638452011-01-19 14:31:32 +01001453 drbd_csum_bio(mdev, mdev->tconn->integrity_r_tfm, bio, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001454 if (memcmp(dig_in, dig_vv, dgs)) {
1455 dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n");
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001456 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001457 }
1458 }
1459
1460 D_ASSERT(data_size == 0);
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001461 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001462}
1463
1464/* e_end_resync_block() is called via
1465 * drbd_process_done_ee() by asender only */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001466static int e_end_resync_block(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001467{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001468 struct drbd_peer_request *peer_req =
1469 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001470 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001471 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001472 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001473
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001474 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001475
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001476 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1477 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001478 err = drbd_send_ack(mdev, P_RS_WRITE_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001479 } else {
1480 /* Record failure to sync */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001481 drbd_rs_failed_io(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001482
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001483 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001484 }
1485 dec_unacked(mdev);
1486
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001487 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001488}
1489
1490static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local)
1491{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001492 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001493
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001494 peer_req = read_in_block(mdev, ID_SYNCER, sector, data_size);
1495 if (!peer_req)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001496 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001497
1498 dec_rs_pending(mdev);
1499
Philipp Reisnerb411b362009-09-25 16:07:19 -07001500 inc_unacked(mdev);
1501 /* corresponding dec_unacked() in e_end_resync_block()
1502 * respective _drbd_clear_done_ee */
1503
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001504 peer_req->w.cb = e_end_resync_block;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001505
Philipp Reisner87eeee42011-01-19 14:16:30 +01001506 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001507 list_add(&peer_req->w.list, &mdev->sync_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001508 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001509
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001510 atomic_add(data_size >> 9, &mdev->rs_sect_ev);
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001511 if (drbd_submit_peer_request(mdev, peer_req, WRITE, DRBD_FAULT_RS_WR) == 0)
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001512 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001513
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001514 /* don't care for the reason here */
1515 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01001516 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001517 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001518 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001519
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001520 drbd_free_ee(mdev, peer_req);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001521fail:
1522 put_ldev(mdev);
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001523 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001524}
1525
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001526static struct drbd_request *
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001527find_request(struct drbd_conf *mdev, struct rb_root *root, u64 id,
1528 sector_t sector, bool missing_ok, const char *func)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001529{
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001530 struct drbd_request *req;
1531
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001532 /* Request object according to our peer */
1533 req = (struct drbd_request *)(unsigned long)id;
Andreas Gruenbacher5e472262011-01-27 14:42:51 +01001534 if (drbd_contains_interval(root, sector, &req->i) && req->i.local)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001535 return req;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001536 if (!missing_ok) {
1537 dev_err(DEV, "%s: failed to find request %lu, sector %llus\n", func,
1538 (unsigned long)id, (unsigned long long)sector);
1539 }
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001540 return NULL;
1541}
1542
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001543static int receive_DataReply(struct drbd_conf *mdev, enum drbd_packet cmd,
1544 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001545{
1546 struct drbd_request *req;
1547 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001548 int err;
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01001549 struct p_data *p = mdev->tconn->data.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001550
1551 sector = be64_to_cpu(p->sector);
1552
Philipp Reisner87eeee42011-01-19 14:16:30 +01001553 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001554 req = find_request(mdev, &mdev->read_requests, p->block_id, sector, false, __func__);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001555 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001556 if (unlikely(!req))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001557 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001558
Bart Van Assche24c48302011-05-21 18:32:29 +02001559 /* hlist_del(&req->collision) is done in _req_may_be_done, to avoid
Philipp Reisnerb411b362009-09-25 16:07:19 -07001560 * special casing it there for the various failure cases.
1561 * still no race with drbd_fail_pending_reads */
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001562 err = recv_dless_read(mdev, req, sector, data_size);
1563 if (!err)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001564 req_mod(req, DATA_RECEIVED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001565 /* else: nothing. handled from drbd_disconnect...
1566 * I don't think we may complete this just yet
1567 * in case we are "on-disconnect: freeze" */
1568
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001569 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001570}
1571
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001572static int receive_RSDataReply(struct drbd_conf *mdev, enum drbd_packet cmd,
1573 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001574{
1575 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001576 int err;
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01001577 struct p_data *p = mdev->tconn->data.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001578
1579 sector = be64_to_cpu(p->sector);
1580 D_ASSERT(p->block_id == ID_SYNCER);
1581
1582 if (get_ldev(mdev)) {
1583 /* data is submitted to disk within recv_resync_read.
1584 * corresponding put_ldev done below on error,
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001585 * or in drbd_peer_request_endio. */
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001586 err = recv_resync_read(mdev, sector, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001587 } else {
1588 if (__ratelimit(&drbd_ratelimit_state))
1589 dev_err(DEV, "Can not write resync data to local disk.\n");
1590
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001591 err = drbd_drain_block(mdev, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001592
Lars Ellenberg2b2bf212010-10-06 11:46:55 +02001593 drbd_send_ack_dp(mdev, P_NEG_ACK, p, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001594 }
1595
Philipp Reisner778f2712010-07-06 11:14:00 +02001596 atomic_add(data_size >> 9, &mdev->rs_sect_in);
1597
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001598 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001599}
1600
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001601static int w_restart_write(struct drbd_work *w, int cancel)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001602{
1603 struct drbd_request *req = container_of(w, struct drbd_request, w);
1604 struct drbd_conf *mdev = w->mdev;
1605 struct bio *bio;
1606 unsigned long start_time;
1607 unsigned long flags;
1608
1609 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
1610 if (!expect(req->rq_state & RQ_POSTPONED)) {
1611 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001612 return -EIO;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001613 }
1614 bio = req->master_bio;
1615 start_time = req->start_time;
1616 /* Postponed requests will not have their master_bio completed! */
1617 __req_mod(req, DISCARD_WRITE, NULL);
1618 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
1619
1620 while (__drbd_make_request(mdev, bio, start_time))
1621 /* retry */ ;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001622 return 0;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001623}
1624
1625static void restart_conflicting_writes(struct drbd_conf *mdev,
1626 sector_t sector, int size)
1627{
1628 struct drbd_interval *i;
1629 struct drbd_request *req;
1630
1631 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1632 if (!i->local)
1633 continue;
1634 req = container_of(i, struct drbd_request, i);
1635 if (req->rq_state & RQ_LOCAL_PENDING ||
1636 !(req->rq_state & RQ_POSTPONED))
1637 continue;
1638 if (expect(list_empty(&req->w.list))) {
1639 req->w.mdev = mdev;
1640 req->w.cb = w_restart_write;
1641 drbd_queue_work(&mdev->tconn->data.work, &req->w);
1642 }
1643 }
1644}
1645
Philipp Reisnerb411b362009-09-25 16:07:19 -07001646/* e_end_block() is called via drbd_process_done_ee().
1647 * this means this function only runs in the asender thread
1648 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001649static int e_end_block(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001650{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001651 struct drbd_peer_request *peer_req =
1652 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001653 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001654 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001655 int err = 0, pcmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001656
Philipp Reisner89e58e72011-01-19 13:12:45 +01001657 if (mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001658 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001659 pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
1660 mdev->state.conn <= C_PAUSED_SYNC_T &&
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001661 peer_req->flags & EE_MAY_SET_IN_SYNC) ?
Philipp Reisnerb411b362009-09-25 16:07:19 -07001662 P_RS_WRITE_ACK : P_WRITE_ACK;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001663 err = drbd_send_ack(mdev, pcmd, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001664 if (pcmd == P_RS_WRITE_ACK)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001665 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001666 } else {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001667 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001668 /* we expect it to be marked out of sync anyways...
1669 * maybe assert this? */
1670 }
1671 dec_unacked(mdev);
1672 }
1673 /* we delete from the conflict detection hash _after_ we sent out the
1674 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
Philipp Reisner89e58e72011-01-19 13:12:45 +01001675 if (mdev->tconn->net_conf->two_primaries) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01001676 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001677 D_ASSERT(!drbd_interval_empty(&peer_req->i));
1678 drbd_remove_epoch_entry_interval(mdev, peer_req);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001679 if (peer_req->flags & EE_RESTART_REQUESTS)
1680 restart_conflicting_writes(mdev, sector, peer_req->i.size);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001681 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +01001682 } else
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001683 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001684
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001685 drbd_may_finish_epoch(mdev, peer_req->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001686
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001687 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001688}
1689
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001690static int e_send_ack(struct drbd_work *w, enum drbd_packet ack)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001691{
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001692 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001693 struct drbd_peer_request *peer_req =
1694 container_of(w, struct drbd_peer_request, w);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001695 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001696
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001697 err = drbd_send_ack(mdev, ack, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001698 dec_unacked(mdev);
1699
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001700 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001701}
1702
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001703static int e_send_discard_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001704{
1705 return e_send_ack(w, P_DISCARD_WRITE);
1706}
1707
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001708static int e_send_retry_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001709{
1710 struct drbd_tconn *tconn = w->mdev->tconn;
1711
1712 return e_send_ack(w, tconn->agreed_pro_version >= 100 ?
1713 P_RETRY_WRITE : P_DISCARD_WRITE);
1714}
1715
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001716static bool seq_greater(u32 a, u32 b)
1717{
1718 /*
1719 * We assume 32-bit wrap-around here.
1720 * For 24-bit wrap-around, we would have to shift:
1721 * a <<= 8; b <<= 8;
1722 */
1723 return (s32)a - (s32)b > 0;
1724}
1725
1726static u32 seq_max(u32 a, u32 b)
1727{
1728 return seq_greater(a, b) ? a : b;
1729}
1730
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001731static bool need_peer_seq(struct drbd_conf *mdev)
1732{
1733 struct drbd_tconn *tconn = mdev->tconn;
1734
1735 /*
1736 * We only need to keep track of the last packet_seq number of our peer
1737 * if we are in dual-primary mode and we have the discard flag set; see
1738 * handle_write_conflicts().
1739 */
1740 return tconn->net_conf->two_primaries &&
1741 test_bit(DISCARD_CONCURRENT, &tconn->flags);
1742}
1743
Andreas Gruenbacher43ae0772011-02-03 18:42:08 +01001744static void update_peer_seq(struct drbd_conf *mdev, unsigned int peer_seq)
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001745{
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001746 unsigned int newest_peer_seq;
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001747
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001748 if (need_peer_seq(mdev)) {
1749 spin_lock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001750 newest_peer_seq = seq_max(mdev->peer_seq, peer_seq);
1751 mdev->peer_seq = newest_peer_seq;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001752 spin_unlock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001753 /* wake up only if we actually changed mdev->peer_seq */
1754 if (peer_seq == newest_peer_seq)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001755 wake_up(&mdev->seq_wait);
1756 }
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001757}
1758
Philipp Reisnerb411b362009-09-25 16:07:19 -07001759/* Called from receive_Data.
1760 * Synchronize packets on sock with packets on msock.
1761 *
1762 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
1763 * packet traveling on msock, they are still processed in the order they have
1764 * been sent.
1765 *
1766 * Note: we don't care for Ack packets overtaking P_DATA packets.
1767 *
1768 * In case packet_seq is larger than mdev->peer_seq number, there are
1769 * outstanding packets on the msock. We wait for them to arrive.
1770 * In case we are the logically next packet, we update mdev->peer_seq
1771 * ourselves. Correctly handles 32bit wrap around.
1772 *
1773 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
1774 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
1775 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
1776 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
1777 *
1778 * returns 0 if we may process the packet,
1779 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001780static int wait_for_and_update_peer_seq(struct drbd_conf *mdev, const u32 peer_seq)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001781{
1782 DEFINE_WAIT(wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001783 long timeout;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001784 int ret;
1785
1786 if (!need_peer_seq(mdev))
1787 return 0;
1788
Philipp Reisnerb411b362009-09-25 16:07:19 -07001789 spin_lock(&mdev->peer_seq_lock);
1790 for (;;) {
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001791 if (!seq_greater(peer_seq - 1, mdev->peer_seq)) {
1792 mdev->peer_seq = seq_max(mdev->peer_seq, peer_seq);
1793 ret = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001794 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001795 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001796 if (signal_pending(current)) {
1797 ret = -ERESTARTSYS;
1798 break;
1799 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001800 prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001801 spin_unlock(&mdev->peer_seq_lock);
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001802 timeout = mdev->tconn->net_conf->ping_timeo*HZ/10;
1803 timeout = schedule_timeout(timeout);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001804 spin_lock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001805 if (!timeout) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001806 ret = -ETIMEDOUT;
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001807 dev_err(DEV, "Timed out waiting for missing ack packets; disconnecting\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001808 break;
1809 }
1810 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001811 spin_unlock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001812 finish_wait(&mdev->seq_wait, &wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001813 return ret;
1814}
1815
Lars Ellenberg688593c2010-11-17 22:25:03 +01001816/* see also bio_flags_to_wire()
1817 * DRBD_REQ_*, because we need to semantically map the flags to data packet
1818 * flags and back. We may replicate to other kernel versions. */
1819static unsigned long wire_flags_to_bio(struct drbd_conf *mdev, u32 dpf)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001820{
Lars Ellenberg688593c2010-11-17 22:25:03 +01001821 return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
1822 (dpf & DP_FUA ? REQ_FUA : 0) |
1823 (dpf & DP_FLUSH ? REQ_FLUSH : 0) |
1824 (dpf & DP_DISCARD ? REQ_DISCARD : 0);
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001825}
1826
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001827static void fail_postponed_requests(struct drbd_conf *mdev, sector_t sector,
1828 unsigned int size)
1829{
1830 struct drbd_interval *i;
1831
1832 repeat:
1833 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1834 struct drbd_request *req;
1835 struct bio_and_error m;
1836
1837 if (!i->local)
1838 continue;
1839 req = container_of(i, struct drbd_request, i);
1840 if (!(req->rq_state & RQ_POSTPONED))
1841 continue;
1842 req->rq_state &= ~RQ_POSTPONED;
1843 __req_mod(req, NEG_ACKED, &m);
1844 spin_unlock_irq(&mdev->tconn->req_lock);
1845 if (m.bio)
1846 complete_master_bio(mdev, &m);
1847 spin_lock_irq(&mdev->tconn->req_lock);
1848 goto repeat;
1849 }
1850}
1851
1852static int handle_write_conflicts(struct drbd_conf *mdev,
1853 struct drbd_peer_request *peer_req)
1854{
1855 struct drbd_tconn *tconn = mdev->tconn;
1856 bool resolve_conflicts = test_bit(DISCARD_CONCURRENT, &tconn->flags);
1857 sector_t sector = peer_req->i.sector;
1858 const unsigned int size = peer_req->i.size;
1859 struct drbd_interval *i;
1860 bool equal;
1861 int err;
1862
1863 /*
1864 * Inserting the peer request into the write_requests tree will prevent
1865 * new conflicting local requests from being added.
1866 */
1867 drbd_insert_interval(&mdev->write_requests, &peer_req->i);
1868
1869 repeat:
1870 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1871 if (i == &peer_req->i)
1872 continue;
1873
1874 if (!i->local) {
1875 /*
1876 * Our peer has sent a conflicting remote request; this
1877 * should not happen in a two-node setup. Wait for the
1878 * earlier peer request to complete.
1879 */
1880 err = drbd_wait_misc(mdev, i);
1881 if (err)
1882 goto out;
1883 goto repeat;
1884 }
1885
1886 equal = i->sector == sector && i->size == size;
1887 if (resolve_conflicts) {
1888 /*
1889 * If the peer request is fully contained within the
1890 * overlapping request, it can be discarded; otherwise,
1891 * it will be retried once all overlapping requests
1892 * have completed.
1893 */
1894 bool discard = i->sector <= sector && i->sector +
1895 (i->size >> 9) >= sector + (size >> 9);
1896
1897 if (!equal)
1898 dev_alert(DEV, "Concurrent writes detected: "
1899 "local=%llus +%u, remote=%llus +%u, "
1900 "assuming %s came first\n",
1901 (unsigned long long)i->sector, i->size,
1902 (unsigned long long)sector, size,
1903 discard ? "local" : "remote");
1904
1905 inc_unacked(mdev);
1906 peer_req->w.cb = discard ? e_send_discard_write :
1907 e_send_retry_write;
1908 list_add_tail(&peer_req->w.list, &mdev->done_ee);
1909 wake_asender(mdev->tconn);
1910
1911 err = -ENOENT;
1912 goto out;
1913 } else {
1914 struct drbd_request *req =
1915 container_of(i, struct drbd_request, i);
1916
1917 if (!equal)
1918 dev_alert(DEV, "Concurrent writes detected: "
1919 "local=%llus +%u, remote=%llus +%u\n",
1920 (unsigned long long)i->sector, i->size,
1921 (unsigned long long)sector, size);
1922
1923 if (req->rq_state & RQ_LOCAL_PENDING ||
1924 !(req->rq_state & RQ_POSTPONED)) {
1925 /*
1926 * Wait for the node with the discard flag to
1927 * decide if this request will be discarded or
1928 * retried. Requests that are discarded will
1929 * disappear from the write_requests tree.
1930 *
1931 * In addition, wait for the conflicting
1932 * request to finish locally before submitting
1933 * the conflicting peer request.
1934 */
1935 err = drbd_wait_misc(mdev, &req->i);
1936 if (err) {
1937 _conn_request_state(mdev->tconn,
1938 NS(conn, C_TIMEOUT),
1939 CS_HARD);
1940 fail_postponed_requests(mdev, sector, size);
1941 goto out;
1942 }
1943 goto repeat;
1944 }
1945 /*
1946 * Remember to restart the conflicting requests after
1947 * the new peer request has completed.
1948 */
1949 peer_req->flags |= EE_RESTART_REQUESTS;
1950 }
1951 }
1952 err = 0;
1953
1954 out:
1955 if (err)
1956 drbd_remove_epoch_entry_interval(mdev, peer_req);
1957 return err;
1958}
1959
Philipp Reisnerb411b362009-09-25 16:07:19 -07001960/* mirrored write */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01001961static int receive_Data(struct drbd_conf *mdev, enum drbd_packet cmd,
1962 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001963{
1964 sector_t sector;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001965 struct drbd_peer_request *peer_req;
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01001966 struct p_data *p = mdev->tconn->data.rbuf;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001967 u32 peer_seq = be32_to_cpu(p->seq_num);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001968 int rw = WRITE;
1969 u32 dp_flags;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001970 int err;
1971
Philipp Reisnerb411b362009-09-25 16:07:19 -07001972 if (!get_ldev(mdev)) {
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001973 int err2;
1974
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001975 err = wait_for_and_update_peer_seq(mdev, peer_seq);
Lars Ellenberg2b2bf212010-10-06 11:46:55 +02001976 drbd_send_ack_dp(mdev, P_NEG_ACK, p, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001977 atomic_inc(&mdev->current_epoch->epoch_size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001978 err2 = drbd_drain_block(mdev, data_size);
1979 if (!err)
1980 err = err2;
1981 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001982 }
1983
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001984 /*
1985 * Corresponding put_ldev done either below (on various errors), or in
1986 * drbd_peer_request_endio, if we successfully submit the data at the
1987 * end of this function.
1988 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001989
1990 sector = be64_to_cpu(p->sector);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001991 peer_req = read_in_block(mdev, p->block_id, sector, data_size);
1992 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001993 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001994 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001995 }
1996
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001997 peer_req->w.cb = e_end_block;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001998
Lars Ellenberg688593c2010-11-17 22:25:03 +01001999 dp_flags = be32_to_cpu(p->dp_flags);
2000 rw |= wire_flags_to_bio(mdev, dp_flags);
2001
2002 if (dp_flags & DP_MAY_SET_IN_SYNC)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002003 peer_req->flags |= EE_MAY_SET_IN_SYNC;
Lars Ellenberg688593c2010-11-17 22:25:03 +01002004
Philipp Reisnerb411b362009-09-25 16:07:19 -07002005 spin_lock(&mdev->epoch_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002006 peer_req->epoch = mdev->current_epoch;
2007 atomic_inc(&peer_req->epoch->epoch_size);
2008 atomic_inc(&peer_req->epoch->active);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002009 spin_unlock(&mdev->epoch_lock);
2010
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002011 if (mdev->tconn->net_conf->two_primaries) {
2012 err = wait_for_and_update_peer_seq(mdev, peer_seq);
2013 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002014 goto out_interrupted;
Philipp Reisner87eeee42011-01-19 14:16:30 +01002015 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002016 err = handle_write_conflicts(mdev, peer_req);
2017 if (err) {
2018 spin_unlock_irq(&mdev->tconn->req_lock);
2019 if (err == -ENOENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002020 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002021 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002022 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002023 goto out_interrupted;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002024 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002025 } else
2026 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002027 list_add(&peer_req->w.list, &mdev->active_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002028 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002029
Philipp Reisner89e58e72011-01-19 13:12:45 +01002030 switch (mdev->tconn->net_conf->wire_protocol) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002031 case DRBD_PROT_C:
2032 inc_unacked(mdev);
2033 /* corresponding dec_unacked() in e_end_block()
2034 * respective _drbd_clear_done_ee */
2035 break;
2036 case DRBD_PROT_B:
2037 /* I really don't like it that the receiver thread
2038 * sends on the msock, but anyways */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002039 drbd_send_ack(mdev, P_RECV_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002040 break;
2041 case DRBD_PROT_A:
2042 /* nothing to do */
2043 break;
2044 }
2045
Lars Ellenberg6719fb02010-10-18 23:04:07 +02002046 if (mdev->state.pdsk < D_INCONSISTENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002047 /* In case we have the only disk of the cluster, */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002048 drbd_set_out_of_sync(mdev, peer_req->i.sector, peer_req->i.size);
2049 peer_req->flags |= EE_CALL_AL_COMPLETE_IO;
2050 peer_req->flags &= ~EE_MAY_SET_IN_SYNC;
2051 drbd_al_begin_io(mdev, peer_req->i.sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002052 }
2053
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002054 err = drbd_submit_peer_request(mdev, peer_req, rw, DRBD_FAULT_DT_WR);
2055 if (!err)
2056 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002057
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002058 /* don't care for the reason here */
2059 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002060 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002061 list_del(&peer_req->w.list);
2062 drbd_remove_epoch_entry_interval(mdev, peer_req);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002063 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002064 if (peer_req->flags & EE_CALL_AL_COMPLETE_IO)
2065 drbd_al_complete_io(mdev, peer_req->i.sector);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002066
Philipp Reisnerb411b362009-09-25 16:07:19 -07002067out_interrupted:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002068 drbd_may_finish_epoch(mdev, peer_req->epoch, EV_PUT + EV_CLEANUP);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002069 put_ldev(mdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002070 drbd_free_ee(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002071 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002072}
2073
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002074/* We may throttle resync, if the lower device seems to be busy,
2075 * and current sync rate is above c_min_rate.
2076 *
2077 * To decide whether or not the lower device is busy, we use a scheme similar
2078 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
2079 * (more than 64 sectors) of activity we cannot account for with our own resync
2080 * activity, it obviously is "busy".
2081 *
2082 * The current sync rate used here uses only the most recent two step marks,
2083 * to have a short time average so we can react faster.
2084 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002085int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002086{
2087 struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
2088 unsigned long db, dt, dbdt;
Philipp Reisnere3555d82010-11-07 15:56:29 +01002089 struct lc_element *tmp;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002090 int curr_events;
2091 int throttle = 0;
2092
2093 /* feature disabled? */
Lars Ellenbergf3990022011-03-23 14:31:09 +01002094 if (mdev->ldev->dc.c_min_rate == 0)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002095 return 0;
2096
Philipp Reisnere3555d82010-11-07 15:56:29 +01002097 spin_lock_irq(&mdev->al_lock);
2098 tmp = lc_find(mdev->resync, BM_SECT_TO_EXT(sector));
2099 if (tmp) {
2100 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
2101 if (test_bit(BME_PRIORITY, &bm_ext->flags)) {
2102 spin_unlock_irq(&mdev->al_lock);
2103 return 0;
2104 }
2105 /* Do not slow down if app IO is already waiting for this extent */
2106 }
2107 spin_unlock_irq(&mdev->al_lock);
2108
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002109 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
2110 (int)part_stat_read(&disk->part0, sectors[1]) -
2111 atomic_read(&mdev->rs_sect_ev);
Philipp Reisnere3555d82010-11-07 15:56:29 +01002112
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002113 if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
2114 unsigned long rs_left;
2115 int i;
2116
2117 mdev->rs_last_events = curr_events;
2118
2119 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
2120 * approx. */
Lars Ellenberg2649f082010-11-05 10:05:47 +01002121 i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
2122
2123 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
2124 rs_left = mdev->ov_left;
2125 else
2126 rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002127
2128 dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
2129 if (!dt)
2130 dt++;
2131 db = mdev->rs_mark_left[i] - rs_left;
2132 dbdt = Bit2KB(db/dt);
2133
Lars Ellenbergf3990022011-03-23 14:31:09 +01002134 if (dbdt > mdev->ldev->dc.c_min_rate)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002135 throttle = 1;
2136 }
2137 return throttle;
2138}
2139
2140
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01002141static int receive_DataRequest(struct drbd_conf *mdev, enum drbd_packet cmd,
2142 unsigned int digest_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002143{
2144 sector_t sector;
2145 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002146 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002147 struct digest_info *di = NULL;
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002148 int size, verb;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002149 unsigned int fault_type;
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01002150 struct p_block_req *p = mdev->tconn->data.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002151
2152 sector = be64_to_cpu(p->sector);
2153 size = be32_to_cpu(p->blksize);
2154
Andreas Gruenbacherc670a392011-02-21 12:41:39 +01002155 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002156 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2157 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002158 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002159 }
2160 if (sector + (size>>9) > capacity) {
2161 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2162 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002163 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002164 }
2165
2166 if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002167 verb = 1;
2168 switch (cmd) {
2169 case P_DATA_REQUEST:
2170 drbd_send_ack_rp(mdev, P_NEG_DREPLY, p);
2171 break;
2172 case P_RS_DATA_REQUEST:
2173 case P_CSUM_RS_REQUEST:
2174 case P_OV_REQUEST:
2175 drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p);
2176 break;
2177 case P_OV_REPLY:
2178 verb = 0;
2179 dec_rs_pending(mdev);
2180 drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC);
2181 break;
2182 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002183 BUG();
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002184 }
2185 if (verb && __ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002186 dev_err(DEV, "Can not satisfy peer's read request, "
2187 "no local data.\n");
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002188
Lars Ellenberga821cc42010-09-06 12:31:37 +02002189 /* drain possibly payload */
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002190 return drbd_drain_block(mdev, digest_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002191 }
2192
2193 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
2194 * "criss-cross" setup, that might cause write-out on some other DRBD,
2195 * which in turn might block on the other node at this very place. */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002196 peer_req = drbd_alloc_ee(mdev, p->block_id, sector, size, GFP_NOIO);
2197 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002198 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002199 return -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002200 }
2201
Philipp Reisner02918be2010-08-20 14:35:10 +02002202 switch (cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002203 case P_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002204 peer_req->w.cb = w_e_end_data_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002205 fault_type = DRBD_FAULT_DT_RD;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002206 /* application IO, don't drbd_rs_begin_io */
2207 goto submit;
2208
Philipp Reisnerb411b362009-09-25 16:07:19 -07002209 case P_RS_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002210 peer_req->w.cb = w_e_end_rsdata_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002211 fault_type = DRBD_FAULT_RS_RD;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002212 /* used in the sector offset progress display */
2213 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002214 break;
2215
2216 case P_OV_REPLY:
2217 case P_CSUM_RS_REQUEST:
2218 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002219 di = kmalloc(sizeof(*di) + digest_size, GFP_NOIO);
2220 if (!di)
2221 goto out_free_e;
2222
2223 di->digest_size = digest_size;
2224 di->digest = (((char *)di)+sizeof(struct digest_info));
2225
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002226 peer_req->digest = di;
2227 peer_req->flags |= EE_HAS_DIGEST;
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +02002228
Philipp Reisnerde0ff332011-02-07 16:56:20 +01002229 if (drbd_recv(mdev->tconn, di->digest, digest_size) != digest_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002230 goto out_free_e;
2231
Philipp Reisner02918be2010-08-20 14:35:10 +02002232 if (cmd == P_CSUM_RS_REQUEST) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002233 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002234 peer_req->w.cb = w_e_end_csum_rs_req;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002235 /* used in the sector offset progress display */
2236 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisner02918be2010-08-20 14:35:10 +02002237 } else if (cmd == P_OV_REPLY) {
Lars Ellenberg2649f082010-11-05 10:05:47 +01002238 /* track progress, we may need to throttle */
2239 atomic_add(size >> 9, &mdev->rs_sect_in);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002240 peer_req->w.cb = w_e_end_ov_reply;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002241 dec_rs_pending(mdev);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002242 /* drbd_rs_begin_io done when we sent this request,
2243 * but accounting still needs to be done. */
2244 goto submit_for_resync;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002245 }
2246 break;
2247
2248 case P_OV_REQUEST:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002249 if (mdev->ov_start_sector == ~(sector_t)0 &&
Philipp Reisner31890f42011-01-19 14:12:51 +01002250 mdev->tconn->agreed_pro_version >= 90) {
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002251 unsigned long now = jiffies;
2252 int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002253 mdev->ov_start_sector = sector;
2254 mdev->ov_position = sector;
Lars Ellenberg30b743a2010-11-05 09:39:06 +01002255 mdev->ov_left = drbd_bm_bits(mdev) - BM_SECT_TO_BIT(sector);
2256 mdev->rs_total = mdev->ov_left;
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002257 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
2258 mdev->rs_mark_left[i] = mdev->ov_left;
2259 mdev->rs_mark_time[i] = now;
2260 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002261 dev_info(DEV, "Online Verify start sector: %llu\n",
2262 (unsigned long long)sector);
2263 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002264 peer_req->w.cb = w_e_end_ov_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002265 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002266 break;
2267
Philipp Reisnerb411b362009-09-25 16:07:19 -07002268 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002269 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002270 }
2271
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002272 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2273 * wrt the receiver, but it is not as straightforward as it may seem.
2274 * Various places in the resync start and stop logic assume resync
2275 * requests are processed in order, requeuing this on the worker thread
2276 * introduces a bunch of new code for synchronization between threads.
2277 *
2278 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2279 * "forever", throttling after drbd_rs_begin_io will lock that extent
2280 * for application writes for the same time. For now, just throttle
2281 * here, where the rest of the code expects the receiver to sleep for
2282 * a while, anyways.
2283 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002284
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002285 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2286 * this defers syncer requests for some time, before letting at least
2287 * on request through. The resync controller on the receiving side
2288 * will adapt to the incoming rate accordingly.
2289 *
2290 * We cannot throttle here if remote is Primary/SyncTarget:
2291 * we would also throttle its application reads.
2292 * In that case, throttling is done on the SyncTarget only.
2293 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002294 if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev, sector))
2295 schedule_timeout_uninterruptible(HZ/10);
2296 if (drbd_rs_begin_io(mdev, sector))
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002297 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002298
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002299submit_for_resync:
2300 atomic_add(size >> 9, &mdev->rs_sect_ev);
2301
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002302submit:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002303 inc_unacked(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002304 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002305 list_add_tail(&peer_req->w.list, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002306 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002307
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01002308 if (drbd_submit_peer_request(mdev, peer_req, READ, fault_type) == 0)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002309 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002310
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002311 /* don't care for the reason here */
2312 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002313 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002314 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002315 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002316 /* no drbd_rs_complete_io(), we are dropping the connection anyways */
2317
Philipp Reisnerb411b362009-09-25 16:07:19 -07002318out_free_e:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002319 put_ldev(mdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002320 drbd_free_ee(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002321 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002322}
2323
2324static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
2325{
2326 int self, peer, rv = -100;
2327 unsigned long ch_self, ch_peer;
2328
2329 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2330 peer = mdev->p_uuid[UI_BITMAP] & 1;
2331
2332 ch_peer = mdev->p_uuid[UI_SIZE];
2333 ch_self = mdev->comm_bm_set;
2334
Philipp Reisner89e58e72011-01-19 13:12:45 +01002335 switch (mdev->tconn->net_conf->after_sb_0p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002336 case ASB_CONSENSUS:
2337 case ASB_DISCARD_SECONDARY:
2338 case ASB_CALL_HELPER:
2339 dev_err(DEV, "Configuration error.\n");
2340 break;
2341 case ASB_DISCONNECT:
2342 break;
2343 case ASB_DISCARD_YOUNGER_PRI:
2344 if (self == 0 && peer == 1) {
2345 rv = -1;
2346 break;
2347 }
2348 if (self == 1 && peer == 0) {
2349 rv = 1;
2350 break;
2351 }
2352 /* Else fall through to one of the other strategies... */
2353 case ASB_DISCARD_OLDER_PRI:
2354 if (self == 0 && peer == 1) {
2355 rv = 1;
2356 break;
2357 }
2358 if (self == 1 && peer == 0) {
2359 rv = -1;
2360 break;
2361 }
2362 /* Else fall through to one of the other strategies... */
Lars Ellenbergad19bf62009-10-14 09:36:49 +02002363 dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
Philipp Reisnerb411b362009-09-25 16:07:19 -07002364 "Using discard-least-changes instead\n");
2365 case ASB_DISCARD_ZERO_CHG:
2366 if (ch_peer == 0 && ch_self == 0) {
Philipp Reisner25703f82011-02-07 14:35:25 +01002367 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002368 ? -1 : 1;
2369 break;
2370 } else {
2371 if (ch_peer == 0) { rv = 1; break; }
2372 if (ch_self == 0) { rv = -1; break; }
2373 }
Philipp Reisner89e58e72011-01-19 13:12:45 +01002374 if (mdev->tconn->net_conf->after_sb_0p == ASB_DISCARD_ZERO_CHG)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002375 break;
2376 case ASB_DISCARD_LEAST_CHG:
2377 if (ch_self < ch_peer)
2378 rv = -1;
2379 else if (ch_self > ch_peer)
2380 rv = 1;
2381 else /* ( ch_self == ch_peer ) */
2382 /* Well, then use something else. */
Philipp Reisner25703f82011-02-07 14:35:25 +01002383 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002384 ? -1 : 1;
2385 break;
2386 case ASB_DISCARD_LOCAL:
2387 rv = -1;
2388 break;
2389 case ASB_DISCARD_REMOTE:
2390 rv = 1;
2391 }
2392
2393 return rv;
2394}
2395
2396static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
2397{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002398 int hg, rv = -100;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002399
Philipp Reisner89e58e72011-01-19 13:12:45 +01002400 switch (mdev->tconn->net_conf->after_sb_1p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002401 case ASB_DISCARD_YOUNGER_PRI:
2402 case ASB_DISCARD_OLDER_PRI:
2403 case ASB_DISCARD_LEAST_CHG:
2404 case ASB_DISCARD_LOCAL:
2405 case ASB_DISCARD_REMOTE:
2406 dev_err(DEV, "Configuration error.\n");
2407 break;
2408 case ASB_DISCONNECT:
2409 break;
2410 case ASB_CONSENSUS:
2411 hg = drbd_asb_recover_0p(mdev);
2412 if (hg == -1 && mdev->state.role == R_SECONDARY)
2413 rv = hg;
2414 if (hg == 1 && mdev->state.role == R_PRIMARY)
2415 rv = hg;
2416 break;
2417 case ASB_VIOLENTLY:
2418 rv = drbd_asb_recover_0p(mdev);
2419 break;
2420 case ASB_DISCARD_SECONDARY:
2421 return mdev->state.role == R_PRIMARY ? 1 : -1;
2422 case ASB_CALL_HELPER:
2423 hg = drbd_asb_recover_0p(mdev);
2424 if (hg == -1 && mdev->state.role == R_PRIMARY) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002425 enum drbd_state_rv rv2;
2426
2427 drbd_set_role(mdev, R_SECONDARY, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002428 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2429 * we might be here in C_WF_REPORT_PARAMS which is transient.
2430 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002431 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2432 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002433 drbd_khelper(mdev, "pri-lost-after-sb");
2434 } else {
2435 dev_warn(DEV, "Successfully gave up primary role.\n");
2436 rv = hg;
2437 }
2438 } else
2439 rv = hg;
2440 }
2441
2442 return rv;
2443}
2444
2445static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
2446{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002447 int hg, rv = -100;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002448
Philipp Reisner89e58e72011-01-19 13:12:45 +01002449 switch (mdev->tconn->net_conf->after_sb_2p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002450 case ASB_DISCARD_YOUNGER_PRI:
2451 case ASB_DISCARD_OLDER_PRI:
2452 case ASB_DISCARD_LEAST_CHG:
2453 case ASB_DISCARD_LOCAL:
2454 case ASB_DISCARD_REMOTE:
2455 case ASB_CONSENSUS:
2456 case ASB_DISCARD_SECONDARY:
2457 dev_err(DEV, "Configuration error.\n");
2458 break;
2459 case ASB_VIOLENTLY:
2460 rv = drbd_asb_recover_0p(mdev);
2461 break;
2462 case ASB_DISCONNECT:
2463 break;
2464 case ASB_CALL_HELPER:
2465 hg = drbd_asb_recover_0p(mdev);
2466 if (hg == -1) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002467 enum drbd_state_rv rv2;
2468
Philipp Reisnerb411b362009-09-25 16:07:19 -07002469 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2470 * we might be here in C_WF_REPORT_PARAMS which is transient.
2471 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002472 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2473 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002474 drbd_khelper(mdev, "pri-lost-after-sb");
2475 } else {
2476 dev_warn(DEV, "Successfully gave up primary role.\n");
2477 rv = hg;
2478 }
2479 } else
2480 rv = hg;
2481 }
2482
2483 return rv;
2484}
2485
2486static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
2487 u64 bits, u64 flags)
2488{
2489 if (!uuid) {
2490 dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
2491 return;
2492 }
2493 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
2494 text,
2495 (unsigned long long)uuid[UI_CURRENT],
2496 (unsigned long long)uuid[UI_BITMAP],
2497 (unsigned long long)uuid[UI_HISTORY_START],
2498 (unsigned long long)uuid[UI_HISTORY_END],
2499 (unsigned long long)bits,
2500 (unsigned long long)flags);
2501}
2502
2503/*
2504 100 after split brain try auto recover
2505 2 C_SYNC_SOURCE set BitMap
2506 1 C_SYNC_SOURCE use BitMap
2507 0 no Sync
2508 -1 C_SYNC_TARGET use BitMap
2509 -2 C_SYNC_TARGET set BitMap
2510 -100 after split brain, disconnect
2511-1000 unrelated data
Philipp Reisner4a23f262011-01-11 17:42:17 +01002512-1091 requires proto 91
2513-1096 requires proto 96
Philipp Reisnerb411b362009-09-25 16:07:19 -07002514 */
2515static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
2516{
2517 u64 self, peer;
2518 int i, j;
2519
2520 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2521 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2522
2523 *rule_nr = 10;
2524 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
2525 return 0;
2526
2527 *rule_nr = 20;
2528 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
2529 peer != UUID_JUST_CREATED)
2530 return -2;
2531
2532 *rule_nr = 30;
2533 if (self != UUID_JUST_CREATED &&
2534 (peer == UUID_JUST_CREATED || peer == (u64)0))
2535 return 2;
2536
2537 if (self == peer) {
2538 int rct, dc; /* roles at crash time */
2539
2540 if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
2541
Philipp Reisner31890f42011-01-19 14:12:51 +01002542 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002543 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002544
2545 if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
2546 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
2547 dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
2548 drbd_uuid_set_bm(mdev, 0UL);
2549
2550 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2551 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2552 *rule_nr = 34;
2553 } else {
2554 dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
2555 *rule_nr = 36;
2556 }
2557
2558 return 1;
2559 }
2560
2561 if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
2562
Philipp Reisner31890f42011-01-19 14:12:51 +01002563 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002564 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002565
2566 if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
2567 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
2568 dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
2569
2570 mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
2571 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
2572 mdev->p_uuid[UI_BITMAP] = 0UL;
2573
2574 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2575 *rule_nr = 35;
2576 } else {
2577 dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
2578 *rule_nr = 37;
2579 }
2580
2581 return -1;
2582 }
2583
2584 /* Common power [off|failure] */
2585 rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
2586 (mdev->p_uuid[UI_FLAGS] & 2);
2587 /* lowest bit is set when we were primary,
2588 * next bit (weight 2) is set when peer was primary */
2589 *rule_nr = 40;
2590
2591 switch (rct) {
2592 case 0: /* !self_pri && !peer_pri */ return 0;
2593 case 1: /* self_pri && !peer_pri */ return 1;
2594 case 2: /* !self_pri && peer_pri */ return -1;
2595 case 3: /* self_pri && peer_pri */
Philipp Reisner25703f82011-02-07 14:35:25 +01002596 dc = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002597 return dc ? -1 : 1;
2598 }
2599 }
2600
2601 *rule_nr = 50;
2602 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2603 if (self == peer)
2604 return -1;
2605
2606 *rule_nr = 51;
2607 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2608 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002609 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002610 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) ==
2611 (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) :
2612 peer + UUID_NEW_BM_OFFSET == (mdev->p_uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002613 /* The last P_SYNC_UUID did not get though. Undo the last start of
2614 resync as sync source modifications of the peer's UUIDs. */
2615
Philipp Reisner31890f42011-01-19 14:12:51 +01002616 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002617 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002618
2619 mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
2620 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
Philipp Reisner4a23f262011-01-11 17:42:17 +01002621
2622 dev_info(DEV, "Did not got last syncUUID packet, corrected:\n");
2623 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2624
Philipp Reisnerb411b362009-09-25 16:07:19 -07002625 return -1;
2626 }
2627 }
2628
2629 *rule_nr = 60;
2630 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2631 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2632 peer = mdev->p_uuid[i] & ~((u64)1);
2633 if (self == peer)
2634 return -2;
2635 }
2636
2637 *rule_nr = 70;
2638 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2639 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2640 if (self == peer)
2641 return 1;
2642
2643 *rule_nr = 71;
2644 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2645 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002646 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002647 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) ==
2648 (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) :
2649 self + UUID_NEW_BM_OFFSET == (mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002650 /* The last P_SYNC_UUID did not get though. Undo the last start of
2651 resync as sync source modifications of our UUIDs. */
2652
Philipp Reisner31890f42011-01-19 14:12:51 +01002653 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002654 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002655
2656 _drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
2657 _drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
2658
Philipp Reisner4a23f262011-01-11 17:42:17 +01002659 dev_info(DEV, "Last syncUUID did not get through, corrected:\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002660 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2661 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2662
2663 return 1;
2664 }
2665 }
2666
2667
2668 *rule_nr = 80;
Philipp Reisnerd8c2a362009-11-18 15:52:51 +01002669 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002670 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2671 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2672 if (self == peer)
2673 return 2;
2674 }
2675
2676 *rule_nr = 90;
2677 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2678 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2679 if (self == peer && self != ((u64)0))
2680 return 100;
2681
2682 *rule_nr = 100;
2683 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2684 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2685 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
2686 peer = mdev->p_uuid[j] & ~((u64)1);
2687 if (self == peer)
2688 return -100;
2689 }
2690 }
2691
2692 return -1000;
2693}
2694
2695/* drbd_sync_handshake() returns the new conn state on success, or
2696 CONN_MASK (-1) on failure.
2697 */
2698static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
2699 enum drbd_disk_state peer_disk) __must_hold(local)
2700{
2701 int hg, rule_nr;
2702 enum drbd_conns rv = C_MASK;
2703 enum drbd_disk_state mydisk;
2704
2705 mydisk = mdev->state.disk;
2706 if (mydisk == D_NEGOTIATING)
2707 mydisk = mdev->new_state_tmp.disk;
2708
2709 dev_info(DEV, "drbd_sync_handshake:\n");
2710 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0);
2711 drbd_uuid_dump(mdev, "peer", mdev->p_uuid,
2712 mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2713
2714 hg = drbd_uuid_compare(mdev, &rule_nr);
2715
2716 dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
2717
2718 if (hg == -1000) {
2719 dev_alert(DEV, "Unrelated data, aborting!\n");
2720 return C_MASK;
2721 }
Philipp Reisner4a23f262011-01-11 17:42:17 +01002722 if (hg < -1000) {
2723 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 -07002724 return C_MASK;
2725 }
2726
2727 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
2728 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
2729 int f = (hg == -100) || abs(hg) == 2;
2730 hg = mydisk > D_INCONSISTENT ? 1 : -1;
2731 if (f)
2732 hg = hg*2;
2733 dev_info(DEV, "Becoming sync %s due to disk states.\n",
2734 hg > 0 ? "source" : "target");
2735 }
2736
Adam Gandelman3a11a482010-04-08 16:48:23 -07002737 if (abs(hg) == 100)
2738 drbd_khelper(mdev, "initial-split-brain");
2739
Philipp Reisner89e58e72011-01-19 13:12:45 +01002740 if (hg == 100 || (hg == -100 && mdev->tconn->net_conf->always_asbp)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002741 int pcount = (mdev->state.role == R_PRIMARY)
2742 + (peer_role == R_PRIMARY);
2743 int forced = (hg == -100);
2744
2745 switch (pcount) {
2746 case 0:
2747 hg = drbd_asb_recover_0p(mdev);
2748 break;
2749 case 1:
2750 hg = drbd_asb_recover_1p(mdev);
2751 break;
2752 case 2:
2753 hg = drbd_asb_recover_2p(mdev);
2754 break;
2755 }
2756 if (abs(hg) < 100) {
2757 dev_warn(DEV, "Split-Brain detected, %d primaries, "
2758 "automatically solved. Sync from %s node\n",
2759 pcount, (hg < 0) ? "peer" : "this");
2760 if (forced) {
2761 dev_warn(DEV, "Doing a full sync, since"
2762 " UUIDs where ambiguous.\n");
2763 hg = hg*2;
2764 }
2765 }
2766 }
2767
2768 if (hg == -100) {
Philipp Reisner89e58e72011-01-19 13:12:45 +01002769 if (mdev->tconn->net_conf->want_lose && !(mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002770 hg = -1;
Philipp Reisner89e58e72011-01-19 13:12:45 +01002771 if (!mdev->tconn->net_conf->want_lose && (mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002772 hg = 1;
2773
2774 if (abs(hg) < 100)
2775 dev_warn(DEV, "Split-Brain detected, manually solved. "
2776 "Sync from %s node\n",
2777 (hg < 0) ? "peer" : "this");
2778 }
2779
2780 if (hg == -100) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01002781 /* FIXME this log message is not correct if we end up here
2782 * after an attempted attach on a diskless node.
2783 * We just refuse to attach -- well, we drop the "connection"
2784 * to that disk, in a way... */
Adam Gandelman3a11a482010-04-08 16:48:23 -07002785 dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002786 drbd_khelper(mdev, "split-brain");
2787 return C_MASK;
2788 }
2789
2790 if (hg > 0 && mydisk <= D_INCONSISTENT) {
2791 dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
2792 return C_MASK;
2793 }
2794
2795 if (hg < 0 && /* by intention we do not use mydisk here. */
2796 mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
Philipp Reisner89e58e72011-01-19 13:12:45 +01002797 switch (mdev->tconn->net_conf->rr_conflict) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002798 case ASB_CALL_HELPER:
2799 drbd_khelper(mdev, "pri-lost");
2800 /* fall through */
2801 case ASB_DISCONNECT:
2802 dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
2803 return C_MASK;
2804 case ASB_VIOLENTLY:
2805 dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
2806 "assumption\n");
2807 }
2808 }
2809
Philipp Reisner8169e412011-03-15 18:40:27 +01002810 if (mdev->tconn->net_conf->dry_run || test_bit(CONN_DRY_RUN, &mdev->tconn->flags)) {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002811 if (hg == 0)
2812 dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
2813 else
2814 dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
2815 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
2816 abs(hg) >= 2 ? "full" : "bit-map based");
2817 return C_MASK;
2818 }
2819
Philipp Reisnerb411b362009-09-25 16:07:19 -07002820 if (abs(hg) >= 2) {
2821 dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002822 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake",
2823 BM_LOCKED_SET_ALLOWED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002824 return C_MASK;
2825 }
2826
2827 if (hg > 0) { /* become sync source. */
2828 rv = C_WF_BITMAP_S;
2829 } else if (hg < 0) { /* become sync target */
2830 rv = C_WF_BITMAP_T;
2831 } else {
2832 rv = C_CONNECTED;
2833 if (drbd_bm_total_weight(mdev)) {
2834 dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
2835 drbd_bm_total_weight(mdev));
2836 }
2837 }
2838
2839 return rv;
2840}
2841
2842/* returns 1 if invalid */
2843static int cmp_after_sb(enum drbd_after_sb_p peer, enum drbd_after_sb_p self)
2844{
2845 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
2846 if ((peer == ASB_DISCARD_REMOTE && self == ASB_DISCARD_LOCAL) ||
2847 (self == ASB_DISCARD_REMOTE && peer == ASB_DISCARD_LOCAL))
2848 return 0;
2849
2850 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
2851 if (peer == ASB_DISCARD_REMOTE || peer == ASB_DISCARD_LOCAL ||
2852 self == ASB_DISCARD_REMOTE || self == ASB_DISCARD_LOCAL)
2853 return 1;
2854
2855 /* everything else is valid if they are equal on both sides. */
2856 if (peer == self)
2857 return 0;
2858
2859 /* everything es is invalid. */
2860 return 1;
2861}
2862
Philipp Reisner7204624c2011-03-15 18:51:47 +01002863static int receive_protocol(struct drbd_tconn *tconn, enum drbd_packet cmd,
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01002864 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002865{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01002866 struct p_protocol *p = tconn->data.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002867 int p_proto, p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002868 int p_want_lose, p_two_primaries, cf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002869 char p_integrity_alg[SHARED_SECRET_MAX] = "";
2870
Philipp Reisnerb411b362009-09-25 16:07:19 -07002871 p_proto = be32_to_cpu(p->protocol);
2872 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
2873 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
2874 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002875 p_two_primaries = be32_to_cpu(p->two_primaries);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002876 cf = be32_to_cpu(p->conn_flags);
2877 p_want_lose = cf & CF_WANT_LOSE;
2878
Philipp Reisner7204624c2011-03-15 18:51:47 +01002879 clear_bit(CONN_DRY_RUN, &tconn->flags);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002880
2881 if (cf & CF_DRY_RUN)
Philipp Reisner7204624c2011-03-15 18:51:47 +01002882 set_bit(CONN_DRY_RUN, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002883
Philipp Reisner7204624c2011-03-15 18:51:47 +01002884 if (p_proto != tconn->net_conf->wire_protocol) {
2885 conn_err(tconn, "incompatible communication protocols\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002886 goto disconnect;
2887 }
2888
Philipp Reisner7204624c2011-03-15 18:51:47 +01002889 if (cmp_after_sb(p_after_sb_0p, tconn->net_conf->after_sb_0p)) {
2890 conn_err(tconn, "incompatible after-sb-0pri settings\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002891 goto disconnect;
2892 }
2893
Philipp Reisner7204624c2011-03-15 18:51:47 +01002894 if (cmp_after_sb(p_after_sb_1p, tconn->net_conf->after_sb_1p)) {
2895 conn_err(tconn, "incompatible after-sb-1pri settings\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002896 goto disconnect;
2897 }
2898
Philipp Reisner7204624c2011-03-15 18:51:47 +01002899 if (cmp_after_sb(p_after_sb_2p, tconn->net_conf->after_sb_2p)) {
2900 conn_err(tconn, "incompatible after-sb-2pri settings\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002901 goto disconnect;
2902 }
2903
Philipp Reisner7204624c2011-03-15 18:51:47 +01002904 if (p_want_lose && tconn->net_conf->want_lose) {
2905 conn_err(tconn, "both sides have the 'want_lose' flag set\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002906 goto disconnect;
2907 }
2908
Philipp Reisner7204624c2011-03-15 18:51:47 +01002909 if (p_two_primaries != tconn->net_conf->two_primaries) {
2910 conn_err(tconn, "incompatible setting of the two-primaries options\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002911 goto disconnect;
2912 }
2913
Philipp Reisner7204624c2011-03-15 18:51:47 +01002914 if (tconn->agreed_pro_version >= 87) {
2915 unsigned char *my_alg = tconn->net_conf->integrity_alg;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002916 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002917
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002918 err = drbd_recv_all(tconn, p_integrity_alg, data_size);
2919 if (err)
2920 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002921
2922 p_integrity_alg[SHARED_SECRET_MAX-1] = 0;
2923 if (strcmp(p_integrity_alg, my_alg)) {
Philipp Reisner7204624c2011-03-15 18:51:47 +01002924 conn_err(tconn, "incompatible setting of the data-integrity-alg\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002925 goto disconnect;
2926 }
Philipp Reisner7204624c2011-03-15 18:51:47 +01002927 conn_info(tconn, "data-integrity-alg: %s\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -07002928 my_alg[0] ? my_alg : (unsigned char *)"<not-used>");
2929 }
2930
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002931 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002932
2933disconnect:
Philipp Reisner7204624c2011-03-15 18:51:47 +01002934 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002935 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002936}
2937
2938/* helper function
2939 * input: alg name, feature name
2940 * return: NULL (alg name was "")
2941 * ERR_PTR(error) if something goes wrong
2942 * or the crypto hash ptr, if it worked out ok. */
2943struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
2944 const char *alg, const char *name)
2945{
2946 struct crypto_hash *tfm;
2947
2948 if (!alg[0])
2949 return NULL;
2950
2951 tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
2952 if (IS_ERR(tfm)) {
2953 dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
2954 alg, name, PTR_ERR(tfm));
2955 return tfm;
2956 }
2957 if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) {
2958 crypto_free_hash(tfm);
2959 dev_err(DEV, "\"%s\" is not a digest (%s)\n", alg, name);
2960 return ERR_PTR(-EINVAL);
2961 }
2962 return tfm;
2963}
2964
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01002965static int receive_SyncParam(struct drbd_conf *mdev, enum drbd_packet cmd,
2966 unsigned int packet_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002967{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01002968 struct p_rs_param_95 *p = mdev->tconn->data.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002969 unsigned int header_size, data_size, exp_max_sz;
2970 struct crypto_hash *verify_tfm = NULL;
2971 struct crypto_hash *csums_tfm = NULL;
Philipp Reisner31890f42011-01-19 14:12:51 +01002972 const int apv = mdev->tconn->agreed_pro_version;
Philipp Reisner778f2712010-07-06 11:14:00 +02002973 int *rs_plan_s = NULL;
2974 int fifo_size = 0;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002975 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002976
2977 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
2978 : apv == 88 ? sizeof(struct p_rs_param)
2979 + SHARED_SECRET_MAX
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002980 : apv <= 94 ? sizeof(struct p_rs_param_89)
2981 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002982
Philipp Reisner02918be2010-08-20 14:35:10 +02002983 if (packet_size > exp_max_sz) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002984 dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
Philipp Reisner02918be2010-08-20 14:35:10 +02002985 packet_size, exp_max_sz);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002986 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002987 }
2988
2989 if (apv <= 88) {
Philipp Reisner257d0af2011-01-26 12:15:29 +01002990 header_size = sizeof(struct p_rs_param) - sizeof(struct p_header);
Philipp Reisner02918be2010-08-20 14:35:10 +02002991 data_size = packet_size - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002992 } else if (apv <= 94) {
Philipp Reisner257d0af2011-01-26 12:15:29 +01002993 header_size = sizeof(struct p_rs_param_89) - sizeof(struct p_header);
Philipp Reisner02918be2010-08-20 14:35:10 +02002994 data_size = packet_size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002995 D_ASSERT(data_size == 0);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02002996 } else {
Philipp Reisner257d0af2011-01-26 12:15:29 +01002997 header_size = sizeof(struct p_rs_param_95) - sizeof(struct p_header);
Philipp Reisner02918be2010-08-20 14:35:10 +02002998 data_size = packet_size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002999 D_ASSERT(data_size == 0);
3000 }
3001
3002 /* initialize verify_alg and csums_alg */
3003 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
3004
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003005 err = drbd_recv_all(mdev->tconn, &p->head.payload, header_size);
3006 if (err)
3007 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003008
Lars Ellenbergf3990022011-03-23 14:31:09 +01003009 if (get_ldev(mdev)) {
3010 mdev->ldev->dc.resync_rate = be32_to_cpu(p->rate);
3011 put_ldev(mdev);
3012 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003013
3014 if (apv >= 88) {
3015 if (apv == 88) {
3016 if (data_size > SHARED_SECRET_MAX) {
3017 dev_err(DEV, "verify-alg too long, "
3018 "peer wants %u, accepting only %u byte\n",
3019 data_size, SHARED_SECRET_MAX);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003020 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003021 }
3022
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003023 err = drbd_recv_all(mdev->tconn, p->verify_alg, data_size);
3024 if (err)
3025 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003026
3027 /* we expect NUL terminated string */
3028 /* but just in case someone tries to be evil */
3029 D_ASSERT(p->verify_alg[data_size-1] == 0);
3030 p->verify_alg[data_size-1] = 0;
3031
3032 } else /* apv >= 89 */ {
3033 /* we still expect NUL terminated strings */
3034 /* but just in case someone tries to be evil */
3035 D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
3036 D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
3037 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
3038 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
3039 }
3040
Lars Ellenbergf3990022011-03-23 14:31:09 +01003041 if (strcmp(mdev->tconn->net_conf->verify_alg, p->verify_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003042 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3043 dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
Lars Ellenbergf3990022011-03-23 14:31:09 +01003044 mdev->tconn->net_conf->verify_alg, p->verify_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003045 goto disconnect;
3046 }
3047 verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
3048 p->verify_alg, "verify-alg");
3049 if (IS_ERR(verify_tfm)) {
3050 verify_tfm = NULL;
3051 goto disconnect;
3052 }
3053 }
3054
Lars Ellenbergf3990022011-03-23 14:31:09 +01003055 if (apv >= 89 && strcmp(mdev->tconn->net_conf->csums_alg, p->csums_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003056 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3057 dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
Lars Ellenbergf3990022011-03-23 14:31:09 +01003058 mdev->tconn->net_conf->csums_alg, p->csums_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003059 goto disconnect;
3060 }
3061 csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
3062 p->csums_alg, "csums-alg");
3063 if (IS_ERR(csums_tfm)) {
3064 csums_tfm = NULL;
3065 goto disconnect;
3066 }
3067 }
3068
Lars Ellenbergf3990022011-03-23 14:31:09 +01003069 if (apv > 94 && get_ldev(mdev)) {
3070 mdev->ldev->dc.resync_rate = be32_to_cpu(p->rate);
3071 mdev->ldev->dc.c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
3072 mdev->ldev->dc.c_delay_target = be32_to_cpu(p->c_delay_target);
3073 mdev->ldev->dc.c_fill_target = be32_to_cpu(p->c_fill_target);
3074 mdev->ldev->dc.c_max_rate = be32_to_cpu(p->c_max_rate);
Philipp Reisner778f2712010-07-06 11:14:00 +02003075
Lars Ellenbergf3990022011-03-23 14:31:09 +01003076 fifo_size = (mdev->ldev->dc.c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Philipp Reisner778f2712010-07-06 11:14:00 +02003077 if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) {
3078 rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL);
3079 if (!rs_plan_s) {
3080 dev_err(DEV, "kmalloc of fifo_buffer failed");
Lars Ellenbergf3990022011-03-23 14:31:09 +01003081 put_ldev(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02003082 goto disconnect;
3083 }
3084 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01003085 put_ldev(mdev);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003086 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003087
3088 spin_lock(&mdev->peer_seq_lock);
3089 /* lock against drbd_nl_syncer_conf() */
3090 if (verify_tfm) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01003091 strcpy(mdev->tconn->net_conf->verify_alg, p->verify_alg);
3092 mdev->tconn->net_conf->verify_alg_len = strlen(p->verify_alg) + 1;
3093 crypto_free_hash(mdev->tconn->verify_tfm);
3094 mdev->tconn->verify_tfm = verify_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003095 dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
3096 }
3097 if (csums_tfm) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01003098 strcpy(mdev->tconn->net_conf->csums_alg, p->csums_alg);
3099 mdev->tconn->net_conf->csums_alg_len = strlen(p->csums_alg) + 1;
3100 crypto_free_hash(mdev->tconn->csums_tfm);
3101 mdev->tconn->csums_tfm = csums_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003102 dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
3103 }
Philipp Reisner778f2712010-07-06 11:14:00 +02003104 if (fifo_size != mdev->rs_plan_s.size) {
3105 kfree(mdev->rs_plan_s.values);
3106 mdev->rs_plan_s.values = rs_plan_s;
3107 mdev->rs_plan_s.size = fifo_size;
3108 mdev->rs_planed = 0;
3109 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003110 spin_unlock(&mdev->peer_seq_lock);
3111 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003112 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003113
Philipp Reisnerb411b362009-09-25 16:07:19 -07003114disconnect:
3115 /* just for completeness: actually not needed,
3116 * as this is not reached if csums_tfm was ok. */
3117 crypto_free_hash(csums_tfm);
3118 /* but free the verify_tfm again, if csums_tfm did not work out */
3119 crypto_free_hash(verify_tfm);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003120 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003121 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003122}
3123
Philipp Reisnerb411b362009-09-25 16:07:19 -07003124/* warn if the arguments differ by more than 12.5% */
3125static void warn_if_differ_considerably(struct drbd_conf *mdev,
3126 const char *s, sector_t a, sector_t b)
3127{
3128 sector_t d;
3129 if (a == 0 || b == 0)
3130 return;
3131 d = (a > b) ? (a - b) : (b - a);
3132 if (d > (a>>3) || d > (b>>3))
3133 dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
3134 (unsigned long long)a, (unsigned long long)b);
3135}
3136
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003137static int receive_sizes(struct drbd_conf *mdev, enum drbd_packet cmd,
3138 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003139{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01003140 struct p_sizes *p = mdev->tconn->data.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003141 enum determine_dev_size dd = unchanged;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003142 sector_t p_size, p_usize, my_usize;
3143 int ldsc = 0; /* local disk size changed */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003144 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003145
Philipp Reisnerb411b362009-09-25 16:07:19 -07003146 p_size = be64_to_cpu(p->d_size);
3147 p_usize = be64_to_cpu(p->u_size);
3148
Philipp Reisnerb411b362009-09-25 16:07:19 -07003149 /* just store the peer's disk size for now.
3150 * we still need to figure out whether we accept that. */
3151 mdev->p_size = p_size;
3152
Philipp Reisnerb411b362009-09-25 16:07:19 -07003153 if (get_ldev(mdev)) {
3154 warn_if_differ_considerably(mdev, "lower level device sizes",
3155 p_size, drbd_get_max_capacity(mdev->ldev));
3156 warn_if_differ_considerably(mdev, "user requested size",
3157 p_usize, mdev->ldev->dc.disk_size);
3158
3159 /* if this is the first connect, or an otherwise expected
3160 * param exchange, choose the minimum */
3161 if (mdev->state.conn == C_WF_REPORT_PARAMS)
3162 p_usize = min_not_zero((sector_t)mdev->ldev->dc.disk_size,
3163 p_usize);
3164
3165 my_usize = mdev->ldev->dc.disk_size;
3166
3167 if (mdev->ldev->dc.disk_size != p_usize) {
3168 mdev->ldev->dc.disk_size = p_usize;
3169 dev_info(DEV, "Peer sets u_size to %lu sectors\n",
3170 (unsigned long)mdev->ldev->dc.disk_size);
3171 }
3172
3173 /* Never shrink a device with usable data during connect.
3174 But allow online shrinking if we are connected. */
Philipp Reisnera393db62009-12-22 13:35:52 +01003175 if (drbd_new_dev_size(mdev, mdev->ldev, 0) <
Philipp Reisnerb411b362009-09-25 16:07:19 -07003176 drbd_get_capacity(mdev->this_bdev) &&
3177 mdev->state.disk >= D_OUTDATED &&
3178 mdev->state.conn < C_CONNECTED) {
3179 dev_err(DEV, "The peer's disk size is too small!\n");
Philipp Reisner38fa9982011-03-15 18:24:49 +01003180 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003181 mdev->ldev->dc.disk_size = my_usize;
3182 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003183 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003184 }
3185 put_ldev(mdev);
3186 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003187
Philipp Reisnere89b5912010-03-24 17:11:33 +01003188 ddsf = be16_to_cpu(p->dds_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003189 if (get_ldev(mdev)) {
Bart Van Assche24c48302011-05-21 18:32:29 +02003190 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003191 put_ldev(mdev);
3192 if (dd == dev_size_error)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003193 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003194 drbd_md_sync(mdev);
3195 } else {
3196 /* I am diskless, need to accept the peer's size. */
3197 drbd_set_my_capacity(mdev, p_size);
3198 }
3199
Philipp Reisner99432fc2011-05-20 16:39:13 +02003200 mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size);
3201 drbd_reconsider_max_bio_size(mdev);
3202
Philipp Reisnerb411b362009-09-25 16:07:19 -07003203 if (get_ldev(mdev)) {
3204 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
3205 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
3206 ldsc = 1;
3207 }
3208
Philipp Reisnerb411b362009-09-25 16:07:19 -07003209 put_ldev(mdev);
3210 }
3211
3212 if (mdev->state.conn > C_WF_REPORT_PARAMS) {
3213 if (be64_to_cpu(p->c_size) !=
3214 drbd_get_capacity(mdev->this_bdev) || ldsc) {
3215 /* we have different sizes, probably peer
3216 * needs to know my new size... */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003217 drbd_send_sizes(mdev, 0, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003218 }
3219 if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
3220 (dd == grew && mdev->state.conn == C_CONNECTED)) {
3221 if (mdev->state.pdsk >= D_INCONSISTENT &&
Philipp Reisnere89b5912010-03-24 17:11:33 +01003222 mdev->state.disk >= D_INCONSISTENT) {
3223 if (ddsf & DDSF_NO_RESYNC)
3224 dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
3225 else
3226 resync_after_online_grow(mdev);
3227 } else
Philipp Reisnerb411b362009-09-25 16:07:19 -07003228 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
3229 }
3230 }
3231
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003232 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003233}
3234
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003235static int receive_uuids(struct drbd_conf *mdev, enum drbd_packet cmd,
3236 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003237{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01003238 struct p_uuids *p = mdev->tconn->data.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003239 u64 *p_uuid;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003240 int i, updated_uuids = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003241
Philipp Reisnerb411b362009-09-25 16:07:19 -07003242 p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
3243
3244 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
3245 p_uuid[i] = be64_to_cpu(p->uuid[i]);
3246
3247 kfree(mdev->p_uuid);
3248 mdev->p_uuid = p_uuid;
3249
3250 if (mdev->state.conn < C_CONNECTED &&
3251 mdev->state.disk < D_INCONSISTENT &&
3252 mdev->state.role == R_PRIMARY &&
3253 (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
3254 dev_err(DEV, "Can only connect to data with current UUID=%016llX\n",
3255 (unsigned long long)mdev->ed_uuid);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003256 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003257 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003258 }
3259
3260 if (get_ldev(mdev)) {
3261 int skip_initial_sync =
3262 mdev->state.conn == C_CONNECTED &&
Philipp Reisner31890f42011-01-19 14:12:51 +01003263 mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003264 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
3265 (p_uuid[UI_FLAGS] & 8);
3266 if (skip_initial_sync) {
3267 dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n");
3268 drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003269 "clear_n_write from receive_uuids",
3270 BM_LOCKED_TEST_ALLOWED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003271 _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]);
3272 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3273 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3274 CS_VERBOSE, NULL);
3275 drbd_md_sync(mdev);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003276 updated_uuids = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003277 }
3278 put_ldev(mdev);
Philipp Reisner18a50fa2010-06-21 14:14:15 +02003279 } else if (mdev->state.disk < D_INCONSISTENT &&
3280 mdev->state.role == R_PRIMARY) {
3281 /* I am a diskless primary, the peer just created a new current UUID
3282 for me. */
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003283 updated_uuids = drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003284 }
3285
3286 /* Before we test for the disk state, we should wait until an eventually
3287 ongoing cluster wide state change is finished. That is important if
3288 we are primary and are detaching from our disk. We need to see the
3289 new disk state... */
Philipp Reisner8410da82011-02-11 20:11:10 +01003290 mutex_lock(mdev->state_mutex);
3291 mutex_unlock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003292 if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT)
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003293 updated_uuids |= drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
3294
3295 if (updated_uuids)
3296 drbd_print_uuids(mdev, "receiver updated UUIDs to");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003297
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003298 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003299}
3300
3301/**
3302 * convert_state() - Converts the peer's view of the cluster state to our point of view
3303 * @ps: The state as seen by the peer.
3304 */
3305static union drbd_state convert_state(union drbd_state ps)
3306{
3307 union drbd_state ms;
3308
3309 static enum drbd_conns c_tab[] = {
3310 [C_CONNECTED] = C_CONNECTED,
3311
3312 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
3313 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
3314 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
3315 [C_VERIFY_S] = C_VERIFY_T,
3316 [C_MASK] = C_MASK,
3317 };
3318
3319 ms.i = ps.i;
3320
3321 ms.conn = c_tab[ps.conn];
3322 ms.peer = ps.role;
3323 ms.role = ps.peer;
3324 ms.pdsk = ps.disk;
3325 ms.disk = ps.pdsk;
3326 ms.peer_isp = (ps.aftr_isp | ps.user_isp);
3327
3328 return ms;
3329}
3330
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003331static int receive_req_state(struct drbd_conf *mdev, enum drbd_packet cmd,
3332 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003333{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01003334 struct p_req_state *p = mdev->tconn->data.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003335 union drbd_state mask, val;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01003336 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003337
Philipp Reisnerb411b362009-09-25 16:07:19 -07003338 mask.i = be32_to_cpu(p->mask);
3339 val.i = be32_to_cpu(p->val);
3340
Philipp Reisner25703f82011-02-07 14:35:25 +01003341 if (test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags) &&
Philipp Reisner8410da82011-02-11 20:11:10 +01003342 mutex_is_locked(mdev->state_mutex)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003343 drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003344 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003345 }
3346
3347 mask = convert_state(mask);
3348 val = convert_state(val);
3349
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003350 rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
3351 drbd_send_sr_reply(mdev, rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003352
Philipp Reisnerb411b362009-09-25 16:07:19 -07003353 drbd_md_sync(mdev);
3354
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003355 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003356}
3357
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003358static int receive_req_conn_state(struct drbd_tconn *tconn, enum drbd_packet cmd,
3359 unsigned int data_size)
3360{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01003361 struct p_req_state *p = tconn->data.rbuf;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003362 union drbd_state mask, val;
3363 enum drbd_state_rv rv;
3364
3365 mask.i = be32_to_cpu(p->mask);
3366 val.i = be32_to_cpu(p->val);
3367
3368 if (test_bit(DISCARD_CONCURRENT, &tconn->flags) &&
3369 mutex_is_locked(&tconn->cstate_mutex)) {
3370 conn_send_sr_reply(tconn, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003371 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003372 }
3373
3374 mask = convert_state(mask);
3375 val = convert_state(val);
3376
3377 rv = conn_request_state(tconn, mask, val, CS_VERBOSE | CS_LOCAL_ONLY);
3378 conn_send_sr_reply(tconn, rv);
3379
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003380 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003381}
3382
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003383static int receive_state(struct drbd_conf *mdev, enum drbd_packet cmd,
3384 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003385{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01003386 struct p_state *p = mdev->tconn->data.rbuf;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003387 union drbd_state os, ns, peer_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003388 enum drbd_disk_state real_peer_disk;
Philipp Reisner65d922c2010-06-16 16:18:09 +02003389 enum chg_state_flags cs_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003390 int rv;
3391
Philipp Reisnerb411b362009-09-25 16:07:19 -07003392 peer_state.i = be32_to_cpu(p->state);
3393
3394 real_peer_disk = peer_state.disk;
3395 if (peer_state.disk == D_NEGOTIATING) {
3396 real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
3397 dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
3398 }
3399
Philipp Reisner87eeee42011-01-19 14:16:30 +01003400 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003401 retry:
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003402 os = ns = mdev->state;
Philipp Reisner87eeee42011-01-19 14:16:30 +01003403 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003404
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003405 /* peer says his disk is uptodate, while we think it is inconsistent,
3406 * and this happens while we think we have a sync going on. */
3407 if (os.pdsk == D_INCONSISTENT && real_peer_disk == D_UP_TO_DATE &&
3408 os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) {
3409 /* If we are (becoming) SyncSource, but peer is still in sync
3410 * preparation, ignore its uptodate-ness to avoid flapping, it
3411 * will change to inconsistent once the peer reaches active
3412 * syncing states.
3413 * It may have changed syncer-paused flags, however, so we
3414 * cannot ignore this completely. */
3415 if (peer_state.conn > C_CONNECTED &&
3416 peer_state.conn < C_SYNC_SOURCE)
3417 real_peer_disk = D_INCONSISTENT;
3418
3419 /* if peer_state changes to connected at the same time,
3420 * it explicitly notifies us that it finished resync.
3421 * Maybe we should finish it up, too? */
3422 else if (os.conn >= C_SYNC_SOURCE &&
3423 peer_state.conn == C_CONNECTED) {
3424 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed)
3425 drbd_resync_finished(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003426 return 0;
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003427 }
3428 }
3429
3430 /* peer says his disk is inconsistent, while we think it is uptodate,
3431 * and this happens while the peer still thinks we have a sync going on,
3432 * but we think we are already done with the sync.
3433 * We ignore this to avoid flapping pdsk.
3434 * This should not happen, if the peer is a recent version of drbd. */
3435 if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT &&
3436 os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE)
3437 real_peer_disk = D_UP_TO_DATE;
3438
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003439 if (ns.conn == C_WF_REPORT_PARAMS)
3440 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003441
Philipp Reisner67531712010-10-27 12:21:30 +02003442 if (peer_state.conn == C_AHEAD)
3443 ns.conn = C_BEHIND;
3444
Philipp Reisnerb411b362009-09-25 16:07:19 -07003445 if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING &&
3446 get_ldev_if_state(mdev, D_NEGOTIATING)) {
3447 int cr; /* consider resync */
3448
3449 /* if we established a new connection */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003450 cr = (os.conn < C_CONNECTED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003451 /* if we had an established connection
3452 * and one of the nodes newly attaches a disk */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003453 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003454 (peer_state.disk == D_NEGOTIATING ||
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003455 os.disk == D_NEGOTIATING));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003456 /* if we have both been inconsistent, and the peer has been
3457 * forced to be UpToDate with --overwrite-data */
3458 cr |= test_bit(CONSIDER_RESYNC, &mdev->flags);
3459 /* if we had been plain connected, and the admin requested to
3460 * start a sync by "invalidate" or "invalidate-remote" */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003461 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003462 (peer_state.conn >= C_STARTING_SYNC_S &&
3463 peer_state.conn <= C_WF_BITMAP_T));
3464
3465 if (cr)
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003466 ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003467
3468 put_ldev(mdev);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003469 if (ns.conn == C_MASK) {
3470 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003471 if (mdev->state.disk == D_NEGOTIATING) {
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003472 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003473 } else if (peer_state.disk == D_NEGOTIATING) {
3474 dev_err(DEV, "Disk attach process on the peer node was aborted.\n");
3475 peer_state.disk = D_DISKLESS;
Lars Ellenberg580b9762010-02-26 23:15:23 +01003476 real_peer_disk = D_DISKLESS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003477 } else {
Philipp Reisner8169e412011-03-15 18:40:27 +01003478 if (test_and_clear_bit(CONN_DRY_RUN, &mdev->tconn->flags))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003479 return -EIO;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003480 D_ASSERT(os.conn == C_WF_REPORT_PARAMS);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003481 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003482 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003483 }
3484 }
3485 }
3486
Philipp Reisner87eeee42011-01-19 14:16:30 +01003487 spin_lock_irq(&mdev->tconn->req_lock);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003488 if (mdev->state.i != os.i)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003489 goto retry;
3490 clear_bit(CONSIDER_RESYNC, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003491 ns.peer = peer_state.role;
3492 ns.pdsk = real_peer_disk;
3493 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003494 if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003495 ns.disk = mdev->new_state_tmp.disk;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003496 cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD);
3497 if (ns.pdsk == D_CONSISTENT && is_susp(ns) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED &&
Philipp Reisner481c6f52010-06-22 14:03:27 +02003498 test_bit(NEW_CUR_UUID, &mdev->flags)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01003499 /* Do not allow tl_restart(RESEND) for a rebooted peer. We can only allow this
Philipp Reisner481c6f52010-06-22 14:03:27 +02003500 for temporal network outages! */
Philipp Reisner87eeee42011-01-19 14:16:30 +01003501 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003502 dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01003503 tl_clear(mdev->tconn);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003504 drbd_uuid_new_current(mdev);
3505 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003506 conn_request_state(mdev->tconn, NS2(conn, C_PROTOCOL_ERROR, susp, 0), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003507 return -EIO;
Philipp Reisner481c6f52010-06-22 14:03:27 +02003508 }
Philipp Reisner65d922c2010-06-16 16:18:09 +02003509 rv = _drbd_set_state(mdev, ns, cs_flags, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003510 ns = mdev->state;
Philipp Reisner87eeee42011-01-19 14:16:30 +01003511 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003512
3513 if (rv < SS_SUCCESS) {
Philipp Reisner38fa9982011-03-15 18:24:49 +01003514 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003515 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003516 }
3517
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003518 if (os.conn > C_WF_REPORT_PARAMS) {
3519 if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003520 peer_state.disk != D_NEGOTIATING ) {
3521 /* we want resync, peer has not yet decided to sync... */
3522 /* Nowadays only used when forcing a node into primary role and
3523 setting its disk to UpToDate with that */
3524 drbd_send_uuids(mdev);
3525 drbd_send_state(mdev);
3526 }
3527 }
3528
Philipp Reisner89e58e72011-01-19 13:12:45 +01003529 mdev->tconn->net_conf->want_lose = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003530
3531 drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
3532
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003533 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003534}
3535
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003536static int receive_sync_uuid(struct drbd_conf *mdev, enum drbd_packet cmd,
3537 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003538{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01003539 struct p_rs_uuid *p = mdev->tconn->data.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003540
3541 wait_event(mdev->misc_wait,
3542 mdev->state.conn == C_WF_SYNC_UUID ||
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02003543 mdev->state.conn == C_BEHIND ||
Philipp Reisnerb411b362009-09-25 16:07:19 -07003544 mdev->state.conn < C_CONNECTED ||
3545 mdev->state.disk < D_NEGOTIATING);
3546
3547 /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */
3548
Philipp Reisnerb411b362009-09-25 16:07:19 -07003549 /* Here the _drbd_uuid_ functions are right, current should
3550 _not_ be rotated into the history */
3551 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
3552 _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid));
3553 _drbd_uuid_set(mdev, UI_BITMAP, 0UL);
3554
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003555 drbd_print_uuids(mdev, "updated sync uuid");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003556 drbd_start_resync(mdev, C_SYNC_TARGET);
3557
3558 put_ldev(mdev);
3559 } else
3560 dev_err(DEV, "Ignoring SyncUUID packet!\n");
3561
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003562 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003563}
3564
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003565/**
3566 * receive_bitmap_plain
3567 *
3568 * Return 0 when done, 1 when another iteration is needed, and a negative error
3569 * code upon failure.
3570 */
3571static int
Philipp Reisner02918be2010-08-20 14:35:10 +02003572receive_bitmap_plain(struct drbd_conf *mdev, unsigned int data_size,
Andreas Gruenbacherfc568152011-03-24 21:23:50 +01003573 struct p_header *h, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003574{
Andreas Gruenbacherfc568152011-03-24 21:23:50 +01003575 unsigned long *buffer = (unsigned long *)h->payload;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003576 unsigned num_words = min_t(size_t, BM_PACKET_WORDS, c->bm_words - c->word_offset);
3577 unsigned want = num_words * sizeof(long);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003578 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003579
Philipp Reisner02918be2010-08-20 14:35:10 +02003580 if (want != data_size) {
3581 dev_err(DEV, "%s:want (%u) != data_size (%u)\n", __func__, want, data_size);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003582 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003583 }
3584 if (want == 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003585 return 0;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003586 err = drbd_recv_all(mdev->tconn, buffer, want);
3587 if (err)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003588 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003589
3590 drbd_bm_merge_lel(mdev, c->word_offset, num_words, buffer);
3591
3592 c->word_offset += num_words;
3593 c->bit_offset = c->word_offset * BITS_PER_LONG;
3594 if (c->bit_offset > c->bm_bits)
3595 c->bit_offset = c->bm_bits;
3596
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003597 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003598}
3599
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003600static enum drbd_bitmap_code dcbp_get_code(struct p_compressed_bm *p)
3601{
3602 return (enum drbd_bitmap_code)(p->encoding & 0x0f);
3603}
3604
3605static int dcbp_get_start(struct p_compressed_bm *p)
3606{
3607 return (p->encoding & 0x80) != 0;
3608}
3609
3610static int dcbp_get_pad_bits(struct p_compressed_bm *p)
3611{
3612 return (p->encoding >> 4) & 0x7;
3613}
3614
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003615/**
3616 * recv_bm_rle_bits
3617 *
3618 * Return 0 when done, 1 when another iteration is needed, and a negative error
3619 * code upon failure.
3620 */
3621static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07003622recv_bm_rle_bits(struct drbd_conf *mdev,
3623 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003624 struct bm_xfer_ctx *c,
3625 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003626{
3627 struct bitstream bs;
3628 u64 look_ahead;
3629 u64 rl;
3630 u64 tmp;
3631 unsigned long s = c->bit_offset;
3632 unsigned long e;
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003633 int toggle = dcbp_get_start(p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003634 int have;
3635 int bits;
3636
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003637 bitstream_init(&bs, p->code, len, dcbp_get_pad_bits(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003638
3639 bits = bitstream_get_bits(&bs, &look_ahead, 64);
3640 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003641 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003642
3643 for (have = bits; have > 0; s += rl, toggle = !toggle) {
3644 bits = vli_decode_bits(&rl, look_ahead);
3645 if (bits <= 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003646 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003647
3648 if (toggle) {
3649 e = s + rl -1;
3650 if (e >= c->bm_bits) {
3651 dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003652 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003653 }
3654 _drbd_bm_set_bits(mdev, s, e);
3655 }
3656
3657 if (have < bits) {
3658 dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
3659 have, bits, look_ahead,
3660 (unsigned int)(bs.cur.b - p->code),
3661 (unsigned int)bs.buf_len);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003662 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003663 }
3664 look_ahead >>= bits;
3665 have -= bits;
3666
3667 bits = bitstream_get_bits(&bs, &tmp, 64 - have);
3668 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003669 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003670 look_ahead |= tmp << have;
3671 have += bits;
3672 }
3673
3674 c->bit_offset = s;
3675 bm_xfer_ctx_bit_to_word_offset(c);
3676
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003677 return (s != c->bm_bits);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003678}
3679
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003680/**
3681 * decode_bitmap_c
3682 *
3683 * Return 0 when done, 1 when another iteration is needed, and a negative error
3684 * code upon failure.
3685 */
3686static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07003687decode_bitmap_c(struct drbd_conf *mdev,
3688 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003689 struct bm_xfer_ctx *c,
3690 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003691{
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003692 if (dcbp_get_code(p) == RLE_VLI_Bits)
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003693 return recv_bm_rle_bits(mdev, p, c, len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003694
3695 /* other variants had been implemented for evaluation,
3696 * but have been dropped as this one turned out to be "best"
3697 * during all our tests. */
3698
3699 dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003700 conn_request_state(mdev->tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003701 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003702}
3703
3704void INFO_bm_xfer_stats(struct drbd_conf *mdev,
3705 const char *direction, struct bm_xfer_ctx *c)
3706{
3707 /* what would it take to transfer it "plaintext" */
Philipp Reisnerc0129492011-01-19 16:58:16 +01003708 unsigned plain = sizeof(struct p_header) *
Philipp Reisnerb411b362009-09-25 16:07:19 -07003709 ((c->bm_words+BM_PACKET_WORDS-1)/BM_PACKET_WORDS+1)
3710 + c->bm_words * sizeof(long);
3711 unsigned total = c->bytes[0] + c->bytes[1];
3712 unsigned r;
3713
3714 /* total can not be zero. but just in case: */
3715 if (total == 0)
3716 return;
3717
3718 /* don't report if not compressed */
3719 if (total >= plain)
3720 return;
3721
3722 /* total < plain. check for overflow, still */
3723 r = (total > UINT_MAX/1000) ? (total / (plain/1000))
3724 : (1000 * total / plain);
3725
3726 if (r > 1000)
3727 r = 1000;
3728
3729 r = 1000 - r;
3730 dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
3731 "total %u; compression: %u.%u%%\n",
3732 direction,
3733 c->bytes[1], c->packets[1],
3734 c->bytes[0], c->packets[0],
3735 total, r/10, r % 10);
3736}
3737
3738/* Since we are processing the bitfield from lower addresses to higher,
3739 it does not matter if the process it in 32 bit chunks or 64 bit
3740 chunks as long as it is little endian. (Understand it as byte stream,
3741 beginning with the lowest byte...) If we would use big endian
3742 we would need to process it from the highest address to the lowest,
3743 in order to be agnostic to the 32 vs 64 bits issue.
3744
3745 returns 0 on failure, 1 if we successfully received it. */
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003746static int receive_bitmap(struct drbd_conf *mdev, enum drbd_packet cmd,
3747 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003748{
3749 struct bm_xfer_ctx c;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003750 int err;
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01003751 struct p_header *h = mdev->tconn->data.rbuf;
Philipp Reisner77351055b2011-02-07 17:24:26 +01003752 struct packet_info pi;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003753
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003754 drbd_bm_lock(mdev, "receive bitmap", BM_LOCKED_SET_ALLOWED);
3755 /* you are supposed to send additional out-of-sync information
3756 * if you actually set bits during this phase */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003757
Philipp Reisnerb411b362009-09-25 16:07:19 -07003758 c = (struct bm_xfer_ctx) {
3759 .bm_bits = drbd_bm_bits(mdev),
3760 .bm_words = drbd_bm_words(mdev),
3761 };
3762
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003763 for(;;) {
Philipp Reisner02918be2010-08-20 14:35:10 +02003764 if (cmd == P_BITMAP) {
Andreas Gruenbacherfc568152011-03-24 21:23:50 +01003765 err = receive_bitmap_plain(mdev, data_size, h, &c);
Philipp Reisner02918be2010-08-20 14:35:10 +02003766 } else if (cmd == P_COMPRESSED_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003767 /* MAYBE: sanity check that we speak proto >= 90,
3768 * and the feature is enabled! */
3769 struct p_compressed_bm *p;
3770
Philipp Reisner02918be2010-08-20 14:35:10 +02003771 if (data_size > BM_PACKET_PAYLOAD_BYTES) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003772 dev_err(DEV, "ReportCBitmap packet too large\n");
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003773 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003774 goto out;
3775 }
Andreas Gruenbacherfc568152011-03-24 21:23:50 +01003776
3777 p = mdev->tconn->data.rbuf;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003778 err = drbd_recv_all(mdev->tconn, p->head.payload, data_size);
3779 if (err)
3780 goto out;
Lars Ellenberg004352f2010-10-05 20:13:58 +02003781 if (data_size <= (sizeof(*p) - sizeof(p->head))) {
3782 dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", data_size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003783 err = -EIO;
Andreas Gruenbacher78fcbda2010-12-10 22:18:27 +01003784 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003785 }
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003786 err = decode_bitmap_c(mdev, p, &c, data_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003787 } else {
Philipp Reisner02918be2010-08-20 14:35:10 +02003788 dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", cmd);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003789 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003790 goto out;
3791 }
3792
Philipp Reisner02918be2010-08-20 14:35:10 +02003793 c.packets[cmd == P_BITMAP]++;
Philipp Reisner257d0af2011-01-26 12:15:29 +01003794 c.bytes[cmd == P_BITMAP] += sizeof(struct p_header) + data_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003795
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003796 if (err <= 0) {
3797 if (err < 0)
3798 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003799 break;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003800 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003801 err = drbd_recv_header(mdev->tconn, &pi);
3802 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003803 goto out;
Philipp Reisner77351055b2011-02-07 17:24:26 +01003804 cmd = pi.cmd;
3805 data_size = pi.size;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003806 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003807
3808 INFO_bm_xfer_stats(mdev, "receive", &c);
3809
3810 if (mdev->state.conn == C_WF_BITMAP_T) {
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01003811 enum drbd_state_rv rv;
3812
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003813 err = drbd_send_bitmap(mdev);
3814 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003815 goto out;
3816 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01003817 rv = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
3818 D_ASSERT(rv == SS_SUCCESS);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003819 } else if (mdev->state.conn != C_WF_BITMAP_S) {
3820 /* admin may have requested C_DISCONNECTING,
3821 * other threads may have noticed network errors */
3822 dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n",
3823 drbd_conn_str(mdev->state.conn));
3824 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003825 err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003826
Philipp Reisnerb411b362009-09-25 16:07:19 -07003827 out:
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003828 drbd_bm_unlock(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003829 if (!err && mdev->state.conn == C_WF_BITMAP_S)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003830 drbd_start_resync(mdev, C_SYNC_SOURCE);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003831 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003832}
3833
Philipp Reisner2de876e2011-03-15 14:38:01 +01003834static int _tconn_receive_skip(struct drbd_tconn *tconn, unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003835{
3836 /* TODO zero copy sink :) */
3837 static char sink[128];
3838 int size, want, r;
3839
Philipp Reisner02918be2010-08-20 14:35:10 +02003840 size = data_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003841 while (size > 0) {
3842 want = min_t(int, size, sizeof(sink));
Philipp Reisner2de876e2011-03-15 14:38:01 +01003843 r = drbd_recv(tconn, sink, want);
3844 if (r <= 0)
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01003845 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003846 size -= r;
3847 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003848 return size ? -EIO : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003849}
3850
Philipp Reisner2de876e2011-03-15 14:38:01 +01003851static int receive_skip(struct drbd_conf *mdev, enum drbd_packet cmd,
3852 unsigned int data_size)
3853{
3854 dev_warn(DEV, "skipping unknown optional packet type %d, l: %d!\n",
3855 cmd, data_size);
3856
3857 return _tconn_receive_skip(mdev->tconn, data_size);
3858}
3859
3860static int tconn_receive_skip(struct drbd_tconn *tconn, enum drbd_packet cmd, unsigned int data_size)
3861{
3862 conn_warn(tconn, "skipping packet for non existing volume type %d, l: %d!\n",
3863 cmd, data_size);
3864
3865 return _tconn_receive_skip(tconn, data_size);
3866}
3867
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003868static int receive_UnplugRemote(struct drbd_conf *mdev, enum drbd_packet cmd,
3869 unsigned int data_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003870{
Philipp Reisnerb411b362009-09-25 16:07:19 -07003871 /* Make sure we've acked all the TCP data associated
3872 * with the data requests being unplugged */
Philipp Reisnere42325a2011-01-19 13:55:45 +01003873 drbd_tcp_quickack(mdev->tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003874
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003875 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003876}
3877
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01003878static int receive_out_of_sync(struct drbd_conf *mdev, enum drbd_packet cmd,
3879 unsigned int data_size)
Philipp Reisner73a01a12010-10-27 14:33:00 +02003880{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01003881 struct p_block_desc *p = mdev->tconn->data.rbuf;
Philipp Reisner73a01a12010-10-27 14:33:00 +02003882
Lars Ellenbergf735e3632010-12-17 21:06:18 +01003883 switch (mdev->state.conn) {
3884 case C_WF_SYNC_UUID:
3885 case C_WF_BITMAP_T:
3886 case C_BEHIND:
3887 break;
3888 default:
3889 dev_err(DEV, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n",
3890 drbd_conn_str(mdev->state.conn));
3891 }
3892
Philipp Reisner73a01a12010-10-27 14:33:00 +02003893 drbd_set_out_of_sync(mdev, be64_to_cpu(p->sector), be32_to_cpu(p->blksize));
3894
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003895 return 0;
Philipp Reisner73a01a12010-10-27 14:33:00 +02003896}
3897
Philipp Reisner02918be2010-08-20 14:35:10 +02003898struct data_cmd {
3899 int expect_payload;
3900 size_t pkt_size;
Philipp Reisnera4fbda82011-03-16 11:13:17 +01003901 enum mdev_or_conn fa_type; /* first argument's type */
Philipp Reisnerd9ae84e2011-03-15 18:50:22 +01003902 union {
3903 int (*mdev_fn)(struct drbd_conf *, enum drbd_packet cmd,
3904 unsigned int to_receive);
3905 int (*conn_fn)(struct drbd_tconn *, enum drbd_packet cmd,
3906 unsigned int to_receive);
3907 };
Philipp Reisnerb411b362009-09-25 16:07:19 -07003908};
3909
Philipp Reisner02918be2010-08-20 14:35:10 +02003910static struct data_cmd drbd_cmd_handler[] = {
Philipp Reisnerd9ae84e2011-03-15 18:50:22 +01003911 [P_DATA] = { 1, sizeof(struct p_data), MDEV, { receive_Data } },
3912 [P_DATA_REPLY] = { 1, sizeof(struct p_data), MDEV, { receive_DataReply } },
3913 [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), MDEV, { receive_RSDataReply } } ,
3914 [P_BARRIER] = { 0, sizeof(struct p_barrier), MDEV, { receive_Barrier } } ,
3915 [P_BITMAP] = { 1, sizeof(struct p_header), MDEV, { receive_bitmap } } ,
3916 [P_COMPRESSED_BITMAP] = { 1, sizeof(struct p_header), MDEV, { receive_bitmap } } ,
3917 [P_UNPLUG_REMOTE] = { 0, sizeof(struct p_header), MDEV, { receive_UnplugRemote } },
3918 [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), MDEV, { receive_DataRequest } },
3919 [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), MDEV, { receive_DataRequest } },
3920 [P_SYNC_PARAM] = { 1, sizeof(struct p_header), MDEV, { receive_SyncParam } },
3921 [P_SYNC_PARAM89] = { 1, sizeof(struct p_header), MDEV, { receive_SyncParam } },
Philipp Reisner7204624c2011-03-15 18:51:47 +01003922 [P_PROTOCOL] = { 1, sizeof(struct p_protocol), CONN, { .conn_fn = receive_protocol } },
Philipp Reisnerd9ae84e2011-03-15 18:50:22 +01003923 [P_UUIDS] = { 0, sizeof(struct p_uuids), MDEV, { receive_uuids } },
3924 [P_SIZES] = { 0, sizeof(struct p_sizes), MDEV, { receive_sizes } },
3925 [P_STATE] = { 0, sizeof(struct p_state), MDEV, { receive_state } },
3926 [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), MDEV, { receive_req_state } },
3927 [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), MDEV, { receive_sync_uuid } },
3928 [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), MDEV, { receive_DataRequest } },
3929 [P_OV_REPLY] = { 1, sizeof(struct p_block_req), MDEV, { receive_DataRequest } },
3930 [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), MDEV, { receive_DataRequest } },
3931 [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), MDEV, { receive_skip } },
3932 [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), MDEV, { receive_out_of_sync } },
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003933 [P_CONN_ST_CHG_REQ] = { 0, sizeof(struct p_req_state), CONN, { .conn_fn = receive_req_conn_state } },
Philipp Reisner02918be2010-08-20 14:35:10 +02003934};
3935
Philipp Reisnereefc2f72011-02-08 12:55:24 +01003936static void drbdd(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003937{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01003938 struct p_header *header = tconn->data.rbuf;
Philipp Reisner77351055b2011-02-07 17:24:26 +01003939 struct packet_info pi;
Philipp Reisner02918be2010-08-20 14:35:10 +02003940 size_t shs; /* sub header size */
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003941 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003942
Philipp Reisnereefc2f72011-02-08 12:55:24 +01003943 while (get_t_state(&tconn->receiver) == RUNNING) {
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01003944 struct data_cmd *cmd;
3945
Philipp Reisnereefc2f72011-02-08 12:55:24 +01003946 drbd_thread_current_set_cpu(&tconn->receiver);
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01003947 if (drbd_recv_header(tconn, &pi))
Philipp Reisner02918be2010-08-20 14:35:10 +02003948 goto err_out;
3949
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01003950 cmd = &drbd_cmd_handler[pi.cmd];
3951 if (unlikely(pi.cmd >= ARRAY_SIZE(drbd_cmd_handler) || !cmd->mdev_fn)) {
Philipp Reisnereefc2f72011-02-08 12:55:24 +01003952 conn_err(tconn, "unknown packet type %d, l: %d!\n", pi.cmd, pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02003953 goto err_out;
Lars Ellenberg0b33a912009-11-16 15:58:04 +01003954 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003955
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01003956 shs = cmd->pkt_size - sizeof(struct p_header);
3957 if (pi.size - shs > 0 && !cmd->expect_payload) {
Philipp Reisnereefc2f72011-02-08 12:55:24 +01003958 conn_err(tconn, "No payload expected %s l:%d\n", cmdname(pi.cmd), pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02003959 goto err_out;
3960 }
3961
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02003962 if (shs) {
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01003963 err = drbd_recv_all_warn(tconn, &header->payload, shs);
3964 if (err)
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02003965 goto err_out;
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02003966 }
3967
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01003968 if (cmd->fa_type == CONN) {
3969 err = cmd->conn_fn(tconn, pi.cmd, pi.size - shs);
Philipp Reisnerd9ae84e2011-03-15 18:50:22 +01003970 } else {
3971 struct drbd_conf *mdev = vnr_to_mdev(tconn, pi.vnr);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003972 err = mdev ?
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01003973 cmd->mdev_fn(mdev, pi.cmd, pi.size - shs) :
Philipp Reisnerd9ae84e2011-03-15 18:50:22 +01003974 tconn_receive_skip(tconn, pi.cmd, pi.size - shs);
3975 }
Philipp Reisner02918be2010-08-20 14:35:10 +02003976
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003977 if (unlikely(err)) {
Philipp Reisnereefc2f72011-02-08 12:55:24 +01003978 conn_err(tconn, "error receiving %s, l: %d!\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01003979 cmdname(pi.cmd), pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02003980 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003981 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003982 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003983 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003984
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003985 err_out:
3986 conn_request_state(tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003987}
3988
Philipp Reisner0e29d162011-02-18 14:23:11 +01003989void conn_flush_workqueue(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003990{
3991 struct drbd_wq_barrier barr;
3992
3993 barr.w.cb = w_prev_work_done;
Philipp Reisner0e29d162011-02-18 14:23:11 +01003994 barr.w.tconn = tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003995 init_completion(&barr.done);
Philipp Reisner0e29d162011-02-18 14:23:11 +01003996 drbd_queue_work(&tconn->data.work, &barr.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003997 wait_for_completion(&barr.done);
3998}
3999
Philipp Reisner360cc742011-02-08 14:29:53 +01004000static void drbd_disconnect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004001{
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004002 enum drbd_conns oc;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004003 int rv = SS_UNKNOWN_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004004
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004005 if (tconn->cstate == C_STANDALONE)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004006 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004007
4008 /* asender does not clean up anything. it must not interfere, either */
Philipp Reisner360cc742011-02-08 14:29:53 +01004009 drbd_thread_stop(&tconn->asender);
4010 drbd_free_sock(tconn);
4011
4012 idr_for_each(&tconn->volumes, drbd_disconnected, tconn);
Philipp Reisner360cc742011-02-08 14:29:53 +01004013 conn_info(tconn, "Connection closed\n");
4014
Philipp Reisnercb703452011-03-24 11:03:07 +01004015 if (conn_highest_role(tconn) == R_PRIMARY && conn_highest_pdsk(tconn) >= D_UNKNOWN)
4016 conn_try_outdate_peer_async(tconn);
4017
Philipp Reisner360cc742011-02-08 14:29:53 +01004018 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004019 oc = tconn->cstate;
4020 if (oc >= C_UNCONNECTED)
4021 rv = _conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
4022
Philipp Reisner360cc742011-02-08 14:29:53 +01004023 spin_unlock_irq(&tconn->req_lock);
4024
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004025 if (oc == C_DISCONNECTING) {
Philipp Reisner360cc742011-02-08 14:29:53 +01004026 wait_event(tconn->net_cnt_wait, atomic_read(&tconn->net_cnt) == 0);
4027
4028 crypto_free_hash(tconn->cram_hmac_tfm);
4029 tconn->cram_hmac_tfm = NULL;
4030
4031 kfree(tconn->net_conf);
4032 tconn->net_conf = NULL;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004033 conn_request_state(tconn, NS(conn, C_STANDALONE), CS_VERBOSE);
Philipp Reisner360cc742011-02-08 14:29:53 +01004034 }
4035}
4036
4037static int drbd_disconnected(int vnr, void *p, void *data)
4038{
4039 struct drbd_conf *mdev = (struct drbd_conf *)p;
4040 enum drbd_fencing_p fp;
4041 unsigned int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004042
Philipp Reisner85719572010-07-21 10:20:17 +02004043 /* wait for current activity to cease. */
Philipp Reisner87eeee42011-01-19 14:16:30 +01004044 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004045 _drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
4046 _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee);
4047 _drbd_wait_ee_list_empty(mdev, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004048 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004049
4050 /* We do not have data structures that would allow us to
4051 * get the rs_pending_cnt down to 0 again.
4052 * * On C_SYNC_TARGET we do not have any data structures describing
4053 * the pending RSDataRequest's we have sent.
4054 * * On C_SYNC_SOURCE there is no data structure that tracks
4055 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
4056 * And no, it is not the sum of the reference counts in the
4057 * resync_LRU. The resync_LRU tracks the whole operation including
4058 * the disk-IO, while the rs_pending_cnt only tracks the blocks
4059 * on the fly. */
4060 drbd_rs_cancel_all(mdev);
4061 mdev->rs_total = 0;
4062 mdev->rs_failed = 0;
4063 atomic_set(&mdev->rs_pending_cnt, 0);
4064 wake_up(&mdev->misc_wait);
4065
Philipp Reisner7fde2be2011-03-01 11:08:28 +01004066 del_timer(&mdev->request_timer);
4067
Philipp Reisnerb411b362009-09-25 16:07:19 -07004068 del_timer_sync(&mdev->resync_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004069 resync_timer_fn((unsigned long)mdev);
4070
Philipp Reisnerb411b362009-09-25 16:07:19 -07004071 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
4072 * w_make_resync_request etc. which may still be on the worker queue
4073 * to be "canceled" */
Philipp Reisnera21e9292011-02-08 15:08:49 +01004074 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004075
4076 /* This also does reclaim_net_ee(). If we do this too early, we might
4077 * miss some resync ee and pages.*/
4078 drbd_process_done_ee(mdev);
4079
4080 kfree(mdev->p_uuid);
4081 mdev->p_uuid = NULL;
4082
Philipp Reisnerfb22c402010-09-08 23:20:21 +02004083 if (!is_susp(mdev->state))
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01004084 tl_clear(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004085
Philipp Reisnerb411b362009-09-25 16:07:19 -07004086 drbd_md_sync(mdev);
4087
4088 fp = FP_DONT_CARE;
4089 if (get_ldev(mdev)) {
4090 fp = mdev->ldev->dc.fencing;
4091 put_ldev(mdev);
4092 }
4093
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004094 /* serialize with bitmap writeout triggered by the state change,
4095 * if any. */
4096 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
4097
Philipp Reisnerb411b362009-09-25 16:07:19 -07004098 /* tcp_close and release of sendpage pages can be deferred. I don't
4099 * want to use SO_LINGER, because apparently it can be deferred for
4100 * more than 20 seconds (longest time I checked).
4101 *
4102 * Actually we don't care for exactly when the network stack does its
4103 * put_page(), but release our reference on these pages right here.
4104 */
4105 i = drbd_release_ee(mdev, &mdev->net_ee);
4106 if (i)
4107 dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
Lars Ellenberg435f0742010-09-06 12:30:25 +02004108 i = atomic_read(&mdev->pp_in_use_by_net);
4109 if (i)
4110 dev_info(DEV, "pp_in_use_by_net = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004111 i = atomic_read(&mdev->pp_in_use);
4112 if (i)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02004113 dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004114
4115 D_ASSERT(list_empty(&mdev->read_ee));
4116 D_ASSERT(list_empty(&mdev->active_ee));
4117 D_ASSERT(list_empty(&mdev->sync_ee));
4118 D_ASSERT(list_empty(&mdev->done_ee));
4119
4120 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
4121 atomic_set(&mdev->current_epoch->epoch_size, 0);
4122 D_ASSERT(list_empty(&mdev->current_epoch->list));
Philipp Reisner360cc742011-02-08 14:29:53 +01004123
4124 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004125}
4126
4127/*
4128 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
4129 * we can agree on is stored in agreed_pro_version.
4130 *
4131 * feature flags and the reserved array should be enough room for future
4132 * enhancements of the handshake protocol, and possible plugins...
4133 *
4134 * for now, they are expected to be zero, but ignored.
4135 */
Philipp Reisner8a22ccc2011-02-07 16:47:12 +01004136static int drbd_send_handshake(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004137{
Philipp Reisnere6b3ea82011-01-19 14:02:01 +01004138 /* ASSERT current == mdev->tconn->receiver ... */
Andreas Gruenbacher5a87d922011-03-24 21:17:52 +01004139 struct p_handshake *p = tconn->data.sbuf;
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004140 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004141
Philipp Reisner8a22ccc2011-02-07 16:47:12 +01004142 if (mutex_lock_interruptible(&tconn->data.mutex)) {
4143 conn_err(tconn, "interrupted during initial handshake\n");
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004144 return -EINTR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004145 }
4146
Philipp Reisner8a22ccc2011-02-07 16:47:12 +01004147 if (tconn->data.socket == NULL) {
4148 mutex_unlock(&tconn->data.mutex);
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004149 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004150 }
4151
4152 memset(p, 0, sizeof(*p));
4153 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
4154 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004155 err = _conn_send_cmd(tconn, 0, tconn->data.socket, P_HAND_SHAKE,
Andreas Gruenbacherecf23632011-03-15 23:48:25 +01004156 &p->head, sizeof(*p), 0);
Philipp Reisner8a22ccc2011-02-07 16:47:12 +01004157 mutex_unlock(&tconn->data.mutex);
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004158 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004159}
4160
4161/*
4162 * return values:
4163 * 1 yes, we have a valid connection
4164 * 0 oops, did not work out, please try again
4165 * -1 peer talks different language,
4166 * no point in trying again, please go standalone.
4167 */
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004168static int drbd_do_handshake(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004169{
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004170 /* ASSERT current == tconn->receiver ... */
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01004171 struct p_handshake *p = tconn->data.rbuf;
Philipp Reisner02918be2010-08-20 14:35:10 +02004172 const int expect = sizeof(struct p_handshake) - sizeof(struct p_header80);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004173 struct packet_info pi;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004174 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004175
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004176 err = drbd_send_handshake(tconn);
4177 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004178 return 0;
4179
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004180 err = drbd_recv_header(tconn, &pi);
4181 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004182 return 0;
4183
Philipp Reisner77351055b2011-02-07 17:24:26 +01004184 if (pi.cmd != P_HAND_SHAKE) {
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004185 conn_err(tconn, "expected HandShake packet, received: %s (0x%04x)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004186 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004187 return -1;
4188 }
4189
Philipp Reisner77351055b2011-02-07 17:24:26 +01004190 if (pi.size != expect) {
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004191 conn_err(tconn, "expected HandShake length: %u, received: %u\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004192 expect, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004193 return -1;
4194 }
4195
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004196 err = drbd_recv_all_warn(tconn, &p->head.payload, expect);
4197 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004198 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004199
Philipp Reisnerb411b362009-09-25 16:07:19 -07004200 p->protocol_min = be32_to_cpu(p->protocol_min);
4201 p->protocol_max = be32_to_cpu(p->protocol_max);
4202 if (p->protocol_max == 0)
4203 p->protocol_max = p->protocol_min;
4204
4205 if (PRO_VERSION_MAX < p->protocol_min ||
4206 PRO_VERSION_MIN > p->protocol_max)
4207 goto incompat;
4208
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004209 tconn->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004210
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004211 conn_info(tconn, "Handshake successful: "
4212 "Agreed network protocol version %d\n", tconn->agreed_pro_version);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004213
4214 return 1;
4215
4216 incompat:
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004217 conn_err(tconn, "incompatible DRBD dialects: "
Philipp Reisnerb411b362009-09-25 16:07:19 -07004218 "I support %d-%d, peer supports %d-%d\n",
4219 PRO_VERSION_MIN, PRO_VERSION_MAX,
4220 p->protocol_min, p->protocol_max);
4221 return -1;
4222}
4223
4224#if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
Philipp Reisner13e60372011-02-08 09:54:40 +01004225static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004226{
4227 dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
4228 dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004229 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004230}
4231#else
4232#define CHALLENGE_LEN 64
Johannes Thomab10d96c2010-01-07 16:02:50 +01004233
4234/* Return value:
4235 1 - auth succeeded,
4236 0 - failed, try again (network error),
4237 -1 - auth failed, don't try again.
4238*/
4239
Philipp Reisner13e60372011-02-08 09:54:40 +01004240static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004241{
4242 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
4243 struct scatterlist sg;
4244 char *response = NULL;
4245 char *right_response = NULL;
4246 char *peers_ch = NULL;
Philipp Reisner13e60372011-02-08 09:54:40 +01004247 unsigned int key_len = strlen(tconn->net_conf->shared_secret);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004248 unsigned int resp_size;
4249 struct hash_desc desc;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004250 struct packet_info pi;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004251 int err, rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004252
Philipp Reisner13e60372011-02-08 09:54:40 +01004253 desc.tfm = tconn->cram_hmac_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004254 desc.flags = 0;
4255
Philipp Reisner13e60372011-02-08 09:54:40 +01004256 rv = crypto_hash_setkey(tconn->cram_hmac_tfm,
4257 (u8 *)tconn->net_conf->shared_secret, key_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004258 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004259 conn_err(tconn, "crypto_hash_setkey() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004260 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004261 goto fail;
4262 }
4263
4264 get_random_bytes(my_challenge, CHALLENGE_LEN);
4265
Andreas Gruenbacherce9879c2011-03-15 23:34:29 +01004266 rv = !conn_send_cmd2(tconn, P_AUTH_CHALLENGE, my_challenge, CHALLENGE_LEN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004267 if (!rv)
4268 goto fail;
4269
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004270 err = drbd_recv_header(tconn, &pi);
4271 if (err) {
4272 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004273 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004274 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004275
Philipp Reisner77351055b2011-02-07 17:24:26 +01004276 if (pi.cmd != P_AUTH_CHALLENGE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004277 conn_err(tconn, "expected AuthChallenge packet, received: %s (0x%04x)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004278 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004279 rv = 0;
4280 goto fail;
4281 }
4282
Philipp Reisner77351055b2011-02-07 17:24:26 +01004283 if (pi.size > CHALLENGE_LEN * 2) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004284 conn_err(tconn, "expected AuthChallenge payload too big.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004285 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004286 goto fail;
4287 }
4288
Philipp Reisner77351055b2011-02-07 17:24:26 +01004289 peers_ch = kmalloc(pi.size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004290 if (peers_ch == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004291 conn_err(tconn, "kmalloc of peers_ch failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004292 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004293 goto fail;
4294 }
4295
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004296 err = drbd_recv_all_warn(tconn, peers_ch, pi.size);
4297 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004298 rv = 0;
4299 goto fail;
4300 }
4301
Philipp Reisner13e60372011-02-08 09:54:40 +01004302 resp_size = crypto_hash_digestsize(tconn->cram_hmac_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004303 response = kmalloc(resp_size, GFP_NOIO);
4304 if (response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004305 conn_err(tconn, "kmalloc of response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004306 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004307 goto fail;
4308 }
4309
4310 sg_init_table(&sg, 1);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004311 sg_set_buf(&sg, peers_ch, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004312
4313 rv = crypto_hash_digest(&desc, &sg, sg.length, response);
4314 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004315 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004316 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004317 goto fail;
4318 }
4319
Andreas Gruenbacherce9879c2011-03-15 23:34:29 +01004320 rv = !conn_send_cmd2(tconn, P_AUTH_RESPONSE, response, resp_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004321 if (!rv)
4322 goto fail;
4323
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004324 err = drbd_recv_header(tconn, &pi);
4325 if (err) {
4326 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004327 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004328 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004329
Philipp Reisner77351055b2011-02-07 17:24:26 +01004330 if (pi.cmd != P_AUTH_RESPONSE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004331 conn_err(tconn, "expected AuthResponse packet, received: %s (0x%04x)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004332 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004333 rv = 0;
4334 goto fail;
4335 }
4336
Philipp Reisner77351055b2011-02-07 17:24:26 +01004337 if (pi.size != resp_size) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004338 conn_err(tconn, "expected AuthResponse payload of wrong size\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004339 rv = 0;
4340 goto fail;
4341 }
4342
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004343 err = drbd_recv_all_warn(tconn, response , resp_size);
4344 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004345 rv = 0;
4346 goto fail;
4347 }
4348
4349 right_response = kmalloc(resp_size, GFP_NOIO);
Julia Lawall2d1ee872009-12-27 22:27:11 +01004350 if (right_response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004351 conn_err(tconn, "kmalloc of right_response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004352 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004353 goto fail;
4354 }
4355
4356 sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
4357
4358 rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
4359 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004360 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004361 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004362 goto fail;
4363 }
4364
4365 rv = !memcmp(response, right_response, resp_size);
4366
4367 if (rv)
Philipp Reisner13e60372011-02-08 09:54:40 +01004368 conn_info(tconn, "Peer authenticated using %d bytes of '%s' HMAC\n",
4369 resp_size, tconn->net_conf->cram_hmac_alg);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004370 else
4371 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004372
4373 fail:
4374 kfree(peers_ch);
4375 kfree(response);
4376 kfree(right_response);
4377
4378 return rv;
4379}
4380#endif
4381
4382int drbdd_init(struct drbd_thread *thi)
4383{
Philipp Reisner392c8802011-02-09 10:33:31 +01004384 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004385 int h;
4386
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004387 conn_info(tconn, "receiver (re)started\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004388
4389 do {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004390 h = drbd_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004391 if (h == 0) {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004392 drbd_disconnect(tconn);
Philipp Reisner20ee6392011-01-18 15:28:59 +01004393 schedule_timeout_interruptible(HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004394 }
4395 if (h == -1) {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004396 conn_warn(tconn, "Discarding network configuration.\n");
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004397 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004398 }
4399 } while (h == 0);
4400
4401 if (h > 0) {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004402 if (get_net_conf(tconn)) {
4403 drbdd(tconn);
4404 put_net_conf(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004405 }
4406 }
4407
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004408 drbd_disconnect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004409
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004410 conn_info(tconn, "receiver terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004411 return 0;
4412}
4413
4414/* ********* acknowledge sender ******** */
4415
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004416static int got_conn_RqSReply(struct drbd_tconn *tconn, enum drbd_packet cmd)
4417{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01004418 struct p_req_state_reply *p = tconn->meta.rbuf;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004419 int retcode = be32_to_cpu(p->retcode);
4420
4421 if (retcode >= SS_SUCCESS) {
4422 set_bit(CONN_WD_ST_CHG_OKAY, &tconn->flags);
4423 } else {
4424 set_bit(CONN_WD_ST_CHG_FAIL, &tconn->flags);
4425 conn_err(tconn, "Requested state change failed by peer: %s (%d)\n",
4426 drbd_set_st_err_str(retcode), retcode);
4427 }
4428 wake_up(&tconn->ping_wait);
4429
4430 return true;
4431}
4432
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004433static int got_RqSReply(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004434{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01004435 struct p_req_state_reply *p = mdev->tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004436 int retcode = be32_to_cpu(p->retcode);
4437
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004438 if (retcode >= SS_SUCCESS) {
4439 set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
4440 } else {
4441 set_bit(CL_ST_CHG_FAIL, &mdev->flags);
4442 dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
4443 drbd_set_st_err_str(retcode), retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004444 }
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004445 wake_up(&mdev->state_wait);
4446
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004447 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004448}
4449
Philipp Reisnerf19e4f82011-03-16 11:21:50 +01004450static int got_Ping(struct drbd_tconn *tconn, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004451{
Philipp Reisnerf19e4f82011-03-16 11:21:50 +01004452 return drbd_send_ping_ack(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004453
4454}
4455
Philipp Reisnerf19e4f82011-03-16 11:21:50 +01004456static int got_PingAck(struct drbd_tconn *tconn, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004457{
4458 /* restore idle timeout */
Philipp Reisner2a67d8b2011-02-09 14:10:32 +01004459 tconn->meta.socket->sk->sk_rcvtimeo = tconn->net_conf->ping_int*HZ;
4460 if (!test_and_set_bit(GOT_PING_ACK, &tconn->flags))
4461 wake_up(&tconn->ping_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004462
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004463 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004464}
4465
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004466static int got_IsInSync(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004467{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01004468 struct p_block_ack *p = mdev->tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004469 sector_t sector = be64_to_cpu(p->sector);
4470 int blksize = be32_to_cpu(p->blksize);
4471
Philipp Reisner31890f42011-01-19 14:12:51 +01004472 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004473
4474 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4475
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004476 if (get_ldev(mdev)) {
4477 drbd_rs_complete_io(mdev, sector);
4478 drbd_set_in_sync(mdev, sector, blksize);
4479 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
4480 mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
4481 put_ldev(mdev);
4482 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004483 dec_rs_pending(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02004484 atomic_add(blksize >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004485
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004486 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004487}
4488
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004489static int
4490validate_req_change_req_state(struct drbd_conf *mdev, u64 id, sector_t sector,
4491 struct rb_root *root, const char *func,
4492 enum drbd_req_event what, bool missing_ok)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004493{
4494 struct drbd_request *req;
4495 struct bio_and_error m;
4496
Philipp Reisner87eeee42011-01-19 14:16:30 +01004497 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004498 req = find_request(mdev, root, id, sector, missing_ok, func);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004499 if (unlikely(!req)) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01004500 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004501 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004502 }
4503 __req_mod(req, what, &m);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004504 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004505
4506 if (m.bio)
4507 complete_master_bio(mdev, &m);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004508 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004509}
4510
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004511static int got_BlockAck(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004512{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01004513 struct p_block_ack *p = mdev->tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004514 sector_t sector = be64_to_cpu(p->sector);
4515 int blksize = be32_to_cpu(p->blksize);
4516 enum drbd_req_event what;
4517
4518 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4519
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004520 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004521 drbd_set_in_sync(mdev, sector, blksize);
4522 dec_rs_pending(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004523 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004524 }
Philipp Reisner257d0af2011-01-26 12:15:29 +01004525 switch (cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004526 case P_RS_WRITE_ACK:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004527 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004528 what = WRITE_ACKED_BY_PEER_AND_SIS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004529 break;
4530 case P_WRITE_ACK:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004531 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004532 what = WRITE_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004533 break;
4534 case P_RECV_ACK:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004535 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_B);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004536 what = RECV_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004537 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004538 case P_DISCARD_WRITE:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004539 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004540 what = DISCARD_WRITE;
4541 break;
4542 case P_RETRY_WRITE:
4543 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
4544 what = POSTPONE_WRITE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004545 break;
4546 default:
4547 D_ASSERT(0);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004548 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004549 }
4550
4551 return validate_req_change_req_state(mdev, p->block_id, sector,
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004552 &mdev->write_requests, __func__,
4553 what, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004554}
4555
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004556static int got_NegAck(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004557{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01004558 struct p_block_ack *p = mdev->tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004559 sector_t sector = be64_to_cpu(p->sector);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004560 int size = be32_to_cpu(p->blksize);
Philipp Reisner89e58e72011-01-19 13:12:45 +01004561 bool missing_ok = mdev->tconn->net_conf->wire_protocol == DRBD_PROT_A ||
4562 mdev->tconn->net_conf->wire_protocol == DRBD_PROT_B;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004563 bool found;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004564
4565 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4566
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004567 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004568 dec_rs_pending(mdev);
4569 drbd_rs_failed_io(mdev, sector, size);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004570 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004571 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01004572
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004573 found = validate_req_change_req_state(mdev, p->block_id, sector,
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004574 &mdev->write_requests, __func__,
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004575 NEG_ACKED, missing_ok);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004576 if (!found) {
4577 /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs.
4578 The master bio might already be completed, therefore the
4579 request is no longer in the collision hash. */
4580 /* In Protocol B we might already have got a P_RECV_ACK
4581 but then get a P_NEG_ACK afterwards. */
4582 if (!missing_ok)
Philipp Reisner2deb8332011-01-17 18:39:18 +01004583 return false;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004584 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004585 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01004586 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004587}
4588
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004589static int got_NegDReply(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004590{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01004591 struct p_block_ack *p = mdev->tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004592 sector_t sector = be64_to_cpu(p->sector);
4593
4594 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004595
Philipp Reisnerb411b362009-09-25 16:07:19 -07004596 dev_err(DEV, "Got NegDReply; Sector %llus, len %u; Fail original request.\n",
4597 (unsigned long long)sector, be32_to_cpu(p->blksize));
4598
4599 return validate_req_change_req_state(mdev, p->block_id, sector,
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004600 &mdev->read_requests, __func__,
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004601 NEG_ACKED, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004602}
4603
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004604static int got_NegRSDReply(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004605{
4606 sector_t sector;
4607 int size;
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01004608 struct p_block_ack *p = mdev->tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004609
4610 sector = be64_to_cpu(p->sector);
4611 size = be32_to_cpu(p->blksize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004612
4613 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4614
4615 dec_rs_pending(mdev);
4616
4617 if (get_ldev_if_state(mdev, D_FAILED)) {
4618 drbd_rs_complete_io(mdev, sector);
Philipp Reisner257d0af2011-01-26 12:15:29 +01004619 switch (cmd) {
Philipp Reisnerd612d302010-12-27 10:53:28 +01004620 case P_NEG_RS_DREPLY:
4621 drbd_rs_failed_io(mdev, sector, size);
4622 case P_RS_CANCEL:
4623 break;
4624 default:
4625 D_ASSERT(0);
4626 put_ldev(mdev);
4627 return false;
4628 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004629 put_ldev(mdev);
4630 }
4631
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004632 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004633}
4634
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004635static int got_BarrierAck(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004636{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01004637 struct p_barrier_ack *p = mdev->tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004638
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01004639 tl_release(mdev->tconn, p->barrier, be32_to_cpu(p->set_size));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004640
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02004641 if (mdev->state.conn == C_AHEAD &&
4642 atomic_read(&mdev->ap_in_flight) == 0 &&
Philipp Reisner370a43e2011-01-14 16:03:11 +01004643 !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->current_epoch->flags)) {
4644 mdev->start_resync_timer.expires = jiffies + HZ;
4645 add_timer(&mdev->start_resync_timer);
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02004646 }
4647
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004648 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004649}
4650
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004651static int got_OVResult(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004652{
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01004653 struct p_block_ack *p = mdev->tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004654 struct drbd_work *w;
4655 sector_t sector;
4656 int size;
4657
4658 sector = be64_to_cpu(p->sector);
4659 size = be32_to_cpu(p->blksize);
4660
4661 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4662
4663 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01004664 drbd_ov_out_of_sync_found(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004665 else
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01004666 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004667
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004668 if (!get_ldev(mdev))
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004669 return true;
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004670
Philipp Reisnerb411b362009-09-25 16:07:19 -07004671 drbd_rs_complete_io(mdev, sector);
4672 dec_rs_pending(mdev);
4673
Lars Ellenbergea5442a2010-11-05 09:48:01 +01004674 --mdev->ov_left;
4675
4676 /* let's advance progress step marks only for every other megabyte */
4677 if ((mdev->ov_left & 0x200) == 0x200)
4678 drbd_advance_rs_marks(mdev, mdev->ov_left);
4679
4680 if (mdev->ov_left == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004681 w = kmalloc(sizeof(*w), GFP_NOIO);
4682 if (w) {
4683 w->cb = w_ov_finished;
Philipp Reisnera21e9292011-02-08 15:08:49 +01004684 w->mdev = mdev;
Philipp Reisnere42325a2011-01-19 13:55:45 +01004685 drbd_queue_work_front(&mdev->tconn->data.work, w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004686 } else {
4687 dev_err(DEV, "kmalloc(w) failed.");
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01004688 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004689 drbd_resync_finished(mdev);
4690 }
4691 }
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004692 put_ldev(mdev);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004693 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004694}
4695
Andreas Gruenbacherd8763022011-01-26 17:39:41 +01004696static int got_skip(struct drbd_conf *mdev, enum drbd_packet cmd)
Philipp Reisner0ced55a2010-04-30 15:26:20 +02004697{
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01004698 return true;
Philipp Reisner0ced55a2010-04-30 15:26:20 +02004699}
4700
Philipp Reisner32862ec2011-02-08 16:41:01 +01004701static int tconn_process_done_ee(struct drbd_tconn *tconn)
4702{
Philipp Reisner082a3432011-03-15 16:05:42 +01004703 struct drbd_conf *mdev;
4704 int i, not_empty = 0;
Philipp Reisner32862ec2011-02-08 16:41:01 +01004705
4706 do {
4707 clear_bit(SIGNAL_ASENDER, &tconn->flags);
4708 flush_signals(current);
Philipp Reisner082a3432011-03-15 16:05:42 +01004709 idr_for_each_entry(&tconn->volumes, mdev, i) {
Andreas Gruenbachere2b30322011-03-16 17:16:12 +01004710 if (drbd_process_done_ee(mdev))
Philipp Reisner082a3432011-03-15 16:05:42 +01004711 return 1; /* error */
4712 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01004713 set_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisner082a3432011-03-15 16:05:42 +01004714
4715 spin_lock_irq(&tconn->req_lock);
4716 idr_for_each_entry(&tconn->volumes, mdev, i) {
4717 not_empty = !list_empty(&mdev->done_ee);
4718 if (not_empty)
4719 break;
4720 }
4721 spin_unlock_irq(&tconn->req_lock);
Philipp Reisner32862ec2011-02-08 16:41:01 +01004722 } while (not_empty);
4723
4724 return 0;
4725}
4726
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004727struct asender_cmd {
4728 size_t pkt_size;
Philipp Reisnera4fbda82011-03-16 11:13:17 +01004729 enum mdev_or_conn fa_type; /* first argument's type */
4730 union {
4731 int (*mdev_fn)(struct drbd_conf *mdev, enum drbd_packet cmd);
4732 int (*conn_fn)(struct drbd_tconn *tconn, enum drbd_packet cmd);
4733 };
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004734};
4735
4736static struct asender_cmd asender_tbl[] = {
Philipp Reisnerf19e4f82011-03-16 11:21:50 +01004737 [P_PING] = { sizeof(struct p_header), CONN, { .conn_fn = got_Ping } },
4738 [P_PING_ACK] = { sizeof(struct p_header), CONN, { .conn_fn = got_PingAck } },
Philipp Reisnera4fbda82011-03-16 11:13:17 +01004739 [P_RECV_ACK] = { sizeof(struct p_block_ack), MDEV, { got_BlockAck } },
4740 [P_WRITE_ACK] = { sizeof(struct p_block_ack), MDEV, { got_BlockAck } },
4741 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), MDEV, { got_BlockAck } },
4742 [P_DISCARD_WRITE] = { sizeof(struct p_block_ack), MDEV, { got_BlockAck } },
4743 [P_NEG_ACK] = { sizeof(struct p_block_ack), MDEV, { got_NegAck } },
4744 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), MDEV, { got_NegDReply } },
4745 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), MDEV, { got_NegRSDReply } },
4746 [P_OV_RESULT] = { sizeof(struct p_block_ack), MDEV, { got_OVResult } },
4747 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), MDEV, { got_BarrierAck } },
4748 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), MDEV, { got_RqSReply } },
4749 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), MDEV, { got_IsInSync } },
4750 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), MDEV, { got_skip } },
4751 [P_RS_CANCEL] = { sizeof(struct p_block_ack), MDEV, { got_NegRSDReply } },
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004752 [P_CONN_ST_CHG_REPLY]={ sizeof(struct p_req_state_reply), CONN, {.conn_fn = got_conn_RqSReply}},
Philipp Reisnera4fbda82011-03-16 11:13:17 +01004753 [P_RETRY_WRITE] = { sizeof(struct p_block_ack), MDEV, { got_BlockAck } },
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004754};
4755
Philipp Reisnerb411b362009-09-25 16:07:19 -07004756int drbd_asender(struct drbd_thread *thi)
4757{
Philipp Reisner392c8802011-02-09 10:33:31 +01004758 struct drbd_tconn *tconn = thi->tconn;
Andreas Gruenbachere6ef8a52011-03-24 18:07:54 +01004759 struct p_header *h = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004760 struct asender_cmd *cmd = NULL;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004761 struct packet_info pi;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004762 int rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004763 void *buf = h;
4764 int received = 0;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004765 int expect = sizeof(struct p_header);
Lars Ellenbergf36af182011-03-09 22:44:55 +01004766 int ping_timeout_active = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004767
Philipp Reisnerb411b362009-09-25 16:07:19 -07004768 current->policy = SCHED_RR; /* Make this a realtime task! */
4769 current->rt_priority = 2; /* more important than all other tasks */
4770
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01004771 while (get_t_state(thi) == RUNNING) {
Philipp Reisner80822282011-02-08 12:46:30 +01004772 drbd_thread_current_set_cpu(thi);
Philipp Reisner32862ec2011-02-08 16:41:01 +01004773 if (test_and_clear_bit(SEND_PING, &tconn->flags)) {
Philipp Reisner2a67d8b2011-02-09 14:10:32 +01004774 if (!drbd_send_ping(tconn)) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004775 conn_err(tconn, "drbd_send_ping has failed\n");
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01004776 goto reconnect;
4777 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01004778 tconn->meta.socket->sk->sk_rcvtimeo =
4779 tconn->net_conf->ping_timeo*HZ/10;
Lars Ellenbergf36af182011-03-09 22:44:55 +01004780 ping_timeout_active = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004781 }
4782
Philipp Reisner32862ec2011-02-08 16:41:01 +01004783 /* TODO: conditionally cork; it may hurt latency if we cork without
4784 much to send */
4785 if (!tconn->net_conf->no_cork)
4786 drbd_tcp_cork(tconn->meta.socket);
Philipp Reisner082a3432011-03-15 16:05:42 +01004787 if (tconn_process_done_ee(tconn)) {
4788 conn_err(tconn, "tconn_process_done_ee() failed\n");
Philipp Reisner32862ec2011-02-08 16:41:01 +01004789 goto reconnect;
Philipp Reisner082a3432011-03-15 16:05:42 +01004790 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004791 /* but unconditionally uncork unless disabled */
Philipp Reisner32862ec2011-02-08 16:41:01 +01004792 if (!tconn->net_conf->no_cork)
4793 drbd_tcp_uncork(tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004794
4795 /* short circuit, recv_msg would return EINTR anyways. */
4796 if (signal_pending(current))
4797 continue;
4798
Philipp Reisner32862ec2011-02-08 16:41:01 +01004799 rv = drbd_recv_short(tconn->meta.socket, buf, expect-received, 0);
4800 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004801
4802 flush_signals(current);
4803
4804 /* Note:
4805 * -EINTR (on meta) we got a signal
4806 * -EAGAIN (on meta) rcvtimeo expired
4807 * -ECONNRESET other side closed the connection
4808 * -ERESTARTSYS (on data) we got a signal
4809 * rv < 0 other than above: unexpected error!
4810 * rv == expected: full header or command
4811 * rv < expected: "woken" by signal during receive
4812 * rv == 0 : "connection shut down by peer"
4813 */
4814 if (likely(rv > 0)) {
4815 received += rv;
4816 buf += rv;
4817 } else if (rv == 0) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004818 conn_err(tconn, "meta connection shut down by peer.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004819 goto reconnect;
4820 } else if (rv == -EAGAIN) {
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02004821 /* If the data socket received something meanwhile,
4822 * that is good enough: peer is still alive. */
Philipp Reisner32862ec2011-02-08 16:41:01 +01004823 if (time_after(tconn->last_received,
4824 jiffies - tconn->meta.socket->sk->sk_rcvtimeo))
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02004825 continue;
Lars Ellenbergf36af182011-03-09 22:44:55 +01004826 if (ping_timeout_active) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004827 conn_err(tconn, "PingAck did not arrive in time.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004828 goto reconnect;
4829 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01004830 set_bit(SEND_PING, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004831 continue;
4832 } else if (rv == -EINTR) {
4833 continue;
4834 } else {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004835 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004836 goto reconnect;
4837 }
4838
4839 if (received == expect && cmd == NULL) {
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01004840 if (decode_header(tconn, h, &pi))
Philipp Reisnerb411b362009-09-25 16:07:19 -07004841 goto reconnect;
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004842 cmd = &asender_tbl[pi.cmd];
4843 if (pi.cmd >= ARRAY_SIZE(asender_tbl) || !cmd) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004844 conn_err(tconn, "unknown command %d on meta (l: %d)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004845 pi.cmd, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004846 goto disconnect;
4847 }
4848 expect = cmd->pkt_size;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004849 if (pi.size != expect - sizeof(struct p_header)) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004850 conn_err(tconn, "Wrong packet size on meta (c: %d, l: %d)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004851 pi.cmd, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004852 goto reconnect;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004853 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004854 }
4855 if (received == expect) {
Philipp Reisnera4fbda82011-03-16 11:13:17 +01004856 bool rv;
4857
4858 if (cmd->fa_type == CONN) {
4859 rv = cmd->conn_fn(tconn, pi.cmd);
4860 } else {
4861 struct drbd_conf *mdev = vnr_to_mdev(tconn, pi.vnr);
4862 rv = cmd->mdev_fn(mdev, pi.cmd);
4863 }
4864
4865 if (!rv)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004866 goto reconnect;
4867
Philipp Reisnera4fbda82011-03-16 11:13:17 +01004868 tconn->last_received = jiffies;
4869
Lars Ellenbergf36af182011-03-09 22:44:55 +01004870 /* the idle_timeout (ping-int)
4871 * has been restored in got_PingAck() */
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004872 if (cmd == &asender_tbl[P_PING_ACK])
Lars Ellenbergf36af182011-03-09 22:44:55 +01004873 ping_timeout_active = 0;
4874
Philipp Reisnerb411b362009-09-25 16:07:19 -07004875 buf = h;
4876 received = 0;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004877 expect = sizeof(struct p_header);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004878 cmd = NULL;
4879 }
4880 }
4881
4882 if (0) {
4883reconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004884 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004885 }
4886 if (0) {
4887disconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004888 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004889 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01004890 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004891
Philipp Reisner32862ec2011-02-08 16:41:01 +01004892 conn_info(tconn, "asender terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004893
4894 return 0;
4895}