blob: 4ba530cf810f24417ccf9b8bee4fc1957319d153 [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:
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +0200428 * e_end_block, and e_end_resync_block, e_send_superseded.
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);
Philipp Reisner155522d2012-08-08 21:19:09 +0200507 rv = sock_recvmsg(tconn->data.socket, &msg, size, msg.msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700508 set_fs(oldfs);
509
Philipp Reisner155522d2012-08-08 21:19:09 +0200510 if (rv < 0) {
511 if (rv == -ECONNRESET)
512 conn_info(tconn, "sock was reset by peer\n");
513 else if (rv != -ERESTARTSYS)
514 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
515 } else if (rv == 0) {
Philipp Reisnerb66623e2012-08-08 21:19:09 +0200516 if (test_bit(DISCONNECT_SENT, &tconn->flags)) {
517 long t;
518 rcu_read_lock();
519 t = rcu_dereference(tconn->net_conf)->ping_timeo * HZ/10;
520 rcu_read_unlock();
521
522 t = wait_event_timeout(tconn->ping_wait, tconn->cstate < C_WF_REPORT_PARAMS, t);
523
524 if (t)
525 goto out;
526 }
527 conn_info(tconn, "sock was shut down by peer\n");
528 }
529
Philipp Reisnerb411b362009-09-25 16:07:19 -0700530 if (rv != size)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100531 conn_request_state(tconn, NS(conn, C_BROKEN_PIPE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700532
Philipp Reisnerb66623e2012-08-08 21:19:09 +0200533out:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700534 return rv;
535}
536
Andreas Gruenbacherc6967742011-03-17 17:15:20 +0100537static int drbd_recv_all(struct drbd_tconn *tconn, void *buf, size_t size)
538{
539 int err;
540
541 err = drbd_recv(tconn, buf, size);
542 if (err != size) {
543 if (err >= 0)
544 err = -EIO;
545 } else
546 err = 0;
547 return err;
548}
549
Andreas Gruenbachera5c31902011-03-24 03:28:04 +0100550static int drbd_recv_all_warn(struct drbd_tconn *tconn, void *buf, size_t size)
551{
552 int err;
553
554 err = drbd_recv_all(tconn, buf, size);
555 if (err && !signal_pending(current))
556 conn_warn(tconn, "short read (expected size %d)\n", (int)size);
557 return err;
558}
559
Lars Ellenberg5dbf1672010-05-25 16:18:01 +0200560/* quoting tcp(7):
561 * On individual connections, the socket buffer size must be set prior to the
562 * listen(2) or connect(2) calls in order to have it take effect.
563 * This is our wrapper to do so.
564 */
565static void drbd_setbufsize(struct socket *sock, unsigned int snd,
566 unsigned int rcv)
567{
568 /* open coded SO_SNDBUF, SO_RCVBUF */
569 if (snd) {
570 sock->sk->sk_sndbuf = snd;
571 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
572 }
573 if (rcv) {
574 sock->sk->sk_rcvbuf = rcv;
575 sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
576 }
577}
578
Philipp Reisnereac3e992011-02-07 14:05:07 +0100579static struct socket *drbd_try_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700580{
581 const char *what;
582 struct socket *sock;
583 struct sockaddr_in6 src_in6;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200584 struct sockaddr_in6 peer_in6;
585 struct net_conf *nc;
586 int err, peer_addr_len, my_addr_len;
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200587 int sndbuf_size, rcvbuf_size, connect_int;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700588 int disconnect_on_error = 1;
589
Philipp Reisner44ed1672011-04-19 17:10:19 +0200590 rcu_read_lock();
591 nc = rcu_dereference(tconn->net_conf);
592 if (!nc) {
593 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700594 return NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200595 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200596 sndbuf_size = nc->sndbuf_size;
597 rcvbuf_size = nc->rcvbuf_size;
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200598 connect_int = nc->connect_int;
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200599 rcu_read_unlock();
Philipp Reisner44ed1672011-04-19 17:10:19 +0200600
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200601 my_addr_len = min_t(int, tconn->my_addr_len, sizeof(src_in6));
602 memcpy(&src_in6, &tconn->my_addr, my_addr_len);
Philipp Reisner44ed1672011-04-19 17:10:19 +0200603
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200604 if (((struct sockaddr *)&tconn->my_addr)->sa_family == AF_INET6)
Philipp Reisner44ed1672011-04-19 17:10:19 +0200605 src_in6.sin6_port = 0;
606 else
607 ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */
608
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200609 peer_addr_len = min_t(int, tconn->peer_addr_len, sizeof(src_in6));
610 memcpy(&peer_in6, &tconn->peer_addr, peer_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700611
612 what = "sock_create_kern";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200613 err = sock_create_kern(((struct sockaddr *)&src_in6)->sa_family,
614 SOCK_STREAM, IPPROTO_TCP, &sock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700615 if (err < 0) {
616 sock = NULL;
617 goto out;
618 }
619
620 sock->sk->sk_rcvtimeo =
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200621 sock->sk->sk_sndtimeo = connect_int * HZ;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200622 drbd_setbufsize(sock, sndbuf_size, rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700623
624 /* explicitly bind to the configured IP as source IP
625 * for the outgoing connections.
626 * This is needed for multihomed hosts and to be
627 * able to use lo: interfaces for drbd.
628 * Make sure to use 0 as port number, so linux selects
629 * a free one dynamically.
630 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700631 what = "bind before connect";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200632 err = sock->ops->bind(sock, (struct sockaddr *) &src_in6, my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700633 if (err < 0)
634 goto out;
635
636 /* connect may fail, peer not yet available.
637 * stay C_WF_CONNECTION, don't go Disconnecting! */
638 disconnect_on_error = 0;
639 what = "connect";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200640 err = sock->ops->connect(sock, (struct sockaddr *) &peer_in6, peer_addr_len, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700641
642out:
643 if (err < 0) {
644 if (sock) {
645 sock_release(sock);
646 sock = NULL;
647 }
648 switch (-err) {
649 /* timeout, busy, signal pending */
650 case ETIMEDOUT: case EAGAIN: case EINPROGRESS:
651 case EINTR: case ERESTARTSYS:
652 /* peer not (yet) available, network problem */
653 case ECONNREFUSED: case ENETUNREACH:
654 case EHOSTDOWN: case EHOSTUNREACH:
655 disconnect_on_error = 0;
656 break;
657 default:
Philipp Reisnereac3e992011-02-07 14:05:07 +0100658 conn_err(tconn, "%s failed, err = %d\n", what, err);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700659 }
660 if (disconnect_on_error)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100661 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700662 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200663
Philipp Reisnerb411b362009-09-25 16:07:19 -0700664 return sock;
665}
666
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200667struct accept_wait_data {
668 struct drbd_tconn *tconn;
669 struct socket *s_listen;
670 struct completion door_bell;
671 void (*original_sk_state_change)(struct sock *sk);
672
673};
674
Andreas Gruenbacher715306f2012-08-10 17:00:30 +0200675static void drbd_incoming_connection(struct sock *sk)
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200676{
677 struct accept_wait_data *ad = sk->sk_user_data;
Andreas Gruenbacher715306f2012-08-10 17:00:30 +0200678 void (*state_change)(struct sock *sk);
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200679
Andreas Gruenbacher715306f2012-08-10 17:00:30 +0200680 state_change = ad->original_sk_state_change;
681 if (sk->sk_state == TCP_ESTABLISHED)
682 complete(&ad->door_bell);
683 state_change(sk);
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200684}
685
686static int prepare_listen_socket(struct drbd_tconn *tconn, struct accept_wait_data *ad)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700687{
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200688 int err, sndbuf_size, rcvbuf_size, my_addr_len;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200689 struct sockaddr_in6 my_addr;
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200690 struct socket *s_listen;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200691 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700692 const char *what;
693
Philipp Reisner44ed1672011-04-19 17:10:19 +0200694 rcu_read_lock();
695 nc = rcu_dereference(tconn->net_conf);
696 if (!nc) {
697 rcu_read_unlock();
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200698 return -EIO;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200699 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200700 sndbuf_size = nc->sndbuf_size;
701 rcvbuf_size = nc->rcvbuf_size;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200702 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700703
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200704 my_addr_len = min_t(int, tconn->my_addr_len, sizeof(struct sockaddr_in6));
705 memcpy(&my_addr, &tconn->my_addr, my_addr_len);
706
Philipp Reisnerb411b362009-09-25 16:07:19 -0700707 what = "sock_create_kern";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200708 err = sock_create_kern(((struct sockaddr *)&my_addr)->sa_family,
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200709 SOCK_STREAM, IPPROTO_TCP, &s_listen);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700710 if (err) {
711 s_listen = NULL;
712 goto out;
713 }
714
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200715 s_listen->sk->sk_reuse = 1; /* SO_REUSEADDR */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200716 drbd_setbufsize(s_listen, sndbuf_size, rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700717
718 what = "bind before listen";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200719 err = s_listen->ops->bind(s_listen, (struct sockaddr *)&my_addr, my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700720 if (err < 0)
721 goto out;
722
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200723 ad->s_listen = s_listen;
724 write_lock_bh(&s_listen->sk->sk_callback_lock);
725 ad->original_sk_state_change = s_listen->sk->sk_state_change;
Andreas Gruenbacher715306f2012-08-10 17:00:30 +0200726 s_listen->sk->sk_state_change = drbd_incoming_connection;
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200727 s_listen->sk->sk_user_data = ad;
728 write_unlock_bh(&s_listen->sk->sk_callback_lock);
729
Philipp Reisner2820fd32012-07-12 10:22:48 +0200730 what = "listen";
731 err = s_listen->ops->listen(s_listen, 5);
732 if (err < 0)
733 goto out;
734
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200735 return 0;
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200736out:
737 if (s_listen)
738 sock_release(s_listen);
739 if (err < 0) {
740 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
741 conn_err(tconn, "%s failed, err = %d\n", what, err);
742 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
743 }
744 }
745
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200746 return -EIO;
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200747}
748
Andreas Gruenbacher715306f2012-08-10 17:00:30 +0200749static void unregister_state_change(struct sock *sk, struct accept_wait_data *ad)
750{
751 write_lock_bh(&sk->sk_callback_lock);
752 sk->sk_state_change = ad->original_sk_state_change;
753 sk->sk_user_data = NULL;
754 write_unlock_bh(&sk->sk_callback_lock);
755}
756
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200757static struct socket *drbd_wait_for_connect(struct drbd_tconn *tconn, struct accept_wait_data *ad)
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200758{
759 int timeo, connect_int, err = 0;
760 struct socket *s_estab = NULL;
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200761 struct net_conf *nc;
762
763 rcu_read_lock();
764 nc = rcu_dereference(tconn->net_conf);
765 if (!nc) {
766 rcu_read_unlock();
767 return NULL;
768 }
769 connect_int = nc->connect_int;
770 rcu_read_unlock();
771
772 timeo = connect_int * HZ;
773 timeo += (random32() & 1) ? timeo / 7 : -timeo / 7; /* 28.5% random jitter */
774
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200775 err = wait_for_completion_interruptible_timeout(&ad->door_bell, timeo);
776 if (err <= 0)
777 return NULL;
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200778
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200779 err = kernel_accept(ad->s_listen, &s_estab, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700780 if (err < 0) {
781 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200782 conn_err(tconn, "accept failed, err = %d\n", err);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100783 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700784 }
785 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700786
Andreas Gruenbacher715306f2012-08-10 17:00:30 +0200787 if (s_estab)
788 unregister_state_change(s_estab->sk, ad);
789
Philipp Reisnerb411b362009-09-25 16:07:19 -0700790 return s_estab;
791}
792
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200793static int decode_header(struct drbd_tconn *, void *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700794
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200795static int send_first_packet(struct drbd_tconn *tconn, struct drbd_socket *sock,
796 enum drbd_packet cmd)
797{
798 if (!conn_prepare_command(tconn, sock))
799 return -EIO;
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200800 return conn_send_command(tconn, sock, cmd, 0, NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700801}
802
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200803static int receive_first_packet(struct drbd_tconn *tconn, struct socket *sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700804{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200805 unsigned int header_size = drbd_header_size(tconn);
806 struct packet_info pi;
807 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700808
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200809 err = drbd_recv_short(sock, tconn->data.rbuf, header_size, 0);
810 if (err != header_size) {
811 if (err >= 0)
812 err = -EIO;
813 return err;
814 }
815 err = decode_header(tconn, tconn->data.rbuf, &pi);
816 if (err)
817 return err;
818 return pi.cmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700819}
820
821/**
822 * drbd_socket_okay() - Free the socket if its connection is not okay
Philipp Reisnerb411b362009-09-25 16:07:19 -0700823 * @sock: pointer to the pointer to the socket.
824 */
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100825static int drbd_socket_okay(struct socket **sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700826{
827 int rr;
828 char tb[4];
829
830 if (!*sock)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100831 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700832
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100833 rr = drbd_recv_short(*sock, tb, 4, MSG_DONTWAIT | MSG_PEEK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700834
835 if (rr > 0 || rr == -EAGAIN) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100836 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700837 } else {
838 sock_release(*sock);
839 *sock = NULL;
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100840 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700841 }
842}
Philipp Reisner2325eb62011-03-15 16:56:18 +0100843/* Gets called if a connection is established, or if a new minor gets created
844 in a connection */
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200845int drbd_connected(struct drbd_conf *mdev)
Philipp Reisner907599e2011-02-08 11:25:37 +0100846{
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100847 int err;
Philipp Reisner907599e2011-02-08 11:25:37 +0100848
849 atomic_set(&mdev->packet_seq, 0);
850 mdev->peer_seq = 0;
851
Philipp Reisner8410da82011-02-11 20:11:10 +0100852 mdev->state_mutex = mdev->tconn->agreed_pro_version < 100 ?
853 &mdev->tconn->cstate_mutex :
854 &mdev->own_state_mutex;
855
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100856 err = drbd_send_sync_param(mdev);
857 if (!err)
858 err = drbd_send_sizes(mdev, 0, 0);
859 if (!err)
860 err = drbd_send_uuids(mdev);
861 if (!err)
Philipp Reisner43de7c82011-11-10 13:16:13 +0100862 err = drbd_send_current_state(mdev);
Philipp Reisner907599e2011-02-08 11:25:37 +0100863 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
864 clear_bit(RESIZE_PENDING, &mdev->flags);
Philipp Reisner8b924f12011-03-01 11:08:28 +0100865 mod_timer(&mdev->request_timer, jiffies + HZ); /* just start it here. */
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100866 return err;
Philipp Reisner907599e2011-02-08 11:25:37 +0100867}
868
Philipp Reisnerb411b362009-09-25 16:07:19 -0700869/*
870 * return values:
871 * 1 yes, we have a valid connection
872 * 0 oops, did not work out, please try again
873 * -1 peer talks different language,
874 * no point in trying again, please go standalone.
875 * -2 We do not have a network config...
876 */
Philipp Reisner81fa2e62011-05-04 15:10:30 +0200877static int conn_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700878{
Philipp Reisner7da35862011-12-19 22:42:56 +0100879 struct drbd_socket sock, msock;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200880 struct drbd_conf *mdev;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200881 struct net_conf *nc;
Philipp Reisner92f14952012-08-01 11:41:01 +0200882 int vnr, timeout, h, ok;
Philipp Reisner08b165b2011-09-05 16:22:33 +0200883 bool discard_my_data;
Philipp Reisnera1096a62012-04-06 12:07:34 +0200884 enum drbd_state_rv rv;
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200885 struct accept_wait_data ad = {
886 .tconn = tconn,
887 .door_bell = COMPLETION_INITIALIZER_ONSTACK(ad.door_bell),
888 };
Philipp Reisnerb411b362009-09-25 16:07:19 -0700889
Philipp Reisnerb66623e2012-08-08 21:19:09 +0200890 clear_bit(DISCONNECT_SENT, &tconn->flags);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100891 if (conn_request_state(tconn, NS(conn, C_WF_CONNECTION), CS_VERBOSE) < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700892 return -2;
893
Philipp Reisner7da35862011-12-19 22:42:56 +0100894 mutex_init(&sock.mutex);
895 sock.sbuf = tconn->data.sbuf;
896 sock.rbuf = tconn->data.rbuf;
897 sock.socket = NULL;
898 mutex_init(&msock.mutex);
899 msock.sbuf = tconn->meta.sbuf;
900 msock.rbuf = tconn->meta.rbuf;
901 msock.socket = NULL;
902
Andreas Gruenbacher0916e0e2011-03-21 14:10:15 +0100903 /* Assume that the peer only understands protocol 80 until we know better. */
904 tconn->agreed_pro_version = 80;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700905
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200906 if (prepare_listen_socket(tconn, &ad))
907 return 0;
908
Philipp Reisnerb411b362009-09-25 16:07:19 -0700909 do {
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200910 struct socket *s;
911
Philipp Reisner92f14952012-08-01 11:41:01 +0200912 s = drbd_try_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700913 if (s) {
Philipp Reisner7da35862011-12-19 22:42:56 +0100914 if (!sock.socket) {
915 sock.socket = s;
916 send_first_packet(tconn, &sock, P_INITIAL_DATA);
917 } else if (!msock.socket) {
Lars Ellenberg427c0432012-08-01 12:43:01 +0200918 clear_bit(RESOLVE_CONFLICTS, &tconn->flags);
Philipp Reisner7da35862011-12-19 22:42:56 +0100919 msock.socket = s;
920 send_first_packet(tconn, &msock, P_INITIAL_META);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700921 } else {
Philipp Reisner81fa2e62011-05-04 15:10:30 +0200922 conn_err(tconn, "Logic error in conn_connect()\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700923 goto out_release_sockets;
924 }
925 }
926
Philipp Reisner7da35862011-12-19 22:42:56 +0100927 if (sock.socket && msock.socket) {
928 rcu_read_lock();
929 nc = rcu_dereference(tconn->net_conf);
930 timeout = nc->ping_timeo * HZ / 10;
931 rcu_read_unlock();
932 schedule_timeout_interruptible(timeout);
933 ok = drbd_socket_okay(&sock.socket);
934 ok = drbd_socket_okay(&msock.socket) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700935 if (ok)
936 break;
937 }
938
939retry:
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200940 s = drbd_wait_for_connect(tconn, &ad);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700941 if (s) {
Philipp Reisner92f14952012-08-01 11:41:01 +0200942 int fp = receive_first_packet(tconn, s);
Philipp Reisner7da35862011-12-19 22:42:56 +0100943 drbd_socket_okay(&sock.socket);
944 drbd_socket_okay(&msock.socket);
Philipp Reisner92f14952012-08-01 11:41:01 +0200945 switch (fp) {
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200946 case P_INITIAL_DATA:
Philipp Reisner7da35862011-12-19 22:42:56 +0100947 if (sock.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100948 conn_warn(tconn, "initial packet S crossed\n");
Philipp Reisner7da35862011-12-19 22:42:56 +0100949 sock_release(sock.socket);
Philipp Reisner80c6eed2012-08-01 14:53:39 +0200950 sock.socket = s;
951 goto randomize;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700952 }
Philipp Reisner7da35862011-12-19 22:42:56 +0100953 sock.socket = s;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700954 break;
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200955 case P_INITIAL_META:
Lars Ellenberg427c0432012-08-01 12:43:01 +0200956 set_bit(RESOLVE_CONFLICTS, &tconn->flags);
Philipp Reisner7da35862011-12-19 22:42:56 +0100957 if (msock.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100958 conn_warn(tconn, "initial packet M crossed\n");
Philipp Reisner7da35862011-12-19 22:42:56 +0100959 sock_release(msock.socket);
Philipp Reisner80c6eed2012-08-01 14:53:39 +0200960 msock.socket = s;
961 goto randomize;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700962 }
Philipp Reisner7da35862011-12-19 22:42:56 +0100963 msock.socket = s;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700964 break;
965 default:
Philipp Reisner907599e2011-02-08 11:25:37 +0100966 conn_warn(tconn, "Error receiving initial packet\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700967 sock_release(s);
Philipp Reisner80c6eed2012-08-01 14:53:39 +0200968randomize:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700969 if (random32() & 1)
970 goto retry;
971 }
972 }
973
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100974 if (tconn->cstate <= C_DISCONNECTING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700975 goto out_release_sockets;
976 if (signal_pending(current)) {
977 flush_signals(current);
978 smp_rmb();
Philipp Reisner907599e2011-02-08 11:25:37 +0100979 if (get_t_state(&tconn->receiver) == EXITING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700980 goto out_release_sockets;
981 }
982
Philipp Reisnerb666dbf2012-07-26 14:12:59 +0200983 ok = drbd_socket_okay(&sock.socket);
984 ok = drbd_socket_okay(&msock.socket) && ok;
985 } while (!ok);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700986
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200987 if (ad.s_listen)
988 sock_release(ad.s_listen);
989
Philipp Reisner7da35862011-12-19 22:42:56 +0100990 sock.socket->sk->sk_reuse = 1; /* SO_REUSEADDR */
991 msock.socket->sk->sk_reuse = 1; /* SO_REUSEADDR */
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200992
Philipp Reisner7da35862011-12-19 22:42:56 +0100993 sock.socket->sk->sk_allocation = GFP_NOIO;
994 msock.socket->sk->sk_allocation = GFP_NOIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700995
Philipp Reisner7da35862011-12-19 22:42:56 +0100996 sock.socket->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
997 msock.socket->sk->sk_priority = TC_PRIO_INTERACTIVE;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700998
Philipp Reisnerb411b362009-09-25 16:07:19 -0700999 /* NOT YET ...
Philipp Reisner7da35862011-12-19 22:42:56 +01001000 * sock.socket->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
1001 * sock.socket->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
Andreas Gruenbacher60381782011-03-28 17:05:50 +02001002 * first set it to the P_CONNECTION_FEATURES timeout,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001003 * which we set to 4x the configured ping_timeout. */
Philipp Reisner44ed1672011-04-19 17:10:19 +02001004 rcu_read_lock();
1005 nc = rcu_dereference(tconn->net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001006
Philipp Reisner7da35862011-12-19 22:42:56 +01001007 sock.socket->sk->sk_sndtimeo =
1008 sock.socket->sk->sk_rcvtimeo = nc->ping_timeo*4*HZ/10;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001009
Philipp Reisner7da35862011-12-19 22:42:56 +01001010 msock.socket->sk->sk_rcvtimeo = nc->ping_int*HZ;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001011 timeout = nc->timeout * HZ / 10;
Philipp Reisner08b165b2011-09-05 16:22:33 +02001012 discard_my_data = nc->discard_my_data;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001013 rcu_read_unlock();
1014
Philipp Reisner7da35862011-12-19 22:42:56 +01001015 msock.socket->sk->sk_sndtimeo = timeout;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001016
1017 /* we don't want delays.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001018 * we use TCP_CORK where appropriate, though */
Philipp Reisner7da35862011-12-19 22:42:56 +01001019 drbd_tcp_nodelay(sock.socket);
1020 drbd_tcp_nodelay(msock.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001021
Philipp Reisner7da35862011-12-19 22:42:56 +01001022 tconn->data.socket = sock.socket;
1023 tconn->meta.socket = msock.socket;
Philipp Reisner907599e2011-02-08 11:25:37 +01001024 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001025
Andreas Gruenbacher60381782011-03-28 17:05:50 +02001026 h = drbd_do_features(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001027 if (h <= 0)
1028 return h;
1029
Philipp Reisner907599e2011-02-08 11:25:37 +01001030 if (tconn->cram_hmac_tfm) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001031 /* drbd_request_state(mdev, NS(conn, WFAuth)); */
Philipp Reisner907599e2011-02-08 11:25:37 +01001032 switch (drbd_do_auth(tconn)) {
Johannes Thomab10d96c2010-01-07 16:02:50 +01001033 case -1:
Philipp Reisner907599e2011-02-08 11:25:37 +01001034 conn_err(tconn, "Authentication of peer failed\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001035 return -1;
Johannes Thomab10d96c2010-01-07 16:02:50 +01001036 case 0:
Philipp Reisner907599e2011-02-08 11:25:37 +01001037 conn_err(tconn, "Authentication of peer failed, trying again.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01001038 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001039 }
1040 }
1041
Philipp Reisner7da35862011-12-19 22:42:56 +01001042 tconn->data.socket->sk->sk_sndtimeo = timeout;
1043 tconn->data.socket->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001044
Andreas Gruenbacher387eb302011-03-16 01:05:37 +01001045 if (drbd_send_protocol(tconn) == -EOPNOTSUPP)
Philipp Reisner7e2455c2010-04-22 14:50:23 +02001046 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001047
Philipp Reisnera1096a62012-04-06 12:07:34 +02001048 set_bit(STATE_SENT, &tconn->flags);
1049
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001050 rcu_read_lock();
1051 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1052 kref_get(&mdev->kref);
1053 rcu_read_unlock();
Philipp Reisner08b165b2011-09-05 16:22:33 +02001054
1055 if (discard_my_data)
1056 set_bit(DISCARD_MY_DATA, &mdev->flags);
1057 else
1058 clear_bit(DISCARD_MY_DATA, &mdev->flags);
1059
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001060 drbd_connected(mdev);
1061 kref_put(&mdev->kref, &drbd_minor_destroy);
1062 rcu_read_lock();
1063 }
1064 rcu_read_unlock();
1065
Philipp Reisnera1096a62012-04-06 12:07:34 +02001066 rv = conn_request_state(tconn, NS(conn, C_WF_REPORT_PARAMS), CS_VERBOSE);
1067 if (rv < SS_SUCCESS) {
1068 clear_bit(STATE_SENT, &tconn->flags);
Philipp Reisner823bd832012-11-08 15:04:36 +01001069 return 0;
Philipp Reisnera1096a62012-04-06 12:07:34 +02001070 }
Philipp Reisner823bd832012-11-08 15:04:36 +01001071
1072 drbd_thread_start(&tconn->asender);
1073
Philipp Reisner08b165b2011-09-05 16:22:33 +02001074 mutex_lock(&tconn->conf_update);
1075 /* The discard_my_data flag is a single-shot modifier to the next
1076 * connection attempt, the handshake of which is now well underway.
1077 * No need for rcu style copying of the whole struct
1078 * just to clear a single value. */
1079 tconn->net_conf->discard_my_data = 0;
1080 mutex_unlock(&tconn->conf_update);
1081
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07001082 return h;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001083
1084out_release_sockets:
Philipp Reisner7a426fd2012-07-12 14:22:37 +02001085 if (ad.s_listen)
1086 sock_release(ad.s_listen);
Philipp Reisner7da35862011-12-19 22:42:56 +01001087 if (sock.socket)
1088 sock_release(sock.socket);
1089 if (msock.socket)
1090 sock_release(msock.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001091 return -1;
1092}
1093
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001094static int decode_header(struct drbd_tconn *tconn, void *header, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001095{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001096 unsigned int header_size = drbd_header_size(tconn);
1097
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +02001098 if (header_size == sizeof(struct p_header100) &&
1099 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC_100)) {
1100 struct p_header100 *h = header;
1101 if (h->pad != 0) {
1102 conn_err(tconn, "Header padding is not zero\n");
1103 return -EINVAL;
1104 }
1105 pi->vnr = be16_to_cpu(h->volume);
1106 pi->cmd = be16_to_cpu(h->command);
1107 pi->size = be32_to_cpu(h->length);
1108 } else if (header_size == sizeof(struct p_header95) &&
1109 *(__be16 *)header == cpu_to_be16(DRBD_MAGIC_BIG)) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001110 struct p_header95 *h = header;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001111 pi->cmd = be16_to_cpu(h->command);
Andreas Gruenbacherb55d84b2011-03-22 13:17:47 +01001112 pi->size = be32_to_cpu(h->length);
1113 pi->vnr = 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001114 } else if (header_size == sizeof(struct p_header80) &&
1115 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC)) {
1116 struct p_header80 *h = header;
1117 pi->cmd = be16_to_cpu(h->command);
1118 pi->size = be16_to_cpu(h->length);
Philipp Reisner77351055b2011-02-07 17:24:26 +01001119 pi->vnr = 0;
Philipp Reisner02918be2010-08-20 14:35:10 +02001120 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001121 conn_err(tconn, "Wrong magic value 0x%08x in protocol version %d\n",
1122 be32_to_cpu(*(__be32 *)header),
1123 tconn->agreed_pro_version);
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001124 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001125 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001126 pi->data = header + header_size;
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001127 return 0;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001128}
1129
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001130static int drbd_recv_header(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner257d0af2011-01-26 12:15:29 +01001131{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001132 void *buffer = tconn->data.rbuf;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001133 int err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001134
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001135 err = drbd_recv_all_warn(tconn, buffer, drbd_header_size(tconn));
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001136 if (err)
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001137 return err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001138
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001139 err = decode_header(tconn, buffer, pi);
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001140 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001141
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001142 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001143}
1144
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001145static void drbd_flush(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001146{
1147 int rv;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001148 struct drbd_conf *mdev;
1149 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001150
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001151 if (tconn->write_ordering >= WO_bdev_flush) {
Lars Ellenberg615e0872011-11-17 14:32:12 +01001152 rcu_read_lock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001153 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Lars Ellenberg615e0872011-11-17 14:32:12 +01001154 if (!get_ldev(mdev))
1155 continue;
1156 kref_get(&mdev->kref);
1157 rcu_read_unlock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001158
Lars Ellenberg615e0872011-11-17 14:32:12 +01001159 rv = blkdev_issue_flush(mdev->ldev->backing_bdev,
1160 GFP_NOIO, NULL);
1161 if (rv) {
1162 dev_info(DEV, "local disk flush failed with status %d\n", rv);
1163 /* would rather check on EOPNOTSUPP, but that is not reliable.
1164 * don't try again for ANY return value != 0
1165 * if (rv == -EOPNOTSUPP) */
1166 drbd_bump_write_ordering(tconn, WO_drain_io);
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001167 }
Lars Ellenberg615e0872011-11-17 14:32:12 +01001168 put_ldev(mdev);
1169 kref_put(&mdev->kref, &drbd_minor_destroy);
1170
1171 rcu_read_lock();
1172 if (rv)
1173 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001174 }
Lars Ellenberg615e0872011-11-17 14:32:12 +01001175 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001176 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001177}
1178
1179/**
1180 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
1181 * @mdev: DRBD device.
1182 * @epoch: Epoch object.
1183 * @ev: Epoch event.
1184 */
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001185static enum finish_epoch drbd_may_finish_epoch(struct drbd_tconn *tconn,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001186 struct drbd_epoch *epoch,
1187 enum epoch_event ev)
1188{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001189 int epoch_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001190 struct drbd_epoch *next_epoch;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001191 enum finish_epoch rv = FE_STILL_LIVE;
1192
Philipp Reisner12038a32011-11-09 19:18:00 +01001193 spin_lock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001194 do {
1195 next_epoch = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001196
1197 epoch_size = atomic_read(&epoch->epoch_size);
1198
1199 switch (ev & ~EV_CLEANUP) {
1200 case EV_PUT:
1201 atomic_dec(&epoch->active);
1202 break;
1203 case EV_GOT_BARRIER_NR:
1204 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001205 break;
1206 case EV_BECAME_LAST:
1207 /* nothing to do*/
1208 break;
1209 }
1210
Philipp Reisnerb411b362009-09-25 16:07:19 -07001211 if (epoch_size != 0 &&
1212 atomic_read(&epoch->active) == 0 &&
Philipp Reisner85d73512011-07-18 15:45:15 +02001213 (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags) || ev & EV_CLEANUP)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001214 if (!(ev & EV_CLEANUP)) {
Philipp Reisner12038a32011-11-09 19:18:00 +01001215 spin_unlock(&tconn->epoch_lock);
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001216 drbd_send_b_ack(epoch->tconn, epoch->barrier_nr, epoch_size);
Philipp Reisner12038a32011-11-09 19:18:00 +01001217 spin_lock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001218 }
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001219#if 0
1220 /* FIXME: dec unacked on connection, once we have
1221 * something to count pending connection packets in. */
Philipp Reisner85d73512011-07-18 15:45:15 +02001222 if (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags))
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001223 dec_unacked(epoch->tconn);
1224#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -07001225
Philipp Reisner12038a32011-11-09 19:18:00 +01001226 if (tconn->current_epoch != epoch) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001227 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1228 list_del(&epoch->list);
1229 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
Philipp Reisner12038a32011-11-09 19:18:00 +01001230 tconn->epochs--;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001231 kfree(epoch);
1232
1233 if (rv == FE_STILL_LIVE)
1234 rv = FE_DESTROYED;
1235 } else {
1236 epoch->flags = 0;
1237 atomic_set(&epoch->epoch_size, 0);
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001238 /* atomic_set(&epoch->active, 0); is already zero */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001239 if (rv == FE_STILL_LIVE)
1240 rv = FE_RECYCLED;
1241 }
1242 }
1243
1244 if (!next_epoch)
1245 break;
1246
1247 epoch = next_epoch;
1248 } while (1);
1249
Philipp Reisner12038a32011-11-09 19:18:00 +01001250 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001251
Philipp Reisnerb411b362009-09-25 16:07:19 -07001252 return rv;
1253}
1254
1255/**
1256 * drbd_bump_write_ordering() - Fall back to an other write ordering method
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001257 * @tconn: DRBD connection.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001258 * @wo: Write ordering method to try.
1259 */
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001260void drbd_bump_write_ordering(struct drbd_tconn *tconn, enum write_ordering_e wo)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001261{
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001262 struct disk_conf *dc;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001263 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001264 enum write_ordering_e pwo;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001265 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001266 static char *write_ordering_str[] = {
1267 [WO_none] = "none",
1268 [WO_drain_io] = "drain",
1269 [WO_bdev_flush] = "flush",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001270 };
1271
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001272 pwo = tconn->write_ordering;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001273 wo = min(pwo, wo);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001274 rcu_read_lock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001275 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisner27eb13e2012-03-30 14:12:15 +02001276 if (!get_ldev_if_state(mdev, D_ATTACHING))
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001277 continue;
1278 dc = rcu_dereference(mdev->ldev->disk_conf);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001279
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001280 if (wo == WO_bdev_flush && !dc->disk_flushes)
1281 wo = WO_drain_io;
1282 if (wo == WO_drain_io && !dc->disk_drain)
1283 wo = WO_none;
1284 put_ldev(mdev);
1285 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001286 rcu_read_unlock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001287 tconn->write_ordering = wo;
1288 if (pwo != tconn->write_ordering || wo == WO_bdev_flush)
1289 conn_info(tconn, "Method to ensure write ordering: %s\n", write_ordering_str[tconn->write_ordering]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001290}
1291
1292/**
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001293 * drbd_submit_peer_request()
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001294 * @mdev: DRBD device.
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001295 * @peer_req: peer request
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001296 * @rw: flag field, see bio->bi_rw
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001297 *
1298 * May spread the pages to multiple bios,
1299 * depending on bio_add_page restrictions.
1300 *
1301 * Returns 0 if all bios have been submitted,
1302 * -ENOMEM if we could not allocate enough bios,
1303 * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a
1304 * single page to an empty bio (which should never happen and likely indicates
1305 * that the lower level IO stack is in some way broken). This has been observed
1306 * on certain Xen deployments.
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001307 */
1308/* TODO allocate from our own bio_set. */
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001309int drbd_submit_peer_request(struct drbd_conf *mdev,
1310 struct drbd_peer_request *peer_req,
1311 const unsigned rw, const int fault_type)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001312{
1313 struct bio *bios = NULL;
1314 struct bio *bio;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001315 struct page *page = peer_req->pages;
1316 sector_t sector = peer_req->i.sector;
1317 unsigned ds = peer_req->i.size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001318 unsigned n_bios = 0;
1319 unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001320 int err = -ENOMEM;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001321
1322 /* In most cases, we will only need one bio. But in case the lower
1323 * level restrictions happen to be different at this offset on this
1324 * side than those of the sending peer, we may need to submit the
Lars Ellenbergda4a75d2011-02-23 17:02:01 +01001325 * request in more than one bio.
1326 *
1327 * Plain bio_alloc is good enough here, this is no DRBD internally
1328 * generated bio, but a bio allocated on behalf of the peer.
1329 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001330next_bio:
1331 bio = bio_alloc(GFP_NOIO, nr_pages);
1332 if (!bio) {
1333 dev_err(DEV, "submit_ee: Allocation of a bio failed\n");
1334 goto fail;
1335 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001336 /* > peer_req->i.sector, unless this is the first bio */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001337 bio->bi_sector = sector;
1338 bio->bi_bdev = mdev->ldev->backing_bdev;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001339 bio->bi_rw = rw;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001340 bio->bi_private = peer_req;
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001341 bio->bi_end_io = drbd_peer_request_endio;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001342
1343 bio->bi_next = bios;
1344 bios = bio;
1345 ++n_bios;
1346
1347 page_chain_for_each(page) {
1348 unsigned len = min_t(unsigned, ds, PAGE_SIZE);
1349 if (!bio_add_page(bio, page, len, 0)) {
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001350 /* A single page must always be possible!
1351 * But in case it fails anyways,
1352 * we deal with it, and complain (below). */
1353 if (bio->bi_vcnt == 0) {
1354 dev_err(DEV,
1355 "bio_add_page failed for len=%u, "
1356 "bi_vcnt=0 (bi_sector=%llu)\n",
1357 len, (unsigned long long)bio->bi_sector);
1358 err = -ENOSPC;
1359 goto fail;
1360 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001361 goto next_bio;
1362 }
1363 ds -= len;
1364 sector += len >> 9;
1365 --nr_pages;
1366 }
1367 D_ASSERT(page == NULL);
1368 D_ASSERT(ds == 0);
1369
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001370 atomic_set(&peer_req->pending_bios, n_bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001371 do {
1372 bio = bios;
1373 bios = bios->bi_next;
1374 bio->bi_next = NULL;
1375
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001376 drbd_generic_make_request(mdev, fault_type, bio);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001377 } while (bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001378 return 0;
1379
1380fail:
1381 while (bios) {
1382 bio = bios;
1383 bios = bios->bi_next;
1384 bio_put(bio);
1385 }
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001386 return err;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001387}
1388
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001389static void drbd_remove_epoch_entry_interval(struct drbd_conf *mdev,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001390 struct drbd_peer_request *peer_req)
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001391{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001392 struct drbd_interval *i = &peer_req->i;
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001393
1394 drbd_remove_interval(&mdev->write_requests, i);
1395 drbd_clear_interval(i);
1396
Andreas Gruenbacher6c852be2011-02-04 15:38:52 +01001397 /* Wake up any processes waiting for this peer request to complete. */
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001398 if (i->waiting)
1399 wake_up(&mdev->misc_wait);
1400}
1401
Philipp Reisner77fede52011-11-10 21:19:11 +01001402void conn_wait_active_ee_empty(struct drbd_tconn *tconn)
1403{
1404 struct drbd_conf *mdev;
1405 int vnr;
1406
1407 rcu_read_lock();
1408 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1409 kref_get(&mdev->kref);
1410 rcu_read_unlock();
1411 drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
1412 kref_put(&mdev->kref, &drbd_minor_destroy);
1413 rcu_read_lock();
1414 }
1415 rcu_read_unlock();
1416}
1417
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001418static int receive_Barrier(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001419{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001420 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001421 struct p_barrier *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001422 struct drbd_epoch *epoch;
1423
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001424 /* FIXME these are unacked on connection,
1425 * not a specific (peer)device.
1426 */
Philipp Reisner12038a32011-11-09 19:18:00 +01001427 tconn->current_epoch->barrier_nr = p->barrier;
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001428 tconn->current_epoch->tconn = tconn;
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001429 rv = drbd_may_finish_epoch(tconn, tconn->current_epoch, EV_GOT_BARRIER_NR);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001430
1431 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
1432 * the activity log, which means it would not be resynced in case the
1433 * R_PRIMARY crashes now.
1434 * Therefore we must send the barrier_ack after the barrier request was
1435 * completed. */
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001436 switch (tconn->write_ordering) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001437 case WO_none:
1438 if (rv == FE_RECYCLED)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001439 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001440
1441 /* receiver context, in the writeout path of the other node.
1442 * avoid potential distributed deadlock */
1443 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1444 if (epoch)
1445 break;
1446 else
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001447 conn_warn(tconn, "Allocation of an epoch failed, slowing down\n");
Philipp Reisner2451fc32010-08-24 13:43:11 +02001448 /* Fall through */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001449
1450 case WO_bdev_flush:
1451 case WO_drain_io:
Philipp Reisner77fede52011-11-10 21:19:11 +01001452 conn_wait_active_ee_empty(tconn);
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001453 drbd_flush(tconn);
Philipp Reisner2451fc32010-08-24 13:43:11 +02001454
Philipp Reisner12038a32011-11-09 19:18:00 +01001455 if (atomic_read(&tconn->current_epoch->epoch_size)) {
Philipp Reisner2451fc32010-08-24 13:43:11 +02001456 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1457 if (epoch)
1458 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001459 }
1460
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001461 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001462 default:
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001463 conn_err(tconn, "Strangeness in tconn->write_ordering %d\n", tconn->write_ordering);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001464 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001465 }
1466
1467 epoch->flags = 0;
1468 atomic_set(&epoch->epoch_size, 0);
1469 atomic_set(&epoch->active, 0);
1470
Philipp Reisner12038a32011-11-09 19:18:00 +01001471 spin_lock(&tconn->epoch_lock);
1472 if (atomic_read(&tconn->current_epoch->epoch_size)) {
1473 list_add(&epoch->list, &tconn->current_epoch->list);
1474 tconn->current_epoch = epoch;
1475 tconn->epochs++;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001476 } else {
1477 /* The current_epoch got recycled while we allocated this one... */
1478 kfree(epoch);
1479 }
Philipp Reisner12038a32011-11-09 19:18:00 +01001480 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001481
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001482 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001483}
1484
1485/* used from receive_RSDataReply (recv_resync_read)
1486 * and from receive_Data */
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001487static struct drbd_peer_request *
1488read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector,
1489 int data_size) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001490{
Lars Ellenberg66660322010-04-06 12:15:04 +02001491 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001492 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001493 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001494 int dgs, ds, err;
Philipp Reisnera0638452011-01-19 14:31:32 +01001495 void *dig_in = mdev->tconn->int_dig_in;
1496 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001497 unsigned long *data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001498
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001499 dgs = 0;
1500 if (mdev->tconn->peer_integrity_tfm) {
1501 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001502 /*
1503 * FIXME: Receive the incoming digest into the receive buffer
1504 * here, together with its struct p_data?
1505 */
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001506 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1507 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001508 return NULL;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001509 data_size -= dgs;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001510 }
1511
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001512 if (!expect(IS_ALIGNED(data_size, 512)))
1513 return NULL;
1514 if (!expect(data_size <= DRBD_MAX_BIO_SIZE))
1515 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001516
Lars Ellenberg66660322010-04-06 12:15:04 +02001517 /* even though we trust out peer,
1518 * we sometimes have to double check. */
1519 if (sector + (data_size>>9) > capacity) {
Lars Ellenbergfdda6542011-01-24 15:11:01 +01001520 dev_err(DEV, "request from peer beyond end of local disk: "
1521 "capacity: %llus < sector: %llus + size: %u\n",
Lars Ellenberg66660322010-04-06 12:15:04 +02001522 (unsigned long long)capacity,
1523 (unsigned long long)sector, data_size);
1524 return NULL;
1525 }
1526
Philipp Reisnerb411b362009-09-25 16:07:19 -07001527 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1528 * "criss-cross" setup, that might cause write-out on some other DRBD,
1529 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02001530 peer_req = drbd_alloc_peer_req(mdev, id, sector, data_size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001531 if (!peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001532 return NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001533
Lars Ellenberg81a35372012-07-30 09:00:54 +02001534 if (!data_size)
1535 return peer_req;
1536
Philipp Reisnerb411b362009-09-25 16:07:19 -07001537 ds = data_size;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001538 page = peer_req->pages;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001539 page_chain_for_each(page) {
1540 unsigned len = min_t(int, ds, PAGE_SIZE);
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001541 data = kmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001542 err = drbd_recv_all_warn(mdev->tconn, data, len);
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +01001543 if (drbd_insert_fault(mdev, DRBD_FAULT_RECEIVE)) {
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001544 dev_err(DEV, "Fault injection: Corrupting data on receive\n");
1545 data[0] = data[0] ^ (unsigned long)-1;
1546 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001547 kunmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001548 if (err) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001549 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001550 return NULL;
1551 }
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001552 ds -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001553 }
1554
1555 if (dgs) {
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02001556 drbd_csum_ee(mdev, mdev->tconn->peer_integrity_tfm, peer_req, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001557 if (memcmp(dig_in, dig_vv, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001558 dev_err(DEV, "Digest integrity check FAILED: %llus +%u\n",
1559 (unsigned long long)sector, data_size);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001560 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001561 return NULL;
1562 }
1563 }
1564 mdev->recv_cnt += data_size>>9;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001565 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001566}
1567
1568/* drbd_drain_block() just takes a data block
1569 * out of the socket input buffer, and discards it.
1570 */
1571static int drbd_drain_block(struct drbd_conf *mdev, int data_size)
1572{
1573 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001574 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001575 void *data;
1576
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001577 if (!data_size)
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001578 return 0;
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001579
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +02001580 page = drbd_alloc_pages(mdev, 1, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001581
1582 data = kmap(page);
1583 while (data_size) {
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001584 unsigned int len = min_t(int, data_size, PAGE_SIZE);
1585
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001586 err = drbd_recv_all_warn(mdev->tconn, data, len);
1587 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001588 break;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001589 data_size -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001590 }
1591 kunmap(page);
Andreas Gruenbacher5cc287e2011-04-07 21:02:59 +02001592 drbd_free_pages(mdev, page, 0);
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001593 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001594}
1595
1596static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req,
1597 sector_t sector, int data_size)
1598{
1599 struct bio_vec *bvec;
1600 struct bio *bio;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001601 int dgs, err, i, expect;
Philipp Reisnera0638452011-01-19 14:31:32 +01001602 void *dig_in = mdev->tconn->int_dig_in;
1603 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001604
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001605 dgs = 0;
1606 if (mdev->tconn->peer_integrity_tfm) {
1607 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001608 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1609 if (err)
1610 return err;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001611 data_size -= dgs;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001612 }
1613
Philipp Reisnerb411b362009-09-25 16:07:19 -07001614 /* optimistically update recv_cnt. if receiving fails below,
1615 * we disconnect anyways, and counters will be reset. */
1616 mdev->recv_cnt += data_size>>9;
1617
1618 bio = req->master_bio;
1619 D_ASSERT(sector == bio->bi_sector);
1620
1621 bio_for_each_segment(bvec, bio, i) {
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001622 void *mapped = kmap(bvec->bv_page) + bvec->bv_offset;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001623 expect = min_t(int, data_size, bvec->bv_len);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001624 err = drbd_recv_all_warn(mdev->tconn, mapped, expect);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001625 kunmap(bvec->bv_page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001626 if (err)
1627 return err;
1628 data_size -= expect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001629 }
1630
1631 if (dgs) {
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02001632 drbd_csum_bio(mdev, mdev->tconn->peer_integrity_tfm, bio, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001633 if (memcmp(dig_in, dig_vv, dgs)) {
1634 dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n");
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001635 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001636 }
1637 }
1638
1639 D_ASSERT(data_size == 0);
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001640 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001641}
1642
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001643/*
1644 * e_end_resync_block() is called in asender context via
1645 * drbd_finish_peer_reqs().
1646 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001647static int e_end_resync_block(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001648{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001649 struct drbd_peer_request *peer_req =
1650 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001651 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001652 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001653 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001654
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001655 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001656
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001657 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1658 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001659 err = drbd_send_ack(mdev, P_RS_WRITE_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001660 } else {
1661 /* Record failure to sync */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001662 drbd_rs_failed_io(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001663
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001664 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001665 }
1666 dec_unacked(mdev);
1667
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001668 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001669}
1670
1671static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local)
1672{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001673 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001674
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001675 peer_req = read_in_block(mdev, ID_SYNCER, sector, data_size);
1676 if (!peer_req)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001677 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001678
1679 dec_rs_pending(mdev);
1680
Philipp Reisnerb411b362009-09-25 16:07:19 -07001681 inc_unacked(mdev);
1682 /* corresponding dec_unacked() in e_end_resync_block()
1683 * respective _drbd_clear_done_ee */
1684
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001685 peer_req->w.cb = e_end_resync_block;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001686
Philipp Reisner87eeee42011-01-19 14:16:30 +01001687 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001688 list_add(&peer_req->w.list, &mdev->sync_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001689 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001690
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001691 atomic_add(data_size >> 9, &mdev->rs_sect_ev);
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001692 if (drbd_submit_peer_request(mdev, peer_req, WRITE, DRBD_FAULT_RS_WR) == 0)
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001693 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001694
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001695 /* don't care for the reason here */
1696 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01001697 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001698 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001699 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001700
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001701 drbd_free_peer_req(mdev, peer_req);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001702fail:
1703 put_ldev(mdev);
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001704 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001705}
1706
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001707static struct drbd_request *
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001708find_request(struct drbd_conf *mdev, struct rb_root *root, u64 id,
1709 sector_t sector, bool missing_ok, const char *func)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001710{
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001711 struct drbd_request *req;
1712
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001713 /* Request object according to our peer */
1714 req = (struct drbd_request *)(unsigned long)id;
Andreas Gruenbacher5e472262011-01-27 14:42:51 +01001715 if (drbd_contains_interval(root, sector, &req->i) && req->i.local)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001716 return req;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001717 if (!missing_ok) {
Andreas Gruenbacher5af172e2011-07-15 09:43:23 +02001718 dev_err(DEV, "%s: failed to find request 0x%lx, sector %llus\n", func,
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001719 (unsigned long)id, (unsigned long long)sector);
1720 }
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001721 return NULL;
1722}
1723
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001724static int receive_DataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001725{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001726 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001727 struct drbd_request *req;
1728 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001729 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001730 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001731
1732 mdev = vnr_to_mdev(tconn, pi->vnr);
1733 if (!mdev)
1734 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001735
1736 sector = be64_to_cpu(p->sector);
1737
Philipp Reisner87eeee42011-01-19 14:16:30 +01001738 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001739 req = find_request(mdev, &mdev->read_requests, p->block_id, sector, false, __func__);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001740 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001741 if (unlikely(!req))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001742 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001743
Bart Van Assche24c48302011-05-21 18:32:29 +02001744 /* hlist_del(&req->collision) is done in _req_may_be_done, to avoid
Philipp Reisnerb411b362009-09-25 16:07:19 -07001745 * special casing it there for the various failure cases.
1746 * still no race with drbd_fail_pending_reads */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001747 err = recv_dless_read(mdev, req, sector, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001748 if (!err)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001749 req_mod(req, DATA_RECEIVED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001750 /* else: nothing. handled from drbd_disconnect...
1751 * I don't think we may complete this just yet
1752 * in case we are "on-disconnect: freeze" */
1753
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001754 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001755}
1756
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001757static int receive_RSDataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001758{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001759 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001760 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001761 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001762 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001763
1764 mdev = vnr_to_mdev(tconn, pi->vnr);
1765 if (!mdev)
1766 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001767
1768 sector = be64_to_cpu(p->sector);
1769 D_ASSERT(p->block_id == ID_SYNCER);
1770
1771 if (get_ldev(mdev)) {
1772 /* data is submitted to disk within recv_resync_read.
1773 * corresponding put_ldev done below on error,
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001774 * or in drbd_peer_request_endio. */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001775 err = recv_resync_read(mdev, sector, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001776 } else {
1777 if (__ratelimit(&drbd_ratelimit_state))
1778 dev_err(DEV, "Can not write resync data to local disk.\n");
1779
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001780 err = drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001781
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001782 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001783 }
1784
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001785 atomic_add(pi->size >> 9, &mdev->rs_sect_in);
Philipp Reisner778f2712010-07-06 11:14:00 +02001786
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001787 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001788}
1789
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001790static void restart_conflicting_writes(struct drbd_conf *mdev,
1791 sector_t sector, int size)
1792{
1793 struct drbd_interval *i;
1794 struct drbd_request *req;
1795
1796 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1797 if (!i->local)
1798 continue;
1799 req = container_of(i, struct drbd_request, i);
1800 if (req->rq_state & RQ_LOCAL_PENDING ||
1801 !(req->rq_state & RQ_POSTPONED))
1802 continue;
Lars Ellenberg2312f0b32011-11-24 10:36:25 +01001803 /* as it is RQ_POSTPONED, this will cause it to
1804 * be queued on the retry workqueue. */
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02001805 __req_mod(req, CONFLICT_RESOLVED, NULL);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001806 }
1807}
1808
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001809/*
1810 * e_end_block() is called in asender context via drbd_finish_peer_reqs().
Philipp Reisnerb411b362009-09-25 16:07:19 -07001811 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001812static int e_end_block(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001813{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001814 struct drbd_peer_request *peer_req =
1815 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001816 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001817 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001818 int err = 0, pcmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001819
Philipp Reisner303d1442011-04-13 16:24:47 -07001820 if (peer_req->flags & EE_SEND_WRITE_ACK) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001821 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001822 pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
1823 mdev->state.conn <= C_PAUSED_SYNC_T &&
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001824 peer_req->flags & EE_MAY_SET_IN_SYNC) ?
Philipp Reisnerb411b362009-09-25 16:07:19 -07001825 P_RS_WRITE_ACK : P_WRITE_ACK;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001826 err = drbd_send_ack(mdev, pcmd, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001827 if (pcmd == P_RS_WRITE_ACK)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001828 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001829 } else {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001830 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001831 /* we expect it to be marked out of sync anyways...
1832 * maybe assert this? */
1833 }
1834 dec_unacked(mdev);
1835 }
1836 /* we delete from the conflict detection hash _after_ we sent out the
1837 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
Philipp Reisner302bdea2011-04-21 11:36:49 +02001838 if (peer_req->flags & EE_IN_INTERVAL_TREE) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01001839 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001840 D_ASSERT(!drbd_interval_empty(&peer_req->i));
1841 drbd_remove_epoch_entry_interval(mdev, peer_req);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001842 if (peer_req->flags & EE_RESTART_REQUESTS)
1843 restart_conflicting_writes(mdev, sector, peer_req->i.size);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001844 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +01001845 } else
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001846 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001847
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001848 drbd_may_finish_epoch(mdev->tconn, peer_req->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001849
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001850 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001851}
1852
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001853static int e_send_ack(struct drbd_work *w, enum drbd_packet ack)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001854{
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001855 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001856 struct drbd_peer_request *peer_req =
1857 container_of(w, struct drbd_peer_request, w);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001858 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001859
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001860 err = drbd_send_ack(mdev, ack, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001861 dec_unacked(mdev);
1862
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001863 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001864}
1865
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02001866static int e_send_superseded(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001867{
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02001868 return e_send_ack(w, P_SUPERSEDED);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001869}
1870
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001871static int e_send_retry_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001872{
1873 struct drbd_tconn *tconn = w->mdev->tconn;
1874
1875 return e_send_ack(w, tconn->agreed_pro_version >= 100 ?
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02001876 P_RETRY_WRITE : P_SUPERSEDED);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001877}
1878
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001879static bool seq_greater(u32 a, u32 b)
1880{
1881 /*
1882 * We assume 32-bit wrap-around here.
1883 * For 24-bit wrap-around, we would have to shift:
1884 * a <<= 8; b <<= 8;
1885 */
1886 return (s32)a - (s32)b > 0;
1887}
1888
1889static u32 seq_max(u32 a, u32 b)
1890{
1891 return seq_greater(a, b) ? a : b;
1892}
1893
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001894static bool need_peer_seq(struct drbd_conf *mdev)
1895{
1896 struct drbd_tconn *tconn = mdev->tconn;
Philipp Reisner302bdea2011-04-21 11:36:49 +02001897 int tp;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001898
1899 /*
1900 * We only need to keep track of the last packet_seq number of our peer
Lars Ellenberg427c0432012-08-01 12:43:01 +02001901 * if we are in dual-primary mode and we have the resolve-conflicts flag set; see
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001902 * handle_write_conflicts().
1903 */
Philipp Reisner302bdea2011-04-21 11:36:49 +02001904
1905 rcu_read_lock();
1906 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
1907 rcu_read_unlock();
1908
Lars Ellenberg427c0432012-08-01 12:43:01 +02001909 return tp && test_bit(RESOLVE_CONFLICTS, &tconn->flags);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001910}
1911
Andreas Gruenbacher43ae0772011-02-03 18:42:08 +01001912static void update_peer_seq(struct drbd_conf *mdev, unsigned int peer_seq)
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001913{
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001914 unsigned int newest_peer_seq;
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001915
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001916 if (need_peer_seq(mdev)) {
1917 spin_lock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001918 newest_peer_seq = seq_max(mdev->peer_seq, peer_seq);
1919 mdev->peer_seq = newest_peer_seq;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001920 spin_unlock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001921 /* wake up only if we actually changed mdev->peer_seq */
1922 if (peer_seq == newest_peer_seq)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001923 wake_up(&mdev->seq_wait);
1924 }
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001925}
1926
Lars Ellenbergd93f6302012-03-26 15:49:13 +02001927static inline int overlaps(sector_t s1, int l1, sector_t s2, int l2)
1928{
1929 return !((s1 + (l1>>9) <= s2) || (s1 >= s2 + (l2>>9)));
1930}
1931
1932/* maybe change sync_ee into interval trees as well? */
Philipp Reisner3ea35df2012-04-06 12:13:18 +02001933static bool overlapping_resync_write(struct drbd_conf *mdev, struct drbd_peer_request *peer_req)
Lars Ellenbergd93f6302012-03-26 15:49:13 +02001934{
1935 struct drbd_peer_request *rs_req;
1936 bool rv = 0;
1937
1938 spin_lock_irq(&mdev->tconn->req_lock);
1939 list_for_each_entry(rs_req, &mdev->sync_ee, w.list) {
1940 if (overlaps(peer_req->i.sector, peer_req->i.size,
1941 rs_req->i.sector, rs_req->i.size)) {
1942 rv = 1;
1943 break;
1944 }
1945 }
1946 spin_unlock_irq(&mdev->tconn->req_lock);
1947
Lars Ellenbergd93f6302012-03-26 15:49:13 +02001948 return rv;
1949}
1950
Philipp Reisnerb411b362009-09-25 16:07:19 -07001951/* Called from receive_Data.
1952 * Synchronize packets on sock with packets on msock.
1953 *
1954 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
1955 * packet traveling on msock, they are still processed in the order they have
1956 * been sent.
1957 *
1958 * Note: we don't care for Ack packets overtaking P_DATA packets.
1959 *
1960 * In case packet_seq is larger than mdev->peer_seq number, there are
1961 * outstanding packets on the msock. We wait for them to arrive.
1962 * In case we are the logically next packet, we update mdev->peer_seq
1963 * ourselves. Correctly handles 32bit wrap around.
1964 *
1965 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
1966 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
1967 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
1968 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
1969 *
1970 * returns 0 if we may process the packet,
1971 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001972static int wait_for_and_update_peer_seq(struct drbd_conf *mdev, const u32 peer_seq)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001973{
1974 DEFINE_WAIT(wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001975 long timeout;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001976 int ret;
1977
1978 if (!need_peer_seq(mdev))
1979 return 0;
1980
Philipp Reisnerb411b362009-09-25 16:07:19 -07001981 spin_lock(&mdev->peer_seq_lock);
1982 for (;;) {
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001983 if (!seq_greater(peer_seq - 1, mdev->peer_seq)) {
1984 mdev->peer_seq = seq_max(mdev->peer_seq, peer_seq);
1985 ret = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001986 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001987 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001988 if (signal_pending(current)) {
1989 ret = -ERESTARTSYS;
1990 break;
1991 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001992 prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001993 spin_unlock(&mdev->peer_seq_lock);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001994 rcu_read_lock();
1995 timeout = rcu_dereference(mdev->tconn->net_conf)->ping_timeo*HZ/10;
1996 rcu_read_unlock();
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001997 timeout = schedule_timeout(timeout);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001998 spin_lock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001999 if (!timeout) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002000 ret = -ETIMEDOUT;
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01002001 dev_err(DEV, "Timed out waiting for missing ack packets; disconnecting\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002002 break;
2003 }
2004 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002005 spin_unlock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002006 finish_wait(&mdev->seq_wait, &wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002007 return ret;
2008}
2009
Lars Ellenberg688593c2010-11-17 22:25:03 +01002010/* see also bio_flags_to_wire()
2011 * DRBD_REQ_*, because we need to semantically map the flags to data packet
2012 * flags and back. We may replicate to other kernel versions. */
2013static unsigned long wire_flags_to_bio(struct drbd_conf *mdev, u32 dpf)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02002014{
Lars Ellenberg688593c2010-11-17 22:25:03 +01002015 return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
2016 (dpf & DP_FUA ? REQ_FUA : 0) |
2017 (dpf & DP_FLUSH ? REQ_FLUSH : 0) |
2018 (dpf & DP_DISCARD ? REQ_DISCARD : 0);
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02002019}
2020
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002021static void fail_postponed_requests(struct drbd_conf *mdev, sector_t sector,
2022 unsigned int size)
2023{
2024 struct drbd_interval *i;
2025
2026 repeat:
2027 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
2028 struct drbd_request *req;
2029 struct bio_and_error m;
2030
2031 if (!i->local)
2032 continue;
2033 req = container_of(i, struct drbd_request, i);
2034 if (!(req->rq_state & RQ_POSTPONED))
2035 continue;
2036 req->rq_state &= ~RQ_POSTPONED;
2037 __req_mod(req, NEG_ACKED, &m);
2038 spin_unlock_irq(&mdev->tconn->req_lock);
2039 if (m.bio)
2040 complete_master_bio(mdev, &m);
2041 spin_lock_irq(&mdev->tconn->req_lock);
2042 goto repeat;
2043 }
2044}
2045
2046static int handle_write_conflicts(struct drbd_conf *mdev,
2047 struct drbd_peer_request *peer_req)
2048{
2049 struct drbd_tconn *tconn = mdev->tconn;
Lars Ellenberg427c0432012-08-01 12:43:01 +02002050 bool resolve_conflicts = test_bit(RESOLVE_CONFLICTS, &tconn->flags);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002051 sector_t sector = peer_req->i.sector;
2052 const unsigned int size = peer_req->i.size;
2053 struct drbd_interval *i;
2054 bool equal;
2055 int err;
2056
2057 /*
2058 * Inserting the peer request into the write_requests tree will prevent
2059 * new conflicting local requests from being added.
2060 */
2061 drbd_insert_interval(&mdev->write_requests, &peer_req->i);
2062
2063 repeat:
2064 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
2065 if (i == &peer_req->i)
2066 continue;
2067
2068 if (!i->local) {
2069 /*
2070 * Our peer has sent a conflicting remote request; this
2071 * should not happen in a two-node setup. Wait for the
2072 * earlier peer request to complete.
2073 */
2074 err = drbd_wait_misc(mdev, i);
2075 if (err)
2076 goto out;
2077 goto repeat;
2078 }
2079
2080 equal = i->sector == sector && i->size == size;
2081 if (resolve_conflicts) {
2082 /*
2083 * If the peer request is fully contained within the
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02002084 * overlapping request, it can be considered overwritten
2085 * and thus superseded; otherwise, it will be retried
2086 * once all overlapping requests have completed.
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002087 */
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02002088 bool superseded = i->sector <= sector && i->sector +
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002089 (i->size >> 9) >= sector + (size >> 9);
2090
2091 if (!equal)
2092 dev_alert(DEV, "Concurrent writes detected: "
2093 "local=%llus +%u, remote=%llus +%u, "
2094 "assuming %s came first\n",
2095 (unsigned long long)i->sector, i->size,
2096 (unsigned long long)sector, size,
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02002097 superseded ? "local" : "remote");
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002098
2099 inc_unacked(mdev);
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02002100 peer_req->w.cb = superseded ? e_send_superseded :
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002101 e_send_retry_write;
2102 list_add_tail(&peer_req->w.list, &mdev->done_ee);
2103 wake_asender(mdev->tconn);
2104
2105 err = -ENOENT;
2106 goto out;
2107 } else {
2108 struct drbd_request *req =
2109 container_of(i, struct drbd_request, i);
2110
2111 if (!equal)
2112 dev_alert(DEV, "Concurrent writes detected: "
2113 "local=%llus +%u, remote=%llus +%u\n",
2114 (unsigned long long)i->sector, i->size,
2115 (unsigned long long)sector, size);
2116
2117 if (req->rq_state & RQ_LOCAL_PENDING ||
2118 !(req->rq_state & RQ_POSTPONED)) {
2119 /*
2120 * Wait for the node with the discard flag to
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02002121 * decide if this request has been superseded
2122 * or needs to be retried.
2123 * Requests that have been superseded will
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002124 * disappear from the write_requests tree.
2125 *
2126 * In addition, wait for the conflicting
2127 * request to finish locally before submitting
2128 * the conflicting peer request.
2129 */
2130 err = drbd_wait_misc(mdev, &req->i);
2131 if (err) {
2132 _conn_request_state(mdev->tconn,
2133 NS(conn, C_TIMEOUT),
2134 CS_HARD);
2135 fail_postponed_requests(mdev, sector, size);
2136 goto out;
2137 }
2138 goto repeat;
2139 }
2140 /*
2141 * Remember to restart the conflicting requests after
2142 * the new peer request has completed.
2143 */
2144 peer_req->flags |= EE_RESTART_REQUESTS;
2145 }
2146 }
2147 err = 0;
2148
2149 out:
2150 if (err)
2151 drbd_remove_epoch_entry_interval(mdev, peer_req);
2152 return err;
2153}
2154
Philipp Reisnerb411b362009-09-25 16:07:19 -07002155/* mirrored write */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002156static int receive_Data(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002157{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002158 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002159 sector_t sector;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002160 struct drbd_peer_request *peer_req;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002161 struct p_data *p = pi->data;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002162 u32 peer_seq = be32_to_cpu(p->seq_num);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002163 int rw = WRITE;
2164 u32 dp_flags;
Philipp Reisner302bdea2011-04-21 11:36:49 +02002165 int err, tp;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002166
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002167 mdev = vnr_to_mdev(tconn, pi->vnr);
2168 if (!mdev)
2169 return -EIO;
2170
Philipp Reisnerb411b362009-09-25 16:07:19 -07002171 if (!get_ldev(mdev)) {
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002172 int err2;
2173
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002174 err = wait_for_and_update_peer_seq(mdev, peer_seq);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002175 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisner12038a32011-11-09 19:18:00 +01002176 atomic_inc(&tconn->current_epoch->epoch_size);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002177 err2 = drbd_drain_block(mdev, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002178 if (!err)
2179 err = err2;
2180 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002181 }
2182
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01002183 /*
2184 * Corresponding put_ldev done either below (on various errors), or in
2185 * drbd_peer_request_endio, if we successfully submit the data at the
2186 * end of this function.
2187 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002188
2189 sector = be64_to_cpu(p->sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002190 peer_req = read_in_block(mdev, p->block_id, sector, pi->size);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002191 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002192 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002193 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002194 }
2195
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002196 peer_req->w.cb = e_end_block;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002197
Lars Ellenberg688593c2010-11-17 22:25:03 +01002198 dp_flags = be32_to_cpu(p->dp_flags);
2199 rw |= wire_flags_to_bio(mdev, dp_flags);
Lars Ellenberg81a35372012-07-30 09:00:54 +02002200 if (peer_req->pages == NULL) {
2201 D_ASSERT(peer_req->i.size == 0);
2202 D_ASSERT(dp_flags & DP_FLUSH);
2203 }
Lars Ellenberg688593c2010-11-17 22:25:03 +01002204
2205 if (dp_flags & DP_MAY_SET_IN_SYNC)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002206 peer_req->flags |= EE_MAY_SET_IN_SYNC;
Lars Ellenberg688593c2010-11-17 22:25:03 +01002207
Philipp Reisner12038a32011-11-09 19:18:00 +01002208 spin_lock(&tconn->epoch_lock);
2209 peer_req->epoch = tconn->current_epoch;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002210 atomic_inc(&peer_req->epoch->epoch_size);
2211 atomic_inc(&peer_req->epoch->active);
Philipp Reisner12038a32011-11-09 19:18:00 +01002212 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002213
Philipp Reisner302bdea2011-04-21 11:36:49 +02002214 rcu_read_lock();
2215 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
2216 rcu_read_unlock();
2217 if (tp) {
2218 peer_req->flags |= EE_IN_INTERVAL_TREE;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002219 err = wait_for_and_update_peer_seq(mdev, peer_seq);
2220 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002221 goto out_interrupted;
Philipp Reisner87eeee42011-01-19 14:16:30 +01002222 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002223 err = handle_write_conflicts(mdev, peer_req);
2224 if (err) {
2225 spin_unlock_irq(&mdev->tconn->req_lock);
2226 if (err == -ENOENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002227 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002228 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002229 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002230 goto out_interrupted;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002231 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002232 } else
2233 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002234 list_add(&peer_req->w.list, &mdev->active_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002235 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002236
Lars Ellenbergd93f6302012-03-26 15:49:13 +02002237 if (mdev->state.conn == C_SYNC_TARGET)
Philipp Reisner3ea35df2012-04-06 12:13:18 +02002238 wait_event(mdev->ee_wait, !overlapping_resync_write(mdev, peer_req));
Lars Ellenbergd93f6302012-03-26 15:49:13 +02002239
Philipp Reisner303d1442011-04-13 16:24:47 -07002240 if (mdev->tconn->agreed_pro_version < 100) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02002241 rcu_read_lock();
2242 switch (rcu_dereference(mdev->tconn->net_conf)->wire_protocol) {
Philipp Reisner303d1442011-04-13 16:24:47 -07002243 case DRBD_PROT_C:
2244 dp_flags |= DP_SEND_WRITE_ACK;
2245 break;
2246 case DRBD_PROT_B:
2247 dp_flags |= DP_SEND_RECEIVE_ACK;
2248 break;
2249 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002250 rcu_read_unlock();
Philipp Reisner303d1442011-04-13 16:24:47 -07002251 }
2252
2253 if (dp_flags & DP_SEND_WRITE_ACK) {
2254 peer_req->flags |= EE_SEND_WRITE_ACK;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002255 inc_unacked(mdev);
2256 /* corresponding dec_unacked() in e_end_block()
2257 * respective _drbd_clear_done_ee */
Philipp Reisner303d1442011-04-13 16:24:47 -07002258 }
2259
2260 if (dp_flags & DP_SEND_RECEIVE_ACK) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002261 /* I really don't like it that the receiver thread
2262 * sends on the msock, but anyways */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002263 drbd_send_ack(mdev, P_RECV_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002264 }
2265
Lars Ellenberg6719fb02010-10-18 23:04:07 +02002266 if (mdev->state.pdsk < D_INCONSISTENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002267 /* In case we have the only disk of the cluster, */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002268 drbd_set_out_of_sync(mdev, peer_req->i.sector, peer_req->i.size);
2269 peer_req->flags |= EE_CALL_AL_COMPLETE_IO;
2270 peer_req->flags &= ~EE_MAY_SET_IN_SYNC;
Lars Ellenberg181286a2011-03-31 15:18:56 +02002271 drbd_al_begin_io(mdev, &peer_req->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002272 }
2273
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002274 err = drbd_submit_peer_request(mdev, peer_req, rw, DRBD_FAULT_DT_WR);
2275 if (!err)
2276 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002277
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002278 /* don't care for the reason here */
2279 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002280 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002281 list_del(&peer_req->w.list);
2282 drbd_remove_epoch_entry_interval(mdev, peer_req);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002283 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002284 if (peer_req->flags & EE_CALL_AL_COMPLETE_IO)
Lars Ellenberg181286a2011-03-31 15:18:56 +02002285 drbd_al_complete_io(mdev, &peer_req->i);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002286
Philipp Reisnerb411b362009-09-25 16:07:19 -07002287out_interrupted:
Philipp Reisner1e9dd292011-11-10 15:14:53 +01002288 drbd_may_finish_epoch(tconn, peer_req->epoch, EV_PUT + EV_CLEANUP);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002289 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002290 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002291 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002292}
2293
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002294/* We may throttle resync, if the lower device seems to be busy,
2295 * and current sync rate is above c_min_rate.
2296 *
2297 * To decide whether or not the lower device is busy, we use a scheme similar
2298 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
2299 * (more than 64 sectors) of activity we cannot account for with our own resync
2300 * activity, it obviously is "busy".
2301 *
2302 * The current sync rate used here uses only the most recent two step marks,
2303 * to have a short time average so we can react faster.
2304 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002305int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002306{
2307 struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
2308 unsigned long db, dt, dbdt;
Philipp Reisnere3555d82010-11-07 15:56:29 +01002309 struct lc_element *tmp;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002310 int curr_events;
2311 int throttle = 0;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002312 unsigned int c_min_rate;
2313
2314 rcu_read_lock();
2315 c_min_rate = rcu_dereference(mdev->ldev->disk_conf)->c_min_rate;
2316 rcu_read_unlock();
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002317
2318 /* feature disabled? */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002319 if (c_min_rate == 0)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002320 return 0;
2321
Philipp Reisnere3555d82010-11-07 15:56:29 +01002322 spin_lock_irq(&mdev->al_lock);
2323 tmp = lc_find(mdev->resync, BM_SECT_TO_EXT(sector));
2324 if (tmp) {
2325 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
2326 if (test_bit(BME_PRIORITY, &bm_ext->flags)) {
2327 spin_unlock_irq(&mdev->al_lock);
2328 return 0;
2329 }
2330 /* Do not slow down if app IO is already waiting for this extent */
2331 }
2332 spin_unlock_irq(&mdev->al_lock);
2333
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002334 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
2335 (int)part_stat_read(&disk->part0, sectors[1]) -
2336 atomic_read(&mdev->rs_sect_ev);
Philipp Reisnere3555d82010-11-07 15:56:29 +01002337
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002338 if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
2339 unsigned long rs_left;
2340 int i;
2341
2342 mdev->rs_last_events = curr_events;
2343
2344 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
2345 * approx. */
Lars Ellenberg2649f082010-11-05 10:05:47 +01002346 i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
2347
2348 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
2349 rs_left = mdev->ov_left;
2350 else
2351 rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002352
2353 dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
2354 if (!dt)
2355 dt++;
2356 db = mdev->rs_mark_left[i] - rs_left;
2357 dbdt = Bit2KB(db/dt);
2358
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002359 if (dbdt > c_min_rate)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002360 throttle = 1;
2361 }
2362 return throttle;
2363}
2364
2365
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002366static int receive_DataRequest(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002367{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002368 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002369 sector_t sector;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002370 sector_t capacity;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002371 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002372 struct digest_info *di = NULL;
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002373 int size, verb;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002374 unsigned int fault_type;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002375 struct p_block_req *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002376
2377 mdev = vnr_to_mdev(tconn, pi->vnr);
2378 if (!mdev)
2379 return -EIO;
2380 capacity = drbd_get_capacity(mdev->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002381
2382 sector = be64_to_cpu(p->sector);
2383 size = be32_to_cpu(p->blksize);
2384
Andreas Gruenbacherc670a392011-02-21 12:41:39 +01002385 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002386 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2387 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002388 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002389 }
2390 if (sector + (size>>9) > capacity) {
2391 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2392 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002393 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002394 }
2395
2396 if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002397 verb = 1;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002398 switch (pi->cmd) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002399 case P_DATA_REQUEST:
2400 drbd_send_ack_rp(mdev, P_NEG_DREPLY, p);
2401 break;
2402 case P_RS_DATA_REQUEST:
2403 case P_CSUM_RS_REQUEST:
2404 case P_OV_REQUEST:
2405 drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p);
2406 break;
2407 case P_OV_REPLY:
2408 verb = 0;
2409 dec_rs_pending(mdev);
2410 drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC);
2411 break;
2412 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002413 BUG();
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002414 }
2415 if (verb && __ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002416 dev_err(DEV, "Can not satisfy peer's read request, "
2417 "no local data.\n");
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002418
Lars Ellenberga821cc42010-09-06 12:31:37 +02002419 /* drain possibly payload */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002420 return drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002421 }
2422
2423 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
2424 * "criss-cross" setup, that might cause write-out on some other DRBD,
2425 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02002426 peer_req = drbd_alloc_peer_req(mdev, p->block_id, sector, size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002427 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002428 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002429 return -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002430 }
2431
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002432 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002433 case P_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002434 peer_req->w.cb = w_e_end_data_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002435 fault_type = DRBD_FAULT_DT_RD;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002436 /* application IO, don't drbd_rs_begin_io */
2437 goto submit;
2438
Philipp Reisnerb411b362009-09-25 16:07:19 -07002439 case P_RS_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002440 peer_req->w.cb = w_e_end_rsdata_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002441 fault_type = DRBD_FAULT_RS_RD;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002442 /* used in the sector offset progress display */
2443 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002444 break;
2445
2446 case P_OV_REPLY:
2447 case P_CSUM_RS_REQUEST:
2448 fault_type = DRBD_FAULT_RS_RD;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002449 di = kmalloc(sizeof(*di) + pi->size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002450 if (!di)
2451 goto out_free_e;
2452
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002453 di->digest_size = pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002454 di->digest = (((char *)di)+sizeof(struct digest_info));
2455
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002456 peer_req->digest = di;
2457 peer_req->flags |= EE_HAS_DIGEST;
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +02002458
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002459 if (drbd_recv_all(mdev->tconn, di->digest, pi->size))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002460 goto out_free_e;
2461
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002462 if (pi->cmd == P_CSUM_RS_REQUEST) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002463 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002464 peer_req->w.cb = w_e_end_csum_rs_req;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002465 /* used in the sector offset progress display */
2466 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002467 } else if (pi->cmd == P_OV_REPLY) {
Lars Ellenberg2649f082010-11-05 10:05:47 +01002468 /* track progress, we may need to throttle */
2469 atomic_add(size >> 9, &mdev->rs_sect_in);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002470 peer_req->w.cb = w_e_end_ov_reply;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002471 dec_rs_pending(mdev);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002472 /* drbd_rs_begin_io done when we sent this request,
2473 * but accounting still needs to be done. */
2474 goto submit_for_resync;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002475 }
2476 break;
2477
2478 case P_OV_REQUEST:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002479 if (mdev->ov_start_sector == ~(sector_t)0 &&
Philipp Reisner31890f42011-01-19 14:12:51 +01002480 mdev->tconn->agreed_pro_version >= 90) {
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002481 unsigned long now = jiffies;
2482 int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002483 mdev->ov_start_sector = sector;
2484 mdev->ov_position = sector;
Lars Ellenberg30b743a2010-11-05 09:39:06 +01002485 mdev->ov_left = drbd_bm_bits(mdev) - BM_SECT_TO_BIT(sector);
2486 mdev->rs_total = mdev->ov_left;
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002487 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
2488 mdev->rs_mark_left[i] = mdev->ov_left;
2489 mdev->rs_mark_time[i] = now;
2490 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002491 dev_info(DEV, "Online Verify start sector: %llu\n",
2492 (unsigned long long)sector);
2493 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002494 peer_req->w.cb = w_e_end_ov_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002495 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002496 break;
2497
Philipp Reisnerb411b362009-09-25 16:07:19 -07002498 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002499 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002500 }
2501
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002502 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2503 * wrt the receiver, but it is not as straightforward as it may seem.
2504 * Various places in the resync start and stop logic assume resync
2505 * requests are processed in order, requeuing this on the worker thread
2506 * introduces a bunch of new code for synchronization between threads.
2507 *
2508 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2509 * "forever", throttling after drbd_rs_begin_io will lock that extent
2510 * for application writes for the same time. For now, just throttle
2511 * here, where the rest of the code expects the receiver to sleep for
2512 * a while, anyways.
2513 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002514
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002515 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2516 * this defers syncer requests for some time, before letting at least
2517 * on request through. The resync controller on the receiving side
2518 * will adapt to the incoming rate accordingly.
2519 *
2520 * We cannot throttle here if remote is Primary/SyncTarget:
2521 * we would also throttle its application reads.
2522 * In that case, throttling is done on the SyncTarget only.
2523 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002524 if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev, sector))
2525 schedule_timeout_uninterruptible(HZ/10);
2526 if (drbd_rs_begin_io(mdev, sector))
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002527 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002528
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002529submit_for_resync:
2530 atomic_add(size >> 9, &mdev->rs_sect_ev);
2531
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002532submit:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002533 inc_unacked(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002534 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002535 list_add_tail(&peer_req->w.list, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002536 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002537
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01002538 if (drbd_submit_peer_request(mdev, peer_req, READ, fault_type) == 0)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002539 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002540
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002541 /* don't care for the reason here */
2542 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002543 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002544 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002545 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002546 /* no drbd_rs_complete_io(), we are dropping the connection anyways */
2547
Philipp Reisnerb411b362009-09-25 16:07:19 -07002548out_free_e:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002549 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002550 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002551 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002552}
2553
2554static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
2555{
2556 int self, peer, rv = -100;
2557 unsigned long ch_self, ch_peer;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002558 enum drbd_after_sb_p after_sb_0p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002559
2560 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2561 peer = mdev->p_uuid[UI_BITMAP] & 1;
2562
2563 ch_peer = mdev->p_uuid[UI_SIZE];
2564 ch_self = mdev->comm_bm_set;
2565
Philipp Reisner44ed1672011-04-19 17:10:19 +02002566 rcu_read_lock();
2567 after_sb_0p = rcu_dereference(mdev->tconn->net_conf)->after_sb_0p;
2568 rcu_read_unlock();
2569 switch (after_sb_0p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002570 case ASB_CONSENSUS:
2571 case ASB_DISCARD_SECONDARY:
2572 case ASB_CALL_HELPER:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002573 case ASB_VIOLENTLY:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002574 dev_err(DEV, "Configuration error.\n");
2575 break;
2576 case ASB_DISCONNECT:
2577 break;
2578 case ASB_DISCARD_YOUNGER_PRI:
2579 if (self == 0 && peer == 1) {
2580 rv = -1;
2581 break;
2582 }
2583 if (self == 1 && peer == 0) {
2584 rv = 1;
2585 break;
2586 }
2587 /* Else fall through to one of the other strategies... */
2588 case ASB_DISCARD_OLDER_PRI:
2589 if (self == 0 && peer == 1) {
2590 rv = 1;
2591 break;
2592 }
2593 if (self == 1 && peer == 0) {
2594 rv = -1;
2595 break;
2596 }
2597 /* Else fall through to one of the other strategies... */
Lars Ellenbergad19bf62009-10-14 09:36:49 +02002598 dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
Philipp Reisnerb411b362009-09-25 16:07:19 -07002599 "Using discard-least-changes instead\n");
2600 case ASB_DISCARD_ZERO_CHG:
2601 if (ch_peer == 0 && ch_self == 0) {
Lars Ellenberg427c0432012-08-01 12:43:01 +02002602 rv = test_bit(RESOLVE_CONFLICTS, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002603 ? -1 : 1;
2604 break;
2605 } else {
2606 if (ch_peer == 0) { rv = 1; break; }
2607 if (ch_self == 0) { rv = -1; break; }
2608 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002609 if (after_sb_0p == ASB_DISCARD_ZERO_CHG)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002610 break;
2611 case ASB_DISCARD_LEAST_CHG:
2612 if (ch_self < ch_peer)
2613 rv = -1;
2614 else if (ch_self > ch_peer)
2615 rv = 1;
2616 else /* ( ch_self == ch_peer ) */
2617 /* Well, then use something else. */
Lars Ellenberg427c0432012-08-01 12:43:01 +02002618 rv = test_bit(RESOLVE_CONFLICTS, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002619 ? -1 : 1;
2620 break;
2621 case ASB_DISCARD_LOCAL:
2622 rv = -1;
2623 break;
2624 case ASB_DISCARD_REMOTE:
2625 rv = 1;
2626 }
2627
2628 return rv;
2629}
2630
2631static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
2632{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002633 int hg, rv = -100;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002634 enum drbd_after_sb_p after_sb_1p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002635
Philipp Reisner44ed1672011-04-19 17:10:19 +02002636 rcu_read_lock();
2637 after_sb_1p = rcu_dereference(mdev->tconn->net_conf)->after_sb_1p;
2638 rcu_read_unlock();
2639 switch (after_sb_1p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002640 case ASB_DISCARD_YOUNGER_PRI:
2641 case ASB_DISCARD_OLDER_PRI:
2642 case ASB_DISCARD_LEAST_CHG:
2643 case ASB_DISCARD_LOCAL:
2644 case ASB_DISCARD_REMOTE:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002645 case ASB_DISCARD_ZERO_CHG:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002646 dev_err(DEV, "Configuration error.\n");
2647 break;
2648 case ASB_DISCONNECT:
2649 break;
2650 case ASB_CONSENSUS:
2651 hg = drbd_asb_recover_0p(mdev);
2652 if (hg == -1 && mdev->state.role == R_SECONDARY)
2653 rv = hg;
2654 if (hg == 1 && mdev->state.role == R_PRIMARY)
2655 rv = hg;
2656 break;
2657 case ASB_VIOLENTLY:
2658 rv = drbd_asb_recover_0p(mdev);
2659 break;
2660 case ASB_DISCARD_SECONDARY:
2661 return mdev->state.role == R_PRIMARY ? 1 : -1;
2662 case ASB_CALL_HELPER:
2663 hg = drbd_asb_recover_0p(mdev);
2664 if (hg == -1 && mdev->state.role == R_PRIMARY) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002665 enum drbd_state_rv rv2;
2666
2667 drbd_set_role(mdev, R_SECONDARY, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002668 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2669 * we might be here in C_WF_REPORT_PARAMS which is transient.
2670 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002671 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2672 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002673 drbd_khelper(mdev, "pri-lost-after-sb");
2674 } else {
2675 dev_warn(DEV, "Successfully gave up primary role.\n");
2676 rv = hg;
2677 }
2678 } else
2679 rv = hg;
2680 }
2681
2682 return rv;
2683}
2684
2685static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
2686{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002687 int hg, rv = -100;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002688 enum drbd_after_sb_p after_sb_2p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002689
Philipp Reisner44ed1672011-04-19 17:10:19 +02002690 rcu_read_lock();
2691 after_sb_2p = rcu_dereference(mdev->tconn->net_conf)->after_sb_2p;
2692 rcu_read_unlock();
2693 switch (after_sb_2p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002694 case ASB_DISCARD_YOUNGER_PRI:
2695 case ASB_DISCARD_OLDER_PRI:
2696 case ASB_DISCARD_LEAST_CHG:
2697 case ASB_DISCARD_LOCAL:
2698 case ASB_DISCARD_REMOTE:
2699 case ASB_CONSENSUS:
2700 case ASB_DISCARD_SECONDARY:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002701 case ASB_DISCARD_ZERO_CHG:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002702 dev_err(DEV, "Configuration error.\n");
2703 break;
2704 case ASB_VIOLENTLY:
2705 rv = drbd_asb_recover_0p(mdev);
2706 break;
2707 case ASB_DISCONNECT:
2708 break;
2709 case ASB_CALL_HELPER:
2710 hg = drbd_asb_recover_0p(mdev);
2711 if (hg == -1) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002712 enum drbd_state_rv rv2;
2713
Philipp Reisnerb411b362009-09-25 16:07:19 -07002714 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2715 * we might be here in C_WF_REPORT_PARAMS which is transient.
2716 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002717 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2718 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002719 drbd_khelper(mdev, "pri-lost-after-sb");
2720 } else {
2721 dev_warn(DEV, "Successfully gave up primary role.\n");
2722 rv = hg;
2723 }
2724 } else
2725 rv = hg;
2726 }
2727
2728 return rv;
2729}
2730
2731static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
2732 u64 bits, u64 flags)
2733{
2734 if (!uuid) {
2735 dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
2736 return;
2737 }
2738 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
2739 text,
2740 (unsigned long long)uuid[UI_CURRENT],
2741 (unsigned long long)uuid[UI_BITMAP],
2742 (unsigned long long)uuid[UI_HISTORY_START],
2743 (unsigned long long)uuid[UI_HISTORY_END],
2744 (unsigned long long)bits,
2745 (unsigned long long)flags);
2746}
2747
2748/*
2749 100 after split brain try auto recover
2750 2 C_SYNC_SOURCE set BitMap
2751 1 C_SYNC_SOURCE use BitMap
2752 0 no Sync
2753 -1 C_SYNC_TARGET use BitMap
2754 -2 C_SYNC_TARGET set BitMap
2755 -100 after split brain, disconnect
2756-1000 unrelated data
Philipp Reisner4a23f262011-01-11 17:42:17 +01002757-1091 requires proto 91
2758-1096 requires proto 96
Philipp Reisnerb411b362009-09-25 16:07:19 -07002759 */
2760static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
2761{
2762 u64 self, peer;
2763 int i, j;
2764
2765 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2766 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2767
2768 *rule_nr = 10;
2769 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
2770 return 0;
2771
2772 *rule_nr = 20;
2773 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
2774 peer != UUID_JUST_CREATED)
2775 return -2;
2776
2777 *rule_nr = 30;
2778 if (self != UUID_JUST_CREATED &&
2779 (peer == UUID_JUST_CREATED || peer == (u64)0))
2780 return 2;
2781
2782 if (self == peer) {
2783 int rct, dc; /* roles at crash time */
2784
2785 if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
2786
Philipp Reisner31890f42011-01-19 14:12:51 +01002787 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002788 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002789
2790 if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
2791 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
2792 dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
Philipp Reisner39a1aa72012-08-08 21:19:09 +02002793 drbd_uuid_move_history(mdev);
2794 mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[UI_BITMAP];
2795 mdev->ldev->md.uuid[UI_BITMAP] = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002796
2797 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2798 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2799 *rule_nr = 34;
2800 } else {
2801 dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
2802 *rule_nr = 36;
2803 }
2804
2805 return 1;
2806 }
2807
2808 if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
2809
Philipp Reisner31890f42011-01-19 14:12:51 +01002810 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002811 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002812
2813 if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
2814 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
2815 dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
2816
2817 mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
2818 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
2819 mdev->p_uuid[UI_BITMAP] = 0UL;
2820
2821 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2822 *rule_nr = 35;
2823 } else {
2824 dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
2825 *rule_nr = 37;
2826 }
2827
2828 return -1;
2829 }
2830
2831 /* Common power [off|failure] */
2832 rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
2833 (mdev->p_uuid[UI_FLAGS] & 2);
2834 /* lowest bit is set when we were primary,
2835 * next bit (weight 2) is set when peer was primary */
2836 *rule_nr = 40;
2837
2838 switch (rct) {
2839 case 0: /* !self_pri && !peer_pri */ return 0;
2840 case 1: /* self_pri && !peer_pri */ return 1;
2841 case 2: /* !self_pri && peer_pri */ return -1;
2842 case 3: /* self_pri && peer_pri */
Lars Ellenberg427c0432012-08-01 12:43:01 +02002843 dc = test_bit(RESOLVE_CONFLICTS, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002844 return dc ? -1 : 1;
2845 }
2846 }
2847
2848 *rule_nr = 50;
2849 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2850 if (self == peer)
2851 return -1;
2852
2853 *rule_nr = 51;
2854 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2855 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002856 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002857 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) ==
2858 (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) :
2859 peer + UUID_NEW_BM_OFFSET == (mdev->p_uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002860 /* The last P_SYNC_UUID did not get though. Undo the last start of
2861 resync as sync source modifications of the peer's UUIDs. */
2862
Philipp Reisner31890f42011-01-19 14:12:51 +01002863 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002864 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002865
2866 mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
2867 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
Philipp Reisner4a23f262011-01-11 17:42:17 +01002868
Lars Ellenberg1882e222012-05-07 13:09:00 +02002869 dev_info(DEV, "Lost last syncUUID packet, corrected:\n");
Philipp Reisner4a23f262011-01-11 17:42:17 +01002870 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2871
Philipp Reisnerb411b362009-09-25 16:07:19 -07002872 return -1;
2873 }
2874 }
2875
2876 *rule_nr = 60;
2877 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2878 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2879 peer = mdev->p_uuid[i] & ~((u64)1);
2880 if (self == peer)
2881 return -2;
2882 }
2883
2884 *rule_nr = 70;
2885 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2886 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2887 if (self == peer)
2888 return 1;
2889
2890 *rule_nr = 71;
2891 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2892 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002893 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002894 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) ==
2895 (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) :
2896 self + UUID_NEW_BM_OFFSET == (mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002897 /* The last P_SYNC_UUID did not get though. Undo the last start of
2898 resync as sync source modifications of our UUIDs. */
2899
Philipp Reisner31890f42011-01-19 14:12:51 +01002900 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002901 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002902
Philipp Reisner39a1aa72012-08-08 21:19:09 +02002903 __drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
2904 __drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002905
Philipp Reisner4a23f262011-01-11 17:42:17 +01002906 dev_info(DEV, "Last syncUUID did not get through, corrected:\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002907 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2908 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2909
2910 return 1;
2911 }
2912 }
2913
2914
2915 *rule_nr = 80;
Philipp Reisnerd8c2a362009-11-18 15:52:51 +01002916 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002917 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2918 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2919 if (self == peer)
2920 return 2;
2921 }
2922
2923 *rule_nr = 90;
2924 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2925 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2926 if (self == peer && self != ((u64)0))
2927 return 100;
2928
2929 *rule_nr = 100;
2930 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2931 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2932 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
2933 peer = mdev->p_uuid[j] & ~((u64)1);
2934 if (self == peer)
2935 return -100;
2936 }
2937 }
2938
2939 return -1000;
2940}
2941
2942/* drbd_sync_handshake() returns the new conn state on success, or
2943 CONN_MASK (-1) on failure.
2944 */
2945static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
2946 enum drbd_disk_state peer_disk) __must_hold(local)
2947{
Philipp Reisnerb411b362009-09-25 16:07:19 -07002948 enum drbd_conns rv = C_MASK;
2949 enum drbd_disk_state mydisk;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002950 struct net_conf *nc;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02002951 int hg, rule_nr, rr_conflict, tentative;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002952
2953 mydisk = mdev->state.disk;
2954 if (mydisk == D_NEGOTIATING)
2955 mydisk = mdev->new_state_tmp.disk;
2956
2957 dev_info(DEV, "drbd_sync_handshake:\n");
Philipp Reisner39a1aa72012-08-08 21:19:09 +02002958
2959 spin_lock_irq(&mdev->ldev->md.uuid_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002960 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);
Philipp Reisner39a1aa72012-08-08 21:19:09 +02002965 spin_unlock_irq(&mdev->ldev->md.uuid_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002966
2967 dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
2968
2969 if (hg == -1000) {
2970 dev_alert(DEV, "Unrelated data, aborting!\n");
2971 return C_MASK;
2972 }
Philipp Reisner4a23f262011-01-11 17:42:17 +01002973 if (hg < -1000) {
2974 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 -07002975 return C_MASK;
2976 }
2977
2978 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
2979 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
2980 int f = (hg == -100) || abs(hg) == 2;
2981 hg = mydisk > D_INCONSISTENT ? 1 : -1;
2982 if (f)
2983 hg = hg*2;
2984 dev_info(DEV, "Becoming sync %s due to disk states.\n",
2985 hg > 0 ? "source" : "target");
2986 }
2987
Adam Gandelman3a11a482010-04-08 16:48:23 -07002988 if (abs(hg) == 100)
2989 drbd_khelper(mdev, "initial-split-brain");
2990
Philipp Reisner44ed1672011-04-19 17:10:19 +02002991 rcu_read_lock();
2992 nc = rcu_dereference(mdev->tconn->net_conf);
2993
2994 if (hg == 100 || (hg == -100 && nc->always_asbp)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002995 int pcount = (mdev->state.role == R_PRIMARY)
2996 + (peer_role == R_PRIMARY);
2997 int forced = (hg == -100);
2998
2999 switch (pcount) {
3000 case 0:
3001 hg = drbd_asb_recover_0p(mdev);
3002 break;
3003 case 1:
3004 hg = drbd_asb_recover_1p(mdev);
3005 break;
3006 case 2:
3007 hg = drbd_asb_recover_2p(mdev);
3008 break;
3009 }
3010 if (abs(hg) < 100) {
3011 dev_warn(DEV, "Split-Brain detected, %d primaries, "
3012 "automatically solved. Sync from %s node\n",
3013 pcount, (hg < 0) ? "peer" : "this");
3014 if (forced) {
3015 dev_warn(DEV, "Doing a full sync, since"
3016 " UUIDs where ambiguous.\n");
3017 hg = hg*2;
3018 }
3019 }
3020 }
3021
3022 if (hg == -100) {
Philipp Reisner08b165b2011-09-05 16:22:33 +02003023 if (test_bit(DISCARD_MY_DATA, &mdev->flags) && !(mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003024 hg = -1;
Philipp Reisner08b165b2011-09-05 16:22:33 +02003025 if (!test_bit(DISCARD_MY_DATA, &mdev->flags) && (mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003026 hg = 1;
3027
3028 if (abs(hg) < 100)
3029 dev_warn(DEV, "Split-Brain detected, manually solved. "
3030 "Sync from %s node\n",
3031 (hg < 0) ? "peer" : "this");
3032 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02003033 rr_conflict = nc->rr_conflict;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02003034 tentative = nc->tentative;
Philipp Reisner44ed1672011-04-19 17:10:19 +02003035 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07003036
3037 if (hg == -100) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01003038 /* FIXME this log message is not correct if we end up here
3039 * after an attempted attach on a diskless node.
3040 * We just refuse to attach -- well, we drop the "connection"
3041 * to that disk, in a way... */
Adam Gandelman3a11a482010-04-08 16:48:23 -07003042 dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003043 drbd_khelper(mdev, "split-brain");
3044 return C_MASK;
3045 }
3046
3047 if (hg > 0 && mydisk <= D_INCONSISTENT) {
3048 dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
3049 return C_MASK;
3050 }
3051
3052 if (hg < 0 && /* by intention we do not use mydisk here. */
3053 mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02003054 switch (rr_conflict) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003055 case ASB_CALL_HELPER:
3056 drbd_khelper(mdev, "pri-lost");
3057 /* fall through */
3058 case ASB_DISCONNECT:
3059 dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
3060 return C_MASK;
3061 case ASB_VIOLENTLY:
3062 dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
3063 "assumption\n");
3064 }
3065 }
3066
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02003067 if (tentative || test_bit(CONN_DRY_RUN, &mdev->tconn->flags)) {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003068 if (hg == 0)
3069 dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
3070 else
3071 dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
3072 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
3073 abs(hg) >= 2 ? "full" : "bit-map based");
3074 return C_MASK;
3075 }
3076
Philipp Reisnerb411b362009-09-25 16:07:19 -07003077 if (abs(hg) >= 2) {
3078 dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003079 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake",
3080 BM_LOCKED_SET_ALLOWED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003081 return C_MASK;
3082 }
3083
3084 if (hg > 0) { /* become sync source. */
3085 rv = C_WF_BITMAP_S;
3086 } else if (hg < 0) { /* become sync target */
3087 rv = C_WF_BITMAP_T;
3088 } else {
3089 rv = C_CONNECTED;
3090 if (drbd_bm_total_weight(mdev)) {
3091 dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
3092 drbd_bm_total_weight(mdev));
3093 }
3094 }
3095
3096 return rv;
3097}
3098
Philipp Reisnerf179d762011-05-16 17:31:47 +02003099static enum drbd_after_sb_p convert_after_sb(enum drbd_after_sb_p peer)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003100{
3101 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003102 if (peer == ASB_DISCARD_REMOTE)
3103 return ASB_DISCARD_LOCAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003104
3105 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003106 if (peer == ASB_DISCARD_LOCAL)
3107 return ASB_DISCARD_REMOTE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003108
3109 /* everything else is valid if they are equal on both sides. */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003110 return peer;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003111}
3112
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003113static int receive_protocol(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003114{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003115 struct p_protocol *p = pi->data;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003116 enum drbd_after_sb_p p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
3117 int p_proto, p_discard_my_data, p_two_primaries, cf;
3118 struct net_conf *nc, *old_net_conf, *new_net_conf = NULL;
3119 char integrity_alg[SHARED_SECRET_MAX] = "";
Andreas Gruenbacheraccdbcc2011-07-15 17:41:09 +02003120 struct crypto_hash *peer_integrity_tfm = NULL;
Philipp Reisner7aca6c72011-05-17 10:12:56 +02003121 void *int_dig_in = NULL, *int_dig_vv = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003122
Philipp Reisnerb411b362009-09-25 16:07:19 -07003123 p_proto = be32_to_cpu(p->protocol);
3124 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
3125 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
3126 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003127 p_two_primaries = be32_to_cpu(p->two_primaries);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003128 cf = be32_to_cpu(p->conn_flags);
Andreas Gruenbacher6139f602011-05-06 20:00:02 +02003129 p_discard_my_data = cf & CF_DISCARD_MY_DATA;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003130
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003131 if (tconn->agreed_pro_version >= 87) {
3132 int err;
3133
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003134 if (pi->size > sizeof(integrity_alg))
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003135 return -EIO;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003136 err = drbd_recv_all(tconn, integrity_alg, pi->size);
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003137 if (err)
3138 return err;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003139 integrity_alg[SHARED_SECRET_MAX - 1] = 0;
3140 }
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003141
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003142 if (pi->cmd != P_PROTOCOL_UPDATE) {
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003143 clear_bit(CONN_DRY_RUN, &tconn->flags);
Philipp Reisner036b17e2011-05-16 17:38:11 +02003144
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003145 if (cf & CF_DRY_RUN)
3146 set_bit(CONN_DRY_RUN, &tconn->flags);
3147
3148 rcu_read_lock();
3149 nc = rcu_dereference(tconn->net_conf);
3150
3151 if (p_proto != nc->wire_protocol) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003152 conn_err(tconn, "incompatible %s settings\n", "protocol");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003153 goto disconnect_rcu_unlock;
3154 }
3155
3156 if (convert_after_sb(p_after_sb_0p) != nc->after_sb_0p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003157 conn_err(tconn, "incompatible %s settings\n", "after-sb-0pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003158 goto disconnect_rcu_unlock;
3159 }
3160
3161 if (convert_after_sb(p_after_sb_1p) != nc->after_sb_1p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003162 conn_err(tconn, "incompatible %s settings\n", "after-sb-1pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003163 goto disconnect_rcu_unlock;
3164 }
3165
3166 if (convert_after_sb(p_after_sb_2p) != nc->after_sb_2p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003167 conn_err(tconn, "incompatible %s settings\n", "after-sb-2pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003168 goto disconnect_rcu_unlock;
3169 }
3170
3171 if (p_discard_my_data && nc->discard_my_data) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003172 conn_err(tconn, "incompatible %s settings\n", "discard-my-data");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003173 goto disconnect_rcu_unlock;
3174 }
3175
3176 if (p_two_primaries != nc->two_primaries) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003177 conn_err(tconn, "incompatible %s settings\n", "allow-two-primaries");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003178 goto disconnect_rcu_unlock;
3179 }
3180
3181 if (strcmp(integrity_alg, nc->integrity_alg)) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003182 conn_err(tconn, "incompatible %s settings\n", "data-integrity-alg");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003183 goto disconnect_rcu_unlock;
3184 }
3185
3186 rcu_read_unlock();
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003187 }
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003188
3189 if (integrity_alg[0]) {
3190 int hash_size;
3191
3192 /*
3193 * We can only change the peer data integrity algorithm
3194 * here. Changing our own data integrity algorithm
3195 * requires that we send a P_PROTOCOL_UPDATE packet at
3196 * the same time; otherwise, the peer has no way to
3197 * tell between which packets the algorithm should
3198 * change.
3199 */
3200
3201 peer_integrity_tfm = crypto_alloc_hash(integrity_alg, 0, CRYPTO_ALG_ASYNC);
3202 if (!peer_integrity_tfm) {
3203 conn_err(tconn, "peer data-integrity-alg %s not supported\n",
3204 integrity_alg);
3205 goto disconnect;
3206 }
3207
3208 hash_size = crypto_hash_digestsize(peer_integrity_tfm);
3209 int_dig_in = kmalloc(hash_size, GFP_KERNEL);
3210 int_dig_vv = kmalloc(hash_size, GFP_KERNEL);
3211 if (!(int_dig_in && int_dig_vv)) {
3212 conn_err(tconn, "Allocation of buffers for data integrity checking failed\n");
3213 goto disconnect;
3214 }
3215 }
3216
3217 new_net_conf = kmalloc(sizeof(struct net_conf), GFP_KERNEL);
3218 if (!new_net_conf) {
3219 conn_err(tconn, "Allocation of new net_conf failed\n");
3220 goto disconnect;
3221 }
3222
3223 mutex_lock(&tconn->data.mutex);
3224 mutex_lock(&tconn->conf_update);
3225 old_net_conf = tconn->net_conf;
3226 *new_net_conf = *old_net_conf;
3227
3228 new_net_conf->wire_protocol = p_proto;
3229 new_net_conf->after_sb_0p = convert_after_sb(p_after_sb_0p);
3230 new_net_conf->after_sb_1p = convert_after_sb(p_after_sb_1p);
3231 new_net_conf->after_sb_2p = convert_after_sb(p_after_sb_2p);
3232 new_net_conf->two_primaries = p_two_primaries;
3233
3234 rcu_assign_pointer(tconn->net_conf, new_net_conf);
3235 mutex_unlock(&tconn->conf_update);
3236 mutex_unlock(&tconn->data.mutex);
3237
3238 crypto_free_hash(tconn->peer_integrity_tfm);
3239 kfree(tconn->int_dig_in);
3240 kfree(tconn->int_dig_vv);
3241 tconn->peer_integrity_tfm = peer_integrity_tfm;
3242 tconn->int_dig_in = int_dig_in;
3243 tconn->int_dig_vv = int_dig_vv;
3244
3245 if (strcmp(old_net_conf->integrity_alg, integrity_alg))
3246 conn_info(tconn, "peer data-integrity-alg: %s\n",
3247 integrity_alg[0] ? integrity_alg : "(none)");
3248
3249 synchronize_rcu();
3250 kfree(old_net_conf);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003251 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003252
Philipp Reisner44ed1672011-04-19 17:10:19 +02003253disconnect_rcu_unlock:
3254 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07003255disconnect:
Andreas Gruenbacherb792c352011-07-15 16:48:49 +02003256 crypto_free_hash(peer_integrity_tfm);
Philipp Reisner036b17e2011-05-16 17:38:11 +02003257 kfree(int_dig_in);
3258 kfree(int_dig_vv);
Philipp Reisner7204624c2011-03-15 18:51:47 +01003259 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003260 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003261}
3262
3263/* helper function
3264 * input: alg name, feature name
3265 * return: NULL (alg name was "")
3266 * ERR_PTR(error) if something goes wrong
3267 * or the crypto hash ptr, if it worked out ok. */
3268struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
3269 const char *alg, const char *name)
3270{
3271 struct crypto_hash *tfm;
3272
3273 if (!alg[0])
3274 return NULL;
3275
3276 tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
3277 if (IS_ERR(tfm)) {
3278 dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
3279 alg, name, PTR_ERR(tfm));
3280 return tfm;
3281 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003282 return tfm;
3283}
3284
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003285static int ignore_remaining_packet(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003286{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003287 void *buffer = tconn->data.rbuf;
3288 int size = pi->size;
3289
3290 while (size) {
3291 int s = min_t(int, size, DRBD_SOCKET_BUFFER_SIZE);
3292 s = drbd_recv(tconn, buffer, s);
3293 if (s <= 0) {
3294 if (s < 0)
3295 return s;
3296 break;
3297 }
3298 size -= s;
3299 }
3300 if (size)
3301 return -EIO;
3302 return 0;
3303}
3304
3305/*
3306 * config_unknown_volume - device configuration command for unknown volume
3307 *
3308 * When a device is added to an existing connection, the node on which the
3309 * device is added first will send configuration commands to its peer but the
3310 * peer will not know about the device yet. It will warn and ignore these
3311 * commands. Once the device is added on the second node, the second node will
3312 * send the same device configuration commands, but in the other direction.
3313 *
3314 * (We can also end up here if drbd is misconfigured.)
3315 */
3316static int config_unknown_volume(struct drbd_tconn *tconn, struct packet_info *pi)
3317{
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02003318 conn_warn(tconn, "%s packet received for volume %u, which is not configured locally\n",
3319 cmdname(pi->cmd), pi->vnr);
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003320 return ignore_remaining_packet(tconn, pi);
3321}
3322
3323static int receive_SyncParam(struct drbd_tconn *tconn, struct packet_info *pi)
3324{
3325 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003326 struct p_rs_param_95 *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003327 unsigned int header_size, data_size, exp_max_sz;
3328 struct crypto_hash *verify_tfm = NULL;
3329 struct crypto_hash *csums_tfm = NULL;
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003330 struct net_conf *old_net_conf, *new_net_conf = NULL;
Philipp Reisner813472c2011-05-03 16:47:02 +02003331 struct disk_conf *old_disk_conf = NULL, *new_disk_conf = NULL;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003332 const int apv = tconn->agreed_pro_version;
Philipp Reisner813472c2011-05-03 16:47:02 +02003333 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
Philipp Reisner778f2712010-07-06 11:14:00 +02003334 int fifo_size = 0;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003335 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003336
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003337 mdev = vnr_to_mdev(tconn, pi->vnr);
3338 if (!mdev)
3339 return config_unknown_volume(tconn, pi);
3340
Philipp Reisnerb411b362009-09-25 16:07:19 -07003341 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
3342 : apv == 88 ? sizeof(struct p_rs_param)
3343 + SHARED_SECRET_MAX
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003344 : apv <= 94 ? sizeof(struct p_rs_param_89)
3345 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003346
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003347 if (pi->size > exp_max_sz) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003348 dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003349 pi->size, exp_max_sz);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003350 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003351 }
3352
3353 if (apv <= 88) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003354 header_size = sizeof(struct p_rs_param);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003355 data_size = pi->size - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003356 } else if (apv <= 94) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003357 header_size = sizeof(struct p_rs_param_89);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003358 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003359 D_ASSERT(data_size == 0);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003360 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003361 header_size = sizeof(struct p_rs_param_95);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003362 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003363 D_ASSERT(data_size == 0);
3364 }
3365
3366 /* initialize verify_alg and csums_alg */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003367 p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003368 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
3369
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003370 err = drbd_recv_all(mdev->tconn, p, header_size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003371 if (err)
3372 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003373
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003374 mutex_lock(&mdev->tconn->conf_update);
3375 old_net_conf = mdev->tconn->net_conf;
Philipp Reisner813472c2011-05-03 16:47:02 +02003376 if (get_ldev(mdev)) {
3377 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3378 if (!new_disk_conf) {
3379 put_ldev(mdev);
3380 mutex_unlock(&mdev->tconn->conf_update);
3381 dev_err(DEV, "Allocation of new disk_conf failed\n");
3382 return -ENOMEM;
3383 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003384
Philipp Reisner813472c2011-05-03 16:47:02 +02003385 old_disk_conf = mdev->ldev->disk_conf;
3386 *new_disk_conf = *old_disk_conf;
3387
Andreas Gruenbacher6394b932011-05-11 14:29:52 +02003388 new_disk_conf->resync_rate = be32_to_cpu(p->resync_rate);
Philipp Reisner813472c2011-05-03 16:47:02 +02003389 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003390
Philipp Reisnerb411b362009-09-25 16:07:19 -07003391 if (apv >= 88) {
3392 if (apv == 88) {
Philipp Reisnere4bad1b2012-04-06 12:08:51 +02003393 if (data_size > SHARED_SECRET_MAX || data_size == 0) {
3394 dev_err(DEV, "verify-alg of wrong size, "
3395 "peer wants %u, accepting only up to %u byte\n",
3396 data_size, SHARED_SECRET_MAX);
Philipp Reisner813472c2011-05-03 16:47:02 +02003397 err = -EIO;
3398 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003399 }
3400
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003401 err = drbd_recv_all(mdev->tconn, p->verify_alg, data_size);
Philipp Reisner813472c2011-05-03 16:47:02 +02003402 if (err)
3403 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003404 /* we expect NUL terminated string */
3405 /* but just in case someone tries to be evil */
3406 D_ASSERT(p->verify_alg[data_size-1] == 0);
3407 p->verify_alg[data_size-1] = 0;
3408
3409 } else /* apv >= 89 */ {
3410 /* we still expect NUL terminated strings */
3411 /* but just in case someone tries to be evil */
3412 D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
3413 D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
3414 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
3415 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
3416 }
3417
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003418 if (strcmp(old_net_conf->verify_alg, p->verify_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003419 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3420 dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003421 old_net_conf->verify_alg, p->verify_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003422 goto disconnect;
3423 }
3424 verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
3425 p->verify_alg, "verify-alg");
3426 if (IS_ERR(verify_tfm)) {
3427 verify_tfm = NULL;
3428 goto disconnect;
3429 }
3430 }
3431
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003432 if (apv >= 89 && strcmp(old_net_conf->csums_alg, p->csums_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003433 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3434 dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003435 old_net_conf->csums_alg, p->csums_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003436 goto disconnect;
3437 }
3438 csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
3439 p->csums_alg, "csums-alg");
3440 if (IS_ERR(csums_tfm)) {
3441 csums_tfm = NULL;
3442 goto disconnect;
3443 }
3444 }
3445
Philipp Reisner813472c2011-05-03 16:47:02 +02003446 if (apv > 94 && new_disk_conf) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003447 new_disk_conf->c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
3448 new_disk_conf->c_delay_target = be32_to_cpu(p->c_delay_target);
3449 new_disk_conf->c_fill_target = be32_to_cpu(p->c_fill_target);
3450 new_disk_conf->c_max_rate = be32_to_cpu(p->c_max_rate);
Philipp Reisner778f2712010-07-06 11:14:00 +02003451
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003452 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Philipp Reisner9958c852011-05-03 16:19:31 +02003453 if (fifo_size != mdev->rs_plan_s->size) {
Philipp Reisner813472c2011-05-03 16:47:02 +02003454 new_plan = fifo_alloc(fifo_size);
3455 if (!new_plan) {
Philipp Reisner778f2712010-07-06 11:14:00 +02003456 dev_err(DEV, "kmalloc of fifo_buffer failed");
Lars Ellenbergf3990022011-03-23 14:31:09 +01003457 put_ldev(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02003458 goto disconnect;
3459 }
3460 }
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003461 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003462
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003463 if (verify_tfm || csums_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003464 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
3465 if (!new_net_conf) {
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003466 dev_err(DEV, "Allocation of new net_conf failed\n");
3467 goto disconnect;
3468 }
3469
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003470 *new_net_conf = *old_net_conf;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003471
3472 if (verify_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003473 strcpy(new_net_conf->verify_alg, p->verify_alg);
3474 new_net_conf->verify_alg_len = strlen(p->verify_alg) + 1;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003475 crypto_free_hash(mdev->tconn->verify_tfm);
3476 mdev->tconn->verify_tfm = verify_tfm;
3477 dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
3478 }
3479 if (csums_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003480 strcpy(new_net_conf->csums_alg, p->csums_alg);
3481 new_net_conf->csums_alg_len = strlen(p->csums_alg) + 1;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003482 crypto_free_hash(mdev->tconn->csums_tfm);
3483 mdev->tconn->csums_tfm = csums_tfm;
3484 dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
3485 }
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003486 rcu_assign_pointer(tconn->net_conf, new_net_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003487 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003488 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003489
Philipp Reisner813472c2011-05-03 16:47:02 +02003490 if (new_disk_conf) {
3491 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3492 put_ldev(mdev);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003493 }
Philipp Reisner813472c2011-05-03 16:47:02 +02003494
3495 if (new_plan) {
3496 old_plan = mdev->rs_plan_s;
3497 rcu_assign_pointer(mdev->rs_plan_s, new_plan);
3498 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003499
3500 mutex_unlock(&mdev->tconn->conf_update);
3501 synchronize_rcu();
3502 if (new_net_conf)
3503 kfree(old_net_conf);
3504 kfree(old_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02003505 kfree(old_plan);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003506
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003507 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003508
Philipp Reisner813472c2011-05-03 16:47:02 +02003509reconnect:
3510 if (new_disk_conf) {
3511 put_ldev(mdev);
3512 kfree(new_disk_conf);
3513 }
3514 mutex_unlock(&mdev->tconn->conf_update);
3515 return -EIO;
3516
Philipp Reisnerb411b362009-09-25 16:07:19 -07003517disconnect:
Philipp Reisner813472c2011-05-03 16:47:02 +02003518 kfree(new_plan);
3519 if (new_disk_conf) {
3520 put_ldev(mdev);
3521 kfree(new_disk_conf);
3522 }
Philipp Reisnera0095502011-05-03 13:14:15 +02003523 mutex_unlock(&mdev->tconn->conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003524 /* just for completeness: actually not needed,
3525 * as this is not reached if csums_tfm was ok. */
3526 crypto_free_hash(csums_tfm);
3527 /* but free the verify_tfm again, if csums_tfm did not work out */
3528 crypto_free_hash(verify_tfm);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003529 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003530 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003531}
3532
Philipp Reisnerb411b362009-09-25 16:07:19 -07003533/* warn if the arguments differ by more than 12.5% */
3534static void warn_if_differ_considerably(struct drbd_conf *mdev,
3535 const char *s, sector_t a, sector_t b)
3536{
3537 sector_t d;
3538 if (a == 0 || b == 0)
3539 return;
3540 d = (a > b) ? (a - b) : (b - a);
3541 if (d > (a>>3) || d > (b>>3))
3542 dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
3543 (unsigned long long)a, (unsigned long long)b);
3544}
3545
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003546static int receive_sizes(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003547{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003548 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003549 struct p_sizes *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003550 enum determine_dev_size dd = unchanged;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003551 sector_t p_size, p_usize, my_usize;
3552 int ldsc = 0; /* local disk size changed */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003553 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003554
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003555 mdev = vnr_to_mdev(tconn, pi->vnr);
3556 if (!mdev)
3557 return config_unknown_volume(tconn, pi);
3558
Philipp Reisnerb411b362009-09-25 16:07:19 -07003559 p_size = be64_to_cpu(p->d_size);
3560 p_usize = be64_to_cpu(p->u_size);
3561
Philipp Reisnerb411b362009-09-25 16:07:19 -07003562 /* just store the peer's disk size for now.
3563 * we still need to figure out whether we accept that. */
3564 mdev->p_size = p_size;
3565
Philipp Reisnerb411b362009-09-25 16:07:19 -07003566 if (get_ldev(mdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003567 rcu_read_lock();
3568 my_usize = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
3569 rcu_read_unlock();
3570
Philipp Reisnerb411b362009-09-25 16:07:19 -07003571 warn_if_differ_considerably(mdev, "lower level device sizes",
3572 p_size, drbd_get_max_capacity(mdev->ldev));
3573 warn_if_differ_considerably(mdev, "user requested size",
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003574 p_usize, my_usize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003575
3576 /* if this is the first connect, or an otherwise expected
3577 * param exchange, choose the minimum */
3578 if (mdev->state.conn == C_WF_REPORT_PARAMS)
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003579 p_usize = min_not_zero(my_usize, p_usize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003580
3581 /* Never shrink a device with usable data during connect.
3582 But allow online shrinking if we are connected. */
Philipp Reisneref5e44a2011-05-03 13:27:43 +02003583 if (drbd_new_dev_size(mdev, mdev->ldev, p_usize, 0) <
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003584 drbd_get_capacity(mdev->this_bdev) &&
3585 mdev->state.disk >= D_OUTDATED &&
3586 mdev->state.conn < C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003587 dev_err(DEV, "The peer's disk size is too small!\n");
Philipp Reisner38fa9982011-03-15 18:24:49 +01003588 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003589 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003590 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003591 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003592
3593 if (my_usize != p_usize) {
3594 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
3595
3596 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3597 if (!new_disk_conf) {
3598 dev_err(DEV, "Allocation of new disk_conf failed\n");
3599 put_ldev(mdev);
3600 return -ENOMEM;
3601 }
3602
3603 mutex_lock(&mdev->tconn->conf_update);
3604 old_disk_conf = mdev->ldev->disk_conf;
3605 *new_disk_conf = *old_disk_conf;
3606 new_disk_conf->disk_size = p_usize;
3607
3608 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3609 mutex_unlock(&mdev->tconn->conf_update);
3610 synchronize_rcu();
3611 kfree(old_disk_conf);
3612
3613 dev_info(DEV, "Peer sets u_size to %lu sectors\n",
3614 (unsigned long)my_usize);
3615 }
3616
Philipp Reisnerb411b362009-09-25 16:07:19 -07003617 put_ldev(mdev);
3618 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003619
Philipp Reisnere89b5912010-03-24 17:11:33 +01003620 ddsf = be16_to_cpu(p->dds_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003621 if (get_ldev(mdev)) {
Bart Van Assche24c48302011-05-21 18:32:29 +02003622 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003623 put_ldev(mdev);
3624 if (dd == dev_size_error)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003625 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003626 drbd_md_sync(mdev);
3627 } else {
3628 /* I am diskless, need to accept the peer's size. */
3629 drbd_set_my_capacity(mdev, p_size);
3630 }
3631
Philipp Reisner99432fc2011-05-20 16:39:13 +02003632 mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size);
3633 drbd_reconsider_max_bio_size(mdev);
3634
Philipp Reisnerb411b362009-09-25 16:07:19 -07003635 if (get_ldev(mdev)) {
3636 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
3637 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
3638 ldsc = 1;
3639 }
3640
Philipp Reisnerb411b362009-09-25 16:07:19 -07003641 put_ldev(mdev);
3642 }
3643
3644 if (mdev->state.conn > C_WF_REPORT_PARAMS) {
3645 if (be64_to_cpu(p->c_size) !=
3646 drbd_get_capacity(mdev->this_bdev) || ldsc) {
3647 /* we have different sizes, probably peer
3648 * needs to know my new size... */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003649 drbd_send_sizes(mdev, 0, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003650 }
3651 if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
3652 (dd == grew && mdev->state.conn == C_CONNECTED)) {
3653 if (mdev->state.pdsk >= D_INCONSISTENT &&
Philipp Reisnere89b5912010-03-24 17:11:33 +01003654 mdev->state.disk >= D_INCONSISTENT) {
3655 if (ddsf & DDSF_NO_RESYNC)
3656 dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
3657 else
3658 resync_after_online_grow(mdev);
3659 } else
Philipp Reisnerb411b362009-09-25 16:07:19 -07003660 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
3661 }
3662 }
3663
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003664 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003665}
3666
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003667static int receive_uuids(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003668{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003669 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003670 struct p_uuids *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003671 u64 *p_uuid;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003672 int i, updated_uuids = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003673
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003674 mdev = vnr_to_mdev(tconn, pi->vnr);
3675 if (!mdev)
3676 return config_unknown_volume(tconn, pi);
3677
Philipp Reisnerb411b362009-09-25 16:07:19 -07003678 p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
3679
3680 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
3681 p_uuid[i] = be64_to_cpu(p->uuid[i]);
3682
3683 kfree(mdev->p_uuid);
3684 mdev->p_uuid = p_uuid;
3685
3686 if (mdev->state.conn < C_CONNECTED &&
3687 mdev->state.disk < D_INCONSISTENT &&
3688 mdev->state.role == R_PRIMARY &&
3689 (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
3690 dev_err(DEV, "Can only connect to data with current UUID=%016llX\n",
3691 (unsigned long long)mdev->ed_uuid);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003692 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003693 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003694 }
3695
3696 if (get_ldev(mdev)) {
3697 int skip_initial_sync =
3698 mdev->state.conn == C_CONNECTED &&
Philipp Reisner31890f42011-01-19 14:12:51 +01003699 mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003700 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
3701 (p_uuid[UI_FLAGS] & 8);
3702 if (skip_initial_sync) {
3703 dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n");
3704 drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003705 "clear_n_write from receive_uuids",
3706 BM_LOCKED_TEST_ALLOWED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003707 _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]);
3708 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3709 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3710 CS_VERBOSE, NULL);
3711 drbd_md_sync(mdev);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003712 updated_uuids = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003713 }
3714 put_ldev(mdev);
Philipp Reisner18a50fa2010-06-21 14:14:15 +02003715 } else if (mdev->state.disk < D_INCONSISTENT &&
3716 mdev->state.role == R_PRIMARY) {
3717 /* I am a diskless primary, the peer just created a new current UUID
3718 for me. */
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003719 updated_uuids = drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003720 }
3721
3722 /* Before we test for the disk state, we should wait until an eventually
3723 ongoing cluster wide state change is finished. That is important if
3724 we are primary and are detaching from our disk. We need to see the
3725 new disk state... */
Philipp Reisner8410da82011-02-11 20:11:10 +01003726 mutex_lock(mdev->state_mutex);
3727 mutex_unlock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003728 if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT)
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003729 updated_uuids |= drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
3730
3731 if (updated_uuids)
3732 drbd_print_uuids(mdev, "receiver updated UUIDs to");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003733
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003734 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003735}
3736
3737/**
3738 * convert_state() - Converts the peer's view of the cluster state to our point of view
3739 * @ps: The state as seen by the peer.
3740 */
3741static union drbd_state convert_state(union drbd_state ps)
3742{
3743 union drbd_state ms;
3744
3745 static enum drbd_conns c_tab[] = {
Philipp Reisner369bea62011-07-06 23:04:44 +02003746 [C_WF_REPORT_PARAMS] = C_WF_REPORT_PARAMS,
Philipp Reisnerb411b362009-09-25 16:07:19 -07003747 [C_CONNECTED] = C_CONNECTED,
3748
3749 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
3750 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
3751 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
3752 [C_VERIFY_S] = C_VERIFY_T,
3753 [C_MASK] = C_MASK,
3754 };
3755
3756 ms.i = ps.i;
3757
3758 ms.conn = c_tab[ps.conn];
3759 ms.peer = ps.role;
3760 ms.role = ps.peer;
3761 ms.pdsk = ps.disk;
3762 ms.disk = ps.pdsk;
3763 ms.peer_isp = (ps.aftr_isp | ps.user_isp);
3764
3765 return ms;
3766}
3767
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003768static int receive_req_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003769{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003770 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003771 struct p_req_state *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003772 union drbd_state mask, val;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01003773 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003774
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003775 mdev = vnr_to_mdev(tconn, pi->vnr);
3776 if (!mdev)
3777 return -EIO;
3778
Philipp Reisnerb411b362009-09-25 16:07:19 -07003779 mask.i = be32_to_cpu(p->mask);
3780 val.i = be32_to_cpu(p->val);
3781
Lars Ellenberg427c0432012-08-01 12:43:01 +02003782 if (test_bit(RESOLVE_CONFLICTS, &mdev->tconn->flags) &&
Philipp Reisner8410da82011-02-11 20:11:10 +01003783 mutex_is_locked(mdev->state_mutex)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003784 drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003785 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003786 }
3787
3788 mask = convert_state(mask);
3789 val = convert_state(val);
3790
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003791 rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
3792 drbd_send_sr_reply(mdev, rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003793
Philipp Reisnerb411b362009-09-25 16:07:19 -07003794 drbd_md_sync(mdev);
3795
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003796 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003797}
3798
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003799static int receive_req_conn_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003800{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003801 struct p_req_state *p = pi->data;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003802 union drbd_state mask, val;
3803 enum drbd_state_rv rv;
3804
3805 mask.i = be32_to_cpu(p->mask);
3806 val.i = be32_to_cpu(p->val);
3807
Lars Ellenberg427c0432012-08-01 12:43:01 +02003808 if (test_bit(RESOLVE_CONFLICTS, &tconn->flags) &&
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003809 mutex_is_locked(&tconn->cstate_mutex)) {
3810 conn_send_sr_reply(tconn, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003811 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003812 }
3813
3814 mask = convert_state(mask);
3815 val = convert_state(val);
3816
Philipp Reisner778bcf22011-03-28 12:55:03 +02003817 rv = conn_request_state(tconn, mask, val, CS_VERBOSE | CS_LOCAL_ONLY | CS_IGN_OUTD_FAIL);
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003818 conn_send_sr_reply(tconn, rv);
3819
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003820 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003821}
3822
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003823static int receive_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003824{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003825 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003826 struct p_state *p = pi->data;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003827 union drbd_state os, ns, peer_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003828 enum drbd_disk_state real_peer_disk;
Philipp Reisner65d922c2010-06-16 16:18:09 +02003829 enum chg_state_flags cs_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003830 int rv;
3831
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003832 mdev = vnr_to_mdev(tconn, pi->vnr);
3833 if (!mdev)
3834 return config_unknown_volume(tconn, pi);
3835
Philipp Reisnerb411b362009-09-25 16:07:19 -07003836 peer_state.i = be32_to_cpu(p->state);
3837
3838 real_peer_disk = peer_state.disk;
3839 if (peer_state.disk == D_NEGOTIATING) {
3840 real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
3841 dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
3842 }
3843
Philipp Reisner87eeee42011-01-19 14:16:30 +01003844 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003845 retry:
Philipp Reisner78bae592011-03-28 15:40:12 +02003846 os = ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003847 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003848
Philipp Reisnerb8853db2011-12-13 11:09:16 +01003849 /* If some other part of the code (asender thread, timeout)
3850 * already decided to close the connection again,
3851 * we must not "re-establish" it here. */
3852 if (os.conn <= C_TEAR_DOWN)
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003853 return -ECONNRESET;
Philipp Reisnerb8853db2011-12-13 11:09:16 +01003854
Philipp Reisner9bcd2522011-09-29 13:00:14 +02003855 /* If this is the "end of sync" confirmation, usually the peer disk
3856 * transitions from D_INCONSISTENT to D_UP_TO_DATE. For empty (0 bits
3857 * set) resync started in PausedSyncT, or if the timing of pause-/
3858 * unpause-sync events has been "just right", the peer disk may
3859 * transition from D_CONSISTENT to D_UP_TO_DATE as well.
3860 */
3861 if ((os.pdsk == D_INCONSISTENT || os.pdsk == D_CONSISTENT) &&
3862 real_peer_disk == D_UP_TO_DATE &&
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003863 os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) {
3864 /* If we are (becoming) SyncSource, but peer is still in sync
3865 * preparation, ignore its uptodate-ness to avoid flapping, it
3866 * will change to inconsistent once the peer reaches active
3867 * syncing states.
3868 * It may have changed syncer-paused flags, however, so we
3869 * cannot ignore this completely. */
3870 if (peer_state.conn > C_CONNECTED &&
3871 peer_state.conn < C_SYNC_SOURCE)
3872 real_peer_disk = D_INCONSISTENT;
3873
3874 /* if peer_state changes to connected at the same time,
3875 * it explicitly notifies us that it finished resync.
3876 * Maybe we should finish it up, too? */
3877 else if (os.conn >= C_SYNC_SOURCE &&
3878 peer_state.conn == C_CONNECTED) {
3879 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed)
3880 drbd_resync_finished(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003881 return 0;
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003882 }
3883 }
3884
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003885 /* explicit verify finished notification, stop sector reached. */
3886 if (os.conn == C_VERIFY_T && os.disk == D_UP_TO_DATE &&
3887 peer_state.conn == C_CONNECTED && real_peer_disk == D_UP_TO_DATE) {
3888 ov_out_of_sync_print(mdev);
3889 drbd_resync_finished(mdev);
3890 return 0;
3891 }
3892
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003893 /* peer says his disk is inconsistent, while we think it is uptodate,
3894 * and this happens while the peer still thinks we have a sync going on,
3895 * but we think we are already done with the sync.
3896 * We ignore this to avoid flapping pdsk.
3897 * This should not happen, if the peer is a recent version of drbd. */
3898 if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT &&
3899 os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE)
3900 real_peer_disk = D_UP_TO_DATE;
3901
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003902 if (ns.conn == C_WF_REPORT_PARAMS)
3903 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003904
Philipp Reisner67531712010-10-27 12:21:30 +02003905 if (peer_state.conn == C_AHEAD)
3906 ns.conn = C_BEHIND;
3907
Philipp Reisnerb411b362009-09-25 16:07:19 -07003908 if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING &&
3909 get_ldev_if_state(mdev, D_NEGOTIATING)) {
3910 int cr; /* consider resync */
3911
3912 /* if we established a new connection */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003913 cr = (os.conn < C_CONNECTED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003914 /* if we had an established connection
3915 * and one of the nodes newly attaches a disk */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003916 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003917 (peer_state.disk == D_NEGOTIATING ||
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003918 os.disk == D_NEGOTIATING));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003919 /* if we have both been inconsistent, and the peer has been
3920 * forced to be UpToDate with --overwrite-data */
3921 cr |= test_bit(CONSIDER_RESYNC, &mdev->flags);
3922 /* if we had been plain connected, and the admin requested to
3923 * start a sync by "invalidate" or "invalidate-remote" */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003924 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003925 (peer_state.conn >= C_STARTING_SYNC_S &&
3926 peer_state.conn <= C_WF_BITMAP_T));
3927
3928 if (cr)
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003929 ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003930
3931 put_ldev(mdev);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003932 if (ns.conn == C_MASK) {
3933 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003934 if (mdev->state.disk == D_NEGOTIATING) {
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003935 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003936 } else if (peer_state.disk == D_NEGOTIATING) {
3937 dev_err(DEV, "Disk attach process on the peer node was aborted.\n");
3938 peer_state.disk = D_DISKLESS;
Lars Ellenberg580b9762010-02-26 23:15:23 +01003939 real_peer_disk = D_DISKLESS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003940 } else {
Philipp Reisner8169e412011-03-15 18:40:27 +01003941 if (test_and_clear_bit(CONN_DRY_RUN, &mdev->tconn->flags))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003942 return -EIO;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003943 D_ASSERT(os.conn == C_WF_REPORT_PARAMS);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003944 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003945 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003946 }
3947 }
3948 }
3949
Philipp Reisner87eeee42011-01-19 14:16:30 +01003950 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner78bae592011-03-28 15:40:12 +02003951 if (os.i != drbd_read_state(mdev).i)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003952 goto retry;
3953 clear_bit(CONSIDER_RESYNC, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003954 ns.peer = peer_state.role;
3955 ns.pdsk = real_peer_disk;
3956 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003957 if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003958 ns.disk = mdev->new_state_tmp.disk;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003959 cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD);
Philipp Reisner2aebfab2011-03-28 16:48:11 +02003960 if (ns.pdsk == D_CONSISTENT && drbd_suspended(mdev) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED &&
Philipp Reisner481c6f52010-06-22 14:03:27 +02003961 test_bit(NEW_CUR_UUID, &mdev->flags)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01003962 /* Do not allow tl_restart(RESEND) for a rebooted peer. We can only allow this
Philipp Reisner481c6f52010-06-22 14:03:27 +02003963 for temporal network outages! */
Philipp Reisner87eeee42011-01-19 14:16:30 +01003964 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003965 dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01003966 tl_clear(mdev->tconn);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003967 drbd_uuid_new_current(mdev);
3968 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003969 conn_request_state(mdev->tconn, NS2(conn, C_PROTOCOL_ERROR, susp, 0), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003970 return -EIO;
Philipp Reisner481c6f52010-06-22 14:03:27 +02003971 }
Philipp Reisner65d922c2010-06-16 16:18:09 +02003972 rv = _drbd_set_state(mdev, ns, cs_flags, NULL);
Philipp Reisner78bae592011-03-28 15:40:12 +02003973 ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003974 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003975
3976 if (rv < SS_SUCCESS) {
Philipp Reisner38fa9982011-03-15 18:24:49 +01003977 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003978 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003979 }
3980
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003981 if (os.conn > C_WF_REPORT_PARAMS) {
3982 if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003983 peer_state.disk != D_NEGOTIATING ) {
3984 /* we want resync, peer has not yet decided to sync... */
3985 /* Nowadays only used when forcing a node into primary role and
3986 setting its disk to UpToDate with that */
3987 drbd_send_uuids(mdev);
Philipp Reisner43de7c82011-11-10 13:16:13 +01003988 drbd_send_current_state(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003989 }
3990 }
3991
Philipp Reisner08b165b2011-09-05 16:22:33 +02003992 clear_bit(DISCARD_MY_DATA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003993
3994 drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
3995
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003996 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003997}
3998
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003999static int receive_sync_uuid(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004000{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004001 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004002 struct p_rs_uuid *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004003
4004 mdev = vnr_to_mdev(tconn, pi->vnr);
4005 if (!mdev)
4006 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004007
4008 wait_event(mdev->misc_wait,
4009 mdev->state.conn == C_WF_SYNC_UUID ||
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02004010 mdev->state.conn == C_BEHIND ||
Philipp Reisnerb411b362009-09-25 16:07:19 -07004011 mdev->state.conn < C_CONNECTED ||
4012 mdev->state.disk < D_NEGOTIATING);
4013
4014 /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */
4015
Philipp Reisnerb411b362009-09-25 16:07:19 -07004016 /* Here the _drbd_uuid_ functions are right, current should
4017 _not_ be rotated into the history */
4018 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
4019 _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid));
4020 _drbd_uuid_set(mdev, UI_BITMAP, 0UL);
4021
Lars Ellenberg62b0da32011-01-20 13:25:21 +01004022 drbd_print_uuids(mdev, "updated sync uuid");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004023 drbd_start_resync(mdev, C_SYNC_TARGET);
4024
4025 put_ldev(mdev);
4026 } else
4027 dev_err(DEV, "Ignoring SyncUUID packet!\n");
4028
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004029 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004030}
4031
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004032/**
4033 * receive_bitmap_plain
4034 *
4035 * Return 0 when done, 1 when another iteration is needed, and a negative error
4036 * code upon failure.
4037 */
4038static int
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004039receive_bitmap_plain(struct drbd_conf *mdev, unsigned int size,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004040 unsigned long *p, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004041{
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004042 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE -
4043 drbd_header_size(mdev->tconn);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004044 unsigned int num_words = min_t(size_t, data_size / sizeof(*p),
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004045 c->bm_words - c->word_offset);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004046 unsigned int want = num_words * sizeof(*p);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004047 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004048
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004049 if (want != size) {
4050 dev_err(DEV, "%s:want (%u) != size (%u)\n", __func__, want, size);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004051 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004052 }
4053 if (want == 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004054 return 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004055 err = drbd_recv_all(mdev->tconn, p, want);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004056 if (err)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004057 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004058
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004059 drbd_bm_merge_lel(mdev, c->word_offset, num_words, p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004060
4061 c->word_offset += num_words;
4062 c->bit_offset = c->word_offset * BITS_PER_LONG;
4063 if (c->bit_offset > c->bm_bits)
4064 c->bit_offset = c->bm_bits;
4065
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004066 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004067}
4068
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004069static enum drbd_bitmap_code dcbp_get_code(struct p_compressed_bm *p)
4070{
4071 return (enum drbd_bitmap_code)(p->encoding & 0x0f);
4072}
4073
4074static int dcbp_get_start(struct p_compressed_bm *p)
4075{
4076 return (p->encoding & 0x80) != 0;
4077}
4078
4079static int dcbp_get_pad_bits(struct p_compressed_bm *p)
4080{
4081 return (p->encoding >> 4) & 0x7;
4082}
4083
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004084/**
4085 * recv_bm_rle_bits
4086 *
4087 * Return 0 when done, 1 when another iteration is needed, and a negative error
4088 * code upon failure.
4089 */
4090static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07004091recv_bm_rle_bits(struct drbd_conf *mdev,
4092 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01004093 struct bm_xfer_ctx *c,
4094 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004095{
4096 struct bitstream bs;
4097 u64 look_ahead;
4098 u64 rl;
4099 u64 tmp;
4100 unsigned long s = c->bit_offset;
4101 unsigned long e;
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004102 int toggle = dcbp_get_start(p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004103 int have;
4104 int bits;
4105
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004106 bitstream_init(&bs, p->code, len, dcbp_get_pad_bits(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004107
4108 bits = bitstream_get_bits(&bs, &look_ahead, 64);
4109 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004110 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004111
4112 for (have = bits; have > 0; s += rl, toggle = !toggle) {
4113 bits = vli_decode_bits(&rl, look_ahead);
4114 if (bits <= 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004115 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004116
4117 if (toggle) {
4118 e = s + rl -1;
4119 if (e >= c->bm_bits) {
4120 dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004121 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004122 }
4123 _drbd_bm_set_bits(mdev, s, e);
4124 }
4125
4126 if (have < bits) {
4127 dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
4128 have, bits, look_ahead,
4129 (unsigned int)(bs.cur.b - p->code),
4130 (unsigned int)bs.buf_len);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004131 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004132 }
4133 look_ahead >>= bits;
4134 have -= bits;
4135
4136 bits = bitstream_get_bits(&bs, &tmp, 64 - have);
4137 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004138 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004139 look_ahead |= tmp << have;
4140 have += bits;
4141 }
4142
4143 c->bit_offset = s;
4144 bm_xfer_ctx_bit_to_word_offset(c);
4145
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004146 return (s != c->bm_bits);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004147}
4148
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004149/**
4150 * decode_bitmap_c
4151 *
4152 * Return 0 when done, 1 when another iteration is needed, and a negative error
4153 * code upon failure.
4154 */
4155static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07004156decode_bitmap_c(struct drbd_conf *mdev,
4157 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01004158 struct bm_xfer_ctx *c,
4159 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004160{
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004161 if (dcbp_get_code(p) == RLE_VLI_Bits)
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004162 return recv_bm_rle_bits(mdev, p, c, len - sizeof(*p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004163
4164 /* other variants had been implemented for evaluation,
4165 * but have been dropped as this one turned out to be "best"
4166 * during all our tests. */
4167
4168 dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
Philipp Reisner38fa9982011-03-15 18:24:49 +01004169 conn_request_state(mdev->tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004170 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004171}
4172
4173void INFO_bm_xfer_stats(struct drbd_conf *mdev,
4174 const char *direction, struct bm_xfer_ctx *c)
4175{
4176 /* what would it take to transfer it "plaintext" */
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004177 unsigned int header_size = drbd_header_size(mdev->tconn);
4178 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE - header_size;
4179 unsigned int plain =
4180 header_size * (DIV_ROUND_UP(c->bm_words, data_size) + 1) +
4181 c->bm_words * sizeof(unsigned long);
4182 unsigned int total = c->bytes[0] + c->bytes[1];
4183 unsigned int r;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004184
4185 /* total can not be zero. but just in case: */
4186 if (total == 0)
4187 return;
4188
4189 /* don't report if not compressed */
4190 if (total >= plain)
4191 return;
4192
4193 /* total < plain. check for overflow, still */
4194 r = (total > UINT_MAX/1000) ? (total / (plain/1000))
4195 : (1000 * total / plain);
4196
4197 if (r > 1000)
4198 r = 1000;
4199
4200 r = 1000 - r;
4201 dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
4202 "total %u; compression: %u.%u%%\n",
4203 direction,
4204 c->bytes[1], c->packets[1],
4205 c->bytes[0], c->packets[0],
4206 total, r/10, r % 10);
4207}
4208
4209/* Since we are processing the bitfield from lower addresses to higher,
4210 it does not matter if the process it in 32 bit chunks or 64 bit
4211 chunks as long as it is little endian. (Understand it as byte stream,
4212 beginning with the lowest byte...) If we would use big endian
4213 we would need to process it from the highest address to the lowest,
4214 in order to be agnostic to the 32 vs 64 bits issue.
4215
4216 returns 0 on failure, 1 if we successfully received it. */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004217static int receive_bitmap(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004218{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004219 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004220 struct bm_xfer_ctx c;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004221 int err;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004222
4223 mdev = vnr_to_mdev(tconn, pi->vnr);
4224 if (!mdev)
4225 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004226
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004227 drbd_bm_lock(mdev, "receive bitmap", BM_LOCKED_SET_ALLOWED);
4228 /* you are supposed to send additional out-of-sync information
4229 * if you actually set bits during this phase */
Philipp Reisnerb411b362009-09-25 16:07:19 -07004230
Philipp Reisnerb411b362009-09-25 16:07:19 -07004231 c = (struct bm_xfer_ctx) {
4232 .bm_bits = drbd_bm_bits(mdev),
4233 .bm_words = drbd_bm_words(mdev),
4234 };
4235
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004236 for(;;) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004237 if (pi->cmd == P_BITMAP)
4238 err = receive_bitmap_plain(mdev, pi->size, pi->data, &c);
4239 else if (pi->cmd == P_COMPRESSED_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004240 /* MAYBE: sanity check that we speak proto >= 90,
4241 * and the feature is enabled! */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004242 struct p_compressed_bm *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004243
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004244 if (pi->size > DRBD_SOCKET_BUFFER_SIZE - drbd_header_size(tconn)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004245 dev_err(DEV, "ReportCBitmap packet too large\n");
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004246 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004247 goto out;
4248 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004249 if (pi->size <= sizeof(*p)) {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004250 dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004251 err = -EIO;
Andreas Gruenbacher78fcbda2010-12-10 22:18:27 +01004252 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004253 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004254 err = drbd_recv_all(mdev->tconn, p, pi->size);
4255 if (err)
4256 goto out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004257 err = decode_bitmap_c(mdev, p, &c, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004258 } else {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004259 dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", pi->cmd);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004260 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004261 goto out;
4262 }
4263
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004264 c.packets[pi->cmd == P_BITMAP]++;
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004265 c.bytes[pi->cmd == P_BITMAP] += drbd_header_size(tconn) + pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004266
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004267 if (err <= 0) {
4268 if (err < 0)
4269 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004270 break;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004271 }
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004272 err = drbd_recv_header(mdev->tconn, pi);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004273 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004274 goto out;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004275 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004276
4277 INFO_bm_xfer_stats(mdev, "receive", &c);
4278
4279 if (mdev->state.conn == C_WF_BITMAP_T) {
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01004280 enum drbd_state_rv rv;
4281
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004282 err = drbd_send_bitmap(mdev);
4283 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004284 goto out;
4285 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01004286 rv = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
4287 D_ASSERT(rv == SS_SUCCESS);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004288 } else if (mdev->state.conn != C_WF_BITMAP_S) {
4289 /* admin may have requested C_DISCONNECTING,
4290 * other threads may have noticed network errors */
4291 dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n",
4292 drbd_conn_str(mdev->state.conn));
4293 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004294 err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004295
Philipp Reisnerb411b362009-09-25 16:07:19 -07004296 out:
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004297 drbd_bm_unlock(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004298 if (!err && mdev->state.conn == C_WF_BITMAP_S)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004299 drbd_start_resync(mdev, C_SYNC_SOURCE);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004300 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004301}
4302
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004303static int receive_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004304{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004305 conn_warn(tconn, "skipping unknown optional packet type %d, l: %d!\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004306 pi->cmd, pi->size);
Philipp Reisner2de876e2011-03-15 14:38:01 +01004307
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004308 return ignore_remaining_packet(tconn, pi);
Philipp Reisner2de876e2011-03-15 14:38:01 +01004309}
4310
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004311static int receive_UnplugRemote(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004312{
Philipp Reisnerb411b362009-09-25 16:07:19 -07004313 /* Make sure we've acked all the TCP data associated
4314 * with the data requests being unplugged */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004315 drbd_tcp_quickack(tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004316
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004317 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004318}
4319
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004320static int receive_out_of_sync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner73a01a12010-10-27 14:33:00 +02004321{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004322 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004323 struct p_block_desc *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004324
4325 mdev = vnr_to_mdev(tconn, pi->vnr);
4326 if (!mdev)
4327 return -EIO;
Philipp Reisner73a01a12010-10-27 14:33:00 +02004328
Lars Ellenbergf735e3632010-12-17 21:06:18 +01004329 switch (mdev->state.conn) {
4330 case C_WF_SYNC_UUID:
4331 case C_WF_BITMAP_T:
4332 case C_BEHIND:
4333 break;
4334 default:
4335 dev_err(DEV, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n",
4336 drbd_conn_str(mdev->state.conn));
4337 }
4338
Philipp Reisner73a01a12010-10-27 14:33:00 +02004339 drbd_set_out_of_sync(mdev, be64_to_cpu(p->sector), be32_to_cpu(p->blksize));
4340
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004341 return 0;
Philipp Reisner73a01a12010-10-27 14:33:00 +02004342}
4343
Philipp Reisner02918be2010-08-20 14:35:10 +02004344struct data_cmd {
4345 int expect_payload;
4346 size_t pkt_size;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004347 int (*fn)(struct drbd_tconn *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004348};
4349
Philipp Reisner02918be2010-08-20 14:35:10 +02004350static struct data_cmd drbd_cmd_handler[] = {
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004351 [P_DATA] = { 1, sizeof(struct p_data), receive_Data },
4352 [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply },
4353 [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } ,
4354 [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } ,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004355 [P_BITMAP] = { 1, 0, receive_bitmap } ,
4356 [P_COMPRESSED_BITMAP] = { 1, 0, receive_bitmap } ,
4357 [P_UNPLUG_REMOTE] = { 0, 0, receive_UnplugRemote },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004358 [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4359 [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004360 [P_SYNC_PARAM] = { 1, 0, receive_SyncParam },
4361 [P_SYNC_PARAM89] = { 1, 0, receive_SyncParam },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004362 [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol },
4363 [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids },
4364 [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes },
4365 [P_STATE] = { 0, sizeof(struct p_state), receive_state },
4366 [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state },
4367 [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid },
4368 [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4369 [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4370 [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4371 [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip },
4372 [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync },
4373 [P_CONN_ST_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_conn_state },
Philipp Reisner036b17e2011-05-16 17:38:11 +02004374 [P_PROTOCOL_UPDATE] = { 1, sizeof(struct p_protocol), receive_protocol },
Philipp Reisner02918be2010-08-20 14:35:10 +02004375};
4376
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004377static void drbdd(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004378{
Philipp Reisner77351055b2011-02-07 17:24:26 +01004379 struct packet_info pi;
Philipp Reisner02918be2010-08-20 14:35:10 +02004380 size_t shs; /* sub header size */
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004381 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004382
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004383 while (get_t_state(&tconn->receiver) == RUNNING) {
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004384 struct data_cmd *cmd;
4385
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004386 drbd_thread_current_set_cpu(&tconn->receiver);
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004387 if (drbd_recv_header(tconn, &pi))
Philipp Reisner02918be2010-08-20 14:35:10 +02004388 goto err_out;
4389
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004390 cmd = &drbd_cmd_handler[pi.cmd];
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004391 if (unlikely(pi.cmd >= ARRAY_SIZE(drbd_cmd_handler) || !cmd->fn)) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004392 conn_err(tconn, "Unexpected data packet %s (0x%04x)",
4393 cmdname(pi.cmd), pi.cmd);
Philipp Reisner02918be2010-08-20 14:35:10 +02004394 goto err_out;
Lars Ellenberg0b33a912009-11-16 15:58:04 +01004395 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004396
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004397 shs = cmd->pkt_size;
4398 if (pi.size > shs && !cmd->expect_payload) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004399 conn_err(tconn, "No payload expected %s l:%d\n",
4400 cmdname(pi.cmd), pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004401 goto err_out;
4402 }
4403
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004404 if (shs) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004405 err = drbd_recv_all_warn(tconn, pi.data, shs);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004406 if (err)
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004407 goto err_out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004408 pi.size -= shs;
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004409 }
4410
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004411 err = cmd->fn(tconn, &pi);
4412 if (err) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004413 conn_err(tconn, "error receiving %s, e: %d l: %d!\n",
4414 cmdname(pi.cmd), err, pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004415 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004416 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004417 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004418 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004419
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004420 err_out:
4421 conn_request_state(tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004422}
4423
Philipp Reisner0e29d162011-02-18 14:23:11 +01004424void conn_flush_workqueue(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004425{
4426 struct drbd_wq_barrier barr;
4427
4428 barr.w.cb = w_prev_work_done;
Philipp Reisner0e29d162011-02-18 14:23:11 +01004429 barr.w.tconn = tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004430 init_completion(&barr.done);
Lars Ellenbergd5b27b02011-11-14 15:42:37 +01004431 drbd_queue_work(&tconn->sender_work, &barr.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004432 wait_for_completion(&barr.done);
4433}
4434
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004435static void conn_disconnect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004436{
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004437 struct drbd_conf *mdev;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004438 enum drbd_conns oc;
Philipp Reisner376694a2011-11-07 10:54:28 +01004439 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004440
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004441 if (tconn->cstate == C_STANDALONE)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004442 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004443
Philipp Reisnerb8853db2011-12-13 11:09:16 +01004444 /* We are about to start the cleanup after connection loss.
4445 * Make sure drbd_make_request knows about that.
4446 * Usually we should be in some network failure state already,
4447 * but just in case we are not, we fix it up here.
4448 */
4449 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
4450
Philipp Reisnerb411b362009-09-25 16:07:19 -07004451 /* asender does not clean up anything. it must not interfere, either */
Philipp Reisner360cc742011-02-08 14:29:53 +01004452 drbd_thread_stop(&tconn->asender);
4453 drbd_free_sock(tconn);
4454
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004455 rcu_read_lock();
4456 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
4457 kref_get(&mdev->kref);
4458 rcu_read_unlock();
4459 drbd_disconnected(mdev);
4460 kref_put(&mdev->kref, &drbd_minor_destroy);
4461 rcu_read_lock();
4462 }
4463 rcu_read_unlock();
4464
Philipp Reisner12038a32011-11-09 19:18:00 +01004465 if (!list_empty(&tconn->current_epoch->list))
4466 conn_err(tconn, "ASSERTION FAILED: tconn->current_epoch->list not empty\n");
4467 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
4468 atomic_set(&tconn->current_epoch->epoch_size, 0);
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01004469 tconn->send.seen_any_write_yet = false;
Philipp Reisner12038a32011-11-09 19:18:00 +01004470
Philipp Reisner360cc742011-02-08 14:29:53 +01004471 conn_info(tconn, "Connection closed\n");
4472
Philipp Reisnercb703452011-03-24 11:03:07 +01004473 if (conn_highest_role(tconn) == R_PRIMARY && conn_highest_pdsk(tconn) >= D_UNKNOWN)
4474 conn_try_outdate_peer_async(tconn);
4475
Philipp Reisner360cc742011-02-08 14:29:53 +01004476 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004477 oc = tconn->cstate;
4478 if (oc >= C_UNCONNECTED)
Philipp Reisner376694a2011-11-07 10:54:28 +01004479 _conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004480
Philipp Reisner360cc742011-02-08 14:29:53 +01004481 spin_unlock_irq(&tconn->req_lock);
4482
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02004483 if (oc == C_DISCONNECTING)
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +02004484 conn_request_state(tconn, NS(conn, C_STANDALONE), CS_VERBOSE | CS_HARD);
Philipp Reisner360cc742011-02-08 14:29:53 +01004485}
4486
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004487static int drbd_disconnected(struct drbd_conf *mdev)
Philipp Reisner360cc742011-02-08 14:29:53 +01004488{
Philipp Reisner360cc742011-02-08 14:29:53 +01004489 unsigned int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004490
Philipp Reisner85719572010-07-21 10:20:17 +02004491 /* wait for current activity to cease. */
Philipp Reisner87eeee42011-01-19 14:16:30 +01004492 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004493 _drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
4494 _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee);
4495 _drbd_wait_ee_list_empty(mdev, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004496 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004497
4498 /* We do not have data structures that would allow us to
4499 * get the rs_pending_cnt down to 0 again.
4500 * * On C_SYNC_TARGET we do not have any data structures describing
4501 * the pending RSDataRequest's we have sent.
4502 * * On C_SYNC_SOURCE there is no data structure that tracks
4503 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
4504 * And no, it is not the sum of the reference counts in the
4505 * resync_LRU. The resync_LRU tracks the whole operation including
4506 * the disk-IO, while the rs_pending_cnt only tracks the blocks
4507 * on the fly. */
4508 drbd_rs_cancel_all(mdev);
4509 mdev->rs_total = 0;
4510 mdev->rs_failed = 0;
4511 atomic_set(&mdev->rs_pending_cnt, 0);
4512 wake_up(&mdev->misc_wait);
4513
Philipp Reisnerb411b362009-09-25 16:07:19 -07004514 del_timer_sync(&mdev->resync_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004515 resync_timer_fn((unsigned long)mdev);
4516
Philipp Reisnerb411b362009-09-25 16:07:19 -07004517 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
4518 * w_make_resync_request etc. which may still be on the worker queue
4519 * to be "canceled" */
Philipp Reisnera21e9292011-02-08 15:08:49 +01004520 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004521
Andreas Gruenbachera990be42011-04-06 17:56:48 +02004522 drbd_finish_peer_reqs(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004523
Philipp Reisnerd10b4ea2011-11-30 23:25:36 +01004524 /* This second workqueue flush is necessary, since drbd_finish_peer_reqs()
4525 might have issued a work again. The one before drbd_finish_peer_reqs() is
4526 necessary to reclain net_ee in drbd_finish_peer_reqs(). */
4527 drbd_flush_workqueue(mdev);
4528
Philipp Reisnerb411b362009-09-25 16:07:19 -07004529 kfree(mdev->p_uuid);
4530 mdev->p_uuid = NULL;
4531
Philipp Reisner2aebfab2011-03-28 16:48:11 +02004532 if (!drbd_suspended(mdev))
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01004533 tl_clear(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004534
Philipp Reisnerb411b362009-09-25 16:07:19 -07004535 drbd_md_sync(mdev);
4536
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004537 /* serialize with bitmap writeout triggered by the state change,
4538 * if any. */
4539 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
4540
Philipp Reisnerb411b362009-09-25 16:07:19 -07004541 /* tcp_close and release of sendpage pages can be deferred. I don't
4542 * want to use SO_LINGER, because apparently it can be deferred for
4543 * more than 20 seconds (longest time I checked).
4544 *
4545 * Actually we don't care for exactly when the network stack does its
4546 * put_page(), but release our reference on these pages right here.
4547 */
Andreas Gruenbacher7721f562011-04-06 17:14:02 +02004548 i = drbd_free_peer_reqs(mdev, &mdev->net_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004549 if (i)
4550 dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
Lars Ellenberg435f0742010-09-06 12:30:25 +02004551 i = atomic_read(&mdev->pp_in_use_by_net);
4552 if (i)
4553 dev_info(DEV, "pp_in_use_by_net = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004554 i = atomic_read(&mdev->pp_in_use);
4555 if (i)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02004556 dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004557
4558 D_ASSERT(list_empty(&mdev->read_ee));
4559 D_ASSERT(list_empty(&mdev->active_ee));
4560 D_ASSERT(list_empty(&mdev->sync_ee));
4561 D_ASSERT(list_empty(&mdev->done_ee));
4562
Philipp Reisner360cc742011-02-08 14:29:53 +01004563 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004564}
4565
4566/*
4567 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
4568 * we can agree on is stored in agreed_pro_version.
4569 *
4570 * feature flags and the reserved array should be enough room for future
4571 * enhancements of the handshake protocol, and possible plugins...
4572 *
4573 * for now, they are expected to be zero, but ignored.
4574 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004575static int drbd_send_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004576{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004577 struct drbd_socket *sock;
4578 struct p_connection_features *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004579
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004580 sock = &tconn->data;
4581 p = conn_prepare_command(tconn, sock);
4582 if (!p)
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004583 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004584 memset(p, 0, sizeof(*p));
4585 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
4586 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004587 return conn_send_command(tconn, sock, P_CONNECTION_FEATURES, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004588}
4589
4590/*
4591 * return values:
4592 * 1 yes, we have a valid connection
4593 * 0 oops, did not work out, please try again
4594 * -1 peer talks different language,
4595 * no point in trying again, please go standalone.
4596 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004597static int drbd_do_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004598{
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004599 /* ASSERT current == tconn->receiver ... */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004600 struct p_connection_features *p;
4601 const int expect = sizeof(struct p_connection_features);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004602 struct packet_info pi;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004603 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004604
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004605 err = drbd_send_features(tconn);
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004606 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004607 return 0;
4608
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004609 err = drbd_recv_header(tconn, &pi);
4610 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004611 return 0;
4612
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004613 if (pi.cmd != P_CONNECTION_FEATURES) {
4614 conn_err(tconn, "expected ConnectionFeatures packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004615 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004616 return -1;
4617 }
4618
Philipp Reisner77351055b2011-02-07 17:24:26 +01004619 if (pi.size != expect) {
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004620 conn_err(tconn, "expected ConnectionFeatures length: %u, received: %u\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004621 expect, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004622 return -1;
4623 }
4624
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004625 p = pi.data;
4626 err = drbd_recv_all_warn(tconn, p, expect);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004627 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004628 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004629
Philipp Reisnerb411b362009-09-25 16:07:19 -07004630 p->protocol_min = be32_to_cpu(p->protocol_min);
4631 p->protocol_max = be32_to_cpu(p->protocol_max);
4632 if (p->protocol_max == 0)
4633 p->protocol_max = p->protocol_min;
4634
4635 if (PRO_VERSION_MAX < p->protocol_min ||
4636 PRO_VERSION_MIN > p->protocol_max)
4637 goto incompat;
4638
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004639 tconn->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004640
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004641 conn_info(tconn, "Handshake successful: "
4642 "Agreed network protocol version %d\n", tconn->agreed_pro_version);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004643
4644 return 1;
4645
4646 incompat:
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004647 conn_err(tconn, "incompatible DRBD dialects: "
Philipp Reisnerb411b362009-09-25 16:07:19 -07004648 "I support %d-%d, peer supports %d-%d\n",
4649 PRO_VERSION_MIN, PRO_VERSION_MAX,
4650 p->protocol_min, p->protocol_max);
4651 return -1;
4652}
4653
4654#if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
Philipp Reisner13e60372011-02-08 09:54:40 +01004655static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004656{
4657 dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
4658 dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004659 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004660}
4661#else
4662#define CHALLENGE_LEN 64
Johannes Thomab10d96c2010-01-07 16:02:50 +01004663
4664/* Return value:
4665 1 - auth succeeded,
4666 0 - failed, try again (network error),
4667 -1 - auth failed, don't try again.
4668*/
4669
Philipp Reisner13e60372011-02-08 09:54:40 +01004670static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004671{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004672 struct drbd_socket *sock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004673 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
4674 struct scatterlist sg;
4675 char *response = NULL;
4676 char *right_response = NULL;
4677 char *peers_ch = NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +02004678 unsigned int key_len;
4679 char secret[SHARED_SECRET_MAX]; /* 64 byte */
Philipp Reisnerb411b362009-09-25 16:07:19 -07004680 unsigned int resp_size;
4681 struct hash_desc desc;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004682 struct packet_info pi;
Philipp Reisner44ed1672011-04-19 17:10:19 +02004683 struct net_conf *nc;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004684 int err, rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004685
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004686 /* FIXME: Put the challenge/response into the preallocated socket buffer. */
4687
Philipp Reisner44ed1672011-04-19 17:10:19 +02004688 rcu_read_lock();
4689 nc = rcu_dereference(tconn->net_conf);
4690 key_len = strlen(nc->shared_secret);
4691 memcpy(secret, nc->shared_secret, key_len);
4692 rcu_read_unlock();
4693
Philipp Reisner13e60372011-02-08 09:54:40 +01004694 desc.tfm = tconn->cram_hmac_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004695 desc.flags = 0;
4696
Philipp Reisner44ed1672011-04-19 17:10:19 +02004697 rv = crypto_hash_setkey(tconn->cram_hmac_tfm, (u8 *)secret, key_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004698 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004699 conn_err(tconn, "crypto_hash_setkey() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004700 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004701 goto fail;
4702 }
4703
4704 get_random_bytes(my_challenge, CHALLENGE_LEN);
4705
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004706 sock = &tconn->data;
4707 if (!conn_prepare_command(tconn, sock)) {
4708 rv = 0;
4709 goto fail;
4710 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004711 rv = !conn_send_command(tconn, sock, P_AUTH_CHALLENGE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004712 my_challenge, CHALLENGE_LEN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004713 if (!rv)
4714 goto fail;
4715
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004716 err = drbd_recv_header(tconn, &pi);
4717 if (err) {
4718 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004719 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004720 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004721
Philipp Reisner77351055b2011-02-07 17:24:26 +01004722 if (pi.cmd != P_AUTH_CHALLENGE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004723 conn_err(tconn, "expected AuthChallenge packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004724 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004725 rv = 0;
4726 goto fail;
4727 }
4728
Philipp Reisner77351055b2011-02-07 17:24:26 +01004729 if (pi.size > CHALLENGE_LEN * 2) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004730 conn_err(tconn, "expected AuthChallenge payload too big.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004731 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004732 goto fail;
4733 }
4734
Philipp Reisner77351055b2011-02-07 17:24:26 +01004735 peers_ch = kmalloc(pi.size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004736 if (peers_ch == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004737 conn_err(tconn, "kmalloc of peers_ch failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004738 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004739 goto fail;
4740 }
4741
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004742 err = drbd_recv_all_warn(tconn, peers_ch, pi.size);
4743 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004744 rv = 0;
4745 goto fail;
4746 }
4747
Philipp Reisner13e60372011-02-08 09:54:40 +01004748 resp_size = crypto_hash_digestsize(tconn->cram_hmac_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004749 response = kmalloc(resp_size, GFP_NOIO);
4750 if (response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004751 conn_err(tconn, "kmalloc of response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004752 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004753 goto fail;
4754 }
4755
4756 sg_init_table(&sg, 1);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004757 sg_set_buf(&sg, peers_ch, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004758
4759 rv = crypto_hash_digest(&desc, &sg, sg.length, response);
4760 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004761 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004762 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004763 goto fail;
4764 }
4765
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004766 if (!conn_prepare_command(tconn, sock)) {
4767 rv = 0;
4768 goto fail;
4769 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004770 rv = !conn_send_command(tconn, sock, P_AUTH_RESPONSE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004771 response, resp_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004772 if (!rv)
4773 goto fail;
4774
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004775 err = drbd_recv_header(tconn, &pi);
4776 if (err) {
4777 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004778 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004779 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004780
Philipp Reisner77351055b2011-02-07 17:24:26 +01004781 if (pi.cmd != P_AUTH_RESPONSE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004782 conn_err(tconn, "expected AuthResponse packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004783 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004784 rv = 0;
4785 goto fail;
4786 }
4787
Philipp Reisner77351055b2011-02-07 17:24:26 +01004788 if (pi.size != resp_size) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004789 conn_err(tconn, "expected AuthResponse payload of wrong size\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004790 rv = 0;
4791 goto fail;
4792 }
4793
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004794 err = drbd_recv_all_warn(tconn, response , resp_size);
4795 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004796 rv = 0;
4797 goto fail;
4798 }
4799
4800 right_response = kmalloc(resp_size, GFP_NOIO);
Julia Lawall2d1ee872009-12-27 22:27:11 +01004801 if (right_response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004802 conn_err(tconn, "kmalloc of right_response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004803 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004804 goto fail;
4805 }
4806
4807 sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
4808
4809 rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
4810 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004811 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004812 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004813 goto fail;
4814 }
4815
4816 rv = !memcmp(response, right_response, resp_size);
4817
4818 if (rv)
Philipp Reisner44ed1672011-04-19 17:10:19 +02004819 conn_info(tconn, "Peer authenticated using %d bytes HMAC\n",
4820 resp_size);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004821 else
4822 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004823
4824 fail:
4825 kfree(peers_ch);
4826 kfree(response);
4827 kfree(right_response);
4828
4829 return rv;
4830}
4831#endif
4832
4833int drbdd_init(struct drbd_thread *thi)
4834{
Philipp Reisner392c8802011-02-09 10:33:31 +01004835 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004836 int h;
4837
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004838 conn_info(tconn, "receiver (re)started\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004839
4840 do {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004841 h = conn_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004842 if (h == 0) {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004843 conn_disconnect(tconn);
Philipp Reisner20ee6392011-01-18 15:28:59 +01004844 schedule_timeout_interruptible(HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004845 }
4846 if (h == -1) {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004847 conn_warn(tconn, "Discarding network configuration.\n");
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004848 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004849 }
4850 } while (h == 0);
4851
Philipp Reisner91fd4da2011-04-20 17:47:29 +02004852 if (h > 0)
4853 drbdd(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004854
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004855 conn_disconnect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004856
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004857 conn_info(tconn, "receiver terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004858 return 0;
4859}
4860
4861/* ********* acknowledge sender ******** */
4862
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004863static int got_conn_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004864{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004865 struct p_req_state_reply *p = pi->data;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004866 int retcode = be32_to_cpu(p->retcode);
4867
4868 if (retcode >= SS_SUCCESS) {
4869 set_bit(CONN_WD_ST_CHG_OKAY, &tconn->flags);
4870 } else {
4871 set_bit(CONN_WD_ST_CHG_FAIL, &tconn->flags);
4872 conn_err(tconn, "Requested state change failed by peer: %s (%d)\n",
4873 drbd_set_st_err_str(retcode), retcode);
4874 }
4875 wake_up(&tconn->ping_wait);
4876
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004877 return 0;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004878}
4879
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004880static int got_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004881{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004882 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004883 struct p_req_state_reply *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004884 int retcode = be32_to_cpu(p->retcode);
4885
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004886 mdev = vnr_to_mdev(tconn, pi->vnr);
4887 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004888 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004889
Philipp Reisner4d0fc3f2012-01-20 13:52:27 +01004890 if (test_bit(CONN_WD_ST_CHG_REQ, &tconn->flags)) {
4891 D_ASSERT(tconn->agreed_pro_version < 100);
4892 return got_conn_RqSReply(tconn, pi);
4893 }
4894
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004895 if (retcode >= SS_SUCCESS) {
4896 set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
4897 } else {
4898 set_bit(CL_ST_CHG_FAIL, &mdev->flags);
4899 dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
4900 drbd_set_st_err_str(retcode), retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004901 }
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004902 wake_up(&mdev->state_wait);
4903
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004904 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004905}
4906
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004907static int got_Ping(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004908{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004909 return drbd_send_ping_ack(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004910
4911}
4912
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004913static int got_PingAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004914{
4915 /* restore idle timeout */
Philipp Reisner2a67d8b2011-02-09 14:10:32 +01004916 tconn->meta.socket->sk->sk_rcvtimeo = tconn->net_conf->ping_int*HZ;
4917 if (!test_and_set_bit(GOT_PING_ACK, &tconn->flags))
4918 wake_up(&tconn->ping_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004919
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004920 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004921}
4922
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004923static int got_IsInSync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004924{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004925 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004926 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004927 sector_t sector = be64_to_cpu(p->sector);
4928 int blksize = be32_to_cpu(p->blksize);
4929
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004930 mdev = vnr_to_mdev(tconn, pi->vnr);
4931 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004932 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004933
Philipp Reisner31890f42011-01-19 14:12:51 +01004934 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004935
4936 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4937
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004938 if (get_ldev(mdev)) {
4939 drbd_rs_complete_io(mdev, sector);
4940 drbd_set_in_sync(mdev, sector, blksize);
4941 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
4942 mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
4943 put_ldev(mdev);
4944 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004945 dec_rs_pending(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02004946 atomic_add(blksize >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004947
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004948 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004949}
4950
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004951static int
4952validate_req_change_req_state(struct drbd_conf *mdev, u64 id, sector_t sector,
4953 struct rb_root *root, const char *func,
4954 enum drbd_req_event what, bool missing_ok)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004955{
4956 struct drbd_request *req;
4957 struct bio_and_error m;
4958
Philipp Reisner87eeee42011-01-19 14:16:30 +01004959 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004960 req = find_request(mdev, root, id, sector, missing_ok, func);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004961 if (unlikely(!req)) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01004962 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004963 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004964 }
4965 __req_mod(req, what, &m);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004966 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004967
4968 if (m.bio)
4969 complete_master_bio(mdev, &m);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004970 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004971}
4972
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004973static int got_BlockAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004974{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004975 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004976 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004977 sector_t sector = be64_to_cpu(p->sector);
4978 int blksize = be32_to_cpu(p->blksize);
4979 enum drbd_req_event what;
4980
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004981 mdev = vnr_to_mdev(tconn, pi->vnr);
4982 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004983 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004984
Philipp Reisnerb411b362009-09-25 16:07:19 -07004985 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4986
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004987 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004988 drbd_set_in_sync(mdev, sector, blksize);
4989 dec_rs_pending(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004990 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004991 }
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004992 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004993 case P_RS_WRITE_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004994 what = WRITE_ACKED_BY_PEER_AND_SIS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004995 break;
4996 case P_WRITE_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004997 what = WRITE_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004998 break;
4999 case P_RECV_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01005000 what = RECV_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005001 break;
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02005002 case P_SUPERSEDED:
5003 what = CONFLICT_RESOLVED;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01005004 break;
5005 case P_RETRY_WRITE:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01005006 what = POSTPONE_WRITE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005007 break;
5008 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005009 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07005010 }
5011
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005012 return validate_req_change_req_state(mdev, p->block_id, sector,
5013 &mdev->write_requests, __func__,
5014 what, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005015}
5016
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005017static int got_NegAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005018{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005019 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005020 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005021 sector_t sector = be64_to_cpu(p->sector);
Philipp Reisner2deb8332011-01-17 18:39:18 +01005022 int size = be32_to_cpu(p->blksize);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02005023 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005024
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005025 mdev = vnr_to_mdev(tconn, pi->vnr);
5026 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005027 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005028
Philipp Reisnerb411b362009-09-25 16:07:19 -07005029 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5030
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01005031 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07005032 dec_rs_pending(mdev);
5033 drbd_rs_failed_io(mdev, sector, size);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005034 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005035 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01005036
Andreas Gruenbacher85997672011-04-04 13:09:15 +02005037 err = validate_req_change_req_state(mdev, p->block_id, sector,
5038 &mdev->write_requests, __func__,
Philipp Reisner303d1442011-04-13 16:24:47 -07005039 NEG_ACKED, true);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02005040 if (err) {
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01005041 /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs.
5042 The master bio might already be completed, therefore the
5043 request is no longer in the collision hash. */
5044 /* In Protocol B we might already have got a P_RECV_ACK
5045 but then get a P_NEG_ACK afterwards. */
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01005046 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisner2deb8332011-01-17 18:39:18 +01005047 }
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005048 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005049}
5050
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005051static int got_NegDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005052{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005053 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005054 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005055 sector_t sector = be64_to_cpu(p->sector);
5056
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005057 mdev = vnr_to_mdev(tconn, pi->vnr);
5058 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005059 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005060
Philipp Reisnerb411b362009-09-25 16:07:19 -07005061 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01005062
Philipp Reisner380207d2011-11-11 12:31:20 +01005063 dev_err(DEV, "Got NegDReply; Sector %llus, len %u.\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -07005064 (unsigned long long)sector, be32_to_cpu(p->blksize));
5065
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005066 return validate_req_change_req_state(mdev, p->block_id, sector,
5067 &mdev->read_requests, __func__,
5068 NEG_ACKED, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005069}
5070
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005071static int got_NegRSDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005072{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005073 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005074 sector_t sector;
5075 int size;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005076 struct p_block_ack *p = pi->data;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005077
5078 mdev = vnr_to_mdev(tconn, pi->vnr);
5079 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005080 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005081
5082 sector = be64_to_cpu(p->sector);
5083 size = be32_to_cpu(p->blksize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005084
5085 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5086
5087 dec_rs_pending(mdev);
5088
5089 if (get_ldev_if_state(mdev, D_FAILED)) {
5090 drbd_rs_complete_io(mdev, sector);
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01005091 switch (pi->cmd) {
Philipp Reisnerd612d302010-12-27 10:53:28 +01005092 case P_NEG_RS_DREPLY:
5093 drbd_rs_failed_io(mdev, sector, size);
5094 case P_RS_CANCEL:
5095 break;
5096 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005097 BUG();
Philipp Reisnerd612d302010-12-27 10:53:28 +01005098 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005099 put_ldev(mdev);
5100 }
5101
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005102 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005103}
5104
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005105static int got_BarrierAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005106{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005107 struct p_barrier_ack *p = pi->data;
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02005108 struct drbd_conf *mdev;
5109 int vnr;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005110
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02005111 tl_release(tconn, p->barrier, be32_to_cpu(p->set_size));
Philipp Reisnerb411b362009-09-25 16:07:19 -07005112
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02005113 rcu_read_lock();
5114 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
5115 if (mdev->state.conn == C_AHEAD &&
5116 atomic_read(&mdev->ap_in_flight) == 0 &&
5117 !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->flags)) {
5118 mdev->start_resync_timer.expires = jiffies + HZ;
5119 add_timer(&mdev->start_resync_timer);
5120 }
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02005121 }
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02005122 rcu_read_unlock();
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02005123
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005124 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005125}
5126
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005127static int got_OVResult(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005128{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005129 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005130 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005131 struct drbd_work *w;
5132 sector_t sector;
5133 int size;
5134
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005135 mdev = vnr_to_mdev(tconn, pi->vnr);
5136 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005137 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005138
Philipp Reisnerb411b362009-09-25 16:07:19 -07005139 sector = be64_to_cpu(p->sector);
5140 size = be32_to_cpu(p->blksize);
5141
5142 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5143
5144 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005145 drbd_ov_out_of_sync_found(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005146 else
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005147 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005148
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005149 if (!get_ldev(mdev))
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005150 return 0;
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005151
Philipp Reisnerb411b362009-09-25 16:07:19 -07005152 drbd_rs_complete_io(mdev, sector);
5153 dec_rs_pending(mdev);
5154
Lars Ellenbergea5442a2010-11-05 09:48:01 +01005155 --mdev->ov_left;
5156
5157 /* let's advance progress step marks only for every other megabyte */
5158 if ((mdev->ov_left & 0x200) == 0x200)
5159 drbd_advance_rs_marks(mdev, mdev->ov_left);
5160
5161 if (mdev->ov_left == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07005162 w = kmalloc(sizeof(*w), GFP_NOIO);
5163 if (w) {
5164 w->cb = w_ov_finished;
Philipp Reisnera21e9292011-02-08 15:08:49 +01005165 w->mdev = mdev;
Lars Ellenbergd5b27b02011-11-14 15:42:37 +01005166 drbd_queue_work(&mdev->tconn->sender_work, w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005167 } else {
5168 dev_err(DEV, "kmalloc(w) failed.");
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005169 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005170 drbd_resync_finished(mdev);
5171 }
5172 }
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005173 put_ldev(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005174 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005175}
5176
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005177static int got_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner0ced55a2010-04-30 15:26:20 +02005178{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005179 return 0;
Philipp Reisner0ced55a2010-04-30 15:26:20 +02005180}
5181
Andreas Gruenbachera990be42011-04-06 17:56:48 +02005182static int tconn_finish_peer_reqs(struct drbd_tconn *tconn)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005183{
Philipp Reisner082a3432011-03-15 16:05:42 +01005184 struct drbd_conf *mdev;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005185 int vnr, not_empty = 0;
Philipp Reisner32862ec2011-02-08 16:41:01 +01005186
5187 do {
5188 clear_bit(SIGNAL_ASENDER, &tconn->flags);
5189 flush_signals(current);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005190
5191 rcu_read_lock();
5192 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
5193 kref_get(&mdev->kref);
5194 rcu_read_unlock();
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07005195 if (drbd_finish_peer_reqs(mdev)) {
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005196 kref_put(&mdev->kref, &drbd_minor_destroy);
5197 return 1;
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07005198 }
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005199 kref_put(&mdev->kref, &drbd_minor_destroy);
5200 rcu_read_lock();
Philipp Reisner082a3432011-03-15 16:05:42 +01005201 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005202 set_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisner082a3432011-03-15 16:05:42 +01005203
5204 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005205 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisner082a3432011-03-15 16:05:42 +01005206 not_empty = !list_empty(&mdev->done_ee);
5207 if (not_empty)
5208 break;
5209 }
5210 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005211 rcu_read_unlock();
Philipp Reisner32862ec2011-02-08 16:41:01 +01005212 } while (not_empty);
5213
5214 return 0;
5215}
5216
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005217struct asender_cmd {
5218 size_t pkt_size;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005219 int (*fn)(struct drbd_tconn *tconn, struct packet_info *);
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005220};
5221
5222static struct asender_cmd asender_tbl[] = {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005223 [P_PING] = { 0, got_Ping },
5224 [P_PING_ACK] = { 0, got_PingAck },
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005225 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5226 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5227 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02005228 [P_SUPERSEDED] = { sizeof(struct p_block_ack), got_BlockAck },
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005229 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
5230 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
5231 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply },
5232 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
5233 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
5234 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
5235 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
5236 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
5237 [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply },
5238 [P_CONN_ST_CHG_REPLY]={ sizeof(struct p_req_state_reply), got_conn_RqSReply },
5239 [P_RETRY_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005240};
5241
Philipp Reisnerb411b362009-09-25 16:07:19 -07005242int drbd_asender(struct drbd_thread *thi)
5243{
Philipp Reisner392c8802011-02-09 10:33:31 +01005244 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005245 struct asender_cmd *cmd = NULL;
Philipp Reisner77351055b2011-02-07 17:24:26 +01005246 struct packet_info pi;
Philipp Reisner257d0af2011-01-26 12:15:29 +01005247 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005248 void *buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005249 int received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005250 unsigned int header_size = drbd_header_size(tconn);
5251 int expect = header_size;
Philipp Reisner44ed1672011-04-19 17:10:19 +02005252 bool ping_timeout_active = false;
5253 struct net_conf *nc;
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005254 int ping_timeo, tcp_cork, ping_int;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005255
Philipp Reisnerb411b362009-09-25 16:07:19 -07005256 current->policy = SCHED_RR; /* Make this a realtime task! */
5257 current->rt_priority = 2; /* more important than all other tasks */
5258
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01005259 while (get_t_state(thi) == RUNNING) {
Philipp Reisner80822282011-02-08 12:46:30 +01005260 drbd_thread_current_set_cpu(thi);
Philipp Reisner44ed1672011-04-19 17:10:19 +02005261
5262 rcu_read_lock();
5263 nc = rcu_dereference(tconn->net_conf);
5264 ping_timeo = nc->ping_timeo;
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005265 tcp_cork = nc->tcp_cork;
Philipp Reisner44ed1672011-04-19 17:10:19 +02005266 ping_int = nc->ping_int;
5267 rcu_read_unlock();
5268
Philipp Reisner32862ec2011-02-08 16:41:01 +01005269 if (test_and_clear_bit(SEND_PING, &tconn->flags)) {
Andreas Gruenbachera17647a2011-04-01 12:49:42 +02005270 if (drbd_send_ping(tconn)) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005271 conn_err(tconn, "drbd_send_ping has failed\n");
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01005272 goto reconnect;
5273 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02005274 tconn->meta.socket->sk->sk_rcvtimeo = ping_timeo * HZ / 10;
5275 ping_timeout_active = true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005276 }
5277
Philipp Reisner32862ec2011-02-08 16:41:01 +01005278 /* TODO: conditionally cork; it may hurt latency if we cork without
5279 much to send */
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005280 if (tcp_cork)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005281 drbd_tcp_cork(tconn->meta.socket);
Andreas Gruenbachera990be42011-04-06 17:56:48 +02005282 if (tconn_finish_peer_reqs(tconn)) {
5283 conn_err(tconn, "tconn_finish_peer_reqs() failed\n");
Philipp Reisner32862ec2011-02-08 16:41:01 +01005284 goto reconnect;
Philipp Reisner082a3432011-03-15 16:05:42 +01005285 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005286 /* but unconditionally uncork unless disabled */
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005287 if (tcp_cork)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005288 drbd_tcp_uncork(tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005289
5290 /* short circuit, recv_msg would return EINTR anyways. */
5291 if (signal_pending(current))
5292 continue;
5293
Philipp Reisner32862ec2011-02-08 16:41:01 +01005294 rv = drbd_recv_short(tconn->meta.socket, buf, expect-received, 0);
5295 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005296
5297 flush_signals(current);
5298
5299 /* Note:
5300 * -EINTR (on meta) we got a signal
5301 * -EAGAIN (on meta) rcvtimeo expired
5302 * -ECONNRESET other side closed the connection
5303 * -ERESTARTSYS (on data) we got a signal
5304 * rv < 0 other than above: unexpected error!
5305 * rv == expected: full header or command
5306 * rv < expected: "woken" by signal during receive
5307 * rv == 0 : "connection shut down by peer"
5308 */
5309 if (likely(rv > 0)) {
5310 received += rv;
5311 buf += rv;
5312 } else if (rv == 0) {
Philipp Reisnerb66623e2012-08-08 21:19:09 +02005313 if (test_bit(DISCONNECT_SENT, &tconn->flags)) {
5314 long t;
5315 rcu_read_lock();
5316 t = rcu_dereference(tconn->net_conf)->ping_timeo * HZ/10;
5317 rcu_read_unlock();
5318
5319 t = wait_event_timeout(tconn->ping_wait,
5320 tconn->cstate < C_WF_REPORT_PARAMS,
5321 t);
5322 if (t)
5323 break;
5324 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005325 conn_err(tconn, "meta connection shut down by peer.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005326 goto reconnect;
5327 } else if (rv == -EAGAIN) {
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02005328 /* If the data socket received something meanwhile,
5329 * that is good enough: peer is still alive. */
Philipp Reisner32862ec2011-02-08 16:41:01 +01005330 if (time_after(tconn->last_received,
5331 jiffies - tconn->meta.socket->sk->sk_rcvtimeo))
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02005332 continue;
Lars Ellenbergf36af182011-03-09 22:44:55 +01005333 if (ping_timeout_active) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005334 conn_err(tconn, "PingAck did not arrive in time.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005335 goto reconnect;
5336 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005337 set_bit(SEND_PING, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005338 continue;
5339 } else if (rv == -EINTR) {
5340 continue;
5341 } else {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005342 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005343 goto reconnect;
5344 }
5345
5346 if (received == expect && cmd == NULL) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005347 if (decode_header(tconn, tconn->meta.rbuf, &pi))
Philipp Reisnerb411b362009-09-25 16:07:19 -07005348 goto reconnect;
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005349 cmd = &asender_tbl[pi.cmd];
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005350 if (pi.cmd >= ARRAY_SIZE(asender_tbl) || !cmd->fn) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02005351 conn_err(tconn, "Unexpected meta packet %s (0x%04x)\n",
5352 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005353 goto disconnect;
5354 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005355 expect = header_size + cmd->pkt_size;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005356 if (pi.size != expect - header_size) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005357 conn_err(tconn, "Wrong packet size on meta (c: %d, l: %d)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01005358 pi.cmd, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005359 goto reconnect;
Philipp Reisner257d0af2011-01-26 12:15:29 +01005360 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005361 }
5362 if (received == expect) {
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005363 bool err;
Philipp Reisnera4fbda82011-03-16 11:13:17 +01005364
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005365 err = cmd->fn(tconn, &pi);
5366 if (err) {
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005367 conn_err(tconn, "%pf failed\n", cmd->fn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005368 goto reconnect;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005369 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005370
Philipp Reisnera4fbda82011-03-16 11:13:17 +01005371 tconn->last_received = jiffies;
5372
Philipp Reisner44ed1672011-04-19 17:10:19 +02005373 if (cmd == &asender_tbl[P_PING_ACK]) {
5374 /* restore idle timeout */
5375 tconn->meta.socket->sk->sk_rcvtimeo = ping_int * HZ;
5376 ping_timeout_active = false;
5377 }
Lars Ellenbergf36af182011-03-09 22:44:55 +01005378
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005379 buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005380 received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005381 expect = header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005382 cmd = NULL;
5383 }
5384 }
5385
5386 if (0) {
5387reconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01005388 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005389 }
5390 if (0) {
5391disconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01005392 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005393 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005394 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005395
Philipp Reisner32862ec2011-02-08 16:41:01 +01005396 conn_info(tconn, "asender terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005397
5398 return 0;
5399}