blob: 2b0b0ab90f2172e9c1da7a63747925f5f787a52a [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
Andreas Gruenbacher18c2d522011-04-07 21:08:50 +0200153static struct page *__drbd_alloc_pages(struct drbd_conf *mdev,
154 unsigned int number)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700155{
156 struct page *page = NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200157 struct page *tmp = NULL;
Andreas Gruenbacher18c2d522011-04-07 21:08:50 +0200158 unsigned int i = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700159
160 /* Yes, testing drbd_pp_vacant outside the lock is racy.
161 * So what. It saves a spin_lock. */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200162 if (drbd_pp_vacant >= number) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700163 spin_lock(&drbd_pp_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200164 page = page_chain_del(&drbd_pp_pool, number);
165 if (page)
166 drbd_pp_vacant -= number;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700167 spin_unlock(&drbd_pp_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200168 if (page)
169 return page;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700170 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200171
Philipp Reisnerb411b362009-09-25 16:07:19 -0700172 /* GFP_TRY, because we must not cause arbitrary write-out: in a DRBD
173 * "criss-cross" setup, that might cause write-out on some other DRBD,
174 * which in turn might block on the other node at this very place. */
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200175 for (i = 0; i < number; i++) {
176 tmp = alloc_page(GFP_TRY);
177 if (!tmp)
178 break;
179 set_page_private(tmp, (unsigned long)page);
180 page = tmp;
181 }
182
183 if (i == number)
184 return page;
185
186 /* Not enough pages immediately available this time.
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200187 * No need to jump around here, drbd_alloc_pages will retry this
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200188 * function "soon". */
189 if (page) {
190 tmp = page_chain_tail(page, NULL);
191 spin_lock(&drbd_pp_lock);
192 page_chain_add(&drbd_pp_pool, page, tmp);
193 drbd_pp_vacant += i;
194 spin_unlock(&drbd_pp_lock);
195 }
196 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700197}
198
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200199static void reclaim_finished_net_peer_reqs(struct drbd_conf *mdev,
200 struct list_head *to_be_freed)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700201{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100202 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700203 struct list_head *le, *tle;
204
205 /* The EEs are always appended to the end of the list. Since
206 they are sent in order over the wire, they have to finish
207 in order. As soon as we see the first not finished we can
208 stop to examine the list... */
209
210 list_for_each_safe(le, tle, &mdev->net_ee) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100211 peer_req = list_entry(le, struct drbd_peer_request, w.list);
Andreas Gruenbacher045417f2011-04-07 21:34:24 +0200212 if (drbd_peer_req_has_active_page(peer_req))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700213 break;
214 list_move(le, to_be_freed);
215 }
216}
217
218static void drbd_kick_lo_and_reclaim_net(struct drbd_conf *mdev)
219{
220 LIST_HEAD(reclaimed);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100221 struct drbd_peer_request *peer_req, *t;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700222
Philipp Reisner87eeee42011-01-19 14:16:30 +0100223 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200224 reclaim_finished_net_peer_reqs(mdev, &reclaimed);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100225 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700226
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100227 list_for_each_entry_safe(peer_req, t, &reclaimed, w.list)
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200228 drbd_free_net_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700229}
230
231/**
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200232 * drbd_alloc_pages() - Returns @number pages, retries forever (or until signalled)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700233 * @mdev: DRBD device.
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200234 * @number: number of pages requested
235 * @retry: whether to retry, if not enough pages are available right now
Philipp Reisnerb411b362009-09-25 16:07:19 -0700236 *
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200237 * Tries to allocate number pages, first from our own page pool, then from
238 * the kernel, unless this allocation would exceed the max_buffers setting.
239 * Possibly retry until DRBD frees sufficient pages somewhere else.
240 *
241 * Returns a page chain linked via page->private.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700242 */
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200243struct page *drbd_alloc_pages(struct drbd_conf *mdev, unsigned int number,
244 bool retry)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700245{
246 struct page *page = NULL;
247 DEFINE_WAIT(wait);
248
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200249 /* Yes, we may run up to @number over max_buffers. If we
250 * follow it strictly, the admin will get it wrong anyways. */
Philipp Reisner89e58e72011-01-19 13:12:45 +0100251 if (atomic_read(&mdev->pp_in_use) < mdev->tconn->net_conf->max_buffers)
Andreas Gruenbacher18c2d522011-04-07 21:08:50 +0200252 page = __drbd_alloc_pages(mdev, number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700253
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200254 while (page == NULL) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700255 prepare_to_wait(&drbd_pp_wait, &wait, TASK_INTERRUPTIBLE);
256
257 drbd_kick_lo_and_reclaim_net(mdev);
258
Philipp Reisner89e58e72011-01-19 13:12:45 +0100259 if (atomic_read(&mdev->pp_in_use) < mdev->tconn->net_conf->max_buffers) {
Andreas Gruenbacher18c2d522011-04-07 21:08:50 +0200260 page = __drbd_alloc_pages(mdev, number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700261 if (page)
262 break;
263 }
264
265 if (!retry)
266 break;
267
268 if (signal_pending(current)) {
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200269 dev_warn(DEV, "drbd_alloc_pages interrupted!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700270 break;
271 }
272
273 schedule();
274 }
275 finish_wait(&drbd_pp_wait, &wait);
276
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200277 if (page)
278 atomic_add(number, &mdev->pp_in_use);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700279 return page;
280}
281
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200282/* Must not be used from irq, as that may deadlock: see drbd_alloc_pages.
Philipp Reisner87eeee42011-01-19 14:16:30 +0100283 * Is also used from inside an other spin_lock_irq(&mdev->tconn->req_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200284 * Either links the page chain back to the global pool,
285 * or returns all pages to the system. */
Andreas Gruenbacher5cc287e2011-04-07 21:02:59 +0200286static void drbd_free_pages(struct drbd_conf *mdev, struct page *page, int is_net)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700287{
Lars Ellenberg435f0742010-09-06 12:30:25 +0200288 atomic_t *a = is_net ? &mdev->pp_in_use_by_net : &mdev->pp_in_use;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700289 int i;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200290
Philipp Reisner81a5d602011-02-22 19:53:16 -0500291 if (drbd_pp_vacant > (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * minor_count)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200292 i = page_chain_free(page);
293 else {
294 struct page *tmp;
295 tmp = page_chain_tail(page, &i);
296 spin_lock(&drbd_pp_lock);
297 page_chain_add(&drbd_pp_pool, page, tmp);
298 drbd_pp_vacant += i;
299 spin_unlock(&drbd_pp_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700300 }
Lars Ellenberg435f0742010-09-06 12:30:25 +0200301 i = atomic_sub_return(i, a);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200302 if (i < 0)
Lars Ellenberg435f0742010-09-06 12:30:25 +0200303 dev_warn(DEV, "ASSERTION FAILED: %s: %d < 0\n",
304 is_net ? "pp_in_use_by_net" : "pp_in_use", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700305 wake_up(&drbd_pp_wait);
306}
307
308/*
309You need to hold the req_lock:
310 _drbd_wait_ee_list_empty()
311
312You must not have the req_lock:
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200313 drbd_free_peer_req()
Andreas Gruenbacher0db55362011-04-06 16:09:15 +0200314 drbd_alloc_peer_req()
Andreas Gruenbacher7721f562011-04-06 17:14:02 +0200315 drbd_free_peer_reqs()
Philipp Reisnerb411b362009-09-25 16:07:19 -0700316 drbd_ee_fix_bhs()
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200317 drbd_finish_peer_reqs()
Philipp Reisnerb411b362009-09-25 16:07:19 -0700318 drbd_clear_done_ee()
319 drbd_wait_ee_list_empty()
320*/
321
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +0100322struct drbd_peer_request *
Andreas Gruenbacher0db55362011-04-06 16:09:15 +0200323drbd_alloc_peer_req(struct drbd_conf *mdev, u64 id, sector_t sector,
324 unsigned int data_size, gfp_t gfp_mask) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700325{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100326 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700327 struct page *page;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200328 unsigned nr_pages = (data_size + PAGE_SIZE -1) >> PAGE_SHIFT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700329
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100330 if (drbd_insert_fault(mdev, DRBD_FAULT_AL_EE))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700331 return NULL;
332
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100333 peer_req = mempool_alloc(drbd_ee_mempool, gfp_mask & ~__GFP_HIGHMEM);
334 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700335 if (!(gfp_mask & __GFP_NOWARN))
Andreas Gruenbacher0db55362011-04-06 16:09:15 +0200336 dev_err(DEV, "%s: allocation failed\n", __func__);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700337 return NULL;
338 }
339
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200340 page = drbd_alloc_pages(mdev, nr_pages, (gfp_mask & __GFP_WAIT));
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200341 if (!page)
342 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700343
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100344 drbd_clear_interval(&peer_req->i);
345 peer_req->i.size = data_size;
346 peer_req->i.sector = sector;
347 peer_req->i.local = false;
348 peer_req->i.waiting = false;
Andreas Gruenbacher53840642011-01-28 10:31:04 +0100349
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100350 peer_req->epoch = NULL;
Philipp Reisnera21e9292011-02-08 15:08:49 +0100351 peer_req->w.mdev = mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100352 peer_req->pages = page;
353 atomic_set(&peer_req->pending_bios, 0);
354 peer_req->flags = 0;
Andreas Gruenbacher9a8e7752011-01-11 14:04:09 +0100355 /*
356 * The block_id is opaque to the receiver. It is not endianness
357 * converted, and sent back to the sender unchanged.
358 */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100359 peer_req->block_id = id;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700360
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100361 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700362
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200363 fail:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100364 mempool_free(peer_req, drbd_ee_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700365 return NULL;
366}
367
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200368void __drbd_free_peer_req(struct drbd_conf *mdev, struct drbd_peer_request *peer_req,
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +0100369 int is_net)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700370{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100371 if (peer_req->flags & EE_HAS_DIGEST)
372 kfree(peer_req->digest);
Andreas Gruenbacher5cc287e2011-04-07 21:02:59 +0200373 drbd_free_pages(mdev, peer_req->pages, is_net);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100374 D_ASSERT(atomic_read(&peer_req->pending_bios) == 0);
375 D_ASSERT(drbd_interval_empty(&peer_req->i));
376 mempool_free(peer_req, drbd_ee_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700377}
378
Andreas Gruenbacher7721f562011-04-06 17:14:02 +0200379int drbd_free_peer_reqs(struct drbd_conf *mdev, struct list_head *list)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700380{
381 LIST_HEAD(work_list);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100382 struct drbd_peer_request *peer_req, *t;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700383 int count = 0;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200384 int is_net = list == &mdev->net_ee;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700385
Philipp Reisner87eeee42011-01-19 14:16:30 +0100386 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700387 list_splice_init(list, &work_list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100388 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700389
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100390 list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200391 __drbd_free_peer_req(mdev, peer_req, is_net);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700392 count++;
393 }
394 return count;
395}
396
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200397/*
398 * See also comments in _req_mod(,BARRIER_ACKED) and receive_Barrier.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700399 */
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200400static int drbd_finish_peer_reqs(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700401{
402 LIST_HEAD(work_list);
403 LIST_HEAD(reclaimed);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100404 struct drbd_peer_request *peer_req, *t;
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100405 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700406
Philipp Reisner87eeee42011-01-19 14:16:30 +0100407 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200408 reclaim_finished_net_peer_reqs(mdev, &reclaimed);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700409 list_splice_init(&mdev->done_ee, &work_list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100410 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700411
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100412 list_for_each_entry_safe(peer_req, t, &reclaimed, w.list)
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200413 drbd_free_net_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700414
415 /* possible callbacks here:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100416 * e_end_block, and e_end_resync_block, e_send_discard_write.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700417 * all ignore the last argument.
418 */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100419 list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100420 int err2;
421
Philipp Reisnerb411b362009-09-25 16:07:19 -0700422 /* list_del not necessary, next/prev members not touched */
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100423 err2 = peer_req->w.cb(&peer_req->w, !!err);
424 if (!err)
425 err = err2;
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200426 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700427 }
428 wake_up(&mdev->ee_wait);
429
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100430 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700431}
432
Andreas Gruenbacherd4da1532011-04-07 00:06:56 +0200433static void _drbd_wait_ee_list_empty(struct drbd_conf *mdev,
434 struct list_head *head)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700435{
436 DEFINE_WAIT(wait);
437
438 /* avoids spin_lock/unlock
439 * and calling prepare_to_wait in the fast path */
440 while (!list_empty(head)) {
441 prepare_to_wait(&mdev->ee_wait, &wait, TASK_UNINTERRUPTIBLE);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100442 spin_unlock_irq(&mdev->tconn->req_lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100443 io_schedule();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700444 finish_wait(&mdev->ee_wait, &wait);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100445 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700446 }
447}
448
Andreas Gruenbacherd4da1532011-04-07 00:06:56 +0200449static void drbd_wait_ee_list_empty(struct drbd_conf *mdev,
450 struct list_head *head)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700451{
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 Reisner8410da82011-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 Gruenbacher3967deb2011-04-06 16:16:56 +02001398 drbd_free_peer_req(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 Gruenbacher3967deb2011-04-06 16:16:56 +02001409 drbd_free_peer_req(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
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +02001429 page = drbd_alloc_pages(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);
Andreas Gruenbacher5cc287e2011-04-07 21:02:59 +02001441 drbd_free_pages(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
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001494/*
1495 * e_end_resync_block() is called in asender context via
1496 * drbd_finish_peer_reqs().
1497 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001498static int e_end_resync_block(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001499{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001500 struct drbd_peer_request *peer_req =
1501 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001502 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001503 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001504 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001505
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001506 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001507
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001508 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1509 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001510 err = drbd_send_ack(mdev, P_RS_WRITE_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001511 } else {
1512 /* Record failure to sync */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001513 drbd_rs_failed_io(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001514
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001515 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001516 }
1517 dec_unacked(mdev);
1518
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001519 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001520}
1521
1522static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local)
1523{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001524 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001525
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001526 peer_req = read_in_block(mdev, ID_SYNCER, sector, data_size);
1527 if (!peer_req)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001528 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001529
1530 dec_rs_pending(mdev);
1531
Philipp Reisnerb411b362009-09-25 16:07:19 -07001532 inc_unacked(mdev);
1533 /* corresponding dec_unacked() in e_end_resync_block()
1534 * respective _drbd_clear_done_ee */
1535
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001536 peer_req->w.cb = e_end_resync_block;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001537
Philipp Reisner87eeee42011-01-19 14:16:30 +01001538 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001539 list_add(&peer_req->w.list, &mdev->sync_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001540 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001541
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001542 atomic_add(data_size >> 9, &mdev->rs_sect_ev);
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001543 if (drbd_submit_peer_request(mdev, peer_req, WRITE, DRBD_FAULT_RS_WR) == 0)
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001544 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001545
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001546 /* don't care for the reason here */
1547 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01001548 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001549 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001550 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001551
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001552 drbd_free_peer_req(mdev, peer_req);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001553fail:
1554 put_ldev(mdev);
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001555 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001556}
1557
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001558static struct drbd_request *
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001559find_request(struct drbd_conf *mdev, struct rb_root *root, u64 id,
1560 sector_t sector, bool missing_ok, const char *func)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001561{
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001562 struct drbd_request *req;
1563
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001564 /* Request object according to our peer */
1565 req = (struct drbd_request *)(unsigned long)id;
Andreas Gruenbacher5e472262011-01-27 14:42:51 +01001566 if (drbd_contains_interval(root, sector, &req->i) && req->i.local)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001567 return req;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001568 if (!missing_ok) {
1569 dev_err(DEV, "%s: failed to find request %lu, sector %llus\n", func,
1570 (unsigned long)id, (unsigned long long)sector);
1571 }
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001572 return NULL;
1573}
1574
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001575static int receive_DataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001576{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001577 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001578 struct drbd_request *req;
1579 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001580 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001581 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001582
1583 mdev = vnr_to_mdev(tconn, pi->vnr);
1584 if (!mdev)
1585 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001586
1587 sector = be64_to_cpu(p->sector);
1588
Philipp Reisner87eeee42011-01-19 14:16:30 +01001589 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001590 req = find_request(mdev, &mdev->read_requests, p->block_id, sector, false, __func__);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001591 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001592 if (unlikely(!req))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001593 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001594
Bart Van Assche24c48302011-05-21 18:32:29 +02001595 /* hlist_del(&req->collision) is done in _req_may_be_done, to avoid
Philipp Reisnerb411b362009-09-25 16:07:19 -07001596 * special casing it there for the various failure cases.
1597 * still no race with drbd_fail_pending_reads */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001598 err = recv_dless_read(mdev, req, sector, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001599 if (!err)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001600 req_mod(req, DATA_RECEIVED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001601 /* else: nothing. handled from drbd_disconnect...
1602 * I don't think we may complete this just yet
1603 * in case we are "on-disconnect: freeze" */
1604
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001605 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001606}
1607
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001608static int receive_RSDataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001609{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001610 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001611 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001612 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001613 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001614
1615 mdev = vnr_to_mdev(tconn, pi->vnr);
1616 if (!mdev)
1617 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001618
1619 sector = be64_to_cpu(p->sector);
1620 D_ASSERT(p->block_id == ID_SYNCER);
1621
1622 if (get_ldev(mdev)) {
1623 /* data is submitted to disk within recv_resync_read.
1624 * corresponding put_ldev done below on error,
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001625 * or in drbd_peer_request_endio. */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001626 err = recv_resync_read(mdev, sector, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001627 } else {
1628 if (__ratelimit(&drbd_ratelimit_state))
1629 dev_err(DEV, "Can not write resync data to local disk.\n");
1630
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001631 err = drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001632
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001633 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001634 }
1635
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001636 atomic_add(pi->size >> 9, &mdev->rs_sect_in);
Philipp Reisner778f2712010-07-06 11:14:00 +02001637
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001638 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001639}
1640
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001641static int w_restart_write(struct drbd_work *w, int cancel)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001642{
1643 struct drbd_request *req = container_of(w, struct drbd_request, w);
1644 struct drbd_conf *mdev = w->mdev;
1645 struct bio *bio;
1646 unsigned long start_time;
1647 unsigned long flags;
1648
1649 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
1650 if (!expect(req->rq_state & RQ_POSTPONED)) {
1651 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001652 return -EIO;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001653 }
1654 bio = req->master_bio;
1655 start_time = req->start_time;
1656 /* Postponed requests will not have their master_bio completed! */
1657 __req_mod(req, DISCARD_WRITE, NULL);
1658 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
1659
1660 while (__drbd_make_request(mdev, bio, start_time))
1661 /* retry */ ;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001662 return 0;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001663}
1664
1665static void restart_conflicting_writes(struct drbd_conf *mdev,
1666 sector_t sector, int size)
1667{
1668 struct drbd_interval *i;
1669 struct drbd_request *req;
1670
1671 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1672 if (!i->local)
1673 continue;
1674 req = container_of(i, struct drbd_request, i);
1675 if (req->rq_state & RQ_LOCAL_PENDING ||
1676 !(req->rq_state & RQ_POSTPONED))
1677 continue;
1678 if (expect(list_empty(&req->w.list))) {
1679 req->w.mdev = mdev;
1680 req->w.cb = w_restart_write;
1681 drbd_queue_work(&mdev->tconn->data.work, &req->w);
1682 }
1683 }
1684}
1685
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001686/*
1687 * e_end_block() is called in asender context via drbd_finish_peer_reqs().
Philipp Reisnerb411b362009-09-25 16:07:19 -07001688 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001689static int e_end_block(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001690{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001691 struct drbd_peer_request *peer_req =
1692 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001693 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001694 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001695 int err = 0, pcmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001696
Philipp Reisner89e58e72011-01-19 13:12:45 +01001697 if (mdev->tconn->net_conf->wire_protocol == DRBD_PROT_C) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001698 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001699 pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
1700 mdev->state.conn <= C_PAUSED_SYNC_T &&
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001701 peer_req->flags & EE_MAY_SET_IN_SYNC) ?
Philipp Reisnerb411b362009-09-25 16:07:19 -07001702 P_RS_WRITE_ACK : P_WRITE_ACK;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001703 err = drbd_send_ack(mdev, pcmd, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001704 if (pcmd == P_RS_WRITE_ACK)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001705 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001706 } else {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001707 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001708 /* we expect it to be marked out of sync anyways...
1709 * maybe assert this? */
1710 }
1711 dec_unacked(mdev);
1712 }
1713 /* we delete from the conflict detection hash _after_ we sent out the
1714 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
Philipp Reisner89e58e72011-01-19 13:12:45 +01001715 if (mdev->tconn->net_conf->two_primaries) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01001716 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001717 D_ASSERT(!drbd_interval_empty(&peer_req->i));
1718 drbd_remove_epoch_entry_interval(mdev, peer_req);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001719 if (peer_req->flags & EE_RESTART_REQUESTS)
1720 restart_conflicting_writes(mdev, sector, peer_req->i.size);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001721 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +01001722 } else
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001723 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001724
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001725 drbd_may_finish_epoch(mdev, peer_req->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001726
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001727 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001728}
1729
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001730static int e_send_ack(struct drbd_work *w, enum drbd_packet ack)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001731{
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001732 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001733 struct drbd_peer_request *peer_req =
1734 container_of(w, struct drbd_peer_request, w);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001735 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001736
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001737 err = drbd_send_ack(mdev, ack, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001738 dec_unacked(mdev);
1739
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001740 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001741}
1742
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001743static int e_send_discard_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001744{
1745 return e_send_ack(w, P_DISCARD_WRITE);
1746}
1747
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001748static int e_send_retry_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001749{
1750 struct drbd_tconn *tconn = w->mdev->tconn;
1751
1752 return e_send_ack(w, tconn->agreed_pro_version >= 100 ?
1753 P_RETRY_WRITE : P_DISCARD_WRITE);
1754}
1755
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001756static bool seq_greater(u32 a, u32 b)
1757{
1758 /*
1759 * We assume 32-bit wrap-around here.
1760 * For 24-bit wrap-around, we would have to shift:
1761 * a <<= 8; b <<= 8;
1762 */
1763 return (s32)a - (s32)b > 0;
1764}
1765
1766static u32 seq_max(u32 a, u32 b)
1767{
1768 return seq_greater(a, b) ? a : b;
1769}
1770
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001771static bool need_peer_seq(struct drbd_conf *mdev)
1772{
1773 struct drbd_tconn *tconn = mdev->tconn;
1774
1775 /*
1776 * We only need to keep track of the last packet_seq number of our peer
1777 * if we are in dual-primary mode and we have the discard flag set; see
1778 * handle_write_conflicts().
1779 */
1780 return tconn->net_conf->two_primaries &&
1781 test_bit(DISCARD_CONCURRENT, &tconn->flags);
1782}
1783
Andreas Gruenbacher43ae0772011-02-03 18:42:08 +01001784static void update_peer_seq(struct drbd_conf *mdev, unsigned int peer_seq)
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001785{
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001786 unsigned int newest_peer_seq;
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001787
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001788 if (need_peer_seq(mdev)) {
1789 spin_lock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001790 newest_peer_seq = seq_max(mdev->peer_seq, peer_seq);
1791 mdev->peer_seq = newest_peer_seq;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001792 spin_unlock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001793 /* wake up only if we actually changed mdev->peer_seq */
1794 if (peer_seq == newest_peer_seq)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001795 wake_up(&mdev->seq_wait);
1796 }
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001797}
1798
Philipp Reisnerb411b362009-09-25 16:07:19 -07001799/* Called from receive_Data.
1800 * Synchronize packets on sock with packets on msock.
1801 *
1802 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
1803 * packet traveling on msock, they are still processed in the order they have
1804 * been sent.
1805 *
1806 * Note: we don't care for Ack packets overtaking P_DATA packets.
1807 *
1808 * In case packet_seq is larger than mdev->peer_seq number, there are
1809 * outstanding packets on the msock. We wait for them to arrive.
1810 * In case we are the logically next packet, we update mdev->peer_seq
1811 * ourselves. Correctly handles 32bit wrap around.
1812 *
1813 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
1814 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
1815 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
1816 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
1817 *
1818 * returns 0 if we may process the packet,
1819 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001820static int wait_for_and_update_peer_seq(struct drbd_conf *mdev, const u32 peer_seq)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001821{
1822 DEFINE_WAIT(wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001823 long timeout;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001824 int ret;
1825
1826 if (!need_peer_seq(mdev))
1827 return 0;
1828
Philipp Reisnerb411b362009-09-25 16:07:19 -07001829 spin_lock(&mdev->peer_seq_lock);
1830 for (;;) {
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001831 if (!seq_greater(peer_seq - 1, mdev->peer_seq)) {
1832 mdev->peer_seq = seq_max(mdev->peer_seq, peer_seq);
1833 ret = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001834 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001835 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001836 if (signal_pending(current)) {
1837 ret = -ERESTARTSYS;
1838 break;
1839 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001840 prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001841 spin_unlock(&mdev->peer_seq_lock);
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001842 timeout = mdev->tconn->net_conf->ping_timeo*HZ/10;
1843 timeout = schedule_timeout(timeout);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001844 spin_lock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001845 if (!timeout) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001846 ret = -ETIMEDOUT;
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001847 dev_err(DEV, "Timed out waiting for missing ack packets; disconnecting\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001848 break;
1849 }
1850 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001851 spin_unlock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001852 finish_wait(&mdev->seq_wait, &wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001853 return ret;
1854}
1855
Lars Ellenberg688593c2010-11-17 22:25:03 +01001856/* see also bio_flags_to_wire()
1857 * DRBD_REQ_*, because we need to semantically map the flags to data packet
1858 * flags and back. We may replicate to other kernel versions. */
1859static unsigned long wire_flags_to_bio(struct drbd_conf *mdev, u32 dpf)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001860{
Lars Ellenberg688593c2010-11-17 22:25:03 +01001861 return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
1862 (dpf & DP_FUA ? REQ_FUA : 0) |
1863 (dpf & DP_FLUSH ? REQ_FLUSH : 0) |
1864 (dpf & DP_DISCARD ? REQ_DISCARD : 0);
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001865}
1866
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001867static void fail_postponed_requests(struct drbd_conf *mdev, sector_t sector,
1868 unsigned int size)
1869{
1870 struct drbd_interval *i;
1871
1872 repeat:
1873 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1874 struct drbd_request *req;
1875 struct bio_and_error m;
1876
1877 if (!i->local)
1878 continue;
1879 req = container_of(i, struct drbd_request, i);
1880 if (!(req->rq_state & RQ_POSTPONED))
1881 continue;
1882 req->rq_state &= ~RQ_POSTPONED;
1883 __req_mod(req, NEG_ACKED, &m);
1884 spin_unlock_irq(&mdev->tconn->req_lock);
1885 if (m.bio)
1886 complete_master_bio(mdev, &m);
1887 spin_lock_irq(&mdev->tconn->req_lock);
1888 goto repeat;
1889 }
1890}
1891
1892static int handle_write_conflicts(struct drbd_conf *mdev,
1893 struct drbd_peer_request *peer_req)
1894{
1895 struct drbd_tconn *tconn = mdev->tconn;
1896 bool resolve_conflicts = test_bit(DISCARD_CONCURRENT, &tconn->flags);
1897 sector_t sector = peer_req->i.sector;
1898 const unsigned int size = peer_req->i.size;
1899 struct drbd_interval *i;
1900 bool equal;
1901 int err;
1902
1903 /*
1904 * Inserting the peer request into the write_requests tree will prevent
1905 * new conflicting local requests from being added.
1906 */
1907 drbd_insert_interval(&mdev->write_requests, &peer_req->i);
1908
1909 repeat:
1910 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1911 if (i == &peer_req->i)
1912 continue;
1913
1914 if (!i->local) {
1915 /*
1916 * Our peer has sent a conflicting remote request; this
1917 * should not happen in a two-node setup. Wait for the
1918 * earlier peer request to complete.
1919 */
1920 err = drbd_wait_misc(mdev, i);
1921 if (err)
1922 goto out;
1923 goto repeat;
1924 }
1925
1926 equal = i->sector == sector && i->size == size;
1927 if (resolve_conflicts) {
1928 /*
1929 * If the peer request is fully contained within the
1930 * overlapping request, it can be discarded; otherwise,
1931 * it will be retried once all overlapping requests
1932 * have completed.
1933 */
1934 bool discard = i->sector <= sector && i->sector +
1935 (i->size >> 9) >= sector + (size >> 9);
1936
1937 if (!equal)
1938 dev_alert(DEV, "Concurrent writes detected: "
1939 "local=%llus +%u, remote=%llus +%u, "
1940 "assuming %s came first\n",
1941 (unsigned long long)i->sector, i->size,
1942 (unsigned long long)sector, size,
1943 discard ? "local" : "remote");
1944
1945 inc_unacked(mdev);
1946 peer_req->w.cb = discard ? e_send_discard_write :
1947 e_send_retry_write;
1948 list_add_tail(&peer_req->w.list, &mdev->done_ee);
1949 wake_asender(mdev->tconn);
1950
1951 err = -ENOENT;
1952 goto out;
1953 } else {
1954 struct drbd_request *req =
1955 container_of(i, struct drbd_request, i);
1956
1957 if (!equal)
1958 dev_alert(DEV, "Concurrent writes detected: "
1959 "local=%llus +%u, remote=%llus +%u\n",
1960 (unsigned long long)i->sector, i->size,
1961 (unsigned long long)sector, size);
1962
1963 if (req->rq_state & RQ_LOCAL_PENDING ||
1964 !(req->rq_state & RQ_POSTPONED)) {
1965 /*
1966 * Wait for the node with the discard flag to
1967 * decide if this request will be discarded or
1968 * retried. Requests that are discarded will
1969 * disappear from the write_requests tree.
1970 *
1971 * In addition, wait for the conflicting
1972 * request to finish locally before submitting
1973 * the conflicting peer request.
1974 */
1975 err = drbd_wait_misc(mdev, &req->i);
1976 if (err) {
1977 _conn_request_state(mdev->tconn,
1978 NS(conn, C_TIMEOUT),
1979 CS_HARD);
1980 fail_postponed_requests(mdev, sector, size);
1981 goto out;
1982 }
1983 goto repeat;
1984 }
1985 /*
1986 * Remember to restart the conflicting requests after
1987 * the new peer request has completed.
1988 */
1989 peer_req->flags |= EE_RESTART_REQUESTS;
1990 }
1991 }
1992 err = 0;
1993
1994 out:
1995 if (err)
1996 drbd_remove_epoch_entry_interval(mdev, peer_req);
1997 return err;
1998}
1999
Philipp Reisnerb411b362009-09-25 16:07:19 -07002000/* mirrored write */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002001static int receive_Data(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002002{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002003 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002004 sector_t sector;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002005 struct drbd_peer_request *peer_req;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002006 struct p_data *p = pi->data;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002007 u32 peer_seq = be32_to_cpu(p->seq_num);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002008 int rw = WRITE;
2009 u32 dp_flags;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002010 int err;
2011
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002012 mdev = vnr_to_mdev(tconn, pi->vnr);
2013 if (!mdev)
2014 return -EIO;
2015
Philipp Reisnerb411b362009-09-25 16:07:19 -07002016 if (!get_ldev(mdev)) {
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002017 int err2;
2018
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002019 err = wait_for_and_update_peer_seq(mdev, peer_seq);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002020 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002021 atomic_inc(&mdev->current_epoch->epoch_size);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002022 err2 = drbd_drain_block(mdev, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002023 if (!err)
2024 err = err2;
2025 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002026 }
2027
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01002028 /*
2029 * Corresponding put_ldev done either below (on various errors), or in
2030 * drbd_peer_request_endio, if we successfully submit the data at the
2031 * end of this function.
2032 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002033
2034 sector = be64_to_cpu(p->sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002035 peer_req = read_in_block(mdev, p->block_id, sector, pi->size);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002036 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002037 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002038 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002039 }
2040
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002041 peer_req->w.cb = e_end_block;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002042
Lars Ellenberg688593c2010-11-17 22:25:03 +01002043 dp_flags = be32_to_cpu(p->dp_flags);
2044 rw |= wire_flags_to_bio(mdev, dp_flags);
2045
2046 if (dp_flags & DP_MAY_SET_IN_SYNC)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002047 peer_req->flags |= EE_MAY_SET_IN_SYNC;
Lars Ellenberg688593c2010-11-17 22:25:03 +01002048
Philipp Reisnerb411b362009-09-25 16:07:19 -07002049 spin_lock(&mdev->epoch_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002050 peer_req->epoch = mdev->current_epoch;
2051 atomic_inc(&peer_req->epoch->epoch_size);
2052 atomic_inc(&peer_req->epoch->active);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002053 spin_unlock(&mdev->epoch_lock);
2054
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002055 if (mdev->tconn->net_conf->two_primaries) {
2056 err = wait_for_and_update_peer_seq(mdev, peer_seq);
2057 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002058 goto out_interrupted;
Philipp Reisner87eeee42011-01-19 14:16:30 +01002059 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002060 err = handle_write_conflicts(mdev, peer_req);
2061 if (err) {
2062 spin_unlock_irq(&mdev->tconn->req_lock);
2063 if (err == -ENOENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002064 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002065 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002066 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002067 goto out_interrupted;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002068 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002069 } else
2070 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002071 list_add(&peer_req->w.list, &mdev->active_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002072 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002073
Philipp Reisner89e58e72011-01-19 13:12:45 +01002074 switch (mdev->tconn->net_conf->wire_protocol) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002075 case DRBD_PROT_C:
2076 inc_unacked(mdev);
2077 /* corresponding dec_unacked() in e_end_block()
2078 * respective _drbd_clear_done_ee */
2079 break;
2080 case DRBD_PROT_B:
2081 /* I really don't like it that the receiver thread
2082 * sends on the msock, but anyways */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002083 drbd_send_ack(mdev, P_RECV_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002084 break;
2085 case DRBD_PROT_A:
2086 /* nothing to do */
2087 break;
2088 }
2089
Lars Ellenberg6719fb02010-10-18 23:04:07 +02002090 if (mdev->state.pdsk < D_INCONSISTENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002091 /* In case we have the only disk of the cluster, */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002092 drbd_set_out_of_sync(mdev, peer_req->i.sector, peer_req->i.size);
2093 peer_req->flags |= EE_CALL_AL_COMPLETE_IO;
2094 peer_req->flags &= ~EE_MAY_SET_IN_SYNC;
Lars Ellenberg181286a2011-03-31 15:18:56 +02002095 drbd_al_begin_io(mdev, &peer_req->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002096 }
2097
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002098 err = drbd_submit_peer_request(mdev, peer_req, rw, DRBD_FAULT_DT_WR);
2099 if (!err)
2100 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002101
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002102 /* don't care for the reason here */
2103 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002104 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002105 list_del(&peer_req->w.list);
2106 drbd_remove_epoch_entry_interval(mdev, peer_req);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002107 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002108 if (peer_req->flags & EE_CALL_AL_COMPLETE_IO)
Lars Ellenberg181286a2011-03-31 15:18:56 +02002109 drbd_al_complete_io(mdev, &peer_req->i);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002110
Philipp Reisnerb411b362009-09-25 16:07:19 -07002111out_interrupted:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002112 drbd_may_finish_epoch(mdev, peer_req->epoch, EV_PUT + EV_CLEANUP);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002113 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002114 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002115 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002116}
2117
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002118/* We may throttle resync, if the lower device seems to be busy,
2119 * and current sync rate is above c_min_rate.
2120 *
2121 * To decide whether or not the lower device is busy, we use a scheme similar
2122 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
2123 * (more than 64 sectors) of activity we cannot account for with our own resync
2124 * activity, it obviously is "busy".
2125 *
2126 * The current sync rate used here uses only the most recent two step marks,
2127 * to have a short time average so we can react faster.
2128 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002129int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002130{
2131 struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
2132 unsigned long db, dt, dbdt;
Philipp Reisnere3555d82010-11-07 15:56:29 +01002133 struct lc_element *tmp;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002134 int curr_events;
2135 int throttle = 0;
2136
2137 /* feature disabled? */
Lars Ellenbergf3990022011-03-23 14:31:09 +01002138 if (mdev->ldev->dc.c_min_rate == 0)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002139 return 0;
2140
Philipp Reisnere3555d82010-11-07 15:56:29 +01002141 spin_lock_irq(&mdev->al_lock);
2142 tmp = lc_find(mdev->resync, BM_SECT_TO_EXT(sector));
2143 if (tmp) {
2144 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
2145 if (test_bit(BME_PRIORITY, &bm_ext->flags)) {
2146 spin_unlock_irq(&mdev->al_lock);
2147 return 0;
2148 }
2149 /* Do not slow down if app IO is already waiting for this extent */
2150 }
2151 spin_unlock_irq(&mdev->al_lock);
2152
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002153 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
2154 (int)part_stat_read(&disk->part0, sectors[1]) -
2155 atomic_read(&mdev->rs_sect_ev);
Philipp Reisnere3555d82010-11-07 15:56:29 +01002156
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002157 if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
2158 unsigned long rs_left;
2159 int i;
2160
2161 mdev->rs_last_events = curr_events;
2162
2163 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
2164 * approx. */
Lars Ellenberg2649f082010-11-05 10:05:47 +01002165 i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
2166
2167 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
2168 rs_left = mdev->ov_left;
2169 else
2170 rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002171
2172 dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
2173 if (!dt)
2174 dt++;
2175 db = mdev->rs_mark_left[i] - rs_left;
2176 dbdt = Bit2KB(db/dt);
2177
Lars Ellenbergf3990022011-03-23 14:31:09 +01002178 if (dbdt > mdev->ldev->dc.c_min_rate)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002179 throttle = 1;
2180 }
2181 return throttle;
2182}
2183
2184
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002185static int receive_DataRequest(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002186{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002187 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002188 sector_t sector;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002189 sector_t capacity;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002190 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002191 struct digest_info *di = NULL;
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002192 int size, verb;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002193 unsigned int fault_type;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002194 struct p_block_req *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002195
2196 mdev = vnr_to_mdev(tconn, pi->vnr);
2197 if (!mdev)
2198 return -EIO;
2199 capacity = drbd_get_capacity(mdev->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002200
2201 sector = be64_to_cpu(p->sector);
2202 size = be32_to_cpu(p->blksize);
2203
Andreas Gruenbacherc670a392011-02-21 12:41:39 +01002204 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002205 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2206 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002207 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002208 }
2209 if (sector + (size>>9) > capacity) {
2210 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2211 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002212 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002213 }
2214
2215 if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002216 verb = 1;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002217 switch (pi->cmd) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002218 case P_DATA_REQUEST:
2219 drbd_send_ack_rp(mdev, P_NEG_DREPLY, p);
2220 break;
2221 case P_RS_DATA_REQUEST:
2222 case P_CSUM_RS_REQUEST:
2223 case P_OV_REQUEST:
2224 drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p);
2225 break;
2226 case P_OV_REPLY:
2227 verb = 0;
2228 dec_rs_pending(mdev);
2229 drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC);
2230 break;
2231 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002232 BUG();
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002233 }
2234 if (verb && __ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002235 dev_err(DEV, "Can not satisfy peer's read request, "
2236 "no local data.\n");
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002237
Lars Ellenberga821cc42010-09-06 12:31:37 +02002238 /* drain possibly payload */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002239 return drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002240 }
2241
2242 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
2243 * "criss-cross" setup, that might cause write-out on some other DRBD,
2244 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02002245 peer_req = drbd_alloc_peer_req(mdev, p->block_id, sector, size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002246 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002247 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002248 return -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002249 }
2250
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002251 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002252 case P_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002253 peer_req->w.cb = w_e_end_data_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002254 fault_type = DRBD_FAULT_DT_RD;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002255 /* application IO, don't drbd_rs_begin_io */
2256 goto submit;
2257
Philipp Reisnerb411b362009-09-25 16:07:19 -07002258 case P_RS_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002259 peer_req->w.cb = w_e_end_rsdata_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002260 fault_type = DRBD_FAULT_RS_RD;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002261 /* used in the sector offset progress display */
2262 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002263 break;
2264
2265 case P_OV_REPLY:
2266 case P_CSUM_RS_REQUEST:
2267 fault_type = DRBD_FAULT_RS_RD;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002268 di = kmalloc(sizeof(*di) + pi->size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002269 if (!di)
2270 goto out_free_e;
2271
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002272 di->digest_size = pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002273 di->digest = (((char *)di)+sizeof(struct digest_info));
2274
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002275 peer_req->digest = di;
2276 peer_req->flags |= EE_HAS_DIGEST;
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +02002277
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002278 if (drbd_recv_all(mdev->tconn, di->digest, pi->size))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002279 goto out_free_e;
2280
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002281 if (pi->cmd == P_CSUM_RS_REQUEST) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002282 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002283 peer_req->w.cb = w_e_end_csum_rs_req;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002284 /* used in the sector offset progress display */
2285 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002286 } else if (pi->cmd == P_OV_REPLY) {
Lars Ellenberg2649f082010-11-05 10:05:47 +01002287 /* track progress, we may need to throttle */
2288 atomic_add(size >> 9, &mdev->rs_sect_in);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002289 peer_req->w.cb = w_e_end_ov_reply;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002290 dec_rs_pending(mdev);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002291 /* drbd_rs_begin_io done when we sent this request,
2292 * but accounting still needs to be done. */
2293 goto submit_for_resync;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002294 }
2295 break;
2296
2297 case P_OV_REQUEST:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002298 if (mdev->ov_start_sector == ~(sector_t)0 &&
Philipp Reisner31890f42011-01-19 14:12:51 +01002299 mdev->tconn->agreed_pro_version >= 90) {
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002300 unsigned long now = jiffies;
2301 int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002302 mdev->ov_start_sector = sector;
2303 mdev->ov_position = sector;
Lars Ellenberg30b743a2010-11-05 09:39:06 +01002304 mdev->ov_left = drbd_bm_bits(mdev) - BM_SECT_TO_BIT(sector);
2305 mdev->rs_total = mdev->ov_left;
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002306 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
2307 mdev->rs_mark_left[i] = mdev->ov_left;
2308 mdev->rs_mark_time[i] = now;
2309 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002310 dev_info(DEV, "Online Verify start sector: %llu\n",
2311 (unsigned long long)sector);
2312 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002313 peer_req->w.cb = w_e_end_ov_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002314 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002315 break;
2316
Philipp Reisnerb411b362009-09-25 16:07:19 -07002317 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002318 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002319 }
2320
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002321 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2322 * wrt the receiver, but it is not as straightforward as it may seem.
2323 * Various places in the resync start and stop logic assume resync
2324 * requests are processed in order, requeuing this on the worker thread
2325 * introduces a bunch of new code for synchronization between threads.
2326 *
2327 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2328 * "forever", throttling after drbd_rs_begin_io will lock that extent
2329 * for application writes for the same time. For now, just throttle
2330 * here, where the rest of the code expects the receiver to sleep for
2331 * a while, anyways.
2332 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002333
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002334 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2335 * this defers syncer requests for some time, before letting at least
2336 * on request through. The resync controller on the receiving side
2337 * will adapt to the incoming rate accordingly.
2338 *
2339 * We cannot throttle here if remote is Primary/SyncTarget:
2340 * we would also throttle its application reads.
2341 * In that case, throttling is done on the SyncTarget only.
2342 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002343 if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev, sector))
2344 schedule_timeout_uninterruptible(HZ/10);
2345 if (drbd_rs_begin_io(mdev, sector))
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002346 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002347
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002348submit_for_resync:
2349 atomic_add(size >> 9, &mdev->rs_sect_ev);
2350
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002351submit:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002352 inc_unacked(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002353 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002354 list_add_tail(&peer_req->w.list, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002355 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002356
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01002357 if (drbd_submit_peer_request(mdev, peer_req, READ, fault_type) == 0)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002358 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002359
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002360 /* don't care for the reason here */
2361 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002362 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002363 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002364 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002365 /* no drbd_rs_complete_io(), we are dropping the connection anyways */
2366
Philipp Reisnerb411b362009-09-25 16:07:19 -07002367out_free_e:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002368 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002369 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002370 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002371}
2372
2373static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
2374{
2375 int self, peer, rv = -100;
2376 unsigned long ch_self, ch_peer;
2377
2378 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2379 peer = mdev->p_uuid[UI_BITMAP] & 1;
2380
2381 ch_peer = mdev->p_uuid[UI_SIZE];
2382 ch_self = mdev->comm_bm_set;
2383
Philipp Reisner89e58e72011-01-19 13:12:45 +01002384 switch (mdev->tconn->net_conf->after_sb_0p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002385 case ASB_CONSENSUS:
2386 case ASB_DISCARD_SECONDARY:
2387 case ASB_CALL_HELPER:
2388 dev_err(DEV, "Configuration error.\n");
2389 break;
2390 case ASB_DISCONNECT:
2391 break;
2392 case ASB_DISCARD_YOUNGER_PRI:
2393 if (self == 0 && peer == 1) {
2394 rv = -1;
2395 break;
2396 }
2397 if (self == 1 && peer == 0) {
2398 rv = 1;
2399 break;
2400 }
2401 /* Else fall through to one of the other strategies... */
2402 case ASB_DISCARD_OLDER_PRI:
2403 if (self == 0 && peer == 1) {
2404 rv = 1;
2405 break;
2406 }
2407 if (self == 1 && peer == 0) {
2408 rv = -1;
2409 break;
2410 }
2411 /* Else fall through to one of the other strategies... */
Lars Ellenbergad19bf62009-10-14 09:36:49 +02002412 dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
Philipp Reisnerb411b362009-09-25 16:07:19 -07002413 "Using discard-least-changes instead\n");
2414 case ASB_DISCARD_ZERO_CHG:
2415 if (ch_peer == 0 && ch_self == 0) {
Philipp Reisner25703f82011-02-07 14:35:25 +01002416 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002417 ? -1 : 1;
2418 break;
2419 } else {
2420 if (ch_peer == 0) { rv = 1; break; }
2421 if (ch_self == 0) { rv = -1; break; }
2422 }
Philipp Reisner89e58e72011-01-19 13:12:45 +01002423 if (mdev->tconn->net_conf->after_sb_0p == ASB_DISCARD_ZERO_CHG)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002424 break;
2425 case ASB_DISCARD_LEAST_CHG:
2426 if (ch_self < ch_peer)
2427 rv = -1;
2428 else if (ch_self > ch_peer)
2429 rv = 1;
2430 else /* ( ch_self == ch_peer ) */
2431 /* Well, then use something else. */
Philipp Reisner25703f82011-02-07 14:35:25 +01002432 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002433 ? -1 : 1;
2434 break;
2435 case ASB_DISCARD_LOCAL:
2436 rv = -1;
2437 break;
2438 case ASB_DISCARD_REMOTE:
2439 rv = 1;
2440 }
2441
2442 return rv;
2443}
2444
2445static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
2446{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002447 int hg, rv = -100;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002448
Philipp Reisner89e58e72011-01-19 13:12:45 +01002449 switch (mdev->tconn->net_conf->after_sb_1p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002450 case ASB_DISCARD_YOUNGER_PRI:
2451 case ASB_DISCARD_OLDER_PRI:
2452 case ASB_DISCARD_LEAST_CHG:
2453 case ASB_DISCARD_LOCAL:
2454 case ASB_DISCARD_REMOTE:
2455 dev_err(DEV, "Configuration error.\n");
2456 break;
2457 case ASB_DISCONNECT:
2458 break;
2459 case ASB_CONSENSUS:
2460 hg = drbd_asb_recover_0p(mdev);
2461 if (hg == -1 && mdev->state.role == R_SECONDARY)
2462 rv = hg;
2463 if (hg == 1 && mdev->state.role == R_PRIMARY)
2464 rv = hg;
2465 break;
2466 case ASB_VIOLENTLY:
2467 rv = drbd_asb_recover_0p(mdev);
2468 break;
2469 case ASB_DISCARD_SECONDARY:
2470 return mdev->state.role == R_PRIMARY ? 1 : -1;
2471 case ASB_CALL_HELPER:
2472 hg = drbd_asb_recover_0p(mdev);
2473 if (hg == -1 && mdev->state.role == R_PRIMARY) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002474 enum drbd_state_rv rv2;
2475
2476 drbd_set_role(mdev, R_SECONDARY, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002477 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2478 * we might be here in C_WF_REPORT_PARAMS which is transient.
2479 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002480 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2481 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002482 drbd_khelper(mdev, "pri-lost-after-sb");
2483 } else {
2484 dev_warn(DEV, "Successfully gave up primary role.\n");
2485 rv = hg;
2486 }
2487 } else
2488 rv = hg;
2489 }
2490
2491 return rv;
2492}
2493
2494static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
2495{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002496 int hg, rv = -100;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002497
Philipp Reisner89e58e72011-01-19 13:12:45 +01002498 switch (mdev->tconn->net_conf->after_sb_2p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002499 case ASB_DISCARD_YOUNGER_PRI:
2500 case ASB_DISCARD_OLDER_PRI:
2501 case ASB_DISCARD_LEAST_CHG:
2502 case ASB_DISCARD_LOCAL:
2503 case ASB_DISCARD_REMOTE:
2504 case ASB_CONSENSUS:
2505 case ASB_DISCARD_SECONDARY:
2506 dev_err(DEV, "Configuration error.\n");
2507 break;
2508 case ASB_VIOLENTLY:
2509 rv = drbd_asb_recover_0p(mdev);
2510 break;
2511 case ASB_DISCONNECT:
2512 break;
2513 case ASB_CALL_HELPER:
2514 hg = drbd_asb_recover_0p(mdev);
2515 if (hg == -1) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002516 enum drbd_state_rv rv2;
2517
Philipp Reisnerb411b362009-09-25 16:07:19 -07002518 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2519 * we might be here in C_WF_REPORT_PARAMS which is transient.
2520 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002521 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2522 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002523 drbd_khelper(mdev, "pri-lost-after-sb");
2524 } else {
2525 dev_warn(DEV, "Successfully gave up primary role.\n");
2526 rv = hg;
2527 }
2528 } else
2529 rv = hg;
2530 }
2531
2532 return rv;
2533}
2534
2535static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
2536 u64 bits, u64 flags)
2537{
2538 if (!uuid) {
2539 dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
2540 return;
2541 }
2542 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
2543 text,
2544 (unsigned long long)uuid[UI_CURRENT],
2545 (unsigned long long)uuid[UI_BITMAP],
2546 (unsigned long long)uuid[UI_HISTORY_START],
2547 (unsigned long long)uuid[UI_HISTORY_END],
2548 (unsigned long long)bits,
2549 (unsigned long long)flags);
2550}
2551
2552/*
2553 100 after split brain try auto recover
2554 2 C_SYNC_SOURCE set BitMap
2555 1 C_SYNC_SOURCE use BitMap
2556 0 no Sync
2557 -1 C_SYNC_TARGET use BitMap
2558 -2 C_SYNC_TARGET set BitMap
2559 -100 after split brain, disconnect
2560-1000 unrelated data
Philipp Reisner4a23f262011-01-11 17:42:17 +01002561-1091 requires proto 91
2562-1096 requires proto 96
Philipp Reisnerb411b362009-09-25 16:07:19 -07002563 */
2564static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
2565{
2566 u64 self, peer;
2567 int i, j;
2568
2569 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2570 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2571
2572 *rule_nr = 10;
2573 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
2574 return 0;
2575
2576 *rule_nr = 20;
2577 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
2578 peer != UUID_JUST_CREATED)
2579 return -2;
2580
2581 *rule_nr = 30;
2582 if (self != UUID_JUST_CREATED &&
2583 (peer == UUID_JUST_CREATED || peer == (u64)0))
2584 return 2;
2585
2586 if (self == peer) {
2587 int rct, dc; /* roles at crash time */
2588
2589 if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
2590
Philipp Reisner31890f42011-01-19 14:12:51 +01002591 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002592 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002593
2594 if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
2595 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
2596 dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
2597 drbd_uuid_set_bm(mdev, 0UL);
2598
2599 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2600 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2601 *rule_nr = 34;
2602 } else {
2603 dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
2604 *rule_nr = 36;
2605 }
2606
2607 return 1;
2608 }
2609
2610 if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
2611
Philipp Reisner31890f42011-01-19 14:12:51 +01002612 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002613 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002614
2615 if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
2616 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
2617 dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
2618
2619 mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
2620 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
2621 mdev->p_uuid[UI_BITMAP] = 0UL;
2622
2623 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2624 *rule_nr = 35;
2625 } else {
2626 dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
2627 *rule_nr = 37;
2628 }
2629
2630 return -1;
2631 }
2632
2633 /* Common power [off|failure] */
2634 rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
2635 (mdev->p_uuid[UI_FLAGS] & 2);
2636 /* lowest bit is set when we were primary,
2637 * next bit (weight 2) is set when peer was primary */
2638 *rule_nr = 40;
2639
2640 switch (rct) {
2641 case 0: /* !self_pri && !peer_pri */ return 0;
2642 case 1: /* self_pri && !peer_pri */ return 1;
2643 case 2: /* !self_pri && peer_pri */ return -1;
2644 case 3: /* self_pri && peer_pri */
Philipp Reisner25703f82011-02-07 14:35:25 +01002645 dc = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002646 return dc ? -1 : 1;
2647 }
2648 }
2649
2650 *rule_nr = 50;
2651 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2652 if (self == peer)
2653 return -1;
2654
2655 *rule_nr = 51;
2656 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2657 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002658 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002659 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) ==
2660 (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) :
2661 peer + UUID_NEW_BM_OFFSET == (mdev->p_uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002662 /* The last P_SYNC_UUID did not get though. Undo the last start of
2663 resync as sync source modifications of the peer's UUIDs. */
2664
Philipp Reisner31890f42011-01-19 14:12:51 +01002665 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002666 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002667
2668 mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
2669 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
Philipp Reisner4a23f262011-01-11 17:42:17 +01002670
2671 dev_info(DEV, "Did not got last syncUUID packet, corrected:\n");
2672 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2673
Philipp Reisnerb411b362009-09-25 16:07:19 -07002674 return -1;
2675 }
2676 }
2677
2678 *rule_nr = 60;
2679 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2680 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2681 peer = mdev->p_uuid[i] & ~((u64)1);
2682 if (self == peer)
2683 return -2;
2684 }
2685
2686 *rule_nr = 70;
2687 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2688 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2689 if (self == peer)
2690 return 1;
2691
2692 *rule_nr = 71;
2693 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2694 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002695 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002696 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) ==
2697 (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) :
2698 self + UUID_NEW_BM_OFFSET == (mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002699 /* The last P_SYNC_UUID did not get though. Undo the last start of
2700 resync as sync source modifications of our UUIDs. */
2701
Philipp Reisner31890f42011-01-19 14:12:51 +01002702 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002703 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002704
2705 _drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
2706 _drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
2707
Philipp Reisner4a23f262011-01-11 17:42:17 +01002708 dev_info(DEV, "Last syncUUID did not get through, corrected:\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002709 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2710 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2711
2712 return 1;
2713 }
2714 }
2715
2716
2717 *rule_nr = 80;
Philipp Reisnerd8c2a362009-11-18 15:52:51 +01002718 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002719 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2720 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2721 if (self == peer)
2722 return 2;
2723 }
2724
2725 *rule_nr = 90;
2726 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2727 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2728 if (self == peer && self != ((u64)0))
2729 return 100;
2730
2731 *rule_nr = 100;
2732 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2733 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2734 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
2735 peer = mdev->p_uuid[j] & ~((u64)1);
2736 if (self == peer)
2737 return -100;
2738 }
2739 }
2740
2741 return -1000;
2742}
2743
2744/* drbd_sync_handshake() returns the new conn state on success, or
2745 CONN_MASK (-1) on failure.
2746 */
2747static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
2748 enum drbd_disk_state peer_disk) __must_hold(local)
2749{
2750 int hg, rule_nr;
2751 enum drbd_conns rv = C_MASK;
2752 enum drbd_disk_state mydisk;
2753
2754 mydisk = mdev->state.disk;
2755 if (mydisk == D_NEGOTIATING)
2756 mydisk = mdev->new_state_tmp.disk;
2757
2758 dev_info(DEV, "drbd_sync_handshake:\n");
2759 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0);
2760 drbd_uuid_dump(mdev, "peer", mdev->p_uuid,
2761 mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2762
2763 hg = drbd_uuid_compare(mdev, &rule_nr);
2764
2765 dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
2766
2767 if (hg == -1000) {
2768 dev_alert(DEV, "Unrelated data, aborting!\n");
2769 return C_MASK;
2770 }
Philipp Reisner4a23f262011-01-11 17:42:17 +01002771 if (hg < -1000) {
2772 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 -07002773 return C_MASK;
2774 }
2775
2776 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
2777 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
2778 int f = (hg == -100) || abs(hg) == 2;
2779 hg = mydisk > D_INCONSISTENT ? 1 : -1;
2780 if (f)
2781 hg = hg*2;
2782 dev_info(DEV, "Becoming sync %s due to disk states.\n",
2783 hg > 0 ? "source" : "target");
2784 }
2785
Adam Gandelman3a11a482010-04-08 16:48:23 -07002786 if (abs(hg) == 100)
2787 drbd_khelper(mdev, "initial-split-brain");
2788
Philipp Reisner89e58e72011-01-19 13:12:45 +01002789 if (hg == 100 || (hg == -100 && mdev->tconn->net_conf->always_asbp)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002790 int pcount = (mdev->state.role == R_PRIMARY)
2791 + (peer_role == R_PRIMARY);
2792 int forced = (hg == -100);
2793
2794 switch (pcount) {
2795 case 0:
2796 hg = drbd_asb_recover_0p(mdev);
2797 break;
2798 case 1:
2799 hg = drbd_asb_recover_1p(mdev);
2800 break;
2801 case 2:
2802 hg = drbd_asb_recover_2p(mdev);
2803 break;
2804 }
2805 if (abs(hg) < 100) {
2806 dev_warn(DEV, "Split-Brain detected, %d primaries, "
2807 "automatically solved. Sync from %s node\n",
2808 pcount, (hg < 0) ? "peer" : "this");
2809 if (forced) {
2810 dev_warn(DEV, "Doing a full sync, since"
2811 " UUIDs where ambiguous.\n");
2812 hg = hg*2;
2813 }
2814 }
2815 }
2816
2817 if (hg == -100) {
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;
Philipp Reisner89e58e72011-01-19 13:12:45 +01002820 if (!mdev->tconn->net_conf->want_lose && (mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002821 hg = 1;
2822
2823 if (abs(hg) < 100)
2824 dev_warn(DEV, "Split-Brain detected, manually solved. "
2825 "Sync from %s node\n",
2826 (hg < 0) ? "peer" : "this");
2827 }
2828
2829 if (hg == -100) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01002830 /* FIXME this log message is not correct if we end up here
2831 * after an attempted attach on a diskless node.
2832 * We just refuse to attach -- well, we drop the "connection"
2833 * to that disk, in a way... */
Adam Gandelman3a11a482010-04-08 16:48:23 -07002834 dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002835 drbd_khelper(mdev, "split-brain");
2836 return C_MASK;
2837 }
2838
2839 if (hg > 0 && mydisk <= D_INCONSISTENT) {
2840 dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
2841 return C_MASK;
2842 }
2843
2844 if (hg < 0 && /* by intention we do not use mydisk here. */
2845 mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
Philipp Reisner89e58e72011-01-19 13:12:45 +01002846 switch (mdev->tconn->net_conf->rr_conflict) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002847 case ASB_CALL_HELPER:
2848 drbd_khelper(mdev, "pri-lost");
2849 /* fall through */
2850 case ASB_DISCONNECT:
2851 dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
2852 return C_MASK;
2853 case ASB_VIOLENTLY:
2854 dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
2855 "assumption\n");
2856 }
2857 }
2858
Philipp Reisner8169e412011-03-15 18:40:27 +01002859 if (mdev->tconn->net_conf->dry_run || test_bit(CONN_DRY_RUN, &mdev->tconn->flags)) {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002860 if (hg == 0)
2861 dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
2862 else
2863 dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
2864 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
2865 abs(hg) >= 2 ? "full" : "bit-map based");
2866 return C_MASK;
2867 }
2868
Philipp Reisnerb411b362009-09-25 16:07:19 -07002869 if (abs(hg) >= 2) {
2870 dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01002871 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake",
2872 BM_LOCKED_SET_ALLOWED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002873 return C_MASK;
2874 }
2875
2876 if (hg > 0) { /* become sync source. */
2877 rv = C_WF_BITMAP_S;
2878 } else if (hg < 0) { /* become sync target */
2879 rv = C_WF_BITMAP_T;
2880 } else {
2881 rv = C_CONNECTED;
2882 if (drbd_bm_total_weight(mdev)) {
2883 dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
2884 drbd_bm_total_weight(mdev));
2885 }
2886 }
2887
2888 return rv;
2889}
2890
2891/* returns 1 if invalid */
2892static int cmp_after_sb(enum drbd_after_sb_p peer, enum drbd_after_sb_p self)
2893{
2894 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
2895 if ((peer == ASB_DISCARD_REMOTE && self == ASB_DISCARD_LOCAL) ||
2896 (self == ASB_DISCARD_REMOTE && peer == ASB_DISCARD_LOCAL))
2897 return 0;
2898
2899 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
2900 if (peer == ASB_DISCARD_REMOTE || peer == ASB_DISCARD_LOCAL ||
2901 self == ASB_DISCARD_REMOTE || self == ASB_DISCARD_LOCAL)
2902 return 1;
2903
2904 /* everything else is valid if they are equal on both sides. */
2905 if (peer == self)
2906 return 0;
2907
2908 /* everything es is invalid. */
2909 return 1;
2910}
2911
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002912static int receive_protocol(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002913{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002914 struct p_protocol *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002915 int p_proto, p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002916 int p_want_lose, p_two_primaries, cf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002917 char p_integrity_alg[SHARED_SECRET_MAX] = "";
2918
Philipp Reisnerb411b362009-09-25 16:07:19 -07002919 p_proto = be32_to_cpu(p->protocol);
2920 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
2921 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
2922 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002923 p_two_primaries = be32_to_cpu(p->two_primaries);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002924 cf = be32_to_cpu(p->conn_flags);
2925 p_want_lose = cf & CF_WANT_LOSE;
2926
Philipp Reisner72046242011-03-15 18:51:47 +01002927 clear_bit(CONN_DRY_RUN, &tconn->flags);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01002928
2929 if (cf & CF_DRY_RUN)
Philipp Reisner72046242011-03-15 18:51:47 +01002930 set_bit(CONN_DRY_RUN, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002931
Philipp Reisner72046242011-03-15 18:51:47 +01002932 if (p_proto != tconn->net_conf->wire_protocol) {
2933 conn_err(tconn, "incompatible communication protocols\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002934 goto disconnect;
2935 }
2936
Philipp Reisner72046242011-03-15 18:51:47 +01002937 if (cmp_after_sb(p_after_sb_0p, tconn->net_conf->after_sb_0p)) {
2938 conn_err(tconn, "incompatible after-sb-0pri settings\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002939 goto disconnect;
2940 }
2941
Philipp Reisner72046242011-03-15 18:51:47 +01002942 if (cmp_after_sb(p_after_sb_1p, tconn->net_conf->after_sb_1p)) {
2943 conn_err(tconn, "incompatible after-sb-1pri settings\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002944 goto disconnect;
2945 }
2946
Philipp Reisner72046242011-03-15 18:51:47 +01002947 if (cmp_after_sb(p_after_sb_2p, tconn->net_conf->after_sb_2p)) {
2948 conn_err(tconn, "incompatible after-sb-2pri settings\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002949 goto disconnect;
2950 }
2951
Philipp Reisner72046242011-03-15 18:51:47 +01002952 if (p_want_lose && tconn->net_conf->want_lose) {
2953 conn_err(tconn, "both sides have the 'want_lose' flag set\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002954 goto disconnect;
2955 }
2956
Philipp Reisner72046242011-03-15 18:51:47 +01002957 if (p_two_primaries != tconn->net_conf->two_primaries) {
2958 conn_err(tconn, "incompatible setting of the two-primaries options\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002959 goto disconnect;
2960 }
2961
Philipp Reisner72046242011-03-15 18:51:47 +01002962 if (tconn->agreed_pro_version >= 87) {
2963 unsigned char *my_alg = tconn->net_conf->integrity_alg;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002964 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002965
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002966 err = drbd_recv_all(tconn, p_integrity_alg, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002967 if (err)
2968 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002969
2970 p_integrity_alg[SHARED_SECRET_MAX-1] = 0;
2971 if (strcmp(p_integrity_alg, my_alg)) {
Philipp Reisner72046242011-03-15 18:51:47 +01002972 conn_err(tconn, "incompatible setting of the data-integrity-alg\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002973 goto disconnect;
2974 }
Philipp Reisner72046242011-03-15 18:51:47 +01002975 conn_info(tconn, "data-integrity-alg: %s\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -07002976 my_alg[0] ? my_alg : (unsigned char *)"<not-used>");
2977 }
2978
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002979 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002980
2981disconnect:
Philipp Reisner72046242011-03-15 18:51:47 +01002982 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002983 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002984}
2985
2986/* helper function
2987 * input: alg name, feature name
2988 * return: NULL (alg name was "")
2989 * ERR_PTR(error) if something goes wrong
2990 * or the crypto hash ptr, if it worked out ok. */
2991struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
2992 const char *alg, const char *name)
2993{
2994 struct crypto_hash *tfm;
2995
2996 if (!alg[0])
2997 return NULL;
2998
2999 tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
3000 if (IS_ERR(tfm)) {
3001 dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
3002 alg, name, PTR_ERR(tfm));
3003 return tfm;
3004 }
3005 if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) {
3006 crypto_free_hash(tfm);
3007 dev_err(DEV, "\"%s\" is not a digest (%s)\n", alg, name);
3008 return ERR_PTR(-EINVAL);
3009 }
3010 return tfm;
3011}
3012
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003013static int ignore_remaining_packet(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003014{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003015 void *buffer = tconn->data.rbuf;
3016 int size = pi->size;
3017
3018 while (size) {
3019 int s = min_t(int, size, DRBD_SOCKET_BUFFER_SIZE);
3020 s = drbd_recv(tconn, buffer, s);
3021 if (s <= 0) {
3022 if (s < 0)
3023 return s;
3024 break;
3025 }
3026 size -= s;
3027 }
3028 if (size)
3029 return -EIO;
3030 return 0;
3031}
3032
3033/*
3034 * config_unknown_volume - device configuration command for unknown volume
3035 *
3036 * When a device is added to an existing connection, the node on which the
3037 * device is added first will send configuration commands to its peer but the
3038 * peer will not know about the device yet. It will warn and ignore these
3039 * commands. Once the device is added on the second node, the second node will
3040 * send the same device configuration commands, but in the other direction.
3041 *
3042 * (We can also end up here if drbd is misconfigured.)
3043 */
3044static int config_unknown_volume(struct drbd_tconn *tconn, struct packet_info *pi)
3045{
3046 conn_warn(tconn, "Volume %u unknown; ignoring %s packet\n",
3047 pi->vnr, cmdname(pi->cmd));
3048 return ignore_remaining_packet(tconn, pi);
3049}
3050
3051static int receive_SyncParam(struct drbd_tconn *tconn, struct packet_info *pi)
3052{
3053 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003054 struct p_rs_param_95 *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003055 unsigned int header_size, data_size, exp_max_sz;
3056 struct crypto_hash *verify_tfm = NULL;
3057 struct crypto_hash *csums_tfm = NULL;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003058 const int apv = tconn->agreed_pro_version;
Philipp Reisner778f2712010-07-06 11:14:00 +02003059 int *rs_plan_s = NULL;
3060 int fifo_size = 0;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003061 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003062
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003063 mdev = vnr_to_mdev(tconn, pi->vnr);
3064 if (!mdev)
3065 return config_unknown_volume(tconn, pi);
3066
Philipp Reisnerb411b362009-09-25 16:07:19 -07003067 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
3068 : apv == 88 ? sizeof(struct p_rs_param)
3069 + SHARED_SECRET_MAX
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003070 : apv <= 94 ? sizeof(struct p_rs_param_89)
3071 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003072
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003073 if (pi->size > exp_max_sz) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003074 dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003075 pi->size, exp_max_sz);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003076 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003077 }
3078
3079 if (apv <= 88) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003080 header_size = sizeof(struct p_rs_param);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003081 data_size = pi->size - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003082 } else if (apv <= 94) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003083 header_size = sizeof(struct p_rs_param_89);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003084 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003085 D_ASSERT(data_size == 0);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003086 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003087 header_size = sizeof(struct p_rs_param_95);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003088 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003089 D_ASSERT(data_size == 0);
3090 }
3091
3092 /* initialize verify_alg and csums_alg */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003093 p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003094 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
3095
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003096 err = drbd_recv_all(mdev->tconn, p, header_size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003097 if (err)
3098 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003099
Lars Ellenbergf3990022011-03-23 14:31:09 +01003100 if (get_ldev(mdev)) {
3101 mdev->ldev->dc.resync_rate = be32_to_cpu(p->rate);
3102 put_ldev(mdev);
3103 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003104
3105 if (apv >= 88) {
3106 if (apv == 88) {
3107 if (data_size > SHARED_SECRET_MAX) {
3108 dev_err(DEV, "verify-alg too long, "
3109 "peer wants %u, accepting only %u byte\n",
3110 data_size, SHARED_SECRET_MAX);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003111 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003112 }
3113
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003114 err = drbd_recv_all(mdev->tconn, p->verify_alg, data_size);
3115 if (err)
3116 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003117
3118 /* we expect NUL terminated string */
3119 /* but just in case someone tries to be evil */
3120 D_ASSERT(p->verify_alg[data_size-1] == 0);
3121 p->verify_alg[data_size-1] = 0;
3122
3123 } else /* apv >= 89 */ {
3124 /* we still expect NUL terminated strings */
3125 /* but just in case someone tries to be evil */
3126 D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
3127 D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
3128 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
3129 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
3130 }
3131
Lars Ellenbergf3990022011-03-23 14:31:09 +01003132 if (strcmp(mdev->tconn->net_conf->verify_alg, p->verify_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003133 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3134 dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
Lars Ellenbergf3990022011-03-23 14:31:09 +01003135 mdev->tconn->net_conf->verify_alg, p->verify_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003136 goto disconnect;
3137 }
3138 verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
3139 p->verify_alg, "verify-alg");
3140 if (IS_ERR(verify_tfm)) {
3141 verify_tfm = NULL;
3142 goto disconnect;
3143 }
3144 }
3145
Lars Ellenbergf3990022011-03-23 14:31:09 +01003146 if (apv >= 89 && strcmp(mdev->tconn->net_conf->csums_alg, p->csums_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003147 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3148 dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
Lars Ellenbergf3990022011-03-23 14:31:09 +01003149 mdev->tconn->net_conf->csums_alg, p->csums_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003150 goto disconnect;
3151 }
3152 csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
3153 p->csums_alg, "csums-alg");
3154 if (IS_ERR(csums_tfm)) {
3155 csums_tfm = NULL;
3156 goto disconnect;
3157 }
3158 }
3159
Lars Ellenbergf3990022011-03-23 14:31:09 +01003160 if (apv > 94 && get_ldev(mdev)) {
3161 mdev->ldev->dc.resync_rate = be32_to_cpu(p->rate);
3162 mdev->ldev->dc.c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
3163 mdev->ldev->dc.c_delay_target = be32_to_cpu(p->c_delay_target);
3164 mdev->ldev->dc.c_fill_target = be32_to_cpu(p->c_fill_target);
3165 mdev->ldev->dc.c_max_rate = be32_to_cpu(p->c_max_rate);
Philipp Reisner778f2712010-07-06 11:14:00 +02003166
Lars Ellenbergf3990022011-03-23 14:31:09 +01003167 fifo_size = (mdev->ldev->dc.c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Philipp Reisner778f2712010-07-06 11:14:00 +02003168 if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) {
3169 rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL);
3170 if (!rs_plan_s) {
3171 dev_err(DEV, "kmalloc of fifo_buffer failed");
Lars Ellenbergf3990022011-03-23 14:31:09 +01003172 put_ldev(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02003173 goto disconnect;
3174 }
3175 }
Lars Ellenbergf3990022011-03-23 14:31:09 +01003176 put_ldev(mdev);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003177 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003178
3179 spin_lock(&mdev->peer_seq_lock);
3180 /* lock against drbd_nl_syncer_conf() */
3181 if (verify_tfm) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01003182 strcpy(mdev->tconn->net_conf->verify_alg, p->verify_alg);
3183 mdev->tconn->net_conf->verify_alg_len = strlen(p->verify_alg) + 1;
3184 crypto_free_hash(mdev->tconn->verify_tfm);
3185 mdev->tconn->verify_tfm = verify_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003186 dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
3187 }
3188 if (csums_tfm) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01003189 strcpy(mdev->tconn->net_conf->csums_alg, p->csums_alg);
3190 mdev->tconn->net_conf->csums_alg_len = strlen(p->csums_alg) + 1;
3191 crypto_free_hash(mdev->tconn->csums_tfm);
3192 mdev->tconn->csums_tfm = csums_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003193 dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
3194 }
Philipp Reisner778f2712010-07-06 11:14:00 +02003195 if (fifo_size != mdev->rs_plan_s.size) {
3196 kfree(mdev->rs_plan_s.values);
3197 mdev->rs_plan_s.values = rs_plan_s;
3198 mdev->rs_plan_s.size = fifo_size;
3199 mdev->rs_planed = 0;
3200 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003201 spin_unlock(&mdev->peer_seq_lock);
3202 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003203 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003204
Philipp Reisnerb411b362009-09-25 16:07:19 -07003205disconnect:
3206 /* just for completeness: actually not needed,
3207 * as this is not reached if csums_tfm was ok. */
3208 crypto_free_hash(csums_tfm);
3209 /* but free the verify_tfm again, if csums_tfm did not work out */
3210 crypto_free_hash(verify_tfm);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003211 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003212 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003213}
3214
Philipp Reisnerb411b362009-09-25 16:07:19 -07003215/* warn if the arguments differ by more than 12.5% */
3216static void warn_if_differ_considerably(struct drbd_conf *mdev,
3217 const char *s, sector_t a, sector_t b)
3218{
3219 sector_t d;
3220 if (a == 0 || b == 0)
3221 return;
3222 d = (a > b) ? (a - b) : (b - a);
3223 if (d > (a>>3) || d > (b>>3))
3224 dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
3225 (unsigned long long)a, (unsigned long long)b);
3226}
3227
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003228static int receive_sizes(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003229{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003230 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003231 struct p_sizes *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003232 enum determine_dev_size dd = unchanged;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003233 sector_t p_size, p_usize, my_usize;
3234 int ldsc = 0; /* local disk size changed */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003235 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003236
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003237 mdev = vnr_to_mdev(tconn, pi->vnr);
3238 if (!mdev)
3239 return config_unknown_volume(tconn, pi);
3240
Philipp Reisnerb411b362009-09-25 16:07:19 -07003241 p_size = be64_to_cpu(p->d_size);
3242 p_usize = be64_to_cpu(p->u_size);
3243
Philipp Reisnerb411b362009-09-25 16:07:19 -07003244 /* just store the peer's disk size for now.
3245 * we still need to figure out whether we accept that. */
3246 mdev->p_size = p_size;
3247
Philipp Reisnerb411b362009-09-25 16:07:19 -07003248 if (get_ldev(mdev)) {
3249 warn_if_differ_considerably(mdev, "lower level device sizes",
3250 p_size, drbd_get_max_capacity(mdev->ldev));
3251 warn_if_differ_considerably(mdev, "user requested size",
3252 p_usize, mdev->ldev->dc.disk_size);
3253
3254 /* if this is the first connect, or an otherwise expected
3255 * param exchange, choose the minimum */
3256 if (mdev->state.conn == C_WF_REPORT_PARAMS)
3257 p_usize = min_not_zero((sector_t)mdev->ldev->dc.disk_size,
3258 p_usize);
3259
3260 my_usize = mdev->ldev->dc.disk_size;
3261
3262 if (mdev->ldev->dc.disk_size != p_usize) {
3263 mdev->ldev->dc.disk_size = p_usize;
3264 dev_info(DEV, "Peer sets u_size to %lu sectors\n",
3265 (unsigned long)mdev->ldev->dc.disk_size);
3266 }
3267
3268 /* Never shrink a device with usable data during connect.
3269 But allow online shrinking if we are connected. */
Philipp Reisnera393db62009-12-22 13:35:52 +01003270 if (drbd_new_dev_size(mdev, mdev->ldev, 0) <
Philipp Reisnerb411b362009-09-25 16:07:19 -07003271 drbd_get_capacity(mdev->this_bdev) &&
3272 mdev->state.disk >= D_OUTDATED &&
3273 mdev->state.conn < C_CONNECTED) {
3274 dev_err(DEV, "The peer's disk size is too small!\n");
Philipp Reisner38fa9982011-03-15 18:24:49 +01003275 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003276 mdev->ldev->dc.disk_size = my_usize;
3277 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003278 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003279 }
3280 put_ldev(mdev);
3281 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003282
Philipp Reisnere89b5912010-03-24 17:11:33 +01003283 ddsf = be16_to_cpu(p->dds_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003284 if (get_ldev(mdev)) {
Bart Van Assche24c48302011-05-21 18:32:29 +02003285 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003286 put_ldev(mdev);
3287 if (dd == dev_size_error)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003288 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003289 drbd_md_sync(mdev);
3290 } else {
3291 /* I am diskless, need to accept the peer's size. */
3292 drbd_set_my_capacity(mdev, p_size);
3293 }
3294
Philipp Reisner99432fc2011-05-20 16:39:13 +02003295 mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size);
3296 drbd_reconsider_max_bio_size(mdev);
3297
Philipp Reisnerb411b362009-09-25 16:07:19 -07003298 if (get_ldev(mdev)) {
3299 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
3300 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
3301 ldsc = 1;
3302 }
3303
Philipp Reisnerb411b362009-09-25 16:07:19 -07003304 put_ldev(mdev);
3305 }
3306
3307 if (mdev->state.conn > C_WF_REPORT_PARAMS) {
3308 if (be64_to_cpu(p->c_size) !=
3309 drbd_get_capacity(mdev->this_bdev) || ldsc) {
3310 /* we have different sizes, probably peer
3311 * needs to know my new size... */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003312 drbd_send_sizes(mdev, 0, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003313 }
3314 if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
3315 (dd == grew && mdev->state.conn == C_CONNECTED)) {
3316 if (mdev->state.pdsk >= D_INCONSISTENT &&
Philipp Reisnere89b5912010-03-24 17:11:33 +01003317 mdev->state.disk >= D_INCONSISTENT) {
3318 if (ddsf & DDSF_NO_RESYNC)
3319 dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
3320 else
3321 resync_after_online_grow(mdev);
3322 } else
Philipp Reisnerb411b362009-09-25 16:07:19 -07003323 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
3324 }
3325 }
3326
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003327 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003328}
3329
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003330static int receive_uuids(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003331{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003332 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003333 struct p_uuids *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003334 u64 *p_uuid;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003335 int i, updated_uuids = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003336
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003337 mdev = vnr_to_mdev(tconn, pi->vnr);
3338 if (!mdev)
3339 return config_unknown_volume(tconn, pi);
3340
Philipp Reisnerb411b362009-09-25 16:07:19 -07003341 p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
3342
3343 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
3344 p_uuid[i] = be64_to_cpu(p->uuid[i]);
3345
3346 kfree(mdev->p_uuid);
3347 mdev->p_uuid = p_uuid;
3348
3349 if (mdev->state.conn < C_CONNECTED &&
3350 mdev->state.disk < D_INCONSISTENT &&
3351 mdev->state.role == R_PRIMARY &&
3352 (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
3353 dev_err(DEV, "Can only connect to data with current UUID=%016llX\n",
3354 (unsigned long long)mdev->ed_uuid);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003355 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003356 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003357 }
3358
3359 if (get_ldev(mdev)) {
3360 int skip_initial_sync =
3361 mdev->state.conn == C_CONNECTED &&
Philipp Reisner31890f42011-01-19 14:12:51 +01003362 mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003363 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
3364 (p_uuid[UI_FLAGS] & 8);
3365 if (skip_initial_sync) {
3366 dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n");
3367 drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003368 "clear_n_write from receive_uuids",
3369 BM_LOCKED_TEST_ALLOWED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003370 _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]);
3371 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3372 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3373 CS_VERBOSE, NULL);
3374 drbd_md_sync(mdev);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003375 updated_uuids = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003376 }
3377 put_ldev(mdev);
Philipp Reisner18a50fa2010-06-21 14:14:15 +02003378 } else if (mdev->state.disk < D_INCONSISTENT &&
3379 mdev->state.role == R_PRIMARY) {
3380 /* I am a diskless primary, the peer just created a new current UUID
3381 for me. */
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003382 updated_uuids = drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003383 }
3384
3385 /* Before we test for the disk state, we should wait until an eventually
3386 ongoing cluster wide state change is finished. That is important if
3387 we are primary and are detaching from our disk. We need to see the
3388 new disk state... */
Philipp Reisner8410da82011-02-11 20:11:10 +01003389 mutex_lock(mdev->state_mutex);
3390 mutex_unlock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003391 if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT)
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003392 updated_uuids |= drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
3393
3394 if (updated_uuids)
3395 drbd_print_uuids(mdev, "receiver updated UUIDs to");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003396
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003397 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003398}
3399
3400/**
3401 * convert_state() - Converts the peer's view of the cluster state to our point of view
3402 * @ps: The state as seen by the peer.
3403 */
3404static union drbd_state convert_state(union drbd_state ps)
3405{
3406 union drbd_state ms;
3407
3408 static enum drbd_conns c_tab[] = {
3409 [C_CONNECTED] = C_CONNECTED,
3410
3411 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
3412 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
3413 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
3414 [C_VERIFY_S] = C_VERIFY_T,
3415 [C_MASK] = C_MASK,
3416 };
3417
3418 ms.i = ps.i;
3419
3420 ms.conn = c_tab[ps.conn];
3421 ms.peer = ps.role;
3422 ms.role = ps.peer;
3423 ms.pdsk = ps.disk;
3424 ms.disk = ps.pdsk;
3425 ms.peer_isp = (ps.aftr_isp | ps.user_isp);
3426
3427 return ms;
3428}
3429
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003430static int receive_req_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003431{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003432 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003433 struct p_req_state *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003434 union drbd_state mask, val;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01003435 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003436
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003437 mdev = vnr_to_mdev(tconn, pi->vnr);
3438 if (!mdev)
3439 return -EIO;
3440
Philipp Reisnerb411b362009-09-25 16:07:19 -07003441 mask.i = be32_to_cpu(p->mask);
3442 val.i = be32_to_cpu(p->val);
3443
Philipp Reisner25703f82011-02-07 14:35:25 +01003444 if (test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags) &&
Philipp Reisner8410da82011-02-11 20:11:10 +01003445 mutex_is_locked(mdev->state_mutex)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003446 drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003447 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003448 }
3449
3450 mask = convert_state(mask);
3451 val = convert_state(val);
3452
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003453 rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
3454 drbd_send_sr_reply(mdev, rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003455
Philipp Reisnerb411b362009-09-25 16:07:19 -07003456 drbd_md_sync(mdev);
3457
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003458 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003459}
3460
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003461static int receive_req_conn_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003462{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003463 struct p_req_state *p = pi->data;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003464 union drbd_state mask, val;
3465 enum drbd_state_rv rv;
3466
3467 mask.i = be32_to_cpu(p->mask);
3468 val.i = be32_to_cpu(p->val);
3469
3470 if (test_bit(DISCARD_CONCURRENT, &tconn->flags) &&
3471 mutex_is_locked(&tconn->cstate_mutex)) {
3472 conn_send_sr_reply(tconn, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003473 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003474 }
3475
3476 mask = convert_state(mask);
3477 val = convert_state(val);
3478
Philipp Reisner778bcf22011-03-28 12:55:03 +02003479 rv = conn_request_state(tconn, mask, val, CS_VERBOSE | CS_LOCAL_ONLY | CS_IGN_OUTD_FAIL);
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003480 conn_send_sr_reply(tconn, rv);
3481
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003482 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003483}
3484
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003485static int receive_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003486{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003487 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003488 struct p_state *p = pi->data;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003489 union drbd_state os, ns, peer_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003490 enum drbd_disk_state real_peer_disk;
Philipp Reisner65d922c2010-06-16 16:18:09 +02003491 enum chg_state_flags cs_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003492 int rv;
3493
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003494 mdev = vnr_to_mdev(tconn, pi->vnr);
3495 if (!mdev)
3496 return config_unknown_volume(tconn, pi);
3497
Philipp Reisnerb411b362009-09-25 16:07:19 -07003498 peer_state.i = be32_to_cpu(p->state);
3499
3500 real_peer_disk = peer_state.disk;
3501 if (peer_state.disk == D_NEGOTIATING) {
3502 real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
3503 dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
3504 }
3505
Philipp Reisner87eeee42011-01-19 14:16:30 +01003506 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003507 retry:
Philipp Reisner78bae592011-03-28 15:40:12 +02003508 os = ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003509 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003510
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003511 /* peer says his disk is uptodate, while we think it is inconsistent,
3512 * and this happens while we think we have a sync going on. */
3513 if (os.pdsk == D_INCONSISTENT && real_peer_disk == D_UP_TO_DATE &&
3514 os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) {
3515 /* If we are (becoming) SyncSource, but peer is still in sync
3516 * preparation, ignore its uptodate-ness to avoid flapping, it
3517 * will change to inconsistent once the peer reaches active
3518 * syncing states.
3519 * It may have changed syncer-paused flags, however, so we
3520 * cannot ignore this completely. */
3521 if (peer_state.conn > C_CONNECTED &&
3522 peer_state.conn < C_SYNC_SOURCE)
3523 real_peer_disk = D_INCONSISTENT;
3524
3525 /* if peer_state changes to connected at the same time,
3526 * it explicitly notifies us that it finished resync.
3527 * Maybe we should finish it up, too? */
3528 else if (os.conn >= C_SYNC_SOURCE &&
3529 peer_state.conn == C_CONNECTED) {
3530 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed)
3531 drbd_resync_finished(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003532 return 0;
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003533 }
3534 }
3535
3536 /* peer says his disk is inconsistent, while we think it is uptodate,
3537 * and this happens while the peer still thinks we have a sync going on,
3538 * but we think we are already done with the sync.
3539 * We ignore this to avoid flapping pdsk.
3540 * This should not happen, if the peer is a recent version of drbd. */
3541 if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT &&
3542 os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE)
3543 real_peer_disk = D_UP_TO_DATE;
3544
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003545 if (ns.conn == C_WF_REPORT_PARAMS)
3546 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003547
Philipp Reisner67531712010-10-27 12:21:30 +02003548 if (peer_state.conn == C_AHEAD)
3549 ns.conn = C_BEHIND;
3550
Philipp Reisnerb411b362009-09-25 16:07:19 -07003551 if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING &&
3552 get_ldev_if_state(mdev, D_NEGOTIATING)) {
3553 int cr; /* consider resync */
3554
3555 /* if we established a new connection */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003556 cr = (os.conn < C_CONNECTED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003557 /* if we had an established connection
3558 * and one of the nodes newly attaches a disk */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003559 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003560 (peer_state.disk == D_NEGOTIATING ||
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003561 os.disk == D_NEGOTIATING));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003562 /* if we have both been inconsistent, and the peer has been
3563 * forced to be UpToDate with --overwrite-data */
3564 cr |= test_bit(CONSIDER_RESYNC, &mdev->flags);
3565 /* if we had been plain connected, and the admin requested to
3566 * start a sync by "invalidate" or "invalidate-remote" */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003567 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003568 (peer_state.conn >= C_STARTING_SYNC_S &&
3569 peer_state.conn <= C_WF_BITMAP_T));
3570
3571 if (cr)
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003572 ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003573
3574 put_ldev(mdev);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003575 if (ns.conn == C_MASK) {
3576 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003577 if (mdev->state.disk == D_NEGOTIATING) {
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003578 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003579 } else if (peer_state.disk == D_NEGOTIATING) {
3580 dev_err(DEV, "Disk attach process on the peer node was aborted.\n");
3581 peer_state.disk = D_DISKLESS;
Lars Ellenberg580b9762010-02-26 23:15:23 +01003582 real_peer_disk = D_DISKLESS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003583 } else {
Philipp Reisner8169e412011-03-15 18:40:27 +01003584 if (test_and_clear_bit(CONN_DRY_RUN, &mdev->tconn->flags))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003585 return -EIO;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003586 D_ASSERT(os.conn == C_WF_REPORT_PARAMS);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003587 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003588 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003589 }
3590 }
3591 }
3592
Philipp Reisner87eeee42011-01-19 14:16:30 +01003593 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner78bae592011-03-28 15:40:12 +02003594 if (os.i != drbd_read_state(mdev).i)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003595 goto retry;
3596 clear_bit(CONSIDER_RESYNC, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003597 ns.peer = peer_state.role;
3598 ns.pdsk = real_peer_disk;
3599 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003600 if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003601 ns.disk = mdev->new_state_tmp.disk;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003602 cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD);
Philipp Reisner2aebfab2011-03-28 16:48:11 +02003603 if (ns.pdsk == D_CONSISTENT && drbd_suspended(mdev) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED &&
Philipp Reisner481c6f52010-06-22 14:03:27 +02003604 test_bit(NEW_CUR_UUID, &mdev->flags)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01003605 /* Do not allow tl_restart(RESEND) for a rebooted peer. We can only allow this
Philipp Reisner481c6f52010-06-22 14:03:27 +02003606 for temporal network outages! */
Philipp Reisner87eeee42011-01-19 14:16:30 +01003607 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003608 dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01003609 tl_clear(mdev->tconn);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003610 drbd_uuid_new_current(mdev);
3611 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003612 conn_request_state(mdev->tconn, NS2(conn, C_PROTOCOL_ERROR, susp, 0), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003613 return -EIO;
Philipp Reisner481c6f52010-06-22 14:03:27 +02003614 }
Philipp Reisner65d922c2010-06-16 16:18:09 +02003615 rv = _drbd_set_state(mdev, ns, cs_flags, NULL);
Philipp Reisner78bae592011-03-28 15:40:12 +02003616 ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003617 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003618
3619 if (rv < SS_SUCCESS) {
Philipp Reisner38fa9982011-03-15 18:24:49 +01003620 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003621 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003622 }
3623
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003624 if (os.conn > C_WF_REPORT_PARAMS) {
3625 if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003626 peer_state.disk != D_NEGOTIATING ) {
3627 /* we want resync, peer has not yet decided to sync... */
3628 /* Nowadays only used when forcing a node into primary role and
3629 setting its disk to UpToDate with that */
3630 drbd_send_uuids(mdev);
3631 drbd_send_state(mdev);
3632 }
3633 }
3634
Philipp Reisner89e58e72011-01-19 13:12:45 +01003635 mdev->tconn->net_conf->want_lose = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003636
3637 drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
3638
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003639 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003640}
3641
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003642static int receive_sync_uuid(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003643{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003644 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003645 struct p_rs_uuid *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003646
3647 mdev = vnr_to_mdev(tconn, pi->vnr);
3648 if (!mdev)
3649 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003650
3651 wait_event(mdev->misc_wait,
3652 mdev->state.conn == C_WF_SYNC_UUID ||
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02003653 mdev->state.conn == C_BEHIND ||
Philipp Reisnerb411b362009-09-25 16:07:19 -07003654 mdev->state.conn < C_CONNECTED ||
3655 mdev->state.disk < D_NEGOTIATING);
3656
3657 /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */
3658
Philipp Reisnerb411b362009-09-25 16:07:19 -07003659 /* Here the _drbd_uuid_ functions are right, current should
3660 _not_ be rotated into the history */
3661 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
3662 _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid));
3663 _drbd_uuid_set(mdev, UI_BITMAP, 0UL);
3664
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003665 drbd_print_uuids(mdev, "updated sync uuid");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003666 drbd_start_resync(mdev, C_SYNC_TARGET);
3667
3668 put_ldev(mdev);
3669 } else
3670 dev_err(DEV, "Ignoring SyncUUID packet!\n");
3671
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003672 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003673}
3674
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003675/**
3676 * receive_bitmap_plain
3677 *
3678 * Return 0 when done, 1 when another iteration is needed, and a negative error
3679 * code upon failure.
3680 */
3681static int
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003682receive_bitmap_plain(struct drbd_conf *mdev, unsigned int size,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003683 unsigned long *p, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003684{
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003685 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE -
3686 drbd_header_size(mdev->tconn);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003687 unsigned int num_words = min_t(size_t, data_size / sizeof(*p),
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003688 c->bm_words - c->word_offset);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003689 unsigned int want = num_words * sizeof(*p);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003690 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003691
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003692 if (want != size) {
3693 dev_err(DEV, "%s:want (%u) != size (%u)\n", __func__, want, size);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003694 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003695 }
3696 if (want == 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003697 return 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003698 err = drbd_recv_all(mdev->tconn, p, want);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003699 if (err)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003700 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003701
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003702 drbd_bm_merge_lel(mdev, c->word_offset, num_words, p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003703
3704 c->word_offset += num_words;
3705 c->bit_offset = c->word_offset * BITS_PER_LONG;
3706 if (c->bit_offset > c->bm_bits)
3707 c->bit_offset = c->bm_bits;
3708
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003709 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003710}
3711
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003712static enum drbd_bitmap_code dcbp_get_code(struct p_compressed_bm *p)
3713{
3714 return (enum drbd_bitmap_code)(p->encoding & 0x0f);
3715}
3716
3717static int dcbp_get_start(struct p_compressed_bm *p)
3718{
3719 return (p->encoding & 0x80) != 0;
3720}
3721
3722static int dcbp_get_pad_bits(struct p_compressed_bm *p)
3723{
3724 return (p->encoding >> 4) & 0x7;
3725}
3726
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003727/**
3728 * recv_bm_rle_bits
3729 *
3730 * Return 0 when done, 1 when another iteration is needed, and a negative error
3731 * code upon failure.
3732 */
3733static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07003734recv_bm_rle_bits(struct drbd_conf *mdev,
3735 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003736 struct bm_xfer_ctx *c,
3737 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003738{
3739 struct bitstream bs;
3740 u64 look_ahead;
3741 u64 rl;
3742 u64 tmp;
3743 unsigned long s = c->bit_offset;
3744 unsigned long e;
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003745 int toggle = dcbp_get_start(p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003746 int have;
3747 int bits;
3748
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003749 bitstream_init(&bs, p->code, len, dcbp_get_pad_bits(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003750
3751 bits = bitstream_get_bits(&bs, &look_ahead, 64);
3752 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003753 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003754
3755 for (have = bits; have > 0; s += rl, toggle = !toggle) {
3756 bits = vli_decode_bits(&rl, look_ahead);
3757 if (bits <= 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003758 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003759
3760 if (toggle) {
3761 e = s + rl -1;
3762 if (e >= c->bm_bits) {
3763 dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003764 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003765 }
3766 _drbd_bm_set_bits(mdev, s, e);
3767 }
3768
3769 if (have < bits) {
3770 dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
3771 have, bits, look_ahead,
3772 (unsigned int)(bs.cur.b - p->code),
3773 (unsigned int)bs.buf_len);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003774 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003775 }
3776 look_ahead >>= bits;
3777 have -= bits;
3778
3779 bits = bitstream_get_bits(&bs, &tmp, 64 - have);
3780 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003781 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003782 look_ahead |= tmp << have;
3783 have += bits;
3784 }
3785
3786 c->bit_offset = s;
3787 bm_xfer_ctx_bit_to_word_offset(c);
3788
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003789 return (s != c->bm_bits);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003790}
3791
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003792/**
3793 * decode_bitmap_c
3794 *
3795 * Return 0 when done, 1 when another iteration is needed, and a negative error
3796 * code upon failure.
3797 */
3798static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07003799decode_bitmap_c(struct drbd_conf *mdev,
3800 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01003801 struct bm_xfer_ctx *c,
3802 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003803{
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003804 if (dcbp_get_code(p) == RLE_VLI_Bits)
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003805 return recv_bm_rle_bits(mdev, p, c, len - sizeof(*p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003806
3807 /* other variants had been implemented for evaluation,
3808 * but have been dropped as this one turned out to be "best"
3809 * during all our tests. */
3810
3811 dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003812 conn_request_state(mdev->tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003813 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003814}
3815
3816void INFO_bm_xfer_stats(struct drbd_conf *mdev,
3817 const char *direction, struct bm_xfer_ctx *c)
3818{
3819 /* what would it take to transfer it "plaintext" */
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003820 unsigned int header_size = drbd_header_size(mdev->tconn);
3821 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE - header_size;
3822 unsigned int plain =
3823 header_size * (DIV_ROUND_UP(c->bm_words, data_size) + 1) +
3824 c->bm_words * sizeof(unsigned long);
3825 unsigned int total = c->bytes[0] + c->bytes[1];
3826 unsigned int r;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003827
3828 /* total can not be zero. but just in case: */
3829 if (total == 0)
3830 return;
3831
3832 /* don't report if not compressed */
3833 if (total >= plain)
3834 return;
3835
3836 /* total < plain. check for overflow, still */
3837 r = (total > UINT_MAX/1000) ? (total / (plain/1000))
3838 : (1000 * total / plain);
3839
3840 if (r > 1000)
3841 r = 1000;
3842
3843 r = 1000 - r;
3844 dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
3845 "total %u; compression: %u.%u%%\n",
3846 direction,
3847 c->bytes[1], c->packets[1],
3848 c->bytes[0], c->packets[0],
3849 total, r/10, r % 10);
3850}
3851
3852/* Since we are processing the bitfield from lower addresses to higher,
3853 it does not matter if the process it in 32 bit chunks or 64 bit
3854 chunks as long as it is little endian. (Understand it as byte stream,
3855 beginning with the lowest byte...) If we would use big endian
3856 we would need to process it from the highest address to the lowest,
3857 in order to be agnostic to the 32 vs 64 bits issue.
3858
3859 returns 0 on failure, 1 if we successfully received it. */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003860static int receive_bitmap(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003861{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003862 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003863 struct bm_xfer_ctx c;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003864 int err;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003865
3866 mdev = vnr_to_mdev(tconn, pi->vnr);
3867 if (!mdev)
3868 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003869
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003870 drbd_bm_lock(mdev, "receive bitmap", BM_LOCKED_SET_ALLOWED);
3871 /* you are supposed to send additional out-of-sync information
3872 * if you actually set bits during this phase */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003873
Philipp Reisnerb411b362009-09-25 16:07:19 -07003874 c = (struct bm_xfer_ctx) {
3875 .bm_bits = drbd_bm_bits(mdev),
3876 .bm_words = drbd_bm_words(mdev),
3877 };
3878
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003879 for(;;) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003880 if (pi->cmd == P_BITMAP)
3881 err = receive_bitmap_plain(mdev, pi->size, pi->data, &c);
3882 else if (pi->cmd == P_COMPRESSED_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003883 /* MAYBE: sanity check that we speak proto >= 90,
3884 * and the feature is enabled! */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003885 struct p_compressed_bm *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003886
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003887 if (pi->size > DRBD_SOCKET_BUFFER_SIZE - drbd_header_size(tconn)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003888 dev_err(DEV, "ReportCBitmap packet too large\n");
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003889 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003890 goto out;
3891 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003892 if (pi->size <= sizeof(*p)) {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003893 dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003894 err = -EIO;
Andreas Gruenbacher78fcbda2010-12-10 22:18:27 +01003895 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003896 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003897 err = drbd_recv_all(mdev->tconn, p, pi->size);
3898 if (err)
3899 goto out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003900 err = decode_bitmap_c(mdev, p, &c, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003901 } else {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003902 dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", pi->cmd);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003903 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003904 goto out;
3905 }
3906
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003907 c.packets[pi->cmd == P_BITMAP]++;
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003908 c.bytes[pi->cmd == P_BITMAP] += drbd_header_size(tconn) + pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003909
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003910 if (err <= 0) {
3911 if (err < 0)
3912 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003913 break;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003914 }
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003915 err = drbd_recv_header(mdev->tconn, pi);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003916 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003917 goto out;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003918 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003919
3920 INFO_bm_xfer_stats(mdev, "receive", &c);
3921
3922 if (mdev->state.conn == C_WF_BITMAP_T) {
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01003923 enum drbd_state_rv rv;
3924
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003925 err = drbd_send_bitmap(mdev);
3926 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003927 goto out;
3928 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01003929 rv = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
3930 D_ASSERT(rv == SS_SUCCESS);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003931 } else if (mdev->state.conn != C_WF_BITMAP_S) {
3932 /* admin may have requested C_DISCONNECTING,
3933 * other threads may have noticed network errors */
3934 dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n",
3935 drbd_conn_str(mdev->state.conn));
3936 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003937 err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003938
Philipp Reisnerb411b362009-09-25 16:07:19 -07003939 out:
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003940 drbd_bm_unlock(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003941 if (!err && mdev->state.conn == C_WF_BITMAP_S)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003942 drbd_start_resync(mdev, C_SYNC_SOURCE);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003943 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003944}
3945
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003946static int receive_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003947{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003948 conn_warn(tconn, "skipping unknown optional packet type %d, l: %d!\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003949 pi->cmd, pi->size);
Philipp Reisner2de876e2011-03-15 14:38:01 +01003950
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003951 return ignore_remaining_packet(tconn, pi);
Philipp Reisner2de876e2011-03-15 14:38:01 +01003952}
3953
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003954static int receive_UnplugRemote(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003955{
Philipp Reisnerb411b362009-09-25 16:07:19 -07003956 /* Make sure we've acked all the TCP data associated
3957 * with the data requests being unplugged */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003958 drbd_tcp_quickack(tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003959
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003960 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003961}
3962
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003963static int receive_out_of_sync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner73a01a12010-10-27 14:33:00 +02003964{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003965 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003966 struct p_block_desc *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003967
3968 mdev = vnr_to_mdev(tconn, pi->vnr);
3969 if (!mdev)
3970 return -EIO;
Philipp Reisner73a01a12010-10-27 14:33:00 +02003971
Lars Ellenbergf735e3632010-12-17 21:06:18 +01003972 switch (mdev->state.conn) {
3973 case C_WF_SYNC_UUID:
3974 case C_WF_BITMAP_T:
3975 case C_BEHIND:
3976 break;
3977 default:
3978 dev_err(DEV, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n",
3979 drbd_conn_str(mdev->state.conn));
3980 }
3981
Philipp Reisner73a01a12010-10-27 14:33:00 +02003982 drbd_set_out_of_sync(mdev, be64_to_cpu(p->sector), be32_to_cpu(p->blksize));
3983
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003984 return 0;
Philipp Reisner73a01a12010-10-27 14:33:00 +02003985}
3986
Philipp Reisner02918be2010-08-20 14:35:10 +02003987struct data_cmd {
3988 int expect_payload;
3989 size_t pkt_size;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003990 int (*fn)(struct drbd_tconn *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003991};
3992
Philipp Reisner02918be2010-08-20 14:35:10 +02003993static struct data_cmd drbd_cmd_handler[] = {
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003994 [P_DATA] = { 1, sizeof(struct p_data), receive_Data },
3995 [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply },
3996 [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } ,
3997 [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } ,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003998 [P_BITMAP] = { 1, 0, receive_bitmap } ,
3999 [P_COMPRESSED_BITMAP] = { 1, 0, receive_bitmap } ,
4000 [P_UNPLUG_REMOTE] = { 0, 0, receive_UnplugRemote },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004001 [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4002 [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004003 [P_SYNC_PARAM] = { 1, 0, receive_SyncParam },
4004 [P_SYNC_PARAM89] = { 1, 0, receive_SyncParam },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004005 [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol },
4006 [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids },
4007 [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes },
4008 [P_STATE] = { 0, sizeof(struct p_state), receive_state },
4009 [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state },
4010 [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid },
4011 [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4012 [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4013 [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4014 [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip },
4015 [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync },
4016 [P_CONN_ST_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_conn_state },
Philipp Reisner02918be2010-08-20 14:35:10 +02004017};
4018
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004019static void drbdd(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004020{
Philipp Reisner77351055b2011-02-07 17:24:26 +01004021 struct packet_info pi;
Philipp Reisner02918be2010-08-20 14:35:10 +02004022 size_t shs; /* sub header size */
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004023 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004024
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004025 while (get_t_state(&tconn->receiver) == RUNNING) {
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004026 struct data_cmd *cmd;
4027
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004028 drbd_thread_current_set_cpu(&tconn->receiver);
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004029 if (drbd_recv_header(tconn, &pi))
Philipp Reisner02918be2010-08-20 14:35:10 +02004030 goto err_out;
4031
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004032 cmd = &drbd_cmd_handler[pi.cmd];
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004033 if (unlikely(pi.cmd >= ARRAY_SIZE(drbd_cmd_handler) || !cmd->fn)) {
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004034 conn_err(tconn, "unknown packet type %d, l: %d!\n", pi.cmd, pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004035 goto err_out;
Lars Ellenberg0b33a912009-11-16 15:58:04 +01004036 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004037
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004038 shs = cmd->pkt_size;
4039 if (pi.size > shs && !cmd->expect_payload) {
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004040 conn_err(tconn, "No payload expected %s l:%d\n", cmdname(pi.cmd), pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004041 goto err_out;
4042 }
4043
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004044 if (shs) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004045 err = drbd_recv_all_warn(tconn, pi.data, shs);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004046 if (err)
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004047 goto err_out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004048 pi.size -= shs;
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004049 }
4050
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004051 err = cmd->fn(tconn, &pi);
4052 if (err) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004053 conn_err(tconn, "error receiving %s, e: %d l: %d!\n",
4054 cmdname(pi.cmd), err, pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004055 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004056 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004057 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004058 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004059
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004060 err_out:
4061 conn_request_state(tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004062}
4063
Philipp Reisner0e29d162011-02-18 14:23:11 +01004064void conn_flush_workqueue(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004065{
4066 struct drbd_wq_barrier barr;
4067
4068 barr.w.cb = w_prev_work_done;
Philipp Reisner0e29d162011-02-18 14:23:11 +01004069 barr.w.tconn = tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004070 init_completion(&barr.done);
Philipp Reisner0e29d162011-02-18 14:23:11 +01004071 drbd_queue_work(&tconn->data.work, &barr.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004072 wait_for_completion(&barr.done);
4073}
4074
Philipp Reisner360cc742011-02-08 14:29:53 +01004075static void drbd_disconnect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004076{
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004077 enum drbd_conns oc;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004078 int rv = SS_UNKNOWN_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004079
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004080 if (tconn->cstate == C_STANDALONE)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004081 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004082
4083 /* asender does not clean up anything. it must not interfere, either */
Philipp Reisner360cc742011-02-08 14:29:53 +01004084 drbd_thread_stop(&tconn->asender);
4085 drbd_free_sock(tconn);
4086
4087 idr_for_each(&tconn->volumes, drbd_disconnected, tconn);
Philipp Reisner360cc742011-02-08 14:29:53 +01004088 conn_info(tconn, "Connection closed\n");
4089
Philipp Reisnercb703452011-03-24 11:03:07 +01004090 if (conn_highest_role(tconn) == R_PRIMARY && conn_highest_pdsk(tconn) >= D_UNKNOWN)
4091 conn_try_outdate_peer_async(tconn);
4092
Philipp Reisner360cc742011-02-08 14:29:53 +01004093 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004094 oc = tconn->cstate;
4095 if (oc >= C_UNCONNECTED)
4096 rv = _conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
4097
Philipp Reisner360cc742011-02-08 14:29:53 +01004098 spin_unlock_irq(&tconn->req_lock);
4099
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004100 if (oc == C_DISCONNECTING) {
Philipp Reisner360cc742011-02-08 14:29:53 +01004101 wait_event(tconn->net_cnt_wait, atomic_read(&tconn->net_cnt) == 0);
4102
4103 crypto_free_hash(tconn->cram_hmac_tfm);
4104 tconn->cram_hmac_tfm = NULL;
4105
4106 kfree(tconn->net_conf);
4107 tconn->net_conf = NULL;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004108 conn_request_state(tconn, NS(conn, C_STANDALONE), CS_VERBOSE);
Philipp Reisner360cc742011-02-08 14:29:53 +01004109 }
4110}
4111
4112static int drbd_disconnected(int vnr, void *p, void *data)
4113{
4114 struct drbd_conf *mdev = (struct drbd_conf *)p;
4115 enum drbd_fencing_p fp;
4116 unsigned int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004117
Philipp Reisner85719572010-07-21 10:20:17 +02004118 /* wait for current activity to cease. */
Philipp Reisner87eeee42011-01-19 14:16:30 +01004119 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004120 _drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
4121 _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee);
4122 _drbd_wait_ee_list_empty(mdev, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004123 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004124
4125 /* We do not have data structures that would allow us to
4126 * get the rs_pending_cnt down to 0 again.
4127 * * On C_SYNC_TARGET we do not have any data structures describing
4128 * the pending RSDataRequest's we have sent.
4129 * * On C_SYNC_SOURCE there is no data structure that tracks
4130 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
4131 * And no, it is not the sum of the reference counts in the
4132 * resync_LRU. The resync_LRU tracks the whole operation including
4133 * the disk-IO, while the rs_pending_cnt only tracks the blocks
4134 * on the fly. */
4135 drbd_rs_cancel_all(mdev);
4136 mdev->rs_total = 0;
4137 mdev->rs_failed = 0;
4138 atomic_set(&mdev->rs_pending_cnt, 0);
4139 wake_up(&mdev->misc_wait);
4140
Philipp Reisner7fde2be2011-03-01 11:08:28 +01004141 del_timer(&mdev->request_timer);
4142
Philipp Reisnerb411b362009-09-25 16:07:19 -07004143 del_timer_sync(&mdev->resync_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004144 resync_timer_fn((unsigned long)mdev);
4145
Philipp Reisnerb411b362009-09-25 16:07:19 -07004146 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
4147 * w_make_resync_request etc. which may still be on the worker queue
4148 * to be "canceled" */
Philipp Reisnera21e9292011-02-08 15:08:49 +01004149 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004150
Andreas Gruenbachera990be42011-04-06 17:56:48 +02004151 drbd_finish_peer_reqs(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004152
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 */
Andreas Gruenbacher7721f562011-04-06 17:14:02 +02004178 i = drbd_free_peer_reqs(mdev, &mdev->net_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004179 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
Andreas Gruenbachera990be42011-04-06 17:56:48 +02004816static int tconn_finish_peer_reqs(struct drbd_tconn *tconn)
Philipp Reisner32862ec2011-02-08 16:41:01 +01004817{
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 Gruenbachera990be42011-04-06 17:56:48 +02004825 if (drbd_finish_peer_reqs(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);
Philipp Reisner695d08f2011-04-11 22:53:32 -07004831 rcu_read_lock();
Philipp Reisner082a3432011-03-15 16:05:42 +01004832 idr_for_each_entry(&tconn->volumes, mdev, i) {
4833 not_empty = !list_empty(&mdev->done_ee);
4834 if (not_empty)
4835 break;
4836 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07004837 rcu_read_unlock();
Philipp Reisner082a3432011-03-15 16:05:42 +01004838 spin_unlock_irq(&tconn->req_lock);
Philipp Reisner32862ec2011-02-08 16:41:01 +01004839 } while (not_empty);
4840
4841 return 0;
4842}
4843
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004844struct asender_cmd {
4845 size_t pkt_size;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004846 int (*fn)(struct drbd_tconn *tconn, struct packet_info *);
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004847};
4848
4849static struct asender_cmd asender_tbl[] = {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004850 [P_PING] = { 0, got_Ping },
4851 [P_PING_ACK] = { 0, got_PingAck },
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004852 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4853 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4854 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
4855 [P_DISCARD_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
4856 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
4857 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
4858 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply },
4859 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
4860 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
4861 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
4862 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
4863 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
4864 [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply },
4865 [P_CONN_ST_CHG_REPLY]={ sizeof(struct p_req_state_reply), got_conn_RqSReply },
4866 [P_RETRY_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004867};
4868
Philipp Reisnerb411b362009-09-25 16:07:19 -07004869int drbd_asender(struct drbd_thread *thi)
4870{
Philipp Reisner392c8802011-02-09 10:33:31 +01004871 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004872 struct asender_cmd *cmd = NULL;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004873 struct packet_info pi;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004874 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004875 void *buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004876 int received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02004877 unsigned int header_size = drbd_header_size(tconn);
4878 int expect = header_size;
Lars Ellenbergf36af182011-03-09 22:44:55 +01004879 int ping_timeout_active = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004880
Philipp Reisnerb411b362009-09-25 16:07:19 -07004881 current->policy = SCHED_RR; /* Make this a realtime task! */
4882 current->rt_priority = 2; /* more important than all other tasks */
4883
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01004884 while (get_t_state(thi) == RUNNING) {
Philipp Reisner80822282011-02-08 12:46:30 +01004885 drbd_thread_current_set_cpu(thi);
Philipp Reisner32862ec2011-02-08 16:41:01 +01004886 if (test_and_clear_bit(SEND_PING, &tconn->flags)) {
Andreas Gruenbachera17647a2011-04-01 12:49:42 +02004887 if (drbd_send_ping(tconn)) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004888 conn_err(tconn, "drbd_send_ping has failed\n");
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01004889 goto reconnect;
4890 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01004891 tconn->meta.socket->sk->sk_rcvtimeo =
4892 tconn->net_conf->ping_timeo*HZ/10;
Lars Ellenbergf36af182011-03-09 22:44:55 +01004893 ping_timeout_active = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004894 }
4895
Philipp Reisner32862ec2011-02-08 16:41:01 +01004896 /* TODO: conditionally cork; it may hurt latency if we cork without
4897 much to send */
4898 if (!tconn->net_conf->no_cork)
4899 drbd_tcp_cork(tconn->meta.socket);
Andreas Gruenbachera990be42011-04-06 17:56:48 +02004900 if (tconn_finish_peer_reqs(tconn)) {
4901 conn_err(tconn, "tconn_finish_peer_reqs() failed\n");
Philipp Reisner32862ec2011-02-08 16:41:01 +01004902 goto reconnect;
Philipp Reisner082a3432011-03-15 16:05:42 +01004903 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004904 /* but unconditionally uncork unless disabled */
Philipp Reisner32862ec2011-02-08 16:41:01 +01004905 if (!tconn->net_conf->no_cork)
4906 drbd_tcp_uncork(tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004907
4908 /* short circuit, recv_msg would return EINTR anyways. */
4909 if (signal_pending(current))
4910 continue;
4911
Philipp Reisner32862ec2011-02-08 16:41:01 +01004912 rv = drbd_recv_short(tconn->meta.socket, buf, expect-received, 0);
4913 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004914
4915 flush_signals(current);
4916
4917 /* Note:
4918 * -EINTR (on meta) we got a signal
4919 * -EAGAIN (on meta) rcvtimeo expired
4920 * -ECONNRESET other side closed the connection
4921 * -ERESTARTSYS (on data) we got a signal
4922 * rv < 0 other than above: unexpected error!
4923 * rv == expected: full header or command
4924 * rv < expected: "woken" by signal during receive
4925 * rv == 0 : "connection shut down by peer"
4926 */
4927 if (likely(rv > 0)) {
4928 received += rv;
4929 buf += rv;
4930 } else if (rv == 0) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004931 conn_err(tconn, "meta connection shut down by peer.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004932 goto reconnect;
4933 } else if (rv == -EAGAIN) {
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02004934 /* If the data socket received something meanwhile,
4935 * that is good enough: peer is still alive. */
Philipp Reisner32862ec2011-02-08 16:41:01 +01004936 if (time_after(tconn->last_received,
4937 jiffies - tconn->meta.socket->sk->sk_rcvtimeo))
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02004938 continue;
Lars Ellenbergf36af182011-03-09 22:44:55 +01004939 if (ping_timeout_active) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004940 conn_err(tconn, "PingAck did not arrive in time.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004941 goto reconnect;
4942 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01004943 set_bit(SEND_PING, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004944 continue;
4945 } else if (rv == -EINTR) {
4946 continue;
4947 } else {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004948 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004949 goto reconnect;
4950 }
4951
4952 if (received == expect && cmd == NULL) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004953 if (decode_header(tconn, tconn->meta.rbuf, &pi))
Philipp Reisnerb411b362009-09-25 16:07:19 -07004954 goto reconnect;
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004955 cmd = &asender_tbl[pi.cmd];
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004956 if (pi.cmd >= ARRAY_SIZE(asender_tbl) || !cmd->fn) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004957 conn_err(tconn, "unknown command %d on meta (l: %d)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004958 pi.cmd, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004959 goto disconnect;
4960 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004961 expect = header_size + cmd->pkt_size;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02004962 if (pi.size != expect - header_size) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01004963 conn_err(tconn, "Wrong packet size on meta (c: %d, l: %d)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004964 pi.cmd, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004965 goto reconnect;
Philipp Reisner257d0af2011-01-26 12:15:29 +01004966 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004967 }
4968 if (received == expect) {
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004969 bool err;
Philipp Reisnera4fbda82011-03-16 11:13:17 +01004970
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004971 err = cmd->fn(tconn, &pi);
4972 if (err) {
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004973 conn_err(tconn, "%pf failed\n", cmd->fn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004974 goto reconnect;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004975 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004976
Philipp Reisnera4fbda82011-03-16 11:13:17 +01004977 tconn->last_received = jiffies;
4978
Lars Ellenbergf36af182011-03-09 22:44:55 +01004979 /* the idle_timeout (ping-int)
4980 * has been restored in got_PingAck() */
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01004981 if (cmd == &asender_tbl[P_PING_ACK])
Lars Ellenbergf36af182011-03-09 22:44:55 +01004982 ping_timeout_active = 0;
4983
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004984 buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004985 received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02004986 expect = header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004987 cmd = NULL;
4988 }
4989 }
4990
4991 if (0) {
4992reconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004993 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004994 }
4995 if (0) {
4996disconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004997 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004998 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01004999 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005000
Philipp Reisner32862ec2011-02-08 16:41:01 +01005001 conn_info(tconn, "asender terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005002
5003 return 0;
5004}