blob: 855cadfe614677442a95e0d53b18e18a6b23a84d [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 Reisnerc141ebd2011-05-05 16:13:10 +020066static int drbd_disconnected(struct drbd_conf *mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -070067
Philipp Reisner1e9dd292011-11-10 15:14:53 +010068static enum finish_epoch drbd_may_finish_epoch(struct drbd_tconn *, 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;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200247 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700248 DEFINE_WAIT(wait);
Philipp Reisner44ed1672011-04-19 17:10:19 +0200249 int mxb;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700250
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200251 /* Yes, we may run up to @number over max_buffers. If we
252 * follow it strictly, the admin will get it wrong anyways. */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200253 rcu_read_lock();
254 nc = rcu_dereference(mdev->tconn->net_conf);
255 mxb = nc ? nc->max_buffers : 1000000;
256 rcu_read_unlock();
257
258 if (atomic_read(&mdev->pp_in_use) < mxb)
Andreas Gruenbacher18c2d522011-04-07 21:08:50 +0200259 page = __drbd_alloc_pages(mdev, number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700260
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200261 while (page == NULL) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700262 prepare_to_wait(&drbd_pp_wait, &wait, TASK_INTERRUPTIBLE);
263
264 drbd_kick_lo_and_reclaim_net(mdev);
265
Philipp Reisner44ed1672011-04-19 17:10:19 +0200266 if (atomic_read(&mdev->pp_in_use) < mxb) {
Andreas Gruenbacher18c2d522011-04-07 21:08:50 +0200267 page = __drbd_alloc_pages(mdev, number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700268 if (page)
269 break;
270 }
271
272 if (!retry)
273 break;
274
275 if (signal_pending(current)) {
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200276 dev_warn(DEV, "drbd_alloc_pages interrupted!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700277 break;
278 }
279
280 schedule();
281 }
282 finish_wait(&drbd_pp_wait, &wait);
283
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200284 if (page)
285 atomic_add(number, &mdev->pp_in_use);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700286 return page;
287}
288
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200289/* Must not be used from irq, as that may deadlock: see drbd_alloc_pages.
Philipp Reisner87eeee42011-01-19 14:16:30 +0100290 * Is also used from inside an other spin_lock_irq(&mdev->tconn->req_lock);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200291 * Either links the page chain back to the global pool,
292 * or returns all pages to the system. */
Andreas Gruenbacher5cc287e2011-04-07 21:02:59 +0200293static void drbd_free_pages(struct drbd_conf *mdev, struct page *page, int is_net)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700294{
Lars Ellenberg435f0742010-09-06 12:30:25 +0200295 atomic_t *a = is_net ? &mdev->pp_in_use_by_net : &mdev->pp_in_use;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700296 int i;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200297
Lars Ellenberg81a35372012-07-30 09:00:54 +0200298 if (page == NULL)
299 return;
300
Philipp Reisner81a5d602011-02-22 19:53:16 -0500301 if (drbd_pp_vacant > (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * minor_count)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200302 i = page_chain_free(page);
303 else {
304 struct page *tmp;
305 tmp = page_chain_tail(page, &i);
306 spin_lock(&drbd_pp_lock);
307 page_chain_add(&drbd_pp_pool, page, tmp);
308 drbd_pp_vacant += i;
309 spin_unlock(&drbd_pp_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700310 }
Lars Ellenberg435f0742010-09-06 12:30:25 +0200311 i = atomic_sub_return(i, a);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200312 if (i < 0)
Lars Ellenberg435f0742010-09-06 12:30:25 +0200313 dev_warn(DEV, "ASSERTION FAILED: %s: %d < 0\n",
314 is_net ? "pp_in_use_by_net" : "pp_in_use", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700315 wake_up(&drbd_pp_wait);
316}
317
318/*
319You need to hold the req_lock:
320 _drbd_wait_ee_list_empty()
321
322You must not have the req_lock:
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200323 drbd_free_peer_req()
Andreas Gruenbacher0db55362011-04-06 16:09:15 +0200324 drbd_alloc_peer_req()
Andreas Gruenbacher7721f562011-04-06 17:14:02 +0200325 drbd_free_peer_reqs()
Philipp Reisnerb411b362009-09-25 16:07:19 -0700326 drbd_ee_fix_bhs()
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200327 drbd_finish_peer_reqs()
Philipp Reisnerb411b362009-09-25 16:07:19 -0700328 drbd_clear_done_ee()
329 drbd_wait_ee_list_empty()
330*/
331
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +0100332struct drbd_peer_request *
Andreas Gruenbacher0db55362011-04-06 16:09:15 +0200333drbd_alloc_peer_req(struct drbd_conf *mdev, u64 id, sector_t sector,
334 unsigned int data_size, gfp_t gfp_mask) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700335{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100336 struct drbd_peer_request *peer_req;
Lars Ellenberg81a35372012-07-30 09:00:54 +0200337 struct page *page = NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200338 unsigned nr_pages = (data_size + PAGE_SIZE -1) >> PAGE_SHIFT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700339
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100340 if (drbd_insert_fault(mdev, DRBD_FAULT_AL_EE))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700341 return NULL;
342
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100343 peer_req = mempool_alloc(drbd_ee_mempool, gfp_mask & ~__GFP_HIGHMEM);
344 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700345 if (!(gfp_mask & __GFP_NOWARN))
Andreas Gruenbacher0db55362011-04-06 16:09:15 +0200346 dev_err(DEV, "%s: allocation failed\n", __func__);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700347 return NULL;
348 }
349
Lars Ellenberg81a35372012-07-30 09:00:54 +0200350 if (data_size) {
351 page = drbd_alloc_pages(mdev, nr_pages, (gfp_mask & __GFP_WAIT));
352 if (!page)
353 goto fail;
354 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700355
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100356 drbd_clear_interval(&peer_req->i);
357 peer_req->i.size = data_size;
358 peer_req->i.sector = sector;
359 peer_req->i.local = false;
360 peer_req->i.waiting = false;
Andreas Gruenbacher53840642011-01-28 10:31:04 +0100361
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100362 peer_req->epoch = NULL;
Philipp Reisnera21e9292011-02-08 15:08:49 +0100363 peer_req->w.mdev = mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100364 peer_req->pages = page;
365 atomic_set(&peer_req->pending_bios, 0);
366 peer_req->flags = 0;
Andreas Gruenbacher9a8e7752011-01-11 14:04:09 +0100367 /*
368 * The block_id is opaque to the receiver. It is not endianness
369 * converted, and sent back to the sender unchanged.
370 */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100371 peer_req->block_id = id;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700372
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100373 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700374
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200375 fail:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100376 mempool_free(peer_req, drbd_ee_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700377 return NULL;
378}
379
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200380void __drbd_free_peer_req(struct drbd_conf *mdev, struct drbd_peer_request *peer_req,
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +0100381 int is_net)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700382{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100383 if (peer_req->flags & EE_HAS_DIGEST)
384 kfree(peer_req->digest);
Andreas Gruenbacher5cc287e2011-04-07 21:02:59 +0200385 drbd_free_pages(mdev, peer_req->pages, is_net);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100386 D_ASSERT(atomic_read(&peer_req->pending_bios) == 0);
387 D_ASSERT(drbd_interval_empty(&peer_req->i));
388 mempool_free(peer_req, drbd_ee_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700389}
390
Andreas Gruenbacher7721f562011-04-06 17:14:02 +0200391int drbd_free_peer_reqs(struct drbd_conf *mdev, struct list_head *list)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700392{
393 LIST_HEAD(work_list);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100394 struct drbd_peer_request *peer_req, *t;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700395 int count = 0;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200396 int is_net = list == &mdev->net_ee;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700397
Philipp Reisner87eeee42011-01-19 14:16:30 +0100398 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700399 list_splice_init(list, &work_list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100400 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700401
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100402 list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200403 __drbd_free_peer_req(mdev, peer_req, is_net);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700404 count++;
405 }
406 return count;
407}
408
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200409/*
410 * See also comments in _req_mod(,BARRIER_ACKED) and receive_Barrier.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700411 */
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200412static int drbd_finish_peer_reqs(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700413{
414 LIST_HEAD(work_list);
415 LIST_HEAD(reclaimed);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100416 struct drbd_peer_request *peer_req, *t;
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100417 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700418
Philipp Reisner87eeee42011-01-19 14:16:30 +0100419 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200420 reclaim_finished_net_peer_reqs(mdev, &reclaimed);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700421 list_splice_init(&mdev->done_ee, &work_list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100422 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700423
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100424 list_for_each_entry_safe(peer_req, t, &reclaimed, w.list)
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200425 drbd_free_net_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700426
427 /* possible callbacks here:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100428 * e_end_block, and e_end_resync_block, e_send_discard_write.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700429 * all ignore the last argument.
430 */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100431 list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100432 int err2;
433
Philipp Reisnerb411b362009-09-25 16:07:19 -0700434 /* list_del not necessary, next/prev members not touched */
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100435 err2 = peer_req->w.cb(&peer_req->w, !!err);
436 if (!err)
437 err = err2;
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200438 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700439 }
440 wake_up(&mdev->ee_wait);
441
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100442 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700443}
444
Andreas Gruenbacherd4da1532011-04-07 00:06:56 +0200445static void _drbd_wait_ee_list_empty(struct drbd_conf *mdev,
446 struct list_head *head)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700447{
448 DEFINE_WAIT(wait);
449
450 /* avoids spin_lock/unlock
451 * and calling prepare_to_wait in the fast path */
452 while (!list_empty(head)) {
453 prepare_to_wait(&mdev->ee_wait, &wait, TASK_UNINTERRUPTIBLE);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100454 spin_unlock_irq(&mdev->tconn->req_lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100455 io_schedule();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700456 finish_wait(&mdev->ee_wait, &wait);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100457 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700458 }
459}
460
Andreas Gruenbacherd4da1532011-04-07 00:06:56 +0200461static void drbd_wait_ee_list_empty(struct drbd_conf *mdev,
462 struct list_head *head)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700463{
Philipp Reisner87eeee42011-01-19 14:16:30 +0100464 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700465 _drbd_wait_ee_list_empty(mdev, head);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100466 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700467}
468
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100469static int drbd_recv_short(struct socket *sock, void *buf, size_t size, int flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700470{
471 mm_segment_t oldfs;
472 struct kvec iov = {
473 .iov_base = buf,
474 .iov_len = size,
475 };
476 struct msghdr msg = {
477 .msg_iovlen = 1,
478 .msg_iov = (struct iovec *)&iov,
479 .msg_flags = (flags ? flags : MSG_WAITALL | MSG_NOSIGNAL)
480 };
481 int rv;
482
483 oldfs = get_fs();
484 set_fs(KERNEL_DS);
485 rv = sock_recvmsg(sock, &msg, size, msg.msg_flags);
486 set_fs(oldfs);
487
488 return rv;
489}
490
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100491static int drbd_recv(struct drbd_tconn *tconn, void *buf, size_t size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700492{
493 mm_segment_t oldfs;
494 struct kvec iov = {
495 .iov_base = buf,
496 .iov_len = size,
497 };
498 struct msghdr msg = {
499 .msg_iovlen = 1,
500 .msg_iov = (struct iovec *)&iov,
501 .msg_flags = MSG_WAITALL | MSG_NOSIGNAL
502 };
503 int rv;
504
505 oldfs = get_fs();
506 set_fs(KERNEL_DS);
507
508 for (;;) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100509 rv = sock_recvmsg(tconn->data.socket, &msg, size, msg.msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700510 if (rv == size)
511 break;
512
513 /* Note:
514 * ECONNRESET other side closed the connection
515 * ERESTARTSYS (on sock) we got a signal
516 */
517
518 if (rv < 0) {
519 if (rv == -ECONNRESET)
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100520 conn_info(tconn, "sock was reset by peer\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700521 else if (rv != -ERESTARTSYS)
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100522 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700523 break;
524 } else if (rv == 0) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100525 conn_info(tconn, "sock was shut down by peer\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700526 break;
527 } else {
528 /* signal came in, or peer/link went down,
529 * after we read a partial message
530 */
531 /* D_ASSERT(signal_pending(current)); */
532 break;
533 }
534 };
535
536 set_fs(oldfs);
537
538 if (rv != size)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100539 conn_request_state(tconn, NS(conn, C_BROKEN_PIPE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700540
541 return rv;
542}
543
Andreas Gruenbacherc6967742011-03-17 17:15:20 +0100544static int drbd_recv_all(struct drbd_tconn *tconn, void *buf, size_t size)
545{
546 int err;
547
548 err = drbd_recv(tconn, buf, size);
549 if (err != size) {
550 if (err >= 0)
551 err = -EIO;
552 } else
553 err = 0;
554 return err;
555}
556
Andreas Gruenbachera5c31902011-03-24 03:28:04 +0100557static int drbd_recv_all_warn(struct drbd_tconn *tconn, void *buf, size_t size)
558{
559 int err;
560
561 err = drbd_recv_all(tconn, buf, size);
562 if (err && !signal_pending(current))
563 conn_warn(tconn, "short read (expected size %d)\n", (int)size);
564 return err;
565}
566
Lars Ellenberg5dbf1672010-05-25 16:18:01 +0200567/* quoting tcp(7):
568 * On individual connections, the socket buffer size must be set prior to the
569 * listen(2) or connect(2) calls in order to have it take effect.
570 * This is our wrapper to do so.
571 */
572static void drbd_setbufsize(struct socket *sock, unsigned int snd,
573 unsigned int rcv)
574{
575 /* open coded SO_SNDBUF, SO_RCVBUF */
576 if (snd) {
577 sock->sk->sk_sndbuf = snd;
578 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
579 }
580 if (rcv) {
581 sock->sk->sk_rcvbuf = rcv;
582 sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
583 }
584}
585
Philipp Reisnereac3e992011-02-07 14:05:07 +0100586static struct socket *drbd_try_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700587{
588 const char *what;
589 struct socket *sock;
590 struct sockaddr_in6 src_in6;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200591 struct sockaddr_in6 peer_in6;
592 struct net_conf *nc;
593 int err, peer_addr_len, my_addr_len;
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200594 int sndbuf_size, rcvbuf_size, connect_int;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700595 int disconnect_on_error = 1;
596
Philipp Reisner44ed1672011-04-19 17:10:19 +0200597 rcu_read_lock();
598 nc = rcu_dereference(tconn->net_conf);
599 if (!nc) {
600 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700601 return NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200602 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200603 sndbuf_size = nc->sndbuf_size;
604 rcvbuf_size = nc->rcvbuf_size;
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200605 connect_int = nc->connect_int;
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200606 rcu_read_unlock();
Philipp Reisner44ed1672011-04-19 17:10:19 +0200607
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200608 my_addr_len = min_t(int, tconn->my_addr_len, sizeof(src_in6));
609 memcpy(&src_in6, &tconn->my_addr, my_addr_len);
Philipp Reisner44ed1672011-04-19 17:10:19 +0200610
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200611 if (((struct sockaddr *)&tconn->my_addr)->sa_family == AF_INET6)
Philipp Reisner44ed1672011-04-19 17:10:19 +0200612 src_in6.sin6_port = 0;
613 else
614 ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */
615
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200616 peer_addr_len = min_t(int, tconn->peer_addr_len, sizeof(src_in6));
617 memcpy(&peer_in6, &tconn->peer_addr, peer_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700618
619 what = "sock_create_kern";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200620 err = sock_create_kern(((struct sockaddr *)&src_in6)->sa_family,
621 SOCK_STREAM, IPPROTO_TCP, &sock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700622 if (err < 0) {
623 sock = NULL;
624 goto out;
625 }
626
627 sock->sk->sk_rcvtimeo =
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200628 sock->sk->sk_sndtimeo = connect_int * HZ;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200629 drbd_setbufsize(sock, sndbuf_size, rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700630
631 /* explicitly bind to the configured IP as source IP
632 * for the outgoing connections.
633 * This is needed for multihomed hosts and to be
634 * able to use lo: interfaces for drbd.
635 * Make sure to use 0 as port number, so linux selects
636 * a free one dynamically.
637 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700638 what = "bind before connect";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200639 err = sock->ops->bind(sock, (struct sockaddr *) &src_in6, my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700640 if (err < 0)
641 goto out;
642
643 /* connect may fail, peer not yet available.
644 * stay C_WF_CONNECTION, don't go Disconnecting! */
645 disconnect_on_error = 0;
646 what = "connect";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200647 err = sock->ops->connect(sock, (struct sockaddr *) &peer_in6, peer_addr_len, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700648
649out:
650 if (err < 0) {
651 if (sock) {
652 sock_release(sock);
653 sock = NULL;
654 }
655 switch (-err) {
656 /* timeout, busy, signal pending */
657 case ETIMEDOUT: case EAGAIN: case EINPROGRESS:
658 case EINTR: case ERESTARTSYS:
659 /* peer not (yet) available, network problem */
660 case ECONNREFUSED: case ENETUNREACH:
661 case EHOSTDOWN: case EHOSTUNREACH:
662 disconnect_on_error = 0;
663 break;
664 default:
Philipp Reisnereac3e992011-02-07 14:05:07 +0100665 conn_err(tconn, "%s failed, err = %d\n", what, err);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700666 }
667 if (disconnect_on_error)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100668 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700669 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200670
Philipp Reisnerb411b362009-09-25 16:07:19 -0700671 return sock;
672}
673
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200674struct accept_wait_data {
675 struct drbd_tconn *tconn;
676 struct socket *s_listen;
677 struct completion door_bell;
678 void (*original_sk_state_change)(struct sock *sk);
679
680};
681
682static void incomming_connection(struct sock *sk)
683{
684 struct accept_wait_data *ad = sk->sk_user_data;
685 struct drbd_tconn *tconn = ad->tconn;
686
687 if (sk->sk_state != TCP_ESTABLISHED)
688 conn_warn(tconn, "unexpected tcp state change. sk_state = %d\n", sk->sk_state);
689
690 write_lock_bh(&sk->sk_callback_lock);
691 sk->sk_state_change = ad->original_sk_state_change;
692 sk->sk_user_data = NULL;
693 write_unlock_bh(&sk->sk_callback_lock);
694
695 sk->sk_state_change(sk);
696 complete(&ad->door_bell);
697}
698
699static int prepare_listen_socket(struct drbd_tconn *tconn, struct accept_wait_data *ad)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700700{
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200701 int err, sndbuf_size, rcvbuf_size, my_addr_len;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200702 struct sockaddr_in6 my_addr;
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200703 struct socket *s_listen;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200704 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700705 const char *what;
706
Philipp Reisner44ed1672011-04-19 17:10:19 +0200707 rcu_read_lock();
708 nc = rcu_dereference(tconn->net_conf);
709 if (!nc) {
710 rcu_read_unlock();
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200711 return -EIO;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200712 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200713 sndbuf_size = nc->sndbuf_size;
714 rcvbuf_size = nc->rcvbuf_size;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200715 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700716
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200717 my_addr_len = min_t(int, tconn->my_addr_len, sizeof(struct sockaddr_in6));
718 memcpy(&my_addr, &tconn->my_addr, my_addr_len);
719
Philipp Reisnerb411b362009-09-25 16:07:19 -0700720 what = "sock_create_kern";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200721 err = sock_create_kern(((struct sockaddr *)&my_addr)->sa_family,
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200722 SOCK_STREAM, IPPROTO_TCP, &s_listen);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700723 if (err) {
724 s_listen = NULL;
725 goto out;
726 }
727
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200728 s_listen->sk->sk_reuse = 1; /* SO_REUSEADDR */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200729 drbd_setbufsize(s_listen, sndbuf_size, rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700730
731 what = "bind before listen";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200732 err = s_listen->ops->bind(s_listen, (struct sockaddr *)&my_addr, my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700733 if (err < 0)
734 goto out;
735
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200736 ad->s_listen = s_listen;
737 write_lock_bh(&s_listen->sk->sk_callback_lock);
738 ad->original_sk_state_change = s_listen->sk->sk_state_change;
739 s_listen->sk->sk_state_change = incomming_connection;
740 s_listen->sk->sk_user_data = ad;
741 write_unlock_bh(&s_listen->sk->sk_callback_lock);
742
Philipp Reisner2820fd32012-07-12 10:22:48 +0200743 what = "listen";
744 err = s_listen->ops->listen(s_listen, 5);
745 if (err < 0)
746 goto out;
747
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200748 return 0;
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200749out:
750 if (s_listen)
751 sock_release(s_listen);
752 if (err < 0) {
753 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
754 conn_err(tconn, "%s failed, err = %d\n", what, err);
755 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
756 }
757 }
758
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200759 return -EIO;
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200760}
761
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200762static struct socket *drbd_wait_for_connect(struct drbd_tconn *tconn, struct accept_wait_data *ad)
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200763{
764 int timeo, connect_int, err = 0;
765 struct socket *s_estab = NULL;
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200766 struct net_conf *nc;
767
768 rcu_read_lock();
769 nc = rcu_dereference(tconn->net_conf);
770 if (!nc) {
771 rcu_read_unlock();
772 return NULL;
773 }
774 connect_int = nc->connect_int;
775 rcu_read_unlock();
776
777 timeo = connect_int * HZ;
778 timeo += (random32() & 1) ? timeo / 7 : -timeo / 7; /* 28.5% random jitter */
779
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200780 err = wait_for_completion_interruptible_timeout(&ad->door_bell, timeo);
781 if (err <= 0)
782 return NULL;
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200783
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200784 err = kernel_accept(ad->s_listen, &s_estab, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700785 if (err < 0) {
786 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200787 conn_err(tconn, "accept failed, err = %d\n", err);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100788 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700789 }
790 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700791
792 return s_estab;
793}
794
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200795static int decode_header(struct drbd_tconn *, void *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700796
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200797static int send_first_packet(struct drbd_tconn *tconn, struct drbd_socket *sock,
798 enum drbd_packet cmd)
799{
800 if (!conn_prepare_command(tconn, sock))
801 return -EIO;
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200802 return conn_send_command(tconn, sock, cmd, 0, NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700803}
804
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200805static int receive_first_packet(struct drbd_tconn *tconn, struct socket *sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700806{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200807 unsigned int header_size = drbd_header_size(tconn);
808 struct packet_info pi;
809 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700810
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200811 err = drbd_recv_short(sock, tconn->data.rbuf, header_size, 0);
812 if (err != header_size) {
813 if (err >= 0)
814 err = -EIO;
815 return err;
816 }
817 err = decode_header(tconn, tconn->data.rbuf, &pi);
818 if (err)
819 return err;
820 return pi.cmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700821}
822
823/**
824 * drbd_socket_okay() - Free the socket if its connection is not okay
Philipp Reisnerb411b362009-09-25 16:07:19 -0700825 * @sock: pointer to the pointer to the socket.
826 */
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100827static int drbd_socket_okay(struct socket **sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700828{
829 int rr;
830 char tb[4];
831
832 if (!*sock)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100833 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700834
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100835 rr = drbd_recv_short(*sock, tb, 4, MSG_DONTWAIT | MSG_PEEK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700836
837 if (rr > 0 || rr == -EAGAIN) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100838 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700839 } else {
840 sock_release(*sock);
841 *sock = NULL;
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100842 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700843 }
844}
Philipp Reisner2325eb62011-03-15 16:56:18 +0100845/* Gets called if a connection is established, or if a new minor gets created
846 in a connection */
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200847int drbd_connected(struct drbd_conf *mdev)
Philipp Reisner907599e2011-02-08 11:25:37 +0100848{
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100849 int err;
Philipp Reisner907599e2011-02-08 11:25:37 +0100850
851 atomic_set(&mdev->packet_seq, 0);
852 mdev->peer_seq = 0;
853
Philipp Reisner8410da82011-02-11 20:11:10 +0100854 mdev->state_mutex = mdev->tconn->agreed_pro_version < 100 ?
855 &mdev->tconn->cstate_mutex :
856 &mdev->own_state_mutex;
857
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100858 err = drbd_send_sync_param(mdev);
859 if (!err)
860 err = drbd_send_sizes(mdev, 0, 0);
861 if (!err)
862 err = drbd_send_uuids(mdev);
863 if (!err)
Philipp Reisner43de7c82011-11-10 13:16:13 +0100864 err = drbd_send_current_state(mdev);
Philipp Reisner907599e2011-02-08 11:25:37 +0100865 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
866 clear_bit(RESIZE_PENDING, &mdev->flags);
Philipp Reisner8b924f12011-03-01 11:08:28 +0100867 mod_timer(&mdev->request_timer, jiffies + HZ); /* just start it here. */
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100868 return err;
Philipp Reisner907599e2011-02-08 11:25:37 +0100869}
870
Philipp Reisnerb411b362009-09-25 16:07:19 -0700871/*
872 * return values:
873 * 1 yes, we have a valid connection
874 * 0 oops, did not work out, please try again
875 * -1 peer talks different language,
876 * no point in trying again, please go standalone.
877 * -2 We do not have a network config...
878 */
Philipp Reisner81fa2e62011-05-04 15:10:30 +0200879static int conn_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700880{
Philipp Reisner7da35862011-12-19 22:42:56 +0100881 struct drbd_socket sock, msock;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200882 struct drbd_conf *mdev;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200883 struct net_conf *nc;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200884 int vnr, timeout, try, h, ok;
Philipp Reisner08b165b2011-09-05 16:22:33 +0200885 bool discard_my_data;
Philipp Reisnera1096a62012-04-06 12:07:34 +0200886 enum drbd_state_rv rv;
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200887 struct accept_wait_data ad = {
888 .tconn = tconn,
889 .door_bell = COMPLETION_INITIALIZER_ONSTACK(ad.door_bell),
890 };
Philipp Reisnerb411b362009-09-25 16:07:19 -0700891
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100892 if (conn_request_state(tconn, NS(conn, C_WF_CONNECTION), CS_VERBOSE) < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700893 return -2;
894
Philipp Reisner7da35862011-12-19 22:42:56 +0100895 mutex_init(&sock.mutex);
896 sock.sbuf = tconn->data.sbuf;
897 sock.rbuf = tconn->data.rbuf;
898 sock.socket = NULL;
899 mutex_init(&msock.mutex);
900 msock.sbuf = tconn->meta.sbuf;
901 msock.rbuf = tconn->meta.rbuf;
902 msock.socket = NULL;
903
Philipp Reisner907599e2011-02-08 11:25:37 +0100904 clear_bit(DISCARD_CONCURRENT, &tconn->flags);
Andreas Gruenbacher0916e0e2011-03-21 14:10:15 +0100905
906 /* Assume that the peer only understands protocol 80 until we know better. */
907 tconn->agreed_pro_version = 80;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700908
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200909 if (prepare_listen_socket(tconn, &ad))
910 return 0;
911
Philipp Reisnerb411b362009-09-25 16:07:19 -0700912 do {
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200913 struct socket *s;
914
Philipp Reisnerb411b362009-09-25 16:07:19 -0700915 for (try = 0;;) {
916 /* 3 tries, this should take less than a second! */
Philipp Reisner907599e2011-02-08 11:25:37 +0100917 s = drbd_try_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700918 if (s || ++try >= 3)
919 break;
920 /* give the other side time to call bind() & listen() */
Philipp Reisner20ee6392011-01-18 15:28:59 +0100921 schedule_timeout_interruptible(HZ / 10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700922 }
923
924 if (s) {
Philipp Reisner7da35862011-12-19 22:42:56 +0100925 if (!sock.socket) {
926 sock.socket = s;
927 send_first_packet(tconn, &sock, P_INITIAL_DATA);
928 } else if (!msock.socket) {
929 msock.socket = s;
930 send_first_packet(tconn, &msock, P_INITIAL_META);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700931 } else {
Philipp Reisner81fa2e62011-05-04 15:10:30 +0200932 conn_err(tconn, "Logic error in conn_connect()\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700933 goto out_release_sockets;
934 }
935 }
936
Philipp Reisner7da35862011-12-19 22:42:56 +0100937 if (sock.socket && msock.socket) {
938 rcu_read_lock();
939 nc = rcu_dereference(tconn->net_conf);
940 timeout = nc->ping_timeo * HZ / 10;
941 rcu_read_unlock();
942 schedule_timeout_interruptible(timeout);
943 ok = drbd_socket_okay(&sock.socket);
944 ok = drbd_socket_okay(&msock.socket) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700945 if (ok)
946 break;
947 }
948
949retry:
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200950 s = drbd_wait_for_connect(tconn, &ad);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700951 if (s) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200952 try = receive_first_packet(tconn, s);
Philipp Reisner7da35862011-12-19 22:42:56 +0100953 drbd_socket_okay(&sock.socket);
954 drbd_socket_okay(&msock.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700955 switch (try) {
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200956 case P_INITIAL_DATA:
Philipp Reisner7da35862011-12-19 22:42:56 +0100957 if (sock.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100958 conn_warn(tconn, "initial packet S crossed\n");
Philipp Reisner7da35862011-12-19 22:42:56 +0100959 sock_release(sock.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700960 }
Philipp Reisner7da35862011-12-19 22:42:56 +0100961 sock.socket = s;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700962 break;
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200963 case P_INITIAL_META:
Philipp Reisner7da35862011-12-19 22:42:56 +0100964 if (msock.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100965 conn_warn(tconn, "initial packet M crossed\n");
Philipp Reisner7da35862011-12-19 22:42:56 +0100966 sock_release(msock.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700967 }
Philipp Reisner7da35862011-12-19 22:42:56 +0100968 msock.socket = s;
Philipp Reisner907599e2011-02-08 11:25:37 +0100969 set_bit(DISCARD_CONCURRENT, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700970 break;
971 default:
Philipp Reisner907599e2011-02-08 11:25:37 +0100972 conn_warn(tconn, "Error receiving initial packet\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700973 sock_release(s);
974 if (random32() & 1)
975 goto retry;
976 }
977 }
978
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100979 if (tconn->cstate <= C_DISCONNECTING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700980 goto out_release_sockets;
981 if (signal_pending(current)) {
982 flush_signals(current);
983 smp_rmb();
Philipp Reisner907599e2011-02-08 11:25:37 +0100984 if (get_t_state(&tconn->receiver) == EXITING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700985 goto out_release_sockets;
986 }
987
Philipp Reisnerb666dbf2012-07-26 14:12:59 +0200988 ok = drbd_socket_okay(&sock.socket);
989 ok = drbd_socket_okay(&msock.socket) && ok;
990 } while (!ok);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700991
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200992 if (ad.s_listen)
993 sock_release(ad.s_listen);
994
Philipp Reisner7da35862011-12-19 22:42:56 +0100995 sock.socket->sk->sk_reuse = 1; /* SO_REUSEADDR */
996 msock.socket->sk->sk_reuse = 1; /* SO_REUSEADDR */
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200997
Philipp Reisner7da35862011-12-19 22:42:56 +0100998 sock.socket->sk->sk_allocation = GFP_NOIO;
999 msock.socket->sk->sk_allocation = GFP_NOIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001000
Philipp Reisner7da35862011-12-19 22:42:56 +01001001 sock.socket->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
1002 msock.socket->sk->sk_priority = TC_PRIO_INTERACTIVE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001003
Philipp Reisnerb411b362009-09-25 16:07:19 -07001004 /* NOT YET ...
Philipp Reisner7da35862011-12-19 22:42:56 +01001005 * sock.socket->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
1006 * sock.socket->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
Andreas Gruenbacher60381782011-03-28 17:05:50 +02001007 * first set it to the P_CONNECTION_FEATURES timeout,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001008 * which we set to 4x the configured ping_timeout. */
Philipp Reisner44ed1672011-04-19 17:10:19 +02001009 rcu_read_lock();
1010 nc = rcu_dereference(tconn->net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001011
Philipp Reisner7da35862011-12-19 22:42:56 +01001012 sock.socket->sk->sk_sndtimeo =
1013 sock.socket->sk->sk_rcvtimeo = nc->ping_timeo*4*HZ/10;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001014
Philipp Reisner7da35862011-12-19 22:42:56 +01001015 msock.socket->sk->sk_rcvtimeo = nc->ping_int*HZ;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001016 timeout = nc->timeout * HZ / 10;
Philipp Reisner08b165b2011-09-05 16:22:33 +02001017 discard_my_data = nc->discard_my_data;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001018 rcu_read_unlock();
1019
Philipp Reisner7da35862011-12-19 22:42:56 +01001020 msock.socket->sk->sk_sndtimeo = timeout;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001021
1022 /* we don't want delays.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001023 * we use TCP_CORK where appropriate, though */
Philipp Reisner7da35862011-12-19 22:42:56 +01001024 drbd_tcp_nodelay(sock.socket);
1025 drbd_tcp_nodelay(msock.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001026
Philipp Reisner7da35862011-12-19 22:42:56 +01001027 tconn->data.socket = sock.socket;
1028 tconn->meta.socket = msock.socket;
Philipp Reisner907599e2011-02-08 11:25:37 +01001029 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001030
Andreas Gruenbacher60381782011-03-28 17:05:50 +02001031 h = drbd_do_features(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001032 if (h <= 0)
1033 return h;
1034
Philipp Reisner907599e2011-02-08 11:25:37 +01001035 if (tconn->cram_hmac_tfm) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001036 /* drbd_request_state(mdev, NS(conn, WFAuth)); */
Philipp Reisner907599e2011-02-08 11:25:37 +01001037 switch (drbd_do_auth(tconn)) {
Johannes Thomab10d96c2010-01-07 16:02:50 +01001038 case -1:
Philipp Reisner907599e2011-02-08 11:25:37 +01001039 conn_err(tconn, "Authentication of peer failed\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001040 return -1;
Johannes Thomab10d96c2010-01-07 16:02:50 +01001041 case 0:
Philipp Reisner907599e2011-02-08 11:25:37 +01001042 conn_err(tconn, "Authentication of peer failed, trying again.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01001043 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001044 }
1045 }
1046
Philipp Reisner7da35862011-12-19 22:42:56 +01001047 tconn->data.socket->sk->sk_sndtimeo = timeout;
1048 tconn->data.socket->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001049
Andreas Gruenbacher387eb302011-03-16 01:05:37 +01001050 if (drbd_send_protocol(tconn) == -EOPNOTSUPP)
Philipp Reisner7e2455c2010-04-22 14:50:23 +02001051 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001052
Philipp Reisnera1096a62012-04-06 12:07:34 +02001053 set_bit(STATE_SENT, &tconn->flags);
1054
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001055 rcu_read_lock();
1056 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1057 kref_get(&mdev->kref);
1058 rcu_read_unlock();
Philipp Reisner08b165b2011-09-05 16:22:33 +02001059
1060 if (discard_my_data)
1061 set_bit(DISCARD_MY_DATA, &mdev->flags);
1062 else
1063 clear_bit(DISCARD_MY_DATA, &mdev->flags);
1064
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001065 drbd_connected(mdev);
1066 kref_put(&mdev->kref, &drbd_minor_destroy);
1067 rcu_read_lock();
1068 }
1069 rcu_read_unlock();
1070
Philipp Reisnera1096a62012-04-06 12:07:34 +02001071 rv = conn_request_state(tconn, NS(conn, C_WF_REPORT_PARAMS), CS_VERBOSE);
1072 if (rv < SS_SUCCESS) {
1073 clear_bit(STATE_SENT, &tconn->flags);
Philipp Reisner823bd832012-11-08 15:04:36 +01001074 return 0;
Philipp Reisnera1096a62012-04-06 12:07:34 +02001075 }
Philipp Reisner823bd832012-11-08 15:04:36 +01001076
1077 drbd_thread_start(&tconn->asender);
1078
Philipp Reisner08b165b2011-09-05 16:22:33 +02001079 mutex_lock(&tconn->conf_update);
1080 /* The discard_my_data flag is a single-shot modifier to the next
1081 * connection attempt, the handshake of which is now well underway.
1082 * No need for rcu style copying of the whole struct
1083 * just to clear a single value. */
1084 tconn->net_conf->discard_my_data = 0;
1085 mutex_unlock(&tconn->conf_update);
1086
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07001087 return h;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001088
1089out_release_sockets:
Philipp Reisner7a426fd2012-07-12 14:22:37 +02001090 if (ad.s_listen)
1091 sock_release(ad.s_listen);
Philipp Reisner7da35862011-12-19 22:42:56 +01001092 if (sock.socket)
1093 sock_release(sock.socket);
1094 if (msock.socket)
1095 sock_release(msock.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001096 return -1;
1097}
1098
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001099static int decode_header(struct drbd_tconn *tconn, void *header, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001100{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001101 unsigned int header_size = drbd_header_size(tconn);
1102
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +02001103 if (header_size == sizeof(struct p_header100) &&
1104 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC_100)) {
1105 struct p_header100 *h = header;
1106 if (h->pad != 0) {
1107 conn_err(tconn, "Header padding is not zero\n");
1108 return -EINVAL;
1109 }
1110 pi->vnr = be16_to_cpu(h->volume);
1111 pi->cmd = be16_to_cpu(h->command);
1112 pi->size = be32_to_cpu(h->length);
1113 } else if (header_size == sizeof(struct p_header95) &&
1114 *(__be16 *)header == cpu_to_be16(DRBD_MAGIC_BIG)) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001115 struct p_header95 *h = header;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001116 pi->cmd = be16_to_cpu(h->command);
Andreas Gruenbacherb55d84b2011-03-22 13:17:47 +01001117 pi->size = be32_to_cpu(h->length);
1118 pi->vnr = 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001119 } else if (header_size == sizeof(struct p_header80) &&
1120 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC)) {
1121 struct p_header80 *h = header;
1122 pi->cmd = be16_to_cpu(h->command);
1123 pi->size = be16_to_cpu(h->length);
Philipp Reisner77351055b2011-02-07 17:24:26 +01001124 pi->vnr = 0;
Philipp Reisner02918be2010-08-20 14:35:10 +02001125 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001126 conn_err(tconn, "Wrong magic value 0x%08x in protocol version %d\n",
1127 be32_to_cpu(*(__be32 *)header),
1128 tconn->agreed_pro_version);
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001129 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001130 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001131 pi->data = header + header_size;
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001132 return 0;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001133}
1134
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001135static int drbd_recv_header(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner257d0af2011-01-26 12:15:29 +01001136{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001137 void *buffer = tconn->data.rbuf;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001138 int err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001139
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001140 err = drbd_recv_all_warn(tconn, buffer, drbd_header_size(tconn));
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001141 if (err)
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001142 return err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001143
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001144 err = decode_header(tconn, buffer, pi);
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001145 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001146
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001147 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001148}
1149
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001150static void drbd_flush(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001151{
1152 int rv;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001153 struct drbd_conf *mdev;
1154 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001155
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001156 if (tconn->write_ordering >= WO_bdev_flush) {
Lars Ellenberg615e0872011-11-17 14:32:12 +01001157 rcu_read_lock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001158 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Lars Ellenberg615e0872011-11-17 14:32:12 +01001159 if (!get_ldev(mdev))
1160 continue;
1161 kref_get(&mdev->kref);
1162 rcu_read_unlock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001163
Lars Ellenberg615e0872011-11-17 14:32:12 +01001164 rv = blkdev_issue_flush(mdev->ldev->backing_bdev,
1165 GFP_NOIO, NULL);
1166 if (rv) {
1167 dev_info(DEV, "local disk flush failed with status %d\n", rv);
1168 /* would rather check on EOPNOTSUPP, but that is not reliable.
1169 * don't try again for ANY return value != 0
1170 * if (rv == -EOPNOTSUPP) */
1171 drbd_bump_write_ordering(tconn, WO_drain_io);
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001172 }
Lars Ellenberg615e0872011-11-17 14:32:12 +01001173 put_ldev(mdev);
1174 kref_put(&mdev->kref, &drbd_minor_destroy);
1175
1176 rcu_read_lock();
1177 if (rv)
1178 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001179 }
Lars Ellenberg615e0872011-11-17 14:32:12 +01001180 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001181 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001182}
1183
1184/**
1185 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
1186 * @mdev: DRBD device.
1187 * @epoch: Epoch object.
1188 * @ev: Epoch event.
1189 */
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001190static enum finish_epoch drbd_may_finish_epoch(struct drbd_tconn *tconn,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001191 struct drbd_epoch *epoch,
1192 enum epoch_event ev)
1193{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001194 int epoch_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001195 struct drbd_epoch *next_epoch;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001196 enum finish_epoch rv = FE_STILL_LIVE;
1197
Philipp Reisner12038a32011-11-09 19:18:00 +01001198 spin_lock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001199 do {
1200 next_epoch = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001201
1202 epoch_size = atomic_read(&epoch->epoch_size);
1203
1204 switch (ev & ~EV_CLEANUP) {
1205 case EV_PUT:
1206 atomic_dec(&epoch->active);
1207 break;
1208 case EV_GOT_BARRIER_NR:
1209 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001210 break;
1211 case EV_BECAME_LAST:
1212 /* nothing to do*/
1213 break;
1214 }
1215
Philipp Reisnerb411b362009-09-25 16:07:19 -07001216 if (epoch_size != 0 &&
1217 atomic_read(&epoch->active) == 0 &&
Philipp Reisner85d735132011-07-18 15:45:15 +02001218 (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags) || ev & EV_CLEANUP)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001219 if (!(ev & EV_CLEANUP)) {
Philipp Reisner12038a32011-11-09 19:18:00 +01001220 spin_unlock(&tconn->epoch_lock);
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001221 drbd_send_b_ack(epoch->tconn, epoch->barrier_nr, epoch_size);
Philipp Reisner12038a32011-11-09 19:18:00 +01001222 spin_lock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001223 }
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001224#if 0
1225 /* FIXME: dec unacked on connection, once we have
1226 * something to count pending connection packets in. */
Philipp Reisner85d735132011-07-18 15:45:15 +02001227 if (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags))
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001228 dec_unacked(epoch->tconn);
1229#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -07001230
Philipp Reisner12038a32011-11-09 19:18:00 +01001231 if (tconn->current_epoch != epoch) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001232 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1233 list_del(&epoch->list);
1234 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
Philipp Reisner12038a32011-11-09 19:18:00 +01001235 tconn->epochs--;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001236 kfree(epoch);
1237
1238 if (rv == FE_STILL_LIVE)
1239 rv = FE_DESTROYED;
1240 } else {
1241 epoch->flags = 0;
1242 atomic_set(&epoch->epoch_size, 0);
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001243 /* atomic_set(&epoch->active, 0); is already zero */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001244 if (rv == FE_STILL_LIVE)
1245 rv = FE_RECYCLED;
1246 }
1247 }
1248
1249 if (!next_epoch)
1250 break;
1251
1252 epoch = next_epoch;
1253 } while (1);
1254
Philipp Reisner12038a32011-11-09 19:18:00 +01001255 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001256
Philipp Reisnerb411b362009-09-25 16:07:19 -07001257 return rv;
1258}
1259
1260/**
1261 * drbd_bump_write_ordering() - Fall back to an other write ordering method
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001262 * @tconn: DRBD connection.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001263 * @wo: Write ordering method to try.
1264 */
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001265void drbd_bump_write_ordering(struct drbd_tconn *tconn, enum write_ordering_e wo)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001266{
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001267 struct disk_conf *dc;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001268 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001269 enum write_ordering_e pwo;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001270 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001271 static char *write_ordering_str[] = {
1272 [WO_none] = "none",
1273 [WO_drain_io] = "drain",
1274 [WO_bdev_flush] = "flush",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001275 };
1276
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001277 pwo = tconn->write_ordering;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001278 wo = min(pwo, wo);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001279 rcu_read_lock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001280 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisner27eb13e2012-03-30 14:12:15 +02001281 if (!get_ldev_if_state(mdev, D_ATTACHING))
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001282 continue;
1283 dc = rcu_dereference(mdev->ldev->disk_conf);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001284
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001285 if (wo == WO_bdev_flush && !dc->disk_flushes)
1286 wo = WO_drain_io;
1287 if (wo == WO_drain_io && !dc->disk_drain)
1288 wo = WO_none;
1289 put_ldev(mdev);
1290 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001291 rcu_read_unlock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001292 tconn->write_ordering = wo;
1293 if (pwo != tconn->write_ordering || wo == WO_bdev_flush)
1294 conn_info(tconn, "Method to ensure write ordering: %s\n", write_ordering_str[tconn->write_ordering]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001295}
1296
1297/**
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001298 * drbd_submit_peer_request()
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001299 * @mdev: DRBD device.
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001300 * @peer_req: peer request
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001301 * @rw: flag field, see bio->bi_rw
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001302 *
1303 * May spread the pages to multiple bios,
1304 * depending on bio_add_page restrictions.
1305 *
1306 * Returns 0 if all bios have been submitted,
1307 * -ENOMEM if we could not allocate enough bios,
1308 * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a
1309 * single page to an empty bio (which should never happen and likely indicates
1310 * that the lower level IO stack is in some way broken). This has been observed
1311 * on certain Xen deployments.
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001312 */
1313/* TODO allocate from our own bio_set. */
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001314int drbd_submit_peer_request(struct drbd_conf *mdev,
1315 struct drbd_peer_request *peer_req,
1316 const unsigned rw, const int fault_type)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001317{
1318 struct bio *bios = NULL;
1319 struct bio *bio;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001320 struct page *page = peer_req->pages;
1321 sector_t sector = peer_req->i.sector;
1322 unsigned ds = peer_req->i.size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001323 unsigned n_bios = 0;
1324 unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001325 int err = -ENOMEM;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001326
1327 /* In most cases, we will only need one bio. But in case the lower
1328 * level restrictions happen to be different at this offset on this
1329 * side than those of the sending peer, we may need to submit the
Lars Ellenbergda4a75d2011-02-23 17:02:01 +01001330 * request in more than one bio.
1331 *
1332 * Plain bio_alloc is good enough here, this is no DRBD internally
1333 * generated bio, but a bio allocated on behalf of the peer.
1334 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001335next_bio:
1336 bio = bio_alloc(GFP_NOIO, nr_pages);
1337 if (!bio) {
1338 dev_err(DEV, "submit_ee: Allocation of a bio failed\n");
1339 goto fail;
1340 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001341 /* > peer_req->i.sector, unless this is the first bio */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001342 bio->bi_sector = sector;
1343 bio->bi_bdev = mdev->ldev->backing_bdev;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001344 bio->bi_rw = rw;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001345 bio->bi_private = peer_req;
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001346 bio->bi_end_io = drbd_peer_request_endio;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001347
1348 bio->bi_next = bios;
1349 bios = bio;
1350 ++n_bios;
1351
1352 page_chain_for_each(page) {
1353 unsigned len = min_t(unsigned, ds, PAGE_SIZE);
1354 if (!bio_add_page(bio, page, len, 0)) {
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001355 /* A single page must always be possible!
1356 * But in case it fails anyways,
1357 * we deal with it, and complain (below). */
1358 if (bio->bi_vcnt == 0) {
1359 dev_err(DEV,
1360 "bio_add_page failed for len=%u, "
1361 "bi_vcnt=0 (bi_sector=%llu)\n",
1362 len, (unsigned long long)bio->bi_sector);
1363 err = -ENOSPC;
1364 goto fail;
1365 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001366 goto next_bio;
1367 }
1368 ds -= len;
1369 sector += len >> 9;
1370 --nr_pages;
1371 }
1372 D_ASSERT(page == NULL);
1373 D_ASSERT(ds == 0);
1374
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001375 atomic_set(&peer_req->pending_bios, n_bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001376 do {
1377 bio = bios;
1378 bios = bios->bi_next;
1379 bio->bi_next = NULL;
1380
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001381 drbd_generic_make_request(mdev, fault_type, bio);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001382 } while (bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001383 return 0;
1384
1385fail:
1386 while (bios) {
1387 bio = bios;
1388 bios = bios->bi_next;
1389 bio_put(bio);
1390 }
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001391 return err;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001392}
1393
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001394static void drbd_remove_epoch_entry_interval(struct drbd_conf *mdev,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001395 struct drbd_peer_request *peer_req)
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001396{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001397 struct drbd_interval *i = &peer_req->i;
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001398
1399 drbd_remove_interval(&mdev->write_requests, i);
1400 drbd_clear_interval(i);
1401
Andreas Gruenbacher6c852be2011-02-04 15:38:52 +01001402 /* Wake up any processes waiting for this peer request to complete. */
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001403 if (i->waiting)
1404 wake_up(&mdev->misc_wait);
1405}
1406
Philipp Reisner77fede52011-11-10 21:19:11 +01001407void conn_wait_active_ee_empty(struct drbd_tconn *tconn)
1408{
1409 struct drbd_conf *mdev;
1410 int vnr;
1411
1412 rcu_read_lock();
1413 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1414 kref_get(&mdev->kref);
1415 rcu_read_unlock();
1416 drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
1417 kref_put(&mdev->kref, &drbd_minor_destroy);
1418 rcu_read_lock();
1419 }
1420 rcu_read_unlock();
1421}
1422
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001423static int receive_Barrier(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001424{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001425 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001426 struct p_barrier *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001427 struct drbd_epoch *epoch;
1428
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001429 /* FIXME these are unacked on connection,
1430 * not a specific (peer)device.
1431 */
Philipp Reisner12038a32011-11-09 19:18:00 +01001432 tconn->current_epoch->barrier_nr = p->barrier;
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001433 tconn->current_epoch->tconn = tconn;
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001434 rv = drbd_may_finish_epoch(tconn, tconn->current_epoch, EV_GOT_BARRIER_NR);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001435
1436 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
1437 * the activity log, which means it would not be resynced in case the
1438 * R_PRIMARY crashes now.
1439 * Therefore we must send the barrier_ack after the barrier request was
1440 * completed. */
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001441 switch (tconn->write_ordering) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001442 case WO_none:
1443 if (rv == FE_RECYCLED)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001444 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001445
1446 /* receiver context, in the writeout path of the other node.
1447 * avoid potential distributed deadlock */
1448 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1449 if (epoch)
1450 break;
1451 else
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001452 conn_warn(tconn, "Allocation of an epoch failed, slowing down\n");
Philipp Reisner2451fc32010-08-24 13:43:11 +02001453 /* Fall through */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001454
1455 case WO_bdev_flush:
1456 case WO_drain_io:
Philipp Reisner77fede52011-11-10 21:19:11 +01001457 conn_wait_active_ee_empty(tconn);
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001458 drbd_flush(tconn);
Philipp Reisner2451fc32010-08-24 13:43:11 +02001459
Philipp Reisner12038a32011-11-09 19:18:00 +01001460 if (atomic_read(&tconn->current_epoch->epoch_size)) {
Philipp Reisner2451fc32010-08-24 13:43:11 +02001461 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1462 if (epoch)
1463 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001464 }
1465
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001466 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001467 default:
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001468 conn_err(tconn, "Strangeness in tconn->write_ordering %d\n", tconn->write_ordering);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001469 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001470 }
1471
1472 epoch->flags = 0;
1473 atomic_set(&epoch->epoch_size, 0);
1474 atomic_set(&epoch->active, 0);
1475
Philipp Reisner12038a32011-11-09 19:18:00 +01001476 spin_lock(&tconn->epoch_lock);
1477 if (atomic_read(&tconn->current_epoch->epoch_size)) {
1478 list_add(&epoch->list, &tconn->current_epoch->list);
1479 tconn->current_epoch = epoch;
1480 tconn->epochs++;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001481 } else {
1482 /* The current_epoch got recycled while we allocated this one... */
1483 kfree(epoch);
1484 }
Philipp Reisner12038a32011-11-09 19:18:00 +01001485 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001486
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001487 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001488}
1489
1490/* used from receive_RSDataReply (recv_resync_read)
1491 * and from receive_Data */
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001492static struct drbd_peer_request *
1493read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector,
1494 int data_size) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001495{
Lars Ellenberg66660322010-04-06 12:15:04 +02001496 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001497 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001498 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001499 int dgs, ds, err;
Philipp Reisnera0638452011-01-19 14:31:32 +01001500 void *dig_in = mdev->tconn->int_dig_in;
1501 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001502 unsigned long *data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001503
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001504 dgs = 0;
1505 if (mdev->tconn->peer_integrity_tfm) {
1506 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001507 /*
1508 * FIXME: Receive the incoming digest into the receive buffer
1509 * here, together with its struct p_data?
1510 */
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001511 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1512 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001513 return NULL;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001514 data_size -= dgs;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001515 }
1516
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001517 if (!expect(IS_ALIGNED(data_size, 512)))
1518 return NULL;
1519 if (!expect(data_size <= DRBD_MAX_BIO_SIZE))
1520 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001521
Lars Ellenberg66660322010-04-06 12:15:04 +02001522 /* even though we trust out peer,
1523 * we sometimes have to double check. */
1524 if (sector + (data_size>>9) > capacity) {
Lars Ellenbergfdda6542011-01-24 15:11:01 +01001525 dev_err(DEV, "request from peer beyond end of local disk: "
1526 "capacity: %llus < sector: %llus + size: %u\n",
Lars Ellenberg66660322010-04-06 12:15:04 +02001527 (unsigned long long)capacity,
1528 (unsigned long long)sector, data_size);
1529 return NULL;
1530 }
1531
Philipp Reisnerb411b362009-09-25 16:07:19 -07001532 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1533 * "criss-cross" setup, that might cause write-out on some other DRBD,
1534 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02001535 peer_req = drbd_alloc_peer_req(mdev, id, sector, data_size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001536 if (!peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001537 return NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001538
Lars Ellenberg81a35372012-07-30 09:00:54 +02001539 if (!data_size)
1540 return peer_req;
1541
Philipp Reisnerb411b362009-09-25 16:07:19 -07001542 ds = data_size;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001543 page = peer_req->pages;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001544 page_chain_for_each(page) {
1545 unsigned len = min_t(int, ds, PAGE_SIZE);
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001546 data = kmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001547 err = drbd_recv_all_warn(mdev->tconn, data, len);
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +01001548 if (drbd_insert_fault(mdev, DRBD_FAULT_RECEIVE)) {
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001549 dev_err(DEV, "Fault injection: Corrupting data on receive\n");
1550 data[0] = data[0] ^ (unsigned long)-1;
1551 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001552 kunmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001553 if (err) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001554 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001555 return NULL;
1556 }
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001557 ds -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001558 }
1559
1560 if (dgs) {
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02001561 drbd_csum_ee(mdev, mdev->tconn->peer_integrity_tfm, peer_req, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001562 if (memcmp(dig_in, dig_vv, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001563 dev_err(DEV, "Digest integrity check FAILED: %llus +%u\n",
1564 (unsigned long long)sector, data_size);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001565 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001566 return NULL;
1567 }
1568 }
1569 mdev->recv_cnt += data_size>>9;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001570 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001571}
1572
1573/* drbd_drain_block() just takes a data block
1574 * out of the socket input buffer, and discards it.
1575 */
1576static int drbd_drain_block(struct drbd_conf *mdev, int data_size)
1577{
1578 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001579 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001580 void *data;
1581
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001582 if (!data_size)
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001583 return 0;
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001584
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +02001585 page = drbd_alloc_pages(mdev, 1, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001586
1587 data = kmap(page);
1588 while (data_size) {
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001589 unsigned int len = min_t(int, data_size, PAGE_SIZE);
1590
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001591 err = drbd_recv_all_warn(mdev->tconn, data, len);
1592 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001593 break;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001594 data_size -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001595 }
1596 kunmap(page);
Andreas Gruenbacher5cc287e2011-04-07 21:02:59 +02001597 drbd_free_pages(mdev, page, 0);
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001598 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001599}
1600
1601static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req,
1602 sector_t sector, int data_size)
1603{
1604 struct bio_vec *bvec;
1605 struct bio *bio;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001606 int dgs, err, i, expect;
Philipp Reisnera0638452011-01-19 14:31:32 +01001607 void *dig_in = mdev->tconn->int_dig_in;
1608 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001609
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001610 dgs = 0;
1611 if (mdev->tconn->peer_integrity_tfm) {
1612 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001613 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1614 if (err)
1615 return err;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001616 data_size -= dgs;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001617 }
1618
Philipp Reisnerb411b362009-09-25 16:07:19 -07001619 /* optimistically update recv_cnt. if receiving fails below,
1620 * we disconnect anyways, and counters will be reset. */
1621 mdev->recv_cnt += data_size>>9;
1622
1623 bio = req->master_bio;
1624 D_ASSERT(sector == bio->bi_sector);
1625
1626 bio_for_each_segment(bvec, bio, i) {
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001627 void *mapped = kmap(bvec->bv_page) + bvec->bv_offset;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001628 expect = min_t(int, data_size, bvec->bv_len);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001629 err = drbd_recv_all_warn(mdev->tconn, mapped, expect);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001630 kunmap(bvec->bv_page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001631 if (err)
1632 return err;
1633 data_size -= expect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001634 }
1635
1636 if (dgs) {
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02001637 drbd_csum_bio(mdev, mdev->tconn->peer_integrity_tfm, bio, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001638 if (memcmp(dig_in, dig_vv, dgs)) {
1639 dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n");
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001640 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001641 }
1642 }
1643
1644 D_ASSERT(data_size == 0);
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001645 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001646}
1647
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001648/*
1649 * e_end_resync_block() is called in asender context via
1650 * drbd_finish_peer_reqs().
1651 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001652static int e_end_resync_block(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001653{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001654 struct drbd_peer_request *peer_req =
1655 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001656 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001657 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001658 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001659
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001660 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001661
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001662 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1663 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001664 err = drbd_send_ack(mdev, P_RS_WRITE_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001665 } else {
1666 /* Record failure to sync */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001667 drbd_rs_failed_io(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001668
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001669 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001670 }
1671 dec_unacked(mdev);
1672
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001673 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001674}
1675
1676static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local)
1677{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001678 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001679
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001680 peer_req = read_in_block(mdev, ID_SYNCER, sector, data_size);
1681 if (!peer_req)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001682 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001683
1684 dec_rs_pending(mdev);
1685
Philipp Reisnerb411b362009-09-25 16:07:19 -07001686 inc_unacked(mdev);
1687 /* corresponding dec_unacked() in e_end_resync_block()
1688 * respective _drbd_clear_done_ee */
1689
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001690 peer_req->w.cb = e_end_resync_block;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001691
Philipp Reisner87eeee42011-01-19 14:16:30 +01001692 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001693 list_add(&peer_req->w.list, &mdev->sync_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001694 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001695
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001696 atomic_add(data_size >> 9, &mdev->rs_sect_ev);
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001697 if (drbd_submit_peer_request(mdev, peer_req, WRITE, DRBD_FAULT_RS_WR) == 0)
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001698 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001699
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001700 /* don't care for the reason here */
1701 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01001702 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001703 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001704 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001705
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001706 drbd_free_peer_req(mdev, peer_req);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001707fail:
1708 put_ldev(mdev);
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001709 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001710}
1711
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001712static struct drbd_request *
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001713find_request(struct drbd_conf *mdev, struct rb_root *root, u64 id,
1714 sector_t sector, bool missing_ok, const char *func)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001715{
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001716 struct drbd_request *req;
1717
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001718 /* Request object according to our peer */
1719 req = (struct drbd_request *)(unsigned long)id;
Andreas Gruenbacher5e472262011-01-27 14:42:51 +01001720 if (drbd_contains_interval(root, sector, &req->i) && req->i.local)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001721 return req;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001722 if (!missing_ok) {
Andreas Gruenbacher5af172e2011-07-15 09:43:23 +02001723 dev_err(DEV, "%s: failed to find request 0x%lx, sector %llus\n", func,
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001724 (unsigned long)id, (unsigned long long)sector);
1725 }
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001726 return NULL;
1727}
1728
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001729static int receive_DataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001730{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001731 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001732 struct drbd_request *req;
1733 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001734 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001735 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001736
1737 mdev = vnr_to_mdev(tconn, pi->vnr);
1738 if (!mdev)
1739 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001740
1741 sector = be64_to_cpu(p->sector);
1742
Philipp Reisner87eeee42011-01-19 14:16:30 +01001743 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001744 req = find_request(mdev, &mdev->read_requests, p->block_id, sector, false, __func__);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001745 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001746 if (unlikely(!req))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001747 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001748
Bart Van Assche24c48302011-05-21 18:32:29 +02001749 /* hlist_del(&req->collision) is done in _req_may_be_done, to avoid
Philipp Reisnerb411b362009-09-25 16:07:19 -07001750 * special casing it there for the various failure cases.
1751 * still no race with drbd_fail_pending_reads */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001752 err = recv_dless_read(mdev, req, sector, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001753 if (!err)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001754 req_mod(req, DATA_RECEIVED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001755 /* else: nothing. handled from drbd_disconnect...
1756 * I don't think we may complete this just yet
1757 * in case we are "on-disconnect: freeze" */
1758
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001759 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001760}
1761
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001762static int receive_RSDataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001763{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001764 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001765 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001766 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001767 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001768
1769 mdev = vnr_to_mdev(tconn, pi->vnr);
1770 if (!mdev)
1771 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001772
1773 sector = be64_to_cpu(p->sector);
1774 D_ASSERT(p->block_id == ID_SYNCER);
1775
1776 if (get_ldev(mdev)) {
1777 /* data is submitted to disk within recv_resync_read.
1778 * corresponding put_ldev done below on error,
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001779 * or in drbd_peer_request_endio. */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001780 err = recv_resync_read(mdev, sector, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001781 } else {
1782 if (__ratelimit(&drbd_ratelimit_state))
1783 dev_err(DEV, "Can not write resync data to local disk.\n");
1784
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001785 err = drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001786
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001787 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001788 }
1789
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001790 atomic_add(pi->size >> 9, &mdev->rs_sect_in);
Philipp Reisner778f2712010-07-06 11:14:00 +02001791
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001792 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001793}
1794
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001795static void restart_conflicting_writes(struct drbd_conf *mdev,
1796 sector_t sector, int size)
1797{
1798 struct drbd_interval *i;
1799 struct drbd_request *req;
1800
1801 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1802 if (!i->local)
1803 continue;
1804 req = container_of(i, struct drbd_request, i);
1805 if (req->rq_state & RQ_LOCAL_PENDING ||
1806 !(req->rq_state & RQ_POSTPONED))
1807 continue;
Lars Ellenberg2312f0b32011-11-24 10:36:25 +01001808 /* as it is RQ_POSTPONED, this will cause it to
1809 * be queued on the retry workqueue. */
1810 __req_mod(req, DISCARD_WRITE, NULL);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001811 }
1812}
1813
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001814/*
1815 * e_end_block() is called in asender context via drbd_finish_peer_reqs().
Philipp Reisnerb411b362009-09-25 16:07:19 -07001816 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001817static int e_end_block(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001818{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001819 struct drbd_peer_request *peer_req =
1820 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001821 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001822 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001823 int err = 0, pcmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001824
Philipp Reisner303d1442011-04-13 16:24:47 -07001825 if (peer_req->flags & EE_SEND_WRITE_ACK) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001826 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001827 pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
1828 mdev->state.conn <= C_PAUSED_SYNC_T &&
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001829 peer_req->flags & EE_MAY_SET_IN_SYNC) ?
Philipp Reisnerb411b362009-09-25 16:07:19 -07001830 P_RS_WRITE_ACK : P_WRITE_ACK;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001831 err = drbd_send_ack(mdev, pcmd, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001832 if (pcmd == P_RS_WRITE_ACK)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001833 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001834 } else {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001835 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001836 /* we expect it to be marked out of sync anyways...
1837 * maybe assert this? */
1838 }
1839 dec_unacked(mdev);
1840 }
1841 /* we delete from the conflict detection hash _after_ we sent out the
1842 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
Philipp Reisner302bdea2011-04-21 11:36:49 +02001843 if (peer_req->flags & EE_IN_INTERVAL_TREE) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01001844 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001845 D_ASSERT(!drbd_interval_empty(&peer_req->i));
1846 drbd_remove_epoch_entry_interval(mdev, peer_req);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001847 if (peer_req->flags & EE_RESTART_REQUESTS)
1848 restart_conflicting_writes(mdev, sector, peer_req->i.size);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001849 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +01001850 } else
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001851 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001852
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001853 drbd_may_finish_epoch(mdev->tconn, peer_req->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001854
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001855 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001856}
1857
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001858static int e_send_ack(struct drbd_work *w, enum drbd_packet ack)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001859{
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001860 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001861 struct drbd_peer_request *peer_req =
1862 container_of(w, struct drbd_peer_request, w);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001863 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001864
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001865 err = drbd_send_ack(mdev, ack, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001866 dec_unacked(mdev);
1867
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001868 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001869}
1870
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001871static int e_send_discard_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001872{
1873 return e_send_ack(w, P_DISCARD_WRITE);
1874}
1875
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001876static int e_send_retry_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001877{
1878 struct drbd_tconn *tconn = w->mdev->tconn;
1879
1880 return e_send_ack(w, tconn->agreed_pro_version >= 100 ?
1881 P_RETRY_WRITE : P_DISCARD_WRITE);
1882}
1883
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001884static bool seq_greater(u32 a, u32 b)
1885{
1886 /*
1887 * We assume 32-bit wrap-around here.
1888 * For 24-bit wrap-around, we would have to shift:
1889 * a <<= 8; b <<= 8;
1890 */
1891 return (s32)a - (s32)b > 0;
1892}
1893
1894static u32 seq_max(u32 a, u32 b)
1895{
1896 return seq_greater(a, b) ? a : b;
1897}
1898
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001899static bool need_peer_seq(struct drbd_conf *mdev)
1900{
1901 struct drbd_tconn *tconn = mdev->tconn;
Philipp Reisner302bdea2011-04-21 11:36:49 +02001902 int tp;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001903
1904 /*
1905 * We only need to keep track of the last packet_seq number of our peer
1906 * if we are in dual-primary mode and we have the discard flag set; see
1907 * handle_write_conflicts().
1908 */
Philipp Reisner302bdea2011-04-21 11:36:49 +02001909
1910 rcu_read_lock();
1911 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
1912 rcu_read_unlock();
1913
1914 return tp && test_bit(DISCARD_CONCURRENT, &tconn->flags);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001915}
1916
Andreas Gruenbacher43ae0772011-02-03 18:42:08 +01001917static void update_peer_seq(struct drbd_conf *mdev, unsigned int peer_seq)
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001918{
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001919 unsigned int newest_peer_seq;
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001920
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001921 if (need_peer_seq(mdev)) {
1922 spin_lock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001923 newest_peer_seq = seq_max(mdev->peer_seq, peer_seq);
1924 mdev->peer_seq = newest_peer_seq;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001925 spin_unlock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001926 /* wake up only if we actually changed mdev->peer_seq */
1927 if (peer_seq == newest_peer_seq)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001928 wake_up(&mdev->seq_wait);
1929 }
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001930}
1931
Lars Ellenbergd93f6302012-03-26 15:49:13 +02001932static inline int overlaps(sector_t s1, int l1, sector_t s2, int l2)
1933{
1934 return !((s1 + (l1>>9) <= s2) || (s1 >= s2 + (l2>>9)));
1935}
1936
1937/* maybe change sync_ee into interval trees as well? */
Philipp Reisner3ea35df2012-04-06 12:13:18 +02001938static bool overlapping_resync_write(struct drbd_conf *mdev, struct drbd_peer_request *peer_req)
Lars Ellenbergd93f6302012-03-26 15:49:13 +02001939{
1940 struct drbd_peer_request *rs_req;
1941 bool rv = 0;
1942
1943 spin_lock_irq(&mdev->tconn->req_lock);
1944 list_for_each_entry(rs_req, &mdev->sync_ee, w.list) {
1945 if (overlaps(peer_req->i.sector, peer_req->i.size,
1946 rs_req->i.sector, rs_req->i.size)) {
1947 rv = 1;
1948 break;
1949 }
1950 }
1951 spin_unlock_irq(&mdev->tconn->req_lock);
1952
Lars Ellenbergd93f6302012-03-26 15:49:13 +02001953 return rv;
1954}
1955
Philipp Reisnerb411b362009-09-25 16:07:19 -07001956/* Called from receive_Data.
1957 * Synchronize packets on sock with packets on msock.
1958 *
1959 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
1960 * packet traveling on msock, they are still processed in the order they have
1961 * been sent.
1962 *
1963 * Note: we don't care for Ack packets overtaking P_DATA packets.
1964 *
1965 * In case packet_seq is larger than mdev->peer_seq number, there are
1966 * outstanding packets on the msock. We wait for them to arrive.
1967 * In case we are the logically next packet, we update mdev->peer_seq
1968 * ourselves. Correctly handles 32bit wrap around.
1969 *
1970 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
1971 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
1972 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
1973 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
1974 *
1975 * returns 0 if we may process the packet,
1976 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001977static int wait_for_and_update_peer_seq(struct drbd_conf *mdev, const u32 peer_seq)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001978{
1979 DEFINE_WAIT(wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001980 long timeout;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001981 int ret;
1982
1983 if (!need_peer_seq(mdev))
1984 return 0;
1985
Philipp Reisnerb411b362009-09-25 16:07:19 -07001986 spin_lock(&mdev->peer_seq_lock);
1987 for (;;) {
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001988 if (!seq_greater(peer_seq - 1, mdev->peer_seq)) {
1989 mdev->peer_seq = seq_max(mdev->peer_seq, peer_seq);
1990 ret = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001991 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001992 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001993 if (signal_pending(current)) {
1994 ret = -ERESTARTSYS;
1995 break;
1996 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001997 prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001998 spin_unlock(&mdev->peer_seq_lock);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001999 rcu_read_lock();
2000 timeout = rcu_dereference(mdev->tconn->net_conf)->ping_timeo*HZ/10;
2001 rcu_read_unlock();
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01002002 timeout = schedule_timeout(timeout);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002003 spin_lock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002004 if (!timeout) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002005 ret = -ETIMEDOUT;
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01002006 dev_err(DEV, "Timed out waiting for missing ack packets; disconnecting\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002007 break;
2008 }
2009 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002010 spin_unlock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002011 finish_wait(&mdev->seq_wait, &wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002012 return ret;
2013}
2014
Lars Ellenberg688593c2010-11-17 22:25:03 +01002015/* see also bio_flags_to_wire()
2016 * DRBD_REQ_*, because we need to semantically map the flags to data packet
2017 * flags and back. We may replicate to other kernel versions. */
2018static unsigned long wire_flags_to_bio(struct drbd_conf *mdev, u32 dpf)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02002019{
Lars Ellenberg688593c2010-11-17 22:25:03 +01002020 return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
2021 (dpf & DP_FUA ? REQ_FUA : 0) |
2022 (dpf & DP_FLUSH ? REQ_FLUSH : 0) |
2023 (dpf & DP_DISCARD ? REQ_DISCARD : 0);
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02002024}
2025
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002026static void fail_postponed_requests(struct drbd_conf *mdev, sector_t sector,
2027 unsigned int size)
2028{
2029 struct drbd_interval *i;
2030
2031 repeat:
2032 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
2033 struct drbd_request *req;
2034 struct bio_and_error m;
2035
2036 if (!i->local)
2037 continue;
2038 req = container_of(i, struct drbd_request, i);
2039 if (!(req->rq_state & RQ_POSTPONED))
2040 continue;
2041 req->rq_state &= ~RQ_POSTPONED;
2042 __req_mod(req, NEG_ACKED, &m);
2043 spin_unlock_irq(&mdev->tconn->req_lock);
2044 if (m.bio)
2045 complete_master_bio(mdev, &m);
2046 spin_lock_irq(&mdev->tconn->req_lock);
2047 goto repeat;
2048 }
2049}
2050
2051static int handle_write_conflicts(struct drbd_conf *mdev,
2052 struct drbd_peer_request *peer_req)
2053{
2054 struct drbd_tconn *tconn = mdev->tconn;
2055 bool resolve_conflicts = test_bit(DISCARD_CONCURRENT, &tconn->flags);
2056 sector_t sector = peer_req->i.sector;
2057 const unsigned int size = peer_req->i.size;
2058 struct drbd_interval *i;
2059 bool equal;
2060 int err;
2061
2062 /*
2063 * Inserting the peer request into the write_requests tree will prevent
2064 * new conflicting local requests from being added.
2065 */
2066 drbd_insert_interval(&mdev->write_requests, &peer_req->i);
2067
2068 repeat:
2069 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
2070 if (i == &peer_req->i)
2071 continue;
2072
2073 if (!i->local) {
2074 /*
2075 * Our peer has sent a conflicting remote request; this
2076 * should not happen in a two-node setup. Wait for the
2077 * earlier peer request to complete.
2078 */
2079 err = drbd_wait_misc(mdev, i);
2080 if (err)
2081 goto out;
2082 goto repeat;
2083 }
2084
2085 equal = i->sector == sector && i->size == size;
2086 if (resolve_conflicts) {
2087 /*
2088 * If the peer request is fully contained within the
2089 * overlapping request, it can be discarded; otherwise,
2090 * it will be retried once all overlapping requests
2091 * have completed.
2092 */
2093 bool discard = i->sector <= sector && i->sector +
2094 (i->size >> 9) >= sector + (size >> 9);
2095
2096 if (!equal)
2097 dev_alert(DEV, "Concurrent writes detected: "
2098 "local=%llus +%u, remote=%llus +%u, "
2099 "assuming %s came first\n",
2100 (unsigned long long)i->sector, i->size,
2101 (unsigned long long)sector, size,
2102 discard ? "local" : "remote");
2103
2104 inc_unacked(mdev);
2105 peer_req->w.cb = discard ? e_send_discard_write :
2106 e_send_retry_write;
2107 list_add_tail(&peer_req->w.list, &mdev->done_ee);
2108 wake_asender(mdev->tconn);
2109
2110 err = -ENOENT;
2111 goto out;
2112 } else {
2113 struct drbd_request *req =
2114 container_of(i, struct drbd_request, i);
2115
2116 if (!equal)
2117 dev_alert(DEV, "Concurrent writes detected: "
2118 "local=%llus +%u, remote=%llus +%u\n",
2119 (unsigned long long)i->sector, i->size,
2120 (unsigned long long)sector, size);
2121
2122 if (req->rq_state & RQ_LOCAL_PENDING ||
2123 !(req->rq_state & RQ_POSTPONED)) {
2124 /*
2125 * Wait for the node with the discard flag to
2126 * decide if this request will be discarded or
2127 * retried. Requests that are discarded will
2128 * disappear from the write_requests tree.
2129 *
2130 * In addition, wait for the conflicting
2131 * request to finish locally before submitting
2132 * the conflicting peer request.
2133 */
2134 err = drbd_wait_misc(mdev, &req->i);
2135 if (err) {
2136 _conn_request_state(mdev->tconn,
2137 NS(conn, C_TIMEOUT),
2138 CS_HARD);
2139 fail_postponed_requests(mdev, sector, size);
2140 goto out;
2141 }
2142 goto repeat;
2143 }
2144 /*
2145 * Remember to restart the conflicting requests after
2146 * the new peer request has completed.
2147 */
2148 peer_req->flags |= EE_RESTART_REQUESTS;
2149 }
2150 }
2151 err = 0;
2152
2153 out:
2154 if (err)
2155 drbd_remove_epoch_entry_interval(mdev, peer_req);
2156 return err;
2157}
2158
Philipp Reisnerb411b362009-09-25 16:07:19 -07002159/* mirrored write */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002160static int receive_Data(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002161{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002162 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002163 sector_t sector;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002164 struct drbd_peer_request *peer_req;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002165 struct p_data *p = pi->data;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002166 u32 peer_seq = be32_to_cpu(p->seq_num);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002167 int rw = WRITE;
2168 u32 dp_flags;
Philipp Reisner302bdea2011-04-21 11:36:49 +02002169 int err, tp;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002170
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002171 mdev = vnr_to_mdev(tconn, pi->vnr);
2172 if (!mdev)
2173 return -EIO;
2174
Philipp Reisnerb411b362009-09-25 16:07:19 -07002175 if (!get_ldev(mdev)) {
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002176 int err2;
2177
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002178 err = wait_for_and_update_peer_seq(mdev, peer_seq);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002179 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisner12038a32011-11-09 19:18:00 +01002180 atomic_inc(&tconn->current_epoch->epoch_size);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002181 err2 = drbd_drain_block(mdev, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002182 if (!err)
2183 err = err2;
2184 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002185 }
2186
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01002187 /*
2188 * Corresponding put_ldev done either below (on various errors), or in
2189 * drbd_peer_request_endio, if we successfully submit the data at the
2190 * end of this function.
2191 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002192
2193 sector = be64_to_cpu(p->sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002194 peer_req = read_in_block(mdev, p->block_id, sector, pi->size);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002195 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002196 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002197 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002198 }
2199
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002200 peer_req->w.cb = e_end_block;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002201
Lars Ellenberg688593c2010-11-17 22:25:03 +01002202 dp_flags = be32_to_cpu(p->dp_flags);
2203 rw |= wire_flags_to_bio(mdev, dp_flags);
Lars Ellenberg81a35372012-07-30 09:00:54 +02002204 if (peer_req->pages == NULL) {
2205 D_ASSERT(peer_req->i.size == 0);
2206 D_ASSERT(dp_flags & DP_FLUSH);
2207 }
Lars Ellenberg688593c2010-11-17 22:25:03 +01002208
2209 if (dp_flags & DP_MAY_SET_IN_SYNC)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002210 peer_req->flags |= EE_MAY_SET_IN_SYNC;
Lars Ellenberg688593c2010-11-17 22:25:03 +01002211
Philipp Reisner12038a32011-11-09 19:18:00 +01002212 spin_lock(&tconn->epoch_lock);
2213 peer_req->epoch = tconn->current_epoch;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002214 atomic_inc(&peer_req->epoch->epoch_size);
2215 atomic_inc(&peer_req->epoch->active);
Philipp Reisner12038a32011-11-09 19:18:00 +01002216 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002217
Philipp Reisner302bdea2011-04-21 11:36:49 +02002218 rcu_read_lock();
2219 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
2220 rcu_read_unlock();
2221 if (tp) {
2222 peer_req->flags |= EE_IN_INTERVAL_TREE;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002223 err = wait_for_and_update_peer_seq(mdev, peer_seq);
2224 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002225 goto out_interrupted;
Philipp Reisner87eeee42011-01-19 14:16:30 +01002226 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002227 err = handle_write_conflicts(mdev, peer_req);
2228 if (err) {
2229 spin_unlock_irq(&mdev->tconn->req_lock);
2230 if (err == -ENOENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002231 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002232 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002233 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002234 goto out_interrupted;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002235 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002236 } else
2237 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002238 list_add(&peer_req->w.list, &mdev->active_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002239 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002240
Lars Ellenbergd93f6302012-03-26 15:49:13 +02002241 if (mdev->state.conn == C_SYNC_TARGET)
Philipp Reisner3ea35df2012-04-06 12:13:18 +02002242 wait_event(mdev->ee_wait, !overlapping_resync_write(mdev, peer_req));
Lars Ellenbergd93f6302012-03-26 15:49:13 +02002243
Philipp Reisner303d1442011-04-13 16:24:47 -07002244 if (mdev->tconn->agreed_pro_version < 100) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02002245 rcu_read_lock();
2246 switch (rcu_dereference(mdev->tconn->net_conf)->wire_protocol) {
Philipp Reisner303d1442011-04-13 16:24:47 -07002247 case DRBD_PROT_C:
2248 dp_flags |= DP_SEND_WRITE_ACK;
2249 break;
2250 case DRBD_PROT_B:
2251 dp_flags |= DP_SEND_RECEIVE_ACK;
2252 break;
2253 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002254 rcu_read_unlock();
Philipp Reisner303d1442011-04-13 16:24:47 -07002255 }
2256
2257 if (dp_flags & DP_SEND_WRITE_ACK) {
2258 peer_req->flags |= EE_SEND_WRITE_ACK;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002259 inc_unacked(mdev);
2260 /* corresponding dec_unacked() in e_end_block()
2261 * respective _drbd_clear_done_ee */
Philipp Reisner303d1442011-04-13 16:24:47 -07002262 }
2263
2264 if (dp_flags & DP_SEND_RECEIVE_ACK) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002265 /* I really don't like it that the receiver thread
2266 * sends on the msock, but anyways */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002267 drbd_send_ack(mdev, P_RECV_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002268 }
2269
Lars Ellenberg6719fb02010-10-18 23:04:07 +02002270 if (mdev->state.pdsk < D_INCONSISTENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002271 /* In case we have the only disk of the cluster, */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002272 drbd_set_out_of_sync(mdev, peer_req->i.sector, peer_req->i.size);
2273 peer_req->flags |= EE_CALL_AL_COMPLETE_IO;
2274 peer_req->flags &= ~EE_MAY_SET_IN_SYNC;
Lars Ellenberg181286a2011-03-31 15:18:56 +02002275 drbd_al_begin_io(mdev, &peer_req->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002276 }
2277
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002278 err = drbd_submit_peer_request(mdev, peer_req, rw, DRBD_FAULT_DT_WR);
2279 if (!err)
2280 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002281
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002282 /* don't care for the reason here */
2283 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002284 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002285 list_del(&peer_req->w.list);
2286 drbd_remove_epoch_entry_interval(mdev, peer_req);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002287 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002288 if (peer_req->flags & EE_CALL_AL_COMPLETE_IO)
Lars Ellenberg181286a2011-03-31 15:18:56 +02002289 drbd_al_complete_io(mdev, &peer_req->i);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002290
Philipp Reisnerb411b362009-09-25 16:07:19 -07002291out_interrupted:
Philipp Reisner1e9dd292011-11-10 15:14:53 +01002292 drbd_may_finish_epoch(tconn, peer_req->epoch, EV_PUT + EV_CLEANUP);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002293 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002294 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002295 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002296}
2297
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002298/* We may throttle resync, if the lower device seems to be busy,
2299 * and current sync rate is above c_min_rate.
2300 *
2301 * To decide whether or not the lower device is busy, we use a scheme similar
2302 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
2303 * (more than 64 sectors) of activity we cannot account for with our own resync
2304 * activity, it obviously is "busy".
2305 *
2306 * The current sync rate used here uses only the most recent two step marks,
2307 * to have a short time average so we can react faster.
2308 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002309int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002310{
2311 struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
2312 unsigned long db, dt, dbdt;
Philipp Reisnere3555d82010-11-07 15:56:29 +01002313 struct lc_element *tmp;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002314 int curr_events;
2315 int throttle = 0;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002316 unsigned int c_min_rate;
2317
2318 rcu_read_lock();
2319 c_min_rate = rcu_dereference(mdev->ldev->disk_conf)->c_min_rate;
2320 rcu_read_unlock();
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002321
2322 /* feature disabled? */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002323 if (c_min_rate == 0)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002324 return 0;
2325
Philipp Reisnere3555d82010-11-07 15:56:29 +01002326 spin_lock_irq(&mdev->al_lock);
2327 tmp = lc_find(mdev->resync, BM_SECT_TO_EXT(sector));
2328 if (tmp) {
2329 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
2330 if (test_bit(BME_PRIORITY, &bm_ext->flags)) {
2331 spin_unlock_irq(&mdev->al_lock);
2332 return 0;
2333 }
2334 /* Do not slow down if app IO is already waiting for this extent */
2335 }
2336 spin_unlock_irq(&mdev->al_lock);
2337
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002338 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
2339 (int)part_stat_read(&disk->part0, sectors[1]) -
2340 atomic_read(&mdev->rs_sect_ev);
Philipp Reisnere3555d82010-11-07 15:56:29 +01002341
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002342 if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
2343 unsigned long rs_left;
2344 int i;
2345
2346 mdev->rs_last_events = curr_events;
2347
2348 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
2349 * approx. */
Lars Ellenberg2649f082010-11-05 10:05:47 +01002350 i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
2351
2352 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
2353 rs_left = mdev->ov_left;
2354 else
2355 rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002356
2357 dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
2358 if (!dt)
2359 dt++;
2360 db = mdev->rs_mark_left[i] - rs_left;
2361 dbdt = Bit2KB(db/dt);
2362
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002363 if (dbdt > c_min_rate)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002364 throttle = 1;
2365 }
2366 return throttle;
2367}
2368
2369
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002370static int receive_DataRequest(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002371{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002372 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002373 sector_t sector;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002374 sector_t capacity;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002375 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002376 struct digest_info *di = NULL;
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002377 int size, verb;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002378 unsigned int fault_type;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002379 struct p_block_req *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002380
2381 mdev = vnr_to_mdev(tconn, pi->vnr);
2382 if (!mdev)
2383 return -EIO;
2384 capacity = drbd_get_capacity(mdev->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002385
2386 sector = be64_to_cpu(p->sector);
2387 size = be32_to_cpu(p->blksize);
2388
Andreas Gruenbacherc670a392011-02-21 12:41:39 +01002389 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002390 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2391 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002392 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002393 }
2394 if (sector + (size>>9) > capacity) {
2395 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2396 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002397 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002398 }
2399
2400 if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002401 verb = 1;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002402 switch (pi->cmd) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002403 case P_DATA_REQUEST:
2404 drbd_send_ack_rp(mdev, P_NEG_DREPLY, p);
2405 break;
2406 case P_RS_DATA_REQUEST:
2407 case P_CSUM_RS_REQUEST:
2408 case P_OV_REQUEST:
2409 drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p);
2410 break;
2411 case P_OV_REPLY:
2412 verb = 0;
2413 dec_rs_pending(mdev);
2414 drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC);
2415 break;
2416 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002417 BUG();
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002418 }
2419 if (verb && __ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002420 dev_err(DEV, "Can not satisfy peer's read request, "
2421 "no local data.\n");
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002422
Lars Ellenberga821cc42010-09-06 12:31:37 +02002423 /* drain possibly payload */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002424 return drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002425 }
2426
2427 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
2428 * "criss-cross" setup, that might cause write-out on some other DRBD,
2429 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02002430 peer_req = drbd_alloc_peer_req(mdev, p->block_id, sector, size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002431 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002432 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002433 return -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002434 }
2435
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002436 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002437 case P_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002438 peer_req->w.cb = w_e_end_data_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002439 fault_type = DRBD_FAULT_DT_RD;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002440 /* application IO, don't drbd_rs_begin_io */
2441 goto submit;
2442
Philipp Reisnerb411b362009-09-25 16:07:19 -07002443 case P_RS_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002444 peer_req->w.cb = w_e_end_rsdata_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002445 fault_type = DRBD_FAULT_RS_RD;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002446 /* used in the sector offset progress display */
2447 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002448 break;
2449
2450 case P_OV_REPLY:
2451 case P_CSUM_RS_REQUEST:
2452 fault_type = DRBD_FAULT_RS_RD;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002453 di = kmalloc(sizeof(*di) + pi->size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002454 if (!di)
2455 goto out_free_e;
2456
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002457 di->digest_size = pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002458 di->digest = (((char *)di)+sizeof(struct digest_info));
2459
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002460 peer_req->digest = di;
2461 peer_req->flags |= EE_HAS_DIGEST;
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +02002462
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002463 if (drbd_recv_all(mdev->tconn, di->digest, pi->size))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002464 goto out_free_e;
2465
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002466 if (pi->cmd == P_CSUM_RS_REQUEST) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002467 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002468 peer_req->w.cb = w_e_end_csum_rs_req;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002469 /* used in the sector offset progress display */
2470 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002471 } else if (pi->cmd == P_OV_REPLY) {
Lars Ellenberg2649f082010-11-05 10:05:47 +01002472 /* track progress, we may need to throttle */
2473 atomic_add(size >> 9, &mdev->rs_sect_in);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002474 peer_req->w.cb = w_e_end_ov_reply;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002475 dec_rs_pending(mdev);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002476 /* drbd_rs_begin_io done when we sent this request,
2477 * but accounting still needs to be done. */
2478 goto submit_for_resync;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002479 }
2480 break;
2481
2482 case P_OV_REQUEST:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002483 if (mdev->ov_start_sector == ~(sector_t)0 &&
Philipp Reisner31890f42011-01-19 14:12:51 +01002484 mdev->tconn->agreed_pro_version >= 90) {
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002485 unsigned long now = jiffies;
2486 int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002487 mdev->ov_start_sector = sector;
2488 mdev->ov_position = sector;
Lars Ellenberg30b743a2010-11-05 09:39:06 +01002489 mdev->ov_left = drbd_bm_bits(mdev) - BM_SECT_TO_BIT(sector);
2490 mdev->rs_total = mdev->ov_left;
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002491 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
2492 mdev->rs_mark_left[i] = mdev->ov_left;
2493 mdev->rs_mark_time[i] = now;
2494 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002495 dev_info(DEV, "Online Verify start sector: %llu\n",
2496 (unsigned long long)sector);
2497 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002498 peer_req->w.cb = w_e_end_ov_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002499 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002500 break;
2501
Philipp Reisnerb411b362009-09-25 16:07:19 -07002502 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002503 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002504 }
2505
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002506 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2507 * wrt the receiver, but it is not as straightforward as it may seem.
2508 * Various places in the resync start and stop logic assume resync
2509 * requests are processed in order, requeuing this on the worker thread
2510 * introduces a bunch of new code for synchronization between threads.
2511 *
2512 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2513 * "forever", throttling after drbd_rs_begin_io will lock that extent
2514 * for application writes for the same time. For now, just throttle
2515 * here, where the rest of the code expects the receiver to sleep for
2516 * a while, anyways.
2517 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002518
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002519 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2520 * this defers syncer requests for some time, before letting at least
2521 * on request through. The resync controller on the receiving side
2522 * will adapt to the incoming rate accordingly.
2523 *
2524 * We cannot throttle here if remote is Primary/SyncTarget:
2525 * we would also throttle its application reads.
2526 * In that case, throttling is done on the SyncTarget only.
2527 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002528 if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev, sector))
2529 schedule_timeout_uninterruptible(HZ/10);
2530 if (drbd_rs_begin_io(mdev, sector))
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002531 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002532
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002533submit_for_resync:
2534 atomic_add(size >> 9, &mdev->rs_sect_ev);
2535
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002536submit:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002537 inc_unacked(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002538 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002539 list_add_tail(&peer_req->w.list, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002540 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002541
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01002542 if (drbd_submit_peer_request(mdev, peer_req, READ, fault_type) == 0)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002543 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002544
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002545 /* don't care for the reason here */
2546 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002547 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002548 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002549 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002550 /* no drbd_rs_complete_io(), we are dropping the connection anyways */
2551
Philipp Reisnerb411b362009-09-25 16:07:19 -07002552out_free_e:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002553 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002554 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002555 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002556}
2557
2558static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
2559{
2560 int self, peer, rv = -100;
2561 unsigned long ch_self, ch_peer;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002562 enum drbd_after_sb_p after_sb_0p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002563
2564 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2565 peer = mdev->p_uuid[UI_BITMAP] & 1;
2566
2567 ch_peer = mdev->p_uuid[UI_SIZE];
2568 ch_self = mdev->comm_bm_set;
2569
Philipp Reisner44ed1672011-04-19 17:10:19 +02002570 rcu_read_lock();
2571 after_sb_0p = rcu_dereference(mdev->tconn->net_conf)->after_sb_0p;
2572 rcu_read_unlock();
2573 switch (after_sb_0p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002574 case ASB_CONSENSUS:
2575 case ASB_DISCARD_SECONDARY:
2576 case ASB_CALL_HELPER:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002577 case ASB_VIOLENTLY:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002578 dev_err(DEV, "Configuration error.\n");
2579 break;
2580 case ASB_DISCONNECT:
2581 break;
2582 case ASB_DISCARD_YOUNGER_PRI:
2583 if (self == 0 && peer == 1) {
2584 rv = -1;
2585 break;
2586 }
2587 if (self == 1 && peer == 0) {
2588 rv = 1;
2589 break;
2590 }
2591 /* Else fall through to one of the other strategies... */
2592 case ASB_DISCARD_OLDER_PRI:
2593 if (self == 0 && peer == 1) {
2594 rv = 1;
2595 break;
2596 }
2597 if (self == 1 && peer == 0) {
2598 rv = -1;
2599 break;
2600 }
2601 /* Else fall through to one of the other strategies... */
Lars Ellenbergad19bf62009-10-14 09:36:49 +02002602 dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
Philipp Reisnerb411b362009-09-25 16:07:19 -07002603 "Using discard-least-changes instead\n");
2604 case ASB_DISCARD_ZERO_CHG:
2605 if (ch_peer == 0 && ch_self == 0) {
Philipp Reisner25703f82011-02-07 14:35:25 +01002606 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002607 ? -1 : 1;
2608 break;
2609 } else {
2610 if (ch_peer == 0) { rv = 1; break; }
2611 if (ch_self == 0) { rv = -1; break; }
2612 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002613 if (after_sb_0p == ASB_DISCARD_ZERO_CHG)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002614 break;
2615 case ASB_DISCARD_LEAST_CHG:
2616 if (ch_self < ch_peer)
2617 rv = -1;
2618 else if (ch_self > ch_peer)
2619 rv = 1;
2620 else /* ( ch_self == ch_peer ) */
2621 /* Well, then use something else. */
Philipp Reisner25703f82011-02-07 14:35:25 +01002622 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002623 ? -1 : 1;
2624 break;
2625 case ASB_DISCARD_LOCAL:
2626 rv = -1;
2627 break;
2628 case ASB_DISCARD_REMOTE:
2629 rv = 1;
2630 }
2631
2632 return rv;
2633}
2634
2635static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
2636{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002637 int hg, rv = -100;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002638 enum drbd_after_sb_p after_sb_1p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002639
Philipp Reisner44ed1672011-04-19 17:10:19 +02002640 rcu_read_lock();
2641 after_sb_1p = rcu_dereference(mdev->tconn->net_conf)->after_sb_1p;
2642 rcu_read_unlock();
2643 switch (after_sb_1p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002644 case ASB_DISCARD_YOUNGER_PRI:
2645 case ASB_DISCARD_OLDER_PRI:
2646 case ASB_DISCARD_LEAST_CHG:
2647 case ASB_DISCARD_LOCAL:
2648 case ASB_DISCARD_REMOTE:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002649 case ASB_DISCARD_ZERO_CHG:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002650 dev_err(DEV, "Configuration error.\n");
2651 break;
2652 case ASB_DISCONNECT:
2653 break;
2654 case ASB_CONSENSUS:
2655 hg = drbd_asb_recover_0p(mdev);
2656 if (hg == -1 && mdev->state.role == R_SECONDARY)
2657 rv = hg;
2658 if (hg == 1 && mdev->state.role == R_PRIMARY)
2659 rv = hg;
2660 break;
2661 case ASB_VIOLENTLY:
2662 rv = drbd_asb_recover_0p(mdev);
2663 break;
2664 case ASB_DISCARD_SECONDARY:
2665 return mdev->state.role == R_PRIMARY ? 1 : -1;
2666 case ASB_CALL_HELPER:
2667 hg = drbd_asb_recover_0p(mdev);
2668 if (hg == -1 && mdev->state.role == R_PRIMARY) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002669 enum drbd_state_rv rv2;
2670
2671 drbd_set_role(mdev, R_SECONDARY, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002672 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2673 * we might be here in C_WF_REPORT_PARAMS which is transient.
2674 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002675 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2676 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002677 drbd_khelper(mdev, "pri-lost-after-sb");
2678 } else {
2679 dev_warn(DEV, "Successfully gave up primary role.\n");
2680 rv = hg;
2681 }
2682 } else
2683 rv = hg;
2684 }
2685
2686 return rv;
2687}
2688
2689static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
2690{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002691 int hg, rv = -100;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002692 enum drbd_after_sb_p after_sb_2p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002693
Philipp Reisner44ed1672011-04-19 17:10:19 +02002694 rcu_read_lock();
2695 after_sb_2p = rcu_dereference(mdev->tconn->net_conf)->after_sb_2p;
2696 rcu_read_unlock();
2697 switch (after_sb_2p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002698 case ASB_DISCARD_YOUNGER_PRI:
2699 case ASB_DISCARD_OLDER_PRI:
2700 case ASB_DISCARD_LEAST_CHG:
2701 case ASB_DISCARD_LOCAL:
2702 case ASB_DISCARD_REMOTE:
2703 case ASB_CONSENSUS:
2704 case ASB_DISCARD_SECONDARY:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002705 case ASB_DISCARD_ZERO_CHG:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002706 dev_err(DEV, "Configuration error.\n");
2707 break;
2708 case ASB_VIOLENTLY:
2709 rv = drbd_asb_recover_0p(mdev);
2710 break;
2711 case ASB_DISCONNECT:
2712 break;
2713 case ASB_CALL_HELPER:
2714 hg = drbd_asb_recover_0p(mdev);
2715 if (hg == -1) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002716 enum drbd_state_rv rv2;
2717
Philipp Reisnerb411b362009-09-25 16:07:19 -07002718 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2719 * we might be here in C_WF_REPORT_PARAMS which is transient.
2720 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002721 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2722 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002723 drbd_khelper(mdev, "pri-lost-after-sb");
2724 } else {
2725 dev_warn(DEV, "Successfully gave up primary role.\n");
2726 rv = hg;
2727 }
2728 } else
2729 rv = hg;
2730 }
2731
2732 return rv;
2733}
2734
2735static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
2736 u64 bits, u64 flags)
2737{
2738 if (!uuid) {
2739 dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
2740 return;
2741 }
2742 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
2743 text,
2744 (unsigned long long)uuid[UI_CURRENT],
2745 (unsigned long long)uuid[UI_BITMAP],
2746 (unsigned long long)uuid[UI_HISTORY_START],
2747 (unsigned long long)uuid[UI_HISTORY_END],
2748 (unsigned long long)bits,
2749 (unsigned long long)flags);
2750}
2751
2752/*
2753 100 after split brain try auto recover
2754 2 C_SYNC_SOURCE set BitMap
2755 1 C_SYNC_SOURCE use BitMap
2756 0 no Sync
2757 -1 C_SYNC_TARGET use BitMap
2758 -2 C_SYNC_TARGET set BitMap
2759 -100 after split brain, disconnect
2760-1000 unrelated data
Philipp Reisner4a23f262011-01-11 17:42:17 +01002761-1091 requires proto 91
2762-1096 requires proto 96
Philipp Reisnerb411b362009-09-25 16:07:19 -07002763 */
2764static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
2765{
2766 u64 self, peer;
2767 int i, j;
2768
2769 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2770 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2771
2772 *rule_nr = 10;
2773 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
2774 return 0;
2775
2776 *rule_nr = 20;
2777 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
2778 peer != UUID_JUST_CREATED)
2779 return -2;
2780
2781 *rule_nr = 30;
2782 if (self != UUID_JUST_CREATED &&
2783 (peer == UUID_JUST_CREATED || peer == (u64)0))
2784 return 2;
2785
2786 if (self == peer) {
2787 int rct, dc; /* roles at crash time */
2788
2789 if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
2790
Philipp Reisner31890f42011-01-19 14:12:51 +01002791 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002792 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002793
2794 if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
2795 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
2796 dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
2797 drbd_uuid_set_bm(mdev, 0UL);
2798
2799 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2800 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2801 *rule_nr = 34;
2802 } else {
2803 dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
2804 *rule_nr = 36;
2805 }
2806
2807 return 1;
2808 }
2809
2810 if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
2811
Philipp Reisner31890f42011-01-19 14:12:51 +01002812 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002813 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002814
2815 if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
2816 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
2817 dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
2818
2819 mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
2820 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
2821 mdev->p_uuid[UI_BITMAP] = 0UL;
2822
2823 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2824 *rule_nr = 35;
2825 } else {
2826 dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
2827 *rule_nr = 37;
2828 }
2829
2830 return -1;
2831 }
2832
2833 /* Common power [off|failure] */
2834 rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
2835 (mdev->p_uuid[UI_FLAGS] & 2);
2836 /* lowest bit is set when we were primary,
2837 * next bit (weight 2) is set when peer was primary */
2838 *rule_nr = 40;
2839
2840 switch (rct) {
2841 case 0: /* !self_pri && !peer_pri */ return 0;
2842 case 1: /* self_pri && !peer_pri */ return 1;
2843 case 2: /* !self_pri && peer_pri */ return -1;
2844 case 3: /* self_pri && peer_pri */
Philipp Reisner25703f82011-02-07 14:35:25 +01002845 dc = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002846 return dc ? -1 : 1;
2847 }
2848 }
2849
2850 *rule_nr = 50;
2851 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2852 if (self == peer)
2853 return -1;
2854
2855 *rule_nr = 51;
2856 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2857 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002858 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002859 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) ==
2860 (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) :
2861 peer + UUID_NEW_BM_OFFSET == (mdev->p_uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002862 /* The last P_SYNC_UUID did not get though. Undo the last start of
2863 resync as sync source modifications of the peer's UUIDs. */
2864
Philipp Reisner31890f42011-01-19 14:12:51 +01002865 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002866 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002867
2868 mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
2869 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
Philipp Reisner4a23f262011-01-11 17:42:17 +01002870
Lars Ellenberg1882e222012-05-07 13:09:00 +02002871 dev_info(DEV, "Lost last syncUUID packet, corrected:\n");
Philipp Reisner4a23f262011-01-11 17:42:17 +01002872 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2873
Philipp Reisnerb411b362009-09-25 16:07:19 -07002874 return -1;
2875 }
2876 }
2877
2878 *rule_nr = 60;
2879 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2880 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2881 peer = mdev->p_uuid[i] & ~((u64)1);
2882 if (self == peer)
2883 return -2;
2884 }
2885
2886 *rule_nr = 70;
2887 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2888 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2889 if (self == peer)
2890 return 1;
2891
2892 *rule_nr = 71;
2893 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2894 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002895 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002896 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) ==
2897 (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) :
2898 self + UUID_NEW_BM_OFFSET == (mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002899 /* The last P_SYNC_UUID did not get though. Undo the last start of
2900 resync as sync source modifications of our UUIDs. */
2901
Philipp Reisner31890f42011-01-19 14:12:51 +01002902 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002903 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002904
2905 _drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
2906 _drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
2907
Philipp Reisner4a23f262011-01-11 17:42:17 +01002908 dev_info(DEV, "Last syncUUID did not get through, corrected:\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002909 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2910 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2911
2912 return 1;
2913 }
2914 }
2915
2916
2917 *rule_nr = 80;
Philipp Reisnerd8c2a362009-11-18 15:52:51 +01002918 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002919 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2920 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2921 if (self == peer)
2922 return 2;
2923 }
2924
2925 *rule_nr = 90;
2926 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2927 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2928 if (self == peer && self != ((u64)0))
2929 return 100;
2930
2931 *rule_nr = 100;
2932 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2933 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2934 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
2935 peer = mdev->p_uuid[j] & ~((u64)1);
2936 if (self == peer)
2937 return -100;
2938 }
2939 }
2940
2941 return -1000;
2942}
2943
2944/* drbd_sync_handshake() returns the new conn state on success, or
2945 CONN_MASK (-1) on failure.
2946 */
2947static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
2948 enum drbd_disk_state peer_disk) __must_hold(local)
2949{
Philipp Reisnerb411b362009-09-25 16:07:19 -07002950 enum drbd_conns rv = C_MASK;
2951 enum drbd_disk_state mydisk;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002952 struct net_conf *nc;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02002953 int hg, rule_nr, rr_conflict, tentative;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002954
2955 mydisk = mdev->state.disk;
2956 if (mydisk == D_NEGOTIATING)
2957 mydisk = mdev->new_state_tmp.disk;
2958
2959 dev_info(DEV, "drbd_sync_handshake:\n");
2960 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0);
2961 drbd_uuid_dump(mdev, "peer", mdev->p_uuid,
2962 mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2963
2964 hg = drbd_uuid_compare(mdev, &rule_nr);
2965
2966 dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
2967
2968 if (hg == -1000) {
2969 dev_alert(DEV, "Unrelated data, aborting!\n");
2970 return C_MASK;
2971 }
Philipp Reisner4a23f262011-01-11 17:42:17 +01002972 if (hg < -1000) {
2973 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 -07002974 return C_MASK;
2975 }
2976
2977 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
2978 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
2979 int f = (hg == -100) || abs(hg) == 2;
2980 hg = mydisk > D_INCONSISTENT ? 1 : -1;
2981 if (f)
2982 hg = hg*2;
2983 dev_info(DEV, "Becoming sync %s due to disk states.\n",
2984 hg > 0 ? "source" : "target");
2985 }
2986
Adam Gandelman3a11a482010-04-08 16:48:23 -07002987 if (abs(hg) == 100)
2988 drbd_khelper(mdev, "initial-split-brain");
2989
Philipp Reisner44ed1672011-04-19 17:10:19 +02002990 rcu_read_lock();
2991 nc = rcu_dereference(mdev->tconn->net_conf);
2992
2993 if (hg == 100 || (hg == -100 && nc->always_asbp)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002994 int pcount = (mdev->state.role == R_PRIMARY)
2995 + (peer_role == R_PRIMARY);
2996 int forced = (hg == -100);
2997
2998 switch (pcount) {
2999 case 0:
3000 hg = drbd_asb_recover_0p(mdev);
3001 break;
3002 case 1:
3003 hg = drbd_asb_recover_1p(mdev);
3004 break;
3005 case 2:
3006 hg = drbd_asb_recover_2p(mdev);
3007 break;
3008 }
3009 if (abs(hg) < 100) {
3010 dev_warn(DEV, "Split-Brain detected, %d primaries, "
3011 "automatically solved. Sync from %s node\n",
3012 pcount, (hg < 0) ? "peer" : "this");
3013 if (forced) {
3014 dev_warn(DEV, "Doing a full sync, since"
3015 " UUIDs where ambiguous.\n");
3016 hg = hg*2;
3017 }
3018 }
3019 }
3020
3021 if (hg == -100) {
Philipp Reisner08b165b2011-09-05 16:22:33 +02003022 if (test_bit(DISCARD_MY_DATA, &mdev->flags) && !(mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003023 hg = -1;
Philipp Reisner08b165b2011-09-05 16:22:33 +02003024 if (!test_bit(DISCARD_MY_DATA, &mdev->flags) && (mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003025 hg = 1;
3026
3027 if (abs(hg) < 100)
3028 dev_warn(DEV, "Split-Brain detected, manually solved. "
3029 "Sync from %s node\n",
3030 (hg < 0) ? "peer" : "this");
3031 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02003032 rr_conflict = nc->rr_conflict;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02003033 tentative = nc->tentative;
Philipp Reisner44ed1672011-04-19 17:10:19 +02003034 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07003035
3036 if (hg == -100) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01003037 /* FIXME this log message is not correct if we end up here
3038 * after an attempted attach on a diskless node.
3039 * We just refuse to attach -- well, we drop the "connection"
3040 * to that disk, in a way... */
Adam Gandelman3a11a482010-04-08 16:48:23 -07003041 dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003042 drbd_khelper(mdev, "split-brain");
3043 return C_MASK;
3044 }
3045
3046 if (hg > 0 && mydisk <= D_INCONSISTENT) {
3047 dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
3048 return C_MASK;
3049 }
3050
3051 if (hg < 0 && /* by intention we do not use mydisk here. */
3052 mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02003053 switch (rr_conflict) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003054 case ASB_CALL_HELPER:
3055 drbd_khelper(mdev, "pri-lost");
3056 /* fall through */
3057 case ASB_DISCONNECT:
3058 dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
3059 return C_MASK;
3060 case ASB_VIOLENTLY:
3061 dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
3062 "assumption\n");
3063 }
3064 }
3065
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02003066 if (tentative || test_bit(CONN_DRY_RUN, &mdev->tconn->flags)) {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003067 if (hg == 0)
3068 dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
3069 else
3070 dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
3071 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
3072 abs(hg) >= 2 ? "full" : "bit-map based");
3073 return C_MASK;
3074 }
3075
Philipp Reisnerb411b362009-09-25 16:07:19 -07003076 if (abs(hg) >= 2) {
3077 dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003078 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake",
3079 BM_LOCKED_SET_ALLOWED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003080 return C_MASK;
3081 }
3082
3083 if (hg > 0) { /* become sync source. */
3084 rv = C_WF_BITMAP_S;
3085 } else if (hg < 0) { /* become sync target */
3086 rv = C_WF_BITMAP_T;
3087 } else {
3088 rv = C_CONNECTED;
3089 if (drbd_bm_total_weight(mdev)) {
3090 dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
3091 drbd_bm_total_weight(mdev));
3092 }
3093 }
3094
3095 return rv;
3096}
3097
Philipp Reisnerf179d762011-05-16 17:31:47 +02003098static enum drbd_after_sb_p convert_after_sb(enum drbd_after_sb_p peer)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003099{
3100 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003101 if (peer == ASB_DISCARD_REMOTE)
3102 return ASB_DISCARD_LOCAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003103
3104 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003105 if (peer == ASB_DISCARD_LOCAL)
3106 return ASB_DISCARD_REMOTE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003107
3108 /* everything else is valid if they are equal on both sides. */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003109 return peer;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003110}
3111
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003112static int receive_protocol(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003113{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003114 struct p_protocol *p = pi->data;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003115 enum drbd_after_sb_p p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
3116 int p_proto, p_discard_my_data, p_two_primaries, cf;
3117 struct net_conf *nc, *old_net_conf, *new_net_conf = NULL;
3118 char integrity_alg[SHARED_SECRET_MAX] = "";
Andreas Gruenbacheraccdbcc2011-07-15 17:41:09 +02003119 struct crypto_hash *peer_integrity_tfm = NULL;
Philipp Reisner7aca6c72011-05-17 10:12:56 +02003120 void *int_dig_in = NULL, *int_dig_vv = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003121
Philipp Reisnerb411b362009-09-25 16:07:19 -07003122 p_proto = be32_to_cpu(p->protocol);
3123 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
3124 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
3125 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003126 p_two_primaries = be32_to_cpu(p->two_primaries);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003127 cf = be32_to_cpu(p->conn_flags);
Andreas Gruenbacher6139f602011-05-06 20:00:02 +02003128 p_discard_my_data = cf & CF_DISCARD_MY_DATA;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003129
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003130 if (tconn->agreed_pro_version >= 87) {
3131 int err;
3132
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003133 if (pi->size > sizeof(integrity_alg))
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003134 return -EIO;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003135 err = drbd_recv_all(tconn, integrity_alg, pi->size);
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003136 if (err)
3137 return err;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003138 integrity_alg[SHARED_SECRET_MAX - 1] = 0;
3139 }
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003140
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003141 if (pi->cmd != P_PROTOCOL_UPDATE) {
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003142 clear_bit(CONN_DRY_RUN, &tconn->flags);
Philipp Reisner036b17e2011-05-16 17:38:11 +02003143
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003144 if (cf & CF_DRY_RUN)
3145 set_bit(CONN_DRY_RUN, &tconn->flags);
3146
3147 rcu_read_lock();
3148 nc = rcu_dereference(tconn->net_conf);
3149
3150 if (p_proto != nc->wire_protocol) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003151 conn_err(tconn, "incompatible %s settings\n", "protocol");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003152 goto disconnect_rcu_unlock;
3153 }
3154
3155 if (convert_after_sb(p_after_sb_0p) != nc->after_sb_0p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003156 conn_err(tconn, "incompatible %s settings\n", "after-sb-0pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003157 goto disconnect_rcu_unlock;
3158 }
3159
3160 if (convert_after_sb(p_after_sb_1p) != nc->after_sb_1p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003161 conn_err(tconn, "incompatible %s settings\n", "after-sb-1pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003162 goto disconnect_rcu_unlock;
3163 }
3164
3165 if (convert_after_sb(p_after_sb_2p) != nc->after_sb_2p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003166 conn_err(tconn, "incompatible %s settings\n", "after-sb-2pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003167 goto disconnect_rcu_unlock;
3168 }
3169
3170 if (p_discard_my_data && nc->discard_my_data) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003171 conn_err(tconn, "incompatible %s settings\n", "discard-my-data");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003172 goto disconnect_rcu_unlock;
3173 }
3174
3175 if (p_two_primaries != nc->two_primaries) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003176 conn_err(tconn, "incompatible %s settings\n", "allow-two-primaries");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003177 goto disconnect_rcu_unlock;
3178 }
3179
3180 if (strcmp(integrity_alg, nc->integrity_alg)) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003181 conn_err(tconn, "incompatible %s settings\n", "data-integrity-alg");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003182 goto disconnect_rcu_unlock;
3183 }
3184
3185 rcu_read_unlock();
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003186 }
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003187
3188 if (integrity_alg[0]) {
3189 int hash_size;
3190
3191 /*
3192 * We can only change the peer data integrity algorithm
3193 * here. Changing our own data integrity algorithm
3194 * requires that we send a P_PROTOCOL_UPDATE packet at
3195 * the same time; otherwise, the peer has no way to
3196 * tell between which packets the algorithm should
3197 * change.
3198 */
3199
3200 peer_integrity_tfm = crypto_alloc_hash(integrity_alg, 0, CRYPTO_ALG_ASYNC);
3201 if (!peer_integrity_tfm) {
3202 conn_err(tconn, "peer data-integrity-alg %s not supported\n",
3203 integrity_alg);
3204 goto disconnect;
3205 }
3206
3207 hash_size = crypto_hash_digestsize(peer_integrity_tfm);
3208 int_dig_in = kmalloc(hash_size, GFP_KERNEL);
3209 int_dig_vv = kmalloc(hash_size, GFP_KERNEL);
3210 if (!(int_dig_in && int_dig_vv)) {
3211 conn_err(tconn, "Allocation of buffers for data integrity checking failed\n");
3212 goto disconnect;
3213 }
3214 }
3215
3216 new_net_conf = kmalloc(sizeof(struct net_conf), GFP_KERNEL);
3217 if (!new_net_conf) {
3218 conn_err(tconn, "Allocation of new net_conf failed\n");
3219 goto disconnect;
3220 }
3221
3222 mutex_lock(&tconn->data.mutex);
3223 mutex_lock(&tconn->conf_update);
3224 old_net_conf = tconn->net_conf;
3225 *new_net_conf = *old_net_conf;
3226
3227 new_net_conf->wire_protocol = p_proto;
3228 new_net_conf->after_sb_0p = convert_after_sb(p_after_sb_0p);
3229 new_net_conf->after_sb_1p = convert_after_sb(p_after_sb_1p);
3230 new_net_conf->after_sb_2p = convert_after_sb(p_after_sb_2p);
3231 new_net_conf->two_primaries = p_two_primaries;
3232
3233 rcu_assign_pointer(tconn->net_conf, new_net_conf);
3234 mutex_unlock(&tconn->conf_update);
3235 mutex_unlock(&tconn->data.mutex);
3236
3237 crypto_free_hash(tconn->peer_integrity_tfm);
3238 kfree(tconn->int_dig_in);
3239 kfree(tconn->int_dig_vv);
3240 tconn->peer_integrity_tfm = peer_integrity_tfm;
3241 tconn->int_dig_in = int_dig_in;
3242 tconn->int_dig_vv = int_dig_vv;
3243
3244 if (strcmp(old_net_conf->integrity_alg, integrity_alg))
3245 conn_info(tconn, "peer data-integrity-alg: %s\n",
3246 integrity_alg[0] ? integrity_alg : "(none)");
3247
3248 synchronize_rcu();
3249 kfree(old_net_conf);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003250 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003251
Philipp Reisner44ed1672011-04-19 17:10:19 +02003252disconnect_rcu_unlock:
3253 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07003254disconnect:
Andreas Gruenbacherb792c352011-07-15 16:48:49 +02003255 crypto_free_hash(peer_integrity_tfm);
Philipp Reisner036b17e2011-05-16 17:38:11 +02003256 kfree(int_dig_in);
3257 kfree(int_dig_vv);
Philipp Reisner72046242011-03-15 18:51:47 +01003258 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003259 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003260}
3261
3262/* helper function
3263 * input: alg name, feature name
3264 * return: NULL (alg name was "")
3265 * ERR_PTR(error) if something goes wrong
3266 * or the crypto hash ptr, if it worked out ok. */
3267struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
3268 const char *alg, const char *name)
3269{
3270 struct crypto_hash *tfm;
3271
3272 if (!alg[0])
3273 return NULL;
3274
3275 tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
3276 if (IS_ERR(tfm)) {
3277 dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
3278 alg, name, PTR_ERR(tfm));
3279 return tfm;
3280 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003281 return tfm;
3282}
3283
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003284static int ignore_remaining_packet(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003285{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003286 void *buffer = tconn->data.rbuf;
3287 int size = pi->size;
3288
3289 while (size) {
3290 int s = min_t(int, size, DRBD_SOCKET_BUFFER_SIZE);
3291 s = drbd_recv(tconn, buffer, s);
3292 if (s <= 0) {
3293 if (s < 0)
3294 return s;
3295 break;
3296 }
3297 size -= s;
3298 }
3299 if (size)
3300 return -EIO;
3301 return 0;
3302}
3303
3304/*
3305 * config_unknown_volume - device configuration command for unknown volume
3306 *
3307 * When a device is added to an existing connection, the node on which the
3308 * device is added first will send configuration commands to its peer but the
3309 * peer will not know about the device yet. It will warn and ignore these
3310 * commands. Once the device is added on the second node, the second node will
3311 * send the same device configuration commands, but in the other direction.
3312 *
3313 * (We can also end up here if drbd is misconfigured.)
3314 */
3315static int config_unknown_volume(struct drbd_tconn *tconn, struct packet_info *pi)
3316{
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02003317 conn_warn(tconn, "%s packet received for volume %u, which is not configured locally\n",
3318 cmdname(pi->cmd), pi->vnr);
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003319 return ignore_remaining_packet(tconn, pi);
3320}
3321
3322static int receive_SyncParam(struct drbd_tconn *tconn, struct packet_info *pi)
3323{
3324 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003325 struct p_rs_param_95 *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003326 unsigned int header_size, data_size, exp_max_sz;
3327 struct crypto_hash *verify_tfm = NULL;
3328 struct crypto_hash *csums_tfm = NULL;
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003329 struct net_conf *old_net_conf, *new_net_conf = NULL;
Philipp Reisner813472c2011-05-03 16:47:02 +02003330 struct disk_conf *old_disk_conf = NULL, *new_disk_conf = NULL;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003331 const int apv = tconn->agreed_pro_version;
Philipp Reisner813472c2011-05-03 16:47:02 +02003332 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
Philipp Reisner778f2712010-07-06 11:14:00 +02003333 int fifo_size = 0;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003334 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003335
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003336 mdev = vnr_to_mdev(tconn, pi->vnr);
3337 if (!mdev)
3338 return config_unknown_volume(tconn, pi);
3339
Philipp Reisnerb411b362009-09-25 16:07:19 -07003340 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
3341 : apv == 88 ? sizeof(struct p_rs_param)
3342 + SHARED_SECRET_MAX
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003343 : apv <= 94 ? sizeof(struct p_rs_param_89)
3344 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003345
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003346 if (pi->size > exp_max_sz) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003347 dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003348 pi->size, exp_max_sz);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003349 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003350 }
3351
3352 if (apv <= 88) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003353 header_size = sizeof(struct p_rs_param);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003354 data_size = pi->size - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003355 } else if (apv <= 94) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003356 header_size = sizeof(struct p_rs_param_89);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003357 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003358 D_ASSERT(data_size == 0);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003359 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003360 header_size = sizeof(struct p_rs_param_95);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003361 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003362 D_ASSERT(data_size == 0);
3363 }
3364
3365 /* initialize verify_alg and csums_alg */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003366 p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003367 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
3368
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003369 err = drbd_recv_all(mdev->tconn, p, header_size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003370 if (err)
3371 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003372
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003373 mutex_lock(&mdev->tconn->conf_update);
3374 old_net_conf = mdev->tconn->net_conf;
Philipp Reisner813472c2011-05-03 16:47:02 +02003375 if (get_ldev(mdev)) {
3376 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3377 if (!new_disk_conf) {
3378 put_ldev(mdev);
3379 mutex_unlock(&mdev->tconn->conf_update);
3380 dev_err(DEV, "Allocation of new disk_conf failed\n");
3381 return -ENOMEM;
3382 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003383
Philipp Reisner813472c2011-05-03 16:47:02 +02003384 old_disk_conf = mdev->ldev->disk_conf;
3385 *new_disk_conf = *old_disk_conf;
3386
Andreas Gruenbacher6394b932011-05-11 14:29:52 +02003387 new_disk_conf->resync_rate = be32_to_cpu(p->resync_rate);
Philipp Reisner813472c2011-05-03 16:47:02 +02003388 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003389
Philipp Reisnerb411b362009-09-25 16:07:19 -07003390 if (apv >= 88) {
3391 if (apv == 88) {
Philipp Reisnere4bad1b2012-04-06 12:08:51 +02003392 if (data_size > SHARED_SECRET_MAX || data_size == 0) {
3393 dev_err(DEV, "verify-alg of wrong size, "
3394 "peer wants %u, accepting only up to %u byte\n",
3395 data_size, SHARED_SECRET_MAX);
Philipp Reisner813472c2011-05-03 16:47:02 +02003396 err = -EIO;
3397 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003398 }
3399
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003400 err = drbd_recv_all(mdev->tconn, p->verify_alg, data_size);
Philipp Reisner813472c2011-05-03 16:47:02 +02003401 if (err)
3402 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003403 /* we expect NUL terminated string */
3404 /* but just in case someone tries to be evil */
3405 D_ASSERT(p->verify_alg[data_size-1] == 0);
3406 p->verify_alg[data_size-1] = 0;
3407
3408 } else /* apv >= 89 */ {
3409 /* we still expect NUL terminated strings */
3410 /* but just in case someone tries to be evil */
3411 D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
3412 D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
3413 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
3414 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
3415 }
3416
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003417 if (strcmp(old_net_conf->verify_alg, p->verify_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003418 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3419 dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003420 old_net_conf->verify_alg, p->verify_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003421 goto disconnect;
3422 }
3423 verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
3424 p->verify_alg, "verify-alg");
3425 if (IS_ERR(verify_tfm)) {
3426 verify_tfm = NULL;
3427 goto disconnect;
3428 }
3429 }
3430
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003431 if (apv >= 89 && strcmp(old_net_conf->csums_alg, p->csums_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003432 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3433 dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003434 old_net_conf->csums_alg, p->csums_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003435 goto disconnect;
3436 }
3437 csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
3438 p->csums_alg, "csums-alg");
3439 if (IS_ERR(csums_tfm)) {
3440 csums_tfm = NULL;
3441 goto disconnect;
3442 }
3443 }
3444
Philipp Reisner813472c2011-05-03 16:47:02 +02003445 if (apv > 94 && new_disk_conf) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003446 new_disk_conf->c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
3447 new_disk_conf->c_delay_target = be32_to_cpu(p->c_delay_target);
3448 new_disk_conf->c_fill_target = be32_to_cpu(p->c_fill_target);
3449 new_disk_conf->c_max_rate = be32_to_cpu(p->c_max_rate);
Philipp Reisner778f2712010-07-06 11:14:00 +02003450
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003451 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Philipp Reisner9958c852011-05-03 16:19:31 +02003452 if (fifo_size != mdev->rs_plan_s->size) {
Philipp Reisner813472c2011-05-03 16:47:02 +02003453 new_plan = fifo_alloc(fifo_size);
3454 if (!new_plan) {
Philipp Reisner778f2712010-07-06 11:14:00 +02003455 dev_err(DEV, "kmalloc of fifo_buffer failed");
Lars Ellenbergf3990022011-03-23 14:31:09 +01003456 put_ldev(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02003457 goto disconnect;
3458 }
3459 }
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003460 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003461
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003462 if (verify_tfm || csums_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003463 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
3464 if (!new_net_conf) {
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003465 dev_err(DEV, "Allocation of new net_conf failed\n");
3466 goto disconnect;
3467 }
3468
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003469 *new_net_conf = *old_net_conf;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003470
3471 if (verify_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003472 strcpy(new_net_conf->verify_alg, p->verify_alg);
3473 new_net_conf->verify_alg_len = strlen(p->verify_alg) + 1;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003474 crypto_free_hash(mdev->tconn->verify_tfm);
3475 mdev->tconn->verify_tfm = verify_tfm;
3476 dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
3477 }
3478 if (csums_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003479 strcpy(new_net_conf->csums_alg, p->csums_alg);
3480 new_net_conf->csums_alg_len = strlen(p->csums_alg) + 1;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003481 crypto_free_hash(mdev->tconn->csums_tfm);
3482 mdev->tconn->csums_tfm = csums_tfm;
3483 dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
3484 }
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003485 rcu_assign_pointer(tconn->net_conf, new_net_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003486 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003487 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003488
Philipp Reisner813472c2011-05-03 16:47:02 +02003489 if (new_disk_conf) {
3490 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3491 put_ldev(mdev);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003492 }
Philipp Reisner813472c2011-05-03 16:47:02 +02003493
3494 if (new_plan) {
3495 old_plan = mdev->rs_plan_s;
3496 rcu_assign_pointer(mdev->rs_plan_s, new_plan);
3497 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003498
3499 mutex_unlock(&mdev->tconn->conf_update);
3500 synchronize_rcu();
3501 if (new_net_conf)
3502 kfree(old_net_conf);
3503 kfree(old_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02003504 kfree(old_plan);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003505
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003506 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003507
Philipp Reisner813472c2011-05-03 16:47:02 +02003508reconnect:
3509 if (new_disk_conf) {
3510 put_ldev(mdev);
3511 kfree(new_disk_conf);
3512 }
3513 mutex_unlock(&mdev->tconn->conf_update);
3514 return -EIO;
3515
Philipp Reisnerb411b362009-09-25 16:07:19 -07003516disconnect:
Philipp Reisner813472c2011-05-03 16:47:02 +02003517 kfree(new_plan);
3518 if (new_disk_conf) {
3519 put_ldev(mdev);
3520 kfree(new_disk_conf);
3521 }
Philipp Reisnera0095502011-05-03 13:14:15 +02003522 mutex_unlock(&mdev->tconn->conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003523 /* just for completeness: actually not needed,
3524 * as this is not reached if csums_tfm was ok. */
3525 crypto_free_hash(csums_tfm);
3526 /* but free the verify_tfm again, if csums_tfm did not work out */
3527 crypto_free_hash(verify_tfm);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003528 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003529 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003530}
3531
Philipp Reisnerb411b362009-09-25 16:07:19 -07003532/* warn if the arguments differ by more than 12.5% */
3533static void warn_if_differ_considerably(struct drbd_conf *mdev,
3534 const char *s, sector_t a, sector_t b)
3535{
3536 sector_t d;
3537 if (a == 0 || b == 0)
3538 return;
3539 d = (a > b) ? (a - b) : (b - a);
3540 if (d > (a>>3) || d > (b>>3))
3541 dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
3542 (unsigned long long)a, (unsigned long long)b);
3543}
3544
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003545static int receive_sizes(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003546{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003547 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003548 struct p_sizes *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003549 enum determine_dev_size dd = unchanged;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003550 sector_t p_size, p_usize, my_usize;
3551 int ldsc = 0; /* local disk size changed */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003552 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003553
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003554 mdev = vnr_to_mdev(tconn, pi->vnr);
3555 if (!mdev)
3556 return config_unknown_volume(tconn, pi);
3557
Philipp Reisnerb411b362009-09-25 16:07:19 -07003558 p_size = be64_to_cpu(p->d_size);
3559 p_usize = be64_to_cpu(p->u_size);
3560
Philipp Reisnerb411b362009-09-25 16:07:19 -07003561 /* just store the peer's disk size for now.
3562 * we still need to figure out whether we accept that. */
3563 mdev->p_size = p_size;
3564
Philipp Reisnerb411b362009-09-25 16:07:19 -07003565 if (get_ldev(mdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003566 rcu_read_lock();
3567 my_usize = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
3568 rcu_read_unlock();
3569
Philipp Reisnerb411b362009-09-25 16:07:19 -07003570 warn_if_differ_considerably(mdev, "lower level device sizes",
3571 p_size, drbd_get_max_capacity(mdev->ldev));
3572 warn_if_differ_considerably(mdev, "user requested size",
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003573 p_usize, my_usize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003574
3575 /* if this is the first connect, or an otherwise expected
3576 * param exchange, choose the minimum */
3577 if (mdev->state.conn == C_WF_REPORT_PARAMS)
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003578 p_usize = min_not_zero(my_usize, p_usize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003579
3580 /* Never shrink a device with usable data during connect.
3581 But allow online shrinking if we are connected. */
Philipp Reisneref5e44a2011-05-03 13:27:43 +02003582 if (drbd_new_dev_size(mdev, mdev->ldev, p_usize, 0) <
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003583 drbd_get_capacity(mdev->this_bdev) &&
3584 mdev->state.disk >= D_OUTDATED &&
3585 mdev->state.conn < C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003586 dev_err(DEV, "The peer's disk size is too small!\n");
Philipp Reisner38fa9982011-03-15 18:24:49 +01003587 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003588 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003589 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003590 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003591
3592 if (my_usize != p_usize) {
3593 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
3594
3595 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3596 if (!new_disk_conf) {
3597 dev_err(DEV, "Allocation of new disk_conf failed\n");
3598 put_ldev(mdev);
3599 return -ENOMEM;
3600 }
3601
3602 mutex_lock(&mdev->tconn->conf_update);
3603 old_disk_conf = mdev->ldev->disk_conf;
3604 *new_disk_conf = *old_disk_conf;
3605 new_disk_conf->disk_size = p_usize;
3606
3607 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3608 mutex_unlock(&mdev->tconn->conf_update);
3609 synchronize_rcu();
3610 kfree(old_disk_conf);
3611
3612 dev_info(DEV, "Peer sets u_size to %lu sectors\n",
3613 (unsigned long)my_usize);
3614 }
3615
Philipp Reisnerb411b362009-09-25 16:07:19 -07003616 put_ldev(mdev);
3617 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003618
Philipp Reisnere89b5912010-03-24 17:11:33 +01003619 ddsf = be16_to_cpu(p->dds_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003620 if (get_ldev(mdev)) {
Bart Van Assche24c48302011-05-21 18:32:29 +02003621 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003622 put_ldev(mdev);
3623 if (dd == dev_size_error)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003624 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003625 drbd_md_sync(mdev);
3626 } else {
3627 /* I am diskless, need to accept the peer's size. */
3628 drbd_set_my_capacity(mdev, p_size);
3629 }
3630
Philipp Reisner99432fc2011-05-20 16:39:13 +02003631 mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size);
3632 drbd_reconsider_max_bio_size(mdev);
3633
Philipp Reisnerb411b362009-09-25 16:07:19 -07003634 if (get_ldev(mdev)) {
3635 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
3636 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
3637 ldsc = 1;
3638 }
3639
Philipp Reisnerb411b362009-09-25 16:07:19 -07003640 put_ldev(mdev);
3641 }
3642
3643 if (mdev->state.conn > C_WF_REPORT_PARAMS) {
3644 if (be64_to_cpu(p->c_size) !=
3645 drbd_get_capacity(mdev->this_bdev) || ldsc) {
3646 /* we have different sizes, probably peer
3647 * needs to know my new size... */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003648 drbd_send_sizes(mdev, 0, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003649 }
3650 if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
3651 (dd == grew && mdev->state.conn == C_CONNECTED)) {
3652 if (mdev->state.pdsk >= D_INCONSISTENT &&
Philipp Reisnere89b5912010-03-24 17:11:33 +01003653 mdev->state.disk >= D_INCONSISTENT) {
3654 if (ddsf & DDSF_NO_RESYNC)
3655 dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
3656 else
3657 resync_after_online_grow(mdev);
3658 } else
Philipp Reisnerb411b362009-09-25 16:07:19 -07003659 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
3660 }
3661 }
3662
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003663 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003664}
3665
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003666static int receive_uuids(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003667{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003668 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003669 struct p_uuids *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003670 u64 *p_uuid;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003671 int i, updated_uuids = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003672
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003673 mdev = vnr_to_mdev(tconn, pi->vnr);
3674 if (!mdev)
3675 return config_unknown_volume(tconn, pi);
3676
Philipp Reisnerb411b362009-09-25 16:07:19 -07003677 p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
3678
3679 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
3680 p_uuid[i] = be64_to_cpu(p->uuid[i]);
3681
3682 kfree(mdev->p_uuid);
3683 mdev->p_uuid = p_uuid;
3684
3685 if (mdev->state.conn < C_CONNECTED &&
3686 mdev->state.disk < D_INCONSISTENT &&
3687 mdev->state.role == R_PRIMARY &&
3688 (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
3689 dev_err(DEV, "Can only connect to data with current UUID=%016llX\n",
3690 (unsigned long long)mdev->ed_uuid);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003691 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003692 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003693 }
3694
3695 if (get_ldev(mdev)) {
3696 int skip_initial_sync =
3697 mdev->state.conn == C_CONNECTED &&
Philipp Reisner31890f42011-01-19 14:12:51 +01003698 mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003699 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
3700 (p_uuid[UI_FLAGS] & 8);
3701 if (skip_initial_sync) {
3702 dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n");
3703 drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003704 "clear_n_write from receive_uuids",
3705 BM_LOCKED_TEST_ALLOWED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003706 _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]);
3707 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3708 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3709 CS_VERBOSE, NULL);
3710 drbd_md_sync(mdev);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003711 updated_uuids = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003712 }
3713 put_ldev(mdev);
Philipp Reisner18a50fa2010-06-21 14:14:15 +02003714 } else if (mdev->state.disk < D_INCONSISTENT &&
3715 mdev->state.role == R_PRIMARY) {
3716 /* I am a diskless primary, the peer just created a new current UUID
3717 for me. */
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003718 updated_uuids = drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003719 }
3720
3721 /* Before we test for the disk state, we should wait until an eventually
3722 ongoing cluster wide state change is finished. That is important if
3723 we are primary and are detaching from our disk. We need to see the
3724 new disk state... */
Philipp Reisner8410da82011-02-11 20:11:10 +01003725 mutex_lock(mdev->state_mutex);
3726 mutex_unlock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003727 if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT)
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003728 updated_uuids |= drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
3729
3730 if (updated_uuids)
3731 drbd_print_uuids(mdev, "receiver updated UUIDs to");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003732
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003733 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003734}
3735
3736/**
3737 * convert_state() - Converts the peer's view of the cluster state to our point of view
3738 * @ps: The state as seen by the peer.
3739 */
3740static union drbd_state convert_state(union drbd_state ps)
3741{
3742 union drbd_state ms;
3743
3744 static enum drbd_conns c_tab[] = {
Philipp Reisner369bea62011-07-06 23:04:44 +02003745 [C_WF_REPORT_PARAMS] = C_WF_REPORT_PARAMS,
Philipp Reisnerb411b362009-09-25 16:07:19 -07003746 [C_CONNECTED] = C_CONNECTED,
3747
3748 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
3749 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
3750 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
3751 [C_VERIFY_S] = C_VERIFY_T,
3752 [C_MASK] = C_MASK,
3753 };
3754
3755 ms.i = ps.i;
3756
3757 ms.conn = c_tab[ps.conn];
3758 ms.peer = ps.role;
3759 ms.role = ps.peer;
3760 ms.pdsk = ps.disk;
3761 ms.disk = ps.pdsk;
3762 ms.peer_isp = (ps.aftr_isp | ps.user_isp);
3763
3764 return ms;
3765}
3766
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003767static int receive_req_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003768{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003769 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003770 struct p_req_state *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003771 union drbd_state mask, val;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01003772 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003773
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003774 mdev = vnr_to_mdev(tconn, pi->vnr);
3775 if (!mdev)
3776 return -EIO;
3777
Philipp Reisnerb411b362009-09-25 16:07:19 -07003778 mask.i = be32_to_cpu(p->mask);
3779 val.i = be32_to_cpu(p->val);
3780
Philipp Reisner25703f82011-02-07 14:35:25 +01003781 if (test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags) &&
Philipp Reisner8410da82011-02-11 20:11:10 +01003782 mutex_is_locked(mdev->state_mutex)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003783 drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003784 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003785 }
3786
3787 mask = convert_state(mask);
3788 val = convert_state(val);
3789
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003790 rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
3791 drbd_send_sr_reply(mdev, rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003792
Philipp Reisnerb411b362009-09-25 16:07:19 -07003793 drbd_md_sync(mdev);
3794
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003795 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003796}
3797
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003798static int receive_req_conn_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003799{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003800 struct p_req_state *p = pi->data;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003801 union drbd_state mask, val;
3802 enum drbd_state_rv rv;
3803
3804 mask.i = be32_to_cpu(p->mask);
3805 val.i = be32_to_cpu(p->val);
3806
3807 if (test_bit(DISCARD_CONCURRENT, &tconn->flags) &&
3808 mutex_is_locked(&tconn->cstate_mutex)) {
3809 conn_send_sr_reply(tconn, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003810 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003811 }
3812
3813 mask = convert_state(mask);
3814 val = convert_state(val);
3815
Philipp Reisner778bcf22011-03-28 12:55:03 +02003816 rv = conn_request_state(tconn, mask, val, CS_VERBOSE | CS_LOCAL_ONLY | CS_IGN_OUTD_FAIL);
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003817 conn_send_sr_reply(tconn, rv);
3818
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003819 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003820}
3821
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003822static int receive_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003823{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003824 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003825 struct p_state *p = pi->data;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003826 union drbd_state os, ns, peer_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003827 enum drbd_disk_state real_peer_disk;
Philipp Reisner65d922c2010-06-16 16:18:09 +02003828 enum chg_state_flags cs_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003829 int rv;
3830
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003831 mdev = vnr_to_mdev(tconn, pi->vnr);
3832 if (!mdev)
3833 return config_unknown_volume(tconn, pi);
3834
Philipp Reisnerb411b362009-09-25 16:07:19 -07003835 peer_state.i = be32_to_cpu(p->state);
3836
3837 real_peer_disk = peer_state.disk;
3838 if (peer_state.disk == D_NEGOTIATING) {
3839 real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
3840 dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
3841 }
3842
Philipp Reisner87eeee42011-01-19 14:16:30 +01003843 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003844 retry:
Philipp Reisner78bae592011-03-28 15:40:12 +02003845 os = ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003846 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003847
Philipp Reisnerb8853db2011-12-13 11:09:16 +01003848 /* If some other part of the code (asender thread, timeout)
3849 * already decided to close the connection again,
3850 * we must not "re-establish" it here. */
3851 if (os.conn <= C_TEAR_DOWN)
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003852 return -ECONNRESET;
Philipp Reisnerb8853db2011-12-13 11:09:16 +01003853
Philipp Reisner9bcd2522011-09-29 13:00:14 +02003854 /* If this is the "end of sync" confirmation, usually the peer disk
3855 * transitions from D_INCONSISTENT to D_UP_TO_DATE. For empty (0 bits
3856 * set) resync started in PausedSyncT, or if the timing of pause-/
3857 * unpause-sync events has been "just right", the peer disk may
3858 * transition from D_CONSISTENT to D_UP_TO_DATE as well.
3859 */
3860 if ((os.pdsk == D_INCONSISTENT || os.pdsk == D_CONSISTENT) &&
3861 real_peer_disk == D_UP_TO_DATE &&
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003862 os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) {
3863 /* If we are (becoming) SyncSource, but peer is still in sync
3864 * preparation, ignore its uptodate-ness to avoid flapping, it
3865 * will change to inconsistent once the peer reaches active
3866 * syncing states.
3867 * It may have changed syncer-paused flags, however, so we
3868 * cannot ignore this completely. */
3869 if (peer_state.conn > C_CONNECTED &&
3870 peer_state.conn < C_SYNC_SOURCE)
3871 real_peer_disk = D_INCONSISTENT;
3872
3873 /* if peer_state changes to connected at the same time,
3874 * it explicitly notifies us that it finished resync.
3875 * Maybe we should finish it up, too? */
3876 else if (os.conn >= C_SYNC_SOURCE &&
3877 peer_state.conn == C_CONNECTED) {
3878 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed)
3879 drbd_resync_finished(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003880 return 0;
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003881 }
3882 }
3883
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003884 /* explicit verify finished notification, stop sector reached. */
3885 if (os.conn == C_VERIFY_T && os.disk == D_UP_TO_DATE &&
3886 peer_state.conn == C_CONNECTED && real_peer_disk == D_UP_TO_DATE) {
3887 ov_out_of_sync_print(mdev);
3888 drbd_resync_finished(mdev);
3889 return 0;
3890 }
3891
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003892 /* peer says his disk is inconsistent, while we think it is uptodate,
3893 * and this happens while the peer still thinks we have a sync going on,
3894 * but we think we are already done with the sync.
3895 * We ignore this to avoid flapping pdsk.
3896 * This should not happen, if the peer is a recent version of drbd. */
3897 if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT &&
3898 os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE)
3899 real_peer_disk = D_UP_TO_DATE;
3900
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003901 if (ns.conn == C_WF_REPORT_PARAMS)
3902 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003903
Philipp Reisner67531712010-10-27 12:21:30 +02003904 if (peer_state.conn == C_AHEAD)
3905 ns.conn = C_BEHIND;
3906
Philipp Reisnerb411b362009-09-25 16:07:19 -07003907 if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING &&
3908 get_ldev_if_state(mdev, D_NEGOTIATING)) {
3909 int cr; /* consider resync */
3910
3911 /* if we established a new connection */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003912 cr = (os.conn < C_CONNECTED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003913 /* if we had an established connection
3914 * and one of the nodes newly attaches a disk */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003915 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003916 (peer_state.disk == D_NEGOTIATING ||
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003917 os.disk == D_NEGOTIATING));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003918 /* if we have both been inconsistent, and the peer has been
3919 * forced to be UpToDate with --overwrite-data */
3920 cr |= test_bit(CONSIDER_RESYNC, &mdev->flags);
3921 /* if we had been plain connected, and the admin requested to
3922 * start a sync by "invalidate" or "invalidate-remote" */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003923 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003924 (peer_state.conn >= C_STARTING_SYNC_S &&
3925 peer_state.conn <= C_WF_BITMAP_T));
3926
3927 if (cr)
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003928 ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003929
3930 put_ldev(mdev);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003931 if (ns.conn == C_MASK) {
3932 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003933 if (mdev->state.disk == D_NEGOTIATING) {
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003934 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003935 } else if (peer_state.disk == D_NEGOTIATING) {
3936 dev_err(DEV, "Disk attach process on the peer node was aborted.\n");
3937 peer_state.disk = D_DISKLESS;
Lars Ellenberg580b9762010-02-26 23:15:23 +01003938 real_peer_disk = D_DISKLESS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003939 } else {
Philipp Reisner8169e412011-03-15 18:40:27 +01003940 if (test_and_clear_bit(CONN_DRY_RUN, &mdev->tconn->flags))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003941 return -EIO;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003942 D_ASSERT(os.conn == C_WF_REPORT_PARAMS);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003943 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003944 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003945 }
3946 }
3947 }
3948
Philipp Reisner87eeee42011-01-19 14:16:30 +01003949 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner78bae592011-03-28 15:40:12 +02003950 if (os.i != drbd_read_state(mdev).i)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003951 goto retry;
3952 clear_bit(CONSIDER_RESYNC, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003953 ns.peer = peer_state.role;
3954 ns.pdsk = real_peer_disk;
3955 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003956 if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003957 ns.disk = mdev->new_state_tmp.disk;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003958 cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD);
Philipp Reisner2aebfab2011-03-28 16:48:11 +02003959 if (ns.pdsk == D_CONSISTENT && drbd_suspended(mdev) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED &&
Philipp Reisner481c6f52010-06-22 14:03:27 +02003960 test_bit(NEW_CUR_UUID, &mdev->flags)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01003961 /* Do not allow tl_restart(RESEND) for a rebooted peer. We can only allow this
Philipp Reisner481c6f52010-06-22 14:03:27 +02003962 for temporal network outages! */
Philipp Reisner87eeee42011-01-19 14:16:30 +01003963 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003964 dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01003965 tl_clear(mdev->tconn);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003966 drbd_uuid_new_current(mdev);
3967 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003968 conn_request_state(mdev->tconn, NS2(conn, C_PROTOCOL_ERROR, susp, 0), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003969 return -EIO;
Philipp Reisner481c6f52010-06-22 14:03:27 +02003970 }
Philipp Reisner65d922c2010-06-16 16:18:09 +02003971 rv = _drbd_set_state(mdev, ns, cs_flags, NULL);
Philipp Reisner78bae592011-03-28 15:40:12 +02003972 ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003973 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003974
3975 if (rv < SS_SUCCESS) {
Philipp Reisner38fa9982011-03-15 18:24:49 +01003976 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003977 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003978 }
3979
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003980 if (os.conn > C_WF_REPORT_PARAMS) {
3981 if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003982 peer_state.disk != D_NEGOTIATING ) {
3983 /* we want resync, peer has not yet decided to sync... */
3984 /* Nowadays only used when forcing a node into primary role and
3985 setting its disk to UpToDate with that */
3986 drbd_send_uuids(mdev);
Philipp Reisner43de7c82011-11-10 13:16:13 +01003987 drbd_send_current_state(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003988 }
3989 }
3990
Philipp Reisner08b165b2011-09-05 16:22:33 +02003991 clear_bit(DISCARD_MY_DATA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003992
3993 drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
3994
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003995 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003996}
3997
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003998static int receive_sync_uuid(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003999{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004000 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004001 struct p_rs_uuid *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004002
4003 mdev = vnr_to_mdev(tconn, pi->vnr);
4004 if (!mdev)
4005 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004006
4007 wait_event(mdev->misc_wait,
4008 mdev->state.conn == C_WF_SYNC_UUID ||
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02004009 mdev->state.conn == C_BEHIND ||
Philipp Reisnerb411b362009-09-25 16:07:19 -07004010 mdev->state.conn < C_CONNECTED ||
4011 mdev->state.disk < D_NEGOTIATING);
4012
4013 /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */
4014
Philipp Reisnerb411b362009-09-25 16:07:19 -07004015 /* Here the _drbd_uuid_ functions are right, current should
4016 _not_ be rotated into the history */
4017 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
4018 _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid));
4019 _drbd_uuid_set(mdev, UI_BITMAP, 0UL);
4020
Lars Ellenberg62b0da32011-01-20 13:25:21 +01004021 drbd_print_uuids(mdev, "updated sync uuid");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004022 drbd_start_resync(mdev, C_SYNC_TARGET);
4023
4024 put_ldev(mdev);
4025 } else
4026 dev_err(DEV, "Ignoring SyncUUID packet!\n");
4027
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004028 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004029}
4030
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004031/**
4032 * receive_bitmap_plain
4033 *
4034 * Return 0 when done, 1 when another iteration is needed, and a negative error
4035 * code upon failure.
4036 */
4037static int
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004038receive_bitmap_plain(struct drbd_conf *mdev, unsigned int size,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004039 unsigned long *p, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004040{
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004041 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE -
4042 drbd_header_size(mdev->tconn);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004043 unsigned int num_words = min_t(size_t, data_size / sizeof(*p),
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004044 c->bm_words - c->word_offset);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004045 unsigned int want = num_words * sizeof(*p);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004046 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004047
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004048 if (want != size) {
4049 dev_err(DEV, "%s:want (%u) != size (%u)\n", __func__, want, size);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004050 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004051 }
4052 if (want == 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004053 return 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004054 err = drbd_recv_all(mdev->tconn, p, want);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004055 if (err)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004056 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004057
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004058 drbd_bm_merge_lel(mdev, c->word_offset, num_words, p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004059
4060 c->word_offset += num_words;
4061 c->bit_offset = c->word_offset * BITS_PER_LONG;
4062 if (c->bit_offset > c->bm_bits)
4063 c->bit_offset = c->bm_bits;
4064
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004065 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004066}
4067
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004068static enum drbd_bitmap_code dcbp_get_code(struct p_compressed_bm *p)
4069{
4070 return (enum drbd_bitmap_code)(p->encoding & 0x0f);
4071}
4072
4073static int dcbp_get_start(struct p_compressed_bm *p)
4074{
4075 return (p->encoding & 0x80) != 0;
4076}
4077
4078static int dcbp_get_pad_bits(struct p_compressed_bm *p)
4079{
4080 return (p->encoding >> 4) & 0x7;
4081}
4082
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004083/**
4084 * recv_bm_rle_bits
4085 *
4086 * Return 0 when done, 1 when another iteration is needed, and a negative error
4087 * code upon failure.
4088 */
4089static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07004090recv_bm_rle_bits(struct drbd_conf *mdev,
4091 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01004092 struct bm_xfer_ctx *c,
4093 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004094{
4095 struct bitstream bs;
4096 u64 look_ahead;
4097 u64 rl;
4098 u64 tmp;
4099 unsigned long s = c->bit_offset;
4100 unsigned long e;
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004101 int toggle = dcbp_get_start(p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004102 int have;
4103 int bits;
4104
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004105 bitstream_init(&bs, p->code, len, dcbp_get_pad_bits(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004106
4107 bits = bitstream_get_bits(&bs, &look_ahead, 64);
4108 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004109 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004110
4111 for (have = bits; have > 0; s += rl, toggle = !toggle) {
4112 bits = vli_decode_bits(&rl, look_ahead);
4113 if (bits <= 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004114 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004115
4116 if (toggle) {
4117 e = s + rl -1;
4118 if (e >= c->bm_bits) {
4119 dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004120 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004121 }
4122 _drbd_bm_set_bits(mdev, s, e);
4123 }
4124
4125 if (have < bits) {
4126 dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
4127 have, bits, look_ahead,
4128 (unsigned int)(bs.cur.b - p->code),
4129 (unsigned int)bs.buf_len);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004130 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004131 }
4132 look_ahead >>= bits;
4133 have -= bits;
4134
4135 bits = bitstream_get_bits(&bs, &tmp, 64 - have);
4136 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004137 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004138 look_ahead |= tmp << have;
4139 have += bits;
4140 }
4141
4142 c->bit_offset = s;
4143 bm_xfer_ctx_bit_to_word_offset(c);
4144
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004145 return (s != c->bm_bits);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004146}
4147
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004148/**
4149 * decode_bitmap_c
4150 *
4151 * Return 0 when done, 1 when another iteration is needed, and a negative error
4152 * code upon failure.
4153 */
4154static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07004155decode_bitmap_c(struct drbd_conf *mdev,
4156 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01004157 struct bm_xfer_ctx *c,
4158 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004159{
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004160 if (dcbp_get_code(p) == RLE_VLI_Bits)
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004161 return recv_bm_rle_bits(mdev, p, c, len - sizeof(*p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004162
4163 /* other variants had been implemented for evaluation,
4164 * but have been dropped as this one turned out to be "best"
4165 * during all our tests. */
4166
4167 dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
Philipp Reisner38fa9982011-03-15 18:24:49 +01004168 conn_request_state(mdev->tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004169 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004170}
4171
4172void INFO_bm_xfer_stats(struct drbd_conf *mdev,
4173 const char *direction, struct bm_xfer_ctx *c)
4174{
4175 /* what would it take to transfer it "plaintext" */
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004176 unsigned int header_size = drbd_header_size(mdev->tconn);
4177 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE - header_size;
4178 unsigned int plain =
4179 header_size * (DIV_ROUND_UP(c->bm_words, data_size) + 1) +
4180 c->bm_words * sizeof(unsigned long);
4181 unsigned int total = c->bytes[0] + c->bytes[1];
4182 unsigned int r;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004183
4184 /* total can not be zero. but just in case: */
4185 if (total == 0)
4186 return;
4187
4188 /* don't report if not compressed */
4189 if (total >= plain)
4190 return;
4191
4192 /* total < plain. check for overflow, still */
4193 r = (total > UINT_MAX/1000) ? (total / (plain/1000))
4194 : (1000 * total / plain);
4195
4196 if (r > 1000)
4197 r = 1000;
4198
4199 r = 1000 - r;
4200 dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
4201 "total %u; compression: %u.%u%%\n",
4202 direction,
4203 c->bytes[1], c->packets[1],
4204 c->bytes[0], c->packets[0],
4205 total, r/10, r % 10);
4206}
4207
4208/* Since we are processing the bitfield from lower addresses to higher,
4209 it does not matter if the process it in 32 bit chunks or 64 bit
4210 chunks as long as it is little endian. (Understand it as byte stream,
4211 beginning with the lowest byte...) If we would use big endian
4212 we would need to process it from the highest address to the lowest,
4213 in order to be agnostic to the 32 vs 64 bits issue.
4214
4215 returns 0 on failure, 1 if we successfully received it. */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004216static int receive_bitmap(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004217{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004218 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004219 struct bm_xfer_ctx c;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004220 int err;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004221
4222 mdev = vnr_to_mdev(tconn, pi->vnr);
4223 if (!mdev)
4224 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004225
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004226 drbd_bm_lock(mdev, "receive bitmap", BM_LOCKED_SET_ALLOWED);
4227 /* you are supposed to send additional out-of-sync information
4228 * if you actually set bits during this phase */
Philipp Reisnerb411b362009-09-25 16:07:19 -07004229
Philipp Reisnerb411b362009-09-25 16:07:19 -07004230 c = (struct bm_xfer_ctx) {
4231 .bm_bits = drbd_bm_bits(mdev),
4232 .bm_words = drbd_bm_words(mdev),
4233 };
4234
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004235 for(;;) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004236 if (pi->cmd == P_BITMAP)
4237 err = receive_bitmap_plain(mdev, pi->size, pi->data, &c);
4238 else if (pi->cmd == P_COMPRESSED_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004239 /* MAYBE: sanity check that we speak proto >= 90,
4240 * and the feature is enabled! */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004241 struct p_compressed_bm *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004242
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004243 if (pi->size > DRBD_SOCKET_BUFFER_SIZE - drbd_header_size(tconn)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004244 dev_err(DEV, "ReportCBitmap packet too large\n");
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004245 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004246 goto out;
4247 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004248 if (pi->size <= sizeof(*p)) {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004249 dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004250 err = -EIO;
Andreas Gruenbacher78fcbda2010-12-10 22:18:27 +01004251 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004252 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004253 err = drbd_recv_all(mdev->tconn, p, pi->size);
4254 if (err)
4255 goto out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004256 err = decode_bitmap_c(mdev, p, &c, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004257 } else {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004258 dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", pi->cmd);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004259 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004260 goto out;
4261 }
4262
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004263 c.packets[pi->cmd == P_BITMAP]++;
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004264 c.bytes[pi->cmd == P_BITMAP] += drbd_header_size(tconn) + pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004265
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004266 if (err <= 0) {
4267 if (err < 0)
4268 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004269 break;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004270 }
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004271 err = drbd_recv_header(mdev->tconn, pi);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004272 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004273 goto out;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004274 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004275
4276 INFO_bm_xfer_stats(mdev, "receive", &c);
4277
4278 if (mdev->state.conn == C_WF_BITMAP_T) {
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01004279 enum drbd_state_rv rv;
4280
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004281 err = drbd_send_bitmap(mdev);
4282 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004283 goto out;
4284 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01004285 rv = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
4286 D_ASSERT(rv == SS_SUCCESS);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004287 } else if (mdev->state.conn != C_WF_BITMAP_S) {
4288 /* admin may have requested C_DISCONNECTING,
4289 * other threads may have noticed network errors */
4290 dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n",
4291 drbd_conn_str(mdev->state.conn));
4292 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004293 err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004294
Philipp Reisnerb411b362009-09-25 16:07:19 -07004295 out:
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004296 drbd_bm_unlock(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004297 if (!err && mdev->state.conn == C_WF_BITMAP_S)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004298 drbd_start_resync(mdev, C_SYNC_SOURCE);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004299 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004300}
4301
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004302static int receive_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004303{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004304 conn_warn(tconn, "skipping unknown optional packet type %d, l: %d!\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004305 pi->cmd, pi->size);
Philipp Reisner2de876e2011-03-15 14:38:01 +01004306
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004307 return ignore_remaining_packet(tconn, pi);
Philipp Reisner2de876e2011-03-15 14:38:01 +01004308}
4309
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004310static int receive_UnplugRemote(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004311{
Philipp Reisnerb411b362009-09-25 16:07:19 -07004312 /* Make sure we've acked all the TCP data associated
4313 * with the data requests being unplugged */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004314 drbd_tcp_quickack(tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004315
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004316 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004317}
4318
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004319static int receive_out_of_sync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner73a01a12010-10-27 14:33:00 +02004320{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004321 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004322 struct p_block_desc *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004323
4324 mdev = vnr_to_mdev(tconn, pi->vnr);
4325 if (!mdev)
4326 return -EIO;
Philipp Reisner73a01a12010-10-27 14:33:00 +02004327
Lars Ellenbergf735e3632010-12-17 21:06:18 +01004328 switch (mdev->state.conn) {
4329 case C_WF_SYNC_UUID:
4330 case C_WF_BITMAP_T:
4331 case C_BEHIND:
4332 break;
4333 default:
4334 dev_err(DEV, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n",
4335 drbd_conn_str(mdev->state.conn));
4336 }
4337
Philipp Reisner73a01a12010-10-27 14:33:00 +02004338 drbd_set_out_of_sync(mdev, be64_to_cpu(p->sector), be32_to_cpu(p->blksize));
4339
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004340 return 0;
Philipp Reisner73a01a12010-10-27 14:33:00 +02004341}
4342
Philipp Reisner02918be2010-08-20 14:35:10 +02004343struct data_cmd {
4344 int expect_payload;
4345 size_t pkt_size;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004346 int (*fn)(struct drbd_tconn *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004347};
4348
Philipp Reisner02918be2010-08-20 14:35:10 +02004349static struct data_cmd drbd_cmd_handler[] = {
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004350 [P_DATA] = { 1, sizeof(struct p_data), receive_Data },
4351 [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply },
4352 [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } ,
4353 [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } ,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004354 [P_BITMAP] = { 1, 0, receive_bitmap } ,
4355 [P_COMPRESSED_BITMAP] = { 1, 0, receive_bitmap } ,
4356 [P_UNPLUG_REMOTE] = { 0, 0, receive_UnplugRemote },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004357 [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4358 [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004359 [P_SYNC_PARAM] = { 1, 0, receive_SyncParam },
4360 [P_SYNC_PARAM89] = { 1, 0, receive_SyncParam },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004361 [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol },
4362 [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids },
4363 [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes },
4364 [P_STATE] = { 0, sizeof(struct p_state), receive_state },
4365 [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state },
4366 [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid },
4367 [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4368 [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4369 [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4370 [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip },
4371 [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync },
4372 [P_CONN_ST_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_conn_state },
Philipp Reisner036b17e2011-05-16 17:38:11 +02004373 [P_PROTOCOL_UPDATE] = { 1, sizeof(struct p_protocol), receive_protocol },
Philipp Reisner02918be2010-08-20 14:35:10 +02004374};
4375
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004376static void drbdd(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004377{
Philipp Reisner77351055b2011-02-07 17:24:26 +01004378 struct packet_info pi;
Philipp Reisner02918be2010-08-20 14:35:10 +02004379 size_t shs; /* sub header size */
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004380 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004381
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004382 while (get_t_state(&tconn->receiver) == RUNNING) {
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004383 struct data_cmd *cmd;
4384
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004385 drbd_thread_current_set_cpu(&tconn->receiver);
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004386 if (drbd_recv_header(tconn, &pi))
Philipp Reisner02918be2010-08-20 14:35:10 +02004387 goto err_out;
4388
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004389 cmd = &drbd_cmd_handler[pi.cmd];
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004390 if (unlikely(pi.cmd >= ARRAY_SIZE(drbd_cmd_handler) || !cmd->fn)) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004391 conn_err(tconn, "Unexpected data packet %s (0x%04x)",
4392 cmdname(pi.cmd), pi.cmd);
Philipp Reisner02918be2010-08-20 14:35:10 +02004393 goto err_out;
Lars Ellenberg0b33a912009-11-16 15:58:04 +01004394 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004395
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004396 shs = cmd->pkt_size;
4397 if (pi.size > shs && !cmd->expect_payload) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004398 conn_err(tconn, "No payload expected %s l:%d\n",
4399 cmdname(pi.cmd), pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004400 goto err_out;
4401 }
4402
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004403 if (shs) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004404 err = drbd_recv_all_warn(tconn, pi.data, shs);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004405 if (err)
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004406 goto err_out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004407 pi.size -= shs;
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004408 }
4409
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004410 err = cmd->fn(tconn, &pi);
4411 if (err) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004412 conn_err(tconn, "error receiving %s, e: %d l: %d!\n",
4413 cmdname(pi.cmd), err, pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004414 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004415 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004416 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004417 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004418
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004419 err_out:
4420 conn_request_state(tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004421}
4422
Philipp Reisner0e29d162011-02-18 14:23:11 +01004423void conn_flush_workqueue(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004424{
4425 struct drbd_wq_barrier barr;
4426
4427 barr.w.cb = w_prev_work_done;
Philipp Reisner0e29d162011-02-18 14:23:11 +01004428 barr.w.tconn = tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004429 init_completion(&barr.done);
Lars Ellenbergd5b27b02011-11-14 15:42:37 +01004430 drbd_queue_work(&tconn->sender_work, &barr.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004431 wait_for_completion(&barr.done);
4432}
4433
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004434static void conn_disconnect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004435{
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004436 struct drbd_conf *mdev;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004437 enum drbd_conns oc;
Philipp Reisner376694a2011-11-07 10:54:28 +01004438 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004439
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004440 if (tconn->cstate == C_STANDALONE)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004441 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004442
Philipp Reisnerb8853db2011-12-13 11:09:16 +01004443 /* We are about to start the cleanup after connection loss.
4444 * Make sure drbd_make_request knows about that.
4445 * Usually we should be in some network failure state already,
4446 * but just in case we are not, we fix it up here.
4447 */
4448 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
4449
Philipp Reisnerb411b362009-09-25 16:07:19 -07004450 /* asender does not clean up anything. it must not interfere, either */
Philipp Reisner360cc742011-02-08 14:29:53 +01004451 drbd_thread_stop(&tconn->asender);
4452 drbd_free_sock(tconn);
4453
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004454 rcu_read_lock();
4455 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
4456 kref_get(&mdev->kref);
4457 rcu_read_unlock();
4458 drbd_disconnected(mdev);
4459 kref_put(&mdev->kref, &drbd_minor_destroy);
4460 rcu_read_lock();
4461 }
4462 rcu_read_unlock();
4463
Philipp Reisner12038a32011-11-09 19:18:00 +01004464 if (!list_empty(&tconn->current_epoch->list))
4465 conn_err(tconn, "ASSERTION FAILED: tconn->current_epoch->list not empty\n");
4466 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
4467 atomic_set(&tconn->current_epoch->epoch_size, 0);
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01004468 tconn->send.seen_any_write_yet = false;
Philipp Reisner12038a32011-11-09 19:18:00 +01004469
Philipp Reisner360cc742011-02-08 14:29:53 +01004470 conn_info(tconn, "Connection closed\n");
4471
Philipp Reisnercb703452011-03-24 11:03:07 +01004472 if (conn_highest_role(tconn) == R_PRIMARY && conn_highest_pdsk(tconn) >= D_UNKNOWN)
4473 conn_try_outdate_peer_async(tconn);
4474
Philipp Reisner360cc742011-02-08 14:29:53 +01004475 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004476 oc = tconn->cstate;
4477 if (oc >= C_UNCONNECTED)
Philipp Reisner376694a2011-11-07 10:54:28 +01004478 _conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004479
Philipp Reisner360cc742011-02-08 14:29:53 +01004480 spin_unlock_irq(&tconn->req_lock);
4481
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02004482 if (oc == C_DISCONNECTING)
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +02004483 conn_request_state(tconn, NS(conn, C_STANDALONE), CS_VERBOSE | CS_HARD);
Philipp Reisner360cc742011-02-08 14:29:53 +01004484}
4485
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004486static int drbd_disconnected(struct drbd_conf *mdev)
Philipp Reisner360cc742011-02-08 14:29:53 +01004487{
Philipp Reisner360cc742011-02-08 14:29:53 +01004488 unsigned int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004489
Philipp Reisner85719572010-07-21 10:20:17 +02004490 /* wait for current activity to cease. */
Philipp Reisner87eeee42011-01-19 14:16:30 +01004491 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004492 _drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
4493 _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee);
4494 _drbd_wait_ee_list_empty(mdev, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004495 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004496
4497 /* We do not have data structures that would allow us to
4498 * get the rs_pending_cnt down to 0 again.
4499 * * On C_SYNC_TARGET we do not have any data structures describing
4500 * the pending RSDataRequest's we have sent.
4501 * * On C_SYNC_SOURCE there is no data structure that tracks
4502 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
4503 * And no, it is not the sum of the reference counts in the
4504 * resync_LRU. The resync_LRU tracks the whole operation including
4505 * the disk-IO, while the rs_pending_cnt only tracks the blocks
4506 * on the fly. */
4507 drbd_rs_cancel_all(mdev);
4508 mdev->rs_total = 0;
4509 mdev->rs_failed = 0;
4510 atomic_set(&mdev->rs_pending_cnt, 0);
4511 wake_up(&mdev->misc_wait);
4512
Philipp Reisnerb411b362009-09-25 16:07:19 -07004513 del_timer_sync(&mdev->resync_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004514 resync_timer_fn((unsigned long)mdev);
4515
Philipp Reisnerb411b362009-09-25 16:07:19 -07004516 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
4517 * w_make_resync_request etc. which may still be on the worker queue
4518 * to be "canceled" */
Philipp Reisnera21e9292011-02-08 15:08:49 +01004519 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004520
Andreas Gruenbachera990be42011-04-06 17:56:48 +02004521 drbd_finish_peer_reqs(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004522
Philipp Reisnerd10b4ea2011-11-30 23:25:36 +01004523 /* This second workqueue flush is necessary, since drbd_finish_peer_reqs()
4524 might have issued a work again. The one before drbd_finish_peer_reqs() is
4525 necessary to reclain net_ee in drbd_finish_peer_reqs(). */
4526 drbd_flush_workqueue(mdev);
4527
Philipp Reisnerb411b362009-09-25 16:07:19 -07004528 kfree(mdev->p_uuid);
4529 mdev->p_uuid = NULL;
4530
Philipp Reisner2aebfab2011-03-28 16:48:11 +02004531 if (!drbd_suspended(mdev))
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01004532 tl_clear(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004533
Philipp Reisnerb411b362009-09-25 16:07:19 -07004534 drbd_md_sync(mdev);
4535
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004536 /* serialize with bitmap writeout triggered by the state change,
4537 * if any. */
4538 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
4539
Philipp Reisnerb411b362009-09-25 16:07:19 -07004540 /* tcp_close and release of sendpage pages can be deferred. I don't
4541 * want to use SO_LINGER, because apparently it can be deferred for
4542 * more than 20 seconds (longest time I checked).
4543 *
4544 * Actually we don't care for exactly when the network stack does its
4545 * put_page(), but release our reference on these pages right here.
4546 */
Andreas Gruenbacher7721f562011-04-06 17:14:02 +02004547 i = drbd_free_peer_reqs(mdev, &mdev->net_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004548 if (i)
4549 dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
Lars Ellenberg435f0742010-09-06 12:30:25 +02004550 i = atomic_read(&mdev->pp_in_use_by_net);
4551 if (i)
4552 dev_info(DEV, "pp_in_use_by_net = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004553 i = atomic_read(&mdev->pp_in_use);
4554 if (i)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02004555 dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004556
4557 D_ASSERT(list_empty(&mdev->read_ee));
4558 D_ASSERT(list_empty(&mdev->active_ee));
4559 D_ASSERT(list_empty(&mdev->sync_ee));
4560 D_ASSERT(list_empty(&mdev->done_ee));
4561
Philipp Reisner360cc742011-02-08 14:29:53 +01004562 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004563}
4564
4565/*
4566 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
4567 * we can agree on is stored in agreed_pro_version.
4568 *
4569 * feature flags and the reserved array should be enough room for future
4570 * enhancements of the handshake protocol, and possible plugins...
4571 *
4572 * for now, they are expected to be zero, but ignored.
4573 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004574static int drbd_send_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004575{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004576 struct drbd_socket *sock;
4577 struct p_connection_features *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004578
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004579 sock = &tconn->data;
4580 p = conn_prepare_command(tconn, sock);
4581 if (!p)
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004582 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004583 memset(p, 0, sizeof(*p));
4584 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
4585 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004586 return conn_send_command(tconn, sock, P_CONNECTION_FEATURES, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004587}
4588
4589/*
4590 * return values:
4591 * 1 yes, we have a valid connection
4592 * 0 oops, did not work out, please try again
4593 * -1 peer talks different language,
4594 * no point in trying again, please go standalone.
4595 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004596static int drbd_do_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004597{
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004598 /* ASSERT current == tconn->receiver ... */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004599 struct p_connection_features *p;
4600 const int expect = sizeof(struct p_connection_features);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004601 struct packet_info pi;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004602 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004603
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004604 err = drbd_send_features(tconn);
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004605 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004606 return 0;
4607
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004608 err = drbd_recv_header(tconn, &pi);
4609 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004610 return 0;
4611
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004612 if (pi.cmd != P_CONNECTION_FEATURES) {
4613 conn_err(tconn, "expected ConnectionFeatures packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004614 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004615 return -1;
4616 }
4617
Philipp Reisner77351055b2011-02-07 17:24:26 +01004618 if (pi.size != expect) {
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004619 conn_err(tconn, "expected ConnectionFeatures length: %u, received: %u\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004620 expect, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004621 return -1;
4622 }
4623
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004624 p = pi.data;
4625 err = drbd_recv_all_warn(tconn, p, expect);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004626 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004627 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004628
Philipp Reisnerb411b362009-09-25 16:07:19 -07004629 p->protocol_min = be32_to_cpu(p->protocol_min);
4630 p->protocol_max = be32_to_cpu(p->protocol_max);
4631 if (p->protocol_max == 0)
4632 p->protocol_max = p->protocol_min;
4633
4634 if (PRO_VERSION_MAX < p->protocol_min ||
4635 PRO_VERSION_MIN > p->protocol_max)
4636 goto incompat;
4637
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004638 tconn->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004639
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004640 conn_info(tconn, "Handshake successful: "
4641 "Agreed network protocol version %d\n", tconn->agreed_pro_version);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004642
4643 return 1;
4644
4645 incompat:
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004646 conn_err(tconn, "incompatible DRBD dialects: "
Philipp Reisnerb411b362009-09-25 16:07:19 -07004647 "I support %d-%d, peer supports %d-%d\n",
4648 PRO_VERSION_MIN, PRO_VERSION_MAX,
4649 p->protocol_min, p->protocol_max);
4650 return -1;
4651}
4652
4653#if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
Philipp Reisner13e60372011-02-08 09:54:40 +01004654static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004655{
4656 dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
4657 dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004658 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004659}
4660#else
4661#define CHALLENGE_LEN 64
Johannes Thomab10d96c2010-01-07 16:02:50 +01004662
4663/* Return value:
4664 1 - auth succeeded,
4665 0 - failed, try again (network error),
4666 -1 - auth failed, don't try again.
4667*/
4668
Philipp Reisner13e60372011-02-08 09:54:40 +01004669static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004670{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004671 struct drbd_socket *sock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004672 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
4673 struct scatterlist sg;
4674 char *response = NULL;
4675 char *right_response = NULL;
4676 char *peers_ch = NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +02004677 unsigned int key_len;
4678 char secret[SHARED_SECRET_MAX]; /* 64 byte */
Philipp Reisnerb411b362009-09-25 16:07:19 -07004679 unsigned int resp_size;
4680 struct hash_desc desc;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004681 struct packet_info pi;
Philipp Reisner44ed1672011-04-19 17:10:19 +02004682 struct net_conf *nc;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004683 int err, rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004684
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004685 /* FIXME: Put the challenge/response into the preallocated socket buffer. */
4686
Philipp Reisner44ed1672011-04-19 17:10:19 +02004687 rcu_read_lock();
4688 nc = rcu_dereference(tconn->net_conf);
4689 key_len = strlen(nc->shared_secret);
4690 memcpy(secret, nc->shared_secret, key_len);
4691 rcu_read_unlock();
4692
Philipp Reisner13e60372011-02-08 09:54:40 +01004693 desc.tfm = tconn->cram_hmac_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004694 desc.flags = 0;
4695
Philipp Reisner44ed1672011-04-19 17:10:19 +02004696 rv = crypto_hash_setkey(tconn->cram_hmac_tfm, (u8 *)secret, key_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004697 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004698 conn_err(tconn, "crypto_hash_setkey() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004699 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004700 goto fail;
4701 }
4702
4703 get_random_bytes(my_challenge, CHALLENGE_LEN);
4704
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004705 sock = &tconn->data;
4706 if (!conn_prepare_command(tconn, sock)) {
4707 rv = 0;
4708 goto fail;
4709 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004710 rv = !conn_send_command(tconn, sock, P_AUTH_CHALLENGE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004711 my_challenge, CHALLENGE_LEN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004712 if (!rv)
4713 goto fail;
4714
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004715 err = drbd_recv_header(tconn, &pi);
4716 if (err) {
4717 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004718 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004719 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004720
Philipp Reisner77351055b2011-02-07 17:24:26 +01004721 if (pi.cmd != P_AUTH_CHALLENGE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004722 conn_err(tconn, "expected AuthChallenge packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004723 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004724 rv = 0;
4725 goto fail;
4726 }
4727
Philipp Reisner77351055b2011-02-07 17:24:26 +01004728 if (pi.size > CHALLENGE_LEN * 2) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004729 conn_err(tconn, "expected AuthChallenge payload too big.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004730 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004731 goto fail;
4732 }
4733
Philipp Reisner77351055b2011-02-07 17:24:26 +01004734 peers_ch = kmalloc(pi.size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004735 if (peers_ch == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004736 conn_err(tconn, "kmalloc of peers_ch failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004737 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004738 goto fail;
4739 }
4740
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004741 err = drbd_recv_all_warn(tconn, peers_ch, pi.size);
4742 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004743 rv = 0;
4744 goto fail;
4745 }
4746
Philipp Reisner13e60372011-02-08 09:54:40 +01004747 resp_size = crypto_hash_digestsize(tconn->cram_hmac_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004748 response = kmalloc(resp_size, GFP_NOIO);
4749 if (response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004750 conn_err(tconn, "kmalloc of response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004751 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004752 goto fail;
4753 }
4754
4755 sg_init_table(&sg, 1);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004756 sg_set_buf(&sg, peers_ch, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004757
4758 rv = crypto_hash_digest(&desc, &sg, sg.length, response);
4759 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004760 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004761 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004762 goto fail;
4763 }
4764
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004765 if (!conn_prepare_command(tconn, sock)) {
4766 rv = 0;
4767 goto fail;
4768 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004769 rv = !conn_send_command(tconn, sock, P_AUTH_RESPONSE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004770 response, resp_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004771 if (!rv)
4772 goto fail;
4773
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004774 err = drbd_recv_header(tconn, &pi);
4775 if (err) {
4776 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004777 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004778 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004779
Philipp Reisner77351055b2011-02-07 17:24:26 +01004780 if (pi.cmd != P_AUTH_RESPONSE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004781 conn_err(tconn, "expected AuthResponse packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004782 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004783 rv = 0;
4784 goto fail;
4785 }
4786
Philipp Reisner77351055b2011-02-07 17:24:26 +01004787 if (pi.size != resp_size) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004788 conn_err(tconn, "expected AuthResponse payload of wrong size\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004789 rv = 0;
4790 goto fail;
4791 }
4792
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004793 err = drbd_recv_all_warn(tconn, response , resp_size);
4794 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004795 rv = 0;
4796 goto fail;
4797 }
4798
4799 right_response = kmalloc(resp_size, GFP_NOIO);
Julia Lawall2d1ee872009-12-27 22:27:11 +01004800 if (right_response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004801 conn_err(tconn, "kmalloc of right_response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004802 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004803 goto fail;
4804 }
4805
4806 sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
4807
4808 rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
4809 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004810 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004811 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004812 goto fail;
4813 }
4814
4815 rv = !memcmp(response, right_response, resp_size);
4816
4817 if (rv)
Philipp Reisner44ed1672011-04-19 17:10:19 +02004818 conn_info(tconn, "Peer authenticated using %d bytes HMAC\n",
4819 resp_size);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004820 else
4821 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004822
4823 fail:
4824 kfree(peers_ch);
4825 kfree(response);
4826 kfree(right_response);
4827
4828 return rv;
4829}
4830#endif
4831
4832int drbdd_init(struct drbd_thread *thi)
4833{
Philipp Reisner392c8802011-02-09 10:33:31 +01004834 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004835 int h;
4836
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004837 conn_info(tconn, "receiver (re)started\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004838
4839 do {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004840 h = conn_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004841 if (h == 0) {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004842 conn_disconnect(tconn);
Philipp Reisner20ee6392011-01-18 15:28:59 +01004843 schedule_timeout_interruptible(HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004844 }
4845 if (h == -1) {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004846 conn_warn(tconn, "Discarding network configuration.\n");
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004847 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004848 }
4849 } while (h == 0);
4850
Philipp Reisner91fd4da2011-04-20 17:47:29 +02004851 if (h > 0)
4852 drbdd(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004853
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004854 conn_disconnect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004855
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004856 conn_info(tconn, "receiver terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004857 return 0;
4858}
4859
4860/* ********* acknowledge sender ******** */
4861
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004862static int got_conn_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004863{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004864 struct p_req_state_reply *p = pi->data;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004865 int retcode = be32_to_cpu(p->retcode);
4866
4867 if (retcode >= SS_SUCCESS) {
4868 set_bit(CONN_WD_ST_CHG_OKAY, &tconn->flags);
4869 } else {
4870 set_bit(CONN_WD_ST_CHG_FAIL, &tconn->flags);
4871 conn_err(tconn, "Requested state change failed by peer: %s (%d)\n",
4872 drbd_set_st_err_str(retcode), retcode);
4873 }
4874 wake_up(&tconn->ping_wait);
4875
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004876 return 0;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004877}
4878
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004879static int got_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004880{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004881 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004882 struct p_req_state_reply *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004883 int retcode = be32_to_cpu(p->retcode);
4884
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004885 mdev = vnr_to_mdev(tconn, pi->vnr);
4886 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004887 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004888
Philipp Reisner4d0fc3f2012-01-20 13:52:27 +01004889 if (test_bit(CONN_WD_ST_CHG_REQ, &tconn->flags)) {
4890 D_ASSERT(tconn->agreed_pro_version < 100);
4891 return got_conn_RqSReply(tconn, pi);
4892 }
4893
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004894 if (retcode >= SS_SUCCESS) {
4895 set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
4896 } else {
4897 set_bit(CL_ST_CHG_FAIL, &mdev->flags);
4898 dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
4899 drbd_set_st_err_str(retcode), retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004900 }
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004901 wake_up(&mdev->state_wait);
4902
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004903 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004904}
4905
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004906static int got_Ping(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004907{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004908 return drbd_send_ping_ack(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004909
4910}
4911
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004912static int got_PingAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004913{
4914 /* restore idle timeout */
Philipp Reisner2a67d8b2011-02-09 14:10:32 +01004915 tconn->meta.socket->sk->sk_rcvtimeo = tconn->net_conf->ping_int*HZ;
4916 if (!test_and_set_bit(GOT_PING_ACK, &tconn->flags))
4917 wake_up(&tconn->ping_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004918
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004919 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004920}
4921
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004922static int got_IsInSync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004923{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004924 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004925 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004926 sector_t sector = be64_to_cpu(p->sector);
4927 int blksize = be32_to_cpu(p->blksize);
4928
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004929 mdev = vnr_to_mdev(tconn, pi->vnr);
4930 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004931 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004932
Philipp Reisner31890f42011-01-19 14:12:51 +01004933 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004934
4935 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4936
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004937 if (get_ldev(mdev)) {
4938 drbd_rs_complete_io(mdev, sector);
4939 drbd_set_in_sync(mdev, sector, blksize);
4940 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
4941 mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
4942 put_ldev(mdev);
4943 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004944 dec_rs_pending(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02004945 atomic_add(blksize >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004946
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004947 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004948}
4949
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004950static int
4951validate_req_change_req_state(struct drbd_conf *mdev, u64 id, sector_t sector,
4952 struct rb_root *root, const char *func,
4953 enum drbd_req_event what, bool missing_ok)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004954{
4955 struct drbd_request *req;
4956 struct bio_and_error m;
4957
Philipp Reisner87eeee42011-01-19 14:16:30 +01004958 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004959 req = find_request(mdev, root, id, sector, missing_ok, func);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004960 if (unlikely(!req)) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01004961 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004962 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004963 }
4964 __req_mod(req, what, &m);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004965 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004966
4967 if (m.bio)
4968 complete_master_bio(mdev, &m);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004969 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004970}
4971
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004972static int got_BlockAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004973{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004974 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004975 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004976 sector_t sector = be64_to_cpu(p->sector);
4977 int blksize = be32_to_cpu(p->blksize);
4978 enum drbd_req_event what;
4979
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004980 mdev = vnr_to_mdev(tconn, pi->vnr);
4981 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004982 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004983
Philipp Reisnerb411b362009-09-25 16:07:19 -07004984 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4985
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004986 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004987 drbd_set_in_sync(mdev, sector, blksize);
4988 dec_rs_pending(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004989 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004990 }
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004991 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004992 case P_RS_WRITE_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004993 what = WRITE_ACKED_BY_PEER_AND_SIS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004994 break;
4995 case P_WRITE_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004996 what = WRITE_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004997 break;
4998 case P_RECV_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004999 what = RECV_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005000 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01005001 case P_DISCARD_WRITE:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01005002 what = DISCARD_WRITE;
5003 break;
5004 case P_RETRY_WRITE:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01005005 what = POSTPONE_WRITE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005006 break;
5007 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005008 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07005009 }
5010
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005011 return validate_req_change_req_state(mdev, p->block_id, sector,
5012 &mdev->write_requests, __func__,
5013 what, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005014}
5015
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005016static int got_NegAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005017{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005018 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005019 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005020 sector_t sector = be64_to_cpu(p->sector);
Philipp Reisner2deb8332011-01-17 18:39:18 +01005021 int size = be32_to_cpu(p->blksize);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02005022 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005023
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005024 mdev = vnr_to_mdev(tconn, pi->vnr);
5025 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005026 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005027
Philipp Reisnerb411b362009-09-25 16:07:19 -07005028 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5029
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01005030 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07005031 dec_rs_pending(mdev);
5032 drbd_rs_failed_io(mdev, sector, size);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005033 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005034 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01005035
Andreas Gruenbacher85997672011-04-04 13:09:15 +02005036 err = validate_req_change_req_state(mdev, p->block_id, sector,
5037 &mdev->write_requests, __func__,
Philipp Reisner303d1442011-04-13 16:24:47 -07005038 NEG_ACKED, true);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02005039 if (err) {
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01005040 /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs.
5041 The master bio might already be completed, therefore the
5042 request is no longer in the collision hash. */
5043 /* In Protocol B we might already have got a P_RECV_ACK
5044 but then get a P_NEG_ACK afterwards. */
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01005045 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisner2deb8332011-01-17 18:39:18 +01005046 }
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005047 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005048}
5049
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005050static int got_NegDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005051{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005052 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005053 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005054 sector_t sector = be64_to_cpu(p->sector);
5055
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005056 mdev = vnr_to_mdev(tconn, pi->vnr);
5057 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005058 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005059
Philipp Reisnerb411b362009-09-25 16:07:19 -07005060 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01005061
Philipp Reisner380207d2011-11-11 12:31:20 +01005062 dev_err(DEV, "Got NegDReply; Sector %llus, len %u.\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -07005063 (unsigned long long)sector, be32_to_cpu(p->blksize));
5064
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005065 return validate_req_change_req_state(mdev, p->block_id, sector,
5066 &mdev->read_requests, __func__,
5067 NEG_ACKED, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005068}
5069
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005070static int got_NegRSDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005071{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005072 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005073 sector_t sector;
5074 int size;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005075 struct p_block_ack *p = pi->data;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005076
5077 mdev = vnr_to_mdev(tconn, pi->vnr);
5078 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005079 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005080
5081 sector = be64_to_cpu(p->sector);
5082 size = be32_to_cpu(p->blksize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005083
5084 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5085
5086 dec_rs_pending(mdev);
5087
5088 if (get_ldev_if_state(mdev, D_FAILED)) {
5089 drbd_rs_complete_io(mdev, sector);
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01005090 switch (pi->cmd) {
Philipp Reisnerd612d302010-12-27 10:53:28 +01005091 case P_NEG_RS_DREPLY:
5092 drbd_rs_failed_io(mdev, sector, size);
5093 case P_RS_CANCEL:
5094 break;
5095 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005096 BUG();
Philipp Reisnerd612d302010-12-27 10:53:28 +01005097 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005098 put_ldev(mdev);
5099 }
5100
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005101 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005102}
5103
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005104static int got_BarrierAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005105{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005106 struct p_barrier_ack *p = pi->data;
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02005107 struct drbd_conf *mdev;
5108 int vnr;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005109
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02005110 tl_release(tconn, p->barrier, be32_to_cpu(p->set_size));
Philipp Reisnerb411b362009-09-25 16:07:19 -07005111
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02005112 rcu_read_lock();
5113 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
5114 if (mdev->state.conn == C_AHEAD &&
5115 atomic_read(&mdev->ap_in_flight) == 0 &&
5116 !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->flags)) {
5117 mdev->start_resync_timer.expires = jiffies + HZ;
5118 add_timer(&mdev->start_resync_timer);
5119 }
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02005120 }
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02005121 rcu_read_unlock();
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02005122
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005123 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005124}
5125
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005126static int got_OVResult(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005127{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005128 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005129 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005130 struct drbd_work *w;
5131 sector_t sector;
5132 int size;
5133
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005134 mdev = vnr_to_mdev(tconn, pi->vnr);
5135 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005136 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005137
Philipp Reisnerb411b362009-09-25 16:07:19 -07005138 sector = be64_to_cpu(p->sector);
5139 size = be32_to_cpu(p->blksize);
5140
5141 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5142
5143 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005144 drbd_ov_out_of_sync_found(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005145 else
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005146 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005147
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005148 if (!get_ldev(mdev))
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005149 return 0;
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005150
Philipp Reisnerb411b362009-09-25 16:07:19 -07005151 drbd_rs_complete_io(mdev, sector);
5152 dec_rs_pending(mdev);
5153
Lars Ellenbergea5442a2010-11-05 09:48:01 +01005154 --mdev->ov_left;
5155
5156 /* let's advance progress step marks only for every other megabyte */
5157 if ((mdev->ov_left & 0x200) == 0x200)
5158 drbd_advance_rs_marks(mdev, mdev->ov_left);
5159
5160 if (mdev->ov_left == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07005161 w = kmalloc(sizeof(*w), GFP_NOIO);
5162 if (w) {
5163 w->cb = w_ov_finished;
Philipp Reisnera21e9292011-02-08 15:08:49 +01005164 w->mdev = mdev;
Lars Ellenbergd5b27b02011-11-14 15:42:37 +01005165 drbd_queue_work(&mdev->tconn->sender_work, w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005166 } else {
5167 dev_err(DEV, "kmalloc(w) failed.");
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005168 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005169 drbd_resync_finished(mdev);
5170 }
5171 }
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005172 put_ldev(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005173 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005174}
5175
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005176static int got_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner0ced55a2010-04-30 15:26:20 +02005177{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005178 return 0;
Philipp Reisner0ced55a2010-04-30 15:26:20 +02005179}
5180
Andreas Gruenbachera990be42011-04-06 17:56:48 +02005181static int tconn_finish_peer_reqs(struct drbd_tconn *tconn)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005182{
Philipp Reisner082a3432011-03-15 16:05:42 +01005183 struct drbd_conf *mdev;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005184 int vnr, not_empty = 0;
Philipp Reisner32862ec2011-02-08 16:41:01 +01005185
5186 do {
5187 clear_bit(SIGNAL_ASENDER, &tconn->flags);
5188 flush_signals(current);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005189
5190 rcu_read_lock();
5191 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
5192 kref_get(&mdev->kref);
5193 rcu_read_unlock();
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07005194 if (drbd_finish_peer_reqs(mdev)) {
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005195 kref_put(&mdev->kref, &drbd_minor_destroy);
5196 return 1;
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07005197 }
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005198 kref_put(&mdev->kref, &drbd_minor_destroy);
5199 rcu_read_lock();
Philipp Reisner082a3432011-03-15 16:05:42 +01005200 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005201 set_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisner082a3432011-03-15 16:05:42 +01005202
5203 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005204 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisner082a3432011-03-15 16:05:42 +01005205 not_empty = !list_empty(&mdev->done_ee);
5206 if (not_empty)
5207 break;
5208 }
5209 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005210 rcu_read_unlock();
Philipp Reisner32862ec2011-02-08 16:41:01 +01005211 } while (not_empty);
5212
5213 return 0;
5214}
5215
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005216struct asender_cmd {
5217 size_t pkt_size;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005218 int (*fn)(struct drbd_tconn *tconn, struct packet_info *);
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005219};
5220
5221static struct asender_cmd asender_tbl[] = {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005222 [P_PING] = { 0, got_Ping },
5223 [P_PING_ACK] = { 0, got_PingAck },
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005224 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5225 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5226 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5227 [P_DISCARD_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
5228 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
5229 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
5230 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply },
5231 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
5232 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
5233 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
5234 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
5235 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
5236 [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply },
5237 [P_CONN_ST_CHG_REPLY]={ sizeof(struct p_req_state_reply), got_conn_RqSReply },
5238 [P_RETRY_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005239};
5240
Philipp Reisnerb411b362009-09-25 16:07:19 -07005241int drbd_asender(struct drbd_thread *thi)
5242{
Philipp Reisner392c8802011-02-09 10:33:31 +01005243 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005244 struct asender_cmd *cmd = NULL;
Philipp Reisner77351055b2011-02-07 17:24:26 +01005245 struct packet_info pi;
Philipp Reisner257d0af2011-01-26 12:15:29 +01005246 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005247 void *buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005248 int received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005249 unsigned int header_size = drbd_header_size(tconn);
5250 int expect = header_size;
Philipp Reisner44ed1672011-04-19 17:10:19 +02005251 bool ping_timeout_active = false;
5252 struct net_conf *nc;
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005253 int ping_timeo, tcp_cork, ping_int;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005254
Philipp Reisnerb411b362009-09-25 16:07:19 -07005255 current->policy = SCHED_RR; /* Make this a realtime task! */
5256 current->rt_priority = 2; /* more important than all other tasks */
5257
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01005258 while (get_t_state(thi) == RUNNING) {
Philipp Reisner80822282011-02-08 12:46:30 +01005259 drbd_thread_current_set_cpu(thi);
Philipp Reisner44ed1672011-04-19 17:10:19 +02005260
5261 rcu_read_lock();
5262 nc = rcu_dereference(tconn->net_conf);
5263 ping_timeo = nc->ping_timeo;
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005264 tcp_cork = nc->tcp_cork;
Philipp Reisner44ed1672011-04-19 17:10:19 +02005265 ping_int = nc->ping_int;
5266 rcu_read_unlock();
5267
Philipp Reisner32862ec2011-02-08 16:41:01 +01005268 if (test_and_clear_bit(SEND_PING, &tconn->flags)) {
Andreas Gruenbachera17647a2011-04-01 12:49:42 +02005269 if (drbd_send_ping(tconn)) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005270 conn_err(tconn, "drbd_send_ping has failed\n");
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01005271 goto reconnect;
5272 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02005273 tconn->meta.socket->sk->sk_rcvtimeo = ping_timeo * HZ / 10;
5274 ping_timeout_active = true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005275 }
5276
Philipp Reisner32862ec2011-02-08 16:41:01 +01005277 /* TODO: conditionally cork; it may hurt latency if we cork without
5278 much to send */
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005279 if (tcp_cork)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005280 drbd_tcp_cork(tconn->meta.socket);
Andreas Gruenbachera990be42011-04-06 17:56:48 +02005281 if (tconn_finish_peer_reqs(tconn)) {
5282 conn_err(tconn, "tconn_finish_peer_reqs() failed\n");
Philipp Reisner32862ec2011-02-08 16:41:01 +01005283 goto reconnect;
Philipp Reisner082a3432011-03-15 16:05:42 +01005284 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005285 /* but unconditionally uncork unless disabled */
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005286 if (tcp_cork)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005287 drbd_tcp_uncork(tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005288
5289 /* short circuit, recv_msg would return EINTR anyways. */
5290 if (signal_pending(current))
5291 continue;
5292
Philipp Reisner32862ec2011-02-08 16:41:01 +01005293 rv = drbd_recv_short(tconn->meta.socket, buf, expect-received, 0);
5294 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005295
5296 flush_signals(current);
5297
5298 /* Note:
5299 * -EINTR (on meta) we got a signal
5300 * -EAGAIN (on meta) rcvtimeo expired
5301 * -ECONNRESET other side closed the connection
5302 * -ERESTARTSYS (on data) we got a signal
5303 * rv < 0 other than above: unexpected error!
5304 * rv == expected: full header or command
5305 * rv < expected: "woken" by signal during receive
5306 * rv == 0 : "connection shut down by peer"
5307 */
5308 if (likely(rv > 0)) {
5309 received += rv;
5310 buf += rv;
5311 } else if (rv == 0) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005312 conn_err(tconn, "meta connection shut down by peer.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005313 goto reconnect;
5314 } else if (rv == -EAGAIN) {
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02005315 /* If the data socket received something meanwhile,
5316 * that is good enough: peer is still alive. */
Philipp Reisner32862ec2011-02-08 16:41:01 +01005317 if (time_after(tconn->last_received,
5318 jiffies - tconn->meta.socket->sk->sk_rcvtimeo))
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02005319 continue;
Lars Ellenbergf36af182011-03-09 22:44:55 +01005320 if (ping_timeout_active) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005321 conn_err(tconn, "PingAck did not arrive in time.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005322 goto reconnect;
5323 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005324 set_bit(SEND_PING, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005325 continue;
5326 } else if (rv == -EINTR) {
5327 continue;
5328 } else {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005329 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005330 goto reconnect;
5331 }
5332
5333 if (received == expect && cmd == NULL) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005334 if (decode_header(tconn, tconn->meta.rbuf, &pi))
Philipp Reisnerb411b362009-09-25 16:07:19 -07005335 goto reconnect;
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005336 cmd = &asender_tbl[pi.cmd];
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005337 if (pi.cmd >= ARRAY_SIZE(asender_tbl) || !cmd->fn) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02005338 conn_err(tconn, "Unexpected meta packet %s (0x%04x)\n",
5339 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005340 goto disconnect;
5341 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005342 expect = header_size + cmd->pkt_size;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005343 if (pi.size != expect - header_size) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005344 conn_err(tconn, "Wrong packet size on meta (c: %d, l: %d)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01005345 pi.cmd, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005346 goto reconnect;
Philipp Reisner257d0af2011-01-26 12:15:29 +01005347 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005348 }
5349 if (received == expect) {
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005350 bool err;
Philipp Reisnera4fbda82011-03-16 11:13:17 +01005351
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005352 err = cmd->fn(tconn, &pi);
5353 if (err) {
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005354 conn_err(tconn, "%pf failed\n", cmd->fn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005355 goto reconnect;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005356 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005357
Philipp Reisnera4fbda82011-03-16 11:13:17 +01005358 tconn->last_received = jiffies;
5359
Philipp Reisner44ed1672011-04-19 17:10:19 +02005360 if (cmd == &asender_tbl[P_PING_ACK]) {
5361 /* restore idle timeout */
5362 tconn->meta.socket->sk->sk_rcvtimeo = ping_int * HZ;
5363 ping_timeout_active = false;
5364 }
Lars Ellenbergf36af182011-03-09 22:44:55 +01005365
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005366 buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005367 received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005368 expect = header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005369 cmd = NULL;
5370 }
5371 }
5372
5373 if (0) {
5374reconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01005375 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005376 }
5377 if (0) {
5378disconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01005379 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005380 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005381 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005382
Philipp Reisner32862ec2011-02-08 16:41:01 +01005383 conn_info(tconn, "asender terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005384
5385 return 0;
5386}