blob: 9e56010a1078a0e2d427c3d56f7fac602a267b2c [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
Philipp Reisnerb411b362009-09-25 16:07:19 -0700198static void reclaim_net_ee(struct drbd_conf *mdev, struct list_head *to_be_freed)
199{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100200 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700201 struct list_head *le, *tle;
202
203 /* The EEs are always appended to the end of the list. Since
204 they are sent in order over the wire, they have to finish
205 in order. As soon as we see the first not finished we can
206 stop to examine the list... */
207
208 list_for_each_safe(le, tle, &mdev->net_ee) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100209 peer_req = list_entry(le, struct drbd_peer_request, w.list);
210 if (drbd_ee_has_active_page(peer_req))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700211 break;
212 list_move(le, to_be_freed);
213 }
214}
215
216static void drbd_kick_lo_and_reclaim_net(struct drbd_conf *mdev)
217{
218 LIST_HEAD(reclaimed);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100219 struct drbd_peer_request *peer_req, *t;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700220
Philipp Reisner87eeee42011-01-19 14:16:30 +0100221 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700222 reclaim_net_ee(mdev, &reclaimed);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100223 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700224
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100225 list_for_each_entry_safe(peer_req, t, &reclaimed, w.list)
226 drbd_free_net_ee(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700227}
228
229/**
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200230 * drbd_pp_alloc() - Returns @number pages, retries forever (or until signalled)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700231 * @mdev: DRBD device.
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200232 * @number: number of pages requested
233 * @retry: whether to retry, if not enough pages are available right now
Philipp Reisnerb411b362009-09-25 16:07:19 -0700234 *
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200235 * Tries to allocate number pages, first from our own page pool, then from
236 * the kernel, unless this allocation would exceed the max_buffers setting.
237 * Possibly retry until DRBD frees sufficient pages somewhere else.
238 *
239 * Returns a page chain linked via page->private.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700240 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200241static struct page *drbd_pp_alloc(struct drbd_conf *mdev, unsigned number, bool retry)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700242{
243 struct page *page = NULL;
244 DEFINE_WAIT(wait);
245
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200246 /* Yes, we may run up to @number over max_buffers. If we
247 * follow it strictly, the admin will get it wrong anyways. */
Philipp Reisner89e58e72011-01-19 13:12:45 +0100248 if (atomic_read(&mdev->pp_in_use) < mdev->tconn->net_conf->max_buffers)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200249 page = drbd_pp_first_pages_or_try_alloc(mdev, number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700250
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200251 while (page == NULL) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700252 prepare_to_wait(&drbd_pp_wait, &wait, TASK_INTERRUPTIBLE);
253
254 drbd_kick_lo_and_reclaim_net(mdev);
255
Philipp Reisner89e58e72011-01-19 13:12:45 +0100256 if (atomic_read(&mdev->pp_in_use) < mdev->tconn->net_conf->max_buffers) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200257 page = drbd_pp_first_pages_or_try_alloc(mdev, number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700258 if (page)
259 break;
260 }
261
262 if (!retry)
263 break;
264
265 if (signal_pending(current)) {
266 dev_warn(DEV, "drbd_pp_alloc interrupted!\n");
267 break;
268 }
269
270 schedule();
271 }
272 finish_wait(&drbd_pp_wait, &wait);
273
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200274 if (page)
275 atomic_add(number, &mdev->pp_in_use);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700276 return page;
277}
278
279/* Must not be used from irq, as that may deadlock: see drbd_pp_alloc.
Philipp Reisner87eeee42011-01-19 14:16:30 +0100280 * Is also used from inside an other spin_lock_irq(&mdev->tconn->req_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200281 * Either links the page chain back to the global pool,
282 * or returns all pages to the system. */
Lars Ellenberg435f0742010-09-06 12:30:25 +0200283static void drbd_pp_free(struct drbd_conf *mdev, struct page *page, int is_net)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700284{
Lars Ellenberg435f0742010-09-06 12:30:25 +0200285 atomic_t *a = is_net ? &mdev->pp_in_use_by_net : &mdev->pp_in_use;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700286 int i;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200287
Philipp Reisner81a5d602011-02-22 19:53:16 -0500288 if (drbd_pp_vacant > (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * minor_count)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200289 i = page_chain_free(page);
290 else {
291 struct page *tmp;
292 tmp = page_chain_tail(page, &i);
293 spin_lock(&drbd_pp_lock);
294 page_chain_add(&drbd_pp_pool, page, tmp);
295 drbd_pp_vacant += i;
296 spin_unlock(&drbd_pp_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700297 }
Lars Ellenberg435f0742010-09-06 12:30:25 +0200298 i = atomic_sub_return(i, a);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200299 if (i < 0)
Lars Ellenberg435f0742010-09-06 12:30:25 +0200300 dev_warn(DEV, "ASSERTION FAILED: %s: %d < 0\n",
301 is_net ? "pp_in_use_by_net" : "pp_in_use", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700302 wake_up(&drbd_pp_wait);
303}
304
305/*
306You need to hold the req_lock:
307 _drbd_wait_ee_list_empty()
308
309You must not have the req_lock:
310 drbd_free_ee()
Andreas Gruenbacher0db55362011-04-06 16:09:15 +0200311 drbd_alloc_peer_req()
Philipp Reisnerb411b362009-09-25 16:07:19 -0700312 drbd_release_ee()
313 drbd_ee_fix_bhs()
314 drbd_process_done_ee()
315 drbd_clear_done_ee()
316 drbd_wait_ee_list_empty()
317*/
318
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +0100319struct drbd_peer_request *
Andreas Gruenbacher0db55362011-04-06 16:09:15 +0200320drbd_alloc_peer_req(struct drbd_conf *mdev, u64 id, sector_t sector,
321 unsigned int data_size, gfp_t gfp_mask) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700322{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100323 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700324 struct page *page;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200325 unsigned nr_pages = (data_size + PAGE_SIZE -1) >> PAGE_SHIFT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700326
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100327 if (drbd_insert_fault(mdev, DRBD_FAULT_AL_EE))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700328 return NULL;
329
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100330 peer_req = mempool_alloc(drbd_ee_mempool, gfp_mask & ~__GFP_HIGHMEM);
331 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700332 if (!(gfp_mask & __GFP_NOWARN))
Andreas Gruenbacher0db55362011-04-06 16:09:15 +0200333 dev_err(DEV, "%s: allocation failed\n", __func__);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700334 return NULL;
335 }
336
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200337 page = drbd_pp_alloc(mdev, nr_pages, (gfp_mask & __GFP_WAIT));
338 if (!page)
339 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700340
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100341 drbd_clear_interval(&peer_req->i);
342 peer_req->i.size = data_size;
343 peer_req->i.sector = sector;
344 peer_req->i.local = false;
345 peer_req->i.waiting = false;
Andreas Gruenbacher53840642011-01-28 10:31:04 +0100346
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100347 peer_req->epoch = NULL;
Philipp Reisnera21e9292011-02-08 15:08:49 +0100348 peer_req->w.mdev = mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100349 peer_req->pages = page;
350 atomic_set(&peer_req->pending_bios, 0);
351 peer_req->flags = 0;
Andreas Gruenbacher9a8e7752011-01-11 14:04:09 +0100352 /*
353 * The block_id is opaque to the receiver. It is not endianness
354 * converted, and sent back to the sender unchanged.
355 */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100356 peer_req->block_id = id;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700357
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100358 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700359
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200360 fail:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100361 mempool_free(peer_req, drbd_ee_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700362 return NULL;
363}
364
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100365void drbd_free_some_ee(struct drbd_conf *mdev, struct drbd_peer_request *peer_req,
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +0100366 int is_net)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700367{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100368 if (peer_req->flags & EE_HAS_DIGEST)
369 kfree(peer_req->digest);
370 drbd_pp_free(mdev, peer_req->pages, is_net);
371 D_ASSERT(atomic_read(&peer_req->pending_bios) == 0);
372 D_ASSERT(drbd_interval_empty(&peer_req->i));
373 mempool_free(peer_req, drbd_ee_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700374}
375
376int drbd_release_ee(struct drbd_conf *mdev, struct list_head *list)
377{
378 LIST_HEAD(work_list);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100379 struct drbd_peer_request *peer_req, *t;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700380 int count = 0;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200381 int is_net = list == &mdev->net_ee;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700382
Philipp Reisner87eeee42011-01-19 14:16:30 +0100383 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700384 list_splice_init(list, &work_list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100385 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700386
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100387 list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
388 drbd_free_some_ee(mdev, peer_req, is_net);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700389 count++;
390 }
391 return count;
392}
393
394
Philipp Reisner32862ec2011-02-08 16:41:01 +0100395/* See also comments in _req_mod(,BARRIER_ACKED)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700396 * and receive_Barrier.
397 *
398 * Move entries from net_ee to done_ee, if ready.
399 * Grab done_ee, call all callbacks, free the entries.
400 * The callbacks typically send out ACKs.
401 */
402static int drbd_process_done_ee(struct drbd_conf *mdev)
403{
404 LIST_HEAD(work_list);
405 LIST_HEAD(reclaimed);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100406 struct drbd_peer_request *peer_req, *t;
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100407 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700408
Philipp Reisner87eeee42011-01-19 14:16:30 +0100409 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700410 reclaim_net_ee(mdev, &reclaimed);
411 list_splice_init(&mdev->done_ee, &work_list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100412 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700413
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100414 list_for_each_entry_safe(peer_req, t, &reclaimed, w.list)
415 drbd_free_net_ee(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700416
417 /* possible callbacks here:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100418 * e_end_block, and e_end_resync_block, e_send_discard_write.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700419 * all ignore the last argument.
420 */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100421 list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100422 int err2;
423
Philipp Reisnerb411b362009-09-25 16:07:19 -0700424 /* list_del not necessary, next/prev members not touched */
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100425 err2 = peer_req->w.cb(&peer_req->w, !!err);
426 if (!err)
427 err = err2;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100428 drbd_free_ee(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700429 }
430 wake_up(&mdev->ee_wait);
431
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100432 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700433}
434
435void _drbd_wait_ee_list_empty(struct drbd_conf *mdev, struct list_head *head)
436{
437 DEFINE_WAIT(wait);
438
439 /* avoids spin_lock/unlock
440 * and calling prepare_to_wait in the fast path */
441 while (!list_empty(head)) {
442 prepare_to_wait(&mdev->ee_wait, &wait, TASK_UNINTERRUPTIBLE);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100443 spin_unlock_irq(&mdev->tconn->req_lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100444 io_schedule();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700445 finish_wait(&mdev->ee_wait, &wait);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100446 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700447 }
448}
449
450void drbd_wait_ee_list_empty(struct drbd_conf *mdev, struct list_head *head)
451{
Philipp Reisner87eeee42011-01-19 14:16:30 +0100452 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700453 _drbd_wait_ee_list_empty(mdev, head);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100454 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700455}
456
457/* see also kernel_accept; which is only present since 2.6.18.
458 * also we want to log which part of it failed, exactly */
Philipp Reisner76536202011-02-07 14:09:54 +0100459static int drbd_accept(const char **what, struct socket *sock, struct socket **newsock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700460{
461 struct sock *sk = sock->sk;
462 int err = 0;
463
464 *what = "listen";
465 err = sock->ops->listen(sock, 5);
466 if (err < 0)
467 goto out;
468
469 *what = "sock_create_lite";
470 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
471 newsock);
472 if (err < 0)
473 goto out;
474
475 *what = "accept";
476 err = sock->ops->accept(sock, *newsock, 0);
477 if (err < 0) {
478 sock_release(*newsock);
479 *newsock = NULL;
480 goto out;
481 }
482 (*newsock)->ops = sock->ops;
483
484out:
485 return err;
486}
487
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100488static int drbd_recv_short(struct socket *sock, void *buf, size_t size, int flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700489{
490 mm_segment_t oldfs;
491 struct kvec iov = {
492 .iov_base = buf,
493 .iov_len = size,
494 };
495 struct msghdr msg = {
496 .msg_iovlen = 1,
497 .msg_iov = (struct iovec *)&iov,
498 .msg_flags = (flags ? flags : MSG_WAITALL | MSG_NOSIGNAL)
499 };
500 int rv;
501
502 oldfs = get_fs();
503 set_fs(KERNEL_DS);
504 rv = sock_recvmsg(sock, &msg, size, msg.msg_flags);
505 set_fs(oldfs);
506
507 return rv;
508}
509
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100510static int drbd_recv(struct drbd_tconn *tconn, void *buf, size_t size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700511{
512 mm_segment_t oldfs;
513 struct kvec iov = {
514 .iov_base = buf,
515 .iov_len = size,
516 };
517 struct msghdr msg = {
518 .msg_iovlen = 1,
519 .msg_iov = (struct iovec *)&iov,
520 .msg_flags = MSG_WAITALL | MSG_NOSIGNAL
521 };
522 int rv;
523
524 oldfs = get_fs();
525 set_fs(KERNEL_DS);
526
527 for (;;) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100528 rv = sock_recvmsg(tconn->data.socket, &msg, size, msg.msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700529 if (rv == size)
530 break;
531
532 /* Note:
533 * ECONNRESET other side closed the connection
534 * ERESTARTSYS (on sock) we got a signal
535 */
536
537 if (rv < 0) {
538 if (rv == -ECONNRESET)
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100539 conn_info(tconn, "sock was reset by peer\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700540 else if (rv != -ERESTARTSYS)
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100541 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700542 break;
543 } else if (rv == 0) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100544 conn_info(tconn, "sock was shut down by peer\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700545 break;
546 } else {
547 /* signal came in, or peer/link went down,
548 * after we read a partial message
549 */
550 /* D_ASSERT(signal_pending(current)); */
551 break;
552 }
553 };
554
555 set_fs(oldfs);
556
557 if (rv != size)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100558 conn_request_state(tconn, NS(conn, C_BROKEN_PIPE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700559
560 return rv;
561}
562
Andreas Gruenbacherc6967742011-03-17 17:15:20 +0100563static int drbd_recv_all(struct drbd_tconn *tconn, void *buf, size_t size)
564{
565 int err;
566
567 err = drbd_recv(tconn, buf, size);
568 if (err != size) {
569 if (err >= 0)
570 err = -EIO;
571 } else
572 err = 0;
573 return err;
574}
575
Andreas Gruenbachera5c31902011-03-24 03:28:04 +0100576static int drbd_recv_all_warn(struct drbd_tconn *tconn, void *buf, size_t size)
577{
578 int err;
579
580 err = drbd_recv_all(tconn, buf, size);
581 if (err && !signal_pending(current))
582 conn_warn(tconn, "short read (expected size %d)\n", (int)size);
583 return err;
584}
585
Lars Ellenberg5dbf1672010-05-25 16:18:01 +0200586/* quoting tcp(7):
587 * On individual connections, the socket buffer size must be set prior to the
588 * listen(2) or connect(2) calls in order to have it take effect.
589 * This is our wrapper to do so.
590 */
591static void drbd_setbufsize(struct socket *sock, unsigned int snd,
592 unsigned int rcv)
593{
594 /* open coded SO_SNDBUF, SO_RCVBUF */
595 if (snd) {
596 sock->sk->sk_sndbuf = snd;
597 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
598 }
599 if (rcv) {
600 sock->sk->sk_rcvbuf = rcv;
601 sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
602 }
603}
604
Philipp Reisnereac3e992011-02-07 14:05:07 +0100605static struct socket *drbd_try_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700606{
607 const char *what;
608 struct socket *sock;
609 struct sockaddr_in6 src_in6;
610 int err;
611 int disconnect_on_error = 1;
612
Philipp Reisnereac3e992011-02-07 14:05:07 +0100613 if (!get_net_conf(tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700614 return NULL;
615
616 what = "sock_create_kern";
Philipp Reisnereac3e992011-02-07 14:05:07 +0100617 err = sock_create_kern(((struct sockaddr *)tconn->net_conf->my_addr)->sa_family,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700618 SOCK_STREAM, IPPROTO_TCP, &sock);
619 if (err < 0) {
620 sock = NULL;
621 goto out;
622 }
623
624 sock->sk->sk_rcvtimeo =
Philipp Reisnereac3e992011-02-07 14:05:07 +0100625 sock->sk->sk_sndtimeo = tconn->net_conf->try_connect_int*HZ;
626 drbd_setbufsize(sock, tconn->net_conf->sndbuf_size,
627 tconn->net_conf->rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700628
629 /* explicitly bind to the configured IP as source IP
630 * for the outgoing connections.
631 * This is needed for multihomed hosts and to be
632 * able to use lo: interfaces for drbd.
633 * Make sure to use 0 as port number, so linux selects
634 * a free one dynamically.
635 */
Philipp Reisnereac3e992011-02-07 14:05:07 +0100636 memcpy(&src_in6, tconn->net_conf->my_addr,
637 min_t(int, tconn->net_conf->my_addr_len, sizeof(src_in6)));
638 if (((struct sockaddr *)tconn->net_conf->my_addr)->sa_family == AF_INET6)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700639 src_in6.sin6_port = 0;
640 else
641 ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */
642
643 what = "bind before connect";
644 err = sock->ops->bind(sock,
645 (struct sockaddr *) &src_in6,
Philipp Reisnereac3e992011-02-07 14:05:07 +0100646 tconn->net_conf->my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700647 if (err < 0)
648 goto out;
649
650 /* connect may fail, peer not yet available.
651 * stay C_WF_CONNECTION, don't go Disconnecting! */
652 disconnect_on_error = 0;
653 what = "connect";
654 err = sock->ops->connect(sock,
Philipp Reisnereac3e992011-02-07 14:05:07 +0100655 (struct sockaddr *)tconn->net_conf->peer_addr,
656 tconn->net_conf->peer_addr_len, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700657
658out:
659 if (err < 0) {
660 if (sock) {
661 sock_release(sock);
662 sock = NULL;
663 }
664 switch (-err) {
665 /* timeout, busy, signal pending */
666 case ETIMEDOUT: case EAGAIN: case EINPROGRESS:
667 case EINTR: case ERESTARTSYS:
668 /* peer not (yet) available, network problem */
669 case ECONNREFUSED: case ENETUNREACH:
670 case EHOSTDOWN: case EHOSTUNREACH:
671 disconnect_on_error = 0;
672 break;
673 default:
Philipp Reisnereac3e992011-02-07 14:05:07 +0100674 conn_err(tconn, "%s failed, err = %d\n", what, err);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700675 }
676 if (disconnect_on_error)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100677 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700678 }
Philipp Reisnereac3e992011-02-07 14:05:07 +0100679 put_net_conf(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700680 return sock;
681}
682
Philipp Reisner76536202011-02-07 14:09:54 +0100683static struct socket *drbd_wait_for_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700684{
685 int timeo, err;
686 struct socket *s_estab = NULL, *s_listen;
687 const char *what;
688
Philipp Reisner76536202011-02-07 14:09:54 +0100689 if (!get_net_conf(tconn))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700690 return NULL;
691
692 what = "sock_create_kern";
Philipp Reisner76536202011-02-07 14:09:54 +0100693 err = sock_create_kern(((struct sockaddr *)tconn->net_conf->my_addr)->sa_family,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700694 SOCK_STREAM, IPPROTO_TCP, &s_listen);
695 if (err) {
696 s_listen = NULL;
697 goto out;
698 }
699
Philipp Reisner76536202011-02-07 14:09:54 +0100700 timeo = tconn->net_conf->try_connect_int * HZ;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700701 timeo += (random32() & 1) ? timeo / 7 : -timeo / 7; /* 28.5% random jitter */
702
703 s_listen->sk->sk_reuse = 1; /* SO_REUSEADDR */
704 s_listen->sk->sk_rcvtimeo = timeo;
705 s_listen->sk->sk_sndtimeo = timeo;
Philipp Reisner76536202011-02-07 14:09:54 +0100706 drbd_setbufsize(s_listen, tconn->net_conf->sndbuf_size,
707 tconn->net_conf->rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700708
709 what = "bind before listen";
710 err = s_listen->ops->bind(s_listen,
Philipp Reisner76536202011-02-07 14:09:54 +0100711 (struct sockaddr *) tconn->net_conf->my_addr,
712 tconn->net_conf->my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700713 if (err < 0)
714 goto out;
715
Philipp Reisner76536202011-02-07 14:09:54 +0100716 err = drbd_accept(&what, s_listen, &s_estab);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700717
718out:
719 if (s_listen)
720 sock_release(s_listen);
721 if (err < 0) {
722 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
Philipp Reisner76536202011-02-07 14:09:54 +0100723 conn_err(tconn, "%s failed, err = %d\n", what, err);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100724 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700725 }
726 }
Philipp Reisner76536202011-02-07 14:09:54 +0100727 put_net_conf(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700728
729 return s_estab;
730}
731
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200732static int decode_header(struct drbd_tconn *, void *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700733
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200734static int send_first_packet(struct drbd_tconn *tconn, struct drbd_socket *sock,
735 enum drbd_packet cmd)
736{
737 if (!conn_prepare_command(tconn, sock))
738 return -EIO;
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200739 return conn_send_command(tconn, sock, cmd, 0, NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700740}
741
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200742static int receive_first_packet(struct drbd_tconn *tconn, struct socket *sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700743{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200744 unsigned int header_size = drbd_header_size(tconn);
745 struct packet_info pi;
746 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700747
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200748 err = drbd_recv_short(sock, tconn->data.rbuf, header_size, 0);
749 if (err != header_size) {
750 if (err >= 0)
751 err = -EIO;
752 return err;
753 }
754 err = decode_header(tconn, tconn->data.rbuf, &pi);
755 if (err)
756 return err;
757 return pi.cmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700758}
759
760/**
761 * drbd_socket_okay() - Free the socket if its connection is not okay
Philipp Reisnerb411b362009-09-25 16:07:19 -0700762 * @sock: pointer to the pointer to the socket.
763 */
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100764static int drbd_socket_okay(struct socket **sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700765{
766 int rr;
767 char tb[4];
768
769 if (!*sock)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100770 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700771
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100772 rr = drbd_recv_short(*sock, tb, 4, MSG_DONTWAIT | MSG_PEEK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700773
774 if (rr > 0 || rr == -EAGAIN) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100775 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700776 } else {
777 sock_release(*sock);
778 *sock = NULL;
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100779 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700780 }
781}
Philipp Reisner2325eb62011-03-15 16:56:18 +0100782/* Gets called if a connection is established, or if a new minor gets created
783 in a connection */
784int drbd_connected(int vnr, void *p, void *data)
Philipp Reisner907599e2011-02-08 11:25:37 +0100785{
786 struct drbd_conf *mdev = (struct drbd_conf *)p;
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100787 int err;
Philipp Reisner907599e2011-02-08 11:25:37 +0100788
789 atomic_set(&mdev->packet_seq, 0);
790 mdev->peer_seq = 0;
791
Philipp Reisner8410da8f02011-02-11 20:11:10 +0100792 mdev->state_mutex = mdev->tconn->agreed_pro_version < 100 ?
793 &mdev->tconn->cstate_mutex :
794 &mdev->own_state_mutex;
795
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100796 err = drbd_send_sync_param(mdev);
797 if (!err)
798 err = drbd_send_sizes(mdev, 0, 0);
799 if (!err)
800 err = drbd_send_uuids(mdev);
801 if (!err)
802 err = drbd_send_state(mdev);
Philipp Reisner907599e2011-02-08 11:25:37 +0100803 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
804 clear_bit(RESIZE_PENDING, &mdev->flags);
Philipp Reisner8b924f12011-03-01 11:08:28 +0100805 mod_timer(&mdev->request_timer, jiffies + HZ); /* just start it here. */
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100806 return err;
Philipp Reisner907599e2011-02-08 11:25:37 +0100807}
808
Philipp Reisnerb411b362009-09-25 16:07:19 -0700809/*
810 * return values:
811 * 1 yes, we have a valid connection
812 * 0 oops, did not work out, please try again
813 * -1 peer talks different language,
814 * no point in trying again, please go standalone.
815 * -2 We do not have a network config...
816 */
Philipp Reisner907599e2011-02-08 11:25:37 +0100817static int drbd_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700818{
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200819 struct socket *sock, *msock;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700820 int try, h, ok;
821
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100822 if (conn_request_state(tconn, NS(conn, C_WF_CONNECTION), CS_VERBOSE) < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700823 return -2;
824
Philipp Reisner907599e2011-02-08 11:25:37 +0100825 clear_bit(DISCARD_CONCURRENT, &tconn->flags);
Andreas Gruenbacher0916e0e2011-03-21 14:10:15 +0100826
827 /* Assume that the peer only understands protocol 80 until we know better. */
828 tconn->agreed_pro_version = 80;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700829
Philipp Reisnerb411b362009-09-25 16:07:19 -0700830 do {
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200831 struct socket *s;
832
Philipp Reisnerb411b362009-09-25 16:07:19 -0700833 for (try = 0;;) {
834 /* 3 tries, this should take less than a second! */
Philipp Reisner907599e2011-02-08 11:25:37 +0100835 s = drbd_try_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700836 if (s || ++try >= 3)
837 break;
838 /* give the other side time to call bind() & listen() */
Philipp Reisner20ee6392011-01-18 15:28:59 +0100839 schedule_timeout_interruptible(HZ / 10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700840 }
841
842 if (s) {
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200843 if (!tconn->data.socket) {
844 tconn->data.socket = s;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200845 send_first_packet(tconn, &tconn->data, P_INITIAL_DATA);
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200846 } else if (!tconn->meta.socket) {
847 tconn->meta.socket = s;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200848 send_first_packet(tconn, &tconn->meta, P_INITIAL_META);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700849 } else {
Philipp Reisner907599e2011-02-08 11:25:37 +0100850 conn_err(tconn, "Logic error in drbd_connect()\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700851 goto out_release_sockets;
852 }
853 }
854
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200855 if (tconn->data.socket && tconn->meta.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100856 schedule_timeout_interruptible(tconn->net_conf->ping_timeo*HZ/10);
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200857 ok = drbd_socket_okay(&tconn->data.socket);
858 ok = drbd_socket_okay(&tconn->meta.socket) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700859 if (ok)
860 break;
861 }
862
863retry:
Philipp Reisner907599e2011-02-08 11:25:37 +0100864 s = drbd_wait_for_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700865 if (s) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200866 try = receive_first_packet(tconn, s);
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200867 drbd_socket_okay(&tconn->data.socket);
868 drbd_socket_okay(&tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700869 switch (try) {
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200870 case P_INITIAL_DATA:
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200871 if (tconn->data.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100872 conn_warn(tconn, "initial packet S crossed\n");
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200873 sock_release(tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700874 }
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200875 tconn->data.socket = s;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700876 break;
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200877 case P_INITIAL_META:
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200878 if (tconn->meta.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100879 conn_warn(tconn, "initial packet M crossed\n");
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200880 sock_release(tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700881 }
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200882 tconn->meta.socket = s;
Philipp Reisner907599e2011-02-08 11:25:37 +0100883 set_bit(DISCARD_CONCURRENT, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700884 break;
885 default:
Philipp Reisner907599e2011-02-08 11:25:37 +0100886 conn_warn(tconn, "Error receiving initial packet\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700887 sock_release(s);
888 if (random32() & 1)
889 goto retry;
890 }
891 }
892
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100893 if (tconn->cstate <= C_DISCONNECTING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700894 goto out_release_sockets;
895 if (signal_pending(current)) {
896 flush_signals(current);
897 smp_rmb();
Philipp Reisner907599e2011-02-08 11:25:37 +0100898 if (get_t_state(&tconn->receiver) == EXITING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700899 goto out_release_sockets;
900 }
901
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200902 if (tconn->data.socket && &tconn->meta.socket) {
903 ok = drbd_socket_okay(&tconn->data.socket);
904 ok = drbd_socket_okay(&tconn->meta.socket) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700905 if (ok)
906 break;
907 }
908 } while (1);
909
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200910 sock = tconn->data.socket;
911 msock = tconn->meta.socket;
912
Philipp Reisnerb411b362009-09-25 16:07:19 -0700913 msock->sk->sk_reuse = 1; /* SO_REUSEADDR */
914 sock->sk->sk_reuse = 1; /* SO_REUSEADDR */
915
916 sock->sk->sk_allocation = GFP_NOIO;
917 msock->sk->sk_allocation = GFP_NOIO;
918
919 sock->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
920 msock->sk->sk_priority = TC_PRIO_INTERACTIVE;
921
Philipp Reisnerb411b362009-09-25 16:07:19 -0700922 /* NOT YET ...
Philipp Reisner907599e2011-02-08 11:25:37 +0100923 * sock->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700924 * sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
Andreas Gruenbacher60381782011-03-28 17:05:50 +0200925 * first set it to the P_CONNECTION_FEATURES timeout,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700926 * which we set to 4x the configured ping_timeout. */
927 sock->sk->sk_sndtimeo =
Philipp Reisner907599e2011-02-08 11:25:37 +0100928 sock->sk->sk_rcvtimeo = tconn->net_conf->ping_timeo*4*HZ/10;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700929
Philipp Reisner907599e2011-02-08 11:25:37 +0100930 msock->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
931 msock->sk->sk_rcvtimeo = tconn->net_conf->ping_int*HZ;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700932
933 /* we don't want delays.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300934 * we use TCP_CORK where appropriate, though */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700935 drbd_tcp_nodelay(sock);
936 drbd_tcp_nodelay(msock);
937
Philipp Reisner907599e2011-02-08 11:25:37 +0100938 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700939
Andreas Gruenbacher60381782011-03-28 17:05:50 +0200940 h = drbd_do_features(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700941 if (h <= 0)
942 return h;
943
Philipp Reisner907599e2011-02-08 11:25:37 +0100944 if (tconn->cram_hmac_tfm) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700945 /* drbd_request_state(mdev, NS(conn, WFAuth)); */
Philipp Reisner907599e2011-02-08 11:25:37 +0100946 switch (drbd_do_auth(tconn)) {
Johannes Thomab10d96c2010-01-07 16:02:50 +0100947 case -1:
Philipp Reisner907599e2011-02-08 11:25:37 +0100948 conn_err(tconn, "Authentication of peer failed\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700949 return -1;
Johannes Thomab10d96c2010-01-07 16:02:50 +0100950 case 0:
Philipp Reisner907599e2011-02-08 11:25:37 +0100951 conn_err(tconn, "Authentication of peer failed, trying again.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +0100952 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700953 }
954 }
955
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100956 if (conn_request_state(tconn, NS(conn, C_WF_REPORT_PARAMS), CS_VERBOSE) < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700957 return 0;
958
Philipp Reisner907599e2011-02-08 11:25:37 +0100959 sock->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700960 sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
961
Philipp Reisner907599e2011-02-08 11:25:37 +0100962 drbd_thread_start(&tconn->asender);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700963
Andreas Gruenbacher387eb302011-03-16 01:05:37 +0100964 if (drbd_send_protocol(tconn) == -EOPNOTSUPP)
Philipp Reisner7e2455c2010-04-22 14:50:23 +0200965 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700966
Philipp Reisner907599e2011-02-08 11:25:37 +0100967 return !idr_for_each(&tconn->volumes, drbd_connected, tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700968
969out_release_sockets:
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200970 if (tconn->data.socket) {
971 sock_release(tconn->data.socket);
972 tconn->data.socket = NULL;
973 }
974 if (tconn->meta.socket) {
975 sock_release(tconn->meta.socket);
976 tconn->meta.socket = NULL;
977 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700978 return -1;
979}
980
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200981static int decode_header(struct drbd_tconn *tconn, void *header, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700982{
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200983 unsigned int header_size = drbd_header_size(tconn);
984
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +0200985 if (header_size == sizeof(struct p_header100) &&
986 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC_100)) {
987 struct p_header100 *h = header;
988 if (h->pad != 0) {
989 conn_err(tconn, "Header padding is not zero\n");
990 return -EINVAL;
991 }
992 pi->vnr = be16_to_cpu(h->volume);
993 pi->cmd = be16_to_cpu(h->command);
994 pi->size = be32_to_cpu(h->length);
995 } else if (header_size == sizeof(struct p_header95) &&
996 *(__be16 *)header == cpu_to_be16(DRBD_MAGIC_BIG)) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200997 struct p_header95 *h = header;
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200998 pi->cmd = be16_to_cpu(h->command);
Andreas Gruenbacherb55d84b2011-03-22 13:17:47 +0100999 pi->size = be32_to_cpu(h->length);
1000 pi->vnr = 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001001 } else if (header_size == sizeof(struct p_header80) &&
1002 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC)) {
1003 struct p_header80 *h = header;
1004 pi->cmd = be16_to_cpu(h->command);
1005 pi->size = be16_to_cpu(h->length);
Philipp Reisner77351055b2011-02-07 17:24:26 +01001006 pi->vnr = 0;
Philipp Reisner02918be2010-08-20 14:35:10 +02001007 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001008 conn_err(tconn, "Wrong magic value 0x%08x in protocol version %d\n",
1009 be32_to_cpu(*(__be32 *)header),
1010 tconn->agreed_pro_version);
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001011 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001012 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001013 pi->data = header + header_size;
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001014 return 0;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001015}
1016
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001017static int drbd_recv_header(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner257d0af2011-01-26 12:15:29 +01001018{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001019 void *buffer = tconn->data.rbuf;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001020 int err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001021
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001022 err = drbd_recv_all_warn(tconn, buffer, drbd_header_size(tconn));
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001023 if (err)
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001024 return err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001025
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001026 err = decode_header(tconn, buffer, pi);
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001027 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001028
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001029 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001030}
1031
Philipp Reisner2451fc32010-08-24 13:43:11 +02001032static void drbd_flush(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001033{
1034 int rv;
1035
1036 if (mdev->write_ordering >= WO_bdev_flush && get_ldev(mdev)) {
Dmitry Monakhovfbd9b092010-04-28 17:55:06 +04001037 rv = blkdev_issue_flush(mdev->ldev->backing_bdev, GFP_KERNEL,
Christoph Hellwigdd3932e2010-09-16 20:51:46 +02001038 NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001039 if (rv) {
1040 dev_err(DEV, "local disk flush failed with status %d\n", rv);
1041 /* would rather check on EOPNOTSUPP, but that is not reliable.
1042 * don't try again for ANY return value != 0
1043 * if (rv == -EOPNOTSUPP) */
1044 drbd_bump_write_ordering(mdev, WO_drain_io);
1045 }
1046 put_ldev(mdev);
1047 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001048}
1049
1050/**
1051 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
1052 * @mdev: DRBD device.
1053 * @epoch: Epoch object.
1054 * @ev: Epoch event.
1055 */
1056static enum finish_epoch drbd_may_finish_epoch(struct drbd_conf *mdev,
1057 struct drbd_epoch *epoch,
1058 enum epoch_event ev)
1059{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001060 int epoch_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001061 struct drbd_epoch *next_epoch;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001062 enum finish_epoch rv = FE_STILL_LIVE;
1063
1064 spin_lock(&mdev->epoch_lock);
1065 do {
1066 next_epoch = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001067
1068 epoch_size = atomic_read(&epoch->epoch_size);
1069
1070 switch (ev & ~EV_CLEANUP) {
1071 case EV_PUT:
1072 atomic_dec(&epoch->active);
1073 break;
1074 case EV_GOT_BARRIER_NR:
1075 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001076 break;
1077 case EV_BECAME_LAST:
1078 /* nothing to do*/
1079 break;
1080 }
1081
Philipp Reisnerb411b362009-09-25 16:07:19 -07001082 if (epoch_size != 0 &&
1083 atomic_read(&epoch->active) == 0 &&
Philipp Reisner2451fc32010-08-24 13:43:11 +02001084 test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001085 if (!(ev & EV_CLEANUP)) {
1086 spin_unlock(&mdev->epoch_lock);
1087 drbd_send_b_ack(mdev, epoch->barrier_nr, epoch_size);
1088 spin_lock(&mdev->epoch_lock);
1089 }
1090 dec_unacked(mdev);
1091
1092 if (mdev->current_epoch != epoch) {
1093 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1094 list_del(&epoch->list);
1095 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
1096 mdev->epochs--;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001097 kfree(epoch);
1098
1099 if (rv == FE_STILL_LIVE)
1100 rv = FE_DESTROYED;
1101 } else {
1102 epoch->flags = 0;
1103 atomic_set(&epoch->epoch_size, 0);
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001104 /* atomic_set(&epoch->active, 0); is already zero */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001105 if (rv == FE_STILL_LIVE)
1106 rv = FE_RECYCLED;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001107 wake_up(&mdev->ee_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001108 }
1109 }
1110
1111 if (!next_epoch)
1112 break;
1113
1114 epoch = next_epoch;
1115 } while (1);
1116
1117 spin_unlock(&mdev->epoch_lock);
1118
Philipp Reisnerb411b362009-09-25 16:07:19 -07001119 return rv;
1120}
1121
1122/**
1123 * drbd_bump_write_ordering() - Fall back to an other write ordering method
1124 * @mdev: DRBD device.
1125 * @wo: Write ordering method to try.
1126 */
1127void drbd_bump_write_ordering(struct drbd_conf *mdev, enum write_ordering_e wo) __must_hold(local)
1128{
1129 enum write_ordering_e pwo;
1130 static char *write_ordering_str[] = {
1131 [WO_none] = "none",
1132 [WO_drain_io] = "drain",
1133 [WO_bdev_flush] = "flush",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001134 };
1135
1136 pwo = mdev->write_ordering;
1137 wo = min(pwo, wo);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001138 if (wo == WO_bdev_flush && mdev->ldev->dc.no_disk_flush)
1139 wo = WO_drain_io;
1140 if (wo == WO_drain_io && mdev->ldev->dc.no_disk_drain)
1141 wo = WO_none;
1142 mdev->write_ordering = wo;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001143 if (pwo != mdev->write_ordering || wo == WO_bdev_flush)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001144 dev_info(DEV, "Method to ensure write ordering: %s\n", write_ordering_str[mdev->write_ordering]);
1145}
1146
1147/**
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001148 * drbd_submit_peer_request()
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001149 * @mdev: DRBD device.
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001150 * @peer_req: peer request
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001151 * @rw: flag field, see bio->bi_rw
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001152 *
1153 * May spread the pages to multiple bios,
1154 * depending on bio_add_page restrictions.
1155 *
1156 * Returns 0 if all bios have been submitted,
1157 * -ENOMEM if we could not allocate enough bios,
1158 * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a
1159 * single page to an empty bio (which should never happen and likely indicates
1160 * that the lower level IO stack is in some way broken). This has been observed
1161 * on certain Xen deployments.
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001162 */
1163/* TODO allocate from our own bio_set. */
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001164int drbd_submit_peer_request(struct drbd_conf *mdev,
1165 struct drbd_peer_request *peer_req,
1166 const unsigned rw, const int fault_type)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001167{
1168 struct bio *bios = NULL;
1169 struct bio *bio;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001170 struct page *page = peer_req->pages;
1171 sector_t sector = peer_req->i.sector;
1172 unsigned ds = peer_req->i.size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001173 unsigned n_bios = 0;
1174 unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001175 int err = -ENOMEM;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001176
1177 /* In most cases, we will only need one bio. But in case the lower
1178 * level restrictions happen to be different at this offset on this
1179 * side than those of the sending peer, we may need to submit the
Lars Ellenbergda4a75d2011-02-23 17:02:01 +01001180 * request in more than one bio.
1181 *
1182 * Plain bio_alloc is good enough here, this is no DRBD internally
1183 * generated bio, but a bio allocated on behalf of the peer.
1184 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001185next_bio:
1186 bio = bio_alloc(GFP_NOIO, nr_pages);
1187 if (!bio) {
1188 dev_err(DEV, "submit_ee: Allocation of a bio failed\n");
1189 goto fail;
1190 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001191 /* > peer_req->i.sector, unless this is the first bio */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001192 bio->bi_sector = sector;
1193 bio->bi_bdev = mdev->ldev->backing_bdev;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001194 bio->bi_rw = rw;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001195 bio->bi_private = peer_req;
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001196 bio->bi_end_io = drbd_peer_request_endio;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001197
1198 bio->bi_next = bios;
1199 bios = bio;
1200 ++n_bios;
1201
1202 page_chain_for_each(page) {
1203 unsigned len = min_t(unsigned, ds, PAGE_SIZE);
1204 if (!bio_add_page(bio, page, len, 0)) {
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001205 /* A single page must always be possible!
1206 * But in case it fails anyways,
1207 * we deal with it, and complain (below). */
1208 if (bio->bi_vcnt == 0) {
1209 dev_err(DEV,
1210 "bio_add_page failed for len=%u, "
1211 "bi_vcnt=0 (bi_sector=%llu)\n",
1212 len, (unsigned long long)bio->bi_sector);
1213 err = -ENOSPC;
1214 goto fail;
1215 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001216 goto next_bio;
1217 }
1218 ds -= len;
1219 sector += len >> 9;
1220 --nr_pages;
1221 }
1222 D_ASSERT(page == NULL);
1223 D_ASSERT(ds == 0);
1224
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001225 atomic_set(&peer_req->pending_bios, n_bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001226 do {
1227 bio = bios;
1228 bios = bios->bi_next;
1229 bio->bi_next = NULL;
1230
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001231 drbd_generic_make_request(mdev, fault_type, bio);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001232 } while (bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001233 return 0;
1234
1235fail:
1236 while (bios) {
1237 bio = bios;
1238 bios = bios->bi_next;
1239 bio_put(bio);
1240 }
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001241 return err;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001242}
1243
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001244static void drbd_remove_epoch_entry_interval(struct drbd_conf *mdev,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001245 struct drbd_peer_request *peer_req)
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001246{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001247 struct drbd_interval *i = &peer_req->i;
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001248
1249 drbd_remove_interval(&mdev->write_requests, i);
1250 drbd_clear_interval(i);
1251
Andreas Gruenbacher6c852be2011-02-04 15:38:52 +01001252 /* Wake up any processes waiting for this peer request to complete. */
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001253 if (i->waiting)
1254 wake_up(&mdev->misc_wait);
1255}
1256
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001257static int receive_Barrier(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001258{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001259 struct drbd_conf *mdev;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001260 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001261 struct p_barrier *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001262 struct drbd_epoch *epoch;
1263
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001264 mdev = vnr_to_mdev(tconn, pi->vnr);
1265 if (!mdev)
1266 return -EIO;
1267
Philipp Reisnerb411b362009-09-25 16:07:19 -07001268 inc_unacked(mdev);
1269
Philipp Reisnerb411b362009-09-25 16:07:19 -07001270 mdev->current_epoch->barrier_nr = p->barrier;
1271 rv = drbd_may_finish_epoch(mdev, mdev->current_epoch, EV_GOT_BARRIER_NR);
1272
1273 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
1274 * the activity log, which means it would not be resynced in case the
1275 * R_PRIMARY crashes now.
1276 * Therefore we must send the barrier_ack after the barrier request was
1277 * completed. */
1278 switch (mdev->write_ordering) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001279 case WO_none:
1280 if (rv == FE_RECYCLED)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001281 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001282
1283 /* receiver context, in the writeout path of the other node.
1284 * avoid potential distributed deadlock */
1285 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1286 if (epoch)
1287 break;
1288 else
1289 dev_warn(DEV, "Allocation of an epoch failed, slowing down\n");
1290 /* Fall through */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001291
1292 case WO_bdev_flush:
1293 case WO_drain_io:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001294 drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
Philipp Reisner2451fc32010-08-24 13:43:11 +02001295 drbd_flush(mdev);
1296
1297 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1298 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1299 if (epoch)
1300 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001301 }
1302
Philipp Reisner2451fc32010-08-24 13:43:11 +02001303 epoch = mdev->current_epoch;
1304 wait_event(mdev->ee_wait, atomic_read(&epoch->epoch_size) == 0);
1305
1306 D_ASSERT(atomic_read(&epoch->active) == 0);
1307 D_ASSERT(epoch->flags == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001308
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001309 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001310 default:
1311 dev_err(DEV, "Strangeness in mdev->write_ordering %d\n", mdev->write_ordering);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001312 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001313 }
1314
1315 epoch->flags = 0;
1316 atomic_set(&epoch->epoch_size, 0);
1317 atomic_set(&epoch->active, 0);
1318
1319 spin_lock(&mdev->epoch_lock);
1320 if (atomic_read(&mdev->current_epoch->epoch_size)) {
1321 list_add(&epoch->list, &mdev->current_epoch->list);
1322 mdev->current_epoch = epoch;
1323 mdev->epochs++;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001324 } else {
1325 /* The current_epoch got recycled while we allocated this one... */
1326 kfree(epoch);
1327 }
1328 spin_unlock(&mdev->epoch_lock);
1329
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001330 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001331}
1332
1333/* used from receive_RSDataReply (recv_resync_read)
1334 * and from receive_Data */
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001335static struct drbd_peer_request *
1336read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector,
1337 int data_size) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001338{
Lars Ellenberg66660322010-04-06 12:15:04 +02001339 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001340 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001341 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001342 int dgs, ds, err;
Philipp Reisnera0638452011-01-19 14:31:32 +01001343 void *dig_in = mdev->tconn->int_dig_in;
1344 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001345 unsigned long *data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001346
Philipp Reisnera0638452011-01-19 14:31:32 +01001347 dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_r_tfm) ?
1348 crypto_hash_digestsize(mdev->tconn->integrity_r_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001349
1350 if (dgs) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001351 /*
1352 * FIXME: Receive the incoming digest into the receive buffer
1353 * here, together with its struct p_data?
1354 */
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001355 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1356 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001357 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001358 }
1359
1360 data_size -= dgs;
1361
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001362 if (!expect(data_size != 0))
1363 return NULL;
1364 if (!expect(IS_ALIGNED(data_size, 512)))
1365 return NULL;
1366 if (!expect(data_size <= DRBD_MAX_BIO_SIZE))
1367 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001368
Lars Ellenberg66660322010-04-06 12:15:04 +02001369 /* even though we trust out peer,
1370 * we sometimes have to double check. */
1371 if (sector + (data_size>>9) > capacity) {
Lars Ellenbergfdda6542011-01-24 15:11:01 +01001372 dev_err(DEV, "request from peer beyond end of local disk: "
1373 "capacity: %llus < sector: %llus + size: %u\n",
Lars Ellenberg66660322010-04-06 12:15:04 +02001374 (unsigned long long)capacity,
1375 (unsigned long long)sector, data_size);
1376 return NULL;
1377 }
1378
Philipp Reisnerb411b362009-09-25 16:07:19 -07001379 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1380 * "criss-cross" setup, that might cause write-out on some other DRBD,
1381 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02001382 peer_req = drbd_alloc_peer_req(mdev, id, sector, data_size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001383 if (!peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001384 return NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001385
Philipp Reisnerb411b362009-09-25 16:07:19 -07001386 ds = data_size;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001387 page = peer_req->pages;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001388 page_chain_for_each(page) {
1389 unsigned len = min_t(int, ds, PAGE_SIZE);
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001390 data = kmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001391 err = drbd_recv_all_warn(mdev->tconn, data, len);
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +01001392 if (drbd_insert_fault(mdev, DRBD_FAULT_RECEIVE)) {
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001393 dev_err(DEV, "Fault injection: Corrupting data on receive\n");
1394 data[0] = data[0] ^ (unsigned long)-1;
1395 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001396 kunmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001397 if (err) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001398 drbd_free_ee(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001399 return NULL;
1400 }
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001401 ds -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001402 }
1403
1404 if (dgs) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001405 drbd_csum_ee(mdev, mdev->tconn->integrity_r_tfm, peer_req, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001406 if (memcmp(dig_in, dig_vv, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001407 dev_err(DEV, "Digest integrity check FAILED: %llus +%u\n",
1408 (unsigned long long)sector, data_size);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001409 drbd_free_ee(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001410 return NULL;
1411 }
1412 }
1413 mdev->recv_cnt += data_size>>9;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001414 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001415}
1416
1417/* drbd_drain_block() just takes a data block
1418 * out of the socket input buffer, and discards it.
1419 */
1420static int drbd_drain_block(struct drbd_conf *mdev, int data_size)
1421{
1422 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001423 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001424 void *data;
1425
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001426 if (!data_size)
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001427 return 0;
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001428
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001429 page = drbd_pp_alloc(mdev, 1, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001430
1431 data = kmap(page);
1432 while (data_size) {
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001433 unsigned int len = min_t(int, data_size, PAGE_SIZE);
1434
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001435 err = drbd_recv_all_warn(mdev->tconn, data, len);
1436 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001437 break;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001438 data_size -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001439 }
1440 kunmap(page);
Lars Ellenberg435f0742010-09-06 12:30:25 +02001441 drbd_pp_free(mdev, page, 0);
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001442 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001443}
1444
1445static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req,
1446 sector_t sector, int data_size)
1447{
1448 struct bio_vec *bvec;
1449 struct bio *bio;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001450 int dgs, err, i, expect;
Philipp Reisnera0638452011-01-19 14:31:32 +01001451 void *dig_in = mdev->tconn->int_dig_in;
1452 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001453
Philipp Reisnera0638452011-01-19 14:31:32 +01001454 dgs = (mdev->tconn->agreed_pro_version >= 87 && mdev->tconn->integrity_r_tfm) ?
1455 crypto_hash_digestsize(mdev->tconn->integrity_r_tfm) : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001456
1457 if (dgs) {
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001458 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1459 if (err)
1460 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001461 }
1462
1463 data_size -= dgs;
1464
1465 /* optimistically update recv_cnt. if receiving fails below,
1466 * we disconnect anyways, and counters will be reset. */
1467 mdev->recv_cnt += data_size>>9;
1468
1469 bio = req->master_bio;
1470 D_ASSERT(sector == bio->bi_sector);
1471
1472 bio_for_each_segment(bvec, bio, i) {
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001473 void *mapped = kmap(bvec->bv_page) + bvec->bv_offset;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001474 expect = min_t(int, data_size, bvec->bv_len);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001475 err = drbd_recv_all_warn(mdev->tconn, mapped, expect);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001476 kunmap(bvec->bv_page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001477 if (err)
1478 return err;
1479 data_size -= expect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001480 }
1481
1482 if (dgs) {
Philipp Reisnera0638452011-01-19 14:31:32 +01001483 drbd_csum_bio(mdev, mdev->tconn->integrity_r_tfm, bio, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001484 if (memcmp(dig_in, dig_vv, dgs)) {
1485 dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n");
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001486 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001487 }
1488 }
1489
1490 D_ASSERT(data_size == 0);
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001491 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001492}
1493
1494/* e_end_resync_block() is called via
1495 * drbd_process_done_ee() by asender only */
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 Gruenbacherdb830c42011-02-04 15:57:48 +01001550 drbd_free_ee(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
Philipp Reisnerb411b362009-09-25 16:07:19 -07001684/* e_end_block() is called via drbd_process_done_ee().
1685 * this means this function only runs in the asender thread
1686 */
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 Gruenbacherdb830c42011-02-04 15:57:48 +01002112 drbd_free_ee(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 Gruenbacherdb830c42011-02-04 15:57:48 +01002367 drbd_free_ee(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 Reisner7204624c2011-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 Reisner7204624c2011-03-15 18:51:47 +01002928 set_bit(CONN_DRY_RUN, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002929
Philipp Reisner7204624c2011-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 Reisner7204624c2011-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 Reisner7204624c2011-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 Reisner7204624c2011-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 Reisner7204624c2011-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 Reisner7204624c2011-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 Reisner7204624c2011-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 Reisner7204624c2011-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 Reisner7204624c2011-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 Reisner7204624c2011-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 Reisner8410da8f02011-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 Reisner8410da8f02011-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
4149 /* This also does reclaim_net_ee(). If we do this too early, we might
4150 * miss some resync ee and pages.*/
4151 drbd_process_done_ee(mdev);
4152
4153 kfree(mdev->p_uuid);
4154 mdev->p_uuid = NULL;
4155
Philipp Reisner2aebfab2011-03-28 16:48:11 +02004156 if (!drbd_suspended(mdev))
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01004157 tl_clear(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004158
Philipp Reisnerb411b362009-09-25 16:07:19 -07004159 drbd_md_sync(mdev);
4160
4161 fp = FP_DONT_CARE;
4162 if (get_ldev(mdev)) {
4163 fp = mdev->ldev->dc.fencing;
4164 put_ldev(mdev);
4165 }
4166
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004167 /* serialize with bitmap writeout triggered by the state change,
4168 * if any. */
4169 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
4170
Philipp Reisnerb411b362009-09-25 16:07:19 -07004171 /* tcp_close and release of sendpage pages can be deferred. I don't
4172 * want to use SO_LINGER, because apparently it can be deferred for
4173 * more than 20 seconds (longest time I checked).
4174 *
4175 * Actually we don't care for exactly when the network stack does its
4176 * put_page(), but release our reference on these pages right here.
4177 */
4178 i = drbd_release_ee(mdev, &mdev->net_ee);
4179 if (i)
4180 dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
Lars Ellenberg435f0742010-09-06 12:30:25 +02004181 i = atomic_read(&mdev->pp_in_use_by_net);
4182 if (i)
4183 dev_info(DEV, "pp_in_use_by_net = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004184 i = atomic_read(&mdev->pp_in_use);
4185 if (i)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02004186 dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004187
4188 D_ASSERT(list_empty(&mdev->read_ee));
4189 D_ASSERT(list_empty(&mdev->active_ee));
4190 D_ASSERT(list_empty(&mdev->sync_ee));
4191 D_ASSERT(list_empty(&mdev->done_ee));
4192
4193 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
4194 atomic_set(&mdev->current_epoch->epoch_size, 0);
4195 D_ASSERT(list_empty(&mdev->current_epoch->list));
Philipp Reisner360cc742011-02-08 14:29:53 +01004196
4197 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004198}
4199
4200/*
4201 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
4202 * we can agree on is stored in agreed_pro_version.
4203 *
4204 * feature flags and the reserved array should be enough room for future
4205 * enhancements of the handshake protocol, and possible plugins...
4206 *
4207 * for now, they are expected to be zero, but ignored.
4208 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004209static int drbd_send_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004210{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004211 struct drbd_socket *sock;
4212 struct p_connection_features *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004213
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004214 sock = &tconn->data;
4215 p = conn_prepare_command(tconn, sock);
4216 if (!p)
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004217 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004218 memset(p, 0, sizeof(*p));
4219 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
4220 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004221 return conn_send_command(tconn, sock, P_CONNECTION_FEATURES, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004222}
4223
4224/*
4225 * return values:
4226 * 1 yes, we have a valid connection
4227 * 0 oops, did not work out, please try again
4228 * -1 peer talks different language,
4229 * no point in trying again, please go standalone.
4230 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004231static int drbd_do_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004232{
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004233 /* ASSERT current == tconn->receiver ... */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004234 struct p_connection_features *p;
4235 const int expect = sizeof(struct p_connection_features);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004236 struct packet_info pi;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004237 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004238
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004239 err = drbd_send_features(tconn);
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004240 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004241 return 0;
4242
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004243 err = drbd_recv_header(tconn, &pi);
4244 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004245 return 0;
4246
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004247 if (pi.cmd != P_CONNECTION_FEATURES) {
4248 conn_err(tconn, "expected ConnectionFeatures packet, received: %s (0x%04x)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004249 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004250 return -1;
4251 }
4252
Philipp Reisner77351055b2011-02-07 17:24:26 +01004253 if (pi.size != expect) {
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004254 conn_err(tconn, "expected ConnectionFeatures length: %u, received: %u\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004255 expect, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004256 return -1;
4257 }
4258
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004259 p = pi.data;
4260 err = drbd_recv_all_warn(tconn, p, expect);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004261 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004262 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004263
Philipp Reisnerb411b362009-09-25 16:07:19 -07004264 p->protocol_min = be32_to_cpu(p->protocol_min);
4265 p->protocol_max = be32_to_cpu(p->protocol_max);
4266 if (p->protocol_max == 0)
4267 p->protocol_max = p->protocol_min;
4268
4269 if (PRO_VERSION_MAX < p->protocol_min ||
4270 PRO_VERSION_MIN > p->protocol_max)
4271 goto incompat;
4272
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004273 tconn->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004274
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004275 conn_info(tconn, "Handshake successful: "
4276 "Agreed network protocol version %d\n", tconn->agreed_pro_version);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004277
4278 return 1;
4279
4280 incompat:
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004281 conn_err(tconn, "incompatible DRBD dialects: "
Philipp Reisnerb411b362009-09-25 16:07:19 -07004282 "I support %d-%d, peer supports %d-%d\n",
4283 PRO_VERSION_MIN, PRO_VERSION_MAX,
4284 p->protocol_min, p->protocol_max);
4285 return -1;
4286}
4287
4288#if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
Philipp Reisner13e60372011-02-08 09:54:40 +01004289static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004290{
4291 dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
4292 dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004293 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004294}
4295#else
4296#define CHALLENGE_LEN 64
Johannes Thomab10d96c2010-01-07 16:02:50 +01004297
4298/* Return value:
4299 1 - auth succeeded,
4300 0 - failed, try again (network error),
4301 -1 - auth failed, don't try again.
4302*/
4303
Philipp Reisner13e60372011-02-08 09:54:40 +01004304static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004305{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004306 struct drbd_socket *sock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004307 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
4308 struct scatterlist sg;
4309 char *response = NULL;
4310 char *right_response = NULL;
4311 char *peers_ch = NULL;
Philipp Reisner13e60372011-02-08 09:54:40 +01004312 unsigned int key_len = strlen(tconn->net_conf->shared_secret);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004313 unsigned int resp_size;
4314 struct hash_desc desc;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004315 struct packet_info pi;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004316 int err, rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004317
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004318 /* FIXME: Put the challenge/response into the preallocated socket buffer. */
4319
Philipp Reisner13e60372011-02-08 09:54:40 +01004320 desc.tfm = tconn->cram_hmac_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004321 desc.flags = 0;
4322
Philipp Reisner13e60372011-02-08 09:54:40 +01004323 rv = crypto_hash_setkey(tconn->cram_hmac_tfm,
4324 (u8 *)tconn->net_conf->shared_secret, key_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004325 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004326 conn_err(tconn, "crypto_hash_setkey() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004327 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004328 goto fail;
4329 }
4330
4331 get_random_bytes(my_challenge, CHALLENGE_LEN);
4332
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004333 sock = &tconn->data;
4334 if (!conn_prepare_command(tconn, sock)) {
4335 rv = 0;
4336 goto fail;
4337 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004338 rv = !conn_send_command(tconn, sock, P_AUTH_CHALLENGE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004339 my_challenge, CHALLENGE_LEN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004340 if (!rv)
4341 goto fail;
4342
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004343 err = drbd_recv_header(tconn, &pi);
4344 if (err) {
4345 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004346 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004347 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004348
Philipp Reisner77351055b2011-02-07 17:24:26 +01004349 if (pi.cmd != P_AUTH_CHALLENGE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004350 conn_err(tconn, "expected AuthChallenge packet, received: %s (0x%04x)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004351 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004352 rv = 0;
4353 goto fail;
4354 }
4355
Philipp Reisner77351055b2011-02-07 17:24:26 +01004356 if (pi.size > CHALLENGE_LEN * 2) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004357 conn_err(tconn, "expected AuthChallenge payload too big.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004358 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004359 goto fail;
4360 }
4361
Philipp Reisner77351055b2011-02-07 17:24:26 +01004362 peers_ch = kmalloc(pi.size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004363 if (peers_ch == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004364 conn_err(tconn, "kmalloc of peers_ch failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004365 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004366 goto fail;
4367 }
4368
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004369 err = drbd_recv_all_warn(tconn, peers_ch, pi.size);
4370 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004371 rv = 0;
4372 goto fail;
4373 }
4374
Philipp Reisner13e60372011-02-08 09:54:40 +01004375 resp_size = crypto_hash_digestsize(tconn->cram_hmac_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004376 response = kmalloc(resp_size, GFP_NOIO);
4377 if (response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004378 conn_err(tconn, "kmalloc of response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004379 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004380 goto fail;
4381 }
4382
4383 sg_init_table(&sg, 1);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004384 sg_set_buf(&sg, peers_ch, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004385
4386 rv = crypto_hash_digest(&desc, &sg, sg.length, response);
4387 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004388 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004389 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004390 goto fail;
4391 }
4392
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004393 if (!conn_prepare_command(tconn, sock)) {
4394 rv = 0;
4395 goto fail;
4396 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004397 rv = !conn_send_command(tconn, sock, P_AUTH_RESPONSE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004398 response, resp_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004399 if (!rv)
4400 goto fail;
4401
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004402 err = drbd_recv_header(tconn, &pi);
4403 if (err) {
4404 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004405 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004406 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004407
Philipp Reisner77351055b2011-02-07 17:24:26 +01004408 if (pi.cmd != P_AUTH_RESPONSE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004409 conn_err(tconn, "expected AuthResponse packet, received: %s (0x%04x)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004410 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004411 rv = 0;
4412 goto fail;
4413 }
4414
Philipp Reisner77351055b2011-02-07 17:24:26 +01004415 if (pi.size != resp_size) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004416 conn_err(tconn, "expected AuthResponse payload of wrong size\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004417 rv = 0;
4418 goto fail;
4419 }
4420
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004421 err = drbd_recv_all_warn(tconn, response , resp_size);
4422 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004423 rv = 0;
4424 goto fail;
4425 }
4426
4427 right_response = kmalloc(resp_size, GFP_NOIO);
Julia Lawall2d1ee872009-12-27 22:27:11 +01004428 if (right_response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004429 conn_err(tconn, "kmalloc of right_response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004430 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004431 goto fail;
4432 }
4433
4434 sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
4435
4436 rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
4437 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004438 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004439 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004440 goto fail;
4441 }
4442
4443 rv = !memcmp(response, right_response, resp_size);
4444
4445 if (rv)
Philipp Reisner13e60372011-02-08 09:54:40 +01004446 conn_info(tconn, "Peer authenticated using %d bytes of '%s' HMAC\n",
4447 resp_size, tconn->net_conf->cram_hmac_alg);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004448 else
4449 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004450
4451 fail:
4452 kfree(peers_ch);
4453 kfree(response);
4454 kfree(right_response);
4455
4456 return rv;
4457}
4458#endif
4459
4460int drbdd_init(struct drbd_thread *thi)
4461{
Philipp Reisner392c8802011-02-09 10:33:31 +01004462 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004463 int h;
4464
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004465 conn_info(tconn, "receiver (re)started\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004466
4467 do {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004468 h = drbd_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004469 if (h == 0) {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004470 drbd_disconnect(tconn);
Philipp Reisner20ee6392011-01-18 15:28:59 +01004471 schedule_timeout_interruptible(HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004472 }
4473 if (h == -1) {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004474 conn_warn(tconn, "Discarding network configuration.\n");
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004475 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004476 }
4477 } while (h == 0);
4478
4479 if (h > 0) {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004480 if (get_net_conf(tconn)) {
4481 drbdd(tconn);
4482 put_net_conf(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004483 }
4484 }
4485
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004486 drbd_disconnect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004487
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004488 conn_info(tconn, "receiver terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004489 return 0;
4490}
4491
4492/* ********* acknowledge sender ******** */
4493
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004494static int got_conn_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004495{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004496 struct p_req_state_reply *p = pi->data;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004497 int retcode = be32_to_cpu(p->retcode);
4498
4499 if (retcode >= SS_SUCCESS) {
4500 set_bit(CONN_WD_ST_CHG_OKAY, &tconn->flags);
4501 } else {
4502 set_bit(CONN_WD_ST_CHG_FAIL, &tconn->flags);
4503 conn_err(tconn, "Requested state change failed by peer: %s (%d)\n",
4504 drbd_set_st_err_str(retcode), retcode);
4505 }
4506 wake_up(&tconn->ping_wait);
4507
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004508 return 0;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004509}
4510
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004511static int got_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004512{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004513 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004514 struct p_req_state_reply *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004515 int retcode = be32_to_cpu(p->retcode);
4516
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004517 mdev = vnr_to_mdev(tconn, pi->vnr);
4518 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004519 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004520
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004521 if (retcode >= SS_SUCCESS) {
4522 set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
4523 } else {
4524 set_bit(CL_ST_CHG_FAIL, &mdev->flags);
4525 dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
4526 drbd_set_st_err_str(retcode), retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004527 }
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004528 wake_up(&mdev->state_wait);
4529
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004530 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004531}
4532
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004533static int got_Ping(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004534{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004535 return drbd_send_ping_ack(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004536
4537}
4538
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004539static int got_PingAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004540{
4541 /* restore idle timeout */
Philipp Reisner2a67d8b2011-02-09 14:10:32 +01004542 tconn->meta.socket->sk->sk_rcvtimeo = tconn->net_conf->ping_int*HZ;
4543 if (!test_and_set_bit(GOT_PING_ACK, &tconn->flags))
4544 wake_up(&tconn->ping_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004545
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004546 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004547}
4548
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004549static int got_IsInSync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004550{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004551 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004552 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004553 sector_t sector = be64_to_cpu(p->sector);
4554 int blksize = be32_to_cpu(p->blksize);
4555
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004556 mdev = vnr_to_mdev(tconn, pi->vnr);
4557 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004558 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004559
Philipp Reisner31890f42011-01-19 14:12:51 +01004560 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004561
4562 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4563
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004564 if (get_ldev(mdev)) {
4565 drbd_rs_complete_io(mdev, sector);
4566 drbd_set_in_sync(mdev, sector, blksize);
4567 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
4568 mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
4569 put_ldev(mdev);
4570 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004571 dec_rs_pending(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02004572 atomic_add(blksize >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004573
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004574 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004575}
4576
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004577static int
4578validate_req_change_req_state(struct drbd_conf *mdev, u64 id, sector_t sector,
4579 struct rb_root *root, const char *func,
4580 enum drbd_req_event what, bool missing_ok)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004581{
4582 struct drbd_request *req;
4583 struct bio_and_error m;
4584
Philipp Reisner87eeee42011-01-19 14:16:30 +01004585 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004586 req = find_request(mdev, root, id, sector, missing_ok, func);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004587 if (unlikely(!req)) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01004588 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004589 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004590 }
4591 __req_mod(req, what, &m);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004592 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004593
4594 if (m.bio)
4595 complete_master_bio(mdev, &m);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004596 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004597}
4598
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004599static int got_BlockAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004600{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004601 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004602 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004603 sector_t sector = be64_to_cpu(p->sector);
4604 int blksize = be32_to_cpu(p->blksize);
4605 enum drbd_req_event what;
4606
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004607 mdev = vnr_to_mdev(tconn, pi->vnr);
4608 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004609 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004610
Philipp Reisnerb411b362009-09-25 16:07:19 -07004611 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4612
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004613 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004614 drbd_set_in_sync(mdev, sector, blksize);
4615 dec_rs_pending(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004616 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004617 }
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004618 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004619 case P_RS_WRITE_ACK:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004620 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004621 what = WRITE_ACKED_BY_PEER_AND_SIS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004622 break;
4623 case P_WRITE_ACK:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004624 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004625 what = WRITE_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004626 break;
4627 case P_RECV_ACK:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004628 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_B);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004629 what = RECV_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004630 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004631 case P_DISCARD_WRITE:
Philipp Reisner89e58e72011-01-19 13:12:45 +01004632 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004633 what = DISCARD_WRITE;
4634 break;
4635 case P_RETRY_WRITE:
4636 D_ASSERT(mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C);
4637 what = POSTPONE_WRITE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004638 break;
4639 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004640 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07004641 }
4642
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004643 return validate_req_change_req_state(mdev, p->block_id, sector,
4644 &mdev->write_requests, __func__,
4645 what, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004646}
4647
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004648static int got_NegAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004649{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004650 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004651 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004652 sector_t sector = be64_to_cpu(p->sector);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004653 int size = be32_to_cpu(p->blksize);
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004654 bool missing_ok = tconn->net_conf->wire_protocol == DRBD_PROT_A ||
4655 tconn->net_conf->wire_protocol == DRBD_PROT_B;
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004656 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004657
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004658 mdev = vnr_to_mdev(tconn, pi->vnr);
4659 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004660 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004661
Philipp Reisnerb411b362009-09-25 16:07:19 -07004662 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4663
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004664 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004665 dec_rs_pending(mdev);
4666 drbd_rs_failed_io(mdev, sector, size);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004667 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004668 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01004669
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004670 err = validate_req_change_req_state(mdev, p->block_id, sector,
4671 &mdev->write_requests, __func__,
4672 NEG_ACKED, missing_ok);
4673 if (err) {
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004674 /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs.
4675 The master bio might already be completed, therefore the
4676 request is no longer in the collision hash. */
4677 /* In Protocol B we might already have got a P_RECV_ACK
4678 but then get a P_NEG_ACK afterwards. */
4679 if (!missing_ok)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004680 return err;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004681 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004682 }
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004683 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004684}
4685
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004686static int got_NegDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004687{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004688 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004689 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004690 sector_t sector = be64_to_cpu(p->sector);
4691
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004692 mdev = vnr_to_mdev(tconn, pi->vnr);
4693 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004694 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004695
Philipp Reisnerb411b362009-09-25 16:07:19 -07004696 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004697
Philipp Reisnerb411b362009-09-25 16:07:19 -07004698 dev_err(DEV, "Got NegDReply; Sector %llus, len %u; Fail original request.\n",
4699 (unsigned long long)sector, be32_to_cpu(p->blksize));
4700
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004701 return validate_req_change_req_state(mdev, p->block_id, sector,
4702 &mdev->read_requests, __func__,
4703 NEG_ACKED, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004704}
4705
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004706static int got_NegRSDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004707{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004708 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004709 sector_t sector;
4710 int size;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004711 struct p_block_ack *p = pi->data;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004712
4713 mdev = vnr_to_mdev(tconn, pi->vnr);
4714 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004715 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004716
4717 sector = be64_to_cpu(p->sector);
4718 size = be32_to_cpu(p->blksize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004719
4720 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4721
4722 dec_rs_pending(mdev);
4723
4724 if (get_ldev_if_state(mdev, D_FAILED)) {
4725 drbd_rs_complete_io(mdev, sector);
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004726 switch (pi->cmd) {
Philipp Reisnerd612d302010-12-27 10:53:28 +01004727 case P_NEG_RS_DREPLY:
4728 drbd_rs_failed_io(mdev, sector, size);
4729 case P_RS_CANCEL:
4730 break;
4731 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004732 BUG();
Philipp Reisnerd612d302010-12-27 10:53:28 +01004733 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004734 put_ldev(mdev);
4735 }
4736
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004737 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004738}
4739
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004740static int got_BarrierAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004741{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004742 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004743 struct p_barrier_ack *p = pi->data;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004744
4745 mdev = vnr_to_mdev(tconn, pi->vnr);
4746 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004747 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004748
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01004749 tl_release(mdev->tconn, p->barrier, be32_to_cpu(p->set_size));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004750
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02004751 if (mdev->state.conn == C_AHEAD &&
4752 atomic_read(&mdev->ap_in_flight) == 0 &&
Philipp Reisner370a43e2011-01-14 16:03:11 +01004753 !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->current_epoch->flags)) {
4754 mdev->start_resync_timer.expires = jiffies + HZ;
4755 add_timer(&mdev->start_resync_timer);
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02004756 }
4757
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004758 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004759}
4760
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004761static int got_OVResult(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004762{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004763 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004764 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004765 struct drbd_work *w;
4766 sector_t sector;
4767 int size;
4768
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004769 mdev = vnr_to_mdev(tconn, pi->vnr);
4770 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004771 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004772
Philipp Reisnerb411b362009-09-25 16:07:19 -07004773 sector = be64_to_cpu(p->sector);
4774 size = be32_to_cpu(p->blksize);
4775
4776 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4777
4778 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01004779 drbd_ov_out_of_sync_found(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004780 else
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01004781 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004782
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004783 if (!get_ldev(mdev))
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004784 return 0;
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004785
Philipp Reisnerb411b362009-09-25 16:07:19 -07004786 drbd_rs_complete_io(mdev, sector);
4787 dec_rs_pending(mdev);
4788
Lars Ellenbergea5442a2010-11-05 09:48:01 +01004789 --mdev->ov_left;
4790
4791 /* let's advance progress step marks only for every other megabyte */
4792 if ((mdev->ov_left & 0x200) == 0x200)
4793 drbd_advance_rs_marks(mdev, mdev->ov_left);
4794
4795 if (mdev->ov_left == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004796 w = kmalloc(sizeof(*w), GFP_NOIO);
4797 if (w) {
4798 w->cb = w_ov_finished;
Philipp Reisnera21e9292011-02-08 15:08:49 +01004799 w->mdev = mdev;
Philipp Reisnere42325a2011-01-19 13:55:45 +01004800 drbd_queue_work_front(&mdev->tconn->data.work, w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004801 } else {
4802 dev_err(DEV, "kmalloc(w) failed.");
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01004803 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004804 drbd_resync_finished(mdev);
4805 }
4806 }
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004807 put_ldev(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004808 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004809}
4810
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004811static int got_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner0ced55a2010-04-30 15:26:20 +02004812{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004813 return 0;
Philipp Reisner0ced55a2010-04-30 15:26:20 +02004814}
4815
Philipp Reisner32862ec2011-02-08 16:41:01 +01004816static int tconn_process_done_ee(struct drbd_tconn *tconn)
4817{
Philipp Reisner082a3432011-03-15 16:05:42 +01004818 struct drbd_conf *mdev;
4819 int i, not_empty = 0;
Philipp Reisner32862ec2011-02-08 16:41:01 +01004820
4821 do {
4822 clear_bit(SIGNAL_ASENDER, &tconn->flags);
4823 flush_signals(current);
Philipp Reisner082a3432011-03-15 16:05:42 +01004824 idr_for_each_entry(&tconn->volumes, mdev, i) {
Andreas Gruenbachere2b30322011-03-16 17:16:12 +01004825 if (drbd_process_done_ee(mdev))
Philipp Reisner082a3432011-03-15 16:05:42 +01004826 return 1; /* error */
4827 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01004828 set_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisner082a3432011-03-15 16:05:42 +01004829
4830 spin_lock_irq(&tconn->req_lock);
4831 idr_for_each_entry(&tconn->volumes, mdev, i) {
4832 not_empty = !list_empty(&mdev->done_ee);
4833 if (not_empty)
4834 break;
4835 }
4836 spin_unlock_irq(&tconn->req_lock);
Philipp Reisner32862ec2011-02-08 16:41:01 +01004837 } while (not_empty);
4838
4839 return 0;
4840}
4841
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004842struct asender_cmd {
4843 size_t pkt_size;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004844 int (*fn)(struct drbd_tconn *tconn, struct packet_info *);
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004845};
4846
4847static struct asender_cmd asender_tbl[] = {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004848 [P_PING] = { 0, got_Ping },
4849 [P_PING_ACK] = { 0, got_PingAck },
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004850 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4851 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4852 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4853 [P_DISCARD_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
4854 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
4855 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
4856 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply },
4857 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
4858 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
4859 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
4860 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
4861 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
4862 [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply },
4863 [P_CONN_ST_CHG_REPLY]={ sizeof(struct p_req_state_reply), got_conn_RqSReply },
4864 [P_RETRY_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004865};
4866
Philipp Reisnerb411b362009-09-25 16:07:19 -07004867int drbd_asender(struct drbd_thread *thi)
4868{
Philipp Reisner392c8802011-02-09 10:33:31 +01004869 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004870 struct asender_cmd *cmd = NULL;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004871 struct packet_info pi;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004872 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004873 void *buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004874 int received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02004875 unsigned int header_size = drbd_header_size(tconn);
4876 int expect = header_size;
Lars Ellenbergf36af182011-03-09 22:44:55 +01004877 int ping_timeout_active = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004878
Philipp Reisnerb411b362009-09-25 16:07:19 -07004879 current->policy = SCHED_RR; /* Make this a realtime task! */
4880 current->rt_priority = 2; /* more important than all other tasks */
4881
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01004882 while (get_t_state(thi) == RUNNING) {
Philipp Reisner80822282011-02-08 12:46:30 +01004883 drbd_thread_current_set_cpu(thi);
Philipp Reisner32862ec2011-02-08 16:41:01 +01004884 if (test_and_clear_bit(SEND_PING, &tconn->flags)) {
Andreas Gruenbachera17647a2011-04-01 12:49:42 +02004885 if (drbd_send_ping(tconn)) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004886 conn_err(tconn, "drbd_send_ping has failed\n");
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01004887 goto reconnect;
4888 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01004889 tconn->meta.socket->sk->sk_rcvtimeo =
4890 tconn->net_conf->ping_timeo*HZ/10;
Lars Ellenbergf36af182011-03-09 22:44:55 +01004891 ping_timeout_active = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004892 }
4893
Philipp Reisner32862ec2011-02-08 16:41:01 +01004894 /* TODO: conditionally cork; it may hurt latency if we cork without
4895 much to send */
4896 if (!tconn->net_conf->no_cork)
4897 drbd_tcp_cork(tconn->meta.socket);
Philipp Reisner082a3432011-03-15 16:05:42 +01004898 if (tconn_process_done_ee(tconn)) {
4899 conn_err(tconn, "tconn_process_done_ee() failed\n");
Philipp Reisner32862ec2011-02-08 16:41:01 +01004900 goto reconnect;
Philipp Reisner082a3432011-03-15 16:05:42 +01004901 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004902 /* but unconditionally uncork unless disabled */
Philipp Reisner32862ec2011-02-08 16:41:01 +01004903 if (!tconn->net_conf->no_cork)
4904 drbd_tcp_uncork(tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004905
4906 /* short circuit, recv_msg would return EINTR anyways. */
4907 if (signal_pending(current))
4908 continue;
4909
Philipp Reisner32862ec2011-02-08 16:41:01 +01004910 rv = drbd_recv_short(tconn->meta.socket, buf, expect-received, 0);
4911 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004912
4913 flush_signals(current);
4914
4915 /* Note:
4916 * -EINTR (on meta) we got a signal
4917 * -EAGAIN (on meta) rcvtimeo expired
4918 * -ECONNRESET other side closed the connection
4919 * -ERESTARTSYS (on data) we got a signal
4920 * rv < 0 other than above: unexpected error!
4921 * rv == expected: full header or command
4922 * rv < expected: "woken" by signal during receive
4923 * rv == 0 : "connection shut down by peer"
4924 */
4925 if (likely(rv > 0)) {
4926 received += rv;
4927 buf += rv;
4928 } else if (rv == 0) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004929 conn_err(tconn, "meta connection shut down by peer.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004930 goto reconnect;
4931 } else if (rv == -EAGAIN) {
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02004932 /* If the data socket received something meanwhile,
4933 * that is good enough: peer is still alive. */
Philipp Reisner32862ec2011-02-08 16:41:01 +01004934 if (time_after(tconn->last_received,
4935 jiffies - tconn->meta.socket->sk->sk_rcvtimeo))
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02004936 continue;
Lars Ellenbergf36af182011-03-09 22:44:55 +01004937 if (ping_timeout_active) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004938 conn_err(tconn, "PingAck did not arrive in time.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004939 goto reconnect;
4940 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01004941 set_bit(SEND_PING, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004942 continue;
4943 } else if (rv == -EINTR) {
4944 continue;
4945 } else {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004946 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004947 goto reconnect;
4948 }
4949
4950 if (received == expect && cmd == NULL) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004951 if (decode_header(tconn, tconn->meta.rbuf, &pi))
Philipp Reisnerb411b362009-09-25 16:07:19 -07004952 goto reconnect;
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004953 cmd = &asender_tbl[pi.cmd];
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004954 if (pi.cmd >= ARRAY_SIZE(asender_tbl) || !cmd->fn) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004955 conn_err(tconn, "unknown command %d on meta (l: %d)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004956 pi.cmd, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004957 goto disconnect;
4958 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004959 expect = header_size + cmd->pkt_size;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02004960 if (pi.size != expect - header_size) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004961 conn_err(tconn, "Wrong packet size on meta (c: %d, l: %d)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004962 pi.cmd, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004963 goto reconnect;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004964 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004965 }
4966 if (received == expect) {
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004967 bool err;
Philipp Reisnera4fbda82011-03-16 11:13:17 +01004968
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004969 err = cmd->fn(tconn, &pi);
4970 if (err) {
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004971 conn_err(tconn, "%pf failed\n", cmd->fn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004972 goto reconnect;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004973 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004974
Philipp Reisnera4fbda82011-03-16 11:13:17 +01004975 tconn->last_received = jiffies;
4976
Lars Ellenbergf36af182011-03-09 22:44:55 +01004977 /* the idle_timeout (ping-int)
4978 * has been restored in got_PingAck() */
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004979 if (cmd == &asender_tbl[P_PING_ACK])
Lars Ellenbergf36af182011-03-09 22:44:55 +01004980 ping_timeout_active = 0;
4981
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004982 buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004983 received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02004984 expect = header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004985 cmd = NULL;
4986 }
4987 }
4988
4989 if (0) {
4990reconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004991 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004992 }
4993 if (0) {
4994disconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004995 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004996 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01004997 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004998
Philipp Reisner32862ec2011-02-08 16:41:01 +01004999 conn_info(tconn, "asender terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005000
5001 return 0;
5002}