blob: 3f68404a17e8a2a12288d6fc0ed937f15ad57cc4 [file] [log] [blame]
Philipp Reisnerb411b362009-09-25 16:07:19 -07001/*
2 drbd_receiver.c
3
4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5
6 Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
7 Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8 Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
9
10 drbd is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 drbd is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with drbd; see the file COPYING. If not, write to
22 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25
Philipp Reisnerb411b362009-09-25 16:07:19 -070026#include <linux/module.h>
27
28#include <asm/uaccess.h>
29#include <net/sock.h>
30
Philipp Reisnerb411b362009-09-25 16:07:19 -070031#include <linux/drbd.h>
32#include <linux/fs.h>
33#include <linux/file.h>
34#include <linux/in.h>
35#include <linux/mm.h>
36#include <linux/memcontrol.h>
37#include <linux/mm_inline.h>
38#include <linux/slab.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070039#include <linux/pkt_sched.h>
40#define __KERNEL_SYSCALLS__
41#include <linux/unistd.h>
42#include <linux/vmalloc.h>
43#include <linux/random.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070044#include <linux/string.h>
45#include <linux/scatterlist.h>
46#include "drbd_int.h"
Philipp Reisnerb411b362009-09-25 16:07:19 -070047#include "drbd_req.h"
48
49#include "drbd_vli.h"
50
Philipp Reisner77351055b2011-02-07 17:24:26 +010051struct packet_info {
52 enum drbd_packet cmd;
Andreas Gruenbachere2857212011-03-25 00:57:38 +010053 unsigned int size;
54 unsigned int vnr;
Andreas Gruenbachere6589832011-03-30 12:54:42 +020055 void *data;
Philipp Reisner77351055b2011-02-07 17:24:26 +010056};
57
Philipp Reisnerb411b362009-09-25 16:07:19 -070058enum finish_epoch {
59 FE_STILL_LIVE,
60 FE_DESTROYED,
61 FE_RECYCLED,
62};
63
Andreas Gruenbacher60381782011-03-28 17:05:50 +020064static int drbd_do_features(struct drbd_tconn *tconn);
Philipp Reisner13e60372011-02-08 09:54:40 +010065static int drbd_do_auth(struct drbd_tconn *tconn);
Philipp Reisner360cc742011-02-08 14:29:53 +010066static int drbd_disconnected(int vnr, void *p, void *data);
Philipp Reisnerb411b362009-09-25 16:07:19 -070067
68static enum finish_epoch drbd_may_finish_epoch(struct drbd_conf *, struct drbd_epoch *, enum epoch_event);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +010069static int e_end_block(struct drbd_work *, int);
Philipp Reisnerb411b362009-09-25 16:07:19 -070070
Philipp Reisnerb411b362009-09-25 16:07:19 -070071
72#define GFP_TRY (__GFP_HIGHMEM | __GFP_NOWARN)
73
Lars Ellenberg45bb9122010-05-14 17:10:48 +020074/*
75 * some helper functions to deal with single linked page lists,
76 * page->private being our "next" pointer.
77 */
78
79/* If at least n pages are linked at head, get n pages off.
80 * Otherwise, don't modify head, and return NULL.
81 * Locking is the responsibility of the caller.
82 */
83static struct page *page_chain_del(struct page **head, int n)
84{
85 struct page *page;
86 struct page *tmp;
87
88 BUG_ON(!n);
89 BUG_ON(!head);
90
91 page = *head;
Philipp Reisner23ce4222010-05-20 13:35:31 +020092
93 if (!page)
94 return NULL;
95
Lars Ellenberg45bb9122010-05-14 17:10:48 +020096 while (page) {
97 tmp = page_chain_next(page);
98 if (--n == 0)
99 break; /* found sufficient pages */
100 if (tmp == NULL)
101 /* insufficient pages, don't use any of them. */
102 return NULL;
103 page = tmp;
104 }
105
106 /* add end of list marker for the returned list */
107 set_page_private(page, 0);
108 /* actual return value, and adjustment of head */
109 page = *head;
110 *head = tmp;
111 return page;
112}
113
114/* may be used outside of locks to find the tail of a (usually short)
115 * "private" page chain, before adding it back to a global chain head
116 * with page_chain_add() under a spinlock. */
117static struct page *page_chain_tail(struct page *page, int *len)
118{
119 struct page *tmp;
120 int i = 1;
121 while ((tmp = page_chain_next(page)))
122 ++i, page = tmp;
123 if (len)
124 *len = i;
125 return page;
126}
127
128static int page_chain_free(struct page *page)
129{
130 struct page *tmp;
131 int i = 0;
132 page_chain_for_each_safe(page, tmp) {
133 put_page(page);
134 ++i;
135 }
136 return i;
137}
138
139static void page_chain_add(struct page **head,
140 struct page *chain_first, struct page *chain_last)
141{
142#if 1
143 struct page *tmp;
144 tmp = page_chain_tail(chain_first, NULL);
145 BUG_ON(tmp != chain_last);
146#endif
147
148 /* add chain to head */
149 set_page_private(chain_last, (unsigned long)*head);
150 *head = chain_first;
151}
152
153static struct page *drbd_pp_first_pages_or_try_alloc(struct drbd_conf *mdev, int number)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700154{
155 struct page *page = NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200156 struct page *tmp = NULL;
157 int i = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700158
159 /* Yes, testing drbd_pp_vacant outside the lock is racy.
160 * So what. It saves a spin_lock. */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200161 if (drbd_pp_vacant >= number) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700162 spin_lock(&drbd_pp_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200163 page = page_chain_del(&drbd_pp_pool, number);
164 if (page)
165 drbd_pp_vacant -= number;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700166 spin_unlock(&drbd_pp_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200167 if (page)
168 return page;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700169 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200170
Philipp Reisnerb411b362009-09-25 16:07:19 -0700171 /* GFP_TRY, because we must not cause arbitrary write-out: in a DRBD
172 * "criss-cross" setup, that might cause write-out on some other DRBD,
173 * which in turn might block on the other node at this very place. */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200174 for (i = 0; i < number; i++) {
175 tmp = alloc_page(GFP_TRY);
176 if (!tmp)
177 break;
178 set_page_private(tmp, (unsigned long)page);
179 page = tmp;
180 }
181
182 if (i == number)
183 return page;
184
185 /* Not enough pages immediately available this time.
186 * No need to jump around here, drbd_pp_alloc will retry this
187 * function "soon". */
188 if (page) {
189 tmp = page_chain_tail(page, NULL);
190 spin_lock(&drbd_pp_lock);
191 page_chain_add(&drbd_pp_pool, page, tmp);
192 drbd_pp_vacant += i;
193 spin_unlock(&drbd_pp_lock);
194 }
195 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700196}
197
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200198static void reclaim_finished_net_peer_reqs(struct drbd_conf *mdev,
199 struct list_head *to_be_freed)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700200{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100201 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700202 struct list_head *le, *tle;
203
204 /* The EEs are always appended to the end of the list. Since
205 they are sent in order over the wire, they have to finish
206 in order. As soon as we see the first not finished we can
207 stop to examine the list... */
208
209 list_for_each_safe(le, tle, &mdev->net_ee) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100210 peer_req = list_entry(le, struct drbd_peer_request, w.list);
Andreas Gruenbacher045417f2011-04-07 21:34:24 +0200211 if (drbd_peer_req_has_active_page(peer_req))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700212 break;
213 list_move(le, to_be_freed);
214 }
215}
216
217static void drbd_kick_lo_and_reclaim_net(struct drbd_conf *mdev)
218{
219 LIST_HEAD(reclaimed);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100220 struct drbd_peer_request *peer_req, *t;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700221
Philipp Reisner87eeee42011-01-19 14:16:30 +0100222 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200223 reclaim_finished_net_peer_reqs(mdev, &reclaimed);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100224 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700225
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100226 list_for_each_entry_safe(peer_req, t, &reclaimed, w.list)
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200227 drbd_free_net_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700228}
229
230/**
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200231 * drbd_pp_alloc() - Returns @number pages, retries forever (or until signalled)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700232 * @mdev: DRBD device.
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200233 * @number: number of pages requested
234 * @retry: whether to retry, if not enough pages are available right now
Philipp Reisnerb411b362009-09-25 16:07:19 -0700235 *
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200236 * Tries to allocate number pages, first from our own page pool, then from
237 * the kernel, unless this allocation would exceed the max_buffers setting.
238 * Possibly retry until DRBD frees sufficient pages somewhere else.
239 *
240 * Returns a page chain linked via page->private.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700241 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200242static struct page *drbd_pp_alloc(struct drbd_conf *mdev, unsigned number, bool retry)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700243{
244 struct page *page = NULL;
245 DEFINE_WAIT(wait);
246
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200247 /* Yes, we may run up to @number over max_buffers. If we
248 * follow it strictly, the admin will get it wrong anyways. */
Philipp Reisner89e58e72011-01-19 13:12:45 +0100249 if (atomic_read(&mdev->pp_in_use) < mdev->tconn->net_conf->max_buffers)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200250 page = drbd_pp_first_pages_or_try_alloc(mdev, number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700251
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200252 while (page == NULL) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700253 prepare_to_wait(&drbd_pp_wait, &wait, TASK_INTERRUPTIBLE);
254
255 drbd_kick_lo_and_reclaim_net(mdev);
256
Philipp Reisner89e58e72011-01-19 13:12:45 +0100257 if (atomic_read(&mdev->pp_in_use) < mdev->tconn->net_conf->max_buffers) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200258 page = drbd_pp_first_pages_or_try_alloc(mdev, number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700259 if (page)
260 break;
261 }
262
263 if (!retry)
264 break;
265
266 if (signal_pending(current)) {
267 dev_warn(DEV, "drbd_pp_alloc interrupted!\n");
268 break;
269 }
270
271 schedule();
272 }
273 finish_wait(&drbd_pp_wait, &wait);
274
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200275 if (page)
276 atomic_add(number, &mdev->pp_in_use);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700277 return page;
278}
279
280/* Must not be used from irq, as that may deadlock: see drbd_pp_alloc.
Philipp Reisner87eeee42011-01-19 14:16:30 +0100281 * Is also used from inside an other spin_lock_irq(&mdev->tconn->req_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200282 * Either links the page chain back to the global pool,
283 * or returns all pages to the system. */
Lars Ellenberg435f0742010-09-06 12:30:25 +0200284static void drbd_pp_free(struct drbd_conf *mdev, struct page *page, int is_net)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700285{
Lars Ellenberg435f0742010-09-06 12:30:25 +0200286 atomic_t *a = is_net ? &mdev->pp_in_use_by_net : &mdev->pp_in_use;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700287 int i;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200288
Philipp Reisner81a5d602011-02-22 19:53:16 -0500289 if (drbd_pp_vacant > (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * minor_count)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200290 i = page_chain_free(page);
291 else {
292 struct page *tmp;
293 tmp = page_chain_tail(page, &i);
294 spin_lock(&drbd_pp_lock);
295 page_chain_add(&drbd_pp_pool, page, tmp);
296 drbd_pp_vacant += i;
297 spin_unlock(&drbd_pp_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700298 }
Lars Ellenberg435f0742010-09-06 12:30:25 +0200299 i = atomic_sub_return(i, a);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200300 if (i < 0)
Lars Ellenberg435f0742010-09-06 12:30:25 +0200301 dev_warn(DEV, "ASSERTION FAILED: %s: %d < 0\n",
302 is_net ? "pp_in_use_by_net" : "pp_in_use", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700303 wake_up(&drbd_pp_wait);
304}
305
306/*
307You need to hold the req_lock:
308 _drbd_wait_ee_list_empty()
309
310You must not have the req_lock:
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200311 drbd_free_peer_req()
Andreas Gruenbacher0db55362011-04-06 16:09:15 +0200312 drbd_alloc_peer_req()
Andreas Gruenbacher7721f562011-04-06 17:14:02 +0200313 drbd_free_peer_reqs()
Philipp Reisnerb411b362009-09-25 16:07:19 -0700314 drbd_ee_fix_bhs()
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200315 drbd_finish_peer_reqs()
Philipp Reisnerb411b362009-09-25 16:07:19 -0700316 drbd_clear_done_ee()
317 drbd_wait_ee_list_empty()
318*/
319
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +0100320struct drbd_peer_request *
Andreas Gruenbacher0db55362011-04-06 16:09:15 +0200321drbd_alloc_peer_req(struct drbd_conf *mdev, u64 id, sector_t sector,
322 unsigned int data_size, gfp_t gfp_mask) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700323{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100324 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700325 struct page *page;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200326 unsigned nr_pages = (data_size + PAGE_SIZE -1) >> PAGE_SHIFT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700327
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100328 if (drbd_insert_fault(mdev, DRBD_FAULT_AL_EE))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700329 return NULL;
330
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100331 peer_req = mempool_alloc(drbd_ee_mempool, gfp_mask & ~__GFP_HIGHMEM);
332 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700333 if (!(gfp_mask & __GFP_NOWARN))
Andreas Gruenbacher0db55362011-04-06 16:09:15 +0200334 dev_err(DEV, "%s: allocation failed\n", __func__);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700335 return NULL;
336 }
337
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200338 page = drbd_pp_alloc(mdev, nr_pages, (gfp_mask & __GFP_WAIT));
339 if (!page)
340 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700341
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100342 drbd_clear_interval(&peer_req->i);
343 peer_req->i.size = data_size;
344 peer_req->i.sector = sector;
345 peer_req->i.local = false;
346 peer_req->i.waiting = false;
Andreas Gruenbacher53840642011-01-28 10:31:04 +0100347
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100348 peer_req->epoch = NULL;
Philipp Reisnera21e9292011-02-08 15:08:49 +0100349 peer_req->w.mdev = mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100350 peer_req->pages = page;
351 atomic_set(&peer_req->pending_bios, 0);
352 peer_req->flags = 0;
Andreas Gruenbacher9a8e7752011-01-11 14:04:09 +0100353 /*
354 * The block_id is opaque to the receiver. It is not endianness
355 * converted, and sent back to the sender unchanged.
356 */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100357 peer_req->block_id = id;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700358
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100359 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700360
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200361 fail:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100362 mempool_free(peer_req, drbd_ee_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700363 return NULL;
364}
365
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200366void __drbd_free_peer_req(struct drbd_conf *mdev, struct drbd_peer_request *peer_req,
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +0100367 int is_net)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700368{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100369 if (peer_req->flags & EE_HAS_DIGEST)
370 kfree(peer_req->digest);
371 drbd_pp_free(mdev, peer_req->pages, is_net);
372 D_ASSERT(atomic_read(&peer_req->pending_bios) == 0);
373 D_ASSERT(drbd_interval_empty(&peer_req->i));
374 mempool_free(peer_req, drbd_ee_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700375}
376
Andreas Gruenbacher7721f562011-04-06 17:14:02 +0200377int drbd_free_peer_reqs(struct drbd_conf *mdev, struct list_head *list)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700378{
379 LIST_HEAD(work_list);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100380 struct drbd_peer_request *peer_req, *t;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700381 int count = 0;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200382 int is_net = list == &mdev->net_ee;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700383
Philipp Reisner87eeee42011-01-19 14:16:30 +0100384 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700385 list_splice_init(list, &work_list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100386 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700387
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100388 list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200389 __drbd_free_peer_req(mdev, peer_req, is_net);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700390 count++;
391 }
392 return count;
393}
394
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200395/*
396 * See also comments in _req_mod(,BARRIER_ACKED) and receive_Barrier.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700397 */
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200398static int drbd_finish_peer_reqs(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700399{
400 LIST_HEAD(work_list);
401 LIST_HEAD(reclaimed);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100402 struct drbd_peer_request *peer_req, *t;
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100403 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700404
Philipp Reisner87eeee42011-01-19 14:16:30 +0100405 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200406 reclaim_finished_net_peer_reqs(mdev, &reclaimed);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700407 list_splice_init(&mdev->done_ee, &work_list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100408 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700409
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100410 list_for_each_entry_safe(peer_req, t, &reclaimed, w.list)
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200411 drbd_free_net_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700412
413 /* possible callbacks here:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100414 * e_end_block, and e_end_resync_block, e_send_discard_write.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700415 * all ignore the last argument.
416 */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100417 list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100418 int err2;
419
Philipp Reisnerb411b362009-09-25 16:07:19 -0700420 /* list_del not necessary, next/prev members not touched */
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100421 err2 = peer_req->w.cb(&peer_req->w, !!err);
422 if (!err)
423 err = err2;
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200424 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700425 }
426 wake_up(&mdev->ee_wait);
427
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100428 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700429}
430
Andreas Gruenbacherd4da1532011-04-07 00:06:56 +0200431static void _drbd_wait_ee_list_empty(struct drbd_conf *mdev,
432 struct list_head *head)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700433{
434 DEFINE_WAIT(wait);
435
436 /* avoids spin_lock/unlock
437 * and calling prepare_to_wait in the fast path */
438 while (!list_empty(head)) {
439 prepare_to_wait(&mdev->ee_wait, &wait, TASK_UNINTERRUPTIBLE);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100440 spin_unlock_irq(&mdev->tconn->req_lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100441 io_schedule();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700442 finish_wait(&mdev->ee_wait, &wait);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100443 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700444 }
445}
446
Andreas Gruenbacherd4da1532011-04-07 00:06:56 +0200447static void drbd_wait_ee_list_empty(struct drbd_conf *mdev,
448 struct list_head *head)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700449{
Philipp Reisner87eeee42011-01-19 14:16:30 +0100450 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700451 _drbd_wait_ee_list_empty(mdev, head);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100452 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700453}
454
455/* see also kernel_accept; which is only present since 2.6.18.
456 * also we want to log which part of it failed, exactly */
Philipp Reisner76536202011-02-07 14:09:54 +0100457static int drbd_accept(const char **what, struct socket *sock, struct socket **newsock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700458{
459 struct sock *sk = sock->sk;
460 int err = 0;
461
462 *what = "listen";
463 err = sock->ops->listen(sock, 5);
464 if (err < 0)
465 goto out;
466
467 *what = "sock_create_lite";
468 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
469 newsock);
470 if (err < 0)
471 goto out;
472
473 *what = "accept";
474 err = sock->ops->accept(sock, *newsock, 0);
475 if (err < 0) {
476 sock_release(*newsock);
477 *newsock = NULL;
478 goto out;
479 }
480 (*newsock)->ops = sock->ops;
481
482out:
483 return err;
484}
485
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100486static int drbd_recv_short(struct socket *sock, void *buf, size_t size, int flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700487{
488 mm_segment_t oldfs;
489 struct kvec iov = {
490 .iov_base = buf,
491 .iov_len = size,
492 };
493 struct msghdr msg = {
494 .msg_iovlen = 1,
495 .msg_iov = (struct iovec *)&iov,
496 .msg_flags = (flags ? flags : MSG_WAITALL | MSG_NOSIGNAL)
497 };
498 int rv;
499
500 oldfs = get_fs();
501 set_fs(KERNEL_DS);
502 rv = sock_recvmsg(sock, &msg, size, msg.msg_flags);
503 set_fs(oldfs);
504
505 return rv;
506}
507
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100508static int drbd_recv(struct drbd_tconn *tconn, void *buf, size_t size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700509{
510 mm_segment_t oldfs;
511 struct kvec iov = {
512 .iov_base = buf,
513 .iov_len = size,
514 };
515 struct msghdr msg = {
516 .msg_iovlen = 1,
517 .msg_iov = (struct iovec *)&iov,
518 .msg_flags = MSG_WAITALL | MSG_NOSIGNAL
519 };
520 int rv;
521
522 oldfs = get_fs();
523 set_fs(KERNEL_DS);
524
525 for (;;) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100526 rv = sock_recvmsg(tconn->data.socket, &msg, size, msg.msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700527 if (rv == size)
528 break;
529
530 /* Note:
531 * ECONNRESET other side closed the connection
532 * ERESTARTSYS (on sock) we got a signal
533 */
534
535 if (rv < 0) {
536 if (rv == -ECONNRESET)
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100537 conn_info(tconn, "sock was reset by peer\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700538 else if (rv != -ERESTARTSYS)
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100539 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700540 break;
541 } else if (rv == 0) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100542 conn_info(tconn, "sock was shut down by peer\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700543 break;
544 } else {
545 /* signal came in, or peer/link went down,
546 * after we read a partial message
547 */
548 /* D_ASSERT(signal_pending(current)); */
549 break;
550 }
551 };
552
553 set_fs(oldfs);
554
555 if (rv != size)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100556 conn_request_state(tconn, NS(conn, C_BROKEN_PIPE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700557
558 return rv;
559}
560
Andreas Gruenbacherc6967742011-03-17 17:15:20 +0100561static int drbd_recv_all(struct drbd_tconn *tconn, void *buf, size_t size)
562{
563 int err;
564
565 err = drbd_recv(tconn, buf, size);
566 if (err != size) {
567 if (err >= 0)
568 err = -EIO;
569 } else
570 err = 0;
571 return err;
572}
573
Andreas Gruenbachera5c31902011-03-24 03:28:04 +0100574static int drbd_recv_all_warn(struct drbd_tconn *tconn, void *buf, size_t size)
575{
576 int err;
577
578 err = drbd_recv_all(tconn, buf, size);
579 if (err && !signal_pending(current))
580 conn_warn(tconn, "short read (expected size %d)\n", (int)size);
581 return err;
582}
583
Lars Ellenberg5dbf1672010-05-25 16:18:01 +0200584/* quoting tcp(7):
585 * On individual connections, the socket buffer size must be set prior to the
586 * listen(2) or connect(2) calls in order to have it take effect.
587 * This is our wrapper to do so.
588 */
589static void drbd_setbufsize(struct socket *sock, unsigned int snd,
590 unsigned int rcv)
591{
592 /* open coded SO_SNDBUF, SO_RCVBUF */
593 if (snd) {
594 sock->sk->sk_sndbuf = snd;
595 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
596 }
597 if (rcv) {
598 sock->sk->sk_rcvbuf = rcv;
599 sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
600 }
601}
602
Philipp Reisnereac3e992011-02-07 14:05:07 +0100603static struct socket *drbd_try_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700604{
605 const char *what;
606 struct socket *sock;
607 struct sockaddr_in6 src_in6;
608 int err;
609 int disconnect_on_error = 1;
610
Philipp Reisnereac3e992011-02-07 14:05:07 +0100611 if (!get_net_conf(tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700612 return NULL;
613
614 what = "sock_create_kern";
Philipp Reisnereac3e992011-02-07 14:05:07 +0100615 err = sock_create_kern(((struct sockaddr *)tconn->net_conf->my_addr)->sa_family,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700616 SOCK_STREAM, IPPROTO_TCP, &sock);
617 if (err < 0) {
618 sock = NULL;
619 goto out;
620 }
621
622 sock->sk->sk_rcvtimeo =
Philipp Reisnereac3e992011-02-07 14:05:07 +0100623 sock->sk->sk_sndtimeo = tconn->net_conf->try_connect_int*HZ;
624 drbd_setbufsize(sock, tconn->net_conf->sndbuf_size,
625 tconn->net_conf->rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700626
627 /* explicitly bind to the configured IP as source IP
628 * for the outgoing connections.
629 * This is needed for multihomed hosts and to be
630 * able to use lo: interfaces for drbd.
631 * Make sure to use 0 as port number, so linux selects
632 * a free one dynamically.
633 */
Philipp Reisnereac3e992011-02-07 14:05:07 +0100634 memcpy(&src_in6, tconn->net_conf->my_addr,
635 min_t(int, tconn->net_conf->my_addr_len, sizeof(src_in6)));
636 if (((struct sockaddr *)tconn->net_conf->my_addr)->sa_family == AF_INET6)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700637 src_in6.sin6_port = 0;
638 else
639 ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */
640
641 what = "bind before connect";
642 err = sock->ops->bind(sock,
643 (struct sockaddr *) &src_in6,
Philipp Reisnereac3e992011-02-07 14:05:07 +0100644 tconn->net_conf->my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700645 if (err < 0)
646 goto out;
647
648 /* connect may fail, peer not yet available.
649 * stay C_WF_CONNECTION, don't go Disconnecting! */
650 disconnect_on_error = 0;
651 what = "connect";
652 err = sock->ops->connect(sock,
Philipp Reisnereac3e992011-02-07 14:05:07 +0100653 (struct sockaddr *)tconn->net_conf->peer_addr,
654 tconn->net_conf->peer_addr_len, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700655
656out:
657 if (err < 0) {
658 if (sock) {
659 sock_release(sock);
660 sock = NULL;
661 }
662 switch (-err) {
663 /* timeout, busy, signal pending */
664 case ETIMEDOUT: case EAGAIN: case EINPROGRESS:
665 case EINTR: case ERESTARTSYS:
666 /* peer not (yet) available, network problem */
667 case ECONNREFUSED: case ENETUNREACH:
668 case EHOSTDOWN: case EHOSTUNREACH:
669 disconnect_on_error = 0;
670 break;
671 default:
Philipp Reisnereac3e992011-02-07 14:05:07 +0100672 conn_err(tconn, "%s failed, err = %d\n", what, err);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700673 }
674 if (disconnect_on_error)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100675 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700676 }
Philipp Reisnereac3e992011-02-07 14:05:07 +0100677 put_net_conf(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700678 return sock;
679}
680
Philipp Reisner76536202011-02-07 14:09:54 +0100681static struct socket *drbd_wait_for_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700682{
683 int timeo, err;
684 struct socket *s_estab = NULL, *s_listen;
685 const char *what;
686
Philipp Reisner76536202011-02-07 14:09:54 +0100687 if (!get_net_conf(tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700688 return NULL;
689
690 what = "sock_create_kern";
Philipp Reisner76536202011-02-07 14:09:54 +0100691 err = sock_create_kern(((struct sockaddr *)tconn->net_conf->my_addr)->sa_family,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700692 SOCK_STREAM, IPPROTO_TCP, &s_listen);
693 if (err) {
694 s_listen = NULL;
695 goto out;
696 }
697
Philipp Reisner76536202011-02-07 14:09:54 +0100698 timeo = tconn->net_conf->try_connect_int * HZ;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700699 timeo += (random32() & 1) ? timeo / 7 : -timeo / 7; /* 28.5% random jitter */
700
701 s_listen->sk->sk_reuse = 1; /* SO_REUSEADDR */
702 s_listen->sk->sk_rcvtimeo = timeo;
703 s_listen->sk->sk_sndtimeo = timeo;
Philipp Reisner76536202011-02-07 14:09:54 +0100704 drbd_setbufsize(s_listen, tconn->net_conf->sndbuf_size,
705 tconn->net_conf->rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700706
707 what = "bind before listen";
708 err = s_listen->ops->bind(s_listen,
Philipp Reisner76536202011-02-07 14:09:54 +0100709 (struct sockaddr *) tconn->net_conf->my_addr,
710 tconn->net_conf->my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700711 if (err < 0)
712 goto out;
713
Philipp Reisner76536202011-02-07 14:09:54 +0100714 err = drbd_accept(&what, s_listen, &s_estab);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700715
716out:
717 if (s_listen)
718 sock_release(s_listen);
719 if (err < 0) {
720 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
Philipp Reisner76536202011-02-07 14:09:54 +0100721 conn_err(tconn, "%s failed, err = %d\n", what, err);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100722 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700723 }
724 }
Philipp Reisner76536202011-02-07 14:09:54 +0100725 put_net_conf(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700726
727 return s_estab;
728}
729
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200730static int decode_header(struct drbd_tconn *, void *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700731
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200732static int send_first_packet(struct drbd_tconn *tconn, struct drbd_socket *sock,
733 enum drbd_packet cmd)
734{
735 if (!conn_prepare_command(tconn, sock))
736 return -EIO;
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200737 return conn_send_command(tconn, sock, cmd, 0, NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700738}
739
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200740static int receive_first_packet(struct drbd_tconn *tconn, struct socket *sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700741{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200742 unsigned int header_size = drbd_header_size(tconn);
743 struct packet_info pi;
744 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700745
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200746 err = drbd_recv_short(sock, tconn->data.rbuf, header_size, 0);
747 if (err != header_size) {
748 if (err >= 0)
749 err = -EIO;
750 return err;
751 }
752 err = decode_header(tconn, tconn->data.rbuf, &pi);
753 if (err)
754 return err;
755 return pi.cmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700756}
757
758/**
759 * drbd_socket_okay() - Free the socket if its connection is not okay
Philipp Reisnerb411b362009-09-25 16:07:19 -0700760 * @sock: pointer to the pointer to the socket.
761 */
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100762static int drbd_socket_okay(struct socket **sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700763{
764 int rr;
765 char tb[4];
766
767 if (!*sock)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100768 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700769
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100770 rr = drbd_recv_short(*sock, tb, 4, MSG_DONTWAIT | MSG_PEEK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700771
772 if (rr > 0 || rr == -EAGAIN) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100773 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700774 } else {
775 sock_release(*sock);
776 *sock = NULL;
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100777 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700778 }
779}
Philipp Reisner2325eb62011-03-15 16:56:18 +0100780/* Gets called if a connection is established, or if a new minor gets created
781 in a connection */
782int drbd_connected(int vnr, void *p, void *data)
Philipp Reisner907599e2011-02-08 11:25:37 +0100783{
784 struct drbd_conf *mdev = (struct drbd_conf *)p;
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100785 int err;
Philipp Reisner907599e2011-02-08 11:25:37 +0100786
787 atomic_set(&mdev->packet_seq, 0);
788 mdev->peer_seq = 0;
789
Philipp Reisner8410da82011-02-11 20:11:10 +0100790 mdev->state_mutex = mdev->tconn->agreed_pro_version < 100 ?
791 &mdev->tconn->cstate_mutex :
792 &mdev->own_state_mutex;
793
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100794 err = drbd_send_sync_param(mdev);
795 if (!err)
796 err = drbd_send_sizes(mdev, 0, 0);
797 if (!err)
798 err = drbd_send_uuids(mdev);
799 if (!err)
800 err = drbd_send_state(mdev);
Philipp Reisner907599e2011-02-08 11:25:37 +0100801 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
802 clear_bit(RESIZE_PENDING, &mdev->flags);
Philipp Reisner8b924f12011-03-01 11:08:28 +0100803 mod_timer(&mdev->request_timer, jiffies + HZ); /* just start it here. */
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100804 return err;
Philipp Reisner907599e2011-02-08 11:25:37 +0100805}
806
Philipp Reisnerb411b362009-09-25 16:07:19 -0700807/*
808 * return values:
809 * 1 yes, we have a valid connection
810 * 0 oops, did not work out, please try again
811 * -1 peer talks different language,
812 * no point in trying again, please go standalone.
813 * -2 We do not have a network config...
814 */
Philipp Reisner907599e2011-02-08 11:25:37 +0100815static int drbd_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700816{
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200817 struct socket *sock, *msock;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700818 int try, h, ok;
819
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100820 if (conn_request_state(tconn, NS(conn, C_WF_CONNECTION), CS_VERBOSE) < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700821 return -2;
822
Philipp Reisner907599e2011-02-08 11:25:37 +0100823 clear_bit(DISCARD_CONCURRENT, &tconn->flags);
Andreas Gruenbacher0916e0e2011-03-21 14:10:15 +0100824
825 /* Assume that the peer only understands protocol 80 until we know better. */
826 tconn->agreed_pro_version = 80;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700827
Philipp Reisnerb411b362009-09-25 16:07:19 -0700828 do {
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200829 struct socket *s;
830
Philipp Reisnerb411b362009-09-25 16:07:19 -0700831 for (try = 0;;) {
832 /* 3 tries, this should take less than a second! */
Philipp Reisner907599e2011-02-08 11:25:37 +0100833 s = drbd_try_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700834 if (s || ++try >= 3)
835 break;
836 /* give the other side time to call bind() & listen() */
Philipp Reisner20ee6392011-01-18 15:28:59 +0100837 schedule_timeout_interruptible(HZ / 10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700838 }
839
840 if (s) {
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200841 if (!tconn->data.socket) {
842 tconn->data.socket = s;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200843 send_first_packet(tconn, &tconn->data, P_INITIAL_DATA);
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200844 } else if (!tconn->meta.socket) {
845 tconn->meta.socket = s;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200846 send_first_packet(tconn, &tconn->meta, P_INITIAL_META);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700847 } else {
Philipp Reisner907599e2011-02-08 11:25:37 +0100848 conn_err(tconn, "Logic error in drbd_connect()\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700849 goto out_release_sockets;
850 }
851 }
852
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200853 if (tconn->data.socket && tconn->meta.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100854 schedule_timeout_interruptible(tconn->net_conf->ping_timeo*HZ/10);
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200855 ok = drbd_socket_okay(&tconn->data.socket);
856 ok = drbd_socket_okay(&tconn->meta.socket) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700857 if (ok)
858 break;
859 }
860
861retry:
Philipp Reisner907599e2011-02-08 11:25:37 +0100862 s = drbd_wait_for_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700863 if (s) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200864 try = receive_first_packet(tconn, s);
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200865 drbd_socket_okay(&tconn->data.socket);
866 drbd_socket_okay(&tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700867 switch (try) {
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200868 case P_INITIAL_DATA:
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200869 if (tconn->data.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100870 conn_warn(tconn, "initial packet S crossed\n");
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200871 sock_release(tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700872 }
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200873 tconn->data.socket = s;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700874 break;
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200875 case P_INITIAL_META:
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200876 if (tconn->meta.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100877 conn_warn(tconn, "initial packet M crossed\n");
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200878 sock_release(tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700879 }
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200880 tconn->meta.socket = s;
Philipp Reisner907599e2011-02-08 11:25:37 +0100881 set_bit(DISCARD_CONCURRENT, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700882 break;
883 default:
Philipp Reisner907599e2011-02-08 11:25:37 +0100884 conn_warn(tconn, "Error receiving initial packet\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700885 sock_release(s);
886 if (random32() & 1)
887 goto retry;
888 }
889 }
890
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100891 if (tconn->cstate <= C_DISCONNECTING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700892 goto out_release_sockets;
893 if (signal_pending(current)) {
894 flush_signals(current);
895 smp_rmb();
Philipp Reisner907599e2011-02-08 11:25:37 +0100896 if (get_t_state(&tconn->receiver) == EXITING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700897 goto out_release_sockets;
898 }
899
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200900 if (tconn->data.socket && &tconn->meta.socket) {
901 ok = drbd_socket_okay(&tconn->data.socket);
902 ok = drbd_socket_okay(&tconn->meta.socket) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700903 if (ok)
904 break;
905 }
906 } while (1);
907
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200908 sock = tconn->data.socket;
909 msock = tconn->meta.socket;
910
Philipp Reisnerb411b362009-09-25 16:07:19 -0700911 msock->sk->sk_reuse = 1; /* SO_REUSEADDR */
912 sock->sk->sk_reuse = 1; /* SO_REUSEADDR */
913
914 sock->sk->sk_allocation = GFP_NOIO;
915 msock->sk->sk_allocation = GFP_NOIO;
916
917 sock->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
918 msock->sk->sk_priority = TC_PRIO_INTERACTIVE;
919
Philipp Reisnerb411b362009-09-25 16:07:19 -0700920 /* NOT YET ...
Philipp Reisner907599e2011-02-08 11:25:37 +0100921 * sock->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700922 * sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
Andreas Gruenbacher60381782011-03-28 17:05:50 +0200923 * first set it to the P_CONNECTION_FEATURES timeout,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700924 * which we set to 4x the configured ping_timeout. */
925 sock->sk->sk_sndtimeo =
Philipp Reisner907599e2011-02-08 11:25:37 +0100926 sock->sk->sk_rcvtimeo = tconn->net_conf->ping_timeo*4*HZ/10;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700927
Philipp Reisner907599e2011-02-08 11:25:37 +0100928 msock->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
929 msock->sk->sk_rcvtimeo = tconn->net_conf->ping_int*HZ;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700930
931 /* we don't want delays.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300932 * we use TCP_CORK where appropriate, though */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700933 drbd_tcp_nodelay(sock);
934 drbd_tcp_nodelay(msock);
935
Philipp Reisner907599e2011-02-08 11:25:37 +0100936 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700937
Andreas Gruenbacher60381782011-03-28 17:05:50 +0200938 h = drbd_do_features(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:
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200968 if (tconn->data.socket) {
969 sock_release(tconn->data.socket);
970 tconn->data.socket = NULL;
971 }
972 if (tconn->meta.socket) {
973 sock_release(tconn->meta.socket);
974 tconn->meta.socket = NULL;
975 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700976 return -1;
977}
978
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200979static int decode_header(struct drbd_tconn *tconn, void *header, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700980{
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200981 unsigned int header_size = drbd_header_size(tconn);
982
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +0200983 if (header_size == sizeof(struct p_header100) &&
984 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC_100)) {
985 struct p_header100 *h = header;
986 if (h->pad != 0) {
987 conn_err(tconn, "Header padding is not zero\n");
988 return -EINVAL;
989 }
990 pi->vnr = be16_to_cpu(h->volume);
991 pi->cmd = be16_to_cpu(h->command);
992 pi->size = be32_to_cpu(h->length);
993 } else if (header_size == sizeof(struct p_header95) &&
994 *(__be16 *)header == cpu_to_be16(DRBD_MAGIC_BIG)) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200995 struct p_header95 *h = header;
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200996 pi->cmd = be16_to_cpu(h->command);
Andreas Gruenbacherb55d84b2011-03-22 13:17:47 +0100997 pi->size = be32_to_cpu(h->length);
998 pi->vnr = 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200999 } else if (header_size == sizeof(struct p_header80) &&
1000 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC)) {
1001 struct p_header80 *h = header;
1002 pi->cmd = be16_to_cpu(h->command);
1003 pi->size = be16_to_cpu(h->length);
Philipp Reisner77351055b2011-02-07 17:24:26 +01001004 pi->vnr = 0;
Philipp Reisner02918be2010-08-20 14:35:10 +02001005 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001006 conn_err(tconn, "Wrong magic value 0x%08x in protocol version %d\n",
1007 be32_to_cpu(*(__be32 *)header),
1008 tconn->agreed_pro_version);
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001009 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001010 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001011 pi->data = header + header_size;
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001012 return 0;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001013}
1014
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001015static int drbd_recv_header(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner257d0af2011-01-26 12:15:29 +01001016{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001017 void *buffer = tconn->data.rbuf;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001018 int err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001019
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001020 err = drbd_recv_all_warn(tconn, buffer, drbd_header_size(tconn));
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001021 if (err)
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001022 return err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001023
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001024 err = decode_header(tconn, buffer, pi);
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001025 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001026
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001027 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001028}
1029
Philipp Reisner2451fc32010-08-24 13:43:11 +02001030static void drbd_flush(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001031{
1032 int rv;
1033
1034 if (mdev->write_ordering >= WO_bdev_flush && get_ldev(mdev)) {
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +04001035 rv = blkdev_issue_flush(mdev->ldev->backing_bdev, GFP_KERNEL,
Christoph Hellwigdd3932e2010-09-16 20:51:46 +02001036 NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001037 if (rv) {
1038 dev_err(DEV, "local disk flush failed with status %d\n", rv);
1039 /* would rather check on EOPNOTSUPP, but that is not reliable.
1040 * don't try again for ANY return value != 0
1041 * if (rv == -EOPNOTSUPP) */
1042 drbd_bump_write_ordering(mdev, WO_drain_io);
1043 }
1044 put_ldev(mdev);
1045 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001046}
1047
1048/**
1049 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
1050 * @mdev: DRBD device.
1051 * @epoch: Epoch object.
1052 * @ev: Epoch event.
1053 */
1054static enum finish_epoch drbd_may_finish_epoch(struct drbd_conf *mdev,
1055 struct drbd_epoch *epoch,
1056 enum epoch_event ev)
1057{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001058 int epoch_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001059 struct drbd_epoch *next_epoch;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001060 enum finish_epoch rv = FE_STILL_LIVE;
1061
1062 spin_lock(&mdev->epoch_lock);
1063 do {
1064 next_epoch = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001065
1066 epoch_size = atomic_read(&epoch->epoch_size);
1067
1068 switch (ev & ~EV_CLEANUP) {
1069 case EV_PUT:
1070 atomic_dec(&epoch->active);
1071 break;
1072 case EV_GOT_BARRIER_NR:
1073 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001074 break;
1075 case EV_BECAME_LAST:
1076 /* nothing to do*/
1077 break;
1078 }
1079
Philipp Reisnerb411b362009-09-25 16:07:19 -07001080 if (epoch_size != 0 &&
1081 atomic_read(&epoch->active) == 0 &&
Philipp Reisner2451fc32010-08-24 13:43:11 +02001082 test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001083 if (!(ev & EV_CLEANUP)) {
1084 spin_unlock(&mdev->epoch_lock);
1085 drbd_send_b_ack(mdev, epoch->barrier_nr, epoch_size);
1086 spin_lock(&mdev->epoch_lock);
1087 }
1088 dec_unacked(mdev);
1089
1090 if (mdev->current_epoch != epoch) {
1091 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1092 list_del(&epoch->list);
1093 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
1094 mdev->epochs--;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001095 kfree(epoch);
1096
1097 if (rv == FE_STILL_LIVE)
1098 rv = FE_DESTROYED;
1099 } else {
1100 epoch->flags = 0;
1101 atomic_set(&epoch->epoch_size, 0);
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001102 /* atomic_set(&epoch->active, 0); is already zero */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001103 if (rv == FE_STILL_LIVE)
1104 rv = FE_RECYCLED;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001105 wake_up(&mdev->ee_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001106 }
1107 }
1108
1109 if (!next_epoch)
1110 break;
1111
1112 epoch = next_epoch;
1113 } while (1);
1114
1115 spin_unlock(&mdev->epoch_lock);
1116
Philipp Reisnerb411b362009-09-25 16:07:19 -07001117 return rv;
1118}
1119
1120/**
1121 * drbd_bump_write_ordering() - Fall back to an other write ordering method
1122 * @mdev: DRBD device.
1123 * @wo: Write ordering method to try.
1124 */
1125void drbd_bump_write_ordering(struct drbd_conf *mdev, enum write_ordering_e wo) __must_hold(local)
1126{
1127 enum write_ordering_e pwo;
1128 static char *write_ordering_str[] = {
1129 [WO_none] = "none",
1130 [WO_drain_io] = "drain",
1131 [WO_bdev_flush] = "flush",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001132 };
1133
1134 pwo = mdev->write_ordering;
1135 wo = min(pwo, wo);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001136 if (wo == WO_bdev_flush && mdev->ldev->dc.no_disk_flush)
1137 wo = WO_drain_io;
1138 if (wo == WO_drain_io && mdev->ldev->dc.no_disk_drain)
1139 wo = WO_none;
1140 mdev->write_ordering = wo;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001141 if (pwo != mdev->write_ordering || wo == WO_bdev_flush)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001142 dev_info(DEV, "Method to ensure write ordering: %s\n", write_ordering_str[mdev->write_ordering]);
1143}
1144
1145/**
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001146 * drbd_submit_peer_request()
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001147 * @mdev: DRBD device.
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001148 * @peer_req: peer request
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001149 * @rw: flag field, see bio->bi_rw
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001150 *
1151 * May spread the pages to multiple bios,
1152 * depending on bio_add_page restrictions.
1153 *
1154 * Returns 0 if all bios have been submitted,
1155 * -ENOMEM if we could not allocate enough bios,
1156 * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a
1157 * single page to an empty bio (which should never happen and likely indicates
1158 * that the lower level IO stack is in some way broken). This has been observed
1159 * on certain Xen deployments.
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001160 */
1161/* TODO allocate from our own bio_set. */
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001162int drbd_submit_peer_request(struct drbd_conf *mdev,
1163 struct drbd_peer_request *peer_req,
1164 const unsigned rw, const int fault_type)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001165{
1166 struct bio *bios = NULL;
1167 struct bio *bio;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001168 struct page *page = peer_req->pages;
1169 sector_t sector = peer_req->i.sector;
1170 unsigned ds = peer_req->i.size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001171 unsigned n_bios = 0;
1172 unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001173 int err = -ENOMEM;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001174
1175 /* In most cases, we will only need one bio. But in case the lower
1176 * level restrictions happen to be different at this offset on this
1177 * side than those of the sending peer, we may need to submit the
Lars Ellenbergda4a75d2011-02-23 17:02:01 +01001178 * request in more than one bio.
1179 *
1180 * Plain bio_alloc is good enough here, this is no DRBD internally
1181 * generated bio, but a bio allocated on behalf of the peer.
1182 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001183next_bio:
1184 bio = bio_alloc(GFP_NOIO, nr_pages);
1185 if (!bio) {
1186 dev_err(DEV, "submit_ee: Allocation of a bio failed\n");
1187 goto fail;
1188 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001189 /* > peer_req->i.sector, unless this is the first bio */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001190 bio->bi_sector = sector;
1191 bio->bi_bdev = mdev->ldev->backing_bdev;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001192 bio->bi_rw = rw;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001193 bio->bi_private = peer_req;
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001194 bio->bi_end_io = drbd_peer_request_endio;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001195
1196 bio->bi_next = bios;
1197 bios = bio;
1198 ++n_bios;
1199
1200 page_chain_for_each(page) {
1201 unsigned len = min_t(unsigned, ds, PAGE_SIZE);
1202 if (!bio_add_page(bio, page, len, 0)) {
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001203 /* A single page must always be possible!
1204 * But in case it fails anyways,
1205 * we deal with it, and complain (below). */
1206 if (bio->bi_vcnt == 0) {
1207 dev_err(DEV,
1208 "bio_add_page failed for len=%u, "
1209 "bi_vcnt=0 (bi_sector=%llu)\n",
1210 len, (unsigned long long)bio->bi_sector);
1211 err = -ENOSPC;
1212 goto fail;
1213 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001214 goto next_bio;
1215 }
1216 ds -= len;
1217 sector += len >> 9;
1218 --nr_pages;
1219 }
1220 D_ASSERT(page == NULL);
1221 D_ASSERT(ds == 0);
1222
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001223 atomic_set(&peer_req->pending_bios, n_bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001224 do {
1225 bio = bios;
1226 bios = bios->bi_next;
1227 bio->bi_next = NULL;
1228
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001229 drbd_generic_make_request(mdev, fault_type, bio);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001230 } while (bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001231 return 0;
1232
1233fail:
1234 while (bios) {
1235 bio = bios;
1236 bios = bios->bi_next;
1237 bio_put(bio);
1238 }
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001239 return err;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001240}
1241
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001242static void drbd_remove_epoch_entry_interval(struct drbd_conf *mdev,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001243 struct drbd_peer_request *peer_req)
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001244{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001245 struct drbd_interval *i = &peer_req->i;
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001246
1247 drbd_remove_interval(&mdev->write_requests, i);
1248 drbd_clear_interval(i);
1249
Andreas Gruenbacher6c852be2011-02-04 15:38:52 +01001250 /* Wake up any processes waiting for this peer request to complete. */
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001251 if (i->waiting)
1252 wake_up(&mdev->misc_wait);
1253}
1254
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001255static int receive_Barrier(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001256{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001257 struct drbd_conf *mdev;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001258 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001259 struct p_barrier *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001260 struct drbd_epoch *epoch;
1261
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001262 mdev = vnr_to_mdev(tconn, pi->vnr);
1263 if (!mdev)
1264 return -EIO;
1265
Philipp Reisnerb411b362009-09-25 16:07:19 -07001266 inc_unacked(mdev);
1267
Philipp Reisnerb411b362009-09-25 16:07:19 -07001268 mdev->current_epoch->barrier_nr = p->barrier;
1269 rv = drbd_may_finish_epoch(mdev, mdev->current_epoch, EV_GOT_BARRIER_NR);
1270
1271 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
1272 * the activity log, which means it would not be resynced in case the
1273 * R_PRIMARY crashes now.
1274 * Therefore we must send the barrier_ack after the barrier request was
1275 * completed. */
1276 switch (mdev->write_ordering) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001277 case WO_none:
1278 if (rv == FE_RECYCLED)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001279 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001280
1281 /* receiver context, in the writeout path of the other node.
1282 * avoid potential distributed deadlock */
1283 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1284 if (epoch)
1285 break;
1286 else
1287 dev_warn(DEV, "Allocation of an epoch failed, slowing down\n");
1288 /* Fall through */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001289
1290 case WO_bdev_flush:
1291 case WO_drain_io:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001292 drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
Philipp Reisner2451fc32010-08-24 13:43:11 +02001293 drbd_flush(mdev);
1294
1295 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1296 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1297 if (epoch)
1298 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001299 }
1300
Philipp Reisner2451fc32010-08-24 13:43:11 +02001301 epoch = mdev->current_epoch;
1302 wait_event(mdev->ee_wait, atomic_read(&epoch->epoch_size) == 0);
1303
1304 D_ASSERT(atomic_read(&epoch->active) == 0);
1305 D_ASSERT(epoch->flags == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001306
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001307 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001308 default:
1309 dev_err(DEV, "Strangeness in mdev->write_ordering %d\n", mdev->write_ordering);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001310 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001311 }
1312
1313 epoch->flags = 0;
1314 atomic_set(&epoch->epoch_size, 0);
1315 atomic_set(&epoch->active, 0);
1316
1317 spin_lock(&mdev->epoch_lock);
1318 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1319 list_add(&epoch->list, &mdev->current_epoch->list);
1320 mdev->current_epoch = epoch;
1321 mdev->epochs++;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001322 } else {
1323 /* The current_epoch got recycled while we allocated this one... */
1324 kfree(epoch);
1325 }
1326 spin_unlock(&mdev->epoch_lock);
1327
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001328 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001329}
1330
1331/* used from receive_RSDataReply (recv_resync_read)
1332 * and from receive_Data */
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001333static struct drbd_peer_request *
1334read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector,
1335 int data_size) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001336{
Lars Ellenberg66660322010-04-06 12:15:04 +02001337 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001338 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001339 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001340 int dgs, ds, err;
Philipp Reisnera0638452011-01-19 14:31:32 +01001341 void *dig_in = mdev->tconn->int_dig_in;
1342 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001343 unsigned long *data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001344
Philipp Reisnera0638452011-01-19 14:31:32 +01001345 dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_r_tfm) ?
1346 crypto_hash_digestsize(mdev->tconn->integrity_r_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001347
1348 if (dgs) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001349 /*
1350 * FIXME: Receive the incoming digest into the receive buffer
1351 * here, together with its struct p_data?
1352 */
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001353 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1354 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001355 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001356 }
1357
1358 data_size -= dgs;
1359
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001360 if (!expect(data_size != 0))
1361 return NULL;
1362 if (!expect(IS_ALIGNED(data_size, 512)))
1363 return NULL;
1364 if (!expect(data_size <= DRBD_MAX_BIO_SIZE))
1365 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001366
Lars Ellenberg66660322010-04-06 12:15:04 +02001367 /* even though we trust out peer,
1368 * we sometimes have to double check. */
1369 if (sector + (data_size>>9) > capacity) {
Lars Ellenbergfdda6542011-01-24 15:11:01 +01001370 dev_err(DEV, "request from peer beyond end of local disk: "
1371 "capacity: %llus < sector: %llus + size: %u\n",
Lars Ellenberg66660322010-04-06 12:15:04 +02001372 (unsigned long long)capacity,
1373 (unsigned long long)sector, data_size);
1374 return NULL;
1375 }
1376
Philipp Reisnerb411b362009-09-25 16:07:19 -07001377 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1378 * "criss-cross" setup, that might cause write-out on some other DRBD,
1379 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02001380 peer_req = drbd_alloc_peer_req(mdev, id, sector, data_size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001381 if (!peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001382 return NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001383
Philipp Reisnerb411b362009-09-25 16:07:19 -07001384 ds = data_size;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001385 page = peer_req->pages;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001386 page_chain_for_each(page) {
1387 unsigned len = min_t(int, ds, PAGE_SIZE);
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001388 data = kmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001389 err = drbd_recv_all_warn(mdev->tconn, data, len);
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +01001390 if (drbd_insert_fault(mdev, DRBD_FAULT_RECEIVE)) {
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001391 dev_err(DEV, "Fault injection: Corrupting data on receive\n");
1392 data[0] = data[0] ^ (unsigned long)-1;
1393 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001394 kunmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001395 if (err) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001396 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001397 return NULL;
1398 }
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001399 ds -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001400 }
1401
1402 if (dgs) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001403 drbd_csum_ee(mdev, mdev->tconn->integrity_r_tfm, peer_req, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001404 if (memcmp(dig_in, dig_vv, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001405 dev_err(DEV, "Digest integrity check FAILED: %llus +%u\n",
1406 (unsigned long long)sector, data_size);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001407 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001408 return NULL;
1409 }
1410 }
1411 mdev->recv_cnt += data_size>>9;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001412 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001413}
1414
1415/* drbd_drain_block() just takes a data block
1416 * out of the socket input buffer, and discards it.
1417 */
1418static int drbd_drain_block(struct drbd_conf *mdev, int data_size)
1419{
1420 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001421 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001422 void *data;
1423
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001424 if (!data_size)
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001425 return 0;
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001426
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001427 page = drbd_pp_alloc(mdev, 1, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001428
1429 data = kmap(page);
1430 while (data_size) {
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001431 unsigned int len = min_t(int, data_size, PAGE_SIZE);
1432
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001433 err = drbd_recv_all_warn(mdev->tconn, data, len);
1434 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001435 break;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001436 data_size -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001437 }
1438 kunmap(page);
Lars Ellenberg435f0742010-09-06 12:30:25 +02001439 drbd_pp_free(mdev, page, 0);
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001440 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001441}
1442
1443static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req,
1444 sector_t sector, int data_size)
1445{
1446 struct bio_vec *bvec;
1447 struct bio *bio;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001448 int dgs, err, i, expect;
Philipp Reisnera0638452011-01-19 14:31:32 +01001449 void *dig_in = mdev->tconn->int_dig_in;
1450 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001451
Philipp Reisnera0638452011-01-19 14:31:32 +01001452 dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_r_tfm) ?
1453 crypto_hash_digestsize(mdev->tconn->integrity_r_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001454
1455 if (dgs) {
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001456 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1457 if (err)
1458 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001459 }
1460
1461 data_size -= dgs;
1462
1463 /* optimistically update recv_cnt. if receiving fails below,
1464 * we disconnect anyways, and counters will be reset. */
1465 mdev->recv_cnt += data_size>>9;
1466
1467 bio = req->master_bio;
1468 D_ASSERT(sector == bio->bi_sector);
1469
1470 bio_for_each_segment(bvec, bio, i) {
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001471 void *mapped = kmap(bvec->bv_page) + bvec->bv_offset;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001472 expect = min_t(int, data_size, bvec->bv_len);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001473 err = drbd_recv_all_warn(mdev->tconn, mapped, expect);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001474 kunmap(bvec->bv_page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001475 if (err)
1476 return err;
1477 data_size -= expect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001478 }
1479
1480 if (dgs) {
Philipp Reisnera0638452011-01-19 14:31:32 +01001481 drbd_csum_bio(mdev, mdev->tconn->integrity_r_tfm, bio, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001482 if (memcmp(dig_in, dig_vv, dgs)) {
1483 dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n");
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001484 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001485 }
1486 }
1487
1488 D_ASSERT(data_size == 0);
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001489 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001490}
1491
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001492/*
1493 * e_end_resync_block() is called in asender context via
1494 * drbd_finish_peer_reqs().
1495 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001496static int e_end_resync_block(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001497{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001498 struct drbd_peer_request *peer_req =
1499 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001500 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001501 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001502 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001503
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001504 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001505
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001506 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1507 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001508 err = drbd_send_ack(mdev, P_RS_WRITE_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001509 } else {
1510 /* Record failure to sync */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001511 drbd_rs_failed_io(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001512
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001513 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001514 }
1515 dec_unacked(mdev);
1516
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001517 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001518}
1519
1520static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local)
1521{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001522 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001523
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001524 peer_req = read_in_block(mdev, ID_SYNCER, sector, data_size);
1525 if (!peer_req)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001526 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001527
1528 dec_rs_pending(mdev);
1529
Philipp Reisnerb411b362009-09-25 16:07:19 -07001530 inc_unacked(mdev);
1531 /* corresponding dec_unacked() in e_end_resync_block()
1532 * respective _drbd_clear_done_ee */
1533
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001534 peer_req->w.cb = e_end_resync_block;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001535
Philipp Reisner87eeee42011-01-19 14:16:30 +01001536 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001537 list_add(&peer_req->w.list, &mdev->sync_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001538 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001539
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001540 atomic_add(data_size >> 9, &mdev->rs_sect_ev);
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001541 if (drbd_submit_peer_request(mdev, peer_req, WRITE, DRBD_FAULT_RS_WR) == 0)
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001542 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001543
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001544 /* don't care for the reason here */
1545 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01001546 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001547 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001548 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001549
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001550 drbd_free_peer_req(mdev, peer_req);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001551fail:
1552 put_ldev(mdev);
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001553 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001554}
1555
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001556static struct drbd_request *
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001557find_request(struct drbd_conf *mdev, struct rb_root *root, u64 id,
1558 sector_t sector, bool missing_ok, const char *func)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001559{
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001560 struct drbd_request *req;
1561
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001562 /* Request object according to our peer */
1563 req = (struct drbd_request *)(unsigned long)id;
Andreas Gruenbacher5e472262011-01-27 14:42:51 +01001564 if (drbd_contains_interval(root, sector, &req->i) && req->i.local)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001565 return req;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001566 if (!missing_ok) {
1567 dev_err(DEV, "%s: failed to find request %lu, sector %llus\n", func,
1568 (unsigned long)id, (unsigned long long)sector);
1569 }
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001570 return NULL;
1571}
1572
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001573static int receive_DataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001574{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001575 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001576 struct drbd_request *req;
1577 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001578 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001579 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001580
1581 mdev = vnr_to_mdev(tconn, pi->vnr);
1582 if (!mdev)
1583 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001584
1585 sector = be64_to_cpu(p->sector);
1586
Philipp Reisner87eeee42011-01-19 14:16:30 +01001587 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001588 req = find_request(mdev, &mdev->read_requests, p->block_id, sector, false, __func__);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001589 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001590 if (unlikely(!req))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001591 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001592
Bart Van Assche24c48302011-05-21 18:32:29 +02001593 /* hlist_del(&req->collision) is done in _req_may_be_done, to avoid
Philipp Reisnerb411b362009-09-25 16:07:19 -07001594 * special casing it there for the various failure cases.
1595 * still no race with drbd_fail_pending_reads */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001596 err = recv_dless_read(mdev, req, sector, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001597 if (!err)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001598 req_mod(req, DATA_RECEIVED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001599 /* else: nothing. handled from drbd_disconnect...
1600 * I don't think we may complete this just yet
1601 * in case we are "on-disconnect: freeze" */
1602
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001603 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001604}
1605
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001606static int receive_RSDataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001607{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001608 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001609 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001610 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001611 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001612
1613 mdev = vnr_to_mdev(tconn, pi->vnr);
1614 if (!mdev)
1615 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001616
1617 sector = be64_to_cpu(p->sector);
1618 D_ASSERT(p->block_id == ID_SYNCER);
1619
1620 if (get_ldev(mdev)) {
1621 /* data is submitted to disk within recv_resync_read.
1622 * corresponding put_ldev done below on error,
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001623 * or in drbd_peer_request_endio. */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001624 err = recv_resync_read(mdev, sector, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001625 } else {
1626 if (__ratelimit(&drbd_ratelimit_state))
1627 dev_err(DEV, "Can not write resync data to local disk.\n");
1628
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001629 err = drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001630
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001631 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001632 }
1633
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001634 atomic_add(pi->size >> 9, &mdev->rs_sect_in);
Philipp Reisner778f2712010-07-06 11:14:00 +02001635
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001636 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001637}
1638
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001639static int w_restart_write(struct drbd_work *w, int cancel)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001640{
1641 struct drbd_request *req = container_of(w, struct drbd_request, w);
1642 struct drbd_conf *mdev = w->mdev;
1643 struct bio *bio;
1644 unsigned long start_time;
1645 unsigned long flags;
1646
1647 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
1648 if (!expect(req->rq_state & RQ_POSTPONED)) {
1649 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001650 return -EIO;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001651 }
1652 bio = req->master_bio;
1653 start_time = req->start_time;
1654 /* Postponed requests will not have their master_bio completed! */
1655 __req_mod(req, DISCARD_WRITE, NULL);
1656 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
1657
1658 while (__drbd_make_request(mdev, bio, start_time))
1659 /* retry */ ;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001660 return 0;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001661}
1662
1663static void restart_conflicting_writes(struct drbd_conf *mdev,
1664 sector_t sector, int size)
1665{
1666 struct drbd_interval *i;
1667 struct drbd_request *req;
1668
1669 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1670 if (!i->local)
1671 continue;
1672 req = container_of(i, struct drbd_request, i);
1673 if (req->rq_state & RQ_LOCAL_PENDING ||
1674 !(req->rq_state & RQ_POSTPONED))
1675 continue;
1676 if (expect(list_empty(&req->w.list))) {
1677 req->w.mdev = mdev;
1678 req->w.cb = w_restart_write;
1679 drbd_queue_work(&mdev->tconn->data.work, &req->w);
1680 }
1681 }
1682}
1683
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001684/*
1685 * e_end_block() is called in asender context via drbd_finish_peer_reqs().
Philipp Reisnerb411b362009-09-25 16:07:19 -07001686 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001687static int e_end_block(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001688{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001689 struct drbd_peer_request *peer_req =
1690 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001691 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001692 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001693 int err = 0, pcmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001694
Philipp Reisner89e58e72011-01-19 13:12:45 +01001695 if (mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001696 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001697 pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
1698 mdev->state.conn <= C_PAUSED_SYNC_T &&
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001699 peer_req->flags & EE_MAY_SET_IN_SYNC) ?
Philipp Reisnerb411b362009-09-25 16:07:19 -07001700 P_RS_WRITE_ACK : P_WRITE_ACK;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001701 err = drbd_send_ack(mdev, pcmd, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001702 if (pcmd == P_RS_WRITE_ACK)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001703 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001704 } else {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001705 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001706 /* we expect it to be marked out of sync anyways...
1707 * maybe assert this? */
1708 }
1709 dec_unacked(mdev);
1710 }
1711 /* we delete from the conflict detection hash _after_ we sent out the
1712 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
Philipp Reisner89e58e72011-01-19 13:12:45 +01001713 if (mdev->tconn->net_conf->two_primaries) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01001714 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001715 D_ASSERT(!drbd_interval_empty(&peer_req->i));
1716 drbd_remove_epoch_entry_interval(mdev, peer_req);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001717 if (peer_req->flags & EE_RESTART_REQUESTS)
1718 restart_conflicting_writes(mdev, sector, peer_req->i.size);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001719 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +01001720 } else
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001721 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001722
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001723 drbd_may_finish_epoch(mdev, peer_req->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001724
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001725 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001726}
1727
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001728static int e_send_ack(struct drbd_work *w, enum drbd_packet ack)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001729{
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001730 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001731 struct drbd_peer_request *peer_req =
1732 container_of(w, struct drbd_peer_request, w);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001733 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001734
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001735 err = drbd_send_ack(mdev, ack, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001736 dec_unacked(mdev);
1737
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001738 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001739}
1740
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001741static int e_send_discard_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001742{
1743 return e_send_ack(w, P_DISCARD_WRITE);
1744}
1745
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001746static int e_send_retry_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001747{
1748 struct drbd_tconn *tconn = w->mdev->tconn;
1749
1750 return e_send_ack(w, tconn->agreed_pro_version >= 100 ?
1751 P_RETRY_WRITE : P_DISCARD_WRITE);
1752}
1753
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001754static bool seq_greater(u32 a, u32 b)
1755{
1756 /*
1757 * We assume 32-bit wrap-around here.
1758 * For 24-bit wrap-around, we would have to shift:
1759 * a <<= 8; b <<= 8;
1760 */
1761 return (s32)a - (s32)b > 0;
1762}
1763
1764static u32 seq_max(u32 a, u32 b)
1765{
1766 return seq_greater(a, b) ? a : b;
1767}
1768
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001769static bool need_peer_seq(struct drbd_conf *mdev)
1770{
1771 struct drbd_tconn *tconn = mdev->tconn;
1772
1773 /*
1774 * We only need to keep track of the last packet_seq number of our peer
1775 * if we are in dual-primary mode and we have the discard flag set; see
1776 * handle_write_conflicts().
1777 */
1778 return tconn->net_conf->two_primaries &&
1779 test_bit(DISCARD_CONCURRENT, &tconn->flags);
1780}
1781
Andreas Gruenbacher43ae0772011-02-03 18:42:08 +01001782static void update_peer_seq(struct drbd_conf *mdev, unsigned int peer_seq)
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001783{
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001784 unsigned int newest_peer_seq;
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001785
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001786 if (need_peer_seq(mdev)) {
1787 spin_lock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001788 newest_peer_seq = seq_max(mdev->peer_seq, peer_seq);
1789 mdev->peer_seq = newest_peer_seq;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001790 spin_unlock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001791 /* wake up only if we actually changed mdev->peer_seq */
1792 if (peer_seq == newest_peer_seq)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001793 wake_up(&mdev->seq_wait);
1794 }
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001795}
1796
Philipp Reisnerb411b362009-09-25 16:07:19 -07001797/* Called from receive_Data.
1798 * Synchronize packets on sock with packets on msock.
1799 *
1800 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
1801 * packet traveling on msock, they are still processed in the order they have
1802 * been sent.
1803 *
1804 * Note: we don't care for Ack packets overtaking P_DATA packets.
1805 *
1806 * In case packet_seq is larger than mdev->peer_seq number, there are
1807 * outstanding packets on the msock. We wait for them to arrive.
1808 * In case we are the logically next packet, we update mdev->peer_seq
1809 * ourselves. Correctly handles 32bit wrap around.
1810 *
1811 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
1812 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
1813 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
1814 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
1815 *
1816 * returns 0 if we may process the packet,
1817 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001818static int wait_for_and_update_peer_seq(struct drbd_conf *mdev, const u32 peer_seq)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001819{
1820 DEFINE_WAIT(wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001821 long timeout;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001822 int ret;
1823
1824 if (!need_peer_seq(mdev))
1825 return 0;
1826
Philipp Reisnerb411b362009-09-25 16:07:19 -07001827 spin_lock(&mdev->peer_seq_lock);
1828 for (;;) {
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001829 if (!seq_greater(peer_seq - 1, mdev->peer_seq)) {
1830 mdev->peer_seq = seq_max(mdev->peer_seq, peer_seq);
1831 ret = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001832 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001833 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001834 if (signal_pending(current)) {
1835 ret = -ERESTARTSYS;
1836 break;
1837 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001838 prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001839 spin_unlock(&mdev->peer_seq_lock);
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001840 timeout = mdev->tconn->net_conf->ping_timeo*HZ/10;
1841 timeout = schedule_timeout(timeout);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001842 spin_lock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001843 if (!timeout) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001844 ret = -ETIMEDOUT;
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001845 dev_err(DEV, "Timed out waiting for missing ack packets; disconnecting\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001846 break;
1847 }
1848 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001849 spin_unlock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001850 finish_wait(&mdev->seq_wait, &wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001851 return ret;
1852}
1853
Lars Ellenberg688593c2010-11-17 22:25:03 +01001854/* see also bio_flags_to_wire()
1855 * DRBD_REQ_*, because we need to semantically map the flags to data packet
1856 * flags and back. We may replicate to other kernel versions. */
1857static unsigned long wire_flags_to_bio(struct drbd_conf *mdev, u32 dpf)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001858{
Lars Ellenberg688593c2010-11-17 22:25:03 +01001859 return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
1860 (dpf & DP_FUA ? REQ_FUA : 0) |
1861 (dpf & DP_FLUSH ? REQ_FLUSH : 0) |
1862 (dpf & DP_DISCARD ? REQ_DISCARD : 0);
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001863}
1864
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001865static void fail_postponed_requests(struct drbd_conf *mdev, sector_t sector,
1866 unsigned int size)
1867{
1868 struct drbd_interval *i;
1869
1870 repeat:
1871 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1872 struct drbd_request *req;
1873 struct bio_and_error m;
1874
1875 if (!i->local)
1876 continue;
1877 req = container_of(i, struct drbd_request, i);
1878 if (!(req->rq_state & RQ_POSTPONED))
1879 continue;
1880 req->rq_state &= ~RQ_POSTPONED;
1881 __req_mod(req, NEG_ACKED, &m);
1882 spin_unlock_irq(&mdev->tconn->req_lock);
1883 if (m.bio)
1884 complete_master_bio(mdev, &m);
1885 spin_lock_irq(&mdev->tconn->req_lock);
1886 goto repeat;
1887 }
1888}
1889
1890static int handle_write_conflicts(struct drbd_conf *mdev,
1891 struct drbd_peer_request *peer_req)
1892{
1893 struct drbd_tconn *tconn = mdev->tconn;
1894 bool resolve_conflicts = test_bit(DISCARD_CONCURRENT, &tconn->flags);
1895 sector_t sector = peer_req->i.sector;
1896 const unsigned int size = peer_req->i.size;
1897 struct drbd_interval *i;
1898 bool equal;
1899 int err;
1900
1901 /*
1902 * Inserting the peer request into the write_requests tree will prevent
1903 * new conflicting local requests from being added.
1904 */
1905 drbd_insert_interval(&mdev->write_requests, &peer_req->i);
1906
1907 repeat:
1908 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1909 if (i == &peer_req->i)
1910 continue;
1911
1912 if (!i->local) {
1913 /*
1914 * Our peer has sent a conflicting remote request; this
1915 * should not happen in a two-node setup. Wait for the
1916 * earlier peer request to complete.
1917 */
1918 err = drbd_wait_misc(mdev, i);
1919 if (err)
1920 goto out;
1921 goto repeat;
1922 }
1923
1924 equal = i->sector == sector && i->size == size;
1925 if (resolve_conflicts) {
1926 /*
1927 * If the peer request is fully contained within the
1928 * overlapping request, it can be discarded; otherwise,
1929 * it will be retried once all overlapping requests
1930 * have completed.
1931 */
1932 bool discard = i->sector <= sector && i->sector +
1933 (i->size >> 9) >= sector + (size >> 9);
1934
1935 if (!equal)
1936 dev_alert(DEV, "Concurrent writes detected: "
1937 "local=%llus +%u, remote=%llus +%u, "
1938 "assuming %s came first\n",
1939 (unsigned long long)i->sector, i->size,
1940 (unsigned long long)sector, size,
1941 discard ? "local" : "remote");
1942
1943 inc_unacked(mdev);
1944 peer_req->w.cb = discard ? e_send_discard_write :
1945 e_send_retry_write;
1946 list_add_tail(&peer_req->w.list, &mdev->done_ee);
1947 wake_asender(mdev->tconn);
1948
1949 err = -ENOENT;
1950 goto out;
1951 } else {
1952 struct drbd_request *req =
1953 container_of(i, struct drbd_request, i);
1954
1955 if (!equal)
1956 dev_alert(DEV, "Concurrent writes detected: "
1957 "local=%llus +%u, remote=%llus +%u\n",
1958 (unsigned long long)i->sector, i->size,
1959 (unsigned long long)sector, size);
1960
1961 if (req->rq_state & RQ_LOCAL_PENDING ||
1962 !(req->rq_state & RQ_POSTPONED)) {
1963 /*
1964 * Wait for the node with the discard flag to
1965 * decide if this request will be discarded or
1966 * retried. Requests that are discarded will
1967 * disappear from the write_requests tree.
1968 *
1969 * In addition, wait for the conflicting
1970 * request to finish locally before submitting
1971 * the conflicting peer request.
1972 */
1973 err = drbd_wait_misc(mdev, &req->i);
1974 if (err) {
1975 _conn_request_state(mdev->tconn,
1976 NS(conn, C_TIMEOUT),
1977 CS_HARD);
1978 fail_postponed_requests(mdev, sector, size);
1979 goto out;
1980 }
1981 goto repeat;
1982 }
1983 /*
1984 * Remember to restart the conflicting requests after
1985 * the new peer request has completed.
1986 */
1987 peer_req->flags |= EE_RESTART_REQUESTS;
1988 }
1989 }
1990 err = 0;
1991
1992 out:
1993 if (err)
1994 drbd_remove_epoch_entry_interval(mdev, peer_req);
1995 return err;
1996}
1997
Philipp Reisnerb411b362009-09-25 16:07:19 -07001998/* mirrored write */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001999static int receive_Data(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002000{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002001 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002002 sector_t sector;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002003 struct drbd_peer_request *peer_req;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002004 struct p_data *p = pi->data;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002005 u32 peer_seq = be32_to_cpu(p->seq_num);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002006 int rw = WRITE;
2007 u32 dp_flags;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002008 int err;
2009
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002010 mdev = vnr_to_mdev(tconn, pi->vnr);
2011 if (!mdev)
2012 return -EIO;
2013
Philipp Reisnerb411b362009-09-25 16:07:19 -07002014 if (!get_ldev(mdev)) {
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002015 int err2;
2016
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002017 err = wait_for_and_update_peer_seq(mdev, peer_seq);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002018 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002019 atomic_inc(&mdev->current_epoch->epoch_size);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002020 err2 = drbd_drain_block(mdev, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002021 if (!err)
2022 err = err2;
2023 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002024 }
2025
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01002026 /*
2027 * Corresponding put_ldev done either below (on various errors), or in
2028 * drbd_peer_request_endio, if we successfully submit the data at the
2029 * end of this function.
2030 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002031
2032 sector = be64_to_cpu(p->sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002033 peer_req = read_in_block(mdev, p->block_id, sector, pi->size);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002034 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002035 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002036 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002037 }
2038
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002039 peer_req->w.cb = e_end_block;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002040
Lars Ellenberg688593c2010-11-17 22:25:03 +01002041 dp_flags = be32_to_cpu(p->dp_flags);
2042 rw |= wire_flags_to_bio(mdev, dp_flags);
2043
2044 if (dp_flags & DP_MAY_SET_IN_SYNC)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002045 peer_req->flags |= EE_MAY_SET_IN_SYNC;
Lars Ellenberg688593c2010-11-17 22:25:03 +01002046
Philipp Reisnerb411b362009-09-25 16:07:19 -07002047 spin_lock(&mdev->epoch_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002048 peer_req->epoch = mdev->current_epoch;
2049 atomic_inc(&peer_req->epoch->epoch_size);
2050 atomic_inc(&peer_req->epoch->active);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002051 spin_unlock(&mdev->epoch_lock);
2052
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002053 if (mdev->tconn->net_conf->two_primaries) {
2054 err = wait_for_and_update_peer_seq(mdev, peer_seq);
2055 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002056 goto out_interrupted;
Philipp Reisner87eeee42011-01-19 14:16:30 +01002057 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002058 err = handle_write_conflicts(mdev, peer_req);
2059 if (err) {
2060 spin_unlock_irq(&mdev->tconn->req_lock);
2061 if (err == -ENOENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002062 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002063 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002064 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002065 goto out_interrupted;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002066 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002067 } else
2068 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002069 list_add(&peer_req->w.list, &mdev->active_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002070 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002071
Philipp Reisner89e58e72011-01-19 13:12:45 +01002072 switch (mdev->tconn->net_conf->wire_protocol) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002073 case DRBD_PROT_C:
2074 inc_unacked(mdev);
2075 /* corresponding dec_unacked() in e_end_block()
2076 * respective _drbd_clear_done_ee */
2077 break;
2078 case DRBD_PROT_B:
2079 /* I really don't like it that the receiver thread
2080 * sends on the msock, but anyways */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002081 drbd_send_ack(mdev, P_RECV_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002082 break;
2083 case DRBD_PROT_A:
2084 /* nothing to do */
2085 break;
2086 }
2087
Lars Ellenberg6719fb02010-10-18 23:04:07 +02002088 if (mdev->state.pdsk < D_INCONSISTENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002089 /* In case we have the only disk of the cluster, */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002090 drbd_set_out_of_sync(mdev, peer_req->i.sector, peer_req->i.size);
2091 peer_req->flags |= EE_CALL_AL_COMPLETE_IO;
2092 peer_req->flags &= ~EE_MAY_SET_IN_SYNC;
Lars Ellenberg181286a2011-03-31 15:18:56 +02002093 drbd_al_begin_io(mdev, &peer_req->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002094 }
2095
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002096 err = drbd_submit_peer_request(mdev, peer_req, rw, DRBD_FAULT_DT_WR);
2097 if (!err)
2098 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002099
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002100 /* don't care for the reason here */
2101 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002102 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002103 list_del(&peer_req->w.list);
2104 drbd_remove_epoch_entry_interval(mdev, peer_req);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002105 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002106 if (peer_req->flags & EE_CALL_AL_COMPLETE_IO)
Lars Ellenberg181286a2011-03-31 15:18:56 +02002107 drbd_al_complete_io(mdev, &peer_req->i);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002108
Philipp Reisnerb411b362009-09-25 16:07:19 -07002109out_interrupted:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002110 drbd_may_finish_epoch(mdev, peer_req->epoch, EV_PUT + EV_CLEANUP);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002111 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002112 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002113 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002114}
2115
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002116/* We may throttle resync, if the lower device seems to be busy,
2117 * and current sync rate is above c_min_rate.
2118 *
2119 * To decide whether or not the lower device is busy, we use a scheme similar
2120 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
2121 * (more than 64 sectors) of activity we cannot account for with our own resync
2122 * activity, it obviously is "busy".
2123 *
2124 * The current sync rate used here uses only the most recent two step marks,
2125 * to have a short time average so we can react faster.
2126 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002127int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002128{
2129 struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
2130 unsigned long db, dt, dbdt;
Philipp Reisnere3555d82010-11-07 15:56:29 +01002131 struct lc_element *tmp;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002132 int curr_events;
2133 int throttle = 0;
2134
2135 /* feature disabled? */
Lars Ellenbergf3990022011-03-23 14:31:09 +01002136 if (mdev->ldev->dc.c_min_rate == 0)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002137 return 0;
2138
Philipp Reisnere3555d82010-11-07 15:56:29 +01002139 spin_lock_irq(&mdev->al_lock);
2140 tmp = lc_find(mdev->resync, BM_SECT_TO_EXT(sector));
2141 if (tmp) {
2142 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
2143 if (test_bit(BME_PRIORITY, &bm_ext->flags)) {
2144 spin_unlock_irq(&mdev->al_lock);
2145 return 0;
2146 }
2147 /* Do not slow down if app IO is already waiting for this extent */
2148 }
2149 spin_unlock_irq(&mdev->al_lock);
2150
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002151 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
2152 (int)part_stat_read(&disk->part0, sectors[1]) -
2153 atomic_read(&mdev->rs_sect_ev);
Philipp Reisnere3555d82010-11-07 15:56:29 +01002154
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002155 if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
2156 unsigned long rs_left;
2157 int i;
2158
2159 mdev->rs_last_events = curr_events;
2160
2161 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
2162 * approx. */
Lars Ellenberg2649f082010-11-05 10:05:47 +01002163 i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
2164
2165 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
2166 rs_left = mdev->ov_left;
2167 else
2168 rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002169
2170 dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
2171 if (!dt)
2172 dt++;
2173 db = mdev->rs_mark_left[i] - rs_left;
2174 dbdt = Bit2KB(db/dt);
2175
Lars Ellenbergf3990022011-03-23 14:31:09 +01002176 if (dbdt > mdev->ldev->dc.c_min_rate)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002177 throttle = 1;
2178 }
2179 return throttle;
2180}
2181
2182
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002183static int receive_DataRequest(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002184{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002185 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002186 sector_t sector;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002187 sector_t capacity;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002188 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002189 struct digest_info *di = NULL;
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002190 int size, verb;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002191 unsigned int fault_type;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002192 struct p_block_req *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002193
2194 mdev = vnr_to_mdev(tconn, pi->vnr);
2195 if (!mdev)
2196 return -EIO;
2197 capacity = drbd_get_capacity(mdev->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002198
2199 sector = be64_to_cpu(p->sector);
2200 size = be32_to_cpu(p->blksize);
2201
Andreas Gruenbacherc670a392011-02-21 12:41:39 +01002202 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002203 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2204 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002205 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002206 }
2207 if (sector + (size>>9) > capacity) {
2208 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2209 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002210 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002211 }
2212
2213 if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002214 verb = 1;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002215 switch (pi->cmd) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002216 case P_DATA_REQUEST:
2217 drbd_send_ack_rp(mdev, P_NEG_DREPLY, p);
2218 break;
2219 case P_RS_DATA_REQUEST:
2220 case P_CSUM_RS_REQUEST:
2221 case P_OV_REQUEST:
2222 drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p);
2223 break;
2224 case P_OV_REPLY:
2225 verb = 0;
2226 dec_rs_pending(mdev);
2227 drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC);
2228 break;
2229 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002230 BUG();
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002231 }
2232 if (verb && __ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002233 dev_err(DEV, "Can not satisfy peer's read request, "
2234 "no local data.\n");
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002235
Lars Ellenberga821cc42010-09-06 12:31:37 +02002236 /* drain possibly payload */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002237 return drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002238 }
2239
2240 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
2241 * "criss-cross" setup, that might cause write-out on some other DRBD,
2242 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02002243 peer_req = drbd_alloc_peer_req(mdev, p->block_id, sector, size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002244 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002245 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002246 return -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002247 }
2248
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002249 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002250 case P_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002251 peer_req->w.cb = w_e_end_data_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002252 fault_type = DRBD_FAULT_DT_RD;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002253 /* application IO, don't drbd_rs_begin_io */
2254 goto submit;
2255
Philipp Reisnerb411b362009-09-25 16:07:19 -07002256 case P_RS_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002257 peer_req->w.cb = w_e_end_rsdata_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002258 fault_type = DRBD_FAULT_RS_RD;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002259 /* used in the sector offset progress display */
2260 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002261 break;
2262
2263 case P_OV_REPLY:
2264 case P_CSUM_RS_REQUEST:
2265 fault_type = DRBD_FAULT_RS_RD;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002266 di = kmalloc(sizeof(*di) + pi->size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002267 if (!di)
2268 goto out_free_e;
2269
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002270 di->digest_size = pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002271 di->digest = (((char *)di)+sizeof(struct digest_info));
2272
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002273 peer_req->digest = di;
2274 peer_req->flags |= EE_HAS_DIGEST;
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +02002275
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002276 if (drbd_recv_all(mdev->tconn, di->digest, pi->size))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002277 goto out_free_e;
2278
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002279 if (pi->cmd == P_CSUM_RS_REQUEST) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002280 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002281 peer_req->w.cb = w_e_end_csum_rs_req;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002282 /* used in the sector offset progress display */
2283 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002284 } else if (pi->cmd == P_OV_REPLY) {
Lars Ellenberg2649f082010-11-05 10:05:47 +01002285 /* track progress, we may need to throttle */
2286 atomic_add(size >> 9, &mdev->rs_sect_in);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002287 peer_req->w.cb = w_e_end_ov_reply;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002288 dec_rs_pending(mdev);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002289 /* drbd_rs_begin_io done when we sent this request,
2290 * but accounting still needs to be done. */
2291 goto submit_for_resync;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002292 }
2293 break;
2294
2295 case P_OV_REQUEST:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002296 if (mdev->ov_start_sector == ~(sector_t)0 &&
Philipp Reisner31890f42011-01-19 14:12:51 +01002297 mdev->tconn->agreed_pro_version >= 90) {
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002298 unsigned long now = jiffies;
2299 int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002300 mdev->ov_start_sector = sector;
2301 mdev->ov_position = sector;
Lars Ellenberg30b743a2010-11-05 09:39:06 +01002302 mdev->ov_left = drbd_bm_bits(mdev) - BM_SECT_TO_BIT(sector);
2303 mdev->rs_total = mdev->ov_left;
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002304 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
2305 mdev->rs_mark_left[i] = mdev->ov_left;
2306 mdev->rs_mark_time[i] = now;
2307 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002308 dev_info(DEV, "Online Verify start sector: %llu\n",
2309 (unsigned long long)sector);
2310 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002311 peer_req->w.cb = w_e_end_ov_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002312 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002313 break;
2314
Philipp Reisnerb411b362009-09-25 16:07:19 -07002315 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002316 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002317 }
2318
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002319 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2320 * wrt the receiver, but it is not as straightforward as it may seem.
2321 * Various places in the resync start and stop logic assume resync
2322 * requests are processed in order, requeuing this on the worker thread
2323 * introduces a bunch of new code for synchronization between threads.
2324 *
2325 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2326 * "forever", throttling after drbd_rs_begin_io will lock that extent
2327 * for application writes for the same time. For now, just throttle
2328 * here, where the rest of the code expects the receiver to sleep for
2329 * a while, anyways.
2330 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002331
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002332 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2333 * this defers syncer requests for some time, before letting at least
2334 * on request through. The resync controller on the receiving side
2335 * will adapt to the incoming rate accordingly.
2336 *
2337 * We cannot throttle here if remote is Primary/SyncTarget:
2338 * we would also throttle its application reads.
2339 * In that case, throttling is done on the SyncTarget only.
2340 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002341 if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev, sector))
2342 schedule_timeout_uninterruptible(HZ/10);
2343 if (drbd_rs_begin_io(mdev, sector))
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002344 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002345
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002346submit_for_resync:
2347 atomic_add(size >> 9, &mdev->rs_sect_ev);
2348
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002349submit:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002350 inc_unacked(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002351 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002352 list_add_tail(&peer_req->w.list, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002353 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002354
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01002355 if (drbd_submit_peer_request(mdev, peer_req, READ, fault_type) == 0)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002356 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002357
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002358 /* don't care for the reason here */
2359 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002360 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002361 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002362 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002363 /* no drbd_rs_complete_io(), we are dropping the connection anyways */
2364
Philipp Reisnerb411b362009-09-25 16:07:19 -07002365out_free_e:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002366 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002367 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002368 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002369}
2370
2371static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
2372{
2373 int self, peer, rv = -100;
2374 unsigned long ch_self, ch_peer;
2375
2376 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2377 peer = mdev->p_uuid[UI_BITMAP] & 1;
2378
2379 ch_peer = mdev->p_uuid[UI_SIZE];
2380 ch_self = mdev->comm_bm_set;
2381
Philipp Reisner89e58e72011-01-19 13:12:45 +01002382 switch (mdev->tconn->net_conf->after_sb_0p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002383 case ASB_CONSENSUS:
2384 case ASB_DISCARD_SECONDARY:
2385 case ASB_CALL_HELPER:
2386 dev_err(DEV, "Configuration error.\n");
2387 break;
2388 case ASB_DISCONNECT:
2389 break;
2390 case ASB_DISCARD_YOUNGER_PRI:
2391 if (self == 0 && peer == 1) {
2392 rv = -1;
2393 break;
2394 }
2395 if (self == 1 && peer == 0) {
2396 rv = 1;
2397 break;
2398 }
2399 /* Else fall through to one of the other strategies... */
2400 case ASB_DISCARD_OLDER_PRI:
2401 if (self == 0 && peer == 1) {
2402 rv = 1;
2403 break;
2404 }
2405 if (self == 1 && peer == 0) {
2406 rv = -1;
2407 break;
2408 }
2409 /* Else fall through to one of the other strategies... */
Lars Ellenbergad19bf62009-10-14 09:36:49 +02002410 dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
Philipp Reisnerb411b362009-09-25 16:07:19 -07002411 "Using discard-least-changes instead\n");
2412 case ASB_DISCARD_ZERO_CHG:
2413 if (ch_peer == 0 && ch_self == 0) {
Philipp Reisner25703f82011-02-07 14:35:25 +01002414 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002415 ? -1 : 1;
2416 break;
2417 } else {
2418 if (ch_peer == 0) { rv = 1; break; }
2419 if (ch_self == 0) { rv = -1; break; }
2420 }
Philipp Reisner89e58e72011-01-19 13:12:45 +01002421 if (mdev->tconn->net_conf->after_sb_0p == ASB_DISCARD_ZERO_CHG)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002422 break;
2423 case ASB_DISCARD_LEAST_CHG:
2424 if (ch_self < ch_peer)
2425 rv = -1;
2426 else if (ch_self > ch_peer)
2427 rv = 1;
2428 else /* ( ch_self == ch_peer ) */
2429 /* Well, then use something else. */
Philipp Reisner25703f82011-02-07 14:35:25 +01002430 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002431 ? -1 : 1;
2432 break;
2433 case ASB_DISCARD_LOCAL:
2434 rv = -1;
2435 break;
2436 case ASB_DISCARD_REMOTE:
2437 rv = 1;
2438 }
2439
2440 return rv;
2441}
2442
2443static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
2444{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002445 int hg, rv = -100;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002446
Philipp Reisner89e58e72011-01-19 13:12:45 +01002447 switch (mdev->tconn->net_conf->after_sb_1p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002448 case ASB_DISCARD_YOUNGER_PRI:
2449 case ASB_DISCARD_OLDER_PRI:
2450 case ASB_DISCARD_LEAST_CHG:
2451 case ASB_DISCARD_LOCAL:
2452 case ASB_DISCARD_REMOTE:
2453 dev_err(DEV, "Configuration error.\n");
2454 break;
2455 case ASB_DISCONNECT:
2456 break;
2457 case ASB_CONSENSUS:
2458 hg = drbd_asb_recover_0p(mdev);
2459 if (hg == -1 && mdev->state.role == R_SECONDARY)
2460 rv = hg;
2461 if (hg == 1 && mdev->state.role == R_PRIMARY)
2462 rv = hg;
2463 break;
2464 case ASB_VIOLENTLY:
2465 rv = drbd_asb_recover_0p(mdev);
2466 break;
2467 case ASB_DISCARD_SECONDARY:
2468 return mdev->state.role == R_PRIMARY ? 1 : -1;
2469 case ASB_CALL_HELPER:
2470 hg = drbd_asb_recover_0p(mdev);
2471 if (hg == -1 && mdev->state.role == R_PRIMARY) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002472 enum drbd_state_rv rv2;
2473
2474 drbd_set_role(mdev, R_SECONDARY, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002475 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2476 * we might be here in C_WF_REPORT_PARAMS which is transient.
2477 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002478 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2479 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002480 drbd_khelper(mdev, "pri-lost-after-sb");
2481 } else {
2482 dev_warn(DEV, "Successfully gave up primary role.\n");
2483 rv = hg;
2484 }
2485 } else
2486 rv = hg;
2487 }
2488
2489 return rv;
2490}
2491
2492static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
2493{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002494 int hg, rv = -100;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002495
Philipp Reisner89e58e72011-01-19 13:12:45 +01002496 switch (mdev->tconn->net_conf->after_sb_2p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002497 case ASB_DISCARD_YOUNGER_PRI:
2498 case ASB_DISCARD_OLDER_PRI:
2499 case ASB_DISCARD_LEAST_CHG:
2500 case ASB_DISCARD_LOCAL:
2501 case ASB_DISCARD_REMOTE:
2502 case ASB_CONSENSUS:
2503 case ASB_DISCARD_SECONDARY:
2504 dev_err(DEV, "Configuration error.\n");
2505 break;
2506 case ASB_VIOLENTLY:
2507 rv = drbd_asb_recover_0p(mdev);
2508 break;
2509 case ASB_DISCONNECT:
2510 break;
2511 case ASB_CALL_HELPER:
2512 hg = drbd_asb_recover_0p(mdev);
2513 if (hg == -1) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002514 enum drbd_state_rv rv2;
2515
Philipp Reisnerb411b362009-09-25 16:07:19 -07002516 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2517 * we might be here in C_WF_REPORT_PARAMS which is transient.
2518 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002519 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2520 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002521 drbd_khelper(mdev, "pri-lost-after-sb");
2522 } else {
2523 dev_warn(DEV, "Successfully gave up primary role.\n");
2524 rv = hg;
2525 }
2526 } else
2527 rv = hg;
2528 }
2529
2530 return rv;
2531}
2532
2533static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
2534 u64 bits, u64 flags)
2535{
2536 if (!uuid) {
2537 dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
2538 return;
2539 }
2540 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
2541 text,
2542 (unsigned long long)uuid[UI_CURRENT],
2543 (unsigned long long)uuid[UI_BITMAP],
2544 (unsigned long long)uuid[UI_HISTORY_START],
2545 (unsigned long long)uuid[UI_HISTORY_END],
2546 (unsigned long long)bits,
2547 (unsigned long long)flags);
2548}
2549
2550/*
2551 100 after split brain try auto recover
2552 2 C_SYNC_SOURCE set BitMap
2553 1 C_SYNC_SOURCE use BitMap
2554 0 no Sync
2555 -1 C_SYNC_TARGET use BitMap
2556 -2 C_SYNC_TARGET set BitMap
2557 -100 after split brain, disconnect
2558-1000 unrelated data
Philipp Reisner4a23f262011-01-11 17:42:17 +01002559-1091 requires proto 91
2560-1096 requires proto 96
Philipp Reisnerb411b362009-09-25 16:07:19 -07002561 */
2562static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
2563{
2564 u64 self, peer;
2565 int i, j;
2566
2567 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2568 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2569
2570 *rule_nr = 10;
2571 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
2572 return 0;
2573
2574 *rule_nr = 20;
2575 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
2576 peer != UUID_JUST_CREATED)
2577 return -2;
2578
2579 *rule_nr = 30;
2580 if (self != UUID_JUST_CREATED &&
2581 (peer == UUID_JUST_CREATED || peer == (u64)0))
2582 return 2;
2583
2584 if (self == peer) {
2585 int rct, dc; /* roles at crash time */
2586
2587 if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
2588
Philipp Reisner31890f42011-01-19 14:12:51 +01002589 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002590 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002591
2592 if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
2593 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
2594 dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
2595 drbd_uuid_set_bm(mdev, 0UL);
2596
2597 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2598 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2599 *rule_nr = 34;
2600 } else {
2601 dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
2602 *rule_nr = 36;
2603 }
2604
2605 return 1;
2606 }
2607
2608 if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
2609
Philipp Reisner31890f42011-01-19 14:12:51 +01002610 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002611 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002612
2613 if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
2614 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
2615 dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
2616
2617 mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
2618 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
2619 mdev->p_uuid[UI_BITMAP] = 0UL;
2620
2621 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2622 *rule_nr = 35;
2623 } else {
2624 dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
2625 *rule_nr = 37;
2626 }
2627
2628 return -1;
2629 }
2630
2631 /* Common power [off|failure] */
2632 rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
2633 (mdev->p_uuid[UI_FLAGS] & 2);
2634 /* lowest bit is set when we were primary,
2635 * next bit (weight 2) is set when peer was primary */
2636 *rule_nr = 40;
2637
2638 switch (rct) {
2639 case 0: /* !self_pri && !peer_pri */ return 0;
2640 case 1: /* self_pri && !peer_pri */ return 1;
2641 case 2: /* !self_pri && peer_pri */ return -1;
2642 case 3: /* self_pri && peer_pri */
Philipp Reisner25703f82011-02-07 14:35:25 +01002643 dc = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002644 return dc ? -1 : 1;
2645 }
2646 }
2647
2648 *rule_nr = 50;
2649 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2650 if (self == peer)
2651 return -1;
2652
2653 *rule_nr = 51;
2654 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2655 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002656 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002657 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) ==
2658 (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) :
2659 peer + UUID_NEW_BM_OFFSET == (mdev->p_uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002660 /* The last P_SYNC_UUID did not get though. Undo the last start of
2661 resync as sync source modifications of the peer's UUIDs. */
2662
Philipp Reisner31890f42011-01-19 14:12:51 +01002663 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002664 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002665
2666 mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
2667 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
Philipp Reisner4a23f262011-01-11 17:42:17 +01002668
2669 dev_info(DEV, "Did not got last syncUUID packet, corrected:\n");
2670 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2671
Philipp Reisnerb411b362009-09-25 16:07:19 -07002672 return -1;
2673 }
2674 }
2675
2676 *rule_nr = 60;
2677 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2678 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2679 peer = mdev->p_uuid[i] & ~((u64)1);
2680 if (self == peer)
2681 return -2;
2682 }
2683
2684 *rule_nr = 70;
2685 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2686 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2687 if (self == peer)
2688 return 1;
2689
2690 *rule_nr = 71;
2691 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2692 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002693 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002694 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) ==
2695 (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) :
2696 self + UUID_NEW_BM_OFFSET == (mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002697 /* The last P_SYNC_UUID did not get though. Undo the last start of
2698 resync as sync source modifications of our UUIDs. */
2699
Philipp Reisner31890f42011-01-19 14:12:51 +01002700 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002701 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002702
2703 _drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
2704 _drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
2705
Philipp Reisner4a23f262011-01-11 17:42:17 +01002706 dev_info(DEV, "Last syncUUID did not get through, corrected:\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002707 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2708 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2709
2710 return 1;
2711 }
2712 }
2713
2714
2715 *rule_nr = 80;
Philipp Reisnerd8c2a362009-11-18 15:52:51 +01002716 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002717 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2718 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2719 if (self == peer)
2720 return 2;
2721 }
2722
2723 *rule_nr = 90;
2724 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2725 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2726 if (self == peer && self != ((u64)0))
2727 return 100;
2728
2729 *rule_nr = 100;
2730 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2731 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2732 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
2733 peer = mdev->p_uuid[j] & ~((u64)1);
2734 if (self == peer)
2735 return -100;
2736 }
2737 }
2738
2739 return -1000;
2740}
2741
2742/* drbd_sync_handshake() returns the new conn state on success, or
2743 CONN_MASK (-1) on failure.
2744 */
2745static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
2746 enum drbd_disk_state peer_disk) __must_hold(local)
2747{
2748 int hg, rule_nr;
2749 enum drbd_conns rv = C_MASK;
2750 enum drbd_disk_state mydisk;
2751
2752 mydisk = mdev->state.disk;
2753 if (mydisk == D_NEGOTIATING)
2754 mydisk = mdev->new_state_tmp.disk;
2755
2756 dev_info(DEV, "drbd_sync_handshake:\n");
2757 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0);
2758 drbd_uuid_dump(mdev, "peer", mdev->p_uuid,
2759 mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2760
2761 hg = drbd_uuid_compare(mdev, &rule_nr);
2762
2763 dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
2764
2765 if (hg == -1000) {
2766 dev_alert(DEV, "Unrelated data, aborting!\n");
2767 return C_MASK;
2768 }
Philipp Reisner4a23f262011-01-11 17:42:17 +01002769 if (hg < -1000) {
2770 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 -07002771 return C_MASK;
2772 }
2773
2774 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
2775 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
2776 int f = (hg == -100) || abs(hg) == 2;
2777 hg = mydisk > D_INCONSISTENT ? 1 : -1;
2778 if (f)
2779 hg = hg*2;
2780 dev_info(DEV, "Becoming sync %s due to disk states.\n",
2781 hg > 0 ? "source" : "target");
2782 }
2783
Adam Gandelman3a11a482010-04-08 16:48:23 -07002784 if (abs(hg) == 100)
2785 drbd_khelper(mdev, "initial-split-brain");
2786
Philipp Reisner89e58e72011-01-19 13:12:45 +01002787 if (hg == 100 || (hg == -100 && mdev->tconn->net_conf->always_asbp)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002788 int pcount = (mdev->state.role == R_PRIMARY)
2789 + (peer_role == R_PRIMARY);
2790 int forced = (hg == -100);
2791
2792 switch (pcount) {
2793 case 0:
2794 hg = drbd_asb_recover_0p(mdev);
2795 break;
2796 case 1:
2797 hg = drbd_asb_recover_1p(mdev);
2798 break;
2799 case 2:
2800 hg = drbd_asb_recover_2p(mdev);
2801 break;
2802 }
2803 if (abs(hg) < 100) {
2804 dev_warn(DEV, "Split-Brain detected, %d primaries, "
2805 "automatically solved. Sync from %s node\n",
2806 pcount, (hg < 0) ? "peer" : "this");
2807 if (forced) {
2808 dev_warn(DEV, "Doing a full sync, since"
2809 " UUIDs where ambiguous.\n");
2810 hg = hg*2;
2811 }
2812 }
2813 }
2814
2815 if (hg == -100) {
Philipp Reisner89e58e72011-01-19 13:12:45 +01002816 if (mdev->tconn->net_conf->want_lose && !(mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002817 hg = -1;
Philipp Reisner89e58e72011-01-19 13:12:45 +01002818 if (!mdev->tconn->net_conf->want_lose && (mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002819 hg = 1;
2820
2821 if (abs(hg) < 100)
2822 dev_warn(DEV, "Split-Brain detected, manually solved. "
2823 "Sync from %s node\n",
2824 (hg < 0) ? "peer" : "this");
2825 }
2826
2827 if (hg == -100) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01002828 /* FIXME this log message is not correct if we end up here
2829 * after an attempted attach on a diskless node.
2830 * We just refuse to attach -- well, we drop the "connection"
2831 * to that disk, in a way... */
Adam Gandelman3a11a482010-04-08 16:48:23 -07002832 dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002833 drbd_khelper(mdev, "split-brain");
2834 return C_MASK;
2835 }
2836
2837 if (hg > 0 && mydisk <= D_INCONSISTENT) {
2838 dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
2839 return C_MASK;
2840 }
2841
2842 if (hg < 0 && /* by intention we do not use mydisk here. */
2843 mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
Philipp Reisner89e58e72011-01-19 13:12:45 +01002844 switch (mdev->tconn->net_conf->rr_conflict) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002845 case ASB_CALL_HELPER:
2846 drbd_khelper(mdev, "pri-lost");
2847 /* fall through */
2848 case ASB_DISCONNECT:
2849 dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
2850 return C_MASK;
2851 case ASB_VIOLENTLY:
2852 dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
2853 "assumption\n");
2854 }
2855 }
2856
Philipp Reisner8169e412011-03-15 18:40:27 +01002857 if (mdev->tconn->net_conf->dry_run || test_bit(CONN_DRY_RUN, &mdev->tconn->flags)) {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002858 if (hg == 0)
2859 dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
2860 else
2861 dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
2862 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
2863 abs(hg) >= 2 ? "full" : "bit-map based");
2864 return C_MASK;
2865 }
2866
Philipp Reisnerb411b362009-09-25 16:07:19 -07002867 if (abs(hg) >= 2) {
2868 dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002869 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake",
2870 BM_LOCKED_SET_ALLOWED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002871 return C_MASK;
2872 }
2873
2874 if (hg > 0) { /* become sync source. */
2875 rv = C_WF_BITMAP_S;
2876 } else if (hg < 0) { /* become sync target */
2877 rv = C_WF_BITMAP_T;
2878 } else {
2879 rv = C_CONNECTED;
2880 if (drbd_bm_total_weight(mdev)) {
2881 dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
2882 drbd_bm_total_weight(mdev));
2883 }
2884 }
2885
2886 return rv;
2887}
2888
2889/* returns 1 if invalid */
2890static int cmp_after_sb(enum drbd_after_sb_p peer, enum drbd_after_sb_p self)
2891{
2892 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
2893 if ((peer == ASB_DISCARD_REMOTE && self == ASB_DISCARD_LOCAL) ||
2894 (self == ASB_DISCARD_REMOTE && peer == ASB_DISCARD_LOCAL))
2895 return 0;
2896
2897 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
2898 if (peer == ASB_DISCARD_REMOTE || peer == ASB_DISCARD_LOCAL ||
2899 self == ASB_DISCARD_REMOTE || self == ASB_DISCARD_LOCAL)
2900 return 1;
2901
2902 /* everything else is valid if they are equal on both sides. */
2903 if (peer == self)
2904 return 0;
2905
2906 /* everything es is invalid. */
2907 return 1;
2908}
2909
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002910static int receive_protocol(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002911{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002912 struct p_protocol *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002913 int p_proto, p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002914 int p_want_lose, p_two_primaries, cf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002915 char p_integrity_alg[SHARED_SECRET_MAX] = "";
2916
Philipp Reisnerb411b362009-09-25 16:07:19 -07002917 p_proto = be32_to_cpu(p->protocol);
2918 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
2919 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
2920 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002921 p_two_primaries = be32_to_cpu(p->two_primaries);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002922 cf = be32_to_cpu(p->conn_flags);
2923 p_want_lose = cf & CF_WANT_LOSE;
2924
Philipp Reisner72046242011-03-15 18:51:47 +01002925 clear_bit(CONN_DRY_RUN, &tconn->flags);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002926
2927 if (cf & CF_DRY_RUN)
Philipp Reisner72046242011-03-15 18:51:47 +01002928 set_bit(CONN_DRY_RUN, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002929
Philipp Reisner72046242011-03-15 18:51:47 +01002930 if (p_proto != tconn->net_conf->wire_protocol) {
2931 conn_err(tconn, "incompatible communication protocols\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002932 goto disconnect;
2933 }
2934
Philipp Reisner72046242011-03-15 18:51:47 +01002935 if (cmp_after_sb(p_after_sb_0p, tconn->net_conf->after_sb_0p)) {
2936 conn_err(tconn, "incompatible after-sb-0pri settings\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002937 goto disconnect;
2938 }
2939
Philipp Reisner72046242011-03-15 18:51:47 +01002940 if (cmp_after_sb(p_after_sb_1p, tconn->net_conf->after_sb_1p)) {
2941 conn_err(tconn, "incompatible after-sb-1pri settings\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002942 goto disconnect;
2943 }
2944
Philipp Reisner72046242011-03-15 18:51:47 +01002945 if (cmp_after_sb(p_after_sb_2p, tconn->net_conf->after_sb_2p)) {
2946 conn_err(tconn, "incompatible after-sb-2pri settings\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002947 goto disconnect;
2948 }
2949
Philipp Reisner72046242011-03-15 18:51:47 +01002950 if (p_want_lose && tconn->net_conf->want_lose) {
2951 conn_err(tconn, "both sides have the 'want_lose' flag set\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002952 goto disconnect;
2953 }
2954
Philipp Reisner72046242011-03-15 18:51:47 +01002955 if (p_two_primaries != tconn->net_conf->two_primaries) {
2956 conn_err(tconn, "incompatible setting of the two-primaries options\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002957 goto disconnect;
2958 }
2959
Philipp Reisner72046242011-03-15 18:51:47 +01002960 if (tconn->agreed_pro_version >= 87) {
2961 unsigned char *my_alg = tconn->net_conf->integrity_alg;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002962 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002963
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002964 err = drbd_recv_all(tconn, p_integrity_alg, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002965 if (err)
2966 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002967
2968 p_integrity_alg[SHARED_SECRET_MAX-1] = 0;
2969 if (strcmp(p_integrity_alg, my_alg)) {
Philipp Reisner72046242011-03-15 18:51:47 +01002970 conn_err(tconn, "incompatible setting of the data-integrity-alg\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002971 goto disconnect;
2972 }
Philipp Reisner72046242011-03-15 18:51:47 +01002973 conn_info(tconn, "data-integrity-alg: %s\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -07002974 my_alg[0] ? my_alg : (unsigned char *)"<not-used>");
2975 }
2976
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002977 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002978
2979disconnect:
Philipp Reisner72046242011-03-15 18:51:47 +01002980 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002981 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002982}
2983
2984/* helper function
2985 * input: alg name, feature name
2986 * return: NULL (alg name was "")
2987 * ERR_PTR(error) if something goes wrong
2988 * or the crypto hash ptr, if it worked out ok. */
2989struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
2990 const char *alg, const char *name)
2991{
2992 struct crypto_hash *tfm;
2993
2994 if (!alg[0])
2995 return NULL;
2996
2997 tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
2998 if (IS_ERR(tfm)) {
2999 dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
3000 alg, name, PTR_ERR(tfm));
3001 return tfm;
3002 }
3003 if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) {
3004 crypto_free_hash(tfm);
3005 dev_err(DEV, "\"%s\" is not a digest (%s)\n", alg, name);
3006 return ERR_PTR(-EINVAL);
3007 }
3008 return tfm;
3009}
3010
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003011static int ignore_remaining_packet(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003012{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003013 void *buffer = tconn->data.rbuf;
3014 int size = pi->size;
3015
3016 while (size) {
3017 int s = min_t(int, size, DRBD_SOCKET_BUFFER_SIZE);
3018 s = drbd_recv(tconn, buffer, s);
3019 if (s <= 0) {
3020 if (s < 0)
3021 return s;
3022 break;
3023 }
3024 size -= s;
3025 }
3026 if (size)
3027 return -EIO;
3028 return 0;
3029}
3030
3031/*
3032 * config_unknown_volume - device configuration command for unknown volume
3033 *
3034 * When a device is added to an existing connection, the node on which the
3035 * device is added first will send configuration commands to its peer but the
3036 * peer will not know about the device yet. It will warn and ignore these
3037 * commands. Once the device is added on the second node, the second node will
3038 * send the same device configuration commands, but in the other direction.
3039 *
3040 * (We can also end up here if drbd is misconfigured.)
3041 */
3042static int config_unknown_volume(struct drbd_tconn *tconn, struct packet_info *pi)
3043{
3044 conn_warn(tconn, "Volume %u unknown; ignoring %s packet\n",
3045 pi->vnr, cmdname(pi->cmd));
3046 return ignore_remaining_packet(tconn, pi);
3047}
3048
3049static int receive_SyncParam(struct drbd_tconn *tconn, struct packet_info *pi)
3050{
3051 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003052 struct p_rs_param_95 *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003053 unsigned int header_size, data_size, exp_max_sz;
3054 struct crypto_hash *verify_tfm = NULL;
3055 struct crypto_hash *csums_tfm = NULL;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003056 const int apv = tconn->agreed_pro_version;
Philipp Reisner778f2712010-07-06 11:14:00 +02003057 int *rs_plan_s = NULL;
3058 int fifo_size = 0;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003059 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003060
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003061 mdev = vnr_to_mdev(tconn, pi->vnr);
3062 if (!mdev)
3063 return config_unknown_volume(tconn, pi);
3064
Philipp Reisnerb411b362009-09-25 16:07:19 -07003065 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
3066 : apv == 88 ? sizeof(struct p_rs_param)
3067 + SHARED_SECRET_MAX
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003068 : apv <= 94 ? sizeof(struct p_rs_param_89)
3069 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003070
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003071 if (pi->size > exp_max_sz) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003072 dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003073 pi->size, exp_max_sz);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003074 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003075 }
3076
3077 if (apv <= 88) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003078 header_size = sizeof(struct p_rs_param);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003079 data_size = pi->size - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003080 } else if (apv <= 94) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003081 header_size = sizeof(struct p_rs_param_89);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003082 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003083 D_ASSERT(data_size == 0);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003084 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003085 header_size = sizeof(struct p_rs_param_95);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003086 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003087 D_ASSERT(data_size == 0);
3088 }
3089
3090 /* initialize verify_alg and csums_alg */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003091 p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003092 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
3093
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003094 err = drbd_recv_all(mdev->tconn, p, header_size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003095 if (err)
3096 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003097
Lars Ellenbergf3990022011-03-23 14:31:09 +01003098 if (get_ldev(mdev)) {
3099 mdev->ldev->dc.resync_rate = be32_to_cpu(p->rate);
3100 put_ldev(mdev);
3101 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003102
3103 if (apv >= 88) {
3104 if (apv == 88) {
3105 if (data_size > SHARED_SECRET_MAX) {
3106 dev_err(DEV, "verify-alg too long, "
3107 "peer wants %u, accepting only %u byte\n",
3108 data_size, SHARED_SECRET_MAX);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003109 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003110 }
3111
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003112 err = drbd_recv_all(mdev->tconn, p->verify_alg, data_size);
3113 if (err)
3114 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003115
3116 /* we expect NUL terminated string */
3117 /* but just in case someone tries to be evil */
3118 D_ASSERT(p->verify_alg[data_size-1] == 0);
3119 p->verify_alg[data_size-1] = 0;
3120
3121 } else /* apv >= 89 */ {
3122 /* we still expect NUL terminated strings */
3123 /* but just in case someone tries to be evil */
3124 D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
3125 D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
3126 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
3127 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
3128 }
3129
Lars Ellenbergf3990022011-03-23 14:31:09 +01003130 if (strcmp(mdev->tconn->net_conf->verify_alg, p->verify_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003131 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3132 dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
Lars Ellenbergf3990022011-03-23 14:31:09 +01003133 mdev->tconn->net_conf->verify_alg, p->verify_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003134 goto disconnect;
3135 }
3136 verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
3137 p->verify_alg, "verify-alg");
3138 if (IS_ERR(verify_tfm)) {
3139 verify_tfm = NULL;
3140 goto disconnect;
3141 }
3142 }
3143
Lars Ellenbergf3990022011-03-23 14:31:09 +01003144 if (apv >= 89 && strcmp(mdev->tconn->net_conf->csums_alg, p->csums_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003145 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3146 dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
Lars Ellenbergf3990022011-03-23 14:31:09 +01003147 mdev->tconn->net_conf->csums_alg, p->csums_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003148 goto disconnect;
3149 }
3150 csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
3151 p->csums_alg, "csums-alg");
3152 if (IS_ERR(csums_tfm)) {
3153 csums_tfm = NULL;
3154 goto disconnect;
3155 }
3156 }
3157
Lars Ellenbergf3990022011-03-23 14:31:09 +01003158 if (apv > 94 && get_ldev(mdev)) {
3159 mdev->ldev->dc.resync_rate = be32_to_cpu(p->rate);
3160 mdev->ldev->dc.c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
3161 mdev->ldev->dc.c_delay_target = be32_to_cpu(p->c_delay_target);
3162 mdev->ldev->dc.c_fill_target = be32_to_cpu(p->c_fill_target);
3163 mdev->ldev->dc.c_max_rate = be32_to_cpu(p->c_max_rate);
Philipp Reisner778f2712010-07-06 11:14:00 +02003164
Lars Ellenbergf3990022011-03-23 14:31:09 +01003165 fifo_size = (mdev->ldev->dc.c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Philipp Reisner778f2712010-07-06 11:14:00 +02003166 if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) {
3167 rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL);
3168 if (!rs_plan_s) {
3169 dev_err(DEV, "kmalloc of fifo_buffer failed");
Lars Ellenbergf3990022011-03-23 14:31:09 +01003170 put_ldev(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02003171 goto disconnect;
3172 }
3173 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01003174 put_ldev(mdev);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003175 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003176
3177 spin_lock(&mdev->peer_seq_lock);
3178 /* lock against drbd_nl_syncer_conf() */
3179 if (verify_tfm) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01003180 strcpy(mdev->tconn->net_conf->verify_alg, p->verify_alg);
3181 mdev->tconn->net_conf->verify_alg_len = strlen(p->verify_alg) + 1;
3182 crypto_free_hash(mdev->tconn->verify_tfm);
3183 mdev->tconn->verify_tfm = verify_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003184 dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
3185 }
3186 if (csums_tfm) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01003187 strcpy(mdev->tconn->net_conf->csums_alg, p->csums_alg);
3188 mdev->tconn->net_conf->csums_alg_len = strlen(p->csums_alg) + 1;
3189 crypto_free_hash(mdev->tconn->csums_tfm);
3190 mdev->tconn->csums_tfm = csums_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003191 dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
3192 }
Philipp Reisner778f2712010-07-06 11:14:00 +02003193 if (fifo_size != mdev->rs_plan_s.size) {
3194 kfree(mdev->rs_plan_s.values);
3195 mdev->rs_plan_s.values = rs_plan_s;
3196 mdev->rs_plan_s.size = fifo_size;
3197 mdev->rs_planed = 0;
3198 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003199 spin_unlock(&mdev->peer_seq_lock);
3200 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003201 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003202
Philipp Reisnerb411b362009-09-25 16:07:19 -07003203disconnect:
3204 /* just for completeness: actually not needed,
3205 * as this is not reached if csums_tfm was ok. */
3206 crypto_free_hash(csums_tfm);
3207 /* but free the verify_tfm again, if csums_tfm did not work out */
3208 crypto_free_hash(verify_tfm);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003209 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003210 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003211}
3212
Philipp Reisnerb411b362009-09-25 16:07:19 -07003213/* warn if the arguments differ by more than 12.5% */
3214static void warn_if_differ_considerably(struct drbd_conf *mdev,
3215 const char *s, sector_t a, sector_t b)
3216{
3217 sector_t d;
3218 if (a == 0 || b == 0)
3219 return;
3220 d = (a > b) ? (a - b) : (b - a);
3221 if (d > (a>>3) || d > (b>>3))
3222 dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
3223 (unsigned long long)a, (unsigned long long)b);
3224}
3225
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003226static int receive_sizes(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003227{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003228 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003229 struct p_sizes *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003230 enum determine_dev_size dd = unchanged;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003231 sector_t p_size, p_usize, my_usize;
3232 int ldsc = 0; /* local disk size changed */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003233 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003234
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003235 mdev = vnr_to_mdev(tconn, pi->vnr);
3236 if (!mdev)
3237 return config_unknown_volume(tconn, pi);
3238
Philipp Reisnerb411b362009-09-25 16:07:19 -07003239 p_size = be64_to_cpu(p->d_size);
3240 p_usize = be64_to_cpu(p->u_size);
3241
Philipp Reisnerb411b362009-09-25 16:07:19 -07003242 /* just store the peer's disk size for now.
3243 * we still need to figure out whether we accept that. */
3244 mdev->p_size = p_size;
3245
Philipp Reisnerb411b362009-09-25 16:07:19 -07003246 if (get_ldev(mdev)) {
3247 warn_if_differ_considerably(mdev, "lower level device sizes",
3248 p_size, drbd_get_max_capacity(mdev->ldev));
3249 warn_if_differ_considerably(mdev, "user requested size",
3250 p_usize, mdev->ldev->dc.disk_size);
3251
3252 /* if this is the first connect, or an otherwise expected
3253 * param exchange, choose the minimum */
3254 if (mdev->state.conn == C_WF_REPORT_PARAMS)
3255 p_usize = min_not_zero((sector_t)mdev->ldev->dc.disk_size,
3256 p_usize);
3257
3258 my_usize = mdev->ldev->dc.disk_size;
3259
3260 if (mdev->ldev->dc.disk_size != p_usize) {
3261 mdev->ldev->dc.disk_size = p_usize;
3262 dev_info(DEV, "Peer sets u_size to %lu sectors\n",
3263 (unsigned long)mdev->ldev->dc.disk_size);
3264 }
3265
3266 /* Never shrink a device with usable data during connect.
3267 But allow online shrinking if we are connected. */
Philipp Reisnera393db62009-12-22 13:35:52 +01003268 if (drbd_new_dev_size(mdev, mdev->ldev, 0) <
Philipp Reisnerb411b362009-09-25 16:07:19 -07003269 drbd_get_capacity(mdev->this_bdev) &&
3270 mdev->state.disk >= D_OUTDATED &&
3271 mdev->state.conn < C_CONNECTED) {
3272 dev_err(DEV, "The peer's disk size is too small!\n");
Philipp Reisner38fa9982011-03-15 18:24:49 +01003273 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003274 mdev->ldev->dc.disk_size = my_usize;
3275 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003276 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003277 }
3278 put_ldev(mdev);
3279 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003280
Philipp Reisnere89b5912010-03-24 17:11:33 +01003281 ddsf = be16_to_cpu(p->dds_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003282 if (get_ldev(mdev)) {
Bart Van Assche24c48302011-05-21 18:32:29 +02003283 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003284 put_ldev(mdev);
3285 if (dd == dev_size_error)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003286 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003287 drbd_md_sync(mdev);
3288 } else {
3289 /* I am diskless, need to accept the peer's size. */
3290 drbd_set_my_capacity(mdev, p_size);
3291 }
3292
Philipp Reisner99432fc2011-05-20 16:39:13 +02003293 mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size);
3294 drbd_reconsider_max_bio_size(mdev);
3295
Philipp Reisnerb411b362009-09-25 16:07:19 -07003296 if (get_ldev(mdev)) {
3297 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
3298 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
3299 ldsc = 1;
3300 }
3301
Philipp Reisnerb411b362009-09-25 16:07:19 -07003302 put_ldev(mdev);
3303 }
3304
3305 if (mdev->state.conn > C_WF_REPORT_PARAMS) {
3306 if (be64_to_cpu(p->c_size) !=
3307 drbd_get_capacity(mdev->this_bdev) || ldsc) {
3308 /* we have different sizes, probably peer
3309 * needs to know my new size... */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003310 drbd_send_sizes(mdev, 0, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003311 }
3312 if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
3313 (dd == grew && mdev->state.conn == C_CONNECTED)) {
3314 if (mdev->state.pdsk >= D_INCONSISTENT &&
Philipp Reisnere89b5912010-03-24 17:11:33 +01003315 mdev->state.disk >= D_INCONSISTENT) {
3316 if (ddsf & DDSF_NO_RESYNC)
3317 dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
3318 else
3319 resync_after_online_grow(mdev);
3320 } else
Philipp Reisnerb411b362009-09-25 16:07:19 -07003321 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
3322 }
3323 }
3324
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003325 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003326}
3327
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003328static int receive_uuids(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003329{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003330 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003331 struct p_uuids *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003332 u64 *p_uuid;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003333 int i, updated_uuids = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003334
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003335 mdev = vnr_to_mdev(tconn, pi->vnr);
3336 if (!mdev)
3337 return config_unknown_volume(tconn, pi);
3338
Philipp Reisnerb411b362009-09-25 16:07:19 -07003339 p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
3340
3341 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
3342 p_uuid[i] = be64_to_cpu(p->uuid[i]);
3343
3344 kfree(mdev->p_uuid);
3345 mdev->p_uuid = p_uuid;
3346
3347 if (mdev->state.conn < C_CONNECTED &&
3348 mdev->state.disk < D_INCONSISTENT &&
3349 mdev->state.role == R_PRIMARY &&
3350 (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
3351 dev_err(DEV, "Can only connect to data with current UUID=%016llX\n",
3352 (unsigned long long)mdev->ed_uuid);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003353 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003354 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003355 }
3356
3357 if (get_ldev(mdev)) {
3358 int skip_initial_sync =
3359 mdev->state.conn == C_CONNECTED &&
Philipp Reisner31890f42011-01-19 14:12:51 +01003360 mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003361 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
3362 (p_uuid[UI_FLAGS] & 8);
3363 if (skip_initial_sync) {
3364 dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n");
3365 drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003366 "clear_n_write from receive_uuids",
3367 BM_LOCKED_TEST_ALLOWED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003368 _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]);
3369 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3370 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3371 CS_VERBOSE, NULL);
3372 drbd_md_sync(mdev);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003373 updated_uuids = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003374 }
3375 put_ldev(mdev);
Philipp Reisner18a50fa2010-06-21 14:14:15 +02003376 } else if (mdev->state.disk < D_INCONSISTENT &&
3377 mdev->state.role == R_PRIMARY) {
3378 /* I am a diskless primary, the peer just created a new current UUID
3379 for me. */
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003380 updated_uuids = drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003381 }
3382
3383 /* Before we test for the disk state, we should wait until an eventually
3384 ongoing cluster wide state change is finished. That is important if
3385 we are primary and are detaching from our disk. We need to see the
3386 new disk state... */
Philipp Reisner8410da82011-02-11 20:11:10 +01003387 mutex_lock(mdev->state_mutex);
3388 mutex_unlock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003389 if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT)
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003390 updated_uuids |= drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
3391
3392 if (updated_uuids)
3393 drbd_print_uuids(mdev, "receiver updated UUIDs to");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003394
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003395 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003396}
3397
3398/**
3399 * convert_state() - Converts the peer's view of the cluster state to our point of view
3400 * @ps: The state as seen by the peer.
3401 */
3402static union drbd_state convert_state(union drbd_state ps)
3403{
3404 union drbd_state ms;
3405
3406 static enum drbd_conns c_tab[] = {
3407 [C_CONNECTED] = C_CONNECTED,
3408
3409 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
3410 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
3411 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
3412 [C_VERIFY_S] = C_VERIFY_T,
3413 [C_MASK] = C_MASK,
3414 };
3415
3416 ms.i = ps.i;
3417
3418 ms.conn = c_tab[ps.conn];
3419 ms.peer = ps.role;
3420 ms.role = ps.peer;
3421 ms.pdsk = ps.disk;
3422 ms.disk = ps.pdsk;
3423 ms.peer_isp = (ps.aftr_isp | ps.user_isp);
3424
3425 return ms;
3426}
3427
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003428static int receive_req_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003429{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003430 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003431 struct p_req_state *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003432 union drbd_state mask, val;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01003433 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003434
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003435 mdev = vnr_to_mdev(tconn, pi->vnr);
3436 if (!mdev)
3437 return -EIO;
3438
Philipp Reisnerb411b362009-09-25 16:07:19 -07003439 mask.i = be32_to_cpu(p->mask);
3440 val.i = be32_to_cpu(p->val);
3441
Philipp Reisner25703f82011-02-07 14:35:25 +01003442 if (test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags) &&
Philipp Reisner8410da82011-02-11 20:11:10 +01003443 mutex_is_locked(mdev->state_mutex)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003444 drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003445 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003446 }
3447
3448 mask = convert_state(mask);
3449 val = convert_state(val);
3450
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003451 rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
3452 drbd_send_sr_reply(mdev, rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003453
Philipp Reisnerb411b362009-09-25 16:07:19 -07003454 drbd_md_sync(mdev);
3455
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003456 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003457}
3458
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003459static int receive_req_conn_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003460{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003461 struct p_req_state *p = pi->data;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003462 union drbd_state mask, val;
3463 enum drbd_state_rv rv;
3464
3465 mask.i = be32_to_cpu(p->mask);
3466 val.i = be32_to_cpu(p->val);
3467
3468 if (test_bit(DISCARD_CONCURRENT, &tconn->flags) &&
3469 mutex_is_locked(&tconn->cstate_mutex)) {
3470 conn_send_sr_reply(tconn, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003471 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003472 }
3473
3474 mask = convert_state(mask);
3475 val = convert_state(val);
3476
Philipp Reisner778bcf22011-03-28 12:55:03 +02003477 rv = conn_request_state(tconn, mask, val, CS_VERBOSE | CS_LOCAL_ONLY | CS_IGN_OUTD_FAIL);
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003478 conn_send_sr_reply(tconn, rv);
3479
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003480 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003481}
3482
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003483static int receive_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003484{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003485 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003486 struct p_state *p = pi->data;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003487 union drbd_state os, ns, peer_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003488 enum drbd_disk_state real_peer_disk;
Philipp Reisner65d922c2010-06-16 16:18:09 +02003489 enum chg_state_flags cs_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003490 int rv;
3491
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003492 mdev = vnr_to_mdev(tconn, pi->vnr);
3493 if (!mdev)
3494 return config_unknown_volume(tconn, pi);
3495
Philipp Reisnerb411b362009-09-25 16:07:19 -07003496 peer_state.i = be32_to_cpu(p->state);
3497
3498 real_peer_disk = peer_state.disk;
3499 if (peer_state.disk == D_NEGOTIATING) {
3500 real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
3501 dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
3502 }
3503
Philipp Reisner87eeee42011-01-19 14:16:30 +01003504 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003505 retry:
Philipp Reisner78bae592011-03-28 15:40:12 +02003506 os = ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003507 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003508
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003509 /* peer says his disk is uptodate, while we think it is inconsistent,
3510 * and this happens while we think we have a sync going on. */
3511 if (os.pdsk == D_INCONSISTENT && real_peer_disk == D_UP_TO_DATE &&
3512 os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) {
3513 /* If we are (becoming) SyncSource, but peer is still in sync
3514 * preparation, ignore its uptodate-ness to avoid flapping, it
3515 * will change to inconsistent once the peer reaches active
3516 * syncing states.
3517 * It may have changed syncer-paused flags, however, so we
3518 * cannot ignore this completely. */
3519 if (peer_state.conn > C_CONNECTED &&
3520 peer_state.conn < C_SYNC_SOURCE)
3521 real_peer_disk = D_INCONSISTENT;
3522
3523 /* if peer_state changes to connected at the same time,
3524 * it explicitly notifies us that it finished resync.
3525 * Maybe we should finish it up, too? */
3526 else if (os.conn >= C_SYNC_SOURCE &&
3527 peer_state.conn == C_CONNECTED) {
3528 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed)
3529 drbd_resync_finished(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003530 return 0;
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003531 }
3532 }
3533
3534 /* peer says his disk is inconsistent, while we think it is uptodate,
3535 * and this happens while the peer still thinks we have a sync going on,
3536 * but we think we are already done with the sync.
3537 * We ignore this to avoid flapping pdsk.
3538 * This should not happen, if the peer is a recent version of drbd. */
3539 if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT &&
3540 os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE)
3541 real_peer_disk = D_UP_TO_DATE;
3542
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003543 if (ns.conn == C_WF_REPORT_PARAMS)
3544 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003545
Philipp Reisner67531712010-10-27 12:21:30 +02003546 if (peer_state.conn == C_AHEAD)
3547 ns.conn = C_BEHIND;
3548
Philipp Reisnerb411b362009-09-25 16:07:19 -07003549 if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING &&
3550 get_ldev_if_state(mdev, D_NEGOTIATING)) {
3551 int cr; /* consider resync */
3552
3553 /* if we established a new connection */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003554 cr = (os.conn < C_CONNECTED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003555 /* if we had an established connection
3556 * and one of the nodes newly attaches a disk */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003557 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003558 (peer_state.disk == D_NEGOTIATING ||
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003559 os.disk == D_NEGOTIATING));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003560 /* if we have both been inconsistent, and the peer has been
3561 * forced to be UpToDate with --overwrite-data */
3562 cr |= test_bit(CONSIDER_RESYNC, &mdev->flags);
3563 /* if we had been plain connected, and the admin requested to
3564 * start a sync by "invalidate" or "invalidate-remote" */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003565 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003566 (peer_state.conn >= C_STARTING_SYNC_S &&
3567 peer_state.conn <= C_WF_BITMAP_T));
3568
3569 if (cr)
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003570 ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003571
3572 put_ldev(mdev);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003573 if (ns.conn == C_MASK) {
3574 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003575 if (mdev->state.disk == D_NEGOTIATING) {
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003576 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003577 } else if (peer_state.disk == D_NEGOTIATING) {
3578 dev_err(DEV, "Disk attach process on the peer node was aborted.\n");
3579 peer_state.disk = D_DISKLESS;
Lars Ellenberg580b9762010-02-26 23:15:23 +01003580 real_peer_disk = D_DISKLESS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003581 } else {
Philipp Reisner8169e412011-03-15 18:40:27 +01003582 if (test_and_clear_bit(CONN_DRY_RUN, &mdev->tconn->flags))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003583 return -EIO;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003584 D_ASSERT(os.conn == C_WF_REPORT_PARAMS);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003585 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003586 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003587 }
3588 }
3589 }
3590
Philipp Reisner87eeee42011-01-19 14:16:30 +01003591 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner78bae592011-03-28 15:40:12 +02003592 if (os.i != drbd_read_state(mdev).i)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003593 goto retry;
3594 clear_bit(CONSIDER_RESYNC, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003595 ns.peer = peer_state.role;
3596 ns.pdsk = real_peer_disk;
3597 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003598 if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003599 ns.disk = mdev->new_state_tmp.disk;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003600 cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD);
Philipp Reisner2aebfab2011-03-28 16:48:11 +02003601 if (ns.pdsk == D_CONSISTENT && drbd_suspended(mdev) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED &&
Philipp Reisner481c6f52010-06-22 14:03:27 +02003602 test_bit(NEW_CUR_UUID, &mdev->flags)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01003603 /* Do not allow tl_restart(RESEND) for a rebooted peer. We can only allow this
Philipp Reisner481c6f52010-06-22 14:03:27 +02003604 for temporal network outages! */
Philipp Reisner87eeee42011-01-19 14:16:30 +01003605 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003606 dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01003607 tl_clear(mdev->tconn);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003608 drbd_uuid_new_current(mdev);
3609 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003610 conn_request_state(mdev->tconn, NS2(conn, C_PROTOCOL_ERROR, susp, 0), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003611 return -EIO;
Philipp Reisner481c6f52010-06-22 14:03:27 +02003612 }
Philipp Reisner65d922c2010-06-16 16:18:09 +02003613 rv = _drbd_set_state(mdev, ns, cs_flags, NULL);
Philipp Reisner78bae592011-03-28 15:40:12 +02003614 ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003615 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003616
3617 if (rv < SS_SUCCESS) {
Philipp Reisner38fa9982011-03-15 18:24:49 +01003618 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003619 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003620 }
3621
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003622 if (os.conn > C_WF_REPORT_PARAMS) {
3623 if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003624 peer_state.disk != D_NEGOTIATING ) {
3625 /* we want resync, peer has not yet decided to sync... */
3626 /* Nowadays only used when forcing a node into primary role and
3627 setting its disk to UpToDate with that */
3628 drbd_send_uuids(mdev);
3629 drbd_send_state(mdev);
3630 }
3631 }
3632
Philipp Reisner89e58e72011-01-19 13:12:45 +01003633 mdev->tconn->net_conf->want_lose = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003634
3635 drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
3636
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003637 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003638}
3639
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003640static int receive_sync_uuid(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003641{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003642 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003643 struct p_rs_uuid *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003644
3645 mdev = vnr_to_mdev(tconn, pi->vnr);
3646 if (!mdev)
3647 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003648
3649 wait_event(mdev->misc_wait,
3650 mdev->state.conn == C_WF_SYNC_UUID ||
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02003651 mdev->state.conn == C_BEHIND ||
Philipp Reisnerb411b362009-09-25 16:07:19 -07003652 mdev->state.conn < C_CONNECTED ||
3653 mdev->state.disk < D_NEGOTIATING);
3654
3655 /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */
3656
Philipp Reisnerb411b362009-09-25 16:07:19 -07003657 /* Here the _drbd_uuid_ functions are right, current should
3658 _not_ be rotated into the history */
3659 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
3660 _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid));
3661 _drbd_uuid_set(mdev, UI_BITMAP, 0UL);
3662
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003663 drbd_print_uuids(mdev, "updated sync uuid");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003664 drbd_start_resync(mdev, C_SYNC_TARGET);
3665
3666 put_ldev(mdev);
3667 } else
3668 dev_err(DEV, "Ignoring SyncUUID packet!\n");
3669
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003670 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003671}
3672
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003673/**
3674 * receive_bitmap_plain
3675 *
3676 * Return 0 when done, 1 when another iteration is needed, and a negative error
3677 * code upon failure.
3678 */
3679static int
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003680receive_bitmap_plain(struct drbd_conf *mdev, unsigned int size,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003681 unsigned long *p, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003682{
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003683 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE -
3684 drbd_header_size(mdev->tconn);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003685 unsigned int num_words = min_t(size_t, data_size / sizeof(*p),
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003686 c->bm_words - c->word_offset);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003687 unsigned int want = num_words * sizeof(*p);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003688 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003689
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003690 if (want != size) {
3691 dev_err(DEV, "%s:want (%u) != size (%u)\n", __func__, want, size);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003692 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003693 }
3694 if (want == 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003695 return 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003696 err = drbd_recv_all(mdev->tconn, p, want);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003697 if (err)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003698 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003699
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003700 drbd_bm_merge_lel(mdev, c->word_offset, num_words, p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003701
3702 c->word_offset += num_words;
3703 c->bit_offset = c->word_offset * BITS_PER_LONG;
3704 if (c->bit_offset > c->bm_bits)
3705 c->bit_offset = c->bm_bits;
3706
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003707 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003708}
3709
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003710static enum drbd_bitmap_code dcbp_get_code(struct p_compressed_bm *p)
3711{
3712 return (enum drbd_bitmap_code)(p->encoding & 0x0f);
3713}
3714
3715static int dcbp_get_start(struct p_compressed_bm *p)
3716{
3717 return (p->encoding & 0x80) != 0;
3718}
3719
3720static int dcbp_get_pad_bits(struct p_compressed_bm *p)
3721{
3722 return (p->encoding >> 4) & 0x7;
3723}
3724
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003725/**
3726 * recv_bm_rle_bits
3727 *
3728 * Return 0 when done, 1 when another iteration is needed, and a negative error
3729 * code upon failure.
3730 */
3731static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07003732recv_bm_rle_bits(struct drbd_conf *mdev,
3733 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003734 struct bm_xfer_ctx *c,
3735 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003736{
3737 struct bitstream bs;
3738 u64 look_ahead;
3739 u64 rl;
3740 u64 tmp;
3741 unsigned long s = c->bit_offset;
3742 unsigned long e;
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003743 int toggle = dcbp_get_start(p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003744 int have;
3745 int bits;
3746
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003747 bitstream_init(&bs, p->code, len, dcbp_get_pad_bits(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003748
3749 bits = bitstream_get_bits(&bs, &look_ahead, 64);
3750 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003751 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003752
3753 for (have = bits; have > 0; s += rl, toggle = !toggle) {
3754 bits = vli_decode_bits(&rl, look_ahead);
3755 if (bits <= 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003756 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003757
3758 if (toggle) {
3759 e = s + rl -1;
3760 if (e >= c->bm_bits) {
3761 dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003762 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003763 }
3764 _drbd_bm_set_bits(mdev, s, e);
3765 }
3766
3767 if (have < bits) {
3768 dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
3769 have, bits, look_ahead,
3770 (unsigned int)(bs.cur.b - p->code),
3771 (unsigned int)bs.buf_len);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003772 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003773 }
3774 look_ahead >>= bits;
3775 have -= bits;
3776
3777 bits = bitstream_get_bits(&bs, &tmp, 64 - have);
3778 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003779 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003780 look_ahead |= tmp << have;
3781 have += bits;
3782 }
3783
3784 c->bit_offset = s;
3785 bm_xfer_ctx_bit_to_word_offset(c);
3786
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003787 return (s != c->bm_bits);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003788}
3789
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003790/**
3791 * decode_bitmap_c
3792 *
3793 * Return 0 when done, 1 when another iteration is needed, and a negative error
3794 * code upon failure.
3795 */
3796static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07003797decode_bitmap_c(struct drbd_conf *mdev,
3798 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003799 struct bm_xfer_ctx *c,
3800 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003801{
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003802 if (dcbp_get_code(p) == RLE_VLI_Bits)
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003803 return recv_bm_rle_bits(mdev, p, c, len - sizeof(*p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003804
3805 /* other variants had been implemented for evaluation,
3806 * but have been dropped as this one turned out to be "best"
3807 * during all our tests. */
3808
3809 dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003810 conn_request_state(mdev->tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003811 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003812}
3813
3814void INFO_bm_xfer_stats(struct drbd_conf *mdev,
3815 const char *direction, struct bm_xfer_ctx *c)
3816{
3817 /* what would it take to transfer it "plaintext" */
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003818 unsigned int header_size = drbd_header_size(mdev->tconn);
3819 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE - header_size;
3820 unsigned int plain =
3821 header_size * (DIV_ROUND_UP(c->bm_words, data_size) + 1) +
3822 c->bm_words * sizeof(unsigned long);
3823 unsigned int total = c->bytes[0] + c->bytes[1];
3824 unsigned int r;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003825
3826 /* total can not be zero. but just in case: */
3827 if (total == 0)
3828 return;
3829
3830 /* don't report if not compressed */
3831 if (total >= plain)
3832 return;
3833
3834 /* total < plain. check for overflow, still */
3835 r = (total > UINT_MAX/1000) ? (total / (plain/1000))
3836 : (1000 * total / plain);
3837
3838 if (r > 1000)
3839 r = 1000;
3840
3841 r = 1000 - r;
3842 dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
3843 "total %u; compression: %u.%u%%\n",
3844 direction,
3845 c->bytes[1], c->packets[1],
3846 c->bytes[0], c->packets[0],
3847 total, r/10, r % 10);
3848}
3849
3850/* Since we are processing the bitfield from lower addresses to higher,
3851 it does not matter if the process it in 32 bit chunks or 64 bit
3852 chunks as long as it is little endian. (Understand it as byte stream,
3853 beginning with the lowest byte...) If we would use big endian
3854 we would need to process it from the highest address to the lowest,
3855 in order to be agnostic to the 32 vs 64 bits issue.
3856
3857 returns 0 on failure, 1 if we successfully received it. */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003858static int receive_bitmap(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003859{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003860 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003861 struct bm_xfer_ctx c;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003862 int err;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003863
3864 mdev = vnr_to_mdev(tconn, pi->vnr);
3865 if (!mdev)
3866 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003867
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003868 drbd_bm_lock(mdev, "receive bitmap", BM_LOCKED_SET_ALLOWED);
3869 /* you are supposed to send additional out-of-sync information
3870 * if you actually set bits during this phase */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003871
Philipp Reisnerb411b362009-09-25 16:07:19 -07003872 c = (struct bm_xfer_ctx) {
3873 .bm_bits = drbd_bm_bits(mdev),
3874 .bm_words = drbd_bm_words(mdev),
3875 };
3876
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003877 for(;;) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003878 if (pi->cmd == P_BITMAP)
3879 err = receive_bitmap_plain(mdev, pi->size, pi->data, &c);
3880 else if (pi->cmd == P_COMPRESSED_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003881 /* MAYBE: sanity check that we speak proto >= 90,
3882 * and the feature is enabled! */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003883 struct p_compressed_bm *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003884
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003885 if (pi->size > DRBD_SOCKET_BUFFER_SIZE - drbd_header_size(tconn)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003886 dev_err(DEV, "ReportCBitmap packet too large\n");
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003887 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003888 goto out;
3889 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003890 if (pi->size <= sizeof(*p)) {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003891 dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003892 err = -EIO;
Andreas Gruenbacher78fcbda2010-12-10 22:18:27 +01003893 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003894 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003895 err = drbd_recv_all(mdev->tconn, p, pi->size);
3896 if (err)
3897 goto out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003898 err = decode_bitmap_c(mdev, p, &c, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003899 } else {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003900 dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", pi->cmd);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003901 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003902 goto out;
3903 }
3904
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003905 c.packets[pi->cmd == P_BITMAP]++;
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003906 c.bytes[pi->cmd == P_BITMAP] += drbd_header_size(tconn) + pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003907
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003908 if (err <= 0) {
3909 if (err < 0)
3910 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003911 break;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003912 }
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003913 err = drbd_recv_header(mdev->tconn, pi);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003914 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003915 goto out;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003916 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003917
3918 INFO_bm_xfer_stats(mdev, "receive", &c);
3919
3920 if (mdev->state.conn == C_WF_BITMAP_T) {
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01003921 enum drbd_state_rv rv;
3922
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003923 err = drbd_send_bitmap(mdev);
3924 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003925 goto out;
3926 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01003927 rv = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
3928 D_ASSERT(rv == SS_SUCCESS);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003929 } else if (mdev->state.conn != C_WF_BITMAP_S) {
3930 /* admin may have requested C_DISCONNECTING,
3931 * other threads may have noticed network errors */
3932 dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n",
3933 drbd_conn_str(mdev->state.conn));
3934 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003935 err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003936
Philipp Reisnerb411b362009-09-25 16:07:19 -07003937 out:
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003938 drbd_bm_unlock(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003939 if (!err && mdev->state.conn == C_WF_BITMAP_S)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003940 drbd_start_resync(mdev, C_SYNC_SOURCE);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003941 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003942}
3943
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003944static int receive_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003945{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003946 conn_warn(tconn, "skipping unknown optional packet type %d, l: %d!\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003947 pi->cmd, pi->size);
Philipp Reisner2de876e2011-03-15 14:38:01 +01003948
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003949 return ignore_remaining_packet(tconn, pi);
Philipp Reisner2de876e2011-03-15 14:38:01 +01003950}
3951
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003952static int receive_UnplugRemote(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003953{
Philipp Reisnerb411b362009-09-25 16:07:19 -07003954 /* Make sure we've acked all the TCP data associated
3955 * with the data requests being unplugged */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003956 drbd_tcp_quickack(tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003957
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003958 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003959}
3960
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003961static int receive_out_of_sync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner73a01a12010-10-27 14:33:00 +02003962{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003963 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003964 struct p_block_desc *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003965
3966 mdev = vnr_to_mdev(tconn, pi->vnr);
3967 if (!mdev)
3968 return -EIO;
Philipp Reisner73a01a12010-10-27 14:33:00 +02003969
Lars Ellenbergf735e3632010-12-17 21:06:18 +01003970 switch (mdev->state.conn) {
3971 case C_WF_SYNC_UUID:
3972 case C_WF_BITMAP_T:
3973 case C_BEHIND:
3974 break;
3975 default:
3976 dev_err(DEV, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n",
3977 drbd_conn_str(mdev->state.conn));
3978 }
3979
Philipp Reisner73a01a12010-10-27 14:33:00 +02003980 drbd_set_out_of_sync(mdev, be64_to_cpu(p->sector), be32_to_cpu(p->blksize));
3981
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003982 return 0;
Philipp Reisner73a01a12010-10-27 14:33:00 +02003983}
3984
Philipp Reisner02918be2010-08-20 14:35:10 +02003985struct data_cmd {
3986 int expect_payload;
3987 size_t pkt_size;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003988 int (*fn)(struct drbd_tconn *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003989};
3990
Philipp Reisner02918be2010-08-20 14:35:10 +02003991static struct data_cmd drbd_cmd_handler[] = {
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003992 [P_DATA] = { 1, sizeof(struct p_data), receive_Data },
3993 [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply },
3994 [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } ,
3995 [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } ,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003996 [P_BITMAP] = { 1, 0, receive_bitmap } ,
3997 [P_COMPRESSED_BITMAP] = { 1, 0, receive_bitmap } ,
3998 [P_UNPLUG_REMOTE] = { 0, 0, receive_UnplugRemote },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003999 [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4000 [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004001 [P_SYNC_PARAM] = { 1, 0, receive_SyncParam },
4002 [P_SYNC_PARAM89] = { 1, 0, receive_SyncParam },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004003 [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol },
4004 [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids },
4005 [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes },
4006 [P_STATE] = { 0, sizeof(struct p_state), receive_state },
4007 [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state },
4008 [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid },
4009 [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4010 [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4011 [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4012 [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip },
4013 [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync },
4014 [P_CONN_ST_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_conn_state },
Philipp Reisner02918be2010-08-20 14:35:10 +02004015};
4016
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004017static void drbdd(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004018{
Philipp Reisner77351055b2011-02-07 17:24:26 +01004019 struct packet_info pi;
Philipp Reisner02918be2010-08-20 14:35:10 +02004020 size_t shs; /* sub header size */
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004021 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004022
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004023 while (get_t_state(&tconn->receiver) == RUNNING) {
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004024 struct data_cmd *cmd;
4025
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004026 drbd_thread_current_set_cpu(&tconn->receiver);
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004027 if (drbd_recv_header(tconn, &pi))
Philipp Reisner02918be2010-08-20 14:35:10 +02004028 goto err_out;
4029
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004030 cmd = &drbd_cmd_handler[pi.cmd];
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004031 if (unlikely(pi.cmd >= ARRAY_SIZE(drbd_cmd_handler) || !cmd->fn)) {
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004032 conn_err(tconn, "unknown packet type %d, l: %d!\n", pi.cmd, pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004033 goto err_out;
Lars Ellenberg0b33a912009-11-16 15:58:04 +01004034 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004035
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004036 shs = cmd->pkt_size;
4037 if (pi.size > shs && !cmd->expect_payload) {
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004038 conn_err(tconn, "No payload expected %s l:%d\n", cmdname(pi.cmd), pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004039 goto err_out;
4040 }
4041
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004042 if (shs) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004043 err = drbd_recv_all_warn(tconn, pi.data, shs);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004044 if (err)
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004045 goto err_out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004046 pi.size -= shs;
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004047 }
4048
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004049 err = cmd->fn(tconn, &pi);
4050 if (err) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004051 conn_err(tconn, "error receiving %s, e: %d l: %d!\n",
4052 cmdname(pi.cmd), err, pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004053 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004054 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004055 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004056 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004057
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004058 err_out:
4059 conn_request_state(tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004060}
4061
Philipp Reisner0e29d162011-02-18 14:23:11 +01004062void conn_flush_workqueue(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004063{
4064 struct drbd_wq_barrier barr;
4065
4066 barr.w.cb = w_prev_work_done;
Philipp Reisner0e29d162011-02-18 14:23:11 +01004067 barr.w.tconn = tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004068 init_completion(&barr.done);
Philipp Reisner0e29d162011-02-18 14:23:11 +01004069 drbd_queue_work(&tconn->data.work, &barr.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004070 wait_for_completion(&barr.done);
4071}
4072
Philipp Reisner360cc742011-02-08 14:29:53 +01004073static void drbd_disconnect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004074{
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004075 enum drbd_conns oc;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004076 int rv = SS_UNKNOWN_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004077
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004078 if (tconn->cstate == C_STANDALONE)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004079 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004080
4081 /* asender does not clean up anything. it must not interfere, either */
Philipp Reisner360cc742011-02-08 14:29:53 +01004082 drbd_thread_stop(&tconn->asender);
4083 drbd_free_sock(tconn);
4084
4085 idr_for_each(&tconn->volumes, drbd_disconnected, tconn);
Philipp Reisner360cc742011-02-08 14:29:53 +01004086 conn_info(tconn, "Connection closed\n");
4087
Philipp Reisnercb703452011-03-24 11:03:07 +01004088 if (conn_highest_role(tconn) == R_PRIMARY && conn_highest_pdsk(tconn) >= D_UNKNOWN)
4089 conn_try_outdate_peer_async(tconn);
4090
Philipp Reisner360cc742011-02-08 14:29:53 +01004091 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004092 oc = tconn->cstate;
4093 if (oc >= C_UNCONNECTED)
4094 rv = _conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
4095
Philipp Reisner360cc742011-02-08 14:29:53 +01004096 spin_unlock_irq(&tconn->req_lock);
4097
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004098 if (oc == C_DISCONNECTING) {
Philipp Reisner360cc742011-02-08 14:29:53 +01004099 wait_event(tconn->net_cnt_wait, atomic_read(&tconn->net_cnt) == 0);
4100
4101 crypto_free_hash(tconn->cram_hmac_tfm);
4102 tconn->cram_hmac_tfm = NULL;
4103
4104 kfree(tconn->net_conf);
4105 tconn->net_conf = NULL;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004106 conn_request_state(tconn, NS(conn, C_STANDALONE), CS_VERBOSE);
Philipp Reisner360cc742011-02-08 14:29:53 +01004107 }
4108}
4109
4110static int drbd_disconnected(int vnr, void *p, void *data)
4111{
4112 struct drbd_conf *mdev = (struct drbd_conf *)p;
4113 enum drbd_fencing_p fp;
4114 unsigned int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004115
Philipp Reisner85719572010-07-21 10:20:17 +02004116 /* wait for current activity to cease. */
Philipp Reisner87eeee42011-01-19 14:16:30 +01004117 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004118 _drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
4119 _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee);
4120 _drbd_wait_ee_list_empty(mdev, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004121 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004122
4123 /* We do not have data structures that would allow us to
4124 * get the rs_pending_cnt down to 0 again.
4125 * * On C_SYNC_TARGET we do not have any data structures describing
4126 * the pending RSDataRequest's we have sent.
4127 * * On C_SYNC_SOURCE there is no data structure that tracks
4128 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
4129 * And no, it is not the sum of the reference counts in the
4130 * resync_LRU. The resync_LRU tracks the whole operation including
4131 * the disk-IO, while the rs_pending_cnt only tracks the blocks
4132 * on the fly. */
4133 drbd_rs_cancel_all(mdev);
4134 mdev->rs_total = 0;
4135 mdev->rs_failed = 0;
4136 atomic_set(&mdev->rs_pending_cnt, 0);
4137 wake_up(&mdev->misc_wait);
4138
Philipp Reisner7fde2be2011-03-01 11:08:28 +01004139 del_timer(&mdev->request_timer);
4140
Philipp Reisnerb411b362009-09-25 16:07:19 -07004141 del_timer_sync(&mdev->resync_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004142 resync_timer_fn((unsigned long)mdev);
4143
Philipp Reisnerb411b362009-09-25 16:07:19 -07004144 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
4145 * w_make_resync_request etc. which may still be on the worker queue
4146 * to be "canceled" */
Philipp Reisnera21e9292011-02-08 15:08:49 +01004147 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004148
Andreas Gruenbachera990be42011-04-06 17:56:48 +02004149 drbd_finish_peer_reqs(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004150
4151 kfree(mdev->p_uuid);
4152 mdev->p_uuid = NULL;
4153
Philipp Reisner2aebfab2011-03-28 16:48:11 +02004154 if (!drbd_suspended(mdev))
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01004155 tl_clear(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004156
Philipp Reisnerb411b362009-09-25 16:07:19 -07004157 drbd_md_sync(mdev);
4158
4159 fp = FP_DONT_CARE;
4160 if (get_ldev(mdev)) {
4161 fp = mdev->ldev->dc.fencing;
4162 put_ldev(mdev);
4163 }
4164
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004165 /* serialize with bitmap writeout triggered by the state change,
4166 * if any. */
4167 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
4168
Philipp Reisnerb411b362009-09-25 16:07:19 -07004169 /* tcp_close and release of sendpage pages can be deferred. I don't
4170 * want to use SO_LINGER, because apparently it can be deferred for
4171 * more than 20 seconds (longest time I checked).
4172 *
4173 * Actually we don't care for exactly when the network stack does its
4174 * put_page(), but release our reference on these pages right here.
4175 */
Andreas Gruenbacher7721f562011-04-06 17:14:02 +02004176 i = drbd_free_peer_reqs(mdev, &mdev->net_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004177 if (i)
4178 dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
Lars Ellenberg435f0742010-09-06 12:30:25 +02004179 i = atomic_read(&mdev->pp_in_use_by_net);
4180 if (i)
4181 dev_info(DEV, "pp_in_use_by_net = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004182 i = atomic_read(&mdev->pp_in_use);
4183 if (i)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02004184 dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004185
4186 D_ASSERT(list_empty(&mdev->read_ee));
4187 D_ASSERT(list_empty(&mdev->active_ee));
4188 D_ASSERT(list_empty(&mdev->sync_ee));
4189 D_ASSERT(list_empty(&mdev->done_ee));
4190
4191 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
4192 atomic_set(&mdev->current_epoch->epoch_size, 0);
4193 D_ASSERT(list_empty(&mdev->current_epoch->list));
Philipp Reisner360cc742011-02-08 14:29:53 +01004194
4195 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004196}
4197
4198/*
4199 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
4200 * we can agree on is stored in agreed_pro_version.
4201 *
4202 * feature flags and the reserved array should be enough room for future
4203 * enhancements of the handshake protocol, and possible plugins...
4204 *
4205 * for now, they are expected to be zero, but ignored.
4206 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004207static int drbd_send_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004208{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004209 struct drbd_socket *sock;
4210 struct p_connection_features *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004211
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004212 sock = &tconn->data;
4213 p = conn_prepare_command(tconn, sock);
4214 if (!p)
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004215 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004216 memset(p, 0, sizeof(*p));
4217 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
4218 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004219 return conn_send_command(tconn, sock, P_CONNECTION_FEATURES, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004220}
4221
4222/*
4223 * return values:
4224 * 1 yes, we have a valid connection
4225 * 0 oops, did not work out, please try again
4226 * -1 peer talks different language,
4227 * no point in trying again, please go standalone.
4228 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004229static int drbd_do_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004230{
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004231 /* ASSERT current == tconn->receiver ... */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004232 struct p_connection_features *p;
4233 const int expect = sizeof(struct p_connection_features);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004234 struct packet_info pi;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004235 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004236
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004237 err = drbd_send_features(tconn);
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004238 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004239 return 0;
4240
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004241 err = drbd_recv_header(tconn, &pi);
4242 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004243 return 0;
4244
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004245 if (pi.cmd != P_CONNECTION_FEATURES) {
4246 conn_err(tconn, "expected ConnectionFeatures packet, received: %s (0x%04x)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004247 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004248 return -1;
4249 }
4250
Philipp Reisner77351055b2011-02-07 17:24:26 +01004251 if (pi.size != expect) {
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004252 conn_err(tconn, "expected ConnectionFeatures length: %u, received: %u\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004253 expect, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004254 return -1;
4255 }
4256
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004257 p = pi.data;
4258 err = drbd_recv_all_warn(tconn, p, expect);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004259 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004260 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004261
Philipp Reisnerb411b362009-09-25 16:07:19 -07004262 p->protocol_min = be32_to_cpu(p->protocol_min);
4263 p->protocol_max = be32_to_cpu(p->protocol_max);
4264 if (p->protocol_max == 0)
4265 p->protocol_max = p->protocol_min;
4266
4267 if (PRO_VERSION_MAX < p->protocol_min ||
4268 PRO_VERSION_MIN > p->protocol_max)
4269 goto incompat;
4270
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004271 tconn->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004272
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004273 conn_info(tconn, "Handshake successful: "
4274 "Agreed network protocol version %d\n", tconn->agreed_pro_version);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004275
4276 return 1;
4277
4278 incompat:
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004279 conn_err(tconn, "incompatible DRBD dialects: "
Philipp Reisnerb411b362009-09-25 16:07:19 -07004280 "I support %d-%d, peer supports %d-%d\n",
4281 PRO_VERSION_MIN, PRO_VERSION_MAX,
4282 p->protocol_min, p->protocol_max);
4283 return -1;
4284}
4285
4286#if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
Philipp Reisner13e60372011-02-08 09:54:40 +01004287static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004288{
4289 dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
4290 dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004291 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004292}
4293#else
4294#define CHALLENGE_LEN 64
Johannes Thomab10d96c2010-01-07 16:02:50 +01004295
4296/* Return value:
4297 1 - auth succeeded,
4298 0 - failed, try again (network error),
4299 -1 - auth failed, don't try again.
4300*/
4301
Philipp Reisner13e60372011-02-08 09:54:40 +01004302static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004303{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004304 struct drbd_socket *sock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004305 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
4306 struct scatterlist sg;
4307 char *response = NULL;
4308 char *right_response = NULL;
4309 char *peers_ch = NULL;
Philipp Reisner13e60372011-02-08 09:54:40 +01004310 unsigned int key_len = strlen(tconn->net_conf->shared_secret);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004311 unsigned int resp_size;
4312 struct hash_desc desc;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004313 struct packet_info pi;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004314 int err, rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004315
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004316 /* FIXME: Put the challenge/response into the preallocated socket buffer. */
4317
Philipp Reisner13e60372011-02-08 09:54:40 +01004318 desc.tfm = tconn->cram_hmac_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004319 desc.flags = 0;
4320
Philipp Reisner13e60372011-02-08 09:54:40 +01004321 rv = crypto_hash_setkey(tconn->cram_hmac_tfm,
4322 (u8 *)tconn->net_conf->shared_secret, key_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004323 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004324 conn_err(tconn, "crypto_hash_setkey() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004325 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004326 goto fail;
4327 }
4328
4329 get_random_bytes(my_challenge, CHALLENGE_LEN);
4330
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004331 sock = &tconn->data;
4332 if (!conn_prepare_command(tconn, sock)) {
4333 rv = 0;
4334 goto fail;
4335 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004336 rv = !conn_send_command(tconn, sock, P_AUTH_CHALLENGE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004337 my_challenge, CHALLENGE_LEN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004338 if (!rv)
4339 goto fail;
4340
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004341 err = drbd_recv_header(tconn, &pi);
4342 if (err) {
4343 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004344 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004345 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004346
Philipp Reisner77351055b2011-02-07 17:24:26 +01004347 if (pi.cmd != P_AUTH_CHALLENGE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004348 conn_err(tconn, "expected AuthChallenge packet, received: %s (0x%04x)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004349 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004350 rv = 0;
4351 goto fail;
4352 }
4353
Philipp Reisner77351055b2011-02-07 17:24:26 +01004354 if (pi.size > CHALLENGE_LEN * 2) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004355 conn_err(tconn, "expected AuthChallenge payload too big.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004356 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004357 goto fail;
4358 }
4359
Philipp Reisner77351055b2011-02-07 17:24:26 +01004360 peers_ch = kmalloc(pi.size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004361 if (peers_ch == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004362 conn_err(tconn, "kmalloc of peers_ch failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004363 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004364 goto fail;
4365 }
4366
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004367 err = drbd_recv_all_warn(tconn, peers_ch, pi.size);
4368 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004369 rv = 0;
4370 goto fail;
4371 }
4372
Philipp Reisner13e60372011-02-08 09:54:40 +01004373 resp_size = crypto_hash_digestsize(tconn->cram_hmac_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004374 response = kmalloc(resp_size, GFP_NOIO);
4375 if (response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004376 conn_err(tconn, "kmalloc of response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004377 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004378 goto fail;
4379 }
4380
4381 sg_init_table(&sg, 1);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004382 sg_set_buf(&sg, peers_ch, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004383
4384 rv = crypto_hash_digest(&desc, &sg, sg.length, response);
4385 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004386 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004387 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004388 goto fail;
4389 }
4390
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004391 if (!conn_prepare_command(tconn, sock)) {
4392 rv = 0;
4393 goto fail;
4394 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004395 rv = !conn_send_command(tconn, sock, P_AUTH_RESPONSE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004396 response, resp_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004397 if (!rv)
4398 goto fail;
4399
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004400 err = drbd_recv_header(tconn, &pi);
4401 if (err) {
4402 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004403 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004404 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004405
Philipp Reisner77351055b2011-02-07 17:24:26 +01004406 if (pi.cmd != P_AUTH_RESPONSE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004407 conn_err(tconn, "expected AuthResponse packet, received: %s (0x%04x)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004408 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004409 rv = 0;
4410 goto fail;
4411 }
4412
Philipp Reisner77351055b2011-02-07 17:24:26 +01004413 if (pi.size != resp_size) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004414 conn_err(tconn, "expected AuthResponse payload of wrong size\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004415 rv = 0;
4416 goto fail;
4417 }
4418
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004419 err = drbd_recv_all_warn(tconn, response , resp_size);
4420 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004421 rv = 0;
4422 goto fail;
4423 }
4424
4425 right_response = kmalloc(resp_size, GFP_NOIO);
Julia Lawall2d1ee872009-12-27 22:27:11 +01004426 if (right_response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004427 conn_err(tconn, "kmalloc of right_response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004428 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004429 goto fail;
4430 }
4431
4432 sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
4433
4434 rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
4435 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004436 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004437 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004438 goto fail;
4439 }
4440
4441 rv = !memcmp(response, right_response, resp_size);
4442
4443 if (rv)
Philipp Reisner13e60372011-02-08 09:54:40 +01004444 conn_info(tconn, "Peer authenticated using %d bytes of '%s' HMAC\n",
4445 resp_size, tconn->net_conf->cram_hmac_alg);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004446 else
4447 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004448
4449 fail:
4450 kfree(peers_ch);
4451 kfree(response);
4452 kfree(right_response);
4453
4454 return rv;
4455}
4456#endif
4457
4458int drbdd_init(struct drbd_thread *thi)
4459{
Philipp Reisner392c8802011-02-09 10:33:31 +01004460 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004461 int h;
4462
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004463 conn_info(tconn, "receiver (re)started\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004464
4465 do {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004466 h = drbd_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004467 if (h == 0) {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004468 drbd_disconnect(tconn);
Philipp Reisner20ee6392011-01-18 15:28:59 +01004469 schedule_timeout_interruptible(HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004470 }
4471 if (h == -1) {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004472 conn_warn(tconn, "Discarding network configuration.\n");
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004473 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004474 }
4475 } while (h == 0);
4476
4477 if (h > 0) {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004478 if (get_net_conf(tconn)) {
4479 drbdd(tconn);
4480 put_net_conf(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004481 }
4482 }
4483
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004484 drbd_disconnect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004485
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004486 conn_info(tconn, "receiver terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004487 return 0;
4488}
4489
4490/* ********* acknowledge sender ******** */
4491
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004492static int got_conn_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004493{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004494 struct p_req_state_reply *p = pi->data;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004495 int retcode = be32_to_cpu(p->retcode);
4496
4497 if (retcode >= SS_SUCCESS) {
4498 set_bit(CONN_WD_ST_CHG_OKAY, &tconn->flags);
4499 } else {
4500 set_bit(CONN_WD_ST_CHG_FAIL, &tconn->flags);
4501 conn_err(tconn, "Requested state change failed by peer: %s (%d)\n",
4502 drbd_set_st_err_str(retcode), retcode);
4503 }
4504 wake_up(&tconn->ping_wait);
4505
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004506 return 0;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004507}
4508
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004509static int got_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004510{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004511 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004512 struct p_req_state_reply *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004513 int retcode = be32_to_cpu(p->retcode);
4514
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004515 mdev = vnr_to_mdev(tconn, pi->vnr);
4516 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004517 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004518
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004519 if (retcode >= SS_SUCCESS) {
4520 set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
4521 } else {
4522 set_bit(CL_ST_CHG_FAIL, &mdev->flags);
4523 dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
4524 drbd_set_st_err_str(retcode), retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004525 }
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004526 wake_up(&mdev->state_wait);
4527
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004528 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004529}
4530
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004531static int got_Ping(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004532{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004533 return drbd_send_ping_ack(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004534
4535}
4536
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004537static int got_PingAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004538{
4539 /* restore idle timeout */
Philipp Reisner2a67d8b2011-02-09 14:10:32 +01004540 tconn->meta.socket->sk->sk_rcvtimeo = tconn->net_conf->ping_int*HZ;
4541 if (!test_and_set_bit(GOT_PING_ACK, &tconn->flags))
4542 wake_up(&tconn->ping_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004543
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004544 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004545}
4546
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004547static int got_IsInSync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004548{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004549 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004550 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004551 sector_t sector = be64_to_cpu(p->sector);
4552 int blksize = be32_to_cpu(p->blksize);
4553
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004554 mdev = vnr_to_mdev(tconn, pi->vnr);
4555 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004556 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004557
Philipp Reisner31890f42011-01-19 14:12:51 +01004558 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004559
4560 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4561
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004562 if (get_ldev(mdev)) {
4563 drbd_rs_complete_io(mdev, sector);
4564 drbd_set_in_sync(mdev, sector, blksize);
4565 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
4566 mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
4567 put_ldev(mdev);
4568 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004569 dec_rs_pending(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02004570 atomic_add(blksize >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004571
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004572 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004573}
4574
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004575static int
4576validate_req_change_req_state(struct drbd_conf *mdev, u64 id, sector_t sector,
4577 struct rb_root *root, const char *func,
4578 enum drbd_req_event what, bool missing_ok)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004579{
4580 struct drbd_request *req;
4581 struct bio_and_error m;
4582
Philipp Reisner87eeee42011-01-19 14:16:30 +01004583 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004584 req = find_request(mdev, root, id, sector, missing_ok, func);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004585 if (unlikely(!req)) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01004586 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004587 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004588 }
4589 __req_mod(req, what, &m);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004590 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004591
4592 if (m.bio)
4593 complete_master_bio(mdev, &m);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004594 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004595}
4596
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004597static int got_BlockAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004598{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004599 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004600 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004601 sector_t sector = be64_to_cpu(p->sector);
4602 int blksize = be32_to_cpu(p->blksize);
4603 enum drbd_req_event what;
4604
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004605 mdev = vnr_to_mdev(tconn, pi->vnr);
4606 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004607 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004608
Philipp Reisnerb411b362009-09-25 16:07:19 -07004609 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4610
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004611 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004612 drbd_set_in_sync(mdev, sector, blksize);
4613 dec_rs_pending(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004614 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004615 }
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004616 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004617 case P_RS_WRITE_ACK:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004618 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004619 what = WRITE_ACKED_BY_PEER_AND_SIS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004620 break;
4621 case P_WRITE_ACK:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004622 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004623 what = WRITE_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004624 break;
4625 case P_RECV_ACK:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004626 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_B);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004627 what = RECV_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004628 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004629 case P_DISCARD_WRITE:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004630 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004631 what = DISCARD_WRITE;
4632 break;
4633 case P_RETRY_WRITE:
4634 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
4635 what = POSTPONE_WRITE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004636 break;
4637 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004638 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07004639 }
4640
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004641 return validate_req_change_req_state(mdev, p->block_id, sector,
4642 &mdev->write_requests, __func__,
4643 what, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004644}
4645
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004646static int got_NegAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004647{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004648 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004649 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004650 sector_t sector = be64_to_cpu(p->sector);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004651 int size = be32_to_cpu(p->blksize);
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004652 bool missing_ok = tconn->net_conf->wire_protocol == DRBD_PROT_A ||
4653 tconn->net_conf->wire_protocol == DRBD_PROT_B;
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004654 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004655
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004656 mdev = vnr_to_mdev(tconn, pi->vnr);
4657 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004658 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004659
Philipp Reisnerb411b362009-09-25 16:07:19 -07004660 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4661
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004662 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004663 dec_rs_pending(mdev);
4664 drbd_rs_failed_io(mdev, sector, size);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004665 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004666 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01004667
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004668 err = validate_req_change_req_state(mdev, p->block_id, sector,
4669 &mdev->write_requests, __func__,
4670 NEG_ACKED, missing_ok);
4671 if (err) {
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004672 /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs.
4673 The master bio might already be completed, therefore the
4674 request is no longer in the collision hash. */
4675 /* In Protocol B we might already have got a P_RECV_ACK
4676 but then get a P_NEG_ACK afterwards. */
4677 if (!missing_ok)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004678 return err;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004679 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004680 }
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004681 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004682}
4683
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004684static int got_NegDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004685{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004686 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004687 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004688 sector_t sector = be64_to_cpu(p->sector);
4689
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004690 mdev = vnr_to_mdev(tconn, pi->vnr);
4691 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004692 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004693
Philipp Reisnerb411b362009-09-25 16:07:19 -07004694 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004695
Philipp Reisnerb411b362009-09-25 16:07:19 -07004696 dev_err(DEV, "Got NegDReply; Sector %llus, len %u; Fail original request.\n",
4697 (unsigned long long)sector, be32_to_cpu(p->blksize));
4698
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004699 return validate_req_change_req_state(mdev, p->block_id, sector,
4700 &mdev->read_requests, __func__,
4701 NEG_ACKED, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004702}
4703
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004704static int got_NegRSDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004705{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004706 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004707 sector_t sector;
4708 int size;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004709 struct p_block_ack *p = pi->data;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004710
4711 mdev = vnr_to_mdev(tconn, pi->vnr);
4712 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004713 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004714
4715 sector = be64_to_cpu(p->sector);
4716 size = be32_to_cpu(p->blksize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004717
4718 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4719
4720 dec_rs_pending(mdev);
4721
4722 if (get_ldev_if_state(mdev, D_FAILED)) {
4723 drbd_rs_complete_io(mdev, sector);
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004724 switch (pi->cmd) {
Philipp Reisnerd612d302010-12-27 10:53:28 +01004725 case P_NEG_RS_DREPLY:
4726 drbd_rs_failed_io(mdev, sector, size);
4727 case P_RS_CANCEL:
4728 break;
4729 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004730 BUG();
Philipp Reisnerd612d302010-12-27 10:53:28 +01004731 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004732 put_ldev(mdev);
4733 }
4734
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004735 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004736}
4737
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004738static int got_BarrierAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004739{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004740 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004741 struct p_barrier_ack *p = pi->data;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004742
4743 mdev = vnr_to_mdev(tconn, pi->vnr);
4744 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004745 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004746
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01004747 tl_release(mdev->tconn, p->barrier, be32_to_cpu(p->set_size));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004748
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02004749 if (mdev->state.conn == C_AHEAD &&
4750 atomic_read(&mdev->ap_in_flight) == 0 &&
Philipp Reisner370a43e2011-01-14 16:03:11 +01004751 !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->current_epoch->flags)) {
4752 mdev->start_resync_timer.expires = jiffies + HZ;
4753 add_timer(&mdev->start_resync_timer);
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02004754 }
4755
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004756 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004757}
4758
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004759static int got_OVResult(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004760{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004761 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004762 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004763 struct drbd_work *w;
4764 sector_t sector;
4765 int size;
4766
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004767 mdev = vnr_to_mdev(tconn, pi->vnr);
4768 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004769 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004770
Philipp Reisnerb411b362009-09-25 16:07:19 -07004771 sector = be64_to_cpu(p->sector);
4772 size = be32_to_cpu(p->blksize);
4773
4774 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4775
4776 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01004777 drbd_ov_out_of_sync_found(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004778 else
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01004779 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004780
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004781 if (!get_ldev(mdev))
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004782 return 0;
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004783
Philipp Reisnerb411b362009-09-25 16:07:19 -07004784 drbd_rs_complete_io(mdev, sector);
4785 dec_rs_pending(mdev);
4786
Lars Ellenbergea5442a2010-11-05 09:48:01 +01004787 --mdev->ov_left;
4788
4789 /* let's advance progress step marks only for every other megabyte */
4790 if ((mdev->ov_left & 0x200) == 0x200)
4791 drbd_advance_rs_marks(mdev, mdev->ov_left);
4792
4793 if (mdev->ov_left == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004794 w = kmalloc(sizeof(*w), GFP_NOIO);
4795 if (w) {
4796 w->cb = w_ov_finished;
Philipp Reisnera21e9292011-02-08 15:08:49 +01004797 w->mdev = mdev;
Philipp Reisnere42325a2011-01-19 13:55:45 +01004798 drbd_queue_work_front(&mdev->tconn->data.work, w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004799 } else {
4800 dev_err(DEV, "kmalloc(w) failed.");
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01004801 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004802 drbd_resync_finished(mdev);
4803 }
4804 }
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004805 put_ldev(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004806 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004807}
4808
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004809static int got_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner0ced55a2010-04-30 15:26:20 +02004810{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004811 return 0;
Philipp Reisner0ced55a2010-04-30 15:26:20 +02004812}
4813
Andreas Gruenbachera990be42011-04-06 17:56:48 +02004814static int tconn_finish_peer_reqs(struct drbd_tconn *tconn)
Philipp Reisner32862ec2011-02-08 16:41:01 +01004815{
Philipp Reisner082a3432011-03-15 16:05:42 +01004816 struct drbd_conf *mdev;
4817 int i, not_empty = 0;
Philipp Reisner32862ec2011-02-08 16:41:01 +01004818
4819 do {
4820 clear_bit(SIGNAL_ASENDER, &tconn->flags);
4821 flush_signals(current);
Philipp Reisner082a3432011-03-15 16:05:42 +01004822 idr_for_each_entry(&tconn->volumes, mdev, i) {
Andreas Gruenbachera990be42011-04-06 17:56:48 +02004823 if (drbd_finish_peer_reqs(mdev))
Philipp Reisner082a3432011-03-15 16:05:42 +01004824 return 1; /* error */
4825 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01004826 set_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisner082a3432011-03-15 16:05:42 +01004827
4828 spin_lock_irq(&tconn->req_lock);
4829 idr_for_each_entry(&tconn->volumes, mdev, i) {
4830 not_empty = !list_empty(&mdev->done_ee);
4831 if (not_empty)
4832 break;
4833 }
4834 spin_unlock_irq(&tconn->req_lock);
Philipp Reisner32862ec2011-02-08 16:41:01 +01004835 } while (not_empty);
4836
4837 return 0;
4838}
4839
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004840struct asender_cmd {
4841 size_t pkt_size;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004842 int (*fn)(struct drbd_tconn *tconn, struct packet_info *);
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004843};
4844
4845static struct asender_cmd asender_tbl[] = {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004846 [P_PING] = { 0, got_Ping },
4847 [P_PING_ACK] = { 0, got_PingAck },
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004848 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4849 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4850 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4851 [P_DISCARD_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
4852 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
4853 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
4854 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply },
4855 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
4856 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
4857 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
4858 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
4859 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
4860 [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply },
4861 [P_CONN_ST_CHG_REPLY]={ sizeof(struct p_req_state_reply), got_conn_RqSReply },
4862 [P_RETRY_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004863};
4864
Philipp Reisnerb411b362009-09-25 16:07:19 -07004865int drbd_asender(struct drbd_thread *thi)
4866{
Philipp Reisner392c8802011-02-09 10:33:31 +01004867 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004868 struct asender_cmd *cmd = NULL;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004869 struct packet_info pi;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004870 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004871 void *buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004872 int received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02004873 unsigned int header_size = drbd_header_size(tconn);
4874 int expect = header_size;
Lars Ellenbergf36af182011-03-09 22:44:55 +01004875 int ping_timeout_active = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004876
Philipp Reisnerb411b362009-09-25 16:07:19 -07004877 current->policy = SCHED_RR; /* Make this a realtime task! */
4878 current->rt_priority = 2; /* more important than all other tasks */
4879
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01004880 while (get_t_state(thi) == RUNNING) {
Philipp Reisner80822282011-02-08 12:46:30 +01004881 drbd_thread_current_set_cpu(thi);
Philipp Reisner32862ec2011-02-08 16:41:01 +01004882 if (test_and_clear_bit(SEND_PING, &tconn->flags)) {
Andreas Gruenbachera17647a2011-04-01 12:49:42 +02004883 if (drbd_send_ping(tconn)) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004884 conn_err(tconn, "drbd_send_ping has failed\n");
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01004885 goto reconnect;
4886 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01004887 tconn->meta.socket->sk->sk_rcvtimeo =
4888 tconn->net_conf->ping_timeo*HZ/10;
Lars Ellenbergf36af182011-03-09 22:44:55 +01004889 ping_timeout_active = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004890 }
4891
Philipp Reisner32862ec2011-02-08 16:41:01 +01004892 /* TODO: conditionally cork; it may hurt latency if we cork without
4893 much to send */
4894 if (!tconn->net_conf->no_cork)
4895 drbd_tcp_cork(tconn->meta.socket);
Andreas Gruenbachera990be42011-04-06 17:56:48 +02004896 if (tconn_finish_peer_reqs(tconn)) {
4897 conn_err(tconn, "tconn_finish_peer_reqs() failed\n");
Philipp Reisner32862ec2011-02-08 16:41:01 +01004898 goto reconnect;
Philipp Reisner082a3432011-03-15 16:05:42 +01004899 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004900 /* but unconditionally uncork unless disabled */
Philipp Reisner32862ec2011-02-08 16:41:01 +01004901 if (!tconn->net_conf->no_cork)
4902 drbd_tcp_uncork(tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004903
4904 /* short circuit, recv_msg would return EINTR anyways. */
4905 if (signal_pending(current))
4906 continue;
4907
Philipp Reisner32862ec2011-02-08 16:41:01 +01004908 rv = drbd_recv_short(tconn->meta.socket, buf, expect-received, 0);
4909 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004910
4911 flush_signals(current);
4912
4913 /* Note:
4914 * -EINTR (on meta) we got a signal
4915 * -EAGAIN (on meta) rcvtimeo expired
4916 * -ECONNRESET other side closed the connection
4917 * -ERESTARTSYS (on data) we got a signal
4918 * rv < 0 other than above: unexpected error!
4919 * rv == expected: full header or command
4920 * rv < expected: "woken" by signal during receive
4921 * rv == 0 : "connection shut down by peer"
4922 */
4923 if (likely(rv > 0)) {
4924 received += rv;
4925 buf += rv;
4926 } else if (rv == 0) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004927 conn_err(tconn, "meta connection shut down by peer.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004928 goto reconnect;
4929 } else if (rv == -EAGAIN) {
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02004930 /* If the data socket received something meanwhile,
4931 * that is good enough: peer is still alive. */
Philipp Reisner32862ec2011-02-08 16:41:01 +01004932 if (time_after(tconn->last_received,
4933 jiffies - tconn->meta.socket->sk->sk_rcvtimeo))
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02004934 continue;
Lars Ellenbergf36af182011-03-09 22:44:55 +01004935 if (ping_timeout_active) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004936 conn_err(tconn, "PingAck did not arrive in time.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004937 goto reconnect;
4938 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01004939 set_bit(SEND_PING, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004940 continue;
4941 } else if (rv == -EINTR) {
4942 continue;
4943 } else {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004944 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004945 goto reconnect;
4946 }
4947
4948 if (received == expect && cmd == NULL) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004949 if (decode_header(tconn, tconn->meta.rbuf, &pi))
Philipp Reisnerb411b362009-09-25 16:07:19 -07004950 goto reconnect;
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004951 cmd = &asender_tbl[pi.cmd];
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004952 if (pi.cmd >= ARRAY_SIZE(asender_tbl) || !cmd->fn) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004953 conn_err(tconn, "unknown command %d on meta (l: %d)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004954 pi.cmd, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004955 goto disconnect;
4956 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004957 expect = header_size + cmd->pkt_size;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02004958 if (pi.size != expect - header_size) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004959 conn_err(tconn, "Wrong packet size on meta (c: %d, l: %d)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004960 pi.cmd, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004961 goto reconnect;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004962 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004963 }
4964 if (received == expect) {
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004965 bool err;
Philipp Reisnera4fbda82011-03-16 11:13:17 +01004966
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004967 err = cmd->fn(tconn, &pi);
4968 if (err) {
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004969 conn_err(tconn, "%pf failed\n", cmd->fn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004970 goto reconnect;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004971 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004972
Philipp Reisnera4fbda82011-03-16 11:13:17 +01004973 tconn->last_received = jiffies;
4974
Lars Ellenbergf36af182011-03-09 22:44:55 +01004975 /* the idle_timeout (ping-int)
4976 * has been restored in got_PingAck() */
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004977 if (cmd == &asender_tbl[P_PING_ACK])
Lars Ellenbergf36af182011-03-09 22:44:55 +01004978 ping_timeout_active = 0;
4979
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004980 buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004981 received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02004982 expect = header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004983 cmd = NULL;
4984 }
4985 }
4986
4987 if (0) {
4988reconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004989 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004990 }
4991 if (0) {
4992disconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004993 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004994 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01004995 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004996
Philipp Reisner32862ec2011-02-08 16:41:01 +01004997 conn_info(tconn, "asender terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004998
4999 return 0;
5000}