blob: 14df37ccd524f3d7f1a2945b95d4c8c242e9f8a8 [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);
507
508 for (;;) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100509 rv = sock_recvmsg(tconn->data.socket, &msg, size, msg.msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700510 if (rv == size)
511 break;
512
513 /* Note:
514 * ECONNRESET other side closed the connection
515 * ERESTARTSYS (on sock) we got a signal
516 */
517
518 if (rv < 0) {
519 if (rv == -ECONNRESET)
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100520 conn_info(tconn, "sock was reset by peer\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700521 else if (rv != -ERESTARTSYS)
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100522 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700523 break;
524 } else if (rv == 0) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100525 conn_info(tconn, "sock was shut down by peer\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700526 break;
527 } else {
528 /* signal came in, or peer/link went down,
529 * after we read a partial message
530 */
531 /* D_ASSERT(signal_pending(current)); */
532 break;
533 }
534 };
535
536 set_fs(oldfs);
537
538 if (rv != size)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100539 conn_request_state(tconn, NS(conn, C_BROKEN_PIPE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700540
541 return rv;
542}
543
Andreas Gruenbacherc6967742011-03-17 17:15:20 +0100544static int drbd_recv_all(struct drbd_tconn *tconn, void *buf, size_t size)
545{
546 int err;
547
548 err = drbd_recv(tconn, buf, size);
549 if (err != size) {
550 if (err >= 0)
551 err = -EIO;
552 } else
553 err = 0;
554 return err;
555}
556
Andreas Gruenbachera5c31902011-03-24 03:28:04 +0100557static int drbd_recv_all_warn(struct drbd_tconn *tconn, void *buf, size_t size)
558{
559 int err;
560
561 err = drbd_recv_all(tconn, buf, size);
562 if (err && !signal_pending(current))
563 conn_warn(tconn, "short read (expected size %d)\n", (int)size);
564 return err;
565}
566
Lars Ellenberg5dbf1672010-05-25 16:18:01 +0200567/* quoting tcp(7):
568 * On individual connections, the socket buffer size must be set prior to the
569 * listen(2) or connect(2) calls in order to have it take effect.
570 * This is our wrapper to do so.
571 */
572static void drbd_setbufsize(struct socket *sock, unsigned int snd,
573 unsigned int rcv)
574{
575 /* open coded SO_SNDBUF, SO_RCVBUF */
576 if (snd) {
577 sock->sk->sk_sndbuf = snd;
578 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
579 }
580 if (rcv) {
581 sock->sk->sk_rcvbuf = rcv;
582 sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
583 }
584}
585
Philipp Reisnereac3e992011-02-07 14:05:07 +0100586static struct socket *drbd_try_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700587{
588 const char *what;
589 struct socket *sock;
590 struct sockaddr_in6 src_in6;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200591 struct sockaddr_in6 peer_in6;
592 struct net_conf *nc;
593 int err, peer_addr_len, my_addr_len;
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200594 int sndbuf_size, rcvbuf_size, connect_int;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700595 int disconnect_on_error = 1;
596
Philipp Reisner44ed1672011-04-19 17:10:19 +0200597 rcu_read_lock();
598 nc = rcu_dereference(tconn->net_conf);
599 if (!nc) {
600 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700601 return NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200602 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200603 sndbuf_size = nc->sndbuf_size;
604 rcvbuf_size = nc->rcvbuf_size;
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200605 connect_int = nc->connect_int;
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200606 rcu_read_unlock();
Philipp Reisner44ed1672011-04-19 17:10:19 +0200607
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200608 my_addr_len = min_t(int, tconn->my_addr_len, sizeof(src_in6));
609 memcpy(&src_in6, &tconn->my_addr, my_addr_len);
Philipp Reisner44ed1672011-04-19 17:10:19 +0200610
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200611 if (((struct sockaddr *)&tconn->my_addr)->sa_family == AF_INET6)
Philipp Reisner44ed1672011-04-19 17:10:19 +0200612 src_in6.sin6_port = 0;
613 else
614 ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */
615
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200616 peer_addr_len = min_t(int, tconn->peer_addr_len, sizeof(src_in6));
617 memcpy(&peer_in6, &tconn->peer_addr, peer_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700618
619 what = "sock_create_kern";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200620 err = sock_create_kern(((struct sockaddr *)&src_in6)->sa_family,
621 SOCK_STREAM, IPPROTO_TCP, &sock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700622 if (err < 0) {
623 sock = NULL;
624 goto out;
625 }
626
627 sock->sk->sk_rcvtimeo =
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200628 sock->sk->sk_sndtimeo = connect_int * HZ;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200629 drbd_setbufsize(sock, sndbuf_size, rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700630
631 /* explicitly bind to the configured IP as source IP
632 * for the outgoing connections.
633 * This is needed for multihomed hosts and to be
634 * able to use lo: interfaces for drbd.
635 * Make sure to use 0 as port number, so linux selects
636 * a free one dynamically.
637 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700638 what = "bind before connect";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200639 err = sock->ops->bind(sock, (struct sockaddr *) &src_in6, my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700640 if (err < 0)
641 goto out;
642
643 /* connect may fail, peer not yet available.
644 * stay C_WF_CONNECTION, don't go Disconnecting! */
645 disconnect_on_error = 0;
646 what = "connect";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200647 err = sock->ops->connect(sock, (struct sockaddr *) &peer_in6, peer_addr_len, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700648
649out:
650 if (err < 0) {
651 if (sock) {
652 sock_release(sock);
653 sock = NULL;
654 }
655 switch (-err) {
656 /* timeout, busy, signal pending */
657 case ETIMEDOUT: case EAGAIN: case EINPROGRESS:
658 case EINTR: case ERESTARTSYS:
659 /* peer not (yet) available, network problem */
660 case ECONNREFUSED: case ENETUNREACH:
661 case EHOSTDOWN: case EHOSTUNREACH:
662 disconnect_on_error = 0;
663 break;
664 default:
Philipp Reisnereac3e992011-02-07 14:05:07 +0100665 conn_err(tconn, "%s failed, err = %d\n", what, err);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700666 }
667 if (disconnect_on_error)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100668 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700669 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200670
Philipp Reisnerb411b362009-09-25 16:07:19 -0700671 return sock;
672}
673
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200674struct accept_wait_data {
675 struct drbd_tconn *tconn;
676 struct socket *s_listen;
677 struct completion door_bell;
678 void (*original_sk_state_change)(struct sock *sk);
679
680};
681
Andreas Gruenbacher715306f2012-08-10 17:00:30 +0200682static void drbd_incoming_connection(struct sock *sk)
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200683{
684 struct accept_wait_data *ad = sk->sk_user_data;
Andreas Gruenbacher715306f2012-08-10 17:00:30 +0200685 void (*state_change)(struct sock *sk);
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200686
Andreas Gruenbacher715306f2012-08-10 17:00:30 +0200687 state_change = ad->original_sk_state_change;
688 if (sk->sk_state == TCP_ESTABLISHED)
689 complete(&ad->door_bell);
690 state_change(sk);
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200691}
692
693static int prepare_listen_socket(struct drbd_tconn *tconn, struct accept_wait_data *ad)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700694{
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200695 int err, sndbuf_size, rcvbuf_size, my_addr_len;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200696 struct sockaddr_in6 my_addr;
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200697 struct socket *s_listen;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200698 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700699 const char *what;
700
Philipp Reisner44ed1672011-04-19 17:10:19 +0200701 rcu_read_lock();
702 nc = rcu_dereference(tconn->net_conf);
703 if (!nc) {
704 rcu_read_unlock();
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200705 return -EIO;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200706 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200707 sndbuf_size = nc->sndbuf_size;
708 rcvbuf_size = nc->rcvbuf_size;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200709 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700710
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200711 my_addr_len = min_t(int, tconn->my_addr_len, sizeof(struct sockaddr_in6));
712 memcpy(&my_addr, &tconn->my_addr, my_addr_len);
713
Philipp Reisnerb411b362009-09-25 16:07:19 -0700714 what = "sock_create_kern";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200715 err = sock_create_kern(((struct sockaddr *)&my_addr)->sa_family,
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200716 SOCK_STREAM, IPPROTO_TCP, &s_listen);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700717 if (err) {
718 s_listen = NULL;
719 goto out;
720 }
721
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200722 s_listen->sk->sk_reuse = 1; /* SO_REUSEADDR */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200723 drbd_setbufsize(s_listen, sndbuf_size, rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700724
725 what = "bind before listen";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200726 err = s_listen->ops->bind(s_listen, (struct sockaddr *)&my_addr, my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700727 if (err < 0)
728 goto out;
729
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200730 ad->s_listen = s_listen;
731 write_lock_bh(&s_listen->sk->sk_callback_lock);
732 ad->original_sk_state_change = s_listen->sk->sk_state_change;
Andreas Gruenbacher715306f2012-08-10 17:00:30 +0200733 s_listen->sk->sk_state_change = drbd_incoming_connection;
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200734 s_listen->sk->sk_user_data = ad;
735 write_unlock_bh(&s_listen->sk->sk_callback_lock);
736
Philipp Reisner2820fd32012-07-12 10:22:48 +0200737 what = "listen";
738 err = s_listen->ops->listen(s_listen, 5);
739 if (err < 0)
740 goto out;
741
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200742 return 0;
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200743out:
744 if (s_listen)
745 sock_release(s_listen);
746 if (err < 0) {
747 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
748 conn_err(tconn, "%s failed, err = %d\n", what, err);
749 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
750 }
751 }
752
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200753 return -EIO;
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200754}
755
Andreas Gruenbacher715306f2012-08-10 17:00:30 +0200756static void unregister_state_change(struct sock *sk, struct accept_wait_data *ad)
757{
758 write_lock_bh(&sk->sk_callback_lock);
759 sk->sk_state_change = ad->original_sk_state_change;
760 sk->sk_user_data = NULL;
761 write_unlock_bh(&sk->sk_callback_lock);
762}
763
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200764static struct socket *drbd_wait_for_connect(struct drbd_tconn *tconn, struct accept_wait_data *ad)
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200765{
766 int timeo, connect_int, err = 0;
767 struct socket *s_estab = NULL;
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200768 struct net_conf *nc;
769
770 rcu_read_lock();
771 nc = rcu_dereference(tconn->net_conf);
772 if (!nc) {
773 rcu_read_unlock();
774 return NULL;
775 }
776 connect_int = nc->connect_int;
777 rcu_read_unlock();
778
779 timeo = connect_int * HZ;
780 timeo += (random32() & 1) ? timeo / 7 : -timeo / 7; /* 28.5% random jitter */
781
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200782 err = wait_for_completion_interruptible_timeout(&ad->door_bell, timeo);
783 if (err <= 0)
784 return NULL;
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200785
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200786 err = kernel_accept(ad->s_listen, &s_estab, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700787 if (err < 0) {
788 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200789 conn_err(tconn, "accept failed, err = %d\n", err);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100790 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700791 }
792 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700793
Andreas Gruenbacher715306f2012-08-10 17:00:30 +0200794 if (s_estab)
795 unregister_state_change(s_estab->sk, ad);
796
Philipp Reisnerb411b362009-09-25 16:07:19 -0700797 return s_estab;
798}
799
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200800static int decode_header(struct drbd_tconn *, void *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700801
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200802static int send_first_packet(struct drbd_tconn *tconn, struct drbd_socket *sock,
803 enum drbd_packet cmd)
804{
805 if (!conn_prepare_command(tconn, sock))
806 return -EIO;
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200807 return conn_send_command(tconn, sock, cmd, 0, NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700808}
809
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200810static int receive_first_packet(struct drbd_tconn *tconn, struct socket *sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700811{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200812 unsigned int header_size = drbd_header_size(tconn);
813 struct packet_info pi;
814 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700815
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200816 err = drbd_recv_short(sock, tconn->data.rbuf, header_size, 0);
817 if (err != header_size) {
818 if (err >= 0)
819 err = -EIO;
820 return err;
821 }
822 err = decode_header(tconn, tconn->data.rbuf, &pi);
823 if (err)
824 return err;
825 return pi.cmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700826}
827
828/**
829 * drbd_socket_okay() - Free the socket if its connection is not okay
Philipp Reisnerb411b362009-09-25 16:07:19 -0700830 * @sock: pointer to the pointer to the socket.
831 */
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100832static int drbd_socket_okay(struct socket **sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700833{
834 int rr;
835 char tb[4];
836
837 if (!*sock)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100838 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700839
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100840 rr = drbd_recv_short(*sock, tb, 4, MSG_DONTWAIT | MSG_PEEK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700841
842 if (rr > 0 || rr == -EAGAIN) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100843 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700844 } else {
845 sock_release(*sock);
846 *sock = NULL;
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100847 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700848 }
849}
Philipp Reisner2325eb62011-03-15 16:56:18 +0100850/* Gets called if a connection is established, or if a new minor gets created
851 in a connection */
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200852int drbd_connected(struct drbd_conf *mdev)
Philipp Reisner907599e2011-02-08 11:25:37 +0100853{
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100854 int err;
Philipp Reisner907599e2011-02-08 11:25:37 +0100855
856 atomic_set(&mdev->packet_seq, 0);
857 mdev->peer_seq = 0;
858
Philipp Reisner8410da82011-02-11 20:11:10 +0100859 mdev->state_mutex = mdev->tconn->agreed_pro_version < 100 ?
860 &mdev->tconn->cstate_mutex :
861 &mdev->own_state_mutex;
862
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100863 err = drbd_send_sync_param(mdev);
864 if (!err)
865 err = drbd_send_sizes(mdev, 0, 0);
866 if (!err)
867 err = drbd_send_uuids(mdev);
868 if (!err)
Philipp Reisner43de7c82011-11-10 13:16:13 +0100869 err = drbd_send_current_state(mdev);
Philipp Reisner907599e2011-02-08 11:25:37 +0100870 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
871 clear_bit(RESIZE_PENDING, &mdev->flags);
Philipp Reisner8b924f12011-03-01 11:08:28 +0100872 mod_timer(&mdev->request_timer, jiffies + HZ); /* just start it here. */
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100873 return err;
Philipp Reisner907599e2011-02-08 11:25:37 +0100874}
875
Philipp Reisnerb411b362009-09-25 16:07:19 -0700876/*
877 * return values:
878 * 1 yes, we have a valid connection
879 * 0 oops, did not work out, please try again
880 * -1 peer talks different language,
881 * no point in trying again, please go standalone.
882 * -2 We do not have a network config...
883 */
Philipp Reisner81fa2e62011-05-04 15:10:30 +0200884static int conn_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700885{
Philipp Reisner7da35862011-12-19 22:42:56 +0100886 struct drbd_socket sock, msock;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200887 struct drbd_conf *mdev;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200888 struct net_conf *nc;
Philipp Reisner92f14952012-08-01 11:41:01 +0200889 int vnr, timeout, h, ok;
Philipp Reisner08b165b2011-09-05 16:22:33 +0200890 bool discard_my_data;
Philipp Reisnera1096a62012-04-06 12:07:34 +0200891 enum drbd_state_rv rv;
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200892 struct accept_wait_data ad = {
893 .tconn = tconn,
894 .door_bell = COMPLETION_INITIALIZER_ONSTACK(ad.door_bell),
895 };
Philipp Reisnerb411b362009-09-25 16:07:19 -0700896
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100897 if (conn_request_state(tconn, NS(conn, C_WF_CONNECTION), CS_VERBOSE) < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700898 return -2;
899
Philipp Reisner7da35862011-12-19 22:42:56 +0100900 mutex_init(&sock.mutex);
901 sock.sbuf = tconn->data.sbuf;
902 sock.rbuf = tconn->data.rbuf;
903 sock.socket = NULL;
904 mutex_init(&msock.mutex);
905 msock.sbuf = tconn->meta.sbuf;
906 msock.rbuf = tconn->meta.rbuf;
907 msock.socket = NULL;
908
Andreas Gruenbacher0916e0e2011-03-21 14:10:15 +0100909 /* Assume that the peer only understands protocol 80 until we know better. */
910 tconn->agreed_pro_version = 80;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700911
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200912 if (prepare_listen_socket(tconn, &ad))
913 return 0;
914
Philipp Reisnerb411b362009-09-25 16:07:19 -0700915 do {
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200916 struct socket *s;
917
Philipp Reisner92f14952012-08-01 11:41:01 +0200918 s = drbd_try_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700919 if (s) {
Philipp Reisner7da35862011-12-19 22:42:56 +0100920 if (!sock.socket) {
921 sock.socket = s;
922 send_first_packet(tconn, &sock, P_INITIAL_DATA);
923 } else if (!msock.socket) {
Lars Ellenberg427c0432012-08-01 12:43:01 +0200924 clear_bit(RESOLVE_CONFLICTS, &tconn->flags);
Philipp Reisner7da35862011-12-19 22:42:56 +0100925 msock.socket = s;
926 send_first_packet(tconn, &msock, P_INITIAL_META);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700927 } else {
Philipp Reisner81fa2e62011-05-04 15:10:30 +0200928 conn_err(tconn, "Logic error in conn_connect()\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700929 goto out_release_sockets;
930 }
931 }
932
Philipp Reisner7da35862011-12-19 22:42:56 +0100933 if (sock.socket && msock.socket) {
934 rcu_read_lock();
935 nc = rcu_dereference(tconn->net_conf);
936 timeout = nc->ping_timeo * HZ / 10;
937 rcu_read_unlock();
938 schedule_timeout_interruptible(timeout);
939 ok = drbd_socket_okay(&sock.socket);
940 ok = drbd_socket_okay(&msock.socket) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700941 if (ok)
942 break;
943 }
944
945retry:
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200946 s = drbd_wait_for_connect(tconn, &ad);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700947 if (s) {
Philipp Reisner92f14952012-08-01 11:41:01 +0200948 int fp = receive_first_packet(tconn, s);
Philipp Reisner7da35862011-12-19 22:42:56 +0100949 drbd_socket_okay(&sock.socket);
950 drbd_socket_okay(&msock.socket);
Philipp Reisner92f14952012-08-01 11:41:01 +0200951 switch (fp) {
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200952 case P_INITIAL_DATA:
Philipp Reisner7da35862011-12-19 22:42:56 +0100953 if (sock.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100954 conn_warn(tconn, "initial packet S crossed\n");
Philipp Reisner7da35862011-12-19 22:42:56 +0100955 sock_release(sock.socket);
Philipp Reisner80c6eed2012-08-01 14:53:39 +0200956 sock.socket = s;
957 goto randomize;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700958 }
Philipp Reisner7da35862011-12-19 22:42:56 +0100959 sock.socket = s;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700960 break;
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200961 case P_INITIAL_META:
Lars Ellenberg427c0432012-08-01 12:43:01 +0200962 set_bit(RESOLVE_CONFLICTS, &tconn->flags);
Philipp Reisner7da35862011-12-19 22:42:56 +0100963 if (msock.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100964 conn_warn(tconn, "initial packet M crossed\n");
Philipp Reisner7da35862011-12-19 22:42:56 +0100965 sock_release(msock.socket);
Philipp Reisner80c6eed2012-08-01 14:53:39 +0200966 msock.socket = s;
967 goto randomize;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700968 }
Philipp Reisner7da35862011-12-19 22:42:56 +0100969 msock.socket = s;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700970 break;
971 default:
Philipp Reisner907599e2011-02-08 11:25:37 +0100972 conn_warn(tconn, "Error receiving initial packet\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700973 sock_release(s);
Philipp Reisner80c6eed2012-08-01 14:53:39 +0200974randomize:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700975 if (random32() & 1)
976 goto retry;
977 }
978 }
979
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100980 if (tconn->cstate <= C_DISCONNECTING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700981 goto out_release_sockets;
982 if (signal_pending(current)) {
983 flush_signals(current);
984 smp_rmb();
Philipp Reisner907599e2011-02-08 11:25:37 +0100985 if (get_t_state(&tconn->receiver) == EXITING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700986 goto out_release_sockets;
987 }
988
Philipp Reisnerb666dbf2012-07-26 14:12:59 +0200989 ok = drbd_socket_okay(&sock.socket);
990 ok = drbd_socket_okay(&msock.socket) && ok;
991 } while (!ok);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700992
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200993 if (ad.s_listen)
994 sock_release(ad.s_listen);
995
Philipp Reisner7da35862011-12-19 22:42:56 +0100996 sock.socket->sk->sk_reuse = 1; /* SO_REUSEADDR */
997 msock.socket->sk->sk_reuse = 1; /* SO_REUSEADDR */
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200998
Philipp Reisner7da35862011-12-19 22:42:56 +0100999 sock.socket->sk->sk_allocation = GFP_NOIO;
1000 msock.socket->sk->sk_allocation = GFP_NOIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001001
Philipp Reisner7da35862011-12-19 22:42:56 +01001002 sock.socket->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
1003 msock.socket->sk->sk_priority = TC_PRIO_INTERACTIVE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001004
Philipp Reisnerb411b362009-09-25 16:07:19 -07001005 /* NOT YET ...
Philipp Reisner7da35862011-12-19 22:42:56 +01001006 * sock.socket->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
1007 * sock.socket->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
Andreas Gruenbacher60381782011-03-28 17:05:50 +02001008 * first set it to the P_CONNECTION_FEATURES timeout,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001009 * which we set to 4x the configured ping_timeout. */
Philipp Reisner44ed1672011-04-19 17:10:19 +02001010 rcu_read_lock();
1011 nc = rcu_dereference(tconn->net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001012
Philipp Reisner7da35862011-12-19 22:42:56 +01001013 sock.socket->sk->sk_sndtimeo =
1014 sock.socket->sk->sk_rcvtimeo = nc->ping_timeo*4*HZ/10;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001015
Philipp Reisner7da35862011-12-19 22:42:56 +01001016 msock.socket->sk->sk_rcvtimeo = nc->ping_int*HZ;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001017 timeout = nc->timeout * HZ / 10;
Philipp Reisner08b165b2011-09-05 16:22:33 +02001018 discard_my_data = nc->discard_my_data;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001019 rcu_read_unlock();
1020
Philipp Reisner7da35862011-12-19 22:42:56 +01001021 msock.socket->sk->sk_sndtimeo = timeout;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001022
1023 /* we don't want delays.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001024 * we use TCP_CORK where appropriate, though */
Philipp Reisner7da35862011-12-19 22:42:56 +01001025 drbd_tcp_nodelay(sock.socket);
1026 drbd_tcp_nodelay(msock.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001027
Philipp Reisner7da35862011-12-19 22:42:56 +01001028 tconn->data.socket = sock.socket;
1029 tconn->meta.socket = msock.socket;
Philipp Reisner907599e2011-02-08 11:25:37 +01001030 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001031
Andreas Gruenbacher60381782011-03-28 17:05:50 +02001032 h = drbd_do_features(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001033 if (h <= 0)
1034 return h;
1035
Philipp Reisner907599e2011-02-08 11:25:37 +01001036 if (tconn->cram_hmac_tfm) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001037 /* drbd_request_state(mdev, NS(conn, WFAuth)); */
Philipp Reisner907599e2011-02-08 11:25:37 +01001038 switch (drbd_do_auth(tconn)) {
Johannes Thomab10d96c2010-01-07 16:02:50 +01001039 case -1:
Philipp Reisner907599e2011-02-08 11:25:37 +01001040 conn_err(tconn, "Authentication of peer failed\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001041 return -1;
Johannes Thomab10d96c2010-01-07 16:02:50 +01001042 case 0:
Philipp Reisner907599e2011-02-08 11:25:37 +01001043 conn_err(tconn, "Authentication of peer failed, trying again.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01001044 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001045 }
1046 }
1047
Philipp Reisner7da35862011-12-19 22:42:56 +01001048 tconn->data.socket->sk->sk_sndtimeo = timeout;
1049 tconn->data.socket->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001050
Andreas Gruenbacher387eb302011-03-16 01:05:37 +01001051 if (drbd_send_protocol(tconn) == -EOPNOTSUPP)
Philipp Reisner7e2455c2010-04-22 14:50:23 +02001052 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001053
Philipp Reisnera1096a62012-04-06 12:07:34 +02001054 set_bit(STATE_SENT, &tconn->flags);
1055
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001056 rcu_read_lock();
1057 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1058 kref_get(&mdev->kref);
1059 rcu_read_unlock();
Philipp Reisner08b165b2011-09-05 16:22:33 +02001060
1061 if (discard_my_data)
1062 set_bit(DISCARD_MY_DATA, &mdev->flags);
1063 else
1064 clear_bit(DISCARD_MY_DATA, &mdev->flags);
1065
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001066 drbd_connected(mdev);
1067 kref_put(&mdev->kref, &drbd_minor_destroy);
1068 rcu_read_lock();
1069 }
1070 rcu_read_unlock();
1071
Philipp Reisnera1096a62012-04-06 12:07:34 +02001072 rv = conn_request_state(tconn, NS(conn, C_WF_REPORT_PARAMS), CS_VERBOSE);
1073 if (rv < SS_SUCCESS) {
1074 clear_bit(STATE_SENT, &tconn->flags);
Philipp Reisner823bd832012-11-08 15:04:36 +01001075 return 0;
Philipp Reisnera1096a62012-04-06 12:07:34 +02001076 }
Philipp Reisner823bd832012-11-08 15:04:36 +01001077
1078 drbd_thread_start(&tconn->asender);
1079
Philipp Reisner08b165b2011-09-05 16:22:33 +02001080 mutex_lock(&tconn->conf_update);
1081 /* The discard_my_data flag is a single-shot modifier to the next
1082 * connection attempt, the handshake of which is now well underway.
1083 * No need for rcu style copying of the whole struct
1084 * just to clear a single value. */
1085 tconn->net_conf->discard_my_data = 0;
1086 mutex_unlock(&tconn->conf_update);
1087
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07001088 return h;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001089
1090out_release_sockets:
Philipp Reisner7a426fd2012-07-12 14:22:37 +02001091 if (ad.s_listen)
1092 sock_release(ad.s_listen);
Philipp Reisner7da35862011-12-19 22:42:56 +01001093 if (sock.socket)
1094 sock_release(sock.socket);
1095 if (msock.socket)
1096 sock_release(msock.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001097 return -1;
1098}
1099
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001100static int decode_header(struct drbd_tconn *tconn, void *header, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001101{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001102 unsigned int header_size = drbd_header_size(tconn);
1103
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +02001104 if (header_size == sizeof(struct p_header100) &&
1105 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC_100)) {
1106 struct p_header100 *h = header;
1107 if (h->pad != 0) {
1108 conn_err(tconn, "Header padding is not zero\n");
1109 return -EINVAL;
1110 }
1111 pi->vnr = be16_to_cpu(h->volume);
1112 pi->cmd = be16_to_cpu(h->command);
1113 pi->size = be32_to_cpu(h->length);
1114 } else if (header_size == sizeof(struct p_header95) &&
1115 *(__be16 *)header == cpu_to_be16(DRBD_MAGIC_BIG)) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001116 struct p_header95 *h = header;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001117 pi->cmd = be16_to_cpu(h->command);
Andreas Gruenbacherb55d84b2011-03-22 13:17:47 +01001118 pi->size = be32_to_cpu(h->length);
1119 pi->vnr = 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001120 } else if (header_size == sizeof(struct p_header80) &&
1121 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC)) {
1122 struct p_header80 *h = header;
1123 pi->cmd = be16_to_cpu(h->command);
1124 pi->size = be16_to_cpu(h->length);
Philipp Reisner77351055b2011-02-07 17:24:26 +01001125 pi->vnr = 0;
Philipp Reisner02918be2010-08-20 14:35:10 +02001126 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001127 conn_err(tconn, "Wrong magic value 0x%08x in protocol version %d\n",
1128 be32_to_cpu(*(__be32 *)header),
1129 tconn->agreed_pro_version);
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001130 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001131 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001132 pi->data = header + header_size;
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001133 return 0;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001134}
1135
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001136static int drbd_recv_header(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner257d0af2011-01-26 12:15:29 +01001137{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001138 void *buffer = tconn->data.rbuf;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001139 int err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001140
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001141 err = drbd_recv_all_warn(tconn, buffer, drbd_header_size(tconn));
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001142 if (err)
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001143 return err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001144
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001145 err = decode_header(tconn, buffer, pi);
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001146 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001147
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001148 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001149}
1150
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001151static void drbd_flush(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001152{
1153 int rv;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001154 struct drbd_conf *mdev;
1155 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001156
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001157 if (tconn->write_ordering >= WO_bdev_flush) {
Lars Ellenberg615e0872011-11-17 14:32:12 +01001158 rcu_read_lock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001159 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Lars Ellenberg615e0872011-11-17 14:32:12 +01001160 if (!get_ldev(mdev))
1161 continue;
1162 kref_get(&mdev->kref);
1163 rcu_read_unlock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001164
Lars Ellenberg615e0872011-11-17 14:32:12 +01001165 rv = blkdev_issue_flush(mdev->ldev->backing_bdev,
1166 GFP_NOIO, NULL);
1167 if (rv) {
1168 dev_info(DEV, "local disk flush failed with status %d\n", rv);
1169 /* would rather check on EOPNOTSUPP, but that is not reliable.
1170 * don't try again for ANY return value != 0
1171 * if (rv == -EOPNOTSUPP) */
1172 drbd_bump_write_ordering(tconn, WO_drain_io);
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001173 }
Lars Ellenberg615e0872011-11-17 14:32:12 +01001174 put_ldev(mdev);
1175 kref_put(&mdev->kref, &drbd_minor_destroy);
1176
1177 rcu_read_lock();
1178 if (rv)
1179 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001180 }
Lars Ellenberg615e0872011-11-17 14:32:12 +01001181 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001182 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001183}
1184
1185/**
1186 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
1187 * @mdev: DRBD device.
1188 * @epoch: Epoch object.
1189 * @ev: Epoch event.
1190 */
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001191static enum finish_epoch drbd_may_finish_epoch(struct drbd_tconn *tconn,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001192 struct drbd_epoch *epoch,
1193 enum epoch_event ev)
1194{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001195 int epoch_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001196 struct drbd_epoch *next_epoch;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001197 enum finish_epoch rv = FE_STILL_LIVE;
1198
Philipp Reisner12038a32011-11-09 19:18:00 +01001199 spin_lock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001200 do {
1201 next_epoch = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001202
1203 epoch_size = atomic_read(&epoch->epoch_size);
1204
1205 switch (ev & ~EV_CLEANUP) {
1206 case EV_PUT:
1207 atomic_dec(&epoch->active);
1208 break;
1209 case EV_GOT_BARRIER_NR:
1210 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001211 break;
1212 case EV_BECAME_LAST:
1213 /* nothing to do*/
1214 break;
1215 }
1216
Philipp Reisnerb411b362009-09-25 16:07:19 -07001217 if (epoch_size != 0 &&
1218 atomic_read(&epoch->active) == 0 &&
Philipp Reisner85d73512011-07-18 15:45:15 +02001219 (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags) || ev & EV_CLEANUP)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001220 if (!(ev & EV_CLEANUP)) {
Philipp Reisner12038a32011-11-09 19:18:00 +01001221 spin_unlock(&tconn->epoch_lock);
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001222 drbd_send_b_ack(epoch->tconn, epoch->barrier_nr, epoch_size);
Philipp Reisner12038a32011-11-09 19:18:00 +01001223 spin_lock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001224 }
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001225#if 0
1226 /* FIXME: dec unacked on connection, once we have
1227 * something to count pending connection packets in. */
Philipp Reisner85d73512011-07-18 15:45:15 +02001228 if (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags))
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001229 dec_unacked(epoch->tconn);
1230#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -07001231
Philipp Reisner12038a32011-11-09 19:18:00 +01001232 if (tconn->current_epoch != epoch) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001233 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1234 list_del(&epoch->list);
1235 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
Philipp Reisner12038a32011-11-09 19:18:00 +01001236 tconn->epochs--;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001237 kfree(epoch);
1238
1239 if (rv == FE_STILL_LIVE)
1240 rv = FE_DESTROYED;
1241 } else {
1242 epoch->flags = 0;
1243 atomic_set(&epoch->epoch_size, 0);
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001244 /* atomic_set(&epoch->active, 0); is already zero */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001245 if (rv == FE_STILL_LIVE)
1246 rv = FE_RECYCLED;
1247 }
1248 }
1249
1250 if (!next_epoch)
1251 break;
1252
1253 epoch = next_epoch;
1254 } while (1);
1255
Philipp Reisner12038a32011-11-09 19:18:00 +01001256 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001257
Philipp Reisnerb411b362009-09-25 16:07:19 -07001258 return rv;
1259}
1260
1261/**
1262 * drbd_bump_write_ordering() - Fall back to an other write ordering method
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001263 * @tconn: DRBD connection.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001264 * @wo: Write ordering method to try.
1265 */
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001266void drbd_bump_write_ordering(struct drbd_tconn *tconn, enum write_ordering_e wo)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001267{
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001268 struct disk_conf *dc;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001269 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001270 enum write_ordering_e pwo;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001271 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001272 static char *write_ordering_str[] = {
1273 [WO_none] = "none",
1274 [WO_drain_io] = "drain",
1275 [WO_bdev_flush] = "flush",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001276 };
1277
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001278 pwo = tconn->write_ordering;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001279 wo = min(pwo, wo);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001280 rcu_read_lock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001281 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisner27eb13e2012-03-30 14:12:15 +02001282 if (!get_ldev_if_state(mdev, D_ATTACHING))
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001283 continue;
1284 dc = rcu_dereference(mdev->ldev->disk_conf);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001285
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001286 if (wo == WO_bdev_flush && !dc->disk_flushes)
1287 wo = WO_drain_io;
1288 if (wo == WO_drain_io && !dc->disk_drain)
1289 wo = WO_none;
1290 put_ldev(mdev);
1291 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001292 rcu_read_unlock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001293 tconn->write_ordering = wo;
1294 if (pwo != tconn->write_ordering || wo == WO_bdev_flush)
1295 conn_info(tconn, "Method to ensure write ordering: %s\n", write_ordering_str[tconn->write_ordering]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001296}
1297
1298/**
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001299 * drbd_submit_peer_request()
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001300 * @mdev: DRBD device.
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001301 * @peer_req: peer request
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001302 * @rw: flag field, see bio->bi_rw
Lars Ellenberg10f6d992011-01-24 14:47:09 +01001303 *
1304 * May spread the pages to multiple bios,
1305 * depending on bio_add_page restrictions.
1306 *
1307 * Returns 0 if all bios have been submitted,
1308 * -ENOMEM if we could not allocate enough bios,
1309 * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a
1310 * single page to an empty bio (which should never happen and likely indicates
1311 * that the lower level IO stack is in some way broken). This has been observed
1312 * on certain Xen deployments.
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001313 */
1314/* TODO allocate from our own bio_set. */
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001315int drbd_submit_peer_request(struct drbd_conf *mdev,
1316 struct drbd_peer_request *peer_req,
1317 const unsigned rw, const int fault_type)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001318{
1319 struct bio *bios = NULL;
1320 struct bio *bio;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001321 struct page *page = peer_req->pages;
1322 sector_t sector = peer_req->i.sector;
1323 unsigned ds = peer_req->i.size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001324 unsigned n_bios = 0;
1325 unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg10f6d992011-01-24 14:47:09 +01001326 int err = -ENOMEM;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001327
1328 /* In most cases, we will only need one bio. But in case the lower
1329 * level restrictions happen to be different at this offset on this
1330 * side than those of the sending peer, we may need to submit the
Lars Ellenbergda4a75d2011-02-23 17:02:01 +01001331 * request in more than one bio.
1332 *
1333 * Plain bio_alloc is good enough here, this is no DRBD internally
1334 * generated bio, but a bio allocated on behalf of the peer.
1335 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001336next_bio:
1337 bio = bio_alloc(GFP_NOIO, nr_pages);
1338 if (!bio) {
1339 dev_err(DEV, "submit_ee: Allocation of a bio failed\n");
1340 goto fail;
1341 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001342 /* > peer_req->i.sector, unless this is the first bio */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001343 bio->bi_sector = sector;
1344 bio->bi_bdev = mdev->ldev->backing_bdev;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001345 bio->bi_rw = rw;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001346 bio->bi_private = peer_req;
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001347 bio->bi_end_io = drbd_peer_request_endio;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001348
1349 bio->bi_next = bios;
1350 bios = bio;
1351 ++n_bios;
1352
1353 page_chain_for_each(page) {
1354 unsigned len = min_t(unsigned, ds, PAGE_SIZE);
1355 if (!bio_add_page(bio, page, len, 0)) {
Lars Ellenberg10f6d992011-01-24 14:47:09 +01001356 /* A single page must always be possible!
1357 * But in case it fails anyways,
1358 * we deal with it, and complain (below). */
1359 if (bio->bi_vcnt == 0) {
1360 dev_err(DEV,
1361 "bio_add_page failed for len=%u, "
1362 "bi_vcnt=0 (bi_sector=%llu)\n",
1363 len, (unsigned long long)bio->bi_sector);
1364 err = -ENOSPC;
1365 goto fail;
1366 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001367 goto next_bio;
1368 }
1369 ds -= len;
1370 sector += len >> 9;
1371 --nr_pages;
1372 }
1373 D_ASSERT(page == NULL);
1374 D_ASSERT(ds == 0);
1375
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001376 atomic_set(&peer_req->pending_bios, n_bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001377 do {
1378 bio = bios;
1379 bios = bios->bi_next;
1380 bio->bi_next = NULL;
1381
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001382 drbd_generic_make_request(mdev, fault_type, bio);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001383 } while (bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001384 return 0;
1385
1386fail:
1387 while (bios) {
1388 bio = bios;
1389 bios = bios->bi_next;
1390 bio_put(bio);
1391 }
Lars Ellenberg10f6d992011-01-24 14:47:09 +01001392 return err;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001393}
1394
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001395static void drbd_remove_epoch_entry_interval(struct drbd_conf *mdev,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001396 struct drbd_peer_request *peer_req)
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001397{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001398 struct drbd_interval *i = &peer_req->i;
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001399
1400 drbd_remove_interval(&mdev->write_requests, i);
1401 drbd_clear_interval(i);
1402
Andreas Gruenbacher6c852be2011-02-04 15:38:52 +01001403 /* Wake up any processes waiting for this peer request to complete. */
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001404 if (i->waiting)
1405 wake_up(&mdev->misc_wait);
1406}
1407
Philipp Reisner77fede52011-11-10 21:19:11 +01001408void conn_wait_active_ee_empty(struct drbd_tconn *tconn)
1409{
1410 struct drbd_conf *mdev;
1411 int vnr;
1412
1413 rcu_read_lock();
1414 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1415 kref_get(&mdev->kref);
1416 rcu_read_unlock();
1417 drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
1418 kref_put(&mdev->kref, &drbd_minor_destroy);
1419 rcu_read_lock();
1420 }
1421 rcu_read_unlock();
1422}
1423
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001424static int receive_Barrier(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001425{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001426 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001427 struct p_barrier *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001428 struct drbd_epoch *epoch;
1429
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001430 /* FIXME these are unacked on connection,
1431 * not a specific (peer)device.
1432 */
Philipp Reisner12038a32011-11-09 19:18:00 +01001433 tconn->current_epoch->barrier_nr = p->barrier;
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001434 tconn->current_epoch->tconn = tconn;
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001435 rv = drbd_may_finish_epoch(tconn, tconn->current_epoch, EV_GOT_BARRIER_NR);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001436
1437 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
1438 * the activity log, which means it would not be resynced in case the
1439 * R_PRIMARY crashes now.
1440 * Therefore we must send the barrier_ack after the barrier request was
1441 * completed. */
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001442 switch (tconn->write_ordering) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001443 case WO_none:
1444 if (rv == FE_RECYCLED)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001445 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001446
1447 /* receiver context, in the writeout path of the other node.
1448 * avoid potential distributed deadlock */
1449 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1450 if (epoch)
1451 break;
1452 else
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001453 conn_warn(tconn, "Allocation of an epoch failed, slowing down\n");
Philipp Reisner2451fc32010-08-24 13:43:11 +02001454 /* Fall through */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001455
1456 case WO_bdev_flush:
1457 case WO_drain_io:
Philipp Reisner77fede52011-11-10 21:19:11 +01001458 conn_wait_active_ee_empty(tconn);
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001459 drbd_flush(tconn);
Philipp Reisner2451fc32010-08-24 13:43:11 +02001460
Philipp Reisner12038a32011-11-09 19:18:00 +01001461 if (atomic_read(&tconn->current_epoch->epoch_size)) {
Philipp Reisner2451fc32010-08-24 13:43:11 +02001462 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1463 if (epoch)
1464 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001465 }
1466
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001467 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001468 default:
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001469 conn_err(tconn, "Strangeness in tconn->write_ordering %d\n", tconn->write_ordering);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001470 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001471 }
1472
1473 epoch->flags = 0;
1474 atomic_set(&epoch->epoch_size, 0);
1475 atomic_set(&epoch->active, 0);
1476
Philipp Reisner12038a32011-11-09 19:18:00 +01001477 spin_lock(&tconn->epoch_lock);
1478 if (atomic_read(&tconn->current_epoch->epoch_size)) {
1479 list_add(&epoch->list, &tconn->current_epoch->list);
1480 tconn->current_epoch = epoch;
1481 tconn->epochs++;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001482 } else {
1483 /* The current_epoch got recycled while we allocated this one... */
1484 kfree(epoch);
1485 }
Philipp Reisner12038a32011-11-09 19:18:00 +01001486 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001487
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001488 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001489}
1490
1491/* used from receive_RSDataReply (recv_resync_read)
1492 * and from receive_Data */
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001493static struct drbd_peer_request *
1494read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector,
1495 int data_size) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001496{
Lars Ellenberg66660322010-04-06 12:15:04 +02001497 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001498 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001499 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001500 int dgs, ds, err;
Philipp Reisnera0638452011-01-19 14:31:32 +01001501 void *dig_in = mdev->tconn->int_dig_in;
1502 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001503 unsigned long *data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001504
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001505 dgs = 0;
1506 if (mdev->tconn->peer_integrity_tfm) {
1507 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001508 /*
1509 * FIXME: Receive the incoming digest into the receive buffer
1510 * here, together with its struct p_data?
1511 */
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001512 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1513 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001514 return NULL;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001515 data_size -= dgs;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001516 }
1517
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001518 if (!expect(IS_ALIGNED(data_size, 512)))
1519 return NULL;
1520 if (!expect(data_size <= DRBD_MAX_BIO_SIZE))
1521 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001522
Lars Ellenberg66660322010-04-06 12:15:04 +02001523 /* even though we trust out peer,
1524 * we sometimes have to double check. */
1525 if (sector + (data_size>>9) > capacity) {
Lars Ellenbergfdda6542011-01-24 15:11:01 +01001526 dev_err(DEV, "request from peer beyond end of local disk: "
1527 "capacity: %llus < sector: %llus + size: %u\n",
Lars Ellenberg66660322010-04-06 12:15:04 +02001528 (unsigned long long)capacity,
1529 (unsigned long long)sector, data_size);
1530 return NULL;
1531 }
1532
Philipp Reisnerb411b362009-09-25 16:07:19 -07001533 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1534 * "criss-cross" setup, that might cause write-out on some other DRBD,
1535 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02001536 peer_req = drbd_alloc_peer_req(mdev, id, sector, data_size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001537 if (!peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001538 return NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001539
Lars Ellenberg81a35372012-07-30 09:00:54 +02001540 if (!data_size)
1541 return peer_req;
1542
Philipp Reisnerb411b362009-09-25 16:07:19 -07001543 ds = data_size;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001544 page = peer_req->pages;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001545 page_chain_for_each(page) {
1546 unsigned len = min_t(int, ds, PAGE_SIZE);
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001547 data = kmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001548 err = drbd_recv_all_warn(mdev->tconn, data, len);
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +01001549 if (drbd_insert_fault(mdev, DRBD_FAULT_RECEIVE)) {
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001550 dev_err(DEV, "Fault injection: Corrupting data on receive\n");
1551 data[0] = data[0] ^ (unsigned long)-1;
1552 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001553 kunmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001554 if (err) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001555 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001556 return NULL;
1557 }
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001558 ds -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001559 }
1560
1561 if (dgs) {
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02001562 drbd_csum_ee(mdev, mdev->tconn->peer_integrity_tfm, peer_req, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001563 if (memcmp(dig_in, dig_vv, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001564 dev_err(DEV, "Digest integrity check FAILED: %llus +%u\n",
1565 (unsigned long long)sector, data_size);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001566 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001567 return NULL;
1568 }
1569 }
1570 mdev->recv_cnt += data_size>>9;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001571 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001572}
1573
1574/* drbd_drain_block() just takes a data block
1575 * out of the socket input buffer, and discards it.
1576 */
1577static int drbd_drain_block(struct drbd_conf *mdev, int data_size)
1578{
1579 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001580 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001581 void *data;
1582
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001583 if (!data_size)
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001584 return 0;
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001585
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +02001586 page = drbd_alloc_pages(mdev, 1, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001587
1588 data = kmap(page);
1589 while (data_size) {
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001590 unsigned int len = min_t(int, data_size, PAGE_SIZE);
1591
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001592 err = drbd_recv_all_warn(mdev->tconn, data, len);
1593 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001594 break;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001595 data_size -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001596 }
1597 kunmap(page);
Andreas Gruenbacher5cc287e2011-04-07 21:02:59 +02001598 drbd_free_pages(mdev, page, 0);
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001599 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001600}
1601
1602static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req,
1603 sector_t sector, int data_size)
1604{
1605 struct bio_vec *bvec;
1606 struct bio *bio;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001607 int dgs, err, i, expect;
Philipp Reisnera0638452011-01-19 14:31:32 +01001608 void *dig_in = mdev->tconn->int_dig_in;
1609 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001610
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001611 dgs = 0;
1612 if (mdev->tconn->peer_integrity_tfm) {
1613 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001614 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1615 if (err)
1616 return err;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001617 data_size -= dgs;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001618 }
1619
Philipp Reisnerb411b362009-09-25 16:07:19 -07001620 /* optimistically update recv_cnt. if receiving fails below,
1621 * we disconnect anyways, and counters will be reset. */
1622 mdev->recv_cnt += data_size>>9;
1623
1624 bio = req->master_bio;
1625 D_ASSERT(sector == bio->bi_sector);
1626
1627 bio_for_each_segment(bvec, bio, i) {
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001628 void *mapped = kmap(bvec->bv_page) + bvec->bv_offset;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001629 expect = min_t(int, data_size, bvec->bv_len);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001630 err = drbd_recv_all_warn(mdev->tconn, mapped, expect);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001631 kunmap(bvec->bv_page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001632 if (err)
1633 return err;
1634 data_size -= expect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001635 }
1636
1637 if (dgs) {
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02001638 drbd_csum_bio(mdev, mdev->tconn->peer_integrity_tfm, bio, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001639 if (memcmp(dig_in, dig_vv, dgs)) {
1640 dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n");
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001641 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001642 }
1643 }
1644
1645 D_ASSERT(data_size == 0);
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001646 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001647}
1648
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001649/*
1650 * e_end_resync_block() is called in asender context via
1651 * drbd_finish_peer_reqs().
1652 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001653static int e_end_resync_block(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001654{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001655 struct drbd_peer_request *peer_req =
1656 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001657 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001658 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001659 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001660
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001661 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001662
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001663 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1664 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001665 err = drbd_send_ack(mdev, P_RS_WRITE_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001666 } else {
1667 /* Record failure to sync */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001668 drbd_rs_failed_io(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001669
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001670 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001671 }
1672 dec_unacked(mdev);
1673
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001674 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001675}
1676
1677static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local)
1678{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001679 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001680
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001681 peer_req = read_in_block(mdev, ID_SYNCER, sector, data_size);
1682 if (!peer_req)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001683 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001684
1685 dec_rs_pending(mdev);
1686
Philipp Reisnerb411b362009-09-25 16:07:19 -07001687 inc_unacked(mdev);
1688 /* corresponding dec_unacked() in e_end_resync_block()
1689 * respective _drbd_clear_done_ee */
1690
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001691 peer_req->w.cb = e_end_resync_block;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001692
Philipp Reisner87eeee42011-01-19 14:16:30 +01001693 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001694 list_add(&peer_req->w.list, &mdev->sync_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001695 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001696
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001697 atomic_add(data_size >> 9, &mdev->rs_sect_ev);
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001698 if (drbd_submit_peer_request(mdev, peer_req, WRITE, DRBD_FAULT_RS_WR) == 0)
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001699 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001700
Lars Ellenberg10f6d992011-01-24 14:47:09 +01001701 /* don't care for the reason here */
1702 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01001703 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001704 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001705 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001706
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001707 drbd_free_peer_req(mdev, peer_req);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001708fail:
1709 put_ldev(mdev);
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001710 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001711}
1712
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001713static struct drbd_request *
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001714find_request(struct drbd_conf *mdev, struct rb_root *root, u64 id,
1715 sector_t sector, bool missing_ok, const char *func)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001716{
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001717 struct drbd_request *req;
1718
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001719 /* Request object according to our peer */
1720 req = (struct drbd_request *)(unsigned long)id;
Andreas Gruenbacher5e472262011-01-27 14:42:51 +01001721 if (drbd_contains_interval(root, sector, &req->i) && req->i.local)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001722 return req;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001723 if (!missing_ok) {
Andreas Gruenbacher5af172e2011-07-15 09:43:23 +02001724 dev_err(DEV, "%s: failed to find request 0x%lx, sector %llus\n", func,
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001725 (unsigned long)id, (unsigned long long)sector);
1726 }
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001727 return NULL;
1728}
1729
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001730static int receive_DataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001731{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001732 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001733 struct drbd_request *req;
1734 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001735 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001736 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001737
1738 mdev = vnr_to_mdev(tconn, pi->vnr);
1739 if (!mdev)
1740 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001741
1742 sector = be64_to_cpu(p->sector);
1743
Philipp Reisner87eeee42011-01-19 14:16:30 +01001744 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001745 req = find_request(mdev, &mdev->read_requests, p->block_id, sector, false, __func__);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001746 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001747 if (unlikely(!req))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001748 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001749
Bart Van Assche24c48302011-05-21 18:32:29 +02001750 /* hlist_del(&req->collision) is done in _req_may_be_done, to avoid
Philipp Reisnerb411b362009-09-25 16:07:19 -07001751 * special casing it there for the various failure cases.
1752 * still no race with drbd_fail_pending_reads */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001753 err = recv_dless_read(mdev, req, sector, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001754 if (!err)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001755 req_mod(req, DATA_RECEIVED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001756 /* else: nothing. handled from drbd_disconnect...
1757 * I don't think we may complete this just yet
1758 * in case we are "on-disconnect: freeze" */
1759
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001760 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001761}
1762
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001763static int receive_RSDataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001764{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001765 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001766 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001767 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001768 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001769
1770 mdev = vnr_to_mdev(tconn, pi->vnr);
1771 if (!mdev)
1772 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001773
1774 sector = be64_to_cpu(p->sector);
1775 D_ASSERT(p->block_id == ID_SYNCER);
1776
1777 if (get_ldev(mdev)) {
1778 /* data is submitted to disk within recv_resync_read.
1779 * corresponding put_ldev done below on error,
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001780 * or in drbd_peer_request_endio. */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001781 err = recv_resync_read(mdev, sector, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001782 } else {
1783 if (__ratelimit(&drbd_ratelimit_state))
1784 dev_err(DEV, "Can not write resync data to local disk.\n");
1785
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001786 err = drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001787
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001788 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001789 }
1790
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001791 atomic_add(pi->size >> 9, &mdev->rs_sect_in);
Philipp Reisner778f2712010-07-06 11:14:00 +02001792
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001793 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001794}
1795
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001796static void restart_conflicting_writes(struct drbd_conf *mdev,
1797 sector_t sector, int size)
1798{
1799 struct drbd_interval *i;
1800 struct drbd_request *req;
1801
1802 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1803 if (!i->local)
1804 continue;
1805 req = container_of(i, struct drbd_request, i);
1806 if (req->rq_state & RQ_LOCAL_PENDING ||
1807 !(req->rq_state & RQ_POSTPONED))
1808 continue;
Lars Ellenberg2312f0b32011-11-24 10:36:25 +01001809 /* as it is RQ_POSTPONED, this will cause it to
1810 * be queued on the retry workqueue. */
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02001811 __req_mod(req, CONFLICT_RESOLVED, NULL);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001812 }
1813}
1814
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001815/*
1816 * e_end_block() is called in asender context via drbd_finish_peer_reqs().
Philipp Reisnerb411b362009-09-25 16:07:19 -07001817 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001818static int e_end_block(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001819{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001820 struct drbd_peer_request *peer_req =
1821 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001822 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001823 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001824 int err = 0, pcmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001825
Philipp Reisner303d1442011-04-13 16:24:47 -07001826 if (peer_req->flags & EE_SEND_WRITE_ACK) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001827 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001828 pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
1829 mdev->state.conn <= C_PAUSED_SYNC_T &&
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001830 peer_req->flags & EE_MAY_SET_IN_SYNC) ?
Philipp Reisnerb411b362009-09-25 16:07:19 -07001831 P_RS_WRITE_ACK : P_WRITE_ACK;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001832 err = drbd_send_ack(mdev, pcmd, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001833 if (pcmd == P_RS_WRITE_ACK)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001834 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001835 } else {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001836 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001837 /* we expect it to be marked out of sync anyways...
1838 * maybe assert this? */
1839 }
1840 dec_unacked(mdev);
1841 }
1842 /* we delete from the conflict detection hash _after_ we sent out the
1843 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
Philipp Reisner302bdea2011-04-21 11:36:49 +02001844 if (peer_req->flags & EE_IN_INTERVAL_TREE) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01001845 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001846 D_ASSERT(!drbd_interval_empty(&peer_req->i));
1847 drbd_remove_epoch_entry_interval(mdev, peer_req);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001848 if (peer_req->flags & EE_RESTART_REQUESTS)
1849 restart_conflicting_writes(mdev, sector, peer_req->i.size);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001850 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +01001851 } else
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001852 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001853
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001854 drbd_may_finish_epoch(mdev->tconn, peer_req->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001855
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001856 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001857}
1858
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001859static int e_send_ack(struct drbd_work *w, enum drbd_packet ack)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001860{
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001861 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001862 struct drbd_peer_request *peer_req =
1863 container_of(w, struct drbd_peer_request, w);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001864 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001865
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001866 err = drbd_send_ack(mdev, ack, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001867 dec_unacked(mdev);
1868
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001869 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001870}
1871
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02001872static int e_send_superseded(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001873{
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02001874 return e_send_ack(w, P_SUPERSEDED);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001875}
1876
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001877static int e_send_retry_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001878{
1879 struct drbd_tconn *tconn = w->mdev->tconn;
1880
1881 return e_send_ack(w, tconn->agreed_pro_version >= 100 ?
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02001882 P_RETRY_WRITE : P_SUPERSEDED);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001883}
1884
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001885static bool seq_greater(u32 a, u32 b)
1886{
1887 /*
1888 * We assume 32-bit wrap-around here.
1889 * For 24-bit wrap-around, we would have to shift:
1890 * a <<= 8; b <<= 8;
1891 */
1892 return (s32)a - (s32)b > 0;
1893}
1894
1895static u32 seq_max(u32 a, u32 b)
1896{
1897 return seq_greater(a, b) ? a : b;
1898}
1899
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001900static bool need_peer_seq(struct drbd_conf *mdev)
1901{
1902 struct drbd_tconn *tconn = mdev->tconn;
Philipp Reisner302bdea2011-04-21 11:36:49 +02001903 int tp;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001904
1905 /*
1906 * We only need to keep track of the last packet_seq number of our peer
Lars Ellenberg427c0432012-08-01 12:43:01 +02001907 * if we are in dual-primary mode and we have the resolve-conflicts flag set; see
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001908 * handle_write_conflicts().
1909 */
Philipp Reisner302bdea2011-04-21 11:36:49 +02001910
1911 rcu_read_lock();
1912 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
1913 rcu_read_unlock();
1914
Lars Ellenberg427c0432012-08-01 12:43:01 +02001915 return tp && test_bit(RESOLVE_CONFLICTS, &tconn->flags);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001916}
1917
Andreas Gruenbacher43ae0772011-02-03 18:42:08 +01001918static void update_peer_seq(struct drbd_conf *mdev, unsigned int peer_seq)
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001919{
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001920 unsigned int newest_peer_seq;
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001921
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001922 if (need_peer_seq(mdev)) {
1923 spin_lock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001924 newest_peer_seq = seq_max(mdev->peer_seq, peer_seq);
1925 mdev->peer_seq = newest_peer_seq;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001926 spin_unlock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001927 /* wake up only if we actually changed mdev->peer_seq */
1928 if (peer_seq == newest_peer_seq)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001929 wake_up(&mdev->seq_wait);
1930 }
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001931}
1932
Lars Ellenbergd93f6302012-03-26 15:49:13 +02001933static inline int overlaps(sector_t s1, int l1, sector_t s2, int l2)
1934{
1935 return !((s1 + (l1>>9) <= s2) || (s1 >= s2 + (l2>>9)));
1936}
1937
1938/* maybe change sync_ee into interval trees as well? */
Philipp Reisner3ea35df2012-04-06 12:13:18 +02001939static bool overlapping_resync_write(struct drbd_conf *mdev, struct drbd_peer_request *peer_req)
Lars Ellenbergd93f6302012-03-26 15:49:13 +02001940{
1941 struct drbd_peer_request *rs_req;
1942 bool rv = 0;
1943
1944 spin_lock_irq(&mdev->tconn->req_lock);
1945 list_for_each_entry(rs_req, &mdev->sync_ee, w.list) {
1946 if (overlaps(peer_req->i.sector, peer_req->i.size,
1947 rs_req->i.sector, rs_req->i.size)) {
1948 rv = 1;
1949 break;
1950 }
1951 }
1952 spin_unlock_irq(&mdev->tconn->req_lock);
1953
Lars Ellenbergd93f6302012-03-26 15:49:13 +02001954 return rv;
1955}
1956
Philipp Reisnerb411b362009-09-25 16:07:19 -07001957/* Called from receive_Data.
1958 * Synchronize packets on sock with packets on msock.
1959 *
1960 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
1961 * packet traveling on msock, they are still processed in the order they have
1962 * been sent.
1963 *
1964 * Note: we don't care for Ack packets overtaking P_DATA packets.
1965 *
1966 * In case packet_seq is larger than mdev->peer_seq number, there are
1967 * outstanding packets on the msock. We wait for them to arrive.
1968 * In case we are the logically next packet, we update mdev->peer_seq
1969 * ourselves. Correctly handles 32bit wrap around.
1970 *
1971 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
1972 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
1973 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
1974 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
1975 *
1976 * returns 0 if we may process the packet,
1977 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001978static int wait_for_and_update_peer_seq(struct drbd_conf *mdev, const u32 peer_seq)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001979{
1980 DEFINE_WAIT(wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001981 long timeout;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001982 int ret;
1983
1984 if (!need_peer_seq(mdev))
1985 return 0;
1986
Philipp Reisnerb411b362009-09-25 16:07:19 -07001987 spin_lock(&mdev->peer_seq_lock);
1988 for (;;) {
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001989 if (!seq_greater(peer_seq - 1, mdev->peer_seq)) {
1990 mdev->peer_seq = seq_max(mdev->peer_seq, peer_seq);
1991 ret = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001992 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001993 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001994 if (signal_pending(current)) {
1995 ret = -ERESTARTSYS;
1996 break;
1997 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001998 prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001999 spin_unlock(&mdev->peer_seq_lock);
Philipp Reisner44ed1672011-04-19 17:10:19 +02002000 rcu_read_lock();
2001 timeout = rcu_dereference(mdev->tconn->net_conf)->ping_timeo*HZ/10;
2002 rcu_read_unlock();
Andreas Gruenbacher71b1c1eb2011-03-01 15:40:43 +01002003 timeout = schedule_timeout(timeout);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002004 spin_lock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002005 if (!timeout) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002006 ret = -ETIMEDOUT;
Andreas Gruenbacher71b1c1eb2011-03-01 15:40:43 +01002007 dev_err(DEV, "Timed out waiting for missing ack packets; disconnecting\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002008 break;
2009 }
2010 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002011 spin_unlock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002012 finish_wait(&mdev->seq_wait, &wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002013 return ret;
2014}
2015
Lars Ellenberg688593c2010-11-17 22:25:03 +01002016/* see also bio_flags_to_wire()
2017 * DRBD_REQ_*, because we need to semantically map the flags to data packet
2018 * flags and back. We may replicate to other kernel versions. */
2019static unsigned long wire_flags_to_bio(struct drbd_conf *mdev, u32 dpf)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02002020{
Lars Ellenberg688593c2010-11-17 22:25:03 +01002021 return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
2022 (dpf & DP_FUA ? REQ_FUA : 0) |
2023 (dpf & DP_FLUSH ? REQ_FLUSH : 0) |
2024 (dpf & DP_DISCARD ? REQ_DISCARD : 0);
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02002025}
2026
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002027static void fail_postponed_requests(struct drbd_conf *mdev, sector_t sector,
2028 unsigned int size)
2029{
2030 struct drbd_interval *i;
2031
2032 repeat:
2033 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
2034 struct drbd_request *req;
2035 struct bio_and_error m;
2036
2037 if (!i->local)
2038 continue;
2039 req = container_of(i, struct drbd_request, i);
2040 if (!(req->rq_state & RQ_POSTPONED))
2041 continue;
2042 req->rq_state &= ~RQ_POSTPONED;
2043 __req_mod(req, NEG_ACKED, &m);
2044 spin_unlock_irq(&mdev->tconn->req_lock);
2045 if (m.bio)
2046 complete_master_bio(mdev, &m);
2047 spin_lock_irq(&mdev->tconn->req_lock);
2048 goto repeat;
2049 }
2050}
2051
2052static int handle_write_conflicts(struct drbd_conf *mdev,
2053 struct drbd_peer_request *peer_req)
2054{
2055 struct drbd_tconn *tconn = mdev->tconn;
Lars Ellenberg427c0432012-08-01 12:43:01 +02002056 bool resolve_conflicts = test_bit(RESOLVE_CONFLICTS, &tconn->flags);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002057 sector_t sector = peer_req->i.sector;
2058 const unsigned int size = peer_req->i.size;
2059 struct drbd_interval *i;
2060 bool equal;
2061 int err;
2062
2063 /*
2064 * Inserting the peer request into the write_requests tree will prevent
2065 * new conflicting local requests from being added.
2066 */
2067 drbd_insert_interval(&mdev->write_requests, &peer_req->i);
2068
2069 repeat:
2070 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
2071 if (i == &peer_req->i)
2072 continue;
2073
2074 if (!i->local) {
2075 /*
2076 * Our peer has sent a conflicting remote request; this
2077 * should not happen in a two-node setup. Wait for the
2078 * earlier peer request to complete.
2079 */
2080 err = drbd_wait_misc(mdev, i);
2081 if (err)
2082 goto out;
2083 goto repeat;
2084 }
2085
2086 equal = i->sector == sector && i->size == size;
2087 if (resolve_conflicts) {
2088 /*
2089 * If the peer request is fully contained within the
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02002090 * overlapping request, it can be considered overwritten
2091 * and thus superseded; otherwise, it will be retried
2092 * once all overlapping requests have completed.
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002093 */
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02002094 bool superseded = i->sector <= sector && i->sector +
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002095 (i->size >> 9) >= sector + (size >> 9);
2096
2097 if (!equal)
2098 dev_alert(DEV, "Concurrent writes detected: "
2099 "local=%llus +%u, remote=%llus +%u, "
2100 "assuming %s came first\n",
2101 (unsigned long long)i->sector, i->size,
2102 (unsigned long long)sector, size,
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02002103 superseded ? "local" : "remote");
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002104
2105 inc_unacked(mdev);
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02002106 peer_req->w.cb = superseded ? e_send_superseded :
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002107 e_send_retry_write;
2108 list_add_tail(&peer_req->w.list, &mdev->done_ee);
2109 wake_asender(mdev->tconn);
2110
2111 err = -ENOENT;
2112 goto out;
2113 } else {
2114 struct drbd_request *req =
2115 container_of(i, struct drbd_request, i);
2116
2117 if (!equal)
2118 dev_alert(DEV, "Concurrent writes detected: "
2119 "local=%llus +%u, remote=%llus +%u\n",
2120 (unsigned long long)i->sector, i->size,
2121 (unsigned long long)sector, size);
2122
2123 if (req->rq_state & RQ_LOCAL_PENDING ||
2124 !(req->rq_state & RQ_POSTPONED)) {
2125 /*
2126 * Wait for the node with the discard flag to
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02002127 * decide if this request has been superseded
2128 * or needs to be retried.
2129 * Requests that have been superseded will
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002130 * disappear from the write_requests tree.
2131 *
2132 * In addition, wait for the conflicting
2133 * request to finish locally before submitting
2134 * the conflicting peer request.
2135 */
2136 err = drbd_wait_misc(mdev, &req->i);
2137 if (err) {
2138 _conn_request_state(mdev->tconn,
2139 NS(conn, C_TIMEOUT),
2140 CS_HARD);
2141 fail_postponed_requests(mdev, sector, size);
2142 goto out;
2143 }
2144 goto repeat;
2145 }
2146 /*
2147 * Remember to restart the conflicting requests after
2148 * the new peer request has completed.
2149 */
2150 peer_req->flags |= EE_RESTART_REQUESTS;
2151 }
2152 }
2153 err = 0;
2154
2155 out:
2156 if (err)
2157 drbd_remove_epoch_entry_interval(mdev, peer_req);
2158 return err;
2159}
2160
Philipp Reisnerb411b362009-09-25 16:07:19 -07002161/* mirrored write */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002162static int receive_Data(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002163{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002164 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002165 sector_t sector;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002166 struct drbd_peer_request *peer_req;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002167 struct p_data *p = pi->data;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002168 u32 peer_seq = be32_to_cpu(p->seq_num);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002169 int rw = WRITE;
2170 u32 dp_flags;
Philipp Reisner302bdea2011-04-21 11:36:49 +02002171 int err, tp;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002172
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002173 mdev = vnr_to_mdev(tconn, pi->vnr);
2174 if (!mdev)
2175 return -EIO;
2176
Philipp Reisnerb411b362009-09-25 16:07:19 -07002177 if (!get_ldev(mdev)) {
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002178 int err2;
2179
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002180 err = wait_for_and_update_peer_seq(mdev, peer_seq);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002181 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisner12038a32011-11-09 19:18:00 +01002182 atomic_inc(&tconn->current_epoch->epoch_size);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002183 err2 = drbd_drain_block(mdev, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002184 if (!err)
2185 err = err2;
2186 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002187 }
2188
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01002189 /*
2190 * Corresponding put_ldev done either below (on various errors), or in
2191 * drbd_peer_request_endio, if we successfully submit the data at the
2192 * end of this function.
2193 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002194
2195 sector = be64_to_cpu(p->sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002196 peer_req = read_in_block(mdev, p->block_id, sector, pi->size);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002197 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002198 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002199 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002200 }
2201
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002202 peer_req->w.cb = e_end_block;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002203
Lars Ellenberg688593c2010-11-17 22:25:03 +01002204 dp_flags = be32_to_cpu(p->dp_flags);
2205 rw |= wire_flags_to_bio(mdev, dp_flags);
Lars Ellenberg81a35372012-07-30 09:00:54 +02002206 if (peer_req->pages == NULL) {
2207 D_ASSERT(peer_req->i.size == 0);
2208 D_ASSERT(dp_flags & DP_FLUSH);
2209 }
Lars Ellenberg688593c2010-11-17 22:25:03 +01002210
2211 if (dp_flags & DP_MAY_SET_IN_SYNC)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002212 peer_req->flags |= EE_MAY_SET_IN_SYNC;
Lars Ellenberg688593c2010-11-17 22:25:03 +01002213
Philipp Reisner12038a32011-11-09 19:18:00 +01002214 spin_lock(&tconn->epoch_lock);
2215 peer_req->epoch = tconn->current_epoch;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002216 atomic_inc(&peer_req->epoch->epoch_size);
2217 atomic_inc(&peer_req->epoch->active);
Philipp Reisner12038a32011-11-09 19:18:00 +01002218 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002219
Philipp Reisner302bdea2011-04-21 11:36:49 +02002220 rcu_read_lock();
2221 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
2222 rcu_read_unlock();
2223 if (tp) {
2224 peer_req->flags |= EE_IN_INTERVAL_TREE;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002225 err = wait_for_and_update_peer_seq(mdev, peer_seq);
2226 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002227 goto out_interrupted;
Philipp Reisner87eeee42011-01-19 14:16:30 +01002228 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002229 err = handle_write_conflicts(mdev, peer_req);
2230 if (err) {
2231 spin_unlock_irq(&mdev->tconn->req_lock);
2232 if (err == -ENOENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002233 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002234 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002235 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002236 goto out_interrupted;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002237 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002238 } else
2239 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002240 list_add(&peer_req->w.list, &mdev->active_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002241 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002242
Lars Ellenbergd93f6302012-03-26 15:49:13 +02002243 if (mdev->state.conn == C_SYNC_TARGET)
Philipp Reisner3ea35df2012-04-06 12:13:18 +02002244 wait_event(mdev->ee_wait, !overlapping_resync_write(mdev, peer_req));
Lars Ellenbergd93f6302012-03-26 15:49:13 +02002245
Philipp Reisner303d1442011-04-13 16:24:47 -07002246 if (mdev->tconn->agreed_pro_version < 100) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02002247 rcu_read_lock();
2248 switch (rcu_dereference(mdev->tconn->net_conf)->wire_protocol) {
Philipp Reisner303d1442011-04-13 16:24:47 -07002249 case DRBD_PROT_C:
2250 dp_flags |= DP_SEND_WRITE_ACK;
2251 break;
2252 case DRBD_PROT_B:
2253 dp_flags |= DP_SEND_RECEIVE_ACK;
2254 break;
2255 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002256 rcu_read_unlock();
Philipp Reisner303d1442011-04-13 16:24:47 -07002257 }
2258
2259 if (dp_flags & DP_SEND_WRITE_ACK) {
2260 peer_req->flags |= EE_SEND_WRITE_ACK;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002261 inc_unacked(mdev);
2262 /* corresponding dec_unacked() in e_end_block()
2263 * respective _drbd_clear_done_ee */
Philipp Reisner303d1442011-04-13 16:24:47 -07002264 }
2265
2266 if (dp_flags & DP_SEND_RECEIVE_ACK) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002267 /* I really don't like it that the receiver thread
2268 * sends on the msock, but anyways */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002269 drbd_send_ack(mdev, P_RECV_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002270 }
2271
Lars Ellenberg6719fb02010-10-18 23:04:07 +02002272 if (mdev->state.pdsk < D_INCONSISTENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002273 /* In case we have the only disk of the cluster, */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002274 drbd_set_out_of_sync(mdev, peer_req->i.sector, peer_req->i.size);
2275 peer_req->flags |= EE_CALL_AL_COMPLETE_IO;
2276 peer_req->flags &= ~EE_MAY_SET_IN_SYNC;
Lars Ellenberg181286a2011-03-31 15:18:56 +02002277 drbd_al_begin_io(mdev, &peer_req->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002278 }
2279
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002280 err = drbd_submit_peer_request(mdev, peer_req, rw, DRBD_FAULT_DT_WR);
2281 if (!err)
2282 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002283
Lars Ellenberg10f6d992011-01-24 14:47:09 +01002284 /* don't care for the reason here */
2285 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002286 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002287 list_del(&peer_req->w.list);
2288 drbd_remove_epoch_entry_interval(mdev, peer_req);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002289 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002290 if (peer_req->flags & EE_CALL_AL_COMPLETE_IO)
Lars Ellenberg181286a2011-03-31 15:18:56 +02002291 drbd_al_complete_io(mdev, &peer_req->i);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002292
Philipp Reisnerb411b362009-09-25 16:07:19 -07002293out_interrupted:
Philipp Reisner1e9dd292011-11-10 15:14:53 +01002294 drbd_may_finish_epoch(tconn, peer_req->epoch, EV_PUT + EV_CLEANUP);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002295 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002296 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002297 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002298}
2299
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002300/* We may throttle resync, if the lower device seems to be busy,
2301 * and current sync rate is above c_min_rate.
2302 *
2303 * To decide whether or not the lower device is busy, we use a scheme similar
2304 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
2305 * (more than 64 sectors) of activity we cannot account for with our own resync
2306 * activity, it obviously is "busy".
2307 *
2308 * The current sync rate used here uses only the most recent two step marks,
2309 * to have a short time average so we can react faster.
2310 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002311int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002312{
2313 struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
2314 unsigned long db, dt, dbdt;
Philipp Reisnere3555d82010-11-07 15:56:29 +01002315 struct lc_element *tmp;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002316 int curr_events;
2317 int throttle = 0;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002318 unsigned int c_min_rate;
2319
2320 rcu_read_lock();
2321 c_min_rate = rcu_dereference(mdev->ldev->disk_conf)->c_min_rate;
2322 rcu_read_unlock();
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002323
2324 /* feature disabled? */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002325 if (c_min_rate == 0)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002326 return 0;
2327
Philipp Reisnere3555d82010-11-07 15:56:29 +01002328 spin_lock_irq(&mdev->al_lock);
2329 tmp = lc_find(mdev->resync, BM_SECT_TO_EXT(sector));
2330 if (tmp) {
2331 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
2332 if (test_bit(BME_PRIORITY, &bm_ext->flags)) {
2333 spin_unlock_irq(&mdev->al_lock);
2334 return 0;
2335 }
2336 /* Do not slow down if app IO is already waiting for this extent */
2337 }
2338 spin_unlock_irq(&mdev->al_lock);
2339
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002340 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
2341 (int)part_stat_read(&disk->part0, sectors[1]) -
2342 atomic_read(&mdev->rs_sect_ev);
Philipp Reisnere3555d82010-11-07 15:56:29 +01002343
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002344 if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
2345 unsigned long rs_left;
2346 int i;
2347
2348 mdev->rs_last_events = curr_events;
2349
2350 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
2351 * approx. */
Lars Ellenberg2649f082010-11-05 10:05:47 +01002352 i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
2353
2354 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
2355 rs_left = mdev->ov_left;
2356 else
2357 rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002358
2359 dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
2360 if (!dt)
2361 dt++;
2362 db = mdev->rs_mark_left[i] - rs_left;
2363 dbdt = Bit2KB(db/dt);
2364
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002365 if (dbdt > c_min_rate)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002366 throttle = 1;
2367 }
2368 return throttle;
2369}
2370
2371
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002372static int receive_DataRequest(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002373{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002374 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002375 sector_t sector;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002376 sector_t capacity;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002377 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002378 struct digest_info *di = NULL;
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002379 int size, verb;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002380 unsigned int fault_type;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002381 struct p_block_req *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002382
2383 mdev = vnr_to_mdev(tconn, pi->vnr);
2384 if (!mdev)
2385 return -EIO;
2386 capacity = drbd_get_capacity(mdev->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002387
2388 sector = be64_to_cpu(p->sector);
2389 size = be32_to_cpu(p->blksize);
2390
Andreas Gruenbacherc670a392011-02-21 12:41:39 +01002391 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002392 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2393 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002394 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002395 }
2396 if (sector + (size>>9) > capacity) {
2397 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2398 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002399 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002400 }
2401
2402 if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002403 verb = 1;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002404 switch (pi->cmd) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002405 case P_DATA_REQUEST:
2406 drbd_send_ack_rp(mdev, P_NEG_DREPLY, p);
2407 break;
2408 case P_RS_DATA_REQUEST:
2409 case P_CSUM_RS_REQUEST:
2410 case P_OV_REQUEST:
2411 drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p);
2412 break;
2413 case P_OV_REPLY:
2414 verb = 0;
2415 dec_rs_pending(mdev);
2416 drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC);
2417 break;
2418 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002419 BUG();
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002420 }
2421 if (verb && __ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002422 dev_err(DEV, "Can not satisfy peer's read request, "
2423 "no local data.\n");
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002424
Lars Ellenberga821cc42010-09-06 12:31:37 +02002425 /* drain possibly payload */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002426 return drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002427 }
2428
2429 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
2430 * "criss-cross" setup, that might cause write-out on some other DRBD,
2431 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02002432 peer_req = drbd_alloc_peer_req(mdev, p->block_id, sector, size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002433 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002434 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002435 return -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002436 }
2437
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002438 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002439 case P_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002440 peer_req->w.cb = w_e_end_data_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002441 fault_type = DRBD_FAULT_DT_RD;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002442 /* application IO, don't drbd_rs_begin_io */
2443 goto submit;
2444
Philipp Reisnerb411b362009-09-25 16:07:19 -07002445 case P_RS_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002446 peer_req->w.cb = w_e_end_rsdata_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002447 fault_type = DRBD_FAULT_RS_RD;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002448 /* used in the sector offset progress display */
2449 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002450 break;
2451
2452 case P_OV_REPLY:
2453 case P_CSUM_RS_REQUEST:
2454 fault_type = DRBD_FAULT_RS_RD;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002455 di = kmalloc(sizeof(*di) + pi->size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002456 if (!di)
2457 goto out_free_e;
2458
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002459 di->digest_size = pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002460 di->digest = (((char *)di)+sizeof(struct digest_info));
2461
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002462 peer_req->digest = di;
2463 peer_req->flags |= EE_HAS_DIGEST;
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +02002464
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002465 if (drbd_recv_all(mdev->tconn, di->digest, pi->size))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002466 goto out_free_e;
2467
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002468 if (pi->cmd == P_CSUM_RS_REQUEST) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002469 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002470 peer_req->w.cb = w_e_end_csum_rs_req;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002471 /* used in the sector offset progress display */
2472 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002473 } else if (pi->cmd == P_OV_REPLY) {
Lars Ellenberg2649f082010-11-05 10:05:47 +01002474 /* track progress, we may need to throttle */
2475 atomic_add(size >> 9, &mdev->rs_sect_in);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002476 peer_req->w.cb = w_e_end_ov_reply;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002477 dec_rs_pending(mdev);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002478 /* drbd_rs_begin_io done when we sent this request,
2479 * but accounting still needs to be done. */
2480 goto submit_for_resync;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002481 }
2482 break;
2483
2484 case P_OV_REQUEST:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002485 if (mdev->ov_start_sector == ~(sector_t)0 &&
Philipp Reisner31890f42011-01-19 14:12:51 +01002486 mdev->tconn->agreed_pro_version >= 90) {
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002487 unsigned long now = jiffies;
2488 int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002489 mdev->ov_start_sector = sector;
2490 mdev->ov_position = sector;
Lars Ellenberg30b743a2010-11-05 09:39:06 +01002491 mdev->ov_left = drbd_bm_bits(mdev) - BM_SECT_TO_BIT(sector);
2492 mdev->rs_total = mdev->ov_left;
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002493 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
2494 mdev->rs_mark_left[i] = mdev->ov_left;
2495 mdev->rs_mark_time[i] = now;
2496 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002497 dev_info(DEV, "Online Verify start sector: %llu\n",
2498 (unsigned long long)sector);
2499 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002500 peer_req->w.cb = w_e_end_ov_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002501 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002502 break;
2503
Philipp Reisnerb411b362009-09-25 16:07:19 -07002504 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002505 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002506 }
2507
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002508 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2509 * wrt the receiver, but it is not as straightforward as it may seem.
2510 * Various places in the resync start and stop logic assume resync
2511 * requests are processed in order, requeuing this on the worker thread
2512 * introduces a bunch of new code for synchronization between threads.
2513 *
2514 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2515 * "forever", throttling after drbd_rs_begin_io will lock that extent
2516 * for application writes for the same time. For now, just throttle
2517 * here, where the rest of the code expects the receiver to sleep for
2518 * a while, anyways.
2519 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002520
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002521 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2522 * this defers syncer requests for some time, before letting at least
2523 * on request through. The resync controller on the receiving side
2524 * will adapt to the incoming rate accordingly.
2525 *
2526 * We cannot throttle here if remote is Primary/SyncTarget:
2527 * we would also throttle its application reads.
2528 * In that case, throttling is done on the SyncTarget only.
2529 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002530 if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev, sector))
2531 schedule_timeout_uninterruptible(HZ/10);
2532 if (drbd_rs_begin_io(mdev, sector))
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002533 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002534
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002535submit_for_resync:
2536 atomic_add(size >> 9, &mdev->rs_sect_ev);
2537
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002538submit:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002539 inc_unacked(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002540 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002541 list_add_tail(&peer_req->w.list, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002542 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002543
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01002544 if (drbd_submit_peer_request(mdev, peer_req, READ, fault_type) == 0)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002545 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002546
Lars Ellenberg10f6d992011-01-24 14:47:09 +01002547 /* don't care for the reason here */
2548 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002549 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002550 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002551 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002552 /* no drbd_rs_complete_io(), we are dropping the connection anyways */
2553
Philipp Reisnerb411b362009-09-25 16:07:19 -07002554out_free_e:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002555 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002556 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002557 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002558}
2559
2560static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
2561{
2562 int self, peer, rv = -100;
2563 unsigned long ch_self, ch_peer;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002564 enum drbd_after_sb_p after_sb_0p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002565
2566 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2567 peer = mdev->p_uuid[UI_BITMAP] & 1;
2568
2569 ch_peer = mdev->p_uuid[UI_SIZE];
2570 ch_self = mdev->comm_bm_set;
2571
Philipp Reisner44ed1672011-04-19 17:10:19 +02002572 rcu_read_lock();
2573 after_sb_0p = rcu_dereference(mdev->tconn->net_conf)->after_sb_0p;
2574 rcu_read_unlock();
2575 switch (after_sb_0p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002576 case ASB_CONSENSUS:
2577 case ASB_DISCARD_SECONDARY:
2578 case ASB_CALL_HELPER:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002579 case ASB_VIOLENTLY:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002580 dev_err(DEV, "Configuration error.\n");
2581 break;
2582 case ASB_DISCONNECT:
2583 break;
2584 case ASB_DISCARD_YOUNGER_PRI:
2585 if (self == 0 && peer == 1) {
2586 rv = -1;
2587 break;
2588 }
2589 if (self == 1 && peer == 0) {
2590 rv = 1;
2591 break;
2592 }
2593 /* Else fall through to one of the other strategies... */
2594 case ASB_DISCARD_OLDER_PRI:
2595 if (self == 0 && peer == 1) {
2596 rv = 1;
2597 break;
2598 }
2599 if (self == 1 && peer == 0) {
2600 rv = -1;
2601 break;
2602 }
2603 /* Else fall through to one of the other strategies... */
Lars Ellenbergad19bf62009-10-14 09:36:49 +02002604 dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
Philipp Reisnerb411b362009-09-25 16:07:19 -07002605 "Using discard-least-changes instead\n");
2606 case ASB_DISCARD_ZERO_CHG:
2607 if (ch_peer == 0 && ch_self == 0) {
Lars Ellenberg427c0432012-08-01 12:43:01 +02002608 rv = test_bit(RESOLVE_CONFLICTS, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002609 ? -1 : 1;
2610 break;
2611 } else {
2612 if (ch_peer == 0) { rv = 1; break; }
2613 if (ch_self == 0) { rv = -1; break; }
2614 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002615 if (after_sb_0p == ASB_DISCARD_ZERO_CHG)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002616 break;
2617 case ASB_DISCARD_LEAST_CHG:
2618 if (ch_self < ch_peer)
2619 rv = -1;
2620 else if (ch_self > ch_peer)
2621 rv = 1;
2622 else /* ( ch_self == ch_peer ) */
2623 /* Well, then use something else. */
Lars Ellenberg427c0432012-08-01 12:43:01 +02002624 rv = test_bit(RESOLVE_CONFLICTS, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002625 ? -1 : 1;
2626 break;
2627 case ASB_DISCARD_LOCAL:
2628 rv = -1;
2629 break;
2630 case ASB_DISCARD_REMOTE:
2631 rv = 1;
2632 }
2633
2634 return rv;
2635}
2636
2637static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
2638{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002639 int hg, rv = -100;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002640 enum drbd_after_sb_p after_sb_1p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002641
Philipp Reisner44ed1672011-04-19 17:10:19 +02002642 rcu_read_lock();
2643 after_sb_1p = rcu_dereference(mdev->tconn->net_conf)->after_sb_1p;
2644 rcu_read_unlock();
2645 switch (after_sb_1p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002646 case ASB_DISCARD_YOUNGER_PRI:
2647 case ASB_DISCARD_OLDER_PRI:
2648 case ASB_DISCARD_LEAST_CHG:
2649 case ASB_DISCARD_LOCAL:
2650 case ASB_DISCARD_REMOTE:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002651 case ASB_DISCARD_ZERO_CHG:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002652 dev_err(DEV, "Configuration error.\n");
2653 break;
2654 case ASB_DISCONNECT:
2655 break;
2656 case ASB_CONSENSUS:
2657 hg = drbd_asb_recover_0p(mdev);
2658 if (hg == -1 && mdev->state.role == R_SECONDARY)
2659 rv = hg;
2660 if (hg == 1 && mdev->state.role == R_PRIMARY)
2661 rv = hg;
2662 break;
2663 case ASB_VIOLENTLY:
2664 rv = drbd_asb_recover_0p(mdev);
2665 break;
2666 case ASB_DISCARD_SECONDARY:
2667 return mdev->state.role == R_PRIMARY ? 1 : -1;
2668 case ASB_CALL_HELPER:
2669 hg = drbd_asb_recover_0p(mdev);
2670 if (hg == -1 && mdev->state.role == R_PRIMARY) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002671 enum drbd_state_rv rv2;
2672
2673 drbd_set_role(mdev, R_SECONDARY, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002674 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2675 * we might be here in C_WF_REPORT_PARAMS which is transient.
2676 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002677 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2678 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002679 drbd_khelper(mdev, "pri-lost-after-sb");
2680 } else {
2681 dev_warn(DEV, "Successfully gave up primary role.\n");
2682 rv = hg;
2683 }
2684 } else
2685 rv = hg;
2686 }
2687
2688 return rv;
2689}
2690
2691static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
2692{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002693 int hg, rv = -100;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002694 enum drbd_after_sb_p after_sb_2p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002695
Philipp Reisner44ed1672011-04-19 17:10:19 +02002696 rcu_read_lock();
2697 after_sb_2p = rcu_dereference(mdev->tconn->net_conf)->after_sb_2p;
2698 rcu_read_unlock();
2699 switch (after_sb_2p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002700 case ASB_DISCARD_YOUNGER_PRI:
2701 case ASB_DISCARD_OLDER_PRI:
2702 case ASB_DISCARD_LEAST_CHG:
2703 case ASB_DISCARD_LOCAL:
2704 case ASB_DISCARD_REMOTE:
2705 case ASB_CONSENSUS:
2706 case ASB_DISCARD_SECONDARY:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002707 case ASB_DISCARD_ZERO_CHG:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002708 dev_err(DEV, "Configuration error.\n");
2709 break;
2710 case ASB_VIOLENTLY:
2711 rv = drbd_asb_recover_0p(mdev);
2712 break;
2713 case ASB_DISCONNECT:
2714 break;
2715 case ASB_CALL_HELPER:
2716 hg = drbd_asb_recover_0p(mdev);
2717 if (hg == -1) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002718 enum drbd_state_rv rv2;
2719
Philipp Reisnerb411b362009-09-25 16:07:19 -07002720 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2721 * we might be here in C_WF_REPORT_PARAMS which is transient.
2722 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002723 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2724 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002725 drbd_khelper(mdev, "pri-lost-after-sb");
2726 } else {
2727 dev_warn(DEV, "Successfully gave up primary role.\n");
2728 rv = hg;
2729 }
2730 } else
2731 rv = hg;
2732 }
2733
2734 return rv;
2735}
2736
2737static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
2738 u64 bits, u64 flags)
2739{
2740 if (!uuid) {
2741 dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
2742 return;
2743 }
2744 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
2745 text,
2746 (unsigned long long)uuid[UI_CURRENT],
2747 (unsigned long long)uuid[UI_BITMAP],
2748 (unsigned long long)uuid[UI_HISTORY_START],
2749 (unsigned long long)uuid[UI_HISTORY_END],
2750 (unsigned long long)bits,
2751 (unsigned long long)flags);
2752}
2753
2754/*
2755 100 after split brain try auto recover
2756 2 C_SYNC_SOURCE set BitMap
2757 1 C_SYNC_SOURCE use BitMap
2758 0 no Sync
2759 -1 C_SYNC_TARGET use BitMap
2760 -2 C_SYNC_TARGET set BitMap
2761 -100 after split brain, disconnect
2762-1000 unrelated data
Philipp Reisner4a23f262011-01-11 17:42:17 +01002763-1091 requires proto 91
2764-1096 requires proto 96
Philipp Reisnerb411b362009-09-25 16:07:19 -07002765 */
2766static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
2767{
2768 u64 self, peer;
2769 int i, j;
2770
2771 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2772 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2773
2774 *rule_nr = 10;
2775 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
2776 return 0;
2777
2778 *rule_nr = 20;
2779 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
2780 peer != UUID_JUST_CREATED)
2781 return -2;
2782
2783 *rule_nr = 30;
2784 if (self != UUID_JUST_CREATED &&
2785 (peer == UUID_JUST_CREATED || peer == (u64)0))
2786 return 2;
2787
2788 if (self == peer) {
2789 int rct, dc; /* roles at crash time */
2790
2791 if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
2792
Philipp Reisner31890f42011-01-19 14:12:51 +01002793 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002794 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002795
2796 if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
2797 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
2798 dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
2799 drbd_uuid_set_bm(mdev, 0UL);
2800
2801 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2802 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2803 *rule_nr = 34;
2804 } else {
2805 dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
2806 *rule_nr = 36;
2807 }
2808
2809 return 1;
2810 }
2811
2812 if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
2813
Philipp Reisner31890f42011-01-19 14:12:51 +01002814 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002815 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002816
2817 if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
2818 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
2819 dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
2820
2821 mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
2822 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
2823 mdev->p_uuid[UI_BITMAP] = 0UL;
2824
2825 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2826 *rule_nr = 35;
2827 } else {
2828 dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
2829 *rule_nr = 37;
2830 }
2831
2832 return -1;
2833 }
2834
2835 /* Common power [off|failure] */
2836 rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
2837 (mdev->p_uuid[UI_FLAGS] & 2);
2838 /* lowest bit is set when we were primary,
2839 * next bit (weight 2) is set when peer was primary */
2840 *rule_nr = 40;
2841
2842 switch (rct) {
2843 case 0: /* !self_pri && !peer_pri */ return 0;
2844 case 1: /* self_pri && !peer_pri */ return 1;
2845 case 2: /* !self_pri && peer_pri */ return -1;
2846 case 3: /* self_pri && peer_pri */
Lars Ellenberg427c0432012-08-01 12:43:01 +02002847 dc = test_bit(RESOLVE_CONFLICTS, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002848 return dc ? -1 : 1;
2849 }
2850 }
2851
2852 *rule_nr = 50;
2853 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2854 if (self == peer)
2855 return -1;
2856
2857 *rule_nr = 51;
2858 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2859 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002860 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002861 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) ==
2862 (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) :
2863 peer + UUID_NEW_BM_OFFSET == (mdev->p_uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002864 /* The last P_SYNC_UUID did not get though. Undo the last start of
2865 resync as sync source modifications of the peer's UUIDs. */
2866
Philipp Reisner31890f42011-01-19 14:12:51 +01002867 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002868 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002869
2870 mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
2871 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
Philipp Reisner4a23f262011-01-11 17:42:17 +01002872
Lars Ellenberg1882e222012-05-07 13:09:00 +02002873 dev_info(DEV, "Lost last syncUUID packet, corrected:\n");
Philipp Reisner4a23f262011-01-11 17:42:17 +01002874 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2875
Philipp Reisnerb411b362009-09-25 16:07:19 -07002876 return -1;
2877 }
2878 }
2879
2880 *rule_nr = 60;
2881 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2882 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2883 peer = mdev->p_uuid[i] & ~((u64)1);
2884 if (self == peer)
2885 return -2;
2886 }
2887
2888 *rule_nr = 70;
2889 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2890 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2891 if (self == peer)
2892 return 1;
2893
2894 *rule_nr = 71;
2895 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2896 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002897 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002898 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) ==
2899 (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) :
2900 self + UUID_NEW_BM_OFFSET == (mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002901 /* The last P_SYNC_UUID did not get though. Undo the last start of
2902 resync as sync source modifications of our UUIDs. */
2903
Philipp Reisner31890f42011-01-19 14:12:51 +01002904 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002905 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002906
2907 _drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
2908 _drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
2909
Philipp Reisner4a23f262011-01-11 17:42:17 +01002910 dev_info(DEV, "Last syncUUID did not get through, corrected:\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002911 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2912 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2913
2914 return 1;
2915 }
2916 }
2917
2918
2919 *rule_nr = 80;
Philipp Reisnerd8c2a362009-11-18 15:52:51 +01002920 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002921 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2922 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2923 if (self == peer)
2924 return 2;
2925 }
2926
2927 *rule_nr = 90;
2928 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2929 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2930 if (self == peer && self != ((u64)0))
2931 return 100;
2932
2933 *rule_nr = 100;
2934 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2935 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2936 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
2937 peer = mdev->p_uuid[j] & ~((u64)1);
2938 if (self == peer)
2939 return -100;
2940 }
2941 }
2942
2943 return -1000;
2944}
2945
2946/* drbd_sync_handshake() returns the new conn state on success, or
2947 CONN_MASK (-1) on failure.
2948 */
2949static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
2950 enum drbd_disk_state peer_disk) __must_hold(local)
2951{
Philipp Reisnerb411b362009-09-25 16:07:19 -07002952 enum drbd_conns rv = C_MASK;
2953 enum drbd_disk_state mydisk;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002954 struct net_conf *nc;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02002955 int hg, rule_nr, rr_conflict, tentative;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002956
2957 mydisk = mdev->state.disk;
2958 if (mydisk == D_NEGOTIATING)
2959 mydisk = mdev->new_state_tmp.disk;
2960
2961 dev_info(DEV, "drbd_sync_handshake:\n");
2962 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0);
2963 drbd_uuid_dump(mdev, "peer", mdev->p_uuid,
2964 mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2965
2966 hg = drbd_uuid_compare(mdev, &rule_nr);
2967
2968 dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
2969
2970 if (hg == -1000) {
2971 dev_alert(DEV, "Unrelated data, aborting!\n");
2972 return C_MASK;
2973 }
Philipp Reisner4a23f262011-01-11 17:42:17 +01002974 if (hg < -1000) {
2975 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 -07002976 return C_MASK;
2977 }
2978
2979 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
2980 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
2981 int f = (hg == -100) || abs(hg) == 2;
2982 hg = mydisk > D_INCONSISTENT ? 1 : -1;
2983 if (f)
2984 hg = hg*2;
2985 dev_info(DEV, "Becoming sync %s due to disk states.\n",
2986 hg > 0 ? "source" : "target");
2987 }
2988
Adam Gandelman3a11a482010-04-08 16:48:23 -07002989 if (abs(hg) == 100)
2990 drbd_khelper(mdev, "initial-split-brain");
2991
Philipp Reisner44ed1672011-04-19 17:10:19 +02002992 rcu_read_lock();
2993 nc = rcu_dereference(mdev->tconn->net_conf);
2994
2995 if (hg == 100 || (hg == -100 && nc->always_asbp)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002996 int pcount = (mdev->state.role == R_PRIMARY)
2997 + (peer_role == R_PRIMARY);
2998 int forced = (hg == -100);
2999
3000 switch (pcount) {
3001 case 0:
3002 hg = drbd_asb_recover_0p(mdev);
3003 break;
3004 case 1:
3005 hg = drbd_asb_recover_1p(mdev);
3006 break;
3007 case 2:
3008 hg = drbd_asb_recover_2p(mdev);
3009 break;
3010 }
3011 if (abs(hg) < 100) {
3012 dev_warn(DEV, "Split-Brain detected, %d primaries, "
3013 "automatically solved. Sync from %s node\n",
3014 pcount, (hg < 0) ? "peer" : "this");
3015 if (forced) {
3016 dev_warn(DEV, "Doing a full sync, since"
3017 " UUIDs where ambiguous.\n");
3018 hg = hg*2;
3019 }
3020 }
3021 }
3022
3023 if (hg == -100) {
Philipp Reisner08b165b2011-09-05 16:22:33 +02003024 if (test_bit(DISCARD_MY_DATA, &mdev->flags) && !(mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003025 hg = -1;
Philipp Reisner08b165b2011-09-05 16:22:33 +02003026 if (!test_bit(DISCARD_MY_DATA, &mdev->flags) && (mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003027 hg = 1;
3028
3029 if (abs(hg) < 100)
3030 dev_warn(DEV, "Split-Brain detected, manually solved. "
3031 "Sync from %s node\n",
3032 (hg < 0) ? "peer" : "this");
3033 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02003034 rr_conflict = nc->rr_conflict;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02003035 tentative = nc->tentative;
Philipp Reisner44ed1672011-04-19 17:10:19 +02003036 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07003037
3038 if (hg == -100) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01003039 /* FIXME this log message is not correct if we end up here
3040 * after an attempted attach on a diskless node.
3041 * We just refuse to attach -- well, we drop the "connection"
3042 * to that disk, in a way... */
Adam Gandelman3a11a482010-04-08 16:48:23 -07003043 dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003044 drbd_khelper(mdev, "split-brain");
3045 return C_MASK;
3046 }
3047
3048 if (hg > 0 && mydisk <= D_INCONSISTENT) {
3049 dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
3050 return C_MASK;
3051 }
3052
3053 if (hg < 0 && /* by intention we do not use mydisk here. */
3054 mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02003055 switch (rr_conflict) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003056 case ASB_CALL_HELPER:
3057 drbd_khelper(mdev, "pri-lost");
3058 /* fall through */
3059 case ASB_DISCONNECT:
3060 dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
3061 return C_MASK;
3062 case ASB_VIOLENTLY:
3063 dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
3064 "assumption\n");
3065 }
3066 }
3067
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02003068 if (tentative || test_bit(CONN_DRY_RUN, &mdev->tconn->flags)) {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003069 if (hg == 0)
3070 dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
3071 else
3072 dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
3073 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
3074 abs(hg) >= 2 ? "full" : "bit-map based");
3075 return C_MASK;
3076 }
3077
Philipp Reisnerb411b362009-09-25 16:07:19 -07003078 if (abs(hg) >= 2) {
3079 dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003080 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake",
3081 BM_LOCKED_SET_ALLOWED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003082 return C_MASK;
3083 }
3084
3085 if (hg > 0) { /* become sync source. */
3086 rv = C_WF_BITMAP_S;
3087 } else if (hg < 0) { /* become sync target */
3088 rv = C_WF_BITMAP_T;
3089 } else {
3090 rv = C_CONNECTED;
3091 if (drbd_bm_total_weight(mdev)) {
3092 dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
3093 drbd_bm_total_weight(mdev));
3094 }
3095 }
3096
3097 return rv;
3098}
3099
Philipp Reisnerf179d762011-05-16 17:31:47 +02003100static enum drbd_after_sb_p convert_after_sb(enum drbd_after_sb_p peer)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003101{
3102 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003103 if (peer == ASB_DISCARD_REMOTE)
3104 return ASB_DISCARD_LOCAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003105
3106 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003107 if (peer == ASB_DISCARD_LOCAL)
3108 return ASB_DISCARD_REMOTE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003109
3110 /* everything else is valid if they are equal on both sides. */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003111 return peer;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003112}
3113
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003114static int receive_protocol(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003115{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003116 struct p_protocol *p = pi->data;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003117 enum drbd_after_sb_p p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
3118 int p_proto, p_discard_my_data, p_two_primaries, cf;
3119 struct net_conf *nc, *old_net_conf, *new_net_conf = NULL;
3120 char integrity_alg[SHARED_SECRET_MAX] = "";
Andreas Gruenbacheraccdbcc2011-07-15 17:41:09 +02003121 struct crypto_hash *peer_integrity_tfm = NULL;
Philipp Reisner7aca6c72011-05-17 10:12:56 +02003122 void *int_dig_in = NULL, *int_dig_vv = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003123
Philipp Reisnerb411b362009-09-25 16:07:19 -07003124 p_proto = be32_to_cpu(p->protocol);
3125 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
3126 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
3127 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003128 p_two_primaries = be32_to_cpu(p->two_primaries);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003129 cf = be32_to_cpu(p->conn_flags);
Andreas Gruenbacher6139f602011-05-06 20:00:02 +02003130 p_discard_my_data = cf & CF_DISCARD_MY_DATA;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003131
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003132 if (tconn->agreed_pro_version >= 87) {
3133 int err;
3134
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003135 if (pi->size > sizeof(integrity_alg))
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003136 return -EIO;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003137 err = drbd_recv_all(tconn, integrity_alg, pi->size);
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003138 if (err)
3139 return err;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003140 integrity_alg[SHARED_SECRET_MAX - 1] = 0;
3141 }
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003142
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003143 if (pi->cmd != P_PROTOCOL_UPDATE) {
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003144 clear_bit(CONN_DRY_RUN, &tconn->flags);
Philipp Reisner036b17e2011-05-16 17:38:11 +02003145
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003146 if (cf & CF_DRY_RUN)
3147 set_bit(CONN_DRY_RUN, &tconn->flags);
3148
3149 rcu_read_lock();
3150 nc = rcu_dereference(tconn->net_conf);
3151
3152 if (p_proto != nc->wire_protocol) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003153 conn_err(tconn, "incompatible %s settings\n", "protocol");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003154 goto disconnect_rcu_unlock;
3155 }
3156
3157 if (convert_after_sb(p_after_sb_0p) != nc->after_sb_0p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003158 conn_err(tconn, "incompatible %s settings\n", "after-sb-0pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003159 goto disconnect_rcu_unlock;
3160 }
3161
3162 if (convert_after_sb(p_after_sb_1p) != nc->after_sb_1p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003163 conn_err(tconn, "incompatible %s settings\n", "after-sb-1pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003164 goto disconnect_rcu_unlock;
3165 }
3166
3167 if (convert_after_sb(p_after_sb_2p) != nc->after_sb_2p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003168 conn_err(tconn, "incompatible %s settings\n", "after-sb-2pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003169 goto disconnect_rcu_unlock;
3170 }
3171
3172 if (p_discard_my_data && nc->discard_my_data) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003173 conn_err(tconn, "incompatible %s settings\n", "discard-my-data");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003174 goto disconnect_rcu_unlock;
3175 }
3176
3177 if (p_two_primaries != nc->two_primaries) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003178 conn_err(tconn, "incompatible %s settings\n", "allow-two-primaries");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003179 goto disconnect_rcu_unlock;
3180 }
3181
3182 if (strcmp(integrity_alg, nc->integrity_alg)) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003183 conn_err(tconn, "incompatible %s settings\n", "data-integrity-alg");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003184 goto disconnect_rcu_unlock;
3185 }
3186
3187 rcu_read_unlock();
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003188 }
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003189
3190 if (integrity_alg[0]) {
3191 int hash_size;
3192
3193 /*
3194 * We can only change the peer data integrity algorithm
3195 * here. Changing our own data integrity algorithm
3196 * requires that we send a P_PROTOCOL_UPDATE packet at
3197 * the same time; otherwise, the peer has no way to
3198 * tell between which packets the algorithm should
3199 * change.
3200 */
3201
3202 peer_integrity_tfm = crypto_alloc_hash(integrity_alg, 0, CRYPTO_ALG_ASYNC);
3203 if (!peer_integrity_tfm) {
3204 conn_err(tconn, "peer data-integrity-alg %s not supported\n",
3205 integrity_alg);
3206 goto disconnect;
3207 }
3208
3209 hash_size = crypto_hash_digestsize(peer_integrity_tfm);
3210 int_dig_in = kmalloc(hash_size, GFP_KERNEL);
3211 int_dig_vv = kmalloc(hash_size, GFP_KERNEL);
3212 if (!(int_dig_in && int_dig_vv)) {
3213 conn_err(tconn, "Allocation of buffers for data integrity checking failed\n");
3214 goto disconnect;
3215 }
3216 }
3217
3218 new_net_conf = kmalloc(sizeof(struct net_conf), GFP_KERNEL);
3219 if (!new_net_conf) {
3220 conn_err(tconn, "Allocation of new net_conf failed\n");
3221 goto disconnect;
3222 }
3223
3224 mutex_lock(&tconn->data.mutex);
3225 mutex_lock(&tconn->conf_update);
3226 old_net_conf = tconn->net_conf;
3227 *new_net_conf = *old_net_conf;
3228
3229 new_net_conf->wire_protocol = p_proto;
3230 new_net_conf->after_sb_0p = convert_after_sb(p_after_sb_0p);
3231 new_net_conf->after_sb_1p = convert_after_sb(p_after_sb_1p);
3232 new_net_conf->after_sb_2p = convert_after_sb(p_after_sb_2p);
3233 new_net_conf->two_primaries = p_two_primaries;
3234
3235 rcu_assign_pointer(tconn->net_conf, new_net_conf);
3236 mutex_unlock(&tconn->conf_update);
3237 mutex_unlock(&tconn->data.mutex);
3238
3239 crypto_free_hash(tconn->peer_integrity_tfm);
3240 kfree(tconn->int_dig_in);
3241 kfree(tconn->int_dig_vv);
3242 tconn->peer_integrity_tfm = peer_integrity_tfm;
3243 tconn->int_dig_in = int_dig_in;
3244 tconn->int_dig_vv = int_dig_vv;
3245
3246 if (strcmp(old_net_conf->integrity_alg, integrity_alg))
3247 conn_info(tconn, "peer data-integrity-alg: %s\n",
3248 integrity_alg[0] ? integrity_alg : "(none)");
3249
3250 synchronize_rcu();
3251 kfree(old_net_conf);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003252 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003253
Philipp Reisner44ed1672011-04-19 17:10:19 +02003254disconnect_rcu_unlock:
3255 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07003256disconnect:
Andreas Gruenbacherb792c352011-07-15 16:48:49 +02003257 crypto_free_hash(peer_integrity_tfm);
Philipp Reisner036b17e2011-05-16 17:38:11 +02003258 kfree(int_dig_in);
3259 kfree(int_dig_vv);
Philipp Reisner72046242011-03-15 18:51:47 +01003260 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003261 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003262}
3263
3264/* helper function
3265 * input: alg name, feature name
3266 * return: NULL (alg name was "")
3267 * ERR_PTR(error) if something goes wrong
3268 * or the crypto hash ptr, if it worked out ok. */
3269struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
3270 const char *alg, const char *name)
3271{
3272 struct crypto_hash *tfm;
3273
3274 if (!alg[0])
3275 return NULL;
3276
3277 tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
3278 if (IS_ERR(tfm)) {
3279 dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
3280 alg, name, PTR_ERR(tfm));
3281 return tfm;
3282 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003283 return tfm;
3284}
3285
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003286static int ignore_remaining_packet(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003287{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003288 void *buffer = tconn->data.rbuf;
3289 int size = pi->size;
3290
3291 while (size) {
3292 int s = min_t(int, size, DRBD_SOCKET_BUFFER_SIZE);
3293 s = drbd_recv(tconn, buffer, s);
3294 if (s <= 0) {
3295 if (s < 0)
3296 return s;
3297 break;
3298 }
3299 size -= s;
3300 }
3301 if (size)
3302 return -EIO;
3303 return 0;
3304}
3305
3306/*
3307 * config_unknown_volume - device configuration command for unknown volume
3308 *
3309 * When a device is added to an existing connection, the node on which the
3310 * device is added first will send configuration commands to its peer but the
3311 * peer will not know about the device yet. It will warn and ignore these
3312 * commands. Once the device is added on the second node, the second node will
3313 * send the same device configuration commands, but in the other direction.
3314 *
3315 * (We can also end up here if drbd is misconfigured.)
3316 */
3317static int config_unknown_volume(struct drbd_tconn *tconn, struct packet_info *pi)
3318{
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02003319 conn_warn(tconn, "%s packet received for volume %u, which is not configured locally\n",
3320 cmdname(pi->cmd), pi->vnr);
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003321 return ignore_remaining_packet(tconn, pi);
3322}
3323
3324static int receive_SyncParam(struct drbd_tconn *tconn, struct packet_info *pi)
3325{
3326 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003327 struct p_rs_param_95 *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003328 unsigned int header_size, data_size, exp_max_sz;
3329 struct crypto_hash *verify_tfm = NULL;
3330 struct crypto_hash *csums_tfm = NULL;
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003331 struct net_conf *old_net_conf, *new_net_conf = NULL;
Philipp Reisner813472c2011-05-03 16:47:02 +02003332 struct disk_conf *old_disk_conf = NULL, *new_disk_conf = NULL;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003333 const int apv = tconn->agreed_pro_version;
Philipp Reisner813472c2011-05-03 16:47:02 +02003334 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
Philipp Reisner778f2712010-07-06 11:14:00 +02003335 int fifo_size = 0;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003336 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003337
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003338 mdev = vnr_to_mdev(tconn, pi->vnr);
3339 if (!mdev)
3340 return config_unknown_volume(tconn, pi);
3341
Philipp Reisnerb411b362009-09-25 16:07:19 -07003342 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
3343 : apv == 88 ? sizeof(struct p_rs_param)
3344 + SHARED_SECRET_MAX
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003345 : apv <= 94 ? sizeof(struct p_rs_param_89)
3346 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003347
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003348 if (pi->size > exp_max_sz) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003349 dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003350 pi->size, exp_max_sz);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003351 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003352 }
3353
3354 if (apv <= 88) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003355 header_size = sizeof(struct p_rs_param);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003356 data_size = pi->size - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003357 } else if (apv <= 94) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003358 header_size = sizeof(struct p_rs_param_89);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003359 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003360 D_ASSERT(data_size == 0);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003361 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003362 header_size = sizeof(struct p_rs_param_95);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003363 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003364 D_ASSERT(data_size == 0);
3365 }
3366
3367 /* initialize verify_alg and csums_alg */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003368 p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003369 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
3370
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003371 err = drbd_recv_all(mdev->tconn, p, header_size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003372 if (err)
3373 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003374
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003375 mutex_lock(&mdev->tconn->conf_update);
3376 old_net_conf = mdev->tconn->net_conf;
Philipp Reisner813472c2011-05-03 16:47:02 +02003377 if (get_ldev(mdev)) {
3378 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3379 if (!new_disk_conf) {
3380 put_ldev(mdev);
3381 mutex_unlock(&mdev->tconn->conf_update);
3382 dev_err(DEV, "Allocation of new disk_conf failed\n");
3383 return -ENOMEM;
3384 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003385
Philipp Reisner813472c2011-05-03 16:47:02 +02003386 old_disk_conf = mdev->ldev->disk_conf;
3387 *new_disk_conf = *old_disk_conf;
3388
Andreas Gruenbacher6394b932011-05-11 14:29:52 +02003389 new_disk_conf->resync_rate = be32_to_cpu(p->resync_rate);
Philipp Reisner813472c2011-05-03 16:47:02 +02003390 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003391
Philipp Reisnerb411b362009-09-25 16:07:19 -07003392 if (apv >= 88) {
3393 if (apv == 88) {
Philipp Reisnere4bad1b2012-04-06 12:08:51 +02003394 if (data_size > SHARED_SECRET_MAX || data_size == 0) {
3395 dev_err(DEV, "verify-alg of wrong size, "
3396 "peer wants %u, accepting only up to %u byte\n",
3397 data_size, SHARED_SECRET_MAX);
Philipp Reisner813472c2011-05-03 16:47:02 +02003398 err = -EIO;
3399 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003400 }
3401
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003402 err = drbd_recv_all(mdev->tconn, p->verify_alg, data_size);
Philipp Reisner813472c2011-05-03 16:47:02 +02003403 if (err)
3404 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003405 /* we expect NUL terminated string */
3406 /* but just in case someone tries to be evil */
3407 D_ASSERT(p->verify_alg[data_size-1] == 0);
3408 p->verify_alg[data_size-1] = 0;
3409
3410 } else /* apv >= 89 */ {
3411 /* we still expect NUL terminated strings */
3412 /* but just in case someone tries to be evil */
3413 D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
3414 D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
3415 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
3416 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
3417 }
3418
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003419 if (strcmp(old_net_conf->verify_alg, p->verify_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003420 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3421 dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003422 old_net_conf->verify_alg, p->verify_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003423 goto disconnect;
3424 }
3425 verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
3426 p->verify_alg, "verify-alg");
3427 if (IS_ERR(verify_tfm)) {
3428 verify_tfm = NULL;
3429 goto disconnect;
3430 }
3431 }
3432
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003433 if (apv >= 89 && strcmp(old_net_conf->csums_alg, p->csums_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003434 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3435 dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003436 old_net_conf->csums_alg, p->csums_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003437 goto disconnect;
3438 }
3439 csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
3440 p->csums_alg, "csums-alg");
3441 if (IS_ERR(csums_tfm)) {
3442 csums_tfm = NULL;
3443 goto disconnect;
3444 }
3445 }
3446
Philipp Reisner813472c2011-05-03 16:47:02 +02003447 if (apv > 94 && new_disk_conf) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003448 new_disk_conf->c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
3449 new_disk_conf->c_delay_target = be32_to_cpu(p->c_delay_target);
3450 new_disk_conf->c_fill_target = be32_to_cpu(p->c_fill_target);
3451 new_disk_conf->c_max_rate = be32_to_cpu(p->c_max_rate);
Philipp Reisner778f2712010-07-06 11:14:00 +02003452
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003453 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Philipp Reisner9958c852011-05-03 16:19:31 +02003454 if (fifo_size != mdev->rs_plan_s->size) {
Philipp Reisner813472c2011-05-03 16:47:02 +02003455 new_plan = fifo_alloc(fifo_size);
3456 if (!new_plan) {
Philipp Reisner778f2712010-07-06 11:14:00 +02003457 dev_err(DEV, "kmalloc of fifo_buffer failed");
Lars Ellenbergf3990022011-03-23 14:31:09 +01003458 put_ldev(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02003459 goto disconnect;
3460 }
3461 }
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003462 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003463
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003464 if (verify_tfm || csums_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003465 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
3466 if (!new_net_conf) {
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003467 dev_err(DEV, "Allocation of new net_conf failed\n");
3468 goto disconnect;
3469 }
3470
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003471 *new_net_conf = *old_net_conf;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003472
3473 if (verify_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003474 strcpy(new_net_conf->verify_alg, p->verify_alg);
3475 new_net_conf->verify_alg_len = strlen(p->verify_alg) + 1;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003476 crypto_free_hash(mdev->tconn->verify_tfm);
3477 mdev->tconn->verify_tfm = verify_tfm;
3478 dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
3479 }
3480 if (csums_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003481 strcpy(new_net_conf->csums_alg, p->csums_alg);
3482 new_net_conf->csums_alg_len = strlen(p->csums_alg) + 1;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003483 crypto_free_hash(mdev->tconn->csums_tfm);
3484 mdev->tconn->csums_tfm = csums_tfm;
3485 dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
3486 }
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003487 rcu_assign_pointer(tconn->net_conf, new_net_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003488 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003489 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003490
Philipp Reisner813472c2011-05-03 16:47:02 +02003491 if (new_disk_conf) {
3492 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3493 put_ldev(mdev);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003494 }
Philipp Reisner813472c2011-05-03 16:47:02 +02003495
3496 if (new_plan) {
3497 old_plan = mdev->rs_plan_s;
3498 rcu_assign_pointer(mdev->rs_plan_s, new_plan);
3499 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003500
3501 mutex_unlock(&mdev->tconn->conf_update);
3502 synchronize_rcu();
3503 if (new_net_conf)
3504 kfree(old_net_conf);
3505 kfree(old_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02003506 kfree(old_plan);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003507
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003508 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003509
Philipp Reisner813472c2011-05-03 16:47:02 +02003510reconnect:
3511 if (new_disk_conf) {
3512 put_ldev(mdev);
3513 kfree(new_disk_conf);
3514 }
3515 mutex_unlock(&mdev->tconn->conf_update);
3516 return -EIO;
3517
Philipp Reisnerb411b362009-09-25 16:07:19 -07003518disconnect:
Philipp Reisner813472c2011-05-03 16:47:02 +02003519 kfree(new_plan);
3520 if (new_disk_conf) {
3521 put_ldev(mdev);
3522 kfree(new_disk_conf);
3523 }
Philipp Reisnera0095502011-05-03 13:14:15 +02003524 mutex_unlock(&mdev->tconn->conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003525 /* just for completeness: actually not needed,
3526 * as this is not reached if csums_tfm was ok. */
3527 crypto_free_hash(csums_tfm);
3528 /* but free the verify_tfm again, if csums_tfm did not work out */
3529 crypto_free_hash(verify_tfm);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003530 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003531 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003532}
3533
Philipp Reisnerb411b362009-09-25 16:07:19 -07003534/* warn if the arguments differ by more than 12.5% */
3535static void warn_if_differ_considerably(struct drbd_conf *mdev,
3536 const char *s, sector_t a, sector_t b)
3537{
3538 sector_t d;
3539 if (a == 0 || b == 0)
3540 return;
3541 d = (a > b) ? (a - b) : (b - a);
3542 if (d > (a>>3) || d > (b>>3))
3543 dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
3544 (unsigned long long)a, (unsigned long long)b);
3545}
3546
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003547static int receive_sizes(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003548{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003549 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003550 struct p_sizes *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003551 enum determine_dev_size dd = unchanged;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003552 sector_t p_size, p_usize, my_usize;
3553 int ldsc = 0; /* local disk size changed */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003554 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003555
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003556 mdev = vnr_to_mdev(tconn, pi->vnr);
3557 if (!mdev)
3558 return config_unknown_volume(tconn, pi);
3559
Philipp Reisnerb411b362009-09-25 16:07:19 -07003560 p_size = be64_to_cpu(p->d_size);
3561 p_usize = be64_to_cpu(p->u_size);
3562
Philipp Reisnerb411b362009-09-25 16:07:19 -07003563 /* just store the peer's disk size for now.
3564 * we still need to figure out whether we accept that. */
3565 mdev->p_size = p_size;
3566
Philipp Reisnerb411b362009-09-25 16:07:19 -07003567 if (get_ldev(mdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003568 rcu_read_lock();
3569 my_usize = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
3570 rcu_read_unlock();
3571
Philipp Reisnerb411b362009-09-25 16:07:19 -07003572 warn_if_differ_considerably(mdev, "lower level device sizes",
3573 p_size, drbd_get_max_capacity(mdev->ldev));
3574 warn_if_differ_considerably(mdev, "user requested size",
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003575 p_usize, my_usize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003576
3577 /* if this is the first connect, or an otherwise expected
3578 * param exchange, choose the minimum */
3579 if (mdev->state.conn == C_WF_REPORT_PARAMS)
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003580 p_usize = min_not_zero(my_usize, p_usize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003581
3582 /* Never shrink a device with usable data during connect.
3583 But allow online shrinking if we are connected. */
Philipp Reisneref5e44a2011-05-03 13:27:43 +02003584 if (drbd_new_dev_size(mdev, mdev->ldev, p_usize, 0) <
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003585 drbd_get_capacity(mdev->this_bdev) &&
3586 mdev->state.disk >= D_OUTDATED &&
3587 mdev->state.conn < C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003588 dev_err(DEV, "The peer's disk size is too small!\n");
Philipp Reisner38fa9982011-03-15 18:24:49 +01003589 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003590 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003591 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003592 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003593
3594 if (my_usize != p_usize) {
3595 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
3596
3597 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3598 if (!new_disk_conf) {
3599 dev_err(DEV, "Allocation of new disk_conf failed\n");
3600 put_ldev(mdev);
3601 return -ENOMEM;
3602 }
3603
3604 mutex_lock(&mdev->tconn->conf_update);
3605 old_disk_conf = mdev->ldev->disk_conf;
3606 *new_disk_conf = *old_disk_conf;
3607 new_disk_conf->disk_size = p_usize;
3608
3609 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3610 mutex_unlock(&mdev->tconn->conf_update);
3611 synchronize_rcu();
3612 kfree(old_disk_conf);
3613
3614 dev_info(DEV, "Peer sets u_size to %lu sectors\n",
3615 (unsigned long)my_usize);
3616 }
3617
Philipp Reisnerb411b362009-09-25 16:07:19 -07003618 put_ldev(mdev);
3619 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003620
Philipp Reisnere89b5912010-03-24 17:11:33 +01003621 ddsf = be16_to_cpu(p->dds_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003622 if (get_ldev(mdev)) {
Bart Van Assche24c48302011-05-21 18:32:29 +02003623 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003624 put_ldev(mdev);
3625 if (dd == dev_size_error)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003626 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003627 drbd_md_sync(mdev);
3628 } else {
3629 /* I am diskless, need to accept the peer's size. */
3630 drbd_set_my_capacity(mdev, p_size);
3631 }
3632
Philipp Reisner99432fc2011-05-20 16:39:13 +02003633 mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size);
3634 drbd_reconsider_max_bio_size(mdev);
3635
Philipp Reisnerb411b362009-09-25 16:07:19 -07003636 if (get_ldev(mdev)) {
3637 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
3638 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
3639 ldsc = 1;
3640 }
3641
Philipp Reisnerb411b362009-09-25 16:07:19 -07003642 put_ldev(mdev);
3643 }
3644
3645 if (mdev->state.conn > C_WF_REPORT_PARAMS) {
3646 if (be64_to_cpu(p->c_size) !=
3647 drbd_get_capacity(mdev->this_bdev) || ldsc) {
3648 /* we have different sizes, probably peer
3649 * needs to know my new size... */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003650 drbd_send_sizes(mdev, 0, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003651 }
3652 if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
3653 (dd == grew && mdev->state.conn == C_CONNECTED)) {
3654 if (mdev->state.pdsk >= D_INCONSISTENT &&
Philipp Reisnere89b5912010-03-24 17:11:33 +01003655 mdev->state.disk >= D_INCONSISTENT) {
3656 if (ddsf & DDSF_NO_RESYNC)
3657 dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
3658 else
3659 resync_after_online_grow(mdev);
3660 } else
Philipp Reisnerb411b362009-09-25 16:07:19 -07003661 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
3662 }
3663 }
3664
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003665 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003666}
3667
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003668static int receive_uuids(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003669{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003670 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003671 struct p_uuids *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003672 u64 *p_uuid;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003673 int i, updated_uuids = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003674
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003675 mdev = vnr_to_mdev(tconn, pi->vnr);
3676 if (!mdev)
3677 return config_unknown_volume(tconn, pi);
3678
Philipp Reisnerb411b362009-09-25 16:07:19 -07003679 p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
3680
3681 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
3682 p_uuid[i] = be64_to_cpu(p->uuid[i]);
3683
3684 kfree(mdev->p_uuid);
3685 mdev->p_uuid = p_uuid;
3686
3687 if (mdev->state.conn < C_CONNECTED &&
3688 mdev->state.disk < D_INCONSISTENT &&
3689 mdev->state.role == R_PRIMARY &&
3690 (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
3691 dev_err(DEV, "Can only connect to data with current UUID=%016llX\n",
3692 (unsigned long long)mdev->ed_uuid);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003693 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003694 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003695 }
3696
3697 if (get_ldev(mdev)) {
3698 int skip_initial_sync =
3699 mdev->state.conn == C_CONNECTED &&
Philipp Reisner31890f42011-01-19 14:12:51 +01003700 mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003701 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
3702 (p_uuid[UI_FLAGS] & 8);
3703 if (skip_initial_sync) {
3704 dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n");
3705 drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003706 "clear_n_write from receive_uuids",
3707 BM_LOCKED_TEST_ALLOWED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003708 _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]);
3709 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3710 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3711 CS_VERBOSE, NULL);
3712 drbd_md_sync(mdev);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003713 updated_uuids = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003714 }
3715 put_ldev(mdev);
Philipp Reisner18a50fa2010-06-21 14:14:15 +02003716 } else if (mdev->state.disk < D_INCONSISTENT &&
3717 mdev->state.role == R_PRIMARY) {
3718 /* I am a diskless primary, the peer just created a new current UUID
3719 for me. */
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003720 updated_uuids = drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003721 }
3722
3723 /* Before we test for the disk state, we should wait until an eventually
3724 ongoing cluster wide state change is finished. That is important if
3725 we are primary and are detaching from our disk. We need to see the
3726 new disk state... */
Philipp Reisner8410da82011-02-11 20:11:10 +01003727 mutex_lock(mdev->state_mutex);
3728 mutex_unlock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003729 if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT)
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003730 updated_uuids |= drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
3731
3732 if (updated_uuids)
3733 drbd_print_uuids(mdev, "receiver updated UUIDs to");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003734
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003735 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003736}
3737
3738/**
3739 * convert_state() - Converts the peer's view of the cluster state to our point of view
3740 * @ps: The state as seen by the peer.
3741 */
3742static union drbd_state convert_state(union drbd_state ps)
3743{
3744 union drbd_state ms;
3745
3746 static enum drbd_conns c_tab[] = {
Philipp Reisner369bea62011-07-06 23:04:44 +02003747 [C_WF_REPORT_PARAMS] = C_WF_REPORT_PARAMS,
Philipp Reisnerb411b362009-09-25 16:07:19 -07003748 [C_CONNECTED] = C_CONNECTED,
3749
3750 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
3751 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
3752 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
3753 [C_VERIFY_S] = C_VERIFY_T,
3754 [C_MASK] = C_MASK,
3755 };
3756
3757 ms.i = ps.i;
3758
3759 ms.conn = c_tab[ps.conn];
3760 ms.peer = ps.role;
3761 ms.role = ps.peer;
3762 ms.pdsk = ps.disk;
3763 ms.disk = ps.pdsk;
3764 ms.peer_isp = (ps.aftr_isp | ps.user_isp);
3765
3766 return ms;
3767}
3768
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003769static int receive_req_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003770{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003771 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003772 struct p_req_state *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003773 union drbd_state mask, val;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01003774 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003775
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003776 mdev = vnr_to_mdev(tconn, pi->vnr);
3777 if (!mdev)
3778 return -EIO;
3779
Philipp Reisnerb411b362009-09-25 16:07:19 -07003780 mask.i = be32_to_cpu(p->mask);
3781 val.i = be32_to_cpu(p->val);
3782
Lars Ellenberg427c0432012-08-01 12:43:01 +02003783 if (test_bit(RESOLVE_CONFLICTS, &mdev->tconn->flags) &&
Philipp Reisner8410da82011-02-11 20:11:10 +01003784 mutex_is_locked(mdev->state_mutex)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003785 drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003786 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003787 }
3788
3789 mask = convert_state(mask);
3790 val = convert_state(val);
3791
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003792 rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
3793 drbd_send_sr_reply(mdev, rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003794
Philipp Reisnerb411b362009-09-25 16:07:19 -07003795 drbd_md_sync(mdev);
3796
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003797 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003798}
3799
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003800static int receive_req_conn_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003801{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003802 struct p_req_state *p = pi->data;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003803 union drbd_state mask, val;
3804 enum drbd_state_rv rv;
3805
3806 mask.i = be32_to_cpu(p->mask);
3807 val.i = be32_to_cpu(p->val);
3808
Lars Ellenberg427c0432012-08-01 12:43:01 +02003809 if (test_bit(RESOLVE_CONFLICTS, &tconn->flags) &&
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003810 mutex_is_locked(&tconn->cstate_mutex)) {
3811 conn_send_sr_reply(tconn, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003812 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003813 }
3814
3815 mask = convert_state(mask);
3816 val = convert_state(val);
3817
Philipp Reisner778bcf22011-03-28 12:55:03 +02003818 rv = conn_request_state(tconn, mask, val, CS_VERBOSE | CS_LOCAL_ONLY | CS_IGN_OUTD_FAIL);
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003819 conn_send_sr_reply(tconn, rv);
3820
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003821 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003822}
3823
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003824static int receive_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003825{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003826 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003827 struct p_state *p = pi->data;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003828 union drbd_state os, ns, peer_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003829 enum drbd_disk_state real_peer_disk;
Philipp Reisner65d922c2010-06-16 16:18:09 +02003830 enum chg_state_flags cs_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003831 int rv;
3832
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003833 mdev = vnr_to_mdev(tconn, pi->vnr);
3834 if (!mdev)
3835 return config_unknown_volume(tconn, pi);
3836
Philipp Reisnerb411b362009-09-25 16:07:19 -07003837 peer_state.i = be32_to_cpu(p->state);
3838
3839 real_peer_disk = peer_state.disk;
3840 if (peer_state.disk == D_NEGOTIATING) {
3841 real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
3842 dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
3843 }
3844
Philipp Reisner87eeee42011-01-19 14:16:30 +01003845 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003846 retry:
Philipp Reisner78bae592011-03-28 15:40:12 +02003847 os = ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003848 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003849
Philipp Reisnerb8853db2011-12-13 11:09:16 +01003850 /* If some other part of the code (asender thread, timeout)
3851 * already decided to close the connection again,
3852 * we must not "re-establish" it here. */
3853 if (os.conn <= C_TEAR_DOWN)
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003854 return -ECONNRESET;
Philipp Reisnerb8853db2011-12-13 11:09:16 +01003855
Philipp Reisner9bcd2522011-09-29 13:00:14 +02003856 /* If this is the "end of sync" confirmation, usually the peer disk
3857 * transitions from D_INCONSISTENT to D_UP_TO_DATE. For empty (0 bits
3858 * set) resync started in PausedSyncT, or if the timing of pause-/
3859 * unpause-sync events has been "just right", the peer disk may
3860 * transition from D_CONSISTENT to D_UP_TO_DATE as well.
3861 */
3862 if ((os.pdsk == D_INCONSISTENT || os.pdsk == D_CONSISTENT) &&
3863 real_peer_disk == D_UP_TO_DATE &&
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003864 os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) {
3865 /* If we are (becoming) SyncSource, but peer is still in sync
3866 * preparation, ignore its uptodate-ness to avoid flapping, it
3867 * will change to inconsistent once the peer reaches active
3868 * syncing states.
3869 * It may have changed syncer-paused flags, however, so we
3870 * cannot ignore this completely. */
3871 if (peer_state.conn > C_CONNECTED &&
3872 peer_state.conn < C_SYNC_SOURCE)
3873 real_peer_disk = D_INCONSISTENT;
3874
3875 /* if peer_state changes to connected at the same time,
3876 * it explicitly notifies us that it finished resync.
3877 * Maybe we should finish it up, too? */
3878 else if (os.conn >= C_SYNC_SOURCE &&
3879 peer_state.conn == C_CONNECTED) {
3880 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed)
3881 drbd_resync_finished(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003882 return 0;
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003883 }
3884 }
3885
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003886 /* explicit verify finished notification, stop sector reached. */
3887 if (os.conn == C_VERIFY_T && os.disk == D_UP_TO_DATE &&
3888 peer_state.conn == C_CONNECTED && real_peer_disk == D_UP_TO_DATE) {
3889 ov_out_of_sync_print(mdev);
3890 drbd_resync_finished(mdev);
3891 return 0;
3892 }
3893
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003894 /* peer says his disk is inconsistent, while we think it is uptodate,
3895 * and this happens while the peer still thinks we have a sync going on,
3896 * but we think we are already done with the sync.
3897 * We ignore this to avoid flapping pdsk.
3898 * This should not happen, if the peer is a recent version of drbd. */
3899 if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT &&
3900 os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE)
3901 real_peer_disk = D_UP_TO_DATE;
3902
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003903 if (ns.conn == C_WF_REPORT_PARAMS)
3904 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003905
Philipp Reisner67531712010-10-27 12:21:30 +02003906 if (peer_state.conn == C_AHEAD)
3907 ns.conn = C_BEHIND;
3908
Philipp Reisnerb411b362009-09-25 16:07:19 -07003909 if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING &&
3910 get_ldev_if_state(mdev, D_NEGOTIATING)) {
3911 int cr; /* consider resync */
3912
3913 /* if we established a new connection */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003914 cr = (os.conn < C_CONNECTED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003915 /* if we had an established connection
3916 * and one of the nodes newly attaches a disk */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003917 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003918 (peer_state.disk == D_NEGOTIATING ||
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003919 os.disk == D_NEGOTIATING));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003920 /* if we have both been inconsistent, and the peer has been
3921 * forced to be UpToDate with --overwrite-data */
3922 cr |= test_bit(CONSIDER_RESYNC, &mdev->flags);
3923 /* if we had been plain connected, and the admin requested to
3924 * start a sync by "invalidate" or "invalidate-remote" */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003925 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003926 (peer_state.conn >= C_STARTING_SYNC_S &&
3927 peer_state.conn <= C_WF_BITMAP_T));
3928
3929 if (cr)
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003930 ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003931
3932 put_ldev(mdev);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003933 if (ns.conn == C_MASK) {
3934 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003935 if (mdev->state.disk == D_NEGOTIATING) {
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003936 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003937 } else if (peer_state.disk == D_NEGOTIATING) {
3938 dev_err(DEV, "Disk attach process on the peer node was aborted.\n");
3939 peer_state.disk = D_DISKLESS;
Lars Ellenberg580b9762010-02-26 23:15:23 +01003940 real_peer_disk = D_DISKLESS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003941 } else {
Philipp Reisner8169e412011-03-15 18:40:27 +01003942 if (test_and_clear_bit(CONN_DRY_RUN, &mdev->tconn->flags))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003943 return -EIO;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003944 D_ASSERT(os.conn == C_WF_REPORT_PARAMS);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003945 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003946 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003947 }
3948 }
3949 }
3950
Philipp Reisner87eeee42011-01-19 14:16:30 +01003951 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner78bae592011-03-28 15:40:12 +02003952 if (os.i != drbd_read_state(mdev).i)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003953 goto retry;
3954 clear_bit(CONSIDER_RESYNC, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003955 ns.peer = peer_state.role;
3956 ns.pdsk = real_peer_disk;
3957 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003958 if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003959 ns.disk = mdev->new_state_tmp.disk;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003960 cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD);
Philipp Reisner2aebfab2011-03-28 16:48:11 +02003961 if (ns.pdsk == D_CONSISTENT && drbd_suspended(mdev) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED &&
Philipp Reisner481c6f52010-06-22 14:03:27 +02003962 test_bit(NEW_CUR_UUID, &mdev->flags)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01003963 /* Do not allow tl_restart(RESEND) for a rebooted peer. We can only allow this
Philipp Reisner481c6f52010-06-22 14:03:27 +02003964 for temporal network outages! */
Philipp Reisner87eeee42011-01-19 14:16:30 +01003965 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003966 dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01003967 tl_clear(mdev->tconn);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003968 drbd_uuid_new_current(mdev);
3969 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003970 conn_request_state(mdev->tconn, NS2(conn, C_PROTOCOL_ERROR, susp, 0), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003971 return -EIO;
Philipp Reisner481c6f52010-06-22 14:03:27 +02003972 }
Philipp Reisner65d922c2010-06-16 16:18:09 +02003973 rv = _drbd_set_state(mdev, ns, cs_flags, NULL);
Philipp Reisner78bae592011-03-28 15:40:12 +02003974 ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003975 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003976
3977 if (rv < SS_SUCCESS) {
Philipp Reisner38fa9982011-03-15 18:24:49 +01003978 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003979 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003980 }
3981
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003982 if (os.conn > C_WF_REPORT_PARAMS) {
3983 if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003984 peer_state.disk != D_NEGOTIATING ) {
3985 /* we want resync, peer has not yet decided to sync... */
3986 /* Nowadays only used when forcing a node into primary role and
3987 setting its disk to UpToDate with that */
3988 drbd_send_uuids(mdev);
Philipp Reisner43de7c82011-11-10 13:16:13 +01003989 drbd_send_current_state(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003990 }
3991 }
3992
Philipp Reisner08b165b2011-09-05 16:22:33 +02003993 clear_bit(DISCARD_MY_DATA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003994
3995 drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
3996
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003997 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003998}
3999
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004000static int receive_sync_uuid(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004001{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004002 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004003 struct p_rs_uuid *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004004
4005 mdev = vnr_to_mdev(tconn, pi->vnr);
4006 if (!mdev)
4007 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004008
4009 wait_event(mdev->misc_wait,
4010 mdev->state.conn == C_WF_SYNC_UUID ||
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02004011 mdev->state.conn == C_BEHIND ||
Philipp Reisnerb411b362009-09-25 16:07:19 -07004012 mdev->state.conn < C_CONNECTED ||
4013 mdev->state.disk < D_NEGOTIATING);
4014
4015 /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */
4016
Philipp Reisnerb411b362009-09-25 16:07:19 -07004017 /* Here the _drbd_uuid_ functions are right, current should
4018 _not_ be rotated into the history */
4019 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
4020 _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid));
4021 _drbd_uuid_set(mdev, UI_BITMAP, 0UL);
4022
Lars Ellenberg62b0da32011-01-20 13:25:21 +01004023 drbd_print_uuids(mdev, "updated sync uuid");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004024 drbd_start_resync(mdev, C_SYNC_TARGET);
4025
4026 put_ldev(mdev);
4027 } else
4028 dev_err(DEV, "Ignoring SyncUUID packet!\n");
4029
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004030 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004031}
4032
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004033/**
4034 * receive_bitmap_plain
4035 *
4036 * Return 0 when done, 1 when another iteration is needed, and a negative error
4037 * code upon failure.
4038 */
4039static int
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004040receive_bitmap_plain(struct drbd_conf *mdev, unsigned int size,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004041 unsigned long *p, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004042{
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004043 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE -
4044 drbd_header_size(mdev->tconn);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004045 unsigned int num_words = min_t(size_t, data_size / sizeof(*p),
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004046 c->bm_words - c->word_offset);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004047 unsigned int want = num_words * sizeof(*p);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004048 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004049
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004050 if (want != size) {
4051 dev_err(DEV, "%s:want (%u) != size (%u)\n", __func__, want, size);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004052 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004053 }
4054 if (want == 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004055 return 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004056 err = drbd_recv_all(mdev->tconn, p, want);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004057 if (err)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004058 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004059
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004060 drbd_bm_merge_lel(mdev, c->word_offset, num_words, p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004061
4062 c->word_offset += num_words;
4063 c->bit_offset = c->word_offset * BITS_PER_LONG;
4064 if (c->bit_offset > c->bm_bits)
4065 c->bit_offset = c->bm_bits;
4066
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004067 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004068}
4069
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004070static enum drbd_bitmap_code dcbp_get_code(struct p_compressed_bm *p)
4071{
4072 return (enum drbd_bitmap_code)(p->encoding & 0x0f);
4073}
4074
4075static int dcbp_get_start(struct p_compressed_bm *p)
4076{
4077 return (p->encoding & 0x80) != 0;
4078}
4079
4080static int dcbp_get_pad_bits(struct p_compressed_bm *p)
4081{
4082 return (p->encoding >> 4) & 0x7;
4083}
4084
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004085/**
4086 * recv_bm_rle_bits
4087 *
4088 * Return 0 when done, 1 when another iteration is needed, and a negative error
4089 * code upon failure.
4090 */
4091static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07004092recv_bm_rle_bits(struct drbd_conf *mdev,
4093 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01004094 struct bm_xfer_ctx *c,
4095 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004096{
4097 struct bitstream bs;
4098 u64 look_ahead;
4099 u64 rl;
4100 u64 tmp;
4101 unsigned long s = c->bit_offset;
4102 unsigned long e;
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004103 int toggle = dcbp_get_start(p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004104 int have;
4105 int bits;
4106
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004107 bitstream_init(&bs, p->code, len, dcbp_get_pad_bits(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004108
4109 bits = bitstream_get_bits(&bs, &look_ahead, 64);
4110 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004111 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004112
4113 for (have = bits; have > 0; s += rl, toggle = !toggle) {
4114 bits = vli_decode_bits(&rl, look_ahead);
4115 if (bits <= 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004116 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004117
4118 if (toggle) {
4119 e = s + rl -1;
4120 if (e >= c->bm_bits) {
4121 dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004122 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004123 }
4124 _drbd_bm_set_bits(mdev, s, e);
4125 }
4126
4127 if (have < bits) {
4128 dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
4129 have, bits, look_ahead,
4130 (unsigned int)(bs.cur.b - p->code),
4131 (unsigned int)bs.buf_len);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004132 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004133 }
4134 look_ahead >>= bits;
4135 have -= bits;
4136
4137 bits = bitstream_get_bits(&bs, &tmp, 64 - have);
4138 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004139 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004140 look_ahead |= tmp << have;
4141 have += bits;
4142 }
4143
4144 c->bit_offset = s;
4145 bm_xfer_ctx_bit_to_word_offset(c);
4146
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004147 return (s != c->bm_bits);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004148}
4149
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004150/**
4151 * decode_bitmap_c
4152 *
4153 * Return 0 when done, 1 when another iteration is needed, and a negative error
4154 * code upon failure.
4155 */
4156static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07004157decode_bitmap_c(struct drbd_conf *mdev,
4158 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01004159 struct bm_xfer_ctx *c,
4160 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004161{
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004162 if (dcbp_get_code(p) == RLE_VLI_Bits)
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004163 return recv_bm_rle_bits(mdev, p, c, len - sizeof(*p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004164
4165 /* other variants had been implemented for evaluation,
4166 * but have been dropped as this one turned out to be "best"
4167 * during all our tests. */
4168
4169 dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
Philipp Reisner38fa9982011-03-15 18:24:49 +01004170 conn_request_state(mdev->tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004171 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004172}
4173
4174void INFO_bm_xfer_stats(struct drbd_conf *mdev,
4175 const char *direction, struct bm_xfer_ctx *c)
4176{
4177 /* what would it take to transfer it "plaintext" */
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004178 unsigned int header_size = drbd_header_size(mdev->tconn);
4179 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE - header_size;
4180 unsigned int plain =
4181 header_size * (DIV_ROUND_UP(c->bm_words, data_size) + 1) +
4182 c->bm_words * sizeof(unsigned long);
4183 unsigned int total = c->bytes[0] + c->bytes[1];
4184 unsigned int r;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004185
4186 /* total can not be zero. but just in case: */
4187 if (total == 0)
4188 return;
4189
4190 /* don't report if not compressed */
4191 if (total >= plain)
4192 return;
4193
4194 /* total < plain. check for overflow, still */
4195 r = (total > UINT_MAX/1000) ? (total / (plain/1000))
4196 : (1000 * total / plain);
4197
4198 if (r > 1000)
4199 r = 1000;
4200
4201 r = 1000 - r;
4202 dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
4203 "total %u; compression: %u.%u%%\n",
4204 direction,
4205 c->bytes[1], c->packets[1],
4206 c->bytes[0], c->packets[0],
4207 total, r/10, r % 10);
4208}
4209
4210/* Since we are processing the bitfield from lower addresses to higher,
4211 it does not matter if the process it in 32 bit chunks or 64 bit
4212 chunks as long as it is little endian. (Understand it as byte stream,
4213 beginning with the lowest byte...) If we would use big endian
4214 we would need to process it from the highest address to the lowest,
4215 in order to be agnostic to the 32 vs 64 bits issue.
4216
4217 returns 0 on failure, 1 if we successfully received it. */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004218static int receive_bitmap(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004219{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004220 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004221 struct bm_xfer_ctx c;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004222 int err;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004223
4224 mdev = vnr_to_mdev(tconn, pi->vnr);
4225 if (!mdev)
4226 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004227
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004228 drbd_bm_lock(mdev, "receive bitmap", BM_LOCKED_SET_ALLOWED);
4229 /* you are supposed to send additional out-of-sync information
4230 * if you actually set bits during this phase */
Philipp Reisnerb411b362009-09-25 16:07:19 -07004231
Philipp Reisnerb411b362009-09-25 16:07:19 -07004232 c = (struct bm_xfer_ctx) {
4233 .bm_bits = drbd_bm_bits(mdev),
4234 .bm_words = drbd_bm_words(mdev),
4235 };
4236
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004237 for(;;) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004238 if (pi->cmd == P_BITMAP)
4239 err = receive_bitmap_plain(mdev, pi->size, pi->data, &c);
4240 else if (pi->cmd == P_COMPRESSED_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004241 /* MAYBE: sanity check that we speak proto >= 90,
4242 * and the feature is enabled! */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004243 struct p_compressed_bm *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004244
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004245 if (pi->size > DRBD_SOCKET_BUFFER_SIZE - drbd_header_size(tconn)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004246 dev_err(DEV, "ReportCBitmap packet too large\n");
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004247 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004248 goto out;
4249 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004250 if (pi->size <= sizeof(*p)) {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004251 dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004252 err = -EIO;
Andreas Gruenbacher78fcbda2010-12-10 22:18:27 +01004253 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004254 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004255 err = drbd_recv_all(mdev->tconn, p, pi->size);
4256 if (err)
4257 goto out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004258 err = decode_bitmap_c(mdev, p, &c, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004259 } else {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004260 dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", pi->cmd);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004261 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004262 goto out;
4263 }
4264
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004265 c.packets[pi->cmd == P_BITMAP]++;
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004266 c.bytes[pi->cmd == P_BITMAP] += drbd_header_size(tconn) + pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004267
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004268 if (err <= 0) {
4269 if (err < 0)
4270 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004271 break;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004272 }
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004273 err = drbd_recv_header(mdev->tconn, pi);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004274 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004275 goto out;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004276 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004277
4278 INFO_bm_xfer_stats(mdev, "receive", &c);
4279
4280 if (mdev->state.conn == C_WF_BITMAP_T) {
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01004281 enum drbd_state_rv rv;
4282
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004283 err = drbd_send_bitmap(mdev);
4284 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004285 goto out;
4286 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01004287 rv = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
4288 D_ASSERT(rv == SS_SUCCESS);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004289 } else if (mdev->state.conn != C_WF_BITMAP_S) {
4290 /* admin may have requested C_DISCONNECTING,
4291 * other threads may have noticed network errors */
4292 dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n",
4293 drbd_conn_str(mdev->state.conn));
4294 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004295 err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004296
Philipp Reisnerb411b362009-09-25 16:07:19 -07004297 out:
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004298 drbd_bm_unlock(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004299 if (!err && mdev->state.conn == C_WF_BITMAP_S)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004300 drbd_start_resync(mdev, C_SYNC_SOURCE);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004301 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004302}
4303
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004304static int receive_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004305{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004306 conn_warn(tconn, "skipping unknown optional packet type %d, l: %d!\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004307 pi->cmd, pi->size);
Philipp Reisner2de876e2011-03-15 14:38:01 +01004308
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004309 return ignore_remaining_packet(tconn, pi);
Philipp Reisner2de876e2011-03-15 14:38:01 +01004310}
4311
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004312static int receive_UnplugRemote(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004313{
Philipp Reisnerb411b362009-09-25 16:07:19 -07004314 /* Make sure we've acked all the TCP data associated
4315 * with the data requests being unplugged */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004316 drbd_tcp_quickack(tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004317
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004318 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004319}
4320
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004321static int receive_out_of_sync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner73a01a12010-10-27 14:33:00 +02004322{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004323 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004324 struct p_block_desc *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004325
4326 mdev = vnr_to_mdev(tconn, pi->vnr);
4327 if (!mdev)
4328 return -EIO;
Philipp Reisner73a01a12010-10-27 14:33:00 +02004329
Lars Ellenbergf735e362010-12-17 21:06:18 +01004330 switch (mdev->state.conn) {
4331 case C_WF_SYNC_UUID:
4332 case C_WF_BITMAP_T:
4333 case C_BEHIND:
4334 break;
4335 default:
4336 dev_err(DEV, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n",
4337 drbd_conn_str(mdev->state.conn));
4338 }
4339
Philipp Reisner73a01a12010-10-27 14:33:00 +02004340 drbd_set_out_of_sync(mdev, be64_to_cpu(p->sector), be32_to_cpu(p->blksize));
4341
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004342 return 0;
Philipp Reisner73a01a12010-10-27 14:33:00 +02004343}
4344
Philipp Reisner02918be2010-08-20 14:35:10 +02004345struct data_cmd {
4346 int expect_payload;
4347 size_t pkt_size;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004348 int (*fn)(struct drbd_tconn *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004349};
4350
Philipp Reisner02918be2010-08-20 14:35:10 +02004351static struct data_cmd drbd_cmd_handler[] = {
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004352 [P_DATA] = { 1, sizeof(struct p_data), receive_Data },
4353 [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply },
4354 [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } ,
4355 [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } ,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004356 [P_BITMAP] = { 1, 0, receive_bitmap } ,
4357 [P_COMPRESSED_BITMAP] = { 1, 0, receive_bitmap } ,
4358 [P_UNPLUG_REMOTE] = { 0, 0, receive_UnplugRemote },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004359 [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4360 [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004361 [P_SYNC_PARAM] = { 1, 0, receive_SyncParam },
4362 [P_SYNC_PARAM89] = { 1, 0, receive_SyncParam },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004363 [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol },
4364 [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids },
4365 [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes },
4366 [P_STATE] = { 0, sizeof(struct p_state), receive_state },
4367 [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state },
4368 [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid },
4369 [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4370 [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4371 [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4372 [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip },
4373 [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync },
4374 [P_CONN_ST_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_conn_state },
Philipp Reisner036b17e2011-05-16 17:38:11 +02004375 [P_PROTOCOL_UPDATE] = { 1, sizeof(struct p_protocol), receive_protocol },
Philipp Reisner02918be2010-08-20 14:35:10 +02004376};
4377
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004378static void drbdd(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004379{
Philipp Reisner77351055b2011-02-07 17:24:26 +01004380 struct packet_info pi;
Philipp Reisner02918be2010-08-20 14:35:10 +02004381 size_t shs; /* sub header size */
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004382 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004383
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004384 while (get_t_state(&tconn->receiver) == RUNNING) {
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004385 struct data_cmd *cmd;
4386
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004387 drbd_thread_current_set_cpu(&tconn->receiver);
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004388 if (drbd_recv_header(tconn, &pi))
Philipp Reisner02918be2010-08-20 14:35:10 +02004389 goto err_out;
4390
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004391 cmd = &drbd_cmd_handler[pi.cmd];
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004392 if (unlikely(pi.cmd >= ARRAY_SIZE(drbd_cmd_handler) || !cmd->fn)) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004393 conn_err(tconn, "Unexpected data packet %s (0x%04x)",
4394 cmdname(pi.cmd), pi.cmd);
Philipp Reisner02918be2010-08-20 14:35:10 +02004395 goto err_out;
Lars Ellenberg0b33a912009-11-16 15:58:04 +01004396 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004397
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004398 shs = cmd->pkt_size;
4399 if (pi.size > shs && !cmd->expect_payload) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004400 conn_err(tconn, "No payload expected %s l:%d\n",
4401 cmdname(pi.cmd), pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004402 goto err_out;
4403 }
4404
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004405 if (shs) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004406 err = drbd_recv_all_warn(tconn, pi.data, shs);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004407 if (err)
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004408 goto err_out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004409 pi.size -= shs;
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004410 }
4411
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004412 err = cmd->fn(tconn, &pi);
4413 if (err) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004414 conn_err(tconn, "error receiving %s, e: %d l: %d!\n",
4415 cmdname(pi.cmd), err, pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004416 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004417 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004418 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004419 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004420
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004421 err_out:
4422 conn_request_state(tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004423}
4424
Philipp Reisner0e29d162011-02-18 14:23:11 +01004425void conn_flush_workqueue(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004426{
4427 struct drbd_wq_barrier barr;
4428
4429 barr.w.cb = w_prev_work_done;
Philipp Reisner0e29d162011-02-18 14:23:11 +01004430 barr.w.tconn = tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004431 init_completion(&barr.done);
Lars Ellenbergd5b27b02011-11-14 15:42:37 +01004432 drbd_queue_work(&tconn->sender_work, &barr.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004433 wait_for_completion(&barr.done);
4434}
4435
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004436static void conn_disconnect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004437{
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004438 struct drbd_conf *mdev;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004439 enum drbd_conns oc;
Philipp Reisner376694a2011-11-07 10:54:28 +01004440 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004441
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004442 if (tconn->cstate == C_STANDALONE)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004443 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004444
Philipp Reisnerb8853db2011-12-13 11:09:16 +01004445 /* We are about to start the cleanup after connection loss.
4446 * Make sure drbd_make_request knows about that.
4447 * Usually we should be in some network failure state already,
4448 * but just in case we are not, we fix it up here.
4449 */
4450 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
4451
Philipp Reisnerb411b362009-09-25 16:07:19 -07004452 /* asender does not clean up anything. it must not interfere, either */
Philipp Reisner360cc742011-02-08 14:29:53 +01004453 drbd_thread_stop(&tconn->asender);
4454 drbd_free_sock(tconn);
4455
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004456 rcu_read_lock();
4457 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
4458 kref_get(&mdev->kref);
4459 rcu_read_unlock();
4460 drbd_disconnected(mdev);
4461 kref_put(&mdev->kref, &drbd_minor_destroy);
4462 rcu_read_lock();
4463 }
4464 rcu_read_unlock();
4465
Philipp Reisner12038a32011-11-09 19:18:00 +01004466 if (!list_empty(&tconn->current_epoch->list))
4467 conn_err(tconn, "ASSERTION FAILED: tconn->current_epoch->list not empty\n");
4468 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
4469 atomic_set(&tconn->current_epoch->epoch_size, 0);
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01004470 tconn->send.seen_any_write_yet = false;
Philipp Reisner12038a32011-11-09 19:18:00 +01004471
Philipp Reisner360cc742011-02-08 14:29:53 +01004472 conn_info(tconn, "Connection closed\n");
4473
Philipp Reisnercb703452011-03-24 11:03:07 +01004474 if (conn_highest_role(tconn) == R_PRIMARY && conn_highest_pdsk(tconn) >= D_UNKNOWN)
4475 conn_try_outdate_peer_async(tconn);
4476
Philipp Reisner360cc742011-02-08 14:29:53 +01004477 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004478 oc = tconn->cstate;
4479 if (oc >= C_UNCONNECTED)
Philipp Reisner376694a2011-11-07 10:54:28 +01004480 _conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004481
Philipp Reisner360cc742011-02-08 14:29:53 +01004482 spin_unlock_irq(&tconn->req_lock);
4483
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02004484 if (oc == C_DISCONNECTING)
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +02004485 conn_request_state(tconn, NS(conn, C_STANDALONE), CS_VERBOSE | CS_HARD);
Philipp Reisner360cc742011-02-08 14:29:53 +01004486}
4487
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004488static int drbd_disconnected(struct drbd_conf *mdev)
Philipp Reisner360cc742011-02-08 14:29:53 +01004489{
Philipp Reisner360cc742011-02-08 14:29:53 +01004490 unsigned int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004491
Philipp Reisner85719572010-07-21 10:20:17 +02004492 /* wait for current activity to cease. */
Philipp Reisner87eeee42011-01-19 14:16:30 +01004493 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004494 _drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
4495 _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee);
4496 _drbd_wait_ee_list_empty(mdev, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004497 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004498
4499 /* We do not have data structures that would allow us to
4500 * get the rs_pending_cnt down to 0 again.
4501 * * On C_SYNC_TARGET we do not have any data structures describing
4502 * the pending RSDataRequest's we have sent.
4503 * * On C_SYNC_SOURCE there is no data structure that tracks
4504 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
4505 * And no, it is not the sum of the reference counts in the
4506 * resync_LRU. The resync_LRU tracks the whole operation including
4507 * the disk-IO, while the rs_pending_cnt only tracks the blocks
4508 * on the fly. */
4509 drbd_rs_cancel_all(mdev);
4510 mdev->rs_total = 0;
4511 mdev->rs_failed = 0;
4512 atomic_set(&mdev->rs_pending_cnt, 0);
4513 wake_up(&mdev->misc_wait);
4514
Philipp Reisnerb411b362009-09-25 16:07:19 -07004515 del_timer_sync(&mdev->resync_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004516 resync_timer_fn((unsigned long)mdev);
4517
Philipp Reisnerb411b362009-09-25 16:07:19 -07004518 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
4519 * w_make_resync_request etc. which may still be on the worker queue
4520 * to be "canceled" */
Philipp Reisnera21e9292011-02-08 15:08:49 +01004521 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004522
Andreas Gruenbachera990be42011-04-06 17:56:48 +02004523 drbd_finish_peer_reqs(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004524
Philipp Reisnerd10b4ea2011-11-30 23:25:36 +01004525 /* This second workqueue flush is necessary, since drbd_finish_peer_reqs()
4526 might have issued a work again. The one before drbd_finish_peer_reqs() is
4527 necessary to reclain net_ee in drbd_finish_peer_reqs(). */
4528 drbd_flush_workqueue(mdev);
4529
Philipp Reisnerb411b362009-09-25 16:07:19 -07004530 kfree(mdev->p_uuid);
4531 mdev->p_uuid = NULL;
4532
Philipp Reisner2aebfab2011-03-28 16:48:11 +02004533 if (!drbd_suspended(mdev))
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01004534 tl_clear(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004535
Philipp Reisnerb411b362009-09-25 16:07:19 -07004536 drbd_md_sync(mdev);
4537
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004538 /* serialize with bitmap writeout triggered by the state change,
4539 * if any. */
4540 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
4541
Philipp Reisnerb411b362009-09-25 16:07:19 -07004542 /* tcp_close and release of sendpage pages can be deferred. I don't
4543 * want to use SO_LINGER, because apparently it can be deferred for
4544 * more than 20 seconds (longest time I checked).
4545 *
4546 * Actually we don't care for exactly when the network stack does its
4547 * put_page(), but release our reference on these pages right here.
4548 */
Andreas Gruenbacher7721f562011-04-06 17:14:02 +02004549 i = drbd_free_peer_reqs(mdev, &mdev->net_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004550 if (i)
4551 dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
Lars Ellenberg435f0742010-09-06 12:30:25 +02004552 i = atomic_read(&mdev->pp_in_use_by_net);
4553 if (i)
4554 dev_info(DEV, "pp_in_use_by_net = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004555 i = atomic_read(&mdev->pp_in_use);
4556 if (i)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02004557 dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004558
4559 D_ASSERT(list_empty(&mdev->read_ee));
4560 D_ASSERT(list_empty(&mdev->active_ee));
4561 D_ASSERT(list_empty(&mdev->sync_ee));
4562 D_ASSERT(list_empty(&mdev->done_ee));
4563
Philipp Reisner360cc742011-02-08 14:29:53 +01004564 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004565}
4566
4567/*
4568 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
4569 * we can agree on is stored in agreed_pro_version.
4570 *
4571 * feature flags and the reserved array should be enough room for future
4572 * enhancements of the handshake protocol, and possible plugins...
4573 *
4574 * for now, they are expected to be zero, but ignored.
4575 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004576static int drbd_send_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004577{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004578 struct drbd_socket *sock;
4579 struct p_connection_features *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004580
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004581 sock = &tconn->data;
4582 p = conn_prepare_command(tconn, sock);
4583 if (!p)
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004584 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004585 memset(p, 0, sizeof(*p));
4586 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
4587 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004588 return conn_send_command(tconn, sock, P_CONNECTION_FEATURES, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004589}
4590
4591/*
4592 * return values:
4593 * 1 yes, we have a valid connection
4594 * 0 oops, did not work out, please try again
4595 * -1 peer talks different language,
4596 * no point in trying again, please go standalone.
4597 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004598static int drbd_do_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004599{
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004600 /* ASSERT current == tconn->receiver ... */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004601 struct p_connection_features *p;
4602 const int expect = sizeof(struct p_connection_features);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004603 struct packet_info pi;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004604 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004605
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004606 err = drbd_send_features(tconn);
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004607 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004608 return 0;
4609
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004610 err = drbd_recv_header(tconn, &pi);
4611 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004612 return 0;
4613
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004614 if (pi.cmd != P_CONNECTION_FEATURES) {
4615 conn_err(tconn, "expected ConnectionFeatures packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004616 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004617 return -1;
4618 }
4619
Philipp Reisner77351055b2011-02-07 17:24:26 +01004620 if (pi.size != expect) {
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004621 conn_err(tconn, "expected ConnectionFeatures length: %u, received: %u\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004622 expect, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004623 return -1;
4624 }
4625
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004626 p = pi.data;
4627 err = drbd_recv_all_warn(tconn, p, expect);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004628 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004629 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004630
Philipp Reisnerb411b362009-09-25 16:07:19 -07004631 p->protocol_min = be32_to_cpu(p->protocol_min);
4632 p->protocol_max = be32_to_cpu(p->protocol_max);
4633 if (p->protocol_max == 0)
4634 p->protocol_max = p->protocol_min;
4635
4636 if (PRO_VERSION_MAX < p->protocol_min ||
4637 PRO_VERSION_MIN > p->protocol_max)
4638 goto incompat;
4639
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004640 tconn->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004641
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004642 conn_info(tconn, "Handshake successful: "
4643 "Agreed network protocol version %d\n", tconn->agreed_pro_version);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004644
4645 return 1;
4646
4647 incompat:
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004648 conn_err(tconn, "incompatible DRBD dialects: "
Philipp Reisnerb411b362009-09-25 16:07:19 -07004649 "I support %d-%d, peer supports %d-%d\n",
4650 PRO_VERSION_MIN, PRO_VERSION_MAX,
4651 p->protocol_min, p->protocol_max);
4652 return -1;
4653}
4654
4655#if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
Philipp Reisner13e60372011-02-08 09:54:40 +01004656static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004657{
4658 dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
4659 dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004660 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004661}
4662#else
4663#define CHALLENGE_LEN 64
Johannes Thomab10d96c2010-01-07 16:02:50 +01004664
4665/* Return value:
4666 1 - auth succeeded,
4667 0 - failed, try again (network error),
4668 -1 - auth failed, don't try again.
4669*/
4670
Philipp Reisner13e60372011-02-08 09:54:40 +01004671static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004672{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004673 struct drbd_socket *sock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004674 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
4675 struct scatterlist sg;
4676 char *response = NULL;
4677 char *right_response = NULL;
4678 char *peers_ch = NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +02004679 unsigned int key_len;
4680 char secret[SHARED_SECRET_MAX]; /* 64 byte */
Philipp Reisnerb411b362009-09-25 16:07:19 -07004681 unsigned int resp_size;
4682 struct hash_desc desc;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004683 struct packet_info pi;
Philipp Reisner44ed1672011-04-19 17:10:19 +02004684 struct net_conf *nc;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004685 int err, rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004686
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004687 /* FIXME: Put the challenge/response into the preallocated socket buffer. */
4688
Philipp Reisner44ed1672011-04-19 17:10:19 +02004689 rcu_read_lock();
4690 nc = rcu_dereference(tconn->net_conf);
4691 key_len = strlen(nc->shared_secret);
4692 memcpy(secret, nc->shared_secret, key_len);
4693 rcu_read_unlock();
4694
Philipp Reisner13e60372011-02-08 09:54:40 +01004695 desc.tfm = tconn->cram_hmac_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004696 desc.flags = 0;
4697
Philipp Reisner44ed1672011-04-19 17:10:19 +02004698 rv = crypto_hash_setkey(tconn->cram_hmac_tfm, (u8 *)secret, key_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004699 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004700 conn_err(tconn, "crypto_hash_setkey() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004701 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004702 goto fail;
4703 }
4704
4705 get_random_bytes(my_challenge, CHALLENGE_LEN);
4706
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004707 sock = &tconn->data;
4708 if (!conn_prepare_command(tconn, sock)) {
4709 rv = 0;
4710 goto fail;
4711 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004712 rv = !conn_send_command(tconn, sock, P_AUTH_CHALLENGE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004713 my_challenge, CHALLENGE_LEN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004714 if (!rv)
4715 goto fail;
4716
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004717 err = drbd_recv_header(tconn, &pi);
4718 if (err) {
4719 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004720 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004721 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004722
Philipp Reisner77351055b2011-02-07 17:24:26 +01004723 if (pi.cmd != P_AUTH_CHALLENGE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004724 conn_err(tconn, "expected AuthChallenge packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004725 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004726 rv = 0;
4727 goto fail;
4728 }
4729
Philipp Reisner77351055b2011-02-07 17:24:26 +01004730 if (pi.size > CHALLENGE_LEN * 2) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004731 conn_err(tconn, "expected AuthChallenge payload too big.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004732 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004733 goto fail;
4734 }
4735
Philipp Reisner77351055b2011-02-07 17:24:26 +01004736 peers_ch = kmalloc(pi.size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004737 if (peers_ch == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004738 conn_err(tconn, "kmalloc of peers_ch failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004739 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004740 goto fail;
4741 }
4742
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004743 err = drbd_recv_all_warn(tconn, peers_ch, pi.size);
4744 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004745 rv = 0;
4746 goto fail;
4747 }
4748
Philipp Reisner13e60372011-02-08 09:54:40 +01004749 resp_size = crypto_hash_digestsize(tconn->cram_hmac_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004750 response = kmalloc(resp_size, GFP_NOIO);
4751 if (response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004752 conn_err(tconn, "kmalloc of response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004753 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004754 goto fail;
4755 }
4756
4757 sg_init_table(&sg, 1);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004758 sg_set_buf(&sg, peers_ch, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004759
4760 rv = crypto_hash_digest(&desc, &sg, sg.length, response);
4761 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004762 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004763 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004764 goto fail;
4765 }
4766
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004767 if (!conn_prepare_command(tconn, sock)) {
4768 rv = 0;
4769 goto fail;
4770 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004771 rv = !conn_send_command(tconn, sock, P_AUTH_RESPONSE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004772 response, resp_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004773 if (!rv)
4774 goto fail;
4775
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004776 err = drbd_recv_header(tconn, &pi);
4777 if (err) {
4778 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004779 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004780 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004781
Philipp Reisner77351055b2011-02-07 17:24:26 +01004782 if (pi.cmd != P_AUTH_RESPONSE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004783 conn_err(tconn, "expected AuthResponse packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004784 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004785 rv = 0;
4786 goto fail;
4787 }
4788
Philipp Reisner77351055b2011-02-07 17:24:26 +01004789 if (pi.size != resp_size) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004790 conn_err(tconn, "expected AuthResponse payload of wrong size\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004791 rv = 0;
4792 goto fail;
4793 }
4794
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004795 err = drbd_recv_all_warn(tconn, response , resp_size);
4796 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004797 rv = 0;
4798 goto fail;
4799 }
4800
4801 right_response = kmalloc(resp_size, GFP_NOIO);
Julia Lawall2d1ee872009-12-27 22:27:11 +01004802 if (right_response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004803 conn_err(tconn, "kmalloc of right_response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004804 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004805 goto fail;
4806 }
4807
4808 sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
4809
4810 rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
4811 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004812 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004813 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004814 goto fail;
4815 }
4816
4817 rv = !memcmp(response, right_response, resp_size);
4818
4819 if (rv)
Philipp Reisner44ed1672011-04-19 17:10:19 +02004820 conn_info(tconn, "Peer authenticated using %d bytes HMAC\n",
4821 resp_size);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004822 else
4823 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004824
4825 fail:
4826 kfree(peers_ch);
4827 kfree(response);
4828 kfree(right_response);
4829
4830 return rv;
4831}
4832#endif
4833
4834int drbdd_init(struct drbd_thread *thi)
4835{
Philipp Reisner392c8802011-02-09 10:33:31 +01004836 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004837 int h;
4838
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004839 conn_info(tconn, "receiver (re)started\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004840
4841 do {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004842 h = conn_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004843 if (h == 0) {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004844 conn_disconnect(tconn);
Philipp Reisner20ee6392011-01-18 15:28:59 +01004845 schedule_timeout_interruptible(HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004846 }
4847 if (h == -1) {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004848 conn_warn(tconn, "Discarding network configuration.\n");
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004849 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004850 }
4851 } while (h == 0);
4852
Philipp Reisner91fd4da2011-04-20 17:47:29 +02004853 if (h > 0)
4854 drbdd(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004855
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004856 conn_disconnect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004857
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004858 conn_info(tconn, "receiver terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004859 return 0;
4860}
4861
4862/* ********* acknowledge sender ******** */
4863
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004864static int got_conn_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004865{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004866 struct p_req_state_reply *p = pi->data;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004867 int retcode = be32_to_cpu(p->retcode);
4868
4869 if (retcode >= SS_SUCCESS) {
4870 set_bit(CONN_WD_ST_CHG_OKAY, &tconn->flags);
4871 } else {
4872 set_bit(CONN_WD_ST_CHG_FAIL, &tconn->flags);
4873 conn_err(tconn, "Requested state change failed by peer: %s (%d)\n",
4874 drbd_set_st_err_str(retcode), retcode);
4875 }
4876 wake_up(&tconn->ping_wait);
4877
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004878 return 0;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004879}
4880
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004881static int got_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004882{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004883 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004884 struct p_req_state_reply *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004885 int retcode = be32_to_cpu(p->retcode);
4886
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004887 mdev = vnr_to_mdev(tconn, pi->vnr);
4888 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004889 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004890
Philipp Reisner4d0fc3f2012-01-20 13:52:27 +01004891 if (test_bit(CONN_WD_ST_CHG_REQ, &tconn->flags)) {
4892 D_ASSERT(tconn->agreed_pro_version < 100);
4893 return got_conn_RqSReply(tconn, pi);
4894 }
4895
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004896 if (retcode >= SS_SUCCESS) {
4897 set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
4898 } else {
4899 set_bit(CL_ST_CHG_FAIL, &mdev->flags);
4900 dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
4901 drbd_set_st_err_str(retcode), retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004902 }
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004903 wake_up(&mdev->state_wait);
4904
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004905 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004906}
4907
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004908static int got_Ping(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004909{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004910 return drbd_send_ping_ack(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004911
4912}
4913
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004914static int got_PingAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004915{
4916 /* restore idle timeout */
Philipp Reisner2a67d8b2011-02-09 14:10:32 +01004917 tconn->meta.socket->sk->sk_rcvtimeo = tconn->net_conf->ping_int*HZ;
4918 if (!test_and_set_bit(GOT_PING_ACK, &tconn->flags))
4919 wake_up(&tconn->ping_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004920
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004921 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004922}
4923
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004924static int got_IsInSync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004925{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004926 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004927 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004928 sector_t sector = be64_to_cpu(p->sector);
4929 int blksize = be32_to_cpu(p->blksize);
4930
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004931 mdev = vnr_to_mdev(tconn, pi->vnr);
4932 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004933 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004934
Philipp Reisner31890f42011-01-19 14:12:51 +01004935 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004936
4937 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4938
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004939 if (get_ldev(mdev)) {
4940 drbd_rs_complete_io(mdev, sector);
4941 drbd_set_in_sync(mdev, sector, blksize);
4942 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
4943 mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
4944 put_ldev(mdev);
4945 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004946 dec_rs_pending(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02004947 atomic_add(blksize >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004948
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004949 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004950}
4951
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004952static int
4953validate_req_change_req_state(struct drbd_conf *mdev, u64 id, sector_t sector,
4954 struct rb_root *root, const char *func,
4955 enum drbd_req_event what, bool missing_ok)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004956{
4957 struct drbd_request *req;
4958 struct bio_and_error m;
4959
Philipp Reisner87eeee42011-01-19 14:16:30 +01004960 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004961 req = find_request(mdev, root, id, sector, missing_ok, func);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004962 if (unlikely(!req)) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01004963 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004964 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004965 }
4966 __req_mod(req, what, &m);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004967 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004968
4969 if (m.bio)
4970 complete_master_bio(mdev, &m);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004971 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004972}
4973
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004974static int got_BlockAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004975{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004976 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004977 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004978 sector_t sector = be64_to_cpu(p->sector);
4979 int blksize = be32_to_cpu(p->blksize);
4980 enum drbd_req_event what;
4981
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004982 mdev = vnr_to_mdev(tconn, pi->vnr);
4983 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004984 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004985
Philipp Reisnerb411b362009-09-25 16:07:19 -07004986 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4987
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004988 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004989 drbd_set_in_sync(mdev, sector, blksize);
4990 dec_rs_pending(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004991 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004992 }
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004993 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004994 case P_RS_WRITE_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004995 what = WRITE_ACKED_BY_PEER_AND_SIS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004996 break;
4997 case P_WRITE_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004998 what = WRITE_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004999 break;
5000 case P_RECV_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01005001 what = RECV_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005002 break;
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02005003 case P_SUPERSEDED:
5004 what = CONFLICT_RESOLVED;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01005005 break;
5006 case P_RETRY_WRITE:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01005007 what = POSTPONE_WRITE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005008 break;
5009 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005010 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07005011 }
5012
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005013 return validate_req_change_req_state(mdev, p->block_id, sector,
5014 &mdev->write_requests, __func__,
5015 what, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005016}
5017
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005018static int got_NegAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005019{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005020 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005021 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005022 sector_t sector = be64_to_cpu(p->sector);
Philipp Reisner2deb8332011-01-17 18:39:18 +01005023 int size = be32_to_cpu(p->blksize);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02005024 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005025
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005026 mdev = vnr_to_mdev(tconn, pi->vnr);
5027 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005028 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005029
Philipp Reisnerb411b362009-09-25 16:07:19 -07005030 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5031
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01005032 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07005033 dec_rs_pending(mdev);
5034 drbd_rs_failed_io(mdev, sector, size);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005035 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005036 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01005037
Andreas Gruenbacher85997672011-04-04 13:09:15 +02005038 err = validate_req_change_req_state(mdev, p->block_id, sector,
5039 &mdev->write_requests, __func__,
Philipp Reisner303d1442011-04-13 16:24:47 -07005040 NEG_ACKED, true);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02005041 if (err) {
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01005042 /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs.
5043 The master bio might already be completed, therefore the
5044 request is no longer in the collision hash. */
5045 /* In Protocol B we might already have got a P_RECV_ACK
5046 but then get a P_NEG_ACK afterwards. */
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01005047 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisner2deb8332011-01-17 18:39:18 +01005048 }
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005049 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005050}
5051
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005052static int got_NegDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005053{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005054 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005055 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005056 sector_t sector = be64_to_cpu(p->sector);
5057
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005058 mdev = vnr_to_mdev(tconn, pi->vnr);
5059 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005060 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005061
Philipp Reisnerb411b362009-09-25 16:07:19 -07005062 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01005063
Philipp Reisner380207d2011-11-11 12:31:20 +01005064 dev_err(DEV, "Got NegDReply; Sector %llus, len %u.\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -07005065 (unsigned long long)sector, be32_to_cpu(p->blksize));
5066
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005067 return validate_req_change_req_state(mdev, p->block_id, sector,
5068 &mdev->read_requests, __func__,
5069 NEG_ACKED, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005070}
5071
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005072static int got_NegRSDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005073{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005074 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005075 sector_t sector;
5076 int size;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005077 struct p_block_ack *p = pi->data;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005078
5079 mdev = vnr_to_mdev(tconn, pi->vnr);
5080 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005081 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005082
5083 sector = be64_to_cpu(p->sector);
5084 size = be32_to_cpu(p->blksize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005085
5086 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5087
5088 dec_rs_pending(mdev);
5089
5090 if (get_ldev_if_state(mdev, D_FAILED)) {
5091 drbd_rs_complete_io(mdev, sector);
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01005092 switch (pi->cmd) {
Philipp Reisnerd612d302010-12-27 10:53:28 +01005093 case P_NEG_RS_DREPLY:
5094 drbd_rs_failed_io(mdev, sector, size);
5095 case P_RS_CANCEL:
5096 break;
5097 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005098 BUG();
Philipp Reisnerd612d302010-12-27 10:53:28 +01005099 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005100 put_ldev(mdev);
5101 }
5102
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005103 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005104}
5105
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005106static int got_BarrierAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005107{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005108 struct p_barrier_ack *p = pi->data;
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02005109 struct drbd_conf *mdev;
5110 int vnr;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005111
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02005112 tl_release(tconn, p->barrier, be32_to_cpu(p->set_size));
Philipp Reisnerb411b362009-09-25 16:07:19 -07005113
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02005114 rcu_read_lock();
5115 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
5116 if (mdev->state.conn == C_AHEAD &&
5117 atomic_read(&mdev->ap_in_flight) == 0 &&
5118 !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->flags)) {
5119 mdev->start_resync_timer.expires = jiffies + HZ;
5120 add_timer(&mdev->start_resync_timer);
5121 }
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02005122 }
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02005123 rcu_read_unlock();
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02005124
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005125 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005126}
5127
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005128static int got_OVResult(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005129{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005130 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005131 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005132 struct drbd_work *w;
5133 sector_t sector;
5134 int size;
5135
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005136 mdev = vnr_to_mdev(tconn, pi->vnr);
5137 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005138 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005139
Philipp Reisnerb411b362009-09-25 16:07:19 -07005140 sector = be64_to_cpu(p->sector);
5141 size = be32_to_cpu(p->blksize);
5142
5143 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5144
5145 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005146 drbd_ov_out_of_sync_found(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005147 else
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005148 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005149
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005150 if (!get_ldev(mdev))
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005151 return 0;
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005152
Philipp Reisnerb411b362009-09-25 16:07:19 -07005153 drbd_rs_complete_io(mdev, sector);
5154 dec_rs_pending(mdev);
5155
Lars Ellenbergea5442a2010-11-05 09:48:01 +01005156 --mdev->ov_left;
5157
5158 /* let's advance progress step marks only for every other megabyte */
5159 if ((mdev->ov_left & 0x200) == 0x200)
5160 drbd_advance_rs_marks(mdev, mdev->ov_left);
5161
5162 if (mdev->ov_left == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07005163 w = kmalloc(sizeof(*w), GFP_NOIO);
5164 if (w) {
5165 w->cb = w_ov_finished;
Philipp Reisnera21e9292011-02-08 15:08:49 +01005166 w->mdev = mdev;
Lars Ellenbergd5b27b02011-11-14 15:42:37 +01005167 drbd_queue_work(&mdev->tconn->sender_work, w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005168 } else {
5169 dev_err(DEV, "kmalloc(w) failed.");
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005170 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005171 drbd_resync_finished(mdev);
5172 }
5173 }
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005174 put_ldev(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005175 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005176}
5177
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005178static int got_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner0ced55a2010-04-30 15:26:20 +02005179{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005180 return 0;
Philipp Reisner0ced55a2010-04-30 15:26:20 +02005181}
5182
Andreas Gruenbachera990be42011-04-06 17:56:48 +02005183static int tconn_finish_peer_reqs(struct drbd_tconn *tconn)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005184{
Philipp Reisner082a3432011-03-15 16:05:42 +01005185 struct drbd_conf *mdev;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005186 int vnr, not_empty = 0;
Philipp Reisner32862ec2011-02-08 16:41:01 +01005187
5188 do {
5189 clear_bit(SIGNAL_ASENDER, &tconn->flags);
5190 flush_signals(current);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005191
5192 rcu_read_lock();
5193 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
5194 kref_get(&mdev->kref);
5195 rcu_read_unlock();
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07005196 if (drbd_finish_peer_reqs(mdev)) {
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005197 kref_put(&mdev->kref, &drbd_minor_destroy);
5198 return 1;
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07005199 }
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005200 kref_put(&mdev->kref, &drbd_minor_destroy);
5201 rcu_read_lock();
Philipp Reisner082a3432011-03-15 16:05:42 +01005202 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005203 set_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisner082a3432011-03-15 16:05:42 +01005204
5205 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005206 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisner082a3432011-03-15 16:05:42 +01005207 not_empty = !list_empty(&mdev->done_ee);
5208 if (not_empty)
5209 break;
5210 }
5211 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005212 rcu_read_unlock();
Philipp Reisner32862ec2011-02-08 16:41:01 +01005213 } while (not_empty);
5214
5215 return 0;
5216}
5217
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005218struct asender_cmd {
5219 size_t pkt_size;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005220 int (*fn)(struct drbd_tconn *tconn, struct packet_info *);
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005221};
5222
5223static struct asender_cmd asender_tbl[] = {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005224 [P_PING] = { 0, got_Ping },
5225 [P_PING_ACK] = { 0, got_PingAck },
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005226 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5227 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5228 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02005229 [P_SUPERSEDED] = { sizeof(struct p_block_ack), got_BlockAck },
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005230 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
5231 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
5232 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply },
5233 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
5234 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
5235 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
5236 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
5237 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
5238 [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply },
5239 [P_CONN_ST_CHG_REPLY]={ sizeof(struct p_req_state_reply), got_conn_RqSReply },
5240 [P_RETRY_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005241};
5242
Philipp Reisnerb411b362009-09-25 16:07:19 -07005243int drbd_asender(struct drbd_thread *thi)
5244{
Philipp Reisner392c8802011-02-09 10:33:31 +01005245 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005246 struct asender_cmd *cmd = NULL;
Philipp Reisner77351055b2011-02-07 17:24:26 +01005247 struct packet_info pi;
Philipp Reisner257d0af2011-01-26 12:15:29 +01005248 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005249 void *buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005250 int received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005251 unsigned int header_size = drbd_header_size(tconn);
5252 int expect = header_size;
Philipp Reisner44ed1672011-04-19 17:10:19 +02005253 bool ping_timeout_active = false;
5254 struct net_conf *nc;
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005255 int ping_timeo, tcp_cork, ping_int;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005256
Philipp Reisnerb411b362009-09-25 16:07:19 -07005257 current->policy = SCHED_RR; /* Make this a realtime task! */
5258 current->rt_priority = 2; /* more important than all other tasks */
5259
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01005260 while (get_t_state(thi) == RUNNING) {
Philipp Reisner80822282011-02-08 12:46:30 +01005261 drbd_thread_current_set_cpu(thi);
Philipp Reisner44ed1672011-04-19 17:10:19 +02005262
5263 rcu_read_lock();
5264 nc = rcu_dereference(tconn->net_conf);
5265 ping_timeo = nc->ping_timeo;
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005266 tcp_cork = nc->tcp_cork;
Philipp Reisner44ed1672011-04-19 17:10:19 +02005267 ping_int = nc->ping_int;
5268 rcu_read_unlock();
5269
Philipp Reisner32862ec2011-02-08 16:41:01 +01005270 if (test_and_clear_bit(SEND_PING, &tconn->flags)) {
Andreas Gruenbachera17647a2011-04-01 12:49:42 +02005271 if (drbd_send_ping(tconn)) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005272 conn_err(tconn, "drbd_send_ping has failed\n");
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01005273 goto reconnect;
5274 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02005275 tconn->meta.socket->sk->sk_rcvtimeo = ping_timeo * HZ / 10;
5276 ping_timeout_active = true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005277 }
5278
Philipp Reisner32862ec2011-02-08 16:41:01 +01005279 /* TODO: conditionally cork; it may hurt latency if we cork without
5280 much to send */
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005281 if (tcp_cork)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005282 drbd_tcp_cork(tconn->meta.socket);
Andreas Gruenbachera990be42011-04-06 17:56:48 +02005283 if (tconn_finish_peer_reqs(tconn)) {
5284 conn_err(tconn, "tconn_finish_peer_reqs() failed\n");
Philipp Reisner32862ec2011-02-08 16:41:01 +01005285 goto reconnect;
Philipp Reisner082a3432011-03-15 16:05:42 +01005286 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005287 /* but unconditionally uncork unless disabled */
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005288 if (tcp_cork)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005289 drbd_tcp_uncork(tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005290
5291 /* short circuit, recv_msg would return EINTR anyways. */
5292 if (signal_pending(current))
5293 continue;
5294
Philipp Reisner32862ec2011-02-08 16:41:01 +01005295 rv = drbd_recv_short(tconn->meta.socket, buf, expect-received, 0);
5296 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005297
5298 flush_signals(current);
5299
5300 /* Note:
5301 * -EINTR (on meta) we got a signal
5302 * -EAGAIN (on meta) rcvtimeo expired
5303 * -ECONNRESET other side closed the connection
5304 * -ERESTARTSYS (on data) we got a signal
5305 * rv < 0 other than above: unexpected error!
5306 * rv == expected: full header or command
5307 * rv < expected: "woken" by signal during receive
5308 * rv == 0 : "connection shut down by peer"
5309 */
5310 if (likely(rv > 0)) {
5311 received += rv;
5312 buf += rv;
5313 } else if (rv == 0) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005314 conn_err(tconn, "meta connection shut down by peer.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005315 goto reconnect;
5316 } else if (rv == -EAGAIN) {
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02005317 /* If the data socket received something meanwhile,
5318 * that is good enough: peer is still alive. */
Philipp Reisner32862ec2011-02-08 16:41:01 +01005319 if (time_after(tconn->last_received,
5320 jiffies - tconn->meta.socket->sk->sk_rcvtimeo))
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02005321 continue;
Lars Ellenbergf36af182011-03-09 22:44:55 +01005322 if (ping_timeout_active) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005323 conn_err(tconn, "PingAck did not arrive in time.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005324 goto reconnect;
5325 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005326 set_bit(SEND_PING, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005327 continue;
5328 } else if (rv == -EINTR) {
5329 continue;
5330 } else {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005331 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005332 goto reconnect;
5333 }
5334
5335 if (received == expect && cmd == NULL) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005336 if (decode_header(tconn, tconn->meta.rbuf, &pi))
Philipp Reisnerb411b362009-09-25 16:07:19 -07005337 goto reconnect;
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005338 cmd = &asender_tbl[pi.cmd];
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005339 if (pi.cmd >= ARRAY_SIZE(asender_tbl) || !cmd->fn) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02005340 conn_err(tconn, "Unexpected meta packet %s (0x%04x)\n",
5341 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005342 goto disconnect;
5343 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005344 expect = header_size + cmd->pkt_size;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005345 if (pi.size != expect - header_size) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005346 conn_err(tconn, "Wrong packet size on meta (c: %d, l: %d)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01005347 pi.cmd, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005348 goto reconnect;
Philipp Reisner257d0af2011-01-26 12:15:29 +01005349 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005350 }
5351 if (received == expect) {
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005352 bool err;
Philipp Reisnera4fbda82011-03-16 11:13:17 +01005353
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005354 err = cmd->fn(tconn, &pi);
5355 if (err) {
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005356 conn_err(tconn, "%pf failed\n", cmd->fn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005357 goto reconnect;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005358 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005359
Philipp Reisnera4fbda82011-03-16 11:13:17 +01005360 tconn->last_received = jiffies;
5361
Philipp Reisner44ed1672011-04-19 17:10:19 +02005362 if (cmd == &asender_tbl[P_PING_ACK]) {
5363 /* restore idle timeout */
5364 tconn->meta.socket->sk->sk_rcvtimeo = ping_int * HZ;
5365 ping_timeout_active = false;
5366 }
Lars Ellenbergf36af182011-03-09 22:44:55 +01005367
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005368 buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005369 received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005370 expect = header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005371 cmd = NULL;
5372 }
5373 }
5374
5375 if (0) {
5376reconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01005377 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005378 }
5379 if (0) {
5380disconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01005381 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005382 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005383 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005384
Philipp Reisner32862ec2011-02-08 16:41:01 +01005385 conn_info(tconn, "asender terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005386
5387 return 0;
5388}