blob: 1599a1a6f1f70bfbaae1064a849eedfa19b3e48b [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);
1040 rcu_read_unlock();
Philipp Reisner08b165b2011-09-05 16:22:33 +02001041
1042 if (discard_my_data)
1043 set_bit(DISCARD_MY_DATA, &mdev->flags);
1044 else
1045 clear_bit(DISCARD_MY_DATA, &mdev->flags);
1046
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001047 drbd_connected(mdev);
1048 kref_put(&mdev->kref, &drbd_minor_destroy);
1049 rcu_read_lock();
1050 }
1051 rcu_read_unlock();
1052
Philipp Reisnera1096a62012-04-06 12:07:34 +02001053 rv = conn_request_state(tconn, NS(conn, C_WF_REPORT_PARAMS), CS_VERBOSE);
Lars Ellenberged635cb02012-11-05 11:54:30 +01001054 if (rv < SS_SUCCESS || tconn->cstate != C_WF_REPORT_PARAMS) {
Philipp Reisnera1096a62012-04-06 12:07:34 +02001055 clear_bit(STATE_SENT, &tconn->flags);
Philipp Reisner1e86ac42011-08-04 10:33:08 +02001056 return 0;
Philipp Reisnera1096a62012-04-06 12:07:34 +02001057 }
Philipp Reisner1e86ac42011-08-04 10:33:08 +02001058
Philipp Reisner823bd832012-11-08 15:04:36 +01001059 drbd_thread_start(&tconn->asender);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001060
Philipp Reisner08b165b2011-09-05 16:22:33 +02001061 mutex_lock(&tconn->conf_update);
1062 /* The discard_my_data flag is a single-shot modifier to the next
1063 * connection attempt, the handshake of which is now well underway.
1064 * No need for rcu style copying of the whole struct
1065 * just to clear a single value. */
1066 tconn->net_conf->discard_my_data = 0;
1067 mutex_unlock(&tconn->conf_update);
1068
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07001069 return h;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001070
1071out_release_sockets:
Philipp Reisner7a426fd2012-07-12 14:22:37 +02001072 if (ad.s_listen)
1073 sock_release(ad.s_listen);
Philipp Reisner7da35862011-12-19 22:42:56 +01001074 if (sock.socket)
1075 sock_release(sock.socket);
1076 if (msock.socket)
1077 sock_release(msock.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001078 return -1;
1079}
1080
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001081static int decode_header(struct drbd_tconn *tconn, void *header, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001082{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001083 unsigned int header_size = drbd_header_size(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001084
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +02001085 if (header_size == sizeof(struct p_header100) &&
1086 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC_100)) {
1087 struct p_header100 *h = header;
1088 if (h->pad != 0) {
1089 conn_err(tconn, "Header padding is not zero\n");
1090 return -EINVAL;
1091 }
1092 pi->vnr = be16_to_cpu(h->volume);
1093 pi->cmd = be16_to_cpu(h->command);
1094 pi->size = be32_to_cpu(h->length);
1095 } else if (header_size == sizeof(struct p_header95) &&
1096 *(__be16 *)header == cpu_to_be16(DRBD_MAGIC_BIG)) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001097 struct p_header95 *h = header;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001098 pi->cmd = be16_to_cpu(h->command);
Andreas Gruenbacherb55d84b2011-03-22 13:17:47 +01001099 pi->size = be32_to_cpu(h->length);
1100 pi->vnr = 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001101 } else if (header_size == sizeof(struct p_header80) &&
1102 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC)) {
1103 struct p_header80 *h = header;
1104 pi->cmd = be16_to_cpu(h->command);
1105 pi->size = be16_to_cpu(h->length);
Philipp Reisner77351055b2011-02-07 17:24:26 +01001106 pi->vnr = 0;
Philipp Reisner02918be2010-08-20 14:35:10 +02001107 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001108 conn_err(tconn, "Wrong magic value 0x%08x in protocol version %d\n",
1109 be32_to_cpu(*(__be32 *)header),
1110 tconn->agreed_pro_version);
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001111 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001112 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001113 pi->data = header + header_size;
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001114 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001115}
1116
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001117static int drbd_recv_header(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner257d0af2011-01-26 12:15:29 +01001118{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001119 void *buffer = tconn->data.rbuf;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001120 int err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001121
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001122 err = drbd_recv_all_warn(tconn, buffer, drbd_header_size(tconn));
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001123 if (err)
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001124 return err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001125
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001126 err = decode_header(tconn, buffer, pi);
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001127 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001128
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001129 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001130}
1131
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001132static void drbd_flush(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001133{
1134 int rv;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001135 struct drbd_conf *mdev;
1136 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001137
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001138 if (tconn->write_ordering >= WO_bdev_flush) {
Lars Ellenberg615e0872011-11-17 14:32:12 +01001139 rcu_read_lock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001140 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Lars Ellenberg615e0872011-11-17 14:32:12 +01001141 if (!get_ldev(mdev))
1142 continue;
1143 kref_get(&mdev->kref);
1144 rcu_read_unlock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001145
Lars Ellenberg615e0872011-11-17 14:32:12 +01001146 rv = blkdev_issue_flush(mdev->ldev->backing_bdev,
1147 GFP_NOIO, NULL);
1148 if (rv) {
1149 dev_info(DEV, "local disk flush failed with status %d\n", rv);
1150 /* would rather check on EOPNOTSUPP, but that is not reliable.
1151 * don't try again for ANY return value != 0
1152 * if (rv == -EOPNOTSUPP) */
1153 drbd_bump_write_ordering(tconn, WO_drain_io);
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001154 }
Lars Ellenberg615e0872011-11-17 14:32:12 +01001155 put_ldev(mdev);
1156 kref_put(&mdev->kref, &drbd_minor_destroy);
1157
1158 rcu_read_lock();
1159 if (rv)
1160 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001161 }
Lars Ellenberg615e0872011-11-17 14:32:12 +01001162 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001163 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001164}
1165
1166/**
1167 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
1168 * @mdev: DRBD device.
1169 * @epoch: Epoch object.
1170 * @ev: Epoch event.
1171 */
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001172static enum finish_epoch drbd_may_finish_epoch(struct drbd_tconn *tconn,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001173 struct drbd_epoch *epoch,
1174 enum epoch_event ev)
1175{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001176 int epoch_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001177 struct drbd_epoch *next_epoch;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001178 enum finish_epoch rv = FE_STILL_LIVE;
1179
Philipp Reisner12038a32011-11-09 19:18:00 +01001180 spin_lock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001181 do {
1182 next_epoch = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001183
1184 epoch_size = atomic_read(&epoch->epoch_size);
1185
1186 switch (ev & ~EV_CLEANUP) {
1187 case EV_PUT:
1188 atomic_dec(&epoch->active);
1189 break;
1190 case EV_GOT_BARRIER_NR:
1191 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001192 break;
1193 case EV_BECAME_LAST:
1194 /* nothing to do*/
1195 break;
1196 }
1197
Philipp Reisnerb411b362009-09-25 16:07:19 -07001198 if (epoch_size != 0 &&
1199 atomic_read(&epoch->active) == 0 &&
Philipp Reisner80f9fd52011-07-18 15:45:15 +02001200 (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags) || ev & EV_CLEANUP)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001201 if (!(ev & EV_CLEANUP)) {
Philipp Reisner12038a32011-11-09 19:18:00 +01001202 spin_unlock(&tconn->epoch_lock);
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001203 drbd_send_b_ack(epoch->tconn, epoch->barrier_nr, epoch_size);
Philipp Reisner12038a32011-11-09 19:18:00 +01001204 spin_lock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001205 }
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001206#if 0
1207 /* FIXME: dec unacked on connection, once we have
1208 * something to count pending connection packets in. */
Philipp Reisner80f9fd52011-07-18 15:45:15 +02001209 if (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags))
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001210 dec_unacked(epoch->tconn);
1211#endif
Philipp Reisnerb411b362009-09-25 16:07:19 -07001212
Philipp Reisner12038a32011-11-09 19:18:00 +01001213 if (tconn->current_epoch != epoch) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001214 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1215 list_del(&epoch->list);
1216 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
Philipp Reisner12038a32011-11-09 19:18:00 +01001217 tconn->epochs--;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001218 kfree(epoch);
1219
1220 if (rv == FE_STILL_LIVE)
1221 rv = FE_DESTROYED;
1222 } else {
1223 epoch->flags = 0;
1224 atomic_set(&epoch->epoch_size, 0);
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001225 /* atomic_set(&epoch->active, 0); is already zero */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001226 if (rv == FE_STILL_LIVE)
1227 rv = FE_RECYCLED;
1228 }
1229 }
1230
1231 if (!next_epoch)
1232 break;
1233
1234 epoch = next_epoch;
1235 } while (1);
1236
Philipp Reisner12038a32011-11-09 19:18:00 +01001237 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001238
Philipp Reisnerb411b362009-09-25 16:07:19 -07001239 return rv;
1240}
1241
1242/**
1243 * drbd_bump_write_ordering() - Fall back to an other write ordering method
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001244 * @tconn: DRBD connection.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001245 * @wo: Write ordering method to try.
1246 */
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001247void drbd_bump_write_ordering(struct drbd_tconn *tconn, enum write_ordering_e wo)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001248{
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001249 struct disk_conf *dc;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001250 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001251 enum write_ordering_e pwo;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001252 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001253 static char *write_ordering_str[] = {
1254 [WO_none] = "none",
1255 [WO_drain_io] = "drain",
1256 [WO_bdev_flush] = "flush",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001257 };
1258
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001259 pwo = tconn->write_ordering;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001260 wo = min(pwo, wo);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001261 rcu_read_lock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001262 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisner27eb13e2012-03-30 14:12:15 +02001263 if (!get_ldev_if_state(mdev, D_ATTACHING))
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001264 continue;
1265 dc = rcu_dereference(mdev->ldev->disk_conf);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001266
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001267 if (wo == WO_bdev_flush && !dc->disk_flushes)
1268 wo = WO_drain_io;
1269 if (wo == WO_drain_io && !dc->disk_drain)
1270 wo = WO_none;
1271 put_ldev(mdev);
1272 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001273 rcu_read_unlock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001274 tconn->write_ordering = wo;
1275 if (pwo != tconn->write_ordering || wo == WO_bdev_flush)
1276 conn_info(tconn, "Method to ensure write ordering: %s\n", write_ordering_str[tconn->write_ordering]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001277}
1278
1279/**
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001280 * drbd_submit_peer_request()
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001281 * @mdev: DRBD device.
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001282 * @peer_req: peer request
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001283 * @rw: flag field, see bio->bi_rw
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001284 *
1285 * May spread the pages to multiple bios,
1286 * depending on bio_add_page restrictions.
1287 *
1288 * Returns 0 if all bios have been submitted,
1289 * -ENOMEM if we could not allocate enough bios,
1290 * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a
1291 * single page to an empty bio (which should never happen and likely indicates
1292 * that the lower level IO stack is in some way broken). This has been observed
1293 * on certain Xen deployments.
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001294 */
1295/* TODO allocate from our own bio_set. */
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001296int drbd_submit_peer_request(struct drbd_conf *mdev,
1297 struct drbd_peer_request *peer_req,
1298 const unsigned rw, const int fault_type)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001299{
1300 struct bio *bios = NULL;
1301 struct bio *bio;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001302 struct page *page = peer_req->pages;
1303 sector_t sector = peer_req->i.sector;
1304 unsigned ds = peer_req->i.size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001305 unsigned n_bios = 0;
1306 unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001307 int err = -ENOMEM;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001308
1309 /* In most cases, we will only need one bio. But in case the lower
1310 * level restrictions happen to be different at this offset on this
1311 * side than those of the sending peer, we may need to submit the
Lars Ellenberg9476f392011-02-23 17:02:01 +01001312 * request in more than one bio.
1313 *
1314 * Plain bio_alloc is good enough here, this is no DRBD internally
1315 * generated bio, but a bio allocated on behalf of the peer.
1316 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001317next_bio:
1318 bio = bio_alloc(GFP_NOIO, nr_pages);
1319 if (!bio) {
1320 dev_err(DEV, "submit_ee: Allocation of a bio failed\n");
1321 goto fail;
1322 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001323 /* > peer_req->i.sector, unless this is the first bio */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001324 bio->bi_sector = sector;
1325 bio->bi_bdev = mdev->ldev->backing_bdev;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001326 bio->bi_rw = rw;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001327 bio->bi_private = peer_req;
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001328 bio->bi_end_io = drbd_peer_request_endio;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001329
1330 bio->bi_next = bios;
1331 bios = bio;
1332 ++n_bios;
1333
1334 page_chain_for_each(page) {
1335 unsigned len = min_t(unsigned, ds, PAGE_SIZE);
1336 if (!bio_add_page(bio, page, len, 0)) {
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001337 /* A single page must always be possible!
1338 * But in case it fails anyways,
1339 * we deal with it, and complain (below). */
1340 if (bio->bi_vcnt == 0) {
1341 dev_err(DEV,
1342 "bio_add_page failed for len=%u, "
1343 "bi_vcnt=0 (bi_sector=%llu)\n",
1344 len, (unsigned long long)bio->bi_sector);
1345 err = -ENOSPC;
1346 goto fail;
1347 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001348 goto next_bio;
1349 }
1350 ds -= len;
1351 sector += len >> 9;
1352 --nr_pages;
1353 }
1354 D_ASSERT(page == NULL);
1355 D_ASSERT(ds == 0);
1356
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001357 atomic_set(&peer_req->pending_bios, n_bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001358 do {
1359 bio = bios;
1360 bios = bios->bi_next;
1361 bio->bi_next = NULL;
1362
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001363 drbd_generic_make_request(mdev, fault_type, bio);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001364 } while (bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001365 return 0;
1366
1367fail:
1368 while (bios) {
1369 bio = bios;
1370 bios = bios->bi_next;
1371 bio_put(bio);
1372 }
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001373 return err;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001374}
1375
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001376static void drbd_remove_epoch_entry_interval(struct drbd_conf *mdev,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001377 struct drbd_peer_request *peer_req)
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001378{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001379 struct drbd_interval *i = &peer_req->i;
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001380
1381 drbd_remove_interval(&mdev->write_requests, i);
1382 drbd_clear_interval(i);
1383
Andreas Gruenbacher6c852be2011-02-04 15:38:52 +01001384 /* Wake up any processes waiting for this peer request to complete. */
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001385 if (i->waiting)
1386 wake_up(&mdev->misc_wait);
1387}
1388
Philipp Reisner77fede52011-11-10 21:19:11 +01001389void conn_wait_active_ee_empty(struct drbd_tconn *tconn)
1390{
1391 struct drbd_conf *mdev;
1392 int vnr;
1393
1394 rcu_read_lock();
1395 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1396 kref_get(&mdev->kref);
1397 rcu_read_unlock();
1398 drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
1399 kref_put(&mdev->kref, &drbd_minor_destroy);
1400 rcu_read_lock();
1401 }
1402 rcu_read_unlock();
1403}
1404
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001405static int receive_Barrier(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001406{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001407 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001408 struct p_barrier *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001409 struct drbd_epoch *epoch;
1410
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001411 /* FIXME these are unacked on connection,
1412 * not a specific (peer)device.
1413 */
Philipp Reisner12038a32011-11-09 19:18:00 +01001414 tconn->current_epoch->barrier_nr = p->barrier;
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001415 tconn->current_epoch->tconn = tconn;
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001416 rv = drbd_may_finish_epoch(tconn, tconn->current_epoch, EV_GOT_BARRIER_NR);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001417
1418 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
1419 * the activity log, which means it would not be resynced in case the
1420 * R_PRIMARY crashes now.
1421 * Therefore we must send the barrier_ack after the barrier request was
1422 * completed. */
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001423 switch (tconn->write_ordering) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001424 case WO_none:
1425 if (rv == FE_RECYCLED)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001426 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001427
1428 /* receiver context, in the writeout path of the other node.
1429 * avoid potential distributed deadlock */
1430 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1431 if (epoch)
1432 break;
1433 else
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001434 conn_warn(tconn, "Allocation of an epoch failed, slowing down\n");
Philipp Reisner2451fc32010-08-24 13:43:11 +02001435 /* Fall through */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001436
1437 case WO_bdev_flush:
1438 case WO_drain_io:
Philipp Reisner77fede52011-11-10 21:19:11 +01001439 conn_wait_active_ee_empty(tconn);
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001440 drbd_flush(tconn);
Philipp Reisner2451fc32010-08-24 13:43:11 +02001441
Philipp Reisner12038a32011-11-09 19:18:00 +01001442 if (atomic_read(&tconn->current_epoch->epoch_size)) {
Philipp Reisner2451fc32010-08-24 13:43:11 +02001443 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1444 if (epoch)
1445 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001446 }
1447
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001448 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001449 default:
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02001450 conn_err(tconn, "Strangeness in tconn->write_ordering %d\n", tconn->write_ordering);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001451 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001452 }
1453
1454 epoch->flags = 0;
1455 atomic_set(&epoch->epoch_size, 0);
1456 atomic_set(&epoch->active, 0);
1457
Philipp Reisner12038a32011-11-09 19:18:00 +01001458 spin_lock(&tconn->epoch_lock);
1459 if (atomic_read(&tconn->current_epoch->epoch_size)) {
1460 list_add(&epoch->list, &tconn->current_epoch->list);
1461 tconn->current_epoch = epoch;
1462 tconn->epochs++;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001463 } else {
1464 /* The current_epoch got recycled while we allocated this one... */
1465 kfree(epoch);
1466 }
Philipp Reisner12038a32011-11-09 19:18:00 +01001467 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001468
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001469 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001470}
1471
1472/* used from receive_RSDataReply (recv_resync_read)
1473 * and from receive_Data */
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001474static struct drbd_peer_request *
1475read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector,
1476 int data_size) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001477{
Lars Ellenberg66660322010-04-06 12:15:04 +02001478 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001479 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001480 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001481 int dgs, ds, err;
Philipp Reisnera0638452011-01-19 14:31:32 +01001482 void *dig_in = mdev->tconn->int_dig_in;
1483 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001484 unsigned long *data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001485
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001486 dgs = 0;
1487 if (mdev->tconn->peer_integrity_tfm) {
1488 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001489 /*
1490 * FIXME: Receive the incoming digest into the receive buffer
1491 * here, together with its struct p_data?
1492 */
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001493 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1494 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001495 return NULL;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001496 data_size -= dgs;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001497 }
1498
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001499 if (!expect(IS_ALIGNED(data_size, 512)))
1500 return NULL;
1501 if (!expect(data_size <= DRBD_MAX_BIO_SIZE))
1502 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001503
Lars Ellenberg66660322010-04-06 12:15:04 +02001504 /* even though we trust out peer,
1505 * we sometimes have to double check. */
1506 if (sector + (data_size>>9) > capacity) {
Lars Ellenbergfdda6542011-01-24 15:11:01 +01001507 dev_err(DEV, "request from peer beyond end of local disk: "
1508 "capacity: %llus < sector: %llus + size: %u\n",
Lars Ellenberg66660322010-04-06 12:15:04 +02001509 (unsigned long long)capacity,
1510 (unsigned long long)sector, data_size);
1511 return NULL;
1512 }
1513
Philipp Reisnerb411b362009-09-25 16:07:19 -07001514 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1515 * "criss-cross" setup, that might cause write-out on some other DRBD,
1516 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02001517 peer_req = drbd_alloc_peer_req(mdev, id, sector, data_size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001518 if (!peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001519 return NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001520
Lars Ellenberga73ff322012-06-25 19:15:38 +02001521 if (!data_size)
Lars Ellenberg81a35372012-07-30 09:00:54 +02001522 return peer_req;
Lars Ellenberga73ff322012-06-25 19:15:38 +02001523
Philipp Reisnerb411b362009-09-25 16:07:19 -07001524 ds = data_size;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001525 page = peer_req->pages;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001526 page_chain_for_each(page) {
1527 unsigned len = min_t(int, ds, PAGE_SIZE);
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001528 data = kmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001529 err = drbd_recv_all_warn(mdev->tconn, data, len);
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +01001530 if (drbd_insert_fault(mdev, DRBD_FAULT_RECEIVE)) {
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001531 dev_err(DEV, "Fault injection: Corrupting data on receive\n");
1532 data[0] = data[0] ^ (unsigned long)-1;
1533 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001534 kunmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001535 if (err) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001536 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001537 return NULL;
1538 }
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001539 ds -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001540 }
1541
1542 if (dgs) {
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02001543 drbd_csum_ee(mdev, mdev->tconn->peer_integrity_tfm, peer_req, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001544 if (memcmp(dig_in, dig_vv, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001545 dev_err(DEV, "Digest integrity check FAILED: %llus +%u\n",
1546 (unsigned long long)sector, data_size);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001547 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001548 return NULL;
1549 }
1550 }
1551 mdev->recv_cnt += data_size>>9;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001552 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001553}
1554
1555/* drbd_drain_block() just takes a data block
1556 * out of the socket input buffer, and discards it.
1557 */
1558static int drbd_drain_block(struct drbd_conf *mdev, int data_size)
1559{
1560 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001561 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001562 void *data;
1563
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001564 if (!data_size)
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001565 return 0;
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001566
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +02001567 page = drbd_alloc_pages(mdev, 1, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001568
1569 data = kmap(page);
1570 while (data_size) {
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001571 unsigned int len = min_t(int, data_size, PAGE_SIZE);
1572
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001573 err = drbd_recv_all_warn(mdev->tconn, data, len);
1574 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001575 break;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001576 data_size -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001577 }
1578 kunmap(page);
Andreas Gruenbacher5cc287e2011-04-07 21:02:59 +02001579 drbd_free_pages(mdev, page, 0);
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001580 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001581}
1582
1583static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req,
1584 sector_t sector, int data_size)
1585{
1586 struct bio_vec *bvec;
1587 struct bio *bio;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001588 int dgs, err, i, expect;
Philipp Reisnera0638452011-01-19 14:31:32 +01001589 void *dig_in = mdev->tconn->int_dig_in;
1590 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001591
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001592 dgs = 0;
1593 if (mdev->tconn->peer_integrity_tfm) {
1594 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001595 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1596 if (err)
1597 return err;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001598 data_size -= dgs;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001599 }
1600
Philipp Reisnerb411b362009-09-25 16:07:19 -07001601 /* optimistically update recv_cnt. if receiving fails below,
1602 * we disconnect anyways, and counters will be reset. */
1603 mdev->recv_cnt += data_size>>9;
1604
1605 bio = req->master_bio;
1606 D_ASSERT(sector == bio->bi_sector);
1607
1608 bio_for_each_segment(bvec, bio, i) {
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001609 void *mapped = kmap(bvec->bv_page) + bvec->bv_offset;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001610 expect = min_t(int, data_size, bvec->bv_len);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001611 err = drbd_recv_all_warn(mdev->tconn, mapped, expect);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001612 kunmap(bvec->bv_page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001613 if (err)
1614 return err;
1615 data_size -= expect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001616 }
1617
1618 if (dgs) {
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02001619 drbd_csum_bio(mdev, mdev->tconn->peer_integrity_tfm, bio, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001620 if (memcmp(dig_in, dig_vv, dgs)) {
1621 dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n");
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001622 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001623 }
1624 }
1625
1626 D_ASSERT(data_size == 0);
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001627 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001628}
1629
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001630/*
1631 * e_end_resync_block() is called in asender context via
1632 * drbd_finish_peer_reqs().
1633 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001634static int e_end_resync_block(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001635{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001636 struct drbd_peer_request *peer_req =
1637 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001638 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001639 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001640 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001641
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001642 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001643
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001644 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1645 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001646 err = drbd_send_ack(mdev, P_RS_WRITE_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001647 } else {
1648 /* Record failure to sync */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001649 drbd_rs_failed_io(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001650
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001651 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001652 }
1653 dec_unacked(mdev);
1654
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001655 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001656}
1657
1658static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local)
1659{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001660 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001661
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001662 peer_req = read_in_block(mdev, ID_SYNCER, sector, data_size);
1663 if (!peer_req)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001664 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001665
1666 dec_rs_pending(mdev);
1667
Philipp Reisnerb411b362009-09-25 16:07:19 -07001668 inc_unacked(mdev);
1669 /* corresponding dec_unacked() in e_end_resync_block()
1670 * respective _drbd_clear_done_ee */
1671
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001672 peer_req->w.cb = e_end_resync_block;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001673
Philipp Reisner87eeee42011-01-19 14:16:30 +01001674 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001675 list_add(&peer_req->w.list, &mdev->sync_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001676 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001677
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001678 atomic_add(data_size >> 9, &mdev->rs_sect_ev);
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001679 if (drbd_submit_peer_request(mdev, peer_req, WRITE, DRBD_FAULT_RS_WR) == 0)
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001680 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001681
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001682 /* don't care for the reason here */
1683 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01001684 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001685 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001686 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001687
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001688 drbd_free_peer_req(mdev, peer_req);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001689fail:
1690 put_ldev(mdev);
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001691 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001692}
1693
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001694static struct drbd_request *
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001695find_request(struct drbd_conf *mdev, struct rb_root *root, u64 id,
1696 sector_t sector, bool missing_ok, const char *func)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001697{
1698 struct drbd_request *req;
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001699
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001700 /* Request object according to our peer */
1701 req = (struct drbd_request *)(unsigned long)id;
Andreas Gruenbacher5e472262011-01-27 14:42:51 +01001702 if (drbd_contains_interval(root, sector, &req->i) && req->i.local)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001703 return req;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001704 if (!missing_ok) {
Andreas Gruenbacher5af172e2011-07-15 09:43:23 +02001705 dev_err(DEV, "%s: failed to find request 0x%lx, sector %llus\n", func,
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001706 (unsigned long)id, (unsigned long long)sector);
1707 }
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001708 return NULL;
1709}
1710
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001711static int receive_DataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001712{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001713 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001714 struct drbd_request *req;
1715 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001716 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001717 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001718
1719 mdev = vnr_to_mdev(tconn, pi->vnr);
1720 if (!mdev)
1721 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001722
1723 sector = be64_to_cpu(p->sector);
1724
Philipp Reisner87eeee42011-01-19 14:16:30 +01001725 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001726 req = find_request(mdev, &mdev->read_requests, p->block_id, sector, false, __func__);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001727 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001728 if (unlikely(!req))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001729 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001730
Bart Van Assche24c48302011-05-21 18:32:29 +02001731 /* hlist_del(&req->collision) is done in _req_may_be_done, to avoid
Philipp Reisnerb411b362009-09-25 16:07:19 -07001732 * special casing it there for the various failure cases.
1733 * still no race with drbd_fail_pending_reads */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001734 err = recv_dless_read(mdev, req, sector, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001735 if (!err)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001736 req_mod(req, DATA_RECEIVED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001737 /* else: nothing. handled from drbd_disconnect...
1738 * I don't think we may complete this just yet
1739 * in case we are "on-disconnect: freeze" */
1740
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001741 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001742}
1743
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001744static int receive_RSDataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001745{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001746 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001747 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001748 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001749 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001750
1751 mdev = vnr_to_mdev(tconn, pi->vnr);
1752 if (!mdev)
1753 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001754
1755 sector = be64_to_cpu(p->sector);
1756 D_ASSERT(p->block_id == ID_SYNCER);
1757
1758 if (get_ldev(mdev)) {
1759 /* data is submitted to disk within recv_resync_read.
1760 * corresponding put_ldev done below on error,
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001761 * or in drbd_peer_request_endio. */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001762 err = recv_resync_read(mdev, sector, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001763 } else {
1764 if (__ratelimit(&drbd_ratelimit_state))
1765 dev_err(DEV, "Can not write resync data to local disk.\n");
1766
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001767 err = drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001768
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001769 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001770 }
1771
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001772 atomic_add(pi->size >> 9, &mdev->rs_sect_in);
Philipp Reisner778f2712010-07-06 11:14:00 +02001773
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001774 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001775}
1776
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001777static void restart_conflicting_writes(struct drbd_conf *mdev,
1778 sector_t sector, int size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001779{
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001780 struct drbd_interval *i;
1781 struct drbd_request *req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001782
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001783 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1784 if (!i->local)
1785 continue;
1786 req = container_of(i, struct drbd_request, i);
1787 if (req->rq_state & RQ_LOCAL_PENDING ||
1788 !(req->rq_state & RQ_POSTPONED))
1789 continue;
Lars Ellenberg2312f0b32011-11-24 10:36:25 +01001790 /* as it is RQ_POSTPONED, this will cause it to
1791 * be queued on the retry workqueue. */
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02001792 __req_mod(req, CONFLICT_RESOLVED, NULL);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001793 }
1794}
1795
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001796/*
1797 * e_end_block() is called in asender context via drbd_finish_peer_reqs().
Philipp Reisnerb411b362009-09-25 16:07:19 -07001798 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001799static int e_end_block(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001800{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001801 struct drbd_peer_request *peer_req =
1802 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001803 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001804 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001805 int err = 0, pcmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001806
Philipp Reisner303d1442011-04-13 16:24:47 -07001807 if (peer_req->flags & EE_SEND_WRITE_ACK) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001808 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001809 pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
1810 mdev->state.conn <= C_PAUSED_SYNC_T &&
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001811 peer_req->flags & EE_MAY_SET_IN_SYNC) ?
Philipp Reisnerb411b362009-09-25 16:07:19 -07001812 P_RS_WRITE_ACK : P_WRITE_ACK;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001813 err = drbd_send_ack(mdev, pcmd, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001814 if (pcmd == P_RS_WRITE_ACK)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001815 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001816 } else {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001817 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001818 /* we expect it to be marked out of sync anyways...
1819 * maybe assert this? */
1820 }
1821 dec_unacked(mdev);
1822 }
1823 /* we delete from the conflict detection hash _after_ we sent out the
1824 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
Philipp Reisner302bdea2011-04-21 11:36:49 +02001825 if (peer_req->flags & EE_IN_INTERVAL_TREE) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01001826 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001827 D_ASSERT(!drbd_interval_empty(&peer_req->i));
1828 drbd_remove_epoch_entry_interval(mdev, peer_req);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001829 if (peer_req->flags & EE_RESTART_REQUESTS)
1830 restart_conflicting_writes(mdev, sector, peer_req->i.size);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001831 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +01001832 } else
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001833 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001834
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001835 drbd_may_finish_epoch(mdev->tconn, peer_req->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001836
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001837 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001838}
1839
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001840static int e_send_ack(struct drbd_work *w, enum drbd_packet ack)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001841{
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001842 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001843 struct drbd_peer_request *peer_req =
1844 container_of(w, struct drbd_peer_request, w);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001845 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001846
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001847 err = drbd_send_ack(mdev, ack, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001848 dec_unacked(mdev);
1849
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001850 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001851}
1852
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02001853static int e_send_superseded(struct drbd_work *w, int unused)
Philipp Reisnerb6a370ba2012-02-19 01:27:53 +01001854{
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02001855 return e_send_ack(w, P_SUPERSEDED);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001856}
Philipp Reisnerb6a370ba2012-02-19 01:27:53 +01001857
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001858static int e_send_retry_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001859{
1860 struct drbd_tconn *tconn = w->mdev->tconn;
1861
1862 return e_send_ack(w, tconn->agreed_pro_version >= 100 ?
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02001863 P_RETRY_WRITE : P_SUPERSEDED);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001864}
1865
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001866static bool seq_greater(u32 a, u32 b)
1867{
1868 /*
1869 * We assume 32-bit wrap-around here.
1870 * For 24-bit wrap-around, we would have to shift:
1871 * a <<= 8; b <<= 8;
1872 */
1873 return (s32)a - (s32)b > 0;
1874}
1875
1876static u32 seq_max(u32 a, u32 b)
1877{
1878 return seq_greater(a, b) ? a : b;
1879}
1880
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001881static bool need_peer_seq(struct drbd_conf *mdev)
1882{
1883 struct drbd_tconn *tconn = mdev->tconn;
Philipp Reisner302bdea2011-04-21 11:36:49 +02001884 int tp;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001885
1886 /*
1887 * We only need to keep track of the last packet_seq number of our peer
Lars Ellenberg427c0432012-08-01 12:43:01 +02001888 * if we are in dual-primary mode and we have the resolve-conflicts flag set; see
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001889 * handle_write_conflicts().
1890 */
Philipp Reisner302bdea2011-04-21 11:36:49 +02001891
1892 rcu_read_lock();
1893 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
1894 rcu_read_unlock();
1895
Lars Ellenberg427c0432012-08-01 12:43:01 +02001896 return tp && test_bit(RESOLVE_CONFLICTS, &tconn->flags);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001897}
1898
Andreas Gruenbacher43ae0772011-02-03 18:42:08 +01001899static void update_peer_seq(struct drbd_conf *mdev, unsigned int peer_seq)
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001900{
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001901 unsigned int newest_peer_seq;
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001902
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001903 if (need_peer_seq(mdev)) {
1904 spin_lock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001905 newest_peer_seq = seq_max(mdev->peer_seq, peer_seq);
1906 mdev->peer_seq = newest_peer_seq;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001907 spin_unlock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001908 /* wake up only if we actually changed mdev->peer_seq */
1909 if (peer_seq == newest_peer_seq)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001910 wake_up(&mdev->seq_wait);
1911 }
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001912}
1913
Lars Ellenbergd93f6302012-03-26 15:49:13 +02001914static inline int overlaps(sector_t s1, int l1, sector_t s2, int l2)
1915{
1916 return !((s1 + (l1>>9) <= s2) || (s1 >= s2 + (l2>>9)));
1917}
1918
1919/* maybe change sync_ee into interval trees as well? */
Philipp Reisner3ea35df2012-04-06 12:13:18 +02001920static bool overlapping_resync_write(struct drbd_conf *mdev, struct drbd_peer_request *peer_req)
Lars Ellenbergd93f6302012-03-26 15:49:13 +02001921{
1922 struct drbd_peer_request *rs_req;
Philipp Reisnerb6a370ba2012-02-19 01:27:53 +01001923 bool rv = 0;
1924
Lars Ellenbergd93f6302012-03-26 15:49:13 +02001925 spin_lock_irq(&mdev->tconn->req_lock);
1926 list_for_each_entry(rs_req, &mdev->sync_ee, w.list) {
1927 if (overlaps(peer_req->i.sector, peer_req->i.size,
1928 rs_req->i.sector, rs_req->i.size)) {
Philipp Reisnerb6a370ba2012-02-19 01:27:53 +01001929 rv = 1;
1930 break;
1931 }
1932 }
Lars Ellenbergd93f6302012-03-26 15:49:13 +02001933 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb6a370ba2012-02-19 01:27:53 +01001934
1935 return rv;
1936}
1937
Philipp Reisnerb411b362009-09-25 16:07:19 -07001938/* Called from receive_Data.
1939 * Synchronize packets on sock with packets on msock.
1940 *
1941 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
1942 * packet traveling on msock, they are still processed in the order they have
1943 * been sent.
1944 *
1945 * Note: we don't care for Ack packets overtaking P_DATA packets.
1946 *
1947 * In case packet_seq is larger than mdev->peer_seq number, there are
1948 * outstanding packets on the msock. We wait for them to arrive.
1949 * In case we are the logically next packet, we update mdev->peer_seq
1950 * ourselves. Correctly handles 32bit wrap around.
1951 *
1952 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
1953 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
1954 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
1955 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
1956 *
1957 * returns 0 if we may process the packet,
1958 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001959static int wait_for_and_update_peer_seq(struct drbd_conf *mdev, const u32 peer_seq)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001960{
1961 DEFINE_WAIT(wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001962 long timeout;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001963 int ret;
1964
1965 if (!need_peer_seq(mdev))
1966 return 0;
1967
Philipp Reisnerb411b362009-09-25 16:07:19 -07001968 spin_lock(&mdev->peer_seq_lock);
1969 for (;;) {
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001970 if (!seq_greater(peer_seq - 1, mdev->peer_seq)) {
1971 mdev->peer_seq = seq_max(mdev->peer_seq, peer_seq);
1972 ret = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001973 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001974 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001975 if (signal_pending(current)) {
1976 ret = -ERESTARTSYS;
1977 break;
1978 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001979 prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001980 spin_unlock(&mdev->peer_seq_lock);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001981 rcu_read_lock();
1982 timeout = rcu_dereference(mdev->tconn->net_conf)->ping_timeo*HZ/10;
1983 rcu_read_unlock();
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001984 timeout = schedule_timeout(timeout);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001985 spin_lock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001986 if (!timeout) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001987 ret = -ETIMEDOUT;
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001988 dev_err(DEV, "Timed out waiting for missing ack packets; disconnecting\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001989 break;
1990 }
1991 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001992 spin_unlock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001993 finish_wait(&mdev->seq_wait, &wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001994 return ret;
1995}
1996
Lars Ellenberg688593c2010-11-17 22:25:03 +01001997/* see also bio_flags_to_wire()
1998 * DRBD_REQ_*, because we need to semantically map the flags to data packet
1999 * flags and back. We may replicate to other kernel versions. */
2000static unsigned long wire_flags_to_bio(struct drbd_conf *mdev, u32 dpf)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02002001{
Lars Ellenberg688593c2010-11-17 22:25:03 +01002002 return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
2003 (dpf & DP_FUA ? REQ_FUA : 0) |
2004 (dpf & DP_FLUSH ? REQ_FLUSH : 0) |
2005 (dpf & DP_DISCARD ? REQ_DISCARD : 0);
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02002006}
2007
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002008static void fail_postponed_requests(struct drbd_conf *mdev, sector_t sector,
2009 unsigned int size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002010{
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002011 struct drbd_interval *i;
2012
2013 repeat:
2014 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
2015 struct drbd_request *req;
2016 struct bio_and_error m;
2017
2018 if (!i->local)
2019 continue;
2020 req = container_of(i, struct drbd_request, i);
2021 if (!(req->rq_state & RQ_POSTPONED))
2022 continue;
2023 req->rq_state &= ~RQ_POSTPONED;
2024 __req_mod(req, NEG_ACKED, &m);
2025 spin_unlock_irq(&mdev->tconn->req_lock);
2026 if (m.bio)
2027 complete_master_bio(mdev, &m);
2028 spin_lock_irq(&mdev->tconn->req_lock);
2029 goto repeat;
2030 }
2031}
2032
2033static int handle_write_conflicts(struct drbd_conf *mdev,
2034 struct drbd_peer_request *peer_req)
2035{
2036 struct drbd_tconn *tconn = mdev->tconn;
Lars Ellenberg427c0432012-08-01 12:43:01 +02002037 bool resolve_conflicts = test_bit(RESOLVE_CONFLICTS, &tconn->flags);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002038 sector_t sector = peer_req->i.sector;
2039 const unsigned int size = peer_req->i.size;
2040 struct drbd_interval *i;
2041 bool equal;
2042 int err;
2043
2044 /*
2045 * Inserting the peer request into the write_requests tree will prevent
2046 * new conflicting local requests from being added.
2047 */
2048 drbd_insert_interval(&mdev->write_requests, &peer_req->i);
2049
2050 repeat:
2051 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
2052 if (i == &peer_req->i)
2053 continue;
2054
2055 if (!i->local) {
2056 /*
2057 * Our peer has sent a conflicting remote request; this
2058 * should not happen in a two-node setup. Wait for the
2059 * earlier peer request to complete.
2060 */
2061 err = drbd_wait_misc(mdev, i);
2062 if (err)
2063 goto out;
2064 goto repeat;
2065 }
2066
2067 equal = i->sector == sector && i->size == size;
2068 if (resolve_conflicts) {
2069 /*
2070 * If the peer request is fully contained within the
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02002071 * overlapping request, it can be considered overwritten
2072 * and thus superseded; otherwise, it will be retried
2073 * once all overlapping requests have completed.
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002074 */
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02002075 bool superseded = i->sector <= sector && i->sector +
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002076 (i->size >> 9) >= sector + (size >> 9);
2077
2078 if (!equal)
2079 dev_alert(DEV, "Concurrent writes detected: "
2080 "local=%llus +%u, remote=%llus +%u, "
2081 "assuming %s came first\n",
2082 (unsigned long long)i->sector, i->size,
2083 (unsigned long long)sector, size,
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02002084 superseded ? "local" : "remote");
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002085
2086 inc_unacked(mdev);
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02002087 peer_req->w.cb = superseded ? e_send_superseded :
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002088 e_send_retry_write;
2089 list_add_tail(&peer_req->w.list, &mdev->done_ee);
2090 wake_asender(mdev->tconn);
2091
2092 err = -ENOENT;
2093 goto out;
2094 } else {
2095 struct drbd_request *req =
2096 container_of(i, struct drbd_request, i);
2097
2098 if (!equal)
2099 dev_alert(DEV, "Concurrent writes detected: "
2100 "local=%llus +%u, remote=%llus +%u\n",
2101 (unsigned long long)i->sector, i->size,
2102 (unsigned long long)sector, size);
2103
2104 if (req->rq_state & RQ_LOCAL_PENDING ||
2105 !(req->rq_state & RQ_POSTPONED)) {
2106 /*
2107 * Wait for the node with the discard flag to
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02002108 * decide if this request has been superseded
2109 * or needs to be retried.
2110 * Requests that have been superseded will
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002111 * disappear from the write_requests tree.
2112 *
2113 * In addition, wait for the conflicting
2114 * request to finish locally before submitting
2115 * the conflicting peer request.
2116 */
2117 err = drbd_wait_misc(mdev, &req->i);
2118 if (err) {
2119 _conn_request_state(mdev->tconn,
2120 NS(conn, C_TIMEOUT),
2121 CS_HARD);
2122 fail_postponed_requests(mdev, sector, size);
2123 goto out;
2124 }
2125 goto repeat;
2126 }
2127 /*
2128 * Remember to restart the conflicting requests after
2129 * the new peer request has completed.
2130 */
2131 peer_req->flags |= EE_RESTART_REQUESTS;
2132 }
2133 }
2134 err = 0;
2135
2136 out:
2137 if (err)
2138 drbd_remove_epoch_entry_interval(mdev, peer_req);
2139 return err;
2140}
2141
Philipp Reisnerb411b362009-09-25 16:07:19 -07002142/* mirrored write */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002143static int receive_Data(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002144{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002145 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002146 sector_t sector;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002147 struct drbd_peer_request *peer_req;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002148 struct p_data *p = pi->data;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002149 u32 peer_seq = be32_to_cpu(p->seq_num);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002150 int rw = WRITE;
2151 u32 dp_flags;
Philipp Reisner302bdea2011-04-21 11:36:49 +02002152 int err, tp;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002153
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002154 mdev = vnr_to_mdev(tconn, pi->vnr);
2155 if (!mdev)
2156 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002157
Philipp Reisnerb411b362009-09-25 16:07:19 -07002158 if (!get_ldev(mdev)) {
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002159 int err2;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002160
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002161 err = wait_for_and_update_peer_seq(mdev, peer_seq);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002162 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisner12038a32011-11-09 19:18:00 +01002163 atomic_inc(&tconn->current_epoch->epoch_size);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002164 err2 = drbd_drain_block(mdev, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002165 if (!err)
2166 err = err2;
2167 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002168 }
2169
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01002170 /*
2171 * Corresponding put_ldev done either below (on various errors), or in
2172 * drbd_peer_request_endio, if we successfully submit the data at the
2173 * end of this function.
2174 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002175
2176 sector = be64_to_cpu(p->sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002177 peer_req = read_in_block(mdev, p->block_id, sector, pi->size);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002178 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002179 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002180 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002181 }
2182
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002183 peer_req->w.cb = e_end_block;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002184
Lars Ellenberg688593c2010-11-17 22:25:03 +01002185 dp_flags = be32_to_cpu(p->dp_flags);
2186 rw |= wire_flags_to_bio(mdev, dp_flags);
Lars Ellenberg81a35372012-07-30 09:00:54 +02002187 if (peer_req->pages == NULL) {
2188 D_ASSERT(peer_req->i.size == 0);
Lars Ellenberga73ff322012-06-25 19:15:38 +02002189 D_ASSERT(dp_flags & DP_FLUSH);
2190 }
Lars Ellenberg688593c2010-11-17 22:25:03 +01002191
2192 if (dp_flags & DP_MAY_SET_IN_SYNC)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002193 peer_req->flags |= EE_MAY_SET_IN_SYNC;
Lars Ellenberg688593c2010-11-17 22:25:03 +01002194
Philipp Reisner12038a32011-11-09 19:18:00 +01002195 spin_lock(&tconn->epoch_lock);
2196 peer_req->epoch = tconn->current_epoch;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002197 atomic_inc(&peer_req->epoch->epoch_size);
2198 atomic_inc(&peer_req->epoch->active);
Philipp Reisner12038a32011-11-09 19:18:00 +01002199 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002200
Philipp Reisner302bdea2011-04-21 11:36:49 +02002201 rcu_read_lock();
2202 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
2203 rcu_read_unlock();
2204 if (tp) {
2205 peer_req->flags |= EE_IN_INTERVAL_TREE;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002206 err = wait_for_and_update_peer_seq(mdev, peer_seq);
2207 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002208 goto out_interrupted;
Philipp Reisner87eeee42011-01-19 14:16:30 +01002209 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002210 err = handle_write_conflicts(mdev, peer_req);
2211 if (err) {
2212 spin_unlock_irq(&mdev->tconn->req_lock);
2213 if (err == -ENOENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002214 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002215 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002216 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002217 goto out_interrupted;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002218 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002219 } else
2220 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002221 list_add(&peer_req->w.list, &mdev->active_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002222 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002223
Philipp Reisnerb6a370ba2012-02-19 01:27:53 +01002224 if (mdev->state.conn == C_SYNC_TARGET)
Philipp Reisner3ea35df2012-04-06 12:13:18 +02002225 wait_event(mdev->ee_wait, !overlapping_resync_write(mdev, peer_req));
Philipp Reisnerb6a370ba2012-02-19 01:27:53 +01002226
Philipp Reisner303d1442011-04-13 16:24:47 -07002227 if (mdev->tconn->agreed_pro_version < 100) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02002228 rcu_read_lock();
2229 switch (rcu_dereference(mdev->tconn->net_conf)->wire_protocol) {
Philipp Reisner303d1442011-04-13 16:24:47 -07002230 case DRBD_PROT_C:
2231 dp_flags |= DP_SEND_WRITE_ACK;
2232 break;
2233 case DRBD_PROT_B:
2234 dp_flags |= DP_SEND_RECEIVE_ACK;
2235 break;
2236 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002237 rcu_read_unlock();
Philipp Reisner303d1442011-04-13 16:24:47 -07002238 }
2239
2240 if (dp_flags & DP_SEND_WRITE_ACK) {
2241 peer_req->flags |= EE_SEND_WRITE_ACK;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002242 inc_unacked(mdev);
2243 /* corresponding dec_unacked() in e_end_block()
2244 * respective _drbd_clear_done_ee */
Philipp Reisner303d1442011-04-13 16:24:47 -07002245 }
2246
2247 if (dp_flags & DP_SEND_RECEIVE_ACK) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002248 /* I really don't like it that the receiver thread
2249 * sends on the msock, but anyways */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002250 drbd_send_ack(mdev, P_RECV_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002251 }
2252
Lars Ellenberg6719fb02010-10-18 23:04:07 +02002253 if (mdev->state.pdsk < D_INCONSISTENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002254 /* In case we have the only disk of the cluster, */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002255 drbd_set_out_of_sync(mdev, peer_req->i.sector, peer_req->i.size);
2256 peer_req->flags |= EE_CALL_AL_COMPLETE_IO;
2257 peer_req->flags &= ~EE_MAY_SET_IN_SYNC;
Lars Ellenberg181286a2011-03-31 15:18:56 +02002258 drbd_al_begin_io(mdev, &peer_req->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002259 }
2260
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002261 err = drbd_submit_peer_request(mdev, peer_req, rw, DRBD_FAULT_DT_WR);
2262 if (!err)
2263 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002264
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002265 /* don't care for the reason here */
2266 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002267 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002268 list_del(&peer_req->w.list);
2269 drbd_remove_epoch_entry_interval(mdev, peer_req);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002270 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002271 if (peer_req->flags & EE_CALL_AL_COMPLETE_IO)
Lars Ellenberg181286a2011-03-31 15:18:56 +02002272 drbd_al_complete_io(mdev, &peer_req->i);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002273
Philipp Reisnerb411b362009-09-25 16:07:19 -07002274out_interrupted:
Philipp Reisner1e9dd292011-11-10 15:14:53 +01002275 drbd_may_finish_epoch(tconn, peer_req->epoch, EV_PUT + EV_CLEANUP);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002276 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002277 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002278 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002279}
2280
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002281/* We may throttle resync, if the lower device seems to be busy,
2282 * and current sync rate is above c_min_rate.
2283 *
2284 * To decide whether or not the lower device is busy, we use a scheme similar
2285 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
2286 * (more than 64 sectors) of activity we cannot account for with our own resync
2287 * activity, it obviously is "busy".
2288 *
2289 * The current sync rate used here uses only the most recent two step marks,
2290 * to have a short time average so we can react faster.
2291 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002292int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002293{
2294 struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
2295 unsigned long db, dt, dbdt;
Philipp Reisnere3555d82010-11-07 15:56:29 +01002296 struct lc_element *tmp;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002297 int curr_events;
2298 int throttle = 0;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002299 unsigned int c_min_rate;
2300
2301 rcu_read_lock();
2302 c_min_rate = rcu_dereference(mdev->ldev->disk_conf)->c_min_rate;
2303 rcu_read_unlock();
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002304
2305 /* feature disabled? */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002306 if (c_min_rate == 0)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002307 return 0;
2308
Philipp Reisnere3555d82010-11-07 15:56:29 +01002309 spin_lock_irq(&mdev->al_lock);
2310 tmp = lc_find(mdev->resync, BM_SECT_TO_EXT(sector));
2311 if (tmp) {
2312 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
2313 if (test_bit(BME_PRIORITY, &bm_ext->flags)) {
2314 spin_unlock_irq(&mdev->al_lock);
2315 return 0;
2316 }
2317 /* Do not slow down if app IO is already waiting for this extent */
2318 }
2319 spin_unlock_irq(&mdev->al_lock);
2320
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002321 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
2322 (int)part_stat_read(&disk->part0, sectors[1]) -
2323 atomic_read(&mdev->rs_sect_ev);
Philipp Reisnere3555d82010-11-07 15:56:29 +01002324
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002325 if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
2326 unsigned long rs_left;
2327 int i;
2328
2329 mdev->rs_last_events = curr_events;
2330
2331 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
2332 * approx. */
Lars Ellenberg2649f082010-11-05 10:05:47 +01002333 i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
2334
2335 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
2336 rs_left = mdev->ov_left;
2337 else
2338 rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002339
2340 dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
2341 if (!dt)
2342 dt++;
2343 db = mdev->rs_mark_left[i] - rs_left;
2344 dbdt = Bit2KB(db/dt);
2345
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002346 if (dbdt > c_min_rate)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002347 throttle = 1;
2348 }
2349 return throttle;
2350}
2351
2352
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002353static int receive_DataRequest(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002354{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002355 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002356 sector_t sector;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002357 sector_t capacity;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002358 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002359 struct digest_info *di = NULL;
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002360 int size, verb;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002361 unsigned int fault_type;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002362 struct p_block_req *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002363
2364 mdev = vnr_to_mdev(tconn, pi->vnr);
2365 if (!mdev)
2366 return -EIO;
2367 capacity = drbd_get_capacity(mdev->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002368
2369 sector = be64_to_cpu(p->sector);
2370 size = be32_to_cpu(p->blksize);
2371
Andreas Gruenbacherc670a392011-02-21 12:41:39 +01002372 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002373 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2374 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002375 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002376 }
2377 if (sector + (size>>9) > capacity) {
2378 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2379 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002380 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002381 }
2382
2383 if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002384 verb = 1;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002385 switch (pi->cmd) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002386 case P_DATA_REQUEST:
2387 drbd_send_ack_rp(mdev, P_NEG_DREPLY, p);
2388 break;
2389 case P_RS_DATA_REQUEST:
2390 case P_CSUM_RS_REQUEST:
2391 case P_OV_REQUEST:
2392 drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p);
2393 break;
2394 case P_OV_REPLY:
2395 verb = 0;
2396 dec_rs_pending(mdev);
2397 drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC);
2398 break;
2399 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002400 BUG();
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002401 }
2402 if (verb && __ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002403 dev_err(DEV, "Can not satisfy peer's read request, "
2404 "no local data.\n");
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002405
Lars Ellenberga821cc42010-09-06 12:31:37 +02002406 /* drain possibly payload */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002407 return drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002408 }
2409
2410 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
2411 * "criss-cross" setup, that might cause write-out on some other DRBD,
2412 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02002413 peer_req = drbd_alloc_peer_req(mdev, p->block_id, sector, size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002414 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002415 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002416 return -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002417 }
2418
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002419 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002420 case P_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002421 peer_req->w.cb = w_e_end_data_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002422 fault_type = DRBD_FAULT_DT_RD;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002423 /* application IO, don't drbd_rs_begin_io */
2424 goto submit;
2425
Philipp Reisnerb411b362009-09-25 16:07:19 -07002426 case P_RS_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002427 peer_req->w.cb = w_e_end_rsdata_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002428 fault_type = DRBD_FAULT_RS_RD;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002429 /* used in the sector offset progress display */
2430 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002431 break;
2432
2433 case P_OV_REPLY:
2434 case P_CSUM_RS_REQUEST:
2435 fault_type = DRBD_FAULT_RS_RD;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002436 di = kmalloc(sizeof(*di) + pi->size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002437 if (!di)
2438 goto out_free_e;
2439
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002440 di->digest_size = pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002441 di->digest = (((char *)di)+sizeof(struct digest_info));
2442
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002443 peer_req->digest = di;
2444 peer_req->flags |= EE_HAS_DIGEST;
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +02002445
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002446 if (drbd_recv_all(mdev->tconn, di->digest, pi->size))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002447 goto out_free_e;
2448
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002449 if (pi->cmd == P_CSUM_RS_REQUEST) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002450 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002451 peer_req->w.cb = w_e_end_csum_rs_req;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002452 /* used in the sector offset progress display */
2453 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002454 } else if (pi->cmd == P_OV_REPLY) {
Lars Ellenberg2649f082010-11-05 10:05:47 +01002455 /* track progress, we may need to throttle */
2456 atomic_add(size >> 9, &mdev->rs_sect_in);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002457 peer_req->w.cb = w_e_end_ov_reply;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002458 dec_rs_pending(mdev);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002459 /* drbd_rs_begin_io done when we sent this request,
2460 * but accounting still needs to be done. */
2461 goto submit_for_resync;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002462 }
2463 break;
2464
2465 case P_OV_REQUEST:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002466 if (mdev->ov_start_sector == ~(sector_t)0 &&
Philipp Reisner31890f42011-01-19 14:12:51 +01002467 mdev->tconn->agreed_pro_version >= 90) {
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002468 unsigned long now = jiffies;
2469 int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002470 mdev->ov_start_sector = sector;
2471 mdev->ov_position = sector;
Lars Ellenberg30b743a2010-11-05 09:39:06 +01002472 mdev->ov_left = drbd_bm_bits(mdev) - BM_SECT_TO_BIT(sector);
2473 mdev->rs_total = mdev->ov_left;
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002474 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
2475 mdev->rs_mark_left[i] = mdev->ov_left;
2476 mdev->rs_mark_time[i] = now;
2477 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002478 dev_info(DEV, "Online Verify start sector: %llu\n",
2479 (unsigned long long)sector);
2480 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002481 peer_req->w.cb = w_e_end_ov_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002482 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002483 break;
2484
Philipp Reisnerb411b362009-09-25 16:07:19 -07002485 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002486 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002487 }
2488
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002489 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2490 * wrt the receiver, but it is not as straightforward as it may seem.
2491 * Various places in the resync start and stop logic assume resync
2492 * requests are processed in order, requeuing this on the worker thread
2493 * introduces a bunch of new code for synchronization between threads.
2494 *
2495 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2496 * "forever", throttling after drbd_rs_begin_io will lock that extent
2497 * for application writes for the same time. For now, just throttle
2498 * here, where the rest of the code expects the receiver to sleep for
2499 * a while, anyways.
2500 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002501
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002502 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2503 * this defers syncer requests for some time, before letting at least
2504 * on request through. The resync controller on the receiving side
2505 * will adapt to the incoming rate accordingly.
2506 *
2507 * We cannot throttle here if remote is Primary/SyncTarget:
2508 * we would also throttle its application reads.
2509 * In that case, throttling is done on the SyncTarget only.
2510 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002511 if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev, sector))
2512 schedule_timeout_uninterruptible(HZ/10);
2513 if (drbd_rs_begin_io(mdev, sector))
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002514 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002515
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002516submit_for_resync:
2517 atomic_add(size >> 9, &mdev->rs_sect_ev);
2518
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002519submit:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002520 inc_unacked(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002521 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002522 list_add_tail(&peer_req->w.list, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002523 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002524
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01002525 if (drbd_submit_peer_request(mdev, peer_req, READ, fault_type) == 0)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002526 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002527
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002528 /* don't care for the reason here */
2529 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002530 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002531 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002532 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002533 /* no drbd_rs_complete_io(), we are dropping the connection anyways */
2534
Philipp Reisnerb411b362009-09-25 16:07:19 -07002535out_free_e:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002536 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002537 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002538 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002539}
2540
2541static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
2542{
2543 int self, peer, rv = -100;
2544 unsigned long ch_self, ch_peer;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002545 enum drbd_after_sb_p after_sb_0p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002546
2547 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2548 peer = mdev->p_uuid[UI_BITMAP] & 1;
2549
2550 ch_peer = mdev->p_uuid[UI_SIZE];
2551 ch_self = mdev->comm_bm_set;
2552
Philipp Reisner44ed1672011-04-19 17:10:19 +02002553 rcu_read_lock();
2554 after_sb_0p = rcu_dereference(mdev->tconn->net_conf)->after_sb_0p;
2555 rcu_read_unlock();
2556 switch (after_sb_0p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002557 case ASB_CONSENSUS:
2558 case ASB_DISCARD_SECONDARY:
2559 case ASB_CALL_HELPER:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002560 case ASB_VIOLENTLY:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002561 dev_err(DEV, "Configuration error.\n");
2562 break;
2563 case ASB_DISCONNECT:
2564 break;
2565 case ASB_DISCARD_YOUNGER_PRI:
2566 if (self == 0 && peer == 1) {
2567 rv = -1;
2568 break;
2569 }
2570 if (self == 1 && peer == 0) {
2571 rv = 1;
2572 break;
2573 }
2574 /* Else fall through to one of the other strategies... */
2575 case ASB_DISCARD_OLDER_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... */
Lars Ellenbergad19bf62009-10-14 09:36:49 +02002585 dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
Philipp Reisnerb411b362009-09-25 16:07:19 -07002586 "Using discard-least-changes instead\n");
2587 case ASB_DISCARD_ZERO_CHG:
2588 if (ch_peer == 0 && ch_self == 0) {
Lars Ellenberg427c0432012-08-01 12:43:01 +02002589 rv = test_bit(RESOLVE_CONFLICTS, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002590 ? -1 : 1;
2591 break;
2592 } else {
2593 if (ch_peer == 0) { rv = 1; break; }
2594 if (ch_self == 0) { rv = -1; break; }
2595 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002596 if (after_sb_0p == ASB_DISCARD_ZERO_CHG)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002597 break;
2598 case ASB_DISCARD_LEAST_CHG:
2599 if (ch_self < ch_peer)
2600 rv = -1;
2601 else if (ch_self > ch_peer)
2602 rv = 1;
2603 else /* ( ch_self == ch_peer ) */
2604 /* Well, then use something else. */
Lars Ellenberg427c0432012-08-01 12:43:01 +02002605 rv = test_bit(RESOLVE_CONFLICTS, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002606 ? -1 : 1;
2607 break;
2608 case ASB_DISCARD_LOCAL:
2609 rv = -1;
2610 break;
2611 case ASB_DISCARD_REMOTE:
2612 rv = 1;
2613 }
2614
2615 return rv;
2616}
2617
2618static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
2619{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002620 int hg, rv = -100;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002621 enum drbd_after_sb_p after_sb_1p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002622
Philipp Reisner44ed1672011-04-19 17:10:19 +02002623 rcu_read_lock();
2624 after_sb_1p = rcu_dereference(mdev->tconn->net_conf)->after_sb_1p;
2625 rcu_read_unlock();
2626 switch (after_sb_1p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002627 case ASB_DISCARD_YOUNGER_PRI:
2628 case ASB_DISCARD_OLDER_PRI:
2629 case ASB_DISCARD_LEAST_CHG:
2630 case ASB_DISCARD_LOCAL:
2631 case ASB_DISCARD_REMOTE:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002632 case ASB_DISCARD_ZERO_CHG:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002633 dev_err(DEV, "Configuration error.\n");
2634 break;
2635 case ASB_DISCONNECT:
2636 break;
2637 case ASB_CONSENSUS:
2638 hg = drbd_asb_recover_0p(mdev);
2639 if (hg == -1 && mdev->state.role == R_SECONDARY)
2640 rv = hg;
2641 if (hg == 1 && mdev->state.role == R_PRIMARY)
2642 rv = hg;
2643 break;
2644 case ASB_VIOLENTLY:
2645 rv = drbd_asb_recover_0p(mdev);
2646 break;
2647 case ASB_DISCARD_SECONDARY:
2648 return mdev->state.role == R_PRIMARY ? 1 : -1;
2649 case ASB_CALL_HELPER:
2650 hg = drbd_asb_recover_0p(mdev);
2651 if (hg == -1 && mdev->state.role == R_PRIMARY) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002652 enum drbd_state_rv rv2;
2653
2654 drbd_set_role(mdev, R_SECONDARY, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002655 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2656 * we might be here in C_WF_REPORT_PARAMS which is transient.
2657 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002658 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2659 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002660 drbd_khelper(mdev, "pri-lost-after-sb");
2661 } else {
2662 dev_warn(DEV, "Successfully gave up primary role.\n");
2663 rv = hg;
2664 }
2665 } else
2666 rv = hg;
2667 }
2668
2669 return rv;
2670}
2671
2672static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
2673{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002674 int hg, rv = -100;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002675 enum drbd_after_sb_p after_sb_2p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002676
Philipp Reisner44ed1672011-04-19 17:10:19 +02002677 rcu_read_lock();
2678 after_sb_2p = rcu_dereference(mdev->tconn->net_conf)->after_sb_2p;
2679 rcu_read_unlock();
2680 switch (after_sb_2p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002681 case ASB_DISCARD_YOUNGER_PRI:
2682 case ASB_DISCARD_OLDER_PRI:
2683 case ASB_DISCARD_LEAST_CHG:
2684 case ASB_DISCARD_LOCAL:
2685 case ASB_DISCARD_REMOTE:
2686 case ASB_CONSENSUS:
2687 case ASB_DISCARD_SECONDARY:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002688 case ASB_DISCARD_ZERO_CHG:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002689 dev_err(DEV, "Configuration error.\n");
2690 break;
2691 case ASB_VIOLENTLY:
2692 rv = drbd_asb_recover_0p(mdev);
2693 break;
2694 case ASB_DISCONNECT:
2695 break;
2696 case ASB_CALL_HELPER:
2697 hg = drbd_asb_recover_0p(mdev);
2698 if (hg == -1) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002699 enum drbd_state_rv rv2;
2700
Philipp Reisnerb411b362009-09-25 16:07:19 -07002701 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2702 * we might be here in C_WF_REPORT_PARAMS which is transient.
2703 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002704 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2705 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002706 drbd_khelper(mdev, "pri-lost-after-sb");
2707 } else {
2708 dev_warn(DEV, "Successfully gave up primary role.\n");
2709 rv = hg;
2710 }
2711 } else
2712 rv = hg;
2713 }
2714
2715 return rv;
2716}
2717
2718static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
2719 u64 bits, u64 flags)
2720{
2721 if (!uuid) {
2722 dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
2723 return;
2724 }
2725 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
2726 text,
2727 (unsigned long long)uuid[UI_CURRENT],
2728 (unsigned long long)uuid[UI_BITMAP],
2729 (unsigned long long)uuid[UI_HISTORY_START],
2730 (unsigned long long)uuid[UI_HISTORY_END],
2731 (unsigned long long)bits,
2732 (unsigned long long)flags);
2733}
2734
2735/*
2736 100 after split brain try auto recover
2737 2 C_SYNC_SOURCE set BitMap
2738 1 C_SYNC_SOURCE use BitMap
2739 0 no Sync
2740 -1 C_SYNC_TARGET use BitMap
2741 -2 C_SYNC_TARGET set BitMap
2742 -100 after split brain, disconnect
2743-1000 unrelated data
Philipp Reisner4a23f262011-01-11 17:42:17 +01002744-1091 requires proto 91
2745-1096 requires proto 96
Philipp Reisnerb411b362009-09-25 16:07:19 -07002746 */
2747static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
2748{
2749 u64 self, peer;
2750 int i, j;
2751
2752 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2753 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2754
2755 *rule_nr = 10;
2756 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
2757 return 0;
2758
2759 *rule_nr = 20;
2760 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
2761 peer != UUID_JUST_CREATED)
2762 return -2;
2763
2764 *rule_nr = 30;
2765 if (self != UUID_JUST_CREATED &&
2766 (peer == UUID_JUST_CREATED || peer == (u64)0))
2767 return 2;
2768
2769 if (self == peer) {
2770 int rct, dc; /* roles at crash time */
2771
2772 if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
2773
Philipp Reisner31890f42011-01-19 14:12:51 +01002774 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002775 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002776
2777 if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
2778 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
2779 dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
Philipp Reisner9f2247b2012-08-16 14:25:58 +02002780 drbd_uuid_move_history(mdev);
2781 mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[UI_BITMAP];
2782 mdev->ldev->md.uuid[UI_BITMAP] = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002783
2784 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2785 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2786 *rule_nr = 34;
2787 } else {
2788 dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
2789 *rule_nr = 36;
2790 }
2791
2792 return 1;
2793 }
2794
2795 if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
2796
Philipp Reisner31890f42011-01-19 14:12:51 +01002797 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002798 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002799
2800 if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
2801 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
2802 dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
2803
2804 mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
2805 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
2806 mdev->p_uuid[UI_BITMAP] = 0UL;
2807
2808 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2809 *rule_nr = 35;
2810 } else {
2811 dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
2812 *rule_nr = 37;
2813 }
2814
2815 return -1;
2816 }
2817
2818 /* Common power [off|failure] */
2819 rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
2820 (mdev->p_uuid[UI_FLAGS] & 2);
2821 /* lowest bit is set when we were primary,
2822 * next bit (weight 2) is set when peer was primary */
2823 *rule_nr = 40;
2824
2825 switch (rct) {
2826 case 0: /* !self_pri && !peer_pri */ return 0;
2827 case 1: /* self_pri && !peer_pri */ return 1;
2828 case 2: /* !self_pri && peer_pri */ return -1;
2829 case 3: /* self_pri && peer_pri */
Lars Ellenberg427c0432012-08-01 12:43:01 +02002830 dc = test_bit(RESOLVE_CONFLICTS, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002831 return dc ? -1 : 1;
2832 }
2833 }
2834
2835 *rule_nr = 50;
2836 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2837 if (self == peer)
2838 return -1;
2839
2840 *rule_nr = 51;
2841 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2842 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002843 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002844 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) ==
2845 (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) :
2846 peer + UUID_NEW_BM_OFFSET == (mdev->p_uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002847 /* The last P_SYNC_UUID did not get though. Undo the last start of
2848 resync as sync source modifications of the peer's UUIDs. */
2849
Philipp Reisner31890f42011-01-19 14:12:51 +01002850 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002851 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002852
2853 mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
2854 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
Philipp Reisner4a23f262011-01-11 17:42:17 +01002855
Lars Ellenberg92b4ca22012-04-30 12:53:52 +02002856 dev_info(DEV, "Lost last syncUUID packet, corrected:\n");
Philipp Reisner4a23f262011-01-11 17:42:17 +01002857 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2858
Philipp Reisnerb411b362009-09-25 16:07:19 -07002859 return -1;
2860 }
2861 }
2862
2863 *rule_nr = 60;
2864 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2865 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2866 peer = mdev->p_uuid[i] & ~((u64)1);
2867 if (self == peer)
2868 return -2;
2869 }
2870
2871 *rule_nr = 70;
2872 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2873 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2874 if (self == peer)
2875 return 1;
2876
2877 *rule_nr = 71;
2878 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2879 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002880 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002881 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) ==
2882 (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) :
2883 self + UUID_NEW_BM_OFFSET == (mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002884 /* The last P_SYNC_UUID did not get though. Undo the last start of
2885 resync as sync source modifications of our UUIDs. */
2886
Philipp Reisner31890f42011-01-19 14:12:51 +01002887 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002888 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002889
Philipp Reisner9f2247b2012-08-16 14:25:58 +02002890 __drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
2891 __drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002892
Philipp Reisner4a23f262011-01-11 17:42:17 +01002893 dev_info(DEV, "Last syncUUID did not get through, corrected:\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002894 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2895 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2896
2897 return 1;
2898 }
2899 }
2900
2901
2902 *rule_nr = 80;
Philipp Reisnerd8c2a362009-11-18 15:52:51 +01002903 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002904 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2905 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2906 if (self == peer)
2907 return 2;
2908 }
2909
2910 *rule_nr = 90;
2911 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2912 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2913 if (self == peer && self != ((u64)0))
2914 return 100;
2915
2916 *rule_nr = 100;
2917 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2918 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2919 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
2920 peer = mdev->p_uuid[j] & ~((u64)1);
2921 if (self == peer)
2922 return -100;
2923 }
2924 }
2925
2926 return -1000;
2927}
2928
2929/* drbd_sync_handshake() returns the new conn state on success, or
2930 CONN_MASK (-1) on failure.
2931 */
2932static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
2933 enum drbd_disk_state peer_disk) __must_hold(local)
2934{
Philipp Reisnerb411b362009-09-25 16:07:19 -07002935 enum drbd_conns rv = C_MASK;
2936 enum drbd_disk_state mydisk;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002937 struct net_conf *nc;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02002938 int hg, rule_nr, rr_conflict, tentative;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002939
2940 mydisk = mdev->state.disk;
2941 if (mydisk == D_NEGOTIATING)
2942 mydisk = mdev->new_state_tmp.disk;
2943
2944 dev_info(DEV, "drbd_sync_handshake:\n");
Philipp Reisner9f2247b2012-08-16 14:25:58 +02002945
2946 spin_lock_irq(&mdev->ldev->md.uuid_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002947 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0);
2948 drbd_uuid_dump(mdev, "peer", mdev->p_uuid,
2949 mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2950
2951 hg = drbd_uuid_compare(mdev, &rule_nr);
Philipp Reisner9f2247b2012-08-16 14:25:58 +02002952 spin_unlock_irq(&mdev->ldev->md.uuid_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002953
2954 dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
2955
2956 if (hg == -1000) {
2957 dev_alert(DEV, "Unrelated data, aborting!\n");
2958 return C_MASK;
2959 }
Philipp Reisner4a23f262011-01-11 17:42:17 +01002960 if (hg < -1000) {
2961 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 -07002962 return C_MASK;
2963 }
2964
2965 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
2966 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
2967 int f = (hg == -100) || abs(hg) == 2;
2968 hg = mydisk > D_INCONSISTENT ? 1 : -1;
2969 if (f)
2970 hg = hg*2;
2971 dev_info(DEV, "Becoming sync %s due to disk states.\n",
2972 hg > 0 ? "source" : "target");
2973 }
2974
Adam Gandelman3a11a482010-04-08 16:48:23 -07002975 if (abs(hg) == 100)
2976 drbd_khelper(mdev, "initial-split-brain");
2977
Philipp Reisner44ed1672011-04-19 17:10:19 +02002978 rcu_read_lock();
2979 nc = rcu_dereference(mdev->tconn->net_conf);
2980
2981 if (hg == 100 || (hg == -100 && nc->always_asbp)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002982 int pcount = (mdev->state.role == R_PRIMARY)
2983 + (peer_role == R_PRIMARY);
2984 int forced = (hg == -100);
2985
2986 switch (pcount) {
2987 case 0:
2988 hg = drbd_asb_recover_0p(mdev);
2989 break;
2990 case 1:
2991 hg = drbd_asb_recover_1p(mdev);
2992 break;
2993 case 2:
2994 hg = drbd_asb_recover_2p(mdev);
2995 break;
2996 }
2997 if (abs(hg) < 100) {
2998 dev_warn(DEV, "Split-Brain detected, %d primaries, "
2999 "automatically solved. Sync from %s node\n",
3000 pcount, (hg < 0) ? "peer" : "this");
3001 if (forced) {
3002 dev_warn(DEV, "Doing a full sync, since"
3003 " UUIDs where ambiguous.\n");
3004 hg = hg*2;
3005 }
3006 }
3007 }
3008
3009 if (hg == -100) {
Philipp Reisner08b165b2011-09-05 16:22:33 +02003010 if (test_bit(DISCARD_MY_DATA, &mdev->flags) && !(mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003011 hg = -1;
Philipp Reisner08b165b2011-09-05 16:22:33 +02003012 if (!test_bit(DISCARD_MY_DATA, &mdev->flags) && (mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003013 hg = 1;
3014
3015 if (abs(hg) < 100)
3016 dev_warn(DEV, "Split-Brain detected, manually solved. "
3017 "Sync from %s node\n",
3018 (hg < 0) ? "peer" : "this");
3019 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02003020 rr_conflict = nc->rr_conflict;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02003021 tentative = nc->tentative;
Philipp Reisner44ed1672011-04-19 17:10:19 +02003022 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07003023
3024 if (hg == -100) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01003025 /* FIXME this log message is not correct if we end up here
3026 * after an attempted attach on a diskless node.
3027 * We just refuse to attach -- well, we drop the "connection"
3028 * to that disk, in a way... */
Adam Gandelman3a11a482010-04-08 16:48:23 -07003029 dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003030 drbd_khelper(mdev, "split-brain");
3031 return C_MASK;
3032 }
3033
3034 if (hg > 0 && mydisk <= D_INCONSISTENT) {
3035 dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
3036 return C_MASK;
3037 }
3038
3039 if (hg < 0 && /* by intention we do not use mydisk here. */
3040 mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02003041 switch (rr_conflict) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003042 case ASB_CALL_HELPER:
3043 drbd_khelper(mdev, "pri-lost");
3044 /* fall through */
3045 case ASB_DISCONNECT:
3046 dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
3047 return C_MASK;
3048 case ASB_VIOLENTLY:
3049 dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
3050 "assumption\n");
3051 }
3052 }
3053
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02003054 if (tentative || test_bit(CONN_DRY_RUN, &mdev->tconn->flags)) {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003055 if (hg == 0)
3056 dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
3057 else
3058 dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
3059 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
3060 abs(hg) >= 2 ? "full" : "bit-map based");
3061 return C_MASK;
3062 }
3063
Philipp Reisnerb411b362009-09-25 16:07:19 -07003064 if (abs(hg) >= 2) {
3065 dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003066 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake",
3067 BM_LOCKED_SET_ALLOWED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003068 return C_MASK;
3069 }
3070
3071 if (hg > 0) { /* become sync source. */
3072 rv = C_WF_BITMAP_S;
3073 } else if (hg < 0) { /* become sync target */
3074 rv = C_WF_BITMAP_T;
3075 } else {
3076 rv = C_CONNECTED;
3077 if (drbd_bm_total_weight(mdev)) {
3078 dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
3079 drbd_bm_total_weight(mdev));
3080 }
3081 }
3082
3083 return rv;
3084}
3085
Philipp Reisnerf179d762011-05-16 17:31:47 +02003086static enum drbd_after_sb_p convert_after_sb(enum drbd_after_sb_p peer)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003087{
3088 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003089 if (peer == ASB_DISCARD_REMOTE)
3090 return ASB_DISCARD_LOCAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003091
3092 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003093 if (peer == ASB_DISCARD_LOCAL)
3094 return ASB_DISCARD_REMOTE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003095
3096 /* everything else is valid if they are equal on both sides. */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003097 return peer;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003098}
3099
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003100static int receive_protocol(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003101{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003102 struct p_protocol *p = pi->data;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003103 enum drbd_after_sb_p p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
3104 int p_proto, p_discard_my_data, p_two_primaries, cf;
3105 struct net_conf *nc, *old_net_conf, *new_net_conf = NULL;
3106 char integrity_alg[SHARED_SECRET_MAX] = "";
Andreas Gruenbacheraccdbcc2011-07-15 17:41:09 +02003107 struct crypto_hash *peer_integrity_tfm = NULL;
Philipp Reisner7aca6c72011-05-17 10:12:56 +02003108 void *int_dig_in = NULL, *int_dig_vv = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003109
Philipp Reisnerb411b362009-09-25 16:07:19 -07003110 p_proto = be32_to_cpu(p->protocol);
3111 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
3112 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
3113 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003114 p_two_primaries = be32_to_cpu(p->two_primaries);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003115 cf = be32_to_cpu(p->conn_flags);
Andreas Gruenbacher6139f602011-05-06 20:00:02 +02003116 p_discard_my_data = cf & CF_DISCARD_MY_DATA;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003117
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003118 if (tconn->agreed_pro_version >= 87) {
3119 int err;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003120
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003121 if (pi->size > sizeof(integrity_alg))
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003122 return -EIO;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003123 err = drbd_recv_all(tconn, integrity_alg, pi->size);
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003124 if (err)
3125 return err;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003126 integrity_alg[SHARED_SECRET_MAX - 1] = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003127 }
3128
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003129 if (pi->cmd != P_PROTOCOL_UPDATE) {
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003130 clear_bit(CONN_DRY_RUN, &tconn->flags);
Philipp Reisner036b17e2011-05-16 17:38:11 +02003131
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003132 if (cf & CF_DRY_RUN)
3133 set_bit(CONN_DRY_RUN, &tconn->flags);
3134
3135 rcu_read_lock();
3136 nc = rcu_dereference(tconn->net_conf);
3137
3138 if (p_proto != nc->wire_protocol) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003139 conn_err(tconn, "incompatible %s settings\n", "protocol");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003140 goto disconnect_rcu_unlock;
3141 }
3142
3143 if (convert_after_sb(p_after_sb_0p) != nc->after_sb_0p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003144 conn_err(tconn, "incompatible %s settings\n", "after-sb-0pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003145 goto disconnect_rcu_unlock;
3146 }
3147
3148 if (convert_after_sb(p_after_sb_1p) != nc->after_sb_1p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003149 conn_err(tconn, "incompatible %s settings\n", "after-sb-1pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003150 goto disconnect_rcu_unlock;
3151 }
3152
3153 if (convert_after_sb(p_after_sb_2p) != nc->after_sb_2p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003154 conn_err(tconn, "incompatible %s settings\n", "after-sb-2pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003155 goto disconnect_rcu_unlock;
3156 }
3157
3158 if (p_discard_my_data && nc->discard_my_data) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003159 conn_err(tconn, "incompatible %s settings\n", "discard-my-data");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003160 goto disconnect_rcu_unlock;
3161 }
3162
3163 if (p_two_primaries != nc->two_primaries) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003164 conn_err(tconn, "incompatible %s settings\n", "allow-two-primaries");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003165 goto disconnect_rcu_unlock;
3166 }
3167
3168 if (strcmp(integrity_alg, nc->integrity_alg)) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003169 conn_err(tconn, "incompatible %s settings\n", "data-integrity-alg");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003170 goto disconnect_rcu_unlock;
3171 }
3172
3173 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07003174 }
3175
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003176 if (integrity_alg[0]) {
3177 int hash_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003178
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003179 /*
3180 * We can only change the peer data integrity algorithm
3181 * here. Changing our own data integrity algorithm
3182 * requires that we send a P_PROTOCOL_UPDATE packet at
3183 * the same time; otherwise, the peer has no way to
3184 * tell between which packets the algorithm should
3185 * change.
3186 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07003187
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003188 peer_integrity_tfm = crypto_alloc_hash(integrity_alg, 0, CRYPTO_ALG_ASYNC);
3189 if (!peer_integrity_tfm) {
3190 conn_err(tconn, "peer data-integrity-alg %s not supported\n",
3191 integrity_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003192 goto disconnect;
3193 }
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003194
3195 hash_size = crypto_hash_digestsize(peer_integrity_tfm);
3196 int_dig_in = kmalloc(hash_size, GFP_KERNEL);
3197 int_dig_vv = kmalloc(hash_size, GFP_KERNEL);
3198 if (!(int_dig_in && int_dig_vv)) {
3199 conn_err(tconn, "Allocation of buffers for data integrity checking failed\n");
3200 goto disconnect;
3201 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003202 }
3203
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003204 new_net_conf = kmalloc(sizeof(struct net_conf), GFP_KERNEL);
3205 if (!new_net_conf) {
3206 conn_err(tconn, "Allocation of new net_conf failed\n");
3207 goto disconnect;
3208 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003209
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003210 mutex_lock(&tconn->data.mutex);
3211 mutex_lock(&tconn->conf_update);
3212 old_net_conf = tconn->net_conf;
3213 *new_net_conf = *old_net_conf;
3214
3215 new_net_conf->wire_protocol = p_proto;
3216 new_net_conf->after_sb_0p = convert_after_sb(p_after_sb_0p);
3217 new_net_conf->after_sb_1p = convert_after_sb(p_after_sb_1p);
3218 new_net_conf->after_sb_2p = convert_after_sb(p_after_sb_2p);
3219 new_net_conf->two_primaries = p_two_primaries;
3220
3221 rcu_assign_pointer(tconn->net_conf, new_net_conf);
3222 mutex_unlock(&tconn->conf_update);
3223 mutex_unlock(&tconn->data.mutex);
3224
3225 crypto_free_hash(tconn->peer_integrity_tfm);
3226 kfree(tconn->int_dig_in);
3227 kfree(tconn->int_dig_vv);
3228 tconn->peer_integrity_tfm = peer_integrity_tfm;
3229 tconn->int_dig_in = int_dig_in;
3230 tconn->int_dig_vv = int_dig_vv;
3231
3232 if (strcmp(old_net_conf->integrity_alg, integrity_alg))
3233 conn_info(tconn, "peer data-integrity-alg: %s\n",
3234 integrity_alg[0] ? integrity_alg : "(none)");
3235
3236 synchronize_rcu();
3237 kfree(old_net_conf);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003238 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003239
Philipp Reisner44ed1672011-04-19 17:10:19 +02003240disconnect_rcu_unlock:
3241 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07003242disconnect:
Andreas Gruenbacherb792c352011-07-15 16:48:49 +02003243 crypto_free_hash(peer_integrity_tfm);
Philipp Reisner036b17e2011-05-16 17:38:11 +02003244 kfree(int_dig_in);
3245 kfree(int_dig_vv);
Philipp Reisner72046242011-03-15 18:51:47 +01003246 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003247 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003248}
3249
3250/* helper function
3251 * input: alg name, feature name
3252 * return: NULL (alg name was "")
3253 * ERR_PTR(error) if something goes wrong
3254 * or the crypto hash ptr, if it worked out ok. */
3255struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
3256 const char *alg, const char *name)
3257{
3258 struct crypto_hash *tfm;
3259
3260 if (!alg[0])
3261 return NULL;
3262
3263 tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
3264 if (IS_ERR(tfm)) {
3265 dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
3266 alg, name, PTR_ERR(tfm));
3267 return tfm;
3268 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003269 return tfm;
3270}
3271
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003272static int ignore_remaining_packet(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003273{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003274 void *buffer = tconn->data.rbuf;
3275 int size = pi->size;
3276
3277 while (size) {
3278 int s = min_t(int, size, DRBD_SOCKET_BUFFER_SIZE);
3279 s = drbd_recv(tconn, buffer, s);
3280 if (s <= 0) {
3281 if (s < 0)
3282 return s;
3283 break;
3284 }
3285 size -= s;
3286 }
3287 if (size)
3288 return -EIO;
3289 return 0;
3290}
3291
3292/*
3293 * config_unknown_volume - device configuration command for unknown volume
3294 *
3295 * When a device is added to an existing connection, the node on which the
3296 * device is added first will send configuration commands to its peer but the
3297 * peer will not know about the device yet. It will warn and ignore these
3298 * commands. Once the device is added on the second node, the second node will
3299 * send the same device configuration commands, but in the other direction.
3300 *
3301 * (We can also end up here if drbd is misconfigured.)
3302 */
3303static int config_unknown_volume(struct drbd_tconn *tconn, struct packet_info *pi)
3304{
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02003305 conn_warn(tconn, "%s packet received for volume %u, which is not configured locally\n",
3306 cmdname(pi->cmd), pi->vnr);
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003307 return ignore_remaining_packet(tconn, pi);
3308}
3309
3310static int receive_SyncParam(struct drbd_tconn *tconn, struct packet_info *pi)
3311{
3312 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003313 struct p_rs_param_95 *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003314 unsigned int header_size, data_size, exp_max_sz;
3315 struct crypto_hash *verify_tfm = NULL;
3316 struct crypto_hash *csums_tfm = NULL;
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003317 struct net_conf *old_net_conf, *new_net_conf = NULL;
Philipp Reisner813472c2011-05-03 16:47:02 +02003318 struct disk_conf *old_disk_conf = NULL, *new_disk_conf = NULL;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003319 const int apv = tconn->agreed_pro_version;
Philipp Reisner813472c2011-05-03 16:47:02 +02003320 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
Philipp Reisner778f2712010-07-06 11:14:00 +02003321 int fifo_size = 0;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003322 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003323
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003324 mdev = vnr_to_mdev(tconn, pi->vnr);
3325 if (!mdev)
3326 return config_unknown_volume(tconn, pi);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003327
3328 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
3329 : apv == 88 ? sizeof(struct p_rs_param)
3330 + SHARED_SECRET_MAX
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003331 : apv <= 94 ? sizeof(struct p_rs_param_89)
3332 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003333
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003334 if (pi->size > exp_max_sz) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003335 dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003336 pi->size, exp_max_sz);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003337 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003338 }
3339
3340 if (apv <= 88) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003341 header_size = sizeof(struct p_rs_param);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003342 data_size = pi->size - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003343 } else if (apv <= 94) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003344 header_size = sizeof(struct p_rs_param_89);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003345 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003346 D_ASSERT(data_size == 0);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003347 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003348 header_size = sizeof(struct p_rs_param_95);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003349 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003350 D_ASSERT(data_size == 0);
3351 }
3352
3353 /* initialize verify_alg and csums_alg */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003354 p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003355 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
3356
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003357 err = drbd_recv_all(mdev->tconn, p, header_size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003358 if (err)
3359 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003360
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003361 mutex_lock(&mdev->tconn->conf_update);
3362 old_net_conf = mdev->tconn->net_conf;
Philipp Reisner813472c2011-05-03 16:47:02 +02003363 if (get_ldev(mdev)) {
3364 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3365 if (!new_disk_conf) {
3366 put_ldev(mdev);
3367 mutex_unlock(&mdev->tconn->conf_update);
3368 dev_err(DEV, "Allocation of new disk_conf failed\n");
3369 return -ENOMEM;
3370 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003371
Philipp Reisner813472c2011-05-03 16:47:02 +02003372 old_disk_conf = mdev->ldev->disk_conf;
3373 *new_disk_conf = *old_disk_conf;
3374
Andreas Gruenbacher6394b932011-05-11 14:29:52 +02003375 new_disk_conf->resync_rate = be32_to_cpu(p->resync_rate);
Philipp Reisner813472c2011-05-03 16:47:02 +02003376 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003377
3378 if (apv >= 88) {
3379 if (apv == 88) {
Philipp Reisner5de73822012-03-28 10:17:32 +02003380 if (data_size > SHARED_SECRET_MAX || data_size == 0) {
3381 dev_err(DEV, "verify-alg of wrong size, "
3382 "peer wants %u, accepting only up to %u byte\n",
3383 data_size, SHARED_SECRET_MAX);
Philipp Reisner813472c2011-05-03 16:47:02 +02003384 err = -EIO;
3385 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003386 }
3387
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003388 err = drbd_recv_all(mdev->tconn, p->verify_alg, data_size);
Philipp Reisner813472c2011-05-03 16:47:02 +02003389 if (err)
3390 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003391 /* we expect NUL terminated string */
3392 /* but just in case someone tries to be evil */
3393 D_ASSERT(p->verify_alg[data_size-1] == 0);
3394 p->verify_alg[data_size-1] = 0;
3395
3396 } else /* apv >= 89 */ {
3397 /* we still expect NUL terminated strings */
3398 /* but just in case someone tries to be evil */
3399 D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
3400 D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
3401 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
3402 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
3403 }
3404
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003405 if (strcmp(old_net_conf->verify_alg, p->verify_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003406 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3407 dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003408 old_net_conf->verify_alg, p->verify_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003409 goto disconnect;
3410 }
3411 verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
3412 p->verify_alg, "verify-alg");
3413 if (IS_ERR(verify_tfm)) {
3414 verify_tfm = NULL;
3415 goto disconnect;
3416 }
3417 }
3418
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003419 if (apv >= 89 && strcmp(old_net_conf->csums_alg, p->csums_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003420 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3421 dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003422 old_net_conf->csums_alg, p->csums_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003423 goto disconnect;
3424 }
3425 csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
3426 p->csums_alg, "csums-alg");
3427 if (IS_ERR(csums_tfm)) {
3428 csums_tfm = NULL;
3429 goto disconnect;
3430 }
3431 }
3432
Philipp Reisner813472c2011-05-03 16:47:02 +02003433 if (apv > 94 && new_disk_conf) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003434 new_disk_conf->c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
3435 new_disk_conf->c_delay_target = be32_to_cpu(p->c_delay_target);
3436 new_disk_conf->c_fill_target = be32_to_cpu(p->c_fill_target);
3437 new_disk_conf->c_max_rate = be32_to_cpu(p->c_max_rate);
Philipp Reisner778f2712010-07-06 11:14:00 +02003438
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003439 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Philipp Reisner9958c852011-05-03 16:19:31 +02003440 if (fifo_size != mdev->rs_plan_s->size) {
Philipp Reisner813472c2011-05-03 16:47:02 +02003441 new_plan = fifo_alloc(fifo_size);
3442 if (!new_plan) {
Philipp Reisner778f2712010-07-06 11:14:00 +02003443 dev_err(DEV, "kmalloc of fifo_buffer failed");
Lars Ellenbergf3990022011-03-23 14:31:09 +01003444 put_ldev(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02003445 goto disconnect;
3446 }
3447 }
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003448 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003449
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003450 if (verify_tfm || csums_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003451 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
3452 if (!new_net_conf) {
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003453 dev_err(DEV, "Allocation of new net_conf failed\n");
3454 goto disconnect;
3455 }
3456
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003457 *new_net_conf = *old_net_conf;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003458
3459 if (verify_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003460 strcpy(new_net_conf->verify_alg, p->verify_alg);
3461 new_net_conf->verify_alg_len = strlen(p->verify_alg) + 1;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003462 crypto_free_hash(mdev->tconn->verify_tfm);
3463 mdev->tconn->verify_tfm = verify_tfm;
3464 dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
3465 }
3466 if (csums_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003467 strcpy(new_net_conf->csums_alg, p->csums_alg);
3468 new_net_conf->csums_alg_len = strlen(p->csums_alg) + 1;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003469 crypto_free_hash(mdev->tconn->csums_tfm);
3470 mdev->tconn->csums_tfm = csums_tfm;
3471 dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
3472 }
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003473 rcu_assign_pointer(tconn->net_conf, new_net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003474 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003475 }
3476
Philipp Reisner813472c2011-05-03 16:47:02 +02003477 if (new_disk_conf) {
3478 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3479 put_ldev(mdev);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003480 }
Philipp Reisner813472c2011-05-03 16:47:02 +02003481
3482 if (new_plan) {
3483 old_plan = mdev->rs_plan_s;
3484 rcu_assign_pointer(mdev->rs_plan_s, new_plan);
3485 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003486
3487 mutex_unlock(&mdev->tconn->conf_update);
3488 synchronize_rcu();
3489 if (new_net_conf)
3490 kfree(old_net_conf);
3491 kfree(old_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02003492 kfree(old_plan);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003493
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003494 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003495
Philipp Reisner813472c2011-05-03 16:47:02 +02003496reconnect:
3497 if (new_disk_conf) {
3498 put_ldev(mdev);
3499 kfree(new_disk_conf);
3500 }
3501 mutex_unlock(&mdev->tconn->conf_update);
3502 return -EIO;
3503
Philipp Reisnerb411b362009-09-25 16:07:19 -07003504disconnect:
Philipp Reisner813472c2011-05-03 16:47:02 +02003505 kfree(new_plan);
3506 if (new_disk_conf) {
3507 put_ldev(mdev);
3508 kfree(new_disk_conf);
3509 }
Philipp Reisnera0095502011-05-03 13:14:15 +02003510 mutex_unlock(&mdev->tconn->conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003511 /* just for completeness: actually not needed,
3512 * as this is not reached if csums_tfm was ok. */
3513 crypto_free_hash(csums_tfm);
3514 /* but free the verify_tfm again, if csums_tfm did not work out */
3515 crypto_free_hash(verify_tfm);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003516 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003517 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003518}
3519
Philipp Reisnerb411b362009-09-25 16:07:19 -07003520/* warn if the arguments differ by more than 12.5% */
3521static void warn_if_differ_considerably(struct drbd_conf *mdev,
3522 const char *s, sector_t a, sector_t b)
3523{
3524 sector_t d;
3525 if (a == 0 || b == 0)
3526 return;
3527 d = (a > b) ? (a - b) : (b - a);
3528 if (d > (a>>3) || d > (b>>3))
3529 dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
3530 (unsigned long long)a, (unsigned long long)b);
3531}
3532
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003533static int receive_sizes(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003534{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003535 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003536 struct p_sizes *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003537 enum determine_dev_size dd = unchanged;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003538 sector_t p_size, p_usize, my_usize;
3539 int ldsc = 0; /* local disk size changed */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003540 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003541
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003542 mdev = vnr_to_mdev(tconn, pi->vnr);
3543 if (!mdev)
3544 return config_unknown_volume(tconn, pi);
3545
Philipp Reisnerb411b362009-09-25 16:07:19 -07003546 p_size = be64_to_cpu(p->d_size);
3547 p_usize = be64_to_cpu(p->u_size);
3548
Philipp Reisnerb411b362009-09-25 16:07:19 -07003549 /* just store the peer's disk size for now.
3550 * we still need to figure out whether we accept that. */
3551 mdev->p_size = p_size;
3552
Philipp Reisnerb411b362009-09-25 16:07:19 -07003553 if (get_ldev(mdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003554 rcu_read_lock();
3555 my_usize = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
3556 rcu_read_unlock();
3557
Philipp Reisnerb411b362009-09-25 16:07:19 -07003558 warn_if_differ_considerably(mdev, "lower level device sizes",
3559 p_size, drbd_get_max_capacity(mdev->ldev));
3560 warn_if_differ_considerably(mdev, "user requested size",
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003561 p_usize, my_usize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003562
3563 /* if this is the first connect, or an otherwise expected
3564 * param exchange, choose the minimum */
3565 if (mdev->state.conn == C_WF_REPORT_PARAMS)
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003566 p_usize = min_not_zero(my_usize, p_usize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003567
3568 /* Never shrink a device with usable data during connect.
3569 But allow online shrinking if we are connected. */
Philipp Reisneref5e44a2011-05-03 13:27:43 +02003570 if (drbd_new_dev_size(mdev, mdev->ldev, p_usize, 0) <
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003571 drbd_get_capacity(mdev->this_bdev) &&
3572 mdev->state.disk >= D_OUTDATED &&
3573 mdev->state.conn < C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003574 dev_err(DEV, "The peer's disk size is too small!\n");
Philipp Reisner38fa9982011-03-15 18:24:49 +01003575 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003576 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003577 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003578 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003579
3580 if (my_usize != p_usize) {
3581 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
3582
3583 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3584 if (!new_disk_conf) {
3585 dev_err(DEV, "Allocation of new disk_conf failed\n");
3586 put_ldev(mdev);
3587 return -ENOMEM;
3588 }
3589
3590 mutex_lock(&mdev->tconn->conf_update);
3591 old_disk_conf = mdev->ldev->disk_conf;
3592 *new_disk_conf = *old_disk_conf;
3593 new_disk_conf->disk_size = p_usize;
3594
3595 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3596 mutex_unlock(&mdev->tconn->conf_update);
3597 synchronize_rcu();
3598 kfree(old_disk_conf);
3599
3600 dev_info(DEV, "Peer sets u_size to %lu sectors\n",
3601 (unsigned long)my_usize);
3602 }
3603
Philipp Reisnerb411b362009-09-25 16:07:19 -07003604 put_ldev(mdev);
3605 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003606
Philipp Reisnere89b5912010-03-24 17:11:33 +01003607 ddsf = be16_to_cpu(p->dds_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003608 if (get_ldev(mdev)) {
Bart Van Assche24c48302011-05-21 18:32:29 +02003609 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003610 put_ldev(mdev);
3611 if (dd == dev_size_error)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003612 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003613 drbd_md_sync(mdev);
3614 } else {
3615 /* I am diskless, need to accept the peer's size. */
3616 drbd_set_my_capacity(mdev, p_size);
3617 }
3618
Philipp Reisner99432fc2011-05-20 16:39:13 +02003619 mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size);
3620 drbd_reconsider_max_bio_size(mdev);
3621
Philipp Reisnerb411b362009-09-25 16:07:19 -07003622 if (get_ldev(mdev)) {
3623 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
3624 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
3625 ldsc = 1;
3626 }
3627
Philipp Reisnerb411b362009-09-25 16:07:19 -07003628 put_ldev(mdev);
3629 }
3630
3631 if (mdev->state.conn > C_WF_REPORT_PARAMS) {
3632 if (be64_to_cpu(p->c_size) !=
3633 drbd_get_capacity(mdev->this_bdev) || ldsc) {
3634 /* we have different sizes, probably peer
3635 * needs to know my new size... */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003636 drbd_send_sizes(mdev, 0, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003637 }
3638 if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
3639 (dd == grew && mdev->state.conn == C_CONNECTED)) {
3640 if (mdev->state.pdsk >= D_INCONSISTENT &&
Philipp Reisnere89b5912010-03-24 17:11:33 +01003641 mdev->state.disk >= D_INCONSISTENT) {
3642 if (ddsf & DDSF_NO_RESYNC)
3643 dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
3644 else
3645 resync_after_online_grow(mdev);
3646 } else
Philipp Reisnerb411b362009-09-25 16:07:19 -07003647 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
3648 }
3649 }
3650
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003651 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003652}
3653
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003654static int receive_uuids(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003655{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003656 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003657 struct p_uuids *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003658 u64 *p_uuid;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003659 int i, updated_uuids = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003660
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003661 mdev = vnr_to_mdev(tconn, pi->vnr);
3662 if (!mdev)
3663 return config_unknown_volume(tconn, pi);
3664
Philipp Reisnerb411b362009-09-25 16:07:19 -07003665 p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
Jing Wang063eacf2012-10-25 15:00:56 +08003666 if (!p_uuid) {
3667 dev_err(DEV, "kmalloc of p_uuid failed\n");
3668 return false;
3669 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003670
3671 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
3672 p_uuid[i] = be64_to_cpu(p->uuid[i]);
3673
3674 kfree(mdev->p_uuid);
3675 mdev->p_uuid = p_uuid;
3676
3677 if (mdev->state.conn < C_CONNECTED &&
3678 mdev->state.disk < D_INCONSISTENT &&
3679 mdev->state.role == R_PRIMARY &&
3680 (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
3681 dev_err(DEV, "Can only connect to data with current UUID=%016llX\n",
3682 (unsigned long long)mdev->ed_uuid);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003683 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003684 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003685 }
3686
3687 if (get_ldev(mdev)) {
3688 int skip_initial_sync =
3689 mdev->state.conn == C_CONNECTED &&
Philipp Reisner31890f42011-01-19 14:12:51 +01003690 mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003691 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
3692 (p_uuid[UI_FLAGS] & 8);
3693 if (skip_initial_sync) {
3694 dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n");
3695 drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003696 "clear_n_write from receive_uuids",
3697 BM_LOCKED_TEST_ALLOWED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003698 _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]);
3699 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3700 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3701 CS_VERBOSE, NULL);
3702 drbd_md_sync(mdev);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003703 updated_uuids = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003704 }
3705 put_ldev(mdev);
Philipp Reisner18a50fa2010-06-21 14:14:15 +02003706 } else if (mdev->state.disk < D_INCONSISTENT &&
3707 mdev->state.role == R_PRIMARY) {
3708 /* I am a diskless primary, the peer just created a new current UUID
3709 for me. */
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003710 updated_uuids = drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003711 }
3712
3713 /* Before we test for the disk state, we should wait until an eventually
3714 ongoing cluster wide state change is finished. That is important if
3715 we are primary and are detaching from our disk. We need to see the
3716 new disk state... */
Philipp Reisner8410da82011-02-11 20:11:10 +01003717 mutex_lock(mdev->state_mutex);
3718 mutex_unlock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003719 if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT)
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003720 updated_uuids |= drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
3721
3722 if (updated_uuids)
3723 drbd_print_uuids(mdev, "receiver updated UUIDs to");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003724
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003725 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003726}
3727
3728/**
3729 * convert_state() - Converts the peer's view of the cluster state to our point of view
3730 * @ps: The state as seen by the peer.
3731 */
3732static union drbd_state convert_state(union drbd_state ps)
3733{
3734 union drbd_state ms;
3735
3736 static enum drbd_conns c_tab[] = {
Philipp Reisner369bea62011-07-06 23:04:44 +02003737 [C_WF_REPORT_PARAMS] = C_WF_REPORT_PARAMS,
Philipp Reisnerb411b362009-09-25 16:07:19 -07003738 [C_CONNECTED] = C_CONNECTED,
3739
3740 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
3741 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
3742 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
3743 [C_VERIFY_S] = C_VERIFY_T,
3744 [C_MASK] = C_MASK,
3745 };
3746
3747 ms.i = ps.i;
3748
3749 ms.conn = c_tab[ps.conn];
3750 ms.peer = ps.role;
3751 ms.role = ps.peer;
3752 ms.pdsk = ps.disk;
3753 ms.disk = ps.pdsk;
3754 ms.peer_isp = (ps.aftr_isp | ps.user_isp);
3755
3756 return ms;
3757}
3758
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003759static int receive_req_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003760{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003761 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003762 struct p_req_state *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003763 union drbd_state mask, val;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01003764 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003765
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003766 mdev = vnr_to_mdev(tconn, pi->vnr);
3767 if (!mdev)
3768 return -EIO;
3769
Philipp Reisnerb411b362009-09-25 16:07:19 -07003770 mask.i = be32_to_cpu(p->mask);
3771 val.i = be32_to_cpu(p->val);
3772
Lars Ellenberg427c0432012-08-01 12:43:01 +02003773 if (test_bit(RESOLVE_CONFLICTS, &mdev->tconn->flags) &&
Philipp Reisner8410da82011-02-11 20:11:10 +01003774 mutex_is_locked(mdev->state_mutex)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003775 drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003776 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003777 }
3778
3779 mask = convert_state(mask);
3780 val = convert_state(val);
3781
3782 rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003783 drbd_send_sr_reply(mdev, rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003784
Philipp Reisnerb411b362009-09-25 16:07:19 -07003785 drbd_md_sync(mdev);
3786
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003787 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003788}
3789
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003790static int receive_req_conn_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003791{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003792 struct p_req_state *p = pi->data;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003793 union drbd_state mask, val;
3794 enum drbd_state_rv rv;
3795
3796 mask.i = be32_to_cpu(p->mask);
3797 val.i = be32_to_cpu(p->val);
3798
Lars Ellenberg427c0432012-08-01 12:43:01 +02003799 if (test_bit(RESOLVE_CONFLICTS, &tconn->flags) &&
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003800 mutex_is_locked(&tconn->cstate_mutex)) {
3801 conn_send_sr_reply(tconn, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003802 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003803 }
3804
3805 mask = convert_state(mask);
3806 val = convert_state(val);
3807
Philipp Reisner778bcf22011-03-28 12:55:03 +02003808 rv = conn_request_state(tconn, mask, val, CS_VERBOSE | CS_LOCAL_ONLY | CS_IGN_OUTD_FAIL);
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003809 conn_send_sr_reply(tconn, rv);
3810
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003811 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003812}
3813
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003814static int receive_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003815{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003816 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003817 struct p_state *p = pi->data;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003818 union drbd_state os, ns, peer_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003819 enum drbd_disk_state real_peer_disk;
Philipp Reisner65d922c2010-06-16 16:18:09 +02003820 enum chg_state_flags cs_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003821 int rv;
3822
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003823 mdev = vnr_to_mdev(tconn, pi->vnr);
3824 if (!mdev)
3825 return config_unknown_volume(tconn, pi);
3826
Philipp Reisnerb411b362009-09-25 16:07:19 -07003827 peer_state.i = be32_to_cpu(p->state);
3828
3829 real_peer_disk = peer_state.disk;
3830 if (peer_state.disk == D_NEGOTIATING) {
3831 real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
3832 dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
3833 }
3834
Philipp Reisner87eeee42011-01-19 14:16:30 +01003835 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003836 retry:
Philipp Reisner78bae592011-03-28 15:40:12 +02003837 os = ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003838 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003839
Lars Ellenberg545752d2011-12-05 14:39:25 +01003840 /* If some other part of the code (asender thread, timeout)
3841 * already decided to close the connection again,
3842 * we must not "re-establish" it here. */
3843 if (os.conn <= C_TEAR_DOWN)
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003844 return -ECONNRESET;
Lars Ellenberg545752d2011-12-05 14:39:25 +01003845
Lars Ellenberg40424e42011-09-26 15:24:56 +02003846 /* If this is the "end of sync" confirmation, usually the peer disk
3847 * transitions from D_INCONSISTENT to D_UP_TO_DATE. For empty (0 bits
3848 * set) resync started in PausedSyncT, or if the timing of pause-/
3849 * unpause-sync events has been "just right", the peer disk may
3850 * transition from D_CONSISTENT to D_UP_TO_DATE as well.
3851 */
3852 if ((os.pdsk == D_INCONSISTENT || os.pdsk == D_CONSISTENT) &&
3853 real_peer_disk == D_UP_TO_DATE &&
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003854 os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) {
3855 /* If we are (becoming) SyncSource, but peer is still in sync
3856 * preparation, ignore its uptodate-ness to avoid flapping, it
3857 * will change to inconsistent once the peer reaches active
3858 * syncing states.
3859 * It may have changed syncer-paused flags, however, so we
3860 * cannot ignore this completely. */
3861 if (peer_state.conn > C_CONNECTED &&
3862 peer_state.conn < C_SYNC_SOURCE)
3863 real_peer_disk = D_INCONSISTENT;
3864
3865 /* if peer_state changes to connected at the same time,
3866 * it explicitly notifies us that it finished resync.
3867 * Maybe we should finish it up, too? */
3868 else if (os.conn >= C_SYNC_SOURCE &&
3869 peer_state.conn == C_CONNECTED) {
3870 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed)
3871 drbd_resync_finished(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003872 return 0;
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003873 }
3874 }
3875
Lars Ellenberg02b91b52012-06-28 18:26:52 +02003876 /* explicit verify finished notification, stop sector reached. */
3877 if (os.conn == C_VERIFY_T && os.disk == D_UP_TO_DATE &&
3878 peer_state.conn == C_CONNECTED && real_peer_disk == D_UP_TO_DATE) {
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003879 ov_out_of_sync_print(mdev);
Lars Ellenberg02b91b52012-06-28 18:26:52 +02003880 drbd_resync_finished(mdev);
Lars Ellenberg58ffa582012-07-26 14:09:49 +02003881 return 0;
Lars Ellenberg02b91b52012-06-28 18:26:52 +02003882 }
3883
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003884 /* peer says his disk is inconsistent, while we think it is uptodate,
3885 * and this happens while the peer still thinks we have a sync going on,
3886 * but we think we are already done with the sync.
3887 * We ignore this to avoid flapping pdsk.
3888 * This should not happen, if the peer is a recent version of drbd. */
3889 if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT &&
3890 os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE)
3891 real_peer_disk = D_UP_TO_DATE;
3892
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003893 if (ns.conn == C_WF_REPORT_PARAMS)
3894 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003895
Philipp Reisner67531712010-10-27 12:21:30 +02003896 if (peer_state.conn == C_AHEAD)
3897 ns.conn = C_BEHIND;
3898
Philipp Reisnerb411b362009-09-25 16:07:19 -07003899 if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING &&
3900 get_ldev_if_state(mdev, D_NEGOTIATING)) {
3901 int cr; /* consider resync */
3902
3903 /* if we established a new connection */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003904 cr = (os.conn < C_CONNECTED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003905 /* if we had an established connection
3906 * and one of the nodes newly attaches a disk */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003907 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003908 (peer_state.disk == D_NEGOTIATING ||
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003909 os.disk == D_NEGOTIATING));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003910 /* if we have both been inconsistent, and the peer has been
3911 * forced to be UpToDate with --overwrite-data */
3912 cr |= test_bit(CONSIDER_RESYNC, &mdev->flags);
3913 /* if we had been plain connected, and the admin requested to
3914 * start a sync by "invalidate" or "invalidate-remote" */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003915 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003916 (peer_state.conn >= C_STARTING_SYNC_S &&
3917 peer_state.conn <= C_WF_BITMAP_T));
3918
3919 if (cr)
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003920 ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003921
3922 put_ldev(mdev);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003923 if (ns.conn == C_MASK) {
3924 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003925 if (mdev->state.disk == D_NEGOTIATING) {
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003926 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003927 } else if (peer_state.disk == D_NEGOTIATING) {
3928 dev_err(DEV, "Disk attach process on the peer node was aborted.\n");
3929 peer_state.disk = D_DISKLESS;
Lars Ellenberg580b9762010-02-26 23:15:23 +01003930 real_peer_disk = D_DISKLESS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003931 } else {
Philipp Reisner8169e412011-03-15 18:40:27 +01003932 if (test_and_clear_bit(CONN_DRY_RUN, &mdev->tconn->flags))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003933 return -EIO;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003934 D_ASSERT(os.conn == C_WF_REPORT_PARAMS);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003935 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003936 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003937 }
3938 }
3939 }
3940
Philipp Reisner87eeee42011-01-19 14:16:30 +01003941 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner78bae592011-03-28 15:40:12 +02003942 if (os.i != drbd_read_state(mdev).i)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003943 goto retry;
3944 clear_bit(CONSIDER_RESYNC, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003945 ns.peer = peer_state.role;
3946 ns.pdsk = real_peer_disk;
3947 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003948 if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003949 ns.disk = mdev->new_state_tmp.disk;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003950 cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD);
Philipp Reisner2aebfab2011-03-28 16:48:11 +02003951 if (ns.pdsk == D_CONSISTENT && drbd_suspended(mdev) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED &&
Philipp Reisner481c6f52010-06-22 14:03:27 +02003952 test_bit(NEW_CUR_UUID, &mdev->flags)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01003953 /* Do not allow tl_restart(RESEND) for a rebooted peer. We can only allow this
Philipp Reisner481c6f52010-06-22 14:03:27 +02003954 for temporal network outages! */
Philipp Reisner87eeee42011-01-19 14:16:30 +01003955 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003956 dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01003957 tl_clear(mdev->tconn);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003958 drbd_uuid_new_current(mdev);
3959 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003960 conn_request_state(mdev->tconn, NS2(conn, C_PROTOCOL_ERROR, susp, 0), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003961 return -EIO;
Philipp Reisner481c6f52010-06-22 14:03:27 +02003962 }
Philipp Reisner65d922c2010-06-16 16:18:09 +02003963 rv = _drbd_set_state(mdev, ns, cs_flags, NULL);
Philipp Reisner78bae592011-03-28 15:40:12 +02003964 ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003965 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003966
3967 if (rv < SS_SUCCESS) {
Philipp Reisner38fa9982011-03-15 18:24:49 +01003968 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003969 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003970 }
3971
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003972 if (os.conn > C_WF_REPORT_PARAMS) {
3973 if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003974 peer_state.disk != D_NEGOTIATING ) {
3975 /* we want resync, peer has not yet decided to sync... */
3976 /* Nowadays only used when forcing a node into primary role and
3977 setting its disk to UpToDate with that */
3978 drbd_send_uuids(mdev);
Lars Ellenbergf479ea02011-10-27 16:52:30 +02003979 drbd_send_current_state(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003980 }
3981 }
3982
Philipp Reisner08b165b2011-09-05 16:22:33 +02003983 clear_bit(DISCARD_MY_DATA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003984
3985 drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
3986
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003987 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003988}
3989
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003990static int receive_sync_uuid(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003991{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003992 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003993 struct p_rs_uuid *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003994
3995 mdev = vnr_to_mdev(tconn, pi->vnr);
3996 if (!mdev)
3997 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003998
3999 wait_event(mdev->misc_wait,
4000 mdev->state.conn == C_WF_SYNC_UUID ||
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02004001 mdev->state.conn == C_BEHIND ||
Philipp Reisnerb411b362009-09-25 16:07:19 -07004002 mdev->state.conn < C_CONNECTED ||
4003 mdev->state.disk < D_NEGOTIATING);
4004
4005 /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */
4006
Philipp Reisnerb411b362009-09-25 16:07:19 -07004007 /* Here the _drbd_uuid_ functions are right, current should
4008 _not_ be rotated into the history */
4009 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
4010 _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid));
4011 _drbd_uuid_set(mdev, UI_BITMAP, 0UL);
4012
Lars Ellenberg62b0da32011-01-20 13:25:21 +01004013 drbd_print_uuids(mdev, "updated sync uuid");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004014 drbd_start_resync(mdev, C_SYNC_TARGET);
4015
4016 put_ldev(mdev);
4017 } else
4018 dev_err(DEV, "Ignoring SyncUUID packet!\n");
4019
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004020 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004021}
4022
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004023/**
4024 * receive_bitmap_plain
4025 *
4026 * Return 0 when done, 1 when another iteration is needed, and a negative error
4027 * code upon failure.
4028 */
4029static int
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004030receive_bitmap_plain(struct drbd_conf *mdev, unsigned int size,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004031 unsigned long *p, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004032{
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004033 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE -
4034 drbd_header_size(mdev->tconn);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004035 unsigned int num_words = min_t(size_t, data_size / sizeof(*p),
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004036 c->bm_words - c->word_offset);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004037 unsigned int want = num_words * sizeof(*p);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004038 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004039
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004040 if (want != size) {
4041 dev_err(DEV, "%s:want (%u) != size (%u)\n", __func__, want, size);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004042 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004043 }
4044 if (want == 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004045 return 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004046 err = drbd_recv_all(mdev->tconn, p, want);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004047 if (err)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004048 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004049
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004050 drbd_bm_merge_lel(mdev, c->word_offset, num_words, p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004051
4052 c->word_offset += num_words;
4053 c->bit_offset = c->word_offset * BITS_PER_LONG;
4054 if (c->bit_offset > c->bm_bits)
4055 c->bit_offset = c->bm_bits;
4056
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004057 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004058}
4059
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004060static enum drbd_bitmap_code dcbp_get_code(struct p_compressed_bm *p)
4061{
4062 return (enum drbd_bitmap_code)(p->encoding & 0x0f);
4063}
4064
4065static int dcbp_get_start(struct p_compressed_bm *p)
4066{
4067 return (p->encoding & 0x80) != 0;
4068}
4069
4070static int dcbp_get_pad_bits(struct p_compressed_bm *p)
4071{
4072 return (p->encoding >> 4) & 0x7;
4073}
4074
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004075/**
4076 * recv_bm_rle_bits
4077 *
4078 * Return 0 when done, 1 when another iteration is needed, and a negative error
4079 * code upon failure.
4080 */
4081static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07004082recv_bm_rle_bits(struct drbd_conf *mdev,
4083 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01004084 struct bm_xfer_ctx *c,
4085 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004086{
4087 struct bitstream bs;
4088 u64 look_ahead;
4089 u64 rl;
4090 u64 tmp;
4091 unsigned long s = c->bit_offset;
4092 unsigned long e;
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004093 int toggle = dcbp_get_start(p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004094 int have;
4095 int bits;
4096
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004097 bitstream_init(&bs, p->code, len, dcbp_get_pad_bits(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004098
4099 bits = bitstream_get_bits(&bs, &look_ahead, 64);
4100 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004101 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004102
4103 for (have = bits; have > 0; s += rl, toggle = !toggle) {
4104 bits = vli_decode_bits(&rl, look_ahead);
4105 if (bits <= 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004106 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004107
4108 if (toggle) {
4109 e = s + rl -1;
4110 if (e >= c->bm_bits) {
4111 dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004112 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004113 }
4114 _drbd_bm_set_bits(mdev, s, e);
4115 }
4116
4117 if (have < bits) {
4118 dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
4119 have, bits, look_ahead,
4120 (unsigned int)(bs.cur.b - p->code),
4121 (unsigned int)bs.buf_len);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004122 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004123 }
4124 look_ahead >>= bits;
4125 have -= bits;
4126
4127 bits = bitstream_get_bits(&bs, &tmp, 64 - have);
4128 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004129 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004130 look_ahead |= tmp << have;
4131 have += bits;
4132 }
4133
4134 c->bit_offset = s;
4135 bm_xfer_ctx_bit_to_word_offset(c);
4136
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004137 return (s != c->bm_bits);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004138}
4139
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004140/**
4141 * decode_bitmap_c
4142 *
4143 * Return 0 when done, 1 when another iteration is needed, and a negative error
4144 * code upon failure.
4145 */
4146static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07004147decode_bitmap_c(struct drbd_conf *mdev,
4148 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01004149 struct bm_xfer_ctx *c,
4150 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004151{
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004152 if (dcbp_get_code(p) == RLE_VLI_Bits)
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004153 return recv_bm_rle_bits(mdev, p, c, len - sizeof(*p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004154
4155 /* other variants had been implemented for evaluation,
4156 * but have been dropped as this one turned out to be "best"
4157 * during all our tests. */
4158
4159 dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
Philipp Reisner38fa9982011-03-15 18:24:49 +01004160 conn_request_state(mdev->tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004161 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004162}
4163
4164void INFO_bm_xfer_stats(struct drbd_conf *mdev,
4165 const char *direction, struct bm_xfer_ctx *c)
4166{
4167 /* what would it take to transfer it "plaintext" */
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004168 unsigned int header_size = drbd_header_size(mdev->tconn);
4169 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE - header_size;
4170 unsigned int plain =
4171 header_size * (DIV_ROUND_UP(c->bm_words, data_size) + 1) +
4172 c->bm_words * sizeof(unsigned long);
4173 unsigned int total = c->bytes[0] + c->bytes[1];
4174 unsigned int r;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004175
4176 /* total can not be zero. but just in case: */
4177 if (total == 0)
4178 return;
4179
4180 /* don't report if not compressed */
4181 if (total >= plain)
4182 return;
4183
4184 /* total < plain. check for overflow, still */
4185 r = (total > UINT_MAX/1000) ? (total / (plain/1000))
4186 : (1000 * total / plain);
4187
4188 if (r > 1000)
4189 r = 1000;
4190
4191 r = 1000 - r;
4192 dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
4193 "total %u; compression: %u.%u%%\n",
4194 direction,
4195 c->bytes[1], c->packets[1],
4196 c->bytes[0], c->packets[0],
4197 total, r/10, r % 10);
4198}
4199
4200/* Since we are processing the bitfield from lower addresses to higher,
4201 it does not matter if the process it in 32 bit chunks or 64 bit
4202 chunks as long as it is little endian. (Understand it as byte stream,
4203 beginning with the lowest byte...) If we would use big endian
4204 we would need to process it from the highest address to the lowest,
4205 in order to be agnostic to the 32 vs 64 bits issue.
4206
4207 returns 0 on failure, 1 if we successfully received it. */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004208static int receive_bitmap(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004209{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004210 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004211 struct bm_xfer_ctx c;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004212 int err;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004213
4214 mdev = vnr_to_mdev(tconn, pi->vnr);
4215 if (!mdev)
4216 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004217
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004218 drbd_bm_lock(mdev, "receive bitmap", BM_LOCKED_SET_ALLOWED);
4219 /* you are supposed to send additional out-of-sync information
4220 * if you actually set bits during this phase */
Philipp Reisnerb411b362009-09-25 16:07:19 -07004221
Philipp Reisnerb411b362009-09-25 16:07:19 -07004222 c = (struct bm_xfer_ctx) {
4223 .bm_bits = drbd_bm_bits(mdev),
4224 .bm_words = drbd_bm_words(mdev),
4225 };
4226
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004227 for(;;) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004228 if (pi->cmd == P_BITMAP)
4229 err = receive_bitmap_plain(mdev, pi->size, pi->data, &c);
4230 else if (pi->cmd == P_COMPRESSED_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004231 /* MAYBE: sanity check that we speak proto >= 90,
4232 * and the feature is enabled! */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004233 struct p_compressed_bm *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004234
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004235 if (pi->size > DRBD_SOCKET_BUFFER_SIZE - drbd_header_size(tconn)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004236 dev_err(DEV, "ReportCBitmap packet too large\n");
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004237 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004238 goto out;
4239 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004240 if (pi->size <= sizeof(*p)) {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004241 dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004242 err = -EIO;
Andreas Gruenbacher78fcbda2010-12-10 22:18:27 +01004243 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004244 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004245 err = drbd_recv_all(mdev->tconn, p, pi->size);
4246 if (err)
4247 goto out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004248 err = decode_bitmap_c(mdev, p, &c, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004249 } else {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004250 dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", pi->cmd);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004251 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004252 goto out;
4253 }
4254
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004255 c.packets[pi->cmd == P_BITMAP]++;
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004256 c.bytes[pi->cmd == P_BITMAP] += drbd_header_size(tconn) + pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004257
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004258 if (err <= 0) {
4259 if (err < 0)
4260 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004261 break;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004262 }
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004263 err = drbd_recv_header(mdev->tconn, pi);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004264 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004265 goto out;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004266 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004267
4268 INFO_bm_xfer_stats(mdev, "receive", &c);
4269
4270 if (mdev->state.conn == C_WF_BITMAP_T) {
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01004271 enum drbd_state_rv rv;
4272
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004273 err = drbd_send_bitmap(mdev);
4274 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004275 goto out;
4276 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01004277 rv = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
4278 D_ASSERT(rv == SS_SUCCESS);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004279 } else if (mdev->state.conn != C_WF_BITMAP_S) {
4280 /* admin may have requested C_DISCONNECTING,
4281 * other threads may have noticed network errors */
4282 dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n",
4283 drbd_conn_str(mdev->state.conn));
4284 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004285 err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004286
Philipp Reisnerb411b362009-09-25 16:07:19 -07004287 out:
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004288 drbd_bm_unlock(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004289 if (!err && mdev->state.conn == C_WF_BITMAP_S)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004290 drbd_start_resync(mdev, C_SYNC_SOURCE);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004291 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004292}
4293
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004294static int receive_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004295{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004296 conn_warn(tconn, "skipping unknown optional packet type %d, l: %d!\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004297 pi->cmd, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004298
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004299 return ignore_remaining_packet(tconn, pi);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004300}
4301
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004302static int receive_UnplugRemote(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004303{
Philipp Reisnerb411b362009-09-25 16:07:19 -07004304 /* Make sure we've acked all the TCP data associated
4305 * with the data requests being unplugged */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004306 drbd_tcp_quickack(tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004307
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004308 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004309}
4310
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004311static int receive_out_of_sync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner73a01a12010-10-27 14:33:00 +02004312{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004313 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004314 struct p_block_desc *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004315
4316 mdev = vnr_to_mdev(tconn, pi->vnr);
4317 if (!mdev)
4318 return -EIO;
Philipp Reisner73a01a12010-10-27 14:33:00 +02004319
Lars Ellenbergf735e3632010-12-17 21:06:18 +01004320 switch (mdev->state.conn) {
4321 case C_WF_SYNC_UUID:
4322 case C_WF_BITMAP_T:
4323 case C_BEHIND:
4324 break;
4325 default:
4326 dev_err(DEV, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n",
4327 drbd_conn_str(mdev->state.conn));
4328 }
4329
Philipp Reisner73a01a12010-10-27 14:33:00 +02004330 drbd_set_out_of_sync(mdev, be64_to_cpu(p->sector), be32_to_cpu(p->blksize));
4331
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004332 return 0;
Philipp Reisner73a01a12010-10-27 14:33:00 +02004333}
4334
Philipp Reisner02918be2010-08-20 14:35:10 +02004335struct data_cmd {
4336 int expect_payload;
4337 size_t pkt_size;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004338 int (*fn)(struct drbd_tconn *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004339};
4340
Philipp Reisner02918be2010-08-20 14:35:10 +02004341static struct data_cmd drbd_cmd_handler[] = {
4342 [P_DATA] = { 1, sizeof(struct p_data), receive_Data },
4343 [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply },
4344 [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } ,
4345 [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } ,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004346 [P_BITMAP] = { 1, 0, receive_bitmap } ,
4347 [P_COMPRESSED_BITMAP] = { 1, 0, receive_bitmap } ,
4348 [P_UNPLUG_REMOTE] = { 0, 0, receive_UnplugRemote },
Philipp Reisner02918be2010-08-20 14:35:10 +02004349 [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4350 [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004351 [P_SYNC_PARAM] = { 1, 0, receive_SyncParam },
4352 [P_SYNC_PARAM89] = { 1, 0, receive_SyncParam },
Philipp Reisner02918be2010-08-20 14:35:10 +02004353 [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol },
4354 [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids },
4355 [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes },
4356 [P_STATE] = { 0, sizeof(struct p_state), receive_state },
4357 [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state },
4358 [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid },
4359 [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4360 [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4361 [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4362 [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip },
Philipp Reisner73a01a12010-10-27 14:33:00 +02004363 [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004364 [P_CONN_ST_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_conn_state },
Philipp Reisner036b17e2011-05-16 17:38:11 +02004365 [P_PROTOCOL_UPDATE] = { 1, sizeof(struct p_protocol), receive_protocol },
Philipp Reisner02918be2010-08-20 14:35:10 +02004366};
4367
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004368static void drbdd(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004369{
Philipp Reisner77351055b2011-02-07 17:24:26 +01004370 struct packet_info pi;
Philipp Reisner02918be2010-08-20 14:35:10 +02004371 size_t shs; /* sub header size */
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004372 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004373
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004374 while (get_t_state(&tconn->receiver) == RUNNING) {
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004375 struct data_cmd *cmd;
4376
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004377 drbd_thread_current_set_cpu(&tconn->receiver);
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004378 if (drbd_recv_header(tconn, &pi))
Philipp Reisner02918be2010-08-20 14:35:10 +02004379 goto err_out;
4380
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004381 cmd = &drbd_cmd_handler[pi.cmd];
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004382 if (unlikely(pi.cmd >= ARRAY_SIZE(drbd_cmd_handler) || !cmd->fn)) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004383 conn_err(tconn, "Unexpected data packet %s (0x%04x)",
4384 cmdname(pi.cmd), pi.cmd);
Philipp Reisner02918be2010-08-20 14:35:10 +02004385 goto err_out;
Lars Ellenberg0b33a912009-11-16 15:58:04 +01004386 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004387
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004388 shs = cmd->pkt_size;
4389 if (pi.size > shs && !cmd->expect_payload) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004390 conn_err(tconn, "No payload expected %s l:%d\n",
4391 cmdname(pi.cmd), pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004392 goto err_out;
4393 }
4394
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004395 if (shs) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004396 err = drbd_recv_all_warn(tconn, pi.data, shs);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004397 if (err)
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004398 goto err_out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004399 pi.size -= shs;
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004400 }
4401
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004402 err = cmd->fn(tconn, &pi);
4403 if (err) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004404 conn_err(tconn, "error receiving %s, e: %d l: %d!\n",
4405 cmdname(pi.cmd), err, pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004406 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004407 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004408 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004409 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004410
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004411 err_out:
4412 conn_request_state(tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004413}
4414
Philipp Reisner0e29d162011-02-18 14:23:11 +01004415void conn_flush_workqueue(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004416{
4417 struct drbd_wq_barrier barr;
4418
4419 barr.w.cb = w_prev_work_done;
Philipp Reisner0e29d162011-02-18 14:23:11 +01004420 barr.w.tconn = tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004421 init_completion(&barr.done);
Lars Ellenbergd5b27b02011-11-14 15:42:37 +01004422 drbd_queue_work(&tconn->sender_work, &barr.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004423 wait_for_completion(&barr.done);
4424}
4425
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004426static void conn_disconnect(struct drbd_tconn *tconn)
Philipp Reisnerf70b35112010-06-24 14:34:40 +02004427{
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004428 struct drbd_conf *mdev;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004429 enum drbd_conns oc;
Philipp Reisner376694a2011-11-07 10:54:28 +01004430 int vnr;
Philipp Reisnerf70b35112010-06-24 14:34:40 +02004431
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004432 if (tconn->cstate == C_STANDALONE)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004433 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004434
Lars Ellenberg545752d2011-12-05 14:39:25 +01004435 /* We are about to start the cleanup after connection loss.
4436 * Make sure drbd_make_request knows about that.
4437 * Usually we should be in some network failure state already,
4438 * but just in case we are not, we fix it up here.
4439 */
Philipp Reisnerb8853db2011-12-13 11:09:16 +01004440 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
Lars Ellenberg545752d2011-12-05 14:39:25 +01004441
Philipp Reisnerb411b362009-09-25 16:07:19 -07004442 /* asender does not clean up anything. it must not interfere, either */
Philipp Reisner360cc742011-02-08 14:29:53 +01004443 drbd_thread_stop(&tconn->asender);
4444 drbd_free_sock(tconn);
4445
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004446 rcu_read_lock();
4447 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
4448 kref_get(&mdev->kref);
4449 rcu_read_unlock();
4450 drbd_disconnected(mdev);
4451 kref_put(&mdev->kref, &drbd_minor_destroy);
4452 rcu_read_lock();
4453 }
4454 rcu_read_unlock();
4455
Philipp Reisner12038a32011-11-09 19:18:00 +01004456 if (!list_empty(&tconn->current_epoch->list))
4457 conn_err(tconn, "ASSERTION FAILED: tconn->current_epoch->list not empty\n");
4458 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
4459 atomic_set(&tconn->current_epoch->epoch_size, 0);
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01004460 tconn->send.seen_any_write_yet = false;
Philipp Reisner12038a32011-11-09 19:18:00 +01004461
Philipp Reisner360cc742011-02-08 14:29:53 +01004462 conn_info(tconn, "Connection closed\n");
4463
Philipp Reisnercb703452011-03-24 11:03:07 +01004464 if (conn_highest_role(tconn) == R_PRIMARY && conn_highest_pdsk(tconn) >= D_UNKNOWN)
4465 conn_try_outdate_peer_async(tconn);
4466
Philipp Reisner360cc742011-02-08 14:29:53 +01004467 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004468 oc = tconn->cstate;
4469 if (oc >= C_UNCONNECTED)
Philipp Reisner376694a2011-11-07 10:54:28 +01004470 _conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004471
Philipp Reisner360cc742011-02-08 14:29:53 +01004472 spin_unlock_irq(&tconn->req_lock);
4473
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02004474 if (oc == C_DISCONNECTING)
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +02004475 conn_request_state(tconn, NS(conn, C_STANDALONE), CS_VERBOSE | CS_HARD);
Philipp Reisner360cc742011-02-08 14:29:53 +01004476}
4477
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004478static int drbd_disconnected(struct drbd_conf *mdev)
Philipp Reisner360cc742011-02-08 14:29:53 +01004479{
Philipp Reisner360cc742011-02-08 14:29:53 +01004480 unsigned int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004481
Philipp Reisner85719572010-07-21 10:20:17 +02004482 /* wait for current activity to cease. */
Philipp Reisner87eeee42011-01-19 14:16:30 +01004483 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004484 _drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
4485 _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee);
4486 _drbd_wait_ee_list_empty(mdev, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004487 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004488
4489 /* We do not have data structures that would allow us to
4490 * get the rs_pending_cnt down to 0 again.
4491 * * On C_SYNC_TARGET we do not have any data structures describing
4492 * the pending RSDataRequest's we have sent.
4493 * * On C_SYNC_SOURCE there is no data structure that tracks
4494 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
4495 * And no, it is not the sum of the reference counts in the
4496 * resync_LRU. The resync_LRU tracks the whole operation including
4497 * the disk-IO, while the rs_pending_cnt only tracks the blocks
4498 * on the fly. */
4499 drbd_rs_cancel_all(mdev);
4500 mdev->rs_total = 0;
4501 mdev->rs_failed = 0;
4502 atomic_set(&mdev->rs_pending_cnt, 0);
4503 wake_up(&mdev->misc_wait);
4504
Philipp Reisnerb411b362009-09-25 16:07:19 -07004505 del_timer_sync(&mdev->resync_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004506 resync_timer_fn((unsigned long)mdev);
4507
Philipp Reisnerb411b362009-09-25 16:07:19 -07004508 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
4509 * w_make_resync_request etc. which may still be on the worker queue
4510 * to be "canceled" */
4511 drbd_flush_workqueue(mdev);
4512
Andreas Gruenbachera990be42011-04-06 17:56:48 +02004513 drbd_finish_peer_reqs(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004514
Philipp Reisnerd10b4ea2011-11-30 23:25:36 +01004515 /* This second workqueue flush is necessary, since drbd_finish_peer_reqs()
4516 might have issued a work again. The one before drbd_finish_peer_reqs() is
4517 necessary to reclain net_ee in drbd_finish_peer_reqs(). */
4518 drbd_flush_workqueue(mdev);
4519
Lars Ellenberg08332d72012-08-17 15:09:13 +02004520 /* need to do it again, drbd_finish_peer_reqs() may have populated it
4521 * again via drbd_try_clear_on_disk_bm(). */
4522 drbd_rs_cancel_all(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004523
4524 kfree(mdev->p_uuid);
4525 mdev->p_uuid = NULL;
4526
Philipp Reisner2aebfab2011-03-28 16:48:11 +02004527 if (!drbd_suspended(mdev))
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01004528 tl_clear(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004529
4530 drbd_md_sync(mdev);
4531
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004532 /* serialize with bitmap writeout triggered by the state change,
4533 * if any. */
4534 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
4535
Philipp Reisnerb411b362009-09-25 16:07:19 -07004536 /* tcp_close and release of sendpage pages can be deferred. I don't
4537 * want to use SO_LINGER, because apparently it can be deferred for
4538 * more than 20 seconds (longest time I checked).
4539 *
4540 * Actually we don't care for exactly when the network stack does its
4541 * put_page(), but release our reference on these pages right here.
4542 */
Andreas Gruenbacher7721f562011-04-06 17:14:02 +02004543 i = drbd_free_peer_reqs(mdev, &mdev->net_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004544 if (i)
4545 dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
Lars Ellenberg435f0742010-09-06 12:30:25 +02004546 i = atomic_read(&mdev->pp_in_use_by_net);
4547 if (i)
4548 dev_info(DEV, "pp_in_use_by_net = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004549 i = atomic_read(&mdev->pp_in_use);
4550 if (i)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02004551 dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004552
4553 D_ASSERT(list_empty(&mdev->read_ee));
4554 D_ASSERT(list_empty(&mdev->active_ee));
4555 D_ASSERT(list_empty(&mdev->sync_ee));
4556 D_ASSERT(list_empty(&mdev->done_ee));
4557
Philipp Reisner360cc742011-02-08 14:29:53 +01004558 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004559}
4560
4561/*
4562 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
4563 * we can agree on is stored in agreed_pro_version.
4564 *
4565 * feature flags and the reserved array should be enough room for future
4566 * enhancements of the handshake protocol, and possible plugins...
4567 *
4568 * for now, they are expected to be zero, but ignored.
4569 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004570static int drbd_send_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004571{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004572 struct drbd_socket *sock;
4573 struct p_connection_features *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004574
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004575 sock = &tconn->data;
4576 p = conn_prepare_command(tconn, sock);
4577 if (!p)
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004578 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004579 memset(p, 0, sizeof(*p));
4580 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
4581 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004582 return conn_send_command(tconn, sock, P_CONNECTION_FEATURES, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004583}
4584
4585/*
4586 * return values:
4587 * 1 yes, we have a valid connection
4588 * 0 oops, did not work out, please try again
4589 * -1 peer talks different language,
4590 * no point in trying again, please go standalone.
4591 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004592static int drbd_do_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004593{
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004594 /* ASSERT current == tconn->receiver ... */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004595 struct p_connection_features *p;
4596 const int expect = sizeof(struct p_connection_features);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004597 struct packet_info pi;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004598 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004599
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004600 err = drbd_send_features(tconn);
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004601 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004602 return 0;
4603
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004604 err = drbd_recv_header(tconn, &pi);
4605 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004606 return 0;
4607
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004608 if (pi.cmd != P_CONNECTION_FEATURES) {
4609 conn_err(tconn, "expected ConnectionFeatures packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004610 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004611 return -1;
4612 }
4613
Philipp Reisner77351055b2011-02-07 17:24:26 +01004614 if (pi.size != expect) {
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004615 conn_err(tconn, "expected ConnectionFeatures length: %u, received: %u\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004616 expect, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004617 return -1;
4618 }
4619
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004620 p = pi.data;
4621 err = drbd_recv_all_warn(tconn, p, expect);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004622 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004623 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004624
Philipp Reisnerb411b362009-09-25 16:07:19 -07004625 p->protocol_min = be32_to_cpu(p->protocol_min);
4626 p->protocol_max = be32_to_cpu(p->protocol_max);
4627 if (p->protocol_max == 0)
4628 p->protocol_max = p->protocol_min;
4629
4630 if (PRO_VERSION_MAX < p->protocol_min ||
4631 PRO_VERSION_MIN > p->protocol_max)
4632 goto incompat;
4633
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004634 tconn->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004635
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004636 conn_info(tconn, "Handshake successful: "
4637 "Agreed network protocol version %d\n", tconn->agreed_pro_version);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004638
4639 return 1;
4640
4641 incompat:
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004642 conn_err(tconn, "incompatible DRBD dialects: "
Philipp Reisnerb411b362009-09-25 16:07:19 -07004643 "I support %d-%d, peer supports %d-%d\n",
4644 PRO_VERSION_MIN, PRO_VERSION_MAX,
4645 p->protocol_min, p->protocol_max);
4646 return -1;
4647}
4648
4649#if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
Philipp Reisner13e60372011-02-08 09:54:40 +01004650static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004651{
4652 dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
4653 dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004654 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004655}
4656#else
4657#define CHALLENGE_LEN 64
Johannes Thomab10d96c2010-01-07 16:02:50 +01004658
4659/* Return value:
4660 1 - auth succeeded,
4661 0 - failed, try again (network error),
4662 -1 - auth failed, don't try again.
4663*/
4664
Philipp Reisner13e60372011-02-08 09:54:40 +01004665static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004666{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004667 struct drbd_socket *sock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004668 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
4669 struct scatterlist sg;
4670 char *response = NULL;
4671 char *right_response = NULL;
4672 char *peers_ch = NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +02004673 unsigned int key_len;
4674 char secret[SHARED_SECRET_MAX]; /* 64 byte */
Philipp Reisnerb411b362009-09-25 16:07:19 -07004675 unsigned int resp_size;
4676 struct hash_desc desc;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004677 struct packet_info pi;
Philipp Reisner44ed1672011-04-19 17:10:19 +02004678 struct net_conf *nc;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004679 int err, rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004680
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004681 /* FIXME: Put the challenge/response into the preallocated socket buffer. */
4682
Philipp Reisner44ed1672011-04-19 17:10:19 +02004683 rcu_read_lock();
4684 nc = rcu_dereference(tconn->net_conf);
4685 key_len = strlen(nc->shared_secret);
4686 memcpy(secret, nc->shared_secret, key_len);
4687 rcu_read_unlock();
4688
Philipp Reisner13e60372011-02-08 09:54:40 +01004689 desc.tfm = tconn->cram_hmac_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004690 desc.flags = 0;
4691
Philipp Reisner44ed1672011-04-19 17:10:19 +02004692 rv = crypto_hash_setkey(tconn->cram_hmac_tfm, (u8 *)secret, key_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004693 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004694 conn_err(tconn, "crypto_hash_setkey() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004695 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004696 goto fail;
4697 }
4698
4699 get_random_bytes(my_challenge, CHALLENGE_LEN);
4700
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004701 sock = &tconn->data;
4702 if (!conn_prepare_command(tconn, sock)) {
4703 rv = 0;
4704 goto fail;
4705 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004706 rv = !conn_send_command(tconn, sock, P_AUTH_CHALLENGE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004707 my_challenge, CHALLENGE_LEN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004708 if (!rv)
4709 goto fail;
4710
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004711 err = drbd_recv_header(tconn, &pi);
4712 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004713 rv = 0;
4714 goto fail;
4715 }
4716
Philipp Reisner77351055b2011-02-07 17:24:26 +01004717 if (pi.cmd != P_AUTH_CHALLENGE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004718 conn_err(tconn, "expected AuthChallenge packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004719 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004720 rv = 0;
4721 goto fail;
4722 }
4723
Philipp Reisner77351055b2011-02-07 17:24:26 +01004724 if (pi.size > CHALLENGE_LEN * 2) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004725 conn_err(tconn, "expected AuthChallenge payload too big.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004726 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004727 goto fail;
4728 }
4729
Philipp Reisner77351055b2011-02-07 17:24:26 +01004730 peers_ch = kmalloc(pi.size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004731 if (peers_ch == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004732 conn_err(tconn, "kmalloc of peers_ch failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004733 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004734 goto fail;
4735 }
4736
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004737 err = drbd_recv_all_warn(tconn, peers_ch, pi.size);
4738 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004739 rv = 0;
4740 goto fail;
4741 }
4742
Philipp Reisner13e60372011-02-08 09:54:40 +01004743 resp_size = crypto_hash_digestsize(tconn->cram_hmac_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004744 response = kmalloc(resp_size, GFP_NOIO);
4745 if (response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004746 conn_err(tconn, "kmalloc of response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004747 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004748 goto fail;
4749 }
4750
4751 sg_init_table(&sg, 1);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004752 sg_set_buf(&sg, peers_ch, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004753
4754 rv = crypto_hash_digest(&desc, &sg, sg.length, response);
4755 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004756 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004757 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004758 goto fail;
4759 }
4760
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004761 if (!conn_prepare_command(tconn, sock)) {
4762 rv = 0;
4763 goto fail;
4764 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004765 rv = !conn_send_command(tconn, sock, P_AUTH_RESPONSE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004766 response, resp_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004767 if (!rv)
4768 goto fail;
4769
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004770 err = drbd_recv_header(tconn, &pi);
4771 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004772 rv = 0;
4773 goto fail;
4774 }
4775
Philipp Reisner77351055b2011-02-07 17:24:26 +01004776 if (pi.cmd != P_AUTH_RESPONSE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004777 conn_err(tconn, "expected AuthResponse packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004778 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004779 rv = 0;
4780 goto fail;
4781 }
4782
Philipp Reisner77351055b2011-02-07 17:24:26 +01004783 if (pi.size != resp_size) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004784 conn_err(tconn, "expected AuthResponse payload of wrong size\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004785 rv = 0;
4786 goto fail;
4787 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004788
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004789 err = drbd_recv_all_warn(tconn, response , resp_size);
4790 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004791 rv = 0;
4792 goto fail;
4793 }
4794
4795 right_response = kmalloc(resp_size, GFP_NOIO);
Julia Lawall2d1ee872009-12-27 22:27:11 +01004796 if (right_response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004797 conn_err(tconn, "kmalloc of right_response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004798 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004799 goto fail;
4800 }
4801
4802 sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
4803
4804 rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
4805 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004806 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004807 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004808 goto fail;
4809 }
4810
4811 rv = !memcmp(response, right_response, resp_size);
4812
4813 if (rv)
Philipp Reisner44ed1672011-04-19 17:10:19 +02004814 conn_info(tconn, "Peer authenticated using %d bytes HMAC\n",
4815 resp_size);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004816 else
4817 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004818
4819 fail:
4820 kfree(peers_ch);
4821 kfree(response);
4822 kfree(right_response);
4823
4824 return rv;
4825}
4826#endif
4827
4828int drbdd_init(struct drbd_thread *thi)
4829{
Philipp Reisner392c8802011-02-09 10:33:31 +01004830 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004831 int h;
4832
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004833 conn_info(tconn, "receiver (re)started\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004834
4835 do {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004836 h = conn_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004837 if (h == 0) {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004838 conn_disconnect(tconn);
Philipp Reisner20ee6392011-01-18 15:28:59 +01004839 schedule_timeout_interruptible(HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004840 }
4841 if (h == -1) {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004842 conn_warn(tconn, "Discarding network configuration.\n");
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004843 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004844 }
4845 } while (h == 0);
4846
Philipp Reisner91fd4da2011-04-20 17:47:29 +02004847 if (h > 0)
4848 drbdd(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004849
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004850 conn_disconnect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004851
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004852 conn_info(tconn, "receiver terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004853 return 0;
4854}
4855
4856/* ********* acknowledge sender ******** */
4857
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004858static int got_conn_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004859{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004860 struct p_req_state_reply *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004861 int retcode = be32_to_cpu(p->retcode);
4862
4863 if (retcode >= SS_SUCCESS) {
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004864 set_bit(CONN_WD_ST_CHG_OKAY, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004865 } else {
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004866 set_bit(CONN_WD_ST_CHG_FAIL, &tconn->flags);
4867 conn_err(tconn, "Requested state change failed by peer: %s (%d)\n",
4868 drbd_set_st_err_str(retcode), retcode);
4869 }
4870 wake_up(&tconn->ping_wait);
4871
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004872 return 0;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004873}
4874
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004875static int got_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004876{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004877 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004878 struct p_req_state_reply *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004879 int retcode = be32_to_cpu(p->retcode);
4880
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004881 mdev = vnr_to_mdev(tconn, pi->vnr);
4882 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004883 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004884
Philipp Reisner4d0fc3f2012-01-20 13:52:27 +01004885 if (test_bit(CONN_WD_ST_CHG_REQ, &tconn->flags)) {
4886 D_ASSERT(tconn->agreed_pro_version < 100);
4887 return got_conn_RqSReply(tconn, pi);
4888 }
4889
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004890 if (retcode >= SS_SUCCESS) {
4891 set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
4892 } else {
4893 set_bit(CL_ST_CHG_FAIL, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004894 dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004895 drbd_set_st_err_str(retcode), retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004896 }
4897 wake_up(&mdev->state_wait);
4898
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004899 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004900}
4901
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004902static int got_Ping(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004903{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004904 return drbd_send_ping_ack(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004905
4906}
4907
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004908static int got_PingAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004909{
4910 /* restore idle timeout */
Philipp Reisner2a67d8b2011-02-09 14:10:32 +01004911 tconn->meta.socket->sk->sk_rcvtimeo = tconn->net_conf->ping_int*HZ;
4912 if (!test_and_set_bit(GOT_PING_ACK, &tconn->flags))
4913 wake_up(&tconn->ping_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004914
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004915 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004916}
4917
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004918static int got_IsInSync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004919{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004920 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004921 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004922 sector_t sector = be64_to_cpu(p->sector);
4923 int blksize = be32_to_cpu(p->blksize);
4924
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004925 mdev = vnr_to_mdev(tconn, pi->vnr);
4926 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004927 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004928
Philipp Reisner31890f42011-01-19 14:12:51 +01004929 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004930
4931 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4932
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004933 if (get_ldev(mdev)) {
4934 drbd_rs_complete_io(mdev, sector);
4935 drbd_set_in_sync(mdev, sector, blksize);
4936 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
4937 mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
4938 put_ldev(mdev);
4939 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004940 dec_rs_pending(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02004941 atomic_add(blksize >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004942
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004943 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004944}
4945
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004946static int
4947validate_req_change_req_state(struct drbd_conf *mdev, u64 id, sector_t sector,
4948 struct rb_root *root, const char *func,
4949 enum drbd_req_event what, bool missing_ok)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004950{
4951 struct drbd_request *req;
4952 struct bio_and_error m;
4953
Philipp Reisner87eeee42011-01-19 14:16:30 +01004954 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004955 req = find_request(mdev, root, id, sector, missing_ok, func);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004956 if (unlikely(!req)) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01004957 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004958 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004959 }
4960 __req_mod(req, what, &m);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004961 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004962
4963 if (m.bio)
4964 complete_master_bio(mdev, &m);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004965 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004966}
4967
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004968static int got_BlockAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004969{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004970 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004971 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004972 sector_t sector = be64_to_cpu(p->sector);
4973 int blksize = be32_to_cpu(p->blksize);
4974 enum drbd_req_event what;
4975
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004976 mdev = vnr_to_mdev(tconn, pi->vnr);
4977 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004978 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004979
Philipp Reisnerb411b362009-09-25 16:07:19 -07004980 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4981
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004982 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004983 drbd_set_in_sync(mdev, sector, blksize);
4984 dec_rs_pending(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004985 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004986 }
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004987 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004988 case P_RS_WRITE_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004989 what = WRITE_ACKED_BY_PEER_AND_SIS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004990 break;
4991 case P_WRITE_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004992 what = WRITE_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004993 break;
4994 case P_RECV_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004995 what = RECV_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004996 break;
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02004997 case P_SUPERSEDED:
4998 what = CONFLICT_RESOLVED;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004999 break;
5000 case P_RETRY_WRITE:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01005001 what = POSTPONE_WRITE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005002 break;
5003 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005004 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07005005 }
5006
5007 return validate_req_change_req_state(mdev, p->block_id, sector,
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005008 &mdev->write_requests, __func__,
5009 what, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005010}
5011
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005012static int got_NegAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005013{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005014 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005015 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005016 sector_t sector = be64_to_cpu(p->sector);
Philipp Reisner2deb8332011-01-17 18:39:18 +01005017 int size = be32_to_cpu(p->blksize);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02005018 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005019
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005020 mdev = vnr_to_mdev(tconn, pi->vnr);
5021 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005022 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005023
5024 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5025
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01005026 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07005027 dec_rs_pending(mdev);
5028 drbd_rs_failed_io(mdev, sector, size);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005029 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005030 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01005031
Andreas Gruenbacher85997672011-04-04 13:09:15 +02005032 err = validate_req_change_req_state(mdev, p->block_id, sector,
5033 &mdev->write_requests, __func__,
Philipp Reisner303d1442011-04-13 16:24:47 -07005034 NEG_ACKED, true);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02005035 if (err) {
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01005036 /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs.
5037 The master bio might already be completed, therefore the
5038 request is no longer in the collision hash. */
5039 /* In Protocol B we might already have got a P_RECV_ACK
5040 but then get a P_NEG_ACK afterwards. */
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01005041 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisner2deb8332011-01-17 18:39:18 +01005042 }
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005043 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005044}
5045
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005046static int got_NegDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005047{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005048 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005049 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005050 sector_t sector = be64_to_cpu(p->sector);
5051
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005052 mdev = vnr_to_mdev(tconn, pi->vnr);
5053 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005054 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005055
Philipp Reisnerb411b362009-09-25 16:07:19 -07005056 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01005057
Philipp Reisner380207d2011-11-11 12:31:20 +01005058 dev_err(DEV, "Got NegDReply; Sector %llus, len %u.\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -07005059 (unsigned long long)sector, be32_to_cpu(p->blksize));
5060
5061 return validate_req_change_req_state(mdev, p->block_id, sector,
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005062 &mdev->read_requests, __func__,
5063 NEG_ACKED, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005064}
5065
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005066static int got_NegRSDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005067{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005068 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005069 sector_t sector;
5070 int size;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005071 struct p_block_ack *p = pi->data;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005072
5073 mdev = vnr_to_mdev(tconn, pi->vnr);
5074 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005075 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005076
5077 sector = be64_to_cpu(p->sector);
5078 size = be32_to_cpu(p->blksize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005079
5080 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5081
5082 dec_rs_pending(mdev);
5083
5084 if (get_ldev_if_state(mdev, D_FAILED)) {
5085 drbd_rs_complete_io(mdev, sector);
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01005086 switch (pi->cmd) {
Philipp Reisnerd612d302010-12-27 10:53:28 +01005087 case P_NEG_RS_DREPLY:
5088 drbd_rs_failed_io(mdev, sector, size);
5089 case P_RS_CANCEL:
5090 break;
5091 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005092 BUG();
Philipp Reisnerd612d302010-12-27 10:53:28 +01005093 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005094 put_ldev(mdev);
5095 }
5096
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005097 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005098}
5099
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005100static int got_BarrierAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005101{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005102 struct p_barrier_ack *p = pi->data;
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02005103 struct drbd_conf *mdev;
5104 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005105
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02005106 tl_release(tconn, p->barrier, be32_to_cpu(p->set_size));
Philipp Reisnerb411b362009-09-25 16:07:19 -07005107
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02005108 rcu_read_lock();
5109 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
5110 if (mdev->state.conn == C_AHEAD &&
5111 atomic_read(&mdev->ap_in_flight) == 0 &&
5112 !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->flags)) {
5113 mdev->start_resync_timer.expires = jiffies + HZ;
5114 add_timer(&mdev->start_resync_timer);
5115 }
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02005116 }
Lars Ellenberg9ed57dc2012-03-26 20:55:17 +02005117 rcu_read_unlock();
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02005118
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005119 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005120}
5121
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005122static int got_OVResult(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005123{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005124 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005125 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005126 struct drbd_work *w;
5127 sector_t sector;
5128 int size;
5129
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005130 mdev = vnr_to_mdev(tconn, pi->vnr);
5131 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005132 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005133
Philipp Reisnerb411b362009-09-25 16:07:19 -07005134 sector = be64_to_cpu(p->sector);
5135 size = be32_to_cpu(p->blksize);
5136
5137 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5138
5139 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005140 drbd_ov_out_of_sync_found(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005141 else
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005142 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005143
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005144 if (!get_ldev(mdev))
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005145 return 0;
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005146
Philipp Reisnerb411b362009-09-25 16:07:19 -07005147 drbd_rs_complete_io(mdev, sector);
5148 dec_rs_pending(mdev);
5149
Lars Ellenbergea5442a2010-11-05 09:48:01 +01005150 --mdev->ov_left;
5151
5152 /* let's advance progress step marks only for every other megabyte */
5153 if ((mdev->ov_left & 0x200) == 0x200)
5154 drbd_advance_rs_marks(mdev, mdev->ov_left);
5155
5156 if (mdev->ov_left == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07005157 w = kmalloc(sizeof(*w), GFP_NOIO);
5158 if (w) {
5159 w->cb = w_ov_finished;
Philipp Reisnera21e9292011-02-08 15:08:49 +01005160 w->mdev = mdev;
Lars Ellenbergd5b27b02011-11-14 15:42:37 +01005161 drbd_queue_work(&mdev->tconn->sender_work, w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005162 } else {
5163 dev_err(DEV, "kmalloc(w) failed.");
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005164 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005165 drbd_resync_finished(mdev);
5166 }
5167 }
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005168 put_ldev(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005169 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005170}
5171
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005172static int got_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner0ced55a2010-04-30 15:26:20 +02005173{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005174 return 0;
Philipp Reisner0ced55a2010-04-30 15:26:20 +02005175}
5176
Andreas Gruenbachera990be42011-04-06 17:56:48 +02005177static int tconn_finish_peer_reqs(struct drbd_tconn *tconn)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005178{
Philipp Reisner082a3432011-03-15 16:05:42 +01005179 struct drbd_conf *mdev;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005180 int vnr, not_empty = 0;
Philipp Reisner32862ec2011-02-08 16:41:01 +01005181
5182 do {
5183 clear_bit(SIGNAL_ASENDER, &tconn->flags);
5184 flush_signals(current);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005185
5186 rcu_read_lock();
5187 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
5188 kref_get(&mdev->kref);
5189 rcu_read_unlock();
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07005190 if (drbd_finish_peer_reqs(mdev)) {
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005191 kref_put(&mdev->kref, &drbd_minor_destroy);
5192 return 1;
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07005193 }
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005194 kref_put(&mdev->kref, &drbd_minor_destroy);
5195 rcu_read_lock();
Philipp Reisner082a3432011-03-15 16:05:42 +01005196 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005197 set_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisner082a3432011-03-15 16:05:42 +01005198
5199 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005200 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisner082a3432011-03-15 16:05:42 +01005201 not_empty = !list_empty(&mdev->done_ee);
5202 if (not_empty)
5203 break;
5204 }
5205 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005206 rcu_read_unlock();
Philipp Reisner32862ec2011-02-08 16:41:01 +01005207 } while (not_empty);
5208
5209 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005210}
5211
5212struct asender_cmd {
5213 size_t pkt_size;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005214 int (*fn)(struct drbd_tconn *tconn, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005215};
5216
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005217static struct asender_cmd asender_tbl[] = {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005218 [P_PING] = { 0, got_Ping },
5219 [P_PING_ACK] = { 0, got_PingAck },
Philipp Reisnerb411b362009-09-25 16:07:19 -07005220 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5221 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5222 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
Lars Ellenbergd4dabbe2012-08-01 12:33:51 +02005223 [P_SUPERSEDED] = { sizeof(struct p_block_ack), got_BlockAck },
Philipp Reisnerb411b362009-09-25 16:07:19 -07005224 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
5225 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005226 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply },
Philipp Reisnerb411b362009-09-25 16:07:19 -07005227 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
5228 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
5229 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
5230 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
Philipp Reisner02918be2010-08-20 14:35:10 +02005231 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005232 [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply },
5233 [P_CONN_ST_CHG_REPLY]={ sizeof(struct p_req_state_reply), got_conn_RqSReply },
5234 [P_RETRY_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005235};
Philipp Reisnerb411b362009-09-25 16:07:19 -07005236
5237int drbd_asender(struct drbd_thread *thi)
5238{
Philipp Reisner392c8802011-02-09 10:33:31 +01005239 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005240 struct asender_cmd *cmd = NULL;
Philipp Reisner77351055b2011-02-07 17:24:26 +01005241 struct packet_info pi;
Philipp Reisner257d0af2011-01-26 12:15:29 +01005242 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005243 void *buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005244 int received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005245 unsigned int header_size = drbd_header_size(tconn);
5246 int expect = header_size;
Philipp Reisner44ed1672011-04-19 17:10:19 +02005247 bool ping_timeout_active = false;
5248 struct net_conf *nc;
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005249 int ping_timeo, tcp_cork, ping_int;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005250
5251 current->policy = SCHED_RR; /* Make this a realtime task! */
5252 current->rt_priority = 2; /* more important than all other tasks */
5253
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01005254 while (get_t_state(thi) == RUNNING) {
Philipp Reisner80822282011-02-08 12:46:30 +01005255 drbd_thread_current_set_cpu(thi);
Philipp Reisner44ed1672011-04-19 17:10:19 +02005256
5257 rcu_read_lock();
5258 nc = rcu_dereference(tconn->net_conf);
5259 ping_timeo = nc->ping_timeo;
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005260 tcp_cork = nc->tcp_cork;
Philipp Reisner44ed1672011-04-19 17:10:19 +02005261 ping_int = nc->ping_int;
5262 rcu_read_unlock();
5263
Philipp Reisner32862ec2011-02-08 16:41:01 +01005264 if (test_and_clear_bit(SEND_PING, &tconn->flags)) {
Andreas Gruenbachera17647a2011-04-01 12:49:42 +02005265 if (drbd_send_ping(tconn)) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005266 conn_err(tconn, "drbd_send_ping has failed\n");
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01005267 goto reconnect;
5268 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02005269 tconn->meta.socket->sk->sk_rcvtimeo = ping_timeo * HZ / 10;
5270 ping_timeout_active = true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005271 }
5272
Philipp Reisner32862ec2011-02-08 16:41:01 +01005273 /* TODO: conditionally cork; it may hurt latency if we cork without
5274 much to send */
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005275 if (tcp_cork)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005276 drbd_tcp_cork(tconn->meta.socket);
Andreas Gruenbachera990be42011-04-06 17:56:48 +02005277 if (tconn_finish_peer_reqs(tconn)) {
5278 conn_err(tconn, "tconn_finish_peer_reqs() failed\n");
Philipp Reisner32862ec2011-02-08 16:41:01 +01005279 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005280 }
5281 /* but unconditionally uncork unless disabled */
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005282 if (tcp_cork)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005283 drbd_tcp_uncork(tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005284
5285 /* short circuit, recv_msg would return EINTR anyways. */
5286 if (signal_pending(current))
5287 continue;
5288
Philipp Reisner32862ec2011-02-08 16:41:01 +01005289 rv = drbd_recv_short(tconn->meta.socket, buf, expect-received, 0);
5290 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005291
5292 flush_signals(current);
5293
5294 /* Note:
5295 * -EINTR (on meta) we got a signal
5296 * -EAGAIN (on meta) rcvtimeo expired
5297 * -ECONNRESET other side closed the connection
5298 * -ERESTARTSYS (on data) we got a signal
5299 * rv < 0 other than above: unexpected error!
5300 * rv == expected: full header or command
5301 * rv < expected: "woken" by signal during receive
5302 * rv == 0 : "connection shut down by peer"
5303 */
5304 if (likely(rv > 0)) {
5305 received += rv;
5306 buf += rv;
5307 } else if (rv == 0) {
Philipp Reisnerb66623e2012-08-08 21:19:09 +02005308 if (test_bit(DISCONNECT_SENT, &tconn->flags)) {
5309 long t;
5310 rcu_read_lock();
5311 t = rcu_dereference(tconn->net_conf)->ping_timeo * HZ/10;
5312 rcu_read_unlock();
5313
5314 t = wait_event_timeout(tconn->ping_wait,
5315 tconn->cstate < C_WF_REPORT_PARAMS,
5316 t);
Philipp Reisner599377a2012-08-17 14:50:22 +02005317 if (t)
5318 break;
5319 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005320 conn_err(tconn, "meta connection shut down by peer.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005321 goto reconnect;
5322 } else if (rv == -EAGAIN) {
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02005323 /* If the data socket received something meanwhile,
5324 * that is good enough: peer is still alive. */
Philipp Reisner32862ec2011-02-08 16:41:01 +01005325 if (time_after(tconn->last_received,
5326 jiffies - tconn->meta.socket->sk->sk_rcvtimeo))
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02005327 continue;
Lars Ellenbergf36af182011-03-09 22:44:55 +01005328 if (ping_timeout_active) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005329 conn_err(tconn, "PingAck did not arrive in time.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005330 goto reconnect;
5331 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005332 set_bit(SEND_PING, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005333 continue;
5334 } else if (rv == -EINTR) {
5335 continue;
5336 } else {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005337 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005338 goto reconnect;
5339 }
5340
5341 if (received == expect && cmd == NULL) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005342 if (decode_header(tconn, tconn->meta.rbuf, &pi))
Philipp Reisnerb411b362009-09-25 16:07:19 -07005343 goto reconnect;
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005344 cmd = &asender_tbl[pi.cmd];
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005345 if (pi.cmd >= ARRAY_SIZE(asender_tbl) || !cmd->fn) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02005346 conn_err(tconn, "Unexpected meta packet %s (0x%04x)\n",
5347 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005348 goto disconnect;
5349 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005350 expect = header_size + cmd->pkt_size;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005351 if (pi.size != expect - header_size) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005352 conn_err(tconn, "Wrong packet size on meta (c: %d, l: %d)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01005353 pi.cmd, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005354 goto reconnect;
Philipp Reisner257d0af2011-01-26 12:15:29 +01005355 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005356 }
5357 if (received == expect) {
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005358 bool err;
Philipp Reisnera4fbda82011-03-16 11:13:17 +01005359
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005360 err = cmd->fn(tconn, &pi);
5361 if (err) {
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005362 conn_err(tconn, "%pf failed\n", cmd->fn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005363 goto reconnect;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005364 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005365
Philipp Reisnera4fbda82011-03-16 11:13:17 +01005366 tconn->last_received = jiffies;
Lars Ellenbergf36af182011-03-09 22:44:55 +01005367
Philipp Reisner44ed1672011-04-19 17:10:19 +02005368 if (cmd == &asender_tbl[P_PING_ACK]) {
5369 /* restore idle timeout */
5370 tconn->meta.socket->sk->sk_rcvtimeo = ping_int * HZ;
5371 ping_timeout_active = false;
5372 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005373
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005374 buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005375 received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005376 expect = header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005377 cmd = NULL;
5378 }
5379 }
5380
5381 if (0) {
5382reconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01005383 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
Philipp Reisner19fffd72012-08-28 16:48:03 +02005384 conn_md_sync(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005385 }
5386 if (0) {
5387disconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01005388 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005389 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005390 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005391
Philipp Reisner32862ec2011-02-08 16:41:01 +01005392 conn_info(tconn, "asender terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005393
5394 return 0;
5395}