blob: a9eccfc6079b02e6fb2f9838a1728b76c9599582 [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 Ellenberga73ff322012-06-25 19:15:38 +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 Ellenberga73ff322012-06-25 19:15:38 +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 Ellenberga73ff322012-06-25 19:15:38 +0200350 if (data_size) {
Lars Ellenberg81a35372012-07-30 09:00:54 +0200351 page = drbd_alloc_pages(mdev, nr_pages, (gfp_mask & __GFP_WAIT));
Lars Ellenberga73ff322012-06-25 19:15:38 +0200352 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;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700361
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
Philipp Reisnerb411b362009-09-25 16:07:19 -0700409/*
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200410 * 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{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700493 int rv;
494
Philipp Reisner1393b592012-09-03 14:04:23 +0200495 rv = drbd_recv_short(tconn->data.socket, buf, size, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700496
Philipp Reisnerdbd08202012-08-17 16:55:47 +0200497 if (rv < 0) {
498 if (rv == -ECONNRESET)
Philipp Reisner155522d2012-08-08 21:19:09 +0200499 conn_info(tconn, "sock was reset by peer\n");
Philipp Reisnerdbd08202012-08-17 16:55:47 +0200500 else if (rv != -ERESTARTSYS)
Philipp Reisner155522d2012-08-08 21:19:09 +0200501 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerdbd08202012-08-17 16:55:47 +0200502 } else if (rv == 0) {
Philipp Reisnerb66623e2012-08-08 21:19:09 +0200503 if (test_bit(DISCONNECT_SENT, &tconn->flags)) {
504 long t;
505 rcu_read_lock();
506 t = rcu_dereference(tconn->net_conf)->ping_timeo * HZ/10;
507 rcu_read_unlock();
508
509 t = wait_event_timeout(tconn->ping_wait, tconn->cstate < C_WF_REPORT_PARAMS, t);
510
Philipp Reisner599377a2012-08-17 14:50:22 +0200511 if (t)
512 goto out;
513 }
Philipp Reisnerb66623e2012-08-08 21:19:09 +0200514 conn_info(tconn, "sock was shut down by peer\n");
Philipp Reisner599377a2012-08-17 14:50:22 +0200515 }
516
Philipp Reisnerb411b362009-09-25 16:07:19 -0700517 if (rv != size)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100518 conn_request_state(tconn, NS(conn, C_BROKEN_PIPE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700519
Philipp Reisner599377a2012-08-17 14:50:22 +0200520out:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700521 return rv;
522}
523
Andreas Gruenbacherc6967742011-03-17 17:15:20 +0100524static int drbd_recv_all(struct drbd_tconn *tconn, void *buf, size_t size)
525{
526 int err;
527
528 err = drbd_recv(tconn, buf, size);
529 if (err != size) {
530 if (err >= 0)
531 err = -EIO;
532 } else
533 err = 0;
534 return err;
535}
536
Andreas Gruenbachera5c31902011-03-24 03:28:04 +0100537static int drbd_recv_all_warn(struct drbd_tconn *tconn, void *buf, size_t size)
538{
539 int err;
540
541 err = drbd_recv_all(tconn, buf, size);
542 if (err && !signal_pending(current))
543 conn_warn(tconn, "short read (expected size %d)\n", (int)size);
544 return err;
545}
546
Lars Ellenberg5dbf1672010-05-25 16:18:01 +0200547/* quoting tcp(7):
548 * On individual connections, the socket buffer size must be set prior to the
549 * listen(2) or connect(2) calls in order to have it take effect.
550 * This is our wrapper to do so.
551 */
552static void drbd_setbufsize(struct socket *sock, unsigned int snd,
553 unsigned int rcv)
554{
555 /* open coded SO_SNDBUF, SO_RCVBUF */
556 if (snd) {
557 sock->sk->sk_sndbuf = snd;
558 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
559 }
560 if (rcv) {
561 sock->sk->sk_rcvbuf = rcv;
562 sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
563 }
564}
565
Philipp Reisnereac3e992011-02-07 14:05:07 +0100566static struct socket *drbd_try_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700567{
568 const char *what;
569 struct socket *sock;
570 struct sockaddr_in6 src_in6;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200571 struct sockaddr_in6 peer_in6;
572 struct net_conf *nc;
573 int err, peer_addr_len, my_addr_len;
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200574 int sndbuf_size, rcvbuf_size, connect_int;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700575 int disconnect_on_error = 1;
576
Philipp Reisner44ed1672011-04-19 17:10:19 +0200577 rcu_read_lock();
578 nc = rcu_dereference(tconn->net_conf);
579 if (!nc) {
580 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700581 return NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200582 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200583 sndbuf_size = nc->sndbuf_size;
584 rcvbuf_size = nc->rcvbuf_size;
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200585 connect_int = nc->connect_int;
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200586 rcu_read_unlock();
Philipp Reisner44ed1672011-04-19 17:10:19 +0200587
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200588 my_addr_len = min_t(int, tconn->my_addr_len, sizeof(src_in6));
589 memcpy(&src_in6, &tconn->my_addr, my_addr_len);
Philipp Reisner44ed1672011-04-19 17:10:19 +0200590
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200591 if (((struct sockaddr *)&tconn->my_addr)->sa_family == AF_INET6)
Philipp Reisner44ed1672011-04-19 17:10:19 +0200592 src_in6.sin6_port = 0;
593 else
594 ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */
595
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200596 peer_addr_len = min_t(int, tconn->peer_addr_len, sizeof(src_in6));
597 memcpy(&peer_in6, &tconn->peer_addr, peer_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700598
599 what = "sock_create_kern";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200600 err = sock_create_kern(((struct sockaddr *)&src_in6)->sa_family,
601 SOCK_STREAM, IPPROTO_TCP, &sock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700602 if (err < 0) {
603 sock = NULL;
604 goto out;
605 }
606
607 sock->sk->sk_rcvtimeo =
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200608 sock->sk->sk_sndtimeo = connect_int * HZ;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200609 drbd_setbufsize(sock, sndbuf_size, rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700610
611 /* explicitly bind to the configured IP as source IP
612 * for the outgoing connections.
613 * This is needed for multihomed hosts and to be
614 * able to use lo: interfaces for drbd.
615 * Make sure to use 0 as port number, so linux selects
616 * a free one dynamically.
617 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700618 what = "bind before connect";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200619 err = sock->ops->bind(sock, (struct sockaddr *) &src_in6, my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700620 if (err < 0)
621 goto out;
622
623 /* connect may fail, peer not yet available.
624 * stay C_WF_CONNECTION, don't go Disconnecting! */
625 disconnect_on_error = 0;
626 what = "connect";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200627 err = sock->ops->connect(sock, (struct sockaddr *) &peer_in6, peer_addr_len, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700628
629out:
630 if (err < 0) {
631 if (sock) {
632 sock_release(sock);
633 sock = NULL;
634 }
635 switch (-err) {
636 /* timeout, busy, signal pending */
637 case ETIMEDOUT: case EAGAIN: case EINPROGRESS:
638 case EINTR: case ERESTARTSYS:
639 /* peer not (yet) available, network problem */
640 case ECONNREFUSED: case ENETUNREACH:
641 case EHOSTDOWN: case EHOSTUNREACH:
642 disconnect_on_error = 0;
643 break;
644 default:
Philipp Reisnereac3e992011-02-07 14:05:07 +0100645 conn_err(tconn, "%s failed, err = %d\n", what, err);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700646 }
647 if (disconnect_on_error)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100648 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700649 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200650
Philipp Reisnerb411b362009-09-25 16:07:19 -0700651 return sock;
652}
653
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200654struct accept_wait_data {
655 struct drbd_tconn *tconn;
656 struct socket *s_listen;
657 struct completion door_bell;
658 void (*original_sk_state_change)(struct sock *sk);
659
660};
661
Andreas Gruenbacher715306f2012-08-10 17:00:30 +0200662static void drbd_incoming_connection(struct sock *sk)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700663{
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200664 struct accept_wait_data *ad = sk->sk_user_data;
Andreas Gruenbacher715306f2012-08-10 17:00:30 +0200665 void (*state_change)(struct sock *sk);
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200666
Andreas Gruenbacher715306f2012-08-10 17:00:30 +0200667 state_change = ad->original_sk_state_change;
668 if (sk->sk_state == TCP_ESTABLISHED)
669 complete(&ad->door_bell);
670 state_change(sk);
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200671}
672
673static int prepare_listen_socket(struct drbd_tconn *tconn, struct accept_wait_data *ad)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700674{
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200675 int err, sndbuf_size, rcvbuf_size, my_addr_len;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200676 struct sockaddr_in6 my_addr;
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200677 struct socket *s_listen;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200678 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700679 const char *what;
680
Philipp Reisner44ed1672011-04-19 17:10:19 +0200681 rcu_read_lock();
682 nc = rcu_dereference(tconn->net_conf);
683 if (!nc) {
684 rcu_read_unlock();
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200685 return -EIO;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200686 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200687 sndbuf_size = nc->sndbuf_size;
688 rcvbuf_size = nc->rcvbuf_size;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200689 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700690
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200691 my_addr_len = min_t(int, tconn->my_addr_len, sizeof(struct sockaddr_in6));
692 memcpy(&my_addr, &tconn->my_addr, my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700693
694 what = "sock_create_kern";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200695 err = sock_create_kern(((struct sockaddr *)&my_addr)->sa_family,
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200696 SOCK_STREAM, IPPROTO_TCP, &s_listen);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700697 if (err) {
698 s_listen = NULL;
699 goto out;
700 }
701
Philipp Reisner98683652012-11-09 14:18:43 +0100702 s_listen->sk->sk_reuse = SK_CAN_REUSE; /* SO_REUSEADDR */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200703 drbd_setbufsize(s_listen, sndbuf_size, rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700704
705 what = "bind before listen";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200706 err = s_listen->ops->bind(s_listen, (struct sockaddr *)&my_addr, my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700707 if (err < 0)
708 goto out;
709
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200710 ad->s_listen = s_listen;
711 write_lock_bh(&s_listen->sk->sk_callback_lock);
712 ad->original_sk_state_change = s_listen->sk->sk_state_change;
Andreas Gruenbacher715306f2012-08-10 17:00:30 +0200713 s_listen->sk->sk_state_change = drbd_incoming_connection;
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200714 s_listen->sk->sk_user_data = ad;
715 write_unlock_bh(&s_listen->sk->sk_callback_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700716
Philipp Reisner2820fd32012-07-12 10:22:48 +0200717 what = "listen";
718 err = s_listen->ops->listen(s_listen, 5);
719 if (err < 0)
720 goto out;
721
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200722 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700723out:
724 if (s_listen)
725 sock_release(s_listen);
726 if (err < 0) {
727 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200728 conn_err(tconn, "%s failed, err = %d\n", what, err);
729 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700730 }
731 }
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200732
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200733 return -EIO;
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200734}
735
Andreas Gruenbacher715306f2012-08-10 17:00:30 +0200736static void unregister_state_change(struct sock *sk, struct accept_wait_data *ad)
737{
738 write_lock_bh(&sk->sk_callback_lock);
739 sk->sk_state_change = ad->original_sk_state_change;
740 sk->sk_user_data = NULL;
741 write_unlock_bh(&sk->sk_callback_lock);
742}
743
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200744static struct socket *drbd_wait_for_connect(struct drbd_tconn *tconn, struct accept_wait_data *ad)
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200745{
746 int timeo, connect_int, err = 0;
747 struct socket *s_estab = NULL;
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200748 struct net_conf *nc;
749
750 rcu_read_lock();
751 nc = rcu_dereference(tconn->net_conf);
752 if (!nc) {
753 rcu_read_unlock();
754 return NULL;
755 }
756 connect_int = nc->connect_int;
757 rcu_read_unlock();
758
759 timeo = connect_int * HZ;
760 timeo += (random32() & 1) ? timeo / 7 : -timeo / 7; /* 28.5% random jitter */
761
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200762 err = wait_for_completion_interruptible_timeout(&ad->door_bell, timeo);
763 if (err <= 0)
764 return NULL;
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200765
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200766 err = kernel_accept(ad->s_listen, &s_estab, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700767 if (err < 0) {
768 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
Philipp Reisner1f3e5092012-07-12 11:08:34 +0200769 conn_err(tconn, "accept failed, err = %d\n", err);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100770 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700771 }
772 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700773
Andreas Gruenbacher715306f2012-08-10 17:00:30 +0200774 if (s_estab)
775 unregister_state_change(s_estab->sk, ad);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700776
777 return s_estab;
778}
779
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200780static int decode_header(struct drbd_tconn *, void *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700781
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200782static int send_first_packet(struct drbd_tconn *tconn, struct drbd_socket *sock,
783 enum drbd_packet cmd)
784{
785 if (!conn_prepare_command(tconn, sock))
786 return -EIO;
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200787 return conn_send_command(tconn, sock, cmd, 0, NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700788}
789
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200790static int receive_first_packet(struct drbd_tconn *tconn, struct socket *sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700791{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200792 unsigned int header_size = drbd_header_size(tconn);
793 struct packet_info pi;
794 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700795
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200796 err = drbd_recv_short(sock, tconn->data.rbuf, header_size, 0);
797 if (err != header_size) {
798 if (err >= 0)
799 err = -EIO;
800 return err;
801 }
802 err = decode_header(tconn, tconn->data.rbuf, &pi);
803 if (err)
804 return err;
805 return pi.cmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700806}
807
808/**
809 * drbd_socket_okay() - Free the socket if its connection is not okay
Philipp Reisnerb411b362009-09-25 16:07:19 -0700810 * @sock: pointer to the pointer to the socket.
811 */
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100812static int drbd_socket_okay(struct socket **sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700813{
814 int rr;
815 char tb[4];
816
817 if (!*sock)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100818 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700819
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100820 rr = drbd_recv_short(*sock, tb, 4, MSG_DONTWAIT | MSG_PEEK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700821
822 if (rr > 0 || rr == -EAGAIN) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100823 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700824 } else {
825 sock_release(*sock);
826 *sock = NULL;
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100827 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700828 }
829}
Philipp Reisner2325eb62011-03-15 16:56:18 +0100830/* Gets called if a connection is established, or if a new minor gets created
831 in a connection */
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200832int drbd_connected(struct drbd_conf *mdev)
Philipp Reisner907599e2011-02-08 11:25:37 +0100833{
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100834 int err;
Philipp Reisner907599e2011-02-08 11:25:37 +0100835
836 atomic_set(&mdev->packet_seq, 0);
837 mdev->peer_seq = 0;
838
Philipp Reisner8410da82011-02-11 20:11:10 +0100839 mdev->state_mutex = mdev->tconn->agreed_pro_version < 100 ?
840 &mdev->tconn->cstate_mutex :
841 &mdev->own_state_mutex;
842
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100843 err = drbd_send_sync_param(mdev);
844 if (!err)
845 err = drbd_send_sizes(mdev, 0, 0);
846 if (!err)
847 err = drbd_send_uuids(mdev);
848 if (!err)
Philipp Reisner43de7c82011-11-10 13:16:13 +0100849 err = drbd_send_current_state(mdev);
Philipp Reisner907599e2011-02-08 11:25:37 +0100850 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
851 clear_bit(RESIZE_PENDING, &mdev->flags);
Philipp Reisner8b924f12011-03-01 11:08:28 +0100852 mod_timer(&mdev->request_timer, jiffies + HZ); /* just start it here. */
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100853 return err;
Philipp Reisner907599e2011-02-08 11:25:37 +0100854}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700855
856/*
857 * return values:
858 * 1 yes, we have a valid connection
859 * 0 oops, did not work out, please try again
860 * -1 peer talks different language,
861 * no point in trying again, please go standalone.
862 * -2 We do not have a network config...
863 */
Philipp Reisner81fa2e62011-05-04 15:10:30 +0200864static int conn_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700865{
Philipp Reisner7da35862011-12-19 22:42:56 +0100866 struct drbd_socket sock, msock;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200867 struct drbd_conf *mdev;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200868 struct net_conf *nc;
Philipp Reisner92f14952012-08-01 11:41:01 +0200869 int vnr, timeout, h, ok;
Philipp Reisner08b165b2011-09-05 16:22:33 +0200870 bool discard_my_data;
Philipp Reisner197296f2012-03-26 16:47:11 +0200871 enum drbd_state_rv rv;
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200872 struct accept_wait_data ad = {
873 .tconn = tconn,
874 .door_bell = COMPLETION_INITIALIZER_ONSTACK(ad.door_bell),
875 };
Philipp Reisnerb411b362009-09-25 16:07:19 -0700876
Philipp Reisnerb66623e2012-08-08 21:19:09 +0200877 clear_bit(DISCONNECT_SENT, &tconn->flags);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100878 if (conn_request_state(tconn, NS(conn, C_WF_CONNECTION), CS_VERBOSE) < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700879 return -2;
880
Philipp Reisner7da35862011-12-19 22:42:56 +0100881 mutex_init(&sock.mutex);
882 sock.sbuf = tconn->data.sbuf;
883 sock.rbuf = tconn->data.rbuf;
884 sock.socket = NULL;
885 mutex_init(&msock.mutex);
886 msock.sbuf = tconn->meta.sbuf;
887 msock.rbuf = tconn->meta.rbuf;
888 msock.socket = NULL;
889
Andreas Gruenbacher0916e0e2011-03-21 14:10:15 +0100890 /* Assume that the peer only understands protocol 80 until we know better. */
891 tconn->agreed_pro_version = 80;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700892
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200893 if (prepare_listen_socket(tconn, &ad))
894 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700895
896 do {
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200897 struct socket *s;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700898
Philipp Reisner92f14952012-08-01 11:41:01 +0200899 s = drbd_try_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700900 if (s) {
Philipp Reisner7da35862011-12-19 22:42:56 +0100901 if (!sock.socket) {
902 sock.socket = s;
903 send_first_packet(tconn, &sock, P_INITIAL_DATA);
904 } else if (!msock.socket) {
Lars Ellenberg427c0432012-08-01 12:43:01 +0200905 clear_bit(RESOLVE_CONFLICTS, &tconn->flags);
Philipp Reisner7da35862011-12-19 22:42:56 +0100906 msock.socket = s;
907 send_first_packet(tconn, &msock, P_INITIAL_META);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700908 } else {
Philipp Reisner81fa2e62011-05-04 15:10:30 +0200909 conn_err(tconn, "Logic error in conn_connect()\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700910 goto out_release_sockets;
911 }
912 }
913
Philipp Reisner7da35862011-12-19 22:42:56 +0100914 if (sock.socket && msock.socket) {
915 rcu_read_lock();
916 nc = rcu_dereference(tconn->net_conf);
917 timeout = nc->ping_timeo * HZ / 10;
918 rcu_read_unlock();
919 schedule_timeout_interruptible(timeout);
920 ok = drbd_socket_okay(&sock.socket);
921 ok = drbd_socket_okay(&msock.socket) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700922 if (ok)
923 break;
924 }
925
926retry:
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200927 s = drbd_wait_for_connect(tconn, &ad);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700928 if (s) {
Philipp Reisner92f14952012-08-01 11:41:01 +0200929 int fp = receive_first_packet(tconn, s);
Philipp Reisner7da35862011-12-19 22:42:56 +0100930 drbd_socket_okay(&sock.socket);
931 drbd_socket_okay(&msock.socket);
Philipp Reisner92f14952012-08-01 11:41:01 +0200932 switch (fp) {
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200933 case P_INITIAL_DATA:
Philipp Reisner7da35862011-12-19 22:42:56 +0100934 if (sock.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100935 conn_warn(tconn, "initial packet S crossed\n");
Philipp Reisner7da35862011-12-19 22:42:56 +0100936 sock_release(sock.socket);
Philipp Reisner80c6eed2012-08-01 14:53:39 +0200937 sock.socket = s;
938 goto randomize;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700939 }
Philipp Reisner7da35862011-12-19 22:42:56 +0100940 sock.socket = s;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700941 break;
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200942 case P_INITIAL_META:
Lars Ellenberg427c0432012-08-01 12:43:01 +0200943 set_bit(RESOLVE_CONFLICTS, &tconn->flags);
Philipp Reisner7da35862011-12-19 22:42:56 +0100944 if (msock.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100945 conn_warn(tconn, "initial packet M crossed\n");
Philipp Reisner7da35862011-12-19 22:42:56 +0100946 sock_release(msock.socket);
Philipp Reisner80c6eed2012-08-01 14:53:39 +0200947 msock.socket = s;
948 goto randomize;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700949 }
Philipp Reisner7da35862011-12-19 22:42:56 +0100950 msock.socket = s;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700951 break;
952 default:
Philipp Reisner907599e2011-02-08 11:25:37 +0100953 conn_warn(tconn, "Error receiving initial packet\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700954 sock_release(s);
Philipp Reisner80c6eed2012-08-01 14:53:39 +0200955randomize:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700956 if (random32() & 1)
957 goto retry;
958 }
959 }
960
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100961 if (tconn->cstate <= C_DISCONNECTING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700962 goto out_release_sockets;
963 if (signal_pending(current)) {
964 flush_signals(current);
965 smp_rmb();
Philipp Reisner907599e2011-02-08 11:25:37 +0100966 if (get_t_state(&tconn->receiver) == EXITING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700967 goto out_release_sockets;
968 }
969
Philipp Reisnerb666dbf2012-07-26 14:12:59 +0200970 ok = drbd_socket_okay(&sock.socket);
971 ok = drbd_socket_okay(&msock.socket) && ok;
972 } while (!ok);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700973
Philipp Reisner7a426fd2012-07-12 14:22:37 +0200974 if (ad.s_listen)
975 sock_release(ad.s_listen);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700976
Philipp Reisner98683652012-11-09 14:18:43 +0100977 sock.socket->sk->sk_reuse = SK_CAN_REUSE; /* SO_REUSEADDR */
978 msock.socket->sk->sk_reuse = SK_CAN_REUSE; /* SO_REUSEADDR */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700979
Philipp Reisner7da35862011-12-19 22:42:56 +0100980 sock.socket->sk->sk_allocation = GFP_NOIO;
981 msock.socket->sk->sk_allocation = GFP_NOIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700982
Philipp Reisner7da35862011-12-19 22:42:56 +0100983 sock.socket->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
984 msock.socket->sk->sk_priority = TC_PRIO_INTERACTIVE;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700985
Philipp Reisnerb411b362009-09-25 16:07:19 -0700986 /* NOT YET ...
Philipp Reisner7da35862011-12-19 22:42:56 +0100987 * sock.socket->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
988 * sock.socket->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
Andreas Gruenbacher60381782011-03-28 17:05:50 +0200989 * first set it to the P_CONNECTION_FEATURES timeout,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700990 * which we set to 4x the configured ping_timeout. */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200991 rcu_read_lock();
992 nc = rcu_dereference(tconn->net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700993
Philipp Reisner7da35862011-12-19 22:42:56 +0100994 sock.socket->sk->sk_sndtimeo =
995 sock.socket->sk->sk_rcvtimeo = nc->ping_timeo*4*HZ/10;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200996
Philipp Reisner7da35862011-12-19 22:42:56 +0100997 msock.socket->sk->sk_rcvtimeo = nc->ping_int*HZ;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200998 timeout = nc->timeout * HZ / 10;
Philipp Reisner08b165b2011-09-05 16:22:33 +0200999 discard_my_data = nc->discard_my_data;
Philipp Reisner44ed1672011-04-19 17:10:19 +02001000 rcu_read_unlock();
1001
Philipp Reisner7da35862011-12-19 22:42:56 +01001002 msock.socket->sk->sk_sndtimeo = timeout;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001003
1004 /* we don't want delays.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001005 * we use TCP_CORK where appropriate, though */
Philipp Reisner7da35862011-12-19 22:42:56 +01001006 drbd_tcp_nodelay(sock.socket);
1007 drbd_tcp_nodelay(msock.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001008
Philipp Reisner7da35862011-12-19 22:42:56 +01001009 tconn->data.socket = sock.socket;
1010 tconn->meta.socket = msock.socket;
Philipp Reisner907599e2011-02-08 11:25:37 +01001011 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001012
Andreas Gruenbacher60381782011-03-28 17:05:50 +02001013 h = drbd_do_features(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001014 if (h <= 0)
1015 return h;
1016
Philipp Reisner907599e2011-02-08 11:25:37 +01001017 if (tconn->cram_hmac_tfm) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001018 /* drbd_request_state(mdev, NS(conn, WFAuth)); */
Philipp Reisner907599e2011-02-08 11:25:37 +01001019 switch (drbd_do_auth(tconn)) {
Johannes Thomab10d96c2010-01-07 16:02:50 +01001020 case -1:
Philipp Reisner907599e2011-02-08 11:25:37 +01001021 conn_err(tconn, "Authentication of peer failed\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001022 return -1;
Johannes Thomab10d96c2010-01-07 16:02:50 +01001023 case 0:
Philipp Reisner907599e2011-02-08 11:25:37 +01001024 conn_err(tconn, "Authentication of peer failed, trying again.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01001025 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001026 }
1027 }
1028
Philipp Reisner7da35862011-12-19 22:42:56 +01001029 tconn->data.socket->sk->sk_sndtimeo = timeout;
1030 tconn->data.socket->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001031
Andreas Gruenbacher387eb302011-03-16 01:05:37 +01001032 if (drbd_send_protocol(tconn) == -EOPNOTSUPP)
Philipp Reisner7e2455c2010-04-22 14:50:23 +02001033 return -1;
Philipp Reisner1e86ac42011-08-04 10:33:08 +02001034
Philipp Reisnera1096a62012-04-06 12:07:34 +02001035 set_bit(STATE_SENT, &tconn->flags);
Philipp Reisner197296f2012-03-26 16:47:11 +02001036
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001037 rcu_read_lock();
1038 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1039 kref_get(&mdev->kref);
Philipp Reisner13c76ab2012-11-22 17:06:00 +01001040 /* Prevent a race between resync-handshake and
1041 * being promoted to Primary.
1042 *
1043 * Grab and release the state mutex, so we know that any current
1044 * drbd_set_role() is finished, and any incoming drbd_set_role
1045 * will see the STATE_SENT flag, and wait for it to be cleared.
1046 */
1047 mutex_lock(mdev->state_mutex);
1048 mutex_unlock(mdev->state_mutex);
1049
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001050 rcu_read_unlock();
Philipp Reisner08b165b2011-09-05 16:22:33 +02001051
1052 if (discard_my_data)
1053 set_bit(DISCARD_MY_DATA, &mdev->flags);
1054 else
1055 clear_bit(DISCARD_MY_DATA, &mdev->flags);
1056
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001057 drbd_connected(mdev);
1058 kref_put(&mdev->kref, &drbd_minor_destroy);
1059 rcu_read_lock();
1060 }
1061 rcu_read_unlock();
1062
Philipp Reisnera1096a62012-04-06 12:07:34 +02001063 rv = conn_request_state(tconn, NS(conn, C_WF_REPORT_PARAMS), CS_VERBOSE);
Lars Ellenberged635cb02012-11-05 11:54:30 +01001064 if (rv < SS_SUCCESS || tconn->cstate != C_WF_REPORT_PARAMS) {
Philipp Reisnera1096a62012-04-06 12:07:34 +02001065 clear_bit(STATE_SENT, &tconn->flags);
Philipp Reisner1e86ac42011-08-04 10:33:08 +02001066 return 0;
Philipp Reisnera1096a62012-04-06 12:07:34 +02001067 }
Philipp Reisner1e86ac42011-08-04 10:33:08 +02001068
Philipp Reisner823bd832012-11-08 15:04:36 +01001069 drbd_thread_start(&tconn->asender);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001070
Philipp Reisner08b165b2011-09-05 16:22:33 +02001071 mutex_lock(&tconn->conf_update);
1072 /* The discard_my_data flag is a single-shot modifier to the next
1073 * connection attempt, the handshake of which is now well underway.
1074 * No need for rcu style copying of the whole struct
1075 * just to clear a single value. */
1076 tconn->net_conf->discard_my_data = 0;
1077 mutex_unlock(&tconn->conf_update);
1078
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07001079 return h;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001080
1081out_release_sockets:
Philipp Reisner7a426fd2012-07-12 14:22:37 +02001082 if (ad.s_listen)
1083 sock_release(ad.s_listen);
Philipp Reisner7da35862011-12-19 22:42:56 +01001084 if (sock.socket)
1085 sock_release(sock.socket);
1086 if (msock.socket)
1087 sock_release(msock.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001088 return -1;
1089}
1090
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001091static int decode_header(struct drbd_tconn *tconn, void *header, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001092{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001093 unsigned int header_size = drbd_header_size(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001094
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +02001095 if (header_size == sizeof(struct p_header100) &&
1096 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC_100)) {
1097 struct p_header100 *h = header;
1098 if (h->pad != 0) {
1099 conn_err(tconn, "Header padding is not zero\n");
1100 return -EINVAL;
1101 }
1102 pi->vnr = be16_to_cpu(h->volume);
1103 pi->cmd = be16_to_cpu(h->command);
1104 pi->size = be32_to_cpu(h->length);
1105 } else if (header_size == sizeof(struct p_header95) &&
1106 *(__be16 *)header == cpu_to_be16(DRBD_MAGIC_BIG)) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001107 struct p_header95 *h = header;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001108 pi->cmd = be16_to_cpu(h->command);
Andreas Gruenbacherb55d84b2011-03-22 13:17:47 +01001109 pi->size = be32_to_cpu(h->length);
1110 pi->vnr = 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001111 } else if (header_size == sizeof(struct p_header80) &&
1112 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC)) {
1113 struct p_header80 *h = header;
1114 pi->cmd = be16_to_cpu(h->command);
1115 pi->size = be16_to_cpu(h->length);
Philipp Reisner77351055b2011-02-07 17:24:26 +01001116 pi->vnr = 0;
Philipp Reisner02918be2010-08-20 14:35:10 +02001117 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001118 conn_err(tconn, "Wrong magic value 0x%08x in protocol version %d\n",
1119 be32_to_cpu(*(__be32 *)header),
1120 tconn->agreed_pro_version);
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001121 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001122 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001123 pi->data = header + header_size;
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001124 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001125}
1126
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001127static int drbd_recv_header(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner257d0af2011-01-26 12:15:29 +01001128{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001129 void *buffer = tconn->data.rbuf;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001130 int err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001131
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001132 err = drbd_recv_all_warn(tconn, buffer, drbd_header_size(tconn));
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001133 if (err)
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001134 return err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001135
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001136 err = decode_header(tconn, buffer, pi);
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001137 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001138
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001139 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001140}
1141
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001142static void drbd_flush(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001143{
1144 int rv;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001145 struct drbd_conf *mdev;
1146 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001147
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001148 if (tconn->write_ordering >= WO_bdev_flush) {
Lars Ellenberg615e0872011-11-17 14:32:12 +01001149 rcu_read_lock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001150 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Lars Ellenberg615e0872011-11-17 14:32:12 +01001151 if (!get_ldev(mdev))
1152 continue;
1153 kref_get(&mdev->kref);
1154 rcu_read_unlock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001155
Lars Ellenberg615e0872011-11-17 14:32:12 +01001156 rv = blkdev_issue_flush(mdev->ldev->backing_bdev,
1157 GFP_NOIO, NULL);
1158 if (rv) {
1159 dev_info(DEV, "local disk flush failed with status %d\n", rv);
1160 /* would rather check on EOPNOTSUPP, but that is not reliable.
1161 * don't try again for ANY return value != 0
1162 * if (rv == -EOPNOTSUPP) */
1163 drbd_bump_write_ordering(tconn, WO_drain_io);
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001164 }
Lars Ellenberg615e0872011-11-17 14:32:12 +01001165 put_ldev(mdev);
1166 kref_put(&mdev->kref, &drbd_minor_destroy);
1167
1168 rcu_read_lock();
1169 if (rv)
1170 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001171 }
Lars Ellenberg615e0872011-11-17 14:32:12 +01001172 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001173 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001174}
1175
1176/**
1177 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
1178 * @mdev: DRBD device.
1179 * @epoch: Epoch object.
1180 * @ev: Epoch event.
1181 */
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001182static enum finish_epoch drbd_may_finish_epoch(struct drbd_tconn *tconn,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001183 struct drbd_epoch *epoch,
1184 enum epoch_event ev)
1185{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001186 int epoch_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001187 struct drbd_epoch *next_epoch;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001188 enum finish_epoch rv = FE_STILL_LIVE;
1189
Philipp Reisner12038a32011-11-09 19:18:00 +01001190 spin_lock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001191 do {
1192 next_epoch = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001193
1194 epoch_size = atomic_read(&epoch->epoch_size);
1195
1196 switch (ev & ~EV_CLEANUP) {
1197 case EV_PUT:
1198 atomic_dec(&epoch->active);
1199 break;
1200 case EV_GOT_BARRIER_NR:
1201 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001202 break;
1203 case EV_BECAME_LAST:
1204 /* nothing to do*/
1205 break;
1206 }
1207
Philipp Reisnerb411b362009-09-25 16:07:19 -07001208 if (epoch_size != 0 &&
1209 atomic_read(&epoch->active) == 0 &&
Philipp Reisner80f9fd52011-07-18 15:45:15 +02001210 (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags) || ev & EV_CLEANUP)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001211 if (!(ev & EV_CLEANUP)) {
Philipp Reisner12038a32011-11-09 19:18:00 +01001212 spin_unlock(&tconn->epoch_lock);
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001213 drbd_send_b_ack(epoch->tconn, epoch->barrier_nr, epoch_size);
Philipp Reisner12038a32011-11-09 19:18:00 +01001214 spin_lock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001215 }
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001216#if 0
1217 /* FIXME: dec unacked on connection, once we have
1218 * something to count pending connection packets in. */
Philipp Reisner80f9fd52011-07-18 15:45:15 +02001219 if (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags))
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001220 dec_unacked(epoch->tconn);
1221#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -07001222
Philipp Reisner12038a32011-11-09 19:18:00 +01001223 if (tconn->current_epoch != epoch) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001224 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1225 list_del(&epoch->list);
1226 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
Philipp Reisner12038a32011-11-09 19:18:00 +01001227 tconn->epochs--;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001228 kfree(epoch);
1229
1230 if (rv == FE_STILL_LIVE)
1231 rv = FE_DESTROYED;
1232 } else {
1233 epoch->flags = 0;
1234 atomic_set(&epoch->epoch_size, 0);
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001235 /* atomic_set(&epoch->active, 0); is already zero */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001236 if (rv == FE_STILL_LIVE)
1237 rv = FE_RECYCLED;
1238 }
1239 }
1240
1241 if (!next_epoch)
1242 break;
1243
1244 epoch = next_epoch;
1245 } while (1);
1246
Philipp Reisner12038a32011-11-09 19:18:00 +01001247 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001248
Philipp Reisnerb411b362009-09-25 16:07:19 -07001249 return rv;
1250}
1251
1252/**
1253 * drbd_bump_write_ordering() - Fall back to an other write ordering method
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001254 * @tconn: DRBD connection.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001255 * @wo: Write ordering method to try.
1256 */
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001257void drbd_bump_write_ordering(struct drbd_tconn *tconn, enum write_ordering_e wo)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001258{
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001259 struct disk_conf *dc;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001260 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001261 enum write_ordering_e pwo;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001262 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001263 static char *write_ordering_str[] = {
1264 [WO_none] = "none",
1265 [WO_drain_io] = "drain",
1266 [WO_bdev_flush] = "flush",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001267 };
1268
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001269 pwo = tconn->write_ordering;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001270 wo = min(pwo, wo);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001271 rcu_read_lock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001272 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisner27eb13e2012-03-30 14:12:15 +02001273 if (!get_ldev_if_state(mdev, D_ATTACHING))
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001274 continue;
1275 dc = rcu_dereference(mdev->ldev->disk_conf);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001276
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001277 if (wo == WO_bdev_flush && !dc->disk_flushes)
1278 wo = WO_drain_io;
1279 if (wo == WO_drain_io && !dc->disk_drain)
1280 wo = WO_none;
1281 put_ldev(mdev);
1282 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001283 rcu_read_unlock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001284 tconn->write_ordering = wo;
1285 if (pwo != tconn->write_ordering || wo == WO_bdev_flush)
1286 conn_info(tconn, "Method to ensure write ordering: %s\n", write_ordering_str[tconn->write_ordering]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001287}
1288
1289/**
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001290 * drbd_submit_peer_request()
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001291 * @mdev: DRBD device.
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001292 * @peer_req: peer request
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001293 * @rw: flag field, see bio->bi_rw
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001294 *
1295 * May spread the pages to multiple bios,
1296 * depending on bio_add_page restrictions.
1297 *
1298 * Returns 0 if all bios have been submitted,
1299 * -ENOMEM if we could not allocate enough bios,
1300 * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a
1301 * single page to an empty bio (which should never happen and likely indicates
1302 * that the lower level IO stack is in some way broken). This has been observed
1303 * on certain Xen deployments.
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001304 */
1305/* TODO allocate from our own bio_set. */
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001306int drbd_submit_peer_request(struct drbd_conf *mdev,
1307 struct drbd_peer_request *peer_req,
1308 const unsigned rw, const int fault_type)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001309{
1310 struct bio *bios = NULL;
1311 struct bio *bio;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001312 struct page *page = peer_req->pages;
1313 sector_t sector = peer_req->i.sector;
1314 unsigned ds = peer_req->i.size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001315 unsigned n_bios = 0;
1316 unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001317 int err = -ENOMEM;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001318
1319 /* In most cases, we will only need one bio. But in case the lower
1320 * level restrictions happen to be different at this offset on this
1321 * side than those of the sending peer, we may need to submit the
Lars Ellenberg9476f392011-02-23 17:02:01 +01001322 * request in more than one bio.
1323 *
1324 * Plain bio_alloc is good enough here, this is no DRBD internally
1325 * generated bio, but a bio allocated on behalf of the peer.
1326 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001327next_bio:
1328 bio = bio_alloc(GFP_NOIO, nr_pages);
1329 if (!bio) {
1330 dev_err(DEV, "submit_ee: Allocation of a bio failed\n");
1331 goto fail;
1332 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001333 /* > peer_req->i.sector, unless this is the first bio */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001334 bio->bi_sector = sector;
1335 bio->bi_bdev = mdev->ldev->backing_bdev;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001336 bio->bi_rw = rw;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001337 bio->bi_private = peer_req;
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001338 bio->bi_end_io = drbd_peer_request_endio;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001339
1340 bio->bi_next = bios;
1341 bios = bio;
1342 ++n_bios;
1343
1344 page_chain_for_each(page) {
1345 unsigned len = min_t(unsigned, ds, PAGE_SIZE);
1346 if (!bio_add_page(bio, page, len, 0)) {
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001347 /* A single page must always be possible!
1348 * But in case it fails anyways,
1349 * we deal with it, and complain (below). */
1350 if (bio->bi_vcnt == 0) {
1351 dev_err(DEV,
1352 "bio_add_page failed for len=%u, "
1353 "bi_vcnt=0 (bi_sector=%llu)\n",
1354 len, (unsigned long long)bio->bi_sector);
1355 err = -ENOSPC;
1356 goto fail;
1357 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001358 goto next_bio;
1359 }
1360 ds -= len;
1361 sector += len >> 9;
1362 --nr_pages;
1363 }
1364 D_ASSERT(page == NULL);
1365 D_ASSERT(ds == 0);
1366
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001367 atomic_set(&peer_req->pending_bios, n_bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001368 do {
1369 bio = bios;
1370 bios = bios->bi_next;
1371 bio->bi_next = NULL;
1372
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001373 drbd_generic_make_request(mdev, fault_type, bio);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001374 } while (bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001375 return 0;
1376
1377fail:
1378 while (bios) {
1379 bio = bios;
1380 bios = bios->bi_next;
1381 bio_put(bio);
1382 }
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001383 return err;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001384}
1385
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001386static void drbd_remove_epoch_entry_interval(struct drbd_conf *mdev,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001387 struct drbd_peer_request *peer_req)
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001388{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001389 struct drbd_interval *i = &peer_req->i;
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001390
1391 drbd_remove_interval(&mdev->write_requests, i);
1392 drbd_clear_interval(i);
1393
Andreas Gruenbacher6c852be2011-02-04 15:38:52 +01001394 /* Wake up any processes waiting for this peer request to complete. */
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001395 if (i->waiting)
1396 wake_up(&mdev->misc_wait);
1397}
1398
Philipp Reisner77fede52011-11-10 21:19:11 +01001399void conn_wait_active_ee_empty(struct drbd_tconn *tconn)
1400{
1401 struct drbd_conf *mdev;
1402 int vnr;
1403
1404 rcu_read_lock();
1405 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1406 kref_get(&mdev->kref);
1407 rcu_read_unlock();
1408 drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
1409 kref_put(&mdev->kref, &drbd_minor_destroy);
1410 rcu_read_lock();
1411 }
1412 rcu_read_unlock();
1413}
1414
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001415static int receive_Barrier(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001416{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001417 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001418 struct p_barrier *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001419 struct drbd_epoch *epoch;
1420
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001421 /* FIXME these are unacked on connection,
1422 * not a specific (peer)device.
1423 */
Philipp Reisner12038a32011-11-09 19:18:00 +01001424 tconn->current_epoch->barrier_nr = p->barrier;
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001425 tconn->current_epoch->tconn = tconn;
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001426 rv = drbd_may_finish_epoch(tconn, tconn->current_epoch, EV_GOT_BARRIER_NR);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001427
1428 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
1429 * the activity log, which means it would not be resynced in case the
1430 * R_PRIMARY crashes now.
1431 * Therefore we must send the barrier_ack after the barrier request was
1432 * completed. */
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001433 switch (tconn->write_ordering) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001434 case WO_none:
1435 if (rv == FE_RECYCLED)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001436 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001437
1438 /* receiver context, in the writeout path of the other node.
1439 * avoid potential distributed deadlock */
1440 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1441 if (epoch)
1442 break;
1443 else
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001444 conn_warn(tconn, "Allocation of an epoch failed, slowing down\n");
Philipp Reisner2451fc32010-08-24 13:43:11 +02001445 /* Fall through */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001446
1447 case WO_bdev_flush:
1448 case WO_drain_io:
Philipp Reisner77fede52011-11-10 21:19:11 +01001449 conn_wait_active_ee_empty(tconn);
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001450 drbd_flush(tconn);
Philipp Reisner2451fc32010-08-24 13:43:11 +02001451
Philipp Reisner12038a32011-11-09 19:18:00 +01001452 if (atomic_read(&tconn->current_epoch->epoch_size)) {
Philipp Reisner2451fc32010-08-24 13:43:11 +02001453 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1454 if (epoch)
1455 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001456 }
1457
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001458 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001459 default:
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001460 conn_err(tconn, "Strangeness in tconn->write_ordering %d\n", tconn->write_ordering);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001461 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001462 }
1463
1464 epoch->flags = 0;
1465 atomic_set(&epoch->epoch_size, 0);
1466 atomic_set(&epoch->active, 0);
1467
Philipp Reisner12038a32011-11-09 19:18:00 +01001468 spin_lock(&tconn->epoch_lock);
1469 if (atomic_read(&tconn->current_epoch->epoch_size)) {
1470 list_add(&epoch->list, &tconn->current_epoch->list);
1471 tconn->current_epoch = epoch;
1472 tconn->epochs++;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001473 } else {
1474 /* The current_epoch got recycled while we allocated this one... */
1475 kfree(epoch);
1476 }
Philipp Reisner12038a32011-11-09 19:18:00 +01001477 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001478
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001479 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001480}
1481
1482/* used from receive_RSDataReply (recv_resync_read)
1483 * and from receive_Data */
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001484static struct drbd_peer_request *
1485read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector,
1486 int data_size) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001487{
Lars Ellenberg66660322010-04-06 12:15:04 +02001488 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001489 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001490 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001491 int dgs, ds, err;
Philipp Reisnera0638452011-01-19 14:31:32 +01001492 void *dig_in = mdev->tconn->int_dig_in;
1493 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001494 unsigned long *data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001495
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001496 dgs = 0;
1497 if (mdev->tconn->peer_integrity_tfm) {
1498 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001499 /*
1500 * FIXME: Receive the incoming digest into the receive buffer
1501 * here, together with its struct p_data?
1502 */
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001503 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1504 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001505 return NULL;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001506 data_size -= dgs;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001507 }
1508
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001509 if (!expect(IS_ALIGNED(data_size, 512)))
1510 return NULL;
1511 if (!expect(data_size <= DRBD_MAX_BIO_SIZE))
1512 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001513
Lars Ellenberg66660322010-04-06 12:15:04 +02001514 /* even though we trust out peer,
1515 * we sometimes have to double check. */
1516 if (sector + (data_size>>9) > capacity) {
Lars Ellenbergfdda6542011-01-24 15:11:01 +01001517 dev_err(DEV, "request from peer beyond end of local disk: "
1518 "capacity: %llus < sector: %llus + size: %u\n",
Lars Ellenberg66660322010-04-06 12:15:04 +02001519 (unsigned long long)capacity,
1520 (unsigned long long)sector, data_size);
1521 return NULL;
1522 }
1523
Philipp Reisnerb411b362009-09-25 16:07:19 -07001524 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1525 * "criss-cross" setup, that might cause write-out on some other DRBD,
1526 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02001527 peer_req = drbd_alloc_peer_req(mdev, id, sector, data_size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001528 if (!peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001529 return NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001530
Lars Ellenberga73ff322012-06-25 19:15:38 +02001531 if (!data_size)
Lars Ellenberg81a35372012-07-30 09:00:54 +02001532 return peer_req;
Lars Ellenberga73ff322012-06-25 19:15:38 +02001533
Philipp Reisnerb411b362009-09-25 16:07:19 -07001534 ds = data_size;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001535 page = peer_req->pages;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001536 page_chain_for_each(page) {
1537 unsigned len = min_t(int, ds, PAGE_SIZE);
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001538 data = kmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001539 err = drbd_recv_all_warn(mdev->tconn, data, len);
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +01001540 if (drbd_insert_fault(mdev, DRBD_FAULT_RECEIVE)) {
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001541 dev_err(DEV, "Fault injection: Corrupting data on receive\n");
1542 data[0] = data[0] ^ (unsigned long)-1;
1543 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001544 kunmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001545 if (err) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001546 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001547 return NULL;
1548 }
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001549 ds -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001550 }
1551
1552 if (dgs) {
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02001553 drbd_csum_ee(mdev, mdev->tconn->peer_integrity_tfm, peer_req, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001554 if (memcmp(dig_in, dig_vv, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001555 dev_err(DEV, "Digest integrity check FAILED: %llus +%u\n",
1556 (unsigned long long)sector, data_size);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001557 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001558 return NULL;
1559 }
1560 }
1561 mdev->recv_cnt += data_size>>9;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001562 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001563}
1564
1565/* drbd_drain_block() just takes a data block
1566 * out of the socket input buffer, and discards it.
1567 */
1568static int drbd_drain_block(struct drbd_conf *mdev, int data_size)
1569{
1570 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001571 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001572 void *data;
1573
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001574 if (!data_size)
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001575 return 0;
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001576
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +02001577 page = drbd_alloc_pages(mdev, 1, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001578
1579 data = kmap(page);
1580 while (data_size) {
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001581 unsigned int len = min_t(int, data_size, PAGE_SIZE);
1582
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001583 err = drbd_recv_all_warn(mdev->tconn, data, len);
1584 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001585 break;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001586 data_size -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001587 }
1588 kunmap(page);
Andreas Gruenbacher5cc287e2011-04-07 21:02:59 +02001589 drbd_free_pages(mdev, page, 0);
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001590 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001591}
1592
1593static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req,
1594 sector_t sector, int data_size)
1595{
1596 struct bio_vec *bvec;
1597 struct bio *bio;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001598 int dgs, err, i, expect;
Philipp Reisnera0638452011-01-19 14:31:32 +01001599 void *dig_in = mdev->tconn->int_dig_in;
1600 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001601
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001602 dgs = 0;
1603 if (mdev->tconn->peer_integrity_tfm) {
1604 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001605 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1606 if (err)
1607 return err;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001608 data_size -= dgs;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001609 }
1610
Philipp Reisnerb411b362009-09-25 16:07:19 -07001611 /* optimistically update recv_cnt. if receiving fails below,
1612 * we disconnect anyways, and counters will be reset. */
1613 mdev->recv_cnt += data_size>>9;
1614
1615 bio = req->master_bio;
1616 D_ASSERT(sector == bio->bi_sector);
1617
1618 bio_for_each_segment(bvec, bio, i) {
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001619 void *mapped = kmap(bvec->bv_page) + bvec->bv_offset;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001620 expect = min_t(int, data_size, bvec->bv_len);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001621 err = drbd_recv_all_warn(mdev->tconn, mapped, expect);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001622 kunmap(bvec->bv_page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001623 if (err)
1624 return err;
1625 data_size -= expect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001626 }
1627
1628 if (dgs) {
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02001629 drbd_csum_bio(mdev, mdev->tconn->peer_integrity_tfm, bio, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001630 if (memcmp(dig_in, dig_vv, dgs)) {
1631 dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n");
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001632 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001633 }
1634 }
1635
1636 D_ASSERT(data_size == 0);
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001637 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001638}
1639
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001640/*
1641 * e_end_resync_block() is called in asender context via
1642 * drbd_finish_peer_reqs().
1643 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001644static int e_end_resync_block(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001645{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001646 struct drbd_peer_request *peer_req =
1647 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001648 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001649 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001650 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001651
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001652 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001653
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001654 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1655 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001656 err = drbd_send_ack(mdev, P_RS_WRITE_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001657 } else {
1658 /* Record failure to sync */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001659 drbd_rs_failed_io(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001660
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001661 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001662 }
1663 dec_unacked(mdev);
1664
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001665 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001666}
1667
1668static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local)
1669{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001670 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001671
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001672 peer_req = read_in_block(mdev, ID_SYNCER, sector, data_size);
1673 if (!peer_req)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001674 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001675
1676 dec_rs_pending(mdev);
1677
Philipp Reisnerb411b362009-09-25 16:07:19 -07001678 inc_unacked(mdev);
1679 /* corresponding dec_unacked() in e_end_resync_block()
1680 * respective _drbd_clear_done_ee */
1681
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001682 peer_req->w.cb = e_end_resync_block;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001683
Philipp Reisner87eeee42011-01-19 14:16:30 +01001684 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001685 list_add(&peer_req->w.list, &mdev->sync_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001686 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001687
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001688 atomic_add(data_size >> 9, &mdev->rs_sect_ev);
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001689 if (drbd_submit_peer_request(mdev, peer_req, WRITE, DRBD_FAULT_RS_WR) == 0)
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001690 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001691
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001692 /* don't care for the reason here */
1693 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01001694 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001695 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001696 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001697
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001698 drbd_free_peer_req(mdev, peer_req);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001699fail:
1700 put_ldev(mdev);
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001701 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001702}
1703
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001704static struct drbd_request *
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001705find_request(struct drbd_conf *mdev, struct rb_root *root, u64 id,
1706 sector_t sector, bool missing_ok, const char *func)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001707{
1708 struct drbd_request *req;
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001709
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001710 /* Request object according to our peer */
1711 req = (struct drbd_request *)(unsigned long)id;
Andreas Gruenbacher5e472262011-01-27 14:42:51 +01001712 if (drbd_contains_interval(root, sector, &req->i) && req->i.local)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001713 return req;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001714 if (!missing_ok) {
Andreas Gruenbacher5af172e2011-07-15 09:43:23 +02001715 dev_err(DEV, "%s: failed to find request 0x%lx, sector %llus\n", func,
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001716 (unsigned long)id, (unsigned long long)sector);
1717 }
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001718 return NULL;
1719}
1720
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001721static int receive_DataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001722{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001723 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001724 struct drbd_request *req;
1725 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001726 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001727 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001728
1729 mdev = vnr_to_mdev(tconn, pi->vnr);
1730 if (!mdev)
1731 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001732
1733 sector = be64_to_cpu(p->sector);
1734
Philipp Reisner87eeee42011-01-19 14:16:30 +01001735 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001736 req = find_request(mdev, &mdev->read_requests, p->block_id, sector, false, __func__);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001737 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001738 if (unlikely(!req))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001739 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001740
Bart Van Assche24c48302011-05-21 18:32:29 +02001741 /* hlist_del(&req->collision) is done in _req_may_be_done, to avoid
Philipp Reisnerb411b362009-09-25 16:07:19 -07001742 * special casing it there for the various failure cases.
1743 * still no race with drbd_fail_pending_reads */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001744 err = recv_dless_read(mdev, req, sector, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001745 if (!err)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001746 req_mod(req, DATA_RECEIVED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001747 /* else: nothing. handled from drbd_disconnect...
1748 * I don't think we may complete this just yet
1749 * in case we are "on-disconnect: freeze" */
1750
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001751 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001752}
1753
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001754static int receive_RSDataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001755{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001756 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001757 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001758 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001759 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001760
1761 mdev = vnr_to_mdev(tconn, pi->vnr);
1762 if (!mdev)
1763 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001764
1765 sector = be64_to_cpu(p->sector);
1766 D_ASSERT(p->block_id == ID_SYNCER);
1767
1768 if (get_ldev(mdev)) {
1769 /* data is submitted to disk within recv_resync_read.
1770 * corresponding put_ldev done below on error,
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001771 * or in drbd_peer_request_endio. */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001772 err = recv_resync_read(mdev, sector, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001773 } else {
1774 if (__ratelimit(&drbd_ratelimit_state))
1775 dev_err(DEV, "Can not write resync data to local disk.\n");
1776
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001777 err = drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001778
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001779 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001780 }
1781
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001782 atomic_add(pi->size >> 9, &mdev->rs_sect_in);
Philipp Reisner778f2712010-07-06 11:14:00 +02001783
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001784 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001785}
1786
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001787static void restart_conflicting_writes(struct drbd_conf *mdev,
1788 sector_t sector, int size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001789{
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001790 struct drbd_interval *i;
1791 struct drbd_request *req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001792
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001793 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1794 if (!i->local)
1795 continue;
1796 req = container_of(i, struct drbd_request, i);
1797 if (req->rq_state & RQ_LOCAL_PENDING ||
1798 !(req->rq_state & RQ_POSTPONED))
1799 continue;
Lars Ellenberg2312f0b32011-11-24 10:36:25 +01001800 /* as it is RQ_POSTPONED, this will cause it to
1801 * be queued on the retry workqueue. */
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02001802 __req_mod(req, CONFLICT_RESOLVED, NULL);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001803 }
1804}
1805
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001806/*
1807 * e_end_block() is called in asender context via drbd_finish_peer_reqs().
Philipp Reisnerb411b362009-09-25 16:07:19 -07001808 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001809static int e_end_block(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001810{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001811 struct drbd_peer_request *peer_req =
1812 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001813 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001814 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001815 int err = 0, pcmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001816
Philipp Reisner303d1442011-04-13 16:24:47 -07001817 if (peer_req->flags & EE_SEND_WRITE_ACK) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001818 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001819 pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
1820 mdev->state.conn <= C_PAUSED_SYNC_T &&
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001821 peer_req->flags & EE_MAY_SET_IN_SYNC) ?
Philipp Reisnerb411b362009-09-25 16:07:19 -07001822 P_RS_WRITE_ACK : P_WRITE_ACK;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001823 err = drbd_send_ack(mdev, pcmd, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001824 if (pcmd == P_RS_WRITE_ACK)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001825 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001826 } else {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001827 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001828 /* we expect it to be marked out of sync anyways...
1829 * maybe assert this? */
1830 }
1831 dec_unacked(mdev);
1832 }
1833 /* we delete from the conflict detection hash _after_ we sent out the
1834 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
Philipp Reisner302bdea2011-04-21 11:36:49 +02001835 if (peer_req->flags & EE_IN_INTERVAL_TREE) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01001836 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001837 D_ASSERT(!drbd_interval_empty(&peer_req->i));
1838 drbd_remove_epoch_entry_interval(mdev, peer_req);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001839 if (peer_req->flags & EE_RESTART_REQUESTS)
1840 restart_conflicting_writes(mdev, sector, peer_req->i.size);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001841 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +01001842 } else
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001843 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001844
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001845 drbd_may_finish_epoch(mdev->tconn, peer_req->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001846
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001847 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001848}
1849
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001850static int e_send_ack(struct drbd_work *w, enum drbd_packet ack)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001851{
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001852 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001853 struct drbd_peer_request *peer_req =
1854 container_of(w, struct drbd_peer_request, w);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001855 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001856
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001857 err = drbd_send_ack(mdev, ack, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001858 dec_unacked(mdev);
1859
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001860 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001861}
1862
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02001863static int e_send_superseded(struct drbd_work *w, int unused)
Philipp Reisnerb6a370ba2012-02-19 01:27:53 +01001864{
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02001865 return e_send_ack(w, P_SUPERSEDED);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001866}
Philipp Reisnerb6a370ba2012-02-19 01:27:53 +01001867
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001868static int e_send_retry_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001869{
1870 struct drbd_tconn *tconn = w->mdev->tconn;
1871
1872 return e_send_ack(w, tconn->agreed_pro_version >= 100 ?
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02001873 P_RETRY_WRITE : P_SUPERSEDED);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001874}
1875
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001876static bool seq_greater(u32 a, u32 b)
1877{
1878 /*
1879 * We assume 32-bit wrap-around here.
1880 * For 24-bit wrap-around, we would have to shift:
1881 * a <<= 8; b <<= 8;
1882 */
1883 return (s32)a - (s32)b > 0;
1884}
1885
1886static u32 seq_max(u32 a, u32 b)
1887{
1888 return seq_greater(a, b) ? a : b;
1889}
1890
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001891static bool need_peer_seq(struct drbd_conf *mdev)
1892{
1893 struct drbd_tconn *tconn = mdev->tconn;
Philipp Reisner302bdea2011-04-21 11:36:49 +02001894 int tp;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001895
1896 /*
1897 * We only need to keep track of the last packet_seq number of our peer
Lars Ellenberg427c0432012-08-01 12:43:01 +02001898 * if we are in dual-primary mode and we have the resolve-conflicts flag set; see
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001899 * handle_write_conflicts().
1900 */
Philipp Reisner302bdea2011-04-21 11:36:49 +02001901
1902 rcu_read_lock();
1903 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
1904 rcu_read_unlock();
1905
Lars Ellenberg427c0432012-08-01 12:43:01 +02001906 return tp && test_bit(RESOLVE_CONFLICTS, &tconn->flags);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001907}
1908
Andreas Gruenbacher43ae0772011-02-03 18:42:08 +01001909static void update_peer_seq(struct drbd_conf *mdev, unsigned int peer_seq)
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001910{
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001911 unsigned int newest_peer_seq;
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001912
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001913 if (need_peer_seq(mdev)) {
1914 spin_lock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001915 newest_peer_seq = seq_max(mdev->peer_seq, peer_seq);
1916 mdev->peer_seq = newest_peer_seq;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001917 spin_unlock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001918 /* wake up only if we actually changed mdev->peer_seq */
1919 if (peer_seq == newest_peer_seq)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001920 wake_up(&mdev->seq_wait);
1921 }
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001922}
1923
Lars Ellenbergd93f6302012-03-26 15:49:13 +02001924static inline int overlaps(sector_t s1, int l1, sector_t s2, int l2)
1925{
1926 return !((s1 + (l1>>9) <= s2) || (s1 >= s2 + (l2>>9)));
1927}
1928
1929/* maybe change sync_ee into interval trees as well? */
Philipp Reisner3ea35df2012-04-06 12:13:18 +02001930static bool overlapping_resync_write(struct drbd_conf *mdev, struct drbd_peer_request *peer_req)
Lars Ellenbergd93f6302012-03-26 15:49:13 +02001931{
1932 struct drbd_peer_request *rs_req;
Philipp Reisnerb6a370ba2012-02-19 01:27:53 +01001933 bool rv = 0;
1934
Lars Ellenbergd93f6302012-03-26 15:49:13 +02001935 spin_lock_irq(&mdev->tconn->req_lock);
1936 list_for_each_entry(rs_req, &mdev->sync_ee, w.list) {
1937 if (overlaps(peer_req->i.sector, peer_req->i.size,
1938 rs_req->i.sector, rs_req->i.size)) {
Philipp Reisnerb6a370ba2012-02-19 01:27:53 +01001939 rv = 1;
1940 break;
1941 }
1942 }
Lars Ellenbergd93f6302012-03-26 15:49:13 +02001943 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb6a370ba2012-02-19 01:27:53 +01001944
1945 return rv;
1946}
1947
Philipp Reisnerb411b362009-09-25 16:07:19 -07001948/* Called from receive_Data.
1949 * Synchronize packets on sock with packets on msock.
1950 *
1951 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
1952 * packet traveling on msock, they are still processed in the order they have
1953 * been sent.
1954 *
1955 * Note: we don't care for Ack packets overtaking P_DATA packets.
1956 *
1957 * In case packet_seq is larger than mdev->peer_seq number, there are
1958 * outstanding packets on the msock. We wait for them to arrive.
1959 * In case we are the logically next packet, we update mdev->peer_seq
1960 * ourselves. Correctly handles 32bit wrap around.
1961 *
1962 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
1963 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
1964 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
1965 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
1966 *
1967 * returns 0 if we may process the packet,
1968 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001969static int wait_for_and_update_peer_seq(struct drbd_conf *mdev, const u32 peer_seq)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001970{
1971 DEFINE_WAIT(wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001972 long timeout;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001973 int ret;
1974
1975 if (!need_peer_seq(mdev))
1976 return 0;
1977
Philipp Reisnerb411b362009-09-25 16:07:19 -07001978 spin_lock(&mdev->peer_seq_lock);
1979 for (;;) {
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001980 if (!seq_greater(peer_seq - 1, mdev->peer_seq)) {
1981 mdev->peer_seq = seq_max(mdev->peer_seq, peer_seq);
1982 ret = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001983 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001984 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001985 if (signal_pending(current)) {
1986 ret = -ERESTARTSYS;
1987 break;
1988 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001989 prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001990 spin_unlock(&mdev->peer_seq_lock);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001991 rcu_read_lock();
1992 timeout = rcu_dereference(mdev->tconn->net_conf)->ping_timeo*HZ/10;
1993 rcu_read_unlock();
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001994 timeout = schedule_timeout(timeout);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001995 spin_lock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001996 if (!timeout) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001997 ret = -ETIMEDOUT;
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001998 dev_err(DEV, "Timed out waiting for missing ack packets; disconnecting\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001999 break;
2000 }
2001 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002002 spin_unlock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002003 finish_wait(&mdev->seq_wait, &wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002004 return ret;
2005}
2006
Lars Ellenberg688593c2010-11-17 22:25:03 +01002007/* see also bio_flags_to_wire()
2008 * DRBD_REQ_*, because we need to semantically map the flags to data packet
2009 * flags and back. We may replicate to other kernel versions. */
2010static unsigned long wire_flags_to_bio(struct drbd_conf *mdev, u32 dpf)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02002011{
Lars Ellenberg688593c2010-11-17 22:25:03 +01002012 return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
2013 (dpf & DP_FUA ? REQ_FUA : 0) |
2014 (dpf & DP_FLUSH ? REQ_FLUSH : 0) |
2015 (dpf & DP_DISCARD ? REQ_DISCARD : 0);
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02002016}
2017
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002018static void fail_postponed_requests(struct drbd_conf *mdev, sector_t sector,
2019 unsigned int size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002020{
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002021 struct drbd_interval *i;
2022
2023 repeat:
2024 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
2025 struct drbd_request *req;
2026 struct bio_and_error m;
2027
2028 if (!i->local)
2029 continue;
2030 req = container_of(i, struct drbd_request, i);
2031 if (!(req->rq_state & RQ_POSTPONED))
2032 continue;
2033 req->rq_state &= ~RQ_POSTPONED;
2034 __req_mod(req, NEG_ACKED, &m);
2035 spin_unlock_irq(&mdev->tconn->req_lock);
2036 if (m.bio)
2037 complete_master_bio(mdev, &m);
2038 spin_lock_irq(&mdev->tconn->req_lock);
2039 goto repeat;
2040 }
2041}
2042
2043static int handle_write_conflicts(struct drbd_conf *mdev,
2044 struct drbd_peer_request *peer_req)
2045{
2046 struct drbd_tconn *tconn = mdev->tconn;
Lars Ellenberg427c0432012-08-01 12:43:01 +02002047 bool resolve_conflicts = test_bit(RESOLVE_CONFLICTS, &tconn->flags);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002048 sector_t sector = peer_req->i.sector;
2049 const unsigned int size = peer_req->i.size;
2050 struct drbd_interval *i;
2051 bool equal;
2052 int err;
2053
2054 /*
2055 * Inserting the peer request into the write_requests tree will prevent
2056 * new conflicting local requests from being added.
2057 */
2058 drbd_insert_interval(&mdev->write_requests, &peer_req->i);
2059
2060 repeat:
2061 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
2062 if (i == &peer_req->i)
2063 continue;
2064
2065 if (!i->local) {
2066 /*
2067 * Our peer has sent a conflicting remote request; this
2068 * should not happen in a two-node setup. Wait for the
2069 * earlier peer request to complete.
2070 */
2071 err = drbd_wait_misc(mdev, i);
2072 if (err)
2073 goto out;
2074 goto repeat;
2075 }
2076
2077 equal = i->sector == sector && i->size == size;
2078 if (resolve_conflicts) {
2079 /*
2080 * If the peer request is fully contained within the
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02002081 * overlapping request, it can be considered overwritten
2082 * and thus superseded; otherwise, it will be retried
2083 * once all overlapping requests have completed.
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002084 */
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02002085 bool superseded = i->sector <= sector && i->sector +
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002086 (i->size >> 9) >= sector + (size >> 9);
2087
2088 if (!equal)
2089 dev_alert(DEV, "Concurrent writes detected: "
2090 "local=%llus +%u, remote=%llus +%u, "
2091 "assuming %s came first\n",
2092 (unsigned long long)i->sector, i->size,
2093 (unsigned long long)sector, size,
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02002094 superseded ? "local" : "remote");
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002095
2096 inc_unacked(mdev);
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02002097 peer_req->w.cb = superseded ? e_send_superseded :
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002098 e_send_retry_write;
2099 list_add_tail(&peer_req->w.list, &mdev->done_ee);
2100 wake_asender(mdev->tconn);
2101
2102 err = -ENOENT;
2103 goto out;
2104 } else {
2105 struct drbd_request *req =
2106 container_of(i, struct drbd_request, i);
2107
2108 if (!equal)
2109 dev_alert(DEV, "Concurrent writes detected: "
2110 "local=%llus +%u, remote=%llus +%u\n",
2111 (unsigned long long)i->sector, i->size,
2112 (unsigned long long)sector, size);
2113
2114 if (req->rq_state & RQ_LOCAL_PENDING ||
2115 !(req->rq_state & RQ_POSTPONED)) {
2116 /*
2117 * Wait for the node with the discard flag to
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02002118 * decide if this request has been superseded
2119 * or needs to be retried.
2120 * Requests that have been superseded will
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002121 * disappear from the write_requests tree.
2122 *
2123 * In addition, wait for the conflicting
2124 * request to finish locally before submitting
2125 * the conflicting peer request.
2126 */
2127 err = drbd_wait_misc(mdev, &req->i);
2128 if (err) {
2129 _conn_request_state(mdev->tconn,
2130 NS(conn, C_TIMEOUT),
2131 CS_HARD);
2132 fail_postponed_requests(mdev, sector, size);
2133 goto out;
2134 }
2135 goto repeat;
2136 }
2137 /*
2138 * Remember to restart the conflicting requests after
2139 * the new peer request has completed.
2140 */
2141 peer_req->flags |= EE_RESTART_REQUESTS;
2142 }
2143 }
2144 err = 0;
2145
2146 out:
2147 if (err)
2148 drbd_remove_epoch_entry_interval(mdev, peer_req);
2149 return err;
2150}
2151
Philipp Reisnerb411b362009-09-25 16:07:19 -07002152/* mirrored write */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002153static int receive_Data(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002154{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002155 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002156 sector_t sector;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002157 struct drbd_peer_request *peer_req;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002158 struct p_data *p = pi->data;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002159 u32 peer_seq = be32_to_cpu(p->seq_num);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002160 int rw = WRITE;
2161 u32 dp_flags;
Philipp Reisner302bdea2011-04-21 11:36:49 +02002162 int err, tp;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002163
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002164 mdev = vnr_to_mdev(tconn, pi->vnr);
2165 if (!mdev)
2166 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002167
Philipp Reisnerb411b362009-09-25 16:07:19 -07002168 if (!get_ldev(mdev)) {
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002169 int err2;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002170
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002171 err = wait_for_and_update_peer_seq(mdev, peer_seq);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002172 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisner12038a32011-11-09 19:18:00 +01002173 atomic_inc(&tconn->current_epoch->epoch_size);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002174 err2 = drbd_drain_block(mdev, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002175 if (!err)
2176 err = err2;
2177 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002178 }
2179
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01002180 /*
2181 * Corresponding put_ldev done either below (on various errors), or in
2182 * drbd_peer_request_endio, if we successfully submit the data at the
2183 * end of this function.
2184 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002185
2186 sector = be64_to_cpu(p->sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002187 peer_req = read_in_block(mdev, p->block_id, sector, pi->size);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002188 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002189 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002190 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002191 }
2192
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002193 peer_req->w.cb = e_end_block;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002194
Lars Ellenberg688593c2010-11-17 22:25:03 +01002195 dp_flags = be32_to_cpu(p->dp_flags);
2196 rw |= wire_flags_to_bio(mdev, dp_flags);
Lars Ellenberg81a35372012-07-30 09:00:54 +02002197 if (peer_req->pages == NULL) {
2198 D_ASSERT(peer_req->i.size == 0);
Lars Ellenberga73ff322012-06-25 19:15:38 +02002199 D_ASSERT(dp_flags & DP_FLUSH);
2200 }
Lars Ellenberg688593c2010-11-17 22:25:03 +01002201
2202 if (dp_flags & DP_MAY_SET_IN_SYNC)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002203 peer_req->flags |= EE_MAY_SET_IN_SYNC;
Lars Ellenberg688593c2010-11-17 22:25:03 +01002204
Philipp Reisner12038a32011-11-09 19:18:00 +01002205 spin_lock(&tconn->epoch_lock);
2206 peer_req->epoch = tconn->current_epoch;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002207 atomic_inc(&peer_req->epoch->epoch_size);
2208 atomic_inc(&peer_req->epoch->active);
Philipp Reisner12038a32011-11-09 19:18:00 +01002209 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002210
Philipp Reisner302bdea2011-04-21 11:36:49 +02002211 rcu_read_lock();
2212 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
2213 rcu_read_unlock();
2214 if (tp) {
2215 peer_req->flags |= EE_IN_INTERVAL_TREE;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002216 err = wait_for_and_update_peer_seq(mdev, peer_seq);
2217 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002218 goto out_interrupted;
Philipp Reisner87eeee42011-01-19 14:16:30 +01002219 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002220 err = handle_write_conflicts(mdev, peer_req);
2221 if (err) {
2222 spin_unlock_irq(&mdev->tconn->req_lock);
2223 if (err == -ENOENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002224 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002225 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002226 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002227 goto out_interrupted;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002228 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002229 } else
2230 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002231 list_add(&peer_req->w.list, &mdev->active_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002232 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002233
Philipp Reisnerb6a370ba2012-02-19 01:27:53 +01002234 if (mdev->state.conn == C_SYNC_TARGET)
Philipp Reisner3ea35df2012-04-06 12:13:18 +02002235 wait_event(mdev->ee_wait, !overlapping_resync_write(mdev, peer_req));
Philipp Reisnerb6a370ba2012-02-19 01:27:53 +01002236
Philipp Reisner303d1442011-04-13 16:24:47 -07002237 if (mdev->tconn->agreed_pro_version < 100) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02002238 rcu_read_lock();
2239 switch (rcu_dereference(mdev->tconn->net_conf)->wire_protocol) {
Philipp Reisner303d1442011-04-13 16:24:47 -07002240 case DRBD_PROT_C:
2241 dp_flags |= DP_SEND_WRITE_ACK;
2242 break;
2243 case DRBD_PROT_B:
2244 dp_flags |= DP_SEND_RECEIVE_ACK;
2245 break;
2246 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002247 rcu_read_unlock();
Philipp Reisner303d1442011-04-13 16:24:47 -07002248 }
2249
2250 if (dp_flags & DP_SEND_WRITE_ACK) {
2251 peer_req->flags |= EE_SEND_WRITE_ACK;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002252 inc_unacked(mdev);
2253 /* corresponding dec_unacked() in e_end_block()
2254 * respective _drbd_clear_done_ee */
Philipp Reisner303d1442011-04-13 16:24:47 -07002255 }
2256
2257 if (dp_flags & DP_SEND_RECEIVE_ACK) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002258 /* I really don't like it that the receiver thread
2259 * sends on the msock, but anyways */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002260 drbd_send_ack(mdev, P_RECV_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002261 }
2262
Lars Ellenberg6719fb02010-10-18 23:04:07 +02002263 if (mdev->state.pdsk < D_INCONSISTENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002264 /* In case we have the only disk of the cluster, */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002265 drbd_set_out_of_sync(mdev, peer_req->i.sector, peer_req->i.size);
2266 peer_req->flags |= EE_CALL_AL_COMPLETE_IO;
2267 peer_req->flags &= ~EE_MAY_SET_IN_SYNC;
Lars Ellenberg181286a2011-03-31 15:18:56 +02002268 drbd_al_begin_io(mdev, &peer_req->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002269 }
2270
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002271 err = drbd_submit_peer_request(mdev, peer_req, rw, DRBD_FAULT_DT_WR);
2272 if (!err)
2273 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002274
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002275 /* don't care for the reason here */
2276 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002277 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002278 list_del(&peer_req->w.list);
2279 drbd_remove_epoch_entry_interval(mdev, peer_req);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002280 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002281 if (peer_req->flags & EE_CALL_AL_COMPLETE_IO)
Lars Ellenberg181286a2011-03-31 15:18:56 +02002282 drbd_al_complete_io(mdev, &peer_req->i);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002283
Philipp Reisnerb411b362009-09-25 16:07:19 -07002284out_interrupted:
Philipp Reisner1e9dd292011-11-10 15:14:53 +01002285 drbd_may_finish_epoch(tconn, peer_req->epoch, EV_PUT + EV_CLEANUP);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002286 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002287 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002288 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002289}
2290
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002291/* We may throttle resync, if the lower device seems to be busy,
2292 * and current sync rate is above c_min_rate.
2293 *
2294 * To decide whether or not the lower device is busy, we use a scheme similar
2295 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
2296 * (more than 64 sectors) of activity we cannot account for with our own resync
2297 * activity, it obviously is "busy".
2298 *
2299 * The current sync rate used here uses only the most recent two step marks,
2300 * to have a short time average so we can react faster.
2301 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002302int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002303{
2304 struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
2305 unsigned long db, dt, dbdt;
Philipp Reisnere3555d82010-11-07 15:56:29 +01002306 struct lc_element *tmp;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002307 int curr_events;
2308 int throttle = 0;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002309 unsigned int c_min_rate;
2310
2311 rcu_read_lock();
2312 c_min_rate = rcu_dereference(mdev->ldev->disk_conf)->c_min_rate;
2313 rcu_read_unlock();
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002314
2315 /* feature disabled? */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002316 if (c_min_rate == 0)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002317 return 0;
2318
Philipp Reisnere3555d82010-11-07 15:56:29 +01002319 spin_lock_irq(&mdev->al_lock);
2320 tmp = lc_find(mdev->resync, BM_SECT_TO_EXT(sector));
2321 if (tmp) {
2322 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
2323 if (test_bit(BME_PRIORITY, &bm_ext->flags)) {
2324 spin_unlock_irq(&mdev->al_lock);
2325 return 0;
2326 }
2327 /* Do not slow down if app IO is already waiting for this extent */
2328 }
2329 spin_unlock_irq(&mdev->al_lock);
2330
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002331 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
2332 (int)part_stat_read(&disk->part0, sectors[1]) -
2333 atomic_read(&mdev->rs_sect_ev);
Philipp Reisnere3555d82010-11-07 15:56:29 +01002334
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002335 if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
2336 unsigned long rs_left;
2337 int i;
2338
2339 mdev->rs_last_events = curr_events;
2340
2341 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
2342 * approx. */
Lars Ellenberg2649f082010-11-05 10:05:47 +01002343 i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
2344
2345 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
2346 rs_left = mdev->ov_left;
2347 else
2348 rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002349
2350 dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
2351 if (!dt)
2352 dt++;
2353 db = mdev->rs_mark_left[i] - rs_left;
2354 dbdt = Bit2KB(db/dt);
2355
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002356 if (dbdt > c_min_rate)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002357 throttle = 1;
2358 }
2359 return throttle;
2360}
2361
2362
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002363static int receive_DataRequest(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002364{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002365 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002366 sector_t sector;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002367 sector_t capacity;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002368 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002369 struct digest_info *di = NULL;
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002370 int size, verb;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002371 unsigned int fault_type;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002372 struct p_block_req *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002373
2374 mdev = vnr_to_mdev(tconn, pi->vnr);
2375 if (!mdev)
2376 return -EIO;
2377 capacity = drbd_get_capacity(mdev->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002378
2379 sector = be64_to_cpu(p->sector);
2380 size = be32_to_cpu(p->blksize);
2381
Andreas Gruenbacherc670a392011-02-21 12:41:39 +01002382 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002383 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2384 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002385 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002386 }
2387 if (sector + (size>>9) > capacity) {
2388 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2389 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002390 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002391 }
2392
2393 if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002394 verb = 1;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002395 switch (pi->cmd) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002396 case P_DATA_REQUEST:
2397 drbd_send_ack_rp(mdev, P_NEG_DREPLY, p);
2398 break;
2399 case P_RS_DATA_REQUEST:
2400 case P_CSUM_RS_REQUEST:
2401 case P_OV_REQUEST:
2402 drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p);
2403 break;
2404 case P_OV_REPLY:
2405 verb = 0;
2406 dec_rs_pending(mdev);
2407 drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC);
2408 break;
2409 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002410 BUG();
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002411 }
2412 if (verb && __ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002413 dev_err(DEV, "Can not satisfy peer's read request, "
2414 "no local data.\n");
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002415
Lars Ellenberga821cc42010-09-06 12:31:37 +02002416 /* drain possibly payload */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002417 return drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002418 }
2419
2420 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
2421 * "criss-cross" setup, that might cause write-out on some other DRBD,
2422 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02002423 peer_req = drbd_alloc_peer_req(mdev, p->block_id, sector, size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002424 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002425 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002426 return -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002427 }
2428
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002429 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002430 case P_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002431 peer_req->w.cb = w_e_end_data_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002432 fault_type = DRBD_FAULT_DT_RD;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002433 /* application IO, don't drbd_rs_begin_io */
2434 goto submit;
2435
Philipp Reisnerb411b362009-09-25 16:07:19 -07002436 case P_RS_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002437 peer_req->w.cb = w_e_end_rsdata_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002438 fault_type = DRBD_FAULT_RS_RD;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002439 /* used in the sector offset progress display */
2440 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002441 break;
2442
2443 case P_OV_REPLY:
2444 case P_CSUM_RS_REQUEST:
2445 fault_type = DRBD_FAULT_RS_RD;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002446 di = kmalloc(sizeof(*di) + pi->size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002447 if (!di)
2448 goto out_free_e;
2449
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002450 di->digest_size = pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002451 di->digest = (((char *)di)+sizeof(struct digest_info));
2452
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002453 peer_req->digest = di;
2454 peer_req->flags |= EE_HAS_DIGEST;
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +02002455
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002456 if (drbd_recv_all(mdev->tconn, di->digest, pi->size))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002457 goto out_free_e;
2458
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002459 if (pi->cmd == P_CSUM_RS_REQUEST) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002460 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002461 peer_req->w.cb = w_e_end_csum_rs_req;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002462 /* used in the sector offset progress display */
2463 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002464 } else if (pi->cmd == P_OV_REPLY) {
Lars Ellenberg2649f082010-11-05 10:05:47 +01002465 /* track progress, we may need to throttle */
2466 atomic_add(size >> 9, &mdev->rs_sect_in);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002467 peer_req->w.cb = w_e_end_ov_reply;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002468 dec_rs_pending(mdev);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002469 /* drbd_rs_begin_io done when we sent this request,
2470 * but accounting still needs to be done. */
2471 goto submit_for_resync;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002472 }
2473 break;
2474
2475 case P_OV_REQUEST:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002476 if (mdev->ov_start_sector == ~(sector_t)0 &&
Philipp Reisner31890f42011-01-19 14:12:51 +01002477 mdev->tconn->agreed_pro_version >= 90) {
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002478 unsigned long now = jiffies;
2479 int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002480 mdev->ov_start_sector = sector;
2481 mdev->ov_position = sector;
Lars Ellenberg30b743a2010-11-05 09:39:06 +01002482 mdev->ov_left = drbd_bm_bits(mdev) - BM_SECT_TO_BIT(sector);
2483 mdev->rs_total = mdev->ov_left;
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002484 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
2485 mdev->rs_mark_left[i] = mdev->ov_left;
2486 mdev->rs_mark_time[i] = now;
2487 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002488 dev_info(DEV, "Online Verify start sector: %llu\n",
2489 (unsigned long long)sector);
2490 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002491 peer_req->w.cb = w_e_end_ov_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002492 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002493 break;
2494
Philipp Reisnerb411b362009-09-25 16:07:19 -07002495 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002496 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002497 }
2498
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002499 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2500 * wrt the receiver, but it is not as straightforward as it may seem.
2501 * Various places in the resync start and stop logic assume resync
2502 * requests are processed in order, requeuing this on the worker thread
2503 * introduces a bunch of new code for synchronization between threads.
2504 *
2505 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2506 * "forever", throttling after drbd_rs_begin_io will lock that extent
2507 * for application writes for the same time. For now, just throttle
2508 * here, where the rest of the code expects the receiver to sleep for
2509 * a while, anyways.
2510 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002511
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002512 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2513 * this defers syncer requests for some time, before letting at least
2514 * on request through. The resync controller on the receiving side
2515 * will adapt to the incoming rate accordingly.
2516 *
2517 * We cannot throttle here if remote is Primary/SyncTarget:
2518 * we would also throttle its application reads.
2519 * In that case, throttling is done on the SyncTarget only.
2520 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002521 if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev, sector))
2522 schedule_timeout_uninterruptible(HZ/10);
2523 if (drbd_rs_begin_io(mdev, sector))
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002524 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002525
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002526submit_for_resync:
2527 atomic_add(size >> 9, &mdev->rs_sect_ev);
2528
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002529submit:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002530 inc_unacked(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002531 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002532 list_add_tail(&peer_req->w.list, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002533 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002534
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01002535 if (drbd_submit_peer_request(mdev, peer_req, READ, fault_type) == 0)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002536 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002537
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002538 /* don't care for the reason here */
2539 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002540 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002541 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002542 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002543 /* no drbd_rs_complete_io(), we are dropping the connection anyways */
2544
Philipp Reisnerb411b362009-09-25 16:07:19 -07002545out_free_e:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002546 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002547 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002548 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002549}
2550
2551static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
2552{
2553 int self, peer, rv = -100;
2554 unsigned long ch_self, ch_peer;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002555 enum drbd_after_sb_p after_sb_0p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002556
2557 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2558 peer = mdev->p_uuid[UI_BITMAP] & 1;
2559
2560 ch_peer = mdev->p_uuid[UI_SIZE];
2561 ch_self = mdev->comm_bm_set;
2562
Philipp Reisner44ed1672011-04-19 17:10:19 +02002563 rcu_read_lock();
2564 after_sb_0p = rcu_dereference(mdev->tconn->net_conf)->after_sb_0p;
2565 rcu_read_unlock();
2566 switch (after_sb_0p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002567 case ASB_CONSENSUS:
2568 case ASB_DISCARD_SECONDARY:
2569 case ASB_CALL_HELPER:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002570 case ASB_VIOLENTLY:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002571 dev_err(DEV, "Configuration error.\n");
2572 break;
2573 case ASB_DISCONNECT:
2574 break;
2575 case ASB_DISCARD_YOUNGER_PRI:
2576 if (self == 0 && peer == 1) {
2577 rv = -1;
2578 break;
2579 }
2580 if (self == 1 && peer == 0) {
2581 rv = 1;
2582 break;
2583 }
2584 /* Else fall through to one of the other strategies... */
2585 case ASB_DISCARD_OLDER_PRI:
2586 if (self == 0 && peer == 1) {
2587 rv = 1;
2588 break;
2589 }
2590 if (self == 1 && peer == 0) {
2591 rv = -1;
2592 break;
2593 }
2594 /* Else fall through to one of the other strategies... */
Lars Ellenbergad19bf62009-10-14 09:36:49 +02002595 dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
Philipp Reisnerb411b362009-09-25 16:07:19 -07002596 "Using discard-least-changes instead\n");
2597 case ASB_DISCARD_ZERO_CHG:
2598 if (ch_peer == 0 && ch_self == 0) {
Lars Ellenberg427c0432012-08-01 12:43:01 +02002599 rv = test_bit(RESOLVE_CONFLICTS, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002600 ? -1 : 1;
2601 break;
2602 } else {
2603 if (ch_peer == 0) { rv = 1; break; }
2604 if (ch_self == 0) { rv = -1; break; }
2605 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002606 if (after_sb_0p == ASB_DISCARD_ZERO_CHG)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002607 break;
2608 case ASB_DISCARD_LEAST_CHG:
2609 if (ch_self < ch_peer)
2610 rv = -1;
2611 else if (ch_self > ch_peer)
2612 rv = 1;
2613 else /* ( ch_self == ch_peer ) */
2614 /* Well, then use something else. */
Lars Ellenberg427c0432012-08-01 12:43:01 +02002615 rv = test_bit(RESOLVE_CONFLICTS, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002616 ? -1 : 1;
2617 break;
2618 case ASB_DISCARD_LOCAL:
2619 rv = -1;
2620 break;
2621 case ASB_DISCARD_REMOTE:
2622 rv = 1;
2623 }
2624
2625 return rv;
2626}
2627
2628static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
2629{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002630 int hg, rv = -100;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002631 enum drbd_after_sb_p after_sb_1p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002632
Philipp Reisner44ed1672011-04-19 17:10:19 +02002633 rcu_read_lock();
2634 after_sb_1p = rcu_dereference(mdev->tconn->net_conf)->after_sb_1p;
2635 rcu_read_unlock();
2636 switch (after_sb_1p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002637 case ASB_DISCARD_YOUNGER_PRI:
2638 case ASB_DISCARD_OLDER_PRI:
2639 case ASB_DISCARD_LEAST_CHG:
2640 case ASB_DISCARD_LOCAL:
2641 case ASB_DISCARD_REMOTE:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002642 case ASB_DISCARD_ZERO_CHG:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002643 dev_err(DEV, "Configuration error.\n");
2644 break;
2645 case ASB_DISCONNECT:
2646 break;
2647 case ASB_CONSENSUS:
2648 hg = drbd_asb_recover_0p(mdev);
2649 if (hg == -1 && mdev->state.role == R_SECONDARY)
2650 rv = hg;
2651 if (hg == 1 && mdev->state.role == R_PRIMARY)
2652 rv = hg;
2653 break;
2654 case ASB_VIOLENTLY:
2655 rv = drbd_asb_recover_0p(mdev);
2656 break;
2657 case ASB_DISCARD_SECONDARY:
2658 return mdev->state.role == R_PRIMARY ? 1 : -1;
2659 case ASB_CALL_HELPER:
2660 hg = drbd_asb_recover_0p(mdev);
2661 if (hg == -1 && mdev->state.role == R_PRIMARY) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002662 enum drbd_state_rv rv2;
2663
2664 drbd_set_role(mdev, R_SECONDARY, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002665 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2666 * we might be here in C_WF_REPORT_PARAMS which is transient.
2667 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002668 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2669 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002670 drbd_khelper(mdev, "pri-lost-after-sb");
2671 } else {
2672 dev_warn(DEV, "Successfully gave up primary role.\n");
2673 rv = hg;
2674 }
2675 } else
2676 rv = hg;
2677 }
2678
2679 return rv;
2680}
2681
2682static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
2683{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002684 int hg, rv = -100;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002685 enum drbd_after_sb_p after_sb_2p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002686
Philipp Reisner44ed1672011-04-19 17:10:19 +02002687 rcu_read_lock();
2688 after_sb_2p = rcu_dereference(mdev->tconn->net_conf)->after_sb_2p;
2689 rcu_read_unlock();
2690 switch (after_sb_2p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002691 case ASB_DISCARD_YOUNGER_PRI:
2692 case ASB_DISCARD_OLDER_PRI:
2693 case ASB_DISCARD_LEAST_CHG:
2694 case ASB_DISCARD_LOCAL:
2695 case ASB_DISCARD_REMOTE:
2696 case ASB_CONSENSUS:
2697 case ASB_DISCARD_SECONDARY:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002698 case ASB_DISCARD_ZERO_CHG:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002699 dev_err(DEV, "Configuration error.\n");
2700 break;
2701 case ASB_VIOLENTLY:
2702 rv = drbd_asb_recover_0p(mdev);
2703 break;
2704 case ASB_DISCONNECT:
2705 break;
2706 case ASB_CALL_HELPER:
2707 hg = drbd_asb_recover_0p(mdev);
2708 if (hg == -1) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002709 enum drbd_state_rv rv2;
2710
Philipp Reisnerb411b362009-09-25 16:07:19 -07002711 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2712 * we might be here in C_WF_REPORT_PARAMS which is transient.
2713 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002714 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2715 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002716 drbd_khelper(mdev, "pri-lost-after-sb");
2717 } else {
2718 dev_warn(DEV, "Successfully gave up primary role.\n");
2719 rv = hg;
2720 }
2721 } else
2722 rv = hg;
2723 }
2724
2725 return rv;
2726}
2727
2728static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
2729 u64 bits, u64 flags)
2730{
2731 if (!uuid) {
2732 dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
2733 return;
2734 }
2735 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
2736 text,
2737 (unsigned long long)uuid[UI_CURRENT],
2738 (unsigned long long)uuid[UI_BITMAP],
2739 (unsigned long long)uuid[UI_HISTORY_START],
2740 (unsigned long long)uuid[UI_HISTORY_END],
2741 (unsigned long long)bits,
2742 (unsigned long long)flags);
2743}
2744
2745/*
2746 100 after split brain try auto recover
2747 2 C_SYNC_SOURCE set BitMap
2748 1 C_SYNC_SOURCE use BitMap
2749 0 no Sync
2750 -1 C_SYNC_TARGET use BitMap
2751 -2 C_SYNC_TARGET set BitMap
2752 -100 after split brain, disconnect
2753-1000 unrelated data
Philipp Reisner4a23f262011-01-11 17:42:17 +01002754-1091 requires proto 91
2755-1096 requires proto 96
Philipp Reisnerb411b362009-09-25 16:07:19 -07002756 */
2757static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
2758{
2759 u64 self, peer;
2760 int i, j;
2761
2762 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2763 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2764
2765 *rule_nr = 10;
2766 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
2767 return 0;
2768
2769 *rule_nr = 20;
2770 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
2771 peer != UUID_JUST_CREATED)
2772 return -2;
2773
2774 *rule_nr = 30;
2775 if (self != UUID_JUST_CREATED &&
2776 (peer == UUID_JUST_CREATED || peer == (u64)0))
2777 return 2;
2778
2779 if (self == peer) {
2780 int rct, dc; /* roles at crash time */
2781
2782 if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
2783
Philipp Reisner31890f42011-01-19 14:12:51 +01002784 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002785 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002786
2787 if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
2788 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
2789 dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
Philipp Reisner9f2247b2012-08-16 14:25:58 +02002790 drbd_uuid_move_history(mdev);
2791 mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[UI_BITMAP];
2792 mdev->ldev->md.uuid[UI_BITMAP] = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002793
2794 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2795 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2796 *rule_nr = 34;
2797 } else {
2798 dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
2799 *rule_nr = 36;
2800 }
2801
2802 return 1;
2803 }
2804
2805 if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
2806
Philipp Reisner31890f42011-01-19 14:12:51 +01002807 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002808 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002809
2810 if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
2811 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
2812 dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
2813
2814 mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
2815 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
2816 mdev->p_uuid[UI_BITMAP] = 0UL;
2817
2818 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2819 *rule_nr = 35;
2820 } else {
2821 dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
2822 *rule_nr = 37;
2823 }
2824
2825 return -1;
2826 }
2827
2828 /* Common power [off|failure] */
2829 rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
2830 (mdev->p_uuid[UI_FLAGS] & 2);
2831 /* lowest bit is set when we were primary,
2832 * next bit (weight 2) is set when peer was primary */
2833 *rule_nr = 40;
2834
2835 switch (rct) {
2836 case 0: /* !self_pri && !peer_pri */ return 0;
2837 case 1: /* self_pri && !peer_pri */ return 1;
2838 case 2: /* !self_pri && peer_pri */ return -1;
2839 case 3: /* self_pri && peer_pri */
Lars Ellenberg427c0432012-08-01 12:43:01 +02002840 dc = test_bit(RESOLVE_CONFLICTS, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002841 return dc ? -1 : 1;
2842 }
2843 }
2844
2845 *rule_nr = 50;
2846 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2847 if (self == peer)
2848 return -1;
2849
2850 *rule_nr = 51;
2851 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2852 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002853 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002854 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) ==
2855 (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) :
2856 peer + UUID_NEW_BM_OFFSET == (mdev->p_uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002857 /* The last P_SYNC_UUID did not get though. Undo the last start of
2858 resync as sync source modifications of the peer's UUIDs. */
2859
Philipp Reisner31890f42011-01-19 14:12:51 +01002860 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002861 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002862
2863 mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
2864 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
Philipp Reisner4a23f262011-01-11 17:42:17 +01002865
Lars Ellenberg92b4ca22012-04-30 12:53:52 +02002866 dev_info(DEV, "Lost last syncUUID packet, corrected:\n");
Philipp Reisner4a23f262011-01-11 17:42:17 +01002867 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2868
Philipp Reisnerb411b362009-09-25 16:07:19 -07002869 return -1;
2870 }
2871 }
2872
2873 *rule_nr = 60;
2874 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2875 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2876 peer = mdev->p_uuid[i] & ~((u64)1);
2877 if (self == peer)
2878 return -2;
2879 }
2880
2881 *rule_nr = 70;
2882 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2883 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2884 if (self == peer)
2885 return 1;
2886
2887 *rule_nr = 71;
2888 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2889 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002890 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002891 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) ==
2892 (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) :
2893 self + UUID_NEW_BM_OFFSET == (mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002894 /* The last P_SYNC_UUID did not get though. Undo the last start of
2895 resync as sync source modifications of our UUIDs. */
2896
Philipp Reisner31890f42011-01-19 14:12:51 +01002897 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002898 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002899
Philipp Reisner9f2247b2012-08-16 14:25:58 +02002900 __drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
2901 __drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002902
Philipp Reisner4a23f262011-01-11 17:42:17 +01002903 dev_info(DEV, "Last syncUUID did not get through, corrected:\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002904 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2905 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2906
2907 return 1;
2908 }
2909 }
2910
2911
2912 *rule_nr = 80;
Philipp Reisnerd8c2a362009-11-18 15:52:51 +01002913 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002914 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2915 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2916 if (self == peer)
2917 return 2;
2918 }
2919
2920 *rule_nr = 90;
2921 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2922 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2923 if (self == peer && self != ((u64)0))
2924 return 100;
2925
2926 *rule_nr = 100;
2927 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2928 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2929 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
2930 peer = mdev->p_uuid[j] & ~((u64)1);
2931 if (self == peer)
2932 return -100;
2933 }
2934 }
2935
2936 return -1000;
2937}
2938
2939/* drbd_sync_handshake() returns the new conn state on success, or
2940 CONN_MASK (-1) on failure.
2941 */
2942static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
2943 enum drbd_disk_state peer_disk) __must_hold(local)
2944{
Philipp Reisnerb411b362009-09-25 16:07:19 -07002945 enum drbd_conns rv = C_MASK;
2946 enum drbd_disk_state mydisk;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002947 struct net_conf *nc;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02002948 int hg, rule_nr, rr_conflict, tentative;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002949
2950 mydisk = mdev->state.disk;
2951 if (mydisk == D_NEGOTIATING)
2952 mydisk = mdev->new_state_tmp.disk;
2953
2954 dev_info(DEV, "drbd_sync_handshake:\n");
Philipp Reisner9f2247b2012-08-16 14:25:58 +02002955
2956 spin_lock_irq(&mdev->ldev->md.uuid_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002957 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0);
2958 drbd_uuid_dump(mdev, "peer", mdev->p_uuid,
2959 mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2960
2961 hg = drbd_uuid_compare(mdev, &rule_nr);
Philipp Reisner9f2247b2012-08-16 14:25:58 +02002962 spin_unlock_irq(&mdev->ldev->md.uuid_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002963
2964 dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
2965
2966 if (hg == -1000) {
2967 dev_alert(DEV, "Unrelated data, aborting!\n");
2968 return C_MASK;
2969 }
Philipp Reisner4a23f262011-01-11 17:42:17 +01002970 if (hg < -1000) {
2971 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 -07002972 return C_MASK;
2973 }
2974
2975 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
2976 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
2977 int f = (hg == -100) || abs(hg) == 2;
2978 hg = mydisk > D_INCONSISTENT ? 1 : -1;
2979 if (f)
2980 hg = hg*2;
2981 dev_info(DEV, "Becoming sync %s due to disk states.\n",
2982 hg > 0 ? "source" : "target");
2983 }
2984
Adam Gandelman3a11a482010-04-08 16:48:23 -07002985 if (abs(hg) == 100)
2986 drbd_khelper(mdev, "initial-split-brain");
2987
Philipp Reisner44ed1672011-04-19 17:10:19 +02002988 rcu_read_lock();
2989 nc = rcu_dereference(mdev->tconn->net_conf);
2990
2991 if (hg == 100 || (hg == -100 && nc->always_asbp)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002992 int pcount = (mdev->state.role == R_PRIMARY)
2993 + (peer_role == R_PRIMARY);
2994 int forced = (hg == -100);
2995
2996 switch (pcount) {
2997 case 0:
2998 hg = drbd_asb_recover_0p(mdev);
2999 break;
3000 case 1:
3001 hg = drbd_asb_recover_1p(mdev);
3002 break;
3003 case 2:
3004 hg = drbd_asb_recover_2p(mdev);
3005 break;
3006 }
3007 if (abs(hg) < 100) {
3008 dev_warn(DEV, "Split-Brain detected, %d primaries, "
3009 "automatically solved. Sync from %s node\n",
3010 pcount, (hg < 0) ? "peer" : "this");
3011 if (forced) {
3012 dev_warn(DEV, "Doing a full sync, since"
3013 " UUIDs where ambiguous.\n");
3014 hg = hg*2;
3015 }
3016 }
3017 }
3018
3019 if (hg == -100) {
Philipp Reisner08b165b2011-09-05 16:22:33 +02003020 if (test_bit(DISCARD_MY_DATA, &mdev->flags) && !(mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003021 hg = -1;
Philipp Reisner08b165b2011-09-05 16:22:33 +02003022 if (!test_bit(DISCARD_MY_DATA, &mdev->flags) && (mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003023 hg = 1;
3024
3025 if (abs(hg) < 100)
3026 dev_warn(DEV, "Split-Brain detected, manually solved. "
3027 "Sync from %s node\n",
3028 (hg < 0) ? "peer" : "this");
3029 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02003030 rr_conflict = nc->rr_conflict;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02003031 tentative = nc->tentative;
Philipp Reisner44ed1672011-04-19 17:10:19 +02003032 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07003033
3034 if (hg == -100) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01003035 /* FIXME this log message is not correct if we end up here
3036 * after an attempted attach on a diskless node.
3037 * We just refuse to attach -- well, we drop the "connection"
3038 * to that disk, in a way... */
Adam Gandelman3a11a482010-04-08 16:48:23 -07003039 dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003040 drbd_khelper(mdev, "split-brain");
3041 return C_MASK;
3042 }
3043
3044 if (hg > 0 && mydisk <= D_INCONSISTENT) {
3045 dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
3046 return C_MASK;
3047 }
3048
3049 if (hg < 0 && /* by intention we do not use mydisk here. */
3050 mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02003051 switch (rr_conflict) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003052 case ASB_CALL_HELPER:
3053 drbd_khelper(mdev, "pri-lost");
3054 /* fall through */
3055 case ASB_DISCONNECT:
3056 dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
3057 return C_MASK;
3058 case ASB_VIOLENTLY:
3059 dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
3060 "assumption\n");
3061 }
3062 }
3063
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02003064 if (tentative || test_bit(CONN_DRY_RUN, &mdev->tconn->flags)) {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003065 if (hg == 0)
3066 dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
3067 else
3068 dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
3069 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
3070 abs(hg) >= 2 ? "full" : "bit-map based");
3071 return C_MASK;
3072 }
3073
Philipp Reisnerb411b362009-09-25 16:07:19 -07003074 if (abs(hg) >= 2) {
3075 dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003076 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake",
3077 BM_LOCKED_SET_ALLOWED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003078 return C_MASK;
3079 }
3080
3081 if (hg > 0) { /* become sync source. */
3082 rv = C_WF_BITMAP_S;
3083 } else if (hg < 0) { /* become sync target */
3084 rv = C_WF_BITMAP_T;
3085 } else {
3086 rv = C_CONNECTED;
3087 if (drbd_bm_total_weight(mdev)) {
3088 dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
3089 drbd_bm_total_weight(mdev));
3090 }
3091 }
3092
3093 return rv;
3094}
3095
Philipp Reisnerf179d762011-05-16 17:31:47 +02003096static enum drbd_after_sb_p convert_after_sb(enum drbd_after_sb_p peer)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003097{
3098 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003099 if (peer == ASB_DISCARD_REMOTE)
3100 return ASB_DISCARD_LOCAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003101
3102 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003103 if (peer == ASB_DISCARD_LOCAL)
3104 return ASB_DISCARD_REMOTE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003105
3106 /* everything else is valid if they are equal on both sides. */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003107 return peer;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003108}
3109
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003110static int receive_protocol(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003111{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003112 struct p_protocol *p = pi->data;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003113 enum drbd_after_sb_p p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
3114 int p_proto, p_discard_my_data, p_two_primaries, cf;
3115 struct net_conf *nc, *old_net_conf, *new_net_conf = NULL;
3116 char integrity_alg[SHARED_SECRET_MAX] = "";
Andreas Gruenbacheraccdbcc2011-07-15 17:41:09 +02003117 struct crypto_hash *peer_integrity_tfm = NULL;
Philipp Reisner7aca6c72011-05-17 10:12:56 +02003118 void *int_dig_in = NULL, *int_dig_vv = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003119
Philipp Reisnerb411b362009-09-25 16:07:19 -07003120 p_proto = be32_to_cpu(p->protocol);
3121 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
3122 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
3123 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003124 p_two_primaries = be32_to_cpu(p->two_primaries);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003125 cf = be32_to_cpu(p->conn_flags);
Andreas Gruenbacher6139f602011-05-06 20:00:02 +02003126 p_discard_my_data = cf & CF_DISCARD_MY_DATA;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003127
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003128 if (tconn->agreed_pro_version >= 87) {
3129 int err;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003130
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003131 if (pi->size > sizeof(integrity_alg))
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003132 return -EIO;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003133 err = drbd_recv_all(tconn, integrity_alg, pi->size);
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003134 if (err)
3135 return err;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003136 integrity_alg[SHARED_SECRET_MAX - 1] = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003137 }
3138
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003139 if (pi->cmd != P_PROTOCOL_UPDATE) {
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003140 clear_bit(CONN_DRY_RUN, &tconn->flags);
Philipp Reisner036b17e2011-05-16 17:38:11 +02003141
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003142 if (cf & CF_DRY_RUN)
3143 set_bit(CONN_DRY_RUN, &tconn->flags);
3144
3145 rcu_read_lock();
3146 nc = rcu_dereference(tconn->net_conf);
3147
3148 if (p_proto != nc->wire_protocol) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003149 conn_err(tconn, "incompatible %s settings\n", "protocol");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003150 goto disconnect_rcu_unlock;
3151 }
3152
3153 if (convert_after_sb(p_after_sb_0p) != nc->after_sb_0p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003154 conn_err(tconn, "incompatible %s settings\n", "after-sb-0pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003155 goto disconnect_rcu_unlock;
3156 }
3157
3158 if (convert_after_sb(p_after_sb_1p) != nc->after_sb_1p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003159 conn_err(tconn, "incompatible %s settings\n", "after-sb-1pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003160 goto disconnect_rcu_unlock;
3161 }
3162
3163 if (convert_after_sb(p_after_sb_2p) != nc->after_sb_2p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003164 conn_err(tconn, "incompatible %s settings\n", "after-sb-2pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003165 goto disconnect_rcu_unlock;
3166 }
3167
3168 if (p_discard_my_data && nc->discard_my_data) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003169 conn_err(tconn, "incompatible %s settings\n", "discard-my-data");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003170 goto disconnect_rcu_unlock;
3171 }
3172
3173 if (p_two_primaries != nc->two_primaries) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003174 conn_err(tconn, "incompatible %s settings\n", "allow-two-primaries");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003175 goto disconnect_rcu_unlock;
3176 }
3177
3178 if (strcmp(integrity_alg, nc->integrity_alg)) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003179 conn_err(tconn, "incompatible %s settings\n", "data-integrity-alg");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003180 goto disconnect_rcu_unlock;
3181 }
3182
3183 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07003184 }
3185
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003186 if (integrity_alg[0]) {
3187 int hash_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003188
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003189 /*
3190 * We can only change the peer data integrity algorithm
3191 * here. Changing our own data integrity algorithm
3192 * requires that we send a P_PROTOCOL_UPDATE packet at
3193 * the same time; otherwise, the peer has no way to
3194 * tell between which packets the algorithm should
3195 * change.
3196 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003197
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003198 peer_integrity_tfm = crypto_alloc_hash(integrity_alg, 0, CRYPTO_ALG_ASYNC);
3199 if (!peer_integrity_tfm) {
3200 conn_err(tconn, "peer data-integrity-alg %s not supported\n",
3201 integrity_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003202 goto disconnect;
3203 }
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003204
3205 hash_size = crypto_hash_digestsize(peer_integrity_tfm);
3206 int_dig_in = kmalloc(hash_size, GFP_KERNEL);
3207 int_dig_vv = kmalloc(hash_size, GFP_KERNEL);
3208 if (!(int_dig_in && int_dig_vv)) {
3209 conn_err(tconn, "Allocation of buffers for data integrity checking failed\n");
3210 goto disconnect;
3211 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003212 }
3213
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003214 new_net_conf = kmalloc(sizeof(struct net_conf), GFP_KERNEL);
3215 if (!new_net_conf) {
3216 conn_err(tconn, "Allocation of new net_conf failed\n");
3217 goto disconnect;
3218 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003219
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003220 mutex_lock(&tconn->data.mutex);
3221 mutex_lock(&tconn->conf_update);
3222 old_net_conf = tconn->net_conf;
3223 *new_net_conf = *old_net_conf;
3224
3225 new_net_conf->wire_protocol = p_proto;
3226 new_net_conf->after_sb_0p = convert_after_sb(p_after_sb_0p);
3227 new_net_conf->after_sb_1p = convert_after_sb(p_after_sb_1p);
3228 new_net_conf->after_sb_2p = convert_after_sb(p_after_sb_2p);
3229 new_net_conf->two_primaries = p_two_primaries;
3230
3231 rcu_assign_pointer(tconn->net_conf, new_net_conf);
3232 mutex_unlock(&tconn->conf_update);
3233 mutex_unlock(&tconn->data.mutex);
3234
3235 crypto_free_hash(tconn->peer_integrity_tfm);
3236 kfree(tconn->int_dig_in);
3237 kfree(tconn->int_dig_vv);
3238 tconn->peer_integrity_tfm = peer_integrity_tfm;
3239 tconn->int_dig_in = int_dig_in;
3240 tconn->int_dig_vv = int_dig_vv;
3241
3242 if (strcmp(old_net_conf->integrity_alg, integrity_alg))
3243 conn_info(tconn, "peer data-integrity-alg: %s\n",
3244 integrity_alg[0] ? integrity_alg : "(none)");
3245
3246 synchronize_rcu();
3247 kfree(old_net_conf);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003248 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003249
Philipp Reisner44ed1672011-04-19 17:10:19 +02003250disconnect_rcu_unlock:
3251 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07003252disconnect:
Andreas Gruenbacherb792c352011-07-15 16:48:49 +02003253 crypto_free_hash(peer_integrity_tfm);
Philipp Reisner036b17e2011-05-16 17:38:11 +02003254 kfree(int_dig_in);
3255 kfree(int_dig_vv);
Philipp Reisner72046242011-03-15 18:51:47 +01003256 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003257 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003258}
3259
3260/* helper function
3261 * input: alg name, feature name
3262 * return: NULL (alg name was "")
3263 * ERR_PTR(error) if something goes wrong
3264 * or the crypto hash ptr, if it worked out ok. */
3265struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
3266 const char *alg, const char *name)
3267{
3268 struct crypto_hash *tfm;
3269
3270 if (!alg[0])
3271 return NULL;
3272
3273 tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
3274 if (IS_ERR(tfm)) {
3275 dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
3276 alg, name, PTR_ERR(tfm));
3277 return tfm;
3278 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003279 return tfm;
3280}
3281
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003282static int ignore_remaining_packet(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003283{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003284 void *buffer = tconn->data.rbuf;
3285 int size = pi->size;
3286
3287 while (size) {
3288 int s = min_t(int, size, DRBD_SOCKET_BUFFER_SIZE);
3289 s = drbd_recv(tconn, buffer, s);
3290 if (s <= 0) {
3291 if (s < 0)
3292 return s;
3293 break;
3294 }
3295 size -= s;
3296 }
3297 if (size)
3298 return -EIO;
3299 return 0;
3300}
3301
3302/*
3303 * config_unknown_volume - device configuration command for unknown volume
3304 *
3305 * When a device is added to an existing connection, the node on which the
3306 * device is added first will send configuration commands to its peer but the
3307 * peer will not know about the device yet. It will warn and ignore these
3308 * commands. Once the device is added on the second node, the second node will
3309 * send the same device configuration commands, but in the other direction.
3310 *
3311 * (We can also end up here if drbd is misconfigured.)
3312 */
3313static int config_unknown_volume(struct drbd_tconn *tconn, struct packet_info *pi)
3314{
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02003315 conn_warn(tconn, "%s packet received for volume %u, which is not configured locally\n",
3316 cmdname(pi->cmd), pi->vnr);
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003317 return ignore_remaining_packet(tconn, pi);
3318}
3319
3320static int receive_SyncParam(struct drbd_tconn *tconn, struct packet_info *pi)
3321{
3322 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003323 struct p_rs_param_95 *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003324 unsigned int header_size, data_size, exp_max_sz;
3325 struct crypto_hash *verify_tfm = NULL;
3326 struct crypto_hash *csums_tfm = NULL;
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003327 struct net_conf *old_net_conf, *new_net_conf = NULL;
Philipp Reisner813472c2011-05-03 16:47:02 +02003328 struct disk_conf *old_disk_conf = NULL, *new_disk_conf = NULL;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003329 const int apv = tconn->agreed_pro_version;
Philipp Reisner813472c2011-05-03 16:47:02 +02003330 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
Philipp Reisner778f2712010-07-06 11:14:00 +02003331 int fifo_size = 0;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003332 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003333
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003334 mdev = vnr_to_mdev(tconn, pi->vnr);
3335 if (!mdev)
3336 return config_unknown_volume(tconn, pi);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003337
3338 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
3339 : apv == 88 ? sizeof(struct p_rs_param)
3340 + SHARED_SECRET_MAX
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003341 : apv <= 94 ? sizeof(struct p_rs_param_89)
3342 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003343
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003344 if (pi->size > exp_max_sz) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003345 dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003346 pi->size, exp_max_sz);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003347 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003348 }
3349
3350 if (apv <= 88) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003351 header_size = sizeof(struct p_rs_param);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003352 data_size = pi->size - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003353 } else if (apv <= 94) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003354 header_size = sizeof(struct p_rs_param_89);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003355 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003356 D_ASSERT(data_size == 0);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003357 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003358 header_size = sizeof(struct p_rs_param_95);
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);
3361 }
3362
3363 /* initialize verify_alg and csums_alg */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003364 p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003365 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
3366
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003367 err = drbd_recv_all(mdev->tconn, p, header_size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003368 if (err)
3369 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003370
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003371 mutex_lock(&mdev->tconn->conf_update);
3372 old_net_conf = mdev->tconn->net_conf;
Philipp Reisner813472c2011-05-03 16:47:02 +02003373 if (get_ldev(mdev)) {
3374 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3375 if (!new_disk_conf) {
3376 put_ldev(mdev);
3377 mutex_unlock(&mdev->tconn->conf_update);
3378 dev_err(DEV, "Allocation of new disk_conf failed\n");
3379 return -ENOMEM;
3380 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003381
Philipp Reisner813472c2011-05-03 16:47:02 +02003382 old_disk_conf = mdev->ldev->disk_conf;
3383 *new_disk_conf = *old_disk_conf;
3384
Andreas Gruenbacher6394b932011-05-11 14:29:52 +02003385 new_disk_conf->resync_rate = be32_to_cpu(p->resync_rate);
Philipp Reisner813472c2011-05-03 16:47:02 +02003386 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003387
3388 if (apv >= 88) {
3389 if (apv == 88) {
Philipp Reisner5de73822012-03-28 10:17:32 +02003390 if (data_size > SHARED_SECRET_MAX || data_size == 0) {
3391 dev_err(DEV, "verify-alg of wrong size, "
3392 "peer wants %u, accepting only up to %u byte\n",
3393 data_size, SHARED_SECRET_MAX);
Philipp Reisner813472c2011-05-03 16:47:02 +02003394 err = -EIO;
3395 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003396 }
3397
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003398 err = drbd_recv_all(mdev->tconn, p->verify_alg, data_size);
Philipp Reisner813472c2011-05-03 16:47:02 +02003399 if (err)
3400 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003401 /* we expect NUL terminated string */
3402 /* but just in case someone tries to be evil */
3403 D_ASSERT(p->verify_alg[data_size-1] == 0);
3404 p->verify_alg[data_size-1] = 0;
3405
3406 } else /* apv >= 89 */ {
3407 /* we still expect NUL terminated strings */
3408 /* but just in case someone tries to be evil */
3409 D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
3410 D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
3411 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
3412 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
3413 }
3414
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003415 if (strcmp(old_net_conf->verify_alg, p->verify_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003416 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3417 dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003418 old_net_conf->verify_alg, p->verify_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003419 goto disconnect;
3420 }
3421 verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
3422 p->verify_alg, "verify-alg");
3423 if (IS_ERR(verify_tfm)) {
3424 verify_tfm = NULL;
3425 goto disconnect;
3426 }
3427 }
3428
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003429 if (apv >= 89 && strcmp(old_net_conf->csums_alg, p->csums_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003430 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3431 dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003432 old_net_conf->csums_alg, p->csums_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003433 goto disconnect;
3434 }
3435 csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
3436 p->csums_alg, "csums-alg");
3437 if (IS_ERR(csums_tfm)) {
3438 csums_tfm = NULL;
3439 goto disconnect;
3440 }
3441 }
3442
Philipp Reisner813472c2011-05-03 16:47:02 +02003443 if (apv > 94 && new_disk_conf) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003444 new_disk_conf->c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
3445 new_disk_conf->c_delay_target = be32_to_cpu(p->c_delay_target);
3446 new_disk_conf->c_fill_target = be32_to_cpu(p->c_fill_target);
3447 new_disk_conf->c_max_rate = be32_to_cpu(p->c_max_rate);
Philipp Reisner778f2712010-07-06 11:14:00 +02003448
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003449 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Philipp Reisner9958c852011-05-03 16:19:31 +02003450 if (fifo_size != mdev->rs_plan_s->size) {
Philipp Reisner813472c2011-05-03 16:47:02 +02003451 new_plan = fifo_alloc(fifo_size);
3452 if (!new_plan) {
Philipp Reisner778f2712010-07-06 11:14:00 +02003453 dev_err(DEV, "kmalloc of fifo_buffer failed");
Lars Ellenbergf3990022011-03-23 14:31:09 +01003454 put_ldev(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02003455 goto disconnect;
3456 }
3457 }
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003458 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003459
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003460 if (verify_tfm || csums_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003461 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
3462 if (!new_net_conf) {
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003463 dev_err(DEV, "Allocation of new net_conf failed\n");
3464 goto disconnect;
3465 }
3466
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003467 *new_net_conf = *old_net_conf;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003468
3469 if (verify_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003470 strcpy(new_net_conf->verify_alg, p->verify_alg);
3471 new_net_conf->verify_alg_len = strlen(p->verify_alg) + 1;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003472 crypto_free_hash(mdev->tconn->verify_tfm);
3473 mdev->tconn->verify_tfm = verify_tfm;
3474 dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
3475 }
3476 if (csums_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003477 strcpy(new_net_conf->csums_alg, p->csums_alg);
3478 new_net_conf->csums_alg_len = strlen(p->csums_alg) + 1;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003479 crypto_free_hash(mdev->tconn->csums_tfm);
3480 mdev->tconn->csums_tfm = csums_tfm;
3481 dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
3482 }
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003483 rcu_assign_pointer(tconn->net_conf, new_net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003484 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003485 }
3486
Philipp Reisner813472c2011-05-03 16:47:02 +02003487 if (new_disk_conf) {
3488 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3489 put_ldev(mdev);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003490 }
Philipp Reisner813472c2011-05-03 16:47:02 +02003491
3492 if (new_plan) {
3493 old_plan = mdev->rs_plan_s;
3494 rcu_assign_pointer(mdev->rs_plan_s, new_plan);
3495 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003496
3497 mutex_unlock(&mdev->tconn->conf_update);
3498 synchronize_rcu();
3499 if (new_net_conf)
3500 kfree(old_net_conf);
3501 kfree(old_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02003502 kfree(old_plan);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003503
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003504 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003505
Philipp Reisner813472c2011-05-03 16:47:02 +02003506reconnect:
3507 if (new_disk_conf) {
3508 put_ldev(mdev);
3509 kfree(new_disk_conf);
3510 }
3511 mutex_unlock(&mdev->tconn->conf_update);
3512 return -EIO;
3513
Philipp Reisnerb411b362009-09-25 16:07:19 -07003514disconnect:
Philipp Reisner813472c2011-05-03 16:47:02 +02003515 kfree(new_plan);
3516 if (new_disk_conf) {
3517 put_ldev(mdev);
3518 kfree(new_disk_conf);
3519 }
Philipp Reisnera0095502011-05-03 13:14:15 +02003520 mutex_unlock(&mdev->tconn->conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003521 /* just for completeness: actually not needed,
3522 * as this is not reached if csums_tfm was ok. */
3523 crypto_free_hash(csums_tfm);
3524 /* but free the verify_tfm again, if csums_tfm did not work out */
3525 crypto_free_hash(verify_tfm);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003526 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003527 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003528}
3529
Philipp Reisnerb411b362009-09-25 16:07:19 -07003530/* warn if the arguments differ by more than 12.5% */
3531static void warn_if_differ_considerably(struct drbd_conf *mdev,
3532 const char *s, sector_t a, sector_t b)
3533{
3534 sector_t d;
3535 if (a == 0 || b == 0)
3536 return;
3537 d = (a > b) ? (a - b) : (b - a);
3538 if (d > (a>>3) || d > (b>>3))
3539 dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
3540 (unsigned long long)a, (unsigned long long)b);
3541}
3542
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003543static int receive_sizes(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003544{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003545 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003546 struct p_sizes *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003547 enum determine_dev_size dd = unchanged;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003548 sector_t p_size, p_usize, my_usize;
3549 int ldsc = 0; /* local disk size changed */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003550 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003551
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003552 mdev = vnr_to_mdev(tconn, pi->vnr);
3553 if (!mdev)
3554 return config_unknown_volume(tconn, pi);
3555
Philipp Reisnerb411b362009-09-25 16:07:19 -07003556 p_size = be64_to_cpu(p->d_size);
3557 p_usize = be64_to_cpu(p->u_size);
3558
Philipp Reisnerb411b362009-09-25 16:07:19 -07003559 /* just store the peer's disk size for now.
3560 * we still need to figure out whether we accept that. */
3561 mdev->p_size = p_size;
3562
Philipp Reisnerb411b362009-09-25 16:07:19 -07003563 if (get_ldev(mdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003564 rcu_read_lock();
3565 my_usize = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
3566 rcu_read_unlock();
3567
Philipp Reisnerb411b362009-09-25 16:07:19 -07003568 warn_if_differ_considerably(mdev, "lower level device sizes",
3569 p_size, drbd_get_max_capacity(mdev->ldev));
3570 warn_if_differ_considerably(mdev, "user requested size",
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003571 p_usize, my_usize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003572
3573 /* if this is the first connect, or an otherwise expected
3574 * param exchange, choose the minimum */
3575 if (mdev->state.conn == C_WF_REPORT_PARAMS)
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003576 p_usize = min_not_zero(my_usize, p_usize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003577
3578 /* Never shrink a device with usable data during connect.
3579 But allow online shrinking if we are connected. */
Philipp Reisneref5e44a2011-05-03 13:27:43 +02003580 if (drbd_new_dev_size(mdev, mdev->ldev, p_usize, 0) <
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003581 drbd_get_capacity(mdev->this_bdev) &&
3582 mdev->state.disk >= D_OUTDATED &&
3583 mdev->state.conn < C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003584 dev_err(DEV, "The peer's disk size is too small!\n");
Philipp Reisner38fa9982011-03-15 18:24:49 +01003585 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003586 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003587 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003588 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003589
3590 if (my_usize != p_usize) {
3591 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
3592
3593 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3594 if (!new_disk_conf) {
3595 dev_err(DEV, "Allocation of new disk_conf failed\n");
3596 put_ldev(mdev);
3597 return -ENOMEM;
3598 }
3599
3600 mutex_lock(&mdev->tconn->conf_update);
3601 old_disk_conf = mdev->ldev->disk_conf;
3602 *new_disk_conf = *old_disk_conf;
3603 new_disk_conf->disk_size = p_usize;
3604
3605 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3606 mutex_unlock(&mdev->tconn->conf_update);
3607 synchronize_rcu();
3608 kfree(old_disk_conf);
3609
3610 dev_info(DEV, "Peer sets u_size to %lu sectors\n",
3611 (unsigned long)my_usize);
3612 }
3613
Philipp Reisnerb411b362009-09-25 16:07:19 -07003614 put_ldev(mdev);
3615 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003616
Philipp Reisnere89b5912010-03-24 17:11:33 +01003617 ddsf = be16_to_cpu(p->dds_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003618 if (get_ldev(mdev)) {
Bart Van Assche24c48302011-05-21 18:32:29 +02003619 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003620 put_ldev(mdev);
3621 if (dd == dev_size_error)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003622 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003623 drbd_md_sync(mdev);
3624 } else {
3625 /* I am diskless, need to accept the peer's size. */
3626 drbd_set_my_capacity(mdev, p_size);
3627 }
3628
Philipp Reisner99432fc2011-05-20 16:39:13 +02003629 mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size);
3630 drbd_reconsider_max_bio_size(mdev);
3631
Philipp Reisnerb411b362009-09-25 16:07:19 -07003632 if (get_ldev(mdev)) {
3633 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
3634 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
3635 ldsc = 1;
3636 }
3637
Philipp Reisnerb411b362009-09-25 16:07:19 -07003638 put_ldev(mdev);
3639 }
3640
3641 if (mdev->state.conn > C_WF_REPORT_PARAMS) {
3642 if (be64_to_cpu(p->c_size) !=
3643 drbd_get_capacity(mdev->this_bdev) || ldsc) {
3644 /* we have different sizes, probably peer
3645 * needs to know my new size... */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003646 drbd_send_sizes(mdev, 0, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003647 }
3648 if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
3649 (dd == grew && mdev->state.conn == C_CONNECTED)) {
3650 if (mdev->state.pdsk >= D_INCONSISTENT &&
Philipp Reisnere89b5912010-03-24 17:11:33 +01003651 mdev->state.disk >= D_INCONSISTENT) {
3652 if (ddsf & DDSF_NO_RESYNC)
3653 dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
3654 else
3655 resync_after_online_grow(mdev);
3656 } else
Philipp Reisnerb411b362009-09-25 16:07:19 -07003657 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
3658 }
3659 }
3660
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003661 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003662}
3663
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003664static int receive_uuids(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003665{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003666 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003667 struct p_uuids *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003668 u64 *p_uuid;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003669 int i, updated_uuids = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003670
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003671 mdev = vnr_to_mdev(tconn, pi->vnr);
3672 if (!mdev)
3673 return config_unknown_volume(tconn, pi);
3674
Philipp Reisnerb411b362009-09-25 16:07:19 -07003675 p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
Jing Wang063eacf2012-10-25 15:00:56 +08003676 if (!p_uuid) {
3677 dev_err(DEV, "kmalloc of p_uuid failed\n");
3678 return false;
3679 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003680
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
3792 rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003793 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 Reisnerb411b362009-09-25 16:07:19 -07003801{
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
Lars Ellenberg545752d2011-12-05 14:39:25 +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;
Lars Ellenberg545752d2011-12-05 14:39:25 +01003855
Lars Ellenberg40424e42011-09-26 15:24:56 +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 Ellenberg02b91b52012-06-28 18:26:52 +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) {
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003889 ov_out_of_sync_print(mdev);
Lars Ellenberg02b91b52012-06-28 18:26:52 +02003890 drbd_resync_finished(mdev);
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003891 return 0;
Lars Ellenberg02b91b52012-06-28 18:26:52 +02003892 }
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);
Lars Ellenbergf479ea02011-10-27 16:52:30 +02003989 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 Reisnerb411b362009-09-25 16:07:19 -07004308
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004309 return ignore_remaining_packet(tconn, pi);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004310}
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 Ellenbergf735e3632010-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[] = {
4352 [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 },
Philipp Reisner02918be2010-08-20 14:35:10 +02004359 [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 },
Philipp Reisner02918be2010-08-20 14:35:10 +02004363 [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 },
Philipp Reisner73a01a12010-10-27 14:33:00 +02004373 [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004374 [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 Reisnerf70b35112010-06-24 14:34:40 +02004437{
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 Reisnerf70b35112010-06-24 14:34:40 +02004441
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
Lars Ellenberg545752d2011-12-05 14:39:25 +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 */
Philipp Reisnerb8853db2011-12-13 11:09:16 +01004450 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
Lars Ellenberg545752d2011-12-05 14:39:25 +01004451
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" */
4521 drbd_flush_workqueue(mdev);
4522
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
Lars Ellenberg08332d72012-08-17 15:09:13 +02004530 /* need to do it again, drbd_finish_peer_reqs() may have populated it
4531 * again via drbd_try_clear_on_disk_bm(). */
4532 drbd_rs_cancel_all(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004533
4534 kfree(mdev->p_uuid);
4535 mdev->p_uuid = NULL;
4536
Philipp Reisner2aebfab2011-03-28 16:48:11 +02004537 if (!drbd_suspended(mdev))
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01004538 tl_clear(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004539
4540 drbd_md_sync(mdev);
4541
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004542 /* serialize with bitmap writeout triggered by the state change,
4543 * if any. */
4544 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
4545
Philipp Reisnerb411b362009-09-25 16:07:19 -07004546 /* tcp_close and release of sendpage pages can be deferred. I don't
4547 * want to use SO_LINGER, because apparently it can be deferred for
4548 * more than 20 seconds (longest time I checked).
4549 *
4550 * Actually we don't care for exactly when the network stack does its
4551 * put_page(), but release our reference on these pages right here.
4552 */
Andreas Gruenbacher7721f562011-04-06 17:14:02 +02004553 i = drbd_free_peer_reqs(mdev, &mdev->net_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004554 if (i)
4555 dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
Lars Ellenberg435f0742010-09-06 12:30:25 +02004556 i = atomic_read(&mdev->pp_in_use_by_net);
4557 if (i)
4558 dev_info(DEV, "pp_in_use_by_net = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004559 i = atomic_read(&mdev->pp_in_use);
4560 if (i)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02004561 dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004562
4563 D_ASSERT(list_empty(&mdev->read_ee));
4564 D_ASSERT(list_empty(&mdev->active_ee));
4565 D_ASSERT(list_empty(&mdev->sync_ee));
4566 D_ASSERT(list_empty(&mdev->done_ee));
4567
Philipp Reisner360cc742011-02-08 14:29:53 +01004568 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004569}
4570
4571/*
4572 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
4573 * we can agree on is stored in agreed_pro_version.
4574 *
4575 * feature flags and the reserved array should be enough room for future
4576 * enhancements of the handshake protocol, and possible plugins...
4577 *
4578 * for now, they are expected to be zero, but ignored.
4579 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004580static int drbd_send_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004581{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004582 struct drbd_socket *sock;
4583 struct p_connection_features *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004584
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004585 sock = &tconn->data;
4586 p = conn_prepare_command(tconn, sock);
4587 if (!p)
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004588 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004589 memset(p, 0, sizeof(*p));
4590 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
4591 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004592 return conn_send_command(tconn, sock, P_CONNECTION_FEATURES, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004593}
4594
4595/*
4596 * return values:
4597 * 1 yes, we have a valid connection
4598 * 0 oops, did not work out, please try again
4599 * -1 peer talks different language,
4600 * no point in trying again, please go standalone.
4601 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004602static int drbd_do_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004603{
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004604 /* ASSERT current == tconn->receiver ... */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004605 struct p_connection_features *p;
4606 const int expect = sizeof(struct p_connection_features);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004607 struct packet_info pi;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004608 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004609
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004610 err = drbd_send_features(tconn);
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004611 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004612 return 0;
4613
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004614 err = drbd_recv_header(tconn, &pi);
4615 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004616 return 0;
4617
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004618 if (pi.cmd != P_CONNECTION_FEATURES) {
4619 conn_err(tconn, "expected ConnectionFeatures packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004620 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004621 return -1;
4622 }
4623
Philipp Reisner77351055b2011-02-07 17:24:26 +01004624 if (pi.size != expect) {
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004625 conn_err(tconn, "expected ConnectionFeatures length: %u, received: %u\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004626 expect, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004627 return -1;
4628 }
4629
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004630 p = pi.data;
4631 err = drbd_recv_all_warn(tconn, p, expect);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004632 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004633 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004634
Philipp Reisnerb411b362009-09-25 16:07:19 -07004635 p->protocol_min = be32_to_cpu(p->protocol_min);
4636 p->protocol_max = be32_to_cpu(p->protocol_max);
4637 if (p->protocol_max == 0)
4638 p->protocol_max = p->protocol_min;
4639
4640 if (PRO_VERSION_MAX < p->protocol_min ||
4641 PRO_VERSION_MIN > p->protocol_max)
4642 goto incompat;
4643
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004644 tconn->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004645
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004646 conn_info(tconn, "Handshake successful: "
4647 "Agreed network protocol version %d\n", tconn->agreed_pro_version);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004648
4649 return 1;
4650
4651 incompat:
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004652 conn_err(tconn, "incompatible DRBD dialects: "
Philipp Reisnerb411b362009-09-25 16:07:19 -07004653 "I support %d-%d, peer supports %d-%d\n",
4654 PRO_VERSION_MIN, PRO_VERSION_MAX,
4655 p->protocol_min, p->protocol_max);
4656 return -1;
4657}
4658
4659#if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
Philipp Reisner13e60372011-02-08 09:54:40 +01004660static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004661{
4662 dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
4663 dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004664 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004665}
4666#else
4667#define CHALLENGE_LEN 64
Johannes Thomab10d96c2010-01-07 16:02:50 +01004668
4669/* Return value:
4670 1 - auth succeeded,
4671 0 - failed, try again (network error),
4672 -1 - auth failed, don't try again.
4673*/
4674
Philipp Reisner13e60372011-02-08 09:54:40 +01004675static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004676{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004677 struct drbd_socket *sock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004678 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
4679 struct scatterlist sg;
4680 char *response = NULL;
4681 char *right_response = NULL;
4682 char *peers_ch = NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +02004683 unsigned int key_len;
4684 char secret[SHARED_SECRET_MAX]; /* 64 byte */
Philipp Reisnerb411b362009-09-25 16:07:19 -07004685 unsigned int resp_size;
4686 struct hash_desc desc;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004687 struct packet_info pi;
Philipp Reisner44ed1672011-04-19 17:10:19 +02004688 struct net_conf *nc;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004689 int err, rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004690
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004691 /* FIXME: Put the challenge/response into the preallocated socket buffer. */
4692
Philipp Reisner44ed1672011-04-19 17:10:19 +02004693 rcu_read_lock();
4694 nc = rcu_dereference(tconn->net_conf);
4695 key_len = strlen(nc->shared_secret);
4696 memcpy(secret, nc->shared_secret, key_len);
4697 rcu_read_unlock();
4698
Philipp Reisner13e60372011-02-08 09:54:40 +01004699 desc.tfm = tconn->cram_hmac_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004700 desc.flags = 0;
4701
Philipp Reisner44ed1672011-04-19 17:10:19 +02004702 rv = crypto_hash_setkey(tconn->cram_hmac_tfm, (u8 *)secret, key_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004703 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004704 conn_err(tconn, "crypto_hash_setkey() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004705 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004706 goto fail;
4707 }
4708
4709 get_random_bytes(my_challenge, CHALLENGE_LEN);
4710
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004711 sock = &tconn->data;
4712 if (!conn_prepare_command(tconn, sock)) {
4713 rv = 0;
4714 goto fail;
4715 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004716 rv = !conn_send_command(tconn, sock, P_AUTH_CHALLENGE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004717 my_challenge, CHALLENGE_LEN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004718 if (!rv)
4719 goto fail;
4720
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004721 err = drbd_recv_header(tconn, &pi);
4722 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004723 rv = 0;
4724 goto fail;
4725 }
4726
Philipp Reisner77351055b2011-02-07 17:24:26 +01004727 if (pi.cmd != P_AUTH_CHALLENGE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004728 conn_err(tconn, "expected AuthChallenge packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004729 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004730 rv = 0;
4731 goto fail;
4732 }
4733
Philipp Reisner77351055b2011-02-07 17:24:26 +01004734 if (pi.size > CHALLENGE_LEN * 2) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004735 conn_err(tconn, "expected AuthChallenge payload too big.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004736 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004737 goto fail;
4738 }
4739
Philipp Reisner77351055b2011-02-07 17:24:26 +01004740 peers_ch = kmalloc(pi.size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004741 if (peers_ch == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004742 conn_err(tconn, "kmalloc of peers_ch failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004743 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004744 goto fail;
4745 }
4746
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004747 err = drbd_recv_all_warn(tconn, peers_ch, pi.size);
4748 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004749 rv = 0;
4750 goto fail;
4751 }
4752
Philipp Reisner13e60372011-02-08 09:54:40 +01004753 resp_size = crypto_hash_digestsize(tconn->cram_hmac_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004754 response = kmalloc(resp_size, GFP_NOIO);
4755 if (response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004756 conn_err(tconn, "kmalloc of response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004757 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004758 goto fail;
4759 }
4760
4761 sg_init_table(&sg, 1);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004762 sg_set_buf(&sg, peers_ch, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004763
4764 rv = crypto_hash_digest(&desc, &sg, sg.length, response);
4765 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004766 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004767 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004768 goto fail;
4769 }
4770
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004771 if (!conn_prepare_command(tconn, sock)) {
4772 rv = 0;
4773 goto fail;
4774 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004775 rv = !conn_send_command(tconn, sock, P_AUTH_RESPONSE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004776 response, resp_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004777 if (!rv)
4778 goto fail;
4779
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004780 err = drbd_recv_header(tconn, &pi);
4781 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004782 rv = 0;
4783 goto fail;
4784 }
4785
Philipp Reisner77351055b2011-02-07 17:24:26 +01004786 if (pi.cmd != P_AUTH_RESPONSE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004787 conn_err(tconn, "expected AuthResponse packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004788 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004789 rv = 0;
4790 goto fail;
4791 }
4792
Philipp Reisner77351055b2011-02-07 17:24:26 +01004793 if (pi.size != resp_size) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004794 conn_err(tconn, "expected AuthResponse payload of wrong size\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004795 rv = 0;
4796 goto fail;
4797 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004798
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004799 err = drbd_recv_all_warn(tconn, response , resp_size);
4800 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004801 rv = 0;
4802 goto fail;
4803 }
4804
4805 right_response = kmalloc(resp_size, GFP_NOIO);
Julia Lawall2d1ee872009-12-27 22:27:11 +01004806 if (right_response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004807 conn_err(tconn, "kmalloc of right_response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004808 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004809 goto fail;
4810 }
4811
4812 sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
4813
4814 rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
4815 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004816 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004817 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004818 goto fail;
4819 }
4820
4821 rv = !memcmp(response, right_response, resp_size);
4822
4823 if (rv)
Philipp Reisner44ed1672011-04-19 17:10:19 +02004824 conn_info(tconn, "Peer authenticated using %d bytes HMAC\n",
4825 resp_size);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004826 else
4827 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004828
4829 fail:
4830 kfree(peers_ch);
4831 kfree(response);
4832 kfree(right_response);
4833
4834 return rv;
4835}
4836#endif
4837
4838int drbdd_init(struct drbd_thread *thi)
4839{
Philipp Reisner392c8802011-02-09 10:33:31 +01004840 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004841 int h;
4842
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004843 conn_info(tconn, "receiver (re)started\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004844
4845 do {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004846 h = conn_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004847 if (h == 0) {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004848 conn_disconnect(tconn);
Philipp Reisner20ee6392011-01-18 15:28:59 +01004849 schedule_timeout_interruptible(HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004850 }
4851 if (h == -1) {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004852 conn_warn(tconn, "Discarding network configuration.\n");
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004853 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004854 }
4855 } while (h == 0);
4856
Philipp Reisner91fd4da2011-04-20 17:47:29 +02004857 if (h > 0)
4858 drbdd(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004859
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004860 conn_disconnect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004861
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004862 conn_info(tconn, "receiver terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004863 return 0;
4864}
4865
4866/* ********* acknowledge sender ******** */
4867
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004868static int got_conn_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004869{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004870 struct p_req_state_reply *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004871 int retcode = be32_to_cpu(p->retcode);
4872
4873 if (retcode >= SS_SUCCESS) {
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004874 set_bit(CONN_WD_ST_CHG_OKAY, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004875 } else {
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004876 set_bit(CONN_WD_ST_CHG_FAIL, &tconn->flags);
4877 conn_err(tconn, "Requested state change failed by peer: %s (%d)\n",
4878 drbd_set_st_err_str(retcode), retcode);
4879 }
4880 wake_up(&tconn->ping_wait);
4881
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004882 return 0;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004883}
4884
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004885static int got_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004886{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004887 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004888 struct p_req_state_reply *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004889 int retcode = be32_to_cpu(p->retcode);
4890
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004891 mdev = vnr_to_mdev(tconn, pi->vnr);
4892 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004893 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004894
Philipp Reisner4d0fc3f2012-01-20 13:52:27 +01004895 if (test_bit(CONN_WD_ST_CHG_REQ, &tconn->flags)) {
4896 D_ASSERT(tconn->agreed_pro_version < 100);
4897 return got_conn_RqSReply(tconn, pi);
4898 }
4899
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004900 if (retcode >= SS_SUCCESS) {
4901 set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
4902 } else {
4903 set_bit(CL_ST_CHG_FAIL, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004904 dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004905 drbd_set_st_err_str(retcode), retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004906 }
4907 wake_up(&mdev->state_wait);
4908
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004909 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004910}
4911
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004912static int got_Ping(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004913{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004914 return drbd_send_ping_ack(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004915
4916}
4917
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004918static int got_PingAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004919{
4920 /* restore idle timeout */
Philipp Reisner2a67d8b2011-02-09 14:10:32 +01004921 tconn->meta.socket->sk->sk_rcvtimeo = tconn->net_conf->ping_int*HZ;
4922 if (!test_and_set_bit(GOT_PING_ACK, &tconn->flags))
4923 wake_up(&tconn->ping_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004924
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004925 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004926}
4927
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004928static int got_IsInSync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004929{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004930 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004931 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004932 sector_t sector = be64_to_cpu(p->sector);
4933 int blksize = be32_to_cpu(p->blksize);
4934
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004935 mdev = vnr_to_mdev(tconn, pi->vnr);
4936 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004937 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004938
Philipp Reisner31890f42011-01-19 14:12:51 +01004939 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004940
4941 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4942
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004943 if (get_ldev(mdev)) {
4944 drbd_rs_complete_io(mdev, sector);
4945 drbd_set_in_sync(mdev, sector, blksize);
4946 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
4947 mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
4948 put_ldev(mdev);
4949 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004950 dec_rs_pending(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02004951 atomic_add(blksize >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004952
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004953 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004954}
4955
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004956static int
4957validate_req_change_req_state(struct drbd_conf *mdev, u64 id, sector_t sector,
4958 struct rb_root *root, const char *func,
4959 enum drbd_req_event what, bool missing_ok)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004960{
4961 struct drbd_request *req;
4962 struct bio_and_error m;
4963
Philipp Reisner87eeee42011-01-19 14:16:30 +01004964 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004965 req = find_request(mdev, root, id, sector, missing_ok, func);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004966 if (unlikely(!req)) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01004967 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004968 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004969 }
4970 __req_mod(req, what, &m);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004971 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004972
4973 if (m.bio)
4974 complete_master_bio(mdev, &m);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004975 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004976}
4977
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004978static int got_BlockAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004979{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004980 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004981 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004982 sector_t sector = be64_to_cpu(p->sector);
4983 int blksize = be32_to_cpu(p->blksize);
4984 enum drbd_req_event what;
4985
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004986 mdev = vnr_to_mdev(tconn, pi->vnr);
4987 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004988 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004989
Philipp Reisnerb411b362009-09-25 16:07:19 -07004990 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4991
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004992 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004993 drbd_set_in_sync(mdev, sector, blksize);
4994 dec_rs_pending(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004995 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004996 }
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004997 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004998 case P_RS_WRITE_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004999 what = WRITE_ACKED_BY_PEER_AND_SIS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005000 break;
5001 case P_WRITE_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01005002 what = WRITE_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005003 break;
5004 case P_RECV_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01005005 what = RECV_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005006 break;
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02005007 case P_SUPERSEDED:
5008 what = CONFLICT_RESOLVED;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01005009 break;
5010 case P_RETRY_WRITE:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01005011 what = POSTPONE_WRITE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005012 break;
5013 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005014 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07005015 }
5016
5017 return validate_req_change_req_state(mdev, p->block_id, sector,
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005018 &mdev->write_requests, __func__,
5019 what, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005020}
5021
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005022static int got_NegAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005023{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005024 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005025 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005026 sector_t sector = be64_to_cpu(p->sector);
Philipp Reisner2deb8332011-01-17 18:39:18 +01005027 int size = be32_to_cpu(p->blksize);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02005028 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005029
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005030 mdev = vnr_to_mdev(tconn, pi->vnr);
5031 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005032 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005033
5034 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5035
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01005036 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07005037 dec_rs_pending(mdev);
5038 drbd_rs_failed_io(mdev, sector, size);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005039 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005040 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01005041
Andreas Gruenbacher85997672011-04-04 13:09:15 +02005042 err = validate_req_change_req_state(mdev, p->block_id, sector,
5043 &mdev->write_requests, __func__,
Philipp Reisner303d1442011-04-13 16:24:47 -07005044 NEG_ACKED, true);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02005045 if (err) {
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01005046 /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs.
5047 The master bio might already be completed, therefore the
5048 request is no longer in the collision hash. */
5049 /* In Protocol B we might already have got a P_RECV_ACK
5050 but then get a P_NEG_ACK afterwards. */
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01005051 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisner2deb8332011-01-17 18:39:18 +01005052 }
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005053 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005054}
5055
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005056static int got_NegDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005057{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005058 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005059 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005060 sector_t sector = be64_to_cpu(p->sector);
5061
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005062 mdev = vnr_to_mdev(tconn, pi->vnr);
5063 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005064 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005065
Philipp Reisnerb411b362009-09-25 16:07:19 -07005066 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01005067
Philipp Reisner380207d2011-11-11 12:31:20 +01005068 dev_err(DEV, "Got NegDReply; Sector %llus, len %u.\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -07005069 (unsigned long long)sector, be32_to_cpu(p->blksize));
5070
5071 return validate_req_change_req_state(mdev, p->block_id, sector,
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005072 &mdev->read_requests, __func__,
5073 NEG_ACKED, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005074}
5075
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005076static int got_NegRSDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005077{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005078 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005079 sector_t sector;
5080 int size;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005081 struct p_block_ack *p = pi->data;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005082
5083 mdev = vnr_to_mdev(tconn, pi->vnr);
5084 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005085 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005086
5087 sector = be64_to_cpu(p->sector);
5088 size = be32_to_cpu(p->blksize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005089
5090 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5091
5092 dec_rs_pending(mdev);
5093
5094 if (get_ldev_if_state(mdev, D_FAILED)) {
5095 drbd_rs_complete_io(mdev, sector);
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01005096 switch (pi->cmd) {
Philipp Reisnerd612d302010-12-27 10:53:28 +01005097 case P_NEG_RS_DREPLY:
5098 drbd_rs_failed_io(mdev, sector, size);
5099 case P_RS_CANCEL:
5100 break;
5101 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005102 BUG();
Philipp Reisnerd612d302010-12-27 10:53:28 +01005103 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005104 put_ldev(mdev);
5105 }
5106
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005107 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005108}
5109
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005110static int got_BarrierAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005111{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005112 struct p_barrier_ack *p = pi->data;
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02005113 struct drbd_conf *mdev;
5114 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005115
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02005116 tl_release(tconn, p->barrier, be32_to_cpu(p->set_size));
Philipp Reisnerb411b362009-09-25 16:07:19 -07005117
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02005118 rcu_read_lock();
5119 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
5120 if (mdev->state.conn == C_AHEAD &&
5121 atomic_read(&mdev->ap_in_flight) == 0 &&
5122 !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->flags)) {
5123 mdev->start_resync_timer.expires = jiffies + HZ;
5124 add_timer(&mdev->start_resync_timer);
5125 }
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02005126 }
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02005127 rcu_read_unlock();
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02005128
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005129 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005130}
5131
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005132static int got_OVResult(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005133{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005134 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005135 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005136 struct drbd_work *w;
5137 sector_t sector;
5138 int size;
5139
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005140 mdev = vnr_to_mdev(tconn, pi->vnr);
5141 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005142 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005143
Philipp Reisnerb411b362009-09-25 16:07:19 -07005144 sector = be64_to_cpu(p->sector);
5145 size = be32_to_cpu(p->blksize);
5146
5147 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5148
5149 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005150 drbd_ov_out_of_sync_found(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005151 else
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005152 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005153
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005154 if (!get_ldev(mdev))
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005155 return 0;
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005156
Philipp Reisnerb411b362009-09-25 16:07:19 -07005157 drbd_rs_complete_io(mdev, sector);
5158 dec_rs_pending(mdev);
5159
Lars Ellenbergea5442a2010-11-05 09:48:01 +01005160 --mdev->ov_left;
5161
5162 /* let's advance progress step marks only for every other megabyte */
5163 if ((mdev->ov_left & 0x200) == 0x200)
5164 drbd_advance_rs_marks(mdev, mdev->ov_left);
5165
5166 if (mdev->ov_left == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07005167 w = kmalloc(sizeof(*w), GFP_NOIO);
5168 if (w) {
5169 w->cb = w_ov_finished;
Philipp Reisnera21e9292011-02-08 15:08:49 +01005170 w->mdev = mdev;
Lars Ellenbergd5b27b02011-11-14 15:42:37 +01005171 drbd_queue_work(&mdev->tconn->sender_work, w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005172 } else {
5173 dev_err(DEV, "kmalloc(w) failed.");
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005174 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005175 drbd_resync_finished(mdev);
5176 }
5177 }
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005178 put_ldev(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005179 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005180}
5181
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005182static int got_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner0ced55a2010-04-30 15:26:20 +02005183{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005184 return 0;
Philipp Reisner0ced55a2010-04-30 15:26:20 +02005185}
5186
Andreas Gruenbachera990be42011-04-06 17:56:48 +02005187static int tconn_finish_peer_reqs(struct drbd_tconn *tconn)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005188{
Philipp Reisner082a3432011-03-15 16:05:42 +01005189 struct drbd_conf *mdev;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005190 int vnr, not_empty = 0;
Philipp Reisner32862ec2011-02-08 16:41:01 +01005191
5192 do {
5193 clear_bit(SIGNAL_ASENDER, &tconn->flags);
5194 flush_signals(current);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005195
5196 rcu_read_lock();
5197 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
5198 kref_get(&mdev->kref);
5199 rcu_read_unlock();
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07005200 if (drbd_finish_peer_reqs(mdev)) {
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005201 kref_put(&mdev->kref, &drbd_minor_destroy);
5202 return 1;
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07005203 }
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005204 kref_put(&mdev->kref, &drbd_minor_destroy);
5205 rcu_read_lock();
Philipp Reisner082a3432011-03-15 16:05:42 +01005206 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005207 set_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisner082a3432011-03-15 16:05:42 +01005208
5209 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005210 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisner082a3432011-03-15 16:05:42 +01005211 not_empty = !list_empty(&mdev->done_ee);
5212 if (not_empty)
5213 break;
5214 }
5215 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005216 rcu_read_unlock();
Philipp Reisner32862ec2011-02-08 16:41:01 +01005217 } while (not_empty);
5218
5219 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005220}
5221
5222struct asender_cmd {
5223 size_t pkt_size;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005224 int (*fn)(struct drbd_tconn *tconn, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005225};
5226
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005227static struct asender_cmd asender_tbl[] = {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005228 [P_PING] = { 0, got_Ping },
5229 [P_PING_ACK] = { 0, got_PingAck },
Philipp Reisnerb411b362009-09-25 16:07:19 -07005230 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5231 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5232 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02005233 [P_SUPERSEDED] = { sizeof(struct p_block_ack), got_BlockAck },
Philipp Reisnerb411b362009-09-25 16:07:19 -07005234 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
5235 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005236 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply },
Philipp Reisnerb411b362009-09-25 16:07:19 -07005237 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
5238 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
5239 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
5240 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
Philipp Reisner02918be2010-08-20 14:35:10 +02005241 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005242 [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply },
5243 [P_CONN_ST_CHG_REPLY]={ sizeof(struct p_req_state_reply), got_conn_RqSReply },
5244 [P_RETRY_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005245};
Philipp Reisnerb411b362009-09-25 16:07:19 -07005246
5247int drbd_asender(struct drbd_thread *thi)
5248{
Philipp Reisner392c8802011-02-09 10:33:31 +01005249 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005250 struct asender_cmd *cmd = NULL;
Philipp Reisner77351055b2011-02-07 17:24:26 +01005251 struct packet_info pi;
Philipp Reisner257d0af2011-01-26 12:15:29 +01005252 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005253 void *buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005254 int received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005255 unsigned int header_size = drbd_header_size(tconn);
5256 int expect = header_size;
Philipp Reisner44ed1672011-04-19 17:10:19 +02005257 bool ping_timeout_active = false;
5258 struct net_conf *nc;
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005259 int ping_timeo, tcp_cork, ping_int;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005260
5261 current->policy = SCHED_RR; /* Make this a realtime task! */
5262 current->rt_priority = 2; /* more important than all other tasks */
5263
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01005264 while (get_t_state(thi) == RUNNING) {
Philipp Reisner80822282011-02-08 12:46:30 +01005265 drbd_thread_current_set_cpu(thi);
Philipp Reisner44ed1672011-04-19 17:10:19 +02005266
5267 rcu_read_lock();
5268 nc = rcu_dereference(tconn->net_conf);
5269 ping_timeo = nc->ping_timeo;
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005270 tcp_cork = nc->tcp_cork;
Philipp Reisner44ed1672011-04-19 17:10:19 +02005271 ping_int = nc->ping_int;
5272 rcu_read_unlock();
5273
Philipp Reisner32862ec2011-02-08 16:41:01 +01005274 if (test_and_clear_bit(SEND_PING, &tconn->flags)) {
Andreas Gruenbachera17647a2011-04-01 12:49:42 +02005275 if (drbd_send_ping(tconn)) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005276 conn_err(tconn, "drbd_send_ping has failed\n");
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01005277 goto reconnect;
5278 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02005279 tconn->meta.socket->sk->sk_rcvtimeo = ping_timeo * HZ / 10;
5280 ping_timeout_active = true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005281 }
5282
Philipp Reisner32862ec2011-02-08 16:41:01 +01005283 /* TODO: conditionally cork; it may hurt latency if we cork without
5284 much to send */
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005285 if (tcp_cork)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005286 drbd_tcp_cork(tconn->meta.socket);
Andreas Gruenbachera990be42011-04-06 17:56:48 +02005287 if (tconn_finish_peer_reqs(tconn)) {
5288 conn_err(tconn, "tconn_finish_peer_reqs() failed\n");
Philipp Reisner32862ec2011-02-08 16:41:01 +01005289 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005290 }
5291 /* but unconditionally uncork unless disabled */
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005292 if (tcp_cork)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005293 drbd_tcp_uncork(tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005294
5295 /* short circuit, recv_msg would return EINTR anyways. */
5296 if (signal_pending(current))
5297 continue;
5298
Philipp Reisner32862ec2011-02-08 16:41:01 +01005299 rv = drbd_recv_short(tconn->meta.socket, buf, expect-received, 0);
5300 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005301
5302 flush_signals(current);
5303
5304 /* Note:
5305 * -EINTR (on meta) we got a signal
5306 * -EAGAIN (on meta) rcvtimeo expired
5307 * -ECONNRESET other side closed the connection
5308 * -ERESTARTSYS (on data) we got a signal
5309 * rv < 0 other than above: unexpected error!
5310 * rv == expected: full header or command
5311 * rv < expected: "woken" by signal during receive
5312 * rv == 0 : "connection shut down by peer"
5313 */
5314 if (likely(rv > 0)) {
5315 received += rv;
5316 buf += rv;
5317 } else if (rv == 0) {
Philipp Reisnerb66623e2012-08-08 21:19:09 +02005318 if (test_bit(DISCONNECT_SENT, &tconn->flags)) {
5319 long t;
5320 rcu_read_lock();
5321 t = rcu_dereference(tconn->net_conf)->ping_timeo * HZ/10;
5322 rcu_read_unlock();
5323
5324 t = wait_event_timeout(tconn->ping_wait,
5325 tconn->cstate < C_WF_REPORT_PARAMS,
5326 t);
Philipp Reisner599377a2012-08-17 14:50:22 +02005327 if (t)
5328 break;
5329 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005330 conn_err(tconn, "meta connection shut down by peer.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005331 goto reconnect;
5332 } else if (rv == -EAGAIN) {
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02005333 /* If the data socket received something meanwhile,
5334 * that is good enough: peer is still alive. */
Philipp Reisner32862ec2011-02-08 16:41:01 +01005335 if (time_after(tconn->last_received,
5336 jiffies - tconn->meta.socket->sk->sk_rcvtimeo))
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02005337 continue;
Lars Ellenbergf36af182011-03-09 22:44:55 +01005338 if (ping_timeout_active) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005339 conn_err(tconn, "PingAck did not arrive in time.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005340 goto reconnect;
5341 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005342 set_bit(SEND_PING, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005343 continue;
5344 } else if (rv == -EINTR) {
5345 continue;
5346 } else {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005347 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005348 goto reconnect;
5349 }
5350
5351 if (received == expect && cmd == NULL) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005352 if (decode_header(tconn, tconn->meta.rbuf, &pi))
Philipp Reisnerb411b362009-09-25 16:07:19 -07005353 goto reconnect;
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005354 cmd = &asender_tbl[pi.cmd];
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005355 if (pi.cmd >= ARRAY_SIZE(asender_tbl) || !cmd->fn) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02005356 conn_err(tconn, "Unexpected meta packet %s (0x%04x)\n",
5357 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005358 goto disconnect;
5359 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005360 expect = header_size + cmd->pkt_size;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005361 if (pi.size != expect - header_size) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005362 conn_err(tconn, "Wrong packet size on meta (c: %d, l: %d)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01005363 pi.cmd, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005364 goto reconnect;
Philipp Reisner257d0af2011-01-26 12:15:29 +01005365 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005366 }
5367 if (received == expect) {
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005368 bool err;
Philipp Reisnera4fbda82011-03-16 11:13:17 +01005369
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005370 err = cmd->fn(tconn, &pi);
5371 if (err) {
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005372 conn_err(tconn, "%pf failed\n", cmd->fn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005373 goto reconnect;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005374 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005375
Philipp Reisnera4fbda82011-03-16 11:13:17 +01005376 tconn->last_received = jiffies;
Lars Ellenbergf36af182011-03-09 22:44:55 +01005377
Philipp Reisner44ed1672011-04-19 17:10:19 +02005378 if (cmd == &asender_tbl[P_PING_ACK]) {
5379 /* restore idle timeout */
5380 tconn->meta.socket->sk->sk_rcvtimeo = ping_int * HZ;
5381 ping_timeout_active = false;
5382 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005383
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005384 buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005385 received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005386 expect = header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005387 cmd = NULL;
5388 }
5389 }
5390
5391 if (0) {
5392reconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01005393 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
Philipp Reisner19fffd72012-08-28 16:48:03 +02005394 conn_md_sync(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005395 }
5396 if (0) {
5397disconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01005398 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005399 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005400 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005401
Philipp Reisner32862ec2011-02-08 16:41:01 +01005402 conn_info(tconn, "asender terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005403
5404 return 0;
5405}