blob: be8f469bc8f0a50d213f14a8d733392e19150803 [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
Philipp Reisner81a5d602011-02-22 19:53:16 -0500298 if (drbd_pp_vacant > (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * minor_count)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200299 i = page_chain_free(page);
300 else {
301 struct page *tmp;
302 tmp = page_chain_tail(page, &i);
303 spin_lock(&drbd_pp_lock);
304 page_chain_add(&drbd_pp_pool, page, tmp);
305 drbd_pp_vacant += i;
306 spin_unlock(&drbd_pp_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700307 }
Lars Ellenberg435f0742010-09-06 12:30:25 +0200308 i = atomic_sub_return(i, a);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200309 if (i < 0)
Lars Ellenberg435f0742010-09-06 12:30:25 +0200310 dev_warn(DEV, "ASSERTION FAILED: %s: %d < 0\n",
311 is_net ? "pp_in_use_by_net" : "pp_in_use", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700312 wake_up(&drbd_pp_wait);
313}
314
315/*
316You need to hold the req_lock:
317 _drbd_wait_ee_list_empty()
318
319You must not have the req_lock:
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200320 drbd_free_peer_req()
Andreas Gruenbacher0db55362011-04-06 16:09:15 +0200321 drbd_alloc_peer_req()
Andreas Gruenbacher7721f562011-04-06 17:14:02 +0200322 drbd_free_peer_reqs()
Philipp Reisnerb411b362009-09-25 16:07:19 -0700323 drbd_ee_fix_bhs()
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200324 drbd_finish_peer_reqs()
Philipp Reisnerb411b362009-09-25 16:07:19 -0700325 drbd_clear_done_ee()
326 drbd_wait_ee_list_empty()
327*/
328
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +0100329struct drbd_peer_request *
Andreas Gruenbacher0db55362011-04-06 16:09:15 +0200330drbd_alloc_peer_req(struct drbd_conf *mdev, u64 id, sector_t sector,
331 unsigned int data_size, gfp_t gfp_mask) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700332{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100333 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700334 struct page *page;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200335 unsigned nr_pages = (data_size + PAGE_SIZE -1) >> PAGE_SHIFT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700336
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100337 if (drbd_insert_fault(mdev, DRBD_FAULT_AL_EE))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700338 return NULL;
339
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100340 peer_req = mempool_alloc(drbd_ee_mempool, gfp_mask & ~__GFP_HIGHMEM);
341 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700342 if (!(gfp_mask & __GFP_NOWARN))
Andreas Gruenbacher0db55362011-04-06 16:09:15 +0200343 dev_err(DEV, "%s: allocation failed\n", __func__);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700344 return NULL;
345 }
346
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200347 page = drbd_alloc_pages(mdev, nr_pages, (gfp_mask & __GFP_WAIT));
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200348 if (!page)
349 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700350
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100351 drbd_clear_interval(&peer_req->i);
352 peer_req->i.size = data_size;
353 peer_req->i.sector = sector;
354 peer_req->i.local = false;
355 peer_req->i.waiting = false;
Andreas Gruenbacher53840642011-01-28 10:31:04 +0100356
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100357 peer_req->epoch = NULL;
Philipp Reisnera21e9292011-02-08 15:08:49 +0100358 peer_req->w.mdev = mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100359 peer_req->pages = page;
360 atomic_set(&peer_req->pending_bios, 0);
361 peer_req->flags = 0;
Andreas Gruenbacher9a8e7752011-01-11 14:04:09 +0100362 /*
363 * The block_id is opaque to the receiver. It is not endianness
364 * converted, and sent back to the sender unchanged.
365 */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100366 peer_req->block_id = id;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700367
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100368 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700369
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200370 fail:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100371 mempool_free(peer_req, drbd_ee_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700372 return NULL;
373}
374
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200375void __drbd_free_peer_req(struct drbd_conf *mdev, struct drbd_peer_request *peer_req,
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +0100376 int is_net)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700377{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100378 if (peer_req->flags & EE_HAS_DIGEST)
379 kfree(peer_req->digest);
Andreas Gruenbacher5cc287e2011-04-07 21:02:59 +0200380 drbd_free_pages(mdev, peer_req->pages, is_net);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100381 D_ASSERT(atomic_read(&peer_req->pending_bios) == 0);
382 D_ASSERT(drbd_interval_empty(&peer_req->i));
383 mempool_free(peer_req, drbd_ee_mempool);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700384}
385
Andreas Gruenbacher7721f562011-04-06 17:14:02 +0200386int drbd_free_peer_reqs(struct drbd_conf *mdev, struct list_head *list)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700387{
388 LIST_HEAD(work_list);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100389 struct drbd_peer_request *peer_req, *t;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700390 int count = 0;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200391 int is_net = list == &mdev->net_ee;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700392
Philipp Reisner87eeee42011-01-19 14:16:30 +0100393 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700394 list_splice_init(list, &work_list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100395 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700396
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100397 list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200398 __drbd_free_peer_req(mdev, peer_req, is_net);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700399 count++;
400 }
401 return count;
402}
403
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200404/*
405 * See also comments in _req_mod(,BARRIER_ACKED) and receive_Barrier.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700406 */
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200407static int drbd_finish_peer_reqs(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700408{
409 LIST_HEAD(work_list);
410 LIST_HEAD(reclaimed);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100411 struct drbd_peer_request *peer_req, *t;
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100412 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700413
Philipp Reisner87eeee42011-01-19 14:16:30 +0100414 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbachera990be42011-04-06 17:56:48 +0200415 reclaim_finished_net_peer_reqs(mdev, &reclaimed);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700416 list_splice_init(&mdev->done_ee, &work_list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100417 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700418
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100419 list_for_each_entry_safe(peer_req, t, &reclaimed, w.list)
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200420 drbd_free_net_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700421
422 /* possible callbacks here:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +0100423 * e_end_block, and e_end_resync_block, e_send_discard_write.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700424 * all ignore the last argument.
425 */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100426 list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100427 int err2;
428
Philipp Reisnerb411b362009-09-25 16:07:19 -0700429 /* list_del not necessary, next/prev members not touched */
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100430 err2 = peer_req->w.cb(&peer_req->w, !!err);
431 if (!err)
432 err = err2;
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200433 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700434 }
435 wake_up(&mdev->ee_wait);
436
Andreas Gruenbachere2b30322011-03-16 17:16:12 +0100437 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700438}
439
Andreas Gruenbacherd4da1532011-04-07 00:06:56 +0200440static void _drbd_wait_ee_list_empty(struct drbd_conf *mdev,
441 struct list_head *head)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700442{
443 DEFINE_WAIT(wait);
444
445 /* avoids spin_lock/unlock
446 * and calling prepare_to_wait in the fast path */
447 while (!list_empty(head)) {
448 prepare_to_wait(&mdev->ee_wait, &wait, TASK_UNINTERRUPTIBLE);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100449 spin_unlock_irq(&mdev->tconn->req_lock);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100450 io_schedule();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700451 finish_wait(&mdev->ee_wait, &wait);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100452 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700453 }
454}
455
Andreas Gruenbacherd4da1532011-04-07 00:06:56 +0200456static void drbd_wait_ee_list_empty(struct drbd_conf *mdev,
457 struct list_head *head)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700458{
Philipp Reisner87eeee42011-01-19 14:16:30 +0100459 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700460 _drbd_wait_ee_list_empty(mdev, head);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100461 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700462}
463
464/* see also kernel_accept; which is only present since 2.6.18.
465 * also we want to log which part of it failed, exactly */
Philipp Reisner76536202011-02-07 14:09:54 +0100466static int drbd_accept(const char **what, struct socket *sock, struct socket **newsock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700467{
468 struct sock *sk = sock->sk;
469 int err = 0;
470
471 *what = "listen";
472 err = sock->ops->listen(sock, 5);
473 if (err < 0)
474 goto out;
475
476 *what = "sock_create_lite";
477 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
478 newsock);
479 if (err < 0)
480 goto out;
481
482 *what = "accept";
483 err = sock->ops->accept(sock, *newsock, 0);
484 if (err < 0) {
485 sock_release(*newsock);
486 *newsock = NULL;
487 goto out;
488 }
489 (*newsock)->ops = sock->ops;
490
491out:
492 return err;
493}
494
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100495static int drbd_recv_short(struct socket *sock, void *buf, size_t size, int flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700496{
497 mm_segment_t oldfs;
498 struct kvec iov = {
499 .iov_base = buf,
500 .iov_len = size,
501 };
502 struct msghdr msg = {
503 .msg_iovlen = 1,
504 .msg_iov = (struct iovec *)&iov,
505 .msg_flags = (flags ? flags : MSG_WAITALL | MSG_NOSIGNAL)
506 };
507 int rv;
508
509 oldfs = get_fs();
510 set_fs(KERNEL_DS);
511 rv = sock_recvmsg(sock, &msg, size, msg.msg_flags);
512 set_fs(oldfs);
513
514 return rv;
515}
516
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100517static int drbd_recv(struct drbd_tconn *tconn, void *buf, size_t size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700518{
519 mm_segment_t oldfs;
520 struct kvec iov = {
521 .iov_base = buf,
522 .iov_len = size,
523 };
524 struct msghdr msg = {
525 .msg_iovlen = 1,
526 .msg_iov = (struct iovec *)&iov,
527 .msg_flags = MSG_WAITALL | MSG_NOSIGNAL
528 };
529 int rv;
530
531 oldfs = get_fs();
532 set_fs(KERNEL_DS);
533
534 for (;;) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100535 rv = sock_recvmsg(tconn->data.socket, &msg, size, msg.msg_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700536 if (rv == size)
537 break;
538
539 /* Note:
540 * ECONNRESET other side closed the connection
541 * ERESTARTSYS (on sock) we got a signal
542 */
543
544 if (rv < 0) {
545 if (rv == -ECONNRESET)
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100546 conn_info(tconn, "sock was reset by peer\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700547 else if (rv != -ERESTARTSYS)
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100548 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700549 break;
550 } else if (rv == 0) {
Philipp Reisnerde0ff332011-02-07 16:56:20 +0100551 conn_info(tconn, "sock was shut down by peer\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700552 break;
553 } else {
554 /* signal came in, or peer/link went down,
555 * after we read a partial message
556 */
557 /* D_ASSERT(signal_pending(current)); */
558 break;
559 }
560 };
561
562 set_fs(oldfs);
563
564 if (rv != size)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100565 conn_request_state(tconn, NS(conn, C_BROKEN_PIPE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700566
567 return rv;
568}
569
Andreas Gruenbacherc6967742011-03-17 17:15:20 +0100570static int drbd_recv_all(struct drbd_tconn *tconn, void *buf, size_t size)
571{
572 int err;
573
574 err = drbd_recv(tconn, buf, size);
575 if (err != size) {
576 if (err >= 0)
577 err = -EIO;
578 } else
579 err = 0;
580 return err;
581}
582
Andreas Gruenbachera5c31902011-03-24 03:28:04 +0100583static int drbd_recv_all_warn(struct drbd_tconn *tconn, void *buf, size_t size)
584{
585 int err;
586
587 err = drbd_recv_all(tconn, buf, size);
588 if (err && !signal_pending(current))
589 conn_warn(tconn, "short read (expected size %d)\n", (int)size);
590 return err;
591}
592
Lars Ellenberg5dbf1672010-05-25 16:18:01 +0200593/* quoting tcp(7):
594 * On individual connections, the socket buffer size must be set prior to the
595 * listen(2) or connect(2) calls in order to have it take effect.
596 * This is our wrapper to do so.
597 */
598static void drbd_setbufsize(struct socket *sock, unsigned int snd,
599 unsigned int rcv)
600{
601 /* open coded SO_SNDBUF, SO_RCVBUF */
602 if (snd) {
603 sock->sk->sk_sndbuf = snd;
604 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
605 }
606 if (rcv) {
607 sock->sk->sk_rcvbuf = rcv;
608 sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
609 }
610}
611
Philipp Reisnereac3e992011-02-07 14:05:07 +0100612static struct socket *drbd_try_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700613{
614 const char *what;
615 struct socket *sock;
616 struct sockaddr_in6 src_in6;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200617 struct sockaddr_in6 peer_in6;
618 struct net_conf *nc;
619 int err, peer_addr_len, my_addr_len;
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200620 int sndbuf_size, rcvbuf_size, connect_int;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700621 int disconnect_on_error = 1;
622
Philipp Reisner44ed1672011-04-19 17:10:19 +0200623 rcu_read_lock();
624 nc = rcu_dereference(tconn->net_conf);
625 if (!nc) {
626 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700627 return NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200628 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200629 sndbuf_size = nc->sndbuf_size;
630 rcvbuf_size = nc->rcvbuf_size;
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200631 connect_int = nc->connect_int;
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200632 rcu_read_unlock();
Philipp Reisner44ed1672011-04-19 17:10:19 +0200633
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200634 my_addr_len = min_t(int, tconn->my_addr_len, sizeof(src_in6));
635 memcpy(&src_in6, &tconn->my_addr, my_addr_len);
Philipp Reisner44ed1672011-04-19 17:10:19 +0200636
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200637 if (((struct sockaddr *)&tconn->my_addr)->sa_family == AF_INET6)
Philipp Reisner44ed1672011-04-19 17:10:19 +0200638 src_in6.sin6_port = 0;
639 else
640 ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */
641
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200642 peer_addr_len = min_t(int, tconn->peer_addr_len, sizeof(src_in6));
643 memcpy(&peer_in6, &tconn->peer_addr, peer_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700644
645 what = "sock_create_kern";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200646 err = sock_create_kern(((struct sockaddr *)&src_in6)->sa_family,
647 SOCK_STREAM, IPPROTO_TCP, &sock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700648 if (err < 0) {
649 sock = NULL;
650 goto out;
651 }
652
653 sock->sk->sk_rcvtimeo =
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200654 sock->sk->sk_sndtimeo = connect_int * HZ;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200655 drbd_setbufsize(sock, sndbuf_size, rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700656
657 /* explicitly bind to the configured IP as source IP
658 * for the outgoing connections.
659 * This is needed for multihomed hosts and to be
660 * able to use lo: interfaces for drbd.
661 * Make sure to use 0 as port number, so linux selects
662 * a free one dynamically.
663 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700664 what = "bind before connect";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200665 err = sock->ops->bind(sock, (struct sockaddr *) &src_in6, my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700666 if (err < 0)
667 goto out;
668
669 /* connect may fail, peer not yet available.
670 * stay C_WF_CONNECTION, don't go Disconnecting! */
671 disconnect_on_error = 0;
672 what = "connect";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200673 err = sock->ops->connect(sock, (struct sockaddr *) &peer_in6, peer_addr_len, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700674
675out:
676 if (err < 0) {
677 if (sock) {
678 sock_release(sock);
679 sock = NULL;
680 }
681 switch (-err) {
682 /* timeout, busy, signal pending */
683 case ETIMEDOUT: case EAGAIN: case EINPROGRESS:
684 case EINTR: case ERESTARTSYS:
685 /* peer not (yet) available, network problem */
686 case ECONNREFUSED: case ENETUNREACH:
687 case EHOSTDOWN: case EHOSTUNREACH:
688 disconnect_on_error = 0;
689 break;
690 default:
Philipp Reisnereac3e992011-02-07 14:05:07 +0100691 conn_err(tconn, "%s failed, err = %d\n", what, err);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700692 }
693 if (disconnect_on_error)
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100694 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700695 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200696
Philipp Reisnerb411b362009-09-25 16:07:19 -0700697 return sock;
698}
699
Philipp Reisner76536202011-02-07 14:09:54 +0100700static struct socket *drbd_wait_for_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700701{
Philipp Reisner44ed1672011-04-19 17:10:19 +0200702 int timeo, err, my_addr_len;
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200703 int sndbuf_size, rcvbuf_size, connect_int;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700704 struct socket *s_estab = NULL, *s_listen;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200705 struct sockaddr_in6 my_addr;
706 struct net_conf *nc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700707 const char *what;
708
Philipp Reisner44ed1672011-04-19 17:10:19 +0200709 rcu_read_lock();
710 nc = rcu_dereference(tconn->net_conf);
711 if (!nc) {
712 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700713 return NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200714 }
Philipp Reisner44ed1672011-04-19 17:10:19 +0200715 sndbuf_size = nc->sndbuf_size;
716 rcvbuf_size = nc->rcvbuf_size;
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200717 connect_int = nc->connect_int;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200718 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700719
Andreas Gruenbacher089c0752011-06-14 18:28:09 +0200720 my_addr_len = min_t(int, tconn->my_addr_len, sizeof(struct sockaddr_in6));
721 memcpy(&my_addr, &tconn->my_addr, my_addr_len);
722
Philipp Reisnerb411b362009-09-25 16:07:19 -0700723 what = "sock_create_kern";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200724 err = sock_create_kern(((struct sockaddr *)&my_addr)->sa_family,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700725 SOCK_STREAM, IPPROTO_TCP, &s_listen);
726 if (err) {
727 s_listen = NULL;
728 goto out;
729 }
730
Andreas Gruenbacher69ef82d2011-05-11 14:34:35 +0200731 timeo = connect_int * HZ;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700732 timeo += (random32() & 1) ? timeo / 7 : -timeo / 7; /* 28.5% random jitter */
733
734 s_listen->sk->sk_reuse = 1; /* SO_REUSEADDR */
735 s_listen->sk->sk_rcvtimeo = timeo;
736 s_listen->sk->sk_sndtimeo = timeo;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200737 drbd_setbufsize(s_listen, sndbuf_size, rcvbuf_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700738
739 what = "bind before listen";
Philipp Reisner44ed1672011-04-19 17:10:19 +0200740 err = s_listen->ops->bind(s_listen, (struct sockaddr *)&my_addr, my_addr_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700741 if (err < 0)
742 goto out;
743
Philipp Reisner76536202011-02-07 14:09:54 +0100744 err = drbd_accept(&what, s_listen, &s_estab);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700745
746out:
747 if (s_listen)
748 sock_release(s_listen);
749 if (err < 0) {
750 if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
Philipp Reisner76536202011-02-07 14:09:54 +0100751 conn_err(tconn, "%s failed, err = %d\n", what, err);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100752 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700753 }
754 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700755
756 return s_estab;
757}
758
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200759static int decode_header(struct drbd_tconn *, void *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700760
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200761static int send_first_packet(struct drbd_tconn *tconn, struct drbd_socket *sock,
762 enum drbd_packet cmd)
763{
764 if (!conn_prepare_command(tconn, sock))
765 return -EIO;
Andreas Gruenbachere6589832011-03-30 12:54:42 +0200766 return conn_send_command(tconn, sock, cmd, 0, NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700767}
768
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200769static int receive_first_packet(struct drbd_tconn *tconn, struct socket *sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700770{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200771 unsigned int header_size = drbd_header_size(tconn);
772 struct packet_info pi;
773 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700774
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200775 err = drbd_recv_short(sock, tconn->data.rbuf, header_size, 0);
776 if (err != header_size) {
777 if (err >= 0)
778 err = -EIO;
779 return err;
780 }
781 err = decode_header(tconn, tconn->data.rbuf, &pi);
782 if (err)
783 return err;
784 return pi.cmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700785}
786
787/**
788 * drbd_socket_okay() - Free the socket if its connection is not okay
Philipp Reisnerb411b362009-09-25 16:07:19 -0700789 * @sock: pointer to the pointer to the socket.
790 */
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100791static int drbd_socket_okay(struct socket **sock)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700792{
793 int rr;
794 char tb[4];
795
796 if (!*sock)
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100797 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700798
Philipp Reisnerdbd9eea2011-02-07 15:34:16 +0100799 rr = drbd_recv_short(*sock, tb, 4, MSG_DONTWAIT | MSG_PEEK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700800
801 if (rr > 0 || rr == -EAGAIN) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100802 return true;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700803 } else {
804 sock_release(*sock);
805 *sock = NULL;
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100806 return false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700807 }
808}
Philipp Reisner2325eb62011-03-15 16:56:18 +0100809/* Gets called if a connection is established, or if a new minor gets created
810 in a connection */
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200811int drbd_connected(struct drbd_conf *mdev)
Philipp Reisner907599e2011-02-08 11:25:37 +0100812{
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100813 int err;
Philipp Reisner907599e2011-02-08 11:25:37 +0100814
815 atomic_set(&mdev->packet_seq, 0);
816 mdev->peer_seq = 0;
817
Philipp Reisner8410da8f02011-02-11 20:11:10 +0100818 mdev->state_mutex = mdev->tconn->agreed_pro_version < 100 ?
819 &mdev->tconn->cstate_mutex :
820 &mdev->own_state_mutex;
821
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100822 err = drbd_send_sync_param(mdev);
823 if (!err)
824 err = drbd_send_sizes(mdev, 0, 0);
825 if (!err)
826 err = drbd_send_uuids(mdev);
827 if (!err)
Philipp Reisner43de7c82011-11-10 13:16:13 +0100828 err = drbd_send_current_state(mdev);
Philipp Reisner907599e2011-02-08 11:25:37 +0100829 clear_bit(USE_DEGR_WFC_T, &mdev->flags);
830 clear_bit(RESIZE_PENDING, &mdev->flags);
Philipp Reisner8b924f12011-03-01 11:08:28 +0100831 mod_timer(&mdev->request_timer, jiffies + HZ); /* just start it here. */
Andreas Gruenbacher0829f5e2011-03-24 14:31:22 +0100832 return err;
Philipp Reisner907599e2011-02-08 11:25:37 +0100833}
834
Philipp Reisnerb411b362009-09-25 16:07:19 -0700835/*
836 * return values:
837 * 1 yes, we have a valid connection
838 * 0 oops, did not work out, please try again
839 * -1 peer talks different language,
840 * no point in trying again, please go standalone.
841 * -2 We do not have a network config...
842 */
Philipp Reisner81fa2e62011-05-04 15:10:30 +0200843static int conn_connect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700844{
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200845 struct socket *sock, *msock;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200846 struct drbd_conf *mdev;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200847 struct net_conf *nc;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200848 int vnr, timeout, try, h, ok;
Philipp Reisner08b165b2011-09-05 16:22:33 +0200849 bool discard_my_data;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700850
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100851 if (conn_request_state(tconn, NS(conn, C_WF_CONNECTION), CS_VERBOSE) < SS_SUCCESS)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700852 return -2;
853
Philipp Reisner907599e2011-02-08 11:25:37 +0100854 clear_bit(DISCARD_CONCURRENT, &tconn->flags);
Andreas Gruenbacher0916e0e2011-03-21 14:10:15 +0100855
856 /* Assume that the peer only understands protocol 80 until we know better. */
857 tconn->agreed_pro_version = 80;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700858
Philipp Reisnerb411b362009-09-25 16:07:19 -0700859 do {
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200860 struct socket *s;
861
Philipp Reisnerb411b362009-09-25 16:07:19 -0700862 for (try = 0;;) {
863 /* 3 tries, this should take less than a second! */
Philipp Reisner907599e2011-02-08 11:25:37 +0100864 s = drbd_try_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700865 if (s || ++try >= 3)
866 break;
867 /* give the other side time to call bind() & listen() */
Philipp Reisner20ee6392011-01-18 15:28:59 +0100868 schedule_timeout_interruptible(HZ / 10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700869 }
870
871 if (s) {
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200872 if (!tconn->data.socket) {
873 tconn->data.socket = s;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200874 send_first_packet(tconn, &tconn->data, P_INITIAL_DATA);
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200875 } else if (!tconn->meta.socket) {
876 tconn->meta.socket = s;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200877 send_first_packet(tconn, &tconn->meta, P_INITIAL_META);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700878 } else {
Philipp Reisner81fa2e62011-05-04 15:10:30 +0200879 conn_err(tconn, "Logic error in conn_connect()\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700880 goto out_release_sockets;
881 }
882 }
883
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200884 if (tconn->data.socket && tconn->meta.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100885 schedule_timeout_interruptible(tconn->net_conf->ping_timeo*HZ/10);
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200886 ok = drbd_socket_okay(&tconn->data.socket);
887 ok = drbd_socket_okay(&tconn->meta.socket) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700888 if (ok)
889 break;
890 }
891
892retry:
Philipp Reisner907599e2011-02-08 11:25:37 +0100893 s = drbd_wait_for_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700894 if (s) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +0200895 try = receive_first_packet(tconn, s);
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200896 drbd_socket_okay(&tconn->data.socket);
897 drbd_socket_okay(&tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700898 switch (try) {
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200899 case P_INITIAL_DATA:
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200900 if (tconn->data.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100901 conn_warn(tconn, "initial packet S crossed\n");
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200902 sock_release(tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700903 }
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200904 tconn->data.socket = s;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700905 break;
Andreas Gruenbachere5d6f332011-03-28 16:44:40 +0200906 case P_INITIAL_META:
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200907 if (tconn->meta.socket) {
Philipp Reisner907599e2011-02-08 11:25:37 +0100908 conn_warn(tconn, "initial packet M crossed\n");
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200909 sock_release(tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700910 }
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200911 tconn->meta.socket = s;
Philipp Reisner907599e2011-02-08 11:25:37 +0100912 set_bit(DISCARD_CONCURRENT, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700913 break;
914 default:
Philipp Reisner907599e2011-02-08 11:25:37 +0100915 conn_warn(tconn, "Error receiving initial packet\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700916 sock_release(s);
917 if (random32() & 1)
918 goto retry;
919 }
920 }
921
Philipp Reisnerbbeb6412011-02-10 13:45:46 +0100922 if (tconn->cstate <= C_DISCONNECTING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700923 goto out_release_sockets;
924 if (signal_pending(current)) {
925 flush_signals(current);
926 smp_rmb();
Philipp Reisner907599e2011-02-08 11:25:37 +0100927 if (get_t_state(&tconn->receiver) == EXITING)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700928 goto out_release_sockets;
929 }
930
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200931 if (tconn->data.socket && &tconn->meta.socket) {
932 ok = drbd_socket_okay(&tconn->data.socket);
933 ok = drbd_socket_okay(&tconn->meta.socket) && ok;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700934 if (ok)
935 break;
936 }
937 } while (1);
938
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +0200939 sock = tconn->data.socket;
940 msock = tconn->meta.socket;
941
Philipp Reisnerb411b362009-09-25 16:07:19 -0700942 msock->sk->sk_reuse = 1; /* SO_REUSEADDR */
943 sock->sk->sk_reuse = 1; /* SO_REUSEADDR */
944
945 sock->sk->sk_allocation = GFP_NOIO;
946 msock->sk->sk_allocation = GFP_NOIO;
947
948 sock->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
949 msock->sk->sk_priority = TC_PRIO_INTERACTIVE;
950
Philipp Reisnerb411b362009-09-25 16:07:19 -0700951 /* NOT YET ...
Philipp Reisner907599e2011-02-08 11:25:37 +0100952 * sock->sk->sk_sndtimeo = tconn->net_conf->timeout*HZ/10;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700953 * sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
Andreas Gruenbacher60381782011-03-28 17:05:50 +0200954 * first set it to the P_CONNECTION_FEATURES timeout,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700955 * which we set to 4x the configured ping_timeout. */
Philipp Reisner44ed1672011-04-19 17:10:19 +0200956 rcu_read_lock();
957 nc = rcu_dereference(tconn->net_conf);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700958
Philipp Reisner44ed1672011-04-19 17:10:19 +0200959 sock->sk->sk_sndtimeo =
960 sock->sk->sk_rcvtimeo = nc->ping_timeo*4*HZ/10;
961
962 msock->sk->sk_rcvtimeo = nc->ping_int*HZ;
963 timeout = nc->timeout * HZ / 10;
Philipp Reisner08b165b2011-09-05 16:22:33 +0200964 discard_my_data = nc->discard_my_data;
Philipp Reisner44ed1672011-04-19 17:10:19 +0200965 rcu_read_unlock();
966
967 msock->sk->sk_sndtimeo = timeout;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700968
969 /* we don't want delays.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300970 * we use TCP_CORK where appropriate, though */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700971 drbd_tcp_nodelay(sock);
972 drbd_tcp_nodelay(msock);
973
Philipp Reisner907599e2011-02-08 11:25:37 +0100974 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700975
Andreas Gruenbacher60381782011-03-28 17:05:50 +0200976 h = drbd_do_features(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700977 if (h <= 0)
978 return h;
979
Philipp Reisner907599e2011-02-08 11:25:37 +0100980 if (tconn->cram_hmac_tfm) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700981 /* drbd_request_state(mdev, NS(conn, WFAuth)); */
Philipp Reisner907599e2011-02-08 11:25:37 +0100982 switch (drbd_do_auth(tconn)) {
Johannes Thomab10d96c2010-01-07 16:02:50 +0100983 case -1:
Philipp Reisner907599e2011-02-08 11:25:37 +0100984 conn_err(tconn, "Authentication of peer failed\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700985 return -1;
Johannes Thomab10d96c2010-01-07 16:02:50 +0100986 case 0:
Philipp Reisner907599e2011-02-08 11:25:37 +0100987 conn_err(tconn, "Authentication of peer failed, trying again.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +0100988 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700989 }
990 }
991
Philipp Reisner44ed1672011-04-19 17:10:19 +0200992 sock->sk->sk_sndtimeo = timeout;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700993 sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
994
Andreas Gruenbacher387eb302011-03-16 01:05:37 +0100995 if (drbd_send_protocol(tconn) == -EOPNOTSUPP)
Philipp Reisner7e2455c2010-04-22 14:50:23 +0200996 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700997
Philipp Reisnerc141ebd2011-05-05 16:13:10 +0200998 rcu_read_lock();
999 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1000 kref_get(&mdev->kref);
1001 rcu_read_unlock();
Philipp Reisner08b165b2011-09-05 16:22:33 +02001002
1003 if (discard_my_data)
1004 set_bit(DISCARD_MY_DATA, &mdev->flags);
1005 else
1006 clear_bit(DISCARD_MY_DATA, &mdev->flags);
1007
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001008 drbd_connected(mdev);
1009 kref_put(&mdev->kref, &drbd_minor_destroy);
1010 rcu_read_lock();
1011 }
1012 rcu_read_unlock();
1013
Philipp Reisner823bd832012-11-08 15:04:36 +01001014 if (conn_request_state(tconn, NS(conn, C_WF_REPORT_PARAMS), CS_VERBOSE) < SS_SUCCESS)
1015 return 0;
1016
1017 drbd_thread_start(&tconn->asender);
1018
Philipp Reisner08b165b2011-09-05 16:22:33 +02001019 mutex_lock(&tconn->conf_update);
1020 /* The discard_my_data flag is a single-shot modifier to the next
1021 * connection attempt, the handshake of which is now well underway.
1022 * No need for rcu style copying of the whole struct
1023 * just to clear a single value. */
1024 tconn->net_conf->discard_my_data = 0;
1025 mutex_unlock(&tconn->conf_update);
1026
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07001027 return h;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001028
1029out_release_sockets:
Andreas Gruenbacher2bf89622011-03-28 16:33:12 +02001030 if (tconn->data.socket) {
1031 sock_release(tconn->data.socket);
1032 tconn->data.socket = NULL;
1033 }
1034 if (tconn->meta.socket) {
1035 sock_release(tconn->meta.socket);
1036 tconn->meta.socket = NULL;
1037 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001038 return -1;
1039}
1040
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001041static int decode_header(struct drbd_tconn *tconn, void *header, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001042{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001043 unsigned int header_size = drbd_header_size(tconn);
1044
Andreas Gruenbacher0c8e36d2011-03-30 16:00:17 +02001045 if (header_size == sizeof(struct p_header100) &&
1046 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC_100)) {
1047 struct p_header100 *h = header;
1048 if (h->pad != 0) {
1049 conn_err(tconn, "Header padding is not zero\n");
1050 return -EINVAL;
1051 }
1052 pi->vnr = be16_to_cpu(h->volume);
1053 pi->cmd = be16_to_cpu(h->command);
1054 pi->size = be32_to_cpu(h->length);
1055 } else if (header_size == sizeof(struct p_header95) &&
1056 *(__be16 *)header == cpu_to_be16(DRBD_MAGIC_BIG)) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001057 struct p_header95 *h = header;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001058 pi->cmd = be16_to_cpu(h->command);
Andreas Gruenbacherb55d84b2011-03-22 13:17:47 +01001059 pi->size = be32_to_cpu(h->length);
1060 pi->vnr = 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001061 } else if (header_size == sizeof(struct p_header80) &&
1062 *(__be32 *)header == cpu_to_be32(DRBD_MAGIC)) {
1063 struct p_header80 *h = header;
1064 pi->cmd = be16_to_cpu(h->command);
1065 pi->size = be16_to_cpu(h->length);
Philipp Reisner77351055b2011-02-07 17:24:26 +01001066 pi->vnr = 0;
Philipp Reisner02918be2010-08-20 14:35:10 +02001067 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001068 conn_err(tconn, "Wrong magic value 0x%08x in protocol version %d\n",
1069 be32_to_cpu(*(__be32 *)header),
1070 tconn->agreed_pro_version);
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001071 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001072 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001073 pi->data = header + header_size;
Andreas Gruenbacher8172f3e2011-03-16 17:22:39 +01001074 return 0;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001075}
1076
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001077static int drbd_recv_header(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner257d0af2011-01-26 12:15:29 +01001078{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001079 void *buffer = tconn->data.rbuf;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001080 int err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001081
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001082 err = drbd_recv_all_warn(tconn, buffer, drbd_header_size(tconn));
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001083 if (err)
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001084 return err;
Philipp Reisner257d0af2011-01-26 12:15:29 +01001085
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001086 err = decode_header(tconn, buffer, pi);
Philipp Reisner9ba7aa02011-02-07 17:32:41 +01001087 tconn->last_received = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001088
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01001089 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001090}
1091
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001092static void drbd_flush(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001093{
1094 int rv;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001095 struct drbd_conf *mdev;
1096 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001097
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001098 if (tconn->write_ordering >= WO_bdev_flush) {
1099 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1100 if (get_ldev(mdev)) {
1101 rv = blkdev_issue_flush(mdev->ldev->backing_bdev, GFP_KERNEL,
1102 NULL);
1103 put_ldev(mdev);
1104
1105 if (rv) {
1106 dev_info(DEV, "local disk flush failed with status %d\n", rv);
1107 /* would rather check on EOPNOTSUPP, but that is not reliable.
1108 * don't try again for ANY return value != 0
1109 * if (rv == -EOPNOTSUPP) */
1110 drbd_bump_write_ordering(tconn, WO_drain_io);
1111 break;
1112 }
1113 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001114 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001115 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001116}
1117
1118/**
1119 * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
1120 * @mdev: DRBD device.
1121 * @epoch: Epoch object.
1122 * @ev: Epoch event.
1123 */
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001124static enum finish_epoch drbd_may_finish_epoch(struct drbd_tconn *tconn,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001125 struct drbd_epoch *epoch,
1126 enum epoch_event ev)
1127{
Philipp Reisner2451fc32010-08-24 13:43:11 +02001128 int epoch_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001129 struct drbd_epoch *next_epoch;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001130 enum finish_epoch rv = FE_STILL_LIVE;
1131
Philipp Reisner12038a32011-11-09 19:18:00 +01001132 spin_lock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001133 do {
1134 next_epoch = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001135
1136 epoch_size = atomic_read(&epoch->epoch_size);
1137
1138 switch (ev & ~EV_CLEANUP) {
1139 case EV_PUT:
1140 atomic_dec(&epoch->active);
1141 break;
1142 case EV_GOT_BARRIER_NR:
1143 set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001144 break;
1145 case EV_BECAME_LAST:
1146 /* nothing to do*/
1147 break;
1148 }
1149
Philipp Reisnerb411b362009-09-25 16:07:19 -07001150 if (epoch_size != 0 &&
1151 atomic_read(&epoch->active) == 0 &&
Philipp Reisner85d73512011-07-18 15:45:15 +02001152 (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags) || ev & EV_CLEANUP)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001153 if (!(ev & EV_CLEANUP)) {
Philipp Reisner12038a32011-11-09 19:18:00 +01001154 spin_unlock(&tconn->epoch_lock);
Philipp Reisner1d2783d2011-11-10 14:56:07 +01001155 drbd_send_b_ack(epoch->mdev, epoch->barrier_nr, epoch_size);
Philipp Reisner12038a32011-11-09 19:18:00 +01001156 spin_lock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001157 }
Philipp Reisner85d73512011-07-18 15:45:15 +02001158 if (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags))
Philipp Reisner1d2783d2011-11-10 14:56:07 +01001159 dec_unacked(epoch->mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001160
Philipp Reisner12038a32011-11-09 19:18:00 +01001161 if (tconn->current_epoch != epoch) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001162 next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
1163 list_del(&epoch->list);
1164 ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
Philipp Reisner12038a32011-11-09 19:18:00 +01001165 tconn->epochs--;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001166 kfree(epoch);
1167
1168 if (rv == FE_STILL_LIVE)
1169 rv = FE_DESTROYED;
1170 } else {
1171 epoch->flags = 0;
1172 atomic_set(&epoch->epoch_size, 0);
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001173 /* atomic_set(&epoch->active, 0); is already zero */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001174 if (rv == FE_STILL_LIVE)
1175 rv = FE_RECYCLED;
1176 }
1177 }
1178
1179 if (!next_epoch)
1180 break;
1181
1182 epoch = next_epoch;
1183 } while (1);
1184
Philipp Reisner12038a32011-11-09 19:18:00 +01001185 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001186
Philipp Reisnerb411b362009-09-25 16:07:19 -07001187 return rv;
1188}
1189
1190/**
1191 * drbd_bump_write_ordering() - Fall back to an other write ordering method
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001192 * @tconn: DRBD connection.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001193 * @wo: Write ordering method to try.
1194 */
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001195void drbd_bump_write_ordering(struct drbd_tconn *tconn, enum write_ordering_e wo)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001196{
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001197 struct disk_conf *dc;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001198 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001199 enum write_ordering_e pwo;
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001200 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001201 static char *write_ordering_str[] = {
1202 [WO_none] = "none",
1203 [WO_drain_io] = "drain",
1204 [WO_bdev_flush] = "flush",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001205 };
1206
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001207 pwo = tconn->write_ordering;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001208 wo = min(pwo, wo);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001209 rcu_read_lock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001210 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1211 if (!get_ldev(mdev))
1212 continue;
1213 dc = rcu_dereference(mdev->ldev->disk_conf);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001214
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001215 if (wo == WO_bdev_flush && !dc->disk_flushes)
1216 wo = WO_drain_io;
1217 if (wo == WO_drain_io && !dc->disk_drain)
1218 wo = WO_none;
1219 put_ldev(mdev);
1220 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001221 rcu_read_unlock();
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001222 tconn->write_ordering = wo;
1223 if (pwo != tconn->write_ordering || wo == WO_bdev_flush)
1224 conn_info(tconn, "Method to ensure write ordering: %s\n", write_ordering_str[tconn->write_ordering]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001225}
1226
1227/**
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001228 * drbd_submit_peer_request()
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001229 * @mdev: DRBD device.
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001230 * @peer_req: peer request
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001231 * @rw: flag field, see bio->bi_rw
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001232 *
1233 * May spread the pages to multiple bios,
1234 * depending on bio_add_page restrictions.
1235 *
1236 * Returns 0 if all bios have been submitted,
1237 * -ENOMEM if we could not allocate enough bios,
1238 * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a
1239 * single page to an empty bio (which should never happen and likely indicates
1240 * that the lower level IO stack is in some way broken). This has been observed
1241 * on certain Xen deployments.
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001242 */
1243/* TODO allocate from our own bio_set. */
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001244int drbd_submit_peer_request(struct drbd_conf *mdev,
1245 struct drbd_peer_request *peer_req,
1246 const unsigned rw, const int fault_type)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001247{
1248 struct bio *bios = NULL;
1249 struct bio *bio;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001250 struct page *page = peer_req->pages;
1251 sector_t sector = peer_req->i.sector;
1252 unsigned ds = peer_req->i.size;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001253 unsigned n_bios = 0;
1254 unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001255 int err = -ENOMEM;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001256
1257 /* In most cases, we will only need one bio. But in case the lower
1258 * level restrictions happen to be different at this offset on this
1259 * side than those of the sending peer, we may need to submit the
Lars Ellenbergda4a75d2011-02-23 17:02:01 +01001260 * request in more than one bio.
1261 *
1262 * Plain bio_alloc is good enough here, this is no DRBD internally
1263 * generated bio, but a bio allocated on behalf of the peer.
1264 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001265next_bio:
1266 bio = bio_alloc(GFP_NOIO, nr_pages);
1267 if (!bio) {
1268 dev_err(DEV, "submit_ee: Allocation of a bio failed\n");
1269 goto fail;
1270 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001271 /* > peer_req->i.sector, unless this is the first bio */
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001272 bio->bi_sector = sector;
1273 bio->bi_bdev = mdev->ldev->backing_bdev;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001274 bio->bi_rw = rw;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001275 bio->bi_private = peer_req;
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001276 bio->bi_end_io = drbd_peer_request_endio;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001277
1278 bio->bi_next = bios;
1279 bios = bio;
1280 ++n_bios;
1281
1282 page_chain_for_each(page) {
1283 unsigned len = min_t(unsigned, ds, PAGE_SIZE);
1284 if (!bio_add_page(bio, page, len, 0)) {
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001285 /* A single page must always be possible!
1286 * But in case it fails anyways,
1287 * we deal with it, and complain (below). */
1288 if (bio->bi_vcnt == 0) {
1289 dev_err(DEV,
1290 "bio_add_page failed for len=%u, "
1291 "bi_vcnt=0 (bi_sector=%llu)\n",
1292 len, (unsigned long long)bio->bi_sector);
1293 err = -ENOSPC;
1294 goto fail;
1295 }
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001296 goto next_bio;
1297 }
1298 ds -= len;
1299 sector += len >> 9;
1300 --nr_pages;
1301 }
1302 D_ASSERT(page == NULL);
1303 D_ASSERT(ds == 0);
1304
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001305 atomic_set(&peer_req->pending_bios, n_bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001306 do {
1307 bio = bios;
1308 bios = bios->bi_next;
1309 bio->bi_next = NULL;
1310
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001311 drbd_generic_make_request(mdev, fault_type, bio);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001312 } while (bios);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001313 return 0;
1314
1315fail:
1316 while (bios) {
1317 bio = bios;
1318 bios = bios->bi_next;
1319 bio_put(bio);
1320 }
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001321 return err;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001322}
1323
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001324static void drbd_remove_epoch_entry_interval(struct drbd_conf *mdev,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001325 struct drbd_peer_request *peer_req)
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001326{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001327 struct drbd_interval *i = &peer_req->i;
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001328
1329 drbd_remove_interval(&mdev->write_requests, i);
1330 drbd_clear_interval(i);
1331
Andreas Gruenbacher6c852be2011-02-04 15:38:52 +01001332 /* Wake up any processes waiting for this peer request to complete. */
Andreas Gruenbacher53840642011-01-28 10:31:04 +01001333 if (i->waiting)
1334 wake_up(&mdev->misc_wait);
1335}
1336
Philipp Reisner77fede52011-11-10 21:19:11 +01001337void conn_wait_active_ee_empty(struct drbd_tconn *tconn)
1338{
1339 struct drbd_conf *mdev;
1340 int vnr;
1341
1342 rcu_read_lock();
1343 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1344 kref_get(&mdev->kref);
1345 rcu_read_unlock();
1346 drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
1347 kref_put(&mdev->kref, &drbd_minor_destroy);
1348 rcu_read_lock();
1349 }
1350 rcu_read_unlock();
1351}
1352
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001353static int receive_Barrier(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001354{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001355 struct drbd_conf *mdev;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001356 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001357 struct p_barrier *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001358 struct drbd_epoch *epoch;
1359
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001360 mdev = vnr_to_mdev(tconn, pi->vnr);
1361 if (!mdev)
1362 return -EIO;
1363
Philipp Reisnerb411b362009-09-25 16:07:19 -07001364 inc_unacked(mdev);
1365
Philipp Reisner12038a32011-11-09 19:18:00 +01001366 tconn->current_epoch->barrier_nr = p->barrier;
1367 tconn->current_epoch->mdev = mdev;
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001368 rv = drbd_may_finish_epoch(tconn, tconn->current_epoch, EV_GOT_BARRIER_NR);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001369
1370 /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
1371 * the activity log, which means it would not be resynced in case the
1372 * R_PRIMARY crashes now.
1373 * Therefore we must send the barrier_ack after the barrier request was
1374 * completed. */
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001375 switch (tconn->write_ordering) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001376 case WO_none:
1377 if (rv == FE_RECYCLED)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001378 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001379
1380 /* receiver context, in the writeout path of the other node.
1381 * avoid potential distributed deadlock */
1382 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1383 if (epoch)
1384 break;
1385 else
1386 dev_warn(DEV, "Allocation of an epoch failed, slowing down\n");
1387 /* Fall through */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001388
1389 case WO_bdev_flush:
1390 case WO_drain_io:
Philipp Reisner77fede52011-11-10 21:19:11 +01001391 conn_wait_active_ee_empty(tconn);
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001392 drbd_flush(tconn);
Philipp Reisner2451fc32010-08-24 13:43:11 +02001393
Philipp Reisner12038a32011-11-09 19:18:00 +01001394 if (atomic_read(&tconn->current_epoch->epoch_size)) {
Philipp Reisner2451fc32010-08-24 13:43:11 +02001395 epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
1396 if (epoch)
1397 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001398 }
1399
Philipp Reisner12038a32011-11-09 19:18:00 +01001400 epoch = tconn->current_epoch;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001401 wait_event(mdev->ee_wait, atomic_read(&epoch->epoch_size) == 0);
1402
1403 D_ASSERT(atomic_read(&epoch->active) == 0);
1404 D_ASSERT(epoch->flags == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001405
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001406 return 0;
Philipp Reisner2451fc32010-08-24 13:43:11 +02001407 default:
Philipp Reisner4b0007c2011-11-09 20:12:34 +01001408 dev_err(DEV, "Strangeness in tconn->write_ordering %d\n", tconn->write_ordering);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001409 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001410 }
1411
1412 epoch->flags = 0;
1413 atomic_set(&epoch->epoch_size, 0);
1414 atomic_set(&epoch->active, 0);
1415
Philipp Reisner12038a32011-11-09 19:18:00 +01001416 spin_lock(&tconn->epoch_lock);
1417 if (atomic_read(&tconn->current_epoch->epoch_size)) {
1418 list_add(&epoch->list, &tconn->current_epoch->list);
1419 tconn->current_epoch = epoch;
1420 tconn->epochs++;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001421 } else {
1422 /* The current_epoch got recycled while we allocated this one... */
1423 kfree(epoch);
1424 }
Philipp Reisner12038a32011-11-09 19:18:00 +01001425 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001426
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001427 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001428}
1429
1430/* used from receive_RSDataReply (recv_resync_read)
1431 * and from receive_Data */
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +01001432static struct drbd_peer_request *
1433read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector,
1434 int data_size) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001435{
Lars Ellenberg66660322010-04-06 12:15:04 +02001436 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001437 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001438 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001439 int dgs, ds, err;
Philipp Reisnera0638452011-01-19 14:31:32 +01001440 void *dig_in = mdev->tconn->int_dig_in;
1441 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001442 unsigned long *data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001443
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001444 dgs = 0;
1445 if (mdev->tconn->peer_integrity_tfm) {
1446 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001447 /*
1448 * FIXME: Receive the incoming digest into the receive buffer
1449 * here, together with its struct p_data?
1450 */
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001451 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1452 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001453 return NULL;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001454 data_size -= dgs;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001455 }
1456
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001457 if (!expect(data_size != 0))
1458 return NULL;
1459 if (!expect(IS_ALIGNED(data_size, 512)))
1460 return NULL;
1461 if (!expect(data_size <= DRBD_MAX_BIO_SIZE))
1462 return NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001463
Lars Ellenberg66660322010-04-06 12:15:04 +02001464 /* even though we trust out peer,
1465 * we sometimes have to double check. */
1466 if (sector + (data_size>>9) > capacity) {
Lars Ellenbergfdda6542011-01-24 15:11:01 +01001467 dev_err(DEV, "request from peer beyond end of local disk: "
1468 "capacity: %llus < sector: %llus + size: %u\n",
Lars Ellenberg66660322010-04-06 12:15:04 +02001469 (unsigned long long)capacity,
1470 (unsigned long long)sector, data_size);
1471 return NULL;
1472 }
1473
Philipp Reisnerb411b362009-09-25 16:07:19 -07001474 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
1475 * "criss-cross" setup, that might cause write-out on some other DRBD,
1476 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02001477 peer_req = drbd_alloc_peer_req(mdev, id, sector, data_size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001478 if (!peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001479 return NULL;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001480
Philipp Reisnerb411b362009-09-25 16:07:19 -07001481 ds = data_size;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001482 page = peer_req->pages;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001483 page_chain_for_each(page) {
1484 unsigned len = min_t(int, ds, PAGE_SIZE);
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001485 data = kmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001486 err = drbd_recv_all_warn(mdev->tconn, data, len);
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +01001487 if (drbd_insert_fault(mdev, DRBD_FAULT_RECEIVE)) {
Philipp Reisner6b4388a2010-04-26 14:11:45 +02001488 dev_err(DEV, "Fault injection: Corrupting data on receive\n");
1489 data[0] = data[0] ^ (unsigned long)-1;
1490 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001491 kunmap(page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001492 if (err) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001493 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001494 return NULL;
1495 }
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001496 ds -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001497 }
1498
1499 if (dgs) {
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02001500 drbd_csum_ee(mdev, mdev->tconn->peer_integrity_tfm, peer_req, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001501 if (memcmp(dig_in, dig_vv, dgs)) {
Lars Ellenberg470be442010-11-10 10:36:52 +01001502 dev_err(DEV, "Digest integrity check FAILED: %llus +%u\n",
1503 (unsigned long long)sector, data_size);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001504 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001505 return NULL;
1506 }
1507 }
1508 mdev->recv_cnt += data_size>>9;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001509 return peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001510}
1511
1512/* drbd_drain_block() just takes a data block
1513 * out of the socket input buffer, and discards it.
1514 */
1515static int drbd_drain_block(struct drbd_conf *mdev, int data_size)
1516{
1517 struct page *page;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001518 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001519 void *data;
1520
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001521 if (!data_size)
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001522 return 0;
Lars Ellenbergc3470cd2010-04-01 16:57:19 +02001523
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +02001524 page = drbd_alloc_pages(mdev, 1, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001525
1526 data = kmap(page);
1527 while (data_size) {
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001528 unsigned int len = min_t(int, data_size, PAGE_SIZE);
1529
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001530 err = drbd_recv_all_warn(mdev->tconn, data, len);
1531 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001532 break;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001533 data_size -= len;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001534 }
1535 kunmap(page);
Andreas Gruenbacher5cc287e2011-04-07 21:02:59 +02001536 drbd_free_pages(mdev, page, 0);
Andreas Gruenbacherfc5be832011-03-16 17:50:50 +01001537 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001538}
1539
1540static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req,
1541 sector_t sector, int data_size)
1542{
1543 struct bio_vec *bvec;
1544 struct bio *bio;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001545 int dgs, err, i, expect;
Philipp Reisnera0638452011-01-19 14:31:32 +01001546 void *dig_in = mdev->tconn->int_dig_in;
1547 void *dig_vv = mdev->tconn->int_dig_vv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001548
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001549 dgs = 0;
1550 if (mdev->tconn->peer_integrity_tfm) {
1551 dgs = crypto_hash_digestsize(mdev->tconn->peer_integrity_tfm);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001552 err = drbd_recv_all_warn(mdev->tconn, dig_in, dgs);
1553 if (err)
1554 return err;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02001555 data_size -= dgs;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001556 }
1557
Philipp Reisnerb411b362009-09-25 16:07:19 -07001558 /* optimistically update recv_cnt. if receiving fails below,
1559 * we disconnect anyways, and counters will be reset. */
1560 mdev->recv_cnt += data_size>>9;
1561
1562 bio = req->master_bio;
1563 D_ASSERT(sector == bio->bi_sector);
1564
1565 bio_for_each_segment(bvec, bio, i) {
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001566 void *mapped = kmap(bvec->bv_page) + bvec->bv_offset;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001567 expect = min_t(int, data_size, bvec->bv_len);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001568 err = drbd_recv_all_warn(mdev->tconn, mapped, expect);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001569 kunmap(bvec->bv_page);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01001570 if (err)
1571 return err;
1572 data_size -= expect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001573 }
1574
1575 if (dgs) {
Andreas Gruenbacher5b614ab2011-04-27 21:00:12 +02001576 drbd_csum_bio(mdev, mdev->tconn->peer_integrity_tfm, bio, dig_vv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001577 if (memcmp(dig_in, dig_vv, dgs)) {
1578 dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n");
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001579 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001580 }
1581 }
1582
1583 D_ASSERT(data_size == 0);
Andreas Gruenbacher28284ce2011-03-16 17:54:02 +01001584 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001585}
1586
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001587/*
1588 * e_end_resync_block() is called in asender context via
1589 * drbd_finish_peer_reqs().
1590 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001591static int e_end_resync_block(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001592{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001593 struct drbd_peer_request *peer_req =
1594 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001595 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001596 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001597 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001598
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001599 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001600
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001601 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1602 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001603 err = drbd_send_ack(mdev, P_RS_WRITE_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001604 } else {
1605 /* Record failure to sync */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001606 drbd_rs_failed_io(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001607
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001608 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001609 }
1610 dec_unacked(mdev);
1611
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001612 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001613}
1614
1615static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local)
1616{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001617 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001618
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001619 peer_req = read_in_block(mdev, ID_SYNCER, sector, data_size);
1620 if (!peer_req)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001621 goto fail;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001622
1623 dec_rs_pending(mdev);
1624
Philipp Reisnerb411b362009-09-25 16:07:19 -07001625 inc_unacked(mdev);
1626 /* corresponding dec_unacked() in e_end_resync_block()
1627 * respective _drbd_clear_done_ee */
1628
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001629 peer_req->w.cb = e_end_resync_block;
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001630
Philipp Reisner87eeee42011-01-19 14:16:30 +01001631 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001632 list_add(&peer_req->w.list, &mdev->sync_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001633 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001634
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001635 atomic_add(data_size >> 9, &mdev->rs_sect_ev);
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01001636 if (drbd_submit_peer_request(mdev, peer_req, WRITE, DRBD_FAULT_RS_WR) == 0)
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001637 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001638
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01001639 /* don't care for the reason here */
1640 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01001641 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001642 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001643 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02001644
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001645 drbd_free_peer_req(mdev, peer_req);
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001646fail:
1647 put_ldev(mdev);
Andreas Gruenbachere1c1b0f2011-03-16 17:58:27 +01001648 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001649}
1650
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001651static struct drbd_request *
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001652find_request(struct drbd_conf *mdev, struct rb_root *root, u64 id,
1653 sector_t sector, bool missing_ok, const char *func)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001654{
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001655 struct drbd_request *req;
1656
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001657 /* Request object according to our peer */
1658 req = (struct drbd_request *)(unsigned long)id;
Andreas Gruenbacher5e472262011-01-27 14:42:51 +01001659 if (drbd_contains_interval(root, sector, &req->i) && req->i.local)
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001660 return req;
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001661 if (!missing_ok) {
Andreas Gruenbacher5af172e2011-07-15 09:43:23 +02001662 dev_err(DEV, "%s: failed to find request 0x%lx, sector %llus\n", func,
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001663 (unsigned long)id, (unsigned long long)sector);
1664 }
Andreas Gruenbacher668eebc2011-01-20 17:14:26 +01001665 return NULL;
1666}
1667
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001668static int receive_DataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001669{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001670 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001671 struct drbd_request *req;
1672 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001673 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001674 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001675
1676 mdev = vnr_to_mdev(tconn, pi->vnr);
1677 if (!mdev)
1678 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001679
1680 sector = be64_to_cpu(p->sector);
1681
Philipp Reisner87eeee42011-01-19 14:16:30 +01001682 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01001683 req = find_request(mdev, &mdev->read_requests, p->block_id, sector, false, __func__);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001684 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01001685 if (unlikely(!req))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001686 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001687
Bart Van Assche24c48302011-05-21 18:32:29 +02001688 /* hlist_del(&req->collision) is done in _req_may_be_done, to avoid
Philipp Reisnerb411b362009-09-25 16:07:19 -07001689 * special casing it there for the various failure cases.
1690 * still no race with drbd_fail_pending_reads */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001691 err = recv_dless_read(mdev, req, sector, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001692 if (!err)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001693 req_mod(req, DATA_RECEIVED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001694 /* else: nothing. handled from drbd_disconnect...
1695 * I don't think we may complete this just yet
1696 * in case we are "on-disconnect: freeze" */
1697
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001698 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001699}
1700
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001701static int receive_RSDataReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001702{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001703 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001704 sector_t sector;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001705 int err;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001706 struct p_data *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01001707
1708 mdev = vnr_to_mdev(tconn, pi->vnr);
1709 if (!mdev)
1710 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001711
1712 sector = be64_to_cpu(p->sector);
1713 D_ASSERT(p->block_id == ID_SYNCER);
1714
1715 if (get_ldev(mdev)) {
1716 /* data is submitted to disk within recv_resync_read.
1717 * corresponding put_ldev done below on error,
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01001718 * or in drbd_peer_request_endio. */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001719 err = recv_resync_read(mdev, sector, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001720 } else {
1721 if (__ratelimit(&drbd_ratelimit_state))
1722 dev_err(DEV, "Can not write resync data to local disk.\n");
1723
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001724 err = drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001725
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001726 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001727 }
1728
Andreas Gruenbachere2857212011-03-25 00:57:38 +01001729 atomic_add(pi->size >> 9, &mdev->rs_sect_in);
Philipp Reisner778f2712010-07-06 11:14:00 +02001730
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01001731 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001732}
1733
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001734static int w_restart_write(struct drbd_work *w, int cancel)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001735{
1736 struct drbd_request *req = container_of(w, struct drbd_request, w);
1737 struct drbd_conf *mdev = w->mdev;
1738 struct bio *bio;
1739 unsigned long start_time;
1740 unsigned long flags;
1741
1742 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
1743 if (!expect(req->rq_state & RQ_POSTPONED)) {
1744 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001745 return -EIO;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001746 }
1747 bio = req->master_bio;
1748 start_time = req->start_time;
1749 /* Postponed requests will not have their master_bio completed! */
1750 __req_mod(req, DISCARD_WRITE, NULL);
1751 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
1752
1753 while (__drbd_make_request(mdev, bio, start_time))
1754 /* retry */ ;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001755 return 0;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001756}
1757
1758static void restart_conflicting_writes(struct drbd_conf *mdev,
1759 sector_t sector, int size)
1760{
1761 struct drbd_interval *i;
1762 struct drbd_request *req;
1763
1764 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1765 if (!i->local)
1766 continue;
1767 req = container_of(i, struct drbd_request, i);
1768 if (req->rq_state & RQ_LOCAL_PENDING ||
1769 !(req->rq_state & RQ_POSTPONED))
1770 continue;
1771 if (expect(list_empty(&req->w.list))) {
1772 req->w.mdev = mdev;
1773 req->w.cb = w_restart_write;
1774 drbd_queue_work(&mdev->tconn->data.work, &req->w);
1775 }
1776 }
1777}
1778
Andreas Gruenbachera990be42011-04-06 17:56:48 +02001779/*
1780 * e_end_block() is called in asender context via drbd_finish_peer_reqs().
Philipp Reisnerb411b362009-09-25 16:07:19 -07001781 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001782static int e_end_block(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001783{
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001784 struct drbd_peer_request *peer_req =
1785 container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001786 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001787 sector_t sector = peer_req->i.sector;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001788 int err = 0, pcmd;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001789
Philipp Reisner303d1442011-04-13 16:24:47 -07001790 if (peer_req->flags & EE_SEND_WRITE_ACK) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001791 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001792 pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
1793 mdev->state.conn <= C_PAUSED_SYNC_T &&
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001794 peer_req->flags & EE_MAY_SET_IN_SYNC) ?
Philipp Reisnerb411b362009-09-25 16:07:19 -07001795 P_RS_WRITE_ACK : P_WRITE_ACK;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001796 err = drbd_send_ack(mdev, pcmd, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001797 if (pcmd == P_RS_WRITE_ACK)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001798 drbd_set_in_sync(mdev, sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001799 } else {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001800 err = drbd_send_ack(mdev, P_NEG_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001801 /* we expect it to be marked out of sync anyways...
1802 * maybe assert this? */
1803 }
1804 dec_unacked(mdev);
1805 }
1806 /* we delete from the conflict detection hash _after_ we sent out the
1807 * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
Philipp Reisner302bdea2011-04-21 11:36:49 +02001808 if (peer_req->flags & EE_IN_INTERVAL_TREE) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01001809 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001810 D_ASSERT(!drbd_interval_empty(&peer_req->i));
1811 drbd_remove_epoch_entry_interval(mdev, peer_req);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001812 if (peer_req->flags & EE_RESTART_REQUESTS)
1813 restart_conflicting_writes(mdev, sector, peer_req->i.size);
Philipp Reisner87eeee42011-01-19 14:16:30 +01001814 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +01001815 } else
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001816 D_ASSERT(drbd_interval_empty(&peer_req->i));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001817
Philipp Reisner1e9dd292011-11-10 15:14:53 +01001818 drbd_may_finish_epoch(mdev->tconn, peer_req->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001819
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001820 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001821}
1822
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001823static int e_send_ack(struct drbd_work *w, enum drbd_packet ack)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001824{
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001825 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacher8050e6d2011-02-18 16:12:48 +01001826 struct drbd_peer_request *peer_req =
1827 container_of(w, struct drbd_peer_request, w);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001828 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001829
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001830 err = drbd_send_ack(mdev, ack, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001831 dec_unacked(mdev);
1832
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001833 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001834}
1835
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001836static int e_send_discard_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001837{
1838 return e_send_ack(w, P_DISCARD_WRITE);
1839}
1840
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001841static int e_send_retry_write(struct drbd_work *w, int unused)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001842{
1843 struct drbd_tconn *tconn = w->mdev->tconn;
1844
1845 return e_send_ack(w, tconn->agreed_pro_version >= 100 ?
1846 P_RETRY_WRITE : P_DISCARD_WRITE);
1847}
1848
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001849static bool seq_greater(u32 a, u32 b)
1850{
1851 /*
1852 * We assume 32-bit wrap-around here.
1853 * For 24-bit wrap-around, we would have to shift:
1854 * a <<= 8; b <<= 8;
1855 */
1856 return (s32)a - (s32)b > 0;
1857}
1858
1859static u32 seq_max(u32 a, u32 b)
1860{
1861 return seq_greater(a, b) ? a : b;
1862}
1863
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001864static bool need_peer_seq(struct drbd_conf *mdev)
1865{
1866 struct drbd_tconn *tconn = mdev->tconn;
Philipp Reisner302bdea2011-04-21 11:36:49 +02001867 int tp;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001868
1869 /*
1870 * We only need to keep track of the last packet_seq number of our peer
1871 * if we are in dual-primary mode and we have the discard flag set; see
1872 * handle_write_conflicts().
1873 */
Philipp Reisner302bdea2011-04-21 11:36:49 +02001874
1875 rcu_read_lock();
1876 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
1877 rcu_read_unlock();
1878
1879 return tp && test_bit(DISCARD_CONCURRENT, &tconn->flags);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001880}
1881
Andreas Gruenbacher43ae0772011-02-03 18:42:08 +01001882static void update_peer_seq(struct drbd_conf *mdev, unsigned int peer_seq)
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001883{
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001884 unsigned int newest_peer_seq;
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001885
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001886 if (need_peer_seq(mdev)) {
1887 spin_lock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001888 newest_peer_seq = seq_max(mdev->peer_seq, peer_seq);
1889 mdev->peer_seq = newest_peer_seq;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001890 spin_unlock(&mdev->peer_seq_lock);
Lars Ellenberg3c13b682011-02-23 16:10:01 +01001891 /* wake up only if we actually changed mdev->peer_seq */
1892 if (peer_seq == newest_peer_seq)
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001893 wake_up(&mdev->seq_wait);
1894 }
Andreas Gruenbacher3e394da2011-01-26 18:36:55 +01001895}
1896
Philipp Reisnerb411b362009-09-25 16:07:19 -07001897/* Called from receive_Data.
1898 * Synchronize packets on sock with packets on msock.
1899 *
1900 * This is here so even when a P_DATA packet traveling via sock overtook an Ack
1901 * packet traveling on msock, they are still processed in the order they have
1902 * been sent.
1903 *
1904 * Note: we don't care for Ack packets overtaking P_DATA packets.
1905 *
1906 * In case packet_seq is larger than mdev->peer_seq number, there are
1907 * outstanding packets on the msock. We wait for them to arrive.
1908 * In case we are the logically next packet, we update mdev->peer_seq
1909 * ourselves. Correctly handles 32bit wrap around.
1910 *
1911 * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
1912 * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
1913 * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
1914 * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
1915 *
1916 * returns 0 if we may process the packet,
1917 * -ERESTARTSYS if we were interrupted (by disconnect signal). */
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001918static int wait_for_and_update_peer_seq(struct drbd_conf *mdev, const u32 peer_seq)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001919{
1920 DEFINE_WAIT(wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001921 long timeout;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001922 int ret;
1923
1924 if (!need_peer_seq(mdev))
1925 return 0;
1926
Philipp Reisnerb411b362009-09-25 16:07:19 -07001927 spin_lock(&mdev->peer_seq_lock);
1928 for (;;) {
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001929 if (!seq_greater(peer_seq - 1, mdev->peer_seq)) {
1930 mdev->peer_seq = seq_max(mdev->peer_seq, peer_seq);
1931 ret = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001932 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001933 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001934 if (signal_pending(current)) {
1935 ret = -ERESTARTSYS;
1936 break;
1937 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001938 prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001939 spin_unlock(&mdev->peer_seq_lock);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001940 rcu_read_lock();
1941 timeout = rcu_dereference(mdev->tconn->net_conf)->ping_timeo*HZ/10;
1942 rcu_read_unlock();
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001943 timeout = schedule_timeout(timeout);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001944 spin_lock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001945 if (!timeout) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001946 ret = -ETIMEDOUT;
Andreas Gruenbacher71b1c1e2011-03-01 15:40:43 +01001947 dev_err(DEV, "Timed out waiting for missing ack packets; disconnecting\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001948 break;
1949 }
1950 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001951 spin_unlock(&mdev->peer_seq_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001952 finish_wait(&mdev->seq_wait, &wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001953 return ret;
1954}
1955
Lars Ellenberg688593c2010-11-17 22:25:03 +01001956/* see also bio_flags_to_wire()
1957 * DRBD_REQ_*, because we need to semantically map the flags to data packet
1958 * flags and back. We may replicate to other kernel versions. */
1959static unsigned long wire_flags_to_bio(struct drbd_conf *mdev, u32 dpf)
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001960{
Lars Ellenberg688593c2010-11-17 22:25:03 +01001961 return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
1962 (dpf & DP_FUA ? REQ_FUA : 0) |
1963 (dpf & DP_FLUSH ? REQ_FLUSH : 0) |
1964 (dpf & DP_DISCARD ? REQ_DISCARD : 0);
Philipp Reisner76d2e7e2010-08-25 11:58:05 +02001965}
1966
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01001967static void fail_postponed_requests(struct drbd_conf *mdev, sector_t sector,
1968 unsigned int size)
1969{
1970 struct drbd_interval *i;
1971
1972 repeat:
1973 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
1974 struct drbd_request *req;
1975 struct bio_and_error m;
1976
1977 if (!i->local)
1978 continue;
1979 req = container_of(i, struct drbd_request, i);
1980 if (!(req->rq_state & RQ_POSTPONED))
1981 continue;
1982 req->rq_state &= ~RQ_POSTPONED;
1983 __req_mod(req, NEG_ACKED, &m);
1984 spin_unlock_irq(&mdev->tconn->req_lock);
1985 if (m.bio)
1986 complete_master_bio(mdev, &m);
1987 spin_lock_irq(&mdev->tconn->req_lock);
1988 goto repeat;
1989 }
1990}
1991
1992static int handle_write_conflicts(struct drbd_conf *mdev,
1993 struct drbd_peer_request *peer_req)
1994{
1995 struct drbd_tconn *tconn = mdev->tconn;
1996 bool resolve_conflicts = test_bit(DISCARD_CONCURRENT, &tconn->flags);
1997 sector_t sector = peer_req->i.sector;
1998 const unsigned int size = peer_req->i.size;
1999 struct drbd_interval *i;
2000 bool equal;
2001 int err;
2002
2003 /*
2004 * Inserting the peer request into the write_requests tree will prevent
2005 * new conflicting local requests from being added.
2006 */
2007 drbd_insert_interval(&mdev->write_requests, &peer_req->i);
2008
2009 repeat:
2010 drbd_for_each_overlap(i, &mdev->write_requests, sector, size) {
2011 if (i == &peer_req->i)
2012 continue;
2013
2014 if (!i->local) {
2015 /*
2016 * Our peer has sent a conflicting remote request; this
2017 * should not happen in a two-node setup. Wait for the
2018 * earlier peer request to complete.
2019 */
2020 err = drbd_wait_misc(mdev, i);
2021 if (err)
2022 goto out;
2023 goto repeat;
2024 }
2025
2026 equal = i->sector == sector && i->size == size;
2027 if (resolve_conflicts) {
2028 /*
2029 * If the peer request is fully contained within the
2030 * overlapping request, it can be discarded; otherwise,
2031 * it will be retried once all overlapping requests
2032 * have completed.
2033 */
2034 bool discard = i->sector <= sector && i->sector +
2035 (i->size >> 9) >= sector + (size >> 9);
2036
2037 if (!equal)
2038 dev_alert(DEV, "Concurrent writes detected: "
2039 "local=%llus +%u, remote=%llus +%u, "
2040 "assuming %s came first\n",
2041 (unsigned long long)i->sector, i->size,
2042 (unsigned long long)sector, size,
2043 discard ? "local" : "remote");
2044
2045 inc_unacked(mdev);
2046 peer_req->w.cb = discard ? e_send_discard_write :
2047 e_send_retry_write;
2048 list_add_tail(&peer_req->w.list, &mdev->done_ee);
2049 wake_asender(mdev->tconn);
2050
2051 err = -ENOENT;
2052 goto out;
2053 } else {
2054 struct drbd_request *req =
2055 container_of(i, struct drbd_request, i);
2056
2057 if (!equal)
2058 dev_alert(DEV, "Concurrent writes detected: "
2059 "local=%llus +%u, remote=%llus +%u\n",
2060 (unsigned long long)i->sector, i->size,
2061 (unsigned long long)sector, size);
2062
2063 if (req->rq_state & RQ_LOCAL_PENDING ||
2064 !(req->rq_state & RQ_POSTPONED)) {
2065 /*
2066 * Wait for the node with the discard flag to
2067 * decide if this request will be discarded or
2068 * retried. Requests that are discarded will
2069 * disappear from the write_requests tree.
2070 *
2071 * In addition, wait for the conflicting
2072 * request to finish locally before submitting
2073 * the conflicting peer request.
2074 */
2075 err = drbd_wait_misc(mdev, &req->i);
2076 if (err) {
2077 _conn_request_state(mdev->tconn,
2078 NS(conn, C_TIMEOUT),
2079 CS_HARD);
2080 fail_postponed_requests(mdev, sector, size);
2081 goto out;
2082 }
2083 goto repeat;
2084 }
2085 /*
2086 * Remember to restart the conflicting requests after
2087 * the new peer request has completed.
2088 */
2089 peer_req->flags |= EE_RESTART_REQUESTS;
2090 }
2091 }
2092 err = 0;
2093
2094 out:
2095 if (err)
2096 drbd_remove_epoch_entry_interval(mdev, peer_req);
2097 return err;
2098}
2099
Philipp Reisnerb411b362009-09-25 16:07:19 -07002100/* mirrored write */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002101static int receive_Data(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002102{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002103 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002104 sector_t sector;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002105 struct drbd_peer_request *peer_req;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002106 struct p_data *p = pi->data;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002107 u32 peer_seq = be32_to_cpu(p->seq_num);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002108 int rw = WRITE;
2109 u32 dp_flags;
Philipp Reisner302bdea2011-04-21 11:36:49 +02002110 int err, tp;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002111
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002112 mdev = vnr_to_mdev(tconn, pi->vnr);
2113 if (!mdev)
2114 return -EIO;
2115
Philipp Reisnerb411b362009-09-25 16:07:19 -07002116 if (!get_ldev(mdev)) {
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002117 int err2;
2118
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002119 err = wait_for_and_update_peer_seq(mdev, peer_seq);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002120 drbd_send_ack_dp(mdev, P_NEG_ACK, p, pi->size);
Philipp Reisner12038a32011-11-09 19:18:00 +01002121 atomic_inc(&tconn->current_epoch->epoch_size);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002122 err2 = drbd_drain_block(mdev, pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002123 if (!err)
2124 err = err2;
2125 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002126 }
2127
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +01002128 /*
2129 * Corresponding put_ldev done either below (on various errors), or in
2130 * drbd_peer_request_endio, if we successfully submit the data at the
2131 * end of this function.
2132 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002133
2134 sector = be64_to_cpu(p->sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002135 peer_req = read_in_block(mdev, p->block_id, sector, pi->size);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002136 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002137 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002138 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002139 }
2140
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002141 peer_req->w.cb = e_end_block;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002142
Lars Ellenberg688593c2010-11-17 22:25:03 +01002143 dp_flags = be32_to_cpu(p->dp_flags);
2144 rw |= wire_flags_to_bio(mdev, dp_flags);
2145
2146 if (dp_flags & DP_MAY_SET_IN_SYNC)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002147 peer_req->flags |= EE_MAY_SET_IN_SYNC;
Lars Ellenberg688593c2010-11-17 22:25:03 +01002148
Philipp Reisner12038a32011-11-09 19:18:00 +01002149 spin_lock(&tconn->epoch_lock);
2150 peer_req->epoch = tconn->current_epoch;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002151 atomic_inc(&peer_req->epoch->epoch_size);
2152 atomic_inc(&peer_req->epoch->active);
Philipp Reisner12038a32011-11-09 19:18:00 +01002153 spin_unlock(&tconn->epoch_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002154
Philipp Reisner302bdea2011-04-21 11:36:49 +02002155 rcu_read_lock();
2156 tp = rcu_dereference(mdev->tconn->net_conf)->two_primaries;
2157 rcu_read_unlock();
2158 if (tp) {
2159 peer_req->flags |= EE_IN_INTERVAL_TREE;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002160 err = wait_for_and_update_peer_seq(mdev, peer_seq);
2161 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002162 goto out_interrupted;
Philipp Reisner87eeee42011-01-19 14:16:30 +01002163 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002164 err = handle_write_conflicts(mdev, peer_req);
2165 if (err) {
2166 spin_unlock_irq(&mdev->tconn->req_lock);
2167 if (err == -ENOENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002168 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002169 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002170 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002171 goto out_interrupted;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002172 }
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01002173 } else
2174 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002175 list_add(&peer_req->w.list, &mdev->active_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002176 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002177
Philipp Reisner303d1442011-04-13 16:24:47 -07002178 if (mdev->tconn->agreed_pro_version < 100) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02002179 rcu_read_lock();
2180 switch (rcu_dereference(mdev->tconn->net_conf)->wire_protocol) {
Philipp Reisner303d1442011-04-13 16:24:47 -07002181 case DRBD_PROT_C:
2182 dp_flags |= DP_SEND_WRITE_ACK;
2183 break;
2184 case DRBD_PROT_B:
2185 dp_flags |= DP_SEND_RECEIVE_ACK;
2186 break;
2187 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002188 rcu_read_unlock();
Philipp Reisner303d1442011-04-13 16:24:47 -07002189 }
2190
2191 if (dp_flags & DP_SEND_WRITE_ACK) {
2192 peer_req->flags |= EE_SEND_WRITE_ACK;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002193 inc_unacked(mdev);
2194 /* corresponding dec_unacked() in e_end_block()
2195 * respective _drbd_clear_done_ee */
Philipp Reisner303d1442011-04-13 16:24:47 -07002196 }
2197
2198 if (dp_flags & DP_SEND_RECEIVE_ACK) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002199 /* I really don't like it that the receiver thread
2200 * sends on the msock, but anyways */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002201 drbd_send_ack(mdev, P_RECV_ACK, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002202 }
2203
Lars Ellenberg6719fb02010-10-18 23:04:07 +02002204 if (mdev->state.pdsk < D_INCONSISTENT) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002205 /* In case we have the only disk of the cluster, */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002206 drbd_set_out_of_sync(mdev, peer_req->i.sector, peer_req->i.size);
2207 peer_req->flags |= EE_CALL_AL_COMPLETE_IO;
2208 peer_req->flags &= ~EE_MAY_SET_IN_SYNC;
Lars Ellenberg181286a2011-03-31 15:18:56 +02002209 drbd_al_begin_io(mdev, &peer_req->i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002210 }
2211
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002212 err = drbd_submit_peer_request(mdev, peer_req, rw, DRBD_FAULT_DT_WR);
2213 if (!err)
2214 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002215
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002216 /* don't care for the reason here */
2217 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002218 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002219 list_del(&peer_req->w.list);
2220 drbd_remove_epoch_entry_interval(mdev, peer_req);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002221 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002222 if (peer_req->flags & EE_CALL_AL_COMPLETE_IO)
Lars Ellenberg181286a2011-03-31 15:18:56 +02002223 drbd_al_complete_io(mdev, &peer_req->i);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002224
Philipp Reisnerb411b362009-09-25 16:07:19 -07002225out_interrupted:
Philipp Reisner1e9dd292011-11-10 15:14:53 +01002226 drbd_may_finish_epoch(tconn, peer_req->epoch, EV_PUT + EV_CLEANUP);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002227 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002228 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002229 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002230}
2231
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002232/* We may throttle resync, if the lower device seems to be busy,
2233 * and current sync rate is above c_min_rate.
2234 *
2235 * To decide whether or not the lower device is busy, we use a scheme similar
2236 * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
2237 * (more than 64 sectors) of activity we cannot account for with our own resync
2238 * activity, it obviously is "busy".
2239 *
2240 * The current sync rate used here uses only the most recent two step marks,
2241 * to have a short time average so we can react faster.
2242 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002243int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002244{
2245 struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
2246 unsigned long db, dt, dbdt;
Philipp Reisnere3555d82010-11-07 15:56:29 +01002247 struct lc_element *tmp;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002248 int curr_events;
2249 int throttle = 0;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002250 unsigned int c_min_rate;
2251
2252 rcu_read_lock();
2253 c_min_rate = rcu_dereference(mdev->ldev->disk_conf)->c_min_rate;
2254 rcu_read_unlock();
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002255
2256 /* feature disabled? */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002257 if (c_min_rate == 0)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002258 return 0;
2259
Philipp Reisnere3555d82010-11-07 15:56:29 +01002260 spin_lock_irq(&mdev->al_lock);
2261 tmp = lc_find(mdev->resync, BM_SECT_TO_EXT(sector));
2262 if (tmp) {
2263 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
2264 if (test_bit(BME_PRIORITY, &bm_ext->flags)) {
2265 spin_unlock_irq(&mdev->al_lock);
2266 return 0;
2267 }
2268 /* Do not slow down if app IO is already waiting for this extent */
2269 }
2270 spin_unlock_irq(&mdev->al_lock);
2271
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002272 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
2273 (int)part_stat_read(&disk->part0, sectors[1]) -
2274 atomic_read(&mdev->rs_sect_ev);
Philipp Reisnere3555d82010-11-07 15:56:29 +01002275
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002276 if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
2277 unsigned long rs_left;
2278 int i;
2279
2280 mdev->rs_last_events = curr_events;
2281
2282 /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
2283 * approx. */
Lars Ellenberg2649f082010-11-05 10:05:47 +01002284 i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
2285
2286 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
2287 rs_left = mdev->ov_left;
2288 else
2289 rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002290
2291 dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
2292 if (!dt)
2293 dt++;
2294 db = mdev->rs_mark_left[i] - rs_left;
2295 dbdt = Bit2KB(db/dt);
2296
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02002297 if (dbdt > c_min_rate)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002298 throttle = 1;
2299 }
2300 return throttle;
2301}
2302
2303
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002304static int receive_DataRequest(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002305{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002306 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002307 sector_t sector;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002308 sector_t capacity;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002309 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002310 struct digest_info *di = NULL;
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002311 int size, verb;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002312 unsigned int fault_type;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02002313 struct p_block_req *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01002314
2315 mdev = vnr_to_mdev(tconn, pi->vnr);
2316 if (!mdev)
2317 return -EIO;
2318 capacity = drbd_get_capacity(mdev->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002319
2320 sector = be64_to_cpu(p->sector);
2321 size = be32_to_cpu(p->blksize);
2322
Andreas Gruenbacherc670a392011-02-21 12:41:39 +01002323 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002324 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2325 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002326 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002327 }
2328 if (sector + (size>>9) > capacity) {
2329 dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
2330 (unsigned long long)sector, size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002331 return -EINVAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002332 }
2333
2334 if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002335 verb = 1;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002336 switch (pi->cmd) {
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002337 case P_DATA_REQUEST:
2338 drbd_send_ack_rp(mdev, P_NEG_DREPLY, p);
2339 break;
2340 case P_RS_DATA_REQUEST:
2341 case P_CSUM_RS_REQUEST:
2342 case P_OV_REQUEST:
2343 drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p);
2344 break;
2345 case P_OV_REPLY:
2346 verb = 0;
2347 dec_rs_pending(mdev);
2348 drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC);
2349 break;
2350 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002351 BUG();
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002352 }
2353 if (verb && __ratelimit(&drbd_ratelimit_state))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002354 dev_err(DEV, "Can not satisfy peer's read request, "
2355 "no local data.\n");
Philipp Reisnerb18b37b2010-10-13 15:32:44 +02002356
Lars Ellenberga821cc42010-09-06 12:31:37 +02002357 /* drain possibly payload */
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002358 return drbd_drain_block(mdev, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002359 }
2360
2361 /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
2362 * "criss-cross" setup, that might cause write-out on some other DRBD,
2363 * which in turn might block on the other node at this very place. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +02002364 peer_req = drbd_alloc_peer_req(mdev, p->block_id, sector, size, GFP_NOIO);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002365 if (!peer_req) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002366 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002367 return -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002368 }
2369
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002370 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002371 case P_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002372 peer_req->w.cb = w_e_end_data_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002373 fault_type = DRBD_FAULT_DT_RD;
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002374 /* application IO, don't drbd_rs_begin_io */
2375 goto submit;
2376
Philipp Reisnerb411b362009-09-25 16:07:19 -07002377 case P_RS_DATA_REQUEST:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002378 peer_req->w.cb = w_e_end_rsdata_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002379 fault_type = DRBD_FAULT_RS_RD;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002380 /* used in the sector offset progress display */
2381 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002382 break;
2383
2384 case P_OV_REPLY:
2385 case P_CSUM_RS_REQUEST:
2386 fault_type = DRBD_FAULT_RS_RD;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002387 di = kmalloc(sizeof(*di) + pi->size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002388 if (!di)
2389 goto out_free_e;
2390
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002391 di->digest_size = pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002392 di->digest = (((char *)di)+sizeof(struct digest_info));
2393
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002394 peer_req->digest = di;
2395 peer_req->flags |= EE_HAS_DIGEST;
Lars Ellenbergc36c3ce2010-08-11 20:42:55 +02002396
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002397 if (drbd_recv_all(mdev->tconn, di->digest, pi->size))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002398 goto out_free_e;
2399
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002400 if (pi->cmd == P_CSUM_RS_REQUEST) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002401 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002402 peer_req->w.cb = w_e_end_csum_rs_req;
Lars Ellenberg5f9915b2010-11-09 14:15:24 +01002403 /* used in the sector offset progress display */
2404 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01002405 } else if (pi->cmd == P_OV_REPLY) {
Lars Ellenberg2649f082010-11-05 10:05:47 +01002406 /* track progress, we may need to throttle */
2407 atomic_add(size >> 9, &mdev->rs_sect_in);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002408 peer_req->w.cb = w_e_end_ov_reply;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002409 dec_rs_pending(mdev);
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002410 /* drbd_rs_begin_io done when we sent this request,
2411 * but accounting still needs to be done. */
2412 goto submit_for_resync;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002413 }
2414 break;
2415
2416 case P_OV_REQUEST:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002417 if (mdev->ov_start_sector == ~(sector_t)0 &&
Philipp Reisner31890f42011-01-19 14:12:51 +01002418 mdev->tconn->agreed_pro_version >= 90) {
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002419 unsigned long now = jiffies;
2420 int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002421 mdev->ov_start_sector = sector;
2422 mdev->ov_position = sector;
Lars Ellenberg30b743a2010-11-05 09:39:06 +01002423 mdev->ov_left = drbd_bm_bits(mdev) - BM_SECT_TO_BIT(sector);
2424 mdev->rs_total = mdev->ov_left;
Lars Ellenbergde228bb2010-11-05 09:43:15 +01002425 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
2426 mdev->rs_mark_left[i] = mdev->ov_left;
2427 mdev->rs_mark_time[i] = now;
2428 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002429 dev_info(DEV, "Online Verify start sector: %llu\n",
2430 (unsigned long long)sector);
2431 }
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002432 peer_req->w.cb = w_e_end_ov_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002433 fault_type = DRBD_FAULT_RS_RD;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002434 break;
2435
Philipp Reisnerb411b362009-09-25 16:07:19 -07002436 default:
Andreas Gruenbacher49ba9b12011-03-25 00:35:45 +01002437 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002438 }
2439
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002440 /* Throttle, drbd_rs_begin_io and submit should become asynchronous
2441 * wrt the receiver, but it is not as straightforward as it may seem.
2442 * Various places in the resync start and stop logic assume resync
2443 * requests are processed in order, requeuing this on the worker thread
2444 * introduces a bunch of new code for synchronization between threads.
2445 *
2446 * Unlimited throttling before drbd_rs_begin_io may stall the resync
2447 * "forever", throttling after drbd_rs_begin_io will lock that extent
2448 * for application writes for the same time. For now, just throttle
2449 * here, where the rest of the code expects the receiver to sleep for
2450 * a while, anyways.
2451 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07002452
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002453 /* Throttle before drbd_rs_begin_io, as that locks out application IO;
2454 * this defers syncer requests for some time, before letting at least
2455 * on request through. The resync controller on the receiving side
2456 * will adapt to the incoming rate accordingly.
2457 *
2458 * We cannot throttle here if remote is Primary/SyncTarget:
2459 * we would also throttle its application reads.
2460 * In that case, throttling is done on the SyncTarget only.
2461 */
Philipp Reisnere3555d82010-11-07 15:56:29 +01002462 if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev, sector))
2463 schedule_timeout_uninterruptible(HZ/10);
2464 if (drbd_rs_begin_io(mdev, sector))
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002465 goto out_free_e;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002466
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02002467submit_for_resync:
2468 atomic_add(size >> 9, &mdev->rs_sect_ev);
2469
Lars Ellenberg80a40e42010-08-11 23:28:00 +02002470submit:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002471 inc_unacked(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002472 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002473 list_add_tail(&peer_req->w.list, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002474 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002475
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +01002476 if (drbd_submit_peer_request(mdev, peer_req, READ, fault_type) == 0)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002477 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002478
Lars Ellenberg10f6d9922011-01-24 14:47:09 +01002479 /* don't care for the reason here */
2480 dev_err(DEV, "submit failed, triggering re-connect\n");
Philipp Reisner87eeee42011-01-19 14:16:30 +01002481 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01002482 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +01002483 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +02002484 /* no drbd_rs_complete_io(), we are dropping the connection anyways */
2485
Philipp Reisnerb411b362009-09-25 16:07:19 -07002486out_free_e:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002487 put_ldev(mdev);
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02002488 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01002489 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002490}
2491
2492static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
2493{
2494 int self, peer, rv = -100;
2495 unsigned long ch_self, ch_peer;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002496 enum drbd_after_sb_p after_sb_0p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002497
2498 self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
2499 peer = mdev->p_uuid[UI_BITMAP] & 1;
2500
2501 ch_peer = mdev->p_uuid[UI_SIZE];
2502 ch_self = mdev->comm_bm_set;
2503
Philipp Reisner44ed1672011-04-19 17:10:19 +02002504 rcu_read_lock();
2505 after_sb_0p = rcu_dereference(mdev->tconn->net_conf)->after_sb_0p;
2506 rcu_read_unlock();
2507 switch (after_sb_0p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002508 case ASB_CONSENSUS:
2509 case ASB_DISCARD_SECONDARY:
2510 case ASB_CALL_HELPER:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002511 case ASB_VIOLENTLY:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002512 dev_err(DEV, "Configuration error.\n");
2513 break;
2514 case ASB_DISCONNECT:
2515 break;
2516 case ASB_DISCARD_YOUNGER_PRI:
2517 if (self == 0 && peer == 1) {
2518 rv = -1;
2519 break;
2520 }
2521 if (self == 1 && peer == 0) {
2522 rv = 1;
2523 break;
2524 }
2525 /* Else fall through to one of the other strategies... */
2526 case ASB_DISCARD_OLDER_PRI:
2527 if (self == 0 && peer == 1) {
2528 rv = 1;
2529 break;
2530 }
2531 if (self == 1 && peer == 0) {
2532 rv = -1;
2533 break;
2534 }
2535 /* Else fall through to one of the other strategies... */
Lars Ellenbergad19bf62009-10-14 09:36:49 +02002536 dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
Philipp Reisnerb411b362009-09-25 16:07:19 -07002537 "Using discard-least-changes instead\n");
2538 case ASB_DISCARD_ZERO_CHG:
2539 if (ch_peer == 0 && ch_self == 0) {
Philipp Reisner25703f82011-02-07 14:35:25 +01002540 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002541 ? -1 : 1;
2542 break;
2543 } else {
2544 if (ch_peer == 0) { rv = 1; break; }
2545 if (ch_self == 0) { rv = -1; break; }
2546 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002547 if (after_sb_0p == ASB_DISCARD_ZERO_CHG)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002548 break;
2549 case ASB_DISCARD_LEAST_CHG:
2550 if (ch_self < ch_peer)
2551 rv = -1;
2552 else if (ch_self > ch_peer)
2553 rv = 1;
2554 else /* ( ch_self == ch_peer ) */
2555 /* Well, then use something else. */
Philipp Reisner25703f82011-02-07 14:35:25 +01002556 rv = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002557 ? -1 : 1;
2558 break;
2559 case ASB_DISCARD_LOCAL:
2560 rv = -1;
2561 break;
2562 case ASB_DISCARD_REMOTE:
2563 rv = 1;
2564 }
2565
2566 return rv;
2567}
2568
2569static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
2570{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002571 int hg, rv = -100;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002572 enum drbd_after_sb_p after_sb_1p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002573
Philipp Reisner44ed1672011-04-19 17:10:19 +02002574 rcu_read_lock();
2575 after_sb_1p = rcu_dereference(mdev->tconn->net_conf)->after_sb_1p;
2576 rcu_read_unlock();
2577 switch (after_sb_1p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002578 case ASB_DISCARD_YOUNGER_PRI:
2579 case ASB_DISCARD_OLDER_PRI:
2580 case ASB_DISCARD_LEAST_CHG:
2581 case ASB_DISCARD_LOCAL:
2582 case ASB_DISCARD_REMOTE:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002583 case ASB_DISCARD_ZERO_CHG:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002584 dev_err(DEV, "Configuration error.\n");
2585 break;
2586 case ASB_DISCONNECT:
2587 break;
2588 case ASB_CONSENSUS:
2589 hg = drbd_asb_recover_0p(mdev);
2590 if (hg == -1 && mdev->state.role == R_SECONDARY)
2591 rv = hg;
2592 if (hg == 1 && mdev->state.role == R_PRIMARY)
2593 rv = hg;
2594 break;
2595 case ASB_VIOLENTLY:
2596 rv = drbd_asb_recover_0p(mdev);
2597 break;
2598 case ASB_DISCARD_SECONDARY:
2599 return mdev->state.role == R_PRIMARY ? 1 : -1;
2600 case ASB_CALL_HELPER:
2601 hg = drbd_asb_recover_0p(mdev);
2602 if (hg == -1 && mdev->state.role == R_PRIMARY) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002603 enum drbd_state_rv rv2;
2604
2605 drbd_set_role(mdev, R_SECONDARY, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002606 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2607 * we might be here in C_WF_REPORT_PARAMS which is transient.
2608 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002609 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2610 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002611 drbd_khelper(mdev, "pri-lost-after-sb");
2612 } else {
2613 dev_warn(DEV, "Successfully gave up primary role.\n");
2614 rv = hg;
2615 }
2616 } else
2617 rv = hg;
2618 }
2619
2620 return rv;
2621}
2622
2623static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
2624{
Andreas Gruenbacher6184ea22010-12-09 14:23:27 +01002625 int hg, rv = -100;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002626 enum drbd_after_sb_p after_sb_2p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002627
Philipp Reisner44ed1672011-04-19 17:10:19 +02002628 rcu_read_lock();
2629 after_sb_2p = rcu_dereference(mdev->tconn->net_conf)->after_sb_2p;
2630 rcu_read_unlock();
2631 switch (after_sb_2p) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002632 case ASB_DISCARD_YOUNGER_PRI:
2633 case ASB_DISCARD_OLDER_PRI:
2634 case ASB_DISCARD_LEAST_CHG:
2635 case ASB_DISCARD_LOCAL:
2636 case ASB_DISCARD_REMOTE:
2637 case ASB_CONSENSUS:
2638 case ASB_DISCARD_SECONDARY:
Philipp Reisner44ed1672011-04-19 17:10:19 +02002639 case ASB_DISCARD_ZERO_CHG:
Philipp Reisnerb411b362009-09-25 16:07:19 -07002640 dev_err(DEV, "Configuration error.\n");
2641 break;
2642 case ASB_VIOLENTLY:
2643 rv = drbd_asb_recover_0p(mdev);
2644 break;
2645 case ASB_DISCONNECT:
2646 break;
2647 case ASB_CALL_HELPER:
2648 hg = drbd_asb_recover_0p(mdev);
2649 if (hg == -1) {
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002650 enum drbd_state_rv rv2;
2651
Philipp Reisnerb411b362009-09-25 16:07:19 -07002652 /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
2653 * we might be here in C_WF_REPORT_PARAMS which is transient.
2654 * we do not need to wait for the after state change work either. */
Andreas Gruenbacherbb437942010-12-09 14:02:35 +01002655 rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
2656 if (rv2 != SS_SUCCESS) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002657 drbd_khelper(mdev, "pri-lost-after-sb");
2658 } else {
2659 dev_warn(DEV, "Successfully gave up primary role.\n");
2660 rv = hg;
2661 }
2662 } else
2663 rv = hg;
2664 }
2665
2666 return rv;
2667}
2668
2669static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
2670 u64 bits, u64 flags)
2671{
2672 if (!uuid) {
2673 dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
2674 return;
2675 }
2676 dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
2677 text,
2678 (unsigned long long)uuid[UI_CURRENT],
2679 (unsigned long long)uuid[UI_BITMAP],
2680 (unsigned long long)uuid[UI_HISTORY_START],
2681 (unsigned long long)uuid[UI_HISTORY_END],
2682 (unsigned long long)bits,
2683 (unsigned long long)flags);
2684}
2685
2686/*
2687 100 after split brain try auto recover
2688 2 C_SYNC_SOURCE set BitMap
2689 1 C_SYNC_SOURCE use BitMap
2690 0 no Sync
2691 -1 C_SYNC_TARGET use BitMap
2692 -2 C_SYNC_TARGET set BitMap
2693 -100 after split brain, disconnect
2694-1000 unrelated data
Philipp Reisner4a23f262011-01-11 17:42:17 +01002695-1091 requires proto 91
2696-1096 requires proto 96
Philipp Reisnerb411b362009-09-25 16:07:19 -07002697 */
2698static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
2699{
2700 u64 self, peer;
2701 int i, j;
2702
2703 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2704 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2705
2706 *rule_nr = 10;
2707 if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
2708 return 0;
2709
2710 *rule_nr = 20;
2711 if ((self == UUID_JUST_CREATED || self == (u64)0) &&
2712 peer != UUID_JUST_CREATED)
2713 return -2;
2714
2715 *rule_nr = 30;
2716 if (self != UUID_JUST_CREATED &&
2717 (peer == UUID_JUST_CREATED || peer == (u64)0))
2718 return 2;
2719
2720 if (self == peer) {
2721 int rct, dc; /* roles at crash time */
2722
2723 if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
2724
Philipp Reisner31890f42011-01-19 14:12:51 +01002725 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002726 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002727
2728 if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
2729 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
2730 dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
2731 drbd_uuid_set_bm(mdev, 0UL);
2732
2733 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2734 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2735 *rule_nr = 34;
2736 } else {
2737 dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
2738 *rule_nr = 36;
2739 }
2740
2741 return 1;
2742 }
2743
2744 if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
2745
Philipp Reisner31890f42011-01-19 14:12:51 +01002746 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002747 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002748
2749 if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
2750 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
2751 dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
2752
2753 mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
2754 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
2755 mdev->p_uuid[UI_BITMAP] = 0UL;
2756
2757 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2758 *rule_nr = 35;
2759 } else {
2760 dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
2761 *rule_nr = 37;
2762 }
2763
2764 return -1;
2765 }
2766
2767 /* Common power [off|failure] */
2768 rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
2769 (mdev->p_uuid[UI_FLAGS] & 2);
2770 /* lowest bit is set when we were primary,
2771 * next bit (weight 2) is set when peer was primary */
2772 *rule_nr = 40;
2773
2774 switch (rct) {
2775 case 0: /* !self_pri && !peer_pri */ return 0;
2776 case 1: /* self_pri && !peer_pri */ return 1;
2777 case 2: /* !self_pri && peer_pri */ return -1;
2778 case 3: /* self_pri && peer_pri */
Philipp Reisner25703f82011-02-07 14:35:25 +01002779 dc = test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002780 return dc ? -1 : 1;
2781 }
2782 }
2783
2784 *rule_nr = 50;
2785 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2786 if (self == peer)
2787 return -1;
2788
2789 *rule_nr = 51;
2790 peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
2791 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002792 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002793 (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) ==
2794 (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) :
2795 peer + UUID_NEW_BM_OFFSET == (mdev->p_uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002796 /* The last P_SYNC_UUID did not get though. Undo the last start of
2797 resync as sync source modifications of the peer's UUIDs. */
2798
Philipp Reisner31890f42011-01-19 14:12:51 +01002799 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002800 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002801
2802 mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
2803 mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
Philipp Reisner4a23f262011-01-11 17:42:17 +01002804
2805 dev_info(DEV, "Did not got last syncUUID packet, corrected:\n");
2806 drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2807
Philipp Reisnerb411b362009-09-25 16:07:19 -07002808 return -1;
2809 }
2810 }
2811
2812 *rule_nr = 60;
2813 self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
2814 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2815 peer = mdev->p_uuid[i] & ~((u64)1);
2816 if (self == peer)
2817 return -2;
2818 }
2819
2820 *rule_nr = 70;
2821 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2822 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
2823 if (self == peer)
2824 return 1;
2825
2826 *rule_nr = 71;
2827 self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
2828 if (self == peer) {
Philipp Reisner31890f42011-01-19 14:12:51 +01002829 if (mdev->tconn->agreed_pro_version < 96 ?
Philipp Reisner4a23f262011-01-11 17:42:17 +01002830 (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) ==
2831 (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) :
2832 self + UUID_NEW_BM_OFFSET == (mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002833 /* The last P_SYNC_UUID did not get though. Undo the last start of
2834 resync as sync source modifications of our UUIDs. */
2835
Philipp Reisner31890f42011-01-19 14:12:51 +01002836 if (mdev->tconn->agreed_pro_version < 91)
Philipp Reisner4a23f262011-01-11 17:42:17 +01002837 return -1091;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002838
2839 _drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
2840 _drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
2841
Philipp Reisner4a23f262011-01-11 17:42:17 +01002842 dev_info(DEV, "Last syncUUID did not get through, corrected:\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002843 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
2844 mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
2845
2846 return 1;
2847 }
2848 }
2849
2850
2851 *rule_nr = 80;
Philipp Reisnerd8c2a362009-11-18 15:52:51 +01002852 peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002853 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2854 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2855 if (self == peer)
2856 return 2;
2857 }
2858
2859 *rule_nr = 90;
2860 self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
2861 peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
2862 if (self == peer && self != ((u64)0))
2863 return 100;
2864
2865 *rule_nr = 100;
2866 for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
2867 self = mdev->ldev->md.uuid[i] & ~((u64)1);
2868 for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
2869 peer = mdev->p_uuid[j] & ~((u64)1);
2870 if (self == peer)
2871 return -100;
2872 }
2873 }
2874
2875 return -1000;
2876}
2877
2878/* drbd_sync_handshake() returns the new conn state on success, or
2879 CONN_MASK (-1) on failure.
2880 */
2881static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
2882 enum drbd_disk_state peer_disk) __must_hold(local)
2883{
Philipp Reisnerb411b362009-09-25 16:07:19 -07002884 enum drbd_conns rv = C_MASK;
2885 enum drbd_disk_state mydisk;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002886 struct net_conf *nc;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02002887 int hg, rule_nr, rr_conflict, tentative;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002888
2889 mydisk = mdev->state.disk;
2890 if (mydisk == D_NEGOTIATING)
2891 mydisk = mdev->new_state_tmp.disk;
2892
2893 dev_info(DEV, "drbd_sync_handshake:\n");
2894 drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0);
2895 drbd_uuid_dump(mdev, "peer", mdev->p_uuid,
2896 mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
2897
2898 hg = drbd_uuid_compare(mdev, &rule_nr);
2899
2900 dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
2901
2902 if (hg == -1000) {
2903 dev_alert(DEV, "Unrelated data, aborting!\n");
2904 return C_MASK;
2905 }
Philipp Reisner4a23f262011-01-11 17:42:17 +01002906 if (hg < -1000) {
2907 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 -07002908 return C_MASK;
2909 }
2910
2911 if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
2912 (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
2913 int f = (hg == -100) || abs(hg) == 2;
2914 hg = mydisk > D_INCONSISTENT ? 1 : -1;
2915 if (f)
2916 hg = hg*2;
2917 dev_info(DEV, "Becoming sync %s due to disk states.\n",
2918 hg > 0 ? "source" : "target");
2919 }
2920
Adam Gandelman3a11a482010-04-08 16:48:23 -07002921 if (abs(hg) == 100)
2922 drbd_khelper(mdev, "initial-split-brain");
2923
Philipp Reisner44ed1672011-04-19 17:10:19 +02002924 rcu_read_lock();
2925 nc = rcu_dereference(mdev->tconn->net_conf);
2926
2927 if (hg == 100 || (hg == -100 && nc->always_asbp)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002928 int pcount = (mdev->state.role == R_PRIMARY)
2929 + (peer_role == R_PRIMARY);
2930 int forced = (hg == -100);
2931
2932 switch (pcount) {
2933 case 0:
2934 hg = drbd_asb_recover_0p(mdev);
2935 break;
2936 case 1:
2937 hg = drbd_asb_recover_1p(mdev);
2938 break;
2939 case 2:
2940 hg = drbd_asb_recover_2p(mdev);
2941 break;
2942 }
2943 if (abs(hg) < 100) {
2944 dev_warn(DEV, "Split-Brain detected, %d primaries, "
2945 "automatically solved. Sync from %s node\n",
2946 pcount, (hg < 0) ? "peer" : "this");
2947 if (forced) {
2948 dev_warn(DEV, "Doing a full sync, since"
2949 " UUIDs where ambiguous.\n");
2950 hg = hg*2;
2951 }
2952 }
2953 }
2954
2955 if (hg == -100) {
Philipp Reisner08b165b2011-09-05 16:22:33 +02002956 if (test_bit(DISCARD_MY_DATA, &mdev->flags) && !(mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002957 hg = -1;
Philipp Reisner08b165b2011-09-05 16:22:33 +02002958 if (!test_bit(DISCARD_MY_DATA, &mdev->flags) && (mdev->p_uuid[UI_FLAGS]&1))
Philipp Reisnerb411b362009-09-25 16:07:19 -07002959 hg = 1;
2960
2961 if (abs(hg) < 100)
2962 dev_warn(DEV, "Split-Brain detected, manually solved. "
2963 "Sync from %s node\n",
2964 (hg < 0) ? "peer" : "this");
2965 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02002966 rr_conflict = nc->rr_conflict;
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02002967 tentative = nc->tentative;
Philipp Reisner44ed1672011-04-19 17:10:19 +02002968 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002969
2970 if (hg == -100) {
Lars Ellenberg580b9762010-02-26 23:15:23 +01002971 /* FIXME this log message is not correct if we end up here
2972 * after an attempted attach on a diskless node.
2973 * We just refuse to attach -- well, we drop the "connection"
2974 * to that disk, in a way... */
Adam Gandelman3a11a482010-04-08 16:48:23 -07002975 dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002976 drbd_khelper(mdev, "split-brain");
2977 return C_MASK;
2978 }
2979
2980 if (hg > 0 && mydisk <= D_INCONSISTENT) {
2981 dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
2982 return C_MASK;
2983 }
2984
2985 if (hg < 0 && /* by intention we do not use mydisk here. */
2986 mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
Philipp Reisner44ed1672011-04-19 17:10:19 +02002987 switch (rr_conflict) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002988 case ASB_CALL_HELPER:
2989 drbd_khelper(mdev, "pri-lost");
2990 /* fall through */
2991 case ASB_DISCONNECT:
2992 dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
2993 return C_MASK;
2994 case ASB_VIOLENTLY:
2995 dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
2996 "assumption\n");
2997 }
2998 }
2999
Andreas Gruenbacher6dff2902011-06-28 14:18:12 +02003000 if (tentative || test_bit(CONN_DRY_RUN, &mdev->tconn->flags)) {
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003001 if (hg == 0)
3002 dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
3003 else
3004 dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
3005 drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
3006 abs(hg) >= 2 ? "full" : "bit-map based");
3007 return C_MASK;
3008 }
3009
Philipp Reisnerb411b362009-09-25 16:07:19 -07003010 if (abs(hg) >= 2) {
3011 dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003012 if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake",
3013 BM_LOCKED_SET_ALLOWED))
Philipp Reisnerb411b362009-09-25 16:07:19 -07003014 return C_MASK;
3015 }
3016
3017 if (hg > 0) { /* become sync source. */
3018 rv = C_WF_BITMAP_S;
3019 } else if (hg < 0) { /* become sync target */
3020 rv = C_WF_BITMAP_T;
3021 } else {
3022 rv = C_CONNECTED;
3023 if (drbd_bm_total_weight(mdev)) {
3024 dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
3025 drbd_bm_total_weight(mdev));
3026 }
3027 }
3028
3029 return rv;
3030}
3031
Philipp Reisnerf179d762011-05-16 17:31:47 +02003032static enum drbd_after_sb_p convert_after_sb(enum drbd_after_sb_p peer)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003033{
3034 /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003035 if (peer == ASB_DISCARD_REMOTE)
3036 return ASB_DISCARD_LOCAL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003037
3038 /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003039 if (peer == ASB_DISCARD_LOCAL)
3040 return ASB_DISCARD_REMOTE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003041
3042 /* everything else is valid if they are equal on both sides. */
Philipp Reisnerf179d762011-05-16 17:31:47 +02003043 return peer;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003044}
3045
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003046static int receive_protocol(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003047{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003048 struct p_protocol *p = pi->data;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003049 enum drbd_after_sb_p p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
3050 int p_proto, p_discard_my_data, p_two_primaries, cf;
3051 struct net_conf *nc, *old_net_conf, *new_net_conf = NULL;
3052 char integrity_alg[SHARED_SECRET_MAX] = "";
Andreas Gruenbacheraccdbcc2011-07-15 17:41:09 +02003053 struct crypto_hash *peer_integrity_tfm = NULL;
Philipp Reisner7aca6c72011-05-17 10:12:56 +02003054 void *int_dig_in = NULL, *int_dig_vv = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003055
Philipp Reisnerb411b362009-09-25 16:07:19 -07003056 p_proto = be32_to_cpu(p->protocol);
3057 p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
3058 p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
3059 p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003060 p_two_primaries = be32_to_cpu(p->two_primaries);
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003061 cf = be32_to_cpu(p->conn_flags);
Andreas Gruenbacher6139f602011-05-06 20:00:02 +02003062 p_discard_my_data = cf & CF_DISCARD_MY_DATA;
Philipp Reisnercf14c2e2010-02-02 21:03:50 +01003063
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003064 if (tconn->agreed_pro_version >= 87) {
3065 int err;
3066
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003067 if (pi->size > sizeof(integrity_alg))
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003068 return -EIO;
Andreas Gruenbacher88104ca2011-04-28 21:47:21 +02003069 err = drbd_recv_all(tconn, integrity_alg, pi->size);
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003070 if (err)
3071 return err;
Philipp Reisner036b17e2011-05-16 17:38:11 +02003072 integrity_alg[SHARED_SECRET_MAX - 1] = 0;
3073 }
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003074
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003075 if (pi->cmd != P_PROTOCOL_UPDATE) {
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003076 clear_bit(CONN_DRY_RUN, &tconn->flags);
Philipp Reisner036b17e2011-05-16 17:38:11 +02003077
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003078 if (cf & CF_DRY_RUN)
3079 set_bit(CONN_DRY_RUN, &tconn->flags);
3080
3081 rcu_read_lock();
3082 nc = rcu_dereference(tconn->net_conf);
3083
3084 if (p_proto != nc->wire_protocol) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003085 conn_err(tconn, "incompatible %s settings\n", "protocol");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003086 goto disconnect_rcu_unlock;
3087 }
3088
3089 if (convert_after_sb(p_after_sb_0p) != nc->after_sb_0p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003090 conn_err(tconn, "incompatible %s settings\n", "after-sb-0pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003091 goto disconnect_rcu_unlock;
3092 }
3093
3094 if (convert_after_sb(p_after_sb_1p) != nc->after_sb_1p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003095 conn_err(tconn, "incompatible %s settings\n", "after-sb-1pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003096 goto disconnect_rcu_unlock;
3097 }
3098
3099 if (convert_after_sb(p_after_sb_2p) != nc->after_sb_2p) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003100 conn_err(tconn, "incompatible %s settings\n", "after-sb-2pri");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003101 goto disconnect_rcu_unlock;
3102 }
3103
3104 if (p_discard_my_data && nc->discard_my_data) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003105 conn_err(tconn, "incompatible %s settings\n", "discard-my-data");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003106 goto disconnect_rcu_unlock;
3107 }
3108
3109 if (p_two_primaries != nc->two_primaries) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003110 conn_err(tconn, "incompatible %s settings\n", "allow-two-primaries");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003111 goto disconnect_rcu_unlock;
3112 }
3113
3114 if (strcmp(integrity_alg, nc->integrity_alg)) {
Andreas Gruenbacherd505d9b2011-07-15 17:19:18 +02003115 conn_err(tconn, "incompatible %s settings\n", "data-integrity-alg");
Andreas Gruenbacherfbc12f42011-07-15 17:04:26 +02003116 goto disconnect_rcu_unlock;
3117 }
3118
3119 rcu_read_unlock();
Andreas Gruenbacher86db0612011-04-28 15:24:18 +02003120 }
Andreas Gruenbacher7d4c7822011-07-17 23:06:12 +02003121
3122 if (integrity_alg[0]) {
3123 int hash_size;
3124
3125 /*
3126 * We can only change the peer data integrity algorithm
3127 * here. Changing our own data integrity algorithm
3128 * requires that we send a P_PROTOCOL_UPDATE packet at
3129 * the same time; otherwise, the peer has no way to
3130 * tell between which packets the algorithm should
3131 * change.
3132 */
3133
3134 peer_integrity_tfm = crypto_alloc_hash(integrity_alg, 0, CRYPTO_ALG_ASYNC);
3135 if (!peer_integrity_tfm) {
3136 conn_err(tconn, "peer data-integrity-alg %s not supported\n",
3137 integrity_alg);
3138 goto disconnect;
3139 }
3140
3141 hash_size = crypto_hash_digestsize(peer_integrity_tfm);
3142 int_dig_in = kmalloc(hash_size, GFP_KERNEL);
3143 int_dig_vv = kmalloc(hash_size, GFP_KERNEL);
3144 if (!(int_dig_in && int_dig_vv)) {
3145 conn_err(tconn, "Allocation of buffers for data integrity checking failed\n");
3146 goto disconnect;
3147 }
3148 }
3149
3150 new_net_conf = kmalloc(sizeof(struct net_conf), GFP_KERNEL);
3151 if (!new_net_conf) {
3152 conn_err(tconn, "Allocation of new net_conf failed\n");
3153 goto disconnect;
3154 }
3155
3156 mutex_lock(&tconn->data.mutex);
3157 mutex_lock(&tconn->conf_update);
3158 old_net_conf = tconn->net_conf;
3159 *new_net_conf = *old_net_conf;
3160
3161 new_net_conf->wire_protocol = p_proto;
3162 new_net_conf->after_sb_0p = convert_after_sb(p_after_sb_0p);
3163 new_net_conf->after_sb_1p = convert_after_sb(p_after_sb_1p);
3164 new_net_conf->after_sb_2p = convert_after_sb(p_after_sb_2p);
3165 new_net_conf->two_primaries = p_two_primaries;
3166
3167 rcu_assign_pointer(tconn->net_conf, new_net_conf);
3168 mutex_unlock(&tconn->conf_update);
3169 mutex_unlock(&tconn->data.mutex);
3170
3171 crypto_free_hash(tconn->peer_integrity_tfm);
3172 kfree(tconn->int_dig_in);
3173 kfree(tconn->int_dig_vv);
3174 tconn->peer_integrity_tfm = peer_integrity_tfm;
3175 tconn->int_dig_in = int_dig_in;
3176 tconn->int_dig_vv = int_dig_vv;
3177
3178 if (strcmp(old_net_conf->integrity_alg, integrity_alg))
3179 conn_info(tconn, "peer data-integrity-alg: %s\n",
3180 integrity_alg[0] ? integrity_alg : "(none)");
3181
3182 synchronize_rcu();
3183 kfree(old_net_conf);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003184 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003185
Philipp Reisner44ed1672011-04-19 17:10:19 +02003186disconnect_rcu_unlock:
3187 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07003188disconnect:
Andreas Gruenbacherb792c352011-07-15 16:48:49 +02003189 crypto_free_hash(peer_integrity_tfm);
Philipp Reisner036b17e2011-05-16 17:38:11 +02003190 kfree(int_dig_in);
3191 kfree(int_dig_vv);
Philipp Reisner7204624c2011-03-15 18:51:47 +01003192 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003193 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003194}
3195
3196/* helper function
3197 * input: alg name, feature name
3198 * return: NULL (alg name was "")
3199 * ERR_PTR(error) if something goes wrong
3200 * or the crypto hash ptr, if it worked out ok. */
3201struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
3202 const char *alg, const char *name)
3203{
3204 struct crypto_hash *tfm;
3205
3206 if (!alg[0])
3207 return NULL;
3208
3209 tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
3210 if (IS_ERR(tfm)) {
3211 dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
3212 alg, name, PTR_ERR(tfm));
3213 return tfm;
3214 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003215 return tfm;
3216}
3217
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003218static int ignore_remaining_packet(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003219{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003220 void *buffer = tconn->data.rbuf;
3221 int size = pi->size;
3222
3223 while (size) {
3224 int s = min_t(int, size, DRBD_SOCKET_BUFFER_SIZE);
3225 s = drbd_recv(tconn, buffer, s);
3226 if (s <= 0) {
3227 if (s < 0)
3228 return s;
3229 break;
3230 }
3231 size -= s;
3232 }
3233 if (size)
3234 return -EIO;
3235 return 0;
3236}
3237
3238/*
3239 * config_unknown_volume - device configuration command for unknown volume
3240 *
3241 * When a device is added to an existing connection, the node on which the
3242 * device is added first will send configuration commands to its peer but the
3243 * peer will not know about the device yet. It will warn and ignore these
3244 * commands. Once the device is added on the second node, the second node will
3245 * send the same device configuration commands, but in the other direction.
3246 *
3247 * (We can also end up here if drbd is misconfigured.)
3248 */
3249static int config_unknown_volume(struct drbd_tconn *tconn, struct packet_info *pi)
3250{
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02003251 conn_warn(tconn, "%s packet received for volume %u, which is not configured locally\n",
3252 cmdname(pi->cmd), pi->vnr);
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003253 return ignore_remaining_packet(tconn, pi);
3254}
3255
3256static int receive_SyncParam(struct drbd_tconn *tconn, struct packet_info *pi)
3257{
3258 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003259 struct p_rs_param_95 *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003260 unsigned int header_size, data_size, exp_max_sz;
3261 struct crypto_hash *verify_tfm = NULL;
3262 struct crypto_hash *csums_tfm = NULL;
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003263 struct net_conf *old_net_conf, *new_net_conf = NULL;
Philipp Reisner813472c2011-05-03 16:47:02 +02003264 struct disk_conf *old_disk_conf = NULL, *new_disk_conf = NULL;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003265 const int apv = tconn->agreed_pro_version;
Philipp Reisner813472c2011-05-03 16:47:02 +02003266 struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
Philipp Reisner778f2712010-07-06 11:14:00 +02003267 int fifo_size = 0;
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003268 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003269
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003270 mdev = vnr_to_mdev(tconn, pi->vnr);
3271 if (!mdev)
3272 return config_unknown_volume(tconn, pi);
3273
Philipp Reisnerb411b362009-09-25 16:07:19 -07003274 exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
3275 : apv == 88 ? sizeof(struct p_rs_param)
3276 + SHARED_SECRET_MAX
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003277 : apv <= 94 ? sizeof(struct p_rs_param_89)
3278 : /* apv >= 95 */ sizeof(struct p_rs_param_95);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003279
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003280 if (pi->size > exp_max_sz) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003281 dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003282 pi->size, exp_max_sz);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003283 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003284 }
3285
3286 if (apv <= 88) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003287 header_size = sizeof(struct p_rs_param);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003288 data_size = pi->size - header_size;
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003289 } else if (apv <= 94) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003290 header_size = sizeof(struct p_rs_param_89);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003291 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003292 D_ASSERT(data_size == 0);
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003293 } else {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003294 header_size = sizeof(struct p_rs_param_95);
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003295 data_size = pi->size - header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003296 D_ASSERT(data_size == 0);
3297 }
3298
3299 /* initialize verify_alg and csums_alg */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003300 p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003301 memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
3302
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003303 err = drbd_recv_all(mdev->tconn, p, header_size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003304 if (err)
3305 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003306
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003307 mutex_lock(&mdev->tconn->conf_update);
3308 old_net_conf = mdev->tconn->net_conf;
Philipp Reisner813472c2011-05-03 16:47:02 +02003309 if (get_ldev(mdev)) {
3310 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3311 if (!new_disk_conf) {
3312 put_ldev(mdev);
3313 mutex_unlock(&mdev->tconn->conf_update);
3314 dev_err(DEV, "Allocation of new disk_conf failed\n");
3315 return -ENOMEM;
3316 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003317
Philipp Reisner813472c2011-05-03 16:47:02 +02003318 old_disk_conf = mdev->ldev->disk_conf;
3319 *new_disk_conf = *old_disk_conf;
3320
Andreas Gruenbacher6394b932011-05-11 14:29:52 +02003321 new_disk_conf->resync_rate = be32_to_cpu(p->resync_rate);
Philipp Reisner813472c2011-05-03 16:47:02 +02003322 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003323
Philipp Reisnerb411b362009-09-25 16:07:19 -07003324 if (apv >= 88) {
3325 if (apv == 88) {
3326 if (data_size > SHARED_SECRET_MAX) {
3327 dev_err(DEV, "verify-alg too long, "
3328 "peer wants %u, accepting only %u byte\n",
3329 data_size, SHARED_SECRET_MAX);
Philipp Reisner813472c2011-05-03 16:47:02 +02003330 err = -EIO;
3331 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003332 }
3333
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003334 err = drbd_recv_all(mdev->tconn, p->verify_alg, data_size);
Philipp Reisner813472c2011-05-03 16:47:02 +02003335 if (err)
3336 goto reconnect;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003337 /* we expect NUL terminated string */
3338 /* but just in case someone tries to be evil */
3339 D_ASSERT(p->verify_alg[data_size-1] == 0);
3340 p->verify_alg[data_size-1] = 0;
3341
3342 } else /* apv >= 89 */ {
3343 /* we still expect NUL terminated strings */
3344 /* but just in case someone tries to be evil */
3345 D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
3346 D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
3347 p->verify_alg[SHARED_SECRET_MAX-1] = 0;
3348 p->csums_alg[SHARED_SECRET_MAX-1] = 0;
3349 }
3350
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003351 if (strcmp(old_net_conf->verify_alg, p->verify_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003352 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3353 dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003354 old_net_conf->verify_alg, p->verify_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003355 goto disconnect;
3356 }
3357 verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
3358 p->verify_alg, "verify-alg");
3359 if (IS_ERR(verify_tfm)) {
3360 verify_tfm = NULL;
3361 goto disconnect;
3362 }
3363 }
3364
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003365 if (apv >= 89 && strcmp(old_net_conf->csums_alg, p->csums_alg)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003366 if (mdev->state.conn == C_WF_REPORT_PARAMS) {
3367 dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003368 old_net_conf->csums_alg, p->csums_alg);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003369 goto disconnect;
3370 }
3371 csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
3372 p->csums_alg, "csums-alg");
3373 if (IS_ERR(csums_tfm)) {
3374 csums_tfm = NULL;
3375 goto disconnect;
3376 }
3377 }
3378
Philipp Reisner813472c2011-05-03 16:47:02 +02003379 if (apv > 94 && new_disk_conf) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003380 new_disk_conf->c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
3381 new_disk_conf->c_delay_target = be32_to_cpu(p->c_delay_target);
3382 new_disk_conf->c_fill_target = be32_to_cpu(p->c_fill_target);
3383 new_disk_conf->c_max_rate = be32_to_cpu(p->c_max_rate);
Philipp Reisner778f2712010-07-06 11:14:00 +02003384
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003385 fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
Philipp Reisner9958c852011-05-03 16:19:31 +02003386 if (fifo_size != mdev->rs_plan_s->size) {
Philipp Reisner813472c2011-05-03 16:47:02 +02003387 new_plan = fifo_alloc(fifo_size);
3388 if (!new_plan) {
Philipp Reisner778f2712010-07-06 11:14:00 +02003389 dev_err(DEV, "kmalloc of fifo_buffer failed");
Lars Ellenbergf3990022011-03-23 14:31:09 +01003390 put_ldev(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02003391 goto disconnect;
3392 }
3393 }
Philipp Reisner8e26f9c2010-07-06 17:25:54 +02003394 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003395
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003396 if (verify_tfm || csums_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003397 new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
3398 if (!new_net_conf) {
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003399 dev_err(DEV, "Allocation of new net_conf failed\n");
3400 goto disconnect;
3401 }
3402
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003403 *new_net_conf = *old_net_conf;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003404
3405 if (verify_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003406 strcpy(new_net_conf->verify_alg, p->verify_alg);
3407 new_net_conf->verify_alg_len = strlen(p->verify_alg) + 1;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003408 crypto_free_hash(mdev->tconn->verify_tfm);
3409 mdev->tconn->verify_tfm = verify_tfm;
3410 dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
3411 }
3412 if (csums_tfm) {
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003413 strcpy(new_net_conf->csums_alg, p->csums_alg);
3414 new_net_conf->csums_alg_len = strlen(p->csums_alg) + 1;
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003415 crypto_free_hash(mdev->tconn->csums_tfm);
3416 mdev->tconn->csums_tfm = csums_tfm;
3417 dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
3418 }
Philipp Reisner2ec91e02011-05-03 14:58:00 +02003419 rcu_assign_pointer(tconn->net_conf, new_net_conf);
Philipp Reisner91fd4da2011-04-20 17:47:29 +02003420 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003421 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003422
Philipp Reisner813472c2011-05-03 16:47:02 +02003423 if (new_disk_conf) {
3424 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3425 put_ldev(mdev);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003426 }
Philipp Reisner813472c2011-05-03 16:47:02 +02003427
3428 if (new_plan) {
3429 old_plan = mdev->rs_plan_s;
3430 rcu_assign_pointer(mdev->rs_plan_s, new_plan);
3431 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003432
3433 mutex_unlock(&mdev->tconn->conf_update);
3434 synchronize_rcu();
3435 if (new_net_conf)
3436 kfree(old_net_conf);
3437 kfree(old_disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +02003438 kfree(old_plan);
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003439
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003440 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003441
Philipp Reisner813472c2011-05-03 16:47:02 +02003442reconnect:
3443 if (new_disk_conf) {
3444 put_ldev(mdev);
3445 kfree(new_disk_conf);
3446 }
3447 mutex_unlock(&mdev->tconn->conf_update);
3448 return -EIO;
3449
Philipp Reisnerb411b362009-09-25 16:07:19 -07003450disconnect:
Philipp Reisner813472c2011-05-03 16:47:02 +02003451 kfree(new_plan);
3452 if (new_disk_conf) {
3453 put_ldev(mdev);
3454 kfree(new_disk_conf);
3455 }
Philipp Reisnera0095502011-05-03 13:14:15 +02003456 mutex_unlock(&mdev->tconn->conf_update);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003457 /* just for completeness: actually not needed,
3458 * as this is not reached if csums_tfm was ok. */
3459 crypto_free_hash(csums_tfm);
3460 /* but free the verify_tfm again, if csums_tfm did not work out */
3461 crypto_free_hash(verify_tfm);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003462 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003463 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003464}
3465
Philipp Reisnerb411b362009-09-25 16:07:19 -07003466/* warn if the arguments differ by more than 12.5% */
3467static void warn_if_differ_considerably(struct drbd_conf *mdev,
3468 const char *s, sector_t a, sector_t b)
3469{
3470 sector_t d;
3471 if (a == 0 || b == 0)
3472 return;
3473 d = (a > b) ? (a - b) : (b - a);
3474 if (d > (a>>3) || d > (b>>3))
3475 dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
3476 (unsigned long long)a, (unsigned long long)b);
3477}
3478
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003479static int receive_sizes(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003480{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003481 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003482 struct p_sizes *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003483 enum determine_dev_size dd = unchanged;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003484 sector_t p_size, p_usize, my_usize;
3485 int ldsc = 0; /* local disk size changed */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003486 enum dds_flags ddsf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003487
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003488 mdev = vnr_to_mdev(tconn, pi->vnr);
3489 if (!mdev)
3490 return config_unknown_volume(tconn, pi);
3491
Philipp Reisnerb411b362009-09-25 16:07:19 -07003492 p_size = be64_to_cpu(p->d_size);
3493 p_usize = be64_to_cpu(p->u_size);
3494
Philipp Reisnerb411b362009-09-25 16:07:19 -07003495 /* just store the peer's disk size for now.
3496 * we still need to figure out whether we accept that. */
3497 mdev->p_size = p_size;
3498
Philipp Reisnerb411b362009-09-25 16:07:19 -07003499 if (get_ldev(mdev)) {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003500 rcu_read_lock();
3501 my_usize = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
3502 rcu_read_unlock();
3503
Philipp Reisnerb411b362009-09-25 16:07:19 -07003504 warn_if_differ_considerably(mdev, "lower level device sizes",
3505 p_size, drbd_get_max_capacity(mdev->ldev));
3506 warn_if_differ_considerably(mdev, "user requested size",
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003507 p_usize, my_usize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003508
3509 /* if this is the first connect, or an otherwise expected
3510 * param exchange, choose the minimum */
3511 if (mdev->state.conn == C_WF_REPORT_PARAMS)
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003512 p_usize = min_not_zero(my_usize, p_usize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003513
3514 /* Never shrink a device with usable data during connect.
3515 But allow online shrinking if we are connected. */
Philipp Reisneref5e44a2011-05-03 13:27:43 +02003516 if (drbd_new_dev_size(mdev, mdev->ldev, p_usize, 0) <
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003517 drbd_get_capacity(mdev->this_bdev) &&
3518 mdev->state.disk >= D_OUTDATED &&
3519 mdev->state.conn < C_CONNECTED) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003520 dev_err(DEV, "The peer's disk size is too small!\n");
Philipp Reisner38fa9982011-03-15 18:24:49 +01003521 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003522 put_ldev(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003523 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003524 }
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02003525
3526 if (my_usize != p_usize) {
3527 struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
3528
3529 new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
3530 if (!new_disk_conf) {
3531 dev_err(DEV, "Allocation of new disk_conf failed\n");
3532 put_ldev(mdev);
3533 return -ENOMEM;
3534 }
3535
3536 mutex_lock(&mdev->tconn->conf_update);
3537 old_disk_conf = mdev->ldev->disk_conf;
3538 *new_disk_conf = *old_disk_conf;
3539 new_disk_conf->disk_size = p_usize;
3540
3541 rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
3542 mutex_unlock(&mdev->tconn->conf_update);
3543 synchronize_rcu();
3544 kfree(old_disk_conf);
3545
3546 dev_info(DEV, "Peer sets u_size to %lu sectors\n",
3547 (unsigned long)my_usize);
3548 }
3549
Philipp Reisnerb411b362009-09-25 16:07:19 -07003550 put_ldev(mdev);
3551 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07003552
Philipp Reisnere89b5912010-03-24 17:11:33 +01003553 ddsf = be16_to_cpu(p->dds_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003554 if (get_ldev(mdev)) {
Bart Van Assche24c48302011-05-21 18:32:29 +02003555 dd = drbd_determine_dev_size(mdev, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003556 put_ldev(mdev);
3557 if (dd == dev_size_error)
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003558 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003559 drbd_md_sync(mdev);
3560 } else {
3561 /* I am diskless, need to accept the peer's size. */
3562 drbd_set_my_capacity(mdev, p_size);
3563 }
3564
Philipp Reisner99432fc2011-05-20 16:39:13 +02003565 mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size);
3566 drbd_reconsider_max_bio_size(mdev);
3567
Philipp Reisnerb411b362009-09-25 16:07:19 -07003568 if (get_ldev(mdev)) {
3569 if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
3570 mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
3571 ldsc = 1;
3572 }
3573
Philipp Reisnerb411b362009-09-25 16:07:19 -07003574 put_ldev(mdev);
3575 }
3576
3577 if (mdev->state.conn > C_WF_REPORT_PARAMS) {
3578 if (be64_to_cpu(p->c_size) !=
3579 drbd_get_capacity(mdev->this_bdev) || ldsc) {
3580 /* we have different sizes, probably peer
3581 * needs to know my new size... */
Philipp Reisnere89b5912010-03-24 17:11:33 +01003582 drbd_send_sizes(mdev, 0, ddsf);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003583 }
3584 if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
3585 (dd == grew && mdev->state.conn == C_CONNECTED)) {
3586 if (mdev->state.pdsk >= D_INCONSISTENT &&
Philipp Reisnere89b5912010-03-24 17:11:33 +01003587 mdev->state.disk >= D_INCONSISTENT) {
3588 if (ddsf & DDSF_NO_RESYNC)
3589 dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
3590 else
3591 resync_after_online_grow(mdev);
3592 } else
Philipp Reisnerb411b362009-09-25 16:07:19 -07003593 set_bit(RESYNC_AFTER_NEG, &mdev->flags);
3594 }
3595 }
3596
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003597 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003598}
3599
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003600static int receive_uuids(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003601{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003602 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003603 struct p_uuids *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003604 u64 *p_uuid;
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003605 int i, updated_uuids = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003606
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003607 mdev = vnr_to_mdev(tconn, pi->vnr);
3608 if (!mdev)
3609 return config_unknown_volume(tconn, pi);
3610
Philipp Reisnerb411b362009-09-25 16:07:19 -07003611 p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
3612
3613 for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
3614 p_uuid[i] = be64_to_cpu(p->uuid[i]);
3615
3616 kfree(mdev->p_uuid);
3617 mdev->p_uuid = p_uuid;
3618
3619 if (mdev->state.conn < C_CONNECTED &&
3620 mdev->state.disk < D_INCONSISTENT &&
3621 mdev->state.role == R_PRIMARY &&
3622 (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
3623 dev_err(DEV, "Can only connect to data with current UUID=%016llX\n",
3624 (unsigned long long)mdev->ed_uuid);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003625 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003626 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003627 }
3628
3629 if (get_ldev(mdev)) {
3630 int skip_initial_sync =
3631 mdev->state.conn == C_CONNECTED &&
Philipp Reisner31890f42011-01-19 14:12:51 +01003632 mdev->tconn->agreed_pro_version >= 90 &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003633 mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
3634 (p_uuid[UI_FLAGS] & 8);
3635 if (skip_initial_sync) {
3636 dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n");
3637 drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01003638 "clear_n_write from receive_uuids",
3639 BM_LOCKED_TEST_ALLOWED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003640 _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]);
3641 _drbd_uuid_set(mdev, UI_BITMAP, 0);
3642 _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
3643 CS_VERBOSE, NULL);
3644 drbd_md_sync(mdev);
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003645 updated_uuids = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003646 }
3647 put_ldev(mdev);
Philipp Reisner18a50fa2010-06-21 14:14:15 +02003648 } else if (mdev->state.disk < D_INCONSISTENT &&
3649 mdev->state.role == R_PRIMARY) {
3650 /* I am a diskless primary, the peer just created a new current UUID
3651 for me. */
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003652 updated_uuids = drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003653 }
3654
3655 /* Before we test for the disk state, we should wait until an eventually
3656 ongoing cluster wide state change is finished. That is important if
3657 we are primary and are detaching from our disk. We need to see the
3658 new disk state... */
Philipp Reisner8410da8f02011-02-11 20:11:10 +01003659 mutex_lock(mdev->state_mutex);
3660 mutex_unlock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003661 if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT)
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003662 updated_uuids |= drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
3663
3664 if (updated_uuids)
3665 drbd_print_uuids(mdev, "receiver updated UUIDs to");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003666
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003667 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003668}
3669
3670/**
3671 * convert_state() - Converts the peer's view of the cluster state to our point of view
3672 * @ps: The state as seen by the peer.
3673 */
3674static union drbd_state convert_state(union drbd_state ps)
3675{
3676 union drbd_state ms;
3677
3678 static enum drbd_conns c_tab[] = {
Philipp Reisner369bea62011-07-06 23:04:44 +02003679 [C_WF_REPORT_PARAMS] = C_WF_REPORT_PARAMS,
Philipp Reisnerb411b362009-09-25 16:07:19 -07003680 [C_CONNECTED] = C_CONNECTED,
3681
3682 [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
3683 [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
3684 [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
3685 [C_VERIFY_S] = C_VERIFY_T,
3686 [C_MASK] = C_MASK,
3687 };
3688
3689 ms.i = ps.i;
3690
3691 ms.conn = c_tab[ps.conn];
3692 ms.peer = ps.role;
3693 ms.role = ps.peer;
3694 ms.pdsk = ps.disk;
3695 ms.disk = ps.pdsk;
3696 ms.peer_isp = (ps.aftr_isp | ps.user_isp);
3697
3698 return ms;
3699}
3700
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003701static int receive_req_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003702{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003703 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003704 struct p_req_state *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003705 union drbd_state mask, val;
Andreas Gruenbacherbf885f82010-12-08 00:39:32 +01003706 enum drbd_state_rv rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003707
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003708 mdev = vnr_to_mdev(tconn, pi->vnr);
3709 if (!mdev)
3710 return -EIO;
3711
Philipp Reisnerb411b362009-09-25 16:07:19 -07003712 mask.i = be32_to_cpu(p->mask);
3713 val.i = be32_to_cpu(p->val);
3714
Philipp Reisner25703f82011-02-07 14:35:25 +01003715 if (test_bit(DISCARD_CONCURRENT, &mdev->tconn->flags) &&
Philipp Reisner8410da8f02011-02-11 20:11:10 +01003716 mutex_is_locked(mdev->state_mutex)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07003717 drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003718 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003719 }
3720
3721 mask = convert_state(mask);
3722 val = convert_state(val);
3723
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003724 rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
3725 drbd_send_sr_reply(mdev, rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003726
Philipp Reisnerb411b362009-09-25 16:07:19 -07003727 drbd_md_sync(mdev);
3728
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003729 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003730}
3731
Andreas Gruenbachere2857212011-03-25 00:57:38 +01003732static int receive_req_conn_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003733{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003734 struct p_req_state *p = pi->data;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003735 union drbd_state mask, val;
3736 enum drbd_state_rv rv;
3737
3738 mask.i = be32_to_cpu(p->mask);
3739 val.i = be32_to_cpu(p->val);
3740
3741 if (test_bit(DISCARD_CONCURRENT, &tconn->flags) &&
3742 mutex_is_locked(&tconn->cstate_mutex)) {
3743 conn_send_sr_reply(tconn, SS_CONCURRENT_ST_CHG);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003744 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003745 }
3746
3747 mask = convert_state(mask);
3748 val = convert_state(val);
3749
Philipp Reisner778bcf22011-03-28 12:55:03 +02003750 rv = conn_request_state(tconn, mask, val, CS_VERBOSE | CS_LOCAL_ONLY | CS_IGN_OUTD_FAIL);
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003751 conn_send_sr_reply(tconn, rv);
3752
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003753 return 0;
Philipp Reisnerdfafcc82011-03-16 10:55:07 +01003754}
3755
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003756static int receive_state(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003757{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003758 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003759 struct p_state *p = pi->data;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003760 union drbd_state os, ns, peer_state;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003761 enum drbd_disk_state real_peer_disk;
Philipp Reisner65d922c2010-06-16 16:18:09 +02003762 enum chg_state_flags cs_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003763 int rv;
3764
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003765 mdev = vnr_to_mdev(tconn, pi->vnr);
3766 if (!mdev)
3767 return config_unknown_volume(tconn, pi);
3768
Philipp Reisnerb411b362009-09-25 16:07:19 -07003769 peer_state.i = be32_to_cpu(p->state);
3770
3771 real_peer_disk = peer_state.disk;
3772 if (peer_state.disk == D_NEGOTIATING) {
3773 real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
3774 dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
3775 }
3776
Philipp Reisner87eeee42011-01-19 14:16:30 +01003777 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003778 retry:
Philipp Reisner78bae592011-03-28 15:40:12 +02003779 os = ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003780 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003781
Philipp Reisner9bcd2522011-09-29 13:00:14 +02003782 /* If this is the "end of sync" confirmation, usually the peer disk
3783 * transitions from D_INCONSISTENT to D_UP_TO_DATE. For empty (0 bits
3784 * set) resync started in PausedSyncT, or if the timing of pause-/
3785 * unpause-sync events has been "just right", the peer disk may
3786 * transition from D_CONSISTENT to D_UP_TO_DATE as well.
3787 */
3788 if ((os.pdsk == D_INCONSISTENT || os.pdsk == D_CONSISTENT) &&
3789 real_peer_disk == D_UP_TO_DATE &&
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003790 os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) {
3791 /* If we are (becoming) SyncSource, but peer is still in sync
3792 * preparation, ignore its uptodate-ness to avoid flapping, it
3793 * will change to inconsistent once the peer reaches active
3794 * syncing states.
3795 * It may have changed syncer-paused flags, however, so we
3796 * cannot ignore this completely. */
3797 if (peer_state.conn > C_CONNECTED &&
3798 peer_state.conn < C_SYNC_SOURCE)
3799 real_peer_disk = D_INCONSISTENT;
3800
3801 /* if peer_state changes to connected at the same time,
3802 * it explicitly notifies us that it finished resync.
3803 * Maybe we should finish it up, too? */
3804 else if (os.conn >= C_SYNC_SOURCE &&
3805 peer_state.conn == C_CONNECTED) {
3806 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed)
3807 drbd_resync_finished(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003808 return 0;
Lars Ellenberge9ef7bb2010-10-07 15:55:39 +02003809 }
3810 }
3811
3812 /* peer says his disk is inconsistent, while we think it is uptodate,
3813 * and this happens while the peer still thinks we have a sync going on,
3814 * but we think we are already done with the sync.
3815 * We ignore this to avoid flapping pdsk.
3816 * This should not happen, if the peer is a recent version of drbd. */
3817 if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT &&
3818 os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE)
3819 real_peer_disk = D_UP_TO_DATE;
3820
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003821 if (ns.conn == C_WF_REPORT_PARAMS)
3822 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003823
Philipp Reisner67531712010-10-27 12:21:30 +02003824 if (peer_state.conn == C_AHEAD)
3825 ns.conn = C_BEHIND;
3826
Philipp Reisnerb411b362009-09-25 16:07:19 -07003827 if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING &&
3828 get_ldev_if_state(mdev, D_NEGOTIATING)) {
3829 int cr; /* consider resync */
3830
3831 /* if we established a new connection */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003832 cr = (os.conn < C_CONNECTED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003833 /* if we had an established connection
3834 * and one of the nodes newly attaches a disk */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003835 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003836 (peer_state.disk == D_NEGOTIATING ||
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003837 os.disk == D_NEGOTIATING));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003838 /* if we have both been inconsistent, and the peer has been
3839 * forced to be UpToDate with --overwrite-data */
3840 cr |= test_bit(CONSIDER_RESYNC, &mdev->flags);
3841 /* if we had been plain connected, and the admin requested to
3842 * start a sync by "invalidate" or "invalidate-remote" */
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003843 cr |= (os.conn == C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003844 (peer_state.conn >= C_STARTING_SYNC_S &&
3845 peer_state.conn <= C_WF_BITMAP_T));
3846
3847 if (cr)
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003848 ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003849
3850 put_ldev(mdev);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003851 if (ns.conn == C_MASK) {
3852 ns.conn = C_CONNECTED;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003853 if (mdev->state.disk == D_NEGOTIATING) {
Lars Ellenberg82f59cc2010-10-16 12:13:47 +02003854 drbd_force_state(mdev, NS(disk, D_FAILED));
Philipp Reisnerb411b362009-09-25 16:07:19 -07003855 } else if (peer_state.disk == D_NEGOTIATING) {
3856 dev_err(DEV, "Disk attach process on the peer node was aborted.\n");
3857 peer_state.disk = D_DISKLESS;
Lars Ellenberg580b9762010-02-26 23:15:23 +01003858 real_peer_disk = D_DISKLESS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003859 } else {
Philipp Reisner8169e412011-03-15 18:40:27 +01003860 if (test_and_clear_bit(CONN_DRY_RUN, &mdev->tconn->flags))
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003861 return -EIO;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003862 D_ASSERT(os.conn == C_WF_REPORT_PARAMS);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003863 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003864 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003865 }
3866 }
3867 }
3868
Philipp Reisner87eeee42011-01-19 14:16:30 +01003869 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner78bae592011-03-28 15:40:12 +02003870 if (os.i != drbd_read_state(mdev).i)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003871 goto retry;
3872 clear_bit(CONSIDER_RESYNC, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003873 ns.peer = peer_state.role;
3874 ns.pdsk = real_peer_disk;
3875 ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003876 if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003877 ns.disk = mdev->new_state_tmp.disk;
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003878 cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD);
Philipp Reisner2aebfab2011-03-28 16:48:11 +02003879 if (ns.pdsk == D_CONSISTENT && drbd_suspended(mdev) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED &&
Philipp Reisner481c6f52010-06-22 14:03:27 +02003880 test_bit(NEW_CUR_UUID, &mdev->flags)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01003881 /* Do not allow tl_restart(RESEND) for a rebooted peer. We can only allow this
Philipp Reisner481c6f52010-06-22 14:03:27 +02003882 for temporal network outages! */
Philipp Reisner87eeee42011-01-19 14:16:30 +01003883 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003884 dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01003885 tl_clear(mdev->tconn);
Philipp Reisner481c6f52010-06-22 14:03:27 +02003886 drbd_uuid_new_current(mdev);
3887 clear_bit(NEW_CUR_UUID, &mdev->flags);
Philipp Reisner38fa9982011-03-15 18:24:49 +01003888 conn_request_state(mdev->tconn, NS2(conn, C_PROTOCOL_ERROR, susp, 0), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003889 return -EIO;
Philipp Reisner481c6f52010-06-22 14:03:27 +02003890 }
Philipp Reisner65d922c2010-06-16 16:18:09 +02003891 rv = _drbd_set_state(mdev, ns, cs_flags, NULL);
Philipp Reisner78bae592011-03-28 15:40:12 +02003892 ns = drbd_read_state(mdev);
Philipp Reisner87eeee42011-01-19 14:16:30 +01003893 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003894
3895 if (rv < SS_SUCCESS) {
Philipp Reisner38fa9982011-03-15 18:24:49 +01003896 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003897 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003898 }
3899
Lars Ellenberg4ac4aad2010-07-22 17:39:26 +02003900 if (os.conn > C_WF_REPORT_PARAMS) {
3901 if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
Philipp Reisnerb411b362009-09-25 16:07:19 -07003902 peer_state.disk != D_NEGOTIATING ) {
3903 /* we want resync, peer has not yet decided to sync... */
3904 /* Nowadays only used when forcing a node into primary role and
3905 setting its disk to UpToDate with that */
3906 drbd_send_uuids(mdev);
Philipp Reisner43de7c82011-11-10 13:16:13 +01003907 drbd_send_current_state(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003908 }
3909 }
3910
Philipp Reisner08b165b2011-09-05 16:22:33 +02003911 clear_bit(DISCARD_MY_DATA, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003912
3913 drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
3914
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003915 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003916}
3917
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003918static int receive_sync_uuid(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003919{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003920 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003921 struct p_rs_uuid *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01003922
3923 mdev = vnr_to_mdev(tconn, pi->vnr);
3924 if (!mdev)
3925 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003926
3927 wait_event(mdev->misc_wait,
3928 mdev->state.conn == C_WF_SYNC_UUID ||
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02003929 mdev->state.conn == C_BEHIND ||
Philipp Reisnerb411b362009-09-25 16:07:19 -07003930 mdev->state.conn < C_CONNECTED ||
3931 mdev->state.disk < D_NEGOTIATING);
3932
3933 /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */
3934
Philipp Reisnerb411b362009-09-25 16:07:19 -07003935 /* Here the _drbd_uuid_ functions are right, current should
3936 _not_ be rotated into the history */
3937 if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
3938 _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid));
3939 _drbd_uuid_set(mdev, UI_BITMAP, 0UL);
3940
Lars Ellenberg62b0da32011-01-20 13:25:21 +01003941 drbd_print_uuids(mdev, "updated sync uuid");
Philipp Reisnerb411b362009-09-25 16:07:19 -07003942 drbd_start_resync(mdev, C_SYNC_TARGET);
3943
3944 put_ldev(mdev);
3945 } else
3946 dev_err(DEV, "Ignoring SyncUUID packet!\n");
3947
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003948 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003949}
3950
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003951/**
3952 * receive_bitmap_plain
3953 *
3954 * Return 0 when done, 1 when another iteration is needed, and a negative error
3955 * code upon failure.
3956 */
3957static int
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003958receive_bitmap_plain(struct drbd_conf *mdev, unsigned int size,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003959 unsigned long *p, struct bm_xfer_ctx *c)
Philipp Reisnerb411b362009-09-25 16:07:19 -07003960{
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003961 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE -
3962 drbd_header_size(mdev->tconn);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003963 unsigned int num_words = min_t(size_t, data_size / sizeof(*p),
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003964 c->bm_words - c->word_offset);
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003965 unsigned int want = num_words * sizeof(*p);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003966 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003967
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02003968 if (want != size) {
3969 dev_err(DEV, "%s:want (%u) != size (%u)\n", __func__, want, size);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003970 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003971 }
3972 if (want == 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003973 return 0;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003974 err = drbd_recv_all(mdev->tconn, p, want);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01003975 if (err)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003976 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003977
Andreas Gruenbachere6589832011-03-30 12:54:42 +02003978 drbd_bm_merge_lel(mdev, c->word_offset, num_words, p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07003979
3980 c->word_offset += num_words;
3981 c->bit_offset = c->word_offset * BITS_PER_LONG;
3982 if (c->bit_offset > c->bm_bits)
3983 c->bit_offset = c->bm_bits;
3984
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01003985 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07003986}
3987
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01003988static enum drbd_bitmap_code dcbp_get_code(struct p_compressed_bm *p)
3989{
3990 return (enum drbd_bitmap_code)(p->encoding & 0x0f);
3991}
3992
3993static int dcbp_get_start(struct p_compressed_bm *p)
3994{
3995 return (p->encoding & 0x80) != 0;
3996}
3997
3998static int dcbp_get_pad_bits(struct p_compressed_bm *p)
3999{
4000 return (p->encoding >> 4) & 0x7;
4001}
4002
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004003/**
4004 * recv_bm_rle_bits
4005 *
4006 * Return 0 when done, 1 when another iteration is needed, and a negative error
4007 * code upon failure.
4008 */
4009static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07004010recv_bm_rle_bits(struct drbd_conf *mdev,
4011 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01004012 struct bm_xfer_ctx *c,
4013 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004014{
4015 struct bitstream bs;
4016 u64 look_ahead;
4017 u64 rl;
4018 u64 tmp;
4019 unsigned long s = c->bit_offset;
4020 unsigned long e;
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004021 int toggle = dcbp_get_start(p);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004022 int have;
4023 int bits;
4024
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004025 bitstream_init(&bs, p->code, len, dcbp_get_pad_bits(p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004026
4027 bits = bitstream_get_bits(&bs, &look_ahead, 64);
4028 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004029 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004030
4031 for (have = bits; have > 0; s += rl, toggle = !toggle) {
4032 bits = vli_decode_bits(&rl, look_ahead);
4033 if (bits <= 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004034 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004035
4036 if (toggle) {
4037 e = s + rl -1;
4038 if (e >= c->bm_bits) {
4039 dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004040 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004041 }
4042 _drbd_bm_set_bits(mdev, s, e);
4043 }
4044
4045 if (have < bits) {
4046 dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
4047 have, bits, look_ahead,
4048 (unsigned int)(bs.cur.b - p->code),
4049 (unsigned int)bs.buf_len);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004050 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004051 }
4052 look_ahead >>= bits;
4053 have -= bits;
4054
4055 bits = bitstream_get_bits(&bs, &tmp, 64 - have);
4056 if (bits < 0)
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004057 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004058 look_ahead |= tmp << have;
4059 have += bits;
4060 }
4061
4062 c->bit_offset = s;
4063 bm_xfer_ctx_bit_to_word_offset(c);
4064
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004065 return (s != c->bm_bits);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004066}
4067
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004068/**
4069 * decode_bitmap_c
4070 *
4071 * Return 0 when done, 1 when another iteration is needed, and a negative error
4072 * code upon failure.
4073 */
4074static int
Philipp Reisnerb411b362009-09-25 16:07:19 -07004075decode_bitmap_c(struct drbd_conf *mdev,
4076 struct p_compressed_bm *p,
Philipp Reisnerc6d25cf2011-01-19 16:13:06 +01004077 struct bm_xfer_ctx *c,
4078 unsigned int len)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004079{
Andreas Gruenbachera02d1242011-03-22 17:20:45 +01004080 if (dcbp_get_code(p) == RLE_VLI_Bits)
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004081 return recv_bm_rle_bits(mdev, p, c, len - sizeof(*p));
Philipp Reisnerb411b362009-09-25 16:07:19 -07004082
4083 /* other variants had been implemented for evaluation,
4084 * but have been dropped as this one turned out to be "best"
4085 * during all our tests. */
4086
4087 dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
Philipp Reisner38fa9982011-03-15 18:24:49 +01004088 conn_request_state(mdev->tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004089 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004090}
4091
4092void INFO_bm_xfer_stats(struct drbd_conf *mdev,
4093 const char *direction, struct bm_xfer_ctx *c)
4094{
4095 /* what would it take to transfer it "plaintext" */
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004096 unsigned int header_size = drbd_header_size(mdev->tconn);
4097 unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE - header_size;
4098 unsigned int plain =
4099 header_size * (DIV_ROUND_UP(c->bm_words, data_size) + 1) +
4100 c->bm_words * sizeof(unsigned long);
4101 unsigned int total = c->bytes[0] + c->bytes[1];
4102 unsigned int r;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004103
4104 /* total can not be zero. but just in case: */
4105 if (total == 0)
4106 return;
4107
4108 /* don't report if not compressed */
4109 if (total >= plain)
4110 return;
4111
4112 /* total < plain. check for overflow, still */
4113 r = (total > UINT_MAX/1000) ? (total / (plain/1000))
4114 : (1000 * total / plain);
4115
4116 if (r > 1000)
4117 r = 1000;
4118
4119 r = 1000 - r;
4120 dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
4121 "total %u; compression: %u.%u%%\n",
4122 direction,
4123 c->bytes[1], c->packets[1],
4124 c->bytes[0], c->packets[0],
4125 total, r/10, r % 10);
4126}
4127
4128/* Since we are processing the bitfield from lower addresses to higher,
4129 it does not matter if the process it in 32 bit chunks or 64 bit
4130 chunks as long as it is little endian. (Understand it as byte stream,
4131 beginning with the lowest byte...) If we would use big endian
4132 we would need to process it from the highest address to the lowest,
4133 in order to be agnostic to the 32 vs 64 bits issue.
4134
4135 returns 0 on failure, 1 if we successfully received it. */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004136static int receive_bitmap(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004137{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004138 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004139 struct bm_xfer_ctx c;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004140 int err;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004141
4142 mdev = vnr_to_mdev(tconn, pi->vnr);
4143 if (!mdev)
4144 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004145
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004146 drbd_bm_lock(mdev, "receive bitmap", BM_LOCKED_SET_ALLOWED);
4147 /* you are supposed to send additional out-of-sync information
4148 * if you actually set bits during this phase */
Philipp Reisnerb411b362009-09-25 16:07:19 -07004149
Philipp Reisnerb411b362009-09-25 16:07:19 -07004150 c = (struct bm_xfer_ctx) {
4151 .bm_bits = drbd_bm_bits(mdev),
4152 .bm_words = drbd_bm_words(mdev),
4153 };
4154
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004155 for(;;) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004156 if (pi->cmd == P_BITMAP)
4157 err = receive_bitmap_plain(mdev, pi->size, pi->data, &c);
4158 else if (pi->cmd == P_COMPRESSED_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004159 /* MAYBE: sanity check that we speak proto >= 90,
4160 * and the feature is enabled! */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004161 struct p_compressed_bm *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004162
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004163 if (pi->size > DRBD_SOCKET_BUFFER_SIZE - drbd_header_size(tconn)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004164 dev_err(DEV, "ReportCBitmap packet too large\n");
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004165 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004166 goto out;
4167 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004168 if (pi->size <= sizeof(*p)) {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004169 dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", pi->size);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004170 err = -EIO;
Andreas Gruenbacher78fcbda2010-12-10 22:18:27 +01004171 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004172 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004173 err = drbd_recv_all(mdev->tconn, p, pi->size);
4174 if (err)
4175 goto out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004176 err = decode_bitmap_c(mdev, p, &c, pi->size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004177 } else {
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004178 dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", pi->cmd);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004179 err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004180 goto out;
4181 }
4182
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004183 c.packets[pi->cmd == P_BITMAP]++;
Andreas Gruenbacher50d0b1a2011-03-30 11:53:51 +02004184 c.bytes[pi->cmd == P_BITMAP] += drbd_header_size(tconn) + pi->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004185
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004186 if (err <= 0) {
4187 if (err < 0)
4188 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004189 break;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004190 }
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004191 err = drbd_recv_header(mdev->tconn, pi);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004192 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004193 goto out;
Andreas Gruenbacher2c464072010-12-11 21:53:12 +01004194 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004195
4196 INFO_bm_xfer_stats(mdev, "receive", &c);
4197
4198 if (mdev->state.conn == C_WF_BITMAP_T) {
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01004199 enum drbd_state_rv rv;
4200
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004201 err = drbd_send_bitmap(mdev);
4202 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004203 goto out;
4204 /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
Andreas Gruenbacherde1f8e42010-12-10 21:04:00 +01004205 rv = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
4206 D_ASSERT(rv == SS_SUCCESS);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004207 } else if (mdev->state.conn != C_WF_BITMAP_S) {
4208 /* admin may have requested C_DISCONNECTING,
4209 * other threads may have noticed network errors */
4210 dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n",
4211 drbd_conn_str(mdev->state.conn));
4212 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004213 err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004214
Philipp Reisnerb411b362009-09-25 16:07:19 -07004215 out:
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004216 drbd_bm_unlock(mdev);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004217 if (!err && mdev->state.conn == C_WF_BITMAP_S)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004218 drbd_start_resync(mdev, C_SYNC_SOURCE);
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004219 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004220}
4221
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004222static int receive_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004223{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004224 conn_warn(tconn, "skipping unknown optional packet type %d, l: %d!\n",
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004225 pi->cmd, pi->size);
Philipp Reisner2de876e2011-03-15 14:38:01 +01004226
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004227 return ignore_remaining_packet(tconn, pi);
Philipp Reisner2de876e2011-03-15 14:38:01 +01004228}
4229
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004230static int receive_UnplugRemote(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004231{
Philipp Reisnerb411b362009-09-25 16:07:19 -07004232 /* Make sure we've acked all the TCP data associated
4233 * with the data requests being unplugged */
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004234 drbd_tcp_quickack(tconn->data.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004235
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004236 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004237}
4238
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004239static int receive_out_of_sync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner73a01a12010-10-27 14:33:00 +02004240{
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004241 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004242 struct p_block_desc *p = pi->data;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004243
4244 mdev = vnr_to_mdev(tconn, pi->vnr);
4245 if (!mdev)
4246 return -EIO;
Philipp Reisner73a01a12010-10-27 14:33:00 +02004247
Lars Ellenbergf735e3632010-12-17 21:06:18 +01004248 switch (mdev->state.conn) {
4249 case C_WF_SYNC_UUID:
4250 case C_WF_BITMAP_T:
4251 case C_BEHIND:
4252 break;
4253 default:
4254 dev_err(DEV, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n",
4255 drbd_conn_str(mdev->state.conn));
4256 }
4257
Philipp Reisner73a01a12010-10-27 14:33:00 +02004258 drbd_set_out_of_sync(mdev, be64_to_cpu(p->sector), be32_to_cpu(p->blksize));
4259
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004260 return 0;
Philipp Reisner73a01a12010-10-27 14:33:00 +02004261}
4262
Philipp Reisner02918be2010-08-20 14:35:10 +02004263struct data_cmd {
4264 int expect_payload;
4265 size_t pkt_size;
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004266 int (*fn)(struct drbd_tconn *, struct packet_info *);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004267};
4268
Philipp Reisner02918be2010-08-20 14:35:10 +02004269static struct data_cmd drbd_cmd_handler[] = {
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004270 [P_DATA] = { 1, sizeof(struct p_data), receive_Data },
4271 [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply },
4272 [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } ,
4273 [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } ,
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004274 [P_BITMAP] = { 1, 0, receive_bitmap } ,
4275 [P_COMPRESSED_BITMAP] = { 1, 0, receive_bitmap } ,
4276 [P_UNPLUG_REMOTE] = { 0, 0, receive_UnplugRemote },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004277 [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4278 [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004279 [P_SYNC_PARAM] = { 1, 0, receive_SyncParam },
4280 [P_SYNC_PARAM89] = { 1, 0, receive_SyncParam },
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004281 [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol },
4282 [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids },
4283 [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes },
4284 [P_STATE] = { 0, sizeof(struct p_state), receive_state },
4285 [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state },
4286 [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid },
4287 [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
4288 [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4289 [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest },
4290 [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip },
4291 [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync },
4292 [P_CONN_ST_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_conn_state },
Philipp Reisner036b17e2011-05-16 17:38:11 +02004293 [P_PROTOCOL_UPDATE] = { 1, sizeof(struct p_protocol), receive_protocol },
Philipp Reisner02918be2010-08-20 14:35:10 +02004294};
4295
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004296static void drbdd(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004297{
Philipp Reisner77351055b2011-02-07 17:24:26 +01004298 struct packet_info pi;
Philipp Reisner02918be2010-08-20 14:35:10 +02004299 size_t shs; /* sub header size */
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004300 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004301
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004302 while (get_t_state(&tconn->receiver) == RUNNING) {
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004303 struct data_cmd *cmd;
4304
Philipp Reisnereefc2f72011-02-08 12:55:24 +01004305 drbd_thread_current_set_cpu(&tconn->receiver);
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004306 if (drbd_recv_header(tconn, &pi))
Philipp Reisner02918be2010-08-20 14:35:10 +02004307 goto err_out;
4308
Andreas Gruenbacherdeebe192011-03-25 00:01:04 +01004309 cmd = &drbd_cmd_handler[pi.cmd];
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004310 if (unlikely(pi.cmd >= ARRAY_SIZE(drbd_cmd_handler) || !cmd->fn)) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004311 conn_err(tconn, "Unexpected data packet %s (0x%04x)",
4312 cmdname(pi.cmd), pi.cmd);
Philipp Reisner02918be2010-08-20 14:35:10 +02004313 goto err_out;
Lars Ellenberg0b33a912009-11-16 15:58:04 +01004314 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004315
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004316 shs = cmd->pkt_size;
4317 if (pi.size > shs && !cmd->expect_payload) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004318 conn_err(tconn, "No payload expected %s l:%d\n",
4319 cmdname(pi.cmd), pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004320 goto err_out;
4321 }
4322
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004323 if (shs) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004324 err = drbd_recv_all_warn(tconn, pi.data, shs);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004325 if (err)
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004326 goto err_out;
Andreas Gruenbachere2857212011-03-25 00:57:38 +01004327 pi.size -= shs;
Lars Ellenbergc13f7e12010-10-29 23:32:01 +02004328 }
4329
Andreas Gruenbacher4a76b162011-03-25 02:43:51 +01004330 err = cmd->fn(tconn, &pi);
4331 if (err) {
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004332 conn_err(tconn, "error receiving %s, e: %d l: %d!\n",
4333 cmdname(pi.cmd), err, pi.size);
Philipp Reisner02918be2010-08-20 14:35:10 +02004334 goto err_out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004335 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004336 }
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004337 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004338
Andreas Gruenbacher82bc0192011-03-17 12:10:19 +01004339 err_out:
4340 conn_request_state(tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004341}
4342
Philipp Reisner0e29d162011-02-18 14:23:11 +01004343void conn_flush_workqueue(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004344{
4345 struct drbd_wq_barrier barr;
4346
4347 barr.w.cb = w_prev_work_done;
Philipp Reisner0e29d162011-02-18 14:23:11 +01004348 barr.w.tconn = tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004349 init_completion(&barr.done);
Philipp Reisner0e29d162011-02-18 14:23:11 +01004350 drbd_queue_work(&tconn->data.work, &barr.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004351 wait_for_completion(&barr.done);
4352}
4353
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004354static void conn_disconnect(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004355{
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004356 struct drbd_conf *mdev;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004357 enum drbd_conns oc;
Philipp Reisner376694a2011-11-07 10:54:28 +01004358 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004359
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004360 if (tconn->cstate == C_STANDALONE)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004361 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004362
4363 /* asender does not clean up anything. it must not interfere, either */
Philipp Reisner360cc742011-02-08 14:29:53 +01004364 drbd_thread_stop(&tconn->asender);
4365 drbd_free_sock(tconn);
4366
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004367 rcu_read_lock();
4368 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
4369 kref_get(&mdev->kref);
4370 rcu_read_unlock();
4371 drbd_disconnected(mdev);
4372 kref_put(&mdev->kref, &drbd_minor_destroy);
4373 rcu_read_lock();
4374 }
4375 rcu_read_unlock();
4376
Philipp Reisner12038a32011-11-09 19:18:00 +01004377 if (!list_empty(&tconn->current_epoch->list))
4378 conn_err(tconn, "ASSERTION FAILED: tconn->current_epoch->list not empty\n");
4379 /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
4380 atomic_set(&tconn->current_epoch->epoch_size, 0);
4381
Philipp Reisner360cc742011-02-08 14:29:53 +01004382 conn_info(tconn, "Connection closed\n");
4383
Philipp Reisnercb703452011-03-24 11:03:07 +01004384 if (conn_highest_role(tconn) == R_PRIMARY && conn_highest_pdsk(tconn) >= D_UNKNOWN)
4385 conn_try_outdate_peer_async(tconn);
4386
Philipp Reisner360cc742011-02-08 14:29:53 +01004387 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004388 oc = tconn->cstate;
4389 if (oc >= C_UNCONNECTED)
Philipp Reisner376694a2011-11-07 10:54:28 +01004390 _conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004391
Philipp Reisner360cc742011-02-08 14:29:53 +01004392 spin_unlock_irq(&tconn->req_lock);
4393
Lars Ellenbergf3dfa402011-05-02 10:45:05 +02004394 if (oc == C_DISCONNECTING)
Lars Ellenbergd9cc6e22011-04-27 10:25:28 +02004395 conn_request_state(tconn, NS(conn, C_STANDALONE), CS_VERBOSE | CS_HARD);
Philipp Reisner360cc742011-02-08 14:29:53 +01004396}
4397
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02004398static int drbd_disconnected(struct drbd_conf *mdev)
Philipp Reisner360cc742011-02-08 14:29:53 +01004399{
Philipp Reisner360cc742011-02-08 14:29:53 +01004400 unsigned int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004401
Philipp Reisner85719572010-07-21 10:20:17 +02004402 /* wait for current activity to cease. */
Philipp Reisner87eeee42011-01-19 14:16:30 +01004403 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004404 _drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
4405 _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee);
4406 _drbd_wait_ee_list_empty(mdev, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004407 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004408
4409 /* We do not have data structures that would allow us to
4410 * get the rs_pending_cnt down to 0 again.
4411 * * On C_SYNC_TARGET we do not have any data structures describing
4412 * the pending RSDataRequest's we have sent.
4413 * * On C_SYNC_SOURCE there is no data structure that tracks
4414 * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
4415 * And no, it is not the sum of the reference counts in the
4416 * resync_LRU. The resync_LRU tracks the whole operation including
4417 * the disk-IO, while the rs_pending_cnt only tracks the blocks
4418 * on the fly. */
4419 drbd_rs_cancel_all(mdev);
4420 mdev->rs_total = 0;
4421 mdev->rs_failed = 0;
4422 atomic_set(&mdev->rs_pending_cnt, 0);
4423 wake_up(&mdev->misc_wait);
4424
Philipp Reisnerb411b362009-09-25 16:07:19 -07004425 del_timer_sync(&mdev->resync_timer);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004426 resync_timer_fn((unsigned long)mdev);
4427
Philipp Reisnerb411b362009-09-25 16:07:19 -07004428 /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
4429 * w_make_resync_request etc. which may still be on the worker queue
4430 * to be "canceled" */
Philipp Reisnera21e9292011-02-08 15:08:49 +01004431 drbd_flush_workqueue(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004432
Andreas Gruenbachera990be42011-04-06 17:56:48 +02004433 drbd_finish_peer_reqs(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004434
4435 kfree(mdev->p_uuid);
4436 mdev->p_uuid = NULL;
4437
Philipp Reisner2aebfab2011-03-28 16:48:11 +02004438 if (!drbd_suspended(mdev))
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01004439 tl_clear(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004440
Philipp Reisnerb411b362009-09-25 16:07:19 -07004441 drbd_md_sync(mdev);
4442
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01004443 /* serialize with bitmap writeout triggered by the state change,
4444 * if any. */
4445 wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
4446
Philipp Reisnerb411b362009-09-25 16:07:19 -07004447 /* tcp_close and release of sendpage pages can be deferred. I don't
4448 * want to use SO_LINGER, because apparently it can be deferred for
4449 * more than 20 seconds (longest time I checked).
4450 *
4451 * Actually we don't care for exactly when the network stack does its
4452 * put_page(), but release our reference on these pages right here.
4453 */
Andreas Gruenbacher7721f562011-04-06 17:14:02 +02004454 i = drbd_free_peer_reqs(mdev, &mdev->net_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004455 if (i)
4456 dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
Lars Ellenberg435f0742010-09-06 12:30:25 +02004457 i = atomic_read(&mdev->pp_in_use_by_net);
4458 if (i)
4459 dev_info(DEV, "pp_in_use_by_net = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004460 i = atomic_read(&mdev->pp_in_use);
4461 if (i)
Lars Ellenberg45bb9122010-05-14 17:10:48 +02004462 dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004463
4464 D_ASSERT(list_empty(&mdev->read_ee));
4465 D_ASSERT(list_empty(&mdev->active_ee));
4466 D_ASSERT(list_empty(&mdev->sync_ee));
4467 D_ASSERT(list_empty(&mdev->done_ee));
4468
Philipp Reisner360cc742011-02-08 14:29:53 +01004469 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004470}
4471
4472/*
4473 * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
4474 * we can agree on is stored in agreed_pro_version.
4475 *
4476 * feature flags and the reserved array should be enough room for future
4477 * enhancements of the handshake protocol, and possible plugins...
4478 *
4479 * for now, they are expected to be zero, but ignored.
4480 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004481static int drbd_send_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004482{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004483 struct drbd_socket *sock;
4484 struct p_connection_features *p;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004485
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004486 sock = &tconn->data;
4487 p = conn_prepare_command(tconn, sock);
4488 if (!p)
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004489 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004490 memset(p, 0, sizeof(*p));
4491 p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
4492 p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004493 return conn_send_command(tconn, sock, P_CONNECTION_FEATURES, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004494}
4495
4496/*
4497 * return values:
4498 * 1 yes, we have a valid connection
4499 * 0 oops, did not work out, please try again
4500 * -1 peer talks different language,
4501 * no point in trying again, please go standalone.
4502 */
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004503static int drbd_do_features(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004504{
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004505 /* ASSERT current == tconn->receiver ... */
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004506 struct p_connection_features *p;
4507 const int expect = sizeof(struct p_connection_features);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004508 struct packet_info pi;
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004509 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004510
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004511 err = drbd_send_features(tconn);
Andreas Gruenbachere8d17b02011-03-16 00:54:19 +01004512 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004513 return 0;
4514
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004515 err = drbd_recv_header(tconn, &pi);
4516 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004517 return 0;
4518
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004519 if (pi.cmd != P_CONNECTION_FEATURES) {
4520 conn_err(tconn, "expected ConnectionFeatures packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004521 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004522 return -1;
4523 }
4524
Philipp Reisner77351055b2011-02-07 17:24:26 +01004525 if (pi.size != expect) {
Andreas Gruenbacher60381782011-03-28 17:05:50 +02004526 conn_err(tconn, "expected ConnectionFeatures length: %u, received: %u\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01004527 expect, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004528 return -1;
4529 }
4530
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004531 p = pi.data;
4532 err = drbd_recv_all_warn(tconn, p, expect);
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004533 if (err)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004534 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004535
Philipp Reisnerb411b362009-09-25 16:07:19 -07004536 p->protocol_min = be32_to_cpu(p->protocol_min);
4537 p->protocol_max = be32_to_cpu(p->protocol_max);
4538 if (p->protocol_max == 0)
4539 p->protocol_max = p->protocol_min;
4540
4541 if (PRO_VERSION_MAX < p->protocol_min ||
4542 PRO_VERSION_MIN > p->protocol_max)
4543 goto incompat;
4544
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004545 tconn->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004546
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004547 conn_info(tconn, "Handshake successful: "
4548 "Agreed network protocol version %d\n", tconn->agreed_pro_version);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004549
4550 return 1;
4551
4552 incompat:
Philipp Reisner65d11ed2011-02-07 17:35:59 +01004553 conn_err(tconn, "incompatible DRBD dialects: "
Philipp Reisnerb411b362009-09-25 16:07:19 -07004554 "I support %d-%d, peer supports %d-%d\n",
4555 PRO_VERSION_MIN, PRO_VERSION_MAX,
4556 p->protocol_min, p->protocol_max);
4557 return -1;
4558}
4559
4560#if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
Philipp Reisner13e60372011-02-08 09:54:40 +01004561static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004562{
4563 dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
4564 dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004565 return -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004566}
4567#else
4568#define CHALLENGE_LEN 64
Johannes Thomab10d96c2010-01-07 16:02:50 +01004569
4570/* Return value:
4571 1 - auth succeeded,
4572 0 - failed, try again (network error),
4573 -1 - auth failed, don't try again.
4574*/
4575
Philipp Reisner13e60372011-02-08 09:54:40 +01004576static int drbd_do_auth(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004577{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004578 struct drbd_socket *sock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004579 char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
4580 struct scatterlist sg;
4581 char *response = NULL;
4582 char *right_response = NULL;
4583 char *peers_ch = NULL;
Philipp Reisner44ed1672011-04-19 17:10:19 +02004584 unsigned int key_len;
4585 char secret[SHARED_SECRET_MAX]; /* 64 byte */
Philipp Reisnerb411b362009-09-25 16:07:19 -07004586 unsigned int resp_size;
4587 struct hash_desc desc;
Philipp Reisner77351055b2011-02-07 17:24:26 +01004588 struct packet_info pi;
Philipp Reisner44ed1672011-04-19 17:10:19 +02004589 struct net_conf *nc;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004590 int err, rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004591
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004592 /* FIXME: Put the challenge/response into the preallocated socket buffer. */
4593
Philipp Reisner44ed1672011-04-19 17:10:19 +02004594 rcu_read_lock();
4595 nc = rcu_dereference(tconn->net_conf);
4596 key_len = strlen(nc->shared_secret);
4597 memcpy(secret, nc->shared_secret, key_len);
4598 rcu_read_unlock();
4599
Philipp Reisner13e60372011-02-08 09:54:40 +01004600 desc.tfm = tconn->cram_hmac_tfm;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004601 desc.flags = 0;
4602
Philipp Reisner44ed1672011-04-19 17:10:19 +02004603 rv = crypto_hash_setkey(tconn->cram_hmac_tfm, (u8 *)secret, key_len);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004604 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004605 conn_err(tconn, "crypto_hash_setkey() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004606 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004607 goto fail;
4608 }
4609
4610 get_random_bytes(my_challenge, CHALLENGE_LEN);
4611
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004612 sock = &tconn->data;
4613 if (!conn_prepare_command(tconn, sock)) {
4614 rv = 0;
4615 goto fail;
4616 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004617 rv = !conn_send_command(tconn, sock, P_AUTH_CHALLENGE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004618 my_challenge, CHALLENGE_LEN);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004619 if (!rv)
4620 goto fail;
4621
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004622 err = drbd_recv_header(tconn, &pi);
4623 if (err) {
4624 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004625 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004626 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004627
Philipp Reisner77351055b2011-02-07 17:24:26 +01004628 if (pi.cmd != P_AUTH_CHALLENGE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004629 conn_err(tconn, "expected AuthChallenge packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004630 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004631 rv = 0;
4632 goto fail;
4633 }
4634
Philipp Reisner77351055b2011-02-07 17:24:26 +01004635 if (pi.size > CHALLENGE_LEN * 2) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004636 conn_err(tconn, "expected AuthChallenge payload too big.\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004637 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004638 goto fail;
4639 }
4640
Philipp Reisner77351055b2011-02-07 17:24:26 +01004641 peers_ch = kmalloc(pi.size, GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004642 if (peers_ch == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004643 conn_err(tconn, "kmalloc of peers_ch failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004644 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004645 goto fail;
4646 }
4647
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004648 err = drbd_recv_all_warn(tconn, peers_ch, pi.size);
4649 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004650 rv = 0;
4651 goto fail;
4652 }
4653
Philipp Reisner13e60372011-02-08 09:54:40 +01004654 resp_size = crypto_hash_digestsize(tconn->cram_hmac_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004655 response = kmalloc(resp_size, GFP_NOIO);
4656 if (response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004657 conn_err(tconn, "kmalloc of response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004658 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004659 goto fail;
4660 }
4661
4662 sg_init_table(&sg, 1);
Philipp Reisner77351055b2011-02-07 17:24:26 +01004663 sg_set_buf(&sg, peers_ch, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004664
4665 rv = crypto_hash_digest(&desc, &sg, sg.length, response);
4666 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004667 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004668 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004669 goto fail;
4670 }
4671
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004672 if (!conn_prepare_command(tconn, sock)) {
4673 rv = 0;
4674 goto fail;
4675 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004676 rv = !conn_send_command(tconn, sock, P_AUTH_RESPONSE, 0,
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02004677 response, resp_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004678 if (!rv)
4679 goto fail;
4680
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004681 err = drbd_recv_header(tconn, &pi);
4682 if (err) {
4683 rv = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004684 goto fail;
Andreas Gruenbacher69bc7bc2011-03-16 17:31:52 +01004685 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004686
Philipp Reisner77351055b2011-02-07 17:24:26 +01004687 if (pi.cmd != P_AUTH_RESPONSE) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004688 conn_err(tconn, "expected AuthResponse packet, received: %s (0x%04x)\n",
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02004689 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004690 rv = 0;
4691 goto fail;
4692 }
4693
Philipp Reisner77351055b2011-02-07 17:24:26 +01004694 if (pi.size != resp_size) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004695 conn_err(tconn, "expected AuthResponse payload of wrong size\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004696 rv = 0;
4697 goto fail;
4698 }
4699
Andreas Gruenbachera5c31902011-03-24 03:28:04 +01004700 err = drbd_recv_all_warn(tconn, response , resp_size);
4701 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004702 rv = 0;
4703 goto fail;
4704 }
4705
4706 right_response = kmalloc(resp_size, GFP_NOIO);
Julia Lawall2d1ee872009-12-27 22:27:11 +01004707 if (right_response == NULL) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004708 conn_err(tconn, "kmalloc of right_response failed\n");
Johannes Thomab10d96c2010-01-07 16:02:50 +01004709 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004710 goto fail;
4711 }
4712
4713 sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
4714
4715 rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
4716 if (rv) {
Philipp Reisner13e60372011-02-08 09:54:40 +01004717 conn_err(tconn, "crypto_hash_digest() failed with %d\n", rv);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004718 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004719 goto fail;
4720 }
4721
4722 rv = !memcmp(response, right_response, resp_size);
4723
4724 if (rv)
Philipp Reisner44ed1672011-04-19 17:10:19 +02004725 conn_info(tconn, "Peer authenticated using %d bytes HMAC\n",
4726 resp_size);
Johannes Thomab10d96c2010-01-07 16:02:50 +01004727 else
4728 rv = -1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004729
4730 fail:
4731 kfree(peers_ch);
4732 kfree(response);
4733 kfree(right_response);
4734
4735 return rv;
4736}
4737#endif
4738
4739int drbdd_init(struct drbd_thread *thi)
4740{
Philipp Reisner392c8802011-02-09 10:33:31 +01004741 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004742 int h;
4743
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004744 conn_info(tconn, "receiver (re)started\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004745
4746 do {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004747 h = conn_connect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004748 if (h == 0) {
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004749 conn_disconnect(tconn);
Philipp Reisner20ee6392011-01-18 15:28:59 +01004750 schedule_timeout_interruptible(HZ);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004751 }
4752 if (h == -1) {
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004753 conn_warn(tconn, "Discarding network configuration.\n");
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01004754 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004755 }
4756 } while (h == 0);
4757
Philipp Reisner91fd4da2011-04-20 17:47:29 +02004758 if (h > 0)
4759 drbdd(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004760
Philipp Reisner81fa2e62011-05-04 15:10:30 +02004761 conn_disconnect(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004762
Philipp Reisner4d641dd2011-02-08 15:40:24 +01004763 conn_info(tconn, "receiver terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07004764 return 0;
4765}
4766
4767/* ********* acknowledge sender ******** */
4768
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004769static int got_conn_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004770{
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004771 struct p_req_state_reply *p = pi->data;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004772 int retcode = be32_to_cpu(p->retcode);
4773
4774 if (retcode >= SS_SUCCESS) {
4775 set_bit(CONN_WD_ST_CHG_OKAY, &tconn->flags);
4776 } else {
4777 set_bit(CONN_WD_ST_CHG_FAIL, &tconn->flags);
4778 conn_err(tconn, "Requested state change failed by peer: %s (%d)\n",
4779 drbd_set_st_err_str(retcode), retcode);
4780 }
4781 wake_up(&tconn->ping_wait);
4782
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004783 return 0;
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004784}
4785
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004786static int got_RqSReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004787{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004788 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004789 struct p_req_state_reply *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004790 int retcode = be32_to_cpu(p->retcode);
4791
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004792 mdev = vnr_to_mdev(tconn, pi->vnr);
4793 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004794 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004795
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004796 if (retcode >= SS_SUCCESS) {
4797 set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
4798 } else {
4799 set_bit(CL_ST_CHG_FAIL, &mdev->flags);
4800 dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
4801 drbd_set_st_err_str(retcode), retcode);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004802 }
Philipp Reisnere4f78ed2011-03-16 11:27:48 +01004803 wake_up(&mdev->state_wait);
4804
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004805 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004806}
4807
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004808static int got_Ping(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004809{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004810 return drbd_send_ping_ack(tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004811
4812}
4813
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004814static int got_PingAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004815{
4816 /* restore idle timeout */
Philipp Reisner2a67d8b2011-02-09 14:10:32 +01004817 tconn->meta.socket->sk->sk_rcvtimeo = tconn->net_conf->ping_int*HZ;
4818 if (!test_and_set_bit(GOT_PING_ACK, &tconn->flags))
4819 wake_up(&tconn->ping_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004820
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004821 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004822}
4823
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004824static int got_IsInSync(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004825{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004826 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004827 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004828 sector_t sector = be64_to_cpu(p->sector);
4829 int blksize = be32_to_cpu(p->blksize);
4830
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004831 mdev = vnr_to_mdev(tconn, pi->vnr);
4832 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004833 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004834
Philipp Reisner31890f42011-01-19 14:12:51 +01004835 D_ASSERT(mdev->tconn->agreed_pro_version >= 89);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004836
4837 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4838
Lars Ellenberg1d53f092010-09-05 01:13:24 +02004839 if (get_ldev(mdev)) {
4840 drbd_rs_complete_io(mdev, sector);
4841 drbd_set_in_sync(mdev, sector, blksize);
4842 /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
4843 mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
4844 put_ldev(mdev);
4845 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07004846 dec_rs_pending(mdev);
Philipp Reisner778f2712010-07-06 11:14:00 +02004847 atomic_add(blksize >> 9, &mdev->rs_sect_in);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004848
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004849 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004850}
4851
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004852static int
4853validate_req_change_req_state(struct drbd_conf *mdev, u64 id, sector_t sector,
4854 struct rb_root *root, const char *func,
4855 enum drbd_req_event what, bool missing_ok)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004856{
4857 struct drbd_request *req;
4858 struct bio_and_error m;
4859
Philipp Reisner87eeee42011-01-19 14:16:30 +01004860 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherbc9c5c42011-01-21 18:00:55 +01004861 req = find_request(mdev, root, id, sector, missing_ok, func);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004862 if (unlikely(!req)) {
Philipp Reisner87eeee42011-01-19 14:16:30 +01004863 spin_unlock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004864 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004865 }
4866 __req_mod(req, what, &m);
Philipp Reisner87eeee42011-01-19 14:16:30 +01004867 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004868
4869 if (m.bio)
4870 complete_master_bio(mdev, &m);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004871 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004872}
4873
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004874static int got_BlockAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004875{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004876 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004877 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004878 sector_t sector = be64_to_cpu(p->sector);
4879 int blksize = be32_to_cpu(p->blksize);
4880 enum drbd_req_event what;
4881
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004882 mdev = vnr_to_mdev(tconn, pi->vnr);
4883 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004884 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004885
Philipp Reisnerb411b362009-09-25 16:07:19 -07004886 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4887
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004888 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004889 drbd_set_in_sync(mdev, sector, blksize);
4890 dec_rs_pending(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004891 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004892 }
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004893 switch (pi->cmd) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004894 case P_RS_WRITE_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004895 what = WRITE_ACKED_BY_PEER_AND_SIS;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004896 break;
4897 case P_WRITE_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004898 what = WRITE_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004899 break;
4900 case P_RECV_ACK:
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01004901 what = RECV_ACKED_BY_PEER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004902 break;
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004903 case P_DISCARD_WRITE:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004904 what = DISCARD_WRITE;
4905 break;
4906 case P_RETRY_WRITE:
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004907 what = POSTPONE_WRITE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004908 break;
4909 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004910 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -07004911 }
4912
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004913 return validate_req_change_req_state(mdev, p->block_id, sector,
4914 &mdev->write_requests, __func__,
4915 what, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004916}
4917
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004918static int got_NegAck(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);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004923 int size = be32_to_cpu(p->blksize);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004924 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004925
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004926 mdev = vnr_to_mdev(tconn, pi->vnr);
4927 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004928 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004929
Philipp Reisnerb411b362009-09-25 16:07:19 -07004930 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4931
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +01004932 if (p->block_id == ID_SYNCER) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07004933 dec_rs_pending(mdev);
4934 drbd_rs_failed_io(mdev, sector, size);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004935 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004936 }
Philipp Reisner2deb8332011-01-17 18:39:18 +01004937
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004938 err = validate_req_change_req_state(mdev, p->block_id, sector,
4939 &mdev->write_requests, __func__,
Philipp Reisner303d1442011-04-13 16:24:47 -07004940 NEG_ACKED, true);
Andreas Gruenbacher85997672011-04-04 13:09:15 +02004941 if (err) {
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004942 /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs.
4943 The master bio might already be completed, therefore the
4944 request is no longer in the collision hash. */
4945 /* In Protocol B we might already have got a P_RECV_ACK
4946 but then get a P_NEG_ACK afterwards. */
Andreas Gruenbacherc3afd8f2011-01-20 22:25:40 +01004947 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisner2deb8332011-01-17 18:39:18 +01004948 }
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004949 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004950}
4951
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004952static int got_NegDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004953{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004954 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004955 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004956 sector_t sector = be64_to_cpu(p->sector);
4957
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004958 mdev = vnr_to_mdev(tconn, pi->vnr);
4959 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004960 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004961
Philipp Reisnerb411b362009-09-25 16:07:19 -07004962 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
Andreas Gruenbacher7be8da02011-02-22 02:15:32 +01004963
Philipp Reisnerb411b362009-09-25 16:07:19 -07004964 dev_err(DEV, "Got NegDReply; Sector %llus, len %u; Fail original request.\n",
4965 (unsigned long long)sector, be32_to_cpu(p->blksize));
4966
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004967 return validate_req_change_req_state(mdev, p->block_id, sector,
4968 &mdev->read_requests, __func__,
4969 NEG_ACKED, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004970}
4971
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004972static int got_NegRSDReply(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07004973{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004974 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004975 sector_t sector;
4976 int size;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02004977 struct p_block_ack *p = pi->data;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01004978
4979 mdev = vnr_to_mdev(tconn, pi->vnr);
4980 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004981 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07004982
4983 sector = be64_to_cpu(p->sector);
4984 size = be32_to_cpu(p->blksize);
Philipp Reisnerb411b362009-09-25 16:07:19 -07004985
4986 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
4987
4988 dec_rs_pending(mdev);
4989
4990 if (get_ldev_if_state(mdev, D_FAILED)) {
4991 drbd_rs_complete_io(mdev, sector);
Andreas Gruenbachere05e1e52011-03-25 15:16:26 +01004992 switch (pi->cmd) {
Philipp Reisnerd612d302010-12-27 10:53:28 +01004993 case P_NEG_RS_DREPLY:
4994 drbd_rs_failed_io(mdev, sector, size);
4995 case P_RS_CANCEL:
4996 break;
4997 default:
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02004998 BUG();
Philipp Reisnerd612d302010-12-27 10:53:28 +01004999 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005000 put_ldev(mdev);
5001 }
5002
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005003 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005004}
5005
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005006static int got_BarrierAck(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005007{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005008 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005009 struct p_barrier_ack *p = pi->data;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005010
5011 mdev = vnr_to_mdev(tconn, pi->vnr);
5012 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005013 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005014
Philipp Reisner2f5cdd02011-02-21 14:29:27 +01005015 tl_release(mdev->tconn, p->barrier, be32_to_cpu(p->set_size));
Philipp Reisnerb411b362009-09-25 16:07:19 -07005016
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02005017 if (mdev->state.conn == C_AHEAD &&
5018 atomic_read(&mdev->ap_in_flight) == 0 &&
Philipp Reisner36baf612011-11-10 14:27:34 +01005019 !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->flags)) {
Philipp Reisner370a43e2011-01-14 16:03:11 +01005020 mdev->start_resync_timer.expires = jiffies + HZ;
5021 add_timer(&mdev->start_resync_timer);
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02005022 }
5023
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005024 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005025}
5026
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005027static int got_OVResult(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisnerb411b362009-09-25 16:07:19 -07005028{
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005029 struct drbd_conf *mdev;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005030 struct p_block_ack *p = pi->data;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005031 struct drbd_work *w;
5032 sector_t sector;
5033 int size;
5034
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005035 mdev = vnr_to_mdev(tconn, pi->vnr);
5036 if (!mdev)
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005037 return -EIO;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005038
Philipp Reisnerb411b362009-09-25 16:07:19 -07005039 sector = be64_to_cpu(p->sector);
5040 size = be32_to_cpu(p->blksize);
5041
5042 update_peer_seq(mdev, be32_to_cpu(p->seq_num));
5043
5044 if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005045 drbd_ov_out_of_sync_found(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005046 else
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005047 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005048
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005049 if (!get_ldev(mdev))
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005050 return 0;
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005051
Philipp Reisnerb411b362009-09-25 16:07:19 -07005052 drbd_rs_complete_io(mdev, sector);
5053 dec_rs_pending(mdev);
5054
Lars Ellenbergea5442a2010-11-05 09:48:01 +01005055 --mdev->ov_left;
5056
5057 /* let's advance progress step marks only for every other megabyte */
5058 if ((mdev->ov_left & 0x200) == 0x200)
5059 drbd_advance_rs_marks(mdev, mdev->ov_left);
5060
5061 if (mdev->ov_left == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07005062 w = kmalloc(sizeof(*w), GFP_NOIO);
5063 if (w) {
5064 w->cb = w_ov_finished;
Philipp Reisnera21e9292011-02-08 15:08:49 +01005065 w->mdev = mdev;
Philipp Reisnere42325a2011-01-19 13:55:45 +01005066 drbd_queue_work_front(&mdev->tconn->data.work, w);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005067 } else {
5068 dev_err(DEV, "kmalloc(w) failed.");
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01005069 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005070 drbd_resync_finished(mdev);
5071 }
5072 }
Lars Ellenberg1d53f092010-09-05 01:13:24 +02005073 put_ldev(mdev);
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005074 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005075}
5076
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005077static int got_skip(struct drbd_tconn *tconn, struct packet_info *pi)
Philipp Reisner0ced55a2010-04-30 15:26:20 +02005078{
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005079 return 0;
Philipp Reisner0ced55a2010-04-30 15:26:20 +02005080}
5081
Andreas Gruenbachera990be42011-04-06 17:56:48 +02005082static int tconn_finish_peer_reqs(struct drbd_tconn *tconn)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005083{
Philipp Reisner082a3432011-03-15 16:05:42 +01005084 struct drbd_conf *mdev;
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005085 int vnr, not_empty = 0;
Philipp Reisner32862ec2011-02-08 16:41:01 +01005086
5087 do {
5088 clear_bit(SIGNAL_ASENDER, &tconn->flags);
5089 flush_signals(current);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005090
5091 rcu_read_lock();
5092 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
5093 kref_get(&mdev->kref);
5094 rcu_read_unlock();
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07005095 if (drbd_finish_peer_reqs(mdev)) {
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005096 kref_put(&mdev->kref, &drbd_minor_destroy);
5097 return 1;
Philipp Reisnerd3fcb492011-04-13 14:46:05 -07005098 }
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005099 kref_put(&mdev->kref, &drbd_minor_destroy);
5100 rcu_read_lock();
Philipp Reisner082a3432011-03-15 16:05:42 +01005101 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005102 set_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisner082a3432011-03-15 16:05:42 +01005103
5104 spin_lock_irq(&tconn->req_lock);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005105 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisner082a3432011-03-15 16:05:42 +01005106 not_empty = !list_empty(&mdev->done_ee);
5107 if (not_empty)
5108 break;
5109 }
5110 spin_unlock_irq(&tconn->req_lock);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02005111 rcu_read_unlock();
Philipp Reisner32862ec2011-02-08 16:41:01 +01005112 } while (not_empty);
5113
5114 return 0;
5115}
5116
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005117struct asender_cmd {
5118 size_t pkt_size;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005119 int (*fn)(struct drbd_tconn *tconn, struct packet_info *);
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005120};
5121
5122static struct asender_cmd asender_tbl[] = {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005123 [P_PING] = { 0, got_Ping },
5124 [P_PING_ACK] = { 0, got_PingAck },
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005125 [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5126 [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5127 [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
5128 [P_DISCARD_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
5129 [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
5130 [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
5131 [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply },
5132 [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
5133 [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
5134 [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
5135 [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
5136 [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
5137 [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply },
5138 [P_CONN_ST_CHG_REPLY]={ sizeof(struct p_req_state_reply), got_conn_RqSReply },
5139 [P_RETRY_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005140};
5141
Philipp Reisnerb411b362009-09-25 16:07:19 -07005142int drbd_asender(struct drbd_thread *thi)
5143{
Philipp Reisner392c8802011-02-09 10:33:31 +01005144 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005145 struct asender_cmd *cmd = NULL;
Philipp Reisner77351055b2011-02-07 17:24:26 +01005146 struct packet_info pi;
Philipp Reisner257d0af2011-01-26 12:15:29 +01005147 int rv;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005148 void *buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005149 int received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005150 unsigned int header_size = drbd_header_size(tconn);
5151 int expect = header_size;
Philipp Reisner44ed1672011-04-19 17:10:19 +02005152 bool ping_timeout_active = false;
5153 struct net_conf *nc;
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005154 int ping_timeo, tcp_cork, ping_int;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005155
Philipp Reisnerb411b362009-09-25 16:07:19 -07005156 current->policy = SCHED_RR; /* Make this a realtime task! */
5157 current->rt_priority = 2; /* more important than all other tasks */
5158
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01005159 while (get_t_state(thi) == RUNNING) {
Philipp Reisner80822282011-02-08 12:46:30 +01005160 drbd_thread_current_set_cpu(thi);
Philipp Reisner44ed1672011-04-19 17:10:19 +02005161
5162 rcu_read_lock();
5163 nc = rcu_dereference(tconn->net_conf);
5164 ping_timeo = nc->ping_timeo;
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005165 tcp_cork = nc->tcp_cork;
Philipp Reisner44ed1672011-04-19 17:10:19 +02005166 ping_int = nc->ping_int;
5167 rcu_read_unlock();
5168
Philipp Reisner32862ec2011-02-08 16:41:01 +01005169 if (test_and_clear_bit(SEND_PING, &tconn->flags)) {
Andreas Gruenbachera17647a2011-04-01 12:49:42 +02005170 if (drbd_send_ping(tconn)) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005171 conn_err(tconn, "drbd_send_ping has failed\n");
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01005172 goto reconnect;
5173 }
Philipp Reisner44ed1672011-04-19 17:10:19 +02005174 tconn->meta.socket->sk->sk_rcvtimeo = ping_timeo * HZ / 10;
5175 ping_timeout_active = true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005176 }
5177
Philipp Reisner32862ec2011-02-08 16:41:01 +01005178 /* TODO: conditionally cork; it may hurt latency if we cork without
5179 much to send */
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005180 if (tcp_cork)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005181 drbd_tcp_cork(tconn->meta.socket);
Andreas Gruenbachera990be42011-04-06 17:56:48 +02005182 if (tconn_finish_peer_reqs(tconn)) {
5183 conn_err(tconn, "tconn_finish_peer_reqs() failed\n");
Philipp Reisner32862ec2011-02-08 16:41:01 +01005184 goto reconnect;
Philipp Reisner082a3432011-03-15 16:05:42 +01005185 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005186 /* but unconditionally uncork unless disabled */
Andreas Gruenbacherbb77d342011-05-04 15:25:35 +02005187 if (tcp_cork)
Philipp Reisner32862ec2011-02-08 16:41:01 +01005188 drbd_tcp_uncork(tconn->meta.socket);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005189
5190 /* short circuit, recv_msg would return EINTR anyways. */
5191 if (signal_pending(current))
5192 continue;
5193
Philipp Reisner32862ec2011-02-08 16:41:01 +01005194 rv = drbd_recv_short(tconn->meta.socket, buf, expect-received, 0);
5195 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005196
5197 flush_signals(current);
5198
5199 /* Note:
5200 * -EINTR (on meta) we got a signal
5201 * -EAGAIN (on meta) rcvtimeo expired
5202 * -ECONNRESET other side closed the connection
5203 * -ERESTARTSYS (on data) we got a signal
5204 * rv < 0 other than above: unexpected error!
5205 * rv == expected: full header or command
5206 * rv < expected: "woken" by signal during receive
5207 * rv == 0 : "connection shut down by peer"
5208 */
5209 if (likely(rv > 0)) {
5210 received += rv;
5211 buf += rv;
5212 } else if (rv == 0) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005213 conn_err(tconn, "meta connection shut down by peer.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005214 goto reconnect;
5215 } else if (rv == -EAGAIN) {
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02005216 /* If the data socket received something meanwhile,
5217 * that is good enough: peer is still alive. */
Philipp Reisner32862ec2011-02-08 16:41:01 +01005218 if (time_after(tconn->last_received,
5219 jiffies - tconn->meta.socket->sk->sk_rcvtimeo))
Lars Ellenbergcb6518c2011-06-20 14:44:45 +02005220 continue;
Lars Ellenbergf36af182011-03-09 22:44:55 +01005221 if (ping_timeout_active) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005222 conn_err(tconn, "PingAck did not arrive in time.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005223 goto reconnect;
5224 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005225 set_bit(SEND_PING, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005226 continue;
5227 } else if (rv == -EINTR) {
5228 continue;
5229 } else {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005230 conn_err(tconn, "sock_recvmsg returned %d\n", rv);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005231 goto reconnect;
5232 }
5233
5234 if (received == expect && cmd == NULL) {
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005235 if (decode_header(tconn, tconn->meta.rbuf, &pi))
Philipp Reisnerb411b362009-09-25 16:07:19 -07005236 goto reconnect;
Andreas Gruenbacher7201b972011-03-14 18:23:00 +01005237 cmd = &asender_tbl[pi.cmd];
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005238 if (pi.cmd >= ARRAY_SIZE(asender_tbl) || !cmd->fn) {
Andreas Gruenbacher2fcb8f32011-07-03 11:41:08 +02005239 conn_err(tconn, "Unexpected meta packet %s (0x%04x)\n",
5240 cmdname(pi.cmd), pi.cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005241 goto disconnect;
5242 }
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005243 expect = header_size + cmd->pkt_size;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005244 if (pi.size != expect - header_size) {
Philipp Reisner32862ec2011-02-08 16:41:01 +01005245 conn_err(tconn, "Wrong packet size on meta (c: %d, l: %d)\n",
Philipp Reisner77351055b2011-02-07 17:24:26 +01005246 pi.cmd, pi.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005247 goto reconnect;
Philipp Reisner257d0af2011-01-26 12:15:29 +01005248 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005249 }
5250 if (received == expect) {
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005251 bool err;
Philipp Reisnera4fbda82011-03-16 11:13:17 +01005252
Andreas Gruenbacher2735a592011-04-04 15:30:24 +02005253 err = cmd->fn(tconn, &pi);
5254 if (err) {
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005255 conn_err(tconn, "%pf failed\n", cmd->fn);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005256 goto reconnect;
Andreas Gruenbacher1952e912011-03-25 15:37:43 +01005257 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07005258
Philipp Reisnera4fbda82011-03-16 11:13:17 +01005259 tconn->last_received = jiffies;
5260
Philipp Reisner44ed1672011-04-19 17:10:19 +02005261 if (cmd == &asender_tbl[P_PING_ACK]) {
5262 /* restore idle timeout */
5263 tconn->meta.socket->sk->sk_rcvtimeo = ping_int * HZ;
5264 ping_timeout_active = false;
5265 }
Lars Ellenbergf36af182011-03-09 22:44:55 +01005266
Andreas Gruenbachere6589832011-03-30 12:54:42 +02005267 buf = tconn->meta.rbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005268 received = 0;
Andreas Gruenbacher52b061a2011-03-30 11:38:49 +02005269 expect = header_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07005270 cmd = NULL;
5271 }
5272 }
5273
5274 if (0) {
5275reconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01005276 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005277 }
5278 if (0) {
5279disconnect:
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01005280 conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005281 }
Philipp Reisner32862ec2011-02-08 16:41:01 +01005282 clear_bit(SIGNAL_ASENDER, &tconn->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07005283
Philipp Reisner32862ec2011-02-08 16:41:01 +01005284 conn_info(tconn, "asender terminated\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07005285
5286 return 0;
5287}